Whitespace.
[emacs.git] / src / xdisp.c
bloba13463555d6501ab82448e99d1c05a7fbfe99606
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
62 expose_window (asynchronous) |
64 X expose events -----+
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
88 Direct operations.
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
109 Desired matrices.
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
147 Frame matrices.
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "font.h"
192 #include "fontset.h"
193 #include "blockinput.h"
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef HAVE_NS
202 #include "nsterm.h"
203 #endif
204 #ifdef USE_GTK
205 #include "gtkutil.h"
206 #endif
208 #include "font.h"
210 #ifndef FRAME_X_OUTPUT
211 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
212 #endif
214 #define INFINITY 10000000
216 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
217 || defined(HAVE_NS) || defined (USE_GTK)
218 extern void set_frame_menubar P_ ((struct frame *f, int, int));
219 extern int pending_menu_activation;
220 #endif
222 extern int interrupt_input;
223 extern int command_loop_level;
225 extern Lisp_Object do_mouse_tracking;
227 extern int minibuffer_auto_raise;
228 extern Lisp_Object Vminibuffer_list;
230 extern Lisp_Object Qface;
231 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233 extern Lisp_Object Voverriding_local_map;
234 extern Lisp_Object Voverriding_local_map_menu_flag;
235 extern Lisp_Object Qmenu_item;
236 extern Lisp_Object Qwhen;
237 extern Lisp_Object Qhelp_echo;
238 extern Lisp_Object Qbefore_string, Qafter_string;
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
243 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
244 Lisp_Object Qinhibit_point_motion_hooks;
245 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
246 Lisp_Object Qfontified;
247 Lisp_Object Qgrow_only;
248 Lisp_Object Qinhibit_eval_during_redisplay;
249 Lisp_Object Qbuffer_position, Qposition, Qobject;
251 /* Cursor shapes */
252 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
254 /* Pointer shapes */
255 Lisp_Object Qarrow, Qhand, Qtext;
257 Lisp_Object Qrisky_local_variable;
259 /* Holds the list (error). */
260 Lisp_Object list_of_error;
262 /* Functions called to fontify regions of text. */
264 Lisp_Object Vfontification_functions;
265 Lisp_Object Qfontification_functions;
267 /* Non-nil means automatically select any window when the mouse
268 cursor moves into it. */
269 Lisp_Object Vmouse_autoselect_window;
271 Lisp_Object Vwrap_prefix, Qwrap_prefix;
272 Lisp_Object Vline_prefix, Qline_prefix;
274 /* Non-zero means draw tool bar buttons raised when the mouse moves
275 over them. */
277 int auto_raise_tool_bar_buttons_p;
279 /* Non-zero means to reposition window if cursor line is only partially visible. */
281 int make_cursor_line_fully_visible_p;
283 /* Margin below tool bar in pixels. 0 or nil means no margin.
284 If value is `internal-border-width' or `border-width',
285 the corresponding frame parameter is used. */
287 Lisp_Object Vtool_bar_border;
289 /* Margin around tool bar buttons in pixels. */
291 Lisp_Object Vtool_bar_button_margin;
293 /* Thickness of shadow to draw around tool bar buttons. */
295 EMACS_INT tool_bar_button_relief;
297 /* Non-nil means automatically resize tool-bars so that all tool-bar
298 items are visible, and no blank lines remain.
300 If value is `grow-only', only make tool-bar bigger. */
302 Lisp_Object Vauto_resize_tool_bars;
304 /* Non-zero means draw block and hollow cursor as wide as the glyph
305 under it. For example, if a block cursor is over a tab, it will be
306 drawn as wide as that tab on the display. */
308 int x_stretch_cursor_p;
310 /* Non-nil means don't actually do any redisplay. */
312 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
314 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
316 int inhibit_eval_during_redisplay;
318 /* Names of text properties relevant for redisplay. */
320 Lisp_Object Qdisplay;
321 extern Lisp_Object Qface, Qinvisible, Qwidth;
323 /* Symbols used in text property values. */
325 Lisp_Object Vdisplay_pixels_per_inch;
326 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
327 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
328 Lisp_Object Qslice;
329 Lisp_Object Qcenter;
330 Lisp_Object Qmargin, Qpointer;
331 Lisp_Object Qline_height;
332 extern Lisp_Object Qheight;
333 extern Lisp_Object QCwidth, QCheight, QCascent;
334 extern Lisp_Object Qscroll_bar;
335 extern Lisp_Object Qcursor;
337 /* Non-nil means highlight trailing whitespace. */
339 Lisp_Object Vshow_trailing_whitespace;
341 /* Non-nil means escape non-break space and hyphens. */
343 Lisp_Object Vnobreak_char_display;
345 #ifdef HAVE_WINDOW_SYSTEM
346 extern Lisp_Object Voverflow_newline_into_fringe;
348 /* Test if overflow newline into fringe. Called with iterator IT
349 at or past right window margin, and with IT->current_x set. */
351 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
352 (!NILP (Voverflow_newline_into_fringe) \
353 && FRAME_WINDOW_P (it->f) \
354 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
355 && it->current_x == it->last_visible_x \
356 && it->line_wrap != WORD_WRAP)
358 #else /* !HAVE_WINDOW_SYSTEM */
359 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
360 #endif /* HAVE_WINDOW_SYSTEM */
362 /* Test if the display element loaded in IT is a space or tab
363 character. This is used to determine word wrapping. */
365 #define IT_DISPLAYING_WHITESPACE(it) \
366 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
368 /* Non-nil means show the text cursor in void text areas
369 i.e. in blank areas after eol and eob. This used to be
370 the default in 21.3. */
372 Lisp_Object Vvoid_text_area_pointer;
374 /* Name of the face used to highlight trailing whitespace. */
376 Lisp_Object Qtrailing_whitespace;
378 /* Name and number of the face used to highlight escape glyphs. */
380 Lisp_Object Qescape_glyph;
382 /* Name and number of the face used to highlight non-breaking spaces. */
384 Lisp_Object Qnobreak_space;
386 /* The symbol `image' which is the car of the lists used to represent
387 images in Lisp. */
389 Lisp_Object Qimage;
391 /* The image map types. */
392 Lisp_Object QCmap, QCpointer;
393 Lisp_Object Qrect, Qcircle, Qpoly;
395 /* Non-zero means print newline to stdout before next mini-buffer
396 message. */
398 int noninteractive_need_newline;
400 /* Non-zero means print newline to message log before next message. */
402 static int message_log_need_newline;
404 /* Three markers that message_dolog uses.
405 It could allocate them itself, but that causes trouble
406 in handling memory-full errors. */
407 static Lisp_Object message_dolog_marker1;
408 static Lisp_Object message_dolog_marker2;
409 static Lisp_Object message_dolog_marker3;
411 /* The buffer position of the first character appearing entirely or
412 partially on the line of the selected window which contains the
413 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
414 redisplay optimization in redisplay_internal. */
416 static struct text_pos this_line_start_pos;
418 /* Number of characters past the end of the line above, including the
419 terminating newline. */
421 static struct text_pos this_line_end_pos;
423 /* The vertical positions and the height of this line. */
425 static int this_line_vpos;
426 static int this_line_y;
427 static int this_line_pixel_height;
429 /* X position at which this display line starts. Usually zero;
430 negative if first character is partially visible. */
432 static int this_line_start_x;
434 /* Buffer that this_line_.* variables are referring to. */
436 static struct buffer *this_line_buffer;
438 /* Nonzero means truncate lines in all windows less wide than the
439 frame. */
441 Lisp_Object Vtruncate_partial_width_windows;
443 /* A flag to control how to display unibyte 8-bit character. */
445 int unibyte_display_via_language_environment;
447 /* Nonzero means we have more than one non-mini-buffer-only frame.
448 Not guaranteed to be accurate except while parsing
449 frame-title-format. */
451 int multiple_frames;
453 Lisp_Object Vglobal_mode_string;
456 /* List of variables (symbols) which hold markers for overlay arrows.
457 The symbols on this list are examined during redisplay to determine
458 where to display overlay arrows. */
460 Lisp_Object Voverlay_arrow_variable_list;
462 /* Marker for where to display an arrow on top of the buffer text. */
464 Lisp_Object Voverlay_arrow_position;
466 /* String to display for the arrow. Only used on terminal frames. */
468 Lisp_Object Voverlay_arrow_string;
470 /* Values of those variables at last redisplay are stored as
471 properties on `overlay-arrow-position' symbol. However, if
472 Voverlay_arrow_position is a marker, last-arrow-position is its
473 numerical position. */
475 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
477 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
478 properties on a symbol in overlay-arrow-variable-list. */
480 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
482 /* Like mode-line-format, but for the title bar on a visible frame. */
484 Lisp_Object Vframe_title_format;
486 /* Like mode-line-format, but for the title bar on an iconified frame. */
488 Lisp_Object Vicon_title_format;
490 /* List of functions to call when a window's size changes. These
491 functions get one arg, a frame on which one or more windows' sizes
492 have changed. */
494 static Lisp_Object Vwindow_size_change_functions;
496 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
498 /* Nonzero if an overlay arrow has been displayed in this window. */
500 static int overlay_arrow_seen;
502 /* Nonzero means highlight the region even in nonselected windows. */
504 int highlight_nonselected_windows;
506 /* If cursor motion alone moves point off frame, try scrolling this
507 many lines up or down if that will bring it back. */
509 static EMACS_INT scroll_step;
511 /* Nonzero means scroll just far enough to bring point back on the
512 screen, when appropriate. */
514 static EMACS_INT scroll_conservatively;
516 /* Recenter the window whenever point gets within this many lines of
517 the top or bottom of the window. This value is translated into a
518 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
519 that there is really a fixed pixel height scroll margin. */
521 EMACS_INT scroll_margin;
523 /* Number of windows showing the buffer of the selected window (or
524 another buffer with the same base buffer). keyboard.c refers to
525 this. */
527 int buffer_shared;
529 /* Vector containing glyphs for an ellipsis `...'. */
531 static Lisp_Object default_invis_vector[3];
533 /* Zero means display the mode-line/header-line/menu-bar in the default face
534 (this slightly odd definition is for compatibility with previous versions
535 of emacs), non-zero means display them using their respective faces.
537 This variable is deprecated. */
539 int mode_line_inverse_video;
541 /* Prompt to display in front of the mini-buffer contents. */
543 Lisp_Object minibuf_prompt;
545 /* Width of current mini-buffer prompt. Only set after display_line
546 of the line that contains the prompt. */
548 int minibuf_prompt_width;
550 /* This is the window where the echo area message was displayed. It
551 is always a mini-buffer window, but it may not be the same window
552 currently active as a mini-buffer. */
554 Lisp_Object echo_area_window;
556 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
557 pushes the current message and the value of
558 message_enable_multibyte on the stack, the function restore_message
559 pops the stack and displays MESSAGE again. */
561 Lisp_Object Vmessage_stack;
563 /* Nonzero means multibyte characters were enabled when the echo area
564 message was specified. */
566 int message_enable_multibyte;
568 /* Nonzero if we should redraw the mode lines on the next redisplay. */
570 int update_mode_lines;
572 /* Nonzero if window sizes or contents have changed since last
573 redisplay that finished. */
575 int windows_or_buffers_changed;
577 /* Nonzero means a frame's cursor type has been changed. */
579 int cursor_type_changed;
581 /* Nonzero after display_mode_line if %l was used and it displayed a
582 line number. */
584 int line_number_displayed;
586 /* Maximum buffer size for which to display line numbers. */
588 Lisp_Object Vline_number_display_limit;
590 /* Line width to consider when repositioning for line number display. */
592 static EMACS_INT line_number_display_limit_width;
594 /* Number of lines to keep in the message log buffer. t means
595 infinite. nil means don't log at all. */
597 Lisp_Object Vmessage_log_max;
599 /* The name of the *Messages* buffer, a string. */
601 static Lisp_Object Vmessages_buffer_name;
603 /* Current, index 0, and last displayed echo area message. Either
604 buffers from echo_buffers, or nil to indicate no message. */
606 Lisp_Object echo_area_buffer[2];
608 /* The buffers referenced from echo_area_buffer. */
610 static Lisp_Object echo_buffer[2];
612 /* A vector saved used in with_area_buffer to reduce consing. */
614 static Lisp_Object Vwith_echo_area_save_vector;
616 /* Non-zero means display_echo_area should display the last echo area
617 message again. Set by redisplay_preserve_echo_area. */
619 static int display_last_displayed_message_p;
621 /* Nonzero if echo area is being used by print; zero if being used by
622 message. */
624 int message_buf_print;
626 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
628 Lisp_Object Qinhibit_menubar_update;
629 int inhibit_menubar_update;
631 /* When evaluating expressions from menu bar items (enable conditions,
632 for instance), this is the frame they are being processed for. */
634 Lisp_Object Vmenu_updating_frame;
636 /* Maximum height for resizing mini-windows. Either a float
637 specifying a fraction of the available height, or an integer
638 specifying a number of lines. */
640 Lisp_Object Vmax_mini_window_height;
642 /* Non-zero means messages should be displayed with truncated
643 lines instead of being continued. */
645 int message_truncate_lines;
646 Lisp_Object Qmessage_truncate_lines;
648 /* Set to 1 in clear_message to make redisplay_internal aware
649 of an emptied echo area. */
651 static int message_cleared_p;
653 /* How to blink the default frame cursor off. */
654 Lisp_Object Vblink_cursor_alist;
656 /* A scratch glyph row with contents used for generating truncation
657 glyphs. Also used in direct_output_for_insert. */
659 #define MAX_SCRATCH_GLYPHS 100
660 struct glyph_row scratch_glyph_row;
661 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
663 /* Ascent and height of the last line processed by move_it_to. */
665 static int last_max_ascent, last_height;
667 /* Non-zero if there's a help-echo in the echo area. */
669 int help_echo_showing_p;
671 /* If >= 0, computed, exact values of mode-line and header-line height
672 to use in the macros CURRENT_MODE_LINE_HEIGHT and
673 CURRENT_HEADER_LINE_HEIGHT. */
675 int current_mode_line_height, current_header_line_height;
677 /* The maximum distance to look ahead for text properties. Values
678 that are too small let us call compute_char_face and similar
679 functions too often which is expensive. Values that are too large
680 let us call compute_char_face and alike too often because we
681 might not be interested in text properties that far away. */
683 #define TEXT_PROP_DISTANCE_LIMIT 100
685 #if GLYPH_DEBUG
687 /* Variables to turn off display optimizations from Lisp. */
689 int inhibit_try_window_id, inhibit_try_window_reusing;
690 int inhibit_try_cursor_movement;
692 /* Non-zero means print traces of redisplay if compiled with
693 GLYPH_DEBUG != 0. */
695 int trace_redisplay_p;
697 #endif /* GLYPH_DEBUG */
699 #ifdef DEBUG_TRACE_MOVE
700 /* Non-zero means trace with TRACE_MOVE to stderr. */
701 int trace_move;
703 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
704 #else
705 #define TRACE_MOVE(x) (void) 0
706 #endif
708 /* Non-zero means automatically scroll windows horizontally to make
709 point visible. */
711 int automatic_hscrolling_p;
712 Lisp_Object Qauto_hscroll_mode;
714 /* How close to the margin can point get before the window is scrolled
715 horizontally. */
716 EMACS_INT hscroll_margin;
718 /* How much to scroll horizontally when point is inside the above margin. */
719 Lisp_Object Vhscroll_step;
721 /* The variable `resize-mini-windows'. If nil, don't resize
722 mini-windows. If t, always resize them to fit the text they
723 display. If `grow-only', let mini-windows grow only until they
724 become empty. */
726 Lisp_Object Vresize_mini_windows;
728 /* Buffer being redisplayed -- for redisplay_window_error. */
730 struct buffer *displayed_buffer;
732 /* Space between overline and text. */
734 EMACS_INT overline_margin;
736 /* Require underline to be at least this many screen pixels below baseline
737 This to avoid underline "merging" with the base of letters at small
738 font sizes, particularly when x_use_underline_position_properties is on. */
740 EMACS_INT underline_minimum_offset;
742 /* Value returned from text property handlers (see below). */
744 enum prop_handled
746 HANDLED_NORMALLY,
747 HANDLED_RECOMPUTE_PROPS,
748 HANDLED_OVERLAY_STRING_CONSUMED,
749 HANDLED_RETURN
752 /* A description of text properties that redisplay is interested
753 in. */
755 struct props
757 /* The name of the property. */
758 Lisp_Object *name;
760 /* A unique index for the property. */
761 enum prop_idx idx;
763 /* A handler function called to set up iterator IT from the property
764 at IT's current position. Value is used to steer handle_stop. */
765 enum prop_handled (*handler) P_ ((struct it *it));
768 static enum prop_handled handle_face_prop P_ ((struct it *));
769 static enum prop_handled handle_invisible_prop P_ ((struct it *));
770 static enum prop_handled handle_display_prop P_ ((struct it *));
771 static enum prop_handled handle_composition_prop P_ ((struct it *));
772 static enum prop_handled handle_overlay_change P_ ((struct it *));
773 static enum prop_handled handle_fontified_prop P_ ((struct it *));
775 /* Properties handled by iterators. */
777 static struct props it_props[] =
779 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
780 /* Handle `face' before `display' because some sub-properties of
781 `display' need to know the face. */
782 {&Qface, FACE_PROP_IDX, handle_face_prop},
783 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
784 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
785 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
786 {NULL, 0, NULL}
789 /* Value is the position described by X. If X is a marker, value is
790 the marker_position of X. Otherwise, value is X. */
792 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
794 /* Enumeration returned by some move_it_.* functions internally. */
796 enum move_it_result
798 /* Not used. Undefined value. */
799 MOVE_UNDEFINED,
801 /* Move ended at the requested buffer position or ZV. */
802 MOVE_POS_MATCH_OR_ZV,
804 /* Move ended at the requested X pixel position. */
805 MOVE_X_REACHED,
807 /* Move within a line ended at the end of a line that must be
808 continued. */
809 MOVE_LINE_CONTINUED,
811 /* Move within a line ended at the end of a line that would
812 be displayed truncated. */
813 MOVE_LINE_TRUNCATED,
815 /* Move within a line ended at a line end. */
816 MOVE_NEWLINE_OR_CR
819 /* This counter is used to clear the face cache every once in a while
820 in redisplay_internal. It is incremented for each redisplay.
821 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
822 cleared. */
824 #define CLEAR_FACE_CACHE_COUNT 500
825 static int clear_face_cache_count;
827 /* Similarly for the image cache. */
829 #ifdef HAVE_WINDOW_SYSTEM
830 #define CLEAR_IMAGE_CACHE_COUNT 101
831 static int clear_image_cache_count;
832 #endif
834 /* Non-zero while redisplay_internal is in progress. */
836 int redisplaying_p;
838 /* Non-zero means don't free realized faces. Bound while freeing
839 realized faces is dangerous because glyph matrices might still
840 reference them. */
842 int inhibit_free_realized_faces;
843 Lisp_Object Qinhibit_free_realized_faces;
845 /* If a string, XTread_socket generates an event to display that string.
846 (The display is done in read_char.) */
848 Lisp_Object help_echo_string;
849 Lisp_Object help_echo_window;
850 Lisp_Object help_echo_object;
851 int help_echo_pos;
853 /* Temporary variable for XTread_socket. */
855 Lisp_Object previous_help_echo_string;
857 /* Null glyph slice */
859 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
861 /* Platform-independent portion of hourglass implementation. */
863 /* Non-zero means we're allowed to display a hourglass pointer. */
864 int display_hourglass_p;
866 /* Non-zero means an hourglass cursor is currently shown. */
867 int hourglass_shown_p;
869 /* If non-null, an asynchronous timer that, when it expires, displays
870 an hourglass cursor on all frames. */
871 struct atimer *hourglass_atimer;
873 /* Number of seconds to wait before displaying an hourglass cursor. */
874 Lisp_Object Vhourglass_delay;
876 /* Default number of seconds to wait before displaying an hourglass
877 cursor. */
878 #define DEFAULT_HOURGLASS_DELAY 1
881 /* Function prototypes. */
883 static void setup_for_ellipsis P_ ((struct it *, int));
884 static void mark_window_display_accurate_1 P_ ((struct window *, int));
885 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
886 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
887 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
888 static int redisplay_mode_lines P_ ((Lisp_Object, int));
889 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
891 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
893 static void handle_line_prefix P_ ((struct it *));
895 static void pint2str P_ ((char *, int, int));
896 static void pint2hrstr P_ ((char *, int, int));
897 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
898 struct text_pos));
899 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
900 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
901 static void store_mode_line_noprop_char P_ ((char));
902 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
903 static void x_consider_frame_title P_ ((Lisp_Object));
904 static void handle_stop P_ ((struct it *));
905 static int tool_bar_lines_needed P_ ((struct frame *, int *));
906 static int single_display_spec_intangible_p P_ ((Lisp_Object));
907 static void ensure_echo_area_buffers P_ ((void));
908 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
909 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
910 static int with_echo_area_buffer P_ ((struct window *, int,
911 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
912 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static void clear_garbaged_frames P_ ((void));
914 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int display_echo_area P_ ((struct window *));
918 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
921 static int string_char_and_length P_ ((const unsigned char *, int, int *));
922 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
923 struct text_pos));
924 static int compute_window_start_on_continuation_line P_ ((struct window *));
925 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
926 static void insert_left_trunc_glyphs P_ ((struct it *));
927 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
928 Lisp_Object));
929 static void extend_face_to_end_of_line P_ ((struct it *));
930 static int append_space_for_newline P_ ((struct it *, int));
931 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
932 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
933 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
934 static int trailing_whitespace_p P_ ((int));
935 static int message_log_check_duplicate P_ ((int, int, int, int));
936 static void push_it P_ ((struct it *));
937 static void pop_it P_ ((struct it *));
938 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
939 static void select_frame_for_redisplay P_ ((Lisp_Object));
940 static void redisplay_internal P_ ((int));
941 static int echo_area_display P_ ((int));
942 static void redisplay_windows P_ ((Lisp_Object));
943 static void redisplay_window P_ ((Lisp_Object, int));
944 static Lisp_Object redisplay_window_error ();
945 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
946 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
947 static int update_menu_bar P_ ((struct frame *, int, int));
948 static int try_window_reusing_current_matrix P_ ((struct window *));
949 static int try_window_id P_ ((struct window *));
950 static int display_line P_ ((struct it *));
951 static int display_mode_lines P_ ((struct window *));
952 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
953 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
954 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
955 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
956 static void display_menu_bar P_ ((struct window *));
957 static int display_count_lines P_ ((int, int, int, int, int *));
958 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
959 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
960 static void compute_line_metrics P_ ((struct it *));
961 static void run_redisplay_end_trigger_hook P_ ((struct it *));
962 static int get_overlay_strings P_ ((struct it *, int));
963 static int get_overlay_strings_1 P_ ((struct it *, int, int));
964 static void next_overlay_string P_ ((struct it *));
965 static void reseat P_ ((struct it *, struct text_pos, int));
966 static void reseat_1 P_ ((struct it *, struct text_pos, int));
967 static void back_to_previous_visible_line_start P_ ((struct it *));
968 void reseat_at_previous_visible_line_start P_ ((struct it *));
969 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
970 static int next_element_from_ellipsis P_ ((struct it *));
971 static int next_element_from_display_vector P_ ((struct it *));
972 static int next_element_from_string P_ ((struct it *));
973 static int next_element_from_c_string P_ ((struct it *));
974 static int next_element_from_buffer P_ ((struct it *));
975 static int next_element_from_composition P_ ((struct it *));
976 static int next_element_from_image P_ ((struct it *));
977 static int next_element_from_stretch P_ ((struct it *));
978 static void load_overlay_strings P_ ((struct it *, int));
979 static int init_from_display_pos P_ ((struct it *, struct window *,
980 struct display_pos *));
981 static void reseat_to_string P_ ((struct it *, unsigned char *,
982 Lisp_Object, int, int, int, int));
983 static enum move_it_result
984 move_it_in_display_line_to (struct it *, EMACS_INT, int,
985 enum move_operation_enum);
986 void move_it_vertically_backward P_ ((struct it *, int));
987 static void init_to_row_start P_ ((struct it *, struct window *,
988 struct glyph_row *));
989 static int init_to_row_end P_ ((struct it *, struct window *,
990 struct glyph_row *));
991 static void back_to_previous_line_start P_ ((struct it *));
992 static int forward_to_next_line_start P_ ((struct it *, int *));
993 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
994 Lisp_Object, int));
995 static struct text_pos string_pos P_ ((int, Lisp_Object));
996 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
997 static int number_of_chars P_ ((unsigned char *, int));
998 static void compute_stop_pos P_ ((struct it *));
999 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1000 Lisp_Object));
1001 static int face_before_or_after_it_pos P_ ((struct it *, int));
1002 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1003 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1004 Lisp_Object, Lisp_Object,
1005 struct text_pos *, int));
1006 static int underlying_face_id P_ ((struct it *));
1007 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1008 struct window *));
1010 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1011 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1013 #ifdef HAVE_WINDOW_SYSTEM
1015 static void update_tool_bar P_ ((struct frame *, int));
1016 static void build_desired_tool_bar_string P_ ((struct frame *f));
1017 static int redisplay_tool_bar P_ ((struct frame *));
1018 static void display_tool_bar_line P_ ((struct it *, int));
1019 static void notice_overwritten_cursor P_ ((struct window *,
1020 enum glyph_row_area,
1021 int, int, int, int));
1025 #endif /* HAVE_WINDOW_SYSTEM */
1028 /***********************************************************************
1029 Window display dimensions
1030 ***********************************************************************/
1032 /* Return the bottom boundary y-position for text lines in window W.
1033 This is the first y position at which a line cannot start.
1034 It is relative to the top of the window.
1036 This is the height of W minus the height of a mode line, if any. */
1038 INLINE int
1039 window_text_bottom_y (w)
1040 struct window *w;
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1044 if (WINDOW_WANTS_MODELINE_P (w))
1045 height -= CURRENT_MODE_LINE_HEIGHT (w);
1046 return height;
1049 /* Return the pixel width of display area AREA of window W. AREA < 0
1050 means return the total width of W, not including fringes to
1051 the left and right of the window. */
1053 INLINE int
1054 window_box_width (w, area)
1055 struct window *w;
1056 int area;
1058 int cols = XFASTINT (w->total_cols);
1059 int pixels = 0;
1061 if (!w->pseudo_window_p)
1063 cols -= WINDOW_SCROLL_BAR_COLS (w);
1065 if (area == TEXT_AREA)
1067 if (INTEGERP (w->left_margin_cols))
1068 cols -= XFASTINT (w->left_margin_cols);
1069 if (INTEGERP (w->right_margin_cols))
1070 cols -= XFASTINT (w->right_margin_cols);
1071 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1073 else if (area == LEFT_MARGIN_AREA)
1075 cols = (INTEGERP (w->left_margin_cols)
1076 ? XFASTINT (w->left_margin_cols) : 0);
1077 pixels = 0;
1079 else if (area == RIGHT_MARGIN_AREA)
1081 cols = (INTEGERP (w->right_margin_cols)
1082 ? XFASTINT (w->right_margin_cols) : 0);
1083 pixels = 0;
1087 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1091 /* Return the pixel height of the display area of window W, not
1092 including mode lines of W, if any. */
1094 INLINE int
1095 window_box_height (w)
1096 struct window *w;
1098 struct frame *f = XFRAME (w->frame);
1099 int height = WINDOW_TOTAL_HEIGHT (w);
1101 xassert (height >= 0);
1103 /* Note: the code below that determines the mode-line/header-line
1104 height is essentially the same as that contained in the macro
1105 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1106 the appropriate glyph row has its `mode_line_p' flag set,
1107 and if it doesn't, uses estimate_mode_line_height instead. */
1109 if (WINDOW_WANTS_MODELINE_P (w))
1111 struct glyph_row *ml_row
1112 = (w->current_matrix && w->current_matrix->rows
1113 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1114 : 0);
1115 if (ml_row && ml_row->mode_line_p)
1116 height -= ml_row->height;
1117 else
1118 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1121 if (WINDOW_WANTS_HEADER_LINE_P (w))
1123 struct glyph_row *hl_row
1124 = (w->current_matrix && w->current_matrix->rows
1125 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1126 : 0);
1127 if (hl_row && hl_row->mode_line_p)
1128 height -= hl_row->height;
1129 else
1130 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1133 /* With a very small font and a mode-line that's taller than
1134 default, we might end up with a negative height. */
1135 return max (0, height);
1138 /* Return the window-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1142 INLINE int
1143 window_box_left_offset (w, area)
1144 struct window *w;
1145 int area;
1147 int x;
1149 if (w->pseudo_window_p)
1150 return 0;
1152 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1154 if (area == TEXT_AREA)
1155 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1156 + window_box_width (w, LEFT_MARGIN_AREA));
1157 else if (area == RIGHT_MARGIN_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA)
1160 + window_box_width (w, TEXT_AREA)
1161 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1163 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1164 else if (area == LEFT_MARGIN_AREA
1165 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1166 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1168 return x;
1172 /* Return the window-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1176 INLINE int
1177 window_box_right_offset (w, area)
1178 struct window *w;
1179 int area;
1181 return window_box_left_offset (w, area) + window_box_width (w, area);
1184 /* Return the frame-relative coordinate of the left edge of display
1185 area AREA of window W. AREA < 0 means return the left edge of the
1186 whole window, to the right of the left fringe of W. */
1188 INLINE int
1189 window_box_left (w, area)
1190 struct window *w;
1191 int area;
1193 struct frame *f = XFRAME (w->frame);
1194 int x;
1196 if (w->pseudo_window_p)
1197 return FRAME_INTERNAL_BORDER_WIDTH (f);
1199 x = (WINDOW_LEFT_EDGE_X (w)
1200 + window_box_left_offset (w, area));
1202 return x;
1206 /* Return the frame-relative coordinate of the right edge of display
1207 area AREA of window W. AREA < 0 means return the left edge of the
1208 whole window, to the left of the right fringe of W. */
1210 INLINE int
1211 window_box_right (w, area)
1212 struct window *w;
1213 int area;
1215 return window_box_left (w, area) + window_box_width (w, area);
1218 /* Get the bounding box of the display area AREA of window W, without
1219 mode lines, in frame-relative coordinates. AREA < 0 means the
1220 whole window, not including the left and right fringes of
1221 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1222 coordinates of the upper-left corner of the box. Return in
1223 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1225 INLINE void
1226 window_box (w, area, box_x, box_y, box_width, box_height)
1227 struct window *w;
1228 int area;
1229 int *box_x, *box_y, *box_width, *box_height;
1231 if (box_width)
1232 *box_width = window_box_width (w, area);
1233 if (box_height)
1234 *box_height = window_box_height (w);
1235 if (box_x)
1236 *box_x = window_box_left (w, area);
1237 if (box_y)
1239 *box_y = WINDOW_TOP_EDGE_Y (w);
1240 if (WINDOW_WANTS_HEADER_LINE_P (w))
1241 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1246 /* Get the bounding box of the display area AREA of window W, without
1247 mode lines. AREA < 0 means the whole window, not including the
1248 left and right fringe of the window. Return in *TOP_LEFT_X
1249 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1250 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1251 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1252 box. */
1254 INLINE void
1255 window_box_edges (w, area, top_left_x, top_left_y,
1256 bottom_right_x, bottom_right_y)
1257 struct window *w;
1258 int area;
1259 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1261 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1262 bottom_right_y);
1263 *bottom_right_x += *top_left_x;
1264 *bottom_right_y += *top_left_y;
1269 /***********************************************************************
1270 Utilities
1271 ***********************************************************************/
1273 /* Return the bottom y-position of the line the iterator IT is in.
1274 This can modify IT's settings. */
1277 line_bottom_y (it)
1278 struct it *it;
1280 int line_height = it->max_ascent + it->max_descent;
1281 int line_top_y = it->current_y;
1283 if (line_height == 0)
1285 if (last_height)
1286 line_height = last_height;
1287 else if (IT_CHARPOS (*it) < ZV)
1289 move_it_by_lines (it, 1, 1);
1290 line_height = (it->max_ascent || it->max_descent
1291 ? it->max_ascent + it->max_descent
1292 : last_height);
1294 else
1296 struct glyph_row *row = it->glyph_row;
1298 /* Use the default character height. */
1299 it->glyph_row = NULL;
1300 it->what = IT_CHARACTER;
1301 it->c = ' ';
1302 it->len = 1;
1303 PRODUCE_GLYPHS (it);
1304 line_height = it->ascent + it->descent;
1305 it->glyph_row = row;
1309 return line_top_y + line_height;
1313 /* Return 1 if position CHARPOS is visible in window W.
1314 CHARPOS < 0 means return info about WINDOW_END position.
1315 If visible, set *X and *Y to pixel coordinates of top left corner.
1316 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1317 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1320 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1321 struct window *w;
1322 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1324 struct it it;
1325 struct text_pos top;
1326 int visible_p = 0;
1327 struct buffer *old_buffer = NULL;
1329 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1330 return visible_p;
1332 if (XBUFFER (w->buffer) != current_buffer)
1334 old_buffer = current_buffer;
1335 set_buffer_internal_1 (XBUFFER (w->buffer));
1338 SET_TEXT_POS_FROM_MARKER (top, w->start);
1340 /* Compute exact mode line heights. */
1341 if (WINDOW_WANTS_MODELINE_P (w))
1342 current_mode_line_height
1343 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1344 current_buffer->mode_line_format);
1346 if (WINDOW_WANTS_HEADER_LINE_P (w))
1347 current_header_line_height
1348 = display_mode_line (w, HEADER_LINE_FACE_ID,
1349 current_buffer->header_line_format);
1351 start_display (&it, w, top);
1352 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1353 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1355 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1357 /* We have reached CHARPOS, or passed it. How the call to
1358 move_it_to can overshoot: (i) If CHARPOS is on invisible
1359 text, move_it_to stops at the end of the invisible text,
1360 after CHARPOS. (ii) If CHARPOS is in a display vector,
1361 move_it_to stops on its last glyph. */
1362 int top_x = it.current_x;
1363 int top_y = it.current_y;
1364 enum it_method it_method = it.method;
1365 /* Calling line_bottom_y may change it.method. */
1366 int bottom_y = (last_height = 0, line_bottom_y (&it));
1367 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1369 if (top_y < window_top_y)
1370 visible_p = bottom_y > window_top_y;
1371 else if (top_y < it.last_visible_y)
1372 visible_p = 1;
1373 if (visible_p)
1375 if (it_method == GET_FROM_BUFFER)
1377 Lisp_Object window, prop;
1379 XSETWINDOW (window, w);
1380 prop = Fget_char_property (make_number (it.position.charpos),
1381 Qinvisible, window);
1383 /* If charpos coincides with invisible text covered with an
1384 ellipsis, use the first glyph of the ellipsis to compute
1385 the pixel positions. */
1386 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1388 struct glyph_row *row = it.glyph_row;
1389 struct glyph *glyph = row->glyphs[TEXT_AREA];
1390 struct glyph *end = glyph + row->used[TEXT_AREA];
1391 int x = row->x;
1393 for (; glyph < end
1394 && (!BUFFERP (glyph->object)
1395 || glyph->charpos < charpos);
1396 glyph++)
1397 x += glyph->pixel_width;
1398 top_x = x;
1401 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1409 struct it it2;
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 get_next_display_element (&it2);
1413 PRODUCE_GLYPHS (&it2);
1414 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1415 || it2.current_x > it2.last_visible_x)
1416 top_x = it.glyph_row->x;
1417 else
1419 top_x = it2.current_x;
1420 top_y = it2.current_y;
1425 *x = top_x;
1426 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1427 *rtop = max (0, window_top_y - top_y);
1428 *rbot = max (0, bottom_y - it.last_visible_y);
1429 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1430 - max (top_y, window_top_y)));
1431 *vpos = it.vpos;
1434 else
1436 struct it it2;
1438 it2 = it;
1439 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1440 move_it_by_lines (&it, 1, 0);
1441 if (charpos < IT_CHARPOS (it)
1442 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1444 visible_p = 1;
1445 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1446 *x = it2.current_x;
1447 *y = it2.current_y + it2.max_ascent - it2.ascent;
1448 *rtop = max (0, -it2.current_y);
1449 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1450 - it.last_visible_y));
1451 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1452 it.last_visible_y)
1453 - max (it2.current_y,
1454 WINDOW_HEADER_LINE_HEIGHT (w))));
1455 *vpos = it2.vpos;
1459 if (old_buffer)
1460 set_buffer_internal_1 (old_buffer);
1462 current_header_line_height = current_mode_line_height = -1;
1464 if (visible_p && XFASTINT (w->hscroll) > 0)
1465 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1467 #if 0
1468 /* Debugging code. */
1469 if (visible_p)
1470 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1471 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1472 else
1473 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1474 #endif
1476 return visible_p;
1480 /* Return the next character from STR which is MAXLEN bytes long.
1481 Return in *LEN the length of the character. This is like
1482 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1483 we find one, we return a `?', but with the length of the invalid
1484 character. */
1486 static INLINE int
1487 string_char_and_length (str, maxlen, len)
1488 const unsigned char *str;
1489 int maxlen, *len;
1491 int c;
1493 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1494 if (!CHAR_VALID_P (c, 1))
1495 /* We may not change the length here because other places in Emacs
1496 don't use this function, i.e. they silently accept invalid
1497 characters. */
1498 c = '?';
1500 return c;
1505 /* Given a position POS containing a valid character and byte position
1506 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1508 static struct text_pos
1509 string_pos_nchars_ahead (pos, string, nchars)
1510 struct text_pos pos;
1511 Lisp_Object string;
1512 int nchars;
1514 xassert (STRINGP (string) && nchars >= 0);
1516 if (STRING_MULTIBYTE (string))
1518 int rest = SBYTES (string) - BYTEPOS (pos);
1519 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1520 int len;
1522 while (nchars--)
1524 string_char_and_length (p, rest, &len);
1525 p += len, rest -= len;
1526 xassert (rest >= 0);
1527 CHARPOS (pos) += 1;
1528 BYTEPOS (pos) += len;
1531 else
1532 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1534 return pos;
1538 /* Value is the text position, i.e. character and byte position,
1539 for character position CHARPOS in STRING. */
1541 static INLINE struct text_pos
1542 string_pos (charpos, string)
1543 int charpos;
1544 Lisp_Object string;
1546 struct text_pos pos;
1547 xassert (STRINGP (string));
1548 xassert (charpos >= 0);
1549 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1550 return pos;
1554 /* Value is a text position, i.e. character and byte position, for
1555 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1556 means recognize multibyte characters. */
1558 static struct text_pos
1559 c_string_pos (charpos, s, multibyte_p)
1560 int charpos;
1561 unsigned char *s;
1562 int multibyte_p;
1564 struct text_pos pos;
1566 xassert (s != NULL);
1567 xassert (charpos >= 0);
1569 if (multibyte_p)
1571 int rest = strlen (s), len;
1573 SET_TEXT_POS (pos, 0, 0);
1574 while (charpos--)
1576 string_char_and_length (s, rest, &len);
1577 s += len, rest -= len;
1578 xassert (rest >= 0);
1579 CHARPOS (pos) += 1;
1580 BYTEPOS (pos) += len;
1583 else
1584 SET_TEXT_POS (pos, charpos, charpos);
1586 return pos;
1590 /* Value is the number of characters in C string S. MULTIBYTE_P
1591 non-zero means recognize multibyte characters. */
1593 static int
1594 number_of_chars (s, multibyte_p)
1595 unsigned char *s;
1596 int multibyte_p;
1598 int nchars;
1600 if (multibyte_p)
1602 int rest = strlen (s), len;
1603 unsigned char *p = (unsigned char *) s;
1605 for (nchars = 0; rest > 0; ++nchars)
1607 string_char_and_length (p, rest, &len);
1608 rest -= len, p += len;
1611 else
1612 nchars = strlen (s);
1614 return nchars;
1618 /* Compute byte position NEWPOS->bytepos corresponding to
1619 NEWPOS->charpos. POS is a known position in string STRING.
1620 NEWPOS->charpos must be >= POS.charpos. */
1622 static void
1623 compute_string_pos (newpos, pos, string)
1624 struct text_pos *newpos, pos;
1625 Lisp_Object string;
1627 xassert (STRINGP (string));
1628 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1630 if (STRING_MULTIBYTE (string))
1631 *newpos = string_pos_nchars_ahead (pos, string,
1632 CHARPOS (*newpos) - CHARPOS (pos));
1633 else
1634 BYTEPOS (*newpos) = CHARPOS (*newpos);
1637 /* EXPORT:
1638 Return an estimation of the pixel height of mode or top lines on
1639 frame F. FACE_ID specifies what line's height to estimate. */
1642 estimate_mode_line_height (f, face_id)
1643 struct frame *f;
1644 enum face_id face_id;
1646 #ifdef HAVE_WINDOW_SYSTEM
1647 if (FRAME_WINDOW_P (f))
1649 int height = FONT_HEIGHT (FRAME_FONT (f));
1651 /* This function is called so early when Emacs starts that the face
1652 cache and mode line face are not yet initialized. */
1653 if (FRAME_FACE_CACHE (f))
1655 struct face *face = FACE_FROM_ID (f, face_id);
1656 if (face)
1658 if (face->font)
1659 height = FONT_HEIGHT (face->font);
1660 if (face->box_line_width > 0)
1661 height += 2 * face->box_line_width;
1665 return height;
1667 #endif
1669 return 1;
1672 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1673 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1674 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1675 not force the value into range. */
1677 void
1678 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1679 FRAME_PTR f;
1680 register int pix_x, pix_y;
1681 int *x, *y;
1682 NativeRectangle *bounds;
1683 int noclip;
1686 #ifdef HAVE_WINDOW_SYSTEM
1687 if (FRAME_WINDOW_P (f))
1689 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1690 even for negative values. */
1691 if (pix_x < 0)
1692 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1693 if (pix_y < 0)
1694 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1696 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1697 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1699 if (bounds)
1700 STORE_NATIVE_RECT (*bounds,
1701 FRAME_COL_TO_PIXEL_X (f, pix_x),
1702 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1703 FRAME_COLUMN_WIDTH (f) - 1,
1704 FRAME_LINE_HEIGHT (f) - 1);
1706 if (!noclip)
1708 if (pix_x < 0)
1709 pix_x = 0;
1710 else if (pix_x > FRAME_TOTAL_COLS (f))
1711 pix_x = FRAME_TOTAL_COLS (f);
1713 if (pix_y < 0)
1714 pix_y = 0;
1715 else if (pix_y > FRAME_LINES (f))
1716 pix_y = FRAME_LINES (f);
1719 #endif
1721 *x = pix_x;
1722 *y = pix_y;
1726 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1727 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1728 can't tell the positions because W's display is not up to date,
1729 return 0. */
1732 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1733 struct window *w;
1734 int hpos, vpos;
1735 int *frame_x, *frame_y;
1737 #ifdef HAVE_WINDOW_SYSTEM
1738 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1740 int success_p;
1742 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1743 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1745 if (display_completed)
1747 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1748 struct glyph *glyph = row->glyphs[TEXT_AREA];
1749 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1751 hpos = row->x;
1752 vpos = row->y;
1753 while (glyph < end)
1755 hpos += glyph->pixel_width;
1756 ++glyph;
1759 /* If first glyph is partially visible, its first visible position is still 0. */
1760 if (hpos < 0)
1761 hpos = 0;
1763 success_p = 1;
1765 else
1767 hpos = vpos = 0;
1768 success_p = 0;
1771 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1772 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1773 return success_p;
1775 #endif
1777 *frame_x = hpos;
1778 *frame_y = vpos;
1779 return 1;
1783 #ifdef HAVE_WINDOW_SYSTEM
1785 /* Find the glyph under window-relative coordinates X/Y in window W.
1786 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1787 strings. Return in *HPOS and *VPOS the row and column number of
1788 the glyph found. Return in *AREA the glyph area containing X.
1789 Value is a pointer to the glyph found or null if X/Y is not on
1790 text, or we can't tell because W's current matrix is not up to
1791 date. */
1793 static
1794 struct glyph *
1795 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1796 struct window *w;
1797 int x, y;
1798 int *hpos, *vpos, *dx, *dy, *area;
1800 struct glyph *glyph, *end;
1801 struct glyph_row *row = NULL;
1802 int x0, i;
1804 /* Find row containing Y. Give up if some row is not enabled. */
1805 for (i = 0; i < w->current_matrix->nrows; ++i)
1807 row = MATRIX_ROW (w->current_matrix, i);
1808 if (!row->enabled_p)
1809 return NULL;
1810 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1811 break;
1814 *vpos = i;
1815 *hpos = 0;
1817 /* Give up if Y is not in the window. */
1818 if (i == w->current_matrix->nrows)
1819 return NULL;
1821 /* Get the glyph area containing X. */
1822 if (w->pseudo_window_p)
1824 *area = TEXT_AREA;
1825 x0 = 0;
1827 else
1829 if (x < window_box_left_offset (w, TEXT_AREA))
1831 *area = LEFT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1834 else if (x < window_box_right_offset (w, TEXT_AREA))
1836 *area = TEXT_AREA;
1837 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1839 else
1841 *area = RIGHT_MARGIN_AREA;
1842 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1846 /* Find glyph containing X. */
1847 glyph = row->glyphs[*area];
1848 end = glyph + row->used[*area];
1849 x -= x0;
1850 while (glyph < end && x >= glyph->pixel_width)
1852 x -= glyph->pixel_width;
1853 ++glyph;
1856 if (glyph == end)
1857 return NULL;
1859 if (dx)
1861 *dx = x;
1862 *dy = y - (row->y + row->ascent - glyph->ascent);
1865 *hpos = glyph - row->glyphs[*area];
1866 return glyph;
1870 /* EXPORT:
1871 Convert frame-relative x/y to coordinates relative to window W.
1872 Takes pseudo-windows into account. */
1874 void
1875 frame_to_window_pixel_xy (w, x, y)
1876 struct window *w;
1877 int *x, *y;
1879 if (w->pseudo_window_p)
1881 /* A pseudo-window is always full-width, and starts at the
1882 left edge of the frame, plus a frame border. */
1883 struct frame *f = XFRAME (w->frame);
1884 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1885 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1887 else
1889 *x -= WINDOW_LEFT_EDGE_X (w);
1890 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1894 /* EXPORT:
1895 Return in RECTS[] at most N clipping rectangles for glyph string S.
1896 Return the number of stored rectangles. */
1899 get_glyph_string_clip_rects (s, rects, n)
1900 struct glyph_string *s;
1901 NativeRectangle *rects;
1902 int n;
1904 XRectangle r;
1906 if (n <= 0)
1907 return 0;
1909 if (s->row->full_width_p)
1911 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1912 r.x = WINDOW_LEFT_EDGE_X (s->w);
1913 r.width = WINDOW_TOTAL_WIDTH (s->w);
1915 /* Unless displaying a mode or menu bar line, which are always
1916 fully visible, clip to the visible part of the row. */
1917 if (s->w->pseudo_window_p)
1918 r.height = s->row->visible_height;
1919 else
1920 r.height = s->height;
1922 else
1924 /* This is a text line that may be partially visible. */
1925 r.x = window_box_left (s->w, s->area);
1926 r.width = window_box_width (s->w, s->area);
1927 r.height = s->row->visible_height;
1930 if (s->clip_head)
1931 if (r.x < s->clip_head->x)
1933 if (r.width >= s->clip_head->x - r.x)
1934 r.width -= s->clip_head->x - r.x;
1935 else
1936 r.width = 0;
1937 r.x = s->clip_head->x;
1939 if (s->clip_tail)
1940 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1942 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1943 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1944 else
1945 r.width = 0;
1948 /* If S draws overlapping rows, it's sufficient to use the top and
1949 bottom of the window for clipping because this glyph string
1950 intentionally draws over other lines. */
1951 if (s->for_overlaps)
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 r.height = window_text_bottom_y (s->w) - r.y;
1956 /* Alas, the above simple strategy does not work for the
1957 environments with anti-aliased text: if the same text is
1958 drawn onto the same place multiple times, it gets thicker.
1959 If the overlap we are processing is for the erased cursor, we
1960 take the intersection with the rectagle of the cursor. */
1961 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1963 XRectangle rc, r_save = r;
1965 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1966 rc.y = s->w->phys_cursor.y;
1967 rc.width = s->w->phys_cursor_width;
1968 rc.height = s->w->phys_cursor_height;
1970 x_intersect_rectangles (&r_save, &rc, &r);
1973 else
1975 /* Don't use S->y for clipping because it doesn't take partially
1976 visible lines into account. For example, it can be negative for
1977 partially visible lines at the top of a window. */
1978 if (!s->row->full_width_p
1979 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 else
1982 r.y = max (0, s->row->y);
1984 /* If drawing a tool-bar window, draw it over the internal border
1985 at the top of the window. */
1986 if (WINDOWP (s->f->tool_bar_window)
1987 && s->w == XWINDOW (s->f->tool_bar_window))
1988 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1991 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1993 /* If drawing the cursor, don't let glyph draw outside its
1994 advertised boundaries. Cleartype does this under some circumstances. */
1995 if (s->hl == DRAW_CURSOR)
1997 struct glyph *glyph = s->first_glyph;
1998 int height, max_y;
2000 if (s->x > r.x)
2002 r.width -= s->x - r.x;
2003 r.x = s->x;
2005 r.width = min (r.width, glyph->pixel_width);
2007 /* If r.y is below window bottom, ensure that we still see a cursor. */
2008 height = min (glyph->ascent + glyph->descent,
2009 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2010 max_y = window_text_bottom_y (s->w) - height;
2011 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2012 if (s->ybase - glyph->ascent > max_y)
2014 r.y = max_y;
2015 r.height = height;
2017 else
2019 /* Don't draw cursor glyph taller than our actual glyph. */
2020 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2021 if (height < r.height)
2023 max_y = r.y + r.height;
2024 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2025 r.height = min (max_y - r.y, height);
2030 if (s->row->clip)
2032 XRectangle r_save = r;
2034 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2035 r.width = 0;
2038 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2039 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2041 #ifdef CONVERT_FROM_XRECT
2042 CONVERT_FROM_XRECT (r, *rects);
2043 #else
2044 *rects = r;
2045 #endif
2046 return 1;
2048 else
2050 /* If we are processing overlapping and allowed to return
2051 multiple clipping rectangles, we exclude the row of the glyph
2052 string from the clipping rectangle. This is to avoid drawing
2053 the same text on the environment with anti-aliasing. */
2054 #ifdef CONVERT_FROM_XRECT
2055 XRectangle rs[2];
2056 #else
2057 XRectangle *rs = rects;
2058 #endif
2059 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2061 if (s->for_overlaps & OVERLAPS_PRED)
2063 rs[i] = r;
2064 if (r.y + r.height > row_y)
2066 if (r.y < row_y)
2067 rs[i].height = row_y - r.y;
2068 else
2069 rs[i].height = 0;
2071 i++;
2073 if (s->for_overlaps & OVERLAPS_SUCC)
2075 rs[i] = r;
2076 if (r.y < row_y + s->row->visible_height)
2078 if (r.y + r.height > row_y + s->row->visible_height)
2080 rs[i].y = row_y + s->row->visible_height;
2081 rs[i].height = r.y + r.height - rs[i].y;
2083 else
2084 rs[i].height = 0;
2086 i++;
2089 n = i;
2090 #ifdef CONVERT_FROM_XRECT
2091 for (i = 0; i < n; i++)
2092 CONVERT_FROM_XRECT (rs[i], rects[i]);
2093 #endif
2094 return n;
2098 /* EXPORT:
2099 Return in *NR the clipping rectangle for glyph string S. */
2101 void
2102 get_glyph_string_clip_rect (s, nr)
2103 struct glyph_string *s;
2104 NativeRectangle *nr;
2106 get_glyph_string_clip_rects (s, nr, 1);
2110 /* EXPORT:
2111 Return the position and height of the phys cursor in window W.
2112 Set w->phys_cursor_width to width of phys cursor.
2115 void
2116 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2117 struct window *w;
2118 struct glyph_row *row;
2119 struct glyph *glyph;
2120 int *xp, *yp, *heightp;
2122 struct frame *f = XFRAME (WINDOW_FRAME (w));
2123 int x, y, wd, h, h0, y0;
2125 /* Compute the width of the rectangle to draw. If on a stretch
2126 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2127 rectangle as wide as the glyph, but use a canonical character
2128 width instead. */
2129 wd = glyph->pixel_width - 1;
2130 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2131 wd++; /* Why? */
2132 #endif
2134 x = w->phys_cursor.x;
2135 if (x < 0)
2137 wd += x;
2138 x = 0;
2141 if (glyph->type == STRETCH_GLYPH
2142 && !x_stretch_cursor_p)
2143 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2144 w->phys_cursor_width = wd;
2146 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2148 /* If y is below window bottom, ensure that we still see a cursor. */
2149 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2151 h = max (h0, glyph->ascent + glyph->descent);
2152 h0 = min (h0, glyph->ascent + glyph->descent);
2154 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2155 if (y < y0)
2157 h = max (h - (y0 - y) + 1, h0);
2158 y = y0 - 1;
2160 else
2162 y0 = window_text_bottom_y (w) - h0;
2163 if (y > y0)
2165 h += y - y0;
2166 y = y0;
2170 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2171 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2172 *heightp = h;
2176 * Remember which glyph the mouse is over.
2179 void
2180 remember_mouse_glyph (f, gx, gy, rect)
2181 struct frame *f;
2182 int gx, gy;
2183 NativeRectangle *rect;
2185 Lisp_Object window;
2186 struct window *w;
2187 struct glyph_row *r, *gr, *end_row;
2188 enum window_part part;
2189 enum glyph_row_area area;
2190 int x, y, width, height;
2192 /* Try to determine frame pixel position and size of the glyph under
2193 frame pixel coordinates X/Y on frame F. */
2195 if (!f->glyphs_initialized_p
2196 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2197 NILP (window)))
2199 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2200 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2201 goto virtual_glyph;
2204 w = XWINDOW (window);
2205 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2206 height = WINDOW_FRAME_LINE_HEIGHT (w);
2208 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2209 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2211 if (w->pseudo_window_p)
2213 area = TEXT_AREA;
2214 part = ON_MODE_LINE; /* Don't adjust margin. */
2215 goto text_glyph;
2218 switch (part)
2220 case ON_LEFT_MARGIN:
2221 area = LEFT_MARGIN_AREA;
2222 goto text_glyph;
2224 case ON_RIGHT_MARGIN:
2225 area = RIGHT_MARGIN_AREA;
2226 goto text_glyph;
2228 case ON_HEADER_LINE:
2229 case ON_MODE_LINE:
2230 gr = (part == ON_HEADER_LINE
2231 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2232 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2233 gy = gr->y;
2234 area = TEXT_AREA;
2235 goto text_glyph_row_found;
2237 case ON_TEXT:
2238 area = TEXT_AREA;
2240 text_glyph:
2241 gr = 0; gy = 0;
2242 for (; r <= end_row && r->enabled_p; ++r)
2243 if (r->y + r->height > y)
2245 gr = r; gy = r->y;
2246 break;
2249 text_glyph_row_found:
2250 if (gr && gy <= y)
2252 struct glyph *g = gr->glyphs[area];
2253 struct glyph *end = g + gr->used[area];
2255 height = gr->height;
2256 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2257 if (gx + g->pixel_width > x)
2258 break;
2260 if (g < end)
2262 if (g->type == IMAGE_GLYPH)
2264 /* Don't remember when mouse is over image, as
2265 image may have hot-spots. */
2266 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2267 return;
2269 width = g->pixel_width;
2271 else
2273 /* Use nominal char spacing at end of line. */
2274 x -= gx;
2275 gx += (x / width) * width;
2278 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2279 gx += window_box_left_offset (w, area);
2281 else
2283 /* Use nominal line height at end of window. */
2284 gx = (x / width) * width;
2285 y -= gy;
2286 gy += (y / height) * height;
2288 break;
2290 case ON_LEFT_FRINGE:
2291 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2293 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2294 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2295 goto row_glyph;
2297 case ON_RIGHT_FRINGE:
2298 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2300 : window_box_right_offset (w, TEXT_AREA));
2301 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2302 goto row_glyph;
2304 case ON_SCROLL_BAR:
2305 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2307 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2310 : 0)));
2311 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2313 row_glyph:
2314 gr = 0, gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2318 gr = r; gy = r->y;
2319 break;
2322 if (gr && gy <= y)
2323 height = gr->height;
2324 else
2326 /* Use nominal line height at end of window. */
2327 y -= gy;
2328 gy += (y / height) * height;
2330 break;
2332 default:
2334 virtual_glyph:
2335 /* If there is no glyph under the mouse, then we divide the screen
2336 into a grid of the smallest glyph in the frame, and use that
2337 as our "glyph". */
2339 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2340 round down even for negative values. */
2341 if (gx < 0)
2342 gx -= width - 1;
2343 if (gy < 0)
2344 gy -= height - 1;
2346 gx = (gx / width) * width;
2347 gy = (gy / height) * height;
2349 goto store_rect;
2352 gx += WINDOW_LEFT_EDGE_X (w);
2353 gy += WINDOW_TOP_EDGE_Y (w);
2355 store_rect:
2356 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2358 /* Visible feedback for debugging. */
2359 #if 0
2360 #if HAVE_X_WINDOWS
2361 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2362 f->output_data.x->normal_gc,
2363 gx, gy, width, height);
2364 #endif
2365 #endif
2369 #endif /* HAVE_WINDOW_SYSTEM */
2372 /***********************************************************************
2373 Lisp form evaluation
2374 ***********************************************************************/
2376 /* Error handler for safe_eval and safe_call. */
2378 static Lisp_Object
2379 safe_eval_handler (arg)
2380 Lisp_Object arg;
2382 add_to_log ("Error during redisplay: %s", arg, Qnil);
2383 return Qnil;
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2394 Lisp_Object
2395 safe_call (nargs, args)
2396 int nargs;
2397 Lisp_Object *args;
2399 Lisp_Object val;
2401 if (inhibit_eval_during_redisplay)
2402 val = Qnil;
2403 else
2405 int count = SPECPDL_INDEX ();
2406 struct gcpro gcpro1;
2408 GCPRO1 (args[0]);
2409 gcpro1.nvars = nargs;
2410 specbind (Qinhibit_redisplay, Qt);
2411 /* Use Qt to ensure debugger does not run,
2412 so there is no possibility of wanting to redisplay. */
2413 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2414 safe_eval_handler);
2415 UNGCPRO;
2416 val = unbind_to (count, val);
2419 return val;
2423 /* Call function FN with one argument ARG.
2424 Return the result, or nil if something went wrong. */
2426 Lisp_Object
2427 safe_call1 (fn, arg)
2428 Lisp_Object fn, arg;
2430 Lisp_Object args[2];
2431 args[0] = fn;
2432 args[1] = arg;
2433 return safe_call (2, args);
2436 static Lisp_Object Qeval;
2438 Lisp_Object
2439 safe_eval (Lisp_Object sexpr)
2441 return safe_call1 (Qeval, sexpr);
2444 /* Call function FN with one argument ARG.
2445 Return the result, or nil if something went wrong. */
2447 Lisp_Object
2448 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2450 Lisp_Object args[3];
2451 args[0] = fn;
2452 args[1] = arg1;
2453 args[2] = arg2;
2454 return safe_call (3, args);
2459 /***********************************************************************
2460 Debugging
2461 ***********************************************************************/
2463 #if 0
2465 /* Define CHECK_IT to perform sanity checks on iterators.
2466 This is for debugging. It is too slow to do unconditionally. */
2468 static void
2469 check_it (it)
2470 struct it *it;
2472 if (it->method == GET_FROM_STRING)
2474 xassert (STRINGP (it->string));
2475 xassert (IT_STRING_CHARPOS (*it) >= 0);
2477 else
2479 xassert (IT_STRING_CHARPOS (*it) < 0);
2480 if (it->method == GET_FROM_BUFFER)
2482 /* Check that character and byte positions agree. */
2483 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2487 if (it->dpvec)
2488 xassert (it->current.dpvec_index >= 0);
2489 else
2490 xassert (it->current.dpvec_index < 0);
2493 #define CHECK_IT(IT) check_it ((IT))
2495 #else /* not 0 */
2497 #define CHECK_IT(IT) (void) 0
2499 #endif /* not 0 */
2502 #if GLYPH_DEBUG
2504 /* Check that the window end of window W is what we expect it
2505 to be---the last row in the current matrix displaying text. */
2507 static void
2508 check_window_end (w)
2509 struct window *w;
2511 if (!MINI_WINDOW_P (w)
2512 && !NILP (w->window_end_valid))
2514 struct glyph_row *row;
2515 xassert ((row = MATRIX_ROW (w->current_matrix,
2516 XFASTINT (w->window_end_vpos)),
2517 !row->enabled_p
2518 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2519 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2523 #define CHECK_WINDOW_END(W) check_window_end ((W))
2525 #else /* not GLYPH_DEBUG */
2527 #define CHECK_WINDOW_END(W) (void) 0
2529 #endif /* not GLYPH_DEBUG */
2533 /***********************************************************************
2534 Iterator initialization
2535 ***********************************************************************/
2537 /* Initialize IT for displaying current_buffer in window W, starting
2538 at character position CHARPOS. CHARPOS < 0 means that no buffer
2539 position is specified which is useful when the iterator is assigned
2540 a position later. BYTEPOS is the byte position corresponding to
2541 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2543 If ROW is not null, calls to produce_glyphs with IT as parameter
2544 will produce glyphs in that row.
2546 BASE_FACE_ID is the id of a base face to use. It must be one of
2547 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2548 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2549 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2551 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2552 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2553 will be initialized to use the corresponding mode line glyph row of
2554 the desired matrix of W. */
2556 void
2557 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2558 struct it *it;
2559 struct window *w;
2560 int charpos, bytepos;
2561 struct glyph_row *row;
2562 enum face_id base_face_id;
2564 int highlight_region_p;
2565 enum face_id remapped_base_face_id = base_face_id;
2567 /* Some precondition checks. */
2568 xassert (w != NULL && it != NULL);
2569 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2570 && charpos <= ZV));
2572 /* If face attributes have been changed since the last redisplay,
2573 free realized faces now because they depend on face definitions
2574 that might have changed. Don't free faces while there might be
2575 desired matrices pending which reference these faces. */
2576 if (face_change_count && !inhibit_free_realized_faces)
2578 face_change_count = 0;
2579 free_all_realized_faces (Qnil);
2582 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2583 if (! NILP (Vface_remapping_alist))
2584 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2586 /* Use one of the mode line rows of W's desired matrix if
2587 appropriate. */
2588 if (row == NULL)
2590 if (base_face_id == MODE_LINE_FACE_ID
2591 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2592 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2593 else if (base_face_id == HEADER_LINE_FACE_ID)
2594 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2597 /* Clear IT. */
2598 bzero (it, sizeof *it);
2599 it->current.overlay_string_index = -1;
2600 it->current.dpvec_index = -1;
2601 it->base_face_id = remapped_base_face_id;
2602 it->string = Qnil;
2603 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2605 /* The window in which we iterate over current_buffer: */
2606 XSETWINDOW (it->window, w);
2607 it->w = w;
2608 it->f = XFRAME (w->frame);
2610 it->cmp_it.id = -1;
2612 /* Extra space between lines (on window systems only). */
2613 if (base_face_id == DEFAULT_FACE_ID
2614 && FRAME_WINDOW_P (it->f))
2616 if (NATNUMP (current_buffer->extra_line_spacing))
2617 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2618 else if (FLOATP (current_buffer->extra_line_spacing))
2619 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2620 * FRAME_LINE_HEIGHT (it->f));
2621 else if (it->f->extra_line_spacing > 0)
2622 it->extra_line_spacing = it->f->extra_line_spacing;
2623 it->max_extra_line_spacing = 0;
2626 /* If realized faces have been removed, e.g. because of face
2627 attribute changes of named faces, recompute them. When running
2628 in batch mode, the face cache of the initial frame is null. If
2629 we happen to get called, make a dummy face cache. */
2630 if (FRAME_FACE_CACHE (it->f) == NULL)
2631 init_frame_faces (it->f);
2632 if (FRAME_FACE_CACHE (it->f)->used == 0)
2633 recompute_basic_faces (it->f);
2635 /* Current value of the `slice', `space-width', and 'height' properties. */
2636 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2637 it->space_width = Qnil;
2638 it->font_height = Qnil;
2639 it->override_ascent = -1;
2641 /* Are control characters displayed as `^C'? */
2642 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2644 /* -1 means everything between a CR and the following line end
2645 is invisible. >0 means lines indented more than this value are
2646 invisible. */
2647 it->selective = (INTEGERP (current_buffer->selective_display)
2648 ? XFASTINT (current_buffer->selective_display)
2649 : (!NILP (current_buffer->selective_display)
2650 ? -1 : 0));
2651 it->selective_display_ellipsis_p
2652 = !NILP (current_buffer->selective_display_ellipses);
2654 /* Display table to use. */
2655 it->dp = window_display_table (w);
2657 /* Are multibyte characters enabled in current_buffer? */
2658 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2660 /* Non-zero if we should highlight the region. */
2661 highlight_region_p
2662 = (!NILP (Vtransient_mark_mode)
2663 && !NILP (current_buffer->mark_active)
2664 && XMARKER (current_buffer->mark)->buffer != 0);
2666 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2667 start and end of a visible region in window IT->w. Set both to
2668 -1 to indicate no region. */
2669 if (highlight_region_p
2670 /* Maybe highlight only in selected window. */
2671 && (/* Either show region everywhere. */
2672 highlight_nonselected_windows
2673 /* Or show region in the selected window. */
2674 || w == XWINDOW (selected_window)
2675 /* Or show the region if we are in the mini-buffer and W is
2676 the window the mini-buffer refers to. */
2677 || (MINI_WINDOW_P (XWINDOW (selected_window))
2678 && WINDOWP (minibuf_selected_window)
2679 && w == XWINDOW (minibuf_selected_window))))
2681 int charpos = marker_position (current_buffer->mark);
2682 it->region_beg_charpos = min (PT, charpos);
2683 it->region_end_charpos = max (PT, charpos);
2685 else
2686 it->region_beg_charpos = it->region_end_charpos = -1;
2688 /* Get the position at which the redisplay_end_trigger hook should
2689 be run, if it is to be run at all. */
2690 if (MARKERP (w->redisplay_end_trigger)
2691 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2692 it->redisplay_end_trigger_charpos
2693 = marker_position (w->redisplay_end_trigger);
2694 else if (INTEGERP (w->redisplay_end_trigger))
2695 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2697 /* Correct bogus values of tab_width. */
2698 it->tab_width = XINT (current_buffer->tab_width);
2699 if (it->tab_width <= 0 || it->tab_width > 1000)
2700 it->tab_width = 8;
2702 /* Are lines in the display truncated? */
2703 if (base_face_id != DEFAULT_FACE_ID
2704 || XINT (it->w->hscroll)
2705 || (! WINDOW_FULL_WIDTH_P (it->w)
2706 && ((!NILP (Vtruncate_partial_width_windows)
2707 && !INTEGERP (Vtruncate_partial_width_windows))
2708 || (INTEGERP (Vtruncate_partial_width_windows)
2709 && (WINDOW_TOTAL_COLS (it->w)
2710 < XINT (Vtruncate_partial_width_windows))))))
2711 it->line_wrap = TRUNCATE;
2712 else if (NILP (current_buffer->truncate_lines))
2713 it->line_wrap = NILP (current_buffer->word_wrap)
2714 ? WINDOW_WRAP : WORD_WRAP;
2715 else
2716 it->line_wrap = TRUNCATE;
2718 /* Get dimensions of truncation and continuation glyphs. These are
2719 displayed as fringe bitmaps under X, so we don't need them for such
2720 frames. */
2721 if (!FRAME_WINDOW_P (it->f))
2723 if (it->line_wrap == TRUNCATE)
2725 /* We will need the truncation glyph. */
2726 xassert (it->glyph_row == NULL);
2727 produce_special_glyphs (it, IT_TRUNCATION);
2728 it->truncation_pixel_width = it->pixel_width;
2730 else
2732 /* We will need the continuation glyph. */
2733 xassert (it->glyph_row == NULL);
2734 produce_special_glyphs (it, IT_CONTINUATION);
2735 it->continuation_pixel_width = it->pixel_width;
2738 /* Reset these values to zero because the produce_special_glyphs
2739 above has changed them. */
2740 it->pixel_width = it->ascent = it->descent = 0;
2741 it->phys_ascent = it->phys_descent = 0;
2744 /* Set this after getting the dimensions of truncation and
2745 continuation glyphs, so that we don't produce glyphs when calling
2746 produce_special_glyphs, above. */
2747 it->glyph_row = row;
2748 it->area = TEXT_AREA;
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2760 else
2762 it->first_visible_x
2763 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2788 it->last_visible_y = window_text_bottom_y (w);
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2794 struct face *face;
2796 it->face_id = remapped_base_face_id;
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2809 it->end_charpos = ZV;
2810 it->face_id = -1;
2811 IT_CHARPOS (*it) = charpos;
2813 /* Compute byte position if not specified. */
2814 if (bytepos < charpos)
2815 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2816 else
2817 IT_BYTEPOS (*it) = bytepos;
2819 it->start = it->current;
2821 /* Compute faces etc. */
2822 reseat (it, it->current.pos, 1);
2825 CHECK_IT (it);
2829 /* Initialize IT for the display of window W with window start POS. */
2831 void
2832 start_display (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct text_pos pos;
2837 struct glyph_row *row;
2838 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2840 row = w->desired_matrix->rows + first_vpos;
2841 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2842 it->first_vpos = first_vpos;
2844 /* Don't reseat to previous visible line start if current start
2845 position is in a string or image. */
2846 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2848 int start_at_line_beg_p;
2849 int first_y = it->current_y;
2851 /* If window start is not at a line start, skip forward to POS to
2852 get the correct continuation lines width. */
2853 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2854 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2855 if (!start_at_line_beg_p)
2857 int new_x;
2859 reseat_at_previous_visible_line_start (it);
2860 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2862 new_x = it->current_x + it->pixel_width;
2864 /* If lines are continued, this line may end in the middle
2865 of a multi-glyph character (e.g. a control character
2866 displayed as \003, or in the middle of an overlay
2867 string). In this case move_it_to above will not have
2868 taken us to the start of the continuation line but to the
2869 end of the continued line. */
2870 if (it->current_x > 0
2871 && it->line_wrap != TRUNCATE /* Lines are continued. */
2872 && (/* And glyph doesn't fit on the line. */
2873 new_x > it->last_visible_x
2874 /* Or it fits exactly and we're on a window
2875 system frame. */
2876 || (new_x == it->last_visible_x
2877 && FRAME_WINDOW_P (it->f))))
2879 if (it->current.dpvec_index >= 0
2880 || it->current.overlay_string_index >= 0)
2882 set_iterator_to_next (it, 1);
2883 move_it_in_display_line_to (it, -1, -1, 0);
2886 it->continuation_lines_width += it->current_x;
2889 /* We're starting a new display line, not affected by the
2890 height of the continued line, so clear the appropriate
2891 fields in the iterator structure. */
2892 it->max_ascent = it->max_descent = 0;
2893 it->max_phys_ascent = it->max_phys_descent = 0;
2895 it->current_y = first_y;
2896 it->vpos = 0;
2897 it->current_x = it->hpos = 0;
2903 /* Return 1 if POS is a position in ellipses displayed for invisible
2904 text. W is the window we display, for text property lookup. */
2906 static int
2907 in_ellipses_for_invisible_text_p (pos, w)
2908 struct display_pos *pos;
2909 struct window *w;
2911 Lisp_Object prop, window;
2912 int ellipses_p = 0;
2913 int charpos = CHARPOS (pos->pos);
2915 /* If POS specifies a position in a display vector, this might
2916 be for an ellipsis displayed for invisible text. We won't
2917 get the iterator set up for delivering that ellipsis unless
2918 we make sure that it gets aware of the invisible text. */
2919 if (pos->dpvec_index >= 0
2920 && pos->overlay_string_index < 0
2921 && CHARPOS (pos->string_pos) < 0
2922 && charpos > BEGV
2923 && (XSETWINDOW (window, w),
2924 prop = Fget_char_property (make_number (charpos),
2925 Qinvisible, window),
2926 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2928 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2929 window);
2930 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2933 return ellipses_p;
2937 /* Initialize IT for stepping through current_buffer in window W,
2938 starting at position POS that includes overlay string and display
2939 vector/ control character translation position information. Value
2940 is zero if there are overlay strings with newlines at POS. */
2942 static int
2943 init_from_display_pos (it, w, pos)
2944 struct it *it;
2945 struct window *w;
2946 struct display_pos *pos;
2948 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2949 int i, overlay_strings_with_newlines = 0;
2951 /* If POS specifies a position in a display vector, this might
2952 be for an ellipsis displayed for invisible text. We won't
2953 get the iterator set up for delivering that ellipsis unless
2954 we make sure that it gets aware of the invisible text. */
2955 if (in_ellipses_for_invisible_text_p (pos, w))
2957 --charpos;
2958 bytepos = 0;
2961 /* Keep in mind: the call to reseat in init_iterator skips invisible
2962 text, so we might end up at a position different from POS. This
2963 is only a problem when POS is a row start after a newline and an
2964 overlay starts there with an after-string, and the overlay has an
2965 invisible property. Since we don't skip invisible text in
2966 display_line and elsewhere immediately after consuming the
2967 newline before the row start, such a POS will not be in a string,
2968 but the call to init_iterator below will move us to the
2969 after-string. */
2970 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2972 /* This only scans the current chunk -- it should scan all chunks.
2973 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2974 to 16 in 22.1 to make this a lesser problem. */
2975 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2977 const char *s = SDATA (it->overlay_strings[i]);
2978 const char *e = s + SBYTES (it->overlay_strings[i]);
2980 while (s < e && *s != '\n')
2981 ++s;
2983 if (s < e)
2985 overlay_strings_with_newlines = 1;
2986 break;
2990 /* If position is within an overlay string, set up IT to the right
2991 overlay string. */
2992 if (pos->overlay_string_index >= 0)
2994 int relative_index;
2996 /* If the first overlay string happens to have a `display'
2997 property for an image, the iterator will be set up for that
2998 image, and we have to undo that setup first before we can
2999 correct the overlay string index. */
3000 if (it->method == GET_FROM_IMAGE)
3001 pop_it (it);
3003 /* We already have the first chunk of overlay strings in
3004 IT->overlay_strings. Load more until the one for
3005 pos->overlay_string_index is in IT->overlay_strings. */
3006 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3008 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3009 it->current.overlay_string_index = 0;
3010 while (n--)
3012 load_overlay_strings (it, 0);
3013 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3017 it->current.overlay_string_index = pos->overlay_string_index;
3018 relative_index = (it->current.overlay_string_index
3019 % OVERLAY_STRING_CHUNK_SIZE);
3020 it->string = it->overlay_strings[relative_index];
3021 xassert (STRINGP (it->string));
3022 it->current.string_pos = pos->string_pos;
3023 it->method = GET_FROM_STRING;
3026 if (CHARPOS (pos->string_pos) >= 0)
3028 /* Recorded position is not in an overlay string, but in another
3029 string. This can only be a string from a `display' property.
3030 IT should already be filled with that string. */
3031 it->current.string_pos = pos->string_pos;
3032 xassert (STRINGP (it->string));
3035 /* Restore position in display vector translations, control
3036 character translations or ellipses. */
3037 if (pos->dpvec_index >= 0)
3039 if (it->dpvec == NULL)
3040 get_next_display_element (it);
3041 xassert (it->dpvec && it->current.dpvec_index == 0);
3042 it->current.dpvec_index = pos->dpvec_index;
3045 CHECK_IT (it);
3046 return !overlay_strings_with_newlines;
3050 /* Initialize IT for stepping through current_buffer in window W
3051 starting at ROW->start. */
3053 static void
3054 init_to_row_start (it, w, row)
3055 struct it *it;
3056 struct window *w;
3057 struct glyph_row *row;
3059 init_from_display_pos (it, w, &row->start);
3060 it->start = row->start;
3061 it->continuation_lines_width = row->continuation_lines_width;
3062 CHECK_IT (it);
3066 /* Initialize IT for stepping through current_buffer in window W
3067 starting in the line following ROW, i.e. starting at ROW->end.
3068 Value is zero if there are overlay strings with newlines at ROW's
3069 end position. */
3071 static int
3072 init_to_row_end (it, w, row)
3073 struct it *it;
3074 struct window *w;
3075 struct glyph_row *row;
3077 int success = 0;
3079 if (init_from_display_pos (it, w, &row->end))
3081 if (row->continued_p)
3082 it->continuation_lines_width
3083 = row->continuation_lines_width + row->pixel_width;
3084 CHECK_IT (it);
3085 success = 1;
3088 return success;
3094 /***********************************************************************
3095 Text properties
3096 ***********************************************************************/
3098 /* Called when IT reaches IT->stop_charpos. Handle text property and
3099 overlay changes. Set IT->stop_charpos to the next position where
3100 to stop. */
3102 static void
3103 handle_stop (it)
3104 struct it *it;
3106 enum prop_handled handled;
3107 int handle_overlay_change_p;
3108 struct props *p;
3110 it->dpvec = NULL;
3111 it->current.dpvec_index = -1;
3112 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3113 it->ignore_overlay_strings_at_pos_p = 0;
3114 it->ellipsis_p = 0;
3116 /* Use face of preceding text for ellipsis (if invisible) */
3117 if (it->selective_display_ellipsis_p)
3118 it->saved_face_id = it->face_id;
3122 handled = HANDLED_NORMALLY;
3124 /* Call text property handlers. */
3125 for (p = it_props; p->handler; ++p)
3127 handled = p->handler (it);
3129 if (handled == HANDLED_RECOMPUTE_PROPS)
3130 break;
3131 else if (handled == HANDLED_RETURN)
3133 /* We still want to show before and after strings from
3134 overlays even if the actual buffer text is replaced. */
3135 if (!handle_overlay_change_p
3136 || it->sp > 1
3137 || !get_overlay_strings_1 (it, 0, 0))
3139 if (it->ellipsis_p)
3140 setup_for_ellipsis (it, 0);
3141 /* When handling a display spec, we might load an
3142 empty string. In that case, discard it here. We
3143 used to discard it in handle_single_display_spec,
3144 but that causes get_overlay_strings_1, above, to
3145 ignore overlay strings that we must check. */
3146 if (STRINGP (it->string) && !SCHARS (it->string))
3147 pop_it (it);
3148 return;
3150 else if (STRINGP (it->string) && !SCHARS (it->string))
3151 pop_it (it);
3152 else
3154 it->ignore_overlay_strings_at_pos_p = 1;
3155 it->string_from_display_prop_p = 0;
3156 handle_overlay_change_p = 0;
3158 handled = HANDLED_RECOMPUTE_PROPS;
3159 break;
3161 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3162 handle_overlay_change_p = 0;
3165 if (handled != HANDLED_RECOMPUTE_PROPS)
3167 /* Don't check for overlay strings below when set to deliver
3168 characters from a display vector. */
3169 if (it->method == GET_FROM_DISPLAY_VECTOR)
3170 handle_overlay_change_p = 0;
3172 /* Handle overlay changes.
3173 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3174 if it finds overlays. */
3175 if (handle_overlay_change_p)
3176 handled = handle_overlay_change (it);
3179 if (it->ellipsis_p)
3181 setup_for_ellipsis (it, 0);
3182 break;
3185 while (handled == HANDLED_RECOMPUTE_PROPS);
3187 /* Determine where to stop next. */
3188 if (handled == HANDLED_NORMALLY)
3189 compute_stop_pos (it);
3193 /* Compute IT->stop_charpos from text property and overlay change
3194 information for IT's current position. */
3196 static void
3197 compute_stop_pos (it)
3198 struct it *it;
3200 register INTERVAL iv, next_iv;
3201 Lisp_Object object, limit, position;
3202 EMACS_INT charpos, bytepos;
3204 /* If nowhere else, stop at the end. */
3205 it->stop_charpos = it->end_charpos;
3207 if (STRINGP (it->string))
3209 /* Strings are usually short, so don't limit the search for
3210 properties. */
3211 object = it->string;
3212 limit = Qnil;
3213 charpos = IT_STRING_CHARPOS (*it);
3214 bytepos = IT_STRING_BYTEPOS (*it);
3216 else
3218 EMACS_INT pos;
3220 /* If next overlay change is in front of the current stop pos
3221 (which is IT->end_charpos), stop there. Note: value of
3222 next_overlay_change is point-max if no overlay change
3223 follows. */
3224 charpos = IT_CHARPOS (*it);
3225 bytepos = IT_BYTEPOS (*it);
3226 pos = next_overlay_change (charpos);
3227 if (pos < it->stop_charpos)
3228 it->stop_charpos = pos;
3230 /* If showing the region, we have to stop at the region
3231 start or end because the face might change there. */
3232 if (it->region_beg_charpos > 0)
3234 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3235 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3236 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3237 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3240 /* Set up variables for computing the stop position from text
3241 property changes. */
3242 XSETBUFFER (object, current_buffer);
3243 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3246 /* Get the interval containing IT's position. Value is a null
3247 interval if there isn't such an interval. */
3248 position = make_number (charpos);
3249 iv = validate_interval_range (object, &position, &position, 0);
3250 if (!NULL_INTERVAL_P (iv))
3252 Lisp_Object values_here[LAST_PROP_IDX];
3253 struct props *p;
3255 /* Get properties here. */
3256 for (p = it_props; p->handler; ++p)
3257 values_here[p->idx] = textget (iv->plist, *p->name);
3259 /* Look for an interval following iv that has different
3260 properties. */
3261 for (next_iv = next_interval (iv);
3262 (!NULL_INTERVAL_P (next_iv)
3263 && (NILP (limit)
3264 || XFASTINT (limit) > next_iv->position));
3265 next_iv = next_interval (next_iv))
3267 for (p = it_props; p->handler; ++p)
3269 Lisp_Object new_value;
3271 new_value = textget (next_iv->plist, *p->name);
3272 if (!EQ (values_here[p->idx], new_value))
3273 break;
3276 if (p->handler)
3277 break;
3280 if (!NULL_INTERVAL_P (next_iv))
3282 if (INTEGERP (limit)
3283 && next_iv->position >= XFASTINT (limit))
3284 /* No text property change up to limit. */
3285 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3286 else
3287 /* Text properties change in next_iv. */
3288 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3292 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3293 it->stop_charpos, it->string);
3295 xassert (STRINGP (it->string)
3296 || (it->stop_charpos >= BEGV
3297 && it->stop_charpos >= IT_CHARPOS (*it)));
3301 /* Return the position of the next overlay change after POS in
3302 current_buffer. Value is point-max if no overlay change
3303 follows. This is like `next-overlay-change' but doesn't use
3304 xmalloc. */
3306 static EMACS_INT
3307 next_overlay_change (pos)
3308 EMACS_INT pos;
3310 int noverlays;
3311 EMACS_INT endpos;
3312 Lisp_Object *overlays;
3313 int i;
3315 /* Get all overlays at the given position. */
3316 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3318 /* If any of these overlays ends before endpos,
3319 use its ending point instead. */
3320 for (i = 0; i < noverlays; ++i)
3322 Lisp_Object oend;
3323 EMACS_INT oendpos;
3325 oend = OVERLAY_END (overlays[i]);
3326 oendpos = OVERLAY_POSITION (oend);
3327 endpos = min (endpos, oendpos);
3330 return endpos;
3335 /***********************************************************************
3336 Fontification
3337 ***********************************************************************/
3339 /* Handle changes in the `fontified' property of the current buffer by
3340 calling hook functions from Qfontification_functions to fontify
3341 regions of text. */
3343 static enum prop_handled
3344 handle_fontified_prop (it)
3345 struct it *it;
3347 Lisp_Object prop, pos;
3348 enum prop_handled handled = HANDLED_NORMALLY;
3350 if (!NILP (Vmemory_full))
3351 return handled;
3353 /* Get the value of the `fontified' property at IT's current buffer
3354 position. (The `fontified' property doesn't have a special
3355 meaning in strings.) If the value is nil, call functions from
3356 Qfontification_functions. */
3357 if (!STRINGP (it->string)
3358 && it->s == NULL
3359 && !NILP (Vfontification_functions)
3360 && !NILP (Vrun_hooks)
3361 && (pos = make_number (IT_CHARPOS (*it)),
3362 prop = Fget_char_property (pos, Qfontified, Qnil),
3363 /* Ignore the special cased nil value always present at EOB since
3364 no amount of fontifying will be able to change it. */
3365 NILP (prop) && IT_CHARPOS (*it) < Z))
3367 int count = SPECPDL_INDEX ();
3368 Lisp_Object val;
3370 val = Vfontification_functions;
3371 specbind (Qfontification_functions, Qnil);
3373 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3374 safe_call1 (val, pos);
3375 else
3377 Lisp_Object globals, fn;
3378 struct gcpro gcpro1, gcpro2;
3380 globals = Qnil;
3381 GCPRO2 (val, globals);
3383 for (; CONSP (val); val = XCDR (val))
3385 fn = XCAR (val);
3387 if (EQ (fn, Qt))
3389 /* A value of t indicates this hook has a local
3390 binding; it means to run the global binding too.
3391 In a global value, t should not occur. If it
3392 does, we must ignore it to avoid an endless
3393 loop. */
3394 for (globals = Fdefault_value (Qfontification_functions);
3395 CONSP (globals);
3396 globals = XCDR (globals))
3398 fn = XCAR (globals);
3399 if (!EQ (fn, Qt))
3400 safe_call1 (fn, pos);
3403 else
3404 safe_call1 (fn, pos);
3407 UNGCPRO;
3410 unbind_to (count, Qnil);
3412 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3413 something. This avoids an endless loop if they failed to
3414 fontify the text for which reason ever. */
3415 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3416 handled = HANDLED_RECOMPUTE_PROPS;
3419 return handled;
3424 /***********************************************************************
3425 Faces
3426 ***********************************************************************/
3428 /* Set up iterator IT from face properties at its current position.
3429 Called from handle_stop. */
3431 static enum prop_handled
3432 handle_face_prop (it)
3433 struct it *it;
3435 int new_face_id;
3436 EMACS_INT next_stop;
3438 if (!STRINGP (it->string))
3440 new_face_id
3441 = face_at_buffer_position (it->w,
3442 IT_CHARPOS (*it),
3443 it->region_beg_charpos,
3444 it->region_end_charpos,
3445 &next_stop,
3446 (IT_CHARPOS (*it)
3447 + TEXT_PROP_DISTANCE_LIMIT),
3448 0, it->base_face_id);
3450 /* Is this a start of a run of characters with box face?
3451 Caveat: this can be called for a freshly initialized
3452 iterator; face_id is -1 in this case. We know that the new
3453 face will not change until limit, i.e. if the new face has a
3454 box, all characters up to limit will have one. But, as
3455 usual, we don't know whether limit is really the end. */
3456 if (new_face_id != it->face_id)
3458 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3460 /* If new face has a box but old face has not, this is
3461 the start of a run of characters with box, i.e. it has
3462 a shadow on the left side. The value of face_id of the
3463 iterator will be -1 if this is the initial call that gets
3464 the face. In this case, we have to look in front of IT's
3465 position and see whether there is a face != new_face_id. */
3466 it->start_of_box_run_p
3467 = (new_face->box != FACE_NO_BOX
3468 && (it->face_id >= 0
3469 || IT_CHARPOS (*it) == BEG
3470 || new_face_id != face_before_it_pos (it)));
3471 it->face_box_p = new_face->box != FACE_NO_BOX;
3474 else
3476 int base_face_id, bufpos;
3477 int i;
3478 Lisp_Object from_overlay
3479 = (it->current.overlay_string_index >= 0
3480 ? it->string_overlays[it->current.overlay_string_index]
3481 : Qnil);
3483 /* See if we got to this string directly or indirectly from
3484 an overlay property. That includes the before-string or
3485 after-string of an overlay, strings in display properties
3486 provided by an overlay, their text properties, etc.
3488 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3489 if (! NILP (from_overlay))
3490 for (i = it->sp - 1; i >= 0; i--)
3492 if (it->stack[i].current.overlay_string_index >= 0)
3493 from_overlay
3494 = it->string_overlays[it->stack[i].current.overlay_string_index];
3495 else if (! NILP (it->stack[i].from_overlay))
3496 from_overlay = it->stack[i].from_overlay;
3498 if (!NILP (from_overlay))
3499 break;
3502 if (! NILP (from_overlay))
3504 bufpos = IT_CHARPOS (*it);
3505 /* For a string from an overlay, the base face depends
3506 only on text properties and ignores overlays. */
3507 base_face_id
3508 = face_for_overlay_string (it->w,
3509 IT_CHARPOS (*it),
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 (IT_CHARPOS (*it)
3514 + TEXT_PROP_DISTANCE_LIMIT),
3516 from_overlay);
3518 else
3520 bufpos = 0;
3522 /* For strings from a `display' property, use the face at
3523 IT's current buffer position as the base face to merge
3524 with, so that overlay strings appear in the same face as
3525 surrounding text, unless they specify their own
3526 faces. */
3527 base_face_id = underlying_face_id (it);
3530 new_face_id = face_at_string_position (it->w,
3531 it->string,
3532 IT_STRING_CHARPOS (*it),
3533 bufpos,
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_stop,
3537 base_face_id, 0);
3539 /* Is this a start of a run of characters with box? Caveat:
3540 this can be called for a freshly allocated iterator; face_id
3541 is -1 is this case. We know that the new face will not
3542 change until the next check pos, i.e. if the new face has a
3543 box, all characters up to that position will have a
3544 box. But, as usual, we don't know whether that position
3545 is really the end. */
3546 if (new_face_id != it->face_id)
3548 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3549 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3551 /* If new face has a box but old face hasn't, this is the
3552 start of a run of characters with box, i.e. it has a
3553 shadow on the left side. */
3554 it->start_of_box_run_p
3555 = new_face->box && (old_face == NULL || !old_face->box);
3556 it->face_box_p = new_face->box != FACE_NO_BOX;
3560 it->face_id = new_face_id;
3561 return HANDLED_NORMALLY;
3565 /* Return the ID of the face ``underlying'' IT's current position,
3566 which is in a string. If the iterator is associated with a
3567 buffer, return the face at IT's current buffer position.
3568 Otherwise, use the iterator's base_face_id. */
3570 static int
3571 underlying_face_id (it)
3572 struct it *it;
3574 int face_id = it->base_face_id, i;
3576 xassert (STRINGP (it->string));
3578 for (i = it->sp - 1; i >= 0; --i)
3579 if (NILP (it->stack[i].string))
3580 face_id = it->stack[i].face_id;
3582 return face_id;
3586 /* Compute the face one character before or after the current position
3587 of IT. BEFORE_P non-zero means get the face in front of IT's
3588 position. Value is the id of the face. */
3590 static int
3591 face_before_or_after_it_pos (it, before_p)
3592 struct it *it;
3593 int before_p;
3595 int face_id, limit;
3596 EMACS_INT next_check_charpos;
3597 struct text_pos pos;
3599 xassert (it->s == NULL);
3601 if (STRINGP (it->string))
3603 int bufpos, base_face_id;
3605 /* No face change past the end of the string (for the case
3606 we are padding with spaces). No face change before the
3607 string start. */
3608 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3609 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3610 return it->face_id;
3612 /* Set pos to the position before or after IT's current position. */
3613 if (before_p)
3614 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3615 else
3616 /* For composition, we must check the character after the
3617 composition. */
3618 pos = (it->what == IT_COMPOSITION
3619 ? string_pos (IT_STRING_CHARPOS (*it)
3620 + it->cmp_it.nchars, it->string)
3621 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3623 if (it->current.overlay_string_index >= 0)
3624 bufpos = IT_CHARPOS (*it);
3625 else
3626 bufpos = 0;
3628 base_face_id = underlying_face_id (it);
3630 /* Get the face for ASCII, or unibyte. */
3631 face_id = face_at_string_position (it->w,
3632 it->string,
3633 CHARPOS (pos),
3634 bufpos,
3635 it->region_beg_charpos,
3636 it->region_end_charpos,
3637 &next_check_charpos,
3638 base_face_id, 0);
3640 /* Correct the face for charsets different from ASCII. Do it
3641 for the multibyte case only. The face returned above is
3642 suitable for unibyte text if IT->string is unibyte. */
3643 if (STRING_MULTIBYTE (it->string))
3645 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3646 int rest = SBYTES (it->string) - BYTEPOS (pos);
3647 int c, len;
3648 struct face *face = FACE_FROM_ID (it->f, face_id);
3650 c = string_char_and_length (p, rest, &len);
3651 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3654 else
3656 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3657 || (IT_CHARPOS (*it) <= BEGV && before_p))
3658 return it->face_id;
3660 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3661 pos = it->current.pos;
3663 if (before_p)
3664 DEC_TEXT_POS (pos, it->multibyte_p);
3665 else
3667 if (it->what == IT_COMPOSITION)
3668 /* For composition, we must check the position after the
3669 composition. */
3670 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3671 else
3672 INC_TEXT_POS (pos, it->multibyte_p);
3675 /* Determine face for CHARSET_ASCII, or unibyte. */
3676 face_id = face_at_buffer_position (it->w,
3677 CHARPOS (pos),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_check_charpos,
3681 limit, 0, -1);
3683 /* Correct the face for charsets different from ASCII. Do it
3684 for the multibyte case only. The face returned above is
3685 suitable for unibyte text if current_buffer is unibyte. */
3686 if (it->multibyte_p)
3688 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3689 struct face *face = FACE_FROM_ID (it->f, face_id);
3690 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3694 return face_id;
3699 /***********************************************************************
3700 Invisible text
3701 ***********************************************************************/
3703 /* Set up iterator IT from invisible properties at its current
3704 position. Called from handle_stop. */
3706 static enum prop_handled
3707 handle_invisible_prop (it)
3708 struct it *it;
3710 enum prop_handled handled = HANDLED_NORMALLY;
3712 if (STRINGP (it->string))
3714 extern Lisp_Object Qinvisible;
3715 Lisp_Object prop, end_charpos, limit, charpos;
3717 /* Get the value of the invisible text property at the
3718 current position. Value will be nil if there is no such
3719 property. */
3720 charpos = make_number (IT_STRING_CHARPOS (*it));
3721 prop = Fget_text_property (charpos, Qinvisible, it->string);
3723 if (!NILP (prop)
3724 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3726 handled = HANDLED_RECOMPUTE_PROPS;
3728 /* Get the position at which the next change of the
3729 invisible text property can be found in IT->string.
3730 Value will be nil if the property value is the same for
3731 all the rest of IT->string. */
3732 XSETINT (limit, SCHARS (it->string));
3733 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3734 it->string, limit);
3736 /* Text at current position is invisible. The next
3737 change in the property is at position end_charpos.
3738 Move IT's current position to that position. */
3739 if (INTEGERP (end_charpos)
3740 && XFASTINT (end_charpos) < XFASTINT (limit))
3742 struct text_pos old;
3743 old = it->current.string_pos;
3744 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3745 compute_string_pos (&it->current.string_pos, old, it->string);
3747 else
3749 /* The rest of the string is invisible. If this is an
3750 overlay string, proceed with the next overlay string
3751 or whatever comes and return a character from there. */
3752 if (it->current.overlay_string_index >= 0)
3754 next_overlay_string (it);
3755 /* Don't check for overlay strings when we just
3756 finished processing them. */
3757 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3759 else
3761 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3762 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3767 else
3769 int invis_p;
3770 EMACS_INT newpos, next_stop, start_charpos;
3771 Lisp_Object pos, prop, overlay;
3773 /* First of all, is there invisible text at this position? */
3774 start_charpos = IT_CHARPOS (*it);
3775 pos = make_number (IT_CHARPOS (*it));
3776 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3777 &overlay);
3778 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3780 /* If we are on invisible text, skip over it. */
3781 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3783 /* Record whether we have to display an ellipsis for the
3784 invisible text. */
3785 int display_ellipsis_p = invis_p == 2;
3787 handled = HANDLED_RECOMPUTE_PROPS;
3789 /* Loop skipping over invisible text. The loop is left at
3790 ZV or with IT on the first char being visible again. */
3793 /* Try to skip some invisible text. Return value is the
3794 position reached which can be equal to IT's position
3795 if there is nothing invisible here. This skips both
3796 over invisible text properties and overlays with
3797 invisible property. */
3798 newpos = skip_invisible (IT_CHARPOS (*it),
3799 &next_stop, ZV, it->window);
3801 /* If we skipped nothing at all we weren't at invisible
3802 text in the first place. If everything to the end of
3803 the buffer was skipped, end the loop. */
3804 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3805 invis_p = 0;
3806 else
3808 /* We skipped some characters but not necessarily
3809 all there are. Check if we ended up on visible
3810 text. Fget_char_property returns the property of
3811 the char before the given position, i.e. if we
3812 get invis_p = 0, this means that the char at
3813 newpos is visible. */
3814 pos = make_number (newpos);
3815 prop = Fget_char_property (pos, Qinvisible, it->window);
3816 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3819 /* If we ended up on invisible text, proceed to
3820 skip starting with next_stop. */
3821 if (invis_p)
3822 IT_CHARPOS (*it) = next_stop;
3824 /* If there are adjacent invisible texts, don't lose the
3825 second one's ellipsis. */
3826 if (invis_p == 2)
3827 display_ellipsis_p = 1;
3829 while (invis_p);
3831 /* The position newpos is now either ZV or on visible text. */
3832 IT_CHARPOS (*it) = newpos;
3833 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3835 /* If there are before-strings at the start of invisible
3836 text, and the text is invisible because of a text
3837 property, arrange to show before-strings because 20.x did
3838 it that way. (If the text is invisible because of an
3839 overlay property instead of a text property, this is
3840 already handled in the overlay code.) */
3841 if (NILP (overlay)
3842 && get_overlay_strings (it, start_charpos))
3844 handled = HANDLED_RECOMPUTE_PROPS;
3845 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3847 else if (display_ellipsis_p)
3849 /* Make sure that the glyphs of the ellipsis will get
3850 correct `charpos' values. If we would not update
3851 it->position here, the glyphs would belong to the
3852 last visible character _before_ the invisible
3853 text, which confuses `set_cursor_from_row'.
3855 We use the last invisible position instead of the
3856 first because this way the cursor is always drawn on
3857 the first "." of the ellipsis, whenever PT is inside
3858 the invisible text. Otherwise the cursor would be
3859 placed _after_ the ellipsis when the point is after the
3860 first invisible character. */
3861 if (!STRINGP (it->object))
3863 it->position.charpos = IT_CHARPOS (*it) - 1;
3864 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3866 it->ellipsis_p = 1;
3867 /* Let the ellipsis display before
3868 considering any properties of the following char.
3869 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3870 handled = HANDLED_RETURN;
3875 return handled;
3879 /* Make iterator IT return `...' next.
3880 Replaces LEN characters from buffer. */
3882 static void
3883 setup_for_ellipsis (it, len)
3884 struct it *it;
3885 int len;
3887 /* Use the display table definition for `...'. Invalid glyphs
3888 will be handled by the method returning elements from dpvec. */
3889 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3891 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3892 it->dpvec = v->contents;
3893 it->dpend = v->contents + v->size;
3895 else
3897 /* Default `...'. */
3898 it->dpvec = default_invis_vector;
3899 it->dpend = default_invis_vector + 3;
3902 it->dpvec_char_len = len;
3903 it->current.dpvec_index = 0;
3904 it->dpvec_face_id = -1;
3906 /* Remember the current face id in case glyphs specify faces.
3907 IT's face is restored in set_iterator_to_next.
3908 saved_face_id was set to preceding char's face in handle_stop. */
3909 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3910 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3912 it->method = GET_FROM_DISPLAY_VECTOR;
3913 it->ellipsis_p = 1;
3918 /***********************************************************************
3919 'display' property
3920 ***********************************************************************/
3922 /* Set up iterator IT from `display' property at its current position.
3923 Called from handle_stop.
3924 We return HANDLED_RETURN if some part of the display property
3925 overrides the display of the buffer text itself.
3926 Otherwise we return HANDLED_NORMALLY. */
3928 static enum prop_handled
3929 handle_display_prop (it)
3930 struct it *it;
3932 Lisp_Object prop, object, overlay;
3933 struct text_pos *position;
3934 /* Nonzero if some property replaces the display of the text itself. */
3935 int display_replaced_p = 0;
3937 if (STRINGP (it->string))
3939 object = it->string;
3940 position = &it->current.string_pos;
3942 else
3944 XSETWINDOW (object, it->w);
3945 position = &it->current.pos;
3948 /* Reset those iterator values set from display property values. */
3949 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3950 it->space_width = Qnil;
3951 it->font_height = Qnil;
3952 it->voffset = 0;
3954 /* We don't support recursive `display' properties, i.e. string
3955 values that have a string `display' property, that have a string
3956 `display' property etc. */
3957 if (!it->string_from_display_prop_p)
3958 it->area = TEXT_AREA;
3960 prop = get_char_property_and_overlay (make_number (position->charpos),
3961 Qdisplay, object, &overlay);
3962 if (NILP (prop))
3963 return HANDLED_NORMALLY;
3964 /* Now OVERLAY is the overlay that gave us this property, or nil
3965 if it was a text property. */
3967 if (!STRINGP (it->string))
3968 object = it->w->buffer;
3970 if (CONSP (prop)
3971 /* Simple properties. */
3972 && !EQ (XCAR (prop), Qimage)
3973 && !EQ (XCAR (prop), Qspace)
3974 && !EQ (XCAR (prop), Qwhen)
3975 && !EQ (XCAR (prop), Qslice)
3976 && !EQ (XCAR (prop), Qspace_width)
3977 && !EQ (XCAR (prop), Qheight)
3978 && !EQ (XCAR (prop), Qraise)
3979 /* Marginal area specifications. */
3980 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3981 && !EQ (XCAR (prop), Qleft_fringe)
3982 && !EQ (XCAR (prop), Qright_fringe)
3983 && !NILP (XCAR (prop)))
3985 for (; CONSP (prop); prop = XCDR (prop))
3987 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3988 position, display_replaced_p))
3990 display_replaced_p = 1;
3991 /* If some text in a string is replaced, `position' no
3992 longer points to the position of `object'. */
3993 if (STRINGP (object))
3994 break;
3998 else if (VECTORP (prop))
4000 int i;
4001 for (i = 0; i < ASIZE (prop); ++i)
4002 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4003 position, display_replaced_p))
4005 display_replaced_p = 1;
4006 /* If some text in a string is replaced, `position' no
4007 longer points to the position of `object'. */
4008 if (STRINGP (object))
4009 break;
4012 else
4014 if (handle_single_display_spec (it, prop, object, overlay,
4015 position, 0))
4016 display_replaced_p = 1;
4019 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4023 /* Value is the position of the end of the `display' property starting
4024 at START_POS in OBJECT. */
4026 static struct text_pos
4027 display_prop_end (it, object, start_pos)
4028 struct it *it;
4029 Lisp_Object object;
4030 struct text_pos start_pos;
4032 Lisp_Object end;
4033 struct text_pos end_pos;
4035 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4036 Qdisplay, object, Qnil);
4037 CHARPOS (end_pos) = XFASTINT (end);
4038 if (STRINGP (object))
4039 compute_string_pos (&end_pos, start_pos, it->string);
4040 else
4041 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4043 return end_pos;
4047 /* Set up IT from a single `display' specification PROP. OBJECT
4048 is the object in which the `display' property was found. *POSITION
4049 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4050 means that we previously saw a display specification which already
4051 replaced text display with something else, for example an image;
4052 we ignore such properties after the first one has been processed.
4054 OVERLAY is the overlay this `display' property came from,
4055 or nil if it was a text property.
4057 If PROP is a `space' or `image' specification, and in some other
4058 cases too, set *POSITION to the position where the `display'
4059 property ends.
4061 Value is non-zero if something was found which replaces the display
4062 of buffer or string text. */
4064 static int
4065 handle_single_display_spec (it, spec, object, overlay, position,
4066 display_replaced_before_p)
4067 struct it *it;
4068 Lisp_Object spec;
4069 Lisp_Object object;
4070 Lisp_Object overlay;
4071 struct text_pos *position;
4072 int display_replaced_before_p;
4074 Lisp_Object form;
4075 Lisp_Object location, value;
4076 struct text_pos start_pos, save_pos;
4077 int valid_p;
4079 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4080 If the result is non-nil, use VALUE instead of SPEC. */
4081 form = Qt;
4082 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4084 spec = XCDR (spec);
4085 if (!CONSP (spec))
4086 return 0;
4087 form = XCAR (spec);
4088 spec = XCDR (spec);
4091 if (!NILP (form) && !EQ (form, Qt))
4093 int count = SPECPDL_INDEX ();
4094 struct gcpro gcpro1;
4096 /* Bind `object' to the object having the `display' property, a
4097 buffer or string. Bind `position' to the position in the
4098 object where the property was found, and `buffer-position'
4099 to the current position in the buffer. */
4100 specbind (Qobject, object);
4101 specbind (Qposition, make_number (CHARPOS (*position)));
4102 specbind (Qbuffer_position,
4103 make_number (STRINGP (object)
4104 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4105 GCPRO1 (form);
4106 form = safe_eval (form);
4107 UNGCPRO;
4108 unbind_to (count, Qnil);
4111 if (NILP (form))
4112 return 0;
4114 /* Handle `(height HEIGHT)' specifications. */
4115 if (CONSP (spec)
4116 && EQ (XCAR (spec), Qheight)
4117 && CONSP (XCDR (spec)))
4119 if (!FRAME_WINDOW_P (it->f))
4120 return 0;
4122 it->font_height = XCAR (XCDR (spec));
4123 if (!NILP (it->font_height))
4125 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4126 int new_height = -1;
4128 if (CONSP (it->font_height)
4129 && (EQ (XCAR (it->font_height), Qplus)
4130 || EQ (XCAR (it->font_height), Qminus))
4131 && CONSP (XCDR (it->font_height))
4132 && INTEGERP (XCAR (XCDR (it->font_height))))
4134 /* `(+ N)' or `(- N)' where N is an integer. */
4135 int steps = XINT (XCAR (XCDR (it->font_height)));
4136 if (EQ (XCAR (it->font_height), Qplus))
4137 steps = - steps;
4138 it->face_id = smaller_face (it->f, it->face_id, steps);
4140 else if (FUNCTIONP (it->font_height))
4142 /* Call function with current height as argument.
4143 Value is the new height. */
4144 Lisp_Object height;
4145 height = safe_call1 (it->font_height,
4146 face->lface[LFACE_HEIGHT_INDEX]);
4147 if (NUMBERP (height))
4148 new_height = XFLOATINT (height);
4150 else if (NUMBERP (it->font_height))
4152 /* Value is a multiple of the canonical char height. */
4153 struct face *face;
4155 face = FACE_FROM_ID (it->f,
4156 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4157 new_height = (XFLOATINT (it->font_height)
4158 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4160 else
4162 /* Evaluate IT->font_height with `height' bound to the
4163 current specified height to get the new height. */
4164 int count = SPECPDL_INDEX ();
4166 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4167 value = safe_eval (it->font_height);
4168 unbind_to (count, Qnil);
4170 if (NUMBERP (value))
4171 new_height = XFLOATINT (value);
4174 if (new_height > 0)
4175 it->face_id = face_with_height (it->f, it->face_id, new_height);
4178 return 0;
4181 /* Handle `(space-width WIDTH)'. */
4182 if (CONSP (spec)
4183 && EQ (XCAR (spec), Qspace_width)
4184 && CONSP (XCDR (spec)))
4186 if (!FRAME_WINDOW_P (it->f))
4187 return 0;
4189 value = XCAR (XCDR (spec));
4190 if (NUMBERP (value) && XFLOATINT (value) > 0)
4191 it->space_width = value;
4193 return 0;
4196 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4197 if (CONSP (spec)
4198 && EQ (XCAR (spec), Qslice))
4200 Lisp_Object tem;
4202 if (!FRAME_WINDOW_P (it->f))
4203 return 0;
4205 if (tem = XCDR (spec), CONSP (tem))
4207 it->slice.x = XCAR (tem);
4208 if (tem = XCDR (tem), CONSP (tem))
4210 it->slice.y = XCAR (tem);
4211 if (tem = XCDR (tem), CONSP (tem))
4213 it->slice.width = XCAR (tem);
4214 if (tem = XCDR (tem), CONSP (tem))
4215 it->slice.height = XCAR (tem);
4220 return 0;
4223 /* Handle `(raise FACTOR)'. */
4224 if (CONSP (spec)
4225 && EQ (XCAR (spec), Qraise)
4226 && CONSP (XCDR (spec)))
4228 if (!FRAME_WINDOW_P (it->f))
4229 return 0;
4231 #ifdef HAVE_WINDOW_SYSTEM
4232 value = XCAR (XCDR (spec));
4233 if (NUMBERP (value))
4235 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4236 it->voffset = - (XFLOATINT (value)
4237 * (FONT_HEIGHT (face->font)));
4239 #endif /* HAVE_WINDOW_SYSTEM */
4241 return 0;
4244 /* Don't handle the other kinds of display specifications
4245 inside a string that we got from a `display' property. */
4246 if (it->string_from_display_prop_p)
4247 return 0;
4249 /* Characters having this form of property are not displayed, so
4250 we have to find the end of the property. */
4251 start_pos = *position;
4252 *position = display_prop_end (it, object, start_pos);
4253 value = Qnil;
4255 /* Stop the scan at that end position--we assume that all
4256 text properties change there. */
4257 it->stop_charpos = position->charpos;
4259 /* Handle `(left-fringe BITMAP [FACE])'
4260 and `(right-fringe BITMAP [FACE])'. */
4261 if (CONSP (spec)
4262 && (EQ (XCAR (spec), Qleft_fringe)
4263 || EQ (XCAR (spec), Qright_fringe))
4264 && CONSP (XCDR (spec)))
4266 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4267 int fringe_bitmap;
4269 if (!FRAME_WINDOW_P (it->f))
4270 /* If we return here, POSITION has been advanced
4271 across the text with this property. */
4272 return 0;
4274 #ifdef HAVE_WINDOW_SYSTEM
4275 value = XCAR (XCDR (spec));
4276 if (!SYMBOLP (value)
4277 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4278 /* If we return here, POSITION has been advanced
4279 across the text with this property. */
4280 return 0;
4282 if (CONSP (XCDR (XCDR (spec))))
4284 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4285 int face_id2 = lookup_derived_face (it->f, face_name,
4286 FRINGE_FACE_ID, 0);
4287 if (face_id2 >= 0)
4288 face_id = face_id2;
4291 /* Save current settings of IT so that we can restore them
4292 when we are finished with the glyph property value. */
4294 save_pos = it->position;
4295 it->position = *position;
4296 push_it (it);
4297 it->position = save_pos;
4299 it->area = TEXT_AREA;
4300 it->what = IT_IMAGE;
4301 it->image_id = -1; /* no image */
4302 it->position = start_pos;
4303 it->object = NILP (object) ? it->w->buffer : object;
4304 it->method = GET_FROM_IMAGE;
4305 it->from_overlay = Qnil;
4306 it->face_id = face_id;
4308 /* Say that we haven't consumed the characters with
4309 `display' property yet. The call to pop_it in
4310 set_iterator_to_next will clean this up. */
4311 *position = start_pos;
4313 if (EQ (XCAR (spec), Qleft_fringe))
4315 it->left_user_fringe_bitmap = fringe_bitmap;
4316 it->left_user_fringe_face_id = face_id;
4318 else
4320 it->right_user_fringe_bitmap = fringe_bitmap;
4321 it->right_user_fringe_face_id = face_id;
4323 #endif /* HAVE_WINDOW_SYSTEM */
4324 return 1;
4327 /* Prepare to handle `((margin left-margin) ...)',
4328 `((margin right-margin) ...)' and `((margin nil) ...)'
4329 prefixes for display specifications. */
4330 location = Qunbound;
4331 if (CONSP (spec) && CONSP (XCAR (spec)))
4333 Lisp_Object tem;
4335 value = XCDR (spec);
4336 if (CONSP (value))
4337 value = XCAR (value);
4339 tem = XCAR (spec);
4340 if (EQ (XCAR (tem), Qmargin)
4341 && (tem = XCDR (tem),
4342 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4343 (NILP (tem)
4344 || EQ (tem, Qleft_margin)
4345 || EQ (tem, Qright_margin))))
4346 location = tem;
4349 if (EQ (location, Qunbound))
4351 location = Qnil;
4352 value = spec;
4355 /* After this point, VALUE is the property after any
4356 margin prefix has been stripped. It must be a string,
4357 an image specification, or `(space ...)'.
4359 LOCATION specifies where to display: `left-margin',
4360 `right-margin' or nil. */
4362 valid_p = (STRINGP (value)
4363 #ifdef HAVE_WINDOW_SYSTEM
4364 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4365 #endif /* not HAVE_WINDOW_SYSTEM */
4366 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4368 if (valid_p && !display_replaced_before_p)
4370 /* Save current settings of IT so that we can restore them
4371 when we are finished with the glyph property value. */
4372 save_pos = it->position;
4373 it->position = *position;
4374 push_it (it);
4375 it->position = save_pos;
4376 it->from_overlay = overlay;
4378 if (NILP (location))
4379 it->area = TEXT_AREA;
4380 else if (EQ (location, Qleft_margin))
4381 it->area = LEFT_MARGIN_AREA;
4382 else
4383 it->area = RIGHT_MARGIN_AREA;
4385 if (STRINGP (value))
4387 it->string = value;
4388 it->multibyte_p = STRING_MULTIBYTE (it->string);
4389 it->current.overlay_string_index = -1;
4390 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4391 it->end_charpos = it->string_nchars = SCHARS (it->string);
4392 it->method = GET_FROM_STRING;
4393 it->stop_charpos = 0;
4394 it->string_from_display_prop_p = 1;
4395 /* Say that we haven't consumed the characters with
4396 `display' property yet. The call to pop_it in
4397 set_iterator_to_next will clean this up. */
4398 if (BUFFERP (object))
4399 *position = start_pos;
4401 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4403 it->method = GET_FROM_STRETCH;
4404 it->object = value;
4405 *position = it->position = start_pos;
4407 #ifdef HAVE_WINDOW_SYSTEM
4408 else
4410 it->what = IT_IMAGE;
4411 it->image_id = lookup_image (it->f, value);
4412 it->position = start_pos;
4413 it->object = NILP (object) ? it->w->buffer : object;
4414 it->method = GET_FROM_IMAGE;
4416 /* Say that we haven't consumed the characters with
4417 `display' property yet. The call to pop_it in
4418 set_iterator_to_next will clean this up. */
4419 *position = start_pos;
4421 #endif /* HAVE_WINDOW_SYSTEM */
4423 return 1;
4426 /* Invalid property or property not supported. Restore
4427 POSITION to what it was before. */
4428 *position = start_pos;
4429 return 0;
4433 /* Check if SPEC is a display sub-property value whose text should be
4434 treated as intangible. */
4436 static int
4437 single_display_spec_intangible_p (prop)
4438 Lisp_Object prop;
4440 /* Skip over `when FORM'. */
4441 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4443 prop = XCDR (prop);
4444 if (!CONSP (prop))
4445 return 0;
4446 prop = XCDR (prop);
4449 if (STRINGP (prop))
4450 return 1;
4452 if (!CONSP (prop))
4453 return 0;
4455 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4456 we don't need to treat text as intangible. */
4457 if (EQ (XCAR (prop), Qmargin))
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4463 prop = XCDR (prop);
4464 if (!CONSP (prop)
4465 || EQ (XCAR (prop), Qleft_margin)
4466 || EQ (XCAR (prop), Qright_margin))
4467 return 0;
4470 return (CONSP (prop)
4471 && (EQ (XCAR (prop), Qimage)
4472 || EQ (XCAR (prop), Qspace)));
4476 /* Check if PROP is a display property value whose text should be
4477 treated as intangible. */
4480 display_prop_intangible_p (prop)
4481 Lisp_Object prop;
4483 if (CONSP (prop)
4484 && CONSP (XCAR (prop))
4485 && !EQ (Qmargin, XCAR (XCAR (prop))))
4487 /* A list of sub-properties. */
4488 while (CONSP (prop))
4490 if (single_display_spec_intangible_p (XCAR (prop)))
4491 return 1;
4492 prop = XCDR (prop);
4495 else if (VECTORP (prop))
4497 /* A vector of sub-properties. */
4498 int i;
4499 for (i = 0; i < ASIZE (prop); ++i)
4500 if (single_display_spec_intangible_p (AREF (prop, i)))
4501 return 1;
4503 else
4504 return single_display_spec_intangible_p (prop);
4506 return 0;
4510 /* Return 1 if PROP is a display sub-property value containing STRING. */
4512 static int
4513 single_display_spec_string_p (prop, string)
4514 Lisp_Object prop, string;
4516 if (EQ (string, prop))
4517 return 1;
4519 /* Skip over `when FORM'. */
4520 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4522 prop = XCDR (prop);
4523 if (!CONSP (prop))
4524 return 0;
4525 prop = XCDR (prop);
4528 if (CONSP (prop))
4529 /* Skip over `margin LOCATION'. */
4530 if (EQ (XCAR (prop), Qmargin))
4532 prop = XCDR (prop);
4533 if (!CONSP (prop))
4534 return 0;
4536 prop = XCDR (prop);
4537 if (!CONSP (prop))
4538 return 0;
4541 return CONSP (prop) && EQ (XCAR (prop), string);
4545 /* Return 1 if STRING appears in the `display' property PROP. */
4547 static int
4548 display_prop_string_p (prop, string)
4549 Lisp_Object prop, string;
4551 if (CONSP (prop)
4552 && CONSP (XCAR (prop))
4553 && !EQ (Qmargin, XCAR (XCAR (prop))))
4555 /* A list of sub-properties. */
4556 while (CONSP (prop))
4558 if (single_display_spec_string_p (XCAR (prop), string))
4559 return 1;
4560 prop = XCDR (prop);
4563 else if (VECTORP (prop))
4565 /* A vector of sub-properties. */
4566 int i;
4567 for (i = 0; i < ASIZE (prop); ++i)
4568 if (single_display_spec_string_p (AREF (prop, i), string))
4569 return 1;
4571 else
4572 return single_display_spec_string_p (prop, string);
4574 return 0;
4578 /* Determine from which buffer position in W's buffer STRING comes
4579 from. AROUND_CHARPOS is an approximate position where it could
4580 be from. Value is the buffer position or 0 if it couldn't be
4581 determined.
4583 W's buffer must be current.
4585 This function is necessary because we don't record buffer positions
4586 in glyphs generated from strings (to keep struct glyph small).
4587 This function may only use code that doesn't eval because it is
4588 called asynchronously from note_mouse_highlight. */
4591 string_buffer_position (w, string, around_charpos)
4592 struct window *w;
4593 Lisp_Object string;
4594 int around_charpos;
4596 Lisp_Object limit, prop, pos;
4597 const int MAX_DISTANCE = 1000;
4598 int found = 0;
4600 pos = make_number (around_charpos);
4601 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4602 while (!found && !EQ (pos, limit))
4604 prop = Fget_char_property (pos, Qdisplay, Qnil);
4605 if (!NILP (prop) && display_prop_string_p (prop, string))
4606 found = 1;
4607 else
4608 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4611 if (!found)
4613 pos = make_number (around_charpos);
4614 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4615 while (!found && !EQ (pos, limit))
4617 prop = Fget_char_property (pos, Qdisplay, Qnil);
4618 if (!NILP (prop) && display_prop_string_p (prop, string))
4619 found = 1;
4620 else
4621 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4622 limit);
4626 return found ? XINT (pos) : 0;
4631 /***********************************************************************
4632 `composition' property
4633 ***********************************************************************/
4635 /* Set up iterator IT from `composition' property at its current
4636 position. Called from handle_stop. */
4638 static enum prop_handled
4639 handle_composition_prop (it)
4640 struct it *it;
4642 Lisp_Object prop, string;
4643 EMACS_INT pos, pos_byte, start, end;
4645 if (STRINGP (it->string))
4647 unsigned char *s;
4649 pos = IT_STRING_CHARPOS (*it);
4650 pos_byte = IT_STRING_BYTEPOS (*it);
4651 string = it->string;
4652 s = SDATA (string) + pos_byte;
4653 it->c = STRING_CHAR (s, 0);
4655 else
4657 pos = IT_CHARPOS (*it);
4658 pos_byte = IT_BYTEPOS (*it);
4659 string = Qnil;
4660 it->c = FETCH_CHAR (pos_byte);
4663 /* If there's a valid composition and point is not inside of the
4664 composition (in the case that the composition is from the current
4665 buffer), draw a glyph composed from the composition components. */
4666 if (find_composition (pos, -1, &start, &end, &prop, string)
4667 && COMPOSITION_VALID_P (start, end, prop)
4668 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4670 if (start != pos)
4672 if (STRINGP (it->string))
4673 pos_byte = string_char_to_byte (it->string, start);
4674 else
4675 pos_byte = CHAR_TO_BYTE (start);
4677 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4678 prop, string);
4680 if (it->cmp_it.id >= 0)
4682 it->cmp_it.ch = -1;
4683 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4684 it->cmp_it.nglyphs = -1;
4688 return HANDLED_NORMALLY;
4693 /***********************************************************************
4694 Overlay strings
4695 ***********************************************************************/
4697 /* The following structure is used to record overlay strings for
4698 later sorting in load_overlay_strings. */
4700 struct overlay_entry
4702 Lisp_Object overlay;
4703 Lisp_Object string;
4704 int priority;
4705 int after_string_p;
4709 /* Set up iterator IT from overlay strings at its current position.
4710 Called from handle_stop. */
4712 static enum prop_handled
4713 handle_overlay_change (it)
4714 struct it *it;
4716 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4717 return HANDLED_RECOMPUTE_PROPS;
4718 else
4719 return HANDLED_NORMALLY;
4723 /* Set up the next overlay string for delivery by IT, if there is an
4724 overlay string to deliver. Called by set_iterator_to_next when the
4725 end of the current overlay string is reached. If there are more
4726 overlay strings to display, IT->string and
4727 IT->current.overlay_string_index are set appropriately here.
4728 Otherwise IT->string is set to nil. */
4730 static void
4731 next_overlay_string (it)
4732 struct it *it;
4734 ++it->current.overlay_string_index;
4735 if (it->current.overlay_string_index == it->n_overlay_strings)
4737 /* No more overlay strings. Restore IT's settings to what
4738 they were before overlay strings were processed, and
4739 continue to deliver from current_buffer. */
4741 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4742 pop_it (it);
4743 xassert (it->sp > 0
4744 || (NILP (it->string)
4745 && it->method == GET_FROM_BUFFER
4746 && it->stop_charpos >= BEGV
4747 && it->stop_charpos <= it->end_charpos));
4748 it->current.overlay_string_index = -1;
4749 it->n_overlay_strings = 0;
4751 /* If we're at the end of the buffer, record that we have
4752 processed the overlay strings there already, so that
4753 next_element_from_buffer doesn't try it again. */
4754 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4755 it->overlay_strings_at_end_processed_p = 1;
4757 else
4759 /* There are more overlay strings to process. If
4760 IT->current.overlay_string_index has advanced to a position
4761 where we must load IT->overlay_strings with more strings, do
4762 it. */
4763 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4765 if (it->current.overlay_string_index && i == 0)
4766 load_overlay_strings (it, 0);
4768 /* Initialize IT to deliver display elements from the overlay
4769 string. */
4770 it->string = it->overlay_strings[i];
4771 it->multibyte_p = STRING_MULTIBYTE (it->string);
4772 SET_TEXT_POS (it->current.string_pos, 0, 0);
4773 it->method = GET_FROM_STRING;
4774 it->stop_charpos = 0;
4775 if (it->cmp_it.stop_pos >= 0)
4776 it->cmp_it.stop_pos = 0;
4779 CHECK_IT (it);
4783 /* Compare two overlay_entry structures E1 and E2. Used as a
4784 comparison function for qsort in load_overlay_strings. Overlay
4785 strings for the same position are sorted so that
4787 1. All after-strings come in front of before-strings, except
4788 when they come from the same overlay.
4790 2. Within after-strings, strings are sorted so that overlay strings
4791 from overlays with higher priorities come first.
4793 2. Within before-strings, strings are sorted so that overlay
4794 strings from overlays with higher priorities come last.
4796 Value is analogous to strcmp. */
4799 static int
4800 compare_overlay_entries (e1, e2)
4801 void *e1, *e2;
4803 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4804 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4805 int result;
4807 if (entry1->after_string_p != entry2->after_string_p)
4809 /* Let after-strings appear in front of before-strings if
4810 they come from different overlays. */
4811 if (EQ (entry1->overlay, entry2->overlay))
4812 result = entry1->after_string_p ? 1 : -1;
4813 else
4814 result = entry1->after_string_p ? -1 : 1;
4816 else if (entry1->after_string_p)
4817 /* After-strings sorted in order of decreasing priority. */
4818 result = entry2->priority - entry1->priority;
4819 else
4820 /* Before-strings sorted in order of increasing priority. */
4821 result = entry1->priority - entry2->priority;
4823 return result;
4827 /* Load the vector IT->overlay_strings with overlay strings from IT's
4828 current buffer position, or from CHARPOS if that is > 0. Set
4829 IT->n_overlays to the total number of overlay strings found.
4831 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4832 a time. On entry into load_overlay_strings,
4833 IT->current.overlay_string_index gives the number of overlay
4834 strings that have already been loaded by previous calls to this
4835 function.
4837 IT->add_overlay_start contains an additional overlay start
4838 position to consider for taking overlay strings from, if non-zero.
4839 This position comes into play when the overlay has an `invisible'
4840 property, and both before and after-strings. When we've skipped to
4841 the end of the overlay, because of its `invisible' property, we
4842 nevertheless want its before-string to appear.
4843 IT->add_overlay_start will contain the overlay start position
4844 in this case.
4846 Overlay strings are sorted so that after-string strings come in
4847 front of before-string strings. Within before and after-strings,
4848 strings are sorted by overlay priority. See also function
4849 compare_overlay_entries. */
4851 static void
4852 load_overlay_strings (it, charpos)
4853 struct it *it;
4854 int charpos;
4856 extern Lisp_Object Qwindow, Qpriority;
4857 Lisp_Object overlay, window, str, invisible;
4858 struct Lisp_Overlay *ov;
4859 int start, end;
4860 int size = 20;
4861 int n = 0, i, j, invis_p;
4862 struct overlay_entry *entries
4863 = (struct overlay_entry *) alloca (size * sizeof *entries);
4865 if (charpos <= 0)
4866 charpos = IT_CHARPOS (*it);
4868 /* Append the overlay string STRING of overlay OVERLAY to vector
4869 `entries' which has size `size' and currently contains `n'
4870 elements. AFTER_P non-zero means STRING is an after-string of
4871 OVERLAY. */
4872 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4873 do \
4875 Lisp_Object priority; \
4877 if (n == size) \
4879 int new_size = 2 * size; \
4880 struct overlay_entry *old = entries; \
4881 entries = \
4882 (struct overlay_entry *) alloca (new_size \
4883 * sizeof *entries); \
4884 bcopy (old, entries, size * sizeof *entries); \
4885 size = new_size; \
4888 entries[n].string = (STRING); \
4889 entries[n].overlay = (OVERLAY); \
4890 priority = Foverlay_get ((OVERLAY), Qpriority); \
4891 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4892 entries[n].after_string_p = (AFTER_P); \
4893 ++n; \
4895 while (0)
4897 /* Process overlay before the overlay center. */
4898 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4900 XSETMISC (overlay, ov);
4901 xassert (OVERLAYP (overlay));
4902 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4903 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4905 if (end < charpos)
4906 break;
4908 /* Skip this overlay if it doesn't start or end at IT's current
4909 position. */
4910 if (end != charpos && start != charpos)
4911 continue;
4913 /* Skip this overlay if it doesn't apply to IT->w. */
4914 window = Foverlay_get (overlay, Qwindow);
4915 if (WINDOWP (window) && XWINDOW (window) != it->w)
4916 continue;
4918 /* If the text ``under'' the overlay is invisible, both before-
4919 and after-strings from this overlay are visible; start and
4920 end position are indistinguishable. */
4921 invisible = Foverlay_get (overlay, Qinvisible);
4922 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4924 /* If overlay has a non-empty before-string, record it. */
4925 if ((start == charpos || (end == charpos && invis_p))
4926 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4927 && SCHARS (str))
4928 RECORD_OVERLAY_STRING (overlay, str, 0);
4930 /* If overlay has a non-empty after-string, record it. */
4931 if ((end == charpos || (start == charpos && invis_p))
4932 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4933 && SCHARS (str))
4934 RECORD_OVERLAY_STRING (overlay, str, 1);
4937 /* Process overlays after the overlay center. */
4938 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4940 XSETMISC (overlay, ov);
4941 xassert (OVERLAYP (overlay));
4942 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4943 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4945 if (start > charpos)
4946 break;
4948 /* Skip this overlay if it doesn't start or end at IT's current
4949 position. */
4950 if (end != charpos && start != charpos)
4951 continue;
4953 /* Skip this overlay if it doesn't apply to IT->w. */
4954 window = Foverlay_get (overlay, Qwindow);
4955 if (WINDOWP (window) && XWINDOW (window) != it->w)
4956 continue;
4958 /* If the text ``under'' the overlay is invisible, it has a zero
4959 dimension, and both before- and after-strings apply. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4976 #undef RECORD_OVERLAY_STRING
4978 /* Sort entries. */
4979 if (n > 1)
4980 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4982 /* Record the total number of strings to process. */
4983 it->n_overlay_strings = n;
4985 /* IT->current.overlay_string_index is the number of overlay strings
4986 that have already been consumed by IT. Copy some of the
4987 remaining overlay strings to IT->overlay_strings. */
4988 i = 0;
4989 j = it->current.overlay_string_index;
4990 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4992 it->overlay_strings[i] = entries[j].string;
4993 it->string_overlays[i++] = entries[j++].overlay;
4996 CHECK_IT (it);
5000 /* Get the first chunk of overlay strings at IT's current buffer
5001 position, or at CHARPOS if that is > 0. Value is non-zero if at
5002 least one overlay string was found. */
5004 static int
5005 get_overlay_strings_1 (it, charpos, compute_stop_p)
5006 struct it *it;
5007 int charpos;
5008 int compute_stop_p;
5010 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5011 process. This fills IT->overlay_strings with strings, and sets
5012 IT->n_overlay_strings to the total number of strings to process.
5013 IT->pos.overlay_string_index has to be set temporarily to zero
5014 because load_overlay_strings needs this; it must be set to -1
5015 when no overlay strings are found because a zero value would
5016 indicate a position in the first overlay string. */
5017 it->current.overlay_string_index = 0;
5018 load_overlay_strings (it, charpos);
5020 /* If we found overlay strings, set up IT to deliver display
5021 elements from the first one. Otherwise set up IT to deliver
5022 from current_buffer. */
5023 if (it->n_overlay_strings)
5025 /* Make sure we know settings in current_buffer, so that we can
5026 restore meaningful values when we're done with the overlay
5027 strings. */
5028 if (compute_stop_p)
5029 compute_stop_pos (it);
5030 xassert (it->face_id >= 0);
5032 /* Save IT's settings. They are restored after all overlay
5033 strings have been processed. */
5034 xassert (!compute_stop_p || it->sp == 0);
5036 /* When called from handle_stop, there might be an empty display
5037 string loaded. In that case, don't bother saving it. */
5038 if (!STRINGP (it->string) || SCHARS (it->string))
5039 push_it (it);
5041 /* Set up IT to deliver display elements from the first overlay
5042 string. */
5043 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5044 it->string = it->overlay_strings[0];
5045 it->from_overlay = Qnil;
5046 it->stop_charpos = 0;
5047 xassert (STRINGP (it->string));
5048 it->end_charpos = SCHARS (it->string);
5049 it->multibyte_p = STRING_MULTIBYTE (it->string);
5050 it->method = GET_FROM_STRING;
5051 return 1;
5054 it->current.overlay_string_index = -1;
5055 return 0;
5058 static int
5059 get_overlay_strings (it, charpos)
5060 struct it *it;
5061 int charpos;
5063 it->string = Qnil;
5064 it->method = GET_FROM_BUFFER;
5066 (void) get_overlay_strings_1 (it, charpos, 1);
5068 CHECK_IT (it);
5070 /* Value is non-zero if we found at least one overlay string. */
5071 return STRINGP (it->string);
5076 /***********************************************************************
5077 Saving and restoring state
5078 ***********************************************************************/
5080 /* Save current settings of IT on IT->stack. Called, for example,
5081 before setting up IT for an overlay string, to be able to restore
5082 IT's settings to what they were after the overlay string has been
5083 processed. */
5085 static void
5086 push_it (it)
5087 struct it *it;
5089 struct iterator_stack_entry *p;
5091 xassert (it->sp < IT_STACK_SIZE);
5092 p = it->stack + it->sp;
5094 p->stop_charpos = it->stop_charpos;
5095 p->cmp_it = it->cmp_it;
5096 xassert (it->face_id >= 0);
5097 p->face_id = it->face_id;
5098 p->string = it->string;
5099 p->method = it->method;
5100 p->from_overlay = it->from_overlay;
5101 switch (p->method)
5103 case GET_FROM_IMAGE:
5104 p->u.image.object = it->object;
5105 p->u.image.image_id = it->image_id;
5106 p->u.image.slice = it->slice;
5107 break;
5108 case GET_FROM_STRETCH:
5109 p->u.stretch.object = it->object;
5110 break;
5112 p->position = it->position;
5113 p->current = it->current;
5114 p->end_charpos = it->end_charpos;
5115 p->string_nchars = it->string_nchars;
5116 p->area = it->area;
5117 p->multibyte_p = it->multibyte_p;
5118 p->avoid_cursor_p = it->avoid_cursor_p;
5119 p->space_width = it->space_width;
5120 p->font_height = it->font_height;
5121 p->voffset = it->voffset;
5122 p->string_from_display_prop_p = it->string_from_display_prop_p;
5123 p->display_ellipsis_p = 0;
5124 p->line_wrap = it->line_wrap;
5125 ++it->sp;
5129 /* Restore IT's settings from IT->stack. Called, for example, when no
5130 more overlay strings must be processed, and we return to delivering
5131 display elements from a buffer, or when the end of a string from a
5132 `display' property is reached and we return to delivering display
5133 elements from an overlay string, or from a buffer. */
5135 static void
5136 pop_it (it)
5137 struct it *it;
5139 struct iterator_stack_entry *p;
5141 xassert (it->sp > 0);
5142 --it->sp;
5143 p = it->stack + it->sp;
5144 it->stop_charpos = p->stop_charpos;
5145 it->cmp_it = p->cmp_it;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 it->from_overlay = p->from_overlay;
5151 if (NILP (it->string))
5152 SET_TEXT_POS (it->current.string_pos, -1, -1);
5153 it->method = p->method;
5154 switch (it->method)
5156 case GET_FROM_IMAGE:
5157 it->image_id = p->u.image.image_id;
5158 it->object = p->u.image.object;
5159 it->slice = p->u.image.slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 it->object = p->u.comp.object;
5163 break;
5164 case GET_FROM_BUFFER:
5165 it->object = it->w->buffer;
5166 break;
5167 case GET_FROM_STRING:
5168 it->object = it->string;
5169 break;
5170 case GET_FROM_DISPLAY_VECTOR:
5171 if (it->s)
5172 it->method = GET_FROM_C_STRING;
5173 else if (STRINGP (it->string))
5174 it->method = GET_FROM_STRING;
5175 else
5177 it->method = GET_FROM_BUFFER;
5178 it->object = it->w->buffer;
5181 it->end_charpos = p->end_charpos;
5182 it->string_nchars = p->string_nchars;
5183 it->area = p->area;
5184 it->multibyte_p = p->multibyte_p;
5185 it->avoid_cursor_p = p->avoid_cursor_p;
5186 it->space_width = p->space_width;
5187 it->font_height = p->font_height;
5188 it->voffset = p->voffset;
5189 it->string_from_display_prop_p = p->string_from_display_prop_p;
5190 it->line_wrap = p->line_wrap;
5195 /***********************************************************************
5196 Moving over lines
5197 ***********************************************************************/
5199 /* Set IT's current position to the previous line start. */
5201 static void
5202 back_to_previous_line_start (it)
5203 struct it *it;
5205 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5210 /* Move IT to the next line start.
5212 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5213 we skipped over part of the text (as opposed to moving the iterator
5214 continuously over the text). Otherwise, don't change the value
5215 of *SKIPPED_P.
5217 Newlines may come from buffer text, overlay strings, or strings
5218 displayed via the `display' property. That's the reason we can't
5219 simply use find_next_newline_no_quit.
5221 Note that this function may not skip over invisible text that is so
5222 because of text properties and immediately follows a newline. If
5223 it would, function reseat_at_next_visible_line_start, when called
5224 from set_iterator_to_next, would effectively make invisible
5225 characters following a newline part of the wrong glyph row, which
5226 leads to wrong cursor motion. */
5228 static int
5229 forward_to_next_line_start (it, skipped_p)
5230 struct it *it;
5231 int *skipped_p;
5233 int old_selective, newline_found_p, n;
5234 const int MAX_NEWLINE_DISTANCE = 500;
5236 /* If already on a newline, just consume it to avoid unintended
5237 skipping over invisible text below. */
5238 if (it->what == IT_CHARACTER
5239 && it->c == '\n'
5240 && CHARPOS (it->position) == IT_CHARPOS (*it))
5242 set_iterator_to_next (it, 0);
5243 it->c = 0;
5244 return 1;
5247 /* Don't handle selective display in the following. It's (a)
5248 unnecessary because it's done by the caller, and (b) leads to an
5249 infinite recursion because next_element_from_ellipsis indirectly
5250 calls this function. */
5251 old_selective = it->selective;
5252 it->selective = 0;
5254 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5255 from buffer text. */
5256 for (n = newline_found_p = 0;
5257 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5258 n += STRINGP (it->string) ? 0 : 1)
5260 if (!get_next_display_element (it))
5261 return 0;
5262 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5263 set_iterator_to_next (it, 0);
5266 /* If we didn't find a newline near enough, see if we can use a
5267 short-cut. */
5268 if (!newline_found_p)
5270 int start = IT_CHARPOS (*it);
5271 int limit = find_next_newline_no_quit (start, 1);
5272 Lisp_Object pos;
5274 xassert (!STRINGP (it->string));
5276 /* If there isn't any `display' property in sight, and no
5277 overlays, we can just use the position of the newline in
5278 buffer text. */
5279 if (it->stop_charpos >= limit
5280 || ((pos = Fnext_single_property_change (make_number (start),
5281 Qdisplay,
5282 Qnil, make_number (limit)),
5283 NILP (pos))
5284 && next_overlay_change (start) == ZV))
5286 IT_CHARPOS (*it) = limit;
5287 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5288 *skipped_p = newline_found_p = 1;
5290 else
5292 while (get_next_display_element (it)
5293 && !newline_found_p)
5295 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5296 set_iterator_to_next (it, 0);
5301 it->selective = old_selective;
5302 return newline_found_p;
5306 /* Set IT's current position to the previous visible line start. Skip
5307 invisible text that is so either due to text properties or due to
5308 selective display. Caution: this does not change IT->current_x and
5309 IT->hpos. */
5311 static void
5312 back_to_previous_visible_line_start (it)
5313 struct it *it;
5315 while (IT_CHARPOS (*it) > BEGV)
5317 back_to_previous_line_start (it);
5319 if (IT_CHARPOS (*it) <= BEGV)
5320 break;
5322 /* If selective > 0, then lines indented more than that values
5323 are invisible. */
5324 if (it->selective > 0
5325 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5326 (double) it->selective)) /* iftc */
5327 continue;
5329 /* Check the newline before point for invisibility. */
5331 Lisp_Object prop;
5332 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5333 Qinvisible, it->window);
5334 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5335 continue;
5338 if (IT_CHARPOS (*it) <= BEGV)
5339 break;
5342 struct it it2;
5343 int pos;
5344 EMACS_INT beg, end;
5345 Lisp_Object val, overlay;
5347 /* If newline is part of a composition, continue from start of composition */
5348 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5349 && beg < IT_CHARPOS (*it))
5350 goto replaced;
5352 /* If newline is replaced by a display property, find start of overlay
5353 or interval and continue search from that point. */
5354 it2 = *it;
5355 pos = --IT_CHARPOS (it2);
5356 --IT_BYTEPOS (it2);
5357 it2.sp = 0;
5358 it2.string_from_display_prop_p = 0;
5359 if (handle_display_prop (&it2) == HANDLED_RETURN
5360 && !NILP (val = get_char_property_and_overlay
5361 (make_number (pos), Qdisplay, Qnil, &overlay))
5362 && (OVERLAYP (overlay)
5363 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5364 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5365 goto replaced;
5367 /* Newline is not replaced by anything -- so we are done. */
5368 break;
5370 replaced:
5371 if (beg < BEGV)
5372 beg = BEGV;
5373 IT_CHARPOS (*it) = beg;
5374 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5378 it->continuation_lines_width = 0;
5380 xassert (IT_CHARPOS (*it) >= BEGV);
5381 xassert (IT_CHARPOS (*it) == BEGV
5382 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5383 CHECK_IT (it);
5387 /* Reseat iterator IT at the previous visible line start. Skip
5388 invisible text that is so either due to text properties or due to
5389 selective display. At the end, update IT's overlay information,
5390 face information etc. */
5392 void
5393 reseat_at_previous_visible_line_start (it)
5394 struct it *it;
5396 back_to_previous_visible_line_start (it);
5397 reseat (it, it->current.pos, 1);
5398 CHECK_IT (it);
5402 /* Reseat iterator IT on the next visible line start in the current
5403 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5404 preceding the line start. Skip over invisible text that is so
5405 because of selective display. Compute faces, overlays etc at the
5406 new position. Note that this function does not skip over text that
5407 is invisible because of text properties. */
5409 static void
5410 reseat_at_next_visible_line_start (it, on_newline_p)
5411 struct it *it;
5412 int on_newline_p;
5414 int newline_found_p, skipped_p = 0;
5416 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5418 /* Skip over lines that are invisible because they are indented
5419 more than the value of IT->selective. */
5420 if (it->selective > 0)
5421 while (IT_CHARPOS (*it) < ZV
5422 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5423 (double) it->selective)) /* iftc */
5425 xassert (IT_BYTEPOS (*it) == BEGV
5426 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5427 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5430 /* Position on the newline if that's what's requested. */
5431 if (on_newline_p && newline_found_p)
5433 if (STRINGP (it->string))
5435 if (IT_STRING_CHARPOS (*it) > 0)
5437 --IT_STRING_CHARPOS (*it);
5438 --IT_STRING_BYTEPOS (*it);
5441 else if (IT_CHARPOS (*it) > BEGV)
5443 --IT_CHARPOS (*it);
5444 --IT_BYTEPOS (*it);
5445 reseat (it, it->current.pos, 0);
5448 else if (skipped_p)
5449 reseat (it, it->current.pos, 0);
5451 CHECK_IT (it);
5456 /***********************************************************************
5457 Changing an iterator's position
5458 ***********************************************************************/
5460 /* Change IT's current position to POS in current_buffer. If FORCE_P
5461 is non-zero, always check for text properties at the new position.
5462 Otherwise, text properties are only looked up if POS >=
5463 IT->check_charpos of a property. */
5465 static void
5466 reseat (it, pos, force_p)
5467 struct it *it;
5468 struct text_pos pos;
5469 int force_p;
5471 int original_pos = IT_CHARPOS (*it);
5473 reseat_1 (it, pos, 0);
5475 /* Determine where to check text properties. Avoid doing it
5476 where possible because text property lookup is very expensive. */
5477 if (force_p
5478 || CHARPOS (pos) > it->stop_charpos
5479 || CHARPOS (pos) < original_pos)
5480 handle_stop (it);
5482 CHECK_IT (it);
5486 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5487 IT->stop_pos to POS, also. */
5489 static void
5490 reseat_1 (it, pos, set_stop_p)
5491 struct it *it;
5492 struct text_pos pos;
5493 int set_stop_p;
5495 /* Don't call this function when scanning a C string. */
5496 xassert (it->s == NULL);
5498 /* POS must be a reasonable value. */
5499 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5501 it->current.pos = it->position = pos;
5502 it->end_charpos = ZV;
5503 it->dpvec = NULL;
5504 it->current.dpvec_index = -1;
5505 it->current.overlay_string_index = -1;
5506 IT_STRING_CHARPOS (*it) = -1;
5507 IT_STRING_BYTEPOS (*it) = -1;
5508 it->string = Qnil;
5509 it->string_from_display_prop_p = 0;
5510 it->method = GET_FROM_BUFFER;
5511 it->object = it->w->buffer;
5512 it->area = TEXT_AREA;
5513 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5514 it->sp = 0;
5515 it->string_from_display_prop_p = 0;
5516 it->face_before_selective_p = 0;
5518 if (set_stop_p)
5519 it->stop_charpos = CHARPOS (pos);
5523 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5524 If S is non-null, it is a C string to iterate over. Otherwise,
5525 STRING gives a Lisp string to iterate over.
5527 If PRECISION > 0, don't return more then PRECISION number of
5528 characters from the string.
5530 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5531 characters have been returned. FIELD_WIDTH < 0 means an infinite
5532 field width.
5534 MULTIBYTE = 0 means disable processing of multibyte characters,
5535 MULTIBYTE > 0 means enable it,
5536 MULTIBYTE < 0 means use IT->multibyte_p.
5538 IT must be initialized via a prior call to init_iterator before
5539 calling this function. */
5541 static void
5542 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5543 struct it *it;
5544 unsigned char *s;
5545 Lisp_Object string;
5546 int charpos;
5547 int precision, field_width, multibyte;
5549 /* No region in strings. */
5550 it->region_beg_charpos = it->region_end_charpos = -1;
5552 /* No text property checks performed by default, but see below. */
5553 it->stop_charpos = -1;
5555 /* Set iterator position and end position. */
5556 bzero (&it->current, sizeof it->current);
5557 it->current.overlay_string_index = -1;
5558 it->current.dpvec_index = -1;
5559 xassert (charpos >= 0);
5561 /* If STRING is specified, use its multibyteness, otherwise use the
5562 setting of MULTIBYTE, if specified. */
5563 if (multibyte >= 0)
5564 it->multibyte_p = multibyte > 0;
5566 if (s == NULL)
5568 xassert (STRINGP (string));
5569 it->string = string;
5570 it->s = NULL;
5571 it->end_charpos = it->string_nchars = SCHARS (string);
5572 it->method = GET_FROM_STRING;
5573 it->current.string_pos = string_pos (charpos, string);
5575 else
5577 it->s = s;
5578 it->string = Qnil;
5580 /* Note that we use IT->current.pos, not it->current.string_pos,
5581 for displaying C strings. */
5582 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5583 if (it->multibyte_p)
5585 it->current.pos = c_string_pos (charpos, s, 1);
5586 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5588 else
5590 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5591 it->end_charpos = it->string_nchars = strlen (s);
5594 it->method = GET_FROM_C_STRING;
5597 /* PRECISION > 0 means don't return more than PRECISION characters
5598 from the string. */
5599 if (precision > 0 && it->end_charpos - charpos > precision)
5600 it->end_charpos = it->string_nchars = charpos + precision;
5602 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5603 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5604 FIELD_WIDTH < 0 means infinite field width. This is useful for
5605 padding with `-' at the end of a mode line. */
5606 if (field_width < 0)
5607 field_width = INFINITY;
5608 if (field_width > it->end_charpos - charpos)
5609 it->end_charpos = charpos + field_width;
5611 /* Use the standard display table for displaying strings. */
5612 if (DISP_TABLE_P (Vstandard_display_table))
5613 it->dp = XCHAR_TABLE (Vstandard_display_table);
5615 it->stop_charpos = charpos;
5616 CHECK_IT (it);
5621 /***********************************************************************
5622 Iteration
5623 ***********************************************************************/
5625 /* Map enum it_method value to corresponding next_element_from_* function. */
5627 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5629 next_element_from_buffer,
5630 next_element_from_display_vector,
5631 next_element_from_string,
5632 next_element_from_c_string,
5633 next_element_from_image,
5634 next_element_from_stretch
5637 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5640 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5641 (possibly with the following characters). */
5643 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5644 ((IT)->cmp_it.id >= 0 \
5645 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5646 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5647 (IT)->end_charpos, (IT)->w, \
5648 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5649 (IT)->string)))
5652 /* Load IT's display element fields with information about the next
5653 display element from the current position of IT. Value is zero if
5654 end of buffer (or C string) is reached. */
5656 static struct frame *last_escape_glyph_frame = NULL;
5657 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5658 static int last_escape_glyph_merged_face_id = 0;
5661 get_next_display_element (it)
5662 struct it *it;
5664 /* Non-zero means that we found a display element. Zero means that
5665 we hit the end of what we iterate over. Performance note: the
5666 function pointer `method' used here turns out to be faster than
5667 using a sequence of if-statements. */
5668 int success_p;
5670 get_next:
5671 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5673 if (it->what == IT_CHARACTER)
5675 /* Map via display table or translate control characters.
5676 IT->c, IT->len etc. have been set to the next character by
5677 the function call above. If we have a display table, and it
5678 contains an entry for IT->c, translate it. Don't do this if
5679 IT->c itself comes from a display table, otherwise we could
5680 end up in an infinite recursion. (An alternative could be to
5681 count the recursion depth of this function and signal an
5682 error when a certain maximum depth is reached.) Is it worth
5683 it? */
5684 if (success_p && it->dpvec == NULL)
5686 Lisp_Object dv;
5687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5689 if (it->dp
5690 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5691 VECTORP (dv)))
5693 struct Lisp_Vector *v = XVECTOR (dv);
5695 /* Return the first character from the display table
5696 entry, if not empty. If empty, don't display the
5697 current character. */
5698 if (v->size)
5700 it->dpvec_char_len = it->len;
5701 it->dpvec = v->contents;
5702 it->dpend = v->contents + v->size;
5703 it->current.dpvec_index = 0;
5704 it->dpvec_face_id = -1;
5705 it->saved_face_id = it->face_id;
5706 it->method = GET_FROM_DISPLAY_VECTOR;
5707 it->ellipsis_p = 0;
5709 else
5711 set_iterator_to_next (it, 0);
5713 goto get_next;
5716 /* Translate control characters into `\003' or `^C' form.
5717 Control characters coming from a display table entry are
5718 currently not translated because we use IT->dpvec to hold
5719 the translation. This could easily be changed but I
5720 don't believe that it is worth doing.
5722 If it->multibyte_p is nonzero, non-printable non-ASCII
5723 characters are also translated to octal form.
5725 If it->multibyte_p is zero, eight-bit characters that
5726 don't have corresponding multibyte char code are also
5727 translated to octal form. */
5728 else if ((it->c < ' '
5729 ? (it->area != TEXT_AREA
5730 /* In mode line, treat \n, \t like other crl chars. */
5731 || (it->c != '\t'
5732 && it->glyph_row
5733 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5734 || (it->c != '\n' && it->c != '\t'))
5735 : (it->multibyte_p
5736 ? (!CHAR_PRINTABLE_P (it->c)
5737 || (!NILP (Vnobreak_char_display)
5738 && (it->c == 0xA0 /* NO-BREAK SPACE */
5739 || it->c == 0xAD /* SOFT HYPHEN */)))
5740 : (it->c >= 127
5741 && (! unibyte_display_via_language_environment
5742 || (DECODE_CHAR (unibyte, it->c) <= 0xA0))))))
5744 /* IT->c is a control character which must be displayed
5745 either as '\003' or as `^C' where the '\\' and '^'
5746 can be defined in the display table. Fill
5747 IT->ctl_chars with glyphs for what we have to
5748 display. Then, set IT->dpvec to these glyphs. */
5749 Lisp_Object gc;
5750 int ctl_len;
5751 int face_id, lface_id = 0 ;
5752 int escape_glyph;
5754 /* Handle control characters with ^. */
5756 if (it->c < 128 && it->ctl_arrow_p)
5758 int g;
5760 g = '^'; /* default glyph for Control */
5761 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5762 if (it->dp
5763 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5764 && GLYPH_CODE_CHAR_VALID_P (gc))
5766 g = GLYPH_CODE_CHAR (gc);
5767 lface_id = GLYPH_CODE_FACE (gc);
5769 if (lface_id)
5771 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5773 else if (it->f == last_escape_glyph_frame
5774 && it->face_id == last_escape_glyph_face_id)
5776 face_id = last_escape_glyph_merged_face_id;
5778 else
5780 /* Merge the escape-glyph face into the current face. */
5781 face_id = merge_faces (it->f, Qescape_glyph, 0,
5782 it->face_id);
5783 last_escape_glyph_frame = it->f;
5784 last_escape_glyph_face_id = it->face_id;
5785 last_escape_glyph_merged_face_id = face_id;
5788 XSETINT (it->ctl_chars[0], g);
5789 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5790 ctl_len = 2;
5791 goto display_control;
5794 /* Handle non-break space in the mode where it only gets
5795 highlighting. */
5797 if (EQ (Vnobreak_char_display, Qt)
5798 && it->c == 0xA0)
5800 /* Merge the no-break-space face into the current face. */
5801 face_id = merge_faces (it->f, Qnobreak_space, 0,
5802 it->face_id);
5804 it->c = ' ';
5805 XSETINT (it->ctl_chars[0], ' ');
5806 ctl_len = 1;
5807 goto display_control;
5810 /* Handle sequences that start with the "escape glyph". */
5812 /* the default escape glyph is \. */
5813 escape_glyph = '\\';
5815 if (it->dp
5816 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5817 && GLYPH_CODE_CHAR_VALID_P (gc))
5819 escape_glyph = GLYPH_CODE_CHAR (gc);
5820 lface_id = GLYPH_CODE_FACE (gc);
5822 if (lface_id)
5824 /* The display table specified a face.
5825 Merge it into face_id and also into escape_glyph. */
5826 face_id = merge_faces (it->f, Qt, lface_id,
5827 it->face_id);
5829 else if (it->f == last_escape_glyph_frame
5830 && it->face_id == last_escape_glyph_face_id)
5832 face_id = last_escape_glyph_merged_face_id;
5834 else
5836 /* Merge the escape-glyph face into the current face. */
5837 face_id = merge_faces (it->f, Qescape_glyph, 0,
5838 it->face_id);
5839 last_escape_glyph_frame = it->f;
5840 last_escape_glyph_face_id = it->face_id;
5841 last_escape_glyph_merged_face_id = face_id;
5844 /* Handle soft hyphens in the mode where they only get
5845 highlighting. */
5847 if (EQ (Vnobreak_char_display, Qt)
5848 && it->c == 0xAD)
5850 it->c = '-';
5851 XSETINT (it->ctl_chars[0], '-');
5852 ctl_len = 1;
5853 goto display_control;
5856 /* Handle non-break space and soft hyphen
5857 with the escape glyph. */
5859 if (it->c == 0xA0 || it->c == 0xAD)
5861 XSETINT (it->ctl_chars[0], escape_glyph);
5862 it->c = (it->c == 0xA0 ? ' ' : '-');
5863 XSETINT (it->ctl_chars[1], it->c);
5864 ctl_len = 2;
5865 goto display_control;
5869 unsigned char str[MAX_MULTIBYTE_LENGTH];
5870 int len;
5871 int i;
5873 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5874 if (CHAR_BYTE8_P (it->c))
5876 str[0] = CHAR_TO_BYTE8 (it->c);
5877 len = 1;
5879 else if (it->c < 256)
5881 str[0] = it->c;
5882 len = 1;
5884 else
5886 /* It's an invalid character, which shouldn't
5887 happen actually, but due to bugs it may
5888 happen. Let's print the char as is, there's
5889 not much meaningful we can do with it. */
5890 str[0] = it->c;
5891 str[1] = it->c >> 8;
5892 str[2] = it->c >> 16;
5893 str[3] = it->c >> 24;
5894 len = 4;
5897 for (i = 0; i < len; i++)
5899 int g;
5900 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5901 /* Insert three more glyphs into IT->ctl_chars for
5902 the octal display of the character. */
5903 g = ((str[i] >> 6) & 7) + '0';
5904 XSETINT (it->ctl_chars[i * 4 + 1], g);
5905 g = ((str[i] >> 3) & 7) + '0';
5906 XSETINT (it->ctl_chars[i * 4 + 2], g);
5907 g = (str[i] & 7) + '0';
5908 XSETINT (it->ctl_chars[i * 4 + 3], g);
5910 ctl_len = len * 4;
5913 display_control:
5914 /* Set up IT->dpvec and return first character from it. */
5915 it->dpvec_char_len = it->len;
5916 it->dpvec = it->ctl_chars;
5917 it->dpend = it->dpvec + ctl_len;
5918 it->current.dpvec_index = 0;
5919 it->dpvec_face_id = face_id;
5920 it->saved_face_id = it->face_id;
5921 it->method = GET_FROM_DISPLAY_VECTOR;
5922 it->ellipsis_p = 0;
5923 goto get_next;
5928 #ifdef HAVE_WINDOW_SYSTEM
5929 /* Adjust face id for a multibyte character. There are no multibyte
5930 character in unibyte text. */
5931 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5932 && it->multibyte_p
5933 && success_p
5934 && FRAME_WINDOW_P (it->f))
5936 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5938 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5940 /* Automatic composition with glyph-string. */
5941 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5943 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5945 else
5947 int pos = (it->s ? -1
5948 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5949 : IT_CHARPOS (*it));
5951 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5954 #endif
5956 /* Is this character the last one of a run of characters with
5957 box? If yes, set IT->end_of_box_run_p to 1. */
5958 if (it->face_box_p
5959 && it->s == NULL)
5961 if (it->method == GET_FROM_STRING && it->sp)
5963 int face_id = underlying_face_id (it);
5964 struct face *face = FACE_FROM_ID (it->f, face_id);
5966 if (face)
5968 if (face->box == FACE_NO_BOX)
5970 /* If the box comes from face properties in a
5971 display string, check faces in that string. */
5972 int string_face_id = face_after_it_pos (it);
5973 it->end_of_box_run_p
5974 = (FACE_FROM_ID (it->f, string_face_id)->box
5975 == FACE_NO_BOX);
5977 /* Otherwise, the box comes from the underlying face.
5978 If this is the last string character displayed, check
5979 the next buffer location. */
5980 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5981 && (it->current.overlay_string_index
5982 == it->n_overlay_strings - 1))
5984 EMACS_INT ignore;
5985 int next_face_id;
5986 struct text_pos pos = it->current.pos;
5987 INC_TEXT_POS (pos, it->multibyte_p);
5989 next_face_id = face_at_buffer_position
5990 (it->w, CHARPOS (pos), it->region_beg_charpos,
5991 it->region_end_charpos, &ignore,
5992 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5993 -1);
5994 it->end_of_box_run_p
5995 = (FACE_FROM_ID (it->f, next_face_id)->box
5996 == FACE_NO_BOX);
6000 else
6002 int face_id = face_after_it_pos (it);
6003 it->end_of_box_run_p
6004 = (face_id != it->face_id
6005 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6009 /* Value is 0 if end of buffer or string reached. */
6010 return success_p;
6014 /* Move IT to the next display element.
6016 RESEAT_P non-zero means if called on a newline in buffer text,
6017 skip to the next visible line start.
6019 Functions get_next_display_element and set_iterator_to_next are
6020 separate because I find this arrangement easier to handle than a
6021 get_next_display_element function that also increments IT's
6022 position. The way it is we can first look at an iterator's current
6023 display element, decide whether it fits on a line, and if it does,
6024 increment the iterator position. The other way around we probably
6025 would either need a flag indicating whether the iterator has to be
6026 incremented the next time, or we would have to implement a
6027 decrement position function which would not be easy to write. */
6029 void
6030 set_iterator_to_next (it, reseat_p)
6031 struct it *it;
6032 int reseat_p;
6034 /* Reset flags indicating start and end of a sequence of characters
6035 with box. Reset them at the start of this function because
6036 moving the iterator to a new position might set them. */
6037 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6039 switch (it->method)
6041 case GET_FROM_BUFFER:
6042 /* The current display element of IT is a character from
6043 current_buffer. Advance in the buffer, and maybe skip over
6044 invisible lines that are so because of selective display. */
6045 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6046 reseat_at_next_visible_line_start (it, 0);
6047 else if (it->cmp_it.id >= 0)
6049 IT_CHARPOS (*it) += it->cmp_it.nchars;
6050 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6051 if (it->cmp_it.to < it->cmp_it.nglyphs)
6052 it->cmp_it.from = it->cmp_it.to;
6053 else
6055 it->cmp_it.id = -1;
6056 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6057 IT_BYTEPOS (*it), it->stop_charpos,
6058 Qnil);
6061 else
6063 xassert (it->len != 0);
6064 IT_BYTEPOS (*it) += it->len;
6065 IT_CHARPOS (*it) += 1;
6066 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6068 break;
6070 case GET_FROM_C_STRING:
6071 /* Current display element of IT is from a C string. */
6072 IT_BYTEPOS (*it) += it->len;
6073 IT_CHARPOS (*it) += 1;
6074 break;
6076 case GET_FROM_DISPLAY_VECTOR:
6077 /* Current display element of IT is from a display table entry.
6078 Advance in the display table definition. Reset it to null if
6079 end reached, and continue with characters from buffers/
6080 strings. */
6081 ++it->current.dpvec_index;
6083 /* Restore face of the iterator to what they were before the
6084 display vector entry (these entries may contain faces). */
6085 it->face_id = it->saved_face_id;
6087 if (it->dpvec + it->current.dpvec_index == it->dpend)
6089 int recheck_faces = it->ellipsis_p;
6091 if (it->s)
6092 it->method = GET_FROM_C_STRING;
6093 else if (STRINGP (it->string))
6094 it->method = GET_FROM_STRING;
6095 else
6097 it->method = GET_FROM_BUFFER;
6098 it->object = it->w->buffer;
6101 it->dpvec = NULL;
6102 it->current.dpvec_index = -1;
6104 /* Skip over characters which were displayed via IT->dpvec. */
6105 if (it->dpvec_char_len < 0)
6106 reseat_at_next_visible_line_start (it, 1);
6107 else if (it->dpvec_char_len > 0)
6109 if (it->method == GET_FROM_STRING
6110 && it->n_overlay_strings > 0)
6111 it->ignore_overlay_strings_at_pos_p = 1;
6112 it->len = it->dpvec_char_len;
6113 set_iterator_to_next (it, reseat_p);
6116 /* Maybe recheck faces after display vector */
6117 if (recheck_faces)
6118 it->stop_charpos = IT_CHARPOS (*it);
6120 break;
6122 case GET_FROM_STRING:
6123 /* Current display element is a character from a Lisp string. */
6124 xassert (it->s == NULL && STRINGP (it->string));
6125 if (it->cmp_it.id >= 0)
6127 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6128 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6129 if (it->cmp_it.to < it->cmp_it.nglyphs)
6130 it->cmp_it.from = it->cmp_it.to;
6131 else
6133 it->cmp_it.id = -1;
6134 composition_compute_stop_pos (&it->cmp_it,
6135 IT_STRING_CHARPOS (*it),
6136 IT_STRING_BYTEPOS (*it),
6137 it->stop_charpos, it->string);
6140 else
6142 IT_STRING_BYTEPOS (*it) += it->len;
6143 IT_STRING_CHARPOS (*it) += 1;
6146 consider_string_end:
6148 if (it->current.overlay_string_index >= 0)
6150 /* IT->string is an overlay string. Advance to the
6151 next, if there is one. */
6152 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6154 it->ellipsis_p = 0;
6155 next_overlay_string (it);
6156 if (it->ellipsis_p)
6157 setup_for_ellipsis (it, 0);
6160 else
6162 /* IT->string is not an overlay string. If we reached
6163 its end, and there is something on IT->stack, proceed
6164 with what is on the stack. This can be either another
6165 string, this time an overlay string, or a buffer. */
6166 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6167 && it->sp > 0)
6169 pop_it (it);
6170 if (it->method == GET_FROM_STRING)
6171 goto consider_string_end;
6174 break;
6176 case GET_FROM_IMAGE:
6177 case GET_FROM_STRETCH:
6178 /* The position etc with which we have to proceed are on
6179 the stack. The position may be at the end of a string,
6180 if the `display' property takes up the whole string. */
6181 xassert (it->sp > 0);
6182 pop_it (it);
6183 if (it->method == GET_FROM_STRING)
6184 goto consider_string_end;
6185 break;
6187 default:
6188 /* There are no other methods defined, so this should be a bug. */
6189 abort ();
6192 xassert (it->method != GET_FROM_STRING
6193 || (STRINGP (it->string)
6194 && IT_STRING_CHARPOS (*it) >= 0));
6197 /* Load IT's display element fields with information about the next
6198 display element which comes from a display table entry or from the
6199 result of translating a control character to one of the forms `^C'
6200 or `\003'.
6202 IT->dpvec holds the glyphs to return as characters.
6203 IT->saved_face_id holds the face id before the display vector--
6204 it is restored into IT->face_idin set_iterator_to_next. */
6206 static int
6207 next_element_from_display_vector (it)
6208 struct it *it;
6210 Lisp_Object gc;
6212 /* Precondition. */
6213 xassert (it->dpvec && it->current.dpvec_index >= 0);
6215 it->face_id = it->saved_face_id;
6217 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6218 That seemed totally bogus - so I changed it... */
6219 gc = it->dpvec[it->current.dpvec_index];
6221 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6223 it->c = GLYPH_CODE_CHAR (gc);
6224 it->len = CHAR_BYTES (it->c);
6226 /* The entry may contain a face id to use. Such a face id is
6227 the id of a Lisp face, not a realized face. A face id of
6228 zero means no face is specified. */
6229 if (it->dpvec_face_id >= 0)
6230 it->face_id = it->dpvec_face_id;
6231 else
6233 int lface_id = GLYPH_CODE_FACE (gc);
6234 if (lface_id > 0)
6235 it->face_id = merge_faces (it->f, Qt, lface_id,
6236 it->saved_face_id);
6239 else
6240 /* Display table entry is invalid. Return a space. */
6241 it->c = ' ', it->len = 1;
6243 /* Don't change position and object of the iterator here. They are
6244 still the values of the character that had this display table
6245 entry or was translated, and that's what we want. */
6246 it->what = IT_CHARACTER;
6247 return 1;
6251 /* Load IT with the next display element from Lisp string IT->string.
6252 IT->current.string_pos is the current position within the string.
6253 If IT->current.overlay_string_index >= 0, the Lisp string is an
6254 overlay string. */
6256 static int
6257 next_element_from_string (it)
6258 struct it *it;
6260 struct text_pos position;
6262 xassert (STRINGP (it->string));
6263 xassert (IT_STRING_CHARPOS (*it) >= 0);
6264 position = it->current.string_pos;
6266 /* Time to check for invisible text? */
6267 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6268 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6270 handle_stop (it);
6272 /* Since a handler may have changed IT->method, we must
6273 recurse here. */
6274 return GET_NEXT_DISPLAY_ELEMENT (it);
6277 if (it->current.overlay_string_index >= 0)
6279 /* Get the next character from an overlay string. In overlay
6280 strings, There is no field width or padding with spaces to
6281 do. */
6282 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6284 it->what = IT_EOB;
6285 return 0;
6287 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6288 IT_STRING_BYTEPOS (*it))
6289 && next_element_from_composition (it))
6291 return 1;
6293 else if (STRING_MULTIBYTE (it->string))
6295 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6296 const unsigned char *s = (SDATA (it->string)
6297 + IT_STRING_BYTEPOS (*it));
6298 it->c = string_char_and_length (s, remaining, &it->len);
6300 else
6302 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6303 it->len = 1;
6306 else
6308 /* Get the next character from a Lisp string that is not an
6309 overlay string. Such strings come from the mode line, for
6310 example. We may have to pad with spaces, or truncate the
6311 string. See also next_element_from_c_string. */
6312 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6314 it->what = IT_EOB;
6315 return 0;
6317 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6319 /* Pad with spaces. */
6320 it->c = ' ', it->len = 1;
6321 CHARPOS (position) = BYTEPOS (position) = -1;
6323 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6324 IT_STRING_BYTEPOS (*it))
6325 && next_element_from_composition (it))
6327 return 1;
6329 else if (STRING_MULTIBYTE (it->string))
6331 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6332 const unsigned char *s = (SDATA (it->string)
6333 + IT_STRING_BYTEPOS (*it));
6334 it->c = string_char_and_length (s, maxlen, &it->len);
6336 else
6338 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6339 it->len = 1;
6343 /* Record what we have and where it came from. */
6344 it->what = IT_CHARACTER;
6345 it->object = it->string;
6346 it->position = position;
6347 return 1;
6351 /* Load IT with next display element from C string IT->s.
6352 IT->string_nchars is the maximum number of characters to return
6353 from the string. IT->end_charpos may be greater than
6354 IT->string_nchars when this function is called, in which case we
6355 may have to return padding spaces. Value is zero if end of string
6356 reached, including padding spaces. */
6358 static int
6359 next_element_from_c_string (it)
6360 struct it *it;
6362 int success_p = 1;
6364 xassert (it->s);
6365 it->what = IT_CHARACTER;
6366 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6367 it->object = Qnil;
6369 /* IT's position can be greater IT->string_nchars in case a field
6370 width or precision has been specified when the iterator was
6371 initialized. */
6372 if (IT_CHARPOS (*it) >= it->end_charpos)
6374 /* End of the game. */
6375 it->what = IT_EOB;
6376 success_p = 0;
6378 else if (IT_CHARPOS (*it) >= it->string_nchars)
6380 /* Pad with spaces. */
6381 it->c = ' ', it->len = 1;
6382 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6384 else if (it->multibyte_p)
6386 /* Implementation note: The calls to strlen apparently aren't a
6387 performance problem because there is no noticeable performance
6388 difference between Emacs running in unibyte or multibyte mode. */
6389 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6390 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6391 maxlen, &it->len);
6393 else
6394 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6396 return success_p;
6400 /* Set up IT to return characters from an ellipsis, if appropriate.
6401 The definition of the ellipsis glyphs may come from a display table
6402 entry. This function Fills IT with the first glyph from the
6403 ellipsis if an ellipsis is to be displayed. */
6405 static int
6406 next_element_from_ellipsis (it)
6407 struct it *it;
6409 if (it->selective_display_ellipsis_p)
6410 setup_for_ellipsis (it, it->len);
6411 else
6413 /* The face at the current position may be different from the
6414 face we find after the invisible text. Remember what it
6415 was in IT->saved_face_id, and signal that it's there by
6416 setting face_before_selective_p. */
6417 it->saved_face_id = it->face_id;
6418 it->method = GET_FROM_BUFFER;
6419 it->object = it->w->buffer;
6420 reseat_at_next_visible_line_start (it, 1);
6421 it->face_before_selective_p = 1;
6424 return GET_NEXT_DISPLAY_ELEMENT (it);
6428 /* Deliver an image display element. The iterator IT is already
6429 filled with image information (done in handle_display_prop). Value
6430 is always 1. */
6433 static int
6434 next_element_from_image (it)
6435 struct it *it;
6437 it->what = IT_IMAGE;
6438 return 1;
6442 /* Fill iterator IT with next display element from a stretch glyph
6443 property. IT->object is the value of the text property. Value is
6444 always 1. */
6446 static int
6447 next_element_from_stretch (it)
6448 struct it *it;
6450 it->what = IT_STRETCH;
6451 return 1;
6455 /* Load IT with the next display element from current_buffer. Value
6456 is zero if end of buffer reached. IT->stop_charpos is the next
6457 position at which to stop and check for text properties or buffer
6458 end. */
6460 static int
6461 next_element_from_buffer (it)
6462 struct it *it;
6464 int success_p = 1;
6466 xassert (IT_CHARPOS (*it) >= BEGV);
6468 if (IT_CHARPOS (*it) >= it->stop_charpos)
6470 if (IT_CHARPOS (*it) >= it->end_charpos)
6472 int overlay_strings_follow_p;
6474 /* End of the game, except when overlay strings follow that
6475 haven't been returned yet. */
6476 if (it->overlay_strings_at_end_processed_p)
6477 overlay_strings_follow_p = 0;
6478 else
6480 it->overlay_strings_at_end_processed_p = 1;
6481 overlay_strings_follow_p = get_overlay_strings (it, 0);
6484 if (overlay_strings_follow_p)
6485 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6486 else
6488 it->what = IT_EOB;
6489 it->position = it->current.pos;
6490 success_p = 0;
6493 else
6495 handle_stop (it);
6496 return GET_NEXT_DISPLAY_ELEMENT (it);
6499 else
6501 /* No face changes, overlays etc. in sight, so just return a
6502 character from current_buffer. */
6503 unsigned char *p;
6505 /* Maybe run the redisplay end trigger hook. Performance note:
6506 This doesn't seem to cost measurable time. */
6507 if (it->redisplay_end_trigger_charpos
6508 && it->glyph_row
6509 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6510 run_redisplay_end_trigger_hook (it);
6512 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6513 && next_element_from_composition (it))
6515 return 1;
6518 /* Get the next character, maybe multibyte. */
6519 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6520 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6521 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6522 else
6523 it->c = *p, it->len = 1;
6525 /* Record what we have and where it came from. */
6526 it->what = IT_CHARACTER;
6527 it->object = it->w->buffer;
6528 it->position = it->current.pos;
6530 /* Normally we return the character found above, except when we
6531 really want to return an ellipsis for selective display. */
6532 if (it->selective)
6534 if (it->c == '\n')
6536 /* A value of selective > 0 means hide lines indented more
6537 than that number of columns. */
6538 if (it->selective > 0
6539 && IT_CHARPOS (*it) + 1 < ZV
6540 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6541 IT_BYTEPOS (*it) + 1,
6542 (double) it->selective)) /* iftc */
6544 success_p = next_element_from_ellipsis (it);
6545 it->dpvec_char_len = -1;
6548 else if (it->c == '\r' && it->selective == -1)
6550 /* A value of selective == -1 means that everything from the
6551 CR to the end of the line is invisible, with maybe an
6552 ellipsis displayed for it. */
6553 success_p = next_element_from_ellipsis (it);
6554 it->dpvec_char_len = -1;
6559 /* Value is zero if end of buffer reached. */
6560 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6561 return success_p;
6565 /* Run the redisplay end trigger hook for IT. */
6567 static void
6568 run_redisplay_end_trigger_hook (it)
6569 struct it *it;
6571 Lisp_Object args[3];
6573 /* IT->glyph_row should be non-null, i.e. we should be actually
6574 displaying something, or otherwise we should not run the hook. */
6575 xassert (it->glyph_row);
6577 /* Set up hook arguments. */
6578 args[0] = Qredisplay_end_trigger_functions;
6579 args[1] = it->window;
6580 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6581 it->redisplay_end_trigger_charpos = 0;
6583 /* Since we are *trying* to run these functions, don't try to run
6584 them again, even if they get an error. */
6585 it->w->redisplay_end_trigger = Qnil;
6586 Frun_hook_with_args (3, args);
6588 /* Notice if it changed the face of the character we are on. */
6589 handle_face_prop (it);
6593 /* Deliver a composition display element. Unlike the other
6594 next_element_from_XXX, this function is not registered in the array
6595 get_next_element[]. It is called from next_element_from_buffer and
6596 next_element_from_string when necessary. */
6598 static int
6599 next_element_from_composition (it)
6600 struct it *it;
6602 it->what = IT_COMPOSITION;
6603 it->len = it->cmp_it.nbytes;
6604 if (STRINGP (it->string))
6606 if (it->c < 0)
6608 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6609 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6610 return 0;
6612 it->position = it->current.string_pos;
6613 it->object = it->string;
6614 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6615 IT_STRING_BYTEPOS (*it), it->string);
6617 else
6619 if (it->c < 0)
6621 IT_CHARPOS (*it) += it->cmp_it.nchars;
6622 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6623 return 0;
6625 it->position = it->current.pos;
6626 it->object = it->w->buffer;
6627 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6628 IT_BYTEPOS (*it), Qnil);
6630 return 1;
6635 /***********************************************************************
6636 Moving an iterator without producing glyphs
6637 ***********************************************************************/
6639 /* Check if iterator is at a position corresponding to a valid buffer
6640 position after some move_it_ call. */
6642 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6643 ((it)->method == GET_FROM_STRING \
6644 ? IT_STRING_CHARPOS (*it) == 0 \
6645 : 1)
6648 /* Move iterator IT to a specified buffer or X position within one
6649 line on the display without producing glyphs.
6651 OP should be a bit mask including some or all of these bits:
6652 MOVE_TO_X: Stop on reaching x-position TO_X.
6653 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6654 Regardless of OP's value, stop in reaching the end of the display line.
6656 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6657 This means, in particular, that TO_X includes window's horizontal
6658 scroll amount.
6660 The return value has several possible values that
6661 say what condition caused the scan to stop:
6663 MOVE_POS_MATCH_OR_ZV
6664 - when TO_POS or ZV was reached.
6666 MOVE_X_REACHED
6667 -when TO_X was reached before TO_POS or ZV were reached.
6669 MOVE_LINE_CONTINUED
6670 - when we reached the end of the display area and the line must
6671 be continued.
6673 MOVE_LINE_TRUNCATED
6674 - when we reached the end of the display area and the line is
6675 truncated.
6677 MOVE_NEWLINE_OR_CR
6678 - when we stopped at a line end, i.e. a newline or a CR and selective
6679 display is on. */
6681 static enum move_it_result
6682 move_it_in_display_line_to (struct it *it,
6683 EMACS_INT to_charpos, int to_x,
6684 enum move_operation_enum op)
6686 enum move_it_result result = MOVE_UNDEFINED;
6687 struct glyph_row *saved_glyph_row;
6688 struct it wrap_it, atpos_it, atx_it;
6689 int may_wrap = 0;
6691 /* Don't produce glyphs in produce_glyphs. */
6692 saved_glyph_row = it->glyph_row;
6693 it->glyph_row = NULL;
6695 /* Use wrap_it to save a copy of IT wherever a word wrap could
6696 occur. Use atpos_it to save a copy of IT at the desired buffer
6697 position, if found, so that we can scan ahead and check if the
6698 word later overshoots the window edge. Use atx_it similarly, for
6699 pixel positions. */
6700 wrap_it.sp = -1;
6701 atpos_it.sp = -1;
6702 atx_it.sp = -1;
6704 #define BUFFER_POS_REACHED_P() \
6705 ((op & MOVE_TO_POS) != 0 \
6706 && BUFFERP (it->object) \
6707 && IT_CHARPOS (*it) >= to_charpos \
6708 && (it->method == GET_FROM_BUFFER \
6709 || (it->method == GET_FROM_DISPLAY_VECTOR \
6710 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6712 /* If there's a line-/wrap-prefix, handle it. */
6713 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6714 && it->current_y < it->last_visible_y)
6715 handle_line_prefix (it);
6717 while (1)
6719 int x, i, ascent = 0, descent = 0;
6721 /* Utility macro to reset an iterator with x, ascent, and descent. */
6722 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6723 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6724 (IT)->max_descent = descent)
6726 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6727 glyph). */
6728 if ((op & MOVE_TO_POS) != 0
6729 && BUFFERP (it->object)
6730 && it->method == GET_FROM_BUFFER
6731 && IT_CHARPOS (*it) > to_charpos)
6733 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6735 result = MOVE_POS_MATCH_OR_ZV;
6736 break;
6738 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6739 /* If wrap_it is valid, the current position might be in a
6740 word that is wrapped. So, save the iterator in
6741 atpos_it and continue to see if wrapping happens. */
6742 atpos_it = *it;
6745 /* Stop when ZV reached.
6746 We used to stop here when TO_CHARPOS reached as well, but that is
6747 too soon if this glyph does not fit on this line. So we handle it
6748 explicitly below. */
6749 if (!get_next_display_element (it))
6751 result = MOVE_POS_MATCH_OR_ZV;
6752 break;
6755 if (it->line_wrap == TRUNCATE)
6757 if (BUFFER_POS_REACHED_P ())
6759 result = MOVE_POS_MATCH_OR_ZV;
6760 break;
6763 else
6765 if (it->line_wrap == WORD_WRAP)
6767 if (IT_DISPLAYING_WHITESPACE (it))
6768 may_wrap = 1;
6769 else if (may_wrap)
6771 /* We have reached a glyph that follows one or more
6772 whitespace characters. If the position is
6773 already found, we are done. */
6774 if (atpos_it.sp >= 0)
6776 *it = atpos_it;
6777 result = MOVE_POS_MATCH_OR_ZV;
6778 goto done;
6780 if (atx_it.sp >= 0)
6782 *it = atx_it;
6783 result = MOVE_X_REACHED;
6784 goto done;
6786 /* Otherwise, we can wrap here. */
6787 wrap_it = *it;
6788 may_wrap = 0;
6793 /* Remember the line height for the current line, in case
6794 the next element doesn't fit on the line. */
6795 ascent = it->max_ascent;
6796 descent = it->max_descent;
6798 /* The call to produce_glyphs will get the metrics of the
6799 display element IT is loaded with. Record the x-position
6800 before this display element, in case it doesn't fit on the
6801 line. */
6802 x = it->current_x;
6804 PRODUCE_GLYPHS (it);
6806 if (it->area != TEXT_AREA)
6808 set_iterator_to_next (it, 1);
6809 continue;
6812 /* The number of glyphs we get back in IT->nglyphs will normally
6813 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6814 character on a terminal frame, or (iii) a line end. For the
6815 second case, IT->nglyphs - 1 padding glyphs will be present
6816 (on X frames, there is only one glyph produced for a
6817 composite character.
6819 The behavior implemented below means, for continuation lines,
6820 that as many spaces of a TAB as fit on the current line are
6821 displayed there. For terminal frames, as many glyphs of a
6822 multi-glyph character are displayed in the current line, too.
6823 This is what the old redisplay code did, and we keep it that
6824 way. Under X, the whole shape of a complex character must
6825 fit on the line or it will be completely displayed in the
6826 next line.
6828 Note that both for tabs and padding glyphs, all glyphs have
6829 the same width. */
6830 if (it->nglyphs)
6832 /* More than one glyph or glyph doesn't fit on line. All
6833 glyphs have the same width. */
6834 int single_glyph_width = it->pixel_width / it->nglyphs;
6835 int new_x;
6836 int x_before_this_char = x;
6837 int hpos_before_this_char = it->hpos;
6839 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6841 new_x = x + single_glyph_width;
6843 /* We want to leave anything reaching TO_X to the caller. */
6844 if ((op & MOVE_TO_X) && new_x > to_x)
6846 if (BUFFER_POS_REACHED_P ())
6848 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6849 goto buffer_pos_reached;
6850 if (atpos_it.sp < 0)
6852 atpos_it = *it;
6853 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6856 else
6858 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6860 it->current_x = x;
6861 result = MOVE_X_REACHED;
6862 break;
6864 if (atx_it.sp < 0)
6866 atx_it = *it;
6867 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6872 if (/* Lines are continued. */
6873 it->line_wrap != TRUNCATE
6874 && (/* And glyph doesn't fit on the line. */
6875 new_x > it->last_visible_x
6876 /* Or it fits exactly and we're on a window
6877 system frame. */
6878 || (new_x == it->last_visible_x
6879 && FRAME_WINDOW_P (it->f))))
6881 if (/* IT->hpos == 0 means the very first glyph
6882 doesn't fit on the line, e.g. a wide image. */
6883 it->hpos == 0
6884 || (new_x == it->last_visible_x
6885 && FRAME_WINDOW_P (it->f)))
6887 ++it->hpos;
6888 it->current_x = new_x;
6890 /* The character's last glyph just barely fits
6891 in this row. */
6892 if (i == it->nglyphs - 1)
6894 /* If this is the destination position,
6895 return a position *before* it in this row,
6896 now that we know it fits in this row. */
6897 if (BUFFER_POS_REACHED_P ())
6899 if (it->line_wrap != WORD_WRAP
6900 || wrap_it.sp < 0)
6902 it->hpos = hpos_before_this_char;
6903 it->current_x = x_before_this_char;
6904 result = MOVE_POS_MATCH_OR_ZV;
6905 break;
6907 if (it->line_wrap == WORD_WRAP
6908 && atpos_it.sp < 0)
6910 atpos_it = *it;
6911 atpos_it.current_x = x_before_this_char;
6912 atpos_it.hpos = hpos_before_this_char;
6916 set_iterator_to_next (it, 1);
6917 /* One graphical terminals, newlines may
6918 "overflow" into the fringe if
6919 overflow-newline-into-fringe is non-nil.
6920 On text-only terminals, newlines may
6921 overflow into the last glyph on the
6922 display line.*/
6923 if (!FRAME_WINDOW_P (it->f)
6924 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6926 if (!get_next_display_element (it))
6928 result = MOVE_POS_MATCH_OR_ZV;
6929 break;
6931 if (BUFFER_POS_REACHED_P ())
6933 if (ITERATOR_AT_END_OF_LINE_P (it))
6934 result = MOVE_POS_MATCH_OR_ZV;
6935 else
6936 result = MOVE_LINE_CONTINUED;
6937 break;
6939 if (ITERATOR_AT_END_OF_LINE_P (it))
6941 result = MOVE_NEWLINE_OR_CR;
6942 break;
6947 else
6948 IT_RESET_X_ASCENT_DESCENT (it);
6950 if (wrap_it.sp >= 0)
6952 *it = wrap_it;
6953 atpos_it.sp = -1;
6954 atx_it.sp = -1;
6957 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6958 IT_CHARPOS (*it)));
6959 result = MOVE_LINE_CONTINUED;
6960 break;
6963 if (BUFFER_POS_REACHED_P ())
6965 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6966 goto buffer_pos_reached;
6967 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6969 atpos_it = *it;
6970 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6974 if (new_x > it->first_visible_x)
6976 /* Glyph is visible. Increment number of glyphs that
6977 would be displayed. */
6978 ++it->hpos;
6982 if (result != MOVE_UNDEFINED)
6983 break;
6985 else if (BUFFER_POS_REACHED_P ())
6987 buffer_pos_reached:
6988 IT_RESET_X_ASCENT_DESCENT (it);
6989 result = MOVE_POS_MATCH_OR_ZV;
6990 break;
6992 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6994 /* Stop when TO_X specified and reached. This check is
6995 necessary here because of lines consisting of a line end,
6996 only. The line end will not produce any glyphs and we
6997 would never get MOVE_X_REACHED. */
6998 xassert (it->nglyphs == 0);
6999 result = MOVE_X_REACHED;
7000 break;
7003 /* Is this a line end? If yes, we're done. */
7004 if (ITERATOR_AT_END_OF_LINE_P (it))
7006 result = MOVE_NEWLINE_OR_CR;
7007 break;
7010 /* The current display element has been consumed. Advance
7011 to the next. */
7012 set_iterator_to_next (it, 1);
7014 /* Stop if lines are truncated and IT's current x-position is
7015 past the right edge of the window now. */
7016 if (it->line_wrap == TRUNCATE
7017 && it->current_x >= it->last_visible_x)
7019 if (!FRAME_WINDOW_P (it->f)
7020 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7022 if (!get_next_display_element (it)
7023 || BUFFER_POS_REACHED_P ())
7025 result = MOVE_POS_MATCH_OR_ZV;
7026 break;
7028 if (ITERATOR_AT_END_OF_LINE_P (it))
7030 result = MOVE_NEWLINE_OR_CR;
7031 break;
7034 result = MOVE_LINE_TRUNCATED;
7035 break;
7037 #undef IT_RESET_X_ASCENT_DESCENT
7040 #undef BUFFER_POS_REACHED_P
7042 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7043 restore the saved iterator. */
7044 if (atpos_it.sp >= 0)
7045 *it = atpos_it;
7046 else if (atx_it.sp >= 0)
7047 *it = atx_it;
7049 done:
7051 /* Restore the iterator settings altered at the beginning of this
7052 function. */
7053 it->glyph_row = saved_glyph_row;
7054 return result;
7057 /* For external use. */
7058 void
7059 move_it_in_display_line (struct it *it,
7060 EMACS_INT to_charpos, int to_x,
7061 enum move_operation_enum op)
7063 if (it->line_wrap == WORD_WRAP
7064 && (op & MOVE_TO_X))
7066 struct it save_it = *it;
7067 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7068 /* When word-wrap is on, TO_X may lie past the end
7069 of a wrapped line. Then it->current is the
7070 character on the next line, so backtrack to the
7071 space before the wrap point. */
7072 if (skip == MOVE_LINE_CONTINUED)
7074 int prev_x = max (it->current_x - 1, 0);
7075 *it = save_it;
7076 move_it_in_display_line_to
7077 (it, -1, prev_x, MOVE_TO_X);
7080 else
7081 move_it_in_display_line_to (it, to_charpos, to_x, op);
7085 /* Move IT forward until it satisfies one or more of the criteria in
7086 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7088 OP is a bit-mask that specifies where to stop, and in particular,
7089 which of those four position arguments makes a difference. See the
7090 description of enum move_operation_enum.
7092 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7093 screen line, this function will set IT to the next position >
7094 TO_CHARPOS. */
7096 void
7097 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7098 struct it *it;
7099 int to_charpos, to_x, to_y, to_vpos;
7100 int op;
7102 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7103 int line_height, line_start_x = 0, reached = 0;
7105 for (;;)
7107 if (op & MOVE_TO_VPOS)
7109 /* If no TO_CHARPOS and no TO_X specified, stop at the
7110 start of the line TO_VPOS. */
7111 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7113 if (it->vpos == to_vpos)
7115 reached = 1;
7116 break;
7118 else
7119 skip = move_it_in_display_line_to (it, -1, -1, 0);
7121 else
7123 /* TO_VPOS >= 0 means stop at TO_X in the line at
7124 TO_VPOS, or at TO_POS, whichever comes first. */
7125 if (it->vpos == to_vpos)
7127 reached = 2;
7128 break;
7131 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7133 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7135 reached = 3;
7136 break;
7138 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7140 /* We have reached TO_X but not in the line we want. */
7141 skip = move_it_in_display_line_to (it, to_charpos,
7142 -1, MOVE_TO_POS);
7143 if (skip == MOVE_POS_MATCH_OR_ZV)
7145 reached = 4;
7146 break;
7151 else if (op & MOVE_TO_Y)
7153 struct it it_backup;
7155 if (it->line_wrap == WORD_WRAP)
7156 it_backup = *it;
7158 /* TO_Y specified means stop at TO_X in the line containing
7159 TO_Y---or at TO_CHARPOS if this is reached first. The
7160 problem is that we can't really tell whether the line
7161 contains TO_Y before we have completely scanned it, and
7162 this may skip past TO_X. What we do is to first scan to
7163 TO_X.
7165 If TO_X is not specified, use a TO_X of zero. The reason
7166 is to make the outcome of this function more predictable.
7167 If we didn't use TO_X == 0, we would stop at the end of
7168 the line which is probably not what a caller would expect
7169 to happen. */
7170 skip = move_it_in_display_line_to
7171 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7172 (MOVE_TO_X | (op & MOVE_TO_POS)));
7174 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7175 if (skip == MOVE_POS_MATCH_OR_ZV)
7176 reached = 5;
7177 else if (skip == MOVE_X_REACHED)
7179 /* If TO_X was reached, we want to know whether TO_Y is
7180 in the line. We know this is the case if the already
7181 scanned glyphs make the line tall enough. Otherwise,
7182 we must check by scanning the rest of the line. */
7183 line_height = it->max_ascent + it->max_descent;
7184 if (to_y >= it->current_y
7185 && to_y < it->current_y + line_height)
7187 reached = 6;
7188 break;
7190 it_backup = *it;
7191 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7192 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7193 op & MOVE_TO_POS);
7194 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7195 line_height = it->max_ascent + it->max_descent;
7196 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7198 if (to_y >= it->current_y
7199 && to_y < it->current_y + line_height)
7201 /* If TO_Y is in this line and TO_X was reached
7202 above, we scanned too far. We have to restore
7203 IT's settings to the ones before skipping. */
7204 *it = it_backup;
7205 reached = 6;
7207 else
7209 skip = skip2;
7210 if (skip == MOVE_POS_MATCH_OR_ZV)
7211 reached = 7;
7214 else
7216 /* Check whether TO_Y is in this line. */
7217 line_height = it->max_ascent + it->max_descent;
7218 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7220 if (to_y >= it->current_y
7221 && to_y < it->current_y + line_height)
7223 /* When word-wrap is on, TO_X may lie past the end
7224 of a wrapped line. Then it->current is the
7225 character on the next line, so backtrack to the
7226 space before the wrap point. */
7227 if (skip == MOVE_LINE_CONTINUED
7228 && it->line_wrap == WORD_WRAP)
7230 int prev_x = max (it->current_x - 1, 0);
7231 *it = it_backup;
7232 skip = move_it_in_display_line_to
7233 (it, -1, prev_x, MOVE_TO_X);
7235 reached = 6;
7239 if (reached)
7240 break;
7242 else if (BUFFERP (it->object)
7243 && (it->method == GET_FROM_BUFFER
7244 || it->method == GET_FROM_STRETCH)
7245 && IT_CHARPOS (*it) >= to_charpos)
7246 skip = MOVE_POS_MATCH_OR_ZV;
7247 else
7248 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7250 switch (skip)
7252 case MOVE_POS_MATCH_OR_ZV:
7253 reached = 8;
7254 goto out;
7256 case MOVE_NEWLINE_OR_CR:
7257 set_iterator_to_next (it, 1);
7258 it->continuation_lines_width = 0;
7259 break;
7261 case MOVE_LINE_TRUNCATED:
7262 it->continuation_lines_width = 0;
7263 reseat_at_next_visible_line_start (it, 0);
7264 if ((op & MOVE_TO_POS) != 0
7265 && IT_CHARPOS (*it) > to_charpos)
7267 reached = 9;
7268 goto out;
7270 break;
7272 case MOVE_LINE_CONTINUED:
7273 /* For continued lines ending in a tab, some of the glyphs
7274 associated with the tab are displayed on the current
7275 line. Since it->current_x does not include these glyphs,
7276 we use it->last_visible_x instead. */
7277 if (it->c == '\t')
7279 it->continuation_lines_width += it->last_visible_x;
7280 /* When moving by vpos, ensure that the iterator really
7281 advances to the next line (bug#847, bug#969). Fixme:
7282 do we need to do this in other circumstances? */
7283 if (it->current_x != it->last_visible_x
7284 && (op & MOVE_TO_VPOS)
7285 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7287 line_start_x = it->current_x + it->pixel_width
7288 - it->last_visible_x;
7289 set_iterator_to_next (it, 0);
7292 else
7293 it->continuation_lines_width += it->current_x;
7294 break;
7296 default:
7297 abort ();
7300 /* Reset/increment for the next run. */
7301 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7302 it->current_x = line_start_x;
7303 line_start_x = 0;
7304 it->hpos = 0;
7305 it->current_y += it->max_ascent + it->max_descent;
7306 ++it->vpos;
7307 last_height = it->max_ascent + it->max_descent;
7308 last_max_ascent = it->max_ascent;
7309 it->max_ascent = it->max_descent = 0;
7312 out:
7314 /* On text terminals, we may stop at the end of a line in the middle
7315 of a multi-character glyph. If the glyph itself is continued,
7316 i.e. it is actually displayed on the next line, don't treat this
7317 stopping point as valid; move to the next line instead (unless
7318 that brings us offscreen). */
7319 if (!FRAME_WINDOW_P (it->f)
7320 && op & MOVE_TO_POS
7321 && IT_CHARPOS (*it) == to_charpos
7322 && it->what == IT_CHARACTER
7323 && it->nglyphs > 1
7324 && it->line_wrap == WINDOW_WRAP
7325 && it->current_x == it->last_visible_x - 1
7326 && it->c != '\n'
7327 && it->c != '\t'
7328 && it->vpos < XFASTINT (it->w->window_end_vpos))
7330 it->continuation_lines_width += it->current_x;
7331 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7332 it->current_y += it->max_ascent + it->max_descent;
7333 ++it->vpos;
7334 last_height = it->max_ascent + it->max_descent;
7335 last_max_ascent = it->max_ascent;
7338 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7342 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7344 If DY > 0, move IT backward at least that many pixels. DY = 0
7345 means move IT backward to the preceding line start or BEGV. This
7346 function may move over more than DY pixels if IT->current_y - DY
7347 ends up in the middle of a line; in this case IT->current_y will be
7348 set to the top of the line moved to. */
7350 void
7351 move_it_vertically_backward (it, dy)
7352 struct it *it;
7353 int dy;
7355 int nlines, h;
7356 struct it it2, it3;
7357 int start_pos;
7359 move_further_back:
7360 xassert (dy >= 0);
7362 start_pos = IT_CHARPOS (*it);
7364 /* Estimate how many newlines we must move back. */
7365 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7367 /* Set the iterator's position that many lines back. */
7368 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7369 back_to_previous_visible_line_start (it);
7371 /* Reseat the iterator here. When moving backward, we don't want
7372 reseat to skip forward over invisible text, set up the iterator
7373 to deliver from overlay strings at the new position etc. So,
7374 use reseat_1 here. */
7375 reseat_1 (it, it->current.pos, 1);
7377 /* We are now surely at a line start. */
7378 it->current_x = it->hpos = 0;
7379 it->continuation_lines_width = 0;
7381 /* Move forward and see what y-distance we moved. First move to the
7382 start of the next line so that we get its height. We need this
7383 height to be able to tell whether we reached the specified
7384 y-distance. */
7385 it2 = *it;
7386 it2.max_ascent = it2.max_descent = 0;
7389 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7390 MOVE_TO_POS | MOVE_TO_VPOS);
7392 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7393 xassert (IT_CHARPOS (*it) >= BEGV);
7394 it3 = it2;
7396 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7397 xassert (IT_CHARPOS (*it) >= BEGV);
7398 /* H is the actual vertical distance from the position in *IT
7399 and the starting position. */
7400 h = it2.current_y - it->current_y;
7401 /* NLINES is the distance in number of lines. */
7402 nlines = it2.vpos - it->vpos;
7404 /* Correct IT's y and vpos position
7405 so that they are relative to the starting point. */
7406 it->vpos -= nlines;
7407 it->current_y -= h;
7409 if (dy == 0)
7411 /* DY == 0 means move to the start of the screen line. The
7412 value of nlines is > 0 if continuation lines were involved. */
7413 if (nlines > 0)
7414 move_it_by_lines (it, nlines, 1);
7416 else
7418 /* The y-position we try to reach, relative to *IT.
7419 Note that H has been subtracted in front of the if-statement. */
7420 int target_y = it->current_y + h - dy;
7421 int y0 = it3.current_y;
7422 int y1 = line_bottom_y (&it3);
7423 int line_height = y1 - y0;
7425 /* If we did not reach target_y, try to move further backward if
7426 we can. If we moved too far backward, try to move forward. */
7427 if (target_y < it->current_y
7428 /* This is heuristic. In a window that's 3 lines high, with
7429 a line height of 13 pixels each, recentering with point
7430 on the bottom line will try to move -39/2 = 19 pixels
7431 backward. Try to avoid moving into the first line. */
7432 && (it->current_y - target_y
7433 > min (window_box_height (it->w), line_height * 2 / 3))
7434 && IT_CHARPOS (*it) > BEGV)
7436 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7437 target_y - it->current_y));
7438 dy = it->current_y - target_y;
7439 goto move_further_back;
7441 else if (target_y >= it->current_y + line_height
7442 && IT_CHARPOS (*it) < ZV)
7444 /* Should move forward by at least one line, maybe more.
7446 Note: Calling move_it_by_lines can be expensive on
7447 terminal frames, where compute_motion is used (via
7448 vmotion) to do the job, when there are very long lines
7449 and truncate-lines is nil. That's the reason for
7450 treating terminal frames specially here. */
7452 if (!FRAME_WINDOW_P (it->f))
7453 move_it_vertically (it, target_y - (it->current_y + line_height));
7454 else
7458 move_it_by_lines (it, 1, 1);
7460 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7467 /* Move IT by a specified amount of pixel lines DY. DY negative means
7468 move backwards. DY = 0 means move to start of screen line. At the
7469 end, IT will be on the start of a screen line. */
7471 void
7472 move_it_vertically (it, dy)
7473 struct it *it;
7474 int dy;
7476 if (dy <= 0)
7477 move_it_vertically_backward (it, -dy);
7478 else
7480 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7481 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7482 MOVE_TO_POS | MOVE_TO_Y);
7483 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7485 /* If buffer ends in ZV without a newline, move to the start of
7486 the line to satisfy the post-condition. */
7487 if (IT_CHARPOS (*it) == ZV
7488 && ZV > BEGV
7489 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7490 move_it_by_lines (it, 0, 0);
7495 /* Move iterator IT past the end of the text line it is in. */
7497 void
7498 move_it_past_eol (it)
7499 struct it *it;
7501 enum move_it_result rc;
7503 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7504 if (rc == MOVE_NEWLINE_OR_CR)
7505 set_iterator_to_next (it, 0);
7509 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7510 negative means move up. DVPOS == 0 means move to the start of the
7511 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7512 NEED_Y_P is zero, IT->current_y will be left unchanged.
7514 Further optimization ideas: If we would know that IT->f doesn't use
7515 a face with proportional font, we could be faster for
7516 truncate-lines nil. */
7518 void
7519 move_it_by_lines (it, dvpos, need_y_p)
7520 struct it *it;
7521 int dvpos, need_y_p;
7523 struct position pos;
7525 /* The commented-out optimization uses vmotion on terminals. This
7526 gives bad results, because elements like it->what, on which
7527 callers such as pos_visible_p rely, aren't updated. */
7528 /* if (!FRAME_WINDOW_P (it->f))
7530 struct text_pos textpos;
7532 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7533 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7534 reseat (it, textpos, 1);
7535 it->vpos += pos.vpos;
7536 it->current_y += pos.vpos;
7538 else */
7540 if (dvpos == 0)
7542 /* DVPOS == 0 means move to the start of the screen line. */
7543 move_it_vertically_backward (it, 0);
7544 xassert (it->current_x == 0 && it->hpos == 0);
7545 /* Let next call to line_bottom_y calculate real line height */
7546 last_height = 0;
7548 else if (dvpos > 0)
7550 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7551 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7552 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7554 else
7556 struct it it2;
7557 int start_charpos, i;
7559 /* Start at the beginning of the screen line containing IT's
7560 position. This may actually move vertically backwards,
7561 in case of overlays, so adjust dvpos accordingly. */
7562 dvpos += it->vpos;
7563 move_it_vertically_backward (it, 0);
7564 dvpos -= it->vpos;
7566 /* Go back -DVPOS visible lines and reseat the iterator there. */
7567 start_charpos = IT_CHARPOS (*it);
7568 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7569 back_to_previous_visible_line_start (it);
7570 reseat (it, it->current.pos, 1);
7572 /* Move further back if we end up in a string or an image. */
7573 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7575 /* First try to move to start of display line. */
7576 dvpos += it->vpos;
7577 move_it_vertically_backward (it, 0);
7578 dvpos -= it->vpos;
7579 if (IT_POS_VALID_AFTER_MOVE_P (it))
7580 break;
7581 /* If start of line is still in string or image,
7582 move further back. */
7583 back_to_previous_visible_line_start (it);
7584 reseat (it, it->current.pos, 1);
7585 dvpos--;
7588 it->current_x = it->hpos = 0;
7590 /* Above call may have moved too far if continuation lines
7591 are involved. Scan forward and see if it did. */
7592 it2 = *it;
7593 it2.vpos = it2.current_y = 0;
7594 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7595 it->vpos -= it2.vpos;
7596 it->current_y -= it2.current_y;
7597 it->current_x = it->hpos = 0;
7599 /* If we moved too far back, move IT some lines forward. */
7600 if (it2.vpos > -dvpos)
7602 int delta = it2.vpos + dvpos;
7603 it2 = *it;
7604 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7605 /* Move back again if we got too far ahead. */
7606 if (IT_CHARPOS (*it) >= start_charpos)
7607 *it = it2;
7612 /* Return 1 if IT points into the middle of a display vector. */
7615 in_display_vector_p (it)
7616 struct it *it;
7618 return (it->method == GET_FROM_DISPLAY_VECTOR
7619 && it->current.dpvec_index > 0
7620 && it->dpvec + it->current.dpvec_index != it->dpend);
7624 /***********************************************************************
7625 Messages
7626 ***********************************************************************/
7629 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7630 to *Messages*. */
7632 void
7633 add_to_log (format, arg1, arg2)
7634 char *format;
7635 Lisp_Object arg1, arg2;
7637 Lisp_Object args[3];
7638 Lisp_Object msg, fmt;
7639 char *buffer;
7640 int len;
7641 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7642 USE_SAFE_ALLOCA;
7644 /* Do nothing if called asynchronously. Inserting text into
7645 a buffer may call after-change-functions and alike and
7646 that would means running Lisp asynchronously. */
7647 if (handling_signal)
7648 return;
7650 fmt = msg = Qnil;
7651 GCPRO4 (fmt, msg, arg1, arg2);
7653 args[0] = fmt = build_string (format);
7654 args[1] = arg1;
7655 args[2] = arg2;
7656 msg = Fformat (3, args);
7658 len = SBYTES (msg) + 1;
7659 SAFE_ALLOCA (buffer, char *, len);
7660 bcopy (SDATA (msg), buffer, len);
7662 message_dolog (buffer, len - 1, 1, 0);
7663 SAFE_FREE ();
7665 UNGCPRO;
7669 /* Output a newline in the *Messages* buffer if "needs" one. */
7671 void
7672 message_log_maybe_newline ()
7674 if (message_log_need_newline)
7675 message_dolog ("", 0, 1, 0);
7679 /* Add a string M of length NBYTES to the message log, optionally
7680 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7681 nonzero, means interpret the contents of M as multibyte. This
7682 function calls low-level routines in order to bypass text property
7683 hooks, etc. which might not be safe to run.
7685 This may GC (insert may run before/after change hooks),
7686 so the buffer M must NOT point to a Lisp string. */
7688 void
7689 message_dolog (m, nbytes, nlflag, multibyte)
7690 const char *m;
7691 int nbytes, nlflag, multibyte;
7693 if (!NILP (Vmemory_full))
7694 return;
7696 if (!NILP (Vmessage_log_max))
7698 struct buffer *oldbuf;
7699 Lisp_Object oldpoint, oldbegv, oldzv;
7700 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7701 int point_at_end = 0;
7702 int zv_at_end = 0;
7703 Lisp_Object old_deactivate_mark, tem;
7704 struct gcpro gcpro1;
7706 old_deactivate_mark = Vdeactivate_mark;
7707 oldbuf = current_buffer;
7708 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7709 current_buffer->undo_list = Qt;
7711 oldpoint = message_dolog_marker1;
7712 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7713 oldbegv = message_dolog_marker2;
7714 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7715 oldzv = message_dolog_marker3;
7716 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7717 GCPRO1 (old_deactivate_mark);
7719 if (PT == Z)
7720 point_at_end = 1;
7721 if (ZV == Z)
7722 zv_at_end = 1;
7724 BEGV = BEG;
7725 BEGV_BYTE = BEG_BYTE;
7726 ZV = Z;
7727 ZV_BYTE = Z_BYTE;
7728 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7730 /* Insert the string--maybe converting multibyte to single byte
7731 or vice versa, so that all the text fits the buffer. */
7732 if (multibyte
7733 && NILP (current_buffer->enable_multibyte_characters))
7735 int i, c, char_bytes;
7736 unsigned char work[1];
7738 /* Convert a multibyte string to single-byte
7739 for the *Message* buffer. */
7740 for (i = 0; i < nbytes; i += char_bytes)
7742 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7743 work[0] = (ASCII_CHAR_P (c)
7745 : multibyte_char_to_unibyte (c, Qnil));
7746 insert_1_both (work, 1, 1, 1, 0, 0);
7749 else if (! multibyte
7750 && ! NILP (current_buffer->enable_multibyte_characters))
7752 int i, c, char_bytes;
7753 unsigned char *msg = (unsigned char *) m;
7754 unsigned char str[MAX_MULTIBYTE_LENGTH];
7755 /* Convert a single-byte string to multibyte
7756 for the *Message* buffer. */
7757 for (i = 0; i < nbytes; i++)
7759 c = msg[i];
7760 c = unibyte_char_to_multibyte (c);
7761 char_bytes = CHAR_STRING (c, str);
7762 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7765 else if (nbytes)
7766 insert_1 (m, nbytes, 1, 0, 0);
7768 if (nlflag)
7770 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7771 insert_1 ("\n", 1, 1, 0, 0);
7773 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7774 this_bol = PT;
7775 this_bol_byte = PT_BYTE;
7777 /* See if this line duplicates the previous one.
7778 If so, combine duplicates. */
7779 if (this_bol > BEG)
7781 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7782 prev_bol = PT;
7783 prev_bol_byte = PT_BYTE;
7785 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7786 this_bol, this_bol_byte);
7787 if (dup)
7789 del_range_both (prev_bol, prev_bol_byte,
7790 this_bol, this_bol_byte, 0);
7791 if (dup > 1)
7793 char dupstr[40];
7794 int duplen;
7796 /* If you change this format, don't forget to also
7797 change message_log_check_duplicate. */
7798 sprintf (dupstr, " [%d times]", dup);
7799 duplen = strlen (dupstr);
7800 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7801 insert_1 (dupstr, duplen, 1, 0, 1);
7806 /* If we have more than the desired maximum number of lines
7807 in the *Messages* buffer now, delete the oldest ones.
7808 This is safe because we don't have undo in this buffer. */
7810 if (NATNUMP (Vmessage_log_max))
7812 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7813 -XFASTINT (Vmessage_log_max) - 1, 0);
7814 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7817 BEGV = XMARKER (oldbegv)->charpos;
7818 BEGV_BYTE = marker_byte_position (oldbegv);
7820 if (zv_at_end)
7822 ZV = Z;
7823 ZV_BYTE = Z_BYTE;
7825 else
7827 ZV = XMARKER (oldzv)->charpos;
7828 ZV_BYTE = marker_byte_position (oldzv);
7831 if (point_at_end)
7832 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7833 else
7834 /* We can't do Fgoto_char (oldpoint) because it will run some
7835 Lisp code. */
7836 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7837 XMARKER (oldpoint)->bytepos);
7839 UNGCPRO;
7840 unchain_marker (XMARKER (oldpoint));
7841 unchain_marker (XMARKER (oldbegv));
7842 unchain_marker (XMARKER (oldzv));
7844 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7845 set_buffer_internal (oldbuf);
7846 if (NILP (tem))
7847 windows_or_buffers_changed = old_windows_or_buffers_changed;
7848 message_log_need_newline = !nlflag;
7849 Vdeactivate_mark = old_deactivate_mark;
7854 /* We are at the end of the buffer after just having inserted a newline.
7855 (Note: We depend on the fact we won't be crossing the gap.)
7856 Check to see if the most recent message looks a lot like the previous one.
7857 Return 0 if different, 1 if the new one should just replace it, or a
7858 value N > 1 if we should also append " [N times]". */
7860 static int
7861 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7862 int prev_bol, this_bol;
7863 int prev_bol_byte, this_bol_byte;
7865 int i;
7866 int len = Z_BYTE - 1 - this_bol_byte;
7867 int seen_dots = 0;
7868 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7869 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7871 for (i = 0; i < len; i++)
7873 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7874 seen_dots = 1;
7875 if (p1[i] != p2[i])
7876 return seen_dots;
7878 p1 += len;
7879 if (*p1 == '\n')
7880 return 2;
7881 if (*p1++ == ' ' && *p1++ == '[')
7883 int n = 0;
7884 while (*p1 >= '0' && *p1 <= '9')
7885 n = n * 10 + *p1++ - '0';
7886 if (strncmp (p1, " times]\n", 8) == 0)
7887 return n+1;
7889 return 0;
7893 /* Display an echo area message M with a specified length of NBYTES
7894 bytes. The string may include null characters. If M is 0, clear
7895 out any existing message, and let the mini-buffer text show
7896 through.
7898 This may GC, so the buffer M must NOT point to a Lisp string. */
7900 void
7901 message2 (m, nbytes, multibyte)
7902 const char *m;
7903 int nbytes;
7904 int multibyte;
7906 /* First flush out any partial line written with print. */
7907 message_log_maybe_newline ();
7908 if (m)
7909 message_dolog (m, nbytes, 1, multibyte);
7910 message2_nolog (m, nbytes, multibyte);
7914 /* The non-logging counterpart of message2. */
7916 void
7917 message2_nolog (m, nbytes, multibyte)
7918 const char *m;
7919 int nbytes, multibyte;
7921 struct frame *sf = SELECTED_FRAME ();
7922 message_enable_multibyte = multibyte;
7924 if (FRAME_INITIAL_P (sf))
7926 if (noninteractive_need_newline)
7927 putc ('\n', stderr);
7928 noninteractive_need_newline = 0;
7929 if (m)
7930 fwrite (m, nbytes, 1, stderr);
7931 if (cursor_in_echo_area == 0)
7932 fprintf (stderr, "\n");
7933 fflush (stderr);
7935 /* A null message buffer means that the frame hasn't really been
7936 initialized yet. Error messages get reported properly by
7937 cmd_error, so this must be just an informative message; toss it. */
7938 else if (INTERACTIVE
7939 && sf->glyphs_initialized_p
7940 && FRAME_MESSAGE_BUF (sf))
7942 Lisp_Object mini_window;
7943 struct frame *f;
7945 /* Get the frame containing the mini-buffer
7946 that the selected frame is using. */
7947 mini_window = FRAME_MINIBUF_WINDOW (sf);
7948 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7950 FRAME_SAMPLE_VISIBILITY (f);
7951 if (FRAME_VISIBLE_P (sf)
7952 && ! FRAME_VISIBLE_P (f))
7953 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7955 if (m)
7957 set_message (m, Qnil, nbytes, multibyte);
7958 if (minibuffer_auto_raise)
7959 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7961 else
7962 clear_message (1, 1);
7964 do_pending_window_change (0);
7965 echo_area_display (1);
7966 do_pending_window_change (0);
7967 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7968 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7973 /* Display an echo area message M with a specified length of NBYTES
7974 bytes. The string may include null characters. If M is not a
7975 string, clear out any existing message, and let the mini-buffer
7976 text show through.
7978 This function cancels echoing. */
7980 void
7981 message3 (m, nbytes, multibyte)
7982 Lisp_Object m;
7983 int nbytes;
7984 int multibyte;
7986 struct gcpro gcpro1;
7988 GCPRO1 (m);
7989 clear_message (1,1);
7990 cancel_echoing ();
7992 /* First flush out any partial line written with print. */
7993 message_log_maybe_newline ();
7994 if (STRINGP (m))
7996 char *buffer;
7997 USE_SAFE_ALLOCA;
7999 SAFE_ALLOCA (buffer, char *, nbytes);
8000 bcopy (SDATA (m), buffer, nbytes);
8001 message_dolog (buffer, nbytes, 1, multibyte);
8002 SAFE_FREE ();
8004 message3_nolog (m, nbytes, multibyte);
8006 UNGCPRO;
8010 /* The non-logging version of message3.
8011 This does not cancel echoing, because it is used for echoing.
8012 Perhaps we need to make a separate function for echoing
8013 and make this cancel echoing. */
8015 void
8016 message3_nolog (m, nbytes, multibyte)
8017 Lisp_Object m;
8018 int nbytes, multibyte;
8020 struct frame *sf = SELECTED_FRAME ();
8021 message_enable_multibyte = multibyte;
8023 if (FRAME_INITIAL_P (sf))
8025 if (noninteractive_need_newline)
8026 putc ('\n', stderr);
8027 noninteractive_need_newline = 0;
8028 if (STRINGP (m))
8029 fwrite (SDATA (m), nbytes, 1, stderr);
8030 if (cursor_in_echo_area == 0)
8031 fprintf (stderr, "\n");
8032 fflush (stderr);
8034 /* A null message buffer means that the frame hasn't really been
8035 initialized yet. Error messages get reported properly by
8036 cmd_error, so this must be just an informative message; toss it. */
8037 else if (INTERACTIVE
8038 && sf->glyphs_initialized_p
8039 && FRAME_MESSAGE_BUF (sf))
8041 Lisp_Object mini_window;
8042 Lisp_Object frame;
8043 struct frame *f;
8045 /* Get the frame containing the mini-buffer
8046 that the selected frame is using. */
8047 mini_window = FRAME_MINIBUF_WINDOW (sf);
8048 frame = XWINDOW (mini_window)->frame;
8049 f = XFRAME (frame);
8051 FRAME_SAMPLE_VISIBILITY (f);
8052 if (FRAME_VISIBLE_P (sf)
8053 && !FRAME_VISIBLE_P (f))
8054 Fmake_frame_visible (frame);
8056 if (STRINGP (m) && SCHARS (m) > 0)
8058 set_message (NULL, m, nbytes, multibyte);
8059 if (minibuffer_auto_raise)
8060 Fraise_frame (frame);
8061 /* Assume we are not echoing.
8062 (If we are, echo_now will override this.) */
8063 echo_message_buffer = Qnil;
8065 else
8066 clear_message (1, 1);
8068 do_pending_window_change (0);
8069 echo_area_display (1);
8070 do_pending_window_change (0);
8071 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8072 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8077 /* Display a null-terminated echo area message M. If M is 0, clear
8078 out any existing message, and let the mini-buffer text show through.
8080 The buffer M must continue to exist until after the echo area gets
8081 cleared or some other message gets displayed there. Do not pass
8082 text that is stored in a Lisp string. Do not pass text in a buffer
8083 that was alloca'd. */
8085 void
8086 message1 (m)
8087 char *m;
8089 message2 (m, (m ? strlen (m) : 0), 0);
8093 /* The non-logging counterpart of message1. */
8095 void
8096 message1_nolog (m)
8097 char *m;
8099 message2_nolog (m, (m ? strlen (m) : 0), 0);
8102 /* Display a message M which contains a single %s
8103 which gets replaced with STRING. */
8105 void
8106 message_with_string (m, string, log)
8107 char *m;
8108 Lisp_Object string;
8109 int log;
8111 CHECK_STRING (string);
8113 if (noninteractive)
8115 if (m)
8117 if (noninteractive_need_newline)
8118 putc ('\n', stderr);
8119 noninteractive_need_newline = 0;
8120 fprintf (stderr, m, SDATA (string));
8121 if (!cursor_in_echo_area)
8122 fprintf (stderr, "\n");
8123 fflush (stderr);
8126 else if (INTERACTIVE)
8128 /* The frame whose minibuffer we're going to display the message on.
8129 It may be larger than the selected frame, so we need
8130 to use its buffer, not the selected frame's buffer. */
8131 Lisp_Object mini_window;
8132 struct frame *f, *sf = SELECTED_FRAME ();
8134 /* Get the frame containing the minibuffer
8135 that the selected frame is using. */
8136 mini_window = FRAME_MINIBUF_WINDOW (sf);
8137 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8139 /* A null message buffer means that the frame hasn't really been
8140 initialized yet. Error messages get reported properly by
8141 cmd_error, so this must be just an informative message; toss it. */
8142 if (FRAME_MESSAGE_BUF (f))
8144 Lisp_Object args[2], message;
8145 struct gcpro gcpro1, gcpro2;
8147 args[0] = build_string (m);
8148 args[1] = message = string;
8149 GCPRO2 (args[0], message);
8150 gcpro1.nvars = 2;
8152 message = Fformat (2, args);
8154 if (log)
8155 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8156 else
8157 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8159 UNGCPRO;
8161 /* Print should start at the beginning of the message
8162 buffer next time. */
8163 message_buf_print = 0;
8169 /* Dump an informative message to the minibuf. If M is 0, clear out
8170 any existing message, and let the mini-buffer text show through. */
8172 /* VARARGS 1 */
8173 void
8174 message (m, a1, a2, a3)
8175 char *m;
8176 EMACS_INT a1, a2, a3;
8178 if (noninteractive)
8180 if (m)
8182 if (noninteractive_need_newline)
8183 putc ('\n', stderr);
8184 noninteractive_need_newline = 0;
8185 fprintf (stderr, m, a1, a2, a3);
8186 if (cursor_in_echo_area == 0)
8187 fprintf (stderr, "\n");
8188 fflush (stderr);
8191 else if (INTERACTIVE)
8193 /* The frame whose mini-buffer we're going to display the message
8194 on. It may be larger than the selected frame, so we need to
8195 use its buffer, not the selected frame's buffer. */
8196 Lisp_Object mini_window;
8197 struct frame *f, *sf = SELECTED_FRAME ();
8199 /* Get the frame containing the mini-buffer
8200 that the selected frame is using. */
8201 mini_window = FRAME_MINIBUF_WINDOW (sf);
8202 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8204 /* A null message buffer means that the frame hasn't really been
8205 initialized yet. Error messages get reported properly by
8206 cmd_error, so this must be just an informative message; toss
8207 it. */
8208 if (FRAME_MESSAGE_BUF (f))
8210 if (m)
8212 int len;
8213 #ifdef NO_ARG_ARRAY
8214 char *a[3];
8215 a[0] = (char *) a1;
8216 a[1] = (char *) a2;
8217 a[2] = (char *) a3;
8219 len = doprnt (FRAME_MESSAGE_BUF (f),
8220 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8221 #else
8222 len = doprnt (FRAME_MESSAGE_BUF (f),
8223 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8224 (char **) &a1);
8225 #endif /* NO_ARG_ARRAY */
8227 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8229 else
8230 message1 (0);
8232 /* Print should start at the beginning of the message
8233 buffer next time. */
8234 message_buf_print = 0;
8240 /* The non-logging version of message. */
8242 void
8243 message_nolog (m, a1, a2, a3)
8244 char *m;
8245 EMACS_INT a1, a2, a3;
8247 Lisp_Object old_log_max;
8248 old_log_max = Vmessage_log_max;
8249 Vmessage_log_max = Qnil;
8250 message (m, a1, a2, a3);
8251 Vmessage_log_max = old_log_max;
8255 /* Display the current message in the current mini-buffer. This is
8256 only called from error handlers in process.c, and is not time
8257 critical. */
8259 void
8260 update_echo_area ()
8262 if (!NILP (echo_area_buffer[0]))
8264 Lisp_Object string;
8265 string = Fcurrent_message ();
8266 message3 (string, SBYTES (string),
8267 !NILP (current_buffer->enable_multibyte_characters));
8272 /* Make sure echo area buffers in `echo_buffers' are live.
8273 If they aren't, make new ones. */
8275 static void
8276 ensure_echo_area_buffers ()
8278 int i;
8280 for (i = 0; i < 2; ++i)
8281 if (!BUFFERP (echo_buffer[i])
8282 || NILP (XBUFFER (echo_buffer[i])->name))
8284 char name[30];
8285 Lisp_Object old_buffer;
8286 int j;
8288 old_buffer = echo_buffer[i];
8289 sprintf (name, " *Echo Area %d*", i);
8290 echo_buffer[i] = Fget_buffer_create (build_string (name));
8291 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8292 /* to force word wrap in echo area -
8293 it was decided to postpone this*/
8294 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8296 for (j = 0; j < 2; ++j)
8297 if (EQ (old_buffer, echo_area_buffer[j]))
8298 echo_area_buffer[j] = echo_buffer[i];
8303 /* Call FN with args A1..A4 with either the current or last displayed
8304 echo_area_buffer as current buffer.
8306 WHICH zero means use the current message buffer
8307 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8308 from echo_buffer[] and clear it.
8310 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8311 suitable buffer from echo_buffer[] and clear it.
8313 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8314 that the current message becomes the last displayed one, make
8315 choose a suitable buffer for echo_area_buffer[0], and clear it.
8317 Value is what FN returns. */
8319 static int
8320 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8321 struct window *w;
8322 int which;
8323 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8324 EMACS_INT a1;
8325 Lisp_Object a2;
8326 EMACS_INT a3, a4;
8328 Lisp_Object buffer;
8329 int this_one, the_other, clear_buffer_p, rc;
8330 int count = SPECPDL_INDEX ();
8332 /* If buffers aren't live, make new ones. */
8333 ensure_echo_area_buffers ();
8335 clear_buffer_p = 0;
8337 if (which == 0)
8338 this_one = 0, the_other = 1;
8339 else if (which > 0)
8340 this_one = 1, the_other = 0;
8341 else
8343 this_one = 0, the_other = 1;
8344 clear_buffer_p = 1;
8346 /* We need a fresh one in case the current echo buffer equals
8347 the one containing the last displayed echo area message. */
8348 if (!NILP (echo_area_buffer[this_one])
8349 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8350 echo_area_buffer[this_one] = Qnil;
8353 /* Choose a suitable buffer from echo_buffer[] is we don't
8354 have one. */
8355 if (NILP (echo_area_buffer[this_one]))
8357 echo_area_buffer[this_one]
8358 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8359 ? echo_buffer[the_other]
8360 : echo_buffer[this_one]);
8361 clear_buffer_p = 1;
8364 buffer = echo_area_buffer[this_one];
8366 /* Don't get confused by reusing the buffer used for echoing
8367 for a different purpose. */
8368 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8369 cancel_echoing ();
8371 record_unwind_protect (unwind_with_echo_area_buffer,
8372 with_echo_area_buffer_unwind_data (w));
8374 /* Make the echo area buffer current. Note that for display
8375 purposes, it is not necessary that the displayed window's buffer
8376 == current_buffer, except for text property lookup. So, let's
8377 only set that buffer temporarily here without doing a full
8378 Fset_window_buffer. We must also change w->pointm, though,
8379 because otherwise an assertions in unshow_buffer fails, and Emacs
8380 aborts. */
8381 set_buffer_internal_1 (XBUFFER (buffer));
8382 if (w)
8384 w->buffer = buffer;
8385 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8388 current_buffer->undo_list = Qt;
8389 current_buffer->read_only = Qnil;
8390 specbind (Qinhibit_read_only, Qt);
8391 specbind (Qinhibit_modification_hooks, Qt);
8393 if (clear_buffer_p && Z > BEG)
8394 del_range (BEG, Z);
8396 xassert (BEGV >= BEG);
8397 xassert (ZV <= Z && ZV >= BEGV);
8399 rc = fn (a1, a2, a3, a4);
8401 xassert (BEGV >= BEG);
8402 xassert (ZV <= Z && ZV >= BEGV);
8404 unbind_to (count, Qnil);
8405 return rc;
8409 /* Save state that should be preserved around the call to the function
8410 FN called in with_echo_area_buffer. */
8412 static Lisp_Object
8413 with_echo_area_buffer_unwind_data (w)
8414 struct window *w;
8416 int i = 0;
8417 Lisp_Object vector, tmp;
8419 /* Reduce consing by keeping one vector in
8420 Vwith_echo_area_save_vector. */
8421 vector = Vwith_echo_area_save_vector;
8422 Vwith_echo_area_save_vector = Qnil;
8424 if (NILP (vector))
8425 vector = Fmake_vector (make_number (7), Qnil);
8427 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8428 ASET (vector, i, Vdeactivate_mark); ++i;
8429 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8431 if (w)
8433 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8434 ASET (vector, i, w->buffer); ++i;
8435 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8436 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8438 else
8440 int end = i + 4;
8441 for (; i < end; ++i)
8442 ASET (vector, i, Qnil);
8445 xassert (i == ASIZE (vector));
8446 return vector;
8450 /* Restore global state from VECTOR which was created by
8451 with_echo_area_buffer_unwind_data. */
8453 static Lisp_Object
8454 unwind_with_echo_area_buffer (vector)
8455 Lisp_Object vector;
8457 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8458 Vdeactivate_mark = AREF (vector, 1);
8459 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8461 if (WINDOWP (AREF (vector, 3)))
8463 struct window *w;
8464 Lisp_Object buffer, charpos, bytepos;
8466 w = XWINDOW (AREF (vector, 3));
8467 buffer = AREF (vector, 4);
8468 charpos = AREF (vector, 5);
8469 bytepos = AREF (vector, 6);
8471 w->buffer = buffer;
8472 set_marker_both (w->pointm, buffer,
8473 XFASTINT (charpos), XFASTINT (bytepos));
8476 Vwith_echo_area_save_vector = vector;
8477 return Qnil;
8481 /* Set up the echo area for use by print functions. MULTIBYTE_P
8482 non-zero means we will print multibyte. */
8484 void
8485 setup_echo_area_for_printing (multibyte_p)
8486 int multibyte_p;
8488 /* If we can't find an echo area any more, exit. */
8489 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8490 Fkill_emacs (Qnil);
8492 ensure_echo_area_buffers ();
8494 if (!message_buf_print)
8496 /* A message has been output since the last time we printed.
8497 Choose a fresh echo area buffer. */
8498 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8499 echo_area_buffer[0] = echo_buffer[1];
8500 else
8501 echo_area_buffer[0] = echo_buffer[0];
8503 /* Switch to that buffer and clear it. */
8504 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8505 current_buffer->truncate_lines = Qnil;
8507 if (Z > BEG)
8509 int count = SPECPDL_INDEX ();
8510 specbind (Qinhibit_read_only, Qt);
8511 /* Note that undo recording is always disabled. */
8512 del_range (BEG, Z);
8513 unbind_to (count, Qnil);
8515 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8517 /* Set up the buffer for the multibyteness we need. */
8518 if (multibyte_p
8519 != !NILP (current_buffer->enable_multibyte_characters))
8520 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8522 /* Raise the frame containing the echo area. */
8523 if (minibuffer_auto_raise)
8525 struct frame *sf = SELECTED_FRAME ();
8526 Lisp_Object mini_window;
8527 mini_window = FRAME_MINIBUF_WINDOW (sf);
8528 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8531 message_log_maybe_newline ();
8532 message_buf_print = 1;
8534 else
8536 if (NILP (echo_area_buffer[0]))
8538 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8539 echo_area_buffer[0] = echo_buffer[1];
8540 else
8541 echo_area_buffer[0] = echo_buffer[0];
8544 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8546 /* Someone switched buffers between print requests. */
8547 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8548 current_buffer->truncate_lines = Qnil;
8554 /* Display an echo area message in window W. Value is non-zero if W's
8555 height is changed. If display_last_displayed_message_p is
8556 non-zero, display the message that was last displayed, otherwise
8557 display the current message. */
8559 static int
8560 display_echo_area (w)
8561 struct window *w;
8563 int i, no_message_p, window_height_changed_p, count;
8565 /* Temporarily disable garbage collections while displaying the echo
8566 area. This is done because a GC can print a message itself.
8567 That message would modify the echo area buffer's contents while a
8568 redisplay of the buffer is going on, and seriously confuse
8569 redisplay. */
8570 count = inhibit_garbage_collection ();
8572 /* If there is no message, we must call display_echo_area_1
8573 nevertheless because it resizes the window. But we will have to
8574 reset the echo_area_buffer in question to nil at the end because
8575 with_echo_area_buffer will sets it to an empty buffer. */
8576 i = display_last_displayed_message_p ? 1 : 0;
8577 no_message_p = NILP (echo_area_buffer[i]);
8579 window_height_changed_p
8580 = with_echo_area_buffer (w, display_last_displayed_message_p,
8581 display_echo_area_1,
8582 (EMACS_INT) w, Qnil, 0, 0);
8584 if (no_message_p)
8585 echo_area_buffer[i] = Qnil;
8587 unbind_to (count, Qnil);
8588 return window_height_changed_p;
8592 /* Helper for display_echo_area. Display the current buffer which
8593 contains the current echo area message in window W, a mini-window,
8594 a pointer to which is passed in A1. A2..A4 are currently not used.
8595 Change the height of W so that all of the message is displayed.
8596 Value is non-zero if height of W was changed. */
8598 static int
8599 display_echo_area_1 (a1, a2, a3, a4)
8600 EMACS_INT a1;
8601 Lisp_Object a2;
8602 EMACS_INT a3, a4;
8604 struct window *w = (struct window *) a1;
8605 Lisp_Object window;
8606 struct text_pos start;
8607 int window_height_changed_p = 0;
8609 /* Do this before displaying, so that we have a large enough glyph
8610 matrix for the display. If we can't get enough space for the
8611 whole text, display the last N lines. That works by setting w->start. */
8612 window_height_changed_p = resize_mini_window (w, 0);
8614 /* Use the starting position chosen by resize_mini_window. */
8615 SET_TEXT_POS_FROM_MARKER (start, w->start);
8617 /* Display. */
8618 clear_glyph_matrix (w->desired_matrix);
8619 XSETWINDOW (window, w);
8620 try_window (window, start, 0);
8622 return window_height_changed_p;
8626 /* Resize the echo area window to exactly the size needed for the
8627 currently displayed message, if there is one. If a mini-buffer
8628 is active, don't shrink it. */
8630 void
8631 resize_echo_area_exactly ()
8633 if (BUFFERP (echo_area_buffer[0])
8634 && WINDOWP (echo_area_window))
8636 struct window *w = XWINDOW (echo_area_window);
8637 int resized_p;
8638 Lisp_Object resize_exactly;
8640 if (minibuf_level == 0)
8641 resize_exactly = Qt;
8642 else
8643 resize_exactly = Qnil;
8645 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8646 (EMACS_INT) w, resize_exactly, 0, 0);
8647 if (resized_p)
8649 ++windows_or_buffers_changed;
8650 ++update_mode_lines;
8651 redisplay_internal (0);
8657 /* Callback function for with_echo_area_buffer, when used from
8658 resize_echo_area_exactly. A1 contains a pointer to the window to
8659 resize, EXACTLY non-nil means resize the mini-window exactly to the
8660 size of the text displayed. A3 and A4 are not used. Value is what
8661 resize_mini_window returns. */
8663 static int
8664 resize_mini_window_1 (a1, exactly, a3, a4)
8665 EMACS_INT a1;
8666 Lisp_Object exactly;
8667 EMACS_INT a3, a4;
8669 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8673 /* Resize mini-window W to fit the size of its contents. EXACT_P
8674 means size the window exactly to the size needed. Otherwise, it's
8675 only enlarged until W's buffer is empty.
8677 Set W->start to the right place to begin display. If the whole
8678 contents fit, start at the beginning. Otherwise, start so as
8679 to make the end of the contents appear. This is particularly
8680 important for y-or-n-p, but seems desirable generally.
8682 Value is non-zero if the window height has been changed. */
8685 resize_mini_window (w, exact_p)
8686 struct window *w;
8687 int exact_p;
8689 struct frame *f = XFRAME (w->frame);
8690 int window_height_changed_p = 0;
8692 xassert (MINI_WINDOW_P (w));
8694 /* By default, start display at the beginning. */
8695 set_marker_both (w->start, w->buffer,
8696 BUF_BEGV (XBUFFER (w->buffer)),
8697 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8699 /* Don't resize windows while redisplaying a window; it would
8700 confuse redisplay functions when the size of the window they are
8701 displaying changes from under them. Such a resizing can happen,
8702 for instance, when which-func prints a long message while
8703 we are running fontification-functions. We're running these
8704 functions with safe_call which binds inhibit-redisplay to t. */
8705 if (!NILP (Vinhibit_redisplay))
8706 return 0;
8708 /* Nil means don't try to resize. */
8709 if (NILP (Vresize_mini_windows)
8710 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8711 return 0;
8713 if (!FRAME_MINIBUF_ONLY_P (f))
8715 struct it it;
8716 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8717 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8718 int height, max_height;
8719 int unit = FRAME_LINE_HEIGHT (f);
8720 struct text_pos start;
8721 struct buffer *old_current_buffer = NULL;
8723 if (current_buffer != XBUFFER (w->buffer))
8725 old_current_buffer = current_buffer;
8726 set_buffer_internal (XBUFFER (w->buffer));
8729 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8731 /* Compute the max. number of lines specified by the user. */
8732 if (FLOATP (Vmax_mini_window_height))
8733 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8734 else if (INTEGERP (Vmax_mini_window_height))
8735 max_height = XINT (Vmax_mini_window_height);
8736 else
8737 max_height = total_height / 4;
8739 /* Correct that max. height if it's bogus. */
8740 max_height = max (1, max_height);
8741 max_height = min (total_height, max_height);
8743 /* Find out the height of the text in the window. */
8744 if (it.line_wrap == TRUNCATE)
8745 height = 1;
8746 else
8748 last_height = 0;
8749 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8750 if (it.max_ascent == 0 && it.max_descent == 0)
8751 height = it.current_y + last_height;
8752 else
8753 height = it.current_y + it.max_ascent + it.max_descent;
8754 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8755 height = (height + unit - 1) / unit;
8758 /* Compute a suitable window start. */
8759 if (height > max_height)
8761 height = max_height;
8762 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8763 move_it_vertically_backward (&it, (height - 1) * unit);
8764 start = it.current.pos;
8766 else
8767 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8768 SET_MARKER_FROM_TEXT_POS (w->start, start);
8770 if (EQ (Vresize_mini_windows, Qgrow_only))
8772 /* Let it grow only, until we display an empty message, in which
8773 case the window shrinks again. */
8774 if (height > WINDOW_TOTAL_LINES (w))
8776 int old_height = WINDOW_TOTAL_LINES (w);
8777 freeze_window_starts (f, 1);
8778 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8779 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8781 else if (height < WINDOW_TOTAL_LINES (w)
8782 && (exact_p || BEGV == ZV))
8784 int old_height = WINDOW_TOTAL_LINES (w);
8785 freeze_window_starts (f, 0);
8786 shrink_mini_window (w);
8787 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8790 else
8792 /* Always resize to exact size needed. */
8793 if (height > WINDOW_TOTAL_LINES (w))
8795 int old_height = WINDOW_TOTAL_LINES (w);
8796 freeze_window_starts (f, 1);
8797 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8798 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8800 else if (height < WINDOW_TOTAL_LINES (w))
8802 int old_height = WINDOW_TOTAL_LINES (w);
8803 freeze_window_starts (f, 0);
8804 shrink_mini_window (w);
8806 if (height)
8808 freeze_window_starts (f, 1);
8809 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8812 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8816 if (old_current_buffer)
8817 set_buffer_internal (old_current_buffer);
8820 return window_height_changed_p;
8824 /* Value is the current message, a string, or nil if there is no
8825 current message. */
8827 Lisp_Object
8828 current_message ()
8830 Lisp_Object msg;
8832 if (!BUFFERP (echo_area_buffer[0]))
8833 msg = Qnil;
8834 else
8836 with_echo_area_buffer (0, 0, current_message_1,
8837 (EMACS_INT) &msg, Qnil, 0, 0);
8838 if (NILP (msg))
8839 echo_area_buffer[0] = Qnil;
8842 return msg;
8846 static int
8847 current_message_1 (a1, a2, a3, a4)
8848 EMACS_INT a1;
8849 Lisp_Object a2;
8850 EMACS_INT a3, a4;
8852 Lisp_Object *msg = (Lisp_Object *) a1;
8854 if (Z > BEG)
8855 *msg = make_buffer_string (BEG, Z, 1);
8856 else
8857 *msg = Qnil;
8858 return 0;
8862 /* Push the current message on Vmessage_stack for later restauration
8863 by restore_message. Value is non-zero if the current message isn't
8864 empty. This is a relatively infrequent operation, so it's not
8865 worth optimizing. */
8868 push_message ()
8870 Lisp_Object msg;
8871 msg = current_message ();
8872 Vmessage_stack = Fcons (msg, Vmessage_stack);
8873 return STRINGP (msg);
8877 /* Restore message display from the top of Vmessage_stack. */
8879 void
8880 restore_message ()
8882 Lisp_Object msg;
8884 xassert (CONSP (Vmessage_stack));
8885 msg = XCAR (Vmessage_stack);
8886 if (STRINGP (msg))
8887 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8888 else
8889 message3_nolog (msg, 0, 0);
8893 /* Handler for record_unwind_protect calling pop_message. */
8895 Lisp_Object
8896 pop_message_unwind (dummy)
8897 Lisp_Object dummy;
8899 pop_message ();
8900 return Qnil;
8903 /* Pop the top-most entry off Vmessage_stack. */
8905 void
8906 pop_message ()
8908 xassert (CONSP (Vmessage_stack));
8909 Vmessage_stack = XCDR (Vmessage_stack);
8913 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8914 exits. If the stack is not empty, we have a missing pop_message
8915 somewhere. */
8917 void
8918 check_message_stack ()
8920 if (!NILP (Vmessage_stack))
8921 abort ();
8925 /* Truncate to NCHARS what will be displayed in the echo area the next
8926 time we display it---but don't redisplay it now. */
8928 void
8929 truncate_echo_area (nchars)
8930 int nchars;
8932 if (nchars == 0)
8933 echo_area_buffer[0] = Qnil;
8934 /* A null message buffer means that the frame hasn't really been
8935 initialized yet. Error messages get reported properly by
8936 cmd_error, so this must be just an informative message; toss it. */
8937 else if (!noninteractive
8938 && INTERACTIVE
8939 && !NILP (echo_area_buffer[0]))
8941 struct frame *sf = SELECTED_FRAME ();
8942 if (FRAME_MESSAGE_BUF (sf))
8943 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8948 /* Helper function for truncate_echo_area. Truncate the current
8949 message to at most NCHARS characters. */
8951 static int
8952 truncate_message_1 (nchars, a2, a3, a4)
8953 EMACS_INT nchars;
8954 Lisp_Object a2;
8955 EMACS_INT a3, a4;
8957 if (BEG + nchars < Z)
8958 del_range (BEG + nchars, Z);
8959 if (Z == BEG)
8960 echo_area_buffer[0] = Qnil;
8961 return 0;
8965 /* Set the current message to a substring of S or STRING.
8967 If STRING is a Lisp string, set the message to the first NBYTES
8968 bytes from STRING. NBYTES zero means use the whole string. If
8969 STRING is multibyte, the message will be displayed multibyte.
8971 If S is not null, set the message to the first LEN bytes of S. LEN
8972 zero means use the whole string. MULTIBYTE_P non-zero means S is
8973 multibyte. Display the message multibyte in that case.
8975 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8976 to t before calling set_message_1 (which calls insert).
8979 void
8980 set_message (s, string, nbytes, multibyte_p)
8981 const char *s;
8982 Lisp_Object string;
8983 int nbytes, multibyte_p;
8985 message_enable_multibyte
8986 = ((s && multibyte_p)
8987 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8989 with_echo_area_buffer (0, -1, set_message_1,
8990 (EMACS_INT) s, string, nbytes, multibyte_p);
8991 message_buf_print = 0;
8992 help_echo_showing_p = 0;
8996 /* Helper function for set_message. Arguments have the same meaning
8997 as there, with A1 corresponding to S and A2 corresponding to STRING
8998 This function is called with the echo area buffer being
8999 current. */
9001 static int
9002 set_message_1 (a1, a2, nbytes, multibyte_p)
9003 EMACS_INT a1;
9004 Lisp_Object a2;
9005 EMACS_INT nbytes, multibyte_p;
9007 const char *s = (const char *) a1;
9008 Lisp_Object string = a2;
9010 /* Change multibyteness of the echo buffer appropriately. */
9011 if (message_enable_multibyte
9012 != !NILP (current_buffer->enable_multibyte_characters))
9013 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9015 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9017 /* Insert new message at BEG. */
9018 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9020 if (STRINGP (string))
9022 int nchars;
9024 if (nbytes == 0)
9025 nbytes = SBYTES (string);
9026 nchars = string_byte_to_char (string, nbytes);
9028 /* This function takes care of single/multibyte conversion. We
9029 just have to ensure that the echo area buffer has the right
9030 setting of enable_multibyte_characters. */
9031 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9033 else if (s)
9035 if (nbytes == 0)
9036 nbytes = strlen (s);
9038 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9040 /* Convert from multi-byte to single-byte. */
9041 int i, c, n;
9042 unsigned char work[1];
9044 /* Convert a multibyte string to single-byte. */
9045 for (i = 0; i < nbytes; i += n)
9047 c = string_char_and_length (s + i, nbytes - i, &n);
9048 work[0] = (ASCII_CHAR_P (c)
9050 : multibyte_char_to_unibyte (c, Qnil));
9051 insert_1_both (work, 1, 1, 1, 0, 0);
9054 else if (!multibyte_p
9055 && !NILP (current_buffer->enable_multibyte_characters))
9057 /* Convert from single-byte to multi-byte. */
9058 int i, c, n;
9059 const unsigned char *msg = (const unsigned char *) s;
9060 unsigned char str[MAX_MULTIBYTE_LENGTH];
9062 /* Convert a single-byte string to multibyte. */
9063 for (i = 0; i < nbytes; i++)
9065 c = msg[i];
9066 c = unibyte_char_to_multibyte (c);
9067 n = CHAR_STRING (c, str);
9068 insert_1_both (str, 1, n, 1, 0, 0);
9071 else
9072 insert_1 (s, nbytes, 1, 0, 0);
9075 return 0;
9079 /* Clear messages. CURRENT_P non-zero means clear the current
9080 message. LAST_DISPLAYED_P non-zero means clear the message
9081 last displayed. */
9083 void
9084 clear_message (current_p, last_displayed_p)
9085 int current_p, last_displayed_p;
9087 if (current_p)
9089 echo_area_buffer[0] = Qnil;
9090 message_cleared_p = 1;
9093 if (last_displayed_p)
9094 echo_area_buffer[1] = Qnil;
9096 message_buf_print = 0;
9099 /* Clear garbaged frames.
9101 This function is used where the old redisplay called
9102 redraw_garbaged_frames which in turn called redraw_frame which in
9103 turn called clear_frame. The call to clear_frame was a source of
9104 flickering. I believe a clear_frame is not necessary. It should
9105 suffice in the new redisplay to invalidate all current matrices,
9106 and ensure a complete redisplay of all windows. */
9108 static void
9109 clear_garbaged_frames ()
9111 if (frame_garbaged)
9113 Lisp_Object tail, frame;
9114 int changed_count = 0;
9116 FOR_EACH_FRAME (tail, frame)
9118 struct frame *f = XFRAME (frame);
9120 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9122 if (f->resized_p)
9124 Fredraw_frame (frame);
9125 f->force_flush_display_p = 1;
9127 clear_current_matrices (f);
9128 changed_count++;
9129 f->garbaged = 0;
9130 f->resized_p = 0;
9134 frame_garbaged = 0;
9135 if (changed_count)
9136 ++windows_or_buffers_changed;
9141 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9142 is non-zero update selected_frame. Value is non-zero if the
9143 mini-windows height has been changed. */
9145 static int
9146 echo_area_display (update_frame_p)
9147 int update_frame_p;
9149 Lisp_Object mini_window;
9150 struct window *w;
9151 struct frame *f;
9152 int window_height_changed_p = 0;
9153 struct frame *sf = SELECTED_FRAME ();
9155 mini_window = FRAME_MINIBUF_WINDOW (sf);
9156 w = XWINDOW (mini_window);
9157 f = XFRAME (WINDOW_FRAME (w));
9159 /* Don't display if frame is invisible or not yet initialized. */
9160 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9161 return 0;
9163 #ifdef HAVE_WINDOW_SYSTEM
9164 /* When Emacs starts, selected_frame may be the initial terminal
9165 frame. If we let this through, a message would be displayed on
9166 the terminal. */
9167 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9168 return 0;
9169 #endif /* HAVE_WINDOW_SYSTEM */
9171 /* Redraw garbaged frames. */
9172 if (frame_garbaged)
9173 clear_garbaged_frames ();
9175 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9177 echo_area_window = mini_window;
9178 window_height_changed_p = display_echo_area (w);
9179 w->must_be_updated_p = 1;
9181 /* Update the display, unless called from redisplay_internal.
9182 Also don't update the screen during redisplay itself. The
9183 update will happen at the end of redisplay, and an update
9184 here could cause confusion. */
9185 if (update_frame_p && !redisplaying_p)
9187 int n = 0;
9189 /* If the display update has been interrupted by pending
9190 input, update mode lines in the frame. Due to the
9191 pending input, it might have been that redisplay hasn't
9192 been called, so that mode lines above the echo area are
9193 garbaged. This looks odd, so we prevent it here. */
9194 if (!display_completed)
9195 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9197 if (window_height_changed_p
9198 /* Don't do this if Emacs is shutting down. Redisplay
9199 needs to run hooks. */
9200 && !NILP (Vrun_hooks))
9202 /* Must update other windows. Likewise as in other
9203 cases, don't let this update be interrupted by
9204 pending input. */
9205 int count = SPECPDL_INDEX ();
9206 specbind (Qredisplay_dont_pause, Qt);
9207 windows_or_buffers_changed = 1;
9208 redisplay_internal (0);
9209 unbind_to (count, Qnil);
9211 else if (FRAME_WINDOW_P (f) && n == 0)
9213 /* Window configuration is the same as before.
9214 Can do with a display update of the echo area,
9215 unless we displayed some mode lines. */
9216 update_single_window (w, 1);
9217 FRAME_RIF (f)->flush_display (f);
9219 else
9220 update_frame (f, 1, 1);
9222 /* If cursor is in the echo area, make sure that the next
9223 redisplay displays the minibuffer, so that the cursor will
9224 be replaced with what the minibuffer wants. */
9225 if (cursor_in_echo_area)
9226 ++windows_or_buffers_changed;
9229 else if (!EQ (mini_window, selected_window))
9230 windows_or_buffers_changed++;
9232 /* Last displayed message is now the current message. */
9233 echo_area_buffer[1] = echo_area_buffer[0];
9234 /* Inform read_char that we're not echoing. */
9235 echo_message_buffer = Qnil;
9237 /* Prevent redisplay optimization in redisplay_internal by resetting
9238 this_line_start_pos. This is done because the mini-buffer now
9239 displays the message instead of its buffer text. */
9240 if (EQ (mini_window, selected_window))
9241 CHARPOS (this_line_start_pos) = 0;
9243 return window_height_changed_p;
9248 /***********************************************************************
9249 Mode Lines and Frame Titles
9250 ***********************************************************************/
9252 /* A buffer for constructing non-propertized mode-line strings and
9253 frame titles in it; allocated from the heap in init_xdisp and
9254 resized as needed in store_mode_line_noprop_char. */
9256 static char *mode_line_noprop_buf;
9258 /* The buffer's end, and a current output position in it. */
9260 static char *mode_line_noprop_buf_end;
9261 static char *mode_line_noprop_ptr;
9263 #define MODE_LINE_NOPROP_LEN(start) \
9264 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9266 static enum {
9267 MODE_LINE_DISPLAY = 0,
9268 MODE_LINE_TITLE,
9269 MODE_LINE_NOPROP,
9270 MODE_LINE_STRING
9271 } mode_line_target;
9273 /* Alist that caches the results of :propertize.
9274 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9275 static Lisp_Object mode_line_proptrans_alist;
9277 /* List of strings making up the mode-line. */
9278 static Lisp_Object mode_line_string_list;
9280 /* Base face property when building propertized mode line string. */
9281 static Lisp_Object mode_line_string_face;
9282 static Lisp_Object mode_line_string_face_prop;
9285 /* Unwind data for mode line strings */
9287 static Lisp_Object Vmode_line_unwind_vector;
9289 static Lisp_Object
9290 format_mode_line_unwind_data (struct buffer *obuf,
9291 Lisp_Object owin,
9292 int save_proptrans)
9294 Lisp_Object vector, tmp;
9296 /* Reduce consing by keeping one vector in
9297 Vwith_echo_area_save_vector. */
9298 vector = Vmode_line_unwind_vector;
9299 Vmode_line_unwind_vector = Qnil;
9301 if (NILP (vector))
9302 vector = Fmake_vector (make_number (8), Qnil);
9304 ASET (vector, 0, make_number (mode_line_target));
9305 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9306 ASET (vector, 2, mode_line_string_list);
9307 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9308 ASET (vector, 4, mode_line_string_face);
9309 ASET (vector, 5, mode_line_string_face_prop);
9311 if (obuf)
9312 XSETBUFFER (tmp, obuf);
9313 else
9314 tmp = Qnil;
9315 ASET (vector, 6, tmp);
9316 ASET (vector, 7, owin);
9318 return vector;
9321 static Lisp_Object
9322 unwind_format_mode_line (vector)
9323 Lisp_Object vector;
9325 mode_line_target = XINT (AREF (vector, 0));
9326 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9327 mode_line_string_list = AREF (vector, 2);
9328 if (! EQ (AREF (vector, 3), Qt))
9329 mode_line_proptrans_alist = AREF (vector, 3);
9330 mode_line_string_face = AREF (vector, 4);
9331 mode_line_string_face_prop = AREF (vector, 5);
9333 if (!NILP (AREF (vector, 7)))
9334 /* Select window before buffer, since it may change the buffer. */
9335 Fselect_window (AREF (vector, 7), Qt);
9337 if (!NILP (AREF (vector, 6)))
9339 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9340 ASET (vector, 6, Qnil);
9343 Vmode_line_unwind_vector = vector;
9344 return Qnil;
9348 /* Store a single character C for the frame title in mode_line_noprop_buf.
9349 Re-allocate mode_line_noprop_buf if necessary. */
9351 static void
9352 #ifdef PROTOTYPES
9353 store_mode_line_noprop_char (char c)
9354 #else
9355 store_mode_line_noprop_char (c)
9356 char c;
9357 #endif
9359 /* If output position has reached the end of the allocated buffer,
9360 double the buffer's size. */
9361 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9363 int len = MODE_LINE_NOPROP_LEN (0);
9364 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9365 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9366 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9367 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9370 *mode_line_noprop_ptr++ = c;
9374 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9375 mode_line_noprop_ptr. STR is the string to store. Do not copy
9376 characters that yield more columns than PRECISION; PRECISION <= 0
9377 means copy the whole string. Pad with spaces until FIELD_WIDTH
9378 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9379 pad. Called from display_mode_element when it is used to build a
9380 frame title. */
9382 static int
9383 store_mode_line_noprop (str, field_width, precision)
9384 const unsigned char *str;
9385 int field_width, precision;
9387 int n = 0;
9388 int dummy, nbytes;
9390 /* Copy at most PRECISION chars from STR. */
9391 nbytes = strlen (str);
9392 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9393 while (nbytes--)
9394 store_mode_line_noprop_char (*str++);
9396 /* Fill up with spaces until FIELD_WIDTH reached. */
9397 while (field_width > 0
9398 && n < field_width)
9400 store_mode_line_noprop_char (' ');
9401 ++n;
9404 return n;
9407 /***********************************************************************
9408 Frame Titles
9409 ***********************************************************************/
9411 #ifdef HAVE_WINDOW_SYSTEM
9413 /* Set the title of FRAME, if it has changed. The title format is
9414 Vicon_title_format if FRAME is iconified, otherwise it is
9415 frame_title_format. */
9417 static void
9418 x_consider_frame_title (frame)
9419 Lisp_Object frame;
9421 struct frame *f = XFRAME (frame);
9423 if (FRAME_WINDOW_P (f)
9424 || FRAME_MINIBUF_ONLY_P (f)
9425 || f->explicit_name)
9427 /* Do we have more than one visible frame on this X display? */
9428 Lisp_Object tail;
9429 Lisp_Object fmt;
9430 int title_start;
9431 char *title;
9432 int len;
9433 struct it it;
9434 int count = SPECPDL_INDEX ();
9436 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9438 Lisp_Object other_frame = XCAR (tail);
9439 struct frame *tf = XFRAME (other_frame);
9441 if (tf != f
9442 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9443 && !FRAME_MINIBUF_ONLY_P (tf)
9444 && !EQ (other_frame, tip_frame)
9445 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9446 break;
9449 /* Set global variable indicating that multiple frames exist. */
9450 multiple_frames = CONSP (tail);
9452 /* Switch to the buffer of selected window of the frame. Set up
9453 mode_line_target so that display_mode_element will output into
9454 mode_line_noprop_buf; then display the title. */
9455 record_unwind_protect (unwind_format_mode_line,
9456 format_mode_line_unwind_data
9457 (current_buffer, selected_window, 0));
9459 Fselect_window (f->selected_window, Qt);
9460 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9461 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9463 mode_line_target = MODE_LINE_TITLE;
9464 title_start = MODE_LINE_NOPROP_LEN (0);
9465 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9466 NULL, DEFAULT_FACE_ID);
9467 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9468 len = MODE_LINE_NOPROP_LEN (title_start);
9469 title = mode_line_noprop_buf + title_start;
9470 unbind_to (count, Qnil);
9472 /* Set the title only if it's changed. This avoids consing in
9473 the common case where it hasn't. (If it turns out that we've
9474 already wasted too much time by walking through the list with
9475 display_mode_element, then we might need to optimize at a
9476 higher level than this.) */
9477 if (! STRINGP (f->name)
9478 || SBYTES (f->name) != len
9479 || bcmp (title, SDATA (f->name), len) != 0)
9481 #ifdef HAVE_NS
9482 if (FRAME_NS_P (f))
9484 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9486 if (EQ (fmt, Qt))
9487 ns_set_name_as_filename (f);
9488 else
9489 x_implicitly_set_name (f, make_string(title, len),
9490 Qnil);
9493 else
9494 #endif
9495 x_implicitly_set_name (f, make_string (title, len), Qnil);
9497 #ifdef HAVE_NS
9498 if (FRAME_NS_P (f))
9500 /* do this also for frames with explicit names */
9501 ns_implicitly_set_icon_type(f);
9502 ns_set_doc_edited(f, Fbuffer_modified_p
9503 (XWINDOW (f->selected_window)->buffer), Qnil);
9505 #endif
9509 #endif /* not HAVE_WINDOW_SYSTEM */
9514 /***********************************************************************
9515 Menu Bars
9516 ***********************************************************************/
9519 /* Prepare for redisplay by updating menu-bar item lists when
9520 appropriate. This can call eval. */
9522 void
9523 prepare_menu_bars ()
9525 int all_windows;
9526 struct gcpro gcpro1, gcpro2;
9527 struct frame *f;
9528 Lisp_Object tooltip_frame;
9530 #ifdef HAVE_WINDOW_SYSTEM
9531 tooltip_frame = tip_frame;
9532 #else
9533 tooltip_frame = Qnil;
9534 #endif
9536 /* Update all frame titles based on their buffer names, etc. We do
9537 this before the menu bars so that the buffer-menu will show the
9538 up-to-date frame titles. */
9539 #ifdef HAVE_WINDOW_SYSTEM
9540 if (windows_or_buffers_changed || update_mode_lines)
9542 Lisp_Object tail, frame;
9544 FOR_EACH_FRAME (tail, frame)
9546 f = XFRAME (frame);
9547 if (!EQ (frame, tooltip_frame)
9548 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9549 x_consider_frame_title (frame);
9552 #endif /* HAVE_WINDOW_SYSTEM */
9554 /* Update the menu bar item lists, if appropriate. This has to be
9555 done before any actual redisplay or generation of display lines. */
9556 all_windows = (update_mode_lines
9557 || buffer_shared > 1
9558 || windows_or_buffers_changed);
9559 if (all_windows)
9561 Lisp_Object tail, frame;
9562 int count = SPECPDL_INDEX ();
9563 /* 1 means that update_menu_bar has run its hooks
9564 so any further calls to update_menu_bar shouldn't do so again. */
9565 int menu_bar_hooks_run = 0;
9567 record_unwind_save_match_data ();
9569 FOR_EACH_FRAME (tail, frame)
9571 f = XFRAME (frame);
9573 /* Ignore tooltip frame. */
9574 if (EQ (frame, tooltip_frame))
9575 continue;
9577 /* If a window on this frame changed size, report that to
9578 the user and clear the size-change flag. */
9579 if (FRAME_WINDOW_SIZES_CHANGED (f))
9581 Lisp_Object functions;
9583 /* Clear flag first in case we get an error below. */
9584 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9585 functions = Vwindow_size_change_functions;
9586 GCPRO2 (tail, functions);
9588 while (CONSP (functions))
9590 if (!EQ (XCAR (functions), Qt))
9591 call1 (XCAR (functions), frame);
9592 functions = XCDR (functions);
9594 UNGCPRO;
9597 GCPRO1 (tail);
9598 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9599 #ifdef HAVE_WINDOW_SYSTEM
9600 update_tool_bar (f, 0);
9601 #endif
9602 UNGCPRO;
9605 unbind_to (count, Qnil);
9607 else
9609 struct frame *sf = SELECTED_FRAME ();
9610 update_menu_bar (sf, 1, 0);
9611 #ifdef HAVE_WINDOW_SYSTEM
9612 update_tool_bar (sf, 1);
9613 #endif
9616 /* Motif needs this. See comment in xmenu.c. Turn it off when
9617 pending_menu_activation is not defined. */
9618 #ifdef USE_X_TOOLKIT
9619 pending_menu_activation = 0;
9620 #endif
9624 /* Update the menu bar item list for frame F. This has to be done
9625 before we start to fill in any display lines, because it can call
9626 eval.
9628 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9630 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9631 already ran the menu bar hooks for this redisplay, so there
9632 is no need to run them again. The return value is the
9633 updated value of this flag, to pass to the next call. */
9635 static int
9636 update_menu_bar (f, save_match_data, hooks_run)
9637 struct frame *f;
9638 int save_match_data;
9639 int hooks_run;
9641 Lisp_Object window;
9642 register struct window *w;
9644 /* If called recursively during a menu update, do nothing. This can
9645 happen when, for instance, an activate-menubar-hook causes a
9646 redisplay. */
9647 if (inhibit_menubar_update)
9648 return hooks_run;
9650 window = FRAME_SELECTED_WINDOW (f);
9651 w = XWINDOW (window);
9653 if (FRAME_WINDOW_P (f)
9655 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9656 || defined (HAVE_NS) || defined (USE_GTK)
9657 FRAME_EXTERNAL_MENU_BAR (f)
9658 #else
9659 FRAME_MENU_BAR_LINES (f) > 0
9660 #endif
9661 : FRAME_MENU_BAR_LINES (f) > 0)
9663 /* If the user has switched buffers or windows, we need to
9664 recompute to reflect the new bindings. But we'll
9665 recompute when update_mode_lines is set too; that means
9666 that people can use force-mode-line-update to request
9667 that the menu bar be recomputed. The adverse effect on
9668 the rest of the redisplay algorithm is about the same as
9669 windows_or_buffers_changed anyway. */
9670 if (windows_or_buffers_changed
9671 /* This used to test w->update_mode_line, but we believe
9672 there is no need to recompute the menu in that case. */
9673 || update_mode_lines
9674 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9675 < BUF_MODIFF (XBUFFER (w->buffer)))
9676 != !NILP (w->last_had_star))
9677 || ((!NILP (Vtransient_mark_mode)
9678 && !NILP (XBUFFER (w->buffer)->mark_active))
9679 != !NILP (w->region_showing)))
9681 struct buffer *prev = current_buffer;
9682 int count = SPECPDL_INDEX ();
9684 specbind (Qinhibit_menubar_update, Qt);
9686 set_buffer_internal_1 (XBUFFER (w->buffer));
9687 if (save_match_data)
9688 record_unwind_save_match_data ();
9689 if (NILP (Voverriding_local_map_menu_flag))
9691 specbind (Qoverriding_terminal_local_map, Qnil);
9692 specbind (Qoverriding_local_map, Qnil);
9695 if (!hooks_run)
9697 /* Run the Lucid hook. */
9698 safe_run_hooks (Qactivate_menubar_hook);
9700 /* If it has changed current-menubar from previous value,
9701 really recompute the menu-bar from the value. */
9702 if (! NILP (Vlucid_menu_bar_dirty_flag))
9703 call0 (Qrecompute_lucid_menubar);
9705 safe_run_hooks (Qmenu_bar_update_hook);
9707 hooks_run = 1;
9710 XSETFRAME (Vmenu_updating_frame, f);
9711 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9713 /* Redisplay the menu bar in case we changed it. */
9714 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9715 || defined (HAVE_NS) || defined (USE_GTK)
9716 if (FRAME_WINDOW_P (f))
9718 #if defined (HAVE_NS)
9719 /* All frames on Mac OS share the same menubar. So only
9720 the selected frame should be allowed to set it. */
9721 if (f == SELECTED_FRAME ())
9722 #endif
9723 set_frame_menubar (f, 0, 0);
9725 else
9726 /* On a terminal screen, the menu bar is an ordinary screen
9727 line, and this makes it get updated. */
9728 w->update_mode_line = Qt;
9729 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9730 /* In the non-toolkit version, the menu bar is an ordinary screen
9731 line, and this makes it get updated. */
9732 w->update_mode_line = Qt;
9733 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9735 unbind_to (count, Qnil);
9736 set_buffer_internal_1 (prev);
9740 return hooks_run;
9745 /***********************************************************************
9746 Output Cursor
9747 ***********************************************************************/
9749 #ifdef HAVE_WINDOW_SYSTEM
9751 /* EXPORT:
9752 Nominal cursor position -- where to draw output.
9753 HPOS and VPOS are window relative glyph matrix coordinates.
9754 X and Y are window relative pixel coordinates. */
9756 struct cursor_pos output_cursor;
9759 /* EXPORT:
9760 Set the global variable output_cursor to CURSOR. All cursor
9761 positions are relative to updated_window. */
9763 void
9764 set_output_cursor (cursor)
9765 struct cursor_pos *cursor;
9767 output_cursor.hpos = cursor->hpos;
9768 output_cursor.vpos = cursor->vpos;
9769 output_cursor.x = cursor->x;
9770 output_cursor.y = cursor->y;
9774 /* EXPORT for RIF:
9775 Set a nominal cursor position.
9777 HPOS and VPOS are column/row positions in a window glyph matrix. X
9778 and Y are window text area relative pixel positions.
9780 If this is done during an update, updated_window will contain the
9781 window that is being updated and the position is the future output
9782 cursor position for that window. If updated_window is null, use
9783 selected_window and display the cursor at the given position. */
9785 void
9786 x_cursor_to (vpos, hpos, y, x)
9787 int vpos, hpos, y, x;
9789 struct window *w;
9791 /* If updated_window is not set, work on selected_window. */
9792 if (updated_window)
9793 w = updated_window;
9794 else
9795 w = XWINDOW (selected_window);
9797 /* Set the output cursor. */
9798 output_cursor.hpos = hpos;
9799 output_cursor.vpos = vpos;
9800 output_cursor.x = x;
9801 output_cursor.y = y;
9803 /* If not called as part of an update, really display the cursor.
9804 This will also set the cursor position of W. */
9805 if (updated_window == NULL)
9807 BLOCK_INPUT;
9808 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9809 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9810 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9811 UNBLOCK_INPUT;
9815 #endif /* HAVE_WINDOW_SYSTEM */
9818 /***********************************************************************
9819 Tool-bars
9820 ***********************************************************************/
9822 #ifdef HAVE_WINDOW_SYSTEM
9824 /* Where the mouse was last time we reported a mouse event. */
9826 FRAME_PTR last_mouse_frame;
9828 /* Tool-bar item index of the item on which a mouse button was pressed
9829 or -1. */
9831 int last_tool_bar_item;
9834 static Lisp_Object
9835 update_tool_bar_unwind (frame)
9836 Lisp_Object frame;
9838 selected_frame = frame;
9839 return Qnil;
9842 /* Update the tool-bar item list for frame F. This has to be done
9843 before we start to fill in any display lines. Called from
9844 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9845 and restore it here. */
9847 static void
9848 update_tool_bar (f, save_match_data)
9849 struct frame *f;
9850 int save_match_data;
9852 #if defined (USE_GTK) || defined (HAVE_NS)
9853 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9854 #else
9855 int do_update = WINDOWP (f->tool_bar_window)
9856 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9857 #endif
9859 if (do_update)
9861 Lisp_Object window;
9862 struct window *w;
9864 window = FRAME_SELECTED_WINDOW (f);
9865 w = XWINDOW (window);
9867 /* If the user has switched buffers or windows, we need to
9868 recompute to reflect the new bindings. But we'll
9869 recompute when update_mode_lines is set too; that means
9870 that people can use force-mode-line-update to request
9871 that the menu bar be recomputed. The adverse effect on
9872 the rest of the redisplay algorithm is about the same as
9873 windows_or_buffers_changed anyway. */
9874 if (windows_or_buffers_changed
9875 || !NILP (w->update_mode_line)
9876 || update_mode_lines
9877 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9878 < BUF_MODIFF (XBUFFER (w->buffer)))
9879 != !NILP (w->last_had_star))
9880 || ((!NILP (Vtransient_mark_mode)
9881 && !NILP (XBUFFER (w->buffer)->mark_active))
9882 != !NILP (w->region_showing)))
9884 struct buffer *prev = current_buffer;
9885 int count = SPECPDL_INDEX ();
9886 Lisp_Object frame, new_tool_bar;
9887 int new_n_tool_bar;
9888 struct gcpro gcpro1;
9890 /* Set current_buffer to the buffer of the selected
9891 window of the frame, so that we get the right local
9892 keymaps. */
9893 set_buffer_internal_1 (XBUFFER (w->buffer));
9895 /* Save match data, if we must. */
9896 if (save_match_data)
9897 record_unwind_save_match_data ();
9899 /* Make sure that we don't accidentally use bogus keymaps. */
9900 if (NILP (Voverriding_local_map_menu_flag))
9902 specbind (Qoverriding_terminal_local_map, Qnil);
9903 specbind (Qoverriding_local_map, Qnil);
9906 GCPRO1 (new_tool_bar);
9908 /* We must temporarily set the selected frame to this frame
9909 before calling tool_bar_items, because the calculation of
9910 the tool-bar keymap uses the selected frame (see
9911 `tool-bar-make-keymap' in tool-bar.el). */
9912 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9913 XSETFRAME (frame, f);
9914 selected_frame = frame;
9916 /* Build desired tool-bar items from keymaps. */
9917 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9918 &new_n_tool_bar);
9920 /* Redisplay the tool-bar if we changed it. */
9921 if (new_n_tool_bar != f->n_tool_bar_items
9922 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9924 /* Redisplay that happens asynchronously due to an expose event
9925 may access f->tool_bar_items. Make sure we update both
9926 variables within BLOCK_INPUT so no such event interrupts. */
9927 BLOCK_INPUT;
9928 f->tool_bar_items = new_tool_bar;
9929 f->n_tool_bar_items = new_n_tool_bar;
9930 w->update_mode_line = Qt;
9931 UNBLOCK_INPUT;
9934 UNGCPRO;
9936 unbind_to (count, Qnil);
9937 set_buffer_internal_1 (prev);
9943 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9944 F's desired tool-bar contents. F->tool_bar_items must have
9945 been set up previously by calling prepare_menu_bars. */
9947 static void
9948 build_desired_tool_bar_string (f)
9949 struct frame *f;
9951 int i, size, size_needed;
9952 struct gcpro gcpro1, gcpro2, gcpro3;
9953 Lisp_Object image, plist, props;
9955 image = plist = props = Qnil;
9956 GCPRO3 (image, plist, props);
9958 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9959 Otherwise, make a new string. */
9961 /* The size of the string we might be able to reuse. */
9962 size = (STRINGP (f->desired_tool_bar_string)
9963 ? SCHARS (f->desired_tool_bar_string)
9964 : 0);
9966 /* We need one space in the string for each image. */
9967 size_needed = f->n_tool_bar_items;
9969 /* Reuse f->desired_tool_bar_string, if possible. */
9970 if (size < size_needed || NILP (f->desired_tool_bar_string))
9971 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9972 make_number (' '));
9973 else
9975 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9976 Fremove_text_properties (make_number (0), make_number (size),
9977 props, f->desired_tool_bar_string);
9980 /* Put a `display' property on the string for the images to display,
9981 put a `menu_item' property on tool-bar items with a value that
9982 is the index of the item in F's tool-bar item vector. */
9983 for (i = 0; i < f->n_tool_bar_items; ++i)
9985 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9987 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9988 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9989 int hmargin, vmargin, relief, idx, end;
9990 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9992 /* If image is a vector, choose the image according to the
9993 button state. */
9994 image = PROP (TOOL_BAR_ITEM_IMAGES);
9995 if (VECTORP (image))
9997 if (enabled_p)
9998 idx = (selected_p
9999 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10000 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10001 else
10002 idx = (selected_p
10003 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10004 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10006 xassert (ASIZE (image) >= idx);
10007 image = AREF (image, idx);
10009 else
10010 idx = -1;
10012 /* Ignore invalid image specifications. */
10013 if (!valid_image_p (image))
10014 continue;
10016 /* Display the tool-bar button pressed, or depressed. */
10017 plist = Fcopy_sequence (XCDR (image));
10019 /* Compute margin and relief to draw. */
10020 relief = (tool_bar_button_relief >= 0
10021 ? tool_bar_button_relief
10022 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10023 hmargin = vmargin = relief;
10025 if (INTEGERP (Vtool_bar_button_margin)
10026 && XINT (Vtool_bar_button_margin) > 0)
10028 hmargin += XFASTINT (Vtool_bar_button_margin);
10029 vmargin += XFASTINT (Vtool_bar_button_margin);
10031 else if (CONSP (Vtool_bar_button_margin))
10033 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10034 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10035 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10037 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10038 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10039 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10042 if (auto_raise_tool_bar_buttons_p)
10044 /* Add a `:relief' property to the image spec if the item is
10045 selected. */
10046 if (selected_p)
10048 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10049 hmargin -= relief;
10050 vmargin -= relief;
10053 else
10055 /* If image is selected, display it pressed, i.e. with a
10056 negative relief. If it's not selected, display it with a
10057 raised relief. */
10058 plist = Fplist_put (plist, QCrelief,
10059 (selected_p
10060 ? make_number (-relief)
10061 : make_number (relief)));
10062 hmargin -= relief;
10063 vmargin -= relief;
10066 /* Put a margin around the image. */
10067 if (hmargin || vmargin)
10069 if (hmargin == vmargin)
10070 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10071 else
10072 plist = Fplist_put (plist, QCmargin,
10073 Fcons (make_number (hmargin),
10074 make_number (vmargin)));
10077 /* If button is not enabled, and we don't have special images
10078 for the disabled state, make the image appear disabled by
10079 applying an appropriate algorithm to it. */
10080 if (!enabled_p && idx < 0)
10081 plist = Fplist_put (plist, QCconversion, Qdisabled);
10083 /* Put a `display' text property on the string for the image to
10084 display. Put a `menu-item' property on the string that gives
10085 the start of this item's properties in the tool-bar items
10086 vector. */
10087 image = Fcons (Qimage, plist);
10088 props = list4 (Qdisplay, image,
10089 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10091 /* Let the last image hide all remaining spaces in the tool bar
10092 string. The string can be longer than needed when we reuse a
10093 previous string. */
10094 if (i + 1 == f->n_tool_bar_items)
10095 end = SCHARS (f->desired_tool_bar_string);
10096 else
10097 end = i + 1;
10098 Fadd_text_properties (make_number (i), make_number (end),
10099 props, f->desired_tool_bar_string);
10100 #undef PROP
10103 UNGCPRO;
10107 /* Display one line of the tool-bar of frame IT->f.
10109 HEIGHT specifies the desired height of the tool-bar line.
10110 If the actual height of the glyph row is less than HEIGHT, the
10111 row's height is increased to HEIGHT, and the icons are centered
10112 vertically in the new height.
10114 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10115 count a final empty row in case the tool-bar width exactly matches
10116 the window width.
10119 static void
10120 display_tool_bar_line (it, height)
10121 struct it *it;
10122 int height;
10124 struct glyph_row *row = it->glyph_row;
10125 int max_x = it->last_visible_x;
10126 struct glyph *last;
10128 prepare_desired_row (row);
10129 row->y = it->current_y;
10131 /* Note that this isn't made use of if the face hasn't a box,
10132 so there's no need to check the face here. */
10133 it->start_of_box_run_p = 1;
10135 while (it->current_x < max_x)
10137 int x, n_glyphs_before, i, nglyphs;
10138 struct it it_before;
10140 /* Get the next display element. */
10141 if (!get_next_display_element (it))
10143 /* Don't count empty row if we are counting needed tool-bar lines. */
10144 if (height < 0 && !it->hpos)
10145 return;
10146 break;
10149 /* Produce glyphs. */
10150 n_glyphs_before = row->used[TEXT_AREA];
10151 it_before = *it;
10153 PRODUCE_GLYPHS (it);
10155 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10156 i = 0;
10157 x = it_before.current_x;
10158 while (i < nglyphs)
10160 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10162 if (x + glyph->pixel_width > max_x)
10164 /* Glyph doesn't fit on line. Backtrack. */
10165 row->used[TEXT_AREA] = n_glyphs_before;
10166 *it = it_before;
10167 /* If this is the only glyph on this line, it will never fit on the
10168 toolbar, so skip it. But ensure there is at least one glyph,
10169 so we don't accidentally disable the tool-bar. */
10170 if (n_glyphs_before == 0
10171 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10172 break;
10173 goto out;
10176 ++it->hpos;
10177 x += glyph->pixel_width;
10178 ++i;
10181 /* Stop at line ends. */
10182 if (ITERATOR_AT_END_OF_LINE_P (it))
10183 break;
10185 set_iterator_to_next (it, 1);
10188 out:;
10190 row->displays_text_p = row->used[TEXT_AREA] != 0;
10192 /* Use default face for the border below the tool bar.
10194 FIXME: When auto-resize-tool-bars is grow-only, there is
10195 no additional border below the possibly empty tool-bar lines.
10196 So to make the extra empty lines look "normal", we have to
10197 use the tool-bar face for the border too. */
10198 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10199 it->face_id = DEFAULT_FACE_ID;
10201 extend_face_to_end_of_line (it);
10202 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10203 last->right_box_line_p = 1;
10204 if (last == row->glyphs[TEXT_AREA])
10205 last->left_box_line_p = 1;
10207 /* Make line the desired height and center it vertically. */
10208 if ((height -= it->max_ascent + it->max_descent) > 0)
10210 /* Don't add more than one line height. */
10211 height %= FRAME_LINE_HEIGHT (it->f);
10212 it->max_ascent += height / 2;
10213 it->max_descent += (height + 1) / 2;
10216 compute_line_metrics (it);
10218 /* If line is empty, make it occupy the rest of the tool-bar. */
10219 if (!row->displays_text_p)
10221 row->height = row->phys_height = it->last_visible_y - row->y;
10222 row->visible_height = row->height;
10223 row->ascent = row->phys_ascent = 0;
10224 row->extra_line_spacing = 0;
10227 row->full_width_p = 1;
10228 row->continued_p = 0;
10229 row->truncated_on_left_p = 0;
10230 row->truncated_on_right_p = 0;
10232 it->current_x = it->hpos = 0;
10233 it->current_y += row->height;
10234 ++it->vpos;
10235 ++it->glyph_row;
10239 /* Max tool-bar height. */
10241 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10242 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10244 /* Value is the number of screen lines needed to make all tool-bar
10245 items of frame F visible. The number of actual rows needed is
10246 returned in *N_ROWS if non-NULL. */
10248 static int
10249 tool_bar_lines_needed (f, n_rows)
10250 struct frame *f;
10251 int *n_rows;
10253 struct window *w = XWINDOW (f->tool_bar_window);
10254 struct it it;
10255 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10256 the desired matrix, so use (unused) mode-line row as temporary row to
10257 avoid destroying the first tool-bar row. */
10258 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10260 /* Initialize an iterator for iteration over
10261 F->desired_tool_bar_string in the tool-bar window of frame F. */
10262 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10263 it.first_visible_x = 0;
10264 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10265 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10267 while (!ITERATOR_AT_END_P (&it))
10269 clear_glyph_row (temp_row);
10270 it.glyph_row = temp_row;
10271 display_tool_bar_line (&it, -1);
10273 clear_glyph_row (temp_row);
10275 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10276 if (n_rows)
10277 *n_rows = it.vpos > 0 ? it.vpos : -1;
10279 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10283 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10284 0, 1, 0,
10285 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10286 (frame)
10287 Lisp_Object frame;
10289 struct frame *f;
10290 struct window *w;
10291 int nlines = 0;
10293 if (NILP (frame))
10294 frame = selected_frame;
10295 else
10296 CHECK_FRAME (frame);
10297 f = XFRAME (frame);
10299 if (WINDOWP (f->tool_bar_window)
10300 || (w = XWINDOW (f->tool_bar_window),
10301 WINDOW_TOTAL_LINES (w) > 0))
10303 update_tool_bar (f, 1);
10304 if (f->n_tool_bar_items)
10306 build_desired_tool_bar_string (f);
10307 nlines = tool_bar_lines_needed (f, NULL);
10311 return make_number (nlines);
10315 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10316 height should be changed. */
10318 static int
10319 redisplay_tool_bar (f)
10320 struct frame *f;
10322 struct window *w;
10323 struct it it;
10324 struct glyph_row *row;
10326 #if defined (USE_GTK) || defined (HAVE_NS)
10327 if (FRAME_EXTERNAL_TOOL_BAR (f))
10328 update_frame_tool_bar (f);
10329 return 0;
10330 #endif
10332 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10333 do anything. This means you must start with tool-bar-lines
10334 non-zero to get the auto-sizing effect. Or in other words, you
10335 can turn off tool-bars by specifying tool-bar-lines zero. */
10336 if (!WINDOWP (f->tool_bar_window)
10337 || (w = XWINDOW (f->tool_bar_window),
10338 WINDOW_TOTAL_LINES (w) == 0))
10339 return 0;
10341 /* Set up an iterator for the tool-bar window. */
10342 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10343 it.first_visible_x = 0;
10344 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10345 row = it.glyph_row;
10347 /* Build a string that represents the contents of the tool-bar. */
10348 build_desired_tool_bar_string (f);
10349 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10351 if (f->n_tool_bar_rows == 0)
10353 int nlines;
10355 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10356 nlines != WINDOW_TOTAL_LINES (w)))
10358 extern Lisp_Object Qtool_bar_lines;
10359 Lisp_Object frame;
10360 int old_height = WINDOW_TOTAL_LINES (w);
10362 XSETFRAME (frame, f);
10363 Fmodify_frame_parameters (frame,
10364 Fcons (Fcons (Qtool_bar_lines,
10365 make_number (nlines)),
10366 Qnil));
10367 if (WINDOW_TOTAL_LINES (w) != old_height)
10369 clear_glyph_matrix (w->desired_matrix);
10370 fonts_changed_p = 1;
10371 return 1;
10376 /* Display as many lines as needed to display all tool-bar items. */
10378 if (f->n_tool_bar_rows > 0)
10380 int border, rows, height, extra;
10382 if (INTEGERP (Vtool_bar_border))
10383 border = XINT (Vtool_bar_border);
10384 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10385 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10386 else if (EQ (Vtool_bar_border, Qborder_width))
10387 border = f->border_width;
10388 else
10389 border = 0;
10390 if (border < 0)
10391 border = 0;
10393 rows = f->n_tool_bar_rows;
10394 height = max (1, (it.last_visible_y - border) / rows);
10395 extra = it.last_visible_y - border - height * rows;
10397 while (it.current_y < it.last_visible_y)
10399 int h = 0;
10400 if (extra > 0 && rows-- > 0)
10402 h = (extra + rows - 1) / rows;
10403 extra -= h;
10405 display_tool_bar_line (&it, height + h);
10408 else
10410 while (it.current_y < it.last_visible_y)
10411 display_tool_bar_line (&it, 0);
10414 /* It doesn't make much sense to try scrolling in the tool-bar
10415 window, so don't do it. */
10416 w->desired_matrix->no_scrolling_p = 1;
10417 w->must_be_updated_p = 1;
10419 if (!NILP (Vauto_resize_tool_bars))
10421 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10422 int change_height_p = 0;
10424 /* If we couldn't display everything, change the tool-bar's
10425 height if there is room for more. */
10426 if (IT_STRING_CHARPOS (it) < it.end_charpos
10427 && it.current_y < max_tool_bar_height)
10428 change_height_p = 1;
10430 row = it.glyph_row - 1;
10432 /* If there are blank lines at the end, except for a partially
10433 visible blank line at the end that is smaller than
10434 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10435 if (!row->displays_text_p
10436 && row->height >= FRAME_LINE_HEIGHT (f))
10437 change_height_p = 1;
10439 /* If row displays tool-bar items, but is partially visible,
10440 change the tool-bar's height. */
10441 if (row->displays_text_p
10442 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10443 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10444 change_height_p = 1;
10446 /* Resize windows as needed by changing the `tool-bar-lines'
10447 frame parameter. */
10448 if (change_height_p)
10450 extern Lisp_Object Qtool_bar_lines;
10451 Lisp_Object frame;
10452 int old_height = WINDOW_TOTAL_LINES (w);
10453 int nrows;
10454 int nlines = tool_bar_lines_needed (f, &nrows);
10456 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10457 && !f->minimize_tool_bar_window_p)
10458 ? (nlines > old_height)
10459 : (nlines != old_height));
10460 f->minimize_tool_bar_window_p = 0;
10462 if (change_height_p)
10464 XSETFRAME (frame, f);
10465 Fmodify_frame_parameters (frame,
10466 Fcons (Fcons (Qtool_bar_lines,
10467 make_number (nlines)),
10468 Qnil));
10469 if (WINDOW_TOTAL_LINES (w) != old_height)
10471 clear_glyph_matrix (w->desired_matrix);
10472 f->n_tool_bar_rows = nrows;
10473 fonts_changed_p = 1;
10474 return 1;
10480 f->minimize_tool_bar_window_p = 0;
10481 return 0;
10485 /* Get information about the tool-bar item which is displayed in GLYPH
10486 on frame F. Return in *PROP_IDX the index where tool-bar item
10487 properties start in F->tool_bar_items. Value is zero if
10488 GLYPH doesn't display a tool-bar item. */
10490 static int
10491 tool_bar_item_info (f, glyph, prop_idx)
10492 struct frame *f;
10493 struct glyph *glyph;
10494 int *prop_idx;
10496 Lisp_Object prop;
10497 int success_p;
10498 int charpos;
10500 /* This function can be called asynchronously, which means we must
10501 exclude any possibility that Fget_text_property signals an
10502 error. */
10503 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10504 charpos = max (0, charpos);
10506 /* Get the text property `menu-item' at pos. The value of that
10507 property is the start index of this item's properties in
10508 F->tool_bar_items. */
10509 prop = Fget_text_property (make_number (charpos),
10510 Qmenu_item, f->current_tool_bar_string);
10511 if (INTEGERP (prop))
10513 *prop_idx = XINT (prop);
10514 success_p = 1;
10516 else
10517 success_p = 0;
10519 return success_p;
10523 /* Get information about the tool-bar item at position X/Y on frame F.
10524 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10525 the current matrix of the tool-bar window of F, or NULL if not
10526 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10527 item in F->tool_bar_items. Value is
10529 -1 if X/Y is not on a tool-bar item
10530 0 if X/Y is on the same item that was highlighted before.
10531 1 otherwise. */
10533 static int
10534 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10535 struct frame *f;
10536 int x, y;
10537 struct glyph **glyph;
10538 int *hpos, *vpos, *prop_idx;
10540 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10541 struct window *w = XWINDOW (f->tool_bar_window);
10542 int area;
10544 /* Find the glyph under X/Y. */
10545 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10546 if (*glyph == NULL)
10547 return -1;
10549 /* Get the start of this tool-bar item's properties in
10550 f->tool_bar_items. */
10551 if (!tool_bar_item_info (f, *glyph, prop_idx))
10552 return -1;
10554 /* Is mouse on the highlighted item? */
10555 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10556 && *vpos >= dpyinfo->mouse_face_beg_row
10557 && *vpos <= dpyinfo->mouse_face_end_row
10558 && (*vpos > dpyinfo->mouse_face_beg_row
10559 || *hpos >= dpyinfo->mouse_face_beg_col)
10560 && (*vpos < dpyinfo->mouse_face_end_row
10561 || *hpos < dpyinfo->mouse_face_end_col
10562 || dpyinfo->mouse_face_past_end))
10563 return 0;
10565 return 1;
10569 /* EXPORT:
10570 Handle mouse button event on the tool-bar of frame F, at
10571 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10572 0 for button release. MODIFIERS is event modifiers for button
10573 release. */
10575 void
10576 handle_tool_bar_click (f, x, y, down_p, modifiers)
10577 struct frame *f;
10578 int x, y, down_p;
10579 unsigned int modifiers;
10581 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10582 struct window *w = XWINDOW (f->tool_bar_window);
10583 int hpos, vpos, prop_idx;
10584 struct glyph *glyph;
10585 Lisp_Object enabled_p;
10587 /* If not on the highlighted tool-bar item, return. */
10588 frame_to_window_pixel_xy (w, &x, &y);
10589 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10590 return;
10592 /* If item is disabled, do nothing. */
10593 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10594 if (NILP (enabled_p))
10595 return;
10597 if (down_p)
10599 /* Show item in pressed state. */
10600 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10601 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10602 last_tool_bar_item = prop_idx;
10604 else
10606 Lisp_Object key, frame;
10607 struct input_event event;
10608 EVENT_INIT (event);
10610 /* Show item in released state. */
10611 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10612 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10614 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10616 XSETFRAME (frame, f);
10617 event.kind = TOOL_BAR_EVENT;
10618 event.frame_or_window = frame;
10619 event.arg = frame;
10620 kbd_buffer_store_event (&event);
10622 event.kind = TOOL_BAR_EVENT;
10623 event.frame_or_window = frame;
10624 event.arg = key;
10625 event.modifiers = modifiers;
10626 kbd_buffer_store_event (&event);
10627 last_tool_bar_item = -1;
10632 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10633 tool-bar window-relative coordinates X/Y. Called from
10634 note_mouse_highlight. */
10636 static void
10637 note_tool_bar_highlight (f, x, y)
10638 struct frame *f;
10639 int x, y;
10641 Lisp_Object window = f->tool_bar_window;
10642 struct window *w = XWINDOW (window);
10643 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10644 int hpos, vpos;
10645 struct glyph *glyph;
10646 struct glyph_row *row;
10647 int i;
10648 Lisp_Object enabled_p;
10649 int prop_idx;
10650 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10651 int mouse_down_p, rc;
10653 /* Function note_mouse_highlight is called with negative x(y
10654 values when mouse moves outside of the frame. */
10655 if (x <= 0 || y <= 0)
10657 clear_mouse_face (dpyinfo);
10658 return;
10661 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10662 if (rc < 0)
10664 /* Not on tool-bar item. */
10665 clear_mouse_face (dpyinfo);
10666 return;
10668 else if (rc == 0)
10669 /* On same tool-bar item as before. */
10670 goto set_help_echo;
10672 clear_mouse_face (dpyinfo);
10674 /* Mouse is down, but on different tool-bar item? */
10675 mouse_down_p = (dpyinfo->grabbed
10676 && f == last_mouse_frame
10677 && FRAME_LIVE_P (f));
10678 if (mouse_down_p
10679 && last_tool_bar_item != prop_idx)
10680 return;
10682 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10683 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10685 /* If tool-bar item is not enabled, don't highlight it. */
10686 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10687 if (!NILP (enabled_p))
10689 /* Compute the x-position of the glyph. In front and past the
10690 image is a space. We include this in the highlighted area. */
10691 row = MATRIX_ROW (w->current_matrix, vpos);
10692 for (i = x = 0; i < hpos; ++i)
10693 x += row->glyphs[TEXT_AREA][i].pixel_width;
10695 /* Record this as the current active region. */
10696 dpyinfo->mouse_face_beg_col = hpos;
10697 dpyinfo->mouse_face_beg_row = vpos;
10698 dpyinfo->mouse_face_beg_x = x;
10699 dpyinfo->mouse_face_beg_y = row->y;
10700 dpyinfo->mouse_face_past_end = 0;
10702 dpyinfo->mouse_face_end_col = hpos + 1;
10703 dpyinfo->mouse_face_end_row = vpos;
10704 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10705 dpyinfo->mouse_face_end_y = row->y;
10706 dpyinfo->mouse_face_window = window;
10707 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10709 /* Display it as active. */
10710 show_mouse_face (dpyinfo, draw);
10711 dpyinfo->mouse_face_image_state = draw;
10714 set_help_echo:
10716 /* Set help_echo_string to a help string to display for this tool-bar item.
10717 XTread_socket does the rest. */
10718 help_echo_object = help_echo_window = Qnil;
10719 help_echo_pos = -1;
10720 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10721 if (NILP (help_echo_string))
10722 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10725 #endif /* HAVE_WINDOW_SYSTEM */
10729 /************************************************************************
10730 Horizontal scrolling
10731 ************************************************************************/
10733 static int hscroll_window_tree P_ ((Lisp_Object));
10734 static int hscroll_windows P_ ((Lisp_Object));
10736 /* For all leaf windows in the window tree rooted at WINDOW, set their
10737 hscroll value so that PT is (i) visible in the window, and (ii) so
10738 that it is not within a certain margin at the window's left and
10739 right border. Value is non-zero if any window's hscroll has been
10740 changed. */
10742 static int
10743 hscroll_window_tree (window)
10744 Lisp_Object window;
10746 int hscrolled_p = 0;
10747 int hscroll_relative_p = FLOATP (Vhscroll_step);
10748 int hscroll_step_abs = 0;
10749 double hscroll_step_rel = 0;
10751 if (hscroll_relative_p)
10753 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10754 if (hscroll_step_rel < 0)
10756 hscroll_relative_p = 0;
10757 hscroll_step_abs = 0;
10760 else if (INTEGERP (Vhscroll_step))
10762 hscroll_step_abs = XINT (Vhscroll_step);
10763 if (hscroll_step_abs < 0)
10764 hscroll_step_abs = 0;
10766 else
10767 hscroll_step_abs = 0;
10769 while (WINDOWP (window))
10771 struct window *w = XWINDOW (window);
10773 if (WINDOWP (w->hchild))
10774 hscrolled_p |= hscroll_window_tree (w->hchild);
10775 else if (WINDOWP (w->vchild))
10776 hscrolled_p |= hscroll_window_tree (w->vchild);
10777 else if (w->cursor.vpos >= 0)
10779 int h_margin;
10780 int text_area_width;
10781 struct glyph_row *current_cursor_row
10782 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10783 struct glyph_row *desired_cursor_row
10784 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10785 struct glyph_row *cursor_row
10786 = (desired_cursor_row->enabled_p
10787 ? desired_cursor_row
10788 : current_cursor_row);
10790 text_area_width = window_box_width (w, TEXT_AREA);
10792 /* Scroll when cursor is inside this scroll margin. */
10793 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10795 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10796 && ((XFASTINT (w->hscroll)
10797 && w->cursor.x <= h_margin)
10798 || (cursor_row->enabled_p
10799 && cursor_row->truncated_on_right_p
10800 && (w->cursor.x >= text_area_width - h_margin))))
10802 struct it it;
10803 int hscroll;
10804 struct buffer *saved_current_buffer;
10805 int pt;
10806 int wanted_x;
10808 /* Find point in a display of infinite width. */
10809 saved_current_buffer = current_buffer;
10810 current_buffer = XBUFFER (w->buffer);
10812 if (w == XWINDOW (selected_window))
10813 pt = BUF_PT (current_buffer);
10814 else
10816 pt = marker_position (w->pointm);
10817 pt = max (BEGV, pt);
10818 pt = min (ZV, pt);
10821 /* Move iterator to pt starting at cursor_row->start in
10822 a line with infinite width. */
10823 init_to_row_start (&it, w, cursor_row);
10824 it.last_visible_x = INFINITY;
10825 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10826 current_buffer = saved_current_buffer;
10828 /* Position cursor in window. */
10829 if (!hscroll_relative_p && hscroll_step_abs == 0)
10830 hscroll = max (0, (it.current_x
10831 - (ITERATOR_AT_END_OF_LINE_P (&it)
10832 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10833 : (text_area_width / 2))))
10834 / FRAME_COLUMN_WIDTH (it.f);
10835 else if (w->cursor.x >= text_area_width - h_margin)
10837 if (hscroll_relative_p)
10838 wanted_x = text_area_width * (1 - hscroll_step_rel)
10839 - h_margin;
10840 else
10841 wanted_x = text_area_width
10842 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10843 - h_margin;
10844 hscroll
10845 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10847 else
10849 if (hscroll_relative_p)
10850 wanted_x = text_area_width * hscroll_step_rel
10851 + h_margin;
10852 else
10853 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10854 + h_margin;
10855 hscroll
10856 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10858 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10860 /* Don't call Fset_window_hscroll if value hasn't
10861 changed because it will prevent redisplay
10862 optimizations. */
10863 if (XFASTINT (w->hscroll) != hscroll)
10865 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10866 w->hscroll = make_number (hscroll);
10867 hscrolled_p = 1;
10872 window = w->next;
10875 /* Value is non-zero if hscroll of any leaf window has been changed. */
10876 return hscrolled_p;
10880 /* Set hscroll so that cursor is visible and not inside horizontal
10881 scroll margins for all windows in the tree rooted at WINDOW. See
10882 also hscroll_window_tree above. Value is non-zero if any window's
10883 hscroll has been changed. If it has, desired matrices on the frame
10884 of WINDOW are cleared. */
10886 static int
10887 hscroll_windows (window)
10888 Lisp_Object window;
10890 int hscrolled_p = hscroll_window_tree (window);
10891 if (hscrolled_p)
10892 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10893 return hscrolled_p;
10898 /************************************************************************
10899 Redisplay
10900 ************************************************************************/
10902 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10903 to a non-zero value. This is sometimes handy to have in a debugger
10904 session. */
10906 #if GLYPH_DEBUG
10908 /* First and last unchanged row for try_window_id. */
10910 int debug_first_unchanged_at_end_vpos;
10911 int debug_last_unchanged_at_beg_vpos;
10913 /* Delta vpos and y. */
10915 int debug_dvpos, debug_dy;
10917 /* Delta in characters and bytes for try_window_id. */
10919 int debug_delta, debug_delta_bytes;
10921 /* Values of window_end_pos and window_end_vpos at the end of
10922 try_window_id. */
10924 EMACS_INT debug_end_pos, debug_end_vpos;
10926 /* Append a string to W->desired_matrix->method. FMT is a printf
10927 format string. A1...A9 are a supplement for a variable-length
10928 argument list. If trace_redisplay_p is non-zero also printf the
10929 resulting string to stderr. */
10931 static void
10932 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10933 struct window *w;
10934 char *fmt;
10935 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10937 char buffer[512];
10938 char *method = w->desired_matrix->method;
10939 int len = strlen (method);
10940 int size = sizeof w->desired_matrix->method;
10941 int remaining = size - len - 1;
10943 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10944 if (len && remaining)
10946 method[len] = '|';
10947 --remaining, ++len;
10950 strncpy (method + len, buffer, remaining);
10952 if (trace_redisplay_p)
10953 fprintf (stderr, "%p (%s): %s\n",
10955 ((BUFFERP (w->buffer)
10956 && STRINGP (XBUFFER (w->buffer)->name))
10957 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10958 : "no buffer"),
10959 buffer);
10962 #endif /* GLYPH_DEBUG */
10965 /* Value is non-zero if all changes in window W, which displays
10966 current_buffer, are in the text between START and END. START is a
10967 buffer position, END is given as a distance from Z. Used in
10968 redisplay_internal for display optimization. */
10970 static INLINE int
10971 text_outside_line_unchanged_p (w, start, end)
10972 struct window *w;
10973 int start, end;
10975 int unchanged_p = 1;
10977 /* If text or overlays have changed, see where. */
10978 if (XFASTINT (w->last_modified) < MODIFF
10979 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10981 /* Gap in the line? */
10982 if (GPT < start || Z - GPT < end)
10983 unchanged_p = 0;
10985 /* Changes start in front of the line, or end after it? */
10986 if (unchanged_p
10987 && (BEG_UNCHANGED < start - 1
10988 || END_UNCHANGED < end))
10989 unchanged_p = 0;
10991 /* If selective display, can't optimize if changes start at the
10992 beginning of the line. */
10993 if (unchanged_p
10994 && INTEGERP (current_buffer->selective_display)
10995 && XINT (current_buffer->selective_display) > 0
10996 && (BEG_UNCHANGED < start || GPT <= start))
10997 unchanged_p = 0;
10999 /* If there are overlays at the start or end of the line, these
11000 may have overlay strings with newlines in them. A change at
11001 START, for instance, may actually concern the display of such
11002 overlay strings as well, and they are displayed on different
11003 lines. So, quickly rule out this case. (For the future, it
11004 might be desirable to implement something more telling than
11005 just BEG/END_UNCHANGED.) */
11006 if (unchanged_p)
11008 if (BEG + BEG_UNCHANGED == start
11009 && overlay_touches_p (start))
11010 unchanged_p = 0;
11011 if (END_UNCHANGED == end
11012 && overlay_touches_p (Z - end))
11013 unchanged_p = 0;
11017 return unchanged_p;
11021 /* Do a frame update, taking possible shortcuts into account. This is
11022 the main external entry point for redisplay.
11024 If the last redisplay displayed an echo area message and that message
11025 is no longer requested, we clear the echo area or bring back the
11026 mini-buffer if that is in use. */
11028 void
11029 redisplay ()
11031 redisplay_internal (0);
11035 static Lisp_Object
11036 overlay_arrow_string_or_property (var)
11037 Lisp_Object var;
11039 Lisp_Object val;
11041 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11042 return val;
11044 return Voverlay_arrow_string;
11047 /* Return 1 if there are any overlay-arrows in current_buffer. */
11048 static int
11049 overlay_arrow_in_current_buffer_p ()
11051 Lisp_Object vlist;
11053 for (vlist = Voverlay_arrow_variable_list;
11054 CONSP (vlist);
11055 vlist = XCDR (vlist))
11057 Lisp_Object var = XCAR (vlist);
11058 Lisp_Object val;
11060 if (!SYMBOLP (var))
11061 continue;
11062 val = find_symbol_value (var);
11063 if (MARKERP (val)
11064 && current_buffer == XMARKER (val)->buffer)
11065 return 1;
11067 return 0;
11071 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11072 has changed. */
11074 static int
11075 overlay_arrows_changed_p ()
11077 Lisp_Object vlist;
11079 for (vlist = Voverlay_arrow_variable_list;
11080 CONSP (vlist);
11081 vlist = XCDR (vlist))
11083 Lisp_Object var = XCAR (vlist);
11084 Lisp_Object val, pstr;
11086 if (!SYMBOLP (var))
11087 continue;
11088 val = find_symbol_value (var);
11089 if (!MARKERP (val))
11090 continue;
11091 if (! EQ (COERCE_MARKER (val),
11092 Fget (var, Qlast_arrow_position))
11093 || ! (pstr = overlay_arrow_string_or_property (var),
11094 EQ (pstr, Fget (var, Qlast_arrow_string))))
11095 return 1;
11097 return 0;
11100 /* Mark overlay arrows to be updated on next redisplay. */
11102 static void
11103 update_overlay_arrows (up_to_date)
11104 int up_to_date;
11106 Lisp_Object vlist;
11108 for (vlist = Voverlay_arrow_variable_list;
11109 CONSP (vlist);
11110 vlist = XCDR (vlist))
11112 Lisp_Object var = XCAR (vlist);
11114 if (!SYMBOLP (var))
11115 continue;
11117 if (up_to_date > 0)
11119 Lisp_Object val = find_symbol_value (var);
11120 Fput (var, Qlast_arrow_position,
11121 COERCE_MARKER (val));
11122 Fput (var, Qlast_arrow_string,
11123 overlay_arrow_string_or_property (var));
11125 else if (up_to_date < 0
11126 || !NILP (Fget (var, Qlast_arrow_position)))
11128 Fput (var, Qlast_arrow_position, Qt);
11129 Fput (var, Qlast_arrow_string, Qt);
11135 /* Return overlay arrow string to display at row.
11136 Return integer (bitmap number) for arrow bitmap in left fringe.
11137 Return nil if no overlay arrow. */
11139 static Lisp_Object
11140 overlay_arrow_at_row (it, row)
11141 struct it *it;
11142 struct glyph_row *row;
11144 Lisp_Object vlist;
11146 for (vlist = Voverlay_arrow_variable_list;
11147 CONSP (vlist);
11148 vlist = XCDR (vlist))
11150 Lisp_Object var = XCAR (vlist);
11151 Lisp_Object val;
11153 if (!SYMBOLP (var))
11154 continue;
11156 val = find_symbol_value (var);
11158 if (MARKERP (val)
11159 && current_buffer == XMARKER (val)->buffer
11160 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11162 if (FRAME_WINDOW_P (it->f)
11163 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11165 #ifdef HAVE_WINDOW_SYSTEM
11166 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11168 int fringe_bitmap;
11169 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11170 return make_number (fringe_bitmap);
11172 #endif
11173 return make_number (-1); /* Use default arrow bitmap */
11175 return overlay_arrow_string_or_property (var);
11179 return Qnil;
11182 /* Return 1 if point moved out of or into a composition. Otherwise
11183 return 0. PREV_BUF and PREV_PT are the last point buffer and
11184 position. BUF and PT are the current point buffer and position. */
11187 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11188 struct buffer *prev_buf, *buf;
11189 int prev_pt, pt;
11191 EMACS_INT start, end;
11192 Lisp_Object prop;
11193 Lisp_Object buffer;
11195 XSETBUFFER (buffer, buf);
11196 /* Check a composition at the last point if point moved within the
11197 same buffer. */
11198 if (prev_buf == buf)
11200 if (prev_pt == pt)
11201 /* Point didn't move. */
11202 return 0;
11204 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11205 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11206 && COMPOSITION_VALID_P (start, end, prop)
11207 && start < prev_pt && end > prev_pt)
11208 /* The last point was within the composition. Return 1 iff
11209 point moved out of the composition. */
11210 return (pt <= start || pt >= end);
11213 /* Check a composition at the current point. */
11214 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11215 && find_composition (pt, -1, &start, &end, &prop, buffer)
11216 && COMPOSITION_VALID_P (start, end, prop)
11217 && start < pt && end > pt);
11221 /* Reconsider the setting of B->clip_changed which is displayed
11222 in window W. */
11224 static INLINE void
11225 reconsider_clip_changes (w, b)
11226 struct window *w;
11227 struct buffer *b;
11229 if (b->clip_changed
11230 && !NILP (w->window_end_valid)
11231 && w->current_matrix->buffer == b
11232 && w->current_matrix->zv == BUF_ZV (b)
11233 && w->current_matrix->begv == BUF_BEGV (b))
11234 b->clip_changed = 0;
11236 /* If display wasn't paused, and W is not a tool bar window, see if
11237 point has been moved into or out of a composition. In that case,
11238 we set b->clip_changed to 1 to force updating the screen. If
11239 b->clip_changed has already been set to 1, we can skip this
11240 check. */
11241 if (!b->clip_changed
11242 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11244 int pt;
11246 if (w == XWINDOW (selected_window))
11247 pt = BUF_PT (current_buffer);
11248 else
11249 pt = marker_position (w->pointm);
11251 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11252 || pt != XINT (w->last_point))
11253 && check_point_in_composition (w->current_matrix->buffer,
11254 XINT (w->last_point),
11255 XBUFFER (w->buffer), pt))
11256 b->clip_changed = 1;
11261 /* Select FRAME to forward the values of frame-local variables into C
11262 variables so that the redisplay routines can access those values
11263 directly. */
11265 static void
11266 select_frame_for_redisplay (frame)
11267 Lisp_Object frame;
11269 Lisp_Object tail, symbol, val;
11270 Lisp_Object old = selected_frame;
11271 struct Lisp_Symbol *sym;
11273 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11275 selected_frame = frame;
11279 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11280 if (CONSP (XCAR (tail))
11281 && (symbol = XCAR (XCAR (tail)),
11282 SYMBOLP (symbol))
11283 && (sym = indirect_variable (XSYMBOL (symbol)),
11284 val = sym->value,
11285 (BUFFER_LOCAL_VALUEP (val)))
11286 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11287 /* Use find_symbol_value rather than Fsymbol_value
11288 to avoid an error if it is void. */
11289 find_symbol_value (symbol);
11290 } while (!EQ (frame, old) && (frame = old, 1));
11294 #define STOP_POLLING \
11295 do { if (! polling_stopped_here) stop_polling (); \
11296 polling_stopped_here = 1; } while (0)
11298 #define RESUME_POLLING \
11299 do { if (polling_stopped_here) start_polling (); \
11300 polling_stopped_here = 0; } while (0)
11303 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11304 response to any user action; therefore, we should preserve the echo
11305 area. (Actually, our caller does that job.) Perhaps in the future
11306 avoid recentering windows if it is not necessary; currently that
11307 causes some problems. */
11309 static void
11310 redisplay_internal (preserve_echo_area)
11311 int preserve_echo_area;
11313 struct window *w = XWINDOW (selected_window);
11314 struct frame *f;
11315 int pause;
11316 int must_finish = 0;
11317 struct text_pos tlbufpos, tlendpos;
11318 int number_of_visible_frames;
11319 int count, count1;
11320 struct frame *sf;
11321 int polling_stopped_here = 0;
11322 Lisp_Object old_frame = selected_frame;
11324 /* Non-zero means redisplay has to consider all windows on all
11325 frames. Zero means, only selected_window is considered. */
11326 int consider_all_windows_p;
11328 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11330 /* No redisplay if running in batch mode or frame is not yet fully
11331 initialized, or redisplay is explicitly turned off by setting
11332 Vinhibit_redisplay. */
11333 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11334 || !NILP (Vinhibit_redisplay))
11335 return;
11337 /* Don't examine these until after testing Vinhibit_redisplay.
11338 When Emacs is shutting down, perhaps because its connection to
11339 X has dropped, we should not look at them at all. */
11340 f = XFRAME (w->frame);
11341 sf = SELECTED_FRAME ();
11343 if (!f->glyphs_initialized_p)
11344 return;
11346 /* The flag redisplay_performed_directly_p is set by
11347 direct_output_for_insert when it already did the whole screen
11348 update necessary. */
11349 if (redisplay_performed_directly_p)
11351 redisplay_performed_directly_p = 0;
11352 if (!hscroll_windows (selected_window))
11353 return;
11356 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11357 if (popup_activated ())
11358 return;
11359 #endif
11361 /* I don't think this happens but let's be paranoid. */
11362 if (redisplaying_p)
11363 return;
11365 /* Record a function that resets redisplaying_p to its old value
11366 when we leave this function. */
11367 count = SPECPDL_INDEX ();
11368 record_unwind_protect (unwind_redisplay,
11369 Fcons (make_number (redisplaying_p), selected_frame));
11370 ++redisplaying_p;
11371 specbind (Qinhibit_free_realized_faces, Qnil);
11374 Lisp_Object tail, frame;
11376 FOR_EACH_FRAME (tail, frame)
11378 struct frame *f = XFRAME (frame);
11379 f->already_hscrolled_p = 0;
11383 retry:
11384 if (!EQ (old_frame, selected_frame)
11385 && FRAME_LIVE_P (XFRAME (old_frame)))
11386 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11387 selected_frame and selected_window to be temporarily out-of-sync so
11388 when we come back here via `goto retry', we need to resync because we
11389 may need to run Elisp code (via prepare_menu_bars). */
11390 select_frame_for_redisplay (old_frame);
11392 pause = 0;
11393 reconsider_clip_changes (w, current_buffer);
11394 last_escape_glyph_frame = NULL;
11395 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11397 /* If new fonts have been loaded that make a glyph matrix adjustment
11398 necessary, do it. */
11399 if (fonts_changed_p)
11401 adjust_glyphs (NULL);
11402 ++windows_or_buffers_changed;
11403 fonts_changed_p = 0;
11406 /* If face_change_count is non-zero, init_iterator will free all
11407 realized faces, which includes the faces referenced from current
11408 matrices. So, we can't reuse current matrices in this case. */
11409 if (face_change_count)
11410 ++windows_or_buffers_changed;
11412 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11413 && FRAME_TTY (sf)->previous_frame != sf)
11415 /* Since frames on a single ASCII terminal share the same
11416 display area, displaying a different frame means redisplay
11417 the whole thing. */
11418 windows_or_buffers_changed++;
11419 SET_FRAME_GARBAGED (sf);
11420 #ifndef DOS_NT
11421 set_tty_color_mode (FRAME_TTY (sf), sf);
11422 #endif
11423 FRAME_TTY (sf)->previous_frame = sf;
11426 /* Set the visible flags for all frames. Do this before checking
11427 for resized or garbaged frames; they want to know if their frames
11428 are visible. See the comment in frame.h for
11429 FRAME_SAMPLE_VISIBILITY. */
11431 Lisp_Object tail, frame;
11433 number_of_visible_frames = 0;
11435 FOR_EACH_FRAME (tail, frame)
11437 struct frame *f = XFRAME (frame);
11439 FRAME_SAMPLE_VISIBILITY (f);
11440 if (FRAME_VISIBLE_P (f))
11441 ++number_of_visible_frames;
11442 clear_desired_matrices (f);
11446 /* Notice any pending interrupt request to change frame size. */
11447 do_pending_window_change (1);
11449 /* Clear frames marked as garbaged. */
11450 if (frame_garbaged)
11451 clear_garbaged_frames ();
11453 /* Build menubar and tool-bar items. */
11454 if (NILP (Vmemory_full))
11455 prepare_menu_bars ();
11457 if (windows_or_buffers_changed)
11458 update_mode_lines++;
11460 /* Detect case that we need to write or remove a star in the mode line. */
11461 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11463 w->update_mode_line = Qt;
11464 if (buffer_shared > 1)
11465 update_mode_lines++;
11468 /* Avoid invocation of point motion hooks by `current_column' below. */
11469 count1 = SPECPDL_INDEX ();
11470 specbind (Qinhibit_point_motion_hooks, Qt);
11472 /* If %c is in the mode line, update it if needed. */
11473 if (!NILP (w->column_number_displayed)
11474 /* This alternative quickly identifies a common case
11475 where no change is needed. */
11476 && !(PT == XFASTINT (w->last_point)
11477 && XFASTINT (w->last_modified) >= MODIFF
11478 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11479 && (XFASTINT (w->column_number_displayed)
11480 != (int) current_column ())) /* iftc */
11481 w->update_mode_line = Qt;
11483 unbind_to (count1, Qnil);
11485 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11487 /* The variable buffer_shared is set in redisplay_window and
11488 indicates that we redisplay a buffer in different windows. See
11489 there. */
11490 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11491 || cursor_type_changed);
11493 /* If specs for an arrow have changed, do thorough redisplay
11494 to ensure we remove any arrow that should no longer exist. */
11495 if (overlay_arrows_changed_p ())
11496 consider_all_windows_p = windows_or_buffers_changed = 1;
11498 /* Normally the message* functions will have already displayed and
11499 updated the echo area, but the frame may have been trashed, or
11500 the update may have been preempted, so display the echo area
11501 again here. Checking message_cleared_p captures the case that
11502 the echo area should be cleared. */
11503 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11504 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11505 || (message_cleared_p
11506 && minibuf_level == 0
11507 /* If the mini-window is currently selected, this means the
11508 echo-area doesn't show through. */
11509 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11511 int window_height_changed_p = echo_area_display (0);
11512 must_finish = 1;
11514 /* If we don't display the current message, don't clear the
11515 message_cleared_p flag, because, if we did, we wouldn't clear
11516 the echo area in the next redisplay which doesn't preserve
11517 the echo area. */
11518 if (!display_last_displayed_message_p)
11519 message_cleared_p = 0;
11521 if (fonts_changed_p)
11522 goto retry;
11523 else if (window_height_changed_p)
11525 consider_all_windows_p = 1;
11526 ++update_mode_lines;
11527 ++windows_or_buffers_changed;
11529 /* If window configuration was changed, frames may have been
11530 marked garbaged. Clear them or we will experience
11531 surprises wrt scrolling. */
11532 if (frame_garbaged)
11533 clear_garbaged_frames ();
11536 else if (EQ (selected_window, minibuf_window)
11537 && (current_buffer->clip_changed
11538 || XFASTINT (w->last_modified) < MODIFF
11539 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11540 && resize_mini_window (w, 0))
11542 /* Resized active mini-window to fit the size of what it is
11543 showing if its contents might have changed. */
11544 must_finish = 1;
11545 /* FIXME: this causes all frames to be updated, which seems unnecessary
11546 since only the current frame needs to be considered. This function needs
11547 to be rewritten with two variables, consider_all_windows and
11548 consider_all_frames. */
11549 consider_all_windows_p = 1;
11550 ++windows_or_buffers_changed;
11551 ++update_mode_lines;
11553 /* If window configuration was changed, frames may have been
11554 marked garbaged. Clear them or we will experience
11555 surprises wrt scrolling. */
11556 if (frame_garbaged)
11557 clear_garbaged_frames ();
11561 /* If showing the region, and mark has changed, we must redisplay
11562 the whole window. The assignment to this_line_start_pos prevents
11563 the optimization directly below this if-statement. */
11564 if (((!NILP (Vtransient_mark_mode)
11565 && !NILP (XBUFFER (w->buffer)->mark_active))
11566 != !NILP (w->region_showing))
11567 || (!NILP (w->region_showing)
11568 && !EQ (w->region_showing,
11569 Fmarker_position (XBUFFER (w->buffer)->mark))))
11570 CHARPOS (this_line_start_pos) = 0;
11572 /* Optimize the case that only the line containing the cursor in the
11573 selected window has changed. Variables starting with this_ are
11574 set in display_line and record information about the line
11575 containing the cursor. */
11576 tlbufpos = this_line_start_pos;
11577 tlendpos = this_line_end_pos;
11578 if (!consider_all_windows_p
11579 && CHARPOS (tlbufpos) > 0
11580 && NILP (w->update_mode_line)
11581 && !current_buffer->clip_changed
11582 && !current_buffer->prevent_redisplay_optimizations_p
11583 && FRAME_VISIBLE_P (XFRAME (w->frame))
11584 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11585 /* Make sure recorded data applies to current buffer, etc. */
11586 && this_line_buffer == current_buffer
11587 && current_buffer == XBUFFER (w->buffer)
11588 && NILP (w->force_start)
11589 && NILP (w->optional_new_start)
11590 /* Point must be on the line that we have info recorded about. */
11591 && PT >= CHARPOS (tlbufpos)
11592 && PT <= Z - CHARPOS (tlendpos)
11593 /* All text outside that line, including its final newline,
11594 must be unchanged */
11595 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11596 CHARPOS (tlendpos)))
11598 if (CHARPOS (tlbufpos) > BEGV
11599 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11600 && (CHARPOS (tlbufpos) == ZV
11601 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11602 /* Former continuation line has disappeared by becoming empty */
11603 goto cancel;
11604 else if (XFASTINT (w->last_modified) < MODIFF
11605 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11606 || MINI_WINDOW_P (w))
11608 /* We have to handle the case of continuation around a
11609 wide-column character (See the comment in indent.c around
11610 line 885).
11612 For instance, in the following case:
11614 -------- Insert --------
11615 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11616 J_I_ ==> J_I_ `^^' are cursors.
11617 ^^ ^^
11618 -------- --------
11620 As we have to redraw the line above, we should goto cancel. */
11622 struct it it;
11623 int line_height_before = this_line_pixel_height;
11625 /* Note that start_display will handle the case that the
11626 line starting at tlbufpos is a continuation lines. */
11627 start_display (&it, w, tlbufpos);
11629 /* Implementation note: It this still necessary? */
11630 if (it.current_x != this_line_start_x)
11631 goto cancel;
11633 TRACE ((stderr, "trying display optimization 1\n"));
11634 w->cursor.vpos = -1;
11635 overlay_arrow_seen = 0;
11636 it.vpos = this_line_vpos;
11637 it.current_y = this_line_y;
11638 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11639 display_line (&it);
11641 /* If line contains point, is not continued,
11642 and ends at same distance from eob as before, we win */
11643 if (w->cursor.vpos >= 0
11644 /* Line is not continued, otherwise this_line_start_pos
11645 would have been set to 0 in display_line. */
11646 && CHARPOS (this_line_start_pos)
11647 /* Line ends as before. */
11648 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11649 /* Line has same height as before. Otherwise other lines
11650 would have to be shifted up or down. */
11651 && this_line_pixel_height == line_height_before)
11653 /* If this is not the window's last line, we must adjust
11654 the charstarts of the lines below. */
11655 if (it.current_y < it.last_visible_y)
11657 struct glyph_row *row
11658 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11659 int delta, delta_bytes;
11661 if (Z - CHARPOS (tlendpos) == ZV)
11663 /* This line ends at end of (accessible part of)
11664 buffer. There is no newline to count. */
11665 delta = (Z
11666 - CHARPOS (tlendpos)
11667 - MATRIX_ROW_START_CHARPOS (row));
11668 delta_bytes = (Z_BYTE
11669 - BYTEPOS (tlendpos)
11670 - MATRIX_ROW_START_BYTEPOS (row));
11672 else
11674 /* This line ends in a newline. Must take
11675 account of the newline and the rest of the
11676 text that follows. */
11677 delta = (Z
11678 - CHARPOS (tlendpos)
11679 - MATRIX_ROW_START_CHARPOS (row));
11680 delta_bytes = (Z_BYTE
11681 - BYTEPOS (tlendpos)
11682 - MATRIX_ROW_START_BYTEPOS (row));
11685 increment_matrix_positions (w->current_matrix,
11686 this_line_vpos + 1,
11687 w->current_matrix->nrows,
11688 delta, delta_bytes);
11691 /* If this row displays text now but previously didn't,
11692 or vice versa, w->window_end_vpos may have to be
11693 adjusted. */
11694 if ((it.glyph_row - 1)->displays_text_p)
11696 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11697 XSETINT (w->window_end_vpos, this_line_vpos);
11699 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11700 && this_line_vpos > 0)
11701 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11702 w->window_end_valid = Qnil;
11704 /* Update hint: No need to try to scroll in update_window. */
11705 w->desired_matrix->no_scrolling_p = 1;
11707 #if GLYPH_DEBUG
11708 *w->desired_matrix->method = 0;
11709 debug_method_add (w, "optimization 1");
11710 #endif
11711 #ifdef HAVE_WINDOW_SYSTEM
11712 update_window_fringes (w, 0);
11713 #endif
11714 goto update;
11716 else
11717 goto cancel;
11719 else if (/* Cursor position hasn't changed. */
11720 PT == XFASTINT (w->last_point)
11721 /* Make sure the cursor was last displayed
11722 in this window. Otherwise we have to reposition it. */
11723 && 0 <= w->cursor.vpos
11724 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11726 if (!must_finish)
11728 do_pending_window_change (1);
11730 /* We used to always goto end_of_redisplay here, but this
11731 isn't enough if we have a blinking cursor. */
11732 if (w->cursor_off_p == w->last_cursor_off_p)
11733 goto end_of_redisplay;
11735 goto update;
11737 /* If highlighting the region, or if the cursor is in the echo area,
11738 then we can't just move the cursor. */
11739 else if (! (!NILP (Vtransient_mark_mode)
11740 && !NILP (current_buffer->mark_active))
11741 && (EQ (selected_window, current_buffer->last_selected_window)
11742 || highlight_nonselected_windows)
11743 && NILP (w->region_showing)
11744 && NILP (Vshow_trailing_whitespace)
11745 && !cursor_in_echo_area)
11747 struct it it;
11748 struct glyph_row *row;
11750 /* Skip from tlbufpos to PT and see where it is. Note that
11751 PT may be in invisible text. If so, we will end at the
11752 next visible position. */
11753 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11754 NULL, DEFAULT_FACE_ID);
11755 it.current_x = this_line_start_x;
11756 it.current_y = this_line_y;
11757 it.vpos = this_line_vpos;
11759 /* The call to move_it_to stops in front of PT, but
11760 moves over before-strings. */
11761 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11763 if (it.vpos == this_line_vpos
11764 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11765 row->enabled_p))
11767 xassert (this_line_vpos == it.vpos);
11768 xassert (this_line_y == it.current_y);
11769 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11770 #if GLYPH_DEBUG
11771 *w->desired_matrix->method = 0;
11772 debug_method_add (w, "optimization 3");
11773 #endif
11774 goto update;
11776 else
11777 goto cancel;
11780 cancel:
11781 /* Text changed drastically or point moved off of line. */
11782 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11785 CHARPOS (this_line_start_pos) = 0;
11786 consider_all_windows_p |= buffer_shared > 1;
11787 ++clear_face_cache_count;
11788 #ifdef HAVE_WINDOW_SYSTEM
11789 ++clear_image_cache_count;
11790 #endif
11792 /* Build desired matrices, and update the display. If
11793 consider_all_windows_p is non-zero, do it for all windows on all
11794 frames. Otherwise do it for selected_window, only. */
11796 if (consider_all_windows_p)
11798 Lisp_Object tail, frame;
11800 FOR_EACH_FRAME (tail, frame)
11801 XFRAME (frame)->updated_p = 0;
11803 /* Recompute # windows showing selected buffer. This will be
11804 incremented each time such a window is displayed. */
11805 buffer_shared = 0;
11807 FOR_EACH_FRAME (tail, frame)
11809 struct frame *f = XFRAME (frame);
11811 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11813 if (! EQ (frame, selected_frame))
11814 /* Select the frame, for the sake of frame-local
11815 variables. */
11816 select_frame_for_redisplay (frame);
11818 /* Mark all the scroll bars to be removed; we'll redeem
11819 the ones we want when we redisplay their windows. */
11820 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11821 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11823 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11824 redisplay_windows (FRAME_ROOT_WINDOW (f));
11826 /* The X error handler may have deleted that frame. */
11827 if (!FRAME_LIVE_P (f))
11828 continue;
11830 /* Any scroll bars which redisplay_windows should have
11831 nuked should now go away. */
11832 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11833 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11835 /* If fonts changed, display again. */
11836 /* ??? rms: I suspect it is a mistake to jump all the way
11837 back to retry here. It should just retry this frame. */
11838 if (fonts_changed_p)
11839 goto retry;
11841 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11843 /* See if we have to hscroll. */
11844 if (!f->already_hscrolled_p)
11846 f->already_hscrolled_p = 1;
11847 if (hscroll_windows (f->root_window))
11848 goto retry;
11851 /* Prevent various kinds of signals during display
11852 update. stdio is not robust about handling
11853 signals, which can cause an apparent I/O
11854 error. */
11855 if (interrupt_input)
11856 unrequest_sigio ();
11857 STOP_POLLING;
11859 /* Update the display. */
11860 set_window_update_flags (XWINDOW (f->root_window), 1);
11861 pause |= update_frame (f, 0, 0);
11862 f->updated_p = 1;
11867 if (!EQ (old_frame, selected_frame)
11868 && FRAME_LIVE_P (XFRAME (old_frame)))
11869 /* We played a bit fast-and-loose above and allowed selected_frame
11870 and selected_window to be temporarily out-of-sync but let's make
11871 sure this stays contained. */
11872 select_frame_for_redisplay (old_frame);
11873 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11875 if (!pause)
11877 /* Do the mark_window_display_accurate after all windows have
11878 been redisplayed because this call resets flags in buffers
11879 which are needed for proper redisplay. */
11880 FOR_EACH_FRAME (tail, frame)
11882 struct frame *f = XFRAME (frame);
11883 if (f->updated_p)
11885 mark_window_display_accurate (f->root_window, 1);
11886 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11887 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11892 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11894 Lisp_Object mini_window;
11895 struct frame *mini_frame;
11897 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11898 /* Use list_of_error, not Qerror, so that
11899 we catch only errors and don't run the debugger. */
11900 internal_condition_case_1 (redisplay_window_1, selected_window,
11901 list_of_error,
11902 redisplay_window_error);
11904 /* Compare desired and current matrices, perform output. */
11906 update:
11907 /* If fonts changed, display again. */
11908 if (fonts_changed_p)
11909 goto retry;
11911 /* Prevent various kinds of signals during display update.
11912 stdio is not robust about handling signals,
11913 which can cause an apparent I/O error. */
11914 if (interrupt_input)
11915 unrequest_sigio ();
11916 STOP_POLLING;
11918 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11920 if (hscroll_windows (selected_window))
11921 goto retry;
11923 XWINDOW (selected_window)->must_be_updated_p = 1;
11924 pause = update_frame (sf, 0, 0);
11927 /* We may have called echo_area_display at the top of this
11928 function. If the echo area is on another frame, that may
11929 have put text on a frame other than the selected one, so the
11930 above call to update_frame would not have caught it. Catch
11931 it here. */
11932 mini_window = FRAME_MINIBUF_WINDOW (sf);
11933 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11935 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11937 XWINDOW (mini_window)->must_be_updated_p = 1;
11938 pause |= update_frame (mini_frame, 0, 0);
11939 if (!pause && hscroll_windows (mini_window))
11940 goto retry;
11944 /* If display was paused because of pending input, make sure we do a
11945 thorough update the next time. */
11946 if (pause)
11948 /* Prevent the optimization at the beginning of
11949 redisplay_internal that tries a single-line update of the
11950 line containing the cursor in the selected window. */
11951 CHARPOS (this_line_start_pos) = 0;
11953 /* Let the overlay arrow be updated the next time. */
11954 update_overlay_arrows (0);
11956 /* If we pause after scrolling, some rows in the current
11957 matrices of some windows are not valid. */
11958 if (!WINDOW_FULL_WIDTH_P (w)
11959 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11960 update_mode_lines = 1;
11962 else
11964 if (!consider_all_windows_p)
11966 /* This has already been done above if
11967 consider_all_windows_p is set. */
11968 mark_window_display_accurate_1 (w, 1);
11970 /* Say overlay arrows are up to date. */
11971 update_overlay_arrows (1);
11973 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11974 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11977 update_mode_lines = 0;
11978 windows_or_buffers_changed = 0;
11979 cursor_type_changed = 0;
11982 /* Start SIGIO interrupts coming again. Having them off during the
11983 code above makes it less likely one will discard output, but not
11984 impossible, since there might be stuff in the system buffer here.
11985 But it is much hairier to try to do anything about that. */
11986 if (interrupt_input)
11987 request_sigio ();
11988 RESUME_POLLING;
11990 /* If a frame has become visible which was not before, redisplay
11991 again, so that we display it. Expose events for such a frame
11992 (which it gets when becoming visible) don't call the parts of
11993 redisplay constructing glyphs, so simply exposing a frame won't
11994 display anything in this case. So, we have to display these
11995 frames here explicitly. */
11996 if (!pause)
11998 Lisp_Object tail, frame;
11999 int new_count = 0;
12001 FOR_EACH_FRAME (tail, frame)
12003 int this_is_visible = 0;
12005 if (XFRAME (frame)->visible)
12006 this_is_visible = 1;
12007 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12008 if (XFRAME (frame)->visible)
12009 this_is_visible = 1;
12011 if (this_is_visible)
12012 new_count++;
12015 if (new_count != number_of_visible_frames)
12016 windows_or_buffers_changed++;
12019 /* Change frame size now if a change is pending. */
12020 do_pending_window_change (1);
12022 /* If we just did a pending size change, or have additional
12023 visible frames, redisplay again. */
12024 if (windows_or_buffers_changed && !pause)
12025 goto retry;
12027 /* Clear the face cache eventually. */
12028 if (consider_all_windows_p)
12030 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12032 clear_face_cache (0);
12033 clear_face_cache_count = 0;
12035 #ifdef HAVE_WINDOW_SYSTEM
12036 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12038 clear_image_caches (Qnil);
12039 clear_image_cache_count = 0;
12041 #endif /* HAVE_WINDOW_SYSTEM */
12044 end_of_redisplay:
12045 unbind_to (count, Qnil);
12046 RESUME_POLLING;
12050 /* Redisplay, but leave alone any recent echo area message unless
12051 another message has been requested in its place.
12053 This is useful in situations where you need to redisplay but no
12054 user action has occurred, making it inappropriate for the message
12055 area to be cleared. See tracking_off and
12056 wait_reading_process_output for examples of these situations.
12058 FROM_WHERE is an integer saying from where this function was
12059 called. This is useful for debugging. */
12061 void
12062 redisplay_preserve_echo_area (from_where)
12063 int from_where;
12065 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12067 if (!NILP (echo_area_buffer[1]))
12069 /* We have a previously displayed message, but no current
12070 message. Redisplay the previous message. */
12071 display_last_displayed_message_p = 1;
12072 redisplay_internal (1);
12073 display_last_displayed_message_p = 0;
12075 else
12076 redisplay_internal (1);
12078 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12079 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12080 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12084 /* Function registered with record_unwind_protect in
12085 redisplay_internal. Reset redisplaying_p to the value it had
12086 before redisplay_internal was called, and clear
12087 prevent_freeing_realized_faces_p. It also selects the previously
12088 selected frame, unless it has been deleted (by an X connection
12089 failure during redisplay, for example). */
12091 static Lisp_Object
12092 unwind_redisplay (val)
12093 Lisp_Object val;
12095 Lisp_Object old_redisplaying_p, old_frame;
12097 old_redisplaying_p = XCAR (val);
12098 redisplaying_p = XFASTINT (old_redisplaying_p);
12099 old_frame = XCDR (val);
12100 if (! EQ (old_frame, selected_frame)
12101 && FRAME_LIVE_P (XFRAME (old_frame)))
12102 select_frame_for_redisplay (old_frame);
12103 return Qnil;
12107 /* Mark the display of window W as accurate or inaccurate. If
12108 ACCURATE_P is non-zero mark display of W as accurate. If
12109 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12110 redisplay_internal is called. */
12112 static void
12113 mark_window_display_accurate_1 (w, accurate_p)
12114 struct window *w;
12115 int accurate_p;
12117 if (BUFFERP (w->buffer))
12119 struct buffer *b = XBUFFER (w->buffer);
12121 w->last_modified
12122 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12123 w->last_overlay_modified
12124 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12125 w->last_had_star
12126 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12128 if (accurate_p)
12130 b->clip_changed = 0;
12131 b->prevent_redisplay_optimizations_p = 0;
12133 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12134 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12135 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12136 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12138 w->current_matrix->buffer = b;
12139 w->current_matrix->begv = BUF_BEGV (b);
12140 w->current_matrix->zv = BUF_ZV (b);
12142 w->last_cursor = w->cursor;
12143 w->last_cursor_off_p = w->cursor_off_p;
12145 if (w == XWINDOW (selected_window))
12146 w->last_point = make_number (BUF_PT (b));
12147 else
12148 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12152 if (accurate_p)
12154 w->window_end_valid = w->buffer;
12155 w->update_mode_line = Qnil;
12160 /* Mark the display of windows in the window tree rooted at WINDOW as
12161 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12162 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12163 be redisplayed the next time redisplay_internal is called. */
12165 void
12166 mark_window_display_accurate (window, accurate_p)
12167 Lisp_Object window;
12168 int accurate_p;
12170 struct window *w;
12172 for (; !NILP (window); window = w->next)
12174 w = XWINDOW (window);
12175 mark_window_display_accurate_1 (w, accurate_p);
12177 if (!NILP (w->vchild))
12178 mark_window_display_accurate (w->vchild, accurate_p);
12179 if (!NILP (w->hchild))
12180 mark_window_display_accurate (w->hchild, accurate_p);
12183 if (accurate_p)
12185 update_overlay_arrows (1);
12187 else
12189 /* Force a thorough redisplay the next time by setting
12190 last_arrow_position and last_arrow_string to t, which is
12191 unequal to any useful value of Voverlay_arrow_... */
12192 update_overlay_arrows (-1);
12197 /* Return value in display table DP (Lisp_Char_Table *) for character
12198 C. Since a display table doesn't have any parent, we don't have to
12199 follow parent. Do not call this function directly but use the
12200 macro DISP_CHAR_VECTOR. */
12202 Lisp_Object
12203 disp_char_vector (dp, c)
12204 struct Lisp_Char_Table *dp;
12205 int c;
12207 Lisp_Object val;
12209 if (ASCII_CHAR_P (c))
12211 val = dp->ascii;
12212 if (SUB_CHAR_TABLE_P (val))
12213 val = XSUB_CHAR_TABLE (val)->contents[c];
12215 else
12217 Lisp_Object table;
12219 XSETCHAR_TABLE (table, dp);
12220 val = char_table_ref (table, c);
12222 if (NILP (val))
12223 val = dp->defalt;
12224 return val;
12229 /***********************************************************************
12230 Window Redisplay
12231 ***********************************************************************/
12233 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12235 static void
12236 redisplay_windows (window)
12237 Lisp_Object window;
12239 while (!NILP (window))
12241 struct window *w = XWINDOW (window);
12243 if (!NILP (w->hchild))
12244 redisplay_windows (w->hchild);
12245 else if (!NILP (w->vchild))
12246 redisplay_windows (w->vchild);
12247 else if (!NILP (w->buffer))
12249 displayed_buffer = XBUFFER (w->buffer);
12250 /* Use list_of_error, not Qerror, so that
12251 we catch only errors and don't run the debugger. */
12252 internal_condition_case_1 (redisplay_window_0, window,
12253 list_of_error,
12254 redisplay_window_error);
12257 window = w->next;
12261 static Lisp_Object
12262 redisplay_window_error ()
12264 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12265 return Qnil;
12268 static Lisp_Object
12269 redisplay_window_0 (window)
12270 Lisp_Object window;
12272 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12273 redisplay_window (window, 0);
12274 return Qnil;
12277 static Lisp_Object
12278 redisplay_window_1 (window)
12279 Lisp_Object window;
12281 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12282 redisplay_window (window, 1);
12283 return Qnil;
12287 /* Increment GLYPH until it reaches END or CONDITION fails while
12288 adding (GLYPH)->pixel_width to X. */
12290 #define SKIP_GLYPHS(glyph, end, x, condition) \
12291 do \
12293 (x) += (glyph)->pixel_width; \
12294 ++(glyph); \
12296 while ((glyph) < (end) && (condition))
12299 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12300 DELTA is the number of bytes by which positions recorded in ROW
12301 differ from current buffer positions.
12303 Return 0 if cursor is not on this row. 1 otherwise. */
12306 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12307 struct window *w;
12308 struct glyph_row *row;
12309 struct glyph_matrix *matrix;
12310 int delta, delta_bytes, dy, dvpos;
12312 struct glyph *glyph = row->glyphs[TEXT_AREA];
12313 struct glyph *end = glyph + row->used[TEXT_AREA];
12314 struct glyph *cursor = NULL;
12315 /* The first glyph that starts a sequence of glyphs from string. */
12316 struct glyph *string_start;
12317 /* The X coordinate of string_start. */
12318 int string_start_x;
12319 /* The last known character position. */
12320 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12321 /* The last known character position before string_start. */
12322 int string_before_pos;
12323 int x = row->x;
12324 int cursor_x = x;
12325 int cursor_from_overlay_pos = 0;
12326 int pt_old = PT - delta;
12328 /* Skip over glyphs not having an object at the start of the row.
12329 These are special glyphs like truncation marks on terminal
12330 frames. */
12331 if (row->displays_text_p)
12332 while (glyph < end
12333 && INTEGERP (glyph->object)
12334 && glyph->charpos < 0)
12336 x += glyph->pixel_width;
12337 ++glyph;
12340 string_start = NULL;
12341 while (glyph < end
12342 && !INTEGERP (glyph->object)
12343 && (!BUFFERP (glyph->object)
12344 || (last_pos = glyph->charpos) < pt_old
12345 || glyph->avoid_cursor_p))
12347 if (! STRINGP (glyph->object))
12349 string_start = NULL;
12350 x += glyph->pixel_width;
12351 ++glyph;
12352 if (cursor_from_overlay_pos
12353 && last_pos >= cursor_from_overlay_pos)
12355 cursor_from_overlay_pos = 0;
12356 cursor = 0;
12359 else
12361 if (string_start == NULL)
12363 string_before_pos = last_pos;
12364 string_start = glyph;
12365 string_start_x = x;
12367 /* Skip all glyphs from string. */
12370 Lisp_Object cprop;
12371 int pos;
12372 if ((cursor == NULL || glyph > cursor)
12373 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12374 Qcursor, (glyph)->object),
12375 !NILP (cprop))
12376 && (pos = string_buffer_position (w, glyph->object,
12377 string_before_pos),
12378 (pos == 0 /* From overlay */
12379 || pos == pt_old)))
12381 /* Estimate overlay buffer position from the buffer
12382 positions of the glyphs before and after the overlay.
12383 Add 1 to last_pos so that if point corresponds to the
12384 glyph right after the overlay, we still use a 'cursor'
12385 property found in that overlay. */
12386 cursor_from_overlay_pos = (pos ? 0 : last_pos
12387 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12388 cursor = glyph;
12389 cursor_x = x;
12391 x += glyph->pixel_width;
12392 ++glyph;
12394 while (glyph < end && EQ (glyph->object, string_start->object));
12398 if (cursor != NULL)
12400 glyph = cursor;
12401 x = cursor_x;
12403 else if (row->ends_in_ellipsis_p && glyph == end)
12405 /* Scan back over the ellipsis glyphs, decrementing positions. */
12406 while (glyph > row->glyphs[TEXT_AREA]
12407 && (glyph - 1)->charpos == last_pos)
12408 glyph--, x -= glyph->pixel_width;
12409 /* That loop always goes one position too far,
12410 including the glyph before the ellipsis.
12411 So scan forward over that one. */
12412 x += glyph->pixel_width;
12413 glyph++;
12415 else if (string_start
12416 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12418 /* We may have skipped over point because the previous glyphs
12419 are from string. As there's no easy way to know the
12420 character position of the current glyph, find the correct
12421 glyph on point by scanning from string_start again. */
12422 Lisp_Object limit;
12423 Lisp_Object string;
12424 struct glyph *stop = glyph;
12425 int pos;
12427 limit = make_number (pt_old + 1);
12428 glyph = string_start;
12429 x = string_start_x;
12430 string = glyph->object;
12431 pos = string_buffer_position (w, string, string_before_pos);
12432 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12433 because we always put cursor after overlay strings. */
12434 while (pos == 0 && glyph < stop)
12436 string = glyph->object;
12437 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12438 if (glyph < stop)
12439 pos = string_buffer_position (w, glyph->object, string_before_pos);
12442 while (glyph < stop)
12444 pos = XINT (Fnext_single_char_property_change
12445 (make_number (pos), Qdisplay, Qnil, limit));
12446 if (pos > pt_old)
12447 break;
12448 /* Skip glyphs from the same string. */
12449 string = glyph->object;
12450 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12451 /* Skip glyphs from an overlay. */
12452 while (glyph < stop
12453 && ! string_buffer_position (w, glyph->object, pos))
12455 string = glyph->object;
12456 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12460 /* If we reached the end of the line, and end was from a string,
12461 cursor is not on this line. */
12462 if (glyph == end && row->continued_p)
12463 return 0;
12466 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12467 w->cursor.x = x;
12468 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12469 w->cursor.y = row->y + dy;
12471 if (w == XWINDOW (selected_window))
12473 if (!row->continued_p
12474 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12475 && row->x == 0)
12477 this_line_buffer = XBUFFER (w->buffer);
12479 CHARPOS (this_line_start_pos)
12480 = MATRIX_ROW_START_CHARPOS (row) + delta;
12481 BYTEPOS (this_line_start_pos)
12482 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12484 CHARPOS (this_line_end_pos)
12485 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12486 BYTEPOS (this_line_end_pos)
12487 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12489 this_line_y = w->cursor.y;
12490 this_line_pixel_height = row->height;
12491 this_line_vpos = w->cursor.vpos;
12492 this_line_start_x = row->x;
12494 else
12495 CHARPOS (this_line_start_pos) = 0;
12498 return 1;
12502 /* Run window scroll functions, if any, for WINDOW with new window
12503 start STARTP. Sets the window start of WINDOW to that position.
12505 We assume that the window's buffer is really current. */
12507 static INLINE struct text_pos
12508 run_window_scroll_functions (window, startp)
12509 Lisp_Object window;
12510 struct text_pos startp;
12512 struct window *w = XWINDOW (window);
12513 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12515 if (current_buffer != XBUFFER (w->buffer))
12516 abort ();
12518 if (!NILP (Vwindow_scroll_functions))
12520 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12521 make_number (CHARPOS (startp)));
12522 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12523 /* In case the hook functions switch buffers. */
12524 if (current_buffer != XBUFFER (w->buffer))
12525 set_buffer_internal_1 (XBUFFER (w->buffer));
12528 return startp;
12532 /* Make sure the line containing the cursor is fully visible.
12533 A value of 1 means there is nothing to be done.
12534 (Either the line is fully visible, or it cannot be made so,
12535 or we cannot tell.)
12537 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12538 is higher than window.
12540 A value of 0 means the caller should do scrolling
12541 as if point had gone off the screen. */
12543 static int
12544 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12545 struct window *w;
12546 int force_p;
12547 int current_matrix_p;
12549 struct glyph_matrix *matrix;
12550 struct glyph_row *row;
12551 int window_height;
12553 if (!make_cursor_line_fully_visible_p)
12554 return 1;
12556 /* It's not always possible to find the cursor, e.g, when a window
12557 is full of overlay strings. Don't do anything in that case. */
12558 if (w->cursor.vpos < 0)
12559 return 1;
12561 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12562 row = MATRIX_ROW (matrix, w->cursor.vpos);
12564 /* If the cursor row is not partially visible, there's nothing to do. */
12565 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12566 return 1;
12568 /* If the row the cursor is in is taller than the window's height,
12569 it's not clear what to do, so do nothing. */
12570 window_height = window_box_height (w);
12571 if (row->height >= window_height)
12573 if (!force_p || MINI_WINDOW_P (w)
12574 || w->vscroll || w->cursor.vpos == 0)
12575 return 1;
12577 return 0;
12581 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12582 non-zero means only WINDOW is redisplayed in redisplay_internal.
12583 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12584 in redisplay_window to bring a partially visible line into view in
12585 the case that only the cursor has moved.
12587 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12588 last screen line's vertical height extends past the end of the screen.
12590 Value is
12592 1 if scrolling succeeded
12594 0 if scrolling didn't find point.
12596 -1 if new fonts have been loaded so that we must interrupt
12597 redisplay, adjust glyph matrices, and try again. */
12599 enum
12601 SCROLLING_SUCCESS,
12602 SCROLLING_FAILED,
12603 SCROLLING_NEED_LARGER_MATRICES
12606 static int
12607 try_scrolling (window, just_this_one_p, scroll_conservatively,
12608 scroll_step, temp_scroll_step, last_line_misfit)
12609 Lisp_Object window;
12610 int just_this_one_p;
12611 EMACS_INT scroll_conservatively, scroll_step;
12612 int temp_scroll_step;
12613 int last_line_misfit;
12615 struct window *w = XWINDOW (window);
12616 struct frame *f = XFRAME (w->frame);
12617 struct text_pos pos, startp;
12618 struct it it;
12619 int this_scroll_margin, scroll_max, rc, height;
12620 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12621 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12622 Lisp_Object aggressive;
12623 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12625 #if GLYPH_DEBUG
12626 debug_method_add (w, "try_scrolling");
12627 #endif
12629 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12631 /* Compute scroll margin height in pixels. We scroll when point is
12632 within this distance from the top or bottom of the window. */
12633 if (scroll_margin > 0)
12634 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12635 * FRAME_LINE_HEIGHT (f);
12636 else
12637 this_scroll_margin = 0;
12639 /* Force scroll_conservatively to have a reasonable value, to avoid
12640 overflow while computing how much to scroll. Note that the user
12641 can supply scroll-conservatively equal to `most-positive-fixnum',
12642 which can be larger than INT_MAX. */
12643 if (scroll_conservatively > scroll_limit)
12645 scroll_conservatively = scroll_limit;
12646 scroll_max = INT_MAX;
12648 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12649 /* Compute how much we should try to scroll maximally to bring
12650 point into view. */
12651 scroll_max = (max (scroll_step,
12652 max (scroll_conservatively, temp_scroll_step))
12653 * FRAME_LINE_HEIGHT (f));
12654 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12655 || NUMBERP (current_buffer->scroll_up_aggressively))
12656 /* We're trying to scroll because of aggressive scrolling but no
12657 scroll_step is set. Choose an arbitrary one. */
12658 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12659 else
12660 scroll_max = 0;
12662 too_near_end:
12664 /* Decide whether to scroll down. */
12665 if (PT > CHARPOS (startp))
12667 int scroll_margin_y;
12669 /* Compute the pixel ypos of the scroll margin, then move it to
12670 either that ypos or PT, whichever comes first. */
12671 start_display (&it, w, startp);
12672 scroll_margin_y = it.last_visible_y - this_scroll_margin
12673 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12674 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12675 (MOVE_TO_POS | MOVE_TO_Y));
12677 if (PT > CHARPOS (it.current.pos))
12679 int y0 = line_bottom_y (&it);
12681 /* Compute the distance from the scroll margin to PT
12682 (including the height of the cursor line). Moving the
12683 iterator unconditionally to PT can be slow if PT is far
12684 away, so stop 10 lines past the window bottom (is there a
12685 way to do the right thing quickly?). */
12686 move_it_to (&it, PT, -1,
12687 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12688 -1, MOVE_TO_POS | MOVE_TO_Y);
12689 dy = line_bottom_y (&it) - y0;
12691 if (dy > scroll_max)
12692 return SCROLLING_FAILED;
12694 scroll_down_p = 1;
12698 if (scroll_down_p)
12700 /* Point is in or below the bottom scroll margin, so move the
12701 window start down. If scrolling conservatively, move it just
12702 enough down to make point visible. If scroll_step is set,
12703 move it down by scroll_step. */
12704 if (scroll_conservatively)
12705 amount_to_scroll
12706 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12707 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12708 else if (scroll_step || temp_scroll_step)
12709 amount_to_scroll = scroll_max;
12710 else
12712 aggressive = current_buffer->scroll_up_aggressively;
12713 height = WINDOW_BOX_TEXT_HEIGHT (w);
12714 if (NUMBERP (aggressive))
12716 double float_amount = XFLOATINT (aggressive) * height;
12717 amount_to_scroll = float_amount;
12718 if (amount_to_scroll == 0 && float_amount > 0)
12719 amount_to_scroll = 1;
12723 if (amount_to_scroll <= 0)
12724 return SCROLLING_FAILED;
12726 start_display (&it, w, startp);
12727 move_it_vertically (&it, amount_to_scroll);
12729 /* If STARTP is unchanged, move it down another screen line. */
12730 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12731 move_it_by_lines (&it, 1, 1);
12732 startp = it.current.pos;
12734 else
12736 struct text_pos scroll_margin_pos = startp;
12738 /* See if point is inside the scroll margin at the top of the
12739 window. */
12740 if (this_scroll_margin)
12742 start_display (&it, w, startp);
12743 move_it_vertically (&it, this_scroll_margin);
12744 scroll_margin_pos = it.current.pos;
12747 if (PT < CHARPOS (scroll_margin_pos))
12749 /* Point is in the scroll margin at the top of the window or
12750 above what is displayed in the window. */
12751 int y0;
12753 /* Compute the vertical distance from PT to the scroll
12754 margin position. Give up if distance is greater than
12755 scroll_max. */
12756 SET_TEXT_POS (pos, PT, PT_BYTE);
12757 start_display (&it, w, pos);
12758 y0 = it.current_y;
12759 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12760 it.last_visible_y, -1,
12761 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12762 dy = it.current_y - y0;
12763 if (dy > scroll_max)
12764 return SCROLLING_FAILED;
12766 /* Compute new window start. */
12767 start_display (&it, w, startp);
12769 if (scroll_conservatively)
12770 amount_to_scroll
12771 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12772 else if (scroll_step || temp_scroll_step)
12773 amount_to_scroll = scroll_max;
12774 else
12776 aggressive = current_buffer->scroll_down_aggressively;
12777 height = WINDOW_BOX_TEXT_HEIGHT (w);
12778 if (NUMBERP (aggressive))
12780 double float_amount = XFLOATINT (aggressive) * height;
12781 amount_to_scroll = float_amount;
12782 if (amount_to_scroll == 0 && float_amount > 0)
12783 amount_to_scroll = 1;
12787 if (amount_to_scroll <= 0)
12788 return SCROLLING_FAILED;
12790 move_it_vertically_backward (&it, amount_to_scroll);
12791 startp = it.current.pos;
12795 /* Run window scroll functions. */
12796 startp = run_window_scroll_functions (window, startp);
12798 /* Display the window. Give up if new fonts are loaded, or if point
12799 doesn't appear. */
12800 if (!try_window (window, startp, 0))
12801 rc = SCROLLING_NEED_LARGER_MATRICES;
12802 else if (w->cursor.vpos < 0)
12804 clear_glyph_matrix (w->desired_matrix);
12805 rc = SCROLLING_FAILED;
12807 else
12809 /* Maybe forget recorded base line for line number display. */
12810 if (!just_this_one_p
12811 || current_buffer->clip_changed
12812 || BEG_UNCHANGED < CHARPOS (startp))
12813 w->base_line_number = Qnil;
12815 /* If cursor ends up on a partially visible line,
12816 treat that as being off the bottom of the screen. */
12817 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12819 clear_glyph_matrix (w->desired_matrix);
12820 ++extra_scroll_margin_lines;
12821 goto too_near_end;
12823 rc = SCROLLING_SUCCESS;
12826 return rc;
12830 /* Compute a suitable window start for window W if display of W starts
12831 on a continuation line. Value is non-zero if a new window start
12832 was computed.
12834 The new window start will be computed, based on W's width, starting
12835 from the start of the continued line. It is the start of the
12836 screen line with the minimum distance from the old start W->start. */
12838 static int
12839 compute_window_start_on_continuation_line (w)
12840 struct window *w;
12842 struct text_pos pos, start_pos;
12843 int window_start_changed_p = 0;
12845 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12847 /* If window start is on a continuation line... Window start may be
12848 < BEGV in case there's invisible text at the start of the
12849 buffer (M-x rmail, for example). */
12850 if (CHARPOS (start_pos) > BEGV
12851 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12853 struct it it;
12854 struct glyph_row *row;
12856 /* Handle the case that the window start is out of range. */
12857 if (CHARPOS (start_pos) < BEGV)
12858 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12859 else if (CHARPOS (start_pos) > ZV)
12860 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12862 /* Find the start of the continued line. This should be fast
12863 because scan_buffer is fast (newline cache). */
12864 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12865 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12866 row, DEFAULT_FACE_ID);
12867 reseat_at_previous_visible_line_start (&it);
12869 /* If the line start is "too far" away from the window start,
12870 say it takes too much time to compute a new window start. */
12871 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12872 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12874 int min_distance, distance;
12876 /* Move forward by display lines to find the new window
12877 start. If window width was enlarged, the new start can
12878 be expected to be > the old start. If window width was
12879 decreased, the new window start will be < the old start.
12880 So, we're looking for the display line start with the
12881 minimum distance from the old window start. */
12882 pos = it.current.pos;
12883 min_distance = INFINITY;
12884 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12885 distance < min_distance)
12887 min_distance = distance;
12888 pos = it.current.pos;
12889 move_it_by_lines (&it, 1, 0);
12892 /* Set the window start there. */
12893 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12894 window_start_changed_p = 1;
12898 return window_start_changed_p;
12902 /* Try cursor movement in case text has not changed in window WINDOW,
12903 with window start STARTP. Value is
12905 CURSOR_MOVEMENT_SUCCESS if successful
12907 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12909 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12910 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12911 we want to scroll as if scroll-step were set to 1. See the code.
12913 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12914 which case we have to abort this redisplay, and adjust matrices
12915 first. */
12917 enum
12919 CURSOR_MOVEMENT_SUCCESS,
12920 CURSOR_MOVEMENT_CANNOT_BE_USED,
12921 CURSOR_MOVEMENT_MUST_SCROLL,
12922 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12925 static int
12926 try_cursor_movement (window, startp, scroll_step)
12927 Lisp_Object window;
12928 struct text_pos startp;
12929 int *scroll_step;
12931 struct window *w = XWINDOW (window);
12932 struct frame *f = XFRAME (w->frame);
12933 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12935 #if GLYPH_DEBUG
12936 if (inhibit_try_cursor_movement)
12937 return rc;
12938 #endif
12940 /* Handle case where text has not changed, only point, and it has
12941 not moved off the frame. */
12942 if (/* Point may be in this window. */
12943 PT >= CHARPOS (startp)
12944 /* Selective display hasn't changed. */
12945 && !current_buffer->clip_changed
12946 /* Function force-mode-line-update is used to force a thorough
12947 redisplay. It sets either windows_or_buffers_changed or
12948 update_mode_lines. So don't take a shortcut here for these
12949 cases. */
12950 && !update_mode_lines
12951 && !windows_or_buffers_changed
12952 && !cursor_type_changed
12953 /* Can't use this case if highlighting a region. When a
12954 region exists, cursor movement has to do more than just
12955 set the cursor. */
12956 && !(!NILP (Vtransient_mark_mode)
12957 && !NILP (current_buffer->mark_active))
12958 && NILP (w->region_showing)
12959 && NILP (Vshow_trailing_whitespace)
12960 /* Right after splitting windows, last_point may be nil. */
12961 && INTEGERP (w->last_point)
12962 /* This code is not used for mini-buffer for the sake of the case
12963 of redisplaying to replace an echo area message; since in
12964 that case the mini-buffer contents per se are usually
12965 unchanged. This code is of no real use in the mini-buffer
12966 since the handling of this_line_start_pos, etc., in redisplay
12967 handles the same cases. */
12968 && !EQ (window, minibuf_window)
12969 /* When splitting windows or for new windows, it happens that
12970 redisplay is called with a nil window_end_vpos or one being
12971 larger than the window. This should really be fixed in
12972 window.c. I don't have this on my list, now, so we do
12973 approximately the same as the old redisplay code. --gerd. */
12974 && INTEGERP (w->window_end_vpos)
12975 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12976 && (FRAME_WINDOW_P (f)
12977 || !overlay_arrow_in_current_buffer_p ()))
12979 int this_scroll_margin, top_scroll_margin;
12980 struct glyph_row *row = NULL;
12982 #if GLYPH_DEBUG
12983 debug_method_add (w, "cursor movement");
12984 #endif
12986 /* Scroll if point within this distance from the top or bottom
12987 of the window. This is a pixel value. */
12988 if (scroll_margin > 0)
12990 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12991 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12993 else
12994 this_scroll_margin = 0;
12996 top_scroll_margin = this_scroll_margin;
12997 if (WINDOW_WANTS_HEADER_LINE_P (w))
12998 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13000 /* Start with the row the cursor was displayed during the last
13001 not paused redisplay. Give up if that row is not valid. */
13002 if (w->last_cursor.vpos < 0
13003 || w->last_cursor.vpos >= w->current_matrix->nrows)
13004 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13005 else
13007 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13008 if (row->mode_line_p)
13009 ++row;
13010 if (!row->enabled_p)
13011 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13014 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13016 int scroll_p = 0;
13017 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13019 if (PT > XFASTINT (w->last_point))
13021 /* Point has moved forward. */
13022 while (MATRIX_ROW_END_CHARPOS (row) < PT
13023 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13025 xassert (row->enabled_p);
13026 ++row;
13029 /* The end position of a row equals the start position
13030 of the next row. If PT is there, we would rather
13031 display it in the next line. */
13032 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13033 && MATRIX_ROW_END_CHARPOS (row) == PT
13034 && !cursor_row_p (w, row))
13035 ++row;
13037 /* If within the scroll margin, scroll. Note that
13038 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13039 the next line would be drawn, and that
13040 this_scroll_margin can be zero. */
13041 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13042 || PT > MATRIX_ROW_END_CHARPOS (row)
13043 /* Line is completely visible last line in window
13044 and PT is to be set in the next line. */
13045 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13046 && PT == MATRIX_ROW_END_CHARPOS (row)
13047 && !row->ends_at_zv_p
13048 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13049 scroll_p = 1;
13051 else if (PT < XFASTINT (w->last_point))
13053 /* Cursor has to be moved backward. Note that PT >=
13054 CHARPOS (startp) because of the outer if-statement. */
13055 while (!row->mode_line_p
13056 && (MATRIX_ROW_START_CHARPOS (row) > PT
13057 || (MATRIX_ROW_START_CHARPOS (row) == PT
13058 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13059 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13060 row > w->current_matrix->rows
13061 && (row-1)->ends_in_newline_from_string_p))))
13062 && (row->y > top_scroll_margin
13063 || CHARPOS (startp) == BEGV))
13065 xassert (row->enabled_p);
13066 --row;
13069 /* Consider the following case: Window starts at BEGV,
13070 there is invisible, intangible text at BEGV, so that
13071 display starts at some point START > BEGV. It can
13072 happen that we are called with PT somewhere between
13073 BEGV and START. Try to handle that case. */
13074 if (row < w->current_matrix->rows
13075 || row->mode_line_p)
13077 row = w->current_matrix->rows;
13078 if (row->mode_line_p)
13079 ++row;
13082 /* Due to newlines in overlay strings, we may have to
13083 skip forward over overlay strings. */
13084 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13085 && MATRIX_ROW_END_CHARPOS (row) == PT
13086 && !cursor_row_p (w, row))
13087 ++row;
13089 /* If within the scroll margin, scroll. */
13090 if (row->y < top_scroll_margin
13091 && CHARPOS (startp) != BEGV)
13092 scroll_p = 1;
13094 else
13096 /* Cursor did not move. So don't scroll even if cursor line
13097 is partially visible, as it was so before. */
13098 rc = CURSOR_MOVEMENT_SUCCESS;
13101 if (PT < MATRIX_ROW_START_CHARPOS (row)
13102 || PT > MATRIX_ROW_END_CHARPOS (row))
13104 /* if PT is not in the glyph row, give up. */
13105 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13107 else if (rc != CURSOR_MOVEMENT_SUCCESS
13108 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13109 && make_cursor_line_fully_visible_p)
13111 if (PT == MATRIX_ROW_END_CHARPOS (row)
13112 && !row->ends_at_zv_p
13113 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13114 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13115 else if (row->height > window_box_height (w))
13117 /* If we end up in a partially visible line, let's
13118 make it fully visible, except when it's taller
13119 than the window, in which case we can't do much
13120 about it. */
13121 *scroll_step = 1;
13122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13124 else
13126 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13127 if (!cursor_row_fully_visible_p (w, 0, 1))
13128 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13129 else
13130 rc = CURSOR_MOVEMENT_SUCCESS;
13133 else if (scroll_p)
13134 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13135 else
13139 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13141 rc = CURSOR_MOVEMENT_SUCCESS;
13142 break;
13144 ++row;
13146 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13147 && MATRIX_ROW_START_CHARPOS (row) == PT
13148 && cursor_row_p (w, row));
13153 return rc;
13156 void
13157 set_vertical_scroll_bar (w)
13158 struct window *w;
13160 int start, end, whole;
13162 /* Calculate the start and end positions for the current window.
13163 At some point, it would be nice to choose between scrollbars
13164 which reflect the whole buffer size, with special markers
13165 indicating narrowing, and scrollbars which reflect only the
13166 visible region.
13168 Note that mini-buffers sometimes aren't displaying any text. */
13169 if (!MINI_WINDOW_P (w)
13170 || (w == XWINDOW (minibuf_window)
13171 && NILP (echo_area_buffer[0])))
13173 struct buffer *buf = XBUFFER (w->buffer);
13174 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13175 start = marker_position (w->start) - BUF_BEGV (buf);
13176 /* I don't think this is guaranteed to be right. For the
13177 moment, we'll pretend it is. */
13178 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13180 if (end < start)
13181 end = start;
13182 if (whole < (end - start))
13183 whole = end - start;
13185 else
13186 start = end = whole = 0;
13188 /* Indicate what this scroll bar ought to be displaying now. */
13189 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13190 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13191 (w, end - start, whole, start);
13195 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13196 selected_window is redisplayed.
13198 We can return without actually redisplaying the window if
13199 fonts_changed_p is nonzero. In that case, redisplay_internal will
13200 retry. */
13202 static void
13203 redisplay_window (window, just_this_one_p)
13204 Lisp_Object window;
13205 int just_this_one_p;
13207 struct window *w = XWINDOW (window);
13208 struct frame *f = XFRAME (w->frame);
13209 struct buffer *buffer = XBUFFER (w->buffer);
13210 struct buffer *old = current_buffer;
13211 struct text_pos lpoint, opoint, startp;
13212 int update_mode_line;
13213 int tem;
13214 struct it it;
13215 /* Record it now because it's overwritten. */
13216 int current_matrix_up_to_date_p = 0;
13217 int used_current_matrix_p = 0;
13218 /* This is less strict than current_matrix_up_to_date_p.
13219 It indictes that the buffer contents and narrowing are unchanged. */
13220 int buffer_unchanged_p = 0;
13221 int temp_scroll_step = 0;
13222 int count = SPECPDL_INDEX ();
13223 int rc;
13224 int centering_position = -1;
13225 int last_line_misfit = 0;
13226 int beg_unchanged, end_unchanged;
13228 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13229 opoint = lpoint;
13231 /* W must be a leaf window here. */
13232 xassert (!NILP (w->buffer));
13233 #if GLYPH_DEBUG
13234 *w->desired_matrix->method = 0;
13235 #endif
13237 restart:
13238 reconsider_clip_changes (w, buffer);
13240 /* Has the mode line to be updated? */
13241 update_mode_line = (!NILP (w->update_mode_line)
13242 || update_mode_lines
13243 || buffer->clip_changed
13244 || buffer->prevent_redisplay_optimizations_p);
13246 if (MINI_WINDOW_P (w))
13248 if (w == XWINDOW (echo_area_window)
13249 && !NILP (echo_area_buffer[0]))
13251 if (update_mode_line)
13252 /* We may have to update a tty frame's menu bar or a
13253 tool-bar. Example `M-x C-h C-h C-g'. */
13254 goto finish_menu_bars;
13255 else
13256 /* We've already displayed the echo area glyphs in this window. */
13257 goto finish_scroll_bars;
13259 else if ((w != XWINDOW (minibuf_window)
13260 || minibuf_level == 0)
13261 /* When buffer is nonempty, redisplay window normally. */
13262 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13263 /* Quail displays non-mini buffers in minibuffer window.
13264 In that case, redisplay the window normally. */
13265 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13267 /* W is a mini-buffer window, but it's not active, so clear
13268 it. */
13269 int yb = window_text_bottom_y (w);
13270 struct glyph_row *row;
13271 int y;
13273 for (y = 0, row = w->desired_matrix->rows;
13274 y < yb;
13275 y += row->height, ++row)
13276 blank_row (w, row, y);
13277 goto finish_scroll_bars;
13280 clear_glyph_matrix (w->desired_matrix);
13283 /* Otherwise set up data on this window; select its buffer and point
13284 value. */
13285 /* Really select the buffer, for the sake of buffer-local
13286 variables. */
13287 set_buffer_internal_1 (XBUFFER (w->buffer));
13289 current_matrix_up_to_date_p
13290 = (!NILP (w->window_end_valid)
13291 && !current_buffer->clip_changed
13292 && !current_buffer->prevent_redisplay_optimizations_p
13293 && XFASTINT (w->last_modified) >= MODIFF
13294 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13296 /* Run the window-bottom-change-functions
13297 if it is possible that the text on the screen has changed
13298 (either due to modification of the text, or any other reason). */
13299 if (!current_matrix_up_to_date_p
13300 && !NILP (Vwindow_text_change_functions))
13302 safe_run_hooks (Qwindow_text_change_functions);
13303 goto restart;
13306 beg_unchanged = BEG_UNCHANGED;
13307 end_unchanged = END_UNCHANGED;
13309 SET_TEXT_POS (opoint, PT, PT_BYTE);
13311 specbind (Qinhibit_point_motion_hooks, Qt);
13313 buffer_unchanged_p
13314 = (!NILP (w->window_end_valid)
13315 && !current_buffer->clip_changed
13316 && XFASTINT (w->last_modified) >= MODIFF
13317 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13319 /* When windows_or_buffers_changed is non-zero, we can't rely on
13320 the window end being valid, so set it to nil there. */
13321 if (windows_or_buffers_changed)
13323 /* If window starts on a continuation line, maybe adjust the
13324 window start in case the window's width changed. */
13325 if (XMARKER (w->start)->buffer == current_buffer)
13326 compute_window_start_on_continuation_line (w);
13328 w->window_end_valid = Qnil;
13331 /* Some sanity checks. */
13332 CHECK_WINDOW_END (w);
13333 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13334 abort ();
13335 if (BYTEPOS (opoint) < CHARPOS (opoint))
13336 abort ();
13338 /* If %c is in mode line, update it if needed. */
13339 if (!NILP (w->column_number_displayed)
13340 /* This alternative quickly identifies a common case
13341 where no change is needed. */
13342 && !(PT == XFASTINT (w->last_point)
13343 && XFASTINT (w->last_modified) >= MODIFF
13344 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13345 && (XFASTINT (w->column_number_displayed)
13346 != (int) current_column ())) /* iftc */
13347 update_mode_line = 1;
13349 /* Count number of windows showing the selected buffer. An indirect
13350 buffer counts as its base buffer. */
13351 if (!just_this_one_p)
13353 struct buffer *current_base, *window_base;
13354 current_base = current_buffer;
13355 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13356 if (current_base->base_buffer)
13357 current_base = current_base->base_buffer;
13358 if (window_base->base_buffer)
13359 window_base = window_base->base_buffer;
13360 if (current_base == window_base)
13361 buffer_shared++;
13364 /* Point refers normally to the selected window. For any other
13365 window, set up appropriate value. */
13366 if (!EQ (window, selected_window))
13368 int new_pt = XMARKER (w->pointm)->charpos;
13369 int new_pt_byte = marker_byte_position (w->pointm);
13370 if (new_pt < BEGV)
13372 new_pt = BEGV;
13373 new_pt_byte = BEGV_BYTE;
13374 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13376 else if (new_pt > (ZV - 1))
13378 new_pt = ZV;
13379 new_pt_byte = ZV_BYTE;
13380 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13383 /* We don't use SET_PT so that the point-motion hooks don't run. */
13384 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13387 /* If any of the character widths specified in the display table
13388 have changed, invalidate the width run cache. It's true that
13389 this may be a bit late to catch such changes, but the rest of
13390 redisplay goes (non-fatally) haywire when the display table is
13391 changed, so why should we worry about doing any better? */
13392 if (current_buffer->width_run_cache)
13394 struct Lisp_Char_Table *disptab = buffer_display_table ();
13396 if (! disptab_matches_widthtab (disptab,
13397 XVECTOR (current_buffer->width_table)))
13399 invalidate_region_cache (current_buffer,
13400 current_buffer->width_run_cache,
13401 BEG, Z);
13402 recompute_width_table (current_buffer, disptab);
13406 /* If window-start is screwed up, choose a new one. */
13407 if (XMARKER (w->start)->buffer != current_buffer)
13408 goto recenter;
13410 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13412 /* If someone specified a new starting point but did not insist,
13413 check whether it can be used. */
13414 if (!NILP (w->optional_new_start)
13415 && CHARPOS (startp) >= BEGV
13416 && CHARPOS (startp) <= ZV)
13418 w->optional_new_start = Qnil;
13419 start_display (&it, w, startp);
13420 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13421 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13422 if (IT_CHARPOS (it) == PT)
13423 w->force_start = Qt;
13424 /* IT may overshoot PT if text at PT is invisible. */
13425 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13426 w->force_start = Qt;
13429 force_start:
13431 /* Handle case where place to start displaying has been specified,
13432 unless the specified location is outside the accessible range. */
13433 if (!NILP (w->force_start)
13434 || w->frozen_window_start_p)
13436 /* We set this later on if we have to adjust point. */
13437 int new_vpos = -1;
13439 w->force_start = Qnil;
13440 w->vscroll = 0;
13441 w->window_end_valid = Qnil;
13443 /* Forget any recorded base line for line number display. */
13444 if (!buffer_unchanged_p)
13445 w->base_line_number = Qnil;
13447 /* Redisplay the mode line. Select the buffer properly for that.
13448 Also, run the hook window-scroll-functions
13449 because we have scrolled. */
13450 /* Note, we do this after clearing force_start because
13451 if there's an error, it is better to forget about force_start
13452 than to get into an infinite loop calling the hook functions
13453 and having them get more errors. */
13454 if (!update_mode_line
13455 || ! NILP (Vwindow_scroll_functions))
13457 update_mode_line = 1;
13458 w->update_mode_line = Qt;
13459 startp = run_window_scroll_functions (window, startp);
13462 w->last_modified = make_number (0);
13463 w->last_overlay_modified = make_number (0);
13464 if (CHARPOS (startp) < BEGV)
13465 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13466 else if (CHARPOS (startp) > ZV)
13467 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13469 /* Redisplay, then check if cursor has been set during the
13470 redisplay. Give up if new fonts were loaded. */
13471 /* We used to issue a CHECK_MARGINS argument to try_window here,
13472 but this causes scrolling to fail when point begins inside
13473 the scroll margin (bug#148) -- cyd */
13474 if (!try_window (window, startp, 0))
13476 w->force_start = Qt;
13477 clear_glyph_matrix (w->desired_matrix);
13478 goto need_larger_matrices;
13481 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13483 /* If point does not appear, try to move point so it does
13484 appear. The desired matrix has been built above, so we
13485 can use it here. */
13486 new_vpos = window_box_height (w) / 2;
13489 if (!cursor_row_fully_visible_p (w, 0, 0))
13491 /* Point does appear, but on a line partly visible at end of window.
13492 Move it back to a fully-visible line. */
13493 new_vpos = window_box_height (w);
13496 /* If we need to move point for either of the above reasons,
13497 now actually do it. */
13498 if (new_vpos >= 0)
13500 struct glyph_row *row;
13502 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13503 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13504 ++row;
13506 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13507 MATRIX_ROW_START_BYTEPOS (row));
13509 if (w != XWINDOW (selected_window))
13510 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13511 else if (current_buffer == old)
13512 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13514 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13516 /* If we are highlighting the region, then we just changed
13517 the region, so redisplay to show it. */
13518 if (!NILP (Vtransient_mark_mode)
13519 && !NILP (current_buffer->mark_active))
13521 clear_glyph_matrix (w->desired_matrix);
13522 if (!try_window (window, startp, 0))
13523 goto need_larger_matrices;
13527 #if GLYPH_DEBUG
13528 debug_method_add (w, "forced window start");
13529 #endif
13530 goto done;
13533 /* Handle case where text has not changed, only point, and it has
13534 not moved off the frame, and we are not retrying after hscroll.
13535 (current_matrix_up_to_date_p is nonzero when retrying.) */
13536 if (current_matrix_up_to_date_p
13537 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13538 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13540 switch (rc)
13542 case CURSOR_MOVEMENT_SUCCESS:
13543 used_current_matrix_p = 1;
13544 goto done;
13546 case CURSOR_MOVEMENT_MUST_SCROLL:
13547 goto try_to_scroll;
13549 default:
13550 abort ();
13553 /* If current starting point was originally the beginning of a line
13554 but no longer is, find a new starting point. */
13555 else if (!NILP (w->start_at_line_beg)
13556 && !(CHARPOS (startp) <= BEGV
13557 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13559 #if GLYPH_DEBUG
13560 debug_method_add (w, "recenter 1");
13561 #endif
13562 goto recenter;
13565 /* Try scrolling with try_window_id. Value is > 0 if update has
13566 been done, it is -1 if we know that the same window start will
13567 not work. It is 0 if unsuccessful for some other reason. */
13568 else if ((tem = try_window_id (w)) != 0)
13570 #if GLYPH_DEBUG
13571 debug_method_add (w, "try_window_id %d", tem);
13572 #endif
13574 if (fonts_changed_p)
13575 goto need_larger_matrices;
13576 if (tem > 0)
13577 goto done;
13579 /* Otherwise try_window_id has returned -1 which means that we
13580 don't want the alternative below this comment to execute. */
13582 else if (CHARPOS (startp) >= BEGV
13583 && CHARPOS (startp) <= ZV
13584 && PT >= CHARPOS (startp)
13585 && (CHARPOS (startp) < ZV
13586 /* Avoid starting at end of buffer. */
13587 || CHARPOS (startp) == BEGV
13588 || (XFASTINT (w->last_modified) >= MODIFF
13589 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13592 /* If first window line is a continuation line, and window start
13593 is inside the modified region, but the first change is before
13594 current window start, we must select a new window start.
13596 However, if this is the result of a down-mouse event (e.g. by
13597 extending the mouse-drag-overlay), we don't want to select a
13598 new window start, since that would change the position under
13599 the mouse, resulting in an unwanted mouse-movement rather
13600 than a simple mouse-click. */
13601 if (NILP (w->start_at_line_beg)
13602 && NILP (do_mouse_tracking)
13603 && CHARPOS (startp) > BEGV
13604 && CHARPOS (startp) > BEG + beg_unchanged
13605 && CHARPOS (startp) <= Z - end_unchanged
13606 /* Even if w->start_at_line_beg is nil, a new window may
13607 start at a line_beg, since that's how set_buffer_window
13608 sets it. So, we need to check the return value of
13609 compute_window_start_on_continuation_line. (See also
13610 bug#197). */
13611 && XMARKER (w->start)->buffer == current_buffer
13612 && compute_window_start_on_continuation_line (w))
13614 w->force_start = Qt;
13615 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13616 goto force_start;
13619 #if GLYPH_DEBUG
13620 debug_method_add (w, "same window start");
13621 #endif
13623 /* Try to redisplay starting at same place as before.
13624 If point has not moved off frame, accept the results. */
13625 if (!current_matrix_up_to_date_p
13626 /* Don't use try_window_reusing_current_matrix in this case
13627 because a window scroll function can have changed the
13628 buffer. */
13629 || !NILP (Vwindow_scroll_functions)
13630 || MINI_WINDOW_P (w)
13631 || !(used_current_matrix_p
13632 = try_window_reusing_current_matrix (w)))
13634 IF_DEBUG (debug_method_add (w, "1"));
13635 if (try_window (window, startp, 1) < 0)
13636 /* -1 means we need to scroll.
13637 0 means we need new matrices, but fonts_changed_p
13638 is set in that case, so we will detect it below. */
13639 goto try_to_scroll;
13642 if (fonts_changed_p)
13643 goto need_larger_matrices;
13645 if (w->cursor.vpos >= 0)
13647 if (!just_this_one_p
13648 || current_buffer->clip_changed
13649 || BEG_UNCHANGED < CHARPOS (startp))
13650 /* Forget any recorded base line for line number display. */
13651 w->base_line_number = Qnil;
13653 if (!cursor_row_fully_visible_p (w, 1, 0))
13655 clear_glyph_matrix (w->desired_matrix);
13656 last_line_misfit = 1;
13658 /* Drop through and scroll. */
13659 else
13660 goto done;
13662 else
13663 clear_glyph_matrix (w->desired_matrix);
13666 try_to_scroll:
13668 w->last_modified = make_number (0);
13669 w->last_overlay_modified = make_number (0);
13671 /* Redisplay the mode line. Select the buffer properly for that. */
13672 if (!update_mode_line)
13674 update_mode_line = 1;
13675 w->update_mode_line = Qt;
13678 /* Try to scroll by specified few lines. */
13679 if ((scroll_conservatively
13680 || scroll_step
13681 || temp_scroll_step
13682 || NUMBERP (current_buffer->scroll_up_aggressively)
13683 || NUMBERP (current_buffer->scroll_down_aggressively))
13684 && !current_buffer->clip_changed
13685 && CHARPOS (startp) >= BEGV
13686 && CHARPOS (startp) <= ZV)
13688 /* The function returns -1 if new fonts were loaded, 1 if
13689 successful, 0 if not successful. */
13690 int rc = try_scrolling (window, just_this_one_p,
13691 scroll_conservatively,
13692 scroll_step,
13693 temp_scroll_step, last_line_misfit);
13694 switch (rc)
13696 case SCROLLING_SUCCESS:
13697 goto done;
13699 case SCROLLING_NEED_LARGER_MATRICES:
13700 goto need_larger_matrices;
13702 case SCROLLING_FAILED:
13703 break;
13705 default:
13706 abort ();
13710 /* Finally, just choose place to start which centers point */
13712 recenter:
13713 if (centering_position < 0)
13714 centering_position = window_box_height (w) / 2;
13716 #if GLYPH_DEBUG
13717 debug_method_add (w, "recenter");
13718 #endif
13720 /* w->vscroll = 0; */
13722 /* Forget any previously recorded base line for line number display. */
13723 if (!buffer_unchanged_p)
13724 w->base_line_number = Qnil;
13726 /* Move backward half the height of the window. */
13727 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13728 it.current_y = it.last_visible_y;
13729 move_it_vertically_backward (&it, centering_position);
13730 xassert (IT_CHARPOS (it) >= BEGV);
13732 /* The function move_it_vertically_backward may move over more
13733 than the specified y-distance. If it->w is small, e.g. a
13734 mini-buffer window, we may end up in front of the window's
13735 display area. Start displaying at the start of the line
13736 containing PT in this case. */
13737 if (it.current_y <= 0)
13739 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13740 move_it_vertically_backward (&it, 0);
13741 it.current_y = 0;
13744 it.current_x = it.hpos = 0;
13746 /* Set startp here explicitly in case that helps avoid an infinite loop
13747 in case the window-scroll-functions functions get errors. */
13748 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13750 /* Run scroll hooks. */
13751 startp = run_window_scroll_functions (window, it.current.pos);
13753 /* Redisplay the window. */
13754 if (!current_matrix_up_to_date_p
13755 || windows_or_buffers_changed
13756 || cursor_type_changed
13757 /* Don't use try_window_reusing_current_matrix in this case
13758 because it can have changed the buffer. */
13759 || !NILP (Vwindow_scroll_functions)
13760 || !just_this_one_p
13761 || MINI_WINDOW_P (w)
13762 || !(used_current_matrix_p
13763 = try_window_reusing_current_matrix (w)))
13764 try_window (window, startp, 0);
13766 /* If new fonts have been loaded (due to fontsets), give up. We
13767 have to start a new redisplay since we need to re-adjust glyph
13768 matrices. */
13769 if (fonts_changed_p)
13770 goto need_larger_matrices;
13772 /* If cursor did not appear assume that the middle of the window is
13773 in the first line of the window. Do it again with the next line.
13774 (Imagine a window of height 100, displaying two lines of height
13775 60. Moving back 50 from it->last_visible_y will end in the first
13776 line.) */
13777 if (w->cursor.vpos < 0)
13779 if (!NILP (w->window_end_valid)
13780 && PT >= Z - XFASTINT (w->window_end_pos))
13782 clear_glyph_matrix (w->desired_matrix);
13783 move_it_by_lines (&it, 1, 0);
13784 try_window (window, it.current.pos, 0);
13786 else if (PT < IT_CHARPOS (it))
13788 clear_glyph_matrix (w->desired_matrix);
13789 move_it_by_lines (&it, -1, 0);
13790 try_window (window, it.current.pos, 0);
13792 else
13794 /* Not much we can do about it. */
13798 /* Consider the following case: Window starts at BEGV, there is
13799 invisible, intangible text at BEGV, so that display starts at
13800 some point START > BEGV. It can happen that we are called with
13801 PT somewhere between BEGV and START. Try to handle that case. */
13802 if (w->cursor.vpos < 0)
13804 struct glyph_row *row = w->current_matrix->rows;
13805 if (row->mode_line_p)
13806 ++row;
13807 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13810 if (!cursor_row_fully_visible_p (w, 0, 0))
13812 /* If vscroll is enabled, disable it and try again. */
13813 if (w->vscroll)
13815 w->vscroll = 0;
13816 clear_glyph_matrix (w->desired_matrix);
13817 goto recenter;
13820 /* If centering point failed to make the whole line visible,
13821 put point at the top instead. That has to make the whole line
13822 visible, if it can be done. */
13823 if (centering_position == 0)
13824 goto done;
13826 clear_glyph_matrix (w->desired_matrix);
13827 centering_position = 0;
13828 goto recenter;
13831 done:
13833 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13834 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13835 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13836 ? Qt : Qnil);
13838 /* Display the mode line, if we must. */
13839 if ((update_mode_line
13840 /* If window not full width, must redo its mode line
13841 if (a) the window to its side is being redone and
13842 (b) we do a frame-based redisplay. This is a consequence
13843 of how inverted lines are drawn in frame-based redisplay. */
13844 || (!just_this_one_p
13845 && !FRAME_WINDOW_P (f)
13846 && !WINDOW_FULL_WIDTH_P (w))
13847 /* Line number to display. */
13848 || INTEGERP (w->base_line_pos)
13849 /* Column number is displayed and different from the one displayed. */
13850 || (!NILP (w->column_number_displayed)
13851 && (XFASTINT (w->column_number_displayed)
13852 != (int) current_column ()))) /* iftc */
13853 /* This means that the window has a mode line. */
13854 && (WINDOW_WANTS_MODELINE_P (w)
13855 || WINDOW_WANTS_HEADER_LINE_P (w)))
13857 display_mode_lines (w);
13859 /* If mode line height has changed, arrange for a thorough
13860 immediate redisplay using the correct mode line height. */
13861 if (WINDOW_WANTS_MODELINE_P (w)
13862 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13864 fonts_changed_p = 1;
13865 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13866 = DESIRED_MODE_LINE_HEIGHT (w);
13869 /* If top line height has changed, arrange for a thorough
13870 immediate redisplay using the correct mode line height. */
13871 if (WINDOW_WANTS_HEADER_LINE_P (w)
13872 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13874 fonts_changed_p = 1;
13875 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13876 = DESIRED_HEADER_LINE_HEIGHT (w);
13879 if (fonts_changed_p)
13880 goto need_larger_matrices;
13883 if (!line_number_displayed
13884 && !BUFFERP (w->base_line_pos))
13886 w->base_line_pos = Qnil;
13887 w->base_line_number = Qnil;
13890 finish_menu_bars:
13892 /* When we reach a frame's selected window, redo the frame's menu bar. */
13893 if (update_mode_line
13894 && EQ (FRAME_SELECTED_WINDOW (f), window))
13896 int redisplay_menu_p = 0;
13897 int redisplay_tool_bar_p = 0;
13899 if (FRAME_WINDOW_P (f))
13901 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13902 || defined (HAVE_NS) || defined (USE_GTK)
13903 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13904 #else
13905 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13906 #endif
13908 else
13909 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13911 if (redisplay_menu_p)
13912 display_menu_bar (w);
13914 #ifdef HAVE_WINDOW_SYSTEM
13915 if (FRAME_WINDOW_P (f))
13917 #if defined (USE_GTK) || defined (HAVE_NS)
13918 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13919 #else
13920 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13921 && (FRAME_TOOL_BAR_LINES (f) > 0
13922 || !NILP (Vauto_resize_tool_bars));
13923 #endif
13925 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13927 extern int ignore_mouse_drag_p;
13928 ignore_mouse_drag_p = 1;
13931 #endif
13934 #ifdef HAVE_WINDOW_SYSTEM
13935 if (FRAME_WINDOW_P (f)
13936 && update_window_fringes (w, (just_this_one_p
13937 || (!used_current_matrix_p && !overlay_arrow_seen)
13938 || w->pseudo_window_p)))
13940 update_begin (f);
13941 BLOCK_INPUT;
13942 if (draw_window_fringes (w, 1))
13943 x_draw_vertical_border (w);
13944 UNBLOCK_INPUT;
13945 update_end (f);
13947 #endif /* HAVE_WINDOW_SYSTEM */
13949 /* We go to this label, with fonts_changed_p nonzero,
13950 if it is necessary to try again using larger glyph matrices.
13951 We have to redeem the scroll bar even in this case,
13952 because the loop in redisplay_internal expects that. */
13953 need_larger_matrices:
13955 finish_scroll_bars:
13957 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13959 /* Set the thumb's position and size. */
13960 set_vertical_scroll_bar (w);
13962 /* Note that we actually used the scroll bar attached to this
13963 window, so it shouldn't be deleted at the end of redisplay. */
13964 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13965 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13968 /* Restore current_buffer and value of point in it. */
13969 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13970 set_buffer_internal_1 (old);
13971 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13972 shorter. This can be caused by log truncation in *Messages*. */
13973 if (CHARPOS (lpoint) <= ZV)
13974 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13976 unbind_to (count, Qnil);
13980 /* Build the complete desired matrix of WINDOW with a window start
13981 buffer position POS.
13983 Value is 1 if successful. It is zero if fonts were loaded during
13984 redisplay which makes re-adjusting glyph matrices necessary, and -1
13985 if point would appear in the scroll margins.
13986 (We check that only if CHECK_MARGINS is nonzero. */
13989 try_window (window, pos, check_margins)
13990 Lisp_Object window;
13991 struct text_pos pos;
13992 int check_margins;
13994 struct window *w = XWINDOW (window);
13995 struct it it;
13996 struct glyph_row *last_text_row = NULL;
13997 struct frame *f = XFRAME (w->frame);
13999 /* Make POS the new window start. */
14000 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14002 /* Mark cursor position as unknown. No overlay arrow seen. */
14003 w->cursor.vpos = -1;
14004 overlay_arrow_seen = 0;
14006 /* Initialize iterator and info to start at POS. */
14007 start_display (&it, w, pos);
14009 /* Display all lines of W. */
14010 while (it.current_y < it.last_visible_y)
14012 if (display_line (&it))
14013 last_text_row = it.glyph_row - 1;
14014 if (fonts_changed_p)
14015 return 0;
14018 /* Don't let the cursor end in the scroll margins. */
14019 if (check_margins
14020 && !MINI_WINDOW_P (w))
14022 int this_scroll_margin;
14024 if (scroll_margin > 0)
14026 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14027 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14029 else
14030 this_scroll_margin = 0;
14032 if ((w->cursor.y >= 0 /* not vscrolled */
14033 && w->cursor.y < this_scroll_margin
14034 && CHARPOS (pos) > BEGV
14035 && IT_CHARPOS (it) < ZV)
14036 /* rms: considering make_cursor_line_fully_visible_p here
14037 seems to give wrong results. We don't want to recenter
14038 when the last line is partly visible, we want to allow
14039 that case to be handled in the usual way. */
14040 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14042 w->cursor.vpos = -1;
14043 clear_glyph_matrix (w->desired_matrix);
14044 return -1;
14048 /* If bottom moved off end of frame, change mode line percentage. */
14049 if (XFASTINT (w->window_end_pos) <= 0
14050 && Z != IT_CHARPOS (it))
14051 w->update_mode_line = Qt;
14053 /* Set window_end_pos to the offset of the last character displayed
14054 on the window from the end of current_buffer. Set
14055 window_end_vpos to its row number. */
14056 if (last_text_row)
14058 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14059 w->window_end_bytepos
14060 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14061 w->window_end_pos
14062 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14063 w->window_end_vpos
14064 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14065 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14066 ->displays_text_p);
14068 else
14070 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14071 w->window_end_pos = make_number (Z - ZV);
14072 w->window_end_vpos = make_number (0);
14075 /* But that is not valid info until redisplay finishes. */
14076 w->window_end_valid = Qnil;
14077 return 1;
14082 /************************************************************************
14083 Window redisplay reusing current matrix when buffer has not changed
14084 ************************************************************************/
14086 /* Try redisplay of window W showing an unchanged buffer with a
14087 different window start than the last time it was displayed by
14088 reusing its current matrix. Value is non-zero if successful.
14089 W->start is the new window start. */
14091 static int
14092 try_window_reusing_current_matrix (w)
14093 struct window *w;
14095 struct frame *f = XFRAME (w->frame);
14096 struct glyph_row *row, *bottom_row;
14097 struct it it;
14098 struct run run;
14099 struct text_pos start, new_start;
14100 int nrows_scrolled, i;
14101 struct glyph_row *last_text_row;
14102 struct glyph_row *last_reused_text_row;
14103 struct glyph_row *start_row;
14104 int start_vpos, min_y, max_y;
14106 #if GLYPH_DEBUG
14107 if (inhibit_try_window_reusing)
14108 return 0;
14109 #endif
14111 if (/* This function doesn't handle terminal frames. */
14112 !FRAME_WINDOW_P (f)
14113 /* Don't try to reuse the display if windows have been split
14114 or such. */
14115 || windows_or_buffers_changed
14116 || cursor_type_changed)
14117 return 0;
14119 /* Can't do this if region may have changed. */
14120 if ((!NILP (Vtransient_mark_mode)
14121 && !NILP (current_buffer->mark_active))
14122 || !NILP (w->region_showing)
14123 || !NILP (Vshow_trailing_whitespace))
14124 return 0;
14126 /* If top-line visibility has changed, give up. */
14127 if (WINDOW_WANTS_HEADER_LINE_P (w)
14128 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14129 return 0;
14131 /* Give up if old or new display is scrolled vertically. We could
14132 make this function handle this, but right now it doesn't. */
14133 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14134 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14135 return 0;
14137 /* The variable new_start now holds the new window start. The old
14138 start `start' can be determined from the current matrix. */
14139 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14140 start = start_row->start.pos;
14141 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14143 /* Clear the desired matrix for the display below. */
14144 clear_glyph_matrix (w->desired_matrix);
14146 if (CHARPOS (new_start) <= CHARPOS (start))
14148 int first_row_y;
14150 /* Don't use this method if the display starts with an ellipsis
14151 displayed for invisible text. It's not easy to handle that case
14152 below, and it's certainly not worth the effort since this is
14153 not a frequent case. */
14154 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14155 return 0;
14157 IF_DEBUG (debug_method_add (w, "twu1"));
14159 /* Display up to a row that can be reused. The variable
14160 last_text_row is set to the last row displayed that displays
14161 text. Note that it.vpos == 0 if or if not there is a
14162 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14163 start_display (&it, w, new_start);
14164 first_row_y = it.current_y;
14165 w->cursor.vpos = -1;
14166 last_text_row = last_reused_text_row = NULL;
14168 while (it.current_y < it.last_visible_y
14169 && !fonts_changed_p)
14171 /* If we have reached into the characters in the START row,
14172 that means the line boundaries have changed. So we
14173 can't start copying with the row START. Maybe it will
14174 work to start copying with the following row. */
14175 while (IT_CHARPOS (it) > CHARPOS (start))
14177 /* Advance to the next row as the "start". */
14178 start_row++;
14179 start = start_row->start.pos;
14180 /* If there are no more rows to try, or just one, give up. */
14181 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14182 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14183 || CHARPOS (start) == ZV)
14185 clear_glyph_matrix (w->desired_matrix);
14186 return 0;
14189 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14191 /* If we have reached alignment,
14192 we can copy the rest of the rows. */
14193 if (IT_CHARPOS (it) == CHARPOS (start))
14194 break;
14196 if (display_line (&it))
14197 last_text_row = it.glyph_row - 1;
14200 /* A value of current_y < last_visible_y means that we stopped
14201 at the previous window start, which in turn means that we
14202 have at least one reusable row. */
14203 if (it.current_y < it.last_visible_y)
14205 /* IT.vpos always starts from 0; it counts text lines. */
14206 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14208 /* Find PT if not already found in the lines displayed. */
14209 if (w->cursor.vpos < 0)
14211 int dy = it.current_y - start_row->y;
14213 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14214 row = row_containing_pos (w, PT, row, NULL, dy);
14215 if (row)
14216 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14217 dy, nrows_scrolled);
14218 else
14220 clear_glyph_matrix (w->desired_matrix);
14221 return 0;
14225 /* Scroll the display. Do it before the current matrix is
14226 changed. The problem here is that update has not yet
14227 run, i.e. part of the current matrix is not up to date.
14228 scroll_run_hook will clear the cursor, and use the
14229 current matrix to get the height of the row the cursor is
14230 in. */
14231 run.current_y = start_row->y;
14232 run.desired_y = it.current_y;
14233 run.height = it.last_visible_y - it.current_y;
14235 if (run.height > 0 && run.current_y != run.desired_y)
14237 update_begin (f);
14238 FRAME_RIF (f)->update_window_begin_hook (w);
14239 FRAME_RIF (f)->clear_window_mouse_face (w);
14240 FRAME_RIF (f)->scroll_run_hook (w, &run);
14241 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14242 update_end (f);
14245 /* Shift current matrix down by nrows_scrolled lines. */
14246 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14247 rotate_matrix (w->current_matrix,
14248 start_vpos,
14249 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14250 nrows_scrolled);
14252 /* Disable lines that must be updated. */
14253 for (i = 0; i < nrows_scrolled; ++i)
14254 (start_row + i)->enabled_p = 0;
14256 /* Re-compute Y positions. */
14257 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14258 max_y = it.last_visible_y;
14259 for (row = start_row + nrows_scrolled;
14260 row < bottom_row;
14261 ++row)
14263 row->y = it.current_y;
14264 row->visible_height = row->height;
14266 if (row->y < min_y)
14267 row->visible_height -= min_y - row->y;
14268 if (row->y + row->height > max_y)
14269 row->visible_height -= row->y + row->height - max_y;
14270 row->redraw_fringe_bitmaps_p = 1;
14272 it.current_y += row->height;
14274 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14275 last_reused_text_row = row;
14276 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14277 break;
14280 /* Disable lines in the current matrix which are now
14281 below the window. */
14282 for (++row; row < bottom_row; ++row)
14283 row->enabled_p = row->mode_line_p = 0;
14286 /* Update window_end_pos etc.; last_reused_text_row is the last
14287 reused row from the current matrix containing text, if any.
14288 The value of last_text_row is the last displayed line
14289 containing text. */
14290 if (last_reused_text_row)
14292 w->window_end_bytepos
14293 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14294 w->window_end_pos
14295 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14296 w->window_end_vpos
14297 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14298 w->current_matrix));
14300 else if (last_text_row)
14302 w->window_end_bytepos
14303 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14304 w->window_end_pos
14305 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14306 w->window_end_vpos
14307 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14309 else
14311 /* This window must be completely empty. */
14312 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14313 w->window_end_pos = make_number (Z - ZV);
14314 w->window_end_vpos = make_number (0);
14316 w->window_end_valid = Qnil;
14318 /* Update hint: don't try scrolling again in update_window. */
14319 w->desired_matrix->no_scrolling_p = 1;
14321 #if GLYPH_DEBUG
14322 debug_method_add (w, "try_window_reusing_current_matrix 1");
14323 #endif
14324 return 1;
14326 else if (CHARPOS (new_start) > CHARPOS (start))
14328 struct glyph_row *pt_row, *row;
14329 struct glyph_row *first_reusable_row;
14330 struct glyph_row *first_row_to_display;
14331 int dy;
14332 int yb = window_text_bottom_y (w);
14334 /* Find the row starting at new_start, if there is one. Don't
14335 reuse a partially visible line at the end. */
14336 first_reusable_row = start_row;
14337 while (first_reusable_row->enabled_p
14338 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14339 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14340 < CHARPOS (new_start)))
14341 ++first_reusable_row;
14343 /* Give up if there is no row to reuse. */
14344 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14345 || !first_reusable_row->enabled_p
14346 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14347 != CHARPOS (new_start)))
14348 return 0;
14350 /* We can reuse fully visible rows beginning with
14351 first_reusable_row to the end of the window. Set
14352 first_row_to_display to the first row that cannot be reused.
14353 Set pt_row to the row containing point, if there is any. */
14354 pt_row = NULL;
14355 for (first_row_to_display = first_reusable_row;
14356 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14357 ++first_row_to_display)
14359 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14360 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14361 pt_row = first_row_to_display;
14364 /* Start displaying at the start of first_row_to_display. */
14365 xassert (first_row_to_display->y < yb);
14366 init_to_row_start (&it, w, first_row_to_display);
14368 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14369 - start_vpos);
14370 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14371 - nrows_scrolled);
14372 it.current_y = (first_row_to_display->y - first_reusable_row->y
14373 + WINDOW_HEADER_LINE_HEIGHT (w));
14375 /* Display lines beginning with first_row_to_display in the
14376 desired matrix. Set last_text_row to the last row displayed
14377 that displays text. */
14378 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14379 if (pt_row == NULL)
14380 w->cursor.vpos = -1;
14381 last_text_row = NULL;
14382 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14383 if (display_line (&it))
14384 last_text_row = it.glyph_row - 1;
14386 /* If point is in a reused row, adjust y and vpos of the cursor
14387 position. */
14388 if (pt_row)
14390 w->cursor.vpos -= nrows_scrolled;
14391 w->cursor.y -= first_reusable_row->y - start_row->y;
14394 /* Give up if point isn't in a row displayed or reused. (This
14395 also handles the case where w->cursor.vpos < nrows_scrolled
14396 after the calls to display_line, which can happen with scroll
14397 margins. See bug#1295.) */
14398 if (w->cursor.vpos < 0)
14400 clear_glyph_matrix (w->desired_matrix);
14401 return 0;
14404 /* Scroll the display. */
14405 run.current_y = first_reusable_row->y;
14406 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14407 run.height = it.last_visible_y - run.current_y;
14408 dy = run.current_y - run.desired_y;
14410 if (run.height)
14412 update_begin (f);
14413 FRAME_RIF (f)->update_window_begin_hook (w);
14414 FRAME_RIF (f)->clear_window_mouse_face (w);
14415 FRAME_RIF (f)->scroll_run_hook (w, &run);
14416 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14417 update_end (f);
14420 /* Adjust Y positions of reused rows. */
14421 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14422 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14423 max_y = it.last_visible_y;
14424 for (row = first_reusable_row; row < first_row_to_display; ++row)
14426 row->y -= dy;
14427 row->visible_height = row->height;
14428 if (row->y < min_y)
14429 row->visible_height -= min_y - row->y;
14430 if (row->y + row->height > max_y)
14431 row->visible_height -= row->y + row->height - max_y;
14432 row->redraw_fringe_bitmaps_p = 1;
14435 /* Scroll the current matrix. */
14436 xassert (nrows_scrolled > 0);
14437 rotate_matrix (w->current_matrix,
14438 start_vpos,
14439 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14440 -nrows_scrolled);
14442 /* Disable rows not reused. */
14443 for (row -= nrows_scrolled; row < bottom_row; ++row)
14444 row->enabled_p = 0;
14446 /* Point may have moved to a different line, so we cannot assume that
14447 the previous cursor position is valid; locate the correct row. */
14448 if (pt_row)
14450 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14451 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14452 row++)
14454 w->cursor.vpos++;
14455 w->cursor.y = row->y;
14457 if (row < bottom_row)
14459 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14460 struct glyph *end = glyph + row->used[TEXT_AREA];
14462 for (; glyph < end
14463 && (!BUFFERP (glyph->object)
14464 || glyph->charpos < PT);
14465 glyph++)
14467 w->cursor.hpos++;
14468 w->cursor.x += glyph->pixel_width;
14473 /* Adjust window end. A null value of last_text_row means that
14474 the window end is in reused rows which in turn means that
14475 only its vpos can have changed. */
14476 if (last_text_row)
14478 w->window_end_bytepos
14479 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14480 w->window_end_pos
14481 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14482 w->window_end_vpos
14483 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14485 else
14487 w->window_end_vpos
14488 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14491 w->window_end_valid = Qnil;
14492 w->desired_matrix->no_scrolling_p = 1;
14494 #if GLYPH_DEBUG
14495 debug_method_add (w, "try_window_reusing_current_matrix 2");
14496 #endif
14497 return 1;
14500 return 0;
14505 /************************************************************************
14506 Window redisplay reusing current matrix when buffer has changed
14507 ************************************************************************/
14509 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14510 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14511 int *, int *));
14512 static struct glyph_row *
14513 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14514 struct glyph_row *));
14517 /* Return the last row in MATRIX displaying text. If row START is
14518 non-null, start searching with that row. IT gives the dimensions
14519 of the display. Value is null if matrix is empty; otherwise it is
14520 a pointer to the row found. */
14522 static struct glyph_row *
14523 find_last_row_displaying_text (matrix, it, start)
14524 struct glyph_matrix *matrix;
14525 struct it *it;
14526 struct glyph_row *start;
14528 struct glyph_row *row, *row_found;
14530 /* Set row_found to the last row in IT->w's current matrix
14531 displaying text. The loop looks funny but think of partially
14532 visible lines. */
14533 row_found = NULL;
14534 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14535 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14537 xassert (row->enabled_p);
14538 row_found = row;
14539 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14540 break;
14541 ++row;
14544 return row_found;
14548 /* Return the last row in the current matrix of W that is not affected
14549 by changes at the start of current_buffer that occurred since W's
14550 current matrix was built. Value is null if no such row exists.
14552 BEG_UNCHANGED us the number of characters unchanged at the start of
14553 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14554 first changed character in current_buffer. Characters at positions <
14555 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14556 when the current matrix was built. */
14558 static struct glyph_row *
14559 find_last_unchanged_at_beg_row (w)
14560 struct window *w;
14562 int first_changed_pos = BEG + BEG_UNCHANGED;
14563 struct glyph_row *row;
14564 struct glyph_row *row_found = NULL;
14565 int yb = window_text_bottom_y (w);
14567 /* Find the last row displaying unchanged text. */
14568 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14569 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14570 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14571 ++row)
14573 if (/* If row ends before first_changed_pos, it is unchanged,
14574 except in some case. */
14575 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14576 /* When row ends in ZV and we write at ZV it is not
14577 unchanged. */
14578 && !row->ends_at_zv_p
14579 /* When first_changed_pos is the end of a continued line,
14580 row is not unchanged because it may be no longer
14581 continued. */
14582 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14583 && (row->continued_p
14584 || row->exact_window_width_line_p)))
14585 row_found = row;
14587 /* Stop if last visible row. */
14588 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14589 break;
14592 return row_found;
14596 /* Find the first glyph row in the current matrix of W that is not
14597 affected by changes at the end of current_buffer since the
14598 time W's current matrix was built.
14600 Return in *DELTA the number of chars by which buffer positions in
14601 unchanged text at the end of current_buffer must be adjusted.
14603 Return in *DELTA_BYTES the corresponding number of bytes.
14605 Value is null if no such row exists, i.e. all rows are affected by
14606 changes. */
14608 static struct glyph_row *
14609 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14610 struct window *w;
14611 int *delta, *delta_bytes;
14613 struct glyph_row *row;
14614 struct glyph_row *row_found = NULL;
14616 *delta = *delta_bytes = 0;
14618 /* Display must not have been paused, otherwise the current matrix
14619 is not up to date. */
14620 eassert (!NILP (w->window_end_valid));
14622 /* A value of window_end_pos >= END_UNCHANGED means that the window
14623 end is in the range of changed text. If so, there is no
14624 unchanged row at the end of W's current matrix. */
14625 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14626 return NULL;
14628 /* Set row to the last row in W's current matrix displaying text. */
14629 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14631 /* If matrix is entirely empty, no unchanged row exists. */
14632 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14634 /* The value of row is the last glyph row in the matrix having a
14635 meaningful buffer position in it. The end position of row
14636 corresponds to window_end_pos. This allows us to translate
14637 buffer positions in the current matrix to current buffer
14638 positions for characters not in changed text. */
14639 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14640 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14641 int last_unchanged_pos, last_unchanged_pos_old;
14642 struct glyph_row *first_text_row
14643 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14645 *delta = Z - Z_old;
14646 *delta_bytes = Z_BYTE - Z_BYTE_old;
14648 /* Set last_unchanged_pos to the buffer position of the last
14649 character in the buffer that has not been changed. Z is the
14650 index + 1 of the last character in current_buffer, i.e. by
14651 subtracting END_UNCHANGED we get the index of the last
14652 unchanged character, and we have to add BEG to get its buffer
14653 position. */
14654 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14655 last_unchanged_pos_old = last_unchanged_pos - *delta;
14657 /* Search backward from ROW for a row displaying a line that
14658 starts at a minimum position >= last_unchanged_pos_old. */
14659 for (; row > first_text_row; --row)
14661 /* This used to abort, but it can happen.
14662 It is ok to just stop the search instead here. KFS. */
14663 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14664 break;
14666 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14667 row_found = row;
14671 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14673 return row_found;
14677 /* Make sure that glyph rows in the current matrix of window W
14678 reference the same glyph memory as corresponding rows in the
14679 frame's frame matrix. This function is called after scrolling W's
14680 current matrix on a terminal frame in try_window_id and
14681 try_window_reusing_current_matrix. */
14683 static void
14684 sync_frame_with_window_matrix_rows (w)
14685 struct window *w;
14687 struct frame *f = XFRAME (w->frame);
14688 struct glyph_row *window_row, *window_row_end, *frame_row;
14690 /* Preconditions: W must be a leaf window and full-width. Its frame
14691 must have a frame matrix. */
14692 xassert (NILP (w->hchild) && NILP (w->vchild));
14693 xassert (WINDOW_FULL_WIDTH_P (w));
14694 xassert (!FRAME_WINDOW_P (f));
14696 /* If W is a full-width window, glyph pointers in W's current matrix
14697 have, by definition, to be the same as glyph pointers in the
14698 corresponding frame matrix. Note that frame matrices have no
14699 marginal areas (see build_frame_matrix). */
14700 window_row = w->current_matrix->rows;
14701 window_row_end = window_row + w->current_matrix->nrows;
14702 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14703 while (window_row < window_row_end)
14705 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14706 struct glyph *end = window_row->glyphs[LAST_AREA];
14708 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14709 frame_row->glyphs[TEXT_AREA] = start;
14710 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14711 frame_row->glyphs[LAST_AREA] = end;
14713 /* Disable frame rows whose corresponding window rows have
14714 been disabled in try_window_id. */
14715 if (!window_row->enabled_p)
14716 frame_row->enabled_p = 0;
14718 ++window_row, ++frame_row;
14723 /* Find the glyph row in window W containing CHARPOS. Consider all
14724 rows between START and END (not inclusive). END null means search
14725 all rows to the end of the display area of W. Value is the row
14726 containing CHARPOS or null. */
14728 struct glyph_row *
14729 row_containing_pos (w, charpos, start, end, dy)
14730 struct window *w;
14731 int charpos;
14732 struct glyph_row *start, *end;
14733 int dy;
14735 struct glyph_row *row = start;
14736 int last_y;
14738 /* If we happen to start on a header-line, skip that. */
14739 if (row->mode_line_p)
14740 ++row;
14742 if ((end && row >= end) || !row->enabled_p)
14743 return NULL;
14745 last_y = window_text_bottom_y (w) - dy;
14747 while (1)
14749 /* Give up if we have gone too far. */
14750 if (end && row >= end)
14751 return NULL;
14752 /* This formerly returned if they were equal.
14753 I think that both quantities are of a "last plus one" type;
14754 if so, when they are equal, the row is within the screen. -- rms. */
14755 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14756 return NULL;
14758 /* If it is in this row, return this row. */
14759 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14760 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14761 /* The end position of a row equals the start
14762 position of the next row. If CHARPOS is there, we
14763 would rather display it in the next line, except
14764 when this line ends in ZV. */
14765 && !row->ends_at_zv_p
14766 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14767 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14768 return row;
14769 ++row;
14774 /* Try to redisplay window W by reusing its existing display. W's
14775 current matrix must be up to date when this function is called,
14776 i.e. window_end_valid must not be nil.
14778 Value is
14780 1 if display has been updated
14781 0 if otherwise unsuccessful
14782 -1 if redisplay with same window start is known not to succeed
14784 The following steps are performed:
14786 1. Find the last row in the current matrix of W that is not
14787 affected by changes at the start of current_buffer. If no such row
14788 is found, give up.
14790 2. Find the first row in W's current matrix that is not affected by
14791 changes at the end of current_buffer. Maybe there is no such row.
14793 3. Display lines beginning with the row + 1 found in step 1 to the
14794 row found in step 2 or, if step 2 didn't find a row, to the end of
14795 the window.
14797 4. If cursor is not known to appear on the window, give up.
14799 5. If display stopped at the row found in step 2, scroll the
14800 display and current matrix as needed.
14802 6. Maybe display some lines at the end of W, if we must. This can
14803 happen under various circumstances, like a partially visible line
14804 becoming fully visible, or because newly displayed lines are displayed
14805 in smaller font sizes.
14807 7. Update W's window end information. */
14809 static int
14810 try_window_id (w)
14811 struct window *w;
14813 struct frame *f = XFRAME (w->frame);
14814 struct glyph_matrix *current_matrix = w->current_matrix;
14815 struct glyph_matrix *desired_matrix = w->desired_matrix;
14816 struct glyph_row *last_unchanged_at_beg_row;
14817 struct glyph_row *first_unchanged_at_end_row;
14818 struct glyph_row *row;
14819 struct glyph_row *bottom_row;
14820 int bottom_vpos;
14821 struct it it;
14822 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14823 struct text_pos start_pos;
14824 struct run run;
14825 int first_unchanged_at_end_vpos = 0;
14826 struct glyph_row *last_text_row, *last_text_row_at_end;
14827 struct text_pos start;
14828 int first_changed_charpos, last_changed_charpos;
14830 #if GLYPH_DEBUG
14831 if (inhibit_try_window_id)
14832 return 0;
14833 #endif
14835 /* This is handy for debugging. */
14836 #if 0
14837 #define GIVE_UP(X) \
14838 do { \
14839 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14840 return 0; \
14841 } while (0)
14842 #else
14843 #define GIVE_UP(X) return 0
14844 #endif
14846 SET_TEXT_POS_FROM_MARKER (start, w->start);
14848 /* Don't use this for mini-windows because these can show
14849 messages and mini-buffers, and we don't handle that here. */
14850 if (MINI_WINDOW_P (w))
14851 GIVE_UP (1);
14853 /* This flag is used to prevent redisplay optimizations. */
14854 if (windows_or_buffers_changed || cursor_type_changed)
14855 GIVE_UP (2);
14857 /* Verify that narrowing has not changed.
14858 Also verify that we were not told to prevent redisplay optimizations.
14859 It would be nice to further
14860 reduce the number of cases where this prevents try_window_id. */
14861 if (current_buffer->clip_changed
14862 || current_buffer->prevent_redisplay_optimizations_p)
14863 GIVE_UP (3);
14865 /* Window must either use window-based redisplay or be full width. */
14866 if (!FRAME_WINDOW_P (f)
14867 && (!FRAME_LINE_INS_DEL_OK (f)
14868 || !WINDOW_FULL_WIDTH_P (w)))
14869 GIVE_UP (4);
14871 /* Give up if point is not known NOT to appear in W. */
14872 if (PT < CHARPOS (start))
14873 GIVE_UP (5);
14875 /* Another way to prevent redisplay optimizations. */
14876 if (XFASTINT (w->last_modified) == 0)
14877 GIVE_UP (6);
14879 /* Verify that window is not hscrolled. */
14880 if (XFASTINT (w->hscroll) != 0)
14881 GIVE_UP (7);
14883 /* Verify that display wasn't paused. */
14884 if (NILP (w->window_end_valid))
14885 GIVE_UP (8);
14887 /* Can't use this if highlighting a region because a cursor movement
14888 will do more than just set the cursor. */
14889 if (!NILP (Vtransient_mark_mode)
14890 && !NILP (current_buffer->mark_active))
14891 GIVE_UP (9);
14893 /* Likewise if highlighting trailing whitespace. */
14894 if (!NILP (Vshow_trailing_whitespace))
14895 GIVE_UP (11);
14897 /* Likewise if showing a region. */
14898 if (!NILP (w->region_showing))
14899 GIVE_UP (10);
14901 /* Can use this if overlay arrow position and or string have changed. */
14902 if (overlay_arrows_changed_p ())
14903 GIVE_UP (12);
14905 /* When word-wrap is on, adding a space to the first word of a
14906 wrapped line can change the wrap position, altering the line
14907 above it. It might be worthwhile to handle this more
14908 intelligently, but for now just redisplay from scratch. */
14909 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14910 GIVE_UP (21);
14912 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14913 only if buffer has really changed. The reason is that the gap is
14914 initially at Z for freshly visited files. The code below would
14915 set end_unchanged to 0 in that case. */
14916 if (MODIFF > SAVE_MODIFF
14917 /* This seems to happen sometimes after saving a buffer. */
14918 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14920 if (GPT - BEG < BEG_UNCHANGED)
14921 BEG_UNCHANGED = GPT - BEG;
14922 if (Z - GPT < END_UNCHANGED)
14923 END_UNCHANGED = Z - GPT;
14926 /* The position of the first and last character that has been changed. */
14927 first_changed_charpos = BEG + BEG_UNCHANGED;
14928 last_changed_charpos = Z - END_UNCHANGED;
14930 /* If window starts after a line end, and the last change is in
14931 front of that newline, then changes don't affect the display.
14932 This case happens with stealth-fontification. Note that although
14933 the display is unchanged, glyph positions in the matrix have to
14934 be adjusted, of course. */
14935 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14936 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14937 && ((last_changed_charpos < CHARPOS (start)
14938 && CHARPOS (start) == BEGV)
14939 || (last_changed_charpos < CHARPOS (start) - 1
14940 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14942 int Z_old, delta, Z_BYTE_old, delta_bytes;
14943 struct glyph_row *r0;
14945 /* Compute how many chars/bytes have been added to or removed
14946 from the buffer. */
14947 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14948 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14949 delta = Z - Z_old;
14950 delta_bytes = Z_BYTE - Z_BYTE_old;
14952 /* Give up if PT is not in the window. Note that it already has
14953 been checked at the start of try_window_id that PT is not in
14954 front of the window start. */
14955 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14956 GIVE_UP (13);
14958 /* If window start is unchanged, we can reuse the whole matrix
14959 as is, after adjusting glyph positions. No need to compute
14960 the window end again, since its offset from Z hasn't changed. */
14961 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14962 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14963 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14964 /* PT must not be in a partially visible line. */
14965 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14966 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14968 /* Adjust positions in the glyph matrix. */
14969 if (delta || delta_bytes)
14971 struct glyph_row *r1
14972 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14973 increment_matrix_positions (w->current_matrix,
14974 MATRIX_ROW_VPOS (r0, current_matrix),
14975 MATRIX_ROW_VPOS (r1, current_matrix),
14976 delta, delta_bytes);
14979 /* Set the cursor. */
14980 row = row_containing_pos (w, PT, r0, NULL, 0);
14981 if (row)
14982 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14983 else
14984 abort ();
14985 return 1;
14989 /* Handle the case that changes are all below what is displayed in
14990 the window, and that PT is in the window. This shortcut cannot
14991 be taken if ZV is visible in the window, and text has been added
14992 there that is visible in the window. */
14993 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14994 /* ZV is not visible in the window, or there are no
14995 changes at ZV, actually. */
14996 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14997 || first_changed_charpos == last_changed_charpos))
14999 struct glyph_row *r0;
15001 /* Give up if PT is not in the window. Note that it already has
15002 been checked at the start of try_window_id that PT is not in
15003 front of the window start. */
15004 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15005 GIVE_UP (14);
15007 /* If window start is unchanged, we can reuse the whole matrix
15008 as is, without changing glyph positions since no text has
15009 been added/removed in front of the window end. */
15010 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15011 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15012 /* PT must not be in a partially visible line. */
15013 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15014 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15016 /* We have to compute the window end anew since text
15017 can have been added/removed after it. */
15018 w->window_end_pos
15019 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15020 w->window_end_bytepos
15021 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15023 /* Set the cursor. */
15024 row = row_containing_pos (w, PT, r0, NULL, 0);
15025 if (row)
15026 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15027 else
15028 abort ();
15029 return 2;
15033 /* Give up if window start is in the changed area.
15035 The condition used to read
15037 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15039 but why that was tested escapes me at the moment. */
15040 if (CHARPOS (start) >= first_changed_charpos
15041 && CHARPOS (start) <= last_changed_charpos)
15042 GIVE_UP (15);
15044 /* Check that window start agrees with the start of the first glyph
15045 row in its current matrix. Check this after we know the window
15046 start is not in changed text, otherwise positions would not be
15047 comparable. */
15048 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15049 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15050 GIVE_UP (16);
15052 /* Give up if the window ends in strings. Overlay strings
15053 at the end are difficult to handle, so don't try. */
15054 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15055 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15056 GIVE_UP (20);
15058 /* Compute the position at which we have to start displaying new
15059 lines. Some of the lines at the top of the window might be
15060 reusable because they are not displaying changed text. Find the
15061 last row in W's current matrix not affected by changes at the
15062 start of current_buffer. Value is null if changes start in the
15063 first line of window. */
15064 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15065 if (last_unchanged_at_beg_row)
15067 /* Avoid starting to display in the moddle of a character, a TAB
15068 for instance. This is easier than to set up the iterator
15069 exactly, and it's not a frequent case, so the additional
15070 effort wouldn't really pay off. */
15071 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15072 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15073 && last_unchanged_at_beg_row > w->current_matrix->rows)
15074 --last_unchanged_at_beg_row;
15076 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15077 GIVE_UP (17);
15079 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15080 GIVE_UP (18);
15081 start_pos = it.current.pos;
15083 /* Start displaying new lines in the desired matrix at the same
15084 vpos we would use in the current matrix, i.e. below
15085 last_unchanged_at_beg_row. */
15086 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15087 current_matrix);
15088 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15089 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15091 xassert (it.hpos == 0 && it.current_x == 0);
15093 else
15095 /* There are no reusable lines at the start of the window.
15096 Start displaying in the first text line. */
15097 start_display (&it, w, start);
15098 it.vpos = it.first_vpos;
15099 start_pos = it.current.pos;
15102 /* Find the first row that is not affected by changes at the end of
15103 the buffer. Value will be null if there is no unchanged row, in
15104 which case we must redisplay to the end of the window. delta
15105 will be set to the value by which buffer positions beginning with
15106 first_unchanged_at_end_row have to be adjusted due to text
15107 changes. */
15108 first_unchanged_at_end_row
15109 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15110 IF_DEBUG (debug_delta = delta);
15111 IF_DEBUG (debug_delta_bytes = delta_bytes);
15113 /* Set stop_pos to the buffer position up to which we will have to
15114 display new lines. If first_unchanged_at_end_row != NULL, this
15115 is the buffer position of the start of the line displayed in that
15116 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15117 that we don't stop at a buffer position. */
15118 stop_pos = 0;
15119 if (first_unchanged_at_end_row)
15121 xassert (last_unchanged_at_beg_row == NULL
15122 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15124 /* If this is a continuation line, move forward to the next one
15125 that isn't. Changes in lines above affect this line.
15126 Caution: this may move first_unchanged_at_end_row to a row
15127 not displaying text. */
15128 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15129 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15130 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15131 < it.last_visible_y))
15132 ++first_unchanged_at_end_row;
15134 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15135 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15136 >= it.last_visible_y))
15137 first_unchanged_at_end_row = NULL;
15138 else
15140 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15141 + delta);
15142 first_unchanged_at_end_vpos
15143 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15144 xassert (stop_pos >= Z - END_UNCHANGED);
15147 else if (last_unchanged_at_beg_row == NULL)
15148 GIVE_UP (19);
15151 #if GLYPH_DEBUG
15153 /* Either there is no unchanged row at the end, or the one we have
15154 now displays text. This is a necessary condition for the window
15155 end pos calculation at the end of this function. */
15156 xassert (first_unchanged_at_end_row == NULL
15157 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15159 debug_last_unchanged_at_beg_vpos
15160 = (last_unchanged_at_beg_row
15161 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15162 : -1);
15163 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15165 #endif /* GLYPH_DEBUG != 0 */
15168 /* Display new lines. Set last_text_row to the last new line
15169 displayed which has text on it, i.e. might end up as being the
15170 line where the window_end_vpos is. */
15171 w->cursor.vpos = -1;
15172 last_text_row = NULL;
15173 overlay_arrow_seen = 0;
15174 while (it.current_y < it.last_visible_y
15175 && !fonts_changed_p
15176 && (first_unchanged_at_end_row == NULL
15177 || IT_CHARPOS (it) < stop_pos))
15179 if (display_line (&it))
15180 last_text_row = it.glyph_row - 1;
15183 if (fonts_changed_p)
15184 return -1;
15187 /* Compute differences in buffer positions, y-positions etc. for
15188 lines reused at the bottom of the window. Compute what we can
15189 scroll. */
15190 if (first_unchanged_at_end_row
15191 /* No lines reused because we displayed everything up to the
15192 bottom of the window. */
15193 && it.current_y < it.last_visible_y)
15195 dvpos = (it.vpos
15196 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15197 current_matrix));
15198 dy = it.current_y - first_unchanged_at_end_row->y;
15199 run.current_y = first_unchanged_at_end_row->y;
15200 run.desired_y = run.current_y + dy;
15201 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15203 else
15205 delta = delta_bytes = dvpos = dy
15206 = run.current_y = run.desired_y = run.height = 0;
15207 first_unchanged_at_end_row = NULL;
15209 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15212 /* Find the cursor if not already found. We have to decide whether
15213 PT will appear on this window (it sometimes doesn't, but this is
15214 not a very frequent case.) This decision has to be made before
15215 the current matrix is altered. A value of cursor.vpos < 0 means
15216 that PT is either in one of the lines beginning at
15217 first_unchanged_at_end_row or below the window. Don't care for
15218 lines that might be displayed later at the window end; as
15219 mentioned, this is not a frequent case. */
15220 if (w->cursor.vpos < 0)
15222 /* Cursor in unchanged rows at the top? */
15223 if (PT < CHARPOS (start_pos)
15224 && last_unchanged_at_beg_row)
15226 row = row_containing_pos (w, PT,
15227 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15228 last_unchanged_at_beg_row + 1, 0);
15229 if (row)
15230 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15233 /* Start from first_unchanged_at_end_row looking for PT. */
15234 else if (first_unchanged_at_end_row)
15236 row = row_containing_pos (w, PT - delta,
15237 first_unchanged_at_end_row, NULL, 0);
15238 if (row)
15239 set_cursor_from_row (w, row, w->current_matrix, delta,
15240 delta_bytes, dy, dvpos);
15243 /* Give up if cursor was not found. */
15244 if (w->cursor.vpos < 0)
15246 clear_glyph_matrix (w->desired_matrix);
15247 return -1;
15251 /* Don't let the cursor end in the scroll margins. */
15253 int this_scroll_margin, cursor_height;
15255 this_scroll_margin = max (0, scroll_margin);
15256 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15257 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15258 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15260 if ((w->cursor.y < this_scroll_margin
15261 && CHARPOS (start) > BEGV)
15262 /* Old redisplay didn't take scroll margin into account at the bottom,
15263 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15264 || (w->cursor.y + (make_cursor_line_fully_visible_p
15265 ? cursor_height + this_scroll_margin
15266 : 1)) > it.last_visible_y)
15268 w->cursor.vpos = -1;
15269 clear_glyph_matrix (w->desired_matrix);
15270 return -1;
15274 /* Scroll the display. Do it before changing the current matrix so
15275 that xterm.c doesn't get confused about where the cursor glyph is
15276 found. */
15277 if (dy && run.height)
15279 update_begin (f);
15281 if (FRAME_WINDOW_P (f))
15283 FRAME_RIF (f)->update_window_begin_hook (w);
15284 FRAME_RIF (f)->clear_window_mouse_face (w);
15285 FRAME_RIF (f)->scroll_run_hook (w, &run);
15286 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15288 else
15290 /* Terminal frame. In this case, dvpos gives the number of
15291 lines to scroll by; dvpos < 0 means scroll up. */
15292 int first_unchanged_at_end_vpos
15293 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15294 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15295 int end = (WINDOW_TOP_EDGE_LINE (w)
15296 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15297 + window_internal_height (w));
15299 /* Perform the operation on the screen. */
15300 if (dvpos > 0)
15302 /* Scroll last_unchanged_at_beg_row to the end of the
15303 window down dvpos lines. */
15304 set_terminal_window (f, end);
15306 /* On dumb terminals delete dvpos lines at the end
15307 before inserting dvpos empty lines. */
15308 if (!FRAME_SCROLL_REGION_OK (f))
15309 ins_del_lines (f, end - dvpos, -dvpos);
15311 /* Insert dvpos empty lines in front of
15312 last_unchanged_at_beg_row. */
15313 ins_del_lines (f, from, dvpos);
15315 else if (dvpos < 0)
15317 /* Scroll up last_unchanged_at_beg_vpos to the end of
15318 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15319 set_terminal_window (f, end);
15321 /* Delete dvpos lines in front of
15322 last_unchanged_at_beg_vpos. ins_del_lines will set
15323 the cursor to the given vpos and emit |dvpos| delete
15324 line sequences. */
15325 ins_del_lines (f, from + dvpos, dvpos);
15327 /* On a dumb terminal insert dvpos empty lines at the
15328 end. */
15329 if (!FRAME_SCROLL_REGION_OK (f))
15330 ins_del_lines (f, end + dvpos, -dvpos);
15333 set_terminal_window (f, 0);
15336 update_end (f);
15339 /* Shift reused rows of the current matrix to the right position.
15340 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15341 text. */
15342 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15343 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15344 if (dvpos < 0)
15346 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15347 bottom_vpos, dvpos);
15348 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15349 bottom_vpos, 0);
15351 else if (dvpos > 0)
15353 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15354 bottom_vpos, dvpos);
15355 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15356 first_unchanged_at_end_vpos + dvpos, 0);
15359 /* For frame-based redisplay, make sure that current frame and window
15360 matrix are in sync with respect to glyph memory. */
15361 if (!FRAME_WINDOW_P (f))
15362 sync_frame_with_window_matrix_rows (w);
15364 /* Adjust buffer positions in reused rows. */
15365 if (delta || delta_bytes)
15366 increment_matrix_positions (current_matrix,
15367 first_unchanged_at_end_vpos + dvpos,
15368 bottom_vpos, delta, delta_bytes);
15370 /* Adjust Y positions. */
15371 if (dy)
15372 shift_glyph_matrix (w, current_matrix,
15373 first_unchanged_at_end_vpos + dvpos,
15374 bottom_vpos, dy);
15376 if (first_unchanged_at_end_row)
15378 first_unchanged_at_end_row += dvpos;
15379 if (first_unchanged_at_end_row->y >= it.last_visible_y
15380 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15381 first_unchanged_at_end_row = NULL;
15384 /* If scrolling up, there may be some lines to display at the end of
15385 the window. */
15386 last_text_row_at_end = NULL;
15387 if (dy < 0)
15389 /* Scrolling up can leave for example a partially visible line
15390 at the end of the window to be redisplayed. */
15391 /* Set last_row to the glyph row in the current matrix where the
15392 window end line is found. It has been moved up or down in
15393 the matrix by dvpos. */
15394 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15395 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15397 /* If last_row is the window end line, it should display text. */
15398 xassert (last_row->displays_text_p);
15400 /* If window end line was partially visible before, begin
15401 displaying at that line. Otherwise begin displaying with the
15402 line following it. */
15403 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15405 init_to_row_start (&it, w, last_row);
15406 it.vpos = last_vpos;
15407 it.current_y = last_row->y;
15409 else
15411 init_to_row_end (&it, w, last_row);
15412 it.vpos = 1 + last_vpos;
15413 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15414 ++last_row;
15417 /* We may start in a continuation line. If so, we have to
15418 get the right continuation_lines_width and current_x. */
15419 it.continuation_lines_width = last_row->continuation_lines_width;
15420 it.hpos = it.current_x = 0;
15422 /* Display the rest of the lines at the window end. */
15423 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15424 while (it.current_y < it.last_visible_y
15425 && !fonts_changed_p)
15427 /* Is it always sure that the display agrees with lines in
15428 the current matrix? I don't think so, so we mark rows
15429 displayed invalid in the current matrix by setting their
15430 enabled_p flag to zero. */
15431 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15432 if (display_line (&it))
15433 last_text_row_at_end = it.glyph_row - 1;
15437 /* Update window_end_pos and window_end_vpos. */
15438 if (first_unchanged_at_end_row
15439 && !last_text_row_at_end)
15441 /* Window end line if one of the preserved rows from the current
15442 matrix. Set row to the last row displaying text in current
15443 matrix starting at first_unchanged_at_end_row, after
15444 scrolling. */
15445 xassert (first_unchanged_at_end_row->displays_text_p);
15446 row = find_last_row_displaying_text (w->current_matrix, &it,
15447 first_unchanged_at_end_row);
15448 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15450 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15451 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15452 w->window_end_vpos
15453 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15454 xassert (w->window_end_bytepos >= 0);
15455 IF_DEBUG (debug_method_add (w, "A"));
15457 else if (last_text_row_at_end)
15459 w->window_end_pos
15460 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15461 w->window_end_bytepos
15462 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15463 w->window_end_vpos
15464 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15465 xassert (w->window_end_bytepos >= 0);
15466 IF_DEBUG (debug_method_add (w, "B"));
15468 else if (last_text_row)
15470 /* We have displayed either to the end of the window or at the
15471 end of the window, i.e. the last row with text is to be found
15472 in the desired matrix. */
15473 w->window_end_pos
15474 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15475 w->window_end_bytepos
15476 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15477 w->window_end_vpos
15478 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15479 xassert (w->window_end_bytepos >= 0);
15481 else if (first_unchanged_at_end_row == NULL
15482 && last_text_row == NULL
15483 && last_text_row_at_end == NULL)
15485 /* Displayed to end of window, but no line containing text was
15486 displayed. Lines were deleted at the end of the window. */
15487 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15488 int vpos = XFASTINT (w->window_end_vpos);
15489 struct glyph_row *current_row = current_matrix->rows + vpos;
15490 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15492 for (row = NULL;
15493 row == NULL && vpos >= first_vpos;
15494 --vpos, --current_row, --desired_row)
15496 if (desired_row->enabled_p)
15498 if (desired_row->displays_text_p)
15499 row = desired_row;
15501 else if (current_row->displays_text_p)
15502 row = current_row;
15505 xassert (row != NULL);
15506 w->window_end_vpos = make_number (vpos + 1);
15507 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15509 xassert (w->window_end_bytepos >= 0);
15510 IF_DEBUG (debug_method_add (w, "C"));
15512 else
15513 abort ();
15515 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15516 debug_end_vpos = XFASTINT (w->window_end_vpos));
15518 /* Record that display has not been completed. */
15519 w->window_end_valid = Qnil;
15520 w->desired_matrix->no_scrolling_p = 1;
15521 return 3;
15523 #undef GIVE_UP
15528 /***********************************************************************
15529 More debugging support
15530 ***********************************************************************/
15532 #if GLYPH_DEBUG
15534 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15535 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15536 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15539 /* Dump the contents of glyph matrix MATRIX on stderr.
15541 GLYPHS 0 means don't show glyph contents.
15542 GLYPHS 1 means show glyphs in short form
15543 GLYPHS > 1 means show glyphs in long form. */
15545 void
15546 dump_glyph_matrix (matrix, glyphs)
15547 struct glyph_matrix *matrix;
15548 int glyphs;
15550 int i;
15551 for (i = 0; i < matrix->nrows; ++i)
15552 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15556 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15557 the glyph row and area where the glyph comes from. */
15559 void
15560 dump_glyph (row, glyph, area)
15561 struct glyph_row *row;
15562 struct glyph *glyph;
15563 int area;
15565 if (glyph->type == CHAR_GLYPH)
15567 fprintf (stderr,
15568 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15569 glyph - row->glyphs[TEXT_AREA],
15570 'C',
15571 glyph->charpos,
15572 (BUFFERP (glyph->object)
15573 ? 'B'
15574 : (STRINGP (glyph->object)
15575 ? 'S'
15576 : '-')),
15577 glyph->pixel_width,
15578 glyph->u.ch,
15579 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15580 ? glyph->u.ch
15581 : '.'),
15582 glyph->face_id,
15583 glyph->left_box_line_p,
15584 glyph->right_box_line_p);
15586 else if (glyph->type == STRETCH_GLYPH)
15588 fprintf (stderr,
15589 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15590 glyph - row->glyphs[TEXT_AREA],
15591 'S',
15592 glyph->charpos,
15593 (BUFFERP (glyph->object)
15594 ? 'B'
15595 : (STRINGP (glyph->object)
15596 ? 'S'
15597 : '-')),
15598 glyph->pixel_width,
15600 '.',
15601 glyph->face_id,
15602 glyph->left_box_line_p,
15603 glyph->right_box_line_p);
15605 else if (glyph->type == IMAGE_GLYPH)
15607 fprintf (stderr,
15608 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15609 glyph - row->glyphs[TEXT_AREA],
15610 'I',
15611 glyph->charpos,
15612 (BUFFERP (glyph->object)
15613 ? 'B'
15614 : (STRINGP (glyph->object)
15615 ? 'S'
15616 : '-')),
15617 glyph->pixel_width,
15618 glyph->u.img_id,
15619 '.',
15620 glyph->face_id,
15621 glyph->left_box_line_p,
15622 glyph->right_box_line_p);
15624 else if (glyph->type == COMPOSITE_GLYPH)
15626 fprintf (stderr,
15627 " %5d %4c %6d %c %3d 0x%05x",
15628 glyph - row->glyphs[TEXT_AREA],
15629 '+',
15630 glyph->charpos,
15631 (BUFFERP (glyph->object)
15632 ? 'B'
15633 : (STRINGP (glyph->object)
15634 ? 'S'
15635 : '-')),
15636 glyph->pixel_width,
15637 glyph->u.cmp.id);
15638 if (glyph->u.cmp.automatic)
15639 fprintf (stderr,
15640 "[%d-%d]",
15641 glyph->u.cmp.from, glyph->u.cmp.to);
15642 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15643 glyph->face_id,
15644 glyph->left_box_line_p,
15645 glyph->right_box_line_p);
15650 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15651 GLYPHS 0 means don't show glyph contents.
15652 GLYPHS 1 means show glyphs in short form
15653 GLYPHS > 1 means show glyphs in long form. */
15655 void
15656 dump_glyph_row (row, vpos, glyphs)
15657 struct glyph_row *row;
15658 int vpos, glyphs;
15660 if (glyphs != 1)
15662 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15663 fprintf (stderr, "======================================================================\n");
15665 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15666 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15667 vpos,
15668 MATRIX_ROW_START_CHARPOS (row),
15669 MATRIX_ROW_END_CHARPOS (row),
15670 row->used[TEXT_AREA],
15671 row->contains_overlapping_glyphs_p,
15672 row->enabled_p,
15673 row->truncated_on_left_p,
15674 row->truncated_on_right_p,
15675 row->continued_p,
15676 MATRIX_ROW_CONTINUATION_LINE_P (row),
15677 row->displays_text_p,
15678 row->ends_at_zv_p,
15679 row->fill_line_p,
15680 row->ends_in_middle_of_char_p,
15681 row->starts_in_middle_of_char_p,
15682 row->mouse_face_p,
15683 row->x,
15684 row->y,
15685 row->pixel_width,
15686 row->height,
15687 row->visible_height,
15688 row->ascent,
15689 row->phys_ascent);
15690 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15691 row->end.overlay_string_index,
15692 row->continuation_lines_width);
15693 fprintf (stderr, "%9d %5d\n",
15694 CHARPOS (row->start.string_pos),
15695 CHARPOS (row->end.string_pos));
15696 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15697 row->end.dpvec_index);
15700 if (glyphs > 1)
15702 int area;
15704 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15706 struct glyph *glyph = row->glyphs[area];
15707 struct glyph *glyph_end = glyph + row->used[area];
15709 /* Glyph for a line end in text. */
15710 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15711 ++glyph_end;
15713 if (glyph < glyph_end)
15714 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15716 for (; glyph < glyph_end; ++glyph)
15717 dump_glyph (row, glyph, area);
15720 else if (glyphs == 1)
15722 int area;
15724 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15726 char *s = (char *) alloca (row->used[area] + 1);
15727 int i;
15729 for (i = 0; i < row->used[area]; ++i)
15731 struct glyph *glyph = row->glyphs[area] + i;
15732 if (glyph->type == CHAR_GLYPH
15733 && glyph->u.ch < 0x80
15734 && glyph->u.ch >= ' ')
15735 s[i] = glyph->u.ch;
15736 else
15737 s[i] = '.';
15740 s[i] = '\0';
15741 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15747 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15748 Sdump_glyph_matrix, 0, 1, "p",
15749 doc: /* Dump the current matrix of the selected window to stderr.
15750 Shows contents of glyph row structures. With non-nil
15751 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15752 glyphs in short form, otherwise show glyphs in long form. */)
15753 (glyphs)
15754 Lisp_Object glyphs;
15756 struct window *w = XWINDOW (selected_window);
15757 struct buffer *buffer = XBUFFER (w->buffer);
15759 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15760 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15761 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15762 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15763 fprintf (stderr, "=============================================\n");
15764 dump_glyph_matrix (w->current_matrix,
15765 NILP (glyphs) ? 0 : XINT (glyphs));
15766 return Qnil;
15770 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15771 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15774 struct frame *f = XFRAME (selected_frame);
15775 dump_glyph_matrix (f->current_matrix, 1);
15776 return Qnil;
15780 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15781 doc: /* Dump glyph row ROW to stderr.
15782 GLYPH 0 means don't dump glyphs.
15783 GLYPH 1 means dump glyphs in short form.
15784 GLYPH > 1 or omitted means dump glyphs in long form. */)
15785 (row, glyphs)
15786 Lisp_Object row, glyphs;
15788 struct glyph_matrix *matrix;
15789 int vpos;
15791 CHECK_NUMBER (row);
15792 matrix = XWINDOW (selected_window)->current_matrix;
15793 vpos = XINT (row);
15794 if (vpos >= 0 && vpos < matrix->nrows)
15795 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15796 vpos,
15797 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15798 return Qnil;
15802 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15803 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15804 GLYPH 0 means don't dump glyphs.
15805 GLYPH 1 means dump glyphs in short form.
15806 GLYPH > 1 or omitted means dump glyphs in long form. */)
15807 (row, glyphs)
15808 Lisp_Object row, glyphs;
15810 struct frame *sf = SELECTED_FRAME ();
15811 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15812 int vpos;
15814 CHECK_NUMBER (row);
15815 vpos = XINT (row);
15816 if (vpos >= 0 && vpos < m->nrows)
15817 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15818 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15819 return Qnil;
15823 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15824 doc: /* Toggle tracing of redisplay.
15825 With ARG, turn tracing on if and only if ARG is positive. */)
15826 (arg)
15827 Lisp_Object arg;
15829 if (NILP (arg))
15830 trace_redisplay_p = !trace_redisplay_p;
15831 else
15833 arg = Fprefix_numeric_value (arg);
15834 trace_redisplay_p = XINT (arg) > 0;
15837 return Qnil;
15841 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15842 doc: /* Like `format', but print result to stderr.
15843 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15844 (nargs, args)
15845 int nargs;
15846 Lisp_Object *args;
15848 Lisp_Object s = Fformat (nargs, args);
15849 fprintf (stderr, "%s", SDATA (s));
15850 return Qnil;
15853 #endif /* GLYPH_DEBUG */
15857 /***********************************************************************
15858 Building Desired Matrix Rows
15859 ***********************************************************************/
15861 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15862 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15864 static struct glyph_row *
15865 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15866 struct window *w;
15867 Lisp_Object overlay_arrow_string;
15869 struct frame *f = XFRAME (WINDOW_FRAME (w));
15870 struct buffer *buffer = XBUFFER (w->buffer);
15871 struct buffer *old = current_buffer;
15872 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15873 int arrow_len = SCHARS (overlay_arrow_string);
15874 const unsigned char *arrow_end = arrow_string + arrow_len;
15875 const unsigned char *p;
15876 struct it it;
15877 int multibyte_p;
15878 int n_glyphs_before;
15880 set_buffer_temp (buffer);
15881 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15882 it.glyph_row->used[TEXT_AREA] = 0;
15883 SET_TEXT_POS (it.position, 0, 0);
15885 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15886 p = arrow_string;
15887 while (p < arrow_end)
15889 Lisp_Object face, ilisp;
15891 /* Get the next character. */
15892 if (multibyte_p)
15893 it.c = string_char_and_length (p, arrow_len, &it.len);
15894 else
15895 it.c = *p, it.len = 1;
15896 p += it.len;
15898 /* Get its face. */
15899 ilisp = make_number (p - arrow_string);
15900 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15901 it.face_id = compute_char_face (f, it.c, face);
15903 /* Compute its width, get its glyphs. */
15904 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15905 SET_TEXT_POS (it.position, -1, -1);
15906 PRODUCE_GLYPHS (&it);
15908 /* If this character doesn't fit any more in the line, we have
15909 to remove some glyphs. */
15910 if (it.current_x > it.last_visible_x)
15912 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15913 break;
15917 set_buffer_temp (old);
15918 return it.glyph_row;
15922 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15923 glyphs are only inserted for terminal frames since we can't really
15924 win with truncation glyphs when partially visible glyphs are
15925 involved. Which glyphs to insert is determined by
15926 produce_special_glyphs. */
15928 static void
15929 insert_left_trunc_glyphs (it)
15930 struct it *it;
15932 struct it truncate_it;
15933 struct glyph *from, *end, *to, *toend;
15935 xassert (!FRAME_WINDOW_P (it->f));
15937 /* Get the truncation glyphs. */
15938 truncate_it = *it;
15939 truncate_it.current_x = 0;
15940 truncate_it.face_id = DEFAULT_FACE_ID;
15941 truncate_it.glyph_row = &scratch_glyph_row;
15942 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15943 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15944 truncate_it.object = make_number (0);
15945 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15947 /* Overwrite glyphs from IT with truncation glyphs. */
15948 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15949 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15950 to = it->glyph_row->glyphs[TEXT_AREA];
15951 toend = to + it->glyph_row->used[TEXT_AREA];
15953 while (from < end)
15954 *to++ = *from++;
15956 /* There may be padding glyphs left over. Overwrite them too. */
15957 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15959 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15960 while (from < end)
15961 *to++ = *from++;
15964 if (to > toend)
15965 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15969 /* Compute the pixel height and width of IT->glyph_row.
15971 Most of the time, ascent and height of a display line will be equal
15972 to the max_ascent and max_height values of the display iterator
15973 structure. This is not the case if
15975 1. We hit ZV without displaying anything. In this case, max_ascent
15976 and max_height will be zero.
15978 2. We have some glyphs that don't contribute to the line height.
15979 (The glyph row flag contributes_to_line_height_p is for future
15980 pixmap extensions).
15982 The first case is easily covered by using default values because in
15983 these cases, the line height does not really matter, except that it
15984 must not be zero. */
15986 static void
15987 compute_line_metrics (it)
15988 struct it *it;
15990 struct glyph_row *row = it->glyph_row;
15991 int area, i;
15993 if (FRAME_WINDOW_P (it->f))
15995 int i, min_y, max_y;
15997 /* The line may consist of one space only, that was added to
15998 place the cursor on it. If so, the row's height hasn't been
15999 computed yet. */
16000 if (row->height == 0)
16002 if (it->max_ascent + it->max_descent == 0)
16003 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16004 row->ascent = it->max_ascent;
16005 row->height = it->max_ascent + it->max_descent;
16006 row->phys_ascent = it->max_phys_ascent;
16007 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16008 row->extra_line_spacing = it->max_extra_line_spacing;
16011 /* Compute the width of this line. */
16012 row->pixel_width = row->x;
16013 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16014 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16016 xassert (row->pixel_width >= 0);
16017 xassert (row->ascent >= 0 && row->height > 0);
16019 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16020 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16022 /* If first line's physical ascent is larger than its logical
16023 ascent, use the physical ascent, and make the row taller.
16024 This makes accented characters fully visible. */
16025 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16026 && row->phys_ascent > row->ascent)
16028 row->height += row->phys_ascent - row->ascent;
16029 row->ascent = row->phys_ascent;
16032 /* Compute how much of the line is visible. */
16033 row->visible_height = row->height;
16035 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16036 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16038 if (row->y < min_y)
16039 row->visible_height -= min_y - row->y;
16040 if (row->y + row->height > max_y)
16041 row->visible_height -= row->y + row->height - max_y;
16043 else
16045 row->pixel_width = row->used[TEXT_AREA];
16046 if (row->continued_p)
16047 row->pixel_width -= it->continuation_pixel_width;
16048 else if (row->truncated_on_right_p)
16049 row->pixel_width -= it->truncation_pixel_width;
16050 row->ascent = row->phys_ascent = 0;
16051 row->height = row->phys_height = row->visible_height = 1;
16052 row->extra_line_spacing = 0;
16055 /* Compute a hash code for this row. */
16056 row->hash = 0;
16057 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16058 for (i = 0; i < row->used[area]; ++i)
16059 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16060 + row->glyphs[area][i].u.val
16061 + row->glyphs[area][i].face_id
16062 + row->glyphs[area][i].padding_p
16063 + (row->glyphs[area][i].type << 2));
16065 it->max_ascent = it->max_descent = 0;
16066 it->max_phys_ascent = it->max_phys_descent = 0;
16070 /* Append one space to the glyph row of iterator IT if doing a
16071 window-based redisplay. The space has the same face as
16072 IT->face_id. Value is non-zero if a space was added.
16074 This function is called to make sure that there is always one glyph
16075 at the end of a glyph row that the cursor can be set on under
16076 window-systems. (If there weren't such a glyph we would not know
16077 how wide and tall a box cursor should be displayed).
16079 At the same time this space let's a nicely handle clearing to the
16080 end of the line if the row ends in italic text. */
16082 static int
16083 append_space_for_newline (it, default_face_p)
16084 struct it *it;
16085 int default_face_p;
16087 if (FRAME_WINDOW_P (it->f))
16089 int n = it->glyph_row->used[TEXT_AREA];
16091 if (it->glyph_row->glyphs[TEXT_AREA] + n
16092 < it->glyph_row->glyphs[1 + TEXT_AREA])
16094 /* Save some values that must not be changed.
16095 Must save IT->c and IT->len because otherwise
16096 ITERATOR_AT_END_P wouldn't work anymore after
16097 append_space_for_newline has been called. */
16098 enum display_element_type saved_what = it->what;
16099 int saved_c = it->c, saved_len = it->len;
16100 int saved_x = it->current_x;
16101 int saved_face_id = it->face_id;
16102 struct text_pos saved_pos;
16103 Lisp_Object saved_object;
16104 struct face *face;
16106 saved_object = it->object;
16107 saved_pos = it->position;
16109 it->what = IT_CHARACTER;
16110 bzero (&it->position, sizeof it->position);
16111 it->object = make_number (0);
16112 it->c = ' ';
16113 it->len = 1;
16115 if (default_face_p)
16116 it->face_id = DEFAULT_FACE_ID;
16117 else if (it->face_before_selective_p)
16118 it->face_id = it->saved_face_id;
16119 face = FACE_FROM_ID (it->f, it->face_id);
16120 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16122 PRODUCE_GLYPHS (it);
16124 it->override_ascent = -1;
16125 it->constrain_row_ascent_descent_p = 0;
16126 it->current_x = saved_x;
16127 it->object = saved_object;
16128 it->position = saved_pos;
16129 it->what = saved_what;
16130 it->face_id = saved_face_id;
16131 it->len = saved_len;
16132 it->c = saved_c;
16133 return 1;
16137 return 0;
16141 /* Extend the face of the last glyph in the text area of IT->glyph_row
16142 to the end of the display line. Called from display_line.
16143 If the glyph row is empty, add a space glyph to it so that we
16144 know the face to draw. Set the glyph row flag fill_line_p. */
16146 static void
16147 extend_face_to_end_of_line (it)
16148 struct it *it;
16150 struct face *face;
16151 struct frame *f = it->f;
16153 /* If line is already filled, do nothing. */
16154 if (it->current_x >= it->last_visible_x)
16155 return;
16157 /* Face extension extends the background and box of IT->face_id
16158 to the end of the line. If the background equals the background
16159 of the frame, we don't have to do anything. */
16160 if (it->face_before_selective_p)
16161 face = FACE_FROM_ID (it->f, it->saved_face_id);
16162 else
16163 face = FACE_FROM_ID (f, it->face_id);
16165 if (FRAME_WINDOW_P (f)
16166 && it->glyph_row->displays_text_p
16167 && face->box == FACE_NO_BOX
16168 && face->background == FRAME_BACKGROUND_PIXEL (f)
16169 && !face->stipple)
16170 return;
16172 /* Set the glyph row flag indicating that the face of the last glyph
16173 in the text area has to be drawn to the end of the text area. */
16174 it->glyph_row->fill_line_p = 1;
16176 /* If current character of IT is not ASCII, make sure we have the
16177 ASCII face. This will be automatically undone the next time
16178 get_next_display_element returns a multibyte character. Note
16179 that the character will always be single byte in unibyte text. */
16180 if (!ASCII_CHAR_P (it->c))
16182 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16185 if (FRAME_WINDOW_P (f))
16187 /* If the row is empty, add a space with the current face of IT,
16188 so that we know which face to draw. */
16189 if (it->glyph_row->used[TEXT_AREA] == 0)
16191 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16192 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16193 it->glyph_row->used[TEXT_AREA] = 1;
16196 else
16198 /* Save some values that must not be changed. */
16199 int saved_x = it->current_x;
16200 struct text_pos saved_pos;
16201 Lisp_Object saved_object;
16202 enum display_element_type saved_what = it->what;
16203 int saved_face_id = it->face_id;
16205 saved_object = it->object;
16206 saved_pos = it->position;
16208 it->what = IT_CHARACTER;
16209 bzero (&it->position, sizeof it->position);
16210 it->object = make_number (0);
16211 it->c = ' ';
16212 it->len = 1;
16213 it->face_id = face->id;
16215 PRODUCE_GLYPHS (it);
16217 while (it->current_x <= it->last_visible_x)
16218 PRODUCE_GLYPHS (it);
16220 /* Don't count these blanks really. It would let us insert a left
16221 truncation glyph below and make us set the cursor on them, maybe. */
16222 it->current_x = saved_x;
16223 it->object = saved_object;
16224 it->position = saved_pos;
16225 it->what = saved_what;
16226 it->face_id = saved_face_id;
16231 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16232 trailing whitespace. */
16234 static int
16235 trailing_whitespace_p (charpos)
16236 int charpos;
16238 int bytepos = CHAR_TO_BYTE (charpos);
16239 int c = 0;
16241 while (bytepos < ZV_BYTE
16242 && (c = FETCH_CHAR (bytepos),
16243 c == ' ' || c == '\t'))
16244 ++bytepos;
16246 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16248 if (bytepos != PT_BYTE)
16249 return 1;
16251 return 0;
16255 /* Highlight trailing whitespace, if any, in ROW. */
16257 void
16258 highlight_trailing_whitespace (f, row)
16259 struct frame *f;
16260 struct glyph_row *row;
16262 int used = row->used[TEXT_AREA];
16264 if (used)
16266 struct glyph *start = row->glyphs[TEXT_AREA];
16267 struct glyph *glyph = start + used - 1;
16269 /* Skip over glyphs inserted to display the cursor at the
16270 end of a line, for extending the face of the last glyph
16271 to the end of the line on terminals, and for truncation
16272 and continuation glyphs. */
16273 while (glyph >= start
16274 && glyph->type == CHAR_GLYPH
16275 && INTEGERP (glyph->object))
16276 --glyph;
16278 /* If last glyph is a space or stretch, and it's trailing
16279 whitespace, set the face of all trailing whitespace glyphs in
16280 IT->glyph_row to `trailing-whitespace'. */
16281 if (glyph >= start
16282 && BUFFERP (glyph->object)
16283 && (glyph->type == STRETCH_GLYPH
16284 || (glyph->type == CHAR_GLYPH
16285 && glyph->u.ch == ' '))
16286 && trailing_whitespace_p (glyph->charpos))
16288 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16289 if (face_id < 0)
16290 return;
16292 while (glyph >= start
16293 && BUFFERP (glyph->object)
16294 && (glyph->type == STRETCH_GLYPH
16295 || (glyph->type == CHAR_GLYPH
16296 && glyph->u.ch == ' ')))
16297 (glyph--)->face_id = face_id;
16303 /* Value is non-zero if glyph row ROW in window W should be
16304 used to hold the cursor. */
16306 static int
16307 cursor_row_p (w, row)
16308 struct window *w;
16309 struct glyph_row *row;
16311 int cursor_row_p = 1;
16313 if (PT == MATRIX_ROW_END_CHARPOS (row))
16315 /* Suppose the row ends on a string.
16316 Unless the row is continued, that means it ends on a newline
16317 in the string. If it's anything other than a display string
16318 (e.g. a before-string from an overlay), we don't want the
16319 cursor there. (This heuristic seems to give the optimal
16320 behavior for the various types of multi-line strings.) */
16321 if (CHARPOS (row->end.string_pos) >= 0)
16323 if (row->continued_p)
16324 cursor_row_p = 1;
16325 else
16327 /* Check for `display' property. */
16328 struct glyph *beg = row->glyphs[TEXT_AREA];
16329 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16330 struct glyph *glyph;
16332 cursor_row_p = 0;
16333 for (glyph = end; glyph >= beg; --glyph)
16334 if (STRINGP (glyph->object))
16336 Lisp_Object prop
16337 = Fget_char_property (make_number (PT),
16338 Qdisplay, Qnil);
16339 cursor_row_p =
16340 (!NILP (prop)
16341 && display_prop_string_p (prop, glyph->object));
16342 break;
16346 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16348 /* If the row ends in middle of a real character,
16349 and the line is continued, we want the cursor here.
16350 That's because MATRIX_ROW_END_CHARPOS would equal
16351 PT if PT is before the character. */
16352 if (!row->ends_in_ellipsis_p)
16353 cursor_row_p = row->continued_p;
16354 else
16355 /* If the row ends in an ellipsis, then
16356 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16357 We want that position to be displayed after the ellipsis. */
16358 cursor_row_p = 0;
16360 /* If the row ends at ZV, display the cursor at the end of that
16361 row instead of at the start of the row below. */
16362 else if (row->ends_at_zv_p)
16363 cursor_row_p = 1;
16364 else
16365 cursor_row_p = 0;
16368 return cursor_row_p;
16373 /* Push the display property PROP so that it will be rendered at the
16374 current position in IT. */
16376 static void
16377 push_display_prop (struct it *it, Lisp_Object prop)
16379 push_it (it);
16381 /* Never display a cursor on the prefix. */
16382 it->avoid_cursor_p = 1;
16384 if (STRINGP (prop))
16386 if (SCHARS (prop) == 0)
16388 pop_it (it);
16389 return;
16392 it->string = prop;
16393 it->multibyte_p = STRING_MULTIBYTE (it->string);
16394 it->current.overlay_string_index = -1;
16395 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16396 it->end_charpos = it->string_nchars = SCHARS (it->string);
16397 it->method = GET_FROM_STRING;
16398 it->stop_charpos = 0;
16400 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16402 it->method = GET_FROM_STRETCH;
16403 it->object = prop;
16405 #ifdef HAVE_WINDOW_SYSTEM
16406 else if (IMAGEP (prop))
16408 it->what = IT_IMAGE;
16409 it->image_id = lookup_image (it->f, prop);
16410 it->method = GET_FROM_IMAGE;
16412 #endif /* HAVE_WINDOW_SYSTEM */
16413 else
16415 pop_it (it); /* bogus display property, give up */
16416 return;
16420 /* Return the character-property PROP at the current position in IT. */
16422 static Lisp_Object
16423 get_it_property (it, prop)
16424 struct it *it;
16425 Lisp_Object prop;
16427 Lisp_Object position;
16429 if (STRINGP (it->object))
16430 position = make_number (IT_STRING_CHARPOS (*it));
16431 else if (BUFFERP (it->object))
16432 position = make_number (IT_CHARPOS (*it));
16433 else
16434 return Qnil;
16436 return Fget_char_property (position, prop, it->object);
16439 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16441 static void
16442 handle_line_prefix (struct it *it)
16444 Lisp_Object prefix;
16445 if (it->continuation_lines_width > 0)
16447 prefix = get_it_property (it, Qwrap_prefix);
16448 if (NILP (prefix))
16449 prefix = Vwrap_prefix;
16451 else
16453 prefix = get_it_property (it, Qline_prefix);
16454 if (NILP (prefix))
16455 prefix = Vline_prefix;
16457 if (! NILP (prefix))
16459 push_display_prop (it, prefix);
16460 /* If the prefix is wider than the window, and we try to wrap
16461 it, it would acquire its own wrap prefix, and so on till the
16462 iterator stack overflows. So, don't wrap the prefix. */
16463 it->line_wrap = TRUNCATE;
16469 /* Construct the glyph row IT->glyph_row in the desired matrix of
16470 IT->w from text at the current position of IT. See dispextern.h
16471 for an overview of struct it. Value is non-zero if
16472 IT->glyph_row displays text, as opposed to a line displaying ZV
16473 only. */
16475 static int
16476 display_line (it)
16477 struct it *it;
16479 struct glyph_row *row = it->glyph_row;
16480 Lisp_Object overlay_arrow_string;
16481 struct it wrap_it;
16482 int may_wrap = 0, wrap_x;
16483 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16484 int wrap_row_phys_ascent, wrap_row_phys_height;
16485 int wrap_row_extra_line_spacing;
16487 /* We always start displaying at hpos zero even if hscrolled. */
16488 xassert (it->hpos == 0 && it->current_x == 0);
16490 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16491 >= it->w->desired_matrix->nrows)
16493 it->w->nrows_scale_factor++;
16494 fonts_changed_p = 1;
16495 return 0;
16498 /* Is IT->w showing the region? */
16499 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16501 /* Clear the result glyph row and enable it. */
16502 prepare_desired_row (row);
16504 row->y = it->current_y;
16505 row->start = it->start;
16506 row->continuation_lines_width = it->continuation_lines_width;
16507 row->displays_text_p = 1;
16508 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16509 it->starts_in_middle_of_char_p = 0;
16511 /* Arrange the overlays nicely for our purposes. Usually, we call
16512 display_line on only one line at a time, in which case this
16513 can't really hurt too much, or we call it on lines which appear
16514 one after another in the buffer, in which case all calls to
16515 recenter_overlay_lists but the first will be pretty cheap. */
16516 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16518 /* Move over display elements that are not visible because we are
16519 hscrolled. This may stop at an x-position < IT->first_visible_x
16520 if the first glyph is partially visible or if we hit a line end. */
16521 if (it->current_x < it->first_visible_x)
16523 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16524 MOVE_TO_POS | MOVE_TO_X);
16526 else
16528 /* We only do this when not calling `move_it_in_display_line_to'
16529 above, because move_it_in_display_line_to calls
16530 handle_line_prefix itself. */
16531 handle_line_prefix (it);
16534 /* Get the initial row height. This is either the height of the
16535 text hscrolled, if there is any, or zero. */
16536 row->ascent = it->max_ascent;
16537 row->height = it->max_ascent + it->max_descent;
16538 row->phys_ascent = it->max_phys_ascent;
16539 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16540 row->extra_line_spacing = it->max_extra_line_spacing;
16542 /* Loop generating characters. The loop is left with IT on the next
16543 character to display. */
16544 while (1)
16546 int n_glyphs_before, hpos_before, x_before;
16547 int x, i, nglyphs;
16548 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16550 /* Retrieve the next thing to display. Value is zero if end of
16551 buffer reached. */
16552 if (!get_next_display_element (it))
16554 /* Maybe add a space at the end of this line that is used to
16555 display the cursor there under X. Set the charpos of the
16556 first glyph of blank lines not corresponding to any text
16557 to -1. */
16558 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16559 row->exact_window_width_line_p = 1;
16560 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16561 || row->used[TEXT_AREA] == 0)
16563 row->glyphs[TEXT_AREA]->charpos = -1;
16564 row->displays_text_p = 0;
16566 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16567 && (!MINI_WINDOW_P (it->w)
16568 || (minibuf_level && EQ (it->window, minibuf_window))))
16569 row->indicate_empty_line_p = 1;
16572 it->continuation_lines_width = 0;
16573 row->ends_at_zv_p = 1;
16574 break;
16577 /* Now, get the metrics of what we want to display. This also
16578 generates glyphs in `row' (which is IT->glyph_row). */
16579 n_glyphs_before = row->used[TEXT_AREA];
16580 x = it->current_x;
16582 /* Remember the line height so far in case the next element doesn't
16583 fit on the line. */
16584 if (it->line_wrap != TRUNCATE)
16586 ascent = it->max_ascent;
16587 descent = it->max_descent;
16588 phys_ascent = it->max_phys_ascent;
16589 phys_descent = it->max_phys_descent;
16591 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16593 if (IT_DISPLAYING_WHITESPACE (it))
16594 may_wrap = 1;
16595 else if (may_wrap)
16597 wrap_it = *it;
16598 wrap_x = x;
16599 wrap_row_used = row->used[TEXT_AREA];
16600 wrap_row_ascent = row->ascent;
16601 wrap_row_height = row->height;
16602 wrap_row_phys_ascent = row->phys_ascent;
16603 wrap_row_phys_height = row->phys_height;
16604 wrap_row_extra_line_spacing = row->extra_line_spacing;
16605 may_wrap = 0;
16610 PRODUCE_GLYPHS (it);
16612 /* If this display element was in marginal areas, continue with
16613 the next one. */
16614 if (it->area != TEXT_AREA)
16616 row->ascent = max (row->ascent, it->max_ascent);
16617 row->height = max (row->height, it->max_ascent + it->max_descent);
16618 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16619 row->phys_height = max (row->phys_height,
16620 it->max_phys_ascent + it->max_phys_descent);
16621 row->extra_line_spacing = max (row->extra_line_spacing,
16622 it->max_extra_line_spacing);
16623 set_iterator_to_next (it, 1);
16624 continue;
16627 /* Does the display element fit on the line? If we truncate
16628 lines, we should draw past the right edge of the window. If
16629 we don't truncate, we want to stop so that we can display the
16630 continuation glyph before the right margin. If lines are
16631 continued, there are two possible strategies for characters
16632 resulting in more than 1 glyph (e.g. tabs): Display as many
16633 glyphs as possible in this line and leave the rest for the
16634 continuation line, or display the whole element in the next
16635 line. Original redisplay did the former, so we do it also. */
16636 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16637 hpos_before = it->hpos;
16638 x_before = x;
16640 if (/* Not a newline. */
16641 nglyphs > 0
16642 /* Glyphs produced fit entirely in the line. */
16643 && it->current_x < it->last_visible_x)
16645 it->hpos += nglyphs;
16646 row->ascent = max (row->ascent, it->max_ascent);
16647 row->height = max (row->height, it->max_ascent + it->max_descent);
16648 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16649 row->phys_height = max (row->phys_height,
16650 it->max_phys_ascent + it->max_phys_descent);
16651 row->extra_line_spacing = max (row->extra_line_spacing,
16652 it->max_extra_line_spacing);
16653 if (it->current_x - it->pixel_width < it->first_visible_x)
16654 row->x = x - it->first_visible_x;
16656 else
16658 int new_x;
16659 struct glyph *glyph;
16661 for (i = 0; i < nglyphs; ++i, x = new_x)
16663 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16664 new_x = x + glyph->pixel_width;
16666 if (/* Lines are continued. */
16667 it->line_wrap != TRUNCATE
16668 && (/* Glyph doesn't fit on the line. */
16669 new_x > it->last_visible_x
16670 /* Or it fits exactly on a window system frame. */
16671 || (new_x == it->last_visible_x
16672 && FRAME_WINDOW_P (it->f))))
16674 /* End of a continued line. */
16676 if (it->hpos == 0
16677 || (new_x == it->last_visible_x
16678 && FRAME_WINDOW_P (it->f)))
16680 /* Current glyph is the only one on the line or
16681 fits exactly on the line. We must continue
16682 the line because we can't draw the cursor
16683 after the glyph. */
16684 row->continued_p = 1;
16685 it->current_x = new_x;
16686 it->continuation_lines_width += new_x;
16687 ++it->hpos;
16688 if (i == nglyphs - 1)
16690 /* If line-wrap is on, check if a previous
16691 wrap point was found. */
16692 if (wrap_row_used > 0
16693 /* Even if there is a previous wrap
16694 point, continue the line here as
16695 usual, if (i) the previous character
16696 was a space or tab AND (ii) the
16697 current character is not. */
16698 && (!may_wrap
16699 || IT_DISPLAYING_WHITESPACE (it)))
16700 goto back_to_wrap;
16702 set_iterator_to_next (it, 1);
16703 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16705 if (!get_next_display_element (it))
16707 row->exact_window_width_line_p = 1;
16708 it->continuation_lines_width = 0;
16709 row->continued_p = 0;
16710 row->ends_at_zv_p = 1;
16712 else if (ITERATOR_AT_END_OF_LINE_P (it))
16714 row->continued_p = 0;
16715 row->exact_window_width_line_p = 1;
16720 else if (CHAR_GLYPH_PADDING_P (*glyph)
16721 && !FRAME_WINDOW_P (it->f))
16723 /* A padding glyph that doesn't fit on this line.
16724 This means the whole character doesn't fit
16725 on the line. */
16726 row->used[TEXT_AREA] = n_glyphs_before;
16728 /* Fill the rest of the row with continuation
16729 glyphs like in 20.x. */
16730 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16731 < row->glyphs[1 + TEXT_AREA])
16732 produce_special_glyphs (it, IT_CONTINUATION);
16734 row->continued_p = 1;
16735 it->current_x = x_before;
16736 it->continuation_lines_width += x_before;
16738 /* Restore the height to what it was before the
16739 element not fitting on the line. */
16740 it->max_ascent = ascent;
16741 it->max_descent = descent;
16742 it->max_phys_ascent = phys_ascent;
16743 it->max_phys_descent = phys_descent;
16745 else if (wrap_row_used > 0)
16747 back_to_wrap:
16748 *it = wrap_it;
16749 it->continuation_lines_width += wrap_x;
16750 row->used[TEXT_AREA] = wrap_row_used;
16751 row->ascent = wrap_row_ascent;
16752 row->height = wrap_row_height;
16753 row->phys_ascent = wrap_row_phys_ascent;
16754 row->phys_height = wrap_row_phys_height;
16755 row->extra_line_spacing = wrap_row_extra_line_spacing;
16756 row->continued_p = 1;
16757 row->ends_at_zv_p = 0;
16758 row->exact_window_width_line_p = 0;
16759 it->continuation_lines_width += x;
16761 /* Make sure that a non-default face is extended
16762 up to the right margin of the window. */
16763 extend_face_to_end_of_line (it);
16765 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16767 /* A TAB that extends past the right edge of the
16768 window. This produces a single glyph on
16769 window system frames. We leave the glyph in
16770 this row and let it fill the row, but don't
16771 consume the TAB. */
16772 it->continuation_lines_width += it->last_visible_x;
16773 row->ends_in_middle_of_char_p = 1;
16774 row->continued_p = 1;
16775 glyph->pixel_width = it->last_visible_x - x;
16776 it->starts_in_middle_of_char_p = 1;
16778 else
16780 /* Something other than a TAB that draws past
16781 the right edge of the window. Restore
16782 positions to values before the element. */
16783 row->used[TEXT_AREA] = n_glyphs_before + i;
16785 /* Display continuation glyphs. */
16786 if (!FRAME_WINDOW_P (it->f))
16787 produce_special_glyphs (it, IT_CONTINUATION);
16788 row->continued_p = 1;
16790 it->current_x = x_before;
16791 it->continuation_lines_width += x;
16792 extend_face_to_end_of_line (it);
16794 if (nglyphs > 1 && i > 0)
16796 row->ends_in_middle_of_char_p = 1;
16797 it->starts_in_middle_of_char_p = 1;
16800 /* Restore the height to what it was before the
16801 element not fitting on the line. */
16802 it->max_ascent = ascent;
16803 it->max_descent = descent;
16804 it->max_phys_ascent = phys_ascent;
16805 it->max_phys_descent = phys_descent;
16808 break;
16810 else if (new_x > it->first_visible_x)
16812 /* Increment number of glyphs actually displayed. */
16813 ++it->hpos;
16815 if (x < it->first_visible_x)
16816 /* Glyph is partially visible, i.e. row starts at
16817 negative X position. */
16818 row->x = x - it->first_visible_x;
16820 else
16822 /* Glyph is completely off the left margin of the
16823 window. This should not happen because of the
16824 move_it_in_display_line at the start of this
16825 function, unless the text display area of the
16826 window is empty. */
16827 xassert (it->first_visible_x <= it->last_visible_x);
16831 row->ascent = max (row->ascent, it->max_ascent);
16832 row->height = max (row->height, it->max_ascent + it->max_descent);
16833 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16834 row->phys_height = max (row->phys_height,
16835 it->max_phys_ascent + it->max_phys_descent);
16836 row->extra_line_spacing = max (row->extra_line_spacing,
16837 it->max_extra_line_spacing);
16839 /* End of this display line if row is continued. */
16840 if (row->continued_p || row->ends_at_zv_p)
16841 break;
16844 at_end_of_line:
16845 /* Is this a line end? If yes, we're also done, after making
16846 sure that a non-default face is extended up to the right
16847 margin of the window. */
16848 if (ITERATOR_AT_END_OF_LINE_P (it))
16850 int used_before = row->used[TEXT_AREA];
16852 row->ends_in_newline_from_string_p = STRINGP (it->object);
16854 /* Add a space at the end of the line that is used to
16855 display the cursor there. */
16856 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16857 append_space_for_newline (it, 0);
16859 /* Extend the face to the end of the line. */
16860 extend_face_to_end_of_line (it);
16862 /* Make sure we have the position. */
16863 if (used_before == 0)
16864 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16866 /* Consume the line end. This skips over invisible lines. */
16867 set_iterator_to_next (it, 1);
16868 it->continuation_lines_width = 0;
16869 break;
16872 /* Proceed with next display element. Note that this skips
16873 over lines invisible because of selective display. */
16874 set_iterator_to_next (it, 1);
16876 /* If we truncate lines, we are done when the last displayed
16877 glyphs reach past the right margin of the window. */
16878 if (it->line_wrap == TRUNCATE
16879 && (FRAME_WINDOW_P (it->f)
16880 ? (it->current_x >= it->last_visible_x)
16881 : (it->current_x > it->last_visible_x)))
16883 /* Maybe add truncation glyphs. */
16884 if (!FRAME_WINDOW_P (it->f))
16886 int i, n;
16888 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16889 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16890 break;
16892 for (n = row->used[TEXT_AREA]; i < n; ++i)
16894 row->used[TEXT_AREA] = i;
16895 produce_special_glyphs (it, IT_TRUNCATION);
16898 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16900 /* Don't truncate if we can overflow newline into fringe. */
16901 if (!get_next_display_element (it))
16903 it->continuation_lines_width = 0;
16904 row->ends_at_zv_p = 1;
16905 row->exact_window_width_line_p = 1;
16906 break;
16908 if (ITERATOR_AT_END_OF_LINE_P (it))
16910 row->exact_window_width_line_p = 1;
16911 goto at_end_of_line;
16915 row->truncated_on_right_p = 1;
16916 it->continuation_lines_width = 0;
16917 reseat_at_next_visible_line_start (it, 0);
16918 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16919 it->hpos = hpos_before;
16920 it->current_x = x_before;
16921 break;
16925 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16926 at the left window margin. */
16927 if (it->first_visible_x
16928 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16930 if (!FRAME_WINDOW_P (it->f))
16931 insert_left_trunc_glyphs (it);
16932 row->truncated_on_left_p = 1;
16935 /* If the start of this line is the overlay arrow-position, then
16936 mark this glyph row as the one containing the overlay arrow.
16937 This is clearly a mess with variable size fonts. It would be
16938 better to let it be displayed like cursors under X. */
16939 if ((row->displays_text_p || !overlay_arrow_seen)
16940 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16941 !NILP (overlay_arrow_string)))
16943 /* Overlay arrow in window redisplay is a fringe bitmap. */
16944 if (STRINGP (overlay_arrow_string))
16946 struct glyph_row *arrow_row
16947 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16948 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16949 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16950 struct glyph *p = row->glyphs[TEXT_AREA];
16951 struct glyph *p2, *end;
16953 /* Copy the arrow glyphs. */
16954 while (glyph < arrow_end)
16955 *p++ = *glyph++;
16957 /* Throw away padding glyphs. */
16958 p2 = p;
16959 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16960 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16961 ++p2;
16962 if (p2 > p)
16964 while (p2 < end)
16965 *p++ = *p2++;
16966 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16969 else
16971 xassert (INTEGERP (overlay_arrow_string));
16972 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16974 overlay_arrow_seen = 1;
16977 /* Compute pixel dimensions of this line. */
16978 compute_line_metrics (it);
16980 /* Remember the position at which this line ends. */
16981 row->end = it->current;
16983 /* Record whether this row ends inside an ellipsis. */
16984 row->ends_in_ellipsis_p
16985 = (it->method == GET_FROM_DISPLAY_VECTOR
16986 && it->ellipsis_p);
16988 /* Save fringe bitmaps in this row. */
16989 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16990 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16991 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16992 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16994 it->left_user_fringe_bitmap = 0;
16995 it->left_user_fringe_face_id = 0;
16996 it->right_user_fringe_bitmap = 0;
16997 it->right_user_fringe_face_id = 0;
16999 /* Maybe set the cursor. */
17000 if (it->w->cursor.vpos < 0
17001 && PT >= MATRIX_ROW_START_CHARPOS (row)
17002 && PT <= MATRIX_ROW_END_CHARPOS (row)
17003 && cursor_row_p (it->w, row))
17004 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17006 /* Highlight trailing whitespace. */
17007 if (!NILP (Vshow_trailing_whitespace))
17008 highlight_trailing_whitespace (it->f, it->glyph_row);
17010 /* Prepare for the next line. This line starts horizontally at (X
17011 HPOS) = (0 0). Vertical positions are incremented. As a
17012 convenience for the caller, IT->glyph_row is set to the next
17013 row to be used. */
17014 it->current_x = it->hpos = 0;
17015 it->current_y += row->height;
17016 ++it->vpos;
17017 ++it->glyph_row;
17018 it->start = it->current;
17019 return row->displays_text_p;
17024 /***********************************************************************
17025 Menu Bar
17026 ***********************************************************************/
17028 /* Redisplay the menu bar in the frame for window W.
17030 The menu bar of X frames that don't have X toolkit support is
17031 displayed in a special window W->frame->menu_bar_window.
17033 The menu bar of terminal frames is treated specially as far as
17034 glyph matrices are concerned. Menu bar lines are not part of
17035 windows, so the update is done directly on the frame matrix rows
17036 for the menu bar. */
17038 static void
17039 display_menu_bar (w)
17040 struct window *w;
17042 struct frame *f = XFRAME (WINDOW_FRAME (w));
17043 struct it it;
17044 Lisp_Object items;
17045 int i;
17047 /* Don't do all this for graphical frames. */
17048 #ifdef HAVE_NTGUI
17049 if (FRAME_W32_P (f))
17050 return;
17051 #endif
17052 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17053 if (FRAME_X_P (f))
17054 return;
17055 #endif
17057 #ifdef HAVE_NS
17058 if (FRAME_NS_P (f))
17059 return;
17060 #endif /* HAVE_NS */
17062 #ifdef USE_X_TOOLKIT
17063 xassert (!FRAME_WINDOW_P (f));
17064 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17065 it.first_visible_x = 0;
17066 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17067 #else /* not USE_X_TOOLKIT */
17068 if (FRAME_WINDOW_P (f))
17070 /* Menu bar lines are displayed in the desired matrix of the
17071 dummy window menu_bar_window. */
17072 struct window *menu_w;
17073 xassert (WINDOWP (f->menu_bar_window));
17074 menu_w = XWINDOW (f->menu_bar_window);
17075 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17076 MENU_FACE_ID);
17077 it.first_visible_x = 0;
17078 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17080 else
17082 /* This is a TTY frame, i.e. character hpos/vpos are used as
17083 pixel x/y. */
17084 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17085 MENU_FACE_ID);
17086 it.first_visible_x = 0;
17087 it.last_visible_x = FRAME_COLS (f);
17089 #endif /* not USE_X_TOOLKIT */
17091 if (! mode_line_inverse_video)
17092 /* Force the menu-bar to be displayed in the default face. */
17093 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17095 /* Clear all rows of the menu bar. */
17096 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17098 struct glyph_row *row = it.glyph_row + i;
17099 clear_glyph_row (row);
17100 row->enabled_p = 1;
17101 row->full_width_p = 1;
17104 /* Display all items of the menu bar. */
17105 items = FRAME_MENU_BAR_ITEMS (it.f);
17106 for (i = 0; i < XVECTOR (items)->size; i += 4)
17108 Lisp_Object string;
17110 /* Stop at nil string. */
17111 string = AREF (items, i + 1);
17112 if (NILP (string))
17113 break;
17115 /* Remember where item was displayed. */
17116 ASET (items, i + 3, make_number (it.hpos));
17118 /* Display the item, pad with one space. */
17119 if (it.current_x < it.last_visible_x)
17120 display_string (NULL, string, Qnil, 0, 0, &it,
17121 SCHARS (string) + 1, 0, 0, -1);
17124 /* Fill out the line with spaces. */
17125 if (it.current_x < it.last_visible_x)
17126 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17128 /* Compute the total height of the lines. */
17129 compute_line_metrics (&it);
17134 /***********************************************************************
17135 Mode Line
17136 ***********************************************************************/
17138 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17139 FORCE is non-zero, redisplay mode lines unconditionally.
17140 Otherwise, redisplay only mode lines that are garbaged. Value is
17141 the number of windows whose mode lines were redisplayed. */
17143 static int
17144 redisplay_mode_lines (window, force)
17145 Lisp_Object window;
17146 int force;
17148 int nwindows = 0;
17150 while (!NILP (window))
17152 struct window *w = XWINDOW (window);
17154 if (WINDOWP (w->hchild))
17155 nwindows += redisplay_mode_lines (w->hchild, force);
17156 else if (WINDOWP (w->vchild))
17157 nwindows += redisplay_mode_lines (w->vchild, force);
17158 else if (force
17159 || FRAME_GARBAGED_P (XFRAME (w->frame))
17160 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17162 struct text_pos lpoint;
17163 struct buffer *old = current_buffer;
17165 /* Set the window's buffer for the mode line display. */
17166 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17167 set_buffer_internal_1 (XBUFFER (w->buffer));
17169 /* Point refers normally to the selected window. For any
17170 other window, set up appropriate value. */
17171 if (!EQ (window, selected_window))
17173 struct text_pos pt;
17175 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17176 if (CHARPOS (pt) < BEGV)
17177 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17178 else if (CHARPOS (pt) > (ZV - 1))
17179 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17180 else
17181 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17184 /* Display mode lines. */
17185 clear_glyph_matrix (w->desired_matrix);
17186 if (display_mode_lines (w))
17188 ++nwindows;
17189 w->must_be_updated_p = 1;
17192 /* Restore old settings. */
17193 set_buffer_internal_1 (old);
17194 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17197 window = w->next;
17200 return nwindows;
17204 /* Display the mode and/or top line of window W. Value is the number
17205 of mode lines displayed. */
17207 static int
17208 display_mode_lines (w)
17209 struct window *w;
17211 Lisp_Object old_selected_window, old_selected_frame;
17212 int n = 0;
17214 old_selected_frame = selected_frame;
17215 selected_frame = w->frame;
17216 old_selected_window = selected_window;
17217 XSETWINDOW (selected_window, w);
17219 /* These will be set while the mode line specs are processed. */
17220 line_number_displayed = 0;
17221 w->column_number_displayed = Qnil;
17223 if (WINDOW_WANTS_MODELINE_P (w))
17225 struct window *sel_w = XWINDOW (old_selected_window);
17227 /* Select mode line face based on the real selected window. */
17228 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17229 current_buffer->mode_line_format);
17230 ++n;
17233 if (WINDOW_WANTS_HEADER_LINE_P (w))
17235 display_mode_line (w, HEADER_LINE_FACE_ID,
17236 current_buffer->header_line_format);
17237 ++n;
17240 selected_frame = old_selected_frame;
17241 selected_window = old_selected_window;
17242 return n;
17246 /* Display mode or top line of window W. FACE_ID specifies which line
17247 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17248 FORMAT is the mode line format to display. Value is the pixel
17249 height of the mode line displayed. */
17251 static int
17252 display_mode_line (w, face_id, format)
17253 struct window *w;
17254 enum face_id face_id;
17255 Lisp_Object format;
17257 struct it it;
17258 struct face *face;
17259 int count = SPECPDL_INDEX ();
17261 init_iterator (&it, w, -1, -1, NULL, face_id);
17262 /* Don't extend on a previously drawn mode-line.
17263 This may happen if called from pos_visible_p. */
17264 it.glyph_row->enabled_p = 0;
17265 prepare_desired_row (it.glyph_row);
17267 it.glyph_row->mode_line_p = 1;
17269 if (! mode_line_inverse_video)
17270 /* Force the mode-line to be displayed in the default face. */
17271 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17273 record_unwind_protect (unwind_format_mode_line,
17274 format_mode_line_unwind_data (NULL, Qnil, 0));
17276 mode_line_target = MODE_LINE_DISPLAY;
17278 /* Temporarily make frame's keyboard the current kboard so that
17279 kboard-local variables in the mode_line_format will get the right
17280 values. */
17281 push_kboard (FRAME_KBOARD (it.f));
17282 record_unwind_save_match_data ();
17283 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17284 pop_kboard ();
17286 unbind_to (count, Qnil);
17288 /* Fill up with spaces. */
17289 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17291 compute_line_metrics (&it);
17292 it.glyph_row->full_width_p = 1;
17293 it.glyph_row->continued_p = 0;
17294 it.glyph_row->truncated_on_left_p = 0;
17295 it.glyph_row->truncated_on_right_p = 0;
17297 /* Make a 3D mode-line have a shadow at its right end. */
17298 face = FACE_FROM_ID (it.f, face_id);
17299 extend_face_to_end_of_line (&it);
17300 if (face->box != FACE_NO_BOX)
17302 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17303 + it.glyph_row->used[TEXT_AREA] - 1);
17304 last->right_box_line_p = 1;
17307 return it.glyph_row->height;
17310 /* Move element ELT in LIST to the front of LIST.
17311 Return the updated list. */
17313 static Lisp_Object
17314 move_elt_to_front (elt, list)
17315 Lisp_Object elt, list;
17317 register Lisp_Object tail, prev;
17318 register Lisp_Object tem;
17320 tail = list;
17321 prev = Qnil;
17322 while (CONSP (tail))
17324 tem = XCAR (tail);
17326 if (EQ (elt, tem))
17328 /* Splice out the link TAIL. */
17329 if (NILP (prev))
17330 list = XCDR (tail);
17331 else
17332 Fsetcdr (prev, XCDR (tail));
17334 /* Now make it the first. */
17335 Fsetcdr (tail, list);
17336 return tail;
17338 else
17339 prev = tail;
17340 tail = XCDR (tail);
17341 QUIT;
17344 /* Not found--return unchanged LIST. */
17345 return list;
17348 /* Contribute ELT to the mode line for window IT->w. How it
17349 translates into text depends on its data type.
17351 IT describes the display environment in which we display, as usual.
17353 DEPTH is the depth in recursion. It is used to prevent
17354 infinite recursion here.
17356 FIELD_WIDTH is the number of characters the display of ELT should
17357 occupy in the mode line, and PRECISION is the maximum number of
17358 characters to display from ELT's representation. See
17359 display_string for details.
17361 Returns the hpos of the end of the text generated by ELT.
17363 PROPS is a property list to add to any string we encounter.
17365 If RISKY is nonzero, remove (disregard) any properties in any string
17366 we encounter, and ignore :eval and :propertize.
17368 The global variable `mode_line_target' determines whether the
17369 output is passed to `store_mode_line_noprop',
17370 `store_mode_line_string', or `display_string'. */
17372 static int
17373 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17374 struct it *it;
17375 int depth;
17376 int field_width, precision;
17377 Lisp_Object elt, props;
17378 int risky;
17380 int n = 0, field, prec;
17381 int literal = 0;
17383 tail_recurse:
17384 if (depth > 100)
17385 elt = build_string ("*too-deep*");
17387 depth++;
17389 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17391 case Lisp_String:
17393 /* A string: output it and check for %-constructs within it. */
17394 unsigned char c;
17395 int offset = 0;
17397 if (SCHARS (elt) > 0
17398 && (!NILP (props) || risky))
17400 Lisp_Object oprops, aelt;
17401 oprops = Ftext_properties_at (make_number (0), elt);
17403 /* If the starting string's properties are not what
17404 we want, translate the string. Also, if the string
17405 is risky, do that anyway. */
17407 if (NILP (Fequal (props, oprops)) || risky)
17409 /* If the starting string has properties,
17410 merge the specified ones onto the existing ones. */
17411 if (! NILP (oprops) && !risky)
17413 Lisp_Object tem;
17415 oprops = Fcopy_sequence (oprops);
17416 tem = props;
17417 while (CONSP (tem))
17419 oprops = Fplist_put (oprops, XCAR (tem),
17420 XCAR (XCDR (tem)));
17421 tem = XCDR (XCDR (tem));
17423 props = oprops;
17426 aelt = Fassoc (elt, mode_line_proptrans_alist);
17427 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17429 /* AELT is what we want. Move it to the front
17430 without consing. */
17431 elt = XCAR (aelt);
17432 mode_line_proptrans_alist
17433 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17435 else
17437 Lisp_Object tem;
17439 /* If AELT has the wrong props, it is useless.
17440 so get rid of it. */
17441 if (! NILP (aelt))
17442 mode_line_proptrans_alist
17443 = Fdelq (aelt, mode_line_proptrans_alist);
17445 elt = Fcopy_sequence (elt);
17446 Fset_text_properties (make_number (0), Flength (elt),
17447 props, elt);
17448 /* Add this item to mode_line_proptrans_alist. */
17449 mode_line_proptrans_alist
17450 = Fcons (Fcons (elt, props),
17451 mode_line_proptrans_alist);
17452 /* Truncate mode_line_proptrans_alist
17453 to at most 50 elements. */
17454 tem = Fnthcdr (make_number (50),
17455 mode_line_proptrans_alist);
17456 if (! NILP (tem))
17457 XSETCDR (tem, Qnil);
17462 offset = 0;
17464 if (literal)
17466 prec = precision - n;
17467 switch (mode_line_target)
17469 case MODE_LINE_NOPROP:
17470 case MODE_LINE_TITLE:
17471 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17472 break;
17473 case MODE_LINE_STRING:
17474 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17475 break;
17476 case MODE_LINE_DISPLAY:
17477 n += display_string (NULL, elt, Qnil, 0, 0, it,
17478 0, prec, 0, STRING_MULTIBYTE (elt));
17479 break;
17482 break;
17485 /* Handle the non-literal case. */
17487 while ((precision <= 0 || n < precision)
17488 && SREF (elt, offset) != 0
17489 && (mode_line_target != MODE_LINE_DISPLAY
17490 || it->current_x < it->last_visible_x))
17492 int last_offset = offset;
17494 /* Advance to end of string or next format specifier. */
17495 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17498 if (offset - 1 != last_offset)
17500 int nchars, nbytes;
17502 /* Output to end of string or up to '%'. Field width
17503 is length of string. Don't output more than
17504 PRECISION allows us. */
17505 offset--;
17507 prec = c_string_width (SDATA (elt) + last_offset,
17508 offset - last_offset, precision - n,
17509 &nchars, &nbytes);
17511 switch (mode_line_target)
17513 case MODE_LINE_NOPROP:
17514 case MODE_LINE_TITLE:
17515 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17516 break;
17517 case MODE_LINE_STRING:
17519 int bytepos = last_offset;
17520 int charpos = string_byte_to_char (elt, bytepos);
17521 int endpos = (precision <= 0
17522 ? string_byte_to_char (elt, offset)
17523 : charpos + nchars);
17525 n += store_mode_line_string (NULL,
17526 Fsubstring (elt, make_number (charpos),
17527 make_number (endpos)),
17528 0, 0, 0, Qnil);
17530 break;
17531 case MODE_LINE_DISPLAY:
17533 int bytepos = last_offset;
17534 int charpos = string_byte_to_char (elt, bytepos);
17536 if (precision <= 0)
17537 nchars = string_byte_to_char (elt, offset) - charpos;
17538 n += display_string (NULL, elt, Qnil, 0, charpos,
17539 it, 0, nchars, 0,
17540 STRING_MULTIBYTE (elt));
17542 break;
17545 else /* c == '%' */
17547 int percent_position = offset;
17549 /* Get the specified minimum width. Zero means
17550 don't pad. */
17551 field = 0;
17552 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17553 field = field * 10 + c - '0';
17555 /* Don't pad beyond the total padding allowed. */
17556 if (field_width - n > 0 && field > field_width - n)
17557 field = field_width - n;
17559 /* Note that either PRECISION <= 0 or N < PRECISION. */
17560 prec = precision - n;
17562 if (c == 'M')
17563 n += display_mode_element (it, depth, field, prec,
17564 Vglobal_mode_string, props,
17565 risky);
17566 else if (c != 0)
17568 int multibyte;
17569 int bytepos, charpos;
17570 unsigned char *spec;
17572 bytepos = percent_position;
17573 charpos = (STRING_MULTIBYTE (elt)
17574 ? string_byte_to_char (elt, bytepos)
17575 : bytepos);
17576 spec
17577 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17579 switch (mode_line_target)
17581 case MODE_LINE_NOPROP:
17582 case MODE_LINE_TITLE:
17583 n += store_mode_line_noprop (spec, field, prec);
17584 break;
17585 case MODE_LINE_STRING:
17587 int len = strlen (spec);
17588 Lisp_Object tem = make_string (spec, len);
17589 props = Ftext_properties_at (make_number (charpos), elt);
17590 /* Should only keep face property in props */
17591 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17593 break;
17594 case MODE_LINE_DISPLAY:
17596 int nglyphs_before, nwritten;
17598 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17599 nwritten = display_string (spec, Qnil, elt,
17600 charpos, 0, it,
17601 field, prec, 0,
17602 multibyte);
17604 /* Assign to the glyphs written above the
17605 string where the `%x' came from, position
17606 of the `%'. */
17607 if (nwritten > 0)
17609 struct glyph *glyph
17610 = (it->glyph_row->glyphs[TEXT_AREA]
17611 + nglyphs_before);
17612 int i;
17614 for (i = 0; i < nwritten; ++i)
17616 glyph[i].object = elt;
17617 glyph[i].charpos = charpos;
17620 n += nwritten;
17623 break;
17626 else /* c == 0 */
17627 break;
17631 break;
17633 case Lisp_Symbol:
17634 /* A symbol: process the value of the symbol recursively
17635 as if it appeared here directly. Avoid error if symbol void.
17636 Special case: if value of symbol is a string, output the string
17637 literally. */
17639 register Lisp_Object tem;
17641 /* If the variable is not marked as risky to set
17642 then its contents are risky to use. */
17643 if (NILP (Fget (elt, Qrisky_local_variable)))
17644 risky = 1;
17646 tem = Fboundp (elt);
17647 if (!NILP (tem))
17649 tem = Fsymbol_value (elt);
17650 /* If value is a string, output that string literally:
17651 don't check for % within it. */
17652 if (STRINGP (tem))
17653 literal = 1;
17655 if (!EQ (tem, elt))
17657 /* Give up right away for nil or t. */
17658 elt = tem;
17659 goto tail_recurse;
17663 break;
17665 case Lisp_Cons:
17667 register Lisp_Object car, tem;
17669 /* A cons cell: five distinct cases.
17670 If first element is :eval or :propertize, do something special.
17671 If first element is a string or a cons, process all the elements
17672 and effectively concatenate them.
17673 If first element is a negative number, truncate displaying cdr to
17674 at most that many characters. If positive, pad (with spaces)
17675 to at least that many characters.
17676 If first element is a symbol, process the cadr or caddr recursively
17677 according to whether the symbol's value is non-nil or nil. */
17678 car = XCAR (elt);
17679 if (EQ (car, QCeval))
17681 /* An element of the form (:eval FORM) means evaluate FORM
17682 and use the result as mode line elements. */
17684 if (risky)
17685 break;
17687 if (CONSP (XCDR (elt)))
17689 Lisp_Object spec;
17690 spec = safe_eval (XCAR (XCDR (elt)));
17691 n += display_mode_element (it, depth, field_width - n,
17692 precision - n, spec, props,
17693 risky);
17696 else if (EQ (car, QCpropertize))
17698 /* An element of the form (:propertize ELT PROPS...)
17699 means display ELT but applying properties PROPS. */
17701 if (risky)
17702 break;
17704 if (CONSP (XCDR (elt)))
17705 n += display_mode_element (it, depth, field_width - n,
17706 precision - n, XCAR (XCDR (elt)),
17707 XCDR (XCDR (elt)), risky);
17709 else if (SYMBOLP (car))
17711 tem = Fboundp (car);
17712 elt = XCDR (elt);
17713 if (!CONSP (elt))
17714 goto invalid;
17715 /* elt is now the cdr, and we know it is a cons cell.
17716 Use its car if CAR has a non-nil value. */
17717 if (!NILP (tem))
17719 tem = Fsymbol_value (car);
17720 if (!NILP (tem))
17722 elt = XCAR (elt);
17723 goto tail_recurse;
17726 /* Symbol's value is nil (or symbol is unbound)
17727 Get the cddr of the original list
17728 and if possible find the caddr and use that. */
17729 elt = XCDR (elt);
17730 if (NILP (elt))
17731 break;
17732 else if (!CONSP (elt))
17733 goto invalid;
17734 elt = XCAR (elt);
17735 goto tail_recurse;
17737 else if (INTEGERP (car))
17739 register int lim = XINT (car);
17740 elt = XCDR (elt);
17741 if (lim < 0)
17743 /* Negative int means reduce maximum width. */
17744 if (precision <= 0)
17745 precision = -lim;
17746 else
17747 precision = min (precision, -lim);
17749 else if (lim > 0)
17751 /* Padding specified. Don't let it be more than
17752 current maximum. */
17753 if (precision > 0)
17754 lim = min (precision, lim);
17756 /* If that's more padding than already wanted, queue it.
17757 But don't reduce padding already specified even if
17758 that is beyond the current truncation point. */
17759 field_width = max (lim, field_width);
17761 goto tail_recurse;
17763 else if (STRINGP (car) || CONSP (car))
17765 register int limit = 50;
17766 /* Limit is to protect against circular lists. */
17767 while (CONSP (elt)
17768 && --limit > 0
17769 && (precision <= 0 || n < precision))
17771 n += display_mode_element (it, depth,
17772 /* Do padding only after the last
17773 element in the list. */
17774 (! CONSP (XCDR (elt))
17775 ? field_width - n
17776 : 0),
17777 precision - n, XCAR (elt),
17778 props, risky);
17779 elt = XCDR (elt);
17783 break;
17785 default:
17786 invalid:
17787 elt = build_string ("*invalid*");
17788 goto tail_recurse;
17791 /* Pad to FIELD_WIDTH. */
17792 if (field_width > 0 && n < field_width)
17794 switch (mode_line_target)
17796 case MODE_LINE_NOPROP:
17797 case MODE_LINE_TITLE:
17798 n += store_mode_line_noprop ("", field_width - n, 0);
17799 break;
17800 case MODE_LINE_STRING:
17801 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17802 break;
17803 case MODE_LINE_DISPLAY:
17804 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17805 0, 0, 0);
17806 break;
17810 return n;
17813 /* Store a mode-line string element in mode_line_string_list.
17815 If STRING is non-null, display that C string. Otherwise, the Lisp
17816 string LISP_STRING is displayed.
17818 FIELD_WIDTH is the minimum number of output glyphs to produce.
17819 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17820 with spaces. FIELD_WIDTH <= 0 means don't pad.
17822 PRECISION is the maximum number of characters to output from
17823 STRING. PRECISION <= 0 means don't truncate the string.
17825 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17826 properties to the string.
17828 PROPS are the properties to add to the string.
17829 The mode_line_string_face face property is always added to the string.
17832 static int
17833 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17834 char *string;
17835 Lisp_Object lisp_string;
17836 int copy_string;
17837 int field_width;
17838 int precision;
17839 Lisp_Object props;
17841 int len;
17842 int n = 0;
17844 if (string != NULL)
17846 len = strlen (string);
17847 if (precision > 0 && len > precision)
17848 len = precision;
17849 lisp_string = make_string (string, len);
17850 if (NILP (props))
17851 props = mode_line_string_face_prop;
17852 else if (!NILP (mode_line_string_face))
17854 Lisp_Object face = Fplist_get (props, Qface);
17855 props = Fcopy_sequence (props);
17856 if (NILP (face))
17857 face = mode_line_string_face;
17858 else
17859 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17860 props = Fplist_put (props, Qface, face);
17862 Fadd_text_properties (make_number (0), make_number (len),
17863 props, lisp_string);
17865 else
17867 len = XFASTINT (Flength (lisp_string));
17868 if (precision > 0 && len > precision)
17870 len = precision;
17871 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17872 precision = -1;
17874 if (!NILP (mode_line_string_face))
17876 Lisp_Object face;
17877 if (NILP (props))
17878 props = Ftext_properties_at (make_number (0), lisp_string);
17879 face = Fplist_get (props, Qface);
17880 if (NILP (face))
17881 face = mode_line_string_face;
17882 else
17883 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17884 props = Fcons (Qface, Fcons (face, Qnil));
17885 if (copy_string)
17886 lisp_string = Fcopy_sequence (lisp_string);
17888 if (!NILP (props))
17889 Fadd_text_properties (make_number (0), make_number (len),
17890 props, lisp_string);
17893 if (len > 0)
17895 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17896 n += len;
17899 if (field_width > len)
17901 field_width -= len;
17902 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17903 if (!NILP (props))
17904 Fadd_text_properties (make_number (0), make_number (field_width),
17905 props, lisp_string);
17906 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17907 n += field_width;
17910 return n;
17914 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17915 1, 4, 0,
17916 doc: /* Format a string out of a mode line format specification.
17917 First arg FORMAT specifies the mode line format (see `mode-line-format'
17918 for details) to use.
17920 Optional second arg FACE specifies the face property to put
17921 on all characters for which no face is specified.
17922 The value t means whatever face the window's mode line currently uses
17923 \(either `mode-line' or `mode-line-inactive', depending).
17924 A value of nil means the default is no face property.
17925 If FACE is an integer, the value string has no text properties.
17927 Optional third and fourth args WINDOW and BUFFER specify the window
17928 and buffer to use as the context for the formatting (defaults
17929 are the selected window and the window's buffer). */)
17930 (format, face, window, buffer)
17931 Lisp_Object format, face, window, buffer;
17933 struct it it;
17934 int len;
17935 struct window *w;
17936 struct buffer *old_buffer = NULL;
17937 int face_id = -1;
17938 int no_props = INTEGERP (face);
17939 int count = SPECPDL_INDEX ();
17940 Lisp_Object str;
17941 int string_start = 0;
17943 if (NILP (window))
17944 window = selected_window;
17945 CHECK_WINDOW (window);
17946 w = XWINDOW (window);
17948 if (NILP (buffer))
17949 buffer = w->buffer;
17950 CHECK_BUFFER (buffer);
17952 /* Make formatting the modeline a non-op when noninteractive, otherwise
17953 there will be problems later caused by a partially initialized frame. */
17954 if (NILP (format) || noninteractive)
17955 return empty_unibyte_string;
17957 if (no_props)
17958 face = Qnil;
17960 if (!NILP (face))
17962 if (EQ (face, Qt))
17963 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17964 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17967 if (face_id < 0)
17968 face_id = DEFAULT_FACE_ID;
17970 if (XBUFFER (buffer) != current_buffer)
17971 old_buffer = current_buffer;
17973 /* Save things including mode_line_proptrans_alist,
17974 and set that to nil so that we don't alter the outer value. */
17975 record_unwind_protect (unwind_format_mode_line,
17976 format_mode_line_unwind_data
17977 (old_buffer, selected_window, 1));
17978 mode_line_proptrans_alist = Qnil;
17980 Fselect_window (window, Qt);
17981 if (old_buffer)
17982 set_buffer_internal_1 (XBUFFER (buffer));
17984 init_iterator (&it, w, -1, -1, NULL, face_id);
17986 if (no_props)
17988 mode_line_target = MODE_LINE_NOPROP;
17989 mode_line_string_face_prop = Qnil;
17990 mode_line_string_list = Qnil;
17991 string_start = MODE_LINE_NOPROP_LEN (0);
17993 else
17995 mode_line_target = MODE_LINE_STRING;
17996 mode_line_string_list = Qnil;
17997 mode_line_string_face = face;
17998 mode_line_string_face_prop
17999 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18002 push_kboard (FRAME_KBOARD (it.f));
18003 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18004 pop_kboard ();
18006 if (no_props)
18008 len = MODE_LINE_NOPROP_LEN (string_start);
18009 str = make_string (mode_line_noprop_buf + string_start, len);
18011 else
18013 mode_line_string_list = Fnreverse (mode_line_string_list);
18014 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18015 empty_unibyte_string);
18018 unbind_to (count, Qnil);
18019 return str;
18022 /* Write a null-terminated, right justified decimal representation of
18023 the positive integer D to BUF using a minimal field width WIDTH. */
18025 static void
18026 pint2str (buf, width, d)
18027 register char *buf;
18028 register int width;
18029 register int d;
18031 register char *p = buf;
18033 if (d <= 0)
18034 *p++ = '0';
18035 else
18037 while (d > 0)
18039 *p++ = d % 10 + '0';
18040 d /= 10;
18044 for (width -= (int) (p - buf); width > 0; --width)
18045 *p++ = ' ';
18046 *p-- = '\0';
18047 while (p > buf)
18049 d = *buf;
18050 *buf++ = *p;
18051 *p-- = d;
18055 /* Write a null-terminated, right justified decimal and "human
18056 readable" representation of the nonnegative integer D to BUF using
18057 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18059 static const char power_letter[] =
18061 0, /* not used */
18062 'k', /* kilo */
18063 'M', /* mega */
18064 'G', /* giga */
18065 'T', /* tera */
18066 'P', /* peta */
18067 'E', /* exa */
18068 'Z', /* zetta */
18069 'Y' /* yotta */
18072 static void
18073 pint2hrstr (buf, width, d)
18074 char *buf;
18075 int width;
18076 int d;
18078 /* We aim to represent the nonnegative integer D as
18079 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18080 int quotient = d;
18081 int remainder = 0;
18082 /* -1 means: do not use TENTHS. */
18083 int tenths = -1;
18084 int exponent = 0;
18086 /* Length of QUOTIENT.TENTHS as a string. */
18087 int length;
18089 char * psuffix;
18090 char * p;
18092 if (1000 <= quotient)
18094 /* Scale to the appropriate EXPONENT. */
18097 remainder = quotient % 1000;
18098 quotient /= 1000;
18099 exponent++;
18101 while (1000 <= quotient);
18103 /* Round to nearest and decide whether to use TENTHS or not. */
18104 if (quotient <= 9)
18106 tenths = remainder / 100;
18107 if (50 <= remainder % 100)
18109 if (tenths < 9)
18110 tenths++;
18111 else
18113 quotient++;
18114 if (quotient == 10)
18115 tenths = -1;
18116 else
18117 tenths = 0;
18121 else
18122 if (500 <= remainder)
18124 if (quotient < 999)
18125 quotient++;
18126 else
18128 quotient = 1;
18129 exponent++;
18130 tenths = 0;
18135 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18136 if (tenths == -1 && quotient <= 99)
18137 if (quotient <= 9)
18138 length = 1;
18139 else
18140 length = 2;
18141 else
18142 length = 3;
18143 p = psuffix = buf + max (width, length);
18145 /* Print EXPONENT. */
18146 if (exponent)
18147 *psuffix++ = power_letter[exponent];
18148 *psuffix = '\0';
18150 /* Print TENTHS. */
18151 if (tenths >= 0)
18153 *--p = '0' + tenths;
18154 *--p = '.';
18157 /* Print QUOTIENT. */
18160 int digit = quotient % 10;
18161 *--p = '0' + digit;
18163 while ((quotient /= 10) != 0);
18165 /* Print leading spaces. */
18166 while (buf < p)
18167 *--p = ' ';
18170 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18171 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18172 type of CODING_SYSTEM. Return updated pointer into BUF. */
18174 static unsigned char invalid_eol_type[] = "(*invalid*)";
18176 static char *
18177 decode_mode_spec_coding (coding_system, buf, eol_flag)
18178 Lisp_Object coding_system;
18179 register char *buf;
18180 int eol_flag;
18182 Lisp_Object val;
18183 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18184 const unsigned char *eol_str;
18185 int eol_str_len;
18186 /* The EOL conversion we are using. */
18187 Lisp_Object eoltype;
18189 val = CODING_SYSTEM_SPEC (coding_system);
18190 eoltype = Qnil;
18192 if (!VECTORP (val)) /* Not yet decided. */
18194 if (multibyte)
18195 *buf++ = '-';
18196 if (eol_flag)
18197 eoltype = eol_mnemonic_undecided;
18198 /* Don't mention EOL conversion if it isn't decided. */
18200 else
18202 Lisp_Object attrs;
18203 Lisp_Object eolvalue;
18205 attrs = AREF (val, 0);
18206 eolvalue = AREF (val, 2);
18208 if (multibyte)
18209 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18211 if (eol_flag)
18213 /* The EOL conversion that is normal on this system. */
18215 if (NILP (eolvalue)) /* Not yet decided. */
18216 eoltype = eol_mnemonic_undecided;
18217 else if (VECTORP (eolvalue)) /* Not yet decided. */
18218 eoltype = eol_mnemonic_undecided;
18219 else /* eolvalue is Qunix, Qdos, or Qmac. */
18220 eoltype = (EQ (eolvalue, Qunix)
18221 ? eol_mnemonic_unix
18222 : (EQ (eolvalue, Qdos) == 1
18223 ? eol_mnemonic_dos : eol_mnemonic_mac));
18227 if (eol_flag)
18229 /* Mention the EOL conversion if it is not the usual one. */
18230 if (STRINGP (eoltype))
18232 eol_str = SDATA (eoltype);
18233 eol_str_len = SBYTES (eoltype);
18235 else if (CHARACTERP (eoltype))
18237 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18238 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18239 eol_str = tmp;
18241 else
18243 eol_str = invalid_eol_type;
18244 eol_str_len = sizeof (invalid_eol_type) - 1;
18246 bcopy (eol_str, buf, eol_str_len);
18247 buf += eol_str_len;
18250 return buf;
18253 /* Return a string for the output of a mode line %-spec for window W,
18254 generated by character C. PRECISION >= 0 means don't return a
18255 string longer than that value. FIELD_WIDTH > 0 means pad the
18256 string returned with spaces to that value. Return 1 in *MULTIBYTE
18257 if the result is multibyte text.
18259 Note we operate on the current buffer for most purposes,
18260 the exception being w->base_line_pos. */
18262 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18264 static char *
18265 decode_mode_spec (w, c, field_width, precision, multibyte)
18266 struct window *w;
18267 register int c;
18268 int field_width, precision;
18269 int *multibyte;
18271 Lisp_Object obj;
18272 struct frame *f = XFRAME (WINDOW_FRAME (w));
18273 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18274 struct buffer *b = current_buffer;
18276 obj = Qnil;
18277 *multibyte = 0;
18279 switch (c)
18281 case '*':
18282 if (!NILP (b->read_only))
18283 return "%";
18284 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18285 return "*";
18286 return "-";
18288 case '+':
18289 /* This differs from %* only for a modified read-only buffer. */
18290 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18291 return "*";
18292 if (!NILP (b->read_only))
18293 return "%";
18294 return "-";
18296 case '&':
18297 /* This differs from %* in ignoring read-only-ness. */
18298 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18299 return "*";
18300 return "-";
18302 case '%':
18303 return "%";
18305 case '[':
18307 int i;
18308 char *p;
18310 if (command_loop_level > 5)
18311 return "[[[... ";
18312 p = decode_mode_spec_buf;
18313 for (i = 0; i < command_loop_level; i++)
18314 *p++ = '[';
18315 *p = 0;
18316 return decode_mode_spec_buf;
18319 case ']':
18321 int i;
18322 char *p;
18324 if (command_loop_level > 5)
18325 return " ...]]]";
18326 p = decode_mode_spec_buf;
18327 for (i = 0; i < command_loop_level; i++)
18328 *p++ = ']';
18329 *p = 0;
18330 return decode_mode_spec_buf;
18333 case '-':
18335 register int i;
18337 /* Let lots_of_dashes be a string of infinite length. */
18338 if (mode_line_target == MODE_LINE_NOPROP ||
18339 mode_line_target == MODE_LINE_STRING)
18340 return "--";
18341 if (field_width <= 0
18342 || field_width > sizeof (lots_of_dashes))
18344 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18345 decode_mode_spec_buf[i] = '-';
18346 decode_mode_spec_buf[i] = '\0';
18347 return decode_mode_spec_buf;
18349 else
18350 return lots_of_dashes;
18353 case 'b':
18354 obj = b->name;
18355 break;
18357 case 'c':
18358 /* %c and %l are ignored in `frame-title-format'.
18359 (In redisplay_internal, the frame title is drawn _before_ the
18360 windows are updated, so the stuff which depends on actual
18361 window contents (such as %l) may fail to render properly, or
18362 even crash emacs.) */
18363 if (mode_line_target == MODE_LINE_TITLE)
18364 return "";
18365 else
18367 int col = (int) current_column (); /* iftc */
18368 w->column_number_displayed = make_number (col);
18369 pint2str (decode_mode_spec_buf, field_width, col);
18370 return decode_mode_spec_buf;
18373 case 'e':
18374 #ifndef SYSTEM_MALLOC
18376 if (NILP (Vmemory_full))
18377 return "";
18378 else
18379 return "!MEM FULL! ";
18381 #else
18382 return "";
18383 #endif
18385 case 'F':
18386 /* %F displays the frame name. */
18387 if (!NILP (f->title))
18388 return (char *) SDATA (f->title);
18389 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18390 return (char *) SDATA (f->name);
18391 return "Emacs";
18393 case 'f':
18394 obj = b->filename;
18395 break;
18397 case 'i':
18399 int size = ZV - BEGV;
18400 pint2str (decode_mode_spec_buf, field_width, size);
18401 return decode_mode_spec_buf;
18404 case 'I':
18406 int size = ZV - BEGV;
18407 pint2hrstr (decode_mode_spec_buf, field_width, size);
18408 return decode_mode_spec_buf;
18411 case 'l':
18413 int startpos, startpos_byte, line, linepos, linepos_byte;
18414 int topline, nlines, junk, height;
18416 /* %c and %l are ignored in `frame-title-format'. */
18417 if (mode_line_target == MODE_LINE_TITLE)
18418 return "";
18420 startpos = XMARKER (w->start)->charpos;
18421 startpos_byte = marker_byte_position (w->start);
18422 height = WINDOW_TOTAL_LINES (w);
18424 /* If we decided that this buffer isn't suitable for line numbers,
18425 don't forget that too fast. */
18426 if (EQ (w->base_line_pos, w->buffer))
18427 goto no_value;
18428 /* But do forget it, if the window shows a different buffer now. */
18429 else if (BUFFERP (w->base_line_pos))
18430 w->base_line_pos = Qnil;
18432 /* If the buffer is very big, don't waste time. */
18433 if (INTEGERP (Vline_number_display_limit)
18434 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18436 w->base_line_pos = Qnil;
18437 w->base_line_number = Qnil;
18438 goto no_value;
18441 if (INTEGERP (w->base_line_number)
18442 && INTEGERP (w->base_line_pos)
18443 && XFASTINT (w->base_line_pos) <= startpos)
18445 line = XFASTINT (w->base_line_number);
18446 linepos = XFASTINT (w->base_line_pos);
18447 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18449 else
18451 line = 1;
18452 linepos = BUF_BEGV (b);
18453 linepos_byte = BUF_BEGV_BYTE (b);
18456 /* Count lines from base line to window start position. */
18457 nlines = display_count_lines (linepos, linepos_byte,
18458 startpos_byte,
18459 startpos, &junk);
18461 topline = nlines + line;
18463 /* Determine a new base line, if the old one is too close
18464 or too far away, or if we did not have one.
18465 "Too close" means it's plausible a scroll-down would
18466 go back past it. */
18467 if (startpos == BUF_BEGV (b))
18469 w->base_line_number = make_number (topline);
18470 w->base_line_pos = make_number (BUF_BEGV (b));
18472 else if (nlines < height + 25 || nlines > height * 3 + 50
18473 || linepos == BUF_BEGV (b))
18475 int limit = BUF_BEGV (b);
18476 int limit_byte = BUF_BEGV_BYTE (b);
18477 int position;
18478 int distance = (height * 2 + 30) * line_number_display_limit_width;
18480 if (startpos - distance > limit)
18482 limit = startpos - distance;
18483 limit_byte = CHAR_TO_BYTE (limit);
18486 nlines = display_count_lines (startpos, startpos_byte,
18487 limit_byte,
18488 - (height * 2 + 30),
18489 &position);
18490 /* If we couldn't find the lines we wanted within
18491 line_number_display_limit_width chars per line,
18492 give up on line numbers for this window. */
18493 if (position == limit_byte && limit == startpos - distance)
18495 w->base_line_pos = w->buffer;
18496 w->base_line_number = Qnil;
18497 goto no_value;
18500 w->base_line_number = make_number (topline - nlines);
18501 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18504 /* Now count lines from the start pos to point. */
18505 nlines = display_count_lines (startpos, startpos_byte,
18506 PT_BYTE, PT, &junk);
18508 /* Record that we did display the line number. */
18509 line_number_displayed = 1;
18511 /* Make the string to show. */
18512 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18513 return decode_mode_spec_buf;
18514 no_value:
18516 char* p = decode_mode_spec_buf;
18517 int pad = field_width - 2;
18518 while (pad-- > 0)
18519 *p++ = ' ';
18520 *p++ = '?';
18521 *p++ = '?';
18522 *p = '\0';
18523 return decode_mode_spec_buf;
18526 break;
18528 case 'm':
18529 obj = b->mode_name;
18530 break;
18532 case 'n':
18533 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18534 return " Narrow";
18535 break;
18537 case 'p':
18539 int pos = marker_position (w->start);
18540 int total = BUF_ZV (b) - BUF_BEGV (b);
18542 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18544 if (pos <= BUF_BEGV (b))
18545 return "All";
18546 else
18547 return "Bottom";
18549 else if (pos <= BUF_BEGV (b))
18550 return "Top";
18551 else
18553 if (total > 1000000)
18554 /* Do it differently for a large value, to avoid overflow. */
18555 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18556 else
18557 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18558 /* We can't normally display a 3-digit number,
18559 so get us a 2-digit number that is close. */
18560 if (total == 100)
18561 total = 99;
18562 sprintf (decode_mode_spec_buf, "%2d%%", total);
18563 return decode_mode_spec_buf;
18567 /* Display percentage of size above the bottom of the screen. */
18568 case 'P':
18570 int toppos = marker_position (w->start);
18571 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18572 int total = BUF_ZV (b) - BUF_BEGV (b);
18574 if (botpos >= BUF_ZV (b))
18576 if (toppos <= BUF_BEGV (b))
18577 return "All";
18578 else
18579 return "Bottom";
18581 else
18583 if (total > 1000000)
18584 /* Do it differently for a large value, to avoid overflow. */
18585 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18586 else
18587 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18588 /* We can't normally display a 3-digit number,
18589 so get us a 2-digit number that is close. */
18590 if (total == 100)
18591 total = 99;
18592 if (toppos <= BUF_BEGV (b))
18593 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18594 else
18595 sprintf (decode_mode_spec_buf, "%2d%%", total);
18596 return decode_mode_spec_buf;
18600 case 's':
18601 /* status of process */
18602 obj = Fget_buffer_process (Fcurrent_buffer ());
18603 if (NILP (obj))
18604 return "no process";
18605 #ifdef subprocesses
18606 obj = Fsymbol_name (Fprocess_status (obj));
18607 #endif
18608 break;
18610 case '@':
18612 Lisp_Object val;
18613 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18614 if (NILP (val))
18615 return "-";
18616 else
18617 return "@";
18620 case 't': /* indicate TEXT or BINARY */
18621 #ifdef MODE_LINE_BINARY_TEXT
18622 return MODE_LINE_BINARY_TEXT (b);
18623 #else
18624 return "T";
18625 #endif
18627 case 'z':
18628 /* coding-system (not including end-of-line format) */
18629 case 'Z':
18630 /* coding-system (including end-of-line type) */
18632 int eol_flag = (c == 'Z');
18633 char *p = decode_mode_spec_buf;
18635 if (! FRAME_WINDOW_P (f))
18637 /* No need to mention EOL here--the terminal never needs
18638 to do EOL conversion. */
18639 p = decode_mode_spec_coding (CODING_ID_NAME
18640 (FRAME_KEYBOARD_CODING (f)->id),
18641 p, 0);
18642 p = decode_mode_spec_coding (CODING_ID_NAME
18643 (FRAME_TERMINAL_CODING (f)->id),
18644 p, 0);
18646 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18647 p, eol_flag);
18649 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18650 #ifdef subprocesses
18651 obj = Fget_buffer_process (Fcurrent_buffer ());
18652 if (PROCESSP (obj))
18654 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18655 p, eol_flag);
18656 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18657 p, eol_flag);
18659 #endif /* subprocesses */
18660 #endif /* 0 */
18661 *p = 0;
18662 return decode_mode_spec_buf;
18666 if (STRINGP (obj))
18668 *multibyte = STRING_MULTIBYTE (obj);
18669 return (char *) SDATA (obj);
18671 else
18672 return "";
18676 /* Count up to COUNT lines starting from START / START_BYTE.
18677 But don't go beyond LIMIT_BYTE.
18678 Return the number of lines thus found (always nonnegative).
18680 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18682 static int
18683 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18684 int start, start_byte, limit_byte, count;
18685 int *byte_pos_ptr;
18687 register unsigned char *cursor;
18688 unsigned char *base;
18690 register int ceiling;
18691 register unsigned char *ceiling_addr;
18692 int orig_count = count;
18694 /* If we are not in selective display mode,
18695 check only for newlines. */
18696 int selective_display = (!NILP (current_buffer->selective_display)
18697 && !INTEGERP (current_buffer->selective_display));
18699 if (count > 0)
18701 while (start_byte < limit_byte)
18703 ceiling = BUFFER_CEILING_OF (start_byte);
18704 ceiling = min (limit_byte - 1, ceiling);
18705 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18706 base = (cursor = BYTE_POS_ADDR (start_byte));
18707 while (1)
18709 if (selective_display)
18710 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18712 else
18713 while (*cursor != '\n' && ++cursor != ceiling_addr)
18716 if (cursor != ceiling_addr)
18718 if (--count == 0)
18720 start_byte += cursor - base + 1;
18721 *byte_pos_ptr = start_byte;
18722 return orig_count;
18724 else
18725 if (++cursor == ceiling_addr)
18726 break;
18728 else
18729 break;
18731 start_byte += cursor - base;
18734 else
18736 while (start_byte > limit_byte)
18738 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18739 ceiling = max (limit_byte, ceiling);
18740 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18741 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18742 while (1)
18744 if (selective_display)
18745 while (--cursor != ceiling_addr
18746 && *cursor != '\n' && *cursor != 015)
18748 else
18749 while (--cursor != ceiling_addr && *cursor != '\n')
18752 if (cursor != ceiling_addr)
18754 if (++count == 0)
18756 start_byte += cursor - base + 1;
18757 *byte_pos_ptr = start_byte;
18758 /* When scanning backwards, we should
18759 not count the newline posterior to which we stop. */
18760 return - orig_count - 1;
18763 else
18764 break;
18766 /* Here we add 1 to compensate for the last decrement
18767 of CURSOR, which took it past the valid range. */
18768 start_byte += cursor - base + 1;
18772 *byte_pos_ptr = limit_byte;
18774 if (count < 0)
18775 return - orig_count + count;
18776 return orig_count - count;
18782 /***********************************************************************
18783 Displaying strings
18784 ***********************************************************************/
18786 /* Display a NUL-terminated string, starting with index START.
18788 If STRING is non-null, display that C string. Otherwise, the Lisp
18789 string LISP_STRING is displayed.
18791 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18792 FACE_STRING. Display STRING or LISP_STRING with the face at
18793 FACE_STRING_POS in FACE_STRING:
18795 Display the string in the environment given by IT, but use the
18796 standard display table, temporarily.
18798 FIELD_WIDTH is the minimum number of output glyphs to produce.
18799 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18800 with spaces. If STRING has more characters, more than FIELD_WIDTH
18801 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18803 PRECISION is the maximum number of characters to output from
18804 STRING. PRECISION < 0 means don't truncate the string.
18806 This is roughly equivalent to printf format specifiers:
18808 FIELD_WIDTH PRECISION PRINTF
18809 ----------------------------------------
18810 -1 -1 %s
18811 -1 10 %.10s
18812 10 -1 %10s
18813 20 10 %20.10s
18815 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18816 display them, and < 0 means obey the current buffer's value of
18817 enable_multibyte_characters.
18819 Value is the number of columns displayed. */
18821 static int
18822 display_string (string, lisp_string, face_string, face_string_pos,
18823 start, it, field_width, precision, max_x, multibyte)
18824 unsigned char *string;
18825 Lisp_Object lisp_string;
18826 Lisp_Object face_string;
18827 EMACS_INT face_string_pos;
18828 EMACS_INT start;
18829 struct it *it;
18830 int field_width, precision, max_x;
18831 int multibyte;
18833 int hpos_at_start = it->hpos;
18834 int saved_face_id = it->face_id;
18835 struct glyph_row *row = it->glyph_row;
18837 /* Initialize the iterator IT for iteration over STRING beginning
18838 with index START. */
18839 reseat_to_string (it, string, lisp_string, start,
18840 precision, field_width, multibyte);
18842 /* If displaying STRING, set up the face of the iterator
18843 from LISP_STRING, if that's given. */
18844 if (STRINGP (face_string))
18846 EMACS_INT endptr;
18847 struct face *face;
18849 it->face_id
18850 = face_at_string_position (it->w, face_string, face_string_pos,
18851 0, it->region_beg_charpos,
18852 it->region_end_charpos,
18853 &endptr, it->base_face_id, 0);
18854 face = FACE_FROM_ID (it->f, it->face_id);
18855 it->face_box_p = face->box != FACE_NO_BOX;
18858 /* Set max_x to the maximum allowed X position. Don't let it go
18859 beyond the right edge of the window. */
18860 if (max_x <= 0)
18861 max_x = it->last_visible_x;
18862 else
18863 max_x = min (max_x, it->last_visible_x);
18865 /* Skip over display elements that are not visible. because IT->w is
18866 hscrolled. */
18867 if (it->current_x < it->first_visible_x)
18868 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18869 MOVE_TO_POS | MOVE_TO_X);
18871 row->ascent = it->max_ascent;
18872 row->height = it->max_ascent + it->max_descent;
18873 row->phys_ascent = it->max_phys_ascent;
18874 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18875 row->extra_line_spacing = it->max_extra_line_spacing;
18877 /* This condition is for the case that we are called with current_x
18878 past last_visible_x. */
18879 while (it->current_x < max_x)
18881 int x_before, x, n_glyphs_before, i, nglyphs;
18883 /* Get the next display element. */
18884 if (!get_next_display_element (it))
18885 break;
18887 /* Produce glyphs. */
18888 x_before = it->current_x;
18889 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18890 PRODUCE_GLYPHS (it);
18892 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18893 i = 0;
18894 x = x_before;
18895 while (i < nglyphs)
18897 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18899 if (it->line_wrap != TRUNCATE
18900 && x + glyph->pixel_width > max_x)
18902 /* End of continued line or max_x reached. */
18903 if (CHAR_GLYPH_PADDING_P (*glyph))
18905 /* A wide character is unbreakable. */
18906 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18907 it->current_x = x_before;
18909 else
18911 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18912 it->current_x = x;
18914 break;
18916 else if (x + glyph->pixel_width >= it->first_visible_x)
18918 /* Glyph is at least partially visible. */
18919 ++it->hpos;
18920 if (x < it->first_visible_x)
18921 it->glyph_row->x = x - it->first_visible_x;
18923 else
18925 /* Glyph is off the left margin of the display area.
18926 Should not happen. */
18927 abort ();
18930 row->ascent = max (row->ascent, it->max_ascent);
18931 row->height = max (row->height, it->max_ascent + it->max_descent);
18932 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18933 row->phys_height = max (row->phys_height,
18934 it->max_phys_ascent + it->max_phys_descent);
18935 row->extra_line_spacing = max (row->extra_line_spacing,
18936 it->max_extra_line_spacing);
18937 x += glyph->pixel_width;
18938 ++i;
18941 /* Stop if max_x reached. */
18942 if (i < nglyphs)
18943 break;
18945 /* Stop at line ends. */
18946 if (ITERATOR_AT_END_OF_LINE_P (it))
18948 it->continuation_lines_width = 0;
18949 break;
18952 set_iterator_to_next (it, 1);
18954 /* Stop if truncating at the right edge. */
18955 if (it->line_wrap == TRUNCATE
18956 && it->current_x >= it->last_visible_x)
18958 /* Add truncation mark, but don't do it if the line is
18959 truncated at a padding space. */
18960 if (IT_CHARPOS (*it) < it->string_nchars)
18962 if (!FRAME_WINDOW_P (it->f))
18964 int i, n;
18966 if (it->current_x > it->last_visible_x)
18968 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18969 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18970 break;
18971 for (n = row->used[TEXT_AREA]; i < n; ++i)
18973 row->used[TEXT_AREA] = i;
18974 produce_special_glyphs (it, IT_TRUNCATION);
18977 produce_special_glyphs (it, IT_TRUNCATION);
18979 it->glyph_row->truncated_on_right_p = 1;
18981 break;
18985 /* Maybe insert a truncation at the left. */
18986 if (it->first_visible_x
18987 && IT_CHARPOS (*it) > 0)
18989 if (!FRAME_WINDOW_P (it->f))
18990 insert_left_trunc_glyphs (it);
18991 it->glyph_row->truncated_on_left_p = 1;
18994 it->face_id = saved_face_id;
18996 /* Value is number of columns displayed. */
18997 return it->hpos - hpos_at_start;
19002 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19003 appears as an element of LIST or as the car of an element of LIST.
19004 If PROPVAL is a list, compare each element against LIST in that
19005 way, and return 1/2 if any element of PROPVAL is found in LIST.
19006 Otherwise return 0. This function cannot quit.
19007 The return value is 2 if the text is invisible but with an ellipsis
19008 and 1 if it's invisible and without an ellipsis. */
19011 invisible_p (propval, list)
19012 register Lisp_Object propval;
19013 Lisp_Object list;
19015 register Lisp_Object tail, proptail;
19017 for (tail = list; CONSP (tail); tail = XCDR (tail))
19019 register Lisp_Object tem;
19020 tem = XCAR (tail);
19021 if (EQ (propval, tem))
19022 return 1;
19023 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19024 return NILP (XCDR (tem)) ? 1 : 2;
19027 if (CONSP (propval))
19029 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19031 Lisp_Object propelt;
19032 propelt = XCAR (proptail);
19033 for (tail = list; CONSP (tail); tail = XCDR (tail))
19035 register Lisp_Object tem;
19036 tem = XCAR (tail);
19037 if (EQ (propelt, tem))
19038 return 1;
19039 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19040 return NILP (XCDR (tem)) ? 1 : 2;
19045 return 0;
19048 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19049 doc: /* Non-nil if the property makes the text invisible.
19050 POS-OR-PROP can be a marker or number, in which case it is taken to be
19051 a position in the current buffer and the value of the `invisible' property
19052 is checked; or it can be some other value, which is then presumed to be the
19053 value of the `invisible' property of the text of interest.
19054 The non-nil value returned can be t for truly invisible text or something
19055 else if the text is replaced by an ellipsis. */)
19056 (pos_or_prop)
19057 Lisp_Object pos_or_prop;
19059 Lisp_Object prop
19060 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19061 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19062 : pos_or_prop);
19063 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19064 return (invis == 0 ? Qnil
19065 : invis == 1 ? Qt
19066 : make_number (invis));
19069 /* Calculate a width or height in pixels from a specification using
19070 the following elements:
19072 SPEC ::=
19073 NUM - a (fractional) multiple of the default font width/height
19074 (NUM) - specifies exactly NUM pixels
19075 UNIT - a fixed number of pixels, see below.
19076 ELEMENT - size of a display element in pixels, see below.
19077 (NUM . SPEC) - equals NUM * SPEC
19078 (+ SPEC SPEC ...) - add pixel values
19079 (- SPEC SPEC ...) - subtract pixel values
19080 (- SPEC) - negate pixel value
19082 NUM ::=
19083 INT or FLOAT - a number constant
19084 SYMBOL - use symbol's (buffer local) variable binding.
19086 UNIT ::=
19087 in - pixels per inch *)
19088 mm - pixels per 1/1000 meter *)
19089 cm - pixels per 1/100 meter *)
19090 width - width of current font in pixels.
19091 height - height of current font in pixels.
19093 *) using the ratio(s) defined in display-pixels-per-inch.
19095 ELEMENT ::=
19097 left-fringe - left fringe width in pixels
19098 right-fringe - right fringe width in pixels
19100 left-margin - left margin width in pixels
19101 right-margin - right margin width in pixels
19103 scroll-bar - scroll-bar area width in pixels
19105 Examples:
19107 Pixels corresponding to 5 inches:
19108 (5 . in)
19110 Total width of non-text areas on left side of window (if scroll-bar is on left):
19111 '(space :width (+ left-fringe left-margin scroll-bar))
19113 Align to first text column (in header line):
19114 '(space :align-to 0)
19116 Align to middle of text area minus half the width of variable `my-image'
19117 containing a loaded image:
19118 '(space :align-to (0.5 . (- text my-image)))
19120 Width of left margin minus width of 1 character in the default font:
19121 '(space :width (- left-margin 1))
19123 Width of left margin minus width of 2 characters in the current font:
19124 '(space :width (- left-margin (2 . width)))
19126 Center 1 character over left-margin (in header line):
19127 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19129 Different ways to express width of left fringe plus left margin minus one pixel:
19130 '(space :width (- (+ left-fringe left-margin) (1)))
19131 '(space :width (+ left-fringe left-margin (- (1))))
19132 '(space :width (+ left-fringe left-margin (-1)))
19136 #define NUMVAL(X) \
19137 ((INTEGERP (X) || FLOATP (X)) \
19138 ? XFLOATINT (X) \
19139 : - 1)
19142 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19143 double *res;
19144 struct it *it;
19145 Lisp_Object prop;
19146 struct font *font;
19147 int width_p, *align_to;
19149 double pixels;
19151 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19152 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19154 if (NILP (prop))
19155 return OK_PIXELS (0);
19157 xassert (FRAME_LIVE_P (it->f));
19159 if (SYMBOLP (prop))
19161 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19163 char *unit = SDATA (SYMBOL_NAME (prop));
19165 if (unit[0] == 'i' && unit[1] == 'n')
19166 pixels = 1.0;
19167 else if (unit[0] == 'm' && unit[1] == 'm')
19168 pixels = 25.4;
19169 else if (unit[0] == 'c' && unit[1] == 'm')
19170 pixels = 2.54;
19171 else
19172 pixels = 0;
19173 if (pixels > 0)
19175 double ppi;
19176 #ifdef HAVE_WINDOW_SYSTEM
19177 if (FRAME_WINDOW_P (it->f)
19178 && (ppi = (width_p
19179 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19180 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19181 ppi > 0))
19182 return OK_PIXELS (ppi / pixels);
19183 #endif
19185 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19186 || (CONSP (Vdisplay_pixels_per_inch)
19187 && (ppi = (width_p
19188 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19189 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19190 ppi > 0)))
19191 return OK_PIXELS (ppi / pixels);
19193 return 0;
19197 #ifdef HAVE_WINDOW_SYSTEM
19198 if (EQ (prop, Qheight))
19199 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19200 if (EQ (prop, Qwidth))
19201 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19202 #else
19203 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19204 return OK_PIXELS (1);
19205 #endif
19207 if (EQ (prop, Qtext))
19208 return OK_PIXELS (width_p
19209 ? window_box_width (it->w, TEXT_AREA)
19210 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19212 if (align_to && *align_to < 0)
19214 *res = 0;
19215 if (EQ (prop, Qleft))
19216 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19217 if (EQ (prop, Qright))
19218 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19219 if (EQ (prop, Qcenter))
19220 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19221 + window_box_width (it->w, TEXT_AREA) / 2);
19222 if (EQ (prop, Qleft_fringe))
19223 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19224 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19225 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19226 if (EQ (prop, Qright_fringe))
19227 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19228 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19229 : window_box_right_offset (it->w, TEXT_AREA));
19230 if (EQ (prop, Qleft_margin))
19231 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19232 if (EQ (prop, Qright_margin))
19233 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19234 if (EQ (prop, Qscroll_bar))
19235 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19237 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19238 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19239 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19240 : 0)));
19242 else
19244 if (EQ (prop, Qleft_fringe))
19245 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19246 if (EQ (prop, Qright_fringe))
19247 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19248 if (EQ (prop, Qleft_margin))
19249 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19250 if (EQ (prop, Qright_margin))
19251 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19252 if (EQ (prop, Qscroll_bar))
19253 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19256 prop = Fbuffer_local_value (prop, it->w->buffer);
19259 if (INTEGERP (prop) || FLOATP (prop))
19261 int base_unit = (width_p
19262 ? FRAME_COLUMN_WIDTH (it->f)
19263 : FRAME_LINE_HEIGHT (it->f));
19264 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19267 if (CONSP (prop))
19269 Lisp_Object car = XCAR (prop);
19270 Lisp_Object cdr = XCDR (prop);
19272 if (SYMBOLP (car))
19274 #ifdef HAVE_WINDOW_SYSTEM
19275 if (FRAME_WINDOW_P (it->f)
19276 && valid_image_p (prop))
19278 int id = lookup_image (it->f, prop);
19279 struct image *img = IMAGE_FROM_ID (it->f, id);
19281 return OK_PIXELS (width_p ? img->width : img->height);
19283 #endif
19284 if (EQ (car, Qplus) || EQ (car, Qminus))
19286 int first = 1;
19287 double px;
19289 pixels = 0;
19290 while (CONSP (cdr))
19292 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19293 font, width_p, align_to))
19294 return 0;
19295 if (first)
19296 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19297 else
19298 pixels += px;
19299 cdr = XCDR (cdr);
19301 if (EQ (car, Qminus))
19302 pixels = -pixels;
19303 return OK_PIXELS (pixels);
19306 car = Fbuffer_local_value (car, it->w->buffer);
19309 if (INTEGERP (car) || FLOATP (car))
19311 double fact;
19312 pixels = XFLOATINT (car);
19313 if (NILP (cdr))
19314 return OK_PIXELS (pixels);
19315 if (calc_pixel_width_or_height (&fact, it, cdr,
19316 font, width_p, align_to))
19317 return OK_PIXELS (pixels * fact);
19318 return 0;
19321 return 0;
19324 return 0;
19328 /***********************************************************************
19329 Glyph Display
19330 ***********************************************************************/
19332 #ifdef HAVE_WINDOW_SYSTEM
19334 #if GLYPH_DEBUG
19336 void
19337 dump_glyph_string (s)
19338 struct glyph_string *s;
19340 fprintf (stderr, "glyph string\n");
19341 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19342 s->x, s->y, s->width, s->height);
19343 fprintf (stderr, " ybase = %d\n", s->ybase);
19344 fprintf (stderr, " hl = %d\n", s->hl);
19345 fprintf (stderr, " left overhang = %d, right = %d\n",
19346 s->left_overhang, s->right_overhang);
19347 fprintf (stderr, " nchars = %d\n", s->nchars);
19348 fprintf (stderr, " extends to end of line = %d\n",
19349 s->extends_to_end_of_line_p);
19350 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19351 fprintf (stderr, " bg width = %d\n", s->background_width);
19354 #endif /* GLYPH_DEBUG */
19356 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19357 of XChar2b structures for S; it can't be allocated in
19358 init_glyph_string because it must be allocated via `alloca'. W
19359 is the window on which S is drawn. ROW and AREA are the glyph row
19360 and area within the row from which S is constructed. START is the
19361 index of the first glyph structure covered by S. HL is a
19362 face-override for drawing S. */
19364 #ifdef HAVE_NTGUI
19365 #define OPTIONAL_HDC(hdc) hdc,
19366 #define DECLARE_HDC(hdc) HDC hdc;
19367 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19368 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19369 #endif
19371 #ifndef OPTIONAL_HDC
19372 #define OPTIONAL_HDC(hdc)
19373 #define DECLARE_HDC(hdc)
19374 #define ALLOCATE_HDC(hdc, f)
19375 #define RELEASE_HDC(hdc, f)
19376 #endif
19378 static void
19379 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19380 struct glyph_string *s;
19381 DECLARE_HDC (hdc)
19382 XChar2b *char2b;
19383 struct window *w;
19384 struct glyph_row *row;
19385 enum glyph_row_area area;
19386 int start;
19387 enum draw_glyphs_face hl;
19389 bzero (s, sizeof *s);
19390 s->w = w;
19391 s->f = XFRAME (w->frame);
19392 #ifdef HAVE_NTGUI
19393 s->hdc = hdc;
19394 #endif
19395 s->display = FRAME_X_DISPLAY (s->f);
19396 s->window = FRAME_X_WINDOW (s->f);
19397 s->char2b = char2b;
19398 s->hl = hl;
19399 s->row = row;
19400 s->area = area;
19401 s->first_glyph = row->glyphs[area] + start;
19402 s->height = row->height;
19403 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19405 /* Display the internal border below the tool-bar window. */
19406 if (WINDOWP (s->f->tool_bar_window)
19407 && s->w == XWINDOW (s->f->tool_bar_window))
19408 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19410 s->ybase = s->y + row->ascent;
19414 /* Append the list of glyph strings with head H and tail T to the list
19415 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19417 static INLINE void
19418 append_glyph_string_lists (head, tail, h, t)
19419 struct glyph_string **head, **tail;
19420 struct glyph_string *h, *t;
19422 if (h)
19424 if (*head)
19425 (*tail)->next = h;
19426 else
19427 *head = h;
19428 h->prev = *tail;
19429 *tail = t;
19434 /* Prepend the list of glyph strings with head H and tail T to the
19435 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19436 result. */
19438 static INLINE void
19439 prepend_glyph_string_lists (head, tail, h, t)
19440 struct glyph_string **head, **tail;
19441 struct glyph_string *h, *t;
19443 if (h)
19445 if (*head)
19446 (*head)->prev = t;
19447 else
19448 *tail = t;
19449 t->next = *head;
19450 *head = h;
19455 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19456 Set *HEAD and *TAIL to the resulting list. */
19458 static INLINE void
19459 append_glyph_string (head, tail, s)
19460 struct glyph_string **head, **tail;
19461 struct glyph_string *s;
19463 s->next = s->prev = NULL;
19464 append_glyph_string_lists (head, tail, s, s);
19468 /* Get face and two-byte form of character C in face FACE_ID on frame
19469 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19470 means we want to display multibyte text. DISPLAY_P non-zero means
19471 make sure that X resources for the face returned are allocated.
19472 Value is a pointer to a realized face that is ready for display if
19473 DISPLAY_P is non-zero. */
19475 static INLINE struct face *
19476 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19477 struct frame *f;
19478 int c, face_id;
19479 XChar2b *char2b;
19480 int multibyte_p, display_p;
19482 struct face *face = FACE_FROM_ID (f, face_id);
19484 if (face->font)
19486 unsigned code = face->font->driver->encode_char (face->font, c);
19488 if (code != FONT_INVALID_CODE)
19489 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19490 else
19491 STORE_XCHAR2B (char2b, 0, 0);
19494 /* Make sure X resources of the face are allocated. */
19495 #ifdef HAVE_X_WINDOWS
19496 if (display_p)
19497 #endif
19499 xassert (face != NULL);
19500 PREPARE_FACE_FOR_DISPLAY (f, face);
19503 return face;
19507 /* Get face and two-byte form of character glyph GLYPH on frame F.
19508 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19509 a pointer to a realized face that is ready for display. */
19511 static INLINE struct face *
19512 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19513 struct frame *f;
19514 struct glyph *glyph;
19515 XChar2b *char2b;
19516 int *two_byte_p;
19518 struct face *face;
19520 xassert (glyph->type == CHAR_GLYPH);
19521 face = FACE_FROM_ID (f, glyph->face_id);
19523 if (two_byte_p)
19524 *two_byte_p = 0;
19526 if (face->font)
19528 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19530 if (code != FONT_INVALID_CODE)
19531 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19532 else
19533 STORE_XCHAR2B (char2b, 0, 0);
19536 /* Make sure X resources of the face are allocated. */
19537 xassert (face != NULL);
19538 PREPARE_FACE_FOR_DISPLAY (f, face);
19539 return face;
19543 /* Fill glyph string S with composition components specified by S->cmp.
19545 BASE_FACE is the base face of the composition.
19546 S->cmp_from is the index of the first component for S.
19548 OVERLAPS non-zero means S should draw the foreground only, and use
19549 its physical height for clipping. See also draw_glyphs.
19551 Value is the index of a component not in S. */
19553 static int
19554 fill_composite_glyph_string (s, base_face, overlaps)
19555 struct glyph_string *s;
19556 struct face *base_face;
19557 int overlaps;
19559 int i;
19560 /* For all glyphs of this composition, starting at the offset
19561 S->cmp_from, until we reach the end of the definition or encounter a
19562 glyph that requires the different face, add it to S. */
19563 struct face *face;
19565 xassert (s);
19567 s->for_overlaps = overlaps;
19568 s->face = NULL;
19569 s->font = NULL;
19570 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19572 int c = COMPOSITION_GLYPH (s->cmp, i);
19574 if (c != '\t')
19576 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19577 -1, Qnil);
19579 face = get_char_face_and_encoding (s->f, c, face_id,
19580 s->char2b + i, 1, 1);
19581 if (face)
19583 if (! s->face)
19585 s->face = face;
19586 s->font = s->face->font;
19588 else if (s->face != face)
19589 break;
19592 ++s->nchars;
19594 s->cmp_to = i;
19596 /* All glyph strings for the same composition has the same width,
19597 i.e. the width set for the first component of the composition. */
19598 s->width = s->first_glyph->pixel_width;
19600 /* If the specified font could not be loaded, use the frame's
19601 default font, but record the fact that we couldn't load it in
19602 the glyph string so that we can draw rectangles for the
19603 characters of the glyph string. */
19604 if (s->font == NULL)
19606 s->font_not_found_p = 1;
19607 s->font = FRAME_FONT (s->f);
19610 /* Adjust base line for subscript/superscript text. */
19611 s->ybase += s->first_glyph->voffset;
19613 /* This glyph string must always be drawn with 16-bit functions. */
19614 s->two_byte_p = 1;
19616 return s->cmp_to;
19619 static int
19620 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19621 struct glyph_string *s;
19622 int face_id;
19623 int start, end, overlaps;
19625 struct glyph *glyph, *last;
19626 Lisp_Object lgstring;
19627 int i;
19629 s->for_overlaps = overlaps;
19630 glyph = s->row->glyphs[s->area] + start;
19631 last = s->row->glyphs[s->area] + end;
19632 s->cmp_id = glyph->u.cmp.id;
19633 s->cmp_from = glyph->u.cmp.from;
19634 s->cmp_to = glyph->u.cmp.to + 1;
19635 s->face = FACE_FROM_ID (s->f, face_id);
19636 lgstring = composition_gstring_from_id (s->cmp_id);
19637 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19638 glyph++;
19639 while (glyph < last
19640 && glyph->u.cmp.automatic
19641 && glyph->u.cmp.id == s->cmp_id
19642 && s->cmp_to == glyph->u.cmp.from)
19643 s->cmp_to = (glyph++)->u.cmp.to + 1;
19645 for (i = s->cmp_from; i < s->cmp_to; i++)
19647 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19648 unsigned code = LGLYPH_CODE (lglyph);
19650 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19652 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19653 return glyph - s->row->glyphs[s->area];
19657 /* Fill glyph string S from a sequence of character glyphs.
19659 FACE_ID is the face id of the string. START is the index of the
19660 first glyph to consider, END is the index of the last + 1.
19661 OVERLAPS non-zero means S should draw the foreground only, and use
19662 its physical height for clipping. See also draw_glyphs.
19664 Value is the index of the first glyph not in S. */
19666 static int
19667 fill_glyph_string (s, face_id, start, end, overlaps)
19668 struct glyph_string *s;
19669 int face_id;
19670 int start, end, overlaps;
19672 struct glyph *glyph, *last;
19673 int voffset;
19674 int glyph_not_available_p;
19676 xassert (s->f == XFRAME (s->w->frame));
19677 xassert (s->nchars == 0);
19678 xassert (start >= 0 && end > start);
19680 s->for_overlaps = overlaps;
19681 glyph = s->row->glyphs[s->area] + start;
19682 last = s->row->glyphs[s->area] + end;
19683 voffset = glyph->voffset;
19684 s->padding_p = glyph->padding_p;
19685 glyph_not_available_p = glyph->glyph_not_available_p;
19687 while (glyph < last
19688 && glyph->type == CHAR_GLYPH
19689 && glyph->voffset == voffset
19690 /* Same face id implies same font, nowadays. */
19691 && glyph->face_id == face_id
19692 && glyph->glyph_not_available_p == glyph_not_available_p)
19694 int two_byte_p;
19696 s->face = get_glyph_face_and_encoding (s->f, glyph,
19697 s->char2b + s->nchars,
19698 &two_byte_p);
19699 s->two_byte_p = two_byte_p;
19700 ++s->nchars;
19701 xassert (s->nchars <= end - start);
19702 s->width += glyph->pixel_width;
19703 if (glyph++->padding_p != s->padding_p)
19704 break;
19707 s->font = s->face->font;
19709 /* If the specified font could not be loaded, use the frame's font,
19710 but record the fact that we couldn't load it in
19711 S->font_not_found_p so that we can draw rectangles for the
19712 characters of the glyph string. */
19713 if (s->font == NULL || glyph_not_available_p)
19715 s->font_not_found_p = 1;
19716 s->font = FRAME_FONT (s->f);
19719 /* Adjust base line for subscript/superscript text. */
19720 s->ybase += voffset;
19722 xassert (s->face && s->face->gc);
19723 return glyph - s->row->glyphs[s->area];
19727 /* Fill glyph string S from image glyph S->first_glyph. */
19729 static void
19730 fill_image_glyph_string (s)
19731 struct glyph_string *s;
19733 xassert (s->first_glyph->type == IMAGE_GLYPH);
19734 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19735 xassert (s->img);
19736 s->slice = s->first_glyph->slice;
19737 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19738 s->font = s->face->font;
19739 s->width = s->first_glyph->pixel_width;
19741 /* Adjust base line for subscript/superscript text. */
19742 s->ybase += s->first_glyph->voffset;
19746 /* Fill glyph string S from a sequence of stretch glyphs.
19748 ROW is the glyph row in which the glyphs are found, AREA is the
19749 area within the row. START is the index of the first glyph to
19750 consider, END is the index of the last + 1.
19752 Value is the index of the first glyph not in S. */
19754 static int
19755 fill_stretch_glyph_string (s, row, area, start, end)
19756 struct glyph_string *s;
19757 struct glyph_row *row;
19758 enum glyph_row_area area;
19759 int start, end;
19761 struct glyph *glyph, *last;
19762 int voffset, face_id;
19764 xassert (s->first_glyph->type == STRETCH_GLYPH);
19766 glyph = s->row->glyphs[s->area] + start;
19767 last = s->row->glyphs[s->area] + end;
19768 face_id = glyph->face_id;
19769 s->face = FACE_FROM_ID (s->f, face_id);
19770 s->font = s->face->font;
19771 s->width = glyph->pixel_width;
19772 s->nchars = 1;
19773 voffset = glyph->voffset;
19775 for (++glyph;
19776 (glyph < last
19777 && glyph->type == STRETCH_GLYPH
19778 && glyph->voffset == voffset
19779 && glyph->face_id == face_id);
19780 ++glyph)
19781 s->width += glyph->pixel_width;
19783 /* Adjust base line for subscript/superscript text. */
19784 s->ybase += voffset;
19786 /* The case that face->gc == 0 is handled when drawing the glyph
19787 string by calling PREPARE_FACE_FOR_DISPLAY. */
19788 xassert (s->face);
19789 return glyph - s->row->glyphs[s->area];
19792 static struct font_metrics *
19793 get_per_char_metric (f, font, char2b)
19794 struct frame *f;
19795 struct font *font;
19796 XChar2b *char2b;
19798 static struct font_metrics metrics;
19799 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19801 if (! font || code == FONT_INVALID_CODE)
19802 return NULL;
19803 font->driver->text_extents (font, &code, 1, &metrics);
19804 return &metrics;
19807 /* EXPORT for RIF:
19808 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19809 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19810 assumed to be zero. */
19812 void
19813 x_get_glyph_overhangs (glyph, f, left, right)
19814 struct glyph *glyph;
19815 struct frame *f;
19816 int *left, *right;
19818 *left = *right = 0;
19820 if (glyph->type == CHAR_GLYPH)
19822 struct face *face;
19823 XChar2b char2b;
19824 struct font_metrics *pcm;
19826 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19827 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19829 if (pcm->rbearing > pcm->width)
19830 *right = pcm->rbearing - pcm->width;
19831 if (pcm->lbearing < 0)
19832 *left = -pcm->lbearing;
19835 else if (glyph->type == COMPOSITE_GLYPH)
19837 if (! glyph->u.cmp.automatic)
19839 struct composition *cmp = composition_table[glyph->u.cmp.id];
19841 if (cmp->rbearing > cmp->pixel_width)
19842 *right = cmp->rbearing - cmp->pixel_width;
19843 if (cmp->lbearing < 0)
19844 *left = - cmp->lbearing;
19846 else
19848 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19849 struct font_metrics metrics;
19851 composition_gstring_width (gstring, glyph->u.cmp.from,
19852 glyph->u.cmp.to + 1, &metrics);
19853 if (metrics.rbearing > metrics.width)
19854 *right = metrics.rbearing - metrics.width;
19855 if (metrics.lbearing < 0)
19856 *left = - metrics.lbearing;
19862 /* Return the index of the first glyph preceding glyph string S that
19863 is overwritten by S because of S's left overhang. Value is -1
19864 if no glyphs are overwritten. */
19866 static int
19867 left_overwritten (s)
19868 struct glyph_string *s;
19870 int k;
19872 if (s->left_overhang)
19874 int x = 0, i;
19875 struct glyph *glyphs = s->row->glyphs[s->area];
19876 int first = s->first_glyph - glyphs;
19878 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19879 x -= glyphs[i].pixel_width;
19881 k = i + 1;
19883 else
19884 k = -1;
19886 return k;
19890 /* Return the index of the first glyph preceding glyph string S that
19891 is overwriting S because of its right overhang. Value is -1 if no
19892 glyph in front of S overwrites S. */
19894 static int
19895 left_overwriting (s)
19896 struct glyph_string *s;
19898 int i, k, x;
19899 struct glyph *glyphs = s->row->glyphs[s->area];
19900 int first = s->first_glyph - glyphs;
19902 k = -1;
19903 x = 0;
19904 for (i = first - 1; i >= 0; --i)
19906 int left, right;
19907 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19908 if (x + right > 0)
19909 k = i;
19910 x -= glyphs[i].pixel_width;
19913 return k;
19917 /* Return the index of the last glyph following glyph string S that is
19918 overwritten by S because of S's right overhang. Value is -1 if
19919 no such glyph is found. */
19921 static int
19922 right_overwritten (s)
19923 struct glyph_string *s;
19925 int k = -1;
19927 if (s->right_overhang)
19929 int x = 0, i;
19930 struct glyph *glyphs = s->row->glyphs[s->area];
19931 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19932 int end = s->row->used[s->area];
19934 for (i = first; i < end && s->right_overhang > x; ++i)
19935 x += glyphs[i].pixel_width;
19937 k = i;
19940 return k;
19944 /* Return the index of the last glyph following glyph string S that
19945 overwrites S because of its left overhang. Value is negative
19946 if no such glyph is found. */
19948 static int
19949 right_overwriting (s)
19950 struct glyph_string *s;
19952 int i, k, x;
19953 int end = s->row->used[s->area];
19954 struct glyph *glyphs = s->row->glyphs[s->area];
19955 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19957 k = -1;
19958 x = 0;
19959 for (i = first; i < end; ++i)
19961 int left, right;
19962 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19963 if (x - left < 0)
19964 k = i;
19965 x += glyphs[i].pixel_width;
19968 return k;
19972 /* Set background width of glyph string S. START is the index of the
19973 first glyph following S. LAST_X is the right-most x-position + 1
19974 in the drawing area. */
19976 static INLINE void
19977 set_glyph_string_background_width (s, start, last_x)
19978 struct glyph_string *s;
19979 int start;
19980 int last_x;
19982 /* If the face of this glyph string has to be drawn to the end of
19983 the drawing area, set S->extends_to_end_of_line_p. */
19985 if (start == s->row->used[s->area]
19986 && s->area == TEXT_AREA
19987 && ((s->row->fill_line_p
19988 && (s->hl == DRAW_NORMAL_TEXT
19989 || s->hl == DRAW_IMAGE_RAISED
19990 || s->hl == DRAW_IMAGE_SUNKEN))
19991 || s->hl == DRAW_MOUSE_FACE))
19992 s->extends_to_end_of_line_p = 1;
19994 /* If S extends its face to the end of the line, set its
19995 background_width to the distance to the right edge of the drawing
19996 area. */
19997 if (s->extends_to_end_of_line_p)
19998 s->background_width = last_x - s->x + 1;
19999 else
20000 s->background_width = s->width;
20004 /* Compute overhangs and x-positions for glyph string S and its
20005 predecessors, or successors. X is the starting x-position for S.
20006 BACKWARD_P non-zero means process predecessors. */
20008 static void
20009 compute_overhangs_and_x (s, x, backward_p)
20010 struct glyph_string *s;
20011 int x;
20012 int backward_p;
20014 if (backward_p)
20016 while (s)
20018 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20019 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20020 x -= s->width;
20021 s->x = x;
20022 s = s->prev;
20025 else
20027 while (s)
20029 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20030 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20031 s->x = x;
20032 x += s->width;
20033 s = s->next;
20040 /* The following macros are only called from draw_glyphs below.
20041 They reference the following parameters of that function directly:
20042 `w', `row', `area', and `overlap_p'
20043 as well as the following local variables:
20044 `s', `f', and `hdc' (in W32) */
20046 #ifdef HAVE_NTGUI
20047 /* On W32, silently add local `hdc' variable to argument list of
20048 init_glyph_string. */
20049 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20050 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20051 #else
20052 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20053 init_glyph_string (s, char2b, w, row, area, start, hl)
20054 #endif
20056 /* Add a glyph string for a stretch glyph to the list of strings
20057 between HEAD and TAIL. START is the index of the stretch glyph in
20058 row area AREA of glyph row ROW. END is the index of the last glyph
20059 in that glyph row area. X is the current output position assigned
20060 to the new glyph string constructed. HL overrides that face of the
20061 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20062 is the right-most x-position of the drawing area. */
20064 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20065 and below -- keep them on one line. */
20066 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20067 do \
20069 s = (struct glyph_string *) alloca (sizeof *s); \
20070 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20071 START = fill_stretch_glyph_string (s, row, area, START, END); \
20072 append_glyph_string (&HEAD, &TAIL, s); \
20073 s->x = (X); \
20075 while (0)
20078 /* Add a glyph string for an image glyph to the list of strings
20079 between HEAD and TAIL. START is the index of the image glyph in
20080 row area AREA of glyph row ROW. END is the index of the last glyph
20081 in that glyph row area. X is the current output position assigned
20082 to the new glyph string constructed. HL overrides that face of the
20083 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20084 is the right-most x-position of the drawing area. */
20086 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20087 do \
20089 s = (struct glyph_string *) alloca (sizeof *s); \
20090 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20091 fill_image_glyph_string (s); \
20092 append_glyph_string (&HEAD, &TAIL, s); \
20093 ++START; \
20094 s->x = (X); \
20096 while (0)
20099 /* Add a glyph string for a sequence of character glyphs to the list
20100 of strings between HEAD and TAIL. START is the index of the first
20101 glyph in row area AREA of glyph row ROW that is part of the new
20102 glyph string. END is the index of the last glyph in that glyph row
20103 area. X is the current output position assigned to the new glyph
20104 string constructed. HL overrides that face of the glyph; e.g. it
20105 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20106 right-most x-position of the drawing area. */
20108 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20109 do \
20111 int face_id; \
20112 XChar2b *char2b; \
20114 face_id = (row)->glyphs[area][START].face_id; \
20116 s = (struct glyph_string *) alloca (sizeof *s); \
20117 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20118 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20119 append_glyph_string (&HEAD, &TAIL, s); \
20120 s->x = (X); \
20121 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20123 while (0)
20126 /* Add a glyph string for a composite sequence to the list of strings
20127 between HEAD and TAIL. START is the index of the first glyph in
20128 row area AREA of glyph row ROW that is part of the new glyph
20129 string. END is the index of the last glyph in that glyph row area.
20130 X is the current output position assigned to the new glyph string
20131 constructed. HL overrides that face of the glyph; e.g. it is
20132 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20133 x-position of the drawing area. */
20135 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20136 do { \
20137 int face_id = (row)->glyphs[area][START].face_id; \
20138 struct face *base_face = FACE_FROM_ID (f, face_id); \
20139 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20140 struct composition *cmp = composition_table[cmp_id]; \
20141 XChar2b *char2b; \
20142 struct glyph_string *first_s; \
20143 int n; \
20145 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20147 /* Make glyph_strings for each glyph sequence that is drawable by \
20148 the same face, and append them to HEAD/TAIL. */ \
20149 for (n = 0; n < cmp->glyph_len;) \
20151 s = (struct glyph_string *) alloca (sizeof *s); \
20152 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20153 append_glyph_string (&(HEAD), &(TAIL), s); \
20154 s->cmp = cmp; \
20155 s->cmp_from = n; \
20156 s->x = (X); \
20157 if (n == 0) \
20158 first_s = s; \
20159 n = fill_composite_glyph_string (s, base_face, overlaps); \
20162 ++START; \
20163 s = first_s; \
20164 } while (0)
20167 /* Add a glyph string for a glyph-string sequence to the list of strings
20168 between HEAD and TAIL. */
20170 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20171 do { \
20172 int face_id; \
20173 XChar2b *char2b; \
20174 Lisp_Object gstring; \
20176 face_id = (row)->glyphs[area][START].face_id; \
20177 gstring = (composition_gstring_from_id \
20178 ((row)->glyphs[area][START].u.cmp.id)); \
20179 s = (struct glyph_string *) alloca (sizeof *s); \
20180 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20181 * LGSTRING_GLYPH_LEN (gstring)); \
20182 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20183 append_glyph_string (&(HEAD), &(TAIL), s); \
20184 s->x = (X); \
20185 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20186 } while (0)
20189 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20190 of AREA of glyph row ROW on window W between indices START and END.
20191 HL overrides the face for drawing glyph strings, e.g. it is
20192 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20193 x-positions of the drawing area.
20195 This is an ugly monster macro construct because we must use alloca
20196 to allocate glyph strings (because draw_glyphs can be called
20197 asynchronously). */
20199 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20200 do \
20202 HEAD = TAIL = NULL; \
20203 while (START < END) \
20205 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20206 switch (first_glyph->type) \
20208 case CHAR_GLYPH: \
20209 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20210 HL, X, LAST_X); \
20211 break; \
20213 case COMPOSITE_GLYPH: \
20214 if (first_glyph->u.cmp.automatic) \
20215 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20216 HL, X, LAST_X); \
20217 else \
20218 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20219 HL, X, LAST_X); \
20220 break; \
20222 case STRETCH_GLYPH: \
20223 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20224 HL, X, LAST_X); \
20225 break; \
20227 case IMAGE_GLYPH: \
20228 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20229 HL, X, LAST_X); \
20230 break; \
20232 default: \
20233 abort (); \
20236 if (s) \
20238 set_glyph_string_background_width (s, START, LAST_X); \
20239 (X) += s->width; \
20242 } while (0)
20245 /* Draw glyphs between START and END in AREA of ROW on window W,
20246 starting at x-position X. X is relative to AREA in W. HL is a
20247 face-override with the following meaning:
20249 DRAW_NORMAL_TEXT draw normally
20250 DRAW_CURSOR draw in cursor face
20251 DRAW_MOUSE_FACE draw in mouse face.
20252 DRAW_INVERSE_VIDEO draw in mode line face
20253 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20254 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20256 If OVERLAPS is non-zero, draw only the foreground of characters and
20257 clip to the physical height of ROW. Non-zero value also defines
20258 the overlapping part to be drawn:
20260 OVERLAPS_PRED overlap with preceding rows
20261 OVERLAPS_SUCC overlap with succeeding rows
20262 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20263 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20265 Value is the x-position reached, relative to AREA of W. */
20267 static int
20268 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20269 struct window *w;
20270 int x;
20271 struct glyph_row *row;
20272 enum glyph_row_area area;
20273 EMACS_INT start, end;
20274 enum draw_glyphs_face hl;
20275 int overlaps;
20277 struct glyph_string *head, *tail;
20278 struct glyph_string *s;
20279 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20280 int i, j, x_reached, last_x, area_left = 0;
20281 struct frame *f = XFRAME (WINDOW_FRAME (w));
20282 DECLARE_HDC (hdc);
20284 ALLOCATE_HDC (hdc, f);
20286 /* Let's rather be paranoid than getting a SEGV. */
20287 end = min (end, row->used[area]);
20288 start = max (0, start);
20289 start = min (end, start);
20291 /* Translate X to frame coordinates. Set last_x to the right
20292 end of the drawing area. */
20293 if (row->full_width_p)
20295 /* X is relative to the left edge of W, without scroll bars
20296 or fringes. */
20297 area_left = WINDOW_LEFT_EDGE_X (w);
20298 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20300 else
20302 area_left = window_box_left (w, area);
20303 last_x = area_left + window_box_width (w, area);
20305 x += area_left;
20307 /* Build a doubly-linked list of glyph_string structures between
20308 head and tail from what we have to draw. Note that the macro
20309 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20310 the reason we use a separate variable `i'. */
20311 i = start;
20312 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20313 if (tail)
20314 x_reached = tail->x + tail->background_width;
20315 else
20316 x_reached = x;
20318 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20319 the row, redraw some glyphs in front or following the glyph
20320 strings built above. */
20321 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20323 struct glyph_string *h, *t;
20324 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20325 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20326 int dummy_x = 0;
20328 /* If mouse highlighting is on, we may need to draw adjacent
20329 glyphs using mouse-face highlighting. */
20330 if (area == TEXT_AREA && row->mouse_face_p)
20332 struct glyph_row *mouse_beg_row, *mouse_end_row;
20334 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20335 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20337 if (row >= mouse_beg_row && row <= mouse_end_row)
20339 check_mouse_face = 1;
20340 mouse_beg_col = (row == mouse_beg_row)
20341 ? dpyinfo->mouse_face_beg_col : 0;
20342 mouse_end_col = (row == mouse_end_row)
20343 ? dpyinfo->mouse_face_end_col
20344 : row->used[TEXT_AREA];
20348 /* Compute overhangs for all glyph strings. */
20349 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20350 for (s = head; s; s = s->next)
20351 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20353 /* Prepend glyph strings for glyphs in front of the first glyph
20354 string that are overwritten because of the first glyph
20355 string's left overhang. The background of all strings
20356 prepended must be drawn because the first glyph string
20357 draws over it. */
20358 i = left_overwritten (head);
20359 if (i >= 0)
20361 enum draw_glyphs_face overlap_hl;
20363 /* If this row contains mouse highlighting, attempt to draw
20364 the overlapped glyphs with the correct highlight. This
20365 code fails if the overlap encompasses more than one glyph
20366 and mouse-highlight spans only some of these glyphs.
20367 However, making it work perfectly involves a lot more
20368 code, and I don't know if the pathological case occurs in
20369 practice, so we'll stick to this for now. --- cyd */
20370 if (check_mouse_face
20371 && mouse_beg_col < start && mouse_end_col > i)
20372 overlap_hl = DRAW_MOUSE_FACE;
20373 else
20374 overlap_hl = DRAW_NORMAL_TEXT;
20376 j = i;
20377 BUILD_GLYPH_STRINGS (j, start, h, t,
20378 overlap_hl, dummy_x, last_x);
20379 compute_overhangs_and_x (t, head->x, 1);
20380 prepend_glyph_string_lists (&head, &tail, h, t);
20381 clip_head = head;
20384 /* Prepend glyph strings for glyphs in front of the first glyph
20385 string that overwrite that glyph string because of their
20386 right overhang. For these strings, only the foreground must
20387 be drawn, because it draws over the glyph string at `head'.
20388 The background must not be drawn because this would overwrite
20389 right overhangs of preceding glyphs for which no glyph
20390 strings exist. */
20391 i = left_overwriting (head);
20392 if (i >= 0)
20394 enum draw_glyphs_face overlap_hl;
20396 if (check_mouse_face
20397 && mouse_beg_col < start && mouse_end_col > i)
20398 overlap_hl = DRAW_MOUSE_FACE;
20399 else
20400 overlap_hl = DRAW_NORMAL_TEXT;
20402 clip_head = head;
20403 BUILD_GLYPH_STRINGS (i, start, h, t,
20404 overlap_hl, dummy_x, last_x);
20405 for (s = h; s; s = s->next)
20406 s->background_filled_p = 1;
20407 compute_overhangs_and_x (t, head->x, 1);
20408 prepend_glyph_string_lists (&head, &tail, h, t);
20411 /* Append glyphs strings for glyphs following the last glyph
20412 string tail that are overwritten by tail. The background of
20413 these strings has to be drawn because tail's foreground draws
20414 over it. */
20415 i = right_overwritten (tail);
20416 if (i >= 0)
20418 enum draw_glyphs_face overlap_hl;
20420 if (check_mouse_face
20421 && mouse_beg_col < i && mouse_end_col > end)
20422 overlap_hl = DRAW_MOUSE_FACE;
20423 else
20424 overlap_hl = DRAW_NORMAL_TEXT;
20426 BUILD_GLYPH_STRINGS (end, i, h, t,
20427 overlap_hl, x, last_x);
20428 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20429 append_glyph_string_lists (&head, &tail, h, t);
20430 clip_tail = tail;
20433 /* Append glyph strings for glyphs following the last glyph
20434 string tail that overwrite tail. The foreground of such
20435 glyphs has to be drawn because it writes into the background
20436 of tail. The background must not be drawn because it could
20437 paint over the foreground of following glyphs. */
20438 i = right_overwriting (tail);
20439 if (i >= 0)
20441 enum draw_glyphs_face overlap_hl;
20442 if (check_mouse_face
20443 && mouse_beg_col < i && mouse_end_col > end)
20444 overlap_hl = DRAW_MOUSE_FACE;
20445 else
20446 overlap_hl = DRAW_NORMAL_TEXT;
20448 clip_tail = tail;
20449 i++; /* We must include the Ith glyph. */
20450 BUILD_GLYPH_STRINGS (end, i, h, t,
20451 overlap_hl, x, last_x);
20452 for (s = h; s; s = s->next)
20453 s->background_filled_p = 1;
20454 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20455 append_glyph_string_lists (&head, &tail, h, t);
20457 if (clip_head || clip_tail)
20458 for (s = head; s; s = s->next)
20460 s->clip_head = clip_head;
20461 s->clip_tail = clip_tail;
20465 /* Draw all strings. */
20466 for (s = head; s; s = s->next)
20467 FRAME_RIF (f)->draw_glyph_string (s);
20469 #ifndef HAVE_NS
20470 /* When focus a sole frame and move horizontally, this sets on_p to 0
20471 causing a failure to erase prev cursor position. */
20472 if (area == TEXT_AREA
20473 && !row->full_width_p
20474 /* When drawing overlapping rows, only the glyph strings'
20475 foreground is drawn, which doesn't erase a cursor
20476 completely. */
20477 && !overlaps)
20479 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20480 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20481 : (tail ? tail->x + tail->background_width : x));
20482 x0 -= area_left;
20483 x1 -= area_left;
20485 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20486 row->y, MATRIX_ROW_BOTTOM_Y (row));
20488 #endif
20490 /* Value is the x-position up to which drawn, relative to AREA of W.
20491 This doesn't include parts drawn because of overhangs. */
20492 if (row->full_width_p)
20493 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20494 else
20495 x_reached -= area_left;
20497 RELEASE_HDC (hdc, f);
20499 return x_reached;
20502 /* Expand row matrix if too narrow. Don't expand if area
20503 is not present. */
20505 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20507 if (!fonts_changed_p \
20508 && (it->glyph_row->glyphs[area] \
20509 < it->glyph_row->glyphs[area + 1])) \
20511 it->w->ncols_scale_factor++; \
20512 fonts_changed_p = 1; \
20516 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20517 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20519 static INLINE void
20520 append_glyph (it)
20521 struct it *it;
20523 struct glyph *glyph;
20524 enum glyph_row_area area = it->area;
20526 xassert (it->glyph_row);
20527 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20529 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20530 if (glyph < it->glyph_row->glyphs[area + 1])
20532 glyph->charpos = CHARPOS (it->position);
20533 glyph->object = it->object;
20534 if (it->pixel_width > 0)
20536 glyph->pixel_width = it->pixel_width;
20537 glyph->padding_p = 0;
20539 else
20541 /* Assure at least 1-pixel width. Otherwise, cursor can't
20542 be displayed correctly. */
20543 glyph->pixel_width = 1;
20544 glyph->padding_p = 1;
20546 glyph->ascent = it->ascent;
20547 glyph->descent = it->descent;
20548 glyph->voffset = it->voffset;
20549 glyph->type = CHAR_GLYPH;
20550 glyph->avoid_cursor_p = it->avoid_cursor_p;
20551 glyph->multibyte_p = it->multibyte_p;
20552 glyph->left_box_line_p = it->start_of_box_run_p;
20553 glyph->right_box_line_p = it->end_of_box_run_p;
20554 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20555 || it->phys_descent > it->descent);
20556 glyph->glyph_not_available_p = it->glyph_not_available_p;
20557 glyph->face_id = it->face_id;
20558 glyph->u.ch = it->char_to_display;
20559 glyph->slice = null_glyph_slice;
20560 glyph->font_type = FONT_TYPE_UNKNOWN;
20561 ++it->glyph_row->used[area];
20563 else
20564 IT_EXPAND_MATRIX_WIDTH (it, area);
20567 /* Store one glyph for the composition IT->cmp_it.id in
20568 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20569 non-null. */
20571 static INLINE void
20572 append_composite_glyph (it)
20573 struct it *it;
20575 struct glyph *glyph;
20576 enum glyph_row_area area = it->area;
20578 xassert (it->glyph_row);
20580 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20581 if (glyph < it->glyph_row->glyphs[area + 1])
20583 glyph->charpos = CHARPOS (it->position);
20584 glyph->object = it->object;
20585 glyph->pixel_width = it->pixel_width;
20586 glyph->ascent = it->ascent;
20587 glyph->descent = it->descent;
20588 glyph->voffset = it->voffset;
20589 glyph->type = COMPOSITE_GLYPH;
20590 if (it->cmp_it.ch < 0)
20592 glyph->u.cmp.automatic = 0;
20593 glyph->u.cmp.id = it->cmp_it.id;
20595 else
20597 glyph->u.cmp.automatic = 1;
20598 glyph->u.cmp.id = it->cmp_it.id;
20599 glyph->u.cmp.from = it->cmp_it.from;
20600 glyph->u.cmp.to = it->cmp_it.to - 1;
20602 glyph->avoid_cursor_p = it->avoid_cursor_p;
20603 glyph->multibyte_p = it->multibyte_p;
20604 glyph->left_box_line_p = it->start_of_box_run_p;
20605 glyph->right_box_line_p = it->end_of_box_run_p;
20606 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20607 || it->phys_descent > it->descent);
20608 glyph->padding_p = 0;
20609 glyph->glyph_not_available_p = 0;
20610 glyph->face_id = it->face_id;
20611 glyph->slice = null_glyph_slice;
20612 glyph->font_type = FONT_TYPE_UNKNOWN;
20613 ++it->glyph_row->used[area];
20615 else
20616 IT_EXPAND_MATRIX_WIDTH (it, area);
20620 /* Change IT->ascent and IT->height according to the setting of
20621 IT->voffset. */
20623 static INLINE void
20624 take_vertical_position_into_account (it)
20625 struct it *it;
20627 if (it->voffset)
20629 if (it->voffset < 0)
20630 /* Increase the ascent so that we can display the text higher
20631 in the line. */
20632 it->ascent -= it->voffset;
20633 else
20634 /* Increase the descent so that we can display the text lower
20635 in the line. */
20636 it->descent += it->voffset;
20641 /* Produce glyphs/get display metrics for the image IT is loaded with.
20642 See the description of struct display_iterator in dispextern.h for
20643 an overview of struct display_iterator. */
20645 static void
20646 produce_image_glyph (it)
20647 struct it *it;
20649 struct image *img;
20650 struct face *face;
20651 int glyph_ascent, crop;
20652 struct glyph_slice slice;
20654 xassert (it->what == IT_IMAGE);
20656 face = FACE_FROM_ID (it->f, it->face_id);
20657 xassert (face);
20658 /* Make sure X resources of the face is loaded. */
20659 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20661 if (it->image_id < 0)
20663 /* Fringe bitmap. */
20664 it->ascent = it->phys_ascent = 0;
20665 it->descent = it->phys_descent = 0;
20666 it->pixel_width = 0;
20667 it->nglyphs = 0;
20668 return;
20671 img = IMAGE_FROM_ID (it->f, it->image_id);
20672 xassert (img);
20673 /* Make sure X resources of the image is loaded. */
20674 prepare_image_for_display (it->f, img);
20676 slice.x = slice.y = 0;
20677 slice.width = img->width;
20678 slice.height = img->height;
20680 if (INTEGERP (it->slice.x))
20681 slice.x = XINT (it->slice.x);
20682 else if (FLOATP (it->slice.x))
20683 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20685 if (INTEGERP (it->slice.y))
20686 slice.y = XINT (it->slice.y);
20687 else if (FLOATP (it->slice.y))
20688 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20690 if (INTEGERP (it->slice.width))
20691 slice.width = XINT (it->slice.width);
20692 else if (FLOATP (it->slice.width))
20693 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20695 if (INTEGERP (it->slice.height))
20696 slice.height = XINT (it->slice.height);
20697 else if (FLOATP (it->slice.height))
20698 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20700 if (slice.x >= img->width)
20701 slice.x = img->width;
20702 if (slice.y >= img->height)
20703 slice.y = img->height;
20704 if (slice.x + slice.width >= img->width)
20705 slice.width = img->width - slice.x;
20706 if (slice.y + slice.height > img->height)
20707 slice.height = img->height - slice.y;
20709 if (slice.width == 0 || slice.height == 0)
20710 return;
20712 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20714 it->descent = slice.height - glyph_ascent;
20715 if (slice.y == 0)
20716 it->descent += img->vmargin;
20717 if (slice.y + slice.height == img->height)
20718 it->descent += img->vmargin;
20719 it->phys_descent = it->descent;
20721 it->pixel_width = slice.width;
20722 if (slice.x == 0)
20723 it->pixel_width += img->hmargin;
20724 if (slice.x + slice.width == img->width)
20725 it->pixel_width += img->hmargin;
20727 /* It's quite possible for images to have an ascent greater than
20728 their height, so don't get confused in that case. */
20729 if (it->descent < 0)
20730 it->descent = 0;
20732 it->nglyphs = 1;
20734 if (face->box != FACE_NO_BOX)
20736 if (face->box_line_width > 0)
20738 if (slice.y == 0)
20739 it->ascent += face->box_line_width;
20740 if (slice.y + slice.height == img->height)
20741 it->descent += face->box_line_width;
20744 if (it->start_of_box_run_p && slice.x == 0)
20745 it->pixel_width += eabs (face->box_line_width);
20746 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20747 it->pixel_width += eabs (face->box_line_width);
20750 take_vertical_position_into_account (it);
20752 /* Automatically crop wide image glyphs at right edge so we can
20753 draw the cursor on same display row. */
20754 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20755 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20757 it->pixel_width -= crop;
20758 slice.width -= crop;
20761 if (it->glyph_row)
20763 struct glyph *glyph;
20764 enum glyph_row_area area = it->area;
20766 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20767 if (glyph < it->glyph_row->glyphs[area + 1])
20769 glyph->charpos = CHARPOS (it->position);
20770 glyph->object = it->object;
20771 glyph->pixel_width = it->pixel_width;
20772 glyph->ascent = glyph_ascent;
20773 glyph->descent = it->descent;
20774 glyph->voffset = it->voffset;
20775 glyph->type = IMAGE_GLYPH;
20776 glyph->avoid_cursor_p = it->avoid_cursor_p;
20777 glyph->multibyte_p = it->multibyte_p;
20778 glyph->left_box_line_p = it->start_of_box_run_p;
20779 glyph->right_box_line_p = it->end_of_box_run_p;
20780 glyph->overlaps_vertically_p = 0;
20781 glyph->padding_p = 0;
20782 glyph->glyph_not_available_p = 0;
20783 glyph->face_id = it->face_id;
20784 glyph->u.img_id = img->id;
20785 glyph->slice = slice;
20786 glyph->font_type = FONT_TYPE_UNKNOWN;
20787 ++it->glyph_row->used[area];
20789 else
20790 IT_EXPAND_MATRIX_WIDTH (it, area);
20795 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20796 of the glyph, WIDTH and HEIGHT are the width and height of the
20797 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20799 static void
20800 append_stretch_glyph (it, object, width, height, ascent)
20801 struct it *it;
20802 Lisp_Object object;
20803 int width, height;
20804 int ascent;
20806 struct glyph *glyph;
20807 enum glyph_row_area area = it->area;
20809 xassert (ascent >= 0 && ascent <= height);
20811 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20812 if (glyph < it->glyph_row->glyphs[area + 1])
20814 glyph->charpos = CHARPOS (it->position);
20815 glyph->object = object;
20816 glyph->pixel_width = width;
20817 glyph->ascent = ascent;
20818 glyph->descent = height - ascent;
20819 glyph->voffset = it->voffset;
20820 glyph->type = STRETCH_GLYPH;
20821 glyph->avoid_cursor_p = it->avoid_cursor_p;
20822 glyph->multibyte_p = it->multibyte_p;
20823 glyph->left_box_line_p = it->start_of_box_run_p;
20824 glyph->right_box_line_p = it->end_of_box_run_p;
20825 glyph->overlaps_vertically_p = 0;
20826 glyph->padding_p = 0;
20827 glyph->glyph_not_available_p = 0;
20828 glyph->face_id = it->face_id;
20829 glyph->u.stretch.ascent = ascent;
20830 glyph->u.stretch.height = height;
20831 glyph->slice = null_glyph_slice;
20832 glyph->font_type = FONT_TYPE_UNKNOWN;
20833 ++it->glyph_row->used[area];
20835 else
20836 IT_EXPAND_MATRIX_WIDTH (it, area);
20840 /* Produce a stretch glyph for iterator IT. IT->object is the value
20841 of the glyph property displayed. The value must be a list
20842 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20843 being recognized:
20845 1. `:width WIDTH' specifies that the space should be WIDTH *
20846 canonical char width wide. WIDTH may be an integer or floating
20847 point number.
20849 2. `:relative-width FACTOR' specifies that the width of the stretch
20850 should be computed from the width of the first character having the
20851 `glyph' property, and should be FACTOR times that width.
20853 3. `:align-to HPOS' specifies that the space should be wide enough
20854 to reach HPOS, a value in canonical character units.
20856 Exactly one of the above pairs must be present.
20858 4. `:height HEIGHT' specifies that the height of the stretch produced
20859 should be HEIGHT, measured in canonical character units.
20861 5. `:relative-height FACTOR' specifies that the height of the
20862 stretch should be FACTOR times the height of the characters having
20863 the glyph property.
20865 Either none or exactly one of 4 or 5 must be present.
20867 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20868 of the stretch should be used for the ascent of the stretch.
20869 ASCENT must be in the range 0 <= ASCENT <= 100. */
20871 static void
20872 produce_stretch_glyph (it)
20873 struct it *it;
20875 /* (space :width WIDTH :height HEIGHT ...) */
20876 Lisp_Object prop, plist;
20877 int width = 0, height = 0, align_to = -1;
20878 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20879 int ascent = 0;
20880 double tem;
20881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20882 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20884 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20886 /* List should start with `space'. */
20887 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20888 plist = XCDR (it->object);
20890 /* Compute the width of the stretch. */
20891 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20892 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20894 /* Absolute width `:width WIDTH' specified and valid. */
20895 zero_width_ok_p = 1;
20896 width = (int)tem;
20898 else if (prop = Fplist_get (plist, QCrelative_width),
20899 NUMVAL (prop) > 0)
20901 /* Relative width `:relative-width FACTOR' specified and valid.
20902 Compute the width of the characters having the `glyph'
20903 property. */
20904 struct it it2;
20905 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20907 it2 = *it;
20908 if (it->multibyte_p)
20910 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20911 - IT_BYTEPOS (*it));
20912 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20914 else
20915 it2.c = *p, it2.len = 1;
20917 it2.glyph_row = NULL;
20918 it2.what = IT_CHARACTER;
20919 x_produce_glyphs (&it2);
20920 width = NUMVAL (prop) * it2.pixel_width;
20922 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20923 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20925 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20926 align_to = (align_to < 0
20928 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20929 else if (align_to < 0)
20930 align_to = window_box_left_offset (it->w, TEXT_AREA);
20931 width = max (0, (int)tem + align_to - it->current_x);
20932 zero_width_ok_p = 1;
20934 else
20935 /* Nothing specified -> width defaults to canonical char width. */
20936 width = FRAME_COLUMN_WIDTH (it->f);
20938 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20939 width = 1;
20941 /* Compute height. */
20942 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20943 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20945 height = (int)tem;
20946 zero_height_ok_p = 1;
20948 else if (prop = Fplist_get (plist, QCrelative_height),
20949 NUMVAL (prop) > 0)
20950 height = FONT_HEIGHT (font) * NUMVAL (prop);
20951 else
20952 height = FONT_HEIGHT (font);
20954 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20955 height = 1;
20957 /* Compute percentage of height used for ascent. If
20958 `:ascent ASCENT' is present and valid, use that. Otherwise,
20959 derive the ascent from the font in use. */
20960 if (prop = Fplist_get (plist, QCascent),
20961 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20962 ascent = height * NUMVAL (prop) / 100.0;
20963 else if (!NILP (prop)
20964 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20965 ascent = min (max (0, (int)tem), height);
20966 else
20967 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20969 if (width > 0 && it->line_wrap != TRUNCATE
20970 && it->current_x + width > it->last_visible_x)
20971 width = it->last_visible_x - it->current_x - 1;
20973 if (width > 0 && height > 0 && it->glyph_row)
20975 Lisp_Object object = it->stack[it->sp - 1].string;
20976 if (!STRINGP (object))
20977 object = it->w->buffer;
20978 append_stretch_glyph (it, object, width, height, ascent);
20981 it->pixel_width = width;
20982 it->ascent = it->phys_ascent = ascent;
20983 it->descent = it->phys_descent = height - it->ascent;
20984 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20986 take_vertical_position_into_account (it);
20989 /* Calculate line-height and line-spacing properties.
20990 An integer value specifies explicit pixel value.
20991 A float value specifies relative value to current face height.
20992 A cons (float . face-name) specifies relative value to
20993 height of specified face font.
20995 Returns height in pixels, or nil. */
20998 static Lisp_Object
20999 calc_line_height_property (it, val, font, boff, override)
21000 struct it *it;
21001 Lisp_Object val;
21002 struct font *font;
21003 int boff, override;
21005 Lisp_Object face_name = Qnil;
21006 int ascent, descent, height;
21008 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21009 return val;
21011 if (CONSP (val))
21013 face_name = XCAR (val);
21014 val = XCDR (val);
21015 if (!NUMBERP (val))
21016 val = make_number (1);
21017 if (NILP (face_name))
21019 height = it->ascent + it->descent;
21020 goto scale;
21024 if (NILP (face_name))
21026 font = FRAME_FONT (it->f);
21027 boff = FRAME_BASELINE_OFFSET (it->f);
21029 else if (EQ (face_name, Qt))
21031 override = 0;
21033 else
21035 int face_id;
21036 struct face *face;
21038 face_id = lookup_named_face (it->f, face_name, 0);
21039 if (face_id < 0)
21040 return make_number (-1);
21042 face = FACE_FROM_ID (it->f, face_id);
21043 font = face->font;
21044 if (font == NULL)
21045 return make_number (-1);
21046 boff = font->baseline_offset;
21047 if (font->vertical_centering)
21048 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21051 ascent = FONT_BASE (font) + boff;
21052 descent = FONT_DESCENT (font) - boff;
21054 if (override)
21056 it->override_ascent = ascent;
21057 it->override_descent = descent;
21058 it->override_boff = boff;
21061 height = ascent + descent;
21063 scale:
21064 if (FLOATP (val))
21065 height = (int)(XFLOAT_DATA (val) * height);
21066 else if (INTEGERP (val))
21067 height *= XINT (val);
21069 return make_number (height);
21073 /* RIF:
21074 Produce glyphs/get display metrics for the display element IT is
21075 loaded with. See the description of struct it in dispextern.h
21076 for an overview of struct it. */
21078 void
21079 x_produce_glyphs (it)
21080 struct it *it;
21082 int extra_line_spacing = it->extra_line_spacing;
21084 it->glyph_not_available_p = 0;
21086 if (it->what == IT_CHARACTER)
21088 XChar2b char2b;
21089 struct font *font;
21090 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21091 struct font_metrics *pcm;
21092 int font_not_found_p;
21093 int boff; /* baseline offset */
21094 /* We may change it->multibyte_p upon unibyte<->multibyte
21095 conversion. So, save the current value now and restore it
21096 later.
21098 Note: It seems that we don't have to record multibyte_p in
21099 struct glyph because the character code itself tells if or
21100 not the character is multibyte. Thus, in the future, we must
21101 consider eliminating the field `multibyte_p' in the struct
21102 glyph. */
21103 int saved_multibyte_p = it->multibyte_p;
21105 /* Maybe translate single-byte characters to multibyte, or the
21106 other way. */
21107 it->char_to_display = it->c;
21108 if (!ASCII_BYTE_P (it->c)
21109 && ! it->multibyte_p)
21111 if (SINGLE_BYTE_CHAR_P (it->c)
21112 && unibyte_display_via_language_environment)
21114 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21116 /* get_next_display_element assures that this decoding
21117 never fails. */
21118 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21119 it->multibyte_p = 1;
21120 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21121 -1, Qnil);
21122 face = FACE_FROM_ID (it->f, it->face_id);
21126 /* Get font to use. Encode IT->char_to_display. */
21127 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21128 &char2b, it->multibyte_p, 0);
21129 font = face->font;
21131 /* When no suitable font found, use the default font. */
21132 font_not_found_p = font == NULL;
21133 if (font_not_found_p)
21135 font = FRAME_FONT (it->f);
21136 boff = FRAME_BASELINE_OFFSET (it->f);
21138 else
21140 boff = font->baseline_offset;
21141 if (font->vertical_centering)
21142 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21145 if (it->char_to_display >= ' '
21146 && (!it->multibyte_p || it->char_to_display < 128))
21148 /* Either unibyte or ASCII. */
21149 int stretched_p;
21151 it->nglyphs = 1;
21153 pcm = get_per_char_metric (it->f, font, &char2b);
21155 if (it->override_ascent >= 0)
21157 it->ascent = it->override_ascent;
21158 it->descent = it->override_descent;
21159 boff = it->override_boff;
21161 else
21163 it->ascent = FONT_BASE (font) + boff;
21164 it->descent = FONT_DESCENT (font) - boff;
21167 if (pcm)
21169 it->phys_ascent = pcm->ascent + boff;
21170 it->phys_descent = pcm->descent - boff;
21171 it->pixel_width = pcm->width;
21173 else
21175 it->glyph_not_available_p = 1;
21176 it->phys_ascent = it->ascent;
21177 it->phys_descent = it->descent;
21178 it->pixel_width = FONT_WIDTH (font);
21181 if (it->constrain_row_ascent_descent_p)
21183 if (it->descent > it->max_descent)
21185 it->ascent += it->descent - it->max_descent;
21186 it->descent = it->max_descent;
21188 if (it->ascent > it->max_ascent)
21190 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21191 it->ascent = it->max_ascent;
21193 it->phys_ascent = min (it->phys_ascent, it->ascent);
21194 it->phys_descent = min (it->phys_descent, it->descent);
21195 extra_line_spacing = 0;
21198 /* If this is a space inside a region of text with
21199 `space-width' property, change its width. */
21200 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21201 if (stretched_p)
21202 it->pixel_width *= XFLOATINT (it->space_width);
21204 /* If face has a box, add the box thickness to the character
21205 height. If character has a box line to the left and/or
21206 right, add the box line width to the character's width. */
21207 if (face->box != FACE_NO_BOX)
21209 int thick = face->box_line_width;
21211 if (thick > 0)
21213 it->ascent += thick;
21214 it->descent += thick;
21216 else
21217 thick = -thick;
21219 if (it->start_of_box_run_p)
21220 it->pixel_width += thick;
21221 if (it->end_of_box_run_p)
21222 it->pixel_width += thick;
21225 /* If face has an overline, add the height of the overline
21226 (1 pixel) and a 1 pixel margin to the character height. */
21227 if (face->overline_p)
21228 it->ascent += overline_margin;
21230 if (it->constrain_row_ascent_descent_p)
21232 if (it->ascent > it->max_ascent)
21233 it->ascent = it->max_ascent;
21234 if (it->descent > it->max_descent)
21235 it->descent = it->max_descent;
21238 take_vertical_position_into_account (it);
21240 /* If we have to actually produce glyphs, do it. */
21241 if (it->glyph_row)
21243 if (stretched_p)
21245 /* Translate a space with a `space-width' property
21246 into a stretch glyph. */
21247 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21248 / FONT_HEIGHT (font));
21249 append_stretch_glyph (it, it->object, it->pixel_width,
21250 it->ascent + it->descent, ascent);
21252 else
21253 append_glyph (it);
21255 /* If characters with lbearing or rbearing are displayed
21256 in this line, record that fact in a flag of the
21257 glyph row. This is used to optimize X output code. */
21258 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21259 it->glyph_row->contains_overlapping_glyphs_p = 1;
21261 if (! stretched_p && it->pixel_width == 0)
21262 /* We assure that all visible glyphs have at least 1-pixel
21263 width. */
21264 it->pixel_width = 1;
21266 else if (it->char_to_display == '\n')
21268 /* A newline has no width but we need the height of the line.
21269 But if previous part of the line set a height, don't
21270 increase that height */
21272 Lisp_Object height;
21273 Lisp_Object total_height = Qnil;
21275 it->override_ascent = -1;
21276 it->pixel_width = 0;
21277 it->nglyphs = 0;
21279 height = get_it_property(it, Qline_height);
21280 /* Split (line-height total-height) list */
21281 if (CONSP (height)
21282 && CONSP (XCDR (height))
21283 && NILP (XCDR (XCDR (height))))
21285 total_height = XCAR (XCDR (height));
21286 height = XCAR (height);
21288 height = calc_line_height_property(it, height, font, boff, 1);
21290 if (it->override_ascent >= 0)
21292 it->ascent = it->override_ascent;
21293 it->descent = it->override_descent;
21294 boff = it->override_boff;
21296 else
21298 it->ascent = FONT_BASE (font) + boff;
21299 it->descent = FONT_DESCENT (font) - boff;
21302 if (EQ (height, Qt))
21304 if (it->descent > it->max_descent)
21306 it->ascent += it->descent - it->max_descent;
21307 it->descent = it->max_descent;
21309 if (it->ascent > it->max_ascent)
21311 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21312 it->ascent = it->max_ascent;
21314 it->phys_ascent = min (it->phys_ascent, it->ascent);
21315 it->phys_descent = min (it->phys_descent, it->descent);
21316 it->constrain_row_ascent_descent_p = 1;
21317 extra_line_spacing = 0;
21319 else
21321 Lisp_Object spacing;
21323 it->phys_ascent = it->ascent;
21324 it->phys_descent = it->descent;
21326 if ((it->max_ascent > 0 || it->max_descent > 0)
21327 && face->box != FACE_NO_BOX
21328 && face->box_line_width > 0)
21330 it->ascent += face->box_line_width;
21331 it->descent += face->box_line_width;
21333 if (!NILP (height)
21334 && XINT (height) > it->ascent + it->descent)
21335 it->ascent = XINT (height) - it->descent;
21337 if (!NILP (total_height))
21338 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21339 else
21341 spacing = get_it_property(it, Qline_spacing);
21342 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21344 if (INTEGERP (spacing))
21346 extra_line_spacing = XINT (spacing);
21347 if (!NILP (total_height))
21348 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21352 else if (it->char_to_display == '\t')
21354 if (font->space_width > 0)
21356 int tab_width = it->tab_width * font->space_width;
21357 int x = it->current_x + it->continuation_lines_width;
21358 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21360 /* If the distance from the current position to the next tab
21361 stop is less than a space character width, use the
21362 tab stop after that. */
21363 if (next_tab_x - x < font->space_width)
21364 next_tab_x += tab_width;
21366 it->pixel_width = next_tab_x - x;
21367 it->nglyphs = 1;
21368 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21369 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21371 if (it->glyph_row)
21373 append_stretch_glyph (it, it->object, it->pixel_width,
21374 it->ascent + it->descent, it->ascent);
21377 else
21379 it->pixel_width = 0;
21380 it->nglyphs = 1;
21383 else
21385 /* A multi-byte character. Assume that the display width of the
21386 character is the width of the character multiplied by the
21387 width of the font. */
21389 /* If we found a font, this font should give us the right
21390 metrics. If we didn't find a font, use the frame's
21391 default font and calculate the width of the character by
21392 multiplying the width of font by the width of the
21393 character. */
21395 pcm = get_per_char_metric (it->f, font, &char2b);
21397 if (font_not_found_p || !pcm)
21399 int char_width = CHAR_WIDTH (it->char_to_display);
21401 if (char_width == 0)
21402 /* This is a non spacing character. But, as we are
21403 going to display an empty box, the box must occupy
21404 at least one column. */
21405 char_width = 1;
21406 it->glyph_not_available_p = 1;
21407 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21408 it->phys_ascent = FONT_BASE (font) + boff;
21409 it->phys_descent = FONT_DESCENT (font) - boff;
21411 else
21413 it->pixel_width = pcm->width;
21414 it->phys_ascent = pcm->ascent + boff;
21415 it->phys_descent = pcm->descent - boff;
21416 if (it->glyph_row
21417 && (pcm->lbearing < 0
21418 || pcm->rbearing > pcm->width))
21419 it->glyph_row->contains_overlapping_glyphs_p = 1;
21421 it->nglyphs = 1;
21422 it->ascent = FONT_BASE (font) + boff;
21423 it->descent = FONT_DESCENT (font) - boff;
21424 if (face->box != FACE_NO_BOX)
21426 int thick = face->box_line_width;
21428 if (thick > 0)
21430 it->ascent += thick;
21431 it->descent += thick;
21433 else
21434 thick = - thick;
21436 if (it->start_of_box_run_p)
21437 it->pixel_width += thick;
21438 if (it->end_of_box_run_p)
21439 it->pixel_width += thick;
21442 /* If face has an overline, add the height of the overline
21443 (1 pixel) and a 1 pixel margin to the character height. */
21444 if (face->overline_p)
21445 it->ascent += overline_margin;
21447 take_vertical_position_into_account (it);
21449 if (it->ascent < 0)
21450 it->ascent = 0;
21451 if (it->descent < 0)
21452 it->descent = 0;
21454 if (it->glyph_row)
21455 append_glyph (it);
21456 if (it->pixel_width == 0)
21457 /* We assure that all visible glyphs have at least 1-pixel
21458 width. */
21459 it->pixel_width = 1;
21461 it->multibyte_p = saved_multibyte_p;
21463 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21465 /* A static compositoin.
21467 Note: A composition is represented as one glyph in the
21468 glyph matrix. There are no padding glyphs.
21470 Important is that pixel_width, ascent, and descent are the
21471 values of what is drawn by draw_glyphs (i.e. the values of
21472 the overall glyphs composed). */
21473 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21474 int boff; /* baseline offset */
21475 struct composition *cmp = composition_table[it->cmp_it.id];
21476 int glyph_len = cmp->glyph_len;
21477 struct font *font = face->font;
21479 it->nglyphs = 1;
21481 /* If we have not yet calculated pixel size data of glyphs of
21482 the composition for the current face font, calculate them
21483 now. Theoretically, we have to check all fonts for the
21484 glyphs, but that requires much time and memory space. So,
21485 here we check only the font of the first glyph. This leads
21486 to incorrect display, but it's very rare, and C-l (recenter)
21487 can correct the display anyway. */
21488 if (! cmp->font || cmp->font != font)
21490 /* Ascent and descent of the font of the first character
21491 of this composition (adjusted by baseline offset).
21492 Ascent and descent of overall glyphs should not be less
21493 than them respectively. */
21494 int font_ascent, font_descent, font_height;
21495 /* Bounding box of the overall glyphs. */
21496 int leftmost, rightmost, lowest, highest;
21497 int lbearing, rbearing;
21498 int i, width, ascent, descent;
21499 int left_padded = 0, right_padded = 0;
21500 int c;
21501 XChar2b char2b;
21502 struct font_metrics *pcm;
21503 int font_not_found_p;
21504 int pos;
21506 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21507 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21508 break;
21509 if (glyph_len < cmp->glyph_len)
21510 right_padded = 1;
21511 for (i = 0; i < glyph_len; i++)
21513 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21514 break;
21515 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21517 if (i > 0)
21518 left_padded = 1;
21520 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21521 : IT_CHARPOS (*it));
21522 /* When no suitable font found, use the default font. */
21523 font_not_found_p = font == NULL;
21524 if (font_not_found_p)
21526 face = face->ascii_face;
21527 font = face->font;
21529 boff = font->baseline_offset;
21530 if (font->vertical_centering)
21531 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21532 font_ascent = FONT_BASE (font) + boff;
21533 font_descent = FONT_DESCENT (font) - boff;
21534 font_height = FONT_HEIGHT (font);
21536 cmp->font = (void *) font;
21538 pcm = NULL;
21539 if (! font_not_found_p)
21541 get_char_face_and_encoding (it->f, c, it->face_id,
21542 &char2b, it->multibyte_p, 0);
21543 pcm = get_per_char_metric (it->f, font, &char2b);
21546 /* Initialize the bounding box. */
21547 if (pcm)
21549 width = pcm->width;
21550 ascent = pcm->ascent;
21551 descent = pcm->descent;
21552 lbearing = pcm->lbearing;
21553 rbearing = pcm->rbearing;
21555 else
21557 width = FONT_WIDTH (font);
21558 ascent = FONT_BASE (font);
21559 descent = FONT_DESCENT (font);
21560 lbearing = 0;
21561 rbearing = width;
21564 rightmost = width;
21565 leftmost = 0;
21566 lowest = - descent + boff;
21567 highest = ascent + boff;
21569 if (! font_not_found_p
21570 && font->default_ascent
21571 && CHAR_TABLE_P (Vuse_default_ascent)
21572 && !NILP (Faref (Vuse_default_ascent,
21573 make_number (it->char_to_display))))
21574 highest = font->default_ascent + boff;
21576 /* Draw the first glyph at the normal position. It may be
21577 shifted to right later if some other glyphs are drawn
21578 at the left. */
21579 cmp->offsets[i * 2] = 0;
21580 cmp->offsets[i * 2 + 1] = boff;
21581 cmp->lbearing = lbearing;
21582 cmp->rbearing = rbearing;
21584 /* Set cmp->offsets for the remaining glyphs. */
21585 for (i++; i < glyph_len; i++)
21587 int left, right, btm, top;
21588 int ch = COMPOSITION_GLYPH (cmp, i);
21589 int face_id;
21590 struct face *this_face;
21591 int this_boff;
21593 if (ch == '\t')
21594 ch = ' ';
21595 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21596 this_face = FACE_FROM_ID (it->f, face_id);
21597 font = this_face->font;
21599 if (font == NULL)
21600 pcm = NULL;
21601 else
21603 this_boff = font->baseline_offset;
21604 if (font->vertical_centering)
21605 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21606 get_char_face_and_encoding (it->f, ch, face_id,
21607 &char2b, it->multibyte_p, 0);
21608 pcm = get_per_char_metric (it->f, font, &char2b);
21610 if (! pcm)
21611 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21612 else
21614 width = pcm->width;
21615 ascent = pcm->ascent;
21616 descent = pcm->descent;
21617 lbearing = pcm->lbearing;
21618 rbearing = pcm->rbearing;
21619 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21621 /* Relative composition with or without
21622 alternate chars. */
21623 left = (leftmost + rightmost - width) / 2;
21624 btm = - descent + boff;
21625 if (font->relative_compose
21626 && (! CHAR_TABLE_P (Vignore_relative_composition)
21627 || NILP (Faref (Vignore_relative_composition,
21628 make_number (ch)))))
21631 if (- descent >= font->relative_compose)
21632 /* One extra pixel between two glyphs. */
21633 btm = highest + 1;
21634 else if (ascent <= 0)
21635 /* One extra pixel between two glyphs. */
21636 btm = lowest - 1 - ascent - descent;
21639 else
21641 /* A composition rule is specified by an integer
21642 value that encodes global and new reference
21643 points (GREF and NREF). GREF and NREF are
21644 specified by numbers as below:
21646 0---1---2 -- ascent
21650 9--10--11 -- center
21652 ---3---4---5--- baseline
21654 6---7---8 -- descent
21656 int rule = COMPOSITION_RULE (cmp, i);
21657 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21659 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21660 grefx = gref % 3, nrefx = nref % 3;
21661 grefy = gref / 3, nrefy = nref / 3;
21662 if (xoff)
21663 xoff = font_height * (xoff - 128) / 256;
21664 if (yoff)
21665 yoff = font_height * (yoff - 128) / 256;
21667 left = (leftmost
21668 + grefx * (rightmost - leftmost) / 2
21669 - nrefx * width / 2
21670 + xoff);
21672 btm = ((grefy == 0 ? highest
21673 : grefy == 1 ? 0
21674 : grefy == 2 ? lowest
21675 : (highest + lowest) / 2)
21676 - (nrefy == 0 ? ascent + descent
21677 : nrefy == 1 ? descent - boff
21678 : nrefy == 2 ? 0
21679 : (ascent + descent) / 2)
21680 + yoff);
21683 cmp->offsets[i * 2] = left;
21684 cmp->offsets[i * 2 + 1] = btm + descent;
21686 /* Update the bounding box of the overall glyphs. */
21687 if (width > 0)
21689 right = left + width;
21690 if (left < leftmost)
21691 leftmost = left;
21692 if (right > rightmost)
21693 rightmost = right;
21695 top = btm + descent + ascent;
21696 if (top > highest)
21697 highest = top;
21698 if (btm < lowest)
21699 lowest = btm;
21701 if (cmp->lbearing > left + lbearing)
21702 cmp->lbearing = left + lbearing;
21703 if (cmp->rbearing < left + rbearing)
21704 cmp->rbearing = left + rbearing;
21708 /* If there are glyphs whose x-offsets are negative,
21709 shift all glyphs to the right and make all x-offsets
21710 non-negative. */
21711 if (leftmost < 0)
21713 for (i = 0; i < cmp->glyph_len; i++)
21714 cmp->offsets[i * 2] -= leftmost;
21715 rightmost -= leftmost;
21716 cmp->lbearing -= leftmost;
21717 cmp->rbearing -= leftmost;
21720 if (left_padded && cmp->lbearing < 0)
21722 for (i = 0; i < cmp->glyph_len; i++)
21723 cmp->offsets[i * 2] -= cmp->lbearing;
21724 rightmost -= cmp->lbearing;
21725 cmp->rbearing -= cmp->lbearing;
21726 cmp->lbearing = 0;
21728 if (right_padded && rightmost < cmp->rbearing)
21730 rightmost = cmp->rbearing;
21733 cmp->pixel_width = rightmost;
21734 cmp->ascent = highest;
21735 cmp->descent = - lowest;
21736 if (cmp->ascent < font_ascent)
21737 cmp->ascent = font_ascent;
21738 if (cmp->descent < font_descent)
21739 cmp->descent = font_descent;
21742 if (it->glyph_row
21743 && (cmp->lbearing < 0
21744 || cmp->rbearing > cmp->pixel_width))
21745 it->glyph_row->contains_overlapping_glyphs_p = 1;
21747 it->pixel_width = cmp->pixel_width;
21748 it->ascent = it->phys_ascent = cmp->ascent;
21749 it->descent = it->phys_descent = cmp->descent;
21750 if (face->box != FACE_NO_BOX)
21752 int thick = face->box_line_width;
21754 if (thick > 0)
21756 it->ascent += thick;
21757 it->descent += thick;
21759 else
21760 thick = - thick;
21762 if (it->start_of_box_run_p)
21763 it->pixel_width += thick;
21764 if (it->end_of_box_run_p)
21765 it->pixel_width += thick;
21768 /* If face has an overline, add the height of the overline
21769 (1 pixel) and a 1 pixel margin to the character height. */
21770 if (face->overline_p)
21771 it->ascent += overline_margin;
21773 take_vertical_position_into_account (it);
21774 if (it->ascent < 0)
21775 it->ascent = 0;
21776 if (it->descent < 0)
21777 it->descent = 0;
21779 if (it->glyph_row)
21780 append_composite_glyph (it);
21782 else if (it->what == IT_COMPOSITION)
21784 /* A dynamic (automatic) composition. */
21785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21786 Lisp_Object gstring;
21787 struct font_metrics metrics;
21789 gstring = composition_gstring_from_id (it->cmp_it.id);
21790 it->pixel_width
21791 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21792 &metrics);
21793 if (it->glyph_row
21794 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21795 it->glyph_row->contains_overlapping_glyphs_p = 1;
21796 it->ascent = it->phys_ascent = metrics.ascent;
21797 it->descent = it->phys_descent = metrics.descent;
21798 if (face->box != FACE_NO_BOX)
21800 int thick = face->box_line_width;
21802 if (thick > 0)
21804 it->ascent += thick;
21805 it->descent += thick;
21807 else
21808 thick = - thick;
21810 if (it->start_of_box_run_p)
21811 it->pixel_width += thick;
21812 if (it->end_of_box_run_p)
21813 it->pixel_width += thick;
21815 /* If face has an overline, add the height of the overline
21816 (1 pixel) and a 1 pixel margin to the character height. */
21817 if (face->overline_p)
21818 it->ascent += overline_margin;
21819 take_vertical_position_into_account (it);
21820 if (it->ascent < 0)
21821 it->ascent = 0;
21822 if (it->descent < 0)
21823 it->descent = 0;
21825 if (it->glyph_row)
21826 append_composite_glyph (it);
21828 else if (it->what == IT_IMAGE)
21829 produce_image_glyph (it);
21830 else if (it->what == IT_STRETCH)
21831 produce_stretch_glyph (it);
21833 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21834 because this isn't true for images with `:ascent 100'. */
21835 xassert (it->ascent >= 0 && it->descent >= 0);
21836 if (it->area == TEXT_AREA)
21837 it->current_x += it->pixel_width;
21839 if (extra_line_spacing > 0)
21841 it->descent += extra_line_spacing;
21842 if (extra_line_spacing > it->max_extra_line_spacing)
21843 it->max_extra_line_spacing = extra_line_spacing;
21846 it->max_ascent = max (it->max_ascent, it->ascent);
21847 it->max_descent = max (it->max_descent, it->descent);
21848 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21849 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21852 /* EXPORT for RIF:
21853 Output LEN glyphs starting at START at the nominal cursor position.
21854 Advance the nominal cursor over the text. The global variable
21855 updated_window contains the window being updated, updated_row is
21856 the glyph row being updated, and updated_area is the area of that
21857 row being updated. */
21859 void
21860 x_write_glyphs (start, len)
21861 struct glyph *start;
21862 int len;
21864 int x, hpos;
21866 xassert (updated_window && updated_row);
21867 BLOCK_INPUT;
21869 /* Write glyphs. */
21871 hpos = start - updated_row->glyphs[updated_area];
21872 x = draw_glyphs (updated_window, output_cursor.x,
21873 updated_row, updated_area,
21874 hpos, hpos + len,
21875 DRAW_NORMAL_TEXT, 0);
21877 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21878 if (updated_area == TEXT_AREA
21879 && updated_window->phys_cursor_on_p
21880 && updated_window->phys_cursor.vpos == output_cursor.vpos
21881 && updated_window->phys_cursor.hpos >= hpos
21882 && updated_window->phys_cursor.hpos < hpos + len)
21883 updated_window->phys_cursor_on_p = 0;
21885 UNBLOCK_INPUT;
21887 /* Advance the output cursor. */
21888 output_cursor.hpos += len;
21889 output_cursor.x = x;
21893 /* EXPORT for RIF:
21894 Insert LEN glyphs from START at the nominal cursor position. */
21896 void
21897 x_insert_glyphs (start, len)
21898 struct glyph *start;
21899 int len;
21901 struct frame *f;
21902 struct window *w;
21903 int line_height, shift_by_width, shifted_region_width;
21904 struct glyph_row *row;
21905 struct glyph *glyph;
21906 int frame_x, frame_y;
21907 EMACS_INT hpos;
21909 xassert (updated_window && updated_row);
21910 BLOCK_INPUT;
21911 w = updated_window;
21912 f = XFRAME (WINDOW_FRAME (w));
21914 /* Get the height of the line we are in. */
21915 row = updated_row;
21916 line_height = row->height;
21918 /* Get the width of the glyphs to insert. */
21919 shift_by_width = 0;
21920 for (glyph = start; glyph < start + len; ++glyph)
21921 shift_by_width += glyph->pixel_width;
21923 /* Get the width of the region to shift right. */
21924 shifted_region_width = (window_box_width (w, updated_area)
21925 - output_cursor.x
21926 - shift_by_width);
21928 /* Shift right. */
21929 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21930 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21932 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21933 line_height, shift_by_width);
21935 /* Write the glyphs. */
21936 hpos = start - row->glyphs[updated_area];
21937 draw_glyphs (w, output_cursor.x, row, updated_area,
21938 hpos, hpos + len,
21939 DRAW_NORMAL_TEXT, 0);
21941 /* Advance the output cursor. */
21942 output_cursor.hpos += len;
21943 output_cursor.x += shift_by_width;
21944 UNBLOCK_INPUT;
21948 /* EXPORT for RIF:
21949 Erase the current text line from the nominal cursor position
21950 (inclusive) to pixel column TO_X (exclusive). The idea is that
21951 everything from TO_X onward is already erased.
21953 TO_X is a pixel position relative to updated_area of
21954 updated_window. TO_X == -1 means clear to the end of this area. */
21956 void
21957 x_clear_end_of_line (to_x)
21958 int to_x;
21960 struct frame *f;
21961 struct window *w = updated_window;
21962 int max_x, min_y, max_y;
21963 int from_x, from_y, to_y;
21965 xassert (updated_window && updated_row);
21966 f = XFRAME (w->frame);
21968 if (updated_row->full_width_p)
21969 max_x = WINDOW_TOTAL_WIDTH (w);
21970 else
21971 max_x = window_box_width (w, updated_area);
21972 max_y = window_text_bottom_y (w);
21974 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21975 of window. For TO_X > 0, truncate to end of drawing area. */
21976 if (to_x == 0)
21977 return;
21978 else if (to_x < 0)
21979 to_x = max_x;
21980 else
21981 to_x = min (to_x, max_x);
21983 to_y = min (max_y, output_cursor.y + updated_row->height);
21985 /* Notice if the cursor will be cleared by this operation. */
21986 if (!updated_row->full_width_p)
21987 notice_overwritten_cursor (w, updated_area,
21988 output_cursor.x, -1,
21989 updated_row->y,
21990 MATRIX_ROW_BOTTOM_Y (updated_row));
21992 from_x = output_cursor.x;
21994 /* Translate to frame coordinates. */
21995 if (updated_row->full_width_p)
21997 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21998 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22000 else
22002 int area_left = window_box_left (w, updated_area);
22003 from_x += area_left;
22004 to_x += area_left;
22007 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22008 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22009 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22011 /* Prevent inadvertently clearing to end of the X window. */
22012 if (to_x > from_x && to_y > from_y)
22014 BLOCK_INPUT;
22015 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22016 to_x - from_x, to_y - from_y);
22017 UNBLOCK_INPUT;
22021 #endif /* HAVE_WINDOW_SYSTEM */
22025 /***********************************************************************
22026 Cursor types
22027 ***********************************************************************/
22029 /* Value is the internal representation of the specified cursor type
22030 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22031 of the bar cursor. */
22033 static enum text_cursor_kinds
22034 get_specified_cursor_type (arg, width)
22035 Lisp_Object arg;
22036 int *width;
22038 enum text_cursor_kinds type;
22040 if (NILP (arg))
22041 return NO_CURSOR;
22043 if (EQ (arg, Qbox))
22044 return FILLED_BOX_CURSOR;
22046 if (EQ (arg, Qhollow))
22047 return HOLLOW_BOX_CURSOR;
22049 if (EQ (arg, Qbar))
22051 *width = 2;
22052 return BAR_CURSOR;
22055 if (CONSP (arg)
22056 && EQ (XCAR (arg), Qbar)
22057 && INTEGERP (XCDR (arg))
22058 && XINT (XCDR (arg)) >= 0)
22060 *width = XINT (XCDR (arg));
22061 return BAR_CURSOR;
22064 if (EQ (arg, Qhbar))
22066 *width = 2;
22067 return HBAR_CURSOR;
22070 if (CONSP (arg)
22071 && EQ (XCAR (arg), Qhbar)
22072 && INTEGERP (XCDR (arg))
22073 && XINT (XCDR (arg)) >= 0)
22075 *width = XINT (XCDR (arg));
22076 return HBAR_CURSOR;
22079 /* Treat anything unknown as "hollow box cursor".
22080 It was bad to signal an error; people have trouble fixing
22081 .Xdefaults with Emacs, when it has something bad in it. */
22082 type = HOLLOW_BOX_CURSOR;
22084 return type;
22087 /* Set the default cursor types for specified frame. */
22088 void
22089 set_frame_cursor_types (f, arg)
22090 struct frame *f;
22091 Lisp_Object arg;
22093 int width;
22094 Lisp_Object tem;
22096 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22097 FRAME_CURSOR_WIDTH (f) = width;
22099 /* By default, set up the blink-off state depending on the on-state. */
22101 tem = Fassoc (arg, Vblink_cursor_alist);
22102 if (!NILP (tem))
22104 FRAME_BLINK_OFF_CURSOR (f)
22105 = get_specified_cursor_type (XCDR (tem), &width);
22106 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22108 else
22109 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22113 /* Return the cursor we want to be displayed in window W. Return
22114 width of bar/hbar cursor through WIDTH arg. Return with
22115 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22116 (i.e. if the `system caret' should track this cursor).
22118 In a mini-buffer window, we want the cursor only to appear if we
22119 are reading input from this window. For the selected window, we
22120 want the cursor type given by the frame parameter or buffer local
22121 setting of cursor-type. If explicitly marked off, draw no cursor.
22122 In all other cases, we want a hollow box cursor. */
22124 static enum text_cursor_kinds
22125 get_window_cursor_type (w, glyph, width, active_cursor)
22126 struct window *w;
22127 struct glyph *glyph;
22128 int *width;
22129 int *active_cursor;
22131 struct frame *f = XFRAME (w->frame);
22132 struct buffer *b = XBUFFER (w->buffer);
22133 int cursor_type = DEFAULT_CURSOR;
22134 Lisp_Object alt_cursor;
22135 int non_selected = 0;
22137 *active_cursor = 1;
22139 /* Echo area */
22140 if (cursor_in_echo_area
22141 && FRAME_HAS_MINIBUF_P (f)
22142 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22144 if (w == XWINDOW (echo_area_window))
22146 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22148 *width = FRAME_CURSOR_WIDTH (f);
22149 return FRAME_DESIRED_CURSOR (f);
22151 else
22152 return get_specified_cursor_type (b->cursor_type, width);
22155 *active_cursor = 0;
22156 non_selected = 1;
22159 /* Detect a nonselected window or nonselected frame. */
22160 else if (w != XWINDOW (f->selected_window)
22161 #ifdef HAVE_WINDOW_SYSTEM
22162 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22163 #endif
22166 *active_cursor = 0;
22168 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22169 return NO_CURSOR;
22171 non_selected = 1;
22174 /* Never display a cursor in a window in which cursor-type is nil. */
22175 if (NILP (b->cursor_type))
22176 return NO_CURSOR;
22178 /* Get the normal cursor type for this window. */
22179 if (EQ (b->cursor_type, Qt))
22181 cursor_type = FRAME_DESIRED_CURSOR (f);
22182 *width = FRAME_CURSOR_WIDTH (f);
22184 else
22185 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22187 /* Use cursor-in-non-selected-windows instead
22188 for non-selected window or frame. */
22189 if (non_selected)
22191 alt_cursor = b->cursor_in_non_selected_windows;
22192 if (!EQ (Qt, alt_cursor))
22193 return get_specified_cursor_type (alt_cursor, width);
22194 /* t means modify the normal cursor type. */
22195 if (cursor_type == FILLED_BOX_CURSOR)
22196 cursor_type = HOLLOW_BOX_CURSOR;
22197 else if (cursor_type == BAR_CURSOR && *width > 1)
22198 --*width;
22199 return cursor_type;
22202 /* Use normal cursor if not blinked off. */
22203 if (!w->cursor_off_p)
22205 #ifdef HAVE_WINDOW_SYSTEM
22206 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22208 if (cursor_type == FILLED_BOX_CURSOR)
22210 /* Using a block cursor on large images can be very annoying.
22211 So use a hollow cursor for "large" images.
22212 If image is not transparent (no mask), also use hollow cursor. */
22213 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22214 if (img != NULL && IMAGEP (img->spec))
22216 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22217 where N = size of default frame font size.
22218 This should cover most of the "tiny" icons people may use. */
22219 if (!img->mask
22220 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22221 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22222 cursor_type = HOLLOW_BOX_CURSOR;
22225 else if (cursor_type != NO_CURSOR)
22227 /* Display current only supports BOX and HOLLOW cursors for images.
22228 So for now, unconditionally use a HOLLOW cursor when cursor is
22229 not a solid box cursor. */
22230 cursor_type = HOLLOW_BOX_CURSOR;
22233 #endif
22234 return cursor_type;
22237 /* Cursor is blinked off, so determine how to "toggle" it. */
22239 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22240 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22241 return get_specified_cursor_type (XCDR (alt_cursor), width);
22243 /* Then see if frame has specified a specific blink off cursor type. */
22244 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22246 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22247 return FRAME_BLINK_OFF_CURSOR (f);
22250 #if 0
22251 /* Some people liked having a permanently visible blinking cursor,
22252 while others had very strong opinions against it. So it was
22253 decided to remove it. KFS 2003-09-03 */
22255 /* Finally perform built-in cursor blinking:
22256 filled box <-> hollow box
22257 wide [h]bar <-> narrow [h]bar
22258 narrow [h]bar <-> no cursor
22259 other type <-> no cursor */
22261 if (cursor_type == FILLED_BOX_CURSOR)
22262 return HOLLOW_BOX_CURSOR;
22264 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22266 *width = 1;
22267 return cursor_type;
22269 #endif
22271 return NO_CURSOR;
22275 #ifdef HAVE_WINDOW_SYSTEM
22277 /* Notice when the text cursor of window W has been completely
22278 overwritten by a drawing operation that outputs glyphs in AREA
22279 starting at X0 and ending at X1 in the line starting at Y0 and
22280 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22281 the rest of the line after X0 has been written. Y coordinates
22282 are window-relative. */
22284 static void
22285 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22286 struct window *w;
22287 enum glyph_row_area area;
22288 int x0, y0, x1, y1;
22290 int cx0, cx1, cy0, cy1;
22291 struct glyph_row *row;
22293 if (!w->phys_cursor_on_p)
22294 return;
22295 if (area != TEXT_AREA)
22296 return;
22298 if (w->phys_cursor.vpos < 0
22299 || w->phys_cursor.vpos >= w->current_matrix->nrows
22300 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22301 !(row->enabled_p && row->displays_text_p)))
22302 return;
22304 if (row->cursor_in_fringe_p)
22306 row->cursor_in_fringe_p = 0;
22307 draw_fringe_bitmap (w, row, 0);
22308 w->phys_cursor_on_p = 0;
22309 return;
22312 cx0 = w->phys_cursor.x;
22313 cx1 = cx0 + w->phys_cursor_width;
22314 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22315 return;
22317 /* The cursor image will be completely removed from the
22318 screen if the output area intersects the cursor area in
22319 y-direction. When we draw in [y0 y1[, and some part of
22320 the cursor is at y < y0, that part must have been drawn
22321 before. When scrolling, the cursor is erased before
22322 actually scrolling, so we don't come here. When not
22323 scrolling, the rows above the old cursor row must have
22324 changed, and in this case these rows must have written
22325 over the cursor image.
22327 Likewise if part of the cursor is below y1, with the
22328 exception of the cursor being in the first blank row at
22329 the buffer and window end because update_text_area
22330 doesn't draw that row. (Except when it does, but
22331 that's handled in update_text_area.) */
22333 cy0 = w->phys_cursor.y;
22334 cy1 = cy0 + w->phys_cursor_height;
22335 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22336 return;
22338 w->phys_cursor_on_p = 0;
22341 #endif /* HAVE_WINDOW_SYSTEM */
22344 /************************************************************************
22345 Mouse Face
22346 ************************************************************************/
22348 #ifdef HAVE_WINDOW_SYSTEM
22350 /* EXPORT for RIF:
22351 Fix the display of area AREA of overlapping row ROW in window W
22352 with respect to the overlapping part OVERLAPS. */
22354 void
22355 x_fix_overlapping_area (w, row, area, overlaps)
22356 struct window *w;
22357 struct glyph_row *row;
22358 enum glyph_row_area area;
22359 int overlaps;
22361 int i, x;
22363 BLOCK_INPUT;
22365 x = 0;
22366 for (i = 0; i < row->used[area];)
22368 if (row->glyphs[area][i].overlaps_vertically_p)
22370 int start = i, start_x = x;
22374 x += row->glyphs[area][i].pixel_width;
22375 ++i;
22377 while (i < row->used[area]
22378 && row->glyphs[area][i].overlaps_vertically_p);
22380 draw_glyphs (w, start_x, row, area,
22381 start, i,
22382 DRAW_NORMAL_TEXT, overlaps);
22384 else
22386 x += row->glyphs[area][i].pixel_width;
22387 ++i;
22391 UNBLOCK_INPUT;
22395 /* EXPORT:
22396 Draw the cursor glyph of window W in glyph row ROW. See the
22397 comment of draw_glyphs for the meaning of HL. */
22399 void
22400 draw_phys_cursor_glyph (w, row, hl)
22401 struct window *w;
22402 struct glyph_row *row;
22403 enum draw_glyphs_face hl;
22405 /* If cursor hpos is out of bounds, don't draw garbage. This can
22406 happen in mini-buffer windows when switching between echo area
22407 glyphs and mini-buffer. */
22408 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22410 int on_p = w->phys_cursor_on_p;
22411 int x1;
22412 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22413 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22414 hl, 0);
22415 w->phys_cursor_on_p = on_p;
22417 if (hl == DRAW_CURSOR)
22418 w->phys_cursor_width = x1 - w->phys_cursor.x;
22419 /* When we erase the cursor, and ROW is overlapped by other
22420 rows, make sure that these overlapping parts of other rows
22421 are redrawn. */
22422 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22424 w->phys_cursor_width = x1 - w->phys_cursor.x;
22426 if (row > w->current_matrix->rows
22427 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22428 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22429 OVERLAPS_ERASED_CURSOR);
22431 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22432 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22433 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22434 OVERLAPS_ERASED_CURSOR);
22440 /* EXPORT:
22441 Erase the image of a cursor of window W from the screen. */
22443 void
22444 erase_phys_cursor (w)
22445 struct window *w;
22447 struct frame *f = XFRAME (w->frame);
22448 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22449 int hpos = w->phys_cursor.hpos;
22450 int vpos = w->phys_cursor.vpos;
22451 int mouse_face_here_p = 0;
22452 struct glyph_matrix *active_glyphs = w->current_matrix;
22453 struct glyph_row *cursor_row;
22454 struct glyph *cursor_glyph;
22455 enum draw_glyphs_face hl;
22457 /* No cursor displayed or row invalidated => nothing to do on the
22458 screen. */
22459 if (w->phys_cursor_type == NO_CURSOR)
22460 goto mark_cursor_off;
22462 /* VPOS >= active_glyphs->nrows means that window has been resized.
22463 Don't bother to erase the cursor. */
22464 if (vpos >= active_glyphs->nrows)
22465 goto mark_cursor_off;
22467 /* If row containing cursor is marked invalid, there is nothing we
22468 can do. */
22469 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22470 if (!cursor_row->enabled_p)
22471 goto mark_cursor_off;
22473 /* If line spacing is > 0, old cursor may only be partially visible in
22474 window after split-window. So adjust visible height. */
22475 cursor_row->visible_height = min (cursor_row->visible_height,
22476 window_text_bottom_y (w) - cursor_row->y);
22478 /* If row is completely invisible, don't attempt to delete a cursor which
22479 isn't there. This can happen if cursor is at top of a window, and
22480 we switch to a buffer with a header line in that window. */
22481 if (cursor_row->visible_height <= 0)
22482 goto mark_cursor_off;
22484 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22485 if (cursor_row->cursor_in_fringe_p)
22487 cursor_row->cursor_in_fringe_p = 0;
22488 draw_fringe_bitmap (w, cursor_row, 0);
22489 goto mark_cursor_off;
22492 /* This can happen when the new row is shorter than the old one.
22493 In this case, either draw_glyphs or clear_end_of_line
22494 should have cleared the cursor. Note that we wouldn't be
22495 able to erase the cursor in this case because we don't have a
22496 cursor glyph at hand. */
22497 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22498 goto mark_cursor_off;
22500 /* If the cursor is in the mouse face area, redisplay that when
22501 we clear the cursor. */
22502 if (! NILP (dpyinfo->mouse_face_window)
22503 && w == XWINDOW (dpyinfo->mouse_face_window)
22504 && (vpos > dpyinfo->mouse_face_beg_row
22505 || (vpos == dpyinfo->mouse_face_beg_row
22506 && hpos >= dpyinfo->mouse_face_beg_col))
22507 && (vpos < dpyinfo->mouse_face_end_row
22508 || (vpos == dpyinfo->mouse_face_end_row
22509 && hpos < dpyinfo->mouse_face_end_col))
22510 /* Don't redraw the cursor's spot in mouse face if it is at the
22511 end of a line (on a newline). The cursor appears there, but
22512 mouse highlighting does not. */
22513 && cursor_row->used[TEXT_AREA] > hpos)
22514 mouse_face_here_p = 1;
22516 /* Maybe clear the display under the cursor. */
22517 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22519 int x, y, left_x;
22520 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22521 int width;
22523 cursor_glyph = get_phys_cursor_glyph (w);
22524 if (cursor_glyph == NULL)
22525 goto mark_cursor_off;
22527 width = cursor_glyph->pixel_width;
22528 left_x = window_box_left_offset (w, TEXT_AREA);
22529 x = w->phys_cursor.x;
22530 if (x < left_x)
22531 width -= left_x - x;
22532 width = min (width, window_box_width (w, TEXT_AREA) - x);
22533 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22534 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22536 if (width > 0)
22537 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22540 /* Erase the cursor by redrawing the character underneath it. */
22541 if (mouse_face_here_p)
22542 hl = DRAW_MOUSE_FACE;
22543 else
22544 hl = DRAW_NORMAL_TEXT;
22545 draw_phys_cursor_glyph (w, cursor_row, hl);
22547 mark_cursor_off:
22548 w->phys_cursor_on_p = 0;
22549 w->phys_cursor_type = NO_CURSOR;
22553 /* EXPORT:
22554 Display or clear cursor of window W. If ON is zero, clear the
22555 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22556 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22558 void
22559 display_and_set_cursor (w, on, hpos, vpos, x, y)
22560 struct window *w;
22561 int on, hpos, vpos, x, y;
22563 struct frame *f = XFRAME (w->frame);
22564 int new_cursor_type;
22565 int new_cursor_width;
22566 int active_cursor;
22567 struct glyph_row *glyph_row;
22568 struct glyph *glyph;
22570 /* This is pointless on invisible frames, and dangerous on garbaged
22571 windows and frames; in the latter case, the frame or window may
22572 be in the midst of changing its size, and x and y may be off the
22573 window. */
22574 if (! FRAME_VISIBLE_P (f)
22575 || FRAME_GARBAGED_P (f)
22576 || vpos >= w->current_matrix->nrows
22577 || hpos >= w->current_matrix->matrix_w)
22578 return;
22580 /* If cursor is off and we want it off, return quickly. */
22581 if (!on && !w->phys_cursor_on_p)
22582 return;
22584 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22585 /* If cursor row is not enabled, we don't really know where to
22586 display the cursor. */
22587 if (!glyph_row->enabled_p)
22589 w->phys_cursor_on_p = 0;
22590 return;
22593 glyph = NULL;
22594 if (!glyph_row->exact_window_width_line_p
22595 || hpos < glyph_row->used[TEXT_AREA])
22596 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22598 xassert (interrupt_input_blocked);
22600 /* Set new_cursor_type to the cursor we want to be displayed. */
22601 new_cursor_type = get_window_cursor_type (w, glyph,
22602 &new_cursor_width, &active_cursor);
22604 /* If cursor is currently being shown and we don't want it to be or
22605 it is in the wrong place, or the cursor type is not what we want,
22606 erase it. */
22607 if (w->phys_cursor_on_p
22608 && (!on
22609 || w->phys_cursor.x != x
22610 || w->phys_cursor.y != y
22611 || new_cursor_type != w->phys_cursor_type
22612 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22613 && new_cursor_width != w->phys_cursor_width)))
22614 erase_phys_cursor (w);
22616 /* Don't check phys_cursor_on_p here because that flag is only set
22617 to zero in some cases where we know that the cursor has been
22618 completely erased, to avoid the extra work of erasing the cursor
22619 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22620 still not be visible, or it has only been partly erased. */
22621 if (on)
22623 w->phys_cursor_ascent = glyph_row->ascent;
22624 w->phys_cursor_height = glyph_row->height;
22626 /* Set phys_cursor_.* before x_draw_.* is called because some
22627 of them may need the information. */
22628 w->phys_cursor.x = x;
22629 w->phys_cursor.y = glyph_row->y;
22630 w->phys_cursor.hpos = hpos;
22631 w->phys_cursor.vpos = vpos;
22634 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22635 new_cursor_type, new_cursor_width,
22636 on, active_cursor);
22640 /* Switch the display of W's cursor on or off, according to the value
22641 of ON. */
22643 #ifndef HAVE_NS
22644 static
22645 #endif
22646 void
22647 update_window_cursor (w, on)
22648 struct window *w;
22649 int on;
22651 /* Don't update cursor in windows whose frame is in the process
22652 of being deleted. */
22653 if (w->current_matrix)
22655 BLOCK_INPUT;
22656 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22657 w->phys_cursor.x, w->phys_cursor.y);
22658 UNBLOCK_INPUT;
22663 /* Call update_window_cursor with parameter ON_P on all leaf windows
22664 in the window tree rooted at W. */
22666 static void
22667 update_cursor_in_window_tree (w, on_p)
22668 struct window *w;
22669 int on_p;
22671 while (w)
22673 if (!NILP (w->hchild))
22674 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22675 else if (!NILP (w->vchild))
22676 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22677 else
22678 update_window_cursor (w, on_p);
22680 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22685 /* EXPORT:
22686 Display the cursor on window W, or clear it, according to ON_P.
22687 Don't change the cursor's position. */
22689 void
22690 x_update_cursor (f, on_p)
22691 struct frame *f;
22692 int on_p;
22694 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22698 /* EXPORT:
22699 Clear the cursor of window W to background color, and mark the
22700 cursor as not shown. This is used when the text where the cursor
22701 is is about to be rewritten. */
22703 void
22704 x_clear_cursor (w)
22705 struct window *w;
22707 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22708 update_window_cursor (w, 0);
22712 /* EXPORT:
22713 Display the active region described by mouse_face_* according to DRAW. */
22715 void
22716 show_mouse_face (dpyinfo, draw)
22717 Display_Info *dpyinfo;
22718 enum draw_glyphs_face draw;
22720 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22721 struct frame *f = XFRAME (WINDOW_FRAME (w));
22723 if (/* If window is in the process of being destroyed, don't bother
22724 to do anything. */
22725 w->current_matrix != NULL
22726 /* Don't update mouse highlight if hidden */
22727 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22728 /* Recognize when we are called to operate on rows that don't exist
22729 anymore. This can happen when a window is split. */
22730 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22732 int phys_cursor_on_p = w->phys_cursor_on_p;
22733 struct glyph_row *row, *first, *last;
22735 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22736 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22738 for (row = first; row <= last && row->enabled_p; ++row)
22740 int start_hpos, end_hpos, start_x;
22742 /* For all but the first row, the highlight starts at column 0. */
22743 if (row == first)
22745 start_hpos = dpyinfo->mouse_face_beg_col;
22746 start_x = dpyinfo->mouse_face_beg_x;
22748 else
22750 start_hpos = 0;
22751 start_x = 0;
22754 if (row == last)
22755 end_hpos = dpyinfo->mouse_face_end_col;
22756 else
22758 end_hpos = row->used[TEXT_AREA];
22759 if (draw == DRAW_NORMAL_TEXT)
22760 row->fill_line_p = 1; /* Clear to end of line */
22763 if (end_hpos > start_hpos)
22765 draw_glyphs (w, start_x, row, TEXT_AREA,
22766 start_hpos, end_hpos,
22767 draw, 0);
22769 row->mouse_face_p
22770 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22774 /* When we've written over the cursor, arrange for it to
22775 be displayed again. */
22776 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22778 BLOCK_INPUT;
22779 display_and_set_cursor (w, 1,
22780 w->phys_cursor.hpos, w->phys_cursor.vpos,
22781 w->phys_cursor.x, w->phys_cursor.y);
22782 UNBLOCK_INPUT;
22786 /* Change the mouse cursor. */
22787 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22788 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22789 else if (draw == DRAW_MOUSE_FACE)
22790 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22791 else
22792 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22795 /* EXPORT:
22796 Clear out the mouse-highlighted active region.
22797 Redraw it un-highlighted first. Value is non-zero if mouse
22798 face was actually drawn unhighlighted. */
22801 clear_mouse_face (dpyinfo)
22802 Display_Info *dpyinfo;
22804 int cleared = 0;
22806 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22808 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22809 cleared = 1;
22812 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22813 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22814 dpyinfo->mouse_face_window = Qnil;
22815 dpyinfo->mouse_face_overlay = Qnil;
22816 return cleared;
22820 /* EXPORT:
22821 Non-zero if physical cursor of window W is within mouse face. */
22824 cursor_in_mouse_face_p (w)
22825 struct window *w;
22827 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22828 int in_mouse_face = 0;
22830 if (WINDOWP (dpyinfo->mouse_face_window)
22831 && XWINDOW (dpyinfo->mouse_face_window) == w)
22833 int hpos = w->phys_cursor.hpos;
22834 int vpos = w->phys_cursor.vpos;
22836 if (vpos >= dpyinfo->mouse_face_beg_row
22837 && vpos <= dpyinfo->mouse_face_end_row
22838 && (vpos > dpyinfo->mouse_face_beg_row
22839 || hpos >= dpyinfo->mouse_face_beg_col)
22840 && (vpos < dpyinfo->mouse_face_end_row
22841 || hpos < dpyinfo->mouse_face_end_col
22842 || dpyinfo->mouse_face_past_end))
22843 in_mouse_face = 1;
22846 return in_mouse_face;
22852 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22853 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22854 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22855 for the overlay or run of text properties specifying the mouse
22856 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22857 before-string and after-string that must also be highlighted.
22858 DISPLAY_STRING, if non-nil, is a display string that may cover some
22859 or all of the highlighted text. */
22861 static void
22862 mouse_face_from_buffer_pos (Lisp_Object window,
22863 Display_Info *dpyinfo,
22864 EMACS_INT mouse_charpos,
22865 EMACS_INT start_charpos,
22866 EMACS_INT end_charpos,
22867 Lisp_Object before_string,
22868 Lisp_Object after_string,
22869 Lisp_Object display_string)
22871 struct window *w = XWINDOW (window);
22872 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22873 struct glyph_row *row;
22874 struct glyph *glyph, *end;
22875 EMACS_INT ignore;
22876 int x;
22878 xassert (NILP (display_string) || STRINGP (display_string));
22879 xassert (NILP (before_string) || STRINGP (before_string));
22880 xassert (NILP (after_string) || STRINGP (after_string));
22882 /* Find the first highlighted glyph. */
22883 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22885 dpyinfo->mouse_face_beg_col = 0;
22886 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22887 dpyinfo->mouse_face_beg_x = first->x;
22888 dpyinfo->mouse_face_beg_y = first->y;
22890 else
22892 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22893 if (row == NULL)
22894 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22896 /* If the before-string or display-string contains newlines,
22897 row_containing_pos skips to its last row. Move back. */
22898 if (!NILP (before_string) || !NILP (display_string))
22900 struct glyph_row *prev;
22901 while ((prev = row - 1, prev >= first)
22902 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22903 && prev->used[TEXT_AREA] > 0)
22905 struct glyph *beg = prev->glyphs[TEXT_AREA];
22906 glyph = beg + prev->used[TEXT_AREA];
22907 while (--glyph >= beg && INTEGERP (glyph->object));
22908 if (glyph < beg
22909 || !(EQ (glyph->object, before_string)
22910 || EQ (glyph->object, display_string)))
22911 break;
22912 row = prev;
22916 glyph = row->glyphs[TEXT_AREA];
22917 end = glyph + row->used[TEXT_AREA];
22918 x = row->x;
22919 dpyinfo->mouse_face_beg_y = row->y;
22920 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22922 /* Skip truncation glyphs at the start of the glyph row. */
22923 if (row->displays_text_p)
22924 for (; glyph < end
22925 && INTEGERP (glyph->object)
22926 && glyph->charpos < 0;
22927 ++glyph)
22928 x += glyph->pixel_width;
22930 /* Scan the glyph row, stopping before BEFORE_STRING or
22931 DISPLAY_STRING or START_CHARPOS. */
22932 for (; glyph < end
22933 && !INTEGERP (glyph->object)
22934 && !EQ (glyph->object, before_string)
22935 && !EQ (glyph->object, display_string)
22936 && !(BUFFERP (glyph->object)
22937 && glyph->charpos >= start_charpos);
22938 ++glyph)
22939 x += glyph->pixel_width;
22941 dpyinfo->mouse_face_beg_x = x;
22942 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22945 /* Find the last highlighted glyph. */
22946 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22947 if (row == NULL)
22949 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22950 dpyinfo->mouse_face_past_end = 1;
22952 else if (!NILP (after_string))
22954 /* If the after-string has newlines, advance to its last row. */
22955 struct glyph_row *next;
22956 struct glyph_row *last
22957 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22959 for (next = row + 1;
22960 next <= last
22961 && next->used[TEXT_AREA] > 0
22962 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22963 ++next)
22964 row = next;
22967 glyph = row->glyphs[TEXT_AREA];
22968 end = glyph + row->used[TEXT_AREA];
22969 x = row->x;
22970 dpyinfo->mouse_face_end_y = row->y;
22971 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22973 /* Skip truncation glyphs at the start of the row. */
22974 if (row->displays_text_p)
22975 for (; glyph < end
22976 && INTEGERP (glyph->object)
22977 && glyph->charpos < 0;
22978 ++glyph)
22979 x += glyph->pixel_width;
22981 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22982 AFTER_STRING. */
22983 for (; glyph < end
22984 && !INTEGERP (glyph->object)
22985 && !EQ (glyph->object, after_string)
22986 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22987 ++glyph)
22988 x += glyph->pixel_width;
22990 /* If we found AFTER_STRING, consume it and stop. */
22991 if (EQ (glyph->object, after_string))
22993 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22994 x += glyph->pixel_width;
22996 else
22998 /* If there's no after-string, we must check if we overshot,
22999 which might be the case if we stopped after a string glyph.
23000 That glyph may belong to a before-string or display-string
23001 associated with the end position, which must not be
23002 highlighted. */
23003 Lisp_Object prev_object;
23004 int pos;
23006 while (glyph > row->glyphs[TEXT_AREA])
23008 prev_object = (glyph - 1)->object;
23009 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23010 break;
23012 pos = string_buffer_position (w, prev_object, end_charpos);
23013 if (pos && pos < end_charpos)
23014 break;
23016 for (; glyph > row->glyphs[TEXT_AREA]
23017 && EQ ((glyph - 1)->object, prev_object);
23018 --glyph)
23019 x -= (glyph - 1)->pixel_width;
23023 dpyinfo->mouse_face_end_x = x;
23024 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23025 dpyinfo->mouse_face_window = window;
23026 dpyinfo->mouse_face_face_id
23027 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23028 mouse_charpos + 1,
23029 !dpyinfo->mouse_face_hidden, -1);
23030 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23034 /* Find the position of the glyph for position POS in OBJECT in
23035 window W's current matrix, and return in *X, *Y the pixel
23036 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23038 RIGHT_P non-zero means return the position of the right edge of the
23039 glyph, RIGHT_P zero means return the left edge position.
23041 If no glyph for POS exists in the matrix, return the position of
23042 the glyph with the next smaller position that is in the matrix, if
23043 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23044 exists in the matrix, return the position of the glyph with the
23045 next larger position in OBJECT.
23047 Value is non-zero if a glyph was found. */
23049 static int
23050 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23051 struct window *w;
23052 EMACS_INT pos;
23053 Lisp_Object object;
23054 int *hpos, *vpos, *x, *y;
23055 int right_p;
23057 int yb = window_text_bottom_y (w);
23058 struct glyph_row *r;
23059 struct glyph *best_glyph = NULL;
23060 struct glyph_row *best_row = NULL;
23061 int best_x = 0;
23063 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23064 r->enabled_p && r->y < yb;
23065 ++r)
23067 struct glyph *g = r->glyphs[TEXT_AREA];
23068 struct glyph *e = g + r->used[TEXT_AREA];
23069 int gx;
23071 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23072 if (EQ (g->object, object))
23074 if (g->charpos == pos)
23076 best_glyph = g;
23077 best_x = gx;
23078 best_row = r;
23079 goto found;
23081 else if (best_glyph == NULL
23082 || ((eabs (g->charpos - pos)
23083 < eabs (best_glyph->charpos - pos))
23084 && (right_p
23085 ? g->charpos < pos
23086 : g->charpos > pos)))
23088 best_glyph = g;
23089 best_x = gx;
23090 best_row = r;
23095 found:
23097 if (best_glyph)
23099 *x = best_x;
23100 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23102 if (right_p)
23104 *x += best_glyph->pixel_width;
23105 ++*hpos;
23108 *y = best_row->y;
23109 *vpos = best_row - w->current_matrix->rows;
23112 return best_glyph != NULL;
23116 /* See if position X, Y is within a hot-spot of an image. */
23118 static int
23119 on_hot_spot_p (hot_spot, x, y)
23120 Lisp_Object hot_spot;
23121 int x, y;
23123 if (!CONSP (hot_spot))
23124 return 0;
23126 if (EQ (XCAR (hot_spot), Qrect))
23128 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23129 Lisp_Object rect = XCDR (hot_spot);
23130 Lisp_Object tem;
23131 if (!CONSP (rect))
23132 return 0;
23133 if (!CONSP (XCAR (rect)))
23134 return 0;
23135 if (!CONSP (XCDR (rect)))
23136 return 0;
23137 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23138 return 0;
23139 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23140 return 0;
23141 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23142 return 0;
23143 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23144 return 0;
23145 return 1;
23147 else if (EQ (XCAR (hot_spot), Qcircle))
23149 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23150 Lisp_Object circ = XCDR (hot_spot);
23151 Lisp_Object lr, lx0, ly0;
23152 if (CONSP (circ)
23153 && CONSP (XCAR (circ))
23154 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23155 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23156 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23158 double r = XFLOATINT (lr);
23159 double dx = XINT (lx0) - x;
23160 double dy = XINT (ly0) - y;
23161 return (dx * dx + dy * dy <= r * r);
23164 else if (EQ (XCAR (hot_spot), Qpoly))
23166 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23167 if (VECTORP (XCDR (hot_spot)))
23169 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23170 Lisp_Object *poly = v->contents;
23171 int n = v->size;
23172 int i;
23173 int inside = 0;
23174 Lisp_Object lx, ly;
23175 int x0, y0;
23177 /* Need an even number of coordinates, and at least 3 edges. */
23178 if (n < 6 || n & 1)
23179 return 0;
23181 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23182 If count is odd, we are inside polygon. Pixels on edges
23183 may or may not be included depending on actual geometry of the
23184 polygon. */
23185 if ((lx = poly[n-2], !INTEGERP (lx))
23186 || (ly = poly[n-1], !INTEGERP (lx)))
23187 return 0;
23188 x0 = XINT (lx), y0 = XINT (ly);
23189 for (i = 0; i < n; i += 2)
23191 int x1 = x0, y1 = y0;
23192 if ((lx = poly[i], !INTEGERP (lx))
23193 || (ly = poly[i+1], !INTEGERP (ly)))
23194 return 0;
23195 x0 = XINT (lx), y0 = XINT (ly);
23197 /* Does this segment cross the X line? */
23198 if (x0 >= x)
23200 if (x1 >= x)
23201 continue;
23203 else if (x1 < x)
23204 continue;
23205 if (y > y0 && y > y1)
23206 continue;
23207 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23208 inside = !inside;
23210 return inside;
23213 return 0;
23216 Lisp_Object
23217 find_hot_spot (map, x, y)
23218 Lisp_Object map;
23219 int x, y;
23221 while (CONSP (map))
23223 if (CONSP (XCAR (map))
23224 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23225 return XCAR (map);
23226 map = XCDR (map);
23229 return Qnil;
23232 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23233 3, 3, 0,
23234 doc: /* Lookup in image map MAP coordinates X and Y.
23235 An image map is an alist where each element has the format (AREA ID PLIST).
23236 An AREA is specified as either a rectangle, a circle, or a polygon:
23237 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23238 pixel coordinates of the upper left and bottom right corners.
23239 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23240 and the radius of the circle; r may be a float or integer.
23241 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23242 vector describes one corner in the polygon.
23243 Returns the alist element for the first matching AREA in MAP. */)
23244 (map, x, y)
23245 Lisp_Object map;
23246 Lisp_Object x, y;
23248 if (NILP (map))
23249 return Qnil;
23251 CHECK_NUMBER (x);
23252 CHECK_NUMBER (y);
23254 return find_hot_spot (map, XINT (x), XINT (y));
23258 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23259 static void
23260 define_frame_cursor1 (f, cursor, pointer)
23261 struct frame *f;
23262 Cursor cursor;
23263 Lisp_Object pointer;
23265 /* Do not change cursor shape while dragging mouse. */
23266 if (!NILP (do_mouse_tracking))
23267 return;
23269 if (!NILP (pointer))
23271 if (EQ (pointer, Qarrow))
23272 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23273 else if (EQ (pointer, Qhand))
23274 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23275 else if (EQ (pointer, Qtext))
23276 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23277 else if (EQ (pointer, intern ("hdrag")))
23278 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23279 #ifdef HAVE_X_WINDOWS
23280 else if (EQ (pointer, intern ("vdrag")))
23281 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23282 #endif
23283 else if (EQ (pointer, intern ("hourglass")))
23284 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23285 else if (EQ (pointer, Qmodeline))
23286 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23287 else
23288 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23291 if (cursor != No_Cursor)
23292 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23295 /* Take proper action when mouse has moved to the mode or header line
23296 or marginal area AREA of window W, x-position X and y-position Y.
23297 X is relative to the start of the text display area of W, so the
23298 width of bitmap areas and scroll bars must be subtracted to get a
23299 position relative to the start of the mode line. */
23301 static void
23302 note_mode_line_or_margin_highlight (window, x, y, area)
23303 Lisp_Object window;
23304 int x, y;
23305 enum window_part area;
23307 struct window *w = XWINDOW (window);
23308 struct frame *f = XFRAME (w->frame);
23309 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23310 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23311 Lisp_Object pointer = Qnil;
23312 int charpos, dx, dy, width, height;
23313 Lisp_Object string, object = Qnil;
23314 Lisp_Object pos, help;
23316 Lisp_Object mouse_face;
23317 int original_x_pixel = x;
23318 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23319 struct glyph_row *row;
23321 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23323 int x0;
23324 struct glyph *end;
23326 string = mode_line_string (w, area, &x, &y, &charpos,
23327 &object, &dx, &dy, &width, &height);
23329 row = (area == ON_MODE_LINE
23330 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23331 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23333 /* Find glyph */
23334 if (row->mode_line_p && row->enabled_p)
23336 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23337 end = glyph + row->used[TEXT_AREA];
23339 for (x0 = original_x_pixel;
23340 glyph < end && x0 >= glyph->pixel_width;
23341 ++glyph)
23342 x0 -= glyph->pixel_width;
23344 if (glyph >= end)
23345 glyph = NULL;
23348 else
23350 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23351 string = marginal_area_string (w, area, &x, &y, &charpos,
23352 &object, &dx, &dy, &width, &height);
23355 help = Qnil;
23357 if (IMAGEP (object))
23359 Lisp_Object image_map, hotspot;
23360 if ((image_map = Fplist_get (XCDR (object), QCmap),
23361 !NILP (image_map))
23362 && (hotspot = find_hot_spot (image_map, dx, dy),
23363 CONSP (hotspot))
23364 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23366 Lisp_Object area_id, plist;
23368 area_id = XCAR (hotspot);
23369 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23370 If so, we could look for mouse-enter, mouse-leave
23371 properties in PLIST (and do something...). */
23372 hotspot = XCDR (hotspot);
23373 if (CONSP (hotspot)
23374 && (plist = XCAR (hotspot), CONSP (plist)))
23376 pointer = Fplist_get (plist, Qpointer);
23377 if (NILP (pointer))
23378 pointer = Qhand;
23379 help = Fplist_get (plist, Qhelp_echo);
23380 if (!NILP (help))
23382 help_echo_string = help;
23383 /* Is this correct? ++kfs */
23384 XSETWINDOW (help_echo_window, w);
23385 help_echo_object = w->buffer;
23386 help_echo_pos = charpos;
23390 if (NILP (pointer))
23391 pointer = Fplist_get (XCDR (object), QCpointer);
23394 if (STRINGP (string))
23396 pos = make_number (charpos);
23397 /* If we're on a string with `help-echo' text property, arrange
23398 for the help to be displayed. This is done by setting the
23399 global variable help_echo_string to the help string. */
23400 if (NILP (help))
23402 help = Fget_text_property (pos, Qhelp_echo, string);
23403 if (!NILP (help))
23405 help_echo_string = help;
23406 XSETWINDOW (help_echo_window, w);
23407 help_echo_object = string;
23408 help_echo_pos = charpos;
23412 if (NILP (pointer))
23413 pointer = Fget_text_property (pos, Qpointer, string);
23415 /* Change the mouse pointer according to what is under X/Y. */
23416 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23418 Lisp_Object map;
23419 map = Fget_text_property (pos, Qlocal_map, string);
23420 if (!KEYMAPP (map))
23421 map = Fget_text_property (pos, Qkeymap, string);
23422 if (!KEYMAPP (map))
23423 cursor = dpyinfo->vertical_scroll_bar_cursor;
23426 /* Change the mouse face according to what is under X/Y. */
23427 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23428 if (!NILP (mouse_face)
23429 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23430 && glyph)
23432 Lisp_Object b, e;
23434 struct glyph * tmp_glyph;
23436 int gpos;
23437 int gseq_length;
23438 int total_pixel_width;
23439 EMACS_INT ignore;
23441 int vpos, hpos;
23443 b = Fprevious_single_property_change (make_number (charpos + 1),
23444 Qmouse_face, string, Qnil);
23445 if (NILP (b))
23446 b = make_number (0);
23448 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23449 if (NILP (e))
23450 e = make_number (SCHARS (string));
23452 /* Calculate the position(glyph position: GPOS) of GLYPH in
23453 displayed string. GPOS is different from CHARPOS.
23455 CHARPOS is the position of glyph in internal string
23456 object. A mode line string format has structures which
23457 is converted to a flatten by emacs lisp interpreter.
23458 The internal string is an element of the structures.
23459 The displayed string is the flatten string. */
23460 gpos = 0;
23461 if (glyph > row_start_glyph)
23463 tmp_glyph = glyph - 1;
23464 while (tmp_glyph >= row_start_glyph
23465 && tmp_glyph->charpos >= XINT (b)
23466 && EQ (tmp_glyph->object, glyph->object))
23468 tmp_glyph--;
23469 gpos++;
23473 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23474 displayed string holding GLYPH.
23476 GSEQ_LENGTH is different from SCHARS (STRING).
23477 SCHARS (STRING) returns the length of the internal string. */
23478 for (tmp_glyph = glyph, gseq_length = gpos;
23479 tmp_glyph->charpos < XINT (e);
23480 tmp_glyph++, gseq_length++)
23482 if (!EQ (tmp_glyph->object, glyph->object))
23483 break;
23486 total_pixel_width = 0;
23487 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23488 total_pixel_width += tmp_glyph->pixel_width;
23490 /* Pre calculation of re-rendering position */
23491 vpos = (x - gpos);
23492 hpos = (area == ON_MODE_LINE
23493 ? (w->current_matrix)->nrows - 1
23494 : 0);
23496 /* If the re-rendering position is included in the last
23497 re-rendering area, we should do nothing. */
23498 if ( EQ (window, dpyinfo->mouse_face_window)
23499 && dpyinfo->mouse_face_beg_col <= vpos
23500 && vpos < dpyinfo->mouse_face_end_col
23501 && dpyinfo->mouse_face_beg_row == hpos )
23502 return;
23504 if (clear_mouse_face (dpyinfo))
23505 cursor = No_Cursor;
23507 dpyinfo->mouse_face_beg_col = vpos;
23508 dpyinfo->mouse_face_beg_row = hpos;
23510 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23511 dpyinfo->mouse_face_beg_y = 0;
23513 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23514 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23516 dpyinfo->mouse_face_end_x = 0;
23517 dpyinfo->mouse_face_end_y = 0;
23519 dpyinfo->mouse_face_past_end = 0;
23520 dpyinfo->mouse_face_window = window;
23522 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23523 charpos,
23524 0, 0, 0, &ignore,
23525 glyph->face_id, 1);
23526 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23528 if (NILP (pointer))
23529 pointer = Qhand;
23531 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23532 clear_mouse_face (dpyinfo);
23534 define_frame_cursor1 (f, cursor, pointer);
23538 /* EXPORT:
23539 Take proper action when the mouse has moved to position X, Y on
23540 frame F as regards highlighting characters that have mouse-face
23541 properties. Also de-highlighting chars where the mouse was before.
23542 X and Y can be negative or out of range. */
23544 void
23545 note_mouse_highlight (f, x, y)
23546 struct frame *f;
23547 int x, y;
23549 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23550 enum window_part part;
23551 Lisp_Object window;
23552 struct window *w;
23553 Cursor cursor = No_Cursor;
23554 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23555 struct buffer *b;
23557 /* When a menu is active, don't highlight because this looks odd. */
23558 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23559 if (popup_activated ())
23560 return;
23561 #endif
23563 if (NILP (Vmouse_highlight)
23564 || !f->glyphs_initialized_p)
23565 return;
23567 dpyinfo->mouse_face_mouse_x = x;
23568 dpyinfo->mouse_face_mouse_y = y;
23569 dpyinfo->mouse_face_mouse_frame = f;
23571 if (dpyinfo->mouse_face_defer)
23572 return;
23574 if (gc_in_progress)
23576 dpyinfo->mouse_face_deferred_gc = 1;
23577 return;
23580 /* Which window is that in? */
23581 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23583 /* If we were displaying active text in another window, clear that.
23584 Also clear if we move out of text area in same window. */
23585 if (! EQ (window, dpyinfo->mouse_face_window)
23586 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23587 && !NILP (dpyinfo->mouse_face_window)))
23588 clear_mouse_face (dpyinfo);
23590 /* Not on a window -> return. */
23591 if (!WINDOWP (window))
23592 return;
23594 /* Reset help_echo_string. It will get recomputed below. */
23595 help_echo_string = Qnil;
23597 /* Convert to window-relative pixel coordinates. */
23598 w = XWINDOW (window);
23599 frame_to_window_pixel_xy (w, &x, &y);
23601 /* Handle tool-bar window differently since it doesn't display a
23602 buffer. */
23603 if (EQ (window, f->tool_bar_window))
23605 note_tool_bar_highlight (f, x, y);
23606 return;
23609 /* Mouse is on the mode, header line or margin? */
23610 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23611 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23613 note_mode_line_or_margin_highlight (window, x, y, part);
23614 return;
23617 if (part == ON_VERTICAL_BORDER)
23619 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23620 help_echo_string = build_string ("drag-mouse-1: resize");
23622 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23623 || part == ON_SCROLL_BAR)
23624 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23625 else
23626 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23628 /* Are we in a window whose display is up to date?
23629 And verify the buffer's text has not changed. */
23630 b = XBUFFER (w->buffer);
23631 if (part == ON_TEXT
23632 && EQ (w->window_end_valid, w->buffer)
23633 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23634 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23636 int hpos, vpos, pos, i, dx, dy, area;
23637 struct glyph *glyph;
23638 Lisp_Object object;
23639 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23640 Lisp_Object *overlay_vec = NULL;
23641 int noverlays;
23642 struct buffer *obuf;
23643 int obegv, ozv, same_region;
23645 /* Find the glyph under X/Y. */
23646 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23648 /* Look for :pointer property on image. */
23649 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23651 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23652 if (img != NULL && IMAGEP (img->spec))
23654 Lisp_Object image_map, hotspot;
23655 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23656 !NILP (image_map))
23657 && (hotspot = find_hot_spot (image_map,
23658 glyph->slice.x + dx,
23659 glyph->slice.y + dy),
23660 CONSP (hotspot))
23661 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23663 Lisp_Object area_id, plist;
23665 area_id = XCAR (hotspot);
23666 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23667 If so, we could look for mouse-enter, mouse-leave
23668 properties in PLIST (and do something...). */
23669 hotspot = XCDR (hotspot);
23670 if (CONSP (hotspot)
23671 && (plist = XCAR (hotspot), CONSP (plist)))
23673 pointer = Fplist_get (plist, Qpointer);
23674 if (NILP (pointer))
23675 pointer = Qhand;
23676 help_echo_string = Fplist_get (plist, Qhelp_echo);
23677 if (!NILP (help_echo_string))
23679 help_echo_window = window;
23680 help_echo_object = glyph->object;
23681 help_echo_pos = glyph->charpos;
23685 if (NILP (pointer))
23686 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23690 /* Clear mouse face if X/Y not over text. */
23691 if (glyph == NULL
23692 || area != TEXT_AREA
23693 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23695 if (clear_mouse_face (dpyinfo))
23696 cursor = No_Cursor;
23697 if (NILP (pointer))
23699 if (area != TEXT_AREA)
23700 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23701 else
23702 pointer = Vvoid_text_area_pointer;
23704 goto set_cursor;
23707 pos = glyph->charpos;
23708 object = glyph->object;
23709 if (!STRINGP (object) && !BUFFERP (object))
23710 goto set_cursor;
23712 /* If we get an out-of-range value, return now; avoid an error. */
23713 if (BUFFERP (object) && pos > BUF_Z (b))
23714 goto set_cursor;
23716 /* Make the window's buffer temporarily current for
23717 overlays_at and compute_char_face. */
23718 obuf = current_buffer;
23719 current_buffer = b;
23720 obegv = BEGV;
23721 ozv = ZV;
23722 BEGV = BEG;
23723 ZV = Z;
23725 /* Is this char mouse-active or does it have help-echo? */
23726 position = make_number (pos);
23728 if (BUFFERP (object))
23730 /* Put all the overlays we want in a vector in overlay_vec. */
23731 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23732 /* Sort overlays into increasing priority order. */
23733 noverlays = sort_overlays (overlay_vec, noverlays, w);
23735 else
23736 noverlays = 0;
23738 same_region = (EQ (window, dpyinfo->mouse_face_window)
23739 && vpos >= dpyinfo->mouse_face_beg_row
23740 && vpos <= dpyinfo->mouse_face_end_row
23741 && (vpos > dpyinfo->mouse_face_beg_row
23742 || hpos >= dpyinfo->mouse_face_beg_col)
23743 && (vpos < dpyinfo->mouse_face_end_row
23744 || hpos < dpyinfo->mouse_face_end_col
23745 || dpyinfo->mouse_face_past_end));
23747 if (same_region)
23748 cursor = No_Cursor;
23750 /* Check mouse-face highlighting. */
23751 if (! same_region
23752 /* If there exists an overlay with mouse-face overlapping
23753 the one we are currently highlighting, we have to
23754 check if we enter the overlapping overlay, and then
23755 highlight only that. */
23756 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23757 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23759 /* Find the highest priority overlay with a mouse-face. */
23760 overlay = Qnil;
23761 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23763 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23764 if (!NILP (mouse_face))
23765 overlay = overlay_vec[i];
23768 /* If we're highlighting the same overlay as before, there's
23769 no need to do that again. */
23770 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23771 goto check_help_echo;
23772 dpyinfo->mouse_face_overlay = overlay;
23774 /* Clear the display of the old active region, if any. */
23775 if (clear_mouse_face (dpyinfo))
23776 cursor = No_Cursor;
23778 /* If no overlay applies, get a text property. */
23779 if (NILP (overlay))
23780 mouse_face = Fget_text_property (position, Qmouse_face, object);
23782 /* Next, compute the bounds of the mouse highlighting and
23783 display it. */
23784 if (!NILP (mouse_face) && STRINGP (object))
23786 /* The mouse-highlighting comes from a display string
23787 with a mouse-face. */
23788 Lisp_Object b, e;
23789 EMACS_INT ignore;
23791 b = Fprevious_single_property_change
23792 (make_number (pos + 1), Qmouse_face, object, Qnil);
23793 e = Fnext_single_property_change
23794 (position, Qmouse_face, object, Qnil);
23795 if (NILP (b))
23796 b = make_number (0);
23797 if (NILP (e))
23798 e = make_number (SCHARS (object) - 1);
23800 fast_find_string_pos (w, XINT (b), object,
23801 &dpyinfo->mouse_face_beg_col,
23802 &dpyinfo->mouse_face_beg_row,
23803 &dpyinfo->mouse_face_beg_x,
23804 &dpyinfo->mouse_face_beg_y, 0);
23805 fast_find_string_pos (w, XINT (e), object,
23806 &dpyinfo->mouse_face_end_col,
23807 &dpyinfo->mouse_face_end_row,
23808 &dpyinfo->mouse_face_end_x,
23809 &dpyinfo->mouse_face_end_y, 1);
23810 dpyinfo->mouse_face_past_end = 0;
23811 dpyinfo->mouse_face_window = window;
23812 dpyinfo->mouse_face_face_id
23813 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23814 glyph->face_id, 1);
23815 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23816 cursor = No_Cursor;
23818 else
23820 /* The mouse-highlighting, if any, comes from an overlay
23821 or text property in the buffer. */
23822 Lisp_Object buffer, display_string;
23824 if (STRINGP (object))
23826 /* If we are on a display string with no mouse-face,
23827 check if the text under it has one. */
23828 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23829 int start = MATRIX_ROW_START_CHARPOS (r);
23830 pos = string_buffer_position (w, object, start);
23831 if (pos > 0)
23833 mouse_face = get_char_property_and_overlay
23834 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23835 buffer = w->buffer;
23836 display_string = object;
23839 else
23841 buffer = object;
23842 display_string = Qnil;
23845 if (!NILP (mouse_face))
23847 Lisp_Object before, after;
23848 Lisp_Object before_string, after_string;
23850 if (NILP (overlay))
23852 /* Handle the text property case. */
23853 before = Fprevious_single_property_change
23854 (make_number (pos + 1), Qmouse_face, buffer,
23855 Fmarker_position (w->start));
23856 after = Fnext_single_property_change
23857 (make_number (pos), Qmouse_face, buffer,
23858 make_number (BUF_Z (XBUFFER (buffer))
23859 - XFASTINT (w->window_end_pos)));
23860 before_string = after_string = Qnil;
23862 else
23864 /* Handle the overlay case. */
23865 before = Foverlay_start (overlay);
23866 after = Foverlay_end (overlay);
23867 before_string = Foverlay_get (overlay, Qbefore_string);
23868 after_string = Foverlay_get (overlay, Qafter_string);
23870 if (!STRINGP (before_string)) before_string = Qnil;
23871 if (!STRINGP (after_string)) after_string = Qnil;
23874 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23875 XFASTINT (before),
23876 XFASTINT (after),
23877 before_string, after_string,
23878 display_string);
23879 cursor = No_Cursor;
23884 check_help_echo:
23886 /* Look for a `help-echo' property. */
23887 if (NILP (help_echo_string)) {
23888 Lisp_Object help, overlay;
23890 /* Check overlays first. */
23891 help = overlay = Qnil;
23892 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23894 overlay = overlay_vec[i];
23895 help = Foverlay_get (overlay, Qhelp_echo);
23898 if (!NILP (help))
23900 help_echo_string = help;
23901 help_echo_window = window;
23902 help_echo_object = overlay;
23903 help_echo_pos = pos;
23905 else
23907 Lisp_Object object = glyph->object;
23908 int charpos = glyph->charpos;
23910 /* Try text properties. */
23911 if (STRINGP (object)
23912 && charpos >= 0
23913 && charpos < SCHARS (object))
23915 help = Fget_text_property (make_number (charpos),
23916 Qhelp_echo, object);
23917 if (NILP (help))
23919 /* If the string itself doesn't specify a help-echo,
23920 see if the buffer text ``under'' it does. */
23921 struct glyph_row *r
23922 = MATRIX_ROW (w->current_matrix, vpos);
23923 int start = MATRIX_ROW_START_CHARPOS (r);
23924 int pos = string_buffer_position (w, object, start);
23925 if (pos > 0)
23927 help = Fget_char_property (make_number (pos),
23928 Qhelp_echo, w->buffer);
23929 if (!NILP (help))
23931 charpos = pos;
23932 object = w->buffer;
23937 else if (BUFFERP (object)
23938 && charpos >= BEGV
23939 && charpos < ZV)
23940 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23941 object);
23943 if (!NILP (help))
23945 help_echo_string = help;
23946 help_echo_window = window;
23947 help_echo_object = object;
23948 help_echo_pos = charpos;
23953 /* Look for a `pointer' property. */
23954 if (NILP (pointer))
23956 /* Check overlays first. */
23957 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23958 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23960 if (NILP (pointer))
23962 Lisp_Object object = glyph->object;
23963 int charpos = glyph->charpos;
23965 /* Try text properties. */
23966 if (STRINGP (object)
23967 && charpos >= 0
23968 && charpos < SCHARS (object))
23970 pointer = Fget_text_property (make_number (charpos),
23971 Qpointer, object);
23972 if (NILP (pointer))
23974 /* If the string itself doesn't specify a pointer,
23975 see if the buffer text ``under'' it does. */
23976 struct glyph_row *r
23977 = MATRIX_ROW (w->current_matrix, vpos);
23978 int start = MATRIX_ROW_START_CHARPOS (r);
23979 int pos = string_buffer_position (w, object, start);
23980 if (pos > 0)
23981 pointer = Fget_char_property (make_number (pos),
23982 Qpointer, w->buffer);
23985 else if (BUFFERP (object)
23986 && charpos >= BEGV
23987 && charpos < ZV)
23988 pointer = Fget_text_property (make_number (charpos),
23989 Qpointer, object);
23993 BEGV = obegv;
23994 ZV = ozv;
23995 current_buffer = obuf;
23998 set_cursor:
24000 define_frame_cursor1 (f, cursor, pointer);
24004 /* EXPORT for RIF:
24005 Clear any mouse-face on window W. This function is part of the
24006 redisplay interface, and is called from try_window_id and similar
24007 functions to ensure the mouse-highlight is off. */
24009 void
24010 x_clear_window_mouse_face (w)
24011 struct window *w;
24013 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24014 Lisp_Object window;
24016 BLOCK_INPUT;
24017 XSETWINDOW (window, w);
24018 if (EQ (window, dpyinfo->mouse_face_window))
24019 clear_mouse_face (dpyinfo);
24020 UNBLOCK_INPUT;
24024 /* EXPORT:
24025 Just discard the mouse face information for frame F, if any.
24026 This is used when the size of F is changed. */
24028 void
24029 cancel_mouse_face (f)
24030 struct frame *f;
24032 Lisp_Object window;
24033 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24035 window = dpyinfo->mouse_face_window;
24036 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24038 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24039 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24040 dpyinfo->mouse_face_window = Qnil;
24045 #endif /* HAVE_WINDOW_SYSTEM */
24048 /***********************************************************************
24049 Exposure Events
24050 ***********************************************************************/
24052 #ifdef HAVE_WINDOW_SYSTEM
24054 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24055 which intersects rectangle R. R is in window-relative coordinates. */
24057 static void
24058 expose_area (w, row, r, area)
24059 struct window *w;
24060 struct glyph_row *row;
24061 XRectangle *r;
24062 enum glyph_row_area area;
24064 struct glyph *first = row->glyphs[area];
24065 struct glyph *end = row->glyphs[area] + row->used[area];
24066 struct glyph *last;
24067 int first_x, start_x, x;
24069 if (area == TEXT_AREA && row->fill_line_p)
24070 /* If row extends face to end of line write the whole line. */
24071 draw_glyphs (w, 0, row, area,
24072 0, row->used[area],
24073 DRAW_NORMAL_TEXT, 0);
24074 else
24076 /* Set START_X to the window-relative start position for drawing glyphs of
24077 AREA. The first glyph of the text area can be partially visible.
24078 The first glyphs of other areas cannot. */
24079 start_x = window_box_left_offset (w, area);
24080 x = start_x;
24081 if (area == TEXT_AREA)
24082 x += row->x;
24084 /* Find the first glyph that must be redrawn. */
24085 while (first < end
24086 && x + first->pixel_width < r->x)
24088 x += first->pixel_width;
24089 ++first;
24092 /* Find the last one. */
24093 last = first;
24094 first_x = x;
24095 while (last < end
24096 && x < r->x + r->width)
24098 x += last->pixel_width;
24099 ++last;
24102 /* Repaint. */
24103 if (last > first)
24104 draw_glyphs (w, first_x - start_x, row, area,
24105 first - row->glyphs[area], last - row->glyphs[area],
24106 DRAW_NORMAL_TEXT, 0);
24111 /* Redraw the parts of the glyph row ROW on window W intersecting
24112 rectangle R. R is in window-relative coordinates. Value is
24113 non-zero if mouse-face was overwritten. */
24115 static int
24116 expose_line (w, row, r)
24117 struct window *w;
24118 struct glyph_row *row;
24119 XRectangle *r;
24121 xassert (row->enabled_p);
24123 if (row->mode_line_p || w->pseudo_window_p)
24124 draw_glyphs (w, 0, row, TEXT_AREA,
24125 0, row->used[TEXT_AREA],
24126 DRAW_NORMAL_TEXT, 0);
24127 else
24129 if (row->used[LEFT_MARGIN_AREA])
24130 expose_area (w, row, r, LEFT_MARGIN_AREA);
24131 if (row->used[TEXT_AREA])
24132 expose_area (w, row, r, TEXT_AREA);
24133 if (row->used[RIGHT_MARGIN_AREA])
24134 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24135 draw_row_fringe_bitmaps (w, row);
24138 return row->mouse_face_p;
24142 /* Redraw those parts of glyphs rows during expose event handling that
24143 overlap other rows. Redrawing of an exposed line writes over parts
24144 of lines overlapping that exposed line; this function fixes that.
24146 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24147 row in W's current matrix that is exposed and overlaps other rows.
24148 LAST_OVERLAPPING_ROW is the last such row. */
24150 static void
24151 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24152 struct window *w;
24153 struct glyph_row *first_overlapping_row;
24154 struct glyph_row *last_overlapping_row;
24155 XRectangle *r;
24157 struct glyph_row *row;
24159 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24160 if (row->overlapping_p)
24162 xassert (row->enabled_p && !row->mode_line_p);
24164 row->clip = r;
24165 if (row->used[LEFT_MARGIN_AREA])
24166 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24168 if (row->used[TEXT_AREA])
24169 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24171 if (row->used[RIGHT_MARGIN_AREA])
24172 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24173 row->clip = NULL;
24178 /* Return non-zero if W's cursor intersects rectangle R. */
24180 static int
24181 phys_cursor_in_rect_p (w, r)
24182 struct window *w;
24183 XRectangle *r;
24185 XRectangle cr, result;
24186 struct glyph *cursor_glyph;
24187 struct glyph_row *row;
24189 if (w->phys_cursor.vpos >= 0
24190 && w->phys_cursor.vpos < w->current_matrix->nrows
24191 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24192 row->enabled_p)
24193 && row->cursor_in_fringe_p)
24195 /* Cursor is in the fringe. */
24196 cr.x = window_box_right_offset (w,
24197 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24198 ? RIGHT_MARGIN_AREA
24199 : TEXT_AREA));
24200 cr.y = row->y;
24201 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24202 cr.height = row->height;
24203 return x_intersect_rectangles (&cr, r, &result);
24206 cursor_glyph = get_phys_cursor_glyph (w);
24207 if (cursor_glyph)
24209 /* r is relative to W's box, but w->phys_cursor.x is relative
24210 to left edge of W's TEXT area. Adjust it. */
24211 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24212 cr.y = w->phys_cursor.y;
24213 cr.width = cursor_glyph->pixel_width;
24214 cr.height = w->phys_cursor_height;
24215 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24216 I assume the effect is the same -- and this is portable. */
24217 return x_intersect_rectangles (&cr, r, &result);
24219 /* If we don't understand the format, pretend we're not in the hot-spot. */
24220 return 0;
24224 /* EXPORT:
24225 Draw a vertical window border to the right of window W if W doesn't
24226 have vertical scroll bars. */
24228 void
24229 x_draw_vertical_border (w)
24230 struct window *w;
24232 struct frame *f = XFRAME (WINDOW_FRAME (w));
24234 /* We could do better, if we knew what type of scroll-bar the adjacent
24235 windows (on either side) have... But we don't :-(
24236 However, I think this works ok. ++KFS 2003-04-25 */
24238 /* Redraw borders between horizontally adjacent windows. Don't
24239 do it for frames with vertical scroll bars because either the
24240 right scroll bar of a window, or the left scroll bar of its
24241 neighbor will suffice as a border. */
24242 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24243 return;
24245 if (!WINDOW_RIGHTMOST_P (w)
24246 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24248 int x0, x1, y0, y1;
24250 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24251 y1 -= 1;
24253 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24254 x1 -= 1;
24256 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24258 else if (!WINDOW_LEFTMOST_P (w)
24259 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24261 int x0, x1, y0, y1;
24263 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24264 y1 -= 1;
24266 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24267 x0 -= 1;
24269 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24274 /* Redraw the part of window W intersection rectangle FR. Pixel
24275 coordinates in FR are frame-relative. Call this function with
24276 input blocked. Value is non-zero if the exposure overwrites
24277 mouse-face. */
24279 static int
24280 expose_window (w, fr)
24281 struct window *w;
24282 XRectangle *fr;
24284 struct frame *f = XFRAME (w->frame);
24285 XRectangle wr, r;
24286 int mouse_face_overwritten_p = 0;
24288 /* If window is not yet fully initialized, do nothing. This can
24289 happen when toolkit scroll bars are used and a window is split.
24290 Reconfiguring the scroll bar will generate an expose for a newly
24291 created window. */
24292 if (w->current_matrix == NULL)
24293 return 0;
24295 /* When we're currently updating the window, display and current
24296 matrix usually don't agree. Arrange for a thorough display
24297 later. */
24298 if (w == updated_window)
24300 SET_FRAME_GARBAGED (f);
24301 return 0;
24304 /* Frame-relative pixel rectangle of W. */
24305 wr.x = WINDOW_LEFT_EDGE_X (w);
24306 wr.y = WINDOW_TOP_EDGE_Y (w);
24307 wr.width = WINDOW_TOTAL_WIDTH (w);
24308 wr.height = WINDOW_TOTAL_HEIGHT (w);
24310 if (x_intersect_rectangles (fr, &wr, &r))
24312 int yb = window_text_bottom_y (w);
24313 struct glyph_row *row;
24314 int cursor_cleared_p;
24315 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24317 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24318 r.x, r.y, r.width, r.height));
24320 /* Convert to window coordinates. */
24321 r.x -= WINDOW_LEFT_EDGE_X (w);
24322 r.y -= WINDOW_TOP_EDGE_Y (w);
24324 /* Turn off the cursor. */
24325 if (!w->pseudo_window_p
24326 && phys_cursor_in_rect_p (w, &r))
24328 x_clear_cursor (w);
24329 cursor_cleared_p = 1;
24331 else
24332 cursor_cleared_p = 0;
24334 /* Update lines intersecting rectangle R. */
24335 first_overlapping_row = last_overlapping_row = NULL;
24336 for (row = w->current_matrix->rows;
24337 row->enabled_p;
24338 ++row)
24340 int y0 = row->y;
24341 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24343 if ((y0 >= r.y && y0 < r.y + r.height)
24344 || (y1 > r.y && y1 < r.y + r.height)
24345 || (r.y >= y0 && r.y < y1)
24346 || (r.y + r.height > y0 && r.y + r.height < y1))
24348 /* A header line may be overlapping, but there is no need
24349 to fix overlapping areas for them. KFS 2005-02-12 */
24350 if (row->overlapping_p && !row->mode_line_p)
24352 if (first_overlapping_row == NULL)
24353 first_overlapping_row = row;
24354 last_overlapping_row = row;
24357 row->clip = fr;
24358 if (expose_line (w, row, &r))
24359 mouse_face_overwritten_p = 1;
24360 row->clip = NULL;
24362 else if (row->overlapping_p)
24364 /* We must redraw a row overlapping the exposed area. */
24365 if (y0 < r.y
24366 ? y0 + row->phys_height > r.y
24367 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24369 if (first_overlapping_row == NULL)
24370 first_overlapping_row = row;
24371 last_overlapping_row = row;
24375 if (y1 >= yb)
24376 break;
24379 /* Display the mode line if there is one. */
24380 if (WINDOW_WANTS_MODELINE_P (w)
24381 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24382 row->enabled_p)
24383 && row->y < r.y + r.height)
24385 if (expose_line (w, row, &r))
24386 mouse_face_overwritten_p = 1;
24389 if (!w->pseudo_window_p)
24391 /* Fix the display of overlapping rows. */
24392 if (first_overlapping_row)
24393 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24394 fr);
24396 /* Draw border between windows. */
24397 x_draw_vertical_border (w);
24399 /* Turn the cursor on again. */
24400 if (cursor_cleared_p)
24401 update_window_cursor (w, 1);
24405 return mouse_face_overwritten_p;
24410 /* Redraw (parts) of all windows in the window tree rooted at W that
24411 intersect R. R contains frame pixel coordinates. Value is
24412 non-zero if the exposure overwrites mouse-face. */
24414 static int
24415 expose_window_tree (w, r)
24416 struct window *w;
24417 XRectangle *r;
24419 struct frame *f = XFRAME (w->frame);
24420 int mouse_face_overwritten_p = 0;
24422 while (w && !FRAME_GARBAGED_P (f))
24424 if (!NILP (w->hchild))
24425 mouse_face_overwritten_p
24426 |= expose_window_tree (XWINDOW (w->hchild), r);
24427 else if (!NILP (w->vchild))
24428 mouse_face_overwritten_p
24429 |= expose_window_tree (XWINDOW (w->vchild), r);
24430 else
24431 mouse_face_overwritten_p |= expose_window (w, r);
24433 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24436 return mouse_face_overwritten_p;
24440 /* EXPORT:
24441 Redisplay an exposed area of frame F. X and Y are the upper-left
24442 corner of the exposed rectangle. W and H are width and height of
24443 the exposed area. All are pixel values. W or H zero means redraw
24444 the entire frame. */
24446 void
24447 expose_frame (f, x, y, w, h)
24448 struct frame *f;
24449 int x, y, w, h;
24451 XRectangle r;
24452 int mouse_face_overwritten_p = 0;
24454 TRACE ((stderr, "expose_frame "));
24456 /* No need to redraw if frame will be redrawn soon. */
24457 if (FRAME_GARBAGED_P (f))
24459 TRACE ((stderr, " garbaged\n"));
24460 return;
24463 /* If basic faces haven't been realized yet, there is no point in
24464 trying to redraw anything. This can happen when we get an expose
24465 event while Emacs is starting, e.g. by moving another window. */
24466 if (FRAME_FACE_CACHE (f) == NULL
24467 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24469 TRACE ((stderr, " no faces\n"));
24470 return;
24473 if (w == 0 || h == 0)
24475 r.x = r.y = 0;
24476 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24477 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24479 else
24481 r.x = x;
24482 r.y = y;
24483 r.width = w;
24484 r.height = h;
24487 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24488 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24490 if (WINDOWP (f->tool_bar_window))
24491 mouse_face_overwritten_p
24492 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24494 #ifdef HAVE_X_WINDOWS
24495 #ifndef MSDOS
24496 #ifndef USE_X_TOOLKIT
24497 if (WINDOWP (f->menu_bar_window))
24498 mouse_face_overwritten_p
24499 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24500 #endif /* not USE_X_TOOLKIT */
24501 #endif
24502 #endif
24504 /* Some window managers support a focus-follows-mouse style with
24505 delayed raising of frames. Imagine a partially obscured frame,
24506 and moving the mouse into partially obscured mouse-face on that
24507 frame. The visible part of the mouse-face will be highlighted,
24508 then the WM raises the obscured frame. With at least one WM, KDE
24509 2.1, Emacs is not getting any event for the raising of the frame
24510 (even tried with SubstructureRedirectMask), only Expose events.
24511 These expose events will draw text normally, i.e. not
24512 highlighted. Which means we must redo the highlight here.
24513 Subsume it under ``we love X''. --gerd 2001-08-15 */
24514 /* Included in Windows version because Windows most likely does not
24515 do the right thing if any third party tool offers
24516 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24517 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24519 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24520 if (f == dpyinfo->mouse_face_mouse_frame)
24522 int x = dpyinfo->mouse_face_mouse_x;
24523 int y = dpyinfo->mouse_face_mouse_y;
24524 clear_mouse_face (dpyinfo);
24525 note_mouse_highlight (f, x, y);
24531 /* EXPORT:
24532 Determine the intersection of two rectangles R1 and R2. Return
24533 the intersection in *RESULT. Value is non-zero if RESULT is not
24534 empty. */
24537 x_intersect_rectangles (r1, r2, result)
24538 XRectangle *r1, *r2, *result;
24540 XRectangle *left, *right;
24541 XRectangle *upper, *lower;
24542 int intersection_p = 0;
24544 /* Rearrange so that R1 is the left-most rectangle. */
24545 if (r1->x < r2->x)
24546 left = r1, right = r2;
24547 else
24548 left = r2, right = r1;
24550 /* X0 of the intersection is right.x0, if this is inside R1,
24551 otherwise there is no intersection. */
24552 if (right->x <= left->x + left->width)
24554 result->x = right->x;
24556 /* The right end of the intersection is the minimum of the
24557 the right ends of left and right. */
24558 result->width = (min (left->x + left->width, right->x + right->width)
24559 - result->x);
24561 /* Same game for Y. */
24562 if (r1->y < r2->y)
24563 upper = r1, lower = r2;
24564 else
24565 upper = r2, lower = r1;
24567 /* The upper end of the intersection is lower.y0, if this is inside
24568 of upper. Otherwise, there is no intersection. */
24569 if (lower->y <= upper->y + upper->height)
24571 result->y = lower->y;
24573 /* The lower end of the intersection is the minimum of the lower
24574 ends of upper and lower. */
24575 result->height = (min (lower->y + lower->height,
24576 upper->y + upper->height)
24577 - result->y);
24578 intersection_p = 1;
24582 return intersection_p;
24585 #endif /* HAVE_WINDOW_SYSTEM */
24588 /***********************************************************************
24589 Initialization
24590 ***********************************************************************/
24592 void
24593 syms_of_xdisp ()
24595 Vwith_echo_area_save_vector = Qnil;
24596 staticpro (&Vwith_echo_area_save_vector);
24598 Vmessage_stack = Qnil;
24599 staticpro (&Vmessage_stack);
24601 Qinhibit_redisplay = intern ("inhibit-redisplay");
24602 staticpro (&Qinhibit_redisplay);
24604 message_dolog_marker1 = Fmake_marker ();
24605 staticpro (&message_dolog_marker1);
24606 message_dolog_marker2 = Fmake_marker ();
24607 staticpro (&message_dolog_marker2);
24608 message_dolog_marker3 = Fmake_marker ();
24609 staticpro (&message_dolog_marker3);
24611 #if GLYPH_DEBUG
24612 defsubr (&Sdump_frame_glyph_matrix);
24613 defsubr (&Sdump_glyph_matrix);
24614 defsubr (&Sdump_glyph_row);
24615 defsubr (&Sdump_tool_bar_row);
24616 defsubr (&Strace_redisplay);
24617 defsubr (&Strace_to_stderr);
24618 #endif
24619 #ifdef HAVE_WINDOW_SYSTEM
24620 defsubr (&Stool_bar_lines_needed);
24621 defsubr (&Slookup_image_map);
24622 #endif
24623 defsubr (&Sformat_mode_line);
24624 defsubr (&Sinvisible_p);
24626 staticpro (&Qmenu_bar_update_hook);
24627 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24629 staticpro (&Qoverriding_terminal_local_map);
24630 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24632 staticpro (&Qoverriding_local_map);
24633 Qoverriding_local_map = intern ("overriding-local-map");
24635 staticpro (&Qwindow_scroll_functions);
24636 Qwindow_scroll_functions = intern ("window-scroll-functions");
24638 staticpro (&Qwindow_text_change_functions);
24639 Qwindow_text_change_functions = intern ("window-text-change-functions");
24641 staticpro (&Qredisplay_end_trigger_functions);
24642 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24644 staticpro (&Qinhibit_point_motion_hooks);
24645 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24647 Qeval = intern ("eval");
24648 staticpro (&Qeval);
24650 QCdata = intern (":data");
24651 staticpro (&QCdata);
24652 Qdisplay = intern ("display");
24653 staticpro (&Qdisplay);
24654 Qspace_width = intern ("space-width");
24655 staticpro (&Qspace_width);
24656 Qraise = intern ("raise");
24657 staticpro (&Qraise);
24658 Qslice = intern ("slice");
24659 staticpro (&Qslice);
24660 Qspace = intern ("space");
24661 staticpro (&Qspace);
24662 Qmargin = intern ("margin");
24663 staticpro (&Qmargin);
24664 Qpointer = intern ("pointer");
24665 staticpro (&Qpointer);
24666 Qleft_margin = intern ("left-margin");
24667 staticpro (&Qleft_margin);
24668 Qright_margin = intern ("right-margin");
24669 staticpro (&Qright_margin);
24670 Qcenter = intern ("center");
24671 staticpro (&Qcenter);
24672 Qline_height = intern ("line-height");
24673 staticpro (&Qline_height);
24674 QCalign_to = intern (":align-to");
24675 staticpro (&QCalign_to);
24676 QCrelative_width = intern (":relative-width");
24677 staticpro (&QCrelative_width);
24678 QCrelative_height = intern (":relative-height");
24679 staticpro (&QCrelative_height);
24680 QCeval = intern (":eval");
24681 staticpro (&QCeval);
24682 QCpropertize = intern (":propertize");
24683 staticpro (&QCpropertize);
24684 QCfile = intern (":file");
24685 staticpro (&QCfile);
24686 Qfontified = intern ("fontified");
24687 staticpro (&Qfontified);
24688 Qfontification_functions = intern ("fontification-functions");
24689 staticpro (&Qfontification_functions);
24690 Qtrailing_whitespace = intern ("trailing-whitespace");
24691 staticpro (&Qtrailing_whitespace);
24692 Qescape_glyph = intern ("escape-glyph");
24693 staticpro (&Qescape_glyph);
24694 Qnobreak_space = intern ("nobreak-space");
24695 staticpro (&Qnobreak_space);
24696 Qimage = intern ("image");
24697 staticpro (&Qimage);
24698 QCmap = intern (":map");
24699 staticpro (&QCmap);
24700 QCpointer = intern (":pointer");
24701 staticpro (&QCpointer);
24702 Qrect = intern ("rect");
24703 staticpro (&Qrect);
24704 Qcircle = intern ("circle");
24705 staticpro (&Qcircle);
24706 Qpoly = intern ("poly");
24707 staticpro (&Qpoly);
24708 Qmessage_truncate_lines = intern ("message-truncate-lines");
24709 staticpro (&Qmessage_truncate_lines);
24710 Qgrow_only = intern ("grow-only");
24711 staticpro (&Qgrow_only);
24712 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24713 staticpro (&Qinhibit_menubar_update);
24714 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24715 staticpro (&Qinhibit_eval_during_redisplay);
24716 Qposition = intern ("position");
24717 staticpro (&Qposition);
24718 Qbuffer_position = intern ("buffer-position");
24719 staticpro (&Qbuffer_position);
24720 Qobject = intern ("object");
24721 staticpro (&Qobject);
24722 Qbar = intern ("bar");
24723 staticpro (&Qbar);
24724 Qhbar = intern ("hbar");
24725 staticpro (&Qhbar);
24726 Qbox = intern ("box");
24727 staticpro (&Qbox);
24728 Qhollow = intern ("hollow");
24729 staticpro (&Qhollow);
24730 Qhand = intern ("hand");
24731 staticpro (&Qhand);
24732 Qarrow = intern ("arrow");
24733 staticpro (&Qarrow);
24734 Qtext = intern ("text");
24735 staticpro (&Qtext);
24736 Qrisky_local_variable = intern ("risky-local-variable");
24737 staticpro (&Qrisky_local_variable);
24738 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24739 staticpro (&Qinhibit_free_realized_faces);
24741 list_of_error = Fcons (Fcons (intern ("error"),
24742 Fcons (intern ("void-variable"), Qnil)),
24743 Qnil);
24744 staticpro (&list_of_error);
24746 Qlast_arrow_position = intern ("last-arrow-position");
24747 staticpro (&Qlast_arrow_position);
24748 Qlast_arrow_string = intern ("last-arrow-string");
24749 staticpro (&Qlast_arrow_string);
24751 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24752 staticpro (&Qoverlay_arrow_string);
24753 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24754 staticpro (&Qoverlay_arrow_bitmap);
24756 echo_buffer[0] = echo_buffer[1] = Qnil;
24757 staticpro (&echo_buffer[0]);
24758 staticpro (&echo_buffer[1]);
24760 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24761 staticpro (&echo_area_buffer[0]);
24762 staticpro (&echo_area_buffer[1]);
24764 Vmessages_buffer_name = build_string ("*Messages*");
24765 staticpro (&Vmessages_buffer_name);
24767 mode_line_proptrans_alist = Qnil;
24768 staticpro (&mode_line_proptrans_alist);
24769 mode_line_string_list = Qnil;
24770 staticpro (&mode_line_string_list);
24771 mode_line_string_face = Qnil;
24772 staticpro (&mode_line_string_face);
24773 mode_line_string_face_prop = Qnil;
24774 staticpro (&mode_line_string_face_prop);
24775 Vmode_line_unwind_vector = Qnil;
24776 staticpro (&Vmode_line_unwind_vector);
24778 help_echo_string = Qnil;
24779 staticpro (&help_echo_string);
24780 help_echo_object = Qnil;
24781 staticpro (&help_echo_object);
24782 help_echo_window = Qnil;
24783 staticpro (&help_echo_window);
24784 previous_help_echo_string = Qnil;
24785 staticpro (&previous_help_echo_string);
24786 help_echo_pos = -1;
24788 #ifdef HAVE_WINDOW_SYSTEM
24789 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24790 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24791 For example, if a block cursor is over a tab, it will be drawn as
24792 wide as that tab on the display. */);
24793 x_stretch_cursor_p = 0;
24794 #endif
24796 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24797 doc: /* *Non-nil means highlight trailing whitespace.
24798 The face used for trailing whitespace is `trailing-whitespace'. */);
24799 Vshow_trailing_whitespace = Qnil;
24801 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24802 doc: /* *Control highlighting of nobreak space and soft hyphen.
24803 A value of t means highlight the character itself (for nobreak space,
24804 use face `nobreak-space').
24805 A value of nil means no highlighting.
24806 Other values mean display the escape glyph followed by an ordinary
24807 space or ordinary hyphen. */);
24808 Vnobreak_char_display = Qt;
24810 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24811 doc: /* *The pointer shape to show in void text areas.
24812 A value of nil means to show the text pointer. Other options are `arrow',
24813 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24814 Vvoid_text_area_pointer = Qarrow;
24816 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24817 doc: /* Non-nil means don't actually do any redisplay.
24818 This is used for internal purposes. */);
24819 Vinhibit_redisplay = Qnil;
24821 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24822 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24823 Vglobal_mode_string = Qnil;
24825 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24826 doc: /* Marker for where to display an arrow on top of the buffer text.
24827 This must be the beginning of a line in order to work.
24828 See also `overlay-arrow-string'. */);
24829 Voverlay_arrow_position = Qnil;
24831 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24832 doc: /* String to display as an arrow in non-window frames.
24833 See also `overlay-arrow-position'. */);
24834 Voverlay_arrow_string = build_string ("=>");
24836 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24837 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24838 The symbols on this list are examined during redisplay to determine
24839 where to display overlay arrows. */);
24840 Voverlay_arrow_variable_list
24841 = Fcons (intern ("overlay-arrow-position"), Qnil);
24843 DEFVAR_INT ("scroll-step", &scroll_step,
24844 doc: /* *The number of lines to try scrolling a window by when point moves out.
24845 If that fails to bring point back on frame, point is centered instead.
24846 If this is zero, point is always centered after it moves off frame.
24847 If you want scrolling to always be a line at a time, you should set
24848 `scroll-conservatively' to a large value rather than set this to 1. */);
24850 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24851 doc: /* *Scroll up to this many lines, to bring point back on screen.
24852 If point moves off-screen, redisplay will scroll by up to
24853 `scroll-conservatively' lines in order to bring point just barely
24854 onto the screen again. If that cannot be done, then redisplay
24855 recenters point as usual.
24857 A value of zero means always recenter point if it moves off screen. */);
24858 scroll_conservatively = 0;
24860 DEFVAR_INT ("scroll-margin", &scroll_margin,
24861 doc: /* *Number of lines of margin at the top and bottom of a window.
24862 Recenter the window whenever point gets within this many lines
24863 of the top or bottom of the window. */);
24864 scroll_margin = 0;
24866 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24867 doc: /* Pixels per inch value for non-window system displays.
24868 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24869 Vdisplay_pixels_per_inch = make_float (72.0);
24871 #if GLYPH_DEBUG
24872 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24873 #endif
24875 DEFVAR_LISP ("truncate-partial-width-windows",
24876 &Vtruncate_partial_width_windows,
24877 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24878 For an integer value, truncate lines in each window with less than the
24879 full frame width, provided the window width is less than that integer;
24880 otherwise, respect the value of `truncate-lines'.
24882 For any other non-nil value, truncate lines in all windows with
24883 less than the full frame width.
24885 A value of nil means to respect the value of `truncate-lines'.
24887 If `word-wrap' is enabled, you might want to reduce this. */);
24888 Vtruncate_partial_width_windows = make_number (50);
24890 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24891 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24892 Any other value means to use the appropriate face, `mode-line',
24893 `header-line', or `menu' respectively. */);
24894 mode_line_inverse_video = 1;
24896 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24897 doc: /* *Maximum buffer size for which line number should be displayed.
24898 If the buffer is bigger than this, the line number does not appear
24899 in the mode line. A value of nil means no limit. */);
24900 Vline_number_display_limit = Qnil;
24902 DEFVAR_INT ("line-number-display-limit-width",
24903 &line_number_display_limit_width,
24904 doc: /* *Maximum line width (in characters) for line number display.
24905 If the average length of the lines near point is bigger than this, then the
24906 line number may be omitted from the mode line. */);
24907 line_number_display_limit_width = 200;
24909 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24910 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24911 highlight_nonselected_windows = 0;
24913 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24914 doc: /* Non-nil if more than one frame is visible on this display.
24915 Minibuffer-only frames don't count, but iconified frames do.
24916 This variable is not guaranteed to be accurate except while processing
24917 `frame-title-format' and `icon-title-format'. */);
24919 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24920 doc: /* Template for displaying the title bar of visible frames.
24921 \(Assuming the window manager supports this feature.)
24923 This variable has the same structure as `mode-line-format', except that
24924 the %c and %l constructs are ignored. It is used only on frames for
24925 which no explicit name has been set \(see `modify-frame-parameters'). */);
24927 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24928 doc: /* Template for displaying the title bar of an iconified frame.
24929 \(Assuming the window manager supports this feature.)
24930 This variable has the same structure as `mode-line-format' (which see),
24931 and is used only on frames for which no explicit name has been set
24932 \(see `modify-frame-parameters'). */);
24933 Vicon_title_format
24934 = Vframe_title_format
24935 = Fcons (intern ("multiple-frames"),
24936 Fcons (build_string ("%b"),
24937 Fcons (Fcons (empty_unibyte_string,
24938 Fcons (intern ("invocation-name"),
24939 Fcons (build_string ("@"),
24940 Fcons (intern ("system-name"),
24941 Qnil)))),
24942 Qnil)));
24944 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24945 doc: /* Maximum number of lines to keep in the message log buffer.
24946 If nil, disable message logging. If t, log messages but don't truncate
24947 the buffer when it becomes large. */);
24948 Vmessage_log_max = make_number (100);
24950 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24951 doc: /* Functions called before redisplay, if window sizes have changed.
24952 The value should be a list of functions that take one argument.
24953 Just before redisplay, for each frame, if any of its windows have changed
24954 size since the last redisplay, or have been split or deleted,
24955 all the functions in the list are called, with the frame as argument. */);
24956 Vwindow_size_change_functions = Qnil;
24958 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24959 doc: /* List of functions to call before redisplaying a window with scrolling.
24960 Each function is called with two arguments, the window and its new
24961 display-start position. Note that these functions are also called by
24962 `set-window-buffer'. Also note that the value of `window-end' is not
24963 valid when these functions are called. */);
24964 Vwindow_scroll_functions = Qnil;
24966 DEFVAR_LISP ("window-text-change-functions",
24967 &Vwindow_text_change_functions,
24968 doc: /* Functions to call in redisplay when text in the window might change. */);
24969 Vwindow_text_change_functions = Qnil;
24971 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24972 doc: /* Functions called when redisplay of a window reaches the end trigger.
24973 Each function is called with two arguments, the window and the end trigger value.
24974 See `set-window-redisplay-end-trigger'. */);
24975 Vredisplay_end_trigger_functions = Qnil;
24977 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24978 doc: /* *Non-nil means autoselect window with mouse pointer.
24979 If nil, do not autoselect windows.
24980 A positive number means delay autoselection by that many seconds: a
24981 window is autoselected only after the mouse has remained in that
24982 window for the duration of the delay.
24983 A negative number has a similar effect, but causes windows to be
24984 autoselected only after the mouse has stopped moving. \(Because of
24985 the way Emacs compares mouse events, you will occasionally wait twice
24986 that time before the window gets selected.\)
24987 Any other value means to autoselect window instantaneously when the
24988 mouse pointer enters it.
24990 Autoselection selects the minibuffer only if it is active, and never
24991 unselects the minibuffer if it is active.
24993 When customizing this variable make sure that the actual value of
24994 `focus-follows-mouse' matches the behavior of your window manager. */);
24995 Vmouse_autoselect_window = Qnil;
24997 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24998 doc: /* *Non-nil means automatically resize tool-bars.
24999 This dynamically changes the tool-bar's height to the minimum height
25000 that is needed to make all tool-bar items visible.
25001 If value is `grow-only', the tool-bar's height is only increased
25002 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25003 Vauto_resize_tool_bars = Qt;
25005 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25006 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25007 auto_raise_tool_bar_buttons_p = 1;
25009 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25010 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25011 make_cursor_line_fully_visible_p = 1;
25013 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25014 doc: /* *Border below tool-bar in pixels.
25015 If an integer, use it as the height of the border.
25016 If it is one of `internal-border-width' or `border-width', use the
25017 value of the corresponding frame parameter.
25018 Otherwise, no border is added below the tool-bar. */);
25019 Vtool_bar_border = Qinternal_border_width;
25021 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25022 doc: /* *Margin around tool-bar buttons in pixels.
25023 If an integer, use that for both horizontal and vertical margins.
25024 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25025 HORZ specifying the horizontal margin, and VERT specifying the
25026 vertical margin. */);
25027 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25029 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25030 doc: /* *Relief thickness of tool-bar buttons. */);
25031 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25033 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25034 doc: /* List of functions to call to fontify regions of text.
25035 Each function is called with one argument POS. Functions must
25036 fontify a region starting at POS in the current buffer, and give
25037 fontified regions the property `fontified'. */);
25038 Vfontification_functions = Qnil;
25039 Fmake_variable_buffer_local (Qfontification_functions);
25041 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25042 &unibyte_display_via_language_environment,
25043 doc: /* *Non-nil means display unibyte text according to language environment.
25044 Specifically this means that unibyte non-ASCII characters
25045 are displayed by converting them to the equivalent multibyte characters
25046 according to the current language environment. As a result, they are
25047 displayed according to the current fontset. */);
25048 unibyte_display_via_language_environment = 0;
25050 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25051 doc: /* *Maximum height for resizing mini-windows.
25052 If a float, it specifies a fraction of the mini-window frame's height.
25053 If an integer, it specifies a number of lines. */);
25054 Vmax_mini_window_height = make_float (0.25);
25056 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25057 doc: /* *How to resize mini-windows.
25058 A value of nil means don't automatically resize mini-windows.
25059 A value of t means resize them to fit the text displayed in them.
25060 A value of `grow-only', the default, means let mini-windows grow
25061 only, until their display becomes empty, at which point the windows
25062 go back to their normal size. */);
25063 Vresize_mini_windows = Qgrow_only;
25065 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25066 doc: /* Alist specifying how to blink the cursor off.
25067 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25068 `cursor-type' frame-parameter or variable equals ON-STATE,
25069 comparing using `equal', Emacs uses OFF-STATE to specify
25070 how to blink it off. ON-STATE and OFF-STATE are values for
25071 the `cursor-type' frame parameter.
25073 If a frame's ON-STATE has no entry in this list,
25074 the frame's other specifications determine how to blink the cursor off. */);
25075 Vblink_cursor_alist = Qnil;
25077 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25078 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25079 automatic_hscrolling_p = 1;
25080 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25081 staticpro (&Qauto_hscroll_mode);
25083 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25084 doc: /* *How many columns away from the window edge point is allowed to get
25085 before automatic hscrolling will horizontally scroll the window. */);
25086 hscroll_margin = 5;
25088 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25089 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25090 When point is less than `hscroll-margin' columns from the window
25091 edge, automatic hscrolling will scroll the window by the amount of columns
25092 determined by this variable. If its value is a positive integer, scroll that
25093 many columns. If it's a positive floating-point number, it specifies the
25094 fraction of the window's width to scroll. If it's nil or zero, point will be
25095 centered horizontally after the scroll. Any other value, including negative
25096 numbers, are treated as if the value were zero.
25098 Automatic hscrolling always moves point outside the scroll margin, so if
25099 point was more than scroll step columns inside the margin, the window will
25100 scroll more than the value given by the scroll step.
25102 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25103 and `scroll-right' overrides this variable's effect. */);
25104 Vhscroll_step = make_number (0);
25106 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25107 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25108 Bind this around calls to `message' to let it take effect. */);
25109 message_truncate_lines = 0;
25111 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25112 doc: /* Normal hook run to update the menu bar definitions.
25113 Redisplay runs this hook before it redisplays the menu bar.
25114 This is used to update submenus such as Buffers,
25115 whose contents depend on various data. */);
25116 Vmenu_bar_update_hook = Qnil;
25118 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25119 doc: /* Frame for which we are updating a menu.
25120 The enable predicate for a menu binding should check this variable. */);
25121 Vmenu_updating_frame = Qnil;
25123 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25124 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25125 inhibit_menubar_update = 0;
25127 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25128 doc: /* Prefix prepended to all continuation lines at display time.
25129 The value may be a string, an image, or a stretch-glyph; it is
25130 interpreted in the same way as the value of a `display' text property.
25132 This variable is overridden by any `wrap-prefix' text or overlay
25133 property.
25135 To add a prefix to non-continuation lines, use `line-prefix'. */);
25136 Vwrap_prefix = Qnil;
25137 staticpro (&Qwrap_prefix);
25138 Qwrap_prefix = intern ("wrap-prefix");
25139 Fmake_variable_buffer_local (Qwrap_prefix);
25141 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25142 doc: /* Prefix prepended to all non-continuation lines at display time.
25143 The value may be a string, an image, or a stretch-glyph; it is
25144 interpreted in the same way as the value of a `display' text property.
25146 This variable is overridden by any `line-prefix' text or overlay
25147 property.
25149 To add a prefix to continuation lines, use `wrap-prefix'. */);
25150 Vline_prefix = Qnil;
25151 staticpro (&Qline_prefix);
25152 Qline_prefix = intern ("line-prefix");
25153 Fmake_variable_buffer_local (Qline_prefix);
25155 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25156 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25157 inhibit_eval_during_redisplay = 0;
25159 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25160 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25161 inhibit_free_realized_faces = 0;
25163 #if GLYPH_DEBUG
25164 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25165 doc: /* Inhibit try_window_id display optimization. */);
25166 inhibit_try_window_id = 0;
25168 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25169 doc: /* Inhibit try_window_reusing display optimization. */);
25170 inhibit_try_window_reusing = 0;
25172 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25173 doc: /* Inhibit try_cursor_movement display optimization. */);
25174 inhibit_try_cursor_movement = 0;
25175 #endif /* GLYPH_DEBUG */
25177 DEFVAR_INT ("overline-margin", &overline_margin,
25178 doc: /* *Space between overline and text, in pixels.
25179 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25180 margin to the caracter height. */);
25181 overline_margin = 2;
25183 DEFVAR_INT ("underline-minimum-offset",
25184 &underline_minimum_offset,
25185 doc: /* Minimum distance between baseline and underline.
25186 This can improve legibility of underlined text at small font sizes,
25187 particularly when using variable `x-use-underline-position-properties'
25188 with fonts that specify an UNDERLINE_POSITION relatively close to the
25189 baseline. The default value is 1. */);
25190 underline_minimum_offset = 1;
25192 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25193 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25194 display_hourglass_p = 1;
25196 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25197 doc: /* *Seconds to wait before displaying an hourglass pointer.
25198 Value must be an integer or float. */);
25199 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25201 hourglass_atimer = NULL;
25202 hourglass_shown_p = 0;
25206 /* Initialize this module when Emacs starts. */
25208 void
25209 init_xdisp ()
25211 Lisp_Object root_window;
25212 struct window *mini_w;
25214 current_header_line_height = current_mode_line_height = -1;
25216 CHARPOS (this_line_start_pos) = 0;
25218 mini_w = XWINDOW (minibuf_window);
25219 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25221 if (!noninteractive)
25223 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25224 int i;
25226 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25227 set_window_height (root_window,
25228 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25230 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25231 set_window_height (minibuf_window, 1, 0);
25233 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25234 mini_w->total_cols = make_number (FRAME_COLS (f));
25236 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25237 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25238 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25240 /* The default ellipsis glyphs `...'. */
25241 for (i = 0; i < 3; ++i)
25242 default_invis_vector[i] = make_number ('.');
25246 /* Allocate the buffer for frame titles.
25247 Also used for `format-mode-line'. */
25248 int size = 100;
25249 mode_line_noprop_buf = (char *) xmalloc (size);
25250 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25251 mode_line_noprop_ptr = mode_line_noprop_buf;
25252 mode_line_target = MODE_LINE_DISPLAY;
25255 help_echo_showing_p = 0;
25258 /* Since w32 does not support atimers, it defines its own implementation of
25259 the following three functions in w32fns.c. */
25260 #ifndef WINDOWSNT
25262 /* Platform-independent portion of hourglass implementation. */
25264 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25266 hourglass_started ()
25268 return hourglass_shown_p || hourglass_atimer != NULL;
25271 /* Cancel a currently active hourglass timer, and start a new one. */
25272 void
25273 start_hourglass ()
25275 #if defined (HAVE_WINDOW_SYSTEM)
25276 EMACS_TIME delay;
25277 int secs, usecs = 0;
25279 cancel_hourglass ();
25281 if (INTEGERP (Vhourglass_delay)
25282 && XINT (Vhourglass_delay) > 0)
25283 secs = XFASTINT (Vhourglass_delay);
25284 else if (FLOATP (Vhourglass_delay)
25285 && XFLOAT_DATA (Vhourglass_delay) > 0)
25287 Lisp_Object tem;
25288 tem = Ftruncate (Vhourglass_delay, Qnil);
25289 secs = XFASTINT (tem);
25290 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25292 else
25293 secs = DEFAULT_HOURGLASS_DELAY;
25295 EMACS_SET_SECS_USECS (delay, secs, usecs);
25296 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25297 show_hourglass, NULL);
25298 #endif
25302 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25303 shown. */
25304 void
25305 cancel_hourglass ()
25307 #if defined (HAVE_WINDOW_SYSTEM)
25308 if (hourglass_atimer)
25310 cancel_atimer (hourglass_atimer);
25311 hourglass_atimer = NULL;
25314 if (hourglass_shown_p)
25315 hide_hourglass ();
25316 #endif
25318 #endif /* ! WINDOWSNT */
25320 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25321 (do not change this comment) */