(calc-set-language, calc-edit-finish)
[emacs.git] / src / xdisp.c
blobeb0fda1fa9114dda476cf50257f8cce4d7f1e088
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 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, or (at your option)
11 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; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "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 "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
289 If value is `grow-only', only make tool-bar bigger. */
291 Lisp_Object Vauto_resize_tool_bars;
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
297 int x_stretch_cursor_p;
299 /* Non-nil means don't actually do any redisplay. */
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
305 int inhibit_eval_during_redisplay;
307 /* Names of text properties relevant for redisplay. */
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
312 /* Symbols used in text property values. */
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
326 /* Non-nil means highlight trailing whitespace. */
328 Lisp_Object Vshow_trailing_whitespace;
330 /* Non-nil means escape non-break space and hyphens. */
332 Lisp_Object Vnobreak_char_display;
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
346 #endif /* HAVE_WINDOW_SYSTEM */
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
352 Lisp_Object Vvoid_text_area_pointer;
354 /* Name of the face used to highlight trailing whitespace. */
356 Lisp_Object Qtrailing_whitespace;
358 /* Name and number of the face used to highlight escape glyphs. */
360 Lisp_Object Qescape_glyph;
362 /* Name and number of the face used to highlight non-breaking spaces. */
364 Lisp_Object Qnobreak_space;
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
369 Lisp_Object Qimage;
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
378 int noninteractive_need_newline;
380 /* Non-zero means print newline to message log before next message. */
382 static int message_log_need_newline;
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
396 static struct text_pos this_line_start_pos;
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
401 static struct text_pos this_line_end_pos;
403 /* The vertical positions and the height of this line. */
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
412 static int this_line_start_x;
414 /* Buffer that this_line_.* variables are referring to. */
416 static struct buffer *this_line_buffer;
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
421 int truncate_partial_width_windows;
423 /* A flag to control how to display unibyte 8-bit character. */
425 int unibyte_display_via_language_environment;
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
431 int multiple_frames;
433 Lisp_Object Vglobal_mode_string;
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
440 Lisp_Object Voverlay_arrow_variable_list;
442 /* Marker for where to display an arrow on top of the buffer text. */
444 Lisp_Object Voverlay_arrow_position;
446 /* String to display for the arrow. Only used on terminal frames. */
448 Lisp_Object Voverlay_arrow_string;
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
462 /* Like mode-line-format, but for the title bar on a visible frame. */
464 Lisp_Object Vframe_title_format;
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
468 Lisp_Object Vicon_title_format;
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
474 static Lisp_Object Vwindow_size_change_functions;
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
478 /* Nonzero if an overlay arrow has been displayed in this window. */
480 static int overlay_arrow_seen;
482 /* Nonzero means highlight the region even in nonselected windows. */
484 int highlight_nonselected_windows;
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
489 static EMACS_INT scroll_step;
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
494 static EMACS_INT scroll_conservatively;
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
501 EMACS_INT scroll_margin;
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
507 int buffer_shared;
509 /* Vector containing glyphs for an ellipsis `...'. */
511 static Lisp_Object default_invis_vector[3];
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
517 This variable is deprecated. */
519 int mode_line_inverse_video;
521 /* Prompt to display in front of the mini-buffer contents. */
523 Lisp_Object minibuf_prompt;
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
528 int minibuf_prompt_width;
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
534 Lisp_Object echo_area_window;
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
541 Lisp_Object Vmessage_stack;
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
546 int message_enable_multibyte;
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
550 int update_mode_lines;
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
555 int windows_or_buffers_changed;
557 /* Nonzero means a frame's cursor type has been changed. */
559 int cursor_type_changed;
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
564 int line_number_displayed;
566 /* Maximum buffer size for which to display line numbers. */
568 Lisp_Object Vline_number_display_limit;
570 /* Line width to consider when repositioning for line number display. */
572 static EMACS_INT line_number_display_limit_width;
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
577 Lisp_Object Vmessage_log_max;
579 /* The name of the *Messages* buffer, a string. */
581 static Lisp_Object Vmessages_buffer_name;
583 /* Current, index 0, and last displayed echo area message. Either
584 buffers from echo_buffers, or nil to indicate no message. */
586 Lisp_Object echo_area_buffer[2];
588 /* The buffers referenced from echo_area_buffer. */
590 static Lisp_Object echo_buffer[2];
592 /* A vector saved used in with_area_buffer to reduce consing. */
594 static Lisp_Object Vwith_echo_area_save_vector;
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
599 static int display_last_displayed_message_p;
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
604 int message_buf_print;
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
611 /* When evaluating expressions from menu bar items (enable conditions,
612 for instance), this is the frame they are being processed for. */
614 Lisp_Object Vmenu_updating_frame;
616 /* Maximum height for resizing mini-windows. Either a float
617 specifying a fraction of the available height, or an integer
618 specifying a number of lines. */
620 Lisp_Object Vmax_mini_window_height;
622 /* Non-zero means messages should be displayed with truncated
623 lines instead of being continued. */
625 int message_truncate_lines;
626 Lisp_Object Qmessage_truncate_lines;
628 /* Set to 1 in clear_message to make redisplay_internal aware
629 of an emptied echo area. */
631 static int message_cleared_p;
633 /* How to blink the default frame cursor off. */
634 Lisp_Object Vblink_cursor_alist;
636 /* A scratch glyph row with contents used for generating truncation
637 glyphs. Also used in direct_output_for_insert. */
639 #define MAX_SCRATCH_GLYPHS 100
640 struct glyph_row scratch_glyph_row;
641 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
643 /* Ascent and height of the last line processed by move_it_to. */
645 static int last_max_ascent, last_height;
647 /* Non-zero if there's a help-echo in the echo area. */
649 int help_echo_showing_p;
651 /* If >= 0, computed, exact values of mode-line and header-line height
652 to use in the macros CURRENT_MODE_LINE_HEIGHT and
653 CURRENT_HEADER_LINE_HEIGHT. */
655 int current_mode_line_height, current_header_line_height;
657 /* The maximum distance to look ahead for text properties. Values
658 that are too small let us call compute_char_face and similar
659 functions too often which is expensive. Values that are too large
660 let us call compute_char_face and alike too often because we
661 might not be interested in text properties that far away. */
663 #define TEXT_PROP_DISTANCE_LIMIT 100
665 #if GLYPH_DEBUG
667 /* Variables to turn off display optimizations from Lisp. */
669 int inhibit_try_window_id, inhibit_try_window_reusing;
670 int inhibit_try_cursor_movement;
672 /* Non-zero means print traces of redisplay if compiled with
673 GLYPH_DEBUG != 0. */
675 int trace_redisplay_p;
677 #endif /* GLYPH_DEBUG */
679 #ifdef DEBUG_TRACE_MOVE
680 /* Non-zero means trace with TRACE_MOVE to stderr. */
681 int trace_move;
683 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
684 #else
685 #define TRACE_MOVE(x) (void) 0
686 #endif
688 /* Non-zero means automatically scroll windows horizontally to make
689 point visible. */
691 int automatic_hscrolling_p;
693 /* How close to the margin can point get before the window is scrolled
694 horizontally. */
695 EMACS_INT hscroll_margin;
697 /* How much to scroll horizontally when point is inside the above margin. */
698 Lisp_Object Vhscroll_step;
700 /* The variable `resize-mini-windows'. If nil, don't resize
701 mini-windows. If t, always resize them to fit the text they
702 display. If `grow-only', let mini-windows grow only until they
703 become empty. */
705 Lisp_Object Vresize_mini_windows;
707 /* Buffer being redisplayed -- for redisplay_window_error. */
709 struct buffer *displayed_buffer;
711 /* Space between overline and text. */
713 EMACS_INT overline_margin;
715 /* Value returned from text property handlers (see below). */
717 enum prop_handled
719 HANDLED_NORMALLY,
720 HANDLED_RECOMPUTE_PROPS,
721 HANDLED_OVERLAY_STRING_CONSUMED,
722 HANDLED_RETURN
725 /* A description of text properties that redisplay is interested
726 in. */
728 struct props
730 /* The name of the property. */
731 Lisp_Object *name;
733 /* A unique index for the property. */
734 enum prop_idx idx;
736 /* A handler function called to set up iterator IT from the property
737 at IT's current position. Value is used to steer handle_stop. */
738 enum prop_handled (*handler) P_ ((struct it *it));
741 static enum prop_handled handle_face_prop P_ ((struct it *));
742 static enum prop_handled handle_invisible_prop P_ ((struct it *));
743 static enum prop_handled handle_display_prop P_ ((struct it *));
744 static enum prop_handled handle_composition_prop P_ ((struct it *));
745 static enum prop_handled handle_overlay_change P_ ((struct it *));
746 static enum prop_handled handle_fontified_prop P_ ((struct it *));
748 /* Properties handled by iterators. */
750 static struct props it_props[] =
752 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
753 /* Handle `face' before `display' because some sub-properties of
754 `display' need to know the face. */
755 {&Qface, FACE_PROP_IDX, handle_face_prop},
756 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
757 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
758 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
759 {NULL, 0, NULL}
762 /* Value is the position described by X. If X is a marker, value is
763 the marker_position of X. Otherwise, value is X. */
765 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
767 /* Enumeration returned by some move_it_.* functions internally. */
769 enum move_it_result
771 /* Not used. Undefined value. */
772 MOVE_UNDEFINED,
774 /* Move ended at the requested buffer position or ZV. */
775 MOVE_POS_MATCH_OR_ZV,
777 /* Move ended at the requested X pixel position. */
778 MOVE_X_REACHED,
780 /* Move within a line ended at the end of a line that must be
781 continued. */
782 MOVE_LINE_CONTINUED,
784 /* Move within a line ended at the end of a line that would
785 be displayed truncated. */
786 MOVE_LINE_TRUNCATED,
788 /* Move within a line ended at a line end. */
789 MOVE_NEWLINE_OR_CR
792 /* This counter is used to clear the face cache every once in a while
793 in redisplay_internal. It is incremented for each redisplay.
794 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
795 cleared. */
797 #define CLEAR_FACE_CACHE_COUNT 500
798 static int clear_face_cache_count;
800 /* Similarly for the image cache. */
802 #ifdef HAVE_WINDOW_SYSTEM
803 #define CLEAR_IMAGE_CACHE_COUNT 101
804 static int clear_image_cache_count;
805 #endif
807 /* Non-zero while redisplay_internal is in progress. */
809 int redisplaying_p;
811 /* Non-zero means don't free realized faces. Bound while freeing
812 realized faces is dangerous because glyph matrices might still
813 reference them. */
815 int inhibit_free_realized_faces;
816 Lisp_Object Qinhibit_free_realized_faces;
818 /* If a string, XTread_socket generates an event to display that string.
819 (The display is done in read_char.) */
821 Lisp_Object help_echo_string;
822 Lisp_Object help_echo_window;
823 Lisp_Object help_echo_object;
824 int help_echo_pos;
826 /* Temporary variable for XTread_socket. */
828 Lisp_Object previous_help_echo_string;
830 /* Null glyph slice */
832 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
835 /* Function prototypes. */
837 static void setup_for_ellipsis P_ ((struct it *, int));
838 static void mark_window_display_accurate_1 P_ ((struct window *, int));
839 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
840 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
841 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
842 static int redisplay_mode_lines P_ ((Lisp_Object, int));
843 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
845 #if 0
846 static int invisible_text_between_p P_ ((struct it *, int, int));
847 #endif
849 static void pint2str P_ ((char *, int, int));
850 static void pint2hrstr P_ ((char *, int, int));
851 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
852 struct text_pos));
853 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
854 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
855 static void store_mode_line_noprop_char P_ ((char));
856 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
857 static void x_consider_frame_title P_ ((Lisp_Object));
858 static void handle_stop P_ ((struct it *));
859 static int tool_bar_lines_needed P_ ((struct frame *, int *));
860 static int single_display_spec_intangible_p P_ ((Lisp_Object));
861 static void ensure_echo_area_buffers P_ ((void));
862 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
863 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
864 static int with_echo_area_buffer P_ ((struct window *, int,
865 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
866 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static void clear_garbaged_frames P_ ((void));
868 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int display_echo_area P_ ((struct window *));
872 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
875 static int string_char_and_length P_ ((const unsigned char *, int, int *));
876 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
877 struct text_pos));
878 static int compute_window_start_on_continuation_line P_ ((struct window *));
879 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
880 static void insert_left_trunc_glyphs P_ ((struct it *));
881 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
882 Lisp_Object));
883 static void extend_face_to_end_of_line P_ ((struct it *));
884 static int append_space_for_newline P_ ((struct it *, int));
885 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
886 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
887 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
888 static int trailing_whitespace_p P_ ((int));
889 static int message_log_check_duplicate P_ ((int, int, int, int));
890 static void push_it P_ ((struct it *));
891 static void pop_it P_ ((struct it *));
892 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
893 static void select_frame_for_redisplay P_ ((Lisp_Object));
894 static void redisplay_internal P_ ((int));
895 static int echo_area_display P_ ((int));
896 static void redisplay_windows P_ ((Lisp_Object));
897 static void redisplay_window P_ ((Lisp_Object, int));
898 static Lisp_Object redisplay_window_error ();
899 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
900 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
901 static int update_menu_bar P_ ((struct frame *, int, int));
902 static int try_window_reusing_current_matrix P_ ((struct window *));
903 static int try_window_id P_ ((struct window *));
904 static int display_line P_ ((struct it *));
905 static int display_mode_lines P_ ((struct window *));
906 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
907 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
908 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
909 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
910 static void display_menu_bar P_ ((struct window *));
911 static int display_count_lines P_ ((int, int, int, int, int *));
912 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
913 int, int, struct it *, int, int, int, int));
914 static void compute_line_metrics P_ ((struct it *));
915 static void run_redisplay_end_trigger_hook P_ ((struct it *));
916 static int get_overlay_strings P_ ((struct it *, int));
917 static int get_overlay_strings_1 P_ ((struct it *, int, int));
918 static void next_overlay_string P_ ((struct it *));
919 static void reseat P_ ((struct it *, struct text_pos, int));
920 static void reseat_1 P_ ((struct it *, struct text_pos, int));
921 static void back_to_previous_visible_line_start P_ ((struct it *));
922 void reseat_at_previous_visible_line_start P_ ((struct it *));
923 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
924 static int next_element_from_ellipsis P_ ((struct it *));
925 static int next_element_from_display_vector P_ ((struct it *));
926 static int next_element_from_string P_ ((struct it *));
927 static int next_element_from_c_string P_ ((struct it *));
928 static int next_element_from_buffer P_ ((struct it *));
929 static int next_element_from_composition P_ ((struct it *));
930 static int next_element_from_image P_ ((struct it *));
931 static int next_element_from_stretch P_ ((struct it *));
932 static void load_overlay_strings P_ ((struct it *, int));
933 static int init_from_display_pos P_ ((struct it *, struct window *,
934 struct display_pos *));
935 static void reseat_to_string P_ ((struct it *, unsigned char *,
936 Lisp_Object, int, int, int, int));
937 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
938 int, int, int));
939 void move_it_vertically_backward P_ ((struct it *, int));
940 static void init_to_row_start P_ ((struct it *, struct window *,
941 struct glyph_row *));
942 static int init_to_row_end P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static void back_to_previous_line_start P_ ((struct it *));
945 static int forward_to_next_line_start P_ ((struct it *, int *));
946 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
947 Lisp_Object, int));
948 static struct text_pos string_pos P_ ((int, Lisp_Object));
949 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
950 static int number_of_chars P_ ((unsigned char *, int));
951 static void compute_stop_pos P_ ((struct it *));
952 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
953 Lisp_Object));
954 static int face_before_or_after_it_pos P_ ((struct it *, int));
955 static int next_overlay_change P_ ((int));
956 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
957 Lisp_Object, Lisp_Object,
958 struct text_pos *, int));
959 static int underlying_face_id P_ ((struct it *));
960 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
961 struct window *));
963 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
964 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
966 #ifdef HAVE_WINDOW_SYSTEM
968 static void update_tool_bar P_ ((struct frame *, int));
969 static void build_desired_tool_bar_string P_ ((struct frame *f));
970 static int redisplay_tool_bar P_ ((struct frame *));
971 static void display_tool_bar_line P_ ((struct it *, int));
972 static void notice_overwritten_cursor P_ ((struct window *,
973 enum glyph_row_area,
974 int, int, int, int));
978 #endif /* HAVE_WINDOW_SYSTEM */
981 /***********************************************************************
982 Window display dimensions
983 ***********************************************************************/
985 /* Return the bottom boundary y-position for text lines in window W.
986 This is the first y position at which a line cannot start.
987 It is relative to the top of the window.
989 This is the height of W minus the height of a mode line, if any. */
991 INLINE int
992 window_text_bottom_y (w)
993 struct window *w;
995 int height = WINDOW_TOTAL_HEIGHT (w);
997 if (WINDOW_WANTS_MODELINE_P (w))
998 height -= CURRENT_MODE_LINE_HEIGHT (w);
999 return height;
1002 /* Return the pixel width of display area AREA of window W. AREA < 0
1003 means return the total width of W, not including fringes to
1004 the left and right of the window. */
1006 INLINE int
1007 window_box_width (w, area)
1008 struct window *w;
1009 int area;
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1014 if (!w->pseudo_window_p)
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1018 if (area == TEXT_AREA)
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1026 else if (area == LEFT_MARGIN_AREA)
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1032 else if (area == RIGHT_MARGIN_AREA)
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1047 INLINE int
1048 window_box_height (w)
1049 struct window *w;
1051 struct frame *f = XFRAME (w->frame);
1052 int height = WINDOW_TOTAL_HEIGHT (w);
1054 xassert (height >= 0);
1056 /* Note: the code below that determines the mode-line/header-line
1057 height is essentially the same as that contained in the macro
1058 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1059 the appropriate glyph row has its `mode_line_p' flag set,
1060 and if it doesn't, uses estimate_mode_line_height instead. */
1062 if (WINDOW_WANTS_MODELINE_P (w))
1064 struct glyph_row *ml_row
1065 = (w->current_matrix && w->current_matrix->rows
1066 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1067 : 0);
1068 if (ml_row && ml_row->mode_line_p)
1069 height -= ml_row->height;
1070 else
1071 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1074 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 struct glyph_row *hl_row
1077 = (w->current_matrix && w->current_matrix->rows
1078 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1079 : 0);
1080 if (hl_row && hl_row->mode_line_p)
1081 height -= hl_row->height;
1082 else
1083 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1086 /* With a very small font and a mode-line that's taller than
1087 default, we might end up with a negative height. */
1088 return max (0, height);
1091 /* Return the window-relative coordinate of the left edge of display
1092 area AREA of window W. AREA < 0 means return the left edge of the
1093 whole window, to the right of the left fringe of W. */
1095 INLINE int
1096 window_box_left_offset (w, area)
1097 struct window *w;
1098 int area;
1100 int x;
1102 if (w->pseudo_window_p)
1103 return 0;
1105 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1107 if (area == TEXT_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA));
1110 else if (area == RIGHT_MARGIN_AREA)
1111 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1112 + window_box_width (w, LEFT_MARGIN_AREA)
1113 + window_box_width (w, TEXT_AREA)
1114 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1116 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1117 else if (area == LEFT_MARGIN_AREA
1118 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1119 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1121 return x;
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the left of the right fringe of W. */
1129 INLINE int
1130 window_box_right_offset (w, area)
1131 struct window *w;
1132 int area;
1134 return window_box_left_offset (w, area) + window_box_width (w, area);
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. AREA < 0 means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1141 INLINE int
1142 window_box_left (w, area)
1143 struct window *w;
1144 int area;
1146 struct frame *f = XFRAME (w->frame);
1147 int x;
1149 if (w->pseudo_window_p)
1150 return FRAME_INTERNAL_BORDER_WIDTH (f);
1152 x = (WINDOW_LEFT_EDGE_X (w)
1153 + window_box_left_offset (w, area));
1155 return x;
1159 /* Return the frame-relative coordinate of the right edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the left of the right fringe of W. */
1163 INLINE int
1164 window_box_right (w, area)
1165 struct window *w;
1166 int area;
1168 return window_box_left (w, area) + window_box_width (w, area);
1171 /* Get the bounding box of the display area AREA of window W, without
1172 mode lines, in frame-relative coordinates. AREA < 0 means the
1173 whole window, not including the left and right fringes of
1174 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1175 coordinates of the upper-left corner of the box. Return in
1176 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1178 INLINE void
1179 window_box (w, area, box_x, box_y, box_width, box_height)
1180 struct window *w;
1181 int area;
1182 int *box_x, *box_y, *box_width, *box_height;
1184 if (box_width)
1185 *box_width = window_box_width (w, area);
1186 if (box_height)
1187 *box_height = window_box_height (w);
1188 if (box_x)
1189 *box_x = window_box_left (w, area);
1190 if (box_y)
1192 *box_y = WINDOW_TOP_EDGE_Y (w);
1193 if (WINDOW_WANTS_HEADER_LINE_P (w))
1194 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1199 /* Get the bounding box of the display area AREA of window W, without
1200 mode lines. AREA < 0 means the whole window, not including the
1201 left and right fringe of the window. Return in *TOP_LEFT_X
1202 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1203 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1204 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1205 box. */
1207 INLINE void
1208 window_box_edges (w, area, top_left_x, top_left_y,
1209 bottom_right_x, bottom_right_y)
1210 struct window *w;
1211 int area;
1212 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1214 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1215 bottom_right_y);
1216 *bottom_right_x += *top_left_x;
1217 *bottom_right_y += *top_left_y;
1222 /***********************************************************************
1223 Utilities
1224 ***********************************************************************/
1226 /* Return the bottom y-position of the line the iterator IT is in.
1227 This can modify IT's settings. */
1230 line_bottom_y (it)
1231 struct it *it;
1233 int line_height = it->max_ascent + it->max_descent;
1234 int line_top_y = it->current_y;
1236 if (line_height == 0)
1238 if (last_height)
1239 line_height = last_height;
1240 else if (IT_CHARPOS (*it) < ZV)
1242 move_it_by_lines (it, 1, 1);
1243 line_height = (it->max_ascent || it->max_descent
1244 ? it->max_ascent + it->max_descent
1245 : last_height);
1247 else
1249 struct glyph_row *row = it->glyph_row;
1251 /* Use the default character height. */
1252 it->glyph_row = NULL;
1253 it->what = IT_CHARACTER;
1254 it->c = ' ';
1255 it->len = 1;
1256 PRODUCE_GLYPHS (it);
1257 line_height = it->ascent + it->descent;
1258 it->glyph_row = row;
1262 return line_top_y + line_height;
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1273 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1274 struct window *w;
1275 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1277 struct it it;
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1282 if (noninteractive)
1283 return visible_p;
1285 if (XBUFFER (w->buffer) != current_buffer)
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->buffer));
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293 /* Compute exact mode line heights. */
1294 if (WINDOW_WANTS_MODELINE_P (w))
1295 current_mode_line_height
1296 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1297 current_buffer->mode_line_format);
1299 if (WINDOW_WANTS_HEADER_LINE_P (w))
1300 current_header_line_height
1301 = display_mode_line (w, HEADER_LINE_FACE_ID,
1302 current_buffer->header_line_format);
1304 start_display (&it, w, top);
1305 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1306 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1308 /* Note that we may overshoot because of invisible text. */
1309 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1311 int top_x = it.current_x;
1312 int top_y = it.current_y;
1313 int bottom_y = (last_height = 0, line_bottom_y (&it));
1314 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1316 if (top_y < window_top_y)
1317 visible_p = bottom_y > window_top_y;
1318 else if (top_y < it.last_visible_y)
1319 visible_p = 1;
1320 if (visible_p)
1322 *x = top_x;
1323 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1324 *rtop = max (0, window_top_y - top_y);
1325 *rbot = max (0, bottom_y - it.last_visible_y);
1326 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1327 - max (top_y, window_top_y)));
1328 *vpos = it.vpos;
1331 else
1333 struct it it2;
1335 it2 = it;
1336 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1337 move_it_by_lines (&it, 1, 0);
1338 if (charpos < IT_CHARPOS (it)
1339 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1341 visible_p = 1;
1342 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1343 *x = it2.current_x;
1344 *y = it2.current_y + it2.max_ascent - it2.ascent;
1345 *rtop = max (0, -it2.current_y);
1346 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1347 - it.last_visible_y));
1348 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1349 it.last_visible_y)
1350 - max (it2.current_y,
1351 WINDOW_HEADER_LINE_HEIGHT (w))));
1352 *vpos = it2.vpos;
1356 if (old_buffer)
1357 set_buffer_internal_1 (old_buffer);
1359 current_header_line_height = current_mode_line_height = -1;
1361 if (visible_p && XFASTINT (w->hscroll) > 0)
1362 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1364 #if 0
1365 /* Debugging code. */
1366 if (visible_p)
1367 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1368 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1369 else
1370 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1371 #endif
1373 return visible_p;
1377 /* Return the next character from STR which is MAXLEN bytes long.
1378 Return in *LEN the length of the character. This is like
1379 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1380 we find one, we return a `?', but with the length of the invalid
1381 character. */
1383 static INLINE int
1384 string_char_and_length (str, maxlen, len)
1385 const unsigned char *str;
1386 int maxlen, *len;
1388 int c;
1390 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1391 if (!CHAR_VALID_P (c, 1))
1392 /* We may not change the length here because other places in Emacs
1393 don't use this function, i.e. they silently accept invalid
1394 characters. */
1395 c = '?';
1397 return c;
1402 /* Given a position POS containing a valid character and byte position
1403 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1405 static struct text_pos
1406 string_pos_nchars_ahead (pos, string, nchars)
1407 struct text_pos pos;
1408 Lisp_Object string;
1409 int nchars;
1411 xassert (STRINGP (string) && nchars >= 0);
1413 if (STRING_MULTIBYTE (string))
1415 int rest = SBYTES (string) - BYTEPOS (pos);
1416 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1417 int len;
1419 while (nchars--)
1421 string_char_and_length (p, rest, &len);
1422 p += len, rest -= len;
1423 xassert (rest >= 0);
1424 CHARPOS (pos) += 1;
1425 BYTEPOS (pos) += len;
1428 else
1429 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1431 return pos;
1435 /* Value is the text position, i.e. character and byte position,
1436 for character position CHARPOS in STRING. */
1438 static INLINE struct text_pos
1439 string_pos (charpos, string)
1440 int charpos;
1441 Lisp_Object string;
1443 struct text_pos pos;
1444 xassert (STRINGP (string));
1445 xassert (charpos >= 0);
1446 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1447 return pos;
1451 /* Value is a text position, i.e. character and byte position, for
1452 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1453 means recognize multibyte characters. */
1455 static struct text_pos
1456 c_string_pos (charpos, s, multibyte_p)
1457 int charpos;
1458 unsigned char *s;
1459 int multibyte_p;
1461 struct text_pos pos;
1463 xassert (s != NULL);
1464 xassert (charpos >= 0);
1466 if (multibyte_p)
1468 int rest = strlen (s), len;
1470 SET_TEXT_POS (pos, 0, 0);
1471 while (charpos--)
1473 string_char_and_length (s, rest, &len);
1474 s += len, rest -= len;
1475 xassert (rest >= 0);
1476 CHARPOS (pos) += 1;
1477 BYTEPOS (pos) += len;
1480 else
1481 SET_TEXT_POS (pos, charpos, charpos);
1483 return pos;
1487 /* Value is the number of characters in C string S. MULTIBYTE_P
1488 non-zero means recognize multibyte characters. */
1490 static int
1491 number_of_chars (s, multibyte_p)
1492 unsigned char *s;
1493 int multibyte_p;
1495 int nchars;
1497 if (multibyte_p)
1499 int rest = strlen (s), len;
1500 unsigned char *p = (unsigned char *) s;
1502 for (nchars = 0; rest > 0; ++nchars)
1504 string_char_and_length (p, rest, &len);
1505 rest -= len, p += len;
1508 else
1509 nchars = strlen (s);
1511 return nchars;
1515 /* Compute byte position NEWPOS->bytepos corresponding to
1516 NEWPOS->charpos. POS is a known position in string STRING.
1517 NEWPOS->charpos must be >= POS.charpos. */
1519 static void
1520 compute_string_pos (newpos, pos, string)
1521 struct text_pos *newpos, pos;
1522 Lisp_Object string;
1524 xassert (STRINGP (string));
1525 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1527 if (STRING_MULTIBYTE (string))
1528 *newpos = string_pos_nchars_ahead (pos, string,
1529 CHARPOS (*newpos) - CHARPOS (pos));
1530 else
1531 BYTEPOS (*newpos) = CHARPOS (*newpos);
1534 /* EXPORT:
1535 Return an estimation of the pixel height of mode or top lines on
1536 frame F. FACE_ID specifies what line's height to estimate. */
1539 estimate_mode_line_height (f, face_id)
1540 struct frame *f;
1541 enum face_id face_id;
1543 #ifdef HAVE_WINDOW_SYSTEM
1544 if (FRAME_WINDOW_P (f))
1546 int height = FONT_HEIGHT (FRAME_FONT (f));
1548 /* This function is called so early when Emacs starts that the face
1549 cache and mode line face are not yet initialized. */
1550 if (FRAME_FACE_CACHE (f))
1552 struct face *face = FACE_FROM_ID (f, face_id);
1553 if (face)
1555 if (face->font)
1556 height = FONT_HEIGHT (face->font);
1557 if (face->box_line_width > 0)
1558 height += 2 * face->box_line_width;
1562 return height;
1564 #endif
1566 return 1;
1569 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1570 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1571 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1572 not force the value into range. */
1574 void
1575 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1576 FRAME_PTR f;
1577 register int pix_x, pix_y;
1578 int *x, *y;
1579 NativeRectangle *bounds;
1580 int noclip;
1583 #ifdef HAVE_WINDOW_SYSTEM
1584 if (FRAME_WINDOW_P (f))
1586 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1587 even for negative values. */
1588 if (pix_x < 0)
1589 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1590 if (pix_y < 0)
1591 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1593 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1594 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1596 if (bounds)
1597 STORE_NATIVE_RECT (*bounds,
1598 FRAME_COL_TO_PIXEL_X (f, pix_x),
1599 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1600 FRAME_COLUMN_WIDTH (f) - 1,
1601 FRAME_LINE_HEIGHT (f) - 1);
1603 if (!noclip)
1605 if (pix_x < 0)
1606 pix_x = 0;
1607 else if (pix_x > FRAME_TOTAL_COLS (f))
1608 pix_x = FRAME_TOTAL_COLS (f);
1610 if (pix_y < 0)
1611 pix_y = 0;
1612 else if (pix_y > FRAME_LINES (f))
1613 pix_y = FRAME_LINES (f);
1616 #endif
1618 *x = pix_x;
1619 *y = pix_y;
1623 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1624 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1625 can't tell the positions because W's display is not up to date,
1626 return 0. */
1629 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1630 struct window *w;
1631 int hpos, vpos;
1632 int *frame_x, *frame_y;
1634 #ifdef HAVE_WINDOW_SYSTEM
1635 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1637 int success_p;
1639 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1640 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1642 if (display_completed)
1644 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1645 struct glyph *glyph = row->glyphs[TEXT_AREA];
1646 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1648 hpos = row->x;
1649 vpos = row->y;
1650 while (glyph < end)
1652 hpos += glyph->pixel_width;
1653 ++glyph;
1656 /* If first glyph is partially visible, its first visible position is still 0. */
1657 if (hpos < 0)
1658 hpos = 0;
1660 success_p = 1;
1662 else
1664 hpos = vpos = 0;
1665 success_p = 0;
1668 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1669 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1670 return success_p;
1672 #endif
1674 *frame_x = hpos;
1675 *frame_y = vpos;
1676 return 1;
1680 #ifdef HAVE_WINDOW_SYSTEM
1682 /* Find the glyph under window-relative coordinates X/Y in window W.
1683 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1684 strings. Return in *HPOS and *VPOS the row and column number of
1685 the glyph found. Return in *AREA the glyph area containing X.
1686 Value is a pointer to the glyph found or null if X/Y is not on
1687 text, or we can't tell because W's current matrix is not up to
1688 date. */
1690 static struct glyph *
1691 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1692 struct window *w;
1693 int x, y;
1694 int *hpos, *vpos, *dx, *dy, *area;
1696 struct glyph *glyph, *end;
1697 struct glyph_row *row = NULL;
1698 int x0, i;
1700 /* Find row containing Y. Give up if some row is not enabled. */
1701 for (i = 0; i < w->current_matrix->nrows; ++i)
1703 row = MATRIX_ROW (w->current_matrix, i);
1704 if (!row->enabled_p)
1705 return NULL;
1706 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1707 break;
1710 *vpos = i;
1711 *hpos = 0;
1713 /* Give up if Y is not in the window. */
1714 if (i == w->current_matrix->nrows)
1715 return NULL;
1717 /* Get the glyph area containing X. */
1718 if (w->pseudo_window_p)
1720 *area = TEXT_AREA;
1721 x0 = 0;
1723 else
1725 if (x < window_box_left_offset (w, TEXT_AREA))
1727 *area = LEFT_MARGIN_AREA;
1728 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1730 else if (x < window_box_right_offset (w, TEXT_AREA))
1732 *area = TEXT_AREA;
1733 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1735 else
1737 *area = RIGHT_MARGIN_AREA;
1738 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1742 /* Find glyph containing X. */
1743 glyph = row->glyphs[*area];
1744 end = glyph + row->used[*area];
1745 x -= x0;
1746 while (glyph < end && x >= glyph->pixel_width)
1748 x -= glyph->pixel_width;
1749 ++glyph;
1752 if (glyph == end)
1753 return NULL;
1755 if (dx)
1757 *dx = x;
1758 *dy = y - (row->y + row->ascent - glyph->ascent);
1761 *hpos = glyph - row->glyphs[*area];
1762 return glyph;
1766 /* EXPORT:
1767 Convert frame-relative x/y to coordinates relative to window W.
1768 Takes pseudo-windows into account. */
1770 void
1771 frame_to_window_pixel_xy (w, x, y)
1772 struct window *w;
1773 int *x, *y;
1775 if (w->pseudo_window_p)
1777 /* A pseudo-window is always full-width, and starts at the
1778 left edge of the frame, plus a frame border. */
1779 struct frame *f = XFRAME (w->frame);
1780 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1781 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1783 else
1785 *x -= WINDOW_LEFT_EDGE_X (w);
1786 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1790 /* EXPORT:
1791 Return in RECTS[] at most N clipping rectangles for glyph string S.
1792 Return the number of stored rectangles. */
1795 get_glyph_string_clip_rects (s, rects, n)
1796 struct glyph_string *s;
1797 NativeRectangle *rects;
1798 int n;
1800 XRectangle r;
1802 if (n <= 0)
1803 return 0;
1805 if (s->row->full_width_p)
1807 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1808 r.x = WINDOW_LEFT_EDGE_X (s->w);
1809 r.width = WINDOW_TOTAL_WIDTH (s->w);
1811 /* Unless displaying a mode or menu bar line, which are always
1812 fully visible, clip to the visible part of the row. */
1813 if (s->w->pseudo_window_p)
1814 r.height = s->row->visible_height;
1815 else
1816 r.height = s->height;
1818 else
1820 /* This is a text line that may be partially visible. */
1821 r.x = window_box_left (s->w, s->area);
1822 r.width = window_box_width (s->w, s->area);
1823 r.height = s->row->visible_height;
1826 if (s->clip_head)
1827 if (r.x < s->clip_head->x)
1829 if (r.width >= s->clip_head->x - r.x)
1830 r.width -= s->clip_head->x - r.x;
1831 else
1832 r.width = 0;
1833 r.x = s->clip_head->x;
1835 if (s->clip_tail)
1836 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1838 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1839 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1840 else
1841 r.width = 0;
1844 /* If S draws overlapping rows, it's sufficient to use the top and
1845 bottom of the window for clipping because this glyph string
1846 intentionally draws over other lines. */
1847 if (s->for_overlaps)
1849 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1850 r.height = window_text_bottom_y (s->w) - r.y;
1852 /* Alas, the above simple strategy does not work for the
1853 environments with anti-aliased text: if the same text is
1854 drawn onto the same place multiple times, it gets thicker.
1855 If the overlap we are processing is for the erased cursor, we
1856 take the intersection with the rectagle of the cursor. */
1857 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1859 XRectangle rc, r_save = r;
1861 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1862 rc.y = s->w->phys_cursor.y;
1863 rc.width = s->w->phys_cursor_width;
1864 rc.height = s->w->phys_cursor_height;
1866 x_intersect_rectangles (&r_save, &rc, &r);
1869 else
1871 /* Don't use S->y for clipping because it doesn't take partially
1872 visible lines into account. For example, it can be negative for
1873 partially visible lines at the top of a window. */
1874 if (!s->row->full_width_p
1875 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1876 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1877 else
1878 r.y = max (0, s->row->y);
1880 /* If drawing a tool-bar window, draw it over the internal border
1881 at the top of the window. */
1882 if (WINDOWP (s->f->tool_bar_window)
1883 && s->w == XWINDOW (s->f->tool_bar_window))
1884 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1887 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1889 /* If drawing the cursor, don't let glyph draw outside its
1890 advertised boundaries. Cleartype does this under some circumstances. */
1891 if (s->hl == DRAW_CURSOR)
1893 struct glyph *glyph = s->first_glyph;
1894 int height, max_y;
1896 if (s->x > r.x)
1898 r.width -= s->x - r.x;
1899 r.x = s->x;
1901 r.width = min (r.width, glyph->pixel_width);
1903 /* If r.y is below window bottom, ensure that we still see a cursor. */
1904 height = min (glyph->ascent + glyph->descent,
1905 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1906 max_y = window_text_bottom_y (s->w) - height;
1907 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1908 if (s->ybase - glyph->ascent > max_y)
1910 r.y = max_y;
1911 r.height = height;
1913 else
1915 /* Don't draw cursor glyph taller than our actual glyph. */
1916 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1917 if (height < r.height)
1919 max_y = r.y + r.height;
1920 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1921 r.height = min (max_y - r.y, height);
1926 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1927 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1929 #ifdef CONVERT_FROM_XRECT
1930 CONVERT_FROM_XRECT (r, *rects);
1931 #else
1932 *rects = r;
1933 #endif
1934 return 1;
1936 else
1938 /* If we are processing overlapping and allowed to return
1939 multiple clipping rectangles, we exclude the row of the glyph
1940 string from the clipping rectangle. This is to avoid drawing
1941 the same text on the environment with anti-aliasing. */
1942 #ifdef CONVERT_FROM_XRECT
1943 XRectangle rs[2];
1944 #else
1945 XRectangle *rs = rects;
1946 #endif
1947 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1949 if (s->for_overlaps & OVERLAPS_PRED)
1951 rs[i] = r;
1952 if (r.y + r.height > row_y)
1954 if (r.y < row_y)
1955 rs[i].height = row_y - r.y;
1956 else
1957 rs[i].height = 0;
1959 i++;
1961 if (s->for_overlaps & OVERLAPS_SUCC)
1963 rs[i] = r;
1964 if (r.y < row_y + s->row->visible_height)
1966 if (r.y + r.height > row_y + s->row->visible_height)
1968 rs[i].y = row_y + s->row->visible_height;
1969 rs[i].height = r.y + r.height - rs[i].y;
1971 else
1972 rs[i].height = 0;
1974 i++;
1977 n = i;
1978 #ifdef CONVERT_FROM_XRECT
1979 for (i = 0; i < n; i++)
1980 CONVERT_FROM_XRECT (rs[i], rects[i]);
1981 #endif
1982 return n;
1986 /* EXPORT:
1987 Return in *NR the clipping rectangle for glyph string S. */
1989 void
1990 get_glyph_string_clip_rect (s, nr)
1991 struct glyph_string *s;
1992 NativeRectangle *nr;
1994 get_glyph_string_clip_rects (s, nr, 1);
1998 /* EXPORT:
1999 Return the position and height of the phys cursor in window W.
2000 Set w->phys_cursor_width to width of phys cursor.
2003 void
2004 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2005 struct window *w;
2006 struct glyph_row *row;
2007 struct glyph *glyph;
2008 int *xp, *yp, *heightp;
2010 struct frame *f = XFRAME (WINDOW_FRAME (w));
2011 int x, y, wd, h, h0, y0;
2013 /* Compute the width of the rectangle to draw. If on a stretch
2014 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2015 rectangle as wide as the glyph, but use a canonical character
2016 width instead. */
2017 wd = glyph->pixel_width - 1;
2018 #ifdef HAVE_NTGUI
2019 wd++; /* Why? */
2020 #endif
2022 x = w->phys_cursor.x;
2023 if (x < 0)
2025 wd += x;
2026 x = 0;
2029 if (glyph->type == STRETCH_GLYPH
2030 && !x_stretch_cursor_p)
2031 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2032 w->phys_cursor_width = wd;
2034 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2036 /* If y is below window bottom, ensure that we still see a cursor. */
2037 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2039 h = max (h0, glyph->ascent + glyph->descent);
2040 h0 = min (h0, glyph->ascent + glyph->descent);
2042 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2043 if (y < y0)
2045 h = max (h - (y0 - y) + 1, h0);
2046 y = y0 - 1;
2048 else
2050 y0 = window_text_bottom_y (w) - h0;
2051 if (y > y0)
2053 h += y - y0;
2054 y = y0;
2058 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2059 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2060 *heightp = h;
2064 * Remember which glyph the mouse is over.
2067 void
2068 remember_mouse_glyph (f, gx, gy, rect)
2069 struct frame *f;
2070 int gx, gy;
2071 NativeRectangle *rect;
2073 Lisp_Object window;
2074 struct window *w;
2075 struct glyph_row *r, *gr, *end_row;
2076 enum window_part part;
2077 enum glyph_row_area area;
2078 int x, y, width, height;
2080 /* Try to determine frame pixel position and size of the glyph under
2081 frame pixel coordinates X/Y on frame F. */
2083 if (!f->glyphs_initialized_p
2084 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2085 NILP (window)))
2087 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2088 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2089 goto virtual_glyph;
2092 w = XWINDOW (window);
2093 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2094 height = WINDOW_FRAME_LINE_HEIGHT (w);
2096 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2097 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2099 if (w->pseudo_window_p)
2101 area = TEXT_AREA;
2102 part = ON_MODE_LINE; /* Don't adjust margin. */
2103 goto text_glyph;
2106 switch (part)
2108 case ON_LEFT_MARGIN:
2109 area = LEFT_MARGIN_AREA;
2110 goto text_glyph;
2112 case ON_RIGHT_MARGIN:
2113 area = RIGHT_MARGIN_AREA;
2114 goto text_glyph;
2116 case ON_HEADER_LINE:
2117 case ON_MODE_LINE:
2118 gr = (part == ON_HEADER_LINE
2119 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2120 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2121 gy = gr->y;
2122 area = TEXT_AREA;
2123 goto text_glyph_row_found;
2125 case ON_TEXT:
2126 area = TEXT_AREA;
2128 text_glyph:
2129 gr = 0; gy = 0;
2130 for (; r <= end_row && r->enabled_p; ++r)
2131 if (r->y + r->height > y)
2133 gr = r; gy = r->y;
2134 break;
2137 text_glyph_row_found:
2138 if (gr && gy <= y)
2140 struct glyph *g = gr->glyphs[area];
2141 struct glyph *end = g + gr->used[area];
2143 height = gr->height;
2144 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2145 if (gx + g->pixel_width > x)
2146 break;
2148 if (g < end)
2150 if (g->type == IMAGE_GLYPH)
2152 /* Don't remember when mouse is over image, as
2153 image may have hot-spots. */
2154 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2155 return;
2157 width = g->pixel_width;
2159 else
2161 /* Use nominal char spacing at end of line. */
2162 x -= gx;
2163 gx += (x / width) * width;
2166 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2167 gx += window_box_left_offset (w, area);
2169 else
2171 /* Use nominal line height at end of window. */
2172 gx = (x / width) * width;
2173 y -= gy;
2174 gy += (y / height) * height;
2176 break;
2178 case ON_LEFT_FRINGE:
2179 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2180 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2181 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2182 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2183 goto row_glyph;
2185 case ON_RIGHT_FRINGE:
2186 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2187 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2188 : window_box_right_offset (w, TEXT_AREA));
2189 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2190 goto row_glyph;
2192 case ON_SCROLL_BAR:
2193 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2195 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2196 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2197 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2198 : 0)));
2199 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2201 row_glyph:
2202 gr = 0, gy = 0;
2203 for (; r <= end_row && r->enabled_p; ++r)
2204 if (r->y + r->height > y)
2206 gr = r; gy = r->y;
2207 break;
2210 if (gr && gy <= y)
2211 height = gr->height;
2212 else
2214 /* Use nominal line height at end of window. */
2215 y -= gy;
2216 gy += (y / height) * height;
2218 break;
2220 default:
2222 virtual_glyph:
2223 /* If there is no glyph under the mouse, then we divide the screen
2224 into a grid of the smallest glyph in the frame, and use that
2225 as our "glyph". */
2227 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2228 round down even for negative values. */
2229 if (gx < 0)
2230 gx -= width - 1;
2231 if (gy < 0)
2232 gy -= height - 1;
2234 gx = (gx / width) * width;
2235 gy = (gy / height) * height;
2237 goto store_rect;
2240 gx += WINDOW_LEFT_EDGE_X (w);
2241 gy += WINDOW_TOP_EDGE_Y (w);
2243 store_rect:
2244 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2246 /* Visible feedback for debugging. */
2247 #if 0
2248 #if HAVE_X_WINDOWS
2249 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2250 f->output_data.x->normal_gc,
2251 gx, gy, width, height);
2252 #endif
2253 #endif
2257 #endif /* HAVE_WINDOW_SYSTEM */
2260 /***********************************************************************
2261 Lisp form evaluation
2262 ***********************************************************************/
2264 /* Error handler for safe_eval and safe_call. */
2266 static Lisp_Object
2267 safe_eval_handler (arg)
2268 Lisp_Object arg;
2270 add_to_log ("Error during redisplay: %s", arg, Qnil);
2271 return Qnil;
2275 /* Evaluate SEXPR and return the result, or nil if something went
2276 wrong. Prevent redisplay during the evaluation. */
2278 Lisp_Object
2279 safe_eval (sexpr)
2280 Lisp_Object sexpr;
2282 Lisp_Object val;
2284 if (inhibit_eval_during_redisplay)
2285 val = Qnil;
2286 else
2288 int count = SPECPDL_INDEX ();
2289 struct gcpro gcpro1;
2291 GCPRO1 (sexpr);
2292 specbind (Qinhibit_redisplay, Qt);
2293 /* Use Qt to ensure debugger does not run,
2294 so there is no possibility of wanting to redisplay. */
2295 val = internal_condition_case_1 (Feval, sexpr, Qt,
2296 safe_eval_handler);
2297 UNGCPRO;
2298 val = unbind_to (count, val);
2301 return val;
2305 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2306 Return the result, or nil if something went wrong. Prevent
2307 redisplay during the evaluation. */
2309 Lisp_Object
2310 safe_call (nargs, args)
2311 int nargs;
2312 Lisp_Object *args;
2314 Lisp_Object val;
2316 if (inhibit_eval_during_redisplay)
2317 val = Qnil;
2318 else
2320 int count = SPECPDL_INDEX ();
2321 struct gcpro gcpro1;
2323 GCPRO1 (args[0]);
2324 gcpro1.nvars = nargs;
2325 specbind (Qinhibit_redisplay, Qt);
2326 /* Use Qt to ensure debugger does not run,
2327 so there is no possibility of wanting to redisplay. */
2328 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2329 safe_eval_handler);
2330 UNGCPRO;
2331 val = unbind_to (count, val);
2334 return val;
2338 /* Call function FN with one argument ARG.
2339 Return the result, or nil if something went wrong. */
2341 Lisp_Object
2342 safe_call1 (fn, arg)
2343 Lisp_Object fn, arg;
2345 Lisp_Object args[2];
2346 args[0] = fn;
2347 args[1] = arg;
2348 return safe_call (2, args);
2353 /***********************************************************************
2354 Debugging
2355 ***********************************************************************/
2357 #if 0
2359 /* Define CHECK_IT to perform sanity checks on iterators.
2360 This is for debugging. It is too slow to do unconditionally. */
2362 static void
2363 check_it (it)
2364 struct it *it;
2366 if (it->method == GET_FROM_STRING)
2368 xassert (STRINGP (it->string));
2369 xassert (IT_STRING_CHARPOS (*it) >= 0);
2371 else
2373 xassert (IT_STRING_CHARPOS (*it) < 0);
2374 if (it->method == GET_FROM_BUFFER)
2376 /* Check that character and byte positions agree. */
2377 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2381 if (it->dpvec)
2382 xassert (it->current.dpvec_index >= 0);
2383 else
2384 xassert (it->current.dpvec_index < 0);
2387 #define CHECK_IT(IT) check_it ((IT))
2389 #else /* not 0 */
2391 #define CHECK_IT(IT) (void) 0
2393 #endif /* not 0 */
2396 #if GLYPH_DEBUG
2398 /* Check that the window end of window W is what we expect it
2399 to be---the last row in the current matrix displaying text. */
2401 static void
2402 check_window_end (w)
2403 struct window *w;
2405 if (!MINI_WINDOW_P (w)
2406 && !NILP (w->window_end_valid))
2408 struct glyph_row *row;
2409 xassert ((row = MATRIX_ROW (w->current_matrix,
2410 XFASTINT (w->window_end_vpos)),
2411 !row->enabled_p
2412 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2413 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2417 #define CHECK_WINDOW_END(W) check_window_end ((W))
2419 #else /* not GLYPH_DEBUG */
2421 #define CHECK_WINDOW_END(W) (void) 0
2423 #endif /* not GLYPH_DEBUG */
2427 /***********************************************************************
2428 Iterator initialization
2429 ***********************************************************************/
2431 /* Initialize IT for displaying current_buffer in window W, starting
2432 at character position CHARPOS. CHARPOS < 0 means that no buffer
2433 position is specified which is useful when the iterator is assigned
2434 a position later. BYTEPOS is the byte position corresponding to
2435 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2437 If ROW is not null, calls to produce_glyphs with IT as parameter
2438 will produce glyphs in that row.
2440 BASE_FACE_ID is the id of a base face to use. It must be one of
2441 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2442 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2443 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2445 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2446 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2447 will be initialized to use the corresponding mode line glyph row of
2448 the desired matrix of W. */
2450 void
2451 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2452 struct it *it;
2453 struct window *w;
2454 int charpos, bytepos;
2455 struct glyph_row *row;
2456 enum face_id base_face_id;
2458 int highlight_region_p;
2460 /* Some precondition checks. */
2461 xassert (w != NULL && it != NULL);
2462 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2463 && charpos <= ZV));
2465 /* If face attributes have been changed since the last redisplay,
2466 free realized faces now because they depend on face definitions
2467 that might have changed. Don't free faces while there might be
2468 desired matrices pending which reference these faces. */
2469 if (face_change_count && !inhibit_free_realized_faces)
2471 face_change_count = 0;
2472 free_all_realized_faces (Qnil);
2475 /* Use one of the mode line rows of W's desired matrix if
2476 appropriate. */
2477 if (row == NULL)
2479 if (base_face_id == MODE_LINE_FACE_ID
2480 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2481 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2482 else if (base_face_id == HEADER_LINE_FACE_ID)
2483 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2486 /* Clear IT. */
2487 bzero (it, sizeof *it);
2488 it->current.overlay_string_index = -1;
2489 it->current.dpvec_index = -1;
2490 it->base_face_id = base_face_id;
2491 it->string = Qnil;
2492 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2494 /* The window in which we iterate over current_buffer: */
2495 XSETWINDOW (it->window, w);
2496 it->w = w;
2497 it->f = XFRAME (w->frame);
2499 /* Extra space between lines (on window systems only). */
2500 if (base_face_id == DEFAULT_FACE_ID
2501 && FRAME_WINDOW_P (it->f))
2503 if (NATNUMP (current_buffer->extra_line_spacing))
2504 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2505 else if (FLOATP (current_buffer->extra_line_spacing))
2506 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2507 * FRAME_LINE_HEIGHT (it->f));
2508 else if (it->f->extra_line_spacing > 0)
2509 it->extra_line_spacing = it->f->extra_line_spacing;
2510 it->max_extra_line_spacing = 0;
2513 /* If realized faces have been removed, e.g. because of face
2514 attribute changes of named faces, recompute them. When running
2515 in batch mode, the face cache of the initial frame is null. If
2516 we happen to get called, make a dummy face cache. */
2517 if (FRAME_FACE_CACHE (it->f) == NULL)
2518 init_frame_faces (it->f);
2519 if (FRAME_FACE_CACHE (it->f)->used == 0)
2520 recompute_basic_faces (it->f);
2522 /* Current value of the `slice', `space-width', and 'height' properties. */
2523 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2524 it->space_width = Qnil;
2525 it->font_height = Qnil;
2526 it->override_ascent = -1;
2528 /* Are control characters displayed as `^C'? */
2529 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2531 /* -1 means everything between a CR and the following line end
2532 is invisible. >0 means lines indented more than this value are
2533 invisible. */
2534 it->selective = (INTEGERP (current_buffer->selective_display)
2535 ? XFASTINT (current_buffer->selective_display)
2536 : (!NILP (current_buffer->selective_display)
2537 ? -1 : 0));
2538 it->selective_display_ellipsis_p
2539 = !NILP (current_buffer->selective_display_ellipses);
2541 /* Display table to use. */
2542 it->dp = window_display_table (w);
2544 /* Are multibyte characters enabled in current_buffer? */
2545 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2547 /* Non-zero if we should highlight the region. */
2548 highlight_region_p
2549 = (!NILP (Vtransient_mark_mode)
2550 && !NILP (current_buffer->mark_active)
2551 && XMARKER (current_buffer->mark)->buffer != 0);
2553 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2554 start and end of a visible region in window IT->w. Set both to
2555 -1 to indicate no region. */
2556 if (highlight_region_p
2557 /* Maybe highlight only in selected window. */
2558 && (/* Either show region everywhere. */
2559 highlight_nonselected_windows
2560 /* Or show region in the selected window. */
2561 || w == XWINDOW (selected_window)
2562 /* Or show the region if we are in the mini-buffer and W is
2563 the window the mini-buffer refers to. */
2564 || (MINI_WINDOW_P (XWINDOW (selected_window))
2565 && WINDOWP (minibuf_selected_window)
2566 && w == XWINDOW (minibuf_selected_window))))
2568 int charpos = marker_position (current_buffer->mark);
2569 it->region_beg_charpos = min (PT, charpos);
2570 it->region_end_charpos = max (PT, charpos);
2572 else
2573 it->region_beg_charpos = it->region_end_charpos = -1;
2575 /* Get the position at which the redisplay_end_trigger hook should
2576 be run, if it is to be run at all. */
2577 if (MARKERP (w->redisplay_end_trigger)
2578 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2579 it->redisplay_end_trigger_charpos
2580 = marker_position (w->redisplay_end_trigger);
2581 else if (INTEGERP (w->redisplay_end_trigger))
2582 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2584 /* Correct bogus values of tab_width. */
2585 it->tab_width = XINT (current_buffer->tab_width);
2586 if (it->tab_width <= 0 || it->tab_width > 1000)
2587 it->tab_width = 8;
2589 /* Are lines in the display truncated? */
2590 it->truncate_lines_p
2591 = (base_face_id != DEFAULT_FACE_ID
2592 || XINT (it->w->hscroll)
2593 || (truncate_partial_width_windows
2594 && !WINDOW_FULL_WIDTH_P (it->w))
2595 || !NILP (current_buffer->truncate_lines));
2597 /* Get dimensions of truncation and continuation glyphs. These are
2598 displayed as fringe bitmaps under X, so we don't need them for such
2599 frames. */
2600 if (!FRAME_WINDOW_P (it->f))
2602 if (it->truncate_lines_p)
2604 /* We will need the truncation glyph. */
2605 xassert (it->glyph_row == NULL);
2606 produce_special_glyphs (it, IT_TRUNCATION);
2607 it->truncation_pixel_width = it->pixel_width;
2609 else
2611 /* We will need the continuation glyph. */
2612 xassert (it->glyph_row == NULL);
2613 produce_special_glyphs (it, IT_CONTINUATION);
2614 it->continuation_pixel_width = it->pixel_width;
2617 /* Reset these values to zero because the produce_special_glyphs
2618 above has changed them. */
2619 it->pixel_width = it->ascent = it->descent = 0;
2620 it->phys_ascent = it->phys_descent = 0;
2623 /* Set this after getting the dimensions of truncation and
2624 continuation glyphs, so that we don't produce glyphs when calling
2625 produce_special_glyphs, above. */
2626 it->glyph_row = row;
2627 it->area = TEXT_AREA;
2629 /* Get the dimensions of the display area. The display area
2630 consists of the visible window area plus a horizontally scrolled
2631 part to the left of the window. All x-values are relative to the
2632 start of this total display area. */
2633 if (base_face_id != DEFAULT_FACE_ID)
2635 /* Mode lines, menu bar in terminal frames. */
2636 it->first_visible_x = 0;
2637 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2639 else
2641 it->first_visible_x
2642 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2643 it->last_visible_x = (it->first_visible_x
2644 + window_box_width (w, TEXT_AREA));
2646 /* If we truncate lines, leave room for the truncator glyph(s) at
2647 the right margin. Otherwise, leave room for the continuation
2648 glyph(s). Truncation and continuation glyphs are not inserted
2649 for window-based redisplay. */
2650 if (!FRAME_WINDOW_P (it->f))
2652 if (it->truncate_lines_p)
2653 it->last_visible_x -= it->truncation_pixel_width;
2654 else
2655 it->last_visible_x -= it->continuation_pixel_width;
2658 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2659 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2662 /* Leave room for a border glyph. */
2663 if (!FRAME_WINDOW_P (it->f)
2664 && !WINDOW_RIGHTMOST_P (it->w))
2665 it->last_visible_x -= 1;
2667 it->last_visible_y = window_text_bottom_y (w);
2669 /* For mode lines and alike, arrange for the first glyph having a
2670 left box line if the face specifies a box. */
2671 if (base_face_id != DEFAULT_FACE_ID)
2673 struct face *face;
2675 it->face_id = base_face_id;
2677 /* If we have a boxed mode line, make the first character appear
2678 with a left box line. */
2679 face = FACE_FROM_ID (it->f, base_face_id);
2680 if (face->box != FACE_NO_BOX)
2681 it->start_of_box_run_p = 1;
2684 /* If a buffer position was specified, set the iterator there,
2685 getting overlays and face properties from that position. */
2686 if (charpos >= BUF_BEG (current_buffer))
2688 it->end_charpos = ZV;
2689 it->face_id = -1;
2690 IT_CHARPOS (*it) = charpos;
2692 /* Compute byte position if not specified. */
2693 if (bytepos < charpos)
2694 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2695 else
2696 IT_BYTEPOS (*it) = bytepos;
2698 it->start = it->current;
2700 /* Compute faces etc. */
2701 reseat (it, it->current.pos, 1);
2704 CHECK_IT (it);
2708 /* Initialize IT for the display of window W with window start POS. */
2710 void
2711 start_display (it, w, pos)
2712 struct it *it;
2713 struct window *w;
2714 struct text_pos pos;
2716 struct glyph_row *row;
2717 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2719 row = w->desired_matrix->rows + first_vpos;
2720 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2721 it->first_vpos = first_vpos;
2723 /* Don't reseat to previous visible line start if current start
2724 position is in a string or image. */
2725 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2727 int start_at_line_beg_p;
2728 int first_y = it->current_y;
2730 /* If window start is not at a line start, skip forward to POS to
2731 get the correct continuation lines width. */
2732 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2733 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2734 if (!start_at_line_beg_p)
2736 int new_x;
2738 reseat_at_previous_visible_line_start (it);
2739 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2741 new_x = it->current_x + it->pixel_width;
2743 /* If lines are continued, this line may end in the middle
2744 of a multi-glyph character (e.g. a control character
2745 displayed as \003, or in the middle of an overlay
2746 string). In this case move_it_to above will not have
2747 taken us to the start of the continuation line but to the
2748 end of the continued line. */
2749 if (it->current_x > 0
2750 && !it->truncate_lines_p /* Lines are continued. */
2751 && (/* And glyph doesn't fit on the line. */
2752 new_x > it->last_visible_x
2753 /* Or it fits exactly and we're on a window
2754 system frame. */
2755 || (new_x == it->last_visible_x
2756 && FRAME_WINDOW_P (it->f))))
2758 if (it->current.dpvec_index >= 0
2759 || it->current.overlay_string_index >= 0)
2761 set_iterator_to_next (it, 1);
2762 move_it_in_display_line_to (it, -1, -1, 0);
2765 it->continuation_lines_width += it->current_x;
2768 /* We're starting a new display line, not affected by the
2769 height of the continued line, so clear the appropriate
2770 fields in the iterator structure. */
2771 it->max_ascent = it->max_descent = 0;
2772 it->max_phys_ascent = it->max_phys_descent = 0;
2774 it->current_y = first_y;
2775 it->vpos = 0;
2776 it->current_x = it->hpos = 0;
2780 #if 0 /* Don't assert the following because start_display is sometimes
2781 called intentionally with a window start that is not at a
2782 line start. Please leave this code in as a comment. */
2784 /* Window start should be on a line start, now. */
2785 xassert (it->continuation_lines_width
2786 || IT_CHARPOS (it) == BEGV
2787 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2788 #endif /* 0 */
2792 /* Return 1 if POS is a position in ellipses displayed for invisible
2793 text. W is the window we display, for text property lookup. */
2795 static int
2796 in_ellipses_for_invisible_text_p (pos, w)
2797 struct display_pos *pos;
2798 struct window *w;
2800 Lisp_Object prop, window;
2801 int ellipses_p = 0;
2802 int charpos = CHARPOS (pos->pos);
2804 /* If POS specifies a position in a display vector, this might
2805 be for an ellipsis displayed for invisible text. We won't
2806 get the iterator set up for delivering that ellipsis unless
2807 we make sure that it gets aware of the invisible text. */
2808 if (pos->dpvec_index >= 0
2809 && pos->overlay_string_index < 0
2810 && CHARPOS (pos->string_pos) < 0
2811 && charpos > BEGV
2812 && (XSETWINDOW (window, w),
2813 prop = Fget_char_property (make_number (charpos),
2814 Qinvisible, window),
2815 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2817 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2818 window);
2819 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2822 return ellipses_p;
2826 /* Initialize IT for stepping through current_buffer in window W,
2827 starting at position POS that includes overlay string and display
2828 vector/ control character translation position information. Value
2829 is zero if there are overlay strings with newlines at POS. */
2831 static int
2832 init_from_display_pos (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct display_pos *pos;
2837 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2838 int i, overlay_strings_with_newlines = 0;
2840 /* If POS specifies a position in a display vector, this might
2841 be for an ellipsis displayed for invisible text. We won't
2842 get the iterator set up for delivering that ellipsis unless
2843 we make sure that it gets aware of the invisible text. */
2844 if (in_ellipses_for_invisible_text_p (pos, w))
2846 --charpos;
2847 bytepos = 0;
2850 /* Keep in mind: the call to reseat in init_iterator skips invisible
2851 text, so we might end up at a position different from POS. This
2852 is only a problem when POS is a row start after a newline and an
2853 overlay starts there with an after-string, and the overlay has an
2854 invisible property. Since we don't skip invisible text in
2855 display_line and elsewhere immediately after consuming the
2856 newline before the row start, such a POS will not be in a string,
2857 but the call to init_iterator below will move us to the
2858 after-string. */
2859 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2861 /* This only scans the current chunk -- it should scan all chunks.
2862 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2863 to 16 in 22.1 to make this a lesser problem. */
2864 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2866 const char *s = SDATA (it->overlay_strings[i]);
2867 const char *e = s + SBYTES (it->overlay_strings[i]);
2869 while (s < e && *s != '\n')
2870 ++s;
2872 if (s < e)
2874 overlay_strings_with_newlines = 1;
2875 break;
2879 /* If position is within an overlay string, set up IT to the right
2880 overlay string. */
2881 if (pos->overlay_string_index >= 0)
2883 int relative_index;
2885 /* If the first overlay string happens to have a `display'
2886 property for an image, the iterator will be set up for that
2887 image, and we have to undo that setup first before we can
2888 correct the overlay string index. */
2889 if (it->method == GET_FROM_IMAGE)
2890 pop_it (it);
2892 /* We already have the first chunk of overlay strings in
2893 IT->overlay_strings. Load more until the one for
2894 pos->overlay_string_index is in IT->overlay_strings. */
2895 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2897 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2898 it->current.overlay_string_index = 0;
2899 while (n--)
2901 load_overlay_strings (it, 0);
2902 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2906 it->current.overlay_string_index = pos->overlay_string_index;
2907 relative_index = (it->current.overlay_string_index
2908 % OVERLAY_STRING_CHUNK_SIZE);
2909 it->string = it->overlay_strings[relative_index];
2910 xassert (STRINGP (it->string));
2911 it->current.string_pos = pos->string_pos;
2912 it->method = GET_FROM_STRING;
2915 #if 0 /* This is bogus because POS not having an overlay string
2916 position does not mean it's after the string. Example: A
2917 line starting with a before-string and initialization of IT
2918 to the previous row's end position. */
2919 else if (it->current.overlay_string_index >= 0)
2921 /* If POS says we're already after an overlay string ending at
2922 POS, make sure to pop the iterator because it will be in
2923 front of that overlay string. When POS is ZV, we've thereby
2924 also ``processed'' overlay strings at ZV. */
2925 while (it->sp)
2926 pop_it (it);
2927 xassert (it->current.overlay_string_index == -1);
2928 xassert (it->method == GET_FROM_BUFFER);
2929 if (CHARPOS (pos->pos) == ZV)
2930 it->overlay_strings_at_end_processed_p = 1;
2932 #endif /* 0 */
2934 if (CHARPOS (pos->string_pos) >= 0)
2936 /* Recorded position is not in an overlay string, but in another
2937 string. This can only be a string from a `display' property.
2938 IT should already be filled with that string. */
2939 it->current.string_pos = pos->string_pos;
2940 xassert (STRINGP (it->string));
2943 /* Restore position in display vector translations, control
2944 character translations or ellipses. */
2945 if (pos->dpvec_index >= 0)
2947 if (it->dpvec == NULL)
2948 get_next_display_element (it);
2949 xassert (it->dpvec && it->current.dpvec_index == 0);
2950 it->current.dpvec_index = pos->dpvec_index;
2953 CHECK_IT (it);
2954 return !overlay_strings_with_newlines;
2958 /* Initialize IT for stepping through current_buffer in window W
2959 starting at ROW->start. */
2961 static void
2962 init_to_row_start (it, w, row)
2963 struct it *it;
2964 struct window *w;
2965 struct glyph_row *row;
2967 init_from_display_pos (it, w, &row->start);
2968 it->start = row->start;
2969 it->continuation_lines_width = row->continuation_lines_width;
2970 CHECK_IT (it);
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting in the line following ROW, i.e. starting at ROW->end.
2976 Value is zero if there are overlay strings with newlines at ROW's
2977 end position. */
2979 static int
2980 init_to_row_end (it, w, row)
2981 struct it *it;
2982 struct window *w;
2983 struct glyph_row *row;
2985 int success = 0;
2987 if (init_from_display_pos (it, w, &row->end))
2989 if (row->continued_p)
2990 it->continuation_lines_width
2991 = row->continuation_lines_width + row->pixel_width;
2992 CHECK_IT (it);
2993 success = 1;
2996 return success;
3002 /***********************************************************************
3003 Text properties
3004 ***********************************************************************/
3006 /* Called when IT reaches IT->stop_charpos. Handle text property and
3007 overlay changes. Set IT->stop_charpos to the next position where
3008 to stop. */
3010 static void
3011 handle_stop (it)
3012 struct it *it;
3014 enum prop_handled handled;
3015 int handle_overlay_change_p;
3016 struct props *p;
3018 it->dpvec = NULL;
3019 it->current.dpvec_index = -1;
3020 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3021 it->ignore_overlay_strings_at_pos_p = 0;
3023 /* Use face of preceding text for ellipsis (if invisible) */
3024 if (it->selective_display_ellipsis_p)
3025 it->saved_face_id = it->face_id;
3029 handled = HANDLED_NORMALLY;
3031 /* Call text property handlers. */
3032 for (p = it_props; p->handler; ++p)
3034 handled = p->handler (it);
3036 if (handled == HANDLED_RECOMPUTE_PROPS)
3037 break;
3038 else if (handled == HANDLED_RETURN)
3040 /* We still want to show before and after strings from
3041 overlays even if the actual buffer text is replaced. */
3042 if (!handle_overlay_change_p || it->sp > 1)
3043 return;
3044 if (!get_overlay_strings_1 (it, 0, 0))
3045 return;
3046 it->ignore_overlay_strings_at_pos_p = 1;
3047 it->string_from_display_prop_p = 0;
3048 handle_overlay_change_p = 0;
3049 handled = HANDLED_RECOMPUTE_PROPS;
3050 break;
3052 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3053 handle_overlay_change_p = 0;
3056 if (handled != HANDLED_RECOMPUTE_PROPS)
3058 /* Don't check for overlay strings below when set to deliver
3059 characters from a display vector. */
3060 if (it->method == GET_FROM_DISPLAY_VECTOR)
3061 handle_overlay_change_p = 0;
3063 /* Handle overlay changes.
3064 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3065 if it finds overlays. */
3066 if (handle_overlay_change_p)
3067 handled = handle_overlay_change (it);
3070 while (handled == HANDLED_RECOMPUTE_PROPS);
3072 /* Determine where to stop next. */
3073 if (handled == HANDLED_NORMALLY)
3074 compute_stop_pos (it);
3078 /* Compute IT->stop_charpos from text property and overlay change
3079 information for IT's current position. */
3081 static void
3082 compute_stop_pos (it)
3083 struct it *it;
3085 register INTERVAL iv, next_iv;
3086 Lisp_Object object, limit, position;
3088 /* If nowhere else, stop at the end. */
3089 it->stop_charpos = it->end_charpos;
3091 if (STRINGP (it->string))
3093 /* Strings are usually short, so don't limit the search for
3094 properties. */
3095 object = it->string;
3096 limit = Qnil;
3097 position = make_number (IT_STRING_CHARPOS (*it));
3099 else
3101 int charpos;
3103 /* If next overlay change is in front of the current stop pos
3104 (which is IT->end_charpos), stop there. Note: value of
3105 next_overlay_change is point-max if no overlay change
3106 follows. */
3107 charpos = next_overlay_change (IT_CHARPOS (*it));
3108 if (charpos < it->stop_charpos)
3109 it->stop_charpos = charpos;
3111 /* If showing the region, we have to stop at the region
3112 start or end because the face might change there. */
3113 if (it->region_beg_charpos > 0)
3115 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3116 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3117 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3118 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3121 /* Set up variables for computing the stop position from text
3122 property changes. */
3123 XSETBUFFER (object, current_buffer);
3124 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3125 position = make_number (IT_CHARPOS (*it));
3129 /* Get the interval containing IT's position. Value is a null
3130 interval if there isn't such an interval. */
3131 iv = validate_interval_range (object, &position, &position, 0);
3132 if (!NULL_INTERVAL_P (iv))
3134 Lisp_Object values_here[LAST_PROP_IDX];
3135 struct props *p;
3137 /* Get properties here. */
3138 for (p = it_props; p->handler; ++p)
3139 values_here[p->idx] = textget (iv->plist, *p->name);
3141 /* Look for an interval following iv that has different
3142 properties. */
3143 for (next_iv = next_interval (iv);
3144 (!NULL_INTERVAL_P (next_iv)
3145 && (NILP (limit)
3146 || XFASTINT (limit) > next_iv->position));
3147 next_iv = next_interval (next_iv))
3149 for (p = it_props; p->handler; ++p)
3151 Lisp_Object new_value;
3153 new_value = textget (next_iv->plist, *p->name);
3154 if (!EQ (values_here[p->idx], new_value))
3155 break;
3158 if (p->handler)
3159 break;
3162 if (!NULL_INTERVAL_P (next_iv))
3164 if (INTEGERP (limit)
3165 && next_iv->position >= XFASTINT (limit))
3166 /* No text property change up to limit. */
3167 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3168 else
3169 /* Text properties change in next_iv. */
3170 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3174 xassert (STRINGP (it->string)
3175 || (it->stop_charpos >= BEGV
3176 && it->stop_charpos >= IT_CHARPOS (*it)));
3180 /* Return the position of the next overlay change after POS in
3181 current_buffer. Value is point-max if no overlay change
3182 follows. This is like `next-overlay-change' but doesn't use
3183 xmalloc. */
3185 static int
3186 next_overlay_change (pos)
3187 int pos;
3189 int noverlays;
3190 int endpos;
3191 Lisp_Object *overlays;
3192 int i;
3194 /* Get all overlays at the given position. */
3195 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3197 /* If any of these overlays ends before endpos,
3198 use its ending point instead. */
3199 for (i = 0; i < noverlays; ++i)
3201 Lisp_Object oend;
3202 int oendpos;
3204 oend = OVERLAY_END (overlays[i]);
3205 oendpos = OVERLAY_POSITION (oend);
3206 endpos = min (endpos, oendpos);
3209 return endpos;
3214 /***********************************************************************
3215 Fontification
3216 ***********************************************************************/
3218 /* Handle changes in the `fontified' property of the current buffer by
3219 calling hook functions from Qfontification_functions to fontify
3220 regions of text. */
3222 static enum prop_handled
3223 handle_fontified_prop (it)
3224 struct it *it;
3226 Lisp_Object prop, pos;
3227 enum prop_handled handled = HANDLED_NORMALLY;
3229 if (!NILP (Vmemory_full))
3230 return handled;
3232 /* Get the value of the `fontified' property at IT's current buffer
3233 position. (The `fontified' property doesn't have a special
3234 meaning in strings.) If the value is nil, call functions from
3235 Qfontification_functions. */
3236 if (!STRINGP (it->string)
3237 && it->s == NULL
3238 && !NILP (Vfontification_functions)
3239 && !NILP (Vrun_hooks)
3240 && (pos = make_number (IT_CHARPOS (*it)),
3241 prop = Fget_char_property (pos, Qfontified, Qnil),
3242 /* Ignore the special cased nil value always present at EOB since
3243 no amount of fontifying will be able to change it. */
3244 NILP (prop) && IT_CHARPOS (*it) < Z))
3246 int count = SPECPDL_INDEX ();
3247 Lisp_Object val;
3249 val = Vfontification_functions;
3250 specbind (Qfontification_functions, Qnil);
3252 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3253 safe_call1 (val, pos);
3254 else
3256 Lisp_Object globals, fn;
3257 struct gcpro gcpro1, gcpro2;
3259 globals = Qnil;
3260 GCPRO2 (val, globals);
3262 for (; CONSP (val); val = XCDR (val))
3264 fn = XCAR (val);
3266 if (EQ (fn, Qt))
3268 /* A value of t indicates this hook has a local
3269 binding; it means to run the global binding too.
3270 In a global value, t should not occur. If it
3271 does, we must ignore it to avoid an endless
3272 loop. */
3273 for (globals = Fdefault_value (Qfontification_functions);
3274 CONSP (globals);
3275 globals = XCDR (globals))
3277 fn = XCAR (globals);
3278 if (!EQ (fn, Qt))
3279 safe_call1 (fn, pos);
3282 else
3283 safe_call1 (fn, pos);
3286 UNGCPRO;
3289 unbind_to (count, Qnil);
3291 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3292 something. This avoids an endless loop if they failed to
3293 fontify the text for which reason ever. */
3294 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3295 handled = HANDLED_RECOMPUTE_PROPS;
3298 return handled;
3303 /***********************************************************************
3304 Faces
3305 ***********************************************************************/
3307 /* Set up iterator IT from face properties at its current position.
3308 Called from handle_stop. */
3310 static enum prop_handled
3311 handle_face_prop (it)
3312 struct it *it;
3314 int new_face_id, next_stop;
3316 if (!STRINGP (it->string))
3318 new_face_id
3319 = face_at_buffer_position (it->w,
3320 IT_CHARPOS (*it),
3321 it->region_beg_charpos,
3322 it->region_end_charpos,
3323 &next_stop,
3324 (IT_CHARPOS (*it)
3325 + TEXT_PROP_DISTANCE_LIMIT),
3328 /* Is this a start of a run of characters with box face?
3329 Caveat: this can be called for a freshly initialized
3330 iterator; face_id is -1 in this case. We know that the new
3331 face will not change until limit, i.e. if the new face has a
3332 box, all characters up to limit will have one. But, as
3333 usual, we don't know whether limit is really the end. */
3334 if (new_face_id != it->face_id)
3336 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3338 /* If new face has a box but old face has not, this is
3339 the start of a run of characters with box, i.e. it has
3340 a shadow on the left side. The value of face_id of the
3341 iterator will be -1 if this is the initial call that gets
3342 the face. In this case, we have to look in front of IT's
3343 position and see whether there is a face != new_face_id. */
3344 it->start_of_box_run_p
3345 = (new_face->box != FACE_NO_BOX
3346 && (it->face_id >= 0
3347 || IT_CHARPOS (*it) == BEG
3348 || new_face_id != face_before_it_pos (it)));
3349 it->face_box_p = new_face->box != FACE_NO_BOX;
3352 else
3354 int base_face_id, bufpos;
3355 int i;
3356 Lisp_Object from_overlay
3357 = (it->current.overlay_string_index >= 0
3358 ? it->string_overlays[it->current.overlay_string_index]
3359 : Qnil);
3361 /* See if we got to this string directly or indirectly from
3362 an overlay property. That includes the before-string or
3363 after-string of an overlay, strings in display properties
3364 provided by an overlay, their text properties, etc.
3366 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3367 if (! NILP (from_overlay))
3368 for (i = it->sp - 1; i >= 0; i--)
3370 if (it->stack[i].current.overlay_string_index >= 0)
3371 from_overlay
3372 = it->string_overlays[it->stack[i].current.overlay_string_index];
3373 else if (! NILP (it->stack[i].from_overlay))
3374 from_overlay = it->stack[i].from_overlay;
3376 if (!NILP (from_overlay))
3377 break;
3380 if (! NILP (from_overlay))
3382 bufpos = IT_CHARPOS (*it);
3383 /* For a string from an overlay, the base face depends
3384 only on text properties and ignores overlays. */
3385 base_face_id
3386 = face_for_overlay_string (it->w,
3387 IT_CHARPOS (*it),
3388 it->region_beg_charpos,
3389 it->region_end_charpos,
3390 &next_stop,
3391 (IT_CHARPOS (*it)
3392 + TEXT_PROP_DISTANCE_LIMIT),
3394 from_overlay);
3396 else
3398 bufpos = 0;
3400 /* For strings from a `display' property, use the face at
3401 IT's current buffer position as the base face to merge
3402 with, so that overlay strings appear in the same face as
3403 surrounding text, unless they specify their own
3404 faces. */
3405 base_face_id = underlying_face_id (it);
3408 new_face_id = face_at_string_position (it->w,
3409 it->string,
3410 IT_STRING_CHARPOS (*it),
3411 bufpos,
3412 it->region_beg_charpos,
3413 it->region_end_charpos,
3414 &next_stop,
3415 base_face_id, 0);
3417 #if 0 /* This shouldn't be neccessary. Let's check it. */
3418 /* If IT is used to display a mode line we would really like to
3419 use the mode line face instead of the frame's default face. */
3420 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3421 && new_face_id == DEFAULT_FACE_ID)
3422 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3423 #endif
3425 /* Is this a start of a run of characters with box? Caveat:
3426 this can be called for a freshly allocated iterator; face_id
3427 is -1 is this case. We know that the new face will not
3428 change until the next check pos, i.e. if the new face has a
3429 box, all characters up to that position will have a
3430 box. But, as usual, we don't know whether that position
3431 is really the end. */
3432 if (new_face_id != it->face_id)
3434 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3435 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3437 /* If new face has a box but old face hasn't, this is the
3438 start of a run of characters with box, i.e. it has a
3439 shadow on the left side. */
3440 it->start_of_box_run_p
3441 = new_face->box && (old_face == NULL || !old_face->box);
3442 it->face_box_p = new_face->box != FACE_NO_BOX;
3446 it->face_id = new_face_id;
3447 return HANDLED_NORMALLY;
3451 /* Return the ID of the face ``underlying'' IT's current position,
3452 which is in a string. If the iterator is associated with a
3453 buffer, return the face at IT's current buffer position.
3454 Otherwise, use the iterator's base_face_id. */
3456 static int
3457 underlying_face_id (it)
3458 struct it *it;
3460 int face_id = it->base_face_id, i;
3462 xassert (STRINGP (it->string));
3464 for (i = it->sp - 1; i >= 0; --i)
3465 if (NILP (it->stack[i].string))
3466 face_id = it->stack[i].face_id;
3468 return face_id;
3472 /* Compute the face one character before or after the current position
3473 of IT. BEFORE_P non-zero means get the face in front of IT's
3474 position. Value is the id of the face. */
3476 static int
3477 face_before_or_after_it_pos (it, before_p)
3478 struct it *it;
3479 int before_p;
3481 int face_id, limit;
3482 int next_check_charpos;
3483 struct text_pos pos;
3485 xassert (it->s == NULL);
3487 if (STRINGP (it->string))
3489 int bufpos, base_face_id;
3491 /* No face change past the end of the string (for the case
3492 we are padding with spaces). No face change before the
3493 string start. */
3494 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3495 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3496 return it->face_id;
3498 /* Set pos to the position before or after IT's current position. */
3499 if (before_p)
3500 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3501 else
3502 /* For composition, we must check the character after the
3503 composition. */
3504 pos = (it->what == IT_COMPOSITION
3505 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3506 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3508 if (it->current.overlay_string_index >= 0)
3509 bufpos = IT_CHARPOS (*it);
3510 else
3511 bufpos = 0;
3513 base_face_id = underlying_face_id (it);
3515 /* Get the face for ASCII, or unibyte. */
3516 face_id = face_at_string_position (it->w,
3517 it->string,
3518 CHARPOS (pos),
3519 bufpos,
3520 it->region_beg_charpos,
3521 it->region_end_charpos,
3522 &next_check_charpos,
3523 base_face_id, 0);
3525 /* Correct the face for charsets different from ASCII. Do it
3526 for the multibyte case only. The face returned above is
3527 suitable for unibyte text if IT->string is unibyte. */
3528 if (STRING_MULTIBYTE (it->string))
3530 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3531 int rest = SBYTES (it->string) - BYTEPOS (pos);
3532 int c, len;
3533 struct face *face = FACE_FROM_ID (it->f, face_id);
3535 c = string_char_and_length (p, rest, &len);
3536 face_id = FACE_FOR_CHAR (it->f, face, c);
3539 else
3541 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3542 || (IT_CHARPOS (*it) <= BEGV && before_p))
3543 return it->face_id;
3545 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3546 pos = it->current.pos;
3548 if (before_p)
3549 DEC_TEXT_POS (pos, it->multibyte_p);
3550 else
3552 if (it->what == IT_COMPOSITION)
3553 /* For composition, we must check the position after the
3554 composition. */
3555 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3556 else
3557 INC_TEXT_POS (pos, it->multibyte_p);
3560 /* Determine face for CHARSET_ASCII, or unibyte. */
3561 face_id = face_at_buffer_position (it->w,
3562 CHARPOS (pos),
3563 it->region_beg_charpos,
3564 it->region_end_charpos,
3565 &next_check_charpos,
3566 limit, 0);
3568 /* Correct the face for charsets different from ASCII. Do it
3569 for the multibyte case only. The face returned above is
3570 suitable for unibyte text if current_buffer is unibyte. */
3571 if (it->multibyte_p)
3573 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3574 struct face *face = FACE_FROM_ID (it->f, face_id);
3575 face_id = FACE_FOR_CHAR (it->f, face, c);
3579 return face_id;
3584 /***********************************************************************
3585 Invisible text
3586 ***********************************************************************/
3588 /* Set up iterator IT from invisible properties at its current
3589 position. Called from handle_stop. */
3591 static enum prop_handled
3592 handle_invisible_prop (it)
3593 struct it *it;
3595 enum prop_handled handled = HANDLED_NORMALLY;
3597 if (STRINGP (it->string))
3599 extern Lisp_Object Qinvisible;
3600 Lisp_Object prop, end_charpos, limit, charpos;
3602 /* Get the value of the invisible text property at the
3603 current position. Value will be nil if there is no such
3604 property. */
3605 charpos = make_number (IT_STRING_CHARPOS (*it));
3606 prop = Fget_text_property (charpos, Qinvisible, it->string);
3608 if (!NILP (prop)
3609 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3611 handled = HANDLED_RECOMPUTE_PROPS;
3613 /* Get the position at which the next change of the
3614 invisible text property can be found in IT->string.
3615 Value will be nil if the property value is the same for
3616 all the rest of IT->string. */
3617 XSETINT (limit, SCHARS (it->string));
3618 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3619 it->string, limit);
3621 /* Text at current position is invisible. The next
3622 change in the property is at position end_charpos.
3623 Move IT's current position to that position. */
3624 if (INTEGERP (end_charpos)
3625 && XFASTINT (end_charpos) < XFASTINT (limit))
3627 struct text_pos old;
3628 old = it->current.string_pos;
3629 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3630 compute_string_pos (&it->current.string_pos, old, it->string);
3632 else
3634 /* The rest of the string is invisible. If this is an
3635 overlay string, proceed with the next overlay string
3636 or whatever comes and return a character from there. */
3637 if (it->current.overlay_string_index >= 0)
3639 next_overlay_string (it);
3640 /* Don't check for overlay strings when we just
3641 finished processing them. */
3642 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3644 else
3646 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3647 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3652 else
3654 int invis_p;
3655 EMACS_INT newpos, next_stop, start_charpos;
3656 Lisp_Object pos, prop, overlay;
3658 /* First of all, is there invisible text at this position? */
3659 start_charpos = IT_CHARPOS (*it);
3660 pos = make_number (IT_CHARPOS (*it));
3661 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3662 &overlay);
3663 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3665 /* If we are on invisible text, skip over it. */
3666 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3668 /* Record whether we have to display an ellipsis for the
3669 invisible text. */
3670 int display_ellipsis_p = invis_p == 2;
3672 handled = HANDLED_RECOMPUTE_PROPS;
3674 /* Loop skipping over invisible text. The loop is left at
3675 ZV or with IT on the first char being visible again. */
3678 /* Try to skip some invisible text. Return value is the
3679 position reached which can be equal to IT's position
3680 if there is nothing invisible here. This skips both
3681 over invisible text properties and overlays with
3682 invisible property. */
3683 newpos = skip_invisible (IT_CHARPOS (*it),
3684 &next_stop, ZV, it->window);
3686 /* If we skipped nothing at all we weren't at invisible
3687 text in the first place. If everything to the end of
3688 the buffer was skipped, end the loop. */
3689 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3690 invis_p = 0;
3691 else
3693 /* We skipped some characters but not necessarily
3694 all there are. Check if we ended up on visible
3695 text. Fget_char_property returns the property of
3696 the char before the given position, i.e. if we
3697 get invis_p = 0, this means that the char at
3698 newpos is visible. */
3699 pos = make_number (newpos);
3700 prop = Fget_char_property (pos, Qinvisible, it->window);
3701 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3704 /* If we ended up on invisible text, proceed to
3705 skip starting with next_stop. */
3706 if (invis_p)
3707 IT_CHARPOS (*it) = next_stop;
3709 /* If there are adjacent invisible texts, don't lose the
3710 second one's ellipsis. */
3711 if (invis_p == 2)
3712 display_ellipsis_p = 1;
3714 while (invis_p);
3716 /* The position newpos is now either ZV or on visible text. */
3717 IT_CHARPOS (*it) = newpos;
3718 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3720 /* If there are before-strings at the start of invisible
3721 text, and the text is invisible because of a text
3722 property, arrange to show before-strings because 20.x did
3723 it that way. (If the text is invisible because of an
3724 overlay property instead of a text property, this is
3725 already handled in the overlay code.) */
3726 if (NILP (overlay)
3727 && get_overlay_strings (it, start_charpos))
3729 handled = HANDLED_RECOMPUTE_PROPS;
3730 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3732 else if (display_ellipsis_p)
3734 /* Make sure that the glyphs of the ellipsis will get
3735 correct `charpos' values. If we would not update
3736 it->position here, the glyphs would belong to the
3737 last visible character _before_ the invisible
3738 text, which confuses `set_cursor_from_row'.
3740 We use the last invisible position instead of the
3741 first because this way the cursor is always drawn on
3742 the first "." of the ellipsis, whenever PT is inside
3743 the invisible text. Otherwise the cursor would be
3744 placed _after_ the ellipsis when the point is after the
3745 first invisible character. */
3746 if (!STRINGP (it->object))
3748 it->position.charpos = IT_CHARPOS (*it) - 1;
3749 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3751 setup_for_ellipsis (it, 0);
3752 /* Let the ellipsis display before
3753 considering any properties of the following char.
3754 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3755 handled = HANDLED_RETURN;
3760 return handled;
3764 /* Make iterator IT return `...' next.
3765 Replaces LEN characters from buffer. */
3767 static void
3768 setup_for_ellipsis (it, len)
3769 struct it *it;
3770 int len;
3772 /* Use the display table definition for `...'. Invalid glyphs
3773 will be handled by the method returning elements from dpvec. */
3774 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3776 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3777 it->dpvec = v->contents;
3778 it->dpend = v->contents + v->size;
3780 else
3782 /* Default `...'. */
3783 it->dpvec = default_invis_vector;
3784 it->dpend = default_invis_vector + 3;
3787 it->dpvec_char_len = len;
3788 it->current.dpvec_index = 0;
3789 it->dpvec_face_id = -1;
3791 /* Remember the current face id in case glyphs specify faces.
3792 IT's face is restored in set_iterator_to_next.
3793 saved_face_id was set to preceding char's face in handle_stop. */
3794 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3795 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3797 it->method = GET_FROM_DISPLAY_VECTOR;
3798 it->ellipsis_p = 1;
3803 /***********************************************************************
3804 'display' property
3805 ***********************************************************************/
3807 /* Set up iterator IT from `display' property at its current position.
3808 Called from handle_stop.
3809 We return HANDLED_RETURN if some part of the display property
3810 overrides the display of the buffer text itself.
3811 Otherwise we return HANDLED_NORMALLY. */
3813 static enum prop_handled
3814 handle_display_prop (it)
3815 struct it *it;
3817 Lisp_Object prop, object, overlay;
3818 struct text_pos *position;
3819 /* Nonzero if some property replaces the display of the text itself. */
3820 int display_replaced_p = 0;
3822 if (STRINGP (it->string))
3824 object = it->string;
3825 position = &it->current.string_pos;
3827 else
3829 XSETWINDOW (object, it->w);
3830 position = &it->current.pos;
3833 /* Reset those iterator values set from display property values. */
3834 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3835 it->space_width = Qnil;
3836 it->font_height = Qnil;
3837 it->voffset = 0;
3839 /* We don't support recursive `display' properties, i.e. string
3840 values that have a string `display' property, that have a string
3841 `display' property etc. */
3842 if (!it->string_from_display_prop_p)
3843 it->area = TEXT_AREA;
3845 prop = get_char_property_and_overlay (make_number (position->charpos),
3846 Qdisplay, object, &overlay);
3847 if (NILP (prop))
3848 return HANDLED_NORMALLY;
3849 /* Now OVERLAY is the overlay that gave us this property, or nil
3850 if it was a text property. */
3852 if (!STRINGP (it->string))
3853 object = it->w->buffer;
3855 if (CONSP (prop)
3856 /* Simple properties. */
3857 && !EQ (XCAR (prop), Qimage)
3858 && !EQ (XCAR (prop), Qspace)
3859 && !EQ (XCAR (prop), Qwhen)
3860 && !EQ (XCAR (prop), Qslice)
3861 && !EQ (XCAR (prop), Qspace_width)
3862 && !EQ (XCAR (prop), Qheight)
3863 && !EQ (XCAR (prop), Qraise)
3864 /* Marginal area specifications. */
3865 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3866 && !EQ (XCAR (prop), Qleft_fringe)
3867 && !EQ (XCAR (prop), Qright_fringe)
3868 && !NILP (XCAR (prop)))
3870 for (; CONSP (prop); prop = XCDR (prop))
3872 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3873 position, display_replaced_p))
3875 display_replaced_p = 1;
3876 /* If some text in a string is replaced, `position' no
3877 longer points to the position of `object'. */
3878 if (STRINGP (object))
3879 break;
3883 else if (VECTORP (prop))
3885 int i;
3886 for (i = 0; i < ASIZE (prop); ++i)
3887 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3888 position, display_replaced_p))
3890 display_replaced_p = 1;
3891 /* If some text in a string is replaced, `position' no
3892 longer points to the position of `object'. */
3893 if (STRINGP (object))
3894 break;
3897 else
3899 int ret = handle_single_display_spec (it, prop, object, overlay,
3900 position, 0);
3901 if (ret < 0) /* Replaced by "", i.e. nothing. */
3902 return HANDLED_RECOMPUTE_PROPS;
3903 if (ret)
3904 display_replaced_p = 1;
3907 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3911 /* Value is the position of the end of the `display' property starting
3912 at START_POS in OBJECT. */
3914 static struct text_pos
3915 display_prop_end (it, object, start_pos)
3916 struct it *it;
3917 Lisp_Object object;
3918 struct text_pos start_pos;
3920 Lisp_Object end;
3921 struct text_pos end_pos;
3923 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3924 Qdisplay, object, Qnil);
3925 CHARPOS (end_pos) = XFASTINT (end);
3926 if (STRINGP (object))
3927 compute_string_pos (&end_pos, start_pos, it->string);
3928 else
3929 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3931 return end_pos;
3935 /* Set up IT from a single `display' specification PROP. OBJECT
3936 is the object in which the `display' property was found. *POSITION
3937 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3938 means that we previously saw a display specification which already
3939 replaced text display with something else, for example an image;
3940 we ignore such properties after the first one has been processed.
3942 OVERLAY is the overlay this `display' property came from,
3943 or nil if it was a text property.
3945 If PROP is a `space' or `image' specification, and in some other
3946 cases too, set *POSITION to the position where the `display'
3947 property ends.
3949 Value is non-zero if something was found which replaces the display
3950 of buffer or string text. Specifically, the value is -1 if that
3951 "something" is "nothing". */
3953 static int
3954 handle_single_display_spec (it, spec, object, overlay, position,
3955 display_replaced_before_p)
3956 struct it *it;
3957 Lisp_Object spec;
3958 Lisp_Object object;
3959 Lisp_Object overlay;
3960 struct text_pos *position;
3961 int display_replaced_before_p;
3963 Lisp_Object form;
3964 Lisp_Object location, value;
3965 struct text_pos start_pos, save_pos;
3966 int valid_p;
3968 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3969 If the result is non-nil, use VALUE instead of SPEC. */
3970 form = Qt;
3971 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3973 spec = XCDR (spec);
3974 if (!CONSP (spec))
3975 return 0;
3976 form = XCAR (spec);
3977 spec = XCDR (spec);
3980 if (!NILP (form) && !EQ (form, Qt))
3982 int count = SPECPDL_INDEX ();
3983 struct gcpro gcpro1;
3985 /* Bind `object' to the object having the `display' property, a
3986 buffer or string. Bind `position' to the position in the
3987 object where the property was found, and `buffer-position'
3988 to the current position in the buffer. */
3989 specbind (Qobject, object);
3990 specbind (Qposition, make_number (CHARPOS (*position)));
3991 specbind (Qbuffer_position,
3992 make_number (STRINGP (object)
3993 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3994 GCPRO1 (form);
3995 form = safe_eval (form);
3996 UNGCPRO;
3997 unbind_to (count, Qnil);
4000 if (NILP (form))
4001 return 0;
4003 /* Handle `(height HEIGHT)' specifications. */
4004 if (CONSP (spec)
4005 && EQ (XCAR (spec), Qheight)
4006 && CONSP (XCDR (spec)))
4008 if (!FRAME_WINDOW_P (it->f))
4009 return 0;
4011 it->font_height = XCAR (XCDR (spec));
4012 if (!NILP (it->font_height))
4014 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4015 int new_height = -1;
4017 if (CONSP (it->font_height)
4018 && (EQ (XCAR (it->font_height), Qplus)
4019 || EQ (XCAR (it->font_height), Qminus))
4020 && CONSP (XCDR (it->font_height))
4021 && INTEGERP (XCAR (XCDR (it->font_height))))
4023 /* `(+ N)' or `(- N)' where N is an integer. */
4024 int steps = XINT (XCAR (XCDR (it->font_height)));
4025 if (EQ (XCAR (it->font_height), Qplus))
4026 steps = - steps;
4027 it->face_id = smaller_face (it->f, it->face_id, steps);
4029 else if (FUNCTIONP (it->font_height))
4031 /* Call function with current height as argument.
4032 Value is the new height. */
4033 Lisp_Object height;
4034 height = safe_call1 (it->font_height,
4035 face->lface[LFACE_HEIGHT_INDEX]);
4036 if (NUMBERP (height))
4037 new_height = XFLOATINT (height);
4039 else if (NUMBERP (it->font_height))
4041 /* Value is a multiple of the canonical char height. */
4042 struct face *face;
4044 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4045 new_height = (XFLOATINT (it->font_height)
4046 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4048 else
4050 /* Evaluate IT->font_height with `height' bound to the
4051 current specified height to get the new height. */
4052 int count = SPECPDL_INDEX ();
4054 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4055 value = safe_eval (it->font_height);
4056 unbind_to (count, Qnil);
4058 if (NUMBERP (value))
4059 new_height = XFLOATINT (value);
4062 if (new_height > 0)
4063 it->face_id = face_with_height (it->f, it->face_id, new_height);
4066 return 0;
4069 /* Handle `(space-width WIDTH)'. */
4070 if (CONSP (spec)
4071 && EQ (XCAR (spec), Qspace_width)
4072 && CONSP (XCDR (spec)))
4074 if (!FRAME_WINDOW_P (it->f))
4075 return 0;
4077 value = XCAR (XCDR (spec));
4078 if (NUMBERP (value) && XFLOATINT (value) > 0)
4079 it->space_width = value;
4081 return 0;
4084 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4085 if (CONSP (spec)
4086 && EQ (XCAR (spec), Qslice))
4088 Lisp_Object tem;
4090 if (!FRAME_WINDOW_P (it->f))
4091 return 0;
4093 if (tem = XCDR (spec), CONSP (tem))
4095 it->slice.x = XCAR (tem);
4096 if (tem = XCDR (tem), CONSP (tem))
4098 it->slice.y = XCAR (tem);
4099 if (tem = XCDR (tem), CONSP (tem))
4101 it->slice.width = XCAR (tem);
4102 if (tem = XCDR (tem), CONSP (tem))
4103 it->slice.height = XCAR (tem);
4108 return 0;
4111 /* Handle `(raise FACTOR)'. */
4112 if (CONSP (spec)
4113 && EQ (XCAR (spec), Qraise)
4114 && CONSP (XCDR (spec)))
4116 if (!FRAME_WINDOW_P (it->f))
4117 return 0;
4119 #ifdef HAVE_WINDOW_SYSTEM
4120 value = XCAR (XCDR (spec));
4121 if (NUMBERP (value))
4123 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4124 it->voffset = - (XFLOATINT (value)
4125 * (FONT_HEIGHT (face->font)));
4127 #endif /* HAVE_WINDOW_SYSTEM */
4129 return 0;
4132 /* Don't handle the other kinds of display specifications
4133 inside a string that we got from a `display' property. */
4134 if (it->string_from_display_prop_p)
4135 return 0;
4137 /* Characters having this form of property are not displayed, so
4138 we have to find the end of the property. */
4139 start_pos = *position;
4140 *position = display_prop_end (it, object, start_pos);
4141 value = Qnil;
4143 /* Stop the scan at that end position--we assume that all
4144 text properties change there. */
4145 it->stop_charpos = position->charpos;
4147 /* Handle `(left-fringe BITMAP [FACE])'
4148 and `(right-fringe BITMAP [FACE])'. */
4149 if (CONSP (spec)
4150 && (EQ (XCAR (spec), Qleft_fringe)
4151 || EQ (XCAR (spec), Qright_fringe))
4152 && CONSP (XCDR (spec)))
4154 int face_id = DEFAULT_FACE_ID;
4155 int fringe_bitmap;
4157 if (!FRAME_WINDOW_P (it->f))
4158 /* If we return here, POSITION has been advanced
4159 across the text with this property. */
4160 return 0;
4162 #ifdef HAVE_WINDOW_SYSTEM
4163 value = XCAR (XCDR (spec));
4164 if (!SYMBOLP (value)
4165 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4166 /* If we return here, POSITION has been advanced
4167 across the text with this property. */
4168 return 0;
4170 if (CONSP (XCDR (XCDR (spec))))
4172 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4173 int face_id2 = lookup_derived_face (it->f, face_name,
4174 'A', FRINGE_FACE_ID, 0);
4175 if (face_id2 >= 0)
4176 face_id = face_id2;
4179 /* Save current settings of IT so that we can restore them
4180 when we are finished with the glyph property value. */
4182 save_pos = it->position;
4183 it->position = *position;
4184 push_it (it);
4185 it->position = save_pos;
4187 it->area = TEXT_AREA;
4188 it->what = IT_IMAGE;
4189 it->image_id = -1; /* no image */
4190 it->position = start_pos;
4191 it->object = NILP (object) ? it->w->buffer : object;
4192 it->method = GET_FROM_IMAGE;
4193 it->from_overlay = Qnil;
4194 it->face_id = face_id;
4196 /* Say that we haven't consumed the characters with
4197 `display' property yet. The call to pop_it in
4198 set_iterator_to_next will clean this up. */
4199 *position = start_pos;
4201 if (EQ (XCAR (spec), Qleft_fringe))
4203 it->left_user_fringe_bitmap = fringe_bitmap;
4204 it->left_user_fringe_face_id = face_id;
4206 else
4208 it->right_user_fringe_bitmap = fringe_bitmap;
4209 it->right_user_fringe_face_id = face_id;
4211 #endif /* HAVE_WINDOW_SYSTEM */
4212 return 1;
4215 /* Prepare to handle `((margin left-margin) ...)',
4216 `((margin right-margin) ...)' and `((margin nil) ...)'
4217 prefixes for display specifications. */
4218 location = Qunbound;
4219 if (CONSP (spec) && CONSP (XCAR (spec)))
4221 Lisp_Object tem;
4223 value = XCDR (spec);
4224 if (CONSP (value))
4225 value = XCAR (value);
4227 tem = XCAR (spec);
4228 if (EQ (XCAR (tem), Qmargin)
4229 && (tem = XCDR (tem),
4230 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4231 (NILP (tem)
4232 || EQ (tem, Qleft_margin)
4233 || EQ (tem, Qright_margin))))
4234 location = tem;
4237 if (EQ (location, Qunbound))
4239 location = Qnil;
4240 value = spec;
4243 /* After this point, VALUE is the property after any
4244 margin prefix has been stripped. It must be a string,
4245 an image specification, or `(space ...)'.
4247 LOCATION specifies where to display: `left-margin',
4248 `right-margin' or nil. */
4250 valid_p = (STRINGP (value)
4251 #ifdef HAVE_WINDOW_SYSTEM
4252 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4253 #endif /* not HAVE_WINDOW_SYSTEM */
4254 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4256 if (valid_p && !display_replaced_before_p)
4258 /* Save current settings of IT so that we can restore them
4259 when we are finished with the glyph property value. */
4260 save_pos = it->position;
4261 it->position = *position;
4262 push_it (it);
4263 it->position = save_pos;
4264 it->from_overlay = overlay;
4266 if (NILP (location))
4267 it->area = TEXT_AREA;
4268 else if (EQ (location, Qleft_margin))
4269 it->area = LEFT_MARGIN_AREA;
4270 else
4271 it->area = RIGHT_MARGIN_AREA;
4273 if (STRINGP (value))
4275 if (SCHARS (value) == 0)
4277 pop_it (it);
4278 return -1; /* Replaced by "", i.e. nothing. */
4280 it->string = value;
4281 it->multibyte_p = STRING_MULTIBYTE (it->string);
4282 it->current.overlay_string_index = -1;
4283 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4284 it->end_charpos = it->string_nchars = SCHARS (it->string);
4285 it->method = GET_FROM_STRING;
4286 it->stop_charpos = 0;
4287 it->string_from_display_prop_p = 1;
4288 /* Say that we haven't consumed the characters with
4289 `display' property yet. The call to pop_it in
4290 set_iterator_to_next will clean this up. */
4291 if (BUFFERP (object))
4292 it->current.pos = start_pos;
4294 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4296 it->method = GET_FROM_STRETCH;
4297 it->object = value;
4298 it->position = start_pos;
4299 if (BUFFERP (object))
4300 it->current.pos = start_pos;
4302 #ifdef HAVE_WINDOW_SYSTEM
4303 else
4305 it->what = IT_IMAGE;
4306 it->image_id = lookup_image (it->f, value);
4307 it->position = start_pos;
4308 it->object = NILP (object) ? it->w->buffer : object;
4309 it->method = GET_FROM_IMAGE;
4311 /* Say that we haven't consumed the characters with
4312 `display' property yet. The call to pop_it in
4313 set_iterator_to_next will clean this up. */
4314 if (BUFFERP (object))
4315 it->current.pos = start_pos;
4317 #endif /* HAVE_WINDOW_SYSTEM */
4319 return 1;
4322 /* Invalid property or property not supported. Restore
4323 POSITION to what it was before. */
4324 *position = start_pos;
4325 return 0;
4329 /* Check if SPEC is a display sub-property value whose text should be
4330 treated as intangible. */
4332 static int
4333 single_display_spec_intangible_p (prop)
4334 Lisp_Object prop;
4336 /* Skip over `when FORM'. */
4337 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4339 prop = XCDR (prop);
4340 if (!CONSP (prop))
4341 return 0;
4342 prop = XCDR (prop);
4345 if (STRINGP (prop))
4346 return 1;
4348 if (!CONSP (prop))
4349 return 0;
4351 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4352 we don't need to treat text as intangible. */
4353 if (EQ (XCAR (prop), Qmargin))
4355 prop = XCDR (prop);
4356 if (!CONSP (prop))
4357 return 0;
4359 prop = XCDR (prop);
4360 if (!CONSP (prop)
4361 || EQ (XCAR (prop), Qleft_margin)
4362 || EQ (XCAR (prop), Qright_margin))
4363 return 0;
4366 return (CONSP (prop)
4367 && (EQ (XCAR (prop), Qimage)
4368 || EQ (XCAR (prop), Qspace)));
4372 /* Check if PROP is a display property value whose text should be
4373 treated as intangible. */
4376 display_prop_intangible_p (prop)
4377 Lisp_Object prop;
4379 if (CONSP (prop)
4380 && CONSP (XCAR (prop))
4381 && !EQ (Qmargin, XCAR (XCAR (prop))))
4383 /* A list of sub-properties. */
4384 while (CONSP (prop))
4386 if (single_display_spec_intangible_p (XCAR (prop)))
4387 return 1;
4388 prop = XCDR (prop);
4391 else if (VECTORP (prop))
4393 /* A vector of sub-properties. */
4394 int i;
4395 for (i = 0; i < ASIZE (prop); ++i)
4396 if (single_display_spec_intangible_p (AREF (prop, i)))
4397 return 1;
4399 else
4400 return single_display_spec_intangible_p (prop);
4402 return 0;
4406 /* Return 1 if PROP is a display sub-property value containing STRING. */
4408 static int
4409 single_display_spec_string_p (prop, string)
4410 Lisp_Object prop, string;
4412 if (EQ (string, prop))
4413 return 1;
4415 /* Skip over `when FORM'. */
4416 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4418 prop = XCDR (prop);
4419 if (!CONSP (prop))
4420 return 0;
4421 prop = XCDR (prop);
4424 if (CONSP (prop))
4425 /* Skip over `margin LOCATION'. */
4426 if (EQ (XCAR (prop), Qmargin))
4428 prop = XCDR (prop);
4429 if (!CONSP (prop))
4430 return 0;
4432 prop = XCDR (prop);
4433 if (!CONSP (prop))
4434 return 0;
4437 return CONSP (prop) && EQ (XCAR (prop), string);
4441 /* Return 1 if STRING appears in the `display' property PROP. */
4443 static int
4444 display_prop_string_p (prop, string)
4445 Lisp_Object prop, string;
4447 if (CONSP (prop)
4448 && CONSP (XCAR (prop))
4449 && !EQ (Qmargin, XCAR (XCAR (prop))))
4451 /* A list of sub-properties. */
4452 while (CONSP (prop))
4454 if (single_display_spec_string_p (XCAR (prop), string))
4455 return 1;
4456 prop = XCDR (prop);
4459 else if (VECTORP (prop))
4461 /* A vector of sub-properties. */
4462 int i;
4463 for (i = 0; i < ASIZE (prop); ++i)
4464 if (single_display_spec_string_p (AREF (prop, i), string))
4465 return 1;
4467 else
4468 return single_display_spec_string_p (prop, string);
4470 return 0;
4474 /* Determine from which buffer position in W's buffer STRING comes
4475 from. AROUND_CHARPOS is an approximate position where it could
4476 be from. Value is the buffer position or 0 if it couldn't be
4477 determined.
4479 W's buffer must be current.
4481 This function is necessary because we don't record buffer positions
4482 in glyphs generated from strings (to keep struct glyph small).
4483 This function may only use code that doesn't eval because it is
4484 called asynchronously from note_mouse_highlight. */
4487 string_buffer_position (w, string, around_charpos)
4488 struct window *w;
4489 Lisp_Object string;
4490 int around_charpos;
4492 Lisp_Object limit, prop, pos;
4493 const int MAX_DISTANCE = 1000;
4494 int found = 0;
4496 pos = make_number (around_charpos);
4497 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4498 while (!found && !EQ (pos, limit))
4500 prop = Fget_char_property (pos, Qdisplay, Qnil);
4501 if (!NILP (prop) && display_prop_string_p (prop, string))
4502 found = 1;
4503 else
4504 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4507 if (!found)
4509 pos = make_number (around_charpos);
4510 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4511 while (!found && !EQ (pos, limit))
4513 prop = Fget_char_property (pos, Qdisplay, Qnil);
4514 if (!NILP (prop) && display_prop_string_p (prop, string))
4515 found = 1;
4516 else
4517 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4518 limit);
4522 return found ? XINT (pos) : 0;
4527 /***********************************************************************
4528 `composition' property
4529 ***********************************************************************/
4531 /* Set up iterator IT from `composition' property at its current
4532 position. Called from handle_stop. */
4534 static enum prop_handled
4535 handle_composition_prop (it)
4536 struct it *it;
4538 Lisp_Object prop, string;
4539 int pos, pos_byte, end;
4540 enum prop_handled handled = HANDLED_NORMALLY;
4542 if (STRINGP (it->string))
4544 pos = IT_STRING_CHARPOS (*it);
4545 pos_byte = IT_STRING_BYTEPOS (*it);
4546 string = it->string;
4548 else
4550 pos = IT_CHARPOS (*it);
4551 pos_byte = IT_BYTEPOS (*it);
4552 string = Qnil;
4555 /* If there's a valid composition and point is not inside of the
4556 composition (in the case that the composition is from the current
4557 buffer), draw a glyph composed from the composition components. */
4558 if (find_composition (pos, -1, &pos, &end, &prop, string)
4559 && COMPOSITION_VALID_P (pos, end, prop)
4560 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4562 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4564 if (id >= 0)
4566 struct composition *cmp = composition_table[id];
4568 if (cmp->glyph_len == 0)
4570 /* No glyph. */
4571 if (STRINGP (it->string))
4573 IT_STRING_CHARPOS (*it) = end;
4574 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4575 end);
4577 else
4579 IT_CHARPOS (*it) = end;
4580 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4582 return HANDLED_RECOMPUTE_PROPS;
4585 it->stop_charpos = end;
4586 push_it (it);
4588 it->method = GET_FROM_COMPOSITION;
4589 it->cmp_id = id;
4590 it->cmp_len = COMPOSITION_LENGTH (prop);
4591 /* For a terminal, draw only the first character of the
4592 components. */
4593 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4594 it->len = (STRINGP (it->string)
4595 ? string_char_to_byte (it->string, end)
4596 : CHAR_TO_BYTE (end)) - pos_byte;
4597 handled = HANDLED_RETURN;
4601 return handled;
4606 /***********************************************************************
4607 Overlay strings
4608 ***********************************************************************/
4610 /* The following structure is used to record overlay strings for
4611 later sorting in load_overlay_strings. */
4613 struct overlay_entry
4615 Lisp_Object overlay;
4616 Lisp_Object string;
4617 int priority;
4618 int after_string_p;
4622 /* Set up iterator IT from overlay strings at its current position.
4623 Called from handle_stop. */
4625 static enum prop_handled
4626 handle_overlay_change (it)
4627 struct it *it;
4629 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4630 return HANDLED_RECOMPUTE_PROPS;
4631 else
4632 return HANDLED_NORMALLY;
4636 /* Set up the next overlay string for delivery by IT, if there is an
4637 overlay string to deliver. Called by set_iterator_to_next when the
4638 end of the current overlay string is reached. If there are more
4639 overlay strings to display, IT->string and
4640 IT->current.overlay_string_index are set appropriately here.
4641 Otherwise IT->string is set to nil. */
4643 static void
4644 next_overlay_string (it)
4645 struct it *it;
4647 ++it->current.overlay_string_index;
4648 if (it->current.overlay_string_index == it->n_overlay_strings)
4650 /* No more overlay strings. Restore IT's settings to what
4651 they were before overlay strings were processed, and
4652 continue to deliver from current_buffer. */
4653 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4655 pop_it (it);
4656 xassert (it->sp > 0
4657 || it->method == GET_FROM_COMPOSITION
4658 || (NILP (it->string)
4659 && it->method == GET_FROM_BUFFER
4660 && it->stop_charpos >= BEGV
4661 && it->stop_charpos <= it->end_charpos));
4662 it->current.overlay_string_index = -1;
4663 it->n_overlay_strings = 0;
4665 /* If we're at the end of the buffer, record that we have
4666 processed the overlay strings there already, so that
4667 next_element_from_buffer doesn't try it again. */
4668 if (IT_CHARPOS (*it) >= it->end_charpos)
4669 it->overlay_strings_at_end_processed_p = 1;
4671 /* If we have to display `...' for invisible text, set
4672 the iterator up for that. */
4673 if (display_ellipsis_p)
4674 setup_for_ellipsis (it, 0);
4676 else
4678 /* There are more overlay strings to process. If
4679 IT->current.overlay_string_index has advanced to a position
4680 where we must load IT->overlay_strings with more strings, do
4681 it. */
4682 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4684 if (it->current.overlay_string_index && i == 0)
4685 load_overlay_strings (it, 0);
4687 /* Initialize IT to deliver display elements from the overlay
4688 string. */
4689 it->string = it->overlay_strings[i];
4690 it->multibyte_p = STRING_MULTIBYTE (it->string);
4691 SET_TEXT_POS (it->current.string_pos, 0, 0);
4692 it->method = GET_FROM_STRING;
4693 it->stop_charpos = 0;
4696 CHECK_IT (it);
4700 /* Compare two overlay_entry structures E1 and E2. Used as a
4701 comparison function for qsort in load_overlay_strings. Overlay
4702 strings for the same position are sorted so that
4704 1. All after-strings come in front of before-strings, except
4705 when they come from the same overlay.
4707 2. Within after-strings, strings are sorted so that overlay strings
4708 from overlays with higher priorities come first.
4710 2. Within before-strings, strings are sorted so that overlay
4711 strings from overlays with higher priorities come last.
4713 Value is analogous to strcmp. */
4716 static int
4717 compare_overlay_entries (e1, e2)
4718 void *e1, *e2;
4720 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4721 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4722 int result;
4724 if (entry1->after_string_p != entry2->after_string_p)
4726 /* Let after-strings appear in front of before-strings if
4727 they come from different overlays. */
4728 if (EQ (entry1->overlay, entry2->overlay))
4729 result = entry1->after_string_p ? 1 : -1;
4730 else
4731 result = entry1->after_string_p ? -1 : 1;
4733 else if (entry1->after_string_p)
4734 /* After-strings sorted in order of decreasing priority. */
4735 result = entry2->priority - entry1->priority;
4736 else
4737 /* Before-strings sorted in order of increasing priority. */
4738 result = entry1->priority - entry2->priority;
4740 return result;
4744 /* Load the vector IT->overlay_strings with overlay strings from IT's
4745 current buffer position, or from CHARPOS if that is > 0. Set
4746 IT->n_overlays to the total number of overlay strings found.
4748 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4749 a time. On entry into load_overlay_strings,
4750 IT->current.overlay_string_index gives the number of overlay
4751 strings that have already been loaded by previous calls to this
4752 function.
4754 IT->add_overlay_start contains an additional overlay start
4755 position to consider for taking overlay strings from, if non-zero.
4756 This position comes into play when the overlay has an `invisible'
4757 property, and both before and after-strings. When we've skipped to
4758 the end of the overlay, because of its `invisible' property, we
4759 nevertheless want its before-string to appear.
4760 IT->add_overlay_start will contain the overlay start position
4761 in this case.
4763 Overlay strings are sorted so that after-string strings come in
4764 front of before-string strings. Within before and after-strings,
4765 strings are sorted by overlay priority. See also function
4766 compare_overlay_entries. */
4768 static void
4769 load_overlay_strings (it, charpos)
4770 struct it *it;
4771 int charpos;
4773 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4774 Lisp_Object overlay, window, str, invisible;
4775 struct Lisp_Overlay *ov;
4776 int start, end;
4777 int size = 20;
4778 int n = 0, i, j, invis_p;
4779 struct overlay_entry *entries
4780 = (struct overlay_entry *) alloca (size * sizeof *entries);
4782 if (charpos <= 0)
4783 charpos = IT_CHARPOS (*it);
4785 /* Append the overlay string STRING of overlay OVERLAY to vector
4786 `entries' which has size `size' and currently contains `n'
4787 elements. AFTER_P non-zero means STRING is an after-string of
4788 OVERLAY. */
4789 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4790 do \
4792 Lisp_Object priority; \
4794 if (n == size) \
4796 int new_size = 2 * size; \
4797 struct overlay_entry *old = entries; \
4798 entries = \
4799 (struct overlay_entry *) alloca (new_size \
4800 * sizeof *entries); \
4801 bcopy (old, entries, size * sizeof *entries); \
4802 size = new_size; \
4805 entries[n].string = (STRING); \
4806 entries[n].overlay = (OVERLAY); \
4807 priority = Foverlay_get ((OVERLAY), Qpriority); \
4808 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4809 entries[n].after_string_p = (AFTER_P); \
4810 ++n; \
4812 while (0)
4814 /* Process overlay before the overlay center. */
4815 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4817 XSETMISC (overlay, ov);
4818 xassert (OVERLAYP (overlay));
4819 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4820 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4822 if (end < charpos)
4823 break;
4825 /* Skip this overlay if it doesn't start or end at IT's current
4826 position. */
4827 if (end != charpos && start != charpos)
4828 continue;
4830 /* Skip this overlay if it doesn't apply to IT->w. */
4831 window = Foverlay_get (overlay, Qwindow);
4832 if (WINDOWP (window) && XWINDOW (window) != it->w)
4833 continue;
4835 /* If the text ``under'' the overlay is invisible, both before-
4836 and after-strings from this overlay are visible; start and
4837 end position are indistinguishable. */
4838 invisible = Foverlay_get (overlay, Qinvisible);
4839 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4841 /* If overlay has a non-empty before-string, record it. */
4842 if ((start == charpos || (end == charpos && invis_p))
4843 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4844 && SCHARS (str))
4845 RECORD_OVERLAY_STRING (overlay, str, 0);
4847 /* If overlay has a non-empty after-string, record it. */
4848 if ((end == charpos || (start == charpos && invis_p))
4849 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4850 && SCHARS (str))
4851 RECORD_OVERLAY_STRING (overlay, str, 1);
4854 /* Process overlays after the overlay center. */
4855 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4857 XSETMISC (overlay, ov);
4858 xassert (OVERLAYP (overlay));
4859 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4860 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4862 if (start > charpos)
4863 break;
4865 /* Skip this overlay if it doesn't start or end at IT's current
4866 position. */
4867 if (end != charpos && start != charpos)
4868 continue;
4870 /* Skip this overlay if it doesn't apply to IT->w. */
4871 window = Foverlay_get (overlay, Qwindow);
4872 if (WINDOWP (window) && XWINDOW (window) != it->w)
4873 continue;
4875 /* If the text ``under'' the overlay is invisible, it has a zero
4876 dimension, and both before- and after-strings apply. */
4877 invisible = Foverlay_get (overlay, Qinvisible);
4878 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4880 /* If overlay has a non-empty before-string, record it. */
4881 if ((start == charpos || (end == charpos && invis_p))
4882 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4883 && SCHARS (str))
4884 RECORD_OVERLAY_STRING (overlay, str, 0);
4886 /* If overlay has a non-empty after-string, record it. */
4887 if ((end == charpos || (start == charpos && invis_p))
4888 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4889 && SCHARS (str))
4890 RECORD_OVERLAY_STRING (overlay, str, 1);
4893 #undef RECORD_OVERLAY_STRING
4895 /* Sort entries. */
4896 if (n > 1)
4897 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4899 /* Record the total number of strings to process. */
4900 it->n_overlay_strings = n;
4902 /* IT->current.overlay_string_index is the number of overlay strings
4903 that have already been consumed by IT. Copy some of the
4904 remaining overlay strings to IT->overlay_strings. */
4905 i = 0;
4906 j = it->current.overlay_string_index;
4907 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4909 it->overlay_strings[i] = entries[j].string;
4910 it->string_overlays[i++] = entries[j++].overlay;
4913 CHECK_IT (it);
4917 /* Get the first chunk of overlay strings at IT's current buffer
4918 position, or at CHARPOS if that is > 0. Value is non-zero if at
4919 least one overlay string was found. */
4921 static int
4922 get_overlay_strings_1 (it, charpos, compute_stop_p)
4923 struct it *it;
4924 int charpos;
4926 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4927 process. This fills IT->overlay_strings with strings, and sets
4928 IT->n_overlay_strings to the total number of strings to process.
4929 IT->pos.overlay_string_index has to be set temporarily to zero
4930 because load_overlay_strings needs this; it must be set to -1
4931 when no overlay strings are found because a zero value would
4932 indicate a position in the first overlay string. */
4933 it->current.overlay_string_index = 0;
4934 load_overlay_strings (it, charpos);
4936 /* If we found overlay strings, set up IT to deliver display
4937 elements from the first one. Otherwise set up IT to deliver
4938 from current_buffer. */
4939 if (it->n_overlay_strings)
4941 /* Make sure we know settings in current_buffer, so that we can
4942 restore meaningful values when we're done with the overlay
4943 strings. */
4944 if (compute_stop_p)
4945 compute_stop_pos (it);
4946 xassert (it->face_id >= 0);
4948 /* Save IT's settings. They are restored after all overlay
4949 strings have been processed. */
4950 xassert (!compute_stop_p || it->sp == 0);
4951 push_it (it);
4953 /* Set up IT to deliver display elements from the first overlay
4954 string. */
4955 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4956 it->string = it->overlay_strings[0];
4957 it->from_overlay = Qnil;
4958 it->stop_charpos = 0;
4959 xassert (STRINGP (it->string));
4960 it->end_charpos = SCHARS (it->string);
4961 it->multibyte_p = STRING_MULTIBYTE (it->string);
4962 it->method = GET_FROM_STRING;
4963 return 1;
4966 it->current.overlay_string_index = -1;
4967 return 0;
4970 static int
4971 get_overlay_strings (it, charpos)
4972 struct it *it;
4973 int charpos;
4975 it->string = Qnil;
4976 it->method = GET_FROM_BUFFER;
4978 (void) get_overlay_strings_1 (it, charpos, 1);
4980 CHECK_IT (it);
4982 /* Value is non-zero if we found at least one overlay string. */
4983 return STRINGP (it->string);
4988 /***********************************************************************
4989 Saving and restoring state
4990 ***********************************************************************/
4992 /* Save current settings of IT on IT->stack. Called, for example,
4993 before setting up IT for an overlay string, to be able to restore
4994 IT's settings to what they were after the overlay string has been
4995 processed. */
4997 static void
4998 push_it (it)
4999 struct it *it;
5001 struct iterator_stack_entry *p;
5003 xassert (it->sp < IT_STACK_SIZE);
5004 p = it->stack + it->sp;
5006 p->stop_charpos = it->stop_charpos;
5007 xassert (it->face_id >= 0);
5008 p->face_id = it->face_id;
5009 p->string = it->string;
5010 p->method = it->method;
5011 p->from_overlay = it->from_overlay;
5012 switch (p->method)
5014 case GET_FROM_IMAGE:
5015 p->u.image.object = it->object;
5016 p->u.image.image_id = it->image_id;
5017 p->u.image.slice = it->slice;
5018 break;
5019 case GET_FROM_COMPOSITION:
5020 p->u.comp.object = it->object;
5021 p->u.comp.c = it->c;
5022 p->u.comp.len = it->len;
5023 p->u.comp.cmp_id = it->cmp_id;
5024 p->u.comp.cmp_len = it->cmp_len;
5025 break;
5026 case GET_FROM_STRETCH:
5027 p->u.stretch.object = it->object;
5028 break;
5030 p->position = it->position;
5031 p->current = it->current;
5032 p->end_charpos = it->end_charpos;
5033 p->string_nchars = it->string_nchars;
5034 p->area = it->area;
5035 p->multibyte_p = it->multibyte_p;
5036 p->space_width = it->space_width;
5037 p->font_height = it->font_height;
5038 p->voffset = it->voffset;
5039 p->string_from_display_prop_p = it->string_from_display_prop_p;
5040 p->display_ellipsis_p = 0;
5041 ++it->sp;
5045 /* Restore IT's settings from IT->stack. Called, for example, when no
5046 more overlay strings must be processed, and we return to delivering
5047 display elements from a buffer, or when the end of a string from a
5048 `display' property is reached and we return to delivering display
5049 elements from an overlay string, or from a buffer. */
5051 static void
5052 pop_it (it)
5053 struct it *it;
5055 struct iterator_stack_entry *p;
5057 xassert (it->sp > 0);
5058 --it->sp;
5059 p = it->stack + it->sp;
5060 it->stop_charpos = p->stop_charpos;
5061 it->face_id = p->face_id;
5062 it->current = p->current;
5063 it->position = p->position;
5064 it->string = p->string;
5065 it->from_overlay = p->from_overlay;
5066 if (NILP (it->string))
5067 SET_TEXT_POS (it->current.string_pos, -1, -1);
5068 it->method = p->method;
5069 switch (it->method)
5071 case GET_FROM_IMAGE:
5072 it->image_id = p->u.image.image_id;
5073 it->object = p->u.image.object;
5074 it->slice = p->u.image.slice;
5075 break;
5076 case GET_FROM_COMPOSITION:
5077 it->object = p->u.comp.object;
5078 it->c = p->u.comp.c;
5079 it->len = p->u.comp.len;
5080 it->cmp_id = p->u.comp.cmp_id;
5081 it->cmp_len = p->u.comp.cmp_len;
5082 break;
5083 case GET_FROM_STRETCH:
5084 it->object = p->u.comp.object;
5085 break;
5086 case GET_FROM_BUFFER:
5087 it->object = it->w->buffer;
5088 break;
5089 case GET_FROM_STRING:
5090 it->object = it->string;
5091 break;
5093 it->end_charpos = p->end_charpos;
5094 it->string_nchars = p->string_nchars;
5095 it->area = p->area;
5096 it->multibyte_p = p->multibyte_p;
5097 it->space_width = p->space_width;
5098 it->font_height = p->font_height;
5099 it->voffset = p->voffset;
5100 it->string_from_display_prop_p = p->string_from_display_prop_p;
5105 /***********************************************************************
5106 Moving over lines
5107 ***********************************************************************/
5109 /* Set IT's current position to the previous line start. */
5111 static void
5112 back_to_previous_line_start (it)
5113 struct it *it;
5115 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5116 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5120 /* Move IT to the next line start.
5122 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5123 we skipped over part of the text (as opposed to moving the iterator
5124 continuously over the text). Otherwise, don't change the value
5125 of *SKIPPED_P.
5127 Newlines may come from buffer text, overlay strings, or strings
5128 displayed via the `display' property. That's the reason we can't
5129 simply use find_next_newline_no_quit.
5131 Note that this function may not skip over invisible text that is so
5132 because of text properties and immediately follows a newline. If
5133 it would, function reseat_at_next_visible_line_start, when called
5134 from set_iterator_to_next, would effectively make invisible
5135 characters following a newline part of the wrong glyph row, which
5136 leads to wrong cursor motion. */
5138 static int
5139 forward_to_next_line_start (it, skipped_p)
5140 struct it *it;
5141 int *skipped_p;
5143 int old_selective, newline_found_p, n;
5144 const int MAX_NEWLINE_DISTANCE = 500;
5146 /* If already on a newline, just consume it to avoid unintended
5147 skipping over invisible text below. */
5148 if (it->what == IT_CHARACTER
5149 && it->c == '\n'
5150 && CHARPOS (it->position) == IT_CHARPOS (*it))
5152 set_iterator_to_next (it, 0);
5153 it->c = 0;
5154 return 1;
5157 /* Don't handle selective display in the following. It's (a)
5158 unnecessary because it's done by the caller, and (b) leads to an
5159 infinite recursion because next_element_from_ellipsis indirectly
5160 calls this function. */
5161 old_selective = it->selective;
5162 it->selective = 0;
5164 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5165 from buffer text. */
5166 for (n = newline_found_p = 0;
5167 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5168 n += STRINGP (it->string) ? 0 : 1)
5170 if (!get_next_display_element (it))
5171 return 0;
5172 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5173 set_iterator_to_next (it, 0);
5176 /* If we didn't find a newline near enough, see if we can use a
5177 short-cut. */
5178 if (!newline_found_p)
5180 int start = IT_CHARPOS (*it);
5181 int limit = find_next_newline_no_quit (start, 1);
5182 Lisp_Object pos;
5184 xassert (!STRINGP (it->string));
5186 /* If there isn't any `display' property in sight, and no
5187 overlays, we can just use the position of the newline in
5188 buffer text. */
5189 if (it->stop_charpos >= limit
5190 || ((pos = Fnext_single_property_change (make_number (start),
5191 Qdisplay,
5192 Qnil, make_number (limit)),
5193 NILP (pos))
5194 && next_overlay_change (start) == ZV))
5196 IT_CHARPOS (*it) = limit;
5197 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5198 *skipped_p = newline_found_p = 1;
5200 else
5202 while (get_next_display_element (it)
5203 && !newline_found_p)
5205 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5206 set_iterator_to_next (it, 0);
5211 it->selective = old_selective;
5212 return newline_found_p;
5216 /* Set IT's current position to the previous visible line start. Skip
5217 invisible text that is so either due to text properties or due to
5218 selective display. Caution: this does not change IT->current_x and
5219 IT->hpos. */
5221 static void
5222 back_to_previous_visible_line_start (it)
5223 struct it *it;
5225 while (IT_CHARPOS (*it) > BEGV)
5227 back_to_previous_line_start (it);
5229 if (IT_CHARPOS (*it) <= BEGV)
5230 break;
5232 /* If selective > 0, then lines indented more than that values
5233 are invisible. */
5234 if (it->selective > 0
5235 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5236 (double) it->selective)) /* iftc */
5237 continue;
5239 /* Check the newline before point for invisibility. */
5241 Lisp_Object prop;
5242 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5243 Qinvisible, it->window);
5244 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5245 continue;
5248 if (IT_CHARPOS (*it) <= BEGV)
5249 break;
5252 struct it it2;
5253 int pos;
5254 int beg, end;
5255 Lisp_Object val, overlay;
5257 /* If newline is part of a composition, continue from start of composition */
5258 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5259 && beg < IT_CHARPOS (*it))
5260 goto replaced;
5262 /* If newline is replaced by a display property, find start of overlay
5263 or interval and continue search from that point. */
5264 it2 = *it;
5265 pos = --IT_CHARPOS (it2);
5266 --IT_BYTEPOS (it2);
5267 it2.sp = 0;
5268 if (handle_display_prop (&it2) == HANDLED_RETURN
5269 && !NILP (val = get_char_property_and_overlay
5270 (make_number (pos), Qdisplay, Qnil, &overlay))
5271 && (OVERLAYP (overlay)
5272 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5273 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5274 goto replaced;
5276 /* Newline is not replaced by anything -- so we are done. */
5277 break;
5279 replaced:
5280 if (beg < BEGV)
5281 beg = BEGV;
5282 IT_CHARPOS (*it) = beg;
5283 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5287 it->continuation_lines_width = 0;
5289 xassert (IT_CHARPOS (*it) >= BEGV);
5290 xassert (IT_CHARPOS (*it) == BEGV
5291 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5292 CHECK_IT (it);
5296 /* Reseat iterator IT at the previous visible line start. Skip
5297 invisible text that is so either due to text properties or due to
5298 selective display. At the end, update IT's overlay information,
5299 face information etc. */
5301 void
5302 reseat_at_previous_visible_line_start (it)
5303 struct it *it;
5305 back_to_previous_visible_line_start (it);
5306 reseat (it, it->current.pos, 1);
5307 CHECK_IT (it);
5311 /* Reseat iterator IT on the next visible line start in the current
5312 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5313 preceding the line start. Skip over invisible text that is so
5314 because of selective display. Compute faces, overlays etc at the
5315 new position. Note that this function does not skip over text that
5316 is invisible because of text properties. */
5318 static void
5319 reseat_at_next_visible_line_start (it, on_newline_p)
5320 struct it *it;
5321 int on_newline_p;
5323 int newline_found_p, skipped_p = 0;
5325 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5327 /* Skip over lines that are invisible because they are indented
5328 more than the value of IT->selective. */
5329 if (it->selective > 0)
5330 while (IT_CHARPOS (*it) < ZV
5331 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5332 (double) it->selective)) /* iftc */
5334 xassert (IT_BYTEPOS (*it) == BEGV
5335 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5336 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5339 /* Position on the newline if that's what's requested. */
5340 if (on_newline_p && newline_found_p)
5342 if (STRINGP (it->string))
5344 if (IT_STRING_CHARPOS (*it) > 0)
5346 --IT_STRING_CHARPOS (*it);
5347 --IT_STRING_BYTEPOS (*it);
5350 else if (IT_CHARPOS (*it) > BEGV)
5352 --IT_CHARPOS (*it);
5353 --IT_BYTEPOS (*it);
5354 reseat (it, it->current.pos, 0);
5357 else if (skipped_p)
5358 reseat (it, it->current.pos, 0);
5360 CHECK_IT (it);
5365 /***********************************************************************
5366 Changing an iterator's position
5367 ***********************************************************************/
5369 /* Change IT's current position to POS in current_buffer. If FORCE_P
5370 is non-zero, always check for text properties at the new position.
5371 Otherwise, text properties are only looked up if POS >=
5372 IT->check_charpos of a property. */
5374 static void
5375 reseat (it, pos, force_p)
5376 struct it *it;
5377 struct text_pos pos;
5378 int force_p;
5380 int original_pos = IT_CHARPOS (*it);
5382 reseat_1 (it, pos, 0);
5384 /* Determine where to check text properties. Avoid doing it
5385 where possible because text property lookup is very expensive. */
5386 if (force_p
5387 || CHARPOS (pos) > it->stop_charpos
5388 || CHARPOS (pos) < original_pos)
5389 handle_stop (it);
5391 CHECK_IT (it);
5395 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5396 IT->stop_pos to POS, also. */
5398 static void
5399 reseat_1 (it, pos, set_stop_p)
5400 struct it *it;
5401 struct text_pos pos;
5402 int set_stop_p;
5404 /* Don't call this function when scanning a C string. */
5405 xassert (it->s == NULL);
5407 /* POS must be a reasonable value. */
5408 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5410 it->current.pos = it->position = pos;
5411 it->end_charpos = ZV;
5412 it->dpvec = NULL;
5413 it->current.dpvec_index = -1;
5414 it->current.overlay_string_index = -1;
5415 IT_STRING_CHARPOS (*it) = -1;
5416 IT_STRING_BYTEPOS (*it) = -1;
5417 it->string = Qnil;
5418 it->method = GET_FROM_BUFFER;
5419 it->object = it->w->buffer;
5420 it->area = TEXT_AREA;
5421 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5422 it->sp = 0;
5423 it->string_from_display_prop_p = 0;
5424 it->face_before_selective_p = 0;
5426 if (set_stop_p)
5427 it->stop_charpos = CHARPOS (pos);
5431 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5432 If S is non-null, it is a C string to iterate over. Otherwise,
5433 STRING gives a Lisp string to iterate over.
5435 If PRECISION > 0, don't return more then PRECISION number of
5436 characters from the string.
5438 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5439 characters have been returned. FIELD_WIDTH < 0 means an infinite
5440 field width.
5442 MULTIBYTE = 0 means disable processing of multibyte characters,
5443 MULTIBYTE > 0 means enable it,
5444 MULTIBYTE < 0 means use IT->multibyte_p.
5446 IT must be initialized via a prior call to init_iterator before
5447 calling this function. */
5449 static void
5450 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5451 struct it *it;
5452 unsigned char *s;
5453 Lisp_Object string;
5454 int charpos;
5455 int precision, field_width, multibyte;
5457 /* No region in strings. */
5458 it->region_beg_charpos = it->region_end_charpos = -1;
5460 /* No text property checks performed by default, but see below. */
5461 it->stop_charpos = -1;
5463 /* Set iterator position and end position. */
5464 bzero (&it->current, sizeof it->current);
5465 it->current.overlay_string_index = -1;
5466 it->current.dpvec_index = -1;
5467 xassert (charpos >= 0);
5469 /* If STRING is specified, use its multibyteness, otherwise use the
5470 setting of MULTIBYTE, if specified. */
5471 if (multibyte >= 0)
5472 it->multibyte_p = multibyte > 0;
5474 if (s == NULL)
5476 xassert (STRINGP (string));
5477 it->string = string;
5478 it->s = NULL;
5479 it->end_charpos = it->string_nchars = SCHARS (string);
5480 it->method = GET_FROM_STRING;
5481 it->current.string_pos = string_pos (charpos, string);
5483 else
5485 it->s = s;
5486 it->string = Qnil;
5488 /* Note that we use IT->current.pos, not it->current.string_pos,
5489 for displaying C strings. */
5490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5491 if (it->multibyte_p)
5493 it->current.pos = c_string_pos (charpos, s, 1);
5494 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5496 else
5498 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5499 it->end_charpos = it->string_nchars = strlen (s);
5502 it->method = GET_FROM_C_STRING;
5505 /* PRECISION > 0 means don't return more than PRECISION characters
5506 from the string. */
5507 if (precision > 0 && it->end_charpos - charpos > precision)
5508 it->end_charpos = it->string_nchars = charpos + precision;
5510 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5511 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5512 FIELD_WIDTH < 0 means infinite field width. This is useful for
5513 padding with `-' at the end of a mode line. */
5514 if (field_width < 0)
5515 field_width = INFINITY;
5516 if (field_width > it->end_charpos - charpos)
5517 it->end_charpos = charpos + field_width;
5519 /* Use the standard display table for displaying strings. */
5520 if (DISP_TABLE_P (Vstandard_display_table))
5521 it->dp = XCHAR_TABLE (Vstandard_display_table);
5523 it->stop_charpos = charpos;
5524 CHECK_IT (it);
5529 /***********************************************************************
5530 Iteration
5531 ***********************************************************************/
5533 /* Map enum it_method value to corresponding next_element_from_* function. */
5535 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5537 next_element_from_buffer,
5538 next_element_from_display_vector,
5539 next_element_from_composition,
5540 next_element_from_string,
5541 next_element_from_c_string,
5542 next_element_from_image,
5543 next_element_from_stretch
5547 /* Load IT's display element fields with information about the next
5548 display element from the current position of IT. Value is zero if
5549 end of buffer (or C string) is reached. */
5551 static struct frame *last_escape_glyph_frame = NULL;
5552 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5553 static int last_escape_glyph_merged_face_id = 0;
5556 get_next_display_element (it)
5557 struct it *it;
5559 /* Non-zero means that we found a display element. Zero means that
5560 we hit the end of what we iterate over. Performance note: the
5561 function pointer `method' used here turns out to be faster than
5562 using a sequence of if-statements. */
5563 int success_p;
5565 get_next:
5566 success_p = (*get_next_element[it->method]) (it);
5568 if (it->what == IT_CHARACTER)
5570 /* Map via display table or translate control characters.
5571 IT->c, IT->len etc. have been set to the next character by
5572 the function call above. If we have a display table, and it
5573 contains an entry for IT->c, translate it. Don't do this if
5574 IT->c itself comes from a display table, otherwise we could
5575 end up in an infinite recursion. (An alternative could be to
5576 count the recursion depth of this function and signal an
5577 error when a certain maximum depth is reached.) Is it worth
5578 it? */
5579 if (success_p && it->dpvec == NULL)
5581 Lisp_Object dv;
5583 if (it->dp
5584 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5585 VECTORP (dv)))
5587 struct Lisp_Vector *v = XVECTOR (dv);
5589 /* Return the first character from the display table
5590 entry, if not empty. If empty, don't display the
5591 current character. */
5592 if (v->size)
5594 it->dpvec_char_len = it->len;
5595 it->dpvec = v->contents;
5596 it->dpend = v->contents + v->size;
5597 it->current.dpvec_index = 0;
5598 it->dpvec_face_id = -1;
5599 it->saved_face_id = it->face_id;
5600 it->method = GET_FROM_DISPLAY_VECTOR;
5601 it->ellipsis_p = 0;
5603 else
5605 set_iterator_to_next (it, 0);
5607 goto get_next;
5610 /* Translate control characters into `\003' or `^C' form.
5611 Control characters coming from a display table entry are
5612 currently not translated because we use IT->dpvec to hold
5613 the translation. This could easily be changed but I
5614 don't believe that it is worth doing.
5616 If it->multibyte_p is nonzero, eight-bit characters and
5617 non-printable multibyte characters are also translated to
5618 octal form.
5620 If it->multibyte_p is zero, eight-bit characters that
5621 don't have corresponding multibyte char code are also
5622 translated to octal form. */
5623 else if ((it->c < ' '
5624 && (it->area != TEXT_AREA
5625 /* In mode line, treat \n like other crl chars. */
5626 || (it->c != '\t'
5627 && it->glyph_row && it->glyph_row->mode_line_p)
5628 || (it->c != '\n' && it->c != '\t')))
5629 || (it->multibyte_p
5630 ? ((it->c >= 127
5631 && it->len == 1)
5632 || !CHAR_PRINTABLE_P (it->c)
5633 || (!NILP (Vnobreak_char_display)
5634 && (it->c == 0x8a0 || it->c == 0x8ad
5635 || it->c == 0x920 || it->c == 0x92d
5636 || it->c == 0xe20 || it->c == 0xe2d
5637 || it->c == 0xf20 || it->c == 0xf2d)))
5638 : (it->c >= 127
5639 && (!unibyte_display_via_language_environment
5640 || it->c == unibyte_char_to_multibyte (it->c)))))
5642 /* IT->c is a control character which must be displayed
5643 either as '\003' or as `^C' where the '\\' and '^'
5644 can be defined in the display table. Fill
5645 IT->ctl_chars with glyphs for what we have to
5646 display. Then, set IT->dpvec to these glyphs. */
5647 GLYPH g;
5648 int ctl_len;
5649 int face_id, lface_id = 0 ;
5650 GLYPH escape_glyph;
5652 /* Handle control characters with ^. */
5654 if (it->c < 128 && it->ctl_arrow_p)
5656 g = '^'; /* default glyph for Control */
5657 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5658 if (it->dp
5659 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5660 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5662 g = XINT (DISP_CTRL_GLYPH (it->dp));
5663 lface_id = FAST_GLYPH_FACE (g);
5665 if (lface_id)
5667 g = FAST_GLYPH_CHAR (g);
5668 face_id = merge_faces (it->f, Qt, lface_id,
5669 it->face_id);
5671 else if (it->f == last_escape_glyph_frame
5672 && it->face_id == last_escape_glyph_face_id)
5674 face_id = last_escape_glyph_merged_face_id;
5676 else
5678 /* Merge the escape-glyph face into the current face. */
5679 face_id = merge_faces (it->f, Qescape_glyph, 0,
5680 it->face_id);
5681 last_escape_glyph_frame = it->f;
5682 last_escape_glyph_face_id = it->face_id;
5683 last_escape_glyph_merged_face_id = face_id;
5686 XSETINT (it->ctl_chars[0], g);
5687 g = it->c ^ 0100;
5688 XSETINT (it->ctl_chars[1], g);
5689 ctl_len = 2;
5690 goto display_control;
5693 /* Handle non-break space in the mode where it only gets
5694 highlighting. */
5696 if (EQ (Vnobreak_char_display, Qt)
5697 && (it->c == 0x8a0 || it->c == 0x920
5698 || it->c == 0xe20 || it->c == 0xf20))
5700 /* Merge the no-break-space face into the current face. */
5701 face_id = merge_faces (it->f, Qnobreak_space, 0,
5702 it->face_id);
5704 g = it->c = ' ';
5705 XSETINT (it->ctl_chars[0], g);
5706 ctl_len = 1;
5707 goto display_control;
5710 /* Handle sequences that start with the "escape glyph". */
5712 /* the default escape glyph is \. */
5713 escape_glyph = '\\';
5715 if (it->dp
5716 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5717 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5719 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5720 lface_id = FAST_GLYPH_FACE (escape_glyph);
5722 if (lface_id)
5724 /* The display table specified a face.
5725 Merge it into face_id and also into escape_glyph. */
5726 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5727 face_id = merge_faces (it->f, Qt, lface_id,
5728 it->face_id);
5730 else if (it->f == last_escape_glyph_frame
5731 && it->face_id == last_escape_glyph_face_id)
5733 face_id = last_escape_glyph_merged_face_id;
5735 else
5737 /* Merge the escape-glyph face into the current face. */
5738 face_id = merge_faces (it->f, Qescape_glyph, 0,
5739 it->face_id);
5740 last_escape_glyph_frame = it->f;
5741 last_escape_glyph_face_id = it->face_id;
5742 last_escape_glyph_merged_face_id = face_id;
5745 /* Handle soft hyphens in the mode where they only get
5746 highlighting. */
5748 if (EQ (Vnobreak_char_display, Qt)
5749 && (it->c == 0x8ad || it->c == 0x92d
5750 || it->c == 0xe2d || it->c == 0xf2d))
5752 g = it->c = '-';
5753 XSETINT (it->ctl_chars[0], g);
5754 ctl_len = 1;
5755 goto display_control;
5758 /* Handle non-break space and soft hyphen
5759 with the escape glyph. */
5761 if (it->c == 0x8a0 || it->c == 0x8ad
5762 || it->c == 0x920 || it->c == 0x92d
5763 || it->c == 0xe20 || it->c == 0xe2d
5764 || it->c == 0xf20 || it->c == 0xf2d)
5766 XSETINT (it->ctl_chars[0], escape_glyph);
5767 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5768 XSETINT (it->ctl_chars[1], g);
5769 ctl_len = 2;
5770 goto display_control;
5774 unsigned char str[MAX_MULTIBYTE_LENGTH];
5775 int len;
5776 int i;
5778 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5779 if (SINGLE_BYTE_CHAR_P (it->c))
5780 str[0] = it->c, len = 1;
5781 else
5783 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5784 if (len < 0)
5786 /* It's an invalid character, which shouldn't
5787 happen actually, but due to bugs it may
5788 happen. Let's print the char as is, there's
5789 not much meaningful we can do with it. */
5790 str[0] = it->c;
5791 str[1] = it->c >> 8;
5792 str[2] = it->c >> 16;
5793 str[3] = it->c >> 24;
5794 len = 4;
5798 for (i = 0; i < len; i++)
5800 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5801 /* Insert three more glyphs into IT->ctl_chars for
5802 the octal display of the character. */
5803 g = ((str[i] >> 6) & 7) + '0';
5804 XSETINT (it->ctl_chars[i * 4 + 1], g);
5805 g = ((str[i] >> 3) & 7) + '0';
5806 XSETINT (it->ctl_chars[i * 4 + 2], g);
5807 g = (str[i] & 7) + '0';
5808 XSETINT (it->ctl_chars[i * 4 + 3], g);
5810 ctl_len = len * 4;
5813 display_control:
5814 /* Set up IT->dpvec and return first character from it. */
5815 it->dpvec_char_len = it->len;
5816 it->dpvec = it->ctl_chars;
5817 it->dpend = it->dpvec + ctl_len;
5818 it->current.dpvec_index = 0;
5819 it->dpvec_face_id = face_id;
5820 it->saved_face_id = it->face_id;
5821 it->method = GET_FROM_DISPLAY_VECTOR;
5822 it->ellipsis_p = 0;
5823 goto get_next;
5827 /* Adjust face id for a multibyte character. There are no
5828 multibyte character in unibyte text. */
5829 if (it->multibyte_p
5830 && success_p
5831 && FRAME_WINDOW_P (it->f))
5833 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5834 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5838 /* Is this character the last one of a run of characters with
5839 box? If yes, set IT->end_of_box_run_p to 1. */
5840 if (it->face_box_p
5841 && it->s == NULL)
5843 int face_id;
5844 struct face *face;
5846 it->end_of_box_run_p
5847 = ((face_id = face_after_it_pos (it),
5848 face_id != it->face_id)
5849 && (face = FACE_FROM_ID (it->f, face_id),
5850 face->box == FACE_NO_BOX));
5853 /* Value is 0 if end of buffer or string reached. */
5854 return success_p;
5858 /* Move IT to the next display element.
5860 RESEAT_P non-zero means if called on a newline in buffer text,
5861 skip to the next visible line start.
5863 Functions get_next_display_element and set_iterator_to_next are
5864 separate because I find this arrangement easier to handle than a
5865 get_next_display_element function that also increments IT's
5866 position. The way it is we can first look at an iterator's current
5867 display element, decide whether it fits on a line, and if it does,
5868 increment the iterator position. The other way around we probably
5869 would either need a flag indicating whether the iterator has to be
5870 incremented the next time, or we would have to implement a
5871 decrement position function which would not be easy to write. */
5873 void
5874 set_iterator_to_next (it, reseat_p)
5875 struct it *it;
5876 int reseat_p;
5878 /* Reset flags indicating start and end of a sequence of characters
5879 with box. Reset them at the start of this function because
5880 moving the iterator to a new position might set them. */
5881 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5883 switch (it->method)
5885 case GET_FROM_BUFFER:
5886 /* The current display element of IT is a character from
5887 current_buffer. Advance in the buffer, and maybe skip over
5888 invisible lines that are so because of selective display. */
5889 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5890 reseat_at_next_visible_line_start (it, 0);
5891 else
5893 xassert (it->len != 0);
5894 IT_BYTEPOS (*it) += it->len;
5895 IT_CHARPOS (*it) += 1;
5896 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5898 break;
5900 case GET_FROM_COMPOSITION:
5901 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5902 xassert (it->sp > 0);
5903 pop_it (it);
5904 if (it->method == GET_FROM_STRING)
5906 IT_STRING_BYTEPOS (*it) += it->len;
5907 IT_STRING_CHARPOS (*it) += it->cmp_len;
5908 goto consider_string_end;
5910 else if (it->method == GET_FROM_BUFFER)
5912 IT_BYTEPOS (*it) += it->len;
5913 IT_CHARPOS (*it) += it->cmp_len;
5915 break;
5917 case GET_FROM_C_STRING:
5918 /* Current display element of IT is from a C string. */
5919 IT_BYTEPOS (*it) += it->len;
5920 IT_CHARPOS (*it) += 1;
5921 break;
5923 case GET_FROM_DISPLAY_VECTOR:
5924 /* Current display element of IT is from a display table entry.
5925 Advance in the display table definition. Reset it to null if
5926 end reached, and continue with characters from buffers/
5927 strings. */
5928 ++it->current.dpvec_index;
5930 /* Restore face of the iterator to what they were before the
5931 display vector entry (these entries may contain faces). */
5932 it->face_id = it->saved_face_id;
5934 if (it->dpvec + it->current.dpvec_index == it->dpend)
5936 int recheck_faces = it->ellipsis_p;
5938 if (it->s)
5939 it->method = GET_FROM_C_STRING;
5940 else if (STRINGP (it->string))
5941 it->method = GET_FROM_STRING;
5942 else
5944 it->method = GET_FROM_BUFFER;
5945 it->object = it->w->buffer;
5948 it->dpvec = NULL;
5949 it->current.dpvec_index = -1;
5951 /* Skip over characters which were displayed via IT->dpvec. */
5952 if (it->dpvec_char_len < 0)
5953 reseat_at_next_visible_line_start (it, 1);
5954 else if (it->dpvec_char_len > 0)
5956 if (it->method == GET_FROM_STRING
5957 && it->n_overlay_strings > 0)
5958 it->ignore_overlay_strings_at_pos_p = 1;
5959 it->len = it->dpvec_char_len;
5960 set_iterator_to_next (it, reseat_p);
5963 /* Maybe recheck faces after display vector */
5964 if (recheck_faces)
5965 it->stop_charpos = IT_CHARPOS (*it);
5967 break;
5969 case GET_FROM_STRING:
5970 /* Current display element is a character from a Lisp string. */
5971 xassert (it->s == NULL && STRINGP (it->string));
5972 IT_STRING_BYTEPOS (*it) += it->len;
5973 IT_STRING_CHARPOS (*it) += 1;
5975 consider_string_end:
5977 if (it->current.overlay_string_index >= 0)
5979 /* IT->string is an overlay string. Advance to the
5980 next, if there is one. */
5981 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5982 next_overlay_string (it);
5984 else
5986 /* IT->string is not an overlay string. If we reached
5987 its end, and there is something on IT->stack, proceed
5988 with what is on the stack. This can be either another
5989 string, this time an overlay string, or a buffer. */
5990 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5991 && it->sp > 0)
5993 pop_it (it);
5994 if (it->method == GET_FROM_STRING)
5995 goto consider_string_end;
5998 break;
6000 case GET_FROM_IMAGE:
6001 case GET_FROM_STRETCH:
6002 /* The position etc with which we have to proceed are on
6003 the stack. The position may be at the end of a string,
6004 if the `display' property takes up the whole string. */
6005 xassert (it->sp > 0);
6006 pop_it (it);
6007 if (it->method == GET_FROM_STRING)
6008 goto consider_string_end;
6009 break;
6011 default:
6012 /* There are no other methods defined, so this should be a bug. */
6013 abort ();
6016 xassert (it->method != GET_FROM_STRING
6017 || (STRINGP (it->string)
6018 && IT_STRING_CHARPOS (*it) >= 0));
6021 /* Load IT's display element fields with information about the next
6022 display element which comes from a display table entry or from the
6023 result of translating a control character to one of the forms `^C'
6024 or `\003'.
6026 IT->dpvec holds the glyphs to return as characters.
6027 IT->saved_face_id holds the face id before the display vector--
6028 it is restored into IT->face_idin set_iterator_to_next. */
6030 static int
6031 next_element_from_display_vector (it)
6032 struct it *it;
6034 /* Precondition. */
6035 xassert (it->dpvec && it->current.dpvec_index >= 0);
6037 it->face_id = it->saved_face_id;
6039 if (INTEGERP (*it->dpvec)
6040 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6042 GLYPH g;
6044 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6045 it->c = FAST_GLYPH_CHAR (g);
6046 it->len = CHAR_BYTES (it->c);
6048 /* The entry may contain a face id to use. Such a face id is
6049 the id of a Lisp face, not a realized face. A face id of
6050 zero means no face is specified. */
6051 if (it->dpvec_face_id >= 0)
6052 it->face_id = it->dpvec_face_id;
6053 else
6055 int lface_id = FAST_GLYPH_FACE (g);
6056 if (lface_id > 0)
6057 it->face_id = merge_faces (it->f, Qt, lface_id,
6058 it->saved_face_id);
6061 else
6062 /* Display table entry is invalid. Return a space. */
6063 it->c = ' ', it->len = 1;
6065 /* Don't change position and object of the iterator here. They are
6066 still the values of the character that had this display table
6067 entry or was translated, and that's what we want. */
6068 it->what = IT_CHARACTER;
6069 return 1;
6073 /* Load IT with the next display element from Lisp string IT->string.
6074 IT->current.string_pos is the current position within the string.
6075 If IT->current.overlay_string_index >= 0, the Lisp string is an
6076 overlay string. */
6078 static int
6079 next_element_from_string (it)
6080 struct it *it;
6082 struct text_pos position;
6084 xassert (STRINGP (it->string));
6085 xassert (IT_STRING_CHARPOS (*it) >= 0);
6086 position = it->current.string_pos;
6088 /* Time to check for invisible text? */
6089 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6090 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6092 handle_stop (it);
6094 /* Since a handler may have changed IT->method, we must
6095 recurse here. */
6096 return get_next_display_element (it);
6099 if (it->current.overlay_string_index >= 0)
6101 /* Get the next character from an overlay string. In overlay
6102 strings, There is no field width or padding with spaces to
6103 do. */
6104 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6106 it->what = IT_EOB;
6107 return 0;
6109 else if (STRING_MULTIBYTE (it->string))
6111 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6112 const unsigned char *s = (SDATA (it->string)
6113 + IT_STRING_BYTEPOS (*it));
6114 it->c = string_char_and_length (s, remaining, &it->len);
6116 else
6118 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6119 it->len = 1;
6122 else
6124 /* Get the next character from a Lisp string that is not an
6125 overlay string. Such strings come from the mode line, for
6126 example. We may have to pad with spaces, or truncate the
6127 string. See also next_element_from_c_string. */
6128 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6130 it->what = IT_EOB;
6131 return 0;
6133 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6135 /* Pad with spaces. */
6136 it->c = ' ', it->len = 1;
6137 CHARPOS (position) = BYTEPOS (position) = -1;
6139 else if (STRING_MULTIBYTE (it->string))
6141 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6142 const unsigned char *s = (SDATA (it->string)
6143 + IT_STRING_BYTEPOS (*it));
6144 it->c = string_char_and_length (s, maxlen, &it->len);
6146 else
6148 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6149 it->len = 1;
6153 /* Record what we have and where it came from. */
6154 it->what = IT_CHARACTER;
6155 it->object = it->string;
6156 it->position = position;
6157 return 1;
6161 /* Load IT with next display element from C string IT->s.
6162 IT->string_nchars is the maximum number of characters to return
6163 from the string. IT->end_charpos may be greater than
6164 IT->string_nchars when this function is called, in which case we
6165 may have to return padding spaces. Value is zero if end of string
6166 reached, including padding spaces. */
6168 static int
6169 next_element_from_c_string (it)
6170 struct it *it;
6172 int success_p = 1;
6174 xassert (it->s);
6175 it->what = IT_CHARACTER;
6176 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6177 it->object = Qnil;
6179 /* IT's position can be greater IT->string_nchars in case a field
6180 width or precision has been specified when the iterator was
6181 initialized. */
6182 if (IT_CHARPOS (*it) >= it->end_charpos)
6184 /* End of the game. */
6185 it->what = IT_EOB;
6186 success_p = 0;
6188 else if (IT_CHARPOS (*it) >= it->string_nchars)
6190 /* Pad with spaces. */
6191 it->c = ' ', it->len = 1;
6192 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6194 else if (it->multibyte_p)
6196 /* Implementation note: The calls to strlen apparently aren't a
6197 performance problem because there is no noticeable performance
6198 difference between Emacs running in unibyte or multibyte mode. */
6199 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6200 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6201 maxlen, &it->len);
6203 else
6204 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6206 return success_p;
6210 /* Set up IT to return characters from an ellipsis, if appropriate.
6211 The definition of the ellipsis glyphs may come from a display table
6212 entry. This function Fills IT with the first glyph from the
6213 ellipsis if an ellipsis is to be displayed. */
6215 static int
6216 next_element_from_ellipsis (it)
6217 struct it *it;
6219 if (it->selective_display_ellipsis_p)
6220 setup_for_ellipsis (it, it->len);
6221 else
6223 /* The face at the current position may be different from the
6224 face we find after the invisible text. Remember what it
6225 was in IT->saved_face_id, and signal that it's there by
6226 setting face_before_selective_p. */
6227 it->saved_face_id = it->face_id;
6228 it->method = GET_FROM_BUFFER;
6229 it->object = it->w->buffer;
6230 reseat_at_next_visible_line_start (it, 1);
6231 it->face_before_selective_p = 1;
6234 return get_next_display_element (it);
6238 /* Deliver an image display element. The iterator IT is already
6239 filled with image information (done in handle_display_prop). Value
6240 is always 1. */
6243 static int
6244 next_element_from_image (it)
6245 struct it *it;
6247 it->what = IT_IMAGE;
6248 return 1;
6252 /* Fill iterator IT with next display element from a stretch glyph
6253 property. IT->object is the value of the text property. Value is
6254 always 1. */
6256 static int
6257 next_element_from_stretch (it)
6258 struct it *it;
6260 it->what = IT_STRETCH;
6261 return 1;
6265 /* Load IT with the next display element from current_buffer. Value
6266 is zero if end of buffer reached. IT->stop_charpos is the next
6267 position at which to stop and check for text properties or buffer
6268 end. */
6270 static int
6271 next_element_from_buffer (it)
6272 struct it *it;
6274 int success_p = 1;
6276 /* Check this assumption, otherwise, we would never enter the
6277 if-statement, below. */
6278 xassert (IT_CHARPOS (*it) >= BEGV
6279 && IT_CHARPOS (*it) <= it->stop_charpos);
6281 if (IT_CHARPOS (*it) >= it->stop_charpos)
6283 if (IT_CHARPOS (*it) >= it->end_charpos)
6285 int overlay_strings_follow_p;
6287 /* End of the game, except when overlay strings follow that
6288 haven't been returned yet. */
6289 if (it->overlay_strings_at_end_processed_p)
6290 overlay_strings_follow_p = 0;
6291 else
6293 it->overlay_strings_at_end_processed_p = 1;
6294 overlay_strings_follow_p = get_overlay_strings (it, 0);
6297 if (overlay_strings_follow_p)
6298 success_p = get_next_display_element (it);
6299 else
6301 it->what = IT_EOB;
6302 it->position = it->current.pos;
6303 success_p = 0;
6306 else
6308 handle_stop (it);
6309 return get_next_display_element (it);
6312 else
6314 /* No face changes, overlays etc. in sight, so just return a
6315 character from current_buffer. */
6316 unsigned char *p;
6318 /* Maybe run the redisplay end trigger hook. Performance note:
6319 This doesn't seem to cost measurable time. */
6320 if (it->redisplay_end_trigger_charpos
6321 && it->glyph_row
6322 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6323 run_redisplay_end_trigger_hook (it);
6325 /* Get the next character, maybe multibyte. */
6326 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6327 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6329 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6330 - IT_BYTEPOS (*it));
6331 it->c = string_char_and_length (p, maxlen, &it->len);
6333 else
6334 it->c = *p, it->len = 1;
6336 /* Record what we have and where it came from. */
6337 it->what = IT_CHARACTER;
6338 it->object = it->w->buffer;
6339 it->position = it->current.pos;
6341 /* Normally we return the character found above, except when we
6342 really want to return an ellipsis for selective display. */
6343 if (it->selective)
6345 if (it->c == '\n')
6347 /* A value of selective > 0 means hide lines indented more
6348 than that number of columns. */
6349 if (it->selective > 0
6350 && IT_CHARPOS (*it) + 1 < ZV
6351 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6352 IT_BYTEPOS (*it) + 1,
6353 (double) it->selective)) /* iftc */
6355 success_p = next_element_from_ellipsis (it);
6356 it->dpvec_char_len = -1;
6359 else if (it->c == '\r' && it->selective == -1)
6361 /* A value of selective == -1 means that everything from the
6362 CR to the end of the line is invisible, with maybe an
6363 ellipsis displayed for it. */
6364 success_p = next_element_from_ellipsis (it);
6365 it->dpvec_char_len = -1;
6370 /* Value is zero if end of buffer reached. */
6371 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6372 return success_p;
6376 /* Run the redisplay end trigger hook for IT. */
6378 static void
6379 run_redisplay_end_trigger_hook (it)
6380 struct it *it;
6382 Lisp_Object args[3];
6384 /* IT->glyph_row should be non-null, i.e. we should be actually
6385 displaying something, or otherwise we should not run the hook. */
6386 xassert (it->glyph_row);
6388 /* Set up hook arguments. */
6389 args[0] = Qredisplay_end_trigger_functions;
6390 args[1] = it->window;
6391 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6392 it->redisplay_end_trigger_charpos = 0;
6394 /* Since we are *trying* to run these functions, don't try to run
6395 them again, even if they get an error. */
6396 it->w->redisplay_end_trigger = Qnil;
6397 Frun_hook_with_args (3, args);
6399 /* Notice if it changed the face of the character we are on. */
6400 handle_face_prop (it);
6404 /* Deliver a composition display element. The iterator IT is already
6405 filled with composition information (done in
6406 handle_composition_prop). Value is always 1. */
6408 static int
6409 next_element_from_composition (it)
6410 struct it *it;
6412 it->what = IT_COMPOSITION;
6413 it->position = (STRINGP (it->string)
6414 ? it->current.string_pos
6415 : it->current.pos);
6416 if (STRINGP (it->string))
6417 it->object = it->string;
6418 else
6419 it->object = it->w->buffer;
6420 return 1;
6425 /***********************************************************************
6426 Moving an iterator without producing glyphs
6427 ***********************************************************************/
6429 /* Check if iterator is at a position corresponding to a valid buffer
6430 position after some move_it_ call. */
6432 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6433 ((it)->method == GET_FROM_STRING \
6434 ? IT_STRING_CHARPOS (*it) == 0 \
6435 : 1)
6438 /* Move iterator IT to a specified buffer or X position within one
6439 line on the display without producing glyphs.
6441 OP should be a bit mask including some or all of these bits:
6442 MOVE_TO_X: Stop on reaching x-position TO_X.
6443 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6444 Regardless of OP's value, stop in reaching the end of the display line.
6446 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6447 This means, in particular, that TO_X includes window's horizontal
6448 scroll amount.
6450 The return value has several possible values that
6451 say what condition caused the scan to stop:
6453 MOVE_POS_MATCH_OR_ZV
6454 - when TO_POS or ZV was reached.
6456 MOVE_X_REACHED
6457 -when TO_X was reached before TO_POS or ZV were reached.
6459 MOVE_LINE_CONTINUED
6460 - when we reached the end of the display area and the line must
6461 be continued.
6463 MOVE_LINE_TRUNCATED
6464 - when we reached the end of the display area and the line is
6465 truncated.
6467 MOVE_NEWLINE_OR_CR
6468 - when we stopped at a line end, i.e. a newline or a CR and selective
6469 display is on. */
6471 static enum move_it_result
6472 move_it_in_display_line_to (it, to_charpos, to_x, op)
6473 struct it *it;
6474 int to_charpos, to_x, op;
6476 enum move_it_result result = MOVE_UNDEFINED;
6477 struct glyph_row *saved_glyph_row;
6479 /* Don't produce glyphs in produce_glyphs. */
6480 saved_glyph_row = it->glyph_row;
6481 it->glyph_row = NULL;
6483 #define BUFFER_POS_REACHED_P() \
6484 ((op & MOVE_TO_POS) != 0 \
6485 && BUFFERP (it->object) \
6486 && IT_CHARPOS (*it) >= to_charpos \
6487 && (it->method == GET_FROM_BUFFER \
6488 || (it->method == GET_FROM_DISPLAY_VECTOR \
6489 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6492 while (1)
6494 int x, i, ascent = 0, descent = 0;
6496 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6497 if ((op & MOVE_TO_POS) != 0
6498 && BUFFERP (it->object)
6499 && it->method == GET_FROM_BUFFER
6500 && IT_CHARPOS (*it) > to_charpos)
6502 result = MOVE_POS_MATCH_OR_ZV;
6503 break;
6506 /* Stop when ZV reached.
6507 We used to stop here when TO_CHARPOS reached as well, but that is
6508 too soon if this glyph does not fit on this line. So we handle it
6509 explicitly below. */
6510 if (!get_next_display_element (it)
6511 || (it->truncate_lines_p
6512 && BUFFER_POS_REACHED_P ()))
6514 result = MOVE_POS_MATCH_OR_ZV;
6515 break;
6518 /* The call to produce_glyphs will get the metrics of the
6519 display element IT is loaded with. We record in x the
6520 x-position before this display element in case it does not
6521 fit on the line. */
6522 x = it->current_x;
6524 /* Remember the line height so far in case the next element doesn't
6525 fit on the line. */
6526 if (!it->truncate_lines_p)
6528 ascent = it->max_ascent;
6529 descent = it->max_descent;
6532 PRODUCE_GLYPHS (it);
6534 if (it->area != TEXT_AREA)
6536 set_iterator_to_next (it, 1);
6537 continue;
6540 /* The number of glyphs we get back in IT->nglyphs will normally
6541 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6542 character on a terminal frame, or (iii) a line end. For the
6543 second case, IT->nglyphs - 1 padding glyphs will be present
6544 (on X frames, there is only one glyph produced for a
6545 composite character.
6547 The behavior implemented below means, for continuation lines,
6548 that as many spaces of a TAB as fit on the current line are
6549 displayed there. For terminal frames, as many glyphs of a
6550 multi-glyph character are displayed in the current line, too.
6551 This is what the old redisplay code did, and we keep it that
6552 way. Under X, the whole shape of a complex character must
6553 fit on the line or it will be completely displayed in the
6554 next line.
6556 Note that both for tabs and padding glyphs, all glyphs have
6557 the same width. */
6558 if (it->nglyphs)
6560 /* More than one glyph or glyph doesn't fit on line. All
6561 glyphs have the same width. */
6562 int single_glyph_width = it->pixel_width / it->nglyphs;
6563 int new_x;
6564 int x_before_this_char = x;
6565 int hpos_before_this_char = it->hpos;
6567 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6569 new_x = x + single_glyph_width;
6571 /* We want to leave anything reaching TO_X to the caller. */
6572 if ((op & MOVE_TO_X) && new_x > to_x)
6574 if (BUFFER_POS_REACHED_P ())
6575 goto buffer_pos_reached;
6576 it->current_x = x;
6577 result = MOVE_X_REACHED;
6578 break;
6580 else if (/* Lines are continued. */
6581 !it->truncate_lines_p
6582 && (/* And glyph doesn't fit on the line. */
6583 new_x > it->last_visible_x
6584 /* Or it fits exactly and we're on a window
6585 system frame. */
6586 || (new_x == it->last_visible_x
6587 && FRAME_WINDOW_P (it->f))))
6589 if (/* IT->hpos == 0 means the very first glyph
6590 doesn't fit on the line, e.g. a wide image. */
6591 it->hpos == 0
6592 || (new_x == it->last_visible_x
6593 && FRAME_WINDOW_P (it->f)))
6595 ++it->hpos;
6596 it->current_x = new_x;
6598 /* The character's last glyph just barely fits
6599 in this row. */
6600 if (i == it->nglyphs - 1)
6602 /* If this is the destination position,
6603 return a position *before* it in this row,
6604 now that we know it fits in this row. */
6605 if (BUFFER_POS_REACHED_P ())
6607 it->hpos = hpos_before_this_char;
6608 it->current_x = x_before_this_char;
6609 result = MOVE_POS_MATCH_OR_ZV;
6610 break;
6613 set_iterator_to_next (it, 1);
6614 #ifdef HAVE_WINDOW_SYSTEM
6615 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6617 if (!get_next_display_element (it))
6619 result = MOVE_POS_MATCH_OR_ZV;
6620 break;
6622 if (BUFFER_POS_REACHED_P ())
6624 if (ITERATOR_AT_END_OF_LINE_P (it))
6625 result = MOVE_POS_MATCH_OR_ZV;
6626 else
6627 result = MOVE_LINE_CONTINUED;
6628 break;
6630 if (ITERATOR_AT_END_OF_LINE_P (it))
6632 result = MOVE_NEWLINE_OR_CR;
6633 break;
6636 #endif /* HAVE_WINDOW_SYSTEM */
6639 else
6641 it->current_x = x;
6642 it->max_ascent = ascent;
6643 it->max_descent = descent;
6646 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6647 IT_CHARPOS (*it)));
6648 result = MOVE_LINE_CONTINUED;
6649 break;
6651 else if (BUFFER_POS_REACHED_P ())
6652 goto buffer_pos_reached;
6653 else if (new_x > it->first_visible_x)
6655 /* Glyph is visible. Increment number of glyphs that
6656 would be displayed. */
6657 ++it->hpos;
6659 else
6661 /* Glyph is completely off the left margin of the display
6662 area. Nothing to do. */
6666 if (result != MOVE_UNDEFINED)
6667 break;
6669 else if (BUFFER_POS_REACHED_P ())
6671 buffer_pos_reached:
6672 it->current_x = x;
6673 it->max_ascent = ascent;
6674 it->max_descent = descent;
6675 result = MOVE_POS_MATCH_OR_ZV;
6676 break;
6678 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6680 /* Stop when TO_X specified and reached. This check is
6681 necessary here because of lines consisting of a line end,
6682 only. The line end will not produce any glyphs and we
6683 would never get MOVE_X_REACHED. */
6684 xassert (it->nglyphs == 0);
6685 result = MOVE_X_REACHED;
6686 break;
6689 /* Is this a line end? If yes, we're done. */
6690 if (ITERATOR_AT_END_OF_LINE_P (it))
6692 result = MOVE_NEWLINE_OR_CR;
6693 break;
6696 /* The current display element has been consumed. Advance
6697 to the next. */
6698 set_iterator_to_next (it, 1);
6700 /* Stop if lines are truncated and IT's current x-position is
6701 past the right edge of the window now. */
6702 if (it->truncate_lines_p
6703 && it->current_x >= it->last_visible_x)
6705 #ifdef HAVE_WINDOW_SYSTEM
6706 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6708 if (!get_next_display_element (it)
6709 || BUFFER_POS_REACHED_P ())
6711 result = MOVE_POS_MATCH_OR_ZV;
6712 break;
6714 if (ITERATOR_AT_END_OF_LINE_P (it))
6716 result = MOVE_NEWLINE_OR_CR;
6717 break;
6720 #endif /* HAVE_WINDOW_SYSTEM */
6721 result = MOVE_LINE_TRUNCATED;
6722 break;
6726 #undef BUFFER_POS_REACHED_P
6728 /* Restore the iterator settings altered at the beginning of this
6729 function. */
6730 it->glyph_row = saved_glyph_row;
6731 return result;
6735 /* Move IT forward until it satisfies one or more of the criteria in
6736 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6738 OP is a bit-mask that specifies where to stop, and in particular,
6739 which of those four position arguments makes a difference. See the
6740 description of enum move_operation_enum.
6742 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6743 screen line, this function will set IT to the next position >
6744 TO_CHARPOS. */
6746 void
6747 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6748 struct it *it;
6749 int to_charpos, to_x, to_y, to_vpos;
6750 int op;
6752 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6753 int line_height;
6754 int reached = 0;
6756 for (;;)
6758 if (op & MOVE_TO_VPOS)
6760 /* If no TO_CHARPOS and no TO_X specified, stop at the
6761 start of the line TO_VPOS. */
6762 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6764 if (it->vpos == to_vpos)
6766 reached = 1;
6767 break;
6769 else
6770 skip = move_it_in_display_line_to (it, -1, -1, 0);
6772 else
6774 /* TO_VPOS >= 0 means stop at TO_X in the line at
6775 TO_VPOS, or at TO_POS, whichever comes first. */
6776 if (it->vpos == to_vpos)
6778 reached = 2;
6779 break;
6782 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6784 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6786 reached = 3;
6787 break;
6789 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6791 /* We have reached TO_X but not in the line we want. */
6792 skip = move_it_in_display_line_to (it, to_charpos,
6793 -1, MOVE_TO_POS);
6794 if (skip == MOVE_POS_MATCH_OR_ZV)
6796 reached = 4;
6797 break;
6802 else if (op & MOVE_TO_Y)
6804 struct it it_backup;
6806 /* TO_Y specified means stop at TO_X in the line containing
6807 TO_Y---or at TO_CHARPOS if this is reached first. The
6808 problem is that we can't really tell whether the line
6809 contains TO_Y before we have completely scanned it, and
6810 this may skip past TO_X. What we do is to first scan to
6811 TO_X.
6813 If TO_X is not specified, use a TO_X of zero. The reason
6814 is to make the outcome of this function more predictable.
6815 If we didn't use TO_X == 0, we would stop at the end of
6816 the line which is probably not what a caller would expect
6817 to happen. */
6818 skip = move_it_in_display_line_to (it, to_charpos,
6819 ((op & MOVE_TO_X)
6820 ? to_x : 0),
6821 (MOVE_TO_X
6822 | (op & MOVE_TO_POS)));
6824 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6825 if (skip == MOVE_POS_MATCH_OR_ZV)
6827 reached = 5;
6828 break;
6831 /* If TO_X was reached, we would like to know whether TO_Y
6832 is in the line. This can only be said if we know the
6833 total line height which requires us to scan the rest of
6834 the line. */
6835 if (skip == MOVE_X_REACHED)
6837 it_backup = *it;
6838 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6839 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6840 op & MOVE_TO_POS);
6841 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6844 /* Now, decide whether TO_Y is in this line. */
6845 line_height = it->max_ascent + it->max_descent;
6846 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6848 if (to_y >= it->current_y
6849 && to_y < it->current_y + line_height)
6851 if (skip == MOVE_X_REACHED)
6852 /* If TO_Y is in this line and TO_X was reached above,
6853 we scanned too far. We have to restore IT's settings
6854 to the ones before skipping. */
6855 *it = it_backup;
6856 reached = 6;
6858 else if (skip == MOVE_X_REACHED)
6860 skip = skip2;
6861 if (skip == MOVE_POS_MATCH_OR_ZV)
6862 reached = 7;
6865 if (reached)
6866 break;
6868 else if (BUFFERP (it->object)
6869 && it->method == GET_FROM_BUFFER
6870 && IT_CHARPOS (*it) >= to_charpos)
6871 skip = MOVE_POS_MATCH_OR_ZV;
6872 else
6873 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6875 switch (skip)
6877 case MOVE_POS_MATCH_OR_ZV:
6878 reached = 8;
6879 goto out;
6881 case MOVE_NEWLINE_OR_CR:
6882 set_iterator_to_next (it, 1);
6883 it->continuation_lines_width = 0;
6884 break;
6886 case MOVE_LINE_TRUNCATED:
6887 it->continuation_lines_width = 0;
6888 reseat_at_next_visible_line_start (it, 0);
6889 if ((op & MOVE_TO_POS) != 0
6890 && IT_CHARPOS (*it) > to_charpos)
6892 reached = 9;
6893 goto out;
6895 break;
6897 case MOVE_LINE_CONTINUED:
6898 /* For continued lines ending in a tab, some of the glyphs
6899 associated with the tab are displayed on the current
6900 line. Since it->current_x does not include these glyphs,
6901 we use it->last_visible_x instead. */
6902 it->continuation_lines_width +=
6903 (it->c == '\t') ? it->last_visible_x : it->current_x;
6904 break;
6906 default:
6907 abort ();
6910 /* Reset/increment for the next run. */
6911 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6912 it->current_x = it->hpos = 0;
6913 it->current_y += it->max_ascent + it->max_descent;
6914 ++it->vpos;
6915 last_height = it->max_ascent + it->max_descent;
6916 last_max_ascent = it->max_ascent;
6917 it->max_ascent = it->max_descent = 0;
6920 out:
6922 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6926 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6928 If DY > 0, move IT backward at least that many pixels. DY = 0
6929 means move IT backward to the preceding line start or BEGV. This
6930 function may move over more than DY pixels if IT->current_y - DY
6931 ends up in the middle of a line; in this case IT->current_y will be
6932 set to the top of the line moved to. */
6934 void
6935 move_it_vertically_backward (it, dy)
6936 struct it *it;
6937 int dy;
6939 int nlines, h;
6940 struct it it2, it3;
6941 int start_pos;
6943 move_further_back:
6944 xassert (dy >= 0);
6946 start_pos = IT_CHARPOS (*it);
6948 /* Estimate how many newlines we must move back. */
6949 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6951 /* Set the iterator's position that many lines back. */
6952 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6953 back_to_previous_visible_line_start (it);
6955 /* Reseat the iterator here. When moving backward, we don't want
6956 reseat to skip forward over invisible text, set up the iterator
6957 to deliver from overlay strings at the new position etc. So,
6958 use reseat_1 here. */
6959 reseat_1 (it, it->current.pos, 1);
6961 /* We are now surely at a line start. */
6962 it->current_x = it->hpos = 0;
6963 it->continuation_lines_width = 0;
6965 /* Move forward and see what y-distance we moved. First move to the
6966 start of the next line so that we get its height. We need this
6967 height to be able to tell whether we reached the specified
6968 y-distance. */
6969 it2 = *it;
6970 it2.max_ascent = it2.max_descent = 0;
6973 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6974 MOVE_TO_POS | MOVE_TO_VPOS);
6976 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6977 xassert (IT_CHARPOS (*it) >= BEGV);
6978 it3 = it2;
6980 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6981 xassert (IT_CHARPOS (*it) >= BEGV);
6982 /* H is the actual vertical distance from the position in *IT
6983 and the starting position. */
6984 h = it2.current_y - it->current_y;
6985 /* NLINES is the distance in number of lines. */
6986 nlines = it2.vpos - it->vpos;
6988 /* Correct IT's y and vpos position
6989 so that they are relative to the starting point. */
6990 it->vpos -= nlines;
6991 it->current_y -= h;
6993 if (dy == 0)
6995 /* DY == 0 means move to the start of the screen line. The
6996 value of nlines is > 0 if continuation lines were involved. */
6997 if (nlines > 0)
6998 move_it_by_lines (it, nlines, 1);
6999 #if 0
7000 /* I think this assert is bogus if buffer contains
7001 invisible text or images. KFS. */
7002 xassert (IT_CHARPOS (*it) <= start_pos);
7003 #endif
7005 else
7007 /* The y-position we try to reach, relative to *IT.
7008 Note that H has been subtracted in front of the if-statement. */
7009 int target_y = it->current_y + h - dy;
7010 int y0 = it3.current_y;
7011 int y1 = line_bottom_y (&it3);
7012 int line_height = y1 - y0;
7014 /* If we did not reach target_y, try to move further backward if
7015 we can. If we moved too far backward, try to move forward. */
7016 if (target_y < it->current_y
7017 /* This is heuristic. In a window that's 3 lines high, with
7018 a line height of 13 pixels each, recentering with point
7019 on the bottom line will try to move -39/2 = 19 pixels
7020 backward. Try to avoid moving into the first line. */
7021 && (it->current_y - target_y
7022 > min (window_box_height (it->w), line_height * 2 / 3))
7023 && IT_CHARPOS (*it) > BEGV)
7025 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7026 target_y - it->current_y));
7027 dy = it->current_y - target_y;
7028 goto move_further_back;
7030 else if (target_y >= it->current_y + line_height
7031 && IT_CHARPOS (*it) < ZV)
7033 /* Should move forward by at least one line, maybe more.
7035 Note: Calling move_it_by_lines can be expensive on
7036 terminal frames, where compute_motion is used (via
7037 vmotion) to do the job, when there are very long lines
7038 and truncate-lines is nil. That's the reason for
7039 treating terminal frames specially here. */
7041 if (!FRAME_WINDOW_P (it->f))
7042 move_it_vertically (it, target_y - (it->current_y + line_height));
7043 else
7047 move_it_by_lines (it, 1, 1);
7049 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7052 #if 0
7053 /* I think this assert is bogus if buffer contains
7054 invisible text or images. KFS. */
7055 xassert (IT_CHARPOS (*it) >= BEGV);
7056 #endif
7062 /* Move IT by a specified amount of pixel lines DY. DY negative means
7063 move backwards. DY = 0 means move to start of screen line. At the
7064 end, IT will be on the start of a screen line. */
7066 void
7067 move_it_vertically (it, dy)
7068 struct it *it;
7069 int dy;
7071 if (dy <= 0)
7072 move_it_vertically_backward (it, -dy);
7073 else
7075 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7076 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7077 MOVE_TO_POS | MOVE_TO_Y);
7078 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7080 /* If buffer ends in ZV without a newline, move to the start of
7081 the line to satisfy the post-condition. */
7082 if (IT_CHARPOS (*it) == ZV
7083 && ZV > BEGV
7084 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7085 move_it_by_lines (it, 0, 0);
7090 /* Move iterator IT past the end of the text line it is in. */
7092 void
7093 move_it_past_eol (it)
7094 struct it *it;
7096 enum move_it_result rc;
7098 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7099 if (rc == MOVE_NEWLINE_OR_CR)
7100 set_iterator_to_next (it, 0);
7104 #if 0 /* Currently not used. */
7106 /* Return non-zero if some text between buffer positions START_CHARPOS
7107 and END_CHARPOS is invisible. IT->window is the window for text
7108 property lookup. */
7110 static int
7111 invisible_text_between_p (it, start_charpos, end_charpos)
7112 struct it *it;
7113 int start_charpos, end_charpos;
7115 Lisp_Object prop, limit;
7116 int invisible_found_p;
7118 xassert (it != NULL && start_charpos <= end_charpos);
7120 /* Is text at START invisible? */
7121 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7122 it->window);
7123 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7124 invisible_found_p = 1;
7125 else
7127 limit = Fnext_single_char_property_change (make_number (start_charpos),
7128 Qinvisible, Qnil,
7129 make_number (end_charpos));
7130 invisible_found_p = XFASTINT (limit) < end_charpos;
7133 return invisible_found_p;
7136 #endif /* 0 */
7139 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7140 negative means move up. DVPOS == 0 means move to the start of the
7141 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7142 NEED_Y_P is zero, IT->current_y will be left unchanged.
7144 Further optimization ideas: If we would know that IT->f doesn't use
7145 a face with proportional font, we could be faster for
7146 truncate-lines nil. */
7148 void
7149 move_it_by_lines (it, dvpos, need_y_p)
7150 struct it *it;
7151 int dvpos, need_y_p;
7153 struct position pos;
7155 /* The commented-out optimization uses vmotion on terminals. This
7156 gives bad results, because elements like it->what, on which
7157 callers such as pos_visible_p rely, aren't updated. */
7158 /* if (!FRAME_WINDOW_P (it->f))
7160 struct text_pos textpos;
7162 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7163 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7164 reseat (it, textpos, 1);
7165 it->vpos += pos.vpos;
7166 it->current_y += pos.vpos;
7168 else */
7170 if (dvpos == 0)
7172 /* DVPOS == 0 means move to the start of the screen line. */
7173 move_it_vertically_backward (it, 0);
7174 xassert (it->current_x == 0 && it->hpos == 0);
7175 /* Let next call to line_bottom_y calculate real line height */
7176 last_height = 0;
7178 else if (dvpos > 0)
7180 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7181 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7182 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7184 else
7186 struct it it2;
7187 int start_charpos, i;
7189 /* Start at the beginning of the screen line containing IT's
7190 position. This may actually move vertically backwards,
7191 in case of overlays, so adjust dvpos accordingly. */
7192 dvpos += it->vpos;
7193 move_it_vertically_backward (it, 0);
7194 dvpos -= it->vpos;
7196 /* Go back -DVPOS visible lines and reseat the iterator there. */
7197 start_charpos = IT_CHARPOS (*it);
7198 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7199 back_to_previous_visible_line_start (it);
7200 reseat (it, it->current.pos, 1);
7202 /* Move further back if we end up in a string or an image. */
7203 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7205 /* First try to move to start of display line. */
7206 dvpos += it->vpos;
7207 move_it_vertically_backward (it, 0);
7208 dvpos -= it->vpos;
7209 if (IT_POS_VALID_AFTER_MOVE_P (it))
7210 break;
7211 /* If start of line is still in string or image,
7212 move further back. */
7213 back_to_previous_visible_line_start (it);
7214 reseat (it, it->current.pos, 1);
7215 dvpos--;
7218 it->current_x = it->hpos = 0;
7220 /* Above call may have moved too far if continuation lines
7221 are involved. Scan forward and see if it did. */
7222 it2 = *it;
7223 it2.vpos = it2.current_y = 0;
7224 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7225 it->vpos -= it2.vpos;
7226 it->current_y -= it2.current_y;
7227 it->current_x = it->hpos = 0;
7229 /* If we moved too far back, move IT some lines forward. */
7230 if (it2.vpos > -dvpos)
7232 int delta = it2.vpos + dvpos;
7233 it2 = *it;
7234 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7235 /* Move back again if we got too far ahead. */
7236 if (IT_CHARPOS (*it) >= start_charpos)
7237 *it = it2;
7242 /* Return 1 if IT points into the middle of a display vector. */
7245 in_display_vector_p (it)
7246 struct it *it;
7248 return (it->method == GET_FROM_DISPLAY_VECTOR
7249 && it->current.dpvec_index > 0
7250 && it->dpvec + it->current.dpvec_index != it->dpend);
7254 /***********************************************************************
7255 Messages
7256 ***********************************************************************/
7259 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7260 to *Messages*. */
7262 void
7263 add_to_log (format, arg1, arg2)
7264 char *format;
7265 Lisp_Object arg1, arg2;
7267 Lisp_Object args[3];
7268 Lisp_Object msg, fmt;
7269 char *buffer;
7270 int len;
7271 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7272 USE_SAFE_ALLOCA;
7274 /* Do nothing if called asynchronously. Inserting text into
7275 a buffer may call after-change-functions and alike and
7276 that would means running Lisp asynchronously. */
7277 if (handling_signal)
7278 return;
7280 fmt = msg = Qnil;
7281 GCPRO4 (fmt, msg, arg1, arg2);
7283 args[0] = fmt = build_string (format);
7284 args[1] = arg1;
7285 args[2] = arg2;
7286 msg = Fformat (3, args);
7288 len = SBYTES (msg) + 1;
7289 SAFE_ALLOCA (buffer, char *, len);
7290 bcopy (SDATA (msg), buffer, len);
7292 message_dolog (buffer, len - 1, 1, 0);
7293 SAFE_FREE ();
7295 UNGCPRO;
7299 /* Output a newline in the *Messages* buffer if "needs" one. */
7301 void
7302 message_log_maybe_newline ()
7304 if (message_log_need_newline)
7305 message_dolog ("", 0, 1, 0);
7309 /* Add a string M of length NBYTES to the message log, optionally
7310 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7311 nonzero, means interpret the contents of M as multibyte. This
7312 function calls low-level routines in order to bypass text property
7313 hooks, etc. which might not be safe to run.
7315 This may GC (insert may run before/after change hooks),
7316 so the buffer M must NOT point to a Lisp string. */
7318 void
7319 message_dolog (m, nbytes, nlflag, multibyte)
7320 const char *m;
7321 int nbytes, nlflag, multibyte;
7323 if (!NILP (Vmemory_full))
7324 return;
7326 if (!NILP (Vmessage_log_max))
7328 struct buffer *oldbuf;
7329 Lisp_Object oldpoint, oldbegv, oldzv;
7330 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7331 int point_at_end = 0;
7332 int zv_at_end = 0;
7333 Lisp_Object old_deactivate_mark, tem;
7334 struct gcpro gcpro1;
7336 old_deactivate_mark = Vdeactivate_mark;
7337 oldbuf = current_buffer;
7338 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7339 current_buffer->undo_list = Qt;
7341 oldpoint = message_dolog_marker1;
7342 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7343 oldbegv = message_dolog_marker2;
7344 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7345 oldzv = message_dolog_marker3;
7346 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7347 GCPRO1 (old_deactivate_mark);
7349 if (PT == Z)
7350 point_at_end = 1;
7351 if (ZV == Z)
7352 zv_at_end = 1;
7354 BEGV = BEG;
7355 BEGV_BYTE = BEG_BYTE;
7356 ZV = Z;
7357 ZV_BYTE = Z_BYTE;
7358 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7360 /* Insert the string--maybe converting multibyte to single byte
7361 or vice versa, so that all the text fits the buffer. */
7362 if (multibyte
7363 && NILP (current_buffer->enable_multibyte_characters))
7365 int i, c, char_bytes;
7366 unsigned char work[1];
7368 /* Convert a multibyte string to single-byte
7369 for the *Message* buffer. */
7370 for (i = 0; i < nbytes; i += char_bytes)
7372 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7373 work[0] = (SINGLE_BYTE_CHAR_P (c)
7375 : multibyte_char_to_unibyte (c, Qnil));
7376 insert_1_both (work, 1, 1, 1, 0, 0);
7379 else if (! multibyte
7380 && ! NILP (current_buffer->enable_multibyte_characters))
7382 int i, c, char_bytes;
7383 unsigned char *msg = (unsigned char *) m;
7384 unsigned char str[MAX_MULTIBYTE_LENGTH];
7385 /* Convert a single-byte string to multibyte
7386 for the *Message* buffer. */
7387 for (i = 0; i < nbytes; i++)
7389 c = unibyte_char_to_multibyte (msg[i]);
7390 char_bytes = CHAR_STRING (c, str);
7391 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7394 else if (nbytes)
7395 insert_1 (m, nbytes, 1, 0, 0);
7397 if (nlflag)
7399 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7400 insert_1 ("\n", 1, 1, 0, 0);
7402 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7403 this_bol = PT;
7404 this_bol_byte = PT_BYTE;
7406 /* See if this line duplicates the previous one.
7407 If so, combine duplicates. */
7408 if (this_bol > BEG)
7410 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7411 prev_bol = PT;
7412 prev_bol_byte = PT_BYTE;
7414 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7415 this_bol, this_bol_byte);
7416 if (dup)
7418 del_range_both (prev_bol, prev_bol_byte,
7419 this_bol, this_bol_byte, 0);
7420 if (dup > 1)
7422 char dupstr[40];
7423 int duplen;
7425 /* If you change this format, don't forget to also
7426 change message_log_check_duplicate. */
7427 sprintf (dupstr, " [%d times]", dup);
7428 duplen = strlen (dupstr);
7429 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7430 insert_1 (dupstr, duplen, 1, 0, 1);
7435 /* If we have more than the desired maximum number of lines
7436 in the *Messages* buffer now, delete the oldest ones.
7437 This is safe because we don't have undo in this buffer. */
7439 if (NATNUMP (Vmessage_log_max))
7441 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7442 -XFASTINT (Vmessage_log_max) - 1, 0);
7443 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7446 BEGV = XMARKER (oldbegv)->charpos;
7447 BEGV_BYTE = marker_byte_position (oldbegv);
7449 if (zv_at_end)
7451 ZV = Z;
7452 ZV_BYTE = Z_BYTE;
7454 else
7456 ZV = XMARKER (oldzv)->charpos;
7457 ZV_BYTE = marker_byte_position (oldzv);
7460 if (point_at_end)
7461 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7462 else
7463 /* We can't do Fgoto_char (oldpoint) because it will run some
7464 Lisp code. */
7465 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7466 XMARKER (oldpoint)->bytepos);
7468 UNGCPRO;
7469 unchain_marker (XMARKER (oldpoint));
7470 unchain_marker (XMARKER (oldbegv));
7471 unchain_marker (XMARKER (oldzv));
7473 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7474 set_buffer_internal (oldbuf);
7475 if (NILP (tem))
7476 windows_or_buffers_changed = old_windows_or_buffers_changed;
7477 message_log_need_newline = !nlflag;
7478 Vdeactivate_mark = old_deactivate_mark;
7483 /* We are at the end of the buffer after just having inserted a newline.
7484 (Note: We depend on the fact we won't be crossing the gap.)
7485 Check to see if the most recent message looks a lot like the previous one.
7486 Return 0 if different, 1 if the new one should just replace it, or a
7487 value N > 1 if we should also append " [N times]". */
7489 static int
7490 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7491 int prev_bol, this_bol;
7492 int prev_bol_byte, this_bol_byte;
7494 int i;
7495 int len = Z_BYTE - 1 - this_bol_byte;
7496 int seen_dots = 0;
7497 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7498 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7500 for (i = 0; i < len; i++)
7502 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7503 seen_dots = 1;
7504 if (p1[i] != p2[i])
7505 return seen_dots;
7507 p1 += len;
7508 if (*p1 == '\n')
7509 return 2;
7510 if (*p1++ == ' ' && *p1++ == '[')
7512 int n = 0;
7513 while (*p1 >= '0' && *p1 <= '9')
7514 n = n * 10 + *p1++ - '0';
7515 if (strncmp (p1, " times]\n", 8) == 0)
7516 return n+1;
7518 return 0;
7522 /* Display an echo area message M with a specified length of NBYTES
7523 bytes. The string may include null characters. If M is 0, clear
7524 out any existing message, and let the mini-buffer text show
7525 through.
7527 This may GC, so the buffer M must NOT point to a Lisp string. */
7529 void
7530 message2 (m, nbytes, multibyte)
7531 const char *m;
7532 int nbytes;
7533 int multibyte;
7535 /* First flush out any partial line written with print. */
7536 message_log_maybe_newline ();
7537 if (m)
7538 message_dolog (m, nbytes, 1, multibyte);
7539 message2_nolog (m, nbytes, multibyte);
7543 /* The non-logging counterpart of message2. */
7545 void
7546 message2_nolog (m, nbytes, multibyte)
7547 const char *m;
7548 int nbytes, multibyte;
7550 struct frame *sf = SELECTED_FRAME ();
7551 message_enable_multibyte = multibyte;
7553 if (noninteractive)
7555 if (noninteractive_need_newline)
7556 putc ('\n', stderr);
7557 noninteractive_need_newline = 0;
7558 if (m)
7559 fwrite (m, nbytes, 1, stderr);
7560 if (cursor_in_echo_area == 0)
7561 fprintf (stderr, "\n");
7562 fflush (stderr);
7564 /* A null message buffer means that the frame hasn't really been
7565 initialized yet. Error messages get reported properly by
7566 cmd_error, so this must be just an informative message; toss it. */
7567 else if (INTERACTIVE
7568 && sf->glyphs_initialized_p
7569 && FRAME_MESSAGE_BUF (sf))
7571 Lisp_Object mini_window;
7572 struct frame *f;
7574 /* Get the frame containing the mini-buffer
7575 that the selected frame is using. */
7576 mini_window = FRAME_MINIBUF_WINDOW (sf);
7577 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7579 FRAME_SAMPLE_VISIBILITY (f);
7580 if (FRAME_VISIBLE_P (sf)
7581 && ! FRAME_VISIBLE_P (f))
7582 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7584 if (m)
7586 set_message (m, Qnil, nbytes, multibyte);
7587 if (minibuffer_auto_raise)
7588 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7590 else
7591 clear_message (1, 1);
7593 do_pending_window_change (0);
7594 echo_area_display (1);
7595 do_pending_window_change (0);
7596 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7597 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7602 /* Display an echo area message M with a specified length of NBYTES
7603 bytes. The string may include null characters. If M is not a
7604 string, clear out any existing message, and let the mini-buffer
7605 text show through.
7607 This function cancels echoing. */
7609 void
7610 message3 (m, nbytes, multibyte)
7611 Lisp_Object m;
7612 int nbytes;
7613 int multibyte;
7615 struct gcpro gcpro1;
7617 GCPRO1 (m);
7618 clear_message (1,1);
7619 cancel_echoing ();
7621 /* First flush out any partial line written with print. */
7622 message_log_maybe_newline ();
7623 if (STRINGP (m))
7625 char *buffer;
7626 USE_SAFE_ALLOCA;
7628 SAFE_ALLOCA (buffer, char *, nbytes);
7629 bcopy (SDATA (m), buffer, nbytes);
7630 message_dolog (buffer, nbytes, 1, multibyte);
7631 SAFE_FREE ();
7633 message3_nolog (m, nbytes, multibyte);
7635 UNGCPRO;
7639 /* The non-logging version of message3.
7640 This does not cancel echoing, because it is used for echoing.
7641 Perhaps we need to make a separate function for echoing
7642 and make this cancel echoing. */
7644 void
7645 message3_nolog (m, nbytes, multibyte)
7646 Lisp_Object m;
7647 int nbytes, multibyte;
7649 struct frame *sf = SELECTED_FRAME ();
7650 message_enable_multibyte = multibyte;
7652 if (noninteractive)
7654 if (noninteractive_need_newline)
7655 putc ('\n', stderr);
7656 noninteractive_need_newline = 0;
7657 if (STRINGP (m))
7658 fwrite (SDATA (m), nbytes, 1, stderr);
7659 if (cursor_in_echo_area == 0)
7660 fprintf (stderr, "\n");
7661 fflush (stderr);
7663 /* A null message buffer means that the frame hasn't really been
7664 initialized yet. Error messages get reported properly by
7665 cmd_error, so this must be just an informative message; toss it. */
7666 else if (INTERACTIVE
7667 && sf->glyphs_initialized_p
7668 && FRAME_MESSAGE_BUF (sf))
7670 Lisp_Object mini_window;
7671 Lisp_Object frame;
7672 struct frame *f;
7674 /* Get the frame containing the mini-buffer
7675 that the selected frame is using. */
7676 mini_window = FRAME_MINIBUF_WINDOW (sf);
7677 frame = XWINDOW (mini_window)->frame;
7678 f = XFRAME (frame);
7680 FRAME_SAMPLE_VISIBILITY (f);
7681 if (FRAME_VISIBLE_P (sf)
7682 && !FRAME_VISIBLE_P (f))
7683 Fmake_frame_visible (frame);
7685 if (STRINGP (m) && SCHARS (m) > 0)
7687 set_message (NULL, m, nbytes, multibyte);
7688 if (minibuffer_auto_raise)
7689 Fraise_frame (frame);
7690 /* Assume we are not echoing.
7691 (If we are, echo_now will override this.) */
7692 echo_message_buffer = Qnil;
7694 else
7695 clear_message (1, 1);
7697 do_pending_window_change (0);
7698 echo_area_display (1);
7699 do_pending_window_change (0);
7700 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7701 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7706 /* Display a null-terminated echo area message M. If M is 0, clear
7707 out any existing message, and let the mini-buffer text show through.
7709 The buffer M must continue to exist until after the echo area gets
7710 cleared or some other message gets displayed there. Do not pass
7711 text that is stored in a Lisp string. Do not pass text in a buffer
7712 that was alloca'd. */
7714 void
7715 message1 (m)
7716 char *m;
7718 message2 (m, (m ? strlen (m) : 0), 0);
7722 /* The non-logging counterpart of message1. */
7724 void
7725 message1_nolog (m)
7726 char *m;
7728 message2_nolog (m, (m ? strlen (m) : 0), 0);
7731 /* Display a message M which contains a single %s
7732 which gets replaced with STRING. */
7734 void
7735 message_with_string (m, string, log)
7736 char *m;
7737 Lisp_Object string;
7738 int log;
7740 CHECK_STRING (string);
7742 if (noninteractive)
7744 if (m)
7746 if (noninteractive_need_newline)
7747 putc ('\n', stderr);
7748 noninteractive_need_newline = 0;
7749 fprintf (stderr, m, SDATA (string));
7750 if (cursor_in_echo_area == 0)
7751 fprintf (stderr, "\n");
7752 fflush (stderr);
7755 else if (INTERACTIVE)
7757 /* The frame whose minibuffer we're going to display the message on.
7758 It may be larger than the selected frame, so we need
7759 to use its buffer, not the selected frame's buffer. */
7760 Lisp_Object mini_window;
7761 struct frame *f, *sf = SELECTED_FRAME ();
7763 /* Get the frame containing the minibuffer
7764 that the selected frame is using. */
7765 mini_window = FRAME_MINIBUF_WINDOW (sf);
7766 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7768 /* A null message buffer means that the frame hasn't really been
7769 initialized yet. Error messages get reported properly by
7770 cmd_error, so this must be just an informative message; toss it. */
7771 if (FRAME_MESSAGE_BUF (f))
7773 Lisp_Object args[2], message;
7774 struct gcpro gcpro1, gcpro2;
7776 args[0] = build_string (m);
7777 args[1] = message = string;
7778 GCPRO2 (args[0], message);
7779 gcpro1.nvars = 2;
7781 message = Fformat (2, args);
7783 if (log)
7784 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7785 else
7786 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7788 UNGCPRO;
7790 /* Print should start at the beginning of the message
7791 buffer next time. */
7792 message_buf_print = 0;
7798 /* Dump an informative message to the minibuf. If M is 0, clear out
7799 any existing message, and let the mini-buffer text show through. */
7801 /* VARARGS 1 */
7802 void
7803 message (m, a1, a2, a3)
7804 char *m;
7805 EMACS_INT a1, a2, a3;
7807 if (noninteractive)
7809 if (m)
7811 if (noninteractive_need_newline)
7812 putc ('\n', stderr);
7813 noninteractive_need_newline = 0;
7814 fprintf (stderr, m, a1, a2, a3);
7815 if (cursor_in_echo_area == 0)
7816 fprintf (stderr, "\n");
7817 fflush (stderr);
7820 else if (INTERACTIVE)
7822 /* The frame whose mini-buffer we're going to display the message
7823 on. It may be larger than the selected frame, so we need to
7824 use its buffer, not the selected frame's buffer. */
7825 Lisp_Object mini_window;
7826 struct frame *f, *sf = SELECTED_FRAME ();
7828 /* Get the frame containing the mini-buffer
7829 that the selected frame is using. */
7830 mini_window = FRAME_MINIBUF_WINDOW (sf);
7831 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7833 /* A null message buffer means that the frame hasn't really been
7834 initialized yet. Error messages get reported properly by
7835 cmd_error, so this must be just an informative message; toss
7836 it. */
7837 if (FRAME_MESSAGE_BUF (f))
7839 if (m)
7841 int len;
7842 #ifdef NO_ARG_ARRAY
7843 char *a[3];
7844 a[0] = (char *) a1;
7845 a[1] = (char *) a2;
7846 a[2] = (char *) a3;
7848 len = doprnt (FRAME_MESSAGE_BUF (f),
7849 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7850 #else
7851 len = doprnt (FRAME_MESSAGE_BUF (f),
7852 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7853 (char **) &a1);
7854 #endif /* NO_ARG_ARRAY */
7856 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7858 else
7859 message1 (0);
7861 /* Print should start at the beginning of the message
7862 buffer next time. */
7863 message_buf_print = 0;
7869 /* The non-logging version of message. */
7871 void
7872 message_nolog (m, a1, a2, a3)
7873 char *m;
7874 EMACS_INT a1, a2, a3;
7876 Lisp_Object old_log_max;
7877 old_log_max = Vmessage_log_max;
7878 Vmessage_log_max = Qnil;
7879 message (m, a1, a2, a3);
7880 Vmessage_log_max = old_log_max;
7884 /* Display the current message in the current mini-buffer. This is
7885 only called from error handlers in process.c, and is not time
7886 critical. */
7888 void
7889 update_echo_area ()
7891 if (!NILP (echo_area_buffer[0]))
7893 Lisp_Object string;
7894 string = Fcurrent_message ();
7895 message3 (string, SBYTES (string),
7896 !NILP (current_buffer->enable_multibyte_characters));
7901 /* Make sure echo area buffers in `echo_buffers' are live.
7902 If they aren't, make new ones. */
7904 static void
7905 ensure_echo_area_buffers ()
7907 int i;
7909 for (i = 0; i < 2; ++i)
7910 if (!BUFFERP (echo_buffer[i])
7911 || NILP (XBUFFER (echo_buffer[i])->name))
7913 char name[30];
7914 Lisp_Object old_buffer;
7915 int j;
7917 old_buffer = echo_buffer[i];
7918 sprintf (name, " *Echo Area %d*", i);
7919 echo_buffer[i] = Fget_buffer_create (build_string (name));
7920 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7922 for (j = 0; j < 2; ++j)
7923 if (EQ (old_buffer, echo_area_buffer[j]))
7924 echo_area_buffer[j] = echo_buffer[i];
7929 /* Call FN with args A1..A4 with either the current or last displayed
7930 echo_area_buffer as current buffer.
7932 WHICH zero means use the current message buffer
7933 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7934 from echo_buffer[] and clear it.
7936 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7937 suitable buffer from echo_buffer[] and clear it.
7939 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7940 that the current message becomes the last displayed one, make
7941 choose a suitable buffer for echo_area_buffer[0], and clear it.
7943 Value is what FN returns. */
7945 static int
7946 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7947 struct window *w;
7948 int which;
7949 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7950 EMACS_INT a1;
7951 Lisp_Object a2;
7952 EMACS_INT a3, a4;
7954 Lisp_Object buffer;
7955 int this_one, the_other, clear_buffer_p, rc;
7956 int count = SPECPDL_INDEX ();
7958 /* If buffers aren't live, make new ones. */
7959 ensure_echo_area_buffers ();
7961 clear_buffer_p = 0;
7963 if (which == 0)
7964 this_one = 0, the_other = 1;
7965 else if (which > 0)
7966 this_one = 1, the_other = 0;
7967 else
7969 this_one = 0, the_other = 1;
7970 clear_buffer_p = 1;
7972 /* We need a fresh one in case the current echo buffer equals
7973 the one containing the last displayed echo area message. */
7974 if (!NILP (echo_area_buffer[this_one])
7975 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7976 echo_area_buffer[this_one] = Qnil;
7979 /* Choose a suitable buffer from echo_buffer[] is we don't
7980 have one. */
7981 if (NILP (echo_area_buffer[this_one]))
7983 echo_area_buffer[this_one]
7984 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7985 ? echo_buffer[the_other]
7986 : echo_buffer[this_one]);
7987 clear_buffer_p = 1;
7990 buffer = echo_area_buffer[this_one];
7992 /* Don't get confused by reusing the buffer used for echoing
7993 for a different purpose. */
7994 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7995 cancel_echoing ();
7997 record_unwind_protect (unwind_with_echo_area_buffer,
7998 with_echo_area_buffer_unwind_data (w));
8000 /* Make the echo area buffer current. Note that for display
8001 purposes, it is not necessary that the displayed window's buffer
8002 == current_buffer, except for text property lookup. So, let's
8003 only set that buffer temporarily here without doing a full
8004 Fset_window_buffer. We must also change w->pointm, though,
8005 because otherwise an assertions in unshow_buffer fails, and Emacs
8006 aborts. */
8007 set_buffer_internal_1 (XBUFFER (buffer));
8008 if (w)
8010 w->buffer = buffer;
8011 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8014 current_buffer->undo_list = Qt;
8015 current_buffer->read_only = Qnil;
8016 specbind (Qinhibit_read_only, Qt);
8017 specbind (Qinhibit_modification_hooks, Qt);
8019 if (clear_buffer_p && Z > BEG)
8020 del_range (BEG, Z);
8022 xassert (BEGV >= BEG);
8023 xassert (ZV <= Z && ZV >= BEGV);
8025 rc = fn (a1, a2, a3, a4);
8027 xassert (BEGV >= BEG);
8028 xassert (ZV <= Z && ZV >= BEGV);
8030 unbind_to (count, Qnil);
8031 return rc;
8035 /* Save state that should be preserved around the call to the function
8036 FN called in with_echo_area_buffer. */
8038 static Lisp_Object
8039 with_echo_area_buffer_unwind_data (w)
8040 struct window *w;
8042 int i = 0;
8043 Lisp_Object vector;
8045 /* Reduce consing by keeping one vector in
8046 Vwith_echo_area_save_vector. */
8047 vector = Vwith_echo_area_save_vector;
8048 Vwith_echo_area_save_vector = Qnil;
8050 if (NILP (vector))
8051 vector = Fmake_vector (make_number (7), Qnil);
8053 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8054 AREF (vector, i) = Vdeactivate_mark, ++i;
8055 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8057 if (w)
8059 XSETWINDOW (AREF (vector, i), w); ++i;
8060 AREF (vector, i) = w->buffer; ++i;
8061 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8062 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8064 else
8066 int end = i + 4;
8067 for (; i < end; ++i)
8068 AREF (vector, i) = Qnil;
8071 xassert (i == ASIZE (vector));
8072 return vector;
8076 /* Restore global state from VECTOR which was created by
8077 with_echo_area_buffer_unwind_data. */
8079 static Lisp_Object
8080 unwind_with_echo_area_buffer (vector)
8081 Lisp_Object vector;
8083 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8084 Vdeactivate_mark = AREF (vector, 1);
8085 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8087 if (WINDOWP (AREF (vector, 3)))
8089 struct window *w;
8090 Lisp_Object buffer, charpos, bytepos;
8092 w = XWINDOW (AREF (vector, 3));
8093 buffer = AREF (vector, 4);
8094 charpos = AREF (vector, 5);
8095 bytepos = AREF (vector, 6);
8097 w->buffer = buffer;
8098 set_marker_both (w->pointm, buffer,
8099 XFASTINT (charpos), XFASTINT (bytepos));
8102 Vwith_echo_area_save_vector = vector;
8103 return Qnil;
8107 /* Set up the echo area for use by print functions. MULTIBYTE_P
8108 non-zero means we will print multibyte. */
8110 void
8111 setup_echo_area_for_printing (multibyte_p)
8112 int multibyte_p;
8114 /* If we can't find an echo area any more, exit. */
8115 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8116 Fkill_emacs (Qnil);
8118 ensure_echo_area_buffers ();
8120 if (!message_buf_print)
8122 /* A message has been output since the last time we printed.
8123 Choose a fresh echo area buffer. */
8124 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8125 echo_area_buffer[0] = echo_buffer[1];
8126 else
8127 echo_area_buffer[0] = echo_buffer[0];
8129 /* Switch to that buffer and clear it. */
8130 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8131 current_buffer->truncate_lines = Qnil;
8133 if (Z > BEG)
8135 int count = SPECPDL_INDEX ();
8136 specbind (Qinhibit_read_only, Qt);
8137 /* Note that undo recording is always disabled. */
8138 del_range (BEG, Z);
8139 unbind_to (count, Qnil);
8141 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8143 /* Set up the buffer for the multibyteness we need. */
8144 if (multibyte_p
8145 != !NILP (current_buffer->enable_multibyte_characters))
8146 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8148 /* Raise the frame containing the echo area. */
8149 if (minibuffer_auto_raise)
8151 struct frame *sf = SELECTED_FRAME ();
8152 Lisp_Object mini_window;
8153 mini_window = FRAME_MINIBUF_WINDOW (sf);
8154 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8157 message_log_maybe_newline ();
8158 message_buf_print = 1;
8160 else
8162 if (NILP (echo_area_buffer[0]))
8164 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8165 echo_area_buffer[0] = echo_buffer[1];
8166 else
8167 echo_area_buffer[0] = echo_buffer[0];
8170 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8172 /* Someone switched buffers between print requests. */
8173 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8174 current_buffer->truncate_lines = Qnil;
8180 /* Display an echo area message in window W. Value is non-zero if W's
8181 height is changed. If display_last_displayed_message_p is
8182 non-zero, display the message that was last displayed, otherwise
8183 display the current message. */
8185 static int
8186 display_echo_area (w)
8187 struct window *w;
8189 int i, no_message_p, window_height_changed_p, count;
8191 /* Temporarily disable garbage collections while displaying the echo
8192 area. This is done because a GC can print a message itself.
8193 That message would modify the echo area buffer's contents while a
8194 redisplay of the buffer is going on, and seriously confuse
8195 redisplay. */
8196 count = inhibit_garbage_collection ();
8198 /* If there is no message, we must call display_echo_area_1
8199 nevertheless because it resizes the window. But we will have to
8200 reset the echo_area_buffer in question to nil at the end because
8201 with_echo_area_buffer will sets it to an empty buffer. */
8202 i = display_last_displayed_message_p ? 1 : 0;
8203 no_message_p = NILP (echo_area_buffer[i]);
8205 window_height_changed_p
8206 = with_echo_area_buffer (w, display_last_displayed_message_p,
8207 display_echo_area_1,
8208 (EMACS_INT) w, Qnil, 0, 0);
8210 if (no_message_p)
8211 echo_area_buffer[i] = Qnil;
8213 unbind_to (count, Qnil);
8214 return window_height_changed_p;
8218 /* Helper for display_echo_area. Display the current buffer which
8219 contains the current echo area message in window W, a mini-window,
8220 a pointer to which is passed in A1. A2..A4 are currently not used.
8221 Change the height of W so that all of the message is displayed.
8222 Value is non-zero if height of W was changed. */
8224 static int
8225 display_echo_area_1 (a1, a2, a3, a4)
8226 EMACS_INT a1;
8227 Lisp_Object a2;
8228 EMACS_INT a3, a4;
8230 struct window *w = (struct window *) a1;
8231 Lisp_Object window;
8232 struct text_pos start;
8233 int window_height_changed_p = 0;
8235 /* Do this before displaying, so that we have a large enough glyph
8236 matrix for the display. If we can't get enough space for the
8237 whole text, display the last N lines. That works by setting w->start. */
8238 window_height_changed_p = resize_mini_window (w, 0);
8240 /* Use the starting position chosen by resize_mini_window. */
8241 SET_TEXT_POS_FROM_MARKER (start, w->start);
8243 /* Display. */
8244 clear_glyph_matrix (w->desired_matrix);
8245 XSETWINDOW (window, w);
8246 try_window (window, start, 0);
8248 return window_height_changed_p;
8252 /* Resize the echo area window to exactly the size needed for the
8253 currently displayed message, if there is one. If a mini-buffer
8254 is active, don't shrink it. */
8256 void
8257 resize_echo_area_exactly ()
8259 if (BUFFERP (echo_area_buffer[0])
8260 && WINDOWP (echo_area_window))
8262 struct window *w = XWINDOW (echo_area_window);
8263 int resized_p;
8264 Lisp_Object resize_exactly;
8266 if (minibuf_level == 0)
8267 resize_exactly = Qt;
8268 else
8269 resize_exactly = Qnil;
8271 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8272 (EMACS_INT) w, resize_exactly, 0, 0);
8273 if (resized_p)
8275 ++windows_or_buffers_changed;
8276 ++update_mode_lines;
8277 redisplay_internal (0);
8283 /* Callback function for with_echo_area_buffer, when used from
8284 resize_echo_area_exactly. A1 contains a pointer to the window to
8285 resize, EXACTLY non-nil means resize the mini-window exactly to the
8286 size of the text displayed. A3 and A4 are not used. Value is what
8287 resize_mini_window returns. */
8289 static int
8290 resize_mini_window_1 (a1, exactly, a3, a4)
8291 EMACS_INT a1;
8292 Lisp_Object exactly;
8293 EMACS_INT a3, a4;
8295 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8299 /* Resize mini-window W to fit the size of its contents. EXACT:P
8300 means size the window exactly to the size needed. Otherwise, it's
8301 only enlarged until W's buffer is empty.
8303 Set W->start to the right place to begin display. If the whole
8304 contents fit, start at the beginning. Otherwise, start so as
8305 to make the end of the contents appear. This is particularly
8306 important for y-or-n-p, but seems desirable generally.
8308 Value is non-zero if the window height has been changed. */
8311 resize_mini_window (w, exact_p)
8312 struct window *w;
8313 int exact_p;
8315 struct frame *f = XFRAME (w->frame);
8316 int window_height_changed_p = 0;
8318 xassert (MINI_WINDOW_P (w));
8320 /* By default, start display at the beginning. */
8321 set_marker_both (w->start, w->buffer,
8322 BUF_BEGV (XBUFFER (w->buffer)),
8323 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8325 /* Don't resize windows while redisplaying a window; it would
8326 confuse redisplay functions when the size of the window they are
8327 displaying changes from under them. Such a resizing can happen,
8328 for instance, when which-func prints a long message while
8329 we are running fontification-functions. We're running these
8330 functions with safe_call which binds inhibit-redisplay to t. */
8331 if (!NILP (Vinhibit_redisplay))
8332 return 0;
8334 /* Nil means don't try to resize. */
8335 if (NILP (Vresize_mini_windows)
8336 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8337 return 0;
8339 if (!FRAME_MINIBUF_ONLY_P (f))
8341 struct it it;
8342 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8343 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8344 int height, max_height;
8345 int unit = FRAME_LINE_HEIGHT (f);
8346 struct text_pos start;
8347 struct buffer *old_current_buffer = NULL;
8349 if (current_buffer != XBUFFER (w->buffer))
8351 old_current_buffer = current_buffer;
8352 set_buffer_internal (XBUFFER (w->buffer));
8355 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8357 /* Compute the max. number of lines specified by the user. */
8358 if (FLOATP (Vmax_mini_window_height))
8359 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8360 else if (INTEGERP (Vmax_mini_window_height))
8361 max_height = XINT (Vmax_mini_window_height);
8362 else
8363 max_height = total_height / 4;
8365 /* Correct that max. height if it's bogus. */
8366 max_height = max (1, max_height);
8367 max_height = min (total_height, max_height);
8369 /* Find out the height of the text in the window. */
8370 if (it.truncate_lines_p)
8371 height = 1;
8372 else
8374 last_height = 0;
8375 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8376 if (it.max_ascent == 0 && it.max_descent == 0)
8377 height = it.current_y + last_height;
8378 else
8379 height = it.current_y + it.max_ascent + it.max_descent;
8380 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8381 height = (height + unit - 1) / unit;
8384 /* Compute a suitable window start. */
8385 if (height > max_height)
8387 height = max_height;
8388 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8389 move_it_vertically_backward (&it, (height - 1) * unit);
8390 start = it.current.pos;
8392 else
8393 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8394 SET_MARKER_FROM_TEXT_POS (w->start, start);
8396 if (EQ (Vresize_mini_windows, Qgrow_only))
8398 /* Let it grow only, until we display an empty message, in which
8399 case the window shrinks again. */
8400 if (height > WINDOW_TOTAL_LINES (w))
8402 int old_height = WINDOW_TOTAL_LINES (w);
8403 freeze_window_starts (f, 1);
8404 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8405 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8407 else if (height < WINDOW_TOTAL_LINES (w)
8408 && (exact_p || BEGV == ZV))
8410 int old_height = WINDOW_TOTAL_LINES (w);
8411 freeze_window_starts (f, 0);
8412 shrink_mini_window (w);
8413 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8416 else
8418 /* Always resize to exact size needed. */
8419 if (height > WINDOW_TOTAL_LINES (w))
8421 int old_height = WINDOW_TOTAL_LINES (w);
8422 freeze_window_starts (f, 1);
8423 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8424 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8426 else if (height < WINDOW_TOTAL_LINES (w))
8428 int old_height = WINDOW_TOTAL_LINES (w);
8429 freeze_window_starts (f, 0);
8430 shrink_mini_window (w);
8432 if (height)
8434 freeze_window_starts (f, 1);
8435 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8438 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8442 if (old_current_buffer)
8443 set_buffer_internal (old_current_buffer);
8446 return window_height_changed_p;
8450 /* Value is the current message, a string, or nil if there is no
8451 current message. */
8453 Lisp_Object
8454 current_message ()
8456 Lisp_Object msg;
8458 if (NILP (echo_area_buffer[0]))
8459 msg = Qnil;
8460 else
8462 with_echo_area_buffer (0, 0, current_message_1,
8463 (EMACS_INT) &msg, Qnil, 0, 0);
8464 if (NILP (msg))
8465 echo_area_buffer[0] = Qnil;
8468 return msg;
8472 static int
8473 current_message_1 (a1, a2, a3, a4)
8474 EMACS_INT a1;
8475 Lisp_Object a2;
8476 EMACS_INT a3, a4;
8478 Lisp_Object *msg = (Lisp_Object *) a1;
8480 if (Z > BEG)
8481 *msg = make_buffer_string (BEG, Z, 1);
8482 else
8483 *msg = Qnil;
8484 return 0;
8488 /* Push the current message on Vmessage_stack for later restauration
8489 by restore_message. Value is non-zero if the current message isn't
8490 empty. This is a relatively infrequent operation, so it's not
8491 worth optimizing. */
8494 push_message ()
8496 Lisp_Object msg;
8497 msg = current_message ();
8498 Vmessage_stack = Fcons (msg, Vmessage_stack);
8499 return STRINGP (msg);
8503 /* Restore message display from the top of Vmessage_stack. */
8505 void
8506 restore_message ()
8508 Lisp_Object msg;
8510 xassert (CONSP (Vmessage_stack));
8511 msg = XCAR (Vmessage_stack);
8512 if (STRINGP (msg))
8513 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8514 else
8515 message3_nolog (msg, 0, 0);
8519 /* Handler for record_unwind_protect calling pop_message. */
8521 Lisp_Object
8522 pop_message_unwind (dummy)
8523 Lisp_Object dummy;
8525 pop_message ();
8526 return Qnil;
8529 /* Pop the top-most entry off Vmessage_stack. */
8531 void
8532 pop_message ()
8534 xassert (CONSP (Vmessage_stack));
8535 Vmessage_stack = XCDR (Vmessage_stack);
8539 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8540 exits. If the stack is not empty, we have a missing pop_message
8541 somewhere. */
8543 void
8544 check_message_stack ()
8546 if (!NILP (Vmessage_stack))
8547 abort ();
8551 /* Truncate to NCHARS what will be displayed in the echo area the next
8552 time we display it---but don't redisplay it now. */
8554 void
8555 truncate_echo_area (nchars)
8556 int nchars;
8558 if (nchars == 0)
8559 echo_area_buffer[0] = Qnil;
8560 /* A null message buffer means that the frame hasn't really been
8561 initialized yet. Error messages get reported properly by
8562 cmd_error, so this must be just an informative message; toss it. */
8563 else if (!noninteractive
8564 && INTERACTIVE
8565 && !NILP (echo_area_buffer[0]))
8567 struct frame *sf = SELECTED_FRAME ();
8568 if (FRAME_MESSAGE_BUF (sf))
8569 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8574 /* Helper function for truncate_echo_area. Truncate the current
8575 message to at most NCHARS characters. */
8577 static int
8578 truncate_message_1 (nchars, a2, a3, a4)
8579 EMACS_INT nchars;
8580 Lisp_Object a2;
8581 EMACS_INT a3, a4;
8583 if (BEG + nchars < Z)
8584 del_range (BEG + nchars, Z);
8585 if (Z == BEG)
8586 echo_area_buffer[0] = Qnil;
8587 return 0;
8591 /* Set the current message to a substring of S or STRING.
8593 If STRING is a Lisp string, set the message to the first NBYTES
8594 bytes from STRING. NBYTES zero means use the whole string. If
8595 STRING is multibyte, the message will be displayed multibyte.
8597 If S is not null, set the message to the first LEN bytes of S. LEN
8598 zero means use the whole string. MULTIBYTE_P non-zero means S is
8599 multibyte. Display the message multibyte in that case.
8601 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8602 to t before calling set_message_1 (which calls insert).
8605 void
8606 set_message (s, string, nbytes, multibyte_p)
8607 const char *s;
8608 Lisp_Object string;
8609 int nbytes, multibyte_p;
8611 message_enable_multibyte
8612 = ((s && multibyte_p)
8613 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8615 with_echo_area_buffer (0, -1, set_message_1,
8616 (EMACS_INT) s, string, nbytes, multibyte_p);
8617 message_buf_print = 0;
8618 help_echo_showing_p = 0;
8622 /* Helper function for set_message. Arguments have the same meaning
8623 as there, with A1 corresponding to S and A2 corresponding to STRING
8624 This function is called with the echo area buffer being
8625 current. */
8627 static int
8628 set_message_1 (a1, a2, nbytes, multibyte_p)
8629 EMACS_INT a1;
8630 Lisp_Object a2;
8631 EMACS_INT nbytes, multibyte_p;
8633 const char *s = (const char *) a1;
8634 Lisp_Object string = a2;
8636 /* Change multibyteness of the echo buffer appropriately. */
8637 if (message_enable_multibyte
8638 != !NILP (current_buffer->enable_multibyte_characters))
8639 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8641 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8643 /* Insert new message at BEG. */
8644 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8646 if (STRINGP (string))
8648 int nchars;
8650 if (nbytes == 0)
8651 nbytes = SBYTES (string);
8652 nchars = string_byte_to_char (string, nbytes);
8654 /* This function takes care of single/multibyte conversion. We
8655 just have to ensure that the echo area buffer has the right
8656 setting of enable_multibyte_characters. */
8657 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8659 else if (s)
8661 if (nbytes == 0)
8662 nbytes = strlen (s);
8664 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8666 /* Convert from multi-byte to single-byte. */
8667 int i, c, n;
8668 unsigned char work[1];
8670 /* Convert a multibyte string to single-byte. */
8671 for (i = 0; i < nbytes; i += n)
8673 c = string_char_and_length (s + i, nbytes - i, &n);
8674 work[0] = (SINGLE_BYTE_CHAR_P (c)
8676 : multibyte_char_to_unibyte (c, Qnil));
8677 insert_1_both (work, 1, 1, 1, 0, 0);
8680 else if (!multibyte_p
8681 && !NILP (current_buffer->enable_multibyte_characters))
8683 /* Convert from single-byte to multi-byte. */
8684 int i, c, n;
8685 const unsigned char *msg = (const unsigned char *) s;
8686 unsigned char str[MAX_MULTIBYTE_LENGTH];
8688 /* Convert a single-byte string to multibyte. */
8689 for (i = 0; i < nbytes; i++)
8691 c = unibyte_char_to_multibyte (msg[i]);
8692 n = CHAR_STRING (c, str);
8693 insert_1_both (str, 1, n, 1, 0, 0);
8696 else
8697 insert_1 (s, nbytes, 1, 0, 0);
8700 return 0;
8704 /* Clear messages. CURRENT_P non-zero means clear the current
8705 message. LAST_DISPLAYED_P non-zero means clear the message
8706 last displayed. */
8708 void
8709 clear_message (current_p, last_displayed_p)
8710 int current_p, last_displayed_p;
8712 if (current_p)
8714 echo_area_buffer[0] = Qnil;
8715 message_cleared_p = 1;
8718 if (last_displayed_p)
8719 echo_area_buffer[1] = Qnil;
8721 message_buf_print = 0;
8724 /* Clear garbaged frames.
8726 This function is used where the old redisplay called
8727 redraw_garbaged_frames which in turn called redraw_frame which in
8728 turn called clear_frame. The call to clear_frame was a source of
8729 flickering. I believe a clear_frame is not necessary. It should
8730 suffice in the new redisplay to invalidate all current matrices,
8731 and ensure a complete redisplay of all windows. */
8733 static void
8734 clear_garbaged_frames ()
8736 if (frame_garbaged)
8738 Lisp_Object tail, frame;
8739 int changed_count = 0;
8741 FOR_EACH_FRAME (tail, frame)
8743 struct frame *f = XFRAME (frame);
8745 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8747 if (f->resized_p)
8749 Fredraw_frame (frame);
8750 f->force_flush_display_p = 1;
8752 clear_current_matrices (f);
8753 changed_count++;
8754 f->garbaged = 0;
8755 f->resized_p = 0;
8759 frame_garbaged = 0;
8760 if (changed_count)
8761 ++windows_or_buffers_changed;
8766 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8767 is non-zero update selected_frame. Value is non-zero if the
8768 mini-windows height has been changed. */
8770 static int
8771 echo_area_display (update_frame_p)
8772 int update_frame_p;
8774 Lisp_Object mini_window;
8775 struct window *w;
8776 struct frame *f;
8777 int window_height_changed_p = 0;
8778 struct frame *sf = SELECTED_FRAME ();
8780 mini_window = FRAME_MINIBUF_WINDOW (sf);
8781 w = XWINDOW (mini_window);
8782 f = XFRAME (WINDOW_FRAME (w));
8784 /* Don't display if frame is invisible or not yet initialized. */
8785 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8786 return 0;
8788 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8789 #ifndef MAC_OS8
8790 #ifdef HAVE_WINDOW_SYSTEM
8791 /* When Emacs starts, selected_frame may be the initial terminal
8792 frame. If we let this through, a message would be displayed on
8793 the terminal. */
8794 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8795 return 0;
8796 #endif /* HAVE_WINDOW_SYSTEM */
8797 #endif
8799 /* Redraw garbaged frames. */
8800 if (frame_garbaged)
8801 clear_garbaged_frames ();
8803 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8805 echo_area_window = mini_window;
8806 window_height_changed_p = display_echo_area (w);
8807 w->must_be_updated_p = 1;
8809 /* Update the display, unless called from redisplay_internal.
8810 Also don't update the screen during redisplay itself. The
8811 update will happen at the end of redisplay, and an update
8812 here could cause confusion. */
8813 if (update_frame_p && !redisplaying_p)
8815 int n = 0;
8817 /* If the display update has been interrupted by pending
8818 input, update mode lines in the frame. Due to the
8819 pending input, it might have been that redisplay hasn't
8820 been called, so that mode lines above the echo area are
8821 garbaged. This looks odd, so we prevent it here. */
8822 if (!display_completed)
8823 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8825 if (window_height_changed_p
8826 /* Don't do this if Emacs is shutting down. Redisplay
8827 needs to run hooks. */
8828 && !NILP (Vrun_hooks))
8830 /* Must update other windows. Likewise as in other
8831 cases, don't let this update be interrupted by
8832 pending input. */
8833 int count = SPECPDL_INDEX ();
8834 specbind (Qredisplay_dont_pause, Qt);
8835 windows_or_buffers_changed = 1;
8836 redisplay_internal (0);
8837 unbind_to (count, Qnil);
8839 else if (FRAME_WINDOW_P (f) && n == 0)
8841 /* Window configuration is the same as before.
8842 Can do with a display update of the echo area,
8843 unless we displayed some mode lines. */
8844 update_single_window (w, 1);
8845 FRAME_RIF (f)->flush_display (f);
8847 else
8848 update_frame (f, 1, 1);
8850 /* If cursor is in the echo area, make sure that the next
8851 redisplay displays the minibuffer, so that the cursor will
8852 be replaced with what the minibuffer wants. */
8853 if (cursor_in_echo_area)
8854 ++windows_or_buffers_changed;
8857 else if (!EQ (mini_window, selected_window))
8858 windows_or_buffers_changed++;
8860 /* Last displayed message is now the current message. */
8861 echo_area_buffer[1] = echo_area_buffer[0];
8862 /* Inform read_char that we're not echoing. */
8863 echo_message_buffer = Qnil;
8865 /* Prevent redisplay optimization in redisplay_internal by resetting
8866 this_line_start_pos. This is done because the mini-buffer now
8867 displays the message instead of its buffer text. */
8868 if (EQ (mini_window, selected_window))
8869 CHARPOS (this_line_start_pos) = 0;
8871 return window_height_changed_p;
8876 /***********************************************************************
8877 Mode Lines and Frame Titles
8878 ***********************************************************************/
8880 /* A buffer for constructing non-propertized mode-line strings and
8881 frame titles in it; allocated from the heap in init_xdisp and
8882 resized as needed in store_mode_line_noprop_char. */
8884 static char *mode_line_noprop_buf;
8886 /* The buffer's end, and a current output position in it. */
8888 static char *mode_line_noprop_buf_end;
8889 static char *mode_line_noprop_ptr;
8891 #define MODE_LINE_NOPROP_LEN(start) \
8892 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8894 static enum {
8895 MODE_LINE_DISPLAY = 0,
8896 MODE_LINE_TITLE,
8897 MODE_LINE_NOPROP,
8898 MODE_LINE_STRING
8899 } mode_line_target;
8901 /* Alist that caches the results of :propertize.
8902 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8903 static Lisp_Object mode_line_proptrans_alist;
8905 /* List of strings making up the mode-line. */
8906 static Lisp_Object mode_line_string_list;
8908 /* Base face property when building propertized mode line string. */
8909 static Lisp_Object mode_line_string_face;
8910 static Lisp_Object mode_line_string_face_prop;
8913 /* Unwind data for mode line strings */
8915 static Lisp_Object Vmode_line_unwind_vector;
8917 static Lisp_Object
8918 format_mode_line_unwind_data (obuf, save_proptrans)
8919 struct buffer *obuf;
8921 Lisp_Object vector;
8923 /* Reduce consing by keeping one vector in
8924 Vwith_echo_area_save_vector. */
8925 vector = Vmode_line_unwind_vector;
8926 Vmode_line_unwind_vector = Qnil;
8928 if (NILP (vector))
8929 vector = Fmake_vector (make_number (7), Qnil);
8931 AREF (vector, 0) = make_number (mode_line_target);
8932 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8933 AREF (vector, 2) = mode_line_string_list;
8934 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8935 AREF (vector, 4) = mode_line_string_face;
8936 AREF (vector, 5) = mode_line_string_face_prop;
8938 if (obuf)
8939 XSETBUFFER (AREF (vector, 6), obuf);
8940 else
8941 AREF (vector, 6) = Qnil;
8943 return vector;
8946 static Lisp_Object
8947 unwind_format_mode_line (vector)
8948 Lisp_Object vector;
8950 mode_line_target = XINT (AREF (vector, 0));
8951 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8952 mode_line_string_list = AREF (vector, 2);
8953 if (! EQ (AREF (vector, 3), Qt))
8954 mode_line_proptrans_alist = AREF (vector, 3);
8955 mode_line_string_face = AREF (vector, 4);
8956 mode_line_string_face_prop = AREF (vector, 5);
8958 if (!NILP (AREF (vector, 6)))
8960 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8961 AREF (vector, 6) = Qnil;
8964 Vmode_line_unwind_vector = vector;
8965 return Qnil;
8969 /* Store a single character C for the frame title in mode_line_noprop_buf.
8970 Re-allocate mode_line_noprop_buf if necessary. */
8972 static void
8973 #ifdef PROTOTYPES
8974 store_mode_line_noprop_char (char c)
8975 #else
8976 store_mode_line_noprop_char (c)
8977 char c;
8978 #endif
8980 /* If output position has reached the end of the allocated buffer,
8981 double the buffer's size. */
8982 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8984 int len = MODE_LINE_NOPROP_LEN (0);
8985 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8986 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8987 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8988 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8991 *mode_line_noprop_ptr++ = c;
8995 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8996 mode_line_noprop_ptr. STR is the string to store. Do not copy
8997 characters that yield more columns than PRECISION; PRECISION <= 0
8998 means copy the whole string. Pad with spaces until FIELD_WIDTH
8999 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9000 pad. Called from display_mode_element when it is used to build a
9001 frame title. */
9003 static int
9004 store_mode_line_noprop (str, field_width, precision)
9005 const unsigned char *str;
9006 int field_width, precision;
9008 int n = 0;
9009 int dummy, nbytes;
9011 /* Copy at most PRECISION chars from STR. */
9012 nbytes = strlen (str);
9013 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9014 while (nbytes--)
9015 store_mode_line_noprop_char (*str++);
9017 /* Fill up with spaces until FIELD_WIDTH reached. */
9018 while (field_width > 0
9019 && n < field_width)
9021 store_mode_line_noprop_char (' ');
9022 ++n;
9025 return n;
9028 /***********************************************************************
9029 Frame Titles
9030 ***********************************************************************/
9032 #ifdef HAVE_WINDOW_SYSTEM
9034 /* Set the title of FRAME, if it has changed. The title format is
9035 Vicon_title_format if FRAME is iconified, otherwise it is
9036 frame_title_format. */
9038 static void
9039 x_consider_frame_title (frame)
9040 Lisp_Object frame;
9042 struct frame *f = XFRAME (frame);
9044 if (FRAME_WINDOW_P (f)
9045 || FRAME_MINIBUF_ONLY_P (f)
9046 || f->explicit_name)
9048 /* Do we have more than one visible frame on this X display? */
9049 Lisp_Object tail;
9050 Lisp_Object fmt;
9051 int title_start;
9052 char *title;
9053 int len;
9054 struct it it;
9055 int count = SPECPDL_INDEX ();
9057 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9059 Lisp_Object other_frame = XCAR (tail);
9060 struct frame *tf = XFRAME (other_frame);
9062 if (tf != f
9063 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9064 && !FRAME_MINIBUF_ONLY_P (tf)
9065 && !EQ (other_frame, tip_frame)
9066 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9067 break;
9070 /* Set global variable indicating that multiple frames exist. */
9071 multiple_frames = CONSP (tail);
9073 /* Switch to the buffer of selected window of the frame. Set up
9074 mode_line_target so that display_mode_element will output into
9075 mode_line_noprop_buf; then display the title. */
9076 record_unwind_protect (unwind_format_mode_line,
9077 format_mode_line_unwind_data (current_buffer, 0));
9079 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9080 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9082 mode_line_target = MODE_LINE_TITLE;
9083 title_start = MODE_LINE_NOPROP_LEN (0);
9084 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9085 NULL, DEFAULT_FACE_ID);
9086 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9087 len = MODE_LINE_NOPROP_LEN (title_start);
9088 title = mode_line_noprop_buf + title_start;
9089 unbind_to (count, Qnil);
9091 /* Set the title only if it's changed. This avoids consing in
9092 the common case where it hasn't. (If it turns out that we've
9093 already wasted too much time by walking through the list with
9094 display_mode_element, then we might need to optimize at a
9095 higher level than this.) */
9096 if (! STRINGP (f->name)
9097 || SBYTES (f->name) != len
9098 || bcmp (title, SDATA (f->name), len) != 0)
9099 x_implicitly_set_name (f, make_string (title, len), Qnil);
9103 #endif /* not HAVE_WINDOW_SYSTEM */
9108 /***********************************************************************
9109 Menu Bars
9110 ***********************************************************************/
9113 /* Prepare for redisplay by updating menu-bar item lists when
9114 appropriate. This can call eval. */
9116 void
9117 prepare_menu_bars ()
9119 int all_windows;
9120 struct gcpro gcpro1, gcpro2;
9121 struct frame *f;
9122 Lisp_Object tooltip_frame;
9124 #ifdef HAVE_WINDOW_SYSTEM
9125 tooltip_frame = tip_frame;
9126 #else
9127 tooltip_frame = Qnil;
9128 #endif
9130 /* Update all frame titles based on their buffer names, etc. We do
9131 this before the menu bars so that the buffer-menu will show the
9132 up-to-date frame titles. */
9133 #ifdef HAVE_WINDOW_SYSTEM
9134 if (windows_or_buffers_changed || update_mode_lines)
9136 Lisp_Object tail, frame;
9138 FOR_EACH_FRAME (tail, frame)
9140 f = XFRAME (frame);
9141 if (!EQ (frame, tooltip_frame)
9142 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9143 x_consider_frame_title (frame);
9146 #endif /* HAVE_WINDOW_SYSTEM */
9148 /* Update the menu bar item lists, if appropriate. This has to be
9149 done before any actual redisplay or generation of display lines. */
9150 all_windows = (update_mode_lines
9151 || buffer_shared > 1
9152 || windows_or_buffers_changed);
9153 if (all_windows)
9155 Lisp_Object tail, frame;
9156 int count = SPECPDL_INDEX ();
9157 /* 1 means that update_menu_bar has run its hooks
9158 so any further calls to update_menu_bar shouldn't do so again. */
9159 int menu_bar_hooks_run = 0;
9161 record_unwind_save_match_data ();
9163 FOR_EACH_FRAME (tail, frame)
9165 f = XFRAME (frame);
9167 /* Ignore tooltip frame. */
9168 if (EQ (frame, tooltip_frame))
9169 continue;
9171 /* If a window on this frame changed size, report that to
9172 the user and clear the size-change flag. */
9173 if (FRAME_WINDOW_SIZES_CHANGED (f))
9175 Lisp_Object functions;
9177 /* Clear flag first in case we get an error below. */
9178 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9179 functions = Vwindow_size_change_functions;
9180 GCPRO2 (tail, functions);
9182 while (CONSP (functions))
9184 call1 (XCAR (functions), frame);
9185 functions = XCDR (functions);
9187 UNGCPRO;
9190 GCPRO1 (tail);
9191 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9192 #ifdef HAVE_WINDOW_SYSTEM
9193 update_tool_bar (f, 0);
9194 #ifdef MAC_OS
9195 mac_update_title_bar (f, 0);
9196 #endif
9197 #endif
9198 UNGCPRO;
9201 unbind_to (count, Qnil);
9203 else
9205 struct frame *sf = SELECTED_FRAME ();
9206 update_menu_bar (sf, 1, 0);
9207 #ifdef HAVE_WINDOW_SYSTEM
9208 update_tool_bar (sf, 1);
9209 #ifdef MAC_OS
9210 mac_update_title_bar (sf, 1);
9211 #endif
9212 #endif
9215 /* Motif needs this. See comment in xmenu.c. Turn it off when
9216 pending_menu_activation is not defined. */
9217 #ifdef USE_X_TOOLKIT
9218 pending_menu_activation = 0;
9219 #endif
9223 /* Update the menu bar item list for frame F. This has to be done
9224 before we start to fill in any display lines, because it can call
9225 eval.
9227 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9229 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9230 already ran the menu bar hooks for this redisplay, so there
9231 is no need to run them again. The return value is the
9232 updated value of this flag, to pass to the next call. */
9234 static int
9235 update_menu_bar (f, save_match_data, hooks_run)
9236 struct frame *f;
9237 int save_match_data;
9238 int hooks_run;
9240 Lisp_Object window;
9241 register struct window *w;
9243 /* If called recursively during a menu update, do nothing. This can
9244 happen when, for instance, an activate-menubar-hook causes a
9245 redisplay. */
9246 if (inhibit_menubar_update)
9247 return hooks_run;
9249 window = FRAME_SELECTED_WINDOW (f);
9250 w = XWINDOW (window);
9252 #if 0 /* The if statement below this if statement used to include the
9253 condition !NILP (w->update_mode_line), rather than using
9254 update_mode_lines directly, and this if statement may have
9255 been added to make that condition work. Now the if
9256 statement below matches its comment, this isn't needed. */
9257 if (update_mode_lines)
9258 w->update_mode_line = Qt;
9259 #endif
9261 if (FRAME_WINDOW_P (f)
9263 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9264 || defined (USE_GTK)
9265 FRAME_EXTERNAL_MENU_BAR (f)
9266 #else
9267 FRAME_MENU_BAR_LINES (f) > 0
9268 #endif
9269 : FRAME_MENU_BAR_LINES (f) > 0)
9271 /* If the user has switched buffers or windows, we need to
9272 recompute to reflect the new bindings. But we'll
9273 recompute when update_mode_lines is set too; that means
9274 that people can use force-mode-line-update to request
9275 that the menu bar be recomputed. The adverse effect on
9276 the rest of the redisplay algorithm is about the same as
9277 windows_or_buffers_changed anyway. */
9278 if (windows_or_buffers_changed
9279 /* This used to test w->update_mode_line, but we believe
9280 there is no need to recompute the menu in that case. */
9281 || update_mode_lines
9282 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9283 < BUF_MODIFF (XBUFFER (w->buffer)))
9284 != !NILP (w->last_had_star))
9285 || ((!NILP (Vtransient_mark_mode)
9286 && !NILP (XBUFFER (w->buffer)->mark_active))
9287 != !NILP (w->region_showing)))
9289 struct buffer *prev = current_buffer;
9290 int count = SPECPDL_INDEX ();
9292 specbind (Qinhibit_menubar_update, Qt);
9294 set_buffer_internal_1 (XBUFFER (w->buffer));
9295 if (save_match_data)
9296 record_unwind_save_match_data ();
9297 if (NILP (Voverriding_local_map_menu_flag))
9299 specbind (Qoverriding_terminal_local_map, Qnil);
9300 specbind (Qoverriding_local_map, Qnil);
9303 if (!hooks_run)
9305 /* Run the Lucid hook. */
9306 safe_run_hooks (Qactivate_menubar_hook);
9308 /* If it has changed current-menubar from previous value,
9309 really recompute the menu-bar from the value. */
9310 if (! NILP (Vlucid_menu_bar_dirty_flag))
9311 call0 (Qrecompute_lucid_menubar);
9313 safe_run_hooks (Qmenu_bar_update_hook);
9315 hooks_run = 1;
9318 XSETFRAME (Vmenu_updating_frame, f);
9319 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9321 /* Redisplay the menu bar in case we changed it. */
9322 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9323 || defined (USE_GTK)
9324 if (FRAME_WINDOW_P (f))
9326 #ifdef MAC_OS
9327 /* All frames on Mac OS share the same menubar. So only
9328 the selected frame should be allowed to set it. */
9329 if (f == SELECTED_FRAME ())
9330 #endif
9331 set_frame_menubar (f, 0, 0);
9333 else
9334 /* On a terminal screen, the menu bar is an ordinary screen
9335 line, and this makes it get updated. */
9336 w->update_mode_line = Qt;
9337 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9338 /* In the non-toolkit version, the menu bar is an ordinary screen
9339 line, and this makes it get updated. */
9340 w->update_mode_line = Qt;
9341 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9343 unbind_to (count, Qnil);
9344 set_buffer_internal_1 (prev);
9348 return hooks_run;
9353 /***********************************************************************
9354 Output Cursor
9355 ***********************************************************************/
9357 #ifdef HAVE_WINDOW_SYSTEM
9359 /* EXPORT:
9360 Nominal cursor position -- where to draw output.
9361 HPOS and VPOS are window relative glyph matrix coordinates.
9362 X and Y are window relative pixel coordinates. */
9364 struct cursor_pos output_cursor;
9367 /* EXPORT:
9368 Set the global variable output_cursor to CURSOR. All cursor
9369 positions are relative to updated_window. */
9371 void
9372 set_output_cursor (cursor)
9373 struct cursor_pos *cursor;
9375 output_cursor.hpos = cursor->hpos;
9376 output_cursor.vpos = cursor->vpos;
9377 output_cursor.x = cursor->x;
9378 output_cursor.y = cursor->y;
9382 /* EXPORT for RIF:
9383 Set a nominal cursor position.
9385 HPOS and VPOS are column/row positions in a window glyph matrix. X
9386 and Y are window text area relative pixel positions.
9388 If this is done during an update, updated_window will contain the
9389 window that is being updated and the position is the future output
9390 cursor position for that window. If updated_window is null, use
9391 selected_window and display the cursor at the given position. */
9393 void
9394 x_cursor_to (vpos, hpos, y, x)
9395 int vpos, hpos, y, x;
9397 struct window *w;
9399 /* If updated_window is not set, work on selected_window. */
9400 if (updated_window)
9401 w = updated_window;
9402 else
9403 w = XWINDOW (selected_window);
9405 /* Set the output cursor. */
9406 output_cursor.hpos = hpos;
9407 output_cursor.vpos = vpos;
9408 output_cursor.x = x;
9409 output_cursor.y = y;
9411 /* If not called as part of an update, really display the cursor.
9412 This will also set the cursor position of W. */
9413 if (updated_window == NULL)
9415 BLOCK_INPUT;
9416 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9417 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9418 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9419 UNBLOCK_INPUT;
9423 #endif /* HAVE_WINDOW_SYSTEM */
9426 /***********************************************************************
9427 Tool-bars
9428 ***********************************************************************/
9430 #ifdef HAVE_WINDOW_SYSTEM
9432 /* Where the mouse was last time we reported a mouse event. */
9434 FRAME_PTR last_mouse_frame;
9436 /* Tool-bar item index of the item on which a mouse button was pressed
9437 or -1. */
9439 int last_tool_bar_item;
9442 /* Update the tool-bar item list for frame F. This has to be done
9443 before we start to fill in any display lines. Called from
9444 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9445 and restore it here. */
9447 static void
9448 update_tool_bar (f, save_match_data)
9449 struct frame *f;
9450 int save_match_data;
9452 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9453 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9454 #else
9455 int do_update = WINDOWP (f->tool_bar_window)
9456 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9457 #endif
9459 if (do_update)
9461 Lisp_Object window;
9462 struct window *w;
9464 window = FRAME_SELECTED_WINDOW (f);
9465 w = XWINDOW (window);
9467 /* If the user has switched buffers or windows, we need to
9468 recompute to reflect the new bindings. But we'll
9469 recompute when update_mode_lines is set too; that means
9470 that people can use force-mode-line-update to request
9471 that the menu bar be recomputed. The adverse effect on
9472 the rest of the redisplay algorithm is about the same as
9473 windows_or_buffers_changed anyway. */
9474 if (windows_or_buffers_changed
9475 || !NILP (w->update_mode_line)
9476 || update_mode_lines
9477 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9478 < BUF_MODIFF (XBUFFER (w->buffer)))
9479 != !NILP (w->last_had_star))
9480 || ((!NILP (Vtransient_mark_mode)
9481 && !NILP (XBUFFER (w->buffer)->mark_active))
9482 != !NILP (w->region_showing)))
9484 struct buffer *prev = current_buffer;
9485 int count = SPECPDL_INDEX ();
9486 Lisp_Object new_tool_bar;
9487 int new_n_tool_bar;
9488 struct gcpro gcpro1;
9490 /* Set current_buffer to the buffer of the selected
9491 window of the frame, so that we get the right local
9492 keymaps. */
9493 set_buffer_internal_1 (XBUFFER (w->buffer));
9495 /* Save match data, if we must. */
9496 if (save_match_data)
9497 record_unwind_save_match_data ();
9499 /* Make sure that we don't accidentally use bogus keymaps. */
9500 if (NILP (Voverriding_local_map_menu_flag))
9502 specbind (Qoverriding_terminal_local_map, Qnil);
9503 specbind (Qoverriding_local_map, Qnil);
9506 GCPRO1 (new_tool_bar);
9508 /* Build desired tool-bar items from keymaps. */
9509 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9510 &new_n_tool_bar);
9512 /* Redisplay the tool-bar if we changed it. */
9513 if (new_n_tool_bar != f->n_tool_bar_items
9514 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9516 /* Redisplay that happens asynchronously due to an expose event
9517 may access f->tool_bar_items. Make sure we update both
9518 variables within BLOCK_INPUT so no such event interrupts. */
9519 BLOCK_INPUT;
9520 f->tool_bar_items = new_tool_bar;
9521 f->n_tool_bar_items = new_n_tool_bar;
9522 w->update_mode_line = Qt;
9523 UNBLOCK_INPUT;
9526 UNGCPRO;
9528 unbind_to (count, Qnil);
9529 set_buffer_internal_1 (prev);
9535 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9536 F's desired tool-bar contents. F->tool_bar_items must have
9537 been set up previously by calling prepare_menu_bars. */
9539 static void
9540 build_desired_tool_bar_string (f)
9541 struct frame *f;
9543 int i, size, size_needed;
9544 struct gcpro gcpro1, gcpro2, gcpro3;
9545 Lisp_Object image, plist, props;
9547 image = plist = props = Qnil;
9548 GCPRO3 (image, plist, props);
9550 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9551 Otherwise, make a new string. */
9553 /* The size of the string we might be able to reuse. */
9554 size = (STRINGP (f->desired_tool_bar_string)
9555 ? SCHARS (f->desired_tool_bar_string)
9556 : 0);
9558 /* We need one space in the string for each image. */
9559 size_needed = f->n_tool_bar_items;
9561 /* Reuse f->desired_tool_bar_string, if possible. */
9562 if (size < size_needed || NILP (f->desired_tool_bar_string))
9563 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9564 make_number (' '));
9565 else
9567 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9568 Fremove_text_properties (make_number (0), make_number (size),
9569 props, f->desired_tool_bar_string);
9572 /* Put a `display' property on the string for the images to display,
9573 put a `menu_item' property on tool-bar items with a value that
9574 is the index of the item in F's tool-bar item vector. */
9575 for (i = 0; i < f->n_tool_bar_items; ++i)
9577 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9579 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9580 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9581 int hmargin, vmargin, relief, idx, end;
9582 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9584 /* If image is a vector, choose the image according to the
9585 button state. */
9586 image = PROP (TOOL_BAR_ITEM_IMAGES);
9587 if (VECTORP (image))
9589 if (enabled_p)
9590 idx = (selected_p
9591 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9592 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9593 else
9594 idx = (selected_p
9595 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9596 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9598 xassert (ASIZE (image) >= idx);
9599 image = AREF (image, idx);
9601 else
9602 idx = -1;
9604 /* Ignore invalid image specifications. */
9605 if (!valid_image_p (image))
9606 continue;
9608 /* Display the tool-bar button pressed, or depressed. */
9609 plist = Fcopy_sequence (XCDR (image));
9611 /* Compute margin and relief to draw. */
9612 relief = (tool_bar_button_relief >= 0
9613 ? tool_bar_button_relief
9614 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9615 hmargin = vmargin = relief;
9617 if (INTEGERP (Vtool_bar_button_margin)
9618 && XINT (Vtool_bar_button_margin) > 0)
9620 hmargin += XFASTINT (Vtool_bar_button_margin);
9621 vmargin += XFASTINT (Vtool_bar_button_margin);
9623 else if (CONSP (Vtool_bar_button_margin))
9625 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9626 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9627 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9629 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9630 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9631 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9634 if (auto_raise_tool_bar_buttons_p)
9636 /* Add a `:relief' property to the image spec if the item is
9637 selected. */
9638 if (selected_p)
9640 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9641 hmargin -= relief;
9642 vmargin -= relief;
9645 else
9647 /* If image is selected, display it pressed, i.e. with a
9648 negative relief. If it's not selected, display it with a
9649 raised relief. */
9650 plist = Fplist_put (plist, QCrelief,
9651 (selected_p
9652 ? make_number (-relief)
9653 : make_number (relief)));
9654 hmargin -= relief;
9655 vmargin -= relief;
9658 /* Put a margin around the image. */
9659 if (hmargin || vmargin)
9661 if (hmargin == vmargin)
9662 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9663 else
9664 plist = Fplist_put (plist, QCmargin,
9665 Fcons (make_number (hmargin),
9666 make_number (vmargin)));
9669 /* If button is not enabled, and we don't have special images
9670 for the disabled state, make the image appear disabled by
9671 applying an appropriate algorithm to it. */
9672 if (!enabled_p && idx < 0)
9673 plist = Fplist_put (plist, QCconversion, Qdisabled);
9675 /* Put a `display' text property on the string for the image to
9676 display. Put a `menu-item' property on the string that gives
9677 the start of this item's properties in the tool-bar items
9678 vector. */
9679 image = Fcons (Qimage, plist);
9680 props = list4 (Qdisplay, image,
9681 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9683 /* Let the last image hide all remaining spaces in the tool bar
9684 string. The string can be longer than needed when we reuse a
9685 previous string. */
9686 if (i + 1 == f->n_tool_bar_items)
9687 end = SCHARS (f->desired_tool_bar_string);
9688 else
9689 end = i + 1;
9690 Fadd_text_properties (make_number (i), make_number (end),
9691 props, f->desired_tool_bar_string);
9692 #undef PROP
9695 UNGCPRO;
9699 /* Display one line of the tool-bar of frame IT->f.
9701 HEIGHT specifies the desired height of the tool-bar line.
9702 If the actual height of the glyph row is less than HEIGHT, the
9703 row's height is increased to HEIGHT, and the icons are centered
9704 vertically in the new height.
9706 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9707 count a final empty row in case the tool-bar width exactly matches
9708 the window width.
9711 static void
9712 display_tool_bar_line (it, height)
9713 struct it *it;
9714 int height;
9716 struct glyph_row *row = it->glyph_row;
9717 int max_x = it->last_visible_x;
9718 struct glyph *last;
9720 prepare_desired_row (row);
9721 row->y = it->current_y;
9723 /* Note that this isn't made use of if the face hasn't a box,
9724 so there's no need to check the face here. */
9725 it->start_of_box_run_p = 1;
9727 while (it->current_x < max_x)
9729 int x, n_glyphs_before, i, nglyphs;
9730 struct it it_before;
9732 /* Get the next display element. */
9733 if (!get_next_display_element (it))
9735 /* Don't count empty row if we are counting needed tool-bar lines. */
9736 if (height < 0 && !it->hpos)
9737 return;
9738 break;
9741 /* Produce glyphs. */
9742 n_glyphs_before = row->used[TEXT_AREA];
9743 it_before = *it;
9745 PRODUCE_GLYPHS (it);
9747 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9748 i = 0;
9749 x = it_before.current_x;
9750 while (i < nglyphs)
9752 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9754 if (x + glyph->pixel_width > max_x)
9756 /* Glyph doesn't fit on line. Backtrack. */
9757 row->used[TEXT_AREA] = n_glyphs_before;
9758 *it = it_before;
9759 /* If this is the only glyph on this line, it will never fit on the
9760 toolbar, so skip it. But ensure there is at least one glyph,
9761 so we don't accidentally disable the tool-bar. */
9762 if (n_glyphs_before == 0
9763 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9764 break;
9765 goto out;
9768 ++it->hpos;
9769 x += glyph->pixel_width;
9770 ++i;
9773 /* Stop at line ends. */
9774 if (ITERATOR_AT_END_OF_LINE_P (it))
9775 break;
9777 set_iterator_to_next (it, 1);
9780 out:;
9782 row->displays_text_p = row->used[TEXT_AREA] != 0;
9784 /* Use default face for the border below the tool bar.
9786 FIXME: When auto-resize-tool-bars is grow-only, there is
9787 no additional border below the possibly empty tool-bar lines.
9788 So to make the extra empty lines look "normal", we have to
9789 use the tool-bar face for the border too. */
9790 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9791 it->face_id = DEFAULT_FACE_ID;
9793 extend_face_to_end_of_line (it);
9794 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9795 last->right_box_line_p = 1;
9796 if (last == row->glyphs[TEXT_AREA])
9797 last->left_box_line_p = 1;
9799 /* Make line the desired height and center it vertically. */
9800 if ((height -= it->max_ascent + it->max_descent) > 0)
9802 /* Don't add more than one line height. */
9803 height %= FRAME_LINE_HEIGHT (it->f);
9804 it->max_ascent += height / 2;
9805 it->max_descent += (height + 1) / 2;
9808 compute_line_metrics (it);
9810 /* If line is empty, make it occupy the rest of the tool-bar. */
9811 if (!row->displays_text_p)
9813 row->height = row->phys_height = it->last_visible_y - row->y;
9814 row->visible_height = row->height;
9815 row->ascent = row->phys_ascent = 0;
9816 row->extra_line_spacing = 0;
9819 row->full_width_p = 1;
9820 row->continued_p = 0;
9821 row->truncated_on_left_p = 0;
9822 row->truncated_on_right_p = 0;
9824 it->current_x = it->hpos = 0;
9825 it->current_y += row->height;
9826 ++it->vpos;
9827 ++it->glyph_row;
9831 /* Max tool-bar height. */
9833 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9834 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9836 /* Value is the number of screen lines needed to make all tool-bar
9837 items of frame F visible. The number of actual rows needed is
9838 returned in *N_ROWS if non-NULL. */
9840 static int
9841 tool_bar_lines_needed (f, n_rows)
9842 struct frame *f;
9843 int *n_rows;
9845 struct window *w = XWINDOW (f->tool_bar_window);
9846 struct it it;
9847 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9848 the desired matrix, so use (unused) mode-line row as temporary row to
9849 avoid destroying the first tool-bar row. */
9850 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9852 /* Initialize an iterator for iteration over
9853 F->desired_tool_bar_string in the tool-bar window of frame F. */
9854 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9855 it.first_visible_x = 0;
9856 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9857 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9859 while (!ITERATOR_AT_END_P (&it))
9861 clear_glyph_row (temp_row);
9862 it.glyph_row = temp_row;
9863 display_tool_bar_line (&it, -1);
9865 clear_glyph_row (temp_row);
9867 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9868 if (n_rows)
9869 *n_rows = it.vpos > 0 ? it.vpos : -1;
9871 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9875 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9876 0, 1, 0,
9877 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9878 (frame)
9879 Lisp_Object frame;
9881 struct frame *f;
9882 struct window *w;
9883 int nlines = 0;
9885 if (NILP (frame))
9886 frame = selected_frame;
9887 else
9888 CHECK_FRAME (frame);
9889 f = XFRAME (frame);
9891 if (WINDOWP (f->tool_bar_window)
9892 || (w = XWINDOW (f->tool_bar_window),
9893 WINDOW_TOTAL_LINES (w) > 0))
9895 update_tool_bar (f, 1);
9896 if (f->n_tool_bar_items)
9898 build_desired_tool_bar_string (f);
9899 nlines = tool_bar_lines_needed (f, NULL);
9903 return make_number (nlines);
9907 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9908 height should be changed. */
9910 static int
9911 redisplay_tool_bar (f)
9912 struct frame *f;
9914 struct window *w;
9915 struct it it;
9916 struct glyph_row *row;
9918 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9919 if (FRAME_EXTERNAL_TOOL_BAR (f))
9920 update_frame_tool_bar (f);
9921 return 0;
9922 #endif
9924 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9925 do anything. This means you must start with tool-bar-lines
9926 non-zero to get the auto-sizing effect. Or in other words, you
9927 can turn off tool-bars by specifying tool-bar-lines zero. */
9928 if (!WINDOWP (f->tool_bar_window)
9929 || (w = XWINDOW (f->tool_bar_window),
9930 WINDOW_TOTAL_LINES (w) == 0))
9931 return 0;
9933 /* Set up an iterator for the tool-bar window. */
9934 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9935 it.first_visible_x = 0;
9936 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9937 row = it.glyph_row;
9939 /* Build a string that represents the contents of the tool-bar. */
9940 build_desired_tool_bar_string (f);
9941 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9943 if (f->n_tool_bar_rows == 0)
9945 int nlines;
9947 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9948 nlines != WINDOW_TOTAL_LINES (w)))
9950 extern Lisp_Object Qtool_bar_lines;
9951 Lisp_Object frame;
9952 int old_height = WINDOW_TOTAL_LINES (w);
9954 XSETFRAME (frame, f);
9955 Fmodify_frame_parameters (frame,
9956 Fcons (Fcons (Qtool_bar_lines,
9957 make_number (nlines)),
9958 Qnil));
9959 if (WINDOW_TOTAL_LINES (w) != old_height)
9961 clear_glyph_matrix (w->desired_matrix);
9962 fonts_changed_p = 1;
9963 return 1;
9968 /* Display as many lines as needed to display all tool-bar items. */
9970 if (f->n_tool_bar_rows > 0)
9972 int border, rows, height, extra;
9974 if (INTEGERP (Vtool_bar_border))
9975 border = XINT (Vtool_bar_border);
9976 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9977 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9978 else if (EQ (Vtool_bar_border, Qborder_width))
9979 border = f->border_width;
9980 else
9981 border = 0;
9982 if (border < 0)
9983 border = 0;
9985 rows = f->n_tool_bar_rows;
9986 height = max (1, (it.last_visible_y - border) / rows);
9987 extra = it.last_visible_y - border - height * rows;
9989 while (it.current_y < it.last_visible_y)
9991 int h = 0;
9992 if (extra > 0 && rows-- > 0)
9994 h = (extra + rows - 1) / rows;
9995 extra -= h;
9997 display_tool_bar_line (&it, height + h);
10000 else
10002 while (it.current_y < it.last_visible_y)
10003 display_tool_bar_line (&it, 0);
10006 /* It doesn't make much sense to try scrolling in the tool-bar
10007 window, so don't do it. */
10008 w->desired_matrix->no_scrolling_p = 1;
10009 w->must_be_updated_p = 1;
10011 if (!NILP (Vauto_resize_tool_bars))
10013 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10014 int change_height_p = 0;
10016 /* If we couldn't display everything, change the tool-bar's
10017 height if there is room for more. */
10018 if (IT_STRING_CHARPOS (it) < it.end_charpos
10019 && it.current_y < max_tool_bar_height)
10020 change_height_p = 1;
10022 row = it.glyph_row - 1;
10024 /* If there are blank lines at the end, except for a partially
10025 visible blank line at the end that is smaller than
10026 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10027 if (!row->displays_text_p
10028 && row->height >= FRAME_LINE_HEIGHT (f))
10029 change_height_p = 1;
10031 /* If row displays tool-bar items, but is partially visible,
10032 change the tool-bar's height. */
10033 if (row->displays_text_p
10034 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10035 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10036 change_height_p = 1;
10038 /* Resize windows as needed by changing the `tool-bar-lines'
10039 frame parameter. */
10040 if (change_height_p)
10042 extern Lisp_Object Qtool_bar_lines;
10043 Lisp_Object frame;
10044 int old_height = WINDOW_TOTAL_LINES (w);
10045 int nrows;
10046 int nlines = tool_bar_lines_needed (f, &nrows);
10048 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10049 && !f->minimize_tool_bar_window_p)
10050 ? (nlines > old_height)
10051 : (nlines != old_height));
10052 f->minimize_tool_bar_window_p = 0;
10054 if (change_height_p)
10056 XSETFRAME (frame, f);
10057 Fmodify_frame_parameters (frame,
10058 Fcons (Fcons (Qtool_bar_lines,
10059 make_number (nlines)),
10060 Qnil));
10061 if (WINDOW_TOTAL_LINES (w) != old_height)
10063 clear_glyph_matrix (w->desired_matrix);
10064 f->n_tool_bar_rows = nrows;
10065 fonts_changed_p = 1;
10066 return 1;
10072 f->minimize_tool_bar_window_p = 0;
10073 return 0;
10077 /* Get information about the tool-bar item which is displayed in GLYPH
10078 on frame F. Return in *PROP_IDX the index where tool-bar item
10079 properties start in F->tool_bar_items. Value is zero if
10080 GLYPH doesn't display a tool-bar item. */
10082 static int
10083 tool_bar_item_info (f, glyph, prop_idx)
10084 struct frame *f;
10085 struct glyph *glyph;
10086 int *prop_idx;
10088 Lisp_Object prop;
10089 int success_p;
10090 int charpos;
10092 /* This function can be called asynchronously, which means we must
10093 exclude any possibility that Fget_text_property signals an
10094 error. */
10095 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10096 charpos = max (0, charpos);
10098 /* Get the text property `menu-item' at pos. The value of that
10099 property is the start index of this item's properties in
10100 F->tool_bar_items. */
10101 prop = Fget_text_property (make_number (charpos),
10102 Qmenu_item, f->current_tool_bar_string);
10103 if (INTEGERP (prop))
10105 *prop_idx = XINT (prop);
10106 success_p = 1;
10108 else
10109 success_p = 0;
10111 return success_p;
10115 /* Get information about the tool-bar item at position X/Y on frame F.
10116 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10117 the current matrix of the tool-bar window of F, or NULL if not
10118 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10119 item in F->tool_bar_items. Value is
10121 -1 if X/Y is not on a tool-bar item
10122 0 if X/Y is on the same item that was highlighted before.
10123 1 otherwise. */
10125 static int
10126 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10127 struct frame *f;
10128 int x, y;
10129 struct glyph **glyph;
10130 int *hpos, *vpos, *prop_idx;
10132 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10133 struct window *w = XWINDOW (f->tool_bar_window);
10134 int area;
10136 /* Find the glyph under X/Y. */
10137 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10138 if (*glyph == NULL)
10139 return -1;
10141 /* Get the start of this tool-bar item's properties in
10142 f->tool_bar_items. */
10143 if (!tool_bar_item_info (f, *glyph, prop_idx))
10144 return -1;
10146 /* Is mouse on the highlighted item? */
10147 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10148 && *vpos >= dpyinfo->mouse_face_beg_row
10149 && *vpos <= dpyinfo->mouse_face_end_row
10150 && (*vpos > dpyinfo->mouse_face_beg_row
10151 || *hpos >= dpyinfo->mouse_face_beg_col)
10152 && (*vpos < dpyinfo->mouse_face_end_row
10153 || *hpos < dpyinfo->mouse_face_end_col
10154 || dpyinfo->mouse_face_past_end))
10155 return 0;
10157 return 1;
10161 /* EXPORT:
10162 Handle mouse button event on the tool-bar of frame F, at
10163 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10164 0 for button release. MODIFIERS is event modifiers for button
10165 release. */
10167 void
10168 handle_tool_bar_click (f, x, y, down_p, modifiers)
10169 struct frame *f;
10170 int x, y, down_p;
10171 unsigned int modifiers;
10173 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10174 struct window *w = XWINDOW (f->tool_bar_window);
10175 int hpos, vpos, prop_idx;
10176 struct glyph *glyph;
10177 Lisp_Object enabled_p;
10179 /* If not on the highlighted tool-bar item, return. */
10180 frame_to_window_pixel_xy (w, &x, &y);
10181 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10182 return;
10184 /* If item is disabled, do nothing. */
10185 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10186 if (NILP (enabled_p))
10187 return;
10189 if (down_p)
10191 /* Show item in pressed state. */
10192 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10193 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10194 last_tool_bar_item = prop_idx;
10196 else
10198 Lisp_Object key, frame;
10199 struct input_event event;
10200 EVENT_INIT (event);
10202 /* Show item in released state. */
10203 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10204 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10206 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10208 XSETFRAME (frame, f);
10209 event.kind = TOOL_BAR_EVENT;
10210 event.frame_or_window = frame;
10211 event.arg = frame;
10212 kbd_buffer_store_event (&event);
10214 event.kind = TOOL_BAR_EVENT;
10215 event.frame_or_window = frame;
10216 event.arg = key;
10217 event.modifiers = modifiers;
10218 kbd_buffer_store_event (&event);
10219 last_tool_bar_item = -1;
10224 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10225 tool-bar window-relative coordinates X/Y. Called from
10226 note_mouse_highlight. */
10228 static void
10229 note_tool_bar_highlight (f, x, y)
10230 struct frame *f;
10231 int x, y;
10233 Lisp_Object window = f->tool_bar_window;
10234 struct window *w = XWINDOW (window);
10235 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10236 int hpos, vpos;
10237 struct glyph *glyph;
10238 struct glyph_row *row;
10239 int i;
10240 Lisp_Object enabled_p;
10241 int prop_idx;
10242 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10243 int mouse_down_p, rc;
10245 /* Function note_mouse_highlight is called with negative x(y
10246 values when mouse moves outside of the frame. */
10247 if (x <= 0 || y <= 0)
10249 clear_mouse_face (dpyinfo);
10250 return;
10253 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10254 if (rc < 0)
10256 /* Not on tool-bar item. */
10257 clear_mouse_face (dpyinfo);
10258 return;
10260 else if (rc == 0)
10261 /* On same tool-bar item as before. */
10262 goto set_help_echo;
10264 clear_mouse_face (dpyinfo);
10266 /* Mouse is down, but on different tool-bar item? */
10267 mouse_down_p = (dpyinfo->grabbed
10268 && f == last_mouse_frame
10269 && FRAME_LIVE_P (f));
10270 if (mouse_down_p
10271 && last_tool_bar_item != prop_idx)
10272 return;
10274 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10275 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10277 /* If tool-bar item is not enabled, don't highlight it. */
10278 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10279 if (!NILP (enabled_p))
10281 /* Compute the x-position of the glyph. In front and past the
10282 image is a space. We include this in the highlighted area. */
10283 row = MATRIX_ROW (w->current_matrix, vpos);
10284 for (i = x = 0; i < hpos; ++i)
10285 x += row->glyphs[TEXT_AREA][i].pixel_width;
10287 /* Record this as the current active region. */
10288 dpyinfo->mouse_face_beg_col = hpos;
10289 dpyinfo->mouse_face_beg_row = vpos;
10290 dpyinfo->mouse_face_beg_x = x;
10291 dpyinfo->mouse_face_beg_y = row->y;
10292 dpyinfo->mouse_face_past_end = 0;
10294 dpyinfo->mouse_face_end_col = hpos + 1;
10295 dpyinfo->mouse_face_end_row = vpos;
10296 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10297 dpyinfo->mouse_face_end_y = row->y;
10298 dpyinfo->mouse_face_window = window;
10299 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10301 /* Display it as active. */
10302 show_mouse_face (dpyinfo, draw);
10303 dpyinfo->mouse_face_image_state = draw;
10306 set_help_echo:
10308 /* Set help_echo_string to a help string to display for this tool-bar item.
10309 XTread_socket does the rest. */
10310 help_echo_object = help_echo_window = Qnil;
10311 help_echo_pos = -1;
10312 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10313 if (NILP (help_echo_string))
10314 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10317 #endif /* HAVE_WINDOW_SYSTEM */
10321 /************************************************************************
10322 Horizontal scrolling
10323 ************************************************************************/
10325 static int hscroll_window_tree P_ ((Lisp_Object));
10326 static int hscroll_windows P_ ((Lisp_Object));
10328 /* For all leaf windows in the window tree rooted at WINDOW, set their
10329 hscroll value so that PT is (i) visible in the window, and (ii) so
10330 that it is not within a certain margin at the window's left and
10331 right border. Value is non-zero if any window's hscroll has been
10332 changed. */
10334 static int
10335 hscroll_window_tree (window)
10336 Lisp_Object window;
10338 int hscrolled_p = 0;
10339 int hscroll_relative_p = FLOATP (Vhscroll_step);
10340 int hscroll_step_abs = 0;
10341 double hscroll_step_rel = 0;
10343 if (hscroll_relative_p)
10345 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10346 if (hscroll_step_rel < 0)
10348 hscroll_relative_p = 0;
10349 hscroll_step_abs = 0;
10352 else if (INTEGERP (Vhscroll_step))
10354 hscroll_step_abs = XINT (Vhscroll_step);
10355 if (hscroll_step_abs < 0)
10356 hscroll_step_abs = 0;
10358 else
10359 hscroll_step_abs = 0;
10361 while (WINDOWP (window))
10363 struct window *w = XWINDOW (window);
10365 if (WINDOWP (w->hchild))
10366 hscrolled_p |= hscroll_window_tree (w->hchild);
10367 else if (WINDOWP (w->vchild))
10368 hscrolled_p |= hscroll_window_tree (w->vchild);
10369 else if (w->cursor.vpos >= 0)
10371 int h_margin;
10372 int text_area_width;
10373 struct glyph_row *current_cursor_row
10374 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10375 struct glyph_row *desired_cursor_row
10376 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10377 struct glyph_row *cursor_row
10378 = (desired_cursor_row->enabled_p
10379 ? desired_cursor_row
10380 : current_cursor_row);
10382 text_area_width = window_box_width (w, TEXT_AREA);
10384 /* Scroll when cursor is inside this scroll margin. */
10385 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10387 if ((XFASTINT (w->hscroll)
10388 && w->cursor.x <= h_margin)
10389 || (cursor_row->enabled_p
10390 && cursor_row->truncated_on_right_p
10391 && (w->cursor.x >= text_area_width - h_margin)))
10393 struct it it;
10394 int hscroll;
10395 struct buffer *saved_current_buffer;
10396 int pt;
10397 int wanted_x;
10399 /* Find point in a display of infinite width. */
10400 saved_current_buffer = current_buffer;
10401 current_buffer = XBUFFER (w->buffer);
10403 if (w == XWINDOW (selected_window))
10404 pt = BUF_PT (current_buffer);
10405 else
10407 pt = marker_position (w->pointm);
10408 pt = max (BEGV, pt);
10409 pt = min (ZV, pt);
10412 /* Move iterator to pt starting at cursor_row->start in
10413 a line with infinite width. */
10414 init_to_row_start (&it, w, cursor_row);
10415 it.last_visible_x = INFINITY;
10416 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10417 current_buffer = saved_current_buffer;
10419 /* Position cursor in window. */
10420 if (!hscroll_relative_p && hscroll_step_abs == 0)
10421 hscroll = max (0, (it.current_x
10422 - (ITERATOR_AT_END_OF_LINE_P (&it)
10423 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10424 : (text_area_width / 2))))
10425 / FRAME_COLUMN_WIDTH (it.f);
10426 else if (w->cursor.x >= text_area_width - h_margin)
10428 if (hscroll_relative_p)
10429 wanted_x = text_area_width * (1 - hscroll_step_rel)
10430 - h_margin;
10431 else
10432 wanted_x = text_area_width
10433 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10434 - h_margin;
10435 hscroll
10436 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10438 else
10440 if (hscroll_relative_p)
10441 wanted_x = text_area_width * hscroll_step_rel
10442 + h_margin;
10443 else
10444 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10445 + h_margin;
10446 hscroll
10447 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10449 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10451 /* Don't call Fset_window_hscroll if value hasn't
10452 changed because it will prevent redisplay
10453 optimizations. */
10454 if (XFASTINT (w->hscroll) != hscroll)
10456 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10457 w->hscroll = make_number (hscroll);
10458 hscrolled_p = 1;
10463 window = w->next;
10466 /* Value is non-zero if hscroll of any leaf window has been changed. */
10467 return hscrolled_p;
10471 /* Set hscroll so that cursor is visible and not inside horizontal
10472 scroll margins for all windows in the tree rooted at WINDOW. See
10473 also hscroll_window_tree above. Value is non-zero if any window's
10474 hscroll has been changed. If it has, desired matrices on the frame
10475 of WINDOW are cleared. */
10477 static int
10478 hscroll_windows (window)
10479 Lisp_Object window;
10481 int hscrolled_p;
10483 if (automatic_hscrolling_p)
10485 hscrolled_p = hscroll_window_tree (window);
10486 if (hscrolled_p)
10487 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10489 else
10490 hscrolled_p = 0;
10491 return hscrolled_p;
10496 /************************************************************************
10497 Redisplay
10498 ************************************************************************/
10500 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10501 to a non-zero value. This is sometimes handy to have in a debugger
10502 session. */
10504 #if GLYPH_DEBUG
10506 /* First and last unchanged row for try_window_id. */
10508 int debug_first_unchanged_at_end_vpos;
10509 int debug_last_unchanged_at_beg_vpos;
10511 /* Delta vpos and y. */
10513 int debug_dvpos, debug_dy;
10515 /* Delta in characters and bytes for try_window_id. */
10517 int debug_delta, debug_delta_bytes;
10519 /* Values of window_end_pos and window_end_vpos at the end of
10520 try_window_id. */
10522 EMACS_INT debug_end_pos, debug_end_vpos;
10524 /* Append a string to W->desired_matrix->method. FMT is a printf
10525 format string. A1...A9 are a supplement for a variable-length
10526 argument list. If trace_redisplay_p is non-zero also printf the
10527 resulting string to stderr. */
10529 static void
10530 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10531 struct window *w;
10532 char *fmt;
10533 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10535 char buffer[512];
10536 char *method = w->desired_matrix->method;
10537 int len = strlen (method);
10538 int size = sizeof w->desired_matrix->method;
10539 int remaining = size - len - 1;
10541 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10542 if (len && remaining)
10544 method[len] = '|';
10545 --remaining, ++len;
10548 strncpy (method + len, buffer, remaining);
10550 if (trace_redisplay_p)
10551 fprintf (stderr, "%p (%s): %s\n",
10553 ((BUFFERP (w->buffer)
10554 && STRINGP (XBUFFER (w->buffer)->name))
10555 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10556 : "no buffer"),
10557 buffer);
10560 #endif /* GLYPH_DEBUG */
10563 /* Value is non-zero if all changes in window W, which displays
10564 current_buffer, are in the text between START and END. START is a
10565 buffer position, END is given as a distance from Z. Used in
10566 redisplay_internal for display optimization. */
10568 static INLINE int
10569 text_outside_line_unchanged_p (w, start, end)
10570 struct window *w;
10571 int start, end;
10573 int unchanged_p = 1;
10575 /* If text or overlays have changed, see where. */
10576 if (XFASTINT (w->last_modified) < MODIFF
10577 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10579 /* Gap in the line? */
10580 if (GPT < start || Z - GPT < end)
10581 unchanged_p = 0;
10583 /* Changes start in front of the line, or end after it? */
10584 if (unchanged_p
10585 && (BEG_UNCHANGED < start - 1
10586 || END_UNCHANGED < end))
10587 unchanged_p = 0;
10589 /* If selective display, can't optimize if changes start at the
10590 beginning of the line. */
10591 if (unchanged_p
10592 && INTEGERP (current_buffer->selective_display)
10593 && XINT (current_buffer->selective_display) > 0
10594 && (BEG_UNCHANGED < start || GPT <= start))
10595 unchanged_p = 0;
10597 /* If there are overlays at the start or end of the line, these
10598 may have overlay strings with newlines in them. A change at
10599 START, for instance, may actually concern the display of such
10600 overlay strings as well, and they are displayed on different
10601 lines. So, quickly rule out this case. (For the future, it
10602 might be desirable to implement something more telling than
10603 just BEG/END_UNCHANGED.) */
10604 if (unchanged_p)
10606 if (BEG + BEG_UNCHANGED == start
10607 && overlay_touches_p (start))
10608 unchanged_p = 0;
10609 if (END_UNCHANGED == end
10610 && overlay_touches_p (Z - end))
10611 unchanged_p = 0;
10615 return unchanged_p;
10619 /* Do a frame update, taking possible shortcuts into account. This is
10620 the main external entry point for redisplay.
10622 If the last redisplay displayed an echo area message and that message
10623 is no longer requested, we clear the echo area or bring back the
10624 mini-buffer if that is in use. */
10626 void
10627 redisplay ()
10629 redisplay_internal (0);
10633 static Lisp_Object
10634 overlay_arrow_string_or_property (var)
10635 Lisp_Object var;
10637 Lisp_Object val;
10639 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10640 return val;
10642 return Voverlay_arrow_string;
10645 /* Return 1 if there are any overlay-arrows in current_buffer. */
10646 static int
10647 overlay_arrow_in_current_buffer_p ()
10649 Lisp_Object vlist;
10651 for (vlist = Voverlay_arrow_variable_list;
10652 CONSP (vlist);
10653 vlist = XCDR (vlist))
10655 Lisp_Object var = XCAR (vlist);
10656 Lisp_Object val;
10658 if (!SYMBOLP (var))
10659 continue;
10660 val = find_symbol_value (var);
10661 if (MARKERP (val)
10662 && current_buffer == XMARKER (val)->buffer)
10663 return 1;
10665 return 0;
10669 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10670 has changed. */
10672 static int
10673 overlay_arrows_changed_p ()
10675 Lisp_Object vlist;
10677 for (vlist = Voverlay_arrow_variable_list;
10678 CONSP (vlist);
10679 vlist = XCDR (vlist))
10681 Lisp_Object var = XCAR (vlist);
10682 Lisp_Object val, pstr;
10684 if (!SYMBOLP (var))
10685 continue;
10686 val = find_symbol_value (var);
10687 if (!MARKERP (val))
10688 continue;
10689 if (! EQ (COERCE_MARKER (val),
10690 Fget (var, Qlast_arrow_position))
10691 || ! (pstr = overlay_arrow_string_or_property (var),
10692 EQ (pstr, Fget (var, Qlast_arrow_string))))
10693 return 1;
10695 return 0;
10698 /* Mark overlay arrows to be updated on next redisplay. */
10700 static void
10701 update_overlay_arrows (up_to_date)
10702 int up_to_date;
10704 Lisp_Object vlist;
10706 for (vlist = Voverlay_arrow_variable_list;
10707 CONSP (vlist);
10708 vlist = XCDR (vlist))
10710 Lisp_Object var = XCAR (vlist);
10712 if (!SYMBOLP (var))
10713 continue;
10715 if (up_to_date > 0)
10717 Lisp_Object val = find_symbol_value (var);
10718 Fput (var, Qlast_arrow_position,
10719 COERCE_MARKER (val));
10720 Fput (var, Qlast_arrow_string,
10721 overlay_arrow_string_or_property (var));
10723 else if (up_to_date < 0
10724 || !NILP (Fget (var, Qlast_arrow_position)))
10726 Fput (var, Qlast_arrow_position, Qt);
10727 Fput (var, Qlast_arrow_string, Qt);
10733 /* Return overlay arrow string to display at row.
10734 Return integer (bitmap number) for arrow bitmap in left fringe.
10735 Return nil if no overlay arrow. */
10737 static Lisp_Object
10738 overlay_arrow_at_row (it, row)
10739 struct it *it;
10740 struct glyph_row *row;
10742 Lisp_Object vlist;
10744 for (vlist = Voverlay_arrow_variable_list;
10745 CONSP (vlist);
10746 vlist = XCDR (vlist))
10748 Lisp_Object var = XCAR (vlist);
10749 Lisp_Object val;
10751 if (!SYMBOLP (var))
10752 continue;
10754 val = find_symbol_value (var);
10756 if (MARKERP (val)
10757 && current_buffer == XMARKER (val)->buffer
10758 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10760 if (FRAME_WINDOW_P (it->f)
10761 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10763 #ifdef HAVE_WINDOW_SYSTEM
10764 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10766 int fringe_bitmap;
10767 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10768 return make_number (fringe_bitmap);
10770 #endif
10771 return make_number (-1); /* Use default arrow bitmap */
10773 return overlay_arrow_string_or_property (var);
10777 return Qnil;
10780 /* Return 1 if point moved out of or into a composition. Otherwise
10781 return 0. PREV_BUF and PREV_PT are the last point buffer and
10782 position. BUF and PT are the current point buffer and position. */
10785 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10786 struct buffer *prev_buf, *buf;
10787 int prev_pt, pt;
10789 int start, end;
10790 Lisp_Object prop;
10791 Lisp_Object buffer;
10793 XSETBUFFER (buffer, buf);
10794 /* Check a composition at the last point if point moved within the
10795 same buffer. */
10796 if (prev_buf == buf)
10798 if (prev_pt == pt)
10799 /* Point didn't move. */
10800 return 0;
10802 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10803 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10804 && COMPOSITION_VALID_P (start, end, prop)
10805 && start < prev_pt && end > prev_pt)
10806 /* The last point was within the composition. Return 1 iff
10807 point moved out of the composition. */
10808 return (pt <= start || pt >= end);
10811 /* Check a composition at the current point. */
10812 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10813 && find_composition (pt, -1, &start, &end, &prop, buffer)
10814 && COMPOSITION_VALID_P (start, end, prop)
10815 && start < pt && end > pt);
10819 /* Reconsider the setting of B->clip_changed which is displayed
10820 in window W. */
10822 static INLINE void
10823 reconsider_clip_changes (w, b)
10824 struct window *w;
10825 struct buffer *b;
10827 if (b->clip_changed
10828 && !NILP (w->window_end_valid)
10829 && w->current_matrix->buffer == b
10830 && w->current_matrix->zv == BUF_ZV (b)
10831 && w->current_matrix->begv == BUF_BEGV (b))
10832 b->clip_changed = 0;
10834 /* If display wasn't paused, and W is not a tool bar window, see if
10835 point has been moved into or out of a composition. In that case,
10836 we set b->clip_changed to 1 to force updating the screen. If
10837 b->clip_changed has already been set to 1, we can skip this
10838 check. */
10839 if (!b->clip_changed
10840 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10842 int pt;
10844 if (w == XWINDOW (selected_window))
10845 pt = BUF_PT (current_buffer);
10846 else
10847 pt = marker_position (w->pointm);
10849 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10850 || pt != XINT (w->last_point))
10851 && check_point_in_composition (w->current_matrix->buffer,
10852 XINT (w->last_point),
10853 XBUFFER (w->buffer), pt))
10854 b->clip_changed = 1;
10859 /* Select FRAME to forward the values of frame-local variables into C
10860 variables so that the redisplay routines can access those values
10861 directly. */
10863 static void
10864 select_frame_for_redisplay (frame)
10865 Lisp_Object frame;
10867 Lisp_Object tail, sym, val;
10868 Lisp_Object old = selected_frame;
10870 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10872 selected_frame = frame;
10874 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10875 if (CONSP (XCAR (tail))
10876 && (sym = XCAR (XCAR (tail)),
10877 SYMBOLP (sym))
10878 && (sym = indirect_variable (sym),
10879 val = SYMBOL_VALUE (sym),
10880 (BUFFER_LOCAL_VALUEP (val)))
10881 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10882 /* Use find_symbol_value rather than Fsymbol_value
10883 to avoid an error if it is void. */
10884 find_symbol_value (sym);
10886 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10887 if (CONSP (XCAR (tail))
10888 && (sym = XCAR (XCAR (tail)),
10889 SYMBOLP (sym))
10890 && (sym = indirect_variable (sym),
10891 val = SYMBOL_VALUE (sym),
10892 (BUFFER_LOCAL_VALUEP (val)))
10893 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10894 find_symbol_value (sym);
10898 #define STOP_POLLING \
10899 do { if (! polling_stopped_here) stop_polling (); \
10900 polling_stopped_here = 1; } while (0)
10902 #define RESUME_POLLING \
10903 do { if (polling_stopped_here) start_polling (); \
10904 polling_stopped_here = 0; } while (0)
10907 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10908 response to any user action; therefore, we should preserve the echo
10909 area. (Actually, our caller does that job.) Perhaps in the future
10910 avoid recentering windows if it is not necessary; currently that
10911 causes some problems. */
10913 static void
10914 redisplay_internal (preserve_echo_area)
10915 int preserve_echo_area;
10917 struct window *w = XWINDOW (selected_window);
10918 struct frame *f;
10919 int pause;
10920 int must_finish = 0;
10921 struct text_pos tlbufpos, tlendpos;
10922 int number_of_visible_frames;
10923 int count, count1;
10924 struct frame *sf;
10925 int polling_stopped_here = 0;
10926 Lisp_Object old_frame = selected_frame;
10928 /* Non-zero means redisplay has to consider all windows on all
10929 frames. Zero means, only selected_window is considered. */
10930 int consider_all_windows_p;
10932 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10934 /* No redisplay if running in batch mode or frame is not yet fully
10935 initialized, or redisplay is explicitly turned off by setting
10936 Vinhibit_redisplay. */
10937 if (noninteractive
10938 || !NILP (Vinhibit_redisplay))
10939 return;
10941 /* Don't examine these until after testing Vinhibit_redisplay.
10942 When Emacs is shutting down, perhaps because its connection to
10943 X has dropped, we should not look at them at all. */
10944 f = XFRAME (w->frame);
10945 sf = SELECTED_FRAME ();
10947 if (!f->glyphs_initialized_p)
10948 return;
10950 /* The flag redisplay_performed_directly_p is set by
10951 direct_output_for_insert when it already did the whole screen
10952 update necessary. */
10953 if (redisplay_performed_directly_p)
10955 redisplay_performed_directly_p = 0;
10956 if (!hscroll_windows (selected_window))
10957 return;
10960 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10961 if (popup_activated ())
10962 return;
10963 #endif
10965 /* I don't think this happens but let's be paranoid. */
10966 if (redisplaying_p)
10967 return;
10969 /* Record a function that resets redisplaying_p to its old value
10970 when we leave this function. */
10971 count = SPECPDL_INDEX ();
10972 record_unwind_protect (unwind_redisplay,
10973 Fcons (make_number (redisplaying_p), selected_frame));
10974 ++redisplaying_p;
10975 specbind (Qinhibit_free_realized_faces, Qnil);
10978 Lisp_Object tail, frame;
10980 FOR_EACH_FRAME (tail, frame)
10982 struct frame *f = XFRAME (frame);
10983 f->already_hscrolled_p = 0;
10987 retry:
10988 if (!EQ (old_frame, selected_frame)
10989 && FRAME_LIVE_P (XFRAME (old_frame)))
10990 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
10991 selected_frame and selected_window to be temporarily out-of-sync so
10992 when we come back here via `goto retry', we need to resync because we
10993 may need to run Elisp code (via prepare_menu_bars). */
10994 select_frame_for_redisplay (old_frame);
10996 pause = 0;
10997 reconsider_clip_changes (w, current_buffer);
10998 last_escape_glyph_frame = NULL;
10999 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11001 /* If new fonts have been loaded that make a glyph matrix adjustment
11002 necessary, do it. */
11003 if (fonts_changed_p)
11005 adjust_glyphs (NULL);
11006 ++windows_or_buffers_changed;
11007 fonts_changed_p = 0;
11010 /* If face_change_count is non-zero, init_iterator will free all
11011 realized faces, which includes the faces referenced from current
11012 matrices. So, we can't reuse current matrices in this case. */
11013 if (face_change_count)
11014 ++windows_or_buffers_changed;
11016 if (FRAME_TERMCAP_P (sf)
11017 && FRAME_TTY (sf)->previous_frame != sf)
11019 /* Since frames on a single ASCII terminal share the same
11020 display area, displaying a different frame means redisplay
11021 the whole thing. */
11022 windows_or_buffers_changed++;
11023 SET_FRAME_GARBAGED (sf);
11024 FRAME_TTY (sf)->previous_frame = sf;
11027 /* Set the visible flags for all frames. Do this before checking
11028 for resized or garbaged frames; they want to know if their frames
11029 are visible. See the comment in frame.h for
11030 FRAME_SAMPLE_VISIBILITY. */
11032 Lisp_Object tail, frame;
11034 number_of_visible_frames = 0;
11036 FOR_EACH_FRAME (tail, frame)
11038 struct frame *f = XFRAME (frame);
11040 FRAME_SAMPLE_VISIBILITY (f);
11041 if (FRAME_VISIBLE_P (f))
11042 ++number_of_visible_frames;
11043 clear_desired_matrices (f);
11048 /* Notice any pending interrupt request to change frame size. */
11049 do_pending_window_change (1);
11051 /* Clear frames marked as garbaged. */
11052 if (frame_garbaged)
11053 clear_garbaged_frames ();
11055 /* Build menubar and tool-bar items. */
11056 if (NILP (Vmemory_full))
11057 prepare_menu_bars ();
11059 if (windows_or_buffers_changed)
11060 update_mode_lines++;
11062 /* Detect case that we need to write or remove a star in the mode line. */
11063 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11065 w->update_mode_line = Qt;
11066 if (buffer_shared > 1)
11067 update_mode_lines++;
11070 /* Avoid invocation of point motion hooks by `current_column' below. */
11071 count1 = SPECPDL_INDEX ();
11072 specbind (Qinhibit_point_motion_hooks, Qt);
11074 /* If %c is in the mode line, update it if needed. */
11075 if (!NILP (w->column_number_displayed)
11076 /* This alternative quickly identifies a common case
11077 where no change is needed. */
11078 && !(PT == XFASTINT (w->last_point)
11079 && XFASTINT (w->last_modified) >= MODIFF
11080 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11081 && (XFASTINT (w->column_number_displayed)
11082 != (int) current_column ())) /* iftc */
11083 w->update_mode_line = Qt;
11085 unbind_to (count1, Qnil);
11087 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11089 /* The variable buffer_shared is set in redisplay_window and
11090 indicates that we redisplay a buffer in different windows. See
11091 there. */
11092 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11093 || cursor_type_changed);
11095 /* If specs for an arrow have changed, do thorough redisplay
11096 to ensure we remove any arrow that should no longer exist. */
11097 if (overlay_arrows_changed_p ())
11098 consider_all_windows_p = windows_or_buffers_changed = 1;
11100 /* Normally the message* functions will have already displayed and
11101 updated the echo area, but the frame may have been trashed, or
11102 the update may have been preempted, so display the echo area
11103 again here. Checking message_cleared_p captures the case that
11104 the echo area should be cleared. */
11105 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11106 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11107 || (message_cleared_p
11108 && minibuf_level == 0
11109 /* If the mini-window is currently selected, this means the
11110 echo-area doesn't show through. */
11111 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11113 int window_height_changed_p = echo_area_display (0);
11114 must_finish = 1;
11116 /* If we don't display the current message, don't clear the
11117 message_cleared_p flag, because, if we did, we wouldn't clear
11118 the echo area in the next redisplay which doesn't preserve
11119 the echo area. */
11120 if (!display_last_displayed_message_p)
11121 message_cleared_p = 0;
11123 if (fonts_changed_p)
11124 goto retry;
11125 else if (window_height_changed_p)
11127 consider_all_windows_p = 1;
11128 ++update_mode_lines;
11129 ++windows_or_buffers_changed;
11131 /* If window configuration was changed, frames may have been
11132 marked garbaged. Clear them or we will experience
11133 surprises wrt scrolling. */
11134 if (frame_garbaged)
11135 clear_garbaged_frames ();
11138 else if (EQ (selected_window, minibuf_window)
11139 && (current_buffer->clip_changed
11140 || XFASTINT (w->last_modified) < MODIFF
11141 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11142 && resize_mini_window (w, 0))
11144 /* Resized active mini-window to fit the size of what it is
11145 showing if its contents might have changed. */
11146 must_finish = 1;
11147 consider_all_windows_p = 1;
11148 ++windows_or_buffers_changed;
11149 ++update_mode_lines;
11151 /* If window configuration was changed, frames may have been
11152 marked garbaged. Clear them or we will experience
11153 surprises wrt scrolling. */
11154 if (frame_garbaged)
11155 clear_garbaged_frames ();
11159 /* If showing the region, and mark has changed, we must redisplay
11160 the whole window. The assignment to this_line_start_pos prevents
11161 the optimization directly below this if-statement. */
11162 if (((!NILP (Vtransient_mark_mode)
11163 && !NILP (XBUFFER (w->buffer)->mark_active))
11164 != !NILP (w->region_showing))
11165 || (!NILP (w->region_showing)
11166 && !EQ (w->region_showing,
11167 Fmarker_position (XBUFFER (w->buffer)->mark))))
11168 CHARPOS (this_line_start_pos) = 0;
11170 /* Optimize the case that only the line containing the cursor in the
11171 selected window has changed. Variables starting with this_ are
11172 set in display_line and record information about the line
11173 containing the cursor. */
11174 tlbufpos = this_line_start_pos;
11175 tlendpos = this_line_end_pos;
11176 if (!consider_all_windows_p
11177 && CHARPOS (tlbufpos) > 0
11178 && NILP (w->update_mode_line)
11179 && !current_buffer->clip_changed
11180 && !current_buffer->prevent_redisplay_optimizations_p
11181 && FRAME_VISIBLE_P (XFRAME (w->frame))
11182 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11183 /* Make sure recorded data applies to current buffer, etc. */
11184 && this_line_buffer == current_buffer
11185 && current_buffer == XBUFFER (w->buffer)
11186 && NILP (w->force_start)
11187 && NILP (w->optional_new_start)
11188 /* Point must be on the line that we have info recorded about. */
11189 && PT >= CHARPOS (tlbufpos)
11190 && PT <= Z - CHARPOS (tlendpos)
11191 /* All text outside that line, including its final newline,
11192 must be unchanged */
11193 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11194 CHARPOS (tlendpos)))
11196 if (CHARPOS (tlbufpos) > BEGV
11197 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11198 && (CHARPOS (tlbufpos) == ZV
11199 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11200 /* Former continuation line has disappeared by becoming empty */
11201 goto cancel;
11202 else if (XFASTINT (w->last_modified) < MODIFF
11203 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11204 || MINI_WINDOW_P (w))
11206 /* We have to handle the case of continuation around a
11207 wide-column character (See the comment in indent.c around
11208 line 885).
11210 For instance, in the following case:
11212 -------- Insert --------
11213 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11214 J_I_ ==> J_I_ `^^' are cursors.
11215 ^^ ^^
11216 -------- --------
11218 As we have to redraw the line above, we should goto cancel. */
11220 struct it it;
11221 int line_height_before = this_line_pixel_height;
11223 /* Note that start_display will handle the case that the
11224 line starting at tlbufpos is a continuation lines. */
11225 start_display (&it, w, tlbufpos);
11227 /* Implementation note: It this still necessary? */
11228 if (it.current_x != this_line_start_x)
11229 goto cancel;
11231 TRACE ((stderr, "trying display optimization 1\n"));
11232 w->cursor.vpos = -1;
11233 overlay_arrow_seen = 0;
11234 it.vpos = this_line_vpos;
11235 it.current_y = this_line_y;
11236 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11237 display_line (&it);
11239 /* If line contains point, is not continued,
11240 and ends at same distance from eob as before, we win */
11241 if (w->cursor.vpos >= 0
11242 /* Line is not continued, otherwise this_line_start_pos
11243 would have been set to 0 in display_line. */
11244 && CHARPOS (this_line_start_pos)
11245 /* Line ends as before. */
11246 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11247 /* Line has same height as before. Otherwise other lines
11248 would have to be shifted up or down. */
11249 && this_line_pixel_height == line_height_before)
11251 /* If this is not the window's last line, we must adjust
11252 the charstarts of the lines below. */
11253 if (it.current_y < it.last_visible_y)
11255 struct glyph_row *row
11256 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11257 int delta, delta_bytes;
11259 if (Z - CHARPOS (tlendpos) == ZV)
11261 /* This line ends at end of (accessible part of)
11262 buffer. There is no newline to count. */
11263 delta = (Z
11264 - CHARPOS (tlendpos)
11265 - MATRIX_ROW_START_CHARPOS (row));
11266 delta_bytes = (Z_BYTE
11267 - BYTEPOS (tlendpos)
11268 - MATRIX_ROW_START_BYTEPOS (row));
11270 else
11272 /* This line ends in a newline. Must take
11273 account of the newline and the rest of the
11274 text that follows. */
11275 delta = (Z
11276 - CHARPOS (tlendpos)
11277 - MATRIX_ROW_START_CHARPOS (row));
11278 delta_bytes = (Z_BYTE
11279 - BYTEPOS (tlendpos)
11280 - MATRIX_ROW_START_BYTEPOS (row));
11283 increment_matrix_positions (w->current_matrix,
11284 this_line_vpos + 1,
11285 w->current_matrix->nrows,
11286 delta, delta_bytes);
11289 /* If this row displays text now but previously didn't,
11290 or vice versa, w->window_end_vpos may have to be
11291 adjusted. */
11292 if ((it.glyph_row - 1)->displays_text_p)
11294 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11295 XSETINT (w->window_end_vpos, this_line_vpos);
11297 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11298 && this_line_vpos > 0)
11299 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11300 w->window_end_valid = Qnil;
11302 /* Update hint: No need to try to scroll in update_window. */
11303 w->desired_matrix->no_scrolling_p = 1;
11305 #if GLYPH_DEBUG
11306 *w->desired_matrix->method = 0;
11307 debug_method_add (w, "optimization 1");
11308 #endif
11309 #ifdef HAVE_WINDOW_SYSTEM
11310 update_window_fringes (w, 0);
11311 #endif
11312 goto update;
11314 else
11315 goto cancel;
11317 else if (/* Cursor position hasn't changed. */
11318 PT == XFASTINT (w->last_point)
11319 /* Make sure the cursor was last displayed
11320 in this window. Otherwise we have to reposition it. */
11321 && 0 <= w->cursor.vpos
11322 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11324 if (!must_finish)
11326 do_pending_window_change (1);
11328 /* We used to always goto end_of_redisplay here, but this
11329 isn't enough if we have a blinking cursor. */
11330 if (w->cursor_off_p == w->last_cursor_off_p)
11331 goto end_of_redisplay;
11333 goto update;
11335 /* If highlighting the region, or if the cursor is in the echo area,
11336 then we can't just move the cursor. */
11337 else if (! (!NILP (Vtransient_mark_mode)
11338 && !NILP (current_buffer->mark_active))
11339 && (EQ (selected_window, current_buffer->last_selected_window)
11340 || highlight_nonselected_windows)
11341 && NILP (w->region_showing)
11342 && NILP (Vshow_trailing_whitespace)
11343 && !cursor_in_echo_area)
11345 struct it it;
11346 struct glyph_row *row;
11348 /* Skip from tlbufpos to PT and see where it is. Note that
11349 PT may be in invisible text. If so, we will end at the
11350 next visible position. */
11351 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11352 NULL, DEFAULT_FACE_ID);
11353 it.current_x = this_line_start_x;
11354 it.current_y = this_line_y;
11355 it.vpos = this_line_vpos;
11357 /* The call to move_it_to stops in front of PT, but
11358 moves over before-strings. */
11359 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11361 if (it.vpos == this_line_vpos
11362 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11363 row->enabled_p))
11365 xassert (this_line_vpos == it.vpos);
11366 xassert (this_line_y == it.current_y);
11367 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11368 #if GLYPH_DEBUG
11369 *w->desired_matrix->method = 0;
11370 debug_method_add (w, "optimization 3");
11371 #endif
11372 goto update;
11374 else
11375 goto cancel;
11378 cancel:
11379 /* Text changed drastically or point moved off of line. */
11380 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11383 CHARPOS (this_line_start_pos) = 0;
11384 consider_all_windows_p |= buffer_shared > 1;
11385 ++clear_face_cache_count;
11386 #ifdef HAVE_WINDOW_SYSTEM
11387 ++clear_image_cache_count;
11388 #endif
11390 /* Build desired matrices, and update the display. If
11391 consider_all_windows_p is non-zero, do it for all windows on all
11392 frames. Otherwise do it for selected_window, only. */
11394 if (consider_all_windows_p)
11396 Lisp_Object tail, frame;
11398 FOR_EACH_FRAME (tail, frame)
11399 XFRAME (frame)->updated_p = 0;
11401 /* Recompute # windows showing selected buffer. This will be
11402 incremented each time such a window is displayed. */
11403 buffer_shared = 0;
11405 FOR_EACH_FRAME (tail, frame)
11407 struct frame *f = XFRAME (frame);
11409 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11411 if (! EQ (frame, selected_frame))
11412 /* Select the frame, for the sake of frame-local
11413 variables. */
11414 select_frame_for_redisplay (frame);
11416 /* Mark all the scroll bars to be removed; we'll redeem
11417 the ones we want when we redisplay their windows. */
11418 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11419 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11421 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11422 redisplay_windows (FRAME_ROOT_WINDOW (f));
11424 /* Any scroll bars which redisplay_windows should have
11425 nuked should now go away. */
11426 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11427 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11429 /* If fonts changed, display again. */
11430 /* ??? rms: I suspect it is a mistake to jump all the way
11431 back to retry here. It should just retry this frame. */
11432 if (fonts_changed_p)
11433 goto retry;
11435 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11437 /* See if we have to hscroll. */
11438 if (!f->already_hscrolled_p)
11440 f->already_hscrolled_p = 1;
11441 if (hscroll_windows (f->root_window))
11442 goto retry;
11445 /* Prevent various kinds of signals during display
11446 update. stdio is not robust about handling
11447 signals, which can cause an apparent I/O
11448 error. */
11449 if (interrupt_input)
11450 unrequest_sigio ();
11451 STOP_POLLING;
11453 /* Update the display. */
11454 set_window_update_flags (XWINDOW (f->root_window), 1);
11455 pause |= update_frame (f, 0, 0);
11456 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11457 if (pause)
11458 break;
11459 #endif
11461 f->updated_p = 1;
11466 if (!pause)
11468 /* Do the mark_window_display_accurate after all windows have
11469 been redisplayed because this call resets flags in buffers
11470 which are needed for proper redisplay. */
11471 FOR_EACH_FRAME (tail, frame)
11473 struct frame *f = XFRAME (frame);
11474 if (f->updated_p)
11476 mark_window_display_accurate (f->root_window, 1);
11477 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11478 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11483 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11485 Lisp_Object mini_window;
11486 struct frame *mini_frame;
11488 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11489 /* Use list_of_error, not Qerror, so that
11490 we catch only errors and don't run the debugger. */
11491 internal_condition_case_1 (redisplay_window_1, selected_window,
11492 list_of_error,
11493 redisplay_window_error);
11495 /* Compare desired and current matrices, perform output. */
11497 update:
11498 /* If fonts changed, display again. */
11499 if (fonts_changed_p)
11500 goto retry;
11502 /* Prevent various kinds of signals during display update.
11503 stdio is not robust about handling signals,
11504 which can cause an apparent I/O error. */
11505 if (interrupt_input)
11506 unrequest_sigio ();
11507 STOP_POLLING;
11509 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11511 if (hscroll_windows (selected_window))
11512 goto retry;
11514 XWINDOW (selected_window)->must_be_updated_p = 1;
11515 pause = update_frame (sf, 0, 0);
11518 /* We may have called echo_area_display at the top of this
11519 function. If the echo area is on another frame, that may
11520 have put text on a frame other than the selected one, so the
11521 above call to update_frame would not have caught it. Catch
11522 it here. */
11523 mini_window = FRAME_MINIBUF_WINDOW (sf);
11524 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11526 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11528 XWINDOW (mini_window)->must_be_updated_p = 1;
11529 pause |= update_frame (mini_frame, 0, 0);
11530 if (!pause && hscroll_windows (mini_window))
11531 goto retry;
11535 /* If display was paused because of pending input, make sure we do a
11536 thorough update the next time. */
11537 if (pause)
11539 /* Prevent the optimization at the beginning of
11540 redisplay_internal that tries a single-line update of the
11541 line containing the cursor in the selected window. */
11542 CHARPOS (this_line_start_pos) = 0;
11544 /* Let the overlay arrow be updated the next time. */
11545 update_overlay_arrows (0);
11547 /* If we pause after scrolling, some rows in the current
11548 matrices of some windows are not valid. */
11549 if (!WINDOW_FULL_WIDTH_P (w)
11550 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11551 update_mode_lines = 1;
11553 else
11555 if (!consider_all_windows_p)
11557 /* This has already been done above if
11558 consider_all_windows_p is set. */
11559 mark_window_display_accurate_1 (w, 1);
11561 /* Say overlay arrows are up to date. */
11562 update_overlay_arrows (1);
11564 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11565 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11568 update_mode_lines = 0;
11569 windows_or_buffers_changed = 0;
11570 cursor_type_changed = 0;
11573 /* Start SIGIO interrupts coming again. Having them off during the
11574 code above makes it less likely one will discard output, but not
11575 impossible, since there might be stuff in the system buffer here.
11576 But it is much hairier to try to do anything about that. */
11577 if (interrupt_input)
11578 request_sigio ();
11579 RESUME_POLLING;
11581 /* If a frame has become visible which was not before, redisplay
11582 again, so that we display it. Expose events for such a frame
11583 (which it gets when becoming visible) don't call the parts of
11584 redisplay constructing glyphs, so simply exposing a frame won't
11585 display anything in this case. So, we have to display these
11586 frames here explicitly. */
11587 if (!pause)
11589 Lisp_Object tail, frame;
11590 int new_count = 0;
11592 FOR_EACH_FRAME (tail, frame)
11594 int this_is_visible = 0;
11596 if (XFRAME (frame)->visible)
11597 this_is_visible = 1;
11598 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11599 if (XFRAME (frame)->visible)
11600 this_is_visible = 1;
11602 if (this_is_visible)
11603 new_count++;
11606 if (new_count != number_of_visible_frames)
11607 windows_or_buffers_changed++;
11610 /* Change frame size now if a change is pending. */
11611 do_pending_window_change (1);
11613 /* If we just did a pending size change, or have additional
11614 visible frames, redisplay again. */
11615 if (windows_or_buffers_changed && !pause)
11616 goto retry;
11618 /* Clear the face cache eventually. */
11619 if (consider_all_windows_p)
11621 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11623 clear_face_cache (0);
11624 clear_face_cache_count = 0;
11626 #ifdef HAVE_WINDOW_SYSTEM
11627 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11629 Lisp_Object tail, frame;
11630 FOR_EACH_FRAME (tail, frame)
11632 struct frame *f = XFRAME (frame);
11633 if (FRAME_WINDOW_P (f))
11634 clear_image_cache (f, 0);
11636 clear_image_cache_count = 0;
11638 #endif /* HAVE_WINDOW_SYSTEM */
11641 end_of_redisplay:
11642 unbind_to (count, Qnil);
11643 RESUME_POLLING;
11647 /* Redisplay, but leave alone any recent echo area message unless
11648 another message has been requested in its place.
11650 This is useful in situations where you need to redisplay but no
11651 user action has occurred, making it inappropriate for the message
11652 area to be cleared. See tracking_off and
11653 wait_reading_process_output for examples of these situations.
11655 FROM_WHERE is an integer saying from where this function was
11656 called. This is useful for debugging. */
11658 void
11659 redisplay_preserve_echo_area (from_where)
11660 int from_where;
11662 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11664 if (!NILP (echo_area_buffer[1]))
11666 /* We have a previously displayed message, but no current
11667 message. Redisplay the previous message. */
11668 display_last_displayed_message_p = 1;
11669 redisplay_internal (1);
11670 display_last_displayed_message_p = 0;
11672 else
11673 redisplay_internal (1);
11675 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11676 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11677 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11681 /* Function registered with record_unwind_protect in
11682 redisplay_internal. Reset redisplaying_p to the value it had
11683 before redisplay_internal was called, and clear
11684 prevent_freeing_realized_faces_p. It also selects the previously
11685 selected frame, unless it has been deleted (by an X connection
11686 failure during redisplay, for example). */
11688 static Lisp_Object
11689 unwind_redisplay (val)
11690 Lisp_Object val;
11692 Lisp_Object old_redisplaying_p, old_frame;
11694 old_redisplaying_p = XCAR (val);
11695 redisplaying_p = XFASTINT (old_redisplaying_p);
11696 old_frame = XCDR (val);
11697 if (! EQ (old_frame, selected_frame)
11698 && FRAME_LIVE_P (XFRAME (old_frame)))
11699 select_frame_for_redisplay (old_frame);
11700 return Qnil;
11704 /* Mark the display of window W as accurate or inaccurate. If
11705 ACCURATE_P is non-zero mark display of W as accurate. If
11706 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11707 redisplay_internal is called. */
11709 static void
11710 mark_window_display_accurate_1 (w, accurate_p)
11711 struct window *w;
11712 int accurate_p;
11714 if (BUFFERP (w->buffer))
11716 struct buffer *b = XBUFFER (w->buffer);
11718 w->last_modified
11719 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11720 w->last_overlay_modified
11721 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11722 w->last_had_star
11723 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11725 if (accurate_p)
11727 b->clip_changed = 0;
11728 b->prevent_redisplay_optimizations_p = 0;
11730 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11731 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11732 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11733 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11735 w->current_matrix->buffer = b;
11736 w->current_matrix->begv = BUF_BEGV (b);
11737 w->current_matrix->zv = BUF_ZV (b);
11739 w->last_cursor = w->cursor;
11740 w->last_cursor_off_p = w->cursor_off_p;
11742 if (w == XWINDOW (selected_window))
11743 w->last_point = make_number (BUF_PT (b));
11744 else
11745 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11749 if (accurate_p)
11751 w->window_end_valid = w->buffer;
11752 #if 0 /* This is incorrect with variable-height lines. */
11753 xassert (XINT (w->window_end_vpos)
11754 < (WINDOW_TOTAL_LINES (w)
11755 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11756 #endif
11757 w->update_mode_line = Qnil;
11762 /* Mark the display of windows in the window tree rooted at WINDOW as
11763 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11764 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11765 be redisplayed the next time redisplay_internal is called. */
11767 void
11768 mark_window_display_accurate (window, accurate_p)
11769 Lisp_Object window;
11770 int accurate_p;
11772 struct window *w;
11774 for (; !NILP (window); window = w->next)
11776 w = XWINDOW (window);
11777 mark_window_display_accurate_1 (w, accurate_p);
11779 if (!NILP (w->vchild))
11780 mark_window_display_accurate (w->vchild, accurate_p);
11781 if (!NILP (w->hchild))
11782 mark_window_display_accurate (w->hchild, accurate_p);
11785 if (accurate_p)
11787 update_overlay_arrows (1);
11789 else
11791 /* Force a thorough redisplay the next time by setting
11792 last_arrow_position and last_arrow_string to t, which is
11793 unequal to any useful value of Voverlay_arrow_... */
11794 update_overlay_arrows (-1);
11799 /* Return value in display table DP (Lisp_Char_Table *) for character
11800 C. Since a display table doesn't have any parent, we don't have to
11801 follow parent. Do not call this function directly but use the
11802 macro DISP_CHAR_VECTOR. */
11804 Lisp_Object
11805 disp_char_vector (dp, c)
11806 struct Lisp_Char_Table *dp;
11807 int c;
11809 int code[4], i;
11810 Lisp_Object val;
11812 if (SINGLE_BYTE_CHAR_P (c))
11813 return (dp->contents[c]);
11815 SPLIT_CHAR (c, code[0], code[1], code[2]);
11816 if (code[1] < 32)
11817 code[1] = -1;
11818 else if (code[2] < 32)
11819 code[2] = -1;
11821 /* Here, the possible range of code[0] (== charset ID) is
11822 128..max_charset. Since the top level char table contains data
11823 for multibyte characters after 256th element, we must increment
11824 code[0] by 128 to get a correct index. */
11825 code[0] += 128;
11826 code[3] = -1; /* anchor */
11828 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11830 val = dp->contents[code[i]];
11831 if (!SUB_CHAR_TABLE_P (val))
11832 return (NILP (val) ? dp->defalt : val);
11835 /* Here, val is a sub char table. We return the default value of
11836 it. */
11837 return (dp->defalt);
11842 /***********************************************************************
11843 Window Redisplay
11844 ***********************************************************************/
11846 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11848 static void
11849 redisplay_windows (window)
11850 Lisp_Object window;
11852 while (!NILP (window))
11854 struct window *w = XWINDOW (window);
11856 if (!NILP (w->hchild))
11857 redisplay_windows (w->hchild);
11858 else if (!NILP (w->vchild))
11859 redisplay_windows (w->vchild);
11860 else
11862 displayed_buffer = XBUFFER (w->buffer);
11863 /* Use list_of_error, not Qerror, so that
11864 we catch only errors and don't run the debugger. */
11865 internal_condition_case_1 (redisplay_window_0, window,
11866 list_of_error,
11867 redisplay_window_error);
11870 window = w->next;
11874 static Lisp_Object
11875 redisplay_window_error ()
11877 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11878 return Qnil;
11881 static Lisp_Object
11882 redisplay_window_0 (window)
11883 Lisp_Object window;
11885 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11886 redisplay_window (window, 0);
11887 return Qnil;
11890 static Lisp_Object
11891 redisplay_window_1 (window)
11892 Lisp_Object window;
11894 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11895 redisplay_window (window, 1);
11896 return Qnil;
11900 /* Increment GLYPH until it reaches END or CONDITION fails while
11901 adding (GLYPH)->pixel_width to X. */
11903 #define SKIP_GLYPHS(glyph, end, x, condition) \
11904 do \
11906 (x) += (glyph)->pixel_width; \
11907 ++(glyph); \
11909 while ((glyph) < (end) && (condition))
11912 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11913 DELTA is the number of bytes by which positions recorded in ROW
11914 differ from current buffer positions.
11916 Return 0 if cursor is not on this row. 1 otherwise. */
11919 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11920 struct window *w;
11921 struct glyph_row *row;
11922 struct glyph_matrix *matrix;
11923 int delta, delta_bytes, dy, dvpos;
11925 struct glyph *glyph = row->glyphs[TEXT_AREA];
11926 struct glyph *end = glyph + row->used[TEXT_AREA];
11927 struct glyph *cursor = NULL;
11928 /* The first glyph that starts a sequence of glyphs from string. */
11929 struct glyph *string_start;
11930 /* The X coordinate of string_start. */
11931 int string_start_x;
11932 /* The last known character position. */
11933 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11934 /* The last known character position before string_start. */
11935 int string_before_pos;
11936 int x = row->x;
11937 int cursor_x = x;
11938 int cursor_from_overlay_pos = 0;
11939 int pt_old = PT - delta;
11941 /* Skip over glyphs not having an object at the start of the row.
11942 These are special glyphs like truncation marks on terminal
11943 frames. */
11944 if (row->displays_text_p)
11945 while (glyph < end
11946 && INTEGERP (glyph->object)
11947 && glyph->charpos < 0)
11949 x += glyph->pixel_width;
11950 ++glyph;
11953 string_start = NULL;
11954 while (glyph < end
11955 && !INTEGERP (glyph->object)
11956 && (!BUFFERP (glyph->object)
11957 || (last_pos = glyph->charpos) < pt_old))
11959 if (! STRINGP (glyph->object))
11961 string_start = NULL;
11962 x += glyph->pixel_width;
11963 ++glyph;
11964 if (cursor_from_overlay_pos
11965 && last_pos >= cursor_from_overlay_pos)
11967 cursor_from_overlay_pos = 0;
11968 cursor = 0;
11971 else
11973 if (string_start == NULL)
11975 string_before_pos = last_pos;
11976 string_start = glyph;
11977 string_start_x = x;
11979 /* Skip all glyphs from string. */
11982 Lisp_Object cprop;
11983 int pos;
11984 if ((cursor == NULL || glyph > cursor)
11985 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11986 Qcursor, (glyph)->object),
11987 !NILP (cprop))
11988 && (pos = string_buffer_position (w, glyph->object,
11989 string_before_pos),
11990 (pos == 0 /* From overlay */
11991 || pos == pt_old)))
11993 /* Estimate overlay buffer position from the buffer
11994 positions of the glyphs before and after the overlay.
11995 Add 1 to last_pos so that if point corresponds to the
11996 glyph right after the overlay, we still use a 'cursor'
11997 property found in that overlay. */
11998 cursor_from_overlay_pos = (pos ? 0 : last_pos
11999 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12000 cursor = glyph;
12001 cursor_x = x;
12003 x += glyph->pixel_width;
12004 ++glyph;
12006 while (glyph < end && EQ (glyph->object, string_start->object));
12010 if (cursor != NULL)
12012 glyph = cursor;
12013 x = cursor_x;
12015 else if (row->ends_in_ellipsis_p && glyph == end)
12017 /* Scan back over the ellipsis glyphs, decrementing positions. */
12018 while (glyph > row->glyphs[TEXT_AREA]
12019 && (glyph - 1)->charpos == last_pos)
12020 glyph--, x -= glyph->pixel_width;
12021 /* That loop always goes one position too far,
12022 including the glyph before the ellipsis.
12023 So scan forward over that one. */
12024 x += glyph->pixel_width;
12025 glyph++;
12027 else if (string_start
12028 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12030 /* We may have skipped over point because the previous glyphs
12031 are from string. As there's no easy way to know the
12032 character position of the current glyph, find the correct
12033 glyph on point by scanning from string_start again. */
12034 Lisp_Object limit;
12035 Lisp_Object string;
12036 struct glyph *stop = glyph;
12037 int pos;
12039 limit = make_number (pt_old + 1);
12040 glyph = string_start;
12041 x = string_start_x;
12042 string = glyph->object;
12043 pos = string_buffer_position (w, string, string_before_pos);
12044 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12045 because we always put cursor after overlay strings. */
12046 while (pos == 0 && glyph < stop)
12048 string = glyph->object;
12049 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12050 if (glyph < stop)
12051 pos = string_buffer_position (w, glyph->object, string_before_pos);
12054 while (glyph < stop)
12056 pos = XINT (Fnext_single_char_property_change
12057 (make_number (pos), Qdisplay, Qnil, limit));
12058 if (pos > pt_old)
12059 break;
12060 /* Skip glyphs from the same string. */
12061 string = glyph->object;
12062 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12063 /* Skip glyphs from an overlay. */
12064 while (glyph < stop
12065 && ! string_buffer_position (w, glyph->object, pos))
12067 string = glyph->object;
12068 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12072 /* If we reached the end of the line, and end was from a string,
12073 cursor is not on this line. */
12074 if (glyph == end && row->continued_p)
12075 return 0;
12078 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12079 w->cursor.x = x;
12080 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12081 w->cursor.y = row->y + dy;
12083 if (w == XWINDOW (selected_window))
12085 if (!row->continued_p
12086 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12087 && row->x == 0)
12089 this_line_buffer = XBUFFER (w->buffer);
12091 CHARPOS (this_line_start_pos)
12092 = MATRIX_ROW_START_CHARPOS (row) + delta;
12093 BYTEPOS (this_line_start_pos)
12094 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12096 CHARPOS (this_line_end_pos)
12097 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12098 BYTEPOS (this_line_end_pos)
12099 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12101 this_line_y = w->cursor.y;
12102 this_line_pixel_height = row->height;
12103 this_line_vpos = w->cursor.vpos;
12104 this_line_start_x = row->x;
12106 else
12107 CHARPOS (this_line_start_pos) = 0;
12110 return 1;
12114 /* Run window scroll functions, if any, for WINDOW with new window
12115 start STARTP. Sets the window start of WINDOW to that position.
12117 We assume that the window's buffer is really current. */
12119 static INLINE struct text_pos
12120 run_window_scroll_functions (window, startp)
12121 Lisp_Object window;
12122 struct text_pos startp;
12124 struct window *w = XWINDOW (window);
12125 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12127 if (current_buffer != XBUFFER (w->buffer))
12128 abort ();
12130 if (!NILP (Vwindow_scroll_functions))
12132 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12133 make_number (CHARPOS (startp)));
12134 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12135 /* In case the hook functions switch buffers. */
12136 if (current_buffer != XBUFFER (w->buffer))
12137 set_buffer_internal_1 (XBUFFER (w->buffer));
12140 return startp;
12144 /* Make sure the line containing the cursor is fully visible.
12145 A value of 1 means there is nothing to be done.
12146 (Either the line is fully visible, or it cannot be made so,
12147 or we cannot tell.)
12149 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12150 is higher than window.
12152 A value of 0 means the caller should do scrolling
12153 as if point had gone off the screen. */
12155 static int
12156 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12157 struct window *w;
12158 int force_p;
12159 int current_matrix_p;
12161 struct glyph_matrix *matrix;
12162 struct glyph_row *row;
12163 int window_height;
12165 if (!make_cursor_line_fully_visible_p)
12166 return 1;
12168 /* It's not always possible to find the cursor, e.g, when a window
12169 is full of overlay strings. Don't do anything in that case. */
12170 if (w->cursor.vpos < 0)
12171 return 1;
12173 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12174 row = MATRIX_ROW (matrix, w->cursor.vpos);
12176 /* If the cursor row is not partially visible, there's nothing to do. */
12177 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12178 return 1;
12180 /* If the row the cursor is in is taller than the window's height,
12181 it's not clear what to do, so do nothing. */
12182 window_height = window_box_height (w);
12183 if (row->height >= window_height)
12185 if (!force_p || MINI_WINDOW_P (w)
12186 || w->vscroll || w->cursor.vpos == 0)
12187 return 1;
12189 return 0;
12191 #if 0
12192 /* This code used to try to scroll the window just enough to make
12193 the line visible. It returned 0 to say that the caller should
12194 allocate larger glyph matrices. */
12196 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12198 int dy = row->height - row->visible_height;
12199 w->vscroll = 0;
12200 w->cursor.y += dy;
12201 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12203 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12205 int dy = - (row->height - row->visible_height);
12206 w->vscroll = dy;
12207 w->cursor.y += dy;
12208 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12211 /* When we change the cursor y-position of the selected window,
12212 change this_line_y as well so that the display optimization for
12213 the cursor line of the selected window in redisplay_internal uses
12214 the correct y-position. */
12215 if (w == XWINDOW (selected_window))
12216 this_line_y = w->cursor.y;
12218 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12219 redisplay with larger matrices. */
12220 if (matrix->nrows < required_matrix_height (w))
12222 fonts_changed_p = 1;
12223 return 0;
12226 return 1;
12227 #endif /* 0 */
12231 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12232 non-zero means only WINDOW is redisplayed in redisplay_internal.
12233 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12234 in redisplay_window to bring a partially visible line into view in
12235 the case that only the cursor has moved.
12237 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12238 last screen line's vertical height extends past the end of the screen.
12240 Value is
12242 1 if scrolling succeeded
12244 0 if scrolling didn't find point.
12246 -1 if new fonts have been loaded so that we must interrupt
12247 redisplay, adjust glyph matrices, and try again. */
12249 enum
12251 SCROLLING_SUCCESS,
12252 SCROLLING_FAILED,
12253 SCROLLING_NEED_LARGER_MATRICES
12256 static int
12257 try_scrolling (window, just_this_one_p, scroll_conservatively,
12258 scroll_step, temp_scroll_step, last_line_misfit)
12259 Lisp_Object window;
12260 int just_this_one_p;
12261 EMACS_INT scroll_conservatively, scroll_step;
12262 int temp_scroll_step;
12263 int last_line_misfit;
12265 struct window *w = XWINDOW (window);
12266 struct frame *f = XFRAME (w->frame);
12267 struct text_pos scroll_margin_pos;
12268 struct text_pos pos;
12269 struct text_pos startp;
12270 struct it it;
12271 Lisp_Object window_end;
12272 int this_scroll_margin;
12273 int dy = 0;
12274 int scroll_max;
12275 int rc;
12276 int amount_to_scroll = 0;
12277 Lisp_Object aggressive;
12278 int height;
12279 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12281 #if GLYPH_DEBUG
12282 debug_method_add (w, "try_scrolling");
12283 #endif
12285 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12287 /* Compute scroll margin height in pixels. We scroll when point is
12288 within this distance from the top or bottom of the window. */
12289 if (scroll_margin > 0)
12291 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12292 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12294 else
12295 this_scroll_margin = 0;
12297 /* Force scroll_conservatively to have a reasonable value so it doesn't
12298 cause an overflow while computing how much to scroll. */
12299 if (scroll_conservatively)
12300 scroll_conservatively = min (scroll_conservatively,
12301 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12303 /* Compute how much we should try to scroll maximally to bring point
12304 into view. */
12305 if (scroll_step || scroll_conservatively || temp_scroll_step)
12306 scroll_max = max (scroll_step,
12307 max (scroll_conservatively, temp_scroll_step));
12308 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12309 || NUMBERP (current_buffer->scroll_up_aggressively))
12310 /* We're trying to scroll because of aggressive scrolling
12311 but no scroll_step is set. Choose an arbitrary one. Maybe
12312 there should be a variable for this. */
12313 scroll_max = 10;
12314 else
12315 scroll_max = 0;
12316 scroll_max *= FRAME_LINE_HEIGHT (f);
12318 /* Decide whether we have to scroll down. Start at the window end
12319 and move this_scroll_margin up to find the position of the scroll
12320 margin. */
12321 window_end = Fwindow_end (window, Qt);
12323 too_near_end:
12325 CHARPOS (scroll_margin_pos) = XINT (window_end);
12326 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12328 if (this_scroll_margin || extra_scroll_margin_lines)
12330 start_display (&it, w, scroll_margin_pos);
12331 if (this_scroll_margin)
12332 move_it_vertically_backward (&it, this_scroll_margin);
12333 if (extra_scroll_margin_lines)
12334 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12335 scroll_margin_pos = it.current.pos;
12338 if (PT >= CHARPOS (scroll_margin_pos))
12340 int y0;
12342 /* Point is in the scroll margin at the bottom of the window, or
12343 below. Compute a new window start that makes point visible. */
12345 /* Compute the distance from the scroll margin to PT.
12346 Give up if the distance is greater than scroll_max. */
12347 start_display (&it, w, scroll_margin_pos);
12348 y0 = it.current_y;
12349 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12350 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12352 /* To make point visible, we have to move the window start
12353 down so that the line the cursor is in is visible, which
12354 means we have to add in the height of the cursor line. */
12355 dy = line_bottom_y (&it) - y0;
12357 if (dy > scroll_max)
12358 return SCROLLING_FAILED;
12360 /* Move the window start down. If scrolling conservatively,
12361 move it just enough down to make point visible. If
12362 scroll_step is set, move it down by scroll_step. */
12363 start_display (&it, w, startp);
12365 if (scroll_conservatively)
12366 /* Set AMOUNT_TO_SCROLL to at least one line,
12367 and at most scroll_conservatively lines. */
12368 amount_to_scroll
12369 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12370 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12371 else if (scroll_step || temp_scroll_step)
12372 amount_to_scroll = scroll_max;
12373 else
12375 aggressive = current_buffer->scroll_up_aggressively;
12376 height = WINDOW_BOX_TEXT_HEIGHT (w);
12377 if (NUMBERP (aggressive))
12379 double float_amount = XFLOATINT (aggressive) * height;
12380 amount_to_scroll = float_amount;
12381 if (amount_to_scroll == 0 && float_amount > 0)
12382 amount_to_scroll = 1;
12386 if (amount_to_scroll <= 0)
12387 return SCROLLING_FAILED;
12389 /* If moving by amount_to_scroll leaves STARTP unchanged,
12390 move it down one screen line. */
12392 move_it_vertically (&it, amount_to_scroll);
12393 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12394 move_it_by_lines (&it, 1, 1);
12395 startp = it.current.pos;
12397 else
12399 /* See if point is inside the scroll margin at the top of the
12400 window. */
12401 scroll_margin_pos = startp;
12402 if (this_scroll_margin)
12404 start_display (&it, w, startp);
12405 move_it_vertically (&it, this_scroll_margin);
12406 scroll_margin_pos = it.current.pos;
12409 if (PT < CHARPOS (scroll_margin_pos))
12411 /* Point is in the scroll margin at the top of the window or
12412 above what is displayed in the window. */
12413 int y0;
12415 /* Compute the vertical distance from PT to the scroll
12416 margin position. Give up if distance is greater than
12417 scroll_max. */
12418 SET_TEXT_POS (pos, PT, PT_BYTE);
12419 start_display (&it, w, pos);
12420 y0 = it.current_y;
12421 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12422 it.last_visible_y, -1,
12423 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12424 dy = it.current_y - y0;
12425 if (dy > scroll_max)
12426 return SCROLLING_FAILED;
12428 /* Compute new window start. */
12429 start_display (&it, w, startp);
12431 if (scroll_conservatively)
12432 amount_to_scroll
12433 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12434 else if (scroll_step || temp_scroll_step)
12435 amount_to_scroll = scroll_max;
12436 else
12438 aggressive = current_buffer->scroll_down_aggressively;
12439 height = WINDOW_BOX_TEXT_HEIGHT (w);
12440 if (NUMBERP (aggressive))
12442 double float_amount = XFLOATINT (aggressive) * height;
12443 amount_to_scroll = float_amount;
12444 if (amount_to_scroll == 0 && float_amount > 0)
12445 amount_to_scroll = 1;
12449 if (amount_to_scroll <= 0)
12450 return SCROLLING_FAILED;
12452 move_it_vertically_backward (&it, amount_to_scroll);
12453 startp = it.current.pos;
12457 /* Run window scroll functions. */
12458 startp = run_window_scroll_functions (window, startp);
12460 /* Display the window. Give up if new fonts are loaded, or if point
12461 doesn't appear. */
12462 if (!try_window (window, startp, 0))
12463 rc = SCROLLING_NEED_LARGER_MATRICES;
12464 else if (w->cursor.vpos < 0)
12466 clear_glyph_matrix (w->desired_matrix);
12467 rc = SCROLLING_FAILED;
12469 else
12471 /* Maybe forget recorded base line for line number display. */
12472 if (!just_this_one_p
12473 || current_buffer->clip_changed
12474 || BEG_UNCHANGED < CHARPOS (startp))
12475 w->base_line_number = Qnil;
12477 /* If cursor ends up on a partially visible line,
12478 treat that as being off the bottom of the screen. */
12479 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12481 clear_glyph_matrix (w->desired_matrix);
12482 ++extra_scroll_margin_lines;
12483 goto too_near_end;
12485 rc = SCROLLING_SUCCESS;
12488 return rc;
12492 /* Compute a suitable window start for window W if display of W starts
12493 on a continuation line. Value is non-zero if a new window start
12494 was computed.
12496 The new window start will be computed, based on W's width, starting
12497 from the start of the continued line. It is the start of the
12498 screen line with the minimum distance from the old start W->start. */
12500 static int
12501 compute_window_start_on_continuation_line (w)
12502 struct window *w;
12504 struct text_pos pos, start_pos;
12505 int window_start_changed_p = 0;
12507 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12509 /* If window start is on a continuation line... Window start may be
12510 < BEGV in case there's invisible text at the start of the
12511 buffer (M-x rmail, for example). */
12512 if (CHARPOS (start_pos) > BEGV
12513 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12515 struct it it;
12516 struct glyph_row *row;
12518 /* Handle the case that the window start is out of range. */
12519 if (CHARPOS (start_pos) < BEGV)
12520 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12521 else if (CHARPOS (start_pos) > ZV)
12522 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12524 /* Find the start of the continued line. This should be fast
12525 because scan_buffer is fast (newline cache). */
12526 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12527 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12528 row, DEFAULT_FACE_ID);
12529 reseat_at_previous_visible_line_start (&it);
12531 /* If the line start is "too far" away from the window start,
12532 say it takes too much time to compute a new window start. */
12533 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12534 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12536 int min_distance, distance;
12538 /* Move forward by display lines to find the new window
12539 start. If window width was enlarged, the new start can
12540 be expected to be > the old start. If window width was
12541 decreased, the new window start will be < the old start.
12542 So, we're looking for the display line start with the
12543 minimum distance from the old window start. */
12544 pos = it.current.pos;
12545 min_distance = INFINITY;
12546 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12547 distance < min_distance)
12549 min_distance = distance;
12550 pos = it.current.pos;
12551 move_it_by_lines (&it, 1, 0);
12554 /* Set the window start there. */
12555 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12556 window_start_changed_p = 1;
12560 return window_start_changed_p;
12564 /* Try cursor movement in case text has not changed in window WINDOW,
12565 with window start STARTP. Value is
12567 CURSOR_MOVEMENT_SUCCESS if successful
12569 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12571 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12572 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12573 we want to scroll as if scroll-step were set to 1. See the code.
12575 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12576 which case we have to abort this redisplay, and adjust matrices
12577 first. */
12579 enum
12581 CURSOR_MOVEMENT_SUCCESS,
12582 CURSOR_MOVEMENT_CANNOT_BE_USED,
12583 CURSOR_MOVEMENT_MUST_SCROLL,
12584 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12587 static int
12588 try_cursor_movement (window, startp, scroll_step)
12589 Lisp_Object window;
12590 struct text_pos startp;
12591 int *scroll_step;
12593 struct window *w = XWINDOW (window);
12594 struct frame *f = XFRAME (w->frame);
12595 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12597 #if GLYPH_DEBUG
12598 if (inhibit_try_cursor_movement)
12599 return rc;
12600 #endif
12602 /* Handle case where text has not changed, only point, and it has
12603 not moved off the frame. */
12604 if (/* Point may be in this window. */
12605 PT >= CHARPOS (startp)
12606 /* Selective display hasn't changed. */
12607 && !current_buffer->clip_changed
12608 /* Function force-mode-line-update is used to force a thorough
12609 redisplay. It sets either windows_or_buffers_changed or
12610 update_mode_lines. So don't take a shortcut here for these
12611 cases. */
12612 && !update_mode_lines
12613 && !windows_or_buffers_changed
12614 && !cursor_type_changed
12615 /* Can't use this case if highlighting a region. When a
12616 region exists, cursor movement has to do more than just
12617 set the cursor. */
12618 && !(!NILP (Vtransient_mark_mode)
12619 && !NILP (current_buffer->mark_active))
12620 && NILP (w->region_showing)
12621 && NILP (Vshow_trailing_whitespace)
12622 /* Right after splitting windows, last_point may be nil. */
12623 && INTEGERP (w->last_point)
12624 /* This code is not used for mini-buffer for the sake of the case
12625 of redisplaying to replace an echo area message; since in
12626 that case the mini-buffer contents per se are usually
12627 unchanged. This code is of no real use in the mini-buffer
12628 since the handling of this_line_start_pos, etc., in redisplay
12629 handles the same cases. */
12630 && !EQ (window, minibuf_window)
12631 /* When splitting windows or for new windows, it happens that
12632 redisplay is called with a nil window_end_vpos or one being
12633 larger than the window. This should really be fixed in
12634 window.c. I don't have this on my list, now, so we do
12635 approximately the same as the old redisplay code. --gerd. */
12636 && INTEGERP (w->window_end_vpos)
12637 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12638 && (FRAME_WINDOW_P (f)
12639 || !overlay_arrow_in_current_buffer_p ()))
12641 int this_scroll_margin, top_scroll_margin;
12642 struct glyph_row *row = NULL;
12644 #if GLYPH_DEBUG
12645 debug_method_add (w, "cursor movement");
12646 #endif
12648 /* Scroll if point within this distance from the top or bottom
12649 of the window. This is a pixel value. */
12650 this_scroll_margin = max (0, scroll_margin);
12651 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12652 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12654 top_scroll_margin = this_scroll_margin;
12655 if (WINDOW_WANTS_HEADER_LINE_P (w))
12656 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12658 /* Start with the row the cursor was displayed during the last
12659 not paused redisplay. Give up if that row is not valid. */
12660 if (w->last_cursor.vpos < 0
12661 || w->last_cursor.vpos >= w->current_matrix->nrows)
12662 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12663 else
12665 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12666 if (row->mode_line_p)
12667 ++row;
12668 if (!row->enabled_p)
12669 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12672 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12674 int scroll_p = 0;
12675 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12677 if (PT > XFASTINT (w->last_point))
12679 /* Point has moved forward. */
12680 while (MATRIX_ROW_END_CHARPOS (row) < PT
12681 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12683 xassert (row->enabled_p);
12684 ++row;
12687 /* The end position of a row equals the start position
12688 of the next row. If PT is there, we would rather
12689 display it in the next line. */
12690 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12691 && MATRIX_ROW_END_CHARPOS (row) == PT
12692 && !cursor_row_p (w, row))
12693 ++row;
12695 /* If within the scroll margin, scroll. Note that
12696 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12697 the next line would be drawn, and that
12698 this_scroll_margin can be zero. */
12699 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12700 || PT > MATRIX_ROW_END_CHARPOS (row)
12701 /* Line is completely visible last line in window
12702 and PT is to be set in the next line. */
12703 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12704 && PT == MATRIX_ROW_END_CHARPOS (row)
12705 && !row->ends_at_zv_p
12706 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12707 scroll_p = 1;
12709 else if (PT < XFASTINT (w->last_point))
12711 /* Cursor has to be moved backward. Note that PT >=
12712 CHARPOS (startp) because of the outer if-statement. */
12713 while (!row->mode_line_p
12714 && (MATRIX_ROW_START_CHARPOS (row) > PT
12715 || (MATRIX_ROW_START_CHARPOS (row) == PT
12716 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12717 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12718 row > w->current_matrix->rows
12719 && (row-1)->ends_in_newline_from_string_p))))
12720 && (row->y > top_scroll_margin
12721 || CHARPOS (startp) == BEGV))
12723 xassert (row->enabled_p);
12724 --row;
12727 /* Consider the following case: Window starts at BEGV,
12728 there is invisible, intangible text at BEGV, so that
12729 display starts at some point START > BEGV. It can
12730 happen that we are called with PT somewhere between
12731 BEGV and START. Try to handle that case. */
12732 if (row < w->current_matrix->rows
12733 || row->mode_line_p)
12735 row = w->current_matrix->rows;
12736 if (row->mode_line_p)
12737 ++row;
12740 /* Due to newlines in overlay strings, we may have to
12741 skip forward over overlay strings. */
12742 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12743 && MATRIX_ROW_END_CHARPOS (row) == PT
12744 && !cursor_row_p (w, row))
12745 ++row;
12747 /* If within the scroll margin, scroll. */
12748 if (row->y < top_scroll_margin
12749 && CHARPOS (startp) != BEGV)
12750 scroll_p = 1;
12752 else
12754 /* Cursor did not move. So don't scroll even if cursor line
12755 is partially visible, as it was so before. */
12756 rc = CURSOR_MOVEMENT_SUCCESS;
12759 if (PT < MATRIX_ROW_START_CHARPOS (row)
12760 || PT > MATRIX_ROW_END_CHARPOS (row))
12762 /* if PT is not in the glyph row, give up. */
12763 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12765 else if (rc != CURSOR_MOVEMENT_SUCCESS
12766 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12767 && make_cursor_line_fully_visible_p)
12769 if (PT == MATRIX_ROW_END_CHARPOS (row)
12770 && !row->ends_at_zv_p
12771 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12772 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12773 else if (row->height > window_box_height (w))
12775 /* If we end up in a partially visible line, let's
12776 make it fully visible, except when it's taller
12777 than the window, in which case we can't do much
12778 about it. */
12779 *scroll_step = 1;
12780 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12782 else
12784 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12785 if (!cursor_row_fully_visible_p (w, 0, 1))
12786 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12787 else
12788 rc = CURSOR_MOVEMENT_SUCCESS;
12791 else if (scroll_p)
12792 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12793 else
12797 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12799 rc = CURSOR_MOVEMENT_SUCCESS;
12800 break;
12802 ++row;
12804 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12805 && MATRIX_ROW_START_CHARPOS (row) == PT
12806 && cursor_row_p (w, row));
12811 return rc;
12814 void
12815 set_vertical_scroll_bar (w)
12816 struct window *w;
12818 int start, end, whole;
12820 /* Calculate the start and end positions for the current window.
12821 At some point, it would be nice to choose between scrollbars
12822 which reflect the whole buffer size, with special markers
12823 indicating narrowing, and scrollbars which reflect only the
12824 visible region.
12826 Note that mini-buffers sometimes aren't displaying any text. */
12827 if (!MINI_WINDOW_P (w)
12828 || (w == XWINDOW (minibuf_window)
12829 && NILP (echo_area_buffer[0])))
12831 struct buffer *buf = XBUFFER (w->buffer);
12832 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12833 start = marker_position (w->start) - BUF_BEGV (buf);
12834 /* I don't think this is guaranteed to be right. For the
12835 moment, we'll pretend it is. */
12836 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12838 if (end < start)
12839 end = start;
12840 if (whole < (end - start))
12841 whole = end - start;
12843 else
12844 start = end = whole = 0;
12846 /* Indicate what this scroll bar ought to be displaying now. */
12847 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12848 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12849 (w, end - start, whole, start);
12853 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12854 selected_window is redisplayed.
12856 We can return without actually redisplaying the window if
12857 fonts_changed_p is nonzero. In that case, redisplay_internal will
12858 retry. */
12860 static void
12861 redisplay_window (window, just_this_one_p)
12862 Lisp_Object window;
12863 int just_this_one_p;
12865 struct window *w = XWINDOW (window);
12866 struct frame *f = XFRAME (w->frame);
12867 struct buffer *buffer = XBUFFER (w->buffer);
12868 struct buffer *old = current_buffer;
12869 struct text_pos lpoint, opoint, startp;
12870 int update_mode_line;
12871 int tem;
12872 struct it it;
12873 /* Record it now because it's overwritten. */
12874 int current_matrix_up_to_date_p = 0;
12875 int used_current_matrix_p = 0;
12876 /* This is less strict than current_matrix_up_to_date_p.
12877 It indictes that the buffer contents and narrowing are unchanged. */
12878 int buffer_unchanged_p = 0;
12879 int temp_scroll_step = 0;
12880 int count = SPECPDL_INDEX ();
12881 int rc;
12882 int centering_position = -1;
12883 int last_line_misfit = 0;
12884 int beg_unchanged, end_unchanged;
12886 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12887 opoint = lpoint;
12889 /* W must be a leaf window here. */
12890 xassert (!NILP (w->buffer));
12891 #if GLYPH_DEBUG
12892 *w->desired_matrix->method = 0;
12893 #endif
12895 specbind (Qinhibit_point_motion_hooks, Qt);
12897 reconsider_clip_changes (w, buffer);
12899 /* Has the mode line to be updated? */
12900 update_mode_line = (!NILP (w->update_mode_line)
12901 || update_mode_lines
12902 || buffer->clip_changed
12903 || buffer->prevent_redisplay_optimizations_p);
12905 if (MINI_WINDOW_P (w))
12907 if (w == XWINDOW (echo_area_window)
12908 && !NILP (echo_area_buffer[0]))
12910 if (update_mode_line)
12911 /* We may have to update a tty frame's menu bar or a
12912 tool-bar. Example `M-x C-h C-h C-g'. */
12913 goto finish_menu_bars;
12914 else
12915 /* We've already displayed the echo area glyphs in this window. */
12916 goto finish_scroll_bars;
12918 else if ((w != XWINDOW (minibuf_window)
12919 || minibuf_level == 0)
12920 /* When buffer is nonempty, redisplay window normally. */
12921 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12922 /* Quail displays non-mini buffers in minibuffer window.
12923 In that case, redisplay the window normally. */
12924 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12926 /* W is a mini-buffer window, but it's not active, so clear
12927 it. */
12928 int yb = window_text_bottom_y (w);
12929 struct glyph_row *row;
12930 int y;
12932 for (y = 0, row = w->desired_matrix->rows;
12933 y < yb;
12934 y += row->height, ++row)
12935 blank_row (w, row, y);
12936 goto finish_scroll_bars;
12939 clear_glyph_matrix (w->desired_matrix);
12942 /* Otherwise set up data on this window; select its buffer and point
12943 value. */
12944 /* Really select the buffer, for the sake of buffer-local
12945 variables. */
12946 set_buffer_internal_1 (XBUFFER (w->buffer));
12947 SET_TEXT_POS (opoint, PT, PT_BYTE);
12949 beg_unchanged = BEG_UNCHANGED;
12950 end_unchanged = END_UNCHANGED;
12952 current_matrix_up_to_date_p
12953 = (!NILP (w->window_end_valid)
12954 && !current_buffer->clip_changed
12955 && !current_buffer->prevent_redisplay_optimizations_p
12956 && XFASTINT (w->last_modified) >= MODIFF
12957 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12959 buffer_unchanged_p
12960 = (!NILP (w->window_end_valid)
12961 && !current_buffer->clip_changed
12962 && XFASTINT (w->last_modified) >= MODIFF
12963 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12965 /* When windows_or_buffers_changed is non-zero, we can't rely on
12966 the window end being valid, so set it to nil there. */
12967 if (windows_or_buffers_changed)
12969 /* If window starts on a continuation line, maybe adjust the
12970 window start in case the window's width changed. */
12971 if (XMARKER (w->start)->buffer == current_buffer)
12972 compute_window_start_on_continuation_line (w);
12974 w->window_end_valid = Qnil;
12977 /* Some sanity checks. */
12978 CHECK_WINDOW_END (w);
12979 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12980 abort ();
12981 if (BYTEPOS (opoint) < CHARPOS (opoint))
12982 abort ();
12984 /* If %c is in mode line, update it if needed. */
12985 if (!NILP (w->column_number_displayed)
12986 /* This alternative quickly identifies a common case
12987 where no change is needed. */
12988 && !(PT == XFASTINT (w->last_point)
12989 && XFASTINT (w->last_modified) >= MODIFF
12990 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12991 && (XFASTINT (w->column_number_displayed)
12992 != (int) current_column ())) /* iftc */
12993 update_mode_line = 1;
12995 /* Count number of windows showing the selected buffer. An indirect
12996 buffer counts as its base buffer. */
12997 if (!just_this_one_p)
12999 struct buffer *current_base, *window_base;
13000 current_base = current_buffer;
13001 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13002 if (current_base->base_buffer)
13003 current_base = current_base->base_buffer;
13004 if (window_base->base_buffer)
13005 window_base = window_base->base_buffer;
13006 if (current_base == window_base)
13007 buffer_shared++;
13010 /* Point refers normally to the selected window. For any other
13011 window, set up appropriate value. */
13012 if (!EQ (window, selected_window))
13014 int new_pt = XMARKER (w->pointm)->charpos;
13015 int new_pt_byte = marker_byte_position (w->pointm);
13016 if (new_pt < BEGV)
13018 new_pt = BEGV;
13019 new_pt_byte = BEGV_BYTE;
13020 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13022 else if (new_pt > (ZV - 1))
13024 new_pt = ZV;
13025 new_pt_byte = ZV_BYTE;
13026 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13029 /* We don't use SET_PT so that the point-motion hooks don't run. */
13030 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13033 /* If any of the character widths specified in the display table
13034 have changed, invalidate the width run cache. It's true that
13035 this may be a bit late to catch such changes, but the rest of
13036 redisplay goes (non-fatally) haywire when the display table is
13037 changed, so why should we worry about doing any better? */
13038 if (current_buffer->width_run_cache)
13040 struct Lisp_Char_Table *disptab = buffer_display_table ();
13042 if (! disptab_matches_widthtab (disptab,
13043 XVECTOR (current_buffer->width_table)))
13045 invalidate_region_cache (current_buffer,
13046 current_buffer->width_run_cache,
13047 BEG, Z);
13048 recompute_width_table (current_buffer, disptab);
13052 /* If window-start is screwed up, choose a new one. */
13053 if (XMARKER (w->start)->buffer != current_buffer)
13054 goto recenter;
13056 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13058 /* If someone specified a new starting point but did not insist,
13059 check whether it can be used. */
13060 if (!NILP (w->optional_new_start)
13061 && CHARPOS (startp) >= BEGV
13062 && CHARPOS (startp) <= ZV)
13064 w->optional_new_start = Qnil;
13065 start_display (&it, w, startp);
13066 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13067 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13068 if (IT_CHARPOS (it) == PT)
13069 w->force_start = Qt;
13070 /* IT may overshoot PT if text at PT is invisible. */
13071 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13072 w->force_start = Qt;
13075 force_start:
13077 /* Handle case where place to start displaying has been specified,
13078 unless the specified location is outside the accessible range. */
13079 if (!NILP (w->force_start)
13080 || w->frozen_window_start_p)
13082 /* We set this later on if we have to adjust point. */
13083 int new_vpos = -1;
13084 int val;
13086 w->force_start = Qnil;
13087 w->vscroll = 0;
13088 w->window_end_valid = Qnil;
13090 /* Forget any recorded base line for line number display. */
13091 if (!buffer_unchanged_p)
13092 w->base_line_number = Qnil;
13094 /* Redisplay the mode line. Select the buffer properly for that.
13095 Also, run the hook window-scroll-functions
13096 because we have scrolled. */
13097 /* Note, we do this after clearing force_start because
13098 if there's an error, it is better to forget about force_start
13099 than to get into an infinite loop calling the hook functions
13100 and having them get more errors. */
13101 if (!update_mode_line
13102 || ! NILP (Vwindow_scroll_functions))
13104 update_mode_line = 1;
13105 w->update_mode_line = Qt;
13106 startp = run_window_scroll_functions (window, startp);
13109 w->last_modified = make_number (0);
13110 w->last_overlay_modified = make_number (0);
13111 if (CHARPOS (startp) < BEGV)
13112 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13113 else if (CHARPOS (startp) > ZV)
13114 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13116 /* Redisplay, then check if cursor has been set during the
13117 redisplay. Give up if new fonts were loaded. */
13118 val = try_window (window, startp, 1);
13119 if (!val)
13121 w->force_start = Qt;
13122 clear_glyph_matrix (w->desired_matrix);
13123 goto need_larger_matrices;
13125 /* Point was outside the scroll margins. */
13126 if (val < 0)
13127 new_vpos = window_box_height (w) / 2;
13129 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13131 /* If point does not appear, try to move point so it does
13132 appear. The desired matrix has been built above, so we
13133 can use it here. */
13134 new_vpos = window_box_height (w) / 2;
13137 if (!cursor_row_fully_visible_p (w, 0, 0))
13139 /* Point does appear, but on a line partly visible at end of window.
13140 Move it back to a fully-visible line. */
13141 new_vpos = window_box_height (w);
13144 /* If we need to move point for either of the above reasons,
13145 now actually do it. */
13146 if (new_vpos >= 0)
13148 struct glyph_row *row;
13150 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13151 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13152 ++row;
13154 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13155 MATRIX_ROW_START_BYTEPOS (row));
13157 if (w != XWINDOW (selected_window))
13158 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13159 else if (current_buffer == old)
13160 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13162 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13164 /* If we are highlighting the region, then we just changed
13165 the region, so redisplay to show it. */
13166 if (!NILP (Vtransient_mark_mode)
13167 && !NILP (current_buffer->mark_active))
13169 clear_glyph_matrix (w->desired_matrix);
13170 if (!try_window (window, startp, 0))
13171 goto need_larger_matrices;
13175 #if GLYPH_DEBUG
13176 debug_method_add (w, "forced window start");
13177 #endif
13178 goto done;
13181 /* Handle case where text has not changed, only point, and it has
13182 not moved off the frame, and we are not retrying after hscroll.
13183 (current_matrix_up_to_date_p is nonzero when retrying.) */
13184 if (current_matrix_up_to_date_p
13185 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13186 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13188 switch (rc)
13190 case CURSOR_MOVEMENT_SUCCESS:
13191 used_current_matrix_p = 1;
13192 goto done;
13194 #if 0 /* try_cursor_movement never returns this value. */
13195 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13196 goto need_larger_matrices;
13197 #endif
13199 case CURSOR_MOVEMENT_MUST_SCROLL:
13200 goto try_to_scroll;
13202 default:
13203 abort ();
13206 /* If current starting point was originally the beginning of a line
13207 but no longer is, find a new starting point. */
13208 else if (!NILP (w->start_at_line_beg)
13209 && !(CHARPOS (startp) <= BEGV
13210 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13212 #if GLYPH_DEBUG
13213 debug_method_add (w, "recenter 1");
13214 #endif
13215 goto recenter;
13218 /* Try scrolling with try_window_id. Value is > 0 if update has
13219 been done, it is -1 if we know that the same window start will
13220 not work. It is 0 if unsuccessful for some other reason. */
13221 else if ((tem = try_window_id (w)) != 0)
13223 #if GLYPH_DEBUG
13224 debug_method_add (w, "try_window_id %d", tem);
13225 #endif
13227 if (fonts_changed_p)
13228 goto need_larger_matrices;
13229 if (tem > 0)
13230 goto done;
13232 /* Otherwise try_window_id has returned -1 which means that we
13233 don't want the alternative below this comment to execute. */
13235 else if (CHARPOS (startp) >= BEGV
13236 && CHARPOS (startp) <= ZV
13237 && PT >= CHARPOS (startp)
13238 && (CHARPOS (startp) < ZV
13239 /* Avoid starting at end of buffer. */
13240 || CHARPOS (startp) == BEGV
13241 || (XFASTINT (w->last_modified) >= MODIFF
13242 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13245 /* If first window line is a continuation line, and window start
13246 is inside the modified region, but the first change is before
13247 current window start, we must select a new window start.
13249 However, if this is the result of a down-mouse event (e.g. by
13250 extending the mouse-drag-overlay), we don't want to select a
13251 new window start, since that would change the position under
13252 the mouse, resulting in an unwanted mouse-movement rather
13253 than a simple mouse-click. */
13254 if (NILP (w->start_at_line_beg)
13255 && NILP (do_mouse_tracking)
13256 && CHARPOS (startp) > BEGV
13257 && CHARPOS (startp) > BEG + beg_unchanged
13258 && CHARPOS (startp) <= Z - end_unchanged)
13260 w->force_start = Qt;
13261 if (XMARKER (w->start)->buffer == current_buffer)
13262 compute_window_start_on_continuation_line (w);
13263 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13264 goto force_start;
13267 #if GLYPH_DEBUG
13268 debug_method_add (w, "same window start");
13269 #endif
13271 /* Try to redisplay starting at same place as before.
13272 If point has not moved off frame, accept the results. */
13273 if (!current_matrix_up_to_date_p
13274 /* Don't use try_window_reusing_current_matrix in this case
13275 because a window scroll function can have changed the
13276 buffer. */
13277 || !NILP (Vwindow_scroll_functions)
13278 || MINI_WINDOW_P (w)
13279 || !(used_current_matrix_p
13280 = try_window_reusing_current_matrix (w)))
13282 IF_DEBUG (debug_method_add (w, "1"));
13283 if (try_window (window, startp, 1) < 0)
13284 /* -1 means we need to scroll.
13285 0 means we need new matrices, but fonts_changed_p
13286 is set in that case, so we will detect it below. */
13287 goto try_to_scroll;
13290 if (fonts_changed_p)
13291 goto need_larger_matrices;
13293 if (w->cursor.vpos >= 0)
13295 if (!just_this_one_p
13296 || current_buffer->clip_changed
13297 || BEG_UNCHANGED < CHARPOS (startp))
13298 /* Forget any recorded base line for line number display. */
13299 w->base_line_number = Qnil;
13301 if (!cursor_row_fully_visible_p (w, 1, 0))
13303 clear_glyph_matrix (w->desired_matrix);
13304 last_line_misfit = 1;
13306 /* Drop through and scroll. */
13307 else
13308 goto done;
13310 else
13311 clear_glyph_matrix (w->desired_matrix);
13314 try_to_scroll:
13316 w->last_modified = make_number (0);
13317 w->last_overlay_modified = make_number (0);
13319 /* Redisplay the mode line. Select the buffer properly for that. */
13320 if (!update_mode_line)
13322 update_mode_line = 1;
13323 w->update_mode_line = Qt;
13326 /* Try to scroll by specified few lines. */
13327 if ((scroll_conservatively
13328 || scroll_step
13329 || temp_scroll_step
13330 || NUMBERP (current_buffer->scroll_up_aggressively)
13331 || NUMBERP (current_buffer->scroll_down_aggressively))
13332 && !current_buffer->clip_changed
13333 && CHARPOS (startp) >= BEGV
13334 && CHARPOS (startp) <= ZV)
13336 /* The function returns -1 if new fonts were loaded, 1 if
13337 successful, 0 if not successful. */
13338 int rc = try_scrolling (window, just_this_one_p,
13339 scroll_conservatively,
13340 scroll_step,
13341 temp_scroll_step, last_line_misfit);
13342 switch (rc)
13344 case SCROLLING_SUCCESS:
13345 goto done;
13347 case SCROLLING_NEED_LARGER_MATRICES:
13348 goto need_larger_matrices;
13350 case SCROLLING_FAILED:
13351 break;
13353 default:
13354 abort ();
13358 /* Finally, just choose place to start which centers point */
13360 recenter:
13361 if (centering_position < 0)
13362 centering_position = window_box_height (w) / 2;
13364 #if GLYPH_DEBUG
13365 debug_method_add (w, "recenter");
13366 #endif
13368 /* w->vscroll = 0; */
13370 /* Forget any previously recorded base line for line number display. */
13371 if (!buffer_unchanged_p)
13372 w->base_line_number = Qnil;
13374 /* Move backward half the height of the window. */
13375 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13376 it.current_y = it.last_visible_y;
13377 move_it_vertically_backward (&it, centering_position);
13378 xassert (IT_CHARPOS (it) >= BEGV);
13380 /* The function move_it_vertically_backward may move over more
13381 than the specified y-distance. If it->w is small, e.g. a
13382 mini-buffer window, we may end up in front of the window's
13383 display area. Start displaying at the start of the line
13384 containing PT in this case. */
13385 if (it.current_y <= 0)
13387 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13388 move_it_vertically_backward (&it, 0);
13389 #if 0
13390 /* I think this assert is bogus if buffer contains
13391 invisible text or images. KFS. */
13392 xassert (IT_CHARPOS (it) <= PT);
13393 #endif
13394 it.current_y = 0;
13397 it.current_x = it.hpos = 0;
13399 /* Set startp here explicitly in case that helps avoid an infinite loop
13400 in case the window-scroll-functions functions get errors. */
13401 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13403 /* Run scroll hooks. */
13404 startp = run_window_scroll_functions (window, it.current.pos);
13406 /* Redisplay the window. */
13407 if (!current_matrix_up_to_date_p
13408 || windows_or_buffers_changed
13409 || cursor_type_changed
13410 /* Don't use try_window_reusing_current_matrix in this case
13411 because it can have changed the buffer. */
13412 || !NILP (Vwindow_scroll_functions)
13413 || !just_this_one_p
13414 || MINI_WINDOW_P (w)
13415 || !(used_current_matrix_p
13416 = try_window_reusing_current_matrix (w)))
13417 try_window (window, startp, 0);
13419 /* If new fonts have been loaded (due to fontsets), give up. We
13420 have to start a new redisplay since we need to re-adjust glyph
13421 matrices. */
13422 if (fonts_changed_p)
13423 goto need_larger_matrices;
13425 /* If cursor did not appear assume that the middle of the window is
13426 in the first line of the window. Do it again with the next line.
13427 (Imagine a window of height 100, displaying two lines of height
13428 60. Moving back 50 from it->last_visible_y will end in the first
13429 line.) */
13430 if (w->cursor.vpos < 0)
13432 if (!NILP (w->window_end_valid)
13433 && PT >= Z - XFASTINT (w->window_end_pos))
13435 clear_glyph_matrix (w->desired_matrix);
13436 move_it_by_lines (&it, 1, 0);
13437 try_window (window, it.current.pos, 0);
13439 else if (PT < IT_CHARPOS (it))
13441 clear_glyph_matrix (w->desired_matrix);
13442 move_it_by_lines (&it, -1, 0);
13443 try_window (window, it.current.pos, 0);
13445 else
13447 /* Not much we can do about it. */
13451 /* Consider the following case: Window starts at BEGV, there is
13452 invisible, intangible text at BEGV, so that display starts at
13453 some point START > BEGV. It can happen that we are called with
13454 PT somewhere between BEGV and START. Try to handle that case. */
13455 if (w->cursor.vpos < 0)
13457 struct glyph_row *row = w->current_matrix->rows;
13458 if (row->mode_line_p)
13459 ++row;
13460 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13463 if (!cursor_row_fully_visible_p (w, 0, 0))
13465 /* If vscroll is enabled, disable it and try again. */
13466 if (w->vscroll)
13468 w->vscroll = 0;
13469 clear_glyph_matrix (w->desired_matrix);
13470 goto recenter;
13473 /* If centering point failed to make the whole line visible,
13474 put point at the top instead. That has to make the whole line
13475 visible, if it can be done. */
13476 if (centering_position == 0)
13477 goto done;
13479 clear_glyph_matrix (w->desired_matrix);
13480 centering_position = 0;
13481 goto recenter;
13484 done:
13486 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13487 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13488 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13489 ? Qt : Qnil);
13491 /* Display the mode line, if we must. */
13492 if ((update_mode_line
13493 /* If window not full width, must redo its mode line
13494 if (a) the window to its side is being redone and
13495 (b) we do a frame-based redisplay. This is a consequence
13496 of how inverted lines are drawn in frame-based redisplay. */
13497 || (!just_this_one_p
13498 && !FRAME_WINDOW_P (f)
13499 && !WINDOW_FULL_WIDTH_P (w))
13500 /* Line number to display. */
13501 || INTEGERP (w->base_line_pos)
13502 /* Column number is displayed and different from the one displayed. */
13503 || (!NILP (w->column_number_displayed)
13504 && (XFASTINT (w->column_number_displayed)
13505 != (int) current_column ()))) /* iftc */
13506 /* This means that the window has a mode line. */
13507 && (WINDOW_WANTS_MODELINE_P (w)
13508 || WINDOW_WANTS_HEADER_LINE_P (w)))
13510 display_mode_lines (w);
13512 /* If mode line height has changed, arrange for a thorough
13513 immediate redisplay using the correct mode line height. */
13514 if (WINDOW_WANTS_MODELINE_P (w)
13515 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13517 fonts_changed_p = 1;
13518 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13519 = DESIRED_MODE_LINE_HEIGHT (w);
13522 /* If top line height has changed, arrange for a thorough
13523 immediate redisplay using the correct mode line height. */
13524 if (WINDOW_WANTS_HEADER_LINE_P (w)
13525 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13527 fonts_changed_p = 1;
13528 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13529 = DESIRED_HEADER_LINE_HEIGHT (w);
13532 if (fonts_changed_p)
13533 goto need_larger_matrices;
13536 if (!line_number_displayed
13537 && !BUFFERP (w->base_line_pos))
13539 w->base_line_pos = Qnil;
13540 w->base_line_number = Qnil;
13543 finish_menu_bars:
13545 /* When we reach a frame's selected window, redo the frame's menu bar. */
13546 if (update_mode_line
13547 && EQ (FRAME_SELECTED_WINDOW (f), window))
13549 int redisplay_menu_p = 0;
13550 int redisplay_tool_bar_p = 0;
13552 if (FRAME_WINDOW_P (f))
13554 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13555 || defined (USE_GTK)
13556 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13557 #else
13558 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13559 #endif
13561 else
13562 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13564 if (redisplay_menu_p)
13565 display_menu_bar (w);
13567 #ifdef HAVE_WINDOW_SYSTEM
13568 if (FRAME_WINDOW_P (f))
13570 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13571 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13572 #else
13573 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13574 && (FRAME_TOOL_BAR_LINES (f) > 0
13575 || !NILP (Vauto_resize_tool_bars));
13576 #endif
13578 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13580 extern int ignore_mouse_drag_p;
13581 ignore_mouse_drag_p = 1;
13584 #endif
13587 #ifdef HAVE_WINDOW_SYSTEM
13588 if (FRAME_WINDOW_P (f)
13589 && update_window_fringes (w, (just_this_one_p
13590 || (!used_current_matrix_p && !overlay_arrow_seen)
13591 || w->pseudo_window_p)))
13593 update_begin (f);
13594 BLOCK_INPUT;
13595 if (draw_window_fringes (w, 1))
13596 x_draw_vertical_border (w);
13597 UNBLOCK_INPUT;
13598 update_end (f);
13600 #endif /* HAVE_WINDOW_SYSTEM */
13602 /* We go to this label, with fonts_changed_p nonzero,
13603 if it is necessary to try again using larger glyph matrices.
13604 We have to redeem the scroll bar even in this case,
13605 because the loop in redisplay_internal expects that. */
13606 need_larger_matrices:
13608 finish_scroll_bars:
13610 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13612 /* Set the thumb's position and size. */
13613 set_vertical_scroll_bar (w);
13615 /* Note that we actually used the scroll bar attached to this
13616 window, so it shouldn't be deleted at the end of redisplay. */
13617 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13618 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13621 /* Restore current_buffer and value of point in it. */
13622 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13623 set_buffer_internal_1 (old);
13624 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13625 shorter. This can be caused by log truncation in *Messages*. */
13626 if (CHARPOS (lpoint) <= ZV)
13627 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13629 unbind_to (count, Qnil);
13633 /* Build the complete desired matrix of WINDOW with a window start
13634 buffer position POS.
13636 Value is 1 if successful. It is zero if fonts were loaded during
13637 redisplay which makes re-adjusting glyph matrices necessary, and -1
13638 if point would appear in the scroll margins.
13639 (We check that only if CHECK_MARGINS is nonzero. */
13642 try_window (window, pos, check_margins)
13643 Lisp_Object window;
13644 struct text_pos pos;
13645 int check_margins;
13647 struct window *w = XWINDOW (window);
13648 struct it it;
13649 struct glyph_row *last_text_row = NULL;
13650 struct frame *f = XFRAME (w->frame);
13652 /* Make POS the new window start. */
13653 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13655 /* Mark cursor position as unknown. No overlay arrow seen. */
13656 w->cursor.vpos = -1;
13657 overlay_arrow_seen = 0;
13659 /* Initialize iterator and info to start at POS. */
13660 start_display (&it, w, pos);
13662 /* Display all lines of W. */
13663 while (it.current_y < it.last_visible_y)
13665 if (display_line (&it))
13666 last_text_row = it.glyph_row - 1;
13667 if (fonts_changed_p)
13668 return 0;
13671 /* Don't let the cursor end in the scroll margins. */
13672 if (check_margins
13673 && !MINI_WINDOW_P (w))
13675 int this_scroll_margin;
13677 this_scroll_margin = max (0, scroll_margin);
13678 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13679 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13681 if ((w->cursor.y >= 0 /* not vscrolled */
13682 && w->cursor.y < this_scroll_margin
13683 && CHARPOS (pos) > BEGV
13684 && IT_CHARPOS (it) < ZV)
13685 /* rms: considering make_cursor_line_fully_visible_p here
13686 seems to give wrong results. We don't want to recenter
13687 when the last line is partly visible, we want to allow
13688 that case to be handled in the usual way. */
13689 || (w->cursor.y + 1) > it.last_visible_y)
13691 w->cursor.vpos = -1;
13692 clear_glyph_matrix (w->desired_matrix);
13693 return -1;
13697 /* If bottom moved off end of frame, change mode line percentage. */
13698 if (XFASTINT (w->window_end_pos) <= 0
13699 && Z != IT_CHARPOS (it))
13700 w->update_mode_line = Qt;
13702 /* Set window_end_pos to the offset of the last character displayed
13703 on the window from the end of current_buffer. Set
13704 window_end_vpos to its row number. */
13705 if (last_text_row)
13707 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13708 w->window_end_bytepos
13709 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13710 w->window_end_pos
13711 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13712 w->window_end_vpos
13713 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13714 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13715 ->displays_text_p);
13717 else
13719 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13720 w->window_end_pos = make_number (Z - ZV);
13721 w->window_end_vpos = make_number (0);
13724 /* But that is not valid info until redisplay finishes. */
13725 w->window_end_valid = Qnil;
13726 return 1;
13731 /************************************************************************
13732 Window redisplay reusing current matrix when buffer has not changed
13733 ************************************************************************/
13735 /* Try redisplay of window W showing an unchanged buffer with a
13736 different window start than the last time it was displayed by
13737 reusing its current matrix. Value is non-zero if successful.
13738 W->start is the new window start. */
13740 static int
13741 try_window_reusing_current_matrix (w)
13742 struct window *w;
13744 struct frame *f = XFRAME (w->frame);
13745 struct glyph_row *row, *bottom_row;
13746 struct it it;
13747 struct run run;
13748 struct text_pos start, new_start;
13749 int nrows_scrolled, i;
13750 struct glyph_row *last_text_row;
13751 struct glyph_row *last_reused_text_row;
13752 struct glyph_row *start_row;
13753 int start_vpos, min_y, max_y;
13755 #if GLYPH_DEBUG
13756 if (inhibit_try_window_reusing)
13757 return 0;
13758 #endif
13760 if (/* This function doesn't handle terminal frames. */
13761 !FRAME_WINDOW_P (f)
13762 /* Don't try to reuse the display if windows have been split
13763 or such. */
13764 || windows_or_buffers_changed
13765 || cursor_type_changed)
13766 return 0;
13768 /* Can't do this if region may have changed. */
13769 if ((!NILP (Vtransient_mark_mode)
13770 && !NILP (current_buffer->mark_active))
13771 || !NILP (w->region_showing)
13772 || !NILP (Vshow_trailing_whitespace))
13773 return 0;
13775 /* If top-line visibility has changed, give up. */
13776 if (WINDOW_WANTS_HEADER_LINE_P (w)
13777 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13778 return 0;
13780 /* Give up if old or new display is scrolled vertically. We could
13781 make this function handle this, but right now it doesn't. */
13782 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13783 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13784 return 0;
13786 /* The variable new_start now holds the new window start. The old
13787 start `start' can be determined from the current matrix. */
13788 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13789 start = start_row->start.pos;
13790 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13792 /* Clear the desired matrix for the display below. */
13793 clear_glyph_matrix (w->desired_matrix);
13795 if (CHARPOS (new_start) <= CHARPOS (start))
13797 int first_row_y;
13799 /* Don't use this method if the display starts with an ellipsis
13800 displayed for invisible text. It's not easy to handle that case
13801 below, and it's certainly not worth the effort since this is
13802 not a frequent case. */
13803 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13804 return 0;
13806 IF_DEBUG (debug_method_add (w, "twu1"));
13808 /* Display up to a row that can be reused. The variable
13809 last_text_row is set to the last row displayed that displays
13810 text. Note that it.vpos == 0 if or if not there is a
13811 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13812 start_display (&it, w, new_start);
13813 first_row_y = it.current_y;
13814 w->cursor.vpos = -1;
13815 last_text_row = last_reused_text_row = NULL;
13817 while (it.current_y < it.last_visible_y
13818 && !fonts_changed_p)
13820 /* If we have reached into the characters in the START row,
13821 that means the line boundaries have changed. So we
13822 can't start copying with the row START. Maybe it will
13823 work to start copying with the following row. */
13824 while (IT_CHARPOS (it) > CHARPOS (start))
13826 /* Advance to the next row as the "start". */
13827 start_row++;
13828 start = start_row->start.pos;
13829 /* If there are no more rows to try, or just one, give up. */
13830 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13831 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13832 || CHARPOS (start) == ZV)
13834 clear_glyph_matrix (w->desired_matrix);
13835 return 0;
13838 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13840 /* If we have reached alignment,
13841 we can copy the rest of the rows. */
13842 if (IT_CHARPOS (it) == CHARPOS (start))
13843 break;
13845 if (display_line (&it))
13846 last_text_row = it.glyph_row - 1;
13849 /* A value of current_y < last_visible_y means that we stopped
13850 at the previous window start, which in turn means that we
13851 have at least one reusable row. */
13852 if (it.current_y < it.last_visible_y)
13854 /* IT.vpos always starts from 0; it counts text lines. */
13855 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13857 /* Find PT if not already found in the lines displayed. */
13858 if (w->cursor.vpos < 0)
13860 int dy = it.current_y - start_row->y;
13862 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13863 row = row_containing_pos (w, PT, row, NULL, dy);
13864 if (row)
13865 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13866 dy, nrows_scrolled);
13867 else
13869 clear_glyph_matrix (w->desired_matrix);
13870 return 0;
13874 /* Scroll the display. Do it before the current matrix is
13875 changed. The problem here is that update has not yet
13876 run, i.e. part of the current matrix is not up to date.
13877 scroll_run_hook will clear the cursor, and use the
13878 current matrix to get the height of the row the cursor is
13879 in. */
13880 run.current_y = start_row->y;
13881 run.desired_y = it.current_y;
13882 run.height = it.last_visible_y - it.current_y;
13884 if (run.height > 0 && run.current_y != run.desired_y)
13886 update_begin (f);
13887 FRAME_RIF (f)->update_window_begin_hook (w);
13888 FRAME_RIF (f)->clear_window_mouse_face (w);
13889 FRAME_RIF (f)->scroll_run_hook (w, &run);
13890 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13891 update_end (f);
13894 /* Shift current matrix down by nrows_scrolled lines. */
13895 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13896 rotate_matrix (w->current_matrix,
13897 start_vpos,
13898 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13899 nrows_scrolled);
13901 /* Disable lines that must be updated. */
13902 for (i = 0; i < nrows_scrolled; ++i)
13903 (start_row + i)->enabled_p = 0;
13905 /* Re-compute Y positions. */
13906 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13907 max_y = it.last_visible_y;
13908 for (row = start_row + nrows_scrolled;
13909 row < bottom_row;
13910 ++row)
13912 row->y = it.current_y;
13913 row->visible_height = row->height;
13915 if (row->y < min_y)
13916 row->visible_height -= min_y - row->y;
13917 if (row->y + row->height > max_y)
13918 row->visible_height -= row->y + row->height - max_y;
13919 row->redraw_fringe_bitmaps_p = 1;
13921 it.current_y += row->height;
13923 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13924 last_reused_text_row = row;
13925 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13926 break;
13929 /* Disable lines in the current matrix which are now
13930 below the window. */
13931 for (++row; row < bottom_row; ++row)
13932 row->enabled_p = row->mode_line_p = 0;
13935 /* Update window_end_pos etc.; last_reused_text_row is the last
13936 reused row from the current matrix containing text, if any.
13937 The value of last_text_row is the last displayed line
13938 containing text. */
13939 if (last_reused_text_row)
13941 w->window_end_bytepos
13942 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13943 w->window_end_pos
13944 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13945 w->window_end_vpos
13946 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13947 w->current_matrix));
13949 else if (last_text_row)
13951 w->window_end_bytepos
13952 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13953 w->window_end_pos
13954 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13955 w->window_end_vpos
13956 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13958 else
13960 /* This window must be completely empty. */
13961 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13962 w->window_end_pos = make_number (Z - ZV);
13963 w->window_end_vpos = make_number (0);
13965 w->window_end_valid = Qnil;
13967 /* Update hint: don't try scrolling again in update_window. */
13968 w->desired_matrix->no_scrolling_p = 1;
13970 #if GLYPH_DEBUG
13971 debug_method_add (w, "try_window_reusing_current_matrix 1");
13972 #endif
13973 return 1;
13975 else if (CHARPOS (new_start) > CHARPOS (start))
13977 struct glyph_row *pt_row, *row;
13978 struct glyph_row *first_reusable_row;
13979 struct glyph_row *first_row_to_display;
13980 int dy;
13981 int yb = window_text_bottom_y (w);
13983 /* Find the row starting at new_start, if there is one. Don't
13984 reuse a partially visible line at the end. */
13985 first_reusable_row = start_row;
13986 while (first_reusable_row->enabled_p
13987 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13988 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13989 < CHARPOS (new_start)))
13990 ++first_reusable_row;
13992 /* Give up if there is no row to reuse. */
13993 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13994 || !first_reusable_row->enabled_p
13995 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13996 != CHARPOS (new_start)))
13997 return 0;
13999 /* We can reuse fully visible rows beginning with
14000 first_reusable_row to the end of the window. Set
14001 first_row_to_display to the first row that cannot be reused.
14002 Set pt_row to the row containing point, if there is any. */
14003 pt_row = NULL;
14004 for (first_row_to_display = first_reusable_row;
14005 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14006 ++first_row_to_display)
14008 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14009 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14010 pt_row = first_row_to_display;
14013 /* Start displaying at the start of first_row_to_display. */
14014 xassert (first_row_to_display->y < yb);
14015 init_to_row_start (&it, w, first_row_to_display);
14017 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14018 - start_vpos);
14019 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14020 - nrows_scrolled);
14021 it.current_y = (first_row_to_display->y - first_reusable_row->y
14022 + WINDOW_HEADER_LINE_HEIGHT (w));
14024 /* Display lines beginning with first_row_to_display in the
14025 desired matrix. Set last_text_row to the last row displayed
14026 that displays text. */
14027 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14028 if (pt_row == NULL)
14029 w->cursor.vpos = -1;
14030 last_text_row = NULL;
14031 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14032 if (display_line (&it))
14033 last_text_row = it.glyph_row - 1;
14035 /* Give up If point isn't in a row displayed or reused. */
14036 if (w->cursor.vpos < 0)
14038 clear_glyph_matrix (w->desired_matrix);
14039 return 0;
14042 /* If point is in a reused row, adjust y and vpos of the cursor
14043 position. */
14044 if (pt_row)
14046 w->cursor.vpos -= nrows_scrolled;
14047 w->cursor.y -= first_reusable_row->y - start_row->y;
14050 /* Scroll the display. */
14051 run.current_y = first_reusable_row->y;
14052 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14053 run.height = it.last_visible_y - run.current_y;
14054 dy = run.current_y - run.desired_y;
14056 if (run.height)
14058 update_begin (f);
14059 FRAME_RIF (f)->update_window_begin_hook (w);
14060 FRAME_RIF (f)->clear_window_mouse_face (w);
14061 FRAME_RIF (f)->scroll_run_hook (w, &run);
14062 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14063 update_end (f);
14066 /* Adjust Y positions of reused rows. */
14067 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14068 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14069 max_y = it.last_visible_y;
14070 for (row = first_reusable_row; row < first_row_to_display; ++row)
14072 row->y -= dy;
14073 row->visible_height = row->height;
14074 if (row->y < min_y)
14075 row->visible_height -= min_y - row->y;
14076 if (row->y + row->height > max_y)
14077 row->visible_height -= row->y + row->height - max_y;
14078 row->redraw_fringe_bitmaps_p = 1;
14081 /* Scroll the current matrix. */
14082 xassert (nrows_scrolled > 0);
14083 rotate_matrix (w->current_matrix,
14084 start_vpos,
14085 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14086 -nrows_scrolled);
14088 /* Disable rows not reused. */
14089 for (row -= nrows_scrolled; row < bottom_row; ++row)
14090 row->enabled_p = 0;
14092 /* Point may have moved to a different line, so we cannot assume that
14093 the previous cursor position is valid; locate the correct row. */
14094 if (pt_row)
14096 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14097 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14098 row++)
14100 w->cursor.vpos++;
14101 w->cursor.y = row->y;
14103 if (row < bottom_row)
14105 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14106 while (glyph->charpos < PT)
14108 w->cursor.hpos++;
14109 w->cursor.x += glyph->pixel_width;
14110 glyph++;
14115 /* Adjust window end. A null value of last_text_row means that
14116 the window end is in reused rows which in turn means that
14117 only its vpos can have changed. */
14118 if (last_text_row)
14120 w->window_end_bytepos
14121 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14122 w->window_end_pos
14123 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14124 w->window_end_vpos
14125 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14127 else
14129 w->window_end_vpos
14130 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14133 w->window_end_valid = Qnil;
14134 w->desired_matrix->no_scrolling_p = 1;
14136 #if GLYPH_DEBUG
14137 debug_method_add (w, "try_window_reusing_current_matrix 2");
14138 #endif
14139 return 1;
14142 return 0;
14147 /************************************************************************
14148 Window redisplay reusing current matrix when buffer has changed
14149 ************************************************************************/
14151 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14152 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14153 int *, int *));
14154 static struct glyph_row *
14155 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14156 struct glyph_row *));
14159 /* Return the last row in MATRIX displaying text. If row START is
14160 non-null, start searching with that row. IT gives the dimensions
14161 of the display. Value is null if matrix is empty; otherwise it is
14162 a pointer to the row found. */
14164 static struct glyph_row *
14165 find_last_row_displaying_text (matrix, it, start)
14166 struct glyph_matrix *matrix;
14167 struct it *it;
14168 struct glyph_row *start;
14170 struct glyph_row *row, *row_found;
14172 /* Set row_found to the last row in IT->w's current matrix
14173 displaying text. The loop looks funny but think of partially
14174 visible lines. */
14175 row_found = NULL;
14176 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14177 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14179 xassert (row->enabled_p);
14180 row_found = row;
14181 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14182 break;
14183 ++row;
14186 return row_found;
14190 /* Return the last row in the current matrix of W that is not affected
14191 by changes at the start of current_buffer that occurred since W's
14192 current matrix was built. Value is null if no such row exists.
14194 BEG_UNCHANGED us the number of characters unchanged at the start of
14195 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14196 first changed character in current_buffer. Characters at positions <
14197 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14198 when the current matrix was built. */
14200 static struct glyph_row *
14201 find_last_unchanged_at_beg_row (w)
14202 struct window *w;
14204 int first_changed_pos = BEG + BEG_UNCHANGED;
14205 struct glyph_row *row;
14206 struct glyph_row *row_found = NULL;
14207 int yb = window_text_bottom_y (w);
14209 /* Find the last row displaying unchanged text. */
14210 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14211 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14212 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14214 if (/* If row ends before first_changed_pos, it is unchanged,
14215 except in some case. */
14216 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14217 /* When row ends in ZV and we write at ZV it is not
14218 unchanged. */
14219 && !row->ends_at_zv_p
14220 /* When first_changed_pos is the end of a continued line,
14221 row is not unchanged because it may be no longer
14222 continued. */
14223 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14224 && (row->continued_p
14225 || row->exact_window_width_line_p)))
14226 row_found = row;
14228 /* Stop if last visible row. */
14229 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14230 break;
14232 ++row;
14235 return row_found;
14239 /* Find the first glyph row in the current matrix of W that is not
14240 affected by changes at the end of current_buffer since the
14241 time W's current matrix was built.
14243 Return in *DELTA the number of chars by which buffer positions in
14244 unchanged text at the end of current_buffer must be adjusted.
14246 Return in *DELTA_BYTES the corresponding number of bytes.
14248 Value is null if no such row exists, i.e. all rows are affected by
14249 changes. */
14251 static struct glyph_row *
14252 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14253 struct window *w;
14254 int *delta, *delta_bytes;
14256 struct glyph_row *row;
14257 struct glyph_row *row_found = NULL;
14259 *delta = *delta_bytes = 0;
14261 /* Display must not have been paused, otherwise the current matrix
14262 is not up to date. */
14263 if (NILP (w->window_end_valid))
14264 abort ();
14266 /* A value of window_end_pos >= END_UNCHANGED means that the window
14267 end is in the range of changed text. If so, there is no
14268 unchanged row at the end of W's current matrix. */
14269 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14270 return NULL;
14272 /* Set row to the last row in W's current matrix displaying text. */
14273 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14275 /* If matrix is entirely empty, no unchanged row exists. */
14276 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14278 /* The value of row is the last glyph row in the matrix having a
14279 meaningful buffer position in it. The end position of row
14280 corresponds to window_end_pos. This allows us to translate
14281 buffer positions in the current matrix to current buffer
14282 positions for characters not in changed text. */
14283 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14284 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14285 int last_unchanged_pos, last_unchanged_pos_old;
14286 struct glyph_row *first_text_row
14287 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14289 *delta = Z - Z_old;
14290 *delta_bytes = Z_BYTE - Z_BYTE_old;
14292 /* Set last_unchanged_pos to the buffer position of the last
14293 character in the buffer that has not been changed. Z is the
14294 index + 1 of the last character in current_buffer, i.e. by
14295 subtracting END_UNCHANGED we get the index of the last
14296 unchanged character, and we have to add BEG to get its buffer
14297 position. */
14298 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14299 last_unchanged_pos_old = last_unchanged_pos - *delta;
14301 /* Search backward from ROW for a row displaying a line that
14302 starts at a minimum position >= last_unchanged_pos_old. */
14303 for (; row > first_text_row; --row)
14305 /* This used to abort, but it can happen.
14306 It is ok to just stop the search instead here. KFS. */
14307 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14308 break;
14310 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14311 row_found = row;
14315 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14316 abort ();
14318 return row_found;
14322 /* Make sure that glyph rows in the current matrix of window W
14323 reference the same glyph memory as corresponding rows in the
14324 frame's frame matrix. This function is called after scrolling W's
14325 current matrix on a terminal frame in try_window_id and
14326 try_window_reusing_current_matrix. */
14328 static void
14329 sync_frame_with_window_matrix_rows (w)
14330 struct window *w;
14332 struct frame *f = XFRAME (w->frame);
14333 struct glyph_row *window_row, *window_row_end, *frame_row;
14335 /* Preconditions: W must be a leaf window and full-width. Its frame
14336 must have a frame matrix. */
14337 xassert (NILP (w->hchild) && NILP (w->vchild));
14338 xassert (WINDOW_FULL_WIDTH_P (w));
14339 xassert (!FRAME_WINDOW_P (f));
14341 /* If W is a full-width window, glyph pointers in W's current matrix
14342 have, by definition, to be the same as glyph pointers in the
14343 corresponding frame matrix. Note that frame matrices have no
14344 marginal areas (see build_frame_matrix). */
14345 window_row = w->current_matrix->rows;
14346 window_row_end = window_row + w->current_matrix->nrows;
14347 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14348 while (window_row < window_row_end)
14350 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14351 struct glyph *end = window_row->glyphs[LAST_AREA];
14353 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14354 frame_row->glyphs[TEXT_AREA] = start;
14355 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14356 frame_row->glyphs[LAST_AREA] = end;
14358 /* Disable frame rows whose corresponding window rows have
14359 been disabled in try_window_id. */
14360 if (!window_row->enabled_p)
14361 frame_row->enabled_p = 0;
14363 ++window_row, ++frame_row;
14368 /* Find the glyph row in window W containing CHARPOS. Consider all
14369 rows between START and END (not inclusive). END null means search
14370 all rows to the end of the display area of W. Value is the row
14371 containing CHARPOS or null. */
14373 struct glyph_row *
14374 row_containing_pos (w, charpos, start, end, dy)
14375 struct window *w;
14376 int charpos;
14377 struct glyph_row *start, *end;
14378 int dy;
14380 struct glyph_row *row = start;
14381 int last_y;
14383 /* If we happen to start on a header-line, skip that. */
14384 if (row->mode_line_p)
14385 ++row;
14387 if ((end && row >= end) || !row->enabled_p)
14388 return NULL;
14390 last_y = window_text_bottom_y (w) - dy;
14392 while (1)
14394 /* Give up if we have gone too far. */
14395 if (end && row >= end)
14396 return NULL;
14397 /* This formerly returned if they were equal.
14398 I think that both quantities are of a "last plus one" type;
14399 if so, when they are equal, the row is within the screen. -- rms. */
14400 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14401 return NULL;
14403 /* If it is in this row, return this row. */
14404 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14405 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14406 /* The end position of a row equals the start
14407 position of the next row. If CHARPOS is there, we
14408 would rather display it in the next line, except
14409 when this line ends in ZV. */
14410 && !row->ends_at_zv_p
14411 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14412 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14413 return row;
14414 ++row;
14419 /* Try to redisplay window W by reusing its existing display. W's
14420 current matrix must be up to date when this function is called,
14421 i.e. window_end_valid must not be nil.
14423 Value is
14425 1 if display has been updated
14426 0 if otherwise unsuccessful
14427 -1 if redisplay with same window start is known not to succeed
14429 The following steps are performed:
14431 1. Find the last row in the current matrix of W that is not
14432 affected by changes at the start of current_buffer. If no such row
14433 is found, give up.
14435 2. Find the first row in W's current matrix that is not affected by
14436 changes at the end of current_buffer. Maybe there is no such row.
14438 3. Display lines beginning with the row + 1 found in step 1 to the
14439 row found in step 2 or, if step 2 didn't find a row, to the end of
14440 the window.
14442 4. If cursor is not known to appear on the window, give up.
14444 5. If display stopped at the row found in step 2, scroll the
14445 display and current matrix as needed.
14447 6. Maybe display some lines at the end of W, if we must. This can
14448 happen under various circumstances, like a partially visible line
14449 becoming fully visible, or because newly displayed lines are displayed
14450 in smaller font sizes.
14452 7. Update W's window end information. */
14454 static int
14455 try_window_id (w)
14456 struct window *w;
14458 struct frame *f = XFRAME (w->frame);
14459 struct glyph_matrix *current_matrix = w->current_matrix;
14460 struct glyph_matrix *desired_matrix = w->desired_matrix;
14461 struct glyph_row *last_unchanged_at_beg_row;
14462 struct glyph_row *first_unchanged_at_end_row;
14463 struct glyph_row *row;
14464 struct glyph_row *bottom_row;
14465 int bottom_vpos;
14466 struct it it;
14467 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14468 struct text_pos start_pos;
14469 struct run run;
14470 int first_unchanged_at_end_vpos = 0;
14471 struct glyph_row *last_text_row, *last_text_row_at_end;
14472 struct text_pos start;
14473 int first_changed_charpos, last_changed_charpos;
14475 #if GLYPH_DEBUG
14476 if (inhibit_try_window_id)
14477 return 0;
14478 #endif
14480 /* This is handy for debugging. */
14481 #if 0
14482 #define GIVE_UP(X) \
14483 do { \
14484 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14485 return 0; \
14486 } while (0)
14487 #else
14488 #define GIVE_UP(X) return 0
14489 #endif
14491 SET_TEXT_POS_FROM_MARKER (start, w->start);
14493 /* Don't use this for mini-windows because these can show
14494 messages and mini-buffers, and we don't handle that here. */
14495 if (MINI_WINDOW_P (w))
14496 GIVE_UP (1);
14498 /* This flag is used to prevent redisplay optimizations. */
14499 if (windows_or_buffers_changed || cursor_type_changed)
14500 GIVE_UP (2);
14502 /* Verify that narrowing has not changed.
14503 Also verify that we were not told to prevent redisplay optimizations.
14504 It would be nice to further
14505 reduce the number of cases where this prevents try_window_id. */
14506 if (current_buffer->clip_changed
14507 || current_buffer->prevent_redisplay_optimizations_p)
14508 GIVE_UP (3);
14510 /* Window must either use window-based redisplay or be full width. */
14511 if (!FRAME_WINDOW_P (f)
14512 && (!FRAME_LINE_INS_DEL_OK (f)
14513 || !WINDOW_FULL_WIDTH_P (w)))
14514 GIVE_UP (4);
14516 /* Give up if point is not known NOT to appear in W. */
14517 if (PT < CHARPOS (start))
14518 GIVE_UP (5);
14520 /* Another way to prevent redisplay optimizations. */
14521 if (XFASTINT (w->last_modified) == 0)
14522 GIVE_UP (6);
14524 /* Verify that window is not hscrolled. */
14525 if (XFASTINT (w->hscroll) != 0)
14526 GIVE_UP (7);
14528 /* Verify that display wasn't paused. */
14529 if (NILP (w->window_end_valid))
14530 GIVE_UP (8);
14532 /* Can't use this if highlighting a region because a cursor movement
14533 will do more than just set the cursor. */
14534 if (!NILP (Vtransient_mark_mode)
14535 && !NILP (current_buffer->mark_active))
14536 GIVE_UP (9);
14538 /* Likewise if highlighting trailing whitespace. */
14539 if (!NILP (Vshow_trailing_whitespace))
14540 GIVE_UP (11);
14542 /* Likewise if showing a region. */
14543 if (!NILP (w->region_showing))
14544 GIVE_UP (10);
14546 /* Can use this if overlay arrow position and or string have changed. */
14547 if (overlay_arrows_changed_p ())
14548 GIVE_UP (12);
14551 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14552 only if buffer has really changed. The reason is that the gap is
14553 initially at Z for freshly visited files. The code below would
14554 set end_unchanged to 0 in that case. */
14555 if (MODIFF > SAVE_MODIFF
14556 /* This seems to happen sometimes after saving a buffer. */
14557 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14559 if (GPT - BEG < BEG_UNCHANGED)
14560 BEG_UNCHANGED = GPT - BEG;
14561 if (Z - GPT < END_UNCHANGED)
14562 END_UNCHANGED = Z - GPT;
14565 /* The position of the first and last character that has been changed. */
14566 first_changed_charpos = BEG + BEG_UNCHANGED;
14567 last_changed_charpos = Z - END_UNCHANGED;
14569 /* If window starts after a line end, and the last change is in
14570 front of that newline, then changes don't affect the display.
14571 This case happens with stealth-fontification. Note that although
14572 the display is unchanged, glyph positions in the matrix have to
14573 be adjusted, of course. */
14574 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14575 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14576 && ((last_changed_charpos < CHARPOS (start)
14577 && CHARPOS (start) == BEGV)
14578 || (last_changed_charpos < CHARPOS (start) - 1
14579 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14581 int Z_old, delta, Z_BYTE_old, delta_bytes;
14582 struct glyph_row *r0;
14584 /* Compute how many chars/bytes have been added to or removed
14585 from the buffer. */
14586 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14587 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14588 delta = Z - Z_old;
14589 delta_bytes = Z_BYTE - Z_BYTE_old;
14591 /* Give up if PT is not in the window. Note that it already has
14592 been checked at the start of try_window_id that PT is not in
14593 front of the window start. */
14594 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14595 GIVE_UP (13);
14597 /* If window start is unchanged, we can reuse the whole matrix
14598 as is, after adjusting glyph positions. No need to compute
14599 the window end again, since its offset from Z hasn't changed. */
14600 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14601 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14602 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14603 /* PT must not be in a partially visible line. */
14604 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14605 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14607 /* Adjust positions in the glyph matrix. */
14608 if (delta || delta_bytes)
14610 struct glyph_row *r1
14611 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14612 increment_matrix_positions (w->current_matrix,
14613 MATRIX_ROW_VPOS (r0, current_matrix),
14614 MATRIX_ROW_VPOS (r1, current_matrix),
14615 delta, delta_bytes);
14618 /* Set the cursor. */
14619 row = row_containing_pos (w, PT, r0, NULL, 0);
14620 if (row)
14621 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14622 else
14623 abort ();
14624 return 1;
14628 /* Handle the case that changes are all below what is displayed in
14629 the window, and that PT is in the window. This shortcut cannot
14630 be taken if ZV is visible in the window, and text has been added
14631 there that is visible in the window. */
14632 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14633 /* ZV is not visible in the window, or there are no
14634 changes at ZV, actually. */
14635 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14636 || first_changed_charpos == last_changed_charpos))
14638 struct glyph_row *r0;
14640 /* Give up if PT is not in the window. Note that it already has
14641 been checked at the start of try_window_id that PT is not in
14642 front of the window start. */
14643 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14644 GIVE_UP (14);
14646 /* If window start is unchanged, we can reuse the whole matrix
14647 as is, without changing glyph positions since no text has
14648 been added/removed in front of the window end. */
14649 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14650 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14651 /* PT must not be in a partially visible line. */
14652 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14653 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14655 /* We have to compute the window end anew since text
14656 can have been added/removed after it. */
14657 w->window_end_pos
14658 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14659 w->window_end_bytepos
14660 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14662 /* Set the cursor. */
14663 row = row_containing_pos (w, PT, r0, NULL, 0);
14664 if (row)
14665 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14666 else
14667 abort ();
14668 return 2;
14672 /* Give up if window start is in the changed area.
14674 The condition used to read
14676 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14678 but why that was tested escapes me at the moment. */
14679 if (CHARPOS (start) >= first_changed_charpos
14680 && CHARPOS (start) <= last_changed_charpos)
14681 GIVE_UP (15);
14683 /* Check that window start agrees with the start of the first glyph
14684 row in its current matrix. Check this after we know the window
14685 start is not in changed text, otherwise positions would not be
14686 comparable. */
14687 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14688 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14689 GIVE_UP (16);
14691 /* Give up if the window ends in strings. Overlay strings
14692 at the end are difficult to handle, so don't try. */
14693 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14694 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14695 GIVE_UP (20);
14697 /* Compute the position at which we have to start displaying new
14698 lines. Some of the lines at the top of the window might be
14699 reusable because they are not displaying changed text. Find the
14700 last row in W's current matrix not affected by changes at the
14701 start of current_buffer. Value is null if changes start in the
14702 first line of window. */
14703 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14704 if (last_unchanged_at_beg_row)
14706 /* Avoid starting to display in the moddle of a character, a TAB
14707 for instance. This is easier than to set up the iterator
14708 exactly, and it's not a frequent case, so the additional
14709 effort wouldn't really pay off. */
14710 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14711 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14712 && last_unchanged_at_beg_row > w->current_matrix->rows)
14713 --last_unchanged_at_beg_row;
14715 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14716 GIVE_UP (17);
14718 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14719 GIVE_UP (18);
14720 start_pos = it.current.pos;
14722 /* Start displaying new lines in the desired matrix at the same
14723 vpos we would use in the current matrix, i.e. below
14724 last_unchanged_at_beg_row. */
14725 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14726 current_matrix);
14727 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14728 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14730 xassert (it.hpos == 0 && it.current_x == 0);
14732 else
14734 /* There are no reusable lines at the start of the window.
14735 Start displaying in the first text line. */
14736 start_display (&it, w, start);
14737 it.vpos = it.first_vpos;
14738 start_pos = it.current.pos;
14741 /* Find the first row that is not affected by changes at the end of
14742 the buffer. Value will be null if there is no unchanged row, in
14743 which case we must redisplay to the end of the window. delta
14744 will be set to the value by which buffer positions beginning with
14745 first_unchanged_at_end_row have to be adjusted due to text
14746 changes. */
14747 first_unchanged_at_end_row
14748 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14749 IF_DEBUG (debug_delta = delta);
14750 IF_DEBUG (debug_delta_bytes = delta_bytes);
14752 /* Set stop_pos to the buffer position up to which we will have to
14753 display new lines. If first_unchanged_at_end_row != NULL, this
14754 is the buffer position of the start of the line displayed in that
14755 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14756 that we don't stop at a buffer position. */
14757 stop_pos = 0;
14758 if (first_unchanged_at_end_row)
14760 xassert (last_unchanged_at_beg_row == NULL
14761 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14763 /* If this is a continuation line, move forward to the next one
14764 that isn't. Changes in lines above affect this line.
14765 Caution: this may move first_unchanged_at_end_row to a row
14766 not displaying text. */
14767 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14768 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14769 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14770 < it.last_visible_y))
14771 ++first_unchanged_at_end_row;
14773 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14774 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14775 >= it.last_visible_y))
14776 first_unchanged_at_end_row = NULL;
14777 else
14779 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14780 + delta);
14781 first_unchanged_at_end_vpos
14782 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14783 xassert (stop_pos >= Z - END_UNCHANGED);
14786 else if (last_unchanged_at_beg_row == NULL)
14787 GIVE_UP (19);
14790 #if GLYPH_DEBUG
14792 /* Either there is no unchanged row at the end, or the one we have
14793 now displays text. This is a necessary condition for the window
14794 end pos calculation at the end of this function. */
14795 xassert (first_unchanged_at_end_row == NULL
14796 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14798 debug_last_unchanged_at_beg_vpos
14799 = (last_unchanged_at_beg_row
14800 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14801 : -1);
14802 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14804 #endif /* GLYPH_DEBUG != 0 */
14807 /* Display new lines. Set last_text_row to the last new line
14808 displayed which has text on it, i.e. might end up as being the
14809 line where the window_end_vpos is. */
14810 w->cursor.vpos = -1;
14811 last_text_row = NULL;
14812 overlay_arrow_seen = 0;
14813 while (it.current_y < it.last_visible_y
14814 && !fonts_changed_p
14815 && (first_unchanged_at_end_row == NULL
14816 || IT_CHARPOS (it) < stop_pos))
14818 if (display_line (&it))
14819 last_text_row = it.glyph_row - 1;
14822 if (fonts_changed_p)
14823 return -1;
14826 /* Compute differences in buffer positions, y-positions etc. for
14827 lines reused at the bottom of the window. Compute what we can
14828 scroll. */
14829 if (first_unchanged_at_end_row
14830 /* No lines reused because we displayed everything up to the
14831 bottom of the window. */
14832 && it.current_y < it.last_visible_y)
14834 dvpos = (it.vpos
14835 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14836 current_matrix));
14837 dy = it.current_y - first_unchanged_at_end_row->y;
14838 run.current_y = first_unchanged_at_end_row->y;
14839 run.desired_y = run.current_y + dy;
14840 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14842 else
14844 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14845 first_unchanged_at_end_row = NULL;
14847 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14850 /* Find the cursor if not already found. We have to decide whether
14851 PT will appear on this window (it sometimes doesn't, but this is
14852 not a very frequent case.) This decision has to be made before
14853 the current matrix is altered. A value of cursor.vpos < 0 means
14854 that PT is either in one of the lines beginning at
14855 first_unchanged_at_end_row or below the window. Don't care for
14856 lines that might be displayed later at the window end; as
14857 mentioned, this is not a frequent case. */
14858 if (w->cursor.vpos < 0)
14860 /* Cursor in unchanged rows at the top? */
14861 if (PT < CHARPOS (start_pos)
14862 && last_unchanged_at_beg_row)
14864 row = row_containing_pos (w, PT,
14865 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14866 last_unchanged_at_beg_row + 1, 0);
14867 if (row)
14868 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14871 /* Start from first_unchanged_at_end_row looking for PT. */
14872 else if (first_unchanged_at_end_row)
14874 row = row_containing_pos (w, PT - delta,
14875 first_unchanged_at_end_row, NULL, 0);
14876 if (row)
14877 set_cursor_from_row (w, row, w->current_matrix, delta,
14878 delta_bytes, dy, dvpos);
14881 /* Give up if cursor was not found. */
14882 if (w->cursor.vpos < 0)
14884 clear_glyph_matrix (w->desired_matrix);
14885 return -1;
14889 /* Don't let the cursor end in the scroll margins. */
14891 int this_scroll_margin, cursor_height;
14893 this_scroll_margin = max (0, scroll_margin);
14894 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14895 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14896 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14898 if ((w->cursor.y < this_scroll_margin
14899 && CHARPOS (start) > BEGV)
14900 /* Old redisplay didn't take scroll margin into account at the bottom,
14901 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14902 || (w->cursor.y + (make_cursor_line_fully_visible_p
14903 ? cursor_height + this_scroll_margin
14904 : 1)) > it.last_visible_y)
14906 w->cursor.vpos = -1;
14907 clear_glyph_matrix (w->desired_matrix);
14908 return -1;
14912 /* Scroll the display. Do it before changing the current matrix so
14913 that xterm.c doesn't get confused about where the cursor glyph is
14914 found. */
14915 if (dy && run.height)
14917 update_begin (f);
14919 if (FRAME_WINDOW_P (f))
14921 FRAME_RIF (f)->update_window_begin_hook (w);
14922 FRAME_RIF (f)->clear_window_mouse_face (w);
14923 FRAME_RIF (f)->scroll_run_hook (w, &run);
14924 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14926 else
14928 /* Terminal frame. In this case, dvpos gives the number of
14929 lines to scroll by; dvpos < 0 means scroll up. */
14930 int first_unchanged_at_end_vpos
14931 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14932 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14933 int end = (WINDOW_TOP_EDGE_LINE (w)
14934 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14935 + window_internal_height (w));
14937 /* Perform the operation on the screen. */
14938 if (dvpos > 0)
14940 /* Scroll last_unchanged_at_beg_row to the end of the
14941 window down dvpos lines. */
14942 set_terminal_window (f, end);
14944 /* On dumb terminals delete dvpos lines at the end
14945 before inserting dvpos empty lines. */
14946 if (!FRAME_SCROLL_REGION_OK (f))
14947 ins_del_lines (f, end - dvpos, -dvpos);
14949 /* Insert dvpos empty lines in front of
14950 last_unchanged_at_beg_row. */
14951 ins_del_lines (f, from, dvpos);
14953 else if (dvpos < 0)
14955 /* Scroll up last_unchanged_at_beg_vpos to the end of
14956 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14957 set_terminal_window (f, end);
14959 /* Delete dvpos lines in front of
14960 last_unchanged_at_beg_vpos. ins_del_lines will set
14961 the cursor to the given vpos and emit |dvpos| delete
14962 line sequences. */
14963 ins_del_lines (f, from + dvpos, dvpos);
14965 /* On a dumb terminal insert dvpos empty lines at the
14966 end. */
14967 if (!FRAME_SCROLL_REGION_OK (f))
14968 ins_del_lines (f, end + dvpos, -dvpos);
14971 set_terminal_window (f, 0);
14974 update_end (f);
14977 /* Shift reused rows of the current matrix to the right position.
14978 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14979 text. */
14980 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14981 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14982 if (dvpos < 0)
14984 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14985 bottom_vpos, dvpos);
14986 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14987 bottom_vpos, 0);
14989 else if (dvpos > 0)
14991 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14992 bottom_vpos, dvpos);
14993 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14994 first_unchanged_at_end_vpos + dvpos, 0);
14997 /* For frame-based redisplay, make sure that current frame and window
14998 matrix are in sync with respect to glyph memory. */
14999 if (!FRAME_WINDOW_P (f))
15000 sync_frame_with_window_matrix_rows (w);
15002 /* Adjust buffer positions in reused rows. */
15003 if (delta || delta_bytes)
15004 increment_matrix_positions (current_matrix,
15005 first_unchanged_at_end_vpos + dvpos,
15006 bottom_vpos, delta, delta_bytes);
15008 /* Adjust Y positions. */
15009 if (dy)
15010 shift_glyph_matrix (w, current_matrix,
15011 first_unchanged_at_end_vpos + dvpos,
15012 bottom_vpos, dy);
15014 if (first_unchanged_at_end_row)
15016 first_unchanged_at_end_row += dvpos;
15017 if (first_unchanged_at_end_row->y >= it.last_visible_y
15018 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15019 first_unchanged_at_end_row = NULL;
15022 /* If scrolling up, there may be some lines to display at the end of
15023 the window. */
15024 last_text_row_at_end = NULL;
15025 if (dy < 0)
15027 /* Scrolling up can leave for example a partially visible line
15028 at the end of the window to be redisplayed. */
15029 /* Set last_row to the glyph row in the current matrix where the
15030 window end line is found. It has been moved up or down in
15031 the matrix by dvpos. */
15032 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15033 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15035 /* If last_row is the window end line, it should display text. */
15036 xassert (last_row->displays_text_p);
15038 /* If window end line was partially visible before, begin
15039 displaying at that line. Otherwise begin displaying with the
15040 line following it. */
15041 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15043 init_to_row_start (&it, w, last_row);
15044 it.vpos = last_vpos;
15045 it.current_y = last_row->y;
15047 else
15049 init_to_row_end (&it, w, last_row);
15050 it.vpos = 1 + last_vpos;
15051 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15052 ++last_row;
15055 /* We may start in a continuation line. If so, we have to
15056 get the right continuation_lines_width and current_x. */
15057 it.continuation_lines_width = last_row->continuation_lines_width;
15058 it.hpos = it.current_x = 0;
15060 /* Display the rest of the lines at the window end. */
15061 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15062 while (it.current_y < it.last_visible_y
15063 && !fonts_changed_p)
15065 /* Is it always sure that the display agrees with lines in
15066 the current matrix? I don't think so, so we mark rows
15067 displayed invalid in the current matrix by setting their
15068 enabled_p flag to zero. */
15069 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15070 if (display_line (&it))
15071 last_text_row_at_end = it.glyph_row - 1;
15075 /* Update window_end_pos and window_end_vpos. */
15076 if (first_unchanged_at_end_row
15077 && !last_text_row_at_end)
15079 /* Window end line if one of the preserved rows from the current
15080 matrix. Set row to the last row displaying text in current
15081 matrix starting at first_unchanged_at_end_row, after
15082 scrolling. */
15083 xassert (first_unchanged_at_end_row->displays_text_p);
15084 row = find_last_row_displaying_text (w->current_matrix, &it,
15085 first_unchanged_at_end_row);
15086 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15088 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15089 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15090 w->window_end_vpos
15091 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15092 xassert (w->window_end_bytepos >= 0);
15093 IF_DEBUG (debug_method_add (w, "A"));
15095 else if (last_text_row_at_end)
15097 w->window_end_pos
15098 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15099 w->window_end_bytepos
15100 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15101 w->window_end_vpos
15102 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15103 xassert (w->window_end_bytepos >= 0);
15104 IF_DEBUG (debug_method_add (w, "B"));
15106 else if (last_text_row)
15108 /* We have displayed either to the end of the window or at the
15109 end of the window, i.e. the last row with text is to be found
15110 in the desired matrix. */
15111 w->window_end_pos
15112 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15113 w->window_end_bytepos
15114 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15115 w->window_end_vpos
15116 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15117 xassert (w->window_end_bytepos >= 0);
15119 else if (first_unchanged_at_end_row == NULL
15120 && last_text_row == NULL
15121 && last_text_row_at_end == NULL)
15123 /* Displayed to end of window, but no line containing text was
15124 displayed. Lines were deleted at the end of the window. */
15125 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15126 int vpos = XFASTINT (w->window_end_vpos);
15127 struct glyph_row *current_row = current_matrix->rows + vpos;
15128 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15130 for (row = NULL;
15131 row == NULL && vpos >= first_vpos;
15132 --vpos, --current_row, --desired_row)
15134 if (desired_row->enabled_p)
15136 if (desired_row->displays_text_p)
15137 row = desired_row;
15139 else if (current_row->displays_text_p)
15140 row = current_row;
15143 xassert (row != NULL);
15144 w->window_end_vpos = make_number (vpos + 1);
15145 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15146 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15147 xassert (w->window_end_bytepos >= 0);
15148 IF_DEBUG (debug_method_add (w, "C"));
15150 else
15151 abort ();
15153 #if 0 /* This leads to problems, for instance when the cursor is
15154 at ZV, and the cursor line displays no text. */
15155 /* Disable rows below what's displayed in the window. This makes
15156 debugging easier. */
15157 enable_glyph_matrix_rows (current_matrix,
15158 XFASTINT (w->window_end_vpos) + 1,
15159 bottom_vpos, 0);
15160 #endif
15162 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15163 debug_end_vpos = XFASTINT (w->window_end_vpos));
15165 /* Record that display has not been completed. */
15166 w->window_end_valid = Qnil;
15167 w->desired_matrix->no_scrolling_p = 1;
15168 return 3;
15170 #undef GIVE_UP
15175 /***********************************************************************
15176 More debugging support
15177 ***********************************************************************/
15179 #if GLYPH_DEBUG
15181 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15182 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15183 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15186 /* Dump the contents of glyph matrix MATRIX on stderr.
15188 GLYPHS 0 means don't show glyph contents.
15189 GLYPHS 1 means show glyphs in short form
15190 GLYPHS > 1 means show glyphs in long form. */
15192 void
15193 dump_glyph_matrix (matrix, glyphs)
15194 struct glyph_matrix *matrix;
15195 int glyphs;
15197 int i;
15198 for (i = 0; i < matrix->nrows; ++i)
15199 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15203 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15204 the glyph row and area where the glyph comes from. */
15206 void
15207 dump_glyph (row, glyph, area)
15208 struct glyph_row *row;
15209 struct glyph *glyph;
15210 int area;
15212 if (glyph->type == CHAR_GLYPH)
15214 fprintf (stderr,
15215 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15216 glyph - row->glyphs[TEXT_AREA],
15217 'C',
15218 glyph->charpos,
15219 (BUFFERP (glyph->object)
15220 ? 'B'
15221 : (STRINGP (glyph->object)
15222 ? 'S'
15223 : '-')),
15224 glyph->pixel_width,
15225 glyph->u.ch,
15226 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15227 ? glyph->u.ch
15228 : '.'),
15229 glyph->face_id,
15230 glyph->left_box_line_p,
15231 glyph->right_box_line_p);
15233 else if (glyph->type == STRETCH_GLYPH)
15235 fprintf (stderr,
15236 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15237 glyph - row->glyphs[TEXT_AREA],
15238 'S',
15239 glyph->charpos,
15240 (BUFFERP (glyph->object)
15241 ? 'B'
15242 : (STRINGP (glyph->object)
15243 ? 'S'
15244 : '-')),
15245 glyph->pixel_width,
15247 '.',
15248 glyph->face_id,
15249 glyph->left_box_line_p,
15250 glyph->right_box_line_p);
15252 else if (glyph->type == IMAGE_GLYPH)
15254 fprintf (stderr,
15255 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15256 glyph - row->glyphs[TEXT_AREA],
15257 'I',
15258 glyph->charpos,
15259 (BUFFERP (glyph->object)
15260 ? 'B'
15261 : (STRINGP (glyph->object)
15262 ? 'S'
15263 : '-')),
15264 glyph->pixel_width,
15265 glyph->u.img_id,
15266 '.',
15267 glyph->face_id,
15268 glyph->left_box_line_p,
15269 glyph->right_box_line_p);
15271 else if (glyph->type == COMPOSITE_GLYPH)
15273 fprintf (stderr,
15274 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15275 glyph - row->glyphs[TEXT_AREA],
15276 '+',
15277 glyph->charpos,
15278 (BUFFERP (glyph->object)
15279 ? 'B'
15280 : (STRINGP (glyph->object)
15281 ? 'S'
15282 : '-')),
15283 glyph->pixel_width,
15284 glyph->u.cmp_id,
15285 '.',
15286 glyph->face_id,
15287 glyph->left_box_line_p,
15288 glyph->right_box_line_p);
15293 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15294 GLYPHS 0 means don't show glyph contents.
15295 GLYPHS 1 means show glyphs in short form
15296 GLYPHS > 1 means show glyphs in long form. */
15298 void
15299 dump_glyph_row (row, vpos, glyphs)
15300 struct glyph_row *row;
15301 int vpos, glyphs;
15303 if (glyphs != 1)
15305 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15306 fprintf (stderr, "======================================================================\n");
15308 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15309 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15310 vpos,
15311 MATRIX_ROW_START_CHARPOS (row),
15312 MATRIX_ROW_END_CHARPOS (row),
15313 row->used[TEXT_AREA],
15314 row->contains_overlapping_glyphs_p,
15315 row->enabled_p,
15316 row->truncated_on_left_p,
15317 row->truncated_on_right_p,
15318 row->continued_p,
15319 MATRIX_ROW_CONTINUATION_LINE_P (row),
15320 row->displays_text_p,
15321 row->ends_at_zv_p,
15322 row->fill_line_p,
15323 row->ends_in_middle_of_char_p,
15324 row->starts_in_middle_of_char_p,
15325 row->mouse_face_p,
15326 row->x,
15327 row->y,
15328 row->pixel_width,
15329 row->height,
15330 row->visible_height,
15331 row->ascent,
15332 row->phys_ascent);
15333 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15334 row->end.overlay_string_index,
15335 row->continuation_lines_width);
15336 fprintf (stderr, "%9d %5d\n",
15337 CHARPOS (row->start.string_pos),
15338 CHARPOS (row->end.string_pos));
15339 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15340 row->end.dpvec_index);
15343 if (glyphs > 1)
15345 int area;
15347 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15349 struct glyph *glyph = row->glyphs[area];
15350 struct glyph *glyph_end = glyph + row->used[area];
15352 /* Glyph for a line end in text. */
15353 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15354 ++glyph_end;
15356 if (glyph < glyph_end)
15357 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15359 for (; glyph < glyph_end; ++glyph)
15360 dump_glyph (row, glyph, area);
15363 else if (glyphs == 1)
15365 int area;
15367 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15369 char *s = (char *) alloca (row->used[area] + 1);
15370 int i;
15372 for (i = 0; i < row->used[area]; ++i)
15374 struct glyph *glyph = row->glyphs[area] + i;
15375 if (glyph->type == CHAR_GLYPH
15376 && glyph->u.ch < 0x80
15377 && glyph->u.ch >= ' ')
15378 s[i] = glyph->u.ch;
15379 else
15380 s[i] = '.';
15383 s[i] = '\0';
15384 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15390 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15391 Sdump_glyph_matrix, 0, 1, "p",
15392 doc: /* Dump the current matrix of the selected window to stderr.
15393 Shows contents of glyph row structures. With non-nil
15394 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15395 glyphs in short form, otherwise show glyphs in long form. */)
15396 (glyphs)
15397 Lisp_Object glyphs;
15399 struct window *w = XWINDOW (selected_window);
15400 struct buffer *buffer = XBUFFER (w->buffer);
15402 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15403 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15404 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15405 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15406 fprintf (stderr, "=============================================\n");
15407 dump_glyph_matrix (w->current_matrix,
15408 NILP (glyphs) ? 0 : XINT (glyphs));
15409 return Qnil;
15413 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15414 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15417 struct frame *f = XFRAME (selected_frame);
15418 dump_glyph_matrix (f->current_matrix, 1);
15419 return Qnil;
15423 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15424 doc: /* Dump glyph row ROW to stderr.
15425 GLYPH 0 means don't dump glyphs.
15426 GLYPH 1 means dump glyphs in short form.
15427 GLYPH > 1 or omitted means dump glyphs in long form. */)
15428 (row, glyphs)
15429 Lisp_Object row, glyphs;
15431 struct glyph_matrix *matrix;
15432 int vpos;
15434 CHECK_NUMBER (row);
15435 matrix = XWINDOW (selected_window)->current_matrix;
15436 vpos = XINT (row);
15437 if (vpos >= 0 && vpos < matrix->nrows)
15438 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15439 vpos,
15440 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15441 return Qnil;
15445 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15446 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15447 GLYPH 0 means don't dump glyphs.
15448 GLYPH 1 means dump glyphs in short form.
15449 GLYPH > 1 or omitted means dump glyphs in long form. */)
15450 (row, glyphs)
15451 Lisp_Object row, glyphs;
15453 struct frame *sf = SELECTED_FRAME ();
15454 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15455 int vpos;
15457 CHECK_NUMBER (row);
15458 vpos = XINT (row);
15459 if (vpos >= 0 && vpos < m->nrows)
15460 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15461 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15462 return Qnil;
15466 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15467 doc: /* Toggle tracing of redisplay.
15468 With ARG, turn tracing on if and only if ARG is positive. */)
15469 (arg)
15470 Lisp_Object arg;
15472 if (NILP (arg))
15473 trace_redisplay_p = !trace_redisplay_p;
15474 else
15476 arg = Fprefix_numeric_value (arg);
15477 trace_redisplay_p = XINT (arg) > 0;
15480 return Qnil;
15484 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15485 doc: /* Like `format', but print result to stderr.
15486 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15487 (nargs, args)
15488 int nargs;
15489 Lisp_Object *args;
15491 Lisp_Object s = Fformat (nargs, args);
15492 fprintf (stderr, "%s", SDATA (s));
15493 return Qnil;
15496 #endif /* GLYPH_DEBUG */
15500 /***********************************************************************
15501 Building Desired Matrix Rows
15502 ***********************************************************************/
15504 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15505 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15507 static struct glyph_row *
15508 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15509 struct window *w;
15510 Lisp_Object overlay_arrow_string;
15512 struct frame *f = XFRAME (WINDOW_FRAME (w));
15513 struct buffer *buffer = XBUFFER (w->buffer);
15514 struct buffer *old = current_buffer;
15515 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15516 int arrow_len = SCHARS (overlay_arrow_string);
15517 const unsigned char *arrow_end = arrow_string + arrow_len;
15518 const unsigned char *p;
15519 struct it it;
15520 int multibyte_p;
15521 int n_glyphs_before;
15523 set_buffer_temp (buffer);
15524 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15525 it.glyph_row->used[TEXT_AREA] = 0;
15526 SET_TEXT_POS (it.position, 0, 0);
15528 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15529 p = arrow_string;
15530 while (p < arrow_end)
15532 Lisp_Object face, ilisp;
15534 /* Get the next character. */
15535 if (multibyte_p)
15536 it.c = string_char_and_length (p, arrow_len, &it.len);
15537 else
15538 it.c = *p, it.len = 1;
15539 p += it.len;
15541 /* Get its face. */
15542 ilisp = make_number (p - arrow_string);
15543 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15544 it.face_id = compute_char_face (f, it.c, face);
15546 /* Compute its width, get its glyphs. */
15547 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15548 SET_TEXT_POS (it.position, -1, -1);
15549 PRODUCE_GLYPHS (&it);
15551 /* If this character doesn't fit any more in the line, we have
15552 to remove some glyphs. */
15553 if (it.current_x > it.last_visible_x)
15555 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15556 break;
15560 set_buffer_temp (old);
15561 return it.glyph_row;
15565 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15566 glyphs are only inserted for terminal frames since we can't really
15567 win with truncation glyphs when partially visible glyphs are
15568 involved. Which glyphs to insert is determined by
15569 produce_special_glyphs. */
15571 static void
15572 insert_left_trunc_glyphs (it)
15573 struct it *it;
15575 struct it truncate_it;
15576 struct glyph *from, *end, *to, *toend;
15578 xassert (!FRAME_WINDOW_P (it->f));
15580 /* Get the truncation glyphs. */
15581 truncate_it = *it;
15582 truncate_it.current_x = 0;
15583 truncate_it.face_id = DEFAULT_FACE_ID;
15584 truncate_it.glyph_row = &scratch_glyph_row;
15585 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15586 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15587 truncate_it.object = make_number (0);
15588 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15590 /* Overwrite glyphs from IT with truncation glyphs. */
15591 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15592 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15593 to = it->glyph_row->glyphs[TEXT_AREA];
15594 toend = to + it->glyph_row->used[TEXT_AREA];
15596 while (from < end)
15597 *to++ = *from++;
15599 /* There may be padding glyphs left over. Overwrite them too. */
15600 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15602 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15603 while (from < end)
15604 *to++ = *from++;
15607 if (to > toend)
15608 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15612 /* Compute the pixel height and width of IT->glyph_row.
15614 Most of the time, ascent and height of a display line will be equal
15615 to the max_ascent and max_height values of the display iterator
15616 structure. This is not the case if
15618 1. We hit ZV without displaying anything. In this case, max_ascent
15619 and max_height will be zero.
15621 2. We have some glyphs that don't contribute to the line height.
15622 (The glyph row flag contributes_to_line_height_p is for future
15623 pixmap extensions).
15625 The first case is easily covered by using default values because in
15626 these cases, the line height does not really matter, except that it
15627 must not be zero. */
15629 static void
15630 compute_line_metrics (it)
15631 struct it *it;
15633 struct glyph_row *row = it->glyph_row;
15634 int area, i;
15636 if (FRAME_WINDOW_P (it->f))
15638 int i, min_y, max_y;
15640 /* The line may consist of one space only, that was added to
15641 place the cursor on it. If so, the row's height hasn't been
15642 computed yet. */
15643 if (row->height == 0)
15645 if (it->max_ascent + it->max_descent == 0)
15646 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15647 row->ascent = it->max_ascent;
15648 row->height = it->max_ascent + it->max_descent;
15649 row->phys_ascent = it->max_phys_ascent;
15650 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15651 row->extra_line_spacing = it->max_extra_line_spacing;
15654 /* Compute the width of this line. */
15655 row->pixel_width = row->x;
15656 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15657 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15659 xassert (row->pixel_width >= 0);
15660 xassert (row->ascent >= 0 && row->height > 0);
15662 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15663 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15665 /* If first line's physical ascent is larger than its logical
15666 ascent, use the physical ascent, and make the row taller.
15667 This makes accented characters fully visible. */
15668 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15669 && row->phys_ascent > row->ascent)
15671 row->height += row->phys_ascent - row->ascent;
15672 row->ascent = row->phys_ascent;
15675 /* Compute how much of the line is visible. */
15676 row->visible_height = row->height;
15678 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15679 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15681 if (row->y < min_y)
15682 row->visible_height -= min_y - row->y;
15683 if (row->y + row->height > max_y)
15684 row->visible_height -= row->y + row->height - max_y;
15686 else
15688 row->pixel_width = row->used[TEXT_AREA];
15689 if (row->continued_p)
15690 row->pixel_width -= it->continuation_pixel_width;
15691 else if (row->truncated_on_right_p)
15692 row->pixel_width -= it->truncation_pixel_width;
15693 row->ascent = row->phys_ascent = 0;
15694 row->height = row->phys_height = row->visible_height = 1;
15695 row->extra_line_spacing = 0;
15698 /* Compute a hash code for this row. */
15699 row->hash = 0;
15700 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15701 for (i = 0; i < row->used[area]; ++i)
15702 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15703 + row->glyphs[area][i].u.val
15704 + row->glyphs[area][i].face_id
15705 + row->glyphs[area][i].padding_p
15706 + (row->glyphs[area][i].type << 2));
15708 it->max_ascent = it->max_descent = 0;
15709 it->max_phys_ascent = it->max_phys_descent = 0;
15713 /* Append one space to the glyph row of iterator IT if doing a
15714 window-based redisplay. The space has the same face as
15715 IT->face_id. Value is non-zero if a space was added.
15717 This function is called to make sure that there is always one glyph
15718 at the end of a glyph row that the cursor can be set on under
15719 window-systems. (If there weren't such a glyph we would not know
15720 how wide and tall a box cursor should be displayed).
15722 At the same time this space let's a nicely handle clearing to the
15723 end of the line if the row ends in italic text. */
15725 static int
15726 append_space_for_newline (it, default_face_p)
15727 struct it *it;
15728 int default_face_p;
15730 if (FRAME_WINDOW_P (it->f))
15732 int n = it->glyph_row->used[TEXT_AREA];
15734 if (it->glyph_row->glyphs[TEXT_AREA] + n
15735 < it->glyph_row->glyphs[1 + TEXT_AREA])
15737 /* Save some values that must not be changed.
15738 Must save IT->c and IT->len because otherwise
15739 ITERATOR_AT_END_P wouldn't work anymore after
15740 append_space_for_newline has been called. */
15741 enum display_element_type saved_what = it->what;
15742 int saved_c = it->c, saved_len = it->len;
15743 int saved_x = it->current_x;
15744 int saved_face_id = it->face_id;
15745 struct text_pos saved_pos;
15746 Lisp_Object saved_object;
15747 struct face *face;
15749 saved_object = it->object;
15750 saved_pos = it->position;
15752 it->what = IT_CHARACTER;
15753 bzero (&it->position, sizeof it->position);
15754 it->object = make_number (0);
15755 it->c = ' ';
15756 it->len = 1;
15758 if (default_face_p)
15759 it->face_id = DEFAULT_FACE_ID;
15760 else if (it->face_before_selective_p)
15761 it->face_id = it->saved_face_id;
15762 face = FACE_FROM_ID (it->f, it->face_id);
15763 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15765 PRODUCE_GLYPHS (it);
15767 it->override_ascent = -1;
15768 it->constrain_row_ascent_descent_p = 0;
15769 it->current_x = saved_x;
15770 it->object = saved_object;
15771 it->position = saved_pos;
15772 it->what = saved_what;
15773 it->face_id = saved_face_id;
15774 it->len = saved_len;
15775 it->c = saved_c;
15776 return 1;
15780 return 0;
15784 /* Extend the face of the last glyph in the text area of IT->glyph_row
15785 to the end of the display line. Called from display_line.
15786 If the glyph row is empty, add a space glyph to it so that we
15787 know the face to draw. Set the glyph row flag fill_line_p. */
15789 static void
15790 extend_face_to_end_of_line (it)
15791 struct it *it;
15793 struct face *face;
15794 struct frame *f = it->f;
15796 /* If line is already filled, do nothing. */
15797 if (it->current_x >= it->last_visible_x)
15798 return;
15800 /* Face extension extends the background and box of IT->face_id
15801 to the end of the line. If the background equals the background
15802 of the frame, we don't have to do anything. */
15803 if (it->face_before_selective_p)
15804 face = FACE_FROM_ID (it->f, it->saved_face_id);
15805 else
15806 face = FACE_FROM_ID (f, it->face_id);
15808 if (FRAME_WINDOW_P (f)
15809 && it->glyph_row->displays_text_p
15810 && face->box == FACE_NO_BOX
15811 && face->background == FRAME_BACKGROUND_PIXEL (f)
15812 && !face->stipple)
15813 return;
15815 /* Set the glyph row flag indicating that the face of the last glyph
15816 in the text area has to be drawn to the end of the text area. */
15817 it->glyph_row->fill_line_p = 1;
15819 /* If current character of IT is not ASCII, make sure we have the
15820 ASCII face. This will be automatically undone the next time
15821 get_next_display_element returns a multibyte character. Note
15822 that the character will always be single byte in unibyte text. */
15823 if (!SINGLE_BYTE_CHAR_P (it->c))
15825 it->face_id = FACE_FOR_CHAR (f, face, 0);
15828 if (FRAME_WINDOW_P (f))
15830 /* If the row is empty, add a space with the current face of IT,
15831 so that we know which face to draw. */
15832 if (it->glyph_row->used[TEXT_AREA] == 0)
15834 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15835 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15836 it->glyph_row->used[TEXT_AREA] = 1;
15839 else
15841 /* Save some values that must not be changed. */
15842 int saved_x = it->current_x;
15843 struct text_pos saved_pos;
15844 Lisp_Object saved_object;
15845 enum display_element_type saved_what = it->what;
15846 int saved_face_id = it->face_id;
15848 saved_object = it->object;
15849 saved_pos = it->position;
15851 it->what = IT_CHARACTER;
15852 bzero (&it->position, sizeof it->position);
15853 it->object = make_number (0);
15854 it->c = ' ';
15855 it->len = 1;
15856 it->face_id = face->id;
15858 PRODUCE_GLYPHS (it);
15860 while (it->current_x <= it->last_visible_x)
15861 PRODUCE_GLYPHS (it);
15863 /* Don't count these blanks really. It would let us insert a left
15864 truncation glyph below and make us set the cursor on them, maybe. */
15865 it->current_x = saved_x;
15866 it->object = saved_object;
15867 it->position = saved_pos;
15868 it->what = saved_what;
15869 it->face_id = saved_face_id;
15874 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15875 trailing whitespace. */
15877 static int
15878 trailing_whitespace_p (charpos)
15879 int charpos;
15881 int bytepos = CHAR_TO_BYTE (charpos);
15882 int c = 0;
15884 while (bytepos < ZV_BYTE
15885 && (c = FETCH_CHAR (bytepos),
15886 c == ' ' || c == '\t'))
15887 ++bytepos;
15889 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15891 if (bytepos != PT_BYTE)
15892 return 1;
15894 return 0;
15898 /* Highlight trailing whitespace, if any, in ROW. */
15900 void
15901 highlight_trailing_whitespace (f, row)
15902 struct frame *f;
15903 struct glyph_row *row;
15905 int used = row->used[TEXT_AREA];
15907 if (used)
15909 struct glyph *start = row->glyphs[TEXT_AREA];
15910 struct glyph *glyph = start + used - 1;
15912 /* Skip over glyphs inserted to display the cursor at the
15913 end of a line, for extending the face of the last glyph
15914 to the end of the line on terminals, and for truncation
15915 and continuation glyphs. */
15916 while (glyph >= start
15917 && glyph->type == CHAR_GLYPH
15918 && INTEGERP (glyph->object))
15919 --glyph;
15921 /* If last glyph is a space or stretch, and it's trailing
15922 whitespace, set the face of all trailing whitespace glyphs in
15923 IT->glyph_row to `trailing-whitespace'. */
15924 if (glyph >= start
15925 && BUFFERP (glyph->object)
15926 && (glyph->type == STRETCH_GLYPH
15927 || (glyph->type == CHAR_GLYPH
15928 && glyph->u.ch == ' '))
15929 && trailing_whitespace_p (glyph->charpos))
15931 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15932 if (face_id < 0)
15933 return;
15935 while (glyph >= start
15936 && BUFFERP (glyph->object)
15937 && (glyph->type == STRETCH_GLYPH
15938 || (glyph->type == CHAR_GLYPH
15939 && glyph->u.ch == ' ')))
15940 (glyph--)->face_id = face_id;
15946 /* Value is non-zero if glyph row ROW in window W should be
15947 used to hold the cursor. */
15949 static int
15950 cursor_row_p (w, row)
15951 struct window *w;
15952 struct glyph_row *row;
15954 int cursor_row_p = 1;
15956 if (PT == MATRIX_ROW_END_CHARPOS (row))
15958 /* Suppose the row ends on a string.
15959 Unless the row is continued, that means it ends on a newline
15960 in the string. If it's anything other than a display string
15961 (e.g. a before-string from an overlay), we don't want the
15962 cursor there. (This heuristic seems to give the optimal
15963 behavior for the various types of multi-line strings.) */
15964 if (CHARPOS (row->end.string_pos) >= 0)
15966 if (row->continued_p)
15967 cursor_row_p = 1;
15968 else
15970 /* Check for `display' property. */
15971 struct glyph *beg = row->glyphs[TEXT_AREA];
15972 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15973 struct glyph *glyph;
15975 cursor_row_p = 0;
15976 for (glyph = end; glyph >= beg; --glyph)
15977 if (STRINGP (glyph->object))
15979 Lisp_Object prop
15980 = Fget_char_property (make_number (PT),
15981 Qdisplay, Qnil);
15982 cursor_row_p =
15983 (!NILP (prop)
15984 && display_prop_string_p (prop, glyph->object));
15985 break;
15989 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15991 /* If the row ends in middle of a real character,
15992 and the line is continued, we want the cursor here.
15993 That's because MATRIX_ROW_END_CHARPOS would equal
15994 PT if PT is before the character. */
15995 if (!row->ends_in_ellipsis_p)
15996 cursor_row_p = row->continued_p;
15997 else
15998 /* If the row ends in an ellipsis, then
15999 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16000 We want that position to be displayed after the ellipsis. */
16001 cursor_row_p = 0;
16003 /* If the row ends at ZV, display the cursor at the end of that
16004 row instead of at the start of the row below. */
16005 else if (row->ends_at_zv_p)
16006 cursor_row_p = 1;
16007 else
16008 cursor_row_p = 0;
16011 return cursor_row_p;
16015 /* Construct the glyph row IT->glyph_row in the desired matrix of
16016 IT->w from text at the current position of IT. See dispextern.h
16017 for an overview of struct it. Value is non-zero if
16018 IT->glyph_row displays text, as opposed to a line displaying ZV
16019 only. */
16021 static int
16022 display_line (it)
16023 struct it *it;
16025 struct glyph_row *row = it->glyph_row;
16026 Lisp_Object overlay_arrow_string;
16028 /* We always start displaying at hpos zero even if hscrolled. */
16029 xassert (it->hpos == 0 && it->current_x == 0);
16031 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16032 >= it->w->desired_matrix->nrows)
16034 it->w->nrows_scale_factor++;
16035 fonts_changed_p = 1;
16036 return 0;
16039 /* Is IT->w showing the region? */
16040 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16042 /* Clear the result glyph row and enable it. */
16043 prepare_desired_row (row);
16045 row->y = it->current_y;
16046 row->start = it->start;
16047 row->continuation_lines_width = it->continuation_lines_width;
16048 row->displays_text_p = 1;
16049 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16050 it->starts_in_middle_of_char_p = 0;
16052 /* Arrange the overlays nicely for our purposes. Usually, we call
16053 display_line on only one line at a time, in which case this
16054 can't really hurt too much, or we call it on lines which appear
16055 one after another in the buffer, in which case all calls to
16056 recenter_overlay_lists but the first will be pretty cheap. */
16057 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16059 /* Move over display elements that are not visible because we are
16060 hscrolled. This may stop at an x-position < IT->first_visible_x
16061 if the first glyph is partially visible or if we hit a line end. */
16062 if (it->current_x < it->first_visible_x)
16064 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16065 MOVE_TO_POS | MOVE_TO_X);
16068 /* Get the initial row height. This is either the height of the
16069 text hscrolled, if there is any, or zero. */
16070 row->ascent = it->max_ascent;
16071 row->height = it->max_ascent + it->max_descent;
16072 row->phys_ascent = it->max_phys_ascent;
16073 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16074 row->extra_line_spacing = it->max_extra_line_spacing;
16076 /* Loop generating characters. The loop is left with IT on the next
16077 character to display. */
16078 while (1)
16080 int n_glyphs_before, hpos_before, x_before;
16081 int x, i, nglyphs;
16082 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16084 /* Retrieve the next thing to display. Value is zero if end of
16085 buffer reached. */
16086 if (!get_next_display_element (it))
16088 /* Maybe add a space at the end of this line that is used to
16089 display the cursor there under X. Set the charpos of the
16090 first glyph of blank lines not corresponding to any text
16091 to -1. */
16092 #ifdef HAVE_WINDOW_SYSTEM
16093 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16094 row->exact_window_width_line_p = 1;
16095 else
16096 #endif /* HAVE_WINDOW_SYSTEM */
16097 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16098 || row->used[TEXT_AREA] == 0)
16100 row->glyphs[TEXT_AREA]->charpos = -1;
16101 row->displays_text_p = 0;
16103 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16104 && (!MINI_WINDOW_P (it->w)
16105 || (minibuf_level && EQ (it->window, minibuf_window))))
16106 row->indicate_empty_line_p = 1;
16109 it->continuation_lines_width = 0;
16110 row->ends_at_zv_p = 1;
16111 break;
16114 /* Now, get the metrics of what we want to display. This also
16115 generates glyphs in `row' (which is IT->glyph_row). */
16116 n_glyphs_before = row->used[TEXT_AREA];
16117 x = it->current_x;
16119 /* Remember the line height so far in case the next element doesn't
16120 fit on the line. */
16121 if (!it->truncate_lines_p)
16123 ascent = it->max_ascent;
16124 descent = it->max_descent;
16125 phys_ascent = it->max_phys_ascent;
16126 phys_descent = it->max_phys_descent;
16129 PRODUCE_GLYPHS (it);
16131 /* If this display element was in marginal areas, continue with
16132 the next one. */
16133 if (it->area != TEXT_AREA)
16135 row->ascent = max (row->ascent, it->max_ascent);
16136 row->height = max (row->height, it->max_ascent + it->max_descent);
16137 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16138 row->phys_height = max (row->phys_height,
16139 it->max_phys_ascent + it->max_phys_descent);
16140 row->extra_line_spacing = max (row->extra_line_spacing,
16141 it->max_extra_line_spacing);
16142 set_iterator_to_next (it, 1);
16143 continue;
16146 /* Does the display element fit on the line? If we truncate
16147 lines, we should draw past the right edge of the window. If
16148 we don't truncate, we want to stop so that we can display the
16149 continuation glyph before the right margin. If lines are
16150 continued, there are two possible strategies for characters
16151 resulting in more than 1 glyph (e.g. tabs): Display as many
16152 glyphs as possible in this line and leave the rest for the
16153 continuation line, or display the whole element in the next
16154 line. Original redisplay did the former, so we do it also. */
16155 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16156 hpos_before = it->hpos;
16157 x_before = x;
16159 if (/* Not a newline. */
16160 nglyphs > 0
16161 /* Glyphs produced fit entirely in the line. */
16162 && it->current_x < it->last_visible_x)
16164 it->hpos += nglyphs;
16165 row->ascent = max (row->ascent, it->max_ascent);
16166 row->height = max (row->height, it->max_ascent + it->max_descent);
16167 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16168 row->phys_height = max (row->phys_height,
16169 it->max_phys_ascent + it->max_phys_descent);
16170 row->extra_line_spacing = max (row->extra_line_spacing,
16171 it->max_extra_line_spacing);
16172 if (it->current_x - it->pixel_width < it->first_visible_x)
16173 row->x = x - it->first_visible_x;
16175 else
16177 int new_x;
16178 struct glyph *glyph;
16180 for (i = 0; i < nglyphs; ++i, x = new_x)
16182 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16183 new_x = x + glyph->pixel_width;
16185 if (/* Lines are continued. */
16186 !it->truncate_lines_p
16187 && (/* Glyph doesn't fit on the line. */
16188 new_x > it->last_visible_x
16189 /* Or it fits exactly on a window system frame. */
16190 || (new_x == it->last_visible_x
16191 && FRAME_WINDOW_P (it->f))))
16193 /* End of a continued line. */
16195 if (it->hpos == 0
16196 || (new_x == it->last_visible_x
16197 && FRAME_WINDOW_P (it->f)))
16199 /* Current glyph is the only one on the line or
16200 fits exactly on the line. We must continue
16201 the line because we can't draw the cursor
16202 after the glyph. */
16203 row->continued_p = 1;
16204 it->current_x = new_x;
16205 it->continuation_lines_width += new_x;
16206 ++it->hpos;
16207 if (i == nglyphs - 1)
16209 set_iterator_to_next (it, 1);
16210 #ifdef HAVE_WINDOW_SYSTEM
16211 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16213 if (!get_next_display_element (it))
16215 row->exact_window_width_line_p = 1;
16216 it->continuation_lines_width = 0;
16217 row->continued_p = 0;
16218 row->ends_at_zv_p = 1;
16220 else if (ITERATOR_AT_END_OF_LINE_P (it))
16222 row->continued_p = 0;
16223 row->exact_window_width_line_p = 1;
16226 #endif /* HAVE_WINDOW_SYSTEM */
16229 else if (CHAR_GLYPH_PADDING_P (*glyph)
16230 && !FRAME_WINDOW_P (it->f))
16232 /* A padding glyph that doesn't fit on this line.
16233 This means the whole character doesn't fit
16234 on the line. */
16235 row->used[TEXT_AREA] = n_glyphs_before;
16237 /* Fill the rest of the row with continuation
16238 glyphs like in 20.x. */
16239 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16240 < row->glyphs[1 + TEXT_AREA])
16241 produce_special_glyphs (it, IT_CONTINUATION);
16243 row->continued_p = 1;
16244 it->current_x = x_before;
16245 it->continuation_lines_width += x_before;
16247 /* Restore the height to what it was before the
16248 element not fitting on the line. */
16249 it->max_ascent = ascent;
16250 it->max_descent = descent;
16251 it->max_phys_ascent = phys_ascent;
16252 it->max_phys_descent = phys_descent;
16254 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16256 /* A TAB that extends past the right edge of the
16257 window. This produces a single glyph on
16258 window system frames. We leave the glyph in
16259 this row and let it fill the row, but don't
16260 consume the TAB. */
16261 it->continuation_lines_width += it->last_visible_x;
16262 row->ends_in_middle_of_char_p = 1;
16263 row->continued_p = 1;
16264 glyph->pixel_width = it->last_visible_x - x;
16265 it->starts_in_middle_of_char_p = 1;
16267 else
16269 /* Something other than a TAB that draws past
16270 the right edge of the window. Restore
16271 positions to values before the element. */
16272 row->used[TEXT_AREA] = n_glyphs_before + i;
16274 /* Display continuation glyphs. */
16275 if (!FRAME_WINDOW_P (it->f))
16276 produce_special_glyphs (it, IT_CONTINUATION);
16277 row->continued_p = 1;
16279 it->current_x = x_before;
16280 it->continuation_lines_width += x;
16281 extend_face_to_end_of_line (it);
16283 if (nglyphs > 1 && i > 0)
16285 row->ends_in_middle_of_char_p = 1;
16286 it->starts_in_middle_of_char_p = 1;
16289 /* Restore the height to what it was before the
16290 element not fitting on the line. */
16291 it->max_ascent = ascent;
16292 it->max_descent = descent;
16293 it->max_phys_ascent = phys_ascent;
16294 it->max_phys_descent = phys_descent;
16297 break;
16299 else if (new_x > it->first_visible_x)
16301 /* Increment number of glyphs actually displayed. */
16302 ++it->hpos;
16304 if (x < it->first_visible_x)
16305 /* Glyph is partially visible, i.e. row starts at
16306 negative X position. */
16307 row->x = x - it->first_visible_x;
16309 else
16311 /* Glyph is completely off the left margin of the
16312 window. This should not happen because of the
16313 move_it_in_display_line at the start of this
16314 function, unless the text display area of the
16315 window is empty. */
16316 xassert (it->first_visible_x <= it->last_visible_x);
16320 row->ascent = max (row->ascent, it->max_ascent);
16321 row->height = max (row->height, it->max_ascent + it->max_descent);
16322 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16323 row->phys_height = max (row->phys_height,
16324 it->max_phys_ascent + it->max_phys_descent);
16325 row->extra_line_spacing = max (row->extra_line_spacing,
16326 it->max_extra_line_spacing);
16328 /* End of this display line if row is continued. */
16329 if (row->continued_p || row->ends_at_zv_p)
16330 break;
16333 at_end_of_line:
16334 /* Is this a line end? If yes, we're also done, after making
16335 sure that a non-default face is extended up to the right
16336 margin of the window. */
16337 if (ITERATOR_AT_END_OF_LINE_P (it))
16339 int used_before = row->used[TEXT_AREA];
16341 row->ends_in_newline_from_string_p = STRINGP (it->object);
16343 #ifdef HAVE_WINDOW_SYSTEM
16344 /* Add a space at the end of the line that is used to
16345 display the cursor there. */
16346 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16347 append_space_for_newline (it, 0);
16348 #endif /* HAVE_WINDOW_SYSTEM */
16350 /* Extend the face to the end of the line. */
16351 extend_face_to_end_of_line (it);
16353 /* Make sure we have the position. */
16354 if (used_before == 0)
16355 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16357 /* Consume the line end. This skips over invisible lines. */
16358 set_iterator_to_next (it, 1);
16359 it->continuation_lines_width = 0;
16360 break;
16363 /* Proceed with next display element. Note that this skips
16364 over lines invisible because of selective display. */
16365 set_iterator_to_next (it, 1);
16367 /* If we truncate lines, we are done when the last displayed
16368 glyphs reach past the right margin of the window. */
16369 if (it->truncate_lines_p
16370 && (FRAME_WINDOW_P (it->f)
16371 ? (it->current_x >= it->last_visible_x)
16372 : (it->current_x > it->last_visible_x)))
16374 /* Maybe add truncation glyphs. */
16375 if (!FRAME_WINDOW_P (it->f))
16377 int i, n;
16379 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16380 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16381 break;
16383 for (n = row->used[TEXT_AREA]; i < n; ++i)
16385 row->used[TEXT_AREA] = i;
16386 produce_special_glyphs (it, IT_TRUNCATION);
16389 #ifdef HAVE_WINDOW_SYSTEM
16390 else
16392 /* Don't truncate if we can overflow newline into fringe. */
16393 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16395 if (!get_next_display_element (it))
16397 it->continuation_lines_width = 0;
16398 row->ends_at_zv_p = 1;
16399 row->exact_window_width_line_p = 1;
16400 break;
16402 if (ITERATOR_AT_END_OF_LINE_P (it))
16404 row->exact_window_width_line_p = 1;
16405 goto at_end_of_line;
16409 #endif /* HAVE_WINDOW_SYSTEM */
16411 row->truncated_on_right_p = 1;
16412 it->continuation_lines_width = 0;
16413 reseat_at_next_visible_line_start (it, 0);
16414 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16415 it->hpos = hpos_before;
16416 it->current_x = x_before;
16417 break;
16421 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16422 at the left window margin. */
16423 if (it->first_visible_x
16424 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16426 if (!FRAME_WINDOW_P (it->f))
16427 insert_left_trunc_glyphs (it);
16428 row->truncated_on_left_p = 1;
16431 /* If the start of this line is the overlay arrow-position, then
16432 mark this glyph row as the one containing the overlay arrow.
16433 This is clearly a mess with variable size fonts. It would be
16434 better to let it be displayed like cursors under X. */
16435 if ((row->displays_text_p || !overlay_arrow_seen)
16436 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16437 !NILP (overlay_arrow_string)))
16439 /* Overlay arrow in window redisplay is a fringe bitmap. */
16440 if (STRINGP (overlay_arrow_string))
16442 struct glyph_row *arrow_row
16443 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16444 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16445 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16446 struct glyph *p = row->glyphs[TEXT_AREA];
16447 struct glyph *p2, *end;
16449 /* Copy the arrow glyphs. */
16450 while (glyph < arrow_end)
16451 *p++ = *glyph++;
16453 /* Throw away padding glyphs. */
16454 p2 = p;
16455 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16456 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16457 ++p2;
16458 if (p2 > p)
16460 while (p2 < end)
16461 *p++ = *p2++;
16462 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16465 else
16467 xassert (INTEGERP (overlay_arrow_string));
16468 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16470 overlay_arrow_seen = 1;
16473 /* Compute pixel dimensions of this line. */
16474 compute_line_metrics (it);
16476 /* Remember the position at which this line ends. */
16477 row->end = it->current;
16479 /* Record whether this row ends inside an ellipsis. */
16480 row->ends_in_ellipsis_p
16481 = (it->method == GET_FROM_DISPLAY_VECTOR
16482 && it->ellipsis_p);
16484 /* Save fringe bitmaps in this row. */
16485 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16486 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16487 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16488 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16490 it->left_user_fringe_bitmap = 0;
16491 it->left_user_fringe_face_id = 0;
16492 it->right_user_fringe_bitmap = 0;
16493 it->right_user_fringe_face_id = 0;
16495 /* Maybe set the cursor. */
16496 if (it->w->cursor.vpos < 0
16497 && PT >= MATRIX_ROW_START_CHARPOS (row)
16498 && PT <= MATRIX_ROW_END_CHARPOS (row)
16499 && cursor_row_p (it->w, row))
16500 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16502 /* Highlight trailing whitespace. */
16503 if (!NILP (Vshow_trailing_whitespace))
16504 highlight_trailing_whitespace (it->f, it->glyph_row);
16506 /* Prepare for the next line. This line starts horizontally at (X
16507 HPOS) = (0 0). Vertical positions are incremented. As a
16508 convenience for the caller, IT->glyph_row is set to the next
16509 row to be used. */
16510 it->current_x = it->hpos = 0;
16511 it->current_y += row->height;
16512 ++it->vpos;
16513 ++it->glyph_row;
16514 it->start = it->current;
16515 return row->displays_text_p;
16520 /***********************************************************************
16521 Menu Bar
16522 ***********************************************************************/
16524 /* Redisplay the menu bar in the frame for window W.
16526 The menu bar of X frames that don't have X toolkit support is
16527 displayed in a special window W->frame->menu_bar_window.
16529 The menu bar of terminal frames is treated specially as far as
16530 glyph matrices are concerned. Menu bar lines are not part of
16531 windows, so the update is done directly on the frame matrix rows
16532 for the menu bar. */
16534 static void
16535 display_menu_bar (w)
16536 struct window *w;
16538 struct frame *f = XFRAME (WINDOW_FRAME (w));
16539 struct it it;
16540 Lisp_Object items;
16541 int i;
16543 /* Don't do all this for graphical frames. */
16544 #ifdef HAVE_NTGUI
16545 if (FRAME_W32_P (f))
16546 return;
16547 #endif
16548 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16549 if (FRAME_X_P (f))
16550 return;
16551 #endif
16552 #ifdef MAC_OS
16553 if (FRAME_MAC_P (f))
16554 return;
16555 #endif
16557 #ifdef USE_X_TOOLKIT
16558 xassert (!FRAME_WINDOW_P (f));
16559 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16560 it.first_visible_x = 0;
16561 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16562 #else /* not USE_X_TOOLKIT */
16563 if (FRAME_WINDOW_P (f))
16565 /* Menu bar lines are displayed in the desired matrix of the
16566 dummy window menu_bar_window. */
16567 struct window *menu_w;
16568 xassert (WINDOWP (f->menu_bar_window));
16569 menu_w = XWINDOW (f->menu_bar_window);
16570 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16571 MENU_FACE_ID);
16572 it.first_visible_x = 0;
16573 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16575 else
16577 /* This is a TTY frame, i.e. character hpos/vpos are used as
16578 pixel x/y. */
16579 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16580 MENU_FACE_ID);
16581 it.first_visible_x = 0;
16582 it.last_visible_x = FRAME_COLS (f);
16584 #endif /* not USE_X_TOOLKIT */
16586 if (! mode_line_inverse_video)
16587 /* Force the menu-bar to be displayed in the default face. */
16588 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16590 /* Clear all rows of the menu bar. */
16591 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16593 struct glyph_row *row = it.glyph_row + i;
16594 clear_glyph_row (row);
16595 row->enabled_p = 1;
16596 row->full_width_p = 1;
16599 /* Display all items of the menu bar. */
16600 items = FRAME_MENU_BAR_ITEMS (it.f);
16601 for (i = 0; i < XVECTOR (items)->size; i += 4)
16603 Lisp_Object string;
16605 /* Stop at nil string. */
16606 string = AREF (items, i + 1);
16607 if (NILP (string))
16608 break;
16610 /* Remember where item was displayed. */
16611 AREF (items, i + 3) = make_number (it.hpos);
16613 /* Display the item, pad with one space. */
16614 if (it.current_x < it.last_visible_x)
16615 display_string (NULL, string, Qnil, 0, 0, &it,
16616 SCHARS (string) + 1, 0, 0, -1);
16619 /* Fill out the line with spaces. */
16620 if (it.current_x < it.last_visible_x)
16621 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16623 /* Compute the total height of the lines. */
16624 compute_line_metrics (&it);
16629 /***********************************************************************
16630 Mode Line
16631 ***********************************************************************/
16633 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16634 FORCE is non-zero, redisplay mode lines unconditionally.
16635 Otherwise, redisplay only mode lines that are garbaged. Value is
16636 the number of windows whose mode lines were redisplayed. */
16638 static int
16639 redisplay_mode_lines (window, force)
16640 Lisp_Object window;
16641 int force;
16643 int nwindows = 0;
16645 while (!NILP (window))
16647 struct window *w = XWINDOW (window);
16649 if (WINDOWP (w->hchild))
16650 nwindows += redisplay_mode_lines (w->hchild, force);
16651 else if (WINDOWP (w->vchild))
16652 nwindows += redisplay_mode_lines (w->vchild, force);
16653 else if (force
16654 || FRAME_GARBAGED_P (XFRAME (w->frame))
16655 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16657 struct text_pos lpoint;
16658 struct buffer *old = current_buffer;
16660 /* Set the window's buffer for the mode line display. */
16661 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16662 set_buffer_internal_1 (XBUFFER (w->buffer));
16664 /* Point refers normally to the selected window. For any
16665 other window, set up appropriate value. */
16666 if (!EQ (window, selected_window))
16668 struct text_pos pt;
16670 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16671 if (CHARPOS (pt) < BEGV)
16672 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16673 else if (CHARPOS (pt) > (ZV - 1))
16674 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16675 else
16676 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16679 /* Display mode lines. */
16680 clear_glyph_matrix (w->desired_matrix);
16681 if (display_mode_lines (w))
16683 ++nwindows;
16684 w->must_be_updated_p = 1;
16687 /* Restore old settings. */
16688 set_buffer_internal_1 (old);
16689 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16692 window = w->next;
16695 return nwindows;
16699 /* Display the mode and/or top line of window W. Value is the number
16700 of mode lines displayed. */
16702 static int
16703 display_mode_lines (w)
16704 struct window *w;
16706 Lisp_Object old_selected_window, old_selected_frame;
16707 int n = 0;
16709 old_selected_frame = selected_frame;
16710 selected_frame = w->frame;
16711 old_selected_window = selected_window;
16712 XSETWINDOW (selected_window, w);
16714 /* These will be set while the mode line specs are processed. */
16715 line_number_displayed = 0;
16716 w->column_number_displayed = Qnil;
16718 if (WINDOW_WANTS_MODELINE_P (w))
16720 struct window *sel_w = XWINDOW (old_selected_window);
16722 /* Select mode line face based on the real selected window. */
16723 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16724 current_buffer->mode_line_format);
16725 ++n;
16728 if (WINDOW_WANTS_HEADER_LINE_P (w))
16730 display_mode_line (w, HEADER_LINE_FACE_ID,
16731 current_buffer->header_line_format);
16732 ++n;
16735 selected_frame = old_selected_frame;
16736 selected_window = old_selected_window;
16737 return n;
16741 /* Display mode or top line of window W. FACE_ID specifies which line
16742 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16743 FORMAT is the mode line format to display. Value is the pixel
16744 height of the mode line displayed. */
16746 static int
16747 display_mode_line (w, face_id, format)
16748 struct window *w;
16749 enum face_id face_id;
16750 Lisp_Object format;
16752 struct it it;
16753 struct face *face;
16754 int count = SPECPDL_INDEX ();
16756 init_iterator (&it, w, -1, -1, NULL, face_id);
16757 /* Don't extend on a previously drawn mode-line.
16758 This may happen if called from pos_visible_p. */
16759 it.glyph_row->enabled_p = 0;
16760 prepare_desired_row (it.glyph_row);
16762 it.glyph_row->mode_line_p = 1;
16764 if (! mode_line_inverse_video)
16765 /* Force the mode-line to be displayed in the default face. */
16766 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16768 record_unwind_protect (unwind_format_mode_line,
16769 format_mode_line_unwind_data (NULL, 0));
16771 mode_line_target = MODE_LINE_DISPLAY;
16773 /* Temporarily make frame's keyboard the current kboard so that
16774 kboard-local variables in the mode_line_format will get the right
16775 values. */
16776 push_kboard (FRAME_KBOARD (it.f));
16777 record_unwind_save_match_data ();
16778 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16779 pop_kboard ();
16781 unbind_to (count, Qnil);
16783 /* Fill up with spaces. */
16784 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16786 compute_line_metrics (&it);
16787 it.glyph_row->full_width_p = 1;
16788 it.glyph_row->continued_p = 0;
16789 it.glyph_row->truncated_on_left_p = 0;
16790 it.glyph_row->truncated_on_right_p = 0;
16792 /* Make a 3D mode-line have a shadow at its right end. */
16793 face = FACE_FROM_ID (it.f, face_id);
16794 extend_face_to_end_of_line (&it);
16795 if (face->box != FACE_NO_BOX)
16797 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16798 + it.glyph_row->used[TEXT_AREA] - 1);
16799 last->right_box_line_p = 1;
16802 return it.glyph_row->height;
16805 /* Move element ELT in LIST to the front of LIST.
16806 Return the updated list. */
16808 static Lisp_Object
16809 move_elt_to_front (elt, list)
16810 Lisp_Object elt, list;
16812 register Lisp_Object tail, prev;
16813 register Lisp_Object tem;
16815 tail = list;
16816 prev = Qnil;
16817 while (CONSP (tail))
16819 tem = XCAR (tail);
16821 if (EQ (elt, tem))
16823 /* Splice out the link TAIL. */
16824 if (NILP (prev))
16825 list = XCDR (tail);
16826 else
16827 Fsetcdr (prev, XCDR (tail));
16829 /* Now make it the first. */
16830 Fsetcdr (tail, list);
16831 return tail;
16833 else
16834 prev = tail;
16835 tail = XCDR (tail);
16836 QUIT;
16839 /* Not found--return unchanged LIST. */
16840 return list;
16843 /* Contribute ELT to the mode line for window IT->w. How it
16844 translates into text depends on its data type.
16846 IT describes the display environment in which we display, as usual.
16848 DEPTH is the depth in recursion. It is used to prevent
16849 infinite recursion here.
16851 FIELD_WIDTH is the number of characters the display of ELT should
16852 occupy in the mode line, and PRECISION is the maximum number of
16853 characters to display from ELT's representation. See
16854 display_string for details.
16856 Returns the hpos of the end of the text generated by ELT.
16858 PROPS is a property list to add to any string we encounter.
16860 If RISKY is nonzero, remove (disregard) any properties in any string
16861 we encounter, and ignore :eval and :propertize.
16863 The global variable `mode_line_target' determines whether the
16864 output is passed to `store_mode_line_noprop',
16865 `store_mode_line_string', or `display_string'. */
16867 static int
16868 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16869 struct it *it;
16870 int depth;
16871 int field_width, precision;
16872 Lisp_Object elt, props;
16873 int risky;
16875 int n = 0, field, prec;
16876 int literal = 0;
16878 tail_recurse:
16879 if (depth > 100)
16880 elt = build_string ("*too-deep*");
16882 depth++;
16884 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16886 case Lisp_String:
16888 /* A string: output it and check for %-constructs within it. */
16889 unsigned char c;
16890 int offset = 0;
16892 if (SCHARS (elt) > 0
16893 && (!NILP (props) || risky))
16895 Lisp_Object oprops, aelt;
16896 oprops = Ftext_properties_at (make_number (0), elt);
16898 /* If the starting string's properties are not what
16899 we want, translate the string. Also, if the string
16900 is risky, do that anyway. */
16902 if (NILP (Fequal (props, oprops)) || risky)
16904 /* If the starting string has properties,
16905 merge the specified ones onto the existing ones. */
16906 if (! NILP (oprops) && !risky)
16908 Lisp_Object tem;
16910 oprops = Fcopy_sequence (oprops);
16911 tem = props;
16912 while (CONSP (tem))
16914 oprops = Fplist_put (oprops, XCAR (tem),
16915 XCAR (XCDR (tem)));
16916 tem = XCDR (XCDR (tem));
16918 props = oprops;
16921 aelt = Fassoc (elt, mode_line_proptrans_alist);
16922 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16924 /* AELT is what we want. Move it to the front
16925 without consing. */
16926 elt = XCAR (aelt);
16927 mode_line_proptrans_alist
16928 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16930 else
16932 Lisp_Object tem;
16934 /* If AELT has the wrong props, it is useless.
16935 so get rid of it. */
16936 if (! NILP (aelt))
16937 mode_line_proptrans_alist
16938 = Fdelq (aelt, mode_line_proptrans_alist);
16940 elt = Fcopy_sequence (elt);
16941 Fset_text_properties (make_number (0), Flength (elt),
16942 props, elt);
16943 /* Add this item to mode_line_proptrans_alist. */
16944 mode_line_proptrans_alist
16945 = Fcons (Fcons (elt, props),
16946 mode_line_proptrans_alist);
16947 /* Truncate mode_line_proptrans_alist
16948 to at most 50 elements. */
16949 tem = Fnthcdr (make_number (50),
16950 mode_line_proptrans_alist);
16951 if (! NILP (tem))
16952 XSETCDR (tem, Qnil);
16957 offset = 0;
16959 if (literal)
16961 prec = precision - n;
16962 switch (mode_line_target)
16964 case MODE_LINE_NOPROP:
16965 case MODE_LINE_TITLE:
16966 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16967 break;
16968 case MODE_LINE_STRING:
16969 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16970 break;
16971 case MODE_LINE_DISPLAY:
16972 n += display_string (NULL, elt, Qnil, 0, 0, it,
16973 0, prec, 0, STRING_MULTIBYTE (elt));
16974 break;
16977 break;
16980 /* Handle the non-literal case. */
16982 while ((precision <= 0 || n < precision)
16983 && SREF (elt, offset) != 0
16984 && (mode_line_target != MODE_LINE_DISPLAY
16985 || it->current_x < it->last_visible_x))
16987 int last_offset = offset;
16989 /* Advance to end of string or next format specifier. */
16990 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16993 if (offset - 1 != last_offset)
16995 int nchars, nbytes;
16997 /* Output to end of string or up to '%'. Field width
16998 is length of string. Don't output more than
16999 PRECISION allows us. */
17000 offset--;
17002 prec = c_string_width (SDATA (elt) + last_offset,
17003 offset - last_offset, precision - n,
17004 &nchars, &nbytes);
17006 switch (mode_line_target)
17008 case MODE_LINE_NOPROP:
17009 case MODE_LINE_TITLE:
17010 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17011 break;
17012 case MODE_LINE_STRING:
17014 int bytepos = last_offset;
17015 int charpos = string_byte_to_char (elt, bytepos);
17016 int endpos = (precision <= 0
17017 ? string_byte_to_char (elt, offset)
17018 : charpos + nchars);
17020 n += store_mode_line_string (NULL,
17021 Fsubstring (elt, make_number (charpos),
17022 make_number (endpos)),
17023 0, 0, 0, Qnil);
17025 break;
17026 case MODE_LINE_DISPLAY:
17028 int bytepos = last_offset;
17029 int charpos = string_byte_to_char (elt, bytepos);
17031 if (precision <= 0)
17032 nchars = string_byte_to_char (elt, offset) - charpos;
17033 n += display_string (NULL, elt, Qnil, 0, charpos,
17034 it, 0, nchars, 0,
17035 STRING_MULTIBYTE (elt));
17037 break;
17040 else /* c == '%' */
17042 int percent_position = offset;
17044 /* Get the specified minimum width. Zero means
17045 don't pad. */
17046 field = 0;
17047 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17048 field = field * 10 + c - '0';
17050 /* Don't pad beyond the total padding allowed. */
17051 if (field_width - n > 0 && field > field_width - n)
17052 field = field_width - n;
17054 /* Note that either PRECISION <= 0 or N < PRECISION. */
17055 prec = precision - n;
17057 if (c == 'M')
17058 n += display_mode_element (it, depth, field, prec,
17059 Vglobal_mode_string, props,
17060 risky);
17061 else if (c != 0)
17063 int multibyte;
17064 int bytepos, charpos;
17065 unsigned char *spec;
17067 bytepos = percent_position;
17068 charpos = (STRING_MULTIBYTE (elt)
17069 ? string_byte_to_char (elt, bytepos)
17070 : bytepos);
17072 spec
17073 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17075 switch (mode_line_target)
17077 case MODE_LINE_NOPROP:
17078 case MODE_LINE_TITLE:
17079 n += store_mode_line_noprop (spec, field, prec);
17080 break;
17081 case MODE_LINE_STRING:
17083 int len = strlen (spec);
17084 Lisp_Object tem = make_string (spec, len);
17085 props = Ftext_properties_at (make_number (charpos), elt);
17086 /* Should only keep face property in props */
17087 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17089 break;
17090 case MODE_LINE_DISPLAY:
17092 int nglyphs_before, nwritten;
17094 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17095 nwritten = display_string (spec, Qnil, elt,
17096 charpos, 0, it,
17097 field, prec, 0,
17098 multibyte);
17100 /* Assign to the glyphs written above the
17101 string where the `%x' came from, position
17102 of the `%'. */
17103 if (nwritten > 0)
17105 struct glyph *glyph
17106 = (it->glyph_row->glyphs[TEXT_AREA]
17107 + nglyphs_before);
17108 int i;
17110 for (i = 0; i < nwritten; ++i)
17112 glyph[i].object = elt;
17113 glyph[i].charpos = charpos;
17116 n += nwritten;
17119 break;
17122 else /* c == 0 */
17123 break;
17127 break;
17129 case Lisp_Symbol:
17130 /* A symbol: process the value of the symbol recursively
17131 as if it appeared here directly. Avoid error if symbol void.
17132 Special case: if value of symbol is a string, output the string
17133 literally. */
17135 register Lisp_Object tem;
17137 /* If the variable is not marked as risky to set
17138 then its contents are risky to use. */
17139 if (NILP (Fget (elt, Qrisky_local_variable)))
17140 risky = 1;
17142 tem = Fboundp (elt);
17143 if (!NILP (tem))
17145 tem = Fsymbol_value (elt);
17146 /* If value is a string, output that string literally:
17147 don't check for % within it. */
17148 if (STRINGP (tem))
17149 literal = 1;
17151 if (!EQ (tem, elt))
17153 /* Give up right away for nil or t. */
17154 elt = tem;
17155 goto tail_recurse;
17159 break;
17161 case Lisp_Cons:
17163 register Lisp_Object car, tem;
17165 /* A cons cell: five distinct cases.
17166 If first element is :eval or :propertize, do something special.
17167 If first element is a string or a cons, process all the elements
17168 and effectively concatenate them.
17169 If first element is a negative number, truncate displaying cdr to
17170 at most that many characters. If positive, pad (with spaces)
17171 to at least that many characters.
17172 If first element is a symbol, process the cadr or caddr recursively
17173 according to whether the symbol's value is non-nil or nil. */
17174 car = XCAR (elt);
17175 if (EQ (car, QCeval))
17177 /* An element of the form (:eval FORM) means evaluate FORM
17178 and use the result as mode line elements. */
17180 if (risky)
17181 break;
17183 if (CONSP (XCDR (elt)))
17185 Lisp_Object spec;
17186 spec = safe_eval (XCAR (XCDR (elt)));
17187 n += display_mode_element (it, depth, field_width - n,
17188 precision - n, spec, props,
17189 risky);
17192 else if (EQ (car, QCpropertize))
17194 /* An element of the form (:propertize ELT PROPS...)
17195 means display ELT but applying properties PROPS. */
17197 if (risky)
17198 break;
17200 if (CONSP (XCDR (elt)))
17201 n += display_mode_element (it, depth, field_width - n,
17202 precision - n, XCAR (XCDR (elt)),
17203 XCDR (XCDR (elt)), risky);
17205 else if (SYMBOLP (car))
17207 tem = Fboundp (car);
17208 elt = XCDR (elt);
17209 if (!CONSP (elt))
17210 goto invalid;
17211 /* elt is now the cdr, and we know it is a cons cell.
17212 Use its car if CAR has a non-nil value. */
17213 if (!NILP (tem))
17215 tem = Fsymbol_value (car);
17216 if (!NILP (tem))
17218 elt = XCAR (elt);
17219 goto tail_recurse;
17222 /* Symbol's value is nil (or symbol is unbound)
17223 Get the cddr of the original list
17224 and if possible find the caddr and use that. */
17225 elt = XCDR (elt);
17226 if (NILP (elt))
17227 break;
17228 else if (!CONSP (elt))
17229 goto invalid;
17230 elt = XCAR (elt);
17231 goto tail_recurse;
17233 else if (INTEGERP (car))
17235 register int lim = XINT (car);
17236 elt = XCDR (elt);
17237 if (lim < 0)
17239 /* Negative int means reduce maximum width. */
17240 if (precision <= 0)
17241 precision = -lim;
17242 else
17243 precision = min (precision, -lim);
17245 else if (lim > 0)
17247 /* Padding specified. Don't let it be more than
17248 current maximum. */
17249 if (precision > 0)
17250 lim = min (precision, lim);
17252 /* If that's more padding than already wanted, queue it.
17253 But don't reduce padding already specified even if
17254 that is beyond the current truncation point. */
17255 field_width = max (lim, field_width);
17257 goto tail_recurse;
17259 else if (STRINGP (car) || CONSP (car))
17261 register int limit = 50;
17262 /* Limit is to protect against circular lists. */
17263 while (CONSP (elt)
17264 && --limit > 0
17265 && (precision <= 0 || n < precision))
17267 n += display_mode_element (it, depth,
17268 /* Do padding only after the last
17269 element in the list. */
17270 (! CONSP (XCDR (elt))
17271 ? field_width - n
17272 : 0),
17273 precision - n, XCAR (elt),
17274 props, risky);
17275 elt = XCDR (elt);
17279 break;
17281 default:
17282 invalid:
17283 elt = build_string ("*invalid*");
17284 goto tail_recurse;
17287 /* Pad to FIELD_WIDTH. */
17288 if (field_width > 0 && n < field_width)
17290 switch (mode_line_target)
17292 case MODE_LINE_NOPROP:
17293 case MODE_LINE_TITLE:
17294 n += store_mode_line_noprop ("", field_width - n, 0);
17295 break;
17296 case MODE_LINE_STRING:
17297 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17298 break;
17299 case MODE_LINE_DISPLAY:
17300 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17301 0, 0, 0);
17302 break;
17306 return n;
17309 /* Store a mode-line string element in mode_line_string_list.
17311 If STRING is non-null, display that C string. Otherwise, the Lisp
17312 string LISP_STRING is displayed.
17314 FIELD_WIDTH is the minimum number of output glyphs to produce.
17315 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17316 with spaces. FIELD_WIDTH <= 0 means don't pad.
17318 PRECISION is the maximum number of characters to output from
17319 STRING. PRECISION <= 0 means don't truncate the string.
17321 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17322 properties to the string.
17324 PROPS are the properties to add to the string.
17325 The mode_line_string_face face property is always added to the string.
17328 static int
17329 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17330 char *string;
17331 Lisp_Object lisp_string;
17332 int copy_string;
17333 int field_width;
17334 int precision;
17335 Lisp_Object props;
17337 int len;
17338 int n = 0;
17340 if (string != NULL)
17342 len = strlen (string);
17343 if (precision > 0 && len > precision)
17344 len = precision;
17345 lisp_string = make_string (string, len);
17346 if (NILP (props))
17347 props = mode_line_string_face_prop;
17348 else if (!NILP (mode_line_string_face))
17350 Lisp_Object face = Fplist_get (props, Qface);
17351 props = Fcopy_sequence (props);
17352 if (NILP (face))
17353 face = mode_line_string_face;
17354 else
17355 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17356 props = Fplist_put (props, Qface, face);
17358 Fadd_text_properties (make_number (0), make_number (len),
17359 props, lisp_string);
17361 else
17363 len = XFASTINT (Flength (lisp_string));
17364 if (precision > 0 && len > precision)
17366 len = precision;
17367 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17368 precision = -1;
17370 if (!NILP (mode_line_string_face))
17372 Lisp_Object face;
17373 if (NILP (props))
17374 props = Ftext_properties_at (make_number (0), lisp_string);
17375 face = Fplist_get (props, Qface);
17376 if (NILP (face))
17377 face = mode_line_string_face;
17378 else
17379 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17380 props = Fcons (Qface, Fcons (face, Qnil));
17381 if (copy_string)
17382 lisp_string = Fcopy_sequence (lisp_string);
17384 if (!NILP (props))
17385 Fadd_text_properties (make_number (0), make_number (len),
17386 props, lisp_string);
17389 if (len > 0)
17391 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17392 n += len;
17395 if (field_width > len)
17397 field_width -= len;
17398 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17399 if (!NILP (props))
17400 Fadd_text_properties (make_number (0), make_number (field_width),
17401 props, lisp_string);
17402 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17403 n += field_width;
17406 return n;
17410 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17411 1, 4, 0,
17412 doc: /* Format a string out of a mode line format specification.
17413 First arg FORMAT specifies the mode line format (see `mode-line-format'
17414 for details) to use.
17416 Optional second arg FACE specifies the face property to put
17417 on all characters for which no face is specified.
17418 The value t means whatever face the window's mode line currently uses
17419 \(either `mode-line' or `mode-line-inactive', depending).
17420 A value of nil means the default is no face property.
17421 If FACE is an integer, the value string has no text properties.
17423 Optional third and fourth args WINDOW and BUFFER specify the window
17424 and buffer to use as the context for the formatting (defaults
17425 are the selected window and the window's buffer). */)
17426 (format, face, window, buffer)
17427 Lisp_Object format, face, window, buffer;
17429 struct it it;
17430 int len;
17431 struct window *w;
17432 struct buffer *old_buffer = NULL;
17433 int face_id = -1;
17434 int no_props = INTEGERP (face);
17435 int count = SPECPDL_INDEX ();
17436 Lisp_Object str;
17437 int string_start = 0;
17439 if (NILP (window))
17440 window = selected_window;
17441 CHECK_WINDOW (window);
17442 w = XWINDOW (window);
17444 if (NILP (buffer))
17445 buffer = w->buffer;
17446 CHECK_BUFFER (buffer);
17448 /* Make formatting the modeline a non-op when noninteractive, otherwise
17449 there will be problems later caused by a partially initialized frame. */
17450 if (NILP (format) || noninteractive)
17451 return empty_unibyte_string;
17453 if (no_props)
17454 face = Qnil;
17456 if (!NILP (face))
17458 if (EQ (face, Qt))
17459 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17460 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17463 if (face_id < 0)
17464 face_id = DEFAULT_FACE_ID;
17466 if (XBUFFER (buffer) != current_buffer)
17467 old_buffer = current_buffer;
17469 /* Save things including mode_line_proptrans_alist,
17470 and set that to nil so that we don't alter the outer value. */
17471 record_unwind_protect (unwind_format_mode_line,
17472 format_mode_line_unwind_data (old_buffer, 1));
17473 mode_line_proptrans_alist = Qnil;
17475 if (old_buffer)
17476 set_buffer_internal_1 (XBUFFER (buffer));
17478 init_iterator (&it, w, -1, -1, NULL, face_id);
17480 if (no_props)
17482 mode_line_target = MODE_LINE_NOPROP;
17483 mode_line_string_face_prop = Qnil;
17484 mode_line_string_list = Qnil;
17485 string_start = MODE_LINE_NOPROP_LEN (0);
17487 else
17489 mode_line_target = MODE_LINE_STRING;
17490 mode_line_string_list = Qnil;
17491 mode_line_string_face = face;
17492 mode_line_string_face_prop
17493 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17496 push_kboard (FRAME_KBOARD (it.f));
17497 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17498 pop_kboard ();
17500 if (no_props)
17502 len = MODE_LINE_NOPROP_LEN (string_start);
17503 str = make_string (mode_line_noprop_buf + string_start, len);
17505 else
17507 mode_line_string_list = Fnreverse (mode_line_string_list);
17508 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17509 empty_unibyte_string);
17512 unbind_to (count, Qnil);
17513 return str;
17516 /* Write a null-terminated, right justified decimal representation of
17517 the positive integer D to BUF using a minimal field width WIDTH. */
17519 static void
17520 pint2str (buf, width, d)
17521 register char *buf;
17522 register int width;
17523 register int d;
17525 register char *p = buf;
17527 if (d <= 0)
17528 *p++ = '0';
17529 else
17531 while (d > 0)
17533 *p++ = d % 10 + '0';
17534 d /= 10;
17538 for (width -= (int) (p - buf); width > 0; --width)
17539 *p++ = ' ';
17540 *p-- = '\0';
17541 while (p > buf)
17543 d = *buf;
17544 *buf++ = *p;
17545 *p-- = d;
17549 /* Write a null-terminated, right justified decimal and "human
17550 readable" representation of the nonnegative integer D to BUF using
17551 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17553 static const char power_letter[] =
17555 0, /* not used */
17556 'k', /* kilo */
17557 'M', /* mega */
17558 'G', /* giga */
17559 'T', /* tera */
17560 'P', /* peta */
17561 'E', /* exa */
17562 'Z', /* zetta */
17563 'Y' /* yotta */
17566 static void
17567 pint2hrstr (buf, width, d)
17568 char *buf;
17569 int width;
17570 int d;
17572 /* We aim to represent the nonnegative integer D as
17573 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17574 int quotient = d;
17575 int remainder = 0;
17576 /* -1 means: do not use TENTHS. */
17577 int tenths = -1;
17578 int exponent = 0;
17580 /* Length of QUOTIENT.TENTHS as a string. */
17581 int length;
17583 char * psuffix;
17584 char * p;
17586 if (1000 <= quotient)
17588 /* Scale to the appropriate EXPONENT. */
17591 remainder = quotient % 1000;
17592 quotient /= 1000;
17593 exponent++;
17595 while (1000 <= quotient);
17597 /* Round to nearest and decide whether to use TENTHS or not. */
17598 if (quotient <= 9)
17600 tenths = remainder / 100;
17601 if (50 <= remainder % 100)
17603 if (tenths < 9)
17604 tenths++;
17605 else
17607 quotient++;
17608 if (quotient == 10)
17609 tenths = -1;
17610 else
17611 tenths = 0;
17615 else
17616 if (500 <= remainder)
17618 if (quotient < 999)
17619 quotient++;
17620 else
17622 quotient = 1;
17623 exponent++;
17624 tenths = 0;
17629 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17630 if (tenths == -1 && quotient <= 99)
17631 if (quotient <= 9)
17632 length = 1;
17633 else
17634 length = 2;
17635 else
17636 length = 3;
17637 p = psuffix = buf + max (width, length);
17639 /* Print EXPONENT. */
17640 if (exponent)
17641 *psuffix++ = power_letter[exponent];
17642 *psuffix = '\0';
17644 /* Print TENTHS. */
17645 if (tenths >= 0)
17647 *--p = '0' + tenths;
17648 *--p = '.';
17651 /* Print QUOTIENT. */
17654 int digit = quotient % 10;
17655 *--p = '0' + digit;
17657 while ((quotient /= 10) != 0);
17659 /* Print leading spaces. */
17660 while (buf < p)
17661 *--p = ' ';
17664 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17665 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17666 type of CODING_SYSTEM. Return updated pointer into BUF. */
17668 static unsigned char invalid_eol_type[] = "(*invalid*)";
17670 static char *
17671 decode_mode_spec_coding (coding_system, buf, eol_flag)
17672 Lisp_Object coding_system;
17673 register char *buf;
17674 int eol_flag;
17676 Lisp_Object val;
17677 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17678 const unsigned char *eol_str;
17679 int eol_str_len;
17680 /* The EOL conversion we are using. */
17681 Lisp_Object eoltype;
17683 val = Fget (coding_system, Qcoding_system);
17684 eoltype = Qnil;
17686 if (!VECTORP (val)) /* Not yet decided. */
17688 if (multibyte)
17689 *buf++ = '-';
17690 if (eol_flag)
17691 eoltype = eol_mnemonic_undecided;
17692 /* Don't mention EOL conversion if it isn't decided. */
17694 else
17696 Lisp_Object eolvalue;
17698 eolvalue = Fget (coding_system, Qeol_type);
17700 if (multibyte)
17701 *buf++ = XFASTINT (AREF (val, 1));
17703 if (eol_flag)
17705 /* The EOL conversion that is normal on this system. */
17707 if (NILP (eolvalue)) /* Not yet decided. */
17708 eoltype = eol_mnemonic_undecided;
17709 else if (VECTORP (eolvalue)) /* Not yet decided. */
17710 eoltype = eol_mnemonic_undecided;
17711 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17712 eoltype = (XFASTINT (eolvalue) == 0
17713 ? eol_mnemonic_unix
17714 : (XFASTINT (eolvalue) == 1
17715 ? eol_mnemonic_dos : eol_mnemonic_mac));
17719 if (eol_flag)
17721 /* Mention the EOL conversion if it is not the usual one. */
17722 if (STRINGP (eoltype))
17724 eol_str = SDATA (eoltype);
17725 eol_str_len = SBYTES (eoltype);
17727 else if (INTEGERP (eoltype)
17728 && CHAR_VALID_P (XINT (eoltype), 0))
17730 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17731 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17732 eol_str = tmp;
17734 else
17736 eol_str = invalid_eol_type;
17737 eol_str_len = sizeof (invalid_eol_type) - 1;
17739 bcopy (eol_str, buf, eol_str_len);
17740 buf += eol_str_len;
17743 return buf;
17746 /* Return a string for the output of a mode line %-spec for window W,
17747 generated by character C. PRECISION >= 0 means don't return a
17748 string longer than that value. FIELD_WIDTH > 0 means pad the
17749 string returned with spaces to that value. Return 1 in *MULTIBYTE
17750 if the result is multibyte text.
17752 Note we operate on the current buffer for most purposes,
17753 the exception being w->base_line_pos. */
17755 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17757 static char *
17758 decode_mode_spec (w, c, field_width, precision, multibyte)
17759 struct window *w;
17760 register int c;
17761 int field_width, precision;
17762 int *multibyte;
17764 Lisp_Object obj;
17765 struct frame *f = XFRAME (WINDOW_FRAME (w));
17766 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17767 struct buffer *b = current_buffer;
17769 obj = Qnil;
17770 *multibyte = 0;
17772 switch (c)
17774 case '*':
17775 if (!NILP (b->read_only))
17776 return "%";
17777 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17778 return "*";
17779 return "-";
17781 case '+':
17782 /* This differs from %* only for a modified read-only buffer. */
17783 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17784 return "*";
17785 if (!NILP (b->read_only))
17786 return "%";
17787 return "-";
17789 case '&':
17790 /* This differs from %* in ignoring read-only-ness. */
17791 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17792 return "*";
17793 return "-";
17795 case '%':
17796 return "%";
17798 case '[':
17800 int i;
17801 char *p;
17803 if (command_loop_level > 5)
17804 return "[[[... ";
17805 p = decode_mode_spec_buf;
17806 for (i = 0; i < command_loop_level; i++)
17807 *p++ = '[';
17808 *p = 0;
17809 return decode_mode_spec_buf;
17812 case ']':
17814 int i;
17815 char *p;
17817 if (command_loop_level > 5)
17818 return " ...]]]";
17819 p = decode_mode_spec_buf;
17820 for (i = 0; i < command_loop_level; i++)
17821 *p++ = ']';
17822 *p = 0;
17823 return decode_mode_spec_buf;
17826 case '-':
17828 register int i;
17830 /* Let lots_of_dashes be a string of infinite length. */
17831 if (mode_line_target == MODE_LINE_NOPROP ||
17832 mode_line_target == MODE_LINE_STRING)
17833 return "--";
17834 if (field_width <= 0
17835 || field_width > sizeof (lots_of_dashes))
17837 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17838 decode_mode_spec_buf[i] = '-';
17839 decode_mode_spec_buf[i] = '\0';
17840 return decode_mode_spec_buf;
17842 else
17843 return lots_of_dashes;
17846 case 'b':
17847 obj = b->name;
17848 break;
17850 case 'c':
17851 /* %c and %l are ignored in `frame-title-format'.
17852 (In redisplay_internal, the frame title is drawn _before_ the
17853 windows are updated, so the stuff which depends on actual
17854 window contents (such as %l) may fail to render properly, or
17855 even crash emacs.) */
17856 if (mode_line_target == MODE_LINE_TITLE)
17857 return "";
17858 else
17860 int col = (int) current_column (); /* iftc */
17861 w->column_number_displayed = make_number (col);
17862 pint2str (decode_mode_spec_buf, field_width, col);
17863 return decode_mode_spec_buf;
17866 case 'e':
17867 #ifndef SYSTEM_MALLOC
17869 if (NILP (Vmemory_full))
17870 return "";
17871 else
17872 return "!MEM FULL! ";
17874 #else
17875 return "";
17876 #endif
17878 case 'F':
17879 /* %F displays the frame name. */
17880 if (!NILP (f->title))
17881 return (char *) SDATA (f->title);
17882 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17883 return (char *) SDATA (f->name);
17884 return "Emacs";
17886 case 'f':
17887 obj = b->filename;
17888 break;
17890 case 'i':
17892 int size = ZV - BEGV;
17893 pint2str (decode_mode_spec_buf, field_width, size);
17894 return decode_mode_spec_buf;
17897 case 'I':
17899 int size = ZV - BEGV;
17900 pint2hrstr (decode_mode_spec_buf, field_width, size);
17901 return decode_mode_spec_buf;
17904 case 'l':
17906 int startpos, startpos_byte, line, linepos, linepos_byte;
17907 int topline, nlines, junk, height;
17909 /* %c and %l are ignored in `frame-title-format'. */
17910 if (mode_line_target == MODE_LINE_TITLE)
17911 return "";
17913 startpos = XMARKER (w->start)->charpos;
17914 startpos_byte = marker_byte_position (w->start);
17915 height = WINDOW_TOTAL_LINES (w);
17917 /* If we decided that this buffer isn't suitable for line numbers,
17918 don't forget that too fast. */
17919 if (EQ (w->base_line_pos, w->buffer))
17920 goto no_value;
17921 /* But do forget it, if the window shows a different buffer now. */
17922 else if (BUFFERP (w->base_line_pos))
17923 w->base_line_pos = Qnil;
17925 /* If the buffer is very big, don't waste time. */
17926 if (INTEGERP (Vline_number_display_limit)
17927 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17929 w->base_line_pos = Qnil;
17930 w->base_line_number = Qnil;
17931 goto no_value;
17934 if (!NILP (w->base_line_number)
17935 && !NILP (w->base_line_pos)
17936 && XFASTINT (w->base_line_pos) <= startpos)
17938 line = XFASTINT (w->base_line_number);
17939 linepos = XFASTINT (w->base_line_pos);
17940 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17942 else
17944 line = 1;
17945 linepos = BUF_BEGV (b);
17946 linepos_byte = BUF_BEGV_BYTE (b);
17949 /* Count lines from base line to window start position. */
17950 nlines = display_count_lines (linepos, linepos_byte,
17951 startpos_byte,
17952 startpos, &junk);
17954 topline = nlines + line;
17956 /* Determine a new base line, if the old one is too close
17957 or too far away, or if we did not have one.
17958 "Too close" means it's plausible a scroll-down would
17959 go back past it. */
17960 if (startpos == BUF_BEGV (b))
17962 w->base_line_number = make_number (topline);
17963 w->base_line_pos = make_number (BUF_BEGV (b));
17965 else if (nlines < height + 25 || nlines > height * 3 + 50
17966 || linepos == BUF_BEGV (b))
17968 int limit = BUF_BEGV (b);
17969 int limit_byte = BUF_BEGV_BYTE (b);
17970 int position;
17971 int distance = (height * 2 + 30) * line_number_display_limit_width;
17973 if (startpos - distance > limit)
17975 limit = startpos - distance;
17976 limit_byte = CHAR_TO_BYTE (limit);
17979 nlines = display_count_lines (startpos, startpos_byte,
17980 limit_byte,
17981 - (height * 2 + 30),
17982 &position);
17983 /* If we couldn't find the lines we wanted within
17984 line_number_display_limit_width chars per line,
17985 give up on line numbers for this window. */
17986 if (position == limit_byte && limit == startpos - distance)
17988 w->base_line_pos = w->buffer;
17989 w->base_line_number = Qnil;
17990 goto no_value;
17993 w->base_line_number = make_number (topline - nlines);
17994 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17997 /* Now count lines from the start pos to point. */
17998 nlines = display_count_lines (startpos, startpos_byte,
17999 PT_BYTE, PT, &junk);
18001 /* Record that we did display the line number. */
18002 line_number_displayed = 1;
18004 /* Make the string to show. */
18005 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18006 return decode_mode_spec_buf;
18007 no_value:
18009 char* p = decode_mode_spec_buf;
18010 int pad = field_width - 2;
18011 while (pad-- > 0)
18012 *p++ = ' ';
18013 *p++ = '?';
18014 *p++ = '?';
18015 *p = '\0';
18016 return decode_mode_spec_buf;
18019 break;
18021 case 'm':
18022 obj = b->mode_name;
18023 break;
18025 case 'n':
18026 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18027 return " Narrow";
18028 break;
18030 case 'p':
18032 int pos = marker_position (w->start);
18033 int total = BUF_ZV (b) - BUF_BEGV (b);
18035 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18037 if (pos <= BUF_BEGV (b))
18038 return "All";
18039 else
18040 return "Bottom";
18042 else if (pos <= BUF_BEGV (b))
18043 return "Top";
18044 else
18046 if (total > 1000000)
18047 /* Do it differently for a large value, to avoid overflow. */
18048 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18049 else
18050 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18051 /* We can't normally display a 3-digit number,
18052 so get us a 2-digit number that is close. */
18053 if (total == 100)
18054 total = 99;
18055 sprintf (decode_mode_spec_buf, "%2d%%", total);
18056 return decode_mode_spec_buf;
18060 /* Display percentage of size above the bottom of the screen. */
18061 case 'P':
18063 int toppos = marker_position (w->start);
18064 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18065 int total = BUF_ZV (b) - BUF_BEGV (b);
18067 if (botpos >= BUF_ZV (b))
18069 if (toppos <= BUF_BEGV (b))
18070 return "All";
18071 else
18072 return "Bottom";
18074 else
18076 if (total > 1000000)
18077 /* Do it differently for a large value, to avoid overflow. */
18078 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18079 else
18080 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18081 /* We can't normally display a 3-digit number,
18082 so get us a 2-digit number that is close. */
18083 if (total == 100)
18084 total = 99;
18085 if (toppos <= BUF_BEGV (b))
18086 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18087 else
18088 sprintf (decode_mode_spec_buf, "%2d%%", total);
18089 return decode_mode_spec_buf;
18093 case 's':
18094 /* status of process */
18095 obj = Fget_buffer_process (Fcurrent_buffer ());
18096 if (NILP (obj))
18097 return "no process";
18098 #ifdef subprocesses
18099 obj = Fsymbol_name (Fprocess_status (obj));
18100 #endif
18101 break;
18103 case '@':
18105 Lisp_Object val;
18106 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18107 if (NILP (val))
18108 return "-";
18109 else
18110 return "@";
18113 case 't': /* indicate TEXT or BINARY */
18114 #ifdef MODE_LINE_BINARY_TEXT
18115 return MODE_LINE_BINARY_TEXT (b);
18116 #else
18117 return "T";
18118 #endif
18120 case 'z':
18121 /* coding-system (not including end-of-line format) */
18122 case 'Z':
18123 /* coding-system (including end-of-line type) */
18125 int eol_flag = (c == 'Z');
18126 char *p = decode_mode_spec_buf;
18128 if (! FRAME_WINDOW_P (f))
18130 /* No need to mention EOL here--the terminal never needs
18131 to do EOL conversion. */
18132 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
18133 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
18135 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18136 p, eol_flag);
18138 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18139 #ifdef subprocesses
18140 obj = Fget_buffer_process (Fcurrent_buffer ());
18141 if (PROCESSP (obj))
18143 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18144 p, eol_flag);
18145 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18146 p, eol_flag);
18148 #endif /* subprocesses */
18149 #endif /* 0 */
18150 *p = 0;
18151 return decode_mode_spec_buf;
18155 if (STRINGP (obj))
18157 *multibyte = STRING_MULTIBYTE (obj);
18158 return (char *) SDATA (obj);
18160 else
18161 return "";
18165 /* Count up to COUNT lines starting from START / START_BYTE.
18166 But don't go beyond LIMIT_BYTE.
18167 Return the number of lines thus found (always nonnegative).
18169 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18171 static int
18172 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18173 int start, start_byte, limit_byte, count;
18174 int *byte_pos_ptr;
18176 register unsigned char *cursor;
18177 unsigned char *base;
18179 register int ceiling;
18180 register unsigned char *ceiling_addr;
18181 int orig_count = count;
18183 /* If we are not in selective display mode,
18184 check only for newlines. */
18185 int selective_display = (!NILP (current_buffer->selective_display)
18186 && !INTEGERP (current_buffer->selective_display));
18188 if (count > 0)
18190 while (start_byte < limit_byte)
18192 ceiling = BUFFER_CEILING_OF (start_byte);
18193 ceiling = min (limit_byte - 1, ceiling);
18194 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18195 base = (cursor = BYTE_POS_ADDR (start_byte));
18196 while (1)
18198 if (selective_display)
18199 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18201 else
18202 while (*cursor != '\n' && ++cursor != ceiling_addr)
18205 if (cursor != ceiling_addr)
18207 if (--count == 0)
18209 start_byte += cursor - base + 1;
18210 *byte_pos_ptr = start_byte;
18211 return orig_count;
18213 else
18214 if (++cursor == ceiling_addr)
18215 break;
18217 else
18218 break;
18220 start_byte += cursor - base;
18223 else
18225 while (start_byte > limit_byte)
18227 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18228 ceiling = max (limit_byte, ceiling);
18229 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18230 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18231 while (1)
18233 if (selective_display)
18234 while (--cursor != ceiling_addr
18235 && *cursor != '\n' && *cursor != 015)
18237 else
18238 while (--cursor != ceiling_addr && *cursor != '\n')
18241 if (cursor != ceiling_addr)
18243 if (++count == 0)
18245 start_byte += cursor - base + 1;
18246 *byte_pos_ptr = start_byte;
18247 /* When scanning backwards, we should
18248 not count the newline posterior to which we stop. */
18249 return - orig_count - 1;
18252 else
18253 break;
18255 /* Here we add 1 to compensate for the last decrement
18256 of CURSOR, which took it past the valid range. */
18257 start_byte += cursor - base + 1;
18261 *byte_pos_ptr = limit_byte;
18263 if (count < 0)
18264 return - orig_count + count;
18265 return orig_count - count;
18271 /***********************************************************************
18272 Displaying strings
18273 ***********************************************************************/
18275 /* Display a NUL-terminated string, starting with index START.
18277 If STRING is non-null, display that C string. Otherwise, the Lisp
18278 string LISP_STRING is displayed.
18280 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18281 FACE_STRING. Display STRING or LISP_STRING with the face at
18282 FACE_STRING_POS in FACE_STRING:
18284 Display the string in the environment given by IT, but use the
18285 standard display table, temporarily.
18287 FIELD_WIDTH is the minimum number of output glyphs to produce.
18288 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18289 with spaces. If STRING has more characters, more than FIELD_WIDTH
18290 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18292 PRECISION is the maximum number of characters to output from
18293 STRING. PRECISION < 0 means don't truncate the string.
18295 This is roughly equivalent to printf format specifiers:
18297 FIELD_WIDTH PRECISION PRINTF
18298 ----------------------------------------
18299 -1 -1 %s
18300 -1 10 %.10s
18301 10 -1 %10s
18302 20 10 %20.10s
18304 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18305 display them, and < 0 means obey the current buffer's value of
18306 enable_multibyte_characters.
18308 Value is the number of columns displayed. */
18310 static int
18311 display_string (string, lisp_string, face_string, face_string_pos,
18312 start, it, field_width, precision, max_x, multibyte)
18313 unsigned char *string;
18314 Lisp_Object lisp_string;
18315 Lisp_Object face_string;
18316 int face_string_pos;
18317 int start;
18318 struct it *it;
18319 int field_width, precision, max_x;
18320 int multibyte;
18322 int hpos_at_start = it->hpos;
18323 int saved_face_id = it->face_id;
18324 struct glyph_row *row = it->glyph_row;
18326 /* Initialize the iterator IT for iteration over STRING beginning
18327 with index START. */
18328 reseat_to_string (it, string, lisp_string, start,
18329 precision, field_width, multibyte);
18331 /* If displaying STRING, set up the face of the iterator
18332 from LISP_STRING, if that's given. */
18333 if (STRINGP (face_string))
18335 int endptr;
18336 struct face *face;
18338 it->face_id
18339 = face_at_string_position (it->w, face_string, face_string_pos,
18340 0, it->region_beg_charpos,
18341 it->region_end_charpos,
18342 &endptr, it->base_face_id, 0);
18343 face = FACE_FROM_ID (it->f, it->face_id);
18344 it->face_box_p = face->box != FACE_NO_BOX;
18347 /* Set max_x to the maximum allowed X position. Don't let it go
18348 beyond the right edge of the window. */
18349 if (max_x <= 0)
18350 max_x = it->last_visible_x;
18351 else
18352 max_x = min (max_x, it->last_visible_x);
18354 /* Skip over display elements that are not visible. because IT->w is
18355 hscrolled. */
18356 if (it->current_x < it->first_visible_x)
18357 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18358 MOVE_TO_POS | MOVE_TO_X);
18360 row->ascent = it->max_ascent;
18361 row->height = it->max_ascent + it->max_descent;
18362 row->phys_ascent = it->max_phys_ascent;
18363 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18364 row->extra_line_spacing = it->max_extra_line_spacing;
18366 /* This condition is for the case that we are called with current_x
18367 past last_visible_x. */
18368 while (it->current_x < max_x)
18370 int x_before, x, n_glyphs_before, i, nglyphs;
18372 /* Get the next display element. */
18373 if (!get_next_display_element (it))
18374 break;
18376 /* Produce glyphs. */
18377 x_before = it->current_x;
18378 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18379 PRODUCE_GLYPHS (it);
18381 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18382 i = 0;
18383 x = x_before;
18384 while (i < nglyphs)
18386 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18388 if (!it->truncate_lines_p
18389 && x + glyph->pixel_width > max_x)
18391 /* End of continued line or max_x reached. */
18392 if (CHAR_GLYPH_PADDING_P (*glyph))
18394 /* A wide character is unbreakable. */
18395 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18396 it->current_x = x_before;
18398 else
18400 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18401 it->current_x = x;
18403 break;
18405 else if (x + glyph->pixel_width > it->first_visible_x)
18407 /* Glyph is at least partially visible. */
18408 ++it->hpos;
18409 if (x < it->first_visible_x)
18410 it->glyph_row->x = x - it->first_visible_x;
18412 else
18414 /* Glyph is off the left margin of the display area.
18415 Should not happen. */
18416 abort ();
18419 row->ascent = max (row->ascent, it->max_ascent);
18420 row->height = max (row->height, it->max_ascent + it->max_descent);
18421 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18422 row->phys_height = max (row->phys_height,
18423 it->max_phys_ascent + it->max_phys_descent);
18424 row->extra_line_spacing = max (row->extra_line_spacing,
18425 it->max_extra_line_spacing);
18426 x += glyph->pixel_width;
18427 ++i;
18430 /* Stop if max_x reached. */
18431 if (i < nglyphs)
18432 break;
18434 /* Stop at line ends. */
18435 if (ITERATOR_AT_END_OF_LINE_P (it))
18437 it->continuation_lines_width = 0;
18438 break;
18441 set_iterator_to_next (it, 1);
18443 /* Stop if truncating at the right edge. */
18444 if (it->truncate_lines_p
18445 && it->current_x >= it->last_visible_x)
18447 /* Add truncation mark, but don't do it if the line is
18448 truncated at a padding space. */
18449 if (IT_CHARPOS (*it) < it->string_nchars)
18451 if (!FRAME_WINDOW_P (it->f))
18453 int i, n;
18455 if (it->current_x > it->last_visible_x)
18457 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18458 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18459 break;
18460 for (n = row->used[TEXT_AREA]; i < n; ++i)
18462 row->used[TEXT_AREA] = i;
18463 produce_special_glyphs (it, IT_TRUNCATION);
18466 produce_special_glyphs (it, IT_TRUNCATION);
18468 it->glyph_row->truncated_on_right_p = 1;
18470 break;
18474 /* Maybe insert a truncation at the left. */
18475 if (it->first_visible_x
18476 && IT_CHARPOS (*it) > 0)
18478 if (!FRAME_WINDOW_P (it->f))
18479 insert_left_trunc_glyphs (it);
18480 it->glyph_row->truncated_on_left_p = 1;
18483 it->face_id = saved_face_id;
18485 /* Value is number of columns displayed. */
18486 return it->hpos - hpos_at_start;
18491 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18492 appears as an element of LIST or as the car of an element of LIST.
18493 If PROPVAL is a list, compare each element against LIST in that
18494 way, and return 1/2 if any element of PROPVAL is found in LIST.
18495 Otherwise return 0. This function cannot quit.
18496 The return value is 2 if the text is invisible but with an ellipsis
18497 and 1 if it's invisible and without an ellipsis. */
18500 invisible_p (propval, list)
18501 register Lisp_Object propval;
18502 Lisp_Object list;
18504 register Lisp_Object tail, proptail;
18506 for (tail = list; CONSP (tail); tail = XCDR (tail))
18508 register Lisp_Object tem;
18509 tem = XCAR (tail);
18510 if (EQ (propval, tem))
18511 return 1;
18512 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18513 return NILP (XCDR (tem)) ? 1 : 2;
18516 if (CONSP (propval))
18518 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18520 Lisp_Object propelt;
18521 propelt = XCAR (proptail);
18522 for (tail = list; CONSP (tail); tail = XCDR (tail))
18524 register Lisp_Object tem;
18525 tem = XCAR (tail);
18526 if (EQ (propelt, tem))
18527 return 1;
18528 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18529 return NILP (XCDR (tem)) ? 1 : 2;
18534 return 0;
18537 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18538 doc: /* Non-nil if the property makes the text invisible.
18539 POS-OR-PROP can be a marker or number, in which case it is taken to be
18540 a position in the current buffer and the value of the `invisible' property
18541 is checked; or it can be some other value, which is then presumed to be the
18542 value of the `invisible' property of the text of interest.
18543 The non-nil value returned can be t for truly invisible text or something
18544 else if the text is replaced by an ellipsis. */)
18545 (pos_or_prop)
18546 Lisp_Object pos_or_prop;
18548 Lisp_Object prop
18549 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18550 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18551 : pos_or_prop);
18552 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18553 return (invis == 0 ? Qnil
18554 : invis == 1 ? Qt
18555 : make_number (invis));
18558 /* Calculate a width or height in pixels from a specification using
18559 the following elements:
18561 SPEC ::=
18562 NUM - a (fractional) multiple of the default font width/height
18563 (NUM) - specifies exactly NUM pixels
18564 UNIT - a fixed number of pixels, see below.
18565 ELEMENT - size of a display element in pixels, see below.
18566 (NUM . SPEC) - equals NUM * SPEC
18567 (+ SPEC SPEC ...) - add pixel values
18568 (- SPEC SPEC ...) - subtract pixel values
18569 (- SPEC) - negate pixel value
18571 NUM ::=
18572 INT or FLOAT - a number constant
18573 SYMBOL - use symbol's (buffer local) variable binding.
18575 UNIT ::=
18576 in - pixels per inch *)
18577 mm - pixels per 1/1000 meter *)
18578 cm - pixels per 1/100 meter *)
18579 width - width of current font in pixels.
18580 height - height of current font in pixels.
18582 *) using the ratio(s) defined in display-pixels-per-inch.
18584 ELEMENT ::=
18586 left-fringe - left fringe width in pixels
18587 right-fringe - right fringe width in pixels
18589 left-margin - left margin width in pixels
18590 right-margin - right margin width in pixels
18592 scroll-bar - scroll-bar area width in pixels
18594 Examples:
18596 Pixels corresponding to 5 inches:
18597 (5 . in)
18599 Total width of non-text areas on left side of window (if scroll-bar is on left):
18600 '(space :width (+ left-fringe left-margin scroll-bar))
18602 Align to first text column (in header line):
18603 '(space :align-to 0)
18605 Align to middle of text area minus half the width of variable `my-image'
18606 containing a loaded image:
18607 '(space :align-to (0.5 . (- text my-image)))
18609 Width of left margin minus width of 1 character in the default font:
18610 '(space :width (- left-margin 1))
18612 Width of left margin minus width of 2 characters in the current font:
18613 '(space :width (- left-margin (2 . width)))
18615 Center 1 character over left-margin (in header line):
18616 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18618 Different ways to express width of left fringe plus left margin minus one pixel:
18619 '(space :width (- (+ left-fringe left-margin) (1)))
18620 '(space :width (+ left-fringe left-margin (- (1))))
18621 '(space :width (+ left-fringe left-margin (-1)))
18625 #define NUMVAL(X) \
18626 ((INTEGERP (X) || FLOATP (X)) \
18627 ? XFLOATINT (X) \
18628 : - 1)
18631 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18632 double *res;
18633 struct it *it;
18634 Lisp_Object prop;
18635 void *font;
18636 int width_p, *align_to;
18638 double pixels;
18640 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18641 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18643 if (NILP (prop))
18644 return OK_PIXELS (0);
18646 xassert (FRAME_LIVE_P (it->f));
18648 if (SYMBOLP (prop))
18650 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18652 char *unit = SDATA (SYMBOL_NAME (prop));
18654 if (unit[0] == 'i' && unit[1] == 'n')
18655 pixels = 1.0;
18656 else if (unit[0] == 'm' && unit[1] == 'm')
18657 pixels = 25.4;
18658 else if (unit[0] == 'c' && unit[1] == 'm')
18659 pixels = 2.54;
18660 else
18661 pixels = 0;
18662 if (pixels > 0)
18664 double ppi;
18665 #ifdef HAVE_WINDOW_SYSTEM
18666 if (FRAME_WINDOW_P (it->f)
18667 && (ppi = (width_p
18668 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18669 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18670 ppi > 0))
18671 return OK_PIXELS (ppi / pixels);
18672 #endif
18674 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18675 || (CONSP (Vdisplay_pixels_per_inch)
18676 && (ppi = (width_p
18677 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18678 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18679 ppi > 0)))
18680 return OK_PIXELS (ppi / pixels);
18682 return 0;
18686 #ifdef HAVE_WINDOW_SYSTEM
18687 if (EQ (prop, Qheight))
18688 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18689 if (EQ (prop, Qwidth))
18690 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18691 #else
18692 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18693 return OK_PIXELS (1);
18694 #endif
18696 if (EQ (prop, Qtext))
18697 return OK_PIXELS (width_p
18698 ? window_box_width (it->w, TEXT_AREA)
18699 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18701 if (align_to && *align_to < 0)
18703 *res = 0;
18704 if (EQ (prop, Qleft))
18705 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18706 if (EQ (prop, Qright))
18707 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18708 if (EQ (prop, Qcenter))
18709 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18710 + window_box_width (it->w, TEXT_AREA) / 2);
18711 if (EQ (prop, Qleft_fringe))
18712 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18713 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18714 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18715 if (EQ (prop, Qright_fringe))
18716 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18717 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18718 : window_box_right_offset (it->w, TEXT_AREA));
18719 if (EQ (prop, Qleft_margin))
18720 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18721 if (EQ (prop, Qright_margin))
18722 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18723 if (EQ (prop, Qscroll_bar))
18724 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18726 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18727 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18728 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18729 : 0)));
18731 else
18733 if (EQ (prop, Qleft_fringe))
18734 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18735 if (EQ (prop, Qright_fringe))
18736 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18737 if (EQ (prop, Qleft_margin))
18738 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18739 if (EQ (prop, Qright_margin))
18740 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18741 if (EQ (prop, Qscroll_bar))
18742 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18745 prop = Fbuffer_local_value (prop, it->w->buffer);
18748 if (INTEGERP (prop) || FLOATP (prop))
18750 int base_unit = (width_p
18751 ? FRAME_COLUMN_WIDTH (it->f)
18752 : FRAME_LINE_HEIGHT (it->f));
18753 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18756 if (CONSP (prop))
18758 Lisp_Object car = XCAR (prop);
18759 Lisp_Object cdr = XCDR (prop);
18761 if (SYMBOLP (car))
18763 #ifdef HAVE_WINDOW_SYSTEM
18764 if (FRAME_WINDOW_P (it->f)
18765 && valid_image_p (prop))
18767 int id = lookup_image (it->f, prop);
18768 struct image *img = IMAGE_FROM_ID (it->f, id);
18770 return OK_PIXELS (width_p ? img->width : img->height);
18772 #endif
18773 if (EQ (car, Qplus) || EQ (car, Qminus))
18775 int first = 1;
18776 double px;
18778 pixels = 0;
18779 while (CONSP (cdr))
18781 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18782 font, width_p, align_to))
18783 return 0;
18784 if (first)
18785 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18786 else
18787 pixels += px;
18788 cdr = XCDR (cdr);
18790 if (EQ (car, Qminus))
18791 pixels = -pixels;
18792 return OK_PIXELS (pixels);
18795 car = Fbuffer_local_value (car, it->w->buffer);
18798 if (INTEGERP (car) || FLOATP (car))
18800 double fact;
18801 pixels = XFLOATINT (car);
18802 if (NILP (cdr))
18803 return OK_PIXELS (pixels);
18804 if (calc_pixel_width_or_height (&fact, it, cdr,
18805 font, width_p, align_to))
18806 return OK_PIXELS (pixels * fact);
18807 return 0;
18810 return 0;
18813 return 0;
18817 /***********************************************************************
18818 Glyph Display
18819 ***********************************************************************/
18821 #ifdef HAVE_WINDOW_SYSTEM
18823 #if GLYPH_DEBUG
18825 void
18826 dump_glyph_string (s)
18827 struct glyph_string *s;
18829 fprintf (stderr, "glyph string\n");
18830 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18831 s->x, s->y, s->width, s->height);
18832 fprintf (stderr, " ybase = %d\n", s->ybase);
18833 fprintf (stderr, " hl = %d\n", s->hl);
18834 fprintf (stderr, " left overhang = %d, right = %d\n",
18835 s->left_overhang, s->right_overhang);
18836 fprintf (stderr, " nchars = %d\n", s->nchars);
18837 fprintf (stderr, " extends to end of line = %d\n",
18838 s->extends_to_end_of_line_p);
18839 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18840 fprintf (stderr, " bg width = %d\n", s->background_width);
18843 #endif /* GLYPH_DEBUG */
18845 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18846 of XChar2b structures for S; it can't be allocated in
18847 init_glyph_string because it must be allocated via `alloca'. W
18848 is the window on which S is drawn. ROW and AREA are the glyph row
18849 and area within the row from which S is constructed. START is the
18850 index of the first glyph structure covered by S. HL is a
18851 face-override for drawing S. */
18853 #ifdef HAVE_NTGUI
18854 #define OPTIONAL_HDC(hdc) hdc,
18855 #define DECLARE_HDC(hdc) HDC hdc;
18856 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18857 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18858 #endif
18860 #ifndef OPTIONAL_HDC
18861 #define OPTIONAL_HDC(hdc)
18862 #define DECLARE_HDC(hdc)
18863 #define ALLOCATE_HDC(hdc, f)
18864 #define RELEASE_HDC(hdc, f)
18865 #endif
18867 static void
18868 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18869 struct glyph_string *s;
18870 DECLARE_HDC (hdc)
18871 XChar2b *char2b;
18872 struct window *w;
18873 struct glyph_row *row;
18874 enum glyph_row_area area;
18875 int start;
18876 enum draw_glyphs_face hl;
18878 bzero (s, sizeof *s);
18879 s->w = w;
18880 s->f = XFRAME (w->frame);
18881 #ifdef HAVE_NTGUI
18882 s->hdc = hdc;
18883 #endif
18884 s->display = FRAME_X_DISPLAY (s->f);
18885 s->window = FRAME_X_WINDOW (s->f);
18886 s->char2b = char2b;
18887 s->hl = hl;
18888 s->row = row;
18889 s->area = area;
18890 s->first_glyph = row->glyphs[area] + start;
18891 s->height = row->height;
18892 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18894 /* Display the internal border below the tool-bar window. */
18895 if (WINDOWP (s->f->tool_bar_window)
18896 && s->w == XWINDOW (s->f->tool_bar_window))
18897 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18899 s->ybase = s->y + row->ascent;
18903 /* Append the list of glyph strings with head H and tail T to the list
18904 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18906 static INLINE void
18907 append_glyph_string_lists (head, tail, h, t)
18908 struct glyph_string **head, **tail;
18909 struct glyph_string *h, *t;
18911 if (h)
18913 if (*head)
18914 (*tail)->next = h;
18915 else
18916 *head = h;
18917 h->prev = *tail;
18918 *tail = t;
18923 /* Prepend the list of glyph strings with head H and tail T to the
18924 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18925 result. */
18927 static INLINE void
18928 prepend_glyph_string_lists (head, tail, h, t)
18929 struct glyph_string **head, **tail;
18930 struct glyph_string *h, *t;
18932 if (h)
18934 if (*head)
18935 (*head)->prev = t;
18936 else
18937 *tail = t;
18938 t->next = *head;
18939 *head = h;
18944 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18945 Set *HEAD and *TAIL to the resulting list. */
18947 static INLINE void
18948 append_glyph_string (head, tail, s)
18949 struct glyph_string **head, **tail;
18950 struct glyph_string *s;
18952 s->next = s->prev = NULL;
18953 append_glyph_string_lists (head, tail, s, s);
18957 /* Get face and two-byte form of character glyph GLYPH on frame F.
18958 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18959 a pointer to a realized face that is ready for display. */
18961 static INLINE struct face *
18962 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18963 struct frame *f;
18964 struct glyph *glyph;
18965 XChar2b *char2b;
18966 int *two_byte_p;
18968 struct face *face;
18970 xassert (glyph->type == CHAR_GLYPH);
18971 face = FACE_FROM_ID (f, glyph->face_id);
18973 if (two_byte_p)
18974 *two_byte_p = 0;
18976 if (!glyph->multibyte_p)
18978 /* Unibyte case. We don't have to encode, but we have to make
18979 sure to use a face suitable for unibyte. */
18980 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18982 else if (glyph->u.ch < 128)
18984 /* Case of ASCII in a face known to fit ASCII. */
18985 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18987 else
18989 int c1, c2, charset;
18991 /* Split characters into bytes. If c2 is -1 afterwards, C is
18992 really a one-byte character so that byte1 is zero. */
18993 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18994 if (c2 > 0)
18995 STORE_XCHAR2B (char2b, c1, c2);
18996 else
18997 STORE_XCHAR2B (char2b, 0, c1);
18999 /* Maybe encode the character in *CHAR2B. */
19000 if (charset != CHARSET_ASCII)
19002 struct font_info *font_info
19003 = FONT_INFO_FROM_ID (f, face->font_info_id);
19004 if (font_info)
19005 glyph->font_type
19006 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
19010 /* Make sure X resources of the face are allocated. */
19011 xassert (face != NULL);
19012 PREPARE_FACE_FOR_DISPLAY (f, face);
19013 return face;
19017 /* Fill glyph string S with composition components specified by S->cmp.
19019 FACES is an array of faces for all components of this composition.
19020 S->gidx is the index of the first component for S.
19022 OVERLAPS non-zero means S should draw the foreground only, and use
19023 its physical height for clipping. See also draw_glyphs.
19025 Value is the index of a component not in S. */
19027 static int
19028 fill_composite_glyph_string (s, faces, overlaps)
19029 struct glyph_string *s;
19030 struct face **faces;
19031 int overlaps;
19033 int i;
19035 xassert (s);
19037 s->for_overlaps = overlaps;
19039 s->face = faces[s->gidx];
19040 s->font = s->face->font;
19041 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19043 /* For all glyphs of this composition, starting at the offset
19044 S->gidx, until we reach the end of the definition or encounter a
19045 glyph that requires the different face, add it to S. */
19046 ++s->nchars;
19047 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
19048 ++s->nchars;
19050 /* All glyph strings for the same composition has the same width,
19051 i.e. the width set for the first component of the composition. */
19053 s->width = s->first_glyph->pixel_width;
19055 /* If the specified font could not be loaded, use the frame's
19056 default font, but record the fact that we couldn't load it in
19057 the glyph string so that we can draw rectangles for the
19058 characters of the glyph string. */
19059 if (s->font == NULL)
19061 s->font_not_found_p = 1;
19062 s->font = FRAME_FONT (s->f);
19065 /* Adjust base line for subscript/superscript text. */
19066 s->ybase += s->first_glyph->voffset;
19068 xassert (s->face && s->face->gc);
19070 /* This glyph string must always be drawn with 16-bit functions. */
19071 s->two_byte_p = 1;
19073 return s->gidx + s->nchars;
19077 /* Fill glyph string S from a sequence of character glyphs.
19079 FACE_ID is the face id of the string. START is the index of the
19080 first glyph to consider, END is the index of the last + 1.
19081 OVERLAPS non-zero means S should draw the foreground only, and use
19082 its physical height for clipping. See also draw_glyphs.
19084 Value is the index of the first glyph not in S. */
19086 static int
19087 fill_glyph_string (s, face_id, start, end, overlaps)
19088 struct glyph_string *s;
19089 int face_id;
19090 int start, end, overlaps;
19092 struct glyph *glyph, *last;
19093 int voffset;
19094 int glyph_not_available_p;
19096 xassert (s->f == XFRAME (s->w->frame));
19097 xassert (s->nchars == 0);
19098 xassert (start >= 0 && end > start);
19100 s->for_overlaps = overlaps,
19101 glyph = s->row->glyphs[s->area] + start;
19102 last = s->row->glyphs[s->area] + end;
19103 voffset = glyph->voffset;
19105 glyph_not_available_p = glyph->glyph_not_available_p;
19107 while (glyph < last
19108 && glyph->type == CHAR_GLYPH
19109 && glyph->voffset == voffset
19110 /* Same face id implies same font, nowadays. */
19111 && glyph->face_id == face_id
19112 && glyph->glyph_not_available_p == glyph_not_available_p)
19114 int two_byte_p;
19116 s->face = get_glyph_face_and_encoding (s->f, glyph,
19117 s->char2b + s->nchars,
19118 &two_byte_p);
19119 s->two_byte_p = two_byte_p;
19120 ++s->nchars;
19121 xassert (s->nchars <= end - start);
19122 s->width += glyph->pixel_width;
19123 ++glyph;
19126 s->font = s->face->font;
19127 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19129 /* If the specified font could not be loaded, use the frame's font,
19130 but record the fact that we couldn't load it in
19131 S->font_not_found_p so that we can draw rectangles for the
19132 characters of the glyph string. */
19133 if (s->font == NULL || glyph_not_available_p)
19135 s->font_not_found_p = 1;
19136 s->font = FRAME_FONT (s->f);
19139 /* Adjust base line for subscript/superscript text. */
19140 s->ybase += voffset;
19142 xassert (s->face && s->face->gc);
19143 return glyph - s->row->glyphs[s->area];
19147 /* Fill glyph string S from image glyph S->first_glyph. */
19149 static void
19150 fill_image_glyph_string (s)
19151 struct glyph_string *s;
19153 xassert (s->first_glyph->type == IMAGE_GLYPH);
19154 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19155 xassert (s->img);
19156 s->slice = s->first_glyph->slice;
19157 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19158 s->font = s->face->font;
19159 s->width = s->first_glyph->pixel_width;
19161 /* Adjust base line for subscript/superscript text. */
19162 s->ybase += s->first_glyph->voffset;
19166 /* Fill glyph string S from a sequence of stretch glyphs.
19168 ROW is the glyph row in which the glyphs are found, AREA is the
19169 area within the row. START is the index of the first glyph to
19170 consider, END is the index of the last + 1.
19172 Value is the index of the first glyph not in S. */
19174 static int
19175 fill_stretch_glyph_string (s, row, area, start, end)
19176 struct glyph_string *s;
19177 struct glyph_row *row;
19178 enum glyph_row_area area;
19179 int start, end;
19181 struct glyph *glyph, *last;
19182 int voffset, face_id;
19184 xassert (s->first_glyph->type == STRETCH_GLYPH);
19186 glyph = s->row->glyphs[s->area] + start;
19187 last = s->row->glyphs[s->area] + end;
19188 face_id = glyph->face_id;
19189 s->face = FACE_FROM_ID (s->f, face_id);
19190 s->font = s->face->font;
19191 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19192 s->width = glyph->pixel_width;
19193 s->nchars = 1;
19194 voffset = glyph->voffset;
19196 for (++glyph;
19197 (glyph < last
19198 && glyph->type == STRETCH_GLYPH
19199 && glyph->voffset == voffset
19200 && glyph->face_id == face_id);
19201 ++glyph)
19202 s->width += glyph->pixel_width;
19204 /* Adjust base line for subscript/superscript text. */
19205 s->ybase += voffset;
19207 /* The case that face->gc == 0 is handled when drawing the glyph
19208 string by calling PREPARE_FACE_FOR_DISPLAY. */
19209 xassert (s->face);
19210 return glyph - s->row->glyphs[s->area];
19214 /* EXPORT for RIF:
19215 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19216 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19217 assumed to be zero. */
19219 void
19220 x_get_glyph_overhangs (glyph, f, left, right)
19221 struct glyph *glyph;
19222 struct frame *f;
19223 int *left, *right;
19225 *left = *right = 0;
19227 if (glyph->type == CHAR_GLYPH)
19229 XFontStruct *font;
19230 struct face *face;
19231 struct font_info *font_info;
19232 XChar2b char2b;
19233 XCharStruct *pcm;
19235 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19236 font = face->font;
19237 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19238 if (font /* ++KFS: Should this be font_info ? */
19239 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
19241 if (pcm->rbearing > pcm->width)
19242 *right = pcm->rbearing - pcm->width;
19243 if (pcm->lbearing < 0)
19244 *left = -pcm->lbearing;
19250 /* Return the index of the first glyph preceding glyph string S that
19251 is overwritten by S because of S's left overhang. Value is -1
19252 if no glyphs are overwritten. */
19254 static int
19255 left_overwritten (s)
19256 struct glyph_string *s;
19258 int k;
19260 if (s->left_overhang)
19262 int x = 0, i;
19263 struct glyph *glyphs = s->row->glyphs[s->area];
19264 int first = s->first_glyph - glyphs;
19266 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19267 x -= glyphs[i].pixel_width;
19269 k = i + 1;
19271 else
19272 k = -1;
19274 return k;
19278 /* Return the index of the first glyph preceding glyph string S that
19279 is overwriting S because of its right overhang. Value is -1 if no
19280 glyph in front of S overwrites S. */
19282 static int
19283 left_overwriting (s)
19284 struct glyph_string *s;
19286 int i, k, x;
19287 struct glyph *glyphs = s->row->glyphs[s->area];
19288 int first = s->first_glyph - glyphs;
19290 k = -1;
19291 x = 0;
19292 for (i = first - 1; i >= 0; --i)
19294 int left, right;
19295 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19296 if (x + right > 0)
19297 k = i;
19298 x -= glyphs[i].pixel_width;
19301 return k;
19305 /* Return the index of the last glyph following glyph string S that is
19306 not overwritten by S because of S's right overhang. Value is -1 if
19307 no such glyph is found. */
19309 static int
19310 right_overwritten (s)
19311 struct glyph_string *s;
19313 int k = -1;
19315 if (s->right_overhang)
19317 int x = 0, i;
19318 struct glyph *glyphs = s->row->glyphs[s->area];
19319 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19320 int end = s->row->used[s->area];
19322 for (i = first; i < end && s->right_overhang > x; ++i)
19323 x += glyphs[i].pixel_width;
19325 k = i;
19328 return k;
19332 /* Return the index of the last glyph following glyph string S that
19333 overwrites S because of its left overhang. Value is negative
19334 if no such glyph is found. */
19336 static int
19337 right_overwriting (s)
19338 struct glyph_string *s;
19340 int i, k, x;
19341 int end = s->row->used[s->area];
19342 struct glyph *glyphs = s->row->glyphs[s->area];
19343 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19345 k = -1;
19346 x = 0;
19347 for (i = first; i < end; ++i)
19349 int left, right;
19350 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19351 if (x - left < 0)
19352 k = i;
19353 x += glyphs[i].pixel_width;
19356 return k;
19360 /* Get face and two-byte form of character C in face FACE_ID on frame
19361 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19362 means we want to display multibyte text. DISPLAY_P non-zero means
19363 make sure that X resources for the face returned are allocated.
19364 Value is a pointer to a realized face that is ready for display if
19365 DISPLAY_P is non-zero. */
19367 static INLINE struct face *
19368 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19369 struct frame *f;
19370 int c, face_id;
19371 XChar2b *char2b;
19372 int multibyte_p, display_p;
19374 struct face *face = FACE_FROM_ID (f, face_id);
19376 if (!multibyte_p)
19378 /* Unibyte case. We don't have to encode, but we have to make
19379 sure to use a face suitable for unibyte. */
19380 STORE_XCHAR2B (char2b, 0, c);
19381 face_id = FACE_FOR_CHAR (f, face, c);
19382 face = FACE_FROM_ID (f, face_id);
19384 else if (c < 128)
19386 /* Case of ASCII in a face known to fit ASCII. */
19387 STORE_XCHAR2B (char2b, 0, c);
19389 else
19391 int c1, c2, charset;
19393 /* Split characters into bytes. If c2 is -1 afterwards, C is
19394 really a one-byte character so that byte1 is zero. */
19395 SPLIT_CHAR (c, charset, c1, c2);
19396 if (c2 > 0)
19397 STORE_XCHAR2B (char2b, c1, c2);
19398 else
19399 STORE_XCHAR2B (char2b, 0, c1);
19401 /* Maybe encode the character in *CHAR2B. */
19402 if (face->font != NULL)
19404 struct font_info *font_info
19405 = FONT_INFO_FROM_ID (f, face->font_info_id);
19406 if (font_info)
19407 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
19411 /* Make sure X resources of the face are allocated. */
19412 #ifdef HAVE_X_WINDOWS
19413 if (display_p)
19414 #endif
19416 xassert (face != NULL);
19417 PREPARE_FACE_FOR_DISPLAY (f, face);
19420 return face;
19424 /* Set background width of glyph string S. START is the index of the
19425 first glyph following S. LAST_X is the right-most x-position + 1
19426 in the drawing area. */
19428 static INLINE void
19429 set_glyph_string_background_width (s, start, last_x)
19430 struct glyph_string *s;
19431 int start;
19432 int last_x;
19434 /* If the face of this glyph string has to be drawn to the end of
19435 the drawing area, set S->extends_to_end_of_line_p. */
19437 if (start == s->row->used[s->area]
19438 && s->area == TEXT_AREA
19439 && ((s->row->fill_line_p
19440 && (s->hl == DRAW_NORMAL_TEXT
19441 || s->hl == DRAW_IMAGE_RAISED
19442 || s->hl == DRAW_IMAGE_SUNKEN))
19443 || s->hl == DRAW_MOUSE_FACE))
19444 s->extends_to_end_of_line_p = 1;
19446 /* If S extends its face to the end of the line, set its
19447 background_width to the distance to the right edge of the drawing
19448 area. */
19449 if (s->extends_to_end_of_line_p)
19450 s->background_width = last_x - s->x + 1;
19451 else
19452 s->background_width = s->width;
19456 /* Compute overhangs and x-positions for glyph string S and its
19457 predecessors, or successors. X is the starting x-position for S.
19458 BACKWARD_P non-zero means process predecessors. */
19460 static void
19461 compute_overhangs_and_x (s, x, backward_p)
19462 struct glyph_string *s;
19463 int x;
19464 int backward_p;
19466 if (backward_p)
19468 while (s)
19470 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19471 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19472 x -= s->width;
19473 s->x = x;
19474 s = s->prev;
19477 else
19479 while (s)
19481 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19482 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19483 s->x = x;
19484 x += s->width;
19485 s = s->next;
19492 /* The following macros are only called from draw_glyphs below.
19493 They reference the following parameters of that function directly:
19494 `w', `row', `area', and `overlap_p'
19495 as well as the following local variables:
19496 `s', `f', and `hdc' (in W32) */
19498 #ifdef HAVE_NTGUI
19499 /* On W32, silently add local `hdc' variable to argument list of
19500 init_glyph_string. */
19501 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19502 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19503 #else
19504 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19505 init_glyph_string (s, char2b, w, row, area, start, hl)
19506 #endif
19508 /* Add a glyph string for a stretch glyph to the list of strings
19509 between HEAD and TAIL. START is the index of the stretch glyph in
19510 row area AREA of glyph row ROW. END is the index of the last glyph
19511 in that glyph row area. X is the current output position assigned
19512 to the new glyph string constructed. HL overrides that face of the
19513 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19514 is the right-most x-position of the drawing area. */
19516 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19517 and below -- keep them on one line. */
19518 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19519 do \
19521 s = (struct glyph_string *) alloca (sizeof *s); \
19522 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19523 START = fill_stretch_glyph_string (s, row, area, START, END); \
19524 append_glyph_string (&HEAD, &TAIL, s); \
19525 s->x = (X); \
19527 while (0)
19530 /* Add a glyph string for an image glyph to the list of strings
19531 between HEAD and TAIL. START is the index of the image glyph in
19532 row area AREA of glyph row ROW. END is the index of the last glyph
19533 in that glyph row area. X is the current output position assigned
19534 to the new glyph string constructed. HL overrides that face of the
19535 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19536 is the right-most x-position of the drawing area. */
19538 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19539 do \
19541 s = (struct glyph_string *) alloca (sizeof *s); \
19542 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19543 fill_image_glyph_string (s); \
19544 append_glyph_string (&HEAD, &TAIL, s); \
19545 ++START; \
19546 s->x = (X); \
19548 while (0)
19551 /* Add a glyph string for a sequence of character glyphs to the list
19552 of strings between HEAD and TAIL. START is the index of the first
19553 glyph in row area AREA of glyph row ROW that is part of the new
19554 glyph string. END is the index of the last glyph in that glyph row
19555 area. X is the current output position assigned to the new glyph
19556 string constructed. HL overrides that face of the glyph; e.g. it
19557 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19558 right-most x-position of the drawing area. */
19560 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19561 do \
19563 int c, face_id; \
19564 XChar2b *char2b; \
19566 c = (row)->glyphs[area][START].u.ch; \
19567 face_id = (row)->glyphs[area][START].face_id; \
19569 s = (struct glyph_string *) alloca (sizeof *s); \
19570 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19571 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19572 append_glyph_string (&HEAD, &TAIL, s); \
19573 s->x = (X); \
19574 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19576 while (0)
19579 /* Add a glyph string for a composite sequence to the list of strings
19580 between HEAD and TAIL. START is the index of the first glyph in
19581 row area AREA of glyph row ROW that is part of the new glyph
19582 string. END is the index of the last glyph in that glyph row area.
19583 X is the current output position assigned to the new glyph string
19584 constructed. HL overrides that face of the glyph; e.g. it is
19585 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19586 x-position of the drawing area. */
19588 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19589 do { \
19590 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19591 int face_id = (row)->glyphs[area][START].face_id; \
19592 struct face *base_face = FACE_FROM_ID (f, face_id); \
19593 struct composition *cmp = composition_table[cmp_id]; \
19594 int glyph_len = cmp->glyph_len; \
19595 XChar2b *char2b; \
19596 struct face **faces; \
19597 struct glyph_string *first_s = NULL; \
19598 int n; \
19600 base_face = base_face->ascii_face; \
19601 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19602 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19603 /* At first, fill in `char2b' and `faces'. */ \
19604 for (n = 0; n < glyph_len; n++) \
19606 int c = COMPOSITION_GLYPH (cmp, n); \
19607 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19608 faces[n] = FACE_FROM_ID (f, this_face_id); \
19609 get_char_face_and_encoding (f, c, this_face_id, \
19610 char2b + n, 1, 1); \
19613 /* Make glyph_strings for each glyph sequence that is drawable by \
19614 the same face, and append them to HEAD/TAIL. */ \
19615 for (n = 0; n < cmp->glyph_len;) \
19617 s = (struct glyph_string *) alloca (sizeof *s); \
19618 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19619 append_glyph_string (&(HEAD), &(TAIL), s); \
19620 s->cmp = cmp; \
19621 s->gidx = n; \
19622 s->x = (X); \
19624 if (n == 0) \
19625 first_s = s; \
19627 n = fill_composite_glyph_string (s, faces, overlaps); \
19630 ++START; \
19631 s = first_s; \
19632 } while (0)
19635 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19636 of AREA of glyph row ROW on window W between indices START and END.
19637 HL overrides the face for drawing glyph strings, e.g. it is
19638 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19639 x-positions of the drawing area.
19641 This is an ugly monster macro construct because we must use alloca
19642 to allocate glyph strings (because draw_glyphs can be called
19643 asynchronously). */
19645 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19646 do \
19648 HEAD = TAIL = NULL; \
19649 while (START < END) \
19651 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19652 switch (first_glyph->type) \
19654 case CHAR_GLYPH: \
19655 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19656 HL, X, LAST_X); \
19657 break; \
19659 case COMPOSITE_GLYPH: \
19660 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19661 HL, X, LAST_X); \
19662 break; \
19664 case STRETCH_GLYPH: \
19665 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19666 HL, X, LAST_X); \
19667 break; \
19669 case IMAGE_GLYPH: \
19670 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19671 HL, X, LAST_X); \
19672 break; \
19674 default: \
19675 abort (); \
19678 set_glyph_string_background_width (s, START, LAST_X); \
19679 (X) += s->width; \
19682 while (0)
19685 /* Draw glyphs between START and END in AREA of ROW on window W,
19686 starting at x-position X. X is relative to AREA in W. HL is a
19687 face-override with the following meaning:
19689 DRAW_NORMAL_TEXT draw normally
19690 DRAW_CURSOR draw in cursor face
19691 DRAW_MOUSE_FACE draw in mouse face.
19692 DRAW_INVERSE_VIDEO draw in mode line face
19693 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19694 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19696 If OVERLAPS is non-zero, draw only the foreground of characters and
19697 clip to the physical height of ROW. Non-zero value also defines
19698 the overlapping part to be drawn:
19700 OVERLAPS_PRED overlap with preceding rows
19701 OVERLAPS_SUCC overlap with succeeding rows
19702 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19703 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19705 Value is the x-position reached, relative to AREA of W. */
19707 static int
19708 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19709 struct window *w;
19710 int x;
19711 struct glyph_row *row;
19712 enum glyph_row_area area;
19713 int start, end;
19714 enum draw_glyphs_face hl;
19715 int overlaps;
19717 struct glyph_string *head, *tail;
19718 struct glyph_string *s;
19719 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19720 int last_x, area_width;
19721 int x_reached;
19722 int i, j;
19723 struct frame *f = XFRAME (WINDOW_FRAME (w));
19724 DECLARE_HDC (hdc);
19726 ALLOCATE_HDC (hdc, f);
19728 /* Let's rather be paranoid than getting a SEGV. */
19729 end = min (end, row->used[area]);
19730 start = max (0, start);
19731 start = min (end, start);
19733 /* Translate X to frame coordinates. Set last_x to the right
19734 end of the drawing area. */
19735 if (row->full_width_p)
19737 /* X is relative to the left edge of W, without scroll bars
19738 or fringes. */
19739 x += WINDOW_LEFT_EDGE_X (w);
19740 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19742 else
19744 int area_left = window_box_left (w, area);
19745 x += area_left;
19746 area_width = window_box_width (w, area);
19747 last_x = area_left + area_width;
19750 /* Build a doubly-linked list of glyph_string structures between
19751 head and tail from what we have to draw. Note that the macro
19752 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19753 the reason we use a separate variable `i'. */
19754 i = start;
19755 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19756 if (tail)
19757 x_reached = tail->x + tail->background_width;
19758 else
19759 x_reached = x;
19761 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19762 the row, redraw some glyphs in front or following the glyph
19763 strings built above. */
19764 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19766 int dummy_x = 0;
19767 struct glyph_string *h, *t;
19769 /* Compute overhangs for all glyph strings. */
19770 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19771 for (s = head; s; s = s->next)
19772 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19774 /* Prepend glyph strings for glyphs in front of the first glyph
19775 string that are overwritten because of the first glyph
19776 string's left overhang. The background of all strings
19777 prepended must be drawn because the first glyph string
19778 draws over it. */
19779 i = left_overwritten (head);
19780 if (i >= 0)
19782 j = i;
19783 BUILD_GLYPH_STRINGS (j, start, h, t,
19784 DRAW_NORMAL_TEXT, dummy_x, last_x);
19785 start = i;
19786 compute_overhangs_and_x (t, head->x, 1);
19787 prepend_glyph_string_lists (&head, &tail, h, t);
19788 clip_head = head;
19791 /* Prepend glyph strings for glyphs in front of the first glyph
19792 string that overwrite that glyph string because of their
19793 right overhang. For these strings, only the foreground must
19794 be drawn, because it draws over the glyph string at `head'.
19795 The background must not be drawn because this would overwrite
19796 right overhangs of preceding glyphs for which no glyph
19797 strings exist. */
19798 i = left_overwriting (head);
19799 if (i >= 0)
19801 clip_head = head;
19802 BUILD_GLYPH_STRINGS (i, start, h, t,
19803 DRAW_NORMAL_TEXT, dummy_x, last_x);
19804 for (s = h; s; s = s->next)
19805 s->background_filled_p = 1;
19806 compute_overhangs_and_x (t, head->x, 1);
19807 prepend_glyph_string_lists (&head, &tail, h, t);
19810 /* Append glyphs strings for glyphs following the last glyph
19811 string tail that are overwritten by tail. The background of
19812 these strings has to be drawn because tail's foreground draws
19813 over it. */
19814 i = right_overwritten (tail);
19815 if (i >= 0)
19817 BUILD_GLYPH_STRINGS (end, i, h, t,
19818 DRAW_NORMAL_TEXT, x, last_x);
19819 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19820 append_glyph_string_lists (&head, &tail, h, t);
19821 clip_tail = tail;
19824 /* Append glyph strings for glyphs following the last glyph
19825 string tail that overwrite tail. The foreground of such
19826 glyphs has to be drawn because it writes into the background
19827 of tail. The background must not be drawn because it could
19828 paint over the foreground of following glyphs. */
19829 i = right_overwriting (tail);
19830 if (i >= 0)
19832 clip_tail = tail;
19833 BUILD_GLYPH_STRINGS (end, i, h, t,
19834 DRAW_NORMAL_TEXT, x, last_x);
19835 for (s = h; s; s = s->next)
19836 s->background_filled_p = 1;
19837 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19838 append_glyph_string_lists (&head, &tail, h, t);
19840 if (clip_head || clip_tail)
19841 for (s = head; s; s = s->next)
19843 s->clip_head = clip_head;
19844 s->clip_tail = clip_tail;
19848 /* Draw all strings. */
19849 for (s = head; s; s = s->next)
19850 FRAME_RIF (f)->draw_glyph_string (s);
19852 if (area == TEXT_AREA
19853 && !row->full_width_p
19854 /* When drawing overlapping rows, only the glyph strings'
19855 foreground is drawn, which doesn't erase a cursor
19856 completely. */
19857 && !overlaps)
19859 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19860 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19861 : (tail ? tail->x + tail->background_width : x));
19863 int text_left = window_box_left (w, TEXT_AREA);
19864 x0 -= text_left;
19865 x1 -= text_left;
19867 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19868 row->y, MATRIX_ROW_BOTTOM_Y (row));
19871 /* Value is the x-position up to which drawn, relative to AREA of W.
19872 This doesn't include parts drawn because of overhangs. */
19873 if (row->full_width_p)
19874 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19875 else
19876 x_reached -= window_box_left (w, area);
19878 RELEASE_HDC (hdc, f);
19880 return x_reached;
19883 /* Expand row matrix if too narrow. Don't expand if area
19884 is not present. */
19886 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19888 if (!fonts_changed_p \
19889 && (it->glyph_row->glyphs[area] \
19890 < it->glyph_row->glyphs[area + 1])) \
19892 it->w->ncols_scale_factor++; \
19893 fonts_changed_p = 1; \
19897 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19898 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19900 static INLINE void
19901 append_glyph (it)
19902 struct it *it;
19904 struct glyph *glyph;
19905 enum glyph_row_area area = it->area;
19907 xassert (it->glyph_row);
19908 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19910 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19911 if (glyph < it->glyph_row->glyphs[area + 1])
19913 glyph->charpos = CHARPOS (it->position);
19914 glyph->object = it->object;
19915 glyph->pixel_width = it->pixel_width;
19916 glyph->ascent = it->ascent;
19917 glyph->descent = it->descent;
19918 glyph->voffset = it->voffset;
19919 glyph->type = CHAR_GLYPH;
19920 glyph->multibyte_p = it->multibyte_p;
19921 glyph->left_box_line_p = it->start_of_box_run_p;
19922 glyph->right_box_line_p = it->end_of_box_run_p;
19923 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19924 || it->phys_descent > it->descent);
19925 glyph->padding_p = 0;
19926 glyph->glyph_not_available_p = it->glyph_not_available_p;
19927 glyph->face_id = it->face_id;
19928 glyph->u.ch = it->char_to_display;
19929 glyph->slice = null_glyph_slice;
19930 glyph->font_type = FONT_TYPE_UNKNOWN;
19931 ++it->glyph_row->used[area];
19933 else
19934 IT_EXPAND_MATRIX_WIDTH (it, area);
19937 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19938 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19940 static INLINE void
19941 append_composite_glyph (it)
19942 struct it *it;
19944 struct glyph *glyph;
19945 enum glyph_row_area area = it->area;
19947 xassert (it->glyph_row);
19949 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19950 if (glyph < it->glyph_row->glyphs[area + 1])
19952 glyph->charpos = CHARPOS (it->position);
19953 glyph->object = it->object;
19954 glyph->pixel_width = it->pixel_width;
19955 glyph->ascent = it->ascent;
19956 glyph->descent = it->descent;
19957 glyph->voffset = it->voffset;
19958 glyph->type = COMPOSITE_GLYPH;
19959 glyph->multibyte_p = it->multibyte_p;
19960 glyph->left_box_line_p = it->start_of_box_run_p;
19961 glyph->right_box_line_p = it->end_of_box_run_p;
19962 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19963 || it->phys_descent > it->descent);
19964 glyph->padding_p = 0;
19965 glyph->glyph_not_available_p = 0;
19966 glyph->face_id = it->face_id;
19967 glyph->u.cmp_id = it->cmp_id;
19968 glyph->slice = null_glyph_slice;
19969 glyph->font_type = FONT_TYPE_UNKNOWN;
19970 ++it->glyph_row->used[area];
19972 else
19973 IT_EXPAND_MATRIX_WIDTH (it, area);
19977 /* Change IT->ascent and IT->height according to the setting of
19978 IT->voffset. */
19980 static INLINE void
19981 take_vertical_position_into_account (it)
19982 struct it *it;
19984 if (it->voffset)
19986 if (it->voffset < 0)
19987 /* Increase the ascent so that we can display the text higher
19988 in the line. */
19989 it->ascent -= it->voffset;
19990 else
19991 /* Increase the descent so that we can display the text lower
19992 in the line. */
19993 it->descent += it->voffset;
19998 /* Produce glyphs/get display metrics for the image IT is loaded with.
19999 See the description of struct display_iterator in dispextern.h for
20000 an overview of struct display_iterator. */
20002 static void
20003 produce_image_glyph (it)
20004 struct it *it;
20006 struct image *img;
20007 struct face *face;
20008 int glyph_ascent, crop;
20009 struct glyph_slice slice;
20011 xassert (it->what == IT_IMAGE);
20013 face = FACE_FROM_ID (it->f, it->face_id);
20014 xassert (face);
20015 /* Make sure X resources of the face is loaded. */
20016 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20018 if (it->image_id < 0)
20020 /* Fringe bitmap. */
20021 it->ascent = it->phys_ascent = 0;
20022 it->descent = it->phys_descent = 0;
20023 it->pixel_width = 0;
20024 it->nglyphs = 0;
20025 return;
20028 img = IMAGE_FROM_ID (it->f, it->image_id);
20029 xassert (img);
20030 /* Make sure X resources of the image is loaded. */
20031 prepare_image_for_display (it->f, img);
20033 slice.x = slice.y = 0;
20034 slice.width = img->width;
20035 slice.height = img->height;
20037 if (INTEGERP (it->slice.x))
20038 slice.x = XINT (it->slice.x);
20039 else if (FLOATP (it->slice.x))
20040 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20042 if (INTEGERP (it->slice.y))
20043 slice.y = XINT (it->slice.y);
20044 else if (FLOATP (it->slice.y))
20045 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20047 if (INTEGERP (it->slice.width))
20048 slice.width = XINT (it->slice.width);
20049 else if (FLOATP (it->slice.width))
20050 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20052 if (INTEGERP (it->slice.height))
20053 slice.height = XINT (it->slice.height);
20054 else if (FLOATP (it->slice.height))
20055 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20057 if (slice.x >= img->width)
20058 slice.x = img->width;
20059 if (slice.y >= img->height)
20060 slice.y = img->height;
20061 if (slice.x + slice.width >= img->width)
20062 slice.width = img->width - slice.x;
20063 if (slice.y + slice.height > img->height)
20064 slice.height = img->height - slice.y;
20066 if (slice.width == 0 || slice.height == 0)
20067 return;
20069 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20071 it->descent = slice.height - glyph_ascent;
20072 if (slice.y == 0)
20073 it->descent += img->vmargin;
20074 if (slice.y + slice.height == img->height)
20075 it->descent += img->vmargin;
20076 it->phys_descent = it->descent;
20078 it->pixel_width = slice.width;
20079 if (slice.x == 0)
20080 it->pixel_width += img->hmargin;
20081 if (slice.x + slice.width == img->width)
20082 it->pixel_width += img->hmargin;
20084 /* It's quite possible for images to have an ascent greater than
20085 their height, so don't get confused in that case. */
20086 if (it->descent < 0)
20087 it->descent = 0;
20089 #if 0 /* this breaks image tiling */
20090 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20091 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20092 if (face_ascent > it->ascent)
20093 it->ascent = it->phys_ascent = face_ascent;
20094 #endif
20096 it->nglyphs = 1;
20098 if (face->box != FACE_NO_BOX)
20100 if (face->box_line_width > 0)
20102 if (slice.y == 0)
20103 it->ascent += face->box_line_width;
20104 if (slice.y + slice.height == img->height)
20105 it->descent += face->box_line_width;
20108 if (it->start_of_box_run_p && slice.x == 0)
20109 it->pixel_width += eabs (face->box_line_width);
20110 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20111 it->pixel_width += eabs (face->box_line_width);
20114 take_vertical_position_into_account (it);
20116 /* Automatically crop wide image glyphs at right edge so we can
20117 draw the cursor on same display row. */
20118 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20119 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20121 it->pixel_width -= crop;
20122 slice.width -= crop;
20125 if (it->glyph_row)
20127 struct glyph *glyph;
20128 enum glyph_row_area area = it->area;
20130 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20131 if (glyph < it->glyph_row->glyphs[area + 1])
20133 glyph->charpos = CHARPOS (it->position);
20134 glyph->object = it->object;
20135 glyph->pixel_width = it->pixel_width;
20136 glyph->ascent = glyph_ascent;
20137 glyph->descent = it->descent;
20138 glyph->voffset = it->voffset;
20139 glyph->type = IMAGE_GLYPH;
20140 glyph->multibyte_p = it->multibyte_p;
20141 glyph->left_box_line_p = it->start_of_box_run_p;
20142 glyph->right_box_line_p = it->end_of_box_run_p;
20143 glyph->overlaps_vertically_p = 0;
20144 glyph->padding_p = 0;
20145 glyph->glyph_not_available_p = 0;
20146 glyph->face_id = it->face_id;
20147 glyph->u.img_id = img->id;
20148 glyph->slice = slice;
20149 glyph->font_type = FONT_TYPE_UNKNOWN;
20150 ++it->glyph_row->used[area];
20152 else
20153 IT_EXPAND_MATRIX_WIDTH (it, area);
20158 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20159 of the glyph, WIDTH and HEIGHT are the width and height of the
20160 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20162 static void
20163 append_stretch_glyph (it, object, width, height, ascent)
20164 struct it *it;
20165 Lisp_Object object;
20166 int width, height;
20167 int ascent;
20169 struct glyph *glyph;
20170 enum glyph_row_area area = it->area;
20172 xassert (ascent >= 0 && ascent <= height);
20174 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20175 if (glyph < it->glyph_row->glyphs[area + 1])
20177 glyph->charpos = CHARPOS (it->position);
20178 glyph->object = object;
20179 glyph->pixel_width = width;
20180 glyph->ascent = ascent;
20181 glyph->descent = height - ascent;
20182 glyph->voffset = it->voffset;
20183 glyph->type = STRETCH_GLYPH;
20184 glyph->multibyte_p = it->multibyte_p;
20185 glyph->left_box_line_p = it->start_of_box_run_p;
20186 glyph->right_box_line_p = it->end_of_box_run_p;
20187 glyph->overlaps_vertically_p = 0;
20188 glyph->padding_p = 0;
20189 glyph->glyph_not_available_p = 0;
20190 glyph->face_id = it->face_id;
20191 glyph->u.stretch.ascent = ascent;
20192 glyph->u.stretch.height = height;
20193 glyph->slice = null_glyph_slice;
20194 glyph->font_type = FONT_TYPE_UNKNOWN;
20195 ++it->glyph_row->used[area];
20197 else
20198 IT_EXPAND_MATRIX_WIDTH (it, area);
20202 /* Produce a stretch glyph for iterator IT. IT->object is the value
20203 of the glyph property displayed. The value must be a list
20204 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20205 being recognized:
20207 1. `:width WIDTH' specifies that the space should be WIDTH *
20208 canonical char width wide. WIDTH may be an integer or floating
20209 point number.
20211 2. `:relative-width FACTOR' specifies that the width of the stretch
20212 should be computed from the width of the first character having the
20213 `glyph' property, and should be FACTOR times that width.
20215 3. `:align-to HPOS' specifies that the space should be wide enough
20216 to reach HPOS, a value in canonical character units.
20218 Exactly one of the above pairs must be present.
20220 4. `:height HEIGHT' specifies that the height of the stretch produced
20221 should be HEIGHT, measured in canonical character units.
20223 5. `:relative-height FACTOR' specifies that the height of the
20224 stretch should be FACTOR times the height of the characters having
20225 the glyph property.
20227 Either none or exactly one of 4 or 5 must be present.
20229 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20230 of the stretch should be used for the ascent of the stretch.
20231 ASCENT must be in the range 0 <= ASCENT <= 100. */
20233 static void
20234 produce_stretch_glyph (it)
20235 struct it *it;
20237 /* (space :width WIDTH :height HEIGHT ...) */
20238 Lisp_Object prop, plist;
20239 int width = 0, height = 0, align_to = -1;
20240 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20241 int ascent = 0;
20242 double tem;
20243 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20244 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20246 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20248 /* List should start with `space'. */
20249 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20250 plist = XCDR (it->object);
20252 /* Compute the width of the stretch. */
20253 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20254 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20256 /* Absolute width `:width WIDTH' specified and valid. */
20257 zero_width_ok_p = 1;
20258 width = (int)tem;
20260 else if (prop = Fplist_get (plist, QCrelative_width),
20261 NUMVAL (prop) > 0)
20263 /* Relative width `:relative-width FACTOR' specified and valid.
20264 Compute the width of the characters having the `glyph'
20265 property. */
20266 struct it it2;
20267 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20269 it2 = *it;
20270 if (it->multibyte_p)
20272 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20273 - IT_BYTEPOS (*it));
20274 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20276 else
20277 it2.c = *p, it2.len = 1;
20279 it2.glyph_row = NULL;
20280 it2.what = IT_CHARACTER;
20281 x_produce_glyphs (&it2);
20282 width = NUMVAL (prop) * it2.pixel_width;
20284 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20285 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20287 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20288 align_to = (align_to < 0
20290 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20291 else if (align_to < 0)
20292 align_to = window_box_left_offset (it->w, TEXT_AREA);
20293 width = max (0, (int)tem + align_to - it->current_x);
20294 zero_width_ok_p = 1;
20296 else
20297 /* Nothing specified -> width defaults to canonical char width. */
20298 width = FRAME_COLUMN_WIDTH (it->f);
20300 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20301 width = 1;
20303 /* Compute height. */
20304 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20305 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20307 height = (int)tem;
20308 zero_height_ok_p = 1;
20310 else if (prop = Fplist_get (plist, QCrelative_height),
20311 NUMVAL (prop) > 0)
20312 height = FONT_HEIGHT (font) * NUMVAL (prop);
20313 else
20314 height = FONT_HEIGHT (font);
20316 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20317 height = 1;
20319 /* Compute percentage of height used for ascent. If
20320 `:ascent ASCENT' is present and valid, use that. Otherwise,
20321 derive the ascent from the font in use. */
20322 if (prop = Fplist_get (plist, QCascent),
20323 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20324 ascent = height * NUMVAL (prop) / 100.0;
20325 else if (!NILP (prop)
20326 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20327 ascent = min (max (0, (int)tem), height);
20328 else
20329 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20331 if (width > 0 && !it->truncate_lines_p
20332 && it->current_x + width > it->last_visible_x)
20333 width = it->last_visible_x - it->current_x - 1;
20335 if (width > 0 && height > 0 && it->glyph_row)
20337 Lisp_Object object = it->stack[it->sp - 1].string;
20338 if (!STRINGP (object))
20339 object = it->w->buffer;
20340 append_stretch_glyph (it, object, width, height, ascent);
20343 it->pixel_width = width;
20344 it->ascent = it->phys_ascent = ascent;
20345 it->descent = it->phys_descent = height - it->ascent;
20346 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20348 take_vertical_position_into_account (it);
20351 /* Get line-height and line-spacing property at point.
20352 If line-height has format (HEIGHT TOTAL), return TOTAL
20353 in TOTAL_HEIGHT. */
20355 static Lisp_Object
20356 get_line_height_property (it, prop)
20357 struct it *it;
20358 Lisp_Object prop;
20360 Lisp_Object position;
20362 if (STRINGP (it->object))
20363 position = make_number (IT_STRING_CHARPOS (*it));
20364 else if (BUFFERP (it->object))
20365 position = make_number (IT_CHARPOS (*it));
20366 else
20367 return Qnil;
20369 return Fget_char_property (position, prop, it->object);
20372 /* Calculate line-height and line-spacing properties.
20373 An integer value specifies explicit pixel value.
20374 A float value specifies relative value to current face height.
20375 A cons (float . face-name) specifies relative value to
20376 height of specified face font.
20378 Returns height in pixels, or nil. */
20381 static Lisp_Object
20382 calc_line_height_property (it, val, font, boff, override)
20383 struct it *it;
20384 Lisp_Object val;
20385 XFontStruct *font;
20386 int boff, override;
20388 Lisp_Object face_name = Qnil;
20389 int ascent, descent, height;
20391 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20392 return val;
20394 if (CONSP (val))
20396 face_name = XCAR (val);
20397 val = XCDR (val);
20398 if (!NUMBERP (val))
20399 val = make_number (1);
20400 if (NILP (face_name))
20402 height = it->ascent + it->descent;
20403 goto scale;
20407 if (NILP (face_name))
20409 font = FRAME_FONT (it->f);
20410 boff = FRAME_BASELINE_OFFSET (it->f);
20412 else if (EQ (face_name, Qt))
20414 override = 0;
20416 else
20418 int face_id;
20419 struct face *face;
20420 struct font_info *font_info;
20422 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20423 if (face_id < 0)
20424 return make_number (-1);
20426 face = FACE_FROM_ID (it->f, face_id);
20427 font = face->font;
20428 if (font == NULL)
20429 return make_number (-1);
20431 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20432 boff = font_info->baseline_offset;
20433 if (font_info->vertical_centering)
20434 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20437 ascent = FONT_BASE (font) + boff;
20438 descent = FONT_DESCENT (font) - boff;
20440 if (override)
20442 it->override_ascent = ascent;
20443 it->override_descent = descent;
20444 it->override_boff = boff;
20447 height = ascent + descent;
20449 scale:
20450 if (FLOATP (val))
20451 height = (int)(XFLOAT_DATA (val) * height);
20452 else if (INTEGERP (val))
20453 height *= XINT (val);
20455 return make_number (height);
20459 /* RIF:
20460 Produce glyphs/get display metrics for the display element IT is
20461 loaded with. See the description of struct it in dispextern.h
20462 for an overview of struct it. */
20464 void
20465 x_produce_glyphs (it)
20466 struct it *it;
20468 int extra_line_spacing = it->extra_line_spacing;
20470 it->glyph_not_available_p = 0;
20472 if (it->what == IT_CHARACTER)
20474 XChar2b char2b;
20475 XFontStruct *font;
20476 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20477 XCharStruct *pcm;
20478 int font_not_found_p;
20479 struct font_info *font_info;
20480 int boff; /* baseline offset */
20481 /* We may change it->multibyte_p upon unibyte<->multibyte
20482 conversion. So, save the current value now and restore it
20483 later.
20485 Note: It seems that we don't have to record multibyte_p in
20486 struct glyph because the character code itself tells if or
20487 not the character is multibyte. Thus, in the future, we must
20488 consider eliminating the field `multibyte_p' in the struct
20489 glyph. */
20490 int saved_multibyte_p = it->multibyte_p;
20492 /* Maybe translate single-byte characters to multibyte, or the
20493 other way. */
20494 it->char_to_display = it->c;
20495 if (!ASCII_BYTE_P (it->c))
20497 if (unibyte_display_via_language_environment
20498 && SINGLE_BYTE_CHAR_P (it->c)
20499 && (it->c >= 0240
20500 || !NILP (Vnonascii_translation_table)))
20502 it->char_to_display = unibyte_char_to_multibyte (it->c);
20503 it->multibyte_p = 1;
20504 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20505 face = FACE_FROM_ID (it->f, it->face_id);
20507 else if (!SINGLE_BYTE_CHAR_P (it->c)
20508 && !it->multibyte_p)
20510 it->multibyte_p = 1;
20511 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20512 face = FACE_FROM_ID (it->f, it->face_id);
20516 /* Get font to use. Encode IT->char_to_display. */
20517 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20518 &char2b, it->multibyte_p, 0);
20519 font = face->font;
20521 /* When no suitable font found, use the default font. */
20522 font_not_found_p = font == NULL;
20523 if (font_not_found_p)
20525 font = FRAME_FONT (it->f);
20526 boff = FRAME_BASELINE_OFFSET (it->f);
20527 font_info = NULL;
20529 else
20531 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20532 boff = font_info->baseline_offset;
20533 if (font_info->vertical_centering)
20534 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20537 if (it->char_to_display >= ' '
20538 && (!it->multibyte_p || it->char_to_display < 128))
20540 /* Either unibyte or ASCII. */
20541 int stretched_p;
20543 it->nglyphs = 1;
20545 pcm = FRAME_RIF (it->f)->per_char_metric
20546 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20548 if (it->override_ascent >= 0)
20550 it->ascent = it->override_ascent;
20551 it->descent = it->override_descent;
20552 boff = it->override_boff;
20554 else
20556 it->ascent = FONT_BASE (font) + boff;
20557 it->descent = FONT_DESCENT (font) - boff;
20560 if (pcm)
20562 it->phys_ascent = pcm->ascent + boff;
20563 it->phys_descent = pcm->descent - boff;
20564 it->pixel_width = pcm->width;
20566 else
20568 it->glyph_not_available_p = 1;
20569 it->phys_ascent = it->ascent;
20570 it->phys_descent = it->descent;
20571 it->pixel_width = FONT_WIDTH (font);
20574 if (it->constrain_row_ascent_descent_p)
20576 if (it->descent > it->max_descent)
20578 it->ascent += it->descent - it->max_descent;
20579 it->descent = it->max_descent;
20581 if (it->ascent > it->max_ascent)
20583 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20584 it->ascent = it->max_ascent;
20586 it->phys_ascent = min (it->phys_ascent, it->ascent);
20587 it->phys_descent = min (it->phys_descent, it->descent);
20588 extra_line_spacing = 0;
20591 /* If this is a space inside a region of text with
20592 `space-width' property, change its width. */
20593 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20594 if (stretched_p)
20595 it->pixel_width *= XFLOATINT (it->space_width);
20597 /* If face has a box, add the box thickness to the character
20598 height. If character has a box line to the left and/or
20599 right, add the box line width to the character's width. */
20600 if (face->box != FACE_NO_BOX)
20602 int thick = face->box_line_width;
20604 if (thick > 0)
20606 it->ascent += thick;
20607 it->descent += thick;
20609 else
20610 thick = -thick;
20612 if (it->start_of_box_run_p)
20613 it->pixel_width += thick;
20614 if (it->end_of_box_run_p)
20615 it->pixel_width += thick;
20618 /* If face has an overline, add the height of the overline
20619 (1 pixel) and a 1 pixel margin to the character height. */
20620 if (face->overline_p)
20621 it->ascent += overline_margin;
20623 if (it->constrain_row_ascent_descent_p)
20625 if (it->ascent > it->max_ascent)
20626 it->ascent = it->max_ascent;
20627 if (it->descent > it->max_descent)
20628 it->descent = it->max_descent;
20631 take_vertical_position_into_account (it);
20633 /* If we have to actually produce glyphs, do it. */
20634 if (it->glyph_row)
20636 if (stretched_p)
20638 /* Translate a space with a `space-width' property
20639 into a stretch glyph. */
20640 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20641 / FONT_HEIGHT (font));
20642 append_stretch_glyph (it, it->object, it->pixel_width,
20643 it->ascent + it->descent, ascent);
20645 else
20646 append_glyph (it);
20648 /* If characters with lbearing or rbearing are displayed
20649 in this line, record that fact in a flag of the
20650 glyph row. This is used to optimize X output code. */
20651 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20652 it->glyph_row->contains_overlapping_glyphs_p = 1;
20655 else if (it->char_to_display == '\n')
20657 /* A newline has no width but we need the height of the line.
20658 But if previous part of the line set a height, don't
20659 increase that height */
20661 Lisp_Object height;
20662 Lisp_Object total_height = Qnil;
20664 it->override_ascent = -1;
20665 it->pixel_width = 0;
20666 it->nglyphs = 0;
20668 height = get_line_height_property(it, Qline_height);
20669 /* Split (line-height total-height) list */
20670 if (CONSP (height)
20671 && CONSP (XCDR (height))
20672 && NILP (XCDR (XCDR (height))))
20674 total_height = XCAR (XCDR (height));
20675 height = XCAR (height);
20677 height = calc_line_height_property(it, height, font, boff, 1);
20679 if (it->override_ascent >= 0)
20681 it->ascent = it->override_ascent;
20682 it->descent = it->override_descent;
20683 boff = it->override_boff;
20685 else
20687 it->ascent = FONT_BASE (font) + boff;
20688 it->descent = FONT_DESCENT (font) - boff;
20691 if (EQ (height, Qt))
20693 if (it->descent > it->max_descent)
20695 it->ascent += it->descent - it->max_descent;
20696 it->descent = it->max_descent;
20698 if (it->ascent > it->max_ascent)
20700 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20701 it->ascent = it->max_ascent;
20703 it->phys_ascent = min (it->phys_ascent, it->ascent);
20704 it->phys_descent = min (it->phys_descent, it->descent);
20705 it->constrain_row_ascent_descent_p = 1;
20706 extra_line_spacing = 0;
20708 else
20710 Lisp_Object spacing;
20712 it->phys_ascent = it->ascent;
20713 it->phys_descent = it->descent;
20715 if ((it->max_ascent > 0 || it->max_descent > 0)
20716 && face->box != FACE_NO_BOX
20717 && face->box_line_width > 0)
20719 it->ascent += face->box_line_width;
20720 it->descent += face->box_line_width;
20722 if (!NILP (height)
20723 && XINT (height) > it->ascent + it->descent)
20724 it->ascent = XINT (height) - it->descent;
20726 if (!NILP (total_height))
20727 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20728 else
20730 spacing = get_line_height_property(it, Qline_spacing);
20731 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20733 if (INTEGERP (spacing))
20735 extra_line_spacing = XINT (spacing);
20736 if (!NILP (total_height))
20737 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20741 else if (it->char_to_display == '\t')
20743 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20744 int x = it->current_x + it->continuation_lines_width;
20745 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20747 /* If the distance from the current position to the next tab
20748 stop is less than a space character width, use the
20749 tab stop after that. */
20750 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20751 next_tab_x += tab_width;
20753 it->pixel_width = next_tab_x - x;
20754 it->nglyphs = 1;
20755 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20756 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20758 if (it->glyph_row)
20760 append_stretch_glyph (it, it->object, it->pixel_width,
20761 it->ascent + it->descent, it->ascent);
20764 else
20766 /* A multi-byte character. Assume that the display width of the
20767 character is the width of the character multiplied by the
20768 width of the font. */
20770 /* If we found a font, this font should give us the right
20771 metrics. If we didn't find a font, use the frame's
20772 default font and calculate the width of the character
20773 from the charset width; this is what old redisplay code
20774 did. */
20776 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20777 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20779 if (font_not_found_p || !pcm)
20781 int charset = CHAR_CHARSET (it->char_to_display);
20783 it->glyph_not_available_p = 1;
20784 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20785 * CHARSET_WIDTH (charset));
20786 it->phys_ascent = FONT_BASE (font) + boff;
20787 it->phys_descent = FONT_DESCENT (font) - boff;
20789 else
20791 it->pixel_width = pcm->width;
20792 it->phys_ascent = pcm->ascent + boff;
20793 it->phys_descent = pcm->descent - boff;
20794 if (it->glyph_row
20795 && (pcm->lbearing < 0
20796 || pcm->rbearing > pcm->width))
20797 it->glyph_row->contains_overlapping_glyphs_p = 1;
20799 it->nglyphs = 1;
20800 it->ascent = FONT_BASE (font) + boff;
20801 it->descent = FONT_DESCENT (font) - boff;
20802 if (face->box != FACE_NO_BOX)
20804 int thick = face->box_line_width;
20806 if (thick > 0)
20808 it->ascent += thick;
20809 it->descent += thick;
20811 else
20812 thick = - thick;
20814 if (it->start_of_box_run_p)
20815 it->pixel_width += thick;
20816 if (it->end_of_box_run_p)
20817 it->pixel_width += thick;
20820 /* If face has an overline, add the height of the overline
20821 (1 pixel) and a 1 pixel margin to the character height. */
20822 if (face->overline_p)
20823 it->ascent += overline_margin;
20825 take_vertical_position_into_account (it);
20827 if (it->glyph_row)
20828 append_glyph (it);
20830 it->multibyte_p = saved_multibyte_p;
20832 else if (it->what == IT_COMPOSITION)
20834 /* Note: A composition is represented as one glyph in the
20835 glyph matrix. There are no padding glyphs. */
20836 XChar2b char2b;
20837 XFontStruct *font;
20838 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20839 XCharStruct *pcm;
20840 int font_not_found_p;
20841 struct font_info *font_info;
20842 int boff; /* baseline offset */
20843 struct composition *cmp = composition_table[it->cmp_id];
20845 /* Maybe translate single-byte characters to multibyte. */
20846 it->char_to_display = it->c;
20847 if (unibyte_display_via_language_environment
20848 && SINGLE_BYTE_CHAR_P (it->c)
20849 && (it->c >= 0240
20850 || (it->c >= 0200
20851 && !NILP (Vnonascii_translation_table))))
20853 it->char_to_display = unibyte_char_to_multibyte (it->c);
20856 /* Get face and font to use. Encode IT->char_to_display. */
20857 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20858 face = FACE_FROM_ID (it->f, it->face_id);
20859 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20860 &char2b, it->multibyte_p, 0);
20861 font = face->font;
20863 /* When no suitable font found, use the default font. */
20864 font_not_found_p = font == NULL;
20865 if (font_not_found_p)
20867 font = FRAME_FONT (it->f);
20868 boff = FRAME_BASELINE_OFFSET (it->f);
20869 font_info = NULL;
20871 else
20873 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20874 boff = font_info->baseline_offset;
20875 if (font_info->vertical_centering)
20876 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20879 /* There are no padding glyphs, so there is only one glyph to
20880 produce for the composition. Important is that pixel_width,
20881 ascent and descent are the values of what is drawn by
20882 draw_glyphs (i.e. the values of the overall glyphs composed). */
20883 it->nglyphs = 1;
20885 /* If we have not yet calculated pixel size data of glyphs of
20886 the composition for the current face font, calculate them
20887 now. Theoretically, we have to check all fonts for the
20888 glyphs, but that requires much time and memory space. So,
20889 here we check only the font of the first glyph. This leads
20890 to incorrect display very rarely, and C-l (recenter) can
20891 correct the display anyway. */
20892 if (cmp->font != (void *) font)
20894 /* Ascent and descent of the font of the first character of
20895 this composition (adjusted by baseline offset). Ascent
20896 and descent of overall glyphs should not be less than
20897 them respectively. */
20898 int font_ascent = FONT_BASE (font) + boff;
20899 int font_descent = FONT_DESCENT (font) - boff;
20900 /* Bounding box of the overall glyphs. */
20901 int leftmost, rightmost, lowest, highest;
20902 int i, width, ascent, descent;
20904 cmp->font = (void *) font;
20906 /* Initialize the bounding box. */
20907 if (font_info
20908 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20909 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20911 width = pcm->width;
20912 ascent = pcm->ascent;
20913 descent = pcm->descent;
20915 else
20917 width = FONT_WIDTH (font);
20918 ascent = FONT_BASE (font);
20919 descent = FONT_DESCENT (font);
20922 rightmost = width;
20923 lowest = - descent + boff;
20924 highest = ascent + boff;
20925 leftmost = 0;
20927 if (font_info
20928 && font_info->default_ascent
20929 && CHAR_TABLE_P (Vuse_default_ascent)
20930 && !NILP (Faref (Vuse_default_ascent,
20931 make_number (it->char_to_display))))
20932 highest = font_info->default_ascent + boff;
20934 /* Draw the first glyph at the normal position. It may be
20935 shifted to right later if some other glyphs are drawn at
20936 the left. */
20937 cmp->offsets[0] = 0;
20938 cmp->offsets[1] = boff;
20940 /* Set cmp->offsets for the remaining glyphs. */
20941 for (i = 1; i < cmp->glyph_len; i++)
20943 int left, right, btm, top;
20944 int ch = COMPOSITION_GLYPH (cmp, i);
20945 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20947 face = FACE_FROM_ID (it->f, face_id);
20948 get_char_face_and_encoding (it->f, ch, face->id,
20949 &char2b, it->multibyte_p, 0);
20950 font = face->font;
20951 if (font == NULL)
20953 font = FRAME_FONT (it->f);
20954 boff = FRAME_BASELINE_OFFSET (it->f);
20955 font_info = NULL;
20957 else
20959 font_info
20960 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20961 boff = font_info->baseline_offset;
20962 if (font_info->vertical_centering)
20963 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20966 if (font_info
20967 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20968 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20970 width = pcm->width;
20971 ascent = pcm->ascent;
20972 descent = pcm->descent;
20974 else
20976 width = FONT_WIDTH (font);
20977 ascent = 1;
20978 descent = 0;
20981 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20983 /* Relative composition with or without
20984 alternate chars. */
20985 left = (leftmost + rightmost - width) / 2;
20986 btm = - descent + boff;
20987 if (font_info && font_info->relative_compose
20988 && (! CHAR_TABLE_P (Vignore_relative_composition)
20989 || NILP (Faref (Vignore_relative_composition,
20990 make_number (ch)))))
20993 if (- descent >= font_info->relative_compose)
20994 /* One extra pixel between two glyphs. */
20995 btm = highest + 1;
20996 else if (ascent <= 0)
20997 /* One extra pixel between two glyphs. */
20998 btm = lowest - 1 - ascent - descent;
21001 else
21003 /* A composition rule is specified by an integer
21004 value that encodes global and new reference
21005 points (GREF and NREF). GREF and NREF are
21006 specified by numbers as below:
21008 0---1---2 -- ascent
21012 9--10--11 -- center
21014 ---3---4---5--- baseline
21016 6---7---8 -- descent
21018 int rule = COMPOSITION_RULE (cmp, i);
21019 int gref, nref, grefx, grefy, nrefx, nrefy;
21021 COMPOSITION_DECODE_RULE (rule, gref, nref);
21022 grefx = gref % 3, nrefx = nref % 3;
21023 grefy = gref / 3, nrefy = nref / 3;
21025 left = (leftmost
21026 + grefx * (rightmost - leftmost) / 2
21027 - nrefx * width / 2);
21028 btm = ((grefy == 0 ? highest
21029 : grefy == 1 ? 0
21030 : grefy == 2 ? lowest
21031 : (highest + lowest) / 2)
21032 - (nrefy == 0 ? ascent + descent
21033 : nrefy == 1 ? descent - boff
21034 : nrefy == 2 ? 0
21035 : (ascent + descent) / 2));
21038 cmp->offsets[i * 2] = left;
21039 cmp->offsets[i * 2 + 1] = btm + descent;
21041 /* Update the bounding box of the overall glyphs. */
21042 right = left + width;
21043 top = btm + descent + ascent;
21044 if (left < leftmost)
21045 leftmost = left;
21046 if (right > rightmost)
21047 rightmost = right;
21048 if (top > highest)
21049 highest = top;
21050 if (btm < lowest)
21051 lowest = btm;
21054 /* If there are glyphs whose x-offsets are negative,
21055 shift all glyphs to the right and make all x-offsets
21056 non-negative. */
21057 if (leftmost < 0)
21059 for (i = 0; i < cmp->glyph_len; i++)
21060 cmp->offsets[i * 2] -= leftmost;
21061 rightmost -= leftmost;
21064 cmp->pixel_width = rightmost;
21065 cmp->ascent = highest;
21066 cmp->descent = - lowest;
21067 if (cmp->ascent < font_ascent)
21068 cmp->ascent = font_ascent;
21069 if (cmp->descent < font_descent)
21070 cmp->descent = font_descent;
21073 it->pixel_width = cmp->pixel_width;
21074 it->ascent = it->phys_ascent = cmp->ascent;
21075 it->descent = it->phys_descent = cmp->descent;
21077 if (face->box != FACE_NO_BOX)
21079 int thick = face->box_line_width;
21081 if (thick > 0)
21083 it->ascent += thick;
21084 it->descent += thick;
21086 else
21087 thick = - thick;
21089 if (it->start_of_box_run_p)
21090 it->pixel_width += thick;
21091 if (it->end_of_box_run_p)
21092 it->pixel_width += thick;
21095 /* If face has an overline, add the height of the overline
21096 (1 pixel) and a 1 pixel margin to the character height. */
21097 if (face->overline_p)
21098 it->ascent += overline_margin;
21100 take_vertical_position_into_account (it);
21102 if (it->glyph_row)
21103 append_composite_glyph (it);
21105 else if (it->what == IT_IMAGE)
21106 produce_image_glyph (it);
21107 else if (it->what == IT_STRETCH)
21108 produce_stretch_glyph (it);
21110 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21111 because this isn't true for images with `:ascent 100'. */
21112 xassert (it->ascent >= 0 && it->descent >= 0);
21113 if (it->area == TEXT_AREA)
21114 it->current_x += it->pixel_width;
21116 if (extra_line_spacing > 0)
21118 it->descent += extra_line_spacing;
21119 if (extra_line_spacing > it->max_extra_line_spacing)
21120 it->max_extra_line_spacing = extra_line_spacing;
21123 it->max_ascent = max (it->max_ascent, it->ascent);
21124 it->max_descent = max (it->max_descent, it->descent);
21125 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21126 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21129 /* EXPORT for RIF:
21130 Output LEN glyphs starting at START at the nominal cursor position.
21131 Advance the nominal cursor over the text. The global variable
21132 updated_window contains the window being updated, updated_row is
21133 the glyph row being updated, and updated_area is the area of that
21134 row being updated. */
21136 void
21137 x_write_glyphs (start, len)
21138 struct glyph *start;
21139 int len;
21141 int x, hpos;
21143 xassert (updated_window && updated_row);
21144 BLOCK_INPUT;
21146 /* Write glyphs. */
21148 hpos = start - updated_row->glyphs[updated_area];
21149 x = draw_glyphs (updated_window, output_cursor.x,
21150 updated_row, updated_area,
21151 hpos, hpos + len,
21152 DRAW_NORMAL_TEXT, 0);
21154 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21155 if (updated_area == TEXT_AREA
21156 && updated_window->phys_cursor_on_p
21157 && updated_window->phys_cursor.vpos == output_cursor.vpos
21158 && updated_window->phys_cursor.hpos >= hpos
21159 && updated_window->phys_cursor.hpos < hpos + len)
21160 updated_window->phys_cursor_on_p = 0;
21162 UNBLOCK_INPUT;
21164 /* Advance the output cursor. */
21165 output_cursor.hpos += len;
21166 output_cursor.x = x;
21170 /* EXPORT for RIF:
21171 Insert LEN glyphs from START at the nominal cursor position. */
21173 void
21174 x_insert_glyphs (start, len)
21175 struct glyph *start;
21176 int len;
21178 struct frame *f;
21179 struct window *w;
21180 int line_height, shift_by_width, shifted_region_width;
21181 struct glyph_row *row;
21182 struct glyph *glyph;
21183 int frame_x, frame_y, hpos;
21185 xassert (updated_window && updated_row);
21186 BLOCK_INPUT;
21187 w = updated_window;
21188 f = XFRAME (WINDOW_FRAME (w));
21190 /* Get the height of the line we are in. */
21191 row = updated_row;
21192 line_height = row->height;
21194 /* Get the width of the glyphs to insert. */
21195 shift_by_width = 0;
21196 for (glyph = start; glyph < start + len; ++glyph)
21197 shift_by_width += glyph->pixel_width;
21199 /* Get the width of the region to shift right. */
21200 shifted_region_width = (window_box_width (w, updated_area)
21201 - output_cursor.x
21202 - shift_by_width);
21204 /* Shift right. */
21205 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21206 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21208 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21209 line_height, shift_by_width);
21211 /* Write the glyphs. */
21212 hpos = start - row->glyphs[updated_area];
21213 draw_glyphs (w, output_cursor.x, row, updated_area,
21214 hpos, hpos + len,
21215 DRAW_NORMAL_TEXT, 0);
21217 /* Advance the output cursor. */
21218 output_cursor.hpos += len;
21219 output_cursor.x += shift_by_width;
21220 UNBLOCK_INPUT;
21224 /* EXPORT for RIF:
21225 Erase the current text line from the nominal cursor position
21226 (inclusive) to pixel column TO_X (exclusive). The idea is that
21227 everything from TO_X onward is already erased.
21229 TO_X is a pixel position relative to updated_area of
21230 updated_window. TO_X == -1 means clear to the end of this area. */
21232 void
21233 x_clear_end_of_line (to_x)
21234 int to_x;
21236 struct frame *f;
21237 struct window *w = updated_window;
21238 int max_x, min_y, max_y;
21239 int from_x, from_y, to_y;
21241 xassert (updated_window && updated_row);
21242 f = XFRAME (w->frame);
21244 if (updated_row->full_width_p)
21245 max_x = WINDOW_TOTAL_WIDTH (w);
21246 else
21247 max_x = window_box_width (w, updated_area);
21248 max_y = window_text_bottom_y (w);
21250 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21251 of window. For TO_X > 0, truncate to end of drawing area. */
21252 if (to_x == 0)
21253 return;
21254 else if (to_x < 0)
21255 to_x = max_x;
21256 else
21257 to_x = min (to_x, max_x);
21259 to_y = min (max_y, output_cursor.y + updated_row->height);
21261 /* Notice if the cursor will be cleared by this operation. */
21262 if (!updated_row->full_width_p)
21263 notice_overwritten_cursor (w, updated_area,
21264 output_cursor.x, -1,
21265 updated_row->y,
21266 MATRIX_ROW_BOTTOM_Y (updated_row));
21268 from_x = output_cursor.x;
21270 /* Translate to frame coordinates. */
21271 if (updated_row->full_width_p)
21273 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21274 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21276 else
21278 int area_left = window_box_left (w, updated_area);
21279 from_x += area_left;
21280 to_x += area_left;
21283 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21284 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21285 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21287 /* Prevent inadvertently clearing to end of the X window. */
21288 if (to_x > from_x && to_y > from_y)
21290 BLOCK_INPUT;
21291 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21292 to_x - from_x, to_y - from_y);
21293 UNBLOCK_INPUT;
21297 #endif /* HAVE_WINDOW_SYSTEM */
21301 /***********************************************************************
21302 Cursor types
21303 ***********************************************************************/
21305 /* Value is the internal representation of the specified cursor type
21306 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21307 of the bar cursor. */
21309 static enum text_cursor_kinds
21310 get_specified_cursor_type (arg, width)
21311 Lisp_Object arg;
21312 int *width;
21314 enum text_cursor_kinds type;
21316 if (NILP (arg))
21317 return NO_CURSOR;
21319 if (EQ (arg, Qbox))
21320 return FILLED_BOX_CURSOR;
21322 if (EQ (arg, Qhollow))
21323 return HOLLOW_BOX_CURSOR;
21325 if (EQ (arg, Qbar))
21327 *width = 2;
21328 return BAR_CURSOR;
21331 if (CONSP (arg)
21332 && EQ (XCAR (arg), Qbar)
21333 && INTEGERP (XCDR (arg))
21334 && XINT (XCDR (arg)) >= 0)
21336 *width = XINT (XCDR (arg));
21337 return BAR_CURSOR;
21340 if (EQ (arg, Qhbar))
21342 *width = 2;
21343 return HBAR_CURSOR;
21346 if (CONSP (arg)
21347 && EQ (XCAR (arg), Qhbar)
21348 && INTEGERP (XCDR (arg))
21349 && XINT (XCDR (arg)) >= 0)
21351 *width = XINT (XCDR (arg));
21352 return HBAR_CURSOR;
21355 /* Treat anything unknown as "hollow box cursor".
21356 It was bad to signal an error; people have trouble fixing
21357 .Xdefaults with Emacs, when it has something bad in it. */
21358 type = HOLLOW_BOX_CURSOR;
21360 return type;
21363 /* Set the default cursor types for specified frame. */
21364 void
21365 set_frame_cursor_types (f, arg)
21366 struct frame *f;
21367 Lisp_Object arg;
21369 int width;
21370 Lisp_Object tem;
21372 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21373 FRAME_CURSOR_WIDTH (f) = width;
21375 /* By default, set up the blink-off state depending on the on-state. */
21377 tem = Fassoc (arg, Vblink_cursor_alist);
21378 if (!NILP (tem))
21380 FRAME_BLINK_OFF_CURSOR (f)
21381 = get_specified_cursor_type (XCDR (tem), &width);
21382 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21384 else
21385 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21389 /* Return the cursor we want to be displayed in window W. Return
21390 width of bar/hbar cursor through WIDTH arg. Return with
21391 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21392 (i.e. if the `system caret' should track this cursor).
21394 In a mini-buffer window, we want the cursor only to appear if we
21395 are reading input from this window. For the selected window, we
21396 want the cursor type given by the frame parameter or buffer local
21397 setting of cursor-type. If explicitly marked off, draw no cursor.
21398 In all other cases, we want a hollow box cursor. */
21400 static enum text_cursor_kinds
21401 get_window_cursor_type (w, glyph, width, active_cursor)
21402 struct window *w;
21403 struct glyph *glyph;
21404 int *width;
21405 int *active_cursor;
21407 struct frame *f = XFRAME (w->frame);
21408 struct buffer *b = XBUFFER (w->buffer);
21409 int cursor_type = DEFAULT_CURSOR;
21410 Lisp_Object alt_cursor;
21411 int non_selected = 0;
21413 *active_cursor = 1;
21415 /* Echo area */
21416 if (cursor_in_echo_area
21417 && FRAME_HAS_MINIBUF_P (f)
21418 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21420 if (w == XWINDOW (echo_area_window))
21422 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21424 *width = FRAME_CURSOR_WIDTH (f);
21425 return FRAME_DESIRED_CURSOR (f);
21427 else
21428 return get_specified_cursor_type (b->cursor_type, width);
21431 *active_cursor = 0;
21432 non_selected = 1;
21435 /* Detect a nonselected window or nonselected frame. */
21436 else if (w != XWINDOW (f->selected_window)
21437 #ifdef HAVE_WINDOW_SYSTEM
21438 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21439 #endif
21442 *active_cursor = 0;
21444 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21445 return NO_CURSOR;
21447 non_selected = 1;
21450 /* Never display a cursor in a window in which cursor-type is nil. */
21451 if (NILP (b->cursor_type))
21452 return NO_CURSOR;
21454 /* Get the normal cursor type for this window. */
21455 if (EQ (b->cursor_type, Qt))
21457 cursor_type = FRAME_DESIRED_CURSOR (f);
21458 *width = FRAME_CURSOR_WIDTH (f);
21460 else
21461 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21463 /* Use cursor-in-non-selected-windows instead
21464 for non-selected window or frame. */
21465 if (non_selected)
21467 alt_cursor = b->cursor_in_non_selected_windows;
21468 if (!EQ (Qt, alt_cursor))
21469 return get_specified_cursor_type (alt_cursor, width);
21470 /* t means modify the normal cursor type. */
21471 if (cursor_type == FILLED_BOX_CURSOR)
21472 cursor_type = HOLLOW_BOX_CURSOR;
21473 else if (cursor_type == BAR_CURSOR && *width > 1)
21474 --*width;
21475 return cursor_type;
21478 /* Use normal cursor if not blinked off. */
21479 if (!w->cursor_off_p)
21481 #ifdef HAVE_WINDOW_SYSTEM
21482 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21484 if (cursor_type == FILLED_BOX_CURSOR)
21486 /* Using a block cursor on large images can be very annoying.
21487 So use a hollow cursor for "large" images.
21488 If image is not transparent (no mask), also use hollow cursor. */
21489 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21490 if (img != NULL && IMAGEP (img->spec))
21492 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21493 where N = size of default frame font size.
21494 This should cover most of the "tiny" icons people may use. */
21495 if (!img->mask
21496 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21497 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21498 cursor_type = HOLLOW_BOX_CURSOR;
21501 else if (cursor_type != NO_CURSOR)
21503 /* Display current only supports BOX and HOLLOW cursors for images.
21504 So for now, unconditionally use a HOLLOW cursor when cursor is
21505 not a solid box cursor. */
21506 cursor_type = HOLLOW_BOX_CURSOR;
21509 #endif
21510 return cursor_type;
21513 /* Cursor is blinked off, so determine how to "toggle" it. */
21515 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21516 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21517 return get_specified_cursor_type (XCDR (alt_cursor), width);
21519 /* Then see if frame has specified a specific blink off cursor type. */
21520 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21522 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21523 return FRAME_BLINK_OFF_CURSOR (f);
21526 #if 0
21527 /* Some people liked having a permanently visible blinking cursor,
21528 while others had very strong opinions against it. So it was
21529 decided to remove it. KFS 2003-09-03 */
21531 /* Finally perform built-in cursor blinking:
21532 filled box <-> hollow box
21533 wide [h]bar <-> narrow [h]bar
21534 narrow [h]bar <-> no cursor
21535 other type <-> no cursor */
21537 if (cursor_type == FILLED_BOX_CURSOR)
21538 return HOLLOW_BOX_CURSOR;
21540 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21542 *width = 1;
21543 return cursor_type;
21545 #endif
21547 return NO_CURSOR;
21551 #ifdef HAVE_WINDOW_SYSTEM
21553 /* Notice when the text cursor of window W has been completely
21554 overwritten by a drawing operation that outputs glyphs in AREA
21555 starting at X0 and ending at X1 in the line starting at Y0 and
21556 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21557 the rest of the line after X0 has been written. Y coordinates
21558 are window-relative. */
21560 static void
21561 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21562 struct window *w;
21563 enum glyph_row_area area;
21564 int x0, y0, x1, y1;
21566 int cx0, cx1, cy0, cy1;
21567 struct glyph_row *row;
21569 if (!w->phys_cursor_on_p)
21570 return;
21571 if (area != TEXT_AREA)
21572 return;
21574 if (w->phys_cursor.vpos < 0
21575 || w->phys_cursor.vpos >= w->current_matrix->nrows
21576 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21577 !(row->enabled_p && row->displays_text_p)))
21578 return;
21580 if (row->cursor_in_fringe_p)
21582 row->cursor_in_fringe_p = 0;
21583 draw_fringe_bitmap (w, row, 0);
21584 w->phys_cursor_on_p = 0;
21585 return;
21588 cx0 = w->phys_cursor.x;
21589 cx1 = cx0 + w->phys_cursor_width;
21590 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21591 return;
21593 /* The cursor image will be completely removed from the
21594 screen if the output area intersects the cursor area in
21595 y-direction. When we draw in [y0 y1[, and some part of
21596 the cursor is at y < y0, that part must have been drawn
21597 before. When scrolling, the cursor is erased before
21598 actually scrolling, so we don't come here. When not
21599 scrolling, the rows above the old cursor row must have
21600 changed, and in this case these rows must have written
21601 over the cursor image.
21603 Likewise if part of the cursor is below y1, with the
21604 exception of the cursor being in the first blank row at
21605 the buffer and window end because update_text_area
21606 doesn't draw that row. (Except when it does, but
21607 that's handled in update_text_area.) */
21609 cy0 = w->phys_cursor.y;
21610 cy1 = cy0 + w->phys_cursor_height;
21611 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21612 return;
21614 w->phys_cursor_on_p = 0;
21617 #endif /* HAVE_WINDOW_SYSTEM */
21620 /************************************************************************
21621 Mouse Face
21622 ************************************************************************/
21624 #ifdef HAVE_WINDOW_SYSTEM
21626 /* EXPORT for RIF:
21627 Fix the display of area AREA of overlapping row ROW in window W
21628 with respect to the overlapping part OVERLAPS. */
21630 void
21631 x_fix_overlapping_area (w, row, area, overlaps)
21632 struct window *w;
21633 struct glyph_row *row;
21634 enum glyph_row_area area;
21635 int overlaps;
21637 int i, x;
21639 BLOCK_INPUT;
21641 x = 0;
21642 for (i = 0; i < row->used[area];)
21644 if (row->glyphs[area][i].overlaps_vertically_p)
21646 int start = i, start_x = x;
21650 x += row->glyphs[area][i].pixel_width;
21651 ++i;
21653 while (i < row->used[area]
21654 && row->glyphs[area][i].overlaps_vertically_p);
21656 draw_glyphs (w, start_x, row, area,
21657 start, i,
21658 DRAW_NORMAL_TEXT, overlaps);
21660 else
21662 x += row->glyphs[area][i].pixel_width;
21663 ++i;
21667 UNBLOCK_INPUT;
21671 /* EXPORT:
21672 Draw the cursor glyph of window W in glyph row ROW. See the
21673 comment of draw_glyphs for the meaning of HL. */
21675 void
21676 draw_phys_cursor_glyph (w, row, hl)
21677 struct window *w;
21678 struct glyph_row *row;
21679 enum draw_glyphs_face hl;
21681 /* If cursor hpos is out of bounds, don't draw garbage. This can
21682 happen in mini-buffer windows when switching between echo area
21683 glyphs and mini-buffer. */
21684 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21686 int on_p = w->phys_cursor_on_p;
21687 int x1;
21688 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21689 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21690 hl, 0);
21691 w->phys_cursor_on_p = on_p;
21693 if (hl == DRAW_CURSOR)
21694 w->phys_cursor_width = x1 - w->phys_cursor.x;
21695 /* When we erase the cursor, and ROW is overlapped by other
21696 rows, make sure that these overlapping parts of other rows
21697 are redrawn. */
21698 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21700 w->phys_cursor_width = x1 - w->phys_cursor.x;
21702 if (row > w->current_matrix->rows
21703 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21704 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21705 OVERLAPS_ERASED_CURSOR);
21707 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21708 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21709 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21710 OVERLAPS_ERASED_CURSOR);
21716 /* EXPORT:
21717 Erase the image of a cursor of window W from the screen. */
21719 void
21720 erase_phys_cursor (w)
21721 struct window *w;
21723 struct frame *f = XFRAME (w->frame);
21724 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21725 int hpos = w->phys_cursor.hpos;
21726 int vpos = w->phys_cursor.vpos;
21727 int mouse_face_here_p = 0;
21728 struct glyph_matrix *active_glyphs = w->current_matrix;
21729 struct glyph_row *cursor_row;
21730 struct glyph *cursor_glyph;
21731 enum draw_glyphs_face hl;
21733 /* No cursor displayed or row invalidated => nothing to do on the
21734 screen. */
21735 if (w->phys_cursor_type == NO_CURSOR)
21736 goto mark_cursor_off;
21738 /* VPOS >= active_glyphs->nrows means that window has been resized.
21739 Don't bother to erase the cursor. */
21740 if (vpos >= active_glyphs->nrows)
21741 goto mark_cursor_off;
21743 /* If row containing cursor is marked invalid, there is nothing we
21744 can do. */
21745 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21746 if (!cursor_row->enabled_p)
21747 goto mark_cursor_off;
21749 /* If line spacing is > 0, old cursor may only be partially visible in
21750 window after split-window. So adjust visible height. */
21751 cursor_row->visible_height = min (cursor_row->visible_height,
21752 window_text_bottom_y (w) - cursor_row->y);
21754 /* If row is completely invisible, don't attempt to delete a cursor which
21755 isn't there. This can happen if cursor is at top of a window, and
21756 we switch to a buffer with a header line in that window. */
21757 if (cursor_row->visible_height <= 0)
21758 goto mark_cursor_off;
21760 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21761 if (cursor_row->cursor_in_fringe_p)
21763 cursor_row->cursor_in_fringe_p = 0;
21764 draw_fringe_bitmap (w, cursor_row, 0);
21765 goto mark_cursor_off;
21768 /* This can happen when the new row is shorter than the old one.
21769 In this case, either draw_glyphs or clear_end_of_line
21770 should have cleared the cursor. Note that we wouldn't be
21771 able to erase the cursor in this case because we don't have a
21772 cursor glyph at hand. */
21773 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21774 goto mark_cursor_off;
21776 /* If the cursor is in the mouse face area, redisplay that when
21777 we clear the cursor. */
21778 if (! NILP (dpyinfo->mouse_face_window)
21779 && w == XWINDOW (dpyinfo->mouse_face_window)
21780 && (vpos > dpyinfo->mouse_face_beg_row
21781 || (vpos == dpyinfo->mouse_face_beg_row
21782 && hpos >= dpyinfo->mouse_face_beg_col))
21783 && (vpos < dpyinfo->mouse_face_end_row
21784 || (vpos == dpyinfo->mouse_face_end_row
21785 && hpos < dpyinfo->mouse_face_end_col))
21786 /* Don't redraw the cursor's spot in mouse face if it is at the
21787 end of a line (on a newline). The cursor appears there, but
21788 mouse highlighting does not. */
21789 && cursor_row->used[TEXT_AREA] > hpos)
21790 mouse_face_here_p = 1;
21792 /* Maybe clear the display under the cursor. */
21793 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21795 int x, y, left_x;
21796 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21797 int width;
21799 cursor_glyph = get_phys_cursor_glyph (w);
21800 if (cursor_glyph == NULL)
21801 goto mark_cursor_off;
21803 width = cursor_glyph->pixel_width;
21804 left_x = window_box_left_offset (w, TEXT_AREA);
21805 x = w->phys_cursor.x;
21806 if (x < left_x)
21807 width -= left_x - x;
21808 width = min (width, window_box_width (w, TEXT_AREA) - x);
21809 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21810 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21812 if (width > 0)
21813 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21816 /* Erase the cursor by redrawing the character underneath it. */
21817 if (mouse_face_here_p)
21818 hl = DRAW_MOUSE_FACE;
21819 else
21820 hl = DRAW_NORMAL_TEXT;
21821 draw_phys_cursor_glyph (w, cursor_row, hl);
21823 mark_cursor_off:
21824 w->phys_cursor_on_p = 0;
21825 w->phys_cursor_type = NO_CURSOR;
21829 /* EXPORT:
21830 Display or clear cursor of window W. If ON is zero, clear the
21831 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21832 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21834 void
21835 display_and_set_cursor (w, on, hpos, vpos, x, y)
21836 struct window *w;
21837 int on, hpos, vpos, x, y;
21839 struct frame *f = XFRAME (w->frame);
21840 int new_cursor_type;
21841 int new_cursor_width;
21842 int active_cursor;
21843 struct glyph_row *glyph_row;
21844 struct glyph *glyph;
21846 /* This is pointless on invisible frames, and dangerous on garbaged
21847 windows and frames; in the latter case, the frame or window may
21848 be in the midst of changing its size, and x and y may be off the
21849 window. */
21850 if (! FRAME_VISIBLE_P (f)
21851 || FRAME_GARBAGED_P (f)
21852 || vpos >= w->current_matrix->nrows
21853 || hpos >= w->current_matrix->matrix_w)
21854 return;
21856 /* If cursor is off and we want it off, return quickly. */
21857 if (!on && !w->phys_cursor_on_p)
21858 return;
21860 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21861 /* If cursor row is not enabled, we don't really know where to
21862 display the cursor. */
21863 if (!glyph_row->enabled_p)
21865 w->phys_cursor_on_p = 0;
21866 return;
21869 glyph = NULL;
21870 if (!glyph_row->exact_window_width_line_p
21871 || hpos < glyph_row->used[TEXT_AREA])
21872 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21874 xassert (interrupt_input_blocked);
21876 /* Set new_cursor_type to the cursor we want to be displayed. */
21877 new_cursor_type = get_window_cursor_type (w, glyph,
21878 &new_cursor_width, &active_cursor);
21880 /* If cursor is currently being shown and we don't want it to be or
21881 it is in the wrong place, or the cursor type is not what we want,
21882 erase it. */
21883 if (w->phys_cursor_on_p
21884 && (!on
21885 || w->phys_cursor.x != x
21886 || w->phys_cursor.y != y
21887 || new_cursor_type != w->phys_cursor_type
21888 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21889 && new_cursor_width != w->phys_cursor_width)))
21890 erase_phys_cursor (w);
21892 /* Don't check phys_cursor_on_p here because that flag is only set
21893 to zero in some cases where we know that the cursor has been
21894 completely erased, to avoid the extra work of erasing the cursor
21895 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21896 still not be visible, or it has only been partly erased. */
21897 if (on)
21899 w->phys_cursor_ascent = glyph_row->ascent;
21900 w->phys_cursor_height = glyph_row->height;
21902 /* Set phys_cursor_.* before x_draw_.* is called because some
21903 of them may need the information. */
21904 w->phys_cursor.x = x;
21905 w->phys_cursor.y = glyph_row->y;
21906 w->phys_cursor.hpos = hpos;
21907 w->phys_cursor.vpos = vpos;
21910 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21911 new_cursor_type, new_cursor_width,
21912 on, active_cursor);
21916 /* Switch the display of W's cursor on or off, according to the value
21917 of ON. */
21919 static void
21920 update_window_cursor (w, on)
21921 struct window *w;
21922 int on;
21924 /* Don't update cursor in windows whose frame is in the process
21925 of being deleted. */
21926 if (w->current_matrix)
21928 BLOCK_INPUT;
21929 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21930 w->phys_cursor.x, w->phys_cursor.y);
21931 UNBLOCK_INPUT;
21936 /* Call update_window_cursor with parameter ON_P on all leaf windows
21937 in the window tree rooted at W. */
21939 static void
21940 update_cursor_in_window_tree (w, on_p)
21941 struct window *w;
21942 int on_p;
21944 while (w)
21946 if (!NILP (w->hchild))
21947 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21948 else if (!NILP (w->vchild))
21949 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21950 else
21951 update_window_cursor (w, on_p);
21953 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21958 /* EXPORT:
21959 Display the cursor on window W, or clear it, according to ON_P.
21960 Don't change the cursor's position. */
21962 void
21963 x_update_cursor (f, on_p)
21964 struct frame *f;
21965 int on_p;
21967 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21971 /* EXPORT:
21972 Clear the cursor of window W to background color, and mark the
21973 cursor as not shown. This is used when the text where the cursor
21974 is is about to be rewritten. */
21976 void
21977 x_clear_cursor (w)
21978 struct window *w;
21980 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21981 update_window_cursor (w, 0);
21985 /* EXPORT:
21986 Display the active region described by mouse_face_* according to DRAW. */
21988 void
21989 show_mouse_face (dpyinfo, draw)
21990 Display_Info *dpyinfo;
21991 enum draw_glyphs_face draw;
21993 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21994 struct frame *f = XFRAME (WINDOW_FRAME (w));
21996 if (/* If window is in the process of being destroyed, don't bother
21997 to do anything. */
21998 w->current_matrix != NULL
21999 /* Don't update mouse highlight if hidden */
22000 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22001 /* Recognize when we are called to operate on rows that don't exist
22002 anymore. This can happen when a window is split. */
22003 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22005 int phys_cursor_on_p = w->phys_cursor_on_p;
22006 struct glyph_row *row, *first, *last;
22008 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22009 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22011 for (row = first; row <= last && row->enabled_p; ++row)
22013 int start_hpos, end_hpos, start_x;
22015 /* For all but the first row, the highlight starts at column 0. */
22016 if (row == first)
22018 start_hpos = dpyinfo->mouse_face_beg_col;
22019 start_x = dpyinfo->mouse_face_beg_x;
22021 else
22023 start_hpos = 0;
22024 start_x = 0;
22027 if (row == last)
22028 end_hpos = dpyinfo->mouse_face_end_col;
22029 else
22031 end_hpos = row->used[TEXT_AREA];
22032 if (draw == DRAW_NORMAL_TEXT)
22033 row->fill_line_p = 1; /* Clear to end of line */
22036 if (end_hpos > start_hpos)
22038 draw_glyphs (w, start_x, row, TEXT_AREA,
22039 start_hpos, end_hpos,
22040 draw, 0);
22042 row->mouse_face_p
22043 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22047 /* When we've written over the cursor, arrange for it to
22048 be displayed again. */
22049 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22051 BLOCK_INPUT;
22052 display_and_set_cursor (w, 1,
22053 w->phys_cursor.hpos, w->phys_cursor.vpos,
22054 w->phys_cursor.x, w->phys_cursor.y);
22055 UNBLOCK_INPUT;
22059 /* Change the mouse cursor. */
22060 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22061 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22062 else if (draw == DRAW_MOUSE_FACE)
22063 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22064 else
22065 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22068 /* EXPORT:
22069 Clear out the mouse-highlighted active region.
22070 Redraw it un-highlighted first. Value is non-zero if mouse
22071 face was actually drawn unhighlighted. */
22074 clear_mouse_face (dpyinfo)
22075 Display_Info *dpyinfo;
22077 int cleared = 0;
22079 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22081 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22082 cleared = 1;
22085 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22086 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22087 dpyinfo->mouse_face_window = Qnil;
22088 dpyinfo->mouse_face_overlay = Qnil;
22089 return cleared;
22093 /* EXPORT:
22094 Non-zero if physical cursor of window W is within mouse face. */
22097 cursor_in_mouse_face_p (w)
22098 struct window *w;
22100 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22101 int in_mouse_face = 0;
22103 if (WINDOWP (dpyinfo->mouse_face_window)
22104 && XWINDOW (dpyinfo->mouse_face_window) == w)
22106 int hpos = w->phys_cursor.hpos;
22107 int vpos = w->phys_cursor.vpos;
22109 if (vpos >= dpyinfo->mouse_face_beg_row
22110 && vpos <= dpyinfo->mouse_face_end_row
22111 && (vpos > dpyinfo->mouse_face_beg_row
22112 || hpos >= dpyinfo->mouse_face_beg_col)
22113 && (vpos < dpyinfo->mouse_face_end_row
22114 || hpos < dpyinfo->mouse_face_end_col
22115 || dpyinfo->mouse_face_past_end))
22116 in_mouse_face = 1;
22119 return in_mouse_face;
22125 /* Find the glyph matrix position of buffer position CHARPOS in window
22126 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22127 current glyphs must be up to date. If CHARPOS is above window
22128 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22129 of last line in W. In the row containing CHARPOS, stop before glyphs
22130 having STOP as object. */
22132 #if 1 /* This is a version of fast_find_position that's more correct
22133 in the presence of hscrolling, for example. I didn't install
22134 it right away because the problem fixed is minor, it failed
22135 in 20.x as well, and I think it's too risky to install
22136 so near the release of 21.1. 2001-09-25 gerd. */
22138 static int
22139 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22140 struct window *w;
22141 int charpos;
22142 int *hpos, *vpos, *x, *y;
22143 Lisp_Object stop;
22145 struct glyph_row *row, *first;
22146 struct glyph *glyph, *end;
22147 int past_end = 0;
22149 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22150 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22152 *x = first->x;
22153 *y = first->y;
22154 *hpos = 0;
22155 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22156 return 1;
22159 row = row_containing_pos (w, charpos, first, NULL, 0);
22160 if (row == NULL)
22162 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22163 past_end = 1;
22166 /* If whole rows or last part of a row came from a display overlay,
22167 row_containing_pos will skip over such rows because their end pos
22168 equals the start pos of the overlay or interval.
22170 Move back if we have a STOP object and previous row's
22171 end glyph came from STOP. */
22172 if (!NILP (stop))
22174 struct glyph_row *prev;
22175 while ((prev = row - 1, prev >= first)
22176 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22177 && prev->used[TEXT_AREA] > 0)
22179 struct glyph *beg = prev->glyphs[TEXT_AREA];
22180 glyph = beg + prev->used[TEXT_AREA];
22181 while (--glyph >= beg
22182 && INTEGERP (glyph->object));
22183 if (glyph < beg
22184 || !EQ (stop, glyph->object))
22185 break;
22186 row = prev;
22190 *x = row->x;
22191 *y = row->y;
22192 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22194 glyph = row->glyphs[TEXT_AREA];
22195 end = glyph + row->used[TEXT_AREA];
22197 /* Skip over glyphs not having an object at the start of the row.
22198 These are special glyphs like truncation marks on terminal
22199 frames. */
22200 if (row->displays_text_p)
22201 while (glyph < end
22202 && INTEGERP (glyph->object)
22203 && !EQ (stop, glyph->object)
22204 && glyph->charpos < 0)
22206 *x += glyph->pixel_width;
22207 ++glyph;
22210 while (glyph < end
22211 && !INTEGERP (glyph->object)
22212 && !EQ (stop, glyph->object)
22213 && (!BUFFERP (glyph->object)
22214 || glyph->charpos < charpos))
22216 *x += glyph->pixel_width;
22217 ++glyph;
22220 *hpos = glyph - row->glyphs[TEXT_AREA];
22221 return !past_end;
22224 #else /* not 1 */
22226 static int
22227 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22228 struct window *w;
22229 int pos;
22230 int *hpos, *vpos, *x, *y;
22231 Lisp_Object stop;
22233 int i;
22234 int lastcol;
22235 int maybe_next_line_p = 0;
22236 int line_start_position;
22237 int yb = window_text_bottom_y (w);
22238 struct glyph_row *row, *best_row;
22239 int row_vpos, best_row_vpos;
22240 int current_x;
22242 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22243 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22245 while (row->y < yb)
22247 if (row->used[TEXT_AREA])
22248 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22249 else
22250 line_start_position = 0;
22252 if (line_start_position > pos)
22253 break;
22254 /* If the position sought is the end of the buffer,
22255 don't include the blank lines at the bottom of the window. */
22256 else if (line_start_position == pos
22257 && pos == BUF_ZV (XBUFFER (w->buffer)))
22259 maybe_next_line_p = 1;
22260 break;
22262 else if (line_start_position > 0)
22264 best_row = row;
22265 best_row_vpos = row_vpos;
22268 if (row->y + row->height >= yb)
22269 break;
22271 ++row;
22272 ++row_vpos;
22275 /* Find the right column within BEST_ROW. */
22276 lastcol = 0;
22277 current_x = best_row->x;
22278 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22280 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22281 int charpos = glyph->charpos;
22283 if (BUFFERP (glyph->object))
22285 if (charpos == pos)
22287 *hpos = i;
22288 *vpos = best_row_vpos;
22289 *x = current_x;
22290 *y = best_row->y;
22291 return 1;
22293 else if (charpos > pos)
22294 break;
22296 else if (EQ (glyph->object, stop))
22297 break;
22299 if (charpos > 0)
22300 lastcol = i;
22301 current_x += glyph->pixel_width;
22304 /* If we're looking for the end of the buffer,
22305 and we didn't find it in the line we scanned,
22306 use the start of the following line. */
22307 if (maybe_next_line_p)
22309 ++best_row;
22310 ++best_row_vpos;
22311 lastcol = 0;
22312 current_x = best_row->x;
22315 *vpos = best_row_vpos;
22316 *hpos = lastcol + 1;
22317 *x = current_x;
22318 *y = best_row->y;
22319 return 0;
22322 #endif /* not 1 */
22325 /* Find the position of the glyph for position POS in OBJECT in
22326 window W's current matrix, and return in *X, *Y the pixel
22327 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22329 RIGHT_P non-zero means return the position of the right edge of the
22330 glyph, RIGHT_P zero means return the left edge position.
22332 If no glyph for POS exists in the matrix, return the position of
22333 the glyph with the next smaller position that is in the matrix, if
22334 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22335 exists in the matrix, return the position of the glyph with the
22336 next larger position in OBJECT.
22338 Value is non-zero if a glyph was found. */
22340 static int
22341 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22342 struct window *w;
22343 int pos;
22344 Lisp_Object object;
22345 int *hpos, *vpos, *x, *y;
22346 int right_p;
22348 int yb = window_text_bottom_y (w);
22349 struct glyph_row *r;
22350 struct glyph *best_glyph = NULL;
22351 struct glyph_row *best_row = NULL;
22352 int best_x = 0;
22354 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22355 r->enabled_p && r->y < yb;
22356 ++r)
22358 struct glyph *g = r->glyphs[TEXT_AREA];
22359 struct glyph *e = g + r->used[TEXT_AREA];
22360 int gx;
22362 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22363 if (EQ (g->object, object))
22365 if (g->charpos == pos)
22367 best_glyph = g;
22368 best_x = gx;
22369 best_row = r;
22370 goto found;
22372 else if (best_glyph == NULL
22373 || ((eabs (g->charpos - pos)
22374 < eabs (best_glyph->charpos - pos))
22375 && (right_p
22376 ? g->charpos < pos
22377 : g->charpos > pos)))
22379 best_glyph = g;
22380 best_x = gx;
22381 best_row = r;
22386 found:
22388 if (best_glyph)
22390 *x = best_x;
22391 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22393 if (right_p)
22395 *x += best_glyph->pixel_width;
22396 ++*hpos;
22399 *y = best_row->y;
22400 *vpos = best_row - w->current_matrix->rows;
22403 return best_glyph != NULL;
22407 /* See if position X, Y is within a hot-spot of an image. */
22409 static int
22410 on_hot_spot_p (hot_spot, x, y)
22411 Lisp_Object hot_spot;
22412 int x, y;
22414 if (!CONSP (hot_spot))
22415 return 0;
22417 if (EQ (XCAR (hot_spot), Qrect))
22419 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22420 Lisp_Object rect = XCDR (hot_spot);
22421 Lisp_Object tem;
22422 if (!CONSP (rect))
22423 return 0;
22424 if (!CONSP (XCAR (rect)))
22425 return 0;
22426 if (!CONSP (XCDR (rect)))
22427 return 0;
22428 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22429 return 0;
22430 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22431 return 0;
22432 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22433 return 0;
22434 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22435 return 0;
22436 return 1;
22438 else if (EQ (XCAR (hot_spot), Qcircle))
22440 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22441 Lisp_Object circ = XCDR (hot_spot);
22442 Lisp_Object lr, lx0, ly0;
22443 if (CONSP (circ)
22444 && CONSP (XCAR (circ))
22445 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22446 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22447 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22449 double r = XFLOATINT (lr);
22450 double dx = XINT (lx0) - x;
22451 double dy = XINT (ly0) - y;
22452 return (dx * dx + dy * dy <= r * r);
22455 else if (EQ (XCAR (hot_spot), Qpoly))
22457 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22458 if (VECTORP (XCDR (hot_spot)))
22460 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22461 Lisp_Object *poly = v->contents;
22462 int n = v->size;
22463 int i;
22464 int inside = 0;
22465 Lisp_Object lx, ly;
22466 int x0, y0;
22468 /* Need an even number of coordinates, and at least 3 edges. */
22469 if (n < 6 || n & 1)
22470 return 0;
22472 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22473 If count is odd, we are inside polygon. Pixels on edges
22474 may or may not be included depending on actual geometry of the
22475 polygon. */
22476 if ((lx = poly[n-2], !INTEGERP (lx))
22477 || (ly = poly[n-1], !INTEGERP (lx)))
22478 return 0;
22479 x0 = XINT (lx), y0 = XINT (ly);
22480 for (i = 0; i < n; i += 2)
22482 int x1 = x0, y1 = y0;
22483 if ((lx = poly[i], !INTEGERP (lx))
22484 || (ly = poly[i+1], !INTEGERP (ly)))
22485 return 0;
22486 x0 = XINT (lx), y0 = XINT (ly);
22488 /* Does this segment cross the X line? */
22489 if (x0 >= x)
22491 if (x1 >= x)
22492 continue;
22494 else if (x1 < x)
22495 continue;
22496 if (y > y0 && y > y1)
22497 continue;
22498 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22499 inside = !inside;
22501 return inside;
22504 return 0;
22507 Lisp_Object
22508 find_hot_spot (map, x, y)
22509 Lisp_Object map;
22510 int x, y;
22512 while (CONSP (map))
22514 if (CONSP (XCAR (map))
22515 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22516 return XCAR (map);
22517 map = XCDR (map);
22520 return Qnil;
22523 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22524 3, 3, 0,
22525 doc: /* Lookup in image map MAP coordinates X and Y.
22526 An image map is an alist where each element has the format (AREA ID PLIST).
22527 An AREA is specified as either a rectangle, a circle, or a polygon:
22528 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22529 pixel coordinates of the upper left and bottom right corners.
22530 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22531 and the radius of the circle; r may be a float or integer.
22532 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22533 vector describes one corner in the polygon.
22534 Returns the alist element for the first matching AREA in MAP. */)
22535 (map, x, y)
22536 Lisp_Object map;
22537 Lisp_Object x, y;
22539 if (NILP (map))
22540 return Qnil;
22542 CHECK_NUMBER (x);
22543 CHECK_NUMBER (y);
22545 return find_hot_spot (map, XINT (x), XINT (y));
22549 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22550 static void
22551 define_frame_cursor1 (f, cursor, pointer)
22552 struct frame *f;
22553 Cursor cursor;
22554 Lisp_Object pointer;
22556 /* Do not change cursor shape while dragging mouse. */
22557 if (!NILP (do_mouse_tracking))
22558 return;
22560 if (!NILP (pointer))
22562 if (EQ (pointer, Qarrow))
22563 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22564 else if (EQ (pointer, Qhand))
22565 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22566 else if (EQ (pointer, Qtext))
22567 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22568 else if (EQ (pointer, intern ("hdrag")))
22569 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22570 #ifdef HAVE_X_WINDOWS
22571 else if (EQ (pointer, intern ("vdrag")))
22572 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22573 #endif
22574 else if (EQ (pointer, intern ("hourglass")))
22575 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22576 else if (EQ (pointer, Qmodeline))
22577 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22578 else
22579 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22582 if (cursor != No_Cursor)
22583 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22586 /* Take proper action when mouse has moved to the mode or header line
22587 or marginal area AREA of window W, x-position X and y-position Y.
22588 X is relative to the start of the text display area of W, so the
22589 width of bitmap areas and scroll bars must be subtracted to get a
22590 position relative to the start of the mode line. */
22592 static void
22593 note_mode_line_or_margin_highlight (window, x, y, area)
22594 Lisp_Object window;
22595 int x, y;
22596 enum window_part area;
22598 struct window *w = XWINDOW (window);
22599 struct frame *f = XFRAME (w->frame);
22600 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22601 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22602 Lisp_Object pointer = Qnil;
22603 int charpos, dx, dy, width, height;
22604 Lisp_Object string, object = Qnil;
22605 Lisp_Object pos, help;
22607 Lisp_Object mouse_face;
22608 int original_x_pixel = x;
22609 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22610 struct glyph_row *row;
22612 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22614 int x0;
22615 struct glyph *end;
22617 string = mode_line_string (w, area, &x, &y, &charpos,
22618 &object, &dx, &dy, &width, &height);
22620 row = (area == ON_MODE_LINE
22621 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22622 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22624 /* Find glyph */
22625 if (row->mode_line_p && row->enabled_p)
22627 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22628 end = glyph + row->used[TEXT_AREA];
22630 for (x0 = original_x_pixel;
22631 glyph < end && x0 >= glyph->pixel_width;
22632 ++glyph)
22633 x0 -= glyph->pixel_width;
22635 if (glyph >= end)
22636 glyph = NULL;
22639 else
22641 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22642 string = marginal_area_string (w, area, &x, &y, &charpos,
22643 &object, &dx, &dy, &width, &height);
22646 help = Qnil;
22648 if (IMAGEP (object))
22650 Lisp_Object image_map, hotspot;
22651 if ((image_map = Fplist_get (XCDR (object), QCmap),
22652 !NILP (image_map))
22653 && (hotspot = find_hot_spot (image_map, dx, dy),
22654 CONSP (hotspot))
22655 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22657 Lisp_Object area_id, plist;
22659 area_id = XCAR (hotspot);
22660 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22661 If so, we could look for mouse-enter, mouse-leave
22662 properties in PLIST (and do something...). */
22663 hotspot = XCDR (hotspot);
22664 if (CONSP (hotspot)
22665 && (plist = XCAR (hotspot), CONSP (plist)))
22667 pointer = Fplist_get (plist, Qpointer);
22668 if (NILP (pointer))
22669 pointer = Qhand;
22670 help = Fplist_get (plist, Qhelp_echo);
22671 if (!NILP (help))
22673 help_echo_string = help;
22674 /* Is this correct? ++kfs */
22675 XSETWINDOW (help_echo_window, w);
22676 help_echo_object = w->buffer;
22677 help_echo_pos = charpos;
22681 if (NILP (pointer))
22682 pointer = Fplist_get (XCDR (object), QCpointer);
22685 if (STRINGP (string))
22687 pos = make_number (charpos);
22688 /* If we're on a string with `help-echo' text property, arrange
22689 for the help to be displayed. This is done by setting the
22690 global variable help_echo_string to the help string. */
22691 if (NILP (help))
22693 help = Fget_text_property (pos, Qhelp_echo, string);
22694 if (!NILP (help))
22696 help_echo_string = help;
22697 XSETWINDOW (help_echo_window, w);
22698 help_echo_object = string;
22699 help_echo_pos = charpos;
22703 if (NILP (pointer))
22704 pointer = Fget_text_property (pos, Qpointer, string);
22706 /* Change the mouse pointer according to what is under X/Y. */
22707 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22709 Lisp_Object map;
22710 map = Fget_text_property (pos, Qlocal_map, string);
22711 if (!KEYMAPP (map))
22712 map = Fget_text_property (pos, Qkeymap, string);
22713 if (!KEYMAPP (map))
22714 cursor = dpyinfo->vertical_scroll_bar_cursor;
22717 /* Change the mouse face according to what is under X/Y. */
22718 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22719 if (!NILP (mouse_face)
22720 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22721 && glyph)
22723 Lisp_Object b, e;
22725 struct glyph * tmp_glyph;
22727 int gpos;
22728 int gseq_length;
22729 int total_pixel_width;
22730 int ignore;
22732 int vpos, hpos;
22734 b = Fprevious_single_property_change (make_number (charpos + 1),
22735 Qmouse_face, string, Qnil);
22736 if (NILP (b))
22737 b = make_number (0);
22739 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22740 if (NILP (e))
22741 e = make_number (SCHARS (string));
22743 /* Calculate the position(glyph position: GPOS) of GLYPH in
22744 displayed string. GPOS is different from CHARPOS.
22746 CHARPOS is the position of glyph in internal string
22747 object. A mode line string format has structures which
22748 is converted to a flatten by emacs lisp interpreter.
22749 The internal string is an element of the structures.
22750 The displayed string is the flatten string. */
22751 gpos = 0;
22752 if (glyph > row_start_glyph)
22754 tmp_glyph = glyph - 1;
22755 while (tmp_glyph >= row_start_glyph
22756 && tmp_glyph->charpos >= XINT (b)
22757 && EQ (tmp_glyph->object, glyph->object))
22759 tmp_glyph--;
22760 gpos++;
22764 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22765 displayed string holding GLYPH.
22767 GSEQ_LENGTH is different from SCHARS (STRING).
22768 SCHARS (STRING) returns the length of the internal string. */
22769 for (tmp_glyph = glyph, gseq_length = gpos;
22770 tmp_glyph->charpos < XINT (e);
22771 tmp_glyph++, gseq_length++)
22773 if (!EQ (tmp_glyph->object, glyph->object))
22774 break;
22777 total_pixel_width = 0;
22778 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22779 total_pixel_width += tmp_glyph->pixel_width;
22781 /* Pre calculation of re-rendering position */
22782 vpos = (x - gpos);
22783 hpos = (area == ON_MODE_LINE
22784 ? (w->current_matrix)->nrows - 1
22785 : 0);
22787 /* If the re-rendering position is included in the last
22788 re-rendering area, we should do nothing. */
22789 if ( EQ (window, dpyinfo->mouse_face_window)
22790 && dpyinfo->mouse_face_beg_col <= vpos
22791 && vpos < dpyinfo->mouse_face_end_col
22792 && dpyinfo->mouse_face_beg_row == hpos )
22793 return;
22795 if (clear_mouse_face (dpyinfo))
22796 cursor = No_Cursor;
22798 dpyinfo->mouse_face_beg_col = vpos;
22799 dpyinfo->mouse_face_beg_row = hpos;
22801 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22802 dpyinfo->mouse_face_beg_y = 0;
22804 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22805 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22807 dpyinfo->mouse_face_end_x = 0;
22808 dpyinfo->mouse_face_end_y = 0;
22810 dpyinfo->mouse_face_past_end = 0;
22811 dpyinfo->mouse_face_window = window;
22813 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22814 charpos,
22815 0, 0, 0, &ignore,
22816 glyph->face_id, 1);
22817 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22819 if (NILP (pointer))
22820 pointer = Qhand;
22822 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22823 clear_mouse_face (dpyinfo);
22825 define_frame_cursor1 (f, cursor, pointer);
22829 /* EXPORT:
22830 Take proper action when the mouse has moved to position X, Y on
22831 frame F as regards highlighting characters that have mouse-face
22832 properties. Also de-highlighting chars where the mouse was before.
22833 X and Y can be negative or out of range. */
22835 void
22836 note_mouse_highlight (f, x, y)
22837 struct frame *f;
22838 int x, y;
22840 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22841 enum window_part part;
22842 Lisp_Object window;
22843 struct window *w;
22844 Cursor cursor = No_Cursor;
22845 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22846 struct buffer *b;
22848 /* When a menu is active, don't highlight because this looks odd. */
22849 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22850 if (popup_activated ())
22851 return;
22852 #endif
22854 if (NILP (Vmouse_highlight)
22855 || !f->glyphs_initialized_p)
22856 return;
22858 dpyinfo->mouse_face_mouse_x = x;
22859 dpyinfo->mouse_face_mouse_y = y;
22860 dpyinfo->mouse_face_mouse_frame = f;
22862 if (dpyinfo->mouse_face_defer)
22863 return;
22865 if (gc_in_progress)
22867 dpyinfo->mouse_face_deferred_gc = 1;
22868 return;
22871 /* Which window is that in? */
22872 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22874 /* If we were displaying active text in another window, clear that.
22875 Also clear if we move out of text area in same window. */
22876 if (! EQ (window, dpyinfo->mouse_face_window)
22877 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22878 && !NILP (dpyinfo->mouse_face_window)))
22879 clear_mouse_face (dpyinfo);
22881 /* Not on a window -> return. */
22882 if (!WINDOWP (window))
22883 return;
22885 /* Reset help_echo_string. It will get recomputed below. */
22886 help_echo_string = Qnil;
22888 /* Convert to window-relative pixel coordinates. */
22889 w = XWINDOW (window);
22890 frame_to_window_pixel_xy (w, &x, &y);
22892 /* Handle tool-bar window differently since it doesn't display a
22893 buffer. */
22894 if (EQ (window, f->tool_bar_window))
22896 note_tool_bar_highlight (f, x, y);
22897 return;
22900 /* Mouse is on the mode, header line or margin? */
22901 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22902 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22904 note_mode_line_or_margin_highlight (window, x, y, part);
22905 return;
22908 if (part == ON_VERTICAL_BORDER)
22910 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22911 help_echo_string = build_string ("drag-mouse-1: resize");
22913 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22914 || part == ON_SCROLL_BAR)
22915 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22916 else
22917 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22919 /* Are we in a window whose display is up to date?
22920 And verify the buffer's text has not changed. */
22921 b = XBUFFER (w->buffer);
22922 if (part == ON_TEXT
22923 && EQ (w->window_end_valid, w->buffer)
22924 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22925 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22927 int hpos, vpos, pos, i, dx, dy, area;
22928 struct glyph *glyph;
22929 Lisp_Object object;
22930 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22931 Lisp_Object *overlay_vec = NULL;
22932 int noverlays;
22933 struct buffer *obuf;
22934 int obegv, ozv, same_region;
22936 /* Find the glyph under X/Y. */
22937 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22939 /* Look for :pointer property on image. */
22940 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22942 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22943 if (img != NULL && IMAGEP (img->spec))
22945 Lisp_Object image_map, hotspot;
22946 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22947 !NILP (image_map))
22948 && (hotspot = find_hot_spot (image_map,
22949 glyph->slice.x + dx,
22950 glyph->slice.y + dy),
22951 CONSP (hotspot))
22952 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22954 Lisp_Object area_id, plist;
22956 area_id = XCAR (hotspot);
22957 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22958 If so, we could look for mouse-enter, mouse-leave
22959 properties in PLIST (and do something...). */
22960 hotspot = XCDR (hotspot);
22961 if (CONSP (hotspot)
22962 && (plist = XCAR (hotspot), CONSP (plist)))
22964 pointer = Fplist_get (plist, Qpointer);
22965 if (NILP (pointer))
22966 pointer = Qhand;
22967 help_echo_string = Fplist_get (plist, Qhelp_echo);
22968 if (!NILP (help_echo_string))
22970 help_echo_window = window;
22971 help_echo_object = glyph->object;
22972 help_echo_pos = glyph->charpos;
22976 if (NILP (pointer))
22977 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22981 /* Clear mouse face if X/Y not over text. */
22982 if (glyph == NULL
22983 || area != TEXT_AREA
22984 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22986 if (clear_mouse_face (dpyinfo))
22987 cursor = No_Cursor;
22988 if (NILP (pointer))
22990 if (area != TEXT_AREA)
22991 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22992 else
22993 pointer = Vvoid_text_area_pointer;
22995 goto set_cursor;
22998 pos = glyph->charpos;
22999 object = glyph->object;
23000 if (!STRINGP (object) && !BUFFERP (object))
23001 goto set_cursor;
23003 /* If we get an out-of-range value, return now; avoid an error. */
23004 if (BUFFERP (object) && pos > BUF_Z (b))
23005 goto set_cursor;
23007 /* Make the window's buffer temporarily current for
23008 overlays_at and compute_char_face. */
23009 obuf = current_buffer;
23010 current_buffer = b;
23011 obegv = BEGV;
23012 ozv = ZV;
23013 BEGV = BEG;
23014 ZV = Z;
23016 /* Is this char mouse-active or does it have help-echo? */
23017 position = make_number (pos);
23019 if (BUFFERP (object))
23021 /* Put all the overlays we want in a vector in overlay_vec. */
23022 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23023 /* Sort overlays into increasing priority order. */
23024 noverlays = sort_overlays (overlay_vec, noverlays, w);
23026 else
23027 noverlays = 0;
23029 same_region = (EQ (window, dpyinfo->mouse_face_window)
23030 && vpos >= dpyinfo->mouse_face_beg_row
23031 && vpos <= dpyinfo->mouse_face_end_row
23032 && (vpos > dpyinfo->mouse_face_beg_row
23033 || hpos >= dpyinfo->mouse_face_beg_col)
23034 && (vpos < dpyinfo->mouse_face_end_row
23035 || hpos < dpyinfo->mouse_face_end_col
23036 || dpyinfo->mouse_face_past_end));
23038 if (same_region)
23039 cursor = No_Cursor;
23041 /* Check mouse-face highlighting. */
23042 if (! same_region
23043 /* If there exists an overlay with mouse-face overlapping
23044 the one we are currently highlighting, we have to
23045 check if we enter the overlapping overlay, and then
23046 highlight only that. */
23047 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23048 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23050 /* Find the highest priority overlay that has a mouse-face
23051 property. */
23052 overlay = Qnil;
23053 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23055 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23056 if (!NILP (mouse_face))
23057 overlay = overlay_vec[i];
23060 /* If we're actually highlighting the same overlay as
23061 before, there's no need to do that again. */
23062 if (!NILP (overlay)
23063 && EQ (overlay, dpyinfo->mouse_face_overlay))
23064 goto check_help_echo;
23066 dpyinfo->mouse_face_overlay = overlay;
23068 /* Clear the display of the old active region, if any. */
23069 if (clear_mouse_face (dpyinfo))
23070 cursor = No_Cursor;
23072 /* If no overlay applies, get a text property. */
23073 if (NILP (overlay))
23074 mouse_face = Fget_text_property (position, Qmouse_face, object);
23076 /* Handle the overlay case. */
23077 if (!NILP (overlay))
23079 /* Find the range of text around this char that
23080 should be active. */
23081 Lisp_Object before, after;
23082 int ignore;
23084 before = Foverlay_start (overlay);
23085 after = Foverlay_end (overlay);
23086 /* Record this as the current active region. */
23087 fast_find_position (w, XFASTINT (before),
23088 &dpyinfo->mouse_face_beg_col,
23089 &dpyinfo->mouse_face_beg_row,
23090 &dpyinfo->mouse_face_beg_x,
23091 &dpyinfo->mouse_face_beg_y, Qnil);
23093 dpyinfo->mouse_face_past_end
23094 = !fast_find_position (w, XFASTINT (after),
23095 &dpyinfo->mouse_face_end_col,
23096 &dpyinfo->mouse_face_end_row,
23097 &dpyinfo->mouse_face_end_x,
23098 &dpyinfo->mouse_face_end_y, Qnil);
23099 dpyinfo->mouse_face_window = window;
23101 dpyinfo->mouse_face_face_id
23102 = face_at_buffer_position (w, pos, 0, 0,
23103 &ignore, pos + 1,
23104 !dpyinfo->mouse_face_hidden);
23106 /* Display it as active. */
23107 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23108 cursor = No_Cursor;
23110 /* Handle the text property case. */
23111 else if (!NILP (mouse_face) && BUFFERP (object))
23113 /* Find the range of text around this char that
23114 should be active. */
23115 Lisp_Object before, after, beginning, end;
23116 int ignore;
23118 beginning = Fmarker_position (w->start);
23119 end = make_number (BUF_Z (XBUFFER (object))
23120 - XFASTINT (w->window_end_pos));
23121 before
23122 = Fprevious_single_property_change (make_number (pos + 1),
23123 Qmouse_face,
23124 object, beginning);
23125 after
23126 = Fnext_single_property_change (position, Qmouse_face,
23127 object, end);
23129 /* Record this as the current active region. */
23130 fast_find_position (w, XFASTINT (before),
23131 &dpyinfo->mouse_face_beg_col,
23132 &dpyinfo->mouse_face_beg_row,
23133 &dpyinfo->mouse_face_beg_x,
23134 &dpyinfo->mouse_face_beg_y, Qnil);
23135 dpyinfo->mouse_face_past_end
23136 = !fast_find_position (w, XFASTINT (after),
23137 &dpyinfo->mouse_face_end_col,
23138 &dpyinfo->mouse_face_end_row,
23139 &dpyinfo->mouse_face_end_x,
23140 &dpyinfo->mouse_face_end_y, Qnil);
23141 dpyinfo->mouse_face_window = window;
23143 if (BUFFERP (object))
23144 dpyinfo->mouse_face_face_id
23145 = face_at_buffer_position (w, pos, 0, 0,
23146 &ignore, pos + 1,
23147 !dpyinfo->mouse_face_hidden);
23149 /* Display it as active. */
23150 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23151 cursor = No_Cursor;
23153 else if (!NILP (mouse_face) && STRINGP (object))
23155 Lisp_Object b, e;
23156 int ignore;
23158 b = Fprevious_single_property_change (make_number (pos + 1),
23159 Qmouse_face,
23160 object, Qnil);
23161 e = Fnext_single_property_change (position, Qmouse_face,
23162 object, Qnil);
23163 if (NILP (b))
23164 b = make_number (0);
23165 if (NILP (e))
23166 e = make_number (SCHARS (object) - 1);
23168 fast_find_string_pos (w, XINT (b), object,
23169 &dpyinfo->mouse_face_beg_col,
23170 &dpyinfo->mouse_face_beg_row,
23171 &dpyinfo->mouse_face_beg_x,
23172 &dpyinfo->mouse_face_beg_y, 0);
23173 fast_find_string_pos (w, XINT (e), object,
23174 &dpyinfo->mouse_face_end_col,
23175 &dpyinfo->mouse_face_end_row,
23176 &dpyinfo->mouse_face_end_x,
23177 &dpyinfo->mouse_face_end_y, 1);
23178 dpyinfo->mouse_face_past_end = 0;
23179 dpyinfo->mouse_face_window = window;
23180 dpyinfo->mouse_face_face_id
23181 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23182 glyph->face_id, 1);
23183 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23184 cursor = No_Cursor;
23186 else if (STRINGP (object) && NILP (mouse_face))
23188 /* A string which doesn't have mouse-face, but
23189 the text ``under'' it might have. */
23190 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23191 int start = MATRIX_ROW_START_CHARPOS (r);
23193 pos = string_buffer_position (w, object, start);
23194 if (pos > 0)
23195 mouse_face = get_char_property_and_overlay (make_number (pos),
23196 Qmouse_face,
23197 w->buffer,
23198 &overlay);
23199 if (!NILP (mouse_face) && !NILP (overlay))
23201 Lisp_Object before = Foverlay_start (overlay);
23202 Lisp_Object after = Foverlay_end (overlay);
23203 int ignore;
23205 /* Note that we might not be able to find position
23206 BEFORE in the glyph matrix if the overlay is
23207 entirely covered by a `display' property. In
23208 this case, we overshoot. So let's stop in
23209 the glyph matrix before glyphs for OBJECT. */
23210 fast_find_position (w, XFASTINT (before),
23211 &dpyinfo->mouse_face_beg_col,
23212 &dpyinfo->mouse_face_beg_row,
23213 &dpyinfo->mouse_face_beg_x,
23214 &dpyinfo->mouse_face_beg_y,
23215 object);
23217 dpyinfo->mouse_face_past_end
23218 = !fast_find_position (w, XFASTINT (after),
23219 &dpyinfo->mouse_face_end_col,
23220 &dpyinfo->mouse_face_end_row,
23221 &dpyinfo->mouse_face_end_x,
23222 &dpyinfo->mouse_face_end_y,
23223 Qnil);
23224 dpyinfo->mouse_face_window = window;
23225 dpyinfo->mouse_face_face_id
23226 = face_at_buffer_position (w, pos, 0, 0,
23227 &ignore, pos + 1,
23228 !dpyinfo->mouse_face_hidden);
23230 /* Display it as active. */
23231 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23232 cursor = No_Cursor;
23237 check_help_echo:
23239 /* Look for a `help-echo' property. */
23240 if (NILP (help_echo_string)) {
23241 Lisp_Object help, overlay;
23243 /* Check overlays first. */
23244 help = overlay = Qnil;
23245 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23247 overlay = overlay_vec[i];
23248 help = Foverlay_get (overlay, Qhelp_echo);
23251 if (!NILP (help))
23253 help_echo_string = help;
23254 help_echo_window = window;
23255 help_echo_object = overlay;
23256 help_echo_pos = pos;
23258 else
23260 Lisp_Object object = glyph->object;
23261 int charpos = glyph->charpos;
23263 /* Try text properties. */
23264 if (STRINGP (object)
23265 && charpos >= 0
23266 && charpos < SCHARS (object))
23268 help = Fget_text_property (make_number (charpos),
23269 Qhelp_echo, object);
23270 if (NILP (help))
23272 /* If the string itself doesn't specify a help-echo,
23273 see if the buffer text ``under'' it does. */
23274 struct glyph_row *r
23275 = MATRIX_ROW (w->current_matrix, vpos);
23276 int start = MATRIX_ROW_START_CHARPOS (r);
23277 int pos = string_buffer_position (w, object, start);
23278 if (pos > 0)
23280 help = Fget_char_property (make_number (pos),
23281 Qhelp_echo, w->buffer);
23282 if (!NILP (help))
23284 charpos = pos;
23285 object = w->buffer;
23290 else if (BUFFERP (object)
23291 && charpos >= BEGV
23292 && charpos < ZV)
23293 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23294 object);
23296 if (!NILP (help))
23298 help_echo_string = help;
23299 help_echo_window = window;
23300 help_echo_object = object;
23301 help_echo_pos = charpos;
23306 /* Look for a `pointer' property. */
23307 if (NILP (pointer))
23309 /* Check overlays first. */
23310 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23311 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23313 if (NILP (pointer))
23315 Lisp_Object object = glyph->object;
23316 int charpos = glyph->charpos;
23318 /* Try text properties. */
23319 if (STRINGP (object)
23320 && charpos >= 0
23321 && charpos < SCHARS (object))
23323 pointer = Fget_text_property (make_number (charpos),
23324 Qpointer, object);
23325 if (NILP (pointer))
23327 /* If the string itself doesn't specify a pointer,
23328 see if the buffer text ``under'' it does. */
23329 struct glyph_row *r
23330 = MATRIX_ROW (w->current_matrix, vpos);
23331 int start = MATRIX_ROW_START_CHARPOS (r);
23332 int pos = string_buffer_position (w, object, start);
23333 if (pos > 0)
23334 pointer = Fget_char_property (make_number (pos),
23335 Qpointer, w->buffer);
23338 else if (BUFFERP (object)
23339 && charpos >= BEGV
23340 && charpos < ZV)
23341 pointer = Fget_text_property (make_number (charpos),
23342 Qpointer, object);
23346 BEGV = obegv;
23347 ZV = ozv;
23348 current_buffer = obuf;
23351 set_cursor:
23353 define_frame_cursor1 (f, cursor, pointer);
23357 /* EXPORT for RIF:
23358 Clear any mouse-face on window W. This function is part of the
23359 redisplay interface, and is called from try_window_id and similar
23360 functions to ensure the mouse-highlight is off. */
23362 void
23363 x_clear_window_mouse_face (w)
23364 struct window *w;
23366 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23367 Lisp_Object window;
23369 BLOCK_INPUT;
23370 XSETWINDOW (window, w);
23371 if (EQ (window, dpyinfo->mouse_face_window))
23372 clear_mouse_face (dpyinfo);
23373 UNBLOCK_INPUT;
23377 /* EXPORT:
23378 Just discard the mouse face information for frame F, if any.
23379 This is used when the size of F is changed. */
23381 void
23382 cancel_mouse_face (f)
23383 struct frame *f;
23385 Lisp_Object window;
23386 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23388 window = dpyinfo->mouse_face_window;
23389 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23391 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23392 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23393 dpyinfo->mouse_face_window = Qnil;
23398 #endif /* HAVE_WINDOW_SYSTEM */
23401 /***********************************************************************
23402 Exposure Events
23403 ***********************************************************************/
23405 #ifdef HAVE_WINDOW_SYSTEM
23407 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23408 which intersects rectangle R. R is in window-relative coordinates. */
23410 static void
23411 expose_area (w, row, r, area)
23412 struct window *w;
23413 struct glyph_row *row;
23414 XRectangle *r;
23415 enum glyph_row_area area;
23417 struct glyph *first = row->glyphs[area];
23418 struct glyph *end = row->glyphs[area] + row->used[area];
23419 struct glyph *last;
23420 int first_x, start_x, x;
23422 if (area == TEXT_AREA && row->fill_line_p)
23423 /* If row extends face to end of line write the whole line. */
23424 draw_glyphs (w, 0, row, area,
23425 0, row->used[area],
23426 DRAW_NORMAL_TEXT, 0);
23427 else
23429 /* Set START_X to the window-relative start position for drawing glyphs of
23430 AREA. The first glyph of the text area can be partially visible.
23431 The first glyphs of other areas cannot. */
23432 start_x = window_box_left_offset (w, area);
23433 x = start_x;
23434 if (area == TEXT_AREA)
23435 x += row->x;
23437 /* Find the first glyph that must be redrawn. */
23438 while (first < end
23439 && x + first->pixel_width < r->x)
23441 x += first->pixel_width;
23442 ++first;
23445 /* Find the last one. */
23446 last = first;
23447 first_x = x;
23448 while (last < end
23449 && x < r->x + r->width)
23451 x += last->pixel_width;
23452 ++last;
23455 /* Repaint. */
23456 if (last > first)
23457 draw_glyphs (w, first_x - start_x, row, area,
23458 first - row->glyphs[area], last - row->glyphs[area],
23459 DRAW_NORMAL_TEXT, 0);
23464 /* Redraw the parts of the glyph row ROW on window W intersecting
23465 rectangle R. R is in window-relative coordinates. Value is
23466 non-zero if mouse-face was overwritten. */
23468 static int
23469 expose_line (w, row, r)
23470 struct window *w;
23471 struct glyph_row *row;
23472 XRectangle *r;
23474 xassert (row->enabled_p);
23476 if (row->mode_line_p || w->pseudo_window_p)
23477 draw_glyphs (w, 0, row, TEXT_AREA,
23478 0, row->used[TEXT_AREA],
23479 DRAW_NORMAL_TEXT, 0);
23480 else
23482 if (row->used[LEFT_MARGIN_AREA])
23483 expose_area (w, row, r, LEFT_MARGIN_AREA);
23484 if (row->used[TEXT_AREA])
23485 expose_area (w, row, r, TEXT_AREA);
23486 if (row->used[RIGHT_MARGIN_AREA])
23487 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23488 draw_row_fringe_bitmaps (w, row);
23491 return row->mouse_face_p;
23495 /* Redraw those parts of glyphs rows during expose event handling that
23496 overlap other rows. Redrawing of an exposed line writes over parts
23497 of lines overlapping that exposed line; this function fixes that.
23499 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23500 row in W's current matrix that is exposed and overlaps other rows.
23501 LAST_OVERLAPPING_ROW is the last such row. */
23503 static void
23504 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23505 struct window *w;
23506 struct glyph_row *first_overlapping_row;
23507 struct glyph_row *last_overlapping_row;
23509 struct glyph_row *row;
23511 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23512 if (row->overlapping_p)
23514 xassert (row->enabled_p && !row->mode_line_p);
23516 if (row->used[LEFT_MARGIN_AREA])
23517 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23519 if (row->used[TEXT_AREA])
23520 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23522 if (row->used[RIGHT_MARGIN_AREA])
23523 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23528 /* Return non-zero if W's cursor intersects rectangle R. */
23530 static int
23531 phys_cursor_in_rect_p (w, r)
23532 struct window *w;
23533 XRectangle *r;
23535 XRectangle cr, result;
23536 struct glyph *cursor_glyph;
23538 cursor_glyph = get_phys_cursor_glyph (w);
23539 if (cursor_glyph)
23541 /* r is relative to W's box, but w->phys_cursor.x is relative
23542 to left edge of W's TEXT area. Adjust it. */
23543 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23544 cr.y = w->phys_cursor.y;
23545 cr.width = cursor_glyph->pixel_width;
23546 cr.height = w->phys_cursor_height;
23547 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23548 I assume the effect is the same -- and this is portable. */
23549 return x_intersect_rectangles (&cr, r, &result);
23551 /* If we don't understand the format, pretend we're not in the hot-spot. */
23552 return 0;
23556 /* EXPORT:
23557 Draw a vertical window border to the right of window W if W doesn't
23558 have vertical scroll bars. */
23560 void
23561 x_draw_vertical_border (w)
23562 struct window *w;
23564 struct frame *f = XFRAME (WINDOW_FRAME (w));
23566 /* We could do better, if we knew what type of scroll-bar the adjacent
23567 windows (on either side) have... But we don't :-(
23568 However, I think this works ok. ++KFS 2003-04-25 */
23570 /* Redraw borders between horizontally adjacent windows. Don't
23571 do it for frames with vertical scroll bars because either the
23572 right scroll bar of a window, or the left scroll bar of its
23573 neighbor will suffice as a border. */
23574 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23575 return;
23577 if (!WINDOW_RIGHTMOST_P (w)
23578 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23580 int x0, x1, y0, y1;
23582 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23583 y1 -= 1;
23585 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23586 x1 -= 1;
23588 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23590 else if (!WINDOW_LEFTMOST_P (w)
23591 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23593 int x0, x1, y0, y1;
23595 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23596 y1 -= 1;
23598 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23599 x0 -= 1;
23601 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23606 /* Redraw the part of window W intersection rectangle FR. Pixel
23607 coordinates in FR are frame-relative. Call this function with
23608 input blocked. Value is non-zero if the exposure overwrites
23609 mouse-face. */
23611 static int
23612 expose_window (w, fr)
23613 struct window *w;
23614 XRectangle *fr;
23616 struct frame *f = XFRAME (w->frame);
23617 XRectangle wr, r;
23618 int mouse_face_overwritten_p = 0;
23620 /* If window is not yet fully initialized, do nothing. This can
23621 happen when toolkit scroll bars are used and a window is split.
23622 Reconfiguring the scroll bar will generate an expose for a newly
23623 created window. */
23624 if (w->current_matrix == NULL)
23625 return 0;
23627 /* When we're currently updating the window, display and current
23628 matrix usually don't agree. Arrange for a thorough display
23629 later. */
23630 if (w == updated_window)
23632 SET_FRAME_GARBAGED (f);
23633 return 0;
23636 /* Frame-relative pixel rectangle of W. */
23637 wr.x = WINDOW_LEFT_EDGE_X (w);
23638 wr.y = WINDOW_TOP_EDGE_Y (w);
23639 wr.width = WINDOW_TOTAL_WIDTH (w);
23640 wr.height = WINDOW_TOTAL_HEIGHT (w);
23642 if (x_intersect_rectangles (fr, &wr, &r))
23644 int yb = window_text_bottom_y (w);
23645 struct glyph_row *row;
23646 int cursor_cleared_p;
23647 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23649 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23650 r.x, r.y, r.width, r.height));
23652 /* Convert to window coordinates. */
23653 r.x -= WINDOW_LEFT_EDGE_X (w);
23654 r.y -= WINDOW_TOP_EDGE_Y (w);
23656 /* Turn off the cursor. */
23657 if (!w->pseudo_window_p
23658 && phys_cursor_in_rect_p (w, &r))
23660 x_clear_cursor (w);
23661 cursor_cleared_p = 1;
23663 else
23664 cursor_cleared_p = 0;
23666 /* Update lines intersecting rectangle R. */
23667 first_overlapping_row = last_overlapping_row = NULL;
23668 for (row = w->current_matrix->rows;
23669 row->enabled_p;
23670 ++row)
23672 int y0 = row->y;
23673 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23675 if ((y0 >= r.y && y0 < r.y + r.height)
23676 || (y1 > r.y && y1 < r.y + r.height)
23677 || (r.y >= y0 && r.y < y1)
23678 || (r.y + r.height > y0 && r.y + r.height < y1))
23680 /* A header line may be overlapping, but there is no need
23681 to fix overlapping areas for them. KFS 2005-02-12 */
23682 if (row->overlapping_p && !row->mode_line_p)
23684 if (first_overlapping_row == NULL)
23685 first_overlapping_row = row;
23686 last_overlapping_row = row;
23689 if (expose_line (w, row, &r))
23690 mouse_face_overwritten_p = 1;
23693 if (y1 >= yb)
23694 break;
23697 /* Display the mode line if there is one. */
23698 if (WINDOW_WANTS_MODELINE_P (w)
23699 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23700 row->enabled_p)
23701 && row->y < r.y + r.height)
23703 if (expose_line (w, row, &r))
23704 mouse_face_overwritten_p = 1;
23707 if (!w->pseudo_window_p)
23709 /* Fix the display of overlapping rows. */
23710 if (first_overlapping_row)
23711 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23713 /* Draw border between windows. */
23714 x_draw_vertical_border (w);
23716 /* Turn the cursor on again. */
23717 if (cursor_cleared_p)
23718 update_window_cursor (w, 1);
23722 return mouse_face_overwritten_p;
23727 /* Redraw (parts) of all windows in the window tree rooted at W that
23728 intersect R. R contains frame pixel coordinates. Value is
23729 non-zero if the exposure overwrites mouse-face. */
23731 static int
23732 expose_window_tree (w, r)
23733 struct window *w;
23734 XRectangle *r;
23736 struct frame *f = XFRAME (w->frame);
23737 int mouse_face_overwritten_p = 0;
23739 while (w && !FRAME_GARBAGED_P (f))
23741 if (!NILP (w->hchild))
23742 mouse_face_overwritten_p
23743 |= expose_window_tree (XWINDOW (w->hchild), r);
23744 else if (!NILP (w->vchild))
23745 mouse_face_overwritten_p
23746 |= expose_window_tree (XWINDOW (w->vchild), r);
23747 else
23748 mouse_face_overwritten_p |= expose_window (w, r);
23750 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23753 return mouse_face_overwritten_p;
23757 /* EXPORT:
23758 Redisplay an exposed area of frame F. X and Y are the upper-left
23759 corner of the exposed rectangle. W and H are width and height of
23760 the exposed area. All are pixel values. W or H zero means redraw
23761 the entire frame. */
23763 void
23764 expose_frame (f, x, y, w, h)
23765 struct frame *f;
23766 int x, y, w, h;
23768 XRectangle r;
23769 int mouse_face_overwritten_p = 0;
23771 TRACE ((stderr, "expose_frame "));
23773 /* No need to redraw if frame will be redrawn soon. */
23774 if (FRAME_GARBAGED_P (f))
23776 TRACE ((stderr, " garbaged\n"));
23777 return;
23780 /* If basic faces haven't been realized yet, there is no point in
23781 trying to redraw anything. This can happen when we get an expose
23782 event while Emacs is starting, e.g. by moving another window. */
23783 if (FRAME_FACE_CACHE (f) == NULL
23784 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23786 TRACE ((stderr, " no faces\n"));
23787 return;
23790 if (w == 0 || h == 0)
23792 r.x = r.y = 0;
23793 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23794 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23796 else
23798 r.x = x;
23799 r.y = y;
23800 r.width = w;
23801 r.height = h;
23804 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23805 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23807 if (WINDOWP (f->tool_bar_window))
23808 mouse_face_overwritten_p
23809 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23811 #ifdef HAVE_X_WINDOWS
23812 #ifndef MSDOS
23813 #ifndef USE_X_TOOLKIT
23814 if (WINDOWP (f->menu_bar_window))
23815 mouse_face_overwritten_p
23816 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23817 #endif /* not USE_X_TOOLKIT */
23818 #endif
23819 #endif
23821 /* Some window managers support a focus-follows-mouse style with
23822 delayed raising of frames. Imagine a partially obscured frame,
23823 and moving the mouse into partially obscured mouse-face on that
23824 frame. The visible part of the mouse-face will be highlighted,
23825 then the WM raises the obscured frame. With at least one WM, KDE
23826 2.1, Emacs is not getting any event for the raising of the frame
23827 (even tried with SubstructureRedirectMask), only Expose events.
23828 These expose events will draw text normally, i.e. not
23829 highlighted. Which means we must redo the highlight here.
23830 Subsume it under ``we love X''. --gerd 2001-08-15 */
23831 /* Included in Windows version because Windows most likely does not
23832 do the right thing if any third party tool offers
23833 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23834 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23836 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23837 if (f == dpyinfo->mouse_face_mouse_frame)
23839 int x = dpyinfo->mouse_face_mouse_x;
23840 int y = dpyinfo->mouse_face_mouse_y;
23841 clear_mouse_face (dpyinfo);
23842 note_mouse_highlight (f, x, y);
23848 /* EXPORT:
23849 Determine the intersection of two rectangles R1 and R2. Return
23850 the intersection in *RESULT. Value is non-zero if RESULT is not
23851 empty. */
23854 x_intersect_rectangles (r1, r2, result)
23855 XRectangle *r1, *r2, *result;
23857 XRectangle *left, *right;
23858 XRectangle *upper, *lower;
23859 int intersection_p = 0;
23861 /* Rearrange so that R1 is the left-most rectangle. */
23862 if (r1->x < r2->x)
23863 left = r1, right = r2;
23864 else
23865 left = r2, right = r1;
23867 /* X0 of the intersection is right.x0, if this is inside R1,
23868 otherwise there is no intersection. */
23869 if (right->x <= left->x + left->width)
23871 result->x = right->x;
23873 /* The right end of the intersection is the minimum of the
23874 the right ends of left and right. */
23875 result->width = (min (left->x + left->width, right->x + right->width)
23876 - result->x);
23878 /* Same game for Y. */
23879 if (r1->y < r2->y)
23880 upper = r1, lower = r2;
23881 else
23882 upper = r2, lower = r1;
23884 /* The upper end of the intersection is lower.y0, if this is inside
23885 of upper. Otherwise, there is no intersection. */
23886 if (lower->y <= upper->y + upper->height)
23888 result->y = lower->y;
23890 /* The lower end of the intersection is the minimum of the lower
23891 ends of upper and lower. */
23892 result->height = (min (lower->y + lower->height,
23893 upper->y + upper->height)
23894 - result->y);
23895 intersection_p = 1;
23899 return intersection_p;
23902 #endif /* HAVE_WINDOW_SYSTEM */
23905 /***********************************************************************
23906 Initialization
23907 ***********************************************************************/
23909 void
23910 syms_of_xdisp ()
23912 Vwith_echo_area_save_vector = Qnil;
23913 staticpro (&Vwith_echo_area_save_vector);
23915 Vmessage_stack = Qnil;
23916 staticpro (&Vmessage_stack);
23918 Qinhibit_redisplay = intern ("inhibit-redisplay");
23919 staticpro (&Qinhibit_redisplay);
23921 message_dolog_marker1 = Fmake_marker ();
23922 staticpro (&message_dolog_marker1);
23923 message_dolog_marker2 = Fmake_marker ();
23924 staticpro (&message_dolog_marker2);
23925 message_dolog_marker3 = Fmake_marker ();
23926 staticpro (&message_dolog_marker3);
23928 #if GLYPH_DEBUG
23929 defsubr (&Sdump_frame_glyph_matrix);
23930 defsubr (&Sdump_glyph_matrix);
23931 defsubr (&Sdump_glyph_row);
23932 defsubr (&Sdump_tool_bar_row);
23933 defsubr (&Strace_redisplay);
23934 defsubr (&Strace_to_stderr);
23935 #endif
23936 #ifdef HAVE_WINDOW_SYSTEM
23937 defsubr (&Stool_bar_lines_needed);
23938 defsubr (&Slookup_image_map);
23939 #endif
23940 defsubr (&Sformat_mode_line);
23941 defsubr (&Sinvisible_p);
23943 staticpro (&Qmenu_bar_update_hook);
23944 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23946 staticpro (&Qoverriding_terminal_local_map);
23947 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23949 staticpro (&Qoverriding_local_map);
23950 Qoverriding_local_map = intern ("overriding-local-map");
23952 staticpro (&Qwindow_scroll_functions);
23953 Qwindow_scroll_functions = intern ("window-scroll-functions");
23955 staticpro (&Qredisplay_end_trigger_functions);
23956 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23958 staticpro (&Qinhibit_point_motion_hooks);
23959 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23961 QCdata = intern (":data");
23962 staticpro (&QCdata);
23963 Qdisplay = intern ("display");
23964 staticpro (&Qdisplay);
23965 Qspace_width = intern ("space-width");
23966 staticpro (&Qspace_width);
23967 Qraise = intern ("raise");
23968 staticpro (&Qraise);
23969 Qslice = intern ("slice");
23970 staticpro (&Qslice);
23971 Qspace = intern ("space");
23972 staticpro (&Qspace);
23973 Qmargin = intern ("margin");
23974 staticpro (&Qmargin);
23975 Qpointer = intern ("pointer");
23976 staticpro (&Qpointer);
23977 Qleft_margin = intern ("left-margin");
23978 staticpro (&Qleft_margin);
23979 Qright_margin = intern ("right-margin");
23980 staticpro (&Qright_margin);
23981 Qcenter = intern ("center");
23982 staticpro (&Qcenter);
23983 Qline_height = intern ("line-height");
23984 staticpro (&Qline_height);
23985 QCalign_to = intern (":align-to");
23986 staticpro (&QCalign_to);
23987 QCrelative_width = intern (":relative-width");
23988 staticpro (&QCrelative_width);
23989 QCrelative_height = intern (":relative-height");
23990 staticpro (&QCrelative_height);
23991 QCeval = intern (":eval");
23992 staticpro (&QCeval);
23993 QCpropertize = intern (":propertize");
23994 staticpro (&QCpropertize);
23995 QCfile = intern (":file");
23996 staticpro (&QCfile);
23997 Qfontified = intern ("fontified");
23998 staticpro (&Qfontified);
23999 Qfontification_functions = intern ("fontification-functions");
24000 staticpro (&Qfontification_functions);
24001 Qtrailing_whitespace = intern ("trailing-whitespace");
24002 staticpro (&Qtrailing_whitespace);
24003 Qescape_glyph = intern ("escape-glyph");
24004 staticpro (&Qescape_glyph);
24005 Qnobreak_space = intern ("nobreak-space");
24006 staticpro (&Qnobreak_space);
24007 Qimage = intern ("image");
24008 staticpro (&Qimage);
24009 QCmap = intern (":map");
24010 staticpro (&QCmap);
24011 QCpointer = intern (":pointer");
24012 staticpro (&QCpointer);
24013 Qrect = intern ("rect");
24014 staticpro (&Qrect);
24015 Qcircle = intern ("circle");
24016 staticpro (&Qcircle);
24017 Qpoly = intern ("poly");
24018 staticpro (&Qpoly);
24019 Qmessage_truncate_lines = intern ("message-truncate-lines");
24020 staticpro (&Qmessage_truncate_lines);
24021 Qgrow_only = intern ("grow-only");
24022 staticpro (&Qgrow_only);
24023 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24024 staticpro (&Qinhibit_menubar_update);
24025 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24026 staticpro (&Qinhibit_eval_during_redisplay);
24027 Qposition = intern ("position");
24028 staticpro (&Qposition);
24029 Qbuffer_position = intern ("buffer-position");
24030 staticpro (&Qbuffer_position);
24031 Qobject = intern ("object");
24032 staticpro (&Qobject);
24033 Qbar = intern ("bar");
24034 staticpro (&Qbar);
24035 Qhbar = intern ("hbar");
24036 staticpro (&Qhbar);
24037 Qbox = intern ("box");
24038 staticpro (&Qbox);
24039 Qhollow = intern ("hollow");
24040 staticpro (&Qhollow);
24041 Qhand = intern ("hand");
24042 staticpro (&Qhand);
24043 Qarrow = intern ("arrow");
24044 staticpro (&Qarrow);
24045 Qtext = intern ("text");
24046 staticpro (&Qtext);
24047 Qrisky_local_variable = intern ("risky-local-variable");
24048 staticpro (&Qrisky_local_variable);
24049 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24050 staticpro (&Qinhibit_free_realized_faces);
24052 list_of_error = Fcons (Fcons (intern ("error"),
24053 Fcons (intern ("void-variable"), Qnil)),
24054 Qnil);
24055 staticpro (&list_of_error);
24057 Qlast_arrow_position = intern ("last-arrow-position");
24058 staticpro (&Qlast_arrow_position);
24059 Qlast_arrow_string = intern ("last-arrow-string");
24060 staticpro (&Qlast_arrow_string);
24062 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24063 staticpro (&Qoverlay_arrow_string);
24064 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24065 staticpro (&Qoverlay_arrow_bitmap);
24067 echo_buffer[0] = echo_buffer[1] = Qnil;
24068 staticpro (&echo_buffer[0]);
24069 staticpro (&echo_buffer[1]);
24071 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24072 staticpro (&echo_area_buffer[0]);
24073 staticpro (&echo_area_buffer[1]);
24075 Vmessages_buffer_name = build_string ("*Messages*");
24076 staticpro (&Vmessages_buffer_name);
24078 mode_line_proptrans_alist = Qnil;
24079 staticpro (&mode_line_proptrans_alist);
24080 mode_line_string_list = Qnil;
24081 staticpro (&mode_line_string_list);
24082 mode_line_string_face = Qnil;
24083 staticpro (&mode_line_string_face);
24084 mode_line_string_face_prop = Qnil;
24085 staticpro (&mode_line_string_face_prop);
24086 Vmode_line_unwind_vector = Qnil;
24087 staticpro (&Vmode_line_unwind_vector);
24089 help_echo_string = Qnil;
24090 staticpro (&help_echo_string);
24091 help_echo_object = Qnil;
24092 staticpro (&help_echo_object);
24093 help_echo_window = Qnil;
24094 staticpro (&help_echo_window);
24095 previous_help_echo_string = Qnil;
24096 staticpro (&previous_help_echo_string);
24097 help_echo_pos = -1;
24099 #ifdef HAVE_WINDOW_SYSTEM
24100 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24101 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24102 For example, if a block cursor is over a tab, it will be drawn as
24103 wide as that tab on the display. */);
24104 x_stretch_cursor_p = 0;
24105 #endif
24107 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24108 doc: /* *Non-nil means highlight trailing whitespace.
24109 The face used for trailing whitespace is `trailing-whitespace'. */);
24110 Vshow_trailing_whitespace = Qnil;
24112 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24113 doc: /* *Control highlighting of nobreak space and soft hyphen.
24114 A value of t means highlight the character itself (for nobreak space,
24115 use face `nobreak-space').
24116 A value of nil means no highlighting.
24117 Other values mean display the escape glyph followed by an ordinary
24118 space or ordinary hyphen. */);
24119 Vnobreak_char_display = Qt;
24121 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24122 doc: /* *The pointer shape to show in void text areas.
24123 A value of nil means to show the text pointer. Other options are `arrow',
24124 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24125 Vvoid_text_area_pointer = Qarrow;
24127 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24128 doc: /* Non-nil means don't actually do any redisplay.
24129 This is used for internal purposes. */);
24130 Vinhibit_redisplay = Qnil;
24132 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24133 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24134 Vglobal_mode_string = Qnil;
24136 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24137 doc: /* Marker for where to display an arrow on top of the buffer text.
24138 This must be the beginning of a line in order to work.
24139 See also `overlay-arrow-string'. */);
24140 Voverlay_arrow_position = Qnil;
24142 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24143 doc: /* String to display as an arrow in non-window frames.
24144 See also `overlay-arrow-position'. */);
24145 Voverlay_arrow_string = build_string ("=>");
24147 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24148 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24149 The symbols on this list are examined during redisplay to determine
24150 where to display overlay arrows. */);
24151 Voverlay_arrow_variable_list
24152 = Fcons (intern ("overlay-arrow-position"), Qnil);
24154 DEFVAR_INT ("scroll-step", &scroll_step,
24155 doc: /* *The number of lines to try scrolling a window by when point moves out.
24156 If that fails to bring point back on frame, point is centered instead.
24157 If this is zero, point is always centered after it moves off frame.
24158 If you want scrolling to always be a line at a time, you should set
24159 `scroll-conservatively' to a large value rather than set this to 1. */);
24161 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24162 doc: /* *Scroll up to this many lines, to bring point back on screen.
24163 A value of zero means to scroll the text to center point vertically
24164 in the window. */);
24165 scroll_conservatively = 0;
24167 DEFVAR_INT ("scroll-margin", &scroll_margin,
24168 doc: /* *Number of lines of margin at the top and bottom of a window.
24169 Recenter the window whenever point gets within this many lines
24170 of the top or bottom of the window. */);
24171 scroll_margin = 0;
24173 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24174 doc: /* Pixels per inch value for non-window system displays.
24175 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24176 Vdisplay_pixels_per_inch = make_float (72.0);
24178 #if GLYPH_DEBUG
24179 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24180 #endif
24182 DEFVAR_BOOL ("truncate-partial-width-windows",
24183 &truncate_partial_width_windows,
24184 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24185 truncate_partial_width_windows = 1;
24187 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24188 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24189 Any other value means to use the appropriate face, `mode-line',
24190 `header-line', or `menu' respectively. */);
24191 mode_line_inverse_video = 1;
24193 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24194 doc: /* *Maximum buffer size for which line number should be displayed.
24195 If the buffer is bigger than this, the line number does not appear
24196 in the mode line. A value of nil means no limit. */);
24197 Vline_number_display_limit = Qnil;
24199 DEFVAR_INT ("line-number-display-limit-width",
24200 &line_number_display_limit_width,
24201 doc: /* *Maximum line width (in characters) for line number display.
24202 If the average length of the lines near point is bigger than this, then the
24203 line number may be omitted from the mode line. */);
24204 line_number_display_limit_width = 200;
24206 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24207 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24208 highlight_nonselected_windows = 0;
24210 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24211 doc: /* Non-nil if more than one frame is visible on this display.
24212 Minibuffer-only frames don't count, but iconified frames do.
24213 This variable is not guaranteed to be accurate except while processing
24214 `frame-title-format' and `icon-title-format'. */);
24216 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24217 doc: /* Template for displaying the title bar of visible frames.
24218 \(Assuming the window manager supports this feature.)
24220 This variable has the same structure as `mode-line-format', except that
24221 the %c and %l constructs are ignored. It is used only on frames for
24222 which no explicit name has been set \(see `modify-frame-parameters'). */);
24224 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24225 doc: /* Template for displaying the title bar of an iconified frame.
24226 \(Assuming the window manager supports this feature.)
24227 This variable has the same structure as `mode-line-format' (which see),
24228 and is used only on frames for which no explicit name has been set
24229 \(see `modify-frame-parameters'). */);
24230 Vicon_title_format
24231 = Vframe_title_format
24232 = Fcons (intern ("multiple-frames"),
24233 Fcons (build_string ("%b"),
24234 Fcons (Fcons (empty_unibyte_string,
24235 Fcons (intern ("invocation-name"),
24236 Fcons (build_string ("@"),
24237 Fcons (intern ("system-name"),
24238 Qnil)))),
24239 Qnil)));
24241 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24242 doc: /* Maximum number of lines to keep in the message log buffer.
24243 If nil, disable message logging. If t, log messages but don't truncate
24244 the buffer when it becomes large. */);
24245 Vmessage_log_max = make_number (100);
24247 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24248 doc: /* Functions called before redisplay, if window sizes have changed.
24249 The value should be a list of functions that take one argument.
24250 Just before redisplay, for each frame, if any of its windows have changed
24251 size since the last redisplay, or have been split or deleted,
24252 all the functions in the list are called, with the frame as argument. */);
24253 Vwindow_size_change_functions = Qnil;
24255 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24256 doc: /* List of functions to call before redisplaying a window with scrolling.
24257 Each function is called with two arguments, the window
24258 and its new display-start position. Note that the value of `window-end'
24259 is not valid when these functions are called. */);
24260 Vwindow_scroll_functions = Qnil;
24262 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24263 doc: /* Functions called when redisplay of a window reaches the end trigger.
24264 Each function is called with two arguments, the window and the end trigger value.
24265 See `set-window-redisplay-end-trigger'. */);
24266 Vredisplay_end_trigger_functions = Qnil;
24268 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24269 doc: /* *Non-nil means autoselect window with mouse pointer.
24270 If nil, do not autoselect windows.
24271 A positive number means delay autoselection by that many seconds: a
24272 window is autoselected only after the mouse has remained in that
24273 window for the duration of the delay.
24274 A negative number has a similar effect, but causes windows to be
24275 autoselected only after the mouse has stopped moving. \(Because of
24276 the way Emacs compares mouse events, you will occasionally wait twice
24277 that time before the window gets selected.\)
24278 Any other value means to autoselect window instantaneously when the
24279 mouse pointer enters it.
24281 Autoselection selects the minibuffer only if it is active, and never
24282 unselects the minibuffer if it is active.
24284 When customizing this variable make sure that the actual value of
24285 `focus-follows-mouse' matches the behavior of your window manager. */);
24286 Vmouse_autoselect_window = Qnil;
24288 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24289 doc: /* *Non-nil means automatically resize tool-bars.
24290 This dynamically changes the tool-bar's height to the minimum height
24291 that is needed to make all tool-bar items visible.
24292 If value is `grow-only', the tool-bar's height is only increased
24293 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24294 Vauto_resize_tool_bars = Qt;
24296 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24297 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24298 auto_raise_tool_bar_buttons_p = 1;
24300 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24301 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24302 make_cursor_line_fully_visible_p = 1;
24304 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24305 doc: /* *Border below tool-bar in pixels.
24306 If an integer, use it as the height of the border.
24307 If it is one of `internal-border-width' or `border-width', use the
24308 value of the corresponding frame parameter.
24309 Otherwise, no border is added below the tool-bar. */);
24310 Vtool_bar_border = Qinternal_border_width;
24312 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24313 doc: /* *Margin around tool-bar buttons in pixels.
24314 If an integer, use that for both horizontal and vertical margins.
24315 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24316 HORZ specifying the horizontal margin, and VERT specifying the
24317 vertical margin. */);
24318 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24320 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24321 doc: /* *Relief thickness of tool-bar buttons. */);
24322 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24324 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24325 doc: /* List of functions to call to fontify regions of text.
24326 Each function is called with one argument POS. Functions must
24327 fontify a region starting at POS in the current buffer, and give
24328 fontified regions the property `fontified'. */);
24329 Vfontification_functions = Qnil;
24330 Fmake_variable_buffer_local (Qfontification_functions);
24332 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24333 &unibyte_display_via_language_environment,
24334 doc: /* *Non-nil means display unibyte text according to language environment.
24335 Specifically this means that unibyte non-ASCII characters
24336 are displayed by converting them to the equivalent multibyte characters
24337 according to the current language environment. As a result, they are
24338 displayed according to the current fontset. */);
24339 unibyte_display_via_language_environment = 0;
24341 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24342 doc: /* *Maximum height for resizing mini-windows.
24343 If a float, it specifies a fraction of the mini-window frame's height.
24344 If an integer, it specifies a number of lines. */);
24345 Vmax_mini_window_height = make_float (0.25);
24347 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24348 doc: /* *How to resize mini-windows.
24349 A value of nil means don't automatically resize mini-windows.
24350 A value of t means resize them to fit the text displayed in them.
24351 A value of `grow-only', the default, means let mini-windows grow
24352 only, until their display becomes empty, at which point the windows
24353 go back to their normal size. */);
24354 Vresize_mini_windows = Qgrow_only;
24356 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24357 doc: /* Alist specifying how to blink the cursor off.
24358 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24359 `cursor-type' frame-parameter or variable equals ON-STATE,
24360 comparing using `equal', Emacs uses OFF-STATE to specify
24361 how to blink it off. ON-STATE and OFF-STATE are values for
24362 the `cursor-type' frame parameter.
24364 If a frame's ON-STATE has no entry in this list,
24365 the frame's other specifications determine how to blink the cursor off. */);
24366 Vblink_cursor_alist = Qnil;
24368 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24369 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24370 automatic_hscrolling_p = 1;
24372 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24373 doc: /* *How many columns away from the window edge point is allowed to get
24374 before automatic hscrolling will horizontally scroll the window. */);
24375 hscroll_margin = 5;
24377 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24378 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24379 When point is less than `hscroll-margin' columns from the window
24380 edge, automatic hscrolling will scroll the window by the amount of columns
24381 determined by this variable. If its value is a positive integer, scroll that
24382 many columns. If it's a positive floating-point number, it specifies the
24383 fraction of the window's width to scroll. If it's nil or zero, point will be
24384 centered horizontally after the scroll. Any other value, including negative
24385 numbers, are treated as if the value were zero.
24387 Automatic hscrolling always moves point outside the scroll margin, so if
24388 point was more than scroll step columns inside the margin, the window will
24389 scroll more than the value given by the scroll step.
24391 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24392 and `scroll-right' overrides this variable's effect. */);
24393 Vhscroll_step = make_number (0);
24395 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24396 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24397 Bind this around calls to `message' to let it take effect. */);
24398 message_truncate_lines = 0;
24400 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24401 doc: /* Normal hook run to update the menu bar definitions.
24402 Redisplay runs this hook before it redisplays the menu bar.
24403 This is used to update submenus such as Buffers,
24404 whose contents depend on various data. */);
24405 Vmenu_bar_update_hook = Qnil;
24407 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24408 doc: /* Frame for which we are updating a menu.
24409 The enable predicate for a menu binding should check this variable. */);
24410 Vmenu_updating_frame = Qnil;
24412 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24413 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24414 inhibit_menubar_update = 0;
24416 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24417 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24418 inhibit_eval_during_redisplay = 0;
24420 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24421 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24422 inhibit_free_realized_faces = 0;
24424 #if GLYPH_DEBUG
24425 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24426 doc: /* Inhibit try_window_id display optimization. */);
24427 inhibit_try_window_id = 0;
24429 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24430 doc: /* Inhibit try_window_reusing display optimization. */);
24431 inhibit_try_window_reusing = 0;
24433 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24434 doc: /* Inhibit try_cursor_movement display optimization. */);
24435 inhibit_try_cursor_movement = 0;
24436 #endif /* GLYPH_DEBUG */
24438 DEFVAR_INT ("overline-margin", &overline_margin,
24439 doc: /* *Space between overline and text, in pixels.
24440 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24441 margin to the caracter height. */);
24442 overline_margin = 2;
24446 /* Initialize this module when Emacs starts. */
24448 void
24449 init_xdisp ()
24451 Lisp_Object root_window;
24452 struct window *mini_w;
24454 current_header_line_height = current_mode_line_height = -1;
24456 CHARPOS (this_line_start_pos) = 0;
24458 mini_w = XWINDOW (minibuf_window);
24459 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24461 if (!noninteractive)
24463 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24464 int i;
24466 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24467 set_window_height (root_window,
24468 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24470 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24471 set_window_height (minibuf_window, 1, 0);
24473 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24474 mini_w->total_cols = make_number (FRAME_COLS (f));
24476 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24477 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24478 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24480 /* The default ellipsis glyphs `...'. */
24481 for (i = 0; i < 3; ++i)
24482 default_invis_vector[i] = make_number ('.');
24486 /* Allocate the buffer for frame titles.
24487 Also used for `format-mode-line'. */
24488 int size = 100;
24489 mode_line_noprop_buf = (char *) xmalloc (size);
24490 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24491 mode_line_noprop_ptr = mode_line_noprop_buf;
24492 mode_line_target = MODE_LINE_DISPLAY;
24495 help_echo_showing_p = 0;
24499 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24500 (do not change this comment) */