(syms_of_buffer): Fix a few typos.
[emacs.git] / src / xdisp.c
blob306e26702f5c0621bfb721d4ecb25f2902e26184
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
207 #define INFINITY 10000000
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
215 extern int interrupt_input;
216 extern int command_loop_level;
218 extern Lisp_Object do_mouse_tracking;
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
248 Lisp_Object Qrisky_local_variable;
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
253 /* Functions called to fontify regions of text. */
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
265 int auto_raise_tool_bar_buttons_p;
267 /* Margin around tool bar buttons in pixels. */
269 Lisp_Object Vtool_bar_button_margin;
271 /* Thickness of shadow to draw around tool bar buttons. */
273 EMACS_INT tool_bar_button_relief;
275 /* Non-zero means automatically resize tool-bars so that all tool-bar
276 items are visible, and no blank lines remain. */
278 int auto_resize_tool_bars_p;
280 /* Non-zero means draw block and hollow cursor as wide as the glyph
281 under it. For example, if a block cursor is over a tab, it will be
282 drawn as wide as that tab on the display. */
284 int x_stretch_cursor_p;
286 /* Non-nil means don't actually do any redisplay. */
288 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
290 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
292 int inhibit_eval_during_redisplay;
294 /* Names of text properties relevant for redisplay. */
296 Lisp_Object Qdisplay;
297 extern Lisp_Object Qface, Qinvisible, Qwidth;
299 /* Symbols used in text property values. */
301 Lisp_Object Vdisplay_pixels_per_inch;
302 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
303 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
304 Lisp_Object Qslice;
305 Lisp_Object Qcenter;
306 Lisp_Object Qmargin, Qpointer;
307 Lisp_Object Qline_height, Qtotal;
308 extern Lisp_Object Qheight;
309 extern Lisp_Object QCwidth, QCheight, QCascent;
310 extern Lisp_Object Qscroll_bar;
311 extern Lisp_Object Qcursor;
313 /* Non-nil means highlight trailing whitespace. */
315 Lisp_Object Vshow_trailing_whitespace;
317 #ifdef HAVE_WINDOW_SYSTEM
318 extern Lisp_Object Voverflow_newline_into_fringe;
320 /* Test if overflow newline into fringe. Called with iterator IT
321 at or past right window margin, and with IT->current_x set. */
323 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
324 (!NILP (Voverflow_newline_into_fringe) \
325 && FRAME_WINDOW_P (it->f) \
326 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
327 && it->current_x == it->last_visible_x)
329 #endif /* HAVE_WINDOW_SYSTEM */
331 /* Non-nil means show the text cursor in void text areas
332 i.e. in blank areas after eol and eob. This used to be
333 the default in 21.3. */
335 Lisp_Object Vvoid_text_area_pointer;
337 /* Name of the face used to highlight trailing whitespace. */
339 Lisp_Object Qtrailing_whitespace;
341 /* The symbol `image' which is the car of the lists used to represent
342 images in Lisp. */
344 Lisp_Object Qimage;
346 /* The image map types. */
347 Lisp_Object QCmap, QCpointer;
348 Lisp_Object Qrect, Qcircle, Qpoly;
350 /* Non-zero means print newline to stdout before next mini-buffer
351 message. */
353 int noninteractive_need_newline;
355 /* Non-zero means print newline to message log before next message. */
357 static int message_log_need_newline;
359 /* Three markers that message_dolog uses.
360 It could allocate them itself, but that causes trouble
361 in handling memory-full errors. */
362 static Lisp_Object message_dolog_marker1;
363 static Lisp_Object message_dolog_marker2;
364 static Lisp_Object message_dolog_marker3;
366 /* The buffer position of the first character appearing entirely or
367 partially on the line of the selected window which contains the
368 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
369 redisplay optimization in redisplay_internal. */
371 static struct text_pos this_line_start_pos;
373 /* Number of characters past the end of the line above, including the
374 terminating newline. */
376 static struct text_pos this_line_end_pos;
378 /* The vertical positions and the height of this line. */
380 static int this_line_vpos;
381 static int this_line_y;
382 static int this_line_pixel_height;
384 /* X position at which this display line starts. Usually zero;
385 negative if first character is partially visible. */
387 static int this_line_start_x;
389 /* Buffer that this_line_.* variables are referring to. */
391 static struct buffer *this_line_buffer;
393 /* Nonzero means truncate lines in all windows less wide than the
394 frame. */
396 int truncate_partial_width_windows;
398 /* A flag to control how to display unibyte 8-bit character. */
400 int unibyte_display_via_language_environment;
402 /* Nonzero means we have more than one non-mini-buffer-only frame.
403 Not guaranteed to be accurate except while parsing
404 frame-title-format. */
406 int multiple_frames;
408 Lisp_Object Vglobal_mode_string;
411 /* List of variables (symbols) which hold markers for overlay arrows.
412 The symbols on this list are examined during redisplay to determine
413 where to display overlay arrows. */
415 Lisp_Object Voverlay_arrow_variable_list;
417 /* Marker for where to display an arrow on top of the buffer text. */
419 Lisp_Object Voverlay_arrow_position;
421 /* String to display for the arrow. Only used on terminal frames. */
423 Lisp_Object Voverlay_arrow_string;
425 /* Values of those variables at last redisplay are stored as
426 properties on `overlay-arrow-position' symbol. However, if
427 Voverlay_arrow_position is a marker, last-arrow-position is its
428 numerical position. */
430 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
432 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
433 properties on a symbol in overlay-arrow-variable-list. */
435 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
437 /* Like mode-line-format, but for the title bar on a visible frame. */
439 Lisp_Object Vframe_title_format;
441 /* Like mode-line-format, but for the title bar on an iconified frame. */
443 Lisp_Object Vicon_title_format;
445 /* List of functions to call when a window's size changes. These
446 functions get one arg, a frame on which one or more windows' sizes
447 have changed. */
449 static Lisp_Object Vwindow_size_change_functions;
451 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
453 /* Nonzero if overlay arrow has been displayed once in this window. */
455 static int overlay_arrow_seen;
457 /* Nonzero means highlight the region even in nonselected windows. */
459 int highlight_nonselected_windows;
461 /* If cursor motion alone moves point off frame, try scrolling this
462 many lines up or down if that will bring it back. */
464 static EMACS_INT scroll_step;
466 /* Nonzero means scroll just far enough to bring point back on the
467 screen, when appropriate. */
469 static EMACS_INT scroll_conservatively;
471 /* Recenter the window whenever point gets within this many lines of
472 the top or bottom of the window. This value is translated into a
473 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
474 that there is really a fixed pixel height scroll margin. */
476 EMACS_INT scroll_margin;
478 /* Number of windows showing the buffer of the selected window (or
479 another buffer with the same base buffer). keyboard.c refers to
480 this. */
482 int buffer_shared;
484 /* Vector containing glyphs for an ellipsis `...'. */
486 static Lisp_Object default_invis_vector[3];
488 /* Zero means display the mode-line/header-line/menu-bar in the default face
489 (this slightly odd definition is for compatibility with previous versions
490 of emacs), non-zero means display them using their respective faces.
492 This variable is deprecated. */
494 int mode_line_inverse_video;
496 /* Prompt to display in front of the mini-buffer contents. */
498 Lisp_Object minibuf_prompt;
500 /* Width of current mini-buffer prompt. Only set after display_line
501 of the line that contains the prompt. */
503 int minibuf_prompt_width;
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
509 Lisp_Object echo_area_window;
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
516 Lisp_Object Vmessage_stack;
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
521 int message_enable_multibyte;
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
525 int update_mode_lines;
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
530 int windows_or_buffers_changed;
532 /* Nonzero means a frame's cursor type has been changed. */
534 int cursor_type_changed;
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
539 int line_number_displayed;
541 /* Maximum buffer size for which to display line numbers. */
543 Lisp_Object Vline_number_display_limit;
545 /* Line width to consider when repositioning for line number display. */
547 static EMACS_INT line_number_display_limit_width;
549 /* Number of lines to keep in the message log buffer. t means
550 infinite. nil means don't log at all. */
552 Lisp_Object Vmessage_log_max;
554 /* The name of the *Messages* buffer, a string. */
556 static Lisp_Object Vmessages_buffer_name;
558 /* Current, index 0, and last displayed echo area message. Either
559 buffers from echo_buffers, or nil to indicate no message. */
561 Lisp_Object echo_area_buffer[2];
563 /* The buffers referenced from echo_area_buffer. */
565 static Lisp_Object echo_buffer[2];
567 /* A vector saved used in with_area_buffer to reduce consing. */
569 static Lisp_Object Vwith_echo_area_save_vector;
571 /* Non-zero means display_echo_area should display the last echo area
572 message again. Set by redisplay_preserve_echo_area. */
574 static int display_last_displayed_message_p;
576 /* Nonzero if echo area is being used by print; zero if being used by
577 message. */
579 int message_buf_print;
581 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
583 Lisp_Object Qinhibit_menubar_update;
584 int inhibit_menubar_update;
586 /* Maximum height for resizing mini-windows. Either a float
587 specifying a fraction of the available height, or an integer
588 specifying a number of lines. */
590 Lisp_Object Vmax_mini_window_height;
592 /* Non-zero means messages should be displayed with truncated
593 lines instead of being continued. */
595 int message_truncate_lines;
596 Lisp_Object Qmessage_truncate_lines;
598 /* Set to 1 in clear_message to make redisplay_internal aware
599 of an emptied echo area. */
601 static int message_cleared_p;
603 /* Non-zero means we want a hollow cursor in windows that are not
604 selected. Zero means there's no cursor in such windows. */
606 Lisp_Object Vcursor_in_non_selected_windows;
607 Lisp_Object Qcursor_in_non_selected_windows;
609 /* How to blink the default frame cursor off. */
610 Lisp_Object Vblink_cursor_alist;
612 /* A scratch glyph row with contents used for generating truncation
613 glyphs. Also used in direct_output_for_insert. */
615 #define MAX_SCRATCH_GLYPHS 100
616 struct glyph_row scratch_glyph_row;
617 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
619 /* Ascent and height of the last line processed by move_it_to. */
621 static int last_max_ascent, last_height;
623 /* Non-zero if there's a help-echo in the echo area. */
625 int help_echo_showing_p;
627 /* If >= 0, computed, exact values of mode-line and header-line height
628 to use in the macros CURRENT_MODE_LINE_HEIGHT and
629 CURRENT_HEADER_LINE_HEIGHT. */
631 int current_mode_line_height, current_header_line_height;
633 /* The maximum distance to look ahead for text properties. Values
634 that are too small let us call compute_char_face and similar
635 functions too often which is expensive. Values that are too large
636 let us call compute_char_face and alike too often because we
637 might not be interested in text properties that far away. */
639 #define TEXT_PROP_DISTANCE_LIMIT 100
641 #if GLYPH_DEBUG
643 /* Variables to turn off display optimizations from Lisp. */
645 int inhibit_try_window_id, inhibit_try_window_reusing;
646 int inhibit_try_cursor_movement;
648 /* Non-zero means print traces of redisplay if compiled with
649 GLYPH_DEBUG != 0. */
651 int trace_redisplay_p;
653 #endif /* GLYPH_DEBUG */
655 #ifdef DEBUG_TRACE_MOVE
656 /* Non-zero means trace with TRACE_MOVE to stderr. */
657 int trace_move;
659 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
660 #else
661 #define TRACE_MOVE(x) (void) 0
662 #endif
664 /* Non-zero means automatically scroll windows horizontally to make
665 point visible. */
667 int automatic_hscrolling_p;
669 /* How close to the margin can point get before the window is scrolled
670 horizontally. */
671 EMACS_INT hscroll_margin;
673 /* How much to scroll horizontally when point is inside the above margin. */
674 Lisp_Object Vhscroll_step;
676 /* The variable `resize-mini-windows'. If nil, don't resize
677 mini-windows. If t, always resize them to fit the text they
678 display. If `grow-only', let mini-windows grow only until they
679 become empty. */
681 Lisp_Object Vresize_mini_windows;
683 /* Buffer being redisplayed -- for redisplay_window_error. */
685 struct buffer *displayed_buffer;
687 /* Value returned from text property handlers (see below). */
689 enum prop_handled
691 HANDLED_NORMALLY,
692 HANDLED_RECOMPUTE_PROPS,
693 HANDLED_OVERLAY_STRING_CONSUMED,
694 HANDLED_RETURN
697 /* A description of text properties that redisplay is interested
698 in. */
700 struct props
702 /* The name of the property. */
703 Lisp_Object *name;
705 /* A unique index for the property. */
706 enum prop_idx idx;
708 /* A handler function called to set up iterator IT from the property
709 at IT's current position. Value is used to steer handle_stop. */
710 enum prop_handled (*handler) P_ ((struct it *it));
713 static enum prop_handled handle_face_prop P_ ((struct it *));
714 static enum prop_handled handle_invisible_prop P_ ((struct it *));
715 static enum prop_handled handle_display_prop P_ ((struct it *));
716 static enum prop_handled handle_composition_prop P_ ((struct it *));
717 static enum prop_handled handle_overlay_change P_ ((struct it *));
718 static enum prop_handled handle_fontified_prop P_ ((struct it *));
720 /* Properties handled by iterators. */
722 static struct props it_props[] =
724 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
725 /* Handle `face' before `display' because some sub-properties of
726 `display' need to know the face. */
727 {&Qface, FACE_PROP_IDX, handle_face_prop},
728 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
729 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
730 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
731 {NULL, 0, NULL}
734 /* Value is the position described by X. If X is a marker, value is
735 the marker_position of X. Otherwise, value is X. */
737 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
739 /* Enumeration returned by some move_it_.* functions internally. */
741 enum move_it_result
743 /* Not used. Undefined value. */
744 MOVE_UNDEFINED,
746 /* Move ended at the requested buffer position or ZV. */
747 MOVE_POS_MATCH_OR_ZV,
749 /* Move ended at the requested X pixel position. */
750 MOVE_X_REACHED,
752 /* Move within a line ended at the end of a line that must be
753 continued. */
754 MOVE_LINE_CONTINUED,
756 /* Move within a line ended at the end of a line that would
757 be displayed truncated. */
758 MOVE_LINE_TRUNCATED,
760 /* Move within a line ended at a line end. */
761 MOVE_NEWLINE_OR_CR
764 /* This counter is used to clear the face cache every once in a while
765 in redisplay_internal. It is incremented for each redisplay.
766 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
767 cleared. */
769 #define CLEAR_FACE_CACHE_COUNT 500
770 static int clear_face_cache_count;
772 /* Record the previous terminal frame we displayed. */
774 static struct frame *previous_terminal_frame;
776 /* Non-zero while redisplay_internal is in progress. */
778 int redisplaying_p;
780 /* Non-zero means don't free realized faces. Bound while freeing
781 realized faces is dangerous because glyph matrices might still
782 reference them. */
784 int inhibit_free_realized_faces;
785 Lisp_Object Qinhibit_free_realized_faces;
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 int help_echo_pos;
795 /* Temporary variable for XTread_socket. */
797 Lisp_Object previous_help_echo_string;
799 /* Null glyph slice */
801 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
804 /* Function prototypes. */
806 static void setup_for_ellipsis P_ ((struct it *));
807 static void mark_window_display_accurate_1 P_ ((struct window *, int));
808 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
809 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
810 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
811 static int redisplay_mode_lines P_ ((Lisp_Object, int));
812 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
814 #if 0
815 static int invisible_text_between_p P_ ((struct it *, int, int));
816 #endif
818 static int next_element_from_ellipsis P_ ((struct it *));
819 static void pint2str P_ ((char *, int, int));
820 static void pint2hrstr P_ ((char *, int, int));
821 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
822 struct text_pos));
823 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
824 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
825 static void store_frame_title_char P_ ((char));
826 static int store_frame_title P_ ((const unsigned char *, int, int));
827 static void x_consider_frame_title P_ ((Lisp_Object));
828 static void handle_stop P_ ((struct it *));
829 static int tool_bar_lines_needed P_ ((struct frame *));
830 static int single_display_prop_intangible_p P_ ((Lisp_Object));
831 static void ensure_echo_area_buffers P_ ((void));
832 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
833 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
834 static int with_echo_area_buffer P_ ((struct window *, int,
835 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
836 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static void clear_garbaged_frames P_ ((void));
838 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
839 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static int display_echo_area P_ ((struct window *));
842 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
843 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
845 static int string_char_and_length P_ ((const unsigned char *, int, int *));
846 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
847 struct text_pos));
848 static int compute_window_start_on_continuation_line P_ ((struct window *));
849 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
850 static void insert_left_trunc_glyphs P_ ((struct it *));
851 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
852 Lisp_Object));
853 static void extend_face_to_end_of_line P_ ((struct it *));
854 static int append_space_for_newline P_ ((struct it *, int));
855 static int make_cursor_line_fully_visible P_ ((struct window *, int));
856 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
857 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
858 static int trailing_whitespace_p P_ ((int));
859 static int message_log_check_duplicate P_ ((int, int, int, int));
860 static void push_it P_ ((struct it *));
861 static void pop_it P_ ((struct it *));
862 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
863 static void select_frame_for_redisplay P_ ((Lisp_Object));
864 static void redisplay_internal P_ ((int));
865 static int echo_area_display P_ ((int));
866 static void redisplay_windows P_ ((Lisp_Object));
867 static void redisplay_window P_ ((Lisp_Object, int));
868 static Lisp_Object redisplay_window_error ();
869 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
870 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
871 static void update_menu_bar P_ ((struct frame *, int));
872 static int try_window_reusing_current_matrix P_ ((struct window *));
873 static int try_window_id P_ ((struct window *));
874 static int display_line P_ ((struct it *));
875 static int display_mode_lines P_ ((struct window *));
876 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
877 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
878 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
879 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
880 static void display_menu_bar P_ ((struct window *));
881 static int display_count_lines P_ ((int, int, int, int, int *));
882 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
883 int, int, struct it *, int, int, int, int));
884 static void compute_line_metrics P_ ((struct it *));
885 static void run_redisplay_end_trigger_hook P_ ((struct it *));
886 static int get_overlay_strings P_ ((struct it *, int));
887 static void next_overlay_string P_ ((struct it *));
888 static void reseat P_ ((struct it *, struct text_pos, int));
889 static void reseat_1 P_ ((struct it *, struct text_pos, int));
890 static void back_to_previous_visible_line_start P_ ((struct it *));
891 static void reseat_at_previous_visible_line_start P_ ((struct it *));
892 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
893 static int next_element_from_display_vector P_ ((struct it *));
894 static int next_element_from_string P_ ((struct it *));
895 static int next_element_from_c_string P_ ((struct it *));
896 static int next_element_from_buffer P_ ((struct it *));
897 static int next_element_from_composition P_ ((struct it *));
898 static int next_element_from_image P_ ((struct it *));
899 static int next_element_from_stretch P_ ((struct it *));
900 static void load_overlay_strings P_ ((struct it *, int));
901 static int init_from_display_pos P_ ((struct it *, struct window *,
902 struct display_pos *));
903 static void reseat_to_string P_ ((struct it *, unsigned char *,
904 Lisp_Object, int, int, int, int));
905 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
906 int, int, int));
907 void move_it_vertically_backward P_ ((struct it *, int));
908 static void init_to_row_start P_ ((struct it *, struct window *,
909 struct glyph_row *));
910 static int init_to_row_end P_ ((struct it *, struct window *,
911 struct glyph_row *));
912 static void back_to_previous_line_start P_ ((struct it *));
913 static int forward_to_next_line_start P_ ((struct it *, int *));
914 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
915 Lisp_Object, int));
916 static struct text_pos string_pos P_ ((int, Lisp_Object));
917 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
918 static int number_of_chars P_ ((unsigned char *, int));
919 static void compute_stop_pos P_ ((struct it *));
920 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
921 Lisp_Object));
922 static int face_before_or_after_it_pos P_ ((struct it *, int));
923 static int next_overlay_change P_ ((int));
924 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
925 Lisp_Object, struct text_pos *,
926 int));
927 static int underlying_face_id P_ ((struct it *));
928 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
929 struct window *));
931 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
932 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
934 #ifdef HAVE_WINDOW_SYSTEM
936 static void update_tool_bar P_ ((struct frame *, int));
937 static void build_desired_tool_bar_string P_ ((struct frame *f));
938 static int redisplay_tool_bar P_ ((struct frame *));
939 static void display_tool_bar_line P_ ((struct it *));
940 static void notice_overwritten_cursor P_ ((struct window *,
941 enum glyph_row_area,
942 int, int, int, int));
946 #endif /* HAVE_WINDOW_SYSTEM */
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
957 This is the height of W minus the height of a mode line, if any. */
959 INLINE int
960 window_text_bottom_y (w)
961 struct window *w;
963 int height = WINDOW_TOTAL_HEIGHT (w);
965 if (WINDOW_WANTS_MODELINE_P (w))
966 height -= CURRENT_MODE_LINE_HEIGHT (w);
967 return height;
970 /* Return the pixel width of display area AREA of window W. AREA < 0
971 means return the total width of W, not including fringes to
972 the left and right of the window. */
974 INLINE int
975 window_box_width (w, area)
976 struct window *w;
977 int area;
979 int cols = XFASTINT (w->total_cols);
980 int pixels = 0;
982 if (!w->pseudo_window_p)
984 cols -= WINDOW_SCROLL_BAR_COLS (w);
986 if (area == TEXT_AREA)
988 if (INTEGERP (w->left_margin_cols))
989 cols -= XFASTINT (w->left_margin_cols);
990 if (INTEGERP (w->right_margin_cols))
991 cols -= XFASTINT (w->right_margin_cols);
992 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
994 else if (area == LEFT_MARGIN_AREA)
996 cols = (INTEGERP (w->left_margin_cols)
997 ? XFASTINT (w->left_margin_cols) : 0);
998 pixels = 0;
1000 else if (area == RIGHT_MARGIN_AREA)
1002 cols = (INTEGERP (w->right_margin_cols)
1003 ? XFASTINT (w->right_margin_cols) : 0);
1004 pixels = 0;
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1015 INLINE int
1016 window_box_height (w)
1017 struct window *w;
1019 struct frame *f = XFRAME (w->frame);
1020 int height = WINDOW_TOTAL_HEIGHT (w);
1022 xassert (height >= 0);
1024 /* Note: the code below that determines the mode-line/header-line
1025 height is essentially the same as that contained in the macro
1026 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1027 the appropriate glyph row has its `mode_line_p' flag set,
1028 and if it doesn't, uses estimate_mode_line_height instead. */
1030 if (WINDOW_WANTS_MODELINE_P (w))
1032 struct glyph_row *ml_row
1033 = (w->current_matrix && w->current_matrix->rows
1034 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1035 : 0);
1036 if (ml_row && ml_row->mode_line_p)
1037 height -= ml_row->height;
1038 else
1039 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1042 if (WINDOW_WANTS_HEADER_LINE_P (w))
1044 struct glyph_row *hl_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (hl_row && hl_row->mode_line_p)
1049 height -= hl_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1054 /* With a very small font and a mode-line that's taller than
1055 default, we might end up with a negative height. */
1056 return max (0, height);
1059 /* Return the window-relative coordinate of the left edge of display
1060 area AREA of window W. AREA < 0 means return the left edge of the
1061 whole window, to the right of the left fringe of W. */
1063 INLINE int
1064 window_box_left_offset (w, area)
1065 struct window *w;
1066 int area;
1068 int x;
1070 if (w->pseudo_window_p)
1071 return 0;
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1089 return x;
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the left of the right fringe of W. */
1097 INLINE int
1098 window_box_right_offset (w, area)
1099 struct window *w;
1100 int area;
1102 return window_box_left_offset (w, area) + window_box_width (w, area);
1105 /* Return the frame-relative coordinate of the left edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the right of the left fringe of W. */
1109 INLINE int
1110 window_box_left (w, area)
1111 struct window *w;
1112 int area;
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1123 return x;
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the left of the right fringe of W. */
1131 INLINE int
1132 window_box_right (w, area)
1133 struct window *w;
1134 int area;
1136 return window_box_left (w, area) + window_box_width (w, area);
1139 /* Get the bounding box of the display area AREA of window W, without
1140 mode lines, in frame-relative coordinates. AREA < 0 means the
1141 whole window, not including the left and right fringes of
1142 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1143 coordinates of the upper-left corner of the box. Return in
1144 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1146 INLINE void
1147 window_box (w, area, box_x, box_y, box_width, box_height)
1148 struct window *w;
1149 int area;
1150 int *box_x, *box_y, *box_width, *box_height;
1152 if (box_width)
1153 *box_width = window_box_width (w, area);
1154 if (box_height)
1155 *box_height = window_box_height (w);
1156 if (box_x)
1157 *box_x = window_box_left (w, area);
1158 if (box_y)
1160 *box_y = WINDOW_TOP_EDGE_Y (w);
1161 if (WINDOW_WANTS_HEADER_LINE_P (w))
1162 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines. AREA < 0 means the whole window, not including the
1169 left and right fringe of the window. Return in *TOP_LEFT_X
1170 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1171 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1172 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1173 box. */
1175 INLINE void
1176 window_box_edges (w, area, top_left_x, top_left_y,
1177 bottom_right_x, bottom_right_y)
1178 struct window *w;
1179 int area;
1180 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1182 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1183 bottom_right_y);
1184 *bottom_right_x += *top_left_x;
1185 *bottom_right_y += *top_left_y;
1190 /***********************************************************************
1191 Utilities
1192 ***********************************************************************/
1194 /* Return the bottom y-position of the line the iterator IT is in.
1195 This can modify IT's settings. */
1198 line_bottom_y (it)
1199 struct it *it;
1201 int line_height = it->max_ascent + it->max_descent;
1202 int line_top_y = it->current_y;
1204 if (line_height == 0)
1206 if (last_height)
1207 line_height = last_height;
1208 else if (IT_CHARPOS (*it) < ZV)
1210 move_it_by_lines (it, 1, 1);
1211 line_height = (it->max_ascent || it->max_descent
1212 ? it->max_ascent + it->max_descent
1213 : last_height);
1215 else
1217 struct glyph_row *row = it->glyph_row;
1219 /* Use the default character height. */
1220 it->glyph_row = NULL;
1221 it->what = IT_CHARACTER;
1222 it->c = ' ';
1223 it->len = 1;
1224 PRODUCE_GLYPHS (it);
1225 line_height = it->ascent + it->descent;
1226 it->glyph_row = row;
1230 return line_top_y + line_height;
1234 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1235 1 if POS is visible and the line containing POS is fully visible.
1236 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1237 and header-lines heights. */
1240 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1241 struct window *w;
1242 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1244 struct it it;
1245 struct text_pos top;
1246 int visible_p;
1247 struct buffer *old_buffer = NULL;
1249 if (XBUFFER (w->buffer) != current_buffer)
1251 old_buffer = current_buffer;
1252 set_buffer_internal_1 (XBUFFER (w->buffer));
1255 *fully = visible_p = 0;
1256 SET_TEXT_POS_FROM_MARKER (top, w->start);
1258 /* Compute exact mode line heights, if requested. */
1259 if (exact_mode_line_heights_p)
1261 if (WINDOW_WANTS_MODELINE_P (w))
1262 current_mode_line_height
1263 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1264 current_buffer->mode_line_format);
1266 if (WINDOW_WANTS_HEADER_LINE_P (w))
1267 current_header_line_height
1268 = display_mode_line (w, HEADER_LINE_FACE_ID,
1269 current_buffer->header_line_format);
1272 start_display (&it, w, top);
1273 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1274 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1276 /* Note that we may overshoot because of invisible text. */
1277 if (IT_CHARPOS (it) >= charpos)
1279 int top_y = it.current_y;
1280 int bottom_y = line_bottom_y (&it);
1281 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1283 if (top_y < window_top_y)
1284 visible_p = bottom_y > window_top_y;
1285 else if (top_y < it.last_visible_y)
1287 visible_p = 1;
1288 *fully = bottom_y <= it.last_visible_y;
1290 if (visible_p && x)
1292 *x = it.current_x;
1293 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1296 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1298 struct it it2;
1300 it2 = it;
1301 move_it_by_lines (&it, 1, 0);
1302 if (charpos < IT_CHARPOS (it))
1304 visible_p = 1;
1305 if (x)
1307 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1308 *x = it2.current_x;
1309 *y = it2.current_y + it2.max_ascent - it2.ascent;
1314 if (old_buffer)
1315 set_buffer_internal_1 (old_buffer);
1317 current_header_line_height = current_mode_line_height = -1;
1319 return visible_p;
1323 /* Return the next character from STR which is MAXLEN bytes long.
1324 Return in *LEN the length of the character. This is like
1325 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1326 we find one, we return a `?', but with the length of the invalid
1327 character. */
1329 static INLINE int
1330 string_char_and_length (str, maxlen, len)
1331 const unsigned char *str;
1332 int maxlen, *len;
1334 int c;
1336 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1337 if (!CHAR_VALID_P (c, 1))
1338 /* We may not change the length here because other places in Emacs
1339 don't use this function, i.e. they silently accept invalid
1340 characters. */
1341 c = '?';
1343 return c;
1348 /* Given a position POS containing a valid character and byte position
1349 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1351 static struct text_pos
1352 string_pos_nchars_ahead (pos, string, nchars)
1353 struct text_pos pos;
1354 Lisp_Object string;
1355 int nchars;
1357 xassert (STRINGP (string) && nchars >= 0);
1359 if (STRING_MULTIBYTE (string))
1361 int rest = SBYTES (string) - BYTEPOS (pos);
1362 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1363 int len;
1365 while (nchars--)
1367 string_char_and_length (p, rest, &len);
1368 p += len, rest -= len;
1369 xassert (rest >= 0);
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1377 return pos;
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1384 static INLINE struct text_pos
1385 string_pos (charpos, string)
1386 int charpos;
1387 Lisp_Object string;
1389 struct text_pos pos;
1390 xassert (STRINGP (string));
1391 xassert (charpos >= 0);
1392 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1393 return pos;
1397 /* Value is a text position, i.e. character and byte position, for
1398 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1399 means recognize multibyte characters. */
1401 static struct text_pos
1402 c_string_pos (charpos, s, multibyte_p)
1403 int charpos;
1404 unsigned char *s;
1405 int multibyte_p;
1407 struct text_pos pos;
1409 xassert (s != NULL);
1410 xassert (charpos >= 0);
1412 if (multibyte_p)
1414 int rest = strlen (s), len;
1416 SET_TEXT_POS (pos, 0, 0);
1417 while (charpos--)
1419 string_char_and_length (s, rest, &len);
1420 s += len, rest -= len;
1421 xassert (rest >= 0);
1422 CHARPOS (pos) += 1;
1423 BYTEPOS (pos) += len;
1426 else
1427 SET_TEXT_POS (pos, charpos, charpos);
1429 return pos;
1433 /* Value is the number of characters in C string S. MULTIBYTE_P
1434 non-zero means recognize multibyte characters. */
1436 static int
1437 number_of_chars (s, multibyte_p)
1438 unsigned char *s;
1439 int multibyte_p;
1441 int nchars;
1443 if (multibyte_p)
1445 int rest = strlen (s), len;
1446 unsigned char *p = (unsigned char *) s;
1448 for (nchars = 0; rest > 0; ++nchars)
1450 string_char_and_length (p, rest, &len);
1451 rest -= len, p += len;
1454 else
1455 nchars = strlen (s);
1457 return nchars;
1461 /* Compute byte position NEWPOS->bytepos corresponding to
1462 NEWPOS->charpos. POS is a known position in string STRING.
1463 NEWPOS->charpos must be >= POS.charpos. */
1465 static void
1466 compute_string_pos (newpos, pos, string)
1467 struct text_pos *newpos, pos;
1468 Lisp_Object string;
1470 xassert (STRINGP (string));
1471 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1473 if (STRING_MULTIBYTE (string))
1474 *newpos = string_pos_nchars_ahead (pos, string,
1475 CHARPOS (*newpos) - CHARPOS (pos));
1476 else
1477 BYTEPOS (*newpos) = CHARPOS (*newpos);
1480 /* EXPORT:
1481 Return an estimation of the pixel height of mode or top lines on
1482 frame F. FACE_ID specifies what line's height to estimate. */
1485 estimate_mode_line_height (f, face_id)
1486 struct frame *f;
1487 enum face_id face_id;
1489 #ifdef HAVE_WINDOW_SYSTEM
1490 if (FRAME_WINDOW_P (f))
1492 int height = FONT_HEIGHT (FRAME_FONT (f));
1494 /* This function is called so early when Emacs starts that the face
1495 cache and mode line face are not yet initialized. */
1496 if (FRAME_FACE_CACHE (f))
1498 struct face *face = FACE_FROM_ID (f, face_id);
1499 if (face)
1501 if (face->font)
1502 height = FONT_HEIGHT (face->font);
1503 if (face->box_line_width > 0)
1504 height += 2 * face->box_line_width;
1508 return height;
1510 #endif
1512 return 1;
1515 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1516 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1517 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1518 not force the value into range. */
1520 void
1521 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1522 FRAME_PTR f;
1523 register int pix_x, pix_y;
1524 int *x, *y;
1525 NativeRectangle *bounds;
1526 int noclip;
1529 #ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (f))
1532 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1533 even for negative values. */
1534 if (pix_x < 0)
1535 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1536 if (pix_y < 0)
1537 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1539 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1540 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1542 if (bounds)
1543 STORE_NATIVE_RECT (*bounds,
1544 FRAME_COL_TO_PIXEL_X (f, pix_x),
1545 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1546 FRAME_COLUMN_WIDTH (f) - 1,
1547 FRAME_LINE_HEIGHT (f) - 1);
1549 if (!noclip)
1551 if (pix_x < 0)
1552 pix_x = 0;
1553 else if (pix_x > FRAME_TOTAL_COLS (f))
1554 pix_x = FRAME_TOTAL_COLS (f);
1556 if (pix_y < 0)
1557 pix_y = 0;
1558 else if (pix_y > FRAME_LINES (f))
1559 pix_y = FRAME_LINES (f);
1562 #endif
1564 *x = pix_x;
1565 *y = pix_y;
1569 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1570 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1571 can't tell the positions because W's display is not up to date,
1572 return 0. */
1575 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1576 struct window *w;
1577 int hpos, vpos;
1578 int *frame_x, *frame_y;
1580 #ifdef HAVE_WINDOW_SYSTEM
1581 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1583 int success_p;
1585 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1586 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1588 if (display_completed)
1590 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1591 struct glyph *glyph = row->glyphs[TEXT_AREA];
1592 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1594 hpos = row->x;
1595 vpos = row->y;
1596 while (glyph < end)
1598 hpos += glyph->pixel_width;
1599 ++glyph;
1602 /* If first glyph is partially visible, its first visible position is still 0. */
1603 if (hpos < 0)
1604 hpos = 0;
1606 success_p = 1;
1608 else
1610 hpos = vpos = 0;
1611 success_p = 0;
1614 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1615 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1616 return success_p;
1618 #endif
1620 *frame_x = hpos;
1621 *frame_y = vpos;
1622 return 1;
1626 #ifdef HAVE_WINDOW_SYSTEM
1628 /* Find the glyph under window-relative coordinates X/Y in window W.
1629 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1630 strings. Return in *HPOS and *VPOS the row and column number of
1631 the glyph found. Return in *AREA the glyph area containing X.
1632 Value is a pointer to the glyph found or null if X/Y is not on
1633 text, or we can't tell because W's current matrix is not up to
1634 date. */
1636 static struct glyph *
1637 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1638 struct window *w;
1639 int x, y;
1640 int *hpos, *vpos, *dx, *dy, *area;
1642 struct glyph *glyph, *end;
1643 struct glyph_row *row = NULL;
1644 int x0, i;
1646 /* Find row containing Y. Give up if some row is not enabled. */
1647 for (i = 0; i < w->current_matrix->nrows; ++i)
1649 row = MATRIX_ROW (w->current_matrix, i);
1650 if (!row->enabled_p)
1651 return NULL;
1652 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1653 break;
1656 *vpos = i;
1657 *hpos = 0;
1659 /* Give up if Y is not in the window. */
1660 if (i == w->current_matrix->nrows)
1661 return NULL;
1663 /* Get the glyph area containing X. */
1664 if (w->pseudo_window_p)
1666 *area = TEXT_AREA;
1667 x0 = 0;
1669 else
1671 if (x < window_box_left_offset (w, TEXT_AREA))
1673 *area = LEFT_MARGIN_AREA;
1674 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1676 else if (x < window_box_right_offset (w, TEXT_AREA))
1678 *area = TEXT_AREA;
1679 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1681 else
1683 *area = RIGHT_MARGIN_AREA;
1684 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1688 /* Find glyph containing X. */
1689 glyph = row->glyphs[*area];
1690 end = glyph + row->used[*area];
1691 x -= x0;
1692 while (glyph < end && x >= glyph->pixel_width)
1694 x -= glyph->pixel_width;
1695 ++glyph;
1698 if (glyph == end)
1699 return NULL;
1701 if (dx)
1703 *dx = x;
1704 *dy = y - (row->y + row->ascent - glyph->ascent);
1707 *hpos = glyph - row->glyphs[*area];
1708 return glyph;
1712 /* EXPORT:
1713 Convert frame-relative x/y to coordinates relative to window W.
1714 Takes pseudo-windows into account. */
1716 void
1717 frame_to_window_pixel_xy (w, x, y)
1718 struct window *w;
1719 int *x, *y;
1721 if (w->pseudo_window_p)
1723 /* A pseudo-window is always full-width, and starts at the
1724 left edge of the frame, plus a frame border. */
1725 struct frame *f = XFRAME (w->frame);
1726 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1727 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1729 else
1731 *x -= WINDOW_LEFT_EDGE_X (w);
1732 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1736 /* EXPORT:
1737 Return in *R the clipping rectangle for glyph string S. */
1739 void
1740 get_glyph_string_clip_rect (s, nr)
1741 struct glyph_string *s;
1742 NativeRectangle *nr;
1744 XRectangle r;
1746 if (s->row->full_width_p)
1748 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1749 r.x = WINDOW_LEFT_EDGE_X (s->w);
1750 r.width = WINDOW_TOTAL_WIDTH (s->w);
1752 /* Unless displaying a mode or menu bar line, which are always
1753 fully visible, clip to the visible part of the row. */
1754 if (s->w->pseudo_window_p)
1755 r.height = s->row->visible_height;
1756 else
1757 r.height = s->height;
1759 else
1761 /* This is a text line that may be partially visible. */
1762 r.x = window_box_left (s->w, s->area);
1763 r.width = window_box_width (s->w, s->area);
1764 r.height = s->row->visible_height;
1767 /* If S draws overlapping rows, it's sufficient to use the top and
1768 bottom of the window for clipping because this glyph string
1769 intentionally draws over other lines. */
1770 if (s->for_overlaps_p)
1772 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1773 r.height = window_text_bottom_y (s->w) - r.y;
1775 else
1777 /* Don't use S->y for clipping because it doesn't take partially
1778 visible lines into account. For example, it can be negative for
1779 partially visible lines at the top of a window. */
1780 if (!s->row->full_width_p
1781 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1782 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1783 else
1784 r.y = max (0, s->row->y);
1786 /* If drawing a tool-bar window, draw it over the internal border
1787 at the top of the window. */
1788 if (s->w == XWINDOW (s->f->tool_bar_window))
1789 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1792 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1794 /* If drawing the cursor, don't let glyph draw outside its
1795 advertised boundaries. Cleartype does this under some circumstances. */
1796 if (s->hl == DRAW_CURSOR)
1798 struct glyph *glyph = s->first_glyph;
1799 int height;
1801 if (s->x > r.x)
1803 r.width -= s->x - r.x;
1804 r.x = s->x;
1806 r.width = min (r.width, glyph->pixel_width);
1808 /* Don't draw cursor glyph taller than our actual glyph. */
1809 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1810 if (height < r.height)
1812 int max_y = r.y + r.height;
1813 r.y = min (max_y, s->ybase + glyph->descent - height);
1814 r.height = min (max_y - r.y, height);
1818 #ifdef CONVERT_FROM_XRECT
1819 CONVERT_FROM_XRECT (r, *nr);
1820 #else
1821 *nr = r;
1822 #endif
1825 #endif /* HAVE_WINDOW_SYSTEM */
1828 /***********************************************************************
1829 Lisp form evaluation
1830 ***********************************************************************/
1832 /* Error handler for safe_eval and safe_call. */
1834 static Lisp_Object
1835 safe_eval_handler (arg)
1836 Lisp_Object arg;
1838 add_to_log ("Error during redisplay: %s", arg, Qnil);
1839 return Qnil;
1843 /* Evaluate SEXPR and return the result, or nil if something went
1844 wrong. Prevent redisplay during the evaluation. */
1846 Lisp_Object
1847 safe_eval (sexpr)
1848 Lisp_Object sexpr;
1850 Lisp_Object val;
1852 if (inhibit_eval_during_redisplay)
1853 val = Qnil;
1854 else
1856 int count = SPECPDL_INDEX ();
1857 struct gcpro gcpro1;
1859 GCPRO1 (sexpr);
1860 specbind (Qinhibit_redisplay, Qt);
1861 /* Use Qt to ensure debugger does not run,
1862 so there is no possibility of wanting to redisplay. */
1863 val = internal_condition_case_1 (Feval, sexpr, Qt,
1864 safe_eval_handler);
1865 UNGCPRO;
1866 val = unbind_to (count, val);
1869 return val;
1873 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1874 Return the result, or nil if something went wrong. Prevent
1875 redisplay during the evaluation. */
1877 Lisp_Object
1878 safe_call (nargs, args)
1879 int nargs;
1880 Lisp_Object *args;
1882 Lisp_Object val;
1884 if (inhibit_eval_during_redisplay)
1885 val = Qnil;
1886 else
1888 int count = SPECPDL_INDEX ();
1889 struct gcpro gcpro1;
1891 GCPRO1 (args[0]);
1892 gcpro1.nvars = nargs;
1893 specbind (Qinhibit_redisplay, Qt);
1894 /* Use Qt to ensure debugger does not run,
1895 so there is no possibility of wanting to redisplay. */
1896 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1897 safe_eval_handler);
1898 UNGCPRO;
1899 val = unbind_to (count, val);
1902 return val;
1906 /* Call function FN with one argument ARG.
1907 Return the result, or nil if something went wrong. */
1909 Lisp_Object
1910 safe_call1 (fn, arg)
1911 Lisp_Object fn, arg;
1913 Lisp_Object args[2];
1914 args[0] = fn;
1915 args[1] = arg;
1916 return safe_call (2, args);
1921 /***********************************************************************
1922 Debugging
1923 ***********************************************************************/
1925 #if 0
1927 /* Define CHECK_IT to perform sanity checks on iterators.
1928 This is for debugging. It is too slow to do unconditionally. */
1930 static void
1931 check_it (it)
1932 struct it *it;
1934 if (it->method == next_element_from_string)
1936 xassert (STRINGP (it->string));
1937 xassert (IT_STRING_CHARPOS (*it) >= 0);
1939 else
1941 xassert (IT_STRING_CHARPOS (*it) < 0);
1942 if (it->method == next_element_from_buffer)
1944 /* Check that character and byte positions agree. */
1945 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1949 if (it->dpvec)
1950 xassert (it->current.dpvec_index >= 0);
1951 else
1952 xassert (it->current.dpvec_index < 0);
1955 #define CHECK_IT(IT) check_it ((IT))
1957 #else /* not 0 */
1959 #define CHECK_IT(IT) (void) 0
1961 #endif /* not 0 */
1964 #if GLYPH_DEBUG
1966 /* Check that the window end of window W is what we expect it
1967 to be---the last row in the current matrix displaying text. */
1969 static void
1970 check_window_end (w)
1971 struct window *w;
1973 if (!MINI_WINDOW_P (w)
1974 && !NILP (w->window_end_valid))
1976 struct glyph_row *row;
1977 xassert ((row = MATRIX_ROW (w->current_matrix,
1978 XFASTINT (w->window_end_vpos)),
1979 !row->enabled_p
1980 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1981 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1985 #define CHECK_WINDOW_END(W) check_window_end ((W))
1987 #else /* not GLYPH_DEBUG */
1989 #define CHECK_WINDOW_END(W) (void) 0
1991 #endif /* not GLYPH_DEBUG */
1995 /***********************************************************************
1996 Iterator initialization
1997 ***********************************************************************/
1999 /* Initialize IT for displaying current_buffer in window W, starting
2000 at character position CHARPOS. CHARPOS < 0 means that no buffer
2001 position is specified which is useful when the iterator is assigned
2002 a position later. BYTEPOS is the byte position corresponding to
2003 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2005 If ROW is not null, calls to produce_glyphs with IT as parameter
2006 will produce glyphs in that row.
2008 BASE_FACE_ID is the id of a base face to use. It must be one of
2009 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2010 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2011 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2013 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2014 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2015 will be initialized to use the corresponding mode line glyph row of
2016 the desired matrix of W. */
2018 void
2019 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2020 struct it *it;
2021 struct window *w;
2022 int charpos, bytepos;
2023 struct glyph_row *row;
2024 enum face_id base_face_id;
2026 int highlight_region_p;
2028 /* Some precondition checks. */
2029 xassert (w != NULL && it != NULL);
2030 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2031 && charpos <= ZV));
2033 /* If face attributes have been changed since the last redisplay,
2034 free realized faces now because they depend on face definitions
2035 that might have changed. Don't free faces while there might be
2036 desired matrices pending which reference these faces. */
2037 if (face_change_count && !inhibit_free_realized_faces)
2039 face_change_count = 0;
2040 free_all_realized_faces (Qnil);
2043 /* Use one of the mode line rows of W's desired matrix if
2044 appropriate. */
2045 if (row == NULL)
2047 if (base_face_id == MODE_LINE_FACE_ID
2048 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2049 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2050 else if (base_face_id == HEADER_LINE_FACE_ID)
2051 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2054 /* Clear IT. */
2055 bzero (it, sizeof *it);
2056 it->current.overlay_string_index = -1;
2057 it->current.dpvec_index = -1;
2058 it->base_face_id = base_face_id;
2059 it->string = Qnil;
2060 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2062 /* The window in which we iterate over current_buffer: */
2063 XSETWINDOW (it->window, w);
2064 it->w = w;
2065 it->f = XFRAME (w->frame);
2067 /* Extra space between lines (on window systems only). */
2068 if (base_face_id == DEFAULT_FACE_ID
2069 && FRAME_WINDOW_P (it->f))
2071 if (NATNUMP (current_buffer->extra_line_spacing))
2072 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2073 else if (FLOATP (current_buffer->extra_line_spacing))
2074 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2075 * FRAME_LINE_HEIGHT (it->f));
2076 else if (it->f->extra_line_spacing > 0)
2077 it->extra_line_spacing = it->f->extra_line_spacing;
2080 /* If realized faces have been removed, e.g. because of face
2081 attribute changes of named faces, recompute them. When running
2082 in batch mode, the face cache of Vterminal_frame is null. If
2083 we happen to get called, make a dummy face cache. */
2084 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2085 init_frame_faces (it->f);
2086 if (FRAME_FACE_CACHE (it->f)->used == 0)
2087 recompute_basic_faces (it->f);
2089 /* Current value of the `slice', `space-width', and 'height' properties. */
2090 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2091 it->space_width = Qnil;
2092 it->font_height = Qnil;
2093 it->override_ascent = -1;
2095 /* Are control characters displayed as `^C'? */
2096 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2098 /* -1 means everything between a CR and the following line end
2099 is invisible. >0 means lines indented more than this value are
2100 invisible. */
2101 it->selective = (INTEGERP (current_buffer->selective_display)
2102 ? XFASTINT (current_buffer->selective_display)
2103 : (!NILP (current_buffer->selective_display)
2104 ? -1 : 0));
2105 it->selective_display_ellipsis_p
2106 = !NILP (current_buffer->selective_display_ellipses);
2108 /* Display table to use. */
2109 it->dp = window_display_table (w);
2111 /* Are multibyte characters enabled in current_buffer? */
2112 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2114 /* Non-zero if we should highlight the region. */
2115 highlight_region_p
2116 = (!NILP (Vtransient_mark_mode)
2117 && !NILP (current_buffer->mark_active)
2118 && XMARKER (current_buffer->mark)->buffer != 0);
2120 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2121 start and end of a visible region in window IT->w. Set both to
2122 -1 to indicate no region. */
2123 if (highlight_region_p
2124 /* Maybe highlight only in selected window. */
2125 && (/* Either show region everywhere. */
2126 highlight_nonselected_windows
2127 /* Or show region in the selected window. */
2128 || w == XWINDOW (selected_window)
2129 /* Or show the region if we are in the mini-buffer and W is
2130 the window the mini-buffer refers to. */
2131 || (MINI_WINDOW_P (XWINDOW (selected_window))
2132 && WINDOWP (minibuf_selected_window)
2133 && w == XWINDOW (minibuf_selected_window))))
2135 int charpos = marker_position (current_buffer->mark);
2136 it->region_beg_charpos = min (PT, charpos);
2137 it->region_end_charpos = max (PT, charpos);
2139 else
2140 it->region_beg_charpos = it->region_end_charpos = -1;
2142 /* Get the position at which the redisplay_end_trigger hook should
2143 be run, if it is to be run at all. */
2144 if (MARKERP (w->redisplay_end_trigger)
2145 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2146 it->redisplay_end_trigger_charpos
2147 = marker_position (w->redisplay_end_trigger);
2148 else if (INTEGERP (w->redisplay_end_trigger))
2149 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2151 /* Correct bogus values of tab_width. */
2152 it->tab_width = XINT (current_buffer->tab_width);
2153 if (it->tab_width <= 0 || it->tab_width > 1000)
2154 it->tab_width = 8;
2156 /* Are lines in the display truncated? */
2157 it->truncate_lines_p
2158 = (base_face_id != DEFAULT_FACE_ID
2159 || XINT (it->w->hscroll)
2160 || (truncate_partial_width_windows
2161 && !WINDOW_FULL_WIDTH_P (it->w))
2162 || !NILP (current_buffer->truncate_lines));
2164 /* Get dimensions of truncation and continuation glyphs. These are
2165 displayed as fringe bitmaps under X, so we don't need them for such
2166 frames. */
2167 if (!FRAME_WINDOW_P (it->f))
2169 if (it->truncate_lines_p)
2171 /* We will need the truncation glyph. */
2172 xassert (it->glyph_row == NULL);
2173 produce_special_glyphs (it, IT_TRUNCATION);
2174 it->truncation_pixel_width = it->pixel_width;
2176 else
2178 /* We will need the continuation glyph. */
2179 xassert (it->glyph_row == NULL);
2180 produce_special_glyphs (it, IT_CONTINUATION);
2181 it->continuation_pixel_width = it->pixel_width;
2184 /* Reset these values to zero because the produce_special_glyphs
2185 above has changed them. */
2186 it->pixel_width = it->ascent = it->descent = 0;
2187 it->phys_ascent = it->phys_descent = 0;
2190 /* Set this after getting the dimensions of truncation and
2191 continuation glyphs, so that we don't produce glyphs when calling
2192 produce_special_glyphs, above. */
2193 it->glyph_row = row;
2194 it->area = TEXT_AREA;
2196 /* Get the dimensions of the display area. The display area
2197 consists of the visible window area plus a horizontally scrolled
2198 part to the left of the window. All x-values are relative to the
2199 start of this total display area. */
2200 if (base_face_id != DEFAULT_FACE_ID)
2202 /* Mode lines, menu bar in terminal frames. */
2203 it->first_visible_x = 0;
2204 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2206 else
2208 it->first_visible_x
2209 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2210 it->last_visible_x = (it->first_visible_x
2211 + window_box_width (w, TEXT_AREA));
2213 /* If we truncate lines, leave room for the truncator glyph(s) at
2214 the right margin. Otherwise, leave room for the continuation
2215 glyph(s). Truncation and continuation glyphs are not inserted
2216 for window-based redisplay. */
2217 if (!FRAME_WINDOW_P (it->f))
2219 if (it->truncate_lines_p)
2220 it->last_visible_x -= it->truncation_pixel_width;
2221 else
2222 it->last_visible_x -= it->continuation_pixel_width;
2225 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2226 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2229 /* Leave room for a border glyph. */
2230 if (!FRAME_WINDOW_P (it->f)
2231 && !WINDOW_RIGHTMOST_P (it->w))
2232 it->last_visible_x -= 1;
2234 it->last_visible_y = window_text_bottom_y (w);
2236 /* For mode lines and alike, arrange for the first glyph having a
2237 left box line if the face specifies a box. */
2238 if (base_face_id != DEFAULT_FACE_ID)
2240 struct face *face;
2242 it->face_id = base_face_id;
2244 /* If we have a boxed mode line, make the first character appear
2245 with a left box line. */
2246 face = FACE_FROM_ID (it->f, base_face_id);
2247 if (face->box != FACE_NO_BOX)
2248 it->start_of_box_run_p = 1;
2251 /* If a buffer position was specified, set the iterator there,
2252 getting overlays and face properties from that position. */
2253 if (charpos >= BUF_BEG (current_buffer))
2255 it->end_charpos = ZV;
2256 it->face_id = -1;
2257 IT_CHARPOS (*it) = charpos;
2259 /* Compute byte position if not specified. */
2260 if (bytepos < charpos)
2261 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2262 else
2263 IT_BYTEPOS (*it) = bytepos;
2265 it->start = it->current;
2267 /* Compute faces etc. */
2268 reseat (it, it->current.pos, 1);
2271 CHECK_IT (it);
2275 /* Initialize IT for the display of window W with window start POS. */
2277 void
2278 start_display (it, w, pos)
2279 struct it *it;
2280 struct window *w;
2281 struct text_pos pos;
2283 struct glyph_row *row;
2284 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2286 row = w->desired_matrix->rows + first_vpos;
2287 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2288 it->first_vpos = first_vpos;
2290 if (!it->truncate_lines_p)
2292 int start_at_line_beg_p;
2293 int first_y = it->current_y;
2295 /* If window start is not at a line start, skip forward to POS to
2296 get the correct continuation lines width. */
2297 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2298 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2299 if (!start_at_line_beg_p)
2301 int new_x;
2303 reseat_at_previous_visible_line_start (it);
2304 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2306 new_x = it->current_x + it->pixel_width;
2308 /* If lines are continued, this line may end in the middle
2309 of a multi-glyph character (e.g. a control character
2310 displayed as \003, or in the middle of an overlay
2311 string). In this case move_it_to above will not have
2312 taken us to the start of the continuation line but to the
2313 end of the continued line. */
2314 if (it->current_x > 0
2315 && !it->truncate_lines_p /* Lines are continued. */
2316 && (/* And glyph doesn't fit on the line. */
2317 new_x > it->last_visible_x
2318 /* Or it fits exactly and we're on a window
2319 system frame. */
2320 || (new_x == it->last_visible_x
2321 && FRAME_WINDOW_P (it->f))))
2323 if (it->current.dpvec_index >= 0
2324 || it->current.overlay_string_index >= 0)
2326 set_iterator_to_next (it, 1);
2327 move_it_in_display_line_to (it, -1, -1, 0);
2330 it->continuation_lines_width += it->current_x;
2333 /* We're starting a new display line, not affected by the
2334 height of the continued line, so clear the appropriate
2335 fields in the iterator structure. */
2336 it->max_ascent = it->max_descent = 0;
2337 it->max_phys_ascent = it->max_phys_descent = 0;
2339 it->current_y = first_y;
2340 it->vpos = 0;
2341 it->current_x = it->hpos = 0;
2345 #if 0 /* Don't assert the following because start_display is sometimes
2346 called intentionally with a window start that is not at a
2347 line start. Please leave this code in as a comment. */
2349 /* Window start should be on a line start, now. */
2350 xassert (it->continuation_lines_width
2351 || IT_CHARPOS (it) == BEGV
2352 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2353 #endif /* 0 */
2357 /* Return 1 if POS is a position in ellipses displayed for invisible
2358 text. W is the window we display, for text property lookup. */
2360 static int
2361 in_ellipses_for_invisible_text_p (pos, w)
2362 struct display_pos *pos;
2363 struct window *w;
2365 Lisp_Object prop, window;
2366 int ellipses_p = 0;
2367 int charpos = CHARPOS (pos->pos);
2369 /* If POS specifies a position in a display vector, this might
2370 be for an ellipsis displayed for invisible text. We won't
2371 get the iterator set up for delivering that ellipsis unless
2372 we make sure that it gets aware of the invisible text. */
2373 if (pos->dpvec_index >= 0
2374 && pos->overlay_string_index < 0
2375 && CHARPOS (pos->string_pos) < 0
2376 && charpos > BEGV
2377 && (XSETWINDOW (window, w),
2378 prop = Fget_char_property (make_number (charpos),
2379 Qinvisible, window),
2380 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2382 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2383 window);
2384 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2387 return ellipses_p;
2391 /* Initialize IT for stepping through current_buffer in window W,
2392 starting at position POS that includes overlay string and display
2393 vector/ control character translation position information. Value
2394 is zero if there are overlay strings with newlines at POS. */
2396 static int
2397 init_from_display_pos (it, w, pos)
2398 struct it *it;
2399 struct window *w;
2400 struct display_pos *pos;
2402 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2403 int i, overlay_strings_with_newlines = 0;
2405 /* If POS specifies a position in a display vector, this might
2406 be for an ellipsis displayed for invisible text. We won't
2407 get the iterator set up for delivering that ellipsis unless
2408 we make sure that it gets aware of the invisible text. */
2409 if (in_ellipses_for_invisible_text_p (pos, w))
2411 --charpos;
2412 bytepos = 0;
2415 /* Keep in mind: the call to reseat in init_iterator skips invisible
2416 text, so we might end up at a position different from POS. This
2417 is only a problem when POS is a row start after a newline and an
2418 overlay starts there with an after-string, and the overlay has an
2419 invisible property. Since we don't skip invisible text in
2420 display_line and elsewhere immediately after consuming the
2421 newline before the row start, such a POS will not be in a string,
2422 but the call to init_iterator below will move us to the
2423 after-string. */
2424 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2426 for (i = 0; i < it->n_overlay_strings; ++i)
2428 const char *s = SDATA (it->overlay_strings[i]);
2429 const char *e = s + SBYTES (it->overlay_strings[i]);
2431 while (s < e && *s != '\n')
2432 ++s;
2434 if (s < e)
2436 overlay_strings_with_newlines = 1;
2437 break;
2441 /* If position is within an overlay string, set up IT to the right
2442 overlay string. */
2443 if (pos->overlay_string_index >= 0)
2445 int relative_index;
2447 /* If the first overlay string happens to have a `display'
2448 property for an image, the iterator will be set up for that
2449 image, and we have to undo that setup first before we can
2450 correct the overlay string index. */
2451 if (it->method == next_element_from_image)
2452 pop_it (it);
2454 /* We already have the first chunk of overlay strings in
2455 IT->overlay_strings. Load more until the one for
2456 pos->overlay_string_index is in IT->overlay_strings. */
2457 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2459 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2460 it->current.overlay_string_index = 0;
2461 while (n--)
2463 load_overlay_strings (it, 0);
2464 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2468 it->current.overlay_string_index = pos->overlay_string_index;
2469 relative_index = (it->current.overlay_string_index
2470 % OVERLAY_STRING_CHUNK_SIZE);
2471 it->string = it->overlay_strings[relative_index];
2472 xassert (STRINGP (it->string));
2473 it->current.string_pos = pos->string_pos;
2474 it->method = next_element_from_string;
2477 #if 0 /* This is bogus because POS not having an overlay string
2478 position does not mean it's after the string. Example: A
2479 line starting with a before-string and initialization of IT
2480 to the previous row's end position. */
2481 else if (it->current.overlay_string_index >= 0)
2483 /* If POS says we're already after an overlay string ending at
2484 POS, make sure to pop the iterator because it will be in
2485 front of that overlay string. When POS is ZV, we've thereby
2486 also ``processed'' overlay strings at ZV. */
2487 while (it->sp)
2488 pop_it (it);
2489 it->current.overlay_string_index = -1;
2490 it->method = next_element_from_buffer;
2491 if (CHARPOS (pos->pos) == ZV)
2492 it->overlay_strings_at_end_processed_p = 1;
2494 #endif /* 0 */
2496 if (CHARPOS (pos->string_pos) >= 0)
2498 /* Recorded position is not in an overlay string, but in another
2499 string. This can only be a string from a `display' property.
2500 IT should already be filled with that string. */
2501 it->current.string_pos = pos->string_pos;
2502 xassert (STRINGP (it->string));
2505 /* Restore position in display vector translations, control
2506 character translations or ellipses. */
2507 if (pos->dpvec_index >= 0)
2509 if (it->dpvec == NULL)
2510 get_next_display_element (it);
2511 xassert (it->dpvec && it->current.dpvec_index == 0);
2512 it->current.dpvec_index = pos->dpvec_index;
2515 CHECK_IT (it);
2516 return !overlay_strings_with_newlines;
2520 /* Initialize IT for stepping through current_buffer in window W
2521 starting at ROW->start. */
2523 static void
2524 init_to_row_start (it, w, row)
2525 struct it *it;
2526 struct window *w;
2527 struct glyph_row *row;
2529 init_from_display_pos (it, w, &row->start);
2530 it->start = row->start;
2531 it->continuation_lines_width = row->continuation_lines_width;
2532 CHECK_IT (it);
2536 /* Initialize IT for stepping through current_buffer in window W
2537 starting in the line following ROW, i.e. starting at ROW->end.
2538 Value is zero if there are overlay strings with newlines at ROW's
2539 end position. */
2541 static int
2542 init_to_row_end (it, w, row)
2543 struct it *it;
2544 struct window *w;
2545 struct glyph_row *row;
2547 int success = 0;
2549 if (init_from_display_pos (it, w, &row->end))
2551 if (row->continued_p)
2552 it->continuation_lines_width
2553 = row->continuation_lines_width + row->pixel_width;
2554 CHECK_IT (it);
2555 success = 1;
2558 return success;
2564 /***********************************************************************
2565 Text properties
2566 ***********************************************************************/
2568 /* Called when IT reaches IT->stop_charpos. Handle text property and
2569 overlay changes. Set IT->stop_charpos to the next position where
2570 to stop. */
2572 static void
2573 handle_stop (it)
2574 struct it *it;
2576 enum prop_handled handled;
2577 int handle_overlay_change_p = 1;
2578 struct props *p;
2580 it->dpvec = NULL;
2581 it->current.dpvec_index = -1;
2585 handled = HANDLED_NORMALLY;
2587 /* Call text property handlers. */
2588 for (p = it_props; p->handler; ++p)
2590 handled = p->handler (it);
2592 if (handled == HANDLED_RECOMPUTE_PROPS)
2593 break;
2594 else if (handled == HANDLED_RETURN)
2595 return;
2596 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2597 handle_overlay_change_p = 0;
2600 if (handled != HANDLED_RECOMPUTE_PROPS)
2602 /* Don't check for overlay strings below when set to deliver
2603 characters from a display vector. */
2604 if (it->method == next_element_from_display_vector)
2605 handle_overlay_change_p = 0;
2607 /* Handle overlay changes. */
2608 if (handle_overlay_change_p)
2609 handled = handle_overlay_change (it);
2611 /* Determine where to stop next. */
2612 if (handled == HANDLED_NORMALLY)
2613 compute_stop_pos (it);
2616 while (handled == HANDLED_RECOMPUTE_PROPS);
2620 /* Compute IT->stop_charpos from text property and overlay change
2621 information for IT's current position. */
2623 static void
2624 compute_stop_pos (it)
2625 struct it *it;
2627 register INTERVAL iv, next_iv;
2628 Lisp_Object object, limit, position;
2630 /* If nowhere else, stop at the end. */
2631 it->stop_charpos = it->end_charpos;
2633 if (STRINGP (it->string))
2635 /* Strings are usually short, so don't limit the search for
2636 properties. */
2637 object = it->string;
2638 limit = Qnil;
2639 position = make_number (IT_STRING_CHARPOS (*it));
2641 else
2643 int charpos;
2645 /* If next overlay change is in front of the current stop pos
2646 (which is IT->end_charpos), stop there. Note: value of
2647 next_overlay_change is point-max if no overlay change
2648 follows. */
2649 charpos = next_overlay_change (IT_CHARPOS (*it));
2650 if (charpos < it->stop_charpos)
2651 it->stop_charpos = charpos;
2653 /* If showing the region, we have to stop at the region
2654 start or end because the face might change there. */
2655 if (it->region_beg_charpos > 0)
2657 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2658 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2659 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2660 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2663 /* Set up variables for computing the stop position from text
2664 property changes. */
2665 XSETBUFFER (object, current_buffer);
2666 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2667 position = make_number (IT_CHARPOS (*it));
2671 /* Get the interval containing IT's position. Value is a null
2672 interval if there isn't such an interval. */
2673 iv = validate_interval_range (object, &position, &position, 0);
2674 if (!NULL_INTERVAL_P (iv))
2676 Lisp_Object values_here[LAST_PROP_IDX];
2677 struct props *p;
2679 /* Get properties here. */
2680 for (p = it_props; p->handler; ++p)
2681 values_here[p->idx] = textget (iv->plist, *p->name);
2683 /* Look for an interval following iv that has different
2684 properties. */
2685 for (next_iv = next_interval (iv);
2686 (!NULL_INTERVAL_P (next_iv)
2687 && (NILP (limit)
2688 || XFASTINT (limit) > next_iv->position));
2689 next_iv = next_interval (next_iv))
2691 for (p = it_props; p->handler; ++p)
2693 Lisp_Object new_value;
2695 new_value = textget (next_iv->plist, *p->name);
2696 if (!EQ (values_here[p->idx], new_value))
2697 break;
2700 if (p->handler)
2701 break;
2704 if (!NULL_INTERVAL_P (next_iv))
2706 if (INTEGERP (limit)
2707 && next_iv->position >= XFASTINT (limit))
2708 /* No text property change up to limit. */
2709 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2710 else
2711 /* Text properties change in next_iv. */
2712 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2716 xassert (STRINGP (it->string)
2717 || (it->stop_charpos >= BEGV
2718 && it->stop_charpos >= IT_CHARPOS (*it)));
2722 /* Return the position of the next overlay change after POS in
2723 current_buffer. Value is point-max if no overlay change
2724 follows. This is like `next-overlay-change' but doesn't use
2725 xmalloc. */
2727 static int
2728 next_overlay_change (pos)
2729 int pos;
2731 int noverlays;
2732 int endpos;
2733 Lisp_Object *overlays;
2734 int i;
2736 /* Get all overlays at the given position. */
2737 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2739 /* If any of these overlays ends before endpos,
2740 use its ending point instead. */
2741 for (i = 0; i < noverlays; ++i)
2743 Lisp_Object oend;
2744 int oendpos;
2746 oend = OVERLAY_END (overlays[i]);
2747 oendpos = OVERLAY_POSITION (oend);
2748 endpos = min (endpos, oendpos);
2751 return endpos;
2756 /***********************************************************************
2757 Fontification
2758 ***********************************************************************/
2760 /* Handle changes in the `fontified' property of the current buffer by
2761 calling hook functions from Qfontification_functions to fontify
2762 regions of text. */
2764 static enum prop_handled
2765 handle_fontified_prop (it)
2766 struct it *it;
2768 Lisp_Object prop, pos;
2769 enum prop_handled handled = HANDLED_NORMALLY;
2771 /* Get the value of the `fontified' property at IT's current buffer
2772 position. (The `fontified' property doesn't have a special
2773 meaning in strings.) If the value is nil, call functions from
2774 Qfontification_functions. */
2775 if (!STRINGP (it->string)
2776 && it->s == NULL
2777 && !NILP (Vfontification_functions)
2778 && !NILP (Vrun_hooks)
2779 && (pos = make_number (IT_CHARPOS (*it)),
2780 prop = Fget_char_property (pos, Qfontified, Qnil),
2781 NILP (prop)))
2783 int count = SPECPDL_INDEX ();
2784 Lisp_Object val;
2786 val = Vfontification_functions;
2787 specbind (Qfontification_functions, Qnil);
2789 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2790 safe_call1 (val, pos);
2791 else
2793 Lisp_Object globals, fn;
2794 struct gcpro gcpro1, gcpro2;
2796 globals = Qnil;
2797 GCPRO2 (val, globals);
2799 for (; CONSP (val); val = XCDR (val))
2801 fn = XCAR (val);
2803 if (EQ (fn, Qt))
2805 /* A value of t indicates this hook has a local
2806 binding; it means to run the global binding too.
2807 In a global value, t should not occur. If it
2808 does, we must ignore it to avoid an endless
2809 loop. */
2810 for (globals = Fdefault_value (Qfontification_functions);
2811 CONSP (globals);
2812 globals = XCDR (globals))
2814 fn = XCAR (globals);
2815 if (!EQ (fn, Qt))
2816 safe_call1 (fn, pos);
2819 else
2820 safe_call1 (fn, pos);
2823 UNGCPRO;
2826 unbind_to (count, Qnil);
2828 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2829 something. This avoids an endless loop if they failed to
2830 fontify the text for which reason ever. */
2831 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2832 handled = HANDLED_RECOMPUTE_PROPS;
2835 return handled;
2840 /***********************************************************************
2841 Faces
2842 ***********************************************************************/
2844 /* Set up iterator IT from face properties at its current position.
2845 Called from handle_stop. */
2847 static enum prop_handled
2848 handle_face_prop (it)
2849 struct it *it;
2851 int new_face_id, next_stop;
2853 if (!STRINGP (it->string))
2855 new_face_id
2856 = face_at_buffer_position (it->w,
2857 IT_CHARPOS (*it),
2858 it->region_beg_charpos,
2859 it->region_end_charpos,
2860 &next_stop,
2861 (IT_CHARPOS (*it)
2862 + TEXT_PROP_DISTANCE_LIMIT),
2865 /* Is this a start of a run of characters with box face?
2866 Caveat: this can be called for a freshly initialized
2867 iterator; face_id is -1 in this case. We know that the new
2868 face will not change until limit, i.e. if the new face has a
2869 box, all characters up to limit will have one. But, as
2870 usual, we don't know whether limit is really the end. */
2871 if (new_face_id != it->face_id)
2873 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2875 /* If new face has a box but old face has not, this is
2876 the start of a run of characters with box, i.e. it has
2877 a shadow on the left side. The value of face_id of the
2878 iterator will be -1 if this is the initial call that gets
2879 the face. In this case, we have to look in front of IT's
2880 position and see whether there is a face != new_face_id. */
2881 it->start_of_box_run_p
2882 = (new_face->box != FACE_NO_BOX
2883 && (it->face_id >= 0
2884 || IT_CHARPOS (*it) == BEG
2885 || new_face_id != face_before_it_pos (it)));
2886 it->face_box_p = new_face->box != FACE_NO_BOX;
2889 else
2891 int base_face_id, bufpos;
2893 if (it->current.overlay_string_index >= 0)
2894 bufpos = IT_CHARPOS (*it);
2895 else
2896 bufpos = 0;
2898 /* For strings from a buffer, i.e. overlay strings or strings
2899 from a `display' property, use the face at IT's current
2900 buffer position as the base face to merge with, so that
2901 overlay strings appear in the same face as surrounding
2902 text, unless they specify their own faces. */
2903 base_face_id = underlying_face_id (it);
2905 new_face_id = face_at_string_position (it->w,
2906 it->string,
2907 IT_STRING_CHARPOS (*it),
2908 bufpos,
2909 it->region_beg_charpos,
2910 it->region_end_charpos,
2911 &next_stop,
2912 base_face_id, 0);
2914 #if 0 /* This shouldn't be neccessary. Let's check it. */
2915 /* If IT is used to display a mode line we would really like to
2916 use the mode line face instead of the frame's default face. */
2917 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2918 && new_face_id == DEFAULT_FACE_ID)
2919 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2920 #endif
2922 /* Is this a start of a run of characters with box? Caveat:
2923 this can be called for a freshly allocated iterator; face_id
2924 is -1 is this case. We know that the new face will not
2925 change until the next check pos, i.e. if the new face has a
2926 box, all characters up to that position will have a
2927 box. But, as usual, we don't know whether that position
2928 is really the end. */
2929 if (new_face_id != it->face_id)
2931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2932 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2934 /* If new face has a box but old face hasn't, this is the
2935 start of a run of characters with box, i.e. it has a
2936 shadow on the left side. */
2937 it->start_of_box_run_p
2938 = new_face->box && (old_face == NULL || !old_face->box);
2939 it->face_box_p = new_face->box != FACE_NO_BOX;
2943 it->face_id = new_face_id;
2944 return HANDLED_NORMALLY;
2948 /* Return the ID of the face ``underlying'' IT's current position,
2949 which is in a string. If the iterator is associated with a
2950 buffer, return the face at IT's current buffer position.
2951 Otherwise, use the iterator's base_face_id. */
2953 static int
2954 underlying_face_id (it)
2955 struct it *it;
2957 int face_id = it->base_face_id, i;
2959 xassert (STRINGP (it->string));
2961 for (i = it->sp - 1; i >= 0; --i)
2962 if (NILP (it->stack[i].string))
2963 face_id = it->stack[i].face_id;
2965 return face_id;
2969 /* Compute the face one character before or after the current position
2970 of IT. BEFORE_P non-zero means get the face in front of IT's
2971 position. Value is the id of the face. */
2973 static int
2974 face_before_or_after_it_pos (it, before_p)
2975 struct it *it;
2976 int before_p;
2978 int face_id, limit;
2979 int next_check_charpos;
2980 struct text_pos pos;
2982 xassert (it->s == NULL);
2984 if (STRINGP (it->string))
2986 int bufpos, base_face_id;
2988 /* No face change past the end of the string (for the case
2989 we are padding with spaces). No face change before the
2990 string start. */
2991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2992 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2993 return it->face_id;
2995 /* Set pos to the position before or after IT's current position. */
2996 if (before_p)
2997 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2998 else
2999 /* For composition, we must check the character after the
3000 composition. */
3001 pos = (it->what == IT_COMPOSITION
3002 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3003 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3005 if (it->current.overlay_string_index >= 0)
3006 bufpos = IT_CHARPOS (*it);
3007 else
3008 bufpos = 0;
3010 base_face_id = underlying_face_id (it);
3012 /* Get the face for ASCII, or unibyte. */
3013 face_id = face_at_string_position (it->w,
3014 it->string,
3015 CHARPOS (pos),
3016 bufpos,
3017 it->region_beg_charpos,
3018 it->region_end_charpos,
3019 &next_check_charpos,
3020 base_face_id, 0);
3022 /* Correct the face for charsets different from ASCII. Do it
3023 for the multibyte case only. The face returned above is
3024 suitable for unibyte text if IT->string is unibyte. */
3025 if (STRING_MULTIBYTE (it->string))
3027 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3028 int rest = SBYTES (it->string) - BYTEPOS (pos);
3029 int c, len;
3030 struct face *face = FACE_FROM_ID (it->f, face_id);
3032 c = string_char_and_length (p, rest, &len);
3033 face_id = FACE_FOR_CHAR (it->f, face, c);
3036 else
3038 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3039 || (IT_CHARPOS (*it) <= BEGV && before_p))
3040 return it->face_id;
3042 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3043 pos = it->current.pos;
3045 if (before_p)
3046 DEC_TEXT_POS (pos, it->multibyte_p);
3047 else
3049 if (it->what == IT_COMPOSITION)
3050 /* For composition, we must check the position after the
3051 composition. */
3052 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3053 else
3054 INC_TEXT_POS (pos, it->multibyte_p);
3057 /* Determine face for CHARSET_ASCII, or unibyte. */
3058 face_id = face_at_buffer_position (it->w,
3059 CHARPOS (pos),
3060 it->region_beg_charpos,
3061 it->region_end_charpos,
3062 &next_check_charpos,
3063 limit, 0);
3065 /* Correct the face for charsets different from ASCII. Do it
3066 for the multibyte case only. The face returned above is
3067 suitable for unibyte text if current_buffer is unibyte. */
3068 if (it->multibyte_p)
3070 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3071 struct face *face = FACE_FROM_ID (it->f, face_id);
3072 face_id = FACE_FOR_CHAR (it->f, face, c);
3076 return face_id;
3081 /***********************************************************************
3082 Invisible text
3083 ***********************************************************************/
3085 /* Set up iterator IT from invisible properties at its current
3086 position. Called from handle_stop. */
3088 static enum prop_handled
3089 handle_invisible_prop (it)
3090 struct it *it;
3092 enum prop_handled handled = HANDLED_NORMALLY;
3094 if (STRINGP (it->string))
3096 extern Lisp_Object Qinvisible;
3097 Lisp_Object prop, end_charpos, limit, charpos;
3099 /* Get the value of the invisible text property at the
3100 current position. Value will be nil if there is no such
3101 property. */
3102 charpos = make_number (IT_STRING_CHARPOS (*it));
3103 prop = Fget_text_property (charpos, Qinvisible, it->string);
3105 if (!NILP (prop)
3106 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3108 handled = HANDLED_RECOMPUTE_PROPS;
3110 /* Get the position at which the next change of the
3111 invisible text property can be found in IT->string.
3112 Value will be nil if the property value is the same for
3113 all the rest of IT->string. */
3114 XSETINT (limit, SCHARS (it->string));
3115 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3116 it->string, limit);
3118 /* Text at current position is invisible. The next
3119 change in the property is at position end_charpos.
3120 Move IT's current position to that position. */
3121 if (INTEGERP (end_charpos)
3122 && XFASTINT (end_charpos) < XFASTINT (limit))
3124 struct text_pos old;
3125 old = it->current.string_pos;
3126 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3127 compute_string_pos (&it->current.string_pos, old, it->string);
3129 else
3131 /* The rest of the string is invisible. If this is an
3132 overlay string, proceed with the next overlay string
3133 or whatever comes and return a character from there. */
3134 if (it->current.overlay_string_index >= 0)
3136 next_overlay_string (it);
3137 /* Don't check for overlay strings when we just
3138 finished processing them. */
3139 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3141 else
3143 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3144 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3149 else
3151 int invis_p, newpos, next_stop, start_charpos;
3152 Lisp_Object pos, prop, overlay;
3154 /* First of all, is there invisible text at this position? */
3155 start_charpos = IT_CHARPOS (*it);
3156 pos = make_number (IT_CHARPOS (*it));
3157 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3158 &overlay);
3159 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3161 /* If we are on invisible text, skip over it. */
3162 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3164 /* Record whether we have to display an ellipsis for the
3165 invisible text. */
3166 int display_ellipsis_p = invis_p == 2;
3168 handled = HANDLED_RECOMPUTE_PROPS;
3170 /* Loop skipping over invisible text. The loop is left at
3171 ZV or with IT on the first char being visible again. */
3174 /* Try to skip some invisible text. Return value is the
3175 position reached which can be equal to IT's position
3176 if there is nothing invisible here. This skips both
3177 over invisible text properties and overlays with
3178 invisible property. */
3179 newpos = skip_invisible (IT_CHARPOS (*it),
3180 &next_stop, ZV, it->window);
3182 /* If we skipped nothing at all we weren't at invisible
3183 text in the first place. If everything to the end of
3184 the buffer was skipped, end the loop. */
3185 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3186 invis_p = 0;
3187 else
3189 /* We skipped some characters but not necessarily
3190 all there are. Check if we ended up on visible
3191 text. Fget_char_property returns the property of
3192 the char before the given position, i.e. if we
3193 get invis_p = 0, this means that the char at
3194 newpos is visible. */
3195 pos = make_number (newpos);
3196 prop = Fget_char_property (pos, Qinvisible, it->window);
3197 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3200 /* If we ended up on invisible text, proceed to
3201 skip starting with next_stop. */
3202 if (invis_p)
3203 IT_CHARPOS (*it) = next_stop;
3205 while (invis_p);
3207 /* The position newpos is now either ZV or on visible text. */
3208 IT_CHARPOS (*it) = newpos;
3209 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3211 /* If there are before-strings at the start of invisible
3212 text, and the text is invisible because of a text
3213 property, arrange to show before-strings because 20.x did
3214 it that way. (If the text is invisible because of an
3215 overlay property instead of a text property, this is
3216 already handled in the overlay code.) */
3217 if (NILP (overlay)
3218 && get_overlay_strings (it, start_charpos))
3220 handled = HANDLED_RECOMPUTE_PROPS;
3221 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3223 else if (display_ellipsis_p)
3224 setup_for_ellipsis (it);
3228 return handled;
3232 /* Make iterator IT return `...' next. */
3234 static void
3235 setup_for_ellipsis (it)
3236 struct it *it;
3238 if (it->dp
3239 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3241 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3242 it->dpvec = v->contents;
3243 it->dpend = v->contents + v->size;
3245 else
3247 /* Default `...'. */
3248 it->dpvec = default_invis_vector;
3249 it->dpend = default_invis_vector + 3;
3252 /* The ellipsis display does not replace the display of the
3253 character at the new position. Indicate this by setting
3254 IT->dpvec_char_len to zero. */
3255 it->dpvec_char_len = 0;
3257 it->current.dpvec_index = 0;
3258 it->method = next_element_from_display_vector;
3263 /***********************************************************************
3264 'display' property
3265 ***********************************************************************/
3267 /* Set up iterator IT from `display' property at its current position.
3268 Called from handle_stop. */
3270 static enum prop_handled
3271 handle_display_prop (it)
3272 struct it *it;
3274 Lisp_Object prop, object;
3275 struct text_pos *position;
3276 int display_replaced_p = 0;
3278 if (STRINGP (it->string))
3280 object = it->string;
3281 position = &it->current.string_pos;
3283 else
3285 object = it->w->buffer;
3286 position = &it->current.pos;
3289 /* Reset those iterator values set from display property values. */
3290 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3291 it->space_width = Qnil;
3292 it->font_height = Qnil;
3293 it->voffset = 0;
3295 /* We don't support recursive `display' properties, i.e. string
3296 values that have a string `display' property, that have a string
3297 `display' property etc. */
3298 if (!it->string_from_display_prop_p)
3299 it->area = TEXT_AREA;
3301 prop = Fget_char_property (make_number (position->charpos),
3302 Qdisplay, object);
3303 if (NILP (prop))
3304 return HANDLED_NORMALLY;
3306 if (CONSP (prop)
3307 /* Simple properties. */
3308 && !EQ (XCAR (prop), Qimage)
3309 && !EQ (XCAR (prop), Qspace)
3310 && !EQ (XCAR (prop), Qwhen)
3311 && !EQ (XCAR (prop), Qslice)
3312 && !EQ (XCAR (prop), Qspace_width)
3313 && !EQ (XCAR (prop), Qheight)
3314 && !EQ (XCAR (prop), Qraise)
3315 /* Marginal area specifications. */
3316 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3317 && !EQ (XCAR (prop), Qleft_fringe)
3318 && !EQ (XCAR (prop), Qright_fringe)
3319 && !NILP (XCAR (prop)))
3321 for (; CONSP (prop); prop = XCDR (prop))
3323 if (handle_single_display_prop (it, XCAR (prop), object,
3324 position, display_replaced_p))
3325 display_replaced_p = 1;
3328 else if (VECTORP (prop))
3330 int i;
3331 for (i = 0; i < ASIZE (prop); ++i)
3332 if (handle_single_display_prop (it, AREF (prop, i), object,
3333 position, display_replaced_p))
3334 display_replaced_p = 1;
3336 else
3338 if (handle_single_display_prop (it, prop, object, position, 0))
3339 display_replaced_p = 1;
3342 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3346 /* Value is the position of the end of the `display' property starting
3347 at START_POS in OBJECT. */
3349 static struct text_pos
3350 display_prop_end (it, object, start_pos)
3351 struct it *it;
3352 Lisp_Object object;
3353 struct text_pos start_pos;
3355 Lisp_Object end;
3356 struct text_pos end_pos;
3358 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3359 Qdisplay, object, Qnil);
3360 CHARPOS (end_pos) = XFASTINT (end);
3361 if (STRINGP (object))
3362 compute_string_pos (&end_pos, start_pos, it->string);
3363 else
3364 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3366 return end_pos;
3370 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3371 is the object in which the `display' property was found. *POSITION
3372 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3373 means that we previously saw a display sub-property which already
3374 replaced text display with something else, for example an image;
3375 ignore such properties after the first one has been processed.
3377 If PROP is a `space' or `image' sub-property, set *POSITION to the
3378 end position of the `display' property.
3380 Value is non-zero if something was found which replaces the display
3381 of buffer or string text. */
3383 static int
3384 handle_single_display_prop (it, prop, object, position,
3385 display_replaced_before_p)
3386 struct it *it;
3387 Lisp_Object prop;
3388 Lisp_Object object;
3389 struct text_pos *position;
3390 int display_replaced_before_p;
3392 Lisp_Object value;
3393 int replaces_text_display_p = 0;
3394 Lisp_Object form;
3396 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3397 evaluated. If the result is nil, VALUE is ignored. */
3398 form = Qt;
3399 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3401 prop = XCDR (prop);
3402 if (!CONSP (prop))
3403 return 0;
3404 form = XCAR (prop);
3405 prop = XCDR (prop);
3408 if (!NILP (form) && !EQ (form, Qt))
3410 int count = SPECPDL_INDEX ();
3411 struct gcpro gcpro1;
3413 /* Bind `object' to the object having the `display' property, a
3414 buffer or string. Bind `position' to the position in the
3415 object where the property was found, and `buffer-position'
3416 to the current position in the buffer. */
3417 specbind (Qobject, object);
3418 specbind (Qposition, make_number (CHARPOS (*position)));
3419 specbind (Qbuffer_position,
3420 make_number (STRINGP (object)
3421 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3422 GCPRO1 (form);
3423 form = safe_eval (form);
3424 UNGCPRO;
3425 unbind_to (count, Qnil);
3428 if (NILP (form))
3429 return 0;
3431 if (CONSP (prop)
3432 && EQ (XCAR (prop), Qheight)
3433 && CONSP (XCDR (prop)))
3435 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3436 return 0;
3438 /* `(height HEIGHT)'. */
3439 it->font_height = XCAR (XCDR (prop));
3440 if (!NILP (it->font_height))
3442 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3443 int new_height = -1;
3445 if (CONSP (it->font_height)
3446 && (EQ (XCAR (it->font_height), Qplus)
3447 || EQ (XCAR (it->font_height), Qminus))
3448 && CONSP (XCDR (it->font_height))
3449 && INTEGERP (XCAR (XCDR (it->font_height))))
3451 /* `(+ N)' or `(- N)' where N is an integer. */
3452 int steps = XINT (XCAR (XCDR (it->font_height)));
3453 if (EQ (XCAR (it->font_height), Qplus))
3454 steps = - steps;
3455 it->face_id = smaller_face (it->f, it->face_id, steps);
3457 else if (FUNCTIONP (it->font_height))
3459 /* Call function with current height as argument.
3460 Value is the new height. */
3461 Lisp_Object height;
3462 height = safe_call1 (it->font_height,
3463 face->lface[LFACE_HEIGHT_INDEX]);
3464 if (NUMBERP (height))
3465 new_height = XFLOATINT (height);
3467 else if (NUMBERP (it->font_height))
3469 /* Value is a multiple of the canonical char height. */
3470 struct face *face;
3472 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3473 new_height = (XFLOATINT (it->font_height)
3474 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3476 else
3478 /* Evaluate IT->font_height with `height' bound to the
3479 current specified height to get the new height. */
3480 Lisp_Object value;
3481 int count = SPECPDL_INDEX ();
3483 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3484 value = safe_eval (it->font_height);
3485 unbind_to (count, Qnil);
3487 if (NUMBERP (value))
3488 new_height = XFLOATINT (value);
3491 if (new_height > 0)
3492 it->face_id = face_with_height (it->f, it->face_id, new_height);
3495 else if (CONSP (prop)
3496 && EQ (XCAR (prop), Qspace_width)
3497 && CONSP (XCDR (prop)))
3499 /* `(space_width WIDTH)'. */
3500 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3501 return 0;
3503 value = XCAR (XCDR (prop));
3504 if (NUMBERP (value) && XFLOATINT (value) > 0)
3505 it->space_width = value;
3507 else if (CONSP (prop)
3508 && EQ (XCAR (prop), Qslice))
3510 /* `(slice X Y WIDTH HEIGHT)'. */
3511 Lisp_Object tem;
3513 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3514 return 0;
3516 if (tem = XCDR (prop), CONSP (tem))
3518 it->slice.x = XCAR (tem);
3519 if (tem = XCDR (tem), CONSP (tem))
3521 it->slice.y = XCAR (tem);
3522 if (tem = XCDR (tem), CONSP (tem))
3524 it->slice.width = XCAR (tem);
3525 if (tem = XCDR (tem), CONSP (tem))
3526 it->slice.height = XCAR (tem);
3531 else if (CONSP (prop)
3532 && EQ (XCAR (prop), Qraise)
3533 && CONSP (XCDR (prop)))
3535 /* `(raise FACTOR)'. */
3536 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3537 return 0;
3539 #ifdef HAVE_WINDOW_SYSTEM
3540 value = XCAR (XCDR (prop));
3541 if (NUMBERP (value))
3543 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3544 it->voffset = - (XFLOATINT (value)
3545 * (FONT_HEIGHT (face->font)));
3547 #endif /* HAVE_WINDOW_SYSTEM */
3549 else if (!it->string_from_display_prop_p)
3551 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3552 VALUE) or `((margin nil) VALUE)' or VALUE. */
3553 Lisp_Object location, value;
3554 struct text_pos start_pos;
3555 int valid_p;
3557 /* Characters having this form of property are not displayed, so
3558 we have to find the end of the property. */
3559 start_pos = *position;
3560 *position = display_prop_end (it, object, start_pos);
3561 value = Qnil;
3563 /* Let's stop at the new position and assume that all
3564 text properties change there. */
3565 it->stop_charpos = position->charpos;
3567 if (CONSP (prop)
3568 && (EQ (XCAR (prop), Qleft_fringe)
3569 || EQ (XCAR (prop), Qright_fringe))
3570 && CONSP (XCDR (prop)))
3572 unsigned face_id = DEFAULT_FACE_ID;
3573 int fringe_bitmap;
3575 /* Save current settings of IT so that we can restore them
3576 when we are finished with the glyph property value. */
3578 /* `(left-fringe BITMAP FACE)'. */
3579 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3580 return 0;
3582 #ifdef HAVE_WINDOW_SYSTEM
3583 value = XCAR (XCDR (prop));
3584 if (!SYMBOLP (value)
3585 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3586 return 0;
3588 if (CONSP (XCDR (XCDR (prop))))
3590 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3592 face_id = lookup_named_face (it->f, face_name, 'A');
3593 if (face_id < 0)
3594 return 0;
3597 push_it (it);
3599 it->area = TEXT_AREA;
3600 it->what = IT_IMAGE;
3601 it->image_id = -1; /* no image */
3602 it->position = start_pos;
3603 it->object = NILP (object) ? it->w->buffer : object;
3604 it->method = next_element_from_image;
3605 it->face_id = face_id;
3607 /* Say that we haven't consumed the characters with
3608 `display' property yet. The call to pop_it in
3609 set_iterator_to_next will clean this up. */
3610 *position = start_pos;
3612 if (EQ (XCAR (prop), Qleft_fringe))
3614 it->left_user_fringe_bitmap = fringe_bitmap;
3615 it->left_user_fringe_face_id = face_id;
3617 else
3619 it->right_user_fringe_bitmap = fringe_bitmap;
3620 it->right_user_fringe_face_id = face_id;
3622 #endif /* HAVE_WINDOW_SYSTEM */
3623 return 1;
3626 location = Qunbound;
3627 if (CONSP (prop) && CONSP (XCAR (prop)))
3629 Lisp_Object tem;
3631 value = XCDR (prop);
3632 if (CONSP (value))
3633 value = XCAR (value);
3635 tem = XCAR (prop);
3636 if (EQ (XCAR (tem), Qmargin)
3637 && (tem = XCDR (tem),
3638 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3639 (NILP (tem)
3640 || EQ (tem, Qleft_margin)
3641 || EQ (tem, Qright_margin))))
3642 location = tem;
3645 if (EQ (location, Qunbound))
3647 location = Qnil;
3648 value = prop;
3651 valid_p = (STRINGP (value)
3652 #ifdef HAVE_WINDOW_SYSTEM
3653 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3654 #endif /* not HAVE_WINDOW_SYSTEM */
3655 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3657 if ((EQ (location, Qleft_margin)
3658 || EQ (location, Qright_margin)
3659 || NILP (location))
3660 && valid_p
3661 && !display_replaced_before_p)
3663 replaces_text_display_p = 1;
3665 /* Save current settings of IT so that we can restore them
3666 when we are finished with the glyph property value. */
3667 push_it (it);
3669 if (NILP (location))
3670 it->area = TEXT_AREA;
3671 else if (EQ (location, Qleft_margin))
3672 it->area = LEFT_MARGIN_AREA;
3673 else
3674 it->area = RIGHT_MARGIN_AREA;
3676 if (STRINGP (value))
3678 it->string = value;
3679 it->multibyte_p = STRING_MULTIBYTE (it->string);
3680 it->current.overlay_string_index = -1;
3681 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3682 it->end_charpos = it->string_nchars = SCHARS (it->string);
3683 it->method = next_element_from_string;
3684 it->stop_charpos = 0;
3685 it->string_from_display_prop_p = 1;
3686 /* Say that we haven't consumed the characters with
3687 `display' property yet. The call to pop_it in
3688 set_iterator_to_next will clean this up. */
3689 *position = start_pos;
3691 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3693 it->method = next_element_from_stretch;
3694 it->object = value;
3695 it->current.pos = it->position = start_pos;
3697 #ifdef HAVE_WINDOW_SYSTEM
3698 else
3700 it->what = IT_IMAGE;
3701 it->image_id = lookup_image (it->f, value);
3702 it->position = start_pos;
3703 it->object = NILP (object) ? it->w->buffer : object;
3704 it->method = next_element_from_image;
3706 /* Say that we haven't consumed the characters with
3707 `display' property yet. The call to pop_it in
3708 set_iterator_to_next will clean this up. */
3709 *position = start_pos;
3711 #endif /* HAVE_WINDOW_SYSTEM */
3713 else
3714 /* Invalid property or property not supported. Restore
3715 the position to what it was before. */
3716 *position = start_pos;
3719 return replaces_text_display_p;
3723 /* Check if PROP is a display sub-property value whose text should be
3724 treated as intangible. */
3726 static int
3727 single_display_prop_intangible_p (prop)
3728 Lisp_Object prop;
3730 /* Skip over `when FORM'. */
3731 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3733 prop = XCDR (prop);
3734 if (!CONSP (prop))
3735 return 0;
3736 prop = XCDR (prop);
3739 if (STRINGP (prop))
3740 return 1;
3742 if (!CONSP (prop))
3743 return 0;
3745 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3746 we don't need to treat text as intangible. */
3747 if (EQ (XCAR (prop), Qmargin))
3749 prop = XCDR (prop);
3750 if (!CONSP (prop))
3751 return 0;
3753 prop = XCDR (prop);
3754 if (!CONSP (prop)
3755 || EQ (XCAR (prop), Qleft_margin)
3756 || EQ (XCAR (prop), Qright_margin))
3757 return 0;
3760 return (CONSP (prop)
3761 && (EQ (XCAR (prop), Qimage)
3762 || EQ (XCAR (prop), Qspace)));
3766 /* Check if PROP is a display property value whose text should be
3767 treated as intangible. */
3770 display_prop_intangible_p (prop)
3771 Lisp_Object prop;
3773 if (CONSP (prop)
3774 && CONSP (XCAR (prop))
3775 && !EQ (Qmargin, XCAR (XCAR (prop))))
3777 /* A list of sub-properties. */
3778 while (CONSP (prop))
3780 if (single_display_prop_intangible_p (XCAR (prop)))
3781 return 1;
3782 prop = XCDR (prop);
3785 else if (VECTORP (prop))
3787 /* A vector of sub-properties. */
3788 int i;
3789 for (i = 0; i < ASIZE (prop); ++i)
3790 if (single_display_prop_intangible_p (AREF (prop, i)))
3791 return 1;
3793 else
3794 return single_display_prop_intangible_p (prop);
3796 return 0;
3800 /* Return 1 if PROP is a display sub-property value containing STRING. */
3802 static int
3803 single_display_prop_string_p (prop, string)
3804 Lisp_Object prop, string;
3806 if (EQ (string, prop))
3807 return 1;
3809 /* Skip over `when FORM'. */
3810 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3812 prop = XCDR (prop);
3813 if (!CONSP (prop))
3814 return 0;
3815 prop = XCDR (prop);
3818 if (CONSP (prop))
3819 /* Skip over `margin LOCATION'. */
3820 if (EQ (XCAR (prop), Qmargin))
3822 prop = XCDR (prop);
3823 if (!CONSP (prop))
3824 return 0;
3826 prop = XCDR (prop);
3827 if (!CONSP (prop))
3828 return 0;
3831 return CONSP (prop) && EQ (XCAR (prop), string);
3835 /* Return 1 if STRING appears in the `display' property PROP. */
3837 static int
3838 display_prop_string_p (prop, string)
3839 Lisp_Object prop, string;
3841 if (CONSP (prop)
3842 && CONSP (XCAR (prop))
3843 && !EQ (Qmargin, XCAR (XCAR (prop))))
3845 /* A list of sub-properties. */
3846 while (CONSP (prop))
3848 if (single_display_prop_string_p (XCAR (prop), string))
3849 return 1;
3850 prop = XCDR (prop);
3853 else if (VECTORP (prop))
3855 /* A vector of sub-properties. */
3856 int i;
3857 for (i = 0; i < ASIZE (prop); ++i)
3858 if (single_display_prop_string_p (AREF (prop, i), string))
3859 return 1;
3861 else
3862 return single_display_prop_string_p (prop, string);
3864 return 0;
3868 /* Determine from which buffer position in W's buffer STRING comes
3869 from. AROUND_CHARPOS is an approximate position where it could
3870 be from. Value is the buffer position or 0 if it couldn't be
3871 determined.
3873 W's buffer must be current.
3875 This function is necessary because we don't record buffer positions
3876 in glyphs generated from strings (to keep struct glyph small).
3877 This function may only use code that doesn't eval because it is
3878 called asynchronously from note_mouse_highlight. */
3881 string_buffer_position (w, string, around_charpos)
3882 struct window *w;
3883 Lisp_Object string;
3884 int around_charpos;
3886 Lisp_Object limit, prop, pos;
3887 const int MAX_DISTANCE = 1000;
3888 int found = 0;
3890 pos = make_number (around_charpos);
3891 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3892 while (!found && !EQ (pos, limit))
3894 prop = Fget_char_property (pos, Qdisplay, Qnil);
3895 if (!NILP (prop) && display_prop_string_p (prop, string))
3896 found = 1;
3897 else
3898 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3901 if (!found)
3903 pos = make_number (around_charpos);
3904 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3905 while (!found && !EQ (pos, limit))
3907 prop = Fget_char_property (pos, Qdisplay, Qnil);
3908 if (!NILP (prop) && display_prop_string_p (prop, string))
3909 found = 1;
3910 else
3911 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3912 limit);
3916 return found ? XINT (pos) : 0;
3921 /***********************************************************************
3922 `composition' property
3923 ***********************************************************************/
3925 /* Set up iterator IT from `composition' property at its current
3926 position. Called from handle_stop. */
3928 static enum prop_handled
3929 handle_composition_prop (it)
3930 struct it *it;
3932 Lisp_Object prop, string;
3933 int pos, pos_byte, end;
3934 enum prop_handled handled = HANDLED_NORMALLY;
3936 if (STRINGP (it->string))
3938 pos = IT_STRING_CHARPOS (*it);
3939 pos_byte = IT_STRING_BYTEPOS (*it);
3940 string = it->string;
3942 else
3944 pos = IT_CHARPOS (*it);
3945 pos_byte = IT_BYTEPOS (*it);
3946 string = Qnil;
3949 /* If there's a valid composition and point is not inside of the
3950 composition (in the case that the composition is from the current
3951 buffer), draw a glyph composed from the composition components. */
3952 if (find_composition (pos, -1, &pos, &end, &prop, string)
3953 && COMPOSITION_VALID_P (pos, end, prop)
3954 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3956 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3958 if (id >= 0)
3960 it->method = next_element_from_composition;
3961 it->cmp_id = id;
3962 it->cmp_len = COMPOSITION_LENGTH (prop);
3963 /* For a terminal, draw only the first character of the
3964 components. */
3965 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3966 it->len = (STRINGP (it->string)
3967 ? string_char_to_byte (it->string, end)
3968 : CHAR_TO_BYTE (end)) - pos_byte;
3969 it->stop_charpos = end;
3970 handled = HANDLED_RETURN;
3974 return handled;
3979 /***********************************************************************
3980 Overlay strings
3981 ***********************************************************************/
3983 /* The following structure is used to record overlay strings for
3984 later sorting in load_overlay_strings. */
3986 struct overlay_entry
3988 Lisp_Object overlay;
3989 Lisp_Object string;
3990 int priority;
3991 int after_string_p;
3995 /* Set up iterator IT from overlay strings at its current position.
3996 Called from handle_stop. */
3998 static enum prop_handled
3999 handle_overlay_change (it)
4000 struct it *it;
4002 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4003 return HANDLED_RECOMPUTE_PROPS;
4004 else
4005 return HANDLED_NORMALLY;
4009 /* Set up the next overlay string for delivery by IT, if there is an
4010 overlay string to deliver. Called by set_iterator_to_next when the
4011 end of the current overlay string is reached. If there are more
4012 overlay strings to display, IT->string and
4013 IT->current.overlay_string_index are set appropriately here.
4014 Otherwise IT->string is set to nil. */
4016 static void
4017 next_overlay_string (it)
4018 struct it *it;
4020 ++it->current.overlay_string_index;
4021 if (it->current.overlay_string_index == it->n_overlay_strings)
4023 /* No more overlay strings. Restore IT's settings to what
4024 they were before overlay strings were processed, and
4025 continue to deliver from current_buffer. */
4026 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4028 pop_it (it);
4029 xassert (it->stop_charpos >= BEGV
4030 && it->stop_charpos <= it->end_charpos);
4031 it->string = Qnil;
4032 it->current.overlay_string_index = -1;
4033 SET_TEXT_POS (it->current.string_pos, -1, -1);
4034 it->n_overlay_strings = 0;
4035 it->method = next_element_from_buffer;
4037 /* If we're at the end of the buffer, record that we have
4038 processed the overlay strings there already, so that
4039 next_element_from_buffer doesn't try it again. */
4040 if (IT_CHARPOS (*it) >= it->end_charpos)
4041 it->overlay_strings_at_end_processed_p = 1;
4043 /* If we have to display `...' for invisible text, set
4044 the iterator up for that. */
4045 if (display_ellipsis_p)
4046 setup_for_ellipsis (it);
4048 else
4050 /* There are more overlay strings to process. If
4051 IT->current.overlay_string_index has advanced to a position
4052 where we must load IT->overlay_strings with more strings, do
4053 it. */
4054 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4056 if (it->current.overlay_string_index && i == 0)
4057 load_overlay_strings (it, 0);
4059 /* Initialize IT to deliver display elements from the overlay
4060 string. */
4061 it->string = it->overlay_strings[i];
4062 it->multibyte_p = STRING_MULTIBYTE (it->string);
4063 SET_TEXT_POS (it->current.string_pos, 0, 0);
4064 it->method = next_element_from_string;
4065 it->stop_charpos = 0;
4068 CHECK_IT (it);
4072 /* Compare two overlay_entry structures E1 and E2. Used as a
4073 comparison function for qsort in load_overlay_strings. Overlay
4074 strings for the same position are sorted so that
4076 1. All after-strings come in front of before-strings, except
4077 when they come from the same overlay.
4079 2. Within after-strings, strings are sorted so that overlay strings
4080 from overlays with higher priorities come first.
4082 2. Within before-strings, strings are sorted so that overlay
4083 strings from overlays with higher priorities come last.
4085 Value is analogous to strcmp. */
4088 static int
4089 compare_overlay_entries (e1, e2)
4090 void *e1, *e2;
4092 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4093 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4094 int result;
4096 if (entry1->after_string_p != entry2->after_string_p)
4098 /* Let after-strings appear in front of before-strings if
4099 they come from different overlays. */
4100 if (EQ (entry1->overlay, entry2->overlay))
4101 result = entry1->after_string_p ? 1 : -1;
4102 else
4103 result = entry1->after_string_p ? -1 : 1;
4105 else if (entry1->after_string_p)
4106 /* After-strings sorted in order of decreasing priority. */
4107 result = entry2->priority - entry1->priority;
4108 else
4109 /* Before-strings sorted in order of increasing priority. */
4110 result = entry1->priority - entry2->priority;
4112 return result;
4116 /* Load the vector IT->overlay_strings with overlay strings from IT's
4117 current buffer position, or from CHARPOS if that is > 0. Set
4118 IT->n_overlays to the total number of overlay strings found.
4120 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4121 a time. On entry into load_overlay_strings,
4122 IT->current.overlay_string_index gives the number of overlay
4123 strings that have already been loaded by previous calls to this
4124 function.
4126 IT->add_overlay_start contains an additional overlay start
4127 position to consider for taking overlay strings from, if non-zero.
4128 This position comes into play when the overlay has an `invisible'
4129 property, and both before and after-strings. When we've skipped to
4130 the end of the overlay, because of its `invisible' property, we
4131 nevertheless want its before-string to appear.
4132 IT->add_overlay_start will contain the overlay start position
4133 in this case.
4135 Overlay strings are sorted so that after-string strings come in
4136 front of before-string strings. Within before and after-strings,
4137 strings are sorted by overlay priority. See also function
4138 compare_overlay_entries. */
4140 static void
4141 load_overlay_strings (it, charpos)
4142 struct it *it;
4143 int charpos;
4145 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4146 Lisp_Object overlay, window, str, invisible;
4147 struct Lisp_Overlay *ov;
4148 int start, end;
4149 int size = 20;
4150 int n = 0, i, j, invis_p;
4151 struct overlay_entry *entries
4152 = (struct overlay_entry *) alloca (size * sizeof *entries);
4154 if (charpos <= 0)
4155 charpos = IT_CHARPOS (*it);
4157 /* Append the overlay string STRING of overlay OVERLAY to vector
4158 `entries' which has size `size' and currently contains `n'
4159 elements. AFTER_P non-zero means STRING is an after-string of
4160 OVERLAY. */
4161 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4162 do \
4164 Lisp_Object priority; \
4166 if (n == size) \
4168 int new_size = 2 * size; \
4169 struct overlay_entry *old = entries; \
4170 entries = \
4171 (struct overlay_entry *) alloca (new_size \
4172 * sizeof *entries); \
4173 bcopy (old, entries, size * sizeof *entries); \
4174 size = new_size; \
4177 entries[n].string = (STRING); \
4178 entries[n].overlay = (OVERLAY); \
4179 priority = Foverlay_get ((OVERLAY), Qpriority); \
4180 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4181 entries[n].after_string_p = (AFTER_P); \
4182 ++n; \
4184 while (0)
4186 /* Process overlay before the overlay center. */
4187 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4189 XSETMISC (overlay, ov);
4190 xassert (OVERLAYP (overlay));
4191 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4192 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4194 if (end < charpos)
4195 break;
4197 /* Skip this overlay if it doesn't start or end at IT's current
4198 position. */
4199 if (end != charpos && start != charpos)
4200 continue;
4202 /* Skip this overlay if it doesn't apply to IT->w. */
4203 window = Foverlay_get (overlay, Qwindow);
4204 if (WINDOWP (window) && XWINDOW (window) != it->w)
4205 continue;
4207 /* If the text ``under'' the overlay is invisible, both before-
4208 and after-strings from this overlay are visible; start and
4209 end position are indistinguishable. */
4210 invisible = Foverlay_get (overlay, Qinvisible);
4211 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4213 /* If overlay has a non-empty before-string, record it. */
4214 if ((start == charpos || (end == charpos && invis_p))
4215 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4216 && SCHARS (str))
4217 RECORD_OVERLAY_STRING (overlay, str, 0);
4219 /* If overlay has a non-empty after-string, record it. */
4220 if ((end == charpos || (start == charpos && invis_p))
4221 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4222 && SCHARS (str))
4223 RECORD_OVERLAY_STRING (overlay, str, 1);
4226 /* Process overlays after the overlay center. */
4227 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4229 XSETMISC (overlay, ov);
4230 xassert (OVERLAYP (overlay));
4231 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4232 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4234 if (start > charpos)
4235 break;
4237 /* Skip this overlay if it doesn't start or end at IT's current
4238 position. */
4239 if (end != charpos && start != charpos)
4240 continue;
4242 /* Skip this overlay if it doesn't apply to IT->w. */
4243 window = Foverlay_get (overlay, Qwindow);
4244 if (WINDOWP (window) && XWINDOW (window) != it->w)
4245 continue;
4247 /* If the text ``under'' the overlay is invisible, it has a zero
4248 dimension, and both before- and after-strings apply. */
4249 invisible = Foverlay_get (overlay, Qinvisible);
4250 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4252 /* If overlay has a non-empty before-string, record it. */
4253 if ((start == charpos || (end == charpos && invis_p))
4254 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4255 && SCHARS (str))
4256 RECORD_OVERLAY_STRING (overlay, str, 0);
4258 /* If overlay has a non-empty after-string, record it. */
4259 if ((end == charpos || (start == charpos && invis_p))
4260 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4261 && SCHARS (str))
4262 RECORD_OVERLAY_STRING (overlay, str, 1);
4265 #undef RECORD_OVERLAY_STRING
4267 /* Sort entries. */
4268 if (n > 1)
4269 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4271 /* Record the total number of strings to process. */
4272 it->n_overlay_strings = n;
4274 /* IT->current.overlay_string_index is the number of overlay strings
4275 that have already been consumed by IT. Copy some of the
4276 remaining overlay strings to IT->overlay_strings. */
4277 i = 0;
4278 j = it->current.overlay_string_index;
4279 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4280 it->overlay_strings[i++] = entries[j++].string;
4282 CHECK_IT (it);
4286 /* Get the first chunk of overlay strings at IT's current buffer
4287 position, or at CHARPOS if that is > 0. Value is non-zero if at
4288 least one overlay string was found. */
4290 static int
4291 get_overlay_strings (it, charpos)
4292 struct it *it;
4293 int charpos;
4295 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4296 process. This fills IT->overlay_strings with strings, and sets
4297 IT->n_overlay_strings to the total number of strings to process.
4298 IT->pos.overlay_string_index has to be set temporarily to zero
4299 because load_overlay_strings needs this; it must be set to -1
4300 when no overlay strings are found because a zero value would
4301 indicate a position in the first overlay string. */
4302 it->current.overlay_string_index = 0;
4303 load_overlay_strings (it, charpos);
4305 /* If we found overlay strings, set up IT to deliver display
4306 elements from the first one. Otherwise set up IT to deliver
4307 from current_buffer. */
4308 if (it->n_overlay_strings)
4310 /* Make sure we know settings in current_buffer, so that we can
4311 restore meaningful values when we're done with the overlay
4312 strings. */
4313 compute_stop_pos (it);
4314 xassert (it->face_id >= 0);
4316 /* Save IT's settings. They are restored after all overlay
4317 strings have been processed. */
4318 xassert (it->sp == 0);
4319 push_it (it);
4321 /* Set up IT to deliver display elements from the first overlay
4322 string. */
4323 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4324 it->string = it->overlay_strings[0];
4325 it->stop_charpos = 0;
4326 xassert (STRINGP (it->string));
4327 it->end_charpos = SCHARS (it->string);
4328 it->multibyte_p = STRING_MULTIBYTE (it->string);
4329 it->method = next_element_from_string;
4331 else
4333 it->string = Qnil;
4334 it->current.overlay_string_index = -1;
4335 it->method = next_element_from_buffer;
4338 CHECK_IT (it);
4340 /* Value is non-zero if we found at least one overlay string. */
4341 return STRINGP (it->string);
4346 /***********************************************************************
4347 Saving and restoring state
4348 ***********************************************************************/
4350 /* Save current settings of IT on IT->stack. Called, for example,
4351 before setting up IT for an overlay string, to be able to restore
4352 IT's settings to what they were after the overlay string has been
4353 processed. */
4355 static void
4356 push_it (it)
4357 struct it *it;
4359 struct iterator_stack_entry *p;
4361 xassert (it->sp < 2);
4362 p = it->stack + it->sp;
4364 p->stop_charpos = it->stop_charpos;
4365 xassert (it->face_id >= 0);
4366 p->face_id = it->face_id;
4367 p->string = it->string;
4368 p->pos = it->current;
4369 p->end_charpos = it->end_charpos;
4370 p->string_nchars = it->string_nchars;
4371 p->area = it->area;
4372 p->multibyte_p = it->multibyte_p;
4373 p->slice = it->slice;
4374 p->space_width = it->space_width;
4375 p->font_height = it->font_height;
4376 p->voffset = it->voffset;
4377 p->string_from_display_prop_p = it->string_from_display_prop_p;
4378 p->display_ellipsis_p = 0;
4379 ++it->sp;
4383 /* Restore IT's settings from IT->stack. Called, for example, when no
4384 more overlay strings must be processed, and we return to delivering
4385 display elements from a buffer, or when the end of a string from a
4386 `display' property is reached and we return to delivering display
4387 elements from an overlay string, or from a buffer. */
4389 static void
4390 pop_it (it)
4391 struct it *it;
4393 struct iterator_stack_entry *p;
4395 xassert (it->sp > 0);
4396 --it->sp;
4397 p = it->stack + it->sp;
4398 it->stop_charpos = p->stop_charpos;
4399 it->face_id = p->face_id;
4400 it->string = p->string;
4401 it->current = p->pos;
4402 it->end_charpos = p->end_charpos;
4403 it->string_nchars = p->string_nchars;
4404 it->area = p->area;
4405 it->multibyte_p = p->multibyte_p;
4406 it->slice = p->slice;
4407 it->space_width = p->space_width;
4408 it->font_height = p->font_height;
4409 it->voffset = p->voffset;
4410 it->string_from_display_prop_p = p->string_from_display_prop_p;
4415 /***********************************************************************
4416 Moving over lines
4417 ***********************************************************************/
4419 /* Set IT's current position to the previous line start. */
4421 static void
4422 back_to_previous_line_start (it)
4423 struct it *it;
4425 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4426 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4430 /* Move IT to the next line start.
4432 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4433 we skipped over part of the text (as opposed to moving the iterator
4434 continuously over the text). Otherwise, don't change the value
4435 of *SKIPPED_P.
4437 Newlines may come from buffer text, overlay strings, or strings
4438 displayed via the `display' property. That's the reason we can't
4439 simply use find_next_newline_no_quit.
4441 Note that this function may not skip over invisible text that is so
4442 because of text properties and immediately follows a newline. If
4443 it would, function reseat_at_next_visible_line_start, when called
4444 from set_iterator_to_next, would effectively make invisible
4445 characters following a newline part of the wrong glyph row, which
4446 leads to wrong cursor motion. */
4448 static int
4449 forward_to_next_line_start (it, skipped_p)
4450 struct it *it;
4451 int *skipped_p;
4453 int old_selective, newline_found_p, n;
4454 const int MAX_NEWLINE_DISTANCE = 500;
4456 /* If already on a newline, just consume it to avoid unintended
4457 skipping over invisible text below. */
4458 if (it->what == IT_CHARACTER
4459 && it->c == '\n'
4460 && CHARPOS (it->position) == IT_CHARPOS (*it))
4462 set_iterator_to_next (it, 0);
4463 it->c = 0;
4464 return 1;
4467 /* Don't handle selective display in the following. It's (a)
4468 unnecessary because it's done by the caller, and (b) leads to an
4469 infinite recursion because next_element_from_ellipsis indirectly
4470 calls this function. */
4471 old_selective = it->selective;
4472 it->selective = 0;
4474 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4475 from buffer text. */
4476 for (n = newline_found_p = 0;
4477 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4478 n += STRINGP (it->string) ? 0 : 1)
4480 if (!get_next_display_element (it))
4481 return 0;
4482 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4483 set_iterator_to_next (it, 0);
4486 /* If we didn't find a newline near enough, see if we can use a
4487 short-cut. */
4488 if (!newline_found_p)
4490 int start = IT_CHARPOS (*it);
4491 int limit = find_next_newline_no_quit (start, 1);
4492 Lisp_Object pos;
4494 xassert (!STRINGP (it->string));
4496 /* If there isn't any `display' property in sight, and no
4497 overlays, we can just use the position of the newline in
4498 buffer text. */
4499 if (it->stop_charpos >= limit
4500 || ((pos = Fnext_single_property_change (make_number (start),
4501 Qdisplay,
4502 Qnil, make_number (limit)),
4503 NILP (pos))
4504 && next_overlay_change (start) == ZV))
4506 IT_CHARPOS (*it) = limit;
4507 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4508 *skipped_p = newline_found_p = 1;
4510 else
4512 while (get_next_display_element (it)
4513 && !newline_found_p)
4515 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4516 set_iterator_to_next (it, 0);
4521 it->selective = old_selective;
4522 return newline_found_p;
4526 /* Set IT's current position to the previous visible line start. Skip
4527 invisible text that is so either due to text properties or due to
4528 selective display. Caution: this does not change IT->current_x and
4529 IT->hpos. */
4531 static void
4532 back_to_previous_visible_line_start (it)
4533 struct it *it;
4535 int visible_p = 0;
4537 /* Go back one newline if not on BEGV already. */
4538 if (IT_CHARPOS (*it) > BEGV)
4539 back_to_previous_line_start (it);
4541 /* Move over lines that are invisible because of selective display
4542 or text properties. */
4543 while (IT_CHARPOS (*it) > BEGV
4544 && !visible_p)
4546 visible_p = 1;
4548 /* If selective > 0, then lines indented more than that values
4549 are invisible. */
4550 if (it->selective > 0
4551 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4552 (double) it->selective)) /* iftc */
4553 visible_p = 0;
4554 else
4556 Lisp_Object prop;
4558 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4559 Qinvisible, it->window);
4560 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4561 visible_p = 0;
4564 if (visible_p)
4566 struct it it2 = *it;
4568 if (handle_display_prop (&it2) == HANDLED_RETURN)
4569 visible_p = 0;
4572 /* Back one more newline if the current one is invisible. */
4573 if (!visible_p)
4574 back_to_previous_line_start (it);
4577 xassert (IT_CHARPOS (*it) >= BEGV);
4578 xassert (IT_CHARPOS (*it) == BEGV
4579 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4580 CHECK_IT (it);
4584 /* Reseat iterator IT at the previous visible line start. Skip
4585 invisible text that is so either due to text properties or due to
4586 selective display. At the end, update IT's overlay information,
4587 face information etc. */
4589 static void
4590 reseat_at_previous_visible_line_start (it)
4591 struct it *it;
4593 back_to_previous_visible_line_start (it);
4594 reseat (it, it->current.pos, 1);
4595 CHECK_IT (it);
4599 /* Reseat iterator IT on the next visible line start in the current
4600 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4601 preceding the line start. Skip over invisible text that is so
4602 because of selective display. Compute faces, overlays etc at the
4603 new position. Note that this function does not skip over text that
4604 is invisible because of text properties. */
4606 static void
4607 reseat_at_next_visible_line_start (it, on_newline_p)
4608 struct it *it;
4609 int on_newline_p;
4611 int newline_found_p, skipped_p = 0;
4613 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4615 /* Skip over lines that are invisible because they are indented
4616 more than the value of IT->selective. */
4617 if (it->selective > 0)
4618 while (IT_CHARPOS (*it) < ZV
4619 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4620 (double) it->selective)) /* iftc */
4622 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4623 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4626 /* Position on the newline if that's what's requested. */
4627 if (on_newline_p && newline_found_p)
4629 if (STRINGP (it->string))
4631 if (IT_STRING_CHARPOS (*it) > 0)
4633 --IT_STRING_CHARPOS (*it);
4634 --IT_STRING_BYTEPOS (*it);
4637 else if (IT_CHARPOS (*it) > BEGV)
4639 --IT_CHARPOS (*it);
4640 --IT_BYTEPOS (*it);
4641 reseat (it, it->current.pos, 0);
4644 else if (skipped_p)
4645 reseat (it, it->current.pos, 0);
4647 CHECK_IT (it);
4652 /***********************************************************************
4653 Changing an iterator's position
4654 ***********************************************************************/
4656 /* Change IT's current position to POS in current_buffer. If FORCE_P
4657 is non-zero, always check for text properties at the new position.
4658 Otherwise, text properties are only looked up if POS >=
4659 IT->check_charpos of a property. */
4661 static void
4662 reseat (it, pos, force_p)
4663 struct it *it;
4664 struct text_pos pos;
4665 int force_p;
4667 int original_pos = IT_CHARPOS (*it);
4669 reseat_1 (it, pos, 0);
4671 /* Determine where to check text properties. Avoid doing it
4672 where possible because text property lookup is very expensive. */
4673 if (force_p
4674 || CHARPOS (pos) > it->stop_charpos
4675 || CHARPOS (pos) < original_pos)
4676 handle_stop (it);
4678 CHECK_IT (it);
4682 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4683 IT->stop_pos to POS, also. */
4685 static void
4686 reseat_1 (it, pos, set_stop_p)
4687 struct it *it;
4688 struct text_pos pos;
4689 int set_stop_p;
4691 /* Don't call this function when scanning a C string. */
4692 xassert (it->s == NULL);
4694 /* POS must be a reasonable value. */
4695 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4697 it->current.pos = it->position = pos;
4698 XSETBUFFER (it->object, current_buffer);
4699 it->end_charpos = ZV;
4700 it->dpvec = NULL;
4701 it->current.dpvec_index = -1;
4702 it->current.overlay_string_index = -1;
4703 IT_STRING_CHARPOS (*it) = -1;
4704 IT_STRING_BYTEPOS (*it) = -1;
4705 it->string = Qnil;
4706 it->method = next_element_from_buffer;
4707 /* RMS: I added this to fix a bug in move_it_vertically_backward
4708 where it->area continued to relate to the starting point
4709 for the backward motion. Bug report from
4710 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4711 However, I am not sure whether reseat still does the right thing
4712 in general after this change. */
4713 it->area = TEXT_AREA;
4714 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4715 it->sp = 0;
4716 it->face_before_selective_p = 0;
4718 if (set_stop_p)
4719 it->stop_charpos = CHARPOS (pos);
4723 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4724 If S is non-null, it is a C string to iterate over. Otherwise,
4725 STRING gives a Lisp string to iterate over.
4727 If PRECISION > 0, don't return more then PRECISION number of
4728 characters from the string.
4730 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4731 characters have been returned. FIELD_WIDTH < 0 means an infinite
4732 field width.
4734 MULTIBYTE = 0 means disable processing of multibyte characters,
4735 MULTIBYTE > 0 means enable it,
4736 MULTIBYTE < 0 means use IT->multibyte_p.
4738 IT must be initialized via a prior call to init_iterator before
4739 calling this function. */
4741 static void
4742 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4743 struct it *it;
4744 unsigned char *s;
4745 Lisp_Object string;
4746 int charpos;
4747 int precision, field_width, multibyte;
4749 /* No region in strings. */
4750 it->region_beg_charpos = it->region_end_charpos = -1;
4752 /* No text property checks performed by default, but see below. */
4753 it->stop_charpos = -1;
4755 /* Set iterator position and end position. */
4756 bzero (&it->current, sizeof it->current);
4757 it->current.overlay_string_index = -1;
4758 it->current.dpvec_index = -1;
4759 xassert (charpos >= 0);
4761 /* If STRING is specified, use its multibyteness, otherwise use the
4762 setting of MULTIBYTE, if specified. */
4763 if (multibyte >= 0)
4764 it->multibyte_p = multibyte > 0;
4766 if (s == NULL)
4768 xassert (STRINGP (string));
4769 it->string = string;
4770 it->s = NULL;
4771 it->end_charpos = it->string_nchars = SCHARS (string);
4772 it->method = next_element_from_string;
4773 it->current.string_pos = string_pos (charpos, string);
4775 else
4777 it->s = s;
4778 it->string = Qnil;
4780 /* Note that we use IT->current.pos, not it->current.string_pos,
4781 for displaying C strings. */
4782 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4783 if (it->multibyte_p)
4785 it->current.pos = c_string_pos (charpos, s, 1);
4786 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4788 else
4790 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4791 it->end_charpos = it->string_nchars = strlen (s);
4794 it->method = next_element_from_c_string;
4797 /* PRECISION > 0 means don't return more than PRECISION characters
4798 from the string. */
4799 if (precision > 0 && it->end_charpos - charpos > precision)
4800 it->end_charpos = it->string_nchars = charpos + precision;
4802 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4803 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4804 FIELD_WIDTH < 0 means infinite field width. This is useful for
4805 padding with `-' at the end of a mode line. */
4806 if (field_width < 0)
4807 field_width = INFINITY;
4808 if (field_width > it->end_charpos - charpos)
4809 it->end_charpos = charpos + field_width;
4811 /* Use the standard display table for displaying strings. */
4812 if (DISP_TABLE_P (Vstandard_display_table))
4813 it->dp = XCHAR_TABLE (Vstandard_display_table);
4815 it->stop_charpos = charpos;
4816 CHECK_IT (it);
4821 /***********************************************************************
4822 Iteration
4823 ***********************************************************************/
4825 /* Load IT's display element fields with information about the next
4826 display element from the current position of IT. Value is zero if
4827 end of buffer (or C string) is reached. */
4830 get_next_display_element (it)
4831 struct it *it;
4833 /* Non-zero means that we found a display element. Zero means that
4834 we hit the end of what we iterate over. Performance note: the
4835 function pointer `method' used here turns out to be faster than
4836 using a sequence of if-statements. */
4837 int success_p = (*it->method) (it);
4839 if (it->what == IT_CHARACTER)
4841 /* Map via display table or translate control characters.
4842 IT->c, IT->len etc. have been set to the next character by
4843 the function call above. If we have a display table, and it
4844 contains an entry for IT->c, translate it. Don't do this if
4845 IT->c itself comes from a display table, otherwise we could
4846 end up in an infinite recursion. (An alternative could be to
4847 count the recursion depth of this function and signal an
4848 error when a certain maximum depth is reached.) Is it worth
4849 it? */
4850 if (success_p && it->dpvec == NULL)
4852 Lisp_Object dv;
4854 if (it->dp
4855 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4856 VECTORP (dv)))
4858 struct Lisp_Vector *v = XVECTOR (dv);
4860 /* Return the first character from the display table
4861 entry, if not empty. If empty, don't display the
4862 current character. */
4863 if (v->size)
4865 it->dpvec_char_len = it->len;
4866 it->dpvec = v->contents;
4867 it->dpend = v->contents + v->size;
4868 it->current.dpvec_index = 0;
4869 it->method = next_element_from_display_vector;
4870 success_p = get_next_display_element (it);
4872 else
4874 set_iterator_to_next (it, 0);
4875 success_p = get_next_display_element (it);
4879 /* Translate control characters into `\003' or `^C' form.
4880 Control characters coming from a display table entry are
4881 currently not translated because we use IT->dpvec to hold
4882 the translation. This could easily be changed but I
4883 don't believe that it is worth doing.
4885 If it->multibyte_p is nonzero, eight-bit characters and
4886 non-printable multibyte characters are also translated to
4887 octal form.
4889 If it->multibyte_p is zero, eight-bit characters that
4890 don't have corresponding multibyte char code are also
4891 translated to octal form. */
4892 else if ((it->c < ' '
4893 && (it->area != TEXT_AREA
4894 || (it->c != '\n' && it->c != '\t')))
4895 || (it->multibyte_p
4896 ? ((it->c >= 127
4897 && it->len == 1)
4898 || !CHAR_PRINTABLE_P (it->c))
4899 : (it->c >= 127
4900 && (!unibyte_display_via_language_environment
4901 || it->c == unibyte_char_to_multibyte (it->c)))))
4903 /* IT->c is a control character which must be displayed
4904 either as '\003' or as `^C' where the '\\' and '^'
4905 can be defined in the display table. Fill
4906 IT->ctl_chars with glyphs for what we have to
4907 display. Then, set IT->dpvec to these glyphs. */
4908 GLYPH g;
4910 if (it->c < 128 && it->ctl_arrow_p)
4912 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4913 if (it->dp
4914 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4915 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4916 g = XINT (DISP_CTRL_GLYPH (it->dp));
4917 else
4918 g = FAST_MAKE_GLYPH ('^', 0);
4919 XSETINT (it->ctl_chars[0], g);
4921 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4922 XSETINT (it->ctl_chars[1], g);
4924 /* Set up IT->dpvec and return first character from it. */
4925 it->dpvec_char_len = it->len;
4926 it->dpvec = it->ctl_chars;
4927 it->dpend = it->dpvec + 2;
4928 it->current.dpvec_index = 0;
4929 it->method = next_element_from_display_vector;
4930 get_next_display_element (it);
4932 else
4934 unsigned char str[MAX_MULTIBYTE_LENGTH];
4935 int len;
4936 int i;
4937 GLYPH escape_glyph;
4939 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4940 if (it->dp
4941 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4942 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4943 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4944 else
4945 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4947 if (SINGLE_BYTE_CHAR_P (it->c))
4948 str[0] = it->c, len = 1;
4949 else
4951 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4952 if (len < 0)
4954 /* It's an invalid character, which
4955 shouldn't happen actually, but due to
4956 bugs it may happen. Let's print the char
4957 as is, there's not much meaningful we can
4958 do with it. */
4959 str[0] = it->c;
4960 str[1] = it->c >> 8;
4961 str[2] = it->c >> 16;
4962 str[3] = it->c >> 24;
4963 len = 4;
4967 for (i = 0; i < len; i++)
4969 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4970 /* Insert three more glyphs into IT->ctl_chars for
4971 the octal display of the character. */
4972 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4973 XSETINT (it->ctl_chars[i * 4 + 1], g);
4974 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4975 XSETINT (it->ctl_chars[i * 4 + 2], g);
4976 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4977 XSETINT (it->ctl_chars[i * 4 + 3], g);
4980 /* Set up IT->dpvec and return the first character
4981 from it. */
4982 it->dpvec_char_len = it->len;
4983 it->dpvec = it->ctl_chars;
4984 it->dpend = it->dpvec + len * 4;
4985 it->current.dpvec_index = 0;
4986 it->method = next_element_from_display_vector;
4987 get_next_display_element (it);
4992 /* Adjust face id for a multibyte character. There are no
4993 multibyte character in unibyte text. */
4994 if (it->multibyte_p
4995 && success_p
4996 && FRAME_WINDOW_P (it->f))
4998 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4999 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5003 /* Is this character the last one of a run of characters with
5004 box? If yes, set IT->end_of_box_run_p to 1. */
5005 if (it->face_box_p
5006 && it->s == NULL)
5008 int face_id;
5009 struct face *face;
5011 it->end_of_box_run_p
5012 = ((face_id = face_after_it_pos (it),
5013 face_id != it->face_id)
5014 && (face = FACE_FROM_ID (it->f, face_id),
5015 face->box == FACE_NO_BOX));
5018 /* Value is 0 if end of buffer or string reached. */
5019 return success_p;
5023 /* Move IT to the next display element.
5025 RESEAT_P non-zero means if called on a newline in buffer text,
5026 skip to the next visible line start.
5028 Functions get_next_display_element and set_iterator_to_next are
5029 separate because I find this arrangement easier to handle than a
5030 get_next_display_element function that also increments IT's
5031 position. The way it is we can first look at an iterator's current
5032 display element, decide whether it fits on a line, and if it does,
5033 increment the iterator position. The other way around we probably
5034 would either need a flag indicating whether the iterator has to be
5035 incremented the next time, or we would have to implement a
5036 decrement position function which would not be easy to write. */
5038 void
5039 set_iterator_to_next (it, reseat_p)
5040 struct it *it;
5041 int reseat_p;
5043 /* Reset flags indicating start and end of a sequence of characters
5044 with box. Reset them at the start of this function because
5045 moving the iterator to a new position might set them. */
5046 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5048 if (it->method == next_element_from_buffer)
5050 /* The current display element of IT is a character from
5051 current_buffer. Advance in the buffer, and maybe skip over
5052 invisible lines that are so because of selective display. */
5053 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5054 reseat_at_next_visible_line_start (it, 0);
5055 else
5057 xassert (it->len != 0);
5058 IT_BYTEPOS (*it) += it->len;
5059 IT_CHARPOS (*it) += 1;
5060 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5063 else if (it->method == next_element_from_composition)
5065 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5066 if (STRINGP (it->string))
5068 IT_STRING_BYTEPOS (*it) += it->len;
5069 IT_STRING_CHARPOS (*it) += it->cmp_len;
5070 it->method = next_element_from_string;
5071 goto consider_string_end;
5073 else
5075 IT_BYTEPOS (*it) += it->len;
5076 IT_CHARPOS (*it) += it->cmp_len;
5077 it->method = next_element_from_buffer;
5080 else if (it->method == next_element_from_c_string)
5082 /* Current display element of IT is from a C string. */
5083 IT_BYTEPOS (*it) += it->len;
5084 IT_CHARPOS (*it) += 1;
5086 else if (it->method == next_element_from_display_vector)
5088 /* Current display element of IT is from a display table entry.
5089 Advance in the display table definition. Reset it to null if
5090 end reached, and continue with characters from buffers/
5091 strings. */
5092 ++it->current.dpvec_index;
5094 /* Restore face of the iterator to what they were before the
5095 display vector entry (these entries may contain faces). */
5096 it->face_id = it->saved_face_id;
5098 if (it->dpvec + it->current.dpvec_index == it->dpend)
5100 if (it->s)
5101 it->method = next_element_from_c_string;
5102 else if (STRINGP (it->string))
5103 it->method = next_element_from_string;
5104 else
5105 it->method = next_element_from_buffer;
5107 it->dpvec = NULL;
5108 it->current.dpvec_index = -1;
5110 /* Skip over characters which were displayed via IT->dpvec. */
5111 if (it->dpvec_char_len < 0)
5112 reseat_at_next_visible_line_start (it, 1);
5113 else if (it->dpvec_char_len > 0)
5115 it->len = it->dpvec_char_len;
5116 set_iterator_to_next (it, reseat_p);
5120 else if (it->method == next_element_from_string)
5122 /* Current display element is a character from a Lisp string. */
5123 xassert (it->s == NULL && STRINGP (it->string));
5124 IT_STRING_BYTEPOS (*it) += it->len;
5125 IT_STRING_CHARPOS (*it) += 1;
5127 consider_string_end:
5129 if (it->current.overlay_string_index >= 0)
5131 /* IT->string is an overlay string. Advance to the
5132 next, if there is one. */
5133 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5134 next_overlay_string (it);
5136 else
5138 /* IT->string is not an overlay string. If we reached
5139 its end, and there is something on IT->stack, proceed
5140 with what is on the stack. This can be either another
5141 string, this time an overlay string, or a buffer. */
5142 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5143 && it->sp > 0)
5145 pop_it (it);
5146 if (!STRINGP (it->string))
5147 it->method = next_element_from_buffer;
5148 else
5149 goto consider_string_end;
5153 else if (it->method == next_element_from_image
5154 || it->method == next_element_from_stretch)
5156 /* The position etc with which we have to proceed are on
5157 the stack. The position may be at the end of a string,
5158 if the `display' property takes up the whole string. */
5159 pop_it (it);
5160 it->image_id = 0;
5161 if (STRINGP (it->string))
5163 it->method = next_element_from_string;
5164 goto consider_string_end;
5166 else
5167 it->method = next_element_from_buffer;
5169 else
5170 /* There are no other methods defined, so this should be a bug. */
5171 abort ();
5173 xassert (it->method != next_element_from_string
5174 || (STRINGP (it->string)
5175 && IT_STRING_CHARPOS (*it) >= 0));
5179 /* Load IT's display element fields with information about the next
5180 display element which comes from a display table entry or from the
5181 result of translating a control character to one of the forms `^C'
5182 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5184 static int
5185 next_element_from_display_vector (it)
5186 struct it *it;
5188 /* Precondition. */
5189 xassert (it->dpvec && it->current.dpvec_index >= 0);
5191 /* Remember the current face id in case glyphs specify faces.
5192 IT's face is restored in set_iterator_to_next. */
5193 it->saved_face_id = it->face_id;
5195 if (INTEGERP (*it->dpvec)
5196 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5198 int lface_id;
5199 GLYPH g;
5201 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5202 it->c = FAST_GLYPH_CHAR (g);
5203 it->len = CHAR_BYTES (it->c);
5205 /* The entry may contain a face id to use. Such a face id is
5206 the id of a Lisp face, not a realized face. A face id of
5207 zero means no face is specified. */
5208 lface_id = FAST_GLYPH_FACE (g);
5209 if (lface_id)
5211 /* The function returns -1 if lface_id is invalid. */
5212 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5213 if (face_id >= 0)
5214 it->face_id = face_id;
5217 else
5218 /* Display table entry is invalid. Return a space. */
5219 it->c = ' ', it->len = 1;
5221 /* Don't change position and object of the iterator here. They are
5222 still the values of the character that had this display table
5223 entry or was translated, and that's what we want. */
5224 it->what = IT_CHARACTER;
5225 return 1;
5229 /* Load IT with the next display element from Lisp string IT->string.
5230 IT->current.string_pos is the current position within the string.
5231 If IT->current.overlay_string_index >= 0, the Lisp string is an
5232 overlay string. */
5234 static int
5235 next_element_from_string (it)
5236 struct it *it;
5238 struct text_pos position;
5240 xassert (STRINGP (it->string));
5241 xassert (IT_STRING_CHARPOS (*it) >= 0);
5242 position = it->current.string_pos;
5244 /* Time to check for invisible text? */
5245 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5246 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5248 handle_stop (it);
5250 /* Since a handler may have changed IT->method, we must
5251 recurse here. */
5252 return get_next_display_element (it);
5255 if (it->current.overlay_string_index >= 0)
5257 /* Get the next character from an overlay string. In overlay
5258 strings, There is no field width or padding with spaces to
5259 do. */
5260 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5262 it->what = IT_EOB;
5263 return 0;
5265 else if (STRING_MULTIBYTE (it->string))
5267 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5268 const unsigned char *s = (SDATA (it->string)
5269 + IT_STRING_BYTEPOS (*it));
5270 it->c = string_char_and_length (s, remaining, &it->len);
5272 else
5274 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5275 it->len = 1;
5278 else
5280 /* Get the next character from a Lisp string that is not an
5281 overlay string. Such strings come from the mode line, for
5282 example. We may have to pad with spaces, or truncate the
5283 string. See also next_element_from_c_string. */
5284 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5286 it->what = IT_EOB;
5287 return 0;
5289 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5291 /* Pad with spaces. */
5292 it->c = ' ', it->len = 1;
5293 CHARPOS (position) = BYTEPOS (position) = -1;
5295 else if (STRING_MULTIBYTE (it->string))
5297 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5298 const unsigned char *s = (SDATA (it->string)
5299 + IT_STRING_BYTEPOS (*it));
5300 it->c = string_char_and_length (s, maxlen, &it->len);
5302 else
5304 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5305 it->len = 1;
5309 /* Record what we have and where it came from. Note that we store a
5310 buffer position in IT->position although it could arguably be a
5311 string position. */
5312 it->what = IT_CHARACTER;
5313 it->object = it->string;
5314 it->position = position;
5315 return 1;
5319 /* Load IT with next display element from C string IT->s.
5320 IT->string_nchars is the maximum number of characters to return
5321 from the string. IT->end_charpos may be greater than
5322 IT->string_nchars when this function is called, in which case we
5323 may have to return padding spaces. Value is zero if end of string
5324 reached, including padding spaces. */
5326 static int
5327 next_element_from_c_string (it)
5328 struct it *it;
5330 int success_p = 1;
5332 xassert (it->s);
5333 it->what = IT_CHARACTER;
5334 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5335 it->object = Qnil;
5337 /* IT's position can be greater IT->string_nchars in case a field
5338 width or precision has been specified when the iterator was
5339 initialized. */
5340 if (IT_CHARPOS (*it) >= it->end_charpos)
5342 /* End of the game. */
5343 it->what = IT_EOB;
5344 success_p = 0;
5346 else if (IT_CHARPOS (*it) >= it->string_nchars)
5348 /* Pad with spaces. */
5349 it->c = ' ', it->len = 1;
5350 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5352 else if (it->multibyte_p)
5354 /* Implementation note: The calls to strlen apparently aren't a
5355 performance problem because there is no noticeable performance
5356 difference between Emacs running in unibyte or multibyte mode. */
5357 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5358 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5359 maxlen, &it->len);
5361 else
5362 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5364 return success_p;
5368 /* Set up IT to return characters from an ellipsis, if appropriate.
5369 The definition of the ellipsis glyphs may come from a display table
5370 entry. This function Fills IT with the first glyph from the
5371 ellipsis if an ellipsis is to be displayed. */
5373 static int
5374 next_element_from_ellipsis (it)
5375 struct it *it;
5377 if (it->selective_display_ellipsis_p)
5379 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5381 /* Use the display table definition for `...'. Invalid glyphs
5382 will be handled by the method returning elements from dpvec. */
5383 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5384 it->dpvec_char_len = it->len;
5385 it->dpvec = v->contents;
5386 it->dpend = v->contents + v->size;
5387 it->current.dpvec_index = 0;
5388 it->method = next_element_from_display_vector;
5390 else
5392 /* Use default `...' which is stored in default_invis_vector. */
5393 it->dpvec_char_len = it->len;
5394 it->dpvec = default_invis_vector;
5395 it->dpend = default_invis_vector + 3;
5396 it->current.dpvec_index = 0;
5397 it->method = next_element_from_display_vector;
5400 else
5402 /* The face at the current position may be different from the
5403 face we find after the invisible text. Remember what it
5404 was in IT->saved_face_id, and signal that it's there by
5405 setting face_before_selective_p. */
5406 it->saved_face_id = it->face_id;
5407 it->method = next_element_from_buffer;
5408 reseat_at_next_visible_line_start (it, 1);
5409 it->face_before_selective_p = 1;
5412 return get_next_display_element (it);
5416 /* Deliver an image display element. The iterator IT is already
5417 filled with image information (done in handle_display_prop). Value
5418 is always 1. */
5421 static int
5422 next_element_from_image (it)
5423 struct it *it;
5425 it->what = IT_IMAGE;
5426 return 1;
5430 /* Fill iterator IT with next display element from a stretch glyph
5431 property. IT->object is the value of the text property. Value is
5432 always 1. */
5434 static int
5435 next_element_from_stretch (it)
5436 struct it *it;
5438 it->what = IT_STRETCH;
5439 return 1;
5443 /* Load IT with the next display element from current_buffer. Value
5444 is zero if end of buffer reached. IT->stop_charpos is the next
5445 position at which to stop and check for text properties or buffer
5446 end. */
5448 static int
5449 next_element_from_buffer (it)
5450 struct it *it;
5452 int success_p = 1;
5454 /* Check this assumption, otherwise, we would never enter the
5455 if-statement, below. */
5456 xassert (IT_CHARPOS (*it) >= BEGV
5457 && IT_CHARPOS (*it) <= it->stop_charpos);
5459 if (IT_CHARPOS (*it) >= it->stop_charpos)
5461 if (IT_CHARPOS (*it) >= it->end_charpos)
5463 int overlay_strings_follow_p;
5465 /* End of the game, except when overlay strings follow that
5466 haven't been returned yet. */
5467 if (it->overlay_strings_at_end_processed_p)
5468 overlay_strings_follow_p = 0;
5469 else
5471 it->overlay_strings_at_end_processed_p = 1;
5472 overlay_strings_follow_p = get_overlay_strings (it, 0);
5475 if (overlay_strings_follow_p)
5476 success_p = get_next_display_element (it);
5477 else
5479 it->what = IT_EOB;
5480 it->position = it->current.pos;
5481 success_p = 0;
5484 else
5486 handle_stop (it);
5487 return get_next_display_element (it);
5490 else
5492 /* No face changes, overlays etc. in sight, so just return a
5493 character from current_buffer. */
5494 unsigned char *p;
5496 /* Maybe run the redisplay end trigger hook. Performance note:
5497 This doesn't seem to cost measurable time. */
5498 if (it->redisplay_end_trigger_charpos
5499 && it->glyph_row
5500 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5501 run_redisplay_end_trigger_hook (it);
5503 /* Get the next character, maybe multibyte. */
5504 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5505 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5507 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5508 - IT_BYTEPOS (*it));
5509 it->c = string_char_and_length (p, maxlen, &it->len);
5511 else
5512 it->c = *p, it->len = 1;
5514 /* Record what we have and where it came from. */
5515 it->what = IT_CHARACTER;;
5516 it->object = it->w->buffer;
5517 it->position = it->current.pos;
5519 /* Normally we return the character found above, except when we
5520 really want to return an ellipsis for selective display. */
5521 if (it->selective)
5523 if (it->c == '\n')
5525 /* A value of selective > 0 means hide lines indented more
5526 than that number of columns. */
5527 if (it->selective > 0
5528 && IT_CHARPOS (*it) + 1 < ZV
5529 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5530 IT_BYTEPOS (*it) + 1,
5531 (double) it->selective)) /* iftc */
5533 success_p = next_element_from_ellipsis (it);
5534 it->dpvec_char_len = -1;
5537 else if (it->c == '\r' && it->selective == -1)
5539 /* A value of selective == -1 means that everything from the
5540 CR to the end of the line is invisible, with maybe an
5541 ellipsis displayed for it. */
5542 success_p = next_element_from_ellipsis (it);
5543 it->dpvec_char_len = -1;
5548 /* Value is zero if end of buffer reached. */
5549 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5550 return success_p;
5554 /* Run the redisplay end trigger hook for IT. */
5556 static void
5557 run_redisplay_end_trigger_hook (it)
5558 struct it *it;
5560 Lisp_Object args[3];
5562 /* IT->glyph_row should be non-null, i.e. we should be actually
5563 displaying something, or otherwise we should not run the hook. */
5564 xassert (it->glyph_row);
5566 /* Set up hook arguments. */
5567 args[0] = Qredisplay_end_trigger_functions;
5568 args[1] = it->window;
5569 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5570 it->redisplay_end_trigger_charpos = 0;
5572 /* Since we are *trying* to run these functions, don't try to run
5573 them again, even if they get an error. */
5574 it->w->redisplay_end_trigger = Qnil;
5575 Frun_hook_with_args (3, args);
5577 /* Notice if it changed the face of the character we are on. */
5578 handle_face_prop (it);
5582 /* Deliver a composition display element. The iterator IT is already
5583 filled with composition information (done in
5584 handle_composition_prop). Value is always 1. */
5586 static int
5587 next_element_from_composition (it)
5588 struct it *it;
5590 it->what = IT_COMPOSITION;
5591 it->position = (STRINGP (it->string)
5592 ? it->current.string_pos
5593 : it->current.pos);
5594 return 1;
5599 /***********************************************************************
5600 Moving an iterator without producing glyphs
5601 ***********************************************************************/
5603 /* Move iterator IT to a specified buffer or X position within one
5604 line on the display without producing glyphs.
5606 OP should be a bit mask including some or all of these bits:
5607 MOVE_TO_X: Stop on reaching x-position TO_X.
5608 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5609 Regardless of OP's value, stop in reaching the end of the display line.
5611 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5612 This means, in particular, that TO_X includes window's horizontal
5613 scroll amount.
5615 The return value has several possible values that
5616 say what condition caused the scan to stop:
5618 MOVE_POS_MATCH_OR_ZV
5619 - when TO_POS or ZV was reached.
5621 MOVE_X_REACHED
5622 -when TO_X was reached before TO_POS or ZV were reached.
5624 MOVE_LINE_CONTINUED
5625 - when we reached the end of the display area and the line must
5626 be continued.
5628 MOVE_LINE_TRUNCATED
5629 - when we reached the end of the display area and the line is
5630 truncated.
5632 MOVE_NEWLINE_OR_CR
5633 - when we stopped at a line end, i.e. a newline or a CR and selective
5634 display is on. */
5636 static enum move_it_result
5637 move_it_in_display_line_to (it, to_charpos, to_x, op)
5638 struct it *it;
5639 int to_charpos, to_x, op;
5641 enum move_it_result result = MOVE_UNDEFINED;
5642 struct glyph_row *saved_glyph_row;
5644 /* Don't produce glyphs in produce_glyphs. */
5645 saved_glyph_row = it->glyph_row;
5646 it->glyph_row = NULL;
5648 #define BUFFER_POS_REACHED_P() \
5649 ((op & MOVE_TO_POS) != 0 \
5650 && BUFFERP (it->object) \
5651 && IT_CHARPOS (*it) >= to_charpos)
5653 while (1)
5655 int x, i, ascent = 0, descent = 0;
5657 /* Stop when ZV reached.
5658 We used to stop here when TO_CHARPOS reached as well, but that is
5659 too soon if this glyph does not fit on this line. So we handle it
5660 explicitly below. */
5661 if (!get_next_display_element (it)
5662 || (it->truncate_lines_p
5663 && BUFFER_POS_REACHED_P ()))
5665 result = MOVE_POS_MATCH_OR_ZV;
5666 break;
5669 /* The call to produce_glyphs will get the metrics of the
5670 display element IT is loaded with. We record in x the
5671 x-position before this display element in case it does not
5672 fit on the line. */
5673 x = it->current_x;
5675 /* Remember the line height so far in case the next element doesn't
5676 fit on the line. */
5677 if (!it->truncate_lines_p)
5679 ascent = it->max_ascent;
5680 descent = it->max_descent;
5683 PRODUCE_GLYPHS (it);
5685 if (it->area != TEXT_AREA)
5687 set_iterator_to_next (it, 1);
5688 continue;
5691 /* The number of glyphs we get back in IT->nglyphs will normally
5692 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5693 character on a terminal frame, or (iii) a line end. For the
5694 second case, IT->nglyphs - 1 padding glyphs will be present
5695 (on X frames, there is only one glyph produced for a
5696 composite character.
5698 The behavior implemented below means, for continuation lines,
5699 that as many spaces of a TAB as fit on the current line are
5700 displayed there. For terminal frames, as many glyphs of a
5701 multi-glyph character are displayed in the current line, too.
5702 This is what the old redisplay code did, and we keep it that
5703 way. Under X, the whole shape of a complex character must
5704 fit on the line or it will be completely displayed in the
5705 next line.
5707 Note that both for tabs and padding glyphs, all glyphs have
5708 the same width. */
5709 if (it->nglyphs)
5711 /* More than one glyph or glyph doesn't fit on line. All
5712 glyphs have the same width. */
5713 int single_glyph_width = it->pixel_width / it->nglyphs;
5714 int new_x;
5716 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5718 new_x = x + single_glyph_width;
5720 /* We want to leave anything reaching TO_X to the caller. */
5721 if ((op & MOVE_TO_X) && new_x > to_x)
5723 if (BUFFER_POS_REACHED_P ())
5724 goto buffer_pos_reached;
5725 it->current_x = x;
5726 result = MOVE_X_REACHED;
5727 break;
5729 else if (/* Lines are continued. */
5730 !it->truncate_lines_p
5731 && (/* And glyph doesn't fit on the line. */
5732 new_x > it->last_visible_x
5733 /* Or it fits exactly and we're on a window
5734 system frame. */
5735 || (new_x == it->last_visible_x
5736 && FRAME_WINDOW_P (it->f))))
5738 if (/* IT->hpos == 0 means the very first glyph
5739 doesn't fit on the line, e.g. a wide image. */
5740 it->hpos == 0
5741 || (new_x == it->last_visible_x
5742 && FRAME_WINDOW_P (it->f)))
5744 ++it->hpos;
5745 it->current_x = new_x;
5746 if (i == it->nglyphs - 1)
5748 set_iterator_to_next (it, 1);
5749 #ifdef HAVE_WINDOW_SYSTEM
5750 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5752 if (!get_next_display_element (it))
5754 result = MOVE_POS_MATCH_OR_ZV;
5755 break;
5757 if (BUFFER_POS_REACHED_P ())
5759 if (ITERATOR_AT_END_OF_LINE_P (it))
5760 result = MOVE_POS_MATCH_OR_ZV;
5761 else
5762 result = MOVE_LINE_CONTINUED;
5763 break;
5765 if (ITERATOR_AT_END_OF_LINE_P (it))
5767 result = MOVE_NEWLINE_OR_CR;
5768 break;
5771 #endif /* HAVE_WINDOW_SYSTEM */
5774 else
5776 it->current_x = x;
5777 it->max_ascent = ascent;
5778 it->max_descent = descent;
5781 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5782 IT_CHARPOS (*it)));
5783 result = MOVE_LINE_CONTINUED;
5784 break;
5786 else if (BUFFER_POS_REACHED_P ())
5787 goto buffer_pos_reached;
5788 else if (new_x > it->first_visible_x)
5790 /* Glyph is visible. Increment number of glyphs that
5791 would be displayed. */
5792 ++it->hpos;
5794 else
5796 /* Glyph is completely off the left margin of the display
5797 area. Nothing to do. */
5801 if (result != MOVE_UNDEFINED)
5802 break;
5804 else if (BUFFER_POS_REACHED_P ())
5806 buffer_pos_reached:
5807 it->current_x = x;
5808 it->max_ascent = ascent;
5809 it->max_descent = descent;
5810 result = MOVE_POS_MATCH_OR_ZV;
5811 break;
5813 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5815 /* Stop when TO_X specified and reached. This check is
5816 necessary here because of lines consisting of a line end,
5817 only. The line end will not produce any glyphs and we
5818 would never get MOVE_X_REACHED. */
5819 xassert (it->nglyphs == 0);
5820 result = MOVE_X_REACHED;
5821 break;
5824 /* Is this a line end? If yes, we're done. */
5825 if (ITERATOR_AT_END_OF_LINE_P (it))
5827 result = MOVE_NEWLINE_OR_CR;
5828 break;
5831 /* The current display element has been consumed. Advance
5832 to the next. */
5833 set_iterator_to_next (it, 1);
5835 /* Stop if lines are truncated and IT's current x-position is
5836 past the right edge of the window now. */
5837 if (it->truncate_lines_p
5838 && it->current_x >= it->last_visible_x)
5840 #ifdef HAVE_WINDOW_SYSTEM
5841 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5843 if (!get_next_display_element (it)
5844 || BUFFER_POS_REACHED_P ())
5846 result = MOVE_POS_MATCH_OR_ZV;
5847 break;
5849 if (ITERATOR_AT_END_OF_LINE_P (it))
5851 result = MOVE_NEWLINE_OR_CR;
5852 break;
5855 #endif /* HAVE_WINDOW_SYSTEM */
5856 result = MOVE_LINE_TRUNCATED;
5857 break;
5861 #undef BUFFER_POS_REACHED_P
5863 /* Restore the iterator settings altered at the beginning of this
5864 function. */
5865 it->glyph_row = saved_glyph_row;
5866 return result;
5870 /* Move IT forward until it satisfies one or more of the criteria in
5871 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5873 OP is a bit-mask that specifies where to stop, and in particular,
5874 which of those four position arguments makes a difference. See the
5875 description of enum move_operation_enum.
5877 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5878 screen line, this function will set IT to the next position >
5879 TO_CHARPOS. */
5881 void
5882 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5883 struct it *it;
5884 int to_charpos, to_x, to_y, to_vpos;
5885 int op;
5887 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5888 int line_height;
5889 int reached = 0;
5891 for (;;)
5893 if (op & MOVE_TO_VPOS)
5895 /* If no TO_CHARPOS and no TO_X specified, stop at the
5896 start of the line TO_VPOS. */
5897 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5899 if (it->vpos == to_vpos)
5901 reached = 1;
5902 break;
5904 else
5905 skip = move_it_in_display_line_to (it, -1, -1, 0);
5907 else
5909 /* TO_VPOS >= 0 means stop at TO_X in the line at
5910 TO_VPOS, or at TO_POS, whichever comes first. */
5911 if (it->vpos == to_vpos)
5913 reached = 2;
5914 break;
5917 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5919 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5921 reached = 3;
5922 break;
5924 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5926 /* We have reached TO_X but not in the line we want. */
5927 skip = move_it_in_display_line_to (it, to_charpos,
5928 -1, MOVE_TO_POS);
5929 if (skip == MOVE_POS_MATCH_OR_ZV)
5931 reached = 4;
5932 break;
5937 else if (op & MOVE_TO_Y)
5939 struct it it_backup;
5941 /* TO_Y specified means stop at TO_X in the line containing
5942 TO_Y---or at TO_CHARPOS if this is reached first. The
5943 problem is that we can't really tell whether the line
5944 contains TO_Y before we have completely scanned it, and
5945 this may skip past TO_X. What we do is to first scan to
5946 TO_X.
5948 If TO_X is not specified, use a TO_X of zero. The reason
5949 is to make the outcome of this function more predictable.
5950 If we didn't use TO_X == 0, we would stop at the end of
5951 the line which is probably not what a caller would expect
5952 to happen. */
5953 skip = move_it_in_display_line_to (it, to_charpos,
5954 ((op & MOVE_TO_X)
5955 ? to_x : 0),
5956 (MOVE_TO_X
5957 | (op & MOVE_TO_POS)));
5959 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5960 if (skip == MOVE_POS_MATCH_OR_ZV)
5962 reached = 5;
5963 break;
5966 /* If TO_X was reached, we would like to know whether TO_Y
5967 is in the line. This can only be said if we know the
5968 total line height which requires us to scan the rest of
5969 the line. */
5970 if (skip == MOVE_X_REACHED)
5972 it_backup = *it;
5973 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5974 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5975 op & MOVE_TO_POS);
5976 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5979 /* Now, decide whether TO_Y is in this line. */
5980 line_height = it->max_ascent + it->max_descent;
5981 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5983 if (to_y >= it->current_y
5984 && to_y < it->current_y + line_height)
5986 if (skip == MOVE_X_REACHED)
5987 /* If TO_Y is in this line and TO_X was reached above,
5988 we scanned too far. We have to restore IT's settings
5989 to the ones before skipping. */
5990 *it = it_backup;
5991 reached = 6;
5993 else if (skip == MOVE_X_REACHED)
5995 skip = skip2;
5996 if (skip == MOVE_POS_MATCH_OR_ZV)
5997 reached = 7;
6000 if (reached)
6001 break;
6003 else
6004 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6006 switch (skip)
6008 case MOVE_POS_MATCH_OR_ZV:
6009 reached = 8;
6010 goto out;
6012 case MOVE_NEWLINE_OR_CR:
6013 set_iterator_to_next (it, 1);
6014 it->continuation_lines_width = 0;
6015 break;
6017 case MOVE_LINE_TRUNCATED:
6018 it->continuation_lines_width = 0;
6019 reseat_at_next_visible_line_start (it, 0);
6020 if ((op & MOVE_TO_POS) != 0
6021 && IT_CHARPOS (*it) > to_charpos)
6023 reached = 9;
6024 goto out;
6026 break;
6028 case MOVE_LINE_CONTINUED:
6029 it->continuation_lines_width += it->current_x;
6030 break;
6032 default:
6033 abort ();
6036 /* Reset/increment for the next run. */
6037 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6038 it->current_x = it->hpos = 0;
6039 it->current_y += it->max_ascent + it->max_descent;
6040 ++it->vpos;
6041 last_height = it->max_ascent + it->max_descent;
6042 last_max_ascent = it->max_ascent;
6043 it->max_ascent = it->max_descent = 0;
6046 out:
6048 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6052 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6054 If DY > 0, move IT backward at least that many pixels. DY = 0
6055 means move IT backward to the preceding line start or BEGV. This
6056 function may move over more than DY pixels if IT->current_y - DY
6057 ends up in the middle of a line; in this case IT->current_y will be
6058 set to the top of the line moved to. */
6060 void
6061 move_it_vertically_backward (it, dy)
6062 struct it *it;
6063 int dy;
6065 int nlines, h;
6066 struct it it2, it3;
6067 int start_pos = IT_CHARPOS (*it);
6069 xassert (dy >= 0);
6071 /* Estimate how many newlines we must move back. */
6072 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6074 /* Set the iterator's position that many lines back. */
6075 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6076 back_to_previous_visible_line_start (it);
6078 /* Reseat the iterator here. When moving backward, we don't want
6079 reseat to skip forward over invisible text, set up the iterator
6080 to deliver from overlay strings at the new position etc. So,
6081 use reseat_1 here. */
6082 reseat_1 (it, it->current.pos, 1);
6084 /* We are now surely at a line start. */
6085 it->current_x = it->hpos = 0;
6086 it->continuation_lines_width = 0;
6088 /* Move forward and see what y-distance we moved. First move to the
6089 start of the next line so that we get its height. We need this
6090 height to be able to tell whether we reached the specified
6091 y-distance. */
6092 it2 = *it;
6093 it2.max_ascent = it2.max_descent = 0;
6094 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6095 MOVE_TO_POS | MOVE_TO_VPOS);
6096 xassert (IT_CHARPOS (*it) >= BEGV);
6097 it3 = it2;
6099 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6100 xassert (IT_CHARPOS (*it) >= BEGV);
6101 /* H is the actual vertical distance from the position in *IT
6102 and the starting position. */
6103 h = it2.current_y - it->current_y;
6104 /* NLINES is the distance in number of lines. */
6105 nlines = it2.vpos - it->vpos;
6107 /* Correct IT's y and vpos position
6108 so that they are relative to the starting point. */
6109 it->vpos -= nlines;
6110 it->current_y -= h;
6112 if (dy == 0)
6114 /* DY == 0 means move to the start of the screen line. The
6115 value of nlines is > 0 if continuation lines were involved. */
6116 if (nlines > 0)
6117 move_it_by_lines (it, nlines, 1);
6118 xassert (IT_CHARPOS (*it) <= start_pos);
6120 else
6122 /* The y-position we try to reach, relative to *IT.
6123 Note that H has been subtracted in front of the if-statement. */
6124 int target_y = it->current_y + h - dy;
6125 int y0 = it3.current_y;
6126 int y1 = line_bottom_y (&it3);
6127 int line_height = y1 - y0;
6129 /* If we did not reach target_y, try to move further backward if
6130 we can. If we moved too far backward, try to move forward. */
6131 if (target_y < it->current_y
6132 /* This is heuristic. In a window that's 3 lines high, with
6133 a line height of 13 pixels each, recentering with point
6134 on the bottom line will try to move -39/2 = 19 pixels
6135 backward. Try to avoid moving into the first line. */
6136 && it->current_y - target_y > line_height / 3 * 2
6137 && IT_CHARPOS (*it) > BEGV)
6139 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6140 target_y - it->current_y));
6141 move_it_vertically (it, target_y - it->current_y);
6142 xassert (IT_CHARPOS (*it) >= BEGV);
6144 else if (target_y >= it->current_y + line_height
6145 && IT_CHARPOS (*it) < ZV)
6147 /* Should move forward by at least one line, maybe more.
6149 Note: Calling move_it_by_lines can be expensive on
6150 terminal frames, where compute_motion is used (via
6151 vmotion) to do the job, when there are very long lines
6152 and truncate-lines is nil. That's the reason for
6153 treating terminal frames specially here. */
6155 if (!FRAME_WINDOW_P (it->f))
6156 move_it_vertically (it, target_y - (it->current_y + line_height));
6157 else
6161 move_it_by_lines (it, 1, 1);
6163 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6166 xassert (IT_CHARPOS (*it) >= BEGV);
6172 /* Move IT by a specified amount of pixel lines DY. DY negative means
6173 move backwards. DY = 0 means move to start of screen line. At the
6174 end, IT will be on the start of a screen line. */
6176 void
6177 move_it_vertically (it, dy)
6178 struct it *it;
6179 int dy;
6181 if (dy <= 0)
6182 move_it_vertically_backward (it, -dy);
6183 else if (dy > 0)
6185 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6186 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6187 MOVE_TO_POS | MOVE_TO_Y);
6188 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6190 /* If buffer ends in ZV without a newline, move to the start of
6191 the line to satisfy the post-condition. */
6192 if (IT_CHARPOS (*it) == ZV
6193 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6194 move_it_by_lines (it, 0, 0);
6199 /* Move iterator IT past the end of the text line it is in. */
6201 void
6202 move_it_past_eol (it)
6203 struct it *it;
6205 enum move_it_result rc;
6207 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6208 if (rc == MOVE_NEWLINE_OR_CR)
6209 set_iterator_to_next (it, 0);
6213 #if 0 /* Currently not used. */
6215 /* Return non-zero if some text between buffer positions START_CHARPOS
6216 and END_CHARPOS is invisible. IT->window is the window for text
6217 property lookup. */
6219 static int
6220 invisible_text_between_p (it, start_charpos, end_charpos)
6221 struct it *it;
6222 int start_charpos, end_charpos;
6224 Lisp_Object prop, limit;
6225 int invisible_found_p;
6227 xassert (it != NULL && start_charpos <= end_charpos);
6229 /* Is text at START invisible? */
6230 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6231 it->window);
6232 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6233 invisible_found_p = 1;
6234 else
6236 limit = Fnext_single_char_property_change (make_number (start_charpos),
6237 Qinvisible, Qnil,
6238 make_number (end_charpos));
6239 invisible_found_p = XFASTINT (limit) < end_charpos;
6242 return invisible_found_p;
6245 #endif /* 0 */
6248 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6249 negative means move up. DVPOS == 0 means move to the start of the
6250 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6251 NEED_Y_P is zero, IT->current_y will be left unchanged.
6253 Further optimization ideas: If we would know that IT->f doesn't use
6254 a face with proportional font, we could be faster for
6255 truncate-lines nil. */
6257 void
6258 move_it_by_lines (it, dvpos, need_y_p)
6259 struct it *it;
6260 int dvpos, need_y_p;
6262 struct position pos;
6264 if (!FRAME_WINDOW_P (it->f))
6266 struct text_pos textpos;
6268 /* We can use vmotion on frames without proportional fonts. */
6269 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6270 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6271 reseat (it, textpos, 1);
6272 it->vpos += pos.vpos;
6273 it->current_y += pos.vpos;
6275 else if (dvpos == 0)
6277 /* DVPOS == 0 means move to the start of the screen line. */
6278 move_it_vertically_backward (it, 0);
6279 xassert (it->current_x == 0 && it->hpos == 0);
6281 else if (dvpos > 0)
6282 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6283 else
6285 struct it it2;
6286 int start_charpos, i;
6288 /* Start at the beginning of the screen line containing IT's
6289 position. */
6290 move_it_vertically_backward (it, 0);
6292 /* Go back -DVPOS visible lines and reseat the iterator there. */
6293 start_charpos = IT_CHARPOS (*it);
6294 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6295 back_to_previous_visible_line_start (it);
6296 reseat (it, it->current.pos, 1);
6297 it->current_x = it->hpos = 0;
6299 /* Above call may have moved too far if continuation lines
6300 are involved. Scan forward and see if it did. */
6301 it2 = *it;
6302 it2.vpos = it2.current_y = 0;
6303 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6304 it->vpos -= it2.vpos;
6305 it->current_y -= it2.current_y;
6306 it->current_x = it->hpos = 0;
6308 /* If we moved too far, move IT some lines forward. */
6309 if (it2.vpos > -dvpos)
6311 int delta = it2.vpos + dvpos;
6312 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6317 /* Return 1 if IT points into the middle of a display vector. */
6320 in_display_vector_p (it)
6321 struct it *it;
6323 return (it->method == next_element_from_display_vector
6324 && it->current.dpvec_index > 0
6325 && it->dpvec + it->current.dpvec_index != it->dpend);
6329 /***********************************************************************
6330 Messages
6331 ***********************************************************************/
6334 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6335 to *Messages*. */
6337 void
6338 add_to_log (format, arg1, arg2)
6339 char *format;
6340 Lisp_Object arg1, arg2;
6342 Lisp_Object args[3];
6343 Lisp_Object msg, fmt;
6344 char *buffer;
6345 int len;
6346 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6347 USE_SAFE_ALLOCA;
6349 /* Do nothing if called asynchronously. Inserting text into
6350 a buffer may call after-change-functions and alike and
6351 that would means running Lisp asynchronously. */
6352 if (handling_signal)
6353 return;
6355 fmt = msg = Qnil;
6356 GCPRO4 (fmt, msg, arg1, arg2);
6358 args[0] = fmt = build_string (format);
6359 args[1] = arg1;
6360 args[2] = arg2;
6361 msg = Fformat (3, args);
6363 len = SBYTES (msg) + 1;
6364 SAFE_ALLOCA (buffer, char *, len);
6365 bcopy (SDATA (msg), buffer, len);
6367 message_dolog (buffer, len - 1, 1, 0);
6368 SAFE_FREE (len);
6370 UNGCPRO;
6374 /* Output a newline in the *Messages* buffer if "needs" one. */
6376 void
6377 message_log_maybe_newline ()
6379 if (message_log_need_newline)
6380 message_dolog ("", 0, 1, 0);
6384 /* Add a string M of length NBYTES to the message log, optionally
6385 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6386 nonzero, means interpret the contents of M as multibyte. This
6387 function calls low-level routines in order to bypass text property
6388 hooks, etc. which might not be safe to run. */
6390 void
6391 message_dolog (m, nbytes, nlflag, multibyte)
6392 const char *m;
6393 int nbytes, nlflag, multibyte;
6395 if (!NILP (Vmemory_full))
6396 return;
6398 if (!NILP (Vmessage_log_max))
6400 struct buffer *oldbuf;
6401 Lisp_Object oldpoint, oldbegv, oldzv;
6402 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6403 int point_at_end = 0;
6404 int zv_at_end = 0;
6405 Lisp_Object old_deactivate_mark, tem;
6406 struct gcpro gcpro1;
6408 old_deactivate_mark = Vdeactivate_mark;
6409 oldbuf = current_buffer;
6410 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6411 current_buffer->undo_list = Qt;
6413 oldpoint = message_dolog_marker1;
6414 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6415 oldbegv = message_dolog_marker2;
6416 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6417 oldzv = message_dolog_marker3;
6418 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6419 GCPRO1 (old_deactivate_mark);
6421 if (PT == Z)
6422 point_at_end = 1;
6423 if (ZV == Z)
6424 zv_at_end = 1;
6426 BEGV = BEG;
6427 BEGV_BYTE = BEG_BYTE;
6428 ZV = Z;
6429 ZV_BYTE = Z_BYTE;
6430 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6432 /* Insert the string--maybe converting multibyte to single byte
6433 or vice versa, so that all the text fits the buffer. */
6434 if (multibyte
6435 && NILP (current_buffer->enable_multibyte_characters))
6437 int i, c, char_bytes;
6438 unsigned char work[1];
6440 /* Convert a multibyte string to single-byte
6441 for the *Message* buffer. */
6442 for (i = 0; i < nbytes; i += char_bytes)
6444 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6445 work[0] = (SINGLE_BYTE_CHAR_P (c)
6447 : multibyte_char_to_unibyte (c, Qnil));
6448 insert_1_both (work, 1, 1, 1, 0, 0);
6451 else if (! multibyte
6452 && ! NILP (current_buffer->enable_multibyte_characters))
6454 int i, c, char_bytes;
6455 unsigned char *msg = (unsigned char *) m;
6456 unsigned char str[MAX_MULTIBYTE_LENGTH];
6457 /* Convert a single-byte string to multibyte
6458 for the *Message* buffer. */
6459 for (i = 0; i < nbytes; i++)
6461 c = unibyte_char_to_multibyte (msg[i]);
6462 char_bytes = CHAR_STRING (c, str);
6463 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6466 else if (nbytes)
6467 insert_1 (m, nbytes, 1, 0, 0);
6469 if (nlflag)
6471 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6472 insert_1 ("\n", 1, 1, 0, 0);
6474 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6475 this_bol = PT;
6476 this_bol_byte = PT_BYTE;
6478 /* See if this line duplicates the previous one.
6479 If so, combine duplicates. */
6480 if (this_bol > BEG)
6482 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6483 prev_bol = PT;
6484 prev_bol_byte = PT_BYTE;
6486 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6487 this_bol, this_bol_byte);
6488 if (dup)
6490 del_range_both (prev_bol, prev_bol_byte,
6491 this_bol, this_bol_byte, 0);
6492 if (dup > 1)
6494 char dupstr[40];
6495 int duplen;
6497 /* If you change this format, don't forget to also
6498 change message_log_check_duplicate. */
6499 sprintf (dupstr, " [%d times]", dup);
6500 duplen = strlen (dupstr);
6501 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6502 insert_1 (dupstr, duplen, 1, 0, 1);
6507 /* If we have more than the desired maximum number of lines
6508 in the *Messages* buffer now, delete the oldest ones.
6509 This is safe because we don't have undo in this buffer. */
6511 if (NATNUMP (Vmessage_log_max))
6513 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6514 -XFASTINT (Vmessage_log_max) - 1, 0);
6515 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6518 BEGV = XMARKER (oldbegv)->charpos;
6519 BEGV_BYTE = marker_byte_position (oldbegv);
6521 if (zv_at_end)
6523 ZV = Z;
6524 ZV_BYTE = Z_BYTE;
6526 else
6528 ZV = XMARKER (oldzv)->charpos;
6529 ZV_BYTE = marker_byte_position (oldzv);
6532 if (point_at_end)
6533 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6534 else
6535 /* We can't do Fgoto_char (oldpoint) because it will run some
6536 Lisp code. */
6537 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6538 XMARKER (oldpoint)->bytepos);
6540 UNGCPRO;
6541 unchain_marker (XMARKER (oldpoint));
6542 unchain_marker (XMARKER (oldbegv));
6543 unchain_marker (XMARKER (oldzv));
6545 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6546 set_buffer_internal (oldbuf);
6547 if (NILP (tem))
6548 windows_or_buffers_changed = old_windows_or_buffers_changed;
6549 message_log_need_newline = !nlflag;
6550 Vdeactivate_mark = old_deactivate_mark;
6555 /* We are at the end of the buffer after just having inserted a newline.
6556 (Note: We depend on the fact we won't be crossing the gap.)
6557 Check to see if the most recent message looks a lot like the previous one.
6558 Return 0 if different, 1 if the new one should just replace it, or a
6559 value N > 1 if we should also append " [N times]". */
6561 static int
6562 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6563 int prev_bol, this_bol;
6564 int prev_bol_byte, this_bol_byte;
6566 int i;
6567 int len = Z_BYTE - 1 - this_bol_byte;
6568 int seen_dots = 0;
6569 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6570 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6572 for (i = 0; i < len; i++)
6574 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6575 seen_dots = 1;
6576 if (p1[i] != p2[i])
6577 return seen_dots;
6579 p1 += len;
6580 if (*p1 == '\n')
6581 return 2;
6582 if (*p1++ == ' ' && *p1++ == '[')
6584 int n = 0;
6585 while (*p1 >= '0' && *p1 <= '9')
6586 n = n * 10 + *p1++ - '0';
6587 if (strncmp (p1, " times]\n", 8) == 0)
6588 return n+1;
6590 return 0;
6594 /* Display an echo area message M with a specified length of NBYTES
6595 bytes. The string may include null characters. If M is 0, clear
6596 out any existing message, and let the mini-buffer text show
6597 through.
6599 The buffer M must continue to exist until after the echo area gets
6600 cleared or some other message gets displayed there. This means do
6601 not pass text that is stored in a Lisp string; do not pass text in
6602 a buffer that was alloca'd. */
6604 void
6605 message2 (m, nbytes, multibyte)
6606 const char *m;
6607 int nbytes;
6608 int multibyte;
6610 /* First flush out any partial line written with print. */
6611 message_log_maybe_newline ();
6612 if (m)
6613 message_dolog (m, nbytes, 1, multibyte);
6614 message2_nolog (m, nbytes, multibyte);
6618 /* The non-logging counterpart of message2. */
6620 void
6621 message2_nolog (m, nbytes, multibyte)
6622 const char *m;
6623 int nbytes, multibyte;
6625 struct frame *sf = SELECTED_FRAME ();
6626 message_enable_multibyte = multibyte;
6628 if (noninteractive)
6630 if (noninteractive_need_newline)
6631 putc ('\n', stderr);
6632 noninteractive_need_newline = 0;
6633 if (m)
6634 fwrite (m, nbytes, 1, stderr);
6635 if (cursor_in_echo_area == 0)
6636 fprintf (stderr, "\n");
6637 fflush (stderr);
6639 /* A null message buffer means that the frame hasn't really been
6640 initialized yet. Error messages get reported properly by
6641 cmd_error, so this must be just an informative message; toss it. */
6642 else if (INTERACTIVE
6643 && sf->glyphs_initialized_p
6644 && FRAME_MESSAGE_BUF (sf))
6646 Lisp_Object mini_window;
6647 struct frame *f;
6649 /* Get the frame containing the mini-buffer
6650 that the selected frame is using. */
6651 mini_window = FRAME_MINIBUF_WINDOW (sf);
6652 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6654 FRAME_SAMPLE_VISIBILITY (f);
6655 if (FRAME_VISIBLE_P (sf)
6656 && ! FRAME_VISIBLE_P (f))
6657 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6659 if (m)
6661 set_message (m, Qnil, nbytes, multibyte);
6662 if (minibuffer_auto_raise)
6663 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6665 else
6666 clear_message (1, 1);
6668 do_pending_window_change (0);
6669 echo_area_display (1);
6670 do_pending_window_change (0);
6671 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6672 (*frame_up_to_date_hook) (f);
6677 /* Display an echo area message M with a specified length of NBYTES
6678 bytes. The string may include null characters. If M is not a
6679 string, clear out any existing message, and let the mini-buffer
6680 text show through. */
6682 void
6683 message3 (m, nbytes, multibyte)
6684 Lisp_Object m;
6685 int nbytes;
6686 int multibyte;
6688 struct gcpro gcpro1;
6690 GCPRO1 (m);
6692 /* First flush out any partial line written with print. */
6693 message_log_maybe_newline ();
6694 if (STRINGP (m))
6695 message_dolog (SDATA (m), nbytes, 1, multibyte);
6696 message3_nolog (m, nbytes, multibyte);
6698 UNGCPRO;
6702 /* The non-logging version of message3. */
6704 void
6705 message3_nolog (m, nbytes, multibyte)
6706 Lisp_Object m;
6707 int nbytes, multibyte;
6709 struct frame *sf = SELECTED_FRAME ();
6710 message_enable_multibyte = multibyte;
6712 if (noninteractive)
6714 if (noninteractive_need_newline)
6715 putc ('\n', stderr);
6716 noninteractive_need_newline = 0;
6717 if (STRINGP (m))
6718 fwrite (SDATA (m), nbytes, 1, stderr);
6719 if (cursor_in_echo_area == 0)
6720 fprintf (stderr, "\n");
6721 fflush (stderr);
6723 /* A null message buffer means that the frame hasn't really been
6724 initialized yet. Error messages get reported properly by
6725 cmd_error, so this must be just an informative message; toss it. */
6726 else if (INTERACTIVE
6727 && sf->glyphs_initialized_p
6728 && FRAME_MESSAGE_BUF (sf))
6730 Lisp_Object mini_window;
6731 Lisp_Object frame;
6732 struct frame *f;
6734 /* Get the frame containing the mini-buffer
6735 that the selected frame is using. */
6736 mini_window = FRAME_MINIBUF_WINDOW (sf);
6737 frame = XWINDOW (mini_window)->frame;
6738 f = XFRAME (frame);
6740 FRAME_SAMPLE_VISIBILITY (f);
6741 if (FRAME_VISIBLE_P (sf)
6742 && !FRAME_VISIBLE_P (f))
6743 Fmake_frame_visible (frame);
6745 if (STRINGP (m) && SCHARS (m) > 0)
6747 set_message (NULL, m, nbytes, multibyte);
6748 if (minibuffer_auto_raise)
6749 Fraise_frame (frame);
6751 else
6752 clear_message (1, 1);
6754 do_pending_window_change (0);
6755 echo_area_display (1);
6756 do_pending_window_change (0);
6757 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6758 (*frame_up_to_date_hook) (f);
6763 /* Display a null-terminated echo area message M. If M is 0, clear
6764 out any existing message, and let the mini-buffer text show through.
6766 The buffer M must continue to exist until after the echo area gets
6767 cleared or some other message gets displayed there. Do not pass
6768 text that is stored in a Lisp string. Do not pass text in a buffer
6769 that was alloca'd. */
6771 void
6772 message1 (m)
6773 char *m;
6775 message2 (m, (m ? strlen (m) : 0), 0);
6779 /* The non-logging counterpart of message1. */
6781 void
6782 message1_nolog (m)
6783 char *m;
6785 message2_nolog (m, (m ? strlen (m) : 0), 0);
6788 /* Display a message M which contains a single %s
6789 which gets replaced with STRING. */
6791 void
6792 message_with_string (m, string, log)
6793 char *m;
6794 Lisp_Object string;
6795 int log;
6797 CHECK_STRING (string);
6799 if (noninteractive)
6801 if (m)
6803 if (noninteractive_need_newline)
6804 putc ('\n', stderr);
6805 noninteractive_need_newline = 0;
6806 fprintf (stderr, m, SDATA (string));
6807 if (cursor_in_echo_area == 0)
6808 fprintf (stderr, "\n");
6809 fflush (stderr);
6812 else if (INTERACTIVE)
6814 /* The frame whose minibuffer we're going to display the message on.
6815 It may be larger than the selected frame, so we need
6816 to use its buffer, not the selected frame's buffer. */
6817 Lisp_Object mini_window;
6818 struct frame *f, *sf = SELECTED_FRAME ();
6820 /* Get the frame containing the minibuffer
6821 that the selected frame is using. */
6822 mini_window = FRAME_MINIBUF_WINDOW (sf);
6823 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6825 /* A null message buffer means that the frame hasn't really been
6826 initialized yet. Error messages get reported properly by
6827 cmd_error, so this must be just an informative message; toss it. */
6828 if (FRAME_MESSAGE_BUF (f))
6830 Lisp_Object args[2], message;
6831 struct gcpro gcpro1, gcpro2;
6833 args[0] = build_string (m);
6834 args[1] = message = string;
6835 GCPRO2 (args[0], message);
6836 gcpro1.nvars = 2;
6838 message = Fformat (2, args);
6840 if (log)
6841 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6842 else
6843 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6845 UNGCPRO;
6847 /* Print should start at the beginning of the message
6848 buffer next time. */
6849 message_buf_print = 0;
6855 /* Dump an informative message to the minibuf. If M is 0, clear out
6856 any existing message, and let the mini-buffer text show through. */
6858 /* VARARGS 1 */
6859 void
6860 message (m, a1, a2, a3)
6861 char *m;
6862 EMACS_INT a1, a2, a3;
6864 if (noninteractive)
6866 if (m)
6868 if (noninteractive_need_newline)
6869 putc ('\n', stderr);
6870 noninteractive_need_newline = 0;
6871 fprintf (stderr, m, a1, a2, a3);
6872 if (cursor_in_echo_area == 0)
6873 fprintf (stderr, "\n");
6874 fflush (stderr);
6877 else if (INTERACTIVE)
6879 /* The frame whose mini-buffer we're going to display the message
6880 on. It may be larger than the selected frame, so we need to
6881 use its buffer, not the selected frame's buffer. */
6882 Lisp_Object mini_window;
6883 struct frame *f, *sf = SELECTED_FRAME ();
6885 /* Get the frame containing the mini-buffer
6886 that the selected frame is using. */
6887 mini_window = FRAME_MINIBUF_WINDOW (sf);
6888 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6890 /* A null message buffer means that the frame hasn't really been
6891 initialized yet. Error messages get reported properly by
6892 cmd_error, so this must be just an informative message; toss
6893 it. */
6894 if (FRAME_MESSAGE_BUF (f))
6896 if (m)
6898 int len;
6899 #ifdef NO_ARG_ARRAY
6900 char *a[3];
6901 a[0] = (char *) a1;
6902 a[1] = (char *) a2;
6903 a[2] = (char *) a3;
6905 len = doprnt (FRAME_MESSAGE_BUF (f),
6906 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6907 #else
6908 len = doprnt (FRAME_MESSAGE_BUF (f),
6909 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6910 (char **) &a1);
6911 #endif /* NO_ARG_ARRAY */
6913 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6915 else
6916 message1 (0);
6918 /* Print should start at the beginning of the message
6919 buffer next time. */
6920 message_buf_print = 0;
6926 /* The non-logging version of message. */
6928 void
6929 message_nolog (m, a1, a2, a3)
6930 char *m;
6931 EMACS_INT a1, a2, a3;
6933 Lisp_Object old_log_max;
6934 old_log_max = Vmessage_log_max;
6935 Vmessage_log_max = Qnil;
6936 message (m, a1, a2, a3);
6937 Vmessage_log_max = old_log_max;
6941 /* Display the current message in the current mini-buffer. This is
6942 only called from error handlers in process.c, and is not time
6943 critical. */
6945 void
6946 update_echo_area ()
6948 if (!NILP (echo_area_buffer[0]))
6950 Lisp_Object string;
6951 string = Fcurrent_message ();
6952 message3 (string, SBYTES (string),
6953 !NILP (current_buffer->enable_multibyte_characters));
6958 /* Make sure echo area buffers in `echo_buffers' are live.
6959 If they aren't, make new ones. */
6961 static void
6962 ensure_echo_area_buffers ()
6964 int i;
6966 for (i = 0; i < 2; ++i)
6967 if (!BUFFERP (echo_buffer[i])
6968 || NILP (XBUFFER (echo_buffer[i])->name))
6970 char name[30];
6971 Lisp_Object old_buffer;
6972 int j;
6974 old_buffer = echo_buffer[i];
6975 sprintf (name, " *Echo Area %d*", i);
6976 echo_buffer[i] = Fget_buffer_create (build_string (name));
6977 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6979 for (j = 0; j < 2; ++j)
6980 if (EQ (old_buffer, echo_area_buffer[j]))
6981 echo_area_buffer[j] = echo_buffer[i];
6986 /* Call FN with args A1..A4 with either the current or last displayed
6987 echo_area_buffer as current buffer.
6989 WHICH zero means use the current message buffer
6990 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6991 from echo_buffer[] and clear it.
6993 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6994 suitable buffer from echo_buffer[] and clear it.
6996 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6997 that the current message becomes the last displayed one, make
6998 choose a suitable buffer for echo_area_buffer[0], and clear it.
7000 Value is what FN returns. */
7002 static int
7003 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7004 struct window *w;
7005 int which;
7006 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7007 EMACS_INT a1;
7008 Lisp_Object a2;
7009 EMACS_INT a3, a4;
7011 Lisp_Object buffer;
7012 int this_one, the_other, clear_buffer_p, rc;
7013 int count = SPECPDL_INDEX ();
7015 /* If buffers aren't live, make new ones. */
7016 ensure_echo_area_buffers ();
7018 clear_buffer_p = 0;
7020 if (which == 0)
7021 this_one = 0, the_other = 1;
7022 else if (which > 0)
7023 this_one = 1, the_other = 0;
7024 else
7026 this_one = 0, the_other = 1;
7027 clear_buffer_p = 1;
7029 /* We need a fresh one in case the current echo buffer equals
7030 the one containing the last displayed echo area message. */
7031 if (!NILP (echo_area_buffer[this_one])
7032 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7033 echo_area_buffer[this_one] = Qnil;
7036 /* Choose a suitable buffer from echo_buffer[] is we don't
7037 have one. */
7038 if (NILP (echo_area_buffer[this_one]))
7040 echo_area_buffer[this_one]
7041 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7042 ? echo_buffer[the_other]
7043 : echo_buffer[this_one]);
7044 clear_buffer_p = 1;
7047 buffer = echo_area_buffer[this_one];
7049 /* Don't get confused by reusing the buffer used for echoing
7050 for a different purpose. */
7051 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7052 cancel_echoing ();
7054 record_unwind_protect (unwind_with_echo_area_buffer,
7055 with_echo_area_buffer_unwind_data (w));
7057 /* Make the echo area buffer current. Note that for display
7058 purposes, it is not necessary that the displayed window's buffer
7059 == current_buffer, except for text property lookup. So, let's
7060 only set that buffer temporarily here without doing a full
7061 Fset_window_buffer. We must also change w->pointm, though,
7062 because otherwise an assertions in unshow_buffer fails, and Emacs
7063 aborts. */
7064 set_buffer_internal_1 (XBUFFER (buffer));
7065 if (w)
7067 w->buffer = buffer;
7068 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7071 current_buffer->undo_list = Qt;
7072 current_buffer->read_only = Qnil;
7073 specbind (Qinhibit_read_only, Qt);
7074 specbind (Qinhibit_modification_hooks, Qt);
7076 if (clear_buffer_p && Z > BEG)
7077 del_range (BEG, Z);
7079 xassert (BEGV >= BEG);
7080 xassert (ZV <= Z && ZV >= BEGV);
7082 rc = fn (a1, a2, a3, a4);
7084 xassert (BEGV >= BEG);
7085 xassert (ZV <= Z && ZV >= BEGV);
7087 unbind_to (count, Qnil);
7088 return rc;
7092 /* Save state that should be preserved around the call to the function
7093 FN called in with_echo_area_buffer. */
7095 static Lisp_Object
7096 with_echo_area_buffer_unwind_data (w)
7097 struct window *w;
7099 int i = 0;
7100 Lisp_Object vector;
7102 /* Reduce consing by keeping one vector in
7103 Vwith_echo_area_save_vector. */
7104 vector = Vwith_echo_area_save_vector;
7105 Vwith_echo_area_save_vector = Qnil;
7107 if (NILP (vector))
7108 vector = Fmake_vector (make_number (7), Qnil);
7110 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7111 AREF (vector, i) = Vdeactivate_mark, ++i;
7112 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7114 if (w)
7116 XSETWINDOW (AREF (vector, i), w); ++i;
7117 AREF (vector, i) = w->buffer; ++i;
7118 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7119 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7121 else
7123 int end = i + 4;
7124 for (; i < end; ++i)
7125 AREF (vector, i) = Qnil;
7128 xassert (i == ASIZE (vector));
7129 return vector;
7133 /* Restore global state from VECTOR which was created by
7134 with_echo_area_buffer_unwind_data. */
7136 static Lisp_Object
7137 unwind_with_echo_area_buffer (vector)
7138 Lisp_Object vector;
7140 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7141 Vdeactivate_mark = AREF (vector, 1);
7142 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7144 if (WINDOWP (AREF (vector, 3)))
7146 struct window *w;
7147 Lisp_Object buffer, charpos, bytepos;
7149 w = XWINDOW (AREF (vector, 3));
7150 buffer = AREF (vector, 4);
7151 charpos = AREF (vector, 5);
7152 bytepos = AREF (vector, 6);
7154 w->buffer = buffer;
7155 set_marker_both (w->pointm, buffer,
7156 XFASTINT (charpos), XFASTINT (bytepos));
7159 Vwith_echo_area_save_vector = vector;
7160 return Qnil;
7164 /* Set up the echo area for use by print functions. MULTIBYTE_P
7165 non-zero means we will print multibyte. */
7167 void
7168 setup_echo_area_for_printing (multibyte_p)
7169 int multibyte_p;
7171 /* If we can't find an echo area any more, exit. */
7172 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7173 Fkill_emacs (Qnil);
7175 ensure_echo_area_buffers ();
7177 if (!message_buf_print)
7179 /* A message has been output since the last time we printed.
7180 Choose a fresh echo area buffer. */
7181 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7182 echo_area_buffer[0] = echo_buffer[1];
7183 else
7184 echo_area_buffer[0] = echo_buffer[0];
7186 /* Switch to that buffer and clear it. */
7187 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7188 current_buffer->truncate_lines = Qnil;
7190 if (Z > BEG)
7192 int count = SPECPDL_INDEX ();
7193 specbind (Qinhibit_read_only, Qt);
7194 /* Note that undo recording is always disabled. */
7195 del_range (BEG, Z);
7196 unbind_to (count, Qnil);
7198 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7200 /* Set up the buffer for the multibyteness we need. */
7201 if (multibyte_p
7202 != !NILP (current_buffer->enable_multibyte_characters))
7203 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7205 /* Raise the frame containing the echo area. */
7206 if (minibuffer_auto_raise)
7208 struct frame *sf = SELECTED_FRAME ();
7209 Lisp_Object mini_window;
7210 mini_window = FRAME_MINIBUF_WINDOW (sf);
7211 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7214 message_log_maybe_newline ();
7215 message_buf_print = 1;
7217 else
7219 if (NILP (echo_area_buffer[0]))
7221 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7222 echo_area_buffer[0] = echo_buffer[1];
7223 else
7224 echo_area_buffer[0] = echo_buffer[0];
7227 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7229 /* Someone switched buffers between print requests. */
7230 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7231 current_buffer->truncate_lines = Qnil;
7237 /* Display an echo area message in window W. Value is non-zero if W's
7238 height is changed. If display_last_displayed_message_p is
7239 non-zero, display the message that was last displayed, otherwise
7240 display the current message. */
7242 static int
7243 display_echo_area (w)
7244 struct window *w;
7246 int i, no_message_p, window_height_changed_p, count;
7248 /* Temporarily disable garbage collections while displaying the echo
7249 area. This is done because a GC can print a message itself.
7250 That message would modify the echo area buffer's contents while a
7251 redisplay of the buffer is going on, and seriously confuse
7252 redisplay. */
7253 count = inhibit_garbage_collection ();
7255 /* If there is no message, we must call display_echo_area_1
7256 nevertheless because it resizes the window. But we will have to
7257 reset the echo_area_buffer in question to nil at the end because
7258 with_echo_area_buffer will sets it to an empty buffer. */
7259 i = display_last_displayed_message_p ? 1 : 0;
7260 no_message_p = NILP (echo_area_buffer[i]);
7262 window_height_changed_p
7263 = with_echo_area_buffer (w, display_last_displayed_message_p,
7264 display_echo_area_1,
7265 (EMACS_INT) w, Qnil, 0, 0);
7267 if (no_message_p)
7268 echo_area_buffer[i] = Qnil;
7270 unbind_to (count, Qnil);
7271 return window_height_changed_p;
7275 /* Helper for display_echo_area. Display the current buffer which
7276 contains the current echo area message in window W, a mini-window,
7277 a pointer to which is passed in A1. A2..A4 are currently not used.
7278 Change the height of W so that all of the message is displayed.
7279 Value is non-zero if height of W was changed. */
7281 static int
7282 display_echo_area_1 (a1, a2, a3, a4)
7283 EMACS_INT a1;
7284 Lisp_Object a2;
7285 EMACS_INT a3, a4;
7287 struct window *w = (struct window *) a1;
7288 Lisp_Object window;
7289 struct text_pos start;
7290 int window_height_changed_p = 0;
7292 /* Do this before displaying, so that we have a large enough glyph
7293 matrix for the display. */
7294 window_height_changed_p = resize_mini_window (w, 0);
7296 /* Display. */
7297 clear_glyph_matrix (w->desired_matrix);
7298 XSETWINDOW (window, w);
7299 SET_TEXT_POS (start, BEG, BEG_BYTE);
7300 try_window (window, start);
7302 return window_height_changed_p;
7306 /* Resize the echo area window to exactly the size needed for the
7307 currently displayed message, if there is one. If a mini-buffer
7308 is active, don't shrink it. */
7310 void
7311 resize_echo_area_exactly ()
7313 if (BUFFERP (echo_area_buffer[0])
7314 && WINDOWP (echo_area_window))
7316 struct window *w = XWINDOW (echo_area_window);
7317 int resized_p;
7318 Lisp_Object resize_exactly;
7320 if (minibuf_level == 0)
7321 resize_exactly = Qt;
7322 else
7323 resize_exactly = Qnil;
7325 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7326 (EMACS_INT) w, resize_exactly, 0, 0);
7327 if (resized_p)
7329 ++windows_or_buffers_changed;
7330 ++update_mode_lines;
7331 redisplay_internal (0);
7337 /* Callback function for with_echo_area_buffer, when used from
7338 resize_echo_area_exactly. A1 contains a pointer to the window to
7339 resize, EXACTLY non-nil means resize the mini-window exactly to the
7340 size of the text displayed. A3 and A4 are not used. Value is what
7341 resize_mini_window returns. */
7343 static int
7344 resize_mini_window_1 (a1, exactly, a3, a4)
7345 EMACS_INT a1;
7346 Lisp_Object exactly;
7347 EMACS_INT a3, a4;
7349 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7353 /* Resize mini-window W to fit the size of its contents. EXACT:P
7354 means size the window exactly to the size needed. Otherwise, it's
7355 only enlarged until W's buffer is empty. Value is non-zero if
7356 the window height has been changed. */
7359 resize_mini_window (w, exact_p)
7360 struct window *w;
7361 int exact_p;
7363 struct frame *f = XFRAME (w->frame);
7364 int window_height_changed_p = 0;
7366 xassert (MINI_WINDOW_P (w));
7368 /* Don't resize windows while redisplaying a window; it would
7369 confuse redisplay functions when the size of the window they are
7370 displaying changes from under them. Such a resizing can happen,
7371 for instance, when which-func prints a long message while
7372 we are running fontification-functions. We're running these
7373 functions with safe_call which binds inhibit-redisplay to t. */
7374 if (!NILP (Vinhibit_redisplay))
7375 return 0;
7377 /* Nil means don't try to resize. */
7378 if (NILP (Vresize_mini_windows)
7379 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7380 return 0;
7382 if (!FRAME_MINIBUF_ONLY_P (f))
7384 struct it it;
7385 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7386 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7387 int height, max_height;
7388 int unit = FRAME_LINE_HEIGHT (f);
7389 struct text_pos start;
7390 struct buffer *old_current_buffer = NULL;
7392 if (current_buffer != XBUFFER (w->buffer))
7394 old_current_buffer = current_buffer;
7395 set_buffer_internal (XBUFFER (w->buffer));
7398 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7400 /* Compute the max. number of lines specified by the user. */
7401 if (FLOATP (Vmax_mini_window_height))
7402 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7403 else if (INTEGERP (Vmax_mini_window_height))
7404 max_height = XINT (Vmax_mini_window_height);
7405 else
7406 max_height = total_height / 4;
7408 /* Correct that max. height if it's bogus. */
7409 max_height = max (1, max_height);
7410 max_height = min (total_height, max_height);
7412 /* Find out the height of the text in the window. */
7413 if (it.truncate_lines_p)
7414 height = 1;
7415 else
7417 last_height = 0;
7418 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7419 if (it.max_ascent == 0 && it.max_descent == 0)
7420 height = it.current_y + last_height;
7421 else
7422 height = it.current_y + it.max_ascent + it.max_descent;
7423 height -= it.extra_line_spacing;
7424 height = (height + unit - 1) / unit;
7427 /* Compute a suitable window start. */
7428 if (height > max_height)
7430 height = max_height;
7431 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7432 move_it_vertically_backward (&it, (height - 1) * unit);
7433 start = it.current.pos;
7435 else
7436 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7437 SET_MARKER_FROM_TEXT_POS (w->start, start);
7439 if (EQ (Vresize_mini_windows, Qgrow_only))
7441 /* Let it grow only, until we display an empty message, in which
7442 case the window shrinks again. */
7443 if (height > WINDOW_TOTAL_LINES (w))
7445 int old_height = WINDOW_TOTAL_LINES (w);
7446 freeze_window_starts (f, 1);
7447 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7448 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7450 else if (height < WINDOW_TOTAL_LINES (w)
7451 && (exact_p || BEGV == ZV))
7453 int old_height = WINDOW_TOTAL_LINES (w);
7454 freeze_window_starts (f, 0);
7455 shrink_mini_window (w);
7456 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7459 else
7461 /* Always resize to exact size needed. */
7462 if (height > WINDOW_TOTAL_LINES (w))
7464 int old_height = WINDOW_TOTAL_LINES (w);
7465 freeze_window_starts (f, 1);
7466 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7467 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7469 else if (height < WINDOW_TOTAL_LINES (w))
7471 int old_height = WINDOW_TOTAL_LINES (w);
7472 freeze_window_starts (f, 0);
7473 shrink_mini_window (w);
7475 if (height)
7477 freeze_window_starts (f, 1);
7478 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7481 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7485 if (old_current_buffer)
7486 set_buffer_internal (old_current_buffer);
7489 return window_height_changed_p;
7493 /* Value is the current message, a string, or nil if there is no
7494 current message. */
7496 Lisp_Object
7497 current_message ()
7499 Lisp_Object msg;
7501 if (NILP (echo_area_buffer[0]))
7502 msg = Qnil;
7503 else
7505 with_echo_area_buffer (0, 0, current_message_1,
7506 (EMACS_INT) &msg, Qnil, 0, 0);
7507 if (NILP (msg))
7508 echo_area_buffer[0] = Qnil;
7511 return msg;
7515 static int
7516 current_message_1 (a1, a2, a3, a4)
7517 EMACS_INT a1;
7518 Lisp_Object a2;
7519 EMACS_INT a3, a4;
7521 Lisp_Object *msg = (Lisp_Object *) a1;
7523 if (Z > BEG)
7524 *msg = make_buffer_string (BEG, Z, 1);
7525 else
7526 *msg = Qnil;
7527 return 0;
7531 /* Push the current message on Vmessage_stack for later restauration
7532 by restore_message. Value is non-zero if the current message isn't
7533 empty. This is a relatively infrequent operation, so it's not
7534 worth optimizing. */
7537 push_message ()
7539 Lisp_Object msg;
7540 msg = current_message ();
7541 Vmessage_stack = Fcons (msg, Vmessage_stack);
7542 return STRINGP (msg);
7546 /* Restore message display from the top of Vmessage_stack. */
7548 void
7549 restore_message ()
7551 Lisp_Object msg;
7553 xassert (CONSP (Vmessage_stack));
7554 msg = XCAR (Vmessage_stack);
7555 if (STRINGP (msg))
7556 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7557 else
7558 message3_nolog (msg, 0, 0);
7562 /* Handler for record_unwind_protect calling pop_message. */
7564 Lisp_Object
7565 pop_message_unwind (dummy)
7566 Lisp_Object dummy;
7568 pop_message ();
7569 return Qnil;
7572 /* Pop the top-most entry off Vmessage_stack. */
7574 void
7575 pop_message ()
7577 xassert (CONSP (Vmessage_stack));
7578 Vmessage_stack = XCDR (Vmessage_stack);
7582 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7583 exits. If the stack is not empty, we have a missing pop_message
7584 somewhere. */
7586 void
7587 check_message_stack ()
7589 if (!NILP (Vmessage_stack))
7590 abort ();
7594 /* Truncate to NCHARS what will be displayed in the echo area the next
7595 time we display it---but don't redisplay it now. */
7597 void
7598 truncate_echo_area (nchars)
7599 int nchars;
7601 if (nchars == 0)
7602 echo_area_buffer[0] = Qnil;
7603 /* A null message buffer means that the frame hasn't really been
7604 initialized yet. Error messages get reported properly by
7605 cmd_error, so this must be just an informative message; toss it. */
7606 else if (!noninteractive
7607 && INTERACTIVE
7608 && !NILP (echo_area_buffer[0]))
7610 struct frame *sf = SELECTED_FRAME ();
7611 if (FRAME_MESSAGE_BUF (sf))
7612 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7617 /* Helper function for truncate_echo_area. Truncate the current
7618 message to at most NCHARS characters. */
7620 static int
7621 truncate_message_1 (nchars, a2, a3, a4)
7622 EMACS_INT nchars;
7623 Lisp_Object a2;
7624 EMACS_INT a3, a4;
7626 if (BEG + nchars < Z)
7627 del_range (BEG + nchars, Z);
7628 if (Z == BEG)
7629 echo_area_buffer[0] = Qnil;
7630 return 0;
7634 /* Set the current message to a substring of S or STRING.
7636 If STRING is a Lisp string, set the message to the first NBYTES
7637 bytes from STRING. NBYTES zero means use the whole string. If
7638 STRING is multibyte, the message will be displayed multibyte.
7640 If S is not null, set the message to the first LEN bytes of S. LEN
7641 zero means use the whole string. MULTIBYTE_P non-zero means S is
7642 multibyte. Display the message multibyte in that case. */
7644 void
7645 set_message (s, string, nbytes, multibyte_p)
7646 const char *s;
7647 Lisp_Object string;
7648 int nbytes, multibyte_p;
7650 message_enable_multibyte
7651 = ((s && multibyte_p)
7652 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7654 with_echo_area_buffer (0, -1, set_message_1,
7655 (EMACS_INT) s, string, nbytes, multibyte_p);
7656 message_buf_print = 0;
7657 help_echo_showing_p = 0;
7661 /* Helper function for set_message. Arguments have the same meaning
7662 as there, with A1 corresponding to S and A2 corresponding to STRING
7663 This function is called with the echo area buffer being
7664 current. */
7666 static int
7667 set_message_1 (a1, a2, nbytes, multibyte_p)
7668 EMACS_INT a1;
7669 Lisp_Object a2;
7670 EMACS_INT nbytes, multibyte_p;
7672 const char *s = (const char *) a1;
7673 Lisp_Object string = a2;
7675 xassert (BEG == Z);
7677 /* Change multibyteness of the echo buffer appropriately. */
7678 if (message_enable_multibyte
7679 != !NILP (current_buffer->enable_multibyte_characters))
7680 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7682 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7684 /* Insert new message at BEG. */
7685 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7687 if (STRINGP (string))
7689 int nchars;
7691 if (nbytes == 0)
7692 nbytes = SBYTES (string);
7693 nchars = string_byte_to_char (string, nbytes);
7695 /* This function takes care of single/multibyte conversion. We
7696 just have to ensure that the echo area buffer has the right
7697 setting of enable_multibyte_characters. */
7698 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7700 else if (s)
7702 if (nbytes == 0)
7703 nbytes = strlen (s);
7705 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7707 /* Convert from multi-byte to single-byte. */
7708 int i, c, n;
7709 unsigned char work[1];
7711 /* Convert a multibyte string to single-byte. */
7712 for (i = 0; i < nbytes; i += n)
7714 c = string_char_and_length (s + i, nbytes - i, &n);
7715 work[0] = (SINGLE_BYTE_CHAR_P (c)
7717 : multibyte_char_to_unibyte (c, Qnil));
7718 insert_1_both (work, 1, 1, 1, 0, 0);
7721 else if (!multibyte_p
7722 && !NILP (current_buffer->enable_multibyte_characters))
7724 /* Convert from single-byte to multi-byte. */
7725 int i, c, n;
7726 const unsigned char *msg = (const unsigned char *) s;
7727 unsigned char str[MAX_MULTIBYTE_LENGTH];
7729 /* Convert a single-byte string to multibyte. */
7730 for (i = 0; i < nbytes; i++)
7732 c = unibyte_char_to_multibyte (msg[i]);
7733 n = CHAR_STRING (c, str);
7734 insert_1_both (str, 1, n, 1, 0, 0);
7737 else
7738 insert_1 (s, nbytes, 1, 0, 0);
7741 return 0;
7745 /* Clear messages. CURRENT_P non-zero means clear the current
7746 message. LAST_DISPLAYED_P non-zero means clear the message
7747 last displayed. */
7749 void
7750 clear_message (current_p, last_displayed_p)
7751 int current_p, last_displayed_p;
7753 if (current_p)
7755 echo_area_buffer[0] = Qnil;
7756 message_cleared_p = 1;
7759 if (last_displayed_p)
7760 echo_area_buffer[1] = Qnil;
7762 message_buf_print = 0;
7765 /* Clear garbaged frames.
7767 This function is used where the old redisplay called
7768 redraw_garbaged_frames which in turn called redraw_frame which in
7769 turn called clear_frame. The call to clear_frame was a source of
7770 flickering. I believe a clear_frame is not necessary. It should
7771 suffice in the new redisplay to invalidate all current matrices,
7772 and ensure a complete redisplay of all windows. */
7774 static void
7775 clear_garbaged_frames ()
7777 if (frame_garbaged)
7779 Lisp_Object tail, frame;
7780 int changed_count = 0;
7782 FOR_EACH_FRAME (tail, frame)
7784 struct frame *f = XFRAME (frame);
7786 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7788 if (f->resized_p)
7790 Fredraw_frame (frame);
7791 f->force_flush_display_p = 1;
7793 clear_current_matrices (f);
7794 changed_count++;
7795 f->garbaged = 0;
7796 f->resized_p = 0;
7800 frame_garbaged = 0;
7801 if (changed_count)
7802 ++windows_or_buffers_changed;
7807 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7808 is non-zero update selected_frame. Value is non-zero if the
7809 mini-windows height has been changed. */
7811 static int
7812 echo_area_display (update_frame_p)
7813 int update_frame_p;
7815 Lisp_Object mini_window;
7816 struct window *w;
7817 struct frame *f;
7818 int window_height_changed_p = 0;
7819 struct frame *sf = SELECTED_FRAME ();
7821 mini_window = FRAME_MINIBUF_WINDOW (sf);
7822 w = XWINDOW (mini_window);
7823 f = XFRAME (WINDOW_FRAME (w));
7825 /* Don't display if frame is invisible or not yet initialized. */
7826 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7827 return 0;
7829 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7830 #ifndef MAC_OS8
7831 #ifdef HAVE_WINDOW_SYSTEM
7832 /* When Emacs starts, selected_frame may be a visible terminal
7833 frame, even if we run under a window system. If we let this
7834 through, a message would be displayed on the terminal. */
7835 if (EQ (selected_frame, Vterminal_frame)
7836 && !NILP (Vwindow_system))
7837 return 0;
7838 #endif /* HAVE_WINDOW_SYSTEM */
7839 #endif
7841 /* Redraw garbaged frames. */
7842 if (frame_garbaged)
7843 clear_garbaged_frames ();
7845 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7847 echo_area_window = mini_window;
7848 window_height_changed_p = display_echo_area (w);
7849 w->must_be_updated_p = 1;
7851 /* Update the display, unless called from redisplay_internal.
7852 Also don't update the screen during redisplay itself. The
7853 update will happen at the end of redisplay, and an update
7854 here could cause confusion. */
7855 if (update_frame_p && !redisplaying_p)
7857 int n = 0;
7859 /* If the display update has been interrupted by pending
7860 input, update mode lines in the frame. Due to the
7861 pending input, it might have been that redisplay hasn't
7862 been called, so that mode lines above the echo area are
7863 garbaged. This looks odd, so we prevent it here. */
7864 if (!display_completed)
7865 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7867 if (window_height_changed_p
7868 /* Don't do this if Emacs is shutting down. Redisplay
7869 needs to run hooks. */
7870 && !NILP (Vrun_hooks))
7872 /* Must update other windows. Likewise as in other
7873 cases, don't let this update be interrupted by
7874 pending input. */
7875 int count = SPECPDL_INDEX ();
7876 specbind (Qredisplay_dont_pause, Qt);
7877 windows_or_buffers_changed = 1;
7878 redisplay_internal (0);
7879 unbind_to (count, Qnil);
7881 else if (FRAME_WINDOW_P (f) && n == 0)
7883 /* Window configuration is the same as before.
7884 Can do with a display update of the echo area,
7885 unless we displayed some mode lines. */
7886 update_single_window (w, 1);
7887 rif->flush_display (f);
7889 else
7890 update_frame (f, 1, 1);
7892 /* If cursor is in the echo area, make sure that the next
7893 redisplay displays the minibuffer, so that the cursor will
7894 be replaced with what the minibuffer wants. */
7895 if (cursor_in_echo_area)
7896 ++windows_or_buffers_changed;
7899 else if (!EQ (mini_window, selected_window))
7900 windows_or_buffers_changed++;
7902 /* Last displayed message is now the current message. */
7903 echo_area_buffer[1] = echo_area_buffer[0];
7905 /* Prevent redisplay optimization in redisplay_internal by resetting
7906 this_line_start_pos. This is done because the mini-buffer now
7907 displays the message instead of its buffer text. */
7908 if (EQ (mini_window, selected_window))
7909 CHARPOS (this_line_start_pos) = 0;
7911 return window_height_changed_p;
7916 /***********************************************************************
7917 Frame Titles
7918 ***********************************************************************/
7921 /* The frame title buffering code is also used by Fformat_mode_line.
7922 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7924 /* A buffer for constructing frame titles in it; allocated from the
7925 heap in init_xdisp and resized as needed in store_frame_title_char. */
7927 static char *frame_title_buf;
7929 /* The buffer's end, and a current output position in it. */
7931 static char *frame_title_buf_end;
7932 static char *frame_title_ptr;
7935 /* Store a single character C for the frame title in frame_title_buf.
7936 Re-allocate frame_title_buf if necessary. */
7938 static void
7939 #ifdef PROTOTYPES
7940 store_frame_title_char (char c)
7941 #else
7942 store_frame_title_char (c)
7943 char c;
7944 #endif
7946 /* If output position has reached the end of the allocated buffer,
7947 double the buffer's size. */
7948 if (frame_title_ptr == frame_title_buf_end)
7950 int len = frame_title_ptr - frame_title_buf;
7951 int new_size = 2 * len * sizeof *frame_title_buf;
7952 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7953 frame_title_buf_end = frame_title_buf + new_size;
7954 frame_title_ptr = frame_title_buf + len;
7957 *frame_title_ptr++ = c;
7961 /* Store part of a frame title in frame_title_buf, beginning at
7962 frame_title_ptr. STR is the string to store. Do not copy
7963 characters that yield more columns than PRECISION; PRECISION <= 0
7964 means copy the whole string. Pad with spaces until FIELD_WIDTH
7965 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7966 pad. Called from display_mode_element when it is used to build a
7967 frame title. */
7969 static int
7970 store_frame_title (str, field_width, precision)
7971 const unsigned char *str;
7972 int field_width, precision;
7974 int n = 0;
7975 int dummy, nbytes;
7977 /* Copy at most PRECISION chars from STR. */
7978 nbytes = strlen (str);
7979 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
7980 while (nbytes--)
7981 store_frame_title_char (*str++);
7983 /* Fill up with spaces until FIELD_WIDTH reached. */
7984 while (field_width > 0
7985 && n < field_width)
7987 store_frame_title_char (' ');
7988 ++n;
7991 return n;
7994 #ifdef HAVE_WINDOW_SYSTEM
7996 /* Set the title of FRAME, if it has changed. The title format is
7997 Vicon_title_format if FRAME is iconified, otherwise it is
7998 frame_title_format. */
8000 static void
8001 x_consider_frame_title (frame)
8002 Lisp_Object frame;
8004 struct frame *f = XFRAME (frame);
8006 if (FRAME_WINDOW_P (f)
8007 || FRAME_MINIBUF_ONLY_P (f)
8008 || f->explicit_name)
8010 /* Do we have more than one visible frame on this X display? */
8011 Lisp_Object tail;
8012 Lisp_Object fmt;
8013 struct buffer *obuf;
8014 int len;
8015 struct it it;
8017 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8019 Lisp_Object other_frame = XCAR (tail);
8020 struct frame *tf = XFRAME (other_frame);
8022 if (tf != f
8023 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8024 && !FRAME_MINIBUF_ONLY_P (tf)
8025 && !EQ (other_frame, tip_frame)
8026 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8027 break;
8030 /* Set global variable indicating that multiple frames exist. */
8031 multiple_frames = CONSP (tail);
8033 /* Switch to the buffer of selected window of the frame. Set up
8034 frame_title_ptr so that display_mode_element will output into it;
8035 then display the title. */
8036 obuf = current_buffer;
8037 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8038 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8039 frame_title_ptr = frame_title_buf;
8040 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8041 NULL, DEFAULT_FACE_ID);
8042 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8043 len = frame_title_ptr - frame_title_buf;
8044 frame_title_ptr = NULL;
8045 set_buffer_internal_1 (obuf);
8047 /* Set the title only if it's changed. This avoids consing in
8048 the common case where it hasn't. (If it turns out that we've
8049 already wasted too much time by walking through the list with
8050 display_mode_element, then we might need to optimize at a
8051 higher level than this.) */
8052 if (! STRINGP (f->name)
8053 || SBYTES (f->name) != len
8054 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8055 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8059 #endif /* not HAVE_WINDOW_SYSTEM */
8064 /***********************************************************************
8065 Menu Bars
8066 ***********************************************************************/
8069 /* Prepare for redisplay by updating menu-bar item lists when
8070 appropriate. This can call eval. */
8072 void
8073 prepare_menu_bars ()
8075 int all_windows;
8076 struct gcpro gcpro1, gcpro2;
8077 struct frame *f;
8078 Lisp_Object tooltip_frame;
8080 #ifdef HAVE_WINDOW_SYSTEM
8081 tooltip_frame = tip_frame;
8082 #else
8083 tooltip_frame = Qnil;
8084 #endif
8086 /* Update all frame titles based on their buffer names, etc. We do
8087 this before the menu bars so that the buffer-menu will show the
8088 up-to-date frame titles. */
8089 #ifdef HAVE_WINDOW_SYSTEM
8090 if (windows_or_buffers_changed || update_mode_lines)
8092 Lisp_Object tail, frame;
8094 FOR_EACH_FRAME (tail, frame)
8096 f = XFRAME (frame);
8097 if (!EQ (frame, tooltip_frame)
8098 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8099 x_consider_frame_title (frame);
8102 #endif /* HAVE_WINDOW_SYSTEM */
8104 /* Update the menu bar item lists, if appropriate. This has to be
8105 done before any actual redisplay or generation of display lines. */
8106 all_windows = (update_mode_lines
8107 || buffer_shared > 1
8108 || windows_or_buffers_changed);
8109 if (all_windows)
8111 Lisp_Object tail, frame;
8112 int count = SPECPDL_INDEX ();
8114 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8116 FOR_EACH_FRAME (tail, frame)
8118 f = XFRAME (frame);
8120 /* Ignore tooltip frame. */
8121 if (EQ (frame, tooltip_frame))
8122 continue;
8124 /* If a window on this frame changed size, report that to
8125 the user and clear the size-change flag. */
8126 if (FRAME_WINDOW_SIZES_CHANGED (f))
8128 Lisp_Object functions;
8130 /* Clear flag first in case we get an error below. */
8131 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8132 functions = Vwindow_size_change_functions;
8133 GCPRO2 (tail, functions);
8135 while (CONSP (functions))
8137 call1 (XCAR (functions), frame);
8138 functions = XCDR (functions);
8140 UNGCPRO;
8143 GCPRO1 (tail);
8144 update_menu_bar (f, 0);
8145 #ifdef HAVE_WINDOW_SYSTEM
8146 update_tool_bar (f, 0);
8147 #endif
8148 UNGCPRO;
8151 unbind_to (count, Qnil);
8153 else
8155 struct frame *sf = SELECTED_FRAME ();
8156 update_menu_bar (sf, 1);
8157 #ifdef HAVE_WINDOW_SYSTEM
8158 update_tool_bar (sf, 1);
8159 #endif
8162 /* Motif needs this. See comment in xmenu.c. Turn it off when
8163 pending_menu_activation is not defined. */
8164 #ifdef USE_X_TOOLKIT
8165 pending_menu_activation = 0;
8166 #endif
8170 /* Update the menu bar item list for frame F. This has to be done
8171 before we start to fill in any display lines, because it can call
8172 eval.
8174 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8176 static void
8177 update_menu_bar (f, save_match_data)
8178 struct frame *f;
8179 int save_match_data;
8181 Lisp_Object window;
8182 register struct window *w;
8184 /* If called recursively during a menu update, do nothing. This can
8185 happen when, for instance, an activate-menubar-hook causes a
8186 redisplay. */
8187 if (inhibit_menubar_update)
8188 return;
8190 window = FRAME_SELECTED_WINDOW (f);
8191 w = XWINDOW (window);
8193 #if 0 /* The if statement below this if statement used to include the
8194 condition !NILP (w->update_mode_line), rather than using
8195 update_mode_lines directly, and this if statement may have
8196 been added to make that condition work. Now the if
8197 statement below matches its comment, this isn't needed. */
8198 if (update_mode_lines)
8199 w->update_mode_line = Qt;
8200 #endif
8202 if (FRAME_WINDOW_P (f)
8204 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8205 || defined (USE_GTK)
8206 FRAME_EXTERNAL_MENU_BAR (f)
8207 #else
8208 FRAME_MENU_BAR_LINES (f) > 0
8209 #endif
8210 : FRAME_MENU_BAR_LINES (f) > 0)
8212 /* If the user has switched buffers or windows, we need to
8213 recompute to reflect the new bindings. But we'll
8214 recompute when update_mode_lines is set too; that means
8215 that people can use force-mode-line-update to request
8216 that the menu bar be recomputed. The adverse effect on
8217 the rest of the redisplay algorithm is about the same as
8218 windows_or_buffers_changed anyway. */
8219 if (windows_or_buffers_changed
8220 /* This used to test w->update_mode_line, but we believe
8221 there is no need to recompute the menu in that case. */
8222 || update_mode_lines
8223 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8224 < BUF_MODIFF (XBUFFER (w->buffer)))
8225 != !NILP (w->last_had_star))
8226 || ((!NILP (Vtransient_mark_mode)
8227 && !NILP (XBUFFER (w->buffer)->mark_active))
8228 != !NILP (w->region_showing)))
8230 struct buffer *prev = current_buffer;
8231 int count = SPECPDL_INDEX ();
8233 specbind (Qinhibit_menubar_update, Qt);
8235 set_buffer_internal_1 (XBUFFER (w->buffer));
8236 if (save_match_data)
8237 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8238 if (NILP (Voverriding_local_map_menu_flag))
8240 specbind (Qoverriding_terminal_local_map, Qnil);
8241 specbind (Qoverriding_local_map, Qnil);
8244 /* Run the Lucid hook. */
8245 safe_run_hooks (Qactivate_menubar_hook);
8247 /* If it has changed current-menubar from previous value,
8248 really recompute the menu-bar from the value. */
8249 if (! NILP (Vlucid_menu_bar_dirty_flag))
8250 call0 (Qrecompute_lucid_menubar);
8252 safe_run_hooks (Qmenu_bar_update_hook);
8253 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8255 /* Redisplay the menu bar in case we changed it. */
8256 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8257 || defined (USE_GTK)
8258 if (FRAME_WINDOW_P (f)
8259 #if defined (MAC_OS)
8260 /* All frames on Mac OS share the same menubar. So only the
8261 selected frame should be allowed to set it. */
8262 && f == SELECTED_FRAME ()
8263 #endif
8265 set_frame_menubar (f, 0, 0);
8266 else
8267 /* On a terminal screen, the menu bar is an ordinary screen
8268 line, and this makes it get updated. */
8269 w->update_mode_line = Qt;
8270 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8271 /* In the non-toolkit version, the menu bar is an ordinary screen
8272 line, and this makes it get updated. */
8273 w->update_mode_line = Qt;
8274 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8276 unbind_to (count, Qnil);
8277 set_buffer_internal_1 (prev);
8284 /***********************************************************************
8285 Output Cursor
8286 ***********************************************************************/
8288 #ifdef HAVE_WINDOW_SYSTEM
8290 /* EXPORT:
8291 Nominal cursor position -- where to draw output.
8292 HPOS and VPOS are window relative glyph matrix coordinates.
8293 X and Y are window relative pixel coordinates. */
8295 struct cursor_pos output_cursor;
8298 /* EXPORT:
8299 Set the global variable output_cursor to CURSOR. All cursor
8300 positions are relative to updated_window. */
8302 void
8303 set_output_cursor (cursor)
8304 struct cursor_pos *cursor;
8306 output_cursor.hpos = cursor->hpos;
8307 output_cursor.vpos = cursor->vpos;
8308 output_cursor.x = cursor->x;
8309 output_cursor.y = cursor->y;
8313 /* EXPORT for RIF:
8314 Set a nominal cursor position.
8316 HPOS and VPOS are column/row positions in a window glyph matrix. X
8317 and Y are window text area relative pixel positions.
8319 If this is done during an update, updated_window will contain the
8320 window that is being updated and the position is the future output
8321 cursor position for that window. If updated_window is null, use
8322 selected_window and display the cursor at the given position. */
8324 void
8325 x_cursor_to (vpos, hpos, y, x)
8326 int vpos, hpos, y, x;
8328 struct window *w;
8330 /* If updated_window is not set, work on selected_window. */
8331 if (updated_window)
8332 w = updated_window;
8333 else
8334 w = XWINDOW (selected_window);
8336 /* Set the output cursor. */
8337 output_cursor.hpos = hpos;
8338 output_cursor.vpos = vpos;
8339 output_cursor.x = x;
8340 output_cursor.y = y;
8342 /* If not called as part of an update, really display the cursor.
8343 This will also set the cursor position of W. */
8344 if (updated_window == NULL)
8346 BLOCK_INPUT;
8347 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8348 if (rif->flush_display_optional)
8349 rif->flush_display_optional (SELECTED_FRAME ());
8350 UNBLOCK_INPUT;
8354 #endif /* HAVE_WINDOW_SYSTEM */
8357 /***********************************************************************
8358 Tool-bars
8359 ***********************************************************************/
8361 #ifdef HAVE_WINDOW_SYSTEM
8363 /* Where the mouse was last time we reported a mouse event. */
8365 FRAME_PTR last_mouse_frame;
8367 /* Tool-bar item index of the item on which a mouse button was pressed
8368 or -1. */
8370 int last_tool_bar_item;
8373 /* Update the tool-bar item list for frame F. This has to be done
8374 before we start to fill in any display lines. Called from
8375 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8376 and restore it here. */
8378 static void
8379 update_tool_bar (f, save_match_data)
8380 struct frame *f;
8381 int save_match_data;
8383 #ifdef USE_GTK
8384 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8385 #else
8386 int do_update = WINDOWP (f->tool_bar_window)
8387 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8388 #endif
8390 if (do_update)
8392 Lisp_Object window;
8393 struct window *w;
8395 window = FRAME_SELECTED_WINDOW (f);
8396 w = XWINDOW (window);
8398 /* If the user has switched buffers or windows, we need to
8399 recompute to reflect the new bindings. But we'll
8400 recompute when update_mode_lines is set too; that means
8401 that people can use force-mode-line-update to request
8402 that the menu bar be recomputed. The adverse effect on
8403 the rest of the redisplay algorithm is about the same as
8404 windows_or_buffers_changed anyway. */
8405 if (windows_or_buffers_changed
8406 || !NILP (w->update_mode_line)
8407 || update_mode_lines
8408 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8409 < BUF_MODIFF (XBUFFER (w->buffer)))
8410 != !NILP (w->last_had_star))
8411 || ((!NILP (Vtransient_mark_mode)
8412 && !NILP (XBUFFER (w->buffer)->mark_active))
8413 != !NILP (w->region_showing)))
8415 struct buffer *prev = current_buffer;
8416 int count = SPECPDL_INDEX ();
8417 Lisp_Object old_tool_bar;
8418 struct gcpro gcpro1;
8420 /* Set current_buffer to the buffer of the selected
8421 window of the frame, so that we get the right local
8422 keymaps. */
8423 set_buffer_internal_1 (XBUFFER (w->buffer));
8425 /* Save match data, if we must. */
8426 if (save_match_data)
8427 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8429 /* Make sure that we don't accidentally use bogus keymaps. */
8430 if (NILP (Voverriding_local_map_menu_flag))
8432 specbind (Qoverriding_terminal_local_map, Qnil);
8433 specbind (Qoverriding_local_map, Qnil);
8436 old_tool_bar = f->tool_bar_items;
8437 GCPRO1 (old_tool_bar);
8439 /* Build desired tool-bar items from keymaps. */
8440 BLOCK_INPUT;
8441 f->tool_bar_items
8442 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8443 UNBLOCK_INPUT;
8445 /* Redisplay the tool-bar if we changed it. */
8446 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8447 w->update_mode_line = Qt;
8449 UNGCPRO;
8451 unbind_to (count, Qnil);
8452 set_buffer_internal_1 (prev);
8458 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8459 F's desired tool-bar contents. F->tool_bar_items must have
8460 been set up previously by calling prepare_menu_bars. */
8462 static void
8463 build_desired_tool_bar_string (f)
8464 struct frame *f;
8466 int i, size, size_needed;
8467 struct gcpro gcpro1, gcpro2, gcpro3;
8468 Lisp_Object image, plist, props;
8470 image = plist = props = Qnil;
8471 GCPRO3 (image, plist, props);
8473 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8474 Otherwise, make a new string. */
8476 /* The size of the string we might be able to reuse. */
8477 size = (STRINGP (f->desired_tool_bar_string)
8478 ? SCHARS (f->desired_tool_bar_string)
8479 : 0);
8481 /* We need one space in the string for each image. */
8482 size_needed = f->n_tool_bar_items;
8484 /* Reuse f->desired_tool_bar_string, if possible. */
8485 if (size < size_needed || NILP (f->desired_tool_bar_string))
8486 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8487 make_number (' '));
8488 else
8490 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8491 Fremove_text_properties (make_number (0), make_number (size),
8492 props, f->desired_tool_bar_string);
8495 /* Put a `display' property on the string for the images to display,
8496 put a `menu_item' property on tool-bar items with a value that
8497 is the index of the item in F's tool-bar item vector. */
8498 for (i = 0; i < f->n_tool_bar_items; ++i)
8500 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8502 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8503 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8504 int hmargin, vmargin, relief, idx, end;
8505 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8507 /* If image is a vector, choose the image according to the
8508 button state. */
8509 image = PROP (TOOL_BAR_ITEM_IMAGES);
8510 if (VECTORP (image))
8512 if (enabled_p)
8513 idx = (selected_p
8514 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8515 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8516 else
8517 idx = (selected_p
8518 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8519 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8521 xassert (ASIZE (image) >= idx);
8522 image = AREF (image, idx);
8524 else
8525 idx = -1;
8527 /* Ignore invalid image specifications. */
8528 if (!valid_image_p (image))
8529 continue;
8531 /* Display the tool-bar button pressed, or depressed. */
8532 plist = Fcopy_sequence (XCDR (image));
8534 /* Compute margin and relief to draw. */
8535 relief = (tool_bar_button_relief >= 0
8536 ? tool_bar_button_relief
8537 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8538 hmargin = vmargin = relief;
8540 if (INTEGERP (Vtool_bar_button_margin)
8541 && XINT (Vtool_bar_button_margin) > 0)
8543 hmargin += XFASTINT (Vtool_bar_button_margin);
8544 vmargin += XFASTINT (Vtool_bar_button_margin);
8546 else if (CONSP (Vtool_bar_button_margin))
8548 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8549 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8550 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8552 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8553 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8554 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8557 if (auto_raise_tool_bar_buttons_p)
8559 /* Add a `:relief' property to the image spec if the item is
8560 selected. */
8561 if (selected_p)
8563 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8564 hmargin -= relief;
8565 vmargin -= relief;
8568 else
8570 /* If image is selected, display it pressed, i.e. with a
8571 negative relief. If it's not selected, display it with a
8572 raised relief. */
8573 plist = Fplist_put (plist, QCrelief,
8574 (selected_p
8575 ? make_number (-relief)
8576 : make_number (relief)));
8577 hmargin -= relief;
8578 vmargin -= relief;
8581 /* Put a margin around the image. */
8582 if (hmargin || vmargin)
8584 if (hmargin == vmargin)
8585 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8586 else
8587 plist = Fplist_put (plist, QCmargin,
8588 Fcons (make_number (hmargin),
8589 make_number (vmargin)));
8592 /* If button is not enabled, and we don't have special images
8593 for the disabled state, make the image appear disabled by
8594 applying an appropriate algorithm to it. */
8595 if (!enabled_p && idx < 0)
8596 plist = Fplist_put (plist, QCconversion, Qdisabled);
8598 /* Put a `display' text property on the string for the image to
8599 display. Put a `menu-item' property on the string that gives
8600 the start of this item's properties in the tool-bar items
8601 vector. */
8602 image = Fcons (Qimage, plist);
8603 props = list4 (Qdisplay, image,
8604 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8606 /* Let the last image hide all remaining spaces in the tool bar
8607 string. The string can be longer than needed when we reuse a
8608 previous string. */
8609 if (i + 1 == f->n_tool_bar_items)
8610 end = SCHARS (f->desired_tool_bar_string);
8611 else
8612 end = i + 1;
8613 Fadd_text_properties (make_number (i), make_number (end),
8614 props, f->desired_tool_bar_string);
8615 #undef PROP
8618 UNGCPRO;
8622 /* Display one line of the tool-bar of frame IT->f. */
8624 static void
8625 display_tool_bar_line (it)
8626 struct it *it;
8628 struct glyph_row *row = it->glyph_row;
8629 int max_x = it->last_visible_x;
8630 struct glyph *last;
8632 prepare_desired_row (row);
8633 row->y = it->current_y;
8635 /* Note that this isn't made use of if the face hasn't a box,
8636 so there's no need to check the face here. */
8637 it->start_of_box_run_p = 1;
8639 while (it->current_x < max_x)
8641 int x_before, x, n_glyphs_before, i, nglyphs;
8643 /* Get the next display element. */
8644 if (!get_next_display_element (it))
8645 break;
8647 /* Produce glyphs. */
8648 x_before = it->current_x;
8649 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8650 PRODUCE_GLYPHS (it);
8652 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8653 i = 0;
8654 x = x_before;
8655 while (i < nglyphs)
8657 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8659 if (x + glyph->pixel_width > max_x)
8661 /* Glyph doesn't fit on line. */
8662 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8663 it->current_x = x;
8664 goto out;
8667 ++it->hpos;
8668 x += glyph->pixel_width;
8669 ++i;
8672 /* Stop at line ends. */
8673 if (ITERATOR_AT_END_OF_LINE_P (it))
8674 break;
8676 set_iterator_to_next (it, 1);
8679 out:;
8681 row->displays_text_p = row->used[TEXT_AREA] != 0;
8682 extend_face_to_end_of_line (it);
8683 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8684 last->right_box_line_p = 1;
8685 if (last == row->glyphs[TEXT_AREA])
8686 last->left_box_line_p = 1;
8687 compute_line_metrics (it);
8689 /* If line is empty, make it occupy the rest of the tool-bar. */
8690 if (!row->displays_text_p)
8692 row->height = row->phys_height = it->last_visible_y - row->y;
8693 row->ascent = row->phys_ascent = 0;
8696 row->full_width_p = 1;
8697 row->continued_p = 0;
8698 row->truncated_on_left_p = 0;
8699 row->truncated_on_right_p = 0;
8701 it->current_x = it->hpos = 0;
8702 it->current_y += row->height;
8703 ++it->vpos;
8704 ++it->glyph_row;
8708 /* Value is the number of screen lines needed to make all tool-bar
8709 items of frame F visible. */
8711 static int
8712 tool_bar_lines_needed (f)
8713 struct frame *f;
8715 struct window *w = XWINDOW (f->tool_bar_window);
8716 struct it it;
8718 /* Initialize an iterator for iteration over
8719 F->desired_tool_bar_string in the tool-bar window of frame F. */
8720 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8721 it.first_visible_x = 0;
8722 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8723 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8725 while (!ITERATOR_AT_END_P (&it))
8727 it.glyph_row = w->desired_matrix->rows;
8728 clear_glyph_row (it.glyph_row);
8729 display_tool_bar_line (&it);
8732 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8736 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8737 0, 1, 0,
8738 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8739 (frame)
8740 Lisp_Object frame;
8742 struct frame *f;
8743 struct window *w;
8744 int nlines = 0;
8746 if (NILP (frame))
8747 frame = selected_frame;
8748 else
8749 CHECK_FRAME (frame);
8750 f = XFRAME (frame);
8752 if (WINDOWP (f->tool_bar_window)
8753 || (w = XWINDOW (f->tool_bar_window),
8754 WINDOW_TOTAL_LINES (w) > 0))
8756 update_tool_bar (f, 1);
8757 if (f->n_tool_bar_items)
8759 build_desired_tool_bar_string (f);
8760 nlines = tool_bar_lines_needed (f);
8764 return make_number (nlines);
8768 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8769 height should be changed. */
8771 static int
8772 redisplay_tool_bar (f)
8773 struct frame *f;
8775 struct window *w;
8776 struct it it;
8777 struct glyph_row *row;
8778 int change_height_p = 0;
8780 #ifdef USE_GTK
8781 if (FRAME_EXTERNAL_TOOL_BAR (f))
8782 update_frame_tool_bar (f);
8783 return 0;
8784 #endif
8786 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8787 do anything. This means you must start with tool-bar-lines
8788 non-zero to get the auto-sizing effect. Or in other words, you
8789 can turn off tool-bars by specifying tool-bar-lines zero. */
8790 if (!WINDOWP (f->tool_bar_window)
8791 || (w = XWINDOW (f->tool_bar_window),
8792 WINDOW_TOTAL_LINES (w) == 0))
8793 return 0;
8795 /* Set up an iterator for the tool-bar window. */
8796 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8797 it.first_visible_x = 0;
8798 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8799 row = it.glyph_row;
8801 /* Build a string that represents the contents of the tool-bar. */
8802 build_desired_tool_bar_string (f);
8803 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8805 /* Display as many lines as needed to display all tool-bar items. */
8806 while (it.current_y < it.last_visible_y)
8807 display_tool_bar_line (&it);
8809 /* It doesn't make much sense to try scrolling in the tool-bar
8810 window, so don't do it. */
8811 w->desired_matrix->no_scrolling_p = 1;
8812 w->must_be_updated_p = 1;
8814 if (auto_resize_tool_bars_p)
8816 int nlines;
8818 /* If we couldn't display everything, change the tool-bar's
8819 height. */
8820 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8821 change_height_p = 1;
8823 /* If there are blank lines at the end, except for a partially
8824 visible blank line at the end that is smaller than
8825 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8826 row = it.glyph_row - 1;
8827 if (!row->displays_text_p
8828 && row->height >= FRAME_LINE_HEIGHT (f))
8829 change_height_p = 1;
8831 /* If row displays tool-bar items, but is partially visible,
8832 change the tool-bar's height. */
8833 if (row->displays_text_p
8834 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8835 change_height_p = 1;
8837 /* Resize windows as needed by changing the `tool-bar-lines'
8838 frame parameter. */
8839 if (change_height_p
8840 && (nlines = tool_bar_lines_needed (f),
8841 nlines != WINDOW_TOTAL_LINES (w)))
8843 extern Lisp_Object Qtool_bar_lines;
8844 Lisp_Object frame;
8845 int old_height = WINDOW_TOTAL_LINES (w);
8847 XSETFRAME (frame, f);
8848 clear_glyph_matrix (w->desired_matrix);
8849 Fmodify_frame_parameters (frame,
8850 Fcons (Fcons (Qtool_bar_lines,
8851 make_number (nlines)),
8852 Qnil));
8853 if (WINDOW_TOTAL_LINES (w) != old_height)
8854 fonts_changed_p = 1;
8858 return change_height_p;
8862 /* Get information about the tool-bar item which is displayed in GLYPH
8863 on frame F. Return in *PROP_IDX the index where tool-bar item
8864 properties start in F->tool_bar_items. Value is zero if
8865 GLYPH doesn't display a tool-bar item. */
8867 static int
8868 tool_bar_item_info (f, glyph, prop_idx)
8869 struct frame *f;
8870 struct glyph *glyph;
8871 int *prop_idx;
8873 Lisp_Object prop;
8874 int success_p;
8875 int charpos;
8877 /* This function can be called asynchronously, which means we must
8878 exclude any possibility that Fget_text_property signals an
8879 error. */
8880 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8881 charpos = max (0, charpos);
8883 /* Get the text property `menu-item' at pos. The value of that
8884 property is the start index of this item's properties in
8885 F->tool_bar_items. */
8886 prop = Fget_text_property (make_number (charpos),
8887 Qmenu_item, f->current_tool_bar_string);
8888 if (INTEGERP (prop))
8890 *prop_idx = XINT (prop);
8891 success_p = 1;
8893 else
8894 success_p = 0;
8896 return success_p;
8900 /* Get information about the tool-bar item at position X/Y on frame F.
8901 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8902 the current matrix of the tool-bar window of F, or NULL if not
8903 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8904 item in F->tool_bar_items. Value is
8906 -1 if X/Y is not on a tool-bar item
8907 0 if X/Y is on the same item that was highlighted before.
8908 1 otherwise. */
8910 static int
8911 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8912 struct frame *f;
8913 int x, y;
8914 struct glyph **glyph;
8915 int *hpos, *vpos, *prop_idx;
8917 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8918 struct window *w = XWINDOW (f->tool_bar_window);
8919 int area;
8921 /* Find the glyph under X/Y. */
8922 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8923 if (*glyph == NULL)
8924 return -1;
8926 /* Get the start of this tool-bar item's properties in
8927 f->tool_bar_items. */
8928 if (!tool_bar_item_info (f, *glyph, prop_idx))
8929 return -1;
8931 /* Is mouse on the highlighted item? */
8932 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8933 && *vpos >= dpyinfo->mouse_face_beg_row
8934 && *vpos <= dpyinfo->mouse_face_end_row
8935 && (*vpos > dpyinfo->mouse_face_beg_row
8936 || *hpos >= dpyinfo->mouse_face_beg_col)
8937 && (*vpos < dpyinfo->mouse_face_end_row
8938 || *hpos < dpyinfo->mouse_face_end_col
8939 || dpyinfo->mouse_face_past_end))
8940 return 0;
8942 return 1;
8946 /* EXPORT:
8947 Handle mouse button event on the tool-bar of frame F, at
8948 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8949 0 for button release. MODIFIERS is event modifiers for button
8950 release. */
8952 void
8953 handle_tool_bar_click (f, x, y, down_p, modifiers)
8954 struct frame *f;
8955 int x, y, down_p;
8956 unsigned int modifiers;
8958 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8959 struct window *w = XWINDOW (f->tool_bar_window);
8960 int hpos, vpos, prop_idx;
8961 struct glyph *glyph;
8962 Lisp_Object enabled_p;
8964 /* If not on the highlighted tool-bar item, return. */
8965 frame_to_window_pixel_xy (w, &x, &y);
8966 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8967 return;
8969 /* If item is disabled, do nothing. */
8970 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8971 if (NILP (enabled_p))
8972 return;
8974 if (down_p)
8976 /* Show item in pressed state. */
8977 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8978 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8979 last_tool_bar_item = prop_idx;
8981 else
8983 Lisp_Object key, frame;
8984 struct input_event event;
8985 EVENT_INIT (event);
8987 /* Show item in released state. */
8988 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8989 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8991 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8993 XSETFRAME (frame, f);
8994 event.kind = TOOL_BAR_EVENT;
8995 event.frame_or_window = frame;
8996 event.arg = frame;
8997 kbd_buffer_store_event (&event);
8999 event.kind = TOOL_BAR_EVENT;
9000 event.frame_or_window = frame;
9001 event.arg = key;
9002 event.modifiers = modifiers;
9003 kbd_buffer_store_event (&event);
9004 last_tool_bar_item = -1;
9009 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9010 tool-bar window-relative coordinates X/Y. Called from
9011 note_mouse_highlight. */
9013 static void
9014 note_tool_bar_highlight (f, x, y)
9015 struct frame *f;
9016 int x, y;
9018 Lisp_Object window = f->tool_bar_window;
9019 struct window *w = XWINDOW (window);
9020 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9021 int hpos, vpos;
9022 struct glyph *glyph;
9023 struct glyph_row *row;
9024 int i;
9025 Lisp_Object enabled_p;
9026 int prop_idx;
9027 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9028 int mouse_down_p, rc;
9030 /* Function note_mouse_highlight is called with negative x(y
9031 values when mouse moves outside of the frame. */
9032 if (x <= 0 || y <= 0)
9034 clear_mouse_face (dpyinfo);
9035 return;
9038 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9039 if (rc < 0)
9041 /* Not on tool-bar item. */
9042 clear_mouse_face (dpyinfo);
9043 return;
9045 else if (rc == 0)
9046 /* On same tool-bar item as before. */
9047 goto set_help_echo;
9049 clear_mouse_face (dpyinfo);
9051 /* Mouse is down, but on different tool-bar item? */
9052 mouse_down_p = (dpyinfo->grabbed
9053 && f == last_mouse_frame
9054 && FRAME_LIVE_P (f));
9055 if (mouse_down_p
9056 && last_tool_bar_item != prop_idx)
9057 return;
9059 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9060 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9062 /* If tool-bar item is not enabled, don't highlight it. */
9063 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9064 if (!NILP (enabled_p))
9066 /* Compute the x-position of the glyph. In front and past the
9067 image is a space. We include this in the highlighted area. */
9068 row = MATRIX_ROW (w->current_matrix, vpos);
9069 for (i = x = 0; i < hpos; ++i)
9070 x += row->glyphs[TEXT_AREA][i].pixel_width;
9072 /* Record this as the current active region. */
9073 dpyinfo->mouse_face_beg_col = hpos;
9074 dpyinfo->mouse_face_beg_row = vpos;
9075 dpyinfo->mouse_face_beg_x = x;
9076 dpyinfo->mouse_face_beg_y = row->y;
9077 dpyinfo->mouse_face_past_end = 0;
9079 dpyinfo->mouse_face_end_col = hpos + 1;
9080 dpyinfo->mouse_face_end_row = vpos;
9081 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9082 dpyinfo->mouse_face_end_y = row->y;
9083 dpyinfo->mouse_face_window = window;
9084 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9086 /* Display it as active. */
9087 show_mouse_face (dpyinfo, draw);
9088 dpyinfo->mouse_face_image_state = draw;
9091 set_help_echo:
9093 /* Set help_echo_string to a help string to display for this tool-bar item.
9094 XTread_socket does the rest. */
9095 help_echo_object = help_echo_window = Qnil;
9096 help_echo_pos = -1;
9097 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9098 if (NILP (help_echo_string))
9099 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9102 #endif /* HAVE_WINDOW_SYSTEM */
9106 /************************************************************************
9107 Horizontal scrolling
9108 ************************************************************************/
9110 static int hscroll_window_tree P_ ((Lisp_Object));
9111 static int hscroll_windows P_ ((Lisp_Object));
9113 /* For all leaf windows in the window tree rooted at WINDOW, set their
9114 hscroll value so that PT is (i) visible in the window, and (ii) so
9115 that it is not within a certain margin at the window's left and
9116 right border. Value is non-zero if any window's hscroll has been
9117 changed. */
9119 static int
9120 hscroll_window_tree (window)
9121 Lisp_Object window;
9123 int hscrolled_p = 0;
9124 int hscroll_relative_p = FLOATP (Vhscroll_step);
9125 int hscroll_step_abs = 0;
9126 double hscroll_step_rel = 0;
9128 if (hscroll_relative_p)
9130 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9131 if (hscroll_step_rel < 0)
9133 hscroll_relative_p = 0;
9134 hscroll_step_abs = 0;
9137 else if (INTEGERP (Vhscroll_step))
9139 hscroll_step_abs = XINT (Vhscroll_step);
9140 if (hscroll_step_abs < 0)
9141 hscroll_step_abs = 0;
9143 else
9144 hscroll_step_abs = 0;
9146 while (WINDOWP (window))
9148 struct window *w = XWINDOW (window);
9150 if (WINDOWP (w->hchild))
9151 hscrolled_p |= hscroll_window_tree (w->hchild);
9152 else if (WINDOWP (w->vchild))
9153 hscrolled_p |= hscroll_window_tree (w->vchild);
9154 else if (w->cursor.vpos >= 0)
9156 int h_margin;
9157 int text_area_width;
9158 struct glyph_row *current_cursor_row
9159 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9160 struct glyph_row *desired_cursor_row
9161 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9162 struct glyph_row *cursor_row
9163 = (desired_cursor_row->enabled_p
9164 ? desired_cursor_row
9165 : current_cursor_row);
9167 text_area_width = window_box_width (w, TEXT_AREA);
9169 /* Scroll when cursor is inside this scroll margin. */
9170 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9172 if ((XFASTINT (w->hscroll)
9173 && w->cursor.x <= h_margin)
9174 || (cursor_row->enabled_p
9175 && cursor_row->truncated_on_right_p
9176 && (w->cursor.x >= text_area_width - h_margin)))
9178 struct it it;
9179 int hscroll;
9180 struct buffer *saved_current_buffer;
9181 int pt;
9182 int wanted_x;
9184 /* Find point in a display of infinite width. */
9185 saved_current_buffer = current_buffer;
9186 current_buffer = XBUFFER (w->buffer);
9188 if (w == XWINDOW (selected_window))
9189 pt = BUF_PT (current_buffer);
9190 else
9192 pt = marker_position (w->pointm);
9193 pt = max (BEGV, pt);
9194 pt = min (ZV, pt);
9197 /* Move iterator to pt starting at cursor_row->start in
9198 a line with infinite width. */
9199 init_to_row_start (&it, w, cursor_row);
9200 it.last_visible_x = INFINITY;
9201 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9202 current_buffer = saved_current_buffer;
9204 /* Position cursor in window. */
9205 if (!hscroll_relative_p && hscroll_step_abs == 0)
9206 hscroll = max (0, (it.current_x
9207 - (ITERATOR_AT_END_OF_LINE_P (&it)
9208 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9209 : (text_area_width / 2))))
9210 / FRAME_COLUMN_WIDTH (it.f);
9211 else if (w->cursor.x >= text_area_width - h_margin)
9213 if (hscroll_relative_p)
9214 wanted_x = text_area_width * (1 - hscroll_step_rel)
9215 - h_margin;
9216 else
9217 wanted_x = text_area_width
9218 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9219 - h_margin;
9220 hscroll
9221 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9223 else
9225 if (hscroll_relative_p)
9226 wanted_x = text_area_width * hscroll_step_rel
9227 + h_margin;
9228 else
9229 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9230 + h_margin;
9231 hscroll
9232 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9234 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9236 /* Don't call Fset_window_hscroll if value hasn't
9237 changed because it will prevent redisplay
9238 optimizations. */
9239 if (XFASTINT (w->hscroll) != hscroll)
9241 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9242 w->hscroll = make_number (hscroll);
9243 hscrolled_p = 1;
9248 window = w->next;
9251 /* Value is non-zero if hscroll of any leaf window has been changed. */
9252 return hscrolled_p;
9256 /* Set hscroll so that cursor is visible and not inside horizontal
9257 scroll margins for all windows in the tree rooted at WINDOW. See
9258 also hscroll_window_tree above. Value is non-zero if any window's
9259 hscroll has been changed. If it has, desired matrices on the frame
9260 of WINDOW are cleared. */
9262 static int
9263 hscroll_windows (window)
9264 Lisp_Object window;
9266 int hscrolled_p;
9268 if (automatic_hscrolling_p)
9270 hscrolled_p = hscroll_window_tree (window);
9271 if (hscrolled_p)
9272 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9274 else
9275 hscrolled_p = 0;
9276 return hscrolled_p;
9281 /************************************************************************
9282 Redisplay
9283 ************************************************************************/
9285 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9286 to a non-zero value. This is sometimes handy to have in a debugger
9287 session. */
9289 #if GLYPH_DEBUG
9291 /* First and last unchanged row for try_window_id. */
9293 int debug_first_unchanged_at_end_vpos;
9294 int debug_last_unchanged_at_beg_vpos;
9296 /* Delta vpos and y. */
9298 int debug_dvpos, debug_dy;
9300 /* Delta in characters and bytes for try_window_id. */
9302 int debug_delta, debug_delta_bytes;
9304 /* Values of window_end_pos and window_end_vpos at the end of
9305 try_window_id. */
9307 EMACS_INT debug_end_pos, debug_end_vpos;
9309 /* Append a string to W->desired_matrix->method. FMT is a printf
9310 format string. A1...A9 are a supplement for a variable-length
9311 argument list. If trace_redisplay_p is non-zero also printf the
9312 resulting string to stderr. */
9314 static void
9315 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9316 struct window *w;
9317 char *fmt;
9318 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9320 char buffer[512];
9321 char *method = w->desired_matrix->method;
9322 int len = strlen (method);
9323 int size = sizeof w->desired_matrix->method;
9324 int remaining = size - len - 1;
9326 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9327 if (len && remaining)
9329 method[len] = '|';
9330 --remaining, ++len;
9333 strncpy (method + len, buffer, remaining);
9335 if (trace_redisplay_p)
9336 fprintf (stderr, "%p (%s): %s\n",
9338 ((BUFFERP (w->buffer)
9339 && STRINGP (XBUFFER (w->buffer)->name))
9340 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9341 : "no buffer"),
9342 buffer);
9345 #endif /* GLYPH_DEBUG */
9348 /* Value is non-zero if all changes in window W, which displays
9349 current_buffer, are in the text between START and END. START is a
9350 buffer position, END is given as a distance from Z. Used in
9351 redisplay_internal for display optimization. */
9353 static INLINE int
9354 text_outside_line_unchanged_p (w, start, end)
9355 struct window *w;
9356 int start, end;
9358 int unchanged_p = 1;
9360 /* If text or overlays have changed, see where. */
9361 if (XFASTINT (w->last_modified) < MODIFF
9362 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9364 /* Gap in the line? */
9365 if (GPT < start || Z - GPT < end)
9366 unchanged_p = 0;
9368 /* Changes start in front of the line, or end after it? */
9369 if (unchanged_p
9370 && (BEG_UNCHANGED < start - 1
9371 || END_UNCHANGED < end))
9372 unchanged_p = 0;
9374 /* If selective display, can't optimize if changes start at the
9375 beginning of the line. */
9376 if (unchanged_p
9377 && INTEGERP (current_buffer->selective_display)
9378 && XINT (current_buffer->selective_display) > 0
9379 && (BEG_UNCHANGED < start || GPT <= start))
9380 unchanged_p = 0;
9382 /* If there are overlays at the start or end of the line, these
9383 may have overlay strings with newlines in them. A change at
9384 START, for instance, may actually concern the display of such
9385 overlay strings as well, and they are displayed on different
9386 lines. So, quickly rule out this case. (For the future, it
9387 might be desirable to implement something more telling than
9388 just BEG/END_UNCHANGED.) */
9389 if (unchanged_p)
9391 if (BEG + BEG_UNCHANGED == start
9392 && overlay_touches_p (start))
9393 unchanged_p = 0;
9394 if (END_UNCHANGED == end
9395 && overlay_touches_p (Z - end))
9396 unchanged_p = 0;
9400 return unchanged_p;
9404 /* Do a frame update, taking possible shortcuts into account. This is
9405 the main external entry point for redisplay.
9407 If the last redisplay displayed an echo area message and that message
9408 is no longer requested, we clear the echo area or bring back the
9409 mini-buffer if that is in use. */
9411 void
9412 redisplay ()
9414 redisplay_internal (0);
9418 static Lisp_Object
9419 overlay_arrow_string_or_property (var, pbitmap)
9420 Lisp_Object var;
9421 int *pbitmap;
9423 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9424 Lisp_Object bitmap;
9426 if (pbitmap)
9428 *pbitmap = 0;
9429 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9430 *pbitmap = XINT (bitmap);
9433 if (!NILP (pstr))
9434 return pstr;
9435 return Voverlay_arrow_string;
9438 /* Return 1 if there are any overlay-arrows in current_buffer. */
9439 static int
9440 overlay_arrow_in_current_buffer_p ()
9442 Lisp_Object vlist;
9444 for (vlist = Voverlay_arrow_variable_list;
9445 CONSP (vlist);
9446 vlist = XCDR (vlist))
9448 Lisp_Object var = XCAR (vlist);
9449 Lisp_Object val;
9451 if (!SYMBOLP (var))
9452 continue;
9453 val = find_symbol_value (var);
9454 if (MARKERP (val)
9455 && current_buffer == XMARKER (val)->buffer)
9456 return 1;
9458 return 0;
9462 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9463 has changed. */
9465 static int
9466 overlay_arrows_changed_p ()
9468 Lisp_Object vlist;
9470 for (vlist = Voverlay_arrow_variable_list;
9471 CONSP (vlist);
9472 vlist = XCDR (vlist))
9474 Lisp_Object var = XCAR (vlist);
9475 Lisp_Object val, pstr;
9477 if (!SYMBOLP (var))
9478 continue;
9479 val = find_symbol_value (var);
9480 if (!MARKERP (val))
9481 continue;
9482 if (! EQ (COERCE_MARKER (val),
9483 Fget (var, Qlast_arrow_position))
9484 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9485 EQ (pstr, Fget (var, Qlast_arrow_string))))
9486 return 1;
9488 return 0;
9491 /* Mark overlay arrows to be updated on next redisplay. */
9493 static void
9494 update_overlay_arrows (up_to_date)
9495 int up_to_date;
9497 Lisp_Object vlist;
9499 for (vlist = Voverlay_arrow_variable_list;
9500 CONSP (vlist);
9501 vlist = XCDR (vlist))
9503 Lisp_Object var = XCAR (vlist);
9505 if (!SYMBOLP (var))
9506 continue;
9508 if (up_to_date > 0)
9510 Lisp_Object val = find_symbol_value (var);
9511 Fput (var, Qlast_arrow_position,
9512 COERCE_MARKER (val));
9513 Fput (var, Qlast_arrow_string,
9514 overlay_arrow_string_or_property (var, 0));
9516 else if (up_to_date < 0
9517 || !NILP (Fget (var, Qlast_arrow_position)))
9519 Fput (var, Qlast_arrow_position, Qt);
9520 Fput (var, Qlast_arrow_string, Qt);
9526 /* Return overlay arrow string to display at row.
9527 Return t if display as bitmap in left fringe.
9528 Return nil if no overlay arrow. */
9530 static Lisp_Object
9531 overlay_arrow_at_row (it, row, pbitmap)
9532 struct it *it;
9533 struct glyph_row *row;
9534 int *pbitmap;
9536 Lisp_Object vlist;
9538 for (vlist = Voverlay_arrow_variable_list;
9539 CONSP (vlist);
9540 vlist = XCDR (vlist))
9542 Lisp_Object var = XCAR (vlist);
9543 Lisp_Object val;
9545 if (!SYMBOLP (var))
9546 continue;
9548 val = find_symbol_value (var);
9550 if (MARKERP (val)
9551 && current_buffer == XMARKER (val)->buffer
9552 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9554 val = overlay_arrow_string_or_property (var, pbitmap);
9555 if (FRAME_WINDOW_P (it->f)
9556 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9557 return Qt;
9558 if (STRINGP (val))
9559 return val;
9560 break;
9564 *pbitmap = 0;
9565 return Qnil;
9568 /* Return 1 if point moved out of or into a composition. Otherwise
9569 return 0. PREV_BUF and PREV_PT are the last point buffer and
9570 position. BUF and PT are the current point buffer and position. */
9573 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9574 struct buffer *prev_buf, *buf;
9575 int prev_pt, pt;
9577 int start, end;
9578 Lisp_Object prop;
9579 Lisp_Object buffer;
9581 XSETBUFFER (buffer, buf);
9582 /* Check a composition at the last point if point moved within the
9583 same buffer. */
9584 if (prev_buf == buf)
9586 if (prev_pt == pt)
9587 /* Point didn't move. */
9588 return 0;
9590 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9591 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9592 && COMPOSITION_VALID_P (start, end, prop)
9593 && start < prev_pt && end > prev_pt)
9594 /* The last point was within the composition. Return 1 iff
9595 point moved out of the composition. */
9596 return (pt <= start || pt >= end);
9599 /* Check a composition at the current point. */
9600 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9601 && find_composition (pt, -1, &start, &end, &prop, buffer)
9602 && COMPOSITION_VALID_P (start, end, prop)
9603 && start < pt && end > pt);
9607 /* Reconsider the setting of B->clip_changed which is displayed
9608 in window W. */
9610 static INLINE void
9611 reconsider_clip_changes (w, b)
9612 struct window *w;
9613 struct buffer *b;
9615 if (b->clip_changed
9616 && !NILP (w->window_end_valid)
9617 && w->current_matrix->buffer == b
9618 && w->current_matrix->zv == BUF_ZV (b)
9619 && w->current_matrix->begv == BUF_BEGV (b))
9620 b->clip_changed = 0;
9622 /* If display wasn't paused, and W is not a tool bar window, see if
9623 point has been moved into or out of a composition. In that case,
9624 we set b->clip_changed to 1 to force updating the screen. If
9625 b->clip_changed has already been set to 1, we can skip this
9626 check. */
9627 if (!b->clip_changed
9628 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9630 int pt;
9632 if (w == XWINDOW (selected_window))
9633 pt = BUF_PT (current_buffer);
9634 else
9635 pt = marker_position (w->pointm);
9637 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9638 || pt != XINT (w->last_point))
9639 && check_point_in_composition (w->current_matrix->buffer,
9640 XINT (w->last_point),
9641 XBUFFER (w->buffer), pt))
9642 b->clip_changed = 1;
9647 /* Select FRAME to forward the values of frame-local variables into C
9648 variables so that the redisplay routines can access those values
9649 directly. */
9651 static void
9652 select_frame_for_redisplay (frame)
9653 Lisp_Object frame;
9655 Lisp_Object tail, sym, val;
9656 Lisp_Object old = selected_frame;
9658 selected_frame = frame;
9660 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9661 if (CONSP (XCAR (tail))
9662 && (sym = XCAR (XCAR (tail)),
9663 SYMBOLP (sym))
9664 && (sym = indirect_variable (sym),
9665 val = SYMBOL_VALUE (sym),
9666 (BUFFER_LOCAL_VALUEP (val)
9667 || SOME_BUFFER_LOCAL_VALUEP (val)))
9668 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9669 Fsymbol_value (sym);
9671 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9672 if (CONSP (XCAR (tail))
9673 && (sym = XCAR (XCAR (tail)),
9674 SYMBOLP (sym))
9675 && (sym = indirect_variable (sym),
9676 val = SYMBOL_VALUE (sym),
9677 (BUFFER_LOCAL_VALUEP (val)
9678 || SOME_BUFFER_LOCAL_VALUEP (val)))
9679 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9680 Fsymbol_value (sym);
9684 #define STOP_POLLING \
9685 do { if (! polling_stopped_here) stop_polling (); \
9686 polling_stopped_here = 1; } while (0)
9688 #define RESUME_POLLING \
9689 do { if (polling_stopped_here) start_polling (); \
9690 polling_stopped_here = 0; } while (0)
9693 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9694 response to any user action; therefore, we should preserve the echo
9695 area. (Actually, our caller does that job.) Perhaps in the future
9696 avoid recentering windows if it is not necessary; currently that
9697 causes some problems. */
9699 static void
9700 redisplay_internal (preserve_echo_area)
9701 int preserve_echo_area;
9703 struct window *w = XWINDOW (selected_window);
9704 struct frame *f = XFRAME (w->frame);
9705 int pause;
9706 int must_finish = 0;
9707 struct text_pos tlbufpos, tlendpos;
9708 int number_of_visible_frames;
9709 int count;
9710 struct frame *sf = SELECTED_FRAME ();
9711 int polling_stopped_here = 0;
9713 /* Non-zero means redisplay has to consider all windows on all
9714 frames. Zero means, only selected_window is considered. */
9715 int consider_all_windows_p;
9717 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9719 /* No redisplay if running in batch mode or frame is not yet fully
9720 initialized, or redisplay is explicitly turned off by setting
9721 Vinhibit_redisplay. */
9722 if (noninteractive
9723 || !NILP (Vinhibit_redisplay)
9724 || !f->glyphs_initialized_p)
9725 return;
9727 /* The flag redisplay_performed_directly_p is set by
9728 direct_output_for_insert when it already did the whole screen
9729 update necessary. */
9730 if (redisplay_performed_directly_p)
9732 redisplay_performed_directly_p = 0;
9733 if (!hscroll_windows (selected_window))
9734 return;
9737 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9738 if (popup_activated ())
9739 return;
9740 #endif
9742 /* I don't think this happens but let's be paranoid. */
9743 if (redisplaying_p)
9744 return;
9746 /* Record a function that resets redisplaying_p to its old value
9747 when we leave this function. */
9748 count = SPECPDL_INDEX ();
9749 record_unwind_protect (unwind_redisplay,
9750 Fcons (make_number (redisplaying_p), selected_frame));
9751 ++redisplaying_p;
9752 specbind (Qinhibit_free_realized_faces, Qnil);
9754 retry:
9755 pause = 0;
9756 reconsider_clip_changes (w, current_buffer);
9758 /* If new fonts have been loaded that make a glyph matrix adjustment
9759 necessary, do it. */
9760 if (fonts_changed_p)
9762 adjust_glyphs (NULL);
9763 ++windows_or_buffers_changed;
9764 fonts_changed_p = 0;
9767 /* If face_change_count is non-zero, init_iterator will free all
9768 realized faces, which includes the faces referenced from current
9769 matrices. So, we can't reuse current matrices in this case. */
9770 if (face_change_count)
9771 ++windows_or_buffers_changed;
9773 if (! FRAME_WINDOW_P (sf)
9774 && previous_terminal_frame != sf)
9776 /* Since frames on an ASCII terminal share the same display
9777 area, displaying a different frame means redisplay the whole
9778 thing. */
9779 windows_or_buffers_changed++;
9780 SET_FRAME_GARBAGED (sf);
9781 XSETFRAME (Vterminal_frame, sf);
9783 previous_terminal_frame = sf;
9785 /* Set the visible flags for all frames. Do this before checking
9786 for resized or garbaged frames; they want to know if their frames
9787 are visible. See the comment in frame.h for
9788 FRAME_SAMPLE_VISIBILITY. */
9790 Lisp_Object tail, frame;
9792 number_of_visible_frames = 0;
9794 FOR_EACH_FRAME (tail, frame)
9796 struct frame *f = XFRAME (frame);
9798 FRAME_SAMPLE_VISIBILITY (f);
9799 if (FRAME_VISIBLE_P (f))
9800 ++number_of_visible_frames;
9801 clear_desired_matrices (f);
9805 /* Notice any pending interrupt request to change frame size. */
9806 do_pending_window_change (1);
9808 /* Clear frames marked as garbaged. */
9809 if (frame_garbaged)
9810 clear_garbaged_frames ();
9812 /* Build menubar and tool-bar items. */
9813 prepare_menu_bars ();
9815 if (windows_or_buffers_changed)
9816 update_mode_lines++;
9818 /* Detect case that we need to write or remove a star in the mode line. */
9819 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9821 w->update_mode_line = Qt;
9822 if (buffer_shared > 1)
9823 update_mode_lines++;
9826 /* If %c is in the mode line, update it if needed. */
9827 if (!NILP (w->column_number_displayed)
9828 /* This alternative quickly identifies a common case
9829 where no change is needed. */
9830 && !(PT == XFASTINT (w->last_point)
9831 && XFASTINT (w->last_modified) >= MODIFF
9832 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9833 && (XFASTINT (w->column_number_displayed)
9834 != (int) current_column ())) /* iftc */
9835 w->update_mode_line = Qt;
9837 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9839 /* The variable buffer_shared is set in redisplay_window and
9840 indicates that we redisplay a buffer in different windows. See
9841 there. */
9842 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9843 || cursor_type_changed);
9845 /* If specs for an arrow have changed, do thorough redisplay
9846 to ensure we remove any arrow that should no longer exist. */
9847 if (overlay_arrows_changed_p ())
9848 consider_all_windows_p = windows_or_buffers_changed = 1;
9850 /* Normally the message* functions will have already displayed and
9851 updated the echo area, but the frame may have been trashed, or
9852 the update may have been preempted, so display the echo area
9853 again here. Checking message_cleared_p captures the case that
9854 the echo area should be cleared. */
9855 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9856 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9857 || (message_cleared_p
9858 && minibuf_level == 0
9859 /* If the mini-window is currently selected, this means the
9860 echo-area doesn't show through. */
9861 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9863 int window_height_changed_p = echo_area_display (0);
9864 must_finish = 1;
9866 /* If we don't display the current message, don't clear the
9867 message_cleared_p flag, because, if we did, we wouldn't clear
9868 the echo area in the next redisplay which doesn't preserve
9869 the echo area. */
9870 if (!display_last_displayed_message_p)
9871 message_cleared_p = 0;
9873 if (fonts_changed_p)
9874 goto retry;
9875 else if (window_height_changed_p)
9877 consider_all_windows_p = 1;
9878 ++update_mode_lines;
9879 ++windows_or_buffers_changed;
9881 /* If window configuration was changed, frames may have been
9882 marked garbaged. Clear them or we will experience
9883 surprises wrt scrolling. */
9884 if (frame_garbaged)
9885 clear_garbaged_frames ();
9888 else if (EQ (selected_window, minibuf_window)
9889 && (current_buffer->clip_changed
9890 || XFASTINT (w->last_modified) < MODIFF
9891 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9892 && resize_mini_window (w, 0))
9894 /* Resized active mini-window to fit the size of what it is
9895 showing if its contents might have changed. */
9896 must_finish = 1;
9897 consider_all_windows_p = 1;
9898 ++windows_or_buffers_changed;
9899 ++update_mode_lines;
9901 /* If window configuration was changed, frames may have been
9902 marked garbaged. Clear them or we will experience
9903 surprises wrt scrolling. */
9904 if (frame_garbaged)
9905 clear_garbaged_frames ();
9909 /* If showing the region, and mark has changed, we must redisplay
9910 the whole window. The assignment to this_line_start_pos prevents
9911 the optimization directly below this if-statement. */
9912 if (((!NILP (Vtransient_mark_mode)
9913 && !NILP (XBUFFER (w->buffer)->mark_active))
9914 != !NILP (w->region_showing))
9915 || (!NILP (w->region_showing)
9916 && !EQ (w->region_showing,
9917 Fmarker_position (XBUFFER (w->buffer)->mark))))
9918 CHARPOS (this_line_start_pos) = 0;
9920 /* Optimize the case that only the line containing the cursor in the
9921 selected window has changed. Variables starting with this_ are
9922 set in display_line and record information about the line
9923 containing the cursor. */
9924 tlbufpos = this_line_start_pos;
9925 tlendpos = this_line_end_pos;
9926 if (!consider_all_windows_p
9927 && CHARPOS (tlbufpos) > 0
9928 && NILP (w->update_mode_line)
9929 && !current_buffer->clip_changed
9930 && !current_buffer->prevent_redisplay_optimizations_p
9931 && FRAME_VISIBLE_P (XFRAME (w->frame))
9932 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9933 /* Make sure recorded data applies to current buffer, etc. */
9934 && this_line_buffer == current_buffer
9935 && current_buffer == XBUFFER (w->buffer)
9936 && NILP (w->force_start)
9937 && NILP (w->optional_new_start)
9938 /* Point must be on the line that we have info recorded about. */
9939 && PT >= CHARPOS (tlbufpos)
9940 && PT <= Z - CHARPOS (tlendpos)
9941 /* All text outside that line, including its final newline,
9942 must be unchanged */
9943 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9944 CHARPOS (tlendpos)))
9946 if (CHARPOS (tlbufpos) > BEGV
9947 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9948 && (CHARPOS (tlbufpos) == ZV
9949 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9950 /* Former continuation line has disappeared by becoming empty */
9951 goto cancel;
9952 else if (XFASTINT (w->last_modified) < MODIFF
9953 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9954 || MINI_WINDOW_P (w))
9956 /* We have to handle the case of continuation around a
9957 wide-column character (See the comment in indent.c around
9958 line 885).
9960 For instance, in the following case:
9962 -------- Insert --------
9963 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9964 J_I_ ==> J_I_ `^^' are cursors.
9965 ^^ ^^
9966 -------- --------
9968 As we have to redraw the line above, we should goto cancel. */
9970 struct it it;
9971 int line_height_before = this_line_pixel_height;
9973 /* Note that start_display will handle the case that the
9974 line starting at tlbufpos is a continuation lines. */
9975 start_display (&it, w, tlbufpos);
9977 /* Implementation note: It this still necessary? */
9978 if (it.current_x != this_line_start_x)
9979 goto cancel;
9981 TRACE ((stderr, "trying display optimization 1\n"));
9982 w->cursor.vpos = -1;
9983 overlay_arrow_seen = 0;
9984 it.vpos = this_line_vpos;
9985 it.current_y = this_line_y;
9986 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9987 display_line (&it);
9989 /* If line contains point, is not continued,
9990 and ends at same distance from eob as before, we win */
9991 if (w->cursor.vpos >= 0
9992 /* Line is not continued, otherwise this_line_start_pos
9993 would have been set to 0 in display_line. */
9994 && CHARPOS (this_line_start_pos)
9995 /* Line ends as before. */
9996 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9997 /* Line has same height as before. Otherwise other lines
9998 would have to be shifted up or down. */
9999 && this_line_pixel_height == line_height_before)
10001 /* If this is not the window's last line, we must adjust
10002 the charstarts of the lines below. */
10003 if (it.current_y < it.last_visible_y)
10005 struct glyph_row *row
10006 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10007 int delta, delta_bytes;
10009 if (Z - CHARPOS (tlendpos) == ZV)
10011 /* This line ends at end of (accessible part of)
10012 buffer. There is no newline to count. */
10013 delta = (Z
10014 - CHARPOS (tlendpos)
10015 - MATRIX_ROW_START_CHARPOS (row));
10016 delta_bytes = (Z_BYTE
10017 - BYTEPOS (tlendpos)
10018 - MATRIX_ROW_START_BYTEPOS (row));
10020 else
10022 /* This line ends in a newline. Must take
10023 account of the newline and the rest of the
10024 text that follows. */
10025 delta = (Z
10026 - CHARPOS (tlendpos)
10027 - MATRIX_ROW_START_CHARPOS (row));
10028 delta_bytes = (Z_BYTE
10029 - BYTEPOS (tlendpos)
10030 - MATRIX_ROW_START_BYTEPOS (row));
10033 increment_matrix_positions (w->current_matrix,
10034 this_line_vpos + 1,
10035 w->current_matrix->nrows,
10036 delta, delta_bytes);
10039 /* If this row displays text now but previously didn't,
10040 or vice versa, w->window_end_vpos may have to be
10041 adjusted. */
10042 if ((it.glyph_row - 1)->displays_text_p)
10044 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10045 XSETINT (w->window_end_vpos, this_line_vpos);
10047 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10048 && this_line_vpos > 0)
10049 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10050 w->window_end_valid = Qnil;
10052 /* Update hint: No need to try to scroll in update_window. */
10053 w->desired_matrix->no_scrolling_p = 1;
10055 #if GLYPH_DEBUG
10056 *w->desired_matrix->method = 0;
10057 debug_method_add (w, "optimization 1");
10058 #endif
10059 #ifdef HAVE_WINDOW_SYSTEM
10060 update_window_fringes (w, 0);
10061 #endif
10062 goto update;
10064 else
10065 goto cancel;
10067 else if (/* Cursor position hasn't changed. */
10068 PT == XFASTINT (w->last_point)
10069 /* Make sure the cursor was last displayed
10070 in this window. Otherwise we have to reposition it. */
10071 && 0 <= w->cursor.vpos
10072 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10074 if (!must_finish)
10076 do_pending_window_change (1);
10078 /* We used to always goto end_of_redisplay here, but this
10079 isn't enough if we have a blinking cursor. */
10080 if (w->cursor_off_p == w->last_cursor_off_p)
10081 goto end_of_redisplay;
10083 goto update;
10085 /* If highlighting the region, or if the cursor is in the echo area,
10086 then we can't just move the cursor. */
10087 else if (! (!NILP (Vtransient_mark_mode)
10088 && !NILP (current_buffer->mark_active))
10089 && (EQ (selected_window, current_buffer->last_selected_window)
10090 || highlight_nonselected_windows)
10091 && NILP (w->region_showing)
10092 && NILP (Vshow_trailing_whitespace)
10093 && !cursor_in_echo_area)
10095 struct it it;
10096 struct glyph_row *row;
10098 /* Skip from tlbufpos to PT and see where it is. Note that
10099 PT may be in invisible text. If so, we will end at the
10100 next visible position. */
10101 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10102 NULL, DEFAULT_FACE_ID);
10103 it.current_x = this_line_start_x;
10104 it.current_y = this_line_y;
10105 it.vpos = this_line_vpos;
10107 /* The call to move_it_to stops in front of PT, but
10108 moves over before-strings. */
10109 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10111 if (it.vpos == this_line_vpos
10112 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10113 row->enabled_p))
10115 xassert (this_line_vpos == it.vpos);
10116 xassert (this_line_y == it.current_y);
10117 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10118 #if GLYPH_DEBUG
10119 *w->desired_matrix->method = 0;
10120 debug_method_add (w, "optimization 3");
10121 #endif
10122 goto update;
10124 else
10125 goto cancel;
10128 cancel:
10129 /* Text changed drastically or point moved off of line. */
10130 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10133 CHARPOS (this_line_start_pos) = 0;
10134 consider_all_windows_p |= buffer_shared > 1;
10135 ++clear_face_cache_count;
10138 /* Build desired matrices, and update the display. If
10139 consider_all_windows_p is non-zero, do it for all windows on all
10140 frames. Otherwise do it for selected_window, only. */
10142 if (consider_all_windows_p)
10144 Lisp_Object tail, frame;
10145 int i, n = 0, size = 50;
10146 struct frame **updated
10147 = (struct frame **) alloca (size * sizeof *updated);
10149 /* Clear the face cache eventually. */
10150 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10152 clear_face_cache (0);
10153 clear_face_cache_count = 0;
10156 /* Recompute # windows showing selected buffer. This will be
10157 incremented each time such a window is displayed. */
10158 buffer_shared = 0;
10160 FOR_EACH_FRAME (tail, frame)
10162 struct frame *f = XFRAME (frame);
10164 if (FRAME_WINDOW_P (f) || f == sf)
10166 if (! EQ (frame, selected_frame))
10167 /* Select the frame, for the sake of frame-local
10168 variables. */
10169 select_frame_for_redisplay (frame);
10171 #ifdef HAVE_WINDOW_SYSTEM
10172 if (clear_face_cache_count % 50 == 0
10173 && FRAME_WINDOW_P (f))
10174 clear_image_cache (f, 0);
10175 #endif /* HAVE_WINDOW_SYSTEM */
10177 /* Mark all the scroll bars to be removed; we'll redeem
10178 the ones we want when we redisplay their windows. */
10179 if (condemn_scroll_bars_hook)
10180 condemn_scroll_bars_hook (f);
10182 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10183 redisplay_windows (FRAME_ROOT_WINDOW (f));
10185 /* Any scroll bars which redisplay_windows should have
10186 nuked should now go away. */
10187 if (judge_scroll_bars_hook)
10188 judge_scroll_bars_hook (f);
10190 /* If fonts changed, display again. */
10191 /* ??? rms: I suspect it is a mistake to jump all the way
10192 back to retry here. It should just retry this frame. */
10193 if (fonts_changed_p)
10194 goto retry;
10196 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10198 /* See if we have to hscroll. */
10199 if (hscroll_windows (f->root_window))
10200 goto retry;
10202 /* Prevent various kinds of signals during display
10203 update. stdio is not robust about handling
10204 signals, which can cause an apparent I/O
10205 error. */
10206 if (interrupt_input)
10207 unrequest_sigio ();
10208 STOP_POLLING;
10210 /* Update the display. */
10211 set_window_update_flags (XWINDOW (f->root_window), 1);
10212 pause |= update_frame (f, 0, 0);
10213 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10214 if (pause)
10215 break;
10216 #endif
10218 if (n == size)
10220 int nbytes = size * sizeof *updated;
10221 struct frame **p = (struct frame **) alloca (2 * nbytes);
10222 bcopy (updated, p, nbytes);
10223 size *= 2;
10226 updated[n++] = f;
10231 if (!pause)
10233 /* Do the mark_window_display_accurate after all windows have
10234 been redisplayed because this call resets flags in buffers
10235 which are needed for proper redisplay. */
10236 for (i = 0; i < n; ++i)
10238 struct frame *f = updated[i];
10239 mark_window_display_accurate (f->root_window, 1);
10240 if (frame_up_to_date_hook)
10241 frame_up_to_date_hook (f);
10245 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10247 Lisp_Object mini_window;
10248 struct frame *mini_frame;
10250 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10251 /* Use list_of_error, not Qerror, so that
10252 we catch only errors and don't run the debugger. */
10253 internal_condition_case_1 (redisplay_window_1, selected_window,
10254 list_of_error,
10255 redisplay_window_error);
10257 /* Compare desired and current matrices, perform output. */
10259 update:
10260 /* If fonts changed, display again. */
10261 if (fonts_changed_p)
10262 goto retry;
10264 /* Prevent various kinds of signals during display update.
10265 stdio is not robust about handling signals,
10266 which can cause an apparent I/O error. */
10267 if (interrupt_input)
10268 unrequest_sigio ();
10269 STOP_POLLING;
10271 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10273 if (hscroll_windows (selected_window))
10274 goto retry;
10276 XWINDOW (selected_window)->must_be_updated_p = 1;
10277 pause = update_frame (sf, 0, 0);
10280 /* We may have called echo_area_display at the top of this
10281 function. If the echo area is on another frame, that may
10282 have put text on a frame other than the selected one, so the
10283 above call to update_frame would not have caught it. Catch
10284 it here. */
10285 mini_window = FRAME_MINIBUF_WINDOW (sf);
10286 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10288 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10290 XWINDOW (mini_window)->must_be_updated_p = 1;
10291 pause |= update_frame (mini_frame, 0, 0);
10292 if (!pause && hscroll_windows (mini_window))
10293 goto retry;
10297 /* If display was paused because of pending input, make sure we do a
10298 thorough update the next time. */
10299 if (pause)
10301 /* Prevent the optimization at the beginning of
10302 redisplay_internal that tries a single-line update of the
10303 line containing the cursor in the selected window. */
10304 CHARPOS (this_line_start_pos) = 0;
10306 /* Let the overlay arrow be updated the next time. */
10307 update_overlay_arrows (0);
10309 /* If we pause after scrolling, some rows in the current
10310 matrices of some windows are not valid. */
10311 if (!WINDOW_FULL_WIDTH_P (w)
10312 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10313 update_mode_lines = 1;
10315 else
10317 if (!consider_all_windows_p)
10319 /* This has already been done above if
10320 consider_all_windows_p is set. */
10321 mark_window_display_accurate_1 (w, 1);
10323 /* Say overlay arrows are up to date. */
10324 update_overlay_arrows (1);
10326 if (frame_up_to_date_hook != 0)
10327 frame_up_to_date_hook (sf);
10330 update_mode_lines = 0;
10331 windows_or_buffers_changed = 0;
10332 cursor_type_changed = 0;
10335 /* Start SIGIO interrupts coming again. Having them off during the
10336 code above makes it less likely one will discard output, but not
10337 impossible, since there might be stuff in the system buffer here.
10338 But it is much hairier to try to do anything about that. */
10339 if (interrupt_input)
10340 request_sigio ();
10341 RESUME_POLLING;
10343 /* If a frame has become visible which was not before, redisplay
10344 again, so that we display it. Expose events for such a frame
10345 (which it gets when becoming visible) don't call the parts of
10346 redisplay constructing glyphs, so simply exposing a frame won't
10347 display anything in this case. So, we have to display these
10348 frames here explicitly. */
10349 if (!pause)
10351 Lisp_Object tail, frame;
10352 int new_count = 0;
10354 FOR_EACH_FRAME (tail, frame)
10356 int this_is_visible = 0;
10358 if (XFRAME (frame)->visible)
10359 this_is_visible = 1;
10360 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10361 if (XFRAME (frame)->visible)
10362 this_is_visible = 1;
10364 if (this_is_visible)
10365 new_count++;
10368 if (new_count != number_of_visible_frames)
10369 windows_or_buffers_changed++;
10372 /* Change frame size now if a change is pending. */
10373 do_pending_window_change (1);
10375 /* If we just did a pending size change, or have additional
10376 visible frames, redisplay again. */
10377 if (windows_or_buffers_changed && !pause)
10378 goto retry;
10380 end_of_redisplay:
10381 unbind_to (count, Qnil);
10382 RESUME_POLLING;
10386 /* Redisplay, but leave alone any recent echo area message unless
10387 another message has been requested in its place.
10389 This is useful in situations where you need to redisplay but no
10390 user action has occurred, making it inappropriate for the message
10391 area to be cleared. See tracking_off and
10392 wait_reading_process_output for examples of these situations.
10394 FROM_WHERE is an integer saying from where this function was
10395 called. This is useful for debugging. */
10397 void
10398 redisplay_preserve_echo_area (from_where)
10399 int from_where;
10401 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10403 if (!NILP (echo_area_buffer[1]))
10405 /* We have a previously displayed message, but no current
10406 message. Redisplay the previous message. */
10407 display_last_displayed_message_p = 1;
10408 redisplay_internal (1);
10409 display_last_displayed_message_p = 0;
10411 else
10412 redisplay_internal (1);
10414 if (rif != NULL && rif->flush_display_optional)
10415 rif->flush_display_optional (NULL);
10419 /* Function registered with record_unwind_protect in
10420 redisplay_internal. Reset redisplaying_p to the value it had
10421 before redisplay_internal was called, and clear
10422 prevent_freeing_realized_faces_p. It also selects the previously
10423 selected frame. */
10425 static Lisp_Object
10426 unwind_redisplay (val)
10427 Lisp_Object val;
10429 Lisp_Object old_redisplaying_p, old_frame;
10431 old_redisplaying_p = XCAR (val);
10432 redisplaying_p = XFASTINT (old_redisplaying_p);
10433 old_frame = XCDR (val);
10434 if (! EQ (old_frame, selected_frame))
10435 select_frame_for_redisplay (old_frame);
10436 return Qnil;
10440 /* Mark the display of window W as accurate or inaccurate. If
10441 ACCURATE_P is non-zero mark display of W as accurate. If
10442 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10443 redisplay_internal is called. */
10445 static void
10446 mark_window_display_accurate_1 (w, accurate_p)
10447 struct window *w;
10448 int accurate_p;
10450 if (BUFFERP (w->buffer))
10452 struct buffer *b = XBUFFER (w->buffer);
10454 w->last_modified
10455 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10456 w->last_overlay_modified
10457 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10458 w->last_had_star
10459 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10461 if (accurate_p)
10463 b->clip_changed = 0;
10464 b->prevent_redisplay_optimizations_p = 0;
10466 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10467 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10468 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10469 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10471 w->current_matrix->buffer = b;
10472 w->current_matrix->begv = BUF_BEGV (b);
10473 w->current_matrix->zv = BUF_ZV (b);
10475 w->last_cursor = w->cursor;
10476 w->last_cursor_off_p = w->cursor_off_p;
10478 if (w == XWINDOW (selected_window))
10479 w->last_point = make_number (BUF_PT (b));
10480 else
10481 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10485 if (accurate_p)
10487 w->window_end_valid = w->buffer;
10488 #if 0 /* This is incorrect with variable-height lines. */
10489 xassert (XINT (w->window_end_vpos)
10490 < (WINDOW_TOTAL_LINES (w)
10491 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10492 #endif
10493 w->update_mode_line = Qnil;
10498 /* Mark the display of windows in the window tree rooted at WINDOW as
10499 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10500 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10501 be redisplayed the next time redisplay_internal is called. */
10503 void
10504 mark_window_display_accurate (window, accurate_p)
10505 Lisp_Object window;
10506 int accurate_p;
10508 struct window *w;
10510 for (; !NILP (window); window = w->next)
10512 w = XWINDOW (window);
10513 mark_window_display_accurate_1 (w, accurate_p);
10515 if (!NILP (w->vchild))
10516 mark_window_display_accurate (w->vchild, accurate_p);
10517 if (!NILP (w->hchild))
10518 mark_window_display_accurate (w->hchild, accurate_p);
10521 if (accurate_p)
10523 update_overlay_arrows (1);
10525 else
10527 /* Force a thorough redisplay the next time by setting
10528 last_arrow_position and last_arrow_string to t, which is
10529 unequal to any useful value of Voverlay_arrow_... */
10530 update_overlay_arrows (-1);
10535 /* Return value in display table DP (Lisp_Char_Table *) for character
10536 C. Since a display table doesn't have any parent, we don't have to
10537 follow parent. Do not call this function directly but use the
10538 macro DISP_CHAR_VECTOR. */
10540 Lisp_Object
10541 disp_char_vector (dp, c)
10542 struct Lisp_Char_Table *dp;
10543 int c;
10545 int code[4], i;
10546 Lisp_Object val;
10548 if (SINGLE_BYTE_CHAR_P (c))
10549 return (dp->contents[c]);
10551 SPLIT_CHAR (c, code[0], code[1], code[2]);
10552 if (code[1] < 32)
10553 code[1] = -1;
10554 else if (code[2] < 32)
10555 code[2] = -1;
10557 /* Here, the possible range of code[0] (== charset ID) is
10558 128..max_charset. Since the top level char table contains data
10559 for multibyte characters after 256th element, we must increment
10560 code[0] by 128 to get a correct index. */
10561 code[0] += 128;
10562 code[3] = -1; /* anchor */
10564 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10566 val = dp->contents[code[i]];
10567 if (!SUB_CHAR_TABLE_P (val))
10568 return (NILP (val) ? dp->defalt : val);
10571 /* Here, val is a sub char table. We return the default value of
10572 it. */
10573 return (dp->defalt);
10578 /***********************************************************************
10579 Window Redisplay
10580 ***********************************************************************/
10582 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10584 static void
10585 redisplay_windows (window)
10586 Lisp_Object window;
10588 while (!NILP (window))
10590 struct window *w = XWINDOW (window);
10592 if (!NILP (w->hchild))
10593 redisplay_windows (w->hchild);
10594 else if (!NILP (w->vchild))
10595 redisplay_windows (w->vchild);
10596 else
10598 displayed_buffer = XBUFFER (w->buffer);
10599 /* Use list_of_error, not Qerror, so that
10600 we catch only errors and don't run the debugger. */
10601 internal_condition_case_1 (redisplay_window_0, window,
10602 list_of_error,
10603 redisplay_window_error);
10606 window = w->next;
10610 static Lisp_Object
10611 redisplay_window_error ()
10613 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10614 return Qnil;
10617 static Lisp_Object
10618 redisplay_window_0 (window)
10619 Lisp_Object window;
10621 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10622 redisplay_window (window, 0);
10623 return Qnil;
10626 static Lisp_Object
10627 redisplay_window_1 (window)
10628 Lisp_Object window;
10630 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10631 redisplay_window (window, 1);
10632 return Qnil;
10636 /* Increment GLYPH until it reaches END or CONDITION fails while
10637 adding (GLYPH)->pixel_width to X. */
10639 #define SKIP_GLYPHS(glyph, end, x, condition) \
10640 do \
10642 (x) += (glyph)->pixel_width; \
10643 ++(glyph); \
10645 while ((glyph) < (end) && (condition))
10648 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10649 DELTA is the number of bytes by which positions recorded in ROW
10650 differ from current buffer positions. */
10652 void
10653 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10654 struct window *w;
10655 struct glyph_row *row;
10656 struct glyph_matrix *matrix;
10657 int delta, delta_bytes, dy, dvpos;
10659 struct glyph *glyph = row->glyphs[TEXT_AREA];
10660 struct glyph *end = glyph + row->used[TEXT_AREA];
10661 struct glyph *cursor = NULL;
10662 /* The first glyph that starts a sequence of glyphs from string. */
10663 struct glyph *string_start;
10664 /* The X coordinate of string_start. */
10665 int string_start_x;
10666 /* The last known character position. */
10667 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10668 /* The last known character position before string_start. */
10669 int string_before_pos;
10670 int x = row->x;
10671 int cursor_x = x;
10672 int cursor_from_overlay_pos = 0;
10673 int pt_old = PT - delta;
10675 /* Skip over glyphs not having an object at the start of the row.
10676 These are special glyphs like truncation marks on terminal
10677 frames. */
10678 if (row->displays_text_p)
10679 while (glyph < end
10680 && INTEGERP (glyph->object)
10681 && glyph->charpos < 0)
10683 x += glyph->pixel_width;
10684 ++glyph;
10687 string_start = NULL;
10688 while (glyph < end
10689 && !INTEGERP (glyph->object)
10690 && (!BUFFERP (glyph->object)
10691 || (last_pos = glyph->charpos) < pt_old))
10693 if (! STRINGP (glyph->object))
10695 string_start = NULL;
10696 x += glyph->pixel_width;
10697 ++glyph;
10698 if (cursor_from_overlay_pos
10699 && last_pos > cursor_from_overlay_pos)
10701 cursor_from_overlay_pos = 0;
10702 cursor = 0;
10705 else
10707 string_before_pos = last_pos;
10708 string_start = glyph;
10709 string_start_x = x;
10710 /* Skip all glyphs from string. */
10713 int pos;
10714 if ((cursor == NULL || glyph > cursor)
10715 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10716 Qcursor, (glyph)->object))
10717 && (pos = string_buffer_position (w, glyph->object,
10718 string_before_pos),
10719 (pos == 0 /* From overlay */
10720 || pos == pt_old)))
10722 /* Estimate overlay buffer position from the buffer
10723 positions of the glyphs before and after the overlay.
10724 Add 1 to last_pos so that if point corresponds to the
10725 glyph right after the overlay, we still use a 'cursor'
10726 property found in that overlay. */
10727 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10728 cursor = glyph;
10729 cursor_x = x;
10731 x += glyph->pixel_width;
10732 ++glyph;
10734 while (glyph < end && STRINGP (glyph->object));
10738 if (cursor != NULL)
10740 glyph = cursor;
10741 x = cursor_x;
10743 else if (string_start
10744 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10746 /* We may have skipped over point because the previous glyphs
10747 are from string. As there's no easy way to know the
10748 character position of the current glyph, find the correct
10749 glyph on point by scanning from string_start again. */
10750 Lisp_Object limit;
10751 Lisp_Object string;
10752 int pos;
10754 limit = make_number (pt_old + 1);
10755 end = glyph;
10756 glyph = string_start;
10757 x = string_start_x;
10758 string = glyph->object;
10759 pos = string_buffer_position (w, string, string_before_pos);
10760 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10761 because we always put cursor after overlay strings. */
10762 while (pos == 0 && glyph < end)
10764 string = glyph->object;
10765 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10766 if (glyph < end)
10767 pos = string_buffer_position (w, glyph->object, string_before_pos);
10770 while (glyph < end)
10772 pos = XINT (Fnext_single_char_property_change
10773 (make_number (pos), Qdisplay, Qnil, limit));
10774 if (pos > pt_old)
10775 break;
10776 /* Skip glyphs from the same string. */
10777 string = glyph->object;
10778 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10779 /* Skip glyphs from an overlay. */
10780 while (glyph < end
10781 && ! string_buffer_position (w, glyph->object, pos))
10783 string = glyph->object;
10784 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10789 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10790 w->cursor.x = x;
10791 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10792 w->cursor.y = row->y + dy;
10794 if (w == XWINDOW (selected_window))
10796 if (!row->continued_p
10797 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10798 && row->x == 0)
10800 this_line_buffer = XBUFFER (w->buffer);
10802 CHARPOS (this_line_start_pos)
10803 = MATRIX_ROW_START_CHARPOS (row) + delta;
10804 BYTEPOS (this_line_start_pos)
10805 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10807 CHARPOS (this_line_end_pos)
10808 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10809 BYTEPOS (this_line_end_pos)
10810 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10812 this_line_y = w->cursor.y;
10813 this_line_pixel_height = row->height;
10814 this_line_vpos = w->cursor.vpos;
10815 this_line_start_x = row->x;
10817 else
10818 CHARPOS (this_line_start_pos) = 0;
10823 /* Run window scroll functions, if any, for WINDOW with new window
10824 start STARTP. Sets the window start of WINDOW to that position.
10826 We assume that the window's buffer is really current. */
10828 static INLINE struct text_pos
10829 run_window_scroll_functions (window, startp)
10830 Lisp_Object window;
10831 struct text_pos startp;
10833 struct window *w = XWINDOW (window);
10834 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10836 if (current_buffer != XBUFFER (w->buffer))
10837 abort ();
10839 if (!NILP (Vwindow_scroll_functions))
10841 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10842 make_number (CHARPOS (startp)));
10843 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10844 /* In case the hook functions switch buffers. */
10845 if (current_buffer != XBUFFER (w->buffer))
10846 set_buffer_internal_1 (XBUFFER (w->buffer));
10849 return startp;
10853 /* Make sure the line containing the cursor is fully visible.
10854 A value of 1 means there is nothing to be done.
10855 (Either the line is fully visible, or it cannot be made so,
10856 or we cannot tell.)
10858 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10859 is higher than window.
10861 A value of 0 means the caller should do scrolling
10862 as if point had gone off the screen. */
10864 static int
10865 make_cursor_line_fully_visible (w, force_p)
10866 struct window *w;
10867 int force_p;
10869 struct glyph_matrix *matrix;
10870 struct glyph_row *row;
10871 int window_height;
10873 /* It's not always possible to find the cursor, e.g, when a window
10874 is full of overlay strings. Don't do anything in that case. */
10875 if (w->cursor.vpos < 0)
10876 return 1;
10878 matrix = w->desired_matrix;
10879 row = MATRIX_ROW (matrix, w->cursor.vpos);
10881 /* If the cursor row is not partially visible, there's nothing to do. */
10882 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10883 return 1;
10885 /* If the row the cursor is in is taller than the window's height,
10886 it's not clear what to do, so do nothing. */
10887 window_height = window_box_height (w);
10888 if (row->height >= window_height)
10890 if (!force_p || w->vscroll)
10891 return 1;
10893 return 0;
10895 #if 0
10896 /* This code used to try to scroll the window just enough to make
10897 the line visible. It returned 0 to say that the caller should
10898 allocate larger glyph matrices. */
10900 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10902 int dy = row->height - row->visible_height;
10903 w->vscroll = 0;
10904 w->cursor.y += dy;
10905 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10907 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10909 int dy = - (row->height - row->visible_height);
10910 w->vscroll = dy;
10911 w->cursor.y += dy;
10912 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10915 /* When we change the cursor y-position of the selected window,
10916 change this_line_y as well so that the display optimization for
10917 the cursor line of the selected window in redisplay_internal uses
10918 the correct y-position. */
10919 if (w == XWINDOW (selected_window))
10920 this_line_y = w->cursor.y;
10922 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10923 redisplay with larger matrices. */
10924 if (matrix->nrows < required_matrix_height (w))
10926 fonts_changed_p = 1;
10927 return 0;
10930 return 1;
10931 #endif /* 0 */
10935 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10936 non-zero means only WINDOW is redisplayed in redisplay_internal.
10937 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10938 in redisplay_window to bring a partially visible line into view in
10939 the case that only the cursor has moved.
10941 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10942 last screen line's vertical height extends past the end of the screen.
10944 Value is
10946 1 if scrolling succeeded
10948 0 if scrolling didn't find point.
10950 -1 if new fonts have been loaded so that we must interrupt
10951 redisplay, adjust glyph matrices, and try again. */
10953 enum
10955 SCROLLING_SUCCESS,
10956 SCROLLING_FAILED,
10957 SCROLLING_NEED_LARGER_MATRICES
10960 static int
10961 try_scrolling (window, just_this_one_p, scroll_conservatively,
10962 scroll_step, temp_scroll_step, last_line_misfit)
10963 Lisp_Object window;
10964 int just_this_one_p;
10965 EMACS_INT scroll_conservatively, scroll_step;
10966 int temp_scroll_step;
10967 int last_line_misfit;
10969 struct window *w = XWINDOW (window);
10970 struct frame *f = XFRAME (w->frame);
10971 struct text_pos scroll_margin_pos;
10972 struct text_pos pos;
10973 struct text_pos startp;
10974 struct it it;
10975 Lisp_Object window_end;
10976 int this_scroll_margin;
10977 int dy = 0;
10978 int scroll_max;
10979 int rc;
10980 int amount_to_scroll = 0;
10981 Lisp_Object aggressive;
10982 int height;
10983 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10985 #if GLYPH_DEBUG
10986 debug_method_add (w, "try_scrolling");
10987 #endif
10989 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10991 /* Compute scroll margin height in pixels. We scroll when point is
10992 within this distance from the top or bottom of the window. */
10993 if (scroll_margin > 0)
10995 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10996 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10998 else
10999 this_scroll_margin = 0;
11001 /* Force scroll_conservatively to have a reasonable value so it doesn't
11002 cause an overflow while computing how much to scroll. */
11003 if (scroll_conservatively)
11004 scroll_conservatively = min (scroll_conservatively,
11005 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11007 /* Compute how much we should try to scroll maximally to bring point
11008 into view. */
11009 if (scroll_step || scroll_conservatively || temp_scroll_step)
11010 scroll_max = max (scroll_step,
11011 max (scroll_conservatively, temp_scroll_step));
11012 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11013 || NUMBERP (current_buffer->scroll_up_aggressively))
11014 /* We're trying to scroll because of aggressive scrolling
11015 but no scroll_step is set. Choose an arbitrary one. Maybe
11016 there should be a variable for this. */
11017 scroll_max = 10;
11018 else
11019 scroll_max = 0;
11020 scroll_max *= FRAME_LINE_HEIGHT (f);
11022 /* Decide whether we have to scroll down. Start at the window end
11023 and move this_scroll_margin up to find the position of the scroll
11024 margin. */
11025 window_end = Fwindow_end (window, Qt);
11027 too_near_end:
11029 CHARPOS (scroll_margin_pos) = XINT (window_end);
11030 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11032 if (this_scroll_margin || extra_scroll_margin_lines)
11034 start_display (&it, w, scroll_margin_pos);
11035 if (this_scroll_margin)
11036 move_it_vertically (&it, - this_scroll_margin);
11037 if (extra_scroll_margin_lines)
11038 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11039 scroll_margin_pos = it.current.pos;
11042 if (PT >= CHARPOS (scroll_margin_pos))
11044 int y0;
11046 /* Point is in the scroll margin at the bottom of the window, or
11047 below. Compute a new window start that makes point visible. */
11049 /* Compute the distance from the scroll margin to PT.
11050 Give up if the distance is greater than scroll_max. */
11051 start_display (&it, w, scroll_margin_pos);
11052 y0 = it.current_y;
11053 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11054 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11056 /* To make point visible, we have to move the window start
11057 down so that the line the cursor is in is visible, which
11058 means we have to add in the height of the cursor line. */
11059 dy = line_bottom_y (&it) - y0;
11061 if (dy > scroll_max)
11062 return SCROLLING_FAILED;
11064 /* Move the window start down. If scrolling conservatively,
11065 move it just enough down to make point visible. If
11066 scroll_step is set, move it down by scroll_step. */
11067 start_display (&it, w, startp);
11069 if (scroll_conservatively)
11070 /* Set AMOUNT_TO_SCROLL to at least one line,
11071 and at most scroll_conservatively lines. */
11072 amount_to_scroll
11073 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11074 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11075 else if (scroll_step || temp_scroll_step)
11076 amount_to_scroll = scroll_max;
11077 else
11079 aggressive = current_buffer->scroll_up_aggressively;
11080 height = WINDOW_BOX_TEXT_HEIGHT (w);
11081 if (NUMBERP (aggressive))
11083 double float_amount = XFLOATINT (aggressive) * height;
11084 amount_to_scroll = float_amount;
11085 if (amount_to_scroll == 0 && float_amount > 0)
11086 amount_to_scroll = 1;
11090 if (amount_to_scroll <= 0)
11091 return SCROLLING_FAILED;
11093 /* If moving by amount_to_scroll leaves STARTP unchanged,
11094 move it down one screen line. */
11096 move_it_vertically (&it, amount_to_scroll);
11097 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11098 move_it_by_lines (&it, 1, 1);
11099 startp = it.current.pos;
11101 else
11103 /* See if point is inside the scroll margin at the top of the
11104 window. */
11105 scroll_margin_pos = startp;
11106 if (this_scroll_margin)
11108 start_display (&it, w, startp);
11109 move_it_vertically (&it, this_scroll_margin);
11110 scroll_margin_pos = it.current.pos;
11113 if (PT < CHARPOS (scroll_margin_pos))
11115 /* Point is in the scroll margin at the top of the window or
11116 above what is displayed in the window. */
11117 int y0;
11119 /* Compute the vertical distance from PT to the scroll
11120 margin position. Give up if distance is greater than
11121 scroll_max. */
11122 SET_TEXT_POS (pos, PT, PT_BYTE);
11123 start_display (&it, w, pos);
11124 y0 = it.current_y;
11125 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11126 it.last_visible_y, -1,
11127 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11128 dy = it.current_y - y0;
11129 if (dy > scroll_max)
11130 return SCROLLING_FAILED;
11132 /* Compute new window start. */
11133 start_display (&it, w, startp);
11135 if (scroll_conservatively)
11136 amount_to_scroll
11137 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11138 else if (scroll_step || temp_scroll_step)
11139 amount_to_scroll = scroll_max;
11140 else
11142 aggressive = current_buffer->scroll_down_aggressively;
11143 height = WINDOW_BOX_TEXT_HEIGHT (w);
11144 if (NUMBERP (aggressive))
11146 double float_amount = XFLOATINT (aggressive) * height;
11147 amount_to_scroll = float_amount;
11148 if (amount_to_scroll == 0 && float_amount > 0)
11149 amount_to_scroll = 1;
11153 if (amount_to_scroll <= 0)
11154 return SCROLLING_FAILED;
11156 move_it_vertically (&it, - amount_to_scroll);
11157 startp = it.current.pos;
11161 /* Run window scroll functions. */
11162 startp = run_window_scroll_functions (window, startp);
11164 /* Display the window. Give up if new fonts are loaded, or if point
11165 doesn't appear. */
11166 if (!try_window (window, startp))
11167 rc = SCROLLING_NEED_LARGER_MATRICES;
11168 else if (w->cursor.vpos < 0)
11170 clear_glyph_matrix (w->desired_matrix);
11171 rc = SCROLLING_FAILED;
11173 else
11175 /* Maybe forget recorded base line for line number display. */
11176 if (!just_this_one_p
11177 || current_buffer->clip_changed
11178 || BEG_UNCHANGED < CHARPOS (startp))
11179 w->base_line_number = Qnil;
11181 /* If cursor ends up on a partially visible line,
11182 treat that as being off the bottom of the screen. */
11183 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11185 clear_glyph_matrix (w->desired_matrix);
11186 ++extra_scroll_margin_lines;
11187 goto too_near_end;
11189 rc = SCROLLING_SUCCESS;
11192 return rc;
11196 /* Compute a suitable window start for window W if display of W starts
11197 on a continuation line. Value is non-zero if a new window start
11198 was computed.
11200 The new window start will be computed, based on W's width, starting
11201 from the start of the continued line. It is the start of the
11202 screen line with the minimum distance from the old start W->start. */
11204 static int
11205 compute_window_start_on_continuation_line (w)
11206 struct window *w;
11208 struct text_pos pos, start_pos;
11209 int window_start_changed_p = 0;
11211 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11213 /* If window start is on a continuation line... Window start may be
11214 < BEGV in case there's invisible text at the start of the
11215 buffer (M-x rmail, for example). */
11216 if (CHARPOS (start_pos) > BEGV
11217 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11219 struct it it;
11220 struct glyph_row *row;
11222 /* Handle the case that the window start is out of range. */
11223 if (CHARPOS (start_pos) < BEGV)
11224 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11225 else if (CHARPOS (start_pos) > ZV)
11226 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11228 /* Find the start of the continued line. This should be fast
11229 because scan_buffer is fast (newline cache). */
11230 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11231 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11232 row, DEFAULT_FACE_ID);
11233 reseat_at_previous_visible_line_start (&it);
11235 /* If the line start is "too far" away from the window start,
11236 say it takes too much time to compute a new window start. */
11237 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11238 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11240 int min_distance, distance;
11242 /* Move forward by display lines to find the new window
11243 start. If window width was enlarged, the new start can
11244 be expected to be > the old start. If window width was
11245 decreased, the new window start will be < the old start.
11246 So, we're looking for the display line start with the
11247 minimum distance from the old window start. */
11248 pos = it.current.pos;
11249 min_distance = INFINITY;
11250 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11251 distance < min_distance)
11253 min_distance = distance;
11254 pos = it.current.pos;
11255 move_it_by_lines (&it, 1, 0);
11258 /* Set the window start there. */
11259 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11260 window_start_changed_p = 1;
11264 return window_start_changed_p;
11268 /* Try cursor movement in case text has not changed in window WINDOW,
11269 with window start STARTP. Value is
11271 CURSOR_MOVEMENT_SUCCESS if successful
11273 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11275 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11276 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11277 we want to scroll as if scroll-step were set to 1. See the code.
11279 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11280 which case we have to abort this redisplay, and adjust matrices
11281 first. */
11283 enum
11285 CURSOR_MOVEMENT_SUCCESS,
11286 CURSOR_MOVEMENT_CANNOT_BE_USED,
11287 CURSOR_MOVEMENT_MUST_SCROLL,
11288 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11291 static int
11292 try_cursor_movement (window, startp, scroll_step)
11293 Lisp_Object window;
11294 struct text_pos startp;
11295 int *scroll_step;
11297 struct window *w = XWINDOW (window);
11298 struct frame *f = XFRAME (w->frame);
11299 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11301 #if GLYPH_DEBUG
11302 if (inhibit_try_cursor_movement)
11303 return rc;
11304 #endif
11306 /* Handle case where text has not changed, only point, and it has
11307 not moved off the frame. */
11308 if (/* Point may be in this window. */
11309 PT >= CHARPOS (startp)
11310 /* Selective display hasn't changed. */
11311 && !current_buffer->clip_changed
11312 /* Function force-mode-line-update is used to force a thorough
11313 redisplay. It sets either windows_or_buffers_changed or
11314 update_mode_lines. So don't take a shortcut here for these
11315 cases. */
11316 && !update_mode_lines
11317 && !windows_or_buffers_changed
11318 && !cursor_type_changed
11319 /* Can't use this case if highlighting a region. When a
11320 region exists, cursor movement has to do more than just
11321 set the cursor. */
11322 && !(!NILP (Vtransient_mark_mode)
11323 && !NILP (current_buffer->mark_active))
11324 && NILP (w->region_showing)
11325 && NILP (Vshow_trailing_whitespace)
11326 /* Right after splitting windows, last_point may be nil. */
11327 && INTEGERP (w->last_point)
11328 /* This code is not used for mini-buffer for the sake of the case
11329 of redisplaying to replace an echo area message; since in
11330 that case the mini-buffer contents per se are usually
11331 unchanged. This code is of no real use in the mini-buffer
11332 since the handling of this_line_start_pos, etc., in redisplay
11333 handles the same cases. */
11334 && !EQ (window, minibuf_window)
11335 /* When splitting windows or for new windows, it happens that
11336 redisplay is called with a nil window_end_vpos or one being
11337 larger than the window. This should really be fixed in
11338 window.c. I don't have this on my list, now, so we do
11339 approximately the same as the old redisplay code. --gerd. */
11340 && INTEGERP (w->window_end_vpos)
11341 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11342 && (FRAME_WINDOW_P (f)
11343 || !overlay_arrow_in_current_buffer_p ()))
11345 int this_scroll_margin, top_scroll_margin;
11346 struct glyph_row *row = NULL;
11348 #if GLYPH_DEBUG
11349 debug_method_add (w, "cursor movement");
11350 #endif
11352 /* Scroll if point within this distance from the top or bottom
11353 of the window. This is a pixel value. */
11354 this_scroll_margin = max (0, scroll_margin);
11355 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11356 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11358 top_scroll_margin = this_scroll_margin;
11359 if (WINDOW_WANTS_HEADER_LINE_P (w))
11360 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11362 /* Start with the row the cursor was displayed during the last
11363 not paused redisplay. Give up if that row is not valid. */
11364 if (w->last_cursor.vpos < 0
11365 || w->last_cursor.vpos >= w->current_matrix->nrows)
11366 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11367 else
11369 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11370 if (row->mode_line_p)
11371 ++row;
11372 if (!row->enabled_p)
11373 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11376 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11378 int scroll_p = 0;
11379 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11381 if (PT > XFASTINT (w->last_point))
11383 /* Point has moved forward. */
11384 while (MATRIX_ROW_END_CHARPOS (row) < PT
11385 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11387 xassert (row->enabled_p);
11388 ++row;
11391 /* The end position of a row equals the start position
11392 of the next row. If PT is there, we would rather
11393 display it in the next line. */
11394 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11395 && MATRIX_ROW_END_CHARPOS (row) == PT
11396 && !cursor_row_p (w, row))
11397 ++row;
11399 /* If within the scroll margin, scroll. Note that
11400 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11401 the next line would be drawn, and that
11402 this_scroll_margin can be zero. */
11403 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11404 || PT > MATRIX_ROW_END_CHARPOS (row)
11405 /* Line is completely visible last line in window
11406 and PT is to be set in the next line. */
11407 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11408 && PT == MATRIX_ROW_END_CHARPOS (row)
11409 && !row->ends_at_zv_p
11410 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11411 scroll_p = 1;
11413 else if (PT < XFASTINT (w->last_point))
11415 /* Cursor has to be moved backward. Note that PT >=
11416 CHARPOS (startp) because of the outer if-statement. */
11417 while (!row->mode_line_p
11418 && (MATRIX_ROW_START_CHARPOS (row) > PT
11419 || (MATRIX_ROW_START_CHARPOS (row) == PT
11420 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11421 && (row->y > top_scroll_margin
11422 || CHARPOS (startp) == BEGV))
11424 xassert (row->enabled_p);
11425 --row;
11428 /* Consider the following case: Window starts at BEGV,
11429 there is invisible, intangible text at BEGV, so that
11430 display starts at some point START > BEGV. It can
11431 happen that we are called with PT somewhere between
11432 BEGV and START. Try to handle that case. */
11433 if (row < w->current_matrix->rows
11434 || row->mode_line_p)
11436 row = w->current_matrix->rows;
11437 if (row->mode_line_p)
11438 ++row;
11441 /* Due to newlines in overlay strings, we may have to
11442 skip forward over overlay strings. */
11443 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11444 && MATRIX_ROW_END_CHARPOS (row) == PT
11445 && !cursor_row_p (w, row))
11446 ++row;
11448 /* If within the scroll margin, scroll. */
11449 if (row->y < top_scroll_margin
11450 && CHARPOS (startp) != BEGV)
11451 scroll_p = 1;
11454 if (PT < MATRIX_ROW_START_CHARPOS (row)
11455 || PT > MATRIX_ROW_END_CHARPOS (row))
11457 /* if PT is not in the glyph row, give up. */
11458 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11460 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11462 if (PT == MATRIX_ROW_END_CHARPOS (row)
11463 && !row->ends_at_zv_p
11464 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11465 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11466 else if (row->height > window_box_height (w))
11468 /* If we end up in a partially visible line, let's
11469 make it fully visible, except when it's taller
11470 than the window, in which case we can't do much
11471 about it. */
11472 *scroll_step = 1;
11473 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11475 else
11477 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11478 if (!make_cursor_line_fully_visible (w, 0))
11479 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11480 else
11481 rc = CURSOR_MOVEMENT_SUCCESS;
11484 else if (scroll_p)
11485 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11486 else
11488 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11489 rc = CURSOR_MOVEMENT_SUCCESS;
11494 return rc;
11497 void
11498 set_vertical_scroll_bar (w)
11499 struct window *w;
11501 int start, end, whole;
11503 /* Calculate the start and end positions for the current window.
11504 At some point, it would be nice to choose between scrollbars
11505 which reflect the whole buffer size, with special markers
11506 indicating narrowing, and scrollbars which reflect only the
11507 visible region.
11509 Note that mini-buffers sometimes aren't displaying any text. */
11510 if (!MINI_WINDOW_P (w)
11511 || (w == XWINDOW (minibuf_window)
11512 && NILP (echo_area_buffer[0])))
11514 struct buffer *buf = XBUFFER (w->buffer);
11515 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11516 start = marker_position (w->start) - BUF_BEGV (buf);
11517 /* I don't think this is guaranteed to be right. For the
11518 moment, we'll pretend it is. */
11519 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11521 if (end < start)
11522 end = start;
11523 if (whole < (end - start))
11524 whole = end - start;
11526 else
11527 start = end = whole = 0;
11529 /* Indicate what this scroll bar ought to be displaying now. */
11530 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11534 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11535 selected_window is redisplayed.
11537 We can return without actually redisplaying the window if
11538 fonts_changed_p is nonzero. In that case, redisplay_internal will
11539 retry. */
11541 static void
11542 redisplay_window (window, just_this_one_p)
11543 Lisp_Object window;
11544 int just_this_one_p;
11546 struct window *w = XWINDOW (window);
11547 struct frame *f = XFRAME (w->frame);
11548 struct buffer *buffer = XBUFFER (w->buffer);
11549 struct buffer *old = current_buffer;
11550 struct text_pos lpoint, opoint, startp;
11551 int update_mode_line;
11552 int tem;
11553 struct it it;
11554 /* Record it now because it's overwritten. */
11555 int current_matrix_up_to_date_p = 0;
11556 int used_current_matrix_p = 0;
11557 /* This is less strict than current_matrix_up_to_date_p.
11558 It indictes that the buffer contents and narrowing are unchanged. */
11559 int buffer_unchanged_p = 0;
11560 int temp_scroll_step = 0;
11561 int count = SPECPDL_INDEX ();
11562 int rc;
11563 int centering_position;
11564 int last_line_misfit = 0;
11566 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11567 opoint = lpoint;
11569 /* W must be a leaf window here. */
11570 xassert (!NILP (w->buffer));
11571 #if GLYPH_DEBUG
11572 *w->desired_matrix->method = 0;
11573 #endif
11575 specbind (Qinhibit_point_motion_hooks, Qt);
11577 reconsider_clip_changes (w, buffer);
11579 /* Has the mode line to be updated? */
11580 update_mode_line = (!NILP (w->update_mode_line)
11581 || update_mode_lines
11582 || buffer->clip_changed
11583 || buffer->prevent_redisplay_optimizations_p);
11585 if (MINI_WINDOW_P (w))
11587 if (w == XWINDOW (echo_area_window)
11588 && !NILP (echo_area_buffer[0]))
11590 if (update_mode_line)
11591 /* We may have to update a tty frame's menu bar or a
11592 tool-bar. Example `M-x C-h C-h C-g'. */
11593 goto finish_menu_bars;
11594 else
11595 /* We've already displayed the echo area glyphs in this window. */
11596 goto finish_scroll_bars;
11598 else if ((w != XWINDOW (minibuf_window)
11599 || minibuf_level == 0)
11600 /* When buffer is nonempty, redisplay window normally. */
11601 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11602 /* Quail displays non-mini buffers in minibuffer window.
11603 In that case, redisplay the window normally. */
11604 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11606 /* W is a mini-buffer window, but it's not active, so clear
11607 it. */
11608 int yb = window_text_bottom_y (w);
11609 struct glyph_row *row;
11610 int y;
11612 for (y = 0, row = w->desired_matrix->rows;
11613 y < yb;
11614 y += row->height, ++row)
11615 blank_row (w, row, y);
11616 goto finish_scroll_bars;
11619 clear_glyph_matrix (w->desired_matrix);
11622 /* Otherwise set up data on this window; select its buffer and point
11623 value. */
11624 /* Really select the buffer, for the sake of buffer-local
11625 variables. */
11626 set_buffer_internal_1 (XBUFFER (w->buffer));
11627 SET_TEXT_POS (opoint, PT, PT_BYTE);
11629 current_matrix_up_to_date_p
11630 = (!NILP (w->window_end_valid)
11631 && !current_buffer->clip_changed
11632 && !current_buffer->prevent_redisplay_optimizations_p
11633 && XFASTINT (w->last_modified) >= MODIFF
11634 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11636 buffer_unchanged_p
11637 = (!NILP (w->window_end_valid)
11638 && !current_buffer->clip_changed
11639 && XFASTINT (w->last_modified) >= MODIFF
11640 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11642 /* When windows_or_buffers_changed is non-zero, we can't rely on
11643 the window end being valid, so set it to nil there. */
11644 if (windows_or_buffers_changed)
11646 /* If window starts on a continuation line, maybe adjust the
11647 window start in case the window's width changed. */
11648 if (XMARKER (w->start)->buffer == current_buffer)
11649 compute_window_start_on_continuation_line (w);
11651 w->window_end_valid = Qnil;
11654 /* Some sanity checks. */
11655 CHECK_WINDOW_END (w);
11656 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11657 abort ();
11658 if (BYTEPOS (opoint) < CHARPOS (opoint))
11659 abort ();
11661 /* If %c is in mode line, update it if needed. */
11662 if (!NILP (w->column_number_displayed)
11663 /* This alternative quickly identifies a common case
11664 where no change is needed. */
11665 && !(PT == XFASTINT (w->last_point)
11666 && XFASTINT (w->last_modified) >= MODIFF
11667 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11668 && (XFASTINT (w->column_number_displayed)
11669 != (int) current_column ())) /* iftc */
11670 update_mode_line = 1;
11672 /* Count number of windows showing the selected buffer. An indirect
11673 buffer counts as its base buffer. */
11674 if (!just_this_one_p)
11676 struct buffer *current_base, *window_base;
11677 current_base = current_buffer;
11678 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11679 if (current_base->base_buffer)
11680 current_base = current_base->base_buffer;
11681 if (window_base->base_buffer)
11682 window_base = window_base->base_buffer;
11683 if (current_base == window_base)
11684 buffer_shared++;
11687 /* Point refers normally to the selected window. For any other
11688 window, set up appropriate value. */
11689 if (!EQ (window, selected_window))
11691 int new_pt = XMARKER (w->pointm)->charpos;
11692 int new_pt_byte = marker_byte_position (w->pointm);
11693 if (new_pt < BEGV)
11695 new_pt = BEGV;
11696 new_pt_byte = BEGV_BYTE;
11697 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11699 else if (new_pt > (ZV - 1))
11701 new_pt = ZV;
11702 new_pt_byte = ZV_BYTE;
11703 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11706 /* We don't use SET_PT so that the point-motion hooks don't run. */
11707 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11710 /* If any of the character widths specified in the display table
11711 have changed, invalidate the width run cache. It's true that
11712 this may be a bit late to catch such changes, but the rest of
11713 redisplay goes (non-fatally) haywire when the display table is
11714 changed, so why should we worry about doing any better? */
11715 if (current_buffer->width_run_cache)
11717 struct Lisp_Char_Table *disptab = buffer_display_table ();
11719 if (! disptab_matches_widthtab (disptab,
11720 XVECTOR (current_buffer->width_table)))
11722 invalidate_region_cache (current_buffer,
11723 current_buffer->width_run_cache,
11724 BEG, Z);
11725 recompute_width_table (current_buffer, disptab);
11729 /* If window-start is screwed up, choose a new one. */
11730 if (XMARKER (w->start)->buffer != current_buffer)
11731 goto recenter;
11733 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11735 /* If someone specified a new starting point but did not insist,
11736 check whether it can be used. */
11737 if (!NILP (w->optional_new_start)
11738 && CHARPOS (startp) >= BEGV
11739 && CHARPOS (startp) <= ZV)
11741 w->optional_new_start = Qnil;
11742 start_display (&it, w, startp);
11743 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11744 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11745 if (IT_CHARPOS (it) == PT)
11746 w->force_start = Qt;
11747 /* IT may overshoot PT if text at PT is invisible. */
11748 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11749 w->force_start = Qt;
11754 /* Handle case where place to start displaying has been specified,
11755 unless the specified location is outside the accessible range. */
11756 if (!NILP (w->force_start)
11757 || w->frozen_window_start_p)
11759 /* We set this later on if we have to adjust point. */
11760 int new_vpos = -1;
11762 w->force_start = Qnil;
11763 w->vscroll = 0;
11764 w->window_end_valid = Qnil;
11766 /* Forget any recorded base line for line number display. */
11767 if (!buffer_unchanged_p)
11768 w->base_line_number = Qnil;
11770 /* Redisplay the mode line. Select the buffer properly for that.
11771 Also, run the hook window-scroll-functions
11772 because we have scrolled. */
11773 /* Note, we do this after clearing force_start because
11774 if there's an error, it is better to forget about force_start
11775 than to get into an infinite loop calling the hook functions
11776 and having them get more errors. */
11777 if (!update_mode_line
11778 || ! NILP (Vwindow_scroll_functions))
11780 update_mode_line = 1;
11781 w->update_mode_line = Qt;
11782 startp = run_window_scroll_functions (window, startp);
11785 w->last_modified = make_number (0);
11786 w->last_overlay_modified = make_number (0);
11787 if (CHARPOS (startp) < BEGV)
11788 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11789 else if (CHARPOS (startp) > ZV)
11790 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11792 /* Redisplay, then check if cursor has been set during the
11793 redisplay. Give up if new fonts were loaded. */
11794 if (!try_window (window, startp))
11796 w->force_start = Qt;
11797 clear_glyph_matrix (w->desired_matrix);
11798 goto need_larger_matrices;
11801 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11803 /* If point does not appear, try to move point so it does
11804 appear. The desired matrix has been built above, so we
11805 can use it here. */
11806 new_vpos = window_box_height (w) / 2;
11809 if (!make_cursor_line_fully_visible (w, 0))
11811 /* Point does appear, but on a line partly visible at end of window.
11812 Move it back to a fully-visible line. */
11813 new_vpos = window_box_height (w);
11816 /* If we need to move point for either of the above reasons,
11817 now actually do it. */
11818 if (new_vpos >= 0)
11820 struct glyph_row *row;
11822 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11823 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11824 ++row;
11826 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11827 MATRIX_ROW_START_BYTEPOS (row));
11829 if (w != XWINDOW (selected_window))
11830 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11831 else if (current_buffer == old)
11832 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11834 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11836 /* If we are highlighting the region, then we just changed
11837 the region, so redisplay to show it. */
11838 if (!NILP (Vtransient_mark_mode)
11839 && !NILP (current_buffer->mark_active))
11841 clear_glyph_matrix (w->desired_matrix);
11842 if (!try_window (window, startp))
11843 goto need_larger_matrices;
11847 #if GLYPH_DEBUG
11848 debug_method_add (w, "forced window start");
11849 #endif
11850 goto done;
11853 /* Handle case where text has not changed, only point, and it has
11854 not moved off the frame, and we are not retrying after hscroll.
11855 (current_matrix_up_to_date_p is nonzero when retrying.) */
11856 if (current_matrix_up_to_date_p
11857 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11858 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11860 switch (rc)
11862 case CURSOR_MOVEMENT_SUCCESS:
11863 used_current_matrix_p = 1;
11864 goto done;
11866 #if 0 /* try_cursor_movement never returns this value. */
11867 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11868 goto need_larger_matrices;
11869 #endif
11871 case CURSOR_MOVEMENT_MUST_SCROLL:
11872 goto try_to_scroll;
11874 default:
11875 abort ();
11878 /* If current starting point was originally the beginning of a line
11879 but no longer is, find a new starting point. */
11880 else if (!NILP (w->start_at_line_beg)
11881 && !(CHARPOS (startp) <= BEGV
11882 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11884 #if GLYPH_DEBUG
11885 debug_method_add (w, "recenter 1");
11886 #endif
11887 goto recenter;
11890 /* Try scrolling with try_window_id. Value is > 0 if update has
11891 been done, it is -1 if we know that the same window start will
11892 not work. It is 0 if unsuccessful for some other reason. */
11893 else if ((tem = try_window_id (w)) != 0)
11895 #if GLYPH_DEBUG
11896 debug_method_add (w, "try_window_id %d", tem);
11897 #endif
11899 if (fonts_changed_p)
11900 goto need_larger_matrices;
11901 if (tem > 0)
11902 goto done;
11904 /* Otherwise try_window_id has returned -1 which means that we
11905 don't want the alternative below this comment to execute. */
11907 else if (CHARPOS (startp) >= BEGV
11908 && CHARPOS (startp) <= ZV
11909 && PT >= CHARPOS (startp)
11910 && (CHARPOS (startp) < ZV
11911 /* Avoid starting at end of buffer. */
11912 || CHARPOS (startp) == BEGV
11913 || (XFASTINT (w->last_modified) >= MODIFF
11914 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11916 #if GLYPH_DEBUG
11917 debug_method_add (w, "same window start");
11918 #endif
11920 /* Try to redisplay starting at same place as before.
11921 If point has not moved off frame, accept the results. */
11922 if (!current_matrix_up_to_date_p
11923 /* Don't use try_window_reusing_current_matrix in this case
11924 because a window scroll function can have changed the
11925 buffer. */
11926 || !NILP (Vwindow_scroll_functions)
11927 || MINI_WINDOW_P (w)
11928 || !(used_current_matrix_p
11929 = try_window_reusing_current_matrix (w)))
11931 IF_DEBUG (debug_method_add (w, "1"));
11932 try_window (window, startp);
11935 if (fonts_changed_p)
11936 goto need_larger_matrices;
11938 if (w->cursor.vpos >= 0)
11940 if (!just_this_one_p
11941 || current_buffer->clip_changed
11942 || BEG_UNCHANGED < CHARPOS (startp))
11943 /* Forget any recorded base line for line number display. */
11944 w->base_line_number = Qnil;
11946 if (!make_cursor_line_fully_visible (w, 1))
11948 clear_glyph_matrix (w->desired_matrix);
11949 last_line_misfit = 1;
11951 /* Drop through and scroll. */
11952 else
11953 goto done;
11955 else
11956 clear_glyph_matrix (w->desired_matrix);
11959 try_to_scroll:
11961 w->last_modified = make_number (0);
11962 w->last_overlay_modified = make_number (0);
11964 /* Redisplay the mode line. Select the buffer properly for that. */
11965 if (!update_mode_line)
11967 update_mode_line = 1;
11968 w->update_mode_line = Qt;
11971 /* Try to scroll by specified few lines. */
11972 if ((scroll_conservatively
11973 || scroll_step
11974 || temp_scroll_step
11975 || NUMBERP (current_buffer->scroll_up_aggressively)
11976 || NUMBERP (current_buffer->scroll_down_aggressively))
11977 && !current_buffer->clip_changed
11978 && CHARPOS (startp) >= BEGV
11979 && CHARPOS (startp) <= ZV)
11981 /* The function returns -1 if new fonts were loaded, 1 if
11982 successful, 0 if not successful. */
11983 int rc = try_scrolling (window, just_this_one_p,
11984 scroll_conservatively,
11985 scroll_step,
11986 temp_scroll_step, last_line_misfit);
11987 switch (rc)
11989 case SCROLLING_SUCCESS:
11990 goto done;
11992 case SCROLLING_NEED_LARGER_MATRICES:
11993 goto need_larger_matrices;
11995 case SCROLLING_FAILED:
11996 break;
11998 default:
11999 abort ();
12003 /* Finally, just choose place to start which centers point */
12005 recenter:
12006 centering_position = window_box_height (w) / 2;
12008 point_at_top:
12009 /* Jump here with centering_position already set to 0. */
12011 #if GLYPH_DEBUG
12012 debug_method_add (w, "recenter");
12013 #endif
12015 /* w->vscroll = 0; */
12017 /* Forget any previously recorded base line for line number display. */
12018 if (!buffer_unchanged_p)
12019 w->base_line_number = Qnil;
12021 /* Move backward half the height of the window. */
12022 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12023 it.current_y = it.last_visible_y;
12024 move_it_vertically_backward (&it, centering_position);
12025 xassert (IT_CHARPOS (it) >= BEGV);
12027 /* The function move_it_vertically_backward may move over more
12028 than the specified y-distance. If it->w is small, e.g. a
12029 mini-buffer window, we may end up in front of the window's
12030 display area. Start displaying at the start of the line
12031 containing PT in this case. */
12032 if (it.current_y <= 0)
12034 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12035 move_it_vertically (&it, 0);
12036 xassert (IT_CHARPOS (it) <= PT);
12037 it.current_y = 0;
12040 it.current_x = it.hpos = 0;
12042 /* Set startp here explicitly in case that helps avoid an infinite loop
12043 in case the window-scroll-functions functions get errors. */
12044 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12046 /* Run scroll hooks. */
12047 startp = run_window_scroll_functions (window, it.current.pos);
12049 /* Redisplay the window. */
12050 if (!current_matrix_up_to_date_p
12051 || windows_or_buffers_changed
12052 || cursor_type_changed
12053 /* Don't use try_window_reusing_current_matrix in this case
12054 because it can have changed the buffer. */
12055 || !NILP (Vwindow_scroll_functions)
12056 || !just_this_one_p
12057 || MINI_WINDOW_P (w)
12058 || !(used_current_matrix_p
12059 = try_window_reusing_current_matrix (w)))
12060 try_window (window, startp);
12062 /* If new fonts have been loaded (due to fontsets), give up. We
12063 have to start a new redisplay since we need to re-adjust glyph
12064 matrices. */
12065 if (fonts_changed_p)
12066 goto need_larger_matrices;
12068 /* If cursor did not appear assume that the middle of the window is
12069 in the first line of the window. Do it again with the next line.
12070 (Imagine a window of height 100, displaying two lines of height
12071 60. Moving back 50 from it->last_visible_y will end in the first
12072 line.) */
12073 if (w->cursor.vpos < 0)
12075 if (!NILP (w->window_end_valid)
12076 && PT >= Z - XFASTINT (w->window_end_pos))
12078 clear_glyph_matrix (w->desired_matrix);
12079 move_it_by_lines (&it, 1, 0);
12080 try_window (window, it.current.pos);
12082 else if (PT < IT_CHARPOS (it))
12084 clear_glyph_matrix (w->desired_matrix);
12085 move_it_by_lines (&it, -1, 0);
12086 try_window (window, it.current.pos);
12088 else
12090 /* Not much we can do about it. */
12094 /* Consider the following case: Window starts at BEGV, there is
12095 invisible, intangible text at BEGV, so that display starts at
12096 some point START > BEGV. It can happen that we are called with
12097 PT somewhere between BEGV and START. Try to handle that case. */
12098 if (w->cursor.vpos < 0)
12100 struct glyph_row *row = w->current_matrix->rows;
12101 if (row->mode_line_p)
12102 ++row;
12103 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12106 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12108 /* If vscroll is enabled, disable it and try again. */
12109 if (w->vscroll)
12111 w->vscroll = 0;
12112 clear_glyph_matrix (w->desired_matrix);
12113 goto recenter;
12116 /* If centering point failed to make the whole line visible,
12117 put point at the top instead. That has to make the whole line
12118 visible, if it can be done. */
12119 clear_glyph_matrix (w->desired_matrix);
12120 centering_position = 0;
12121 goto point_at_top;
12124 done:
12126 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12127 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12128 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12129 ? Qt : Qnil);
12131 /* Display the mode line, if we must. */
12132 if ((update_mode_line
12133 /* If window not full width, must redo its mode line
12134 if (a) the window to its side is being redone and
12135 (b) we do a frame-based redisplay. This is a consequence
12136 of how inverted lines are drawn in frame-based redisplay. */
12137 || (!just_this_one_p
12138 && !FRAME_WINDOW_P (f)
12139 && !WINDOW_FULL_WIDTH_P (w))
12140 /* Line number to display. */
12141 || INTEGERP (w->base_line_pos)
12142 /* Column number is displayed and different from the one displayed. */
12143 || (!NILP (w->column_number_displayed)
12144 && (XFASTINT (w->column_number_displayed)
12145 != (int) current_column ()))) /* iftc */
12146 /* This means that the window has a mode line. */
12147 && (WINDOW_WANTS_MODELINE_P (w)
12148 || WINDOW_WANTS_HEADER_LINE_P (w)))
12150 display_mode_lines (w);
12152 /* If mode line height has changed, arrange for a thorough
12153 immediate redisplay using the correct mode line height. */
12154 if (WINDOW_WANTS_MODELINE_P (w)
12155 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12157 fonts_changed_p = 1;
12158 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12159 = DESIRED_MODE_LINE_HEIGHT (w);
12162 /* If top line height has changed, arrange for a thorough
12163 immediate redisplay using the correct mode line height. */
12164 if (WINDOW_WANTS_HEADER_LINE_P (w)
12165 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12167 fonts_changed_p = 1;
12168 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12169 = DESIRED_HEADER_LINE_HEIGHT (w);
12172 if (fonts_changed_p)
12173 goto need_larger_matrices;
12176 if (!line_number_displayed
12177 && !BUFFERP (w->base_line_pos))
12179 w->base_line_pos = Qnil;
12180 w->base_line_number = Qnil;
12183 finish_menu_bars:
12185 /* When we reach a frame's selected window, redo the frame's menu bar. */
12186 if (update_mode_line
12187 && EQ (FRAME_SELECTED_WINDOW (f), window))
12189 int redisplay_menu_p = 0;
12190 int redisplay_tool_bar_p = 0;
12192 if (FRAME_WINDOW_P (f))
12194 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12195 || defined (USE_GTK)
12196 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12197 #else
12198 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12199 #endif
12201 else
12202 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12204 if (redisplay_menu_p)
12205 display_menu_bar (w);
12207 #ifdef HAVE_WINDOW_SYSTEM
12208 #ifdef USE_GTK
12209 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12210 #else
12211 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12212 && (FRAME_TOOL_BAR_LINES (f) > 0
12213 || auto_resize_tool_bars_p);
12215 #endif
12217 if (redisplay_tool_bar_p)
12218 redisplay_tool_bar (f);
12219 #endif
12222 #ifdef HAVE_WINDOW_SYSTEM
12223 if (FRAME_WINDOW_P (f)
12224 && update_window_fringes (w, 0)
12225 && !just_this_one_p
12226 && (used_current_matrix_p || overlay_arrow_seen)
12227 && !w->pseudo_window_p)
12229 update_begin (f);
12230 BLOCK_INPUT;
12231 if (draw_window_fringes (w, 1))
12232 x_draw_vertical_border (w);
12233 UNBLOCK_INPUT;
12234 update_end (f);
12236 #endif /* HAVE_WINDOW_SYSTEM */
12238 /* We go to this label, with fonts_changed_p nonzero,
12239 if it is necessary to try again using larger glyph matrices.
12240 We have to redeem the scroll bar even in this case,
12241 because the loop in redisplay_internal expects that. */
12242 need_larger_matrices:
12244 finish_scroll_bars:
12246 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12248 /* Set the thumb's position and size. */
12249 set_vertical_scroll_bar (w);
12251 /* Note that we actually used the scroll bar attached to this
12252 window, so it shouldn't be deleted at the end of redisplay. */
12253 redeem_scroll_bar_hook (w);
12256 /* Restore current_buffer and value of point in it. */
12257 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12258 set_buffer_internal_1 (old);
12259 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12261 unbind_to (count, Qnil);
12265 /* Build the complete desired matrix of WINDOW with a window start
12266 buffer position POS. Value is non-zero if successful. It is zero
12267 if fonts were loaded during redisplay which makes re-adjusting
12268 glyph matrices necessary. */
12271 try_window (window, pos)
12272 Lisp_Object window;
12273 struct text_pos pos;
12275 struct window *w = XWINDOW (window);
12276 struct it it;
12277 struct glyph_row *last_text_row = NULL;
12279 /* Make POS the new window start. */
12280 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12282 /* Mark cursor position as unknown. No overlay arrow seen. */
12283 w->cursor.vpos = -1;
12284 overlay_arrow_seen = 0;
12286 /* Initialize iterator and info to start at POS. */
12287 start_display (&it, w, pos);
12289 /* Display all lines of W. */
12290 while (it.current_y < it.last_visible_y)
12292 if (display_line (&it))
12293 last_text_row = it.glyph_row - 1;
12294 if (fonts_changed_p)
12295 return 0;
12298 /* If bottom moved off end of frame, change mode line percentage. */
12299 if (XFASTINT (w->window_end_pos) <= 0
12300 && Z != IT_CHARPOS (it))
12301 w->update_mode_line = Qt;
12303 /* Set window_end_pos to the offset of the last character displayed
12304 on the window from the end of current_buffer. Set
12305 window_end_vpos to its row number. */
12306 if (last_text_row)
12308 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12309 w->window_end_bytepos
12310 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12311 w->window_end_pos
12312 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12313 w->window_end_vpos
12314 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12315 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12316 ->displays_text_p);
12318 else
12320 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12321 w->window_end_pos = make_number (Z - ZV);
12322 w->window_end_vpos = make_number (0);
12325 /* But that is not valid info until redisplay finishes. */
12326 w->window_end_valid = Qnil;
12327 return 1;
12332 /************************************************************************
12333 Window redisplay reusing current matrix when buffer has not changed
12334 ************************************************************************/
12336 /* Try redisplay of window W showing an unchanged buffer with a
12337 different window start than the last time it was displayed by
12338 reusing its current matrix. Value is non-zero if successful.
12339 W->start is the new window start. */
12341 static int
12342 try_window_reusing_current_matrix (w)
12343 struct window *w;
12345 struct frame *f = XFRAME (w->frame);
12346 struct glyph_row *row, *bottom_row;
12347 struct it it;
12348 struct run run;
12349 struct text_pos start, new_start;
12350 int nrows_scrolled, i;
12351 struct glyph_row *last_text_row;
12352 struct glyph_row *last_reused_text_row;
12353 struct glyph_row *start_row;
12354 int start_vpos, min_y, max_y;
12356 #if GLYPH_DEBUG
12357 if (inhibit_try_window_reusing)
12358 return 0;
12359 #endif
12361 if (/* This function doesn't handle terminal frames. */
12362 !FRAME_WINDOW_P (f)
12363 /* Don't try to reuse the display if windows have been split
12364 or such. */
12365 || windows_or_buffers_changed
12366 || cursor_type_changed)
12367 return 0;
12369 /* Can't do this if region may have changed. */
12370 if ((!NILP (Vtransient_mark_mode)
12371 && !NILP (current_buffer->mark_active))
12372 || !NILP (w->region_showing)
12373 || !NILP (Vshow_trailing_whitespace))
12374 return 0;
12376 /* If top-line visibility has changed, give up. */
12377 if (WINDOW_WANTS_HEADER_LINE_P (w)
12378 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12379 return 0;
12381 /* Give up if old or new display is scrolled vertically. We could
12382 make this function handle this, but right now it doesn't. */
12383 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12384 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12385 return 0;
12387 /* The variable new_start now holds the new window start. The old
12388 start `start' can be determined from the current matrix. */
12389 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12390 start = start_row->start.pos;
12391 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12393 /* Clear the desired matrix for the display below. */
12394 clear_glyph_matrix (w->desired_matrix);
12396 if (CHARPOS (new_start) <= CHARPOS (start))
12398 int first_row_y;
12400 /* Don't use this method if the display starts with an ellipsis
12401 displayed for invisible text. It's not easy to handle that case
12402 below, and it's certainly not worth the effort since this is
12403 not a frequent case. */
12404 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12405 return 0;
12407 IF_DEBUG (debug_method_add (w, "twu1"));
12409 /* Display up to a row that can be reused. The variable
12410 last_text_row is set to the last row displayed that displays
12411 text. Note that it.vpos == 0 if or if not there is a
12412 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12413 start_display (&it, w, new_start);
12414 first_row_y = it.current_y;
12415 w->cursor.vpos = -1;
12416 last_text_row = last_reused_text_row = NULL;
12418 while (it.current_y < it.last_visible_y
12419 && !fonts_changed_p)
12421 /* If we have reached into the characters in the START row,
12422 that means the line boundaries have changed. So we
12423 can't start copying with the row START. Maybe it will
12424 work to start copying with the following row. */
12425 while (IT_CHARPOS (it) > CHARPOS (start))
12427 /* Advance to the next row as the "start". */
12428 start_row++;
12429 start = start_row->start.pos;
12430 /* If there are no more rows to try, or just one, give up. */
12431 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12432 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row)
12433 || CHARPOS (start) == ZV)
12435 clear_glyph_matrix (w->desired_matrix);
12436 return 0;
12439 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12441 /* If we have reached alignment,
12442 we can copy the rest of the rows. */
12443 if (IT_CHARPOS (it) == CHARPOS (start))
12444 break;
12446 if (display_line (&it))
12447 last_text_row = it.glyph_row - 1;
12450 /* A value of current_y < last_visible_y means that we stopped
12451 at the previous window start, which in turn means that we
12452 have at least one reusable row. */
12453 if (it.current_y < it.last_visible_y)
12455 /* IT.vpos always starts from 0; it counts text lines. */
12456 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12458 /* Find PT if not already found in the lines displayed. */
12459 if (w->cursor.vpos < 0)
12461 int dy = it.current_y - start_row->y;
12463 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12464 row = row_containing_pos (w, PT, row, NULL, dy);
12465 if (row)
12466 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12467 dy, nrows_scrolled);
12468 else
12470 clear_glyph_matrix (w->desired_matrix);
12471 return 0;
12475 /* Scroll the display. Do it before the current matrix is
12476 changed. The problem here is that update has not yet
12477 run, i.e. part of the current matrix is not up to date.
12478 scroll_run_hook will clear the cursor, and use the
12479 current matrix to get the height of the row the cursor is
12480 in. */
12481 run.current_y = start_row->y;
12482 run.desired_y = it.current_y;
12483 run.height = it.last_visible_y - it.current_y;
12485 if (run.height > 0 && run.current_y != run.desired_y)
12487 update_begin (f);
12488 rif->update_window_begin_hook (w);
12489 rif->clear_window_mouse_face (w);
12490 rif->scroll_run_hook (w, &run);
12491 rif->update_window_end_hook (w, 0, 0);
12492 update_end (f);
12495 /* Shift current matrix down by nrows_scrolled lines. */
12496 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12497 rotate_matrix (w->current_matrix,
12498 start_vpos,
12499 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12500 nrows_scrolled);
12502 /* Disable lines that must be updated. */
12503 for (i = 0; i < it.vpos; ++i)
12504 (start_row + i)->enabled_p = 0;
12506 /* Re-compute Y positions. */
12507 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12508 max_y = it.last_visible_y;
12509 for (row = start_row + nrows_scrolled;
12510 row < bottom_row;
12511 ++row)
12513 row->y = it.current_y;
12514 row->visible_height = row->height;
12516 if (row->y < min_y)
12517 row->visible_height -= min_y - row->y;
12518 if (row->y + row->height > max_y)
12519 row->visible_height -= row->y + row->height - max_y;
12520 row->redraw_fringe_bitmaps_p = 1;
12522 it.current_y += row->height;
12524 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12525 last_reused_text_row = row;
12526 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12527 break;
12530 /* Disable lines in the current matrix which are now
12531 below the window. */
12532 for (++row; row < bottom_row; ++row)
12533 row->enabled_p = 0;
12536 /* Update window_end_pos etc.; last_reused_text_row is the last
12537 reused row from the current matrix containing text, if any.
12538 The value of last_text_row is the last displayed line
12539 containing text. */
12540 if (last_reused_text_row)
12542 w->window_end_bytepos
12543 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12544 w->window_end_pos
12545 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12546 w->window_end_vpos
12547 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12548 w->current_matrix));
12550 else if (last_text_row)
12552 w->window_end_bytepos
12553 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12554 w->window_end_pos
12555 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12556 w->window_end_vpos
12557 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12559 else
12561 /* This window must be completely empty. */
12562 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12563 w->window_end_pos = make_number (Z - ZV);
12564 w->window_end_vpos = make_number (0);
12566 w->window_end_valid = Qnil;
12568 /* Update hint: don't try scrolling again in update_window. */
12569 w->desired_matrix->no_scrolling_p = 1;
12571 #if GLYPH_DEBUG
12572 debug_method_add (w, "try_window_reusing_current_matrix 1");
12573 #endif
12574 return 1;
12576 else if (CHARPOS (new_start) > CHARPOS (start))
12578 struct glyph_row *pt_row, *row;
12579 struct glyph_row *first_reusable_row;
12580 struct glyph_row *first_row_to_display;
12581 int dy;
12582 int yb = window_text_bottom_y (w);
12584 /* Find the row starting at new_start, if there is one. Don't
12585 reuse a partially visible line at the end. */
12586 first_reusable_row = start_row;
12587 while (first_reusable_row->enabled_p
12588 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12589 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12590 < CHARPOS (new_start)))
12591 ++first_reusable_row;
12593 /* Give up if there is no row to reuse. */
12594 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12595 || !first_reusable_row->enabled_p
12596 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12597 != CHARPOS (new_start)))
12598 return 0;
12600 /* We can reuse fully visible rows beginning with
12601 first_reusable_row to the end of the window. Set
12602 first_row_to_display to the first row that cannot be reused.
12603 Set pt_row to the row containing point, if there is any. */
12604 pt_row = NULL;
12605 for (first_row_to_display = first_reusable_row;
12606 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12607 ++first_row_to_display)
12609 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12610 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12611 pt_row = first_row_to_display;
12614 /* Start displaying at the start of first_row_to_display. */
12615 xassert (first_row_to_display->y < yb);
12616 init_to_row_start (&it, w, first_row_to_display);
12618 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12619 - start_vpos);
12620 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12621 - nrows_scrolled);
12622 it.current_y = (first_row_to_display->y - first_reusable_row->y
12623 + WINDOW_HEADER_LINE_HEIGHT (w));
12625 /* Display lines beginning with first_row_to_display in the
12626 desired matrix. Set last_text_row to the last row displayed
12627 that displays text. */
12628 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12629 if (pt_row == NULL)
12630 w->cursor.vpos = -1;
12631 last_text_row = NULL;
12632 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12633 if (display_line (&it))
12634 last_text_row = it.glyph_row - 1;
12636 /* Give up If point isn't in a row displayed or reused. */
12637 if (w->cursor.vpos < 0)
12639 clear_glyph_matrix (w->desired_matrix);
12640 return 0;
12643 /* If point is in a reused row, adjust y and vpos of the cursor
12644 position. */
12645 if (pt_row)
12647 w->cursor.vpos -= nrows_scrolled;
12648 w->cursor.y -= first_reusable_row->y - start_row->y;
12651 /* Scroll the display. */
12652 run.current_y = first_reusable_row->y;
12653 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12654 run.height = it.last_visible_y - run.current_y;
12655 dy = run.current_y - run.desired_y;
12657 if (run.height)
12659 update_begin (f);
12660 rif->update_window_begin_hook (w);
12661 rif->clear_window_mouse_face (w);
12662 rif->scroll_run_hook (w, &run);
12663 rif->update_window_end_hook (w, 0, 0);
12664 update_end (f);
12667 /* Adjust Y positions of reused rows. */
12668 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12669 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12670 max_y = it.last_visible_y;
12671 for (row = first_reusable_row; row < first_row_to_display; ++row)
12673 row->y -= dy;
12674 row->visible_height = row->height;
12675 if (row->y < min_y)
12676 row->visible_height -= min_y - row->y;
12677 if (row->y + row->height > max_y)
12678 row->visible_height -= row->y + row->height - max_y;
12679 row->redraw_fringe_bitmaps_p = 1;
12682 /* Scroll the current matrix. */
12683 xassert (nrows_scrolled > 0);
12684 rotate_matrix (w->current_matrix,
12685 start_vpos,
12686 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12687 -nrows_scrolled);
12689 /* Disable rows not reused. */
12690 for (row -= nrows_scrolled; row < bottom_row; ++row)
12691 row->enabled_p = 0;
12693 /* Point may have moved to a different line, so we cannot assume that
12694 the previous cursor position is valid; locate the correct row. */
12695 if (pt_row)
12697 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12698 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12699 row++)
12701 w->cursor.vpos++;
12702 w->cursor.y = row->y;
12704 if (row < bottom_row)
12706 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12707 while (glyph->charpos < PT)
12709 w->cursor.hpos++;
12710 w->cursor.x += glyph->pixel_width;
12711 glyph++;
12716 /* Adjust window end. A null value of last_text_row means that
12717 the window end is in reused rows which in turn means that
12718 only its vpos can have changed. */
12719 if (last_text_row)
12721 w->window_end_bytepos
12722 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12723 w->window_end_pos
12724 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12725 w->window_end_vpos
12726 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12728 else
12730 w->window_end_vpos
12731 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12734 w->window_end_valid = Qnil;
12735 w->desired_matrix->no_scrolling_p = 1;
12737 #if GLYPH_DEBUG
12738 debug_method_add (w, "try_window_reusing_current_matrix 2");
12739 #endif
12740 return 1;
12743 return 0;
12748 /************************************************************************
12749 Window redisplay reusing current matrix when buffer has changed
12750 ************************************************************************/
12752 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12753 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12754 int *, int *));
12755 static struct glyph_row *
12756 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12757 struct glyph_row *));
12760 /* Return the last row in MATRIX displaying text. If row START is
12761 non-null, start searching with that row. IT gives the dimensions
12762 of the display. Value is null if matrix is empty; otherwise it is
12763 a pointer to the row found. */
12765 static struct glyph_row *
12766 find_last_row_displaying_text (matrix, it, start)
12767 struct glyph_matrix *matrix;
12768 struct it *it;
12769 struct glyph_row *start;
12771 struct glyph_row *row, *row_found;
12773 /* Set row_found to the last row in IT->w's current matrix
12774 displaying text. The loop looks funny but think of partially
12775 visible lines. */
12776 row_found = NULL;
12777 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12778 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12780 xassert (row->enabled_p);
12781 row_found = row;
12782 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12783 break;
12784 ++row;
12787 return row_found;
12791 /* Return the last row in the current matrix of W that is not affected
12792 by changes at the start of current_buffer that occurred since W's
12793 current matrix was built. Value is null if no such row exists.
12795 BEG_UNCHANGED us the number of characters unchanged at the start of
12796 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12797 first changed character in current_buffer. Characters at positions <
12798 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12799 when the current matrix was built. */
12801 static struct glyph_row *
12802 find_last_unchanged_at_beg_row (w)
12803 struct window *w;
12805 int first_changed_pos = BEG + BEG_UNCHANGED;
12806 struct glyph_row *row;
12807 struct glyph_row *row_found = NULL;
12808 int yb = window_text_bottom_y (w);
12810 /* Find the last row displaying unchanged text. */
12811 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12812 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12813 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12815 if (/* If row ends before first_changed_pos, it is unchanged,
12816 except in some case. */
12817 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12818 /* When row ends in ZV and we write at ZV it is not
12819 unchanged. */
12820 && !row->ends_at_zv_p
12821 /* When first_changed_pos is the end of a continued line,
12822 row is not unchanged because it may be no longer
12823 continued. */
12824 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12825 && (row->continued_p
12826 || row->exact_window_width_line_p)))
12827 row_found = row;
12829 /* Stop if last visible row. */
12830 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12831 break;
12833 ++row;
12836 return row_found;
12840 /* Find the first glyph row in the current matrix of W that is not
12841 affected by changes at the end of current_buffer since the
12842 time W's current matrix was built.
12844 Return in *DELTA the number of chars by which buffer positions in
12845 unchanged text at the end of current_buffer must be adjusted.
12847 Return in *DELTA_BYTES the corresponding number of bytes.
12849 Value is null if no such row exists, i.e. all rows are affected by
12850 changes. */
12852 static struct glyph_row *
12853 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12854 struct window *w;
12855 int *delta, *delta_bytes;
12857 struct glyph_row *row;
12858 struct glyph_row *row_found = NULL;
12860 *delta = *delta_bytes = 0;
12862 /* Display must not have been paused, otherwise the current matrix
12863 is not up to date. */
12864 if (NILP (w->window_end_valid))
12865 abort ();
12867 /* A value of window_end_pos >= END_UNCHANGED means that the window
12868 end is in the range of changed text. If so, there is no
12869 unchanged row at the end of W's current matrix. */
12870 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12871 return NULL;
12873 /* Set row to the last row in W's current matrix displaying text. */
12874 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12876 /* If matrix is entirely empty, no unchanged row exists. */
12877 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12879 /* The value of row is the last glyph row in the matrix having a
12880 meaningful buffer position in it. The end position of row
12881 corresponds to window_end_pos. This allows us to translate
12882 buffer positions in the current matrix to current buffer
12883 positions for characters not in changed text. */
12884 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12885 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12886 int last_unchanged_pos, last_unchanged_pos_old;
12887 struct glyph_row *first_text_row
12888 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12890 *delta = Z - Z_old;
12891 *delta_bytes = Z_BYTE - Z_BYTE_old;
12893 /* Set last_unchanged_pos to the buffer position of the last
12894 character in the buffer that has not been changed. Z is the
12895 index + 1 of the last character in current_buffer, i.e. by
12896 subtracting END_UNCHANGED we get the index of the last
12897 unchanged character, and we have to add BEG to get its buffer
12898 position. */
12899 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12900 last_unchanged_pos_old = last_unchanged_pos - *delta;
12902 /* Search backward from ROW for a row displaying a line that
12903 starts at a minimum position >= last_unchanged_pos_old. */
12904 for (; row > first_text_row; --row)
12906 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12907 abort ();
12909 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12910 row_found = row;
12914 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12915 abort ();
12917 return row_found;
12921 /* Make sure that glyph rows in the current matrix of window W
12922 reference the same glyph memory as corresponding rows in the
12923 frame's frame matrix. This function is called after scrolling W's
12924 current matrix on a terminal frame in try_window_id and
12925 try_window_reusing_current_matrix. */
12927 static void
12928 sync_frame_with_window_matrix_rows (w)
12929 struct window *w;
12931 struct frame *f = XFRAME (w->frame);
12932 struct glyph_row *window_row, *window_row_end, *frame_row;
12934 /* Preconditions: W must be a leaf window and full-width. Its frame
12935 must have a frame matrix. */
12936 xassert (NILP (w->hchild) && NILP (w->vchild));
12937 xassert (WINDOW_FULL_WIDTH_P (w));
12938 xassert (!FRAME_WINDOW_P (f));
12940 /* If W is a full-width window, glyph pointers in W's current matrix
12941 have, by definition, to be the same as glyph pointers in the
12942 corresponding frame matrix. Note that frame matrices have no
12943 marginal areas (see build_frame_matrix). */
12944 window_row = w->current_matrix->rows;
12945 window_row_end = window_row + w->current_matrix->nrows;
12946 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12947 while (window_row < window_row_end)
12949 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12950 struct glyph *end = window_row->glyphs[LAST_AREA];
12952 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12953 frame_row->glyphs[TEXT_AREA] = start;
12954 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12955 frame_row->glyphs[LAST_AREA] = end;
12957 /* Disable frame rows whose corresponding window rows have
12958 been disabled in try_window_id. */
12959 if (!window_row->enabled_p)
12960 frame_row->enabled_p = 0;
12962 ++window_row, ++frame_row;
12967 /* Find the glyph row in window W containing CHARPOS. Consider all
12968 rows between START and END (not inclusive). END null means search
12969 all rows to the end of the display area of W. Value is the row
12970 containing CHARPOS or null. */
12972 struct glyph_row *
12973 row_containing_pos (w, charpos, start, end, dy)
12974 struct window *w;
12975 int charpos;
12976 struct glyph_row *start, *end;
12977 int dy;
12979 struct glyph_row *row = start;
12980 int last_y;
12982 /* If we happen to start on a header-line, skip that. */
12983 if (row->mode_line_p)
12984 ++row;
12986 if ((end && row >= end) || !row->enabled_p)
12987 return NULL;
12989 last_y = window_text_bottom_y (w) - dy;
12991 while (1)
12993 /* Give up if we have gone too far. */
12994 if (end && row >= end)
12995 return NULL;
12996 /* This formerly returned if they were equal.
12997 I think that both quantities are of a "last plus one" type;
12998 if so, when they are equal, the row is within the screen. -- rms. */
12999 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13000 return NULL;
13002 /* If it is in this row, return this row. */
13003 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13004 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13005 /* The end position of a row equals the start
13006 position of the next row. If CHARPOS is there, we
13007 would rather display it in the next line, except
13008 when this line ends in ZV. */
13009 && !row->ends_at_zv_p
13010 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13011 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13012 return row;
13013 ++row;
13018 /* Try to redisplay window W by reusing its existing display. W's
13019 current matrix must be up to date when this function is called,
13020 i.e. window_end_valid must not be nil.
13022 Value is
13024 1 if display has been updated
13025 0 if otherwise unsuccessful
13026 -1 if redisplay with same window start is known not to succeed
13028 The following steps are performed:
13030 1. Find the last row in the current matrix of W that is not
13031 affected by changes at the start of current_buffer. If no such row
13032 is found, give up.
13034 2. Find the first row in W's current matrix that is not affected by
13035 changes at the end of current_buffer. Maybe there is no such row.
13037 3. Display lines beginning with the row + 1 found in step 1 to the
13038 row found in step 2 or, if step 2 didn't find a row, to the end of
13039 the window.
13041 4. If cursor is not known to appear on the window, give up.
13043 5. If display stopped at the row found in step 2, scroll the
13044 display and current matrix as needed.
13046 6. Maybe display some lines at the end of W, if we must. This can
13047 happen under various circumstances, like a partially visible line
13048 becoming fully visible, or because newly displayed lines are displayed
13049 in smaller font sizes.
13051 7. Update W's window end information. */
13053 static int
13054 try_window_id (w)
13055 struct window *w;
13057 struct frame *f = XFRAME (w->frame);
13058 struct glyph_matrix *current_matrix = w->current_matrix;
13059 struct glyph_matrix *desired_matrix = w->desired_matrix;
13060 struct glyph_row *last_unchanged_at_beg_row;
13061 struct glyph_row *first_unchanged_at_end_row;
13062 struct glyph_row *row;
13063 struct glyph_row *bottom_row;
13064 int bottom_vpos;
13065 struct it it;
13066 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13067 struct text_pos start_pos;
13068 struct run run;
13069 int first_unchanged_at_end_vpos = 0;
13070 struct glyph_row *last_text_row, *last_text_row_at_end;
13071 struct text_pos start;
13072 int first_changed_charpos, last_changed_charpos;
13074 #if GLYPH_DEBUG
13075 if (inhibit_try_window_id)
13076 return 0;
13077 #endif
13079 /* This is handy for debugging. */
13080 #if 0
13081 #define GIVE_UP(X) \
13082 do { \
13083 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13084 return 0; \
13085 } while (0)
13086 #else
13087 #define GIVE_UP(X) return 0
13088 #endif
13090 SET_TEXT_POS_FROM_MARKER (start, w->start);
13092 /* Don't use this for mini-windows because these can show
13093 messages and mini-buffers, and we don't handle that here. */
13094 if (MINI_WINDOW_P (w))
13095 GIVE_UP (1);
13097 /* This flag is used to prevent redisplay optimizations. */
13098 if (windows_or_buffers_changed || cursor_type_changed)
13099 GIVE_UP (2);
13101 /* Verify that narrowing has not changed.
13102 Also verify that we were not told to prevent redisplay optimizations.
13103 It would be nice to further
13104 reduce the number of cases where this prevents try_window_id. */
13105 if (current_buffer->clip_changed
13106 || current_buffer->prevent_redisplay_optimizations_p)
13107 GIVE_UP (3);
13109 /* Window must either use window-based redisplay or be full width. */
13110 if (!FRAME_WINDOW_P (f)
13111 && (!line_ins_del_ok
13112 || !WINDOW_FULL_WIDTH_P (w)))
13113 GIVE_UP (4);
13115 /* Give up if point is not known NOT to appear in W. */
13116 if (PT < CHARPOS (start))
13117 GIVE_UP (5);
13119 /* Another way to prevent redisplay optimizations. */
13120 if (XFASTINT (w->last_modified) == 0)
13121 GIVE_UP (6);
13123 /* Verify that window is not hscrolled. */
13124 if (XFASTINT (w->hscroll) != 0)
13125 GIVE_UP (7);
13127 /* Verify that display wasn't paused. */
13128 if (NILP (w->window_end_valid))
13129 GIVE_UP (8);
13131 /* Can't use this if highlighting a region because a cursor movement
13132 will do more than just set the cursor. */
13133 if (!NILP (Vtransient_mark_mode)
13134 && !NILP (current_buffer->mark_active))
13135 GIVE_UP (9);
13137 /* Likewise if highlighting trailing whitespace. */
13138 if (!NILP (Vshow_trailing_whitespace))
13139 GIVE_UP (11);
13141 /* Likewise if showing a region. */
13142 if (!NILP (w->region_showing))
13143 GIVE_UP (10);
13145 /* Can use this if overlay arrow position and or string have changed. */
13146 if (overlay_arrows_changed_p ())
13147 GIVE_UP (12);
13150 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13151 only if buffer has really changed. The reason is that the gap is
13152 initially at Z for freshly visited files. The code below would
13153 set end_unchanged to 0 in that case. */
13154 if (MODIFF > SAVE_MODIFF
13155 /* This seems to happen sometimes after saving a buffer. */
13156 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13158 if (GPT - BEG < BEG_UNCHANGED)
13159 BEG_UNCHANGED = GPT - BEG;
13160 if (Z - GPT < END_UNCHANGED)
13161 END_UNCHANGED = Z - GPT;
13164 /* The position of the first and last character that has been changed. */
13165 first_changed_charpos = BEG + BEG_UNCHANGED;
13166 last_changed_charpos = Z - END_UNCHANGED;
13168 /* If window starts after a line end, and the last change is in
13169 front of that newline, then changes don't affect the display.
13170 This case happens with stealth-fontification. Note that although
13171 the display is unchanged, glyph positions in the matrix have to
13172 be adjusted, of course. */
13173 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13174 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13175 && ((last_changed_charpos < CHARPOS (start)
13176 && CHARPOS (start) == BEGV)
13177 || (last_changed_charpos < CHARPOS (start) - 1
13178 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13180 int Z_old, delta, Z_BYTE_old, delta_bytes;
13181 struct glyph_row *r0;
13183 /* Compute how many chars/bytes have been added to or removed
13184 from the buffer. */
13185 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13186 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13187 delta = Z - Z_old;
13188 delta_bytes = Z_BYTE - Z_BYTE_old;
13190 /* Give up if PT is not in the window. Note that it already has
13191 been checked at the start of try_window_id that PT is not in
13192 front of the window start. */
13193 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13194 GIVE_UP (13);
13196 /* If window start is unchanged, we can reuse the whole matrix
13197 as is, after adjusting glyph positions. No need to compute
13198 the window end again, since its offset from Z hasn't changed. */
13199 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13200 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13201 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13202 /* PT must not be in a partially visible line. */
13203 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13204 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13206 /* Adjust positions in the glyph matrix. */
13207 if (delta || delta_bytes)
13209 struct glyph_row *r1
13210 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13211 increment_matrix_positions (w->current_matrix,
13212 MATRIX_ROW_VPOS (r0, current_matrix),
13213 MATRIX_ROW_VPOS (r1, current_matrix),
13214 delta, delta_bytes);
13217 /* Set the cursor. */
13218 row = row_containing_pos (w, PT, r0, NULL, 0);
13219 if (row)
13220 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13221 else
13222 abort ();
13223 return 1;
13227 /* Handle the case that changes are all below what is displayed in
13228 the window, and that PT is in the window. This shortcut cannot
13229 be taken if ZV is visible in the window, and text has been added
13230 there that is visible in the window. */
13231 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13232 /* ZV is not visible in the window, or there are no
13233 changes at ZV, actually. */
13234 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13235 || first_changed_charpos == last_changed_charpos))
13237 struct glyph_row *r0;
13239 /* Give up if PT is not in the window. Note that it already has
13240 been checked at the start of try_window_id that PT is not in
13241 front of the window start. */
13242 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13243 GIVE_UP (14);
13245 /* If window start is unchanged, we can reuse the whole matrix
13246 as is, without changing glyph positions since no text has
13247 been added/removed in front of the window end. */
13248 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13249 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13250 /* PT must not be in a partially visible line. */
13251 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13252 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13254 /* We have to compute the window end anew since text
13255 can have been added/removed after it. */
13256 w->window_end_pos
13257 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13258 w->window_end_bytepos
13259 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13261 /* Set the cursor. */
13262 row = row_containing_pos (w, PT, r0, NULL, 0);
13263 if (row)
13264 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13265 else
13266 abort ();
13267 return 2;
13271 /* Give up if window start is in the changed area.
13273 The condition used to read
13275 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13277 but why that was tested escapes me at the moment. */
13278 if (CHARPOS (start) >= first_changed_charpos
13279 && CHARPOS (start) <= last_changed_charpos)
13280 GIVE_UP (15);
13282 /* Check that window start agrees with the start of the first glyph
13283 row in its current matrix. Check this after we know the window
13284 start is not in changed text, otherwise positions would not be
13285 comparable. */
13286 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13287 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13288 GIVE_UP (16);
13290 /* Give up if the window ends in strings. Overlay strings
13291 at the end are difficult to handle, so don't try. */
13292 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13293 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13294 GIVE_UP (20);
13296 /* Compute the position at which we have to start displaying new
13297 lines. Some of the lines at the top of the window might be
13298 reusable because they are not displaying changed text. Find the
13299 last row in W's current matrix not affected by changes at the
13300 start of current_buffer. Value is null if changes start in the
13301 first line of window. */
13302 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13303 if (last_unchanged_at_beg_row)
13305 /* Avoid starting to display in the moddle of a character, a TAB
13306 for instance. This is easier than to set up the iterator
13307 exactly, and it's not a frequent case, so the additional
13308 effort wouldn't really pay off. */
13309 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13310 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13311 && last_unchanged_at_beg_row > w->current_matrix->rows)
13312 --last_unchanged_at_beg_row;
13314 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13315 GIVE_UP (17);
13317 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13318 GIVE_UP (18);
13319 start_pos = it.current.pos;
13321 /* Start displaying new lines in the desired matrix at the same
13322 vpos we would use in the current matrix, i.e. below
13323 last_unchanged_at_beg_row. */
13324 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13325 current_matrix);
13326 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13327 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13329 xassert (it.hpos == 0 && it.current_x == 0);
13331 else
13333 /* There are no reusable lines at the start of the window.
13334 Start displaying in the first text line. */
13335 start_display (&it, w, start);
13336 it.vpos = it.first_vpos;
13337 start_pos = it.current.pos;
13340 /* Find the first row that is not affected by changes at the end of
13341 the buffer. Value will be null if there is no unchanged row, in
13342 which case we must redisplay to the end of the window. delta
13343 will be set to the value by which buffer positions beginning with
13344 first_unchanged_at_end_row have to be adjusted due to text
13345 changes. */
13346 first_unchanged_at_end_row
13347 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13348 IF_DEBUG (debug_delta = delta);
13349 IF_DEBUG (debug_delta_bytes = delta_bytes);
13351 /* Set stop_pos to the buffer position up to which we will have to
13352 display new lines. If first_unchanged_at_end_row != NULL, this
13353 is the buffer position of the start of the line displayed in that
13354 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13355 that we don't stop at a buffer position. */
13356 stop_pos = 0;
13357 if (first_unchanged_at_end_row)
13359 xassert (last_unchanged_at_beg_row == NULL
13360 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13362 /* If this is a continuation line, move forward to the next one
13363 that isn't. Changes in lines above affect this line.
13364 Caution: this may move first_unchanged_at_end_row to a row
13365 not displaying text. */
13366 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13367 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13368 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13369 < it.last_visible_y))
13370 ++first_unchanged_at_end_row;
13372 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13373 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13374 >= it.last_visible_y))
13375 first_unchanged_at_end_row = NULL;
13376 else
13378 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13379 + delta);
13380 first_unchanged_at_end_vpos
13381 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13382 xassert (stop_pos >= Z - END_UNCHANGED);
13385 else if (last_unchanged_at_beg_row == NULL)
13386 GIVE_UP (19);
13389 #if GLYPH_DEBUG
13391 /* Either there is no unchanged row at the end, or the one we have
13392 now displays text. This is a necessary condition for the window
13393 end pos calculation at the end of this function. */
13394 xassert (first_unchanged_at_end_row == NULL
13395 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13397 debug_last_unchanged_at_beg_vpos
13398 = (last_unchanged_at_beg_row
13399 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13400 : -1);
13401 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13403 #endif /* GLYPH_DEBUG != 0 */
13406 /* Display new lines. Set last_text_row to the last new line
13407 displayed which has text on it, i.e. might end up as being the
13408 line where the window_end_vpos is. */
13409 w->cursor.vpos = -1;
13410 last_text_row = NULL;
13411 overlay_arrow_seen = 0;
13412 while (it.current_y < it.last_visible_y
13413 && !fonts_changed_p
13414 && (first_unchanged_at_end_row == NULL
13415 || IT_CHARPOS (it) < stop_pos))
13417 if (display_line (&it))
13418 last_text_row = it.glyph_row - 1;
13421 if (fonts_changed_p)
13422 return -1;
13425 /* Compute differences in buffer positions, y-positions etc. for
13426 lines reused at the bottom of the window. Compute what we can
13427 scroll. */
13428 if (first_unchanged_at_end_row
13429 /* No lines reused because we displayed everything up to the
13430 bottom of the window. */
13431 && it.current_y < it.last_visible_y)
13433 dvpos = (it.vpos
13434 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13435 current_matrix));
13436 dy = it.current_y - first_unchanged_at_end_row->y;
13437 run.current_y = first_unchanged_at_end_row->y;
13438 run.desired_y = run.current_y + dy;
13439 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13441 else
13443 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13444 first_unchanged_at_end_row = NULL;
13446 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13449 /* Find the cursor if not already found. We have to decide whether
13450 PT will appear on this window (it sometimes doesn't, but this is
13451 not a very frequent case.) This decision has to be made before
13452 the current matrix is altered. A value of cursor.vpos < 0 means
13453 that PT is either in one of the lines beginning at
13454 first_unchanged_at_end_row or below the window. Don't care for
13455 lines that might be displayed later at the window end; as
13456 mentioned, this is not a frequent case. */
13457 if (w->cursor.vpos < 0)
13459 /* Cursor in unchanged rows at the top? */
13460 if (PT < CHARPOS (start_pos)
13461 && last_unchanged_at_beg_row)
13463 row = row_containing_pos (w, PT,
13464 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13465 last_unchanged_at_beg_row + 1, 0);
13466 if (row)
13467 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13470 /* Start from first_unchanged_at_end_row looking for PT. */
13471 else if (first_unchanged_at_end_row)
13473 row = row_containing_pos (w, PT - delta,
13474 first_unchanged_at_end_row, NULL, 0);
13475 if (row)
13476 set_cursor_from_row (w, row, w->current_matrix, delta,
13477 delta_bytes, dy, dvpos);
13480 /* Give up if cursor was not found. */
13481 if (w->cursor.vpos < 0)
13483 clear_glyph_matrix (w->desired_matrix);
13484 return -1;
13488 /* Don't let the cursor end in the scroll margins. */
13490 int this_scroll_margin, cursor_height;
13492 this_scroll_margin = max (0, scroll_margin);
13493 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13494 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13495 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13497 if ((w->cursor.y < this_scroll_margin
13498 && CHARPOS (start) > BEGV)
13499 /* Old redisplay didn't take scroll margin into account at the bottom,
13500 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13501 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
13503 w->cursor.vpos = -1;
13504 clear_glyph_matrix (w->desired_matrix);
13505 return -1;
13509 /* Scroll the display. Do it before changing the current matrix so
13510 that xterm.c doesn't get confused about where the cursor glyph is
13511 found. */
13512 if (dy && run.height)
13514 update_begin (f);
13516 if (FRAME_WINDOW_P (f))
13518 rif->update_window_begin_hook (w);
13519 rif->clear_window_mouse_face (w);
13520 rif->scroll_run_hook (w, &run);
13521 rif->update_window_end_hook (w, 0, 0);
13523 else
13525 /* Terminal frame. In this case, dvpos gives the number of
13526 lines to scroll by; dvpos < 0 means scroll up. */
13527 int first_unchanged_at_end_vpos
13528 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13529 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13530 int end = (WINDOW_TOP_EDGE_LINE (w)
13531 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13532 + window_internal_height (w));
13534 /* Perform the operation on the screen. */
13535 if (dvpos > 0)
13537 /* Scroll last_unchanged_at_beg_row to the end of the
13538 window down dvpos lines. */
13539 set_terminal_window (end);
13541 /* On dumb terminals delete dvpos lines at the end
13542 before inserting dvpos empty lines. */
13543 if (!scroll_region_ok)
13544 ins_del_lines (end - dvpos, -dvpos);
13546 /* Insert dvpos empty lines in front of
13547 last_unchanged_at_beg_row. */
13548 ins_del_lines (from, dvpos);
13550 else if (dvpos < 0)
13552 /* Scroll up last_unchanged_at_beg_vpos to the end of
13553 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13554 set_terminal_window (end);
13556 /* Delete dvpos lines in front of
13557 last_unchanged_at_beg_vpos. ins_del_lines will set
13558 the cursor to the given vpos and emit |dvpos| delete
13559 line sequences. */
13560 ins_del_lines (from + dvpos, dvpos);
13562 /* On a dumb terminal insert dvpos empty lines at the
13563 end. */
13564 if (!scroll_region_ok)
13565 ins_del_lines (end + dvpos, -dvpos);
13568 set_terminal_window (0);
13571 update_end (f);
13574 /* Shift reused rows of the current matrix to the right position.
13575 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13576 text. */
13577 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13578 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13579 if (dvpos < 0)
13581 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13582 bottom_vpos, dvpos);
13583 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13584 bottom_vpos, 0);
13586 else if (dvpos > 0)
13588 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13589 bottom_vpos, dvpos);
13590 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13591 first_unchanged_at_end_vpos + dvpos, 0);
13594 /* For frame-based redisplay, make sure that current frame and window
13595 matrix are in sync with respect to glyph memory. */
13596 if (!FRAME_WINDOW_P (f))
13597 sync_frame_with_window_matrix_rows (w);
13599 /* Adjust buffer positions in reused rows. */
13600 if (delta)
13601 increment_matrix_positions (current_matrix,
13602 first_unchanged_at_end_vpos + dvpos,
13603 bottom_vpos, delta, delta_bytes);
13605 /* Adjust Y positions. */
13606 if (dy)
13607 shift_glyph_matrix (w, current_matrix,
13608 first_unchanged_at_end_vpos + dvpos,
13609 bottom_vpos, dy);
13611 if (first_unchanged_at_end_row)
13612 first_unchanged_at_end_row += dvpos;
13614 /* If scrolling up, there may be some lines to display at the end of
13615 the window. */
13616 last_text_row_at_end = NULL;
13617 if (dy < 0)
13619 /* Scrolling up can leave for example a partially visible line
13620 at the end of the window to be redisplayed. */
13621 /* Set last_row to the glyph row in the current matrix where the
13622 window end line is found. It has been moved up or down in
13623 the matrix by dvpos. */
13624 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13625 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13627 /* If last_row is the window end line, it should display text. */
13628 xassert (last_row->displays_text_p);
13630 /* If window end line was partially visible before, begin
13631 displaying at that line. Otherwise begin displaying with the
13632 line following it. */
13633 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13635 init_to_row_start (&it, w, last_row);
13636 it.vpos = last_vpos;
13637 it.current_y = last_row->y;
13639 else
13641 init_to_row_end (&it, w, last_row);
13642 it.vpos = 1 + last_vpos;
13643 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13644 ++last_row;
13647 /* We may start in a continuation line. If so, we have to
13648 get the right continuation_lines_width and current_x. */
13649 it.continuation_lines_width = last_row->continuation_lines_width;
13650 it.hpos = it.current_x = 0;
13652 /* Display the rest of the lines at the window end. */
13653 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13654 while (it.current_y < it.last_visible_y
13655 && !fonts_changed_p)
13657 /* Is it always sure that the display agrees with lines in
13658 the current matrix? I don't think so, so we mark rows
13659 displayed invalid in the current matrix by setting their
13660 enabled_p flag to zero. */
13661 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13662 if (display_line (&it))
13663 last_text_row_at_end = it.glyph_row - 1;
13667 /* Update window_end_pos and window_end_vpos. */
13668 if (first_unchanged_at_end_row
13669 && first_unchanged_at_end_row->y < it.last_visible_y
13670 && !last_text_row_at_end)
13672 /* Window end line if one of the preserved rows from the current
13673 matrix. Set row to the last row displaying text in current
13674 matrix starting at first_unchanged_at_end_row, after
13675 scrolling. */
13676 xassert (first_unchanged_at_end_row->displays_text_p);
13677 row = find_last_row_displaying_text (w->current_matrix, &it,
13678 first_unchanged_at_end_row);
13679 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13681 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13682 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13683 w->window_end_vpos
13684 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13685 xassert (w->window_end_bytepos >= 0);
13686 IF_DEBUG (debug_method_add (w, "A"));
13688 else if (last_text_row_at_end)
13690 w->window_end_pos
13691 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13692 w->window_end_bytepos
13693 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13694 w->window_end_vpos
13695 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13696 xassert (w->window_end_bytepos >= 0);
13697 IF_DEBUG (debug_method_add (w, "B"));
13699 else if (last_text_row)
13701 /* We have displayed either to the end of the window or at the
13702 end of the window, i.e. the last row with text is to be found
13703 in the desired matrix. */
13704 w->window_end_pos
13705 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13706 w->window_end_bytepos
13707 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13708 w->window_end_vpos
13709 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13710 xassert (w->window_end_bytepos >= 0);
13712 else if (first_unchanged_at_end_row == NULL
13713 && last_text_row == NULL
13714 && last_text_row_at_end == NULL)
13716 /* Displayed to end of window, but no line containing text was
13717 displayed. Lines were deleted at the end of the window. */
13718 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13719 int vpos = XFASTINT (w->window_end_vpos);
13720 struct glyph_row *current_row = current_matrix->rows + vpos;
13721 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13723 for (row = NULL;
13724 row == NULL && vpos >= first_vpos;
13725 --vpos, --current_row, --desired_row)
13727 if (desired_row->enabled_p)
13729 if (desired_row->displays_text_p)
13730 row = desired_row;
13732 else if (current_row->displays_text_p)
13733 row = current_row;
13736 xassert (row != NULL);
13737 w->window_end_vpos = make_number (vpos + 1);
13738 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13739 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13740 xassert (w->window_end_bytepos >= 0);
13741 IF_DEBUG (debug_method_add (w, "C"));
13743 else
13744 abort ();
13746 #if 0 /* This leads to problems, for instance when the cursor is
13747 at ZV, and the cursor line displays no text. */
13748 /* Disable rows below what's displayed in the window. This makes
13749 debugging easier. */
13750 enable_glyph_matrix_rows (current_matrix,
13751 XFASTINT (w->window_end_vpos) + 1,
13752 bottom_vpos, 0);
13753 #endif
13755 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13756 debug_end_vpos = XFASTINT (w->window_end_vpos));
13758 /* Record that display has not been completed. */
13759 w->window_end_valid = Qnil;
13760 w->desired_matrix->no_scrolling_p = 1;
13761 return 3;
13763 #undef GIVE_UP
13768 /***********************************************************************
13769 More debugging support
13770 ***********************************************************************/
13772 #if GLYPH_DEBUG
13774 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13775 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13776 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13779 /* Dump the contents of glyph matrix MATRIX on stderr.
13781 GLYPHS 0 means don't show glyph contents.
13782 GLYPHS 1 means show glyphs in short form
13783 GLYPHS > 1 means show glyphs in long form. */
13785 void
13786 dump_glyph_matrix (matrix, glyphs)
13787 struct glyph_matrix *matrix;
13788 int glyphs;
13790 int i;
13791 for (i = 0; i < matrix->nrows; ++i)
13792 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13796 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13797 the glyph row and area where the glyph comes from. */
13799 void
13800 dump_glyph (row, glyph, area)
13801 struct glyph_row *row;
13802 struct glyph *glyph;
13803 int area;
13805 if (glyph->type == CHAR_GLYPH)
13807 fprintf (stderr,
13808 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13809 glyph - row->glyphs[TEXT_AREA],
13810 'C',
13811 glyph->charpos,
13812 (BUFFERP (glyph->object)
13813 ? 'B'
13814 : (STRINGP (glyph->object)
13815 ? 'S'
13816 : '-')),
13817 glyph->pixel_width,
13818 glyph->u.ch,
13819 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13820 ? glyph->u.ch
13821 : '.'),
13822 glyph->face_id,
13823 glyph->left_box_line_p,
13824 glyph->right_box_line_p);
13826 else if (glyph->type == STRETCH_GLYPH)
13828 fprintf (stderr,
13829 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13830 glyph - row->glyphs[TEXT_AREA],
13831 'S',
13832 glyph->charpos,
13833 (BUFFERP (glyph->object)
13834 ? 'B'
13835 : (STRINGP (glyph->object)
13836 ? 'S'
13837 : '-')),
13838 glyph->pixel_width,
13840 '.',
13841 glyph->face_id,
13842 glyph->left_box_line_p,
13843 glyph->right_box_line_p);
13845 else if (glyph->type == IMAGE_GLYPH)
13847 fprintf (stderr,
13848 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13849 glyph - row->glyphs[TEXT_AREA],
13850 'I',
13851 glyph->charpos,
13852 (BUFFERP (glyph->object)
13853 ? 'B'
13854 : (STRINGP (glyph->object)
13855 ? 'S'
13856 : '-')),
13857 glyph->pixel_width,
13858 glyph->u.img_id,
13859 '.',
13860 glyph->face_id,
13861 glyph->left_box_line_p,
13862 glyph->right_box_line_p);
13867 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13868 GLYPHS 0 means don't show glyph contents.
13869 GLYPHS 1 means show glyphs in short form
13870 GLYPHS > 1 means show glyphs in long form. */
13872 void
13873 dump_glyph_row (row, vpos, glyphs)
13874 struct glyph_row *row;
13875 int vpos, glyphs;
13877 if (glyphs != 1)
13879 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13880 fprintf (stderr, "=======================================================================\n");
13882 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13883 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13884 vpos,
13885 MATRIX_ROW_START_CHARPOS (row),
13886 MATRIX_ROW_END_CHARPOS (row),
13887 row->used[TEXT_AREA],
13888 row->contains_overlapping_glyphs_p,
13889 row->enabled_p,
13890 row->truncated_on_left_p,
13891 row->truncated_on_right_p,
13892 row->overlay_arrow_p,
13893 row->continued_p,
13894 MATRIX_ROW_CONTINUATION_LINE_P (row),
13895 row->displays_text_p,
13896 row->ends_at_zv_p,
13897 row->fill_line_p,
13898 row->ends_in_middle_of_char_p,
13899 row->starts_in_middle_of_char_p,
13900 row->mouse_face_p,
13901 row->x,
13902 row->y,
13903 row->pixel_width,
13904 row->height,
13905 row->visible_height,
13906 row->ascent,
13907 row->phys_ascent);
13908 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13909 row->end.overlay_string_index,
13910 row->continuation_lines_width);
13911 fprintf (stderr, "%9d %5d\n",
13912 CHARPOS (row->start.string_pos),
13913 CHARPOS (row->end.string_pos));
13914 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13915 row->end.dpvec_index);
13918 if (glyphs > 1)
13920 int area;
13922 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13924 struct glyph *glyph = row->glyphs[area];
13925 struct glyph *glyph_end = glyph + row->used[area];
13927 /* Glyph for a line end in text. */
13928 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13929 ++glyph_end;
13931 if (glyph < glyph_end)
13932 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13934 for (; glyph < glyph_end; ++glyph)
13935 dump_glyph (row, glyph, area);
13938 else if (glyphs == 1)
13940 int area;
13942 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13944 char *s = (char *) alloca (row->used[area] + 1);
13945 int i;
13947 for (i = 0; i < row->used[area]; ++i)
13949 struct glyph *glyph = row->glyphs[area] + i;
13950 if (glyph->type == CHAR_GLYPH
13951 && glyph->u.ch < 0x80
13952 && glyph->u.ch >= ' ')
13953 s[i] = glyph->u.ch;
13954 else
13955 s[i] = '.';
13958 s[i] = '\0';
13959 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13965 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13966 Sdump_glyph_matrix, 0, 1, "p",
13967 doc: /* Dump the current matrix of the selected window to stderr.
13968 Shows contents of glyph row structures. With non-nil
13969 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13970 glyphs in short form, otherwise show glyphs in long form. */)
13971 (glyphs)
13972 Lisp_Object glyphs;
13974 struct window *w = XWINDOW (selected_window);
13975 struct buffer *buffer = XBUFFER (w->buffer);
13977 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13978 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13979 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13980 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13981 fprintf (stderr, "=============================================\n");
13982 dump_glyph_matrix (w->current_matrix,
13983 NILP (glyphs) ? 0 : XINT (glyphs));
13984 return Qnil;
13988 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13989 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13992 struct frame *f = XFRAME (selected_frame);
13993 dump_glyph_matrix (f->current_matrix, 1);
13994 return Qnil;
13998 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13999 doc: /* Dump glyph row ROW to stderr.
14000 GLYPH 0 means don't dump glyphs.
14001 GLYPH 1 means dump glyphs in short form.
14002 GLYPH > 1 or omitted means dump glyphs in long form. */)
14003 (row, glyphs)
14004 Lisp_Object row, glyphs;
14006 struct glyph_matrix *matrix;
14007 int vpos;
14009 CHECK_NUMBER (row);
14010 matrix = XWINDOW (selected_window)->current_matrix;
14011 vpos = XINT (row);
14012 if (vpos >= 0 && vpos < matrix->nrows)
14013 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14014 vpos,
14015 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14016 return Qnil;
14020 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14021 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14022 GLYPH 0 means don't dump glyphs.
14023 GLYPH 1 means dump glyphs in short form.
14024 GLYPH > 1 or omitted means dump glyphs in long form. */)
14025 (row, glyphs)
14026 Lisp_Object row, glyphs;
14028 struct frame *sf = SELECTED_FRAME ();
14029 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14030 int vpos;
14032 CHECK_NUMBER (row);
14033 vpos = XINT (row);
14034 if (vpos >= 0 && vpos < m->nrows)
14035 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14036 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14037 return Qnil;
14041 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14042 doc: /* Toggle tracing of redisplay.
14043 With ARG, turn tracing on if and only if ARG is positive. */)
14044 (arg)
14045 Lisp_Object arg;
14047 if (NILP (arg))
14048 trace_redisplay_p = !trace_redisplay_p;
14049 else
14051 arg = Fprefix_numeric_value (arg);
14052 trace_redisplay_p = XINT (arg) > 0;
14055 return Qnil;
14059 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14060 doc: /* Like `format', but print result to stderr.
14061 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14062 (nargs, args)
14063 int nargs;
14064 Lisp_Object *args;
14066 Lisp_Object s = Fformat (nargs, args);
14067 fprintf (stderr, "%s", SDATA (s));
14068 return Qnil;
14071 #endif /* GLYPH_DEBUG */
14075 /***********************************************************************
14076 Building Desired Matrix Rows
14077 ***********************************************************************/
14079 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14080 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14082 static struct glyph_row *
14083 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14084 struct window *w;
14085 Lisp_Object overlay_arrow_string;
14087 struct frame *f = XFRAME (WINDOW_FRAME (w));
14088 struct buffer *buffer = XBUFFER (w->buffer);
14089 struct buffer *old = current_buffer;
14090 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14091 int arrow_len = SCHARS (overlay_arrow_string);
14092 const unsigned char *arrow_end = arrow_string + arrow_len;
14093 const unsigned char *p;
14094 struct it it;
14095 int multibyte_p;
14096 int n_glyphs_before;
14098 set_buffer_temp (buffer);
14099 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14100 it.glyph_row->used[TEXT_AREA] = 0;
14101 SET_TEXT_POS (it.position, 0, 0);
14103 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14104 p = arrow_string;
14105 while (p < arrow_end)
14107 Lisp_Object face, ilisp;
14109 /* Get the next character. */
14110 if (multibyte_p)
14111 it.c = string_char_and_length (p, arrow_len, &it.len);
14112 else
14113 it.c = *p, it.len = 1;
14114 p += it.len;
14116 /* Get its face. */
14117 ilisp = make_number (p - arrow_string);
14118 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14119 it.face_id = compute_char_face (f, it.c, face);
14121 /* Compute its width, get its glyphs. */
14122 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14123 SET_TEXT_POS (it.position, -1, -1);
14124 PRODUCE_GLYPHS (&it);
14126 /* If this character doesn't fit any more in the line, we have
14127 to remove some glyphs. */
14128 if (it.current_x > it.last_visible_x)
14130 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14131 break;
14135 set_buffer_temp (old);
14136 return it.glyph_row;
14140 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14141 glyphs are only inserted for terminal frames since we can't really
14142 win with truncation glyphs when partially visible glyphs are
14143 involved. Which glyphs to insert is determined by
14144 produce_special_glyphs. */
14146 static void
14147 insert_left_trunc_glyphs (it)
14148 struct it *it;
14150 struct it truncate_it;
14151 struct glyph *from, *end, *to, *toend;
14153 xassert (!FRAME_WINDOW_P (it->f));
14155 /* Get the truncation glyphs. */
14156 truncate_it = *it;
14157 truncate_it.current_x = 0;
14158 truncate_it.face_id = DEFAULT_FACE_ID;
14159 truncate_it.glyph_row = &scratch_glyph_row;
14160 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14161 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14162 truncate_it.object = make_number (0);
14163 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14165 /* Overwrite glyphs from IT with truncation glyphs. */
14166 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14167 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14168 to = it->glyph_row->glyphs[TEXT_AREA];
14169 toend = to + it->glyph_row->used[TEXT_AREA];
14171 while (from < end)
14172 *to++ = *from++;
14174 /* There may be padding glyphs left over. Overwrite them too. */
14175 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14177 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14178 while (from < end)
14179 *to++ = *from++;
14182 if (to > toend)
14183 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14187 /* Compute the pixel height and width of IT->glyph_row.
14189 Most of the time, ascent and height of a display line will be equal
14190 to the max_ascent and max_height values of the display iterator
14191 structure. This is not the case if
14193 1. We hit ZV without displaying anything. In this case, max_ascent
14194 and max_height will be zero.
14196 2. We have some glyphs that don't contribute to the line height.
14197 (The glyph row flag contributes_to_line_height_p is for future
14198 pixmap extensions).
14200 The first case is easily covered by using default values because in
14201 these cases, the line height does not really matter, except that it
14202 must not be zero. */
14204 static void
14205 compute_line_metrics (it)
14206 struct it *it;
14208 struct glyph_row *row = it->glyph_row;
14209 int area, i;
14211 if (FRAME_WINDOW_P (it->f))
14213 int i, min_y, max_y;
14215 /* The line may consist of one space only, that was added to
14216 place the cursor on it. If so, the row's height hasn't been
14217 computed yet. */
14218 if (row->height == 0)
14220 if (it->max_ascent + it->max_descent == 0)
14221 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14222 row->ascent = it->max_ascent;
14223 row->height = it->max_ascent + it->max_descent;
14224 row->phys_ascent = it->max_phys_ascent;
14225 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14228 /* Compute the width of this line. */
14229 row->pixel_width = row->x;
14230 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14231 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14233 xassert (row->pixel_width >= 0);
14234 xassert (row->ascent >= 0 && row->height > 0);
14236 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14237 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14239 /* If first line's physical ascent is larger than its logical
14240 ascent, use the physical ascent, and make the row taller.
14241 This makes accented characters fully visible. */
14242 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14243 && row->phys_ascent > row->ascent)
14245 row->height += row->phys_ascent - row->ascent;
14246 row->ascent = row->phys_ascent;
14249 /* Compute how much of the line is visible. */
14250 row->visible_height = row->height;
14252 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14253 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14255 if (row->y < min_y)
14256 row->visible_height -= min_y - row->y;
14257 if (row->y + row->height > max_y)
14258 row->visible_height -= row->y + row->height - max_y;
14260 else
14262 row->pixel_width = row->used[TEXT_AREA];
14263 if (row->continued_p)
14264 row->pixel_width -= it->continuation_pixel_width;
14265 else if (row->truncated_on_right_p)
14266 row->pixel_width -= it->truncation_pixel_width;
14267 row->ascent = row->phys_ascent = 0;
14268 row->height = row->phys_height = row->visible_height = 1;
14271 /* Compute a hash code for this row. */
14272 row->hash = 0;
14273 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14274 for (i = 0; i < row->used[area]; ++i)
14275 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14276 + row->glyphs[area][i].u.val
14277 + row->glyphs[area][i].face_id
14278 + row->glyphs[area][i].padding_p
14279 + (row->glyphs[area][i].type << 2));
14281 it->max_ascent = it->max_descent = 0;
14282 it->max_phys_ascent = it->max_phys_descent = 0;
14286 /* Append one space to the glyph row of iterator IT if doing a
14287 window-based redisplay. The space has the same face as
14288 IT->face_id. Value is non-zero if a space was added.
14290 This function is called to make sure that there is always one glyph
14291 at the end of a glyph row that the cursor can be set on under
14292 window-systems. (If there weren't such a glyph we would not know
14293 how wide and tall a box cursor should be displayed).
14295 At the same time this space let's a nicely handle clearing to the
14296 end of the line if the row ends in italic text. */
14298 static int
14299 append_space_for_newline (it, default_face_p)
14300 struct it *it;
14301 int default_face_p;
14303 if (FRAME_WINDOW_P (it->f))
14305 int n = it->glyph_row->used[TEXT_AREA];
14307 if (it->glyph_row->glyphs[TEXT_AREA] + n
14308 < it->glyph_row->glyphs[1 + TEXT_AREA])
14310 /* Save some values that must not be changed.
14311 Must save IT->c and IT->len because otherwise
14312 ITERATOR_AT_END_P wouldn't work anymore after
14313 append_space_for_newline has been called. */
14314 enum display_element_type saved_what = it->what;
14315 int saved_c = it->c, saved_len = it->len;
14316 int saved_x = it->current_x;
14317 int saved_face_id = it->face_id;
14318 struct text_pos saved_pos;
14319 Lisp_Object saved_object;
14320 struct face *face;
14322 saved_object = it->object;
14323 saved_pos = it->position;
14325 it->what = IT_CHARACTER;
14326 bzero (&it->position, sizeof it->position);
14327 it->object = make_number (0);
14328 it->c = ' ';
14329 it->len = 1;
14331 if (default_face_p)
14332 it->face_id = DEFAULT_FACE_ID;
14333 else if (it->face_before_selective_p)
14334 it->face_id = it->saved_face_id;
14335 face = FACE_FROM_ID (it->f, it->face_id);
14336 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14338 PRODUCE_GLYPHS (it);
14340 it->override_ascent = -1;
14341 it->constrain_row_ascent_descent_p = 0;
14342 it->current_x = saved_x;
14343 it->object = saved_object;
14344 it->position = saved_pos;
14345 it->what = saved_what;
14346 it->face_id = saved_face_id;
14347 it->len = saved_len;
14348 it->c = saved_c;
14349 return 1;
14353 return 0;
14357 /* Extend the face of the last glyph in the text area of IT->glyph_row
14358 to the end of the display line. Called from display_line.
14359 If the glyph row is empty, add a space glyph to it so that we
14360 know the face to draw. Set the glyph row flag fill_line_p. */
14362 static void
14363 extend_face_to_end_of_line (it)
14364 struct it *it;
14366 struct face *face;
14367 struct frame *f = it->f;
14369 /* If line is already filled, do nothing. */
14370 if (it->current_x >= it->last_visible_x)
14371 return;
14373 /* Face extension extends the background and box of IT->face_id
14374 to the end of the line. If the background equals the background
14375 of the frame, we don't have to do anything. */
14376 if (it->face_before_selective_p)
14377 face = FACE_FROM_ID (it->f, it->saved_face_id);
14378 else
14379 face = FACE_FROM_ID (f, it->face_id);
14381 if (FRAME_WINDOW_P (f)
14382 && face->box == FACE_NO_BOX
14383 && face->background == FRAME_BACKGROUND_PIXEL (f)
14384 && !face->stipple)
14385 return;
14387 /* Set the glyph row flag indicating that the face of the last glyph
14388 in the text area has to be drawn to the end of the text area. */
14389 it->glyph_row->fill_line_p = 1;
14391 /* If current character of IT is not ASCII, make sure we have the
14392 ASCII face. This will be automatically undone the next time
14393 get_next_display_element returns a multibyte character. Note
14394 that the character will always be single byte in unibyte text. */
14395 if (!SINGLE_BYTE_CHAR_P (it->c))
14397 it->face_id = FACE_FOR_CHAR (f, face, 0);
14400 if (FRAME_WINDOW_P (f))
14402 /* If the row is empty, add a space with the current face of IT,
14403 so that we know which face to draw. */
14404 if (it->glyph_row->used[TEXT_AREA] == 0)
14406 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14407 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14408 it->glyph_row->used[TEXT_AREA] = 1;
14411 else
14413 /* Save some values that must not be changed. */
14414 int saved_x = it->current_x;
14415 struct text_pos saved_pos;
14416 Lisp_Object saved_object;
14417 enum display_element_type saved_what = it->what;
14418 int saved_face_id = it->face_id;
14420 saved_object = it->object;
14421 saved_pos = it->position;
14423 it->what = IT_CHARACTER;
14424 bzero (&it->position, sizeof it->position);
14425 it->object = make_number (0);
14426 it->c = ' ';
14427 it->len = 1;
14428 it->face_id = face->id;
14430 PRODUCE_GLYPHS (it);
14432 while (it->current_x <= it->last_visible_x)
14433 PRODUCE_GLYPHS (it);
14435 /* Don't count these blanks really. It would let us insert a left
14436 truncation glyph below and make us set the cursor on them, maybe. */
14437 it->current_x = saved_x;
14438 it->object = saved_object;
14439 it->position = saved_pos;
14440 it->what = saved_what;
14441 it->face_id = saved_face_id;
14446 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14447 trailing whitespace. */
14449 static int
14450 trailing_whitespace_p (charpos)
14451 int charpos;
14453 int bytepos = CHAR_TO_BYTE (charpos);
14454 int c = 0;
14456 while (bytepos < ZV_BYTE
14457 && (c = FETCH_CHAR (bytepos),
14458 c == ' ' || c == '\t'))
14459 ++bytepos;
14461 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14463 if (bytepos != PT_BYTE)
14464 return 1;
14466 return 0;
14470 /* Highlight trailing whitespace, if any, in ROW. */
14472 void
14473 highlight_trailing_whitespace (f, row)
14474 struct frame *f;
14475 struct glyph_row *row;
14477 int used = row->used[TEXT_AREA];
14479 if (used)
14481 struct glyph *start = row->glyphs[TEXT_AREA];
14482 struct glyph *glyph = start + used - 1;
14484 /* Skip over glyphs inserted to display the cursor at the
14485 end of a line, for extending the face of the last glyph
14486 to the end of the line on terminals, and for truncation
14487 and continuation glyphs. */
14488 while (glyph >= start
14489 && glyph->type == CHAR_GLYPH
14490 && INTEGERP (glyph->object))
14491 --glyph;
14493 /* If last glyph is a space or stretch, and it's trailing
14494 whitespace, set the face of all trailing whitespace glyphs in
14495 IT->glyph_row to `trailing-whitespace'. */
14496 if (glyph >= start
14497 && BUFFERP (glyph->object)
14498 && (glyph->type == STRETCH_GLYPH
14499 || (glyph->type == CHAR_GLYPH
14500 && glyph->u.ch == ' '))
14501 && trailing_whitespace_p (glyph->charpos))
14503 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14505 while (glyph >= start
14506 && BUFFERP (glyph->object)
14507 && (glyph->type == STRETCH_GLYPH
14508 || (glyph->type == CHAR_GLYPH
14509 && glyph->u.ch == ' ')))
14510 (glyph--)->face_id = face_id;
14516 /* Value is non-zero if glyph row ROW in window W should be
14517 used to hold the cursor. */
14519 static int
14520 cursor_row_p (w, row)
14521 struct window *w;
14522 struct glyph_row *row;
14524 int cursor_row_p = 1;
14526 if (PT == MATRIX_ROW_END_CHARPOS (row))
14528 /* If the row ends with a newline from a string, we don't want
14529 the cursor there (if the row is continued it doesn't end in a
14530 newline). */
14531 if (CHARPOS (row->end.string_pos) >= 0
14532 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14533 cursor_row_p = row->continued_p;
14535 /* If the row ends at ZV, display the cursor at the end of that
14536 row instead of at the start of the row below. */
14537 else if (row->ends_at_zv_p)
14538 cursor_row_p = 1;
14539 else
14540 cursor_row_p = 0;
14543 return cursor_row_p;
14547 /* Construct the glyph row IT->glyph_row in the desired matrix of
14548 IT->w from text at the current position of IT. See dispextern.h
14549 for an overview of struct it. Value is non-zero if
14550 IT->glyph_row displays text, as opposed to a line displaying ZV
14551 only. */
14553 static int
14554 display_line (it)
14555 struct it *it;
14557 struct glyph_row *row = it->glyph_row;
14558 int overlay_arrow_bitmap;
14559 Lisp_Object overlay_arrow_string;
14561 /* We always start displaying at hpos zero even if hscrolled. */
14562 xassert (it->hpos == 0 && it->current_x == 0);
14564 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14565 >= it->w->desired_matrix->nrows)
14567 it->w->nrows_scale_factor++;
14568 fonts_changed_p = 1;
14569 return 0;
14572 /* Is IT->w showing the region? */
14573 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14575 /* Clear the result glyph row and enable it. */
14576 prepare_desired_row (row);
14578 row->y = it->current_y;
14579 row->start = it->start;
14580 row->continuation_lines_width = it->continuation_lines_width;
14581 row->displays_text_p = 1;
14582 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14583 it->starts_in_middle_of_char_p = 0;
14585 /* Arrange the overlays nicely for our purposes. Usually, we call
14586 display_line on only one line at a time, in which case this
14587 can't really hurt too much, or we call it on lines which appear
14588 one after another in the buffer, in which case all calls to
14589 recenter_overlay_lists but the first will be pretty cheap. */
14590 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14592 /* Move over display elements that are not visible because we are
14593 hscrolled. This may stop at an x-position < IT->first_visible_x
14594 if the first glyph is partially visible or if we hit a line end. */
14595 if (it->current_x < it->first_visible_x)
14596 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14597 MOVE_TO_POS | MOVE_TO_X);
14599 /* Get the initial row height. This is either the height of the
14600 text hscrolled, if there is any, or zero. */
14601 row->ascent = it->max_ascent;
14602 row->height = it->max_ascent + it->max_descent;
14603 row->phys_ascent = it->max_phys_ascent;
14604 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14606 /* Loop generating characters. The loop is left with IT on the next
14607 character to display. */
14608 while (1)
14610 int n_glyphs_before, hpos_before, x_before;
14611 int x, i, nglyphs;
14612 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14614 /* Retrieve the next thing to display. Value is zero if end of
14615 buffer reached. */
14616 if (!get_next_display_element (it))
14618 /* Maybe add a space at the end of this line that is used to
14619 display the cursor there under X. Set the charpos of the
14620 first glyph of blank lines not corresponding to any text
14621 to -1. */
14622 #ifdef HAVE_WINDOW_SYSTEM
14623 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14624 row->exact_window_width_line_p = 1;
14625 else
14626 #endif /* HAVE_WINDOW_SYSTEM */
14627 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14628 || row->used[TEXT_AREA] == 0)
14630 row->glyphs[TEXT_AREA]->charpos = -1;
14631 row->displays_text_p = 0;
14633 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14634 && (!MINI_WINDOW_P (it->w)
14635 || (minibuf_level && EQ (it->window, minibuf_window))))
14636 row->indicate_empty_line_p = 1;
14639 it->continuation_lines_width = 0;
14640 row->ends_at_zv_p = 1;
14641 break;
14644 /* Now, get the metrics of what we want to display. This also
14645 generates glyphs in `row' (which is IT->glyph_row). */
14646 n_glyphs_before = row->used[TEXT_AREA];
14647 x = it->current_x;
14649 /* Remember the line height so far in case the next element doesn't
14650 fit on the line. */
14651 if (!it->truncate_lines_p)
14653 ascent = it->max_ascent;
14654 descent = it->max_descent;
14655 phys_ascent = it->max_phys_ascent;
14656 phys_descent = it->max_phys_descent;
14659 PRODUCE_GLYPHS (it);
14661 /* If this display element was in marginal areas, continue with
14662 the next one. */
14663 if (it->area != TEXT_AREA)
14665 row->ascent = max (row->ascent, it->max_ascent);
14666 row->height = max (row->height, it->max_ascent + it->max_descent);
14667 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14668 row->phys_height = max (row->phys_height,
14669 it->max_phys_ascent + it->max_phys_descent);
14670 set_iterator_to_next (it, 1);
14671 continue;
14674 /* Does the display element fit on the line? If we truncate
14675 lines, we should draw past the right edge of the window. If
14676 we don't truncate, we want to stop so that we can display the
14677 continuation glyph before the right margin. If lines are
14678 continued, there are two possible strategies for characters
14679 resulting in more than 1 glyph (e.g. tabs): Display as many
14680 glyphs as possible in this line and leave the rest for the
14681 continuation line, or display the whole element in the next
14682 line. Original redisplay did the former, so we do it also. */
14683 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14684 hpos_before = it->hpos;
14685 x_before = x;
14687 if (/* Not a newline. */
14688 nglyphs > 0
14689 /* Glyphs produced fit entirely in the line. */
14690 && it->current_x < it->last_visible_x)
14692 it->hpos += nglyphs;
14693 row->ascent = max (row->ascent, it->max_ascent);
14694 row->height = max (row->height, it->max_ascent + it->max_descent);
14695 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14696 row->phys_height = max (row->phys_height,
14697 it->max_phys_ascent + it->max_phys_descent);
14698 if (it->current_x - it->pixel_width < it->first_visible_x)
14699 row->x = x - it->first_visible_x;
14701 else
14703 int new_x;
14704 struct glyph *glyph;
14706 for (i = 0; i < nglyphs; ++i, x = new_x)
14708 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14709 new_x = x + glyph->pixel_width;
14711 if (/* Lines are continued. */
14712 !it->truncate_lines_p
14713 && (/* Glyph doesn't fit on the line. */
14714 new_x > it->last_visible_x
14715 /* Or it fits exactly on a window system frame. */
14716 || (new_x == it->last_visible_x
14717 && FRAME_WINDOW_P (it->f))))
14719 /* End of a continued line. */
14721 if (it->hpos == 0
14722 || (new_x == it->last_visible_x
14723 && FRAME_WINDOW_P (it->f)))
14725 /* Current glyph is the only one on the line or
14726 fits exactly on the line. We must continue
14727 the line because we can't draw the cursor
14728 after the glyph. */
14729 row->continued_p = 1;
14730 it->current_x = new_x;
14731 it->continuation_lines_width += new_x;
14732 ++it->hpos;
14733 if (i == nglyphs - 1)
14735 set_iterator_to_next (it, 1);
14736 #ifdef HAVE_WINDOW_SYSTEM
14737 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14739 if (!get_next_display_element (it))
14741 row->exact_window_width_line_p = 1;
14742 it->continuation_lines_width = 0;
14743 row->continued_p = 0;
14744 row->ends_at_zv_p = 1;
14746 else if (ITERATOR_AT_END_OF_LINE_P (it))
14748 row->continued_p = 0;
14749 row->exact_window_width_line_p = 1;
14752 #endif /* HAVE_WINDOW_SYSTEM */
14755 else if (CHAR_GLYPH_PADDING_P (*glyph)
14756 && !FRAME_WINDOW_P (it->f))
14758 /* A padding glyph that doesn't fit on this line.
14759 This means the whole character doesn't fit
14760 on the line. */
14761 row->used[TEXT_AREA] = n_glyphs_before;
14763 /* Fill the rest of the row with continuation
14764 glyphs like in 20.x. */
14765 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14766 < row->glyphs[1 + TEXT_AREA])
14767 produce_special_glyphs (it, IT_CONTINUATION);
14769 row->continued_p = 1;
14770 it->current_x = x_before;
14771 it->continuation_lines_width += x_before;
14773 /* Restore the height to what it was before the
14774 element not fitting on the line. */
14775 it->max_ascent = ascent;
14776 it->max_descent = descent;
14777 it->max_phys_ascent = phys_ascent;
14778 it->max_phys_descent = phys_descent;
14780 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14782 /* A TAB that extends past the right edge of the
14783 window. This produces a single glyph on
14784 window system frames. We leave the glyph in
14785 this row and let it fill the row, but don't
14786 consume the TAB. */
14787 it->continuation_lines_width += it->last_visible_x;
14788 row->ends_in_middle_of_char_p = 1;
14789 row->continued_p = 1;
14790 glyph->pixel_width = it->last_visible_x - x;
14791 it->starts_in_middle_of_char_p = 1;
14793 else
14795 /* Something other than a TAB that draws past
14796 the right edge of the window. Restore
14797 positions to values before the element. */
14798 row->used[TEXT_AREA] = n_glyphs_before + i;
14800 /* Display continuation glyphs. */
14801 if (!FRAME_WINDOW_P (it->f))
14802 produce_special_glyphs (it, IT_CONTINUATION);
14803 row->continued_p = 1;
14805 it->continuation_lines_width += x;
14807 if (nglyphs > 1 && i > 0)
14809 row->ends_in_middle_of_char_p = 1;
14810 it->starts_in_middle_of_char_p = 1;
14813 /* Restore the height to what it was before the
14814 element not fitting on the line. */
14815 it->max_ascent = ascent;
14816 it->max_descent = descent;
14817 it->max_phys_ascent = phys_ascent;
14818 it->max_phys_descent = phys_descent;
14821 break;
14823 else if (new_x > it->first_visible_x)
14825 /* Increment number of glyphs actually displayed. */
14826 ++it->hpos;
14828 if (x < it->first_visible_x)
14829 /* Glyph is partially visible, i.e. row starts at
14830 negative X position. */
14831 row->x = x - it->first_visible_x;
14833 else
14835 /* Glyph is completely off the left margin of the
14836 window. This should not happen because of the
14837 move_it_in_display_line at the start of this
14838 function, unless the text display area of the
14839 window is empty. */
14840 xassert (it->first_visible_x <= it->last_visible_x);
14844 row->ascent = max (row->ascent, it->max_ascent);
14845 row->height = max (row->height, it->max_ascent + it->max_descent);
14846 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14847 row->phys_height = max (row->phys_height,
14848 it->max_phys_ascent + it->max_phys_descent);
14850 /* End of this display line if row is continued. */
14851 if (row->continued_p || row->ends_at_zv_p)
14852 break;
14855 at_end_of_line:
14856 /* Is this a line end? If yes, we're also done, after making
14857 sure that a non-default face is extended up to the right
14858 margin of the window. */
14859 if (ITERATOR_AT_END_OF_LINE_P (it))
14861 int used_before = row->used[TEXT_AREA];
14863 row->ends_in_newline_from_string_p = STRINGP (it->object);
14865 #ifdef HAVE_WINDOW_SYSTEM
14866 /* Add a space at the end of the line that is used to
14867 display the cursor there. */
14868 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14869 append_space_for_newline (it, 0);
14870 #endif /* HAVE_WINDOW_SYSTEM */
14872 /* Extend the face to the end of the line. */
14873 extend_face_to_end_of_line (it);
14875 /* Make sure we have the position. */
14876 if (used_before == 0)
14877 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14879 /* Consume the line end. This skips over invisible lines. */
14880 set_iterator_to_next (it, 1);
14881 it->continuation_lines_width = 0;
14882 break;
14885 /* Proceed with next display element. Note that this skips
14886 over lines invisible because of selective display. */
14887 set_iterator_to_next (it, 1);
14889 /* If we truncate lines, we are done when the last displayed
14890 glyphs reach past the right margin of the window. */
14891 if (it->truncate_lines_p
14892 && (FRAME_WINDOW_P (it->f)
14893 ? (it->current_x >= it->last_visible_x)
14894 : (it->current_x > it->last_visible_x)))
14896 /* Maybe add truncation glyphs. */
14897 if (!FRAME_WINDOW_P (it->f))
14899 int i, n;
14901 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14902 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14903 break;
14905 for (n = row->used[TEXT_AREA]; i < n; ++i)
14907 row->used[TEXT_AREA] = i;
14908 produce_special_glyphs (it, IT_TRUNCATION);
14911 #ifdef HAVE_WINDOW_SYSTEM
14912 else
14914 /* Don't truncate if we can overflow newline into fringe. */
14915 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14917 if (!get_next_display_element (it))
14919 #ifdef HAVE_WINDOW_SYSTEM
14920 it->continuation_lines_width = 0;
14921 row->ends_at_zv_p = 1;
14922 row->exact_window_width_line_p = 1;
14923 break;
14924 #endif /* HAVE_WINDOW_SYSTEM */
14926 if (ITERATOR_AT_END_OF_LINE_P (it))
14928 row->exact_window_width_line_p = 1;
14929 goto at_end_of_line;
14933 #endif /* HAVE_WINDOW_SYSTEM */
14935 row->truncated_on_right_p = 1;
14936 it->continuation_lines_width = 0;
14937 reseat_at_next_visible_line_start (it, 0);
14938 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14939 it->hpos = hpos_before;
14940 it->current_x = x_before;
14941 break;
14945 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14946 at the left window margin. */
14947 if (it->first_visible_x
14948 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14950 if (!FRAME_WINDOW_P (it->f))
14951 insert_left_trunc_glyphs (it);
14952 row->truncated_on_left_p = 1;
14955 /* If the start of this line is the overlay arrow-position, then
14956 mark this glyph row as the one containing the overlay arrow.
14957 This is clearly a mess with variable size fonts. It would be
14958 better to let it be displayed like cursors under X. */
14959 if (! overlay_arrow_seen
14960 && (overlay_arrow_string
14961 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
14962 !NILP (overlay_arrow_string)))
14964 /* Overlay arrow in window redisplay is a fringe bitmap. */
14965 if (STRINGP (overlay_arrow_string))
14967 struct glyph_row *arrow_row
14968 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14969 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14970 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14971 struct glyph *p = row->glyphs[TEXT_AREA];
14972 struct glyph *p2, *end;
14974 /* Copy the arrow glyphs. */
14975 while (glyph < arrow_end)
14976 *p++ = *glyph++;
14978 /* Throw away padding glyphs. */
14979 p2 = p;
14980 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14981 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14982 ++p2;
14983 if (p2 > p)
14985 while (p2 < end)
14986 *p++ = *p2++;
14987 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14990 else
14992 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14993 row->overlay_arrow_p = 1;
14995 overlay_arrow_seen = 1;
14998 /* Compute pixel dimensions of this line. */
14999 compute_line_metrics (it);
15001 /* Remember the position at which this line ends. */
15002 row->end = it->current;
15004 /* Save fringe bitmaps in this row. */
15005 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15006 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15007 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15008 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15010 it->left_user_fringe_bitmap = 0;
15011 it->left_user_fringe_face_id = 0;
15012 it->right_user_fringe_bitmap = 0;
15013 it->right_user_fringe_face_id = 0;
15015 /* Maybe set the cursor. */
15016 if (it->w->cursor.vpos < 0
15017 && PT >= MATRIX_ROW_START_CHARPOS (row)
15018 && PT <= MATRIX_ROW_END_CHARPOS (row)
15019 && cursor_row_p (it->w, row))
15020 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15022 /* Highlight trailing whitespace. */
15023 if (!NILP (Vshow_trailing_whitespace))
15024 highlight_trailing_whitespace (it->f, it->glyph_row);
15026 /* Prepare for the next line. This line starts horizontally at (X
15027 HPOS) = (0 0). Vertical positions are incremented. As a
15028 convenience for the caller, IT->glyph_row is set to the next
15029 row to be used. */
15030 it->current_x = it->hpos = 0;
15031 it->current_y += row->height;
15032 ++it->vpos;
15033 ++it->glyph_row;
15034 it->start = it->current;
15035 return row->displays_text_p;
15040 /***********************************************************************
15041 Menu Bar
15042 ***********************************************************************/
15044 /* Redisplay the menu bar in the frame for window W.
15046 The menu bar of X frames that don't have X toolkit support is
15047 displayed in a special window W->frame->menu_bar_window.
15049 The menu bar of terminal frames is treated specially as far as
15050 glyph matrices are concerned. Menu bar lines are not part of
15051 windows, so the update is done directly on the frame matrix rows
15052 for the menu bar. */
15054 static void
15055 display_menu_bar (w)
15056 struct window *w;
15058 struct frame *f = XFRAME (WINDOW_FRAME (w));
15059 struct it it;
15060 Lisp_Object items;
15061 int i;
15063 /* Don't do all this for graphical frames. */
15064 #ifdef HAVE_NTGUI
15065 if (!NILP (Vwindow_system))
15066 return;
15067 #endif
15068 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15069 if (FRAME_X_P (f))
15070 return;
15071 #endif
15072 #ifdef MAC_OS
15073 if (FRAME_MAC_P (f))
15074 return;
15075 #endif
15077 #ifdef USE_X_TOOLKIT
15078 xassert (!FRAME_WINDOW_P (f));
15079 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15080 it.first_visible_x = 0;
15081 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15082 #else /* not USE_X_TOOLKIT */
15083 if (FRAME_WINDOW_P (f))
15085 /* Menu bar lines are displayed in the desired matrix of the
15086 dummy window menu_bar_window. */
15087 struct window *menu_w;
15088 xassert (WINDOWP (f->menu_bar_window));
15089 menu_w = XWINDOW (f->menu_bar_window);
15090 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15091 MENU_FACE_ID);
15092 it.first_visible_x = 0;
15093 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15095 else
15097 /* This is a TTY frame, i.e. character hpos/vpos are used as
15098 pixel x/y. */
15099 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15100 MENU_FACE_ID);
15101 it.first_visible_x = 0;
15102 it.last_visible_x = FRAME_COLS (f);
15104 #endif /* not USE_X_TOOLKIT */
15106 if (! mode_line_inverse_video)
15107 /* Force the menu-bar to be displayed in the default face. */
15108 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15110 /* Clear all rows of the menu bar. */
15111 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15113 struct glyph_row *row = it.glyph_row + i;
15114 clear_glyph_row (row);
15115 row->enabled_p = 1;
15116 row->full_width_p = 1;
15119 /* Display all items of the menu bar. */
15120 items = FRAME_MENU_BAR_ITEMS (it.f);
15121 for (i = 0; i < XVECTOR (items)->size; i += 4)
15123 Lisp_Object string;
15125 /* Stop at nil string. */
15126 string = AREF (items, i + 1);
15127 if (NILP (string))
15128 break;
15130 /* Remember where item was displayed. */
15131 AREF (items, i + 3) = make_number (it.hpos);
15133 /* Display the item, pad with one space. */
15134 if (it.current_x < it.last_visible_x)
15135 display_string (NULL, string, Qnil, 0, 0, &it,
15136 SCHARS (string) + 1, 0, 0, -1);
15139 /* Fill out the line with spaces. */
15140 if (it.current_x < it.last_visible_x)
15141 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15143 /* Compute the total height of the lines. */
15144 compute_line_metrics (&it);
15149 /***********************************************************************
15150 Mode Line
15151 ***********************************************************************/
15153 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15154 FORCE is non-zero, redisplay mode lines unconditionally.
15155 Otherwise, redisplay only mode lines that are garbaged. Value is
15156 the number of windows whose mode lines were redisplayed. */
15158 static int
15159 redisplay_mode_lines (window, force)
15160 Lisp_Object window;
15161 int force;
15163 int nwindows = 0;
15165 while (!NILP (window))
15167 struct window *w = XWINDOW (window);
15169 if (WINDOWP (w->hchild))
15170 nwindows += redisplay_mode_lines (w->hchild, force);
15171 else if (WINDOWP (w->vchild))
15172 nwindows += redisplay_mode_lines (w->vchild, force);
15173 else if (force
15174 || FRAME_GARBAGED_P (XFRAME (w->frame))
15175 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15177 struct text_pos lpoint;
15178 struct buffer *old = current_buffer;
15180 /* Set the window's buffer for the mode line display. */
15181 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15182 set_buffer_internal_1 (XBUFFER (w->buffer));
15184 /* Point refers normally to the selected window. For any
15185 other window, set up appropriate value. */
15186 if (!EQ (window, selected_window))
15188 struct text_pos pt;
15190 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15191 if (CHARPOS (pt) < BEGV)
15192 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15193 else if (CHARPOS (pt) > (ZV - 1))
15194 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15195 else
15196 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15199 /* Display mode lines. */
15200 clear_glyph_matrix (w->desired_matrix);
15201 if (display_mode_lines (w))
15203 ++nwindows;
15204 w->must_be_updated_p = 1;
15207 /* Restore old settings. */
15208 set_buffer_internal_1 (old);
15209 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15212 window = w->next;
15215 return nwindows;
15219 /* Display the mode and/or top line of window W. Value is the number
15220 of mode lines displayed. */
15222 static int
15223 display_mode_lines (w)
15224 struct window *w;
15226 Lisp_Object old_selected_window, old_selected_frame;
15227 int n = 0;
15229 old_selected_frame = selected_frame;
15230 selected_frame = w->frame;
15231 old_selected_window = selected_window;
15232 XSETWINDOW (selected_window, w);
15234 /* These will be set while the mode line specs are processed. */
15235 line_number_displayed = 0;
15236 w->column_number_displayed = Qnil;
15238 if (WINDOW_WANTS_MODELINE_P (w))
15240 struct window *sel_w = XWINDOW (old_selected_window);
15242 /* Select mode line face based on the real selected window. */
15243 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15244 current_buffer->mode_line_format);
15245 ++n;
15248 if (WINDOW_WANTS_HEADER_LINE_P (w))
15250 display_mode_line (w, HEADER_LINE_FACE_ID,
15251 current_buffer->header_line_format);
15252 ++n;
15255 selected_frame = old_selected_frame;
15256 selected_window = old_selected_window;
15257 return n;
15261 /* Display mode or top line of window W. FACE_ID specifies which line
15262 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15263 FORMAT is the mode line format to display. Value is the pixel
15264 height of the mode line displayed. */
15266 static int
15267 display_mode_line (w, face_id, format)
15268 struct window *w;
15269 enum face_id face_id;
15270 Lisp_Object format;
15272 struct it it;
15273 struct face *face;
15275 init_iterator (&it, w, -1, -1, NULL, face_id);
15276 prepare_desired_row (it.glyph_row);
15278 it.glyph_row->mode_line_p = 1;
15280 if (! mode_line_inverse_video)
15281 /* Force the mode-line to be displayed in the default face. */
15282 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15284 /* Temporarily make frame's keyboard the current kboard so that
15285 kboard-local variables in the mode_line_format will get the right
15286 values. */
15287 push_frame_kboard (it.f);
15288 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15289 pop_frame_kboard ();
15291 /* Fill up with spaces. */
15292 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15294 compute_line_metrics (&it);
15295 it.glyph_row->full_width_p = 1;
15296 it.glyph_row->continued_p = 0;
15297 it.glyph_row->truncated_on_left_p = 0;
15298 it.glyph_row->truncated_on_right_p = 0;
15300 /* Make a 3D mode-line have a shadow at its right end. */
15301 face = FACE_FROM_ID (it.f, face_id);
15302 extend_face_to_end_of_line (&it);
15303 if (face->box != FACE_NO_BOX)
15305 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15306 + it.glyph_row->used[TEXT_AREA] - 1);
15307 last->right_box_line_p = 1;
15310 return it.glyph_row->height;
15313 /* Alist that caches the results of :propertize.
15314 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15315 Lisp_Object mode_line_proptrans_alist;
15317 /* List of strings making up the mode-line. */
15318 Lisp_Object mode_line_string_list;
15320 /* Base face property when building propertized mode line string. */
15321 static Lisp_Object mode_line_string_face;
15322 static Lisp_Object mode_line_string_face_prop;
15325 /* Contribute ELT to the mode line for window IT->w. How it
15326 translates into text depends on its data type.
15328 IT describes the display environment in which we display, as usual.
15330 DEPTH is the depth in recursion. It is used to prevent
15331 infinite recursion here.
15333 FIELD_WIDTH is the number of characters the display of ELT should
15334 occupy in the mode line, and PRECISION is the maximum number of
15335 characters to display from ELT's representation. See
15336 display_string for details.
15338 Returns the hpos of the end of the text generated by ELT.
15340 PROPS is a property list to add to any string we encounter.
15342 If RISKY is nonzero, remove (disregard) any properties in any string
15343 we encounter, and ignore :eval and :propertize.
15345 If the global variable `frame_title_ptr' is non-NULL, then the output
15346 is passed to `store_frame_title' instead of `display_string'. */
15348 static int
15349 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15350 struct it *it;
15351 int depth;
15352 int field_width, precision;
15353 Lisp_Object elt, props;
15354 int risky;
15356 int n = 0, field, prec;
15357 int literal = 0;
15359 tail_recurse:
15360 if (depth > 100)
15361 elt = build_string ("*too-deep*");
15363 depth++;
15365 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15367 case Lisp_String:
15369 /* A string: output it and check for %-constructs within it. */
15370 unsigned char c;
15371 const unsigned char *this, *lisp_string;
15373 if (!NILP (props) || risky)
15375 Lisp_Object oprops, aelt;
15376 oprops = Ftext_properties_at (make_number (0), elt);
15378 /* If the starting string's properties are not what
15379 we want, translate the string. Also, if the string
15380 is risky, do that anyway. */
15382 if (NILP (Fequal (props, oprops)) || risky)
15384 /* If the starting string has properties,
15385 merge the specified ones onto the existing ones. */
15386 if (! NILP (oprops) && !risky)
15388 Lisp_Object tem;
15390 oprops = Fcopy_sequence (oprops);
15391 tem = props;
15392 while (CONSP (tem))
15394 oprops = Fplist_put (oprops, XCAR (tem),
15395 XCAR (XCDR (tem)));
15396 tem = XCDR (XCDR (tem));
15398 props = oprops;
15401 aelt = Fassoc (elt, mode_line_proptrans_alist);
15402 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15404 mode_line_proptrans_alist
15405 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15406 elt = XCAR (aelt);
15408 else
15410 Lisp_Object tem;
15412 elt = Fcopy_sequence (elt);
15413 Fset_text_properties (make_number (0), Flength (elt),
15414 props, elt);
15415 /* Add this item to mode_line_proptrans_alist. */
15416 mode_line_proptrans_alist
15417 = Fcons (Fcons (elt, props),
15418 mode_line_proptrans_alist);
15419 /* Truncate mode_line_proptrans_alist
15420 to at most 50 elements. */
15421 tem = Fnthcdr (make_number (50),
15422 mode_line_proptrans_alist);
15423 if (! NILP (tem))
15424 XSETCDR (tem, Qnil);
15429 this = SDATA (elt);
15430 lisp_string = this;
15432 if (literal)
15434 prec = precision - n;
15435 if (frame_title_ptr)
15436 n += store_frame_title (SDATA (elt), -1, prec);
15437 else if (!NILP (mode_line_string_list))
15438 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15439 else
15440 n += display_string (NULL, elt, Qnil, 0, 0, it,
15441 0, prec, 0, STRING_MULTIBYTE (elt));
15443 break;
15446 while ((precision <= 0 || n < precision)
15447 && *this
15448 && (frame_title_ptr
15449 || !NILP (mode_line_string_list)
15450 || it->current_x < it->last_visible_x))
15452 const unsigned char *last = this;
15454 /* Advance to end of string or next format specifier. */
15455 while ((c = *this++) != '\0' && c != '%')
15458 if (this - 1 != last)
15460 int nchars, nbytes;
15462 /* Output to end of string or up to '%'. Field width
15463 is length of string. Don't output more than
15464 PRECISION allows us. */
15465 --this;
15467 prec = c_string_width (last, this - last, precision - n,
15468 &nchars, &nbytes);
15470 if (frame_title_ptr)
15471 n += store_frame_title (last, 0, prec);
15472 else if (!NILP (mode_line_string_list))
15474 int bytepos = last - lisp_string;
15475 int charpos = string_byte_to_char (elt, bytepos);
15476 int endpos = (precision <= 0 ? SCHARS (elt)
15477 : charpos + nchars);
15479 n += store_mode_line_string (NULL,
15480 Fsubstring (elt, make_number (charpos),
15481 make_number (endpos)),
15482 0, 0, 0, Qnil);
15484 else
15486 int bytepos = last - lisp_string;
15487 int charpos = string_byte_to_char (elt, bytepos);
15488 n += display_string (NULL, elt, Qnil, 0, charpos,
15489 it, 0, prec, 0,
15490 STRING_MULTIBYTE (elt));
15493 else /* c == '%' */
15495 const unsigned char *percent_position = this;
15497 /* Get the specified minimum width. Zero means
15498 don't pad. */
15499 field = 0;
15500 while ((c = *this++) >= '0' && c <= '9')
15501 field = field * 10 + c - '0';
15503 /* Don't pad beyond the total padding allowed. */
15504 if (field_width - n > 0 && field > field_width - n)
15505 field = field_width - n;
15507 /* Note that either PRECISION <= 0 or N < PRECISION. */
15508 prec = precision - n;
15510 if (c == 'M')
15511 n += display_mode_element (it, depth, field, prec,
15512 Vglobal_mode_string, props,
15513 risky);
15514 else if (c != 0)
15516 int multibyte;
15517 int bytepos, charpos;
15518 unsigned char *spec;
15520 bytepos = percent_position - lisp_string;
15521 charpos = (STRING_MULTIBYTE (elt)
15522 ? string_byte_to_char (elt, bytepos)
15523 : bytepos);
15525 spec
15526 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15528 if (frame_title_ptr)
15529 n += store_frame_title (spec, field, prec);
15530 else if (!NILP (mode_line_string_list))
15532 int len = strlen (spec);
15533 Lisp_Object tem = make_string (spec, len);
15534 props = Ftext_properties_at (make_number (charpos), elt);
15535 /* Should only keep face property in props */
15536 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15538 else
15540 int nglyphs_before, nwritten;
15542 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15543 nwritten = display_string (spec, Qnil, elt,
15544 charpos, 0, it,
15545 field, prec, 0,
15546 multibyte);
15548 /* Assign to the glyphs written above the
15549 string where the `%x' came from, position
15550 of the `%'. */
15551 if (nwritten > 0)
15553 struct glyph *glyph
15554 = (it->glyph_row->glyphs[TEXT_AREA]
15555 + nglyphs_before);
15556 int i;
15558 for (i = 0; i < nwritten; ++i)
15560 glyph[i].object = elt;
15561 glyph[i].charpos = charpos;
15564 n += nwritten;
15568 else /* c == 0 */
15569 break;
15573 break;
15575 case Lisp_Symbol:
15576 /* A symbol: process the value of the symbol recursively
15577 as if it appeared here directly. Avoid error if symbol void.
15578 Special case: if value of symbol is a string, output the string
15579 literally. */
15581 register Lisp_Object tem;
15583 /* If the variable is not marked as risky to set
15584 then its contents are risky to use. */
15585 if (NILP (Fget (elt, Qrisky_local_variable)))
15586 risky = 1;
15588 tem = Fboundp (elt);
15589 if (!NILP (tem))
15591 tem = Fsymbol_value (elt);
15592 /* If value is a string, output that string literally:
15593 don't check for % within it. */
15594 if (STRINGP (tem))
15595 literal = 1;
15597 if (!EQ (tem, elt))
15599 /* Give up right away for nil or t. */
15600 elt = tem;
15601 goto tail_recurse;
15605 break;
15607 case Lisp_Cons:
15609 register Lisp_Object car, tem;
15611 /* A cons cell: five distinct cases.
15612 If first element is :eval or :propertize, do something special.
15613 If first element is a string or a cons, process all the elements
15614 and effectively concatenate them.
15615 If first element is a negative number, truncate displaying cdr to
15616 at most that many characters. If positive, pad (with spaces)
15617 to at least that many characters.
15618 If first element is a symbol, process the cadr or caddr recursively
15619 according to whether the symbol's value is non-nil or nil. */
15620 car = XCAR (elt);
15621 if (EQ (car, QCeval))
15623 /* An element of the form (:eval FORM) means evaluate FORM
15624 and use the result as mode line elements. */
15626 if (risky)
15627 break;
15629 if (CONSP (XCDR (elt)))
15631 Lisp_Object spec;
15632 spec = safe_eval (XCAR (XCDR (elt)));
15633 n += display_mode_element (it, depth, field_width - n,
15634 precision - n, spec, props,
15635 risky);
15638 else if (EQ (car, QCpropertize))
15640 /* An element of the form (:propertize ELT PROPS...)
15641 means display ELT but applying properties PROPS. */
15643 if (risky)
15644 break;
15646 if (CONSP (XCDR (elt)))
15647 n += display_mode_element (it, depth, field_width - n,
15648 precision - n, XCAR (XCDR (elt)),
15649 XCDR (XCDR (elt)), risky);
15651 else if (SYMBOLP (car))
15653 tem = Fboundp (car);
15654 elt = XCDR (elt);
15655 if (!CONSP (elt))
15656 goto invalid;
15657 /* elt is now the cdr, and we know it is a cons cell.
15658 Use its car if CAR has a non-nil value. */
15659 if (!NILP (tem))
15661 tem = Fsymbol_value (car);
15662 if (!NILP (tem))
15664 elt = XCAR (elt);
15665 goto tail_recurse;
15668 /* Symbol's value is nil (or symbol is unbound)
15669 Get the cddr of the original list
15670 and if possible find the caddr and use that. */
15671 elt = XCDR (elt);
15672 if (NILP (elt))
15673 break;
15674 else if (!CONSP (elt))
15675 goto invalid;
15676 elt = XCAR (elt);
15677 goto tail_recurse;
15679 else if (INTEGERP (car))
15681 register int lim = XINT (car);
15682 elt = XCDR (elt);
15683 if (lim < 0)
15685 /* Negative int means reduce maximum width. */
15686 if (precision <= 0)
15687 precision = -lim;
15688 else
15689 precision = min (precision, -lim);
15691 else if (lim > 0)
15693 /* Padding specified. Don't let it be more than
15694 current maximum. */
15695 if (precision > 0)
15696 lim = min (precision, lim);
15698 /* If that's more padding than already wanted, queue it.
15699 But don't reduce padding already specified even if
15700 that is beyond the current truncation point. */
15701 field_width = max (lim, field_width);
15703 goto tail_recurse;
15705 else if (STRINGP (car) || CONSP (car))
15707 register int limit = 50;
15708 /* Limit is to protect against circular lists. */
15709 while (CONSP (elt)
15710 && --limit > 0
15711 && (precision <= 0 || n < precision))
15713 n += display_mode_element (it, depth, field_width - n,
15714 precision - n, XCAR (elt),
15715 props, risky);
15716 elt = XCDR (elt);
15720 break;
15722 default:
15723 invalid:
15724 elt = build_string ("*invalid*");
15725 goto tail_recurse;
15728 /* Pad to FIELD_WIDTH. */
15729 if (field_width > 0 && n < field_width)
15731 if (frame_title_ptr)
15732 n += store_frame_title ("", field_width - n, 0);
15733 else if (!NILP (mode_line_string_list))
15734 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15735 else
15736 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15737 0, 0, 0);
15740 return n;
15743 /* Store a mode-line string element in mode_line_string_list.
15745 If STRING is non-null, display that C string. Otherwise, the Lisp
15746 string LISP_STRING is displayed.
15748 FIELD_WIDTH is the minimum number of output glyphs to produce.
15749 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15750 with spaces. FIELD_WIDTH <= 0 means don't pad.
15752 PRECISION is the maximum number of characters to output from
15753 STRING. PRECISION <= 0 means don't truncate the string.
15755 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15756 properties to the string.
15758 PROPS are the properties to add to the string.
15759 The mode_line_string_face face property is always added to the string.
15762 static int
15763 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15764 char *string;
15765 Lisp_Object lisp_string;
15766 int copy_string;
15767 int field_width;
15768 int precision;
15769 Lisp_Object props;
15771 int len;
15772 int n = 0;
15774 if (string != NULL)
15776 len = strlen (string);
15777 if (precision > 0 && len > precision)
15778 len = precision;
15779 lisp_string = make_string (string, len);
15780 if (NILP (props))
15781 props = mode_line_string_face_prop;
15782 else if (!NILP (mode_line_string_face))
15784 Lisp_Object face = Fplist_get (props, Qface);
15785 props = Fcopy_sequence (props);
15786 if (NILP (face))
15787 face = mode_line_string_face;
15788 else
15789 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15790 props = Fplist_put (props, Qface, face);
15792 Fadd_text_properties (make_number (0), make_number (len),
15793 props, lisp_string);
15795 else
15797 len = XFASTINT (Flength (lisp_string));
15798 if (precision > 0 && len > precision)
15800 len = precision;
15801 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15802 precision = -1;
15804 if (!NILP (mode_line_string_face))
15806 Lisp_Object face;
15807 if (NILP (props))
15808 props = Ftext_properties_at (make_number (0), lisp_string);
15809 face = Fplist_get (props, Qface);
15810 if (NILP (face))
15811 face = mode_line_string_face;
15812 else
15813 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15814 props = Fcons (Qface, Fcons (face, Qnil));
15815 if (copy_string)
15816 lisp_string = Fcopy_sequence (lisp_string);
15818 if (!NILP (props))
15819 Fadd_text_properties (make_number (0), make_number (len),
15820 props, lisp_string);
15823 if (len > 0)
15825 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15826 n += len;
15829 if (field_width > len)
15831 field_width -= len;
15832 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15833 if (!NILP (props))
15834 Fadd_text_properties (make_number (0), make_number (field_width),
15835 props, lisp_string);
15836 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15837 n += field_width;
15840 return n;
15844 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15845 0, 4, 0,
15846 doc: /* Return the mode-line of selected window as a string.
15847 First optional arg FORMAT specifies a different format string (see
15848 `mode-line-format' for details) to use. If FORMAT is t, return
15849 the buffer's header-line. Second optional arg WINDOW specifies a
15850 different window to use as the context for the formatting.
15851 If third optional arg NO-PROPS is non-nil, string is not propertized.
15852 Fourth optional arg BUFFER specifies which buffer to use. */)
15853 (format, window, no_props, buffer)
15854 Lisp_Object format, window, no_props, buffer;
15856 struct it it;
15857 int len;
15858 struct window *w;
15859 struct buffer *old_buffer = NULL;
15860 enum face_id face_id = DEFAULT_FACE_ID;
15862 if (NILP (window))
15863 window = selected_window;
15864 CHECK_WINDOW (window);
15865 w = XWINDOW (window);
15867 if (NILP (buffer))
15868 buffer = w->buffer;
15870 CHECK_BUFFER (buffer);
15872 if (XBUFFER (buffer) != current_buffer)
15874 old_buffer = current_buffer;
15875 set_buffer_internal_1 (XBUFFER (buffer));
15878 if (NILP (format) || EQ (format, Qt))
15880 face_id = (NILP (format)
15881 ? CURRENT_MODE_LINE_FACE_ID (w)
15882 : HEADER_LINE_FACE_ID);
15883 format = (NILP (format)
15884 ? current_buffer->mode_line_format
15885 : current_buffer->header_line_format);
15888 init_iterator (&it, w, -1, -1, NULL, face_id);
15890 if (NILP (no_props))
15892 mode_line_string_face
15893 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15894 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15895 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15897 mode_line_string_face_prop
15898 = (NILP (mode_line_string_face) ? Qnil
15899 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15901 /* We need a dummy last element in mode_line_string_list to
15902 indicate we are building the propertized mode-line string.
15903 Using mode_line_string_face_prop here GC protects it. */
15904 mode_line_string_list
15905 = Fcons (mode_line_string_face_prop, Qnil);
15906 frame_title_ptr = NULL;
15908 else
15910 mode_line_string_face_prop = Qnil;
15911 mode_line_string_list = Qnil;
15912 frame_title_ptr = frame_title_buf;
15915 push_frame_kboard (it.f);
15916 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15917 pop_frame_kboard ();
15919 if (old_buffer)
15920 set_buffer_internal_1 (old_buffer);
15922 if (NILP (no_props))
15924 Lisp_Object str;
15925 mode_line_string_list = Fnreverse (mode_line_string_list);
15926 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15927 make_string ("", 0));
15928 mode_line_string_face_prop = Qnil;
15929 mode_line_string_list = Qnil;
15930 return str;
15933 len = frame_title_ptr - frame_title_buf;
15934 if (len > 0 && frame_title_ptr[-1] == '-')
15936 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15937 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15939 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15940 if (len > frame_title_ptr - frame_title_buf)
15941 len = frame_title_ptr - frame_title_buf;
15944 frame_title_ptr = NULL;
15945 return make_string (frame_title_buf, len);
15948 /* Write a null-terminated, right justified decimal representation of
15949 the positive integer D to BUF using a minimal field width WIDTH. */
15951 static void
15952 pint2str (buf, width, d)
15953 register char *buf;
15954 register int width;
15955 register int d;
15957 register char *p = buf;
15959 if (d <= 0)
15960 *p++ = '0';
15961 else
15963 while (d > 0)
15965 *p++ = d % 10 + '0';
15966 d /= 10;
15970 for (width -= (int) (p - buf); width > 0; --width)
15971 *p++ = ' ';
15972 *p-- = '\0';
15973 while (p > buf)
15975 d = *buf;
15976 *buf++ = *p;
15977 *p-- = d;
15981 /* Write a null-terminated, right justified decimal and "human
15982 readable" representation of the nonnegative integer D to BUF using
15983 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15985 static const char power_letter[] =
15987 0, /* not used */
15988 'k', /* kilo */
15989 'M', /* mega */
15990 'G', /* giga */
15991 'T', /* tera */
15992 'P', /* peta */
15993 'E', /* exa */
15994 'Z', /* zetta */
15995 'Y' /* yotta */
15998 static void
15999 pint2hrstr (buf, width, d)
16000 char *buf;
16001 int width;
16002 int d;
16004 /* We aim to represent the nonnegative integer D as
16005 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16006 int quotient = d;
16007 int remainder = 0;
16008 /* -1 means: do not use TENTHS. */
16009 int tenths = -1;
16010 int exponent = 0;
16012 /* Length of QUOTIENT.TENTHS as a string. */
16013 int length;
16015 char * psuffix;
16016 char * p;
16018 if (1000 <= quotient)
16020 /* Scale to the appropriate EXPONENT. */
16023 remainder = quotient % 1000;
16024 quotient /= 1000;
16025 exponent++;
16027 while (1000 <= quotient);
16029 /* Round to nearest and decide whether to use TENTHS or not. */
16030 if (quotient <= 9)
16032 tenths = remainder / 100;
16033 if (50 <= remainder % 100)
16034 if (tenths < 9)
16035 tenths++;
16036 else
16038 quotient++;
16039 if (quotient == 10)
16040 tenths = -1;
16041 else
16042 tenths = 0;
16045 else
16046 if (500 <= remainder)
16047 if (quotient < 999)
16048 quotient++;
16049 else
16051 quotient = 1;
16052 exponent++;
16053 tenths = 0;
16057 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16058 if (tenths == -1 && quotient <= 99)
16059 if (quotient <= 9)
16060 length = 1;
16061 else
16062 length = 2;
16063 else
16064 length = 3;
16065 p = psuffix = buf + max (width, length);
16067 /* Print EXPONENT. */
16068 if (exponent)
16069 *psuffix++ = power_letter[exponent];
16070 *psuffix = '\0';
16072 /* Print TENTHS. */
16073 if (tenths >= 0)
16075 *--p = '0' + tenths;
16076 *--p = '.';
16079 /* Print QUOTIENT. */
16082 int digit = quotient % 10;
16083 *--p = '0' + digit;
16085 while ((quotient /= 10) != 0);
16087 /* Print leading spaces. */
16088 while (buf < p)
16089 *--p = ' ';
16092 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16093 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16094 type of CODING_SYSTEM. Return updated pointer into BUF. */
16096 static unsigned char invalid_eol_type[] = "(*invalid*)";
16098 static char *
16099 decode_mode_spec_coding (coding_system, buf, eol_flag)
16100 Lisp_Object coding_system;
16101 register char *buf;
16102 int eol_flag;
16104 Lisp_Object val;
16105 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16106 const unsigned char *eol_str;
16107 int eol_str_len;
16108 /* The EOL conversion we are using. */
16109 Lisp_Object eoltype;
16111 val = Fget (coding_system, Qcoding_system);
16112 eoltype = Qnil;
16114 if (!VECTORP (val)) /* Not yet decided. */
16116 if (multibyte)
16117 *buf++ = '-';
16118 if (eol_flag)
16119 eoltype = eol_mnemonic_undecided;
16120 /* Don't mention EOL conversion if it isn't decided. */
16122 else
16124 Lisp_Object eolvalue;
16126 eolvalue = Fget (coding_system, Qeol_type);
16128 if (multibyte)
16129 *buf++ = XFASTINT (AREF (val, 1));
16131 if (eol_flag)
16133 /* The EOL conversion that is normal on this system. */
16135 if (NILP (eolvalue)) /* Not yet decided. */
16136 eoltype = eol_mnemonic_undecided;
16137 else if (VECTORP (eolvalue)) /* Not yet decided. */
16138 eoltype = eol_mnemonic_undecided;
16139 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16140 eoltype = (XFASTINT (eolvalue) == 0
16141 ? eol_mnemonic_unix
16142 : (XFASTINT (eolvalue) == 1
16143 ? eol_mnemonic_dos : eol_mnemonic_mac));
16147 if (eol_flag)
16149 /* Mention the EOL conversion if it is not the usual one. */
16150 if (STRINGP (eoltype))
16152 eol_str = SDATA (eoltype);
16153 eol_str_len = SBYTES (eoltype);
16155 else if (INTEGERP (eoltype)
16156 && CHAR_VALID_P (XINT (eoltype), 0))
16158 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16159 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16160 eol_str = tmp;
16162 else
16164 eol_str = invalid_eol_type;
16165 eol_str_len = sizeof (invalid_eol_type) - 1;
16167 bcopy (eol_str, buf, eol_str_len);
16168 buf += eol_str_len;
16171 return buf;
16174 /* Return a string for the output of a mode line %-spec for window W,
16175 generated by character C. PRECISION >= 0 means don't return a
16176 string longer than that value. FIELD_WIDTH > 0 means pad the
16177 string returned with spaces to that value. Return 1 in *MULTIBYTE
16178 if the result is multibyte text.
16180 Note we operate on the current buffer for most purposes,
16181 the exception being w->base_line_pos. */
16183 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16185 static char *
16186 decode_mode_spec (w, c, field_width, precision, multibyte)
16187 struct window *w;
16188 register int c;
16189 int field_width, precision;
16190 int *multibyte;
16192 Lisp_Object obj;
16193 struct frame *f = XFRAME (WINDOW_FRAME (w));
16194 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16195 struct buffer *b = current_buffer;
16197 obj = Qnil;
16198 *multibyte = 0;
16200 switch (c)
16202 case '*':
16203 if (!NILP (b->read_only))
16204 return "%";
16205 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16206 return "*";
16207 return "-";
16209 case '+':
16210 /* This differs from %* only for a modified read-only buffer. */
16211 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16212 return "*";
16213 if (!NILP (b->read_only))
16214 return "%";
16215 return "-";
16217 case '&':
16218 /* This differs from %* in ignoring read-only-ness. */
16219 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16220 return "*";
16221 return "-";
16223 case '%':
16224 return "%";
16226 case '[':
16228 int i;
16229 char *p;
16231 if (command_loop_level > 5)
16232 return "[[[... ";
16233 p = decode_mode_spec_buf;
16234 for (i = 0; i < command_loop_level; i++)
16235 *p++ = '[';
16236 *p = 0;
16237 return decode_mode_spec_buf;
16240 case ']':
16242 int i;
16243 char *p;
16245 if (command_loop_level > 5)
16246 return " ...]]]";
16247 p = decode_mode_spec_buf;
16248 for (i = 0; i < command_loop_level; i++)
16249 *p++ = ']';
16250 *p = 0;
16251 return decode_mode_spec_buf;
16254 case '-':
16256 register int i;
16258 /* Let lots_of_dashes be a string of infinite length. */
16259 if (!NILP (mode_line_string_list))
16260 return "--";
16261 if (field_width <= 0
16262 || field_width > sizeof (lots_of_dashes))
16264 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16265 decode_mode_spec_buf[i] = '-';
16266 decode_mode_spec_buf[i] = '\0';
16267 return decode_mode_spec_buf;
16269 else
16270 return lots_of_dashes;
16273 case 'b':
16274 obj = b->name;
16275 break;
16277 case 'c':
16279 int col = (int) current_column (); /* iftc */
16280 w->column_number_displayed = make_number (col);
16281 pint2str (decode_mode_spec_buf, field_width, col);
16282 return decode_mode_spec_buf;
16285 case 'F':
16286 /* %F displays the frame name. */
16287 if (!NILP (f->title))
16288 return (char *) SDATA (f->title);
16289 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16290 return (char *) SDATA (f->name);
16291 return "Emacs";
16293 case 'f':
16294 obj = b->filename;
16295 break;
16297 case 'i':
16299 int size = ZV - BEGV;
16300 pint2str (decode_mode_spec_buf, field_width, size);
16301 return decode_mode_spec_buf;
16304 case 'I':
16306 int size = ZV - BEGV;
16307 pint2hrstr (decode_mode_spec_buf, field_width, size);
16308 return decode_mode_spec_buf;
16311 case 'l':
16313 int startpos = XMARKER (w->start)->charpos;
16314 int startpos_byte = marker_byte_position (w->start);
16315 int line, linepos, linepos_byte, topline;
16316 int nlines, junk;
16317 int height = WINDOW_TOTAL_LINES (w);
16319 /* If we decided that this buffer isn't suitable for line numbers,
16320 don't forget that too fast. */
16321 if (EQ (w->base_line_pos, w->buffer))
16322 goto no_value;
16323 /* But do forget it, if the window shows a different buffer now. */
16324 else if (BUFFERP (w->base_line_pos))
16325 w->base_line_pos = Qnil;
16327 /* If the buffer is very big, don't waste time. */
16328 if (INTEGERP (Vline_number_display_limit)
16329 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16331 w->base_line_pos = Qnil;
16332 w->base_line_number = Qnil;
16333 goto no_value;
16336 if (!NILP (w->base_line_number)
16337 && !NILP (w->base_line_pos)
16338 && XFASTINT (w->base_line_pos) <= startpos)
16340 line = XFASTINT (w->base_line_number);
16341 linepos = XFASTINT (w->base_line_pos);
16342 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16344 else
16346 line = 1;
16347 linepos = BUF_BEGV (b);
16348 linepos_byte = BUF_BEGV_BYTE (b);
16351 /* Count lines from base line to window start position. */
16352 nlines = display_count_lines (linepos, linepos_byte,
16353 startpos_byte,
16354 startpos, &junk);
16356 topline = nlines + line;
16358 /* Determine a new base line, if the old one is too close
16359 or too far away, or if we did not have one.
16360 "Too close" means it's plausible a scroll-down would
16361 go back past it. */
16362 if (startpos == BUF_BEGV (b))
16364 w->base_line_number = make_number (topline);
16365 w->base_line_pos = make_number (BUF_BEGV (b));
16367 else if (nlines < height + 25 || nlines > height * 3 + 50
16368 || linepos == BUF_BEGV (b))
16370 int limit = BUF_BEGV (b);
16371 int limit_byte = BUF_BEGV_BYTE (b);
16372 int position;
16373 int distance = (height * 2 + 30) * line_number_display_limit_width;
16375 if (startpos - distance > limit)
16377 limit = startpos - distance;
16378 limit_byte = CHAR_TO_BYTE (limit);
16381 nlines = display_count_lines (startpos, startpos_byte,
16382 limit_byte,
16383 - (height * 2 + 30),
16384 &position);
16385 /* If we couldn't find the lines we wanted within
16386 line_number_display_limit_width chars per line,
16387 give up on line numbers for this window. */
16388 if (position == limit_byte && limit == startpos - distance)
16390 w->base_line_pos = w->buffer;
16391 w->base_line_number = Qnil;
16392 goto no_value;
16395 w->base_line_number = make_number (topline - nlines);
16396 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16399 /* Now count lines from the start pos to point. */
16400 nlines = display_count_lines (startpos, startpos_byte,
16401 PT_BYTE, PT, &junk);
16403 /* Record that we did display the line number. */
16404 line_number_displayed = 1;
16406 /* Make the string to show. */
16407 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16408 return decode_mode_spec_buf;
16409 no_value:
16411 char* p = decode_mode_spec_buf;
16412 int pad = field_width - 2;
16413 while (pad-- > 0)
16414 *p++ = ' ';
16415 *p++ = '?';
16416 *p++ = '?';
16417 *p = '\0';
16418 return decode_mode_spec_buf;
16421 break;
16423 case 'm':
16424 obj = b->mode_name;
16425 break;
16427 case 'n':
16428 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16429 return " Narrow";
16430 break;
16432 case 'p':
16434 int pos = marker_position (w->start);
16435 int total = BUF_ZV (b) - BUF_BEGV (b);
16437 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16439 if (pos <= BUF_BEGV (b))
16440 return "All";
16441 else
16442 return "Bottom";
16444 else if (pos <= BUF_BEGV (b))
16445 return "Top";
16446 else
16448 if (total > 1000000)
16449 /* Do it differently for a large value, to avoid overflow. */
16450 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16451 else
16452 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16453 /* We can't normally display a 3-digit number,
16454 so get us a 2-digit number that is close. */
16455 if (total == 100)
16456 total = 99;
16457 sprintf (decode_mode_spec_buf, "%2d%%", total);
16458 return decode_mode_spec_buf;
16462 /* Display percentage of size above the bottom of the screen. */
16463 case 'P':
16465 int toppos = marker_position (w->start);
16466 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16467 int total = BUF_ZV (b) - BUF_BEGV (b);
16469 if (botpos >= BUF_ZV (b))
16471 if (toppos <= BUF_BEGV (b))
16472 return "All";
16473 else
16474 return "Bottom";
16476 else
16478 if (total > 1000000)
16479 /* Do it differently for a large value, to avoid overflow. */
16480 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16481 else
16482 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16483 /* We can't normally display a 3-digit number,
16484 so get us a 2-digit number that is close. */
16485 if (total == 100)
16486 total = 99;
16487 if (toppos <= BUF_BEGV (b))
16488 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16489 else
16490 sprintf (decode_mode_spec_buf, "%2d%%", total);
16491 return decode_mode_spec_buf;
16495 case 's':
16496 /* status of process */
16497 obj = Fget_buffer_process (Fcurrent_buffer ());
16498 if (NILP (obj))
16499 return "no process";
16500 #ifdef subprocesses
16501 obj = Fsymbol_name (Fprocess_status (obj));
16502 #endif
16503 break;
16505 case 't': /* indicate TEXT or BINARY */
16506 #ifdef MODE_LINE_BINARY_TEXT
16507 return MODE_LINE_BINARY_TEXT (b);
16508 #else
16509 return "T";
16510 #endif
16512 case 'z':
16513 /* coding-system (not including end-of-line format) */
16514 case 'Z':
16515 /* coding-system (including end-of-line type) */
16517 int eol_flag = (c == 'Z');
16518 char *p = decode_mode_spec_buf;
16520 if (! FRAME_WINDOW_P (f))
16522 /* No need to mention EOL here--the terminal never needs
16523 to do EOL conversion. */
16524 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16525 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16527 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16528 p, eol_flag);
16530 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16531 #ifdef subprocesses
16532 obj = Fget_buffer_process (Fcurrent_buffer ());
16533 if (PROCESSP (obj))
16535 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16536 p, eol_flag);
16537 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16538 p, eol_flag);
16540 #endif /* subprocesses */
16541 #endif /* 0 */
16542 *p = 0;
16543 return decode_mode_spec_buf;
16547 if (STRINGP (obj))
16549 *multibyte = STRING_MULTIBYTE (obj);
16550 return (char *) SDATA (obj);
16552 else
16553 return "";
16557 /* Count up to COUNT lines starting from START / START_BYTE.
16558 But don't go beyond LIMIT_BYTE.
16559 Return the number of lines thus found (always nonnegative).
16561 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16563 static int
16564 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16565 int start, start_byte, limit_byte, count;
16566 int *byte_pos_ptr;
16568 register unsigned char *cursor;
16569 unsigned char *base;
16571 register int ceiling;
16572 register unsigned char *ceiling_addr;
16573 int orig_count = count;
16575 /* If we are not in selective display mode,
16576 check only for newlines. */
16577 int selective_display = (!NILP (current_buffer->selective_display)
16578 && !INTEGERP (current_buffer->selective_display));
16580 if (count > 0)
16582 while (start_byte < limit_byte)
16584 ceiling = BUFFER_CEILING_OF (start_byte);
16585 ceiling = min (limit_byte - 1, ceiling);
16586 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16587 base = (cursor = BYTE_POS_ADDR (start_byte));
16588 while (1)
16590 if (selective_display)
16591 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16593 else
16594 while (*cursor != '\n' && ++cursor != ceiling_addr)
16597 if (cursor != ceiling_addr)
16599 if (--count == 0)
16601 start_byte += cursor - base + 1;
16602 *byte_pos_ptr = start_byte;
16603 return orig_count;
16605 else
16606 if (++cursor == ceiling_addr)
16607 break;
16609 else
16610 break;
16612 start_byte += cursor - base;
16615 else
16617 while (start_byte > limit_byte)
16619 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16620 ceiling = max (limit_byte, ceiling);
16621 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16622 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16623 while (1)
16625 if (selective_display)
16626 while (--cursor != ceiling_addr
16627 && *cursor != '\n' && *cursor != 015)
16629 else
16630 while (--cursor != ceiling_addr && *cursor != '\n')
16633 if (cursor != ceiling_addr)
16635 if (++count == 0)
16637 start_byte += cursor - base + 1;
16638 *byte_pos_ptr = start_byte;
16639 /* When scanning backwards, we should
16640 not count the newline posterior to which we stop. */
16641 return - orig_count - 1;
16644 else
16645 break;
16647 /* Here we add 1 to compensate for the last decrement
16648 of CURSOR, which took it past the valid range. */
16649 start_byte += cursor - base + 1;
16653 *byte_pos_ptr = limit_byte;
16655 if (count < 0)
16656 return - orig_count + count;
16657 return orig_count - count;
16663 /***********************************************************************
16664 Displaying strings
16665 ***********************************************************************/
16667 /* Display a NUL-terminated string, starting with index START.
16669 If STRING is non-null, display that C string. Otherwise, the Lisp
16670 string LISP_STRING is displayed.
16672 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16673 FACE_STRING. Display STRING or LISP_STRING with the face at
16674 FACE_STRING_POS in FACE_STRING:
16676 Display the string in the environment given by IT, but use the
16677 standard display table, temporarily.
16679 FIELD_WIDTH is the minimum number of output glyphs to produce.
16680 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16681 with spaces. If STRING has more characters, more than FIELD_WIDTH
16682 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16684 PRECISION is the maximum number of characters to output from
16685 STRING. PRECISION < 0 means don't truncate the string.
16687 This is roughly equivalent to printf format specifiers:
16689 FIELD_WIDTH PRECISION PRINTF
16690 ----------------------------------------
16691 -1 -1 %s
16692 -1 10 %.10s
16693 10 -1 %10s
16694 20 10 %20.10s
16696 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16697 display them, and < 0 means obey the current buffer's value of
16698 enable_multibyte_characters.
16700 Value is the number of glyphs produced. */
16702 static int
16703 display_string (string, lisp_string, face_string, face_string_pos,
16704 start, it, field_width, precision, max_x, multibyte)
16705 unsigned char *string;
16706 Lisp_Object lisp_string;
16707 Lisp_Object face_string;
16708 int face_string_pos;
16709 int start;
16710 struct it *it;
16711 int field_width, precision, max_x;
16712 int multibyte;
16714 int hpos_at_start = it->hpos;
16715 int saved_face_id = it->face_id;
16716 struct glyph_row *row = it->glyph_row;
16718 /* Initialize the iterator IT for iteration over STRING beginning
16719 with index START. */
16720 reseat_to_string (it, string, lisp_string, start,
16721 precision, field_width, multibyte);
16723 /* If displaying STRING, set up the face of the iterator
16724 from LISP_STRING, if that's given. */
16725 if (STRINGP (face_string))
16727 int endptr;
16728 struct face *face;
16730 it->face_id
16731 = face_at_string_position (it->w, face_string, face_string_pos,
16732 0, it->region_beg_charpos,
16733 it->region_end_charpos,
16734 &endptr, it->base_face_id, 0);
16735 face = FACE_FROM_ID (it->f, it->face_id);
16736 it->face_box_p = face->box != FACE_NO_BOX;
16739 /* Set max_x to the maximum allowed X position. Don't let it go
16740 beyond the right edge of the window. */
16741 if (max_x <= 0)
16742 max_x = it->last_visible_x;
16743 else
16744 max_x = min (max_x, it->last_visible_x);
16746 /* Skip over display elements that are not visible. because IT->w is
16747 hscrolled. */
16748 if (it->current_x < it->first_visible_x)
16749 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16750 MOVE_TO_POS | MOVE_TO_X);
16752 row->ascent = it->max_ascent;
16753 row->height = it->max_ascent + it->max_descent;
16754 row->phys_ascent = it->max_phys_ascent;
16755 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16757 /* This condition is for the case that we are called with current_x
16758 past last_visible_x. */
16759 while (it->current_x < max_x)
16761 int x_before, x, n_glyphs_before, i, nglyphs;
16763 /* Get the next display element. */
16764 if (!get_next_display_element (it))
16765 break;
16767 /* Produce glyphs. */
16768 x_before = it->current_x;
16769 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16770 PRODUCE_GLYPHS (it);
16772 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16773 i = 0;
16774 x = x_before;
16775 while (i < nglyphs)
16777 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16779 if (!it->truncate_lines_p
16780 && x + glyph->pixel_width > max_x)
16782 /* End of continued line or max_x reached. */
16783 if (CHAR_GLYPH_PADDING_P (*glyph))
16785 /* A wide character is unbreakable. */
16786 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16787 it->current_x = x_before;
16789 else
16791 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16792 it->current_x = x;
16794 break;
16796 else if (x + glyph->pixel_width > it->first_visible_x)
16798 /* Glyph is at least partially visible. */
16799 ++it->hpos;
16800 if (x < it->first_visible_x)
16801 it->glyph_row->x = x - it->first_visible_x;
16803 else
16805 /* Glyph is off the left margin of the display area.
16806 Should not happen. */
16807 abort ();
16810 row->ascent = max (row->ascent, it->max_ascent);
16811 row->height = max (row->height, it->max_ascent + it->max_descent);
16812 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16813 row->phys_height = max (row->phys_height,
16814 it->max_phys_ascent + it->max_phys_descent);
16815 x += glyph->pixel_width;
16816 ++i;
16819 /* Stop if max_x reached. */
16820 if (i < nglyphs)
16821 break;
16823 /* Stop at line ends. */
16824 if (ITERATOR_AT_END_OF_LINE_P (it))
16826 it->continuation_lines_width = 0;
16827 break;
16830 set_iterator_to_next (it, 1);
16832 /* Stop if truncating at the right edge. */
16833 if (it->truncate_lines_p
16834 && it->current_x >= it->last_visible_x)
16836 /* Add truncation mark, but don't do it if the line is
16837 truncated at a padding space. */
16838 if (IT_CHARPOS (*it) < it->string_nchars)
16840 if (!FRAME_WINDOW_P (it->f))
16842 int i, n;
16844 if (it->current_x > it->last_visible_x)
16846 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16847 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16848 break;
16849 for (n = row->used[TEXT_AREA]; i < n; ++i)
16851 row->used[TEXT_AREA] = i;
16852 produce_special_glyphs (it, IT_TRUNCATION);
16855 produce_special_glyphs (it, IT_TRUNCATION);
16857 it->glyph_row->truncated_on_right_p = 1;
16859 break;
16863 /* Maybe insert a truncation at the left. */
16864 if (it->first_visible_x
16865 && IT_CHARPOS (*it) > 0)
16867 if (!FRAME_WINDOW_P (it->f))
16868 insert_left_trunc_glyphs (it);
16869 it->glyph_row->truncated_on_left_p = 1;
16872 it->face_id = saved_face_id;
16874 /* Value is number of columns displayed. */
16875 return it->hpos - hpos_at_start;
16880 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16881 appears as an element of LIST or as the car of an element of LIST.
16882 If PROPVAL is a list, compare each element against LIST in that
16883 way, and return 1/2 if any element of PROPVAL is found in LIST.
16884 Otherwise return 0. This function cannot quit.
16885 The return value is 2 if the text is invisible but with an ellipsis
16886 and 1 if it's invisible and without an ellipsis. */
16889 invisible_p (propval, list)
16890 register Lisp_Object propval;
16891 Lisp_Object list;
16893 register Lisp_Object tail, proptail;
16895 for (tail = list; CONSP (tail); tail = XCDR (tail))
16897 register Lisp_Object tem;
16898 tem = XCAR (tail);
16899 if (EQ (propval, tem))
16900 return 1;
16901 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16902 return NILP (XCDR (tem)) ? 1 : 2;
16905 if (CONSP (propval))
16907 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16909 Lisp_Object propelt;
16910 propelt = XCAR (proptail);
16911 for (tail = list; CONSP (tail); tail = XCDR (tail))
16913 register Lisp_Object tem;
16914 tem = XCAR (tail);
16915 if (EQ (propelt, tem))
16916 return 1;
16917 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16918 return NILP (XCDR (tem)) ? 1 : 2;
16923 return 0;
16926 /* Calculate a width or height in pixels from a specification using
16927 the following elements:
16929 SPEC ::=
16930 NUM - a (fractional) multiple of the default font width/height
16931 (NUM) - specifies exactly NUM pixels
16932 UNIT - a fixed number of pixels, see below.
16933 ELEMENT - size of a display element in pixels, see below.
16934 (NUM . SPEC) - equals NUM * SPEC
16935 (+ SPEC SPEC ...) - add pixel values
16936 (- SPEC SPEC ...) - subtract pixel values
16937 (- SPEC) - negate pixel value
16939 NUM ::=
16940 INT or FLOAT - a number constant
16941 SYMBOL - use symbol's (buffer local) variable binding.
16943 UNIT ::=
16944 in - pixels per inch *)
16945 mm - pixels per 1/1000 meter *)
16946 cm - pixels per 1/100 meter *)
16947 width - width of current font in pixels.
16948 height - height of current font in pixels.
16950 *) using the ratio(s) defined in display-pixels-per-inch.
16952 ELEMENT ::=
16954 left-fringe - left fringe width in pixels
16955 right-fringe - right fringe width in pixels
16957 left-margin - left margin width in pixels
16958 right-margin - right margin width in pixels
16960 scroll-bar - scroll-bar area width in pixels
16962 Examples:
16964 Pixels corresponding to 5 inches:
16965 (5 . in)
16967 Total width of non-text areas on left side of window (if scroll-bar is on left):
16968 '(space :width (+ left-fringe left-margin scroll-bar))
16970 Align to first text column (in header line):
16971 '(space :align-to 0)
16973 Align to middle of text area minus half the width of variable `my-image'
16974 containing a loaded image:
16975 '(space :align-to (0.5 . (- text my-image)))
16977 Width of left margin minus width of 1 character in the default font:
16978 '(space :width (- left-margin 1))
16980 Width of left margin minus width of 2 characters in the current font:
16981 '(space :width (- left-margin (2 . width)))
16983 Center 1 character over left-margin (in header line):
16984 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16986 Different ways to express width of left fringe plus left margin minus one pixel:
16987 '(space :width (- (+ left-fringe left-margin) (1)))
16988 '(space :width (+ left-fringe left-margin (- (1))))
16989 '(space :width (+ left-fringe left-margin (-1)))
16993 #define NUMVAL(X) \
16994 ((INTEGERP (X) || FLOATP (X)) \
16995 ? XFLOATINT (X) \
16996 : - 1)
16999 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17000 double *res;
17001 struct it *it;
17002 Lisp_Object prop;
17003 void *font;
17004 int width_p, *align_to;
17006 double pixels;
17008 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17009 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17011 if (NILP (prop))
17012 return OK_PIXELS (0);
17014 if (SYMBOLP (prop))
17016 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17018 char *unit = SDATA (SYMBOL_NAME (prop));
17020 if (unit[0] == 'i' && unit[1] == 'n')
17021 pixels = 1.0;
17022 else if (unit[0] == 'm' && unit[1] == 'm')
17023 pixels = 25.4;
17024 else if (unit[0] == 'c' && unit[1] == 'm')
17025 pixels = 2.54;
17026 else
17027 pixels = 0;
17028 if (pixels > 0)
17030 double ppi;
17031 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17032 || (CONSP (Vdisplay_pixels_per_inch)
17033 && (ppi = (width_p
17034 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17035 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17036 ppi > 0)))
17037 return OK_PIXELS (ppi / pixels);
17039 return 0;
17043 #ifdef HAVE_WINDOW_SYSTEM
17044 if (EQ (prop, Qheight))
17045 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17046 if (EQ (prop, Qwidth))
17047 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17048 #else
17049 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17050 return OK_PIXELS (1);
17051 #endif
17053 if (EQ (prop, Qtext))
17054 return OK_PIXELS (width_p
17055 ? window_box_width (it->w, TEXT_AREA)
17056 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17058 if (align_to && *align_to < 0)
17060 *res = 0;
17061 if (EQ (prop, Qleft))
17062 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17063 if (EQ (prop, Qright))
17064 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17065 if (EQ (prop, Qcenter))
17066 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17067 + window_box_width (it->w, TEXT_AREA) / 2);
17068 if (EQ (prop, Qleft_fringe))
17069 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17070 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17071 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17072 if (EQ (prop, Qright_fringe))
17073 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17074 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17075 : window_box_right_offset (it->w, TEXT_AREA));
17076 if (EQ (prop, Qleft_margin))
17077 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17078 if (EQ (prop, Qright_margin))
17079 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17080 if (EQ (prop, Qscroll_bar))
17081 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17083 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17084 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17085 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17086 : 0)));
17088 else
17090 if (EQ (prop, Qleft_fringe))
17091 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17092 if (EQ (prop, Qright_fringe))
17093 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17094 if (EQ (prop, Qleft_margin))
17095 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17096 if (EQ (prop, Qright_margin))
17097 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17098 if (EQ (prop, Qscroll_bar))
17099 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17102 prop = Fbuffer_local_value (prop, it->w->buffer);
17105 if (INTEGERP (prop) || FLOATP (prop))
17107 int base_unit = (width_p
17108 ? FRAME_COLUMN_WIDTH (it->f)
17109 : FRAME_LINE_HEIGHT (it->f));
17110 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17113 if (CONSP (prop))
17115 Lisp_Object car = XCAR (prop);
17116 Lisp_Object cdr = XCDR (prop);
17118 if (SYMBOLP (car))
17120 #ifdef HAVE_WINDOW_SYSTEM
17121 if (valid_image_p (prop))
17123 int id = lookup_image (it->f, prop);
17124 struct image *img = IMAGE_FROM_ID (it->f, id);
17126 return OK_PIXELS (width_p ? img->width : img->height);
17128 #endif
17129 if (EQ (car, Qplus) || EQ (car, Qminus))
17131 int first = 1;
17132 double px;
17134 pixels = 0;
17135 while (CONSP (cdr))
17137 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17138 font, width_p, align_to))
17139 return 0;
17140 if (first)
17141 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17142 else
17143 pixels += px;
17144 cdr = XCDR (cdr);
17146 if (EQ (car, Qminus))
17147 pixels = -pixels;
17148 return OK_PIXELS (pixels);
17151 car = Fbuffer_local_value (car, it->w->buffer);
17154 if (INTEGERP (car) || FLOATP (car))
17156 double fact;
17157 pixels = XFLOATINT (car);
17158 if (NILP (cdr))
17159 return OK_PIXELS (pixels);
17160 if (calc_pixel_width_or_height (&fact, it, cdr,
17161 font, width_p, align_to))
17162 return OK_PIXELS (pixels * fact);
17163 return 0;
17166 return 0;
17169 return 0;
17173 /***********************************************************************
17174 Glyph Display
17175 ***********************************************************************/
17177 #ifdef HAVE_WINDOW_SYSTEM
17179 #if GLYPH_DEBUG
17181 void
17182 dump_glyph_string (s)
17183 struct glyph_string *s;
17185 fprintf (stderr, "glyph string\n");
17186 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17187 s->x, s->y, s->width, s->height);
17188 fprintf (stderr, " ybase = %d\n", s->ybase);
17189 fprintf (stderr, " hl = %d\n", s->hl);
17190 fprintf (stderr, " left overhang = %d, right = %d\n",
17191 s->left_overhang, s->right_overhang);
17192 fprintf (stderr, " nchars = %d\n", s->nchars);
17193 fprintf (stderr, " extends to end of line = %d\n",
17194 s->extends_to_end_of_line_p);
17195 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17196 fprintf (stderr, " bg width = %d\n", s->background_width);
17199 #endif /* GLYPH_DEBUG */
17201 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17202 of XChar2b structures for S; it can't be allocated in
17203 init_glyph_string because it must be allocated via `alloca'. W
17204 is the window on which S is drawn. ROW and AREA are the glyph row
17205 and area within the row from which S is constructed. START is the
17206 index of the first glyph structure covered by S. HL is a
17207 face-override for drawing S. */
17209 #ifdef HAVE_NTGUI
17210 #define OPTIONAL_HDC(hdc) hdc,
17211 #define DECLARE_HDC(hdc) HDC hdc;
17212 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17213 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17214 #endif
17216 #ifndef OPTIONAL_HDC
17217 #define OPTIONAL_HDC(hdc)
17218 #define DECLARE_HDC(hdc)
17219 #define ALLOCATE_HDC(hdc, f)
17220 #define RELEASE_HDC(hdc, f)
17221 #endif
17223 static void
17224 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17225 struct glyph_string *s;
17226 DECLARE_HDC (hdc)
17227 XChar2b *char2b;
17228 struct window *w;
17229 struct glyph_row *row;
17230 enum glyph_row_area area;
17231 int start;
17232 enum draw_glyphs_face hl;
17234 bzero (s, sizeof *s);
17235 s->w = w;
17236 s->f = XFRAME (w->frame);
17237 #ifdef HAVE_NTGUI
17238 s->hdc = hdc;
17239 #endif
17240 s->display = FRAME_X_DISPLAY (s->f);
17241 s->window = FRAME_X_WINDOW (s->f);
17242 s->char2b = char2b;
17243 s->hl = hl;
17244 s->row = row;
17245 s->area = area;
17246 s->first_glyph = row->glyphs[area] + start;
17247 s->height = row->height;
17248 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17250 /* Display the internal border below the tool-bar window. */
17251 if (s->w == XWINDOW (s->f->tool_bar_window))
17252 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17254 s->ybase = s->y + row->ascent;
17258 /* Append the list of glyph strings with head H and tail T to the list
17259 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17261 static INLINE void
17262 append_glyph_string_lists (head, tail, h, t)
17263 struct glyph_string **head, **tail;
17264 struct glyph_string *h, *t;
17266 if (h)
17268 if (*head)
17269 (*tail)->next = h;
17270 else
17271 *head = h;
17272 h->prev = *tail;
17273 *tail = t;
17278 /* Prepend the list of glyph strings with head H and tail T to the
17279 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17280 result. */
17282 static INLINE void
17283 prepend_glyph_string_lists (head, tail, h, t)
17284 struct glyph_string **head, **tail;
17285 struct glyph_string *h, *t;
17287 if (h)
17289 if (*head)
17290 (*head)->prev = t;
17291 else
17292 *tail = t;
17293 t->next = *head;
17294 *head = h;
17299 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17300 Set *HEAD and *TAIL to the resulting list. */
17302 static INLINE void
17303 append_glyph_string (head, tail, s)
17304 struct glyph_string **head, **tail;
17305 struct glyph_string *s;
17307 s->next = s->prev = NULL;
17308 append_glyph_string_lists (head, tail, s, s);
17312 /* Get face and two-byte form of character glyph GLYPH on frame F.
17313 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17314 a pointer to a realized face that is ready for display. */
17316 static INLINE struct face *
17317 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17318 struct frame *f;
17319 struct glyph *glyph;
17320 XChar2b *char2b;
17321 int *two_byte_p;
17323 struct face *face;
17325 xassert (glyph->type == CHAR_GLYPH);
17326 face = FACE_FROM_ID (f, glyph->face_id);
17328 if (two_byte_p)
17329 *two_byte_p = 0;
17331 if (!glyph->multibyte_p)
17333 /* Unibyte case. We don't have to encode, but we have to make
17334 sure to use a face suitable for unibyte. */
17335 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17337 else if (glyph->u.ch < 128
17338 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17340 /* Case of ASCII in a face known to fit ASCII. */
17341 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17343 else
17345 int c1, c2, charset;
17347 /* Split characters into bytes. If c2 is -1 afterwards, C is
17348 really a one-byte character so that byte1 is zero. */
17349 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17350 if (c2 > 0)
17351 STORE_XCHAR2B (char2b, c1, c2);
17352 else
17353 STORE_XCHAR2B (char2b, 0, c1);
17355 /* Maybe encode the character in *CHAR2B. */
17356 if (charset != CHARSET_ASCII)
17358 struct font_info *font_info
17359 = FONT_INFO_FROM_ID (f, face->font_info_id);
17360 if (font_info)
17361 glyph->font_type
17362 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17366 /* Make sure X resources of the face are allocated. */
17367 xassert (face != NULL);
17368 PREPARE_FACE_FOR_DISPLAY (f, face);
17369 return face;
17373 /* Fill glyph string S with composition components specified by S->cmp.
17375 FACES is an array of faces for all components of this composition.
17376 S->gidx is the index of the first component for S.
17377 OVERLAPS_P non-zero means S should draw the foreground only, and
17378 use its physical height for clipping.
17380 Value is the index of a component not in S. */
17382 static int
17383 fill_composite_glyph_string (s, faces, overlaps_p)
17384 struct glyph_string *s;
17385 struct face **faces;
17386 int overlaps_p;
17388 int i;
17390 xassert (s);
17392 s->for_overlaps_p = overlaps_p;
17394 s->face = faces[s->gidx];
17395 s->font = s->face->font;
17396 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17398 /* For all glyphs of this composition, starting at the offset
17399 S->gidx, until we reach the end of the definition or encounter a
17400 glyph that requires the different face, add it to S. */
17401 ++s->nchars;
17402 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17403 ++s->nchars;
17405 /* All glyph strings for the same composition has the same width,
17406 i.e. the width set for the first component of the composition. */
17408 s->width = s->first_glyph->pixel_width;
17410 /* If the specified font could not be loaded, use the frame's
17411 default font, but record the fact that we couldn't load it in
17412 the glyph string so that we can draw rectangles for the
17413 characters of the glyph string. */
17414 if (s->font == NULL)
17416 s->font_not_found_p = 1;
17417 s->font = FRAME_FONT (s->f);
17420 /* Adjust base line for subscript/superscript text. */
17421 s->ybase += s->first_glyph->voffset;
17423 xassert (s->face && s->face->gc);
17425 /* This glyph string must always be drawn with 16-bit functions. */
17426 s->two_byte_p = 1;
17428 return s->gidx + s->nchars;
17432 /* Fill glyph string S from a sequence of character glyphs.
17434 FACE_ID is the face id of the string. START is the index of the
17435 first glyph to consider, END is the index of the last + 1.
17436 OVERLAPS_P non-zero means S should draw the foreground only, and
17437 use its physical height for clipping.
17439 Value is the index of the first glyph not in S. */
17441 static int
17442 fill_glyph_string (s, face_id, start, end, overlaps_p)
17443 struct glyph_string *s;
17444 int face_id;
17445 int start, end, overlaps_p;
17447 struct glyph *glyph, *last;
17448 int voffset;
17449 int glyph_not_available_p;
17451 xassert (s->f == XFRAME (s->w->frame));
17452 xassert (s->nchars == 0);
17453 xassert (start >= 0 && end > start);
17455 s->for_overlaps_p = overlaps_p,
17456 glyph = s->row->glyphs[s->area] + start;
17457 last = s->row->glyphs[s->area] + end;
17458 voffset = glyph->voffset;
17460 glyph_not_available_p = glyph->glyph_not_available_p;
17462 while (glyph < last
17463 && glyph->type == CHAR_GLYPH
17464 && glyph->voffset == voffset
17465 /* Same face id implies same font, nowadays. */
17466 && glyph->face_id == face_id
17467 && glyph->glyph_not_available_p == glyph_not_available_p)
17469 int two_byte_p;
17471 s->face = get_glyph_face_and_encoding (s->f, glyph,
17472 s->char2b + s->nchars,
17473 &two_byte_p);
17474 s->two_byte_p = two_byte_p;
17475 ++s->nchars;
17476 xassert (s->nchars <= end - start);
17477 s->width += glyph->pixel_width;
17478 ++glyph;
17481 s->font = s->face->font;
17482 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17484 /* If the specified font could not be loaded, use the frame's font,
17485 but record the fact that we couldn't load it in
17486 S->font_not_found_p so that we can draw rectangles for the
17487 characters of the glyph string. */
17488 if (s->font == NULL || glyph_not_available_p)
17490 s->font_not_found_p = 1;
17491 s->font = FRAME_FONT (s->f);
17494 /* Adjust base line for subscript/superscript text. */
17495 s->ybase += voffset;
17497 xassert (s->face && s->face->gc);
17498 return glyph - s->row->glyphs[s->area];
17502 /* Fill glyph string S from image glyph S->first_glyph. */
17504 static void
17505 fill_image_glyph_string (s)
17506 struct glyph_string *s;
17508 xassert (s->first_glyph->type == IMAGE_GLYPH);
17509 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17510 xassert (s->img);
17511 s->slice = s->first_glyph->slice;
17512 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17513 s->font = s->face->font;
17514 s->width = s->first_glyph->pixel_width;
17516 /* Adjust base line for subscript/superscript text. */
17517 s->ybase += s->first_glyph->voffset;
17521 /* Fill glyph string S from a sequence of stretch glyphs.
17523 ROW is the glyph row in which the glyphs are found, AREA is the
17524 area within the row. START is the index of the first glyph to
17525 consider, END is the index of the last + 1.
17527 Value is the index of the first glyph not in S. */
17529 static int
17530 fill_stretch_glyph_string (s, row, area, start, end)
17531 struct glyph_string *s;
17532 struct glyph_row *row;
17533 enum glyph_row_area area;
17534 int start, end;
17536 struct glyph *glyph, *last;
17537 int voffset, face_id;
17539 xassert (s->first_glyph->type == STRETCH_GLYPH);
17541 glyph = s->row->glyphs[s->area] + start;
17542 last = s->row->glyphs[s->area] + end;
17543 face_id = glyph->face_id;
17544 s->face = FACE_FROM_ID (s->f, face_id);
17545 s->font = s->face->font;
17546 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17547 s->width = glyph->pixel_width;
17548 voffset = glyph->voffset;
17550 for (++glyph;
17551 (glyph < last
17552 && glyph->type == STRETCH_GLYPH
17553 && glyph->voffset == voffset
17554 && glyph->face_id == face_id);
17555 ++glyph)
17556 s->width += glyph->pixel_width;
17558 /* Adjust base line for subscript/superscript text. */
17559 s->ybase += voffset;
17561 /* The case that face->gc == 0 is handled when drawing the glyph
17562 string by calling PREPARE_FACE_FOR_DISPLAY. */
17563 xassert (s->face);
17564 return glyph - s->row->glyphs[s->area];
17568 /* EXPORT for RIF:
17569 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17570 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17571 assumed to be zero. */
17573 void
17574 x_get_glyph_overhangs (glyph, f, left, right)
17575 struct glyph *glyph;
17576 struct frame *f;
17577 int *left, *right;
17579 *left = *right = 0;
17581 if (glyph->type == CHAR_GLYPH)
17583 XFontStruct *font;
17584 struct face *face;
17585 struct font_info *font_info;
17586 XChar2b char2b;
17587 XCharStruct *pcm;
17589 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17590 font = face->font;
17591 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17592 if (font /* ++KFS: Should this be font_info ? */
17593 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17595 if (pcm->rbearing > pcm->width)
17596 *right = pcm->rbearing - pcm->width;
17597 if (pcm->lbearing < 0)
17598 *left = -pcm->lbearing;
17604 /* Return the index of the first glyph preceding glyph string S that
17605 is overwritten by S because of S's left overhang. Value is -1
17606 if no glyphs are overwritten. */
17608 static int
17609 left_overwritten (s)
17610 struct glyph_string *s;
17612 int k;
17614 if (s->left_overhang)
17616 int x = 0, i;
17617 struct glyph *glyphs = s->row->glyphs[s->area];
17618 int first = s->first_glyph - glyphs;
17620 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17621 x -= glyphs[i].pixel_width;
17623 k = i + 1;
17625 else
17626 k = -1;
17628 return k;
17632 /* Return the index of the first glyph preceding glyph string S that
17633 is overwriting S because of its right overhang. Value is -1 if no
17634 glyph in front of S overwrites S. */
17636 static int
17637 left_overwriting (s)
17638 struct glyph_string *s;
17640 int i, k, x;
17641 struct glyph *glyphs = s->row->glyphs[s->area];
17642 int first = s->first_glyph - glyphs;
17644 k = -1;
17645 x = 0;
17646 for (i = first - 1; i >= 0; --i)
17648 int left, right;
17649 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17650 if (x + right > 0)
17651 k = i;
17652 x -= glyphs[i].pixel_width;
17655 return k;
17659 /* Return the index of the last glyph following glyph string S that is
17660 not overwritten by S because of S's right overhang. Value is -1 if
17661 no such glyph is found. */
17663 static int
17664 right_overwritten (s)
17665 struct glyph_string *s;
17667 int k = -1;
17669 if (s->right_overhang)
17671 int x = 0, i;
17672 struct glyph *glyphs = s->row->glyphs[s->area];
17673 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17674 int end = s->row->used[s->area];
17676 for (i = first; i < end && s->right_overhang > x; ++i)
17677 x += glyphs[i].pixel_width;
17679 k = i;
17682 return k;
17686 /* Return the index of the last glyph following glyph string S that
17687 overwrites S because of its left overhang. Value is negative
17688 if no such glyph is found. */
17690 static int
17691 right_overwriting (s)
17692 struct glyph_string *s;
17694 int i, k, x;
17695 int end = s->row->used[s->area];
17696 struct glyph *glyphs = s->row->glyphs[s->area];
17697 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17699 k = -1;
17700 x = 0;
17701 for (i = first; i < end; ++i)
17703 int left, right;
17704 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17705 if (x - left < 0)
17706 k = i;
17707 x += glyphs[i].pixel_width;
17710 return k;
17714 /* Get face and two-byte form of character C in face FACE_ID on frame
17715 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17716 means we want to display multibyte text. DISPLAY_P non-zero means
17717 make sure that X resources for the face returned are allocated.
17718 Value is a pointer to a realized face that is ready for display if
17719 DISPLAY_P is non-zero. */
17721 static INLINE struct face *
17722 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17723 struct frame *f;
17724 int c, face_id;
17725 XChar2b *char2b;
17726 int multibyte_p, display_p;
17728 struct face *face = FACE_FROM_ID (f, face_id);
17730 if (!multibyte_p)
17732 /* Unibyte case. We don't have to encode, but we have to make
17733 sure to use a face suitable for unibyte. */
17734 STORE_XCHAR2B (char2b, 0, c);
17735 face_id = FACE_FOR_CHAR (f, face, c);
17736 face = FACE_FROM_ID (f, face_id);
17738 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17740 /* Case of ASCII in a face known to fit ASCII. */
17741 STORE_XCHAR2B (char2b, 0, c);
17743 else
17745 int c1, c2, charset;
17747 /* Split characters into bytes. If c2 is -1 afterwards, C is
17748 really a one-byte character so that byte1 is zero. */
17749 SPLIT_CHAR (c, charset, c1, c2);
17750 if (c2 > 0)
17751 STORE_XCHAR2B (char2b, c1, c2);
17752 else
17753 STORE_XCHAR2B (char2b, 0, c1);
17755 /* Maybe encode the character in *CHAR2B. */
17756 if (face->font != NULL)
17758 struct font_info *font_info
17759 = FONT_INFO_FROM_ID (f, face->font_info_id);
17760 if (font_info)
17761 rif->encode_char (c, char2b, font_info, 0);
17765 /* Make sure X resources of the face are allocated. */
17766 #ifdef HAVE_X_WINDOWS
17767 if (display_p)
17768 #endif
17770 xassert (face != NULL);
17771 PREPARE_FACE_FOR_DISPLAY (f, face);
17774 return face;
17778 /* Set background width of glyph string S. START is the index of the
17779 first glyph following S. LAST_X is the right-most x-position + 1
17780 in the drawing area. */
17782 static INLINE void
17783 set_glyph_string_background_width (s, start, last_x)
17784 struct glyph_string *s;
17785 int start;
17786 int last_x;
17788 /* If the face of this glyph string has to be drawn to the end of
17789 the drawing area, set S->extends_to_end_of_line_p. */
17790 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17792 if (start == s->row->used[s->area]
17793 && s->area == TEXT_AREA
17794 && ((s->hl == DRAW_NORMAL_TEXT
17795 && (s->row->fill_line_p
17796 || s->face->background != default_face->background
17797 || s->face->stipple != default_face->stipple
17798 || s->row->mouse_face_p))
17799 || s->hl == DRAW_MOUSE_FACE
17800 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17801 && s->row->fill_line_p)))
17802 s->extends_to_end_of_line_p = 1;
17804 /* If S extends its face to the end of the line, set its
17805 background_width to the distance to the right edge of the drawing
17806 area. */
17807 if (s->extends_to_end_of_line_p)
17808 s->background_width = last_x - s->x + 1;
17809 else
17810 s->background_width = s->width;
17814 /* Compute overhangs and x-positions for glyph string S and its
17815 predecessors, or successors. X is the starting x-position for S.
17816 BACKWARD_P non-zero means process predecessors. */
17818 static void
17819 compute_overhangs_and_x (s, x, backward_p)
17820 struct glyph_string *s;
17821 int x;
17822 int backward_p;
17824 if (backward_p)
17826 while (s)
17828 if (rif->compute_glyph_string_overhangs)
17829 rif->compute_glyph_string_overhangs (s);
17830 x -= s->width;
17831 s->x = x;
17832 s = s->prev;
17835 else
17837 while (s)
17839 if (rif->compute_glyph_string_overhangs)
17840 rif->compute_glyph_string_overhangs (s);
17841 s->x = x;
17842 x += s->width;
17843 s = s->next;
17850 /* The following macros are only called from draw_glyphs below.
17851 They reference the following parameters of that function directly:
17852 `w', `row', `area', and `overlap_p'
17853 as well as the following local variables:
17854 `s', `f', and `hdc' (in W32) */
17856 #ifdef HAVE_NTGUI
17857 /* On W32, silently add local `hdc' variable to argument list of
17858 init_glyph_string. */
17859 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17860 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17861 #else
17862 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17863 init_glyph_string (s, char2b, w, row, area, start, hl)
17864 #endif
17866 /* Add a glyph string for a stretch glyph to the list of strings
17867 between HEAD and TAIL. START is the index of the stretch glyph in
17868 row area AREA of glyph row ROW. END is the index of the last glyph
17869 in that glyph row area. X is the current output position assigned
17870 to the new glyph string constructed. HL overrides that face of the
17871 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17872 is the right-most x-position of the drawing area. */
17874 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17875 and below -- keep them on one line. */
17876 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17877 do \
17879 s = (struct glyph_string *) alloca (sizeof *s); \
17880 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17881 START = fill_stretch_glyph_string (s, row, area, START, END); \
17882 append_glyph_string (&HEAD, &TAIL, s); \
17883 s->x = (X); \
17885 while (0)
17888 /* Add a glyph string for an image glyph to the list of strings
17889 between HEAD and TAIL. START is the index of the image glyph in
17890 row area AREA of glyph row ROW. END is the index of the last glyph
17891 in that glyph row area. X is the current output position assigned
17892 to the new glyph string constructed. HL overrides that face of the
17893 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17894 is the right-most x-position of the drawing area. */
17896 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17897 do \
17899 s = (struct glyph_string *) alloca (sizeof *s); \
17900 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17901 fill_image_glyph_string (s); \
17902 append_glyph_string (&HEAD, &TAIL, s); \
17903 ++START; \
17904 s->x = (X); \
17906 while (0)
17909 /* Add a glyph string for a sequence of character glyphs to the list
17910 of strings between HEAD and TAIL. START is the index of the first
17911 glyph in row area AREA of glyph row ROW that is part of the new
17912 glyph string. END is the index of the last glyph in that glyph row
17913 area. X is the current output position assigned to the new glyph
17914 string constructed. HL overrides that face of the glyph; e.g. it
17915 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17916 right-most x-position of the drawing area. */
17918 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17919 do \
17921 int c, face_id; \
17922 XChar2b *char2b; \
17924 c = (row)->glyphs[area][START].u.ch; \
17925 face_id = (row)->glyphs[area][START].face_id; \
17927 s = (struct glyph_string *) alloca (sizeof *s); \
17928 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17929 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17930 append_glyph_string (&HEAD, &TAIL, s); \
17931 s->x = (X); \
17932 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17934 while (0)
17937 /* Add a glyph string for a composite sequence to the list of strings
17938 between HEAD and TAIL. START is the index of the first glyph in
17939 row area AREA of glyph row ROW that is part of the new glyph
17940 string. END is the index of the last glyph in that glyph row area.
17941 X is the current output position assigned to the new glyph string
17942 constructed. HL overrides that face of the glyph; e.g. it is
17943 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17944 x-position of the drawing area. */
17946 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17947 do { \
17948 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17949 int face_id = (row)->glyphs[area][START].face_id; \
17950 struct face *base_face = FACE_FROM_ID (f, face_id); \
17951 struct composition *cmp = composition_table[cmp_id]; \
17952 int glyph_len = cmp->glyph_len; \
17953 XChar2b *char2b; \
17954 struct face **faces; \
17955 struct glyph_string *first_s = NULL; \
17956 int n; \
17958 base_face = base_face->ascii_face; \
17959 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17960 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17961 /* At first, fill in `char2b' and `faces'. */ \
17962 for (n = 0; n < glyph_len; n++) \
17964 int c = COMPOSITION_GLYPH (cmp, n); \
17965 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17966 faces[n] = FACE_FROM_ID (f, this_face_id); \
17967 get_char_face_and_encoding (f, c, this_face_id, \
17968 char2b + n, 1, 1); \
17971 /* Make glyph_strings for each glyph sequence that is drawable by \
17972 the same face, and append them to HEAD/TAIL. */ \
17973 for (n = 0; n < cmp->glyph_len;) \
17975 s = (struct glyph_string *) alloca (sizeof *s); \
17976 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17977 append_glyph_string (&(HEAD), &(TAIL), s); \
17978 s->cmp = cmp; \
17979 s->gidx = n; \
17980 s->x = (X); \
17982 if (n == 0) \
17983 first_s = s; \
17985 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17988 ++START; \
17989 s = first_s; \
17990 } while (0)
17993 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17994 of AREA of glyph row ROW on window W between indices START and END.
17995 HL overrides the face for drawing glyph strings, e.g. it is
17996 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17997 x-positions of the drawing area.
17999 This is an ugly monster macro construct because we must use alloca
18000 to allocate glyph strings (because draw_glyphs can be called
18001 asynchronously). */
18003 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18004 do \
18006 HEAD = TAIL = NULL; \
18007 while (START < END) \
18009 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18010 switch (first_glyph->type) \
18012 case CHAR_GLYPH: \
18013 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18014 HL, X, LAST_X); \
18015 break; \
18017 case COMPOSITE_GLYPH: \
18018 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18019 HL, X, LAST_X); \
18020 break; \
18022 case STRETCH_GLYPH: \
18023 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18024 HL, X, LAST_X); \
18025 break; \
18027 case IMAGE_GLYPH: \
18028 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18029 HL, X, LAST_X); \
18030 break; \
18032 default: \
18033 abort (); \
18036 set_glyph_string_background_width (s, START, LAST_X); \
18037 (X) += s->width; \
18040 while (0)
18043 /* Draw glyphs between START and END in AREA of ROW on window W,
18044 starting at x-position X. X is relative to AREA in W. HL is a
18045 face-override with the following meaning:
18047 DRAW_NORMAL_TEXT draw normally
18048 DRAW_CURSOR draw in cursor face
18049 DRAW_MOUSE_FACE draw in mouse face.
18050 DRAW_INVERSE_VIDEO draw in mode line face
18051 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18052 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18054 If OVERLAPS_P is non-zero, draw only the foreground of characters
18055 and clip to the physical height of ROW.
18057 Value is the x-position reached, relative to AREA of W. */
18059 static int
18060 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18061 struct window *w;
18062 int x;
18063 struct glyph_row *row;
18064 enum glyph_row_area area;
18065 int start, end;
18066 enum draw_glyphs_face hl;
18067 int overlaps_p;
18069 struct glyph_string *head, *tail;
18070 struct glyph_string *s;
18071 int last_x, area_width;
18072 int x_reached;
18073 int i, j;
18074 struct frame *f = XFRAME (WINDOW_FRAME (w));
18075 DECLARE_HDC (hdc);
18077 ALLOCATE_HDC (hdc, f);
18079 /* Let's rather be paranoid than getting a SEGV. */
18080 end = min (end, row->used[area]);
18081 start = max (0, start);
18082 start = min (end, start);
18084 /* Translate X to frame coordinates. Set last_x to the right
18085 end of the drawing area. */
18086 if (row->full_width_p)
18088 /* X is relative to the left edge of W, without scroll bars
18089 or fringes. */
18090 x += WINDOW_LEFT_EDGE_X (w);
18091 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18093 else
18095 int area_left = window_box_left (w, area);
18096 x += area_left;
18097 area_width = window_box_width (w, area);
18098 last_x = area_left + area_width;
18101 /* Build a doubly-linked list of glyph_string structures between
18102 head and tail from what we have to draw. Note that the macro
18103 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18104 the reason we use a separate variable `i'. */
18105 i = start;
18106 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18107 if (tail)
18108 x_reached = tail->x + tail->background_width;
18109 else
18110 x_reached = x;
18112 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18113 the row, redraw some glyphs in front or following the glyph
18114 strings built above. */
18115 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18117 int dummy_x = 0;
18118 struct glyph_string *h, *t;
18120 /* Compute overhangs for all glyph strings. */
18121 if (rif->compute_glyph_string_overhangs)
18122 for (s = head; s; s = s->next)
18123 rif->compute_glyph_string_overhangs (s);
18125 /* Prepend glyph strings for glyphs in front of the first glyph
18126 string that are overwritten because of the first glyph
18127 string's left overhang. The background of all strings
18128 prepended must be drawn because the first glyph string
18129 draws over it. */
18130 i = left_overwritten (head);
18131 if (i >= 0)
18133 j = i;
18134 BUILD_GLYPH_STRINGS (j, start, h, t,
18135 DRAW_NORMAL_TEXT, dummy_x, last_x);
18136 start = i;
18137 compute_overhangs_and_x (t, head->x, 1);
18138 prepend_glyph_string_lists (&head, &tail, h, t);
18141 /* Prepend glyph strings for glyphs in front of the first glyph
18142 string that overwrite that glyph string because of their
18143 right overhang. For these strings, only the foreground must
18144 be drawn, because it draws over the glyph string at `head'.
18145 The background must not be drawn because this would overwrite
18146 right overhangs of preceding glyphs for which no glyph
18147 strings exist. */
18148 i = left_overwriting (head);
18149 if (i >= 0)
18151 BUILD_GLYPH_STRINGS (i, start, h, t,
18152 DRAW_NORMAL_TEXT, dummy_x, last_x);
18153 for (s = h; s; s = s->next)
18154 s->background_filled_p = 1;
18155 compute_overhangs_and_x (t, head->x, 1);
18156 prepend_glyph_string_lists (&head, &tail, h, t);
18159 /* Append glyphs strings for glyphs following the last glyph
18160 string tail that are overwritten by tail. The background of
18161 these strings has to be drawn because tail's foreground draws
18162 over it. */
18163 i = right_overwritten (tail);
18164 if (i >= 0)
18166 BUILD_GLYPH_STRINGS (end, i, h, t,
18167 DRAW_NORMAL_TEXT, x, last_x);
18168 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18169 append_glyph_string_lists (&head, &tail, h, t);
18172 /* Append glyph strings for glyphs following the last glyph
18173 string tail that overwrite tail. The foreground of such
18174 glyphs has to be drawn because it writes into the background
18175 of tail. The background must not be drawn because it could
18176 paint over the foreground of following glyphs. */
18177 i = right_overwriting (tail);
18178 if (i >= 0)
18180 BUILD_GLYPH_STRINGS (end, i, h, t,
18181 DRAW_NORMAL_TEXT, x, last_x);
18182 for (s = h; s; s = s->next)
18183 s->background_filled_p = 1;
18184 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18185 append_glyph_string_lists (&head, &tail, h, t);
18189 /* Draw all strings. */
18190 for (s = head; s; s = s->next)
18191 rif->draw_glyph_string (s);
18193 if (area == TEXT_AREA
18194 && !row->full_width_p
18195 /* When drawing overlapping rows, only the glyph strings'
18196 foreground is drawn, which doesn't erase a cursor
18197 completely. */
18198 && !overlaps_p)
18200 int x0 = head ? head->x : x;
18201 int x1 = tail ? tail->x + tail->background_width : x;
18203 int text_left = window_box_left (w, TEXT_AREA);
18204 x0 -= text_left;
18205 x1 -= text_left;
18207 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18208 row->y, MATRIX_ROW_BOTTOM_Y (row));
18211 /* Value is the x-position up to which drawn, relative to AREA of W.
18212 This doesn't include parts drawn because of overhangs. */
18213 if (row->full_width_p)
18214 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18215 else
18216 x_reached -= window_box_left (w, area);
18218 RELEASE_HDC (hdc, f);
18220 return x_reached;
18224 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18225 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18227 static INLINE void
18228 append_glyph (it)
18229 struct it *it;
18231 struct glyph *glyph;
18232 enum glyph_row_area area = it->area;
18234 xassert (it->glyph_row);
18235 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18237 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18238 if (glyph < it->glyph_row->glyphs[area + 1])
18240 glyph->charpos = CHARPOS (it->position);
18241 glyph->object = it->object;
18242 glyph->pixel_width = it->pixel_width;
18243 glyph->ascent = it->ascent;
18244 glyph->descent = it->descent;
18245 glyph->voffset = it->voffset;
18246 glyph->type = CHAR_GLYPH;
18247 glyph->multibyte_p = it->multibyte_p;
18248 glyph->left_box_line_p = it->start_of_box_run_p;
18249 glyph->right_box_line_p = it->end_of_box_run_p;
18250 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18251 || it->phys_descent > it->descent);
18252 glyph->padding_p = 0;
18253 glyph->glyph_not_available_p = it->glyph_not_available_p;
18254 glyph->face_id = it->face_id;
18255 glyph->u.ch = it->char_to_display;
18256 glyph->slice = null_glyph_slice;
18257 glyph->font_type = FONT_TYPE_UNKNOWN;
18258 ++it->glyph_row->used[area];
18260 else if (!fonts_changed_p)
18262 it->w->ncols_scale_factor++;
18263 fonts_changed_p = 1;
18267 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18268 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18270 static INLINE void
18271 append_composite_glyph (it)
18272 struct it *it;
18274 struct glyph *glyph;
18275 enum glyph_row_area area = it->area;
18277 xassert (it->glyph_row);
18279 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18280 if (glyph < it->glyph_row->glyphs[area + 1])
18282 glyph->charpos = CHARPOS (it->position);
18283 glyph->object = it->object;
18284 glyph->pixel_width = it->pixel_width;
18285 glyph->ascent = it->ascent;
18286 glyph->descent = it->descent;
18287 glyph->voffset = it->voffset;
18288 glyph->type = COMPOSITE_GLYPH;
18289 glyph->multibyte_p = it->multibyte_p;
18290 glyph->left_box_line_p = it->start_of_box_run_p;
18291 glyph->right_box_line_p = it->end_of_box_run_p;
18292 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18293 || it->phys_descent > it->descent);
18294 glyph->padding_p = 0;
18295 glyph->glyph_not_available_p = 0;
18296 glyph->face_id = it->face_id;
18297 glyph->u.cmp_id = it->cmp_id;
18298 glyph->slice = null_glyph_slice;
18299 glyph->font_type = FONT_TYPE_UNKNOWN;
18300 ++it->glyph_row->used[area];
18302 else if (!fonts_changed_p)
18304 it->w->ncols_scale_factor++;
18305 fonts_changed_p = 1;
18310 /* Change IT->ascent and IT->height according to the setting of
18311 IT->voffset. */
18313 static INLINE void
18314 take_vertical_position_into_account (it)
18315 struct it *it;
18317 if (it->voffset)
18319 if (it->voffset < 0)
18320 /* Increase the ascent so that we can display the text higher
18321 in the line. */
18322 it->ascent -= it->voffset;
18323 else
18324 /* Increase the descent so that we can display the text lower
18325 in the line. */
18326 it->descent += it->voffset;
18331 /* Produce glyphs/get display metrics for the image IT is loaded with.
18332 See the description of struct display_iterator in dispextern.h for
18333 an overview of struct display_iterator. */
18335 static void
18336 produce_image_glyph (it)
18337 struct it *it;
18339 struct image *img;
18340 struct face *face;
18341 int face_ascent, glyph_ascent;
18342 struct glyph_slice slice;
18344 xassert (it->what == IT_IMAGE);
18346 face = FACE_FROM_ID (it->f, it->face_id);
18347 xassert (face);
18348 /* Make sure X resources of the face is loaded. */
18349 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18351 if (it->image_id < 0)
18353 /* Fringe bitmap. */
18354 it->ascent = it->phys_ascent = 0;
18355 it->descent = it->phys_descent = 0;
18356 it->pixel_width = 0;
18357 it->nglyphs = 0;
18358 return;
18361 img = IMAGE_FROM_ID (it->f, it->image_id);
18362 xassert (img);
18363 /* Make sure X resources of the image is loaded. */
18364 prepare_image_for_display (it->f, img);
18366 slice.x = slice.y = 0;
18367 slice.width = img->width;
18368 slice.height = img->height;
18370 if (INTEGERP (it->slice.x))
18371 slice.x = XINT (it->slice.x);
18372 else if (FLOATP (it->slice.x))
18373 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18375 if (INTEGERP (it->slice.y))
18376 slice.y = XINT (it->slice.y);
18377 else if (FLOATP (it->slice.y))
18378 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18380 if (INTEGERP (it->slice.width))
18381 slice.width = XINT (it->slice.width);
18382 else if (FLOATP (it->slice.width))
18383 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18385 if (INTEGERP (it->slice.height))
18386 slice.height = XINT (it->slice.height);
18387 else if (FLOATP (it->slice.height))
18388 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18390 if (slice.x >= img->width)
18391 slice.x = img->width;
18392 if (slice.y >= img->height)
18393 slice.y = img->height;
18394 if (slice.x + slice.width >= img->width)
18395 slice.width = img->width - slice.x;
18396 if (slice.y + slice.height > img->height)
18397 slice.height = img->height - slice.y;
18399 if (slice.width == 0 || slice.height == 0)
18400 return;
18402 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18404 it->descent = slice.height - glyph_ascent;
18405 if (slice.y == 0)
18406 it->descent += img->vmargin;
18407 if (slice.y + slice.height == img->height)
18408 it->descent += img->vmargin;
18409 it->phys_descent = it->descent;
18411 it->pixel_width = slice.width;
18412 if (slice.x == 0)
18413 it->pixel_width += img->hmargin;
18414 if (slice.x + slice.width == img->width)
18415 it->pixel_width += img->hmargin;
18417 /* It's quite possible for images to have an ascent greater than
18418 their height, so don't get confused in that case. */
18419 if (it->descent < 0)
18420 it->descent = 0;
18422 #if 0 /* this breaks image tiling */
18423 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18424 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18425 if (face_ascent > it->ascent)
18426 it->ascent = it->phys_ascent = face_ascent;
18427 #endif
18429 it->nglyphs = 1;
18431 if (face->box != FACE_NO_BOX)
18433 if (face->box_line_width > 0)
18435 if (slice.y == 0)
18436 it->ascent += face->box_line_width;
18437 if (slice.y + slice.height == img->height)
18438 it->descent += face->box_line_width;
18441 if (it->start_of_box_run_p && slice.x == 0)
18442 it->pixel_width += abs (face->box_line_width);
18443 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18444 it->pixel_width += abs (face->box_line_width);
18447 take_vertical_position_into_account (it);
18449 if (it->glyph_row)
18451 struct glyph *glyph;
18452 enum glyph_row_area area = it->area;
18454 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18455 if (glyph < it->glyph_row->glyphs[area + 1])
18457 glyph->charpos = CHARPOS (it->position);
18458 glyph->object = it->object;
18459 glyph->pixel_width = it->pixel_width;
18460 glyph->ascent = glyph_ascent;
18461 glyph->descent = it->descent;
18462 glyph->voffset = it->voffset;
18463 glyph->type = IMAGE_GLYPH;
18464 glyph->multibyte_p = it->multibyte_p;
18465 glyph->left_box_line_p = it->start_of_box_run_p;
18466 glyph->right_box_line_p = it->end_of_box_run_p;
18467 glyph->overlaps_vertically_p = 0;
18468 glyph->padding_p = 0;
18469 glyph->glyph_not_available_p = 0;
18470 glyph->face_id = it->face_id;
18471 glyph->u.img_id = img->id;
18472 glyph->slice = slice;
18473 glyph->font_type = FONT_TYPE_UNKNOWN;
18474 ++it->glyph_row->used[area];
18476 else if (!fonts_changed_p)
18478 it->w->ncols_scale_factor++;
18479 fonts_changed_p = 1;
18485 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18486 of the glyph, WIDTH and HEIGHT are the width and height of the
18487 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18489 static void
18490 append_stretch_glyph (it, object, width, height, ascent)
18491 struct it *it;
18492 Lisp_Object object;
18493 int width, height;
18494 int ascent;
18496 struct glyph *glyph;
18497 enum glyph_row_area area = it->area;
18499 xassert (ascent >= 0 && ascent <= height);
18501 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18502 if (glyph < it->glyph_row->glyphs[area + 1])
18504 glyph->charpos = CHARPOS (it->position);
18505 glyph->object = object;
18506 glyph->pixel_width = width;
18507 glyph->ascent = ascent;
18508 glyph->descent = height - ascent;
18509 glyph->voffset = it->voffset;
18510 glyph->type = STRETCH_GLYPH;
18511 glyph->multibyte_p = it->multibyte_p;
18512 glyph->left_box_line_p = it->start_of_box_run_p;
18513 glyph->right_box_line_p = it->end_of_box_run_p;
18514 glyph->overlaps_vertically_p = 0;
18515 glyph->padding_p = 0;
18516 glyph->glyph_not_available_p = 0;
18517 glyph->face_id = it->face_id;
18518 glyph->u.stretch.ascent = ascent;
18519 glyph->u.stretch.height = height;
18520 glyph->slice = null_glyph_slice;
18521 glyph->font_type = FONT_TYPE_UNKNOWN;
18522 ++it->glyph_row->used[area];
18524 else if (!fonts_changed_p)
18526 it->w->ncols_scale_factor++;
18527 fonts_changed_p = 1;
18532 /* Produce a stretch glyph for iterator IT. IT->object is the value
18533 of the glyph property displayed. The value must be a list
18534 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18535 being recognized:
18537 1. `:width WIDTH' specifies that the space should be WIDTH *
18538 canonical char width wide. WIDTH may be an integer or floating
18539 point number.
18541 2. `:relative-width FACTOR' specifies that the width of the stretch
18542 should be computed from the width of the first character having the
18543 `glyph' property, and should be FACTOR times that width.
18545 3. `:align-to HPOS' specifies that the space should be wide enough
18546 to reach HPOS, a value in canonical character units.
18548 Exactly one of the above pairs must be present.
18550 4. `:height HEIGHT' specifies that the height of the stretch produced
18551 should be HEIGHT, measured in canonical character units.
18553 5. `:relative-height FACTOR' specifies that the height of the
18554 stretch should be FACTOR times the height of the characters having
18555 the glyph property.
18557 Either none or exactly one of 4 or 5 must be present.
18559 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18560 of the stretch should be used for the ascent of the stretch.
18561 ASCENT must be in the range 0 <= ASCENT <= 100. */
18563 static void
18564 produce_stretch_glyph (it)
18565 struct it *it;
18567 /* (space :width WIDTH :height HEIGHT ...) */
18568 Lisp_Object prop, plist;
18569 int width = 0, height = 0, align_to = -1;
18570 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18571 int ascent = 0;
18572 double tem;
18573 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18574 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18576 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18578 /* List should start with `space'. */
18579 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18580 plist = XCDR (it->object);
18582 /* Compute the width of the stretch. */
18583 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18584 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18586 /* Absolute width `:width WIDTH' specified and valid. */
18587 zero_width_ok_p = 1;
18588 width = (int)tem;
18590 else if (prop = Fplist_get (plist, QCrelative_width),
18591 NUMVAL (prop) > 0)
18593 /* Relative width `:relative-width FACTOR' specified and valid.
18594 Compute the width of the characters having the `glyph'
18595 property. */
18596 struct it it2;
18597 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18599 it2 = *it;
18600 if (it->multibyte_p)
18602 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18603 - IT_BYTEPOS (*it));
18604 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18606 else
18607 it2.c = *p, it2.len = 1;
18609 it2.glyph_row = NULL;
18610 it2.what = IT_CHARACTER;
18611 x_produce_glyphs (&it2);
18612 width = NUMVAL (prop) * it2.pixel_width;
18614 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18615 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18617 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18618 align_to = (align_to < 0
18620 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18621 else if (align_to < 0)
18622 align_to = window_box_left_offset (it->w, TEXT_AREA);
18623 width = max (0, (int)tem + align_to - it->current_x);
18624 zero_width_ok_p = 1;
18626 else
18627 /* Nothing specified -> width defaults to canonical char width. */
18628 width = FRAME_COLUMN_WIDTH (it->f);
18630 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18631 width = 1;
18633 /* Compute height. */
18634 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18635 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18637 height = (int)tem;
18638 zero_height_ok_p = 1;
18640 else if (prop = Fplist_get (plist, QCrelative_height),
18641 NUMVAL (prop) > 0)
18642 height = FONT_HEIGHT (font) * NUMVAL (prop);
18643 else
18644 height = FONT_HEIGHT (font);
18646 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18647 height = 1;
18649 /* Compute percentage of height used for ascent. If
18650 `:ascent ASCENT' is present and valid, use that. Otherwise,
18651 derive the ascent from the font in use. */
18652 if (prop = Fplist_get (plist, QCascent),
18653 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18654 ascent = height * NUMVAL (prop) / 100.0;
18655 else if (!NILP (prop)
18656 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18657 ascent = min (max (0, (int)tem), height);
18658 else
18659 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18661 if (width > 0 && height > 0 && it->glyph_row)
18663 Lisp_Object object = it->stack[it->sp - 1].string;
18664 if (!STRINGP (object))
18665 object = it->w->buffer;
18666 append_stretch_glyph (it, object, width, height, ascent);
18669 it->pixel_width = width;
18670 it->ascent = it->phys_ascent = ascent;
18671 it->descent = it->phys_descent = height - it->ascent;
18672 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18674 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18676 if (face->box_line_width > 0)
18678 it->ascent += face->box_line_width;
18679 it->descent += face->box_line_width;
18682 if (it->start_of_box_run_p)
18683 it->pixel_width += abs (face->box_line_width);
18684 if (it->end_of_box_run_p)
18685 it->pixel_width += abs (face->box_line_width);
18688 take_vertical_position_into_account (it);
18691 /* Calculate line-height and line-spacing properties.
18692 An integer value specifies explicit pixel value.
18693 A float value specifies relative value to current face height.
18694 A cons (float . face-name) specifies relative value to
18695 height of specified face font.
18697 Returns height in pixels, or nil. */
18699 static Lisp_Object
18700 calc_line_height_property (it, prop, font, boff, total)
18701 struct it *it;
18702 Lisp_Object prop;
18703 XFontStruct *font;
18704 int boff, *total;
18706 Lisp_Object position, val;
18707 Lisp_Object face_name = Qnil;
18708 int ascent, descent, height, override;
18710 if (STRINGP (it->object))
18711 position = make_number (IT_STRING_CHARPOS (*it));
18712 else if (BUFFERP (it->object))
18713 position = make_number (IT_CHARPOS (*it));
18714 else
18715 return Qnil;
18717 val = Fget_char_property (position, prop, it->object);
18719 if (NILP (val))
18720 return val;
18722 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18724 *total = 1;
18725 val = XCDR (val);
18728 if (INTEGERP (val))
18729 return val;
18731 if (CONSP (val))
18733 face_name = XCDR (val);
18734 val = XCAR (val);
18736 else if (SYMBOLP (val))
18738 face_name = val;
18739 val = Qnil;
18742 override = EQ (prop, Qline_height);
18744 if (NILP (face_name))
18746 font = FRAME_FONT (it->f);
18747 boff = FRAME_BASELINE_OFFSET (it->f);
18749 else if (EQ (face_name, Qt))
18751 override = 0;
18753 else
18755 int face_id;
18756 struct face *face;
18757 struct font_info *font_info;
18759 face_id = lookup_named_face (it->f, face_name, ' ');
18760 if (face_id < 0)
18761 return make_number (-1);
18763 face = FACE_FROM_ID (it->f, face_id);
18764 font = face->font;
18765 if (font == NULL)
18766 return make_number (-1);
18768 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18769 boff = font_info->baseline_offset;
18770 if (font_info->vertical_centering)
18771 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18774 ascent = FONT_BASE (font) + boff;
18775 descent = FONT_DESCENT (font) - boff;
18777 if (override)
18779 it->override_ascent = ascent;
18780 it->override_descent = descent;
18781 it->override_boff = boff;
18784 height = ascent + descent;
18785 if (FLOATP (val))
18786 height = (int)(XFLOAT_DATA (val) * height);
18787 else if (INTEGERP (val))
18788 height *= XINT (val);
18790 return make_number (height);
18794 /* RIF:
18795 Produce glyphs/get display metrics for the display element IT is
18796 loaded with. See the description of struct display_iterator in
18797 dispextern.h for an overview of struct display_iterator. */
18799 void
18800 x_produce_glyphs (it)
18801 struct it *it;
18803 int extra_line_spacing = it->extra_line_spacing;
18805 it->glyph_not_available_p = 0;
18807 if (it->what == IT_CHARACTER)
18809 XChar2b char2b;
18810 XFontStruct *font;
18811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18812 XCharStruct *pcm;
18813 int font_not_found_p;
18814 struct font_info *font_info;
18815 int boff; /* baseline offset */
18816 /* We may change it->multibyte_p upon unibyte<->multibyte
18817 conversion. So, save the current value now and restore it
18818 later.
18820 Note: It seems that we don't have to record multibyte_p in
18821 struct glyph because the character code itself tells if or
18822 not the character is multibyte. Thus, in the future, we must
18823 consider eliminating the field `multibyte_p' in the struct
18824 glyph. */
18825 int saved_multibyte_p = it->multibyte_p;
18827 /* Maybe translate single-byte characters to multibyte, or the
18828 other way. */
18829 it->char_to_display = it->c;
18830 if (!ASCII_BYTE_P (it->c))
18832 if (unibyte_display_via_language_environment
18833 && SINGLE_BYTE_CHAR_P (it->c)
18834 && (it->c >= 0240
18835 || !NILP (Vnonascii_translation_table)))
18837 it->char_to_display = unibyte_char_to_multibyte (it->c);
18838 it->multibyte_p = 1;
18839 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18840 face = FACE_FROM_ID (it->f, it->face_id);
18842 else if (!SINGLE_BYTE_CHAR_P (it->c)
18843 && !it->multibyte_p)
18845 it->multibyte_p = 1;
18846 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18847 face = FACE_FROM_ID (it->f, it->face_id);
18851 /* Get font to use. Encode IT->char_to_display. */
18852 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18853 &char2b, it->multibyte_p, 0);
18854 font = face->font;
18856 /* When no suitable font found, use the default font. */
18857 font_not_found_p = font == NULL;
18858 if (font_not_found_p)
18860 font = FRAME_FONT (it->f);
18861 boff = FRAME_BASELINE_OFFSET (it->f);
18862 font_info = NULL;
18864 else
18866 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18867 boff = font_info->baseline_offset;
18868 if (font_info->vertical_centering)
18869 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18872 if (it->char_to_display >= ' '
18873 && (!it->multibyte_p || it->char_to_display < 128))
18875 /* Either unibyte or ASCII. */
18876 int stretched_p;
18878 it->nglyphs = 1;
18880 pcm = rif->per_char_metric (font, &char2b,
18881 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18883 if (it->override_ascent >= 0)
18885 it->ascent = it->override_ascent;
18886 it->descent = it->override_descent;
18887 boff = it->override_boff;
18889 else
18891 it->ascent = FONT_BASE (font) + boff;
18892 it->descent = FONT_DESCENT (font) - boff;
18895 if (pcm)
18897 it->phys_ascent = pcm->ascent + boff;
18898 it->phys_descent = pcm->descent - boff;
18899 it->pixel_width = pcm->width;
18901 else
18903 it->glyph_not_available_p = 1;
18904 it->phys_ascent = it->ascent;
18905 it->phys_descent = it->descent;
18906 it->pixel_width = FONT_WIDTH (font);
18909 if (it->constrain_row_ascent_descent_p)
18911 if (it->descent > it->max_descent)
18913 it->ascent += it->descent - it->max_descent;
18914 it->descent = it->max_descent;
18916 if (it->ascent > it->max_ascent)
18918 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18919 it->ascent = it->max_ascent;
18921 it->phys_ascent = min (it->phys_ascent, it->ascent);
18922 it->phys_descent = min (it->phys_descent, it->descent);
18923 extra_line_spacing = 0;
18926 /* If this is a space inside a region of text with
18927 `space-width' property, change its width. */
18928 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18929 if (stretched_p)
18930 it->pixel_width *= XFLOATINT (it->space_width);
18932 /* If face has a box, add the box thickness to the character
18933 height. If character has a box line to the left and/or
18934 right, add the box line width to the character's width. */
18935 if (face->box != FACE_NO_BOX)
18937 int thick = face->box_line_width;
18939 if (thick > 0)
18941 it->ascent += thick;
18942 it->descent += thick;
18944 else
18945 thick = -thick;
18947 if (it->start_of_box_run_p)
18948 it->pixel_width += thick;
18949 if (it->end_of_box_run_p)
18950 it->pixel_width += thick;
18953 /* If face has an overline, add the height of the overline
18954 (1 pixel) and a 1 pixel margin to the character height. */
18955 if (face->overline_p)
18956 it->ascent += 2;
18958 if (it->constrain_row_ascent_descent_p)
18960 if (it->ascent > it->max_ascent)
18961 it->ascent = it->max_ascent;
18962 if (it->descent > it->max_descent)
18963 it->descent = it->max_descent;
18966 take_vertical_position_into_account (it);
18968 /* If we have to actually produce glyphs, do it. */
18969 if (it->glyph_row)
18971 if (stretched_p)
18973 /* Translate a space with a `space-width' property
18974 into a stretch glyph. */
18975 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18976 / FONT_HEIGHT (font));
18977 append_stretch_glyph (it, it->object, it->pixel_width,
18978 it->ascent + it->descent, ascent);
18980 else
18981 append_glyph (it);
18983 /* If characters with lbearing or rbearing are displayed
18984 in this line, record that fact in a flag of the
18985 glyph row. This is used to optimize X output code. */
18986 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18987 it->glyph_row->contains_overlapping_glyphs_p = 1;
18990 else if (it->char_to_display == '\n')
18992 /* A newline has no width but we need the height of the line.
18993 But if previous part of the line set a height, don't
18994 increase that height */
18996 Lisp_Object height;
18998 it->override_ascent = -1;
18999 it->pixel_width = 0;
19000 it->nglyphs = 0;
19002 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19004 if (it->override_ascent >= 0)
19006 it->ascent = it->override_ascent;
19007 it->descent = it->override_descent;
19008 boff = it->override_boff;
19010 else
19012 it->ascent = FONT_BASE (font) + boff;
19013 it->descent = FONT_DESCENT (font) - boff;
19016 if (EQ (height, make_number(0)))
19018 if (it->descent > it->max_descent)
19020 it->ascent += it->descent - it->max_descent;
19021 it->descent = it->max_descent;
19023 if (it->ascent > it->max_ascent)
19025 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19026 it->ascent = it->max_ascent;
19028 it->phys_ascent = min (it->phys_ascent, it->ascent);
19029 it->phys_descent = min (it->phys_descent, it->descent);
19030 it->constrain_row_ascent_descent_p = 1;
19031 extra_line_spacing = 0;
19033 else
19035 Lisp_Object spacing;
19036 int total = 0;
19038 it->phys_ascent = it->ascent;
19039 it->phys_descent = it->descent;
19041 if ((it->max_ascent > 0 || it->max_descent > 0)
19042 && face->box != FACE_NO_BOX
19043 && face->box_line_width > 0)
19045 it->ascent += face->box_line_width;
19046 it->descent += face->box_line_width;
19048 if (!NILP (height)
19049 && XINT (height) > it->ascent + it->descent)
19050 it->ascent = XINT (height) - it->descent;
19052 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19053 if (INTEGERP (spacing))
19055 extra_line_spacing = XINT (spacing);
19056 if (total)
19057 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19061 else if (it->char_to_display == '\t')
19063 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19064 int x = it->current_x + it->continuation_lines_width;
19065 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19067 /* If the distance from the current position to the next tab
19068 stop is less than a canonical character width, use the
19069 tab stop after that. */
19070 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19071 next_tab_x += tab_width;
19073 it->pixel_width = next_tab_x - x;
19074 it->nglyphs = 1;
19075 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19076 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19078 if (it->glyph_row)
19080 append_stretch_glyph (it, it->object, it->pixel_width,
19081 it->ascent + it->descent, it->ascent);
19084 else
19086 /* A multi-byte character. Assume that the display width of the
19087 character is the width of the character multiplied by the
19088 width of the font. */
19090 /* If we found a font, this font should give us the right
19091 metrics. If we didn't find a font, use the frame's
19092 default font and calculate the width of the character
19093 from the charset width; this is what old redisplay code
19094 did. */
19096 pcm = rif->per_char_metric (font, &char2b,
19097 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19099 if (font_not_found_p || !pcm)
19101 int charset = CHAR_CHARSET (it->char_to_display);
19103 it->glyph_not_available_p = 1;
19104 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19105 * CHARSET_WIDTH (charset));
19106 it->phys_ascent = FONT_BASE (font) + boff;
19107 it->phys_descent = FONT_DESCENT (font) - boff;
19109 else
19111 it->pixel_width = pcm->width;
19112 it->phys_ascent = pcm->ascent + boff;
19113 it->phys_descent = pcm->descent - boff;
19114 if (it->glyph_row
19115 && (pcm->lbearing < 0
19116 || pcm->rbearing > pcm->width))
19117 it->glyph_row->contains_overlapping_glyphs_p = 1;
19119 it->nglyphs = 1;
19120 it->ascent = FONT_BASE (font) + boff;
19121 it->descent = FONT_DESCENT (font) - boff;
19122 if (face->box != FACE_NO_BOX)
19124 int thick = face->box_line_width;
19126 if (thick > 0)
19128 it->ascent += thick;
19129 it->descent += thick;
19131 else
19132 thick = - thick;
19134 if (it->start_of_box_run_p)
19135 it->pixel_width += thick;
19136 if (it->end_of_box_run_p)
19137 it->pixel_width += thick;
19140 /* If face has an overline, add the height of the overline
19141 (1 pixel) and a 1 pixel margin to the character height. */
19142 if (face->overline_p)
19143 it->ascent += 2;
19145 take_vertical_position_into_account (it);
19147 if (it->glyph_row)
19148 append_glyph (it);
19150 it->multibyte_p = saved_multibyte_p;
19152 else if (it->what == IT_COMPOSITION)
19154 /* Note: A composition is represented as one glyph in the
19155 glyph matrix. There are no padding glyphs. */
19156 XChar2b char2b;
19157 XFontStruct *font;
19158 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19159 XCharStruct *pcm;
19160 int font_not_found_p;
19161 struct font_info *font_info;
19162 int boff; /* baseline offset */
19163 struct composition *cmp = composition_table[it->cmp_id];
19165 /* Maybe translate single-byte characters to multibyte. */
19166 it->char_to_display = it->c;
19167 if (unibyte_display_via_language_environment
19168 && SINGLE_BYTE_CHAR_P (it->c)
19169 && (it->c >= 0240
19170 || (it->c >= 0200
19171 && !NILP (Vnonascii_translation_table))))
19173 it->char_to_display = unibyte_char_to_multibyte (it->c);
19176 /* Get face and font to use. Encode IT->char_to_display. */
19177 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19178 face = FACE_FROM_ID (it->f, it->face_id);
19179 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19180 &char2b, it->multibyte_p, 0);
19181 font = face->font;
19183 /* When no suitable font found, use the default font. */
19184 font_not_found_p = font == NULL;
19185 if (font_not_found_p)
19187 font = FRAME_FONT (it->f);
19188 boff = FRAME_BASELINE_OFFSET (it->f);
19189 font_info = NULL;
19191 else
19193 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19194 boff = font_info->baseline_offset;
19195 if (font_info->vertical_centering)
19196 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19199 /* There are no padding glyphs, so there is only one glyph to
19200 produce for the composition. Important is that pixel_width,
19201 ascent and descent are the values of what is drawn by
19202 draw_glyphs (i.e. the values of the overall glyphs composed). */
19203 it->nglyphs = 1;
19205 /* If we have not yet calculated pixel size data of glyphs of
19206 the composition for the current face font, calculate them
19207 now. Theoretically, we have to check all fonts for the
19208 glyphs, but that requires much time and memory space. So,
19209 here we check only the font of the first glyph. This leads
19210 to incorrect display very rarely, and C-l (recenter) can
19211 correct the display anyway. */
19212 if (cmp->font != (void *) font)
19214 /* Ascent and descent of the font of the first character of
19215 this composition (adjusted by baseline offset). Ascent
19216 and descent of overall glyphs should not be less than
19217 them respectively. */
19218 int font_ascent = FONT_BASE (font) + boff;
19219 int font_descent = FONT_DESCENT (font) - boff;
19220 /* Bounding box of the overall glyphs. */
19221 int leftmost, rightmost, lowest, highest;
19222 int i, width, ascent, descent;
19224 cmp->font = (void *) font;
19226 /* Initialize the bounding box. */
19227 if (font_info
19228 && (pcm = rif->per_char_metric (font, &char2b,
19229 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19231 width = pcm->width;
19232 ascent = pcm->ascent;
19233 descent = pcm->descent;
19235 else
19237 width = FONT_WIDTH (font);
19238 ascent = FONT_BASE (font);
19239 descent = FONT_DESCENT (font);
19242 rightmost = width;
19243 lowest = - descent + boff;
19244 highest = ascent + boff;
19245 leftmost = 0;
19247 if (font_info
19248 && font_info->default_ascent
19249 && CHAR_TABLE_P (Vuse_default_ascent)
19250 && !NILP (Faref (Vuse_default_ascent,
19251 make_number (it->char_to_display))))
19252 highest = font_info->default_ascent + boff;
19254 /* Draw the first glyph at the normal position. It may be
19255 shifted to right later if some other glyphs are drawn at
19256 the left. */
19257 cmp->offsets[0] = 0;
19258 cmp->offsets[1] = boff;
19260 /* Set cmp->offsets for the remaining glyphs. */
19261 for (i = 1; i < cmp->glyph_len; i++)
19263 int left, right, btm, top;
19264 int ch = COMPOSITION_GLYPH (cmp, i);
19265 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19267 face = FACE_FROM_ID (it->f, face_id);
19268 get_char_face_and_encoding (it->f, ch, face->id,
19269 &char2b, it->multibyte_p, 0);
19270 font = face->font;
19271 if (font == NULL)
19273 font = FRAME_FONT (it->f);
19274 boff = FRAME_BASELINE_OFFSET (it->f);
19275 font_info = NULL;
19277 else
19279 font_info
19280 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19281 boff = font_info->baseline_offset;
19282 if (font_info->vertical_centering)
19283 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19286 if (font_info
19287 && (pcm = rif->per_char_metric (font, &char2b,
19288 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19290 width = pcm->width;
19291 ascent = pcm->ascent;
19292 descent = pcm->descent;
19294 else
19296 width = FONT_WIDTH (font);
19297 ascent = 1;
19298 descent = 0;
19301 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19303 /* Relative composition with or without
19304 alternate chars. */
19305 left = (leftmost + rightmost - width) / 2;
19306 btm = - descent + boff;
19307 if (font_info && font_info->relative_compose
19308 && (! CHAR_TABLE_P (Vignore_relative_composition)
19309 || NILP (Faref (Vignore_relative_composition,
19310 make_number (ch)))))
19313 if (- descent >= font_info->relative_compose)
19314 /* One extra pixel between two glyphs. */
19315 btm = highest + 1;
19316 else if (ascent <= 0)
19317 /* One extra pixel between two glyphs. */
19318 btm = lowest - 1 - ascent - descent;
19321 else
19323 /* A composition rule is specified by an integer
19324 value that encodes global and new reference
19325 points (GREF and NREF). GREF and NREF are
19326 specified by numbers as below:
19328 0---1---2 -- ascent
19332 9--10--11 -- center
19334 ---3---4---5--- baseline
19336 6---7---8 -- descent
19338 int rule = COMPOSITION_RULE (cmp, i);
19339 int gref, nref, grefx, grefy, nrefx, nrefy;
19341 COMPOSITION_DECODE_RULE (rule, gref, nref);
19342 grefx = gref % 3, nrefx = nref % 3;
19343 grefy = gref / 3, nrefy = nref / 3;
19345 left = (leftmost
19346 + grefx * (rightmost - leftmost) / 2
19347 - nrefx * width / 2);
19348 btm = ((grefy == 0 ? highest
19349 : grefy == 1 ? 0
19350 : grefy == 2 ? lowest
19351 : (highest + lowest) / 2)
19352 - (nrefy == 0 ? ascent + descent
19353 : nrefy == 1 ? descent - boff
19354 : nrefy == 2 ? 0
19355 : (ascent + descent) / 2));
19358 cmp->offsets[i * 2] = left;
19359 cmp->offsets[i * 2 + 1] = btm + descent;
19361 /* Update the bounding box of the overall glyphs. */
19362 right = left + width;
19363 top = btm + descent + ascent;
19364 if (left < leftmost)
19365 leftmost = left;
19366 if (right > rightmost)
19367 rightmost = right;
19368 if (top > highest)
19369 highest = top;
19370 if (btm < lowest)
19371 lowest = btm;
19374 /* If there are glyphs whose x-offsets are negative,
19375 shift all glyphs to the right and make all x-offsets
19376 non-negative. */
19377 if (leftmost < 0)
19379 for (i = 0; i < cmp->glyph_len; i++)
19380 cmp->offsets[i * 2] -= leftmost;
19381 rightmost -= leftmost;
19384 cmp->pixel_width = rightmost;
19385 cmp->ascent = highest;
19386 cmp->descent = - lowest;
19387 if (cmp->ascent < font_ascent)
19388 cmp->ascent = font_ascent;
19389 if (cmp->descent < font_descent)
19390 cmp->descent = font_descent;
19393 it->pixel_width = cmp->pixel_width;
19394 it->ascent = it->phys_ascent = cmp->ascent;
19395 it->descent = it->phys_descent = cmp->descent;
19397 if (face->box != FACE_NO_BOX)
19399 int thick = face->box_line_width;
19401 if (thick > 0)
19403 it->ascent += thick;
19404 it->descent += thick;
19406 else
19407 thick = - thick;
19409 if (it->start_of_box_run_p)
19410 it->pixel_width += thick;
19411 if (it->end_of_box_run_p)
19412 it->pixel_width += thick;
19415 /* If face has an overline, add the height of the overline
19416 (1 pixel) and a 1 pixel margin to the character height. */
19417 if (face->overline_p)
19418 it->ascent += 2;
19420 take_vertical_position_into_account (it);
19422 if (it->glyph_row)
19423 append_composite_glyph (it);
19425 else if (it->what == IT_IMAGE)
19426 produce_image_glyph (it);
19427 else if (it->what == IT_STRETCH)
19428 produce_stretch_glyph (it);
19430 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19431 because this isn't true for images with `:ascent 100'. */
19432 xassert (it->ascent >= 0 && it->descent >= 0);
19433 if (it->area == TEXT_AREA)
19434 it->current_x += it->pixel_width;
19436 if (extra_line_spacing > 0)
19437 it->descent += extra_line_spacing;
19439 it->max_ascent = max (it->max_ascent, it->ascent);
19440 it->max_descent = max (it->max_descent, it->descent);
19441 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19442 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19445 /* EXPORT for RIF:
19446 Output LEN glyphs starting at START at the nominal cursor position.
19447 Advance the nominal cursor over the text. The global variable
19448 updated_window contains the window being updated, updated_row is
19449 the glyph row being updated, and updated_area is the area of that
19450 row being updated. */
19452 void
19453 x_write_glyphs (start, len)
19454 struct glyph *start;
19455 int len;
19457 int x, hpos;
19459 xassert (updated_window && updated_row);
19460 BLOCK_INPUT;
19462 /* Write glyphs. */
19464 hpos = start - updated_row->glyphs[updated_area];
19465 x = draw_glyphs (updated_window, output_cursor.x,
19466 updated_row, updated_area,
19467 hpos, hpos + len,
19468 DRAW_NORMAL_TEXT, 0);
19470 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19471 if (updated_area == TEXT_AREA
19472 && updated_window->phys_cursor_on_p
19473 && updated_window->phys_cursor.vpos == output_cursor.vpos
19474 && updated_window->phys_cursor.hpos >= hpos
19475 && updated_window->phys_cursor.hpos < hpos + len)
19476 updated_window->phys_cursor_on_p = 0;
19478 UNBLOCK_INPUT;
19480 /* Advance the output cursor. */
19481 output_cursor.hpos += len;
19482 output_cursor.x = x;
19486 /* EXPORT for RIF:
19487 Insert LEN glyphs from START at the nominal cursor position. */
19489 void
19490 x_insert_glyphs (start, len)
19491 struct glyph *start;
19492 int len;
19494 struct frame *f;
19495 struct window *w;
19496 int line_height, shift_by_width, shifted_region_width;
19497 struct glyph_row *row;
19498 struct glyph *glyph;
19499 int frame_x, frame_y, hpos;
19501 xassert (updated_window && updated_row);
19502 BLOCK_INPUT;
19503 w = updated_window;
19504 f = XFRAME (WINDOW_FRAME (w));
19506 /* Get the height of the line we are in. */
19507 row = updated_row;
19508 line_height = row->height;
19510 /* Get the width of the glyphs to insert. */
19511 shift_by_width = 0;
19512 for (glyph = start; glyph < start + len; ++glyph)
19513 shift_by_width += glyph->pixel_width;
19515 /* Get the width of the region to shift right. */
19516 shifted_region_width = (window_box_width (w, updated_area)
19517 - output_cursor.x
19518 - shift_by_width);
19520 /* Shift right. */
19521 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19522 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19524 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19525 line_height, shift_by_width);
19527 /* Write the glyphs. */
19528 hpos = start - row->glyphs[updated_area];
19529 draw_glyphs (w, output_cursor.x, row, updated_area,
19530 hpos, hpos + len,
19531 DRAW_NORMAL_TEXT, 0);
19533 /* Advance the output cursor. */
19534 output_cursor.hpos += len;
19535 output_cursor.x += shift_by_width;
19536 UNBLOCK_INPUT;
19540 /* EXPORT for RIF:
19541 Erase the current text line from the nominal cursor position
19542 (inclusive) to pixel column TO_X (exclusive). The idea is that
19543 everything from TO_X onward is already erased.
19545 TO_X is a pixel position relative to updated_area of
19546 updated_window. TO_X == -1 means clear to the end of this area. */
19548 void
19549 x_clear_end_of_line (to_x)
19550 int to_x;
19552 struct frame *f;
19553 struct window *w = updated_window;
19554 int max_x, min_y, max_y;
19555 int from_x, from_y, to_y;
19557 xassert (updated_window && updated_row);
19558 f = XFRAME (w->frame);
19560 if (updated_row->full_width_p)
19561 max_x = WINDOW_TOTAL_WIDTH (w);
19562 else
19563 max_x = window_box_width (w, updated_area);
19564 max_y = window_text_bottom_y (w);
19566 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19567 of window. For TO_X > 0, truncate to end of drawing area. */
19568 if (to_x == 0)
19569 return;
19570 else if (to_x < 0)
19571 to_x = max_x;
19572 else
19573 to_x = min (to_x, max_x);
19575 to_y = min (max_y, output_cursor.y + updated_row->height);
19577 /* Notice if the cursor will be cleared by this operation. */
19578 if (!updated_row->full_width_p)
19579 notice_overwritten_cursor (w, updated_area,
19580 output_cursor.x, -1,
19581 updated_row->y,
19582 MATRIX_ROW_BOTTOM_Y (updated_row));
19584 from_x = output_cursor.x;
19586 /* Translate to frame coordinates. */
19587 if (updated_row->full_width_p)
19589 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19590 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19592 else
19594 int area_left = window_box_left (w, updated_area);
19595 from_x += area_left;
19596 to_x += area_left;
19599 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19600 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19601 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19603 /* Prevent inadvertently clearing to end of the X window. */
19604 if (to_x > from_x && to_y > from_y)
19606 BLOCK_INPUT;
19607 rif->clear_frame_area (f, from_x, from_y,
19608 to_x - from_x, to_y - from_y);
19609 UNBLOCK_INPUT;
19613 #endif /* HAVE_WINDOW_SYSTEM */
19617 /***********************************************************************
19618 Cursor types
19619 ***********************************************************************/
19621 /* Value is the internal representation of the specified cursor type
19622 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19623 of the bar cursor. */
19625 static enum text_cursor_kinds
19626 get_specified_cursor_type (arg, width)
19627 Lisp_Object arg;
19628 int *width;
19630 enum text_cursor_kinds type;
19632 if (NILP (arg))
19633 return NO_CURSOR;
19635 if (EQ (arg, Qbox))
19636 return FILLED_BOX_CURSOR;
19638 if (EQ (arg, Qhollow))
19639 return HOLLOW_BOX_CURSOR;
19641 if (EQ (arg, Qbar))
19643 *width = 2;
19644 return BAR_CURSOR;
19647 if (CONSP (arg)
19648 && EQ (XCAR (arg), Qbar)
19649 && INTEGERP (XCDR (arg))
19650 && XINT (XCDR (arg)) >= 0)
19652 *width = XINT (XCDR (arg));
19653 return BAR_CURSOR;
19656 if (EQ (arg, Qhbar))
19658 *width = 2;
19659 return HBAR_CURSOR;
19662 if (CONSP (arg)
19663 && EQ (XCAR (arg), Qhbar)
19664 && INTEGERP (XCDR (arg))
19665 && XINT (XCDR (arg)) >= 0)
19667 *width = XINT (XCDR (arg));
19668 return HBAR_CURSOR;
19671 /* Treat anything unknown as "hollow box cursor".
19672 It was bad to signal an error; people have trouble fixing
19673 .Xdefaults with Emacs, when it has something bad in it. */
19674 type = HOLLOW_BOX_CURSOR;
19676 return type;
19679 /* Set the default cursor types for specified frame. */
19680 void
19681 set_frame_cursor_types (f, arg)
19682 struct frame *f;
19683 Lisp_Object arg;
19685 int width;
19686 Lisp_Object tem;
19688 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19689 FRAME_CURSOR_WIDTH (f) = width;
19691 /* By default, set up the blink-off state depending on the on-state. */
19693 tem = Fassoc (arg, Vblink_cursor_alist);
19694 if (!NILP (tem))
19696 FRAME_BLINK_OFF_CURSOR (f)
19697 = get_specified_cursor_type (XCDR (tem), &width);
19698 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19700 else
19701 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19705 /* Return the cursor we want to be displayed in window W. Return
19706 width of bar/hbar cursor through WIDTH arg. Return with
19707 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19708 (i.e. if the `system caret' should track this cursor).
19710 In a mini-buffer window, we want the cursor only to appear if we
19711 are reading input from this window. For the selected window, we
19712 want the cursor type given by the frame parameter or buffer local
19713 setting of cursor-type. If explicitly marked off, draw no cursor.
19714 In all other cases, we want a hollow box cursor. */
19716 static enum text_cursor_kinds
19717 get_window_cursor_type (w, glyph, width, active_cursor)
19718 struct window *w;
19719 struct glyph *glyph;
19720 int *width;
19721 int *active_cursor;
19723 struct frame *f = XFRAME (w->frame);
19724 struct buffer *b = XBUFFER (w->buffer);
19725 int cursor_type = DEFAULT_CURSOR;
19726 Lisp_Object alt_cursor;
19727 int non_selected = 0;
19729 *active_cursor = 1;
19731 /* Echo area */
19732 if (cursor_in_echo_area
19733 && FRAME_HAS_MINIBUF_P (f)
19734 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19736 if (w == XWINDOW (echo_area_window))
19738 *width = FRAME_CURSOR_WIDTH (f);
19739 return FRAME_DESIRED_CURSOR (f);
19742 *active_cursor = 0;
19743 non_selected = 1;
19746 /* Nonselected window or nonselected frame. */
19747 else if (w != XWINDOW (f->selected_window)
19748 #ifdef HAVE_WINDOW_SYSTEM
19749 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19750 #endif
19753 *active_cursor = 0;
19755 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19756 return NO_CURSOR;
19758 non_selected = 1;
19761 /* Never display a cursor in a window in which cursor-type is nil. */
19762 if (NILP (b->cursor_type))
19763 return NO_CURSOR;
19765 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19766 if (non_selected)
19768 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19769 return get_specified_cursor_type (alt_cursor, width);
19772 /* Get the normal cursor type for this window. */
19773 if (EQ (b->cursor_type, Qt))
19775 cursor_type = FRAME_DESIRED_CURSOR (f);
19776 *width = FRAME_CURSOR_WIDTH (f);
19778 else
19779 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19781 /* Use normal cursor if not blinked off. */
19782 if (!w->cursor_off_p)
19784 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19785 if (cursor_type == FILLED_BOX_CURSOR)
19786 cursor_type = HOLLOW_BOX_CURSOR;
19788 return cursor_type;
19791 /* Cursor is blinked off, so determine how to "toggle" it. */
19793 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19794 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19795 return get_specified_cursor_type (XCDR (alt_cursor), width);
19797 /* Then see if frame has specified a specific blink off cursor type. */
19798 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19800 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19801 return FRAME_BLINK_OFF_CURSOR (f);
19804 #if 0
19805 /* Some people liked having a permanently visible blinking cursor,
19806 while others had very strong opinions against it. So it was
19807 decided to remove it. KFS 2003-09-03 */
19809 /* Finally perform built-in cursor blinking:
19810 filled box <-> hollow box
19811 wide [h]bar <-> narrow [h]bar
19812 narrow [h]bar <-> no cursor
19813 other type <-> no cursor */
19815 if (cursor_type == FILLED_BOX_CURSOR)
19816 return HOLLOW_BOX_CURSOR;
19818 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19820 *width = 1;
19821 return cursor_type;
19823 #endif
19825 return NO_CURSOR;
19829 #ifdef HAVE_WINDOW_SYSTEM
19831 /* Notice when the text cursor of window W has been completely
19832 overwritten by a drawing operation that outputs glyphs in AREA
19833 starting at X0 and ending at X1 in the line starting at Y0 and
19834 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19835 the rest of the line after X0 has been written. Y coordinates
19836 are window-relative. */
19838 static void
19839 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19840 struct window *w;
19841 enum glyph_row_area area;
19842 int x0, y0, x1, y1;
19844 int cx0, cx1, cy0, cy1;
19845 struct glyph_row *row;
19847 if (!w->phys_cursor_on_p)
19848 return;
19849 if (area != TEXT_AREA)
19850 return;
19852 row = w->current_matrix->rows + w->phys_cursor.vpos;
19853 if (!row->displays_text_p)
19854 return;
19856 if (row->cursor_in_fringe_p)
19858 row->cursor_in_fringe_p = 0;
19859 draw_fringe_bitmap (w, row, 0);
19860 w->phys_cursor_on_p = 0;
19861 return;
19864 cx0 = w->phys_cursor.x;
19865 cx1 = cx0 + w->phys_cursor_width;
19866 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19867 return;
19869 /* The cursor image will be completely removed from the
19870 screen if the output area intersects the cursor area in
19871 y-direction. When we draw in [y0 y1[, and some part of
19872 the cursor is at y < y0, that part must have been drawn
19873 before. When scrolling, the cursor is erased before
19874 actually scrolling, so we don't come here. When not
19875 scrolling, the rows above the old cursor row must have
19876 changed, and in this case these rows must have written
19877 over the cursor image.
19879 Likewise if part of the cursor is below y1, with the
19880 exception of the cursor being in the first blank row at
19881 the buffer and window end because update_text_area
19882 doesn't draw that row. (Except when it does, but
19883 that's handled in update_text_area.) */
19885 cy0 = w->phys_cursor.y;
19886 cy1 = cy0 + w->phys_cursor_height;
19887 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19888 return;
19890 w->phys_cursor_on_p = 0;
19893 #endif /* HAVE_WINDOW_SYSTEM */
19896 /************************************************************************
19897 Mouse Face
19898 ************************************************************************/
19900 #ifdef HAVE_WINDOW_SYSTEM
19902 /* EXPORT for RIF:
19903 Fix the display of area AREA of overlapping row ROW in window W. */
19905 void
19906 x_fix_overlapping_area (w, row, area)
19907 struct window *w;
19908 struct glyph_row *row;
19909 enum glyph_row_area area;
19911 int i, x;
19913 BLOCK_INPUT;
19915 x = 0;
19916 for (i = 0; i < row->used[area];)
19918 if (row->glyphs[area][i].overlaps_vertically_p)
19920 int start = i, start_x = x;
19924 x += row->glyphs[area][i].pixel_width;
19925 ++i;
19927 while (i < row->used[area]
19928 && row->glyphs[area][i].overlaps_vertically_p);
19930 draw_glyphs (w, start_x, row, area,
19931 start, i,
19932 DRAW_NORMAL_TEXT, 1);
19934 else
19936 x += row->glyphs[area][i].pixel_width;
19937 ++i;
19941 UNBLOCK_INPUT;
19945 /* EXPORT:
19946 Draw the cursor glyph of window W in glyph row ROW. See the
19947 comment of draw_glyphs for the meaning of HL. */
19949 void
19950 draw_phys_cursor_glyph (w, row, hl)
19951 struct window *w;
19952 struct glyph_row *row;
19953 enum draw_glyphs_face hl;
19955 /* If cursor hpos is out of bounds, don't draw garbage. This can
19956 happen in mini-buffer windows when switching between echo area
19957 glyphs and mini-buffer. */
19958 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19960 int on_p = w->phys_cursor_on_p;
19961 int x1;
19962 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19963 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19964 hl, 0);
19965 w->phys_cursor_on_p = on_p;
19967 if (hl == DRAW_CURSOR)
19968 w->phys_cursor_width = x1 - w->phys_cursor.x;
19969 /* When we erase the cursor, and ROW is overlapped by other
19970 rows, make sure that these overlapping parts of other rows
19971 are redrawn. */
19972 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19974 if (row > w->current_matrix->rows
19975 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19976 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19978 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19979 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19980 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19986 /* EXPORT:
19987 Erase the image of a cursor of window W from the screen. */
19989 void
19990 erase_phys_cursor (w)
19991 struct window *w;
19993 struct frame *f = XFRAME (w->frame);
19994 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19995 int hpos = w->phys_cursor.hpos;
19996 int vpos = w->phys_cursor.vpos;
19997 int mouse_face_here_p = 0;
19998 struct glyph_matrix *active_glyphs = w->current_matrix;
19999 struct glyph_row *cursor_row;
20000 struct glyph *cursor_glyph;
20001 enum draw_glyphs_face hl;
20003 /* No cursor displayed or row invalidated => nothing to do on the
20004 screen. */
20005 if (w->phys_cursor_type == NO_CURSOR)
20006 goto mark_cursor_off;
20008 /* VPOS >= active_glyphs->nrows means that window has been resized.
20009 Don't bother to erase the cursor. */
20010 if (vpos >= active_glyphs->nrows)
20011 goto mark_cursor_off;
20013 /* If row containing cursor is marked invalid, there is nothing we
20014 can do. */
20015 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20016 if (!cursor_row->enabled_p)
20017 goto mark_cursor_off;
20019 /* If row is completely invisible, don't attempt to delete a cursor which
20020 isn't there. This can happen if cursor is at top of a window, and
20021 we switch to a buffer with a header line in that window. */
20022 if (cursor_row->visible_height <= 0)
20023 goto mark_cursor_off;
20025 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20026 if (cursor_row->cursor_in_fringe_p)
20028 cursor_row->cursor_in_fringe_p = 0;
20029 draw_fringe_bitmap (w, cursor_row, 0);
20030 goto mark_cursor_off;
20033 /* This can happen when the new row is shorter than the old one.
20034 In this case, either draw_glyphs or clear_end_of_line
20035 should have cleared the cursor. Note that we wouldn't be
20036 able to erase the cursor in this case because we don't have a
20037 cursor glyph at hand. */
20038 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20039 goto mark_cursor_off;
20041 /* If the cursor is in the mouse face area, redisplay that when
20042 we clear the cursor. */
20043 if (! NILP (dpyinfo->mouse_face_window)
20044 && w == XWINDOW (dpyinfo->mouse_face_window)
20045 && (vpos > dpyinfo->mouse_face_beg_row
20046 || (vpos == dpyinfo->mouse_face_beg_row
20047 && hpos >= dpyinfo->mouse_face_beg_col))
20048 && (vpos < dpyinfo->mouse_face_end_row
20049 || (vpos == dpyinfo->mouse_face_end_row
20050 && hpos < dpyinfo->mouse_face_end_col))
20051 /* Don't redraw the cursor's spot in mouse face if it is at the
20052 end of a line (on a newline). The cursor appears there, but
20053 mouse highlighting does not. */
20054 && cursor_row->used[TEXT_AREA] > hpos)
20055 mouse_face_here_p = 1;
20057 /* Maybe clear the display under the cursor. */
20058 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20060 int x, y;
20061 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20063 cursor_glyph = get_phys_cursor_glyph (w);
20064 if (cursor_glyph == NULL)
20065 goto mark_cursor_off;
20067 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20068 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20070 rif->clear_frame_area (f, x, y,
20071 cursor_glyph->pixel_width, cursor_row->visible_height);
20074 /* Erase the cursor by redrawing the character underneath it. */
20075 if (mouse_face_here_p)
20076 hl = DRAW_MOUSE_FACE;
20077 else
20078 hl = DRAW_NORMAL_TEXT;
20079 draw_phys_cursor_glyph (w, cursor_row, hl);
20081 mark_cursor_off:
20082 w->phys_cursor_on_p = 0;
20083 w->phys_cursor_type = NO_CURSOR;
20087 /* EXPORT:
20088 Display or clear cursor of window W. If ON is zero, clear the
20089 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20090 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20092 void
20093 display_and_set_cursor (w, on, hpos, vpos, x, y)
20094 struct window *w;
20095 int on, hpos, vpos, x, y;
20097 struct frame *f = XFRAME (w->frame);
20098 int new_cursor_type;
20099 int new_cursor_width;
20100 int active_cursor;
20101 struct glyph_row *glyph_row;
20102 struct glyph *glyph;
20104 /* This is pointless on invisible frames, and dangerous on garbaged
20105 windows and frames; in the latter case, the frame or window may
20106 be in the midst of changing its size, and x and y may be off the
20107 window. */
20108 if (! FRAME_VISIBLE_P (f)
20109 || FRAME_GARBAGED_P (f)
20110 || vpos >= w->current_matrix->nrows
20111 || hpos >= w->current_matrix->matrix_w)
20112 return;
20114 /* If cursor is off and we want it off, return quickly. */
20115 if (!on && !w->phys_cursor_on_p)
20116 return;
20118 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20119 /* If cursor row is not enabled, we don't really know where to
20120 display the cursor. */
20121 if (!glyph_row->enabled_p)
20123 w->phys_cursor_on_p = 0;
20124 return;
20127 glyph = NULL;
20128 if (!glyph_row->exact_window_width_line_p
20129 || hpos < glyph_row->used[TEXT_AREA])
20130 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20132 xassert (interrupt_input_blocked);
20134 /* Set new_cursor_type to the cursor we want to be displayed. */
20135 new_cursor_type = get_window_cursor_type (w, glyph,
20136 &new_cursor_width, &active_cursor);
20138 /* If cursor is currently being shown and we don't want it to be or
20139 it is in the wrong place, or the cursor type is not what we want,
20140 erase it. */
20141 if (w->phys_cursor_on_p
20142 && (!on
20143 || w->phys_cursor.x != x
20144 || w->phys_cursor.y != y
20145 || new_cursor_type != w->phys_cursor_type
20146 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20147 && new_cursor_width != w->phys_cursor_width)))
20148 erase_phys_cursor (w);
20150 /* Don't check phys_cursor_on_p here because that flag is only set
20151 to zero in some cases where we know that the cursor has been
20152 completely erased, to avoid the extra work of erasing the cursor
20153 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20154 still not be visible, or it has only been partly erased. */
20155 if (on)
20157 w->phys_cursor_ascent = glyph_row->ascent;
20158 w->phys_cursor_height = glyph_row->height;
20160 /* Set phys_cursor_.* before x_draw_.* is called because some
20161 of them may need the information. */
20162 w->phys_cursor.x = x;
20163 w->phys_cursor.y = glyph_row->y;
20164 w->phys_cursor.hpos = hpos;
20165 w->phys_cursor.vpos = vpos;
20168 rif->draw_window_cursor (w, glyph_row, x, y,
20169 new_cursor_type, new_cursor_width,
20170 on, active_cursor);
20174 /* Switch the display of W's cursor on or off, according to the value
20175 of ON. */
20177 static void
20178 update_window_cursor (w, on)
20179 struct window *w;
20180 int on;
20182 /* Don't update cursor in windows whose frame is in the process
20183 of being deleted. */
20184 if (w->current_matrix)
20186 BLOCK_INPUT;
20187 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20188 w->phys_cursor.x, w->phys_cursor.y);
20189 UNBLOCK_INPUT;
20194 /* Call update_window_cursor with parameter ON_P on all leaf windows
20195 in the window tree rooted at W. */
20197 static void
20198 update_cursor_in_window_tree (w, on_p)
20199 struct window *w;
20200 int on_p;
20202 while (w)
20204 if (!NILP (w->hchild))
20205 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20206 else if (!NILP (w->vchild))
20207 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20208 else
20209 update_window_cursor (w, on_p);
20211 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20216 /* EXPORT:
20217 Display the cursor on window W, or clear it, according to ON_P.
20218 Don't change the cursor's position. */
20220 void
20221 x_update_cursor (f, on_p)
20222 struct frame *f;
20223 int on_p;
20225 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20229 /* EXPORT:
20230 Clear the cursor of window W to background color, and mark the
20231 cursor as not shown. This is used when the text where the cursor
20232 is is about to be rewritten. */
20234 void
20235 x_clear_cursor (w)
20236 struct window *w;
20238 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20239 update_window_cursor (w, 0);
20243 /* EXPORT:
20244 Display the active region described by mouse_face_* according to DRAW. */
20246 void
20247 show_mouse_face (dpyinfo, draw)
20248 Display_Info *dpyinfo;
20249 enum draw_glyphs_face draw;
20251 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20252 struct frame *f = XFRAME (WINDOW_FRAME (w));
20254 if (/* If window is in the process of being destroyed, don't bother
20255 to do anything. */
20256 w->current_matrix != NULL
20257 /* Don't update mouse highlight if hidden */
20258 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20259 /* Recognize when we are called to operate on rows that don't exist
20260 anymore. This can happen when a window is split. */
20261 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20263 int phys_cursor_on_p = w->phys_cursor_on_p;
20264 struct glyph_row *row, *first, *last;
20266 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20267 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20269 for (row = first; row <= last && row->enabled_p; ++row)
20271 int start_hpos, end_hpos, start_x;
20273 /* For all but the first row, the highlight starts at column 0. */
20274 if (row == first)
20276 start_hpos = dpyinfo->mouse_face_beg_col;
20277 start_x = dpyinfo->mouse_face_beg_x;
20279 else
20281 start_hpos = 0;
20282 start_x = 0;
20285 if (row == last)
20286 end_hpos = dpyinfo->mouse_face_end_col;
20287 else
20288 end_hpos = row->used[TEXT_AREA];
20290 if (end_hpos > start_hpos)
20292 draw_glyphs (w, start_x, row, TEXT_AREA,
20293 start_hpos, end_hpos,
20294 draw, 0);
20296 row->mouse_face_p
20297 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20301 /* When we've written over the cursor, arrange for it to
20302 be displayed again. */
20303 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20305 BLOCK_INPUT;
20306 display_and_set_cursor (w, 1,
20307 w->phys_cursor.hpos, w->phys_cursor.vpos,
20308 w->phys_cursor.x, w->phys_cursor.y);
20309 UNBLOCK_INPUT;
20313 /* Change the mouse cursor. */
20314 if (draw == DRAW_NORMAL_TEXT)
20315 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20316 else if (draw == DRAW_MOUSE_FACE)
20317 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20318 else
20319 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20322 /* EXPORT:
20323 Clear out the mouse-highlighted active region.
20324 Redraw it un-highlighted first. Value is non-zero if mouse
20325 face was actually drawn unhighlighted. */
20328 clear_mouse_face (dpyinfo)
20329 Display_Info *dpyinfo;
20331 int cleared = 0;
20333 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20335 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20336 cleared = 1;
20339 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20340 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20341 dpyinfo->mouse_face_window = Qnil;
20342 dpyinfo->mouse_face_overlay = Qnil;
20343 return cleared;
20347 /* EXPORT:
20348 Non-zero if physical cursor of window W is within mouse face. */
20351 cursor_in_mouse_face_p (w)
20352 struct window *w;
20354 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20355 int in_mouse_face = 0;
20357 if (WINDOWP (dpyinfo->mouse_face_window)
20358 && XWINDOW (dpyinfo->mouse_face_window) == w)
20360 int hpos = w->phys_cursor.hpos;
20361 int vpos = w->phys_cursor.vpos;
20363 if (vpos >= dpyinfo->mouse_face_beg_row
20364 && vpos <= dpyinfo->mouse_face_end_row
20365 && (vpos > dpyinfo->mouse_face_beg_row
20366 || hpos >= dpyinfo->mouse_face_beg_col)
20367 && (vpos < dpyinfo->mouse_face_end_row
20368 || hpos < dpyinfo->mouse_face_end_col
20369 || dpyinfo->mouse_face_past_end))
20370 in_mouse_face = 1;
20373 return in_mouse_face;
20379 /* Find the glyph matrix position of buffer position CHARPOS in window
20380 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20381 current glyphs must be up to date. If CHARPOS is above window
20382 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20383 of last line in W. In the row containing CHARPOS, stop before glyphs
20384 having STOP as object. */
20386 #if 1 /* This is a version of fast_find_position that's more correct
20387 in the presence of hscrolling, for example. I didn't install
20388 it right away because the problem fixed is minor, it failed
20389 in 20.x as well, and I think it's too risky to install
20390 so near the release of 21.1. 2001-09-25 gerd. */
20392 static int
20393 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20394 struct window *w;
20395 int charpos;
20396 int *hpos, *vpos, *x, *y;
20397 Lisp_Object stop;
20399 struct glyph_row *row, *first;
20400 struct glyph *glyph, *end;
20401 int past_end = 0;
20403 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20404 row = row_containing_pos (w, charpos, first, NULL, 0);
20405 if (row == NULL)
20407 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20409 *x = *y = *hpos = *vpos = 0;
20410 return 1;
20412 else
20414 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20415 past_end = 1;
20419 *x = row->x;
20420 *y = row->y;
20421 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20423 glyph = row->glyphs[TEXT_AREA];
20424 end = glyph + row->used[TEXT_AREA];
20426 /* Skip over glyphs not having an object at the start of the row.
20427 These are special glyphs like truncation marks on terminal
20428 frames. */
20429 if (row->displays_text_p)
20430 while (glyph < end
20431 && INTEGERP (glyph->object)
20432 && !EQ (stop, glyph->object)
20433 && glyph->charpos < 0)
20435 *x += glyph->pixel_width;
20436 ++glyph;
20439 while (glyph < end
20440 && !INTEGERP (glyph->object)
20441 && !EQ (stop, glyph->object)
20442 && (!BUFFERP (glyph->object)
20443 || glyph->charpos < charpos))
20445 *x += glyph->pixel_width;
20446 ++glyph;
20449 *hpos = glyph - row->glyphs[TEXT_AREA];
20450 return !past_end;
20453 #else /* not 1 */
20455 static int
20456 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20457 struct window *w;
20458 int pos;
20459 int *hpos, *vpos, *x, *y;
20460 Lisp_Object stop;
20462 int i;
20463 int lastcol;
20464 int maybe_next_line_p = 0;
20465 int line_start_position;
20466 int yb = window_text_bottom_y (w);
20467 struct glyph_row *row, *best_row;
20468 int row_vpos, best_row_vpos;
20469 int current_x;
20471 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20472 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20474 while (row->y < yb)
20476 if (row->used[TEXT_AREA])
20477 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20478 else
20479 line_start_position = 0;
20481 if (line_start_position > pos)
20482 break;
20483 /* If the position sought is the end of the buffer,
20484 don't include the blank lines at the bottom of the window. */
20485 else if (line_start_position == pos
20486 && pos == BUF_ZV (XBUFFER (w->buffer)))
20488 maybe_next_line_p = 1;
20489 break;
20491 else if (line_start_position > 0)
20493 best_row = row;
20494 best_row_vpos = row_vpos;
20497 if (row->y + row->height >= yb)
20498 break;
20500 ++row;
20501 ++row_vpos;
20504 /* Find the right column within BEST_ROW. */
20505 lastcol = 0;
20506 current_x = best_row->x;
20507 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20509 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20510 int charpos = glyph->charpos;
20512 if (BUFFERP (glyph->object))
20514 if (charpos == pos)
20516 *hpos = i;
20517 *vpos = best_row_vpos;
20518 *x = current_x;
20519 *y = best_row->y;
20520 return 1;
20522 else if (charpos > pos)
20523 break;
20525 else if (EQ (glyph->object, stop))
20526 break;
20528 if (charpos > 0)
20529 lastcol = i;
20530 current_x += glyph->pixel_width;
20533 /* If we're looking for the end of the buffer,
20534 and we didn't find it in the line we scanned,
20535 use the start of the following line. */
20536 if (maybe_next_line_p)
20538 ++best_row;
20539 ++best_row_vpos;
20540 lastcol = 0;
20541 current_x = best_row->x;
20544 *vpos = best_row_vpos;
20545 *hpos = lastcol + 1;
20546 *x = current_x;
20547 *y = best_row->y;
20548 return 0;
20551 #endif /* not 1 */
20554 /* Find the position of the glyph for position POS in OBJECT in
20555 window W's current matrix, and return in *X, *Y the pixel
20556 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20558 RIGHT_P non-zero means return the position of the right edge of the
20559 glyph, RIGHT_P zero means return the left edge position.
20561 If no glyph for POS exists in the matrix, return the position of
20562 the glyph with the next smaller position that is in the matrix, if
20563 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20564 exists in the matrix, return the position of the glyph with the
20565 next larger position in OBJECT.
20567 Value is non-zero if a glyph was found. */
20569 static int
20570 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20571 struct window *w;
20572 int pos;
20573 Lisp_Object object;
20574 int *hpos, *vpos, *x, *y;
20575 int right_p;
20577 int yb = window_text_bottom_y (w);
20578 struct glyph_row *r;
20579 struct glyph *best_glyph = NULL;
20580 struct glyph_row *best_row = NULL;
20581 int best_x = 0;
20583 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20584 r->enabled_p && r->y < yb;
20585 ++r)
20587 struct glyph *g = r->glyphs[TEXT_AREA];
20588 struct glyph *e = g + r->used[TEXT_AREA];
20589 int gx;
20591 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20592 if (EQ (g->object, object))
20594 if (g->charpos == pos)
20596 best_glyph = g;
20597 best_x = gx;
20598 best_row = r;
20599 goto found;
20601 else if (best_glyph == NULL
20602 || ((abs (g->charpos - pos)
20603 < abs (best_glyph->charpos - pos))
20604 && (right_p
20605 ? g->charpos < pos
20606 : g->charpos > pos)))
20608 best_glyph = g;
20609 best_x = gx;
20610 best_row = r;
20615 found:
20617 if (best_glyph)
20619 *x = best_x;
20620 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20622 if (right_p)
20624 *x += best_glyph->pixel_width;
20625 ++*hpos;
20628 *y = best_row->y;
20629 *vpos = best_row - w->current_matrix->rows;
20632 return best_glyph != NULL;
20636 /* See if position X, Y is within a hot-spot of an image. */
20638 static int
20639 on_hot_spot_p (hot_spot, x, y)
20640 Lisp_Object hot_spot;
20641 int x, y;
20643 if (!CONSP (hot_spot))
20644 return 0;
20646 if (EQ (XCAR (hot_spot), Qrect))
20648 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20649 Lisp_Object rect = XCDR (hot_spot);
20650 Lisp_Object tem;
20651 if (!CONSP (rect))
20652 return 0;
20653 if (!CONSP (XCAR (rect)))
20654 return 0;
20655 if (!CONSP (XCDR (rect)))
20656 return 0;
20657 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20658 return 0;
20659 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20660 return 0;
20661 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20662 return 0;
20663 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20664 return 0;
20665 return 1;
20667 else if (EQ (XCAR (hot_spot), Qcircle))
20669 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20670 Lisp_Object circ = XCDR (hot_spot);
20671 Lisp_Object lr, lx0, ly0;
20672 if (CONSP (circ)
20673 && CONSP (XCAR (circ))
20674 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20675 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20676 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20678 double r = XFLOATINT (lr);
20679 double dx = XINT (lx0) - x;
20680 double dy = XINT (ly0) - y;
20681 return (dx * dx + dy * dy <= r * r);
20684 else if (EQ (XCAR (hot_spot), Qpoly))
20686 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20687 if (VECTORP (XCDR (hot_spot)))
20689 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20690 Lisp_Object *poly = v->contents;
20691 int n = v->size;
20692 int i;
20693 int inside = 0;
20694 Lisp_Object lx, ly;
20695 int x0, y0;
20697 /* Need an even number of coordinates, and at least 3 edges. */
20698 if (n < 6 || n & 1)
20699 return 0;
20701 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20702 If count is odd, we are inside polygon. Pixels on edges
20703 may or may not be included depending on actual geometry of the
20704 polygon. */
20705 if ((lx = poly[n-2], !INTEGERP (lx))
20706 || (ly = poly[n-1], !INTEGERP (lx)))
20707 return 0;
20708 x0 = XINT (lx), y0 = XINT (ly);
20709 for (i = 0; i < n; i += 2)
20711 int x1 = x0, y1 = y0;
20712 if ((lx = poly[i], !INTEGERP (lx))
20713 || (ly = poly[i+1], !INTEGERP (ly)))
20714 return 0;
20715 x0 = XINT (lx), y0 = XINT (ly);
20717 /* Does this segment cross the X line? */
20718 if (x0 >= x)
20720 if (x1 >= x)
20721 continue;
20723 else if (x1 < x)
20724 continue;
20725 if (y > y0 && y > y1)
20726 continue;
20727 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20728 inside = !inside;
20730 return inside;
20733 /* If we don't understand the format, pretend we're not in the hot-spot. */
20734 return 0;
20737 Lisp_Object
20738 find_hot_spot (map, x, y)
20739 Lisp_Object map;
20740 int x, y;
20742 while (CONSP (map))
20744 if (CONSP (XCAR (map))
20745 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20746 return XCAR (map);
20747 map = XCDR (map);
20750 return Qnil;
20753 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20754 3, 3, 0,
20755 doc: /* Lookup in image map MAP coordinates X and Y.
20756 An image map is an alist where each element has the format (AREA ID PLIST).
20757 An AREA is specified as either a rectangle, a circle, or a polygon:
20758 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20759 pixel coordinates of the upper left and bottom right corners.
20760 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20761 and the radius of the circle; r may be a float or integer.
20762 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20763 vector describes one corner in the polygon.
20764 Returns the alist element for the first matching AREA in MAP. */)
20765 (map, x, y)
20766 Lisp_Object map;
20767 Lisp_Object x, y;
20769 if (NILP (map))
20770 return Qnil;
20772 CHECK_NUMBER (x);
20773 CHECK_NUMBER (y);
20775 return find_hot_spot (map, XINT (x), XINT (y));
20779 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20780 static void
20781 define_frame_cursor1 (f, cursor, pointer)
20782 struct frame *f;
20783 Cursor cursor;
20784 Lisp_Object pointer;
20786 /* Do not change cursor shape while dragging mouse. */
20787 if (!NILP (do_mouse_tracking))
20788 return;
20790 if (!NILP (pointer))
20792 if (EQ (pointer, Qarrow))
20793 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20794 else if (EQ (pointer, Qhand))
20795 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20796 else if (EQ (pointer, Qtext))
20797 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20798 else if (EQ (pointer, intern ("hdrag")))
20799 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20800 #ifdef HAVE_X_WINDOWS
20801 else if (EQ (pointer, intern ("vdrag")))
20802 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20803 #endif
20804 else if (EQ (pointer, intern ("hourglass")))
20805 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20806 else if (EQ (pointer, Qmodeline))
20807 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20808 else
20809 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20812 if (cursor != No_Cursor)
20813 rif->define_frame_cursor (f, cursor);
20816 /* Take proper action when mouse has moved to the mode or header line
20817 or marginal area AREA of window W, x-position X and y-position Y.
20818 X is relative to the start of the text display area of W, so the
20819 width of bitmap areas and scroll bars must be subtracted to get a
20820 position relative to the start of the mode line. */
20822 static void
20823 note_mode_line_or_margin_highlight (w, x, y, area)
20824 struct window *w;
20825 int x, y;
20826 enum window_part area;
20828 struct frame *f = XFRAME (w->frame);
20829 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20830 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20831 Lisp_Object pointer = Qnil;
20832 int charpos, dx, dy, width, height;
20833 Lisp_Object string, object = Qnil;
20834 Lisp_Object pos, help;
20836 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20837 string = mode_line_string (w, area, &x, &y, &charpos,
20838 &object, &dx, &dy, &width, &height);
20839 else
20841 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20842 string = marginal_area_string (w, area, &x, &y, &charpos,
20843 &object, &dx, &dy, &width, &height);
20846 help = Qnil;
20848 if (IMAGEP (object))
20850 Lisp_Object image_map, hotspot;
20851 if ((image_map = Fplist_get (XCDR (object), QCmap),
20852 !NILP (image_map))
20853 && (hotspot = find_hot_spot (image_map, dx, dy),
20854 CONSP (hotspot))
20855 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20857 Lisp_Object area_id, plist;
20859 area_id = XCAR (hotspot);
20860 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20861 If so, we could look for mouse-enter, mouse-leave
20862 properties in PLIST (and do something...). */
20863 if ((plist = XCDR (hotspot), CONSP (plist)))
20865 pointer = Fplist_get (plist, Qpointer);
20866 if (NILP (pointer))
20867 pointer = Qhand;
20868 help = Fplist_get (plist, Qhelp_echo);
20869 if (!NILP (help))
20871 help_echo_string = help;
20872 /* Is this correct? ++kfs */
20873 XSETWINDOW (help_echo_window, w);
20874 help_echo_object = w->buffer;
20875 help_echo_pos = charpos;
20878 if (NILP (pointer))
20879 pointer = Fplist_get (XCDR (object), QCpointer);
20883 if (STRINGP (string))
20885 pos = make_number (charpos);
20886 /* If we're on a string with `help-echo' text property, arrange
20887 for the help to be displayed. This is done by setting the
20888 global variable help_echo_string to the help string. */
20889 help = Fget_text_property (pos, Qhelp_echo, string);
20890 if (!NILP (help))
20892 help_echo_string = help;
20893 XSETWINDOW (help_echo_window, w);
20894 help_echo_object = string;
20895 help_echo_pos = charpos;
20898 if (NILP (pointer))
20899 pointer = Fget_text_property (pos, Qpointer, string);
20901 /* Change the mouse pointer according to what is under X/Y. */
20902 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20904 Lisp_Object map;
20905 map = Fget_text_property (pos, Qlocal_map, string);
20906 if (!KEYMAPP (map))
20907 map = Fget_text_property (pos, Qkeymap, string);
20908 if (!KEYMAPP (map))
20909 cursor = dpyinfo->vertical_scroll_bar_cursor;
20913 define_frame_cursor1 (f, cursor, pointer);
20917 /* EXPORT:
20918 Take proper action when the mouse has moved to position X, Y on
20919 frame F as regards highlighting characters that have mouse-face
20920 properties. Also de-highlighting chars where the mouse was before.
20921 X and Y can be negative or out of range. */
20923 void
20924 note_mouse_highlight (f, x, y)
20925 struct frame *f;
20926 int x, y;
20928 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20929 enum window_part part;
20930 Lisp_Object window;
20931 struct window *w;
20932 Cursor cursor = No_Cursor;
20933 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20934 struct buffer *b;
20936 /* When a menu is active, don't highlight because this looks odd. */
20937 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20938 if (popup_activated ())
20939 return;
20940 #endif
20942 if (NILP (Vmouse_highlight)
20943 || !f->glyphs_initialized_p)
20944 return;
20946 dpyinfo->mouse_face_mouse_x = x;
20947 dpyinfo->mouse_face_mouse_y = y;
20948 dpyinfo->mouse_face_mouse_frame = f;
20950 if (dpyinfo->mouse_face_defer)
20951 return;
20953 if (gc_in_progress)
20955 dpyinfo->mouse_face_deferred_gc = 1;
20956 return;
20959 /* Which window is that in? */
20960 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20962 /* If we were displaying active text in another window, clear that. */
20963 if (! EQ (window, dpyinfo->mouse_face_window))
20964 clear_mouse_face (dpyinfo);
20966 /* Not on a window -> return. */
20967 if (!WINDOWP (window))
20968 return;
20970 /* Reset help_echo_string. It will get recomputed below. */
20971 help_echo_string = Qnil;
20973 /* Convert to window-relative pixel coordinates. */
20974 w = XWINDOW (window);
20975 frame_to_window_pixel_xy (w, &x, &y);
20977 /* Handle tool-bar window differently since it doesn't display a
20978 buffer. */
20979 if (EQ (window, f->tool_bar_window))
20981 note_tool_bar_highlight (f, x, y);
20982 return;
20985 /* Mouse is on the mode, header line or margin? */
20986 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20987 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20989 note_mode_line_or_margin_highlight (w, x, y, part);
20990 return;
20993 if (part == ON_VERTICAL_BORDER)
20994 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20995 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
20996 || part == ON_SCROLL_BAR)
20997 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20998 else
20999 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21001 /* Are we in a window whose display is up to date?
21002 And verify the buffer's text has not changed. */
21003 b = XBUFFER (w->buffer);
21004 if (part == ON_TEXT
21005 && EQ (w->window_end_valid, w->buffer)
21006 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21007 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21009 int hpos, vpos, pos, i, dx, dy, area;
21010 struct glyph *glyph;
21011 Lisp_Object object;
21012 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21013 Lisp_Object *overlay_vec = NULL;
21014 int noverlays;
21015 struct buffer *obuf;
21016 int obegv, ozv, same_region;
21018 /* Find the glyph under X/Y. */
21019 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21021 /* Look for :pointer property on image. */
21022 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21024 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21025 if (img != NULL && IMAGEP (img->spec))
21027 Lisp_Object image_map, hotspot;
21028 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21029 !NILP (image_map))
21030 && (hotspot = find_hot_spot (image_map,
21031 glyph->slice.x + dx,
21032 glyph->slice.y + dy),
21033 CONSP (hotspot))
21034 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21036 Lisp_Object area_id, plist;
21038 area_id = XCAR (hotspot);
21039 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21040 If so, we could look for mouse-enter, mouse-leave
21041 properties in PLIST (and do something...). */
21042 if ((plist = XCDR (hotspot), CONSP (plist)))
21044 pointer = Fplist_get (plist, Qpointer);
21045 if (NILP (pointer))
21046 pointer = Qhand;
21047 help_echo_string = Fplist_get (plist, Qhelp_echo);
21048 if (!NILP (help_echo_string))
21050 help_echo_window = window;
21051 help_echo_object = glyph->object;
21052 help_echo_pos = glyph->charpos;
21056 if (NILP (pointer))
21057 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21061 /* Clear mouse face if X/Y not over text. */
21062 if (glyph == NULL
21063 || area != TEXT_AREA
21064 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21066 if (clear_mouse_face (dpyinfo))
21067 cursor = No_Cursor;
21068 if (NILP (pointer))
21070 if (area != TEXT_AREA)
21071 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21072 else
21073 pointer = Vvoid_text_area_pointer;
21075 goto set_cursor;
21078 pos = glyph->charpos;
21079 object = glyph->object;
21080 if (!STRINGP (object) && !BUFFERP (object))
21081 goto set_cursor;
21083 /* If we get an out-of-range value, return now; avoid an error. */
21084 if (BUFFERP (object) && pos > BUF_Z (b))
21085 goto set_cursor;
21087 /* Make the window's buffer temporarily current for
21088 overlays_at and compute_char_face. */
21089 obuf = current_buffer;
21090 current_buffer = b;
21091 obegv = BEGV;
21092 ozv = ZV;
21093 BEGV = BEG;
21094 ZV = Z;
21096 /* Is this char mouse-active or does it have help-echo? */
21097 position = make_number (pos);
21099 if (BUFFERP (object))
21101 /* Put all the overlays we want in a vector in overlay_vec. */
21102 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21103 /* Sort overlays into increasing priority order. */
21104 noverlays = sort_overlays (overlay_vec, noverlays, w);
21106 else
21107 noverlays = 0;
21109 same_region = (EQ (window, dpyinfo->mouse_face_window)
21110 && vpos >= dpyinfo->mouse_face_beg_row
21111 && vpos <= dpyinfo->mouse_face_end_row
21112 && (vpos > dpyinfo->mouse_face_beg_row
21113 || hpos >= dpyinfo->mouse_face_beg_col)
21114 && (vpos < dpyinfo->mouse_face_end_row
21115 || hpos < dpyinfo->mouse_face_end_col
21116 || dpyinfo->mouse_face_past_end));
21118 if (same_region)
21119 cursor = No_Cursor;
21121 /* Check mouse-face highlighting. */
21122 if (! same_region
21123 /* If there exists an overlay with mouse-face overlapping
21124 the one we are currently highlighting, we have to
21125 check if we enter the overlapping overlay, and then
21126 highlight only that. */
21127 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21128 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21130 /* Find the highest priority overlay that has a mouse-face
21131 property. */
21132 overlay = Qnil;
21133 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21135 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21136 if (!NILP (mouse_face))
21137 overlay = overlay_vec[i];
21140 /* If we're actually highlighting the same overlay as
21141 before, there's no need to do that again. */
21142 if (!NILP (overlay)
21143 && EQ (overlay, dpyinfo->mouse_face_overlay))
21144 goto check_help_echo;
21146 dpyinfo->mouse_face_overlay = overlay;
21148 /* Clear the display of the old active region, if any. */
21149 if (clear_mouse_face (dpyinfo))
21150 cursor = No_Cursor;
21152 /* If no overlay applies, get a text property. */
21153 if (NILP (overlay))
21154 mouse_face = Fget_text_property (position, Qmouse_face, object);
21156 /* Handle the overlay case. */
21157 if (!NILP (overlay))
21159 /* Find the range of text around this char that
21160 should be active. */
21161 Lisp_Object before, after;
21162 int ignore;
21164 before = Foverlay_start (overlay);
21165 after = Foverlay_end (overlay);
21166 /* Record this as the current active region. */
21167 fast_find_position (w, XFASTINT (before),
21168 &dpyinfo->mouse_face_beg_col,
21169 &dpyinfo->mouse_face_beg_row,
21170 &dpyinfo->mouse_face_beg_x,
21171 &dpyinfo->mouse_face_beg_y, Qnil);
21173 dpyinfo->mouse_face_past_end
21174 = !fast_find_position (w, XFASTINT (after),
21175 &dpyinfo->mouse_face_end_col,
21176 &dpyinfo->mouse_face_end_row,
21177 &dpyinfo->mouse_face_end_x,
21178 &dpyinfo->mouse_face_end_y, Qnil);
21179 dpyinfo->mouse_face_window = window;
21181 dpyinfo->mouse_face_face_id
21182 = face_at_buffer_position (w, pos, 0, 0,
21183 &ignore, pos + 1,
21184 !dpyinfo->mouse_face_hidden);
21186 /* Display it as active. */
21187 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21188 cursor = No_Cursor;
21190 /* Handle the text property case. */
21191 else if (!NILP (mouse_face) && BUFFERP (object))
21193 /* Find the range of text around this char that
21194 should be active. */
21195 Lisp_Object before, after, beginning, end;
21196 int ignore;
21198 beginning = Fmarker_position (w->start);
21199 end = make_number (BUF_Z (XBUFFER (object))
21200 - XFASTINT (w->window_end_pos));
21201 before
21202 = Fprevious_single_property_change (make_number (pos + 1),
21203 Qmouse_face,
21204 object, beginning);
21205 after
21206 = Fnext_single_property_change (position, Qmouse_face,
21207 object, end);
21209 /* Record this as the current active region. */
21210 fast_find_position (w, XFASTINT (before),
21211 &dpyinfo->mouse_face_beg_col,
21212 &dpyinfo->mouse_face_beg_row,
21213 &dpyinfo->mouse_face_beg_x,
21214 &dpyinfo->mouse_face_beg_y, Qnil);
21215 dpyinfo->mouse_face_past_end
21216 = !fast_find_position (w, XFASTINT (after),
21217 &dpyinfo->mouse_face_end_col,
21218 &dpyinfo->mouse_face_end_row,
21219 &dpyinfo->mouse_face_end_x,
21220 &dpyinfo->mouse_face_end_y, Qnil);
21221 dpyinfo->mouse_face_window = window;
21223 if (BUFFERP (object))
21224 dpyinfo->mouse_face_face_id
21225 = face_at_buffer_position (w, pos, 0, 0,
21226 &ignore, pos + 1,
21227 !dpyinfo->mouse_face_hidden);
21229 /* Display it as active. */
21230 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21231 cursor = No_Cursor;
21233 else if (!NILP (mouse_face) && STRINGP (object))
21235 Lisp_Object b, e;
21236 int ignore;
21238 b = Fprevious_single_property_change (make_number (pos + 1),
21239 Qmouse_face,
21240 object, Qnil);
21241 e = Fnext_single_property_change (position, Qmouse_face,
21242 object, Qnil);
21243 if (NILP (b))
21244 b = make_number (0);
21245 if (NILP (e))
21246 e = make_number (SCHARS (object) - 1);
21247 fast_find_string_pos (w, XINT (b), object,
21248 &dpyinfo->mouse_face_beg_col,
21249 &dpyinfo->mouse_face_beg_row,
21250 &dpyinfo->mouse_face_beg_x,
21251 &dpyinfo->mouse_face_beg_y, 0);
21252 fast_find_string_pos (w, XINT (e), object,
21253 &dpyinfo->mouse_face_end_col,
21254 &dpyinfo->mouse_face_end_row,
21255 &dpyinfo->mouse_face_end_x,
21256 &dpyinfo->mouse_face_end_y, 1);
21257 dpyinfo->mouse_face_past_end = 0;
21258 dpyinfo->mouse_face_window = window;
21259 dpyinfo->mouse_face_face_id
21260 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21261 glyph->face_id, 1);
21262 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21263 cursor = No_Cursor;
21265 else if (STRINGP (object) && NILP (mouse_face))
21267 /* A string which doesn't have mouse-face, but
21268 the text ``under'' it might have. */
21269 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21270 int start = MATRIX_ROW_START_CHARPOS (r);
21272 pos = string_buffer_position (w, object, start);
21273 if (pos > 0)
21274 mouse_face = get_char_property_and_overlay (make_number (pos),
21275 Qmouse_face,
21276 w->buffer,
21277 &overlay);
21278 if (!NILP (mouse_face) && !NILP (overlay))
21280 Lisp_Object before = Foverlay_start (overlay);
21281 Lisp_Object after = Foverlay_end (overlay);
21282 int ignore;
21284 /* Note that we might not be able to find position
21285 BEFORE in the glyph matrix if the overlay is
21286 entirely covered by a `display' property. In
21287 this case, we overshoot. So let's stop in
21288 the glyph matrix before glyphs for OBJECT. */
21289 fast_find_position (w, XFASTINT (before),
21290 &dpyinfo->mouse_face_beg_col,
21291 &dpyinfo->mouse_face_beg_row,
21292 &dpyinfo->mouse_face_beg_x,
21293 &dpyinfo->mouse_face_beg_y,
21294 object);
21296 dpyinfo->mouse_face_past_end
21297 = !fast_find_position (w, XFASTINT (after),
21298 &dpyinfo->mouse_face_end_col,
21299 &dpyinfo->mouse_face_end_row,
21300 &dpyinfo->mouse_face_end_x,
21301 &dpyinfo->mouse_face_end_y,
21302 Qnil);
21303 dpyinfo->mouse_face_window = window;
21304 dpyinfo->mouse_face_face_id
21305 = face_at_buffer_position (w, pos, 0, 0,
21306 &ignore, pos + 1,
21307 !dpyinfo->mouse_face_hidden);
21309 /* Display it as active. */
21310 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21311 cursor = No_Cursor;
21316 check_help_echo:
21318 /* Look for a `help-echo' property. */
21319 if (NILP (help_echo_string)) {
21320 Lisp_Object help, overlay;
21322 /* Check overlays first. */
21323 help = overlay = Qnil;
21324 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21326 overlay = overlay_vec[i];
21327 help = Foverlay_get (overlay, Qhelp_echo);
21330 if (!NILP (help))
21332 help_echo_string = help;
21333 help_echo_window = window;
21334 help_echo_object = overlay;
21335 help_echo_pos = pos;
21337 else
21339 Lisp_Object object = glyph->object;
21340 int charpos = glyph->charpos;
21342 /* Try text properties. */
21343 if (STRINGP (object)
21344 && charpos >= 0
21345 && charpos < SCHARS (object))
21347 help = Fget_text_property (make_number (charpos),
21348 Qhelp_echo, object);
21349 if (NILP (help))
21351 /* If the string itself doesn't specify a help-echo,
21352 see if the buffer text ``under'' it does. */
21353 struct glyph_row *r
21354 = MATRIX_ROW (w->current_matrix, vpos);
21355 int start = MATRIX_ROW_START_CHARPOS (r);
21356 int pos = string_buffer_position (w, object, start);
21357 if (pos > 0)
21359 help = Fget_char_property (make_number (pos),
21360 Qhelp_echo, w->buffer);
21361 if (!NILP (help))
21363 charpos = pos;
21364 object = w->buffer;
21369 else if (BUFFERP (object)
21370 && charpos >= BEGV
21371 && charpos < ZV)
21372 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21373 object);
21375 if (!NILP (help))
21377 help_echo_string = help;
21378 help_echo_window = window;
21379 help_echo_object = object;
21380 help_echo_pos = charpos;
21385 /* Look for a `pointer' property. */
21386 if (NILP (pointer))
21388 /* Check overlays first. */
21389 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21390 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21392 if (NILP (pointer))
21394 Lisp_Object object = glyph->object;
21395 int charpos = glyph->charpos;
21397 /* Try text properties. */
21398 if (STRINGP (object)
21399 && charpos >= 0
21400 && charpos < SCHARS (object))
21402 pointer = Fget_text_property (make_number (charpos),
21403 Qpointer, object);
21404 if (NILP (pointer))
21406 /* If the string itself doesn't specify a pointer,
21407 see if the buffer text ``under'' it does. */
21408 struct glyph_row *r
21409 = MATRIX_ROW (w->current_matrix, vpos);
21410 int start = MATRIX_ROW_START_CHARPOS (r);
21411 int pos = string_buffer_position (w, object, start);
21412 if (pos > 0)
21413 pointer = Fget_char_property (make_number (pos),
21414 Qpointer, w->buffer);
21417 else if (BUFFERP (object)
21418 && charpos >= BEGV
21419 && charpos < ZV)
21420 pointer = Fget_text_property (make_number (charpos),
21421 Qpointer, object);
21425 BEGV = obegv;
21426 ZV = ozv;
21427 current_buffer = obuf;
21430 set_cursor:
21432 define_frame_cursor1 (f, cursor, pointer);
21436 /* EXPORT for RIF:
21437 Clear any mouse-face on window W. This function is part of the
21438 redisplay interface, and is called from try_window_id and similar
21439 functions to ensure the mouse-highlight is off. */
21441 void
21442 x_clear_window_mouse_face (w)
21443 struct window *w;
21445 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21446 Lisp_Object window;
21448 BLOCK_INPUT;
21449 XSETWINDOW (window, w);
21450 if (EQ (window, dpyinfo->mouse_face_window))
21451 clear_mouse_face (dpyinfo);
21452 UNBLOCK_INPUT;
21456 /* EXPORT:
21457 Just discard the mouse face information for frame F, if any.
21458 This is used when the size of F is changed. */
21460 void
21461 cancel_mouse_face (f)
21462 struct frame *f;
21464 Lisp_Object window;
21465 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21467 window = dpyinfo->mouse_face_window;
21468 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21470 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21471 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21472 dpyinfo->mouse_face_window = Qnil;
21477 #endif /* HAVE_WINDOW_SYSTEM */
21480 /***********************************************************************
21481 Exposure Events
21482 ***********************************************************************/
21484 #ifdef HAVE_WINDOW_SYSTEM
21486 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21487 which intersects rectangle R. R is in window-relative coordinates. */
21489 static void
21490 expose_area (w, row, r, area)
21491 struct window *w;
21492 struct glyph_row *row;
21493 XRectangle *r;
21494 enum glyph_row_area area;
21496 struct glyph *first = row->glyphs[area];
21497 struct glyph *end = row->glyphs[area] + row->used[area];
21498 struct glyph *last;
21499 int first_x, start_x, x;
21501 if (area == TEXT_AREA && row->fill_line_p)
21502 /* If row extends face to end of line write the whole line. */
21503 draw_glyphs (w, 0, row, area,
21504 0, row->used[area],
21505 DRAW_NORMAL_TEXT, 0);
21506 else
21508 /* Set START_X to the window-relative start position for drawing glyphs of
21509 AREA. The first glyph of the text area can be partially visible.
21510 The first glyphs of other areas cannot. */
21511 start_x = window_box_left_offset (w, area);
21512 x = start_x;
21513 if (area == TEXT_AREA)
21514 x += row->x;
21516 /* Find the first glyph that must be redrawn. */
21517 while (first < end
21518 && x + first->pixel_width < r->x)
21520 x += first->pixel_width;
21521 ++first;
21524 /* Find the last one. */
21525 last = first;
21526 first_x = x;
21527 while (last < end
21528 && x < r->x + r->width)
21530 x += last->pixel_width;
21531 ++last;
21534 /* Repaint. */
21535 if (last > first)
21536 draw_glyphs (w, first_x - start_x, row, area,
21537 first - row->glyphs[area], last - row->glyphs[area],
21538 DRAW_NORMAL_TEXT, 0);
21543 /* Redraw the parts of the glyph row ROW on window W intersecting
21544 rectangle R. R is in window-relative coordinates. Value is
21545 non-zero if mouse-face was overwritten. */
21547 static int
21548 expose_line (w, row, r)
21549 struct window *w;
21550 struct glyph_row *row;
21551 XRectangle *r;
21553 xassert (row->enabled_p);
21555 if (row->mode_line_p || w->pseudo_window_p)
21556 draw_glyphs (w, 0, row, TEXT_AREA,
21557 0, row->used[TEXT_AREA],
21558 DRAW_NORMAL_TEXT, 0);
21559 else
21561 if (row->used[LEFT_MARGIN_AREA])
21562 expose_area (w, row, r, LEFT_MARGIN_AREA);
21563 if (row->used[TEXT_AREA])
21564 expose_area (w, row, r, TEXT_AREA);
21565 if (row->used[RIGHT_MARGIN_AREA])
21566 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21567 draw_row_fringe_bitmaps (w, row);
21570 return row->mouse_face_p;
21574 /* Redraw those parts of glyphs rows during expose event handling that
21575 overlap other rows. Redrawing of an exposed line writes over parts
21576 of lines overlapping that exposed line; this function fixes that.
21578 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21579 row in W's current matrix that is exposed and overlaps other rows.
21580 LAST_OVERLAPPING_ROW is the last such row. */
21582 static void
21583 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21584 struct window *w;
21585 struct glyph_row *first_overlapping_row;
21586 struct glyph_row *last_overlapping_row;
21588 struct glyph_row *row;
21590 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21591 if (row->overlapping_p)
21593 xassert (row->enabled_p && !row->mode_line_p);
21595 if (row->used[LEFT_MARGIN_AREA])
21596 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21598 if (row->used[TEXT_AREA])
21599 x_fix_overlapping_area (w, row, TEXT_AREA);
21601 if (row->used[RIGHT_MARGIN_AREA])
21602 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21607 /* Return non-zero if W's cursor intersects rectangle R. */
21609 static int
21610 phys_cursor_in_rect_p (w, r)
21611 struct window *w;
21612 XRectangle *r;
21614 XRectangle cr, result;
21615 struct glyph *cursor_glyph;
21617 cursor_glyph = get_phys_cursor_glyph (w);
21618 if (cursor_glyph)
21620 /* r is relative to W's box, but w->phys_cursor.x is relative
21621 to left edge of W's TEXT area. Adjust it. */
21622 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21623 cr.y = w->phys_cursor.y;
21624 cr.width = cursor_glyph->pixel_width;
21625 cr.height = w->phys_cursor_height;
21626 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21627 I assume the effect is the same -- and this is portable. */
21628 return x_intersect_rectangles (&cr, r, &result);
21630 else
21631 return 0;
21635 /* EXPORT:
21636 Draw a vertical window border to the right of window W if W doesn't
21637 have vertical scroll bars. */
21639 void
21640 x_draw_vertical_border (w)
21641 struct window *w;
21643 /* We could do better, if we knew what type of scroll-bar the adjacent
21644 windows (on either side) have... But we don't :-(
21645 However, I think this works ok. ++KFS 2003-04-25 */
21647 /* Redraw borders between horizontally adjacent windows. Don't
21648 do it for frames with vertical scroll bars because either the
21649 right scroll bar of a window, or the left scroll bar of its
21650 neighbor will suffice as a border. */
21651 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21652 return;
21654 if (!WINDOW_RIGHTMOST_P (w)
21655 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21657 int x0, x1, y0, y1;
21659 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21660 y1 -= 1;
21662 rif->draw_vertical_window_border (w, x1, y0, y1);
21664 else if (!WINDOW_LEFTMOST_P (w)
21665 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21667 int x0, x1, y0, y1;
21669 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21670 y1 -= 1;
21672 rif->draw_vertical_window_border (w, x0, y0, y1);
21677 /* Redraw the part of window W intersection rectangle FR. Pixel
21678 coordinates in FR are frame-relative. Call this function with
21679 input blocked. Value is non-zero if the exposure overwrites
21680 mouse-face. */
21682 static int
21683 expose_window (w, fr)
21684 struct window *w;
21685 XRectangle *fr;
21687 struct frame *f = XFRAME (w->frame);
21688 XRectangle wr, r;
21689 int mouse_face_overwritten_p = 0;
21691 /* If window is not yet fully initialized, do nothing. This can
21692 happen when toolkit scroll bars are used and a window is split.
21693 Reconfiguring the scroll bar will generate an expose for a newly
21694 created window. */
21695 if (w->current_matrix == NULL)
21696 return 0;
21698 /* When we're currently updating the window, display and current
21699 matrix usually don't agree. Arrange for a thorough display
21700 later. */
21701 if (w == updated_window)
21703 SET_FRAME_GARBAGED (f);
21704 return 0;
21707 /* Frame-relative pixel rectangle of W. */
21708 wr.x = WINDOW_LEFT_EDGE_X (w);
21709 wr.y = WINDOW_TOP_EDGE_Y (w);
21710 wr.width = WINDOW_TOTAL_WIDTH (w);
21711 wr.height = WINDOW_TOTAL_HEIGHT (w);
21713 if (x_intersect_rectangles (fr, &wr, &r))
21715 int yb = window_text_bottom_y (w);
21716 struct glyph_row *row;
21717 int cursor_cleared_p;
21718 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21720 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21721 r.x, r.y, r.width, r.height));
21723 /* Convert to window coordinates. */
21724 r.x -= WINDOW_LEFT_EDGE_X (w);
21725 r.y -= WINDOW_TOP_EDGE_Y (w);
21727 /* Turn off the cursor. */
21728 if (!w->pseudo_window_p
21729 && phys_cursor_in_rect_p (w, &r))
21731 x_clear_cursor (w);
21732 cursor_cleared_p = 1;
21734 else
21735 cursor_cleared_p = 0;
21737 /* Update lines intersecting rectangle R. */
21738 first_overlapping_row = last_overlapping_row = NULL;
21739 for (row = w->current_matrix->rows;
21740 row->enabled_p;
21741 ++row)
21743 int y0 = row->y;
21744 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21746 if ((y0 >= r.y && y0 < r.y + r.height)
21747 || (y1 > r.y && y1 < r.y + r.height)
21748 || (r.y >= y0 && r.y < y1)
21749 || (r.y + r.height > y0 && r.y + r.height < y1))
21751 if (row->overlapping_p)
21753 if (first_overlapping_row == NULL)
21754 first_overlapping_row = row;
21755 last_overlapping_row = row;
21758 if (expose_line (w, row, &r))
21759 mouse_face_overwritten_p = 1;
21762 if (y1 >= yb)
21763 break;
21766 /* Display the mode line if there is one. */
21767 if (WINDOW_WANTS_MODELINE_P (w)
21768 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21769 row->enabled_p)
21770 && row->y < r.y + r.height)
21772 if (expose_line (w, row, &r))
21773 mouse_face_overwritten_p = 1;
21776 if (!w->pseudo_window_p)
21778 /* Fix the display of overlapping rows. */
21779 if (first_overlapping_row)
21780 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21782 /* Draw border between windows. */
21783 x_draw_vertical_border (w);
21785 /* Turn the cursor on again. */
21786 if (cursor_cleared_p)
21787 update_window_cursor (w, 1);
21791 #ifdef HAVE_CARBON
21792 /* Display scroll bar for this window. */
21793 if (!NILP (w->vertical_scroll_bar))
21795 /* ++KFS:
21796 If this doesn't work here (maybe some header files are missing),
21797 make a function in macterm.c and call it to do the job! */
21798 ControlHandle ch
21799 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21801 Draw1Control (ch);
21803 #endif
21805 return mouse_face_overwritten_p;
21810 /* Redraw (parts) of all windows in the window tree rooted at W that
21811 intersect R. R contains frame pixel coordinates. Value is
21812 non-zero if the exposure overwrites mouse-face. */
21814 static int
21815 expose_window_tree (w, r)
21816 struct window *w;
21817 XRectangle *r;
21819 struct frame *f = XFRAME (w->frame);
21820 int mouse_face_overwritten_p = 0;
21822 while (w && !FRAME_GARBAGED_P (f))
21824 if (!NILP (w->hchild))
21825 mouse_face_overwritten_p
21826 |= expose_window_tree (XWINDOW (w->hchild), r);
21827 else if (!NILP (w->vchild))
21828 mouse_face_overwritten_p
21829 |= expose_window_tree (XWINDOW (w->vchild), r);
21830 else
21831 mouse_face_overwritten_p |= expose_window (w, r);
21833 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21836 return mouse_face_overwritten_p;
21840 /* EXPORT:
21841 Redisplay an exposed area of frame F. X and Y are the upper-left
21842 corner of the exposed rectangle. W and H are width and height of
21843 the exposed area. All are pixel values. W or H zero means redraw
21844 the entire frame. */
21846 void
21847 expose_frame (f, x, y, w, h)
21848 struct frame *f;
21849 int x, y, w, h;
21851 XRectangle r;
21852 int mouse_face_overwritten_p = 0;
21854 TRACE ((stderr, "expose_frame "));
21856 /* No need to redraw if frame will be redrawn soon. */
21857 if (FRAME_GARBAGED_P (f))
21859 TRACE ((stderr, " garbaged\n"));
21860 return;
21863 #ifdef HAVE_CARBON
21864 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21865 or deactivated here, for unknown reasons, activated scroll bars
21866 are shown in deactivated frames in some instances. */
21867 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21868 activate_scroll_bars (f);
21869 else
21870 deactivate_scroll_bars (f);
21871 #endif
21873 /* If basic faces haven't been realized yet, there is no point in
21874 trying to redraw anything. This can happen when we get an expose
21875 event while Emacs is starting, e.g. by moving another window. */
21876 if (FRAME_FACE_CACHE (f) == NULL
21877 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21879 TRACE ((stderr, " no faces\n"));
21880 return;
21883 if (w == 0 || h == 0)
21885 r.x = r.y = 0;
21886 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21887 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21889 else
21891 r.x = x;
21892 r.y = y;
21893 r.width = w;
21894 r.height = h;
21897 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21898 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21900 if (WINDOWP (f->tool_bar_window))
21901 mouse_face_overwritten_p
21902 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21904 #ifdef HAVE_X_WINDOWS
21905 #ifndef MSDOS
21906 #ifndef USE_X_TOOLKIT
21907 if (WINDOWP (f->menu_bar_window))
21908 mouse_face_overwritten_p
21909 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21910 #endif /* not USE_X_TOOLKIT */
21911 #endif
21912 #endif
21914 /* Some window managers support a focus-follows-mouse style with
21915 delayed raising of frames. Imagine a partially obscured frame,
21916 and moving the mouse into partially obscured mouse-face on that
21917 frame. The visible part of the mouse-face will be highlighted,
21918 then the WM raises the obscured frame. With at least one WM, KDE
21919 2.1, Emacs is not getting any event for the raising of the frame
21920 (even tried with SubstructureRedirectMask), only Expose events.
21921 These expose events will draw text normally, i.e. not
21922 highlighted. Which means we must redo the highlight here.
21923 Subsume it under ``we love X''. --gerd 2001-08-15 */
21924 /* Included in Windows version because Windows most likely does not
21925 do the right thing if any third party tool offers
21926 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21927 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21929 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21930 if (f == dpyinfo->mouse_face_mouse_frame)
21932 int x = dpyinfo->mouse_face_mouse_x;
21933 int y = dpyinfo->mouse_face_mouse_y;
21934 clear_mouse_face (dpyinfo);
21935 note_mouse_highlight (f, x, y);
21941 /* EXPORT:
21942 Determine the intersection of two rectangles R1 and R2. Return
21943 the intersection in *RESULT. Value is non-zero if RESULT is not
21944 empty. */
21947 x_intersect_rectangles (r1, r2, result)
21948 XRectangle *r1, *r2, *result;
21950 XRectangle *left, *right;
21951 XRectangle *upper, *lower;
21952 int intersection_p = 0;
21954 /* Rearrange so that R1 is the left-most rectangle. */
21955 if (r1->x < r2->x)
21956 left = r1, right = r2;
21957 else
21958 left = r2, right = r1;
21960 /* X0 of the intersection is right.x0, if this is inside R1,
21961 otherwise there is no intersection. */
21962 if (right->x <= left->x + left->width)
21964 result->x = right->x;
21966 /* The right end of the intersection is the minimum of the
21967 the right ends of left and right. */
21968 result->width = (min (left->x + left->width, right->x + right->width)
21969 - result->x);
21971 /* Same game for Y. */
21972 if (r1->y < r2->y)
21973 upper = r1, lower = r2;
21974 else
21975 upper = r2, lower = r1;
21977 /* The upper end of the intersection is lower.y0, if this is inside
21978 of upper. Otherwise, there is no intersection. */
21979 if (lower->y <= upper->y + upper->height)
21981 result->y = lower->y;
21983 /* The lower end of the intersection is the minimum of the lower
21984 ends of upper and lower. */
21985 result->height = (min (lower->y + lower->height,
21986 upper->y + upper->height)
21987 - result->y);
21988 intersection_p = 1;
21992 return intersection_p;
21995 #endif /* HAVE_WINDOW_SYSTEM */
21998 /***********************************************************************
21999 Initialization
22000 ***********************************************************************/
22002 void
22003 syms_of_xdisp ()
22005 Vwith_echo_area_save_vector = Qnil;
22006 staticpro (&Vwith_echo_area_save_vector);
22008 Vmessage_stack = Qnil;
22009 staticpro (&Vmessage_stack);
22011 Qinhibit_redisplay = intern ("inhibit-redisplay");
22012 staticpro (&Qinhibit_redisplay);
22014 message_dolog_marker1 = Fmake_marker ();
22015 staticpro (&message_dolog_marker1);
22016 message_dolog_marker2 = Fmake_marker ();
22017 staticpro (&message_dolog_marker2);
22018 message_dolog_marker3 = Fmake_marker ();
22019 staticpro (&message_dolog_marker3);
22021 #if GLYPH_DEBUG
22022 defsubr (&Sdump_frame_glyph_matrix);
22023 defsubr (&Sdump_glyph_matrix);
22024 defsubr (&Sdump_glyph_row);
22025 defsubr (&Sdump_tool_bar_row);
22026 defsubr (&Strace_redisplay);
22027 defsubr (&Strace_to_stderr);
22028 #endif
22029 #ifdef HAVE_WINDOW_SYSTEM
22030 defsubr (&Stool_bar_lines_needed);
22031 defsubr (&Slookup_image_map);
22032 #endif
22033 defsubr (&Sformat_mode_line);
22035 staticpro (&Qmenu_bar_update_hook);
22036 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22038 staticpro (&Qoverriding_terminal_local_map);
22039 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22041 staticpro (&Qoverriding_local_map);
22042 Qoverriding_local_map = intern ("overriding-local-map");
22044 staticpro (&Qwindow_scroll_functions);
22045 Qwindow_scroll_functions = intern ("window-scroll-functions");
22047 staticpro (&Qredisplay_end_trigger_functions);
22048 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22050 staticpro (&Qinhibit_point_motion_hooks);
22051 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22053 QCdata = intern (":data");
22054 staticpro (&QCdata);
22055 Qdisplay = intern ("display");
22056 staticpro (&Qdisplay);
22057 Qspace_width = intern ("space-width");
22058 staticpro (&Qspace_width);
22059 Qraise = intern ("raise");
22060 staticpro (&Qraise);
22061 Qslice = intern ("slice");
22062 staticpro (&Qslice);
22063 Qspace = intern ("space");
22064 staticpro (&Qspace);
22065 Qmargin = intern ("margin");
22066 staticpro (&Qmargin);
22067 Qpointer = intern ("pointer");
22068 staticpro (&Qpointer);
22069 Qleft_margin = intern ("left-margin");
22070 staticpro (&Qleft_margin);
22071 Qright_margin = intern ("right-margin");
22072 staticpro (&Qright_margin);
22073 Qcenter = intern ("center");
22074 staticpro (&Qcenter);
22075 Qline_height = intern ("line-height");
22076 staticpro (&Qline_height);
22077 Qtotal = intern ("total");
22078 staticpro (&Qtotal);
22079 QCalign_to = intern (":align-to");
22080 staticpro (&QCalign_to);
22081 QCrelative_width = intern (":relative-width");
22082 staticpro (&QCrelative_width);
22083 QCrelative_height = intern (":relative-height");
22084 staticpro (&QCrelative_height);
22085 QCeval = intern (":eval");
22086 staticpro (&QCeval);
22087 QCpropertize = intern (":propertize");
22088 staticpro (&QCpropertize);
22089 QCfile = intern (":file");
22090 staticpro (&QCfile);
22091 Qfontified = intern ("fontified");
22092 staticpro (&Qfontified);
22093 Qfontification_functions = intern ("fontification-functions");
22094 staticpro (&Qfontification_functions);
22095 Qtrailing_whitespace = intern ("trailing-whitespace");
22096 staticpro (&Qtrailing_whitespace);
22097 Qimage = intern ("image");
22098 staticpro (&Qimage);
22099 QCmap = intern (":map");
22100 staticpro (&QCmap);
22101 QCpointer = intern (":pointer");
22102 staticpro (&QCpointer);
22103 Qrect = intern ("rect");
22104 staticpro (&Qrect);
22105 Qcircle = intern ("circle");
22106 staticpro (&Qcircle);
22107 Qpoly = intern ("poly");
22108 staticpro (&Qpoly);
22109 Qmessage_truncate_lines = intern ("message-truncate-lines");
22110 staticpro (&Qmessage_truncate_lines);
22111 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22112 staticpro (&Qcursor_in_non_selected_windows);
22113 Qgrow_only = intern ("grow-only");
22114 staticpro (&Qgrow_only);
22115 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22116 staticpro (&Qinhibit_menubar_update);
22117 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22118 staticpro (&Qinhibit_eval_during_redisplay);
22119 Qposition = intern ("position");
22120 staticpro (&Qposition);
22121 Qbuffer_position = intern ("buffer-position");
22122 staticpro (&Qbuffer_position);
22123 Qobject = intern ("object");
22124 staticpro (&Qobject);
22125 Qbar = intern ("bar");
22126 staticpro (&Qbar);
22127 Qhbar = intern ("hbar");
22128 staticpro (&Qhbar);
22129 Qbox = intern ("box");
22130 staticpro (&Qbox);
22131 Qhollow = intern ("hollow");
22132 staticpro (&Qhollow);
22133 Qhand = intern ("hand");
22134 staticpro (&Qhand);
22135 Qarrow = intern ("arrow");
22136 staticpro (&Qarrow);
22137 Qtext = intern ("text");
22138 staticpro (&Qtext);
22139 Qrisky_local_variable = intern ("risky-local-variable");
22140 staticpro (&Qrisky_local_variable);
22141 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22142 staticpro (&Qinhibit_free_realized_faces);
22144 list_of_error = Fcons (Fcons (intern ("error"),
22145 Fcons (intern ("void-variable"), Qnil)),
22146 Qnil);
22147 staticpro (&list_of_error);
22149 Qlast_arrow_position = intern ("last-arrow-position");
22150 staticpro (&Qlast_arrow_position);
22151 Qlast_arrow_string = intern ("last-arrow-string");
22152 staticpro (&Qlast_arrow_string);
22154 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22155 staticpro (&Qoverlay_arrow_string);
22156 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22157 staticpro (&Qoverlay_arrow_bitmap);
22159 echo_buffer[0] = echo_buffer[1] = Qnil;
22160 staticpro (&echo_buffer[0]);
22161 staticpro (&echo_buffer[1]);
22163 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22164 staticpro (&echo_area_buffer[0]);
22165 staticpro (&echo_area_buffer[1]);
22167 Vmessages_buffer_name = build_string ("*Messages*");
22168 staticpro (&Vmessages_buffer_name);
22170 mode_line_proptrans_alist = Qnil;
22171 staticpro (&mode_line_proptrans_alist);
22173 mode_line_string_list = Qnil;
22174 staticpro (&mode_line_string_list);
22176 help_echo_string = Qnil;
22177 staticpro (&help_echo_string);
22178 help_echo_object = Qnil;
22179 staticpro (&help_echo_object);
22180 help_echo_window = Qnil;
22181 staticpro (&help_echo_window);
22182 previous_help_echo_string = Qnil;
22183 staticpro (&previous_help_echo_string);
22184 help_echo_pos = -1;
22186 #ifdef HAVE_WINDOW_SYSTEM
22187 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22188 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22189 For example, if a block cursor is over a tab, it will be drawn as
22190 wide as that tab on the display. */);
22191 x_stretch_cursor_p = 0;
22192 #endif
22194 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22195 doc: /* *Non-nil means highlight trailing whitespace.
22196 The face used for trailing whitespace is `trailing-whitespace'. */);
22197 Vshow_trailing_whitespace = Qnil;
22199 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22200 doc: /* *The pointer shape to show in void text areas.
22201 Nil means to show the text pointer. Other options are `arrow', `text',
22202 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22203 Vvoid_text_area_pointer = Qarrow;
22205 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22206 doc: /* Non-nil means don't actually do any redisplay.
22207 This is used for internal purposes. */);
22208 Vinhibit_redisplay = Qnil;
22210 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22211 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22212 Vglobal_mode_string = Qnil;
22214 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22215 doc: /* Marker for where to display an arrow on top of the buffer text.
22216 This must be the beginning of a line in order to work.
22217 See also `overlay-arrow-string'. */);
22218 Voverlay_arrow_position = Qnil;
22220 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22221 doc: /* String to display as an arrow in non-window frames.
22222 See also `overlay-arrow-position'. */);
22223 Voverlay_arrow_string = Qnil;
22225 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22226 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22227 The symbols on this list are examined during redisplay to determine
22228 where to display overlay arrows. */);
22229 Voverlay_arrow_variable_list
22230 = Fcons (intern ("overlay-arrow-position"), Qnil);
22232 DEFVAR_INT ("scroll-step", &scroll_step,
22233 doc: /* *The number of lines to try scrolling a window by when point moves out.
22234 If that fails to bring point back on frame, point is centered instead.
22235 If this is zero, point is always centered after it moves off frame.
22236 If you want scrolling to always be a line at a time, you should set
22237 `scroll-conservatively' to a large value rather than set this to 1. */);
22239 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22240 doc: /* *Scroll up to this many lines, to bring point back on screen.
22241 A value of zero means to scroll the text to center point vertically
22242 in the window. */);
22243 scroll_conservatively = 0;
22245 DEFVAR_INT ("scroll-margin", &scroll_margin,
22246 doc: /* *Number of lines of margin at the top and bottom of a window.
22247 Recenter the window whenever point gets within this many lines
22248 of the top or bottom of the window. */);
22249 scroll_margin = 0;
22251 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22252 doc: /* Pixels per inch on current display.
22253 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22254 Vdisplay_pixels_per_inch = make_float (72.0);
22256 #if GLYPH_DEBUG
22257 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22258 #endif
22260 DEFVAR_BOOL ("truncate-partial-width-windows",
22261 &truncate_partial_width_windows,
22262 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22263 truncate_partial_width_windows = 1;
22265 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22266 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22267 Any other value means to use the appropriate face, `mode-line',
22268 `header-line', or `menu' respectively. */);
22269 mode_line_inverse_video = 1;
22271 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22272 doc: /* *Maximum buffer size for which line number should be displayed.
22273 If the buffer is bigger than this, the line number does not appear
22274 in the mode line. A value of nil means no limit. */);
22275 Vline_number_display_limit = Qnil;
22277 DEFVAR_INT ("line-number-display-limit-width",
22278 &line_number_display_limit_width,
22279 doc: /* *Maximum line width (in characters) for line number display.
22280 If the average length of the lines near point is bigger than this, then the
22281 line number may be omitted from the mode line. */);
22282 line_number_display_limit_width = 200;
22284 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22285 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22286 highlight_nonselected_windows = 0;
22288 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22289 doc: /* Non-nil if more than one frame is visible on this display.
22290 Minibuffer-only frames don't count, but iconified frames do.
22291 This variable is not guaranteed to be accurate except while processing
22292 `frame-title-format' and `icon-title-format'. */);
22294 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22295 doc: /* Template for displaying the title bar of visible frames.
22296 \(Assuming the window manager supports this feature.)
22297 This variable has the same structure as `mode-line-format' (which see),
22298 and is used only on frames for which no explicit name has been set
22299 \(see `modify-frame-parameters'). */);
22301 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22302 doc: /* Template for displaying the title bar of an iconified frame.
22303 \(Assuming the window manager supports this feature.)
22304 This variable has the same structure as `mode-line-format' (which see),
22305 and is used only on frames for which no explicit name has been set
22306 \(see `modify-frame-parameters'). */);
22307 Vicon_title_format
22308 = Vframe_title_format
22309 = Fcons (intern ("multiple-frames"),
22310 Fcons (build_string ("%b"),
22311 Fcons (Fcons (empty_string,
22312 Fcons (intern ("invocation-name"),
22313 Fcons (build_string ("@"),
22314 Fcons (intern ("system-name"),
22315 Qnil)))),
22316 Qnil)));
22318 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22319 doc: /* Maximum number of lines to keep in the message log buffer.
22320 If nil, disable message logging. If t, log messages but don't truncate
22321 the buffer when it becomes large. */);
22322 Vmessage_log_max = make_number (50);
22324 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22325 doc: /* Functions called before redisplay, if window sizes have changed.
22326 The value should be a list of functions that take one argument.
22327 Just before redisplay, for each frame, if any of its windows have changed
22328 size since the last redisplay, or have been split or deleted,
22329 all the functions in the list are called, with the frame as argument. */);
22330 Vwindow_size_change_functions = Qnil;
22332 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22333 doc: /* List of functions to call before redisplaying a window with scrolling.
22334 Each function is called with two arguments, the window
22335 and its new display-start position. Note that the value of `window-end'
22336 is not valid when these functions are called. */);
22337 Vwindow_scroll_functions = Qnil;
22339 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22340 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22341 mouse_autoselect_window = 0;
22343 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22344 doc: /* *Non-nil means automatically resize tool-bars.
22345 This increases a tool-bar's height if not all tool-bar items are visible.
22346 It decreases a tool-bar's height when it would display blank lines
22347 otherwise. */);
22348 auto_resize_tool_bars_p = 1;
22350 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22351 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22352 auto_raise_tool_bar_buttons_p = 1;
22354 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22355 doc: /* *Margin around tool-bar buttons in pixels.
22356 If an integer, use that for both horizontal and vertical margins.
22357 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22358 HORZ specifying the horizontal margin, and VERT specifying the
22359 vertical margin. */);
22360 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22362 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22363 doc: /* *Relief thickness of tool-bar buttons. */);
22364 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22366 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22367 doc: /* List of functions to call to fontify regions of text.
22368 Each function is called with one argument POS. Functions must
22369 fontify a region starting at POS in the current buffer, and give
22370 fontified regions the property `fontified'. */);
22371 Vfontification_functions = Qnil;
22372 Fmake_variable_buffer_local (Qfontification_functions);
22374 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22375 &unibyte_display_via_language_environment,
22376 doc: /* *Non-nil means display unibyte text according to language environment.
22377 Specifically this means that unibyte non-ASCII characters
22378 are displayed by converting them to the equivalent multibyte characters
22379 according to the current language environment. As a result, they are
22380 displayed according to the current fontset. */);
22381 unibyte_display_via_language_environment = 0;
22383 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22384 doc: /* *Maximum height for resizing mini-windows.
22385 If a float, it specifies a fraction of the mini-window frame's height.
22386 If an integer, it specifies a number of lines. */);
22387 Vmax_mini_window_height = make_float (0.25);
22389 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22390 doc: /* *How to resize mini-windows.
22391 A value of nil means don't automatically resize mini-windows.
22392 A value of t means resize them to fit the text displayed in them.
22393 A value of `grow-only', the default, means let mini-windows grow
22394 only, until their display becomes empty, at which point the windows
22395 go back to their normal size. */);
22396 Vresize_mini_windows = Qgrow_only;
22398 DEFVAR_LISP ("cursor-in-non-selected-windows",
22399 &Vcursor_in_non_selected_windows,
22400 doc: /* *Cursor type to display in non-selected windows.
22401 t means to use hollow box cursor. See `cursor-type' for other values. */);
22402 Vcursor_in_non_selected_windows = Qt;
22404 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22405 doc: /* Alist specifying how to blink the cursor off.
22406 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22407 `cursor-type' frame-parameter or variable equals ON-STATE,
22408 comparing using `equal', Emacs uses OFF-STATE to specify
22409 how to blink it off. */);
22410 Vblink_cursor_alist = Qnil;
22412 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22413 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22414 automatic_hscrolling_p = 1;
22416 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22417 doc: /* *How many columns away from the window edge point is allowed to get
22418 before automatic hscrolling will horizontally scroll the window. */);
22419 hscroll_margin = 5;
22421 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22422 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22423 When point is less than `automatic-hscroll-margin' columns from the window
22424 edge, automatic hscrolling will scroll the window by the amount of columns
22425 determined by this variable. If its value is a positive integer, scroll that
22426 many columns. If it's a positive floating-point number, it specifies the
22427 fraction of the window's width to scroll. If it's nil or zero, point will be
22428 centered horizontally after the scroll. Any other value, including negative
22429 numbers, are treated as if the value were zero.
22431 Automatic hscrolling always moves point outside the scroll margin, so if
22432 point was more than scroll step columns inside the margin, the window will
22433 scroll more than the value given by the scroll step.
22435 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22436 and `scroll-right' overrides this variable's effect. */);
22437 Vhscroll_step = make_number (0);
22439 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22440 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22441 Bind this around calls to `message' to let it take effect. */);
22442 message_truncate_lines = 0;
22444 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22445 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22446 Can be used to update submenus whose contents should vary. */);
22447 Vmenu_bar_update_hook = Qnil;
22449 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22450 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22451 inhibit_menubar_update = 0;
22453 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22454 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22455 inhibit_eval_during_redisplay = 0;
22457 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22458 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22459 inhibit_free_realized_faces = 0;
22461 #if GLYPH_DEBUG
22462 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22463 doc: /* Inhibit try_window_id display optimization. */);
22464 inhibit_try_window_id = 0;
22466 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22467 doc: /* Inhibit try_window_reusing display optimization. */);
22468 inhibit_try_window_reusing = 0;
22470 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22471 doc: /* Inhibit try_cursor_movement display optimization. */);
22472 inhibit_try_cursor_movement = 0;
22473 #endif /* GLYPH_DEBUG */
22477 /* Initialize this module when Emacs starts. */
22479 void
22480 init_xdisp ()
22482 Lisp_Object root_window;
22483 struct window *mini_w;
22485 current_header_line_height = current_mode_line_height = -1;
22487 CHARPOS (this_line_start_pos) = 0;
22489 mini_w = XWINDOW (minibuf_window);
22490 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22492 if (!noninteractive)
22494 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22495 int i;
22497 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22498 set_window_height (root_window,
22499 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22501 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22502 set_window_height (minibuf_window, 1, 0);
22504 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22505 mini_w->total_cols = make_number (FRAME_COLS (f));
22507 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22508 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22509 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22511 /* The default ellipsis glyphs `...'. */
22512 for (i = 0; i < 3; ++i)
22513 default_invis_vector[i] = make_number ('.');
22517 /* Allocate the buffer for frame titles.
22518 Also used for `format-mode-line'. */
22519 int size = 100;
22520 frame_title_buf = (char *) xmalloc (size);
22521 frame_title_buf_end = frame_title_buf + size;
22522 frame_title_ptr = NULL;
22525 help_echo_showing_p = 0;
22529 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22530 (do not change this comment) */