Typo.
[emacs.git] / src / xdisp.c
blob3ed57b299bfabf07f9c373fa0dcecc2e34e262fb
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99, 2000, 2001, 2002, 2003
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"
202 Cursor No_Cursor;
203 #endif
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
209 #define INFINITY 10000000
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
217 extern int interrupt_input;
218 extern int command_loop_level;
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 Lisp_Object Qrisky_local_variable;
247 /* Holds the list (error). */
248 Lisp_Object list_of_error;
250 /* Functions called to fontify regions of text. */
252 Lisp_Object Vfontification_functions;
253 Lisp_Object Qfontification_functions;
255 /* Non-zero means automatically select any window when the mouse
256 cursor moves into it. */
257 int mouse_autoselect_window;
259 /* Non-zero means draw tool bar buttons raised when the mouse moves
260 over them. */
262 int auto_raise_tool_bar_buttons_p;
264 /* Margin around tool bar buttons in pixels. */
266 Lisp_Object Vtool_bar_button_margin;
268 /* Thickness of shadow to draw around tool bar buttons. */
270 EMACS_INT tool_bar_button_relief;
272 /* Non-zero means automatically resize tool-bars so that all tool-bar
273 items are visible, and no blank lines remain. */
275 int auto_resize_tool_bars_p;
277 /* Non-zero means draw block and hollow cursor as wide as the glyph
278 under it. For example, if a block cursor is over a tab, it will be
279 drawn as wide as that tab on the display. */
281 int x_stretch_cursor_p;
283 /* Non-nil means don't actually do any redisplay. */
285 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
289 int inhibit_eval_during_redisplay;
291 /* Names of text properties relevant for redisplay. */
293 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
294 extern Lisp_Object Qface, Qinvisible, Qwidth;
296 /* Symbols used in text property values. */
298 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
299 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
300 Lisp_Object Qmargin;
301 extern Lisp_Object Qheight;
302 extern Lisp_Object QCwidth, QCheight, QCascent;
304 /* Non-nil means highlight trailing whitespace. */
306 Lisp_Object Vshow_trailing_whitespace;
308 /* Name of the face used to highlight trailing whitespace. */
310 Lisp_Object Qtrailing_whitespace;
312 /* The symbol `image' which is the car of the lists used to represent
313 images in Lisp. */
315 Lisp_Object Qimage;
317 /* Non-zero means print newline to stdout before next mini-buffer
318 message. */
320 int noninteractive_need_newline;
322 /* Non-zero means print newline to message log before next message. */
324 static int message_log_need_newline;
326 /* Three markers that message_dolog uses.
327 It could allocate them itself, but that causes trouble
328 in handling memory-full errors. */
329 static Lisp_Object message_dolog_marker1;
330 static Lisp_Object message_dolog_marker2;
331 static Lisp_Object message_dolog_marker3;
333 /* The buffer position of the first character appearing entirely or
334 partially on the line of the selected window which contains the
335 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
336 redisplay optimization in redisplay_internal. */
338 static struct text_pos this_line_start_pos;
340 /* Number of characters past the end of the line above, including the
341 terminating newline. */
343 static struct text_pos this_line_end_pos;
345 /* The vertical positions and the height of this line. */
347 static int this_line_vpos;
348 static int this_line_y;
349 static int this_line_pixel_height;
351 /* X position at which this display line starts. Usually zero;
352 negative if first character is partially visible. */
354 static int this_line_start_x;
356 /* Buffer that this_line_.* variables are referring to. */
358 static struct buffer *this_line_buffer;
360 /* Nonzero means truncate lines in all windows less wide than the
361 frame. */
363 int truncate_partial_width_windows;
365 /* A flag to control how to display unibyte 8-bit character. */
367 int unibyte_display_via_language_environment;
369 /* Nonzero means we have more than one non-mini-buffer-only frame.
370 Not guaranteed to be accurate except while parsing
371 frame-title-format. */
373 int multiple_frames;
375 Lisp_Object Vglobal_mode_string;
377 /* Marker for where to display an arrow on top of the buffer text. */
379 Lisp_Object Voverlay_arrow_position;
381 /* String to display for the arrow. Only used on terminal frames. */
383 Lisp_Object Voverlay_arrow_string;
385 /* Values of those variables at last redisplay. However, if
386 Voverlay_arrow_position is a marker, last_arrow_position is its
387 numerical position. */
389 static Lisp_Object last_arrow_position, last_arrow_string;
391 /* Like mode-line-format, but for the title bar on a visible frame. */
393 Lisp_Object Vframe_title_format;
395 /* Like mode-line-format, but for the title bar on an iconified frame. */
397 Lisp_Object Vicon_title_format;
399 /* List of functions to call when a window's size changes. These
400 functions get one arg, a frame on which one or more windows' sizes
401 have changed. */
403 static Lisp_Object Vwindow_size_change_functions;
405 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
407 /* Nonzero if overlay arrow has been displayed once in this window. */
409 static int overlay_arrow_seen;
411 /* Nonzero means highlight the region even in nonselected windows. */
413 int highlight_nonselected_windows;
415 /* If cursor motion alone moves point off frame, try scrolling this
416 many lines up or down if that will bring it back. */
418 static EMACS_INT scroll_step;
420 /* Nonzero means scroll just far enough to bring point back on the
421 screen, when appropriate. */
423 static EMACS_INT scroll_conservatively;
425 /* Recenter the window whenever point gets within this many lines of
426 the top or bottom of the window. This value is translated into a
427 pixel value by multiplying it with CANON_Y_UNIT, which means that
428 there is really a fixed pixel height scroll margin. */
430 EMACS_INT scroll_margin;
432 /* Number of windows showing the buffer of the selected window (or
433 another buffer with the same base buffer). keyboard.c refers to
434 this. */
436 int buffer_shared;
438 /* Vector containing glyphs for an ellipsis `...'. */
440 static Lisp_Object default_invis_vector[3];
442 /* Zero means display the mode-line/header-line/menu-bar in the default face
443 (this slightly odd definition is for compatibility with previous versions
444 of emacs), non-zero means display them using their respective faces.
446 This variable is deprecated. */
448 int mode_line_inverse_video;
450 /* Prompt to display in front of the mini-buffer contents. */
452 Lisp_Object minibuf_prompt;
454 /* Width of current mini-buffer prompt. Only set after display_line
455 of the line that contains the prompt. */
457 int minibuf_prompt_width;
459 /* This is the window where the echo area message was displayed. It
460 is always a mini-buffer window, but it may not be the same window
461 currently active as a mini-buffer. */
463 Lisp_Object echo_area_window;
465 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
466 pushes the current message and the value of
467 message_enable_multibyte on the stack, the function restore_message
468 pops the stack and displays MESSAGE again. */
470 Lisp_Object Vmessage_stack;
472 /* Nonzero means multibyte characters were enabled when the echo area
473 message was specified. */
475 int message_enable_multibyte;
477 /* Nonzero if we should redraw the mode lines on the next redisplay. */
479 int update_mode_lines;
481 /* Nonzero if window sizes or contents have changed since last
482 redisplay that finished. */
484 int windows_or_buffers_changed;
486 /* Nonzero means a frame's cursor type has been changed. */
488 int cursor_type_changed;
490 /* Nonzero after display_mode_line if %l was used and it displayed a
491 line number. */
493 int line_number_displayed;
495 /* Maximum buffer size for which to display line numbers. */
497 Lisp_Object Vline_number_display_limit;
499 /* Line width to consider when repositioning for line number display. */
501 static EMACS_INT line_number_display_limit_width;
503 /* Number of lines to keep in the message log buffer. t means
504 infinite. nil means don't log at all. */
506 Lisp_Object Vmessage_log_max;
508 /* The name of the *Messages* buffer, a string. */
510 static Lisp_Object Vmessages_buffer_name;
512 /* Current, index 0, and last displayed echo area message. Either
513 buffers from echo_buffers, or nil to indicate no message. */
515 Lisp_Object echo_area_buffer[2];
517 /* The buffers referenced from echo_area_buffer. */
519 static Lisp_Object echo_buffer[2];
521 /* A vector saved used in with_area_buffer to reduce consing. */
523 static Lisp_Object Vwith_echo_area_save_vector;
525 /* Non-zero means display_echo_area should display the last echo area
526 message again. Set by redisplay_preserve_echo_area. */
528 static int display_last_displayed_message_p;
530 /* Nonzero if echo area is being used by print; zero if being used by
531 message. */
533 int message_buf_print;
535 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
537 Lisp_Object Qinhibit_menubar_update;
538 int inhibit_menubar_update;
540 /* Maximum height for resizing mini-windows. Either a float
541 specifying a fraction of the available height, or an integer
542 specifying a number of lines. */
544 Lisp_Object Vmax_mini_window_height;
546 /* Non-zero means messages should be displayed with truncated
547 lines instead of being continued. */
549 int message_truncate_lines;
550 Lisp_Object Qmessage_truncate_lines;
552 /* Set to 1 in clear_message to make redisplay_internal aware
553 of an emptied echo area. */
555 static int message_cleared_p;
557 /* Non-zero means we want a hollow cursor in windows that are not
558 selected. Zero means there's no cursor in such windows. */
560 Lisp_Object Vcursor_in_non_selected_windows;
561 Lisp_Object Qcursor_in_non_selected_windows;
563 /* How to blink the default frame cursor off. */
564 Lisp_Object Vblink_cursor_alist;
566 /* A scratch glyph row with contents used for generating truncation
567 glyphs. Also used in direct_output_for_insert. */
569 #define MAX_SCRATCH_GLYPHS 100
570 struct glyph_row scratch_glyph_row;
571 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
573 /* Ascent and height of the last line processed by move_it_to. */
575 static int last_max_ascent, last_height;
577 /* Non-zero if there's a help-echo in the echo area. */
579 int help_echo_showing_p;
581 /* If >= 0, computed, exact values of mode-line and header-line height
582 to use in the macros CURRENT_MODE_LINE_HEIGHT and
583 CURRENT_HEADER_LINE_HEIGHT. */
585 int current_mode_line_height, current_header_line_height;
587 /* The maximum distance to look ahead for text properties. Values
588 that are too small let us call compute_char_face and similar
589 functions too often which is expensive. Values that are too large
590 let us call compute_char_face and alike too often because we
591 might not be interested in text properties that far away. */
593 #define TEXT_PROP_DISTANCE_LIMIT 100
595 #if GLYPH_DEBUG
597 /* Variables to turn off display optimizations from Lisp. */
599 int inhibit_try_window_id, inhibit_try_window_reusing;
600 int inhibit_try_cursor_movement;
602 /* Non-zero means print traces of redisplay if compiled with
603 GLYPH_DEBUG != 0. */
605 int trace_redisplay_p;
607 #endif /* GLYPH_DEBUG */
609 #ifdef DEBUG_TRACE_MOVE
610 /* Non-zero means trace with TRACE_MOVE to stderr. */
611 int trace_move;
613 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
614 #else
615 #define TRACE_MOVE(x) (void) 0
616 #endif
618 /* Non-zero means automatically scroll windows horizontally to make
619 point visible. */
621 int automatic_hscrolling_p;
623 /* How close to the margin can point get before the window is scrolled
624 horizontally. */
625 EMACS_INT hscroll_margin;
627 /* How much to scroll horizontally when point is inside the above margin. */
628 Lisp_Object Vhscroll_step;
630 /* A list of symbols, one for each supported image type. */
632 Lisp_Object Vimage_types;
634 /* The variable `resize-mini-windows'. If nil, don't resize
635 mini-windows. If t, always resize them to fit the text they
636 display. If `grow-only', let mini-windows grow only until they
637 become empty. */
639 Lisp_Object Vresize_mini_windows;
641 /* Buffer being redisplayed -- for redisplay_window_error. */
643 struct buffer *displayed_buffer;
645 /* Value returned from text property handlers (see below). */
647 enum prop_handled
649 HANDLED_NORMALLY,
650 HANDLED_RECOMPUTE_PROPS,
651 HANDLED_OVERLAY_STRING_CONSUMED,
652 HANDLED_RETURN
655 /* A description of text properties that redisplay is interested
656 in. */
658 struct props
660 /* The name of the property. */
661 Lisp_Object *name;
663 /* A unique index for the property. */
664 enum prop_idx idx;
666 /* A handler function called to set up iterator IT from the property
667 at IT's current position. Value is used to steer handle_stop. */
668 enum prop_handled (*handler) P_ ((struct it *it));
671 static enum prop_handled handle_face_prop P_ ((struct it *));
672 static enum prop_handled handle_invisible_prop P_ ((struct it *));
673 static enum prop_handled handle_display_prop P_ ((struct it *));
674 static enum prop_handled handle_composition_prop P_ ((struct it *));
675 static enum prop_handled handle_overlay_change P_ ((struct it *));
676 static enum prop_handled handle_fontified_prop P_ ((struct it *));
678 /* Properties handled by iterators. */
680 static struct props it_props[] =
682 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
683 /* Handle `face' before `display' because some sub-properties of
684 `display' need to know the face. */
685 {&Qface, FACE_PROP_IDX, handle_face_prop},
686 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
687 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
688 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
689 {NULL, 0, NULL}
692 /* Value is the position described by X. If X is a marker, value is
693 the marker_position of X. Otherwise, value is X. */
695 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
697 /* Enumeration returned by some move_it_.* functions internally. */
699 enum move_it_result
701 /* Not used. Undefined value. */
702 MOVE_UNDEFINED,
704 /* Move ended at the requested buffer position or ZV. */
705 MOVE_POS_MATCH_OR_ZV,
707 /* Move ended at the requested X pixel position. */
708 MOVE_X_REACHED,
710 /* Move within a line ended at the end of a line that must be
711 continued. */
712 MOVE_LINE_CONTINUED,
714 /* Move within a line ended at the end of a line that would
715 be displayed truncated. */
716 MOVE_LINE_TRUNCATED,
718 /* Move within a line ended at a line end. */
719 MOVE_NEWLINE_OR_CR
722 /* This counter is used to clear the face cache every once in a while
723 in redisplay_internal. It is incremented for each redisplay.
724 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
725 cleared. */
727 #define CLEAR_FACE_CACHE_COUNT 500
728 static int clear_face_cache_count;
730 /* Record the previous terminal frame we displayed. */
732 static struct frame *previous_terminal_frame;
734 /* Non-zero while redisplay_internal is in progress. */
736 int redisplaying_p;
738 /* Non-zero means don't free realized faces. Bound while freeing
739 realized faces is dangerous because glyph matrices might still
740 reference them. */
742 int inhibit_free_realized_faces;
743 Lisp_Object Qinhibit_free_realized_faces;
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 int help_echo_pos;
753 /* Temporary variable for XTread_socket. */
755 Lisp_Object previous_help_echo_string;
759 /* Function prototypes. */
761 static void setup_for_ellipsis P_ ((struct it *));
762 static void mark_window_display_accurate_1 P_ ((struct window *, int));
763 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
764 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
765 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
766 static int redisplay_mode_lines P_ ((Lisp_Object, int));
767 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
769 #if 0
770 static int invisible_text_between_p P_ ((struct it *, int, int));
771 #endif
773 static int next_element_from_ellipsis P_ ((struct it *));
774 static void pint2str P_ ((char *, int, int));
775 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
776 struct text_pos));
777 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
778 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
779 static void store_frame_title_char P_ ((char));
780 static int store_frame_title P_ ((const unsigned char *, int, int));
781 static void x_consider_frame_title P_ ((Lisp_Object));
782 static void handle_stop P_ ((struct it *));
783 static int tool_bar_lines_needed P_ ((struct frame *));
784 static int single_display_prop_intangible_p P_ ((Lisp_Object));
785 static void ensure_echo_area_buffers P_ ((void));
786 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
787 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
788 static int with_echo_area_buffer P_ ((struct window *, int,
789 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
790 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
791 static void clear_garbaged_frames P_ ((void));
792 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
793 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
794 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
795 static int display_echo_area P_ ((struct window *));
796 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
797 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
798 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
799 static int string_char_and_length P_ ((const unsigned char *, int, int *));
800 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
801 struct text_pos));
802 static int compute_window_start_on_continuation_line P_ ((struct window *));
803 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
804 static void insert_left_trunc_glyphs P_ ((struct it *));
805 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
806 static void extend_face_to_end_of_line P_ ((struct it *));
807 static int append_space P_ ((struct it *, int));
808 static int make_cursor_line_fully_visible P_ ((struct window *));
809 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
810 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
811 static int trailing_whitespace_p P_ ((int));
812 static int message_log_check_duplicate P_ ((int, int, int, int));
813 static void push_it P_ ((struct it *));
814 static void pop_it P_ ((struct it *));
815 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
816 static void redisplay_internal P_ ((int));
817 static int echo_area_display P_ ((int));
818 static void redisplay_windows P_ ((Lisp_Object));
819 static void redisplay_window P_ ((Lisp_Object, int));
820 static Lisp_Object redisplay_window_error ();
821 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
822 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
823 static void update_menu_bar P_ ((struct frame *, int));
824 static int try_window_reusing_current_matrix P_ ((struct window *));
825 static int try_window_id P_ ((struct window *));
826 static int display_line P_ ((struct it *));
827 static int display_mode_lines P_ ((struct window *));
828 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
829 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
830 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
831 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
832 static void display_menu_bar P_ ((struct window *));
833 static int display_count_lines P_ ((int, int, int, int, int *));
834 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
835 int, int, struct it *, int, int, int, int));
836 static void compute_line_metrics P_ ((struct it *));
837 static void run_redisplay_end_trigger_hook P_ ((struct it *));
838 static int get_overlay_strings P_ ((struct it *, int));
839 static void next_overlay_string P_ ((struct it *));
840 static void reseat P_ ((struct it *, struct text_pos, int));
841 static void reseat_1 P_ ((struct it *, struct text_pos, int));
842 static void back_to_previous_visible_line_start P_ ((struct it *));
843 static void reseat_at_previous_visible_line_start P_ ((struct it *));
844 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
845 static int next_element_from_display_vector P_ ((struct it *));
846 static int next_element_from_string P_ ((struct it *));
847 static int next_element_from_c_string P_ ((struct it *));
848 static int next_element_from_buffer P_ ((struct it *));
849 static int next_element_from_composition P_ ((struct it *));
850 static int next_element_from_image P_ ((struct it *));
851 static int next_element_from_stretch P_ ((struct it *));
852 static void load_overlay_strings P_ ((struct it *, int));
853 static int init_from_display_pos P_ ((struct it *, struct window *,
854 struct display_pos *));
855 static void reseat_to_string P_ ((struct it *, unsigned char *,
856 Lisp_Object, int, int, int, int));
857 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
858 int, int, int));
859 void move_it_vertically_backward P_ ((struct it *, int));
860 static void init_to_row_start P_ ((struct it *, struct window *,
861 struct glyph_row *));
862 static int init_to_row_end P_ ((struct it *, struct window *,
863 struct glyph_row *));
864 static void back_to_previous_line_start P_ ((struct it *));
865 static int forward_to_next_line_start P_ ((struct it *, int *));
866 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
867 Lisp_Object, int));
868 static struct text_pos string_pos P_ ((int, Lisp_Object));
869 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
870 static int number_of_chars P_ ((unsigned char *, int));
871 static void compute_stop_pos P_ ((struct it *));
872 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
873 Lisp_Object));
874 static int face_before_or_after_it_pos P_ ((struct it *, int));
875 static int next_overlay_change P_ ((int));
876 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
877 Lisp_Object, struct text_pos *,
878 int));
879 static int underlying_face_id P_ ((struct it *));
880 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
881 struct window *));
883 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
884 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
886 #ifdef HAVE_WINDOW_SYSTEM
888 static void update_tool_bar P_ ((struct frame *, int));
889 static void build_desired_tool_bar_string P_ ((struct frame *f));
890 static int redisplay_tool_bar P_ ((struct frame *));
891 static void display_tool_bar_line P_ ((struct it *));
892 static void notice_overwritten_cursor P_ ((struct window *,
893 enum glyph_row_area,
894 int, int, int, int));
898 #endif /* HAVE_WINDOW_SYSTEM */
901 /***********************************************************************
902 Window display dimensions
903 ***********************************************************************/
905 /* Return the bottom boundary y-position for text lines in window W.
906 This is the first y position at which a line cannot start.
907 It is relative to the top of the window.
909 This is the height of W minus the height of a mode line, if any. */
911 INLINE int
912 window_text_bottom_y (w)
913 struct window *w;
915 struct frame *f = XFRAME (w->frame);
916 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
918 if (WINDOW_WANTS_MODELINE_P (w))
919 height -= CURRENT_MODE_LINE_HEIGHT (w);
920 return height;
924 /* Return the pixel width of display area AREA of window W. AREA < 0
925 means return the total width of W, not including fringes to
926 the left and right of the window. */
928 INLINE int
929 window_box_width (w, area)
930 struct window *w;
931 int area;
933 struct frame *f = XFRAME (w->frame);
934 int width = XFASTINT (w->width);
936 if (!w->pseudo_window_p)
938 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
940 if (area == TEXT_AREA)
942 if (INTEGERP (w->left_margin_width))
943 width -= XFASTINT (w->left_margin_width);
944 if (INTEGERP (w->right_margin_width))
945 width -= XFASTINT (w->right_margin_width);
947 else if (area == LEFT_MARGIN_AREA)
948 width = (INTEGERP (w->left_margin_width)
949 ? XFASTINT (w->left_margin_width) : 0);
950 else if (area == RIGHT_MARGIN_AREA)
951 width = (INTEGERP (w->right_margin_width)
952 ? XFASTINT (w->right_margin_width) : 0);
955 return width * CANON_X_UNIT (f);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
962 INLINE int
963 window_box_height (w)
964 struct window *w;
966 struct frame *f = XFRAME (w->frame);
967 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
969 xassert (height >= 0);
971 /* Note: the code below that determines the mode-line/header-line
972 height is essentially the same as that contained in the macro
973 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
974 the appropriate glyph row has its `mode_line_p' flag set,
975 and if it doesn't, uses estimate_mode_line_height instead. */
977 if (WINDOW_WANTS_MODELINE_P (w))
979 struct glyph_row *ml_row
980 = (w->current_matrix && w->current_matrix->rows
981 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
982 : 0);
983 if (ml_row && ml_row->mode_line_p)
984 height -= ml_row->height;
985 else
986 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
989 if (WINDOW_WANTS_HEADER_LINE_P (w))
991 struct glyph_row *hl_row
992 = (w->current_matrix && w->current_matrix->rows
993 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
994 : 0);
995 if (hl_row && hl_row->mode_line_p)
996 height -= hl_row->height;
997 else
998 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1001 /* With a very small font and a mode-line that's taller than
1002 default, we might end up with a negative height. */
1003 return max (0, height);
1007 /* Return the frame-relative coordinate of the left edge of display
1008 area AREA of window W. AREA < 0 means return the left edge of the
1009 whole window, to the right of the left fringe of W. */
1011 INLINE int
1012 window_box_left (w, area)
1013 struct window *w;
1014 int area;
1016 struct frame *f = XFRAME (w->frame);
1017 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
1019 if (!w->pseudo_window_p)
1021 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
1022 + FRAME_LEFT_FRINGE_WIDTH (f));
1024 if (area == TEXT_AREA)
1025 x += window_box_width (w, LEFT_MARGIN_AREA);
1026 else if (area == RIGHT_MARGIN_AREA)
1027 x += (window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA));
1031 return x;
1035 /* Return the frame-relative coordinate of the right edge of display
1036 area AREA of window W. AREA < 0 means return the left edge of the
1037 whole window, to the left of the right fringe of W. */
1039 INLINE int
1040 window_box_right (w, area)
1041 struct window *w;
1042 int area;
1044 return window_box_left (w, area) + window_box_width (w, area);
1048 /* Get the bounding box of the display area AREA of window W, without
1049 mode lines, in frame-relative coordinates. AREA < 0 means the
1050 whole window, not including the left and right fringes of
1051 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1052 coordinates of the upper-left corner of the box. Return in
1053 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1055 INLINE void
1056 window_box (w, area, box_x, box_y, box_width, box_height)
1057 struct window *w;
1058 int area;
1059 int *box_x, *box_y, *box_width, *box_height;
1061 struct frame *f = XFRAME (w->frame);
1063 *box_width = window_box_width (w, area);
1064 *box_height = window_box_height (w);
1065 *box_x = window_box_left (w, area);
1066 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1067 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1069 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1073 /* Get the bounding box of the display area AREA of window W, without
1074 mode lines. AREA < 0 means the whole window, not including the
1075 left and right fringe of the window. Return in *TOP_LEFT_X
1076 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1077 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1078 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1079 box. */
1081 INLINE void
1082 window_box_edges (w, area, top_left_x, top_left_y,
1083 bottom_right_x, bottom_right_y)
1084 struct window *w;
1085 int area;
1086 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1088 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1089 bottom_right_y);
1090 *bottom_right_x += *top_left_x;
1091 *bottom_right_y += *top_left_y;
1096 /***********************************************************************
1097 Utilities
1098 ***********************************************************************/
1100 /* Return the bottom y-position of the line the iterator IT is in.
1101 This can modify IT's settings. */
1104 line_bottom_y (it)
1105 struct it *it;
1107 int line_height = it->max_ascent + it->max_descent;
1108 int line_top_y = it->current_y;
1110 if (line_height == 0)
1112 if (last_height)
1113 line_height = last_height;
1114 else if (IT_CHARPOS (*it) < ZV)
1116 move_it_by_lines (it, 1, 1);
1117 line_height = (it->max_ascent || it->max_descent
1118 ? it->max_ascent + it->max_descent
1119 : last_height);
1121 else
1123 struct glyph_row *row = it->glyph_row;
1125 /* Use the default character height. */
1126 it->glyph_row = NULL;
1127 it->what = IT_CHARACTER;
1128 it->c = ' ';
1129 it->len = 1;
1130 PRODUCE_GLYPHS (it);
1131 line_height = it->ascent + it->descent;
1132 it->glyph_row = row;
1136 return line_top_y + line_height;
1140 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1141 1 if POS is visible and the line containing POS is fully visible.
1142 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1143 and header-lines heights. */
1146 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1147 struct window *w;
1148 int charpos, *fully, exact_mode_line_heights_p;
1150 struct it it;
1151 struct text_pos top;
1152 int visible_p;
1153 struct buffer *old_buffer = NULL;
1155 if (XBUFFER (w->buffer) != current_buffer)
1157 old_buffer = current_buffer;
1158 set_buffer_internal_1 (XBUFFER (w->buffer));
1161 *fully = visible_p = 0;
1162 SET_TEXT_POS_FROM_MARKER (top, w->start);
1164 /* Compute exact mode line heights, if requested. */
1165 if (exact_mode_line_heights_p)
1167 if (WINDOW_WANTS_MODELINE_P (w))
1168 current_mode_line_height
1169 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1170 current_buffer->mode_line_format);
1172 if (WINDOW_WANTS_HEADER_LINE_P (w))
1173 current_header_line_height
1174 = display_mode_line (w, HEADER_LINE_FACE_ID,
1175 current_buffer->header_line_format);
1178 start_display (&it, w, top);
1179 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1180 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1182 /* Note that we may overshoot because of invisible text. */
1183 if (IT_CHARPOS (it) >= charpos)
1185 int top_y = it.current_y;
1186 int bottom_y = line_bottom_y (&it);
1187 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1189 if (top_y < window_top_y)
1190 visible_p = bottom_y > window_top_y;
1191 else if (top_y < it.last_visible_y)
1193 visible_p = 1;
1194 *fully = bottom_y <= it.last_visible_y;
1197 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1199 move_it_by_lines (&it, 1, 0);
1200 if (charpos < IT_CHARPOS (it))
1202 visible_p = 1;
1203 *fully = 0;
1207 if (old_buffer)
1208 set_buffer_internal_1 (old_buffer);
1210 current_header_line_height = current_mode_line_height = -1;
1211 return visible_p;
1215 /* Return the next character from STR which is MAXLEN bytes long.
1216 Return in *LEN the length of the character. This is like
1217 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1218 we find one, we return a `?', but with the length of the invalid
1219 character. */
1221 static INLINE int
1222 string_char_and_length (str, maxlen, len)
1223 const unsigned char *str;
1224 int maxlen, *len;
1226 int c;
1228 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1229 if (!CHAR_VALID_P (c, 1))
1230 /* We may not change the length here because other places in Emacs
1231 don't use this function, i.e. they silently accept invalid
1232 characters. */
1233 c = '?';
1235 return c;
1240 /* Given a position POS containing a valid character and byte position
1241 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1243 static struct text_pos
1244 string_pos_nchars_ahead (pos, string, nchars)
1245 struct text_pos pos;
1246 Lisp_Object string;
1247 int nchars;
1249 xassert (STRINGP (string) && nchars >= 0);
1251 if (STRING_MULTIBYTE (string))
1253 int rest = SBYTES (string) - BYTEPOS (pos);
1254 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1255 int len;
1257 while (nchars--)
1259 string_char_and_length (p, rest, &len);
1260 p += len, rest -= len;
1261 xassert (rest >= 0);
1262 CHARPOS (pos) += 1;
1263 BYTEPOS (pos) += len;
1266 else
1267 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1269 return pos;
1273 /* Value is the text position, i.e. character and byte position,
1274 for character position CHARPOS in STRING. */
1276 static INLINE struct text_pos
1277 string_pos (charpos, string)
1278 int charpos;
1279 Lisp_Object string;
1281 struct text_pos pos;
1282 xassert (STRINGP (string));
1283 xassert (charpos >= 0);
1284 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1285 return pos;
1289 /* Value is a text position, i.e. character and byte position, for
1290 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1291 means recognize multibyte characters. */
1293 static struct text_pos
1294 c_string_pos (charpos, s, multibyte_p)
1295 int charpos;
1296 unsigned char *s;
1297 int multibyte_p;
1299 struct text_pos pos;
1301 xassert (s != NULL);
1302 xassert (charpos >= 0);
1304 if (multibyte_p)
1306 int rest = strlen (s), len;
1308 SET_TEXT_POS (pos, 0, 0);
1309 while (charpos--)
1311 string_char_and_length (s, rest, &len);
1312 s += len, rest -= len;
1313 xassert (rest >= 0);
1314 CHARPOS (pos) += 1;
1315 BYTEPOS (pos) += len;
1318 else
1319 SET_TEXT_POS (pos, charpos, charpos);
1321 return pos;
1325 /* Value is the number of characters in C string S. MULTIBYTE_P
1326 non-zero means recognize multibyte characters. */
1328 static int
1329 number_of_chars (s, multibyte_p)
1330 unsigned char *s;
1331 int multibyte_p;
1333 int nchars;
1335 if (multibyte_p)
1337 int rest = strlen (s), len;
1338 unsigned char *p = (unsigned char *) s;
1340 for (nchars = 0; rest > 0; ++nchars)
1342 string_char_and_length (p, rest, &len);
1343 rest -= len, p += len;
1346 else
1347 nchars = strlen (s);
1349 return nchars;
1353 /* Compute byte position NEWPOS->bytepos corresponding to
1354 NEWPOS->charpos. POS is a known position in string STRING.
1355 NEWPOS->charpos must be >= POS.charpos. */
1357 static void
1358 compute_string_pos (newpos, pos, string)
1359 struct text_pos *newpos, pos;
1360 Lisp_Object string;
1362 xassert (STRINGP (string));
1363 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1365 if (STRING_MULTIBYTE (string))
1366 *newpos = string_pos_nchars_ahead (pos, string,
1367 CHARPOS (*newpos) - CHARPOS (pos));
1368 else
1369 BYTEPOS (*newpos) = CHARPOS (*newpos);
1372 /* EXPORT:
1373 Return an estimation of the pixel height of mode or top lines on
1374 frame F. FACE_ID specifies what line's height to estimate. */
1377 estimate_mode_line_height (f, face_id)
1378 struct frame *f;
1379 enum face_id face_id;
1381 #ifdef HAVE_WINDOW_SYSTEM
1382 if (FRAME_WINDOW_P (f))
1384 int height = FONT_HEIGHT (FRAME_FONT (f));
1386 /* This function is called so early when Emacs starts that the face
1387 cache and mode line face are not yet initialized. */
1388 if (FRAME_FACE_CACHE (f))
1390 struct face *face = FACE_FROM_ID (f, face_id);
1391 if (face)
1393 if (face->font)
1394 height = FONT_HEIGHT (face->font);
1395 if (face->box_line_width > 0)
1396 height += 2 * face->box_line_width;
1400 return height;
1402 #endif
1404 return 1;
1407 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1408 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1409 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1410 not force the value into range. */
1412 void
1413 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1414 FRAME_PTR f;
1415 register int pix_x, pix_y;
1416 int *x, *y;
1417 NativeRectangle *bounds;
1418 int noclip;
1421 #ifdef HAVE_WINDOW_SYSTEM
1422 if (FRAME_WINDOW_P (f))
1424 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
1425 even for negative values. */
1426 if (pix_x < 0)
1427 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
1428 if (pix_y < 0)
1429 pix_y -= FRAME_X_OUTPUT(f)->line_height - 1;
1431 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
1432 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
1434 if (bounds)
1435 STORE_NATIVE_RECT (*bounds,
1436 CHAR_TO_PIXEL_COL (f, pix_x),
1437 CHAR_TO_PIXEL_ROW (f, pix_y),
1438 FONT_WIDTH (FRAME_FONT (f)) - 1,
1439 FRAME_X_OUTPUT (f)->line_height - 1);
1441 if (!noclip)
1443 if (pix_x < 0)
1444 pix_x = 0;
1445 else if (pix_x > FRAME_WINDOW_WIDTH (f))
1446 pix_x = FRAME_WINDOW_WIDTH (f);
1448 if (pix_y < 0)
1449 pix_y = 0;
1450 else if (pix_y > f->height)
1451 pix_y = f->height;
1454 #endif
1456 *x = pix_x;
1457 *y = pix_y;
1461 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1462 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1463 can't tell the positions because W's display is not up to date,
1464 return 0. */
1467 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1468 struct window *w;
1469 int hpos, vpos;
1470 int *frame_x, *frame_y;
1472 #ifdef HAVE_WINDOW_SYSTEM
1473 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1475 int success_p;
1477 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1478 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1480 if (display_completed)
1482 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1483 struct glyph *glyph = row->glyphs[TEXT_AREA];
1484 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1486 hpos = row->x;
1487 vpos = row->y;
1488 while (glyph < end)
1490 hpos += glyph->pixel_width;
1491 ++glyph;
1494 success_p = 1;
1496 else
1498 hpos = vpos = 0;
1499 success_p = 0;
1502 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1503 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1504 return success_p;
1506 #endif
1508 *frame_x = hpos;
1509 *frame_y = vpos;
1510 return 1;
1514 #ifdef HAVE_WINDOW_SYSTEM
1516 /* Find the glyph under window-relative coordinates X/Y in window W.
1517 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1518 strings. Return in *HPOS and *VPOS the row and column number of
1519 the glyph found. Return in *AREA the glyph area containing X.
1520 Value is a pointer to the glyph found or null if X/Y is not on
1521 text, or we can't tell because W's current matrix is not up to
1522 date. */
1524 static struct glyph *
1525 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1526 struct window *w;
1527 int x, y;
1528 int *hpos, *vpos, *area;
1529 int buffer_only_p;
1531 struct glyph *glyph, *end;
1532 struct glyph_row *row = NULL;
1533 int x0, i, left_area_width;
1535 /* Find row containing Y. Give up if some row is not enabled. */
1536 for (i = 0; i < w->current_matrix->nrows; ++i)
1538 row = MATRIX_ROW (w->current_matrix, i);
1539 if (!row->enabled_p)
1540 return NULL;
1541 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1542 break;
1545 *vpos = i;
1546 *hpos = 0;
1548 /* Give up if Y is not in the window. */
1549 if (i == w->current_matrix->nrows)
1550 return NULL;
1552 /* Get the glyph area containing X. */
1553 if (w->pseudo_window_p)
1555 *area = TEXT_AREA;
1556 x0 = 0;
1558 else
1560 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
1561 if (x < left_area_width)
1563 *area = LEFT_MARGIN_AREA;
1564 x0 = 0;
1566 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
1568 *area = TEXT_AREA;
1569 x0 = row->x + left_area_width;
1571 else
1573 *area = RIGHT_MARGIN_AREA;
1574 x0 = left_area_width + window_box_width (w, TEXT_AREA);
1578 /* Find glyph containing X. */
1579 glyph = row->glyphs[*area];
1580 end = glyph + row->used[*area];
1581 while (glyph < end)
1583 if (x < x0 + glyph->pixel_width)
1585 if (w->pseudo_window_p)
1586 break;
1587 else if (!buffer_only_p || BUFFERP (glyph->object))
1588 break;
1591 x0 += glyph->pixel_width;
1592 ++glyph;
1595 if (glyph == end)
1596 return NULL;
1598 *hpos = glyph - row->glyphs[*area];
1599 return glyph;
1603 /* EXPORT:
1604 Convert frame-relative x/y to coordinates relative to window W.
1605 Takes pseudo-windows into account. */
1607 void
1608 frame_to_window_pixel_xy (w, x, y)
1609 struct window *w;
1610 int *x, *y;
1612 if (w->pseudo_window_p)
1614 /* A pseudo-window is always full-width, and starts at the
1615 left edge of the frame, plus a frame border. */
1616 struct frame *f = XFRAME (w->frame);
1617 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
1618 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1620 else
1622 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
1623 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1627 /* EXPORT:
1628 Return in *R the clipping rectangle for glyph string S. */
1630 void
1631 get_glyph_string_clip_rect (s, nr)
1632 struct glyph_string *s;
1633 NativeRectangle *nr;
1635 XRectangle r;
1637 if (s->row->full_width_p)
1639 /* Draw full-width. X coordinates are relative to S->w->left. */
1640 int canon_x = CANON_X_UNIT (s->f);
1642 r.x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
1643 r.width = XFASTINT (s->w->width) * canon_x;
1645 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
1647 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
1648 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
1649 r.x -= width;
1652 r.x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1654 /* Unless displaying a mode or menu bar line, which are always
1655 fully visible, clip to the visible part of the row. */
1656 if (s->w->pseudo_window_p)
1657 r.height = s->row->visible_height;
1658 else
1659 r.height = s->height;
1661 else
1663 /* This is a text line that may be partially visible. */
1664 r.x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
1665 r.width = window_box_width (s->w, s->area);
1666 r.height = s->row->visible_height;
1669 /* If S draws overlapping rows, it's sufficient to use the top and
1670 bottom of the window for clipping because this glyph string
1671 intentionally draws over other lines. */
1672 if (s->for_overlaps_p)
1674 r.y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1675 r.height = window_text_bottom_y (s->w) - r.y;
1677 else
1679 /* Don't use S->y for clipping because it doesn't take partially
1680 visible lines into account. For example, it can be negative for
1681 partially visible lines at the top of a window. */
1682 if (!s->row->full_width_p
1683 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1684 r.y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1685 else
1686 r.y = max (0, s->row->y);
1688 /* If drawing a tool-bar window, draw it over the internal border
1689 at the top of the window. */
1690 if (s->w == XWINDOW (s->f->tool_bar_window))
1691 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1694 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1696 #ifdef HAVE_NTGUI
1697 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1698 /* If drawing the cursor, don't let glyph draw outside its
1699 advertised boundaries. Cleartype does this under some circumstances. */
1700 if (s->hl == DRAW_CURSOR)
1702 if (s->x > r.x)
1704 r.width -= s->x - r.x;
1705 r.x = s->x;
1707 r.width = min (r.width, s->first_glyph->pixel_width);
1709 #endif
1711 #ifdef CONVERT_FROM_XRECT
1712 CONVERT_FROM_XRECT (r, *nr);
1713 #else
1714 *nr = r;
1715 #endif
1718 #endif /* HAVE_WINDOW_SYSTEM */
1721 /***********************************************************************
1722 Lisp form evaluation
1723 ***********************************************************************/
1725 /* Error handler for safe_eval and safe_call. */
1727 static Lisp_Object
1728 safe_eval_handler (arg)
1729 Lisp_Object arg;
1731 add_to_log ("Error during redisplay: %s", arg, Qnil);
1732 return Qnil;
1736 /* Evaluate SEXPR and return the result, or nil if something went
1737 wrong. Prevent redisplay during the evaluation. */
1739 Lisp_Object
1740 safe_eval (sexpr)
1741 Lisp_Object sexpr;
1743 Lisp_Object val;
1745 if (inhibit_eval_during_redisplay)
1746 val = Qnil;
1747 else
1749 int count = SPECPDL_INDEX ();
1750 struct gcpro gcpro1;
1752 GCPRO1 (sexpr);
1753 specbind (Qinhibit_redisplay, Qt);
1754 /* Use Qt to ensure debugger does not run,
1755 so there is no possibility of wanting to redisplay. */
1756 val = internal_condition_case_1 (Feval, sexpr, Qt,
1757 safe_eval_handler);
1758 UNGCPRO;
1759 val = unbind_to (count, val);
1762 return val;
1766 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1767 Return the result, or nil if something went wrong. Prevent
1768 redisplay during the evaluation. */
1770 Lisp_Object
1771 safe_call (nargs, args)
1772 int nargs;
1773 Lisp_Object *args;
1775 Lisp_Object val;
1777 if (inhibit_eval_during_redisplay)
1778 val = Qnil;
1779 else
1781 int count = SPECPDL_INDEX ();
1782 struct gcpro gcpro1;
1784 GCPRO1 (args[0]);
1785 gcpro1.nvars = nargs;
1786 specbind (Qinhibit_redisplay, Qt);
1787 /* Use Qt to ensure debugger does not run,
1788 so there is no possibility of wanting to redisplay. */
1789 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1790 safe_eval_handler);
1791 UNGCPRO;
1792 val = unbind_to (count, val);
1795 return val;
1799 /* Call function FN with one argument ARG.
1800 Return the result, or nil if something went wrong. */
1802 Lisp_Object
1803 safe_call1 (fn, arg)
1804 Lisp_Object fn, arg;
1806 Lisp_Object args[2];
1807 args[0] = fn;
1808 args[1] = arg;
1809 return safe_call (2, args);
1814 /***********************************************************************
1815 Debugging
1816 ***********************************************************************/
1818 #if 0
1820 /* Define CHECK_IT to perform sanity checks on iterators.
1821 This is for debugging. It is too slow to do unconditionally. */
1823 static void
1824 check_it (it)
1825 struct it *it;
1827 if (it->method == next_element_from_string)
1829 xassert (STRINGP (it->string));
1830 xassert (IT_STRING_CHARPOS (*it) >= 0);
1832 else if (it->method == next_element_from_buffer)
1834 /* Check that character and byte positions agree. */
1835 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1838 if (it->dpvec)
1839 xassert (it->current.dpvec_index >= 0);
1840 else
1841 xassert (it->current.dpvec_index < 0);
1844 #define CHECK_IT(IT) check_it ((IT))
1846 #else /* not 0 */
1848 #define CHECK_IT(IT) (void) 0
1850 #endif /* not 0 */
1853 #if GLYPH_DEBUG
1855 /* Check that the window end of window W is what we expect it
1856 to be---the last row in the current matrix displaying text. */
1858 static void
1859 check_window_end (w)
1860 struct window *w;
1862 if (!MINI_WINDOW_P (w)
1863 && !NILP (w->window_end_valid))
1865 struct glyph_row *row;
1866 xassert ((row = MATRIX_ROW (w->current_matrix,
1867 XFASTINT (w->window_end_vpos)),
1868 !row->enabled_p
1869 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1870 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1874 #define CHECK_WINDOW_END(W) check_window_end ((W))
1876 #else /* not GLYPH_DEBUG */
1878 #define CHECK_WINDOW_END(W) (void) 0
1880 #endif /* not GLYPH_DEBUG */
1884 /***********************************************************************
1885 Iterator initialization
1886 ***********************************************************************/
1888 /* Initialize IT for displaying current_buffer in window W, starting
1889 at character position CHARPOS. CHARPOS < 0 means that no buffer
1890 position is specified which is useful when the iterator is assigned
1891 a position later. BYTEPOS is the byte position corresponding to
1892 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1894 If ROW is not null, calls to produce_glyphs with IT as parameter
1895 will produce glyphs in that row.
1897 BASE_FACE_ID is the id of a base face to use. It must be one of
1898 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1899 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1900 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1902 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1903 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1904 will be initialized to use the corresponding mode line glyph row of
1905 the desired matrix of W. */
1907 void
1908 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1909 struct it *it;
1910 struct window *w;
1911 int charpos, bytepos;
1912 struct glyph_row *row;
1913 enum face_id base_face_id;
1915 int highlight_region_p;
1917 /* Some precondition checks. */
1918 xassert (w != NULL && it != NULL);
1919 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1920 && charpos <= ZV));
1922 /* If face attributes have been changed since the last redisplay,
1923 free realized faces now because they depend on face definitions
1924 that might have changed. Don't free faces while there might be
1925 desired matrices pending which reference these faces. */
1926 if (face_change_count && !inhibit_free_realized_faces)
1928 face_change_count = 0;
1929 free_all_realized_faces (Qnil);
1932 /* Use one of the mode line rows of W's desired matrix if
1933 appropriate. */
1934 if (row == NULL)
1936 if (base_face_id == MODE_LINE_FACE_ID
1937 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1938 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1939 else if (base_face_id == HEADER_LINE_FACE_ID)
1940 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1943 /* Clear IT. */
1944 bzero (it, sizeof *it);
1945 it->current.overlay_string_index = -1;
1946 it->current.dpvec_index = -1;
1947 it->base_face_id = base_face_id;
1949 /* The window in which we iterate over current_buffer: */
1950 XSETWINDOW (it->window, w);
1951 it->w = w;
1952 it->f = XFRAME (w->frame);
1954 /* Extra space between lines (on window systems only). */
1955 if (base_face_id == DEFAULT_FACE_ID
1956 && FRAME_WINDOW_P (it->f))
1958 if (NATNUMP (current_buffer->extra_line_spacing))
1959 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1960 else if (it->f->extra_line_spacing > 0)
1961 it->extra_line_spacing = it->f->extra_line_spacing;
1964 /* If realized faces have been removed, e.g. because of face
1965 attribute changes of named faces, recompute them. When running
1966 in batch mode, the face cache of Vterminal_frame is null. If
1967 we happen to get called, make a dummy face cache. */
1968 if (
1969 #ifndef WINDOWSNT
1970 noninteractive &&
1971 #endif
1972 FRAME_FACE_CACHE (it->f) == NULL)
1973 init_frame_faces (it->f);
1974 if (FRAME_FACE_CACHE (it->f)->used == 0)
1975 recompute_basic_faces (it->f);
1977 /* Current value of the `space-width', and 'height' properties. */
1978 it->space_width = Qnil;
1979 it->font_height = Qnil;
1981 /* Are control characters displayed as `^C'? */
1982 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1984 /* -1 means everything between a CR and the following line end
1985 is invisible. >0 means lines indented more than this value are
1986 invisible. */
1987 it->selective = (INTEGERP (current_buffer->selective_display)
1988 ? XFASTINT (current_buffer->selective_display)
1989 : (!NILP (current_buffer->selective_display)
1990 ? -1 : 0));
1991 it->selective_display_ellipsis_p
1992 = !NILP (current_buffer->selective_display_ellipses);
1994 /* Display table to use. */
1995 it->dp = window_display_table (w);
1997 /* Are multibyte characters enabled in current_buffer? */
1998 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2000 /* Non-zero if we should highlight the region. */
2001 highlight_region_p
2002 = (!NILP (Vtransient_mark_mode)
2003 && !NILP (current_buffer->mark_active)
2004 && XMARKER (current_buffer->mark)->buffer != 0);
2006 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2007 start and end of a visible region in window IT->w. Set both to
2008 -1 to indicate no region. */
2009 if (highlight_region_p
2010 /* Maybe highlight only in selected window. */
2011 && (/* Either show region everywhere. */
2012 highlight_nonselected_windows
2013 /* Or show region in the selected window. */
2014 || w == XWINDOW (selected_window)
2015 /* Or show the region if we are in the mini-buffer and W is
2016 the window the mini-buffer refers to. */
2017 || (MINI_WINDOW_P (XWINDOW (selected_window))
2018 && WINDOWP (minibuf_selected_window)
2019 && w == XWINDOW (minibuf_selected_window))))
2021 int charpos = marker_position (current_buffer->mark);
2022 it->region_beg_charpos = min (PT, charpos);
2023 it->region_end_charpos = max (PT, charpos);
2025 else
2026 it->region_beg_charpos = it->region_end_charpos = -1;
2028 /* Get the position at which the redisplay_end_trigger hook should
2029 be run, if it is to be run at all. */
2030 if (MARKERP (w->redisplay_end_trigger)
2031 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2032 it->redisplay_end_trigger_charpos
2033 = marker_position (w->redisplay_end_trigger);
2034 else if (INTEGERP (w->redisplay_end_trigger))
2035 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2037 /* Correct bogus values of tab_width. */
2038 it->tab_width = XINT (current_buffer->tab_width);
2039 if (it->tab_width <= 0 || it->tab_width > 1000)
2040 it->tab_width = 8;
2042 /* Are lines in the display truncated? */
2043 it->truncate_lines_p
2044 = (base_face_id != DEFAULT_FACE_ID
2045 || XINT (it->w->hscroll)
2046 || (truncate_partial_width_windows
2047 && !WINDOW_FULL_WIDTH_P (it->w))
2048 || !NILP (current_buffer->truncate_lines));
2050 /* Get dimensions of truncation and continuation glyphs. These are
2051 displayed as fringe bitmaps under X, so we don't need them for such
2052 frames. */
2053 if (!FRAME_WINDOW_P (it->f))
2055 if (it->truncate_lines_p)
2057 /* We will need the truncation glyph. */
2058 xassert (it->glyph_row == NULL);
2059 produce_special_glyphs (it, IT_TRUNCATION);
2060 it->truncation_pixel_width = it->pixel_width;
2062 else
2064 /* We will need the continuation glyph. */
2065 xassert (it->glyph_row == NULL);
2066 produce_special_glyphs (it, IT_CONTINUATION);
2067 it->continuation_pixel_width = it->pixel_width;
2070 /* Reset these values to zero because the produce_special_glyphs
2071 above has changed them. */
2072 it->pixel_width = it->ascent = it->descent = 0;
2073 it->phys_ascent = it->phys_descent = 0;
2076 /* Set this after getting the dimensions of truncation and
2077 continuation glyphs, so that we don't produce glyphs when calling
2078 produce_special_glyphs, above. */
2079 it->glyph_row = row;
2080 it->area = TEXT_AREA;
2082 /* Get the dimensions of the display area. The display area
2083 consists of the visible window area plus a horizontally scrolled
2084 part to the left of the window. All x-values are relative to the
2085 start of this total display area. */
2086 if (base_face_id != DEFAULT_FACE_ID)
2088 /* Mode lines, menu bar in terminal frames. */
2089 it->first_visible_x = 0;
2090 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
2092 else
2094 it->first_visible_x
2095 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
2096 it->last_visible_x = (it->first_visible_x
2097 + window_box_width (w, TEXT_AREA));
2099 /* If we truncate lines, leave room for the truncator glyph(s) at
2100 the right margin. Otherwise, leave room for the continuation
2101 glyph(s). Truncation and continuation glyphs are not inserted
2102 for window-based redisplay. */
2103 if (!FRAME_WINDOW_P (it->f))
2105 if (it->truncate_lines_p)
2106 it->last_visible_x -= it->truncation_pixel_width;
2107 else
2108 it->last_visible_x -= it->continuation_pixel_width;
2111 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2112 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
2115 /* Leave room for a border glyph. */
2116 if (!FRAME_WINDOW_P (it->f)
2117 && !WINDOW_RIGHTMOST_P (it->w))
2118 it->last_visible_x -= 1;
2120 it->last_visible_y = window_text_bottom_y (w);
2122 /* For mode lines and alike, arrange for the first glyph having a
2123 left box line if the face specifies a box. */
2124 if (base_face_id != DEFAULT_FACE_ID)
2126 struct face *face;
2128 it->face_id = base_face_id;
2130 /* If we have a boxed mode line, make the first character appear
2131 with a left box line. */
2132 face = FACE_FROM_ID (it->f, base_face_id);
2133 if (face->box != FACE_NO_BOX)
2134 it->start_of_box_run_p = 1;
2137 /* If a buffer position was specified, set the iterator there,
2138 getting overlays and face properties from that position. */
2139 if (charpos >= BUF_BEG (current_buffer))
2141 it->end_charpos = ZV;
2142 it->face_id = -1;
2143 IT_CHARPOS (*it) = charpos;
2145 /* Compute byte position if not specified. */
2146 if (bytepos < charpos)
2147 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2148 else
2149 IT_BYTEPOS (*it) = bytepos;
2151 /* Compute faces etc. */
2152 reseat (it, it->current.pos, 1);
2155 CHECK_IT (it);
2159 /* Initialize IT for the display of window W with window start POS. */
2161 void
2162 start_display (it, w, pos)
2163 struct it *it;
2164 struct window *w;
2165 struct text_pos pos;
2167 struct glyph_row *row;
2168 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2170 row = w->desired_matrix->rows + first_vpos;
2171 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2173 if (!it->truncate_lines_p)
2175 int start_at_line_beg_p;
2176 int first_y = it->current_y;
2178 /* If window start is not at a line start, skip forward to POS to
2179 get the correct continuation lines width. */
2180 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2181 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2182 if (!start_at_line_beg_p)
2184 int new_x;
2186 reseat_at_previous_visible_line_start (it);
2187 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2189 new_x = it->current_x + it->pixel_width;
2191 /* If lines are continued, this line may end in the middle
2192 of a multi-glyph character (e.g. a control character
2193 displayed as \003, or in the middle of an overlay
2194 string). In this case move_it_to above will not have
2195 taken us to the start of the continuation line but to the
2196 end of the continued line. */
2197 if (it->current_x > 0
2198 && !it->truncate_lines_p /* Lines are continued. */
2199 && (/* And glyph doesn't fit on the line. */
2200 new_x > it->last_visible_x
2201 /* Or it fits exactly and we're on a window
2202 system frame. */
2203 || (new_x == it->last_visible_x
2204 && FRAME_WINDOW_P (it->f))))
2206 if (it->current.dpvec_index >= 0
2207 || it->current.overlay_string_index >= 0)
2209 set_iterator_to_next (it, 1);
2210 move_it_in_display_line_to (it, -1, -1, 0);
2213 it->continuation_lines_width += it->current_x;
2216 /* We're starting a new display line, not affected by the
2217 height of the continued line, so clear the appropriate
2218 fields in the iterator structure. */
2219 it->max_ascent = it->max_descent = 0;
2220 it->max_phys_ascent = it->max_phys_descent = 0;
2222 it->current_y = first_y;
2223 it->vpos = 0;
2224 it->current_x = it->hpos = 0;
2228 #if 0 /* Don't assert the following because start_display is sometimes
2229 called intentionally with a window start that is not at a
2230 line start. Please leave this code in as a comment. */
2232 /* Window start should be on a line start, now. */
2233 xassert (it->continuation_lines_width
2234 || IT_CHARPOS (it) == BEGV
2235 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2236 #endif /* 0 */
2240 /* Return 1 if POS is a position in ellipses displayed for invisible
2241 text. W is the window we display, for text property lookup. */
2243 static int
2244 in_ellipses_for_invisible_text_p (pos, w)
2245 struct display_pos *pos;
2246 struct window *w;
2248 Lisp_Object prop, window;
2249 int ellipses_p = 0;
2250 int charpos = CHARPOS (pos->pos);
2252 /* If POS specifies a position in a display vector, this might
2253 be for an ellipsis displayed for invisible text. We won't
2254 get the iterator set up for delivering that ellipsis unless
2255 we make sure that it gets aware of the invisible text. */
2256 if (pos->dpvec_index >= 0
2257 && pos->overlay_string_index < 0
2258 && CHARPOS (pos->string_pos) < 0
2259 && charpos > BEGV
2260 && (XSETWINDOW (window, w),
2261 prop = Fget_char_property (make_number (charpos),
2262 Qinvisible, window),
2263 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2265 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2266 window);
2267 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2270 return ellipses_p;
2274 /* Initialize IT for stepping through current_buffer in window W,
2275 starting at position POS that includes overlay string and display
2276 vector/ control character translation position information. Value
2277 is zero if there are overlay strings with newlines at POS. */
2279 static int
2280 init_from_display_pos (it, w, pos)
2281 struct it *it;
2282 struct window *w;
2283 struct display_pos *pos;
2285 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2286 int i, overlay_strings_with_newlines = 0;
2288 /* If POS specifies a position in a display vector, this might
2289 be for an ellipsis displayed for invisible text. We won't
2290 get the iterator set up for delivering that ellipsis unless
2291 we make sure that it gets aware of the invisible text. */
2292 if (in_ellipses_for_invisible_text_p (pos, w))
2294 --charpos;
2295 bytepos = 0;
2298 /* Keep in mind: the call to reseat in init_iterator skips invisible
2299 text, so we might end up at a position different from POS. This
2300 is only a problem when POS is a row start after a newline and an
2301 overlay starts there with an after-string, and the overlay has an
2302 invisible property. Since we don't skip invisible text in
2303 display_line and elsewhere immediately after consuming the
2304 newline before the row start, such a POS will not be in a string,
2305 but the call to init_iterator below will move us to the
2306 after-string. */
2307 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2309 for (i = 0; i < it->n_overlay_strings; ++i)
2311 const char *s = SDATA (it->overlay_strings[i]);
2312 const char *e = s + SBYTES (it->overlay_strings[i]);
2314 while (s < e && *s != '\n')
2315 ++s;
2317 if (s < e)
2319 overlay_strings_with_newlines = 1;
2320 break;
2324 /* If position is within an overlay string, set up IT to the right
2325 overlay string. */
2326 if (pos->overlay_string_index >= 0)
2328 int relative_index;
2330 /* If the first overlay string happens to have a `display'
2331 property for an image, the iterator will be set up for that
2332 image, and we have to undo that setup first before we can
2333 correct the overlay string index. */
2334 if (it->method == next_element_from_image)
2335 pop_it (it);
2337 /* We already have the first chunk of overlay strings in
2338 IT->overlay_strings. Load more until the one for
2339 pos->overlay_string_index is in IT->overlay_strings. */
2340 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2342 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2343 it->current.overlay_string_index = 0;
2344 while (n--)
2346 load_overlay_strings (it, 0);
2347 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2351 it->current.overlay_string_index = pos->overlay_string_index;
2352 relative_index = (it->current.overlay_string_index
2353 % OVERLAY_STRING_CHUNK_SIZE);
2354 it->string = it->overlay_strings[relative_index];
2355 xassert (STRINGP (it->string));
2356 it->current.string_pos = pos->string_pos;
2357 it->method = next_element_from_string;
2360 #if 0 /* This is bogus because POS not having an overlay string
2361 position does not mean it's after the string. Example: A
2362 line starting with a before-string and initialization of IT
2363 to the previous row's end position. */
2364 else if (it->current.overlay_string_index >= 0)
2366 /* If POS says we're already after an overlay string ending at
2367 POS, make sure to pop the iterator because it will be in
2368 front of that overlay string. When POS is ZV, we've thereby
2369 also ``processed'' overlay strings at ZV. */
2370 while (it->sp)
2371 pop_it (it);
2372 it->current.overlay_string_index = -1;
2373 it->method = next_element_from_buffer;
2374 if (CHARPOS (pos->pos) == ZV)
2375 it->overlay_strings_at_end_processed_p = 1;
2377 #endif /* 0 */
2379 if (CHARPOS (pos->string_pos) >= 0)
2381 /* Recorded position is not in an overlay string, but in another
2382 string. This can only be a string from a `display' property.
2383 IT should already be filled with that string. */
2384 it->current.string_pos = pos->string_pos;
2385 xassert (STRINGP (it->string));
2388 /* Restore position in display vector translations, control
2389 character translations or ellipses. */
2390 if (pos->dpvec_index >= 0)
2392 if (it->dpvec == NULL)
2393 get_next_display_element (it);
2394 xassert (it->dpvec && it->current.dpvec_index == 0);
2395 it->current.dpvec_index = pos->dpvec_index;
2398 CHECK_IT (it);
2399 return !overlay_strings_with_newlines;
2403 /* Initialize IT for stepping through current_buffer in window W
2404 starting at ROW->start. */
2406 static void
2407 init_to_row_start (it, w, row)
2408 struct it *it;
2409 struct window *w;
2410 struct glyph_row *row;
2412 init_from_display_pos (it, w, &row->start);
2413 it->continuation_lines_width = row->continuation_lines_width;
2414 CHECK_IT (it);
2418 /* Initialize IT for stepping through current_buffer in window W
2419 starting in the line following ROW, i.e. starting at ROW->end.
2420 Value is zero if there are overlay strings with newlines at ROW's
2421 end position. */
2423 static int
2424 init_to_row_end (it, w, row)
2425 struct it *it;
2426 struct window *w;
2427 struct glyph_row *row;
2429 int success = 0;
2431 if (init_from_display_pos (it, w, &row->end))
2433 if (row->continued_p)
2434 it->continuation_lines_width
2435 = row->continuation_lines_width + row->pixel_width;
2436 CHECK_IT (it);
2437 success = 1;
2440 return success;
2446 /***********************************************************************
2447 Text properties
2448 ***********************************************************************/
2450 /* Called when IT reaches IT->stop_charpos. Handle text property and
2451 overlay changes. Set IT->stop_charpos to the next position where
2452 to stop. */
2454 static void
2455 handle_stop (it)
2456 struct it *it;
2458 enum prop_handled handled;
2459 int handle_overlay_change_p = 1;
2460 struct props *p;
2462 it->dpvec = NULL;
2463 it->current.dpvec_index = -1;
2467 handled = HANDLED_NORMALLY;
2469 /* Call text property handlers. */
2470 for (p = it_props; p->handler; ++p)
2472 handled = p->handler (it);
2474 if (handled == HANDLED_RECOMPUTE_PROPS)
2475 break;
2476 else if (handled == HANDLED_RETURN)
2477 return;
2478 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2479 handle_overlay_change_p = 0;
2482 if (handled != HANDLED_RECOMPUTE_PROPS)
2484 /* Don't check for overlay strings below when set to deliver
2485 characters from a display vector. */
2486 if (it->method == next_element_from_display_vector)
2487 handle_overlay_change_p = 0;
2489 /* Handle overlay changes. */
2490 if (handle_overlay_change_p)
2491 handled = handle_overlay_change (it);
2493 /* Determine where to stop next. */
2494 if (handled == HANDLED_NORMALLY)
2495 compute_stop_pos (it);
2498 while (handled == HANDLED_RECOMPUTE_PROPS);
2502 /* Compute IT->stop_charpos from text property and overlay change
2503 information for IT's current position. */
2505 static void
2506 compute_stop_pos (it)
2507 struct it *it;
2509 register INTERVAL iv, next_iv;
2510 Lisp_Object object, limit, position;
2512 /* If nowhere else, stop at the end. */
2513 it->stop_charpos = it->end_charpos;
2515 if (STRINGP (it->string))
2517 /* Strings are usually short, so don't limit the search for
2518 properties. */
2519 object = it->string;
2520 limit = Qnil;
2521 position = make_number (IT_STRING_CHARPOS (*it));
2523 else
2525 int charpos;
2527 /* If next overlay change is in front of the current stop pos
2528 (which is IT->end_charpos), stop there. Note: value of
2529 next_overlay_change is point-max if no overlay change
2530 follows. */
2531 charpos = next_overlay_change (IT_CHARPOS (*it));
2532 if (charpos < it->stop_charpos)
2533 it->stop_charpos = charpos;
2535 /* If showing the region, we have to stop at the region
2536 start or end because the face might change there. */
2537 if (it->region_beg_charpos > 0)
2539 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2540 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2541 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2542 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2545 /* Set up variables for computing the stop position from text
2546 property changes. */
2547 XSETBUFFER (object, current_buffer);
2548 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2549 position = make_number (IT_CHARPOS (*it));
2553 /* Get the interval containing IT's position. Value is a null
2554 interval if there isn't such an interval. */
2555 iv = validate_interval_range (object, &position, &position, 0);
2556 if (!NULL_INTERVAL_P (iv))
2558 Lisp_Object values_here[LAST_PROP_IDX];
2559 struct props *p;
2561 /* Get properties here. */
2562 for (p = it_props; p->handler; ++p)
2563 values_here[p->idx] = textget (iv->plist, *p->name);
2565 /* Look for an interval following iv that has different
2566 properties. */
2567 for (next_iv = next_interval (iv);
2568 (!NULL_INTERVAL_P (next_iv)
2569 && (NILP (limit)
2570 || XFASTINT (limit) > next_iv->position));
2571 next_iv = next_interval (next_iv))
2573 for (p = it_props; p->handler; ++p)
2575 Lisp_Object new_value;
2577 new_value = textget (next_iv->plist, *p->name);
2578 if (!EQ (values_here[p->idx], new_value))
2579 break;
2582 if (p->handler)
2583 break;
2586 if (!NULL_INTERVAL_P (next_iv))
2588 if (INTEGERP (limit)
2589 && next_iv->position >= XFASTINT (limit))
2590 /* No text property change up to limit. */
2591 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2592 else
2593 /* Text properties change in next_iv. */
2594 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2598 xassert (STRINGP (it->string)
2599 || (it->stop_charpos >= BEGV
2600 && it->stop_charpos >= IT_CHARPOS (*it)));
2604 /* Return the position of the next overlay change after POS in
2605 current_buffer. Value is point-max if no overlay change
2606 follows. This is like `next-overlay-change' but doesn't use
2607 xmalloc. */
2609 static int
2610 next_overlay_change (pos)
2611 int pos;
2613 int noverlays;
2614 int endpos;
2615 Lisp_Object *overlays;
2616 int len;
2617 int i;
2619 /* Get all overlays at the given position. */
2620 len = 10;
2621 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2622 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2623 if (noverlays > len)
2625 len = noverlays;
2626 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2627 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2630 /* If any of these overlays ends before endpos,
2631 use its ending point instead. */
2632 for (i = 0; i < noverlays; ++i)
2634 Lisp_Object oend;
2635 int oendpos;
2637 oend = OVERLAY_END (overlays[i]);
2638 oendpos = OVERLAY_POSITION (oend);
2639 endpos = min (endpos, oendpos);
2642 return endpos;
2647 /***********************************************************************
2648 Fontification
2649 ***********************************************************************/
2651 /* Handle changes in the `fontified' property of the current buffer by
2652 calling hook functions from Qfontification_functions to fontify
2653 regions of text. */
2655 static enum prop_handled
2656 handle_fontified_prop (it)
2657 struct it *it;
2659 Lisp_Object prop, pos;
2660 enum prop_handled handled = HANDLED_NORMALLY;
2662 /* Get the value of the `fontified' property at IT's current buffer
2663 position. (The `fontified' property doesn't have a special
2664 meaning in strings.) If the value is nil, call functions from
2665 Qfontification_functions. */
2666 if (!STRINGP (it->string)
2667 && it->s == NULL
2668 && !NILP (Vfontification_functions)
2669 && !NILP (Vrun_hooks)
2670 && (pos = make_number (IT_CHARPOS (*it)),
2671 prop = Fget_char_property (pos, Qfontified, Qnil),
2672 NILP (prop)))
2674 int count = SPECPDL_INDEX ();
2675 Lisp_Object val;
2677 val = Vfontification_functions;
2678 specbind (Qfontification_functions, Qnil);
2680 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2681 safe_call1 (val, pos);
2682 else
2684 Lisp_Object globals, fn;
2685 struct gcpro gcpro1, gcpro2;
2687 globals = Qnil;
2688 GCPRO2 (val, globals);
2690 for (; CONSP (val); val = XCDR (val))
2692 fn = XCAR (val);
2694 if (EQ (fn, Qt))
2696 /* A value of t indicates this hook has a local
2697 binding; it means to run the global binding too.
2698 In a global value, t should not occur. If it
2699 does, we must ignore it to avoid an endless
2700 loop. */
2701 for (globals = Fdefault_value (Qfontification_functions);
2702 CONSP (globals);
2703 globals = XCDR (globals))
2705 fn = XCAR (globals);
2706 if (!EQ (fn, Qt))
2707 safe_call1 (fn, pos);
2710 else
2711 safe_call1 (fn, pos);
2714 UNGCPRO;
2717 unbind_to (count, Qnil);
2719 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2720 something. This avoids an endless loop if they failed to
2721 fontify the text for which reason ever. */
2722 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2723 handled = HANDLED_RECOMPUTE_PROPS;
2726 return handled;
2731 /***********************************************************************
2732 Faces
2733 ***********************************************************************/
2735 /* Set up iterator IT from face properties at its current position.
2736 Called from handle_stop. */
2738 static enum prop_handled
2739 handle_face_prop (it)
2740 struct it *it;
2742 int new_face_id, next_stop;
2744 if (!STRINGP (it->string))
2746 new_face_id
2747 = face_at_buffer_position (it->w,
2748 IT_CHARPOS (*it),
2749 it->region_beg_charpos,
2750 it->region_end_charpos,
2751 &next_stop,
2752 (IT_CHARPOS (*it)
2753 + TEXT_PROP_DISTANCE_LIMIT),
2756 /* Is this a start of a run of characters with box face?
2757 Caveat: this can be called for a freshly initialized
2758 iterator; face_id is -1 in this case. We know that the new
2759 face will not change until limit, i.e. if the new face has a
2760 box, all characters up to limit will have one. But, as
2761 usual, we don't know whether limit is really the end. */
2762 if (new_face_id != it->face_id)
2764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2766 /* If new face has a box but old face has not, this is
2767 the start of a run of characters with box, i.e. it has
2768 a shadow on the left side. The value of face_id of the
2769 iterator will be -1 if this is the initial call that gets
2770 the face. In this case, we have to look in front of IT's
2771 position and see whether there is a face != new_face_id. */
2772 it->start_of_box_run_p
2773 = (new_face->box != FACE_NO_BOX
2774 && (it->face_id >= 0
2775 || IT_CHARPOS (*it) == BEG
2776 || new_face_id != face_before_it_pos (it)));
2777 it->face_box_p = new_face->box != FACE_NO_BOX;
2780 else
2782 int base_face_id, bufpos;
2784 if (it->current.overlay_string_index >= 0)
2785 bufpos = IT_CHARPOS (*it);
2786 else
2787 bufpos = 0;
2789 /* For strings from a buffer, i.e. overlay strings or strings
2790 from a `display' property, use the face at IT's current
2791 buffer position as the base face to merge with, so that
2792 overlay strings appear in the same face as surrounding
2793 text, unless they specify their own faces. */
2794 base_face_id = underlying_face_id (it);
2796 new_face_id = face_at_string_position (it->w,
2797 it->string,
2798 IT_STRING_CHARPOS (*it),
2799 bufpos,
2800 it->region_beg_charpos,
2801 it->region_end_charpos,
2802 &next_stop,
2803 base_face_id, 0);
2805 #if 0 /* This shouldn't be neccessary. Let's check it. */
2806 /* If IT is used to display a mode line we would really like to
2807 use the mode line face instead of the frame's default face. */
2808 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2809 && new_face_id == DEFAULT_FACE_ID)
2810 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2811 #endif
2813 /* Is this a start of a run of characters with box? Caveat:
2814 this can be called for a freshly allocated iterator; face_id
2815 is -1 is this case. We know that the new face will not
2816 change until the next check pos, i.e. if the new face has a
2817 box, all characters up to that position will have a
2818 box. But, as usual, we don't know whether that position
2819 is really the end. */
2820 if (new_face_id != it->face_id)
2822 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2823 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2825 /* If new face has a box but old face hasn't, this is the
2826 start of a run of characters with box, i.e. it has a
2827 shadow on the left side. */
2828 it->start_of_box_run_p
2829 = new_face->box && (old_face == NULL || !old_face->box);
2830 it->face_box_p = new_face->box != FACE_NO_BOX;
2834 it->face_id = new_face_id;
2835 return HANDLED_NORMALLY;
2839 /* Return the ID of the face ``underlying'' IT's current position,
2840 which is in a string. If the iterator is associated with a
2841 buffer, return the face at IT's current buffer position.
2842 Otherwise, use the iterator's base_face_id. */
2844 static int
2845 underlying_face_id (it)
2846 struct it *it;
2848 int face_id = it->base_face_id, i;
2850 xassert (STRINGP (it->string));
2852 for (i = it->sp - 1; i >= 0; --i)
2853 if (NILP (it->stack[i].string))
2854 face_id = it->stack[i].face_id;
2856 return face_id;
2860 /* Compute the face one character before or after the current position
2861 of IT. BEFORE_P non-zero means get the face in front of IT's
2862 position. Value is the id of the face. */
2864 static int
2865 face_before_or_after_it_pos (it, before_p)
2866 struct it *it;
2867 int before_p;
2869 int face_id, limit;
2870 int next_check_charpos;
2871 struct text_pos pos;
2873 xassert (it->s == NULL);
2875 if (STRINGP (it->string))
2877 int bufpos, base_face_id;
2879 /* No face change past the end of the string (for the case
2880 we are padding with spaces). No face change before the
2881 string start. */
2882 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2883 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2884 return it->face_id;
2886 /* Set pos to the position before or after IT's current position. */
2887 if (before_p)
2888 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2889 else
2890 /* For composition, we must check the character after the
2891 composition. */
2892 pos = (it->what == IT_COMPOSITION
2893 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2894 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2896 if (it->current.overlay_string_index >= 0)
2897 bufpos = IT_CHARPOS (*it);
2898 else
2899 bufpos = 0;
2901 base_face_id = underlying_face_id (it);
2903 /* Get the face for ASCII, or unibyte. */
2904 face_id = face_at_string_position (it->w,
2905 it->string,
2906 CHARPOS (pos),
2907 bufpos,
2908 it->region_beg_charpos,
2909 it->region_end_charpos,
2910 &next_check_charpos,
2911 base_face_id, 0);
2913 /* Correct the face for charsets different from ASCII. Do it
2914 for the multibyte case only. The face returned above is
2915 suitable for unibyte text if IT->string is unibyte. */
2916 if (STRING_MULTIBYTE (it->string))
2918 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2919 int rest = SBYTES (it->string) - BYTEPOS (pos);
2920 int c, len;
2921 struct face *face = FACE_FROM_ID (it->f, face_id);
2923 c = string_char_and_length (p, rest, &len);
2924 face_id = FACE_FOR_CHAR (it->f, face, c);
2927 else
2929 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2930 || (IT_CHARPOS (*it) <= BEGV && before_p))
2931 return it->face_id;
2933 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2934 pos = it->current.pos;
2936 if (before_p)
2937 DEC_TEXT_POS (pos, it->multibyte_p);
2938 else
2940 if (it->what == IT_COMPOSITION)
2941 /* For composition, we must check the position after the
2942 composition. */
2943 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2944 else
2945 INC_TEXT_POS (pos, it->multibyte_p);
2948 /* Determine face for CHARSET_ASCII, or unibyte. */
2949 face_id = face_at_buffer_position (it->w,
2950 CHARPOS (pos),
2951 it->region_beg_charpos,
2952 it->region_end_charpos,
2953 &next_check_charpos,
2954 limit, 0);
2956 /* Correct the face for charsets different from ASCII. Do it
2957 for the multibyte case only. The face returned above is
2958 suitable for unibyte text if current_buffer is unibyte. */
2959 if (it->multibyte_p)
2961 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2962 struct face *face = FACE_FROM_ID (it->f, face_id);
2963 face_id = FACE_FOR_CHAR (it->f, face, c);
2967 return face_id;
2972 /***********************************************************************
2973 Invisible text
2974 ***********************************************************************/
2976 /* Set up iterator IT from invisible properties at its current
2977 position. Called from handle_stop. */
2979 static enum prop_handled
2980 handle_invisible_prop (it)
2981 struct it *it;
2983 enum prop_handled handled = HANDLED_NORMALLY;
2985 if (STRINGP (it->string))
2987 extern Lisp_Object Qinvisible;
2988 Lisp_Object prop, end_charpos, limit, charpos;
2990 /* Get the value of the invisible text property at the
2991 current position. Value will be nil if there is no such
2992 property. */
2993 charpos = make_number (IT_STRING_CHARPOS (*it));
2994 prop = Fget_text_property (charpos, Qinvisible, it->string);
2996 if (!NILP (prop)
2997 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2999 handled = HANDLED_RECOMPUTE_PROPS;
3001 /* Get the position at which the next change of the
3002 invisible text property can be found in IT->string.
3003 Value will be nil if the property value is the same for
3004 all the rest of IT->string. */
3005 XSETINT (limit, SCHARS (it->string));
3006 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3007 it->string, limit);
3009 /* Text at current position is invisible. The next
3010 change in the property is at position end_charpos.
3011 Move IT's current position to that position. */
3012 if (INTEGERP (end_charpos)
3013 && XFASTINT (end_charpos) < XFASTINT (limit))
3015 struct text_pos old;
3016 old = it->current.string_pos;
3017 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3018 compute_string_pos (&it->current.string_pos, old, it->string);
3020 else
3022 /* The rest of the string is invisible. If this is an
3023 overlay string, proceed with the next overlay string
3024 or whatever comes and return a character from there. */
3025 if (it->current.overlay_string_index >= 0)
3027 next_overlay_string (it);
3028 /* Don't check for overlay strings when we just
3029 finished processing them. */
3030 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3032 else
3034 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3035 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3040 else
3042 int invis_p, newpos, next_stop, start_charpos;
3043 Lisp_Object pos, prop, overlay;
3045 /* First of all, is there invisible text at this position? */
3046 start_charpos = IT_CHARPOS (*it);
3047 pos = make_number (IT_CHARPOS (*it));
3048 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3049 &overlay);
3050 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3052 /* If we are on invisible text, skip over it. */
3053 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3055 /* Record whether we have to display an ellipsis for the
3056 invisible text. */
3057 int display_ellipsis_p = invis_p == 2;
3059 handled = HANDLED_RECOMPUTE_PROPS;
3061 /* Loop skipping over invisible text. The loop is left at
3062 ZV or with IT on the first char being visible again. */
3065 /* Try to skip some invisible text. Return value is the
3066 position reached which can be equal to IT's position
3067 if there is nothing invisible here. This skips both
3068 over invisible text properties and overlays with
3069 invisible property. */
3070 newpos = skip_invisible (IT_CHARPOS (*it),
3071 &next_stop, ZV, it->window);
3073 /* If we skipped nothing at all we weren't at invisible
3074 text in the first place. If everything to the end of
3075 the buffer was skipped, end the loop. */
3076 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3077 invis_p = 0;
3078 else
3080 /* We skipped some characters but not necessarily
3081 all there are. Check if we ended up on visible
3082 text. Fget_char_property returns the property of
3083 the char before the given position, i.e. if we
3084 get invis_p = 0, this means that the char at
3085 newpos is visible. */
3086 pos = make_number (newpos);
3087 prop = Fget_char_property (pos, Qinvisible, it->window);
3088 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3091 /* If we ended up on invisible text, proceed to
3092 skip starting with next_stop. */
3093 if (invis_p)
3094 IT_CHARPOS (*it) = next_stop;
3096 while (invis_p);
3098 /* The position newpos is now either ZV or on visible text. */
3099 IT_CHARPOS (*it) = newpos;
3100 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3102 /* If there are before-strings at the start of invisible
3103 text, and the text is invisible because of a text
3104 property, arrange to show before-strings because 20.x did
3105 it that way. (If the text is invisible because of an
3106 overlay property instead of a text property, this is
3107 already handled in the overlay code.) */
3108 if (NILP (overlay)
3109 && get_overlay_strings (it, start_charpos))
3111 handled = HANDLED_RECOMPUTE_PROPS;
3112 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3114 else if (display_ellipsis_p)
3115 setup_for_ellipsis (it);
3119 return handled;
3123 /* Make iterator IT return `...' next. */
3125 static void
3126 setup_for_ellipsis (it)
3127 struct it *it;
3129 if (it->dp
3130 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3132 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3133 it->dpvec = v->contents;
3134 it->dpend = v->contents + v->size;
3136 else
3138 /* Default `...'. */
3139 it->dpvec = default_invis_vector;
3140 it->dpend = default_invis_vector + 3;
3143 /* The ellipsis display does not replace the display of the
3144 character at the new position. Indicate this by setting
3145 IT->dpvec_char_len to zero. */
3146 it->dpvec_char_len = 0;
3148 it->current.dpvec_index = 0;
3149 it->method = next_element_from_display_vector;
3154 /***********************************************************************
3155 'display' property
3156 ***********************************************************************/
3158 /* Set up iterator IT from `display' property at its current position.
3159 Called from handle_stop. */
3161 static enum prop_handled
3162 handle_display_prop (it)
3163 struct it *it;
3165 Lisp_Object prop, object;
3166 struct text_pos *position;
3167 int display_replaced_p = 0;
3169 if (STRINGP (it->string))
3171 object = it->string;
3172 position = &it->current.string_pos;
3174 else
3176 object = it->w->buffer;
3177 position = &it->current.pos;
3180 /* Reset those iterator values set from display property values. */
3181 it->font_height = Qnil;
3182 it->space_width = Qnil;
3183 it->voffset = 0;
3185 /* We don't support recursive `display' properties, i.e. string
3186 values that have a string `display' property, that have a string
3187 `display' property etc. */
3188 if (!it->string_from_display_prop_p)
3189 it->area = TEXT_AREA;
3191 prop = Fget_char_property (make_number (position->charpos),
3192 Qdisplay, object);
3193 if (NILP (prop))
3194 return HANDLED_NORMALLY;
3196 if (CONSP (prop)
3197 /* Simple properties. */
3198 && !EQ (XCAR (prop), Qimage)
3199 && !EQ (XCAR (prop), Qspace)
3200 && !EQ (XCAR (prop), Qwhen)
3201 && !EQ (XCAR (prop), Qspace_width)
3202 && !EQ (XCAR (prop), Qheight)
3203 && !EQ (XCAR (prop), Qraise)
3204 /* Marginal area specifications. */
3205 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3206 && !NILP (XCAR (prop)))
3208 for (; CONSP (prop); prop = XCDR (prop))
3210 if (handle_single_display_prop (it, XCAR (prop), object,
3211 position, display_replaced_p))
3212 display_replaced_p = 1;
3215 else if (VECTORP (prop))
3217 int i;
3218 for (i = 0; i < ASIZE (prop); ++i)
3219 if (handle_single_display_prop (it, AREF (prop, i), object,
3220 position, display_replaced_p))
3221 display_replaced_p = 1;
3223 else
3225 if (handle_single_display_prop (it, prop, object, position, 0))
3226 display_replaced_p = 1;
3229 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3233 /* Value is the position of the end of the `display' property starting
3234 at START_POS in OBJECT. */
3236 static struct text_pos
3237 display_prop_end (it, object, start_pos)
3238 struct it *it;
3239 Lisp_Object object;
3240 struct text_pos start_pos;
3242 Lisp_Object end;
3243 struct text_pos end_pos;
3245 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3246 Qdisplay, object, Qnil);
3247 CHARPOS (end_pos) = XFASTINT (end);
3248 if (STRINGP (object))
3249 compute_string_pos (&end_pos, start_pos, it->string);
3250 else
3251 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3253 return end_pos;
3257 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3258 is the object in which the `display' property was found. *POSITION
3259 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3260 means that we previously saw a display sub-property which already
3261 replaced text display with something else, for example an image;
3262 ignore such properties after the first one has been processed.
3264 If PROP is a `space' or `image' sub-property, set *POSITION to the
3265 end position of the `display' property.
3267 Value is non-zero if something was found which replaces the display
3268 of buffer or string text. */
3270 static int
3271 handle_single_display_prop (it, prop, object, position,
3272 display_replaced_before_p)
3273 struct it *it;
3274 Lisp_Object prop;
3275 Lisp_Object object;
3276 struct text_pos *position;
3277 int display_replaced_before_p;
3279 Lisp_Object value;
3280 int replaces_text_display_p = 0;
3281 Lisp_Object form;
3283 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3284 evaluated. If the result is nil, VALUE is ignored. */
3285 form = Qt;
3286 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3288 prop = XCDR (prop);
3289 if (!CONSP (prop))
3290 return 0;
3291 form = XCAR (prop);
3292 prop = XCDR (prop);
3295 if (!NILP (form) && !EQ (form, Qt))
3297 int count = SPECPDL_INDEX ();
3298 struct gcpro gcpro1;
3300 /* Bind `object' to the object having the `display' property, a
3301 buffer or string. Bind `position' to the position in the
3302 object where the property was found, and `buffer-position'
3303 to the current position in the buffer. */
3304 specbind (Qobject, object);
3305 specbind (Qposition, make_number (CHARPOS (*position)));
3306 specbind (Qbuffer_position,
3307 make_number (STRINGP (object)
3308 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3309 GCPRO1 (form);
3310 form = safe_eval (form);
3311 UNGCPRO;
3312 unbind_to (count, Qnil);
3315 if (NILP (form))
3316 return 0;
3318 if (CONSP (prop)
3319 && EQ (XCAR (prop), Qheight)
3320 && CONSP (XCDR (prop)))
3322 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3323 return 0;
3325 /* `(height HEIGHT)'. */
3326 it->font_height = XCAR (XCDR (prop));
3327 if (!NILP (it->font_height))
3329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3330 int new_height = -1;
3332 if (CONSP (it->font_height)
3333 && (EQ (XCAR (it->font_height), Qplus)
3334 || EQ (XCAR (it->font_height), Qminus))
3335 && CONSP (XCDR (it->font_height))
3336 && INTEGERP (XCAR (XCDR (it->font_height))))
3338 /* `(+ N)' or `(- N)' where N is an integer. */
3339 int steps = XINT (XCAR (XCDR (it->font_height)));
3340 if (EQ (XCAR (it->font_height), Qplus))
3341 steps = - steps;
3342 it->face_id = smaller_face (it->f, it->face_id, steps);
3344 else if (FUNCTIONP (it->font_height))
3346 /* Call function with current height as argument.
3347 Value is the new height. */
3348 Lisp_Object height;
3349 height = safe_call1 (it->font_height,
3350 face->lface[LFACE_HEIGHT_INDEX]);
3351 if (NUMBERP (height))
3352 new_height = XFLOATINT (height);
3354 else if (NUMBERP (it->font_height))
3356 /* Value is a multiple of the canonical char height. */
3357 struct face *face;
3359 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3360 new_height = (XFLOATINT (it->font_height)
3361 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3363 else
3365 /* Evaluate IT->font_height with `height' bound to the
3366 current specified height to get the new height. */
3367 Lisp_Object value;
3368 int count = SPECPDL_INDEX ();
3370 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3371 value = safe_eval (it->font_height);
3372 unbind_to (count, Qnil);
3374 if (NUMBERP (value))
3375 new_height = XFLOATINT (value);
3378 if (new_height > 0)
3379 it->face_id = face_with_height (it->f, it->face_id, new_height);
3382 else if (CONSP (prop)
3383 && EQ (XCAR (prop), Qspace_width)
3384 && CONSP (XCDR (prop)))
3386 /* `(space_width WIDTH)'. */
3387 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3388 return 0;
3390 value = XCAR (XCDR (prop));
3391 if (NUMBERP (value) && XFLOATINT (value) > 0)
3392 it->space_width = value;
3394 else if (CONSP (prop)
3395 && EQ (XCAR (prop), Qraise)
3396 && CONSP (XCDR (prop)))
3398 /* `(raise FACTOR)'. */
3399 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3400 return 0;
3402 #ifdef HAVE_WINDOW_SYSTEM
3403 value = XCAR (XCDR (prop));
3404 if (NUMBERP (value))
3406 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3407 it->voffset = - (XFLOATINT (value)
3408 * (FONT_HEIGHT (face->font)));
3410 #endif /* HAVE_WINDOW_SYSTEM */
3412 else if (!it->string_from_display_prop_p)
3414 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3415 VALUE) or `((margin nil) VALUE)' or VALUE. */
3416 Lisp_Object location, value;
3417 struct text_pos start_pos;
3418 int valid_p;
3420 /* Characters having this form of property are not displayed, so
3421 we have to find the end of the property. */
3422 start_pos = *position;
3423 *position = display_prop_end (it, object, start_pos);
3424 value = Qnil;
3426 /* Let's stop at the new position and assume that all
3427 text properties change there. */
3428 it->stop_charpos = position->charpos;
3430 location = Qunbound;
3431 if (CONSP (prop) && CONSP (XCAR (prop)))
3433 Lisp_Object tem;
3435 value = XCDR (prop);
3436 if (CONSP (value))
3437 value = XCAR (value);
3439 tem = XCAR (prop);
3440 if (EQ (XCAR (tem), Qmargin)
3441 && (tem = XCDR (tem),
3442 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3443 (NILP (tem)
3444 || EQ (tem, Qleft_margin)
3445 || EQ (tem, Qright_margin))))
3446 location = tem;
3449 if (EQ (location, Qunbound))
3451 location = Qnil;
3452 value = prop;
3455 #ifdef HAVE_WINDOW_SYSTEM
3456 if (FRAME_TERMCAP_P (it->f))
3457 valid_p = STRINGP (value);
3458 else
3459 valid_p = (STRINGP (value)
3460 || (CONSP (value) && EQ (XCAR (value), Qspace))
3461 || valid_image_p (value));
3462 #else /* not HAVE_WINDOW_SYSTEM */
3463 valid_p = STRINGP (value);
3464 #endif /* not HAVE_WINDOW_SYSTEM */
3466 if ((EQ (location, Qleft_margin)
3467 || EQ (location, Qright_margin)
3468 || NILP (location))
3469 && valid_p
3470 && !display_replaced_before_p)
3472 replaces_text_display_p = 1;
3474 /* Save current settings of IT so that we can restore them
3475 when we are finished with the glyph property value. */
3476 push_it (it);
3478 if (NILP (location))
3479 it->area = TEXT_AREA;
3480 else if (EQ (location, Qleft_margin))
3481 it->area = LEFT_MARGIN_AREA;
3482 else
3483 it->area = RIGHT_MARGIN_AREA;
3485 if (STRINGP (value))
3487 it->string = value;
3488 it->multibyte_p = STRING_MULTIBYTE (it->string);
3489 it->current.overlay_string_index = -1;
3490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3491 it->end_charpos = it->string_nchars = SCHARS (it->string);
3492 it->method = next_element_from_string;
3493 it->stop_charpos = 0;
3494 it->string_from_display_prop_p = 1;
3495 /* Say that we haven't consumed the characters with
3496 `display' property yet. The call to pop_it in
3497 set_iterator_to_next will clean this up. */
3498 *position = start_pos;
3500 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3502 it->method = next_element_from_stretch;
3503 it->object = value;
3504 it->current.pos = it->position = start_pos;
3506 #ifdef HAVE_WINDOW_SYSTEM
3507 else
3509 it->what = IT_IMAGE;
3510 it->image_id = lookup_image (it->f, value);
3511 it->position = start_pos;
3512 it->object = NILP (object) ? it->w->buffer : object;
3513 it->method = next_element_from_image;
3515 /* Say that we haven't consumed the characters with
3516 `display' property yet. The call to pop_it in
3517 set_iterator_to_next will clean this up. */
3518 *position = start_pos;
3520 #endif /* HAVE_WINDOW_SYSTEM */
3522 else
3523 /* Invalid property or property not supported. Restore
3524 the position to what it was before. */
3525 *position = start_pos;
3528 return replaces_text_display_p;
3532 /* Check if PROP is a display sub-property value whose text should be
3533 treated as intangible. */
3535 static int
3536 single_display_prop_intangible_p (prop)
3537 Lisp_Object prop;
3539 /* Skip over `when FORM'. */
3540 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3542 prop = XCDR (prop);
3543 if (!CONSP (prop))
3544 return 0;
3545 prop = XCDR (prop);
3548 if (STRINGP (prop))
3549 return 1;
3551 if (!CONSP (prop))
3552 return 0;
3554 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3555 we don't need to treat text as intangible. */
3556 if (EQ (XCAR (prop), Qmargin))
3558 prop = XCDR (prop);
3559 if (!CONSP (prop))
3560 return 0;
3562 prop = XCDR (prop);
3563 if (!CONSP (prop)
3564 || EQ (XCAR (prop), Qleft_margin)
3565 || EQ (XCAR (prop), Qright_margin))
3566 return 0;
3569 return (CONSP (prop)
3570 && (EQ (XCAR (prop), Qimage)
3571 || EQ (XCAR (prop), Qspace)));
3575 /* Check if PROP is a display property value whose text should be
3576 treated as intangible. */
3579 display_prop_intangible_p (prop)
3580 Lisp_Object prop;
3582 if (CONSP (prop)
3583 && CONSP (XCAR (prop))
3584 && !EQ (Qmargin, XCAR (XCAR (prop))))
3586 /* A list of sub-properties. */
3587 while (CONSP (prop))
3589 if (single_display_prop_intangible_p (XCAR (prop)))
3590 return 1;
3591 prop = XCDR (prop);
3594 else if (VECTORP (prop))
3596 /* A vector of sub-properties. */
3597 int i;
3598 for (i = 0; i < ASIZE (prop); ++i)
3599 if (single_display_prop_intangible_p (AREF (prop, i)))
3600 return 1;
3602 else
3603 return single_display_prop_intangible_p (prop);
3605 return 0;
3609 /* Return 1 if PROP is a display sub-property value containing STRING. */
3611 static int
3612 single_display_prop_string_p (prop, string)
3613 Lisp_Object prop, string;
3615 if (EQ (string, prop))
3616 return 1;
3618 /* Skip over `when FORM'. */
3619 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3621 prop = XCDR (prop);
3622 if (!CONSP (prop))
3623 return 0;
3624 prop = XCDR (prop);
3627 if (CONSP (prop))
3628 /* Skip over `margin LOCATION'. */
3629 if (EQ (XCAR (prop), Qmargin))
3631 prop = XCDR (prop);
3632 if (!CONSP (prop))
3633 return 0;
3635 prop = XCDR (prop);
3636 if (!CONSP (prop))
3637 return 0;
3640 return CONSP (prop) && EQ (XCAR (prop), string);
3644 /* Return 1 if STRING appears in the `display' property PROP. */
3646 static int
3647 display_prop_string_p (prop, string)
3648 Lisp_Object prop, string;
3650 if (CONSP (prop)
3651 && CONSP (XCAR (prop))
3652 && !EQ (Qmargin, XCAR (XCAR (prop))))
3654 /* A list of sub-properties. */
3655 while (CONSP (prop))
3657 if (single_display_prop_string_p (XCAR (prop), string))
3658 return 1;
3659 prop = XCDR (prop);
3662 else if (VECTORP (prop))
3664 /* A vector of sub-properties. */
3665 int i;
3666 for (i = 0; i < ASIZE (prop); ++i)
3667 if (single_display_prop_string_p (AREF (prop, i), string))
3668 return 1;
3670 else
3671 return single_display_prop_string_p (prop, string);
3673 return 0;
3677 /* Determine from which buffer position in W's buffer STRING comes
3678 from. AROUND_CHARPOS is an approximate position where it could
3679 be from. Value is the buffer position or 0 if it couldn't be
3680 determined.
3682 W's buffer must be current.
3684 This function is necessary because we don't record buffer positions
3685 in glyphs generated from strings (to keep struct glyph small).
3686 This function may only use code that doesn't eval because it is
3687 called asynchronously from note_mouse_highlight. */
3690 string_buffer_position (w, string, around_charpos)
3691 struct window *w;
3692 Lisp_Object string;
3693 int around_charpos;
3695 Lisp_Object limit, prop, pos;
3696 const int MAX_DISTANCE = 1000;
3697 int found = 0;
3699 pos = make_number (around_charpos);
3700 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3701 while (!found && !EQ (pos, limit))
3703 prop = Fget_char_property (pos, Qdisplay, Qnil);
3704 if (!NILP (prop) && display_prop_string_p (prop, string))
3705 found = 1;
3706 else
3707 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3710 if (!found)
3712 pos = make_number (around_charpos);
3713 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3714 while (!found && !EQ (pos, limit))
3716 prop = Fget_char_property (pos, Qdisplay, Qnil);
3717 if (!NILP (prop) && display_prop_string_p (prop, string))
3718 found = 1;
3719 else
3720 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3721 limit);
3725 return found ? XINT (pos) : 0;
3730 /***********************************************************************
3731 `composition' property
3732 ***********************************************************************/
3734 /* Set up iterator IT from `composition' property at its current
3735 position. Called from handle_stop. */
3737 static enum prop_handled
3738 handle_composition_prop (it)
3739 struct it *it;
3741 Lisp_Object prop, string;
3742 int pos, pos_byte, end;
3743 enum prop_handled handled = HANDLED_NORMALLY;
3745 if (STRINGP (it->string))
3747 pos = IT_STRING_CHARPOS (*it);
3748 pos_byte = IT_STRING_BYTEPOS (*it);
3749 string = it->string;
3751 else
3753 pos = IT_CHARPOS (*it);
3754 pos_byte = IT_BYTEPOS (*it);
3755 string = Qnil;
3758 /* If there's a valid composition and point is not inside of the
3759 composition (in the case that the composition is from the current
3760 buffer), draw a glyph composed from the composition components. */
3761 if (find_composition (pos, -1, &pos, &end, &prop, string)
3762 && COMPOSITION_VALID_P (pos, end, prop)
3763 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3765 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3767 if (id >= 0)
3769 it->method = next_element_from_composition;
3770 it->cmp_id = id;
3771 it->cmp_len = COMPOSITION_LENGTH (prop);
3772 /* For a terminal, draw only the first character of the
3773 components. */
3774 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3775 it->len = (STRINGP (it->string)
3776 ? string_char_to_byte (it->string, end)
3777 : CHAR_TO_BYTE (end)) - pos_byte;
3778 it->stop_charpos = end;
3779 handled = HANDLED_RETURN;
3783 return handled;
3788 /***********************************************************************
3789 Overlay strings
3790 ***********************************************************************/
3792 /* The following structure is used to record overlay strings for
3793 later sorting in load_overlay_strings. */
3795 struct overlay_entry
3797 Lisp_Object overlay;
3798 Lisp_Object string;
3799 int priority;
3800 int after_string_p;
3804 /* Set up iterator IT from overlay strings at its current position.
3805 Called from handle_stop. */
3807 static enum prop_handled
3808 handle_overlay_change (it)
3809 struct it *it;
3811 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3812 return HANDLED_RECOMPUTE_PROPS;
3813 else
3814 return HANDLED_NORMALLY;
3818 /* Set up the next overlay string for delivery by IT, if there is an
3819 overlay string to deliver. Called by set_iterator_to_next when the
3820 end of the current overlay string is reached. If there are more
3821 overlay strings to display, IT->string and
3822 IT->current.overlay_string_index are set appropriately here.
3823 Otherwise IT->string is set to nil. */
3825 static void
3826 next_overlay_string (it)
3827 struct it *it;
3829 ++it->current.overlay_string_index;
3830 if (it->current.overlay_string_index == it->n_overlay_strings)
3832 /* No more overlay strings. Restore IT's settings to what
3833 they were before overlay strings were processed, and
3834 continue to deliver from current_buffer. */
3835 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3837 pop_it (it);
3838 xassert (it->stop_charpos >= BEGV
3839 && it->stop_charpos <= it->end_charpos);
3840 it->string = Qnil;
3841 it->current.overlay_string_index = -1;
3842 SET_TEXT_POS (it->current.string_pos, -1, -1);
3843 it->n_overlay_strings = 0;
3844 it->method = next_element_from_buffer;
3846 /* If we're at the end of the buffer, record that we have
3847 processed the overlay strings there already, so that
3848 next_element_from_buffer doesn't try it again. */
3849 if (IT_CHARPOS (*it) >= it->end_charpos)
3850 it->overlay_strings_at_end_processed_p = 1;
3852 /* If we have to display `...' for invisible text, set
3853 the iterator up for that. */
3854 if (display_ellipsis_p)
3855 setup_for_ellipsis (it);
3857 else
3859 /* There are more overlay strings to process. If
3860 IT->current.overlay_string_index has advanced to a position
3861 where we must load IT->overlay_strings with more strings, do
3862 it. */
3863 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3865 if (it->current.overlay_string_index && i == 0)
3866 load_overlay_strings (it, 0);
3868 /* Initialize IT to deliver display elements from the overlay
3869 string. */
3870 it->string = it->overlay_strings[i];
3871 it->multibyte_p = STRING_MULTIBYTE (it->string);
3872 SET_TEXT_POS (it->current.string_pos, 0, 0);
3873 it->method = next_element_from_string;
3874 it->stop_charpos = 0;
3877 CHECK_IT (it);
3881 /* Compare two overlay_entry structures E1 and E2. Used as a
3882 comparison function for qsort in load_overlay_strings. Overlay
3883 strings for the same position are sorted so that
3885 1. All after-strings come in front of before-strings, except
3886 when they come from the same overlay.
3888 2. Within after-strings, strings are sorted so that overlay strings
3889 from overlays with higher priorities come first.
3891 2. Within before-strings, strings are sorted so that overlay
3892 strings from overlays with higher priorities come last.
3894 Value is analogous to strcmp. */
3897 static int
3898 compare_overlay_entries (e1, e2)
3899 void *e1, *e2;
3901 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3902 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3903 int result;
3905 if (entry1->after_string_p != entry2->after_string_p)
3907 /* Let after-strings appear in front of before-strings if
3908 they come from different overlays. */
3909 if (EQ (entry1->overlay, entry2->overlay))
3910 result = entry1->after_string_p ? 1 : -1;
3911 else
3912 result = entry1->after_string_p ? -1 : 1;
3914 else if (entry1->after_string_p)
3915 /* After-strings sorted in order of decreasing priority. */
3916 result = entry2->priority - entry1->priority;
3917 else
3918 /* Before-strings sorted in order of increasing priority. */
3919 result = entry1->priority - entry2->priority;
3921 return result;
3925 /* Load the vector IT->overlay_strings with overlay strings from IT's
3926 current buffer position, or from CHARPOS if that is > 0. Set
3927 IT->n_overlays to the total number of overlay strings found.
3929 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3930 a time. On entry into load_overlay_strings,
3931 IT->current.overlay_string_index gives the number of overlay
3932 strings that have already been loaded by previous calls to this
3933 function.
3935 IT->add_overlay_start contains an additional overlay start
3936 position to consider for taking overlay strings from, if non-zero.
3937 This position comes into play when the overlay has an `invisible'
3938 property, and both before and after-strings. When we've skipped to
3939 the end of the overlay, because of its `invisible' property, we
3940 nevertheless want its before-string to appear.
3941 IT->add_overlay_start will contain the overlay start position
3942 in this case.
3944 Overlay strings are sorted so that after-string strings come in
3945 front of before-string strings. Within before and after-strings,
3946 strings are sorted by overlay priority. See also function
3947 compare_overlay_entries. */
3949 static void
3950 load_overlay_strings (it, charpos)
3951 struct it *it;
3952 int charpos;
3954 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3955 Lisp_Object ov, overlay, window, str, invisible;
3956 int start, end;
3957 int size = 20;
3958 int n = 0, i, j, invis_p;
3959 struct overlay_entry *entries
3960 = (struct overlay_entry *) alloca (size * sizeof *entries);
3962 if (charpos <= 0)
3963 charpos = IT_CHARPOS (*it);
3965 /* Append the overlay string STRING of overlay OVERLAY to vector
3966 `entries' which has size `size' and currently contains `n'
3967 elements. AFTER_P non-zero means STRING is an after-string of
3968 OVERLAY. */
3969 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3970 do \
3972 Lisp_Object priority; \
3974 if (n == size) \
3976 int new_size = 2 * size; \
3977 struct overlay_entry *old = entries; \
3978 entries = \
3979 (struct overlay_entry *) alloca (new_size \
3980 * sizeof *entries); \
3981 bcopy (old, entries, size * sizeof *entries); \
3982 size = new_size; \
3985 entries[n].string = (STRING); \
3986 entries[n].overlay = (OVERLAY); \
3987 priority = Foverlay_get ((OVERLAY), Qpriority); \
3988 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3989 entries[n].after_string_p = (AFTER_P); \
3990 ++n; \
3992 while (0)
3994 /* Process overlay before the overlay center. */
3995 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3997 overlay = XCAR (ov);
3998 xassert (OVERLAYP (overlay));
3999 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4000 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4002 if (end < charpos)
4003 break;
4005 /* Skip this overlay if it doesn't start or end at IT's current
4006 position. */
4007 if (end != charpos && start != charpos)
4008 continue;
4010 /* Skip this overlay if it doesn't apply to IT->w. */
4011 window = Foverlay_get (overlay, Qwindow);
4012 if (WINDOWP (window) && XWINDOW (window) != it->w)
4013 continue;
4015 /* If the text ``under'' the overlay is invisible, both before-
4016 and after-strings from this overlay are visible; start and
4017 end position are indistinguishable. */
4018 invisible = Foverlay_get (overlay, Qinvisible);
4019 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4021 /* If overlay has a non-empty before-string, record it. */
4022 if ((start == charpos || (end == charpos && invis_p))
4023 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4024 && SCHARS (str))
4025 RECORD_OVERLAY_STRING (overlay, str, 0);
4027 /* If overlay has a non-empty after-string, record it. */
4028 if ((end == charpos || (start == charpos && invis_p))
4029 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4030 && SCHARS (str))
4031 RECORD_OVERLAY_STRING (overlay, str, 1);
4034 /* Process overlays after the overlay center. */
4035 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
4037 overlay = XCAR (ov);
4038 xassert (OVERLAYP (overlay));
4039 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4040 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4042 if (start > charpos)
4043 break;
4045 /* Skip this overlay if it doesn't start or end at IT's current
4046 position. */
4047 if (end != charpos && start != charpos)
4048 continue;
4050 /* Skip this overlay if it doesn't apply to IT->w. */
4051 window = Foverlay_get (overlay, Qwindow);
4052 if (WINDOWP (window) && XWINDOW (window) != it->w)
4053 continue;
4055 /* If the text ``under'' the overlay is invisible, it has a zero
4056 dimension, and both before- and after-strings apply. */
4057 invisible = Foverlay_get (overlay, Qinvisible);
4058 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4060 /* If overlay has a non-empty before-string, record it. */
4061 if ((start == charpos || (end == charpos && invis_p))
4062 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4063 && SCHARS (str))
4064 RECORD_OVERLAY_STRING (overlay, str, 0);
4066 /* If overlay has a non-empty after-string, record it. */
4067 if ((end == charpos || (start == charpos && invis_p))
4068 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4069 && SCHARS (str))
4070 RECORD_OVERLAY_STRING (overlay, str, 1);
4073 #undef RECORD_OVERLAY_STRING
4075 /* Sort entries. */
4076 if (n > 1)
4077 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4079 /* Record the total number of strings to process. */
4080 it->n_overlay_strings = n;
4082 /* IT->current.overlay_string_index is the number of overlay strings
4083 that have already been consumed by IT. Copy some of the
4084 remaining overlay strings to IT->overlay_strings. */
4085 i = 0;
4086 j = it->current.overlay_string_index;
4087 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4088 it->overlay_strings[i++] = entries[j++].string;
4090 CHECK_IT (it);
4094 /* Get the first chunk of overlay strings at IT's current buffer
4095 position, or at CHARPOS if that is > 0. Value is non-zero if at
4096 least one overlay string was found. */
4098 static int
4099 get_overlay_strings (it, charpos)
4100 struct it *it;
4101 int charpos;
4103 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4104 process. This fills IT->overlay_strings with strings, and sets
4105 IT->n_overlay_strings to the total number of strings to process.
4106 IT->pos.overlay_string_index has to be set temporarily to zero
4107 because load_overlay_strings needs this; it must be set to -1
4108 when no overlay strings are found because a zero value would
4109 indicate a position in the first overlay string. */
4110 it->current.overlay_string_index = 0;
4111 load_overlay_strings (it, charpos);
4113 /* If we found overlay strings, set up IT to deliver display
4114 elements from the first one. Otherwise set up IT to deliver
4115 from current_buffer. */
4116 if (it->n_overlay_strings)
4118 /* Make sure we know settings in current_buffer, so that we can
4119 restore meaningful values when we're done with the overlay
4120 strings. */
4121 compute_stop_pos (it);
4122 xassert (it->face_id >= 0);
4124 /* Save IT's settings. They are restored after all overlay
4125 strings have been processed. */
4126 xassert (it->sp == 0);
4127 push_it (it);
4129 /* Set up IT to deliver display elements from the first overlay
4130 string. */
4131 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4132 it->string = it->overlay_strings[0];
4133 it->stop_charpos = 0;
4134 xassert (STRINGP (it->string));
4135 it->end_charpos = SCHARS (it->string);
4136 it->multibyte_p = STRING_MULTIBYTE (it->string);
4137 it->method = next_element_from_string;
4139 else
4141 it->string = Qnil;
4142 it->current.overlay_string_index = -1;
4143 it->method = next_element_from_buffer;
4146 CHECK_IT (it);
4148 /* Value is non-zero if we found at least one overlay string. */
4149 return STRINGP (it->string);
4154 /***********************************************************************
4155 Saving and restoring state
4156 ***********************************************************************/
4158 /* Save current settings of IT on IT->stack. Called, for example,
4159 before setting up IT for an overlay string, to be able to restore
4160 IT's settings to what they were after the overlay string has been
4161 processed. */
4163 static void
4164 push_it (it)
4165 struct it *it;
4167 struct iterator_stack_entry *p;
4169 xassert (it->sp < 2);
4170 p = it->stack + it->sp;
4172 p->stop_charpos = it->stop_charpos;
4173 xassert (it->face_id >= 0);
4174 p->face_id = it->face_id;
4175 p->string = it->string;
4176 p->pos = it->current;
4177 p->end_charpos = it->end_charpos;
4178 p->string_nchars = it->string_nchars;
4179 p->area = it->area;
4180 p->multibyte_p = it->multibyte_p;
4181 p->space_width = it->space_width;
4182 p->font_height = it->font_height;
4183 p->voffset = it->voffset;
4184 p->string_from_display_prop_p = it->string_from_display_prop_p;
4185 p->display_ellipsis_p = 0;
4186 ++it->sp;
4190 /* Restore IT's settings from IT->stack. Called, for example, when no
4191 more overlay strings must be processed, and we return to delivering
4192 display elements from a buffer, or when the end of a string from a
4193 `display' property is reached and we return to delivering display
4194 elements from an overlay string, or from a buffer. */
4196 static void
4197 pop_it (it)
4198 struct it *it;
4200 struct iterator_stack_entry *p;
4202 xassert (it->sp > 0);
4203 --it->sp;
4204 p = it->stack + it->sp;
4205 it->stop_charpos = p->stop_charpos;
4206 it->face_id = p->face_id;
4207 it->string = p->string;
4208 it->current = p->pos;
4209 it->end_charpos = p->end_charpos;
4210 it->string_nchars = p->string_nchars;
4211 it->area = p->area;
4212 it->multibyte_p = p->multibyte_p;
4213 it->space_width = p->space_width;
4214 it->font_height = p->font_height;
4215 it->voffset = p->voffset;
4216 it->string_from_display_prop_p = p->string_from_display_prop_p;
4221 /***********************************************************************
4222 Moving over lines
4223 ***********************************************************************/
4225 /* Set IT's current position to the previous line start. */
4227 static void
4228 back_to_previous_line_start (it)
4229 struct it *it;
4231 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4232 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4236 /* Move IT to the next line start.
4238 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4239 we skipped over part of the text (as opposed to moving the iterator
4240 continuously over the text). Otherwise, don't change the value
4241 of *SKIPPED_P.
4243 Newlines may come from buffer text, overlay strings, or strings
4244 displayed via the `display' property. That's the reason we can't
4245 simply use find_next_newline_no_quit.
4247 Note that this function may not skip over invisible text that is so
4248 because of text properties and immediately follows a newline. If
4249 it would, function reseat_at_next_visible_line_start, when called
4250 from set_iterator_to_next, would effectively make invisible
4251 characters following a newline part of the wrong glyph row, which
4252 leads to wrong cursor motion. */
4254 static int
4255 forward_to_next_line_start (it, skipped_p)
4256 struct it *it;
4257 int *skipped_p;
4259 int old_selective, newline_found_p, n;
4260 const int MAX_NEWLINE_DISTANCE = 500;
4262 /* If already on a newline, just consume it to avoid unintended
4263 skipping over invisible text below. */
4264 if (it->what == IT_CHARACTER
4265 && it->c == '\n'
4266 && CHARPOS (it->position) == IT_CHARPOS (*it))
4268 set_iterator_to_next (it, 0);
4269 it->c = 0;
4270 return 1;
4273 /* Don't handle selective display in the following. It's (a)
4274 unnecessary because it's done by the caller, and (b) leads to an
4275 infinite recursion because next_element_from_ellipsis indirectly
4276 calls this function. */
4277 old_selective = it->selective;
4278 it->selective = 0;
4280 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4281 from buffer text. */
4282 for (n = newline_found_p = 0;
4283 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4284 n += STRINGP (it->string) ? 0 : 1)
4286 if (!get_next_display_element (it))
4287 return 0;
4288 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4289 set_iterator_to_next (it, 0);
4292 /* If we didn't find a newline near enough, see if we can use a
4293 short-cut. */
4294 if (!newline_found_p)
4296 int start = IT_CHARPOS (*it);
4297 int limit = find_next_newline_no_quit (start, 1);
4298 Lisp_Object pos;
4300 xassert (!STRINGP (it->string));
4302 /* If there isn't any `display' property in sight, and no
4303 overlays, we can just use the position of the newline in
4304 buffer text. */
4305 if (it->stop_charpos >= limit
4306 || ((pos = Fnext_single_property_change (make_number (start),
4307 Qdisplay,
4308 Qnil, make_number (limit)),
4309 NILP (pos))
4310 && next_overlay_change (start) == ZV))
4312 IT_CHARPOS (*it) = limit;
4313 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4314 *skipped_p = newline_found_p = 1;
4316 else
4318 while (get_next_display_element (it)
4319 && !newline_found_p)
4321 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4322 set_iterator_to_next (it, 0);
4327 it->selective = old_selective;
4328 return newline_found_p;
4332 /* Set IT's current position to the previous visible line start. Skip
4333 invisible text that is so either due to text properties or due to
4334 selective display. Caution: this does not change IT->current_x and
4335 IT->hpos. */
4337 static void
4338 back_to_previous_visible_line_start (it)
4339 struct it *it;
4341 int visible_p = 0;
4343 /* Go back one newline if not on BEGV already. */
4344 if (IT_CHARPOS (*it) > BEGV)
4345 back_to_previous_line_start (it);
4347 /* Move over lines that are invisible because of selective display
4348 or text properties. */
4349 while (IT_CHARPOS (*it) > BEGV
4350 && !visible_p)
4352 visible_p = 1;
4354 /* If selective > 0, then lines indented more than that values
4355 are invisible. */
4356 if (it->selective > 0
4357 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4358 (double) it->selective)) /* iftc */
4359 visible_p = 0;
4360 else
4362 Lisp_Object prop;
4364 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4365 Qinvisible, it->window);
4366 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4367 visible_p = 0;
4370 /* Back one more newline if the current one is invisible. */
4371 if (!visible_p)
4372 back_to_previous_line_start (it);
4375 xassert (IT_CHARPOS (*it) >= BEGV);
4376 xassert (IT_CHARPOS (*it) == BEGV
4377 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4378 CHECK_IT (it);
4382 /* Reseat iterator IT at the previous visible line start. Skip
4383 invisible text that is so either due to text properties or due to
4384 selective display. At the end, update IT's overlay information,
4385 face information etc. */
4387 static void
4388 reseat_at_previous_visible_line_start (it)
4389 struct it *it;
4391 back_to_previous_visible_line_start (it);
4392 reseat (it, it->current.pos, 1);
4393 CHECK_IT (it);
4397 /* Reseat iterator IT on the next visible line start in the current
4398 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4399 preceding the line start. Skip over invisible text that is so
4400 because of selective display. Compute faces, overlays etc at the
4401 new position. Note that this function does not skip over text that
4402 is invisible because of text properties. */
4404 static void
4405 reseat_at_next_visible_line_start (it, on_newline_p)
4406 struct it *it;
4407 int on_newline_p;
4409 int newline_found_p, skipped_p = 0;
4411 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4413 /* Skip over lines that are invisible because they are indented
4414 more than the value of IT->selective. */
4415 if (it->selective > 0)
4416 while (IT_CHARPOS (*it) < ZV
4417 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4418 (double) it->selective)) /* iftc */
4420 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4421 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4424 /* Position on the newline if that's what's requested. */
4425 if (on_newline_p && newline_found_p)
4427 if (STRINGP (it->string))
4429 if (IT_STRING_CHARPOS (*it) > 0)
4431 --IT_STRING_CHARPOS (*it);
4432 --IT_STRING_BYTEPOS (*it);
4435 else if (IT_CHARPOS (*it) > BEGV)
4437 --IT_CHARPOS (*it);
4438 --IT_BYTEPOS (*it);
4439 reseat (it, it->current.pos, 0);
4442 else if (skipped_p)
4443 reseat (it, it->current.pos, 0);
4445 CHECK_IT (it);
4450 /***********************************************************************
4451 Changing an iterator's position
4452 ***********************************************************************/
4454 /* Change IT's current position to POS in current_buffer. If FORCE_P
4455 is non-zero, always check for text properties at the new position.
4456 Otherwise, text properties are only looked up if POS >=
4457 IT->check_charpos of a property. */
4459 static void
4460 reseat (it, pos, force_p)
4461 struct it *it;
4462 struct text_pos pos;
4463 int force_p;
4465 int original_pos = IT_CHARPOS (*it);
4467 reseat_1 (it, pos, 0);
4469 /* Determine where to check text properties. Avoid doing it
4470 where possible because text property lookup is very expensive. */
4471 if (force_p
4472 || CHARPOS (pos) > it->stop_charpos
4473 || CHARPOS (pos) < original_pos)
4474 handle_stop (it);
4476 CHECK_IT (it);
4480 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4481 IT->stop_pos to POS, also. */
4483 static void
4484 reseat_1 (it, pos, set_stop_p)
4485 struct it *it;
4486 struct text_pos pos;
4487 int set_stop_p;
4489 /* Don't call this function when scanning a C string. */
4490 xassert (it->s == NULL);
4492 /* POS must be a reasonable value. */
4493 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4495 it->current.pos = it->position = pos;
4496 XSETBUFFER (it->object, current_buffer);
4497 it->end_charpos = ZV;
4498 it->dpvec = NULL;
4499 it->current.dpvec_index = -1;
4500 it->current.overlay_string_index = -1;
4501 IT_STRING_CHARPOS (*it) = -1;
4502 IT_STRING_BYTEPOS (*it) = -1;
4503 it->string = Qnil;
4504 it->method = next_element_from_buffer;
4505 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4506 it->sp = 0;
4507 it->face_before_selective_p = 0;
4509 if (set_stop_p)
4510 it->stop_charpos = CHARPOS (pos);
4514 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4515 If S is non-null, it is a C string to iterate over. Otherwise,
4516 STRING gives a Lisp string to iterate over.
4518 If PRECISION > 0, don't return more then PRECISION number of
4519 characters from the string.
4521 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4522 characters have been returned. FIELD_WIDTH < 0 means an infinite
4523 field width.
4525 MULTIBYTE = 0 means disable processing of multibyte characters,
4526 MULTIBYTE > 0 means enable it,
4527 MULTIBYTE < 0 means use IT->multibyte_p.
4529 IT must be initialized via a prior call to init_iterator before
4530 calling this function. */
4532 static void
4533 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4534 struct it *it;
4535 unsigned char *s;
4536 Lisp_Object string;
4537 int charpos;
4538 int precision, field_width, multibyte;
4540 /* No region in strings. */
4541 it->region_beg_charpos = it->region_end_charpos = -1;
4543 /* No text property checks performed by default, but see below. */
4544 it->stop_charpos = -1;
4546 /* Set iterator position and end position. */
4547 bzero (&it->current, sizeof it->current);
4548 it->current.overlay_string_index = -1;
4549 it->current.dpvec_index = -1;
4550 xassert (charpos >= 0);
4552 /* If STRING is specified, use its multibyteness, otherwise use the
4553 setting of MULTIBYTE, if specified. */
4554 if (multibyte >= 0)
4555 it->multibyte_p = multibyte > 0;
4557 if (s == NULL)
4559 xassert (STRINGP (string));
4560 it->string = string;
4561 it->s = NULL;
4562 it->end_charpos = it->string_nchars = SCHARS (string);
4563 it->method = next_element_from_string;
4564 it->current.string_pos = string_pos (charpos, string);
4566 else
4568 it->s = s;
4569 it->string = Qnil;
4571 /* Note that we use IT->current.pos, not it->current.string_pos,
4572 for displaying C strings. */
4573 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4574 if (it->multibyte_p)
4576 it->current.pos = c_string_pos (charpos, s, 1);
4577 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4579 else
4581 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4582 it->end_charpos = it->string_nchars = strlen (s);
4585 it->method = next_element_from_c_string;
4588 /* PRECISION > 0 means don't return more than PRECISION characters
4589 from the string. */
4590 if (precision > 0 && it->end_charpos - charpos > precision)
4591 it->end_charpos = it->string_nchars = charpos + precision;
4593 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4594 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4595 FIELD_WIDTH < 0 means infinite field width. This is useful for
4596 padding with `-' at the end of a mode line. */
4597 if (field_width < 0)
4598 field_width = INFINITY;
4599 if (field_width > it->end_charpos - charpos)
4600 it->end_charpos = charpos + field_width;
4602 /* Use the standard display table for displaying strings. */
4603 if (DISP_TABLE_P (Vstandard_display_table))
4604 it->dp = XCHAR_TABLE (Vstandard_display_table);
4606 it->stop_charpos = charpos;
4607 CHECK_IT (it);
4612 /***********************************************************************
4613 Iteration
4614 ***********************************************************************/
4616 /* Load IT's display element fields with information about the next
4617 display element from the current position of IT. Value is zero if
4618 end of buffer (or C string) is reached. */
4621 get_next_display_element (it)
4622 struct it *it;
4624 /* Non-zero means that we found a display element. Zero means that
4625 we hit the end of what we iterate over. Performance note: the
4626 function pointer `method' used here turns out to be faster than
4627 using a sequence of if-statements. */
4628 int success_p = (*it->method) (it);
4630 if (it->what == IT_CHARACTER)
4632 /* Map via display table or translate control characters.
4633 IT->c, IT->len etc. have been set to the next character by
4634 the function call above. If we have a display table, and it
4635 contains an entry for IT->c, translate it. Don't do this if
4636 IT->c itself comes from a display table, otherwise we could
4637 end up in an infinite recursion. (An alternative could be to
4638 count the recursion depth of this function and signal an
4639 error when a certain maximum depth is reached.) Is it worth
4640 it? */
4641 if (success_p && it->dpvec == NULL)
4643 Lisp_Object dv;
4645 if (it->dp
4646 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4647 VECTORP (dv)))
4649 struct Lisp_Vector *v = XVECTOR (dv);
4651 /* Return the first character from the display table
4652 entry, if not empty. If empty, don't display the
4653 current character. */
4654 if (v->size)
4656 it->dpvec_char_len = it->len;
4657 it->dpvec = v->contents;
4658 it->dpend = v->contents + v->size;
4659 it->current.dpvec_index = 0;
4660 it->method = next_element_from_display_vector;
4661 success_p = get_next_display_element (it);
4663 else
4665 set_iterator_to_next (it, 0);
4666 success_p = get_next_display_element (it);
4670 /* Translate control characters into `\003' or `^C' form.
4671 Control characters coming from a display table entry are
4672 currently not translated because we use IT->dpvec to hold
4673 the translation. This could easily be changed but I
4674 don't believe that it is worth doing.
4676 If it->multibyte_p is nonzero, eight-bit characters and
4677 non-printable multibyte characters are also translated to
4678 octal form.
4680 If it->multibyte_p is zero, eight-bit characters that
4681 don't have corresponding multibyte char code are also
4682 translated to octal form. */
4683 else if ((it->c < ' '
4684 && (it->area != TEXT_AREA
4685 || (it->c != '\n' && it->c != '\t')))
4686 || (it->multibyte_p
4687 ? ((it->c >= 127
4688 && it->len == 1)
4689 || !CHAR_PRINTABLE_P (it->c))
4690 : (it->c >= 127
4691 && it->c == unibyte_char_to_multibyte (it->c))))
4693 /* IT->c is a control character which must be displayed
4694 either as '\003' or as `^C' where the '\\' and '^'
4695 can be defined in the display table. Fill
4696 IT->ctl_chars with glyphs for what we have to
4697 display. Then, set IT->dpvec to these glyphs. */
4698 GLYPH g;
4700 if (it->c < 128 && it->ctl_arrow_p)
4702 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4703 if (it->dp
4704 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4705 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4706 g = XINT (DISP_CTRL_GLYPH (it->dp));
4707 else
4708 g = FAST_MAKE_GLYPH ('^', 0);
4709 XSETINT (it->ctl_chars[0], g);
4711 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4712 XSETINT (it->ctl_chars[1], g);
4714 /* Set up IT->dpvec and return first character from it. */
4715 it->dpvec_char_len = it->len;
4716 it->dpvec = it->ctl_chars;
4717 it->dpend = it->dpvec + 2;
4718 it->current.dpvec_index = 0;
4719 it->method = next_element_from_display_vector;
4720 get_next_display_element (it);
4722 else
4724 unsigned char str[MAX_MULTIBYTE_LENGTH];
4725 int len;
4726 int i;
4727 GLYPH escape_glyph;
4729 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4730 if (it->dp
4731 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4732 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4733 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4734 else
4735 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4737 if (SINGLE_BYTE_CHAR_P (it->c))
4738 str[0] = it->c, len = 1;
4739 else
4741 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4742 if (len < 0)
4744 /* It's an invalid character, which
4745 shouldn't happen actually, but due to
4746 bugs it may happen. Let's print the char
4747 as is, there's not much meaningful we can
4748 do with it. */
4749 str[0] = it->c;
4750 str[1] = it->c >> 8;
4751 str[2] = it->c >> 16;
4752 str[3] = it->c >> 24;
4753 len = 4;
4757 for (i = 0; i < len; i++)
4759 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4760 /* Insert three more glyphs into IT->ctl_chars for
4761 the octal display of the character. */
4762 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4763 XSETINT (it->ctl_chars[i * 4 + 1], g);
4764 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4765 XSETINT (it->ctl_chars[i * 4 + 2], g);
4766 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4767 XSETINT (it->ctl_chars[i * 4 + 3], g);
4770 /* Set up IT->dpvec and return the first character
4771 from it. */
4772 it->dpvec_char_len = it->len;
4773 it->dpvec = it->ctl_chars;
4774 it->dpend = it->dpvec + len * 4;
4775 it->current.dpvec_index = 0;
4776 it->method = next_element_from_display_vector;
4777 get_next_display_element (it);
4782 /* Adjust face id for a multibyte character. There are no
4783 multibyte character in unibyte text. */
4784 if (it->multibyte_p
4785 && success_p
4786 && FRAME_WINDOW_P (it->f))
4788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4789 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4793 /* Is this character the last one of a run of characters with
4794 box? If yes, set IT->end_of_box_run_p to 1. */
4795 if (it->face_box_p
4796 && it->s == NULL)
4798 int face_id;
4799 struct face *face;
4801 it->end_of_box_run_p
4802 = ((face_id = face_after_it_pos (it),
4803 face_id != it->face_id)
4804 && (face = FACE_FROM_ID (it->f, face_id),
4805 face->box == FACE_NO_BOX));
4808 /* Value is 0 if end of buffer or string reached. */
4809 return success_p;
4813 /* Move IT to the next display element.
4815 RESEAT_P non-zero means if called on a newline in buffer text,
4816 skip to the next visible line start.
4818 Functions get_next_display_element and set_iterator_to_next are
4819 separate because I find this arrangement easier to handle than a
4820 get_next_display_element function that also increments IT's
4821 position. The way it is we can first look at an iterator's current
4822 display element, decide whether it fits on a line, and if it does,
4823 increment the iterator position. The other way around we probably
4824 would either need a flag indicating whether the iterator has to be
4825 incremented the next time, or we would have to implement a
4826 decrement position function which would not be easy to write. */
4828 void
4829 set_iterator_to_next (it, reseat_p)
4830 struct it *it;
4831 int reseat_p;
4833 /* Reset flags indicating start and end of a sequence of characters
4834 with box. Reset them at the start of this function because
4835 moving the iterator to a new position might set them. */
4836 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4838 if (it->method == next_element_from_buffer)
4840 /* The current display element of IT is a character from
4841 current_buffer. Advance in the buffer, and maybe skip over
4842 invisible lines that are so because of selective display. */
4843 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4844 reseat_at_next_visible_line_start (it, 0);
4845 else
4847 xassert (it->len != 0);
4848 IT_BYTEPOS (*it) += it->len;
4849 IT_CHARPOS (*it) += 1;
4850 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4853 else if (it->method == next_element_from_composition)
4855 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4856 if (STRINGP (it->string))
4858 IT_STRING_BYTEPOS (*it) += it->len;
4859 IT_STRING_CHARPOS (*it) += it->cmp_len;
4860 it->method = next_element_from_string;
4861 goto consider_string_end;
4863 else
4865 IT_BYTEPOS (*it) += it->len;
4866 IT_CHARPOS (*it) += it->cmp_len;
4867 it->method = next_element_from_buffer;
4870 else if (it->method == next_element_from_c_string)
4872 /* Current display element of IT is from a C string. */
4873 IT_BYTEPOS (*it) += it->len;
4874 IT_CHARPOS (*it) += 1;
4876 else if (it->method == next_element_from_display_vector)
4878 /* Current display element of IT is from a display table entry.
4879 Advance in the display table definition. Reset it to null if
4880 end reached, and continue with characters from buffers/
4881 strings. */
4882 ++it->current.dpvec_index;
4884 /* Restore face of the iterator to what they were before the
4885 display vector entry (these entries may contain faces). */
4886 it->face_id = it->saved_face_id;
4888 if (it->dpvec + it->current.dpvec_index == it->dpend)
4890 if (it->s)
4891 it->method = next_element_from_c_string;
4892 else if (STRINGP (it->string))
4893 it->method = next_element_from_string;
4894 else
4895 it->method = next_element_from_buffer;
4897 it->dpvec = NULL;
4898 it->current.dpvec_index = -1;
4900 /* Skip over characters which were displayed via IT->dpvec. */
4901 if (it->dpvec_char_len < 0)
4902 reseat_at_next_visible_line_start (it, 1);
4903 else if (it->dpvec_char_len > 0)
4905 it->len = it->dpvec_char_len;
4906 set_iterator_to_next (it, reseat_p);
4910 else if (it->method == next_element_from_string)
4912 /* Current display element is a character from a Lisp string. */
4913 xassert (it->s == NULL && STRINGP (it->string));
4914 IT_STRING_BYTEPOS (*it) += it->len;
4915 IT_STRING_CHARPOS (*it) += 1;
4917 consider_string_end:
4919 if (it->current.overlay_string_index >= 0)
4921 /* IT->string is an overlay string. Advance to the
4922 next, if there is one. */
4923 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4924 next_overlay_string (it);
4926 else
4928 /* IT->string is not an overlay string. If we reached
4929 its end, and there is something on IT->stack, proceed
4930 with what is on the stack. This can be either another
4931 string, this time an overlay string, or a buffer. */
4932 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4933 && it->sp > 0)
4935 pop_it (it);
4936 if (!STRINGP (it->string))
4937 it->method = next_element_from_buffer;
4938 else
4939 goto consider_string_end;
4943 else if (it->method == next_element_from_image
4944 || it->method == next_element_from_stretch)
4946 /* The position etc with which we have to proceed are on
4947 the stack. The position may be at the end of a string,
4948 if the `display' property takes up the whole string. */
4949 pop_it (it);
4950 it->image_id = 0;
4951 if (STRINGP (it->string))
4953 it->method = next_element_from_string;
4954 goto consider_string_end;
4956 else
4957 it->method = next_element_from_buffer;
4959 else
4960 /* There are no other methods defined, so this should be a bug. */
4961 abort ();
4963 xassert (it->method != next_element_from_string
4964 || (STRINGP (it->string)
4965 && IT_STRING_CHARPOS (*it) >= 0));
4969 /* Load IT's display element fields with information about the next
4970 display element which comes from a display table entry or from the
4971 result of translating a control character to one of the forms `^C'
4972 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4974 static int
4975 next_element_from_display_vector (it)
4976 struct it *it;
4978 /* Precondition. */
4979 xassert (it->dpvec && it->current.dpvec_index >= 0);
4981 /* Remember the current face id in case glyphs specify faces.
4982 IT's face is restored in set_iterator_to_next. */
4983 it->saved_face_id = it->face_id;
4985 if (INTEGERP (*it->dpvec)
4986 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4988 int lface_id;
4989 GLYPH g;
4991 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4992 it->c = FAST_GLYPH_CHAR (g);
4993 it->len = CHAR_BYTES (it->c);
4995 /* The entry may contain a face id to use. Such a face id is
4996 the id of a Lisp face, not a realized face. A face id of
4997 zero means no face is specified. */
4998 lface_id = FAST_GLYPH_FACE (g);
4999 if (lface_id)
5001 /* The function returns -1 if lface_id is invalid. */
5002 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5003 if (face_id >= 0)
5004 it->face_id = face_id;
5007 else
5008 /* Display table entry is invalid. Return a space. */
5009 it->c = ' ', it->len = 1;
5011 /* Don't change position and object of the iterator here. They are
5012 still the values of the character that had this display table
5013 entry or was translated, and that's what we want. */
5014 it->what = IT_CHARACTER;
5015 return 1;
5019 /* Load IT with the next display element from Lisp string IT->string.
5020 IT->current.string_pos is the current position within the string.
5021 If IT->current.overlay_string_index >= 0, the Lisp string is an
5022 overlay string. */
5024 static int
5025 next_element_from_string (it)
5026 struct it *it;
5028 struct text_pos position;
5030 xassert (STRINGP (it->string));
5031 xassert (IT_STRING_CHARPOS (*it) >= 0);
5032 position = it->current.string_pos;
5034 /* Time to check for invisible text? */
5035 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5036 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5038 handle_stop (it);
5040 /* Since a handler may have changed IT->method, we must
5041 recurse here. */
5042 return get_next_display_element (it);
5045 if (it->current.overlay_string_index >= 0)
5047 /* Get the next character from an overlay string. In overlay
5048 strings, There is no field width or padding with spaces to
5049 do. */
5050 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5052 it->what = IT_EOB;
5053 return 0;
5055 else if (STRING_MULTIBYTE (it->string))
5057 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5058 const unsigned char *s = (SDATA (it->string)
5059 + IT_STRING_BYTEPOS (*it));
5060 it->c = string_char_and_length (s, remaining, &it->len);
5062 else
5064 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5065 it->len = 1;
5068 else
5070 /* Get the next character from a Lisp string that is not an
5071 overlay string. Such strings come from the mode line, for
5072 example. We may have to pad with spaces, or truncate the
5073 string. See also next_element_from_c_string. */
5074 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5076 it->what = IT_EOB;
5077 return 0;
5079 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5081 /* Pad with spaces. */
5082 it->c = ' ', it->len = 1;
5083 CHARPOS (position) = BYTEPOS (position) = -1;
5085 else if (STRING_MULTIBYTE (it->string))
5087 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5088 const unsigned char *s = (SDATA (it->string)
5089 + IT_STRING_BYTEPOS (*it));
5090 it->c = string_char_and_length (s, maxlen, &it->len);
5092 else
5094 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5095 it->len = 1;
5099 /* Record what we have and where it came from. Note that we store a
5100 buffer position in IT->position although it could arguably be a
5101 string position. */
5102 it->what = IT_CHARACTER;
5103 it->object = it->string;
5104 it->position = position;
5105 return 1;
5109 /* Load IT with next display element from C string IT->s.
5110 IT->string_nchars is the maximum number of characters to return
5111 from the string. IT->end_charpos may be greater than
5112 IT->string_nchars when this function is called, in which case we
5113 may have to return padding spaces. Value is zero if end of string
5114 reached, including padding spaces. */
5116 static int
5117 next_element_from_c_string (it)
5118 struct it *it;
5120 int success_p = 1;
5122 xassert (it->s);
5123 it->what = IT_CHARACTER;
5124 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5125 it->object = Qnil;
5127 /* IT's position can be greater IT->string_nchars in case a field
5128 width or precision has been specified when the iterator was
5129 initialized. */
5130 if (IT_CHARPOS (*it) >= it->end_charpos)
5132 /* End of the game. */
5133 it->what = IT_EOB;
5134 success_p = 0;
5136 else if (IT_CHARPOS (*it) >= it->string_nchars)
5138 /* Pad with spaces. */
5139 it->c = ' ', it->len = 1;
5140 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5142 else if (it->multibyte_p)
5144 /* Implementation note: The calls to strlen apparently aren't a
5145 performance problem because there is no noticeable performance
5146 difference between Emacs running in unibyte or multibyte mode. */
5147 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5148 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5149 maxlen, &it->len);
5151 else
5152 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5154 return success_p;
5158 /* Set up IT to return characters from an ellipsis, if appropriate.
5159 The definition of the ellipsis glyphs may come from a display table
5160 entry. This function Fills IT with the first glyph from the
5161 ellipsis if an ellipsis is to be displayed. */
5163 static int
5164 next_element_from_ellipsis (it)
5165 struct it *it;
5167 if (it->selective_display_ellipsis_p)
5169 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5171 /* Use the display table definition for `...'. Invalid glyphs
5172 will be handled by the method returning elements from dpvec. */
5173 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5174 it->dpvec_char_len = it->len;
5175 it->dpvec = v->contents;
5176 it->dpend = v->contents + v->size;
5177 it->current.dpvec_index = 0;
5178 it->method = next_element_from_display_vector;
5180 else
5182 /* Use default `...' which is stored in default_invis_vector. */
5183 it->dpvec_char_len = it->len;
5184 it->dpvec = default_invis_vector;
5185 it->dpend = default_invis_vector + 3;
5186 it->current.dpvec_index = 0;
5187 it->method = next_element_from_display_vector;
5190 else
5192 /* The face at the current position may be different from the
5193 face we find after the invisible text. Remember what it
5194 was in IT->saved_face_id, and signal that it's there by
5195 setting face_before_selective_p. */
5196 it->saved_face_id = it->face_id;
5197 it->method = next_element_from_buffer;
5198 reseat_at_next_visible_line_start (it, 1);
5199 it->face_before_selective_p = 1;
5202 return get_next_display_element (it);
5206 /* Deliver an image display element. The iterator IT is already
5207 filled with image information (done in handle_display_prop). Value
5208 is always 1. */
5211 static int
5212 next_element_from_image (it)
5213 struct it *it;
5215 it->what = IT_IMAGE;
5216 return 1;
5220 /* Fill iterator IT with next display element from a stretch glyph
5221 property. IT->object is the value of the text property. Value is
5222 always 1. */
5224 static int
5225 next_element_from_stretch (it)
5226 struct it *it;
5228 it->what = IT_STRETCH;
5229 return 1;
5233 /* Load IT with the next display element from current_buffer. Value
5234 is zero if end of buffer reached. IT->stop_charpos is the next
5235 position at which to stop and check for text properties or buffer
5236 end. */
5238 static int
5239 next_element_from_buffer (it)
5240 struct it *it;
5242 int success_p = 1;
5244 /* Check this assumption, otherwise, we would never enter the
5245 if-statement, below. */
5246 xassert (IT_CHARPOS (*it) >= BEGV
5247 && IT_CHARPOS (*it) <= it->stop_charpos);
5249 if (IT_CHARPOS (*it) >= it->stop_charpos)
5251 if (IT_CHARPOS (*it) >= it->end_charpos)
5253 int overlay_strings_follow_p;
5255 /* End of the game, except when overlay strings follow that
5256 haven't been returned yet. */
5257 if (it->overlay_strings_at_end_processed_p)
5258 overlay_strings_follow_p = 0;
5259 else
5261 it->overlay_strings_at_end_processed_p = 1;
5262 overlay_strings_follow_p = get_overlay_strings (it, 0);
5265 if (overlay_strings_follow_p)
5266 success_p = get_next_display_element (it);
5267 else
5269 it->what = IT_EOB;
5270 it->position = it->current.pos;
5271 success_p = 0;
5274 else
5276 handle_stop (it);
5277 return get_next_display_element (it);
5280 else
5282 /* No face changes, overlays etc. in sight, so just return a
5283 character from current_buffer. */
5284 unsigned char *p;
5286 /* Maybe run the redisplay end trigger hook. Performance note:
5287 This doesn't seem to cost measurable time. */
5288 if (it->redisplay_end_trigger_charpos
5289 && it->glyph_row
5290 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5291 run_redisplay_end_trigger_hook (it);
5293 /* Get the next character, maybe multibyte. */
5294 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5295 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5297 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5298 - IT_BYTEPOS (*it));
5299 it->c = string_char_and_length (p, maxlen, &it->len);
5301 else
5302 it->c = *p, it->len = 1;
5304 /* Record what we have and where it came from. */
5305 it->what = IT_CHARACTER;;
5306 it->object = it->w->buffer;
5307 it->position = it->current.pos;
5309 /* Normally we return the character found above, except when we
5310 really want to return an ellipsis for selective display. */
5311 if (it->selective)
5313 if (it->c == '\n')
5315 /* A value of selective > 0 means hide lines indented more
5316 than that number of columns. */
5317 if (it->selective > 0
5318 && IT_CHARPOS (*it) + 1 < ZV
5319 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5320 IT_BYTEPOS (*it) + 1,
5321 (double) it->selective)) /* iftc */
5323 success_p = next_element_from_ellipsis (it);
5324 it->dpvec_char_len = -1;
5327 else if (it->c == '\r' && it->selective == -1)
5329 /* A value of selective == -1 means that everything from the
5330 CR to the end of the line is invisible, with maybe an
5331 ellipsis displayed for it. */
5332 success_p = next_element_from_ellipsis (it);
5333 it->dpvec_char_len = -1;
5338 /* Value is zero if end of buffer reached. */
5339 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5340 return success_p;
5344 /* Run the redisplay end trigger hook for IT. */
5346 static void
5347 run_redisplay_end_trigger_hook (it)
5348 struct it *it;
5350 Lisp_Object args[3];
5352 /* IT->glyph_row should be non-null, i.e. we should be actually
5353 displaying something, or otherwise we should not run the hook. */
5354 xassert (it->glyph_row);
5356 /* Set up hook arguments. */
5357 args[0] = Qredisplay_end_trigger_functions;
5358 args[1] = it->window;
5359 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5360 it->redisplay_end_trigger_charpos = 0;
5362 /* Since we are *trying* to run these functions, don't try to run
5363 them again, even if they get an error. */
5364 it->w->redisplay_end_trigger = Qnil;
5365 Frun_hook_with_args (3, args);
5367 /* Notice if it changed the face of the character we are on. */
5368 handle_face_prop (it);
5372 /* Deliver a composition display element. The iterator IT is already
5373 filled with composition information (done in
5374 handle_composition_prop). Value is always 1. */
5376 static int
5377 next_element_from_composition (it)
5378 struct it *it;
5380 it->what = IT_COMPOSITION;
5381 it->position = (STRINGP (it->string)
5382 ? it->current.string_pos
5383 : it->current.pos);
5384 return 1;
5389 /***********************************************************************
5390 Moving an iterator without producing glyphs
5391 ***********************************************************************/
5393 /* Move iterator IT to a specified buffer or X position within one
5394 line on the display without producing glyphs.
5396 OP should be a bit mask including some or all of these bits:
5397 MOVE_TO_X: Stop on reaching x-position TO_X.
5398 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5399 Regardless of OP's value, stop in reaching the end of the display line.
5401 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5402 This means, in particular, that TO_X includes window's horizontal
5403 scroll amount.
5405 The return value has several possible values that
5406 say what condition caused the scan to stop:
5408 MOVE_POS_MATCH_OR_ZV
5409 - when TO_POS or ZV was reached.
5411 MOVE_X_REACHED
5412 -when TO_X was reached before TO_POS or ZV were reached.
5414 MOVE_LINE_CONTINUED
5415 - when we reached the end of the display area and the line must
5416 be continued.
5418 MOVE_LINE_TRUNCATED
5419 - when we reached the end of the display area and the line is
5420 truncated.
5422 MOVE_NEWLINE_OR_CR
5423 - when we stopped at a line end, i.e. a newline or a CR and selective
5424 display is on. */
5426 static enum move_it_result
5427 move_it_in_display_line_to (it, to_charpos, to_x, op)
5428 struct it *it;
5429 int to_charpos, to_x, op;
5431 enum move_it_result result = MOVE_UNDEFINED;
5432 struct glyph_row *saved_glyph_row;
5434 /* Don't produce glyphs in produce_glyphs. */
5435 saved_glyph_row = it->glyph_row;
5436 it->glyph_row = NULL;
5438 while (1)
5440 int x, i, ascent = 0, descent = 0;
5442 /* Stop when ZV or TO_CHARPOS reached. */
5443 if (!get_next_display_element (it)
5444 || ((op & MOVE_TO_POS) != 0
5445 && BUFFERP (it->object)
5446 && IT_CHARPOS (*it) >= to_charpos))
5448 result = MOVE_POS_MATCH_OR_ZV;
5449 break;
5452 /* The call to produce_glyphs will get the metrics of the
5453 display element IT is loaded with. We record in x the
5454 x-position before this display element in case it does not
5455 fit on the line. */
5456 x = it->current_x;
5458 /* Remember the line height so far in case the next element doesn't
5459 fit on the line. */
5460 if (!it->truncate_lines_p)
5462 ascent = it->max_ascent;
5463 descent = it->max_descent;
5466 PRODUCE_GLYPHS (it);
5468 if (it->area != TEXT_AREA)
5470 set_iterator_to_next (it, 1);
5471 continue;
5474 /* The number of glyphs we get back in IT->nglyphs will normally
5475 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5476 character on a terminal frame, or (iii) a line end. For the
5477 second case, IT->nglyphs - 1 padding glyphs will be present
5478 (on X frames, there is only one glyph produced for a
5479 composite character.
5481 The behavior implemented below means, for continuation lines,
5482 that as many spaces of a TAB as fit on the current line are
5483 displayed there. For terminal frames, as many glyphs of a
5484 multi-glyph character are displayed in the current line, too.
5485 This is what the old redisplay code did, and we keep it that
5486 way. Under X, the whole shape of a complex character must
5487 fit on the line or it will be completely displayed in the
5488 next line.
5490 Note that both for tabs and padding glyphs, all glyphs have
5491 the same width. */
5492 if (it->nglyphs)
5494 /* More than one glyph or glyph doesn't fit on line. All
5495 glyphs have the same width. */
5496 int single_glyph_width = it->pixel_width / it->nglyphs;
5497 int new_x;
5499 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5501 new_x = x + single_glyph_width;
5503 /* We want to leave anything reaching TO_X to the caller. */
5504 if ((op & MOVE_TO_X) && new_x > to_x)
5506 it->current_x = x;
5507 result = MOVE_X_REACHED;
5508 break;
5510 else if (/* Lines are continued. */
5511 !it->truncate_lines_p
5512 && (/* And glyph doesn't fit on the line. */
5513 new_x > it->last_visible_x
5514 /* Or it fits exactly and we're on a window
5515 system frame. */
5516 || (new_x == it->last_visible_x
5517 && FRAME_WINDOW_P (it->f))))
5519 if (/* IT->hpos == 0 means the very first glyph
5520 doesn't fit on the line, e.g. a wide image. */
5521 it->hpos == 0
5522 || (new_x == it->last_visible_x
5523 && FRAME_WINDOW_P (it->f)))
5525 ++it->hpos;
5526 it->current_x = new_x;
5527 if (i == it->nglyphs - 1)
5528 set_iterator_to_next (it, 1);
5530 else
5532 it->current_x = x;
5533 it->max_ascent = ascent;
5534 it->max_descent = descent;
5537 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5538 IT_CHARPOS (*it)));
5539 result = MOVE_LINE_CONTINUED;
5540 break;
5542 else if (new_x > it->first_visible_x)
5544 /* Glyph is visible. Increment number of glyphs that
5545 would be displayed. */
5546 ++it->hpos;
5548 else
5550 /* Glyph is completely off the left margin of the display
5551 area. Nothing to do. */
5555 if (result != MOVE_UNDEFINED)
5556 break;
5558 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5560 /* Stop when TO_X specified and reached. This check is
5561 necessary here because of lines consisting of a line end,
5562 only. The line end will not produce any glyphs and we
5563 would never get MOVE_X_REACHED. */
5564 xassert (it->nglyphs == 0);
5565 result = MOVE_X_REACHED;
5566 break;
5569 /* Is this a line end? If yes, we're done. */
5570 if (ITERATOR_AT_END_OF_LINE_P (it))
5572 result = MOVE_NEWLINE_OR_CR;
5573 break;
5576 /* The current display element has been consumed. Advance
5577 to the next. */
5578 set_iterator_to_next (it, 1);
5580 /* Stop if lines are truncated and IT's current x-position is
5581 past the right edge of the window now. */
5582 if (it->truncate_lines_p
5583 && it->current_x >= it->last_visible_x)
5585 result = MOVE_LINE_TRUNCATED;
5586 break;
5590 /* Restore the iterator settings altered at the beginning of this
5591 function. */
5592 it->glyph_row = saved_glyph_row;
5593 return result;
5597 /* Move IT forward until it satisfies one or more of the criteria in
5598 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5600 OP is a bit-mask that specifies where to stop, and in particular,
5601 which of those four position arguments makes a difference. See the
5602 description of enum move_operation_enum.
5604 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5605 screen line, this function will set IT to the next position >
5606 TO_CHARPOS. */
5608 void
5609 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5610 struct it *it;
5611 int to_charpos, to_x, to_y, to_vpos;
5612 int op;
5614 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5615 int line_height;
5616 int reached = 0;
5618 for (;;)
5620 if (op & MOVE_TO_VPOS)
5622 /* If no TO_CHARPOS and no TO_X specified, stop at the
5623 start of the line TO_VPOS. */
5624 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5626 if (it->vpos == to_vpos)
5628 reached = 1;
5629 break;
5631 else
5632 skip = move_it_in_display_line_to (it, -1, -1, 0);
5634 else
5636 /* TO_VPOS >= 0 means stop at TO_X in the line at
5637 TO_VPOS, or at TO_POS, whichever comes first. */
5638 if (it->vpos == to_vpos)
5640 reached = 2;
5641 break;
5644 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5646 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5648 reached = 3;
5649 break;
5651 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5653 /* We have reached TO_X but not in the line we want. */
5654 skip = move_it_in_display_line_to (it, to_charpos,
5655 -1, MOVE_TO_POS);
5656 if (skip == MOVE_POS_MATCH_OR_ZV)
5658 reached = 4;
5659 break;
5664 else if (op & MOVE_TO_Y)
5666 struct it it_backup;
5668 /* TO_Y specified means stop at TO_X in the line containing
5669 TO_Y---or at TO_CHARPOS if this is reached first. The
5670 problem is that we can't really tell whether the line
5671 contains TO_Y before we have completely scanned it, and
5672 this may skip past TO_X. What we do is to first scan to
5673 TO_X.
5675 If TO_X is not specified, use a TO_X of zero. The reason
5676 is to make the outcome of this function more predictable.
5677 If we didn't use TO_X == 0, we would stop at the end of
5678 the line which is probably not what a caller would expect
5679 to happen. */
5680 skip = move_it_in_display_line_to (it, to_charpos,
5681 ((op & MOVE_TO_X)
5682 ? to_x : 0),
5683 (MOVE_TO_X
5684 | (op & MOVE_TO_POS)));
5686 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5687 if (skip == MOVE_POS_MATCH_OR_ZV)
5689 reached = 5;
5690 break;
5693 /* If TO_X was reached, we would like to know whether TO_Y
5694 is in the line. This can only be said if we know the
5695 total line height which requires us to scan the rest of
5696 the line. */
5697 if (skip == MOVE_X_REACHED)
5699 it_backup = *it;
5700 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5701 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5702 op & MOVE_TO_POS);
5703 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5706 /* Now, decide whether TO_Y is in this line. */
5707 line_height = it->max_ascent + it->max_descent;
5708 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5710 if (to_y >= it->current_y
5711 && to_y < it->current_y + line_height)
5713 if (skip == MOVE_X_REACHED)
5714 /* If TO_Y is in this line and TO_X was reached above,
5715 we scanned too far. We have to restore IT's settings
5716 to the ones before skipping. */
5717 *it = it_backup;
5718 reached = 6;
5720 else if (skip == MOVE_X_REACHED)
5722 skip = skip2;
5723 if (skip == MOVE_POS_MATCH_OR_ZV)
5724 reached = 7;
5727 if (reached)
5728 break;
5730 else
5731 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5733 switch (skip)
5735 case MOVE_POS_MATCH_OR_ZV:
5736 reached = 8;
5737 goto out;
5739 case MOVE_NEWLINE_OR_CR:
5740 set_iterator_to_next (it, 1);
5741 it->continuation_lines_width = 0;
5742 break;
5744 case MOVE_LINE_TRUNCATED:
5745 it->continuation_lines_width = 0;
5746 reseat_at_next_visible_line_start (it, 0);
5747 if ((op & MOVE_TO_POS) != 0
5748 && IT_CHARPOS (*it) > to_charpos)
5750 reached = 9;
5751 goto out;
5753 break;
5755 case MOVE_LINE_CONTINUED:
5756 it->continuation_lines_width += it->current_x;
5757 break;
5759 default:
5760 abort ();
5763 /* Reset/increment for the next run. */
5764 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5765 it->current_x = it->hpos = 0;
5766 it->current_y += it->max_ascent + it->max_descent;
5767 ++it->vpos;
5768 last_height = it->max_ascent + it->max_descent;
5769 last_max_ascent = it->max_ascent;
5770 it->max_ascent = it->max_descent = 0;
5773 out:
5775 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5779 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5781 If DY > 0, move IT backward at least that many pixels. DY = 0
5782 means move IT backward to the preceding line start or BEGV. This
5783 function may move over more than DY pixels if IT->current_y - DY
5784 ends up in the middle of a line; in this case IT->current_y will be
5785 set to the top of the line moved to. */
5787 void
5788 move_it_vertically_backward (it, dy)
5789 struct it *it;
5790 int dy;
5792 int nlines, h;
5793 struct it it2, it3;
5794 int start_pos = IT_CHARPOS (*it);
5796 xassert (dy >= 0);
5798 /* Estimate how many newlines we must move back. */
5799 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5801 /* Set the iterator's position that many lines back. */
5802 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5803 back_to_previous_visible_line_start (it);
5805 /* Reseat the iterator here. When moving backward, we don't want
5806 reseat to skip forward over invisible text, set up the iterator
5807 to deliver from overlay strings at the new position etc. So,
5808 use reseat_1 here. */
5809 reseat_1 (it, it->current.pos, 1);
5811 /* We are now surely at a line start. */
5812 it->current_x = it->hpos = 0;
5813 it->continuation_lines_width = 0;
5815 /* Move forward and see what y-distance we moved. First move to the
5816 start of the next line so that we get its height. We need this
5817 height to be able to tell whether we reached the specified
5818 y-distance. */
5819 it2 = *it;
5820 it2.max_ascent = it2.max_descent = 0;
5821 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5822 MOVE_TO_POS | MOVE_TO_VPOS);
5823 xassert (IT_CHARPOS (*it) >= BEGV);
5824 it3 = it2;
5826 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5827 xassert (IT_CHARPOS (*it) >= BEGV);
5828 /* H is the actual vertical distance from the position in *IT
5829 and the starting position. */
5830 h = it2.current_y - it->current_y;
5831 /* NLINES is the distance in number of lines. */
5832 nlines = it2.vpos - it->vpos;
5834 /* Correct IT's y and vpos position
5835 so that they are relative to the starting point. */
5836 it->vpos -= nlines;
5837 it->current_y -= h;
5839 if (dy == 0)
5841 /* DY == 0 means move to the start of the screen line. The
5842 value of nlines is > 0 if continuation lines were involved. */
5843 if (nlines > 0)
5844 move_it_by_lines (it, nlines, 1);
5845 xassert (IT_CHARPOS (*it) <= start_pos);
5847 else
5849 /* The y-position we try to reach, relative to *IT.
5850 Note that H has been subtracted in front of the if-statement. */
5851 int target_y = it->current_y + h - dy;
5852 int y0 = it3.current_y;
5853 int y1 = line_bottom_y (&it3);
5854 int line_height = y1 - y0;
5856 /* If we did not reach target_y, try to move further backward if
5857 we can. If we moved too far backward, try to move forward. */
5858 if (target_y < it->current_y
5859 /* This is heuristic. In a window that's 3 lines high, with
5860 a line height of 13 pixels each, recentering with point
5861 on the bottom line will try to move -39/2 = 19 pixels
5862 backward. Try to avoid moving into the first line. */
5863 && it->current_y - target_y > line_height / 3 * 2
5864 && IT_CHARPOS (*it) > BEGV)
5866 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5867 target_y - it->current_y));
5868 move_it_vertically (it, target_y - it->current_y);
5869 xassert (IT_CHARPOS (*it) >= BEGV);
5871 else if (target_y >= it->current_y + line_height
5872 && IT_CHARPOS (*it) < ZV)
5874 /* Should move forward by at least one line, maybe more.
5876 Note: Calling move_it_by_lines can be expensive on
5877 terminal frames, where compute_motion is used (via
5878 vmotion) to do the job, when there are very long lines
5879 and truncate-lines is nil. That's the reason for
5880 treating terminal frames specially here. */
5882 if (!FRAME_WINDOW_P (it->f))
5883 move_it_vertically (it, target_y - (it->current_y + line_height));
5884 else
5888 move_it_by_lines (it, 1, 1);
5890 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5893 xassert (IT_CHARPOS (*it) >= BEGV);
5899 /* Move IT by a specified amount of pixel lines DY. DY negative means
5900 move backwards. DY = 0 means move to start of screen line. At the
5901 end, IT will be on the start of a screen line. */
5903 void
5904 move_it_vertically (it, dy)
5905 struct it *it;
5906 int dy;
5908 if (dy <= 0)
5909 move_it_vertically_backward (it, -dy);
5910 else if (dy > 0)
5912 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5913 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5914 MOVE_TO_POS | MOVE_TO_Y);
5915 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5917 /* If buffer ends in ZV without a newline, move to the start of
5918 the line to satisfy the post-condition. */
5919 if (IT_CHARPOS (*it) == ZV
5920 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5921 move_it_by_lines (it, 0, 0);
5926 /* Move iterator IT past the end of the text line it is in. */
5928 void
5929 move_it_past_eol (it)
5930 struct it *it;
5932 enum move_it_result rc;
5934 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5935 if (rc == MOVE_NEWLINE_OR_CR)
5936 set_iterator_to_next (it, 0);
5940 #if 0 /* Currently not used. */
5942 /* Return non-zero if some text between buffer positions START_CHARPOS
5943 and END_CHARPOS is invisible. IT->window is the window for text
5944 property lookup. */
5946 static int
5947 invisible_text_between_p (it, start_charpos, end_charpos)
5948 struct it *it;
5949 int start_charpos, end_charpos;
5951 Lisp_Object prop, limit;
5952 int invisible_found_p;
5954 xassert (it != NULL && start_charpos <= end_charpos);
5956 /* Is text at START invisible? */
5957 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5958 it->window);
5959 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5960 invisible_found_p = 1;
5961 else
5963 limit = Fnext_single_char_property_change (make_number (start_charpos),
5964 Qinvisible, Qnil,
5965 make_number (end_charpos));
5966 invisible_found_p = XFASTINT (limit) < end_charpos;
5969 return invisible_found_p;
5972 #endif /* 0 */
5975 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5976 negative means move up. DVPOS == 0 means move to the start of the
5977 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5978 NEED_Y_P is zero, IT->current_y will be left unchanged.
5980 Further optimization ideas: If we would know that IT->f doesn't use
5981 a face with proportional font, we could be faster for
5982 truncate-lines nil. */
5984 void
5985 move_it_by_lines (it, dvpos, need_y_p)
5986 struct it *it;
5987 int dvpos, need_y_p;
5989 struct position pos;
5991 if (!FRAME_WINDOW_P (it->f))
5993 struct text_pos textpos;
5995 /* We can use vmotion on frames without proportional fonts. */
5996 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5997 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5998 reseat (it, textpos, 1);
5999 it->vpos += pos.vpos;
6000 it->current_y += pos.vpos;
6002 else if (dvpos == 0)
6004 /* DVPOS == 0 means move to the start of the screen line. */
6005 move_it_vertically_backward (it, 0);
6006 xassert (it->current_x == 0 && it->hpos == 0);
6008 else if (dvpos > 0)
6009 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6010 else
6012 struct it it2;
6013 int start_charpos, i;
6015 /* Start at the beginning of the screen line containing IT's
6016 position. */
6017 move_it_vertically_backward (it, 0);
6019 /* Go back -DVPOS visible lines and reseat the iterator there. */
6020 start_charpos = IT_CHARPOS (*it);
6021 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6022 back_to_previous_visible_line_start (it);
6023 reseat (it, it->current.pos, 1);
6024 it->current_x = it->hpos = 0;
6026 /* Above call may have moved too far if continuation lines
6027 are involved. Scan forward and see if it did. */
6028 it2 = *it;
6029 it2.vpos = it2.current_y = 0;
6030 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6031 it->vpos -= it2.vpos;
6032 it->current_y -= it2.current_y;
6033 it->current_x = it->hpos = 0;
6035 /* If we moved too far, move IT some lines forward. */
6036 if (it2.vpos > -dvpos)
6038 int delta = it2.vpos + dvpos;
6039 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6044 /* Return 1 if IT points into the middle of a display vector. */
6047 in_display_vector_p (it)
6048 struct it *it;
6050 return (it->method == next_element_from_display_vector
6051 && it->current.dpvec_index > 0
6052 && it->dpvec + it->current.dpvec_index != it->dpend);
6056 /***********************************************************************
6057 Messages
6058 ***********************************************************************/
6061 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6062 to *Messages*. */
6064 void
6065 add_to_log (format, arg1, arg2)
6066 char *format;
6067 Lisp_Object arg1, arg2;
6069 Lisp_Object args[3];
6070 Lisp_Object msg, fmt;
6071 char *buffer;
6072 int len;
6073 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6075 /* Do nothing if called asynchronously. Inserting text into
6076 a buffer may call after-change-functions and alike and
6077 that would means running Lisp asynchronously. */
6078 if (handling_signal)
6079 return;
6081 fmt = msg = Qnil;
6082 GCPRO4 (fmt, msg, arg1, arg2);
6084 args[0] = fmt = build_string (format);
6085 args[1] = arg1;
6086 args[2] = arg2;
6087 msg = Fformat (3, args);
6089 len = SBYTES (msg) + 1;
6090 buffer = (char *) alloca (len);
6091 bcopy (SDATA (msg), buffer, len);
6093 message_dolog (buffer, len - 1, 1, 0);
6094 UNGCPRO;
6098 /* Output a newline in the *Messages* buffer if "needs" one. */
6100 void
6101 message_log_maybe_newline ()
6103 if (message_log_need_newline)
6104 message_dolog ("", 0, 1, 0);
6108 /* Add a string M of length NBYTES to the message log, optionally
6109 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6110 nonzero, means interpret the contents of M as multibyte. This
6111 function calls low-level routines in order to bypass text property
6112 hooks, etc. which might not be safe to run. */
6114 void
6115 message_dolog (m, nbytes, nlflag, multibyte)
6116 const char *m;
6117 int nbytes, nlflag, multibyte;
6119 if (!NILP (Vmemory_full))
6120 return;
6122 if (!NILP (Vmessage_log_max))
6124 struct buffer *oldbuf;
6125 Lisp_Object oldpoint, oldbegv, oldzv;
6126 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6127 int point_at_end = 0;
6128 int zv_at_end = 0;
6129 Lisp_Object old_deactivate_mark, tem;
6130 struct gcpro gcpro1;
6132 old_deactivate_mark = Vdeactivate_mark;
6133 oldbuf = current_buffer;
6134 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6135 current_buffer->undo_list = Qt;
6137 oldpoint = message_dolog_marker1;
6138 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6139 oldbegv = message_dolog_marker2;
6140 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6141 oldzv = message_dolog_marker3;
6142 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6143 GCPRO1 (old_deactivate_mark);
6145 if (PT == Z)
6146 point_at_end = 1;
6147 if (ZV == Z)
6148 zv_at_end = 1;
6150 BEGV = BEG;
6151 BEGV_BYTE = BEG_BYTE;
6152 ZV = Z;
6153 ZV_BYTE = Z_BYTE;
6154 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6156 /* Insert the string--maybe converting multibyte to single byte
6157 or vice versa, so that all the text fits the buffer. */
6158 if (multibyte
6159 && NILP (current_buffer->enable_multibyte_characters))
6161 int i, c, char_bytes;
6162 unsigned char work[1];
6164 /* Convert a multibyte string to single-byte
6165 for the *Message* buffer. */
6166 for (i = 0; i < nbytes; i += char_bytes)
6168 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6169 work[0] = (SINGLE_BYTE_CHAR_P (c)
6171 : multibyte_char_to_unibyte (c, Qnil));
6172 insert_1_both (work, 1, 1, 1, 0, 0);
6175 else if (! multibyte
6176 && ! NILP (current_buffer->enable_multibyte_characters))
6178 int i, c, char_bytes;
6179 unsigned char *msg = (unsigned char *) m;
6180 unsigned char str[MAX_MULTIBYTE_LENGTH];
6181 /* Convert a single-byte string to multibyte
6182 for the *Message* buffer. */
6183 for (i = 0; i < nbytes; i++)
6185 c = unibyte_char_to_multibyte (msg[i]);
6186 char_bytes = CHAR_STRING (c, str);
6187 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6190 else if (nbytes)
6191 insert_1 (m, nbytes, 1, 0, 0);
6193 if (nlflag)
6195 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6196 insert_1 ("\n", 1, 1, 0, 0);
6198 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6199 this_bol = PT;
6200 this_bol_byte = PT_BYTE;
6202 /* See if this line duplicates the previous one.
6203 If so, combine duplicates. */
6204 if (this_bol > BEG)
6206 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6207 prev_bol = PT;
6208 prev_bol_byte = PT_BYTE;
6210 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6211 this_bol, this_bol_byte);
6212 if (dup)
6214 del_range_both (prev_bol, prev_bol_byte,
6215 this_bol, this_bol_byte, 0);
6216 if (dup > 1)
6218 char dupstr[40];
6219 int duplen;
6221 /* If you change this format, don't forget to also
6222 change message_log_check_duplicate. */
6223 sprintf (dupstr, " [%d times]", dup);
6224 duplen = strlen (dupstr);
6225 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6226 insert_1 (dupstr, duplen, 1, 0, 1);
6231 /* If we have more than the desired maximum number of lines
6232 in the *Messages* buffer now, delete the oldest ones.
6233 This is safe because we don't have undo in this buffer. */
6235 if (NATNUMP (Vmessage_log_max))
6237 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6238 -XFASTINT (Vmessage_log_max) - 1, 0);
6239 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6242 BEGV = XMARKER (oldbegv)->charpos;
6243 BEGV_BYTE = marker_byte_position (oldbegv);
6245 if (zv_at_end)
6247 ZV = Z;
6248 ZV_BYTE = Z_BYTE;
6250 else
6252 ZV = XMARKER (oldzv)->charpos;
6253 ZV_BYTE = marker_byte_position (oldzv);
6256 if (point_at_end)
6257 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6258 else
6259 /* We can't do Fgoto_char (oldpoint) because it will run some
6260 Lisp code. */
6261 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6262 XMARKER (oldpoint)->bytepos);
6264 UNGCPRO;
6265 unchain_marker (oldpoint);
6266 unchain_marker (oldbegv);
6267 unchain_marker (oldzv);
6269 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6270 set_buffer_internal (oldbuf);
6271 if (NILP (tem))
6272 windows_or_buffers_changed = old_windows_or_buffers_changed;
6273 message_log_need_newline = !nlflag;
6274 Vdeactivate_mark = old_deactivate_mark;
6279 /* We are at the end of the buffer after just having inserted a newline.
6280 (Note: We depend on the fact we won't be crossing the gap.)
6281 Check to see if the most recent message looks a lot like the previous one.
6282 Return 0 if different, 1 if the new one should just replace it, or a
6283 value N > 1 if we should also append " [N times]". */
6285 static int
6286 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6287 int prev_bol, this_bol;
6288 int prev_bol_byte, this_bol_byte;
6290 int i;
6291 int len = Z_BYTE - 1 - this_bol_byte;
6292 int seen_dots = 0;
6293 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6294 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6296 for (i = 0; i < len; i++)
6298 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6299 seen_dots = 1;
6300 if (p1[i] != p2[i])
6301 return seen_dots;
6303 p1 += len;
6304 if (*p1 == '\n')
6305 return 2;
6306 if (*p1++ == ' ' && *p1++ == '[')
6308 int n = 0;
6309 while (*p1 >= '0' && *p1 <= '9')
6310 n = n * 10 + *p1++ - '0';
6311 if (strncmp (p1, " times]\n", 8) == 0)
6312 return n+1;
6314 return 0;
6318 /* Display an echo area message M with a specified length of NBYTES
6319 bytes. The string may include null characters. If M is 0, clear
6320 out any existing message, and let the mini-buffer text show
6321 through.
6323 The buffer M must continue to exist until after the echo area gets
6324 cleared or some other message gets displayed there. This means do
6325 not pass text that is stored in a Lisp string; do not pass text in
6326 a buffer that was alloca'd. */
6328 void
6329 message2 (m, nbytes, multibyte)
6330 const char *m;
6331 int nbytes;
6332 int multibyte;
6334 /* First flush out any partial line written with print. */
6335 message_log_maybe_newline ();
6336 if (m)
6337 message_dolog (m, nbytes, 1, multibyte);
6338 message2_nolog (m, nbytes, multibyte);
6342 /* The non-logging counterpart of message2. */
6344 void
6345 message2_nolog (m, nbytes, multibyte)
6346 const char *m;
6347 int nbytes, multibyte;
6349 struct frame *sf = SELECTED_FRAME ();
6350 message_enable_multibyte = multibyte;
6352 if (noninteractive)
6354 if (noninteractive_need_newline)
6355 putc ('\n', stderr);
6356 noninteractive_need_newline = 0;
6357 if (m)
6358 fwrite (m, nbytes, 1, stderr);
6359 if (cursor_in_echo_area == 0)
6360 fprintf (stderr, "\n");
6361 fflush (stderr);
6363 /* A null message buffer means that the frame hasn't really been
6364 initialized yet. Error messages get reported properly by
6365 cmd_error, so this must be just an informative message; toss it. */
6366 else if (INTERACTIVE
6367 && sf->glyphs_initialized_p
6368 && FRAME_MESSAGE_BUF (sf))
6370 Lisp_Object mini_window;
6371 struct frame *f;
6373 /* Get the frame containing the mini-buffer
6374 that the selected frame is using. */
6375 mini_window = FRAME_MINIBUF_WINDOW (sf);
6376 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6378 FRAME_SAMPLE_VISIBILITY (f);
6379 if (FRAME_VISIBLE_P (sf)
6380 && ! FRAME_VISIBLE_P (f))
6381 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6383 if (m)
6385 set_message (m, Qnil, nbytes, multibyte);
6386 if (minibuffer_auto_raise)
6387 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6389 else
6390 clear_message (1, 1);
6392 do_pending_window_change (0);
6393 echo_area_display (1);
6394 do_pending_window_change (0);
6395 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6396 (*frame_up_to_date_hook) (f);
6401 /* Display an echo area message M with a specified length of NBYTES
6402 bytes. The string may include null characters. If M is not a
6403 string, clear out any existing message, and let the mini-buffer
6404 text show through. */
6406 void
6407 message3 (m, nbytes, multibyte)
6408 Lisp_Object m;
6409 int nbytes;
6410 int multibyte;
6412 struct gcpro gcpro1;
6414 GCPRO1 (m);
6416 /* First flush out any partial line written with print. */
6417 message_log_maybe_newline ();
6418 if (STRINGP (m))
6419 message_dolog (SDATA (m), nbytes, 1, multibyte);
6420 message3_nolog (m, nbytes, multibyte);
6422 UNGCPRO;
6426 /* The non-logging version of message3. */
6428 void
6429 message3_nolog (m, nbytes, multibyte)
6430 Lisp_Object m;
6431 int nbytes, multibyte;
6433 struct frame *sf = SELECTED_FRAME ();
6434 message_enable_multibyte = multibyte;
6436 if (noninteractive)
6438 if (noninteractive_need_newline)
6439 putc ('\n', stderr);
6440 noninteractive_need_newline = 0;
6441 if (STRINGP (m))
6442 fwrite (SDATA (m), nbytes, 1, stderr);
6443 if (cursor_in_echo_area == 0)
6444 fprintf (stderr, "\n");
6445 fflush (stderr);
6447 /* A null message buffer means that the frame hasn't really been
6448 initialized yet. Error messages get reported properly by
6449 cmd_error, so this must be just an informative message; toss it. */
6450 else if (INTERACTIVE
6451 && sf->glyphs_initialized_p
6452 && FRAME_MESSAGE_BUF (sf))
6454 Lisp_Object mini_window;
6455 Lisp_Object frame;
6456 struct frame *f;
6458 /* Get the frame containing the mini-buffer
6459 that the selected frame is using. */
6460 mini_window = FRAME_MINIBUF_WINDOW (sf);
6461 frame = XWINDOW (mini_window)->frame;
6462 f = XFRAME (frame);
6464 FRAME_SAMPLE_VISIBILITY (f);
6465 if (FRAME_VISIBLE_P (sf)
6466 && !FRAME_VISIBLE_P (f))
6467 Fmake_frame_visible (frame);
6469 if (STRINGP (m) && SCHARS (m) > 0)
6471 set_message (NULL, m, nbytes, multibyte);
6472 if (minibuffer_auto_raise)
6473 Fraise_frame (frame);
6475 else
6476 clear_message (1, 1);
6478 do_pending_window_change (0);
6479 echo_area_display (1);
6480 do_pending_window_change (0);
6481 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6482 (*frame_up_to_date_hook) (f);
6487 /* Display a null-terminated echo area message M. If M is 0, clear
6488 out any existing message, and let the mini-buffer text show through.
6490 The buffer M must continue to exist until after the echo area gets
6491 cleared or some other message gets displayed there. Do not pass
6492 text that is stored in a Lisp string. Do not pass text in a buffer
6493 that was alloca'd. */
6495 void
6496 message1 (m)
6497 char *m;
6499 message2 (m, (m ? strlen (m) : 0), 0);
6503 /* The non-logging counterpart of message1. */
6505 void
6506 message1_nolog (m)
6507 char *m;
6509 message2_nolog (m, (m ? strlen (m) : 0), 0);
6512 /* Display a message M which contains a single %s
6513 which gets replaced with STRING. */
6515 void
6516 message_with_string (m, string, log)
6517 char *m;
6518 Lisp_Object string;
6519 int log;
6521 CHECK_STRING (string);
6523 if (noninteractive)
6525 if (m)
6527 if (noninteractive_need_newline)
6528 putc ('\n', stderr);
6529 noninteractive_need_newline = 0;
6530 fprintf (stderr, m, SDATA (string));
6531 if (cursor_in_echo_area == 0)
6532 fprintf (stderr, "\n");
6533 fflush (stderr);
6536 else if (INTERACTIVE)
6538 /* The frame whose minibuffer we're going to display the message on.
6539 It may be larger than the selected frame, so we need
6540 to use its buffer, not the selected frame's buffer. */
6541 Lisp_Object mini_window;
6542 struct frame *f, *sf = SELECTED_FRAME ();
6544 /* Get the frame containing the minibuffer
6545 that the selected frame is using. */
6546 mini_window = FRAME_MINIBUF_WINDOW (sf);
6547 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6549 /* A null message buffer means that the frame hasn't really been
6550 initialized yet. Error messages get reported properly by
6551 cmd_error, so this must be just an informative message; toss it. */
6552 if (FRAME_MESSAGE_BUF (f))
6554 Lisp_Object args[2], message;
6555 struct gcpro gcpro1, gcpro2;
6557 args[0] = build_string (m);
6558 args[1] = message = string;
6559 GCPRO2 (args[0], message);
6560 gcpro1.nvars = 2;
6562 message = Fformat (2, args);
6564 if (log)
6565 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6566 else
6567 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6569 UNGCPRO;
6571 /* Print should start at the beginning of the message
6572 buffer next time. */
6573 message_buf_print = 0;
6579 /* Dump an informative message to the minibuf. If M is 0, clear out
6580 any existing message, and let the mini-buffer text show through. */
6582 /* VARARGS 1 */
6583 void
6584 message (m, a1, a2, a3)
6585 char *m;
6586 EMACS_INT a1, a2, a3;
6588 if (noninteractive)
6590 if (m)
6592 if (noninteractive_need_newline)
6593 putc ('\n', stderr);
6594 noninteractive_need_newline = 0;
6595 fprintf (stderr, m, a1, a2, a3);
6596 if (cursor_in_echo_area == 0)
6597 fprintf (stderr, "\n");
6598 fflush (stderr);
6601 else if (INTERACTIVE)
6603 /* The frame whose mini-buffer we're going to display the message
6604 on. It may be larger than the selected frame, so we need to
6605 use its buffer, not the selected frame's buffer. */
6606 Lisp_Object mini_window;
6607 struct frame *f, *sf = SELECTED_FRAME ();
6609 /* Get the frame containing the mini-buffer
6610 that the selected frame is using. */
6611 mini_window = FRAME_MINIBUF_WINDOW (sf);
6612 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6614 /* A null message buffer means that the frame hasn't really been
6615 initialized yet. Error messages get reported properly by
6616 cmd_error, so this must be just an informative message; toss
6617 it. */
6618 if (FRAME_MESSAGE_BUF (f))
6620 if (m)
6622 int len;
6623 #ifdef NO_ARG_ARRAY
6624 char *a[3];
6625 a[0] = (char *) a1;
6626 a[1] = (char *) a2;
6627 a[2] = (char *) a3;
6629 len = doprnt (FRAME_MESSAGE_BUF (f),
6630 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6631 #else
6632 len = doprnt (FRAME_MESSAGE_BUF (f),
6633 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6634 (char **) &a1);
6635 #endif /* NO_ARG_ARRAY */
6637 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6639 else
6640 message1 (0);
6642 /* Print should start at the beginning of the message
6643 buffer next time. */
6644 message_buf_print = 0;
6650 /* The non-logging version of message. */
6652 void
6653 message_nolog (m, a1, a2, a3)
6654 char *m;
6655 EMACS_INT a1, a2, a3;
6657 Lisp_Object old_log_max;
6658 old_log_max = Vmessage_log_max;
6659 Vmessage_log_max = Qnil;
6660 message (m, a1, a2, a3);
6661 Vmessage_log_max = old_log_max;
6665 /* Display the current message in the current mini-buffer. This is
6666 only called from error handlers in process.c, and is not time
6667 critical. */
6669 void
6670 update_echo_area ()
6672 if (!NILP (echo_area_buffer[0]))
6674 Lisp_Object string;
6675 string = Fcurrent_message ();
6676 message3 (string, SBYTES (string),
6677 !NILP (current_buffer->enable_multibyte_characters));
6682 /* Make sure echo area buffers in `echo_buffers' are live.
6683 If they aren't, make new ones. */
6685 static void
6686 ensure_echo_area_buffers ()
6688 int i;
6690 for (i = 0; i < 2; ++i)
6691 if (!BUFFERP (echo_buffer[i])
6692 || NILP (XBUFFER (echo_buffer[i])->name))
6694 char name[30];
6695 Lisp_Object old_buffer;
6696 int j;
6698 old_buffer = echo_buffer[i];
6699 sprintf (name, " *Echo Area %d*", i);
6700 echo_buffer[i] = Fget_buffer_create (build_string (name));
6701 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6703 for (j = 0; j < 2; ++j)
6704 if (EQ (old_buffer, echo_area_buffer[j]))
6705 echo_area_buffer[j] = echo_buffer[i];
6710 /* Call FN with args A1..A4 with either the current or last displayed
6711 echo_area_buffer as current buffer.
6713 WHICH zero means use the current message buffer
6714 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6715 from echo_buffer[] and clear it.
6717 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6718 suitable buffer from echo_buffer[] and clear it.
6720 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6721 that the current message becomes the last displayed one, make
6722 choose a suitable buffer for echo_area_buffer[0], and clear it.
6724 Value is what FN returns. */
6726 static int
6727 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6728 struct window *w;
6729 int which;
6730 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6731 EMACS_INT a1;
6732 Lisp_Object a2;
6733 EMACS_INT a3, a4;
6735 Lisp_Object buffer;
6736 int this_one, the_other, clear_buffer_p, rc;
6737 int count = SPECPDL_INDEX ();
6739 /* If buffers aren't live, make new ones. */
6740 ensure_echo_area_buffers ();
6742 clear_buffer_p = 0;
6744 if (which == 0)
6745 this_one = 0, the_other = 1;
6746 else if (which > 0)
6747 this_one = 1, the_other = 0;
6748 else
6750 this_one = 0, the_other = 1;
6751 clear_buffer_p = 1;
6753 /* We need a fresh one in case the current echo buffer equals
6754 the one containing the last displayed echo area message. */
6755 if (!NILP (echo_area_buffer[this_one])
6756 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6757 echo_area_buffer[this_one] = Qnil;
6760 /* Choose a suitable buffer from echo_buffer[] is we don't
6761 have one. */
6762 if (NILP (echo_area_buffer[this_one]))
6764 echo_area_buffer[this_one]
6765 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6766 ? echo_buffer[the_other]
6767 : echo_buffer[this_one]);
6768 clear_buffer_p = 1;
6771 buffer = echo_area_buffer[this_one];
6773 /* Don't get confused by reusing the buffer used for echoing
6774 for a different purpose. */
6775 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6776 cancel_echoing ();
6778 record_unwind_protect (unwind_with_echo_area_buffer,
6779 with_echo_area_buffer_unwind_data (w));
6781 /* Make the echo area buffer current. Note that for display
6782 purposes, it is not necessary that the displayed window's buffer
6783 == current_buffer, except for text property lookup. So, let's
6784 only set that buffer temporarily here without doing a full
6785 Fset_window_buffer. We must also change w->pointm, though,
6786 because otherwise an assertions in unshow_buffer fails, and Emacs
6787 aborts. */
6788 set_buffer_internal_1 (XBUFFER (buffer));
6789 if (w)
6791 w->buffer = buffer;
6792 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6795 current_buffer->undo_list = Qt;
6796 current_buffer->read_only = Qnil;
6797 specbind (Qinhibit_read_only, Qt);
6798 specbind (Qinhibit_modification_hooks, Qt);
6800 if (clear_buffer_p && Z > BEG)
6801 del_range (BEG, Z);
6803 xassert (BEGV >= BEG);
6804 xassert (ZV <= Z && ZV >= BEGV);
6806 rc = fn (a1, a2, a3, a4);
6808 xassert (BEGV >= BEG);
6809 xassert (ZV <= Z && ZV >= BEGV);
6811 unbind_to (count, Qnil);
6812 return rc;
6816 /* Save state that should be preserved around the call to the function
6817 FN called in with_echo_area_buffer. */
6819 static Lisp_Object
6820 with_echo_area_buffer_unwind_data (w)
6821 struct window *w;
6823 int i = 0;
6824 Lisp_Object vector;
6826 /* Reduce consing by keeping one vector in
6827 Vwith_echo_area_save_vector. */
6828 vector = Vwith_echo_area_save_vector;
6829 Vwith_echo_area_save_vector = Qnil;
6831 if (NILP (vector))
6832 vector = Fmake_vector (make_number (7), Qnil);
6834 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6835 AREF (vector, i) = Vdeactivate_mark, ++i;
6836 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6838 if (w)
6840 XSETWINDOW (AREF (vector, i), w); ++i;
6841 AREF (vector, i) = w->buffer; ++i;
6842 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6843 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6845 else
6847 int end = i + 4;
6848 for (; i < end; ++i)
6849 AREF (vector, i) = Qnil;
6852 xassert (i == ASIZE (vector));
6853 return vector;
6857 /* Restore global state from VECTOR which was created by
6858 with_echo_area_buffer_unwind_data. */
6860 static Lisp_Object
6861 unwind_with_echo_area_buffer (vector)
6862 Lisp_Object vector;
6864 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6865 Vdeactivate_mark = AREF (vector, 1);
6866 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6868 if (WINDOWP (AREF (vector, 3)))
6870 struct window *w;
6871 Lisp_Object buffer, charpos, bytepos;
6873 w = XWINDOW (AREF (vector, 3));
6874 buffer = AREF (vector, 4);
6875 charpos = AREF (vector, 5);
6876 bytepos = AREF (vector, 6);
6878 w->buffer = buffer;
6879 set_marker_both (w->pointm, buffer,
6880 XFASTINT (charpos), XFASTINT (bytepos));
6883 Vwith_echo_area_save_vector = vector;
6884 return Qnil;
6888 /* Set up the echo area for use by print functions. MULTIBYTE_P
6889 non-zero means we will print multibyte. */
6891 void
6892 setup_echo_area_for_printing (multibyte_p)
6893 int multibyte_p;
6895 /* If we can't find an echo area any more, exit. */
6896 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6897 Fkill_emacs (Qnil);
6899 ensure_echo_area_buffers ();
6901 if (!message_buf_print)
6903 /* A message has been output since the last time we printed.
6904 Choose a fresh echo area buffer. */
6905 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6906 echo_area_buffer[0] = echo_buffer[1];
6907 else
6908 echo_area_buffer[0] = echo_buffer[0];
6910 /* Switch to that buffer and clear it. */
6911 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6912 current_buffer->truncate_lines = Qnil;
6914 if (Z > BEG)
6916 int count = SPECPDL_INDEX ();
6917 specbind (Qinhibit_read_only, Qt);
6918 /* Note that undo recording is always disabled. */
6919 del_range (BEG, Z);
6920 unbind_to (count, Qnil);
6922 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6924 /* Set up the buffer for the multibyteness we need. */
6925 if (multibyte_p
6926 != !NILP (current_buffer->enable_multibyte_characters))
6927 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6929 /* Raise the frame containing the echo area. */
6930 if (minibuffer_auto_raise)
6932 struct frame *sf = SELECTED_FRAME ();
6933 Lisp_Object mini_window;
6934 mini_window = FRAME_MINIBUF_WINDOW (sf);
6935 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6938 message_log_maybe_newline ();
6939 message_buf_print = 1;
6941 else
6943 if (NILP (echo_area_buffer[0]))
6945 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6946 echo_area_buffer[0] = echo_buffer[1];
6947 else
6948 echo_area_buffer[0] = echo_buffer[0];
6951 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6953 /* Someone switched buffers between print requests. */
6954 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6955 current_buffer->truncate_lines = Qnil;
6961 /* Display an echo area message in window W. Value is non-zero if W's
6962 height is changed. If display_last_displayed_message_p is
6963 non-zero, display the message that was last displayed, otherwise
6964 display the current message. */
6966 static int
6967 display_echo_area (w)
6968 struct window *w;
6970 int i, no_message_p, window_height_changed_p, count;
6972 /* Temporarily disable garbage collections while displaying the echo
6973 area. This is done because a GC can print a message itself.
6974 That message would modify the echo area buffer's contents while a
6975 redisplay of the buffer is going on, and seriously confuse
6976 redisplay. */
6977 count = inhibit_garbage_collection ();
6979 /* If there is no message, we must call display_echo_area_1
6980 nevertheless because it resizes the window. But we will have to
6981 reset the echo_area_buffer in question to nil at the end because
6982 with_echo_area_buffer will sets it to an empty buffer. */
6983 i = display_last_displayed_message_p ? 1 : 0;
6984 no_message_p = NILP (echo_area_buffer[i]);
6986 window_height_changed_p
6987 = with_echo_area_buffer (w, display_last_displayed_message_p,
6988 display_echo_area_1,
6989 (EMACS_INT) w, Qnil, 0, 0);
6991 if (no_message_p)
6992 echo_area_buffer[i] = Qnil;
6994 unbind_to (count, Qnil);
6995 return window_height_changed_p;
6999 /* Helper for display_echo_area. Display the current buffer which
7000 contains the current echo area message in window W, a mini-window,
7001 a pointer to which is passed in A1. A2..A4 are currently not used.
7002 Change the height of W so that all of the message is displayed.
7003 Value is non-zero if height of W was changed. */
7005 static int
7006 display_echo_area_1 (a1, a2, a3, a4)
7007 EMACS_INT a1;
7008 Lisp_Object a2;
7009 EMACS_INT a3, a4;
7011 struct window *w = (struct window *) a1;
7012 Lisp_Object window;
7013 struct text_pos start;
7014 int window_height_changed_p = 0;
7016 /* Do this before displaying, so that we have a large enough glyph
7017 matrix for the display. */
7018 window_height_changed_p = resize_mini_window (w, 0);
7020 /* Display. */
7021 clear_glyph_matrix (w->desired_matrix);
7022 XSETWINDOW (window, w);
7023 SET_TEXT_POS (start, BEG, BEG_BYTE);
7024 try_window (window, start);
7026 return window_height_changed_p;
7030 /* Resize the echo area window to exactly the size needed for the
7031 currently displayed message, if there is one. If a mini-buffer
7032 is active, don't shrink it. */
7034 void
7035 resize_echo_area_exactly ()
7037 if (BUFFERP (echo_area_buffer[0])
7038 && WINDOWP (echo_area_window))
7040 struct window *w = XWINDOW (echo_area_window);
7041 int resized_p;
7042 Lisp_Object resize_exactly;
7044 if (minibuf_level == 0)
7045 resize_exactly = Qt;
7046 else
7047 resize_exactly = Qnil;
7049 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7050 (EMACS_INT) w, resize_exactly, 0, 0);
7051 if (resized_p)
7053 ++windows_or_buffers_changed;
7054 ++update_mode_lines;
7055 redisplay_internal (0);
7061 /* Callback function for with_echo_area_buffer, when used from
7062 resize_echo_area_exactly. A1 contains a pointer to the window to
7063 resize, EXACTLY non-nil means resize the mini-window exactly to the
7064 size of the text displayed. A3 and A4 are not used. Value is what
7065 resize_mini_window returns. */
7067 static int
7068 resize_mini_window_1 (a1, exactly, a3, a4)
7069 EMACS_INT a1;
7070 Lisp_Object exactly;
7071 EMACS_INT a3, a4;
7073 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7077 /* Resize mini-window W to fit the size of its contents. EXACT:P
7078 means size the window exactly to the size needed. Otherwise, it's
7079 only enlarged until W's buffer is empty. Value is non-zero if
7080 the window height has been changed. */
7083 resize_mini_window (w, exact_p)
7084 struct window *w;
7085 int exact_p;
7087 struct frame *f = XFRAME (w->frame);
7088 int window_height_changed_p = 0;
7090 xassert (MINI_WINDOW_P (w));
7092 /* Don't resize windows while redisplaying a window; it would
7093 confuse redisplay functions when the size of the window they are
7094 displaying changes from under them. Such a resizing can happen,
7095 for instance, when which-func prints a long message while
7096 we are running fontification-functions. We're running these
7097 functions with safe_call which binds inhibit-redisplay to t. */
7098 if (!NILP (Vinhibit_redisplay))
7099 return 0;
7101 /* Nil means don't try to resize. */
7102 if (NILP (Vresize_mini_windows)
7103 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7104 return 0;
7106 if (!FRAME_MINIBUF_ONLY_P (f))
7108 struct it it;
7109 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7110 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
7111 int height, max_height;
7112 int unit = CANON_Y_UNIT (f);
7113 struct text_pos start;
7114 struct buffer *old_current_buffer = NULL;
7116 if (current_buffer != XBUFFER (w->buffer))
7118 old_current_buffer = current_buffer;
7119 set_buffer_internal (XBUFFER (w->buffer));
7122 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7124 /* Compute the max. number of lines specified by the user. */
7125 if (FLOATP (Vmax_mini_window_height))
7126 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
7127 else if (INTEGERP (Vmax_mini_window_height))
7128 max_height = XINT (Vmax_mini_window_height);
7129 else
7130 max_height = total_height / 4;
7132 /* Correct that max. height if it's bogus. */
7133 max_height = max (1, max_height);
7134 max_height = min (total_height, max_height);
7136 /* Find out the height of the text in the window. */
7137 if (it.truncate_lines_p)
7138 height = 1;
7139 else
7141 last_height = 0;
7142 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7143 if (it.max_ascent == 0 && it.max_descent == 0)
7144 height = it.current_y + last_height;
7145 else
7146 height = it.current_y + it.max_ascent + it.max_descent;
7147 height -= it.extra_line_spacing;
7148 height = (height + unit - 1) / unit;
7151 /* Compute a suitable window start. */
7152 if (height > max_height)
7154 height = max_height;
7155 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7156 move_it_vertically_backward (&it, (height - 1) * unit);
7157 start = it.current.pos;
7159 else
7160 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7161 SET_MARKER_FROM_TEXT_POS (w->start, start);
7163 if (EQ (Vresize_mini_windows, Qgrow_only))
7165 /* Let it grow only, until we display an empty message, in which
7166 case the window shrinks again. */
7167 if (height > XFASTINT (w->height))
7169 int old_height = XFASTINT (w->height);
7170 freeze_window_starts (f, 1);
7171 grow_mini_window (w, height - XFASTINT (w->height));
7172 window_height_changed_p = XFASTINT (w->height) != old_height;
7174 else if (height < XFASTINT (w->height)
7175 && (exact_p || BEGV == ZV))
7177 int old_height = XFASTINT (w->height);
7178 freeze_window_starts (f, 0);
7179 shrink_mini_window (w);
7180 window_height_changed_p = XFASTINT (w->height) != old_height;
7183 else
7185 /* Always resize to exact size needed. */
7186 if (height > XFASTINT (w->height))
7188 int old_height = XFASTINT (w->height);
7189 freeze_window_starts (f, 1);
7190 grow_mini_window (w, height - XFASTINT (w->height));
7191 window_height_changed_p = XFASTINT (w->height) != old_height;
7193 else if (height < XFASTINT (w->height))
7195 int old_height = XFASTINT (w->height);
7196 freeze_window_starts (f, 0);
7197 shrink_mini_window (w);
7199 if (height)
7201 freeze_window_starts (f, 1);
7202 grow_mini_window (w, height - XFASTINT (w->height));
7205 window_height_changed_p = XFASTINT (w->height) != old_height;
7209 if (old_current_buffer)
7210 set_buffer_internal (old_current_buffer);
7213 return window_height_changed_p;
7217 /* Value is the current message, a string, or nil if there is no
7218 current message. */
7220 Lisp_Object
7221 current_message ()
7223 Lisp_Object msg;
7225 if (NILP (echo_area_buffer[0]))
7226 msg = Qnil;
7227 else
7229 with_echo_area_buffer (0, 0, current_message_1,
7230 (EMACS_INT) &msg, Qnil, 0, 0);
7231 if (NILP (msg))
7232 echo_area_buffer[0] = Qnil;
7235 return msg;
7239 static int
7240 current_message_1 (a1, a2, a3, a4)
7241 EMACS_INT a1;
7242 Lisp_Object a2;
7243 EMACS_INT a3, a4;
7245 Lisp_Object *msg = (Lisp_Object *) a1;
7247 if (Z > BEG)
7248 *msg = make_buffer_string (BEG, Z, 1);
7249 else
7250 *msg = Qnil;
7251 return 0;
7255 /* Push the current message on Vmessage_stack for later restauration
7256 by restore_message. Value is non-zero if the current message isn't
7257 empty. This is a relatively infrequent operation, so it's not
7258 worth optimizing. */
7261 push_message ()
7263 Lisp_Object msg;
7264 msg = current_message ();
7265 Vmessage_stack = Fcons (msg, Vmessage_stack);
7266 return STRINGP (msg);
7270 /* Restore message display from the top of Vmessage_stack. */
7272 void
7273 restore_message ()
7275 Lisp_Object msg;
7277 xassert (CONSP (Vmessage_stack));
7278 msg = XCAR (Vmessage_stack);
7279 if (STRINGP (msg))
7280 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7281 else
7282 message3_nolog (msg, 0, 0);
7286 /* Handler for record_unwind_protect calling pop_message. */
7288 Lisp_Object
7289 pop_message_unwind (dummy)
7290 Lisp_Object dummy;
7292 pop_message ();
7293 return Qnil;
7296 /* Pop the top-most entry off Vmessage_stack. */
7298 void
7299 pop_message ()
7301 xassert (CONSP (Vmessage_stack));
7302 Vmessage_stack = XCDR (Vmessage_stack);
7306 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7307 exits. If the stack is not empty, we have a missing pop_message
7308 somewhere. */
7310 void
7311 check_message_stack ()
7313 if (!NILP (Vmessage_stack))
7314 abort ();
7318 /* Truncate to NCHARS what will be displayed in the echo area the next
7319 time we display it---but don't redisplay it now. */
7321 void
7322 truncate_echo_area (nchars)
7323 int nchars;
7325 if (nchars == 0)
7326 echo_area_buffer[0] = Qnil;
7327 /* A null message buffer means that the frame hasn't really been
7328 initialized yet. Error messages get reported properly by
7329 cmd_error, so this must be just an informative message; toss it. */
7330 else if (!noninteractive
7331 && INTERACTIVE
7332 && !NILP (echo_area_buffer[0]))
7334 struct frame *sf = SELECTED_FRAME ();
7335 if (FRAME_MESSAGE_BUF (sf))
7336 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7341 /* Helper function for truncate_echo_area. Truncate the current
7342 message to at most NCHARS characters. */
7344 static int
7345 truncate_message_1 (nchars, a2, a3, a4)
7346 EMACS_INT nchars;
7347 Lisp_Object a2;
7348 EMACS_INT a3, a4;
7350 if (BEG + nchars < Z)
7351 del_range (BEG + nchars, Z);
7352 if (Z == BEG)
7353 echo_area_buffer[0] = Qnil;
7354 return 0;
7358 /* Set the current message to a substring of S or STRING.
7360 If STRING is a Lisp string, set the message to the first NBYTES
7361 bytes from STRING. NBYTES zero means use the whole string. If
7362 STRING is multibyte, the message will be displayed multibyte.
7364 If S is not null, set the message to the first LEN bytes of S. LEN
7365 zero means use the whole string. MULTIBYTE_P non-zero means S is
7366 multibyte. Display the message multibyte in that case. */
7368 void
7369 set_message (s, string, nbytes, multibyte_p)
7370 const char *s;
7371 Lisp_Object string;
7372 int nbytes, multibyte_p;
7374 message_enable_multibyte
7375 = ((s && multibyte_p)
7376 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7378 with_echo_area_buffer (0, -1, set_message_1,
7379 (EMACS_INT) s, string, nbytes, multibyte_p);
7380 message_buf_print = 0;
7381 help_echo_showing_p = 0;
7385 /* Helper function for set_message. Arguments have the same meaning
7386 as there, with A1 corresponding to S and A2 corresponding to STRING
7387 This function is called with the echo area buffer being
7388 current. */
7390 static int
7391 set_message_1 (a1, a2, nbytes, multibyte_p)
7392 EMACS_INT a1;
7393 Lisp_Object a2;
7394 EMACS_INT nbytes, multibyte_p;
7396 const char *s = (const char *) a1;
7397 Lisp_Object string = a2;
7399 xassert (BEG == Z);
7401 /* Change multibyteness of the echo buffer appropriately. */
7402 if (message_enable_multibyte
7403 != !NILP (current_buffer->enable_multibyte_characters))
7404 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7406 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7408 /* Insert new message at BEG. */
7409 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7411 if (STRINGP (string))
7413 int nchars;
7415 if (nbytes == 0)
7416 nbytes = SBYTES (string);
7417 nchars = string_byte_to_char (string, nbytes);
7419 /* This function takes care of single/multibyte conversion. We
7420 just have to ensure that the echo area buffer has the right
7421 setting of enable_multibyte_characters. */
7422 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7424 else if (s)
7426 if (nbytes == 0)
7427 nbytes = strlen (s);
7429 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7431 /* Convert from multi-byte to single-byte. */
7432 int i, c, n;
7433 unsigned char work[1];
7435 /* Convert a multibyte string to single-byte. */
7436 for (i = 0; i < nbytes; i += n)
7438 c = string_char_and_length (s + i, nbytes - i, &n);
7439 work[0] = (SINGLE_BYTE_CHAR_P (c)
7441 : multibyte_char_to_unibyte (c, Qnil));
7442 insert_1_both (work, 1, 1, 1, 0, 0);
7445 else if (!multibyte_p
7446 && !NILP (current_buffer->enable_multibyte_characters))
7448 /* Convert from single-byte to multi-byte. */
7449 int i, c, n;
7450 const unsigned char *msg = (const unsigned char *) s;
7451 unsigned char str[MAX_MULTIBYTE_LENGTH];
7453 /* Convert a single-byte string to multibyte. */
7454 for (i = 0; i < nbytes; i++)
7456 c = unibyte_char_to_multibyte (msg[i]);
7457 n = CHAR_STRING (c, str);
7458 insert_1_both (str, 1, n, 1, 0, 0);
7461 else
7462 insert_1 (s, nbytes, 1, 0, 0);
7465 return 0;
7469 /* Clear messages. CURRENT_P non-zero means clear the current
7470 message. LAST_DISPLAYED_P non-zero means clear the message
7471 last displayed. */
7473 void
7474 clear_message (current_p, last_displayed_p)
7475 int current_p, last_displayed_p;
7477 if (current_p)
7479 echo_area_buffer[0] = Qnil;
7480 message_cleared_p = 1;
7483 if (last_displayed_p)
7484 echo_area_buffer[1] = Qnil;
7486 message_buf_print = 0;
7489 /* Clear garbaged frames.
7491 This function is used where the old redisplay called
7492 redraw_garbaged_frames which in turn called redraw_frame which in
7493 turn called clear_frame. The call to clear_frame was a source of
7494 flickering. I believe a clear_frame is not necessary. It should
7495 suffice in the new redisplay to invalidate all current matrices,
7496 and ensure a complete redisplay of all windows. */
7498 static void
7499 clear_garbaged_frames ()
7501 if (frame_garbaged)
7503 Lisp_Object tail, frame;
7504 int changed_count = 0;
7506 FOR_EACH_FRAME (tail, frame)
7508 struct frame *f = XFRAME (frame);
7510 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7512 if (f->resized_p)
7513 Fredraw_frame (frame);
7514 clear_current_matrices (f);
7515 changed_count++;
7516 f->garbaged = 0;
7517 f->resized_p = 0;
7521 frame_garbaged = 0;
7522 if (changed_count)
7523 ++windows_or_buffers_changed;
7528 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7529 is non-zero update selected_frame. Value is non-zero if the
7530 mini-windows height has been changed. */
7532 static int
7533 echo_area_display (update_frame_p)
7534 int update_frame_p;
7536 Lisp_Object mini_window;
7537 struct window *w;
7538 struct frame *f;
7539 int window_height_changed_p = 0;
7540 struct frame *sf = SELECTED_FRAME ();
7542 mini_window = FRAME_MINIBUF_WINDOW (sf);
7543 w = XWINDOW (mini_window);
7544 f = XFRAME (WINDOW_FRAME (w));
7546 /* Don't display if frame is invisible or not yet initialized. */
7547 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7548 return 0;
7550 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7551 #ifndef MAC_OS8
7552 #ifdef HAVE_WINDOW_SYSTEM
7553 /* When Emacs starts, selected_frame may be a visible terminal
7554 frame, even if we run under a window system. If we let this
7555 through, a message would be displayed on the terminal. */
7556 if (EQ (selected_frame, Vterminal_frame)
7557 && !NILP (Vwindow_system))
7558 return 0;
7559 #endif /* HAVE_WINDOW_SYSTEM */
7560 #endif
7562 /* Redraw garbaged frames. */
7563 if (frame_garbaged)
7564 clear_garbaged_frames ();
7566 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7568 echo_area_window = mini_window;
7569 window_height_changed_p = display_echo_area (w);
7570 w->must_be_updated_p = 1;
7572 /* Update the display, unless called from redisplay_internal.
7573 Also don't update the screen during redisplay itself. The
7574 update will happen at the end of redisplay, and an update
7575 here could cause confusion. */
7576 if (update_frame_p && !redisplaying_p)
7578 int n = 0;
7580 /* If the display update has been interrupted by pending
7581 input, update mode lines in the frame. Due to the
7582 pending input, it might have been that redisplay hasn't
7583 been called, so that mode lines above the echo area are
7584 garbaged. This looks odd, so we prevent it here. */
7585 if (!display_completed)
7586 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7588 if (window_height_changed_p
7589 /* Don't do this if Emacs is shutting down. Redisplay
7590 needs to run hooks. */
7591 && !NILP (Vrun_hooks))
7593 /* Must update other windows. Likewise as in other
7594 cases, don't let this update be interrupted by
7595 pending input. */
7596 int count = SPECPDL_INDEX ();
7597 specbind (Qredisplay_dont_pause, Qt);
7598 windows_or_buffers_changed = 1;
7599 redisplay_internal (0);
7600 unbind_to (count, Qnil);
7602 else if (FRAME_WINDOW_P (f) && n == 0)
7604 /* Window configuration is the same as before.
7605 Can do with a display update of the echo area,
7606 unless we displayed some mode lines. */
7607 update_single_window (w, 1);
7608 rif->flush_display (f);
7610 else
7611 update_frame (f, 1, 1);
7613 /* If cursor is in the echo area, make sure that the next
7614 redisplay displays the minibuffer, so that the cursor will
7615 be replaced with what the minibuffer wants. */
7616 if (cursor_in_echo_area)
7617 ++windows_or_buffers_changed;
7620 else if (!EQ (mini_window, selected_window))
7621 windows_or_buffers_changed++;
7623 /* Last displayed message is now the current message. */
7624 echo_area_buffer[1] = echo_area_buffer[0];
7626 /* Prevent redisplay optimization in redisplay_internal by resetting
7627 this_line_start_pos. This is done because the mini-buffer now
7628 displays the message instead of its buffer text. */
7629 if (EQ (mini_window, selected_window))
7630 CHARPOS (this_line_start_pos) = 0;
7632 return window_height_changed_p;
7637 /***********************************************************************
7638 Frame Titles
7639 ***********************************************************************/
7642 /* The frame title buffering code is also used by Fformat_mode_line.
7643 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7645 /* A buffer for constructing frame titles in it; allocated from the
7646 heap in init_xdisp and resized as needed in store_frame_title_char. */
7648 static char *frame_title_buf;
7650 /* The buffer's end, and a current output position in it. */
7652 static char *frame_title_buf_end;
7653 static char *frame_title_ptr;
7656 /* Store a single character C for the frame title in frame_title_buf.
7657 Re-allocate frame_title_buf if necessary. */
7659 static void
7660 #ifdef PROTOTYPES
7661 store_frame_title_char (char c)
7662 #else
7663 store_frame_title_char (c)
7664 char c;
7665 #endif
7667 /* If output position has reached the end of the allocated buffer,
7668 double the buffer's size. */
7669 if (frame_title_ptr == frame_title_buf_end)
7671 int len = frame_title_ptr - frame_title_buf;
7672 int new_size = 2 * len * sizeof *frame_title_buf;
7673 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7674 frame_title_buf_end = frame_title_buf + new_size;
7675 frame_title_ptr = frame_title_buf + len;
7678 *frame_title_ptr++ = c;
7682 /* Store part of a frame title in frame_title_buf, beginning at
7683 frame_title_ptr. STR is the string to store. Do not copy
7684 characters that yield more columns than PRECISION; PRECISION <= 0
7685 means copy the whole string. Pad with spaces until FIELD_WIDTH
7686 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7687 pad. Called from display_mode_element when it is used to build a
7688 frame title. */
7690 static int
7691 store_frame_title (str, field_width, precision)
7692 const unsigned char *str;
7693 int field_width, precision;
7695 int n = 0;
7696 int dummy, nbytes;
7698 /* Copy at most PRECISION chars from STR. */
7699 nbytes = strlen (str);
7700 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7701 while (nbytes--)
7702 store_frame_title_char (*str++);
7704 /* Fill up with spaces until FIELD_WIDTH reached. */
7705 while (field_width > 0
7706 && n < field_width)
7708 store_frame_title_char (' ');
7709 ++n;
7712 return n;
7715 #ifdef HAVE_WINDOW_SYSTEM
7717 /* Set the title of FRAME, if it has changed. The title format is
7718 Vicon_title_format if FRAME is iconified, otherwise it is
7719 frame_title_format. */
7721 static void
7722 x_consider_frame_title (frame)
7723 Lisp_Object frame;
7725 struct frame *f = XFRAME (frame);
7727 if (FRAME_WINDOW_P (f)
7728 || FRAME_MINIBUF_ONLY_P (f)
7729 || f->explicit_name)
7731 /* Do we have more than one visible frame on this X display? */
7732 Lisp_Object tail;
7733 Lisp_Object fmt;
7734 struct buffer *obuf;
7735 int len;
7736 struct it it;
7738 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7740 Lisp_Object other_frame = XCAR (tail);
7741 struct frame *tf = XFRAME (other_frame);
7743 if (tf != f
7744 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7745 && !FRAME_MINIBUF_ONLY_P (tf)
7746 && !EQ (other_frame, tip_frame)
7747 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7748 break;
7751 /* Set global variable indicating that multiple frames exist. */
7752 multiple_frames = CONSP (tail);
7754 /* Switch to the buffer of selected window of the frame. Set up
7755 frame_title_ptr so that display_mode_element will output into it;
7756 then display the title. */
7757 obuf = current_buffer;
7758 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7759 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7760 frame_title_ptr = frame_title_buf;
7761 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7762 NULL, DEFAULT_FACE_ID);
7763 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7764 len = frame_title_ptr - frame_title_buf;
7765 frame_title_ptr = NULL;
7766 set_buffer_internal_1 (obuf);
7768 /* Set the title only if it's changed. This avoids consing in
7769 the common case where it hasn't. (If it turns out that we've
7770 already wasted too much time by walking through the list with
7771 display_mode_element, then we might need to optimize at a
7772 higher level than this.) */
7773 if (! STRINGP (f->name)
7774 || SBYTES (f->name) != len
7775 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7776 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7780 #endif /* not HAVE_WINDOW_SYSTEM */
7785 /***********************************************************************
7786 Menu Bars
7787 ***********************************************************************/
7790 /* Prepare for redisplay by updating menu-bar item lists when
7791 appropriate. This can call eval. */
7793 void
7794 prepare_menu_bars ()
7796 int all_windows;
7797 struct gcpro gcpro1, gcpro2;
7798 struct frame *f;
7799 Lisp_Object tooltip_frame;
7801 #ifdef HAVE_WINDOW_SYSTEM
7802 tooltip_frame = tip_frame;
7803 #else
7804 tooltip_frame = Qnil;
7805 #endif
7807 /* Update all frame titles based on their buffer names, etc. We do
7808 this before the menu bars so that the buffer-menu will show the
7809 up-to-date frame titles. */
7810 #ifdef HAVE_WINDOW_SYSTEM
7811 if (windows_or_buffers_changed || update_mode_lines)
7813 Lisp_Object tail, frame;
7815 FOR_EACH_FRAME (tail, frame)
7817 f = XFRAME (frame);
7818 if (!EQ (frame, tooltip_frame)
7819 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7820 x_consider_frame_title (frame);
7823 #endif /* HAVE_WINDOW_SYSTEM */
7825 /* Update the menu bar item lists, if appropriate. This has to be
7826 done before any actual redisplay or generation of display lines. */
7827 all_windows = (update_mode_lines
7828 || buffer_shared > 1
7829 || windows_or_buffers_changed);
7830 if (all_windows)
7832 Lisp_Object tail, frame;
7833 int count = SPECPDL_INDEX ();
7835 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7837 FOR_EACH_FRAME (tail, frame)
7839 f = XFRAME (frame);
7841 /* Ignore tooltip frame. */
7842 if (EQ (frame, tooltip_frame))
7843 continue;
7845 /* If a window on this frame changed size, report that to
7846 the user and clear the size-change flag. */
7847 if (FRAME_WINDOW_SIZES_CHANGED (f))
7849 Lisp_Object functions;
7851 /* Clear flag first in case we get an error below. */
7852 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7853 functions = Vwindow_size_change_functions;
7854 GCPRO2 (tail, functions);
7856 while (CONSP (functions))
7858 call1 (XCAR (functions), frame);
7859 functions = XCDR (functions);
7861 UNGCPRO;
7864 GCPRO1 (tail);
7865 update_menu_bar (f, 0);
7866 #ifdef HAVE_WINDOW_SYSTEM
7867 update_tool_bar (f, 0);
7868 #endif
7869 UNGCPRO;
7872 unbind_to (count, Qnil);
7874 else
7876 struct frame *sf = SELECTED_FRAME ();
7877 update_menu_bar (sf, 1);
7878 #ifdef HAVE_WINDOW_SYSTEM
7879 update_tool_bar (sf, 1);
7880 #endif
7883 /* Motif needs this. See comment in xmenu.c. Turn it off when
7884 pending_menu_activation is not defined. */
7885 #ifdef USE_X_TOOLKIT
7886 pending_menu_activation = 0;
7887 #endif
7891 /* Update the menu bar item list for frame F. This has to be done
7892 before we start to fill in any display lines, because it can call
7893 eval.
7895 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7897 static void
7898 update_menu_bar (f, save_match_data)
7899 struct frame *f;
7900 int save_match_data;
7902 Lisp_Object window;
7903 register struct window *w;
7905 /* If called recursively during a menu update, do nothing. This can
7906 happen when, for instance, an activate-menubar-hook causes a
7907 redisplay. */
7908 if (inhibit_menubar_update)
7909 return;
7911 window = FRAME_SELECTED_WINDOW (f);
7912 w = XWINDOW (window);
7914 #if 0 /* The if statement below this if statement used to include the
7915 condition !NILP (w->update_mode_line), rather than using
7916 update_mode_lines directly, and this if statement may have
7917 been added to make that condition work. Now the if
7918 statement below matches its comment, this isn't needed. */
7919 if (update_mode_lines)
7920 w->update_mode_line = Qt;
7921 #endif
7923 if (FRAME_WINDOW_P (f)
7925 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7926 || defined (USE_GTK)
7927 FRAME_EXTERNAL_MENU_BAR (f)
7928 #else
7929 FRAME_MENU_BAR_LINES (f) > 0
7930 #endif
7931 : FRAME_MENU_BAR_LINES (f) > 0)
7933 /* If the user has switched buffers or windows, we need to
7934 recompute to reflect the new bindings. But we'll
7935 recompute when update_mode_lines is set too; that means
7936 that people can use force-mode-line-update to request
7937 that the menu bar be recomputed. The adverse effect on
7938 the rest of the redisplay algorithm is about the same as
7939 windows_or_buffers_changed anyway. */
7940 if (windows_or_buffers_changed
7941 /* This used to test w->update_mode_line, but we believe
7942 there is no need to recompute the menu in that case. */
7943 || update_mode_lines
7944 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7945 < BUF_MODIFF (XBUFFER (w->buffer)))
7946 != !NILP (w->last_had_star))
7947 || ((!NILP (Vtransient_mark_mode)
7948 && !NILP (XBUFFER (w->buffer)->mark_active))
7949 != !NILP (w->region_showing)))
7951 struct buffer *prev = current_buffer;
7952 int count = SPECPDL_INDEX ();
7954 specbind (Qinhibit_menubar_update, Qt);
7956 set_buffer_internal_1 (XBUFFER (w->buffer));
7957 if (save_match_data)
7958 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7959 if (NILP (Voverriding_local_map_menu_flag))
7961 specbind (Qoverriding_terminal_local_map, Qnil);
7962 specbind (Qoverriding_local_map, Qnil);
7965 /* Run the Lucid hook. */
7966 safe_run_hooks (Qactivate_menubar_hook);
7968 /* If it has changed current-menubar from previous value,
7969 really recompute the menu-bar from the value. */
7970 if (! NILP (Vlucid_menu_bar_dirty_flag))
7971 call0 (Qrecompute_lucid_menubar);
7973 safe_run_hooks (Qmenu_bar_update_hook);
7974 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7976 /* Redisplay the menu bar in case we changed it. */
7977 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7978 || defined (USE_GTK)
7979 if (FRAME_WINDOW_P (f)
7980 #if defined (MAC_OS)
7981 /* All frames on Mac OS share the same menubar. So only the
7982 selected frame should be allowed to set it. */
7983 && f == SELECTED_FRAME ()
7984 #endif
7986 set_frame_menubar (f, 0, 0);
7987 else
7988 /* On a terminal screen, the menu bar is an ordinary screen
7989 line, and this makes it get updated. */
7990 w->update_mode_line = Qt;
7991 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7992 /* In the non-toolkit version, the menu bar is an ordinary screen
7993 line, and this makes it get updated. */
7994 w->update_mode_line = Qt;
7995 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7997 unbind_to (count, Qnil);
7998 set_buffer_internal_1 (prev);
8005 /***********************************************************************
8006 Output Cursor
8007 ***********************************************************************/
8009 #ifdef HAVE_WINDOW_SYSTEM
8011 /* EXPORT:
8012 Nominal cursor position -- where to draw output.
8013 HPOS and VPOS are window relative glyph matrix coordinates.
8014 X and Y are window relative pixel coordinates. */
8016 struct cursor_pos output_cursor;
8019 /* EXPORT:
8020 Set the global variable output_cursor to CURSOR. All cursor
8021 positions are relative to updated_window. */
8023 void
8024 set_output_cursor (cursor)
8025 struct cursor_pos *cursor;
8027 output_cursor.hpos = cursor->hpos;
8028 output_cursor.vpos = cursor->vpos;
8029 output_cursor.x = cursor->x;
8030 output_cursor.y = cursor->y;
8034 /* EXPORT for RIF:
8035 Set a nominal cursor position.
8037 HPOS and VPOS are column/row positions in a window glyph matrix. X
8038 and Y are window text area relative pixel positions.
8040 If this is done during an update, updated_window will contain the
8041 window that is being updated and the position is the future output
8042 cursor position for that window. If updated_window is null, use
8043 selected_window and display the cursor at the given position. */
8045 void
8046 x_cursor_to (vpos, hpos, y, x)
8047 int vpos, hpos, y, x;
8049 struct window *w;
8051 /* If updated_window is not set, work on selected_window. */
8052 if (updated_window)
8053 w = updated_window;
8054 else
8055 w = XWINDOW (selected_window);
8057 /* Set the output cursor. */
8058 output_cursor.hpos = hpos;
8059 output_cursor.vpos = vpos;
8060 output_cursor.x = x;
8061 output_cursor.y = y;
8063 /* If not called as part of an update, really display the cursor.
8064 This will also set the cursor position of W. */
8065 if (updated_window == NULL)
8067 BLOCK_INPUT;
8068 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8069 if (rif->flush_display_optional)
8070 rif->flush_display_optional (SELECTED_FRAME ());
8071 UNBLOCK_INPUT;
8075 #endif /* HAVE_WINDOW_SYSTEM */
8078 /***********************************************************************
8079 Tool-bars
8080 ***********************************************************************/
8082 #ifdef HAVE_WINDOW_SYSTEM
8084 /* Where the mouse was last time we reported a mouse event. */
8086 FRAME_PTR last_mouse_frame;
8088 /* Tool-bar item index of the item on which a mouse button was pressed
8089 or -1. */
8091 int last_tool_bar_item;
8094 /* Update the tool-bar item list for frame F. This has to be done
8095 before we start to fill in any display lines. Called from
8096 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8097 and restore it here. */
8099 static void
8100 update_tool_bar (f, save_match_data)
8101 struct frame *f;
8102 int save_match_data;
8104 #ifdef USE_GTK
8105 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8106 #else
8107 int do_update = WINDOWP (f->tool_bar_window)
8108 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
8109 #endif
8111 if (do_update)
8113 Lisp_Object window;
8114 struct window *w;
8116 window = FRAME_SELECTED_WINDOW (f);
8117 w = XWINDOW (window);
8119 /* If the user has switched buffers or windows, we need to
8120 recompute to reflect the new bindings. But we'll
8121 recompute when update_mode_lines is set too; that means
8122 that people can use force-mode-line-update to request
8123 that the menu bar be recomputed. The adverse effect on
8124 the rest of the redisplay algorithm is about the same as
8125 windows_or_buffers_changed anyway. */
8126 if (windows_or_buffers_changed
8127 || !NILP (w->update_mode_line)
8128 || update_mode_lines
8129 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8130 < BUF_MODIFF (XBUFFER (w->buffer)))
8131 != !NILP (w->last_had_star))
8132 || ((!NILP (Vtransient_mark_mode)
8133 && !NILP (XBUFFER (w->buffer)->mark_active))
8134 != !NILP (w->region_showing)))
8136 struct buffer *prev = current_buffer;
8137 int count = SPECPDL_INDEX ();
8138 Lisp_Object old_tool_bar;
8139 struct gcpro gcpro1;
8141 /* Set current_buffer to the buffer of the selected
8142 window of the frame, so that we get the right local
8143 keymaps. */
8144 set_buffer_internal_1 (XBUFFER (w->buffer));
8146 /* Save match data, if we must. */
8147 if (save_match_data)
8148 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8150 /* Make sure that we don't accidentally use bogus keymaps. */
8151 if (NILP (Voverriding_local_map_menu_flag))
8153 specbind (Qoverriding_terminal_local_map, Qnil);
8154 specbind (Qoverriding_local_map, Qnil);
8157 old_tool_bar = f->tool_bar_items;
8158 GCPRO1 (old_tool_bar);
8160 /* Build desired tool-bar items from keymaps. */
8161 BLOCK_INPUT;
8162 f->tool_bar_items
8163 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8164 UNBLOCK_INPUT;
8166 /* Redisplay the tool-bar if we changed it. */
8167 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8168 w->update_mode_line = Qt;
8170 unbind_to (count, Qnil);
8171 set_buffer_internal_1 (prev);
8177 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8178 F's desired tool-bar contents. F->tool_bar_items must have
8179 been set up previously by calling prepare_menu_bars. */
8181 static void
8182 build_desired_tool_bar_string (f)
8183 struct frame *f;
8185 int i, size, size_needed;
8186 struct gcpro gcpro1, gcpro2, gcpro3;
8187 Lisp_Object image, plist, props;
8189 image = plist = props = Qnil;
8190 GCPRO3 (image, plist, props);
8192 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8193 Otherwise, make a new string. */
8195 /* The size of the string we might be able to reuse. */
8196 size = (STRINGP (f->desired_tool_bar_string)
8197 ? SCHARS (f->desired_tool_bar_string)
8198 : 0);
8200 /* We need one space in the string for each image. */
8201 size_needed = f->n_tool_bar_items;
8203 /* Reuse f->desired_tool_bar_string, if possible. */
8204 if (size < size_needed || NILP (f->desired_tool_bar_string))
8205 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8206 make_number (' '));
8207 else
8209 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8210 Fremove_text_properties (make_number (0), make_number (size),
8211 props, f->desired_tool_bar_string);
8214 /* Put a `display' property on the string for the images to display,
8215 put a `menu_item' property on tool-bar items with a value that
8216 is the index of the item in F's tool-bar item vector. */
8217 for (i = 0; i < f->n_tool_bar_items; ++i)
8219 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8221 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8222 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8223 int hmargin, vmargin, relief, idx, end;
8224 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8226 /* If image is a vector, choose the image according to the
8227 button state. */
8228 image = PROP (TOOL_BAR_ITEM_IMAGES);
8229 if (VECTORP (image))
8231 if (enabled_p)
8232 idx = (selected_p
8233 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8234 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8235 else
8236 idx = (selected_p
8237 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8238 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8240 xassert (ASIZE (image) >= idx);
8241 image = AREF (image, idx);
8243 else
8244 idx = -1;
8246 /* Ignore invalid image specifications. */
8247 if (!valid_image_p (image))
8248 continue;
8250 /* Display the tool-bar button pressed, or depressed. */
8251 plist = Fcopy_sequence (XCDR (image));
8253 /* Compute margin and relief to draw. */
8254 relief = (tool_bar_button_relief >= 0
8255 ? tool_bar_button_relief
8256 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8257 hmargin = vmargin = relief;
8259 if (INTEGERP (Vtool_bar_button_margin)
8260 && XINT (Vtool_bar_button_margin) > 0)
8262 hmargin += XFASTINT (Vtool_bar_button_margin);
8263 vmargin += XFASTINT (Vtool_bar_button_margin);
8265 else if (CONSP (Vtool_bar_button_margin))
8267 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8268 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8269 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8271 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8272 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8273 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8276 if (auto_raise_tool_bar_buttons_p)
8278 /* Add a `:relief' property to the image spec if the item is
8279 selected. */
8280 if (selected_p)
8282 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8283 hmargin -= relief;
8284 vmargin -= relief;
8287 else
8289 /* If image is selected, display it pressed, i.e. with a
8290 negative relief. If it's not selected, display it with a
8291 raised relief. */
8292 plist = Fplist_put (plist, QCrelief,
8293 (selected_p
8294 ? make_number (-relief)
8295 : make_number (relief)));
8296 hmargin -= relief;
8297 vmargin -= relief;
8300 /* Put a margin around the image. */
8301 if (hmargin || vmargin)
8303 if (hmargin == vmargin)
8304 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8305 else
8306 plist = Fplist_put (plist, QCmargin,
8307 Fcons (make_number (hmargin),
8308 make_number (vmargin)));
8311 /* If button is not enabled, and we don't have special images
8312 for the disabled state, make the image appear disabled by
8313 applying an appropriate algorithm to it. */
8314 if (!enabled_p && idx < 0)
8315 plist = Fplist_put (plist, QCconversion, Qdisabled);
8317 /* Put a `display' text property on the string for the image to
8318 display. Put a `menu-item' property on the string that gives
8319 the start of this item's properties in the tool-bar items
8320 vector. */
8321 image = Fcons (Qimage, plist);
8322 props = list4 (Qdisplay, image,
8323 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8325 /* Let the last image hide all remaining spaces in the tool bar
8326 string. The string can be longer than needed when we reuse a
8327 previous string. */
8328 if (i + 1 == f->n_tool_bar_items)
8329 end = SCHARS (f->desired_tool_bar_string);
8330 else
8331 end = i + 1;
8332 Fadd_text_properties (make_number (i), make_number (end),
8333 props, f->desired_tool_bar_string);
8334 #undef PROP
8337 UNGCPRO;
8341 /* Display one line of the tool-bar of frame IT->f. */
8343 static void
8344 display_tool_bar_line (it)
8345 struct it *it;
8347 struct glyph_row *row = it->glyph_row;
8348 int max_x = it->last_visible_x;
8349 struct glyph *last;
8351 prepare_desired_row (row);
8352 row->y = it->current_y;
8354 /* Note that this isn't made use of if the face hasn't a box,
8355 so there's no need to check the face here. */
8356 it->start_of_box_run_p = 1;
8358 while (it->current_x < max_x)
8360 int x_before, x, n_glyphs_before, i, nglyphs;
8362 /* Get the next display element. */
8363 if (!get_next_display_element (it))
8364 break;
8366 /* Produce glyphs. */
8367 x_before = it->current_x;
8368 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8369 PRODUCE_GLYPHS (it);
8371 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8372 i = 0;
8373 x = x_before;
8374 while (i < nglyphs)
8376 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8378 if (x + glyph->pixel_width > max_x)
8380 /* Glyph doesn't fit on line. */
8381 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8382 it->current_x = x;
8383 goto out;
8386 ++it->hpos;
8387 x += glyph->pixel_width;
8388 ++i;
8391 /* Stop at line ends. */
8392 if (ITERATOR_AT_END_OF_LINE_P (it))
8393 break;
8395 set_iterator_to_next (it, 1);
8398 out:;
8400 row->displays_text_p = row->used[TEXT_AREA] != 0;
8401 extend_face_to_end_of_line (it);
8402 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8403 last->right_box_line_p = 1;
8404 if (last == row->glyphs[TEXT_AREA])
8405 last->left_box_line_p = 1;
8406 compute_line_metrics (it);
8408 /* If line is empty, make it occupy the rest of the tool-bar. */
8409 if (!row->displays_text_p)
8411 row->height = row->phys_height = it->last_visible_y - row->y;
8412 row->ascent = row->phys_ascent = 0;
8415 row->full_width_p = 1;
8416 row->continued_p = 0;
8417 row->truncated_on_left_p = 0;
8418 row->truncated_on_right_p = 0;
8420 it->current_x = it->hpos = 0;
8421 it->current_y += row->height;
8422 ++it->vpos;
8423 ++it->glyph_row;
8427 /* Value is the number of screen lines needed to make all tool-bar
8428 items of frame F visible. */
8430 static int
8431 tool_bar_lines_needed (f)
8432 struct frame *f;
8434 struct window *w = XWINDOW (f->tool_bar_window);
8435 struct it it;
8437 /* Initialize an iterator for iteration over
8438 F->desired_tool_bar_string in the tool-bar window of frame F. */
8439 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8440 it.first_visible_x = 0;
8441 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8442 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8444 while (!ITERATOR_AT_END_P (&it))
8446 it.glyph_row = w->desired_matrix->rows;
8447 clear_glyph_row (it.glyph_row);
8448 display_tool_bar_line (&it);
8451 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
8455 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8456 0, 1, 0,
8457 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8458 (frame)
8459 Lisp_Object frame;
8461 struct frame *f;
8462 struct window *w;
8463 int nlines = 0;
8465 if (NILP (frame))
8466 frame = selected_frame;
8467 else
8468 CHECK_FRAME (frame);
8469 f = XFRAME (frame);
8471 if (WINDOWP (f->tool_bar_window)
8472 || (w = XWINDOW (f->tool_bar_window),
8473 XFASTINT (w->height) > 0))
8475 update_tool_bar (f, 1);
8476 if (f->n_tool_bar_items)
8478 build_desired_tool_bar_string (f);
8479 nlines = tool_bar_lines_needed (f);
8483 return make_number (nlines);
8487 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8488 height should be changed. */
8490 static int
8491 redisplay_tool_bar (f)
8492 struct frame *f;
8494 struct window *w;
8495 struct it it;
8496 struct glyph_row *row;
8497 int change_height_p = 0;
8499 #ifdef USE_GTK
8500 if (FRAME_EXTERNAL_TOOL_BAR(f))
8501 update_frame_tool_bar (f);
8502 return 0;
8503 #endif
8505 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8506 do anything. This means you must start with tool-bar-lines
8507 non-zero to get the auto-sizing effect. Or in other words, you
8508 can turn off tool-bars by specifying tool-bar-lines zero. */
8509 if (!WINDOWP (f->tool_bar_window)
8510 || (w = XWINDOW (f->tool_bar_window),
8511 XFASTINT (w->height) == 0))
8512 return 0;
8514 /* Set up an iterator for the tool-bar window. */
8515 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8516 it.first_visible_x = 0;
8517 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8518 row = it.glyph_row;
8520 /* Build a string that represents the contents of the tool-bar. */
8521 build_desired_tool_bar_string (f);
8522 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8524 /* Display as many lines as needed to display all tool-bar items. */
8525 while (it.current_y < it.last_visible_y)
8526 display_tool_bar_line (&it);
8528 /* It doesn't make much sense to try scrolling in the tool-bar
8529 window, so don't do it. */
8530 w->desired_matrix->no_scrolling_p = 1;
8531 w->must_be_updated_p = 1;
8533 if (auto_resize_tool_bars_p)
8535 int nlines;
8537 /* If we couldn't display everything, change the tool-bar's
8538 height. */
8539 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8540 change_height_p = 1;
8542 /* If there are blank lines at the end, except for a partially
8543 visible blank line at the end that is smaller than
8544 CANON_Y_UNIT, change the tool-bar's height. */
8545 row = it.glyph_row - 1;
8546 if (!row->displays_text_p
8547 && row->height >= CANON_Y_UNIT (f))
8548 change_height_p = 1;
8550 /* If row displays tool-bar items, but is partially visible,
8551 change the tool-bar's height. */
8552 if (row->displays_text_p
8553 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8554 change_height_p = 1;
8556 /* Resize windows as needed by changing the `tool-bar-lines'
8557 frame parameter. */
8558 if (change_height_p
8559 && (nlines = tool_bar_lines_needed (f),
8560 nlines != XFASTINT (w->height)))
8562 extern Lisp_Object Qtool_bar_lines;
8563 Lisp_Object frame;
8564 int old_height = XFASTINT (w->height);
8566 XSETFRAME (frame, f);
8567 clear_glyph_matrix (w->desired_matrix);
8568 Fmodify_frame_parameters (frame,
8569 Fcons (Fcons (Qtool_bar_lines,
8570 make_number (nlines)),
8571 Qnil));
8572 if (XFASTINT (w->height) != old_height)
8573 fonts_changed_p = 1;
8577 return change_height_p;
8581 /* Get information about the tool-bar item which is displayed in GLYPH
8582 on frame F. Return in *PROP_IDX the index where tool-bar item
8583 properties start in F->tool_bar_items. Value is zero if
8584 GLYPH doesn't display a tool-bar item. */
8586 static int
8587 tool_bar_item_info (f, glyph, prop_idx)
8588 struct frame *f;
8589 struct glyph *glyph;
8590 int *prop_idx;
8592 Lisp_Object prop;
8593 int success_p;
8594 int charpos;
8596 /* This function can be called asynchronously, which means we must
8597 exclude any possibility that Fget_text_property signals an
8598 error. */
8599 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8600 charpos = max (0, charpos);
8602 /* Get the text property `menu-item' at pos. The value of that
8603 property is the start index of this item's properties in
8604 F->tool_bar_items. */
8605 prop = Fget_text_property (make_number (charpos),
8606 Qmenu_item, f->current_tool_bar_string);
8607 if (INTEGERP (prop))
8609 *prop_idx = XINT (prop);
8610 success_p = 1;
8612 else
8613 success_p = 0;
8615 return success_p;
8619 /* Get information about the tool-bar item at position X/Y on frame F.
8620 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8621 the current matrix of the tool-bar window of F, or NULL if not
8622 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8623 item in F->tool_bar_items. Value is
8625 -1 if X/Y is not on a tool-bar item
8626 0 if X/Y is on the same item that was highlighted before.
8627 1 otherwise. */
8629 static int
8630 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8631 struct frame *f;
8632 int x, y;
8633 struct glyph **glyph;
8634 int *hpos, *vpos, *prop_idx;
8636 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8637 struct window *w = XWINDOW (f->tool_bar_window);
8638 int area;
8640 /* Find the glyph under X/Y. */
8641 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8642 if (*glyph == NULL)
8643 return -1;
8645 /* Get the start of this tool-bar item's properties in
8646 f->tool_bar_items. */
8647 if (!tool_bar_item_info (f, *glyph, prop_idx))
8648 return -1;
8650 /* Is mouse on the highlighted item? */
8651 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8652 && *vpos >= dpyinfo->mouse_face_beg_row
8653 && *vpos <= dpyinfo->mouse_face_end_row
8654 && (*vpos > dpyinfo->mouse_face_beg_row
8655 || *hpos >= dpyinfo->mouse_face_beg_col)
8656 && (*vpos < dpyinfo->mouse_face_end_row
8657 || *hpos < dpyinfo->mouse_face_end_col
8658 || dpyinfo->mouse_face_past_end))
8659 return 0;
8661 return 1;
8665 /* EXPORT:
8666 Handle mouse button event on the tool-bar of frame F, at
8667 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8668 0 for button release. MODIFIERS is event modifiers for button
8669 release. */
8671 void
8672 handle_tool_bar_click (f, x, y, down_p, modifiers)
8673 struct frame *f;
8674 int x, y, down_p;
8675 unsigned int modifiers;
8677 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8678 struct window *w = XWINDOW (f->tool_bar_window);
8679 int hpos, vpos, prop_idx;
8680 struct glyph *glyph;
8681 Lisp_Object enabled_p;
8683 /* If not on the highlighted tool-bar item, return. */
8684 frame_to_window_pixel_xy (w, &x, &y);
8685 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8686 return;
8688 /* If item is disabled, do nothing. */
8689 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8690 if (NILP (enabled_p))
8691 return;
8693 if (down_p)
8695 /* Show item in pressed state. */
8696 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8697 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8698 last_tool_bar_item = prop_idx;
8700 else
8702 Lisp_Object key, frame;
8703 struct input_event event;
8705 /* Show item in released state. */
8706 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8707 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8709 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8711 XSETFRAME (frame, f);
8712 event.kind = TOOL_BAR_EVENT;
8713 event.frame_or_window = frame;
8714 event.arg = frame;
8715 kbd_buffer_store_event (&event);
8717 event.kind = TOOL_BAR_EVENT;
8718 event.frame_or_window = frame;
8719 event.arg = key;
8720 event.modifiers = modifiers;
8721 kbd_buffer_store_event (&event);
8722 last_tool_bar_item = -1;
8727 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8728 tool-bar window-relative coordinates X/Y. Called from
8729 note_mouse_highlight. */
8731 static void
8732 note_tool_bar_highlight (f, x, y)
8733 struct frame *f;
8734 int x, y;
8736 Lisp_Object window = f->tool_bar_window;
8737 struct window *w = XWINDOW (window);
8738 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8739 int hpos, vpos;
8740 struct glyph *glyph;
8741 struct glyph_row *row;
8742 int i;
8743 Lisp_Object enabled_p;
8744 int prop_idx;
8745 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8746 int mouse_down_p, rc;
8748 /* Function note_mouse_highlight is called with negative x(y
8749 values when mouse moves outside of the frame. */
8750 if (x <= 0 || y <= 0)
8752 clear_mouse_face (dpyinfo);
8753 return;
8756 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8757 if (rc < 0)
8759 /* Not on tool-bar item. */
8760 clear_mouse_face (dpyinfo);
8761 return;
8763 else if (rc == 0)
8764 /* On same tool-bar item as before. */
8765 goto set_help_echo;
8767 clear_mouse_face (dpyinfo);
8769 /* Mouse is down, but on different tool-bar item? */
8770 mouse_down_p = (dpyinfo->grabbed
8771 && f == last_mouse_frame
8772 && FRAME_LIVE_P (f));
8773 if (mouse_down_p
8774 && last_tool_bar_item != prop_idx)
8775 return;
8777 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8778 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8780 /* If tool-bar item is not enabled, don't highlight it. */
8781 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8782 if (!NILP (enabled_p))
8784 /* Compute the x-position of the glyph. In front and past the
8785 image is a space. We include this is the highlighted area. */
8786 row = MATRIX_ROW (w->current_matrix, vpos);
8787 for (i = x = 0; i < hpos; ++i)
8788 x += row->glyphs[TEXT_AREA][i].pixel_width;
8790 /* Record this as the current active region. */
8791 dpyinfo->mouse_face_beg_col = hpos;
8792 dpyinfo->mouse_face_beg_row = vpos;
8793 dpyinfo->mouse_face_beg_x = x;
8794 dpyinfo->mouse_face_beg_y = row->y;
8795 dpyinfo->mouse_face_past_end = 0;
8797 dpyinfo->mouse_face_end_col = hpos + 1;
8798 dpyinfo->mouse_face_end_row = vpos;
8799 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8800 dpyinfo->mouse_face_end_y = row->y;
8801 dpyinfo->mouse_face_window = window;
8802 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8804 /* Display it as active. */
8805 show_mouse_face (dpyinfo, draw);
8806 dpyinfo->mouse_face_image_state = draw;
8809 set_help_echo:
8811 /* Set help_echo_string to a help string to display for this tool-bar item.
8812 XTread_socket does the rest. */
8813 help_echo_object = help_echo_window = Qnil;
8814 help_echo_pos = -1;
8815 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8816 if (NILP (help_echo_string))
8817 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8820 #endif /* HAVE_WINDOW_SYSTEM */
8824 /***********************************************************************
8825 Fringes
8826 ***********************************************************************/
8828 #ifdef HAVE_WINDOW_SYSTEM
8830 /* An arrow like this: `<-'. */
8831 static unsigned char left_bits[] = {
8832 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8834 /* Right truncation arrow bitmap `->'. */
8835 static unsigned char right_bits[] = {
8836 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8838 /* Marker for continued lines. */
8839 static unsigned char continued_bits[] = {
8840 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8842 /* Marker for continuation lines. */
8843 static unsigned char continuation_bits[] = {
8844 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8846 /* Overlay arrow bitmap. A triangular arrow. */
8847 static unsigned char ov_bits[] = {
8848 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8850 /* Bitmap drawn to indicate lines not displaying text if
8851 `indicate-empty-lines' is non-nil. */
8852 static unsigned char zv_bits[] = {
8853 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8854 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8855 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8856 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8857 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8858 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8859 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8860 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8862 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8864 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8865 { 8, sizeof (left_bits), 0, left_bits },
8866 { 8, sizeof (right_bits), 0, right_bits },
8867 { 8, sizeof (continued_bits), 0, continued_bits },
8868 { 8, sizeof (continuation_bits), 0, continuation_bits },
8869 { 8, sizeof (ov_bits), 0, ov_bits },
8870 { 8, sizeof (zv_bits), 3, zv_bits }
8874 /* Draw the bitmap WHICH in one of the left or right fringes of
8875 window W. ROW is the glyph row for which to display the bitmap; it
8876 determines the vertical position at which the bitmap has to be
8877 drawn. */
8879 static void
8880 draw_fringe_bitmap (w, row, which, left_p)
8881 struct window *w;
8882 struct glyph_row *row;
8883 enum fringe_bitmap_type which;
8884 int left_p;
8886 struct frame *f = XFRAME (WINDOW_FRAME (w));
8887 struct draw_fringe_bitmap_params p;
8889 /* Convert row to frame coordinates. */
8890 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8892 p.which = which;
8893 p.wd = fringe_bitmaps[which].width;
8895 p.h = fringe_bitmaps[which].height;
8896 p.dh = (fringe_bitmaps[which].period
8897 ? (p.y % fringe_bitmaps[which].period)
8898 : 0);
8899 p.h -= p.dh;
8900 /* Clip bitmap if too high. */
8901 if (p.h > row->height)
8902 p.h = row->height;
8904 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8905 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8907 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8908 the fringe. */
8909 p.bx = -1;
8910 if (left_p)
8912 if (p.wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
8913 p.wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
8914 p.x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
8915 - p.wd
8916 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - p.wd) / 2);
8917 if (p.wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > p.h)
8919 /* If W has a vertical border to its left, don't draw over it. */
8920 int border = ((XFASTINT (w->left) > 0
8921 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8922 ? 1 : 0);
8923 p.bx = (window_box_left (w, -1)
8924 - FRAME_X_LEFT_FRINGE_WIDTH (f)
8925 + border);
8926 p.nx = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
8929 else
8931 if (p.wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
8932 p.wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8933 p.x = (window_box_right (w, -1)
8934 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - p.wd) / 2);
8935 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8936 the fringe. */
8937 if (p.wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > p.h)
8939 p.bx = window_box_right (w, -1);
8940 p.nx = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8944 if (p.bx >= 0)
8946 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8948 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8949 p.ny = row->visible_height;
8952 /* Adjust y to the offset in the row to start drawing the bitmap. */
8953 p.y += (row->height - p.h) / 2;
8955 rif->draw_fringe_bitmap (w, row, &p);
8958 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
8959 function with input blocked. */
8961 void
8962 draw_row_fringe_bitmaps (w, row)
8963 struct window *w;
8964 struct glyph_row *row;
8966 struct frame *f = XFRAME (w->frame);
8967 enum fringe_bitmap_type bitmap;
8969 xassert (interrupt_input_blocked);
8971 /* If row is completely invisible, because of vscrolling, we
8972 don't have to draw anything. */
8973 if (row->visible_height <= 0)
8974 return;
8976 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
8978 /* Decide which bitmap to draw in the left fringe. */
8979 if (row->overlay_arrow_p)
8980 bitmap = OVERLAY_ARROW_BITMAP;
8981 else if (row->truncated_on_left_p)
8982 bitmap = LEFT_TRUNCATION_BITMAP;
8983 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
8984 bitmap = CONTINUATION_LINE_BITMAP;
8985 else if (row->indicate_empty_line_p)
8986 bitmap = ZV_LINE_BITMAP;
8987 else
8988 bitmap = NO_FRINGE_BITMAP;
8990 draw_fringe_bitmap (w, row, bitmap, 1);
8993 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
8995 /* Decide which bitmap to draw in the right fringe. */
8996 if (row->truncated_on_right_p)
8997 bitmap = RIGHT_TRUNCATION_BITMAP;
8998 else if (row->continued_p)
8999 bitmap = CONTINUED_LINE_BITMAP;
9000 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
9001 bitmap = ZV_LINE_BITMAP;
9002 else
9003 bitmap = NO_FRINGE_BITMAP;
9005 draw_fringe_bitmap (w, row, bitmap, 0);
9010 /* Compute actual fringe widths */
9012 void
9013 compute_fringe_widths (f, redraw)
9014 struct frame *f;
9015 int redraw;
9017 int o_left = FRAME_X_LEFT_FRINGE_WIDTH (f);
9018 int o_right = FRAME_X_RIGHT_FRINGE_WIDTH (f);
9019 int o_cols = FRAME_X_FRINGE_COLS (f);
9021 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9022 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9023 int left_fringe_width, right_fringe_width;
9025 if (!NILP (left_fringe))
9026 left_fringe = Fcdr (left_fringe);
9027 if (!NILP (right_fringe))
9028 right_fringe = Fcdr (right_fringe);
9030 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9031 XINT (left_fringe));
9032 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9033 XINT (right_fringe));
9035 if (left_fringe_width || right_fringe_width)
9037 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9038 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9039 int conf_wid = left_wid + right_wid;
9040 int font_wid = FONT_WIDTH (FRAME_FONT (f));
9041 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9042 int real_wid = cols * font_wid;
9043 if (left_wid && right_wid)
9045 if (left_fringe_width < 0)
9047 /* Left fringe width is fixed, adjust right fringe if necessary */
9048 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid;
9049 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9051 else if (right_fringe_width < 0)
9053 /* Right fringe width is fixed, adjust left fringe if necessary */
9054 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9055 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid;
9057 else
9059 /* Adjust both fringes with an equal amount.
9060 Note that we are doing integer arithmetic here, so don't
9061 lose a pixel if the total width is an odd number. */
9062 int fill = real_wid - conf_wid;
9063 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9064 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9067 else if (left_fringe_width)
9069 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid;
9070 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
9072 else
9074 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
9075 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid;
9077 FRAME_X_FRINGE_COLS (f) = cols;
9078 FRAME_X_FRINGE_WIDTH (f) = real_wid;
9080 else
9082 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
9083 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
9084 FRAME_X_FRINGE_COLS (f) = 0;
9085 FRAME_X_FRINGE_WIDTH (f) = 0;
9088 if (redraw && FRAME_VISIBLE_P (f))
9089 if (o_left != FRAME_X_LEFT_FRINGE_WIDTH (f) ||
9090 o_right != FRAME_X_RIGHT_FRINGE_WIDTH (f) ||
9091 o_cols != FRAME_X_FRINGE_COLS (f))
9092 redraw_frame (f);
9095 #endif /* HAVE_WINDOW_SYSTEM */
9099 /************************************************************************
9100 Horizontal scrolling
9101 ************************************************************************/
9103 static int hscroll_window_tree P_ ((Lisp_Object));
9104 static int hscroll_windows P_ ((Lisp_Object));
9106 /* For all leaf windows in the window tree rooted at WINDOW, set their
9107 hscroll value so that PT is (i) visible in the window, and (ii) so
9108 that it is not within a certain margin at the window's left and
9109 right border. Value is non-zero if any window's hscroll has been
9110 changed. */
9112 static int
9113 hscroll_window_tree (window)
9114 Lisp_Object window;
9116 int hscrolled_p = 0;
9117 int hscroll_relative_p = FLOATP (Vhscroll_step);
9118 int hscroll_step_abs = 0;
9119 double hscroll_step_rel = 0;
9121 if (hscroll_relative_p)
9123 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9124 if (hscroll_step_rel < 0)
9126 hscroll_relative_p = 0;
9127 hscroll_step_abs = 0;
9130 else if (INTEGERP (Vhscroll_step))
9132 hscroll_step_abs = XINT (Vhscroll_step);
9133 if (hscroll_step_abs < 0)
9134 hscroll_step_abs = 0;
9136 else
9137 hscroll_step_abs = 0;
9139 while (WINDOWP (window))
9141 struct window *w = XWINDOW (window);
9143 if (WINDOWP (w->hchild))
9144 hscrolled_p |= hscroll_window_tree (w->hchild);
9145 else if (WINDOWP (w->vchild))
9146 hscrolled_p |= hscroll_window_tree (w->vchild);
9147 else if (w->cursor.vpos >= 0)
9149 int h_margin, text_area_x, text_area_y;
9150 int text_area_width, text_area_height;
9151 struct glyph_row *current_cursor_row
9152 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9153 struct glyph_row *desired_cursor_row
9154 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9155 struct glyph_row *cursor_row
9156 = (desired_cursor_row->enabled_p
9157 ? desired_cursor_row
9158 : current_cursor_row);
9160 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
9161 &text_area_width, &text_area_height);
9163 /* Scroll when cursor is inside this scroll margin. */
9164 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
9166 if ((XFASTINT (w->hscroll)
9167 && w->cursor.x <= h_margin)
9168 || (cursor_row->enabled_p
9169 && cursor_row->truncated_on_right_p
9170 && (w->cursor.x >= text_area_width - h_margin)))
9172 struct it it;
9173 int hscroll;
9174 struct buffer *saved_current_buffer;
9175 int pt;
9176 int wanted_x;
9178 /* Find point in a display of infinite width. */
9179 saved_current_buffer = current_buffer;
9180 current_buffer = XBUFFER (w->buffer);
9182 if (w == XWINDOW (selected_window))
9183 pt = BUF_PT (current_buffer);
9184 else
9186 pt = marker_position (w->pointm);
9187 pt = max (BEGV, pt);
9188 pt = min (ZV, pt);
9191 /* Move iterator to pt starting at cursor_row->start in
9192 a line with infinite width. */
9193 init_to_row_start (&it, w, cursor_row);
9194 it.last_visible_x = INFINITY;
9195 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9196 current_buffer = saved_current_buffer;
9198 /* Position cursor in window. */
9199 if (!hscroll_relative_p && hscroll_step_abs == 0)
9200 hscroll = max (0, it.current_x - text_area_width / 2)
9201 / CANON_X_UNIT (it.f);
9202 else if (w->cursor.x >= text_area_width - h_margin)
9204 if (hscroll_relative_p)
9205 wanted_x = text_area_width * (1 - hscroll_step_rel)
9206 - h_margin;
9207 else
9208 wanted_x = text_area_width
9209 - hscroll_step_abs * CANON_X_UNIT (it.f)
9210 - h_margin;
9211 hscroll
9212 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
9214 else
9216 if (hscroll_relative_p)
9217 wanted_x = text_area_width * hscroll_step_rel
9218 + h_margin;
9219 else
9220 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
9221 + h_margin;
9222 hscroll
9223 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
9225 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9227 /* Don't call Fset_window_hscroll if value hasn't
9228 changed because it will prevent redisplay
9229 optimizations. */
9230 if (XFASTINT (w->hscroll) != hscroll)
9232 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9233 w->hscroll = make_number (hscroll);
9234 hscrolled_p = 1;
9239 window = w->next;
9242 /* Value is non-zero if hscroll of any leaf window has been changed. */
9243 return hscrolled_p;
9247 /* Set hscroll so that cursor is visible and not inside horizontal
9248 scroll margins for all windows in the tree rooted at WINDOW. See
9249 also hscroll_window_tree above. Value is non-zero if any window's
9250 hscroll has been changed. If it has, desired matrices on the frame
9251 of WINDOW are cleared. */
9253 static int
9254 hscroll_windows (window)
9255 Lisp_Object window;
9257 int hscrolled_p;
9259 if (automatic_hscrolling_p)
9261 hscrolled_p = hscroll_window_tree (window);
9262 if (hscrolled_p)
9263 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9265 else
9266 hscrolled_p = 0;
9267 return hscrolled_p;
9272 /************************************************************************
9273 Redisplay
9274 ************************************************************************/
9276 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9277 to a non-zero value. This is sometimes handy to have in a debugger
9278 session. */
9280 #if GLYPH_DEBUG
9282 /* First and last unchanged row for try_window_id. */
9284 int debug_first_unchanged_at_end_vpos;
9285 int debug_last_unchanged_at_beg_vpos;
9287 /* Delta vpos and y. */
9289 int debug_dvpos, debug_dy;
9291 /* Delta in characters and bytes for try_window_id. */
9293 int debug_delta, debug_delta_bytes;
9295 /* Values of window_end_pos and window_end_vpos at the end of
9296 try_window_id. */
9298 EMACS_INT debug_end_pos, debug_end_vpos;
9300 /* Append a string to W->desired_matrix->method. FMT is a printf
9301 format string. A1...A9 are a supplement for a variable-length
9302 argument list. If trace_redisplay_p is non-zero also printf the
9303 resulting string to stderr. */
9305 static void
9306 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9307 struct window *w;
9308 char *fmt;
9309 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9311 char buffer[512];
9312 char *method = w->desired_matrix->method;
9313 int len = strlen (method);
9314 int size = sizeof w->desired_matrix->method;
9315 int remaining = size - len - 1;
9317 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9318 if (len && remaining)
9320 method[len] = '|';
9321 --remaining, ++len;
9324 strncpy (method + len, buffer, remaining);
9326 if (trace_redisplay_p)
9327 fprintf (stderr, "%p (%s): %s\n",
9329 ((BUFFERP (w->buffer)
9330 && STRINGP (XBUFFER (w->buffer)->name))
9331 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9332 : "no buffer"),
9333 buffer);
9336 #endif /* GLYPH_DEBUG */
9339 /* Value is non-zero if all changes in window W, which displays
9340 current_buffer, are in the text between START and END. START is a
9341 buffer position, END is given as a distance from Z. Used in
9342 redisplay_internal for display optimization. */
9344 static INLINE int
9345 text_outside_line_unchanged_p (w, start, end)
9346 struct window *w;
9347 int start, end;
9349 int unchanged_p = 1;
9351 /* If text or overlays have changed, see where. */
9352 if (XFASTINT (w->last_modified) < MODIFF
9353 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9355 /* Gap in the line? */
9356 if (GPT < start || Z - GPT < end)
9357 unchanged_p = 0;
9359 /* Changes start in front of the line, or end after it? */
9360 if (unchanged_p
9361 && (BEG_UNCHANGED < start - 1
9362 || END_UNCHANGED < end))
9363 unchanged_p = 0;
9365 /* If selective display, can't optimize if changes start at the
9366 beginning of the line. */
9367 if (unchanged_p
9368 && INTEGERP (current_buffer->selective_display)
9369 && XINT (current_buffer->selective_display) > 0
9370 && (BEG_UNCHANGED < start || GPT <= start))
9371 unchanged_p = 0;
9373 /* If there are overlays at the start or end of the line, these
9374 may have overlay strings with newlines in them. A change at
9375 START, for instance, may actually concern the display of such
9376 overlay strings as well, and they are displayed on different
9377 lines. So, quickly rule out this case. (For the future, it
9378 might be desirable to implement something more telling than
9379 just BEG/END_UNCHANGED.) */
9380 if (unchanged_p)
9382 if (BEG + BEG_UNCHANGED == start
9383 && overlay_touches_p (start))
9384 unchanged_p = 0;
9385 if (END_UNCHANGED == end
9386 && overlay_touches_p (Z - end))
9387 unchanged_p = 0;
9391 return unchanged_p;
9395 /* Do a frame update, taking possible shortcuts into account. This is
9396 the main external entry point for redisplay.
9398 If the last redisplay displayed an echo area message and that message
9399 is no longer requested, we clear the echo area or bring back the
9400 mini-buffer if that is in use. */
9402 void
9403 redisplay ()
9405 redisplay_internal (0);
9409 /* Return 1 if point moved out of or into a composition. Otherwise
9410 return 0. PREV_BUF and PREV_PT are the last point buffer and
9411 position. BUF and PT are the current point buffer and position. */
9414 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9415 struct buffer *prev_buf, *buf;
9416 int prev_pt, pt;
9418 int start, end;
9419 Lisp_Object prop;
9420 Lisp_Object buffer;
9422 XSETBUFFER (buffer, buf);
9423 /* Check a composition at the last point if point moved within the
9424 same buffer. */
9425 if (prev_buf == buf)
9427 if (prev_pt == pt)
9428 /* Point didn't move. */
9429 return 0;
9431 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9432 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9433 && COMPOSITION_VALID_P (start, end, prop)
9434 && start < prev_pt && end > prev_pt)
9435 /* The last point was within the composition. Return 1 iff
9436 point moved out of the composition. */
9437 return (pt <= start || pt >= end);
9440 /* Check a composition at the current point. */
9441 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9442 && find_composition (pt, -1, &start, &end, &prop, buffer)
9443 && COMPOSITION_VALID_P (start, end, prop)
9444 && start < pt && end > pt);
9448 /* Reconsider the setting of B->clip_changed which is displayed
9449 in window W. */
9451 static INLINE void
9452 reconsider_clip_changes (w, b)
9453 struct window *w;
9454 struct buffer *b;
9456 if (b->clip_changed
9457 && !NILP (w->window_end_valid)
9458 && w->current_matrix->buffer == b
9459 && w->current_matrix->zv == BUF_ZV (b)
9460 && w->current_matrix->begv == BUF_BEGV (b))
9461 b->clip_changed = 0;
9463 /* If display wasn't paused, and W is not a tool bar window, see if
9464 point has been moved into or out of a composition. In that case,
9465 we set b->clip_changed to 1 to force updating the screen. If
9466 b->clip_changed has already been set to 1, we can skip this
9467 check. */
9468 if (!b->clip_changed
9469 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9471 int pt;
9473 if (w == XWINDOW (selected_window))
9474 pt = BUF_PT (current_buffer);
9475 else
9476 pt = marker_position (w->pointm);
9478 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9479 || pt != XINT (w->last_point))
9480 && check_point_in_composition (w->current_matrix->buffer,
9481 XINT (w->last_point),
9482 XBUFFER (w->buffer), pt))
9483 b->clip_changed = 1;
9487 #define STOP_POLLING \
9488 do { if (! polling_stopped_here) stop_polling (); \
9489 polling_stopped_here = 1; } while (0)
9491 #define RESUME_POLLING \
9492 do { if (polling_stopped_here) start_polling (); \
9493 polling_stopped_here = 0; } while (0)
9496 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9497 response to any user action; therefore, we should preserve the echo
9498 area. (Actually, our caller does that job.) Perhaps in the future
9499 avoid recentering windows if it is not necessary; currently that
9500 causes some problems. */
9502 static void
9503 redisplay_internal (preserve_echo_area)
9504 int preserve_echo_area;
9506 struct window *w = XWINDOW (selected_window);
9507 struct frame *f = XFRAME (w->frame);
9508 int pause;
9509 int must_finish = 0;
9510 struct text_pos tlbufpos, tlendpos;
9511 int number_of_visible_frames;
9512 int count;
9513 struct frame *sf = SELECTED_FRAME ();
9514 int polling_stopped_here = 0;
9516 /* Non-zero means redisplay has to consider all windows on all
9517 frames. Zero means, only selected_window is considered. */
9518 int consider_all_windows_p;
9520 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9522 /* No redisplay if running in batch mode or frame is not yet fully
9523 initialized, or redisplay is explicitly turned off by setting
9524 Vinhibit_redisplay. */
9525 if (noninteractive
9526 || !NILP (Vinhibit_redisplay)
9527 || !f->glyphs_initialized_p)
9528 return;
9530 /* The flag redisplay_performed_directly_p is set by
9531 direct_output_for_insert when it already did the whole screen
9532 update necessary. */
9533 if (redisplay_performed_directly_p)
9535 redisplay_performed_directly_p = 0;
9536 if (!hscroll_windows (selected_window))
9537 return;
9540 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9541 if (popup_activated ())
9542 return;
9543 #endif
9545 /* I don't think this happens but let's be paranoid. */
9546 if (redisplaying_p)
9547 return;
9549 /* Record a function that resets redisplaying_p to its old value
9550 when we leave this function. */
9551 count = SPECPDL_INDEX ();
9552 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9553 ++redisplaying_p;
9554 specbind (Qinhibit_free_realized_faces, Qnil);
9556 retry:
9557 pause = 0;
9558 reconsider_clip_changes (w, current_buffer);
9560 /* If new fonts have been loaded that make a glyph matrix adjustment
9561 necessary, do it. */
9562 if (fonts_changed_p)
9564 adjust_glyphs (NULL);
9565 ++windows_or_buffers_changed;
9566 fonts_changed_p = 0;
9569 /* If face_change_count is non-zero, init_iterator will free all
9570 realized faces, which includes the faces referenced from current
9571 matrices. So, we can't reuse current matrices in this case. */
9572 if (face_change_count)
9573 ++windows_or_buffers_changed;
9575 if (! FRAME_WINDOW_P (sf)
9576 && previous_terminal_frame != sf)
9578 /* Since frames on an ASCII terminal share the same display
9579 area, displaying a different frame means redisplay the whole
9580 thing. */
9581 windows_or_buffers_changed++;
9582 SET_FRAME_GARBAGED (sf);
9583 XSETFRAME (Vterminal_frame, sf);
9585 previous_terminal_frame = sf;
9587 /* Set the visible flags for all frames. Do this before checking
9588 for resized or garbaged frames; they want to know if their frames
9589 are visible. See the comment in frame.h for
9590 FRAME_SAMPLE_VISIBILITY. */
9592 Lisp_Object tail, frame;
9594 number_of_visible_frames = 0;
9596 FOR_EACH_FRAME (tail, frame)
9598 struct frame *f = XFRAME (frame);
9600 FRAME_SAMPLE_VISIBILITY (f);
9601 if (FRAME_VISIBLE_P (f))
9602 ++number_of_visible_frames;
9603 clear_desired_matrices (f);
9607 /* Notice any pending interrupt request to change frame size. */
9608 do_pending_window_change (1);
9610 /* Clear frames marked as garbaged. */
9611 if (frame_garbaged)
9612 clear_garbaged_frames ();
9614 /* Build menubar and tool-bar items. */
9615 prepare_menu_bars ();
9617 if (windows_or_buffers_changed)
9618 update_mode_lines++;
9620 /* Detect case that we need to write or remove a star in the mode line. */
9621 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9623 w->update_mode_line = Qt;
9624 if (buffer_shared > 1)
9625 update_mode_lines++;
9628 /* If %c is in the mode line, update it if needed. */
9629 if (!NILP (w->column_number_displayed)
9630 /* This alternative quickly identifies a common case
9631 where no change is needed. */
9632 && !(PT == XFASTINT (w->last_point)
9633 && XFASTINT (w->last_modified) >= MODIFF
9634 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9635 && (XFASTINT (w->column_number_displayed)
9636 != (int) current_column ())) /* iftc */
9637 w->update_mode_line = Qt;
9639 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9641 /* The variable buffer_shared is set in redisplay_window and
9642 indicates that we redisplay a buffer in different windows. See
9643 there. */
9644 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9645 || cursor_type_changed);
9647 /* If specs for an arrow have changed, do thorough redisplay
9648 to ensure we remove any arrow that should no longer exist. */
9649 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9650 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9651 consider_all_windows_p = windows_or_buffers_changed = 1;
9653 /* Normally the message* functions will have already displayed and
9654 updated the echo area, but the frame may have been trashed, or
9655 the update may have been preempted, so display the echo area
9656 again here. Checking message_cleared_p captures the case that
9657 the echo area should be cleared. */
9658 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9659 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9660 || (message_cleared_p
9661 && minibuf_level == 0
9662 /* If the mini-window is currently selected, this means the
9663 echo-area doesn't show through. */
9664 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9666 int window_height_changed_p = echo_area_display (0);
9667 must_finish = 1;
9669 /* If we don't display the current message, don't clear the
9670 message_cleared_p flag, because, if we did, we wouldn't clear
9671 the echo area in the next redisplay which doesn't preserve
9672 the echo area. */
9673 if (!display_last_displayed_message_p)
9674 message_cleared_p = 0;
9676 if (fonts_changed_p)
9677 goto retry;
9678 else if (window_height_changed_p)
9680 consider_all_windows_p = 1;
9681 ++update_mode_lines;
9682 ++windows_or_buffers_changed;
9684 /* If window configuration was changed, frames may have been
9685 marked garbaged. Clear them or we will experience
9686 surprises wrt scrolling. */
9687 if (frame_garbaged)
9688 clear_garbaged_frames ();
9691 else if (EQ (selected_window, minibuf_window)
9692 && (current_buffer->clip_changed
9693 || XFASTINT (w->last_modified) < MODIFF
9694 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9695 && resize_mini_window (w, 0))
9697 /* Resized active mini-window to fit the size of what it is
9698 showing if its contents might have changed. */
9699 must_finish = 1;
9700 consider_all_windows_p = 1;
9701 ++windows_or_buffers_changed;
9702 ++update_mode_lines;
9704 /* If window configuration was changed, frames may have been
9705 marked garbaged. Clear them or we will experience
9706 surprises wrt scrolling. */
9707 if (frame_garbaged)
9708 clear_garbaged_frames ();
9712 /* If showing the region, and mark has changed, we must redisplay
9713 the whole window. The assignment to this_line_start_pos prevents
9714 the optimization directly below this if-statement. */
9715 if (((!NILP (Vtransient_mark_mode)
9716 && !NILP (XBUFFER (w->buffer)->mark_active))
9717 != !NILP (w->region_showing))
9718 || (!NILP (w->region_showing)
9719 && !EQ (w->region_showing,
9720 Fmarker_position (XBUFFER (w->buffer)->mark))))
9721 CHARPOS (this_line_start_pos) = 0;
9723 /* Optimize the case that only the line containing the cursor in the
9724 selected window has changed. Variables starting with this_ are
9725 set in display_line and record information about the line
9726 containing the cursor. */
9727 tlbufpos = this_line_start_pos;
9728 tlendpos = this_line_end_pos;
9729 if (!consider_all_windows_p
9730 && CHARPOS (tlbufpos) > 0
9731 && NILP (w->update_mode_line)
9732 && !current_buffer->clip_changed
9733 && !current_buffer->prevent_redisplay_optimizations_p
9734 && FRAME_VISIBLE_P (XFRAME (w->frame))
9735 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9736 /* Make sure recorded data applies to current buffer, etc. */
9737 && this_line_buffer == current_buffer
9738 && current_buffer == XBUFFER (w->buffer)
9739 && NILP (w->force_start)
9740 && NILP (w->optional_new_start)
9741 /* Point must be on the line that we have info recorded about. */
9742 && PT >= CHARPOS (tlbufpos)
9743 && PT <= Z - CHARPOS (tlendpos)
9744 /* All text outside that line, including its final newline,
9745 must be unchanged */
9746 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9747 CHARPOS (tlendpos)))
9749 if (CHARPOS (tlbufpos) > BEGV
9750 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9751 && (CHARPOS (tlbufpos) == ZV
9752 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9753 /* Former continuation line has disappeared by becoming empty */
9754 goto cancel;
9755 else if (XFASTINT (w->last_modified) < MODIFF
9756 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9757 || MINI_WINDOW_P (w))
9759 /* We have to handle the case of continuation around a
9760 wide-column character (See the comment in indent.c around
9761 line 885).
9763 For instance, in the following case:
9765 -------- Insert --------
9766 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9767 J_I_ ==> J_I_ `^^' are cursors.
9768 ^^ ^^
9769 -------- --------
9771 As we have to redraw the line above, we should goto cancel. */
9773 struct it it;
9774 int line_height_before = this_line_pixel_height;
9776 /* Note that start_display will handle the case that the
9777 line starting at tlbufpos is a continuation lines. */
9778 start_display (&it, w, tlbufpos);
9780 /* Implementation note: It this still necessary? */
9781 if (it.current_x != this_line_start_x)
9782 goto cancel;
9784 TRACE ((stderr, "trying display optimization 1\n"));
9785 w->cursor.vpos = -1;
9786 overlay_arrow_seen = 0;
9787 it.vpos = this_line_vpos;
9788 it.current_y = this_line_y;
9789 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9790 display_line (&it);
9792 /* If line contains point, is not continued,
9793 and ends at same distance from eob as before, we win */
9794 if (w->cursor.vpos >= 0
9795 /* Line is not continued, otherwise this_line_start_pos
9796 would have been set to 0 in display_line. */
9797 && CHARPOS (this_line_start_pos)
9798 /* Line ends as before. */
9799 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9800 /* Line has same height as before. Otherwise other lines
9801 would have to be shifted up or down. */
9802 && this_line_pixel_height == line_height_before)
9804 /* If this is not the window's last line, we must adjust
9805 the charstarts of the lines below. */
9806 if (it.current_y < it.last_visible_y)
9808 struct glyph_row *row
9809 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9810 int delta, delta_bytes;
9812 if (Z - CHARPOS (tlendpos) == ZV)
9814 /* This line ends at end of (accessible part of)
9815 buffer. There is no newline to count. */
9816 delta = (Z
9817 - CHARPOS (tlendpos)
9818 - MATRIX_ROW_START_CHARPOS (row));
9819 delta_bytes = (Z_BYTE
9820 - BYTEPOS (tlendpos)
9821 - MATRIX_ROW_START_BYTEPOS (row));
9823 else
9825 /* This line ends in a newline. Must take
9826 account of the newline and the rest of the
9827 text that follows. */
9828 delta = (Z
9829 - CHARPOS (tlendpos)
9830 - MATRIX_ROW_START_CHARPOS (row));
9831 delta_bytes = (Z_BYTE
9832 - BYTEPOS (tlendpos)
9833 - MATRIX_ROW_START_BYTEPOS (row));
9836 increment_matrix_positions (w->current_matrix,
9837 this_line_vpos + 1,
9838 w->current_matrix->nrows,
9839 delta, delta_bytes);
9842 /* If this row displays text now but previously didn't,
9843 or vice versa, w->window_end_vpos may have to be
9844 adjusted. */
9845 if ((it.glyph_row - 1)->displays_text_p)
9847 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9848 XSETINT (w->window_end_vpos, this_line_vpos);
9850 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9851 && this_line_vpos > 0)
9852 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9853 w->window_end_valid = Qnil;
9855 /* Update hint: No need to try to scroll in update_window. */
9856 w->desired_matrix->no_scrolling_p = 1;
9858 #if GLYPH_DEBUG
9859 *w->desired_matrix->method = 0;
9860 debug_method_add (w, "optimization 1");
9861 #endif
9862 goto update;
9864 else
9865 goto cancel;
9867 else if (/* Cursor position hasn't changed. */
9868 PT == XFASTINT (w->last_point)
9869 /* Make sure the cursor was last displayed
9870 in this window. Otherwise we have to reposition it. */
9871 && 0 <= w->cursor.vpos
9872 && XINT (w->height) > w->cursor.vpos)
9874 if (!must_finish)
9876 do_pending_window_change (1);
9878 /* We used to always goto end_of_redisplay here, but this
9879 isn't enough if we have a blinking cursor. */
9880 if (w->cursor_off_p == w->last_cursor_off_p)
9881 goto end_of_redisplay;
9883 goto update;
9885 /* If highlighting the region, or if the cursor is in the echo area,
9886 then we can't just move the cursor. */
9887 else if (! (!NILP (Vtransient_mark_mode)
9888 && !NILP (current_buffer->mark_active))
9889 && (EQ (selected_window, current_buffer->last_selected_window)
9890 || highlight_nonselected_windows)
9891 && NILP (w->region_showing)
9892 && NILP (Vshow_trailing_whitespace)
9893 && !cursor_in_echo_area)
9895 struct it it;
9896 struct glyph_row *row;
9898 /* Skip from tlbufpos to PT and see where it is. Note that
9899 PT may be in invisible text. If so, we will end at the
9900 next visible position. */
9901 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9902 NULL, DEFAULT_FACE_ID);
9903 it.current_x = this_line_start_x;
9904 it.current_y = this_line_y;
9905 it.vpos = this_line_vpos;
9907 /* The call to move_it_to stops in front of PT, but
9908 moves over before-strings. */
9909 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9911 if (it.vpos == this_line_vpos
9912 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9913 row->enabled_p))
9915 xassert (this_line_vpos == it.vpos);
9916 xassert (this_line_y == it.current_y);
9917 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9918 #if GLYPH_DEBUG
9919 *w->desired_matrix->method = 0;
9920 debug_method_add (w, "optimization 3");
9921 #endif
9922 goto update;
9924 else
9925 goto cancel;
9928 cancel:
9929 /* Text changed drastically or point moved off of line. */
9930 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9933 CHARPOS (this_line_start_pos) = 0;
9934 consider_all_windows_p |= buffer_shared > 1;
9935 ++clear_face_cache_count;
9938 /* Build desired matrices, and update the display. If
9939 consider_all_windows_p is non-zero, do it for all windows on all
9940 frames. Otherwise do it for selected_window, only. */
9942 if (consider_all_windows_p)
9944 Lisp_Object tail, frame;
9945 int i, n = 0, size = 50;
9946 struct frame **updated
9947 = (struct frame **) alloca (size * sizeof *updated);
9949 /* Clear the face cache eventually. */
9950 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9952 clear_face_cache (0);
9953 clear_face_cache_count = 0;
9956 /* Recompute # windows showing selected buffer. This will be
9957 incremented each time such a window is displayed. */
9958 buffer_shared = 0;
9960 FOR_EACH_FRAME (tail, frame)
9962 struct frame *f = XFRAME (frame);
9964 if (FRAME_WINDOW_P (f) || f == sf)
9966 #ifdef HAVE_WINDOW_SYSTEM
9967 if (clear_face_cache_count % 50 == 0
9968 && FRAME_WINDOW_P (f))
9969 clear_image_cache (f, 0);
9970 #endif /* HAVE_WINDOW_SYSTEM */
9972 /* Mark all the scroll bars to be removed; we'll redeem
9973 the ones we want when we redisplay their windows. */
9974 if (condemn_scroll_bars_hook)
9975 condemn_scroll_bars_hook (f);
9977 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9978 redisplay_windows (FRAME_ROOT_WINDOW (f));
9980 /* Any scroll bars which redisplay_windows should have
9981 nuked should now go away. */
9982 if (judge_scroll_bars_hook)
9983 judge_scroll_bars_hook (f);
9985 /* If fonts changed, display again. */
9986 /* ??? rms: I suspect it is a mistake to jump all the way
9987 back to retry here. It should just retry this frame. */
9988 if (fonts_changed_p)
9989 goto retry;
9991 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9993 /* See if we have to hscroll. */
9994 if (hscroll_windows (f->root_window))
9995 goto retry;
9997 /* Prevent various kinds of signals during display
9998 update. stdio is not robust about handling
9999 signals, which can cause an apparent I/O
10000 error. */
10001 if (interrupt_input)
10002 unrequest_sigio ();
10003 STOP_POLLING;
10005 /* Update the display. */
10006 set_window_update_flags (XWINDOW (f->root_window), 1);
10007 pause |= update_frame (f, 0, 0);
10008 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10009 if (pause)
10010 break;
10011 #endif
10013 if (n == size)
10015 int nbytes = size * sizeof *updated;
10016 struct frame **p = (struct frame **) alloca (2 * nbytes);
10017 bcopy (updated, p, nbytes);
10018 size *= 2;
10021 updated[n++] = f;
10026 /* Do the mark_window_display_accurate after all windows have
10027 been redisplayed because this call resets flags in buffers
10028 which are needed for proper redisplay. */
10029 for (i = 0; i < n; ++i)
10031 struct frame *f = updated[i];
10032 mark_window_display_accurate (f->root_window, 1);
10033 if (frame_up_to_date_hook)
10034 frame_up_to_date_hook (f);
10037 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10039 Lisp_Object mini_window;
10040 struct frame *mini_frame;
10042 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10043 /* Use list_of_error, not Qerror, so that
10044 we catch only errors and don't run the debugger. */
10045 internal_condition_case_1 (redisplay_window_1, selected_window,
10046 list_of_error,
10047 redisplay_window_error);
10049 /* Compare desired and current matrices, perform output. */
10051 update:
10052 /* If fonts changed, display again. */
10053 if (fonts_changed_p)
10054 goto retry;
10056 /* Prevent various kinds of signals during display update.
10057 stdio is not robust about handling signals,
10058 which can cause an apparent I/O error. */
10059 if (interrupt_input)
10060 unrequest_sigio ();
10061 STOP_POLLING;
10063 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10065 if (hscroll_windows (selected_window))
10066 goto retry;
10068 XWINDOW (selected_window)->must_be_updated_p = 1;
10069 pause = update_frame (sf, 0, 0);
10072 /* We may have called echo_area_display at the top of this
10073 function. If the echo area is on another frame, that may
10074 have put text on a frame other than the selected one, so the
10075 above call to update_frame would not have caught it. Catch
10076 it here. */
10077 mini_window = FRAME_MINIBUF_WINDOW (sf);
10078 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10080 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10082 XWINDOW (mini_window)->must_be_updated_p = 1;
10083 pause |= update_frame (mini_frame, 0, 0);
10084 if (!pause && hscroll_windows (mini_window))
10085 goto retry;
10089 /* If display was paused because of pending input, make sure we do a
10090 thorough update the next time. */
10091 if (pause)
10093 /* Prevent the optimization at the beginning of
10094 redisplay_internal that tries a single-line update of the
10095 line containing the cursor in the selected window. */
10096 CHARPOS (this_line_start_pos) = 0;
10098 /* Let the overlay arrow be updated the next time. */
10099 if (!NILP (last_arrow_position))
10101 last_arrow_position = Qt;
10102 last_arrow_string = Qt;
10105 /* If we pause after scrolling, some rows in the current
10106 matrices of some windows are not valid. */
10107 if (!WINDOW_FULL_WIDTH_P (w)
10108 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10109 update_mode_lines = 1;
10111 else
10113 if (!consider_all_windows_p)
10115 /* This has already been done above if
10116 consider_all_windows_p is set. */
10117 mark_window_display_accurate_1 (w, 1);
10119 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10120 last_arrow_string = Voverlay_arrow_string;
10122 if (frame_up_to_date_hook != 0)
10123 frame_up_to_date_hook (sf);
10126 update_mode_lines = 0;
10127 windows_or_buffers_changed = 0;
10128 cursor_type_changed = 0;
10131 /* Start SIGIO interrupts coming again. Having them off during the
10132 code above makes it less likely one will discard output, but not
10133 impossible, since there might be stuff in the system buffer here.
10134 But it is much hairier to try to do anything about that. */
10135 if (interrupt_input)
10136 request_sigio ();
10137 RESUME_POLLING;
10139 /* If a frame has become visible which was not before, redisplay
10140 again, so that we display it. Expose events for such a frame
10141 (which it gets when becoming visible) don't call the parts of
10142 redisplay constructing glyphs, so simply exposing a frame won't
10143 display anything in this case. So, we have to display these
10144 frames here explicitly. */
10145 if (!pause)
10147 Lisp_Object tail, frame;
10148 int new_count = 0;
10150 FOR_EACH_FRAME (tail, frame)
10152 int this_is_visible = 0;
10154 if (XFRAME (frame)->visible)
10155 this_is_visible = 1;
10156 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10157 if (XFRAME (frame)->visible)
10158 this_is_visible = 1;
10160 if (this_is_visible)
10161 new_count++;
10164 if (new_count != number_of_visible_frames)
10165 windows_or_buffers_changed++;
10168 /* Change frame size now if a change is pending. */
10169 do_pending_window_change (1);
10171 /* If we just did a pending size change, or have additional
10172 visible frames, redisplay again. */
10173 if (windows_or_buffers_changed && !pause)
10174 goto retry;
10176 end_of_redisplay:
10177 unbind_to (count, Qnil);
10178 RESUME_POLLING;
10182 /* Redisplay, but leave alone any recent echo area message unless
10183 another message has been requested in its place.
10185 This is useful in situations where you need to redisplay but no
10186 user action has occurred, making it inappropriate for the message
10187 area to be cleared. See tracking_off and
10188 wait_reading_process_input for examples of these situations.
10190 FROM_WHERE is an integer saying from where this function was
10191 called. This is useful for debugging. */
10193 void
10194 redisplay_preserve_echo_area (from_where)
10195 int from_where;
10197 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10199 if (!NILP (echo_area_buffer[1]))
10201 /* We have a previously displayed message, but no current
10202 message. Redisplay the previous message. */
10203 display_last_displayed_message_p = 1;
10204 redisplay_internal (1);
10205 display_last_displayed_message_p = 0;
10207 else
10208 redisplay_internal (1);
10212 /* Function registered with record_unwind_protect in
10213 redisplay_internal. Reset redisplaying_p to the value it had
10214 before redisplay_internal was called, and clear
10215 prevent_freeing_realized_faces_p. */
10217 static Lisp_Object
10218 unwind_redisplay (old_redisplaying_p)
10219 Lisp_Object old_redisplaying_p;
10221 redisplaying_p = XFASTINT (old_redisplaying_p);
10222 return Qnil;
10226 /* Mark the display of window W as accurate or inaccurate. If
10227 ACCURATE_P is non-zero mark display of W as accurate. If
10228 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10229 redisplay_internal is called. */
10231 static void
10232 mark_window_display_accurate_1 (w, accurate_p)
10233 struct window *w;
10234 int accurate_p;
10236 if (BUFFERP (w->buffer))
10238 struct buffer *b = XBUFFER (w->buffer);
10240 w->last_modified
10241 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10242 w->last_overlay_modified
10243 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10244 w->last_had_star
10245 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10247 if (accurate_p)
10249 b->clip_changed = 0;
10250 b->prevent_redisplay_optimizations_p = 0;
10252 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10253 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10254 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10255 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10257 w->current_matrix->buffer = b;
10258 w->current_matrix->begv = BUF_BEGV (b);
10259 w->current_matrix->zv = BUF_ZV (b);
10261 w->last_cursor = w->cursor;
10262 w->last_cursor_off_p = w->cursor_off_p;
10264 if (w == XWINDOW (selected_window))
10265 w->last_point = make_number (BUF_PT (b));
10266 else
10267 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10271 if (accurate_p)
10273 w->window_end_valid = w->buffer;
10274 #if 0 /* This is incorrect with variable-height lines. */
10275 xassert (XINT (w->window_end_vpos)
10276 < (XINT (w->height)
10277 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10278 #endif
10279 w->update_mode_line = Qnil;
10284 /* Mark the display of windows in the window tree rooted at WINDOW as
10285 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10286 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10287 be redisplayed the next time redisplay_internal is called. */
10289 void
10290 mark_window_display_accurate (window, accurate_p)
10291 Lisp_Object window;
10292 int accurate_p;
10294 struct window *w;
10296 for (; !NILP (window); window = w->next)
10298 w = XWINDOW (window);
10299 mark_window_display_accurate_1 (w, accurate_p);
10301 if (!NILP (w->vchild))
10302 mark_window_display_accurate (w->vchild, accurate_p);
10303 if (!NILP (w->hchild))
10304 mark_window_display_accurate (w->hchild, accurate_p);
10307 if (accurate_p)
10309 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10310 last_arrow_string = Voverlay_arrow_string;
10312 else
10314 /* Force a thorough redisplay the next time by setting
10315 last_arrow_position and last_arrow_string to t, which is
10316 unequal to any useful value of Voverlay_arrow_... */
10317 last_arrow_position = Qt;
10318 last_arrow_string = Qt;
10323 /* Return value in display table DP (Lisp_Char_Table *) for character
10324 C. Since a display table doesn't have any parent, we don't have to
10325 follow parent. Do not call this function directly but use the
10326 macro DISP_CHAR_VECTOR. */
10328 Lisp_Object
10329 disp_char_vector (dp, c)
10330 struct Lisp_Char_Table *dp;
10331 int c;
10333 int code[4], i;
10334 Lisp_Object val;
10336 if (SINGLE_BYTE_CHAR_P (c))
10337 return (dp->contents[c]);
10339 SPLIT_CHAR (c, code[0], code[1], code[2]);
10340 if (code[1] < 32)
10341 code[1] = -1;
10342 else if (code[2] < 32)
10343 code[2] = -1;
10345 /* Here, the possible range of code[0] (== charset ID) is
10346 128..max_charset. Since the top level char table contains data
10347 for multibyte characters after 256th element, we must increment
10348 code[0] by 128 to get a correct index. */
10349 code[0] += 128;
10350 code[3] = -1; /* anchor */
10352 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10354 val = dp->contents[code[i]];
10355 if (!SUB_CHAR_TABLE_P (val))
10356 return (NILP (val) ? dp->defalt : val);
10359 /* Here, val is a sub char table. We return the default value of
10360 it. */
10361 return (dp->defalt);
10366 /***********************************************************************
10367 Window Redisplay
10368 ***********************************************************************/
10370 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10372 static void
10373 redisplay_windows (window)
10374 Lisp_Object window;
10376 while (!NILP (window))
10378 struct window *w = XWINDOW (window);
10380 if (!NILP (w->hchild))
10381 redisplay_windows (w->hchild);
10382 else if (!NILP (w->vchild))
10383 redisplay_windows (w->vchild);
10384 else
10386 displayed_buffer = XBUFFER (w->buffer);
10387 /* Use list_of_error, not Qerror, so that
10388 we catch only errors and don't run the debugger. */
10389 internal_condition_case_1 (redisplay_window_0, window,
10390 list_of_error,
10391 redisplay_window_error);
10394 window = w->next;
10398 static Lisp_Object
10399 redisplay_window_error ()
10401 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10402 return Qnil;
10405 static Lisp_Object
10406 redisplay_window_0 (window)
10407 Lisp_Object window;
10409 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10410 redisplay_window (window, 0);
10411 return Qnil;
10414 static Lisp_Object
10415 redisplay_window_1 (window)
10416 Lisp_Object window;
10418 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10419 redisplay_window (window, 1);
10420 return Qnil;
10424 /* Increment GLYPH until it reaches END or CONDITION fails while
10425 adding (GLYPH)->pixel_width to X. */
10427 #define SKIP_GLYPHS(glyph, end, x, condition) \
10428 do \
10430 (x) += (glyph)->pixel_width; \
10431 ++(glyph); \
10433 while ((glyph) < (end) && (condition))
10436 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10437 DELTA is the number of bytes by which positions recorded in ROW
10438 differ from current buffer positions. */
10440 void
10441 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10442 struct window *w;
10443 struct glyph_row *row;
10444 struct glyph_matrix *matrix;
10445 int delta, delta_bytes, dy, dvpos;
10447 struct glyph *glyph = row->glyphs[TEXT_AREA];
10448 struct glyph *end = glyph + row->used[TEXT_AREA];
10449 /* The first glyph that starts a sequence of glyphs from string. */
10450 struct glyph *string_start;
10451 /* The X coordinate of string_start. */
10452 int string_start_x;
10453 /* The last known character position. */
10454 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10455 /* The last known character position before string_start. */
10456 int string_before_pos;
10457 int x = row->x;
10458 int pt_old = PT - delta;
10460 /* Skip over glyphs not having an object at the start of the row.
10461 These are special glyphs like truncation marks on terminal
10462 frames. */
10463 if (row->displays_text_p)
10464 while (glyph < end
10465 && INTEGERP (glyph->object)
10466 && glyph->charpos < 0)
10468 x += glyph->pixel_width;
10469 ++glyph;
10472 string_start = NULL;
10473 while (glyph < end
10474 && !INTEGERP (glyph->object)
10475 && (!BUFFERP (glyph->object)
10476 || (last_pos = glyph->charpos) < pt_old))
10478 if (! STRINGP (glyph->object))
10480 string_start = NULL;
10481 x += glyph->pixel_width;
10482 ++glyph;
10484 else
10486 string_before_pos = last_pos;
10487 string_start = glyph;
10488 string_start_x = x;
10489 /* Skip all glyphs from string. */
10490 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10494 if (string_start
10495 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10497 /* We may have skipped over point because the previous glyphs
10498 are from string. As there's no easy way to know the
10499 character position of the current glyph, find the correct
10500 glyph on point by scanning from string_start again. */
10501 Lisp_Object limit;
10502 Lisp_Object string;
10503 int pos;
10505 limit = make_number (pt_old + 1);
10506 end = glyph;
10507 glyph = string_start;
10508 x = string_start_x;
10509 string = glyph->object;
10510 pos = string_buffer_position (w, string, string_before_pos);
10511 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10512 because we always put cursor after overlay strings. */
10513 while (pos == 0 && glyph < end)
10515 string = glyph->object;
10516 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10517 if (glyph < end)
10518 pos = string_buffer_position (w, glyph->object, string_before_pos);
10521 while (glyph < end)
10523 pos = XINT (Fnext_single_char_property_change
10524 (make_number (pos), Qdisplay, Qnil, limit));
10525 if (pos > pt_old)
10526 break;
10527 /* Skip glyphs from the same string. */
10528 string = glyph->object;
10529 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10530 /* Skip glyphs from an overlay. */
10531 while (glyph < end
10532 && ! string_buffer_position (w, glyph->object, pos))
10534 string = glyph->object;
10535 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10540 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10541 w->cursor.x = x;
10542 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10543 w->cursor.y = row->y + dy;
10545 if (w == XWINDOW (selected_window))
10547 if (!row->continued_p
10548 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10549 && row->x == 0)
10551 this_line_buffer = XBUFFER (w->buffer);
10553 CHARPOS (this_line_start_pos)
10554 = MATRIX_ROW_START_CHARPOS (row) + delta;
10555 BYTEPOS (this_line_start_pos)
10556 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10558 CHARPOS (this_line_end_pos)
10559 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10560 BYTEPOS (this_line_end_pos)
10561 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10563 this_line_y = w->cursor.y;
10564 this_line_pixel_height = row->height;
10565 this_line_vpos = w->cursor.vpos;
10566 this_line_start_x = row->x;
10568 else
10569 CHARPOS (this_line_start_pos) = 0;
10574 /* Run window scroll functions, if any, for WINDOW with new window
10575 start STARTP. Sets the window start of WINDOW to that position.
10577 We assume that the window's buffer is really current. */
10579 static INLINE struct text_pos
10580 run_window_scroll_functions (window, startp)
10581 Lisp_Object window;
10582 struct text_pos startp;
10584 struct window *w = XWINDOW (window);
10585 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10587 if (current_buffer != XBUFFER (w->buffer))
10588 abort ();
10590 if (!NILP (Vwindow_scroll_functions))
10592 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10593 make_number (CHARPOS (startp)));
10594 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10595 /* In case the hook functions switch buffers. */
10596 if (current_buffer != XBUFFER (w->buffer))
10597 set_buffer_internal_1 (XBUFFER (w->buffer));
10600 return startp;
10604 /* Make sure the line containing the cursor is fully visible.
10605 A value of 1 means there is nothing to be done.
10606 (Either the line is fully visible, or it cannot be made so,
10607 or we cannot tell.)
10608 A value of 0 means the caller should do scrolling
10609 as if point had gone off the screen. */
10611 static int
10612 make_cursor_line_fully_visible (w)
10613 struct window *w;
10615 struct glyph_matrix *matrix;
10616 struct glyph_row *row;
10617 int window_height;
10619 /* It's not always possible to find the cursor, e.g, when a window
10620 is full of overlay strings. Don't do anything in that case. */
10621 if (w->cursor.vpos < 0)
10622 return 1;
10624 matrix = w->desired_matrix;
10625 row = MATRIX_ROW (matrix, w->cursor.vpos);
10627 /* If the cursor row is not partially visible, there's nothing to do. */
10628 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10629 return 1;
10631 /* If the row the cursor is in is taller than the window's height,
10632 it's not clear what to do, so do nothing. */
10633 window_height = window_box_height (w);
10634 if (row->height >= window_height)
10635 return 1;
10637 return 0;
10639 #if 0
10640 /* This code used to try to scroll the window just enough to make
10641 the line visible. It returned 0 to say that the caller should
10642 allocate larger glyph matrices. */
10644 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10646 int dy = row->height - row->visible_height;
10647 w->vscroll = 0;
10648 w->cursor.y += dy;
10649 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10651 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10653 int dy = - (row->height - row->visible_height);
10654 w->vscroll = dy;
10655 w->cursor.y += dy;
10656 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10659 /* When we change the cursor y-position of the selected window,
10660 change this_line_y as well so that the display optimization for
10661 the cursor line of the selected window in redisplay_internal uses
10662 the correct y-position. */
10663 if (w == XWINDOW (selected_window))
10664 this_line_y = w->cursor.y;
10666 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10667 redisplay with larger matrices. */
10668 if (matrix->nrows < required_matrix_height (w))
10670 fonts_changed_p = 1;
10671 return 0;
10674 return 1;
10675 #endif /* 0 */
10679 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10680 non-zero means only WINDOW is redisplayed in redisplay_internal.
10681 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10682 in redisplay_window to bring a partially visible line into view in
10683 the case that only the cursor has moved.
10685 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10686 last screen line's vertical height extends past the end of the screen.
10688 Value is
10690 1 if scrolling succeeded
10692 0 if scrolling didn't find point.
10694 -1 if new fonts have been loaded so that we must interrupt
10695 redisplay, adjust glyph matrices, and try again. */
10697 enum
10699 SCROLLING_SUCCESS,
10700 SCROLLING_FAILED,
10701 SCROLLING_NEED_LARGER_MATRICES
10704 static int
10705 try_scrolling (window, just_this_one_p, scroll_conservatively,
10706 scroll_step, temp_scroll_step, last_line_misfit)
10707 Lisp_Object window;
10708 int just_this_one_p;
10709 EMACS_INT scroll_conservatively, scroll_step;
10710 int temp_scroll_step;
10711 int last_line_misfit;
10713 struct window *w = XWINDOW (window);
10714 struct frame *f = XFRAME (w->frame);
10715 struct text_pos scroll_margin_pos;
10716 struct text_pos pos;
10717 struct text_pos startp;
10718 struct it it;
10719 Lisp_Object window_end;
10720 int this_scroll_margin;
10721 int dy = 0;
10722 int scroll_max;
10723 int rc;
10724 int amount_to_scroll = 0;
10725 Lisp_Object aggressive;
10726 int height;
10727 int end_scroll_margin;
10729 #if GLYPH_DEBUG
10730 debug_method_add (w, "try_scrolling");
10731 #endif
10733 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10735 /* Compute scroll margin height in pixels. We scroll when point is
10736 within this distance from the top or bottom of the window. */
10737 if (scroll_margin > 0)
10739 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
10740 this_scroll_margin *= CANON_Y_UNIT (f);
10742 else
10743 this_scroll_margin = 0;
10745 /* Compute how much we should try to scroll maximally to bring point
10746 into view. */
10747 if (scroll_step || scroll_conservatively || temp_scroll_step)
10748 scroll_max = max (scroll_step,
10749 max (scroll_conservatively, temp_scroll_step));
10750 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10751 || NUMBERP (current_buffer->scroll_up_aggressively))
10752 /* We're trying to scroll because of aggressive scrolling
10753 but no scroll_step is set. Choose an arbitrary one. Maybe
10754 there should be a variable for this. */
10755 scroll_max = 10;
10756 else
10757 scroll_max = 0;
10758 scroll_max *= CANON_Y_UNIT (f);
10760 /* Decide whether we have to scroll down. Start at the window end
10761 and move this_scroll_margin up to find the position of the scroll
10762 margin. */
10763 window_end = Fwindow_end (window, Qt);
10765 too_near_end:
10767 CHARPOS (scroll_margin_pos) = XINT (window_end);
10768 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10770 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10771 if (end_scroll_margin)
10773 start_display (&it, w, scroll_margin_pos);
10774 move_it_vertically (&it, - end_scroll_margin);
10775 scroll_margin_pos = it.current.pos;
10778 if (PT >= CHARPOS (scroll_margin_pos))
10780 int y0;
10782 /* Point is in the scroll margin at the bottom of the window, or
10783 below. Compute a new window start that makes point visible. */
10785 /* Compute the distance from the scroll margin to PT.
10786 Give up if the distance is greater than scroll_max. */
10787 start_display (&it, w, scroll_margin_pos);
10788 y0 = it.current_y;
10789 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10790 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10792 /* To make point visible, we have to move the window start
10793 down so that the line the cursor is in is visible, which
10794 means we have to add in the height of the cursor line. */
10795 dy = line_bottom_y (&it) - y0;
10797 if (dy > scroll_max)
10798 return SCROLLING_FAILED;
10800 /* Move the window start down. If scrolling conservatively,
10801 move it just enough down to make point visible. If
10802 scroll_step is set, move it down by scroll_step. */
10803 start_display (&it, w, startp);
10805 if (scroll_conservatively)
10806 /* Set AMOUNT_TO_SCROLL to at least one line,
10807 and at most scroll_conservatively lines. */
10808 amount_to_scroll
10809 = min (max (dy, CANON_Y_UNIT (f)),
10810 CANON_Y_UNIT (f) * scroll_conservatively);
10811 else if (scroll_step || temp_scroll_step)
10812 amount_to_scroll = scroll_max;
10813 else
10815 aggressive = current_buffer->scroll_up_aggressively;
10816 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10817 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10818 if (NUMBERP (aggressive))
10819 amount_to_scroll = XFLOATINT (aggressive) * height;
10822 if (amount_to_scroll <= 0)
10823 return SCROLLING_FAILED;
10825 /* If moving by amount_to_scroll leaves STARTP unchanged,
10826 move it down one screen line. */
10828 move_it_vertically (&it, amount_to_scroll);
10829 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10830 move_it_by_lines (&it, 1, 1);
10831 startp = it.current.pos;
10833 else
10835 /* See if point is inside the scroll margin at the top of the
10836 window. */
10837 scroll_margin_pos = startp;
10838 if (this_scroll_margin)
10840 start_display (&it, w, startp);
10841 move_it_vertically (&it, this_scroll_margin);
10842 scroll_margin_pos = it.current.pos;
10845 if (PT < CHARPOS (scroll_margin_pos))
10847 /* Point is in the scroll margin at the top of the window or
10848 above what is displayed in the window. */
10849 int y0;
10851 /* Compute the vertical distance from PT to the scroll
10852 margin position. Give up if distance is greater than
10853 scroll_max. */
10854 SET_TEXT_POS (pos, PT, PT_BYTE);
10855 start_display (&it, w, pos);
10856 y0 = it.current_y;
10857 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10858 it.last_visible_y, -1,
10859 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10860 dy = it.current_y - y0;
10861 if (dy > scroll_max)
10862 return SCROLLING_FAILED;
10864 /* Compute new window start. */
10865 start_display (&it, w, startp);
10867 if (scroll_conservatively)
10868 amount_to_scroll =
10869 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
10870 else if (scroll_step || temp_scroll_step)
10871 amount_to_scroll = scroll_max;
10872 else
10874 aggressive = current_buffer->scroll_down_aggressively;
10875 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10876 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10877 if (NUMBERP (aggressive))
10878 amount_to_scroll = XFLOATINT (aggressive) * height;
10881 if (amount_to_scroll <= 0)
10882 return SCROLLING_FAILED;
10884 move_it_vertically (&it, - amount_to_scroll);
10885 startp = it.current.pos;
10889 /* Run window scroll functions. */
10890 startp = run_window_scroll_functions (window, startp);
10892 /* Display the window. Give up if new fonts are loaded, or if point
10893 doesn't appear. */
10894 if (!try_window (window, startp))
10895 rc = SCROLLING_NEED_LARGER_MATRICES;
10896 else if (w->cursor.vpos < 0)
10898 clear_glyph_matrix (w->desired_matrix);
10899 rc = SCROLLING_FAILED;
10901 else
10903 /* Maybe forget recorded base line for line number display. */
10904 if (!just_this_one_p
10905 || current_buffer->clip_changed
10906 || BEG_UNCHANGED < CHARPOS (startp))
10907 w->base_line_number = Qnil;
10909 /* If cursor ends up on a partially visible line,
10910 treat that as being off the bottom of the screen. */
10911 if (! make_cursor_line_fully_visible (w))
10913 clear_glyph_matrix (w->desired_matrix);
10914 last_line_misfit = 1;
10915 goto too_near_end;
10917 rc = SCROLLING_SUCCESS;
10920 return rc;
10924 /* Compute a suitable window start for window W if display of W starts
10925 on a continuation line. Value is non-zero if a new window start
10926 was computed.
10928 The new window start will be computed, based on W's width, starting
10929 from the start of the continued line. It is the start of the
10930 screen line with the minimum distance from the old start W->start. */
10932 static int
10933 compute_window_start_on_continuation_line (w)
10934 struct window *w;
10936 struct text_pos pos, start_pos;
10937 int window_start_changed_p = 0;
10939 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10941 /* If window start is on a continuation line... Window start may be
10942 < BEGV in case there's invisible text at the start of the
10943 buffer (M-x rmail, for example). */
10944 if (CHARPOS (start_pos) > BEGV
10945 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10947 struct it it;
10948 struct glyph_row *row;
10950 /* Handle the case that the window start is out of range. */
10951 if (CHARPOS (start_pos) < BEGV)
10952 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10953 else if (CHARPOS (start_pos) > ZV)
10954 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10956 /* Find the start of the continued line. This should be fast
10957 because scan_buffer is fast (newline cache). */
10958 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
10959 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10960 row, DEFAULT_FACE_ID);
10961 reseat_at_previous_visible_line_start (&it);
10963 /* If the line start is "too far" away from the window start,
10964 say it takes too much time to compute a new window start. */
10965 if (CHARPOS (start_pos) - IT_CHARPOS (it)
10966 < XFASTINT (w->height) * XFASTINT (w->width))
10968 int min_distance, distance;
10970 /* Move forward by display lines to find the new window
10971 start. If window width was enlarged, the new start can
10972 be expected to be > the old start. If window width was
10973 decreased, the new window start will be < the old start.
10974 So, we're looking for the display line start with the
10975 minimum distance from the old window start. */
10976 pos = it.current.pos;
10977 min_distance = INFINITY;
10978 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10979 distance < min_distance)
10981 min_distance = distance;
10982 pos = it.current.pos;
10983 move_it_by_lines (&it, 1, 0);
10986 /* Set the window start there. */
10987 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10988 window_start_changed_p = 1;
10992 return window_start_changed_p;
10996 /* Try cursor movement in case text has not changed in window WINDOW,
10997 with window start STARTP. Value is
10999 CURSOR_MOVEMENT_SUCCESS if successful
11001 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11003 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11004 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11005 we want to scroll as if scroll-step were set to 1. See the code.
11007 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11008 which case we have to abort this redisplay, and adjust matrices
11009 first. */
11011 enum
11013 CURSOR_MOVEMENT_SUCCESS,
11014 CURSOR_MOVEMENT_CANNOT_BE_USED,
11015 CURSOR_MOVEMENT_MUST_SCROLL,
11016 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11019 static int
11020 try_cursor_movement (window, startp, scroll_step)
11021 Lisp_Object window;
11022 struct text_pos startp;
11023 int *scroll_step;
11025 struct window *w = XWINDOW (window);
11026 struct frame *f = XFRAME (w->frame);
11027 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11029 #if GLYPH_DEBUG
11030 if (inhibit_try_cursor_movement)
11031 return rc;
11032 #endif
11034 /* Handle case where text has not changed, only point, and it has
11035 not moved off the frame. */
11036 if (/* Point may be in this window. */
11037 PT >= CHARPOS (startp)
11038 /* Selective display hasn't changed. */
11039 && !current_buffer->clip_changed
11040 /* Function force-mode-line-update is used to force a thorough
11041 redisplay. It sets either windows_or_buffers_changed or
11042 update_mode_lines. So don't take a shortcut here for these
11043 cases. */
11044 && !update_mode_lines
11045 && !windows_or_buffers_changed
11046 && !cursor_type_changed
11047 /* Can't use this case if highlighting a region. When a
11048 region exists, cursor movement has to do more than just
11049 set the cursor. */
11050 && !(!NILP (Vtransient_mark_mode)
11051 && !NILP (current_buffer->mark_active))
11052 && NILP (w->region_showing)
11053 && NILP (Vshow_trailing_whitespace)
11054 /* Right after splitting windows, last_point may be nil. */
11055 && INTEGERP (w->last_point)
11056 /* This code is not used for mini-buffer for the sake of the case
11057 of redisplaying to replace an echo area message; since in
11058 that case the mini-buffer contents per se are usually
11059 unchanged. This code is of no real use in the mini-buffer
11060 since the handling of this_line_start_pos, etc., in redisplay
11061 handles the same cases. */
11062 && !EQ (window, minibuf_window)
11063 /* When splitting windows or for new windows, it happens that
11064 redisplay is called with a nil window_end_vpos or one being
11065 larger than the window. This should really be fixed in
11066 window.c. I don't have this on my list, now, so we do
11067 approximately the same as the old redisplay code. --gerd. */
11068 && INTEGERP (w->window_end_vpos)
11069 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11070 && (FRAME_WINDOW_P (f)
11071 || !MARKERP (Voverlay_arrow_position)
11072 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11074 int this_scroll_margin;
11075 struct glyph_row *row = NULL;
11077 #if GLYPH_DEBUG
11078 debug_method_add (w, "cursor movement");
11079 #endif
11081 /* Scroll if point within this distance from the top or bottom
11082 of the window. This is a pixel value. */
11083 this_scroll_margin = max (0, scroll_margin);
11084 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
11085 this_scroll_margin *= CANON_Y_UNIT (f);
11087 /* Start with the row the cursor was displayed during the last
11088 not paused redisplay. Give up if that row is not valid. */
11089 if (w->last_cursor.vpos < 0
11090 || w->last_cursor.vpos >= w->current_matrix->nrows)
11091 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11092 else
11094 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11095 if (row->mode_line_p)
11096 ++row;
11097 if (!row->enabled_p)
11098 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11101 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11103 int scroll_p = 0;
11104 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11106 if (PT > XFASTINT (w->last_point))
11108 /* Point has moved forward. */
11109 while (MATRIX_ROW_END_CHARPOS (row) < PT
11110 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11112 xassert (row->enabled_p);
11113 ++row;
11116 /* The end position of a row equals the start position
11117 of the next row. If PT is there, we would rather
11118 display it in the next line. */
11119 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11120 && MATRIX_ROW_END_CHARPOS (row) == PT
11121 && !cursor_row_p (w, row))
11122 ++row;
11124 /* If within the scroll margin, scroll. Note that
11125 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11126 the next line would be drawn, and that
11127 this_scroll_margin can be zero. */
11128 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11129 || PT > MATRIX_ROW_END_CHARPOS (row)
11130 /* Line is completely visible last line in window
11131 and PT is to be set in the next line. */
11132 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11133 && PT == MATRIX_ROW_END_CHARPOS (row)
11134 && !row->ends_at_zv_p
11135 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11136 scroll_p = 1;
11138 else if (PT < XFASTINT (w->last_point))
11140 /* Cursor has to be moved backward. Note that PT >=
11141 CHARPOS (startp) because of the outer
11142 if-statement. */
11143 while (!row->mode_line_p
11144 && (MATRIX_ROW_START_CHARPOS (row) > PT
11145 || (MATRIX_ROW_START_CHARPOS (row) == PT
11146 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11147 && (row->y > this_scroll_margin
11148 || CHARPOS (startp) == BEGV))
11150 xassert (row->enabled_p);
11151 --row;
11154 /* Consider the following case: Window starts at BEGV,
11155 there is invisible, intangible text at BEGV, so that
11156 display starts at some point START > BEGV. It can
11157 happen that we are called with PT somewhere between
11158 BEGV and START. Try to handle that case. */
11159 if (row < w->current_matrix->rows
11160 || row->mode_line_p)
11162 row = w->current_matrix->rows;
11163 if (row->mode_line_p)
11164 ++row;
11167 /* Due to newlines in overlay strings, we may have to
11168 skip forward over overlay strings. */
11169 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11170 && MATRIX_ROW_END_CHARPOS (row) == PT
11171 && !cursor_row_p (w, row))
11172 ++row;
11174 /* If within the scroll margin, scroll. */
11175 if (row->y < this_scroll_margin
11176 && CHARPOS (startp) != BEGV)
11177 scroll_p = 1;
11180 if (PT < MATRIX_ROW_START_CHARPOS (row)
11181 || PT > MATRIX_ROW_END_CHARPOS (row))
11183 /* if PT is not in the glyph row, give up. */
11184 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11186 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11188 if (PT == MATRIX_ROW_END_CHARPOS (row)
11189 && !row->ends_at_zv_p
11190 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11192 else if (row->height > window_box_height (w))
11194 /* If we end up in a partially visible line, let's
11195 make it fully visible, except when it's taller
11196 than the window, in which case we can't do much
11197 about it. */
11198 *scroll_step = 1;
11199 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11201 else
11203 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11204 if (!make_cursor_line_fully_visible (w))
11205 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11206 else
11207 rc = CURSOR_MOVEMENT_SUCCESS;
11210 else if (scroll_p)
11211 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11212 else
11214 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11215 rc = CURSOR_MOVEMENT_SUCCESS;
11220 return rc;
11223 void
11224 set_vertical_scroll_bar (w)
11225 struct window *w;
11227 int start, end, whole;
11229 /* Calculate the start and end positions for the current window.
11230 At some point, it would be nice to choose between scrollbars
11231 which reflect the whole buffer size, with special markers
11232 indicating narrowing, and scrollbars which reflect only the
11233 visible region.
11235 Note that mini-buffers sometimes aren't displaying any text. */
11236 if (!MINI_WINDOW_P (w)
11237 || (w == XWINDOW (minibuf_window)
11238 && NILP (echo_area_buffer[0])))
11240 struct buffer *buf = XBUFFER (w->buffer);
11241 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11242 start = marker_position (w->start) - BUF_BEGV (buf);
11243 /* I don't think this is guaranteed to be right. For the
11244 moment, we'll pretend it is. */
11245 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11247 if (end < start)
11248 end = start;
11249 if (whole < (end - start))
11250 whole = end - start;
11252 else
11253 start = end = whole = 0;
11255 /* Indicate what this scroll bar ought to be displaying now. */
11256 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11259 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11260 selected_window is redisplayed.
11262 We can return without actually redisplaying the window if
11263 fonts_changed_p is nonzero. In that case, redisplay_internal will
11264 retry. */
11266 static void
11267 redisplay_window (window, just_this_one_p)
11268 Lisp_Object window;
11269 int just_this_one_p;
11271 struct window *w = XWINDOW (window);
11272 struct frame *f = XFRAME (w->frame);
11273 struct buffer *buffer = XBUFFER (w->buffer);
11274 struct buffer *old = current_buffer;
11275 struct text_pos lpoint, opoint, startp;
11276 int update_mode_line;
11277 int tem;
11278 struct it it;
11279 /* Record it now because it's overwritten. */
11280 int current_matrix_up_to_date_p = 0;
11281 /* This is less strict than current_matrix_up_to_date_p.
11282 It indictes that the buffer contents and narrowing are unchanged. */
11283 int buffer_unchanged_p = 0;
11284 int temp_scroll_step = 0;
11285 int count = SPECPDL_INDEX ();
11286 int rc;
11287 int centering_position;
11288 int last_line_misfit = 0;
11290 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11291 opoint = lpoint;
11293 /* W must be a leaf window here. */
11294 xassert (!NILP (w->buffer));
11295 #if GLYPH_DEBUG
11296 *w->desired_matrix->method = 0;
11297 #endif
11299 specbind (Qinhibit_point_motion_hooks, Qt);
11301 reconsider_clip_changes (w, buffer);
11303 /* Has the mode line to be updated? */
11304 update_mode_line = (!NILP (w->update_mode_line)
11305 || update_mode_lines
11306 || buffer->clip_changed
11307 || buffer->prevent_redisplay_optimizations_p);
11309 if (MINI_WINDOW_P (w))
11311 if (w == XWINDOW (echo_area_window)
11312 && !NILP (echo_area_buffer[0]))
11314 if (update_mode_line)
11315 /* We may have to update a tty frame's menu bar or a
11316 tool-bar. Example `M-x C-h C-h C-g'. */
11317 goto finish_menu_bars;
11318 else
11319 /* We've already displayed the echo area glyphs in this window. */
11320 goto finish_scroll_bars;
11322 else if ((w != XWINDOW (minibuf_window)
11323 || minibuf_level == 0)
11324 /* When buffer is nonempty, redisplay window normally. */
11325 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11326 /* Quail displays non-mini buffers in minibuffer window.
11327 In that case, redisplay the window normally. */
11328 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11330 /* W is a mini-buffer window, but it's not active, so clear
11331 it. */
11332 int yb = window_text_bottom_y (w);
11333 struct glyph_row *row;
11334 int y;
11336 for (y = 0, row = w->desired_matrix->rows;
11337 y < yb;
11338 y += row->height, ++row)
11339 blank_row (w, row, y);
11340 goto finish_scroll_bars;
11343 clear_glyph_matrix (w->desired_matrix);
11346 /* Otherwise set up data on this window; select its buffer and point
11347 value. */
11348 /* Really select the buffer, for the sake of buffer-local
11349 variables. */
11350 set_buffer_internal_1 (XBUFFER (w->buffer));
11351 SET_TEXT_POS (opoint, PT, PT_BYTE);
11353 current_matrix_up_to_date_p
11354 = (!NILP (w->window_end_valid)
11355 && !current_buffer->clip_changed
11356 && !current_buffer->prevent_redisplay_optimizations_p
11357 && XFASTINT (w->last_modified) >= MODIFF
11358 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11360 buffer_unchanged_p
11361 = (!NILP (w->window_end_valid)
11362 && !current_buffer->clip_changed
11363 && XFASTINT (w->last_modified) >= MODIFF
11364 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11366 /* When windows_or_buffers_changed is non-zero, we can't rely on
11367 the window end being valid, so set it to nil there. */
11368 if (windows_or_buffers_changed)
11370 /* If window starts on a continuation line, maybe adjust the
11371 window start in case the window's width changed. */
11372 if (XMARKER (w->start)->buffer == current_buffer)
11373 compute_window_start_on_continuation_line (w);
11375 w->window_end_valid = Qnil;
11378 /* Some sanity checks. */
11379 CHECK_WINDOW_END (w);
11380 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11381 abort ();
11382 if (BYTEPOS (opoint) < CHARPOS (opoint))
11383 abort ();
11385 /* If %c is in mode line, update it if needed. */
11386 if (!NILP (w->column_number_displayed)
11387 /* This alternative quickly identifies a common case
11388 where no change is needed. */
11389 && !(PT == XFASTINT (w->last_point)
11390 && XFASTINT (w->last_modified) >= MODIFF
11391 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11392 && (XFASTINT (w->column_number_displayed)
11393 != (int) current_column ())) /* iftc */
11394 update_mode_line = 1;
11396 /* Count number of windows showing the selected buffer. An indirect
11397 buffer counts as its base buffer. */
11398 if (!just_this_one_p)
11400 struct buffer *current_base, *window_base;
11401 current_base = current_buffer;
11402 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11403 if (current_base->base_buffer)
11404 current_base = current_base->base_buffer;
11405 if (window_base->base_buffer)
11406 window_base = window_base->base_buffer;
11407 if (current_base == window_base)
11408 buffer_shared++;
11411 /* Point refers normally to the selected window. For any other
11412 window, set up appropriate value. */
11413 if (!EQ (window, selected_window))
11415 int new_pt = XMARKER (w->pointm)->charpos;
11416 int new_pt_byte = marker_byte_position (w->pointm);
11417 if (new_pt < BEGV)
11419 new_pt = BEGV;
11420 new_pt_byte = BEGV_BYTE;
11421 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11423 else if (new_pt > (ZV - 1))
11425 new_pt = ZV;
11426 new_pt_byte = ZV_BYTE;
11427 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11430 /* We don't use SET_PT so that the point-motion hooks don't run. */
11431 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11434 /* If any of the character widths specified in the display table
11435 have changed, invalidate the width run cache. It's true that
11436 this may be a bit late to catch such changes, but the rest of
11437 redisplay goes (non-fatally) haywire when the display table is
11438 changed, so why should we worry about doing any better? */
11439 if (current_buffer->width_run_cache)
11441 struct Lisp_Char_Table *disptab = buffer_display_table ();
11443 if (! disptab_matches_widthtab (disptab,
11444 XVECTOR (current_buffer->width_table)))
11446 invalidate_region_cache (current_buffer,
11447 current_buffer->width_run_cache,
11448 BEG, Z);
11449 recompute_width_table (current_buffer, disptab);
11453 /* If window-start is screwed up, choose a new one. */
11454 if (XMARKER (w->start)->buffer != current_buffer)
11455 goto recenter;
11457 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11459 /* If someone specified a new starting point but did not insist,
11460 check whether it can be used. */
11461 if (!NILP (w->optional_new_start)
11462 && CHARPOS (startp) >= BEGV
11463 && CHARPOS (startp) <= ZV)
11465 w->optional_new_start = Qnil;
11466 start_display (&it, w, startp);
11467 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11468 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11469 if (IT_CHARPOS (it) == PT)
11470 w->force_start = Qt;
11473 /* Handle case where place to start displaying has been specified,
11474 unless the specified location is outside the accessible range. */
11475 if (!NILP (w->force_start)
11476 || w->frozen_window_start_p)
11478 /* We set this later on if we have to adjust point. */
11479 int new_vpos = -1;
11481 w->force_start = Qnil;
11482 w->vscroll = 0;
11483 w->window_end_valid = Qnil;
11485 /* Forget any recorded base line for line number display. */
11486 if (!buffer_unchanged_p)
11487 w->base_line_number = Qnil;
11489 /* Redisplay the mode line. Select the buffer properly for that.
11490 Also, run the hook window-scroll-functions
11491 because we have scrolled. */
11492 /* Note, we do this after clearing force_start because
11493 if there's an error, it is better to forget about force_start
11494 than to get into an infinite loop calling the hook functions
11495 and having them get more errors. */
11496 if (!update_mode_line
11497 || ! NILP (Vwindow_scroll_functions))
11499 update_mode_line = 1;
11500 w->update_mode_line = Qt;
11501 startp = run_window_scroll_functions (window, startp);
11504 w->last_modified = make_number (0);
11505 w->last_overlay_modified = make_number (0);
11506 if (CHARPOS (startp) < BEGV)
11507 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11508 else if (CHARPOS (startp) > ZV)
11509 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11511 /* Redisplay, then check if cursor has been set during the
11512 redisplay. Give up if new fonts were loaded. */
11513 if (!try_window (window, startp))
11515 w->force_start = Qt;
11516 clear_glyph_matrix (w->desired_matrix);
11517 goto need_larger_matrices;
11520 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11522 /* If point does not appear, try to move point so it does
11523 appear. The desired matrix has been built above, so we
11524 can use it here. */
11525 new_vpos = window_box_height (w) / 2;
11528 if (!make_cursor_line_fully_visible (w))
11530 /* Point does appear, but on a line partly visible at end of window.
11531 Move it back to a fully-visible line. */
11532 new_vpos = window_box_height (w);
11535 /* If we need to move point for either of the above reasons,
11536 now actually do it. */
11537 if (new_vpos >= 0)
11539 struct glyph_row *row;
11541 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11542 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11543 ++row;
11545 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11546 MATRIX_ROW_START_BYTEPOS (row));
11548 if (w != XWINDOW (selected_window))
11549 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11550 else if (current_buffer == old)
11551 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11553 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11555 /* If we are highlighting the region, then we just changed
11556 the region, so redisplay to show it. */
11557 if (!NILP (Vtransient_mark_mode)
11558 && !NILP (current_buffer->mark_active))
11560 clear_glyph_matrix (w->desired_matrix);
11561 if (!try_window (window, startp))
11562 goto need_larger_matrices;
11566 #if GLYPH_DEBUG
11567 debug_method_add (w, "forced window start");
11568 #endif
11569 goto done;
11572 /* Handle case where text has not changed, only point, and it has
11573 not moved off the frame, and we are not retrying after hscroll.
11574 (current_matrix_up_to_date_p is nonzero when retrying.) */
11575 if (current_matrix_up_to_date_p
11576 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11577 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11579 switch (rc)
11581 case CURSOR_MOVEMENT_SUCCESS:
11582 goto done;
11584 #if 0 /* try_cursor_movement never returns this value. */
11585 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11586 goto need_larger_matrices;
11587 #endif
11589 case CURSOR_MOVEMENT_MUST_SCROLL:
11590 goto try_to_scroll;
11592 default:
11593 abort ();
11596 /* If current starting point was originally the beginning of a line
11597 but no longer is, find a new starting point. */
11598 else if (!NILP (w->start_at_line_beg)
11599 && !(CHARPOS (startp) <= BEGV
11600 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11602 #if GLYPH_DEBUG
11603 debug_method_add (w, "recenter 1");
11604 #endif
11605 goto recenter;
11608 /* Try scrolling with try_window_id. Value is > 0 if update has
11609 been done, it is -1 if we know that the same window start will
11610 not work. It is 0 if unsuccessful for some other reason. */
11611 else if ((tem = try_window_id (w)) != 0)
11613 #if GLYPH_DEBUG
11614 debug_method_add (w, "try_window_id %d", tem);
11615 #endif
11617 if (fonts_changed_p)
11618 goto need_larger_matrices;
11619 if (tem > 0)
11620 goto done;
11622 /* Otherwise try_window_id has returned -1 which means that we
11623 don't want the alternative below this comment to execute. */
11625 else if (CHARPOS (startp) >= BEGV
11626 && CHARPOS (startp) <= ZV
11627 && PT >= CHARPOS (startp)
11628 && (CHARPOS (startp) < ZV
11629 /* Avoid starting at end of buffer. */
11630 || CHARPOS (startp) == BEGV
11631 || (XFASTINT (w->last_modified) >= MODIFF
11632 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11634 #if GLYPH_DEBUG
11635 debug_method_add (w, "same window start");
11636 #endif
11638 /* Try to redisplay starting at same place as before.
11639 If point has not moved off frame, accept the results. */
11640 if (!current_matrix_up_to_date_p
11641 /* Don't use try_window_reusing_current_matrix in this case
11642 because a window scroll function can have changed the
11643 buffer. */
11644 || !NILP (Vwindow_scroll_functions)
11645 || MINI_WINDOW_P (w)
11646 || !try_window_reusing_current_matrix (w))
11648 IF_DEBUG (debug_method_add (w, "1"));
11649 try_window (window, startp);
11652 if (fonts_changed_p)
11653 goto need_larger_matrices;
11655 if (w->cursor.vpos >= 0)
11657 if (!just_this_one_p
11658 || current_buffer->clip_changed
11659 || BEG_UNCHANGED < CHARPOS (startp))
11660 /* Forget any recorded base line for line number display. */
11661 w->base_line_number = Qnil;
11663 if (!make_cursor_line_fully_visible (w))
11665 clear_glyph_matrix (w->desired_matrix);
11666 last_line_misfit = 1;
11668 /* Drop through and scroll. */
11669 else
11670 goto done;
11672 else
11673 clear_glyph_matrix (w->desired_matrix);
11676 try_to_scroll:
11678 w->last_modified = make_number (0);
11679 w->last_overlay_modified = make_number (0);
11681 /* Redisplay the mode line. Select the buffer properly for that. */
11682 if (!update_mode_line)
11684 update_mode_line = 1;
11685 w->update_mode_line = Qt;
11688 /* Try to scroll by specified few lines. */
11689 if ((scroll_conservatively
11690 || scroll_step
11691 || temp_scroll_step
11692 || NUMBERP (current_buffer->scroll_up_aggressively)
11693 || NUMBERP (current_buffer->scroll_down_aggressively))
11694 && !current_buffer->clip_changed
11695 && CHARPOS (startp) >= BEGV
11696 && CHARPOS (startp) <= ZV)
11698 /* The function returns -1 if new fonts were loaded, 1 if
11699 successful, 0 if not successful. */
11700 int rc = try_scrolling (window, just_this_one_p,
11701 scroll_conservatively,
11702 scroll_step,
11703 temp_scroll_step, last_line_misfit);
11704 switch (rc)
11706 case SCROLLING_SUCCESS:
11707 goto done;
11709 case SCROLLING_NEED_LARGER_MATRICES:
11710 goto need_larger_matrices;
11712 case SCROLLING_FAILED:
11713 break;
11715 default:
11716 abort ();
11720 /* Finally, just choose place to start which centers point */
11722 recenter:
11723 centering_position = window_box_height (w) / 2;
11725 point_at_top:
11726 /* Jump here with centering_position already set to 0. */
11728 #if GLYPH_DEBUG
11729 debug_method_add (w, "recenter");
11730 #endif
11732 /* w->vscroll = 0; */
11734 /* Forget any previously recorded base line for line number display. */
11735 if (!buffer_unchanged_p)
11736 w->base_line_number = Qnil;
11738 /* Move backward half the height of the window. */
11739 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11740 it.current_y = it.last_visible_y;
11741 move_it_vertically_backward (&it, centering_position);
11742 xassert (IT_CHARPOS (it) >= BEGV);
11744 /* The function move_it_vertically_backward may move over more
11745 than the specified y-distance. If it->w is small, e.g. a
11746 mini-buffer window, we may end up in front of the window's
11747 display area. Start displaying at the start of the line
11748 containing PT in this case. */
11749 if (it.current_y <= 0)
11751 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11752 move_it_vertically (&it, 0);
11753 xassert (IT_CHARPOS (it) <= PT);
11754 it.current_y = 0;
11757 it.current_x = it.hpos = 0;
11759 /* Set startp here explicitly in case that helps avoid an infinite loop
11760 in case the window-scroll-functions functions get errors. */
11761 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11763 /* Run scroll hooks. */
11764 startp = run_window_scroll_functions (window, it.current.pos);
11766 /* Redisplay the window. */
11767 if (!current_matrix_up_to_date_p
11768 || windows_or_buffers_changed
11769 || cursor_type_changed
11770 /* Don't use try_window_reusing_current_matrix in this case
11771 because it can have changed the buffer. */
11772 || !NILP (Vwindow_scroll_functions)
11773 || !just_this_one_p
11774 || MINI_WINDOW_P (w)
11775 || !try_window_reusing_current_matrix (w))
11776 try_window (window, startp);
11778 /* If new fonts have been loaded (due to fontsets), give up. We
11779 have to start a new redisplay since we need to re-adjust glyph
11780 matrices. */
11781 if (fonts_changed_p)
11782 goto need_larger_matrices;
11784 /* If cursor did not appear assume that the middle of the window is
11785 in the first line of the window. Do it again with the next line.
11786 (Imagine a window of height 100, displaying two lines of height
11787 60. Moving back 50 from it->last_visible_y will end in the first
11788 line.) */
11789 if (w->cursor.vpos < 0)
11791 if (!NILP (w->window_end_valid)
11792 && PT >= Z - XFASTINT (w->window_end_pos))
11794 clear_glyph_matrix (w->desired_matrix);
11795 move_it_by_lines (&it, 1, 0);
11796 try_window (window, it.current.pos);
11798 else if (PT < IT_CHARPOS (it))
11800 clear_glyph_matrix (w->desired_matrix);
11801 move_it_by_lines (&it, -1, 0);
11802 try_window (window, it.current.pos);
11804 else
11806 /* Not much we can do about it. */
11810 /* Consider the following case: Window starts at BEGV, there is
11811 invisible, intangible text at BEGV, so that display starts at
11812 some point START > BEGV. It can happen that we are called with
11813 PT somewhere between BEGV and START. Try to handle that case. */
11814 if (w->cursor.vpos < 0)
11816 struct glyph_row *row = w->current_matrix->rows;
11817 if (row->mode_line_p)
11818 ++row;
11819 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11822 if (!make_cursor_line_fully_visible (w))
11824 /* If vscroll is enabled, disable it and try again. */
11825 if (w->vscroll)
11827 w->vscroll = 0;
11828 clear_glyph_matrix (w->desired_matrix);
11829 goto recenter;
11832 /* If centering point failed to make the whole line visible,
11833 put point at the top instead. That has to make the whole line
11834 visible, if it can be done. */
11835 centering_position = 0;
11836 goto point_at_top;
11839 done:
11841 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11842 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11843 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11844 ? Qt : Qnil);
11846 /* Display the mode line, if we must. */
11847 if ((update_mode_line
11848 /* If window not full width, must redo its mode line
11849 if (a) the window to its side is being redone and
11850 (b) we do a frame-based redisplay. This is a consequence
11851 of how inverted lines are drawn in frame-based redisplay. */
11852 || (!just_this_one_p
11853 && !FRAME_WINDOW_P (f)
11854 && !WINDOW_FULL_WIDTH_P (w))
11855 /* Line number to display. */
11856 || INTEGERP (w->base_line_pos)
11857 /* Column number is displayed and different from the one displayed. */
11858 || (!NILP (w->column_number_displayed)
11859 && (XFASTINT (w->column_number_displayed)
11860 != (int) current_column ()))) /* iftc */
11861 /* This means that the window has a mode line. */
11862 && (WINDOW_WANTS_MODELINE_P (w)
11863 || WINDOW_WANTS_HEADER_LINE_P (w)))
11865 display_mode_lines (w);
11867 /* If mode line height has changed, arrange for a thorough
11868 immediate redisplay using the correct mode line height. */
11869 if (WINDOW_WANTS_MODELINE_P (w)
11870 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11872 fonts_changed_p = 1;
11873 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11874 = DESIRED_MODE_LINE_HEIGHT (w);
11877 /* If top line height has changed, arrange for a thorough
11878 immediate redisplay using the correct mode line height. */
11879 if (WINDOW_WANTS_HEADER_LINE_P (w)
11880 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11882 fonts_changed_p = 1;
11883 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11884 = DESIRED_HEADER_LINE_HEIGHT (w);
11887 if (fonts_changed_p)
11888 goto need_larger_matrices;
11891 if (!line_number_displayed
11892 && !BUFFERP (w->base_line_pos))
11894 w->base_line_pos = Qnil;
11895 w->base_line_number = Qnil;
11898 finish_menu_bars:
11900 /* When we reach a frame's selected window, redo the frame's menu bar. */
11901 if (update_mode_line
11902 && EQ (FRAME_SELECTED_WINDOW (f), window))
11904 int redisplay_menu_p = 0;
11905 int redisplay_tool_bar_p = 0;
11907 if (FRAME_WINDOW_P (f))
11909 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11910 || defined (USE_GTK)
11911 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11912 #else
11913 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11914 #endif
11916 else
11917 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11919 if (redisplay_menu_p)
11920 display_menu_bar (w);
11922 #ifdef HAVE_WINDOW_SYSTEM
11923 #ifdef USE_GTK
11924 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11925 #else
11926 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11927 && (FRAME_TOOL_BAR_LINES (f) > 0
11928 || auto_resize_tool_bars_p);
11930 #endif
11932 if (redisplay_tool_bar_p)
11933 redisplay_tool_bar (f);
11934 #endif
11937 /* We go to this label, with fonts_changed_p nonzero,
11938 if it is necessary to try again using larger glyph matrices.
11939 We have to redeem the scroll bar even in this case,
11940 because the loop in redisplay_internal expects that. */
11941 need_larger_matrices:
11943 finish_scroll_bars:
11945 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11947 /* Set the thumb's position and size. */
11948 set_vertical_scroll_bar (w);
11950 /* Note that we actually used the scroll bar attached to this
11951 window, so it shouldn't be deleted at the end of redisplay. */
11952 redeem_scroll_bar_hook (w);
11955 /* Restore current_buffer and value of point in it. */
11956 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
11957 set_buffer_internal_1 (old);
11958 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
11960 unbind_to (count, Qnil);
11964 /* Build the complete desired matrix of WINDOW with a window start
11965 buffer position POS. Value is non-zero if successful. It is zero
11966 if fonts were loaded during redisplay which makes re-adjusting
11967 glyph matrices necessary. */
11970 try_window (window, pos)
11971 Lisp_Object window;
11972 struct text_pos pos;
11974 struct window *w = XWINDOW (window);
11975 struct it it;
11976 struct glyph_row *last_text_row = NULL;
11978 /* Make POS the new window start. */
11979 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
11981 /* Mark cursor position as unknown. No overlay arrow seen. */
11982 w->cursor.vpos = -1;
11983 overlay_arrow_seen = 0;
11985 /* Initialize iterator and info to start at POS. */
11986 start_display (&it, w, pos);
11988 /* Display all lines of W. */
11989 while (it.current_y < it.last_visible_y)
11991 if (display_line (&it))
11992 last_text_row = it.glyph_row - 1;
11993 if (fonts_changed_p)
11994 return 0;
11997 /* If bottom moved off end of frame, change mode line percentage. */
11998 if (XFASTINT (w->window_end_pos) <= 0
11999 && Z != IT_CHARPOS (it))
12000 w->update_mode_line = Qt;
12002 /* Set window_end_pos to the offset of the last character displayed
12003 on the window from the end of current_buffer. Set
12004 window_end_vpos to its row number. */
12005 if (last_text_row)
12007 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12008 w->window_end_bytepos
12009 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12010 w->window_end_pos
12011 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12012 w->window_end_vpos
12013 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12014 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12015 ->displays_text_p);
12017 else
12019 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12020 w->window_end_pos = make_number (Z - ZV);
12021 w->window_end_vpos = make_number (0);
12024 /* But that is not valid info until redisplay finishes. */
12025 w->window_end_valid = Qnil;
12026 return 1;
12031 /************************************************************************
12032 Window redisplay reusing current matrix when buffer has not changed
12033 ************************************************************************/
12035 /* Try redisplay of window W showing an unchanged buffer with a
12036 different window start than the last time it was displayed by
12037 reusing its current matrix. Value is non-zero if successful.
12038 W->start is the new window start. */
12040 static int
12041 try_window_reusing_current_matrix (w)
12042 struct window *w;
12044 struct frame *f = XFRAME (w->frame);
12045 struct glyph_row *row, *bottom_row;
12046 struct it it;
12047 struct run run;
12048 struct text_pos start, new_start;
12049 int nrows_scrolled, i;
12050 struct glyph_row *last_text_row;
12051 struct glyph_row *last_reused_text_row;
12052 struct glyph_row *start_row;
12053 int start_vpos, min_y, max_y;
12055 #if GLYPH_DEBUG
12056 if (inhibit_try_window_reusing)
12057 return 0;
12058 #endif
12060 if (/* This function doesn't handle terminal frames. */
12061 !FRAME_WINDOW_P (f)
12062 /* Don't try to reuse the display if windows have been split
12063 or such. */
12064 || windows_or_buffers_changed
12065 || cursor_type_changed)
12066 return 0;
12068 /* Can't do this if region may have changed. */
12069 if ((!NILP (Vtransient_mark_mode)
12070 && !NILP (current_buffer->mark_active))
12071 || !NILP (w->region_showing)
12072 || !NILP (Vshow_trailing_whitespace))
12073 return 0;
12075 /* If top-line visibility has changed, give up. */
12076 if (WINDOW_WANTS_HEADER_LINE_P (w)
12077 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12078 return 0;
12080 /* Give up if old or new display is scrolled vertically. We could
12081 make this function handle this, but right now it doesn't. */
12082 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12083 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12084 return 0;
12086 /* The variable new_start now holds the new window start. The old
12087 start `start' can be determined from the current matrix. */
12088 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12089 start = start_row->start.pos;
12090 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12092 /* Clear the desired matrix for the display below. */
12093 clear_glyph_matrix (w->desired_matrix);
12095 if (CHARPOS (new_start) <= CHARPOS (start))
12097 int first_row_y;
12099 /* Don't use this method if the display starts with an ellipsis
12100 displayed for invisible text. It's not easy to handle that case
12101 below, and it's certainly not worth the effort since this is
12102 not a frequent case. */
12103 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12104 return 0;
12106 IF_DEBUG (debug_method_add (w, "twu1"));
12108 /* Display up to a row that can be reused. The variable
12109 last_text_row is set to the last row displayed that displays
12110 text. Note that it.vpos == 0 if or if not there is a
12111 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12112 start_display (&it, w, new_start);
12113 first_row_y = it.current_y;
12114 w->cursor.vpos = -1;
12115 last_text_row = last_reused_text_row = NULL;
12117 while (it.current_y < it.last_visible_y
12118 && IT_CHARPOS (it) < CHARPOS (start)
12119 && !fonts_changed_p)
12120 if (display_line (&it))
12121 last_text_row = it.glyph_row - 1;
12123 /* A value of current_y < last_visible_y means that we stopped
12124 at the previous window start, which in turn means that we
12125 have at least one reusable row. */
12126 if (it.current_y < it.last_visible_y)
12128 /* IT.vpos always starts from 0; it counts text lines. */
12129 nrows_scrolled = it.vpos;
12131 /* Find PT if not already found in the lines displayed. */
12132 if (w->cursor.vpos < 0)
12134 int dy = it.current_y - first_row_y;
12136 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12137 row = row_containing_pos (w, PT, row, NULL, dy);
12138 if (row)
12139 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12140 dy, nrows_scrolled);
12141 else
12143 clear_glyph_matrix (w->desired_matrix);
12144 return 0;
12148 /* Scroll the display. Do it before the current matrix is
12149 changed. The problem here is that update has not yet
12150 run, i.e. part of the current matrix is not up to date.
12151 scroll_run_hook will clear the cursor, and use the
12152 current matrix to get the height of the row the cursor is
12153 in. */
12154 run.current_y = first_row_y;
12155 run.desired_y = it.current_y;
12156 run.height = it.last_visible_y - it.current_y;
12158 if (run.height > 0 && run.current_y != run.desired_y)
12160 update_begin (f);
12161 rif->update_window_begin_hook (w);
12162 rif->clear_window_mouse_face (w);
12163 rif->scroll_run_hook (w, &run);
12164 rif->update_window_end_hook (w, 0, 0);
12165 update_end (f);
12168 /* Shift current matrix down by nrows_scrolled lines. */
12169 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12170 rotate_matrix (w->current_matrix,
12171 start_vpos,
12172 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12173 nrows_scrolled);
12175 /* Disable lines that must be updated. */
12176 for (i = 0; i < it.vpos; ++i)
12177 (start_row + i)->enabled_p = 0;
12179 /* Re-compute Y positions. */
12180 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12181 max_y = it.last_visible_y;
12182 for (row = start_row + nrows_scrolled;
12183 row < bottom_row;
12184 ++row)
12186 row->y = it.current_y;
12187 row->visible_height = row->height;
12189 if (row->y < min_y)
12190 row->visible_height -= min_y - row->y;
12191 if (row->y + row->height > max_y)
12192 row->visible_height -= row->y + row->height - max_y;
12194 it.current_y += row->height;
12196 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12197 last_reused_text_row = row;
12198 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12199 break;
12202 /* Disable lines in the current matrix which are now
12203 below the window. */
12204 for (++row; row < bottom_row; ++row)
12205 row->enabled_p = 0;
12208 /* Update window_end_pos etc.; last_reused_text_row is the last
12209 reused row from the current matrix containing text, if any.
12210 The value of last_text_row is the last displayed line
12211 containing text. */
12212 if (last_reused_text_row)
12214 w->window_end_bytepos
12215 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12216 w->window_end_pos
12217 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12218 w->window_end_vpos
12219 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12220 w->current_matrix));
12222 else if (last_text_row)
12224 w->window_end_bytepos
12225 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12226 w->window_end_pos
12227 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12228 w->window_end_vpos
12229 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12231 else
12233 /* This window must be completely empty. */
12234 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12235 w->window_end_pos = make_number (Z - ZV);
12236 w->window_end_vpos = make_number (0);
12238 w->window_end_valid = Qnil;
12240 /* Update hint: don't try scrolling again in update_window. */
12241 w->desired_matrix->no_scrolling_p = 1;
12243 #if GLYPH_DEBUG
12244 debug_method_add (w, "try_window_reusing_current_matrix 1");
12245 #endif
12246 return 1;
12248 else if (CHARPOS (new_start) > CHARPOS (start))
12250 struct glyph_row *pt_row, *row;
12251 struct glyph_row *first_reusable_row;
12252 struct glyph_row *first_row_to_display;
12253 int dy;
12254 int yb = window_text_bottom_y (w);
12256 /* Find the row starting at new_start, if there is one. Don't
12257 reuse a partially visible line at the end. */
12258 first_reusable_row = start_row;
12259 while (first_reusable_row->enabled_p
12260 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12261 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12262 < CHARPOS (new_start)))
12263 ++first_reusable_row;
12265 /* Give up if there is no row to reuse. */
12266 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12267 || !first_reusable_row->enabled_p
12268 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12269 != CHARPOS (new_start)))
12270 return 0;
12272 /* We can reuse fully visible rows beginning with
12273 first_reusable_row to the end of the window. Set
12274 first_row_to_display to the first row that cannot be reused.
12275 Set pt_row to the row containing point, if there is any. */
12276 pt_row = NULL;
12277 for (first_row_to_display = first_reusable_row;
12278 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12279 ++first_row_to_display)
12281 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12282 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12283 pt_row = first_row_to_display;
12286 /* Start displaying at the start of first_row_to_display. */
12287 xassert (first_row_to_display->y < yb);
12288 init_to_row_start (&it, w, first_row_to_display);
12290 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12291 - start_vpos);
12292 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12293 - nrows_scrolled);
12294 it.current_y = (first_row_to_display->y - first_reusable_row->y
12295 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
12297 /* Display lines beginning with first_row_to_display in the
12298 desired matrix. Set last_text_row to the last row displayed
12299 that displays text. */
12300 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12301 if (pt_row == NULL)
12302 w->cursor.vpos = -1;
12303 last_text_row = NULL;
12304 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12305 if (display_line (&it))
12306 last_text_row = it.glyph_row - 1;
12308 /* Give up If point isn't in a row displayed or reused. */
12309 if (w->cursor.vpos < 0)
12311 clear_glyph_matrix (w->desired_matrix);
12312 return 0;
12315 /* If point is in a reused row, adjust y and vpos of the cursor
12316 position. */
12317 if (pt_row)
12319 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12320 w->current_matrix);
12321 w->cursor.y -= first_reusable_row->y;
12324 /* Scroll the display. */
12325 run.current_y = first_reusable_row->y;
12326 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12327 run.height = it.last_visible_y - run.current_y;
12328 dy = run.current_y - run.desired_y;
12330 if (run.height)
12332 struct frame *f = XFRAME (WINDOW_FRAME (w));
12333 update_begin (f);
12334 rif->update_window_begin_hook (w);
12335 rif->clear_window_mouse_face (w);
12336 rif->scroll_run_hook (w, &run);
12337 rif->update_window_end_hook (w, 0, 0);
12338 update_end (f);
12341 /* Adjust Y positions of reused rows. */
12342 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12343 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12344 max_y = it.last_visible_y;
12345 for (row = first_reusable_row; row < first_row_to_display; ++row)
12347 row->y -= dy;
12348 row->visible_height = row->height;
12349 if (row->y < min_y)
12350 row->visible_height -= min_y - row->y;
12351 if (row->y + row->height > max_y)
12352 row->visible_height -= row->y + row->height - max_y;
12355 /* Scroll the current matrix. */
12356 xassert (nrows_scrolled > 0);
12357 rotate_matrix (w->current_matrix,
12358 start_vpos,
12359 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12360 -nrows_scrolled);
12362 /* Disable rows not reused. */
12363 for (row -= nrows_scrolled; row < bottom_row; ++row)
12364 row->enabled_p = 0;
12366 /* Adjust window end. A null value of last_text_row means that
12367 the window end is in reused rows which in turn means that
12368 only its vpos can have changed. */
12369 if (last_text_row)
12371 w->window_end_bytepos
12372 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12373 w->window_end_pos
12374 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12375 w->window_end_vpos
12376 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12378 else
12380 w->window_end_vpos
12381 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12384 w->window_end_valid = Qnil;
12385 w->desired_matrix->no_scrolling_p = 1;
12387 #if GLYPH_DEBUG
12388 debug_method_add (w, "try_window_reusing_current_matrix 2");
12389 #endif
12390 return 1;
12393 return 0;
12398 /************************************************************************
12399 Window redisplay reusing current matrix when buffer has changed
12400 ************************************************************************/
12402 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12403 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12404 int *, int *));
12405 static struct glyph_row *
12406 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12407 struct glyph_row *));
12410 /* Return the last row in MATRIX displaying text. If row START is
12411 non-null, start searching with that row. IT gives the dimensions
12412 of the display. Value is null if matrix is empty; otherwise it is
12413 a pointer to the row found. */
12415 static struct glyph_row *
12416 find_last_row_displaying_text (matrix, it, start)
12417 struct glyph_matrix *matrix;
12418 struct it *it;
12419 struct glyph_row *start;
12421 struct glyph_row *row, *row_found;
12423 /* Set row_found to the last row in IT->w's current matrix
12424 displaying text. The loop looks funny but think of partially
12425 visible lines. */
12426 row_found = NULL;
12427 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12428 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12430 xassert (row->enabled_p);
12431 row_found = row;
12432 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12433 break;
12434 ++row;
12437 return row_found;
12441 /* Return the last row in the current matrix of W that is not affected
12442 by changes at the start of current_buffer that occurred since W's
12443 current matrix was built. Value is null if no such row exists.
12445 BEG_UNCHANGED us the number of characters unchanged at the start of
12446 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12447 first changed character in current_buffer. Characters at positions <
12448 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12449 when the current matrix was built. */
12451 static struct glyph_row *
12452 find_last_unchanged_at_beg_row (w)
12453 struct window *w;
12455 int first_changed_pos = BEG + BEG_UNCHANGED;
12456 struct glyph_row *row;
12457 struct glyph_row *row_found = NULL;
12458 int yb = window_text_bottom_y (w);
12460 /* Find the last row displaying unchanged text. */
12461 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12462 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12463 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12465 if (/* If row ends before first_changed_pos, it is unchanged,
12466 except in some case. */
12467 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12468 /* When row ends in ZV and we write at ZV it is not
12469 unchanged. */
12470 && !row->ends_at_zv_p
12471 /* When first_changed_pos is the end of a continued line,
12472 row is not unchanged because it may be no longer
12473 continued. */
12474 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12475 && row->continued_p))
12476 row_found = row;
12478 /* Stop if last visible row. */
12479 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12480 break;
12482 ++row;
12485 return row_found;
12489 /* Find the first glyph row in the current matrix of W that is not
12490 affected by changes at the end of current_buffer since the
12491 time W's current matrix was built.
12493 Return in *DELTA the number of chars by which buffer positions in
12494 unchanged text at the end of current_buffer must be adjusted.
12496 Return in *DELTA_BYTES the corresponding number of bytes.
12498 Value is null if no such row exists, i.e. all rows are affected by
12499 changes. */
12501 static struct glyph_row *
12502 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12503 struct window *w;
12504 int *delta, *delta_bytes;
12506 struct glyph_row *row;
12507 struct glyph_row *row_found = NULL;
12509 *delta = *delta_bytes = 0;
12511 /* Display must not have been paused, otherwise the current matrix
12512 is not up to date. */
12513 if (NILP (w->window_end_valid))
12514 abort ();
12516 /* A value of window_end_pos >= END_UNCHANGED means that the window
12517 end is in the range of changed text. If so, there is no
12518 unchanged row at the end of W's current matrix. */
12519 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12520 return NULL;
12522 /* Set row to the last row in W's current matrix displaying text. */
12523 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12525 /* If matrix is entirely empty, no unchanged row exists. */
12526 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12528 /* The value of row is the last glyph row in the matrix having a
12529 meaningful buffer position in it. The end position of row
12530 corresponds to window_end_pos. This allows us to translate
12531 buffer positions in the current matrix to current buffer
12532 positions for characters not in changed text. */
12533 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12534 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12535 int last_unchanged_pos, last_unchanged_pos_old;
12536 struct glyph_row *first_text_row
12537 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12539 *delta = Z - Z_old;
12540 *delta_bytes = Z_BYTE - Z_BYTE_old;
12542 /* Set last_unchanged_pos to the buffer position of the last
12543 character in the buffer that has not been changed. Z is the
12544 index + 1 of the last character in current_buffer, i.e. by
12545 subtracting END_UNCHANGED we get the index of the last
12546 unchanged character, and we have to add BEG to get its buffer
12547 position. */
12548 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12549 last_unchanged_pos_old = last_unchanged_pos - *delta;
12551 /* Search backward from ROW for a row displaying a line that
12552 starts at a minimum position >= last_unchanged_pos_old. */
12553 for (; row > first_text_row; --row)
12555 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12556 abort ();
12558 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12559 row_found = row;
12563 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12564 abort ();
12566 return row_found;
12570 /* Make sure that glyph rows in the current matrix of window W
12571 reference the same glyph memory as corresponding rows in the
12572 frame's frame matrix. This function is called after scrolling W's
12573 current matrix on a terminal frame in try_window_id and
12574 try_window_reusing_current_matrix. */
12576 static void
12577 sync_frame_with_window_matrix_rows (w)
12578 struct window *w;
12580 struct frame *f = XFRAME (w->frame);
12581 struct glyph_row *window_row, *window_row_end, *frame_row;
12583 /* Preconditions: W must be a leaf window and full-width. Its frame
12584 must have a frame matrix. */
12585 xassert (NILP (w->hchild) && NILP (w->vchild));
12586 xassert (WINDOW_FULL_WIDTH_P (w));
12587 xassert (!FRAME_WINDOW_P (f));
12589 /* If W is a full-width window, glyph pointers in W's current matrix
12590 have, by definition, to be the same as glyph pointers in the
12591 corresponding frame matrix. Note that frame matrices have no
12592 marginal areas (see build_frame_matrix). */
12593 window_row = w->current_matrix->rows;
12594 window_row_end = window_row + w->current_matrix->nrows;
12595 frame_row = f->current_matrix->rows + XFASTINT (w->top);
12596 while (window_row < window_row_end)
12598 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12599 struct glyph *end = window_row->glyphs[LAST_AREA];
12601 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12602 frame_row->glyphs[TEXT_AREA] = start;
12603 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12604 frame_row->glyphs[LAST_AREA] = end;
12606 /* Disable frame rows whose corresponding window rows have
12607 been disabled in try_window_id. */
12608 if (!window_row->enabled_p)
12609 frame_row->enabled_p = 0;
12611 ++window_row, ++frame_row;
12616 /* Find the glyph row in window W containing CHARPOS. Consider all
12617 rows between START and END (not inclusive). END null means search
12618 all rows to the end of the display area of W. Value is the row
12619 containing CHARPOS or null. */
12621 struct glyph_row *
12622 row_containing_pos (w, charpos, start, end, dy)
12623 struct window *w;
12624 int charpos;
12625 struct glyph_row *start, *end;
12626 int dy;
12628 struct glyph_row *row = start;
12629 int last_y;
12631 /* If we happen to start on a header-line, skip that. */
12632 if (row->mode_line_p)
12633 ++row;
12635 if ((end && row >= end) || !row->enabled_p)
12636 return NULL;
12638 last_y = window_text_bottom_y (w) - dy;
12640 while (1)
12642 /* Give up if we have gone too far. */
12643 if (end && row >= end)
12644 return NULL;
12645 /* This formerly returned if they were equal.
12646 I think that both quantities are of a "last plus one" type;
12647 if so, when they are equal, the row is within the screen. -- rms. */
12648 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12649 return NULL;
12651 /* If it is in this row, return this row. */
12652 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12653 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12654 /* The end position of a row equals the start
12655 position of the next row. If CHARPOS is there, we
12656 would rather display it in the next line, except
12657 when this line ends in ZV. */
12658 && !row->ends_at_zv_p
12659 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12660 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12661 return row;
12662 ++row;
12667 /* Try to redisplay window W by reusing its existing display. W's
12668 current matrix must be up to date when this function is called,
12669 i.e. window_end_valid must not be nil.
12671 Value is
12673 1 if display has been updated
12674 0 if otherwise unsuccessful
12675 -1 if redisplay with same window start is known not to succeed
12677 The following steps are performed:
12679 1. Find the last row in the current matrix of W that is not
12680 affected by changes at the start of current_buffer. If no such row
12681 is found, give up.
12683 2. Find the first row in W's current matrix that is not affected by
12684 changes at the end of current_buffer. Maybe there is no such row.
12686 3. Display lines beginning with the row + 1 found in step 1 to the
12687 row found in step 2 or, if step 2 didn't find a row, to the end of
12688 the window.
12690 4. If cursor is not known to appear on the window, give up.
12692 5. If display stopped at the row found in step 2, scroll the
12693 display and current matrix as needed.
12695 6. Maybe display some lines at the end of W, if we must. This can
12696 happen under various circumstances, like a partially visible line
12697 becoming fully visible, or because newly displayed lines are displayed
12698 in smaller font sizes.
12700 7. Update W's window end information. */
12702 static int
12703 try_window_id (w)
12704 struct window *w;
12706 struct frame *f = XFRAME (w->frame);
12707 struct glyph_matrix *current_matrix = w->current_matrix;
12708 struct glyph_matrix *desired_matrix = w->desired_matrix;
12709 struct glyph_row *last_unchanged_at_beg_row;
12710 struct glyph_row *first_unchanged_at_end_row;
12711 struct glyph_row *row;
12712 struct glyph_row *bottom_row;
12713 int bottom_vpos;
12714 struct it it;
12715 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12716 struct text_pos start_pos;
12717 struct run run;
12718 int first_unchanged_at_end_vpos = 0;
12719 struct glyph_row *last_text_row, *last_text_row_at_end;
12720 struct text_pos start;
12721 int first_changed_charpos, last_changed_charpos;
12723 #if GLYPH_DEBUG
12724 if (inhibit_try_window_id)
12725 return 0;
12726 #endif
12728 /* This is handy for debugging. */
12729 #if 0
12730 #define GIVE_UP(X) \
12731 do { \
12732 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12733 return 0; \
12734 } while (0)
12735 #else
12736 #define GIVE_UP(X) return 0
12737 #endif
12739 SET_TEXT_POS_FROM_MARKER (start, w->start);
12741 /* Don't use this for mini-windows because these can show
12742 messages and mini-buffers, and we don't handle that here. */
12743 if (MINI_WINDOW_P (w))
12744 GIVE_UP (1);
12746 /* This flag is used to prevent redisplay optimizations. */
12747 if (windows_or_buffers_changed || cursor_type_changed)
12748 GIVE_UP (2);
12750 /* Verify that narrowing has not changed.
12751 Also verify that we were not told to prevent redisplay optimizations.
12752 It would be nice to further
12753 reduce the number of cases where this prevents try_window_id. */
12754 if (current_buffer->clip_changed
12755 || current_buffer->prevent_redisplay_optimizations_p)
12756 GIVE_UP (3);
12758 /* Window must either use window-based redisplay or be full width. */
12759 if (!FRAME_WINDOW_P (f)
12760 && (!line_ins_del_ok
12761 || !WINDOW_FULL_WIDTH_P (w)))
12762 GIVE_UP (4);
12764 /* Give up if point is not known NOT to appear in W. */
12765 if (PT < CHARPOS (start))
12766 GIVE_UP (5);
12768 /* Another way to prevent redisplay optimizations. */
12769 if (XFASTINT (w->last_modified) == 0)
12770 GIVE_UP (6);
12772 /* Verify that window is not hscrolled. */
12773 if (XFASTINT (w->hscroll) != 0)
12774 GIVE_UP (7);
12776 /* Verify that display wasn't paused. */
12777 if (NILP (w->window_end_valid))
12778 GIVE_UP (8);
12780 /* Can't use this if highlighting a region because a cursor movement
12781 will do more than just set the cursor. */
12782 if (!NILP (Vtransient_mark_mode)
12783 && !NILP (current_buffer->mark_active))
12784 GIVE_UP (9);
12786 /* Likewise if highlighting trailing whitespace. */
12787 if (!NILP (Vshow_trailing_whitespace))
12788 GIVE_UP (11);
12790 /* Likewise if showing a region. */
12791 if (!NILP (w->region_showing))
12792 GIVE_UP (10);
12794 /* Can use this if overlay arrow position and or string have changed. */
12795 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12796 || !EQ (last_arrow_string, Voverlay_arrow_string))
12797 GIVE_UP (12);
12800 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12801 only if buffer has really changed. The reason is that the gap is
12802 initially at Z for freshly visited files. The code below would
12803 set end_unchanged to 0 in that case. */
12804 if (MODIFF > SAVE_MODIFF
12805 /* This seems to happen sometimes after saving a buffer. */
12806 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12808 if (GPT - BEG < BEG_UNCHANGED)
12809 BEG_UNCHANGED = GPT - BEG;
12810 if (Z - GPT < END_UNCHANGED)
12811 END_UNCHANGED = Z - GPT;
12814 /* The position of the first and last character that has been changed. */
12815 first_changed_charpos = BEG + BEG_UNCHANGED;
12816 last_changed_charpos = Z - END_UNCHANGED;
12818 /* If window starts after a line end, and the last change is in
12819 front of that newline, then changes don't affect the display.
12820 This case happens with stealth-fontification. Note that although
12821 the display is unchanged, glyph positions in the matrix have to
12822 be adjusted, of course. */
12823 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12824 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12825 && ((last_changed_charpos < CHARPOS (start)
12826 && CHARPOS (start) == BEGV)
12827 || (last_changed_charpos < CHARPOS (start) - 1
12828 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12830 int Z_old, delta, Z_BYTE_old, delta_bytes;
12831 struct glyph_row *r0;
12833 /* Compute how many chars/bytes have been added to or removed
12834 from the buffer. */
12835 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12836 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12837 delta = Z - Z_old;
12838 delta_bytes = Z_BYTE - Z_BYTE_old;
12840 /* Give up if PT is not in the window. Note that it already has
12841 been checked at the start of try_window_id that PT is not in
12842 front of the window start. */
12843 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12844 GIVE_UP (13);
12846 /* If window start is unchanged, we can reuse the whole matrix
12847 as is, after adjusting glyph positions. No need to compute
12848 the window end again, since its offset from Z hasn't changed. */
12849 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12850 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12851 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
12853 /* Adjust positions in the glyph matrix. */
12854 if (delta || delta_bytes)
12856 struct glyph_row *r1
12857 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12858 increment_matrix_positions (w->current_matrix,
12859 MATRIX_ROW_VPOS (r0, current_matrix),
12860 MATRIX_ROW_VPOS (r1, current_matrix),
12861 delta, delta_bytes);
12864 /* Set the cursor. */
12865 row = row_containing_pos (w, PT, r0, NULL, 0);
12866 if (row)
12867 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12868 else
12869 abort ();
12870 return 1;
12874 /* Handle the case that changes are all below what is displayed in
12875 the window, and that PT is in the window. This shortcut cannot
12876 be taken if ZV is visible in the window, and text has been added
12877 there that is visible in the window. */
12878 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12879 /* ZV is not visible in the window, or there are no
12880 changes at ZV, actually. */
12881 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12882 || first_changed_charpos == last_changed_charpos))
12884 struct glyph_row *r0;
12886 /* Give up if PT is not in the window. Note that it already has
12887 been checked at the start of try_window_id that PT is not in
12888 front of the window start. */
12889 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12890 GIVE_UP (14);
12892 /* If window start is unchanged, we can reuse the whole matrix
12893 as is, without changing glyph positions since no text has
12894 been added/removed in front of the window end. */
12895 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12896 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
12898 /* We have to compute the window end anew since text
12899 can have been added/removed after it. */
12900 w->window_end_pos
12901 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12902 w->window_end_bytepos
12903 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12905 /* Set the cursor. */
12906 row = row_containing_pos (w, PT, r0, NULL, 0);
12907 if (row)
12908 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12909 else
12910 abort ();
12911 return 2;
12915 /* Give up if window start is in the changed area.
12917 The condition used to read
12919 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12921 but why that was tested escapes me at the moment. */
12922 if (CHARPOS (start) >= first_changed_charpos
12923 && CHARPOS (start) <= last_changed_charpos)
12924 GIVE_UP (15);
12926 /* Check that window start agrees with the start of the first glyph
12927 row in its current matrix. Check this after we know the window
12928 start is not in changed text, otherwise positions would not be
12929 comparable. */
12930 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12931 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12932 GIVE_UP (16);
12934 /* Give up if the window ends in strings. Overlay strings
12935 at the end are difficult to handle, so don't try. */
12936 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12937 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12938 GIVE_UP (20);
12940 /* Compute the position at which we have to start displaying new
12941 lines. Some of the lines at the top of the window might be
12942 reusable because they are not displaying changed text. Find the
12943 last row in W's current matrix not affected by changes at the
12944 start of current_buffer. Value is null if changes start in the
12945 first line of window. */
12946 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12947 if (last_unchanged_at_beg_row)
12949 /* Avoid starting to display in the moddle of a character, a TAB
12950 for instance. This is easier than to set up the iterator
12951 exactly, and it's not a frequent case, so the additional
12952 effort wouldn't really pay off. */
12953 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12954 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
12955 && last_unchanged_at_beg_row > w->current_matrix->rows)
12956 --last_unchanged_at_beg_row;
12958 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
12959 GIVE_UP (17);
12961 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
12962 GIVE_UP (18);
12963 start_pos = it.current.pos;
12965 /* Start displaying new lines in the desired matrix at the same
12966 vpos we would use in the current matrix, i.e. below
12967 last_unchanged_at_beg_row. */
12968 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
12969 current_matrix);
12970 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12971 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
12973 xassert (it.hpos == 0 && it.current_x == 0);
12975 else
12977 /* There are no reusable lines at the start of the window.
12978 Start displaying in the first line. */
12979 start_display (&it, w, start);
12980 start_pos = it.current.pos;
12983 /* Find the first row that is not affected by changes at the end of
12984 the buffer. Value will be null if there is no unchanged row, in
12985 which case we must redisplay to the end of the window. delta
12986 will be set to the value by which buffer positions beginning with
12987 first_unchanged_at_end_row have to be adjusted due to text
12988 changes. */
12989 first_unchanged_at_end_row
12990 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
12991 IF_DEBUG (debug_delta = delta);
12992 IF_DEBUG (debug_delta_bytes = delta_bytes);
12994 /* Set stop_pos to the buffer position up to which we will have to
12995 display new lines. If first_unchanged_at_end_row != NULL, this
12996 is the buffer position of the start of the line displayed in that
12997 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12998 that we don't stop at a buffer position. */
12999 stop_pos = 0;
13000 if (first_unchanged_at_end_row)
13002 xassert (last_unchanged_at_beg_row == NULL
13003 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13005 /* If this is a continuation line, move forward to the next one
13006 that isn't. Changes in lines above affect this line.
13007 Caution: this may move first_unchanged_at_end_row to a row
13008 not displaying text. */
13009 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13010 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13011 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13012 < it.last_visible_y))
13013 ++first_unchanged_at_end_row;
13015 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13016 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13017 >= it.last_visible_y))
13018 first_unchanged_at_end_row = NULL;
13019 else
13021 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13022 + delta);
13023 first_unchanged_at_end_vpos
13024 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13025 xassert (stop_pos >= Z - END_UNCHANGED);
13028 else if (last_unchanged_at_beg_row == NULL)
13029 GIVE_UP (19);
13032 #if GLYPH_DEBUG
13034 /* Either there is no unchanged row at the end, or the one we have
13035 now displays text. This is a necessary condition for the window
13036 end pos calculation at the end of this function. */
13037 xassert (first_unchanged_at_end_row == NULL
13038 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13040 debug_last_unchanged_at_beg_vpos
13041 = (last_unchanged_at_beg_row
13042 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13043 : -1);
13044 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13046 #endif /* GLYPH_DEBUG != 0 */
13049 /* Display new lines. Set last_text_row to the last new line
13050 displayed which has text on it, i.e. might end up as being the
13051 line where the window_end_vpos is. */
13052 w->cursor.vpos = -1;
13053 last_text_row = NULL;
13054 overlay_arrow_seen = 0;
13055 while (it.current_y < it.last_visible_y
13056 && !fonts_changed_p
13057 && (first_unchanged_at_end_row == NULL
13058 || IT_CHARPOS (it) < stop_pos))
13060 if (display_line (&it))
13061 last_text_row = it.glyph_row - 1;
13064 if (fonts_changed_p)
13065 return -1;
13068 /* Compute differences in buffer positions, y-positions etc. for
13069 lines reused at the bottom of the window. Compute what we can
13070 scroll. */
13071 if (first_unchanged_at_end_row
13072 /* No lines reused because we displayed everything up to the
13073 bottom of the window. */
13074 && it.current_y < it.last_visible_y)
13076 dvpos = (it.vpos
13077 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13078 current_matrix));
13079 dy = it.current_y - first_unchanged_at_end_row->y;
13080 run.current_y = first_unchanged_at_end_row->y;
13081 run.desired_y = run.current_y + dy;
13082 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13084 else
13086 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13087 first_unchanged_at_end_row = NULL;
13089 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13092 /* Find the cursor if not already found. We have to decide whether
13093 PT will appear on this window (it sometimes doesn't, but this is
13094 not a very frequent case.) This decision has to be made before
13095 the current matrix is altered. A value of cursor.vpos < 0 means
13096 that PT is either in one of the lines beginning at
13097 first_unchanged_at_end_row or below the window. Don't care for
13098 lines that might be displayed later at the window end; as
13099 mentioned, this is not a frequent case. */
13100 if (w->cursor.vpos < 0)
13102 /* Cursor in unchanged rows at the top? */
13103 if (PT < CHARPOS (start_pos)
13104 && last_unchanged_at_beg_row)
13106 row = row_containing_pos (w, PT,
13107 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13108 last_unchanged_at_beg_row + 1, 0);
13109 if (row)
13110 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13113 /* Start from first_unchanged_at_end_row looking for PT. */
13114 else if (first_unchanged_at_end_row)
13116 row = row_containing_pos (w, PT - delta,
13117 first_unchanged_at_end_row, NULL, 0);
13118 if (row)
13119 set_cursor_from_row (w, row, w->current_matrix, delta,
13120 delta_bytes, dy, dvpos);
13123 /* Give up if cursor was not found. */
13124 if (w->cursor.vpos < 0)
13126 clear_glyph_matrix (w->desired_matrix);
13127 return -1;
13131 /* Don't let the cursor end in the scroll margins. */
13133 int this_scroll_margin, cursor_height;
13135 this_scroll_margin = max (0, scroll_margin);
13136 this_scroll_margin = min (this_scroll_margin,
13137 XFASTINT (w->height) / 4);
13138 this_scroll_margin *= CANON_Y_UNIT (it.f);
13139 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13141 if ((w->cursor.y < this_scroll_margin
13142 && CHARPOS (start) > BEGV)
13143 /* Don't take scroll margin into account at the bottom because
13144 old redisplay didn't do it either. */
13145 || w->cursor.y + cursor_height > it.last_visible_y)
13147 w->cursor.vpos = -1;
13148 clear_glyph_matrix (w->desired_matrix);
13149 return -1;
13153 /* Scroll the display. Do it before changing the current matrix so
13154 that xterm.c doesn't get confused about where the cursor glyph is
13155 found. */
13156 if (dy && run.height)
13158 update_begin (f);
13160 if (FRAME_WINDOW_P (f))
13162 rif->update_window_begin_hook (w);
13163 rif->clear_window_mouse_face (w);
13164 rif->scroll_run_hook (w, &run);
13165 rif->update_window_end_hook (w, 0, 0);
13167 else
13169 /* Terminal frame. In this case, dvpos gives the number of
13170 lines to scroll by; dvpos < 0 means scroll up. */
13171 int first_unchanged_at_end_vpos
13172 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13173 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
13174 int end = (XFASTINT (w->top)
13175 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13176 + window_internal_height (w));
13178 /* Perform the operation on the screen. */
13179 if (dvpos > 0)
13181 /* Scroll last_unchanged_at_beg_row to the end of the
13182 window down dvpos lines. */
13183 set_terminal_window (end);
13185 /* On dumb terminals delete dvpos lines at the end
13186 before inserting dvpos empty lines. */
13187 if (!scroll_region_ok)
13188 ins_del_lines (end - dvpos, -dvpos);
13190 /* Insert dvpos empty lines in front of
13191 last_unchanged_at_beg_row. */
13192 ins_del_lines (from, dvpos);
13194 else if (dvpos < 0)
13196 /* Scroll up last_unchanged_at_beg_vpos to the end of
13197 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13198 set_terminal_window (end);
13200 /* Delete dvpos lines in front of
13201 last_unchanged_at_beg_vpos. ins_del_lines will set
13202 the cursor to the given vpos and emit |dvpos| delete
13203 line sequences. */
13204 ins_del_lines (from + dvpos, dvpos);
13206 /* On a dumb terminal insert dvpos empty lines at the
13207 end. */
13208 if (!scroll_region_ok)
13209 ins_del_lines (end + dvpos, -dvpos);
13212 set_terminal_window (0);
13215 update_end (f);
13218 /* Shift reused rows of the current matrix to the right position.
13219 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13220 text. */
13221 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13222 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13223 if (dvpos < 0)
13225 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13226 bottom_vpos, dvpos);
13227 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13228 bottom_vpos, 0);
13230 else if (dvpos > 0)
13232 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13233 bottom_vpos, dvpos);
13234 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13235 first_unchanged_at_end_vpos + dvpos, 0);
13238 /* For frame-based redisplay, make sure that current frame and window
13239 matrix are in sync with respect to glyph memory. */
13240 if (!FRAME_WINDOW_P (f))
13241 sync_frame_with_window_matrix_rows (w);
13243 /* Adjust buffer positions in reused rows. */
13244 if (delta)
13245 increment_matrix_positions (current_matrix,
13246 first_unchanged_at_end_vpos + dvpos,
13247 bottom_vpos, delta, delta_bytes);
13249 /* Adjust Y positions. */
13250 if (dy)
13251 shift_glyph_matrix (w, current_matrix,
13252 first_unchanged_at_end_vpos + dvpos,
13253 bottom_vpos, dy);
13255 if (first_unchanged_at_end_row)
13256 first_unchanged_at_end_row += dvpos;
13258 /* If scrolling up, there may be some lines to display at the end of
13259 the window. */
13260 last_text_row_at_end = NULL;
13261 if (dy < 0)
13263 /* Scrolling up can leave for example a partially visible line
13264 at the end of the window to be redisplayed. */
13265 /* Set last_row to the glyph row in the current matrix where the
13266 window end line is found. It has been moved up or down in
13267 the matrix by dvpos. */
13268 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13269 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13271 /* If last_row is the window end line, it should display text. */
13272 xassert (last_row->displays_text_p);
13274 /* If window end line was partially visible before, begin
13275 displaying at that line. Otherwise begin displaying with the
13276 line following it. */
13277 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13279 init_to_row_start (&it, w, last_row);
13280 it.vpos = last_vpos;
13281 it.current_y = last_row->y;
13283 else
13285 init_to_row_end (&it, w, last_row);
13286 it.vpos = 1 + last_vpos;
13287 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13288 ++last_row;
13291 /* We may start in a continuation line. If so, we have to
13292 get the right continuation_lines_width and current_x. */
13293 it.continuation_lines_width = last_row->continuation_lines_width;
13294 it.hpos = it.current_x = 0;
13296 /* Display the rest of the lines at the window end. */
13297 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13298 while (it.current_y < it.last_visible_y
13299 && !fonts_changed_p)
13301 /* Is it always sure that the display agrees with lines in
13302 the current matrix? I don't think so, so we mark rows
13303 displayed invalid in the current matrix by setting their
13304 enabled_p flag to zero. */
13305 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13306 if (display_line (&it))
13307 last_text_row_at_end = it.glyph_row - 1;
13311 /* Update window_end_pos and window_end_vpos. */
13312 if (first_unchanged_at_end_row
13313 && first_unchanged_at_end_row->y < it.last_visible_y
13314 && !last_text_row_at_end)
13316 /* Window end line if one of the preserved rows from the current
13317 matrix. Set row to the last row displaying text in current
13318 matrix starting at first_unchanged_at_end_row, after
13319 scrolling. */
13320 xassert (first_unchanged_at_end_row->displays_text_p);
13321 row = find_last_row_displaying_text (w->current_matrix, &it,
13322 first_unchanged_at_end_row);
13323 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13325 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13326 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13327 w->window_end_vpos
13328 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13329 xassert (w->window_end_bytepos >= 0);
13330 IF_DEBUG (debug_method_add (w, "A"));
13332 else if (last_text_row_at_end)
13334 w->window_end_pos
13335 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13336 w->window_end_bytepos
13337 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13338 w->window_end_vpos
13339 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13340 xassert (w->window_end_bytepos >= 0);
13341 IF_DEBUG (debug_method_add (w, "B"));
13343 else if (last_text_row)
13345 /* We have displayed either to the end of the window or at the
13346 end of the window, i.e. the last row with text is to be found
13347 in the desired matrix. */
13348 w->window_end_pos
13349 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13350 w->window_end_bytepos
13351 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13352 w->window_end_vpos
13353 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13354 xassert (w->window_end_bytepos >= 0);
13356 else if (first_unchanged_at_end_row == NULL
13357 && last_text_row == NULL
13358 && last_text_row_at_end == NULL)
13360 /* Displayed to end of window, but no line containing text was
13361 displayed. Lines were deleted at the end of the window. */
13362 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13363 int vpos = XFASTINT (w->window_end_vpos);
13364 struct glyph_row *current_row = current_matrix->rows + vpos;
13365 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13367 for (row = NULL;
13368 row == NULL && vpos >= first_vpos;
13369 --vpos, --current_row, --desired_row)
13371 if (desired_row->enabled_p)
13373 if (desired_row->displays_text_p)
13374 row = desired_row;
13376 else if (current_row->displays_text_p)
13377 row = current_row;
13380 xassert (row != NULL);
13381 w->window_end_vpos = make_number (vpos + 1);
13382 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13383 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13384 xassert (w->window_end_bytepos >= 0);
13385 IF_DEBUG (debug_method_add (w, "C"));
13387 else
13388 abort ();
13390 #if 0 /* This leads to problems, for instance when the cursor is
13391 at ZV, and the cursor line displays no text. */
13392 /* Disable rows below what's displayed in the window. This makes
13393 debugging easier. */
13394 enable_glyph_matrix_rows (current_matrix,
13395 XFASTINT (w->window_end_vpos) + 1,
13396 bottom_vpos, 0);
13397 #endif
13399 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13400 debug_end_vpos = XFASTINT (w->window_end_vpos));
13402 /* Record that display has not been completed. */
13403 w->window_end_valid = Qnil;
13404 w->desired_matrix->no_scrolling_p = 1;
13405 return 3;
13407 #undef GIVE_UP
13412 /***********************************************************************
13413 More debugging support
13414 ***********************************************************************/
13416 #if GLYPH_DEBUG
13418 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13419 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13420 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13423 /* Dump the contents of glyph matrix MATRIX on stderr.
13425 GLYPHS 0 means don't show glyph contents.
13426 GLYPHS 1 means show glyphs in short form
13427 GLYPHS > 1 means show glyphs in long form. */
13429 void
13430 dump_glyph_matrix (matrix, glyphs)
13431 struct glyph_matrix *matrix;
13432 int glyphs;
13434 int i;
13435 for (i = 0; i < matrix->nrows; ++i)
13436 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13440 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13441 the glyph row and area where the glyph comes from. */
13443 void
13444 dump_glyph (row, glyph, area)
13445 struct glyph_row *row;
13446 struct glyph *glyph;
13447 int area;
13449 if (glyph->type == CHAR_GLYPH)
13451 fprintf (stderr,
13452 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13453 glyph - row->glyphs[TEXT_AREA],
13454 'C',
13455 glyph->charpos,
13456 (BUFFERP (glyph->object)
13457 ? 'B'
13458 : (STRINGP (glyph->object)
13459 ? 'S'
13460 : '-')),
13461 glyph->pixel_width,
13462 glyph->u.ch,
13463 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13464 ? glyph->u.ch
13465 : '.'),
13466 glyph->face_id,
13467 glyph->left_box_line_p,
13468 glyph->right_box_line_p);
13470 else if (glyph->type == STRETCH_GLYPH)
13472 fprintf (stderr,
13473 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13474 glyph - row->glyphs[TEXT_AREA],
13475 'S',
13476 glyph->charpos,
13477 (BUFFERP (glyph->object)
13478 ? 'B'
13479 : (STRINGP (glyph->object)
13480 ? 'S'
13481 : '-')),
13482 glyph->pixel_width,
13484 '.',
13485 glyph->face_id,
13486 glyph->left_box_line_p,
13487 glyph->right_box_line_p);
13489 else if (glyph->type == IMAGE_GLYPH)
13491 fprintf (stderr,
13492 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13493 glyph - row->glyphs[TEXT_AREA],
13494 'I',
13495 glyph->charpos,
13496 (BUFFERP (glyph->object)
13497 ? 'B'
13498 : (STRINGP (glyph->object)
13499 ? 'S'
13500 : '-')),
13501 glyph->pixel_width,
13502 glyph->u.img_id,
13503 '.',
13504 glyph->face_id,
13505 glyph->left_box_line_p,
13506 glyph->right_box_line_p);
13511 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13512 GLYPHS 0 means don't show glyph contents.
13513 GLYPHS 1 means show glyphs in short form
13514 GLYPHS > 1 means show glyphs in long form. */
13516 void
13517 dump_glyph_row (row, vpos, glyphs)
13518 struct glyph_row *row;
13519 int vpos, glyphs;
13521 if (glyphs != 1)
13523 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13524 fprintf (stderr, "=======================================================================\n");
13526 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13527 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13528 vpos,
13529 MATRIX_ROW_START_CHARPOS (row),
13530 MATRIX_ROW_END_CHARPOS (row),
13531 row->used[TEXT_AREA],
13532 row->contains_overlapping_glyphs_p,
13533 row->enabled_p,
13534 row->truncated_on_left_p,
13535 row->truncated_on_right_p,
13536 row->overlay_arrow_p,
13537 row->continued_p,
13538 MATRIX_ROW_CONTINUATION_LINE_P (row),
13539 row->displays_text_p,
13540 row->ends_at_zv_p,
13541 row->fill_line_p,
13542 row->ends_in_middle_of_char_p,
13543 row->starts_in_middle_of_char_p,
13544 row->mouse_face_p,
13545 row->x,
13546 row->y,
13547 row->pixel_width,
13548 row->height,
13549 row->visible_height,
13550 row->ascent,
13551 row->phys_ascent);
13552 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13553 row->end.overlay_string_index,
13554 row->continuation_lines_width);
13555 fprintf (stderr, "%9d %5d\n",
13556 CHARPOS (row->start.string_pos),
13557 CHARPOS (row->end.string_pos));
13558 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13559 row->end.dpvec_index);
13562 if (glyphs > 1)
13564 int area;
13566 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13568 struct glyph *glyph = row->glyphs[area];
13569 struct glyph *glyph_end = glyph + row->used[area];
13571 /* Glyph for a line end in text. */
13572 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13573 ++glyph_end;
13575 if (glyph < glyph_end)
13576 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13578 for (; glyph < glyph_end; ++glyph)
13579 dump_glyph (row, glyph, area);
13582 else if (glyphs == 1)
13584 int area;
13586 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13588 char *s = (char *) alloca (row->used[area] + 1);
13589 int i;
13591 for (i = 0; i < row->used[area]; ++i)
13593 struct glyph *glyph = row->glyphs[area] + i;
13594 if (glyph->type == CHAR_GLYPH
13595 && glyph->u.ch < 0x80
13596 && glyph->u.ch >= ' ')
13597 s[i] = glyph->u.ch;
13598 else
13599 s[i] = '.';
13602 s[i] = '\0';
13603 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13609 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13610 Sdump_glyph_matrix, 0, 1, "p",
13611 doc: /* Dump the current matrix of the selected window to stderr.
13612 Shows contents of glyph row structures. With non-nil
13613 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13614 glyphs in short form, otherwise show glyphs in long form. */)
13615 (glyphs)
13616 Lisp_Object glyphs;
13618 struct window *w = XWINDOW (selected_window);
13619 struct buffer *buffer = XBUFFER (w->buffer);
13621 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13622 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13623 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13624 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13625 fprintf (stderr, "=============================================\n");
13626 dump_glyph_matrix (w->current_matrix,
13627 NILP (glyphs) ? 0 : XINT (glyphs));
13628 return Qnil;
13632 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13633 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13636 struct frame *f = XFRAME (selected_frame);
13637 dump_glyph_matrix (f->current_matrix, 1);
13638 return Qnil;
13642 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13643 doc: /* Dump glyph row ROW to stderr.
13644 GLYPH 0 means don't dump glyphs.
13645 GLYPH 1 means dump glyphs in short form.
13646 GLYPH > 1 or omitted means dump glyphs in long form. */)
13647 (row, glyphs)
13648 Lisp_Object row, glyphs;
13650 struct glyph_matrix *matrix;
13651 int vpos;
13653 CHECK_NUMBER (row);
13654 matrix = XWINDOW (selected_window)->current_matrix;
13655 vpos = XINT (row);
13656 if (vpos >= 0 && vpos < matrix->nrows)
13657 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13658 vpos,
13659 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13660 return Qnil;
13664 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13665 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13666 GLYPH 0 means don't dump glyphs.
13667 GLYPH 1 means dump glyphs in short form.
13668 GLYPH > 1 or omitted means dump glyphs in long form. */)
13669 (row, glyphs)
13670 Lisp_Object row, glyphs;
13672 struct frame *sf = SELECTED_FRAME ();
13673 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13674 int vpos;
13676 CHECK_NUMBER (row);
13677 vpos = XINT (row);
13678 if (vpos >= 0 && vpos < m->nrows)
13679 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13680 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13681 return Qnil;
13685 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13686 doc: /* Toggle tracing of redisplay.
13687 With ARG, turn tracing on if and only if ARG is positive. */)
13688 (arg)
13689 Lisp_Object arg;
13691 if (NILP (arg))
13692 trace_redisplay_p = !trace_redisplay_p;
13693 else
13695 arg = Fprefix_numeric_value (arg);
13696 trace_redisplay_p = XINT (arg) > 0;
13699 return Qnil;
13703 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13704 doc: /* Like `format', but print result to stderr.
13705 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13706 (nargs, args)
13707 int nargs;
13708 Lisp_Object *args;
13710 Lisp_Object s = Fformat (nargs, args);
13711 fprintf (stderr, "%s", SDATA (s));
13712 return Qnil;
13715 #endif /* GLYPH_DEBUG */
13719 /***********************************************************************
13720 Building Desired Matrix Rows
13721 ***********************************************************************/
13723 /* Return a temporary glyph row holding the glyphs of an overlay
13724 arrow. Only used for non-window-redisplay windows. */
13726 static struct glyph_row *
13727 get_overlay_arrow_glyph_row (w)
13728 struct window *w;
13730 struct frame *f = XFRAME (WINDOW_FRAME (w));
13731 struct buffer *buffer = XBUFFER (w->buffer);
13732 struct buffer *old = current_buffer;
13733 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13734 int arrow_len = SCHARS (Voverlay_arrow_string);
13735 const unsigned char *arrow_end = arrow_string + arrow_len;
13736 const unsigned char *p;
13737 struct it it;
13738 int multibyte_p;
13739 int n_glyphs_before;
13741 set_buffer_temp (buffer);
13742 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13743 it.glyph_row->used[TEXT_AREA] = 0;
13744 SET_TEXT_POS (it.position, 0, 0);
13746 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13747 p = arrow_string;
13748 while (p < arrow_end)
13750 Lisp_Object face, ilisp;
13752 /* Get the next character. */
13753 if (multibyte_p)
13754 it.c = string_char_and_length (p, arrow_len, &it.len);
13755 else
13756 it.c = *p, it.len = 1;
13757 p += it.len;
13759 /* Get its face. */
13760 ilisp = make_number (p - arrow_string);
13761 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13762 it.face_id = compute_char_face (f, it.c, face);
13764 /* Compute its width, get its glyphs. */
13765 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13766 SET_TEXT_POS (it.position, -1, -1);
13767 PRODUCE_GLYPHS (&it);
13769 /* If this character doesn't fit any more in the line, we have
13770 to remove some glyphs. */
13771 if (it.current_x > it.last_visible_x)
13773 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13774 break;
13778 set_buffer_temp (old);
13779 return it.glyph_row;
13783 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13784 glyphs are only inserted for terminal frames since we can't really
13785 win with truncation glyphs when partially visible glyphs are
13786 involved. Which glyphs to insert is determined by
13787 produce_special_glyphs. */
13789 static void
13790 insert_left_trunc_glyphs (it)
13791 struct it *it;
13793 struct it truncate_it;
13794 struct glyph *from, *end, *to, *toend;
13796 xassert (!FRAME_WINDOW_P (it->f));
13798 /* Get the truncation glyphs. */
13799 truncate_it = *it;
13800 truncate_it.current_x = 0;
13801 truncate_it.face_id = DEFAULT_FACE_ID;
13802 truncate_it.glyph_row = &scratch_glyph_row;
13803 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13804 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13805 truncate_it.object = make_number (0);
13806 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13808 /* Overwrite glyphs from IT with truncation glyphs. */
13809 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13810 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13811 to = it->glyph_row->glyphs[TEXT_AREA];
13812 toend = to + it->glyph_row->used[TEXT_AREA];
13814 while (from < end)
13815 *to++ = *from++;
13817 /* There may be padding glyphs left over. Overwrite them too. */
13818 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13820 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13821 while (from < end)
13822 *to++ = *from++;
13825 if (to > toend)
13826 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13830 /* Compute the pixel height and width of IT->glyph_row.
13832 Most of the time, ascent and height of a display line will be equal
13833 to the max_ascent and max_height values of the display iterator
13834 structure. This is not the case if
13836 1. We hit ZV without displaying anything. In this case, max_ascent
13837 and max_height will be zero.
13839 2. We have some glyphs that don't contribute to the line height.
13840 (The glyph row flag contributes_to_line_height_p is for future
13841 pixmap extensions).
13843 The first case is easily covered by using default values because in
13844 these cases, the line height does not really matter, except that it
13845 must not be zero. */
13847 static void
13848 compute_line_metrics (it)
13849 struct it *it;
13851 struct glyph_row *row = it->glyph_row;
13852 int area, i;
13854 if (FRAME_WINDOW_P (it->f))
13856 int i, min_y, max_y;
13858 /* The line may consist of one space only, that was added to
13859 place the cursor on it. If so, the row's height hasn't been
13860 computed yet. */
13861 if (row->height == 0)
13863 if (it->max_ascent + it->max_descent == 0)
13864 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
13865 row->ascent = it->max_ascent;
13866 row->height = it->max_ascent + it->max_descent;
13867 row->phys_ascent = it->max_phys_ascent;
13868 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13871 /* Compute the width of this line. */
13872 row->pixel_width = row->x;
13873 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13874 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13876 xassert (row->pixel_width >= 0);
13877 xassert (row->ascent >= 0 && row->height > 0);
13879 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13880 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13882 /* If first line's physical ascent is larger than its logical
13883 ascent, use the physical ascent, and make the row taller.
13884 This makes accented characters fully visible. */
13885 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13886 && row->phys_ascent > row->ascent)
13888 row->height += row->phys_ascent - row->ascent;
13889 row->ascent = row->phys_ascent;
13892 /* Compute how much of the line is visible. */
13893 row->visible_height = row->height;
13895 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
13896 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
13898 if (row->y < min_y)
13899 row->visible_height -= min_y - row->y;
13900 if (row->y + row->height > max_y)
13901 row->visible_height -= row->y + row->height - max_y;
13903 else
13905 row->pixel_width = row->used[TEXT_AREA];
13906 if (row->continued_p)
13907 row->pixel_width -= it->continuation_pixel_width;
13908 else if (row->truncated_on_right_p)
13909 row->pixel_width -= it->truncation_pixel_width;
13910 row->ascent = row->phys_ascent = 0;
13911 row->height = row->phys_height = row->visible_height = 1;
13914 /* Compute a hash code for this row. */
13915 row->hash = 0;
13916 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13917 for (i = 0; i < row->used[area]; ++i)
13918 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13919 + row->glyphs[area][i].u.val
13920 + row->glyphs[area][i].face_id
13921 + row->glyphs[area][i].padding_p
13922 + (row->glyphs[area][i].type << 2));
13924 it->max_ascent = it->max_descent = 0;
13925 it->max_phys_ascent = it->max_phys_descent = 0;
13929 /* Append one space to the glyph row of iterator IT if doing a
13930 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13931 space have the default face, otherwise let it have the same face as
13932 IT->face_id. Value is non-zero if a space was added.
13934 This function is called to make sure that there is always one glyph
13935 at the end of a glyph row that the cursor can be set on under
13936 window-systems. (If there weren't such a glyph we would not know
13937 how wide and tall a box cursor should be displayed).
13939 At the same time this space let's a nicely handle clearing to the
13940 end of the line if the row ends in italic text. */
13942 static int
13943 append_space (it, default_face_p)
13944 struct it *it;
13945 int default_face_p;
13947 if (FRAME_WINDOW_P (it->f))
13949 int n = it->glyph_row->used[TEXT_AREA];
13951 if (it->glyph_row->glyphs[TEXT_AREA] + n
13952 < it->glyph_row->glyphs[1 + TEXT_AREA])
13954 /* Save some values that must not be changed.
13955 Must save IT->c and IT->len because otherwise
13956 ITERATOR_AT_END_P wouldn't work anymore after
13957 append_space has been called. */
13958 enum display_element_type saved_what = it->what;
13959 int saved_c = it->c, saved_len = it->len;
13960 int saved_x = it->current_x;
13961 int saved_face_id = it->face_id;
13962 struct text_pos saved_pos;
13963 Lisp_Object saved_object;
13964 struct face *face;
13966 saved_object = it->object;
13967 saved_pos = it->position;
13969 it->what = IT_CHARACTER;
13970 bzero (&it->position, sizeof it->position);
13971 it->object = make_number (0);
13972 it->c = ' ';
13973 it->len = 1;
13975 if (default_face_p)
13976 it->face_id = DEFAULT_FACE_ID;
13977 else if (it->face_before_selective_p)
13978 it->face_id = it->saved_face_id;
13979 face = FACE_FROM_ID (it->f, it->face_id);
13980 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
13982 PRODUCE_GLYPHS (it);
13984 it->current_x = saved_x;
13985 it->object = saved_object;
13986 it->position = saved_pos;
13987 it->what = saved_what;
13988 it->face_id = saved_face_id;
13989 it->len = saved_len;
13990 it->c = saved_c;
13991 return 1;
13995 return 0;
13999 /* Extend the face of the last glyph in the text area of IT->glyph_row
14000 to the end of the display line. Called from display_line.
14001 If the glyph row is empty, add a space glyph to it so that we
14002 know the face to draw. Set the glyph row flag fill_line_p. */
14004 static void
14005 extend_face_to_end_of_line (it)
14006 struct it *it;
14008 struct face *face;
14009 struct frame *f = it->f;
14011 /* If line is already filled, do nothing. */
14012 if (it->current_x >= it->last_visible_x)
14013 return;
14015 /* Face extension extends the background and box of IT->face_id
14016 to the end of the line. If the background equals the background
14017 of the frame, we don't have to do anything. */
14018 if (it->face_before_selective_p)
14019 face = FACE_FROM_ID (it->f, it->saved_face_id);
14020 else
14021 face = FACE_FROM_ID (f, it->face_id);
14023 if (FRAME_WINDOW_P (f)
14024 && face->box == FACE_NO_BOX
14025 && face->background == FRAME_BACKGROUND_PIXEL (f)
14026 && !face->stipple)
14027 return;
14029 /* Set the glyph row flag indicating that the face of the last glyph
14030 in the text area has to be drawn to the end of the text area. */
14031 it->glyph_row->fill_line_p = 1;
14033 /* If current character of IT is not ASCII, make sure we have the
14034 ASCII face. This will be automatically undone the next time
14035 get_next_display_element returns a multibyte character. Note
14036 that the character will always be single byte in unibyte text. */
14037 if (!SINGLE_BYTE_CHAR_P (it->c))
14039 it->face_id = FACE_FOR_CHAR (f, face, 0);
14042 if (FRAME_WINDOW_P (f))
14044 /* If the row is empty, add a space with the current face of IT,
14045 so that we know which face to draw. */
14046 if (it->glyph_row->used[TEXT_AREA] == 0)
14048 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14049 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14050 it->glyph_row->used[TEXT_AREA] = 1;
14053 else
14055 /* Save some values that must not be changed. */
14056 int saved_x = it->current_x;
14057 struct text_pos saved_pos;
14058 Lisp_Object saved_object;
14059 enum display_element_type saved_what = it->what;
14060 int saved_face_id = it->face_id;
14062 saved_object = it->object;
14063 saved_pos = it->position;
14065 it->what = IT_CHARACTER;
14066 bzero (&it->position, sizeof it->position);
14067 it->object = make_number (0);
14068 it->c = ' ';
14069 it->len = 1;
14070 it->face_id = face->id;
14072 PRODUCE_GLYPHS (it);
14074 while (it->current_x <= it->last_visible_x)
14075 PRODUCE_GLYPHS (it);
14077 /* Don't count these blanks really. It would let us insert a left
14078 truncation glyph below and make us set the cursor on them, maybe. */
14079 it->current_x = saved_x;
14080 it->object = saved_object;
14081 it->position = saved_pos;
14082 it->what = saved_what;
14083 it->face_id = saved_face_id;
14088 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14089 trailing whitespace. */
14091 static int
14092 trailing_whitespace_p (charpos)
14093 int charpos;
14095 int bytepos = CHAR_TO_BYTE (charpos);
14096 int c = 0;
14098 while (bytepos < ZV_BYTE
14099 && (c = FETCH_CHAR (bytepos),
14100 c == ' ' || c == '\t'))
14101 ++bytepos;
14103 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14105 if (bytepos != PT_BYTE)
14106 return 1;
14108 return 0;
14112 /* Highlight trailing whitespace, if any, in ROW. */
14114 void
14115 highlight_trailing_whitespace (f, row)
14116 struct frame *f;
14117 struct glyph_row *row;
14119 int used = row->used[TEXT_AREA];
14121 if (used)
14123 struct glyph *start = row->glyphs[TEXT_AREA];
14124 struct glyph *glyph = start + used - 1;
14126 /* Skip over glyphs inserted to display the cursor at the
14127 end of a line, for extending the face of the last glyph
14128 to the end of the line on terminals, and for truncation
14129 and continuation glyphs. */
14130 while (glyph >= start
14131 && glyph->type == CHAR_GLYPH
14132 && INTEGERP (glyph->object))
14133 --glyph;
14135 /* If last glyph is a space or stretch, and it's trailing
14136 whitespace, set the face of all trailing whitespace glyphs in
14137 IT->glyph_row to `trailing-whitespace'. */
14138 if (glyph >= start
14139 && BUFFERP (glyph->object)
14140 && (glyph->type == STRETCH_GLYPH
14141 || (glyph->type == CHAR_GLYPH
14142 && glyph->u.ch == ' '))
14143 && trailing_whitespace_p (glyph->charpos))
14145 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14147 while (glyph >= start
14148 && BUFFERP (glyph->object)
14149 && (glyph->type == STRETCH_GLYPH
14150 || (glyph->type == CHAR_GLYPH
14151 && glyph->u.ch == ' ')))
14152 (glyph--)->face_id = face_id;
14158 /* Value is non-zero if glyph row ROW in window W should be
14159 used to hold the cursor. */
14161 static int
14162 cursor_row_p (w, row)
14163 struct window *w;
14164 struct glyph_row *row;
14166 int cursor_row_p = 1;
14168 if (PT == MATRIX_ROW_END_CHARPOS (row))
14170 /* If the row ends with a newline from a string, we don't want
14171 the cursor there (if the row is continued it doesn't end in a
14172 newline). */
14173 if (CHARPOS (row->end.string_pos) >= 0
14174 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14175 cursor_row_p = row->continued_p;
14177 /* If the row ends at ZV, display the cursor at the end of that
14178 row instead of at the start of the row below. */
14179 else if (row->ends_at_zv_p)
14180 cursor_row_p = 1;
14181 else
14182 cursor_row_p = 0;
14185 return cursor_row_p;
14189 /* Construct the glyph row IT->glyph_row in the desired matrix of
14190 IT->w from text at the current position of IT. See dispextern.h
14191 for an overview of struct it. Value is non-zero if
14192 IT->glyph_row displays text, as opposed to a line displaying ZV
14193 only. */
14195 static int
14196 display_line (it)
14197 struct it *it;
14199 struct glyph_row *row = it->glyph_row;
14201 /* We always start displaying at hpos zero even if hscrolled. */
14202 xassert (it->hpos == 0 && it->current_x == 0);
14204 /* We must not display in a row that's not a text row. */
14205 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14206 < it->w->desired_matrix->nrows);
14208 /* Is IT->w showing the region? */
14209 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14211 /* Clear the result glyph row and enable it. */
14212 prepare_desired_row (row);
14214 row->y = it->current_y;
14215 row->start = it->current;
14216 row->continuation_lines_width = it->continuation_lines_width;
14217 row->displays_text_p = 1;
14218 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14219 it->starts_in_middle_of_char_p = 0;
14221 /* Arrange the overlays nicely for our purposes. Usually, we call
14222 display_line on only one line at a time, in which case this
14223 can't really hurt too much, or we call it on lines which appear
14224 one after another in the buffer, in which case all calls to
14225 recenter_overlay_lists but the first will be pretty cheap. */
14226 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14228 /* Move over display elements that are not visible because we are
14229 hscrolled. This may stop at an x-position < IT->first_visible_x
14230 if the first glyph is partially visible or if we hit a line end. */
14231 if (it->current_x < it->first_visible_x)
14232 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14233 MOVE_TO_POS | MOVE_TO_X);
14235 /* Get the initial row height. This is either the height of the
14236 text hscrolled, if there is any, or zero. */
14237 row->ascent = it->max_ascent;
14238 row->height = it->max_ascent + it->max_descent;
14239 row->phys_ascent = it->max_phys_ascent;
14240 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14242 /* Loop generating characters. The loop is left with IT on the next
14243 character to display. */
14244 while (1)
14246 int n_glyphs_before, hpos_before, x_before;
14247 int x, i, nglyphs;
14248 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14250 /* Retrieve the next thing to display. Value is zero if end of
14251 buffer reached. */
14252 if (!get_next_display_element (it))
14254 /* Maybe add a space at the end of this line that is used to
14255 display the cursor there under X. Set the charpos of the
14256 first glyph of blank lines not corresponding to any text
14257 to -1. */
14258 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14259 || row->used[TEXT_AREA] == 0)
14261 row->glyphs[TEXT_AREA]->charpos = -1;
14262 row->displays_text_p = 0;
14264 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14265 && (!MINI_WINDOW_P (it->w)
14266 || (minibuf_level && EQ (it->window, minibuf_window))))
14267 row->indicate_empty_line_p = 1;
14270 it->continuation_lines_width = 0;
14271 row->ends_at_zv_p = 1;
14272 break;
14275 /* Now, get the metrics of what we want to display. This also
14276 generates glyphs in `row' (which is IT->glyph_row). */
14277 n_glyphs_before = row->used[TEXT_AREA];
14278 x = it->current_x;
14280 /* Remember the line height so far in case the next element doesn't
14281 fit on the line. */
14282 if (!it->truncate_lines_p)
14284 ascent = it->max_ascent;
14285 descent = it->max_descent;
14286 phys_ascent = it->max_phys_ascent;
14287 phys_descent = it->max_phys_descent;
14290 PRODUCE_GLYPHS (it);
14292 /* If this display element was in marginal areas, continue with
14293 the next one. */
14294 if (it->area != TEXT_AREA)
14296 row->ascent = max (row->ascent, it->max_ascent);
14297 row->height = max (row->height, it->max_ascent + it->max_descent);
14298 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14299 row->phys_height = max (row->phys_height,
14300 it->max_phys_ascent + it->max_phys_descent);
14301 set_iterator_to_next (it, 1);
14302 continue;
14305 /* Does the display element fit on the line? If we truncate
14306 lines, we should draw past the right edge of the window. If
14307 we don't truncate, we want to stop so that we can display the
14308 continuation glyph before the right margin. If lines are
14309 continued, there are two possible strategies for characters
14310 resulting in more than 1 glyph (e.g. tabs): Display as many
14311 glyphs as possible in this line and leave the rest for the
14312 continuation line, or display the whole element in the next
14313 line. Original redisplay did the former, so we do it also. */
14314 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14315 hpos_before = it->hpos;
14316 x_before = x;
14318 if (/* Not a newline. */
14319 nglyphs > 0
14320 /* Glyphs produced fit entirely in the line. */
14321 && it->current_x < it->last_visible_x)
14323 it->hpos += nglyphs;
14324 row->ascent = max (row->ascent, it->max_ascent);
14325 row->height = max (row->height, it->max_ascent + it->max_descent);
14326 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14327 row->phys_height = max (row->phys_height,
14328 it->max_phys_ascent + it->max_phys_descent);
14329 if (it->current_x - it->pixel_width < it->first_visible_x)
14330 row->x = x - it->first_visible_x;
14332 else
14334 int new_x;
14335 struct glyph *glyph;
14337 for (i = 0; i < nglyphs; ++i, x = new_x)
14339 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14340 new_x = x + glyph->pixel_width;
14342 if (/* Lines are continued. */
14343 !it->truncate_lines_p
14344 && (/* Glyph doesn't fit on the line. */
14345 new_x > it->last_visible_x
14346 /* Or it fits exactly on a window system frame. */
14347 || (new_x == it->last_visible_x
14348 && FRAME_WINDOW_P (it->f))))
14350 /* End of a continued line. */
14352 if (it->hpos == 0
14353 || (new_x == it->last_visible_x
14354 && FRAME_WINDOW_P (it->f)))
14356 /* Current glyph is the only one on the line or
14357 fits exactly on the line. We must continue
14358 the line because we can't draw the cursor
14359 after the glyph. */
14360 row->continued_p = 1;
14361 it->current_x = new_x;
14362 it->continuation_lines_width += new_x;
14363 ++it->hpos;
14364 if (i == nglyphs - 1)
14365 set_iterator_to_next (it, 1);
14367 else if (CHAR_GLYPH_PADDING_P (*glyph)
14368 && !FRAME_WINDOW_P (it->f))
14370 /* A padding glyph that doesn't fit on this line.
14371 This means the whole character doesn't fit
14372 on the line. */
14373 row->used[TEXT_AREA] = n_glyphs_before;
14375 /* Fill the rest of the row with continuation
14376 glyphs like in 20.x. */
14377 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14378 < row->glyphs[1 + TEXT_AREA])
14379 produce_special_glyphs (it, IT_CONTINUATION);
14381 row->continued_p = 1;
14382 it->current_x = x_before;
14383 it->continuation_lines_width += x_before;
14385 /* Restore the height to what it was before the
14386 element not fitting on the line. */
14387 it->max_ascent = ascent;
14388 it->max_descent = descent;
14389 it->max_phys_ascent = phys_ascent;
14390 it->max_phys_descent = phys_descent;
14392 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14394 /* A TAB that extends past the right edge of the
14395 window. This produces a single glyph on
14396 window system frames. We leave the glyph in
14397 this row and let it fill the row, but don't
14398 consume the TAB. */
14399 it->continuation_lines_width += it->last_visible_x;
14400 row->ends_in_middle_of_char_p = 1;
14401 row->continued_p = 1;
14402 glyph->pixel_width = it->last_visible_x - x;
14403 it->starts_in_middle_of_char_p = 1;
14405 else
14407 /* Something other than a TAB that draws past
14408 the right edge of the window. Restore
14409 positions to values before the element. */
14410 row->used[TEXT_AREA] = n_glyphs_before + i;
14412 /* Display continuation glyphs. */
14413 if (!FRAME_WINDOW_P (it->f))
14414 produce_special_glyphs (it, IT_CONTINUATION);
14415 row->continued_p = 1;
14417 it->continuation_lines_width += x;
14419 if (nglyphs > 1 && i > 0)
14421 row->ends_in_middle_of_char_p = 1;
14422 it->starts_in_middle_of_char_p = 1;
14425 /* Restore the height to what it was before the
14426 element not fitting on the line. */
14427 it->max_ascent = ascent;
14428 it->max_descent = descent;
14429 it->max_phys_ascent = phys_ascent;
14430 it->max_phys_descent = phys_descent;
14433 break;
14435 else if (new_x > it->first_visible_x)
14437 /* Increment number of glyphs actually displayed. */
14438 ++it->hpos;
14440 if (x < it->first_visible_x)
14441 /* Glyph is partially visible, i.e. row starts at
14442 negative X position. */
14443 row->x = x - it->first_visible_x;
14445 else
14447 /* Glyph is completely off the left margin of the
14448 window. This should not happen because of the
14449 move_it_in_display_line at the start of this
14450 function, unless the text display area of the
14451 window is empty. */
14452 xassert (it->first_visible_x <= it->last_visible_x);
14456 row->ascent = max (row->ascent, it->max_ascent);
14457 row->height = max (row->height, it->max_ascent + it->max_descent);
14458 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14459 row->phys_height = max (row->phys_height,
14460 it->max_phys_ascent + it->max_phys_descent);
14462 /* End of this display line if row is continued. */
14463 if (row->continued_p)
14464 break;
14467 /* Is this a line end? If yes, we're also done, after making
14468 sure that a non-default face is extended up to the right
14469 margin of the window. */
14470 if (ITERATOR_AT_END_OF_LINE_P (it))
14472 int used_before = row->used[TEXT_AREA];
14474 row->ends_in_newline_from_string_p = STRINGP (it->object);
14476 /* Add a space at the end of the line that is used to
14477 display the cursor there. */
14478 append_space (it, 0);
14480 /* Extend the face to the end of the line. */
14481 extend_face_to_end_of_line (it);
14483 /* Make sure we have the position. */
14484 if (used_before == 0)
14485 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14487 /* Consume the line end. This skips over invisible lines. */
14488 set_iterator_to_next (it, 1);
14489 it->continuation_lines_width = 0;
14490 break;
14493 /* Proceed with next display element. Note that this skips
14494 over lines invisible because of selective display. */
14495 set_iterator_to_next (it, 1);
14497 /* If we truncate lines, we are done when the last displayed
14498 glyphs reach past the right margin of the window. */
14499 if (it->truncate_lines_p
14500 && (FRAME_WINDOW_P (it->f)
14501 ? (it->current_x >= it->last_visible_x)
14502 : (it->current_x > it->last_visible_x)))
14504 /* Maybe add truncation glyphs. */
14505 if (!FRAME_WINDOW_P (it->f))
14507 int i, n;
14509 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14510 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14511 break;
14513 for (n = row->used[TEXT_AREA]; i < n; ++i)
14515 row->used[TEXT_AREA] = i;
14516 produce_special_glyphs (it, IT_TRUNCATION);
14520 row->truncated_on_right_p = 1;
14521 it->continuation_lines_width = 0;
14522 reseat_at_next_visible_line_start (it, 0);
14523 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14524 it->hpos = hpos_before;
14525 it->current_x = x_before;
14526 break;
14530 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14531 at the left window margin. */
14532 if (it->first_visible_x
14533 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14535 if (!FRAME_WINDOW_P (it->f))
14536 insert_left_trunc_glyphs (it);
14537 row->truncated_on_left_p = 1;
14540 /* If the start of this line is the overlay arrow-position, then
14541 mark this glyph row as the one containing the overlay arrow.
14542 This is clearly a mess with variable size fonts. It would be
14543 better to let it be displayed like cursors under X. */
14544 if (MARKERP (Voverlay_arrow_position)
14545 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14546 && (MATRIX_ROW_START_CHARPOS (row)
14547 == marker_position (Voverlay_arrow_position))
14548 && STRINGP (Voverlay_arrow_string)
14549 && ! overlay_arrow_seen)
14551 /* Overlay arrow in window redisplay is a fringe bitmap. */
14552 if (!FRAME_WINDOW_P (it->f))
14554 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14555 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14556 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14557 struct glyph *p = row->glyphs[TEXT_AREA];
14558 struct glyph *p2, *end;
14560 /* Copy the arrow glyphs. */
14561 while (glyph < arrow_end)
14562 *p++ = *glyph++;
14564 /* Throw away padding glyphs. */
14565 p2 = p;
14566 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14567 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14568 ++p2;
14569 if (p2 > p)
14571 while (p2 < end)
14572 *p++ = *p2++;
14573 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14577 overlay_arrow_seen = 1;
14578 row->overlay_arrow_p = 1;
14581 /* Compute pixel dimensions of this line. */
14582 compute_line_metrics (it);
14584 /* Remember the position at which this line ends. */
14585 row->end = it->current;
14587 /* Maybe set the cursor. */
14588 if (it->w->cursor.vpos < 0
14589 && PT >= MATRIX_ROW_START_CHARPOS (row)
14590 && PT <= MATRIX_ROW_END_CHARPOS (row)
14591 && cursor_row_p (it->w, row))
14592 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14594 /* Highlight trailing whitespace. */
14595 if (!NILP (Vshow_trailing_whitespace))
14596 highlight_trailing_whitespace (it->f, it->glyph_row);
14598 /* Prepare for the next line. This line starts horizontally at (X
14599 HPOS) = (0 0). Vertical positions are incremented. As a
14600 convenience for the caller, IT->glyph_row is set to the next
14601 row to be used. */
14602 it->current_x = it->hpos = 0;
14603 it->current_y += row->height;
14604 ++it->vpos;
14605 ++it->glyph_row;
14606 return row->displays_text_p;
14611 /***********************************************************************
14612 Menu Bar
14613 ***********************************************************************/
14615 /* Redisplay the menu bar in the frame for window W.
14617 The menu bar of X frames that don't have X toolkit support is
14618 displayed in a special window W->frame->menu_bar_window.
14620 The menu bar of terminal frames is treated specially as far as
14621 glyph matrices are concerned. Menu bar lines are not part of
14622 windows, so the update is done directly on the frame matrix rows
14623 for the menu bar. */
14625 static void
14626 display_menu_bar (w)
14627 struct window *w;
14629 struct frame *f = XFRAME (WINDOW_FRAME (w));
14630 struct it it;
14631 Lisp_Object items;
14632 int i;
14634 /* Don't do all this for graphical frames. */
14635 #ifdef HAVE_NTGUI
14636 if (!NILP (Vwindow_system))
14637 return;
14638 #endif
14639 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14640 if (FRAME_X_P (f))
14641 return;
14642 #endif
14643 #ifdef MAC_OS
14644 if (FRAME_MAC_P (f))
14645 return;
14646 #endif
14648 #ifdef USE_X_TOOLKIT
14649 xassert (!FRAME_WINDOW_P (f));
14650 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14651 it.first_visible_x = 0;
14652 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
14653 #else /* not USE_X_TOOLKIT */
14654 if (FRAME_WINDOW_P (f))
14656 /* Menu bar lines are displayed in the desired matrix of the
14657 dummy window menu_bar_window. */
14658 struct window *menu_w;
14659 xassert (WINDOWP (f->menu_bar_window));
14660 menu_w = XWINDOW (f->menu_bar_window);
14661 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14662 MENU_FACE_ID);
14663 it.first_visible_x = 0;
14664 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
14666 else
14668 /* This is a TTY frame, i.e. character hpos/vpos are used as
14669 pixel x/y. */
14670 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14671 MENU_FACE_ID);
14672 it.first_visible_x = 0;
14673 it.last_visible_x = FRAME_WIDTH (f);
14675 #endif /* not USE_X_TOOLKIT */
14677 if (! mode_line_inverse_video)
14678 /* Force the menu-bar to be displayed in the default face. */
14679 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14681 /* Clear all rows of the menu bar. */
14682 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14684 struct glyph_row *row = it.glyph_row + i;
14685 clear_glyph_row (row);
14686 row->enabled_p = 1;
14687 row->full_width_p = 1;
14690 /* Display all items of the menu bar. */
14691 items = FRAME_MENU_BAR_ITEMS (it.f);
14692 for (i = 0; i < XVECTOR (items)->size; i += 4)
14694 Lisp_Object string;
14696 /* Stop at nil string. */
14697 string = AREF (items, i + 1);
14698 if (NILP (string))
14699 break;
14701 /* Remember where item was displayed. */
14702 AREF (items, i + 3) = make_number (it.hpos);
14704 /* Display the item, pad with one space. */
14705 if (it.current_x < it.last_visible_x)
14706 display_string (NULL, string, Qnil, 0, 0, &it,
14707 SCHARS (string) + 1, 0, 0, -1);
14710 /* Fill out the line with spaces. */
14711 if (it.current_x < it.last_visible_x)
14712 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14714 /* Compute the total height of the lines. */
14715 compute_line_metrics (&it);
14720 /***********************************************************************
14721 Mode Line
14722 ***********************************************************************/
14724 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14725 FORCE is non-zero, redisplay mode lines unconditionally.
14726 Otherwise, redisplay only mode lines that are garbaged. Value is
14727 the number of windows whose mode lines were redisplayed. */
14729 static int
14730 redisplay_mode_lines (window, force)
14731 Lisp_Object window;
14732 int force;
14734 int nwindows = 0;
14736 while (!NILP (window))
14738 struct window *w = XWINDOW (window);
14740 if (WINDOWP (w->hchild))
14741 nwindows += redisplay_mode_lines (w->hchild, force);
14742 else if (WINDOWP (w->vchild))
14743 nwindows += redisplay_mode_lines (w->vchild, force);
14744 else if (force
14745 || FRAME_GARBAGED_P (XFRAME (w->frame))
14746 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14748 struct text_pos lpoint;
14749 struct buffer *old = current_buffer;
14751 /* Set the window's buffer for the mode line display. */
14752 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14753 set_buffer_internal_1 (XBUFFER (w->buffer));
14755 /* Point refers normally to the selected window. For any
14756 other window, set up appropriate value. */
14757 if (!EQ (window, selected_window))
14759 struct text_pos pt;
14761 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14762 if (CHARPOS (pt) < BEGV)
14763 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14764 else if (CHARPOS (pt) > (ZV - 1))
14765 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14766 else
14767 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14770 /* Display mode lines. */
14771 clear_glyph_matrix (w->desired_matrix);
14772 if (display_mode_lines (w))
14774 ++nwindows;
14775 w->must_be_updated_p = 1;
14778 /* Restore old settings. */
14779 set_buffer_internal_1 (old);
14780 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14783 window = w->next;
14786 return nwindows;
14790 /* Display the mode and/or top line of window W. Value is the number
14791 of mode lines displayed. */
14793 static int
14794 display_mode_lines (w)
14795 struct window *w;
14797 Lisp_Object old_selected_window, old_selected_frame;
14798 int n = 0;
14800 old_selected_frame = selected_frame;
14801 selected_frame = w->frame;
14802 old_selected_window = selected_window;
14803 XSETWINDOW (selected_window, w);
14805 /* These will be set while the mode line specs are processed. */
14806 line_number_displayed = 0;
14807 w->column_number_displayed = Qnil;
14809 if (WINDOW_WANTS_MODELINE_P (w))
14811 struct window *sel_w = XWINDOW (old_selected_window);
14813 /* Select mode line face based on the real selected window. */
14814 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14815 current_buffer->mode_line_format);
14816 ++n;
14819 if (WINDOW_WANTS_HEADER_LINE_P (w))
14821 display_mode_line (w, HEADER_LINE_FACE_ID,
14822 current_buffer->header_line_format);
14823 ++n;
14826 selected_frame = old_selected_frame;
14827 selected_window = old_selected_window;
14828 return n;
14832 /* Display mode or top line of window W. FACE_ID specifies which line
14833 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14834 FORMAT is the mode line format to display. Value is the pixel
14835 height of the mode line displayed. */
14837 static int
14838 display_mode_line (w, face_id, format)
14839 struct window *w;
14840 enum face_id face_id;
14841 Lisp_Object format;
14843 struct it it;
14844 struct face *face;
14846 init_iterator (&it, w, -1, -1, NULL, face_id);
14847 prepare_desired_row (it.glyph_row);
14849 if (! mode_line_inverse_video)
14850 /* Force the mode-line to be displayed in the default face. */
14851 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14853 /* Temporarily make frame's keyboard the current kboard so that
14854 kboard-local variables in the mode_line_format will get the right
14855 values. */
14856 push_frame_kboard (it.f);
14857 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14858 pop_frame_kboard ();
14860 /* Fill up with spaces. */
14861 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14863 compute_line_metrics (&it);
14864 it.glyph_row->full_width_p = 1;
14865 it.glyph_row->mode_line_p = 1;
14866 it.glyph_row->continued_p = 0;
14867 it.glyph_row->truncated_on_left_p = 0;
14868 it.glyph_row->truncated_on_right_p = 0;
14870 /* Make a 3D mode-line have a shadow at its right end. */
14871 face = FACE_FROM_ID (it.f, face_id);
14872 extend_face_to_end_of_line (&it);
14873 if (face->box != FACE_NO_BOX)
14875 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14876 + it.glyph_row->used[TEXT_AREA] - 1);
14877 last->right_box_line_p = 1;
14880 return it.glyph_row->height;
14883 /* Alist that caches the results of :propertize.
14884 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14885 Lisp_Object mode_line_proptrans_alist;
14887 /* List of strings making up the mode-line. */
14888 Lisp_Object mode_line_string_list;
14890 /* Base face property when building propertized mode line string. */
14891 static Lisp_Object mode_line_string_face;
14892 static Lisp_Object mode_line_string_face_prop;
14895 /* Contribute ELT to the mode line for window IT->w. How it
14896 translates into text depends on its data type.
14898 IT describes the display environment in which we display, as usual.
14900 DEPTH is the depth in recursion. It is used to prevent
14901 infinite recursion here.
14903 FIELD_WIDTH is the number of characters the display of ELT should
14904 occupy in the mode line, and PRECISION is the maximum number of
14905 characters to display from ELT's representation. See
14906 display_string for details.
14908 Returns the hpos of the end of the text generated by ELT.
14910 PROPS is a property list to add to any string we encounter.
14912 If RISKY is nonzero, remove (disregard) any properties in any string
14913 we encounter, and ignore :eval and :propertize.
14915 If the global variable `frame_title_ptr' is non-NULL, then the output
14916 is passed to `store_frame_title' instead of `display_string'. */
14918 static int
14919 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14920 struct it *it;
14921 int depth;
14922 int field_width, precision;
14923 Lisp_Object elt, props;
14924 int risky;
14926 int n = 0, field, prec;
14927 int literal = 0;
14929 tail_recurse:
14930 if (depth > 10)
14931 goto invalid;
14933 depth++;
14935 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14937 case Lisp_String:
14939 /* A string: output it and check for %-constructs within it. */
14940 unsigned char c;
14941 const unsigned char *this, *lisp_string;
14943 if (!NILP (props) || risky)
14945 Lisp_Object oprops, aelt;
14946 oprops = Ftext_properties_at (make_number (0), elt);
14948 if (NILP (Fequal (props, oprops)) || risky)
14950 /* If the starting string has properties,
14951 merge the specified ones onto the existing ones. */
14952 if (! NILP (oprops) && !risky)
14954 Lisp_Object tem;
14956 oprops = Fcopy_sequence (oprops);
14957 tem = props;
14958 while (CONSP (tem))
14960 oprops = Fplist_put (oprops, XCAR (tem),
14961 XCAR (XCDR (tem)));
14962 tem = XCDR (XCDR (tem));
14964 props = oprops;
14967 aelt = Fassoc (elt, mode_line_proptrans_alist);
14968 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
14970 mode_line_proptrans_alist
14971 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
14972 elt = XCAR (aelt);
14974 else
14976 Lisp_Object tem;
14978 elt = Fcopy_sequence (elt);
14979 Fset_text_properties (make_number (0), Flength (elt),
14980 props, elt);
14981 /* Add this item to mode_line_proptrans_alist. */
14982 mode_line_proptrans_alist
14983 = Fcons (Fcons (elt, props),
14984 mode_line_proptrans_alist);
14985 /* Truncate mode_line_proptrans_alist
14986 to at most 50 elements. */
14987 tem = Fnthcdr (make_number (50),
14988 mode_line_proptrans_alist);
14989 if (! NILP (tem))
14990 XSETCDR (tem, Qnil);
14995 this = SDATA (elt);
14996 lisp_string = this;
14998 if (literal)
15000 prec = precision - n;
15001 if (frame_title_ptr)
15002 n += store_frame_title (SDATA (elt), -1, prec);
15003 else if (!NILP (mode_line_string_list))
15004 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15005 else
15006 n += display_string (NULL, elt, Qnil, 0, 0, it,
15007 0, prec, 0, STRING_MULTIBYTE (elt));
15009 break;
15012 while ((precision <= 0 || n < precision)
15013 && *this
15014 && (frame_title_ptr
15015 || !NILP (mode_line_string_list)
15016 || it->current_x < it->last_visible_x))
15018 const unsigned char *last = this;
15020 /* Advance to end of string or next format specifier. */
15021 while ((c = *this++) != '\0' && c != '%')
15024 if (this - 1 != last)
15026 /* Output to end of string or up to '%'. Field width
15027 is length of string. Don't output more than
15028 PRECISION allows us. */
15029 --this;
15031 prec = chars_in_text (last, this - last);
15032 if (precision > 0 && prec > precision - n)
15033 prec = precision - n;
15035 if (frame_title_ptr)
15036 n += store_frame_title (last, 0, prec);
15037 else if (!NILP (mode_line_string_list))
15039 int bytepos = last - lisp_string;
15040 int charpos = string_byte_to_char (elt, bytepos);
15041 n += store_mode_line_string (NULL,
15042 Fsubstring (elt, make_number (charpos),
15043 make_number (charpos + prec)),
15044 0, 0, 0, Qnil);
15046 else
15048 int bytepos = last - lisp_string;
15049 int charpos = string_byte_to_char (elt, bytepos);
15050 n += display_string (NULL, elt, Qnil, 0, charpos,
15051 it, 0, prec, 0,
15052 STRING_MULTIBYTE (elt));
15055 else /* c == '%' */
15057 const unsigned char *percent_position = this;
15059 /* Get the specified minimum width. Zero means
15060 don't pad. */
15061 field = 0;
15062 while ((c = *this++) >= '0' && c <= '9')
15063 field = field * 10 + c - '0';
15065 /* Don't pad beyond the total padding allowed. */
15066 if (field_width - n > 0 && field > field_width - n)
15067 field = field_width - n;
15069 /* Note that either PRECISION <= 0 or N < PRECISION. */
15070 prec = precision - n;
15072 if (c == 'M')
15073 n += display_mode_element (it, depth, field, prec,
15074 Vglobal_mode_string, props,
15075 risky);
15076 else if (c != 0)
15078 int multibyte;
15079 int bytepos, charpos;
15080 unsigned char *spec;
15082 bytepos = percent_position - lisp_string;
15083 charpos = (STRING_MULTIBYTE (elt)
15084 ? string_byte_to_char (elt, bytepos)
15085 : bytepos);
15087 spec
15088 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15090 if (frame_title_ptr)
15091 n += store_frame_title (spec, field, prec);
15092 else if (!NILP (mode_line_string_list))
15094 int len = strlen (spec);
15095 Lisp_Object tem = make_string (spec, len);
15096 props = Ftext_properties_at (make_number (charpos), elt);
15097 /* Should only keep face property in props */
15098 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15100 else
15102 int nglyphs_before, nwritten;
15104 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15105 nwritten = display_string (spec, Qnil, elt,
15106 charpos, 0, it,
15107 field, prec, 0,
15108 multibyte);
15110 /* Assign to the glyphs written above the
15111 string where the `%x' came from, position
15112 of the `%'. */
15113 if (nwritten > 0)
15115 struct glyph *glyph
15116 = (it->glyph_row->glyphs[TEXT_AREA]
15117 + nglyphs_before);
15118 int i;
15120 for (i = 0; i < nwritten; ++i)
15122 glyph[i].object = elt;
15123 glyph[i].charpos = charpos;
15126 n += nwritten;
15130 else /* c == 0 */
15131 break;
15135 break;
15137 case Lisp_Symbol:
15138 /* A symbol: process the value of the symbol recursively
15139 as if it appeared here directly. Avoid error if symbol void.
15140 Special case: if value of symbol is a string, output the string
15141 literally. */
15143 register Lisp_Object tem;
15145 /* If the variable is not marked as risky to set
15146 then its contents are risky to use. */
15147 if (NILP (Fget (elt, Qrisky_local_variable)))
15148 risky = 1;
15150 tem = Fboundp (elt);
15151 if (!NILP (tem))
15153 tem = Fsymbol_value (elt);
15154 /* If value is a string, output that string literally:
15155 don't check for % within it. */
15156 if (STRINGP (tem))
15157 literal = 1;
15159 if (!EQ (tem, elt))
15161 /* Give up right away for nil or t. */
15162 elt = tem;
15163 goto tail_recurse;
15167 break;
15169 case Lisp_Cons:
15171 register Lisp_Object car, tem;
15173 /* A cons cell: five distinct cases.
15174 If first element is :eval or :propertize, do something special.
15175 If first element is a string or a cons, process all the elements
15176 and effectively concatenate them.
15177 If first element is a negative number, truncate displaying cdr to
15178 at most that many characters. If positive, pad (with spaces)
15179 to at least that many characters.
15180 If first element is a symbol, process the cadr or caddr recursively
15181 according to whether the symbol's value is non-nil or nil. */
15182 car = XCAR (elt);
15183 if (EQ (car, QCeval))
15185 /* An element of the form (:eval FORM) means evaluate FORM
15186 and use the result as mode line elements. */
15188 if (risky)
15189 break;
15191 if (CONSP (XCDR (elt)))
15193 Lisp_Object spec;
15194 spec = safe_eval (XCAR (XCDR (elt)));
15195 n += display_mode_element (it, depth, field_width - n,
15196 precision - n, spec, props,
15197 risky);
15200 else if (EQ (car, QCpropertize))
15202 /* An element of the form (:propertize ELT PROPS...)
15203 means display ELT but applying properties PROPS. */
15205 if (risky)
15206 break;
15208 if (CONSP (XCDR (elt)))
15209 n += display_mode_element (it, depth, field_width - n,
15210 precision - n, XCAR (XCDR (elt)),
15211 XCDR (XCDR (elt)), risky);
15213 else if (SYMBOLP (car))
15215 tem = Fboundp (car);
15216 elt = XCDR (elt);
15217 if (!CONSP (elt))
15218 goto invalid;
15219 /* elt is now the cdr, and we know it is a cons cell.
15220 Use its car if CAR has a non-nil value. */
15221 if (!NILP (tem))
15223 tem = Fsymbol_value (car);
15224 if (!NILP (tem))
15226 elt = XCAR (elt);
15227 goto tail_recurse;
15230 /* Symbol's value is nil (or symbol is unbound)
15231 Get the cddr of the original list
15232 and if possible find the caddr and use that. */
15233 elt = XCDR (elt);
15234 if (NILP (elt))
15235 break;
15236 else if (!CONSP (elt))
15237 goto invalid;
15238 elt = XCAR (elt);
15239 goto tail_recurse;
15241 else if (INTEGERP (car))
15243 register int lim = XINT (car);
15244 elt = XCDR (elt);
15245 if (lim < 0)
15247 /* Negative int means reduce maximum width. */
15248 if (precision <= 0)
15249 precision = -lim;
15250 else
15251 precision = min (precision, -lim);
15253 else if (lim > 0)
15255 /* Padding specified. Don't let it be more than
15256 current maximum. */
15257 if (precision > 0)
15258 lim = min (precision, lim);
15260 /* If that's more padding than already wanted, queue it.
15261 But don't reduce padding already specified even if
15262 that is beyond the current truncation point. */
15263 field_width = max (lim, field_width);
15265 goto tail_recurse;
15267 else if (STRINGP (car) || CONSP (car))
15269 register int limit = 50;
15270 /* Limit is to protect against circular lists. */
15271 while (CONSP (elt)
15272 && --limit > 0
15273 && (precision <= 0 || n < precision))
15275 n += display_mode_element (it, depth, field_width - n,
15276 precision - n, XCAR (elt),
15277 props, risky);
15278 elt = XCDR (elt);
15282 break;
15284 default:
15285 invalid:
15286 if (frame_title_ptr)
15287 n += store_frame_title ("*invalid*", 0, precision - n);
15288 else if (!NILP (mode_line_string_list))
15289 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
15290 else
15291 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
15292 precision - n, 0, 0);
15293 return n;
15296 /* Pad to FIELD_WIDTH. */
15297 if (field_width > 0 && n < field_width)
15299 if (frame_title_ptr)
15300 n += store_frame_title ("", field_width - n, 0);
15301 else if (!NILP (mode_line_string_list))
15302 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15303 else
15304 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15305 0, 0, 0);
15308 return n;
15311 /* Store a mode-line string element in mode_line_string_list.
15313 If STRING is non-null, display that C string. Otherwise, the Lisp
15314 string LISP_STRING is displayed.
15316 FIELD_WIDTH is the minimum number of output glyphs to produce.
15317 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15318 with spaces. FIELD_WIDTH <= 0 means don't pad.
15320 PRECISION is the maximum number of characters to output from
15321 STRING. PRECISION <= 0 means don't truncate the string.
15323 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15324 properties to the string.
15326 PROPS are the properties to add to the string.
15327 The mode_line_string_face face property is always added to the string.
15330 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15331 char *string;
15332 Lisp_Object lisp_string;
15333 int copy_string;
15334 int field_width;
15335 int precision;
15336 Lisp_Object props;
15338 int len;
15339 int n = 0;
15341 if (string != NULL)
15343 len = strlen (string);
15344 if (precision > 0 && len > precision)
15345 len = precision;
15346 lisp_string = make_string (string, len);
15347 if (NILP (props))
15348 props = mode_line_string_face_prop;
15349 else if (!NILP (mode_line_string_face))
15351 Lisp_Object face = Fplist_get (props, Qface);
15352 props = Fcopy_sequence (props);
15353 if (NILP (face))
15354 face = mode_line_string_face;
15355 else
15356 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15357 props = Fplist_put (props, Qface, face);
15359 Fadd_text_properties (make_number (0), make_number (len),
15360 props, lisp_string);
15362 else
15364 len = XFASTINT (Flength (lisp_string));
15365 if (precision > 0 && len > precision)
15367 len = precision;
15368 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15369 precision = -1;
15371 if (!NILP (mode_line_string_face))
15373 Lisp_Object face;
15374 if (NILP (props))
15375 props = Ftext_properties_at (make_number (0), lisp_string);
15376 face = Fplist_get (props, Qface);
15377 if (NILP (face))
15378 face = mode_line_string_face;
15379 else
15380 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15381 props = Fcons (Qface, Fcons (face, Qnil));
15382 if (copy_string)
15383 lisp_string = Fcopy_sequence (lisp_string);
15385 if (!NILP (props))
15386 Fadd_text_properties (make_number (0), make_number (len),
15387 props, lisp_string);
15390 if (len > 0)
15392 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15393 n += len;
15396 if (field_width > len)
15398 field_width -= len;
15399 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15400 if (!NILP (props))
15401 Fadd_text_properties (make_number (0), make_number (field_width),
15402 props, lisp_string);
15403 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15404 n += field_width;
15407 return n;
15411 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15412 0, 3, 0,
15413 doc: /* Return the mode-line of selected window as a string.
15414 First optional arg FORMAT specifies a different format string (see
15415 `mode-line-format' for details) to use. If FORMAT is t, return
15416 the buffer's header-line. Second optional arg WINDOW specifies a
15417 different window to use as the context for the formatting.
15418 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15419 (format, window, no_props)
15420 Lisp_Object format, window, no_props;
15422 struct it it;
15423 int len;
15424 struct window *w;
15425 struct buffer *old_buffer = NULL;
15426 enum face_id face_id = DEFAULT_FACE_ID;
15428 if (NILP (window))
15429 window = selected_window;
15430 CHECK_WINDOW (window);
15431 w = XWINDOW (window);
15432 CHECK_BUFFER (w->buffer);
15434 if (XBUFFER (w->buffer) != current_buffer)
15436 old_buffer = current_buffer;
15437 set_buffer_internal_1 (XBUFFER (w->buffer));
15440 if (NILP (format) || EQ (format, Qt))
15442 face_id = NILP (format)
15443 ? CURRENT_MODE_LINE_FACE_ID (w) :
15444 HEADER_LINE_FACE_ID;
15445 format = NILP (format)
15446 ? current_buffer->mode_line_format
15447 : current_buffer->header_line_format;
15450 init_iterator (&it, w, -1, -1, NULL, face_id);
15452 if (NILP (no_props))
15454 mode_line_string_face =
15455 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15456 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15457 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15459 mode_line_string_face_prop =
15460 NILP (mode_line_string_face) ? Qnil :
15461 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15463 /* We need a dummy last element in mode_line_string_list to
15464 indicate we are building the propertized mode-line string.
15465 Using mode_line_string_face_prop here GC protects it. */
15466 mode_line_string_list =
15467 Fcons (mode_line_string_face_prop, Qnil);
15468 frame_title_ptr = NULL;
15470 else
15472 mode_line_string_face_prop = Qnil;
15473 mode_line_string_list = Qnil;
15474 frame_title_ptr = frame_title_buf;
15477 push_frame_kboard (it.f);
15478 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15479 pop_frame_kboard ();
15481 if (old_buffer)
15482 set_buffer_internal_1 (old_buffer);
15484 if (NILP (no_props))
15486 Lisp_Object str;
15487 mode_line_string_list = Fnreverse (mode_line_string_list);
15488 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15489 make_string ("", 0));
15490 mode_line_string_face_prop = Qnil;
15491 mode_line_string_list = Qnil;
15492 return str;
15495 len = frame_title_ptr - frame_title_buf;
15496 if (len > 0 && frame_title_ptr[-1] == '-')
15498 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15499 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15501 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15502 if (len > frame_title_ptr - frame_title_buf)
15503 len = frame_title_ptr - frame_title_buf;
15506 frame_title_ptr = NULL;
15507 return make_string (frame_title_buf, len);
15510 /* Write a null-terminated, right justified decimal representation of
15511 the positive integer D to BUF using a minimal field width WIDTH. */
15513 static void
15514 pint2str (buf, width, d)
15515 register char *buf;
15516 register int width;
15517 register int d;
15519 register char *p = buf;
15521 if (d <= 0)
15522 *p++ = '0';
15523 else
15525 while (d > 0)
15527 *p++ = d % 10 + '0';
15528 d /= 10;
15532 for (width -= (int) (p - buf); width > 0; --width)
15533 *p++ = ' ';
15534 *p-- = '\0';
15535 while (p > buf)
15537 d = *buf;
15538 *buf++ = *p;
15539 *p-- = d;
15543 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15544 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15545 type of CODING_SYSTEM. Return updated pointer into BUF. */
15547 static unsigned char invalid_eol_type[] = "(*invalid*)";
15549 static char *
15550 decode_mode_spec_coding (coding_system, buf, eol_flag)
15551 Lisp_Object coding_system;
15552 register char *buf;
15553 int eol_flag;
15555 Lisp_Object val;
15556 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15557 const unsigned char *eol_str;
15558 int eol_str_len;
15559 /* The EOL conversion we are using. */
15560 Lisp_Object eoltype;
15562 val = Fget (coding_system, Qcoding_system);
15563 eoltype = Qnil;
15565 if (!VECTORP (val)) /* Not yet decided. */
15567 if (multibyte)
15568 *buf++ = '-';
15569 if (eol_flag)
15570 eoltype = eol_mnemonic_undecided;
15571 /* Don't mention EOL conversion if it isn't decided. */
15573 else
15575 Lisp_Object eolvalue;
15577 eolvalue = Fget (coding_system, Qeol_type);
15579 if (multibyte)
15580 *buf++ = XFASTINT (AREF (val, 1));
15582 if (eol_flag)
15584 /* The EOL conversion that is normal on this system. */
15586 if (NILP (eolvalue)) /* Not yet decided. */
15587 eoltype = eol_mnemonic_undecided;
15588 else if (VECTORP (eolvalue)) /* Not yet decided. */
15589 eoltype = eol_mnemonic_undecided;
15590 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15591 eoltype = (XFASTINT (eolvalue) == 0
15592 ? eol_mnemonic_unix
15593 : (XFASTINT (eolvalue) == 1
15594 ? eol_mnemonic_dos : eol_mnemonic_mac));
15598 if (eol_flag)
15600 /* Mention the EOL conversion if it is not the usual one. */
15601 if (STRINGP (eoltype))
15603 eol_str = SDATA (eoltype);
15604 eol_str_len = SBYTES (eoltype);
15606 else if (INTEGERP (eoltype)
15607 && CHAR_VALID_P (XINT (eoltype), 0))
15609 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15610 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15611 eol_str = tmp;
15613 else
15615 eol_str = invalid_eol_type;
15616 eol_str_len = sizeof (invalid_eol_type) - 1;
15618 bcopy (eol_str, buf, eol_str_len);
15619 buf += eol_str_len;
15622 return buf;
15625 /* Return a string for the output of a mode line %-spec for window W,
15626 generated by character C. PRECISION >= 0 means don't return a
15627 string longer than that value. FIELD_WIDTH > 0 means pad the
15628 string returned with spaces to that value. Return 1 in *MULTIBYTE
15629 if the result is multibyte text. */
15631 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15633 static char *
15634 decode_mode_spec (w, c, field_width, precision, multibyte)
15635 struct window *w;
15636 register int c;
15637 int field_width, precision;
15638 int *multibyte;
15640 Lisp_Object obj;
15641 struct frame *f = XFRAME (WINDOW_FRAME (w));
15642 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15643 struct buffer *b = XBUFFER (w->buffer);
15645 obj = Qnil;
15646 *multibyte = 0;
15648 switch (c)
15650 case '*':
15651 if (!NILP (b->read_only))
15652 return "%";
15653 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15654 return "*";
15655 return "-";
15657 case '+':
15658 /* This differs from %* only for a modified read-only buffer. */
15659 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15660 return "*";
15661 if (!NILP (b->read_only))
15662 return "%";
15663 return "-";
15665 case '&':
15666 /* This differs from %* in ignoring read-only-ness. */
15667 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15668 return "*";
15669 return "-";
15671 case '%':
15672 return "%";
15674 case '[':
15676 int i;
15677 char *p;
15679 if (command_loop_level > 5)
15680 return "[[[... ";
15681 p = decode_mode_spec_buf;
15682 for (i = 0; i < command_loop_level; i++)
15683 *p++ = '[';
15684 *p = 0;
15685 return decode_mode_spec_buf;
15688 case ']':
15690 int i;
15691 char *p;
15693 if (command_loop_level > 5)
15694 return " ...]]]";
15695 p = decode_mode_spec_buf;
15696 for (i = 0; i < command_loop_level; i++)
15697 *p++ = ']';
15698 *p = 0;
15699 return decode_mode_spec_buf;
15702 case '-':
15704 register int i;
15706 /* Let lots_of_dashes be a string of infinite length. */
15707 if (!NILP (mode_line_string_list))
15708 return "--";
15709 if (field_width <= 0
15710 || field_width > sizeof (lots_of_dashes))
15712 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15713 decode_mode_spec_buf[i] = '-';
15714 decode_mode_spec_buf[i] = '\0';
15715 return decode_mode_spec_buf;
15717 else
15718 return lots_of_dashes;
15721 case 'b':
15722 obj = b->name;
15723 break;
15725 case 'c':
15727 int col = (int) current_column (); /* iftc */
15728 w->column_number_displayed = make_number (col);
15729 pint2str (decode_mode_spec_buf, field_width, col);
15730 return decode_mode_spec_buf;
15733 case 'F':
15734 /* %F displays the frame name. */
15735 if (!NILP (f->title))
15736 return (char *) SDATA (f->title);
15737 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15738 return (char *) SDATA (f->name);
15739 return "Emacs";
15741 case 'f':
15742 obj = b->filename;
15743 break;
15745 case 'l':
15747 int startpos = XMARKER (w->start)->charpos;
15748 int startpos_byte = marker_byte_position (w->start);
15749 int line, linepos, linepos_byte, topline;
15750 int nlines, junk;
15751 int height = XFASTINT (w->height);
15753 /* If we decided that this buffer isn't suitable for line numbers,
15754 don't forget that too fast. */
15755 if (EQ (w->base_line_pos, w->buffer))
15756 goto no_value;
15757 /* But do forget it, if the window shows a different buffer now. */
15758 else if (BUFFERP (w->base_line_pos))
15759 w->base_line_pos = Qnil;
15761 /* If the buffer is very big, don't waste time. */
15762 if (INTEGERP (Vline_number_display_limit)
15763 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15765 w->base_line_pos = Qnil;
15766 w->base_line_number = Qnil;
15767 goto no_value;
15770 if (!NILP (w->base_line_number)
15771 && !NILP (w->base_line_pos)
15772 && XFASTINT (w->base_line_pos) <= startpos)
15774 line = XFASTINT (w->base_line_number);
15775 linepos = XFASTINT (w->base_line_pos);
15776 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15778 else
15780 line = 1;
15781 linepos = BUF_BEGV (b);
15782 linepos_byte = BUF_BEGV_BYTE (b);
15785 /* Count lines from base line to window start position. */
15786 nlines = display_count_lines (linepos, linepos_byte,
15787 startpos_byte,
15788 startpos, &junk);
15790 topline = nlines + line;
15792 /* Determine a new base line, if the old one is too close
15793 or too far away, or if we did not have one.
15794 "Too close" means it's plausible a scroll-down would
15795 go back past it. */
15796 if (startpos == BUF_BEGV (b))
15798 w->base_line_number = make_number (topline);
15799 w->base_line_pos = make_number (BUF_BEGV (b));
15801 else if (nlines < height + 25 || nlines > height * 3 + 50
15802 || linepos == BUF_BEGV (b))
15804 int limit = BUF_BEGV (b);
15805 int limit_byte = BUF_BEGV_BYTE (b);
15806 int position;
15807 int distance = (height * 2 + 30) * line_number_display_limit_width;
15809 if (startpos - distance > limit)
15811 limit = startpos - distance;
15812 limit_byte = CHAR_TO_BYTE (limit);
15815 nlines = display_count_lines (startpos, startpos_byte,
15816 limit_byte,
15817 - (height * 2 + 30),
15818 &position);
15819 /* If we couldn't find the lines we wanted within
15820 line_number_display_limit_width chars per line,
15821 give up on line numbers for this window. */
15822 if (position == limit_byte && limit == startpos - distance)
15824 w->base_line_pos = w->buffer;
15825 w->base_line_number = Qnil;
15826 goto no_value;
15829 w->base_line_number = make_number (topline - nlines);
15830 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15833 /* Now count lines from the start pos to point. */
15834 nlines = display_count_lines (startpos, startpos_byte,
15835 PT_BYTE, PT, &junk);
15837 /* Record that we did display the line number. */
15838 line_number_displayed = 1;
15840 /* Make the string to show. */
15841 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15842 return decode_mode_spec_buf;
15843 no_value:
15845 char* p = decode_mode_spec_buf;
15846 int pad = field_width - 2;
15847 while (pad-- > 0)
15848 *p++ = ' ';
15849 *p++ = '?';
15850 *p++ = '?';
15851 *p = '\0';
15852 return decode_mode_spec_buf;
15855 break;
15857 case 'm':
15858 obj = b->mode_name;
15859 break;
15861 case 'n':
15862 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15863 return " Narrow";
15864 break;
15866 case 'p':
15868 int pos = marker_position (w->start);
15869 int total = BUF_ZV (b) - BUF_BEGV (b);
15871 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15873 if (pos <= BUF_BEGV (b))
15874 return "All";
15875 else
15876 return "Bottom";
15878 else if (pos <= BUF_BEGV (b))
15879 return "Top";
15880 else
15882 if (total > 1000000)
15883 /* Do it differently for a large value, to avoid overflow. */
15884 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15885 else
15886 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15887 /* We can't normally display a 3-digit number,
15888 so get us a 2-digit number that is close. */
15889 if (total == 100)
15890 total = 99;
15891 sprintf (decode_mode_spec_buf, "%2d%%", total);
15892 return decode_mode_spec_buf;
15896 /* Display percentage of size above the bottom of the screen. */
15897 case 'P':
15899 int toppos = marker_position (w->start);
15900 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15901 int total = BUF_ZV (b) - BUF_BEGV (b);
15903 if (botpos >= BUF_ZV (b))
15905 if (toppos <= BUF_BEGV (b))
15906 return "All";
15907 else
15908 return "Bottom";
15910 else
15912 if (total > 1000000)
15913 /* Do it differently for a large value, to avoid overflow. */
15914 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15915 else
15916 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15917 /* We can't normally display a 3-digit number,
15918 so get us a 2-digit number that is close. */
15919 if (total == 100)
15920 total = 99;
15921 if (toppos <= BUF_BEGV (b))
15922 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15923 else
15924 sprintf (decode_mode_spec_buf, "%2d%%", total);
15925 return decode_mode_spec_buf;
15929 case 's':
15930 /* status of process */
15931 obj = Fget_buffer_process (w->buffer);
15932 if (NILP (obj))
15933 return "no process";
15934 #ifdef subprocesses
15935 obj = Fsymbol_name (Fprocess_status (obj));
15936 #endif
15937 break;
15939 case 't': /* indicate TEXT or BINARY */
15940 #ifdef MODE_LINE_BINARY_TEXT
15941 return MODE_LINE_BINARY_TEXT (b);
15942 #else
15943 return "T";
15944 #endif
15946 case 'z':
15947 /* coding-system (not including end-of-line format) */
15948 case 'Z':
15949 /* coding-system (including end-of-line type) */
15951 int eol_flag = (c == 'Z');
15952 char *p = decode_mode_spec_buf;
15954 if (! FRAME_WINDOW_P (f))
15956 /* No need to mention EOL here--the terminal never needs
15957 to do EOL conversion. */
15958 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15959 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
15961 p = decode_mode_spec_coding (b->buffer_file_coding_system,
15962 p, eol_flag);
15964 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
15965 #ifdef subprocesses
15966 obj = Fget_buffer_process (Fcurrent_buffer ());
15967 if (PROCESSP (obj))
15969 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
15970 p, eol_flag);
15971 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
15972 p, eol_flag);
15974 #endif /* subprocesses */
15975 #endif /* 0 */
15976 *p = 0;
15977 return decode_mode_spec_buf;
15981 if (STRINGP (obj))
15983 *multibyte = STRING_MULTIBYTE (obj);
15984 return (char *) SDATA (obj);
15986 else
15987 return "";
15991 /* Count up to COUNT lines starting from START / START_BYTE.
15992 But don't go beyond LIMIT_BYTE.
15993 Return the number of lines thus found (always nonnegative).
15995 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
15997 static int
15998 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15999 int start, start_byte, limit_byte, count;
16000 int *byte_pos_ptr;
16002 register unsigned char *cursor;
16003 unsigned char *base;
16005 register int ceiling;
16006 register unsigned char *ceiling_addr;
16007 int orig_count = count;
16009 /* If we are not in selective display mode,
16010 check only for newlines. */
16011 int selective_display = (!NILP (current_buffer->selective_display)
16012 && !INTEGERP (current_buffer->selective_display));
16014 if (count > 0)
16016 while (start_byte < limit_byte)
16018 ceiling = BUFFER_CEILING_OF (start_byte);
16019 ceiling = min (limit_byte - 1, ceiling);
16020 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16021 base = (cursor = BYTE_POS_ADDR (start_byte));
16022 while (1)
16024 if (selective_display)
16025 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16027 else
16028 while (*cursor != '\n' && ++cursor != ceiling_addr)
16031 if (cursor != ceiling_addr)
16033 if (--count == 0)
16035 start_byte += cursor - base + 1;
16036 *byte_pos_ptr = start_byte;
16037 return orig_count;
16039 else
16040 if (++cursor == ceiling_addr)
16041 break;
16043 else
16044 break;
16046 start_byte += cursor - base;
16049 else
16051 while (start_byte > limit_byte)
16053 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16054 ceiling = max (limit_byte, ceiling);
16055 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16056 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16057 while (1)
16059 if (selective_display)
16060 while (--cursor != ceiling_addr
16061 && *cursor != '\n' && *cursor != 015)
16063 else
16064 while (--cursor != ceiling_addr && *cursor != '\n')
16067 if (cursor != ceiling_addr)
16069 if (++count == 0)
16071 start_byte += cursor - base + 1;
16072 *byte_pos_ptr = start_byte;
16073 /* When scanning backwards, we should
16074 not count the newline posterior to which we stop. */
16075 return - orig_count - 1;
16078 else
16079 break;
16081 /* Here we add 1 to compensate for the last decrement
16082 of CURSOR, which took it past the valid range. */
16083 start_byte += cursor - base + 1;
16087 *byte_pos_ptr = limit_byte;
16089 if (count < 0)
16090 return - orig_count + count;
16091 return orig_count - count;
16097 /***********************************************************************
16098 Displaying strings
16099 ***********************************************************************/
16101 /* Display a NUL-terminated string, starting with index START.
16103 If STRING is non-null, display that C string. Otherwise, the Lisp
16104 string LISP_STRING is displayed.
16106 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16107 FACE_STRING. Display STRING or LISP_STRING with the face at
16108 FACE_STRING_POS in FACE_STRING:
16110 Display the string in the environment given by IT, but use the
16111 standard display table, temporarily.
16113 FIELD_WIDTH is the minimum number of output glyphs to produce.
16114 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16115 with spaces. If STRING has more characters, more than FIELD_WIDTH
16116 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16118 PRECISION is the maximum number of characters to output from
16119 STRING. PRECISION < 0 means don't truncate the string.
16121 This is roughly equivalent to printf format specifiers:
16123 FIELD_WIDTH PRECISION PRINTF
16124 ----------------------------------------
16125 -1 -1 %s
16126 -1 10 %.10s
16127 10 -1 %10s
16128 20 10 %20.10s
16130 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16131 display them, and < 0 means obey the current buffer's value of
16132 enable_multibyte_characters.
16134 Value is the number of glyphs produced. */
16136 static int
16137 display_string (string, lisp_string, face_string, face_string_pos,
16138 start, it, field_width, precision, max_x, multibyte)
16139 unsigned char *string;
16140 Lisp_Object lisp_string;
16141 Lisp_Object face_string;
16142 int face_string_pos;
16143 int start;
16144 struct it *it;
16145 int field_width, precision, max_x;
16146 int multibyte;
16148 int hpos_at_start = it->hpos;
16149 int saved_face_id = it->face_id;
16150 struct glyph_row *row = it->glyph_row;
16152 /* Initialize the iterator IT for iteration over STRING beginning
16153 with index START. */
16154 reseat_to_string (it, string, lisp_string, start,
16155 precision, field_width, multibyte);
16157 /* If displaying STRING, set up the face of the iterator
16158 from LISP_STRING, if that's given. */
16159 if (STRINGP (face_string))
16161 int endptr;
16162 struct face *face;
16164 it->face_id
16165 = face_at_string_position (it->w, face_string, face_string_pos,
16166 0, it->region_beg_charpos,
16167 it->region_end_charpos,
16168 &endptr, it->base_face_id, 0);
16169 face = FACE_FROM_ID (it->f, it->face_id);
16170 it->face_box_p = face->box != FACE_NO_BOX;
16173 /* Set max_x to the maximum allowed X position. Don't let it go
16174 beyond the right edge of the window. */
16175 if (max_x <= 0)
16176 max_x = it->last_visible_x;
16177 else
16178 max_x = min (max_x, it->last_visible_x);
16180 /* Skip over display elements that are not visible. because IT->w is
16181 hscrolled. */
16182 if (it->current_x < it->first_visible_x)
16183 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16184 MOVE_TO_POS | MOVE_TO_X);
16186 row->ascent = it->max_ascent;
16187 row->height = it->max_ascent + it->max_descent;
16188 row->phys_ascent = it->max_phys_ascent;
16189 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16191 /* This condition is for the case that we are called with current_x
16192 past last_visible_x. */
16193 while (it->current_x < max_x)
16195 int x_before, x, n_glyphs_before, i, nglyphs;
16197 /* Get the next display element. */
16198 if (!get_next_display_element (it))
16199 break;
16201 /* Produce glyphs. */
16202 x_before = it->current_x;
16203 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16204 PRODUCE_GLYPHS (it);
16206 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16207 i = 0;
16208 x = x_before;
16209 while (i < nglyphs)
16211 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16213 if (!it->truncate_lines_p
16214 && x + glyph->pixel_width > max_x)
16216 /* End of continued line or max_x reached. */
16217 if (CHAR_GLYPH_PADDING_P (*glyph))
16219 /* A wide character is unbreakable. */
16220 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16221 it->current_x = x_before;
16223 else
16225 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16226 it->current_x = x;
16228 break;
16230 else if (x + glyph->pixel_width > it->first_visible_x)
16232 /* Glyph is at least partially visible. */
16233 ++it->hpos;
16234 if (x < it->first_visible_x)
16235 it->glyph_row->x = x - it->first_visible_x;
16237 else
16239 /* Glyph is off the left margin of the display area.
16240 Should not happen. */
16241 abort ();
16244 row->ascent = max (row->ascent, it->max_ascent);
16245 row->height = max (row->height, it->max_ascent + it->max_descent);
16246 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16247 row->phys_height = max (row->phys_height,
16248 it->max_phys_ascent + it->max_phys_descent);
16249 x += glyph->pixel_width;
16250 ++i;
16253 /* Stop if max_x reached. */
16254 if (i < nglyphs)
16255 break;
16257 /* Stop at line ends. */
16258 if (ITERATOR_AT_END_OF_LINE_P (it))
16260 it->continuation_lines_width = 0;
16261 break;
16264 set_iterator_to_next (it, 1);
16266 /* Stop if truncating at the right edge. */
16267 if (it->truncate_lines_p
16268 && it->current_x >= it->last_visible_x)
16270 /* Add truncation mark, but don't do it if the line is
16271 truncated at a padding space. */
16272 if (IT_CHARPOS (*it) < it->string_nchars)
16274 if (!FRAME_WINDOW_P (it->f))
16276 int i, n;
16278 if (it->current_x > it->last_visible_x)
16280 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16281 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16282 break;
16283 for (n = row->used[TEXT_AREA]; i < n; ++i)
16285 row->used[TEXT_AREA] = i;
16286 produce_special_glyphs (it, IT_TRUNCATION);
16289 produce_special_glyphs (it, IT_TRUNCATION);
16291 it->glyph_row->truncated_on_right_p = 1;
16293 break;
16297 /* Maybe insert a truncation at the left. */
16298 if (it->first_visible_x
16299 && IT_CHARPOS (*it) > 0)
16301 if (!FRAME_WINDOW_P (it->f))
16302 insert_left_trunc_glyphs (it);
16303 it->glyph_row->truncated_on_left_p = 1;
16306 it->face_id = saved_face_id;
16308 /* Value is number of columns displayed. */
16309 return it->hpos - hpos_at_start;
16314 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16315 appears as an element of LIST or as the car of an element of LIST.
16316 If PROPVAL is a list, compare each element against LIST in that
16317 way, and return 1/2 if any element of PROPVAL is found in LIST.
16318 Otherwise return 0. This function cannot quit.
16319 The return value is 2 if the text is invisible but with an ellipsis
16320 and 1 if it's invisible and without an ellipsis. */
16323 invisible_p (propval, list)
16324 register Lisp_Object propval;
16325 Lisp_Object list;
16327 register Lisp_Object tail, proptail;
16329 for (tail = list; CONSP (tail); tail = XCDR (tail))
16331 register Lisp_Object tem;
16332 tem = XCAR (tail);
16333 if (EQ (propval, tem))
16334 return 1;
16335 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16336 return NILP (XCDR (tem)) ? 1 : 2;
16339 if (CONSP (propval))
16341 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16343 Lisp_Object propelt;
16344 propelt = XCAR (proptail);
16345 for (tail = list; CONSP (tail); tail = XCDR (tail))
16347 register Lisp_Object tem;
16348 tem = XCAR (tail);
16349 if (EQ (propelt, tem))
16350 return 1;
16351 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16352 return NILP (XCDR (tem)) ? 1 : 2;
16357 return 0;
16361 /***********************************************************************
16362 Glyph Display
16363 ***********************************************************************/
16365 #ifdef HAVE_WINDOW_SYSTEM
16367 #if GLYPH_DEBUG
16369 void
16370 dump_glyph_string (s)
16371 struct glyph_string *s;
16373 fprintf (stderr, "glyph string\n");
16374 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16375 s->x, s->y, s->width, s->height);
16376 fprintf (stderr, " ybase = %d\n", s->ybase);
16377 fprintf (stderr, " hl = %d\n", s->hl);
16378 fprintf (stderr, " left overhang = %d, right = %d\n",
16379 s->left_overhang, s->right_overhang);
16380 fprintf (stderr, " nchars = %d\n", s->nchars);
16381 fprintf (stderr, " extends to end of line = %d\n",
16382 s->extends_to_end_of_line_p);
16383 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16384 fprintf (stderr, " bg width = %d\n", s->background_width);
16387 #endif /* GLYPH_DEBUG */
16389 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16390 of XChar2b structures for S; it can't be allocated in
16391 init_glyph_string because it must be allocated via `alloca'. W
16392 is the window on which S is drawn. ROW and AREA are the glyph row
16393 and area within the row from which S is constructed. START is the
16394 index of the first glyph structure covered by S. HL is a
16395 face-override for drawing S. */
16397 #ifdef HAVE_NTGUI
16398 #define OPTIONAL_HDC(hdc) hdc,
16399 #define DECLARE_HDC(hdc) HDC hdc;
16400 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16401 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16402 #endif
16404 #ifndef OPTIONAL_HDC
16405 #define OPTIONAL_HDC(hdc)
16406 #define DECLARE_HDC(hdc)
16407 #define ALLOCATE_HDC(hdc, f)
16408 #define RELEASE_HDC(hdc, f)
16409 #endif
16411 static void
16412 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16413 struct glyph_string *s;
16414 DECLARE_HDC (hdc)
16415 XChar2b *char2b;
16416 struct window *w;
16417 struct glyph_row *row;
16418 enum glyph_row_area area;
16419 int start;
16420 enum draw_glyphs_face hl;
16422 bzero (s, sizeof *s);
16423 s->w = w;
16424 s->f = XFRAME (w->frame);
16425 #ifdef HAVE_NTGUI
16426 s->hdc = hdc;
16427 #endif
16428 s->display = FRAME_X_DISPLAY (s->f);
16429 s->window = FRAME_X_WINDOW (s->f);
16430 s->char2b = char2b;
16431 s->hl = hl;
16432 s->row = row;
16433 s->area = area;
16434 s->first_glyph = row->glyphs[area] + start;
16435 s->height = row->height;
16436 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16438 /* Display the internal border below the tool-bar window. */
16439 if (s->w == XWINDOW (s->f->tool_bar_window))
16440 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16442 s->ybase = s->y + row->ascent;
16446 /* Append the list of glyph strings with head H and tail T to the list
16447 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16449 static INLINE void
16450 append_glyph_string_lists (head, tail, h, t)
16451 struct glyph_string **head, **tail;
16452 struct glyph_string *h, *t;
16454 if (h)
16456 if (*head)
16457 (*tail)->next = h;
16458 else
16459 *head = h;
16460 h->prev = *tail;
16461 *tail = t;
16466 /* Prepend the list of glyph strings with head H and tail T to the
16467 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16468 result. */
16470 static INLINE void
16471 prepend_glyph_string_lists (head, tail, h, t)
16472 struct glyph_string **head, **tail;
16473 struct glyph_string *h, *t;
16475 if (h)
16477 if (*head)
16478 (*head)->prev = t;
16479 else
16480 *tail = t;
16481 t->next = *head;
16482 *head = h;
16487 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16488 Set *HEAD and *TAIL to the resulting list. */
16490 static INLINE void
16491 append_glyph_string (head, tail, s)
16492 struct glyph_string **head, **tail;
16493 struct glyph_string *s;
16495 s->next = s->prev = NULL;
16496 append_glyph_string_lists (head, tail, s, s);
16500 /* Get face and two-byte form of character glyph GLYPH on frame F.
16501 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16502 a pointer to a realized face that is ready for display. */
16504 static INLINE struct face *
16505 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16506 struct frame *f;
16507 struct glyph *glyph;
16508 XChar2b *char2b;
16509 int *two_byte_p;
16511 struct face *face;
16513 xassert (glyph->type == CHAR_GLYPH);
16514 face = FACE_FROM_ID (f, glyph->face_id);
16516 if (two_byte_p)
16517 *two_byte_p = 0;
16519 if (!glyph->multibyte_p)
16521 /* Unibyte case. We don't have to encode, but we have to make
16522 sure to use a face suitable for unibyte. */
16523 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16525 else if (glyph->u.ch < 128
16526 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16528 /* Case of ASCII in a face known to fit ASCII. */
16529 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16531 else
16533 int c1, c2, charset;
16535 /* Split characters into bytes. If c2 is -1 afterwards, C is
16536 really a one-byte character so that byte1 is zero. */
16537 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16538 if (c2 > 0)
16539 STORE_XCHAR2B (char2b, c1, c2);
16540 else
16541 STORE_XCHAR2B (char2b, 0, c1);
16543 /* Maybe encode the character in *CHAR2B. */
16544 if (charset != CHARSET_ASCII)
16546 struct font_info *font_info
16547 = FONT_INFO_FROM_ID (f, face->font_info_id);
16548 if (font_info)
16549 glyph->font_type
16550 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16554 /* Make sure X resources of the face are allocated. */
16555 xassert (face != NULL);
16556 PREPARE_FACE_FOR_DISPLAY (f, face);
16557 return face;
16561 /* Fill glyph string S with composition components specified by S->cmp.
16563 FACES is an array of faces for all components of this composition.
16564 S->gidx is the index of the first component for S.
16565 OVERLAPS_P non-zero means S should draw the foreground only, and
16566 use its physical height for clipping.
16568 Value is the index of a component not in S. */
16570 static int
16571 fill_composite_glyph_string (s, faces, overlaps_p)
16572 struct glyph_string *s;
16573 struct face **faces;
16574 int overlaps_p;
16576 int i;
16578 xassert (s);
16580 s->for_overlaps_p = overlaps_p;
16582 s->face = faces[s->gidx];
16583 s->font = s->face->font;
16584 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16586 /* For all glyphs of this composition, starting at the offset
16587 S->gidx, until we reach the end of the definition or encounter a
16588 glyph that requires the different face, add it to S. */
16589 ++s->nchars;
16590 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16591 ++s->nchars;
16593 /* All glyph strings for the same composition has the same width,
16594 i.e. the width set for the first component of the composition. */
16596 s->width = s->first_glyph->pixel_width;
16598 /* If the specified font could not be loaded, use the frame's
16599 default font, but record the fact that we couldn't load it in
16600 the glyph string so that we can draw rectangles for the
16601 characters of the glyph string. */
16602 if (s->font == NULL)
16604 s->font_not_found_p = 1;
16605 s->font = FRAME_FONT (s->f);
16608 /* Adjust base line for subscript/superscript text. */
16609 s->ybase += s->first_glyph->voffset;
16611 xassert (s->face && s->face->gc);
16613 /* This glyph string must always be drawn with 16-bit functions. */
16614 s->two_byte_p = 1;
16616 return s->gidx + s->nchars;
16620 /* Fill glyph string S from a sequence of character glyphs.
16622 FACE_ID is the face id of the string. START is the index of the
16623 first glyph to consider, END is the index of the last + 1.
16624 OVERLAPS_P non-zero means S should draw the foreground only, and
16625 use its physical height for clipping.
16627 Value is the index of the first glyph not in S. */
16629 static int
16630 fill_glyph_string (s, face_id, start, end, overlaps_p)
16631 struct glyph_string *s;
16632 int face_id;
16633 int start, end, overlaps_p;
16635 struct glyph *glyph, *last;
16636 int voffset;
16637 int glyph_not_available_p;
16639 xassert (s->f == XFRAME (s->w->frame));
16640 xassert (s->nchars == 0);
16641 xassert (start >= 0 && end > start);
16643 s->for_overlaps_p = overlaps_p,
16644 glyph = s->row->glyphs[s->area] + start;
16645 last = s->row->glyphs[s->area] + end;
16646 voffset = glyph->voffset;
16648 glyph_not_available_p = glyph->glyph_not_available_p;
16650 while (glyph < last
16651 && glyph->type == CHAR_GLYPH
16652 && glyph->voffset == voffset
16653 /* Same face id implies same font, nowadays. */
16654 && glyph->face_id == face_id
16655 && glyph->glyph_not_available_p == glyph_not_available_p)
16657 int two_byte_p;
16659 s->face = get_glyph_face_and_encoding (s->f, glyph,
16660 s->char2b + s->nchars,
16661 &two_byte_p);
16662 s->two_byte_p = two_byte_p;
16663 ++s->nchars;
16664 xassert (s->nchars <= end - start);
16665 s->width += glyph->pixel_width;
16666 ++glyph;
16669 s->font = s->face->font;
16670 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16672 /* If the specified font could not be loaded, use the frame's font,
16673 but record the fact that we couldn't load it in
16674 S->font_not_found_p so that we can draw rectangles for the
16675 characters of the glyph string. */
16676 if (s->font == NULL || glyph_not_available_p)
16678 s->font_not_found_p = 1;
16679 s->font = FRAME_FONT (s->f);
16682 /* Adjust base line for subscript/superscript text. */
16683 s->ybase += voffset;
16685 xassert (s->face && s->face->gc);
16686 return glyph - s->row->glyphs[s->area];
16690 /* Fill glyph string S from image glyph S->first_glyph. */
16692 static void
16693 fill_image_glyph_string (s)
16694 struct glyph_string *s;
16696 xassert (s->first_glyph->type == IMAGE_GLYPH);
16697 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16698 xassert (s->img);
16699 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16700 s->font = s->face->font;
16701 s->width = s->first_glyph->pixel_width;
16703 /* Adjust base line for subscript/superscript text. */
16704 s->ybase += s->first_glyph->voffset;
16708 /* Fill glyph string S from a sequence of stretch glyphs.
16710 ROW is the glyph row in which the glyphs are found, AREA is the
16711 area within the row. START is the index of the first glyph to
16712 consider, END is the index of the last + 1.
16714 Value is the index of the first glyph not in S. */
16716 static int
16717 fill_stretch_glyph_string (s, row, area, start, end)
16718 struct glyph_string *s;
16719 struct glyph_row *row;
16720 enum glyph_row_area area;
16721 int start, end;
16723 struct glyph *glyph, *last;
16724 int voffset, face_id;
16726 xassert (s->first_glyph->type == STRETCH_GLYPH);
16728 glyph = s->row->glyphs[s->area] + start;
16729 last = s->row->glyphs[s->area] + end;
16730 face_id = glyph->face_id;
16731 s->face = FACE_FROM_ID (s->f, face_id);
16732 s->font = s->face->font;
16733 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16734 s->width = glyph->pixel_width;
16735 voffset = glyph->voffset;
16737 for (++glyph;
16738 (glyph < last
16739 && glyph->type == STRETCH_GLYPH
16740 && glyph->voffset == voffset
16741 && glyph->face_id == face_id);
16742 ++glyph)
16743 s->width += glyph->pixel_width;
16745 /* Adjust base line for subscript/superscript text. */
16746 s->ybase += voffset;
16748 /* The case that face->gc == 0 is handled when drawing the glyph
16749 string by calling PREPARE_FACE_FOR_DISPLAY. */
16750 xassert (s->face);
16751 return glyph - s->row->glyphs[s->area];
16755 /* EXPORT for RIF:
16756 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16757 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16758 assumed to be zero. */
16760 void
16761 x_get_glyph_overhangs (glyph, f, left, right)
16762 struct glyph *glyph;
16763 struct frame *f;
16764 int *left, *right;
16766 *left = *right = 0;
16768 if (glyph->type == CHAR_GLYPH)
16770 XFontStruct *font;
16771 struct face *face;
16772 struct font_info *font_info;
16773 XChar2b char2b;
16774 XCharStruct *pcm;
16776 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16777 font = face->font;
16778 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16779 if (font /* ++KFS: Should this be font_info ? */
16780 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16782 if (pcm->rbearing > pcm->width)
16783 *right = pcm->rbearing - pcm->width;
16784 if (pcm->lbearing < 0)
16785 *left = -pcm->lbearing;
16791 /* Return the index of the first glyph preceding glyph string S that
16792 is overwritten by S because of S's left overhang. Value is -1
16793 if no glyphs are overwritten. */
16795 static int
16796 left_overwritten (s)
16797 struct glyph_string *s;
16799 int k;
16801 if (s->left_overhang)
16803 int x = 0, i;
16804 struct glyph *glyphs = s->row->glyphs[s->area];
16805 int first = s->first_glyph - glyphs;
16807 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16808 x -= glyphs[i].pixel_width;
16810 k = i + 1;
16812 else
16813 k = -1;
16815 return k;
16819 /* Return the index of the first glyph preceding glyph string S that
16820 is overwriting S because of its right overhang. Value is -1 if no
16821 glyph in front of S overwrites S. */
16823 static int
16824 left_overwriting (s)
16825 struct glyph_string *s;
16827 int i, k, x;
16828 struct glyph *glyphs = s->row->glyphs[s->area];
16829 int first = s->first_glyph - glyphs;
16831 k = -1;
16832 x = 0;
16833 for (i = first - 1; i >= 0; --i)
16835 int left, right;
16836 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16837 if (x + right > 0)
16838 k = i;
16839 x -= glyphs[i].pixel_width;
16842 return k;
16846 /* Return the index of the last glyph following glyph string S that is
16847 not overwritten by S because of S's right overhang. Value is -1 if
16848 no such glyph is found. */
16850 static int
16851 right_overwritten (s)
16852 struct glyph_string *s;
16854 int k = -1;
16856 if (s->right_overhang)
16858 int x = 0, i;
16859 struct glyph *glyphs = s->row->glyphs[s->area];
16860 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16861 int end = s->row->used[s->area];
16863 for (i = first; i < end && s->right_overhang > x; ++i)
16864 x += glyphs[i].pixel_width;
16866 k = i;
16869 return k;
16873 /* Return the index of the last glyph following glyph string S that
16874 overwrites S because of its left overhang. Value is negative
16875 if no such glyph is found. */
16877 static int
16878 right_overwriting (s)
16879 struct glyph_string *s;
16881 int i, k, x;
16882 int end = s->row->used[s->area];
16883 struct glyph *glyphs = s->row->glyphs[s->area];
16884 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16886 k = -1;
16887 x = 0;
16888 for (i = first; i < end; ++i)
16890 int left, right;
16891 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16892 if (x - left < 0)
16893 k = i;
16894 x += glyphs[i].pixel_width;
16897 return k;
16901 /* Get face and two-byte form of character C in face FACE_ID on frame
16902 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16903 means we want to display multibyte text. DISPLAY_P non-zero means
16904 make sure that X resources for the face returned are allocated.
16905 Value is a pointer to a realized face that is ready for display if
16906 DISPLAY_P is non-zero. */
16908 static INLINE struct face *
16909 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16910 struct frame *f;
16911 int c, face_id;
16912 XChar2b *char2b;
16913 int multibyte_p, display_p;
16915 struct face *face = FACE_FROM_ID (f, face_id);
16917 if (!multibyte_p)
16919 /* Unibyte case. We don't have to encode, but we have to make
16920 sure to use a face suitable for unibyte. */
16921 STORE_XCHAR2B (char2b, 0, c);
16922 face_id = FACE_FOR_CHAR (f, face, c);
16923 face = FACE_FROM_ID (f, face_id);
16925 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16927 /* Case of ASCII in a face known to fit ASCII. */
16928 STORE_XCHAR2B (char2b, 0, c);
16930 else
16932 int c1, c2, charset;
16934 /* Split characters into bytes. If c2 is -1 afterwards, C is
16935 really a one-byte character so that byte1 is zero. */
16936 SPLIT_CHAR (c, charset, c1, c2);
16937 if (c2 > 0)
16938 STORE_XCHAR2B (char2b, c1, c2);
16939 else
16940 STORE_XCHAR2B (char2b, 0, c1);
16942 /* Maybe encode the character in *CHAR2B. */
16943 if (face->font != NULL)
16945 struct font_info *font_info
16946 = FONT_INFO_FROM_ID (f, face->font_info_id);
16947 if (font_info)
16948 rif->encode_char (c, char2b, font_info, 0);
16952 /* Make sure X resources of the face are allocated. */
16953 #ifdef HAVE_X_WINDOWS
16954 if (display_p)
16955 #endif
16957 xassert (face != NULL);
16958 PREPARE_FACE_FOR_DISPLAY (f, face);
16961 return face;
16965 /* Set background width of glyph string S. START is the index of the
16966 first glyph following S. LAST_X is the right-most x-position + 1
16967 in the drawing area. */
16969 static INLINE void
16970 set_glyph_string_background_width (s, start, last_x)
16971 struct glyph_string *s;
16972 int start;
16973 int last_x;
16975 /* If the face of this glyph string has to be drawn to the end of
16976 the drawing area, set S->extends_to_end_of_line_p. */
16977 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
16979 if (start == s->row->used[s->area]
16980 && s->area == TEXT_AREA
16981 && ((s->hl == DRAW_NORMAL_TEXT
16982 && (s->row->fill_line_p
16983 || s->face->background != default_face->background
16984 || s->face->stipple != default_face->stipple
16985 || s->row->mouse_face_p))
16986 || s->hl == DRAW_MOUSE_FACE
16987 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
16988 && s->row->fill_line_p)))
16989 s->extends_to_end_of_line_p = 1;
16991 /* If S extends its face to the end of the line, set its
16992 background_width to the distance to the right edge of the drawing
16993 area. */
16994 if (s->extends_to_end_of_line_p)
16995 s->background_width = last_x - s->x + 1;
16996 else
16997 s->background_width = s->width;
17001 /* Compute overhangs and x-positions for glyph string S and its
17002 predecessors, or successors. X is the starting x-position for S.
17003 BACKWARD_P non-zero means process predecessors. */
17005 static void
17006 compute_overhangs_and_x (s, x, backward_p)
17007 struct glyph_string *s;
17008 int x;
17009 int backward_p;
17011 if (backward_p)
17013 while (s)
17015 if (rif->compute_glyph_string_overhangs)
17016 rif->compute_glyph_string_overhangs (s);
17017 x -= s->width;
17018 s->x = x;
17019 s = s->prev;
17022 else
17024 while (s)
17026 if (rif->compute_glyph_string_overhangs)
17027 rif->compute_glyph_string_overhangs (s);
17028 s->x = x;
17029 x += s->width;
17030 s = s->next;
17037 /* The following macros are only called from draw_glyphs below.
17038 They reference the following parameters of that function directly:
17039 `w', `row', `area', and `overlap_p'
17040 as well as the following local variables:
17041 `s', `f', and `hdc' (in W32) */
17043 #ifdef HAVE_NTGUI
17044 /* On W32, silently add local `hdc' variable to argument list of
17045 init_glyph_string. */
17046 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17047 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17048 #else
17049 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17050 init_glyph_string (s, char2b, w, row, area, start, hl)
17051 #endif
17053 /* Add a glyph string for a stretch glyph to the list of strings
17054 between HEAD and TAIL. START is the index of the stretch glyph in
17055 row area AREA of glyph row ROW. END is the index of the last glyph
17056 in that glyph row area. X is the current output position assigned
17057 to the new glyph string constructed. HL overrides that face of the
17058 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17059 is the right-most x-position of the drawing area. */
17061 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17062 and below -- keep them on one line. */
17063 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17064 do \
17066 s = (struct glyph_string *) alloca (sizeof *s); \
17067 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17068 START = fill_stretch_glyph_string (s, row, area, START, END); \
17069 append_glyph_string (&HEAD, &TAIL, s); \
17070 s->x = (X); \
17072 while (0)
17075 /* Add a glyph string for an image glyph to the list of strings
17076 between HEAD and TAIL. START is the index of the image glyph in
17077 row area AREA of glyph row ROW. END is the index of the last glyph
17078 in that glyph row area. X is the current output position assigned
17079 to the new glyph string constructed. HL overrides that face of the
17080 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17081 is the right-most x-position of the drawing area. */
17083 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17084 do \
17086 s = (struct glyph_string *) alloca (sizeof *s); \
17087 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17088 fill_image_glyph_string (s); \
17089 append_glyph_string (&HEAD, &TAIL, s); \
17090 ++START; \
17091 s->x = (X); \
17093 while (0)
17096 /* Add a glyph string for a sequence of character glyphs to the list
17097 of strings between HEAD and TAIL. START is the index of the first
17098 glyph in row area AREA of glyph row ROW that is part of the new
17099 glyph string. END is the index of the last glyph in that glyph row
17100 area. X is the current output position assigned to the new glyph
17101 string constructed. HL overrides that face of the glyph; e.g. it
17102 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17103 right-most x-position of the drawing area. */
17105 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17106 do \
17108 int c, face_id; \
17109 XChar2b *char2b; \
17111 c = (row)->glyphs[area][START].u.ch; \
17112 face_id = (row)->glyphs[area][START].face_id; \
17114 s = (struct glyph_string *) alloca (sizeof *s); \
17115 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17116 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17117 append_glyph_string (&HEAD, &TAIL, s); \
17118 s->x = (X); \
17119 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17121 while (0)
17124 /* Add a glyph string for a composite sequence to the list of strings
17125 between HEAD and TAIL. START is the index of the first glyph in
17126 row area AREA of glyph row ROW that is part of the new glyph
17127 string. END is the index of the last glyph in that glyph row area.
17128 X is the current output position assigned to the new glyph string
17129 constructed. HL overrides that face of the glyph; e.g. it is
17130 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17131 x-position of the drawing area. */
17133 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17134 do { \
17135 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17136 int face_id = (row)->glyphs[area][START].face_id; \
17137 struct face *base_face = FACE_FROM_ID (f, face_id); \
17138 struct composition *cmp = composition_table[cmp_id]; \
17139 int glyph_len = cmp->glyph_len; \
17140 XChar2b *char2b; \
17141 struct face **faces; \
17142 struct glyph_string *first_s = NULL; \
17143 int n; \
17145 base_face = base_face->ascii_face; \
17146 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17147 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17148 /* At first, fill in `char2b' and `faces'. */ \
17149 for (n = 0; n < glyph_len; n++) \
17151 int c = COMPOSITION_GLYPH (cmp, n); \
17152 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17153 faces[n] = FACE_FROM_ID (f, this_face_id); \
17154 get_char_face_and_encoding (f, c, this_face_id, \
17155 char2b + n, 1, 1); \
17158 /* Make glyph_strings for each glyph sequence that is drawable by \
17159 the same face, and append them to HEAD/TAIL. */ \
17160 for (n = 0; n < cmp->glyph_len;) \
17162 s = (struct glyph_string *) alloca (sizeof *s); \
17163 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17164 append_glyph_string (&(HEAD), &(TAIL), s); \
17165 s->cmp = cmp; \
17166 s->gidx = n; \
17167 s->x = (X); \
17169 if (n == 0) \
17170 first_s = s; \
17172 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17175 ++START; \
17176 s = first_s; \
17177 } while (0)
17180 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17181 of AREA of glyph row ROW on window W between indices START and END.
17182 HL overrides the face for drawing glyph strings, e.g. it is
17183 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17184 x-positions of the drawing area.
17186 This is an ugly monster macro construct because we must use alloca
17187 to allocate glyph strings (because draw_glyphs can be called
17188 asynchronously). */
17190 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17191 do \
17193 HEAD = TAIL = NULL; \
17194 while (START < END) \
17196 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17197 switch (first_glyph->type) \
17199 case CHAR_GLYPH: \
17200 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17201 HL, X, LAST_X); \
17202 break; \
17204 case COMPOSITE_GLYPH: \
17205 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17206 HL, X, LAST_X); \
17207 break; \
17209 case STRETCH_GLYPH: \
17210 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17211 HL, X, LAST_X); \
17212 break; \
17214 case IMAGE_GLYPH: \
17215 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17216 HL, X, LAST_X); \
17217 break; \
17219 default: \
17220 abort (); \
17223 set_glyph_string_background_width (s, START, LAST_X); \
17224 (X) += s->width; \
17227 while (0)
17230 /* Draw glyphs between START and END in AREA of ROW on window W,
17231 starting at x-position X. X is relative to AREA in W. HL is a
17232 face-override with the following meaning:
17234 DRAW_NORMAL_TEXT draw normally
17235 DRAW_CURSOR draw in cursor face
17236 DRAW_MOUSE_FACE draw in mouse face.
17237 DRAW_INVERSE_VIDEO draw in mode line face
17238 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17239 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17241 If OVERLAPS_P is non-zero, draw only the foreground of characters
17242 and clip to the physical height of ROW.
17244 Value is the x-position reached, relative to AREA of W. */
17246 static int
17247 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17248 struct window *w;
17249 int x;
17250 struct glyph_row *row;
17251 enum glyph_row_area area;
17252 int start, end;
17253 enum draw_glyphs_face hl;
17254 int overlaps_p;
17256 struct glyph_string *head, *tail;
17257 struct glyph_string *s;
17258 int last_x, area_width;
17259 int x_reached;
17260 int i, j;
17261 struct frame *f = XFRAME (WINDOW_FRAME (w));
17262 DECLARE_HDC (hdc);
17264 ALLOCATE_HDC (hdc, f);
17266 /* Let's rather be paranoid than getting a SEGV. */
17267 end = min (end, row->used[area]);
17268 start = max (0, start);
17269 start = min (end, start);
17271 /* Translate X to frame coordinates. Set last_x to the right
17272 end of the drawing area. */
17273 if (row->full_width_p)
17275 /* X is relative to the left edge of W, without scroll bars
17276 or fringes. */
17277 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
17279 x += window_left_x;
17280 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
17281 last_x = window_left_x + area_width;
17283 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
17285 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
17286 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
17287 last_x += width;
17288 else
17289 x -= width;
17292 x += FRAME_INTERNAL_BORDER_WIDTH (f);
17293 /* ++KFS: W32 and MAC versions had -= in next line (bug??) */
17294 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
17296 else
17298 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
17299 area_width = window_box_width (w, area);
17300 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
17303 /* Build a doubly-linked list of glyph_string structures between
17304 head and tail from what we have to draw. Note that the macro
17305 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17306 the reason we use a separate variable `i'. */
17307 i = start;
17308 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17309 if (tail)
17310 x_reached = tail->x + tail->background_width;
17311 else
17312 x_reached = x;
17314 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17315 the row, redraw some glyphs in front or following the glyph
17316 strings built above. */
17317 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17319 int dummy_x = 0;
17320 struct glyph_string *h, *t;
17322 /* Compute overhangs for all glyph strings. */
17323 if (rif->compute_glyph_string_overhangs)
17324 for (s = head; s; s = s->next)
17325 rif->compute_glyph_string_overhangs (s);
17327 /* Prepend glyph strings for glyphs in front of the first glyph
17328 string that are overwritten because of the first glyph
17329 string's left overhang. The background of all strings
17330 prepended must be drawn because the first glyph string
17331 draws over it. */
17332 i = left_overwritten (head);
17333 if (i >= 0)
17335 j = i;
17336 BUILD_GLYPH_STRINGS (j, start, h, t,
17337 DRAW_NORMAL_TEXT, dummy_x, last_x);
17338 start = i;
17339 compute_overhangs_and_x (t, head->x, 1);
17340 prepend_glyph_string_lists (&head, &tail, h, t);
17343 /* Prepend glyph strings for glyphs in front of the first glyph
17344 string that overwrite that glyph string because of their
17345 right overhang. For these strings, only the foreground must
17346 be drawn, because it draws over the glyph string at `head'.
17347 The background must not be drawn because this would overwrite
17348 right overhangs of preceding glyphs for which no glyph
17349 strings exist. */
17350 i = left_overwriting (head);
17351 if (i >= 0)
17353 BUILD_GLYPH_STRINGS (i, start, h, t,
17354 DRAW_NORMAL_TEXT, dummy_x, last_x);
17355 for (s = h; s; s = s->next)
17356 s->background_filled_p = 1;
17357 compute_overhangs_and_x (t, head->x, 1);
17358 prepend_glyph_string_lists (&head, &tail, h, t);
17361 /* Append glyphs strings for glyphs following the last glyph
17362 string tail that are overwritten by tail. The background of
17363 these strings has to be drawn because tail's foreground draws
17364 over it. */
17365 i = right_overwritten (tail);
17366 if (i >= 0)
17368 BUILD_GLYPH_STRINGS (end, i, h, t,
17369 DRAW_NORMAL_TEXT, x, last_x);
17370 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17371 append_glyph_string_lists (&head, &tail, h, t);
17374 /* Append glyph strings for glyphs following the last glyph
17375 string tail that overwrite tail. The foreground of such
17376 glyphs has to be drawn because it writes into the background
17377 of tail. The background must not be drawn because it could
17378 paint over the foreground of following glyphs. */
17379 i = right_overwriting (tail);
17380 if (i >= 0)
17382 BUILD_GLYPH_STRINGS (end, i, h, t,
17383 DRAW_NORMAL_TEXT, x, last_x);
17384 for (s = h; s; s = s->next)
17385 s->background_filled_p = 1;
17386 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17387 append_glyph_string_lists (&head, &tail, h, t);
17391 /* Draw all strings. */
17392 for (s = head; s; s = s->next)
17393 rif->draw_glyph_string (s);
17395 if (area == TEXT_AREA
17396 && !row->full_width_p
17397 /* When drawing overlapping rows, only the glyph strings'
17398 foreground is drawn, which doesn't erase a cursor
17399 completely. */
17400 && !overlaps_p)
17402 int x0 = head ? head->x : x;
17403 int x1 = tail ? tail->x + tail->background_width : x;
17405 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
17406 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
17408 /* ++KFS: W32 and MAC versions had following test here:
17409 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
17412 if (XFASTINT (w->left_margin_width) != 0)
17414 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
17415 x0 -= left_area_width;
17416 x1 -= left_area_width;
17419 notice_overwritten_cursor (w, area, x0, x1,
17420 row->y, MATRIX_ROW_BOTTOM_Y (row));
17423 /* Value is the x-position up to which drawn, relative to AREA of W.
17424 This doesn't include parts drawn because of overhangs. */
17425 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17426 if (!row->full_width_p)
17428 /* ++KFS: W32 and MAC versions only had this test here:
17429 if (area > LEFT_MARGIN_AREA)
17432 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
17433 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
17434 if (area > TEXT_AREA)
17435 x_reached -= window_box_width (w, TEXT_AREA);
17438 RELEASE_HDC (hdc, f);
17440 return x_reached;
17444 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17445 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17447 static INLINE void
17448 append_glyph (it)
17449 struct it *it;
17451 struct glyph *glyph;
17452 enum glyph_row_area area = it->area;
17454 xassert (it->glyph_row);
17455 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17457 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17458 if (glyph < it->glyph_row->glyphs[area + 1])
17460 glyph->charpos = CHARPOS (it->position);
17461 glyph->object = it->object;
17462 glyph->pixel_width = it->pixel_width;
17463 glyph->voffset = it->voffset;
17464 glyph->type = CHAR_GLYPH;
17465 glyph->multibyte_p = it->multibyte_p;
17466 glyph->left_box_line_p = it->start_of_box_run_p;
17467 glyph->right_box_line_p = it->end_of_box_run_p;
17468 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17469 || it->phys_descent > it->descent);
17470 glyph->padding_p = 0;
17471 glyph->glyph_not_available_p = it->glyph_not_available_p;
17472 glyph->face_id = it->face_id;
17473 glyph->u.ch = it->char_to_display;
17474 glyph->font_type = FONT_TYPE_UNKNOWN;
17475 ++it->glyph_row->used[area];
17479 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17480 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17482 static INLINE void
17483 append_composite_glyph (it)
17484 struct it *it;
17486 struct glyph *glyph;
17487 enum glyph_row_area area = it->area;
17489 xassert (it->glyph_row);
17491 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17492 if (glyph < it->glyph_row->glyphs[area + 1])
17494 glyph->charpos = CHARPOS (it->position);
17495 glyph->object = it->object;
17496 glyph->pixel_width = it->pixel_width;
17497 glyph->voffset = it->voffset;
17498 glyph->type = COMPOSITE_GLYPH;
17499 glyph->multibyte_p = it->multibyte_p;
17500 glyph->left_box_line_p = it->start_of_box_run_p;
17501 glyph->right_box_line_p = it->end_of_box_run_p;
17502 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17503 || it->phys_descent > it->descent);
17504 glyph->padding_p = 0;
17505 glyph->glyph_not_available_p = 0;
17506 glyph->face_id = it->face_id;
17507 glyph->u.cmp_id = it->cmp_id;
17508 glyph->font_type = FONT_TYPE_UNKNOWN;
17509 ++it->glyph_row->used[area];
17514 /* Change IT->ascent and IT->height according to the setting of
17515 IT->voffset. */
17517 static INLINE void
17518 take_vertical_position_into_account (it)
17519 struct it *it;
17521 if (it->voffset)
17523 if (it->voffset < 0)
17524 /* Increase the ascent so that we can display the text higher
17525 in the line. */
17526 it->ascent += abs (it->voffset);
17527 else
17528 /* Increase the descent so that we can display the text lower
17529 in the line. */
17530 it->descent += it->voffset;
17535 /* Produce glyphs/get display metrics for the image IT is loaded with.
17536 See the description of struct display_iterator in dispextern.h for
17537 an overview of struct display_iterator. */
17539 static void
17540 produce_image_glyph (it)
17541 struct it *it;
17543 struct image *img;
17544 struct face *face;
17546 xassert (it->what == IT_IMAGE);
17548 face = FACE_FROM_ID (it->f, it->face_id);
17549 img = IMAGE_FROM_ID (it->f, it->image_id);
17550 xassert (img);
17552 /* Make sure X resources of the face and image are loaded. */
17553 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17554 prepare_image_for_display (it->f, img);
17556 it->ascent = it->phys_ascent = image_ascent (img, face);
17557 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17558 it->pixel_width = img->width + 2 * img->hmargin;
17560 it->nglyphs = 1;
17562 if (face->box != FACE_NO_BOX)
17564 if (face->box_line_width > 0)
17566 it->ascent += face->box_line_width;
17567 it->descent += face->box_line_width;
17570 if (it->start_of_box_run_p)
17571 it->pixel_width += abs (face->box_line_width);
17572 if (it->end_of_box_run_p)
17573 it->pixel_width += abs (face->box_line_width);
17576 take_vertical_position_into_account (it);
17578 if (it->glyph_row)
17580 struct glyph *glyph;
17581 enum glyph_row_area area = it->area;
17583 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17584 if (glyph < it->glyph_row->glyphs[area + 1])
17586 glyph->charpos = CHARPOS (it->position);
17587 glyph->object = it->object;
17588 glyph->pixel_width = it->pixel_width;
17589 glyph->voffset = it->voffset;
17590 glyph->type = IMAGE_GLYPH;
17591 glyph->multibyte_p = it->multibyte_p;
17592 glyph->left_box_line_p = it->start_of_box_run_p;
17593 glyph->right_box_line_p = it->end_of_box_run_p;
17594 glyph->overlaps_vertically_p = 0;
17595 glyph->padding_p = 0;
17596 glyph->glyph_not_available_p = 0;
17597 glyph->face_id = it->face_id;
17598 glyph->u.img_id = img->id;
17599 glyph->font_type = FONT_TYPE_UNKNOWN;
17600 ++it->glyph_row->used[area];
17606 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17607 of the glyph, WIDTH and HEIGHT are the width and height of the
17608 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17609 ascent of the glyph (0 <= ASCENT <= 1). */
17611 static void
17612 append_stretch_glyph (it, object, width, height, ascent)
17613 struct it *it;
17614 Lisp_Object object;
17615 int width, height;
17616 double ascent;
17618 struct glyph *glyph;
17619 enum glyph_row_area area = it->area;
17621 xassert (ascent >= 0 && ascent <= 1);
17623 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17624 if (glyph < it->glyph_row->glyphs[area + 1])
17626 glyph->charpos = CHARPOS (it->position);
17627 glyph->object = object;
17628 glyph->pixel_width = width;
17629 glyph->voffset = it->voffset;
17630 glyph->type = STRETCH_GLYPH;
17631 glyph->multibyte_p = it->multibyte_p;
17632 glyph->left_box_line_p = it->start_of_box_run_p;
17633 glyph->right_box_line_p = it->end_of_box_run_p;
17634 glyph->overlaps_vertically_p = 0;
17635 glyph->padding_p = 0;
17636 glyph->glyph_not_available_p = 0;
17637 glyph->face_id = it->face_id;
17638 glyph->u.stretch.ascent = height * ascent;
17639 glyph->u.stretch.height = height;
17640 glyph->font_type = FONT_TYPE_UNKNOWN;
17641 ++it->glyph_row->used[area];
17646 /* Produce a stretch glyph for iterator IT. IT->object is the value
17647 of the glyph property displayed. The value must be a list
17648 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17649 being recognized:
17651 1. `:width WIDTH' specifies that the space should be WIDTH *
17652 canonical char width wide. WIDTH may be an integer or floating
17653 point number.
17655 2. `:relative-width FACTOR' specifies that the width of the stretch
17656 should be computed from the width of the first character having the
17657 `glyph' property, and should be FACTOR times that width.
17659 3. `:align-to HPOS' specifies that the space should be wide enough
17660 to reach HPOS, a value in canonical character units.
17662 Exactly one of the above pairs must be present.
17664 4. `:height HEIGHT' specifies that the height of the stretch produced
17665 should be HEIGHT, measured in canonical character units.
17667 5. `:relative-height FACTOR' specifies that the height of the
17668 stretch should be FACTOR times the height of the characters having
17669 the glyph property.
17671 Either none or exactly one of 4 or 5 must be present.
17673 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17674 of the stretch should be used for the ascent of the stretch.
17675 ASCENT must be in the range 0 <= ASCENT <= 100. */
17677 #define NUMVAL(X) \
17678 ((INTEGERP (X) || FLOATP (X)) \
17679 ? XFLOATINT (X) \
17680 : - 1)
17683 static void
17684 produce_stretch_glyph (it)
17685 struct it *it;
17687 /* (space :width WIDTH :height HEIGHT. */
17688 Lisp_Object prop, plist;
17689 int width = 0, height = 0;
17690 double ascent = 0;
17691 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17692 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17694 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17696 /* List should start with `space'. */
17697 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17698 plist = XCDR (it->object);
17700 /* Compute the width of the stretch. */
17701 if (prop = Fplist_get (plist, QCwidth),
17702 NUMVAL (prop) > 0)
17703 /* Absolute width `:width WIDTH' specified and valid. */
17704 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
17705 else if (prop = Fplist_get (plist, QCrelative_width),
17706 NUMVAL (prop) > 0)
17708 /* Relative width `:relative-width FACTOR' specified and valid.
17709 Compute the width of the characters having the `glyph'
17710 property. */
17711 struct it it2;
17712 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17714 it2 = *it;
17715 if (it->multibyte_p)
17717 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17718 - IT_BYTEPOS (*it));
17719 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17721 else
17722 it2.c = *p, it2.len = 1;
17724 it2.glyph_row = NULL;
17725 it2.what = IT_CHARACTER;
17726 x_produce_glyphs (&it2);
17727 width = NUMVAL (prop) * it2.pixel_width;
17729 else if (prop = Fplist_get (plist, QCalign_to),
17730 NUMVAL (prop) > 0)
17731 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
17732 else
17733 /* Nothing specified -> width defaults to canonical char width. */
17734 width = CANON_X_UNIT (it->f);
17736 /* Compute height. */
17737 if (prop = Fplist_get (plist, QCheight),
17738 NUMVAL (prop) > 0)
17739 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
17740 else if (prop = Fplist_get (plist, QCrelative_height),
17741 NUMVAL (prop) > 0)
17742 height = FONT_HEIGHT (font) * NUMVAL (prop);
17743 else
17744 height = FONT_HEIGHT (font);
17746 /* Compute percentage of height used for ascent. If
17747 `:ascent ASCENT' is present and valid, use that. Otherwise,
17748 derive the ascent from the font in use. */
17749 if (prop = Fplist_get (plist, QCascent),
17750 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17751 ascent = NUMVAL (prop) / 100.0;
17752 else
17753 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17755 if (width <= 0)
17756 width = 1;
17757 if (height <= 0)
17758 height = 1;
17760 if (it->glyph_row)
17762 Lisp_Object object = it->stack[it->sp - 1].string;
17763 if (!STRINGP (object))
17764 object = it->w->buffer;
17765 append_stretch_glyph (it, object, width, height, ascent);
17768 it->pixel_width = width;
17769 it->ascent = it->phys_ascent = height * ascent;
17770 it->descent = it->phys_descent = height - it->ascent;
17771 it->nglyphs = 1;
17773 if (face->box != FACE_NO_BOX)
17775 if (face->box_line_width > 0)
17777 it->ascent += face->box_line_width;
17778 it->descent += face->box_line_width;
17781 if (it->start_of_box_run_p)
17782 it->pixel_width += abs (face->box_line_width);
17783 if (it->end_of_box_run_p)
17784 it->pixel_width += abs (face->box_line_width);
17787 take_vertical_position_into_account (it);
17790 /* RIF:
17791 Produce glyphs/get display metrics for the display element IT is
17792 loaded with. See the description of struct display_iterator in
17793 dispextern.h for an overview of struct display_iterator. */
17795 void
17796 x_produce_glyphs (it)
17797 struct it *it;
17799 it->glyph_not_available_p = 0;
17801 if (it->what == IT_CHARACTER)
17803 XChar2b char2b;
17804 XFontStruct *font;
17805 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17806 XCharStruct *pcm;
17807 int font_not_found_p;
17808 struct font_info *font_info;
17809 int boff; /* baseline offset */
17810 /* We may change it->multibyte_p upon unibyte<->multibyte
17811 conversion. So, save the current value now and restore it
17812 later.
17814 Note: It seems that we don't have to record multibyte_p in
17815 struct glyph because the character code itself tells if or
17816 not the character is multibyte. Thus, in the future, we must
17817 consider eliminating the field `multibyte_p' in the struct
17818 glyph. */
17819 int saved_multibyte_p = it->multibyte_p;
17821 /* Maybe translate single-byte characters to multibyte, or the
17822 other way. */
17823 it->char_to_display = it->c;
17824 if (!ASCII_BYTE_P (it->c))
17826 if (unibyte_display_via_language_environment
17827 && SINGLE_BYTE_CHAR_P (it->c)
17828 && (it->c >= 0240
17829 || !NILP (Vnonascii_translation_table)))
17831 it->char_to_display = unibyte_char_to_multibyte (it->c);
17832 it->multibyte_p = 1;
17833 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17834 face = FACE_FROM_ID (it->f, it->face_id);
17836 else if (!SINGLE_BYTE_CHAR_P (it->c)
17837 && !it->multibyte_p)
17839 it->multibyte_p = 1;
17840 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17841 face = FACE_FROM_ID (it->f, it->face_id);
17845 /* Get font to use. Encode IT->char_to_display. */
17846 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17847 &char2b, it->multibyte_p, 0);
17848 font = face->font;
17850 /* When no suitable font found, use the default font. */
17851 font_not_found_p = font == NULL;
17852 if (font_not_found_p)
17854 font = FRAME_FONT (it->f);
17855 boff = FRAME_BASELINE_OFFSET (it->f);
17856 font_info = NULL;
17858 else
17860 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17861 boff = font_info->baseline_offset;
17862 if (font_info->vertical_centering)
17863 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17866 if (it->char_to_display >= ' '
17867 && (!it->multibyte_p || it->char_to_display < 128))
17869 /* Either unibyte or ASCII. */
17870 int stretched_p;
17872 it->nglyphs = 1;
17874 pcm = rif->per_char_metric (font, &char2b,
17875 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17876 it->ascent = FONT_BASE (font) + boff;
17877 it->descent = FONT_DESCENT (font) - boff;
17879 if (pcm)
17881 it->phys_ascent = pcm->ascent + boff;
17882 it->phys_descent = pcm->descent - boff;
17883 it->pixel_width = pcm->width;
17885 else
17887 it->glyph_not_available_p = 1;
17888 it->phys_ascent = FONT_BASE (font) + boff;
17889 it->phys_descent = FONT_DESCENT (font) - boff;
17890 it->pixel_width = FONT_WIDTH (font);
17893 /* If this is a space inside a region of text with
17894 `space-width' property, change its width. */
17895 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17896 if (stretched_p)
17897 it->pixel_width *= XFLOATINT (it->space_width);
17899 /* If face has a box, add the box thickness to the character
17900 height. If character has a box line to the left and/or
17901 right, add the box line width to the character's width. */
17902 if (face->box != FACE_NO_BOX)
17904 int thick = face->box_line_width;
17906 if (thick > 0)
17908 it->ascent += thick;
17909 it->descent += thick;
17911 else
17912 thick = -thick;
17914 if (it->start_of_box_run_p)
17915 it->pixel_width += thick;
17916 if (it->end_of_box_run_p)
17917 it->pixel_width += thick;
17920 /* If face has an overline, add the height of the overline
17921 (1 pixel) and a 1 pixel margin to the character height. */
17922 if (face->overline_p)
17923 it->ascent += 2;
17925 take_vertical_position_into_account (it);
17927 /* If we have to actually produce glyphs, do it. */
17928 if (it->glyph_row)
17930 if (stretched_p)
17932 /* Translate a space with a `space-width' property
17933 into a stretch glyph. */
17934 double ascent = (double) FONT_BASE (font)
17935 / FONT_HEIGHT (font);
17936 append_stretch_glyph (it, it->object, it->pixel_width,
17937 it->ascent + it->descent, ascent);
17939 else
17940 append_glyph (it);
17942 /* If characters with lbearing or rbearing are displayed
17943 in this line, record that fact in a flag of the
17944 glyph row. This is used to optimize X output code. */
17945 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17946 it->glyph_row->contains_overlapping_glyphs_p = 1;
17949 else if (it->char_to_display == '\n')
17951 /* A newline has no width but we need the height of the line. */
17952 it->pixel_width = 0;
17953 it->nglyphs = 0;
17954 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17955 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17957 if (face->box != FACE_NO_BOX
17958 && face->box_line_width > 0)
17960 it->ascent += face->box_line_width;
17961 it->descent += face->box_line_width;
17964 else if (it->char_to_display == '\t')
17966 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
17967 int x = it->current_x + it->continuation_lines_width;
17968 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17970 /* If the distance from the current position to the next tab
17971 stop is less than a canonical character width, use the
17972 tab stop after that. */
17973 if (next_tab_x - x < CANON_X_UNIT (it->f))
17974 next_tab_x += tab_width;
17976 it->pixel_width = next_tab_x - x;
17977 it->nglyphs = 1;
17978 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17979 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17981 if (it->glyph_row)
17983 double ascent = (double) it->ascent / (it->ascent + it->descent);
17984 append_stretch_glyph (it, it->object, it->pixel_width,
17985 it->ascent + it->descent, ascent);
17988 else
17990 /* A multi-byte character. Assume that the display width of the
17991 character is the width of the character multiplied by the
17992 width of the font. */
17994 /* If we found a font, this font should give us the right
17995 metrics. If we didn't find a font, use the frame's
17996 default font and calculate the width of the character
17997 from the charset width; this is what old redisplay code
17998 did. */
18000 pcm = rif->per_char_metric (font, &char2b,
18001 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18003 if (font_not_found_p || !pcm)
18005 int charset = CHAR_CHARSET (it->char_to_display);
18007 it->glyph_not_available_p = 1;
18008 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
18009 * CHARSET_WIDTH (charset));
18010 it->phys_ascent = FONT_BASE (font) + boff;
18011 it->phys_descent = FONT_DESCENT (font) - boff;
18013 else
18015 it->pixel_width = pcm->width;
18016 it->phys_ascent = pcm->ascent + boff;
18017 it->phys_descent = pcm->descent - boff;
18018 if (it->glyph_row
18019 && (pcm->lbearing < 0
18020 || pcm->rbearing > pcm->width))
18021 it->glyph_row->contains_overlapping_glyphs_p = 1;
18023 it->nglyphs = 1;
18024 it->ascent = FONT_BASE (font) + boff;
18025 it->descent = FONT_DESCENT (font) - boff;
18026 if (face->box != FACE_NO_BOX)
18028 int thick = face->box_line_width;
18030 if (thick > 0)
18032 it->ascent += thick;
18033 it->descent += thick;
18035 else
18036 thick = - thick;
18038 if (it->start_of_box_run_p)
18039 it->pixel_width += thick;
18040 if (it->end_of_box_run_p)
18041 it->pixel_width += thick;
18044 /* If face has an overline, add the height of the overline
18045 (1 pixel) and a 1 pixel margin to the character height. */
18046 if (face->overline_p)
18047 it->ascent += 2;
18049 take_vertical_position_into_account (it);
18051 if (it->glyph_row)
18052 append_glyph (it);
18054 it->multibyte_p = saved_multibyte_p;
18056 else if (it->what == IT_COMPOSITION)
18058 /* Note: A composition is represented as one glyph in the
18059 glyph matrix. There are no padding glyphs. */
18060 XChar2b char2b;
18061 XFontStruct *font;
18062 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18063 XCharStruct *pcm;
18064 int font_not_found_p;
18065 struct font_info *font_info;
18066 int boff; /* baseline offset */
18067 struct composition *cmp = composition_table[it->cmp_id];
18069 /* Maybe translate single-byte characters to multibyte. */
18070 it->char_to_display = it->c;
18071 if (unibyte_display_via_language_environment
18072 && SINGLE_BYTE_CHAR_P (it->c)
18073 && (it->c >= 0240
18074 || (it->c >= 0200
18075 && !NILP (Vnonascii_translation_table))))
18077 it->char_to_display = unibyte_char_to_multibyte (it->c);
18080 /* Get face and font to use. Encode IT->char_to_display. */
18081 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18082 face = FACE_FROM_ID (it->f, it->face_id);
18083 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18084 &char2b, it->multibyte_p, 0);
18085 font = face->font;
18087 /* When no suitable font found, use the default font. */
18088 font_not_found_p = font == NULL;
18089 if (font_not_found_p)
18091 font = FRAME_FONT (it->f);
18092 boff = FRAME_BASELINE_OFFSET (it->f);
18093 font_info = NULL;
18095 else
18097 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18098 boff = font_info->baseline_offset;
18099 if (font_info->vertical_centering)
18100 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18103 /* There are no padding glyphs, so there is only one glyph to
18104 produce for the composition. Important is that pixel_width,
18105 ascent and descent are the values of what is drawn by
18106 draw_glyphs (i.e. the values of the overall glyphs composed). */
18107 it->nglyphs = 1;
18109 /* If we have not yet calculated pixel size data of glyphs of
18110 the composition for the current face font, calculate them
18111 now. Theoretically, we have to check all fonts for the
18112 glyphs, but that requires much time and memory space. So,
18113 here we check only the font of the first glyph. This leads
18114 to incorrect display very rarely, and C-l (recenter) can
18115 correct the display anyway. */
18116 if (cmp->font != (void *) font)
18118 /* Ascent and descent of the font of the first character of
18119 this composition (adjusted by baseline offset). Ascent
18120 and descent of overall glyphs should not be less than
18121 them respectively. */
18122 int font_ascent = FONT_BASE (font) + boff;
18123 int font_descent = FONT_DESCENT (font) - boff;
18124 /* Bounding box of the overall glyphs. */
18125 int leftmost, rightmost, lowest, highest;
18126 int i, width, ascent, descent;
18128 cmp->font = (void *) font;
18130 /* Initialize the bounding box. */
18131 if (font_info
18132 && (pcm = rif->per_char_metric (font, &char2b,
18133 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18135 width = pcm->width;
18136 ascent = pcm->ascent;
18137 descent = pcm->descent;
18139 else
18141 width = FONT_WIDTH (font);
18142 ascent = FONT_BASE (font);
18143 descent = FONT_DESCENT (font);
18146 rightmost = width;
18147 lowest = - descent + boff;
18148 highest = ascent + boff;
18149 leftmost = 0;
18151 if (font_info
18152 && font_info->default_ascent
18153 && CHAR_TABLE_P (Vuse_default_ascent)
18154 && !NILP (Faref (Vuse_default_ascent,
18155 make_number (it->char_to_display))))
18156 highest = font_info->default_ascent + boff;
18158 /* Draw the first glyph at the normal position. It may be
18159 shifted to right later if some other glyphs are drawn at
18160 the left. */
18161 cmp->offsets[0] = 0;
18162 cmp->offsets[1] = boff;
18164 /* Set cmp->offsets for the remaining glyphs. */
18165 for (i = 1; i < cmp->glyph_len; i++)
18167 int left, right, btm, top;
18168 int ch = COMPOSITION_GLYPH (cmp, i);
18169 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18171 face = FACE_FROM_ID (it->f, face_id);
18172 get_char_face_and_encoding (it->f, ch, face->id,
18173 &char2b, it->multibyte_p, 0);
18174 font = face->font;
18175 if (font == NULL)
18177 font = FRAME_FONT (it->f);
18178 boff = FRAME_BASELINE_OFFSET (it->f);
18179 font_info = NULL;
18181 else
18183 font_info
18184 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18185 boff = font_info->baseline_offset;
18186 if (font_info->vertical_centering)
18187 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18190 if (font_info
18191 && (pcm = rif->per_char_metric (font, &char2b,
18192 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18194 width = pcm->width;
18195 ascent = pcm->ascent;
18196 descent = pcm->descent;
18198 else
18200 width = FONT_WIDTH (font);
18201 ascent = 1;
18202 descent = 0;
18205 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18207 /* Relative composition with or without
18208 alternate chars. */
18209 left = (leftmost + rightmost - width) / 2;
18210 btm = - descent + boff;
18211 if (font_info && font_info->relative_compose
18212 && (! CHAR_TABLE_P (Vignore_relative_composition)
18213 || NILP (Faref (Vignore_relative_composition,
18214 make_number (ch)))))
18217 if (- descent >= font_info->relative_compose)
18218 /* One extra pixel between two glyphs. */
18219 btm = highest + 1;
18220 else if (ascent <= 0)
18221 /* One extra pixel between two glyphs. */
18222 btm = lowest - 1 - ascent - descent;
18225 else
18227 /* A composition rule is specified by an integer
18228 value that encodes global and new reference
18229 points (GREF and NREF). GREF and NREF are
18230 specified by numbers as below:
18232 0---1---2 -- ascent
18236 9--10--11 -- center
18238 ---3---4---5--- baseline
18240 6---7---8 -- descent
18242 int rule = COMPOSITION_RULE (cmp, i);
18243 int gref, nref, grefx, grefy, nrefx, nrefy;
18245 COMPOSITION_DECODE_RULE (rule, gref, nref);
18246 grefx = gref % 3, nrefx = nref % 3;
18247 grefy = gref / 3, nrefy = nref / 3;
18249 left = (leftmost
18250 + grefx * (rightmost - leftmost) / 2
18251 - nrefx * width / 2);
18252 btm = ((grefy == 0 ? highest
18253 : grefy == 1 ? 0
18254 : grefy == 2 ? lowest
18255 : (highest + lowest) / 2)
18256 - (nrefy == 0 ? ascent + descent
18257 : nrefy == 1 ? descent - boff
18258 : nrefy == 2 ? 0
18259 : (ascent + descent) / 2));
18262 cmp->offsets[i * 2] = left;
18263 cmp->offsets[i * 2 + 1] = btm + descent;
18265 /* Update the bounding box of the overall glyphs. */
18266 right = left + width;
18267 top = btm + descent + ascent;
18268 if (left < leftmost)
18269 leftmost = left;
18270 if (right > rightmost)
18271 rightmost = right;
18272 if (top > highest)
18273 highest = top;
18274 if (btm < lowest)
18275 lowest = btm;
18278 /* If there are glyphs whose x-offsets are negative,
18279 shift all glyphs to the right and make all x-offsets
18280 non-negative. */
18281 if (leftmost < 0)
18283 for (i = 0; i < cmp->glyph_len; i++)
18284 cmp->offsets[i * 2] -= leftmost;
18285 rightmost -= leftmost;
18288 cmp->pixel_width = rightmost;
18289 cmp->ascent = highest;
18290 cmp->descent = - lowest;
18291 if (cmp->ascent < font_ascent)
18292 cmp->ascent = font_ascent;
18293 if (cmp->descent < font_descent)
18294 cmp->descent = font_descent;
18297 it->pixel_width = cmp->pixel_width;
18298 it->ascent = it->phys_ascent = cmp->ascent;
18299 it->descent = it->phys_descent = cmp->descent;
18301 if (face->box != FACE_NO_BOX)
18303 int thick = face->box_line_width;
18305 if (thick > 0)
18307 it->ascent += thick;
18308 it->descent += thick;
18310 else
18311 thick = - thick;
18313 if (it->start_of_box_run_p)
18314 it->pixel_width += thick;
18315 if (it->end_of_box_run_p)
18316 it->pixel_width += thick;
18319 /* If face has an overline, add the height of the overline
18320 (1 pixel) and a 1 pixel margin to the character height. */
18321 if (face->overline_p)
18322 it->ascent += 2;
18324 take_vertical_position_into_account (it);
18326 if (it->glyph_row)
18327 append_composite_glyph (it);
18329 else if (it->what == IT_IMAGE)
18330 produce_image_glyph (it);
18331 else if (it->what == IT_STRETCH)
18332 produce_stretch_glyph (it);
18334 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18335 because this isn't true for images with `:ascent 100'. */
18336 xassert (it->ascent >= 0 && it->descent >= 0);
18337 if (it->area == TEXT_AREA)
18338 it->current_x += it->pixel_width;
18340 it->descent += it->extra_line_spacing;
18342 it->max_ascent = max (it->max_ascent, it->ascent);
18343 it->max_descent = max (it->max_descent, it->descent);
18344 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18345 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18348 /* EXPORT for RIF:
18349 Output LEN glyphs starting at START at the nominal cursor position.
18350 Advance the nominal cursor over the text. The global variable
18351 updated_window contains the window being updated, updated_row is
18352 the glyph row being updated, and updated_area is the area of that
18353 row being updated. */
18355 void
18356 x_write_glyphs (start, len)
18357 struct glyph *start;
18358 int len;
18360 int x, hpos;
18362 xassert (updated_window && updated_row);
18363 BLOCK_INPUT;
18365 /* Write glyphs. */
18367 hpos = start - updated_row->glyphs[updated_area];
18368 x = draw_glyphs (updated_window, output_cursor.x,
18369 updated_row, updated_area,
18370 hpos, hpos + len,
18371 DRAW_NORMAL_TEXT, 0);
18373 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18374 if (updated_area == TEXT_AREA
18375 && updated_window->phys_cursor_on_p
18376 && updated_window->phys_cursor.vpos == output_cursor.vpos
18377 && updated_window->phys_cursor.hpos >= hpos
18378 && updated_window->phys_cursor.hpos < hpos + len)
18379 updated_window->phys_cursor_on_p = 0;
18381 UNBLOCK_INPUT;
18383 /* Advance the output cursor. */
18384 output_cursor.hpos += len;
18385 output_cursor.x = x;
18389 /* EXPORT for RIF:
18390 Insert LEN glyphs from START at the nominal cursor position. */
18392 void
18393 x_insert_glyphs (start, len)
18394 struct glyph *start;
18395 int len;
18397 struct frame *f;
18398 struct window *w;
18399 int line_height, shift_by_width, shifted_region_width;
18400 struct glyph_row *row;
18401 struct glyph *glyph;
18402 int frame_x, frame_y, hpos;
18404 xassert (updated_window && updated_row);
18405 BLOCK_INPUT;
18406 w = updated_window;
18407 f = XFRAME (WINDOW_FRAME (w));
18409 /* Get the height of the line we are in. */
18410 row = updated_row;
18411 line_height = row->height;
18413 /* Get the width of the glyphs to insert. */
18414 shift_by_width = 0;
18415 for (glyph = start; glyph < start + len; ++glyph)
18416 shift_by_width += glyph->pixel_width;
18418 /* Get the width of the region to shift right. */
18419 shifted_region_width = (window_box_width (w, updated_area)
18420 - output_cursor.x
18421 - shift_by_width);
18423 /* Shift right. */
18424 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18425 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18427 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18428 line_height, shift_by_width);
18430 /* Write the glyphs. */
18431 hpos = start - row->glyphs[updated_area];
18432 draw_glyphs (w, output_cursor.x, row, updated_area,
18433 hpos, hpos + len,
18434 DRAW_NORMAL_TEXT, 0);
18436 /* Advance the output cursor. */
18437 output_cursor.hpos += len;
18438 output_cursor.x += shift_by_width;
18439 UNBLOCK_INPUT;
18443 /* EXPORT for RIF:
18444 Erase the current text line from the nominal cursor position
18445 (inclusive) to pixel column TO_X (exclusive). The idea is that
18446 everything from TO_X onward is already erased.
18448 TO_X is a pixel position relative to updated_area of
18449 updated_window. TO_X == -1 means clear to the end of this area. */
18451 void
18452 x_clear_end_of_line (to_x)
18453 int to_x;
18455 struct frame *f;
18456 struct window *w = updated_window;
18457 int max_x, min_y, max_y;
18458 int from_x, from_y, to_y;
18460 xassert (updated_window && updated_row);
18461 f = XFRAME (w->frame);
18463 if (updated_row->full_width_p)
18465 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
18466 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
18467 && !w->pseudo_window_p)
18468 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
18470 else
18471 max_x = window_box_width (w, updated_area);
18472 max_y = window_text_bottom_y (w);
18474 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18475 of window. For TO_X > 0, truncate to end of drawing area. */
18476 if (to_x == 0)
18477 return;
18478 else if (to_x < 0)
18479 to_x = max_x;
18480 else
18481 to_x = min (to_x, max_x);
18483 to_y = min (max_y, output_cursor.y + updated_row->height);
18485 /* Notice if the cursor will be cleared by this operation. */
18486 if (!updated_row->full_width_p)
18487 notice_overwritten_cursor (w, updated_area,
18488 output_cursor.x, -1,
18489 updated_row->y,
18490 MATRIX_ROW_BOTTOM_Y (updated_row));
18492 from_x = output_cursor.x;
18494 /* Translate to frame coordinates. */
18495 if (updated_row->full_width_p)
18497 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18498 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18500 else
18502 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
18503 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
18506 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
18507 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18508 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18510 /* Prevent inadvertently clearing to end of the X window. */
18511 if (to_x > from_x && to_y > from_y)
18513 BLOCK_INPUT;
18514 rif->clear_frame_area (f, from_x, from_y,
18515 to_x - from_x, to_y - from_y);
18516 UNBLOCK_INPUT;
18520 #endif /* HAVE_WINDOW_SYSTEM */
18524 /***********************************************************************
18525 Cursor types
18526 ***********************************************************************/
18528 /* Value is the internal representation of the specified cursor type
18529 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18530 of the bar cursor. */
18532 enum text_cursor_kinds
18533 get_specified_cursor_type (arg, width)
18534 Lisp_Object arg;
18535 int *width;
18537 enum text_cursor_kinds type;
18539 if (NILP (arg))
18540 return NO_CURSOR;
18542 if (EQ (arg, Qbox))
18543 return FILLED_BOX_CURSOR;
18545 if (EQ (arg, Qhollow))
18546 return HOLLOW_BOX_CURSOR;
18548 if (EQ (arg, Qbar))
18550 *width = 2;
18551 return BAR_CURSOR;
18554 if (CONSP (arg)
18555 && EQ (XCAR (arg), Qbar)
18556 && INTEGERP (XCDR (arg))
18557 && XINT (XCDR (arg)) >= 0)
18559 *width = XINT (XCDR (arg));
18560 return BAR_CURSOR;
18563 if (EQ (arg, Qhbar))
18565 *width = 2;
18566 return HBAR_CURSOR;
18569 if (CONSP (arg)
18570 && EQ (XCAR (arg), Qhbar)
18571 && INTEGERP (XCDR (arg))
18572 && XINT (XCDR (arg)) >= 0)
18574 *width = XINT (XCDR (arg));
18575 return HBAR_CURSOR;
18578 /* Treat anything unknown as "hollow box cursor".
18579 It was bad to signal an error; people have trouble fixing
18580 .Xdefaults with Emacs, when it has something bad in it. */
18581 type = HOLLOW_BOX_CURSOR;
18583 return type;
18586 /* Set the default cursor types for specified frame. */
18587 void
18588 set_frame_cursor_types (f, arg)
18589 struct frame *f;
18590 Lisp_Object arg;
18592 int width;
18593 Lisp_Object tem;
18595 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18596 FRAME_CURSOR_WIDTH (f) = width;
18598 /* By default, set up the blink-off state depending on the on-state. */
18600 tem = Fassoc (arg, Vblink_cursor_alist);
18601 if (!NILP (tem))
18603 FRAME_BLINK_OFF_CURSOR (f)
18604 = get_specified_cursor_type (XCDR (tem), &width);
18605 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18607 else
18608 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18612 /* Return the cursor we want to be displayed in window W. Return
18613 width of bar/hbar cursor through WIDTH arg. Return with
18614 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18615 (i.e. if the `system caret' should track this cursor).
18617 In a mini-buffer window, we want the cursor only to appear if we
18618 are reading input from this window. For the selected window, we
18619 want the cursor type given by the frame parameter or buffer local
18620 setting of cursor-type. If explicitly marked off, draw no cursor.
18621 In all other cases, we want a hollow box cursor. */
18623 enum text_cursor_kinds
18624 get_window_cursor_type (w, width, active_cursor)
18625 struct window *w;
18626 int *width;
18627 int *active_cursor;
18629 struct frame *f = XFRAME (w->frame);
18630 struct buffer *b = XBUFFER (w->buffer);
18631 int cursor_type = DEFAULT_CURSOR;
18632 Lisp_Object alt_cursor;
18633 int non_selected = 0;
18635 *active_cursor = 1;
18637 /* Echo area */
18638 if (cursor_in_echo_area
18639 && FRAME_HAS_MINIBUF_P (f)
18640 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18642 if (w == XWINDOW (echo_area_window))
18644 *width = FRAME_CURSOR_WIDTH (f);
18645 return FRAME_DESIRED_CURSOR (f);
18648 *active_cursor = 0;
18649 non_selected = 1;
18652 /* Nonselected window or nonselected frame. */
18653 else if (w != XWINDOW (f->selected_window)
18654 #ifdef HAVE_WINDOW_SYSTEM
18655 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18656 #endif
18659 *active_cursor = 0;
18661 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18662 return NO_CURSOR;
18664 non_selected = 1;
18667 /* Never display a cursor in a window in which cursor-type is nil. */
18668 if (NILP (b->cursor_type))
18669 return NO_CURSOR;
18671 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18672 if (non_selected)
18674 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18675 return get_specified_cursor_type (alt_cursor, width);
18678 /* Get the normal cursor type for this window. */
18679 if (EQ (b->cursor_type, Qt))
18681 cursor_type = FRAME_DESIRED_CURSOR (f);
18682 *width = FRAME_CURSOR_WIDTH (f);
18684 else
18685 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18687 /* Use normal cursor if not blinked off. */
18688 if (!w->cursor_off_p)
18689 return cursor_type;
18691 /* Cursor is blinked off, so determine how to "toggle" it. */
18693 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18694 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18695 return get_specified_cursor_type (XCDR (alt_cursor), width);
18697 /* Then see if frame has specified a specific blink off cursor type. */
18698 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18700 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18701 return FRAME_BLINK_OFF_CURSOR (f);
18704 /* Finally perform built-in cursor blinking:
18705 filled box <-> hollow box
18706 wide [h]bar <-> narrow [h]bar
18707 narrow [h]bar <-> no cursor
18708 other type <-> no cursor */
18710 if (cursor_type == FILLED_BOX_CURSOR)
18711 return HOLLOW_BOX_CURSOR;
18713 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18715 *width = 1;
18716 return cursor_type;
18719 return NO_CURSOR;
18723 #ifdef HAVE_WINDOW_SYSTEM
18725 /* Notice when the text cursor of window W has been completely
18726 overwritten by a drawing operation that outputs glyphs in AREA
18727 starting at X0 and ending at X1 in the line starting at Y0 and
18728 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18729 the rest of the line after X0 has been written. Y coordinates
18730 are window-relative. */
18732 static void
18733 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18734 struct window *w;
18735 enum glyph_row_area area;
18736 int x0, y0, x1, y1;
18738 if (area == TEXT_AREA && w->phys_cursor_on_p)
18740 int cx0 = w->phys_cursor.x;
18741 int cx1 = cx0 + w->phys_cursor_width;
18742 int cy0 = w->phys_cursor.y;
18743 int cy1 = cy0 + w->phys_cursor_height;
18745 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18747 /* The cursor image will be completely removed from the
18748 screen if the output area intersects the cursor area in
18749 y-direction. When we draw in [y0 y1[, and some part of
18750 the cursor is at y < y0, that part must have been drawn
18751 before. When scrolling, the cursor is erased before
18752 actually scrolling, so we don't come here. When not
18753 scrolling, the rows above the old cursor row must have
18754 changed, and in this case these rows must have written
18755 over the cursor image.
18757 Likewise if part of the cursor is below y1, with the
18758 exception of the cursor being in the first blank row at
18759 the buffer and window end because update_text_area
18760 doesn't draw that row. (Except when it does, but
18761 that's handled in update_text_area.) */
18763 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18764 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18765 w->phys_cursor_on_p = 0;
18770 #endif /* HAVE_WINDOW_SYSTEM */
18773 /************************************************************************
18774 Mouse Face
18775 ************************************************************************/
18777 #ifdef HAVE_WINDOW_SYSTEM
18779 /* EXPORT for RIF:
18780 Fix the display of area AREA of overlapping row ROW in window W. */
18782 void
18783 x_fix_overlapping_area (w, row, area)
18784 struct window *w;
18785 struct glyph_row *row;
18786 enum glyph_row_area area;
18788 int i, x;
18790 BLOCK_INPUT;
18792 if (area == LEFT_MARGIN_AREA)
18793 x = 0;
18794 else if (area == TEXT_AREA)
18795 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
18796 else
18797 x = (window_box_width (w, LEFT_MARGIN_AREA)
18798 + window_box_width (w, TEXT_AREA));
18800 for (i = 0; i < row->used[area];)
18802 if (row->glyphs[area][i].overlaps_vertically_p)
18804 int start = i, start_x = x;
18808 x += row->glyphs[area][i].pixel_width;
18809 ++i;
18811 while (i < row->used[area]
18812 && row->glyphs[area][i].overlaps_vertically_p);
18814 draw_glyphs (w, start_x, row, area,
18815 start, i,
18816 DRAW_NORMAL_TEXT, 1);
18818 else
18820 x += row->glyphs[area][i].pixel_width;
18821 ++i;
18825 UNBLOCK_INPUT;
18829 /* EXPORT:
18830 Draw the cursor glyph of window W in glyph row ROW. See the
18831 comment of draw_glyphs for the meaning of HL. */
18833 void
18834 draw_phys_cursor_glyph (w, row, hl)
18835 struct window *w;
18836 struct glyph_row *row;
18837 enum draw_glyphs_face hl;
18839 /* If cursor hpos is out of bounds, don't draw garbage. This can
18840 happen in mini-buffer windows when switching between echo area
18841 glyphs and mini-buffer. */
18842 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18844 int on_p = w->phys_cursor_on_p;
18845 int x1;
18846 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18847 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18848 hl, 0);
18849 w->phys_cursor_on_p = on_p;
18851 if (hl == DRAW_CURSOR)
18852 w->phys_cursor_width = x1 - w->phys_cursor.x;
18853 /* When we erase the cursor, and ROW is overlapped by other
18854 rows, make sure that these overlapping parts of other rows
18855 are redrawn. */
18856 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18858 if (row > w->current_matrix->rows
18859 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18860 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18862 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18863 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18864 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
18870 /* EXPORT:
18871 Erase the image of a cursor of window W from the screen. */
18873 void
18874 erase_phys_cursor (w)
18875 struct window *w;
18877 struct frame *f = XFRAME (w->frame);
18878 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
18879 int hpos = w->phys_cursor.hpos;
18880 int vpos = w->phys_cursor.vpos;
18881 int mouse_face_here_p = 0;
18882 struct glyph_matrix *active_glyphs = w->current_matrix;
18883 struct glyph_row *cursor_row;
18884 struct glyph *cursor_glyph;
18885 enum draw_glyphs_face hl;
18887 /* No cursor displayed or row invalidated => nothing to do on the
18888 screen. */
18889 if (w->phys_cursor_type == NO_CURSOR)
18890 goto mark_cursor_off;
18892 /* VPOS >= active_glyphs->nrows means that window has been resized.
18893 Don't bother to erase the cursor. */
18894 if (vpos >= active_glyphs->nrows)
18895 goto mark_cursor_off;
18897 /* If row containing cursor is marked invalid, there is nothing we
18898 can do. */
18899 cursor_row = MATRIX_ROW (active_glyphs, vpos);
18900 if (!cursor_row->enabled_p)
18901 goto mark_cursor_off;
18903 /* If row is completely invisible, don't attempt to delete a cursor which
18904 isn't there. This can happen if cursor is at top of a window, and
18905 we switch to a buffer with a header line in that window. */
18906 if (cursor_row->visible_height <= 0)
18907 goto mark_cursor_off;
18909 /* This can happen when the new row is shorter than the old one.
18910 In this case, either draw_glyphs or clear_end_of_line
18911 should have cleared the cursor. Note that we wouldn't be
18912 able to erase the cursor in this case because we don't have a
18913 cursor glyph at hand. */
18914 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
18915 goto mark_cursor_off;
18917 /* If the cursor is in the mouse face area, redisplay that when
18918 we clear the cursor. */
18919 if (! NILP (dpyinfo->mouse_face_window)
18920 && w == XWINDOW (dpyinfo->mouse_face_window)
18921 && (vpos > dpyinfo->mouse_face_beg_row
18922 || (vpos == dpyinfo->mouse_face_beg_row
18923 && hpos >= dpyinfo->mouse_face_beg_col))
18924 && (vpos < dpyinfo->mouse_face_end_row
18925 || (vpos == dpyinfo->mouse_face_end_row
18926 && hpos < dpyinfo->mouse_face_end_col))
18927 /* Don't redraw the cursor's spot in mouse face if it is at the
18928 end of a line (on a newline). The cursor appears there, but
18929 mouse highlighting does not. */
18930 && cursor_row->used[TEXT_AREA] > hpos)
18931 mouse_face_here_p = 1;
18933 /* Maybe clear the display under the cursor. */
18934 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
18936 int x, y;
18937 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
18939 cursor_glyph = get_phys_cursor_glyph (w);
18940 if (cursor_glyph == NULL)
18941 goto mark_cursor_off;
18943 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
18944 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
18946 rif->clear_frame_area (f, x, y,
18947 cursor_glyph->pixel_width, cursor_row->visible_height);
18950 /* Erase the cursor by redrawing the character underneath it. */
18951 if (mouse_face_here_p)
18952 hl = DRAW_MOUSE_FACE;
18953 else
18954 hl = DRAW_NORMAL_TEXT;
18955 draw_phys_cursor_glyph (w, cursor_row, hl);
18957 mark_cursor_off:
18958 w->phys_cursor_on_p = 0;
18959 w->phys_cursor_type = NO_CURSOR;
18963 /* EXPORT:
18964 Display or clear cursor of window W. If ON is zero, clear the
18965 cursor. If it is non-zero, display the cursor. If ON is nonzero,
18966 where to put the cursor is specified by HPOS, VPOS, X and Y. */
18968 void
18969 display_and_set_cursor (w, on, hpos, vpos, x, y)
18970 struct window *w;
18971 int on, hpos, vpos, x, y;
18973 struct frame *f = XFRAME (w->frame);
18974 int new_cursor_type;
18975 int new_cursor_width;
18976 int active_cursor;
18977 struct glyph_matrix *current_glyphs;
18978 struct glyph_row *glyph_row;
18979 struct glyph *glyph;
18981 /* This is pointless on invisible frames, and dangerous on garbaged
18982 windows and frames; in the latter case, the frame or window may
18983 be in the midst of changing its size, and x and y may be off the
18984 window. */
18985 if (! FRAME_VISIBLE_P (f)
18986 || FRAME_GARBAGED_P (f)
18987 || vpos >= w->current_matrix->nrows
18988 || hpos >= w->current_matrix->matrix_w)
18989 return;
18991 /* If cursor is off and we want it off, return quickly. */
18992 if (!on && !w->phys_cursor_on_p)
18993 return;
18995 current_glyphs = w->current_matrix;
18996 glyph_row = MATRIX_ROW (current_glyphs, vpos);
18997 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
18999 /* If cursor row is not enabled, we don't really know where to
19000 display the cursor. */
19001 if (!glyph_row->enabled_p)
19003 w->phys_cursor_on_p = 0;
19004 return;
19007 xassert (interrupt_input_blocked);
19009 /* Set new_cursor_type to the cursor we want to be displayed. */
19010 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
19012 /* If cursor is currently being shown and we don't want it to be or
19013 it is in the wrong place, or the cursor type is not what we want,
19014 erase it. */
19015 if (w->phys_cursor_on_p
19016 && (!on
19017 || w->phys_cursor.x != x
19018 || w->phys_cursor.y != y
19019 || new_cursor_type != w->phys_cursor_type
19020 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19021 && new_cursor_width != w->phys_cursor_width)))
19022 erase_phys_cursor (w);
19024 /* Don't check phys_cursor_on_p here because that flag is only set
19025 to zero in some cases where we know that the cursor has been
19026 completely erased, to avoid the extra work of erasing the cursor
19027 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19028 still not be visible, or it has only been partly erased. */
19029 if (on)
19031 w->phys_cursor_ascent = glyph_row->ascent;
19032 w->phys_cursor_height = glyph_row->height;
19034 /* Set phys_cursor_.* before x_draw_.* is called because some
19035 of them may need the information. */
19036 w->phys_cursor.x = x;
19037 w->phys_cursor.y = glyph_row->y;
19038 w->phys_cursor.hpos = hpos;
19039 w->phys_cursor.vpos = vpos;
19042 rif->draw_window_cursor (w, glyph_row, x, y,
19043 new_cursor_type, new_cursor_width,
19044 on, active_cursor);
19048 /* Switch the display of W's cursor on or off, according to the value
19049 of ON. */
19051 static void
19052 update_window_cursor (w, on)
19053 struct window *w;
19054 int on;
19056 /* Don't update cursor in windows whose frame is in the process
19057 of being deleted. */
19058 if (w->current_matrix)
19060 BLOCK_INPUT;
19061 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19062 w->phys_cursor.x, w->phys_cursor.y);
19063 UNBLOCK_INPUT;
19068 /* Call update_window_cursor with parameter ON_P on all leaf windows
19069 in the window tree rooted at W. */
19071 static void
19072 update_cursor_in_window_tree (w, on_p)
19073 struct window *w;
19074 int on_p;
19076 while (w)
19078 if (!NILP (w->hchild))
19079 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19080 else if (!NILP (w->vchild))
19081 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19082 else
19083 update_window_cursor (w, on_p);
19085 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19090 /* EXPORT:
19091 Display the cursor on window W, or clear it, according to ON_P.
19092 Don't change the cursor's position. */
19094 void
19095 x_update_cursor (f, on_p)
19096 struct frame *f;
19097 int on_p;
19099 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19103 /* EXPORT:
19104 Clear the cursor of window W to background color, and mark the
19105 cursor as not shown. This is used when the text where the cursor
19106 is is about to be rewritten. */
19108 void
19109 x_clear_cursor (w)
19110 struct window *w;
19112 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19113 update_window_cursor (w, 0);
19117 /* EXPORT:
19118 Display the active region described by mouse_face_* according to DRAW. */
19120 void
19121 show_mouse_face (dpyinfo, draw)
19122 Display_Info *dpyinfo;
19123 enum draw_glyphs_face draw;
19125 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19126 struct frame *f = XFRAME (WINDOW_FRAME (w));
19128 if (/* If window is in the process of being destroyed, don't bother
19129 to do anything. */
19130 w->current_matrix != NULL
19131 /* Don't update mouse highlight if hidden */
19132 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19133 /* Recognize when we are called to operate on rows that don't exist
19134 anymore. This can happen when a window is split. */
19135 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19137 int phys_cursor_on_p = w->phys_cursor_on_p;
19138 struct glyph_row *row, *first, *last;
19140 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19141 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19143 for (row = first; row <= last && row->enabled_p; ++row)
19145 int start_hpos, end_hpos, start_x;
19147 /* For all but the first row, the highlight starts at column 0. */
19148 if (row == first)
19150 start_hpos = dpyinfo->mouse_face_beg_col;
19151 start_x = dpyinfo->mouse_face_beg_x;
19153 else
19155 start_hpos = 0;
19156 start_x = 0;
19159 if (row == last)
19160 end_hpos = dpyinfo->mouse_face_end_col;
19161 else
19162 end_hpos = row->used[TEXT_AREA];
19164 if (end_hpos > start_hpos)
19166 draw_glyphs (w, start_x, row, TEXT_AREA,
19167 start_hpos, end_hpos,
19168 draw, 0);
19170 row->mouse_face_p
19171 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19175 /* When we've written over the cursor, arrange for it to
19176 be displayed again. */
19177 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19179 BLOCK_INPUT;
19180 display_and_set_cursor (w, 1,
19181 w->phys_cursor.hpos, w->phys_cursor.vpos,
19182 w->phys_cursor.x, w->phys_cursor.y);
19183 UNBLOCK_INPUT;
19187 /* Change the mouse cursor. */
19188 if (draw == DRAW_NORMAL_TEXT)
19189 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19190 else if (draw == DRAW_MOUSE_FACE)
19191 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19192 else
19193 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19196 /* EXPORT:
19197 Clear out the mouse-highlighted active region.
19198 Redraw it un-highlighted first. Value is non-zero if mouse
19199 face was actually drawn unhighlighted. */
19202 clear_mouse_face (dpyinfo)
19203 Display_Info *dpyinfo;
19205 int cleared = 0;
19207 if (!NILP (dpyinfo->mouse_face_window))
19209 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19210 cleared = 1;
19213 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19214 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19215 dpyinfo->mouse_face_window = Qnil;
19216 dpyinfo->mouse_face_overlay = Qnil;
19217 return cleared;
19221 /* EXPORT:
19222 Non-zero if physical cursor of window W is within mouse face. */
19225 cursor_in_mouse_face_p (w)
19226 struct window *w;
19228 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19229 int in_mouse_face = 0;
19231 if (WINDOWP (dpyinfo->mouse_face_window)
19232 && XWINDOW (dpyinfo->mouse_face_window) == w)
19234 int hpos = w->phys_cursor.hpos;
19235 int vpos = w->phys_cursor.vpos;
19237 if (vpos >= dpyinfo->mouse_face_beg_row
19238 && vpos <= dpyinfo->mouse_face_end_row
19239 && (vpos > dpyinfo->mouse_face_beg_row
19240 || hpos >= dpyinfo->mouse_face_beg_col)
19241 && (vpos < dpyinfo->mouse_face_end_row
19242 || hpos < dpyinfo->mouse_face_end_col
19243 || dpyinfo->mouse_face_past_end))
19244 in_mouse_face = 1;
19247 return in_mouse_face;
19253 /* Find the glyph matrix position of buffer position CHARPOS in window
19254 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19255 current glyphs must be up to date. If CHARPOS is above window
19256 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19257 of last line in W. In the row containing CHARPOS, stop before glyphs
19258 having STOP as object. */
19260 #if 0 /* This is a version of fast_find_position that's more correct
19261 in the presence of hscrolling, for example. I didn't install
19262 it right away because the problem fixed is minor, it failed
19263 in 20.x as well, and I think it's too risky to install
19264 so near the release of 21.1. 2001-09-25 gerd. */
19266 static int
19267 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19268 struct window *w;
19269 int charpos;
19270 int *hpos, *vpos, *x, *y;
19271 Lisp_Object stop;
19273 struct glyph_row *row, *first;
19274 struct glyph *glyph, *end;
19275 int i, past_end = 0;
19277 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19278 row = row_containing_pos (w, charpos, first, NULL, 0);
19279 if (row == NULL)
19281 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19283 *x = *y = *hpos = *vpos = 0;
19284 return 0;
19286 else
19288 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19289 past_end = 1;
19293 *x = row->x;
19294 *y = row->y;
19295 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19297 glyph = row->glyphs[TEXT_AREA];
19298 end = glyph + row->used[TEXT_AREA];
19300 /* Skip over glyphs not having an object at the start of the row.
19301 These are special glyphs like truncation marks on terminal
19302 frames. */
19303 if (row->displays_text_p)
19304 while (glyph < end
19305 && INTEGERP (glyph->object)
19306 && !EQ (stop, glyph->object)
19307 && glyph->charpos < 0)
19309 *x += glyph->pixel_width;
19310 ++glyph;
19313 while (glyph < end
19314 && !INTEGERP (glyph->object)
19315 && !EQ (stop, glyph->object)
19316 && (!BUFFERP (glyph->object)
19317 || glyph->charpos < charpos))
19319 *x += glyph->pixel_width;
19320 ++glyph;
19323 *hpos = glyph - row->glyphs[TEXT_AREA];
19324 return past_end;
19327 #else /* not 0 */
19329 static int
19330 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19331 struct window *w;
19332 int pos;
19333 int *hpos, *vpos, *x, *y;
19334 Lisp_Object stop;
19336 int i;
19337 int lastcol;
19338 int maybe_next_line_p = 0;
19339 int line_start_position;
19340 int yb = window_text_bottom_y (w);
19341 struct glyph_row *row, *best_row;
19342 int row_vpos, best_row_vpos;
19343 int current_x;
19345 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19346 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19348 while (row->y < yb)
19350 if (row->used[TEXT_AREA])
19351 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19352 else
19353 line_start_position = 0;
19355 if (line_start_position > pos)
19356 break;
19357 /* If the position sought is the end of the buffer,
19358 don't include the blank lines at the bottom of the window. */
19359 else if (line_start_position == pos
19360 && pos == BUF_ZV (XBUFFER (w->buffer)))
19362 maybe_next_line_p = 1;
19363 break;
19365 else if (line_start_position > 0)
19367 best_row = row;
19368 best_row_vpos = row_vpos;
19371 if (row->y + row->height >= yb)
19372 break;
19374 ++row;
19375 ++row_vpos;
19378 /* Find the right column within BEST_ROW. */
19379 lastcol = 0;
19380 current_x = best_row->x;
19381 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19383 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19384 int charpos = glyph->charpos;
19386 if (BUFFERP (glyph->object))
19388 if (charpos == pos)
19390 *hpos = i;
19391 *vpos = best_row_vpos;
19392 *x = current_x;
19393 *y = best_row->y;
19394 return 1;
19396 else if (charpos > pos)
19397 break;
19399 else if (EQ (glyph->object, stop))
19400 break;
19402 if (charpos > 0)
19403 lastcol = i;
19404 current_x += glyph->pixel_width;
19407 /* If we're looking for the end of the buffer,
19408 and we didn't find it in the line we scanned,
19409 use the start of the following line. */
19410 if (maybe_next_line_p)
19412 ++best_row;
19413 ++best_row_vpos;
19414 lastcol = 0;
19415 current_x = best_row->x;
19418 *vpos = best_row_vpos;
19419 *hpos = lastcol + 1;
19420 *x = current_x;
19421 *y = best_row->y;
19422 return 0;
19425 #endif /* not 0 */
19428 /* Find the position of the glyph for position POS in OBJECT in
19429 window W's current matrix, and return in *X, *Y the pixel
19430 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19432 RIGHT_P non-zero means return the position of the right edge of the
19433 glyph, RIGHT_P zero means return the left edge position.
19435 If no glyph for POS exists in the matrix, return the position of
19436 the glyph with the next smaller position that is in the matrix, if
19437 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19438 exists in the matrix, return the position of the glyph with the
19439 next larger position in OBJECT.
19441 Value is non-zero if a glyph was found. */
19443 static int
19444 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19445 struct window *w;
19446 int pos;
19447 Lisp_Object object;
19448 int *hpos, *vpos, *x, *y;
19449 int right_p;
19451 int yb = window_text_bottom_y (w);
19452 struct glyph_row *r;
19453 struct glyph *best_glyph = NULL;
19454 struct glyph_row *best_row = NULL;
19455 int best_x = 0;
19457 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19458 r->enabled_p && r->y < yb;
19459 ++r)
19461 struct glyph *g = r->glyphs[TEXT_AREA];
19462 struct glyph *e = g + r->used[TEXT_AREA];
19463 int gx;
19465 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19466 if (EQ (g->object, object))
19468 if (g->charpos == pos)
19470 best_glyph = g;
19471 best_x = gx;
19472 best_row = r;
19473 goto found;
19475 else if (best_glyph == NULL
19476 || ((abs (g->charpos - pos)
19477 < abs (best_glyph->charpos - pos))
19478 && (right_p
19479 ? g->charpos < pos
19480 : g->charpos > pos)))
19482 best_glyph = g;
19483 best_x = gx;
19484 best_row = r;
19489 found:
19491 if (best_glyph)
19493 *x = best_x;
19494 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19496 if (right_p)
19498 *x += best_glyph->pixel_width;
19499 ++*hpos;
19502 *y = best_row->y;
19503 *vpos = best_row - w->current_matrix->rows;
19506 return best_glyph != NULL;
19510 /* Take proper action when mouse has moved to the mode or header line
19511 or marginal area AREA of window W, x-position X and y-position Y.
19512 X is relative to the start of the text display area of W, so the
19513 width of bitmap areas and scroll bars must be subtracted to get a
19514 position relative to the start of the mode line. */
19516 static void
19517 note_mode_line_or_margin_highlight (w, x, y, area)
19518 struct window *w;
19519 int x, y;
19520 enum window_part area;
19522 struct frame *f = XFRAME (w->frame);
19523 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19524 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19525 int charpos;
19526 Lisp_Object string, help, map, pos;
19528 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19529 string = mode_line_string (w, x, y, area, &charpos);
19530 else
19531 string = marginal_area_string (w, x, y, area, &charpos);
19533 if (STRINGP (string))
19535 pos = make_number (charpos);
19537 /* If we're on a string with `help-echo' text property, arrange
19538 for the help to be displayed. This is done by setting the
19539 global variable help_echo_string to the help string. */
19540 help = Fget_text_property (pos, Qhelp_echo, string);
19541 if (!NILP (help))
19543 help_echo_string = help;
19544 XSETWINDOW (help_echo_window, w);
19545 help_echo_object = string;
19546 help_echo_pos = charpos;
19549 /* Change the mouse pointer according to what is under X/Y. */
19550 map = Fget_text_property (pos, Qlocal_map, string);
19551 if (!KEYMAPP (map))
19552 map = Fget_text_property (pos, Qkeymap, string);
19553 if (KEYMAPP (map))
19554 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19557 rif->define_frame_cursor (f, cursor);
19561 /* EXPORT:
19562 Take proper action when the mouse has moved to position X, Y on
19563 frame F as regards highlighting characters that have mouse-face
19564 properties. Also de-highlighting chars where the mouse was before.
19565 X and Y can be negative or out of range. */
19567 void
19568 note_mouse_highlight (f, x, y)
19569 struct frame *f;
19570 int x, y;
19572 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19573 enum window_part part;
19574 Lisp_Object window;
19575 struct window *w;
19576 Cursor cursor = No_Cursor;
19577 struct buffer *b;
19579 /* When a menu is active, don't highlight because this looks odd. */
19580 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19581 if (popup_activated ())
19582 return;
19583 #endif
19585 if (NILP (Vmouse_highlight)
19586 || !f->glyphs_initialized_p)
19587 return;
19589 dpyinfo->mouse_face_mouse_x = x;
19590 dpyinfo->mouse_face_mouse_y = y;
19591 dpyinfo->mouse_face_mouse_frame = f;
19593 if (dpyinfo->mouse_face_defer)
19594 return;
19596 if (gc_in_progress)
19598 dpyinfo->mouse_face_deferred_gc = 1;
19599 return;
19602 /* Which window is that in? */
19603 window = window_from_coordinates (f, x, y, &part, 1);
19605 /* If we were displaying active text in another window, clear that. */
19606 if (! EQ (window, dpyinfo->mouse_face_window))
19607 clear_mouse_face (dpyinfo);
19609 /* Not on a window -> return. */
19610 if (!WINDOWP (window))
19611 return;
19613 /* Reset help_echo_string. It will get recomputed below. */
19614 /* ++KFS: X version didn't do this, but it looks harmless. */
19615 help_echo_string = Qnil;
19617 /* Convert to window-relative pixel coordinates. */
19618 w = XWINDOW (window);
19619 frame_to_window_pixel_xy (w, &x, &y);
19621 /* Handle tool-bar window differently since it doesn't display a
19622 buffer. */
19623 if (EQ (window, f->tool_bar_window))
19625 note_tool_bar_highlight (f, x, y);
19626 return;
19629 /* Mouse is on the mode, header line or margin? */
19630 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19631 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19633 note_mode_line_or_margin_highlight (w, x, y, part);
19634 return;
19637 if (part == ON_VERTICAL_BORDER)
19638 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19639 else
19640 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19642 /* Are we in a window whose display is up to date?
19643 And verify the buffer's text has not changed. */
19644 b = XBUFFER (w->buffer);
19645 if (part == ON_TEXT
19646 && EQ (w->window_end_valid, w->buffer)
19647 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19648 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19650 int hpos, vpos, pos, i, area;
19651 struct glyph *glyph;
19652 Lisp_Object object;
19653 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19654 Lisp_Object *overlay_vec = NULL;
19655 int len, noverlays;
19656 struct buffer *obuf;
19657 int obegv, ozv, same_region;
19659 /* Find the glyph under X/Y. */
19660 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19662 /* Clear mouse face if X/Y not over text. */
19663 if (glyph == NULL
19664 || area != TEXT_AREA
19665 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19667 #if defined (HAVE_NTGUI)
19668 /* ++KFS: Why is this necessary on W32 ? */
19669 clear_mouse_face (dpyinfo);
19670 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19671 #else
19672 if (clear_mouse_face (dpyinfo))
19673 cursor = No_Cursor;
19674 #endif
19675 goto set_cursor;
19678 pos = glyph->charpos;
19679 object = glyph->object;
19680 if (!STRINGP (object) && !BUFFERP (object))
19681 goto set_cursor;
19683 /* If we get an out-of-range value, return now; avoid an error. */
19684 if (BUFFERP (object) && pos > BUF_Z (b))
19685 goto set_cursor;
19687 /* Make the window's buffer temporarily current for
19688 overlays_at and compute_char_face. */
19689 obuf = current_buffer;
19690 current_buffer = b;
19691 obegv = BEGV;
19692 ozv = ZV;
19693 BEGV = BEG;
19694 ZV = Z;
19696 /* Is this char mouse-active or does it have help-echo? */
19697 position = make_number (pos);
19699 if (BUFFERP (object))
19701 /* Put all the overlays we want in a vector in overlay_vec.
19702 Store the length in len. If there are more than 10, make
19703 enough space for all, and try again. */
19704 len = 10;
19705 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19706 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19707 if (noverlays > len)
19709 len = noverlays;
19710 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19711 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19714 /* Sort overlays into increasing priority order. */
19715 noverlays = sort_overlays (overlay_vec, noverlays, w);
19717 else
19718 noverlays = 0;
19720 same_region = (EQ (window, dpyinfo->mouse_face_window)
19721 && vpos >= dpyinfo->mouse_face_beg_row
19722 && vpos <= dpyinfo->mouse_face_end_row
19723 && (vpos > dpyinfo->mouse_face_beg_row
19724 || hpos >= dpyinfo->mouse_face_beg_col)
19725 && (vpos < dpyinfo->mouse_face_end_row
19726 || hpos < dpyinfo->mouse_face_end_col
19727 || dpyinfo->mouse_face_past_end));
19729 if (same_region)
19730 cursor = No_Cursor;
19732 /* Check mouse-face highlighting. */
19733 if (! same_region
19734 /* If there exists an overlay with mouse-face overlapping
19735 the one we are currently highlighting, we have to
19736 check if we enter the overlapping overlay, and then
19737 highlight only that. */
19738 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19739 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19741 /* Find the highest priority overlay that has a mouse-face
19742 property. */
19743 overlay = Qnil;
19744 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19746 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19747 if (!NILP (mouse_face))
19748 overlay = overlay_vec[i];
19751 /* If we're actually highlighting the same overlay as
19752 before, there's no need to do that again. */
19753 if (!NILP (overlay)
19754 && EQ (overlay, dpyinfo->mouse_face_overlay))
19755 goto check_help_echo;
19757 dpyinfo->mouse_face_overlay = overlay;
19759 /* Clear the display of the old active region, if any. */
19760 if (clear_mouse_face (dpyinfo))
19761 cursor = No_Cursor;
19763 /* If no overlay applies, get a text property. */
19764 if (NILP (overlay))
19765 mouse_face = Fget_text_property (position, Qmouse_face, object);
19767 /* Handle the overlay case. */
19768 if (!NILP (overlay))
19770 /* Find the range of text around this char that
19771 should be active. */
19772 Lisp_Object before, after;
19773 int ignore;
19775 before = Foverlay_start (overlay);
19776 after = Foverlay_end (overlay);
19777 /* Record this as the current active region. */
19778 fast_find_position (w, XFASTINT (before),
19779 &dpyinfo->mouse_face_beg_col,
19780 &dpyinfo->mouse_face_beg_row,
19781 &dpyinfo->mouse_face_beg_x,
19782 &dpyinfo->mouse_face_beg_y, Qnil);
19784 dpyinfo->mouse_face_past_end
19785 = !fast_find_position (w, XFASTINT (after),
19786 &dpyinfo->mouse_face_end_col,
19787 &dpyinfo->mouse_face_end_row,
19788 &dpyinfo->mouse_face_end_x,
19789 &dpyinfo->mouse_face_end_y, Qnil);
19790 dpyinfo->mouse_face_window = window;
19792 dpyinfo->mouse_face_face_id
19793 = face_at_buffer_position (w, pos, 0, 0,
19794 &ignore, pos + 1,
19795 !dpyinfo->mouse_face_hidden);
19797 /* Display it as active. */
19798 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19799 cursor = No_Cursor;
19801 /* Handle the text property case. */
19802 else if (!NILP (mouse_face) && BUFFERP (object))
19804 /* Find the range of text around this char that
19805 should be active. */
19806 Lisp_Object before, after, beginning, end;
19807 int ignore;
19809 beginning = Fmarker_position (w->start);
19810 end = make_number (BUF_Z (XBUFFER (object))
19811 - XFASTINT (w->window_end_pos));
19812 before
19813 = Fprevious_single_property_change (make_number (pos + 1),
19814 Qmouse_face,
19815 object, beginning);
19816 after
19817 = Fnext_single_property_change (position, Qmouse_face,
19818 object, end);
19820 /* Record this as the current active region. */
19821 fast_find_position (w, XFASTINT (before),
19822 &dpyinfo->mouse_face_beg_col,
19823 &dpyinfo->mouse_face_beg_row,
19824 &dpyinfo->mouse_face_beg_x,
19825 &dpyinfo->mouse_face_beg_y, Qnil);
19826 dpyinfo->mouse_face_past_end
19827 = !fast_find_position (w, XFASTINT (after),
19828 &dpyinfo->mouse_face_end_col,
19829 &dpyinfo->mouse_face_end_row,
19830 &dpyinfo->mouse_face_end_x,
19831 &dpyinfo->mouse_face_end_y, Qnil);
19832 dpyinfo->mouse_face_window = window;
19834 if (BUFFERP (object))
19835 dpyinfo->mouse_face_face_id
19836 = face_at_buffer_position (w, pos, 0, 0,
19837 &ignore, pos + 1,
19838 !dpyinfo->mouse_face_hidden);
19840 /* Display it as active. */
19841 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19842 cursor = No_Cursor;
19844 else if (!NILP (mouse_face) && STRINGP (object))
19846 Lisp_Object b, e;
19847 int ignore;
19849 b = Fprevious_single_property_change (make_number (pos + 1),
19850 Qmouse_face,
19851 object, Qnil);
19852 e = Fnext_single_property_change (position, Qmouse_face,
19853 object, Qnil);
19854 if (NILP (b))
19855 b = make_number (0);
19856 if (NILP (e))
19857 e = make_number (SCHARS (object) - 1);
19858 fast_find_string_pos (w, XINT (b), object,
19859 &dpyinfo->mouse_face_beg_col,
19860 &dpyinfo->mouse_face_beg_row,
19861 &dpyinfo->mouse_face_beg_x,
19862 &dpyinfo->mouse_face_beg_y, 0);
19863 fast_find_string_pos (w, XINT (e), object,
19864 &dpyinfo->mouse_face_end_col,
19865 &dpyinfo->mouse_face_end_row,
19866 &dpyinfo->mouse_face_end_x,
19867 &dpyinfo->mouse_face_end_y, 1);
19868 dpyinfo->mouse_face_past_end = 0;
19869 dpyinfo->mouse_face_window = window;
19870 dpyinfo->mouse_face_face_id
19871 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
19872 glyph->face_id, 1);
19873 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19874 cursor = No_Cursor;
19876 else if (STRINGP (object) && NILP (mouse_face))
19878 /* A string which doesn't have mouse-face, but
19879 the text ``under'' it might have. */
19880 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
19881 int start = MATRIX_ROW_START_CHARPOS (r);
19883 pos = string_buffer_position (w, object, start);
19884 if (pos > 0)
19885 mouse_face = get_char_property_and_overlay (make_number (pos),
19886 Qmouse_face,
19887 w->buffer,
19888 &overlay);
19889 if (!NILP (mouse_face) && !NILP (overlay))
19891 Lisp_Object before = Foverlay_start (overlay);
19892 Lisp_Object after = Foverlay_end (overlay);
19893 int ignore;
19895 /* Note that we might not be able to find position
19896 BEFORE in the glyph matrix if the overlay is
19897 entirely covered by a `display' property. In
19898 this case, we overshoot. So let's stop in
19899 the glyph matrix before glyphs for OBJECT. */
19900 fast_find_position (w, XFASTINT (before),
19901 &dpyinfo->mouse_face_beg_col,
19902 &dpyinfo->mouse_face_beg_row,
19903 &dpyinfo->mouse_face_beg_x,
19904 &dpyinfo->mouse_face_beg_y,
19905 object);
19907 dpyinfo->mouse_face_past_end
19908 = !fast_find_position (w, XFASTINT (after),
19909 &dpyinfo->mouse_face_end_col,
19910 &dpyinfo->mouse_face_end_row,
19911 &dpyinfo->mouse_face_end_x,
19912 &dpyinfo->mouse_face_end_y,
19913 Qnil);
19914 dpyinfo->mouse_face_window = window;
19915 dpyinfo->mouse_face_face_id
19916 = face_at_buffer_position (w, pos, 0, 0,
19917 &ignore, pos + 1,
19918 !dpyinfo->mouse_face_hidden);
19920 /* Display it as active. */
19921 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19922 cursor = No_Cursor;
19927 check_help_echo:
19929 /* Look for a `help-echo' property. */
19931 Lisp_Object help, overlay;
19933 /* Check overlays first. */
19934 help = overlay = Qnil;
19935 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
19937 overlay = overlay_vec[i];
19938 help = Foverlay_get (overlay, Qhelp_echo);
19941 if (!NILP (help))
19943 help_echo_string = help;
19944 help_echo_window = window;
19945 help_echo_object = overlay;
19946 help_echo_pos = pos;
19948 else
19950 Lisp_Object object = glyph->object;
19951 int charpos = glyph->charpos;
19953 /* Try text properties. */
19954 if (STRINGP (object)
19955 && charpos >= 0
19956 && charpos < SCHARS (object))
19958 help = Fget_text_property (make_number (charpos),
19959 Qhelp_echo, object);
19960 if (NILP (help))
19962 /* If the string itself doesn't specify a help-echo,
19963 see if the buffer text ``under'' it does. */
19964 struct glyph_row *r
19965 = MATRIX_ROW (w->current_matrix, vpos);
19966 int start = MATRIX_ROW_START_CHARPOS (r);
19967 int pos = string_buffer_position (w, object, start);
19968 if (pos > 0)
19970 help = Fget_char_property (make_number (pos),
19971 Qhelp_echo, w->buffer);
19972 if (!NILP (help))
19974 charpos = pos;
19975 object = w->buffer;
19980 else if (BUFFERP (object)
19981 && charpos >= BEGV
19982 && charpos < ZV)
19983 help = Fget_text_property (make_number (charpos), Qhelp_echo,
19984 object);
19986 if (!NILP (help))
19988 help_echo_string = help;
19989 help_echo_window = window;
19990 help_echo_object = object;
19991 help_echo_pos = charpos;
19996 BEGV = obegv;
19997 ZV = ozv;
19998 current_buffer = obuf;
20001 set_cursor:
20003 #ifndef HAVE_CARBON
20004 if (cursor != No_Cursor)
20005 #else
20006 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20007 #endif
20008 rif->define_frame_cursor (f, cursor);
20012 /* EXPORT for RIF:
20013 Clear any mouse-face on window W. This function is part of the
20014 redisplay interface, and is called from try_window_id and similar
20015 functions to ensure the mouse-highlight is off. */
20017 void
20018 x_clear_window_mouse_face (w)
20019 struct window *w;
20021 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20022 Lisp_Object window;
20024 BLOCK_INPUT;
20025 XSETWINDOW (window, w);
20026 if (EQ (window, dpyinfo->mouse_face_window))
20027 clear_mouse_face (dpyinfo);
20028 UNBLOCK_INPUT;
20032 /* EXPORT:
20033 Just discard the mouse face information for frame F, if any.
20034 This is used when the size of F is changed. */
20036 void
20037 cancel_mouse_face (f)
20038 struct frame *f;
20040 Lisp_Object window;
20041 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20043 window = dpyinfo->mouse_face_window;
20044 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20046 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20047 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20048 dpyinfo->mouse_face_window = Qnil;
20053 #endif /* HAVE_WINDOW_SYSTEM */
20056 /***********************************************************************
20057 Exposure Events
20058 ***********************************************************************/
20060 #ifdef HAVE_WINDOW_SYSTEM
20062 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20063 which intersects rectangle R. R is in window-relative coordinates. */
20065 static void
20066 expose_area (w, row, r, area)
20067 struct window *w;
20068 struct glyph_row *row;
20069 XRectangle *r;
20070 enum glyph_row_area area;
20072 struct glyph *first = row->glyphs[area];
20073 struct glyph *end = row->glyphs[area] + row->used[area];
20074 struct glyph *last;
20075 int first_x, start_x, x;
20077 if (area == TEXT_AREA && row->fill_line_p)
20078 /* If row extends face to end of line write the whole line. */
20079 draw_glyphs (w, 0, row, area,
20080 0, row->used[area],
20081 DRAW_NORMAL_TEXT, 0);
20082 else
20084 /* Set START_X to the window-relative start position for drawing glyphs of
20085 AREA. The first glyph of the text area can be partially visible.
20086 The first glyphs of other areas cannot. */
20087 if (area == LEFT_MARGIN_AREA)
20088 start_x = 0;
20089 else if (area == TEXT_AREA)
20090 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
20091 else
20092 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
20093 + window_box_width (w, TEXT_AREA));
20094 x = start_x;
20096 /* Find the first glyph that must be redrawn. */
20097 while (first < end
20098 && x + first->pixel_width < r->x)
20100 x += first->pixel_width;
20101 ++first;
20104 /* Find the last one. */
20105 last = first;
20106 first_x = x;
20107 while (last < end
20108 && x < r->x + r->width)
20110 x += last->pixel_width;
20111 ++last;
20114 /* Repaint. */
20115 if (last > first)
20116 draw_glyphs (w, first_x - start_x, row, area,
20117 first - row->glyphs[area], last - row->glyphs[area],
20118 DRAW_NORMAL_TEXT, 0);
20123 /* Redraw the parts of the glyph row ROW on window W intersecting
20124 rectangle R. R is in window-relative coordinates. Value is
20125 non-zero if mouse-face was overwritten. */
20127 static int
20128 expose_line (w, row, r)
20129 struct window *w;
20130 struct glyph_row *row;
20131 XRectangle *r;
20133 xassert (row->enabled_p);
20135 if (row->mode_line_p || w->pseudo_window_p)
20136 draw_glyphs (w, 0, row, TEXT_AREA,
20137 0, row->used[TEXT_AREA],
20138 DRAW_NORMAL_TEXT, 0);
20139 else
20141 if (row->used[LEFT_MARGIN_AREA])
20142 expose_area (w, row, r, LEFT_MARGIN_AREA);
20143 if (row->used[TEXT_AREA])
20144 expose_area (w, row, r, TEXT_AREA);
20145 if (row->used[RIGHT_MARGIN_AREA])
20146 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20147 draw_row_fringe_bitmaps (w, row);
20150 return row->mouse_face_p;
20154 /* Redraw those parts of glyphs rows during expose event handling that
20155 overlap other rows. Redrawing of an exposed line writes over parts
20156 of lines overlapping that exposed line; this function fixes that.
20158 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20159 row in W's current matrix that is exposed and overlaps other rows.
20160 LAST_OVERLAPPING_ROW is the last such row. */
20162 static void
20163 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20164 struct window *w;
20165 struct glyph_row *first_overlapping_row;
20166 struct glyph_row *last_overlapping_row;
20168 struct glyph_row *row;
20170 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20171 if (row->overlapping_p)
20173 xassert (row->enabled_p && !row->mode_line_p);
20175 if (row->used[LEFT_MARGIN_AREA])
20176 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20178 if (row->used[TEXT_AREA])
20179 x_fix_overlapping_area (w, row, TEXT_AREA);
20181 if (row->used[RIGHT_MARGIN_AREA])
20182 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20187 /* Return non-zero if W's cursor intersects rectangle R. */
20189 static int
20190 phys_cursor_in_rect_p (w, r)
20191 struct window *w;
20192 XRectangle *r;
20194 XRectangle cr, result;
20195 struct glyph *cursor_glyph;
20197 cursor_glyph = get_phys_cursor_glyph (w);
20198 if (cursor_glyph)
20200 cr.x = w->phys_cursor.x;
20201 cr.y = w->phys_cursor.y;
20202 cr.width = cursor_glyph->pixel_width;
20203 cr.height = w->phys_cursor_height;
20204 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20205 I assume the effect is the same -- and this is portable. */
20206 return x_intersect_rectangles (&cr, r, &result);
20208 else
20209 return 0;
20213 /* EXPORT:
20214 Draw a vertical window border to the right of window W if W doesn't
20215 have vertical scroll bars. */
20217 void
20218 x_draw_vertical_border (w)
20219 struct window *w;
20221 struct frame *f = XFRAME (WINDOW_FRAME (w));
20223 /* Redraw borders between horizontally adjacent windows. Don't
20224 do it for frames with vertical scroll bars because either the
20225 right scroll bar of a window, or the left scroll bar of its
20226 neighbor will suffice as a border. */
20227 if (!WINDOW_RIGHTMOST_P (w)
20228 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
20230 int x0, x1, y0, y1;
20232 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20233 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
20234 y1 -= 1;
20236 rif->draw_vertical_window_border (w, x1, y0, y1);
20241 /* Redraw the part of window W intersection rectangle FR. Pixel
20242 coordinates in FR are frame-relative. Call this function with
20243 input blocked. Value is non-zero if the exposure overwrites
20244 mouse-face. */
20246 static int
20247 expose_window (w, fr)
20248 struct window *w;
20249 XRectangle *fr;
20251 struct frame *f = XFRAME (w->frame);
20252 XRectangle wr, r;
20253 int mouse_face_overwritten_p = 0;
20255 /* If window is not yet fully initialized, do nothing. This can
20256 happen when toolkit scroll bars are used and a window is split.
20257 Reconfiguring the scroll bar will generate an expose for a newly
20258 created window. */
20259 if (w->current_matrix == NULL)
20260 return 0;
20262 /* When we're currently updating the window, display and current
20263 matrix usually don't agree. Arrange for a thorough display
20264 later. */
20265 if (w == updated_window)
20267 SET_FRAME_GARBAGED (f);
20268 return 0;
20271 /* Frame-relative pixel rectangle of W. */
20272 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
20273 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
20274 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
20275 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
20277 if (x_intersect_rectangles (fr, &wr, &r))
20279 int yb = window_text_bottom_y (w);
20280 struct glyph_row *row;
20281 int cursor_cleared_p;
20282 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20284 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20285 r.x, r.y, r.width, r.height));
20287 /* Convert to window coordinates. */
20288 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
20289 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
20291 /* Turn off the cursor. */
20292 if (!w->pseudo_window_p
20293 && phys_cursor_in_rect_p (w, &r))
20295 x_clear_cursor (w);
20296 cursor_cleared_p = 1;
20298 else
20299 cursor_cleared_p = 0;
20301 /* Update lines intersecting rectangle R. */
20302 first_overlapping_row = last_overlapping_row = NULL;
20303 for (row = w->current_matrix->rows;
20304 row->enabled_p;
20305 ++row)
20307 int y0 = row->y;
20308 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20310 if ((y0 >= r.y && y0 < r.y + r.height)
20311 || (y1 > r.y && y1 < r.y + r.height)
20312 || (r.y >= y0 && r.y < y1)
20313 || (r.y + r.height > y0 && r.y + r.height < y1))
20315 if (row->overlapping_p)
20317 if (first_overlapping_row == NULL)
20318 first_overlapping_row = row;
20319 last_overlapping_row = row;
20322 if (expose_line (w, row, &r))
20323 mouse_face_overwritten_p = 1;
20326 if (y1 >= yb)
20327 break;
20330 /* Display the mode line if there is one. */
20331 if (WINDOW_WANTS_MODELINE_P (w)
20332 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20333 row->enabled_p)
20334 && row->y < r.y + r.height)
20336 if (expose_line (w, row, &r))
20337 mouse_face_overwritten_p = 1;
20340 if (!w->pseudo_window_p)
20342 /* Fix the display of overlapping rows. */
20343 if (first_overlapping_row)
20344 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20346 /* Draw border between windows. */
20347 x_draw_vertical_border (w);
20349 /* Turn the cursor on again. */
20350 if (cursor_cleared_p)
20351 update_window_cursor (w, 1);
20355 #ifdef HAVE_CARBON
20356 /* Display scroll bar for this window. */
20357 if (!NILP (w->vertical_scroll_bar))
20359 /* ++KFS:
20360 If this doesn't work here (maybe some header files are missing),
20361 make a function in macterm.c and call it to do the job! */
20362 ControlHandle ch
20363 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20365 Draw1Control (ch);
20367 #endif
20369 return mouse_face_overwritten_p;
20374 /* Redraw (parts) of all windows in the window tree rooted at W that
20375 intersect R. R contains frame pixel coordinates. Value is
20376 non-zero if the exposure overwrites mouse-face. */
20378 static int
20379 expose_window_tree (w, r)
20380 struct window *w;
20381 XRectangle *r;
20383 struct frame *f = XFRAME (w->frame);
20384 int mouse_face_overwritten_p = 0;
20386 while (w && !FRAME_GARBAGED_P (f))
20388 if (!NILP (w->hchild))
20389 mouse_face_overwritten_p
20390 |= expose_window_tree (XWINDOW (w->hchild), r);
20391 else if (!NILP (w->vchild))
20392 mouse_face_overwritten_p
20393 |= expose_window_tree (XWINDOW (w->vchild), r);
20394 else
20395 mouse_face_overwritten_p |= expose_window (w, r);
20397 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20400 return mouse_face_overwritten_p;
20404 /* EXPORT:
20405 Redisplay an exposed area of frame F. X and Y are the upper-left
20406 corner of the exposed rectangle. W and H are width and height of
20407 the exposed area. All are pixel values. W or H zero means redraw
20408 the entire frame. */
20410 void
20411 expose_frame (f, x, y, w, h)
20412 struct frame *f;
20413 int x, y, w, h;
20415 XRectangle r;
20416 int mouse_face_overwritten_p = 0;
20418 TRACE ((stderr, "expose_frame "));
20420 /* No need to redraw if frame will be redrawn soon. */
20421 if (FRAME_GARBAGED_P (f))
20423 TRACE ((stderr, " garbaged\n"));
20424 return;
20427 #ifdef HAVE_CARBON
20428 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20429 or deactivated here, for unknown reasons, activated scroll bars
20430 are shown in deactivated frames in some instances. */
20431 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20432 activate_scroll_bars (f);
20433 else
20434 deactivate_scroll_bars (f);
20435 #endif
20437 /* If basic faces haven't been realized yet, there is no point in
20438 trying to redraw anything. This can happen when we get an expose
20439 event while Emacs is starting, e.g. by moving another window. */
20440 if (FRAME_FACE_CACHE (f) == NULL
20441 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20443 TRACE ((stderr, " no faces\n"));
20444 return;
20447 if (w == 0 || h == 0)
20449 r.x = r.y = 0;
20450 r.width = CANON_X_UNIT (f) * f->width;
20451 r.height = CANON_Y_UNIT (f) * f->height;
20453 else
20455 r.x = x;
20456 r.y = y;
20457 r.width = w;
20458 r.height = h;
20461 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20462 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20464 if (WINDOWP (f->tool_bar_window))
20465 mouse_face_overwritten_p
20466 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20468 #ifdef HAVE_X_WINDOWS
20469 #ifndef MSDOS
20470 #ifndef USE_X_TOOLKIT
20471 if (WINDOWP (f->menu_bar_window))
20472 mouse_face_overwritten_p
20473 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20474 #endif /* not USE_X_TOOLKIT */
20475 #endif
20476 #endif
20478 /* Some window managers support a focus-follows-mouse style with
20479 delayed raising of frames. Imagine a partially obscured frame,
20480 and moving the mouse into partially obscured mouse-face on that
20481 frame. The visible part of the mouse-face will be highlighted,
20482 then the WM raises the obscured frame. With at least one WM, KDE
20483 2.1, Emacs is not getting any event for the raising of the frame
20484 (even tried with SubstructureRedirectMask), only Expose events.
20485 These expose events will draw text normally, i.e. not
20486 highlighted. Which means we must redo the highlight here.
20487 Subsume it under ``we love X''. --gerd 2001-08-15 */
20488 /* Included in Windows version because Windows most likely does not
20489 do the right thing if any third party tool offers
20490 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20491 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20493 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20494 if (f == dpyinfo->mouse_face_mouse_frame)
20496 int x = dpyinfo->mouse_face_mouse_x;
20497 int y = dpyinfo->mouse_face_mouse_y;
20498 clear_mouse_face (dpyinfo);
20499 note_mouse_highlight (f, x, y);
20505 /* EXPORT:
20506 Determine the intersection of two rectangles R1 and R2. Return
20507 the intersection in *RESULT. Value is non-zero if RESULT is not
20508 empty. */
20511 x_intersect_rectangles (r1, r2, result)
20512 XRectangle *r1, *r2, *result;
20514 XRectangle *left, *right;
20515 XRectangle *upper, *lower;
20516 int intersection_p = 0;
20518 /* Rearrange so that R1 is the left-most rectangle. */
20519 if (r1->x < r2->x)
20520 left = r1, right = r2;
20521 else
20522 left = r2, right = r1;
20524 /* X0 of the intersection is right.x0, if this is inside R1,
20525 otherwise there is no intersection. */
20526 if (right->x <= left->x + left->width)
20528 result->x = right->x;
20530 /* The right end of the intersection is the minimum of the
20531 the right ends of left and right. */
20532 result->width = (min (left->x + left->width, right->x + right->width)
20533 - result->x);
20535 /* Same game for Y. */
20536 if (r1->y < r2->y)
20537 upper = r1, lower = r2;
20538 else
20539 upper = r2, lower = r1;
20541 /* The upper end of the intersection is lower.y0, if this is inside
20542 of upper. Otherwise, there is no intersection. */
20543 if (lower->y <= upper->y + upper->height)
20545 result->y = lower->y;
20547 /* The lower end of the intersection is the minimum of the lower
20548 ends of upper and lower. */
20549 result->height = (min (lower->y + lower->height,
20550 upper->y + upper->height)
20551 - result->y);
20552 intersection_p = 1;
20556 return intersection_p;
20559 #endif /* HAVE_WINDOW_SYSTEM */
20562 /***********************************************************************
20563 Initialization
20564 ***********************************************************************/
20566 void
20567 syms_of_xdisp ()
20569 Vwith_echo_area_save_vector = Qnil;
20570 staticpro (&Vwith_echo_area_save_vector);
20572 Vmessage_stack = Qnil;
20573 staticpro (&Vmessage_stack);
20575 Qinhibit_redisplay = intern ("inhibit-redisplay");
20576 staticpro (&Qinhibit_redisplay);
20578 message_dolog_marker1 = Fmake_marker ();
20579 staticpro (&message_dolog_marker1);
20580 message_dolog_marker2 = Fmake_marker ();
20581 staticpro (&message_dolog_marker2);
20582 message_dolog_marker3 = Fmake_marker ();
20583 staticpro (&message_dolog_marker3);
20585 #if GLYPH_DEBUG
20586 defsubr (&Sdump_frame_glyph_matrix);
20587 defsubr (&Sdump_glyph_matrix);
20588 defsubr (&Sdump_glyph_row);
20589 defsubr (&Sdump_tool_bar_row);
20590 defsubr (&Strace_redisplay);
20591 defsubr (&Strace_to_stderr);
20592 #endif
20593 #ifdef HAVE_WINDOW_SYSTEM
20594 defsubr (&Stool_bar_lines_needed);
20595 #endif
20596 defsubr (&Sformat_mode_line);
20598 staticpro (&Qmenu_bar_update_hook);
20599 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20601 staticpro (&Qoverriding_terminal_local_map);
20602 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20604 staticpro (&Qoverriding_local_map);
20605 Qoverriding_local_map = intern ("overriding-local-map");
20607 staticpro (&Qwindow_scroll_functions);
20608 Qwindow_scroll_functions = intern ("window-scroll-functions");
20610 staticpro (&Qredisplay_end_trigger_functions);
20611 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20613 staticpro (&Qinhibit_point_motion_hooks);
20614 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20616 QCdata = intern (":data");
20617 staticpro (&QCdata);
20618 Qdisplay = intern ("display");
20619 staticpro (&Qdisplay);
20620 Qspace_width = intern ("space-width");
20621 staticpro (&Qspace_width);
20622 Qraise = intern ("raise");
20623 staticpro (&Qraise);
20624 Qspace = intern ("space");
20625 staticpro (&Qspace);
20626 Qmargin = intern ("margin");
20627 staticpro (&Qmargin);
20628 Qleft_margin = intern ("left-margin");
20629 staticpro (&Qleft_margin);
20630 Qright_margin = intern ("right-margin");
20631 staticpro (&Qright_margin);
20632 Qalign_to = intern ("align-to");
20633 staticpro (&Qalign_to);
20634 QCalign_to = intern (":align-to");
20635 staticpro (&QCalign_to);
20636 Qrelative_width = intern ("relative-width");
20637 staticpro (&Qrelative_width);
20638 QCrelative_width = intern (":relative-width");
20639 staticpro (&QCrelative_width);
20640 QCrelative_height = intern (":relative-height");
20641 staticpro (&QCrelative_height);
20642 QCeval = intern (":eval");
20643 staticpro (&QCeval);
20644 QCpropertize = intern (":propertize");
20645 staticpro (&QCpropertize);
20646 QCfile = intern (":file");
20647 staticpro (&QCfile);
20648 Qfontified = intern ("fontified");
20649 staticpro (&Qfontified);
20650 Qfontification_functions = intern ("fontification-functions");
20651 staticpro (&Qfontification_functions);
20652 Qtrailing_whitespace = intern ("trailing-whitespace");
20653 staticpro (&Qtrailing_whitespace);
20654 Qimage = intern ("image");
20655 staticpro (&Qimage);
20656 Qmessage_truncate_lines = intern ("message-truncate-lines");
20657 staticpro (&Qmessage_truncate_lines);
20658 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20659 staticpro (&Qcursor_in_non_selected_windows);
20660 Qgrow_only = intern ("grow-only");
20661 staticpro (&Qgrow_only);
20662 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20663 staticpro (&Qinhibit_menubar_update);
20664 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20665 staticpro (&Qinhibit_eval_during_redisplay);
20666 Qposition = intern ("position");
20667 staticpro (&Qposition);
20668 Qbuffer_position = intern ("buffer-position");
20669 staticpro (&Qbuffer_position);
20670 Qobject = intern ("object");
20671 staticpro (&Qobject);
20672 Qbar = intern ("bar");
20673 staticpro (&Qbar);
20674 Qhbar = intern ("hbar");
20675 staticpro (&Qhbar);
20676 Qbox = intern ("box");
20677 staticpro (&Qbox);
20678 Qhollow = intern ("hollow");
20679 staticpro (&Qhollow);
20680 Qrisky_local_variable = intern ("risky-local-variable");
20681 staticpro (&Qrisky_local_variable);
20682 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20683 staticpro (&Qinhibit_free_realized_faces);
20685 list_of_error = Fcons (intern ("error"), Qnil);
20686 staticpro (&list_of_error);
20688 last_arrow_position = Qnil;
20689 last_arrow_string = Qnil;
20690 staticpro (&last_arrow_position);
20691 staticpro (&last_arrow_string);
20693 echo_buffer[0] = echo_buffer[1] = Qnil;
20694 staticpro (&echo_buffer[0]);
20695 staticpro (&echo_buffer[1]);
20697 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20698 staticpro (&echo_area_buffer[0]);
20699 staticpro (&echo_area_buffer[1]);
20701 Vmessages_buffer_name = build_string ("*Messages*");
20702 staticpro (&Vmessages_buffer_name);
20704 mode_line_proptrans_alist = Qnil;
20705 staticpro (&mode_line_proptrans_alist);
20707 mode_line_string_list = Qnil;
20708 staticpro (&mode_line_string_list);
20710 help_echo_string = Qnil;
20711 staticpro (&help_echo_string);
20712 help_echo_object = Qnil;
20713 staticpro (&help_echo_object);
20714 help_echo_window = Qnil;
20715 staticpro (&help_echo_window);
20716 previous_help_echo_string = Qnil;
20717 staticpro (&previous_help_echo_string);
20718 help_echo_pos = -1;
20720 #ifdef HAVE_WINDOW_SYSTEM
20721 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20722 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20723 For example, if a block cursor is over a tab, it will be drawn as
20724 wide as that tab on the display. */);
20725 x_stretch_cursor_p = 0;
20726 #endif
20728 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20729 doc: /* Non-nil means highlight trailing whitespace.
20730 The face used for trailing whitespace is `trailing-whitespace'. */);
20731 Vshow_trailing_whitespace = Qnil;
20733 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20734 doc: /* Non-nil means don't actually do any redisplay.
20735 This is used for internal purposes. */);
20736 Vinhibit_redisplay = Qnil;
20738 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20739 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20740 Vglobal_mode_string = Qnil;
20742 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20743 doc: /* Marker for where to display an arrow on top of the buffer text.
20744 This must be the beginning of a line in order to work.
20745 See also `overlay-arrow-string'. */);
20746 Voverlay_arrow_position = Qnil;
20748 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20749 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20750 Voverlay_arrow_string = Qnil;
20752 DEFVAR_INT ("scroll-step", &scroll_step,
20753 doc: /* *The number of lines to try scrolling a window by when point moves out.
20754 If that fails to bring point back on frame, point is centered instead.
20755 If this is zero, point is always centered after it moves off frame.
20756 If you want scrolling to always be a line at a time, you should set
20757 `scroll-conservatively' to a large value rather than set this to 1. */);
20759 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20760 doc: /* *Scroll up to this many lines, to bring point back on screen.
20761 A value of zero means to scroll the text to center point vertically
20762 in the window. */);
20763 scroll_conservatively = 0;
20765 DEFVAR_INT ("scroll-margin", &scroll_margin,
20766 doc: /* *Number of lines of margin at the top and bottom of a window.
20767 Recenter the window whenever point gets within this many lines
20768 of the top or bottom of the window. */);
20769 scroll_margin = 0;
20771 #if GLYPH_DEBUG
20772 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20773 #endif
20775 DEFVAR_BOOL ("truncate-partial-width-windows",
20776 &truncate_partial_width_windows,
20777 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20778 truncate_partial_width_windows = 1;
20780 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20781 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20782 Any other value means to use the appropriate face, `mode-line',
20783 `header-line', or `menu' respectively. */);
20784 mode_line_inverse_video = 1;
20786 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20787 doc: /* *Maximum buffer size for which line number should be displayed.
20788 If the buffer is bigger than this, the line number does not appear
20789 in the mode line. A value of nil means no limit. */);
20790 Vline_number_display_limit = Qnil;
20792 DEFVAR_INT ("line-number-display-limit-width",
20793 &line_number_display_limit_width,
20794 doc: /* *Maximum line width (in characters) for line number display.
20795 If the average length of the lines near point is bigger than this, then the
20796 line number may be omitted from the mode line. */);
20797 line_number_display_limit_width = 200;
20799 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20800 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20801 highlight_nonselected_windows = 0;
20803 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20804 doc: /* Non-nil if more than one frame is visible on this display.
20805 Minibuffer-only frames don't count, but iconified frames do.
20806 This variable is not guaranteed to be accurate except while processing
20807 `frame-title-format' and `icon-title-format'. */);
20809 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20810 doc: /* Template for displaying the title bar of visible frames.
20811 \(Assuming the window manager supports this feature.)
20812 This variable has the same structure as `mode-line-format' (which see),
20813 and is used only on frames for which no explicit name has been set
20814 \(see `modify-frame-parameters'). */);
20815 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20816 doc: /* Template for displaying the title bar of an iconified frame.
20817 \(Assuming the window manager supports this feature.)
20818 This variable has the same structure as `mode-line-format' (which see),
20819 and is used only on frames for which no explicit name has been set
20820 \(see `modify-frame-parameters'). */);
20821 Vicon_title_format
20822 = Vframe_title_format
20823 = Fcons (intern ("multiple-frames"),
20824 Fcons (build_string ("%b"),
20825 Fcons (Fcons (empty_string,
20826 Fcons (intern ("invocation-name"),
20827 Fcons (build_string ("@"),
20828 Fcons (intern ("system-name"),
20829 Qnil)))),
20830 Qnil)));
20832 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20833 doc: /* Maximum number of lines to keep in the message log buffer.
20834 If nil, disable message logging. If t, log messages but don't truncate
20835 the buffer when it becomes large. */);
20836 Vmessage_log_max = make_number (50);
20838 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20839 doc: /* Functions called before redisplay, if window sizes have changed.
20840 The value should be a list of functions that take one argument.
20841 Just before redisplay, for each frame, if any of its windows have changed
20842 size since the last redisplay, or have been split or deleted,
20843 all the functions in the list are called, with the frame as argument. */);
20844 Vwindow_size_change_functions = Qnil;
20846 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20847 doc: /* List of Functions to call before redisplaying a window with scrolling.
20848 Each function is called with two arguments, the window
20849 and its new display-start position. Note that the value of `window-end'
20850 is not valid when these functions are called. */);
20851 Vwindow_scroll_functions = Qnil;
20853 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20854 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20855 mouse_autoselect_window = 0;
20857 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20858 doc: /* *Non-nil means automatically resize tool-bars.
20859 This increases a tool-bar's height if not all tool-bar items are visible.
20860 It decreases a tool-bar's height when it would display blank lines
20861 otherwise. */);
20862 auto_resize_tool_bars_p = 1;
20864 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
20865 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
20866 auto_raise_tool_bar_buttons_p = 1;
20868 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
20869 doc: /* *Margin around tool-bar buttons in pixels.
20870 If an integer, use that for both horizontal and vertical margins.
20871 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
20872 HORZ specifying the horizontal margin, and VERT specifying the
20873 vertical margin. */);
20874 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
20876 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
20877 doc: /* *Relief thickness of tool-bar buttons. */);
20878 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
20880 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
20881 doc: /* List of functions to call to fontify regions of text.
20882 Each function is called with one argument POS. Functions must
20883 fontify a region starting at POS in the current buffer, and give
20884 fontified regions the property `fontified'. */);
20885 Vfontification_functions = Qnil;
20886 Fmake_variable_buffer_local (Qfontification_functions);
20888 DEFVAR_BOOL ("unibyte-display-via-language-environment",
20889 &unibyte_display_via_language_environment,
20890 doc: /* *Non-nil means display unibyte text according to language environment.
20891 Specifically this means that unibyte non-ASCII characters
20892 are displayed by converting them to the equivalent multibyte characters
20893 according to the current language environment. As a result, they are
20894 displayed according to the current fontset. */);
20895 unibyte_display_via_language_environment = 0;
20897 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
20898 doc: /* *Maximum height for resizing mini-windows.
20899 If a float, it specifies a fraction of the mini-window frame's height.
20900 If an integer, it specifies a number of lines. */);
20901 Vmax_mini_window_height = make_float (0.25);
20903 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
20904 doc: /* *How to resize mini-windows.
20905 A value of nil means don't automatically resize mini-windows.
20906 A value of t means resize them to fit the text displayed in them.
20907 A value of `grow-only', the default, means let mini-windows grow
20908 only, until their display becomes empty, at which point the windows
20909 go back to their normal size. */);
20910 Vresize_mini_windows = Qgrow_only;
20912 DEFVAR_LISP ("cursor-in-non-selected-windows",
20913 &Vcursor_in_non_selected_windows,
20914 doc: /* *Cursor type to display in non-selected windows.
20915 t means to use hollow box cursor. See `cursor-type' for other values. */);
20916 Vcursor_in_non_selected_windows = Qt;
20918 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
20919 doc: /* Alist specifying how to blink the cursor off.
20920 Each element has the form (ON-STATE . OFF-STATE). Whenever the
20921 `cursor-type' frame-parameter or variable equals ON-STATE,
20922 comparing using `equal', Emacs uses OFF-STATE to specify
20923 how to blink it off. */);
20924 Vblink_cursor_alist = Qnil;
20926 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
20927 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
20928 automatic_hscrolling_p = 1;
20930 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
20931 doc: /* *How many columns away from the window edge point is allowed to get
20932 before automatic hscrolling will horizontally scroll the window. */);
20933 hscroll_margin = 5;
20935 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
20936 doc: /* *How many columns to scroll the window when point gets too close to the edge.
20937 When point is less than `automatic-hscroll-margin' columns from the window
20938 edge, automatic hscrolling will scroll the window by the amount of columns
20939 determined by this variable. If its value is a positive integer, scroll that
20940 many columns. If it's a positive floating-point number, it specifies the
20941 fraction of the window's width to scroll. If it's nil or zero, point will be
20942 centered horizontally after the scroll. Any other value, including negative
20943 numbers, are treated as if the value were zero.
20945 Automatic hscrolling always moves point outside the scroll margin, so if
20946 point was more than scroll step columns inside the margin, the window will
20947 scroll more than the value given by the scroll step.
20949 Note that the lower bound for automatic hscrolling specified by `scroll-left'
20950 and `scroll-right' overrides this variable's effect. */);
20951 Vhscroll_step = make_number (0);
20953 DEFVAR_LISP ("image-types", &Vimage_types,
20954 doc: /* List of supported image types.
20955 Each element of the list is a symbol for a supported image type. */);
20956 Vimage_types = Qnil;
20958 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
20959 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
20960 Bind this around calls to `message' to let it take effect. */);
20961 message_truncate_lines = 0;
20963 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
20964 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
20965 Can be used to update submenus whose contents should vary. */);
20966 Vmenu_bar_update_hook = Qnil;
20968 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
20969 doc: /* Non-nil means don't update menu bars. Internal use only. */);
20970 inhibit_menubar_update = 0;
20972 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
20973 doc: /* Non-nil means don't eval Lisp during redisplay. */);
20974 inhibit_eval_during_redisplay = 0;
20976 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
20977 doc: /* Non-nil means don't free realized faces. Internal use only. */);
20978 inhibit_free_realized_faces = 0;
20980 #if GLYPH_DEBUG
20981 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
20982 doc: /* Inhibit try_window_id display optimization. */);
20983 inhibit_try_window_id = 0;
20985 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
20986 doc: /* Inhibit try_window_reusing display optimization. */);
20987 inhibit_try_window_reusing = 0;
20989 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
20990 doc: /* Inhibit try_cursor_movement display optimization. */);
20991 inhibit_try_cursor_movement = 0;
20992 #endif /* GLYPH_DEBUG */
20996 /* Initialize this module when Emacs starts. */
20998 void
20999 init_xdisp ()
21001 Lisp_Object root_window;
21002 struct window *mini_w;
21004 current_header_line_height = current_mode_line_height = -1;
21006 CHARPOS (this_line_start_pos) = 0;
21008 mini_w = XWINDOW (minibuf_window);
21009 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21011 if (!noninteractive)
21013 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21014 int i;
21016 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
21017 set_window_height (root_window,
21018 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
21020 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
21021 set_window_height (minibuf_window, 1, 0);
21023 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
21024 mini_w->width = make_number (FRAME_WIDTH (f));
21026 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21027 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21028 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21030 /* The default ellipsis glyphs `...'. */
21031 for (i = 0; i < 3; ++i)
21032 default_invis_vector[i] = make_number ('.');
21036 /* Allocate the buffer for frame titles.
21037 Also used for `format-mode-line'. */
21038 int size = 100;
21039 frame_title_buf = (char *) xmalloc (size);
21040 frame_title_buf_end = frame_title_buf + size;
21041 frame_title_ptr = NULL;
21044 help_echo_showing_p = 0;