(map-keymap): Definition deleted.
[emacs.git] / src / xdisp.c
blob2620485bbe3da353f61d16a120b2f83c7a113dbd
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
207 #define INFINITY 10000000
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
215 extern int interrupt_input;
216 extern int command_loop_level;
218 extern Lisp_Object do_mouse_tracking;
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
248 Lisp_Object Qrisky_local_variable;
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
253 /* Functions called to fontify regions of text. */
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
265 int auto_raise_tool_bar_buttons_p;
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
269 int make_cursor_line_fully_visible_p;
271 /* Margin around tool bar buttons in pixels. */
273 Lisp_Object Vtool_bar_button_margin;
275 /* Thickness of shadow to draw around tool bar buttons. */
277 EMACS_INT tool_bar_button_relief;
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
282 int auto_resize_tool_bars_p;
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
288 int x_stretch_cursor_p;
290 /* Non-nil means don't actually do any redisplay. */
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296 int inhibit_eval_during_redisplay;
298 /* Names of text properties relevant for redisplay. */
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
303 /* Symbols used in text property values. */
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height, Qtotal;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
317 /* Non-nil means highlight trailing whitespace. */
319 Lisp_Object Vshow_trailing_whitespace;
321 #ifdef HAVE_WINDOW_SYSTEM
322 extern Lisp_Object Voverflow_newline_into_fringe;
324 /* Test if overflow newline into fringe. Called with iterator IT
325 at or past right window margin, and with IT->current_x set. */
327 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
328 (!NILP (Voverflow_newline_into_fringe) \
329 && FRAME_WINDOW_P (it->f) \
330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
331 && it->current_x == it->last_visible_x)
333 #endif /* HAVE_WINDOW_SYSTEM */
335 /* Non-nil means show the text cursor in void text areas
336 i.e. in blank areas after eol and eob. This used to be
337 the default in 21.3. */
339 Lisp_Object Vvoid_text_area_pointer;
341 /* Name of the face used to highlight trailing whitespace. */
343 Lisp_Object Qtrailing_whitespace;
345 /* The symbol `image' which is the car of the lists used to represent
346 images in Lisp. */
348 Lisp_Object Qimage;
350 /* The image map types. */
351 Lisp_Object QCmap, QCpointer;
352 Lisp_Object Qrect, Qcircle, Qpoly;
354 /* Non-zero means print newline to stdout before next mini-buffer
355 message. */
357 int noninteractive_need_newline;
359 /* Non-zero means print newline to message log before next message. */
361 static int message_log_need_newline;
363 /* Three markers that message_dolog uses.
364 It could allocate them itself, but that causes trouble
365 in handling memory-full errors. */
366 static Lisp_Object message_dolog_marker1;
367 static Lisp_Object message_dolog_marker2;
368 static Lisp_Object message_dolog_marker3;
370 /* The buffer position of the first character appearing entirely or
371 partially on the line of the selected window which contains the
372 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
373 redisplay optimization in redisplay_internal. */
375 static struct text_pos this_line_start_pos;
377 /* Number of characters past the end of the line above, including the
378 terminating newline. */
380 static struct text_pos this_line_end_pos;
382 /* The vertical positions and the height of this line. */
384 static int this_line_vpos;
385 static int this_line_y;
386 static int this_line_pixel_height;
388 /* X position at which this display line starts. Usually zero;
389 negative if first character is partially visible. */
391 static int this_line_start_x;
393 /* Buffer that this_line_.* variables are referring to. */
395 static struct buffer *this_line_buffer;
397 /* Nonzero means truncate lines in all windows less wide than the
398 frame. */
400 int truncate_partial_width_windows;
402 /* A flag to control how to display unibyte 8-bit character. */
404 int unibyte_display_via_language_environment;
406 /* Nonzero means we have more than one non-mini-buffer-only frame.
407 Not guaranteed to be accurate except while parsing
408 frame-title-format. */
410 int multiple_frames;
412 Lisp_Object Vglobal_mode_string;
415 /* List of variables (symbols) which hold markers for overlay arrows.
416 The symbols on this list are examined during redisplay to determine
417 where to display overlay arrows. */
419 Lisp_Object Voverlay_arrow_variable_list;
421 /* Marker for where to display an arrow on top of the buffer text. */
423 Lisp_Object Voverlay_arrow_position;
425 /* String to display for the arrow. Only used on terminal frames. */
427 Lisp_Object Voverlay_arrow_string;
429 /* Values of those variables at last redisplay are stored as
430 properties on `overlay-arrow-position' symbol. However, if
431 Voverlay_arrow_position is a marker, last-arrow-position is its
432 numerical position. */
434 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
436 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
437 properties on a symbol in overlay-arrow-variable-list. */
439 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
441 /* Like mode-line-format, but for the title bar on a visible frame. */
443 Lisp_Object Vframe_title_format;
445 /* Like mode-line-format, but for the title bar on an iconified frame. */
447 Lisp_Object Vicon_title_format;
449 /* List of functions to call when a window's size changes. These
450 functions get one arg, a frame on which one or more windows' sizes
451 have changed. */
453 static Lisp_Object Vwindow_size_change_functions;
455 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
457 /* Nonzero if overlay arrow has been displayed once in this window. */
459 static int overlay_arrow_seen;
461 /* Nonzero means highlight the region even in nonselected windows. */
463 int highlight_nonselected_windows;
465 /* If cursor motion alone moves point off frame, try scrolling this
466 many lines up or down if that will bring it back. */
468 static EMACS_INT scroll_step;
470 /* Nonzero means scroll just far enough to bring point back on the
471 screen, when appropriate. */
473 static EMACS_INT scroll_conservatively;
475 /* Recenter the window whenever point gets within this many lines of
476 the top or bottom of the window. This value is translated into a
477 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
478 that there is really a fixed pixel height scroll margin. */
480 EMACS_INT scroll_margin;
482 /* Number of windows showing the buffer of the selected window (or
483 another buffer with the same base buffer). keyboard.c refers to
484 this. */
486 int buffer_shared;
488 /* Vector containing glyphs for an ellipsis `...'. */
490 static Lisp_Object default_invis_vector[3];
492 /* Zero means display the mode-line/header-line/menu-bar in the default face
493 (this slightly odd definition is for compatibility with previous versions
494 of emacs), non-zero means display them using their respective faces.
496 This variable is deprecated. */
498 int mode_line_inverse_video;
500 /* Prompt to display in front of the mini-buffer contents. */
502 Lisp_Object minibuf_prompt;
504 /* Width of current mini-buffer prompt. Only set after display_line
505 of the line that contains the prompt. */
507 int minibuf_prompt_width;
509 /* This is the window where the echo area message was displayed. It
510 is always a mini-buffer window, but it may not be the same window
511 currently active as a mini-buffer. */
513 Lisp_Object echo_area_window;
515 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
516 pushes the current message and the value of
517 message_enable_multibyte on the stack, the function restore_message
518 pops the stack and displays MESSAGE again. */
520 Lisp_Object Vmessage_stack;
522 /* Nonzero means multibyte characters were enabled when the echo area
523 message was specified. */
525 int message_enable_multibyte;
527 /* Nonzero if we should redraw the mode lines on the next redisplay. */
529 int update_mode_lines;
531 /* Nonzero if window sizes or contents have changed since last
532 redisplay that finished. */
534 int windows_or_buffers_changed;
536 /* Nonzero means a frame's cursor type has been changed. */
538 int cursor_type_changed;
540 /* Nonzero after display_mode_line if %l was used and it displayed a
541 line number. */
543 int line_number_displayed;
545 /* Maximum buffer size for which to display line numbers. */
547 Lisp_Object Vline_number_display_limit;
549 /* Line width to consider when repositioning for line number display. */
551 static EMACS_INT line_number_display_limit_width;
553 /* Number of lines to keep in the message log buffer. t means
554 infinite. nil means don't log at all. */
556 Lisp_Object Vmessage_log_max;
558 /* The name of the *Messages* buffer, a string. */
560 static Lisp_Object Vmessages_buffer_name;
562 /* Current, index 0, and last displayed echo area message. Either
563 buffers from echo_buffers, or nil to indicate no message. */
565 Lisp_Object echo_area_buffer[2];
567 /* The buffers referenced from echo_area_buffer. */
569 static Lisp_Object echo_buffer[2];
571 /* A vector saved used in with_area_buffer to reduce consing. */
573 static Lisp_Object Vwith_echo_area_save_vector;
575 /* Non-zero means display_echo_area should display the last echo area
576 message again. Set by redisplay_preserve_echo_area. */
578 static int display_last_displayed_message_p;
580 /* Nonzero if echo area is being used by print; zero if being used by
581 message. */
583 int message_buf_print;
585 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
587 Lisp_Object Qinhibit_menubar_update;
588 int inhibit_menubar_update;
590 /* Maximum height for resizing mini-windows. Either a float
591 specifying a fraction of the available height, or an integer
592 specifying a number of lines. */
594 Lisp_Object Vmax_mini_window_height;
596 /* Non-zero means messages should be displayed with truncated
597 lines instead of being continued. */
599 int message_truncate_lines;
600 Lisp_Object Qmessage_truncate_lines;
602 /* Set to 1 in clear_message to make redisplay_internal aware
603 of an emptied echo area. */
605 static int message_cleared_p;
607 /* Non-zero means we want a hollow cursor in windows that are not
608 selected. Zero means there's no cursor in such windows. */
610 Lisp_Object Vcursor_in_non_selected_windows;
611 Lisp_Object Qcursor_in_non_selected_windows;
613 /* How to blink the default frame cursor off. */
614 Lisp_Object Vblink_cursor_alist;
616 /* A scratch glyph row with contents used for generating truncation
617 glyphs. Also used in direct_output_for_insert. */
619 #define MAX_SCRATCH_GLYPHS 100
620 struct glyph_row scratch_glyph_row;
621 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
623 /* Ascent and height of the last line processed by move_it_to. */
625 static int last_max_ascent, last_height;
627 /* Non-zero if there's a help-echo in the echo area. */
629 int help_echo_showing_p;
631 /* If >= 0, computed, exact values of mode-line and header-line height
632 to use in the macros CURRENT_MODE_LINE_HEIGHT and
633 CURRENT_HEADER_LINE_HEIGHT. */
635 int current_mode_line_height, current_header_line_height;
637 /* The maximum distance to look ahead for text properties. Values
638 that are too small let us call compute_char_face and similar
639 functions too often which is expensive. Values that are too large
640 let us call compute_char_face and alike too often because we
641 might not be interested in text properties that far away. */
643 #define TEXT_PROP_DISTANCE_LIMIT 100
645 #if GLYPH_DEBUG
647 /* Variables to turn off display optimizations from Lisp. */
649 int inhibit_try_window_id, inhibit_try_window_reusing;
650 int inhibit_try_cursor_movement;
652 /* Non-zero means print traces of redisplay if compiled with
653 GLYPH_DEBUG != 0. */
655 int trace_redisplay_p;
657 #endif /* GLYPH_DEBUG */
659 #ifdef DEBUG_TRACE_MOVE
660 /* Non-zero means trace with TRACE_MOVE to stderr. */
661 int trace_move;
663 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
664 #else
665 #define TRACE_MOVE(x) (void) 0
666 #endif
668 /* Non-zero means automatically scroll windows horizontally to make
669 point visible. */
671 int automatic_hscrolling_p;
673 /* How close to the margin can point get before the window is scrolled
674 horizontally. */
675 EMACS_INT hscroll_margin;
677 /* How much to scroll horizontally when point is inside the above margin. */
678 Lisp_Object Vhscroll_step;
680 /* The variable `resize-mini-windows'. If nil, don't resize
681 mini-windows. If t, always resize them to fit the text they
682 display. If `grow-only', let mini-windows grow only until they
683 become empty. */
685 Lisp_Object Vresize_mini_windows;
687 /* Buffer being redisplayed -- for redisplay_window_error. */
689 struct buffer *displayed_buffer;
691 /* Value returned from text property handlers (see below). */
693 enum prop_handled
695 HANDLED_NORMALLY,
696 HANDLED_RECOMPUTE_PROPS,
697 HANDLED_OVERLAY_STRING_CONSUMED,
698 HANDLED_RETURN
701 /* A description of text properties that redisplay is interested
702 in. */
704 struct props
706 /* The name of the property. */
707 Lisp_Object *name;
709 /* A unique index for the property. */
710 enum prop_idx idx;
712 /* A handler function called to set up iterator IT from the property
713 at IT's current position. Value is used to steer handle_stop. */
714 enum prop_handled (*handler) P_ ((struct it *it));
717 static enum prop_handled handle_face_prop P_ ((struct it *));
718 static enum prop_handled handle_invisible_prop P_ ((struct it *));
719 static enum prop_handled handle_display_prop P_ ((struct it *));
720 static enum prop_handled handle_composition_prop P_ ((struct it *));
721 static enum prop_handled handle_overlay_change P_ ((struct it *));
722 static enum prop_handled handle_fontified_prop P_ ((struct it *));
724 /* Properties handled by iterators. */
726 static struct props it_props[] =
728 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
729 /* Handle `face' before `display' because some sub-properties of
730 `display' need to know the face. */
731 {&Qface, FACE_PROP_IDX, handle_face_prop},
732 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
733 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
734 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
735 {NULL, 0, NULL}
738 /* Value is the position described by X. If X is a marker, value is
739 the marker_position of X. Otherwise, value is X. */
741 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
743 /* Enumeration returned by some move_it_.* functions internally. */
745 enum move_it_result
747 /* Not used. Undefined value. */
748 MOVE_UNDEFINED,
750 /* Move ended at the requested buffer position or ZV. */
751 MOVE_POS_MATCH_OR_ZV,
753 /* Move ended at the requested X pixel position. */
754 MOVE_X_REACHED,
756 /* Move within a line ended at the end of a line that must be
757 continued. */
758 MOVE_LINE_CONTINUED,
760 /* Move within a line ended at the end of a line that would
761 be displayed truncated. */
762 MOVE_LINE_TRUNCATED,
764 /* Move within a line ended at a line end. */
765 MOVE_NEWLINE_OR_CR
768 /* This counter is used to clear the face cache every once in a while
769 in redisplay_internal. It is incremented for each redisplay.
770 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
771 cleared. */
773 #define CLEAR_FACE_CACHE_COUNT 500
774 static int clear_face_cache_count;
776 /* Record the previous terminal frame we displayed. */
778 static struct frame *previous_terminal_frame;
780 /* Non-zero while redisplay_internal is in progress. */
782 int redisplaying_p;
784 /* Non-zero means don't free realized faces. Bound while freeing
785 realized faces is dangerous because glyph matrices might still
786 reference them. */
788 int inhibit_free_realized_faces;
789 Lisp_Object Qinhibit_free_realized_faces;
791 /* If a string, XTread_socket generates an event to display that string.
792 (The display is done in read_char.) */
794 Lisp_Object help_echo_string;
795 Lisp_Object help_echo_window;
796 Lisp_Object help_echo_object;
797 int help_echo_pos;
799 /* Temporary variable for XTread_socket. */
801 Lisp_Object previous_help_echo_string;
803 /* Null glyph slice */
805 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
808 /* Function prototypes. */
810 static void setup_for_ellipsis P_ ((struct it *));
811 static void mark_window_display_accurate_1 P_ ((struct window *, int));
812 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
813 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
814 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
815 static int redisplay_mode_lines P_ ((Lisp_Object, int));
816 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
818 #if 0
819 static int invisible_text_between_p P_ ((struct it *, int, int));
820 #endif
822 static int next_element_from_ellipsis P_ ((struct it *));
823 static void pint2str P_ ((char *, int, int));
824 static void pint2hrstr P_ ((char *, int, int));
825 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
826 struct text_pos));
827 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
828 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
829 static void store_frame_title_char P_ ((char));
830 static int store_frame_title P_ ((const unsigned char *, int, int));
831 static void x_consider_frame_title P_ ((Lisp_Object));
832 static void handle_stop P_ ((struct it *));
833 static int tool_bar_lines_needed P_ ((struct frame *));
834 static int single_display_prop_intangible_p P_ ((Lisp_Object));
835 static void ensure_echo_area_buffers P_ ((void));
836 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
837 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
838 static int with_echo_area_buffer P_ ((struct window *, int,
839 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
840 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static void clear_garbaged_frames P_ ((void));
842 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
843 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
845 static int display_echo_area P_ ((struct window *));
846 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
847 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
849 static int string_char_and_length P_ ((const unsigned char *, int, int *));
850 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
851 struct text_pos));
852 static int compute_window_start_on_continuation_line P_ ((struct window *));
853 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
854 static void insert_left_trunc_glyphs P_ ((struct it *));
855 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
856 Lisp_Object));
857 static void extend_face_to_end_of_line P_ ((struct it *));
858 static int append_space_for_newline P_ ((struct it *, int));
859 static int make_cursor_line_fully_visible P_ ((struct window *, int));
860 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
861 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
862 static int trailing_whitespace_p P_ ((int));
863 static int message_log_check_duplicate P_ ((int, int, int, int));
864 static void push_it P_ ((struct it *));
865 static void pop_it P_ ((struct it *));
866 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
867 static void select_frame_for_redisplay P_ ((Lisp_Object));
868 static void redisplay_internal P_ ((int));
869 static int echo_area_display P_ ((int));
870 static void redisplay_windows P_ ((Lisp_Object));
871 static void redisplay_window P_ ((Lisp_Object, int));
872 static Lisp_Object redisplay_window_error ();
873 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
874 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
875 static void update_menu_bar P_ ((struct frame *, int));
876 static int try_window_reusing_current_matrix P_ ((struct window *));
877 static int try_window_id P_ ((struct window *));
878 static int display_line P_ ((struct it *));
879 static int display_mode_lines P_ ((struct window *));
880 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
881 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
882 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
883 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
884 static void display_menu_bar P_ ((struct window *));
885 static int display_count_lines P_ ((int, int, int, int, int *));
886 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
887 int, int, struct it *, int, int, int, int));
888 static void compute_line_metrics P_ ((struct it *));
889 static void run_redisplay_end_trigger_hook P_ ((struct it *));
890 static int get_overlay_strings P_ ((struct it *, int));
891 static void next_overlay_string P_ ((struct it *));
892 static void reseat P_ ((struct it *, struct text_pos, int));
893 static void reseat_1 P_ ((struct it *, struct text_pos, int));
894 static void back_to_previous_visible_line_start P_ ((struct it *));
895 void reseat_at_previous_visible_line_start P_ ((struct it *));
896 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
897 static int next_element_from_display_vector P_ ((struct it *));
898 static int next_element_from_string P_ ((struct it *));
899 static int next_element_from_c_string P_ ((struct it *));
900 static int next_element_from_buffer P_ ((struct it *));
901 static int next_element_from_composition P_ ((struct it *));
902 static int next_element_from_image P_ ((struct it *));
903 static int next_element_from_stretch P_ ((struct it *));
904 static void load_overlay_strings P_ ((struct it *, int));
905 static int init_from_display_pos P_ ((struct it *, struct window *,
906 struct display_pos *));
907 static void reseat_to_string P_ ((struct it *, unsigned char *,
908 Lisp_Object, int, int, int, int));
909 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
910 int, int, int));
911 void move_it_vertically_backward P_ ((struct it *, int));
912 static void init_to_row_start P_ ((struct it *, struct window *,
913 struct glyph_row *));
914 static int init_to_row_end P_ ((struct it *, struct window *,
915 struct glyph_row *));
916 static void back_to_previous_line_start P_ ((struct it *));
917 static int forward_to_next_line_start P_ ((struct it *, int *));
918 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
919 Lisp_Object, int));
920 static struct text_pos string_pos P_ ((int, Lisp_Object));
921 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
922 static int number_of_chars P_ ((unsigned char *, int));
923 static void compute_stop_pos P_ ((struct it *));
924 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
925 Lisp_Object));
926 static int face_before_or_after_it_pos P_ ((struct it *, int));
927 static int next_overlay_change P_ ((int));
928 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
929 Lisp_Object, struct text_pos *,
930 int));
931 static int underlying_face_id P_ ((struct it *));
932 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
933 struct window *));
935 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
936 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
938 #ifdef HAVE_WINDOW_SYSTEM
940 static void update_tool_bar P_ ((struct frame *, int));
941 static void build_desired_tool_bar_string P_ ((struct frame *f));
942 static int redisplay_tool_bar P_ ((struct frame *));
943 static void display_tool_bar_line P_ ((struct it *));
944 static void notice_overwritten_cursor P_ ((struct window *,
945 enum glyph_row_area,
946 int, int, int, int));
950 #endif /* HAVE_WINDOW_SYSTEM */
953 /***********************************************************************
954 Window display dimensions
955 ***********************************************************************/
957 /* Return the bottom boundary y-position for text lines in window W.
958 This is the first y position at which a line cannot start.
959 It is relative to the top of the window.
961 This is the height of W minus the height of a mode line, if any. */
963 INLINE int
964 window_text_bottom_y (w)
965 struct window *w;
967 int height = WINDOW_TOTAL_HEIGHT (w);
969 if (WINDOW_WANTS_MODELINE_P (w))
970 height -= CURRENT_MODE_LINE_HEIGHT (w);
971 return height;
974 /* Return the pixel width of display area AREA of window W. AREA < 0
975 means return the total width of W, not including fringes to
976 the left and right of the window. */
978 INLINE int
979 window_box_width (w, area)
980 struct window *w;
981 int area;
983 int cols = XFASTINT (w->total_cols);
984 int pixels = 0;
986 if (!w->pseudo_window_p)
988 cols -= WINDOW_SCROLL_BAR_COLS (w);
990 if (area == TEXT_AREA)
992 if (INTEGERP (w->left_margin_cols))
993 cols -= XFASTINT (w->left_margin_cols);
994 if (INTEGERP (w->right_margin_cols))
995 cols -= XFASTINT (w->right_margin_cols);
996 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
998 else if (area == LEFT_MARGIN_AREA)
1000 cols = (INTEGERP (w->left_margin_cols)
1001 ? XFASTINT (w->left_margin_cols) : 0);
1002 pixels = 0;
1004 else if (area == RIGHT_MARGIN_AREA)
1006 cols = (INTEGERP (w->right_margin_cols)
1007 ? XFASTINT (w->right_margin_cols) : 0);
1008 pixels = 0;
1012 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1016 /* Return the pixel height of the display area of window W, not
1017 including mode lines of W, if any. */
1019 INLINE int
1020 window_box_height (w)
1021 struct window *w;
1023 struct frame *f = XFRAME (w->frame);
1024 int height = WINDOW_TOTAL_HEIGHT (w);
1026 xassert (height >= 0);
1028 /* Note: the code below that determines the mode-line/header-line
1029 height is essentially the same as that contained in the macro
1030 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1031 the appropriate glyph row has its `mode_line_p' flag set,
1032 and if it doesn't, uses estimate_mode_line_height instead. */
1034 if (WINDOW_WANTS_MODELINE_P (w))
1036 struct glyph_row *ml_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (ml_row && ml_row->mode_line_p)
1041 height -= ml_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 struct glyph_row *hl_row
1049 = (w->current_matrix && w->current_matrix->rows
1050 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1051 : 0);
1052 if (hl_row && hl_row->mode_line_p)
1053 height -= hl_row->height;
1054 else
1055 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1058 /* With a very small font and a mode-line that's taller than
1059 default, we might end up with a negative height. */
1060 return max (0, height);
1063 /* Return the window-relative coordinate of the left edge of display
1064 area AREA of window W. AREA < 0 means return the left edge of the
1065 whole window, to the right of the left fringe of W. */
1067 INLINE int
1068 window_box_left_offset (w, area)
1069 struct window *w;
1070 int area;
1072 int x;
1074 if (w->pseudo_window_p)
1075 return 0;
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093 return x;
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the left edge of the
1099 whole window, to the left of the right fringe of W. */
1101 INLINE int
1102 window_box_right_offset (w, area)
1103 struct window *w;
1104 int area;
1106 return window_box_left_offset (w, area) + window_box_width (w, area);
1109 /* Return the frame-relative coordinate of the left edge of display
1110 area AREA of window W. AREA < 0 means return the left edge of the
1111 whole window, to the right of the left fringe of W. */
1113 INLINE int
1114 window_box_left (w, area)
1115 struct window *w;
1116 int area;
1118 struct frame *f = XFRAME (w->frame);
1119 int x;
1121 if (w->pseudo_window_p)
1122 return FRAME_INTERNAL_BORDER_WIDTH (f);
1124 x = (WINDOW_LEFT_EDGE_X (w)
1125 + window_box_left_offset (w, area));
1127 return x;
1131 /* Return the frame-relative coordinate of the right edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the left of the right fringe of W. */
1135 INLINE int
1136 window_box_right (w, area)
1137 struct window *w;
1138 int area;
1140 return window_box_left (w, area) + window_box_width (w, area);
1143 /* Get the bounding box of the display area AREA of window W, without
1144 mode lines, in frame-relative coordinates. AREA < 0 means the
1145 whole window, not including the left and right fringes of
1146 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1147 coordinates of the upper-left corner of the box. Return in
1148 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1150 INLINE void
1151 window_box (w, area, box_x, box_y, box_width, box_height)
1152 struct window *w;
1153 int area;
1154 int *box_x, *box_y, *box_width, *box_height;
1156 if (box_width)
1157 *box_width = window_box_width (w, area);
1158 if (box_height)
1159 *box_height = window_box_height (w);
1160 if (box_x)
1161 *box_x = window_box_left (w, area);
1162 if (box_y)
1164 *box_y = WINDOW_TOP_EDGE_Y (w);
1165 if (WINDOW_WANTS_HEADER_LINE_P (w))
1166 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1171 /* Get the bounding box of the display area AREA of window W, without
1172 mode lines. AREA < 0 means the whole window, not including the
1173 left and right fringe of the window. Return in *TOP_LEFT_X
1174 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1175 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1176 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1177 box. */
1179 INLINE void
1180 window_box_edges (w, area, top_left_x, top_left_y,
1181 bottom_right_x, bottom_right_y)
1182 struct window *w;
1183 int area;
1184 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1186 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1187 bottom_right_y);
1188 *bottom_right_x += *top_left_x;
1189 *bottom_right_y += *top_left_y;
1194 /***********************************************************************
1195 Utilities
1196 ***********************************************************************/
1198 /* Return the bottom y-position of the line the iterator IT is in.
1199 This can modify IT's settings. */
1202 line_bottom_y (it)
1203 struct it *it;
1205 int line_height = it->max_ascent + it->max_descent;
1206 int line_top_y = it->current_y;
1208 if (line_height == 0)
1210 if (last_height)
1211 line_height = last_height;
1212 else if (IT_CHARPOS (*it) < ZV)
1214 move_it_by_lines (it, 1, 1);
1215 line_height = (it->max_ascent || it->max_descent
1216 ? it->max_ascent + it->max_descent
1217 : last_height);
1219 else
1221 struct glyph_row *row = it->glyph_row;
1223 /* Use the default character height. */
1224 it->glyph_row = NULL;
1225 it->what = IT_CHARACTER;
1226 it->c = ' ';
1227 it->len = 1;
1228 PRODUCE_GLYPHS (it);
1229 line_height = it->ascent + it->descent;
1230 it->glyph_row = row;
1234 return line_top_y + line_height;
1238 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1239 1 if POS is visible and the line containing POS is fully visible.
1240 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1241 and header-lines heights. */
1244 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1245 struct window *w;
1246 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1248 struct it it;
1249 struct text_pos top;
1250 int visible_p;
1251 struct buffer *old_buffer = NULL;
1253 if (XBUFFER (w->buffer) != current_buffer)
1255 old_buffer = current_buffer;
1256 set_buffer_internal_1 (XBUFFER (w->buffer));
1259 *fully = visible_p = 0;
1260 SET_TEXT_POS_FROM_MARKER (top, w->start);
1262 /* Compute exact mode line heights, if requested. */
1263 if (exact_mode_line_heights_p)
1265 if (WINDOW_WANTS_MODELINE_P (w))
1266 current_mode_line_height
1267 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1268 current_buffer->mode_line_format);
1270 if (WINDOW_WANTS_HEADER_LINE_P (w))
1271 current_header_line_height
1272 = display_mode_line (w, HEADER_LINE_FACE_ID,
1273 current_buffer->header_line_format);
1276 start_display (&it, w, top);
1277 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1278 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1280 /* Note that we may overshoot because of invisible text. */
1281 if (IT_CHARPOS (it) >= charpos)
1283 int top_y = it.current_y;
1284 int bottom_y = line_bottom_y (&it);
1285 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1287 if (top_y < window_top_y)
1288 visible_p = bottom_y > window_top_y;
1289 else if (top_y < it.last_visible_y)
1291 visible_p = 1;
1292 *fully = bottom_y <= it.last_visible_y;
1294 if (visible_p && x)
1296 *x = it.current_x;
1297 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1300 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1302 struct it it2;
1304 it2 = it;
1305 move_it_by_lines (&it, 1, 0);
1306 if (charpos < IT_CHARPOS (it))
1308 visible_p = 1;
1309 if (x)
1311 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1312 *x = it2.current_x;
1313 *y = it2.current_y + it2.max_ascent - it2.ascent;
1318 if (old_buffer)
1319 set_buffer_internal_1 (old_buffer);
1321 current_header_line_height = current_mode_line_height = -1;
1323 return visible_p;
1327 /* Return the next character from STR which is MAXLEN bytes long.
1328 Return in *LEN the length of the character. This is like
1329 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1330 we find one, we return a `?', but with the length of the invalid
1331 character. */
1333 static INLINE int
1334 string_char_and_length (str, maxlen, len)
1335 const unsigned char *str;
1336 int maxlen, *len;
1338 int c;
1340 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1341 if (!CHAR_VALID_P (c, 1))
1342 /* We may not change the length here because other places in Emacs
1343 don't use this function, i.e. they silently accept invalid
1344 characters. */
1345 c = '?';
1347 return c;
1352 /* Given a position POS containing a valid character and byte position
1353 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1355 static struct text_pos
1356 string_pos_nchars_ahead (pos, string, nchars)
1357 struct text_pos pos;
1358 Lisp_Object string;
1359 int nchars;
1361 xassert (STRINGP (string) && nchars >= 0);
1363 if (STRING_MULTIBYTE (string))
1365 int rest = SBYTES (string) - BYTEPOS (pos);
1366 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1367 int len;
1369 while (nchars--)
1371 string_char_and_length (p, rest, &len);
1372 p += len, rest -= len;
1373 xassert (rest >= 0);
1374 CHARPOS (pos) += 1;
1375 BYTEPOS (pos) += len;
1378 else
1379 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1381 return pos;
1385 /* Value is the text position, i.e. character and byte position,
1386 for character position CHARPOS in STRING. */
1388 static INLINE struct text_pos
1389 string_pos (charpos, string)
1390 int charpos;
1391 Lisp_Object string;
1393 struct text_pos pos;
1394 xassert (STRINGP (string));
1395 xassert (charpos >= 0);
1396 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1397 return pos;
1401 /* Value is a text position, i.e. character and byte position, for
1402 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1403 means recognize multibyte characters. */
1405 static struct text_pos
1406 c_string_pos (charpos, s, multibyte_p)
1407 int charpos;
1408 unsigned char *s;
1409 int multibyte_p;
1411 struct text_pos pos;
1413 xassert (s != NULL);
1414 xassert (charpos >= 0);
1416 if (multibyte_p)
1418 int rest = strlen (s), len;
1420 SET_TEXT_POS (pos, 0, 0);
1421 while (charpos--)
1423 string_char_and_length (s, rest, &len);
1424 s += len, rest -= len;
1425 xassert (rest >= 0);
1426 CHARPOS (pos) += 1;
1427 BYTEPOS (pos) += len;
1430 else
1431 SET_TEXT_POS (pos, charpos, charpos);
1433 return pos;
1437 /* Value is the number of characters in C string S. MULTIBYTE_P
1438 non-zero means recognize multibyte characters. */
1440 static int
1441 number_of_chars (s, multibyte_p)
1442 unsigned char *s;
1443 int multibyte_p;
1445 int nchars;
1447 if (multibyte_p)
1449 int rest = strlen (s), len;
1450 unsigned char *p = (unsigned char *) s;
1452 for (nchars = 0; rest > 0; ++nchars)
1454 string_char_and_length (p, rest, &len);
1455 rest -= len, p += len;
1458 else
1459 nchars = strlen (s);
1461 return nchars;
1465 /* Compute byte position NEWPOS->bytepos corresponding to
1466 NEWPOS->charpos. POS is a known position in string STRING.
1467 NEWPOS->charpos must be >= POS.charpos. */
1469 static void
1470 compute_string_pos (newpos, pos, string)
1471 struct text_pos *newpos, pos;
1472 Lisp_Object string;
1474 xassert (STRINGP (string));
1475 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1477 if (STRING_MULTIBYTE (string))
1478 *newpos = string_pos_nchars_ahead (pos, string,
1479 CHARPOS (*newpos) - CHARPOS (pos));
1480 else
1481 BYTEPOS (*newpos) = CHARPOS (*newpos);
1484 /* EXPORT:
1485 Return an estimation of the pixel height of mode or top lines on
1486 frame F. FACE_ID specifies what line's height to estimate. */
1489 estimate_mode_line_height (f, face_id)
1490 struct frame *f;
1491 enum face_id face_id;
1493 #ifdef HAVE_WINDOW_SYSTEM
1494 if (FRAME_WINDOW_P (f))
1496 int height = FONT_HEIGHT (FRAME_FONT (f));
1498 /* This function is called so early when Emacs starts that the face
1499 cache and mode line face are not yet initialized. */
1500 if (FRAME_FACE_CACHE (f))
1502 struct face *face = FACE_FROM_ID (f, face_id);
1503 if (face)
1505 if (face->font)
1506 height = FONT_HEIGHT (face->font);
1507 if (face->box_line_width > 0)
1508 height += 2 * face->box_line_width;
1512 return height;
1514 #endif
1516 return 1;
1519 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1520 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1521 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1522 not force the value into range. */
1524 void
1525 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1526 FRAME_PTR f;
1527 register int pix_x, pix_y;
1528 int *x, *y;
1529 NativeRectangle *bounds;
1530 int noclip;
1533 #ifdef HAVE_WINDOW_SYSTEM
1534 if (FRAME_WINDOW_P (f))
1536 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1537 even for negative values. */
1538 if (pix_x < 0)
1539 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1540 if (pix_y < 0)
1541 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1543 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1544 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1546 if (bounds)
1547 STORE_NATIVE_RECT (*bounds,
1548 FRAME_COL_TO_PIXEL_X (f, pix_x),
1549 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1550 FRAME_COLUMN_WIDTH (f) - 1,
1551 FRAME_LINE_HEIGHT (f) - 1);
1553 if (!noclip)
1555 if (pix_x < 0)
1556 pix_x = 0;
1557 else if (pix_x > FRAME_TOTAL_COLS (f))
1558 pix_x = FRAME_TOTAL_COLS (f);
1560 if (pix_y < 0)
1561 pix_y = 0;
1562 else if (pix_y > FRAME_LINES (f))
1563 pix_y = FRAME_LINES (f);
1566 #endif
1568 *x = pix_x;
1569 *y = pix_y;
1573 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1574 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1575 can't tell the positions because W's display is not up to date,
1576 return 0. */
1579 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1580 struct window *w;
1581 int hpos, vpos;
1582 int *frame_x, *frame_y;
1584 #ifdef HAVE_WINDOW_SYSTEM
1585 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1587 int success_p;
1589 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1590 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1592 if (display_completed)
1594 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1595 struct glyph *glyph = row->glyphs[TEXT_AREA];
1596 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1598 hpos = row->x;
1599 vpos = row->y;
1600 while (glyph < end)
1602 hpos += glyph->pixel_width;
1603 ++glyph;
1606 /* If first glyph is partially visible, its first visible position is still 0. */
1607 if (hpos < 0)
1608 hpos = 0;
1610 success_p = 1;
1612 else
1614 hpos = vpos = 0;
1615 success_p = 0;
1618 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1619 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1620 return success_p;
1622 #endif
1624 *frame_x = hpos;
1625 *frame_y = vpos;
1626 return 1;
1630 #ifdef HAVE_WINDOW_SYSTEM
1632 /* Find the glyph under window-relative coordinates X/Y in window W.
1633 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1634 strings. Return in *HPOS and *VPOS the row and column number of
1635 the glyph found. Return in *AREA the glyph area containing X.
1636 Value is a pointer to the glyph found or null if X/Y is not on
1637 text, or we can't tell because W's current matrix is not up to
1638 date. */
1640 static struct glyph *
1641 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1642 struct window *w;
1643 int x, y;
1644 int *hpos, *vpos, *dx, *dy, *area;
1646 struct glyph *glyph, *end;
1647 struct glyph_row *row = NULL;
1648 int x0, i;
1650 /* Find row containing Y. Give up if some row is not enabled. */
1651 for (i = 0; i < w->current_matrix->nrows; ++i)
1653 row = MATRIX_ROW (w->current_matrix, i);
1654 if (!row->enabled_p)
1655 return NULL;
1656 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1657 break;
1660 *vpos = i;
1661 *hpos = 0;
1663 /* Give up if Y is not in the window. */
1664 if (i == w->current_matrix->nrows)
1665 return NULL;
1667 /* Get the glyph area containing X. */
1668 if (w->pseudo_window_p)
1670 *area = TEXT_AREA;
1671 x0 = 0;
1673 else
1675 if (x < window_box_left_offset (w, TEXT_AREA))
1677 *area = LEFT_MARGIN_AREA;
1678 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1680 else if (x < window_box_right_offset (w, TEXT_AREA))
1682 *area = TEXT_AREA;
1683 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1685 else
1687 *area = RIGHT_MARGIN_AREA;
1688 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1692 /* Find glyph containing X. */
1693 glyph = row->glyphs[*area];
1694 end = glyph + row->used[*area];
1695 x -= x0;
1696 while (glyph < end && x >= glyph->pixel_width)
1698 x -= glyph->pixel_width;
1699 ++glyph;
1702 if (glyph == end)
1703 return NULL;
1705 if (dx)
1707 *dx = x;
1708 *dy = y - (row->y + row->ascent - glyph->ascent);
1711 *hpos = glyph - row->glyphs[*area];
1712 return glyph;
1716 /* EXPORT:
1717 Convert frame-relative x/y to coordinates relative to window W.
1718 Takes pseudo-windows into account. */
1720 void
1721 frame_to_window_pixel_xy (w, x, y)
1722 struct window *w;
1723 int *x, *y;
1725 if (w->pseudo_window_p)
1727 /* A pseudo-window is always full-width, and starts at the
1728 left edge of the frame, plus a frame border. */
1729 struct frame *f = XFRAME (w->frame);
1730 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1731 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1733 else
1735 *x -= WINDOW_LEFT_EDGE_X (w);
1736 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1740 /* EXPORT:
1741 Return in *R the clipping rectangle for glyph string S. */
1743 void
1744 get_glyph_string_clip_rect (s, nr)
1745 struct glyph_string *s;
1746 NativeRectangle *nr;
1748 XRectangle r;
1750 if (s->row->full_width_p)
1752 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1753 r.x = WINDOW_LEFT_EDGE_X (s->w);
1754 r.width = WINDOW_TOTAL_WIDTH (s->w);
1756 /* Unless displaying a mode or menu bar line, which are always
1757 fully visible, clip to the visible part of the row. */
1758 if (s->w->pseudo_window_p)
1759 r.height = s->row->visible_height;
1760 else
1761 r.height = s->height;
1763 else
1765 /* This is a text line that may be partially visible. */
1766 r.x = window_box_left (s->w, s->area);
1767 r.width = window_box_width (s->w, s->area);
1768 r.height = s->row->visible_height;
1771 /* If S draws overlapping rows, it's sufficient to use the top and
1772 bottom of the window for clipping because this glyph string
1773 intentionally draws over other lines. */
1774 if (s->for_overlaps_p)
1776 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1777 r.height = window_text_bottom_y (s->w) - r.y;
1779 else
1781 /* Don't use S->y for clipping because it doesn't take partially
1782 visible lines into account. For example, it can be negative for
1783 partially visible lines at the top of a window. */
1784 if (!s->row->full_width_p
1785 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1786 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1787 else
1788 r.y = max (0, s->row->y);
1790 /* If drawing a tool-bar window, draw it over the internal border
1791 at the top of the window. */
1792 if (WINDOWP (s->f->tool_bar_window)
1793 && s->w == XWINDOW (s->f->tool_bar_window))
1794 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1797 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1799 /* If drawing the cursor, don't let glyph draw outside its
1800 advertised boundaries. Cleartype does this under some circumstances. */
1801 if (s->hl == DRAW_CURSOR)
1803 struct glyph *glyph = s->first_glyph;
1804 int height;
1806 if (s->x > r.x)
1808 r.width -= s->x - r.x;
1809 r.x = s->x;
1811 r.width = min (r.width, glyph->pixel_width);
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1817 int max_y = r.y + r.height;
1818 r.y = min (max_y, s->ybase + glyph->descent - height);
1819 r.height = min (max_y - r.y, height);
1823 #ifdef CONVERT_FROM_XRECT
1824 CONVERT_FROM_XRECT (r, *nr);
1825 #else
1826 *nr = r;
1827 #endif
1830 #endif /* HAVE_WINDOW_SYSTEM */
1833 /***********************************************************************
1834 Lisp form evaluation
1835 ***********************************************************************/
1837 /* Error handler for safe_eval and safe_call. */
1839 static Lisp_Object
1840 safe_eval_handler (arg)
1841 Lisp_Object arg;
1843 add_to_log ("Error during redisplay: %s", arg, Qnil);
1844 return Qnil;
1848 /* Evaluate SEXPR and return the result, or nil if something went
1849 wrong. Prevent redisplay during the evaluation. */
1851 Lisp_Object
1852 safe_eval (sexpr)
1853 Lisp_Object sexpr;
1855 Lisp_Object val;
1857 if (inhibit_eval_during_redisplay)
1858 val = Qnil;
1859 else
1861 int count = SPECPDL_INDEX ();
1862 struct gcpro gcpro1;
1864 GCPRO1 (sexpr);
1865 specbind (Qinhibit_redisplay, Qt);
1866 /* Use Qt to ensure debugger does not run,
1867 so there is no possibility of wanting to redisplay. */
1868 val = internal_condition_case_1 (Feval, sexpr, Qt,
1869 safe_eval_handler);
1870 UNGCPRO;
1871 val = unbind_to (count, val);
1874 return val;
1878 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1879 Return the result, or nil if something went wrong. Prevent
1880 redisplay during the evaluation. */
1882 Lisp_Object
1883 safe_call (nargs, args)
1884 int nargs;
1885 Lisp_Object *args;
1887 Lisp_Object val;
1889 if (inhibit_eval_during_redisplay)
1890 val = Qnil;
1891 else
1893 int count = SPECPDL_INDEX ();
1894 struct gcpro gcpro1;
1896 GCPRO1 (args[0]);
1897 gcpro1.nvars = nargs;
1898 specbind (Qinhibit_redisplay, Qt);
1899 /* Use Qt to ensure debugger does not run,
1900 so there is no possibility of wanting to redisplay. */
1901 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1902 safe_eval_handler);
1903 UNGCPRO;
1904 val = unbind_to (count, val);
1907 return val;
1911 /* Call function FN with one argument ARG.
1912 Return the result, or nil if something went wrong. */
1914 Lisp_Object
1915 safe_call1 (fn, arg)
1916 Lisp_Object fn, arg;
1918 Lisp_Object args[2];
1919 args[0] = fn;
1920 args[1] = arg;
1921 return safe_call (2, args);
1926 /***********************************************************************
1927 Debugging
1928 ***********************************************************************/
1930 #if 0
1932 /* Define CHECK_IT to perform sanity checks on iterators.
1933 This is for debugging. It is too slow to do unconditionally. */
1935 static void
1936 check_it (it)
1937 struct it *it;
1939 if (it->method == next_element_from_string)
1941 xassert (STRINGP (it->string));
1942 xassert (IT_STRING_CHARPOS (*it) >= 0);
1944 else
1946 xassert (IT_STRING_CHARPOS (*it) < 0);
1947 if (it->method == next_element_from_buffer)
1949 /* Check that character and byte positions agree. */
1950 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1954 if (it->dpvec)
1955 xassert (it->current.dpvec_index >= 0);
1956 else
1957 xassert (it->current.dpvec_index < 0);
1960 #define CHECK_IT(IT) check_it ((IT))
1962 #else /* not 0 */
1964 #define CHECK_IT(IT) (void) 0
1966 #endif /* not 0 */
1969 #if GLYPH_DEBUG
1971 /* Check that the window end of window W is what we expect it
1972 to be---the last row in the current matrix displaying text. */
1974 static void
1975 check_window_end (w)
1976 struct window *w;
1978 if (!MINI_WINDOW_P (w)
1979 && !NILP (w->window_end_valid))
1981 struct glyph_row *row;
1982 xassert ((row = MATRIX_ROW (w->current_matrix,
1983 XFASTINT (w->window_end_vpos)),
1984 !row->enabled_p
1985 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1986 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1990 #define CHECK_WINDOW_END(W) check_window_end ((W))
1992 #else /* not GLYPH_DEBUG */
1994 #define CHECK_WINDOW_END(W) (void) 0
1996 #endif /* not GLYPH_DEBUG */
2000 /***********************************************************************
2001 Iterator initialization
2002 ***********************************************************************/
2004 /* Initialize IT for displaying current_buffer in window W, starting
2005 at character position CHARPOS. CHARPOS < 0 means that no buffer
2006 position is specified which is useful when the iterator is assigned
2007 a position later. BYTEPOS is the byte position corresponding to
2008 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2010 If ROW is not null, calls to produce_glyphs with IT as parameter
2011 will produce glyphs in that row.
2013 BASE_FACE_ID is the id of a base face to use. It must be one of
2014 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2015 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2016 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2018 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2019 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2020 will be initialized to use the corresponding mode line glyph row of
2021 the desired matrix of W. */
2023 void
2024 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2025 struct it *it;
2026 struct window *w;
2027 int charpos, bytepos;
2028 struct glyph_row *row;
2029 enum face_id base_face_id;
2031 int highlight_region_p;
2033 /* Some precondition checks. */
2034 xassert (w != NULL && it != NULL);
2035 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2036 && charpos <= ZV));
2038 /* If face attributes have been changed since the last redisplay,
2039 free realized faces now because they depend on face definitions
2040 that might have changed. Don't free faces while there might be
2041 desired matrices pending which reference these faces. */
2042 if (face_change_count && !inhibit_free_realized_faces)
2044 face_change_count = 0;
2045 free_all_realized_faces (Qnil);
2048 /* Use one of the mode line rows of W's desired matrix if
2049 appropriate. */
2050 if (row == NULL)
2052 if (base_face_id == MODE_LINE_FACE_ID
2053 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2054 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2055 else if (base_face_id == HEADER_LINE_FACE_ID)
2056 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2059 /* Clear IT. */
2060 bzero (it, sizeof *it);
2061 it->current.overlay_string_index = -1;
2062 it->current.dpvec_index = -1;
2063 it->base_face_id = base_face_id;
2064 it->string = Qnil;
2065 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2067 /* The window in which we iterate over current_buffer: */
2068 XSETWINDOW (it->window, w);
2069 it->w = w;
2070 it->f = XFRAME (w->frame);
2072 /* Extra space between lines (on window systems only). */
2073 if (base_face_id == DEFAULT_FACE_ID
2074 && FRAME_WINDOW_P (it->f))
2076 if (NATNUMP (current_buffer->extra_line_spacing))
2077 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2078 else if (FLOATP (current_buffer->extra_line_spacing))
2079 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2080 * FRAME_LINE_HEIGHT (it->f));
2081 else if (it->f->extra_line_spacing > 0)
2082 it->extra_line_spacing = it->f->extra_line_spacing;
2083 it->max_extra_line_spacing = 0;
2086 /* If realized faces have been removed, e.g. because of face
2087 attribute changes of named faces, recompute them. When running
2088 in batch mode, the face cache of Vterminal_frame is null. If
2089 we happen to get called, make a dummy face cache. */
2090 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2091 init_frame_faces (it->f);
2092 if (FRAME_FACE_CACHE (it->f)->used == 0)
2093 recompute_basic_faces (it->f);
2095 /* Current value of the `slice', `space-width', and 'height' properties. */
2096 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2097 it->space_width = Qnil;
2098 it->font_height = Qnil;
2099 it->override_ascent = -1;
2101 /* Are control characters displayed as `^C'? */
2102 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2104 /* -1 means everything between a CR and the following line end
2105 is invisible. >0 means lines indented more than this value are
2106 invisible. */
2107 it->selective = (INTEGERP (current_buffer->selective_display)
2108 ? XFASTINT (current_buffer->selective_display)
2109 : (!NILP (current_buffer->selective_display)
2110 ? -1 : 0));
2111 it->selective_display_ellipsis_p
2112 = !NILP (current_buffer->selective_display_ellipses);
2114 /* Display table to use. */
2115 it->dp = window_display_table (w);
2117 /* Are multibyte characters enabled in current_buffer? */
2118 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2120 /* Non-zero if we should highlight the region. */
2121 highlight_region_p
2122 = (!NILP (Vtransient_mark_mode)
2123 && !NILP (current_buffer->mark_active)
2124 && XMARKER (current_buffer->mark)->buffer != 0);
2126 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2127 start and end of a visible region in window IT->w. Set both to
2128 -1 to indicate no region. */
2129 if (highlight_region_p
2130 /* Maybe highlight only in selected window. */
2131 && (/* Either show region everywhere. */
2132 highlight_nonselected_windows
2133 /* Or show region in the selected window. */
2134 || w == XWINDOW (selected_window)
2135 /* Or show the region if we are in the mini-buffer and W is
2136 the window the mini-buffer refers to. */
2137 || (MINI_WINDOW_P (XWINDOW (selected_window))
2138 && WINDOWP (minibuf_selected_window)
2139 && w == XWINDOW (minibuf_selected_window))))
2141 int charpos = marker_position (current_buffer->mark);
2142 it->region_beg_charpos = min (PT, charpos);
2143 it->region_end_charpos = max (PT, charpos);
2145 else
2146 it->region_beg_charpos = it->region_end_charpos = -1;
2148 /* Get the position at which the redisplay_end_trigger hook should
2149 be run, if it is to be run at all. */
2150 if (MARKERP (w->redisplay_end_trigger)
2151 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2152 it->redisplay_end_trigger_charpos
2153 = marker_position (w->redisplay_end_trigger);
2154 else if (INTEGERP (w->redisplay_end_trigger))
2155 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2157 /* Correct bogus values of tab_width. */
2158 it->tab_width = XINT (current_buffer->tab_width);
2159 if (it->tab_width <= 0 || it->tab_width > 1000)
2160 it->tab_width = 8;
2162 /* Are lines in the display truncated? */
2163 it->truncate_lines_p
2164 = (base_face_id != DEFAULT_FACE_ID
2165 || XINT (it->w->hscroll)
2166 || (truncate_partial_width_windows
2167 && !WINDOW_FULL_WIDTH_P (it->w))
2168 || !NILP (current_buffer->truncate_lines));
2170 /* Get dimensions of truncation and continuation glyphs. These are
2171 displayed as fringe bitmaps under X, so we don't need them for such
2172 frames. */
2173 if (!FRAME_WINDOW_P (it->f))
2175 if (it->truncate_lines_p)
2177 /* We will need the truncation glyph. */
2178 xassert (it->glyph_row == NULL);
2179 produce_special_glyphs (it, IT_TRUNCATION);
2180 it->truncation_pixel_width = it->pixel_width;
2182 else
2184 /* We will need the continuation glyph. */
2185 xassert (it->glyph_row == NULL);
2186 produce_special_glyphs (it, IT_CONTINUATION);
2187 it->continuation_pixel_width = it->pixel_width;
2190 /* Reset these values to zero because the produce_special_glyphs
2191 above has changed them. */
2192 it->pixel_width = it->ascent = it->descent = 0;
2193 it->phys_ascent = it->phys_descent = 0;
2196 /* Set this after getting the dimensions of truncation and
2197 continuation glyphs, so that we don't produce glyphs when calling
2198 produce_special_glyphs, above. */
2199 it->glyph_row = row;
2200 it->area = TEXT_AREA;
2202 /* Get the dimensions of the display area. The display area
2203 consists of the visible window area plus a horizontally scrolled
2204 part to the left of the window. All x-values are relative to the
2205 start of this total display area. */
2206 if (base_face_id != DEFAULT_FACE_ID)
2208 /* Mode lines, menu bar in terminal frames. */
2209 it->first_visible_x = 0;
2210 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2212 else
2214 it->first_visible_x
2215 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2216 it->last_visible_x = (it->first_visible_x
2217 + window_box_width (w, TEXT_AREA));
2219 /* If we truncate lines, leave room for the truncator glyph(s) at
2220 the right margin. Otherwise, leave room for the continuation
2221 glyph(s). Truncation and continuation glyphs are not inserted
2222 for window-based redisplay. */
2223 if (!FRAME_WINDOW_P (it->f))
2225 if (it->truncate_lines_p)
2226 it->last_visible_x -= it->truncation_pixel_width;
2227 else
2228 it->last_visible_x -= it->continuation_pixel_width;
2231 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2232 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2235 /* Leave room for a border glyph. */
2236 if (!FRAME_WINDOW_P (it->f)
2237 && !WINDOW_RIGHTMOST_P (it->w))
2238 it->last_visible_x -= 1;
2240 it->last_visible_y = window_text_bottom_y (w);
2242 /* For mode lines and alike, arrange for the first glyph having a
2243 left box line if the face specifies a box. */
2244 if (base_face_id != DEFAULT_FACE_ID)
2246 struct face *face;
2248 it->face_id = base_face_id;
2250 /* If we have a boxed mode line, make the first character appear
2251 with a left box line. */
2252 face = FACE_FROM_ID (it->f, base_face_id);
2253 if (face->box != FACE_NO_BOX)
2254 it->start_of_box_run_p = 1;
2257 /* If a buffer position was specified, set the iterator there,
2258 getting overlays and face properties from that position. */
2259 if (charpos >= BUF_BEG (current_buffer))
2261 it->end_charpos = ZV;
2262 it->face_id = -1;
2263 IT_CHARPOS (*it) = charpos;
2265 /* Compute byte position if not specified. */
2266 if (bytepos < charpos)
2267 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2268 else
2269 IT_BYTEPOS (*it) = bytepos;
2271 it->start = it->current;
2273 /* Compute faces etc. */
2274 reseat (it, it->current.pos, 1);
2277 CHECK_IT (it);
2281 /* Initialize IT for the display of window W with window start POS. */
2283 void
2284 start_display (it, w, pos)
2285 struct it *it;
2286 struct window *w;
2287 struct text_pos pos;
2289 struct glyph_row *row;
2290 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2292 row = w->desired_matrix->rows + first_vpos;
2293 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2294 it->first_vpos = first_vpos;
2296 if (!it->truncate_lines_p)
2298 int start_at_line_beg_p;
2299 int first_y = it->current_y;
2301 /* If window start is not at a line start, skip forward to POS to
2302 get the correct continuation lines width. */
2303 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2304 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2305 if (!start_at_line_beg_p)
2307 int new_x;
2309 reseat_at_previous_visible_line_start (it);
2310 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2312 new_x = it->current_x + it->pixel_width;
2314 /* If lines are continued, this line may end in the middle
2315 of a multi-glyph character (e.g. a control character
2316 displayed as \003, or in the middle of an overlay
2317 string). In this case move_it_to above will not have
2318 taken us to the start of the continuation line but to the
2319 end of the continued line. */
2320 if (it->current_x > 0
2321 && !it->truncate_lines_p /* Lines are continued. */
2322 && (/* And glyph doesn't fit on the line. */
2323 new_x > it->last_visible_x
2324 /* Or it fits exactly and we're on a window
2325 system frame. */
2326 || (new_x == it->last_visible_x
2327 && FRAME_WINDOW_P (it->f))))
2329 if (it->current.dpvec_index >= 0
2330 || it->current.overlay_string_index >= 0)
2332 set_iterator_to_next (it, 1);
2333 move_it_in_display_line_to (it, -1, -1, 0);
2336 it->continuation_lines_width += it->current_x;
2339 /* We're starting a new display line, not affected by the
2340 height of the continued line, so clear the appropriate
2341 fields in the iterator structure. */
2342 it->max_ascent = it->max_descent = 0;
2343 it->max_phys_ascent = it->max_phys_descent = 0;
2345 it->current_y = first_y;
2346 it->vpos = 0;
2347 it->current_x = it->hpos = 0;
2351 #if 0 /* Don't assert the following because start_display is sometimes
2352 called intentionally with a window start that is not at a
2353 line start. Please leave this code in as a comment. */
2355 /* Window start should be on a line start, now. */
2356 xassert (it->continuation_lines_width
2357 || IT_CHARPOS (it) == BEGV
2358 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2359 #endif /* 0 */
2363 /* Return 1 if POS is a position in ellipses displayed for invisible
2364 text. W is the window we display, for text property lookup. */
2366 static int
2367 in_ellipses_for_invisible_text_p (pos, w)
2368 struct display_pos *pos;
2369 struct window *w;
2371 Lisp_Object prop, window;
2372 int ellipses_p = 0;
2373 int charpos = CHARPOS (pos->pos);
2375 /* If POS specifies a position in a display vector, this might
2376 be for an ellipsis displayed for invisible text. We won't
2377 get the iterator set up for delivering that ellipsis unless
2378 we make sure that it gets aware of the invisible text. */
2379 if (pos->dpvec_index >= 0
2380 && pos->overlay_string_index < 0
2381 && CHARPOS (pos->string_pos) < 0
2382 && charpos > BEGV
2383 && (XSETWINDOW (window, w),
2384 prop = Fget_char_property (make_number (charpos),
2385 Qinvisible, window),
2386 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2388 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2389 window);
2390 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2393 return ellipses_p;
2397 /* Initialize IT for stepping through current_buffer in window W,
2398 starting at position POS that includes overlay string and display
2399 vector/ control character translation position information. Value
2400 is zero if there are overlay strings with newlines at POS. */
2402 static int
2403 init_from_display_pos (it, w, pos)
2404 struct it *it;
2405 struct window *w;
2406 struct display_pos *pos;
2408 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2409 int i, overlay_strings_with_newlines = 0;
2411 /* If POS specifies a position in a display vector, this might
2412 be for an ellipsis displayed for invisible text. We won't
2413 get the iterator set up for delivering that ellipsis unless
2414 we make sure that it gets aware of the invisible text. */
2415 if (in_ellipses_for_invisible_text_p (pos, w))
2417 --charpos;
2418 bytepos = 0;
2421 /* Keep in mind: the call to reseat in init_iterator skips invisible
2422 text, so we might end up at a position different from POS. This
2423 is only a problem when POS is a row start after a newline and an
2424 overlay starts there with an after-string, and the overlay has an
2425 invisible property. Since we don't skip invisible text in
2426 display_line and elsewhere immediately after consuming the
2427 newline before the row start, such a POS will not be in a string,
2428 but the call to init_iterator below will move us to the
2429 after-string. */
2430 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2432 for (i = 0; i < it->n_overlay_strings; ++i)
2434 const char *s = SDATA (it->overlay_strings[i]);
2435 const char *e = s + SBYTES (it->overlay_strings[i]);
2437 while (s < e && *s != '\n')
2438 ++s;
2440 if (s < e)
2442 overlay_strings_with_newlines = 1;
2443 break;
2447 /* If position is within an overlay string, set up IT to the right
2448 overlay string. */
2449 if (pos->overlay_string_index >= 0)
2451 int relative_index;
2453 /* If the first overlay string happens to have a `display'
2454 property for an image, the iterator will be set up for that
2455 image, and we have to undo that setup first before we can
2456 correct the overlay string index. */
2457 if (it->method == next_element_from_image)
2458 pop_it (it);
2460 /* We already have the first chunk of overlay strings in
2461 IT->overlay_strings. Load more until the one for
2462 pos->overlay_string_index is in IT->overlay_strings. */
2463 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2465 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2466 it->current.overlay_string_index = 0;
2467 while (n--)
2469 load_overlay_strings (it, 0);
2470 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2474 it->current.overlay_string_index = pos->overlay_string_index;
2475 relative_index = (it->current.overlay_string_index
2476 % OVERLAY_STRING_CHUNK_SIZE);
2477 it->string = it->overlay_strings[relative_index];
2478 xassert (STRINGP (it->string));
2479 it->current.string_pos = pos->string_pos;
2480 it->method = next_element_from_string;
2483 #if 0 /* This is bogus because POS not having an overlay string
2484 position does not mean it's after the string. Example: A
2485 line starting with a before-string and initialization of IT
2486 to the previous row's end position. */
2487 else if (it->current.overlay_string_index >= 0)
2489 /* If POS says we're already after an overlay string ending at
2490 POS, make sure to pop the iterator because it will be in
2491 front of that overlay string. When POS is ZV, we've thereby
2492 also ``processed'' overlay strings at ZV. */
2493 while (it->sp)
2494 pop_it (it);
2495 it->current.overlay_string_index = -1;
2496 it->method = next_element_from_buffer;
2497 if (CHARPOS (pos->pos) == ZV)
2498 it->overlay_strings_at_end_processed_p = 1;
2500 #endif /* 0 */
2502 if (CHARPOS (pos->string_pos) >= 0)
2504 /* Recorded position is not in an overlay string, but in another
2505 string. This can only be a string from a `display' property.
2506 IT should already be filled with that string. */
2507 it->current.string_pos = pos->string_pos;
2508 xassert (STRINGP (it->string));
2511 /* Restore position in display vector translations, control
2512 character translations or ellipses. */
2513 if (pos->dpvec_index >= 0)
2515 if (it->dpvec == NULL)
2516 get_next_display_element (it);
2517 xassert (it->dpvec && it->current.dpvec_index == 0);
2518 it->current.dpvec_index = pos->dpvec_index;
2521 CHECK_IT (it);
2522 return !overlay_strings_with_newlines;
2526 /* Initialize IT for stepping through current_buffer in window W
2527 starting at ROW->start. */
2529 static void
2530 init_to_row_start (it, w, row)
2531 struct it *it;
2532 struct window *w;
2533 struct glyph_row *row;
2535 init_from_display_pos (it, w, &row->start);
2536 it->start = row->start;
2537 it->continuation_lines_width = row->continuation_lines_width;
2538 CHECK_IT (it);
2542 /* Initialize IT for stepping through current_buffer in window W
2543 starting in the line following ROW, i.e. starting at ROW->end.
2544 Value is zero if there are overlay strings with newlines at ROW's
2545 end position. */
2547 static int
2548 init_to_row_end (it, w, row)
2549 struct it *it;
2550 struct window *w;
2551 struct glyph_row *row;
2553 int success = 0;
2555 if (init_from_display_pos (it, w, &row->end))
2557 if (row->continued_p)
2558 it->continuation_lines_width
2559 = row->continuation_lines_width + row->pixel_width;
2560 CHECK_IT (it);
2561 success = 1;
2564 return success;
2570 /***********************************************************************
2571 Text properties
2572 ***********************************************************************/
2574 /* Called when IT reaches IT->stop_charpos. Handle text property and
2575 overlay changes. Set IT->stop_charpos to the next position where
2576 to stop. */
2578 static void
2579 handle_stop (it)
2580 struct it *it;
2582 enum prop_handled handled;
2583 int handle_overlay_change_p = 1;
2584 struct props *p;
2586 it->dpvec = NULL;
2587 it->current.dpvec_index = -1;
2591 handled = HANDLED_NORMALLY;
2593 /* Call text property handlers. */
2594 for (p = it_props; p->handler; ++p)
2596 handled = p->handler (it);
2598 if (handled == HANDLED_RECOMPUTE_PROPS)
2599 break;
2600 else if (handled == HANDLED_RETURN)
2601 return;
2602 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2603 handle_overlay_change_p = 0;
2606 if (handled != HANDLED_RECOMPUTE_PROPS)
2608 /* Don't check for overlay strings below when set to deliver
2609 characters from a display vector. */
2610 if (it->method == next_element_from_display_vector)
2611 handle_overlay_change_p = 0;
2613 /* Handle overlay changes. */
2614 if (handle_overlay_change_p)
2615 handled = handle_overlay_change (it);
2617 /* Determine where to stop next. */
2618 if (handled == HANDLED_NORMALLY)
2619 compute_stop_pos (it);
2622 while (handled == HANDLED_RECOMPUTE_PROPS);
2626 /* Compute IT->stop_charpos from text property and overlay change
2627 information for IT's current position. */
2629 static void
2630 compute_stop_pos (it)
2631 struct it *it;
2633 register INTERVAL iv, next_iv;
2634 Lisp_Object object, limit, position;
2636 /* If nowhere else, stop at the end. */
2637 it->stop_charpos = it->end_charpos;
2639 if (STRINGP (it->string))
2641 /* Strings are usually short, so don't limit the search for
2642 properties. */
2643 object = it->string;
2644 limit = Qnil;
2645 position = make_number (IT_STRING_CHARPOS (*it));
2647 else
2649 int charpos;
2651 /* If next overlay change is in front of the current stop pos
2652 (which is IT->end_charpos), stop there. Note: value of
2653 next_overlay_change is point-max if no overlay change
2654 follows. */
2655 charpos = next_overlay_change (IT_CHARPOS (*it));
2656 if (charpos < it->stop_charpos)
2657 it->stop_charpos = charpos;
2659 /* If showing the region, we have to stop at the region
2660 start or end because the face might change there. */
2661 if (it->region_beg_charpos > 0)
2663 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2664 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2665 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2666 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2669 /* Set up variables for computing the stop position from text
2670 property changes. */
2671 XSETBUFFER (object, current_buffer);
2672 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2673 position = make_number (IT_CHARPOS (*it));
2677 /* Get the interval containing IT's position. Value is a null
2678 interval if there isn't such an interval. */
2679 iv = validate_interval_range (object, &position, &position, 0);
2680 if (!NULL_INTERVAL_P (iv))
2682 Lisp_Object values_here[LAST_PROP_IDX];
2683 struct props *p;
2685 /* Get properties here. */
2686 for (p = it_props; p->handler; ++p)
2687 values_here[p->idx] = textget (iv->plist, *p->name);
2689 /* Look for an interval following iv that has different
2690 properties. */
2691 for (next_iv = next_interval (iv);
2692 (!NULL_INTERVAL_P (next_iv)
2693 && (NILP (limit)
2694 || XFASTINT (limit) > next_iv->position));
2695 next_iv = next_interval (next_iv))
2697 for (p = it_props; p->handler; ++p)
2699 Lisp_Object new_value;
2701 new_value = textget (next_iv->plist, *p->name);
2702 if (!EQ (values_here[p->idx], new_value))
2703 break;
2706 if (p->handler)
2707 break;
2710 if (!NULL_INTERVAL_P (next_iv))
2712 if (INTEGERP (limit)
2713 && next_iv->position >= XFASTINT (limit))
2714 /* No text property change up to limit. */
2715 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2716 else
2717 /* Text properties change in next_iv. */
2718 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2722 xassert (STRINGP (it->string)
2723 || (it->stop_charpos >= BEGV
2724 && it->stop_charpos >= IT_CHARPOS (*it)));
2728 /* Return the position of the next overlay change after POS in
2729 current_buffer. Value is point-max if no overlay change
2730 follows. This is like `next-overlay-change' but doesn't use
2731 xmalloc. */
2733 static int
2734 next_overlay_change (pos)
2735 int pos;
2737 int noverlays;
2738 int endpos;
2739 Lisp_Object *overlays;
2740 int i;
2742 /* Get all overlays at the given position. */
2743 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2745 /* If any of these overlays ends before endpos,
2746 use its ending point instead. */
2747 for (i = 0; i < noverlays; ++i)
2749 Lisp_Object oend;
2750 int oendpos;
2752 oend = OVERLAY_END (overlays[i]);
2753 oendpos = OVERLAY_POSITION (oend);
2754 endpos = min (endpos, oendpos);
2757 return endpos;
2762 /***********************************************************************
2763 Fontification
2764 ***********************************************************************/
2766 /* Handle changes in the `fontified' property of the current buffer by
2767 calling hook functions from Qfontification_functions to fontify
2768 regions of text. */
2770 static enum prop_handled
2771 handle_fontified_prop (it)
2772 struct it *it;
2774 Lisp_Object prop, pos;
2775 enum prop_handled handled = HANDLED_NORMALLY;
2777 /* Get the value of the `fontified' property at IT's current buffer
2778 position. (The `fontified' property doesn't have a special
2779 meaning in strings.) If the value is nil, call functions from
2780 Qfontification_functions. */
2781 if (!STRINGP (it->string)
2782 && it->s == NULL
2783 && !NILP (Vfontification_functions)
2784 && !NILP (Vrun_hooks)
2785 && (pos = make_number (IT_CHARPOS (*it)),
2786 prop = Fget_char_property (pos, Qfontified, Qnil),
2787 NILP (prop)))
2789 int count = SPECPDL_INDEX ();
2790 Lisp_Object val;
2792 val = Vfontification_functions;
2793 specbind (Qfontification_functions, Qnil);
2795 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2796 safe_call1 (val, pos);
2797 else
2799 Lisp_Object globals, fn;
2800 struct gcpro gcpro1, gcpro2;
2802 globals = Qnil;
2803 GCPRO2 (val, globals);
2805 for (; CONSP (val); val = XCDR (val))
2807 fn = XCAR (val);
2809 if (EQ (fn, Qt))
2811 /* A value of t indicates this hook has a local
2812 binding; it means to run the global binding too.
2813 In a global value, t should not occur. If it
2814 does, we must ignore it to avoid an endless
2815 loop. */
2816 for (globals = Fdefault_value (Qfontification_functions);
2817 CONSP (globals);
2818 globals = XCDR (globals))
2820 fn = XCAR (globals);
2821 if (!EQ (fn, Qt))
2822 safe_call1 (fn, pos);
2825 else
2826 safe_call1 (fn, pos);
2829 UNGCPRO;
2832 unbind_to (count, Qnil);
2834 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2835 something. This avoids an endless loop if they failed to
2836 fontify the text for which reason ever. */
2837 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2838 handled = HANDLED_RECOMPUTE_PROPS;
2841 return handled;
2846 /***********************************************************************
2847 Faces
2848 ***********************************************************************/
2850 /* Set up iterator IT from face properties at its current position.
2851 Called from handle_stop. */
2853 static enum prop_handled
2854 handle_face_prop (it)
2855 struct it *it;
2857 int new_face_id, next_stop;
2859 if (!STRINGP (it->string))
2861 new_face_id
2862 = face_at_buffer_position (it->w,
2863 IT_CHARPOS (*it),
2864 it->region_beg_charpos,
2865 it->region_end_charpos,
2866 &next_stop,
2867 (IT_CHARPOS (*it)
2868 + TEXT_PROP_DISTANCE_LIMIT),
2871 /* Is this a start of a run of characters with box face?
2872 Caveat: this can be called for a freshly initialized
2873 iterator; face_id is -1 in this case. We know that the new
2874 face will not change until limit, i.e. if the new face has a
2875 box, all characters up to limit will have one. But, as
2876 usual, we don't know whether limit is really the end. */
2877 if (new_face_id != it->face_id)
2879 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2881 /* If new face has a box but old face has not, this is
2882 the start of a run of characters with box, i.e. it has
2883 a shadow on the left side. The value of face_id of the
2884 iterator will be -1 if this is the initial call that gets
2885 the face. In this case, we have to look in front of IT's
2886 position and see whether there is a face != new_face_id. */
2887 it->start_of_box_run_p
2888 = (new_face->box != FACE_NO_BOX
2889 && (it->face_id >= 0
2890 || IT_CHARPOS (*it) == BEG
2891 || new_face_id != face_before_it_pos (it)));
2892 it->face_box_p = new_face->box != FACE_NO_BOX;
2895 else
2897 int base_face_id, bufpos;
2899 if (it->current.overlay_string_index >= 0)
2900 bufpos = IT_CHARPOS (*it);
2901 else
2902 bufpos = 0;
2904 /* For strings from a buffer, i.e. overlay strings or strings
2905 from a `display' property, use the face at IT's current
2906 buffer position as the base face to merge with, so that
2907 overlay strings appear in the same face as surrounding
2908 text, unless they specify their own faces. */
2909 base_face_id = underlying_face_id (it);
2911 new_face_id = face_at_string_position (it->w,
2912 it->string,
2913 IT_STRING_CHARPOS (*it),
2914 bufpos,
2915 it->region_beg_charpos,
2916 it->region_end_charpos,
2917 &next_stop,
2918 base_face_id, 0);
2920 #if 0 /* This shouldn't be neccessary. Let's check it. */
2921 /* If IT is used to display a mode line we would really like to
2922 use the mode line face instead of the frame's default face. */
2923 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2924 && new_face_id == DEFAULT_FACE_ID)
2925 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2926 #endif
2928 /* Is this a start of a run of characters with box? Caveat:
2929 this can be called for a freshly allocated iterator; face_id
2930 is -1 is this case. We know that the new face will not
2931 change until the next check pos, i.e. if the new face has a
2932 box, all characters up to that position will have a
2933 box. But, as usual, we don't know whether that position
2934 is really the end. */
2935 if (new_face_id != it->face_id)
2937 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2938 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2940 /* If new face has a box but old face hasn't, this is the
2941 start of a run of characters with box, i.e. it has a
2942 shadow on the left side. */
2943 it->start_of_box_run_p
2944 = new_face->box && (old_face == NULL || !old_face->box);
2945 it->face_box_p = new_face->box != FACE_NO_BOX;
2949 it->face_id = new_face_id;
2950 return HANDLED_NORMALLY;
2954 /* Return the ID of the face ``underlying'' IT's current position,
2955 which is in a string. If the iterator is associated with a
2956 buffer, return the face at IT's current buffer position.
2957 Otherwise, use the iterator's base_face_id. */
2959 static int
2960 underlying_face_id (it)
2961 struct it *it;
2963 int face_id = it->base_face_id, i;
2965 xassert (STRINGP (it->string));
2967 for (i = it->sp - 1; i >= 0; --i)
2968 if (NILP (it->stack[i].string))
2969 face_id = it->stack[i].face_id;
2971 return face_id;
2975 /* Compute the face one character before or after the current position
2976 of IT. BEFORE_P non-zero means get the face in front of IT's
2977 position. Value is the id of the face. */
2979 static int
2980 face_before_or_after_it_pos (it, before_p)
2981 struct it *it;
2982 int before_p;
2984 int face_id, limit;
2985 int next_check_charpos;
2986 struct text_pos pos;
2988 xassert (it->s == NULL);
2990 if (STRINGP (it->string))
2992 int bufpos, base_face_id;
2994 /* No face change past the end of the string (for the case
2995 we are padding with spaces). No face change before the
2996 string start. */
2997 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2998 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2999 return it->face_id;
3001 /* Set pos to the position before or after IT's current position. */
3002 if (before_p)
3003 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3004 else
3005 /* For composition, we must check the character after the
3006 composition. */
3007 pos = (it->what == IT_COMPOSITION
3008 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3009 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3011 if (it->current.overlay_string_index >= 0)
3012 bufpos = IT_CHARPOS (*it);
3013 else
3014 bufpos = 0;
3016 base_face_id = underlying_face_id (it);
3018 /* Get the face for ASCII, or unibyte. */
3019 face_id = face_at_string_position (it->w,
3020 it->string,
3021 CHARPOS (pos),
3022 bufpos,
3023 it->region_beg_charpos,
3024 it->region_end_charpos,
3025 &next_check_charpos,
3026 base_face_id, 0);
3028 /* Correct the face for charsets different from ASCII. Do it
3029 for the multibyte case only. The face returned above is
3030 suitable for unibyte text if IT->string is unibyte. */
3031 if (STRING_MULTIBYTE (it->string))
3033 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3034 int rest = SBYTES (it->string) - BYTEPOS (pos);
3035 int c, len;
3036 struct face *face = FACE_FROM_ID (it->f, face_id);
3038 c = string_char_and_length (p, rest, &len);
3039 face_id = FACE_FOR_CHAR (it->f, face, c);
3042 else
3044 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3045 || (IT_CHARPOS (*it) <= BEGV && before_p))
3046 return it->face_id;
3048 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3049 pos = it->current.pos;
3051 if (before_p)
3052 DEC_TEXT_POS (pos, it->multibyte_p);
3053 else
3055 if (it->what == IT_COMPOSITION)
3056 /* For composition, we must check the position after the
3057 composition. */
3058 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3059 else
3060 INC_TEXT_POS (pos, it->multibyte_p);
3063 /* Determine face for CHARSET_ASCII, or unibyte. */
3064 face_id = face_at_buffer_position (it->w,
3065 CHARPOS (pos),
3066 it->region_beg_charpos,
3067 it->region_end_charpos,
3068 &next_check_charpos,
3069 limit, 0);
3071 /* Correct the face for charsets different from ASCII. Do it
3072 for the multibyte case only. The face returned above is
3073 suitable for unibyte text if current_buffer is unibyte. */
3074 if (it->multibyte_p)
3076 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3077 struct face *face = FACE_FROM_ID (it->f, face_id);
3078 face_id = FACE_FOR_CHAR (it->f, face, c);
3082 return face_id;
3087 /***********************************************************************
3088 Invisible text
3089 ***********************************************************************/
3091 /* Set up iterator IT from invisible properties at its current
3092 position. Called from handle_stop. */
3094 static enum prop_handled
3095 handle_invisible_prop (it)
3096 struct it *it;
3098 enum prop_handled handled = HANDLED_NORMALLY;
3100 if (STRINGP (it->string))
3102 extern Lisp_Object Qinvisible;
3103 Lisp_Object prop, end_charpos, limit, charpos;
3105 /* Get the value of the invisible text property at the
3106 current position. Value will be nil if there is no such
3107 property. */
3108 charpos = make_number (IT_STRING_CHARPOS (*it));
3109 prop = Fget_text_property (charpos, Qinvisible, it->string);
3111 if (!NILP (prop)
3112 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3114 handled = HANDLED_RECOMPUTE_PROPS;
3116 /* Get the position at which the next change of the
3117 invisible text property can be found in IT->string.
3118 Value will be nil if the property value is the same for
3119 all the rest of IT->string. */
3120 XSETINT (limit, SCHARS (it->string));
3121 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3122 it->string, limit);
3124 /* Text at current position is invisible. The next
3125 change in the property is at position end_charpos.
3126 Move IT's current position to that position. */
3127 if (INTEGERP (end_charpos)
3128 && XFASTINT (end_charpos) < XFASTINT (limit))
3130 struct text_pos old;
3131 old = it->current.string_pos;
3132 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3133 compute_string_pos (&it->current.string_pos, old, it->string);
3135 else
3137 /* The rest of the string is invisible. If this is an
3138 overlay string, proceed with the next overlay string
3139 or whatever comes and return a character from there. */
3140 if (it->current.overlay_string_index >= 0)
3142 next_overlay_string (it);
3143 /* Don't check for overlay strings when we just
3144 finished processing them. */
3145 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3147 else
3149 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3150 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3155 else
3157 int invis_p, newpos, next_stop, start_charpos;
3158 Lisp_Object pos, prop, overlay;
3160 /* First of all, is there invisible text at this position? */
3161 start_charpos = IT_CHARPOS (*it);
3162 pos = make_number (IT_CHARPOS (*it));
3163 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3164 &overlay);
3165 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3167 /* If we are on invisible text, skip over it. */
3168 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3170 /* Record whether we have to display an ellipsis for the
3171 invisible text. */
3172 int display_ellipsis_p = invis_p == 2;
3174 handled = HANDLED_RECOMPUTE_PROPS;
3176 /* Loop skipping over invisible text. The loop is left at
3177 ZV or with IT on the first char being visible again. */
3180 /* Try to skip some invisible text. Return value is the
3181 position reached which can be equal to IT's position
3182 if there is nothing invisible here. This skips both
3183 over invisible text properties and overlays with
3184 invisible property. */
3185 newpos = skip_invisible (IT_CHARPOS (*it),
3186 &next_stop, ZV, it->window);
3188 /* If we skipped nothing at all we weren't at invisible
3189 text in the first place. If everything to the end of
3190 the buffer was skipped, end the loop. */
3191 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3192 invis_p = 0;
3193 else
3195 /* We skipped some characters but not necessarily
3196 all there are. Check if we ended up on visible
3197 text. Fget_char_property returns the property of
3198 the char before the given position, i.e. if we
3199 get invis_p = 0, this means that the char at
3200 newpos is visible. */
3201 pos = make_number (newpos);
3202 prop = Fget_char_property (pos, Qinvisible, it->window);
3203 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3206 /* If we ended up on invisible text, proceed to
3207 skip starting with next_stop. */
3208 if (invis_p)
3209 IT_CHARPOS (*it) = next_stop;
3211 while (invis_p);
3213 /* The position newpos is now either ZV or on visible text. */
3214 IT_CHARPOS (*it) = newpos;
3215 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3217 /* If there are before-strings at the start of invisible
3218 text, and the text is invisible because of a text
3219 property, arrange to show before-strings because 20.x did
3220 it that way. (If the text is invisible because of an
3221 overlay property instead of a text property, this is
3222 already handled in the overlay code.) */
3223 if (NILP (overlay)
3224 && get_overlay_strings (it, start_charpos))
3226 handled = HANDLED_RECOMPUTE_PROPS;
3227 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3229 else if (display_ellipsis_p)
3230 setup_for_ellipsis (it);
3234 return handled;
3238 /* Make iterator IT return `...' next. */
3240 static void
3241 setup_for_ellipsis (it)
3242 struct it *it;
3244 if (it->dp
3245 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3247 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3248 it->dpvec = v->contents;
3249 it->dpend = v->contents + v->size;
3251 else
3253 /* Default `...'. */
3254 it->dpvec = default_invis_vector;
3255 it->dpend = default_invis_vector + 3;
3258 /* The ellipsis display does not replace the display of the
3259 character at the new position. Indicate this by setting
3260 IT->dpvec_char_len to zero. */
3261 it->dpvec_char_len = 0;
3263 it->current.dpvec_index = 0;
3264 it->method = next_element_from_display_vector;
3269 /***********************************************************************
3270 'display' property
3271 ***********************************************************************/
3273 /* Set up iterator IT from `display' property at its current position.
3274 Called from handle_stop. */
3276 static enum prop_handled
3277 handle_display_prop (it)
3278 struct it *it;
3280 Lisp_Object prop, object;
3281 struct text_pos *position;
3282 int display_replaced_p = 0;
3284 if (STRINGP (it->string))
3286 object = it->string;
3287 position = &it->current.string_pos;
3289 else
3291 object = it->w->buffer;
3292 position = &it->current.pos;
3295 /* Reset those iterator values set from display property values. */
3296 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3297 it->space_width = Qnil;
3298 it->font_height = Qnil;
3299 it->voffset = 0;
3301 /* We don't support recursive `display' properties, i.e. string
3302 values that have a string `display' property, that have a string
3303 `display' property etc. */
3304 if (!it->string_from_display_prop_p)
3305 it->area = TEXT_AREA;
3307 prop = Fget_char_property (make_number (position->charpos),
3308 Qdisplay, object);
3309 if (NILP (prop))
3310 return HANDLED_NORMALLY;
3312 if (CONSP (prop)
3313 /* Simple properties. */
3314 && !EQ (XCAR (prop), Qimage)
3315 && !EQ (XCAR (prop), Qspace)
3316 && !EQ (XCAR (prop), Qwhen)
3317 && !EQ (XCAR (prop), Qslice)
3318 && !EQ (XCAR (prop), Qspace_width)
3319 && !EQ (XCAR (prop), Qheight)
3320 && !EQ (XCAR (prop), Qraise)
3321 /* Marginal area specifications. */
3322 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3323 && !EQ (XCAR (prop), Qleft_fringe)
3324 && !EQ (XCAR (prop), Qright_fringe)
3325 && !NILP (XCAR (prop)))
3327 for (; CONSP (prop); prop = XCDR (prop))
3329 if (handle_single_display_prop (it, XCAR (prop), object,
3330 position, display_replaced_p))
3331 display_replaced_p = 1;
3334 else if (VECTORP (prop))
3336 int i;
3337 for (i = 0; i < ASIZE (prop); ++i)
3338 if (handle_single_display_prop (it, AREF (prop, i), object,
3339 position, display_replaced_p))
3340 display_replaced_p = 1;
3342 else
3344 if (handle_single_display_prop (it, prop, object, position, 0))
3345 display_replaced_p = 1;
3348 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3352 /* Value is the position of the end of the `display' property starting
3353 at START_POS in OBJECT. */
3355 static struct text_pos
3356 display_prop_end (it, object, start_pos)
3357 struct it *it;
3358 Lisp_Object object;
3359 struct text_pos start_pos;
3361 Lisp_Object end;
3362 struct text_pos end_pos;
3364 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3365 Qdisplay, object, Qnil);
3366 CHARPOS (end_pos) = XFASTINT (end);
3367 if (STRINGP (object))
3368 compute_string_pos (&end_pos, start_pos, it->string);
3369 else
3370 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3372 return end_pos;
3376 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3377 is the object in which the `display' property was found. *POSITION
3378 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3379 means that we previously saw a display sub-property which already
3380 replaced text display with something else, for example an image;
3381 ignore such properties after the first one has been processed.
3383 If PROP is a `space' or `image' sub-property, set *POSITION to the
3384 end position of the `display' property.
3386 Value is non-zero if something was found which replaces the display
3387 of buffer or string text. */
3389 static int
3390 handle_single_display_prop (it, prop, object, position,
3391 display_replaced_before_p)
3392 struct it *it;
3393 Lisp_Object prop;
3394 Lisp_Object object;
3395 struct text_pos *position;
3396 int display_replaced_before_p;
3398 Lisp_Object value;
3399 int replaces_text_display_p = 0;
3400 Lisp_Object form;
3402 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3403 evaluated. If the result is nil, VALUE is ignored. */
3404 form = Qt;
3405 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3407 prop = XCDR (prop);
3408 if (!CONSP (prop))
3409 return 0;
3410 form = XCAR (prop);
3411 prop = XCDR (prop);
3414 if (!NILP (form) && !EQ (form, Qt))
3416 int count = SPECPDL_INDEX ();
3417 struct gcpro gcpro1;
3419 /* Bind `object' to the object having the `display' property, a
3420 buffer or string. Bind `position' to the position in the
3421 object where the property was found, and `buffer-position'
3422 to the current position in the buffer. */
3423 specbind (Qobject, object);
3424 specbind (Qposition, make_number (CHARPOS (*position)));
3425 specbind (Qbuffer_position,
3426 make_number (STRINGP (object)
3427 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3428 GCPRO1 (form);
3429 form = safe_eval (form);
3430 UNGCPRO;
3431 unbind_to (count, Qnil);
3434 if (NILP (form))
3435 return 0;
3437 if (CONSP (prop)
3438 && EQ (XCAR (prop), Qheight)
3439 && CONSP (XCDR (prop)))
3441 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3442 return 0;
3444 /* `(height HEIGHT)'. */
3445 it->font_height = XCAR (XCDR (prop));
3446 if (!NILP (it->font_height))
3448 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3449 int new_height = -1;
3451 if (CONSP (it->font_height)
3452 && (EQ (XCAR (it->font_height), Qplus)
3453 || EQ (XCAR (it->font_height), Qminus))
3454 && CONSP (XCDR (it->font_height))
3455 && INTEGERP (XCAR (XCDR (it->font_height))))
3457 /* `(+ N)' or `(- N)' where N is an integer. */
3458 int steps = XINT (XCAR (XCDR (it->font_height)));
3459 if (EQ (XCAR (it->font_height), Qplus))
3460 steps = - steps;
3461 it->face_id = smaller_face (it->f, it->face_id, steps);
3463 else if (FUNCTIONP (it->font_height))
3465 /* Call function with current height as argument.
3466 Value is the new height. */
3467 Lisp_Object height;
3468 height = safe_call1 (it->font_height,
3469 face->lface[LFACE_HEIGHT_INDEX]);
3470 if (NUMBERP (height))
3471 new_height = XFLOATINT (height);
3473 else if (NUMBERP (it->font_height))
3475 /* Value is a multiple of the canonical char height. */
3476 struct face *face;
3478 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3479 new_height = (XFLOATINT (it->font_height)
3480 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3482 else
3484 /* Evaluate IT->font_height with `height' bound to the
3485 current specified height to get the new height. */
3486 Lisp_Object value;
3487 int count = SPECPDL_INDEX ();
3489 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3490 value = safe_eval (it->font_height);
3491 unbind_to (count, Qnil);
3493 if (NUMBERP (value))
3494 new_height = XFLOATINT (value);
3497 if (new_height > 0)
3498 it->face_id = face_with_height (it->f, it->face_id, new_height);
3501 else if (CONSP (prop)
3502 && EQ (XCAR (prop), Qspace_width)
3503 && CONSP (XCDR (prop)))
3505 /* `(space_width WIDTH)'. */
3506 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3507 return 0;
3509 value = XCAR (XCDR (prop));
3510 if (NUMBERP (value) && XFLOATINT (value) > 0)
3511 it->space_width = value;
3513 else if (CONSP (prop)
3514 && EQ (XCAR (prop), Qslice))
3516 /* `(slice X Y WIDTH HEIGHT)'. */
3517 Lisp_Object tem;
3519 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3520 return 0;
3522 if (tem = XCDR (prop), CONSP (tem))
3524 it->slice.x = XCAR (tem);
3525 if (tem = XCDR (tem), CONSP (tem))
3527 it->slice.y = XCAR (tem);
3528 if (tem = XCDR (tem), CONSP (tem))
3530 it->slice.width = XCAR (tem);
3531 if (tem = XCDR (tem), CONSP (tem))
3532 it->slice.height = XCAR (tem);
3537 else if (CONSP (prop)
3538 && EQ (XCAR (prop), Qraise)
3539 && CONSP (XCDR (prop)))
3541 /* `(raise FACTOR)'. */
3542 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3543 return 0;
3545 #ifdef HAVE_WINDOW_SYSTEM
3546 value = XCAR (XCDR (prop));
3547 if (NUMBERP (value))
3549 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3550 it->voffset = - (XFLOATINT (value)
3551 * (FONT_HEIGHT (face->font)));
3553 #endif /* HAVE_WINDOW_SYSTEM */
3555 else if (!it->string_from_display_prop_p)
3557 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3558 VALUE) or `((margin nil) VALUE)' or VALUE. */
3559 Lisp_Object location, value;
3560 struct text_pos start_pos;
3561 int valid_p;
3563 /* Characters having this form of property are not displayed, so
3564 we have to find the end of the property. */
3565 start_pos = *position;
3566 *position = display_prop_end (it, object, start_pos);
3567 value = Qnil;
3569 /* Let's stop at the new position and assume that all
3570 text properties change there. */
3571 it->stop_charpos = position->charpos;
3573 if (CONSP (prop)
3574 && (EQ (XCAR (prop), Qleft_fringe)
3575 || EQ (XCAR (prop), Qright_fringe))
3576 && CONSP (XCDR (prop)))
3578 unsigned face_id = DEFAULT_FACE_ID;
3579 int fringe_bitmap;
3581 /* Save current settings of IT so that we can restore them
3582 when we are finished with the glyph property value. */
3584 /* `(left-fringe BITMAP FACE)'. */
3585 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3586 return 0;
3588 #ifdef HAVE_WINDOW_SYSTEM
3589 value = XCAR (XCDR (prop));
3590 if (!SYMBOLP (value)
3591 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3592 return 0;
3594 if (CONSP (XCDR (XCDR (prop))))
3596 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3598 face_id = lookup_named_face (it->f, face_name, 'A');
3599 if (face_id < 0)
3600 return 0;
3603 push_it (it);
3605 it->area = TEXT_AREA;
3606 it->what = IT_IMAGE;
3607 it->image_id = -1; /* no image */
3608 it->position = start_pos;
3609 it->object = NILP (object) ? it->w->buffer : object;
3610 it->method = next_element_from_image;
3611 it->face_id = face_id;
3613 /* Say that we haven't consumed the characters with
3614 `display' property yet. The call to pop_it in
3615 set_iterator_to_next will clean this up. */
3616 *position = start_pos;
3618 if (EQ (XCAR (prop), Qleft_fringe))
3620 it->left_user_fringe_bitmap = fringe_bitmap;
3621 it->left_user_fringe_face_id = face_id;
3623 else
3625 it->right_user_fringe_bitmap = fringe_bitmap;
3626 it->right_user_fringe_face_id = face_id;
3628 #endif /* HAVE_WINDOW_SYSTEM */
3629 return 1;
3632 location = Qunbound;
3633 if (CONSP (prop) && CONSP (XCAR (prop)))
3635 Lisp_Object tem;
3637 value = XCDR (prop);
3638 if (CONSP (value))
3639 value = XCAR (value);
3641 tem = XCAR (prop);
3642 if (EQ (XCAR (tem), Qmargin)
3643 && (tem = XCDR (tem),
3644 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3645 (NILP (tem)
3646 || EQ (tem, Qleft_margin)
3647 || EQ (tem, Qright_margin))))
3648 location = tem;
3651 if (EQ (location, Qunbound))
3653 location = Qnil;
3654 value = prop;
3657 valid_p = (STRINGP (value)
3658 #ifdef HAVE_WINDOW_SYSTEM
3659 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3660 #endif /* not HAVE_WINDOW_SYSTEM */
3661 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3663 if ((EQ (location, Qleft_margin)
3664 || EQ (location, Qright_margin)
3665 || NILP (location))
3666 && valid_p
3667 && !display_replaced_before_p)
3669 replaces_text_display_p = 1;
3671 /* Save current settings of IT so that we can restore them
3672 when we are finished with the glyph property value. */
3673 push_it (it);
3675 if (NILP (location))
3676 it->area = TEXT_AREA;
3677 else if (EQ (location, Qleft_margin))
3678 it->area = LEFT_MARGIN_AREA;
3679 else
3680 it->area = RIGHT_MARGIN_AREA;
3682 if (STRINGP (value))
3684 it->string = value;
3685 it->multibyte_p = STRING_MULTIBYTE (it->string);
3686 it->current.overlay_string_index = -1;
3687 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3688 it->end_charpos = it->string_nchars = SCHARS (it->string);
3689 it->method = next_element_from_string;
3690 it->stop_charpos = 0;
3691 it->string_from_display_prop_p = 1;
3692 /* Say that we haven't consumed the characters with
3693 `display' property yet. The call to pop_it in
3694 set_iterator_to_next will clean this up. */
3695 *position = start_pos;
3697 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3699 it->method = next_element_from_stretch;
3700 it->object = value;
3701 it->current.pos = it->position = start_pos;
3703 #ifdef HAVE_WINDOW_SYSTEM
3704 else
3706 it->what = IT_IMAGE;
3707 it->image_id = lookup_image (it->f, value);
3708 it->position = start_pos;
3709 it->object = NILP (object) ? it->w->buffer : object;
3710 it->method = next_element_from_image;
3712 /* Say that we haven't consumed the characters with
3713 `display' property yet. The call to pop_it in
3714 set_iterator_to_next will clean this up. */
3715 *position = start_pos;
3717 #endif /* HAVE_WINDOW_SYSTEM */
3719 else
3720 /* Invalid property or property not supported. Restore
3721 the position to what it was before. */
3722 *position = start_pos;
3725 return replaces_text_display_p;
3729 /* Check if PROP is a display sub-property value whose text should be
3730 treated as intangible. */
3732 static int
3733 single_display_prop_intangible_p (prop)
3734 Lisp_Object prop;
3736 /* Skip over `when FORM'. */
3737 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3739 prop = XCDR (prop);
3740 if (!CONSP (prop))
3741 return 0;
3742 prop = XCDR (prop);
3745 if (STRINGP (prop))
3746 return 1;
3748 if (!CONSP (prop))
3749 return 0;
3751 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3752 we don't need to treat text as intangible. */
3753 if (EQ (XCAR (prop), Qmargin))
3755 prop = XCDR (prop);
3756 if (!CONSP (prop))
3757 return 0;
3759 prop = XCDR (prop);
3760 if (!CONSP (prop)
3761 || EQ (XCAR (prop), Qleft_margin)
3762 || EQ (XCAR (prop), Qright_margin))
3763 return 0;
3766 return (CONSP (prop)
3767 && (EQ (XCAR (prop), Qimage)
3768 || EQ (XCAR (prop), Qspace)));
3772 /* Check if PROP is a display property value whose text should be
3773 treated as intangible. */
3776 display_prop_intangible_p (prop)
3777 Lisp_Object prop;
3779 if (CONSP (prop)
3780 && CONSP (XCAR (prop))
3781 && !EQ (Qmargin, XCAR (XCAR (prop))))
3783 /* A list of sub-properties. */
3784 while (CONSP (prop))
3786 if (single_display_prop_intangible_p (XCAR (prop)))
3787 return 1;
3788 prop = XCDR (prop);
3791 else if (VECTORP (prop))
3793 /* A vector of sub-properties. */
3794 int i;
3795 for (i = 0; i < ASIZE (prop); ++i)
3796 if (single_display_prop_intangible_p (AREF (prop, i)))
3797 return 1;
3799 else
3800 return single_display_prop_intangible_p (prop);
3802 return 0;
3806 /* Return 1 if PROP is a display sub-property value containing STRING. */
3808 static int
3809 single_display_prop_string_p (prop, string)
3810 Lisp_Object prop, string;
3812 if (EQ (string, prop))
3813 return 1;
3815 /* Skip over `when FORM'. */
3816 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3821 prop = XCDR (prop);
3824 if (CONSP (prop))
3825 /* Skip over `margin LOCATION'. */
3826 if (EQ (XCAR (prop), Qmargin))
3828 prop = XCDR (prop);
3829 if (!CONSP (prop))
3830 return 0;
3832 prop = XCDR (prop);
3833 if (!CONSP (prop))
3834 return 0;
3837 return CONSP (prop) && EQ (XCAR (prop), string);
3841 /* Return 1 if STRING appears in the `display' property PROP. */
3843 static int
3844 display_prop_string_p (prop, string)
3845 Lisp_Object prop, string;
3847 if (CONSP (prop)
3848 && CONSP (XCAR (prop))
3849 && !EQ (Qmargin, XCAR (XCAR (prop))))
3851 /* A list of sub-properties. */
3852 while (CONSP (prop))
3854 if (single_display_prop_string_p (XCAR (prop), string))
3855 return 1;
3856 prop = XCDR (prop);
3859 else if (VECTORP (prop))
3861 /* A vector of sub-properties. */
3862 int i;
3863 for (i = 0; i < ASIZE (prop); ++i)
3864 if (single_display_prop_string_p (AREF (prop, i), string))
3865 return 1;
3867 else
3868 return single_display_prop_string_p (prop, string);
3870 return 0;
3874 /* Determine from which buffer position in W's buffer STRING comes
3875 from. AROUND_CHARPOS is an approximate position where it could
3876 be from. Value is the buffer position or 0 if it couldn't be
3877 determined.
3879 W's buffer must be current.
3881 This function is necessary because we don't record buffer positions
3882 in glyphs generated from strings (to keep struct glyph small).
3883 This function may only use code that doesn't eval because it is
3884 called asynchronously from note_mouse_highlight. */
3887 string_buffer_position (w, string, around_charpos)
3888 struct window *w;
3889 Lisp_Object string;
3890 int around_charpos;
3892 Lisp_Object limit, prop, pos;
3893 const int MAX_DISTANCE = 1000;
3894 int found = 0;
3896 pos = make_number (around_charpos);
3897 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3898 while (!found && !EQ (pos, limit))
3900 prop = Fget_char_property (pos, Qdisplay, Qnil);
3901 if (!NILP (prop) && display_prop_string_p (prop, string))
3902 found = 1;
3903 else
3904 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3907 if (!found)
3909 pos = make_number (around_charpos);
3910 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3911 while (!found && !EQ (pos, limit))
3913 prop = Fget_char_property (pos, Qdisplay, Qnil);
3914 if (!NILP (prop) && display_prop_string_p (prop, string))
3915 found = 1;
3916 else
3917 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3918 limit);
3922 return found ? XINT (pos) : 0;
3927 /***********************************************************************
3928 `composition' property
3929 ***********************************************************************/
3931 /* Set up iterator IT from `composition' property at its current
3932 position. Called from handle_stop. */
3934 static enum prop_handled
3935 handle_composition_prop (it)
3936 struct it *it;
3938 Lisp_Object prop, string;
3939 int pos, pos_byte, end;
3940 enum prop_handled handled = HANDLED_NORMALLY;
3942 if (STRINGP (it->string))
3944 pos = IT_STRING_CHARPOS (*it);
3945 pos_byte = IT_STRING_BYTEPOS (*it);
3946 string = it->string;
3948 else
3950 pos = IT_CHARPOS (*it);
3951 pos_byte = IT_BYTEPOS (*it);
3952 string = Qnil;
3955 /* If there's a valid composition and point is not inside of the
3956 composition (in the case that the composition is from the current
3957 buffer), draw a glyph composed from the composition components. */
3958 if (find_composition (pos, -1, &pos, &end, &prop, string)
3959 && COMPOSITION_VALID_P (pos, end, prop)
3960 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3962 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3964 if (id >= 0)
3966 it->method = next_element_from_composition;
3967 it->cmp_id = id;
3968 it->cmp_len = COMPOSITION_LENGTH (prop);
3969 /* For a terminal, draw only the first character of the
3970 components. */
3971 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3972 it->len = (STRINGP (it->string)
3973 ? string_char_to_byte (it->string, end)
3974 : CHAR_TO_BYTE (end)) - pos_byte;
3975 it->stop_charpos = end;
3976 handled = HANDLED_RETURN;
3980 return handled;
3985 /***********************************************************************
3986 Overlay strings
3987 ***********************************************************************/
3989 /* The following structure is used to record overlay strings for
3990 later sorting in load_overlay_strings. */
3992 struct overlay_entry
3994 Lisp_Object overlay;
3995 Lisp_Object string;
3996 int priority;
3997 int after_string_p;
4001 /* Set up iterator IT from overlay strings at its current position.
4002 Called from handle_stop. */
4004 static enum prop_handled
4005 handle_overlay_change (it)
4006 struct it *it;
4008 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4009 return HANDLED_RECOMPUTE_PROPS;
4010 else
4011 return HANDLED_NORMALLY;
4015 /* Set up the next overlay string for delivery by IT, if there is an
4016 overlay string to deliver. Called by set_iterator_to_next when the
4017 end of the current overlay string is reached. If there are more
4018 overlay strings to display, IT->string and
4019 IT->current.overlay_string_index are set appropriately here.
4020 Otherwise IT->string is set to nil. */
4022 static void
4023 next_overlay_string (it)
4024 struct it *it;
4026 ++it->current.overlay_string_index;
4027 if (it->current.overlay_string_index == it->n_overlay_strings)
4029 /* No more overlay strings. Restore IT's settings to what
4030 they were before overlay strings were processed, and
4031 continue to deliver from current_buffer. */
4032 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4034 pop_it (it);
4035 xassert (it->stop_charpos >= BEGV
4036 && it->stop_charpos <= it->end_charpos);
4037 it->string = Qnil;
4038 it->current.overlay_string_index = -1;
4039 SET_TEXT_POS (it->current.string_pos, -1, -1);
4040 it->n_overlay_strings = 0;
4041 it->method = next_element_from_buffer;
4043 /* If we're at the end of the buffer, record that we have
4044 processed the overlay strings there already, so that
4045 next_element_from_buffer doesn't try it again. */
4046 if (IT_CHARPOS (*it) >= it->end_charpos)
4047 it->overlay_strings_at_end_processed_p = 1;
4049 /* If we have to display `...' for invisible text, set
4050 the iterator up for that. */
4051 if (display_ellipsis_p)
4052 setup_for_ellipsis (it);
4054 else
4056 /* There are more overlay strings to process. If
4057 IT->current.overlay_string_index has advanced to a position
4058 where we must load IT->overlay_strings with more strings, do
4059 it. */
4060 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4062 if (it->current.overlay_string_index && i == 0)
4063 load_overlay_strings (it, 0);
4065 /* Initialize IT to deliver display elements from the overlay
4066 string. */
4067 it->string = it->overlay_strings[i];
4068 it->multibyte_p = STRING_MULTIBYTE (it->string);
4069 SET_TEXT_POS (it->current.string_pos, 0, 0);
4070 it->method = next_element_from_string;
4071 it->stop_charpos = 0;
4074 CHECK_IT (it);
4078 /* Compare two overlay_entry structures E1 and E2. Used as a
4079 comparison function for qsort in load_overlay_strings. Overlay
4080 strings for the same position are sorted so that
4082 1. All after-strings come in front of before-strings, except
4083 when they come from the same overlay.
4085 2. Within after-strings, strings are sorted so that overlay strings
4086 from overlays with higher priorities come first.
4088 2. Within before-strings, strings are sorted so that overlay
4089 strings from overlays with higher priorities come last.
4091 Value is analogous to strcmp. */
4094 static int
4095 compare_overlay_entries (e1, e2)
4096 void *e1, *e2;
4098 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4099 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4100 int result;
4102 if (entry1->after_string_p != entry2->after_string_p)
4104 /* Let after-strings appear in front of before-strings if
4105 they come from different overlays. */
4106 if (EQ (entry1->overlay, entry2->overlay))
4107 result = entry1->after_string_p ? 1 : -1;
4108 else
4109 result = entry1->after_string_p ? -1 : 1;
4111 else if (entry1->after_string_p)
4112 /* After-strings sorted in order of decreasing priority. */
4113 result = entry2->priority - entry1->priority;
4114 else
4115 /* Before-strings sorted in order of increasing priority. */
4116 result = entry1->priority - entry2->priority;
4118 return result;
4122 /* Load the vector IT->overlay_strings with overlay strings from IT's
4123 current buffer position, or from CHARPOS if that is > 0. Set
4124 IT->n_overlays to the total number of overlay strings found.
4126 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4127 a time. On entry into load_overlay_strings,
4128 IT->current.overlay_string_index gives the number of overlay
4129 strings that have already been loaded by previous calls to this
4130 function.
4132 IT->add_overlay_start contains an additional overlay start
4133 position to consider for taking overlay strings from, if non-zero.
4134 This position comes into play when the overlay has an `invisible'
4135 property, and both before and after-strings. When we've skipped to
4136 the end of the overlay, because of its `invisible' property, we
4137 nevertheless want its before-string to appear.
4138 IT->add_overlay_start will contain the overlay start position
4139 in this case.
4141 Overlay strings are sorted so that after-string strings come in
4142 front of before-string strings. Within before and after-strings,
4143 strings are sorted by overlay priority. See also function
4144 compare_overlay_entries. */
4146 static void
4147 load_overlay_strings (it, charpos)
4148 struct it *it;
4149 int charpos;
4151 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4152 Lisp_Object overlay, window, str, invisible;
4153 struct Lisp_Overlay *ov;
4154 int start, end;
4155 int size = 20;
4156 int n = 0, i, j, invis_p;
4157 struct overlay_entry *entries
4158 = (struct overlay_entry *) alloca (size * sizeof *entries);
4160 if (charpos <= 0)
4161 charpos = IT_CHARPOS (*it);
4163 /* Append the overlay string STRING of overlay OVERLAY to vector
4164 `entries' which has size `size' and currently contains `n'
4165 elements. AFTER_P non-zero means STRING is an after-string of
4166 OVERLAY. */
4167 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4168 do \
4170 Lisp_Object priority; \
4172 if (n == size) \
4174 int new_size = 2 * size; \
4175 struct overlay_entry *old = entries; \
4176 entries = \
4177 (struct overlay_entry *) alloca (new_size \
4178 * sizeof *entries); \
4179 bcopy (old, entries, size * sizeof *entries); \
4180 size = new_size; \
4183 entries[n].string = (STRING); \
4184 entries[n].overlay = (OVERLAY); \
4185 priority = Foverlay_get ((OVERLAY), Qpriority); \
4186 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4187 entries[n].after_string_p = (AFTER_P); \
4188 ++n; \
4190 while (0)
4192 /* Process overlay before the overlay center. */
4193 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4195 XSETMISC (overlay, ov);
4196 xassert (OVERLAYP (overlay));
4197 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4198 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4200 if (end < charpos)
4201 break;
4203 /* Skip this overlay if it doesn't start or end at IT's current
4204 position. */
4205 if (end != charpos && start != charpos)
4206 continue;
4208 /* Skip this overlay if it doesn't apply to IT->w. */
4209 window = Foverlay_get (overlay, Qwindow);
4210 if (WINDOWP (window) && XWINDOW (window) != it->w)
4211 continue;
4213 /* If the text ``under'' the overlay is invisible, both before-
4214 and after-strings from this overlay are visible; start and
4215 end position are indistinguishable. */
4216 invisible = Foverlay_get (overlay, Qinvisible);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4219 /* If overlay has a non-empty before-string, record it. */
4220 if ((start == charpos || (end == charpos && invis_p))
4221 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4222 && SCHARS (str))
4223 RECORD_OVERLAY_STRING (overlay, str, 0);
4225 /* If overlay has a non-empty after-string, record it. */
4226 if ((end == charpos || (start == charpos && invis_p))
4227 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4228 && SCHARS (str))
4229 RECORD_OVERLAY_STRING (overlay, str, 1);
4232 /* Process overlays after the overlay center. */
4233 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4235 XSETMISC (overlay, ov);
4236 xassert (OVERLAYP (overlay));
4237 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4238 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4240 if (start > charpos)
4241 break;
4243 /* Skip this overlay if it doesn't start or end at IT's current
4244 position. */
4245 if (end != charpos && start != charpos)
4246 continue;
4248 /* Skip this overlay if it doesn't apply to IT->w. */
4249 window = Foverlay_get (overlay, Qwindow);
4250 if (WINDOWP (window) && XWINDOW (window) != it->w)
4251 continue;
4253 /* If the text ``under'' the overlay is invisible, it has a zero
4254 dimension, and both before- and after-strings apply. */
4255 invisible = Foverlay_get (overlay, Qinvisible);
4256 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4258 /* If overlay has a non-empty before-string, record it. */
4259 if ((start == charpos || (end == charpos && invis_p))
4260 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4261 && SCHARS (str))
4262 RECORD_OVERLAY_STRING (overlay, str, 0);
4264 /* If overlay has a non-empty after-string, record it. */
4265 if ((end == charpos || (start == charpos && invis_p))
4266 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4267 && SCHARS (str))
4268 RECORD_OVERLAY_STRING (overlay, str, 1);
4271 #undef RECORD_OVERLAY_STRING
4273 /* Sort entries. */
4274 if (n > 1)
4275 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4277 /* Record the total number of strings to process. */
4278 it->n_overlay_strings = n;
4280 /* IT->current.overlay_string_index is the number of overlay strings
4281 that have already been consumed by IT. Copy some of the
4282 remaining overlay strings to IT->overlay_strings. */
4283 i = 0;
4284 j = it->current.overlay_string_index;
4285 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4286 it->overlay_strings[i++] = entries[j++].string;
4288 CHECK_IT (it);
4292 /* Get the first chunk of overlay strings at IT's current buffer
4293 position, or at CHARPOS if that is > 0. Value is non-zero if at
4294 least one overlay string was found. */
4296 static int
4297 get_overlay_strings (it, charpos)
4298 struct it *it;
4299 int charpos;
4301 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4302 process. This fills IT->overlay_strings with strings, and sets
4303 IT->n_overlay_strings to the total number of strings to process.
4304 IT->pos.overlay_string_index has to be set temporarily to zero
4305 because load_overlay_strings needs this; it must be set to -1
4306 when no overlay strings are found because a zero value would
4307 indicate a position in the first overlay string. */
4308 it->current.overlay_string_index = 0;
4309 load_overlay_strings (it, charpos);
4311 /* If we found overlay strings, set up IT to deliver display
4312 elements from the first one. Otherwise set up IT to deliver
4313 from current_buffer. */
4314 if (it->n_overlay_strings)
4316 /* Make sure we know settings in current_buffer, so that we can
4317 restore meaningful values when we're done with the overlay
4318 strings. */
4319 compute_stop_pos (it);
4320 xassert (it->face_id >= 0);
4322 /* Save IT's settings. They are restored after all overlay
4323 strings have been processed. */
4324 xassert (it->sp == 0);
4325 push_it (it);
4327 /* Set up IT to deliver display elements from the first overlay
4328 string. */
4329 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4330 it->string = it->overlay_strings[0];
4331 it->stop_charpos = 0;
4332 xassert (STRINGP (it->string));
4333 it->end_charpos = SCHARS (it->string);
4334 it->multibyte_p = STRING_MULTIBYTE (it->string);
4335 it->method = next_element_from_string;
4337 else
4339 it->string = Qnil;
4340 it->current.overlay_string_index = -1;
4341 it->method = next_element_from_buffer;
4344 CHECK_IT (it);
4346 /* Value is non-zero if we found at least one overlay string. */
4347 return STRINGP (it->string);
4352 /***********************************************************************
4353 Saving and restoring state
4354 ***********************************************************************/
4356 /* Save current settings of IT on IT->stack. Called, for example,
4357 before setting up IT for an overlay string, to be able to restore
4358 IT's settings to what they were after the overlay string has been
4359 processed. */
4361 static void
4362 push_it (it)
4363 struct it *it;
4365 struct iterator_stack_entry *p;
4367 xassert (it->sp < 2);
4368 p = it->stack + it->sp;
4370 p->stop_charpos = it->stop_charpos;
4371 xassert (it->face_id >= 0);
4372 p->face_id = it->face_id;
4373 p->string = it->string;
4374 p->pos = it->current;
4375 p->end_charpos = it->end_charpos;
4376 p->string_nchars = it->string_nchars;
4377 p->area = it->area;
4378 p->multibyte_p = it->multibyte_p;
4379 p->slice = it->slice;
4380 p->space_width = it->space_width;
4381 p->font_height = it->font_height;
4382 p->voffset = it->voffset;
4383 p->string_from_display_prop_p = it->string_from_display_prop_p;
4384 p->display_ellipsis_p = 0;
4385 ++it->sp;
4389 /* Restore IT's settings from IT->stack. Called, for example, when no
4390 more overlay strings must be processed, and we return to delivering
4391 display elements from a buffer, or when the end of a string from a
4392 `display' property is reached and we return to delivering display
4393 elements from an overlay string, or from a buffer. */
4395 static void
4396 pop_it (it)
4397 struct it *it;
4399 struct iterator_stack_entry *p;
4401 xassert (it->sp > 0);
4402 --it->sp;
4403 p = it->stack + it->sp;
4404 it->stop_charpos = p->stop_charpos;
4405 it->face_id = p->face_id;
4406 it->string = p->string;
4407 it->current = p->pos;
4408 it->end_charpos = p->end_charpos;
4409 it->string_nchars = p->string_nchars;
4410 it->area = p->area;
4411 it->multibyte_p = p->multibyte_p;
4412 it->slice = p->slice;
4413 it->space_width = p->space_width;
4414 it->font_height = p->font_height;
4415 it->voffset = p->voffset;
4416 it->string_from_display_prop_p = p->string_from_display_prop_p;
4421 /***********************************************************************
4422 Moving over lines
4423 ***********************************************************************/
4425 /* Set IT's current position to the previous line start. */
4427 static void
4428 back_to_previous_line_start (it)
4429 struct it *it;
4431 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4432 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4436 /* Move IT to the next line start.
4438 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4439 we skipped over part of the text (as opposed to moving the iterator
4440 continuously over the text). Otherwise, don't change the value
4441 of *SKIPPED_P.
4443 Newlines may come from buffer text, overlay strings, or strings
4444 displayed via the `display' property. That's the reason we can't
4445 simply use find_next_newline_no_quit.
4447 Note that this function may not skip over invisible text that is so
4448 because of text properties and immediately follows a newline. If
4449 it would, function reseat_at_next_visible_line_start, when called
4450 from set_iterator_to_next, would effectively make invisible
4451 characters following a newline part of the wrong glyph row, which
4452 leads to wrong cursor motion. */
4454 static int
4455 forward_to_next_line_start (it, skipped_p)
4456 struct it *it;
4457 int *skipped_p;
4459 int old_selective, newline_found_p, n;
4460 const int MAX_NEWLINE_DISTANCE = 500;
4462 /* If already on a newline, just consume it to avoid unintended
4463 skipping over invisible text below. */
4464 if (it->what == IT_CHARACTER
4465 && it->c == '\n'
4466 && CHARPOS (it->position) == IT_CHARPOS (*it))
4468 set_iterator_to_next (it, 0);
4469 it->c = 0;
4470 return 1;
4473 /* Don't handle selective display in the following. It's (a)
4474 unnecessary because it's done by the caller, and (b) leads to an
4475 infinite recursion because next_element_from_ellipsis indirectly
4476 calls this function. */
4477 old_selective = it->selective;
4478 it->selective = 0;
4480 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4481 from buffer text. */
4482 for (n = newline_found_p = 0;
4483 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4484 n += STRINGP (it->string) ? 0 : 1)
4486 if (!get_next_display_element (it))
4487 return 0;
4488 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4489 set_iterator_to_next (it, 0);
4492 /* If we didn't find a newline near enough, see if we can use a
4493 short-cut. */
4494 if (!newline_found_p)
4496 int start = IT_CHARPOS (*it);
4497 int limit = find_next_newline_no_quit (start, 1);
4498 Lisp_Object pos;
4500 xassert (!STRINGP (it->string));
4502 /* If there isn't any `display' property in sight, and no
4503 overlays, we can just use the position of the newline in
4504 buffer text. */
4505 if (it->stop_charpos >= limit
4506 || ((pos = Fnext_single_property_change (make_number (start),
4507 Qdisplay,
4508 Qnil, make_number (limit)),
4509 NILP (pos))
4510 && next_overlay_change (start) == ZV))
4512 IT_CHARPOS (*it) = limit;
4513 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4514 *skipped_p = newline_found_p = 1;
4516 else
4518 while (get_next_display_element (it)
4519 && !newline_found_p)
4521 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4522 set_iterator_to_next (it, 0);
4527 it->selective = old_selective;
4528 return newline_found_p;
4532 /* Set IT's current position to the previous visible line start. Skip
4533 invisible text that is so either due to text properties or due to
4534 selective display. Caution: this does not change IT->current_x and
4535 IT->hpos. */
4537 static void
4538 back_to_previous_visible_line_start (it)
4539 struct it *it;
4541 int visible_p = 0;
4543 /* Go back one newline if not on BEGV already. */
4544 if (IT_CHARPOS (*it) > BEGV)
4545 back_to_previous_line_start (it);
4547 /* Move over lines that are invisible because of selective display
4548 or text properties. */
4549 while (IT_CHARPOS (*it) > BEGV
4550 && !visible_p)
4552 visible_p = 1;
4554 /* If selective > 0, then lines indented more than that values
4555 are invisible. */
4556 if (it->selective > 0
4557 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4558 (double) it->selective)) /* iftc */
4559 visible_p = 0;
4560 else
4562 Lisp_Object prop;
4564 /* Check the newline before point for invisibility. */
4565 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4566 Qinvisible, it->window);
4567 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4568 visible_p = 0;
4571 if (visible_p)
4573 struct it it2 = *it;
4575 if (handle_display_prop (&it2) == HANDLED_RETURN)
4576 visible_p = 0;
4579 /* Back one more newline if the current one is invisible. */
4580 if (!visible_p)
4581 back_to_previous_line_start (it);
4584 xassert (IT_CHARPOS (*it) >= BEGV);
4585 xassert (IT_CHARPOS (*it) == BEGV
4586 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4587 CHECK_IT (it);
4591 /* Reseat iterator IT at the previous visible line start. Skip
4592 invisible text that is so either due to text properties or due to
4593 selective display. At the end, update IT's overlay information,
4594 face information etc. */
4596 void
4597 reseat_at_previous_visible_line_start (it)
4598 struct it *it;
4600 back_to_previous_visible_line_start (it);
4601 reseat (it, it->current.pos, 1);
4602 CHECK_IT (it);
4606 /* Reseat iterator IT on the next visible line start in the current
4607 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4608 preceding the line start. Skip over invisible text that is so
4609 because of selective display. Compute faces, overlays etc at the
4610 new position. Note that this function does not skip over text that
4611 is invisible because of text properties. */
4613 static void
4614 reseat_at_next_visible_line_start (it, on_newline_p)
4615 struct it *it;
4616 int on_newline_p;
4618 int newline_found_p, skipped_p = 0;
4620 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4622 /* Skip over lines that are invisible because they are indented
4623 more than the value of IT->selective. */
4624 if (it->selective > 0)
4625 while (IT_CHARPOS (*it) < ZV
4626 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4627 (double) it->selective)) /* iftc */
4629 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4630 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4633 /* Position on the newline if that's what's requested. */
4634 if (on_newline_p && newline_found_p)
4636 if (STRINGP (it->string))
4638 if (IT_STRING_CHARPOS (*it) > 0)
4640 --IT_STRING_CHARPOS (*it);
4641 --IT_STRING_BYTEPOS (*it);
4644 else if (IT_CHARPOS (*it) > BEGV)
4646 --IT_CHARPOS (*it);
4647 --IT_BYTEPOS (*it);
4648 reseat (it, it->current.pos, 0);
4651 else if (skipped_p)
4652 reseat (it, it->current.pos, 0);
4654 CHECK_IT (it);
4659 /***********************************************************************
4660 Changing an iterator's position
4661 ***********************************************************************/
4663 /* Change IT's current position to POS in current_buffer. If FORCE_P
4664 is non-zero, always check for text properties at the new position.
4665 Otherwise, text properties are only looked up if POS >=
4666 IT->check_charpos of a property. */
4668 static void
4669 reseat (it, pos, force_p)
4670 struct it *it;
4671 struct text_pos pos;
4672 int force_p;
4674 int original_pos = IT_CHARPOS (*it);
4676 reseat_1 (it, pos, 0);
4678 /* Determine where to check text properties. Avoid doing it
4679 where possible because text property lookup is very expensive. */
4680 if (force_p
4681 || CHARPOS (pos) > it->stop_charpos
4682 || CHARPOS (pos) < original_pos)
4683 handle_stop (it);
4685 CHECK_IT (it);
4689 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4690 IT->stop_pos to POS, also. */
4692 static void
4693 reseat_1 (it, pos, set_stop_p)
4694 struct it *it;
4695 struct text_pos pos;
4696 int set_stop_p;
4698 /* Don't call this function when scanning a C string. */
4699 xassert (it->s == NULL);
4701 /* POS must be a reasonable value. */
4702 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4704 it->current.pos = it->position = pos;
4705 XSETBUFFER (it->object, current_buffer);
4706 it->end_charpos = ZV;
4707 it->dpvec = NULL;
4708 it->current.dpvec_index = -1;
4709 it->current.overlay_string_index = -1;
4710 IT_STRING_CHARPOS (*it) = -1;
4711 IT_STRING_BYTEPOS (*it) = -1;
4712 it->string = Qnil;
4713 it->method = next_element_from_buffer;
4714 /* RMS: I added this to fix a bug in move_it_vertically_backward
4715 where it->area continued to relate to the starting point
4716 for the backward motion. Bug report from
4717 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4718 However, I am not sure whether reseat still does the right thing
4719 in general after this change. */
4720 it->area = TEXT_AREA;
4721 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4722 it->sp = 0;
4723 it->face_before_selective_p = 0;
4725 if (set_stop_p)
4726 it->stop_charpos = CHARPOS (pos);
4730 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4731 If S is non-null, it is a C string to iterate over. Otherwise,
4732 STRING gives a Lisp string to iterate over.
4734 If PRECISION > 0, don't return more then PRECISION number of
4735 characters from the string.
4737 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4738 characters have been returned. FIELD_WIDTH < 0 means an infinite
4739 field width.
4741 MULTIBYTE = 0 means disable processing of multibyte characters,
4742 MULTIBYTE > 0 means enable it,
4743 MULTIBYTE < 0 means use IT->multibyte_p.
4745 IT must be initialized via a prior call to init_iterator before
4746 calling this function. */
4748 static void
4749 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4750 struct it *it;
4751 unsigned char *s;
4752 Lisp_Object string;
4753 int charpos;
4754 int precision, field_width, multibyte;
4756 /* No region in strings. */
4757 it->region_beg_charpos = it->region_end_charpos = -1;
4759 /* No text property checks performed by default, but see below. */
4760 it->stop_charpos = -1;
4762 /* Set iterator position and end position. */
4763 bzero (&it->current, sizeof it->current);
4764 it->current.overlay_string_index = -1;
4765 it->current.dpvec_index = -1;
4766 xassert (charpos >= 0);
4768 /* If STRING is specified, use its multibyteness, otherwise use the
4769 setting of MULTIBYTE, if specified. */
4770 if (multibyte >= 0)
4771 it->multibyte_p = multibyte > 0;
4773 if (s == NULL)
4775 xassert (STRINGP (string));
4776 it->string = string;
4777 it->s = NULL;
4778 it->end_charpos = it->string_nchars = SCHARS (string);
4779 it->method = next_element_from_string;
4780 it->current.string_pos = string_pos (charpos, string);
4782 else
4784 it->s = s;
4785 it->string = Qnil;
4787 /* Note that we use IT->current.pos, not it->current.string_pos,
4788 for displaying C strings. */
4789 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4790 if (it->multibyte_p)
4792 it->current.pos = c_string_pos (charpos, s, 1);
4793 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4795 else
4797 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4798 it->end_charpos = it->string_nchars = strlen (s);
4801 it->method = next_element_from_c_string;
4804 /* PRECISION > 0 means don't return more than PRECISION characters
4805 from the string. */
4806 if (precision > 0 && it->end_charpos - charpos > precision)
4807 it->end_charpos = it->string_nchars = charpos + precision;
4809 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4810 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4811 FIELD_WIDTH < 0 means infinite field width. This is useful for
4812 padding with `-' at the end of a mode line. */
4813 if (field_width < 0)
4814 field_width = INFINITY;
4815 if (field_width > it->end_charpos - charpos)
4816 it->end_charpos = charpos + field_width;
4818 /* Use the standard display table for displaying strings. */
4819 if (DISP_TABLE_P (Vstandard_display_table))
4820 it->dp = XCHAR_TABLE (Vstandard_display_table);
4822 it->stop_charpos = charpos;
4823 CHECK_IT (it);
4828 /***********************************************************************
4829 Iteration
4830 ***********************************************************************/
4832 /* Load IT's display element fields with information about the next
4833 display element from the current position of IT. Value is zero if
4834 end of buffer (or C string) is reached. */
4837 get_next_display_element (it)
4838 struct it *it;
4840 /* Non-zero means that we found a display element. Zero means that
4841 we hit the end of what we iterate over. Performance note: the
4842 function pointer `method' used here turns out to be faster than
4843 using a sequence of if-statements. */
4844 int success_p = (*it->method) (it);
4846 if (it->what == IT_CHARACTER)
4848 /* Map via display table or translate control characters.
4849 IT->c, IT->len etc. have been set to the next character by
4850 the function call above. If we have a display table, and it
4851 contains an entry for IT->c, translate it. Don't do this if
4852 IT->c itself comes from a display table, otherwise we could
4853 end up in an infinite recursion. (An alternative could be to
4854 count the recursion depth of this function and signal an
4855 error when a certain maximum depth is reached.) Is it worth
4856 it? */
4857 if (success_p && it->dpvec == NULL)
4859 Lisp_Object dv;
4861 if (it->dp
4862 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4863 VECTORP (dv)))
4865 struct Lisp_Vector *v = XVECTOR (dv);
4867 /* Return the first character from the display table
4868 entry, if not empty. If empty, don't display the
4869 current character. */
4870 if (v->size)
4872 it->dpvec_char_len = it->len;
4873 it->dpvec = v->contents;
4874 it->dpend = v->contents + v->size;
4875 it->current.dpvec_index = 0;
4876 it->method = next_element_from_display_vector;
4877 success_p = get_next_display_element (it);
4879 else
4881 set_iterator_to_next (it, 0);
4882 success_p = get_next_display_element (it);
4886 /* Translate control characters into `\003' or `^C' form.
4887 Control characters coming from a display table entry are
4888 currently not translated because we use IT->dpvec to hold
4889 the translation. This could easily be changed but I
4890 don't believe that it is worth doing.
4892 If it->multibyte_p is nonzero, eight-bit characters and
4893 non-printable multibyte characters are also translated to
4894 octal form.
4896 If it->multibyte_p is zero, eight-bit characters that
4897 don't have corresponding multibyte char code are also
4898 translated to octal form. */
4899 else if ((it->c < ' '
4900 && (it->area != TEXT_AREA
4901 /* In mode line, treat \n like other crl chars. */
4902 || (it->c != '\n'
4903 && it->glyph_row && it->glyph_row->mode_line_p)
4904 || (it->c != '\n' && it->c != '\t')))
4905 || (it->multibyte_p
4906 ? ((it->c >= 127
4907 && it->len == 1)
4908 || !CHAR_PRINTABLE_P (it->c))
4909 : (it->c >= 127
4910 && (!unibyte_display_via_language_environment
4911 || it->c == unibyte_char_to_multibyte (it->c)))))
4913 /* IT->c is a control character which must be displayed
4914 either as '\003' or as `^C' where the '\\' and '^'
4915 can be defined in the display table. Fill
4916 IT->ctl_chars with glyphs for what we have to
4917 display. Then, set IT->dpvec to these glyphs. */
4918 GLYPH g;
4920 if (it->c < 128 && it->ctl_arrow_p)
4922 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4923 if (it->dp
4924 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4925 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4926 g = XINT (DISP_CTRL_GLYPH (it->dp));
4927 else
4928 g = FAST_MAKE_GLYPH ('^', 0);
4929 XSETINT (it->ctl_chars[0], g);
4931 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4932 XSETINT (it->ctl_chars[1], g);
4934 /* Set up IT->dpvec and return first character from it. */
4935 it->dpvec_char_len = it->len;
4936 it->dpvec = it->ctl_chars;
4937 it->dpend = it->dpvec + 2;
4938 it->current.dpvec_index = 0;
4939 it->method = next_element_from_display_vector;
4940 get_next_display_element (it);
4942 else
4944 unsigned char str[MAX_MULTIBYTE_LENGTH];
4945 int len;
4946 int i;
4947 GLYPH escape_glyph;
4949 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4950 if (it->dp
4951 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4952 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4953 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4954 else
4955 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4957 if (SINGLE_BYTE_CHAR_P (it->c))
4958 str[0] = it->c, len = 1;
4959 else
4961 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4962 if (len < 0)
4964 /* It's an invalid character, which
4965 shouldn't happen actually, but due to
4966 bugs it may happen. Let's print the char
4967 as is, there's not much meaningful we can
4968 do with it. */
4969 str[0] = it->c;
4970 str[1] = it->c >> 8;
4971 str[2] = it->c >> 16;
4972 str[3] = it->c >> 24;
4973 len = 4;
4977 for (i = 0; i < len; i++)
4979 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4980 /* Insert three more glyphs into IT->ctl_chars for
4981 the octal display of the character. */
4982 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4983 XSETINT (it->ctl_chars[i * 4 + 1], g);
4984 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4985 XSETINT (it->ctl_chars[i * 4 + 2], g);
4986 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4987 XSETINT (it->ctl_chars[i * 4 + 3], g);
4990 /* Set up IT->dpvec and return the first character
4991 from it. */
4992 it->dpvec_char_len = it->len;
4993 it->dpvec = it->ctl_chars;
4994 it->dpend = it->dpvec + len * 4;
4995 it->current.dpvec_index = 0;
4996 it->method = next_element_from_display_vector;
4997 get_next_display_element (it);
5002 /* Adjust face id for a multibyte character. There are no
5003 multibyte character in unibyte text. */
5004 if (it->multibyte_p
5005 && success_p
5006 && FRAME_WINDOW_P (it->f))
5008 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5009 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5013 /* Is this character the last one of a run of characters with
5014 box? If yes, set IT->end_of_box_run_p to 1. */
5015 if (it->face_box_p
5016 && it->s == NULL)
5018 int face_id;
5019 struct face *face;
5021 it->end_of_box_run_p
5022 = ((face_id = face_after_it_pos (it),
5023 face_id != it->face_id)
5024 && (face = FACE_FROM_ID (it->f, face_id),
5025 face->box == FACE_NO_BOX));
5028 /* Value is 0 if end of buffer or string reached. */
5029 return success_p;
5033 /* Move IT to the next display element.
5035 RESEAT_P non-zero means if called on a newline in buffer text,
5036 skip to the next visible line start.
5038 Functions get_next_display_element and set_iterator_to_next are
5039 separate because I find this arrangement easier to handle than a
5040 get_next_display_element function that also increments IT's
5041 position. The way it is we can first look at an iterator's current
5042 display element, decide whether it fits on a line, and if it does,
5043 increment the iterator position. The other way around we probably
5044 would either need a flag indicating whether the iterator has to be
5045 incremented the next time, or we would have to implement a
5046 decrement position function which would not be easy to write. */
5048 void
5049 set_iterator_to_next (it, reseat_p)
5050 struct it *it;
5051 int reseat_p;
5053 /* Reset flags indicating start and end of a sequence of characters
5054 with box. Reset them at the start of this function because
5055 moving the iterator to a new position might set them. */
5056 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5058 if (it->method == next_element_from_buffer)
5060 /* The current display element of IT is a character from
5061 current_buffer. Advance in the buffer, and maybe skip over
5062 invisible lines that are so because of selective display. */
5063 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5064 reseat_at_next_visible_line_start (it, 0);
5065 else
5067 xassert (it->len != 0);
5068 IT_BYTEPOS (*it) += it->len;
5069 IT_CHARPOS (*it) += 1;
5070 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5073 else if (it->method == next_element_from_composition)
5075 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5076 if (STRINGP (it->string))
5078 IT_STRING_BYTEPOS (*it) += it->len;
5079 IT_STRING_CHARPOS (*it) += it->cmp_len;
5080 it->method = next_element_from_string;
5081 goto consider_string_end;
5083 else
5085 IT_BYTEPOS (*it) += it->len;
5086 IT_CHARPOS (*it) += it->cmp_len;
5087 it->method = next_element_from_buffer;
5090 else if (it->method == next_element_from_c_string)
5092 /* Current display element of IT is from a C string. */
5093 IT_BYTEPOS (*it) += it->len;
5094 IT_CHARPOS (*it) += 1;
5096 else if (it->method == next_element_from_display_vector)
5098 /* Current display element of IT is from a display table entry.
5099 Advance in the display table definition. Reset it to null if
5100 end reached, and continue with characters from buffers/
5101 strings. */
5102 ++it->current.dpvec_index;
5104 /* Restore face of the iterator to what they were before the
5105 display vector entry (these entries may contain faces). */
5106 it->face_id = it->saved_face_id;
5108 if (it->dpvec + it->current.dpvec_index == it->dpend)
5110 if (it->s)
5111 it->method = next_element_from_c_string;
5112 else if (STRINGP (it->string))
5113 it->method = next_element_from_string;
5114 else
5115 it->method = next_element_from_buffer;
5117 it->dpvec = NULL;
5118 it->current.dpvec_index = -1;
5120 /* Skip over characters which were displayed via IT->dpvec. */
5121 if (it->dpvec_char_len < 0)
5122 reseat_at_next_visible_line_start (it, 1);
5123 else if (it->dpvec_char_len > 0)
5125 it->len = it->dpvec_char_len;
5126 set_iterator_to_next (it, reseat_p);
5130 else if (it->method == next_element_from_string)
5132 /* Current display element is a character from a Lisp string. */
5133 xassert (it->s == NULL && STRINGP (it->string));
5134 IT_STRING_BYTEPOS (*it) += it->len;
5135 IT_STRING_CHARPOS (*it) += 1;
5137 consider_string_end:
5139 if (it->current.overlay_string_index >= 0)
5141 /* IT->string is an overlay string. Advance to the
5142 next, if there is one. */
5143 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5144 next_overlay_string (it);
5146 else
5148 /* IT->string is not an overlay string. If we reached
5149 its end, and there is something on IT->stack, proceed
5150 with what is on the stack. This can be either another
5151 string, this time an overlay string, or a buffer. */
5152 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5153 && it->sp > 0)
5155 pop_it (it);
5156 if (!STRINGP (it->string))
5157 it->method = next_element_from_buffer;
5158 else
5159 goto consider_string_end;
5163 else if (it->method == next_element_from_image
5164 || it->method == next_element_from_stretch)
5166 /* The position etc with which we have to proceed are on
5167 the stack. The position may be at the end of a string,
5168 if the `display' property takes up the whole string. */
5169 pop_it (it);
5170 it->image_id = 0;
5171 if (STRINGP (it->string))
5173 it->method = next_element_from_string;
5174 goto consider_string_end;
5176 else
5177 it->method = next_element_from_buffer;
5179 else
5180 /* There are no other methods defined, so this should be a bug. */
5181 abort ();
5183 xassert (it->method != next_element_from_string
5184 || (STRINGP (it->string)
5185 && IT_STRING_CHARPOS (*it) >= 0));
5189 /* Load IT's display element fields with information about the next
5190 display element which comes from a display table entry or from the
5191 result of translating a control character to one of the forms `^C'
5192 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5194 static int
5195 next_element_from_display_vector (it)
5196 struct it *it;
5198 /* Precondition. */
5199 xassert (it->dpvec && it->current.dpvec_index >= 0);
5201 /* Remember the current face id in case glyphs specify faces.
5202 IT's face is restored in set_iterator_to_next. */
5203 it->saved_face_id = it->face_id;
5205 if (INTEGERP (*it->dpvec)
5206 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5208 int lface_id;
5209 GLYPH g;
5211 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5212 it->c = FAST_GLYPH_CHAR (g);
5213 it->len = CHAR_BYTES (it->c);
5215 /* The entry may contain a face id to use. Such a face id is
5216 the id of a Lisp face, not a realized face. A face id of
5217 zero means no face is specified. */
5218 lface_id = FAST_GLYPH_FACE (g);
5219 if (lface_id)
5221 /* The function returns -1 if lface_id is invalid. */
5222 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5223 if (face_id >= 0)
5224 it->face_id = face_id;
5227 else
5228 /* Display table entry is invalid. Return a space. */
5229 it->c = ' ', it->len = 1;
5231 /* Don't change position and object of the iterator here. They are
5232 still the values of the character that had this display table
5233 entry or was translated, and that's what we want. */
5234 it->what = IT_CHARACTER;
5235 return 1;
5239 /* Load IT with the next display element from Lisp string IT->string.
5240 IT->current.string_pos is the current position within the string.
5241 If IT->current.overlay_string_index >= 0, the Lisp string is an
5242 overlay string. */
5244 static int
5245 next_element_from_string (it)
5246 struct it *it;
5248 struct text_pos position;
5250 xassert (STRINGP (it->string));
5251 xassert (IT_STRING_CHARPOS (*it) >= 0);
5252 position = it->current.string_pos;
5254 /* Time to check for invisible text? */
5255 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5256 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5258 handle_stop (it);
5260 /* Since a handler may have changed IT->method, we must
5261 recurse here. */
5262 return get_next_display_element (it);
5265 if (it->current.overlay_string_index >= 0)
5267 /* Get the next character from an overlay string. In overlay
5268 strings, There is no field width or padding with spaces to
5269 do. */
5270 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5272 it->what = IT_EOB;
5273 return 0;
5275 else if (STRING_MULTIBYTE (it->string))
5277 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5278 const unsigned char *s = (SDATA (it->string)
5279 + IT_STRING_BYTEPOS (*it));
5280 it->c = string_char_and_length (s, remaining, &it->len);
5282 else
5284 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5285 it->len = 1;
5288 else
5290 /* Get the next character from a Lisp string that is not an
5291 overlay string. Such strings come from the mode line, for
5292 example. We may have to pad with spaces, or truncate the
5293 string. See also next_element_from_c_string. */
5294 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5296 it->what = IT_EOB;
5297 return 0;
5299 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5301 /* Pad with spaces. */
5302 it->c = ' ', it->len = 1;
5303 CHARPOS (position) = BYTEPOS (position) = -1;
5305 else if (STRING_MULTIBYTE (it->string))
5307 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5308 const unsigned char *s = (SDATA (it->string)
5309 + IT_STRING_BYTEPOS (*it));
5310 it->c = string_char_and_length (s, maxlen, &it->len);
5312 else
5314 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5315 it->len = 1;
5319 /* Record what we have and where it came from. Note that we store a
5320 buffer position in IT->position although it could arguably be a
5321 string position. */
5322 it->what = IT_CHARACTER;
5323 it->object = it->string;
5324 it->position = position;
5325 return 1;
5329 /* Load IT with next display element from C string IT->s.
5330 IT->string_nchars is the maximum number of characters to return
5331 from the string. IT->end_charpos may be greater than
5332 IT->string_nchars when this function is called, in which case we
5333 may have to return padding spaces. Value is zero if end of string
5334 reached, including padding spaces. */
5336 static int
5337 next_element_from_c_string (it)
5338 struct it *it;
5340 int success_p = 1;
5342 xassert (it->s);
5343 it->what = IT_CHARACTER;
5344 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5345 it->object = Qnil;
5347 /* IT's position can be greater IT->string_nchars in case a field
5348 width or precision has been specified when the iterator was
5349 initialized. */
5350 if (IT_CHARPOS (*it) >= it->end_charpos)
5352 /* End of the game. */
5353 it->what = IT_EOB;
5354 success_p = 0;
5356 else if (IT_CHARPOS (*it) >= it->string_nchars)
5358 /* Pad with spaces. */
5359 it->c = ' ', it->len = 1;
5360 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5362 else if (it->multibyte_p)
5364 /* Implementation note: The calls to strlen apparently aren't a
5365 performance problem because there is no noticeable performance
5366 difference between Emacs running in unibyte or multibyte mode. */
5367 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5368 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5369 maxlen, &it->len);
5371 else
5372 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5374 return success_p;
5378 /* Set up IT to return characters from an ellipsis, if appropriate.
5379 The definition of the ellipsis glyphs may come from a display table
5380 entry. This function Fills IT with the first glyph from the
5381 ellipsis if an ellipsis is to be displayed. */
5383 static int
5384 next_element_from_ellipsis (it)
5385 struct it *it;
5387 if (it->selective_display_ellipsis_p)
5389 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5391 /* Use the display table definition for `...'. Invalid glyphs
5392 will be handled by the method returning elements from dpvec. */
5393 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5394 it->dpvec_char_len = it->len;
5395 it->dpvec = v->contents;
5396 it->dpend = v->contents + v->size;
5397 it->current.dpvec_index = 0;
5398 it->method = next_element_from_display_vector;
5400 else
5402 /* Use default `...' which is stored in default_invis_vector. */
5403 it->dpvec_char_len = it->len;
5404 it->dpvec = default_invis_vector;
5405 it->dpend = default_invis_vector + 3;
5406 it->current.dpvec_index = 0;
5407 it->method = next_element_from_display_vector;
5410 else
5412 /* The face at the current position may be different from the
5413 face we find after the invisible text. Remember what it
5414 was in IT->saved_face_id, and signal that it's there by
5415 setting face_before_selective_p. */
5416 it->saved_face_id = it->face_id;
5417 it->method = next_element_from_buffer;
5418 reseat_at_next_visible_line_start (it, 1);
5419 it->face_before_selective_p = 1;
5422 return get_next_display_element (it);
5426 /* Deliver an image display element. The iterator IT is already
5427 filled with image information (done in handle_display_prop). Value
5428 is always 1. */
5431 static int
5432 next_element_from_image (it)
5433 struct it *it;
5435 it->what = IT_IMAGE;
5436 return 1;
5440 /* Fill iterator IT with next display element from a stretch glyph
5441 property. IT->object is the value of the text property. Value is
5442 always 1. */
5444 static int
5445 next_element_from_stretch (it)
5446 struct it *it;
5448 it->what = IT_STRETCH;
5449 return 1;
5453 /* Load IT with the next display element from current_buffer. Value
5454 is zero if end of buffer reached. IT->stop_charpos is the next
5455 position at which to stop and check for text properties or buffer
5456 end. */
5458 static int
5459 next_element_from_buffer (it)
5460 struct it *it;
5462 int success_p = 1;
5464 /* Check this assumption, otherwise, we would never enter the
5465 if-statement, below. */
5466 xassert (IT_CHARPOS (*it) >= BEGV
5467 && IT_CHARPOS (*it) <= it->stop_charpos);
5469 if (IT_CHARPOS (*it) >= it->stop_charpos)
5471 if (IT_CHARPOS (*it) >= it->end_charpos)
5473 int overlay_strings_follow_p;
5475 /* End of the game, except when overlay strings follow that
5476 haven't been returned yet. */
5477 if (it->overlay_strings_at_end_processed_p)
5478 overlay_strings_follow_p = 0;
5479 else
5481 it->overlay_strings_at_end_processed_p = 1;
5482 overlay_strings_follow_p = get_overlay_strings (it, 0);
5485 if (overlay_strings_follow_p)
5486 success_p = get_next_display_element (it);
5487 else
5489 it->what = IT_EOB;
5490 it->position = it->current.pos;
5491 success_p = 0;
5494 else
5496 handle_stop (it);
5497 return get_next_display_element (it);
5500 else
5502 /* No face changes, overlays etc. in sight, so just return a
5503 character from current_buffer. */
5504 unsigned char *p;
5506 /* Maybe run the redisplay end trigger hook. Performance note:
5507 This doesn't seem to cost measurable time. */
5508 if (it->redisplay_end_trigger_charpos
5509 && it->glyph_row
5510 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5511 run_redisplay_end_trigger_hook (it);
5513 /* Get the next character, maybe multibyte. */
5514 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5515 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5517 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5518 - IT_BYTEPOS (*it));
5519 it->c = string_char_and_length (p, maxlen, &it->len);
5521 else
5522 it->c = *p, it->len = 1;
5524 /* Record what we have and where it came from. */
5525 it->what = IT_CHARACTER;;
5526 it->object = it->w->buffer;
5527 it->position = it->current.pos;
5529 /* Normally we return the character found above, except when we
5530 really want to return an ellipsis for selective display. */
5531 if (it->selective)
5533 if (it->c == '\n')
5535 /* A value of selective > 0 means hide lines indented more
5536 than that number of columns. */
5537 if (it->selective > 0
5538 && IT_CHARPOS (*it) + 1 < ZV
5539 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5540 IT_BYTEPOS (*it) + 1,
5541 (double) it->selective)) /* iftc */
5543 success_p = next_element_from_ellipsis (it);
5544 it->dpvec_char_len = -1;
5547 else if (it->c == '\r' && it->selective == -1)
5549 /* A value of selective == -1 means that everything from the
5550 CR to the end of the line is invisible, with maybe an
5551 ellipsis displayed for it. */
5552 success_p = next_element_from_ellipsis (it);
5553 it->dpvec_char_len = -1;
5558 /* Value is zero if end of buffer reached. */
5559 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5560 return success_p;
5564 /* Run the redisplay end trigger hook for IT. */
5566 static void
5567 run_redisplay_end_trigger_hook (it)
5568 struct it *it;
5570 Lisp_Object args[3];
5572 /* IT->glyph_row should be non-null, i.e. we should be actually
5573 displaying something, or otherwise we should not run the hook. */
5574 xassert (it->glyph_row);
5576 /* Set up hook arguments. */
5577 args[0] = Qredisplay_end_trigger_functions;
5578 args[1] = it->window;
5579 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5580 it->redisplay_end_trigger_charpos = 0;
5582 /* Since we are *trying* to run these functions, don't try to run
5583 them again, even if they get an error. */
5584 it->w->redisplay_end_trigger = Qnil;
5585 Frun_hook_with_args (3, args);
5587 /* Notice if it changed the face of the character we are on. */
5588 handle_face_prop (it);
5592 /* Deliver a composition display element. The iterator IT is already
5593 filled with composition information (done in
5594 handle_composition_prop). Value is always 1. */
5596 static int
5597 next_element_from_composition (it)
5598 struct it *it;
5600 it->what = IT_COMPOSITION;
5601 it->position = (STRINGP (it->string)
5602 ? it->current.string_pos
5603 : it->current.pos);
5604 return 1;
5609 /***********************************************************************
5610 Moving an iterator without producing glyphs
5611 ***********************************************************************/
5613 /* Move iterator IT to a specified buffer or X position within one
5614 line on the display without producing glyphs.
5616 OP should be a bit mask including some or all of these bits:
5617 MOVE_TO_X: Stop on reaching x-position TO_X.
5618 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5619 Regardless of OP's value, stop in reaching the end of the display line.
5621 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5622 This means, in particular, that TO_X includes window's horizontal
5623 scroll amount.
5625 The return value has several possible values that
5626 say what condition caused the scan to stop:
5628 MOVE_POS_MATCH_OR_ZV
5629 - when TO_POS or ZV was reached.
5631 MOVE_X_REACHED
5632 -when TO_X was reached before TO_POS or ZV were reached.
5634 MOVE_LINE_CONTINUED
5635 - when we reached the end of the display area and the line must
5636 be continued.
5638 MOVE_LINE_TRUNCATED
5639 - when we reached the end of the display area and the line is
5640 truncated.
5642 MOVE_NEWLINE_OR_CR
5643 - when we stopped at a line end, i.e. a newline or a CR and selective
5644 display is on. */
5646 static enum move_it_result
5647 move_it_in_display_line_to (it, to_charpos, to_x, op)
5648 struct it *it;
5649 int to_charpos, to_x, op;
5651 enum move_it_result result = MOVE_UNDEFINED;
5652 struct glyph_row *saved_glyph_row;
5654 /* Don't produce glyphs in produce_glyphs. */
5655 saved_glyph_row = it->glyph_row;
5656 it->glyph_row = NULL;
5658 #define BUFFER_POS_REACHED_P() \
5659 ((op & MOVE_TO_POS) != 0 \
5660 && BUFFERP (it->object) \
5661 && IT_CHARPOS (*it) >= to_charpos)
5663 while (1)
5665 int x, i, ascent = 0, descent = 0;
5667 /* Stop when ZV reached.
5668 We used to stop here when TO_CHARPOS reached as well, but that is
5669 too soon if this glyph does not fit on this line. So we handle it
5670 explicitly below. */
5671 if (!get_next_display_element (it)
5672 || (it->truncate_lines_p
5673 && BUFFER_POS_REACHED_P ()))
5675 result = MOVE_POS_MATCH_OR_ZV;
5676 break;
5679 /* The call to produce_glyphs will get the metrics of the
5680 display element IT is loaded with. We record in x the
5681 x-position before this display element in case it does not
5682 fit on the line. */
5683 x = it->current_x;
5685 /* Remember the line height so far in case the next element doesn't
5686 fit on the line. */
5687 if (!it->truncate_lines_p)
5689 ascent = it->max_ascent;
5690 descent = it->max_descent;
5693 PRODUCE_GLYPHS (it);
5695 if (it->area != TEXT_AREA)
5697 set_iterator_to_next (it, 1);
5698 continue;
5701 /* The number of glyphs we get back in IT->nglyphs will normally
5702 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5703 character on a terminal frame, or (iii) a line end. For the
5704 second case, IT->nglyphs - 1 padding glyphs will be present
5705 (on X frames, there is only one glyph produced for a
5706 composite character.
5708 The behavior implemented below means, for continuation lines,
5709 that as many spaces of a TAB as fit on the current line are
5710 displayed there. For terminal frames, as many glyphs of a
5711 multi-glyph character are displayed in the current line, too.
5712 This is what the old redisplay code did, and we keep it that
5713 way. Under X, the whole shape of a complex character must
5714 fit on the line or it will be completely displayed in the
5715 next line.
5717 Note that both for tabs and padding glyphs, all glyphs have
5718 the same width. */
5719 if (it->nglyphs)
5721 /* More than one glyph or glyph doesn't fit on line. All
5722 glyphs have the same width. */
5723 int single_glyph_width = it->pixel_width / it->nglyphs;
5724 int new_x;
5726 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5728 new_x = x + single_glyph_width;
5730 /* We want to leave anything reaching TO_X to the caller. */
5731 if ((op & MOVE_TO_X) && new_x > to_x)
5733 if (BUFFER_POS_REACHED_P ())
5734 goto buffer_pos_reached;
5735 it->current_x = x;
5736 result = MOVE_X_REACHED;
5737 break;
5739 else if (/* Lines are continued. */
5740 !it->truncate_lines_p
5741 && (/* And glyph doesn't fit on the line. */
5742 new_x > it->last_visible_x
5743 /* Or it fits exactly and we're on a window
5744 system frame. */
5745 || (new_x == it->last_visible_x
5746 && FRAME_WINDOW_P (it->f))))
5748 if (/* IT->hpos == 0 means the very first glyph
5749 doesn't fit on the line, e.g. a wide image. */
5750 it->hpos == 0
5751 || (new_x == it->last_visible_x
5752 && FRAME_WINDOW_P (it->f)))
5754 ++it->hpos;
5755 it->current_x = new_x;
5756 if (i == it->nglyphs - 1)
5758 set_iterator_to_next (it, 1);
5759 #ifdef HAVE_WINDOW_SYSTEM
5760 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5762 if (!get_next_display_element (it))
5764 result = MOVE_POS_MATCH_OR_ZV;
5765 break;
5767 if (BUFFER_POS_REACHED_P ())
5769 if (ITERATOR_AT_END_OF_LINE_P (it))
5770 result = MOVE_POS_MATCH_OR_ZV;
5771 else
5772 result = MOVE_LINE_CONTINUED;
5773 break;
5775 if (ITERATOR_AT_END_OF_LINE_P (it))
5777 result = MOVE_NEWLINE_OR_CR;
5778 break;
5781 #endif /* HAVE_WINDOW_SYSTEM */
5784 else
5786 it->current_x = x;
5787 it->max_ascent = ascent;
5788 it->max_descent = descent;
5791 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5792 IT_CHARPOS (*it)));
5793 result = MOVE_LINE_CONTINUED;
5794 break;
5796 else if (BUFFER_POS_REACHED_P ())
5797 goto buffer_pos_reached;
5798 else if (new_x > it->first_visible_x)
5800 /* Glyph is visible. Increment number of glyphs that
5801 would be displayed. */
5802 ++it->hpos;
5804 else
5806 /* Glyph is completely off the left margin of the display
5807 area. Nothing to do. */
5811 if (result != MOVE_UNDEFINED)
5812 break;
5814 else if (BUFFER_POS_REACHED_P ())
5816 buffer_pos_reached:
5817 it->current_x = x;
5818 it->max_ascent = ascent;
5819 it->max_descent = descent;
5820 result = MOVE_POS_MATCH_OR_ZV;
5821 break;
5823 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5825 /* Stop when TO_X specified and reached. This check is
5826 necessary here because of lines consisting of a line end,
5827 only. The line end will not produce any glyphs and we
5828 would never get MOVE_X_REACHED. */
5829 xassert (it->nglyphs == 0);
5830 result = MOVE_X_REACHED;
5831 break;
5834 /* Is this a line end? If yes, we're done. */
5835 if (ITERATOR_AT_END_OF_LINE_P (it))
5837 result = MOVE_NEWLINE_OR_CR;
5838 break;
5841 /* The current display element has been consumed. Advance
5842 to the next. */
5843 set_iterator_to_next (it, 1);
5845 /* Stop if lines are truncated and IT's current x-position is
5846 past the right edge of the window now. */
5847 if (it->truncate_lines_p
5848 && it->current_x >= it->last_visible_x)
5850 #ifdef HAVE_WINDOW_SYSTEM
5851 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5853 if (!get_next_display_element (it)
5854 || BUFFER_POS_REACHED_P ())
5856 result = MOVE_POS_MATCH_OR_ZV;
5857 break;
5859 if (ITERATOR_AT_END_OF_LINE_P (it))
5861 result = MOVE_NEWLINE_OR_CR;
5862 break;
5865 #endif /* HAVE_WINDOW_SYSTEM */
5866 result = MOVE_LINE_TRUNCATED;
5867 break;
5871 #undef BUFFER_POS_REACHED_P
5873 /* Restore the iterator settings altered at the beginning of this
5874 function. */
5875 it->glyph_row = saved_glyph_row;
5876 return result;
5880 /* Move IT forward until it satisfies one or more of the criteria in
5881 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5883 OP is a bit-mask that specifies where to stop, and in particular,
5884 which of those four position arguments makes a difference. See the
5885 description of enum move_operation_enum.
5887 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5888 screen line, this function will set IT to the next position >
5889 TO_CHARPOS. */
5891 void
5892 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5893 struct it *it;
5894 int to_charpos, to_x, to_y, to_vpos;
5895 int op;
5897 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5898 int line_height;
5899 int reached = 0;
5901 for (;;)
5903 if (op & MOVE_TO_VPOS)
5905 /* If no TO_CHARPOS and no TO_X specified, stop at the
5906 start of the line TO_VPOS. */
5907 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5909 if (it->vpos == to_vpos)
5911 reached = 1;
5912 break;
5914 else
5915 skip = move_it_in_display_line_to (it, -1, -1, 0);
5917 else
5919 /* TO_VPOS >= 0 means stop at TO_X in the line at
5920 TO_VPOS, or at TO_POS, whichever comes first. */
5921 if (it->vpos == to_vpos)
5923 reached = 2;
5924 break;
5927 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5929 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5931 reached = 3;
5932 break;
5934 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5936 /* We have reached TO_X but not in the line we want. */
5937 skip = move_it_in_display_line_to (it, to_charpos,
5938 -1, MOVE_TO_POS);
5939 if (skip == MOVE_POS_MATCH_OR_ZV)
5941 reached = 4;
5942 break;
5947 else if (op & MOVE_TO_Y)
5949 struct it it_backup;
5951 /* TO_Y specified means stop at TO_X in the line containing
5952 TO_Y---or at TO_CHARPOS if this is reached first. The
5953 problem is that we can't really tell whether the line
5954 contains TO_Y before we have completely scanned it, and
5955 this may skip past TO_X. What we do is to first scan to
5956 TO_X.
5958 If TO_X is not specified, use a TO_X of zero. The reason
5959 is to make the outcome of this function more predictable.
5960 If we didn't use TO_X == 0, we would stop at the end of
5961 the line which is probably not what a caller would expect
5962 to happen. */
5963 skip = move_it_in_display_line_to (it, to_charpos,
5964 ((op & MOVE_TO_X)
5965 ? to_x : 0),
5966 (MOVE_TO_X
5967 | (op & MOVE_TO_POS)));
5969 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5970 if (skip == MOVE_POS_MATCH_OR_ZV)
5972 reached = 5;
5973 break;
5976 /* If TO_X was reached, we would like to know whether TO_Y
5977 is in the line. This can only be said if we know the
5978 total line height which requires us to scan the rest of
5979 the line. */
5980 if (skip == MOVE_X_REACHED)
5982 it_backup = *it;
5983 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5984 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5985 op & MOVE_TO_POS);
5986 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5989 /* Now, decide whether TO_Y is in this line. */
5990 line_height = it->max_ascent + it->max_descent;
5991 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5993 if (to_y >= it->current_y
5994 && to_y < it->current_y + line_height)
5996 if (skip == MOVE_X_REACHED)
5997 /* If TO_Y is in this line and TO_X was reached above,
5998 we scanned too far. We have to restore IT's settings
5999 to the ones before skipping. */
6000 *it = it_backup;
6001 reached = 6;
6003 else if (skip == MOVE_X_REACHED)
6005 skip = skip2;
6006 if (skip == MOVE_POS_MATCH_OR_ZV)
6007 reached = 7;
6010 if (reached)
6011 break;
6013 else
6014 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6016 switch (skip)
6018 case MOVE_POS_MATCH_OR_ZV:
6019 reached = 8;
6020 goto out;
6022 case MOVE_NEWLINE_OR_CR:
6023 set_iterator_to_next (it, 1);
6024 it->continuation_lines_width = 0;
6025 break;
6027 case MOVE_LINE_TRUNCATED:
6028 it->continuation_lines_width = 0;
6029 reseat_at_next_visible_line_start (it, 0);
6030 if ((op & MOVE_TO_POS) != 0
6031 && IT_CHARPOS (*it) > to_charpos)
6033 reached = 9;
6034 goto out;
6036 break;
6038 case MOVE_LINE_CONTINUED:
6039 it->continuation_lines_width += it->current_x;
6040 break;
6042 default:
6043 abort ();
6046 /* Reset/increment for the next run. */
6047 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6048 it->current_x = it->hpos = 0;
6049 it->current_y += it->max_ascent + it->max_descent;
6050 ++it->vpos;
6051 last_height = it->max_ascent + it->max_descent;
6052 last_max_ascent = it->max_ascent;
6053 it->max_ascent = it->max_descent = 0;
6056 out:
6058 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6062 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6064 If DY > 0, move IT backward at least that many pixels. DY = 0
6065 means move IT backward to the preceding line start or BEGV. This
6066 function may move over more than DY pixels if IT->current_y - DY
6067 ends up in the middle of a line; in this case IT->current_y will be
6068 set to the top of the line moved to. */
6070 void
6071 move_it_vertically_backward (it, dy)
6072 struct it *it;
6073 int dy;
6075 int nlines, h;
6076 struct it it2, it3;
6077 int start_pos;
6079 move_further_back:
6080 xassert (dy >= 0);
6082 start_pos = IT_CHARPOS (*it);
6084 /* Estimate how many newlines we must move back. */
6085 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6087 /* Set the iterator's position that many lines back. */
6088 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6089 back_to_previous_visible_line_start (it);
6091 /* Reseat the iterator here. When moving backward, we don't want
6092 reseat to skip forward over invisible text, set up the iterator
6093 to deliver from overlay strings at the new position etc. So,
6094 use reseat_1 here. */
6095 reseat_1 (it, it->current.pos, 1);
6097 /* We are now surely at a line start. */
6098 it->current_x = it->hpos = 0;
6099 it->continuation_lines_width = 0;
6101 /* Move forward and see what y-distance we moved. First move to the
6102 start of the next line so that we get its height. We need this
6103 height to be able to tell whether we reached the specified
6104 y-distance. */
6105 it2 = *it;
6106 it2.max_ascent = it2.max_descent = 0;
6107 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6108 MOVE_TO_POS | MOVE_TO_VPOS);
6109 xassert (IT_CHARPOS (*it) >= BEGV);
6110 it3 = it2;
6112 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6113 xassert (IT_CHARPOS (*it) >= BEGV);
6114 /* H is the actual vertical distance from the position in *IT
6115 and the starting position. */
6116 h = it2.current_y - it->current_y;
6117 /* NLINES is the distance in number of lines. */
6118 nlines = it2.vpos - it->vpos;
6120 /* Correct IT's y and vpos position
6121 so that they are relative to the starting point. */
6122 it->vpos -= nlines;
6123 it->current_y -= h;
6125 if (dy == 0)
6127 /* DY == 0 means move to the start of the screen line. The
6128 value of nlines is > 0 if continuation lines were involved. */
6129 if (nlines > 0)
6130 move_it_by_lines (it, nlines, 1);
6131 xassert (IT_CHARPOS (*it) <= start_pos);
6133 else
6135 /* The y-position we try to reach, relative to *IT.
6136 Note that H has been subtracted in front of the if-statement. */
6137 int target_y = it->current_y + h - dy;
6138 int y0 = it3.current_y;
6139 int y1 = line_bottom_y (&it3);
6140 int line_height = y1 - y0;
6142 /* If we did not reach target_y, try to move further backward if
6143 we can. If we moved too far backward, try to move forward. */
6144 if (target_y < it->current_y
6145 /* This is heuristic. In a window that's 3 lines high, with
6146 a line height of 13 pixels each, recentering with point
6147 on the bottom line will try to move -39/2 = 19 pixels
6148 backward. Try to avoid moving into the first line. */
6149 && it->current_y - target_y > line_height * 2 / 3
6150 && IT_CHARPOS (*it) > BEGV)
6152 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6153 target_y - it->current_y));
6154 dy = it->current_y - target_y;
6155 goto move_further_back;
6157 else if (target_y >= it->current_y + line_height
6158 && IT_CHARPOS (*it) < ZV)
6160 /* Should move forward by at least one line, maybe more.
6162 Note: Calling move_it_by_lines can be expensive on
6163 terminal frames, where compute_motion is used (via
6164 vmotion) to do the job, when there are very long lines
6165 and truncate-lines is nil. That's the reason for
6166 treating terminal frames specially here. */
6168 if (!FRAME_WINDOW_P (it->f))
6169 move_it_vertically (it, target_y - (it->current_y + line_height));
6170 else
6174 move_it_by_lines (it, 1, 1);
6176 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6179 xassert (IT_CHARPOS (*it) >= BEGV);
6185 /* Move IT by a specified amount of pixel lines DY. DY negative means
6186 move backwards. DY = 0 means move to start of screen line. At the
6187 end, IT will be on the start of a screen line. */
6189 void
6190 move_it_vertically (it, dy)
6191 struct it *it;
6192 int dy;
6194 if (dy <= 0)
6195 move_it_vertically_backward (it, -dy);
6196 else
6198 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6199 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6200 MOVE_TO_POS | MOVE_TO_Y);
6201 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6203 /* If buffer ends in ZV without a newline, move to the start of
6204 the line to satisfy the post-condition. */
6205 if (IT_CHARPOS (*it) == ZV
6206 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6207 move_it_by_lines (it, 0, 0);
6212 /* Move iterator IT past the end of the text line it is in. */
6214 void
6215 move_it_past_eol (it)
6216 struct it *it;
6218 enum move_it_result rc;
6220 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6221 if (rc == MOVE_NEWLINE_OR_CR)
6222 set_iterator_to_next (it, 0);
6226 #if 0 /* Currently not used. */
6228 /* Return non-zero if some text between buffer positions START_CHARPOS
6229 and END_CHARPOS is invisible. IT->window is the window for text
6230 property lookup. */
6232 static int
6233 invisible_text_between_p (it, start_charpos, end_charpos)
6234 struct it *it;
6235 int start_charpos, end_charpos;
6237 Lisp_Object prop, limit;
6238 int invisible_found_p;
6240 xassert (it != NULL && start_charpos <= end_charpos);
6242 /* Is text at START invisible? */
6243 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6244 it->window);
6245 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6246 invisible_found_p = 1;
6247 else
6249 limit = Fnext_single_char_property_change (make_number (start_charpos),
6250 Qinvisible, Qnil,
6251 make_number (end_charpos));
6252 invisible_found_p = XFASTINT (limit) < end_charpos;
6255 return invisible_found_p;
6258 #endif /* 0 */
6261 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6262 negative means move up. DVPOS == 0 means move to the start of the
6263 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6264 NEED_Y_P is zero, IT->current_y will be left unchanged.
6266 Further optimization ideas: If we would know that IT->f doesn't use
6267 a face with proportional font, we could be faster for
6268 truncate-lines nil. */
6270 void
6271 move_it_by_lines (it, dvpos, need_y_p)
6272 struct it *it;
6273 int dvpos, need_y_p;
6275 struct position pos;
6277 if (!FRAME_WINDOW_P (it->f))
6279 struct text_pos textpos;
6281 /* We can use vmotion on frames without proportional fonts. */
6282 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6283 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6284 reseat (it, textpos, 1);
6285 it->vpos += pos.vpos;
6286 it->current_y += pos.vpos;
6288 else if (dvpos == 0)
6290 /* DVPOS == 0 means move to the start of the screen line. */
6291 move_it_vertically_backward (it, 0);
6292 xassert (it->current_x == 0 && it->hpos == 0);
6293 /* Let next call to line_bottom_y calculate real line height */
6294 last_height = 0;
6296 else if (dvpos > 0)
6297 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6298 else
6300 struct it it2;
6301 int start_charpos, i;
6303 /* Start at the beginning of the screen line containing IT's
6304 position. */
6305 move_it_vertically_backward (it, 0);
6307 /* Go back -DVPOS visible lines and reseat the iterator there. */
6308 start_charpos = IT_CHARPOS (*it);
6309 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6310 back_to_previous_visible_line_start (it);
6311 reseat (it, it->current.pos, 1);
6312 it->current_x = it->hpos = 0;
6314 /* Above call may have moved too far if continuation lines
6315 are involved. Scan forward and see if it did. */
6316 it2 = *it;
6317 it2.vpos = it2.current_y = 0;
6318 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6319 it->vpos -= it2.vpos;
6320 it->current_y -= it2.current_y;
6321 it->current_x = it->hpos = 0;
6323 /* If we moved too far, move IT some lines forward. */
6324 if (it2.vpos > -dvpos)
6326 int delta = it2.vpos + dvpos;
6327 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6332 /* Return 1 if IT points into the middle of a display vector. */
6335 in_display_vector_p (it)
6336 struct it *it;
6338 return (it->method == next_element_from_display_vector
6339 && it->current.dpvec_index > 0
6340 && it->dpvec + it->current.dpvec_index != it->dpend);
6344 /***********************************************************************
6345 Messages
6346 ***********************************************************************/
6349 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6350 to *Messages*. */
6352 void
6353 add_to_log (format, arg1, arg2)
6354 char *format;
6355 Lisp_Object arg1, arg2;
6357 Lisp_Object args[3];
6358 Lisp_Object msg, fmt;
6359 char *buffer;
6360 int len;
6361 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6362 USE_SAFE_ALLOCA;
6364 /* Do nothing if called asynchronously. Inserting text into
6365 a buffer may call after-change-functions and alike and
6366 that would means running Lisp asynchronously. */
6367 if (handling_signal)
6368 return;
6370 fmt = msg = Qnil;
6371 GCPRO4 (fmt, msg, arg1, arg2);
6373 args[0] = fmt = build_string (format);
6374 args[1] = arg1;
6375 args[2] = arg2;
6376 msg = Fformat (3, args);
6378 len = SBYTES (msg) + 1;
6379 SAFE_ALLOCA (buffer, char *, len);
6380 bcopy (SDATA (msg), buffer, len);
6382 message_dolog (buffer, len - 1, 1, 0);
6383 SAFE_FREE ();
6385 UNGCPRO;
6389 /* Output a newline in the *Messages* buffer if "needs" one. */
6391 void
6392 message_log_maybe_newline ()
6394 if (message_log_need_newline)
6395 message_dolog ("", 0, 1, 0);
6399 /* Add a string M of length NBYTES to the message log, optionally
6400 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6401 nonzero, means interpret the contents of M as multibyte. This
6402 function calls low-level routines in order to bypass text property
6403 hooks, etc. which might not be safe to run. */
6405 void
6406 message_dolog (m, nbytes, nlflag, multibyte)
6407 const char *m;
6408 int nbytes, nlflag, multibyte;
6410 if (!NILP (Vmemory_full))
6411 return;
6413 if (!NILP (Vmessage_log_max))
6415 struct buffer *oldbuf;
6416 Lisp_Object oldpoint, oldbegv, oldzv;
6417 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6418 int point_at_end = 0;
6419 int zv_at_end = 0;
6420 Lisp_Object old_deactivate_mark, tem;
6421 struct gcpro gcpro1;
6423 old_deactivate_mark = Vdeactivate_mark;
6424 oldbuf = current_buffer;
6425 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6426 current_buffer->undo_list = Qt;
6428 oldpoint = message_dolog_marker1;
6429 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6430 oldbegv = message_dolog_marker2;
6431 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6432 oldzv = message_dolog_marker3;
6433 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6434 GCPRO1 (old_deactivate_mark);
6436 if (PT == Z)
6437 point_at_end = 1;
6438 if (ZV == Z)
6439 zv_at_end = 1;
6441 BEGV = BEG;
6442 BEGV_BYTE = BEG_BYTE;
6443 ZV = Z;
6444 ZV_BYTE = Z_BYTE;
6445 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6447 /* Insert the string--maybe converting multibyte to single byte
6448 or vice versa, so that all the text fits the buffer. */
6449 if (multibyte
6450 && NILP (current_buffer->enable_multibyte_characters))
6452 int i, c, char_bytes;
6453 unsigned char work[1];
6455 /* Convert a multibyte string to single-byte
6456 for the *Message* buffer. */
6457 for (i = 0; i < nbytes; i += char_bytes)
6459 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6460 work[0] = (SINGLE_BYTE_CHAR_P (c)
6462 : multibyte_char_to_unibyte (c, Qnil));
6463 insert_1_both (work, 1, 1, 1, 0, 0);
6466 else if (! multibyte
6467 && ! NILP (current_buffer->enable_multibyte_characters))
6469 int i, c, char_bytes;
6470 unsigned char *msg = (unsigned char *) m;
6471 unsigned char str[MAX_MULTIBYTE_LENGTH];
6472 /* Convert a single-byte string to multibyte
6473 for the *Message* buffer. */
6474 for (i = 0; i < nbytes; i++)
6476 c = unibyte_char_to_multibyte (msg[i]);
6477 char_bytes = CHAR_STRING (c, str);
6478 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6481 else if (nbytes)
6482 insert_1 (m, nbytes, 1, 0, 0);
6484 if (nlflag)
6486 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6487 insert_1 ("\n", 1, 1, 0, 0);
6489 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6490 this_bol = PT;
6491 this_bol_byte = PT_BYTE;
6493 /* See if this line duplicates the previous one.
6494 If so, combine duplicates. */
6495 if (this_bol > BEG)
6497 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6498 prev_bol = PT;
6499 prev_bol_byte = PT_BYTE;
6501 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6502 this_bol, this_bol_byte);
6503 if (dup)
6505 del_range_both (prev_bol, prev_bol_byte,
6506 this_bol, this_bol_byte, 0);
6507 if (dup > 1)
6509 char dupstr[40];
6510 int duplen;
6512 /* If you change this format, don't forget to also
6513 change message_log_check_duplicate. */
6514 sprintf (dupstr, " [%d times]", dup);
6515 duplen = strlen (dupstr);
6516 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6517 insert_1 (dupstr, duplen, 1, 0, 1);
6522 /* If we have more than the desired maximum number of lines
6523 in the *Messages* buffer now, delete the oldest ones.
6524 This is safe because we don't have undo in this buffer. */
6526 if (NATNUMP (Vmessage_log_max))
6528 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6529 -XFASTINT (Vmessage_log_max) - 1, 0);
6530 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6533 BEGV = XMARKER (oldbegv)->charpos;
6534 BEGV_BYTE = marker_byte_position (oldbegv);
6536 if (zv_at_end)
6538 ZV = Z;
6539 ZV_BYTE = Z_BYTE;
6541 else
6543 ZV = XMARKER (oldzv)->charpos;
6544 ZV_BYTE = marker_byte_position (oldzv);
6547 if (point_at_end)
6548 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6549 else
6550 /* We can't do Fgoto_char (oldpoint) because it will run some
6551 Lisp code. */
6552 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6553 XMARKER (oldpoint)->bytepos);
6555 UNGCPRO;
6556 unchain_marker (XMARKER (oldpoint));
6557 unchain_marker (XMARKER (oldbegv));
6558 unchain_marker (XMARKER (oldzv));
6560 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6561 set_buffer_internal (oldbuf);
6562 if (NILP (tem))
6563 windows_or_buffers_changed = old_windows_or_buffers_changed;
6564 message_log_need_newline = !nlflag;
6565 Vdeactivate_mark = old_deactivate_mark;
6570 /* We are at the end of the buffer after just having inserted a newline.
6571 (Note: We depend on the fact we won't be crossing the gap.)
6572 Check to see if the most recent message looks a lot like the previous one.
6573 Return 0 if different, 1 if the new one should just replace it, or a
6574 value N > 1 if we should also append " [N times]". */
6576 static int
6577 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6578 int prev_bol, this_bol;
6579 int prev_bol_byte, this_bol_byte;
6581 int i;
6582 int len = Z_BYTE - 1 - this_bol_byte;
6583 int seen_dots = 0;
6584 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6585 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6587 for (i = 0; i < len; i++)
6589 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6590 seen_dots = 1;
6591 if (p1[i] != p2[i])
6592 return seen_dots;
6594 p1 += len;
6595 if (*p1 == '\n')
6596 return 2;
6597 if (*p1++ == ' ' && *p1++ == '[')
6599 int n = 0;
6600 while (*p1 >= '0' && *p1 <= '9')
6601 n = n * 10 + *p1++ - '0';
6602 if (strncmp (p1, " times]\n", 8) == 0)
6603 return n+1;
6605 return 0;
6609 /* Display an echo area message M with a specified length of NBYTES
6610 bytes. The string may include null characters. If M is 0, clear
6611 out any existing message, and let the mini-buffer text show
6612 through.
6614 The buffer M must continue to exist until after the echo area gets
6615 cleared or some other message gets displayed there. This means do
6616 not pass text that is stored in a Lisp string; do not pass text in
6617 a buffer that was alloca'd. */
6619 void
6620 message2 (m, nbytes, multibyte)
6621 const char *m;
6622 int nbytes;
6623 int multibyte;
6625 /* First flush out any partial line written with print. */
6626 message_log_maybe_newline ();
6627 if (m)
6628 message_dolog (m, nbytes, 1, multibyte);
6629 message2_nolog (m, nbytes, multibyte);
6633 /* The non-logging counterpart of message2. */
6635 void
6636 message2_nolog (m, nbytes, multibyte)
6637 const char *m;
6638 int nbytes, multibyte;
6640 struct frame *sf = SELECTED_FRAME ();
6641 message_enable_multibyte = multibyte;
6643 if (noninteractive)
6645 if (noninteractive_need_newline)
6646 putc ('\n', stderr);
6647 noninteractive_need_newline = 0;
6648 if (m)
6649 fwrite (m, nbytes, 1, stderr);
6650 if (cursor_in_echo_area == 0)
6651 fprintf (stderr, "\n");
6652 fflush (stderr);
6654 /* A null message buffer means that the frame hasn't really been
6655 initialized yet. Error messages get reported properly by
6656 cmd_error, so this must be just an informative message; toss it. */
6657 else if (INTERACTIVE
6658 && sf->glyphs_initialized_p
6659 && FRAME_MESSAGE_BUF (sf))
6661 Lisp_Object mini_window;
6662 struct frame *f;
6664 /* Get the frame containing the mini-buffer
6665 that the selected frame is using. */
6666 mini_window = FRAME_MINIBUF_WINDOW (sf);
6667 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6669 FRAME_SAMPLE_VISIBILITY (f);
6670 if (FRAME_VISIBLE_P (sf)
6671 && ! FRAME_VISIBLE_P (f))
6672 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6674 if (m)
6676 set_message (m, Qnil, nbytes, multibyte);
6677 if (minibuffer_auto_raise)
6678 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6680 else
6681 clear_message (1, 1);
6683 do_pending_window_change (0);
6684 echo_area_display (1);
6685 do_pending_window_change (0);
6686 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6687 (*frame_up_to_date_hook) (f);
6692 /* Display an echo area message M with a specified length of NBYTES
6693 bytes. The string may include null characters. If M is not a
6694 string, clear out any existing message, and let the mini-buffer
6695 text show through. */
6697 void
6698 message3 (m, nbytes, multibyte)
6699 Lisp_Object m;
6700 int nbytes;
6701 int multibyte;
6703 struct gcpro gcpro1;
6705 GCPRO1 (m);
6707 /* First flush out any partial line written with print. */
6708 message_log_maybe_newline ();
6709 if (STRINGP (m))
6710 message_dolog (SDATA (m), nbytes, 1, multibyte);
6711 message3_nolog (m, nbytes, multibyte);
6713 UNGCPRO;
6717 /* The non-logging version of message3. */
6719 void
6720 message3_nolog (m, nbytes, multibyte)
6721 Lisp_Object m;
6722 int nbytes, multibyte;
6724 struct frame *sf = SELECTED_FRAME ();
6725 message_enable_multibyte = multibyte;
6727 if (noninteractive)
6729 if (noninteractive_need_newline)
6730 putc ('\n', stderr);
6731 noninteractive_need_newline = 0;
6732 if (STRINGP (m))
6733 fwrite (SDATA (m), nbytes, 1, stderr);
6734 if (cursor_in_echo_area == 0)
6735 fprintf (stderr, "\n");
6736 fflush (stderr);
6738 /* A null message buffer means that the frame hasn't really been
6739 initialized yet. Error messages get reported properly by
6740 cmd_error, so this must be just an informative message; toss it. */
6741 else if (INTERACTIVE
6742 && sf->glyphs_initialized_p
6743 && FRAME_MESSAGE_BUF (sf))
6745 Lisp_Object mini_window;
6746 Lisp_Object frame;
6747 struct frame *f;
6749 /* Get the frame containing the mini-buffer
6750 that the selected frame is using. */
6751 mini_window = FRAME_MINIBUF_WINDOW (sf);
6752 frame = XWINDOW (mini_window)->frame;
6753 f = XFRAME (frame);
6755 FRAME_SAMPLE_VISIBILITY (f);
6756 if (FRAME_VISIBLE_P (sf)
6757 && !FRAME_VISIBLE_P (f))
6758 Fmake_frame_visible (frame);
6760 if (STRINGP (m) && SCHARS (m) > 0)
6762 set_message (NULL, m, nbytes, multibyte);
6763 if (minibuffer_auto_raise)
6764 Fraise_frame (frame);
6766 else
6767 clear_message (1, 1);
6769 do_pending_window_change (0);
6770 echo_area_display (1);
6771 do_pending_window_change (0);
6772 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6773 (*frame_up_to_date_hook) (f);
6778 /* Display a null-terminated echo area message M. If M is 0, clear
6779 out any existing message, and let the mini-buffer text show through.
6781 The buffer M must continue to exist until after the echo area gets
6782 cleared or some other message gets displayed there. Do not pass
6783 text that is stored in a Lisp string. Do not pass text in a buffer
6784 that was alloca'd. */
6786 void
6787 message1 (m)
6788 char *m;
6790 message2 (m, (m ? strlen (m) : 0), 0);
6794 /* The non-logging counterpart of message1. */
6796 void
6797 message1_nolog (m)
6798 char *m;
6800 message2_nolog (m, (m ? strlen (m) : 0), 0);
6803 /* Display a message M which contains a single %s
6804 which gets replaced with STRING. */
6806 void
6807 message_with_string (m, string, log)
6808 char *m;
6809 Lisp_Object string;
6810 int log;
6812 CHECK_STRING (string);
6814 if (noninteractive)
6816 if (m)
6818 if (noninteractive_need_newline)
6819 putc ('\n', stderr);
6820 noninteractive_need_newline = 0;
6821 fprintf (stderr, m, SDATA (string));
6822 if (cursor_in_echo_area == 0)
6823 fprintf (stderr, "\n");
6824 fflush (stderr);
6827 else if (INTERACTIVE)
6829 /* The frame whose minibuffer we're going to display the message on.
6830 It may be larger than the selected frame, so we need
6831 to use its buffer, not the selected frame's buffer. */
6832 Lisp_Object mini_window;
6833 struct frame *f, *sf = SELECTED_FRAME ();
6835 /* Get the frame containing the minibuffer
6836 that the selected frame is using. */
6837 mini_window = FRAME_MINIBUF_WINDOW (sf);
6838 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6840 /* A null message buffer means that the frame hasn't really been
6841 initialized yet. Error messages get reported properly by
6842 cmd_error, so this must be just an informative message; toss it. */
6843 if (FRAME_MESSAGE_BUF (f))
6845 Lisp_Object args[2], message;
6846 struct gcpro gcpro1, gcpro2;
6848 args[0] = build_string (m);
6849 args[1] = message = string;
6850 GCPRO2 (args[0], message);
6851 gcpro1.nvars = 2;
6853 message = Fformat (2, args);
6855 if (log)
6856 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6857 else
6858 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6860 UNGCPRO;
6862 /* Print should start at the beginning of the message
6863 buffer next time. */
6864 message_buf_print = 0;
6870 /* Dump an informative message to the minibuf. If M is 0, clear out
6871 any existing message, and let the mini-buffer text show through. */
6873 /* VARARGS 1 */
6874 void
6875 message (m, a1, a2, a3)
6876 char *m;
6877 EMACS_INT a1, a2, a3;
6879 if (noninteractive)
6881 if (m)
6883 if (noninteractive_need_newline)
6884 putc ('\n', stderr);
6885 noninteractive_need_newline = 0;
6886 fprintf (stderr, m, a1, a2, a3);
6887 if (cursor_in_echo_area == 0)
6888 fprintf (stderr, "\n");
6889 fflush (stderr);
6892 else if (INTERACTIVE)
6894 /* The frame whose mini-buffer we're going to display the message
6895 on. It may be larger than the selected frame, so we need to
6896 use its buffer, not the selected frame's buffer. */
6897 Lisp_Object mini_window;
6898 struct frame *f, *sf = SELECTED_FRAME ();
6900 /* Get the frame containing the mini-buffer
6901 that the selected frame is using. */
6902 mini_window = FRAME_MINIBUF_WINDOW (sf);
6903 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6905 /* A null message buffer means that the frame hasn't really been
6906 initialized yet. Error messages get reported properly by
6907 cmd_error, so this must be just an informative message; toss
6908 it. */
6909 if (FRAME_MESSAGE_BUF (f))
6911 if (m)
6913 int len;
6914 #ifdef NO_ARG_ARRAY
6915 char *a[3];
6916 a[0] = (char *) a1;
6917 a[1] = (char *) a2;
6918 a[2] = (char *) a3;
6920 len = doprnt (FRAME_MESSAGE_BUF (f),
6921 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6922 #else
6923 len = doprnt (FRAME_MESSAGE_BUF (f),
6924 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6925 (char **) &a1);
6926 #endif /* NO_ARG_ARRAY */
6928 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6930 else
6931 message1 (0);
6933 /* Print should start at the beginning of the message
6934 buffer next time. */
6935 message_buf_print = 0;
6941 /* The non-logging version of message. */
6943 void
6944 message_nolog (m, a1, a2, a3)
6945 char *m;
6946 EMACS_INT a1, a2, a3;
6948 Lisp_Object old_log_max;
6949 old_log_max = Vmessage_log_max;
6950 Vmessage_log_max = Qnil;
6951 message (m, a1, a2, a3);
6952 Vmessage_log_max = old_log_max;
6956 /* Display the current message in the current mini-buffer. This is
6957 only called from error handlers in process.c, and is not time
6958 critical. */
6960 void
6961 update_echo_area ()
6963 if (!NILP (echo_area_buffer[0]))
6965 Lisp_Object string;
6966 string = Fcurrent_message ();
6967 message3 (string, SBYTES (string),
6968 !NILP (current_buffer->enable_multibyte_characters));
6973 /* Make sure echo area buffers in `echo_buffers' are live.
6974 If they aren't, make new ones. */
6976 static void
6977 ensure_echo_area_buffers ()
6979 int i;
6981 for (i = 0; i < 2; ++i)
6982 if (!BUFFERP (echo_buffer[i])
6983 || NILP (XBUFFER (echo_buffer[i])->name))
6985 char name[30];
6986 Lisp_Object old_buffer;
6987 int j;
6989 old_buffer = echo_buffer[i];
6990 sprintf (name, " *Echo Area %d*", i);
6991 echo_buffer[i] = Fget_buffer_create (build_string (name));
6992 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6994 for (j = 0; j < 2; ++j)
6995 if (EQ (old_buffer, echo_area_buffer[j]))
6996 echo_area_buffer[j] = echo_buffer[i];
7001 /* Call FN with args A1..A4 with either the current or last displayed
7002 echo_area_buffer as current buffer.
7004 WHICH zero means use the current message buffer
7005 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7006 from echo_buffer[] and clear it.
7008 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7009 suitable buffer from echo_buffer[] and clear it.
7011 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7012 that the current message becomes the last displayed one, make
7013 choose a suitable buffer for echo_area_buffer[0], and clear it.
7015 Value is what FN returns. */
7017 static int
7018 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7019 struct window *w;
7020 int which;
7021 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7022 EMACS_INT a1;
7023 Lisp_Object a2;
7024 EMACS_INT a3, a4;
7026 Lisp_Object buffer;
7027 int this_one, the_other, clear_buffer_p, rc;
7028 int count = SPECPDL_INDEX ();
7030 /* If buffers aren't live, make new ones. */
7031 ensure_echo_area_buffers ();
7033 clear_buffer_p = 0;
7035 if (which == 0)
7036 this_one = 0, the_other = 1;
7037 else if (which > 0)
7038 this_one = 1, the_other = 0;
7039 else
7041 this_one = 0, the_other = 1;
7042 clear_buffer_p = 1;
7044 /* We need a fresh one in case the current echo buffer equals
7045 the one containing the last displayed echo area message. */
7046 if (!NILP (echo_area_buffer[this_one])
7047 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7048 echo_area_buffer[this_one] = Qnil;
7051 /* Choose a suitable buffer from echo_buffer[] is we don't
7052 have one. */
7053 if (NILP (echo_area_buffer[this_one]))
7055 echo_area_buffer[this_one]
7056 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7057 ? echo_buffer[the_other]
7058 : echo_buffer[this_one]);
7059 clear_buffer_p = 1;
7062 buffer = echo_area_buffer[this_one];
7064 /* Don't get confused by reusing the buffer used for echoing
7065 for a different purpose. */
7066 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7067 cancel_echoing ();
7069 record_unwind_protect (unwind_with_echo_area_buffer,
7070 with_echo_area_buffer_unwind_data (w));
7072 /* Make the echo area buffer current. Note that for display
7073 purposes, it is not necessary that the displayed window's buffer
7074 == current_buffer, except for text property lookup. So, let's
7075 only set that buffer temporarily here without doing a full
7076 Fset_window_buffer. We must also change w->pointm, though,
7077 because otherwise an assertions in unshow_buffer fails, and Emacs
7078 aborts. */
7079 set_buffer_internal_1 (XBUFFER (buffer));
7080 if (w)
7082 w->buffer = buffer;
7083 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7086 current_buffer->undo_list = Qt;
7087 current_buffer->read_only = Qnil;
7088 specbind (Qinhibit_read_only, Qt);
7089 specbind (Qinhibit_modification_hooks, Qt);
7091 if (clear_buffer_p && Z > BEG)
7092 del_range (BEG, Z);
7094 xassert (BEGV >= BEG);
7095 xassert (ZV <= Z && ZV >= BEGV);
7097 rc = fn (a1, a2, a3, a4);
7099 xassert (BEGV >= BEG);
7100 xassert (ZV <= Z && ZV >= BEGV);
7102 unbind_to (count, Qnil);
7103 return rc;
7107 /* Save state that should be preserved around the call to the function
7108 FN called in with_echo_area_buffer. */
7110 static Lisp_Object
7111 with_echo_area_buffer_unwind_data (w)
7112 struct window *w;
7114 int i = 0;
7115 Lisp_Object vector;
7117 /* Reduce consing by keeping one vector in
7118 Vwith_echo_area_save_vector. */
7119 vector = Vwith_echo_area_save_vector;
7120 Vwith_echo_area_save_vector = Qnil;
7122 if (NILP (vector))
7123 vector = Fmake_vector (make_number (7), Qnil);
7125 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7126 AREF (vector, i) = Vdeactivate_mark, ++i;
7127 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7129 if (w)
7131 XSETWINDOW (AREF (vector, i), w); ++i;
7132 AREF (vector, i) = w->buffer; ++i;
7133 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7134 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7136 else
7138 int end = i + 4;
7139 for (; i < end; ++i)
7140 AREF (vector, i) = Qnil;
7143 xassert (i == ASIZE (vector));
7144 return vector;
7148 /* Restore global state from VECTOR which was created by
7149 with_echo_area_buffer_unwind_data. */
7151 static Lisp_Object
7152 unwind_with_echo_area_buffer (vector)
7153 Lisp_Object vector;
7155 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7156 Vdeactivate_mark = AREF (vector, 1);
7157 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7159 if (WINDOWP (AREF (vector, 3)))
7161 struct window *w;
7162 Lisp_Object buffer, charpos, bytepos;
7164 w = XWINDOW (AREF (vector, 3));
7165 buffer = AREF (vector, 4);
7166 charpos = AREF (vector, 5);
7167 bytepos = AREF (vector, 6);
7169 w->buffer = buffer;
7170 set_marker_both (w->pointm, buffer,
7171 XFASTINT (charpos), XFASTINT (bytepos));
7174 Vwith_echo_area_save_vector = vector;
7175 return Qnil;
7179 /* Set up the echo area for use by print functions. MULTIBYTE_P
7180 non-zero means we will print multibyte. */
7182 void
7183 setup_echo_area_for_printing (multibyte_p)
7184 int multibyte_p;
7186 /* If we can't find an echo area any more, exit. */
7187 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7188 Fkill_emacs (Qnil);
7190 ensure_echo_area_buffers ();
7192 if (!message_buf_print)
7194 /* A message has been output since the last time we printed.
7195 Choose a fresh echo area buffer. */
7196 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7197 echo_area_buffer[0] = echo_buffer[1];
7198 else
7199 echo_area_buffer[0] = echo_buffer[0];
7201 /* Switch to that buffer and clear it. */
7202 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7203 current_buffer->truncate_lines = Qnil;
7205 if (Z > BEG)
7207 int count = SPECPDL_INDEX ();
7208 specbind (Qinhibit_read_only, Qt);
7209 /* Note that undo recording is always disabled. */
7210 del_range (BEG, Z);
7211 unbind_to (count, Qnil);
7213 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7215 /* Set up the buffer for the multibyteness we need. */
7216 if (multibyte_p
7217 != !NILP (current_buffer->enable_multibyte_characters))
7218 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7220 /* Raise the frame containing the echo area. */
7221 if (minibuffer_auto_raise)
7223 struct frame *sf = SELECTED_FRAME ();
7224 Lisp_Object mini_window;
7225 mini_window = FRAME_MINIBUF_WINDOW (sf);
7226 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7229 message_log_maybe_newline ();
7230 message_buf_print = 1;
7232 else
7234 if (NILP (echo_area_buffer[0]))
7236 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7237 echo_area_buffer[0] = echo_buffer[1];
7238 else
7239 echo_area_buffer[0] = echo_buffer[0];
7242 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7244 /* Someone switched buffers between print requests. */
7245 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7246 current_buffer->truncate_lines = Qnil;
7252 /* Display an echo area message in window W. Value is non-zero if W's
7253 height is changed. If display_last_displayed_message_p is
7254 non-zero, display the message that was last displayed, otherwise
7255 display the current message. */
7257 static int
7258 display_echo_area (w)
7259 struct window *w;
7261 int i, no_message_p, window_height_changed_p, count;
7263 /* Temporarily disable garbage collections while displaying the echo
7264 area. This is done because a GC can print a message itself.
7265 That message would modify the echo area buffer's contents while a
7266 redisplay of the buffer is going on, and seriously confuse
7267 redisplay. */
7268 count = inhibit_garbage_collection ();
7270 /* If there is no message, we must call display_echo_area_1
7271 nevertheless because it resizes the window. But we will have to
7272 reset the echo_area_buffer in question to nil at the end because
7273 with_echo_area_buffer will sets it to an empty buffer. */
7274 i = display_last_displayed_message_p ? 1 : 0;
7275 no_message_p = NILP (echo_area_buffer[i]);
7277 window_height_changed_p
7278 = with_echo_area_buffer (w, display_last_displayed_message_p,
7279 display_echo_area_1,
7280 (EMACS_INT) w, Qnil, 0, 0);
7282 if (no_message_p)
7283 echo_area_buffer[i] = Qnil;
7285 unbind_to (count, Qnil);
7286 return window_height_changed_p;
7290 /* Helper for display_echo_area. Display the current buffer which
7291 contains the current echo area message in window W, a mini-window,
7292 a pointer to which is passed in A1. A2..A4 are currently not used.
7293 Change the height of W so that all of the message is displayed.
7294 Value is non-zero if height of W was changed. */
7296 static int
7297 display_echo_area_1 (a1, a2, a3, a4)
7298 EMACS_INT a1;
7299 Lisp_Object a2;
7300 EMACS_INT a3, a4;
7302 struct window *w = (struct window *) a1;
7303 Lisp_Object window;
7304 struct text_pos start;
7305 int window_height_changed_p = 0;
7307 /* Do this before displaying, so that we have a large enough glyph
7308 matrix for the display. */
7309 window_height_changed_p = resize_mini_window (w, 0);
7311 /* Display. */
7312 clear_glyph_matrix (w->desired_matrix);
7313 XSETWINDOW (window, w);
7314 SET_TEXT_POS (start, BEG, BEG_BYTE);
7315 try_window (window, start);
7317 return window_height_changed_p;
7321 /* Resize the echo area window to exactly the size needed for the
7322 currently displayed message, if there is one. If a mini-buffer
7323 is active, don't shrink it. */
7325 void
7326 resize_echo_area_exactly ()
7328 if (BUFFERP (echo_area_buffer[0])
7329 && WINDOWP (echo_area_window))
7331 struct window *w = XWINDOW (echo_area_window);
7332 int resized_p;
7333 Lisp_Object resize_exactly;
7335 if (minibuf_level == 0)
7336 resize_exactly = Qt;
7337 else
7338 resize_exactly = Qnil;
7340 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7341 (EMACS_INT) w, resize_exactly, 0, 0);
7342 if (resized_p)
7344 ++windows_or_buffers_changed;
7345 ++update_mode_lines;
7346 redisplay_internal (0);
7352 /* Callback function for with_echo_area_buffer, when used from
7353 resize_echo_area_exactly. A1 contains a pointer to the window to
7354 resize, EXACTLY non-nil means resize the mini-window exactly to the
7355 size of the text displayed. A3 and A4 are not used. Value is what
7356 resize_mini_window returns. */
7358 static int
7359 resize_mini_window_1 (a1, exactly, a3, a4)
7360 EMACS_INT a1;
7361 Lisp_Object exactly;
7362 EMACS_INT a3, a4;
7364 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7368 /* Resize mini-window W to fit the size of its contents. EXACT:P
7369 means size the window exactly to the size needed. Otherwise, it's
7370 only enlarged until W's buffer is empty. Value is non-zero if
7371 the window height has been changed. */
7374 resize_mini_window (w, exact_p)
7375 struct window *w;
7376 int exact_p;
7378 struct frame *f = XFRAME (w->frame);
7379 int window_height_changed_p = 0;
7381 xassert (MINI_WINDOW_P (w));
7383 /* Don't resize windows while redisplaying a window; it would
7384 confuse redisplay functions when the size of the window they are
7385 displaying changes from under them. Such a resizing can happen,
7386 for instance, when which-func prints a long message while
7387 we are running fontification-functions. We're running these
7388 functions with safe_call which binds inhibit-redisplay to t. */
7389 if (!NILP (Vinhibit_redisplay))
7390 return 0;
7392 /* Nil means don't try to resize. */
7393 if (NILP (Vresize_mini_windows)
7394 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7395 return 0;
7397 if (!FRAME_MINIBUF_ONLY_P (f))
7399 struct it it;
7400 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7401 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7402 int height, max_height;
7403 int unit = FRAME_LINE_HEIGHT (f);
7404 struct text_pos start;
7405 struct buffer *old_current_buffer = NULL;
7407 if (current_buffer != XBUFFER (w->buffer))
7409 old_current_buffer = current_buffer;
7410 set_buffer_internal (XBUFFER (w->buffer));
7413 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7415 /* Compute the max. number of lines specified by the user. */
7416 if (FLOATP (Vmax_mini_window_height))
7417 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7418 else if (INTEGERP (Vmax_mini_window_height))
7419 max_height = XINT (Vmax_mini_window_height);
7420 else
7421 max_height = total_height / 4;
7423 /* Correct that max. height if it's bogus. */
7424 max_height = max (1, max_height);
7425 max_height = min (total_height, max_height);
7427 /* Find out the height of the text in the window. */
7428 if (it.truncate_lines_p)
7429 height = 1;
7430 else
7432 last_height = 0;
7433 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7434 if (it.max_ascent == 0 && it.max_descent == 0)
7435 height = it.current_y + last_height;
7436 else
7437 height = it.current_y + it.max_ascent + it.max_descent;
7438 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7439 height = (height + unit - 1) / unit;
7442 /* Compute a suitable window start. */
7443 if (height > max_height)
7445 height = max_height;
7446 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7447 move_it_vertically_backward (&it, (height - 1) * unit);
7448 start = it.current.pos;
7450 else
7451 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7452 SET_MARKER_FROM_TEXT_POS (w->start, start);
7454 if (EQ (Vresize_mini_windows, Qgrow_only))
7456 /* Let it grow only, until we display an empty message, in which
7457 case the window shrinks again. */
7458 if (height > WINDOW_TOTAL_LINES (w))
7460 int old_height = WINDOW_TOTAL_LINES (w);
7461 freeze_window_starts (f, 1);
7462 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7463 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7465 else if (height < WINDOW_TOTAL_LINES (w)
7466 && (exact_p || BEGV == ZV))
7468 int old_height = WINDOW_TOTAL_LINES (w);
7469 freeze_window_starts (f, 0);
7470 shrink_mini_window (w);
7471 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7474 else
7476 /* Always resize to exact size needed. */
7477 if (height > WINDOW_TOTAL_LINES (w))
7479 int old_height = WINDOW_TOTAL_LINES (w);
7480 freeze_window_starts (f, 1);
7481 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7482 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7484 else if (height < WINDOW_TOTAL_LINES (w))
7486 int old_height = WINDOW_TOTAL_LINES (w);
7487 freeze_window_starts (f, 0);
7488 shrink_mini_window (w);
7490 if (height)
7492 freeze_window_starts (f, 1);
7493 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7496 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7500 if (old_current_buffer)
7501 set_buffer_internal (old_current_buffer);
7504 return window_height_changed_p;
7508 /* Value is the current message, a string, or nil if there is no
7509 current message. */
7511 Lisp_Object
7512 current_message ()
7514 Lisp_Object msg;
7516 if (NILP (echo_area_buffer[0]))
7517 msg = Qnil;
7518 else
7520 with_echo_area_buffer (0, 0, current_message_1,
7521 (EMACS_INT) &msg, Qnil, 0, 0);
7522 if (NILP (msg))
7523 echo_area_buffer[0] = Qnil;
7526 return msg;
7530 static int
7531 current_message_1 (a1, a2, a3, a4)
7532 EMACS_INT a1;
7533 Lisp_Object a2;
7534 EMACS_INT a3, a4;
7536 Lisp_Object *msg = (Lisp_Object *) a1;
7538 if (Z > BEG)
7539 *msg = make_buffer_string (BEG, Z, 1);
7540 else
7541 *msg = Qnil;
7542 return 0;
7546 /* Push the current message on Vmessage_stack for later restauration
7547 by restore_message. Value is non-zero if the current message isn't
7548 empty. This is a relatively infrequent operation, so it's not
7549 worth optimizing. */
7552 push_message ()
7554 Lisp_Object msg;
7555 msg = current_message ();
7556 Vmessage_stack = Fcons (msg, Vmessage_stack);
7557 return STRINGP (msg);
7561 /* Restore message display from the top of Vmessage_stack. */
7563 void
7564 restore_message ()
7566 Lisp_Object msg;
7568 xassert (CONSP (Vmessage_stack));
7569 msg = XCAR (Vmessage_stack);
7570 if (STRINGP (msg))
7571 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7572 else
7573 message3_nolog (msg, 0, 0);
7577 /* Handler for record_unwind_protect calling pop_message. */
7579 Lisp_Object
7580 pop_message_unwind (dummy)
7581 Lisp_Object dummy;
7583 pop_message ();
7584 return Qnil;
7587 /* Pop the top-most entry off Vmessage_stack. */
7589 void
7590 pop_message ()
7592 xassert (CONSP (Vmessage_stack));
7593 Vmessage_stack = XCDR (Vmessage_stack);
7597 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7598 exits. If the stack is not empty, we have a missing pop_message
7599 somewhere. */
7601 void
7602 check_message_stack ()
7604 if (!NILP (Vmessage_stack))
7605 abort ();
7609 /* Truncate to NCHARS what will be displayed in the echo area the next
7610 time we display it---but don't redisplay it now. */
7612 void
7613 truncate_echo_area (nchars)
7614 int nchars;
7616 if (nchars == 0)
7617 echo_area_buffer[0] = Qnil;
7618 /* A null message buffer means that the frame hasn't really been
7619 initialized yet. Error messages get reported properly by
7620 cmd_error, so this must be just an informative message; toss it. */
7621 else if (!noninteractive
7622 && INTERACTIVE
7623 && !NILP (echo_area_buffer[0]))
7625 struct frame *sf = SELECTED_FRAME ();
7626 if (FRAME_MESSAGE_BUF (sf))
7627 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7632 /* Helper function for truncate_echo_area. Truncate the current
7633 message to at most NCHARS characters. */
7635 static int
7636 truncate_message_1 (nchars, a2, a3, a4)
7637 EMACS_INT nchars;
7638 Lisp_Object a2;
7639 EMACS_INT a3, a4;
7641 if (BEG + nchars < Z)
7642 del_range (BEG + nchars, Z);
7643 if (Z == BEG)
7644 echo_area_buffer[0] = Qnil;
7645 return 0;
7649 /* Set the current message to a substring of S or STRING.
7651 If STRING is a Lisp string, set the message to the first NBYTES
7652 bytes from STRING. NBYTES zero means use the whole string. If
7653 STRING is multibyte, the message will be displayed multibyte.
7655 If S is not null, set the message to the first LEN bytes of S. LEN
7656 zero means use the whole string. MULTIBYTE_P non-zero means S is
7657 multibyte. Display the message multibyte in that case. */
7659 void
7660 set_message (s, string, nbytes, multibyte_p)
7661 const char *s;
7662 Lisp_Object string;
7663 int nbytes, multibyte_p;
7665 message_enable_multibyte
7666 = ((s && multibyte_p)
7667 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7669 with_echo_area_buffer (0, -1, set_message_1,
7670 (EMACS_INT) s, string, nbytes, multibyte_p);
7671 message_buf_print = 0;
7672 help_echo_showing_p = 0;
7676 /* Helper function for set_message. Arguments have the same meaning
7677 as there, with A1 corresponding to S and A2 corresponding to STRING
7678 This function is called with the echo area buffer being
7679 current. */
7681 static int
7682 set_message_1 (a1, a2, nbytes, multibyte_p)
7683 EMACS_INT a1;
7684 Lisp_Object a2;
7685 EMACS_INT nbytes, multibyte_p;
7687 const char *s = (const char *) a1;
7688 Lisp_Object string = a2;
7690 xassert (BEG == Z);
7692 /* Change multibyteness of the echo buffer appropriately. */
7693 if (message_enable_multibyte
7694 != !NILP (current_buffer->enable_multibyte_characters))
7695 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7697 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7699 /* Insert new message at BEG. */
7700 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7702 if (STRINGP (string))
7704 int nchars;
7706 if (nbytes == 0)
7707 nbytes = SBYTES (string);
7708 nchars = string_byte_to_char (string, nbytes);
7710 /* This function takes care of single/multibyte conversion. We
7711 just have to ensure that the echo area buffer has the right
7712 setting of enable_multibyte_characters. */
7713 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7715 else if (s)
7717 if (nbytes == 0)
7718 nbytes = strlen (s);
7720 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7722 /* Convert from multi-byte to single-byte. */
7723 int i, c, n;
7724 unsigned char work[1];
7726 /* Convert a multibyte string to single-byte. */
7727 for (i = 0; i < nbytes; i += n)
7729 c = string_char_and_length (s + i, nbytes - i, &n);
7730 work[0] = (SINGLE_BYTE_CHAR_P (c)
7732 : multibyte_char_to_unibyte (c, Qnil));
7733 insert_1_both (work, 1, 1, 1, 0, 0);
7736 else if (!multibyte_p
7737 && !NILP (current_buffer->enable_multibyte_characters))
7739 /* Convert from single-byte to multi-byte. */
7740 int i, c, n;
7741 const unsigned char *msg = (const unsigned char *) s;
7742 unsigned char str[MAX_MULTIBYTE_LENGTH];
7744 /* Convert a single-byte string to multibyte. */
7745 for (i = 0; i < nbytes; i++)
7747 c = unibyte_char_to_multibyte (msg[i]);
7748 n = CHAR_STRING (c, str);
7749 insert_1_both (str, 1, n, 1, 0, 0);
7752 else
7753 insert_1 (s, nbytes, 1, 0, 0);
7756 return 0;
7760 /* Clear messages. CURRENT_P non-zero means clear the current
7761 message. LAST_DISPLAYED_P non-zero means clear the message
7762 last displayed. */
7764 void
7765 clear_message (current_p, last_displayed_p)
7766 int current_p, last_displayed_p;
7768 if (current_p)
7770 echo_area_buffer[0] = Qnil;
7771 message_cleared_p = 1;
7774 if (last_displayed_p)
7775 echo_area_buffer[1] = Qnil;
7777 message_buf_print = 0;
7780 /* Clear garbaged frames.
7782 This function is used where the old redisplay called
7783 redraw_garbaged_frames which in turn called redraw_frame which in
7784 turn called clear_frame. The call to clear_frame was a source of
7785 flickering. I believe a clear_frame is not necessary. It should
7786 suffice in the new redisplay to invalidate all current matrices,
7787 and ensure a complete redisplay of all windows. */
7789 static void
7790 clear_garbaged_frames ()
7792 if (frame_garbaged)
7794 Lisp_Object tail, frame;
7795 int changed_count = 0;
7797 FOR_EACH_FRAME (tail, frame)
7799 struct frame *f = XFRAME (frame);
7801 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7803 if (f->resized_p)
7805 Fredraw_frame (frame);
7806 f->force_flush_display_p = 1;
7808 clear_current_matrices (f);
7809 changed_count++;
7810 f->garbaged = 0;
7811 f->resized_p = 0;
7815 frame_garbaged = 0;
7816 if (changed_count)
7817 ++windows_or_buffers_changed;
7822 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7823 is non-zero update selected_frame. Value is non-zero if the
7824 mini-windows height has been changed. */
7826 static int
7827 echo_area_display (update_frame_p)
7828 int update_frame_p;
7830 Lisp_Object mini_window;
7831 struct window *w;
7832 struct frame *f;
7833 int window_height_changed_p = 0;
7834 struct frame *sf = SELECTED_FRAME ();
7836 mini_window = FRAME_MINIBUF_WINDOW (sf);
7837 w = XWINDOW (mini_window);
7838 f = XFRAME (WINDOW_FRAME (w));
7840 /* Don't display if frame is invisible or not yet initialized. */
7841 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7842 return 0;
7844 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7845 #ifndef MAC_OS8
7846 #ifdef HAVE_WINDOW_SYSTEM
7847 /* When Emacs starts, selected_frame may be a visible terminal
7848 frame, even if we run under a window system. If we let this
7849 through, a message would be displayed on the terminal. */
7850 if (EQ (selected_frame, Vterminal_frame)
7851 && !NILP (Vwindow_system))
7852 return 0;
7853 #endif /* HAVE_WINDOW_SYSTEM */
7854 #endif
7856 /* Redraw garbaged frames. */
7857 if (frame_garbaged)
7858 clear_garbaged_frames ();
7860 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7862 echo_area_window = mini_window;
7863 window_height_changed_p = display_echo_area (w);
7864 w->must_be_updated_p = 1;
7866 /* Update the display, unless called from redisplay_internal.
7867 Also don't update the screen during redisplay itself. The
7868 update will happen at the end of redisplay, and an update
7869 here could cause confusion. */
7870 if (update_frame_p && !redisplaying_p)
7872 int n = 0;
7874 /* If the display update has been interrupted by pending
7875 input, update mode lines in the frame. Due to the
7876 pending input, it might have been that redisplay hasn't
7877 been called, so that mode lines above the echo area are
7878 garbaged. This looks odd, so we prevent it here. */
7879 if (!display_completed)
7880 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7882 if (window_height_changed_p
7883 /* Don't do this if Emacs is shutting down. Redisplay
7884 needs to run hooks. */
7885 && !NILP (Vrun_hooks))
7887 /* Must update other windows. Likewise as in other
7888 cases, don't let this update be interrupted by
7889 pending input. */
7890 int count = SPECPDL_INDEX ();
7891 specbind (Qredisplay_dont_pause, Qt);
7892 windows_or_buffers_changed = 1;
7893 redisplay_internal (0);
7894 unbind_to (count, Qnil);
7896 else if (FRAME_WINDOW_P (f) && n == 0)
7898 /* Window configuration is the same as before.
7899 Can do with a display update of the echo area,
7900 unless we displayed some mode lines. */
7901 update_single_window (w, 1);
7902 rif->flush_display (f);
7904 else
7905 update_frame (f, 1, 1);
7907 /* If cursor is in the echo area, make sure that the next
7908 redisplay displays the minibuffer, so that the cursor will
7909 be replaced with what the minibuffer wants. */
7910 if (cursor_in_echo_area)
7911 ++windows_or_buffers_changed;
7914 else if (!EQ (mini_window, selected_window))
7915 windows_or_buffers_changed++;
7917 /* Last displayed message is now the current message. */
7918 echo_area_buffer[1] = echo_area_buffer[0];
7920 /* Prevent redisplay optimization in redisplay_internal by resetting
7921 this_line_start_pos. This is done because the mini-buffer now
7922 displays the message instead of its buffer text. */
7923 if (EQ (mini_window, selected_window))
7924 CHARPOS (this_line_start_pos) = 0;
7926 return window_height_changed_p;
7931 /***********************************************************************
7932 Frame Titles
7933 ***********************************************************************/
7936 /* The frame title buffering code is also used by Fformat_mode_line.
7937 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7939 /* A buffer for constructing frame titles in it; allocated from the
7940 heap in init_xdisp and resized as needed in store_frame_title_char. */
7942 static char *frame_title_buf;
7944 /* The buffer's end, and a current output position in it. */
7946 static char *frame_title_buf_end;
7947 static char *frame_title_ptr;
7950 /* Store a single character C for the frame title in frame_title_buf.
7951 Re-allocate frame_title_buf if necessary. */
7953 static void
7954 #ifdef PROTOTYPES
7955 store_frame_title_char (char c)
7956 #else
7957 store_frame_title_char (c)
7958 char c;
7959 #endif
7961 /* If output position has reached the end of the allocated buffer,
7962 double the buffer's size. */
7963 if (frame_title_ptr == frame_title_buf_end)
7965 int len = frame_title_ptr - frame_title_buf;
7966 int new_size = 2 * len * sizeof *frame_title_buf;
7967 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7968 frame_title_buf_end = frame_title_buf + new_size;
7969 frame_title_ptr = frame_title_buf + len;
7972 *frame_title_ptr++ = c;
7976 /* Store part of a frame title in frame_title_buf, beginning at
7977 frame_title_ptr. STR is the string to store. Do not copy
7978 characters that yield more columns than PRECISION; PRECISION <= 0
7979 means copy the whole string. Pad with spaces until FIELD_WIDTH
7980 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7981 pad. Called from display_mode_element when it is used to build a
7982 frame title. */
7984 static int
7985 store_frame_title (str, field_width, precision)
7986 const unsigned char *str;
7987 int field_width, precision;
7989 int n = 0;
7990 int dummy, nbytes;
7992 /* Copy at most PRECISION chars from STR. */
7993 nbytes = strlen (str);
7994 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
7995 while (nbytes--)
7996 store_frame_title_char (*str++);
7998 /* Fill up with spaces until FIELD_WIDTH reached. */
7999 while (field_width > 0
8000 && n < field_width)
8002 store_frame_title_char (' ');
8003 ++n;
8006 return n;
8009 #ifdef HAVE_WINDOW_SYSTEM
8011 /* Set the title of FRAME, if it has changed. The title format is
8012 Vicon_title_format if FRAME is iconified, otherwise it is
8013 frame_title_format. */
8015 static void
8016 x_consider_frame_title (frame)
8017 Lisp_Object frame;
8019 struct frame *f = XFRAME (frame);
8021 if (FRAME_WINDOW_P (f)
8022 || FRAME_MINIBUF_ONLY_P (f)
8023 || f->explicit_name)
8025 /* Do we have more than one visible frame on this X display? */
8026 Lisp_Object tail;
8027 Lisp_Object fmt;
8028 struct buffer *obuf;
8029 int len;
8030 struct it it;
8032 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8034 Lisp_Object other_frame = XCAR (tail);
8035 struct frame *tf = XFRAME (other_frame);
8037 if (tf != f
8038 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8039 && !FRAME_MINIBUF_ONLY_P (tf)
8040 && !EQ (other_frame, tip_frame)
8041 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8042 break;
8045 /* Set global variable indicating that multiple frames exist. */
8046 multiple_frames = CONSP (tail);
8048 /* Switch to the buffer of selected window of the frame. Set up
8049 frame_title_ptr so that display_mode_element will output into it;
8050 then display the title. */
8051 obuf = current_buffer;
8052 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8053 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8054 frame_title_ptr = frame_title_buf;
8055 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8056 NULL, DEFAULT_FACE_ID);
8057 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8058 len = frame_title_ptr - frame_title_buf;
8059 frame_title_ptr = NULL;
8060 set_buffer_internal_1 (obuf);
8062 /* Set the title only if it's changed. This avoids consing in
8063 the common case where it hasn't. (If it turns out that we've
8064 already wasted too much time by walking through the list with
8065 display_mode_element, then we might need to optimize at a
8066 higher level than this.) */
8067 if (! STRINGP (f->name)
8068 || SBYTES (f->name) != len
8069 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8070 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8074 #endif /* not HAVE_WINDOW_SYSTEM */
8079 /***********************************************************************
8080 Menu Bars
8081 ***********************************************************************/
8084 /* Prepare for redisplay by updating menu-bar item lists when
8085 appropriate. This can call eval. */
8087 void
8088 prepare_menu_bars ()
8090 int all_windows;
8091 struct gcpro gcpro1, gcpro2;
8092 struct frame *f;
8093 Lisp_Object tooltip_frame;
8095 #ifdef HAVE_WINDOW_SYSTEM
8096 tooltip_frame = tip_frame;
8097 #else
8098 tooltip_frame = Qnil;
8099 #endif
8101 /* Update all frame titles based on their buffer names, etc. We do
8102 this before the menu bars so that the buffer-menu will show the
8103 up-to-date frame titles. */
8104 #ifdef HAVE_WINDOW_SYSTEM
8105 if (windows_or_buffers_changed || update_mode_lines)
8107 Lisp_Object tail, frame;
8109 FOR_EACH_FRAME (tail, frame)
8111 f = XFRAME (frame);
8112 if (!EQ (frame, tooltip_frame)
8113 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8114 x_consider_frame_title (frame);
8117 #endif /* HAVE_WINDOW_SYSTEM */
8119 /* Update the menu bar item lists, if appropriate. This has to be
8120 done before any actual redisplay or generation of display lines. */
8121 all_windows = (update_mode_lines
8122 || buffer_shared > 1
8123 || windows_or_buffers_changed);
8124 if (all_windows)
8126 Lisp_Object tail, frame;
8127 int count = SPECPDL_INDEX ();
8129 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8131 FOR_EACH_FRAME (tail, frame)
8133 f = XFRAME (frame);
8135 /* Ignore tooltip frame. */
8136 if (EQ (frame, tooltip_frame))
8137 continue;
8139 /* If a window on this frame changed size, report that to
8140 the user and clear the size-change flag. */
8141 if (FRAME_WINDOW_SIZES_CHANGED (f))
8143 Lisp_Object functions;
8145 /* Clear flag first in case we get an error below. */
8146 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8147 functions = Vwindow_size_change_functions;
8148 GCPRO2 (tail, functions);
8150 while (CONSP (functions))
8152 call1 (XCAR (functions), frame);
8153 functions = XCDR (functions);
8155 UNGCPRO;
8158 GCPRO1 (tail);
8159 update_menu_bar (f, 0);
8160 #ifdef HAVE_WINDOW_SYSTEM
8161 update_tool_bar (f, 0);
8162 #endif
8163 UNGCPRO;
8166 unbind_to (count, Qnil);
8168 else
8170 struct frame *sf = SELECTED_FRAME ();
8171 update_menu_bar (sf, 1);
8172 #ifdef HAVE_WINDOW_SYSTEM
8173 update_tool_bar (sf, 1);
8174 #endif
8177 /* Motif needs this. See comment in xmenu.c. Turn it off when
8178 pending_menu_activation is not defined. */
8179 #ifdef USE_X_TOOLKIT
8180 pending_menu_activation = 0;
8181 #endif
8185 /* Update the menu bar item list for frame F. This has to be done
8186 before we start to fill in any display lines, because it can call
8187 eval.
8189 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8191 static void
8192 update_menu_bar (f, save_match_data)
8193 struct frame *f;
8194 int save_match_data;
8196 Lisp_Object window;
8197 register struct window *w;
8199 /* If called recursively during a menu update, do nothing. This can
8200 happen when, for instance, an activate-menubar-hook causes a
8201 redisplay. */
8202 if (inhibit_menubar_update)
8203 return;
8205 window = FRAME_SELECTED_WINDOW (f);
8206 w = XWINDOW (window);
8208 #if 0 /* The if statement below this if statement used to include the
8209 condition !NILP (w->update_mode_line), rather than using
8210 update_mode_lines directly, and this if statement may have
8211 been added to make that condition work. Now the if
8212 statement below matches its comment, this isn't needed. */
8213 if (update_mode_lines)
8214 w->update_mode_line = Qt;
8215 #endif
8217 if (FRAME_WINDOW_P (f)
8219 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8220 || defined (USE_GTK)
8221 FRAME_EXTERNAL_MENU_BAR (f)
8222 #else
8223 FRAME_MENU_BAR_LINES (f) > 0
8224 #endif
8225 : FRAME_MENU_BAR_LINES (f) > 0)
8227 /* If the user has switched buffers or windows, we need to
8228 recompute to reflect the new bindings. But we'll
8229 recompute when update_mode_lines is set too; that means
8230 that people can use force-mode-line-update to request
8231 that the menu bar be recomputed. The adverse effect on
8232 the rest of the redisplay algorithm is about the same as
8233 windows_or_buffers_changed anyway. */
8234 if (windows_or_buffers_changed
8235 /* This used to test w->update_mode_line, but we believe
8236 there is no need to recompute the menu in that case. */
8237 || update_mode_lines
8238 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8239 < BUF_MODIFF (XBUFFER (w->buffer)))
8240 != !NILP (w->last_had_star))
8241 || ((!NILP (Vtransient_mark_mode)
8242 && !NILP (XBUFFER (w->buffer)->mark_active))
8243 != !NILP (w->region_showing)))
8245 struct buffer *prev = current_buffer;
8246 int count = SPECPDL_INDEX ();
8248 specbind (Qinhibit_menubar_update, Qt);
8250 set_buffer_internal_1 (XBUFFER (w->buffer));
8251 if (save_match_data)
8252 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8253 if (NILP (Voverriding_local_map_menu_flag))
8255 specbind (Qoverriding_terminal_local_map, Qnil);
8256 specbind (Qoverriding_local_map, Qnil);
8259 /* Run the Lucid hook. */
8260 safe_run_hooks (Qactivate_menubar_hook);
8262 /* If it has changed current-menubar from previous value,
8263 really recompute the menu-bar from the value. */
8264 if (! NILP (Vlucid_menu_bar_dirty_flag))
8265 call0 (Qrecompute_lucid_menubar);
8267 safe_run_hooks (Qmenu_bar_update_hook);
8268 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8270 /* Redisplay the menu bar in case we changed it. */
8271 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8272 || defined (USE_GTK)
8273 if (FRAME_WINDOW_P (f)
8274 #if defined (MAC_OS)
8275 /* All frames on Mac OS share the same menubar. So only the
8276 selected frame should be allowed to set it. */
8277 && f == SELECTED_FRAME ()
8278 #endif
8280 set_frame_menubar (f, 0, 0);
8281 else
8282 /* On a terminal screen, the menu bar is an ordinary screen
8283 line, and this makes it get updated. */
8284 w->update_mode_line = Qt;
8285 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8286 /* In the non-toolkit version, the menu bar is an ordinary screen
8287 line, and this makes it get updated. */
8288 w->update_mode_line = Qt;
8289 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8291 unbind_to (count, Qnil);
8292 set_buffer_internal_1 (prev);
8299 /***********************************************************************
8300 Output Cursor
8301 ***********************************************************************/
8303 #ifdef HAVE_WINDOW_SYSTEM
8305 /* EXPORT:
8306 Nominal cursor position -- where to draw output.
8307 HPOS and VPOS are window relative glyph matrix coordinates.
8308 X and Y are window relative pixel coordinates. */
8310 struct cursor_pos output_cursor;
8313 /* EXPORT:
8314 Set the global variable output_cursor to CURSOR. All cursor
8315 positions are relative to updated_window. */
8317 void
8318 set_output_cursor (cursor)
8319 struct cursor_pos *cursor;
8321 output_cursor.hpos = cursor->hpos;
8322 output_cursor.vpos = cursor->vpos;
8323 output_cursor.x = cursor->x;
8324 output_cursor.y = cursor->y;
8328 /* EXPORT for RIF:
8329 Set a nominal cursor position.
8331 HPOS and VPOS are column/row positions in a window glyph matrix. X
8332 and Y are window text area relative pixel positions.
8334 If this is done during an update, updated_window will contain the
8335 window that is being updated and the position is the future output
8336 cursor position for that window. If updated_window is null, use
8337 selected_window and display the cursor at the given position. */
8339 void
8340 x_cursor_to (vpos, hpos, y, x)
8341 int vpos, hpos, y, x;
8343 struct window *w;
8345 /* If updated_window is not set, work on selected_window. */
8346 if (updated_window)
8347 w = updated_window;
8348 else
8349 w = XWINDOW (selected_window);
8351 /* Set the output cursor. */
8352 output_cursor.hpos = hpos;
8353 output_cursor.vpos = vpos;
8354 output_cursor.x = x;
8355 output_cursor.y = y;
8357 /* If not called as part of an update, really display the cursor.
8358 This will also set the cursor position of W. */
8359 if (updated_window == NULL)
8361 BLOCK_INPUT;
8362 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8363 if (rif->flush_display_optional)
8364 rif->flush_display_optional (SELECTED_FRAME ());
8365 UNBLOCK_INPUT;
8369 #endif /* HAVE_WINDOW_SYSTEM */
8372 /***********************************************************************
8373 Tool-bars
8374 ***********************************************************************/
8376 #ifdef HAVE_WINDOW_SYSTEM
8378 /* Where the mouse was last time we reported a mouse event. */
8380 FRAME_PTR last_mouse_frame;
8382 /* Tool-bar item index of the item on which a mouse button was pressed
8383 or -1. */
8385 int last_tool_bar_item;
8388 /* Update the tool-bar item list for frame F. This has to be done
8389 before we start to fill in any display lines. Called from
8390 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8391 and restore it here. */
8393 static void
8394 update_tool_bar (f, save_match_data)
8395 struct frame *f;
8396 int save_match_data;
8398 #ifdef USE_GTK
8399 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8400 #else
8401 int do_update = WINDOWP (f->tool_bar_window)
8402 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8403 #endif
8405 if (do_update)
8407 Lisp_Object window;
8408 struct window *w;
8410 window = FRAME_SELECTED_WINDOW (f);
8411 w = XWINDOW (window);
8413 /* If the user has switched buffers or windows, we need to
8414 recompute to reflect the new bindings. But we'll
8415 recompute when update_mode_lines is set too; that means
8416 that people can use force-mode-line-update to request
8417 that the menu bar be recomputed. The adverse effect on
8418 the rest of the redisplay algorithm is about the same as
8419 windows_or_buffers_changed anyway. */
8420 if (windows_or_buffers_changed
8421 || !NILP (w->update_mode_line)
8422 || update_mode_lines
8423 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8424 < BUF_MODIFF (XBUFFER (w->buffer)))
8425 != !NILP (w->last_had_star))
8426 || ((!NILP (Vtransient_mark_mode)
8427 && !NILP (XBUFFER (w->buffer)->mark_active))
8428 != !NILP (w->region_showing)))
8430 struct buffer *prev = current_buffer;
8431 int count = SPECPDL_INDEX ();
8432 Lisp_Object new_tool_bar;
8433 int new_n_tool_bar;
8434 struct gcpro gcpro1;
8436 /* Set current_buffer to the buffer of the selected
8437 window of the frame, so that we get the right local
8438 keymaps. */
8439 set_buffer_internal_1 (XBUFFER (w->buffer));
8441 /* Save match data, if we must. */
8442 if (save_match_data)
8443 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8445 /* Make sure that we don't accidentally use bogus keymaps. */
8446 if (NILP (Voverriding_local_map_menu_flag))
8448 specbind (Qoverriding_terminal_local_map, Qnil);
8449 specbind (Qoverriding_local_map, Qnil);
8452 GCPRO1 (new_tool_bar);
8454 /* Build desired tool-bar items from keymaps. */
8455 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8456 &new_n_tool_bar);
8458 /* Redisplay the tool-bar if we changed it. */
8459 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8461 /* Redisplay that happens asynchronously due to an expose event
8462 may access f->tool_bar_items. Make sure we update both
8463 variables within BLOCK_INPUT so no such event interrupts. */
8464 BLOCK_INPUT;
8465 f->tool_bar_items = new_tool_bar;
8466 f->n_tool_bar_items = new_n_tool_bar;
8467 w->update_mode_line = Qt;
8468 UNBLOCK_INPUT;
8471 UNGCPRO;
8473 unbind_to (count, Qnil);
8474 set_buffer_internal_1 (prev);
8480 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8481 F's desired tool-bar contents. F->tool_bar_items must have
8482 been set up previously by calling prepare_menu_bars. */
8484 static void
8485 build_desired_tool_bar_string (f)
8486 struct frame *f;
8488 int i, size, size_needed;
8489 struct gcpro gcpro1, gcpro2, gcpro3;
8490 Lisp_Object image, plist, props;
8492 image = plist = props = Qnil;
8493 GCPRO3 (image, plist, props);
8495 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8496 Otherwise, make a new string. */
8498 /* The size of the string we might be able to reuse. */
8499 size = (STRINGP (f->desired_tool_bar_string)
8500 ? SCHARS (f->desired_tool_bar_string)
8501 : 0);
8503 /* We need one space in the string for each image. */
8504 size_needed = f->n_tool_bar_items;
8506 /* Reuse f->desired_tool_bar_string, if possible. */
8507 if (size < size_needed || NILP (f->desired_tool_bar_string))
8508 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8509 make_number (' '));
8510 else
8512 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8513 Fremove_text_properties (make_number (0), make_number (size),
8514 props, f->desired_tool_bar_string);
8517 /* Put a `display' property on the string for the images to display,
8518 put a `menu_item' property on tool-bar items with a value that
8519 is the index of the item in F's tool-bar item vector. */
8520 for (i = 0; i < f->n_tool_bar_items; ++i)
8522 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8524 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8525 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8526 int hmargin, vmargin, relief, idx, end;
8527 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8529 /* If image is a vector, choose the image according to the
8530 button state. */
8531 image = PROP (TOOL_BAR_ITEM_IMAGES);
8532 if (VECTORP (image))
8534 if (enabled_p)
8535 idx = (selected_p
8536 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8537 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8538 else
8539 idx = (selected_p
8540 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8541 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8543 xassert (ASIZE (image) >= idx);
8544 image = AREF (image, idx);
8546 else
8547 idx = -1;
8549 /* Ignore invalid image specifications. */
8550 if (!valid_image_p (image))
8551 continue;
8553 /* Display the tool-bar button pressed, or depressed. */
8554 plist = Fcopy_sequence (XCDR (image));
8556 /* Compute margin and relief to draw. */
8557 relief = (tool_bar_button_relief >= 0
8558 ? tool_bar_button_relief
8559 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8560 hmargin = vmargin = relief;
8562 if (INTEGERP (Vtool_bar_button_margin)
8563 && XINT (Vtool_bar_button_margin) > 0)
8565 hmargin += XFASTINT (Vtool_bar_button_margin);
8566 vmargin += XFASTINT (Vtool_bar_button_margin);
8568 else if (CONSP (Vtool_bar_button_margin))
8570 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8571 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8572 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8574 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8575 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8576 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8579 if (auto_raise_tool_bar_buttons_p)
8581 /* Add a `:relief' property to the image spec if the item is
8582 selected. */
8583 if (selected_p)
8585 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8586 hmargin -= relief;
8587 vmargin -= relief;
8590 else
8592 /* If image is selected, display it pressed, i.e. with a
8593 negative relief. If it's not selected, display it with a
8594 raised relief. */
8595 plist = Fplist_put (plist, QCrelief,
8596 (selected_p
8597 ? make_number (-relief)
8598 : make_number (relief)));
8599 hmargin -= relief;
8600 vmargin -= relief;
8603 /* Put a margin around the image. */
8604 if (hmargin || vmargin)
8606 if (hmargin == vmargin)
8607 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8608 else
8609 plist = Fplist_put (plist, QCmargin,
8610 Fcons (make_number (hmargin),
8611 make_number (vmargin)));
8614 /* If button is not enabled, and we don't have special images
8615 for the disabled state, make the image appear disabled by
8616 applying an appropriate algorithm to it. */
8617 if (!enabled_p && idx < 0)
8618 plist = Fplist_put (plist, QCconversion, Qdisabled);
8620 /* Put a `display' text property on the string for the image to
8621 display. Put a `menu-item' property on the string that gives
8622 the start of this item's properties in the tool-bar items
8623 vector. */
8624 image = Fcons (Qimage, plist);
8625 props = list4 (Qdisplay, image,
8626 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8628 /* Let the last image hide all remaining spaces in the tool bar
8629 string. The string can be longer than needed when we reuse a
8630 previous string. */
8631 if (i + 1 == f->n_tool_bar_items)
8632 end = SCHARS (f->desired_tool_bar_string);
8633 else
8634 end = i + 1;
8635 Fadd_text_properties (make_number (i), make_number (end),
8636 props, f->desired_tool_bar_string);
8637 #undef PROP
8640 UNGCPRO;
8644 /* Display one line of the tool-bar of frame IT->f. */
8646 static void
8647 display_tool_bar_line (it)
8648 struct it *it;
8650 struct glyph_row *row = it->glyph_row;
8651 int max_x = it->last_visible_x;
8652 struct glyph *last;
8654 prepare_desired_row (row);
8655 row->y = it->current_y;
8657 /* Note that this isn't made use of if the face hasn't a box,
8658 so there's no need to check the face here. */
8659 it->start_of_box_run_p = 1;
8661 while (it->current_x < max_x)
8663 int x_before, x, n_glyphs_before, i, nglyphs;
8665 /* Get the next display element. */
8666 if (!get_next_display_element (it))
8667 break;
8669 /* Produce glyphs. */
8670 x_before = it->current_x;
8671 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8672 PRODUCE_GLYPHS (it);
8674 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8675 i = 0;
8676 x = x_before;
8677 while (i < nglyphs)
8679 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8681 if (x + glyph->pixel_width > max_x)
8683 /* Glyph doesn't fit on line. */
8684 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8685 it->current_x = x;
8686 goto out;
8689 ++it->hpos;
8690 x += glyph->pixel_width;
8691 ++i;
8694 /* Stop at line ends. */
8695 if (ITERATOR_AT_END_OF_LINE_P (it))
8696 break;
8698 set_iterator_to_next (it, 1);
8701 out:;
8703 row->displays_text_p = row->used[TEXT_AREA] != 0;
8704 extend_face_to_end_of_line (it);
8705 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8706 last->right_box_line_p = 1;
8707 if (last == row->glyphs[TEXT_AREA])
8708 last->left_box_line_p = 1;
8709 compute_line_metrics (it);
8711 /* If line is empty, make it occupy the rest of the tool-bar. */
8712 if (!row->displays_text_p)
8714 row->height = row->phys_height = it->last_visible_y - row->y;
8715 row->ascent = row->phys_ascent = 0;
8716 row->extra_line_spacing = 0;
8719 row->full_width_p = 1;
8720 row->continued_p = 0;
8721 row->truncated_on_left_p = 0;
8722 row->truncated_on_right_p = 0;
8724 it->current_x = it->hpos = 0;
8725 it->current_y += row->height;
8726 ++it->vpos;
8727 ++it->glyph_row;
8731 /* Value is the number of screen lines needed to make all tool-bar
8732 items of frame F visible. */
8734 static int
8735 tool_bar_lines_needed (f)
8736 struct frame *f;
8738 struct window *w = XWINDOW (f->tool_bar_window);
8739 struct it it;
8741 /* Initialize an iterator for iteration over
8742 F->desired_tool_bar_string in the tool-bar window of frame F. */
8743 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8744 it.first_visible_x = 0;
8745 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8746 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8748 while (!ITERATOR_AT_END_P (&it))
8750 it.glyph_row = w->desired_matrix->rows;
8751 clear_glyph_row (it.glyph_row);
8752 display_tool_bar_line (&it);
8755 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8759 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8760 0, 1, 0,
8761 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8762 (frame)
8763 Lisp_Object frame;
8765 struct frame *f;
8766 struct window *w;
8767 int nlines = 0;
8769 if (NILP (frame))
8770 frame = selected_frame;
8771 else
8772 CHECK_FRAME (frame);
8773 f = XFRAME (frame);
8775 if (WINDOWP (f->tool_bar_window)
8776 || (w = XWINDOW (f->tool_bar_window),
8777 WINDOW_TOTAL_LINES (w) > 0))
8779 update_tool_bar (f, 1);
8780 if (f->n_tool_bar_items)
8782 build_desired_tool_bar_string (f);
8783 nlines = tool_bar_lines_needed (f);
8787 return make_number (nlines);
8791 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8792 height should be changed. */
8794 static int
8795 redisplay_tool_bar (f)
8796 struct frame *f;
8798 struct window *w;
8799 struct it it;
8800 struct glyph_row *row;
8801 int change_height_p = 0;
8803 #ifdef USE_GTK
8804 if (FRAME_EXTERNAL_TOOL_BAR (f))
8805 update_frame_tool_bar (f);
8806 return 0;
8807 #endif
8809 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8810 do anything. This means you must start with tool-bar-lines
8811 non-zero to get the auto-sizing effect. Or in other words, you
8812 can turn off tool-bars by specifying tool-bar-lines zero. */
8813 if (!WINDOWP (f->tool_bar_window)
8814 || (w = XWINDOW (f->tool_bar_window),
8815 WINDOW_TOTAL_LINES (w) == 0))
8816 return 0;
8818 /* Set up an iterator for the tool-bar window. */
8819 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8820 it.first_visible_x = 0;
8821 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8822 row = it.glyph_row;
8824 /* Build a string that represents the contents of the tool-bar. */
8825 build_desired_tool_bar_string (f);
8826 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8828 /* Display as many lines as needed to display all tool-bar items. */
8829 while (it.current_y < it.last_visible_y)
8830 display_tool_bar_line (&it);
8832 /* It doesn't make much sense to try scrolling in the tool-bar
8833 window, so don't do it. */
8834 w->desired_matrix->no_scrolling_p = 1;
8835 w->must_be_updated_p = 1;
8837 if (auto_resize_tool_bars_p)
8839 int nlines;
8841 /* If we couldn't display everything, change the tool-bar's
8842 height. */
8843 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8844 change_height_p = 1;
8846 /* If there are blank lines at the end, except for a partially
8847 visible blank line at the end that is smaller than
8848 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8849 row = it.glyph_row - 1;
8850 if (!row->displays_text_p
8851 && row->height >= FRAME_LINE_HEIGHT (f))
8852 change_height_p = 1;
8854 /* If row displays tool-bar items, but is partially visible,
8855 change the tool-bar's height. */
8856 if (row->displays_text_p
8857 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8858 change_height_p = 1;
8860 /* Resize windows as needed by changing the `tool-bar-lines'
8861 frame parameter. */
8862 if (change_height_p
8863 && (nlines = tool_bar_lines_needed (f),
8864 nlines != WINDOW_TOTAL_LINES (w)))
8866 extern Lisp_Object Qtool_bar_lines;
8867 Lisp_Object frame;
8868 int old_height = WINDOW_TOTAL_LINES (w);
8870 XSETFRAME (frame, f);
8871 clear_glyph_matrix (w->desired_matrix);
8872 Fmodify_frame_parameters (frame,
8873 Fcons (Fcons (Qtool_bar_lines,
8874 make_number (nlines)),
8875 Qnil));
8876 if (WINDOW_TOTAL_LINES (w) != old_height)
8877 fonts_changed_p = 1;
8881 return change_height_p;
8885 /* Get information about the tool-bar item which is displayed in GLYPH
8886 on frame F. Return in *PROP_IDX the index where tool-bar item
8887 properties start in F->tool_bar_items. Value is zero if
8888 GLYPH doesn't display a tool-bar item. */
8890 static int
8891 tool_bar_item_info (f, glyph, prop_idx)
8892 struct frame *f;
8893 struct glyph *glyph;
8894 int *prop_idx;
8896 Lisp_Object prop;
8897 int success_p;
8898 int charpos;
8900 /* This function can be called asynchronously, which means we must
8901 exclude any possibility that Fget_text_property signals an
8902 error. */
8903 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8904 charpos = max (0, charpos);
8906 /* Get the text property `menu-item' at pos. The value of that
8907 property is the start index of this item's properties in
8908 F->tool_bar_items. */
8909 prop = Fget_text_property (make_number (charpos),
8910 Qmenu_item, f->current_tool_bar_string);
8911 if (INTEGERP (prop))
8913 *prop_idx = XINT (prop);
8914 success_p = 1;
8916 else
8917 success_p = 0;
8919 return success_p;
8923 /* Get information about the tool-bar item at position X/Y on frame F.
8924 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8925 the current matrix of the tool-bar window of F, or NULL if not
8926 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8927 item in F->tool_bar_items. Value is
8929 -1 if X/Y is not on a tool-bar item
8930 0 if X/Y is on the same item that was highlighted before.
8931 1 otherwise. */
8933 static int
8934 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8935 struct frame *f;
8936 int x, y;
8937 struct glyph **glyph;
8938 int *hpos, *vpos, *prop_idx;
8940 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8941 struct window *w = XWINDOW (f->tool_bar_window);
8942 int area;
8944 /* Find the glyph under X/Y. */
8945 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8946 if (*glyph == NULL)
8947 return -1;
8949 /* Get the start of this tool-bar item's properties in
8950 f->tool_bar_items. */
8951 if (!tool_bar_item_info (f, *glyph, prop_idx))
8952 return -1;
8954 /* Is mouse on the highlighted item? */
8955 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8956 && *vpos >= dpyinfo->mouse_face_beg_row
8957 && *vpos <= dpyinfo->mouse_face_end_row
8958 && (*vpos > dpyinfo->mouse_face_beg_row
8959 || *hpos >= dpyinfo->mouse_face_beg_col)
8960 && (*vpos < dpyinfo->mouse_face_end_row
8961 || *hpos < dpyinfo->mouse_face_end_col
8962 || dpyinfo->mouse_face_past_end))
8963 return 0;
8965 return 1;
8969 /* EXPORT:
8970 Handle mouse button event on the tool-bar of frame F, at
8971 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8972 0 for button release. MODIFIERS is event modifiers for button
8973 release. */
8975 void
8976 handle_tool_bar_click (f, x, y, down_p, modifiers)
8977 struct frame *f;
8978 int x, y, down_p;
8979 unsigned int modifiers;
8981 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8982 struct window *w = XWINDOW (f->tool_bar_window);
8983 int hpos, vpos, prop_idx;
8984 struct glyph *glyph;
8985 Lisp_Object enabled_p;
8987 /* If not on the highlighted tool-bar item, return. */
8988 frame_to_window_pixel_xy (w, &x, &y);
8989 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8990 return;
8992 /* If item is disabled, do nothing. */
8993 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8994 if (NILP (enabled_p))
8995 return;
8997 if (down_p)
8999 /* Show item in pressed state. */
9000 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9001 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9002 last_tool_bar_item = prop_idx;
9004 else
9006 Lisp_Object key, frame;
9007 struct input_event event;
9008 EVENT_INIT (event);
9010 /* Show item in released state. */
9011 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9012 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9014 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9016 XSETFRAME (frame, f);
9017 event.kind = TOOL_BAR_EVENT;
9018 event.frame_or_window = frame;
9019 event.arg = frame;
9020 kbd_buffer_store_event (&event);
9022 event.kind = TOOL_BAR_EVENT;
9023 event.frame_or_window = frame;
9024 event.arg = key;
9025 event.modifiers = modifiers;
9026 kbd_buffer_store_event (&event);
9027 last_tool_bar_item = -1;
9032 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9033 tool-bar window-relative coordinates X/Y. Called from
9034 note_mouse_highlight. */
9036 static void
9037 note_tool_bar_highlight (f, x, y)
9038 struct frame *f;
9039 int x, y;
9041 Lisp_Object window = f->tool_bar_window;
9042 struct window *w = XWINDOW (window);
9043 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9044 int hpos, vpos;
9045 struct glyph *glyph;
9046 struct glyph_row *row;
9047 int i;
9048 Lisp_Object enabled_p;
9049 int prop_idx;
9050 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9051 int mouse_down_p, rc;
9053 /* Function note_mouse_highlight is called with negative x(y
9054 values when mouse moves outside of the frame. */
9055 if (x <= 0 || y <= 0)
9057 clear_mouse_face (dpyinfo);
9058 return;
9061 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9062 if (rc < 0)
9064 /* Not on tool-bar item. */
9065 clear_mouse_face (dpyinfo);
9066 return;
9068 else if (rc == 0)
9069 /* On same tool-bar item as before. */
9070 goto set_help_echo;
9072 clear_mouse_face (dpyinfo);
9074 /* Mouse is down, but on different tool-bar item? */
9075 mouse_down_p = (dpyinfo->grabbed
9076 && f == last_mouse_frame
9077 && FRAME_LIVE_P (f));
9078 if (mouse_down_p
9079 && last_tool_bar_item != prop_idx)
9080 return;
9082 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9083 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9085 /* If tool-bar item is not enabled, don't highlight it. */
9086 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9087 if (!NILP (enabled_p))
9089 /* Compute the x-position of the glyph. In front and past the
9090 image is a space. We include this in the highlighted area. */
9091 row = MATRIX_ROW (w->current_matrix, vpos);
9092 for (i = x = 0; i < hpos; ++i)
9093 x += row->glyphs[TEXT_AREA][i].pixel_width;
9095 /* Record this as the current active region. */
9096 dpyinfo->mouse_face_beg_col = hpos;
9097 dpyinfo->mouse_face_beg_row = vpos;
9098 dpyinfo->mouse_face_beg_x = x;
9099 dpyinfo->mouse_face_beg_y = row->y;
9100 dpyinfo->mouse_face_past_end = 0;
9102 dpyinfo->mouse_face_end_col = hpos + 1;
9103 dpyinfo->mouse_face_end_row = vpos;
9104 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9105 dpyinfo->mouse_face_end_y = row->y;
9106 dpyinfo->mouse_face_window = window;
9107 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9109 /* Display it as active. */
9110 show_mouse_face (dpyinfo, draw);
9111 dpyinfo->mouse_face_image_state = draw;
9114 set_help_echo:
9116 /* Set help_echo_string to a help string to display for this tool-bar item.
9117 XTread_socket does the rest. */
9118 help_echo_object = help_echo_window = Qnil;
9119 help_echo_pos = -1;
9120 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9121 if (NILP (help_echo_string))
9122 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9125 #endif /* HAVE_WINDOW_SYSTEM */
9129 /************************************************************************
9130 Horizontal scrolling
9131 ************************************************************************/
9133 static int hscroll_window_tree P_ ((Lisp_Object));
9134 static int hscroll_windows P_ ((Lisp_Object));
9136 /* For all leaf windows in the window tree rooted at WINDOW, set their
9137 hscroll value so that PT is (i) visible in the window, and (ii) so
9138 that it is not within a certain margin at the window's left and
9139 right border. Value is non-zero if any window's hscroll has been
9140 changed. */
9142 static int
9143 hscroll_window_tree (window)
9144 Lisp_Object window;
9146 int hscrolled_p = 0;
9147 int hscroll_relative_p = FLOATP (Vhscroll_step);
9148 int hscroll_step_abs = 0;
9149 double hscroll_step_rel = 0;
9151 if (hscroll_relative_p)
9153 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9154 if (hscroll_step_rel < 0)
9156 hscroll_relative_p = 0;
9157 hscroll_step_abs = 0;
9160 else if (INTEGERP (Vhscroll_step))
9162 hscroll_step_abs = XINT (Vhscroll_step);
9163 if (hscroll_step_abs < 0)
9164 hscroll_step_abs = 0;
9166 else
9167 hscroll_step_abs = 0;
9169 while (WINDOWP (window))
9171 struct window *w = XWINDOW (window);
9173 if (WINDOWP (w->hchild))
9174 hscrolled_p |= hscroll_window_tree (w->hchild);
9175 else if (WINDOWP (w->vchild))
9176 hscrolled_p |= hscroll_window_tree (w->vchild);
9177 else if (w->cursor.vpos >= 0)
9179 int h_margin;
9180 int text_area_width;
9181 struct glyph_row *current_cursor_row
9182 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9183 struct glyph_row *desired_cursor_row
9184 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9185 struct glyph_row *cursor_row
9186 = (desired_cursor_row->enabled_p
9187 ? desired_cursor_row
9188 : current_cursor_row);
9190 text_area_width = window_box_width (w, TEXT_AREA);
9192 /* Scroll when cursor is inside this scroll margin. */
9193 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9195 if ((XFASTINT (w->hscroll)
9196 && w->cursor.x <= h_margin)
9197 || (cursor_row->enabled_p
9198 && cursor_row->truncated_on_right_p
9199 && (w->cursor.x >= text_area_width - h_margin)))
9201 struct it it;
9202 int hscroll;
9203 struct buffer *saved_current_buffer;
9204 int pt;
9205 int wanted_x;
9207 /* Find point in a display of infinite width. */
9208 saved_current_buffer = current_buffer;
9209 current_buffer = XBUFFER (w->buffer);
9211 if (w == XWINDOW (selected_window))
9212 pt = BUF_PT (current_buffer);
9213 else
9215 pt = marker_position (w->pointm);
9216 pt = max (BEGV, pt);
9217 pt = min (ZV, pt);
9220 /* Move iterator to pt starting at cursor_row->start in
9221 a line with infinite width. */
9222 init_to_row_start (&it, w, cursor_row);
9223 it.last_visible_x = INFINITY;
9224 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9225 current_buffer = saved_current_buffer;
9227 /* Position cursor in window. */
9228 if (!hscroll_relative_p && hscroll_step_abs == 0)
9229 hscroll = max (0, (it.current_x
9230 - (ITERATOR_AT_END_OF_LINE_P (&it)
9231 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9232 : (text_area_width / 2))))
9233 / FRAME_COLUMN_WIDTH (it.f);
9234 else if (w->cursor.x >= text_area_width - h_margin)
9236 if (hscroll_relative_p)
9237 wanted_x = text_area_width * (1 - hscroll_step_rel)
9238 - h_margin;
9239 else
9240 wanted_x = text_area_width
9241 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9242 - h_margin;
9243 hscroll
9244 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9246 else
9248 if (hscroll_relative_p)
9249 wanted_x = text_area_width * hscroll_step_rel
9250 + h_margin;
9251 else
9252 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9253 + h_margin;
9254 hscroll
9255 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9257 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9259 /* Don't call Fset_window_hscroll if value hasn't
9260 changed because it will prevent redisplay
9261 optimizations. */
9262 if (XFASTINT (w->hscroll) != hscroll)
9264 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9265 w->hscroll = make_number (hscroll);
9266 hscrolled_p = 1;
9271 window = w->next;
9274 /* Value is non-zero if hscroll of any leaf window has been changed. */
9275 return hscrolled_p;
9279 /* Set hscroll so that cursor is visible and not inside horizontal
9280 scroll margins for all windows in the tree rooted at WINDOW. See
9281 also hscroll_window_tree above. Value is non-zero if any window's
9282 hscroll has been changed. If it has, desired matrices on the frame
9283 of WINDOW are cleared. */
9285 static int
9286 hscroll_windows (window)
9287 Lisp_Object window;
9289 int hscrolled_p;
9291 if (automatic_hscrolling_p)
9293 hscrolled_p = hscroll_window_tree (window);
9294 if (hscrolled_p)
9295 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9297 else
9298 hscrolled_p = 0;
9299 return hscrolled_p;
9304 /************************************************************************
9305 Redisplay
9306 ************************************************************************/
9308 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9309 to a non-zero value. This is sometimes handy to have in a debugger
9310 session. */
9312 #if GLYPH_DEBUG
9314 /* First and last unchanged row for try_window_id. */
9316 int debug_first_unchanged_at_end_vpos;
9317 int debug_last_unchanged_at_beg_vpos;
9319 /* Delta vpos and y. */
9321 int debug_dvpos, debug_dy;
9323 /* Delta in characters and bytes for try_window_id. */
9325 int debug_delta, debug_delta_bytes;
9327 /* Values of window_end_pos and window_end_vpos at the end of
9328 try_window_id. */
9330 EMACS_INT debug_end_pos, debug_end_vpos;
9332 /* Append a string to W->desired_matrix->method. FMT is a printf
9333 format string. A1...A9 are a supplement for a variable-length
9334 argument list. If trace_redisplay_p is non-zero also printf the
9335 resulting string to stderr. */
9337 static void
9338 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9339 struct window *w;
9340 char *fmt;
9341 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9343 char buffer[512];
9344 char *method = w->desired_matrix->method;
9345 int len = strlen (method);
9346 int size = sizeof w->desired_matrix->method;
9347 int remaining = size - len - 1;
9349 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9350 if (len && remaining)
9352 method[len] = '|';
9353 --remaining, ++len;
9356 strncpy (method + len, buffer, remaining);
9358 if (trace_redisplay_p)
9359 fprintf (stderr, "%p (%s): %s\n",
9361 ((BUFFERP (w->buffer)
9362 && STRINGP (XBUFFER (w->buffer)->name))
9363 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9364 : "no buffer"),
9365 buffer);
9368 #endif /* GLYPH_DEBUG */
9371 /* Value is non-zero if all changes in window W, which displays
9372 current_buffer, are in the text between START and END. START is a
9373 buffer position, END is given as a distance from Z. Used in
9374 redisplay_internal for display optimization. */
9376 static INLINE int
9377 text_outside_line_unchanged_p (w, start, end)
9378 struct window *w;
9379 int start, end;
9381 int unchanged_p = 1;
9383 /* If text or overlays have changed, see where. */
9384 if (XFASTINT (w->last_modified) < MODIFF
9385 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9387 /* Gap in the line? */
9388 if (GPT < start || Z - GPT < end)
9389 unchanged_p = 0;
9391 /* Changes start in front of the line, or end after it? */
9392 if (unchanged_p
9393 && (BEG_UNCHANGED < start - 1
9394 || END_UNCHANGED < end))
9395 unchanged_p = 0;
9397 /* If selective display, can't optimize if changes start at the
9398 beginning of the line. */
9399 if (unchanged_p
9400 && INTEGERP (current_buffer->selective_display)
9401 && XINT (current_buffer->selective_display) > 0
9402 && (BEG_UNCHANGED < start || GPT <= start))
9403 unchanged_p = 0;
9405 /* If there are overlays at the start or end of the line, these
9406 may have overlay strings with newlines in them. A change at
9407 START, for instance, may actually concern the display of such
9408 overlay strings as well, and they are displayed on different
9409 lines. So, quickly rule out this case. (For the future, it
9410 might be desirable to implement something more telling than
9411 just BEG/END_UNCHANGED.) */
9412 if (unchanged_p)
9414 if (BEG + BEG_UNCHANGED == start
9415 && overlay_touches_p (start))
9416 unchanged_p = 0;
9417 if (END_UNCHANGED == end
9418 && overlay_touches_p (Z - end))
9419 unchanged_p = 0;
9423 return unchanged_p;
9427 /* Do a frame update, taking possible shortcuts into account. This is
9428 the main external entry point for redisplay.
9430 If the last redisplay displayed an echo area message and that message
9431 is no longer requested, we clear the echo area or bring back the
9432 mini-buffer if that is in use. */
9434 void
9435 redisplay ()
9437 redisplay_internal (0);
9441 static Lisp_Object
9442 overlay_arrow_string_or_property (var, pbitmap)
9443 Lisp_Object var;
9444 int *pbitmap;
9446 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9447 Lisp_Object bitmap;
9449 if (pbitmap)
9451 *pbitmap = 0;
9452 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9453 *pbitmap = XINT (bitmap);
9456 if (!NILP (pstr))
9457 return pstr;
9458 return Voverlay_arrow_string;
9461 /* Return 1 if there are any overlay-arrows in current_buffer. */
9462 static int
9463 overlay_arrow_in_current_buffer_p ()
9465 Lisp_Object vlist;
9467 for (vlist = Voverlay_arrow_variable_list;
9468 CONSP (vlist);
9469 vlist = XCDR (vlist))
9471 Lisp_Object var = XCAR (vlist);
9472 Lisp_Object val;
9474 if (!SYMBOLP (var))
9475 continue;
9476 val = find_symbol_value (var);
9477 if (MARKERP (val)
9478 && current_buffer == XMARKER (val)->buffer)
9479 return 1;
9481 return 0;
9485 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9486 has changed. */
9488 static int
9489 overlay_arrows_changed_p ()
9491 Lisp_Object vlist;
9493 for (vlist = Voverlay_arrow_variable_list;
9494 CONSP (vlist);
9495 vlist = XCDR (vlist))
9497 Lisp_Object var = XCAR (vlist);
9498 Lisp_Object val, pstr;
9500 if (!SYMBOLP (var))
9501 continue;
9502 val = find_symbol_value (var);
9503 if (!MARKERP (val))
9504 continue;
9505 if (! EQ (COERCE_MARKER (val),
9506 Fget (var, Qlast_arrow_position))
9507 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9508 EQ (pstr, Fget (var, Qlast_arrow_string))))
9509 return 1;
9511 return 0;
9514 /* Mark overlay arrows to be updated on next redisplay. */
9516 static void
9517 update_overlay_arrows (up_to_date)
9518 int up_to_date;
9520 Lisp_Object vlist;
9522 for (vlist = Voverlay_arrow_variable_list;
9523 CONSP (vlist);
9524 vlist = XCDR (vlist))
9526 Lisp_Object var = XCAR (vlist);
9528 if (!SYMBOLP (var))
9529 continue;
9531 if (up_to_date > 0)
9533 Lisp_Object val = find_symbol_value (var);
9534 Fput (var, Qlast_arrow_position,
9535 COERCE_MARKER (val));
9536 Fput (var, Qlast_arrow_string,
9537 overlay_arrow_string_or_property (var, 0));
9539 else if (up_to_date < 0
9540 || !NILP (Fget (var, Qlast_arrow_position)))
9542 Fput (var, Qlast_arrow_position, Qt);
9543 Fput (var, Qlast_arrow_string, Qt);
9549 /* Return overlay arrow string to display at row.
9550 Return t if display as bitmap in left fringe.
9551 Return nil if no overlay arrow. */
9553 static Lisp_Object
9554 overlay_arrow_at_row (it, row, pbitmap)
9555 struct it *it;
9556 struct glyph_row *row;
9557 int *pbitmap;
9559 Lisp_Object vlist;
9561 for (vlist = Voverlay_arrow_variable_list;
9562 CONSP (vlist);
9563 vlist = XCDR (vlist))
9565 Lisp_Object var = XCAR (vlist);
9566 Lisp_Object val;
9568 if (!SYMBOLP (var))
9569 continue;
9571 val = find_symbol_value (var);
9573 if (MARKERP (val)
9574 && current_buffer == XMARKER (val)->buffer
9575 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9577 val = overlay_arrow_string_or_property (var, pbitmap);
9578 if (FRAME_WINDOW_P (it->f)
9579 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9580 return Qt;
9581 if (STRINGP (val))
9582 return val;
9583 break;
9587 *pbitmap = 0;
9588 return Qnil;
9591 /* Return 1 if point moved out of or into a composition. Otherwise
9592 return 0. PREV_BUF and PREV_PT are the last point buffer and
9593 position. BUF and PT are the current point buffer and position. */
9596 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9597 struct buffer *prev_buf, *buf;
9598 int prev_pt, pt;
9600 int start, end;
9601 Lisp_Object prop;
9602 Lisp_Object buffer;
9604 XSETBUFFER (buffer, buf);
9605 /* Check a composition at the last point if point moved within the
9606 same buffer. */
9607 if (prev_buf == buf)
9609 if (prev_pt == pt)
9610 /* Point didn't move. */
9611 return 0;
9613 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9614 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9615 && COMPOSITION_VALID_P (start, end, prop)
9616 && start < prev_pt && end > prev_pt)
9617 /* The last point was within the composition. Return 1 iff
9618 point moved out of the composition. */
9619 return (pt <= start || pt >= end);
9622 /* Check a composition at the current point. */
9623 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9624 && find_composition (pt, -1, &start, &end, &prop, buffer)
9625 && COMPOSITION_VALID_P (start, end, prop)
9626 && start < pt && end > pt);
9630 /* Reconsider the setting of B->clip_changed which is displayed
9631 in window W. */
9633 static INLINE void
9634 reconsider_clip_changes (w, b)
9635 struct window *w;
9636 struct buffer *b;
9638 if (b->clip_changed
9639 && !NILP (w->window_end_valid)
9640 && w->current_matrix->buffer == b
9641 && w->current_matrix->zv == BUF_ZV (b)
9642 && w->current_matrix->begv == BUF_BEGV (b))
9643 b->clip_changed = 0;
9645 /* If display wasn't paused, and W is not a tool bar window, see if
9646 point has been moved into or out of a composition. In that case,
9647 we set b->clip_changed to 1 to force updating the screen. If
9648 b->clip_changed has already been set to 1, we can skip this
9649 check. */
9650 if (!b->clip_changed
9651 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9653 int pt;
9655 if (w == XWINDOW (selected_window))
9656 pt = BUF_PT (current_buffer);
9657 else
9658 pt = marker_position (w->pointm);
9660 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9661 || pt != XINT (w->last_point))
9662 && check_point_in_composition (w->current_matrix->buffer,
9663 XINT (w->last_point),
9664 XBUFFER (w->buffer), pt))
9665 b->clip_changed = 1;
9670 /* Select FRAME to forward the values of frame-local variables into C
9671 variables so that the redisplay routines can access those values
9672 directly. */
9674 static void
9675 select_frame_for_redisplay (frame)
9676 Lisp_Object frame;
9678 Lisp_Object tail, sym, val;
9679 Lisp_Object old = selected_frame;
9681 selected_frame = frame;
9683 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9684 if (CONSP (XCAR (tail))
9685 && (sym = XCAR (XCAR (tail)),
9686 SYMBOLP (sym))
9687 && (sym = indirect_variable (sym),
9688 val = SYMBOL_VALUE (sym),
9689 (BUFFER_LOCAL_VALUEP (val)
9690 || SOME_BUFFER_LOCAL_VALUEP (val)))
9691 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9692 Fsymbol_value (sym);
9694 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9695 if (CONSP (XCAR (tail))
9696 && (sym = XCAR (XCAR (tail)),
9697 SYMBOLP (sym))
9698 && (sym = indirect_variable (sym),
9699 val = SYMBOL_VALUE (sym),
9700 (BUFFER_LOCAL_VALUEP (val)
9701 || SOME_BUFFER_LOCAL_VALUEP (val)))
9702 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9703 Fsymbol_value (sym);
9707 #define STOP_POLLING \
9708 do { if (! polling_stopped_here) stop_polling (); \
9709 polling_stopped_here = 1; } while (0)
9711 #define RESUME_POLLING \
9712 do { if (polling_stopped_here) start_polling (); \
9713 polling_stopped_here = 0; } while (0)
9716 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9717 response to any user action; therefore, we should preserve the echo
9718 area. (Actually, our caller does that job.) Perhaps in the future
9719 avoid recentering windows if it is not necessary; currently that
9720 causes some problems. */
9722 static void
9723 redisplay_internal (preserve_echo_area)
9724 int preserve_echo_area;
9726 struct window *w = XWINDOW (selected_window);
9727 struct frame *f = XFRAME (w->frame);
9728 int pause;
9729 int must_finish = 0;
9730 struct text_pos tlbufpos, tlendpos;
9731 int number_of_visible_frames;
9732 int count;
9733 struct frame *sf = SELECTED_FRAME ();
9734 int polling_stopped_here = 0;
9736 /* Non-zero means redisplay has to consider all windows on all
9737 frames. Zero means, only selected_window is considered. */
9738 int consider_all_windows_p;
9740 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9742 /* No redisplay if running in batch mode or frame is not yet fully
9743 initialized, or redisplay is explicitly turned off by setting
9744 Vinhibit_redisplay. */
9745 if (noninteractive
9746 || !NILP (Vinhibit_redisplay)
9747 || !f->glyphs_initialized_p)
9748 return;
9750 /* The flag redisplay_performed_directly_p is set by
9751 direct_output_for_insert when it already did the whole screen
9752 update necessary. */
9753 if (redisplay_performed_directly_p)
9755 redisplay_performed_directly_p = 0;
9756 if (!hscroll_windows (selected_window))
9757 return;
9760 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9761 if (popup_activated ())
9762 return;
9763 #endif
9765 /* I don't think this happens but let's be paranoid. */
9766 if (redisplaying_p)
9767 return;
9769 /* Record a function that resets redisplaying_p to its old value
9770 when we leave this function. */
9771 count = SPECPDL_INDEX ();
9772 record_unwind_protect (unwind_redisplay,
9773 Fcons (make_number (redisplaying_p), selected_frame));
9774 ++redisplaying_p;
9775 specbind (Qinhibit_free_realized_faces, Qnil);
9777 retry:
9778 pause = 0;
9779 reconsider_clip_changes (w, current_buffer);
9781 /* If new fonts have been loaded that make a glyph matrix adjustment
9782 necessary, do it. */
9783 if (fonts_changed_p)
9785 adjust_glyphs (NULL);
9786 ++windows_or_buffers_changed;
9787 fonts_changed_p = 0;
9790 /* If face_change_count is non-zero, init_iterator will free all
9791 realized faces, which includes the faces referenced from current
9792 matrices. So, we can't reuse current matrices in this case. */
9793 if (face_change_count)
9794 ++windows_or_buffers_changed;
9796 if (! FRAME_WINDOW_P (sf)
9797 && previous_terminal_frame != sf)
9799 /* Since frames on an ASCII terminal share the same display
9800 area, displaying a different frame means redisplay the whole
9801 thing. */
9802 windows_or_buffers_changed++;
9803 SET_FRAME_GARBAGED (sf);
9804 XSETFRAME (Vterminal_frame, sf);
9806 previous_terminal_frame = sf;
9808 /* Set the visible flags for all frames. Do this before checking
9809 for resized or garbaged frames; they want to know if their frames
9810 are visible. See the comment in frame.h for
9811 FRAME_SAMPLE_VISIBILITY. */
9813 Lisp_Object tail, frame;
9815 number_of_visible_frames = 0;
9817 FOR_EACH_FRAME (tail, frame)
9819 struct frame *f = XFRAME (frame);
9821 FRAME_SAMPLE_VISIBILITY (f);
9822 if (FRAME_VISIBLE_P (f))
9823 ++number_of_visible_frames;
9824 clear_desired_matrices (f);
9828 /* Notice any pending interrupt request to change frame size. */
9829 do_pending_window_change (1);
9831 /* Clear frames marked as garbaged. */
9832 if (frame_garbaged)
9833 clear_garbaged_frames ();
9835 /* Build menubar and tool-bar items. */
9836 prepare_menu_bars ();
9838 if (windows_or_buffers_changed)
9839 update_mode_lines++;
9841 /* Detect case that we need to write or remove a star in the mode line. */
9842 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9844 w->update_mode_line = Qt;
9845 if (buffer_shared > 1)
9846 update_mode_lines++;
9849 /* If %c is in the mode line, update it if needed. */
9850 if (!NILP (w->column_number_displayed)
9851 /* This alternative quickly identifies a common case
9852 where no change is needed. */
9853 && !(PT == XFASTINT (w->last_point)
9854 && XFASTINT (w->last_modified) >= MODIFF
9855 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9856 && (XFASTINT (w->column_number_displayed)
9857 != (int) current_column ())) /* iftc */
9858 w->update_mode_line = Qt;
9860 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9862 /* The variable buffer_shared is set in redisplay_window and
9863 indicates that we redisplay a buffer in different windows. See
9864 there. */
9865 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9866 || cursor_type_changed);
9868 /* If specs for an arrow have changed, do thorough redisplay
9869 to ensure we remove any arrow that should no longer exist. */
9870 if (overlay_arrows_changed_p ())
9871 consider_all_windows_p = windows_or_buffers_changed = 1;
9873 /* Normally the message* functions will have already displayed and
9874 updated the echo area, but the frame may have been trashed, or
9875 the update may have been preempted, so display the echo area
9876 again here. Checking message_cleared_p captures the case that
9877 the echo area should be cleared. */
9878 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9879 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9880 || (message_cleared_p
9881 && minibuf_level == 0
9882 /* If the mini-window is currently selected, this means the
9883 echo-area doesn't show through. */
9884 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9886 int window_height_changed_p = echo_area_display (0);
9887 must_finish = 1;
9889 /* If we don't display the current message, don't clear the
9890 message_cleared_p flag, because, if we did, we wouldn't clear
9891 the echo area in the next redisplay which doesn't preserve
9892 the echo area. */
9893 if (!display_last_displayed_message_p)
9894 message_cleared_p = 0;
9896 if (fonts_changed_p)
9897 goto retry;
9898 else if (window_height_changed_p)
9900 consider_all_windows_p = 1;
9901 ++update_mode_lines;
9902 ++windows_or_buffers_changed;
9904 /* If window configuration was changed, frames may have been
9905 marked garbaged. Clear them or we will experience
9906 surprises wrt scrolling. */
9907 if (frame_garbaged)
9908 clear_garbaged_frames ();
9911 else if (EQ (selected_window, minibuf_window)
9912 && (current_buffer->clip_changed
9913 || XFASTINT (w->last_modified) < MODIFF
9914 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9915 && resize_mini_window (w, 0))
9917 /* Resized active mini-window to fit the size of what it is
9918 showing if its contents might have changed. */
9919 must_finish = 1;
9920 consider_all_windows_p = 1;
9921 ++windows_or_buffers_changed;
9922 ++update_mode_lines;
9924 /* If window configuration was changed, frames may have been
9925 marked garbaged. Clear them or we will experience
9926 surprises wrt scrolling. */
9927 if (frame_garbaged)
9928 clear_garbaged_frames ();
9932 /* If showing the region, and mark has changed, we must redisplay
9933 the whole window. The assignment to this_line_start_pos prevents
9934 the optimization directly below this if-statement. */
9935 if (((!NILP (Vtransient_mark_mode)
9936 && !NILP (XBUFFER (w->buffer)->mark_active))
9937 != !NILP (w->region_showing))
9938 || (!NILP (w->region_showing)
9939 && !EQ (w->region_showing,
9940 Fmarker_position (XBUFFER (w->buffer)->mark))))
9941 CHARPOS (this_line_start_pos) = 0;
9943 /* Optimize the case that only the line containing the cursor in the
9944 selected window has changed. Variables starting with this_ are
9945 set in display_line and record information about the line
9946 containing the cursor. */
9947 tlbufpos = this_line_start_pos;
9948 tlendpos = this_line_end_pos;
9949 if (!consider_all_windows_p
9950 && CHARPOS (tlbufpos) > 0
9951 && NILP (w->update_mode_line)
9952 && !current_buffer->clip_changed
9953 && !current_buffer->prevent_redisplay_optimizations_p
9954 && FRAME_VISIBLE_P (XFRAME (w->frame))
9955 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9956 /* Make sure recorded data applies to current buffer, etc. */
9957 && this_line_buffer == current_buffer
9958 && current_buffer == XBUFFER (w->buffer)
9959 && NILP (w->force_start)
9960 && NILP (w->optional_new_start)
9961 /* Point must be on the line that we have info recorded about. */
9962 && PT >= CHARPOS (tlbufpos)
9963 && PT <= Z - CHARPOS (tlendpos)
9964 /* All text outside that line, including its final newline,
9965 must be unchanged */
9966 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9967 CHARPOS (tlendpos)))
9969 if (CHARPOS (tlbufpos) > BEGV
9970 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9971 && (CHARPOS (tlbufpos) == ZV
9972 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9973 /* Former continuation line has disappeared by becoming empty */
9974 goto cancel;
9975 else if (XFASTINT (w->last_modified) < MODIFF
9976 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9977 || MINI_WINDOW_P (w))
9979 /* We have to handle the case of continuation around a
9980 wide-column character (See the comment in indent.c around
9981 line 885).
9983 For instance, in the following case:
9985 -------- Insert --------
9986 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9987 J_I_ ==> J_I_ `^^' are cursors.
9988 ^^ ^^
9989 -------- --------
9991 As we have to redraw the line above, we should goto cancel. */
9993 struct it it;
9994 int line_height_before = this_line_pixel_height;
9996 /* Note that start_display will handle the case that the
9997 line starting at tlbufpos is a continuation lines. */
9998 start_display (&it, w, tlbufpos);
10000 /* Implementation note: It this still necessary? */
10001 if (it.current_x != this_line_start_x)
10002 goto cancel;
10004 TRACE ((stderr, "trying display optimization 1\n"));
10005 w->cursor.vpos = -1;
10006 overlay_arrow_seen = 0;
10007 it.vpos = this_line_vpos;
10008 it.current_y = this_line_y;
10009 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10010 display_line (&it);
10012 /* If line contains point, is not continued,
10013 and ends at same distance from eob as before, we win */
10014 if (w->cursor.vpos >= 0
10015 /* Line is not continued, otherwise this_line_start_pos
10016 would have been set to 0 in display_line. */
10017 && CHARPOS (this_line_start_pos)
10018 /* Line ends as before. */
10019 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10020 /* Line has same height as before. Otherwise other lines
10021 would have to be shifted up or down. */
10022 && this_line_pixel_height == line_height_before)
10024 /* If this is not the window's last line, we must adjust
10025 the charstarts of the lines below. */
10026 if (it.current_y < it.last_visible_y)
10028 struct glyph_row *row
10029 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10030 int delta, delta_bytes;
10032 if (Z - CHARPOS (tlendpos) == ZV)
10034 /* This line ends at end of (accessible part of)
10035 buffer. There is no newline to count. */
10036 delta = (Z
10037 - CHARPOS (tlendpos)
10038 - MATRIX_ROW_START_CHARPOS (row));
10039 delta_bytes = (Z_BYTE
10040 - BYTEPOS (tlendpos)
10041 - MATRIX_ROW_START_BYTEPOS (row));
10043 else
10045 /* This line ends in a newline. Must take
10046 account of the newline and the rest of the
10047 text that follows. */
10048 delta = (Z
10049 - CHARPOS (tlendpos)
10050 - MATRIX_ROW_START_CHARPOS (row));
10051 delta_bytes = (Z_BYTE
10052 - BYTEPOS (tlendpos)
10053 - MATRIX_ROW_START_BYTEPOS (row));
10056 increment_matrix_positions (w->current_matrix,
10057 this_line_vpos + 1,
10058 w->current_matrix->nrows,
10059 delta, delta_bytes);
10062 /* If this row displays text now but previously didn't,
10063 or vice versa, w->window_end_vpos may have to be
10064 adjusted. */
10065 if ((it.glyph_row - 1)->displays_text_p)
10067 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10068 XSETINT (w->window_end_vpos, this_line_vpos);
10070 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10071 && this_line_vpos > 0)
10072 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10073 w->window_end_valid = Qnil;
10075 /* Update hint: No need to try to scroll in update_window. */
10076 w->desired_matrix->no_scrolling_p = 1;
10078 #if GLYPH_DEBUG
10079 *w->desired_matrix->method = 0;
10080 debug_method_add (w, "optimization 1");
10081 #endif
10082 #ifdef HAVE_WINDOW_SYSTEM
10083 update_window_fringes (w, 0);
10084 #endif
10085 goto update;
10087 else
10088 goto cancel;
10090 else if (/* Cursor position hasn't changed. */
10091 PT == XFASTINT (w->last_point)
10092 /* Make sure the cursor was last displayed
10093 in this window. Otherwise we have to reposition it. */
10094 && 0 <= w->cursor.vpos
10095 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10097 if (!must_finish)
10099 do_pending_window_change (1);
10101 /* We used to always goto end_of_redisplay here, but this
10102 isn't enough if we have a blinking cursor. */
10103 if (w->cursor_off_p == w->last_cursor_off_p)
10104 goto end_of_redisplay;
10106 goto update;
10108 /* If highlighting the region, or if the cursor is in the echo area,
10109 then we can't just move the cursor. */
10110 else if (! (!NILP (Vtransient_mark_mode)
10111 && !NILP (current_buffer->mark_active))
10112 && (EQ (selected_window, current_buffer->last_selected_window)
10113 || highlight_nonselected_windows)
10114 && NILP (w->region_showing)
10115 && NILP (Vshow_trailing_whitespace)
10116 && !cursor_in_echo_area)
10118 struct it it;
10119 struct glyph_row *row;
10121 /* Skip from tlbufpos to PT and see where it is. Note that
10122 PT may be in invisible text. If so, we will end at the
10123 next visible position. */
10124 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10125 NULL, DEFAULT_FACE_ID);
10126 it.current_x = this_line_start_x;
10127 it.current_y = this_line_y;
10128 it.vpos = this_line_vpos;
10130 /* The call to move_it_to stops in front of PT, but
10131 moves over before-strings. */
10132 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10134 if (it.vpos == this_line_vpos
10135 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10136 row->enabled_p))
10138 xassert (this_line_vpos == it.vpos);
10139 xassert (this_line_y == it.current_y);
10140 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10141 #if GLYPH_DEBUG
10142 *w->desired_matrix->method = 0;
10143 debug_method_add (w, "optimization 3");
10144 #endif
10145 goto update;
10147 else
10148 goto cancel;
10151 cancel:
10152 /* Text changed drastically or point moved off of line. */
10153 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10156 CHARPOS (this_line_start_pos) = 0;
10157 consider_all_windows_p |= buffer_shared > 1;
10158 ++clear_face_cache_count;
10161 /* Build desired matrices, and update the display. If
10162 consider_all_windows_p is non-zero, do it for all windows on all
10163 frames. Otherwise do it for selected_window, only. */
10165 if (consider_all_windows_p)
10167 Lisp_Object tail, frame;
10168 int i, n = 0, size = 50;
10169 struct frame **updated
10170 = (struct frame **) alloca (size * sizeof *updated);
10172 /* Clear the face cache eventually. */
10173 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10175 clear_face_cache (0);
10176 clear_face_cache_count = 0;
10179 /* Recompute # windows showing selected buffer. This will be
10180 incremented each time such a window is displayed. */
10181 buffer_shared = 0;
10183 FOR_EACH_FRAME (tail, frame)
10185 struct frame *f = XFRAME (frame);
10187 if (FRAME_WINDOW_P (f) || f == sf)
10189 if (! EQ (frame, selected_frame))
10190 /* Select the frame, for the sake of frame-local
10191 variables. */
10192 select_frame_for_redisplay (frame);
10194 #ifdef HAVE_WINDOW_SYSTEM
10195 if (clear_face_cache_count % 50 == 0
10196 && FRAME_WINDOW_P (f))
10197 clear_image_cache (f, 0);
10198 #endif /* HAVE_WINDOW_SYSTEM */
10200 /* Mark all the scroll bars to be removed; we'll redeem
10201 the ones we want when we redisplay their windows. */
10202 if (condemn_scroll_bars_hook)
10203 condemn_scroll_bars_hook (f);
10205 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10206 redisplay_windows (FRAME_ROOT_WINDOW (f));
10208 /* Any scroll bars which redisplay_windows should have
10209 nuked should now go away. */
10210 if (judge_scroll_bars_hook)
10211 judge_scroll_bars_hook (f);
10213 /* If fonts changed, display again. */
10214 /* ??? rms: I suspect it is a mistake to jump all the way
10215 back to retry here. It should just retry this frame. */
10216 if (fonts_changed_p)
10217 goto retry;
10219 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10221 /* See if we have to hscroll. */
10222 if (hscroll_windows (f->root_window))
10223 goto retry;
10225 /* Prevent various kinds of signals during display
10226 update. stdio is not robust about handling
10227 signals, which can cause an apparent I/O
10228 error. */
10229 if (interrupt_input)
10230 unrequest_sigio ();
10231 STOP_POLLING;
10233 /* Update the display. */
10234 set_window_update_flags (XWINDOW (f->root_window), 1);
10235 pause |= update_frame (f, 0, 0);
10236 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10237 if (pause)
10238 break;
10239 #endif
10241 if (n == size)
10243 int nbytes = size * sizeof *updated;
10244 struct frame **p = (struct frame **) alloca (2 * nbytes);
10245 bcopy (updated, p, nbytes);
10246 size *= 2;
10249 updated[n++] = f;
10254 if (!pause)
10256 /* Do the mark_window_display_accurate after all windows have
10257 been redisplayed because this call resets flags in buffers
10258 which are needed for proper redisplay. */
10259 for (i = 0; i < n; ++i)
10261 struct frame *f = updated[i];
10262 mark_window_display_accurate (f->root_window, 1);
10263 if (frame_up_to_date_hook)
10264 frame_up_to_date_hook (f);
10268 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10270 Lisp_Object mini_window;
10271 struct frame *mini_frame;
10273 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10274 /* Use list_of_error, not Qerror, so that
10275 we catch only errors and don't run the debugger. */
10276 internal_condition_case_1 (redisplay_window_1, selected_window,
10277 list_of_error,
10278 redisplay_window_error);
10280 /* Compare desired and current matrices, perform output. */
10282 update:
10283 /* If fonts changed, display again. */
10284 if (fonts_changed_p)
10285 goto retry;
10287 /* Prevent various kinds of signals during display update.
10288 stdio is not robust about handling signals,
10289 which can cause an apparent I/O error. */
10290 if (interrupt_input)
10291 unrequest_sigio ();
10292 STOP_POLLING;
10294 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10296 if (hscroll_windows (selected_window))
10297 goto retry;
10299 XWINDOW (selected_window)->must_be_updated_p = 1;
10300 pause = update_frame (sf, 0, 0);
10303 /* We may have called echo_area_display at the top of this
10304 function. If the echo area is on another frame, that may
10305 have put text on a frame other than the selected one, so the
10306 above call to update_frame would not have caught it. Catch
10307 it here. */
10308 mini_window = FRAME_MINIBUF_WINDOW (sf);
10309 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10311 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10313 XWINDOW (mini_window)->must_be_updated_p = 1;
10314 pause |= update_frame (mini_frame, 0, 0);
10315 if (!pause && hscroll_windows (mini_window))
10316 goto retry;
10320 /* If display was paused because of pending input, make sure we do a
10321 thorough update the next time. */
10322 if (pause)
10324 /* Prevent the optimization at the beginning of
10325 redisplay_internal that tries a single-line update of the
10326 line containing the cursor in the selected window. */
10327 CHARPOS (this_line_start_pos) = 0;
10329 /* Let the overlay arrow be updated the next time. */
10330 update_overlay_arrows (0);
10332 /* If we pause after scrolling, some rows in the current
10333 matrices of some windows are not valid. */
10334 if (!WINDOW_FULL_WIDTH_P (w)
10335 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10336 update_mode_lines = 1;
10338 else
10340 if (!consider_all_windows_p)
10342 /* This has already been done above if
10343 consider_all_windows_p is set. */
10344 mark_window_display_accurate_1 (w, 1);
10346 /* Say overlay arrows are up to date. */
10347 update_overlay_arrows (1);
10349 if (frame_up_to_date_hook != 0)
10350 frame_up_to_date_hook (sf);
10353 update_mode_lines = 0;
10354 windows_or_buffers_changed = 0;
10355 cursor_type_changed = 0;
10358 /* Start SIGIO interrupts coming again. Having them off during the
10359 code above makes it less likely one will discard output, but not
10360 impossible, since there might be stuff in the system buffer here.
10361 But it is much hairier to try to do anything about that. */
10362 if (interrupt_input)
10363 request_sigio ();
10364 RESUME_POLLING;
10366 /* If a frame has become visible which was not before, redisplay
10367 again, so that we display it. Expose events for such a frame
10368 (which it gets when becoming visible) don't call the parts of
10369 redisplay constructing glyphs, so simply exposing a frame won't
10370 display anything in this case. So, we have to display these
10371 frames here explicitly. */
10372 if (!pause)
10374 Lisp_Object tail, frame;
10375 int new_count = 0;
10377 FOR_EACH_FRAME (tail, frame)
10379 int this_is_visible = 0;
10381 if (XFRAME (frame)->visible)
10382 this_is_visible = 1;
10383 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10384 if (XFRAME (frame)->visible)
10385 this_is_visible = 1;
10387 if (this_is_visible)
10388 new_count++;
10391 if (new_count != number_of_visible_frames)
10392 windows_or_buffers_changed++;
10395 /* Change frame size now if a change is pending. */
10396 do_pending_window_change (1);
10398 /* If we just did a pending size change, or have additional
10399 visible frames, redisplay again. */
10400 if (windows_or_buffers_changed && !pause)
10401 goto retry;
10403 end_of_redisplay:
10404 unbind_to (count, Qnil);
10405 RESUME_POLLING;
10409 /* Redisplay, but leave alone any recent echo area message unless
10410 another message has been requested in its place.
10412 This is useful in situations where you need to redisplay but no
10413 user action has occurred, making it inappropriate for the message
10414 area to be cleared. See tracking_off and
10415 wait_reading_process_output for examples of these situations.
10417 FROM_WHERE is an integer saying from where this function was
10418 called. This is useful for debugging. */
10420 void
10421 redisplay_preserve_echo_area (from_where)
10422 int from_where;
10424 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10426 if (!NILP (echo_area_buffer[1]))
10428 /* We have a previously displayed message, but no current
10429 message. Redisplay the previous message. */
10430 display_last_displayed_message_p = 1;
10431 redisplay_internal (1);
10432 display_last_displayed_message_p = 0;
10434 else
10435 redisplay_internal (1);
10437 if (rif != NULL && rif->flush_display_optional)
10438 rif->flush_display_optional (NULL);
10442 /* Function registered with record_unwind_protect in
10443 redisplay_internal. Reset redisplaying_p to the value it had
10444 before redisplay_internal was called, and clear
10445 prevent_freeing_realized_faces_p. It also selects the previously
10446 selected frame. */
10448 static Lisp_Object
10449 unwind_redisplay (val)
10450 Lisp_Object val;
10452 Lisp_Object old_redisplaying_p, old_frame;
10454 old_redisplaying_p = XCAR (val);
10455 redisplaying_p = XFASTINT (old_redisplaying_p);
10456 old_frame = XCDR (val);
10457 if (! EQ (old_frame, selected_frame))
10458 select_frame_for_redisplay (old_frame);
10459 return Qnil;
10463 /* Mark the display of window W as accurate or inaccurate. If
10464 ACCURATE_P is non-zero mark display of W as accurate. If
10465 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10466 redisplay_internal is called. */
10468 static void
10469 mark_window_display_accurate_1 (w, accurate_p)
10470 struct window *w;
10471 int accurate_p;
10473 if (BUFFERP (w->buffer))
10475 struct buffer *b = XBUFFER (w->buffer);
10477 w->last_modified
10478 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10479 w->last_overlay_modified
10480 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10481 w->last_had_star
10482 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10484 if (accurate_p)
10486 b->clip_changed = 0;
10487 b->prevent_redisplay_optimizations_p = 0;
10489 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10490 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10491 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10492 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10494 w->current_matrix->buffer = b;
10495 w->current_matrix->begv = BUF_BEGV (b);
10496 w->current_matrix->zv = BUF_ZV (b);
10498 w->last_cursor = w->cursor;
10499 w->last_cursor_off_p = w->cursor_off_p;
10501 if (w == XWINDOW (selected_window))
10502 w->last_point = make_number (BUF_PT (b));
10503 else
10504 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10508 if (accurate_p)
10510 w->window_end_valid = w->buffer;
10511 #if 0 /* This is incorrect with variable-height lines. */
10512 xassert (XINT (w->window_end_vpos)
10513 < (WINDOW_TOTAL_LINES (w)
10514 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10515 #endif
10516 w->update_mode_line = Qnil;
10521 /* Mark the display of windows in the window tree rooted at WINDOW as
10522 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10523 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10524 be redisplayed the next time redisplay_internal is called. */
10526 void
10527 mark_window_display_accurate (window, accurate_p)
10528 Lisp_Object window;
10529 int accurate_p;
10531 struct window *w;
10533 for (; !NILP (window); window = w->next)
10535 w = XWINDOW (window);
10536 mark_window_display_accurate_1 (w, accurate_p);
10538 if (!NILP (w->vchild))
10539 mark_window_display_accurate (w->vchild, accurate_p);
10540 if (!NILP (w->hchild))
10541 mark_window_display_accurate (w->hchild, accurate_p);
10544 if (accurate_p)
10546 update_overlay_arrows (1);
10548 else
10550 /* Force a thorough redisplay the next time by setting
10551 last_arrow_position and last_arrow_string to t, which is
10552 unequal to any useful value of Voverlay_arrow_... */
10553 update_overlay_arrows (-1);
10558 /* Return value in display table DP (Lisp_Char_Table *) for character
10559 C. Since a display table doesn't have any parent, we don't have to
10560 follow parent. Do not call this function directly but use the
10561 macro DISP_CHAR_VECTOR. */
10563 Lisp_Object
10564 disp_char_vector (dp, c)
10565 struct Lisp_Char_Table *dp;
10566 int c;
10568 int code[4], i;
10569 Lisp_Object val;
10571 if (SINGLE_BYTE_CHAR_P (c))
10572 return (dp->contents[c]);
10574 SPLIT_CHAR (c, code[0], code[1], code[2]);
10575 if (code[1] < 32)
10576 code[1] = -1;
10577 else if (code[2] < 32)
10578 code[2] = -1;
10580 /* Here, the possible range of code[0] (== charset ID) is
10581 128..max_charset. Since the top level char table contains data
10582 for multibyte characters after 256th element, we must increment
10583 code[0] by 128 to get a correct index. */
10584 code[0] += 128;
10585 code[3] = -1; /* anchor */
10587 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10589 val = dp->contents[code[i]];
10590 if (!SUB_CHAR_TABLE_P (val))
10591 return (NILP (val) ? dp->defalt : val);
10594 /* Here, val is a sub char table. We return the default value of
10595 it. */
10596 return (dp->defalt);
10601 /***********************************************************************
10602 Window Redisplay
10603 ***********************************************************************/
10605 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10607 static void
10608 redisplay_windows (window)
10609 Lisp_Object window;
10611 while (!NILP (window))
10613 struct window *w = XWINDOW (window);
10615 if (!NILP (w->hchild))
10616 redisplay_windows (w->hchild);
10617 else if (!NILP (w->vchild))
10618 redisplay_windows (w->vchild);
10619 else
10621 displayed_buffer = XBUFFER (w->buffer);
10622 /* Use list_of_error, not Qerror, so that
10623 we catch only errors and don't run the debugger. */
10624 internal_condition_case_1 (redisplay_window_0, window,
10625 list_of_error,
10626 redisplay_window_error);
10629 window = w->next;
10633 static Lisp_Object
10634 redisplay_window_error ()
10636 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10637 return Qnil;
10640 static Lisp_Object
10641 redisplay_window_0 (window)
10642 Lisp_Object window;
10644 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10645 redisplay_window (window, 0);
10646 return Qnil;
10649 static Lisp_Object
10650 redisplay_window_1 (window)
10651 Lisp_Object window;
10653 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10654 redisplay_window (window, 1);
10655 return Qnil;
10659 /* Increment GLYPH until it reaches END or CONDITION fails while
10660 adding (GLYPH)->pixel_width to X. */
10662 #define SKIP_GLYPHS(glyph, end, x, condition) \
10663 do \
10665 (x) += (glyph)->pixel_width; \
10666 ++(glyph); \
10668 while ((glyph) < (end) && (condition))
10671 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10672 DELTA is the number of bytes by which positions recorded in ROW
10673 differ from current buffer positions. */
10675 void
10676 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10677 struct window *w;
10678 struct glyph_row *row;
10679 struct glyph_matrix *matrix;
10680 int delta, delta_bytes, dy, dvpos;
10682 struct glyph *glyph = row->glyphs[TEXT_AREA];
10683 struct glyph *end = glyph + row->used[TEXT_AREA];
10684 struct glyph *cursor = NULL;
10685 /* The first glyph that starts a sequence of glyphs from string. */
10686 struct glyph *string_start;
10687 /* The X coordinate of string_start. */
10688 int string_start_x;
10689 /* The last known character position. */
10690 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10691 /* The last known character position before string_start. */
10692 int string_before_pos;
10693 int x = row->x;
10694 int cursor_x = x;
10695 int cursor_from_overlay_pos = 0;
10696 int pt_old = PT - delta;
10698 /* Skip over glyphs not having an object at the start of the row.
10699 These are special glyphs like truncation marks on terminal
10700 frames. */
10701 if (row->displays_text_p)
10702 while (glyph < end
10703 && INTEGERP (glyph->object)
10704 && glyph->charpos < 0)
10706 x += glyph->pixel_width;
10707 ++glyph;
10710 string_start = NULL;
10711 while (glyph < end
10712 && !INTEGERP (glyph->object)
10713 && (!BUFFERP (glyph->object)
10714 || (last_pos = glyph->charpos) < pt_old))
10716 if (! STRINGP (glyph->object))
10718 string_start = NULL;
10719 x += glyph->pixel_width;
10720 ++glyph;
10721 if (cursor_from_overlay_pos
10722 && last_pos > cursor_from_overlay_pos)
10724 cursor_from_overlay_pos = 0;
10725 cursor = 0;
10728 else
10730 string_before_pos = last_pos;
10731 string_start = glyph;
10732 string_start_x = x;
10733 /* Skip all glyphs from string. */
10736 int pos;
10737 if ((cursor == NULL || glyph > cursor)
10738 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10739 Qcursor, (glyph)->object))
10740 && (pos = string_buffer_position (w, glyph->object,
10741 string_before_pos),
10742 (pos == 0 /* From overlay */
10743 || pos == pt_old)))
10745 /* Estimate overlay buffer position from the buffer
10746 positions of the glyphs before and after the overlay.
10747 Add 1 to last_pos so that if point corresponds to the
10748 glyph right after the overlay, we still use a 'cursor'
10749 property found in that overlay. */
10750 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10751 cursor = glyph;
10752 cursor_x = x;
10754 x += glyph->pixel_width;
10755 ++glyph;
10757 while (glyph < end && STRINGP (glyph->object));
10761 if (cursor != NULL)
10763 glyph = cursor;
10764 x = cursor_x;
10766 else if (string_start
10767 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10769 /* We may have skipped over point because the previous glyphs
10770 are from string. As there's no easy way to know the
10771 character position of the current glyph, find the correct
10772 glyph on point by scanning from string_start again. */
10773 Lisp_Object limit;
10774 Lisp_Object string;
10775 int pos;
10777 limit = make_number (pt_old + 1);
10778 end = glyph;
10779 glyph = string_start;
10780 x = string_start_x;
10781 string = glyph->object;
10782 pos = string_buffer_position (w, string, string_before_pos);
10783 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10784 because we always put cursor after overlay strings. */
10785 while (pos == 0 && glyph < end)
10787 string = glyph->object;
10788 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10789 if (glyph < end)
10790 pos = string_buffer_position (w, glyph->object, string_before_pos);
10793 while (glyph < end)
10795 pos = XINT (Fnext_single_char_property_change
10796 (make_number (pos), Qdisplay, Qnil, limit));
10797 if (pos > pt_old)
10798 break;
10799 /* Skip glyphs from the same string. */
10800 string = glyph->object;
10801 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10802 /* Skip glyphs from an overlay. */
10803 while (glyph < end
10804 && ! string_buffer_position (w, glyph->object, pos))
10806 string = glyph->object;
10807 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10812 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10813 w->cursor.x = x;
10814 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10815 w->cursor.y = row->y + dy;
10817 if (w == XWINDOW (selected_window))
10819 if (!row->continued_p
10820 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10821 && row->x == 0)
10823 this_line_buffer = XBUFFER (w->buffer);
10825 CHARPOS (this_line_start_pos)
10826 = MATRIX_ROW_START_CHARPOS (row) + delta;
10827 BYTEPOS (this_line_start_pos)
10828 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10830 CHARPOS (this_line_end_pos)
10831 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10832 BYTEPOS (this_line_end_pos)
10833 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10835 this_line_y = w->cursor.y;
10836 this_line_pixel_height = row->height;
10837 this_line_vpos = w->cursor.vpos;
10838 this_line_start_x = row->x;
10840 else
10841 CHARPOS (this_line_start_pos) = 0;
10846 /* Run window scroll functions, if any, for WINDOW with new window
10847 start STARTP. Sets the window start of WINDOW to that position.
10849 We assume that the window's buffer is really current. */
10851 static INLINE struct text_pos
10852 run_window_scroll_functions (window, startp)
10853 Lisp_Object window;
10854 struct text_pos startp;
10856 struct window *w = XWINDOW (window);
10857 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10859 if (current_buffer != XBUFFER (w->buffer))
10860 abort ();
10862 if (!NILP (Vwindow_scroll_functions))
10864 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10865 make_number (CHARPOS (startp)));
10866 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10867 /* In case the hook functions switch buffers. */
10868 if (current_buffer != XBUFFER (w->buffer))
10869 set_buffer_internal_1 (XBUFFER (w->buffer));
10872 return startp;
10876 /* Make sure the line containing the cursor is fully visible.
10877 A value of 1 means there is nothing to be done.
10878 (Either the line is fully visible, or it cannot be made so,
10879 or we cannot tell.)
10881 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10882 is higher than window.
10884 A value of 0 means the caller should do scrolling
10885 as if point had gone off the screen. */
10887 static int
10888 make_cursor_line_fully_visible (w, force_p)
10889 struct window *w;
10890 int force_p;
10892 struct glyph_matrix *matrix;
10893 struct glyph_row *row;
10894 int window_height;
10896 if (!make_cursor_line_fully_visible_p)
10897 return 1;
10899 /* It's not always possible to find the cursor, e.g, when a window
10900 is full of overlay strings. Don't do anything in that case. */
10901 if (w->cursor.vpos < 0)
10902 return 1;
10904 matrix = w->desired_matrix;
10905 row = MATRIX_ROW (matrix, w->cursor.vpos);
10907 /* If the cursor row is not partially visible, there's nothing to do. */
10908 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10909 return 1;
10911 /* If the row the cursor is in is taller than the window's height,
10912 it's not clear what to do, so do nothing. */
10913 window_height = window_box_height (w);
10914 if (row->height >= window_height)
10916 if (!force_p || w->vscroll)
10917 return 1;
10919 return 0;
10921 #if 0
10922 /* This code used to try to scroll the window just enough to make
10923 the line visible. It returned 0 to say that the caller should
10924 allocate larger glyph matrices. */
10926 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10928 int dy = row->height - row->visible_height;
10929 w->vscroll = 0;
10930 w->cursor.y += dy;
10931 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10933 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10935 int dy = - (row->height - row->visible_height);
10936 w->vscroll = dy;
10937 w->cursor.y += dy;
10938 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10941 /* When we change the cursor y-position of the selected window,
10942 change this_line_y as well so that the display optimization for
10943 the cursor line of the selected window in redisplay_internal uses
10944 the correct y-position. */
10945 if (w == XWINDOW (selected_window))
10946 this_line_y = w->cursor.y;
10948 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10949 redisplay with larger matrices. */
10950 if (matrix->nrows < required_matrix_height (w))
10952 fonts_changed_p = 1;
10953 return 0;
10956 return 1;
10957 #endif /* 0 */
10961 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10962 non-zero means only WINDOW is redisplayed in redisplay_internal.
10963 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10964 in redisplay_window to bring a partially visible line into view in
10965 the case that only the cursor has moved.
10967 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10968 last screen line's vertical height extends past the end of the screen.
10970 Value is
10972 1 if scrolling succeeded
10974 0 if scrolling didn't find point.
10976 -1 if new fonts have been loaded so that we must interrupt
10977 redisplay, adjust glyph matrices, and try again. */
10979 enum
10981 SCROLLING_SUCCESS,
10982 SCROLLING_FAILED,
10983 SCROLLING_NEED_LARGER_MATRICES
10986 static int
10987 try_scrolling (window, just_this_one_p, scroll_conservatively,
10988 scroll_step, temp_scroll_step, last_line_misfit)
10989 Lisp_Object window;
10990 int just_this_one_p;
10991 EMACS_INT scroll_conservatively, scroll_step;
10992 int temp_scroll_step;
10993 int last_line_misfit;
10995 struct window *w = XWINDOW (window);
10996 struct frame *f = XFRAME (w->frame);
10997 struct text_pos scroll_margin_pos;
10998 struct text_pos pos;
10999 struct text_pos startp;
11000 struct it it;
11001 Lisp_Object window_end;
11002 int this_scroll_margin;
11003 int dy = 0;
11004 int scroll_max;
11005 int rc;
11006 int amount_to_scroll = 0;
11007 Lisp_Object aggressive;
11008 int height;
11009 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11011 #if GLYPH_DEBUG
11012 debug_method_add (w, "try_scrolling");
11013 #endif
11015 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11017 /* Compute scroll margin height in pixels. We scroll when point is
11018 within this distance from the top or bottom of the window. */
11019 if (scroll_margin > 0)
11021 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11022 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11024 else
11025 this_scroll_margin = 0;
11027 /* Force scroll_conservatively to have a reasonable value so it doesn't
11028 cause an overflow while computing how much to scroll. */
11029 if (scroll_conservatively)
11030 scroll_conservatively = min (scroll_conservatively,
11031 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11033 /* Compute how much we should try to scroll maximally to bring point
11034 into view. */
11035 if (scroll_step || scroll_conservatively || temp_scroll_step)
11036 scroll_max = max (scroll_step,
11037 max (scroll_conservatively, temp_scroll_step));
11038 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11039 || NUMBERP (current_buffer->scroll_up_aggressively))
11040 /* We're trying to scroll because of aggressive scrolling
11041 but no scroll_step is set. Choose an arbitrary one. Maybe
11042 there should be a variable for this. */
11043 scroll_max = 10;
11044 else
11045 scroll_max = 0;
11046 scroll_max *= FRAME_LINE_HEIGHT (f);
11048 /* Decide whether we have to scroll down. Start at the window end
11049 and move this_scroll_margin up to find the position of the scroll
11050 margin. */
11051 window_end = Fwindow_end (window, Qt);
11053 too_near_end:
11055 CHARPOS (scroll_margin_pos) = XINT (window_end);
11056 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11058 if (this_scroll_margin || extra_scroll_margin_lines)
11060 start_display (&it, w, scroll_margin_pos);
11061 if (this_scroll_margin)
11062 move_it_vertically_backward (&it, this_scroll_margin);
11063 if (extra_scroll_margin_lines)
11064 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11065 scroll_margin_pos = it.current.pos;
11068 if (PT >= CHARPOS (scroll_margin_pos))
11070 int y0;
11072 /* Point is in the scroll margin at the bottom of the window, or
11073 below. Compute a new window start that makes point visible. */
11075 /* Compute the distance from the scroll margin to PT.
11076 Give up if the distance is greater than scroll_max. */
11077 start_display (&it, w, scroll_margin_pos);
11078 y0 = it.current_y;
11079 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11080 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11082 /* To make point visible, we have to move the window start
11083 down so that the line the cursor is in is visible, which
11084 means we have to add in the height of the cursor line. */
11085 dy = line_bottom_y (&it) - y0;
11087 if (dy > scroll_max)
11088 return SCROLLING_FAILED;
11090 /* Move the window start down. If scrolling conservatively,
11091 move it just enough down to make point visible. If
11092 scroll_step is set, move it down by scroll_step. */
11093 start_display (&it, w, startp);
11095 if (scroll_conservatively)
11096 /* Set AMOUNT_TO_SCROLL to at least one line,
11097 and at most scroll_conservatively lines. */
11098 amount_to_scroll
11099 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11100 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11101 else if (scroll_step || temp_scroll_step)
11102 amount_to_scroll = scroll_max;
11103 else
11105 aggressive = current_buffer->scroll_up_aggressively;
11106 height = WINDOW_BOX_TEXT_HEIGHT (w);
11107 if (NUMBERP (aggressive))
11109 double float_amount = XFLOATINT (aggressive) * height;
11110 amount_to_scroll = float_amount;
11111 if (amount_to_scroll == 0 && float_amount > 0)
11112 amount_to_scroll = 1;
11116 if (amount_to_scroll <= 0)
11117 return SCROLLING_FAILED;
11119 /* If moving by amount_to_scroll leaves STARTP unchanged,
11120 move it down one screen line. */
11122 move_it_vertically (&it, amount_to_scroll);
11123 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11124 move_it_by_lines (&it, 1, 1);
11125 startp = it.current.pos;
11127 else
11129 /* See if point is inside the scroll margin at the top of the
11130 window. */
11131 scroll_margin_pos = startp;
11132 if (this_scroll_margin)
11134 start_display (&it, w, startp);
11135 move_it_vertically (&it, this_scroll_margin);
11136 scroll_margin_pos = it.current.pos;
11139 if (PT < CHARPOS (scroll_margin_pos))
11141 /* Point is in the scroll margin at the top of the window or
11142 above what is displayed in the window. */
11143 int y0;
11145 /* Compute the vertical distance from PT to the scroll
11146 margin position. Give up if distance is greater than
11147 scroll_max. */
11148 SET_TEXT_POS (pos, PT, PT_BYTE);
11149 start_display (&it, w, pos);
11150 y0 = it.current_y;
11151 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11152 it.last_visible_y, -1,
11153 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11154 dy = it.current_y - y0;
11155 if (dy > scroll_max)
11156 return SCROLLING_FAILED;
11158 /* Compute new window start. */
11159 start_display (&it, w, startp);
11161 if (scroll_conservatively)
11162 amount_to_scroll
11163 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11164 else if (scroll_step || temp_scroll_step)
11165 amount_to_scroll = scroll_max;
11166 else
11168 aggressive = current_buffer->scroll_down_aggressively;
11169 height = WINDOW_BOX_TEXT_HEIGHT (w);
11170 if (NUMBERP (aggressive))
11172 double float_amount = XFLOATINT (aggressive) * height;
11173 amount_to_scroll = float_amount;
11174 if (amount_to_scroll == 0 && float_amount > 0)
11175 amount_to_scroll = 1;
11179 if (amount_to_scroll <= 0)
11180 return SCROLLING_FAILED;
11182 move_it_vertically_backward (&it, amount_to_scroll);
11183 startp = it.current.pos;
11187 /* Run window scroll functions. */
11188 startp = run_window_scroll_functions (window, startp);
11190 /* Display the window. Give up if new fonts are loaded, or if point
11191 doesn't appear. */
11192 if (!try_window (window, startp))
11193 rc = SCROLLING_NEED_LARGER_MATRICES;
11194 else if (w->cursor.vpos < 0)
11196 clear_glyph_matrix (w->desired_matrix);
11197 rc = SCROLLING_FAILED;
11199 else
11201 /* Maybe forget recorded base line for line number display. */
11202 if (!just_this_one_p
11203 || current_buffer->clip_changed
11204 || BEG_UNCHANGED < CHARPOS (startp))
11205 w->base_line_number = Qnil;
11207 /* If cursor ends up on a partially visible line,
11208 treat that as being off the bottom of the screen. */
11209 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11211 clear_glyph_matrix (w->desired_matrix);
11212 ++extra_scroll_margin_lines;
11213 goto too_near_end;
11215 rc = SCROLLING_SUCCESS;
11218 return rc;
11222 /* Compute a suitable window start for window W if display of W starts
11223 on a continuation line. Value is non-zero if a new window start
11224 was computed.
11226 The new window start will be computed, based on W's width, starting
11227 from the start of the continued line. It is the start of the
11228 screen line with the minimum distance from the old start W->start. */
11230 static int
11231 compute_window_start_on_continuation_line (w)
11232 struct window *w;
11234 struct text_pos pos, start_pos;
11235 int window_start_changed_p = 0;
11237 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11239 /* If window start is on a continuation line... Window start may be
11240 < BEGV in case there's invisible text at the start of the
11241 buffer (M-x rmail, for example). */
11242 if (CHARPOS (start_pos) > BEGV
11243 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11245 struct it it;
11246 struct glyph_row *row;
11248 /* Handle the case that the window start is out of range. */
11249 if (CHARPOS (start_pos) < BEGV)
11250 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11251 else if (CHARPOS (start_pos) > ZV)
11252 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11254 /* Find the start of the continued line. This should be fast
11255 because scan_buffer is fast (newline cache). */
11256 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11257 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11258 row, DEFAULT_FACE_ID);
11259 reseat_at_previous_visible_line_start (&it);
11261 /* If the line start is "too far" away from the window start,
11262 say it takes too much time to compute a new window start. */
11263 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11264 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11266 int min_distance, distance;
11268 /* Move forward by display lines to find the new window
11269 start. If window width was enlarged, the new start can
11270 be expected to be > the old start. If window width was
11271 decreased, the new window start will be < the old start.
11272 So, we're looking for the display line start with the
11273 minimum distance from the old window start. */
11274 pos = it.current.pos;
11275 min_distance = INFINITY;
11276 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11277 distance < min_distance)
11279 min_distance = distance;
11280 pos = it.current.pos;
11281 move_it_by_lines (&it, 1, 0);
11284 /* Set the window start there. */
11285 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11286 window_start_changed_p = 1;
11290 return window_start_changed_p;
11294 /* Try cursor movement in case text has not changed in window WINDOW,
11295 with window start STARTP. Value is
11297 CURSOR_MOVEMENT_SUCCESS if successful
11299 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11301 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11302 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11303 we want to scroll as if scroll-step were set to 1. See the code.
11305 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11306 which case we have to abort this redisplay, and adjust matrices
11307 first. */
11309 enum
11311 CURSOR_MOVEMENT_SUCCESS,
11312 CURSOR_MOVEMENT_CANNOT_BE_USED,
11313 CURSOR_MOVEMENT_MUST_SCROLL,
11314 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11317 static int
11318 try_cursor_movement (window, startp, scroll_step)
11319 Lisp_Object window;
11320 struct text_pos startp;
11321 int *scroll_step;
11323 struct window *w = XWINDOW (window);
11324 struct frame *f = XFRAME (w->frame);
11325 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11327 #if GLYPH_DEBUG
11328 if (inhibit_try_cursor_movement)
11329 return rc;
11330 #endif
11332 /* Handle case where text has not changed, only point, and it has
11333 not moved off the frame. */
11334 if (/* Point may be in this window. */
11335 PT >= CHARPOS (startp)
11336 /* Selective display hasn't changed. */
11337 && !current_buffer->clip_changed
11338 /* Function force-mode-line-update is used to force a thorough
11339 redisplay. It sets either windows_or_buffers_changed or
11340 update_mode_lines. So don't take a shortcut here for these
11341 cases. */
11342 && !update_mode_lines
11343 && !windows_or_buffers_changed
11344 && !cursor_type_changed
11345 /* Can't use this case if highlighting a region. When a
11346 region exists, cursor movement has to do more than just
11347 set the cursor. */
11348 && !(!NILP (Vtransient_mark_mode)
11349 && !NILP (current_buffer->mark_active))
11350 && NILP (w->region_showing)
11351 && NILP (Vshow_trailing_whitespace)
11352 /* Right after splitting windows, last_point may be nil. */
11353 && INTEGERP (w->last_point)
11354 /* This code is not used for mini-buffer for the sake of the case
11355 of redisplaying to replace an echo area message; since in
11356 that case the mini-buffer contents per se are usually
11357 unchanged. This code is of no real use in the mini-buffer
11358 since the handling of this_line_start_pos, etc., in redisplay
11359 handles the same cases. */
11360 && !EQ (window, minibuf_window)
11361 /* When splitting windows or for new windows, it happens that
11362 redisplay is called with a nil window_end_vpos or one being
11363 larger than the window. This should really be fixed in
11364 window.c. I don't have this on my list, now, so we do
11365 approximately the same as the old redisplay code. --gerd. */
11366 && INTEGERP (w->window_end_vpos)
11367 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11368 && (FRAME_WINDOW_P (f)
11369 || !overlay_arrow_in_current_buffer_p ()))
11371 int this_scroll_margin, top_scroll_margin;
11372 struct glyph_row *row = NULL;
11374 #if GLYPH_DEBUG
11375 debug_method_add (w, "cursor movement");
11376 #endif
11378 /* Scroll if point within this distance from the top or bottom
11379 of the window. This is a pixel value. */
11380 this_scroll_margin = max (0, scroll_margin);
11381 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11382 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11384 top_scroll_margin = this_scroll_margin;
11385 if (WINDOW_WANTS_HEADER_LINE_P (w))
11386 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11388 /* Start with the row the cursor was displayed during the last
11389 not paused redisplay. Give up if that row is not valid. */
11390 if (w->last_cursor.vpos < 0
11391 || w->last_cursor.vpos >= w->current_matrix->nrows)
11392 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11393 else
11395 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11396 if (row->mode_line_p)
11397 ++row;
11398 if (!row->enabled_p)
11399 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11402 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11404 int scroll_p = 0;
11405 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11407 if (PT > XFASTINT (w->last_point))
11409 /* Point has moved forward. */
11410 while (MATRIX_ROW_END_CHARPOS (row) < PT
11411 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11413 xassert (row->enabled_p);
11414 ++row;
11417 /* The end position of a row equals the start position
11418 of the next row. If PT is there, we would rather
11419 display it in the next line. */
11420 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11421 && MATRIX_ROW_END_CHARPOS (row) == PT
11422 && !cursor_row_p (w, row))
11423 ++row;
11425 /* If within the scroll margin, scroll. Note that
11426 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11427 the next line would be drawn, and that
11428 this_scroll_margin can be zero. */
11429 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11430 || PT > MATRIX_ROW_END_CHARPOS (row)
11431 /* Line is completely visible last line in window
11432 and PT is to be set in the next line. */
11433 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11434 && PT == MATRIX_ROW_END_CHARPOS (row)
11435 && !row->ends_at_zv_p
11436 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11437 scroll_p = 1;
11439 else if (PT < XFASTINT (w->last_point))
11441 /* Cursor has to be moved backward. Note that PT >=
11442 CHARPOS (startp) because of the outer if-statement. */
11443 while (!row->mode_line_p
11444 && (MATRIX_ROW_START_CHARPOS (row) > PT
11445 || (MATRIX_ROW_START_CHARPOS (row) == PT
11446 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11447 && (row->y > top_scroll_margin
11448 || CHARPOS (startp) == BEGV))
11450 xassert (row->enabled_p);
11451 --row;
11454 /* Consider the following case: Window starts at BEGV,
11455 there is invisible, intangible text at BEGV, so that
11456 display starts at some point START > BEGV. It can
11457 happen that we are called with PT somewhere between
11458 BEGV and START. Try to handle that case. */
11459 if (row < w->current_matrix->rows
11460 || row->mode_line_p)
11462 row = w->current_matrix->rows;
11463 if (row->mode_line_p)
11464 ++row;
11467 /* Due to newlines in overlay strings, we may have to
11468 skip forward over overlay strings. */
11469 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11470 && MATRIX_ROW_END_CHARPOS (row) == PT
11471 && !cursor_row_p (w, row))
11472 ++row;
11474 /* If within the scroll margin, scroll. */
11475 if (row->y < top_scroll_margin
11476 && CHARPOS (startp) != BEGV)
11477 scroll_p = 1;
11480 if (PT < MATRIX_ROW_START_CHARPOS (row)
11481 || PT > MATRIX_ROW_END_CHARPOS (row))
11483 /* if PT is not in the glyph row, give up. */
11484 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11486 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11487 && make_cursor_line_fully_visible_p)
11489 if (PT == MATRIX_ROW_END_CHARPOS (row)
11490 && !row->ends_at_zv_p
11491 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11492 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11493 else if (row->height > window_box_height (w))
11495 /* If we end up in a partially visible line, let's
11496 make it fully visible, except when it's taller
11497 than the window, in which case we can't do much
11498 about it. */
11499 *scroll_step = 1;
11500 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11502 else
11504 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11505 if (!make_cursor_line_fully_visible (w, 0))
11506 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11507 else
11508 rc = CURSOR_MOVEMENT_SUCCESS;
11511 else if (scroll_p)
11512 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11513 else
11515 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11516 rc = CURSOR_MOVEMENT_SUCCESS;
11521 return rc;
11524 void
11525 set_vertical_scroll_bar (w)
11526 struct window *w;
11528 int start, end, whole;
11530 /* Calculate the start and end positions for the current window.
11531 At some point, it would be nice to choose between scrollbars
11532 which reflect the whole buffer size, with special markers
11533 indicating narrowing, and scrollbars which reflect only the
11534 visible region.
11536 Note that mini-buffers sometimes aren't displaying any text. */
11537 if (!MINI_WINDOW_P (w)
11538 || (w == XWINDOW (minibuf_window)
11539 && NILP (echo_area_buffer[0])))
11541 struct buffer *buf = XBUFFER (w->buffer);
11542 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11543 start = marker_position (w->start) - BUF_BEGV (buf);
11544 /* I don't think this is guaranteed to be right. For the
11545 moment, we'll pretend it is. */
11546 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11548 if (end < start)
11549 end = start;
11550 if (whole < (end - start))
11551 whole = end - start;
11553 else
11554 start = end = whole = 0;
11556 /* Indicate what this scroll bar ought to be displaying now. */
11557 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11561 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11562 selected_window is redisplayed.
11564 We can return without actually redisplaying the window if
11565 fonts_changed_p is nonzero. In that case, redisplay_internal will
11566 retry. */
11568 static void
11569 redisplay_window (window, just_this_one_p)
11570 Lisp_Object window;
11571 int just_this_one_p;
11573 struct window *w = XWINDOW (window);
11574 struct frame *f = XFRAME (w->frame);
11575 struct buffer *buffer = XBUFFER (w->buffer);
11576 struct buffer *old = current_buffer;
11577 struct text_pos lpoint, opoint, startp;
11578 int update_mode_line;
11579 int tem;
11580 struct it it;
11581 /* Record it now because it's overwritten. */
11582 int current_matrix_up_to_date_p = 0;
11583 int used_current_matrix_p = 0;
11584 /* This is less strict than current_matrix_up_to_date_p.
11585 It indictes that the buffer contents and narrowing are unchanged. */
11586 int buffer_unchanged_p = 0;
11587 int temp_scroll_step = 0;
11588 int count = SPECPDL_INDEX ();
11589 int rc;
11590 int centering_position;
11591 int last_line_misfit = 0;
11593 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11594 opoint = lpoint;
11596 /* W must be a leaf window here. */
11597 xassert (!NILP (w->buffer));
11598 #if GLYPH_DEBUG
11599 *w->desired_matrix->method = 0;
11600 #endif
11602 specbind (Qinhibit_point_motion_hooks, Qt);
11604 reconsider_clip_changes (w, buffer);
11606 /* Has the mode line to be updated? */
11607 update_mode_line = (!NILP (w->update_mode_line)
11608 || update_mode_lines
11609 || buffer->clip_changed
11610 || buffer->prevent_redisplay_optimizations_p);
11612 if (MINI_WINDOW_P (w))
11614 if (w == XWINDOW (echo_area_window)
11615 && !NILP (echo_area_buffer[0]))
11617 if (update_mode_line)
11618 /* We may have to update a tty frame's menu bar or a
11619 tool-bar. Example `M-x C-h C-h C-g'. */
11620 goto finish_menu_bars;
11621 else
11622 /* We've already displayed the echo area glyphs in this window. */
11623 goto finish_scroll_bars;
11625 else if ((w != XWINDOW (minibuf_window)
11626 || minibuf_level == 0)
11627 /* When buffer is nonempty, redisplay window normally. */
11628 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11629 /* Quail displays non-mini buffers in minibuffer window.
11630 In that case, redisplay the window normally. */
11631 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11633 /* W is a mini-buffer window, but it's not active, so clear
11634 it. */
11635 int yb = window_text_bottom_y (w);
11636 struct glyph_row *row;
11637 int y;
11639 for (y = 0, row = w->desired_matrix->rows;
11640 y < yb;
11641 y += row->height, ++row)
11642 blank_row (w, row, y);
11643 goto finish_scroll_bars;
11646 clear_glyph_matrix (w->desired_matrix);
11649 /* Otherwise set up data on this window; select its buffer and point
11650 value. */
11651 /* Really select the buffer, for the sake of buffer-local
11652 variables. */
11653 set_buffer_internal_1 (XBUFFER (w->buffer));
11654 SET_TEXT_POS (opoint, PT, PT_BYTE);
11656 current_matrix_up_to_date_p
11657 = (!NILP (w->window_end_valid)
11658 && !current_buffer->clip_changed
11659 && !current_buffer->prevent_redisplay_optimizations_p
11660 && XFASTINT (w->last_modified) >= MODIFF
11661 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11663 buffer_unchanged_p
11664 = (!NILP (w->window_end_valid)
11665 && !current_buffer->clip_changed
11666 && XFASTINT (w->last_modified) >= MODIFF
11667 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11669 /* When windows_or_buffers_changed is non-zero, we can't rely on
11670 the window end being valid, so set it to nil there. */
11671 if (windows_or_buffers_changed)
11673 /* If window starts on a continuation line, maybe adjust the
11674 window start in case the window's width changed. */
11675 if (XMARKER (w->start)->buffer == current_buffer)
11676 compute_window_start_on_continuation_line (w);
11678 w->window_end_valid = Qnil;
11681 /* Some sanity checks. */
11682 CHECK_WINDOW_END (w);
11683 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11684 abort ();
11685 if (BYTEPOS (opoint) < CHARPOS (opoint))
11686 abort ();
11688 /* If %c is in mode line, update it if needed. */
11689 if (!NILP (w->column_number_displayed)
11690 /* This alternative quickly identifies a common case
11691 where no change is needed. */
11692 && !(PT == XFASTINT (w->last_point)
11693 && XFASTINT (w->last_modified) >= MODIFF
11694 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11695 && (XFASTINT (w->column_number_displayed)
11696 != (int) current_column ())) /* iftc */
11697 update_mode_line = 1;
11699 /* Count number of windows showing the selected buffer. An indirect
11700 buffer counts as its base buffer. */
11701 if (!just_this_one_p)
11703 struct buffer *current_base, *window_base;
11704 current_base = current_buffer;
11705 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11706 if (current_base->base_buffer)
11707 current_base = current_base->base_buffer;
11708 if (window_base->base_buffer)
11709 window_base = window_base->base_buffer;
11710 if (current_base == window_base)
11711 buffer_shared++;
11714 /* Point refers normally to the selected window. For any other
11715 window, set up appropriate value. */
11716 if (!EQ (window, selected_window))
11718 int new_pt = XMARKER (w->pointm)->charpos;
11719 int new_pt_byte = marker_byte_position (w->pointm);
11720 if (new_pt < BEGV)
11722 new_pt = BEGV;
11723 new_pt_byte = BEGV_BYTE;
11724 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11726 else if (new_pt > (ZV - 1))
11728 new_pt = ZV;
11729 new_pt_byte = ZV_BYTE;
11730 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11733 /* We don't use SET_PT so that the point-motion hooks don't run. */
11734 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11737 /* If any of the character widths specified in the display table
11738 have changed, invalidate the width run cache. It's true that
11739 this may be a bit late to catch such changes, but the rest of
11740 redisplay goes (non-fatally) haywire when the display table is
11741 changed, so why should we worry about doing any better? */
11742 if (current_buffer->width_run_cache)
11744 struct Lisp_Char_Table *disptab = buffer_display_table ();
11746 if (! disptab_matches_widthtab (disptab,
11747 XVECTOR (current_buffer->width_table)))
11749 invalidate_region_cache (current_buffer,
11750 current_buffer->width_run_cache,
11751 BEG, Z);
11752 recompute_width_table (current_buffer, disptab);
11756 /* If window-start is screwed up, choose a new one. */
11757 if (XMARKER (w->start)->buffer != current_buffer)
11758 goto recenter;
11760 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11762 /* If someone specified a new starting point but did not insist,
11763 check whether it can be used. */
11764 if (!NILP (w->optional_new_start)
11765 && CHARPOS (startp) >= BEGV
11766 && CHARPOS (startp) <= ZV)
11768 w->optional_new_start = Qnil;
11769 start_display (&it, w, startp);
11770 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11771 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11772 if (IT_CHARPOS (it) == PT)
11773 w->force_start = Qt;
11774 /* IT may overshoot PT if text at PT is invisible. */
11775 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11776 w->force_start = Qt;
11781 /* Handle case where place to start displaying has been specified,
11782 unless the specified location is outside the accessible range. */
11783 if (!NILP (w->force_start)
11784 || w->frozen_window_start_p)
11786 /* We set this later on if we have to adjust point. */
11787 int new_vpos = -1;
11789 w->force_start = Qnil;
11790 w->vscroll = 0;
11791 w->window_end_valid = Qnil;
11793 /* Forget any recorded base line for line number display. */
11794 if (!buffer_unchanged_p)
11795 w->base_line_number = Qnil;
11797 /* Redisplay the mode line. Select the buffer properly for that.
11798 Also, run the hook window-scroll-functions
11799 because we have scrolled. */
11800 /* Note, we do this after clearing force_start because
11801 if there's an error, it is better to forget about force_start
11802 than to get into an infinite loop calling the hook functions
11803 and having them get more errors. */
11804 if (!update_mode_line
11805 || ! NILP (Vwindow_scroll_functions))
11807 update_mode_line = 1;
11808 w->update_mode_line = Qt;
11809 startp = run_window_scroll_functions (window, startp);
11812 w->last_modified = make_number (0);
11813 w->last_overlay_modified = make_number (0);
11814 if (CHARPOS (startp) < BEGV)
11815 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11816 else if (CHARPOS (startp) > ZV)
11817 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11819 /* Redisplay, then check if cursor has been set during the
11820 redisplay. Give up if new fonts were loaded. */
11821 if (!try_window (window, startp))
11823 w->force_start = Qt;
11824 clear_glyph_matrix (w->desired_matrix);
11825 goto need_larger_matrices;
11828 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11830 /* If point does not appear, try to move point so it does
11831 appear. The desired matrix has been built above, so we
11832 can use it here. */
11833 new_vpos = window_box_height (w) / 2;
11836 if (!make_cursor_line_fully_visible (w, 0))
11838 /* Point does appear, but on a line partly visible at end of window.
11839 Move it back to a fully-visible line. */
11840 new_vpos = window_box_height (w);
11843 /* If we need to move point for either of the above reasons,
11844 now actually do it. */
11845 if (new_vpos >= 0)
11847 struct glyph_row *row;
11849 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11850 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11851 ++row;
11853 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11854 MATRIX_ROW_START_BYTEPOS (row));
11856 if (w != XWINDOW (selected_window))
11857 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11858 else if (current_buffer == old)
11859 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11861 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11863 /* If we are highlighting the region, then we just changed
11864 the region, so redisplay to show it. */
11865 if (!NILP (Vtransient_mark_mode)
11866 && !NILP (current_buffer->mark_active))
11868 clear_glyph_matrix (w->desired_matrix);
11869 if (!try_window (window, startp))
11870 goto need_larger_matrices;
11874 #if GLYPH_DEBUG
11875 debug_method_add (w, "forced window start");
11876 #endif
11877 goto done;
11880 /* Handle case where text has not changed, only point, and it has
11881 not moved off the frame, and we are not retrying after hscroll.
11882 (current_matrix_up_to_date_p is nonzero when retrying.) */
11883 if (current_matrix_up_to_date_p
11884 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11885 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11887 switch (rc)
11889 case CURSOR_MOVEMENT_SUCCESS:
11890 used_current_matrix_p = 1;
11891 goto done;
11893 #if 0 /* try_cursor_movement never returns this value. */
11894 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11895 goto need_larger_matrices;
11896 #endif
11898 case CURSOR_MOVEMENT_MUST_SCROLL:
11899 goto try_to_scroll;
11901 default:
11902 abort ();
11905 /* If current starting point was originally the beginning of a line
11906 but no longer is, find a new starting point. */
11907 else if (!NILP (w->start_at_line_beg)
11908 && !(CHARPOS (startp) <= BEGV
11909 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11911 #if GLYPH_DEBUG
11912 debug_method_add (w, "recenter 1");
11913 #endif
11914 goto recenter;
11917 /* Try scrolling with try_window_id. Value is > 0 if update has
11918 been done, it is -1 if we know that the same window start will
11919 not work. It is 0 if unsuccessful for some other reason. */
11920 else if ((tem = try_window_id (w)) != 0)
11922 #if GLYPH_DEBUG
11923 debug_method_add (w, "try_window_id %d", tem);
11924 #endif
11926 if (fonts_changed_p)
11927 goto need_larger_matrices;
11928 if (tem > 0)
11929 goto done;
11931 /* Otherwise try_window_id has returned -1 which means that we
11932 don't want the alternative below this comment to execute. */
11934 else if (CHARPOS (startp) >= BEGV
11935 && CHARPOS (startp) <= ZV
11936 && PT >= CHARPOS (startp)
11937 && (CHARPOS (startp) < ZV
11938 /* Avoid starting at end of buffer. */
11939 || CHARPOS (startp) == BEGV
11940 || (XFASTINT (w->last_modified) >= MODIFF
11941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11943 #if GLYPH_DEBUG
11944 debug_method_add (w, "same window start");
11945 #endif
11947 /* Try to redisplay starting at same place as before.
11948 If point has not moved off frame, accept the results. */
11949 if (!current_matrix_up_to_date_p
11950 /* Don't use try_window_reusing_current_matrix in this case
11951 because a window scroll function can have changed the
11952 buffer. */
11953 || !NILP (Vwindow_scroll_functions)
11954 || MINI_WINDOW_P (w)
11955 || !(used_current_matrix_p
11956 = try_window_reusing_current_matrix (w)))
11958 IF_DEBUG (debug_method_add (w, "1"));
11959 try_window (window, startp);
11962 if (fonts_changed_p)
11963 goto need_larger_matrices;
11965 if (w->cursor.vpos >= 0)
11967 if (!just_this_one_p
11968 || current_buffer->clip_changed
11969 || BEG_UNCHANGED < CHARPOS (startp))
11970 /* Forget any recorded base line for line number display. */
11971 w->base_line_number = Qnil;
11973 if (!make_cursor_line_fully_visible (w, 1))
11975 clear_glyph_matrix (w->desired_matrix);
11976 last_line_misfit = 1;
11978 /* Drop through and scroll. */
11979 else
11980 goto done;
11982 else
11983 clear_glyph_matrix (w->desired_matrix);
11986 try_to_scroll:
11988 w->last_modified = make_number (0);
11989 w->last_overlay_modified = make_number (0);
11991 /* Redisplay the mode line. Select the buffer properly for that. */
11992 if (!update_mode_line)
11994 update_mode_line = 1;
11995 w->update_mode_line = Qt;
11998 /* Try to scroll by specified few lines. */
11999 if ((scroll_conservatively
12000 || scroll_step
12001 || temp_scroll_step
12002 || NUMBERP (current_buffer->scroll_up_aggressively)
12003 || NUMBERP (current_buffer->scroll_down_aggressively))
12004 && !current_buffer->clip_changed
12005 && CHARPOS (startp) >= BEGV
12006 && CHARPOS (startp) <= ZV)
12008 /* The function returns -1 if new fonts were loaded, 1 if
12009 successful, 0 if not successful. */
12010 int rc = try_scrolling (window, just_this_one_p,
12011 scroll_conservatively,
12012 scroll_step,
12013 temp_scroll_step, last_line_misfit);
12014 switch (rc)
12016 case SCROLLING_SUCCESS:
12017 goto done;
12019 case SCROLLING_NEED_LARGER_MATRICES:
12020 goto need_larger_matrices;
12022 case SCROLLING_FAILED:
12023 break;
12025 default:
12026 abort ();
12030 /* Finally, just choose place to start which centers point */
12032 recenter:
12033 centering_position = window_box_height (w) / 2;
12035 point_at_top:
12036 /* Jump here with centering_position already set to 0. */
12038 #if GLYPH_DEBUG
12039 debug_method_add (w, "recenter");
12040 #endif
12042 /* w->vscroll = 0; */
12044 /* Forget any previously recorded base line for line number display. */
12045 if (!buffer_unchanged_p)
12046 w->base_line_number = Qnil;
12048 /* Move backward half the height of the window. */
12049 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12050 it.current_y = it.last_visible_y;
12051 move_it_vertically_backward (&it, centering_position);
12052 xassert (IT_CHARPOS (it) >= BEGV);
12054 /* The function move_it_vertically_backward may move over more
12055 than the specified y-distance. If it->w is small, e.g. a
12056 mini-buffer window, we may end up in front of the window's
12057 display area. Start displaying at the start of the line
12058 containing PT in this case. */
12059 if (it.current_y <= 0)
12061 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12062 move_it_vertically_backward (&it, 0);
12063 xassert (IT_CHARPOS (it) <= PT);
12064 it.current_y = 0;
12067 it.current_x = it.hpos = 0;
12069 /* Set startp here explicitly in case that helps avoid an infinite loop
12070 in case the window-scroll-functions functions get errors. */
12071 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12073 /* Run scroll hooks. */
12074 startp = run_window_scroll_functions (window, it.current.pos);
12076 /* Redisplay the window. */
12077 if (!current_matrix_up_to_date_p
12078 || windows_or_buffers_changed
12079 || cursor_type_changed
12080 /* Don't use try_window_reusing_current_matrix in this case
12081 because it can have changed the buffer. */
12082 || !NILP (Vwindow_scroll_functions)
12083 || !just_this_one_p
12084 || MINI_WINDOW_P (w)
12085 || !(used_current_matrix_p
12086 = try_window_reusing_current_matrix (w)))
12087 try_window (window, startp);
12089 /* If new fonts have been loaded (due to fontsets), give up. We
12090 have to start a new redisplay since we need to re-adjust glyph
12091 matrices. */
12092 if (fonts_changed_p)
12093 goto need_larger_matrices;
12095 /* If cursor did not appear assume that the middle of the window is
12096 in the first line of the window. Do it again with the next line.
12097 (Imagine a window of height 100, displaying two lines of height
12098 60. Moving back 50 from it->last_visible_y will end in the first
12099 line.) */
12100 if (w->cursor.vpos < 0)
12102 if (!NILP (w->window_end_valid)
12103 && PT >= Z - XFASTINT (w->window_end_pos))
12105 clear_glyph_matrix (w->desired_matrix);
12106 move_it_by_lines (&it, 1, 0);
12107 try_window (window, it.current.pos);
12109 else if (PT < IT_CHARPOS (it))
12111 clear_glyph_matrix (w->desired_matrix);
12112 move_it_by_lines (&it, -1, 0);
12113 try_window (window, it.current.pos);
12115 else
12117 /* Not much we can do about it. */
12121 /* Consider the following case: Window starts at BEGV, there is
12122 invisible, intangible text at BEGV, so that display starts at
12123 some point START > BEGV. It can happen that we are called with
12124 PT somewhere between BEGV and START. Try to handle that case. */
12125 if (w->cursor.vpos < 0)
12127 struct glyph_row *row = w->current_matrix->rows;
12128 if (row->mode_line_p)
12129 ++row;
12130 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12133 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12135 /* If vscroll is enabled, disable it and try again. */
12136 if (w->vscroll)
12138 w->vscroll = 0;
12139 clear_glyph_matrix (w->desired_matrix);
12140 goto recenter;
12143 /* If centering point failed to make the whole line visible,
12144 put point at the top instead. That has to make the whole line
12145 visible, if it can be done. */
12146 clear_glyph_matrix (w->desired_matrix);
12147 centering_position = 0;
12148 goto point_at_top;
12151 done:
12153 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12154 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12155 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12156 ? Qt : Qnil);
12158 /* Display the mode line, if we must. */
12159 if ((update_mode_line
12160 /* If window not full width, must redo its mode line
12161 if (a) the window to its side is being redone and
12162 (b) we do a frame-based redisplay. This is a consequence
12163 of how inverted lines are drawn in frame-based redisplay. */
12164 || (!just_this_one_p
12165 && !FRAME_WINDOW_P (f)
12166 && !WINDOW_FULL_WIDTH_P (w))
12167 /* Line number to display. */
12168 || INTEGERP (w->base_line_pos)
12169 /* Column number is displayed and different from the one displayed. */
12170 || (!NILP (w->column_number_displayed)
12171 && (XFASTINT (w->column_number_displayed)
12172 != (int) current_column ()))) /* iftc */
12173 /* This means that the window has a mode line. */
12174 && (WINDOW_WANTS_MODELINE_P (w)
12175 || WINDOW_WANTS_HEADER_LINE_P (w)))
12177 display_mode_lines (w);
12179 /* If mode line height has changed, arrange for a thorough
12180 immediate redisplay using the correct mode line height. */
12181 if (WINDOW_WANTS_MODELINE_P (w)
12182 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12184 fonts_changed_p = 1;
12185 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12186 = DESIRED_MODE_LINE_HEIGHT (w);
12189 /* If top line height has changed, arrange for a thorough
12190 immediate redisplay using the correct mode line height. */
12191 if (WINDOW_WANTS_HEADER_LINE_P (w)
12192 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12194 fonts_changed_p = 1;
12195 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12196 = DESIRED_HEADER_LINE_HEIGHT (w);
12199 if (fonts_changed_p)
12200 goto need_larger_matrices;
12203 if (!line_number_displayed
12204 && !BUFFERP (w->base_line_pos))
12206 w->base_line_pos = Qnil;
12207 w->base_line_number = Qnil;
12210 finish_menu_bars:
12212 /* When we reach a frame's selected window, redo the frame's menu bar. */
12213 if (update_mode_line
12214 && EQ (FRAME_SELECTED_WINDOW (f), window))
12216 int redisplay_menu_p = 0;
12217 int redisplay_tool_bar_p = 0;
12219 if (FRAME_WINDOW_P (f))
12221 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12222 || defined (USE_GTK)
12223 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12224 #else
12225 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12226 #endif
12228 else
12229 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12231 if (redisplay_menu_p)
12232 display_menu_bar (w);
12234 #ifdef HAVE_WINDOW_SYSTEM
12235 #ifdef USE_GTK
12236 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12237 #else
12238 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12239 && (FRAME_TOOL_BAR_LINES (f) > 0
12240 || auto_resize_tool_bars_p);
12242 #endif
12244 if (redisplay_tool_bar_p)
12245 redisplay_tool_bar (f);
12246 #endif
12249 #ifdef HAVE_WINDOW_SYSTEM
12250 if (FRAME_WINDOW_P (f)
12251 && update_window_fringes (w, 0)
12252 && !just_this_one_p
12253 && (used_current_matrix_p || overlay_arrow_seen)
12254 && !w->pseudo_window_p)
12256 update_begin (f);
12257 BLOCK_INPUT;
12258 if (draw_window_fringes (w, 1))
12259 x_draw_vertical_border (w);
12260 UNBLOCK_INPUT;
12261 update_end (f);
12263 #endif /* HAVE_WINDOW_SYSTEM */
12265 /* We go to this label, with fonts_changed_p nonzero,
12266 if it is necessary to try again using larger glyph matrices.
12267 We have to redeem the scroll bar even in this case,
12268 because the loop in redisplay_internal expects that. */
12269 need_larger_matrices:
12271 finish_scroll_bars:
12273 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12275 /* Set the thumb's position and size. */
12276 set_vertical_scroll_bar (w);
12278 /* Note that we actually used the scroll bar attached to this
12279 window, so it shouldn't be deleted at the end of redisplay. */
12280 redeem_scroll_bar_hook (w);
12283 /* Restore current_buffer and value of point in it. */
12284 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12285 set_buffer_internal_1 (old);
12286 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12288 unbind_to (count, Qnil);
12292 /* Build the complete desired matrix of WINDOW with a window start
12293 buffer position POS. Value is non-zero if successful. It is zero
12294 if fonts were loaded during redisplay which makes re-adjusting
12295 glyph matrices necessary. */
12298 try_window (window, pos)
12299 Lisp_Object window;
12300 struct text_pos pos;
12302 struct window *w = XWINDOW (window);
12303 struct it it;
12304 struct glyph_row *last_text_row = NULL;
12306 /* Make POS the new window start. */
12307 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12309 /* Mark cursor position as unknown. No overlay arrow seen. */
12310 w->cursor.vpos = -1;
12311 overlay_arrow_seen = 0;
12313 /* Initialize iterator and info to start at POS. */
12314 start_display (&it, w, pos);
12316 /* Display all lines of W. */
12317 while (it.current_y < it.last_visible_y)
12319 if (display_line (&it))
12320 last_text_row = it.glyph_row - 1;
12321 if (fonts_changed_p)
12322 return 0;
12325 /* If bottom moved off end of frame, change mode line percentage. */
12326 if (XFASTINT (w->window_end_pos) <= 0
12327 && Z != IT_CHARPOS (it))
12328 w->update_mode_line = Qt;
12330 /* Set window_end_pos to the offset of the last character displayed
12331 on the window from the end of current_buffer. Set
12332 window_end_vpos to its row number. */
12333 if (last_text_row)
12335 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12336 w->window_end_bytepos
12337 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12338 w->window_end_pos
12339 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12340 w->window_end_vpos
12341 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12342 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12343 ->displays_text_p);
12345 else
12347 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12348 w->window_end_pos = make_number (Z - ZV);
12349 w->window_end_vpos = make_number (0);
12352 /* But that is not valid info until redisplay finishes. */
12353 w->window_end_valid = Qnil;
12354 return 1;
12359 /************************************************************************
12360 Window redisplay reusing current matrix when buffer has not changed
12361 ************************************************************************/
12363 /* Try redisplay of window W showing an unchanged buffer with a
12364 different window start than the last time it was displayed by
12365 reusing its current matrix. Value is non-zero if successful.
12366 W->start is the new window start. */
12368 static int
12369 try_window_reusing_current_matrix (w)
12370 struct window *w;
12372 struct frame *f = XFRAME (w->frame);
12373 struct glyph_row *row, *bottom_row;
12374 struct it it;
12375 struct run run;
12376 struct text_pos start, new_start;
12377 int nrows_scrolled, i;
12378 struct glyph_row *last_text_row;
12379 struct glyph_row *last_reused_text_row;
12380 struct glyph_row *start_row;
12381 int start_vpos, min_y, max_y;
12383 #if GLYPH_DEBUG
12384 if (inhibit_try_window_reusing)
12385 return 0;
12386 #endif
12388 if (/* This function doesn't handle terminal frames. */
12389 !FRAME_WINDOW_P (f)
12390 /* Don't try to reuse the display if windows have been split
12391 or such. */
12392 || windows_or_buffers_changed
12393 || cursor_type_changed)
12394 return 0;
12396 /* Can't do this if region may have changed. */
12397 if ((!NILP (Vtransient_mark_mode)
12398 && !NILP (current_buffer->mark_active))
12399 || !NILP (w->region_showing)
12400 || !NILP (Vshow_trailing_whitespace))
12401 return 0;
12403 /* If top-line visibility has changed, give up. */
12404 if (WINDOW_WANTS_HEADER_LINE_P (w)
12405 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12406 return 0;
12408 /* Give up if old or new display is scrolled vertically. We could
12409 make this function handle this, but right now it doesn't. */
12410 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12411 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12412 return 0;
12414 /* The variable new_start now holds the new window start. The old
12415 start `start' can be determined from the current matrix. */
12416 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12417 start = start_row->start.pos;
12418 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12420 /* Clear the desired matrix for the display below. */
12421 clear_glyph_matrix (w->desired_matrix);
12423 if (CHARPOS (new_start) <= CHARPOS (start))
12425 int first_row_y;
12427 /* Don't use this method if the display starts with an ellipsis
12428 displayed for invisible text. It's not easy to handle that case
12429 below, and it's certainly not worth the effort since this is
12430 not a frequent case. */
12431 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12432 return 0;
12434 IF_DEBUG (debug_method_add (w, "twu1"));
12436 /* Display up to a row that can be reused. The variable
12437 last_text_row is set to the last row displayed that displays
12438 text. Note that it.vpos == 0 if or if not there is a
12439 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12440 start_display (&it, w, new_start);
12441 first_row_y = it.current_y;
12442 w->cursor.vpos = -1;
12443 last_text_row = last_reused_text_row = NULL;
12445 while (it.current_y < it.last_visible_y
12446 && !fonts_changed_p)
12448 /* If we have reached into the characters in the START row,
12449 that means the line boundaries have changed. So we
12450 can't start copying with the row START. Maybe it will
12451 work to start copying with the following row. */
12452 while (IT_CHARPOS (it) > CHARPOS (start))
12454 /* Advance to the next row as the "start". */
12455 start_row++;
12456 start = start_row->start.pos;
12457 /* If there are no more rows to try, or just one, give up. */
12458 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12459 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12460 || CHARPOS (start) == ZV)
12462 clear_glyph_matrix (w->desired_matrix);
12463 return 0;
12466 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12468 /* If we have reached alignment,
12469 we can copy the rest of the rows. */
12470 if (IT_CHARPOS (it) == CHARPOS (start))
12471 break;
12473 if (display_line (&it))
12474 last_text_row = it.glyph_row - 1;
12477 /* A value of current_y < last_visible_y means that we stopped
12478 at the previous window start, which in turn means that we
12479 have at least one reusable row. */
12480 if (it.current_y < it.last_visible_y)
12482 /* IT.vpos always starts from 0; it counts text lines. */
12483 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12485 /* Find PT if not already found in the lines displayed. */
12486 if (w->cursor.vpos < 0)
12488 int dy = it.current_y - start_row->y;
12490 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12491 row = row_containing_pos (w, PT, row, NULL, dy);
12492 if (row)
12493 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12494 dy, nrows_scrolled);
12495 else
12497 clear_glyph_matrix (w->desired_matrix);
12498 return 0;
12502 /* Scroll the display. Do it before the current matrix is
12503 changed. The problem here is that update has not yet
12504 run, i.e. part of the current matrix is not up to date.
12505 scroll_run_hook will clear the cursor, and use the
12506 current matrix to get the height of the row the cursor is
12507 in. */
12508 run.current_y = start_row->y;
12509 run.desired_y = it.current_y;
12510 run.height = it.last_visible_y - it.current_y;
12512 if (run.height > 0 && run.current_y != run.desired_y)
12514 update_begin (f);
12515 rif->update_window_begin_hook (w);
12516 rif->clear_window_mouse_face (w);
12517 rif->scroll_run_hook (w, &run);
12518 rif->update_window_end_hook (w, 0, 0);
12519 update_end (f);
12522 /* Shift current matrix down by nrows_scrolled lines. */
12523 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12524 rotate_matrix (w->current_matrix,
12525 start_vpos,
12526 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12527 nrows_scrolled);
12529 /* Disable lines that must be updated. */
12530 for (i = 0; i < it.vpos; ++i)
12531 (start_row + i)->enabled_p = 0;
12533 /* Re-compute Y positions. */
12534 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12535 max_y = it.last_visible_y;
12536 for (row = start_row + nrows_scrolled;
12537 row < bottom_row;
12538 ++row)
12540 row->y = it.current_y;
12541 row->visible_height = row->height;
12543 if (row->y < min_y)
12544 row->visible_height -= min_y - row->y;
12545 if (row->y + row->height > max_y)
12546 row->visible_height -= row->y + row->height - max_y;
12547 row->redraw_fringe_bitmaps_p = 1;
12549 it.current_y += row->height;
12551 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12552 last_reused_text_row = row;
12553 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12554 break;
12557 /* Disable lines in the current matrix which are now
12558 below the window. */
12559 for (++row; row < bottom_row; ++row)
12560 row->enabled_p = 0;
12563 /* Update window_end_pos etc.; last_reused_text_row is the last
12564 reused row from the current matrix containing text, if any.
12565 The value of last_text_row is the last displayed line
12566 containing text. */
12567 if (last_reused_text_row)
12569 w->window_end_bytepos
12570 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12571 w->window_end_pos
12572 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12573 w->window_end_vpos
12574 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12575 w->current_matrix));
12577 else if (last_text_row)
12579 w->window_end_bytepos
12580 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12581 w->window_end_pos
12582 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12583 w->window_end_vpos
12584 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12586 else
12588 /* This window must be completely empty. */
12589 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12590 w->window_end_pos = make_number (Z - ZV);
12591 w->window_end_vpos = make_number (0);
12593 w->window_end_valid = Qnil;
12595 /* Update hint: don't try scrolling again in update_window. */
12596 w->desired_matrix->no_scrolling_p = 1;
12598 #if GLYPH_DEBUG
12599 debug_method_add (w, "try_window_reusing_current_matrix 1");
12600 #endif
12601 return 1;
12603 else if (CHARPOS (new_start) > CHARPOS (start))
12605 struct glyph_row *pt_row, *row;
12606 struct glyph_row *first_reusable_row;
12607 struct glyph_row *first_row_to_display;
12608 int dy;
12609 int yb = window_text_bottom_y (w);
12611 /* Find the row starting at new_start, if there is one. Don't
12612 reuse a partially visible line at the end. */
12613 first_reusable_row = start_row;
12614 while (first_reusable_row->enabled_p
12615 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12616 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12617 < CHARPOS (new_start)))
12618 ++first_reusable_row;
12620 /* Give up if there is no row to reuse. */
12621 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12622 || !first_reusable_row->enabled_p
12623 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12624 != CHARPOS (new_start)))
12625 return 0;
12627 /* We can reuse fully visible rows beginning with
12628 first_reusable_row to the end of the window. Set
12629 first_row_to_display to the first row that cannot be reused.
12630 Set pt_row to the row containing point, if there is any. */
12631 pt_row = NULL;
12632 for (first_row_to_display = first_reusable_row;
12633 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12634 ++first_row_to_display)
12636 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12637 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12638 pt_row = first_row_to_display;
12641 /* Start displaying at the start of first_row_to_display. */
12642 xassert (first_row_to_display->y < yb);
12643 init_to_row_start (&it, w, first_row_to_display);
12645 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12646 - start_vpos);
12647 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12648 - nrows_scrolled);
12649 it.current_y = (first_row_to_display->y - first_reusable_row->y
12650 + WINDOW_HEADER_LINE_HEIGHT (w));
12652 /* Display lines beginning with first_row_to_display in the
12653 desired matrix. Set last_text_row to the last row displayed
12654 that displays text. */
12655 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12656 if (pt_row == NULL)
12657 w->cursor.vpos = -1;
12658 last_text_row = NULL;
12659 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12660 if (display_line (&it))
12661 last_text_row = it.glyph_row - 1;
12663 /* Give up If point isn't in a row displayed or reused. */
12664 if (w->cursor.vpos < 0)
12666 clear_glyph_matrix (w->desired_matrix);
12667 return 0;
12670 /* If point is in a reused row, adjust y and vpos of the cursor
12671 position. */
12672 if (pt_row)
12674 w->cursor.vpos -= nrows_scrolled;
12675 w->cursor.y -= first_reusable_row->y - start_row->y;
12678 /* Scroll the display. */
12679 run.current_y = first_reusable_row->y;
12680 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12681 run.height = it.last_visible_y - run.current_y;
12682 dy = run.current_y - run.desired_y;
12684 if (run.height)
12686 update_begin (f);
12687 rif->update_window_begin_hook (w);
12688 rif->clear_window_mouse_face (w);
12689 rif->scroll_run_hook (w, &run);
12690 rif->update_window_end_hook (w, 0, 0);
12691 update_end (f);
12694 /* Adjust Y positions of reused rows. */
12695 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12696 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12697 max_y = it.last_visible_y;
12698 for (row = first_reusable_row; row < first_row_to_display; ++row)
12700 row->y -= dy;
12701 row->visible_height = row->height;
12702 if (row->y < min_y)
12703 row->visible_height -= min_y - row->y;
12704 if (row->y + row->height > max_y)
12705 row->visible_height -= row->y + row->height - max_y;
12706 row->redraw_fringe_bitmaps_p = 1;
12709 /* Scroll the current matrix. */
12710 xassert (nrows_scrolled > 0);
12711 rotate_matrix (w->current_matrix,
12712 start_vpos,
12713 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12714 -nrows_scrolled);
12716 /* Disable rows not reused. */
12717 for (row -= nrows_scrolled; row < bottom_row; ++row)
12718 row->enabled_p = 0;
12720 /* Point may have moved to a different line, so we cannot assume that
12721 the previous cursor position is valid; locate the correct row. */
12722 if (pt_row)
12724 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12725 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12726 row++)
12728 w->cursor.vpos++;
12729 w->cursor.y = row->y;
12731 if (row < bottom_row)
12733 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12734 while (glyph->charpos < PT)
12736 w->cursor.hpos++;
12737 w->cursor.x += glyph->pixel_width;
12738 glyph++;
12743 /* Adjust window end. A null value of last_text_row means that
12744 the window end is in reused rows which in turn means that
12745 only its vpos can have changed. */
12746 if (last_text_row)
12748 w->window_end_bytepos
12749 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12750 w->window_end_pos
12751 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12752 w->window_end_vpos
12753 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12755 else
12757 w->window_end_vpos
12758 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12761 w->window_end_valid = Qnil;
12762 w->desired_matrix->no_scrolling_p = 1;
12764 #if GLYPH_DEBUG
12765 debug_method_add (w, "try_window_reusing_current_matrix 2");
12766 #endif
12767 return 1;
12770 return 0;
12775 /************************************************************************
12776 Window redisplay reusing current matrix when buffer has changed
12777 ************************************************************************/
12779 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12780 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12781 int *, int *));
12782 static struct glyph_row *
12783 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12784 struct glyph_row *));
12787 /* Return the last row in MATRIX displaying text. If row START is
12788 non-null, start searching with that row. IT gives the dimensions
12789 of the display. Value is null if matrix is empty; otherwise it is
12790 a pointer to the row found. */
12792 static struct glyph_row *
12793 find_last_row_displaying_text (matrix, it, start)
12794 struct glyph_matrix *matrix;
12795 struct it *it;
12796 struct glyph_row *start;
12798 struct glyph_row *row, *row_found;
12800 /* Set row_found to the last row in IT->w's current matrix
12801 displaying text. The loop looks funny but think of partially
12802 visible lines. */
12803 row_found = NULL;
12804 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12805 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12807 xassert (row->enabled_p);
12808 row_found = row;
12809 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12810 break;
12811 ++row;
12814 return row_found;
12818 /* Return the last row in the current matrix of W that is not affected
12819 by changes at the start of current_buffer that occurred since W's
12820 current matrix was built. Value is null if no such row exists.
12822 BEG_UNCHANGED us the number of characters unchanged at the start of
12823 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12824 first changed character in current_buffer. Characters at positions <
12825 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12826 when the current matrix was built. */
12828 static struct glyph_row *
12829 find_last_unchanged_at_beg_row (w)
12830 struct window *w;
12832 int first_changed_pos = BEG + BEG_UNCHANGED;
12833 struct glyph_row *row;
12834 struct glyph_row *row_found = NULL;
12835 int yb = window_text_bottom_y (w);
12837 /* Find the last row displaying unchanged text. */
12838 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12839 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12840 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12842 if (/* If row ends before first_changed_pos, it is unchanged,
12843 except in some case. */
12844 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12845 /* When row ends in ZV and we write at ZV it is not
12846 unchanged. */
12847 && !row->ends_at_zv_p
12848 /* When first_changed_pos is the end of a continued line,
12849 row is not unchanged because it may be no longer
12850 continued. */
12851 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12852 && (row->continued_p
12853 || row->exact_window_width_line_p)))
12854 row_found = row;
12856 /* Stop if last visible row. */
12857 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12858 break;
12860 ++row;
12863 return row_found;
12867 /* Find the first glyph row in the current matrix of W that is not
12868 affected by changes at the end of current_buffer since the
12869 time W's current matrix was built.
12871 Return in *DELTA the number of chars by which buffer positions in
12872 unchanged text at the end of current_buffer must be adjusted.
12874 Return in *DELTA_BYTES the corresponding number of bytes.
12876 Value is null if no such row exists, i.e. all rows are affected by
12877 changes. */
12879 static struct glyph_row *
12880 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12881 struct window *w;
12882 int *delta, *delta_bytes;
12884 struct glyph_row *row;
12885 struct glyph_row *row_found = NULL;
12887 *delta = *delta_bytes = 0;
12889 /* Display must not have been paused, otherwise the current matrix
12890 is not up to date. */
12891 if (NILP (w->window_end_valid))
12892 abort ();
12894 /* A value of window_end_pos >= END_UNCHANGED means that the window
12895 end is in the range of changed text. If so, there is no
12896 unchanged row at the end of W's current matrix. */
12897 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12898 return NULL;
12900 /* Set row to the last row in W's current matrix displaying text. */
12901 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12903 /* If matrix is entirely empty, no unchanged row exists. */
12904 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12906 /* The value of row is the last glyph row in the matrix having a
12907 meaningful buffer position in it. The end position of row
12908 corresponds to window_end_pos. This allows us to translate
12909 buffer positions in the current matrix to current buffer
12910 positions for characters not in changed text. */
12911 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12912 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12913 int last_unchanged_pos, last_unchanged_pos_old;
12914 struct glyph_row *first_text_row
12915 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12917 *delta = Z - Z_old;
12918 *delta_bytes = Z_BYTE - Z_BYTE_old;
12920 /* Set last_unchanged_pos to the buffer position of the last
12921 character in the buffer that has not been changed. Z is the
12922 index + 1 of the last character in current_buffer, i.e. by
12923 subtracting END_UNCHANGED we get the index of the last
12924 unchanged character, and we have to add BEG to get its buffer
12925 position. */
12926 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12927 last_unchanged_pos_old = last_unchanged_pos - *delta;
12929 /* Search backward from ROW for a row displaying a line that
12930 starts at a minimum position >= last_unchanged_pos_old. */
12931 for (; row > first_text_row; --row)
12933 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12934 abort ();
12936 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12937 row_found = row;
12941 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12942 abort ();
12944 return row_found;
12948 /* Make sure that glyph rows in the current matrix of window W
12949 reference the same glyph memory as corresponding rows in the
12950 frame's frame matrix. This function is called after scrolling W's
12951 current matrix on a terminal frame in try_window_id and
12952 try_window_reusing_current_matrix. */
12954 static void
12955 sync_frame_with_window_matrix_rows (w)
12956 struct window *w;
12958 struct frame *f = XFRAME (w->frame);
12959 struct glyph_row *window_row, *window_row_end, *frame_row;
12961 /* Preconditions: W must be a leaf window and full-width. Its frame
12962 must have a frame matrix. */
12963 xassert (NILP (w->hchild) && NILP (w->vchild));
12964 xassert (WINDOW_FULL_WIDTH_P (w));
12965 xassert (!FRAME_WINDOW_P (f));
12967 /* If W is a full-width window, glyph pointers in W's current matrix
12968 have, by definition, to be the same as glyph pointers in the
12969 corresponding frame matrix. Note that frame matrices have no
12970 marginal areas (see build_frame_matrix). */
12971 window_row = w->current_matrix->rows;
12972 window_row_end = window_row + w->current_matrix->nrows;
12973 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12974 while (window_row < window_row_end)
12976 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12977 struct glyph *end = window_row->glyphs[LAST_AREA];
12979 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12980 frame_row->glyphs[TEXT_AREA] = start;
12981 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12982 frame_row->glyphs[LAST_AREA] = end;
12984 /* Disable frame rows whose corresponding window rows have
12985 been disabled in try_window_id. */
12986 if (!window_row->enabled_p)
12987 frame_row->enabled_p = 0;
12989 ++window_row, ++frame_row;
12994 /* Find the glyph row in window W containing CHARPOS. Consider all
12995 rows between START and END (not inclusive). END null means search
12996 all rows to the end of the display area of W. Value is the row
12997 containing CHARPOS or null. */
12999 struct glyph_row *
13000 row_containing_pos (w, charpos, start, end, dy)
13001 struct window *w;
13002 int charpos;
13003 struct glyph_row *start, *end;
13004 int dy;
13006 struct glyph_row *row = start;
13007 int last_y;
13009 /* If we happen to start on a header-line, skip that. */
13010 if (row->mode_line_p)
13011 ++row;
13013 if ((end && row >= end) || !row->enabled_p)
13014 return NULL;
13016 last_y = window_text_bottom_y (w) - dy;
13018 while (1)
13020 /* Give up if we have gone too far. */
13021 if (end && row >= end)
13022 return NULL;
13023 /* This formerly returned if they were equal.
13024 I think that both quantities are of a "last plus one" type;
13025 if so, when they are equal, the row is within the screen. -- rms. */
13026 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13027 return NULL;
13029 /* If it is in this row, return this row. */
13030 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13031 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13032 /* The end position of a row equals the start
13033 position of the next row. If CHARPOS is there, we
13034 would rather display it in the next line, except
13035 when this line ends in ZV. */
13036 && !row->ends_at_zv_p
13037 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13038 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13039 return row;
13040 ++row;
13045 /* Try to redisplay window W by reusing its existing display. W's
13046 current matrix must be up to date when this function is called,
13047 i.e. window_end_valid must not be nil.
13049 Value is
13051 1 if display has been updated
13052 0 if otherwise unsuccessful
13053 -1 if redisplay with same window start is known not to succeed
13055 The following steps are performed:
13057 1. Find the last row in the current matrix of W that is not
13058 affected by changes at the start of current_buffer. If no such row
13059 is found, give up.
13061 2. Find the first row in W's current matrix that is not affected by
13062 changes at the end of current_buffer. Maybe there is no such row.
13064 3. Display lines beginning with the row + 1 found in step 1 to the
13065 row found in step 2 or, if step 2 didn't find a row, to the end of
13066 the window.
13068 4. If cursor is not known to appear on the window, give up.
13070 5. If display stopped at the row found in step 2, scroll the
13071 display and current matrix as needed.
13073 6. Maybe display some lines at the end of W, if we must. This can
13074 happen under various circumstances, like a partially visible line
13075 becoming fully visible, or because newly displayed lines are displayed
13076 in smaller font sizes.
13078 7. Update W's window end information. */
13080 static int
13081 try_window_id (w)
13082 struct window *w;
13084 struct frame *f = XFRAME (w->frame);
13085 struct glyph_matrix *current_matrix = w->current_matrix;
13086 struct glyph_matrix *desired_matrix = w->desired_matrix;
13087 struct glyph_row *last_unchanged_at_beg_row;
13088 struct glyph_row *first_unchanged_at_end_row;
13089 struct glyph_row *row;
13090 struct glyph_row *bottom_row;
13091 int bottom_vpos;
13092 struct it it;
13093 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13094 struct text_pos start_pos;
13095 struct run run;
13096 int first_unchanged_at_end_vpos = 0;
13097 struct glyph_row *last_text_row, *last_text_row_at_end;
13098 struct text_pos start;
13099 int first_changed_charpos, last_changed_charpos;
13101 #if GLYPH_DEBUG
13102 if (inhibit_try_window_id)
13103 return 0;
13104 #endif
13106 /* This is handy for debugging. */
13107 #if 0
13108 #define GIVE_UP(X) \
13109 do { \
13110 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13111 return 0; \
13112 } while (0)
13113 #else
13114 #define GIVE_UP(X) return 0
13115 #endif
13117 SET_TEXT_POS_FROM_MARKER (start, w->start);
13119 /* Don't use this for mini-windows because these can show
13120 messages and mini-buffers, and we don't handle that here. */
13121 if (MINI_WINDOW_P (w))
13122 GIVE_UP (1);
13124 /* This flag is used to prevent redisplay optimizations. */
13125 if (windows_or_buffers_changed || cursor_type_changed)
13126 GIVE_UP (2);
13128 /* Verify that narrowing has not changed.
13129 Also verify that we were not told to prevent redisplay optimizations.
13130 It would be nice to further
13131 reduce the number of cases where this prevents try_window_id. */
13132 if (current_buffer->clip_changed
13133 || current_buffer->prevent_redisplay_optimizations_p)
13134 GIVE_UP (3);
13136 /* Window must either use window-based redisplay or be full width. */
13137 if (!FRAME_WINDOW_P (f)
13138 && (!line_ins_del_ok
13139 || !WINDOW_FULL_WIDTH_P (w)))
13140 GIVE_UP (4);
13142 /* Give up if point is not known NOT to appear in W. */
13143 if (PT < CHARPOS (start))
13144 GIVE_UP (5);
13146 /* Another way to prevent redisplay optimizations. */
13147 if (XFASTINT (w->last_modified) == 0)
13148 GIVE_UP (6);
13150 /* Verify that window is not hscrolled. */
13151 if (XFASTINT (w->hscroll) != 0)
13152 GIVE_UP (7);
13154 /* Verify that display wasn't paused. */
13155 if (NILP (w->window_end_valid))
13156 GIVE_UP (8);
13158 /* Can't use this if highlighting a region because a cursor movement
13159 will do more than just set the cursor. */
13160 if (!NILP (Vtransient_mark_mode)
13161 && !NILP (current_buffer->mark_active))
13162 GIVE_UP (9);
13164 /* Likewise if highlighting trailing whitespace. */
13165 if (!NILP (Vshow_trailing_whitespace))
13166 GIVE_UP (11);
13168 /* Likewise if showing a region. */
13169 if (!NILP (w->region_showing))
13170 GIVE_UP (10);
13172 /* Can use this if overlay arrow position and or string have changed. */
13173 if (overlay_arrows_changed_p ())
13174 GIVE_UP (12);
13177 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13178 only if buffer has really changed. The reason is that the gap is
13179 initially at Z for freshly visited files. The code below would
13180 set end_unchanged to 0 in that case. */
13181 if (MODIFF > SAVE_MODIFF
13182 /* This seems to happen sometimes after saving a buffer. */
13183 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13185 if (GPT - BEG < BEG_UNCHANGED)
13186 BEG_UNCHANGED = GPT - BEG;
13187 if (Z - GPT < END_UNCHANGED)
13188 END_UNCHANGED = Z - GPT;
13191 /* The position of the first and last character that has been changed. */
13192 first_changed_charpos = BEG + BEG_UNCHANGED;
13193 last_changed_charpos = Z - END_UNCHANGED;
13195 /* If window starts after a line end, and the last change is in
13196 front of that newline, then changes don't affect the display.
13197 This case happens with stealth-fontification. Note that although
13198 the display is unchanged, glyph positions in the matrix have to
13199 be adjusted, of course. */
13200 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13201 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13202 && ((last_changed_charpos < CHARPOS (start)
13203 && CHARPOS (start) == BEGV)
13204 || (last_changed_charpos < CHARPOS (start) - 1
13205 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13207 int Z_old, delta, Z_BYTE_old, delta_bytes;
13208 struct glyph_row *r0;
13210 /* Compute how many chars/bytes have been added to or removed
13211 from the buffer. */
13212 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13213 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13214 delta = Z - Z_old;
13215 delta_bytes = Z_BYTE - Z_BYTE_old;
13217 /* Give up if PT is not in the window. Note that it already has
13218 been checked at the start of try_window_id that PT is not in
13219 front of the window start. */
13220 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13221 GIVE_UP (13);
13223 /* If window start is unchanged, we can reuse the whole matrix
13224 as is, after adjusting glyph positions. No need to compute
13225 the window end again, since its offset from Z hasn't changed. */
13226 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13227 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13228 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13229 /* PT must not be in a partially visible line. */
13230 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13231 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13233 /* Adjust positions in the glyph matrix. */
13234 if (delta || delta_bytes)
13236 struct glyph_row *r1
13237 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13238 increment_matrix_positions (w->current_matrix,
13239 MATRIX_ROW_VPOS (r0, current_matrix),
13240 MATRIX_ROW_VPOS (r1, current_matrix),
13241 delta, delta_bytes);
13244 /* Set the cursor. */
13245 row = row_containing_pos (w, PT, r0, NULL, 0);
13246 if (row)
13247 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13248 else
13249 abort ();
13250 return 1;
13254 /* Handle the case that changes are all below what is displayed in
13255 the window, and that PT is in the window. This shortcut cannot
13256 be taken if ZV is visible in the window, and text has been added
13257 there that is visible in the window. */
13258 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13259 /* ZV is not visible in the window, or there are no
13260 changes at ZV, actually. */
13261 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13262 || first_changed_charpos == last_changed_charpos))
13264 struct glyph_row *r0;
13266 /* Give up if PT is not in the window. Note that it already has
13267 been checked at the start of try_window_id that PT is not in
13268 front of the window start. */
13269 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13270 GIVE_UP (14);
13272 /* If window start is unchanged, we can reuse the whole matrix
13273 as is, without changing glyph positions since no text has
13274 been added/removed in front of the window end. */
13275 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13276 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13277 /* PT must not be in a partially visible line. */
13278 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13279 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13281 /* We have to compute the window end anew since text
13282 can have been added/removed after it. */
13283 w->window_end_pos
13284 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13285 w->window_end_bytepos
13286 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13288 /* Set the cursor. */
13289 row = row_containing_pos (w, PT, r0, NULL, 0);
13290 if (row)
13291 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13292 else
13293 abort ();
13294 return 2;
13298 /* Give up if window start is in the changed area.
13300 The condition used to read
13302 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13304 but why that was tested escapes me at the moment. */
13305 if (CHARPOS (start) >= first_changed_charpos
13306 && CHARPOS (start) <= last_changed_charpos)
13307 GIVE_UP (15);
13309 /* Check that window start agrees with the start of the first glyph
13310 row in its current matrix. Check this after we know the window
13311 start is not in changed text, otherwise positions would not be
13312 comparable. */
13313 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13314 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13315 GIVE_UP (16);
13317 /* Give up if the window ends in strings. Overlay strings
13318 at the end are difficult to handle, so don't try. */
13319 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13320 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13321 GIVE_UP (20);
13323 /* Compute the position at which we have to start displaying new
13324 lines. Some of the lines at the top of the window might be
13325 reusable because they are not displaying changed text. Find the
13326 last row in W's current matrix not affected by changes at the
13327 start of current_buffer. Value is null if changes start in the
13328 first line of window. */
13329 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13330 if (last_unchanged_at_beg_row)
13332 /* Avoid starting to display in the moddle of a character, a TAB
13333 for instance. This is easier than to set up the iterator
13334 exactly, and it's not a frequent case, so the additional
13335 effort wouldn't really pay off. */
13336 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13337 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13338 && last_unchanged_at_beg_row > w->current_matrix->rows)
13339 --last_unchanged_at_beg_row;
13341 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13342 GIVE_UP (17);
13344 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13345 GIVE_UP (18);
13346 start_pos = it.current.pos;
13348 /* Start displaying new lines in the desired matrix at the same
13349 vpos we would use in the current matrix, i.e. below
13350 last_unchanged_at_beg_row. */
13351 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13352 current_matrix);
13353 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13354 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13356 xassert (it.hpos == 0 && it.current_x == 0);
13358 else
13360 /* There are no reusable lines at the start of the window.
13361 Start displaying in the first text line. */
13362 start_display (&it, w, start);
13363 it.vpos = it.first_vpos;
13364 start_pos = it.current.pos;
13367 /* Find the first row that is not affected by changes at the end of
13368 the buffer. Value will be null if there is no unchanged row, in
13369 which case we must redisplay to the end of the window. delta
13370 will be set to the value by which buffer positions beginning with
13371 first_unchanged_at_end_row have to be adjusted due to text
13372 changes. */
13373 first_unchanged_at_end_row
13374 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13375 IF_DEBUG (debug_delta = delta);
13376 IF_DEBUG (debug_delta_bytes = delta_bytes);
13378 /* Set stop_pos to the buffer position up to which we will have to
13379 display new lines. If first_unchanged_at_end_row != NULL, this
13380 is the buffer position of the start of the line displayed in that
13381 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13382 that we don't stop at a buffer position. */
13383 stop_pos = 0;
13384 if (first_unchanged_at_end_row)
13386 xassert (last_unchanged_at_beg_row == NULL
13387 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13389 /* If this is a continuation line, move forward to the next one
13390 that isn't. Changes in lines above affect this line.
13391 Caution: this may move first_unchanged_at_end_row to a row
13392 not displaying text. */
13393 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13394 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13395 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13396 < it.last_visible_y))
13397 ++first_unchanged_at_end_row;
13399 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13400 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13401 >= it.last_visible_y))
13402 first_unchanged_at_end_row = NULL;
13403 else
13405 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13406 + delta);
13407 first_unchanged_at_end_vpos
13408 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13409 xassert (stop_pos >= Z - END_UNCHANGED);
13412 else if (last_unchanged_at_beg_row == NULL)
13413 GIVE_UP (19);
13416 #if GLYPH_DEBUG
13418 /* Either there is no unchanged row at the end, or the one we have
13419 now displays text. This is a necessary condition for the window
13420 end pos calculation at the end of this function. */
13421 xassert (first_unchanged_at_end_row == NULL
13422 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13424 debug_last_unchanged_at_beg_vpos
13425 = (last_unchanged_at_beg_row
13426 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13427 : -1);
13428 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13430 #endif /* GLYPH_DEBUG != 0 */
13433 /* Display new lines. Set last_text_row to the last new line
13434 displayed which has text on it, i.e. might end up as being the
13435 line where the window_end_vpos is. */
13436 w->cursor.vpos = -1;
13437 last_text_row = NULL;
13438 overlay_arrow_seen = 0;
13439 while (it.current_y < it.last_visible_y
13440 && !fonts_changed_p
13441 && (first_unchanged_at_end_row == NULL
13442 || IT_CHARPOS (it) < stop_pos))
13444 if (display_line (&it))
13445 last_text_row = it.glyph_row - 1;
13448 if (fonts_changed_p)
13449 return -1;
13452 /* Compute differences in buffer positions, y-positions etc. for
13453 lines reused at the bottom of the window. Compute what we can
13454 scroll. */
13455 if (first_unchanged_at_end_row
13456 /* No lines reused because we displayed everything up to the
13457 bottom of the window. */
13458 && it.current_y < it.last_visible_y)
13460 dvpos = (it.vpos
13461 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13462 current_matrix));
13463 dy = it.current_y - first_unchanged_at_end_row->y;
13464 run.current_y = first_unchanged_at_end_row->y;
13465 run.desired_y = run.current_y + dy;
13466 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13468 else
13470 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13471 first_unchanged_at_end_row = NULL;
13473 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13476 /* Find the cursor if not already found. We have to decide whether
13477 PT will appear on this window (it sometimes doesn't, but this is
13478 not a very frequent case.) This decision has to be made before
13479 the current matrix is altered. A value of cursor.vpos < 0 means
13480 that PT is either in one of the lines beginning at
13481 first_unchanged_at_end_row or below the window. Don't care for
13482 lines that might be displayed later at the window end; as
13483 mentioned, this is not a frequent case. */
13484 if (w->cursor.vpos < 0)
13486 /* Cursor in unchanged rows at the top? */
13487 if (PT < CHARPOS (start_pos)
13488 && last_unchanged_at_beg_row)
13490 row = row_containing_pos (w, PT,
13491 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13492 last_unchanged_at_beg_row + 1, 0);
13493 if (row)
13494 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13497 /* Start from first_unchanged_at_end_row looking for PT. */
13498 else if (first_unchanged_at_end_row)
13500 row = row_containing_pos (w, PT - delta,
13501 first_unchanged_at_end_row, NULL, 0);
13502 if (row)
13503 set_cursor_from_row (w, row, w->current_matrix, delta,
13504 delta_bytes, dy, dvpos);
13507 /* Give up if cursor was not found. */
13508 if (w->cursor.vpos < 0)
13510 clear_glyph_matrix (w->desired_matrix);
13511 return -1;
13515 /* Don't let the cursor end in the scroll margins. */
13517 int this_scroll_margin, cursor_height;
13519 this_scroll_margin = max (0, scroll_margin);
13520 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13521 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13522 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13524 if ((w->cursor.y < this_scroll_margin
13525 && CHARPOS (start) > BEGV)
13526 /* Old redisplay didn't take scroll margin into account at the bottom,
13527 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13528 || (w->cursor.y + (make_cursor_line_fully_visible_p
13529 ? cursor_height + this_scroll_margin
13530 : 1)) > it.last_visible_y)
13532 w->cursor.vpos = -1;
13533 clear_glyph_matrix (w->desired_matrix);
13534 return -1;
13538 /* Scroll the display. Do it before changing the current matrix so
13539 that xterm.c doesn't get confused about where the cursor glyph is
13540 found. */
13541 if (dy && run.height)
13543 update_begin (f);
13545 if (FRAME_WINDOW_P (f))
13547 rif->update_window_begin_hook (w);
13548 rif->clear_window_mouse_face (w);
13549 rif->scroll_run_hook (w, &run);
13550 rif->update_window_end_hook (w, 0, 0);
13552 else
13554 /* Terminal frame. In this case, dvpos gives the number of
13555 lines to scroll by; dvpos < 0 means scroll up. */
13556 int first_unchanged_at_end_vpos
13557 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13558 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13559 int end = (WINDOW_TOP_EDGE_LINE (w)
13560 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13561 + window_internal_height (w));
13563 /* Perform the operation on the screen. */
13564 if (dvpos > 0)
13566 /* Scroll last_unchanged_at_beg_row to the end of the
13567 window down dvpos lines. */
13568 set_terminal_window (end);
13570 /* On dumb terminals delete dvpos lines at the end
13571 before inserting dvpos empty lines. */
13572 if (!scroll_region_ok)
13573 ins_del_lines (end - dvpos, -dvpos);
13575 /* Insert dvpos empty lines in front of
13576 last_unchanged_at_beg_row. */
13577 ins_del_lines (from, dvpos);
13579 else if (dvpos < 0)
13581 /* Scroll up last_unchanged_at_beg_vpos to the end of
13582 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13583 set_terminal_window (end);
13585 /* Delete dvpos lines in front of
13586 last_unchanged_at_beg_vpos. ins_del_lines will set
13587 the cursor to the given vpos and emit |dvpos| delete
13588 line sequences. */
13589 ins_del_lines (from + dvpos, dvpos);
13591 /* On a dumb terminal insert dvpos empty lines at the
13592 end. */
13593 if (!scroll_region_ok)
13594 ins_del_lines (end + dvpos, -dvpos);
13597 set_terminal_window (0);
13600 update_end (f);
13603 /* Shift reused rows of the current matrix to the right position.
13604 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13605 text. */
13606 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13607 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13608 if (dvpos < 0)
13610 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13611 bottom_vpos, dvpos);
13612 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13613 bottom_vpos, 0);
13615 else if (dvpos > 0)
13617 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13618 bottom_vpos, dvpos);
13619 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13620 first_unchanged_at_end_vpos + dvpos, 0);
13623 /* For frame-based redisplay, make sure that current frame and window
13624 matrix are in sync with respect to glyph memory. */
13625 if (!FRAME_WINDOW_P (f))
13626 sync_frame_with_window_matrix_rows (w);
13628 /* Adjust buffer positions in reused rows. */
13629 if (delta)
13630 increment_matrix_positions (current_matrix,
13631 first_unchanged_at_end_vpos + dvpos,
13632 bottom_vpos, delta, delta_bytes);
13634 /* Adjust Y positions. */
13635 if (dy)
13636 shift_glyph_matrix (w, current_matrix,
13637 first_unchanged_at_end_vpos + dvpos,
13638 bottom_vpos, dy);
13640 if (first_unchanged_at_end_row)
13641 first_unchanged_at_end_row += dvpos;
13643 /* If scrolling up, there may be some lines to display at the end of
13644 the window. */
13645 last_text_row_at_end = NULL;
13646 if (dy < 0)
13648 /* Scrolling up can leave for example a partially visible line
13649 at the end of the window to be redisplayed. */
13650 /* Set last_row to the glyph row in the current matrix where the
13651 window end line is found. It has been moved up or down in
13652 the matrix by dvpos. */
13653 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13654 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13656 /* If last_row is the window end line, it should display text. */
13657 xassert (last_row->displays_text_p);
13659 /* If window end line was partially visible before, begin
13660 displaying at that line. Otherwise begin displaying with the
13661 line following it. */
13662 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13664 init_to_row_start (&it, w, last_row);
13665 it.vpos = last_vpos;
13666 it.current_y = last_row->y;
13668 else
13670 init_to_row_end (&it, w, last_row);
13671 it.vpos = 1 + last_vpos;
13672 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13673 ++last_row;
13676 /* We may start in a continuation line. If so, we have to
13677 get the right continuation_lines_width and current_x. */
13678 it.continuation_lines_width = last_row->continuation_lines_width;
13679 it.hpos = it.current_x = 0;
13681 /* Display the rest of the lines at the window end. */
13682 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13683 while (it.current_y < it.last_visible_y
13684 && !fonts_changed_p)
13686 /* Is it always sure that the display agrees with lines in
13687 the current matrix? I don't think so, so we mark rows
13688 displayed invalid in the current matrix by setting their
13689 enabled_p flag to zero. */
13690 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13691 if (display_line (&it))
13692 last_text_row_at_end = it.glyph_row - 1;
13696 /* Update window_end_pos and window_end_vpos. */
13697 if (first_unchanged_at_end_row
13698 && first_unchanged_at_end_row->y < it.last_visible_y
13699 && !last_text_row_at_end)
13701 /* Window end line if one of the preserved rows from the current
13702 matrix. Set row to the last row displaying text in current
13703 matrix starting at first_unchanged_at_end_row, after
13704 scrolling. */
13705 xassert (first_unchanged_at_end_row->displays_text_p);
13706 row = find_last_row_displaying_text (w->current_matrix, &it,
13707 first_unchanged_at_end_row);
13708 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13710 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13711 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13712 w->window_end_vpos
13713 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13714 xassert (w->window_end_bytepos >= 0);
13715 IF_DEBUG (debug_method_add (w, "A"));
13717 else if (last_text_row_at_end)
13719 w->window_end_pos
13720 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13721 w->window_end_bytepos
13722 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13723 w->window_end_vpos
13724 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13725 xassert (w->window_end_bytepos >= 0);
13726 IF_DEBUG (debug_method_add (w, "B"));
13728 else if (last_text_row)
13730 /* We have displayed either to the end of the window or at the
13731 end of the window, i.e. the last row with text is to be found
13732 in the desired matrix. */
13733 w->window_end_pos
13734 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13735 w->window_end_bytepos
13736 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13737 w->window_end_vpos
13738 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13739 xassert (w->window_end_bytepos >= 0);
13741 else if (first_unchanged_at_end_row == NULL
13742 && last_text_row == NULL
13743 && last_text_row_at_end == NULL)
13745 /* Displayed to end of window, but no line containing text was
13746 displayed. Lines were deleted at the end of the window. */
13747 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13748 int vpos = XFASTINT (w->window_end_vpos);
13749 struct glyph_row *current_row = current_matrix->rows + vpos;
13750 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13752 for (row = NULL;
13753 row == NULL && vpos >= first_vpos;
13754 --vpos, --current_row, --desired_row)
13756 if (desired_row->enabled_p)
13758 if (desired_row->displays_text_p)
13759 row = desired_row;
13761 else if (current_row->displays_text_p)
13762 row = current_row;
13765 xassert (row != NULL);
13766 w->window_end_vpos = make_number (vpos + 1);
13767 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13768 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13769 xassert (w->window_end_bytepos >= 0);
13770 IF_DEBUG (debug_method_add (w, "C"));
13772 else
13773 abort ();
13775 #if 0 /* This leads to problems, for instance when the cursor is
13776 at ZV, and the cursor line displays no text. */
13777 /* Disable rows below what's displayed in the window. This makes
13778 debugging easier. */
13779 enable_glyph_matrix_rows (current_matrix,
13780 XFASTINT (w->window_end_vpos) + 1,
13781 bottom_vpos, 0);
13782 #endif
13784 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13785 debug_end_vpos = XFASTINT (w->window_end_vpos));
13787 /* Record that display has not been completed. */
13788 w->window_end_valid = Qnil;
13789 w->desired_matrix->no_scrolling_p = 1;
13790 return 3;
13792 #undef GIVE_UP
13797 /***********************************************************************
13798 More debugging support
13799 ***********************************************************************/
13801 #if GLYPH_DEBUG
13803 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13804 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13805 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13808 /* Dump the contents of glyph matrix MATRIX on stderr.
13810 GLYPHS 0 means don't show glyph contents.
13811 GLYPHS 1 means show glyphs in short form
13812 GLYPHS > 1 means show glyphs in long form. */
13814 void
13815 dump_glyph_matrix (matrix, glyphs)
13816 struct glyph_matrix *matrix;
13817 int glyphs;
13819 int i;
13820 for (i = 0; i < matrix->nrows; ++i)
13821 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13825 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13826 the glyph row and area where the glyph comes from. */
13828 void
13829 dump_glyph (row, glyph, area)
13830 struct glyph_row *row;
13831 struct glyph *glyph;
13832 int area;
13834 if (glyph->type == CHAR_GLYPH)
13836 fprintf (stderr,
13837 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13838 glyph - row->glyphs[TEXT_AREA],
13839 'C',
13840 glyph->charpos,
13841 (BUFFERP (glyph->object)
13842 ? 'B'
13843 : (STRINGP (glyph->object)
13844 ? 'S'
13845 : '-')),
13846 glyph->pixel_width,
13847 glyph->u.ch,
13848 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13849 ? glyph->u.ch
13850 : '.'),
13851 glyph->face_id,
13852 glyph->left_box_line_p,
13853 glyph->right_box_line_p);
13855 else if (glyph->type == STRETCH_GLYPH)
13857 fprintf (stderr,
13858 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13859 glyph - row->glyphs[TEXT_AREA],
13860 'S',
13861 glyph->charpos,
13862 (BUFFERP (glyph->object)
13863 ? 'B'
13864 : (STRINGP (glyph->object)
13865 ? 'S'
13866 : '-')),
13867 glyph->pixel_width,
13869 '.',
13870 glyph->face_id,
13871 glyph->left_box_line_p,
13872 glyph->right_box_line_p);
13874 else if (glyph->type == IMAGE_GLYPH)
13876 fprintf (stderr,
13877 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13878 glyph - row->glyphs[TEXT_AREA],
13879 'I',
13880 glyph->charpos,
13881 (BUFFERP (glyph->object)
13882 ? 'B'
13883 : (STRINGP (glyph->object)
13884 ? 'S'
13885 : '-')),
13886 glyph->pixel_width,
13887 glyph->u.img_id,
13888 '.',
13889 glyph->face_id,
13890 glyph->left_box_line_p,
13891 glyph->right_box_line_p);
13896 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13897 GLYPHS 0 means don't show glyph contents.
13898 GLYPHS 1 means show glyphs in short form
13899 GLYPHS > 1 means show glyphs in long form. */
13901 void
13902 dump_glyph_row (row, vpos, glyphs)
13903 struct glyph_row *row;
13904 int vpos, glyphs;
13906 if (glyphs != 1)
13908 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13909 fprintf (stderr, "=======================================================================\n");
13911 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13912 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13913 vpos,
13914 MATRIX_ROW_START_CHARPOS (row),
13915 MATRIX_ROW_END_CHARPOS (row),
13916 row->used[TEXT_AREA],
13917 row->contains_overlapping_glyphs_p,
13918 row->enabled_p,
13919 row->truncated_on_left_p,
13920 row->truncated_on_right_p,
13921 row->overlay_arrow_p,
13922 row->continued_p,
13923 MATRIX_ROW_CONTINUATION_LINE_P (row),
13924 row->displays_text_p,
13925 row->ends_at_zv_p,
13926 row->fill_line_p,
13927 row->ends_in_middle_of_char_p,
13928 row->starts_in_middle_of_char_p,
13929 row->mouse_face_p,
13930 row->x,
13931 row->y,
13932 row->pixel_width,
13933 row->height,
13934 row->visible_height,
13935 row->ascent,
13936 row->phys_ascent);
13937 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13938 row->end.overlay_string_index,
13939 row->continuation_lines_width);
13940 fprintf (stderr, "%9d %5d\n",
13941 CHARPOS (row->start.string_pos),
13942 CHARPOS (row->end.string_pos));
13943 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13944 row->end.dpvec_index);
13947 if (glyphs > 1)
13949 int area;
13951 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13953 struct glyph *glyph = row->glyphs[area];
13954 struct glyph *glyph_end = glyph + row->used[area];
13956 /* Glyph for a line end in text. */
13957 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13958 ++glyph_end;
13960 if (glyph < glyph_end)
13961 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13963 for (; glyph < glyph_end; ++glyph)
13964 dump_glyph (row, glyph, area);
13967 else if (glyphs == 1)
13969 int area;
13971 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13973 char *s = (char *) alloca (row->used[area] + 1);
13974 int i;
13976 for (i = 0; i < row->used[area]; ++i)
13978 struct glyph *glyph = row->glyphs[area] + i;
13979 if (glyph->type == CHAR_GLYPH
13980 && glyph->u.ch < 0x80
13981 && glyph->u.ch >= ' ')
13982 s[i] = glyph->u.ch;
13983 else
13984 s[i] = '.';
13987 s[i] = '\0';
13988 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13994 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13995 Sdump_glyph_matrix, 0, 1, "p",
13996 doc: /* Dump the current matrix of the selected window to stderr.
13997 Shows contents of glyph row structures. With non-nil
13998 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13999 glyphs in short form, otherwise show glyphs in long form. */)
14000 (glyphs)
14001 Lisp_Object glyphs;
14003 struct window *w = XWINDOW (selected_window);
14004 struct buffer *buffer = XBUFFER (w->buffer);
14006 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14007 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14008 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14009 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14010 fprintf (stderr, "=============================================\n");
14011 dump_glyph_matrix (w->current_matrix,
14012 NILP (glyphs) ? 0 : XINT (glyphs));
14013 return Qnil;
14017 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14018 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14021 struct frame *f = XFRAME (selected_frame);
14022 dump_glyph_matrix (f->current_matrix, 1);
14023 return Qnil;
14027 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14028 doc: /* Dump glyph row ROW to stderr.
14029 GLYPH 0 means don't dump glyphs.
14030 GLYPH 1 means dump glyphs in short form.
14031 GLYPH > 1 or omitted means dump glyphs in long form. */)
14032 (row, glyphs)
14033 Lisp_Object row, glyphs;
14035 struct glyph_matrix *matrix;
14036 int vpos;
14038 CHECK_NUMBER (row);
14039 matrix = XWINDOW (selected_window)->current_matrix;
14040 vpos = XINT (row);
14041 if (vpos >= 0 && vpos < matrix->nrows)
14042 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14043 vpos,
14044 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14045 return Qnil;
14049 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14050 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14051 GLYPH 0 means don't dump glyphs.
14052 GLYPH 1 means dump glyphs in short form.
14053 GLYPH > 1 or omitted means dump glyphs in long form. */)
14054 (row, glyphs)
14055 Lisp_Object row, glyphs;
14057 struct frame *sf = SELECTED_FRAME ();
14058 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14059 int vpos;
14061 CHECK_NUMBER (row);
14062 vpos = XINT (row);
14063 if (vpos >= 0 && vpos < m->nrows)
14064 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14065 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14066 return Qnil;
14070 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14071 doc: /* Toggle tracing of redisplay.
14072 With ARG, turn tracing on if and only if ARG is positive. */)
14073 (arg)
14074 Lisp_Object arg;
14076 if (NILP (arg))
14077 trace_redisplay_p = !trace_redisplay_p;
14078 else
14080 arg = Fprefix_numeric_value (arg);
14081 trace_redisplay_p = XINT (arg) > 0;
14084 return Qnil;
14088 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14089 doc: /* Like `format', but print result to stderr.
14090 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14091 (nargs, args)
14092 int nargs;
14093 Lisp_Object *args;
14095 Lisp_Object s = Fformat (nargs, args);
14096 fprintf (stderr, "%s", SDATA (s));
14097 return Qnil;
14100 #endif /* GLYPH_DEBUG */
14104 /***********************************************************************
14105 Building Desired Matrix Rows
14106 ***********************************************************************/
14108 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14109 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14111 static struct glyph_row *
14112 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14113 struct window *w;
14114 Lisp_Object overlay_arrow_string;
14116 struct frame *f = XFRAME (WINDOW_FRAME (w));
14117 struct buffer *buffer = XBUFFER (w->buffer);
14118 struct buffer *old = current_buffer;
14119 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14120 int arrow_len = SCHARS (overlay_arrow_string);
14121 const unsigned char *arrow_end = arrow_string + arrow_len;
14122 const unsigned char *p;
14123 struct it it;
14124 int multibyte_p;
14125 int n_glyphs_before;
14127 set_buffer_temp (buffer);
14128 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14129 it.glyph_row->used[TEXT_AREA] = 0;
14130 SET_TEXT_POS (it.position, 0, 0);
14132 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14133 p = arrow_string;
14134 while (p < arrow_end)
14136 Lisp_Object face, ilisp;
14138 /* Get the next character. */
14139 if (multibyte_p)
14140 it.c = string_char_and_length (p, arrow_len, &it.len);
14141 else
14142 it.c = *p, it.len = 1;
14143 p += it.len;
14145 /* Get its face. */
14146 ilisp = make_number (p - arrow_string);
14147 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14148 it.face_id = compute_char_face (f, it.c, face);
14150 /* Compute its width, get its glyphs. */
14151 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14152 SET_TEXT_POS (it.position, -1, -1);
14153 PRODUCE_GLYPHS (&it);
14155 /* If this character doesn't fit any more in the line, we have
14156 to remove some glyphs. */
14157 if (it.current_x > it.last_visible_x)
14159 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14160 break;
14164 set_buffer_temp (old);
14165 return it.glyph_row;
14169 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14170 glyphs are only inserted for terminal frames since we can't really
14171 win with truncation glyphs when partially visible glyphs are
14172 involved. Which glyphs to insert is determined by
14173 produce_special_glyphs. */
14175 static void
14176 insert_left_trunc_glyphs (it)
14177 struct it *it;
14179 struct it truncate_it;
14180 struct glyph *from, *end, *to, *toend;
14182 xassert (!FRAME_WINDOW_P (it->f));
14184 /* Get the truncation glyphs. */
14185 truncate_it = *it;
14186 truncate_it.current_x = 0;
14187 truncate_it.face_id = DEFAULT_FACE_ID;
14188 truncate_it.glyph_row = &scratch_glyph_row;
14189 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14190 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14191 truncate_it.object = make_number (0);
14192 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14194 /* Overwrite glyphs from IT with truncation glyphs. */
14195 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14196 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14197 to = it->glyph_row->glyphs[TEXT_AREA];
14198 toend = to + it->glyph_row->used[TEXT_AREA];
14200 while (from < end)
14201 *to++ = *from++;
14203 /* There may be padding glyphs left over. Overwrite them too. */
14204 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14206 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14207 while (from < end)
14208 *to++ = *from++;
14211 if (to > toend)
14212 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14216 /* Compute the pixel height and width of IT->glyph_row.
14218 Most of the time, ascent and height of a display line will be equal
14219 to the max_ascent and max_height values of the display iterator
14220 structure. This is not the case if
14222 1. We hit ZV without displaying anything. In this case, max_ascent
14223 and max_height will be zero.
14225 2. We have some glyphs that don't contribute to the line height.
14226 (The glyph row flag contributes_to_line_height_p is for future
14227 pixmap extensions).
14229 The first case is easily covered by using default values because in
14230 these cases, the line height does not really matter, except that it
14231 must not be zero. */
14233 static void
14234 compute_line_metrics (it)
14235 struct it *it;
14237 struct glyph_row *row = it->glyph_row;
14238 int area, i;
14240 if (FRAME_WINDOW_P (it->f))
14242 int i, min_y, max_y;
14244 /* The line may consist of one space only, that was added to
14245 place the cursor on it. If so, the row's height hasn't been
14246 computed yet. */
14247 if (row->height == 0)
14249 if (it->max_ascent + it->max_descent == 0)
14250 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14251 row->ascent = it->max_ascent;
14252 row->height = it->max_ascent + it->max_descent;
14253 row->phys_ascent = it->max_phys_ascent;
14254 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14255 row->extra_line_spacing = it->max_extra_line_spacing;
14258 /* Compute the width of this line. */
14259 row->pixel_width = row->x;
14260 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14261 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14263 xassert (row->pixel_width >= 0);
14264 xassert (row->ascent >= 0 && row->height > 0);
14266 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14267 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14269 /* If first line's physical ascent is larger than its logical
14270 ascent, use the physical ascent, and make the row taller.
14271 This makes accented characters fully visible. */
14272 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14273 && row->phys_ascent > row->ascent)
14275 row->height += row->phys_ascent - row->ascent;
14276 row->ascent = row->phys_ascent;
14279 /* Compute how much of the line is visible. */
14280 row->visible_height = row->height;
14282 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14283 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14285 if (row->y < min_y)
14286 row->visible_height -= min_y - row->y;
14287 if (row->y + row->height > max_y)
14288 row->visible_height -= row->y + row->height - max_y;
14290 else
14292 row->pixel_width = row->used[TEXT_AREA];
14293 if (row->continued_p)
14294 row->pixel_width -= it->continuation_pixel_width;
14295 else if (row->truncated_on_right_p)
14296 row->pixel_width -= it->truncation_pixel_width;
14297 row->ascent = row->phys_ascent = 0;
14298 row->height = row->phys_height = row->visible_height = 1;
14299 row->extra_line_spacing = 0;
14302 /* Compute a hash code for this row. */
14303 row->hash = 0;
14304 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14305 for (i = 0; i < row->used[area]; ++i)
14306 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14307 + row->glyphs[area][i].u.val
14308 + row->glyphs[area][i].face_id
14309 + row->glyphs[area][i].padding_p
14310 + (row->glyphs[area][i].type << 2));
14312 it->max_ascent = it->max_descent = 0;
14313 it->max_phys_ascent = it->max_phys_descent = 0;
14317 /* Append one space to the glyph row of iterator IT if doing a
14318 window-based redisplay. The space has the same face as
14319 IT->face_id. Value is non-zero if a space was added.
14321 This function is called to make sure that there is always one glyph
14322 at the end of a glyph row that the cursor can be set on under
14323 window-systems. (If there weren't such a glyph we would not know
14324 how wide and tall a box cursor should be displayed).
14326 At the same time this space let's a nicely handle clearing to the
14327 end of the line if the row ends in italic text. */
14329 static int
14330 append_space_for_newline (it, default_face_p)
14331 struct it *it;
14332 int default_face_p;
14334 if (FRAME_WINDOW_P (it->f))
14336 int n = it->glyph_row->used[TEXT_AREA];
14338 if (it->glyph_row->glyphs[TEXT_AREA] + n
14339 < it->glyph_row->glyphs[1 + TEXT_AREA])
14341 /* Save some values that must not be changed.
14342 Must save IT->c and IT->len because otherwise
14343 ITERATOR_AT_END_P wouldn't work anymore after
14344 append_space_for_newline has been called. */
14345 enum display_element_type saved_what = it->what;
14346 int saved_c = it->c, saved_len = it->len;
14347 int saved_x = it->current_x;
14348 int saved_face_id = it->face_id;
14349 struct text_pos saved_pos;
14350 Lisp_Object saved_object;
14351 struct face *face;
14353 saved_object = it->object;
14354 saved_pos = it->position;
14356 it->what = IT_CHARACTER;
14357 bzero (&it->position, sizeof it->position);
14358 it->object = make_number (0);
14359 it->c = ' ';
14360 it->len = 1;
14362 if (default_face_p)
14363 it->face_id = DEFAULT_FACE_ID;
14364 else if (it->face_before_selective_p)
14365 it->face_id = it->saved_face_id;
14366 face = FACE_FROM_ID (it->f, it->face_id);
14367 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14369 PRODUCE_GLYPHS (it);
14371 it->override_ascent = -1;
14372 it->constrain_row_ascent_descent_p = 0;
14373 it->current_x = saved_x;
14374 it->object = saved_object;
14375 it->position = saved_pos;
14376 it->what = saved_what;
14377 it->face_id = saved_face_id;
14378 it->len = saved_len;
14379 it->c = saved_c;
14380 return 1;
14384 return 0;
14388 /* Extend the face of the last glyph in the text area of IT->glyph_row
14389 to the end of the display line. Called from display_line.
14390 If the glyph row is empty, add a space glyph to it so that we
14391 know the face to draw. Set the glyph row flag fill_line_p. */
14393 static void
14394 extend_face_to_end_of_line (it)
14395 struct it *it;
14397 struct face *face;
14398 struct frame *f = it->f;
14400 /* If line is already filled, do nothing. */
14401 if (it->current_x >= it->last_visible_x)
14402 return;
14404 /* Face extension extends the background and box of IT->face_id
14405 to the end of the line. If the background equals the background
14406 of the frame, we don't have to do anything. */
14407 if (it->face_before_selective_p)
14408 face = FACE_FROM_ID (it->f, it->saved_face_id);
14409 else
14410 face = FACE_FROM_ID (f, it->face_id);
14412 if (FRAME_WINDOW_P (f)
14413 && face->box == FACE_NO_BOX
14414 && face->background == FRAME_BACKGROUND_PIXEL (f)
14415 && !face->stipple)
14416 return;
14418 /* Set the glyph row flag indicating that the face of the last glyph
14419 in the text area has to be drawn to the end of the text area. */
14420 it->glyph_row->fill_line_p = 1;
14422 /* If current character of IT is not ASCII, make sure we have the
14423 ASCII face. This will be automatically undone the next time
14424 get_next_display_element returns a multibyte character. Note
14425 that the character will always be single byte in unibyte text. */
14426 if (!SINGLE_BYTE_CHAR_P (it->c))
14428 it->face_id = FACE_FOR_CHAR (f, face, 0);
14431 if (FRAME_WINDOW_P (f))
14433 /* If the row is empty, add a space with the current face of IT,
14434 so that we know which face to draw. */
14435 if (it->glyph_row->used[TEXT_AREA] == 0)
14437 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14438 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14439 it->glyph_row->used[TEXT_AREA] = 1;
14442 else
14444 /* Save some values that must not be changed. */
14445 int saved_x = it->current_x;
14446 struct text_pos saved_pos;
14447 Lisp_Object saved_object;
14448 enum display_element_type saved_what = it->what;
14449 int saved_face_id = it->face_id;
14451 saved_object = it->object;
14452 saved_pos = it->position;
14454 it->what = IT_CHARACTER;
14455 bzero (&it->position, sizeof it->position);
14456 it->object = make_number (0);
14457 it->c = ' ';
14458 it->len = 1;
14459 it->face_id = face->id;
14461 PRODUCE_GLYPHS (it);
14463 while (it->current_x <= it->last_visible_x)
14464 PRODUCE_GLYPHS (it);
14466 /* Don't count these blanks really. It would let us insert a left
14467 truncation glyph below and make us set the cursor on them, maybe. */
14468 it->current_x = saved_x;
14469 it->object = saved_object;
14470 it->position = saved_pos;
14471 it->what = saved_what;
14472 it->face_id = saved_face_id;
14477 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14478 trailing whitespace. */
14480 static int
14481 trailing_whitespace_p (charpos)
14482 int charpos;
14484 int bytepos = CHAR_TO_BYTE (charpos);
14485 int c = 0;
14487 while (bytepos < ZV_BYTE
14488 && (c = FETCH_CHAR (bytepos),
14489 c == ' ' || c == '\t'))
14490 ++bytepos;
14492 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14494 if (bytepos != PT_BYTE)
14495 return 1;
14497 return 0;
14501 /* Highlight trailing whitespace, if any, in ROW. */
14503 void
14504 highlight_trailing_whitespace (f, row)
14505 struct frame *f;
14506 struct glyph_row *row;
14508 int used = row->used[TEXT_AREA];
14510 if (used)
14512 struct glyph *start = row->glyphs[TEXT_AREA];
14513 struct glyph *glyph = start + used - 1;
14515 /* Skip over glyphs inserted to display the cursor at the
14516 end of a line, for extending the face of the last glyph
14517 to the end of the line on terminals, and for truncation
14518 and continuation glyphs. */
14519 while (glyph >= start
14520 && glyph->type == CHAR_GLYPH
14521 && INTEGERP (glyph->object))
14522 --glyph;
14524 /* If last glyph is a space or stretch, and it's trailing
14525 whitespace, set the face of all trailing whitespace glyphs in
14526 IT->glyph_row to `trailing-whitespace'. */
14527 if (glyph >= start
14528 && BUFFERP (glyph->object)
14529 && (glyph->type == STRETCH_GLYPH
14530 || (glyph->type == CHAR_GLYPH
14531 && glyph->u.ch == ' '))
14532 && trailing_whitespace_p (glyph->charpos))
14534 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14536 while (glyph >= start
14537 && BUFFERP (glyph->object)
14538 && (glyph->type == STRETCH_GLYPH
14539 || (glyph->type == CHAR_GLYPH
14540 && glyph->u.ch == ' ')))
14541 (glyph--)->face_id = face_id;
14547 /* Value is non-zero if glyph row ROW in window W should be
14548 used to hold the cursor. */
14550 static int
14551 cursor_row_p (w, row)
14552 struct window *w;
14553 struct glyph_row *row;
14555 int cursor_row_p = 1;
14557 if (PT == MATRIX_ROW_END_CHARPOS (row))
14559 /* If the row ends with a newline from a string, we don't want
14560 the cursor there (if the row is continued it doesn't end in a
14561 newline). */
14562 if (CHARPOS (row->end.string_pos) >= 0
14563 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14564 cursor_row_p = row->continued_p;
14566 /* If the row ends at ZV, display the cursor at the end of that
14567 row instead of at the start of the row below. */
14568 else if (row->ends_at_zv_p)
14569 cursor_row_p = 1;
14570 else
14571 cursor_row_p = 0;
14574 return cursor_row_p;
14578 /* Construct the glyph row IT->glyph_row in the desired matrix of
14579 IT->w from text at the current position of IT. See dispextern.h
14580 for an overview of struct it. Value is non-zero if
14581 IT->glyph_row displays text, as opposed to a line displaying ZV
14582 only. */
14584 static int
14585 display_line (it)
14586 struct it *it;
14588 struct glyph_row *row = it->glyph_row;
14589 int overlay_arrow_bitmap;
14590 Lisp_Object overlay_arrow_string;
14592 /* We always start displaying at hpos zero even if hscrolled. */
14593 xassert (it->hpos == 0 && it->current_x == 0);
14595 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14596 >= it->w->desired_matrix->nrows)
14598 it->w->nrows_scale_factor++;
14599 fonts_changed_p = 1;
14600 return 0;
14603 /* Is IT->w showing the region? */
14604 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14606 /* Clear the result glyph row and enable it. */
14607 prepare_desired_row (row);
14609 row->y = it->current_y;
14610 row->start = it->start;
14611 row->continuation_lines_width = it->continuation_lines_width;
14612 row->displays_text_p = 1;
14613 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14614 it->starts_in_middle_of_char_p = 0;
14616 /* Arrange the overlays nicely for our purposes. Usually, we call
14617 display_line on only one line at a time, in which case this
14618 can't really hurt too much, or we call it on lines which appear
14619 one after another in the buffer, in which case all calls to
14620 recenter_overlay_lists but the first will be pretty cheap. */
14621 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14623 /* Move over display elements that are not visible because we are
14624 hscrolled. This may stop at an x-position < IT->first_visible_x
14625 if the first glyph is partially visible or if we hit a line end. */
14626 if (it->current_x < it->first_visible_x)
14627 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14628 MOVE_TO_POS | MOVE_TO_X);
14630 /* Get the initial row height. This is either the height of the
14631 text hscrolled, if there is any, or zero. */
14632 row->ascent = it->max_ascent;
14633 row->height = it->max_ascent + it->max_descent;
14634 row->phys_ascent = it->max_phys_ascent;
14635 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14636 row->extra_line_spacing = it->max_extra_line_spacing;
14638 /* Loop generating characters. The loop is left with IT on the next
14639 character to display. */
14640 while (1)
14642 int n_glyphs_before, hpos_before, x_before;
14643 int x, i, nglyphs;
14644 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14646 /* Retrieve the next thing to display. Value is zero if end of
14647 buffer reached. */
14648 if (!get_next_display_element (it))
14650 /* Maybe add a space at the end of this line that is used to
14651 display the cursor there under X. Set the charpos of the
14652 first glyph of blank lines not corresponding to any text
14653 to -1. */
14654 #ifdef HAVE_WINDOW_SYSTEM
14655 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14656 row->exact_window_width_line_p = 1;
14657 else
14658 #endif /* HAVE_WINDOW_SYSTEM */
14659 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14660 || row->used[TEXT_AREA] == 0)
14662 row->glyphs[TEXT_AREA]->charpos = -1;
14663 row->displays_text_p = 0;
14665 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14666 && (!MINI_WINDOW_P (it->w)
14667 || (minibuf_level && EQ (it->window, minibuf_window))))
14668 row->indicate_empty_line_p = 1;
14671 it->continuation_lines_width = 0;
14672 row->ends_at_zv_p = 1;
14673 break;
14676 /* Now, get the metrics of what we want to display. This also
14677 generates glyphs in `row' (which is IT->glyph_row). */
14678 n_glyphs_before = row->used[TEXT_AREA];
14679 x = it->current_x;
14681 /* Remember the line height so far in case the next element doesn't
14682 fit on the line. */
14683 if (!it->truncate_lines_p)
14685 ascent = it->max_ascent;
14686 descent = it->max_descent;
14687 phys_ascent = it->max_phys_ascent;
14688 phys_descent = it->max_phys_descent;
14691 PRODUCE_GLYPHS (it);
14693 /* If this display element was in marginal areas, continue with
14694 the next one. */
14695 if (it->area != TEXT_AREA)
14697 row->ascent = max (row->ascent, it->max_ascent);
14698 row->height = max (row->height, it->max_ascent + it->max_descent);
14699 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14700 row->phys_height = max (row->phys_height,
14701 it->max_phys_ascent + it->max_phys_descent);
14702 row->extra_line_spacing = max (row->extra_line_spacing,
14703 it->max_extra_line_spacing);
14704 set_iterator_to_next (it, 1);
14705 continue;
14708 /* Does the display element fit on the line? If we truncate
14709 lines, we should draw past the right edge of the window. If
14710 we don't truncate, we want to stop so that we can display the
14711 continuation glyph before the right margin. If lines are
14712 continued, there are two possible strategies for characters
14713 resulting in more than 1 glyph (e.g. tabs): Display as many
14714 glyphs as possible in this line and leave the rest for the
14715 continuation line, or display the whole element in the next
14716 line. Original redisplay did the former, so we do it also. */
14717 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14718 hpos_before = it->hpos;
14719 x_before = x;
14721 if (/* Not a newline. */
14722 nglyphs > 0
14723 /* Glyphs produced fit entirely in the line. */
14724 && it->current_x < it->last_visible_x)
14726 it->hpos += nglyphs;
14727 row->ascent = max (row->ascent, it->max_ascent);
14728 row->height = max (row->height, it->max_ascent + it->max_descent);
14729 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14730 row->phys_height = max (row->phys_height,
14731 it->max_phys_ascent + it->max_phys_descent);
14732 row->extra_line_spacing = max (row->extra_line_spacing,
14733 it->max_extra_line_spacing);
14734 if (it->current_x - it->pixel_width < it->first_visible_x)
14735 row->x = x - it->first_visible_x;
14737 else
14739 int new_x;
14740 struct glyph *glyph;
14742 for (i = 0; i < nglyphs; ++i, x = new_x)
14744 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14745 new_x = x + glyph->pixel_width;
14747 if (/* Lines are continued. */
14748 !it->truncate_lines_p
14749 && (/* Glyph doesn't fit on the line. */
14750 new_x > it->last_visible_x
14751 /* Or it fits exactly on a window system frame. */
14752 || (new_x == it->last_visible_x
14753 && FRAME_WINDOW_P (it->f))))
14755 /* End of a continued line. */
14757 if (it->hpos == 0
14758 || (new_x == it->last_visible_x
14759 && FRAME_WINDOW_P (it->f)))
14761 /* Current glyph is the only one on the line or
14762 fits exactly on the line. We must continue
14763 the line because we can't draw the cursor
14764 after the glyph. */
14765 row->continued_p = 1;
14766 it->current_x = new_x;
14767 it->continuation_lines_width += new_x;
14768 ++it->hpos;
14769 if (i == nglyphs - 1)
14771 set_iterator_to_next (it, 1);
14772 #ifdef HAVE_WINDOW_SYSTEM
14773 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14775 if (!get_next_display_element (it))
14777 row->exact_window_width_line_p = 1;
14778 it->continuation_lines_width = 0;
14779 row->continued_p = 0;
14780 row->ends_at_zv_p = 1;
14782 else if (ITERATOR_AT_END_OF_LINE_P (it))
14784 row->continued_p = 0;
14785 row->exact_window_width_line_p = 1;
14788 #endif /* HAVE_WINDOW_SYSTEM */
14791 else if (CHAR_GLYPH_PADDING_P (*glyph)
14792 && !FRAME_WINDOW_P (it->f))
14794 /* A padding glyph that doesn't fit on this line.
14795 This means the whole character doesn't fit
14796 on the line. */
14797 row->used[TEXT_AREA] = n_glyphs_before;
14799 /* Fill the rest of the row with continuation
14800 glyphs like in 20.x. */
14801 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14802 < row->glyphs[1 + TEXT_AREA])
14803 produce_special_glyphs (it, IT_CONTINUATION);
14805 row->continued_p = 1;
14806 it->current_x = x_before;
14807 it->continuation_lines_width += x_before;
14809 /* Restore the height to what it was before the
14810 element not fitting on the line. */
14811 it->max_ascent = ascent;
14812 it->max_descent = descent;
14813 it->max_phys_ascent = phys_ascent;
14814 it->max_phys_descent = phys_descent;
14816 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14818 /* A TAB that extends past the right edge of the
14819 window. This produces a single glyph on
14820 window system frames. We leave the glyph in
14821 this row and let it fill the row, but don't
14822 consume the TAB. */
14823 it->continuation_lines_width += it->last_visible_x;
14824 row->ends_in_middle_of_char_p = 1;
14825 row->continued_p = 1;
14826 glyph->pixel_width = it->last_visible_x - x;
14827 it->starts_in_middle_of_char_p = 1;
14829 else
14831 /* Something other than a TAB that draws past
14832 the right edge of the window. Restore
14833 positions to values before the element. */
14834 row->used[TEXT_AREA] = n_glyphs_before + i;
14836 /* Display continuation glyphs. */
14837 if (!FRAME_WINDOW_P (it->f))
14838 produce_special_glyphs (it, IT_CONTINUATION);
14839 row->continued_p = 1;
14841 it->continuation_lines_width += x;
14843 if (nglyphs > 1 && i > 0)
14845 row->ends_in_middle_of_char_p = 1;
14846 it->starts_in_middle_of_char_p = 1;
14849 /* Restore the height to what it was before the
14850 element not fitting on the line. */
14851 it->max_ascent = ascent;
14852 it->max_descent = descent;
14853 it->max_phys_ascent = phys_ascent;
14854 it->max_phys_descent = phys_descent;
14857 break;
14859 else if (new_x > it->first_visible_x)
14861 /* Increment number of glyphs actually displayed. */
14862 ++it->hpos;
14864 if (x < it->first_visible_x)
14865 /* Glyph is partially visible, i.e. row starts at
14866 negative X position. */
14867 row->x = x - it->first_visible_x;
14869 else
14871 /* Glyph is completely off the left margin of the
14872 window. This should not happen because of the
14873 move_it_in_display_line at the start of this
14874 function, unless the text display area of the
14875 window is empty. */
14876 xassert (it->first_visible_x <= it->last_visible_x);
14880 row->ascent = max (row->ascent, it->max_ascent);
14881 row->height = max (row->height, it->max_ascent + it->max_descent);
14882 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14883 row->phys_height = max (row->phys_height,
14884 it->max_phys_ascent + it->max_phys_descent);
14885 row->extra_line_spacing = max (row->extra_line_spacing,
14886 it->max_extra_line_spacing);
14888 /* End of this display line if row is continued. */
14889 if (row->continued_p || row->ends_at_zv_p)
14890 break;
14893 at_end_of_line:
14894 /* Is this a line end? If yes, we're also done, after making
14895 sure that a non-default face is extended up to the right
14896 margin of the window. */
14897 if (ITERATOR_AT_END_OF_LINE_P (it))
14899 int used_before = row->used[TEXT_AREA];
14901 row->ends_in_newline_from_string_p = STRINGP (it->object);
14903 #ifdef HAVE_WINDOW_SYSTEM
14904 /* Add a space at the end of the line that is used to
14905 display the cursor there. */
14906 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14907 append_space_for_newline (it, 0);
14908 #endif /* HAVE_WINDOW_SYSTEM */
14910 /* Extend the face to the end of the line. */
14911 extend_face_to_end_of_line (it);
14913 /* Make sure we have the position. */
14914 if (used_before == 0)
14915 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14917 /* Consume the line end. This skips over invisible lines. */
14918 set_iterator_to_next (it, 1);
14919 it->continuation_lines_width = 0;
14920 break;
14923 /* Proceed with next display element. Note that this skips
14924 over lines invisible because of selective display. */
14925 set_iterator_to_next (it, 1);
14927 /* If we truncate lines, we are done when the last displayed
14928 glyphs reach past the right margin of the window. */
14929 if (it->truncate_lines_p
14930 && (FRAME_WINDOW_P (it->f)
14931 ? (it->current_x >= it->last_visible_x)
14932 : (it->current_x > it->last_visible_x)))
14934 /* Maybe add truncation glyphs. */
14935 if (!FRAME_WINDOW_P (it->f))
14937 int i, n;
14939 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14940 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14941 break;
14943 for (n = row->used[TEXT_AREA]; i < n; ++i)
14945 row->used[TEXT_AREA] = i;
14946 produce_special_glyphs (it, IT_TRUNCATION);
14949 #ifdef HAVE_WINDOW_SYSTEM
14950 else
14952 /* Don't truncate if we can overflow newline into fringe. */
14953 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14955 if (!get_next_display_element (it))
14957 #ifdef HAVE_WINDOW_SYSTEM
14958 it->continuation_lines_width = 0;
14959 row->ends_at_zv_p = 1;
14960 row->exact_window_width_line_p = 1;
14961 break;
14962 #endif /* HAVE_WINDOW_SYSTEM */
14964 if (ITERATOR_AT_END_OF_LINE_P (it))
14966 row->exact_window_width_line_p = 1;
14967 goto at_end_of_line;
14971 #endif /* HAVE_WINDOW_SYSTEM */
14973 row->truncated_on_right_p = 1;
14974 it->continuation_lines_width = 0;
14975 reseat_at_next_visible_line_start (it, 0);
14976 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14977 it->hpos = hpos_before;
14978 it->current_x = x_before;
14979 break;
14983 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14984 at the left window margin. */
14985 if (it->first_visible_x
14986 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14988 if (!FRAME_WINDOW_P (it->f))
14989 insert_left_trunc_glyphs (it);
14990 row->truncated_on_left_p = 1;
14993 /* If the start of this line is the overlay arrow-position, then
14994 mark this glyph row as the one containing the overlay arrow.
14995 This is clearly a mess with variable size fonts. It would be
14996 better to let it be displayed like cursors under X. */
14997 if (! overlay_arrow_seen
14998 && (overlay_arrow_string
14999 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15000 !NILP (overlay_arrow_string)))
15002 /* Overlay arrow in window redisplay is a fringe bitmap. */
15003 if (STRINGP (overlay_arrow_string))
15005 struct glyph_row *arrow_row
15006 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15007 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15008 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15009 struct glyph *p = row->glyphs[TEXT_AREA];
15010 struct glyph *p2, *end;
15012 /* Copy the arrow glyphs. */
15013 while (glyph < arrow_end)
15014 *p++ = *glyph++;
15016 /* Throw away padding glyphs. */
15017 p2 = p;
15018 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15019 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15020 ++p2;
15021 if (p2 > p)
15023 while (p2 < end)
15024 *p++ = *p2++;
15025 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15028 else
15030 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15031 row->overlay_arrow_p = 1;
15033 overlay_arrow_seen = 1;
15036 /* Compute pixel dimensions of this line. */
15037 compute_line_metrics (it);
15039 /* Remember the position at which this line ends. */
15040 row->end = it->current;
15042 /* Save fringe bitmaps in this row. */
15043 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15044 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15045 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15046 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15048 it->left_user_fringe_bitmap = 0;
15049 it->left_user_fringe_face_id = 0;
15050 it->right_user_fringe_bitmap = 0;
15051 it->right_user_fringe_face_id = 0;
15053 /* Maybe set the cursor. */
15054 if (it->w->cursor.vpos < 0
15055 && PT >= MATRIX_ROW_START_CHARPOS (row)
15056 && PT <= MATRIX_ROW_END_CHARPOS (row)
15057 && cursor_row_p (it->w, row))
15058 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15060 /* Highlight trailing whitespace. */
15061 if (!NILP (Vshow_trailing_whitespace))
15062 highlight_trailing_whitespace (it->f, it->glyph_row);
15064 /* Prepare for the next line. This line starts horizontally at (X
15065 HPOS) = (0 0). Vertical positions are incremented. As a
15066 convenience for the caller, IT->glyph_row is set to the next
15067 row to be used. */
15068 it->current_x = it->hpos = 0;
15069 it->current_y += row->height;
15070 ++it->vpos;
15071 ++it->glyph_row;
15072 it->start = it->current;
15073 return row->displays_text_p;
15078 /***********************************************************************
15079 Menu Bar
15080 ***********************************************************************/
15082 /* Redisplay the menu bar in the frame for window W.
15084 The menu bar of X frames that don't have X toolkit support is
15085 displayed in a special window W->frame->menu_bar_window.
15087 The menu bar of terminal frames is treated specially as far as
15088 glyph matrices are concerned. Menu bar lines are not part of
15089 windows, so the update is done directly on the frame matrix rows
15090 for the menu bar. */
15092 static void
15093 display_menu_bar (w)
15094 struct window *w;
15096 struct frame *f = XFRAME (WINDOW_FRAME (w));
15097 struct it it;
15098 Lisp_Object items;
15099 int i;
15101 /* Don't do all this for graphical frames. */
15102 #ifdef HAVE_NTGUI
15103 if (!NILP (Vwindow_system))
15104 return;
15105 #endif
15106 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15107 if (FRAME_X_P (f))
15108 return;
15109 #endif
15110 #ifdef MAC_OS
15111 if (FRAME_MAC_P (f))
15112 return;
15113 #endif
15115 #ifdef USE_X_TOOLKIT
15116 xassert (!FRAME_WINDOW_P (f));
15117 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15118 it.first_visible_x = 0;
15119 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15120 #else /* not USE_X_TOOLKIT */
15121 if (FRAME_WINDOW_P (f))
15123 /* Menu bar lines are displayed in the desired matrix of the
15124 dummy window menu_bar_window. */
15125 struct window *menu_w;
15126 xassert (WINDOWP (f->menu_bar_window));
15127 menu_w = XWINDOW (f->menu_bar_window);
15128 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15129 MENU_FACE_ID);
15130 it.first_visible_x = 0;
15131 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15133 else
15135 /* This is a TTY frame, i.e. character hpos/vpos are used as
15136 pixel x/y. */
15137 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15138 MENU_FACE_ID);
15139 it.first_visible_x = 0;
15140 it.last_visible_x = FRAME_COLS (f);
15142 #endif /* not USE_X_TOOLKIT */
15144 if (! mode_line_inverse_video)
15145 /* Force the menu-bar to be displayed in the default face. */
15146 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15148 /* Clear all rows of the menu bar. */
15149 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15151 struct glyph_row *row = it.glyph_row + i;
15152 clear_glyph_row (row);
15153 row->enabled_p = 1;
15154 row->full_width_p = 1;
15157 /* Display all items of the menu bar. */
15158 items = FRAME_MENU_BAR_ITEMS (it.f);
15159 for (i = 0; i < XVECTOR (items)->size; i += 4)
15161 Lisp_Object string;
15163 /* Stop at nil string. */
15164 string = AREF (items, i + 1);
15165 if (NILP (string))
15166 break;
15168 /* Remember where item was displayed. */
15169 AREF (items, i + 3) = make_number (it.hpos);
15171 /* Display the item, pad with one space. */
15172 if (it.current_x < it.last_visible_x)
15173 display_string (NULL, string, Qnil, 0, 0, &it,
15174 SCHARS (string) + 1, 0, 0, -1);
15177 /* Fill out the line with spaces. */
15178 if (it.current_x < it.last_visible_x)
15179 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15181 /* Compute the total height of the lines. */
15182 compute_line_metrics (&it);
15187 /***********************************************************************
15188 Mode Line
15189 ***********************************************************************/
15191 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15192 FORCE is non-zero, redisplay mode lines unconditionally.
15193 Otherwise, redisplay only mode lines that are garbaged. Value is
15194 the number of windows whose mode lines were redisplayed. */
15196 static int
15197 redisplay_mode_lines (window, force)
15198 Lisp_Object window;
15199 int force;
15201 int nwindows = 0;
15203 while (!NILP (window))
15205 struct window *w = XWINDOW (window);
15207 if (WINDOWP (w->hchild))
15208 nwindows += redisplay_mode_lines (w->hchild, force);
15209 else if (WINDOWP (w->vchild))
15210 nwindows += redisplay_mode_lines (w->vchild, force);
15211 else if (force
15212 || FRAME_GARBAGED_P (XFRAME (w->frame))
15213 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15215 struct text_pos lpoint;
15216 struct buffer *old = current_buffer;
15218 /* Set the window's buffer for the mode line display. */
15219 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15220 set_buffer_internal_1 (XBUFFER (w->buffer));
15222 /* Point refers normally to the selected window. For any
15223 other window, set up appropriate value. */
15224 if (!EQ (window, selected_window))
15226 struct text_pos pt;
15228 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15229 if (CHARPOS (pt) < BEGV)
15230 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15231 else if (CHARPOS (pt) > (ZV - 1))
15232 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15233 else
15234 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15237 /* Display mode lines. */
15238 clear_glyph_matrix (w->desired_matrix);
15239 if (display_mode_lines (w))
15241 ++nwindows;
15242 w->must_be_updated_p = 1;
15245 /* Restore old settings. */
15246 set_buffer_internal_1 (old);
15247 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15250 window = w->next;
15253 return nwindows;
15257 /* Display the mode and/or top line of window W. Value is the number
15258 of mode lines displayed. */
15260 static int
15261 display_mode_lines (w)
15262 struct window *w;
15264 Lisp_Object old_selected_window, old_selected_frame;
15265 int n = 0;
15267 old_selected_frame = selected_frame;
15268 selected_frame = w->frame;
15269 old_selected_window = selected_window;
15270 XSETWINDOW (selected_window, w);
15272 /* These will be set while the mode line specs are processed. */
15273 line_number_displayed = 0;
15274 w->column_number_displayed = Qnil;
15276 if (WINDOW_WANTS_MODELINE_P (w))
15278 struct window *sel_w = XWINDOW (old_selected_window);
15280 /* Select mode line face based on the real selected window. */
15281 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15282 current_buffer->mode_line_format);
15283 ++n;
15286 if (WINDOW_WANTS_HEADER_LINE_P (w))
15288 display_mode_line (w, HEADER_LINE_FACE_ID,
15289 current_buffer->header_line_format);
15290 ++n;
15293 selected_frame = old_selected_frame;
15294 selected_window = old_selected_window;
15295 return n;
15299 /* Display mode or top line of window W. FACE_ID specifies which line
15300 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15301 FORMAT is the mode line format to display. Value is the pixel
15302 height of the mode line displayed. */
15304 static int
15305 display_mode_line (w, face_id, format)
15306 struct window *w;
15307 enum face_id face_id;
15308 Lisp_Object format;
15310 struct it it;
15311 struct face *face;
15313 init_iterator (&it, w, -1, -1, NULL, face_id);
15314 prepare_desired_row (it.glyph_row);
15316 it.glyph_row->mode_line_p = 1;
15318 if (! mode_line_inverse_video)
15319 /* Force the mode-line to be displayed in the default face. */
15320 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15322 /* Temporarily make frame's keyboard the current kboard so that
15323 kboard-local variables in the mode_line_format will get the right
15324 values. */
15325 push_frame_kboard (it.f);
15326 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15327 pop_frame_kboard ();
15329 /* Fill up with spaces. */
15330 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15332 compute_line_metrics (&it);
15333 it.glyph_row->full_width_p = 1;
15334 it.glyph_row->continued_p = 0;
15335 it.glyph_row->truncated_on_left_p = 0;
15336 it.glyph_row->truncated_on_right_p = 0;
15338 /* Make a 3D mode-line have a shadow at its right end. */
15339 face = FACE_FROM_ID (it.f, face_id);
15340 extend_face_to_end_of_line (&it);
15341 if (face->box != FACE_NO_BOX)
15343 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15344 + it.glyph_row->used[TEXT_AREA] - 1);
15345 last->right_box_line_p = 1;
15348 return it.glyph_row->height;
15351 /* Alist that caches the results of :propertize.
15352 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15353 Lisp_Object mode_line_proptrans_alist;
15355 /* List of strings making up the mode-line. */
15356 Lisp_Object mode_line_string_list;
15358 /* Base face property when building propertized mode line string. */
15359 static Lisp_Object mode_line_string_face;
15360 static Lisp_Object mode_line_string_face_prop;
15363 /* Contribute ELT to the mode line for window IT->w. How it
15364 translates into text depends on its data type.
15366 IT describes the display environment in which we display, as usual.
15368 DEPTH is the depth in recursion. It is used to prevent
15369 infinite recursion here.
15371 FIELD_WIDTH is the number of characters the display of ELT should
15372 occupy in the mode line, and PRECISION is the maximum number of
15373 characters to display from ELT's representation. See
15374 display_string for details.
15376 Returns the hpos of the end of the text generated by ELT.
15378 PROPS is a property list to add to any string we encounter.
15380 If RISKY is nonzero, remove (disregard) any properties in any string
15381 we encounter, and ignore :eval and :propertize.
15383 If the global variable `frame_title_ptr' is non-NULL, then the output
15384 is passed to `store_frame_title' instead of `display_string'. */
15386 static int
15387 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15388 struct it *it;
15389 int depth;
15390 int field_width, precision;
15391 Lisp_Object elt, props;
15392 int risky;
15394 int n = 0, field, prec;
15395 int literal = 0;
15397 tail_recurse:
15398 if (depth > 100)
15399 elt = build_string ("*too-deep*");
15401 depth++;
15403 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15405 case Lisp_String:
15407 /* A string: output it and check for %-constructs within it. */
15408 unsigned char c;
15409 const unsigned char *this, *lisp_string;
15411 if (!NILP (props) || risky)
15413 Lisp_Object oprops, aelt;
15414 oprops = Ftext_properties_at (make_number (0), elt);
15416 /* If the starting string's properties are not what
15417 we want, translate the string. Also, if the string
15418 is risky, do that anyway. */
15420 if (NILP (Fequal (props, oprops)) || risky)
15422 /* If the starting string has properties,
15423 merge the specified ones onto the existing ones. */
15424 if (! NILP (oprops) && !risky)
15426 Lisp_Object tem;
15428 oprops = Fcopy_sequence (oprops);
15429 tem = props;
15430 while (CONSP (tem))
15432 oprops = Fplist_put (oprops, XCAR (tem),
15433 XCAR (XCDR (tem)));
15434 tem = XCDR (XCDR (tem));
15436 props = oprops;
15439 aelt = Fassoc (elt, mode_line_proptrans_alist);
15440 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15442 mode_line_proptrans_alist
15443 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15444 elt = XCAR (aelt);
15446 else
15448 Lisp_Object tem;
15450 elt = Fcopy_sequence (elt);
15451 Fset_text_properties (make_number (0), Flength (elt),
15452 props, elt);
15453 /* Add this item to mode_line_proptrans_alist. */
15454 mode_line_proptrans_alist
15455 = Fcons (Fcons (elt, props),
15456 mode_line_proptrans_alist);
15457 /* Truncate mode_line_proptrans_alist
15458 to at most 50 elements. */
15459 tem = Fnthcdr (make_number (50),
15460 mode_line_proptrans_alist);
15461 if (! NILP (tem))
15462 XSETCDR (tem, Qnil);
15467 this = SDATA (elt);
15468 lisp_string = this;
15470 if (literal)
15472 prec = precision - n;
15473 if (frame_title_ptr)
15474 n += store_frame_title (SDATA (elt), -1, prec);
15475 else if (!NILP (mode_line_string_list))
15476 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15477 else
15478 n += display_string (NULL, elt, Qnil, 0, 0, it,
15479 0, prec, 0, STRING_MULTIBYTE (elt));
15481 break;
15484 while ((precision <= 0 || n < precision)
15485 && *this
15486 && (frame_title_ptr
15487 || !NILP (mode_line_string_list)
15488 || it->current_x < it->last_visible_x))
15490 const unsigned char *last = this;
15492 /* Advance to end of string or next format specifier. */
15493 while ((c = *this++) != '\0' && c != '%')
15496 if (this - 1 != last)
15498 int nchars, nbytes;
15500 /* Output to end of string or up to '%'. Field width
15501 is length of string. Don't output more than
15502 PRECISION allows us. */
15503 --this;
15505 prec = c_string_width (last, this - last, precision - n,
15506 &nchars, &nbytes);
15508 if (frame_title_ptr)
15509 n += store_frame_title (last, 0, prec);
15510 else if (!NILP (mode_line_string_list))
15512 int bytepos = last - lisp_string;
15513 int charpos = string_byte_to_char (elt, bytepos);
15514 int endpos = (precision <= 0
15515 ? string_byte_to_char (elt,
15516 this - lisp_string)
15517 : charpos + nchars);
15519 n += store_mode_line_string (NULL,
15520 Fsubstring (elt, make_number (charpos),
15521 make_number (endpos)),
15522 0, 0, 0, Qnil);
15524 else
15526 int bytepos = last - lisp_string;
15527 int charpos = string_byte_to_char (elt, bytepos);
15528 n += display_string (NULL, elt, Qnil, 0, charpos,
15529 it, 0, prec, 0,
15530 STRING_MULTIBYTE (elt));
15533 else /* c == '%' */
15535 const unsigned char *percent_position = this;
15537 /* Get the specified minimum width. Zero means
15538 don't pad. */
15539 field = 0;
15540 while ((c = *this++) >= '0' && c <= '9')
15541 field = field * 10 + c - '0';
15543 /* Don't pad beyond the total padding allowed. */
15544 if (field_width - n > 0 && field > field_width - n)
15545 field = field_width - n;
15547 /* Note that either PRECISION <= 0 or N < PRECISION. */
15548 prec = precision - n;
15550 if (c == 'M')
15551 n += display_mode_element (it, depth, field, prec,
15552 Vglobal_mode_string, props,
15553 risky);
15554 else if (c != 0)
15556 int multibyte;
15557 int bytepos, charpos;
15558 unsigned char *spec;
15560 bytepos = percent_position - lisp_string;
15561 charpos = (STRING_MULTIBYTE (elt)
15562 ? string_byte_to_char (elt, bytepos)
15563 : bytepos);
15565 spec
15566 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15568 if (frame_title_ptr)
15569 n += store_frame_title (spec, field, prec);
15570 else if (!NILP (mode_line_string_list))
15572 int len = strlen (spec);
15573 Lisp_Object tem = make_string (spec, len);
15574 props = Ftext_properties_at (make_number (charpos), elt);
15575 /* Should only keep face property in props */
15576 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15578 else
15580 int nglyphs_before, nwritten;
15582 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15583 nwritten = display_string (spec, Qnil, elt,
15584 charpos, 0, it,
15585 field, prec, 0,
15586 multibyte);
15588 /* Assign to the glyphs written above the
15589 string where the `%x' came from, position
15590 of the `%'. */
15591 if (nwritten > 0)
15593 struct glyph *glyph
15594 = (it->glyph_row->glyphs[TEXT_AREA]
15595 + nglyphs_before);
15596 int i;
15598 for (i = 0; i < nwritten; ++i)
15600 glyph[i].object = elt;
15601 glyph[i].charpos = charpos;
15604 n += nwritten;
15608 else /* c == 0 */
15609 break;
15613 break;
15615 case Lisp_Symbol:
15616 /* A symbol: process the value of the symbol recursively
15617 as if it appeared here directly. Avoid error if symbol void.
15618 Special case: if value of symbol is a string, output the string
15619 literally. */
15621 register Lisp_Object tem;
15623 /* If the variable is not marked as risky to set
15624 then its contents are risky to use. */
15625 if (NILP (Fget (elt, Qrisky_local_variable)))
15626 risky = 1;
15628 tem = Fboundp (elt);
15629 if (!NILP (tem))
15631 tem = Fsymbol_value (elt);
15632 /* If value is a string, output that string literally:
15633 don't check for % within it. */
15634 if (STRINGP (tem))
15635 literal = 1;
15637 if (!EQ (tem, elt))
15639 /* Give up right away for nil or t. */
15640 elt = tem;
15641 goto tail_recurse;
15645 break;
15647 case Lisp_Cons:
15649 register Lisp_Object car, tem;
15651 /* A cons cell: five distinct cases.
15652 If first element is :eval or :propertize, do something special.
15653 If first element is a string or a cons, process all the elements
15654 and effectively concatenate them.
15655 If first element is a negative number, truncate displaying cdr to
15656 at most that many characters. If positive, pad (with spaces)
15657 to at least that many characters.
15658 If first element is a symbol, process the cadr or caddr recursively
15659 according to whether the symbol's value is non-nil or nil. */
15660 car = XCAR (elt);
15661 if (EQ (car, QCeval))
15663 /* An element of the form (:eval FORM) means evaluate FORM
15664 and use the result as mode line elements. */
15666 if (risky)
15667 break;
15669 if (CONSP (XCDR (elt)))
15671 Lisp_Object spec;
15672 spec = safe_eval (XCAR (XCDR (elt)));
15673 n += display_mode_element (it, depth, field_width - n,
15674 precision - n, spec, props,
15675 risky);
15678 else if (EQ (car, QCpropertize))
15680 /* An element of the form (:propertize ELT PROPS...)
15681 means display ELT but applying properties PROPS. */
15683 if (risky)
15684 break;
15686 if (CONSP (XCDR (elt)))
15687 n += display_mode_element (it, depth, field_width - n,
15688 precision - n, XCAR (XCDR (elt)),
15689 XCDR (XCDR (elt)), risky);
15691 else if (SYMBOLP (car))
15693 tem = Fboundp (car);
15694 elt = XCDR (elt);
15695 if (!CONSP (elt))
15696 goto invalid;
15697 /* elt is now the cdr, and we know it is a cons cell.
15698 Use its car if CAR has a non-nil value. */
15699 if (!NILP (tem))
15701 tem = Fsymbol_value (car);
15702 if (!NILP (tem))
15704 elt = XCAR (elt);
15705 goto tail_recurse;
15708 /* Symbol's value is nil (or symbol is unbound)
15709 Get the cddr of the original list
15710 and if possible find the caddr and use that. */
15711 elt = XCDR (elt);
15712 if (NILP (elt))
15713 break;
15714 else if (!CONSP (elt))
15715 goto invalid;
15716 elt = XCAR (elt);
15717 goto tail_recurse;
15719 else if (INTEGERP (car))
15721 register int lim = XINT (car);
15722 elt = XCDR (elt);
15723 if (lim < 0)
15725 /* Negative int means reduce maximum width. */
15726 if (precision <= 0)
15727 precision = -lim;
15728 else
15729 precision = min (precision, -lim);
15731 else if (lim > 0)
15733 /* Padding specified. Don't let it be more than
15734 current maximum. */
15735 if (precision > 0)
15736 lim = min (precision, lim);
15738 /* If that's more padding than already wanted, queue it.
15739 But don't reduce padding already specified even if
15740 that is beyond the current truncation point. */
15741 field_width = max (lim, field_width);
15743 goto tail_recurse;
15745 else if (STRINGP (car) || CONSP (car))
15747 register int limit = 50;
15748 /* Limit is to protect against circular lists. */
15749 while (CONSP (elt)
15750 && --limit > 0
15751 && (precision <= 0 || n < precision))
15753 n += display_mode_element (it, depth, field_width - n,
15754 precision - n, XCAR (elt),
15755 props, risky);
15756 elt = XCDR (elt);
15760 break;
15762 default:
15763 invalid:
15764 elt = build_string ("*invalid*");
15765 goto tail_recurse;
15768 /* Pad to FIELD_WIDTH. */
15769 if (field_width > 0 && n < field_width)
15771 if (frame_title_ptr)
15772 n += store_frame_title ("", field_width - n, 0);
15773 else if (!NILP (mode_line_string_list))
15774 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15775 else
15776 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15777 0, 0, 0);
15780 return n;
15783 /* Store a mode-line string element in mode_line_string_list.
15785 If STRING is non-null, display that C string. Otherwise, the Lisp
15786 string LISP_STRING is displayed.
15788 FIELD_WIDTH is the minimum number of output glyphs to produce.
15789 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15790 with spaces. FIELD_WIDTH <= 0 means don't pad.
15792 PRECISION is the maximum number of characters to output from
15793 STRING. PRECISION <= 0 means don't truncate the string.
15795 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15796 properties to the string.
15798 PROPS are the properties to add to the string.
15799 The mode_line_string_face face property is always added to the string.
15802 static int
15803 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15804 char *string;
15805 Lisp_Object lisp_string;
15806 int copy_string;
15807 int field_width;
15808 int precision;
15809 Lisp_Object props;
15811 int len;
15812 int n = 0;
15814 if (string != NULL)
15816 len = strlen (string);
15817 if (precision > 0 && len > precision)
15818 len = precision;
15819 lisp_string = make_string (string, len);
15820 if (NILP (props))
15821 props = mode_line_string_face_prop;
15822 else if (!NILP (mode_line_string_face))
15824 Lisp_Object face = Fsafe_plist_get (props, Qface);
15825 props = Fcopy_sequence (props);
15826 if (NILP (face))
15827 face = mode_line_string_face;
15828 else
15829 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15830 props = Fplist_put (props, Qface, face);
15832 Fadd_text_properties (make_number (0), make_number (len),
15833 props, lisp_string);
15835 else
15837 len = XFASTINT (Flength (lisp_string));
15838 if (precision > 0 && len > precision)
15840 len = precision;
15841 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15842 precision = -1;
15844 if (!NILP (mode_line_string_face))
15846 Lisp_Object face;
15847 if (NILP (props))
15848 props = Ftext_properties_at (make_number (0), lisp_string);
15849 face = Fsafe_plist_get (props, Qface);
15850 if (NILP (face))
15851 face = mode_line_string_face;
15852 else
15853 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15854 props = Fcons (Qface, Fcons (face, Qnil));
15855 if (copy_string)
15856 lisp_string = Fcopy_sequence (lisp_string);
15858 if (!NILP (props))
15859 Fadd_text_properties (make_number (0), make_number (len),
15860 props, lisp_string);
15863 if (len > 0)
15865 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15866 n += len;
15869 if (field_width > len)
15871 field_width -= len;
15872 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15873 if (!NILP (props))
15874 Fadd_text_properties (make_number (0), make_number (field_width),
15875 props, lisp_string);
15876 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15877 n += field_width;
15880 return n;
15884 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15885 0, 4, 0,
15886 doc: /* Return the mode-line of selected window as a string.
15887 First optional arg FORMAT specifies a different format string (see
15888 `mode-line-format' for details) to use. If FORMAT is t, return
15889 the buffer's header-line. Second optional arg WINDOW specifies a
15890 different window to use as the context for the formatting.
15891 If third optional arg NO-PROPS is non-nil, string is not propertized.
15892 Fourth optional arg BUFFER specifies which buffer to use. */)
15893 (format, window, no_props, buffer)
15894 Lisp_Object format, window, no_props, buffer;
15896 struct it it;
15897 int len;
15898 struct window *w;
15899 struct buffer *old_buffer = NULL;
15900 enum face_id face_id = DEFAULT_FACE_ID;
15902 if (NILP (window))
15903 window = selected_window;
15904 CHECK_WINDOW (window);
15905 w = XWINDOW (window);
15907 if (NILP (buffer))
15908 buffer = w->buffer;
15910 CHECK_BUFFER (buffer);
15912 if (XBUFFER (buffer) != current_buffer)
15914 old_buffer = current_buffer;
15915 set_buffer_internal_1 (XBUFFER (buffer));
15918 if (NILP (format) || EQ (format, Qt))
15920 face_id = (NILP (format)
15921 ? CURRENT_MODE_LINE_FACE_ID (w)
15922 : HEADER_LINE_FACE_ID);
15923 format = (NILP (format)
15924 ? current_buffer->mode_line_format
15925 : current_buffer->header_line_format);
15928 init_iterator (&it, w, -1, -1, NULL, face_id);
15930 if (NILP (no_props))
15932 mode_line_string_face
15933 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15934 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15935 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15937 mode_line_string_face_prop
15938 = (NILP (mode_line_string_face) ? Qnil
15939 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15941 /* We need a dummy last element in mode_line_string_list to
15942 indicate we are building the propertized mode-line string.
15943 Using mode_line_string_face_prop here GC protects it. */
15944 mode_line_string_list
15945 = Fcons (mode_line_string_face_prop, Qnil);
15946 frame_title_ptr = NULL;
15948 else
15950 mode_line_string_face_prop = Qnil;
15951 mode_line_string_list = Qnil;
15952 frame_title_ptr = frame_title_buf;
15955 push_frame_kboard (it.f);
15956 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15957 pop_frame_kboard ();
15959 if (old_buffer)
15960 set_buffer_internal_1 (old_buffer);
15962 if (NILP (no_props))
15964 Lisp_Object str;
15965 mode_line_string_list = Fnreverse (mode_line_string_list);
15966 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15967 make_string ("", 0));
15968 mode_line_string_face_prop = Qnil;
15969 mode_line_string_list = Qnil;
15970 return str;
15973 len = frame_title_ptr - frame_title_buf;
15974 if (len > 0 && frame_title_ptr[-1] == '-')
15976 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15977 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15979 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15980 if (len > frame_title_ptr - frame_title_buf)
15981 len = frame_title_ptr - frame_title_buf;
15984 frame_title_ptr = NULL;
15985 return make_string (frame_title_buf, len);
15988 /* Write a null-terminated, right justified decimal representation of
15989 the positive integer D to BUF using a minimal field width WIDTH. */
15991 static void
15992 pint2str (buf, width, d)
15993 register char *buf;
15994 register int width;
15995 register int d;
15997 register char *p = buf;
15999 if (d <= 0)
16000 *p++ = '0';
16001 else
16003 while (d > 0)
16005 *p++ = d % 10 + '0';
16006 d /= 10;
16010 for (width -= (int) (p - buf); width > 0; --width)
16011 *p++ = ' ';
16012 *p-- = '\0';
16013 while (p > buf)
16015 d = *buf;
16016 *buf++ = *p;
16017 *p-- = d;
16021 /* Write a null-terminated, right justified decimal and "human
16022 readable" representation of the nonnegative integer D to BUF using
16023 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16025 static const char power_letter[] =
16027 0, /* not used */
16028 'k', /* kilo */
16029 'M', /* mega */
16030 'G', /* giga */
16031 'T', /* tera */
16032 'P', /* peta */
16033 'E', /* exa */
16034 'Z', /* zetta */
16035 'Y' /* yotta */
16038 static void
16039 pint2hrstr (buf, width, d)
16040 char *buf;
16041 int width;
16042 int d;
16044 /* We aim to represent the nonnegative integer D as
16045 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16046 int quotient = d;
16047 int remainder = 0;
16048 /* -1 means: do not use TENTHS. */
16049 int tenths = -1;
16050 int exponent = 0;
16052 /* Length of QUOTIENT.TENTHS as a string. */
16053 int length;
16055 char * psuffix;
16056 char * p;
16058 if (1000 <= quotient)
16060 /* Scale to the appropriate EXPONENT. */
16063 remainder = quotient % 1000;
16064 quotient /= 1000;
16065 exponent++;
16067 while (1000 <= quotient);
16069 /* Round to nearest and decide whether to use TENTHS or not. */
16070 if (quotient <= 9)
16072 tenths = remainder / 100;
16073 if (50 <= remainder % 100)
16075 if (tenths < 9)
16076 tenths++;
16077 else
16079 quotient++;
16080 if (quotient == 10)
16081 tenths = -1;
16082 else
16083 tenths = 0;
16087 else
16088 if (500 <= remainder)
16090 if (quotient < 999)
16091 quotient++;
16092 else
16094 quotient = 1;
16095 exponent++;
16096 tenths = 0;
16101 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16102 if (tenths == -1 && quotient <= 99)
16103 if (quotient <= 9)
16104 length = 1;
16105 else
16106 length = 2;
16107 else
16108 length = 3;
16109 p = psuffix = buf + max (width, length);
16111 /* Print EXPONENT. */
16112 if (exponent)
16113 *psuffix++ = power_letter[exponent];
16114 *psuffix = '\0';
16116 /* Print TENTHS. */
16117 if (tenths >= 0)
16119 *--p = '0' + tenths;
16120 *--p = '.';
16123 /* Print QUOTIENT. */
16126 int digit = quotient % 10;
16127 *--p = '0' + digit;
16129 while ((quotient /= 10) != 0);
16131 /* Print leading spaces. */
16132 while (buf < p)
16133 *--p = ' ';
16136 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16137 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16138 type of CODING_SYSTEM. Return updated pointer into BUF. */
16140 static unsigned char invalid_eol_type[] = "(*invalid*)";
16142 static char *
16143 decode_mode_spec_coding (coding_system, buf, eol_flag)
16144 Lisp_Object coding_system;
16145 register char *buf;
16146 int eol_flag;
16148 Lisp_Object val;
16149 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16150 const unsigned char *eol_str;
16151 int eol_str_len;
16152 /* The EOL conversion we are using. */
16153 Lisp_Object eoltype;
16155 val = Fget (coding_system, Qcoding_system);
16156 eoltype = Qnil;
16158 if (!VECTORP (val)) /* Not yet decided. */
16160 if (multibyte)
16161 *buf++ = '-';
16162 if (eol_flag)
16163 eoltype = eol_mnemonic_undecided;
16164 /* Don't mention EOL conversion if it isn't decided. */
16166 else
16168 Lisp_Object eolvalue;
16170 eolvalue = Fget (coding_system, Qeol_type);
16172 if (multibyte)
16173 *buf++ = XFASTINT (AREF (val, 1));
16175 if (eol_flag)
16177 /* The EOL conversion that is normal on this system. */
16179 if (NILP (eolvalue)) /* Not yet decided. */
16180 eoltype = eol_mnemonic_undecided;
16181 else if (VECTORP (eolvalue)) /* Not yet decided. */
16182 eoltype = eol_mnemonic_undecided;
16183 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16184 eoltype = (XFASTINT (eolvalue) == 0
16185 ? eol_mnemonic_unix
16186 : (XFASTINT (eolvalue) == 1
16187 ? eol_mnemonic_dos : eol_mnemonic_mac));
16191 if (eol_flag)
16193 /* Mention the EOL conversion if it is not the usual one. */
16194 if (STRINGP (eoltype))
16196 eol_str = SDATA (eoltype);
16197 eol_str_len = SBYTES (eoltype);
16199 else if (INTEGERP (eoltype)
16200 && CHAR_VALID_P (XINT (eoltype), 0))
16202 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16203 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16204 eol_str = tmp;
16206 else
16208 eol_str = invalid_eol_type;
16209 eol_str_len = sizeof (invalid_eol_type) - 1;
16211 bcopy (eol_str, buf, eol_str_len);
16212 buf += eol_str_len;
16215 return buf;
16218 /* Return a string for the output of a mode line %-spec for window W,
16219 generated by character C. PRECISION >= 0 means don't return a
16220 string longer than that value. FIELD_WIDTH > 0 means pad the
16221 string returned with spaces to that value. Return 1 in *MULTIBYTE
16222 if the result is multibyte text.
16224 Note we operate on the current buffer for most purposes,
16225 the exception being w->base_line_pos. */
16227 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16229 static char *
16230 decode_mode_spec (w, c, field_width, precision, multibyte)
16231 struct window *w;
16232 register int c;
16233 int field_width, precision;
16234 int *multibyte;
16236 Lisp_Object obj;
16237 struct frame *f = XFRAME (WINDOW_FRAME (w));
16238 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16239 struct buffer *b = current_buffer;
16241 obj = Qnil;
16242 *multibyte = 0;
16244 switch (c)
16246 case '*':
16247 if (!NILP (b->read_only))
16248 return "%";
16249 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16250 return "*";
16251 return "-";
16253 case '+':
16254 /* This differs from %* only for a modified read-only buffer. */
16255 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16256 return "*";
16257 if (!NILP (b->read_only))
16258 return "%";
16259 return "-";
16261 case '&':
16262 /* This differs from %* in ignoring read-only-ness. */
16263 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16264 return "*";
16265 return "-";
16267 case '%':
16268 return "%";
16270 case '[':
16272 int i;
16273 char *p;
16275 if (command_loop_level > 5)
16276 return "[[[... ";
16277 p = decode_mode_spec_buf;
16278 for (i = 0; i < command_loop_level; i++)
16279 *p++ = '[';
16280 *p = 0;
16281 return decode_mode_spec_buf;
16284 case ']':
16286 int i;
16287 char *p;
16289 if (command_loop_level > 5)
16290 return " ...]]]";
16291 p = decode_mode_spec_buf;
16292 for (i = 0; i < command_loop_level; i++)
16293 *p++ = ']';
16294 *p = 0;
16295 return decode_mode_spec_buf;
16298 case '-':
16300 register int i;
16302 /* Let lots_of_dashes be a string of infinite length. */
16303 if (!NILP (mode_line_string_list))
16304 return "--";
16305 if (field_width <= 0
16306 || field_width > sizeof (lots_of_dashes))
16308 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16309 decode_mode_spec_buf[i] = '-';
16310 decode_mode_spec_buf[i] = '\0';
16311 return decode_mode_spec_buf;
16313 else
16314 return lots_of_dashes;
16317 case 'b':
16318 obj = b->name;
16319 break;
16321 case 'c':
16323 int col = (int) current_column (); /* iftc */
16324 w->column_number_displayed = make_number (col);
16325 pint2str (decode_mode_spec_buf, field_width, col);
16326 return decode_mode_spec_buf;
16329 case 'F':
16330 /* %F displays the frame name. */
16331 if (!NILP (f->title))
16332 return (char *) SDATA (f->title);
16333 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16334 return (char *) SDATA (f->name);
16335 return "Emacs";
16337 case 'f':
16338 obj = b->filename;
16339 break;
16341 case 'i':
16343 int size = ZV - BEGV;
16344 pint2str (decode_mode_spec_buf, field_width, size);
16345 return decode_mode_spec_buf;
16348 case 'I':
16350 int size = ZV - BEGV;
16351 pint2hrstr (decode_mode_spec_buf, field_width, size);
16352 return decode_mode_spec_buf;
16355 case 'l':
16357 int startpos = XMARKER (w->start)->charpos;
16358 int startpos_byte = marker_byte_position (w->start);
16359 int line, linepos, linepos_byte, topline;
16360 int nlines, junk;
16361 int height = WINDOW_TOTAL_LINES (w);
16363 /* If we decided that this buffer isn't suitable for line numbers,
16364 don't forget that too fast. */
16365 if (EQ (w->base_line_pos, w->buffer))
16366 goto no_value;
16367 /* But do forget it, if the window shows a different buffer now. */
16368 else if (BUFFERP (w->base_line_pos))
16369 w->base_line_pos = Qnil;
16371 /* If the buffer is very big, don't waste time. */
16372 if (INTEGERP (Vline_number_display_limit)
16373 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16375 w->base_line_pos = Qnil;
16376 w->base_line_number = Qnil;
16377 goto no_value;
16380 if (!NILP (w->base_line_number)
16381 && !NILP (w->base_line_pos)
16382 && XFASTINT (w->base_line_pos) <= startpos)
16384 line = XFASTINT (w->base_line_number);
16385 linepos = XFASTINT (w->base_line_pos);
16386 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16388 else
16390 line = 1;
16391 linepos = BUF_BEGV (b);
16392 linepos_byte = BUF_BEGV_BYTE (b);
16395 /* Count lines from base line to window start position. */
16396 nlines = display_count_lines (linepos, linepos_byte,
16397 startpos_byte,
16398 startpos, &junk);
16400 topline = nlines + line;
16402 /* Determine a new base line, if the old one is too close
16403 or too far away, or if we did not have one.
16404 "Too close" means it's plausible a scroll-down would
16405 go back past it. */
16406 if (startpos == BUF_BEGV (b))
16408 w->base_line_number = make_number (topline);
16409 w->base_line_pos = make_number (BUF_BEGV (b));
16411 else if (nlines < height + 25 || nlines > height * 3 + 50
16412 || linepos == BUF_BEGV (b))
16414 int limit = BUF_BEGV (b);
16415 int limit_byte = BUF_BEGV_BYTE (b);
16416 int position;
16417 int distance = (height * 2 + 30) * line_number_display_limit_width;
16419 if (startpos - distance > limit)
16421 limit = startpos - distance;
16422 limit_byte = CHAR_TO_BYTE (limit);
16425 nlines = display_count_lines (startpos, startpos_byte,
16426 limit_byte,
16427 - (height * 2 + 30),
16428 &position);
16429 /* If we couldn't find the lines we wanted within
16430 line_number_display_limit_width chars per line,
16431 give up on line numbers for this window. */
16432 if (position == limit_byte && limit == startpos - distance)
16434 w->base_line_pos = w->buffer;
16435 w->base_line_number = Qnil;
16436 goto no_value;
16439 w->base_line_number = make_number (topline - nlines);
16440 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16443 /* Now count lines from the start pos to point. */
16444 nlines = display_count_lines (startpos, startpos_byte,
16445 PT_BYTE, PT, &junk);
16447 /* Record that we did display the line number. */
16448 line_number_displayed = 1;
16450 /* Make the string to show. */
16451 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16452 return decode_mode_spec_buf;
16453 no_value:
16455 char* p = decode_mode_spec_buf;
16456 int pad = field_width - 2;
16457 while (pad-- > 0)
16458 *p++ = ' ';
16459 *p++ = '?';
16460 *p++ = '?';
16461 *p = '\0';
16462 return decode_mode_spec_buf;
16465 break;
16467 case 'm':
16468 obj = b->mode_name;
16469 break;
16471 case 'n':
16472 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16473 return " Narrow";
16474 break;
16476 case 'p':
16478 int pos = marker_position (w->start);
16479 int total = BUF_ZV (b) - BUF_BEGV (b);
16481 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16483 if (pos <= BUF_BEGV (b))
16484 return "All";
16485 else
16486 return "Bottom";
16488 else if (pos <= BUF_BEGV (b))
16489 return "Top";
16490 else
16492 if (total > 1000000)
16493 /* Do it differently for a large value, to avoid overflow. */
16494 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16495 else
16496 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16497 /* We can't normally display a 3-digit number,
16498 so get us a 2-digit number that is close. */
16499 if (total == 100)
16500 total = 99;
16501 sprintf (decode_mode_spec_buf, "%2d%%", total);
16502 return decode_mode_spec_buf;
16506 /* Display percentage of size above the bottom of the screen. */
16507 case 'P':
16509 int toppos = marker_position (w->start);
16510 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16511 int total = BUF_ZV (b) - BUF_BEGV (b);
16513 if (botpos >= BUF_ZV (b))
16515 if (toppos <= BUF_BEGV (b))
16516 return "All";
16517 else
16518 return "Bottom";
16520 else
16522 if (total > 1000000)
16523 /* Do it differently for a large value, to avoid overflow. */
16524 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16525 else
16526 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16527 /* We can't normally display a 3-digit number,
16528 so get us a 2-digit number that is close. */
16529 if (total == 100)
16530 total = 99;
16531 if (toppos <= BUF_BEGV (b))
16532 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16533 else
16534 sprintf (decode_mode_spec_buf, "%2d%%", total);
16535 return decode_mode_spec_buf;
16539 case 's':
16540 /* status of process */
16541 obj = Fget_buffer_process (Fcurrent_buffer ());
16542 if (NILP (obj))
16543 return "no process";
16544 #ifdef subprocesses
16545 obj = Fsymbol_name (Fprocess_status (obj));
16546 #endif
16547 break;
16549 case 't': /* indicate TEXT or BINARY */
16550 #ifdef MODE_LINE_BINARY_TEXT
16551 return MODE_LINE_BINARY_TEXT (b);
16552 #else
16553 return "T";
16554 #endif
16556 case 'z':
16557 /* coding-system (not including end-of-line format) */
16558 case 'Z':
16559 /* coding-system (including end-of-line type) */
16561 int eol_flag = (c == 'Z');
16562 char *p = decode_mode_spec_buf;
16564 if (! FRAME_WINDOW_P (f))
16566 /* No need to mention EOL here--the terminal never needs
16567 to do EOL conversion. */
16568 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16569 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16571 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16572 p, eol_flag);
16574 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16575 #ifdef subprocesses
16576 obj = Fget_buffer_process (Fcurrent_buffer ());
16577 if (PROCESSP (obj))
16579 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16580 p, eol_flag);
16581 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16582 p, eol_flag);
16584 #endif /* subprocesses */
16585 #endif /* 0 */
16586 *p = 0;
16587 return decode_mode_spec_buf;
16591 if (STRINGP (obj))
16593 *multibyte = STRING_MULTIBYTE (obj);
16594 return (char *) SDATA (obj);
16596 else
16597 return "";
16601 /* Count up to COUNT lines starting from START / START_BYTE.
16602 But don't go beyond LIMIT_BYTE.
16603 Return the number of lines thus found (always nonnegative).
16605 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16607 static int
16608 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16609 int start, start_byte, limit_byte, count;
16610 int *byte_pos_ptr;
16612 register unsigned char *cursor;
16613 unsigned char *base;
16615 register int ceiling;
16616 register unsigned char *ceiling_addr;
16617 int orig_count = count;
16619 /* If we are not in selective display mode,
16620 check only for newlines. */
16621 int selective_display = (!NILP (current_buffer->selective_display)
16622 && !INTEGERP (current_buffer->selective_display));
16624 if (count > 0)
16626 while (start_byte < limit_byte)
16628 ceiling = BUFFER_CEILING_OF (start_byte);
16629 ceiling = min (limit_byte - 1, ceiling);
16630 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16631 base = (cursor = BYTE_POS_ADDR (start_byte));
16632 while (1)
16634 if (selective_display)
16635 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16637 else
16638 while (*cursor != '\n' && ++cursor != ceiling_addr)
16641 if (cursor != ceiling_addr)
16643 if (--count == 0)
16645 start_byte += cursor - base + 1;
16646 *byte_pos_ptr = start_byte;
16647 return orig_count;
16649 else
16650 if (++cursor == ceiling_addr)
16651 break;
16653 else
16654 break;
16656 start_byte += cursor - base;
16659 else
16661 while (start_byte > limit_byte)
16663 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16664 ceiling = max (limit_byte, ceiling);
16665 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16666 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16667 while (1)
16669 if (selective_display)
16670 while (--cursor != ceiling_addr
16671 && *cursor != '\n' && *cursor != 015)
16673 else
16674 while (--cursor != ceiling_addr && *cursor != '\n')
16677 if (cursor != ceiling_addr)
16679 if (++count == 0)
16681 start_byte += cursor - base + 1;
16682 *byte_pos_ptr = start_byte;
16683 /* When scanning backwards, we should
16684 not count the newline posterior to which we stop. */
16685 return - orig_count - 1;
16688 else
16689 break;
16691 /* Here we add 1 to compensate for the last decrement
16692 of CURSOR, which took it past the valid range. */
16693 start_byte += cursor - base + 1;
16697 *byte_pos_ptr = limit_byte;
16699 if (count < 0)
16700 return - orig_count + count;
16701 return orig_count - count;
16707 /***********************************************************************
16708 Displaying strings
16709 ***********************************************************************/
16711 /* Display a NUL-terminated string, starting with index START.
16713 If STRING is non-null, display that C string. Otherwise, the Lisp
16714 string LISP_STRING is displayed.
16716 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16717 FACE_STRING. Display STRING or LISP_STRING with the face at
16718 FACE_STRING_POS in FACE_STRING:
16720 Display the string in the environment given by IT, but use the
16721 standard display table, temporarily.
16723 FIELD_WIDTH is the minimum number of output glyphs to produce.
16724 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16725 with spaces. If STRING has more characters, more than FIELD_WIDTH
16726 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16728 PRECISION is the maximum number of characters to output from
16729 STRING. PRECISION < 0 means don't truncate the string.
16731 This is roughly equivalent to printf format specifiers:
16733 FIELD_WIDTH PRECISION PRINTF
16734 ----------------------------------------
16735 -1 -1 %s
16736 -1 10 %.10s
16737 10 -1 %10s
16738 20 10 %20.10s
16740 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16741 display them, and < 0 means obey the current buffer's value of
16742 enable_multibyte_characters.
16744 Value is the number of glyphs produced. */
16746 static int
16747 display_string (string, lisp_string, face_string, face_string_pos,
16748 start, it, field_width, precision, max_x, multibyte)
16749 unsigned char *string;
16750 Lisp_Object lisp_string;
16751 Lisp_Object face_string;
16752 int face_string_pos;
16753 int start;
16754 struct it *it;
16755 int field_width, precision, max_x;
16756 int multibyte;
16758 int hpos_at_start = it->hpos;
16759 int saved_face_id = it->face_id;
16760 struct glyph_row *row = it->glyph_row;
16762 /* Initialize the iterator IT for iteration over STRING beginning
16763 with index START. */
16764 reseat_to_string (it, string, lisp_string, start,
16765 precision, field_width, multibyte);
16767 /* If displaying STRING, set up the face of the iterator
16768 from LISP_STRING, if that's given. */
16769 if (STRINGP (face_string))
16771 int endptr;
16772 struct face *face;
16774 it->face_id
16775 = face_at_string_position (it->w, face_string, face_string_pos,
16776 0, it->region_beg_charpos,
16777 it->region_end_charpos,
16778 &endptr, it->base_face_id, 0);
16779 face = FACE_FROM_ID (it->f, it->face_id);
16780 it->face_box_p = face->box != FACE_NO_BOX;
16783 /* Set max_x to the maximum allowed X position. Don't let it go
16784 beyond the right edge of the window. */
16785 if (max_x <= 0)
16786 max_x = it->last_visible_x;
16787 else
16788 max_x = min (max_x, it->last_visible_x);
16790 /* Skip over display elements that are not visible. because IT->w is
16791 hscrolled. */
16792 if (it->current_x < it->first_visible_x)
16793 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16794 MOVE_TO_POS | MOVE_TO_X);
16796 row->ascent = it->max_ascent;
16797 row->height = it->max_ascent + it->max_descent;
16798 row->phys_ascent = it->max_phys_ascent;
16799 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16800 row->extra_line_spacing = it->max_extra_line_spacing;
16802 /* This condition is for the case that we are called with current_x
16803 past last_visible_x. */
16804 while (it->current_x < max_x)
16806 int x_before, x, n_glyphs_before, i, nglyphs;
16808 /* Get the next display element. */
16809 if (!get_next_display_element (it))
16810 break;
16812 /* Produce glyphs. */
16813 x_before = it->current_x;
16814 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16815 PRODUCE_GLYPHS (it);
16817 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16818 i = 0;
16819 x = x_before;
16820 while (i < nglyphs)
16822 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16824 if (!it->truncate_lines_p
16825 && x + glyph->pixel_width > max_x)
16827 /* End of continued line or max_x reached. */
16828 if (CHAR_GLYPH_PADDING_P (*glyph))
16830 /* A wide character is unbreakable. */
16831 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16832 it->current_x = x_before;
16834 else
16836 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16837 it->current_x = x;
16839 break;
16841 else if (x + glyph->pixel_width > it->first_visible_x)
16843 /* Glyph is at least partially visible. */
16844 ++it->hpos;
16845 if (x < it->first_visible_x)
16846 it->glyph_row->x = x - it->first_visible_x;
16848 else
16850 /* Glyph is off the left margin of the display area.
16851 Should not happen. */
16852 abort ();
16855 row->ascent = max (row->ascent, it->max_ascent);
16856 row->height = max (row->height, it->max_ascent + it->max_descent);
16857 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16858 row->phys_height = max (row->phys_height,
16859 it->max_phys_ascent + it->max_phys_descent);
16860 row->extra_line_spacing = max (row->extra_line_spacing,
16861 it->max_extra_line_spacing);
16862 x += glyph->pixel_width;
16863 ++i;
16866 /* Stop if max_x reached. */
16867 if (i < nglyphs)
16868 break;
16870 /* Stop at line ends. */
16871 if (ITERATOR_AT_END_OF_LINE_P (it))
16873 it->continuation_lines_width = 0;
16874 break;
16877 set_iterator_to_next (it, 1);
16879 /* Stop if truncating at the right edge. */
16880 if (it->truncate_lines_p
16881 && it->current_x >= it->last_visible_x)
16883 /* Add truncation mark, but don't do it if the line is
16884 truncated at a padding space. */
16885 if (IT_CHARPOS (*it) < it->string_nchars)
16887 if (!FRAME_WINDOW_P (it->f))
16889 int i, n;
16891 if (it->current_x > it->last_visible_x)
16893 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16894 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16895 break;
16896 for (n = row->used[TEXT_AREA]; i < n; ++i)
16898 row->used[TEXT_AREA] = i;
16899 produce_special_glyphs (it, IT_TRUNCATION);
16902 produce_special_glyphs (it, IT_TRUNCATION);
16904 it->glyph_row->truncated_on_right_p = 1;
16906 break;
16910 /* Maybe insert a truncation at the left. */
16911 if (it->first_visible_x
16912 && IT_CHARPOS (*it) > 0)
16914 if (!FRAME_WINDOW_P (it->f))
16915 insert_left_trunc_glyphs (it);
16916 it->glyph_row->truncated_on_left_p = 1;
16919 it->face_id = saved_face_id;
16921 /* Value is number of columns displayed. */
16922 return it->hpos - hpos_at_start;
16927 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16928 appears as an element of LIST or as the car of an element of LIST.
16929 If PROPVAL is a list, compare each element against LIST in that
16930 way, and return 1/2 if any element of PROPVAL is found in LIST.
16931 Otherwise return 0. This function cannot quit.
16932 The return value is 2 if the text is invisible but with an ellipsis
16933 and 1 if it's invisible and without an ellipsis. */
16936 invisible_p (propval, list)
16937 register Lisp_Object propval;
16938 Lisp_Object list;
16940 register Lisp_Object tail, proptail;
16942 for (tail = list; CONSP (tail); tail = XCDR (tail))
16944 register Lisp_Object tem;
16945 tem = XCAR (tail);
16946 if (EQ (propval, tem))
16947 return 1;
16948 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16949 return NILP (XCDR (tem)) ? 1 : 2;
16952 if (CONSP (propval))
16954 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16956 Lisp_Object propelt;
16957 propelt = XCAR (proptail);
16958 for (tail = list; CONSP (tail); tail = XCDR (tail))
16960 register Lisp_Object tem;
16961 tem = XCAR (tail);
16962 if (EQ (propelt, tem))
16963 return 1;
16964 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16965 return NILP (XCDR (tem)) ? 1 : 2;
16970 return 0;
16973 /* Calculate a width or height in pixels from a specification using
16974 the following elements:
16976 SPEC ::=
16977 NUM - a (fractional) multiple of the default font width/height
16978 (NUM) - specifies exactly NUM pixels
16979 UNIT - a fixed number of pixels, see below.
16980 ELEMENT - size of a display element in pixels, see below.
16981 (NUM . SPEC) - equals NUM * SPEC
16982 (+ SPEC SPEC ...) - add pixel values
16983 (- SPEC SPEC ...) - subtract pixel values
16984 (- SPEC) - negate pixel value
16986 NUM ::=
16987 INT or FLOAT - a number constant
16988 SYMBOL - use symbol's (buffer local) variable binding.
16990 UNIT ::=
16991 in - pixels per inch *)
16992 mm - pixels per 1/1000 meter *)
16993 cm - pixels per 1/100 meter *)
16994 width - width of current font in pixels.
16995 height - height of current font in pixels.
16997 *) using the ratio(s) defined in display-pixels-per-inch.
16999 ELEMENT ::=
17001 left-fringe - left fringe width in pixels
17002 right-fringe - right fringe width in pixels
17004 left-margin - left margin width in pixels
17005 right-margin - right margin width in pixels
17007 scroll-bar - scroll-bar area width in pixels
17009 Examples:
17011 Pixels corresponding to 5 inches:
17012 (5 . in)
17014 Total width of non-text areas on left side of window (if scroll-bar is on left):
17015 '(space :width (+ left-fringe left-margin scroll-bar))
17017 Align to first text column (in header line):
17018 '(space :align-to 0)
17020 Align to middle of text area minus half the width of variable `my-image'
17021 containing a loaded image:
17022 '(space :align-to (0.5 . (- text my-image)))
17024 Width of left margin minus width of 1 character in the default font:
17025 '(space :width (- left-margin 1))
17027 Width of left margin minus width of 2 characters in the current font:
17028 '(space :width (- left-margin (2 . width)))
17030 Center 1 character over left-margin (in header line):
17031 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17033 Different ways to express width of left fringe plus left margin minus one pixel:
17034 '(space :width (- (+ left-fringe left-margin) (1)))
17035 '(space :width (+ left-fringe left-margin (- (1))))
17036 '(space :width (+ left-fringe left-margin (-1)))
17040 #define NUMVAL(X) \
17041 ((INTEGERP (X) || FLOATP (X)) \
17042 ? XFLOATINT (X) \
17043 : - 1)
17046 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17047 double *res;
17048 struct it *it;
17049 Lisp_Object prop;
17050 void *font;
17051 int width_p, *align_to;
17053 double pixels;
17055 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17056 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17058 if (NILP (prop))
17059 return OK_PIXELS (0);
17061 if (SYMBOLP (prop))
17063 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17065 char *unit = SDATA (SYMBOL_NAME (prop));
17067 if (unit[0] == 'i' && unit[1] == 'n')
17068 pixels = 1.0;
17069 else if (unit[0] == 'm' && unit[1] == 'm')
17070 pixels = 25.4;
17071 else if (unit[0] == 'c' && unit[1] == 'm')
17072 pixels = 2.54;
17073 else
17074 pixels = 0;
17075 if (pixels > 0)
17077 double ppi;
17078 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17079 || (CONSP (Vdisplay_pixels_per_inch)
17080 && (ppi = (width_p
17081 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17082 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17083 ppi > 0)))
17084 return OK_PIXELS (ppi / pixels);
17086 return 0;
17090 #ifdef HAVE_WINDOW_SYSTEM
17091 if (EQ (prop, Qheight))
17092 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17093 if (EQ (prop, Qwidth))
17094 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17095 #else
17096 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17097 return OK_PIXELS (1);
17098 #endif
17100 if (EQ (prop, Qtext))
17101 return OK_PIXELS (width_p
17102 ? window_box_width (it->w, TEXT_AREA)
17103 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17105 if (align_to && *align_to < 0)
17107 *res = 0;
17108 if (EQ (prop, Qleft))
17109 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17110 if (EQ (prop, Qright))
17111 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17112 if (EQ (prop, Qcenter))
17113 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17114 + window_box_width (it->w, TEXT_AREA) / 2);
17115 if (EQ (prop, Qleft_fringe))
17116 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17117 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17118 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17119 if (EQ (prop, Qright_fringe))
17120 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17121 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17122 : window_box_right_offset (it->w, TEXT_AREA));
17123 if (EQ (prop, Qleft_margin))
17124 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17125 if (EQ (prop, Qright_margin))
17126 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17127 if (EQ (prop, Qscroll_bar))
17128 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17130 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17131 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17132 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17133 : 0)));
17135 else
17137 if (EQ (prop, Qleft_fringe))
17138 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17139 if (EQ (prop, Qright_fringe))
17140 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17141 if (EQ (prop, Qleft_margin))
17142 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17143 if (EQ (prop, Qright_margin))
17144 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17145 if (EQ (prop, Qscroll_bar))
17146 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17149 prop = Fbuffer_local_value (prop, it->w->buffer);
17152 if (INTEGERP (prop) || FLOATP (prop))
17154 int base_unit = (width_p
17155 ? FRAME_COLUMN_WIDTH (it->f)
17156 : FRAME_LINE_HEIGHT (it->f));
17157 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17160 if (CONSP (prop))
17162 Lisp_Object car = XCAR (prop);
17163 Lisp_Object cdr = XCDR (prop);
17165 if (SYMBOLP (car))
17167 #ifdef HAVE_WINDOW_SYSTEM
17168 if (valid_image_p (prop))
17170 int id = lookup_image (it->f, prop);
17171 struct image *img = IMAGE_FROM_ID (it->f, id);
17173 return OK_PIXELS (width_p ? img->width : img->height);
17175 #endif
17176 if (EQ (car, Qplus) || EQ (car, Qminus))
17178 int first = 1;
17179 double px;
17181 pixels = 0;
17182 while (CONSP (cdr))
17184 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17185 font, width_p, align_to))
17186 return 0;
17187 if (first)
17188 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17189 else
17190 pixels += px;
17191 cdr = XCDR (cdr);
17193 if (EQ (car, Qminus))
17194 pixels = -pixels;
17195 return OK_PIXELS (pixels);
17198 car = Fbuffer_local_value (car, it->w->buffer);
17201 if (INTEGERP (car) || FLOATP (car))
17203 double fact;
17204 pixels = XFLOATINT (car);
17205 if (NILP (cdr))
17206 return OK_PIXELS (pixels);
17207 if (calc_pixel_width_or_height (&fact, it, cdr,
17208 font, width_p, align_to))
17209 return OK_PIXELS (pixels * fact);
17210 return 0;
17213 return 0;
17216 return 0;
17220 /***********************************************************************
17221 Glyph Display
17222 ***********************************************************************/
17224 #ifdef HAVE_WINDOW_SYSTEM
17226 #if GLYPH_DEBUG
17228 void
17229 dump_glyph_string (s)
17230 struct glyph_string *s;
17232 fprintf (stderr, "glyph string\n");
17233 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17234 s->x, s->y, s->width, s->height);
17235 fprintf (stderr, " ybase = %d\n", s->ybase);
17236 fprintf (stderr, " hl = %d\n", s->hl);
17237 fprintf (stderr, " left overhang = %d, right = %d\n",
17238 s->left_overhang, s->right_overhang);
17239 fprintf (stderr, " nchars = %d\n", s->nchars);
17240 fprintf (stderr, " extends to end of line = %d\n",
17241 s->extends_to_end_of_line_p);
17242 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17243 fprintf (stderr, " bg width = %d\n", s->background_width);
17246 #endif /* GLYPH_DEBUG */
17248 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17249 of XChar2b structures for S; it can't be allocated in
17250 init_glyph_string because it must be allocated via `alloca'. W
17251 is the window on which S is drawn. ROW and AREA are the glyph row
17252 and area within the row from which S is constructed. START is the
17253 index of the first glyph structure covered by S. HL is a
17254 face-override for drawing S. */
17256 #ifdef HAVE_NTGUI
17257 #define OPTIONAL_HDC(hdc) hdc,
17258 #define DECLARE_HDC(hdc) HDC hdc;
17259 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17260 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17261 #endif
17263 #ifndef OPTIONAL_HDC
17264 #define OPTIONAL_HDC(hdc)
17265 #define DECLARE_HDC(hdc)
17266 #define ALLOCATE_HDC(hdc, f)
17267 #define RELEASE_HDC(hdc, f)
17268 #endif
17270 static void
17271 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17272 struct glyph_string *s;
17273 DECLARE_HDC (hdc)
17274 XChar2b *char2b;
17275 struct window *w;
17276 struct glyph_row *row;
17277 enum glyph_row_area area;
17278 int start;
17279 enum draw_glyphs_face hl;
17281 bzero (s, sizeof *s);
17282 s->w = w;
17283 s->f = XFRAME (w->frame);
17284 #ifdef HAVE_NTGUI
17285 s->hdc = hdc;
17286 #endif
17287 s->display = FRAME_X_DISPLAY (s->f);
17288 s->window = FRAME_X_WINDOW (s->f);
17289 s->char2b = char2b;
17290 s->hl = hl;
17291 s->row = row;
17292 s->area = area;
17293 s->first_glyph = row->glyphs[area] + start;
17294 s->height = row->height;
17295 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17297 /* Display the internal border below the tool-bar window. */
17298 if (WINDOWP (s->f->tool_bar_window)
17299 && s->w == XWINDOW (s->f->tool_bar_window))
17300 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17302 s->ybase = s->y + row->ascent;
17306 /* Append the list of glyph strings with head H and tail T to the list
17307 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17309 static INLINE void
17310 append_glyph_string_lists (head, tail, h, t)
17311 struct glyph_string **head, **tail;
17312 struct glyph_string *h, *t;
17314 if (h)
17316 if (*head)
17317 (*tail)->next = h;
17318 else
17319 *head = h;
17320 h->prev = *tail;
17321 *tail = t;
17326 /* Prepend the list of glyph strings with head H and tail T to the
17327 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17328 result. */
17330 static INLINE void
17331 prepend_glyph_string_lists (head, tail, h, t)
17332 struct glyph_string **head, **tail;
17333 struct glyph_string *h, *t;
17335 if (h)
17337 if (*head)
17338 (*head)->prev = t;
17339 else
17340 *tail = t;
17341 t->next = *head;
17342 *head = h;
17347 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17348 Set *HEAD and *TAIL to the resulting list. */
17350 static INLINE void
17351 append_glyph_string (head, tail, s)
17352 struct glyph_string **head, **tail;
17353 struct glyph_string *s;
17355 s->next = s->prev = NULL;
17356 append_glyph_string_lists (head, tail, s, s);
17360 /* Get face and two-byte form of character glyph GLYPH on frame F.
17361 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17362 a pointer to a realized face that is ready for display. */
17364 static INLINE struct face *
17365 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17366 struct frame *f;
17367 struct glyph *glyph;
17368 XChar2b *char2b;
17369 int *two_byte_p;
17371 struct face *face;
17373 xassert (glyph->type == CHAR_GLYPH);
17374 face = FACE_FROM_ID (f, glyph->face_id);
17376 if (two_byte_p)
17377 *two_byte_p = 0;
17379 if (!glyph->multibyte_p)
17381 /* Unibyte case. We don't have to encode, but we have to make
17382 sure to use a face suitable for unibyte. */
17383 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17385 else if (glyph->u.ch < 128
17386 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17388 /* Case of ASCII in a face known to fit ASCII. */
17389 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17391 else
17393 int c1, c2, charset;
17395 /* Split characters into bytes. If c2 is -1 afterwards, C is
17396 really a one-byte character so that byte1 is zero. */
17397 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17398 if (c2 > 0)
17399 STORE_XCHAR2B (char2b, c1, c2);
17400 else
17401 STORE_XCHAR2B (char2b, 0, c1);
17403 /* Maybe encode the character in *CHAR2B. */
17404 if (charset != CHARSET_ASCII)
17406 struct font_info *font_info
17407 = FONT_INFO_FROM_ID (f, face->font_info_id);
17408 if (font_info)
17409 glyph->font_type
17410 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17414 /* Make sure X resources of the face are allocated. */
17415 xassert (face != NULL);
17416 PREPARE_FACE_FOR_DISPLAY (f, face);
17417 return face;
17421 /* Fill glyph string S with composition components specified by S->cmp.
17423 FACES is an array of faces for all components of this composition.
17424 S->gidx is the index of the first component for S.
17425 OVERLAPS_P non-zero means S should draw the foreground only, and
17426 use its physical height for clipping.
17428 Value is the index of a component not in S. */
17430 static int
17431 fill_composite_glyph_string (s, faces, overlaps_p)
17432 struct glyph_string *s;
17433 struct face **faces;
17434 int overlaps_p;
17436 int i;
17438 xassert (s);
17440 s->for_overlaps_p = overlaps_p;
17442 s->face = faces[s->gidx];
17443 s->font = s->face->font;
17444 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17446 /* For all glyphs of this composition, starting at the offset
17447 S->gidx, until we reach the end of the definition or encounter a
17448 glyph that requires the different face, add it to S. */
17449 ++s->nchars;
17450 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17451 ++s->nchars;
17453 /* All glyph strings for the same composition has the same width,
17454 i.e. the width set for the first component of the composition. */
17456 s->width = s->first_glyph->pixel_width;
17458 /* If the specified font could not be loaded, use the frame's
17459 default font, but record the fact that we couldn't load it in
17460 the glyph string so that we can draw rectangles for the
17461 characters of the glyph string. */
17462 if (s->font == NULL)
17464 s->font_not_found_p = 1;
17465 s->font = FRAME_FONT (s->f);
17468 /* Adjust base line for subscript/superscript text. */
17469 s->ybase += s->first_glyph->voffset;
17471 xassert (s->face && s->face->gc);
17473 /* This glyph string must always be drawn with 16-bit functions. */
17474 s->two_byte_p = 1;
17476 return s->gidx + s->nchars;
17480 /* Fill glyph string S from a sequence of character glyphs.
17482 FACE_ID is the face id of the string. START is the index of the
17483 first glyph to consider, END is the index of the last + 1.
17484 OVERLAPS_P non-zero means S should draw the foreground only, and
17485 use its physical height for clipping.
17487 Value is the index of the first glyph not in S. */
17489 static int
17490 fill_glyph_string (s, face_id, start, end, overlaps_p)
17491 struct glyph_string *s;
17492 int face_id;
17493 int start, end, overlaps_p;
17495 struct glyph *glyph, *last;
17496 int voffset;
17497 int glyph_not_available_p;
17499 xassert (s->f == XFRAME (s->w->frame));
17500 xassert (s->nchars == 0);
17501 xassert (start >= 0 && end > start);
17503 s->for_overlaps_p = overlaps_p,
17504 glyph = s->row->glyphs[s->area] + start;
17505 last = s->row->glyphs[s->area] + end;
17506 voffset = glyph->voffset;
17508 glyph_not_available_p = glyph->glyph_not_available_p;
17510 while (glyph < last
17511 && glyph->type == CHAR_GLYPH
17512 && glyph->voffset == voffset
17513 /* Same face id implies same font, nowadays. */
17514 && glyph->face_id == face_id
17515 && glyph->glyph_not_available_p == glyph_not_available_p)
17517 int two_byte_p;
17519 s->face = get_glyph_face_and_encoding (s->f, glyph,
17520 s->char2b + s->nchars,
17521 &two_byte_p);
17522 s->two_byte_p = two_byte_p;
17523 ++s->nchars;
17524 xassert (s->nchars <= end - start);
17525 s->width += glyph->pixel_width;
17526 ++glyph;
17529 s->font = s->face->font;
17530 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17532 /* If the specified font could not be loaded, use the frame's font,
17533 but record the fact that we couldn't load it in
17534 S->font_not_found_p so that we can draw rectangles for the
17535 characters of the glyph string. */
17536 if (s->font == NULL || glyph_not_available_p)
17538 s->font_not_found_p = 1;
17539 s->font = FRAME_FONT (s->f);
17542 /* Adjust base line for subscript/superscript text. */
17543 s->ybase += voffset;
17545 xassert (s->face && s->face->gc);
17546 return glyph - s->row->glyphs[s->area];
17550 /* Fill glyph string S from image glyph S->first_glyph. */
17552 static void
17553 fill_image_glyph_string (s)
17554 struct glyph_string *s;
17556 xassert (s->first_glyph->type == IMAGE_GLYPH);
17557 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17558 xassert (s->img);
17559 s->slice = s->first_glyph->slice;
17560 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17561 s->font = s->face->font;
17562 s->width = s->first_glyph->pixel_width;
17564 /* Adjust base line for subscript/superscript text. */
17565 s->ybase += s->first_glyph->voffset;
17569 /* Fill glyph string S from a sequence of stretch glyphs.
17571 ROW is the glyph row in which the glyphs are found, AREA is the
17572 area within the row. START is the index of the first glyph to
17573 consider, END is the index of the last + 1.
17575 Value is the index of the first glyph not in S. */
17577 static int
17578 fill_stretch_glyph_string (s, row, area, start, end)
17579 struct glyph_string *s;
17580 struct glyph_row *row;
17581 enum glyph_row_area area;
17582 int start, end;
17584 struct glyph *glyph, *last;
17585 int voffset, face_id;
17587 xassert (s->first_glyph->type == STRETCH_GLYPH);
17589 glyph = s->row->glyphs[s->area] + start;
17590 last = s->row->glyphs[s->area] + end;
17591 face_id = glyph->face_id;
17592 s->face = FACE_FROM_ID (s->f, face_id);
17593 s->font = s->face->font;
17594 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17595 s->width = glyph->pixel_width;
17596 voffset = glyph->voffset;
17598 for (++glyph;
17599 (glyph < last
17600 && glyph->type == STRETCH_GLYPH
17601 && glyph->voffset == voffset
17602 && glyph->face_id == face_id);
17603 ++glyph)
17604 s->width += glyph->pixel_width;
17606 /* Adjust base line for subscript/superscript text. */
17607 s->ybase += voffset;
17609 /* The case that face->gc == 0 is handled when drawing the glyph
17610 string by calling PREPARE_FACE_FOR_DISPLAY. */
17611 xassert (s->face);
17612 return glyph - s->row->glyphs[s->area];
17616 /* EXPORT for RIF:
17617 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17618 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17619 assumed to be zero. */
17621 void
17622 x_get_glyph_overhangs (glyph, f, left, right)
17623 struct glyph *glyph;
17624 struct frame *f;
17625 int *left, *right;
17627 *left = *right = 0;
17629 if (glyph->type == CHAR_GLYPH)
17631 XFontStruct *font;
17632 struct face *face;
17633 struct font_info *font_info;
17634 XChar2b char2b;
17635 XCharStruct *pcm;
17637 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17638 font = face->font;
17639 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17640 if (font /* ++KFS: Should this be font_info ? */
17641 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17643 if (pcm->rbearing > pcm->width)
17644 *right = pcm->rbearing - pcm->width;
17645 if (pcm->lbearing < 0)
17646 *left = -pcm->lbearing;
17652 /* Return the index of the first glyph preceding glyph string S that
17653 is overwritten by S because of S's left overhang. Value is -1
17654 if no glyphs are overwritten. */
17656 static int
17657 left_overwritten (s)
17658 struct glyph_string *s;
17660 int k;
17662 if (s->left_overhang)
17664 int x = 0, i;
17665 struct glyph *glyphs = s->row->glyphs[s->area];
17666 int first = s->first_glyph - glyphs;
17668 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17669 x -= glyphs[i].pixel_width;
17671 k = i + 1;
17673 else
17674 k = -1;
17676 return k;
17680 /* Return the index of the first glyph preceding glyph string S that
17681 is overwriting S because of its right overhang. Value is -1 if no
17682 glyph in front of S overwrites S. */
17684 static int
17685 left_overwriting (s)
17686 struct glyph_string *s;
17688 int i, k, x;
17689 struct glyph *glyphs = s->row->glyphs[s->area];
17690 int first = s->first_glyph - glyphs;
17692 k = -1;
17693 x = 0;
17694 for (i = first - 1; i >= 0; --i)
17696 int left, right;
17697 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17698 if (x + right > 0)
17699 k = i;
17700 x -= glyphs[i].pixel_width;
17703 return k;
17707 /* Return the index of the last glyph following glyph string S that is
17708 not overwritten by S because of S's right overhang. Value is -1 if
17709 no such glyph is found. */
17711 static int
17712 right_overwritten (s)
17713 struct glyph_string *s;
17715 int k = -1;
17717 if (s->right_overhang)
17719 int x = 0, i;
17720 struct glyph *glyphs = s->row->glyphs[s->area];
17721 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17722 int end = s->row->used[s->area];
17724 for (i = first; i < end && s->right_overhang > x; ++i)
17725 x += glyphs[i].pixel_width;
17727 k = i;
17730 return k;
17734 /* Return the index of the last glyph following glyph string S that
17735 overwrites S because of its left overhang. Value is negative
17736 if no such glyph is found. */
17738 static int
17739 right_overwriting (s)
17740 struct glyph_string *s;
17742 int i, k, x;
17743 int end = s->row->used[s->area];
17744 struct glyph *glyphs = s->row->glyphs[s->area];
17745 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17747 k = -1;
17748 x = 0;
17749 for (i = first; i < end; ++i)
17751 int left, right;
17752 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17753 if (x - left < 0)
17754 k = i;
17755 x += glyphs[i].pixel_width;
17758 return k;
17762 /* Get face and two-byte form of character C in face FACE_ID on frame
17763 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17764 means we want to display multibyte text. DISPLAY_P non-zero means
17765 make sure that X resources for the face returned are allocated.
17766 Value is a pointer to a realized face that is ready for display if
17767 DISPLAY_P is non-zero. */
17769 static INLINE struct face *
17770 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17771 struct frame *f;
17772 int c, face_id;
17773 XChar2b *char2b;
17774 int multibyte_p, display_p;
17776 struct face *face = FACE_FROM_ID (f, face_id);
17778 if (!multibyte_p)
17780 /* Unibyte case. We don't have to encode, but we have to make
17781 sure to use a face suitable for unibyte. */
17782 STORE_XCHAR2B (char2b, 0, c);
17783 face_id = FACE_FOR_CHAR (f, face, c);
17784 face = FACE_FROM_ID (f, face_id);
17786 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17788 /* Case of ASCII in a face known to fit ASCII. */
17789 STORE_XCHAR2B (char2b, 0, c);
17791 else
17793 int c1, c2, charset;
17795 /* Split characters into bytes. If c2 is -1 afterwards, C is
17796 really a one-byte character so that byte1 is zero. */
17797 SPLIT_CHAR (c, charset, c1, c2);
17798 if (c2 > 0)
17799 STORE_XCHAR2B (char2b, c1, c2);
17800 else
17801 STORE_XCHAR2B (char2b, 0, c1);
17803 /* Maybe encode the character in *CHAR2B. */
17804 if (face->font != NULL)
17806 struct font_info *font_info
17807 = FONT_INFO_FROM_ID (f, face->font_info_id);
17808 if (font_info)
17809 rif->encode_char (c, char2b, font_info, 0);
17813 /* Make sure X resources of the face are allocated. */
17814 #ifdef HAVE_X_WINDOWS
17815 if (display_p)
17816 #endif
17818 xassert (face != NULL);
17819 PREPARE_FACE_FOR_DISPLAY (f, face);
17822 return face;
17826 /* Set background width of glyph string S. START is the index of the
17827 first glyph following S. LAST_X is the right-most x-position + 1
17828 in the drawing area. */
17830 static INLINE void
17831 set_glyph_string_background_width (s, start, last_x)
17832 struct glyph_string *s;
17833 int start;
17834 int last_x;
17836 /* If the face of this glyph string has to be drawn to the end of
17837 the drawing area, set S->extends_to_end_of_line_p. */
17838 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17840 if (start == s->row->used[s->area]
17841 && s->area == TEXT_AREA
17842 && ((s->hl == DRAW_NORMAL_TEXT
17843 && (s->row->fill_line_p
17844 || s->face->background != default_face->background
17845 || s->face->stipple != default_face->stipple
17846 || s->row->mouse_face_p))
17847 || s->hl == DRAW_MOUSE_FACE
17848 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17849 && s->row->fill_line_p)))
17850 s->extends_to_end_of_line_p = 1;
17852 /* If S extends its face to the end of the line, set its
17853 background_width to the distance to the right edge of the drawing
17854 area. */
17855 if (s->extends_to_end_of_line_p)
17856 s->background_width = last_x - s->x + 1;
17857 else
17858 s->background_width = s->width;
17862 /* Compute overhangs and x-positions for glyph string S and its
17863 predecessors, or successors. X is the starting x-position for S.
17864 BACKWARD_P non-zero means process predecessors. */
17866 static void
17867 compute_overhangs_and_x (s, x, backward_p)
17868 struct glyph_string *s;
17869 int x;
17870 int backward_p;
17872 if (backward_p)
17874 while (s)
17876 if (rif->compute_glyph_string_overhangs)
17877 rif->compute_glyph_string_overhangs (s);
17878 x -= s->width;
17879 s->x = x;
17880 s = s->prev;
17883 else
17885 while (s)
17887 if (rif->compute_glyph_string_overhangs)
17888 rif->compute_glyph_string_overhangs (s);
17889 s->x = x;
17890 x += s->width;
17891 s = s->next;
17898 /* The following macros are only called from draw_glyphs below.
17899 They reference the following parameters of that function directly:
17900 `w', `row', `area', and `overlap_p'
17901 as well as the following local variables:
17902 `s', `f', and `hdc' (in W32) */
17904 #ifdef HAVE_NTGUI
17905 /* On W32, silently add local `hdc' variable to argument list of
17906 init_glyph_string. */
17907 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17908 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17909 #else
17910 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17911 init_glyph_string (s, char2b, w, row, area, start, hl)
17912 #endif
17914 /* Add a glyph string for a stretch glyph to the list of strings
17915 between HEAD and TAIL. START is the index of the stretch glyph in
17916 row area AREA of glyph row ROW. END is the index of the last glyph
17917 in that glyph row area. X is the current output position assigned
17918 to the new glyph string constructed. HL overrides that face of the
17919 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17920 is the right-most x-position of the drawing area. */
17922 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17923 and below -- keep them on one line. */
17924 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17925 do \
17927 s = (struct glyph_string *) alloca (sizeof *s); \
17928 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17929 START = fill_stretch_glyph_string (s, row, area, START, END); \
17930 append_glyph_string (&HEAD, &TAIL, s); \
17931 s->x = (X); \
17933 while (0)
17936 /* Add a glyph string for an image glyph to the list of strings
17937 between HEAD and TAIL. START is the index of the image glyph in
17938 row area AREA of glyph row ROW. END is the index of the last glyph
17939 in that glyph row area. X is the current output position assigned
17940 to the new glyph string constructed. HL overrides that face of the
17941 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17942 is the right-most x-position of the drawing area. */
17944 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17945 do \
17947 s = (struct glyph_string *) alloca (sizeof *s); \
17948 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17949 fill_image_glyph_string (s); \
17950 append_glyph_string (&HEAD, &TAIL, s); \
17951 ++START; \
17952 s->x = (X); \
17954 while (0)
17957 /* Add a glyph string for a sequence of character glyphs to the list
17958 of strings between HEAD and TAIL. START is the index of the first
17959 glyph in row area AREA of glyph row ROW that is part of the new
17960 glyph string. END is the index of the last glyph in that glyph row
17961 area. X is the current output position assigned to the new glyph
17962 string constructed. HL overrides that face of the glyph; e.g. it
17963 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17964 right-most x-position of the drawing area. */
17966 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17967 do \
17969 int c, face_id; \
17970 XChar2b *char2b; \
17972 c = (row)->glyphs[area][START].u.ch; \
17973 face_id = (row)->glyphs[area][START].face_id; \
17975 s = (struct glyph_string *) alloca (sizeof *s); \
17976 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17977 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17978 append_glyph_string (&HEAD, &TAIL, s); \
17979 s->x = (X); \
17980 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17982 while (0)
17985 /* Add a glyph string for a composite sequence to the list of strings
17986 between HEAD and TAIL. START is the index of the first glyph in
17987 row area AREA of glyph row ROW that is part of the new glyph
17988 string. END is the index of the last glyph in that glyph row area.
17989 X is the current output position assigned to the new glyph string
17990 constructed. HL overrides that face of the glyph; e.g. it is
17991 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17992 x-position of the drawing area. */
17994 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17995 do { \
17996 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17997 int face_id = (row)->glyphs[area][START].face_id; \
17998 struct face *base_face = FACE_FROM_ID (f, face_id); \
17999 struct composition *cmp = composition_table[cmp_id]; \
18000 int glyph_len = cmp->glyph_len; \
18001 XChar2b *char2b; \
18002 struct face **faces; \
18003 struct glyph_string *first_s = NULL; \
18004 int n; \
18006 base_face = base_face->ascii_face; \
18007 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18008 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18009 /* At first, fill in `char2b' and `faces'. */ \
18010 for (n = 0; n < glyph_len; n++) \
18012 int c = COMPOSITION_GLYPH (cmp, n); \
18013 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18014 faces[n] = FACE_FROM_ID (f, this_face_id); \
18015 get_char_face_and_encoding (f, c, this_face_id, \
18016 char2b + n, 1, 1); \
18019 /* Make glyph_strings for each glyph sequence that is drawable by \
18020 the same face, and append them to HEAD/TAIL. */ \
18021 for (n = 0; n < cmp->glyph_len;) \
18023 s = (struct glyph_string *) alloca (sizeof *s); \
18024 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18025 append_glyph_string (&(HEAD), &(TAIL), s); \
18026 s->cmp = cmp; \
18027 s->gidx = n; \
18028 s->x = (X); \
18030 if (n == 0) \
18031 first_s = s; \
18033 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18036 ++START; \
18037 s = first_s; \
18038 } while (0)
18041 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18042 of AREA of glyph row ROW on window W between indices START and END.
18043 HL overrides the face for drawing glyph strings, e.g. it is
18044 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18045 x-positions of the drawing area.
18047 This is an ugly monster macro construct because we must use alloca
18048 to allocate glyph strings (because draw_glyphs can be called
18049 asynchronously). */
18051 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18052 do \
18054 HEAD = TAIL = NULL; \
18055 while (START < END) \
18057 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18058 switch (first_glyph->type) \
18060 case CHAR_GLYPH: \
18061 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18062 HL, X, LAST_X); \
18063 break; \
18065 case COMPOSITE_GLYPH: \
18066 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18067 HL, X, LAST_X); \
18068 break; \
18070 case STRETCH_GLYPH: \
18071 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18072 HL, X, LAST_X); \
18073 break; \
18075 case IMAGE_GLYPH: \
18076 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18077 HL, X, LAST_X); \
18078 break; \
18080 default: \
18081 abort (); \
18084 set_glyph_string_background_width (s, START, LAST_X); \
18085 (X) += s->width; \
18088 while (0)
18091 /* Draw glyphs between START and END in AREA of ROW on window W,
18092 starting at x-position X. X is relative to AREA in W. HL is a
18093 face-override with the following meaning:
18095 DRAW_NORMAL_TEXT draw normally
18096 DRAW_CURSOR draw in cursor face
18097 DRAW_MOUSE_FACE draw in mouse face.
18098 DRAW_INVERSE_VIDEO draw in mode line face
18099 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18100 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18102 If OVERLAPS_P is non-zero, draw only the foreground of characters
18103 and clip to the physical height of ROW.
18105 Value is the x-position reached, relative to AREA of W. */
18107 static int
18108 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18109 struct window *w;
18110 int x;
18111 struct glyph_row *row;
18112 enum glyph_row_area area;
18113 int start, end;
18114 enum draw_glyphs_face hl;
18115 int overlaps_p;
18117 struct glyph_string *head, *tail;
18118 struct glyph_string *s;
18119 int last_x, area_width;
18120 int x_reached;
18121 int i, j;
18122 struct frame *f = XFRAME (WINDOW_FRAME (w));
18123 DECLARE_HDC (hdc);
18125 ALLOCATE_HDC (hdc, f);
18127 /* Let's rather be paranoid than getting a SEGV. */
18128 end = min (end, row->used[area]);
18129 start = max (0, start);
18130 start = min (end, start);
18132 /* Translate X to frame coordinates. Set last_x to the right
18133 end of the drawing area. */
18134 if (row->full_width_p)
18136 /* X is relative to the left edge of W, without scroll bars
18137 or fringes. */
18138 x += WINDOW_LEFT_EDGE_X (w);
18139 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18141 else
18143 int area_left = window_box_left (w, area);
18144 x += area_left;
18145 area_width = window_box_width (w, area);
18146 last_x = area_left + area_width;
18149 /* Build a doubly-linked list of glyph_string structures between
18150 head and tail from what we have to draw. Note that the macro
18151 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18152 the reason we use a separate variable `i'. */
18153 i = start;
18154 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18155 if (tail)
18156 x_reached = tail->x + tail->background_width;
18157 else
18158 x_reached = x;
18160 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18161 the row, redraw some glyphs in front or following the glyph
18162 strings built above. */
18163 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18165 int dummy_x = 0;
18166 struct glyph_string *h, *t;
18168 /* Compute overhangs for all glyph strings. */
18169 if (rif->compute_glyph_string_overhangs)
18170 for (s = head; s; s = s->next)
18171 rif->compute_glyph_string_overhangs (s);
18173 /* Prepend glyph strings for glyphs in front of the first glyph
18174 string that are overwritten because of the first glyph
18175 string's left overhang. The background of all strings
18176 prepended must be drawn because the first glyph string
18177 draws over it. */
18178 i = left_overwritten (head);
18179 if (i >= 0)
18181 j = i;
18182 BUILD_GLYPH_STRINGS (j, start, h, t,
18183 DRAW_NORMAL_TEXT, dummy_x, last_x);
18184 start = i;
18185 compute_overhangs_and_x (t, head->x, 1);
18186 prepend_glyph_string_lists (&head, &tail, h, t);
18189 /* Prepend glyph strings for glyphs in front of the first glyph
18190 string that overwrite that glyph string because of their
18191 right overhang. For these strings, only the foreground must
18192 be drawn, because it draws over the glyph string at `head'.
18193 The background must not be drawn because this would overwrite
18194 right overhangs of preceding glyphs for which no glyph
18195 strings exist. */
18196 i = left_overwriting (head);
18197 if (i >= 0)
18199 BUILD_GLYPH_STRINGS (i, start, h, t,
18200 DRAW_NORMAL_TEXT, dummy_x, last_x);
18201 for (s = h; s; s = s->next)
18202 s->background_filled_p = 1;
18203 compute_overhangs_and_x (t, head->x, 1);
18204 prepend_glyph_string_lists (&head, &tail, h, t);
18207 /* Append glyphs strings for glyphs following the last glyph
18208 string tail that are overwritten by tail. The background of
18209 these strings has to be drawn because tail's foreground draws
18210 over it. */
18211 i = right_overwritten (tail);
18212 if (i >= 0)
18214 BUILD_GLYPH_STRINGS (end, i, h, t,
18215 DRAW_NORMAL_TEXT, x, last_x);
18216 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18217 append_glyph_string_lists (&head, &tail, h, t);
18220 /* Append glyph strings for glyphs following the last glyph
18221 string tail that overwrite tail. The foreground of such
18222 glyphs has to be drawn because it writes into the background
18223 of tail. The background must not be drawn because it could
18224 paint over the foreground of following glyphs. */
18225 i = right_overwriting (tail);
18226 if (i >= 0)
18228 BUILD_GLYPH_STRINGS (end, i, h, t,
18229 DRAW_NORMAL_TEXT, x, last_x);
18230 for (s = h; s; s = s->next)
18231 s->background_filled_p = 1;
18232 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18233 append_glyph_string_lists (&head, &tail, h, t);
18237 /* Draw all strings. */
18238 for (s = head; s; s = s->next)
18239 rif->draw_glyph_string (s);
18241 if (area == TEXT_AREA
18242 && !row->full_width_p
18243 /* When drawing overlapping rows, only the glyph strings'
18244 foreground is drawn, which doesn't erase a cursor
18245 completely. */
18246 && !overlaps_p)
18248 int x0 = head ? head->x : x;
18249 int x1 = tail ? tail->x + tail->background_width : x;
18251 int text_left = window_box_left (w, TEXT_AREA);
18252 x0 -= text_left;
18253 x1 -= text_left;
18255 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18256 row->y, MATRIX_ROW_BOTTOM_Y (row));
18259 /* Value is the x-position up to which drawn, relative to AREA of W.
18260 This doesn't include parts drawn because of overhangs. */
18261 if (row->full_width_p)
18262 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18263 else
18264 x_reached -= window_box_left (w, area);
18266 RELEASE_HDC (hdc, f);
18268 return x_reached;
18272 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18273 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18275 static INLINE void
18276 append_glyph (it)
18277 struct it *it;
18279 struct glyph *glyph;
18280 enum glyph_row_area area = it->area;
18282 xassert (it->glyph_row);
18283 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18285 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18286 if (glyph < it->glyph_row->glyphs[area + 1])
18288 glyph->charpos = CHARPOS (it->position);
18289 glyph->object = it->object;
18290 glyph->pixel_width = it->pixel_width;
18291 glyph->ascent = it->ascent;
18292 glyph->descent = it->descent;
18293 glyph->voffset = it->voffset;
18294 glyph->type = CHAR_GLYPH;
18295 glyph->multibyte_p = it->multibyte_p;
18296 glyph->left_box_line_p = it->start_of_box_run_p;
18297 glyph->right_box_line_p = it->end_of_box_run_p;
18298 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18299 || it->phys_descent > it->descent);
18300 glyph->padding_p = 0;
18301 glyph->glyph_not_available_p = it->glyph_not_available_p;
18302 glyph->face_id = it->face_id;
18303 glyph->u.ch = it->char_to_display;
18304 glyph->slice = null_glyph_slice;
18305 glyph->font_type = FONT_TYPE_UNKNOWN;
18306 ++it->glyph_row->used[area];
18308 else if (!fonts_changed_p)
18310 it->w->ncols_scale_factor++;
18311 fonts_changed_p = 1;
18315 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18316 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18318 static INLINE void
18319 append_composite_glyph (it)
18320 struct it *it;
18322 struct glyph *glyph;
18323 enum glyph_row_area area = it->area;
18325 xassert (it->glyph_row);
18327 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18328 if (glyph < it->glyph_row->glyphs[area + 1])
18330 glyph->charpos = CHARPOS (it->position);
18331 glyph->object = it->object;
18332 glyph->pixel_width = it->pixel_width;
18333 glyph->ascent = it->ascent;
18334 glyph->descent = it->descent;
18335 glyph->voffset = it->voffset;
18336 glyph->type = COMPOSITE_GLYPH;
18337 glyph->multibyte_p = it->multibyte_p;
18338 glyph->left_box_line_p = it->start_of_box_run_p;
18339 glyph->right_box_line_p = it->end_of_box_run_p;
18340 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18341 || it->phys_descent > it->descent);
18342 glyph->padding_p = 0;
18343 glyph->glyph_not_available_p = 0;
18344 glyph->face_id = it->face_id;
18345 glyph->u.cmp_id = it->cmp_id;
18346 glyph->slice = null_glyph_slice;
18347 glyph->font_type = FONT_TYPE_UNKNOWN;
18348 ++it->glyph_row->used[area];
18350 else if (!fonts_changed_p)
18352 it->w->ncols_scale_factor++;
18353 fonts_changed_p = 1;
18358 /* Change IT->ascent and IT->height according to the setting of
18359 IT->voffset. */
18361 static INLINE void
18362 take_vertical_position_into_account (it)
18363 struct it *it;
18365 if (it->voffset)
18367 if (it->voffset < 0)
18368 /* Increase the ascent so that we can display the text higher
18369 in the line. */
18370 it->ascent -= it->voffset;
18371 else
18372 /* Increase the descent so that we can display the text lower
18373 in the line. */
18374 it->descent += it->voffset;
18379 /* Produce glyphs/get display metrics for the image IT is loaded with.
18380 See the description of struct display_iterator in dispextern.h for
18381 an overview of struct display_iterator. */
18383 static void
18384 produce_image_glyph (it)
18385 struct it *it;
18387 struct image *img;
18388 struct face *face;
18389 int glyph_ascent;
18390 struct glyph_slice slice;
18392 xassert (it->what == IT_IMAGE);
18394 face = FACE_FROM_ID (it->f, it->face_id);
18395 xassert (face);
18396 /* Make sure X resources of the face is loaded. */
18397 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18399 if (it->image_id < 0)
18401 /* Fringe bitmap. */
18402 it->ascent = it->phys_ascent = 0;
18403 it->descent = it->phys_descent = 0;
18404 it->pixel_width = 0;
18405 it->nglyphs = 0;
18406 return;
18409 img = IMAGE_FROM_ID (it->f, it->image_id);
18410 xassert (img);
18411 /* Make sure X resources of the image is loaded. */
18412 prepare_image_for_display (it->f, img);
18414 slice.x = slice.y = 0;
18415 slice.width = img->width;
18416 slice.height = img->height;
18418 if (INTEGERP (it->slice.x))
18419 slice.x = XINT (it->slice.x);
18420 else if (FLOATP (it->slice.x))
18421 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18423 if (INTEGERP (it->slice.y))
18424 slice.y = XINT (it->slice.y);
18425 else if (FLOATP (it->slice.y))
18426 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18428 if (INTEGERP (it->slice.width))
18429 slice.width = XINT (it->slice.width);
18430 else if (FLOATP (it->slice.width))
18431 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18433 if (INTEGERP (it->slice.height))
18434 slice.height = XINT (it->slice.height);
18435 else if (FLOATP (it->slice.height))
18436 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18438 if (slice.x >= img->width)
18439 slice.x = img->width;
18440 if (slice.y >= img->height)
18441 slice.y = img->height;
18442 if (slice.x + slice.width >= img->width)
18443 slice.width = img->width - slice.x;
18444 if (slice.y + slice.height > img->height)
18445 slice.height = img->height - slice.y;
18447 if (slice.width == 0 || slice.height == 0)
18448 return;
18450 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18452 it->descent = slice.height - glyph_ascent;
18453 if (slice.y == 0)
18454 it->descent += img->vmargin;
18455 if (slice.y + slice.height == img->height)
18456 it->descent += img->vmargin;
18457 it->phys_descent = it->descent;
18459 it->pixel_width = slice.width;
18460 if (slice.x == 0)
18461 it->pixel_width += img->hmargin;
18462 if (slice.x + slice.width == img->width)
18463 it->pixel_width += img->hmargin;
18465 /* It's quite possible for images to have an ascent greater than
18466 their height, so don't get confused in that case. */
18467 if (it->descent < 0)
18468 it->descent = 0;
18470 #if 0 /* this breaks image tiling */
18471 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18472 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18473 if (face_ascent > it->ascent)
18474 it->ascent = it->phys_ascent = face_ascent;
18475 #endif
18477 it->nglyphs = 1;
18479 if (face->box != FACE_NO_BOX)
18481 if (face->box_line_width > 0)
18483 if (slice.y == 0)
18484 it->ascent += face->box_line_width;
18485 if (slice.y + slice.height == img->height)
18486 it->descent += face->box_line_width;
18489 if (it->start_of_box_run_p && slice.x == 0)
18490 it->pixel_width += abs (face->box_line_width);
18491 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18492 it->pixel_width += abs (face->box_line_width);
18495 take_vertical_position_into_account (it);
18497 if (it->glyph_row)
18499 struct glyph *glyph;
18500 enum glyph_row_area area = it->area;
18502 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18503 if (glyph < it->glyph_row->glyphs[area + 1])
18505 glyph->charpos = CHARPOS (it->position);
18506 glyph->object = it->object;
18507 glyph->pixel_width = it->pixel_width;
18508 glyph->ascent = glyph_ascent;
18509 glyph->descent = it->descent;
18510 glyph->voffset = it->voffset;
18511 glyph->type = IMAGE_GLYPH;
18512 glyph->multibyte_p = it->multibyte_p;
18513 glyph->left_box_line_p = it->start_of_box_run_p;
18514 glyph->right_box_line_p = it->end_of_box_run_p;
18515 glyph->overlaps_vertically_p = 0;
18516 glyph->padding_p = 0;
18517 glyph->glyph_not_available_p = 0;
18518 glyph->face_id = it->face_id;
18519 glyph->u.img_id = img->id;
18520 glyph->slice = slice;
18521 glyph->font_type = FONT_TYPE_UNKNOWN;
18522 ++it->glyph_row->used[area];
18524 else if (!fonts_changed_p)
18526 it->w->ncols_scale_factor++;
18527 fonts_changed_p = 1;
18533 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18534 of the glyph, WIDTH and HEIGHT are the width and height of the
18535 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18537 static void
18538 append_stretch_glyph (it, object, width, height, ascent)
18539 struct it *it;
18540 Lisp_Object object;
18541 int width, height;
18542 int ascent;
18544 struct glyph *glyph;
18545 enum glyph_row_area area = it->area;
18547 xassert (ascent >= 0 && ascent <= height);
18549 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18550 if (glyph < it->glyph_row->glyphs[area + 1])
18552 glyph->charpos = CHARPOS (it->position);
18553 glyph->object = object;
18554 glyph->pixel_width = width;
18555 glyph->ascent = ascent;
18556 glyph->descent = height - ascent;
18557 glyph->voffset = it->voffset;
18558 glyph->type = STRETCH_GLYPH;
18559 glyph->multibyte_p = it->multibyte_p;
18560 glyph->left_box_line_p = it->start_of_box_run_p;
18561 glyph->right_box_line_p = it->end_of_box_run_p;
18562 glyph->overlaps_vertically_p = 0;
18563 glyph->padding_p = 0;
18564 glyph->glyph_not_available_p = 0;
18565 glyph->face_id = it->face_id;
18566 glyph->u.stretch.ascent = ascent;
18567 glyph->u.stretch.height = height;
18568 glyph->slice = null_glyph_slice;
18569 glyph->font_type = FONT_TYPE_UNKNOWN;
18570 ++it->glyph_row->used[area];
18572 else if (!fonts_changed_p)
18574 it->w->ncols_scale_factor++;
18575 fonts_changed_p = 1;
18580 /* Produce a stretch glyph for iterator IT. IT->object is the value
18581 of the glyph property displayed. The value must be a list
18582 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18583 being recognized:
18585 1. `:width WIDTH' specifies that the space should be WIDTH *
18586 canonical char width wide. WIDTH may be an integer or floating
18587 point number.
18589 2. `:relative-width FACTOR' specifies that the width of the stretch
18590 should be computed from the width of the first character having the
18591 `glyph' property, and should be FACTOR times that width.
18593 3. `:align-to HPOS' specifies that the space should be wide enough
18594 to reach HPOS, a value in canonical character units.
18596 Exactly one of the above pairs must be present.
18598 4. `:height HEIGHT' specifies that the height of the stretch produced
18599 should be HEIGHT, measured in canonical character units.
18601 5. `:relative-height FACTOR' specifies that the height of the
18602 stretch should be FACTOR times the height of the characters having
18603 the glyph property.
18605 Either none or exactly one of 4 or 5 must be present.
18607 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18608 of the stretch should be used for the ascent of the stretch.
18609 ASCENT must be in the range 0 <= ASCENT <= 100. */
18611 static void
18612 produce_stretch_glyph (it)
18613 struct it *it;
18615 /* (space :width WIDTH :height HEIGHT ...) */
18616 Lisp_Object prop, plist;
18617 int width = 0, height = 0, align_to = -1;
18618 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18619 int ascent = 0;
18620 double tem;
18621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18622 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18624 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18626 /* List should start with `space'. */
18627 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18628 plist = XCDR (it->object);
18630 /* Compute the width of the stretch. */
18631 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18632 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18634 /* Absolute width `:width WIDTH' specified and valid. */
18635 zero_width_ok_p = 1;
18636 width = (int)tem;
18638 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18639 NUMVAL (prop) > 0)
18641 /* Relative width `:relative-width FACTOR' specified and valid.
18642 Compute the width of the characters having the `glyph'
18643 property. */
18644 struct it it2;
18645 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18647 it2 = *it;
18648 if (it->multibyte_p)
18650 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18651 - IT_BYTEPOS (*it));
18652 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18654 else
18655 it2.c = *p, it2.len = 1;
18657 it2.glyph_row = NULL;
18658 it2.what = IT_CHARACTER;
18659 x_produce_glyphs (&it2);
18660 width = NUMVAL (prop) * it2.pixel_width;
18662 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18663 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18665 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18666 align_to = (align_to < 0
18668 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18669 else if (align_to < 0)
18670 align_to = window_box_left_offset (it->w, TEXT_AREA);
18671 width = max (0, (int)tem + align_to - it->current_x);
18672 zero_width_ok_p = 1;
18674 else
18675 /* Nothing specified -> width defaults to canonical char width. */
18676 width = FRAME_COLUMN_WIDTH (it->f);
18678 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18679 width = 1;
18681 /* Compute height. */
18682 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18683 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18685 height = (int)tem;
18686 zero_height_ok_p = 1;
18688 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18689 NUMVAL (prop) > 0)
18690 height = FONT_HEIGHT (font) * NUMVAL (prop);
18691 else
18692 height = FONT_HEIGHT (font);
18694 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18695 height = 1;
18697 /* Compute percentage of height used for ascent. If
18698 `:ascent ASCENT' is present and valid, use that. Otherwise,
18699 derive the ascent from the font in use. */
18700 if (prop = Fsafe_plist_get (plist, QCascent),
18701 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18702 ascent = height * NUMVAL (prop) / 100.0;
18703 else if (!NILP (prop)
18704 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18705 ascent = min (max (0, (int)tem), height);
18706 else
18707 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18709 if (width > 0 && height > 0 && it->glyph_row)
18711 Lisp_Object object = it->stack[it->sp - 1].string;
18712 if (!STRINGP (object))
18713 object = it->w->buffer;
18714 append_stretch_glyph (it, object, width, height, ascent);
18717 it->pixel_width = width;
18718 it->ascent = it->phys_ascent = ascent;
18719 it->descent = it->phys_descent = height - it->ascent;
18720 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18722 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18724 if (face->box_line_width > 0)
18726 it->ascent += face->box_line_width;
18727 it->descent += face->box_line_width;
18730 if (it->start_of_box_run_p)
18731 it->pixel_width += abs (face->box_line_width);
18732 if (it->end_of_box_run_p)
18733 it->pixel_width += abs (face->box_line_width);
18736 take_vertical_position_into_account (it);
18739 /* Calculate line-height and line-spacing properties.
18740 An integer value specifies explicit pixel value.
18741 A float value specifies relative value to current face height.
18742 A cons (float . face-name) specifies relative value to
18743 height of specified face font.
18745 Returns height in pixels, or nil. */
18747 static Lisp_Object
18748 calc_line_height_property (it, prop, font, boff, total)
18749 struct it *it;
18750 Lisp_Object prop;
18751 XFontStruct *font;
18752 int boff, *total;
18754 Lisp_Object position, val;
18755 Lisp_Object face_name = Qnil;
18756 int ascent, descent, height, override;
18758 if (STRINGP (it->object))
18759 position = make_number (IT_STRING_CHARPOS (*it));
18760 else if (BUFFERP (it->object))
18761 position = make_number (IT_CHARPOS (*it));
18762 else
18763 return Qnil;
18765 val = Fget_char_property (position, prop, it->object);
18767 if (NILP (val))
18768 return val;
18770 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18772 *total = 1;
18773 val = XCDR (val);
18776 if (INTEGERP (val))
18777 return val;
18779 if (CONSP (val))
18781 face_name = XCDR (val);
18782 val = XCAR (val);
18784 else if (SYMBOLP (val))
18786 face_name = val;
18787 val = Qnil;
18790 override = EQ (prop, Qline_height);
18792 if (NILP (face_name))
18794 font = FRAME_FONT (it->f);
18795 boff = FRAME_BASELINE_OFFSET (it->f);
18797 else if (EQ (face_name, Qt))
18799 override = 0;
18801 else
18803 int face_id;
18804 struct face *face;
18805 struct font_info *font_info;
18807 face_id = lookup_named_face (it->f, face_name, ' ');
18808 if (face_id < 0)
18809 return make_number (-1);
18811 face = FACE_FROM_ID (it->f, face_id);
18812 font = face->font;
18813 if (font == NULL)
18814 return make_number (-1);
18816 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18817 boff = font_info->baseline_offset;
18818 if (font_info->vertical_centering)
18819 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18822 ascent = FONT_BASE (font) + boff;
18823 descent = FONT_DESCENT (font) - boff;
18825 if (override)
18827 it->override_ascent = ascent;
18828 it->override_descent = descent;
18829 it->override_boff = boff;
18832 height = ascent + descent;
18833 if (FLOATP (val))
18834 height = (int)(XFLOAT_DATA (val) * height);
18835 else if (INTEGERP (val))
18836 height *= XINT (val);
18838 return make_number (height);
18842 /* RIF:
18843 Produce glyphs/get display metrics for the display element IT is
18844 loaded with. See the description of struct display_iterator in
18845 dispextern.h for an overview of struct display_iterator. */
18847 void
18848 x_produce_glyphs (it)
18849 struct it *it;
18851 int extra_line_spacing = it->extra_line_spacing;
18853 it->glyph_not_available_p = 0;
18855 if (it->what == IT_CHARACTER)
18857 XChar2b char2b;
18858 XFontStruct *font;
18859 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18860 XCharStruct *pcm;
18861 int font_not_found_p;
18862 struct font_info *font_info;
18863 int boff; /* baseline offset */
18864 /* We may change it->multibyte_p upon unibyte<->multibyte
18865 conversion. So, save the current value now and restore it
18866 later.
18868 Note: It seems that we don't have to record multibyte_p in
18869 struct glyph because the character code itself tells if or
18870 not the character is multibyte. Thus, in the future, we must
18871 consider eliminating the field `multibyte_p' in the struct
18872 glyph. */
18873 int saved_multibyte_p = it->multibyte_p;
18875 /* Maybe translate single-byte characters to multibyte, or the
18876 other way. */
18877 it->char_to_display = it->c;
18878 if (!ASCII_BYTE_P (it->c))
18880 if (unibyte_display_via_language_environment
18881 && SINGLE_BYTE_CHAR_P (it->c)
18882 && (it->c >= 0240
18883 || !NILP (Vnonascii_translation_table)))
18885 it->char_to_display = unibyte_char_to_multibyte (it->c);
18886 it->multibyte_p = 1;
18887 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18888 face = FACE_FROM_ID (it->f, it->face_id);
18890 else if (!SINGLE_BYTE_CHAR_P (it->c)
18891 && !it->multibyte_p)
18893 it->multibyte_p = 1;
18894 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18895 face = FACE_FROM_ID (it->f, it->face_id);
18899 /* Get font to use. Encode IT->char_to_display. */
18900 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18901 &char2b, it->multibyte_p, 0);
18902 font = face->font;
18904 /* When no suitable font found, use the default font. */
18905 font_not_found_p = font == NULL;
18906 if (font_not_found_p)
18908 font = FRAME_FONT (it->f);
18909 boff = FRAME_BASELINE_OFFSET (it->f);
18910 font_info = NULL;
18912 else
18914 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18915 boff = font_info->baseline_offset;
18916 if (font_info->vertical_centering)
18917 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18920 if (it->char_to_display >= ' '
18921 && (!it->multibyte_p || it->char_to_display < 128))
18923 /* Either unibyte or ASCII. */
18924 int stretched_p;
18926 it->nglyphs = 1;
18928 pcm = rif->per_char_metric (font, &char2b,
18929 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18931 if (it->override_ascent >= 0)
18933 it->ascent = it->override_ascent;
18934 it->descent = it->override_descent;
18935 boff = it->override_boff;
18937 else
18939 it->ascent = FONT_BASE (font) + boff;
18940 it->descent = FONT_DESCENT (font) - boff;
18943 if (pcm)
18945 it->phys_ascent = pcm->ascent + boff;
18946 it->phys_descent = pcm->descent - boff;
18947 it->pixel_width = pcm->width;
18949 else
18951 it->glyph_not_available_p = 1;
18952 it->phys_ascent = it->ascent;
18953 it->phys_descent = it->descent;
18954 it->pixel_width = FONT_WIDTH (font);
18957 if (it->constrain_row_ascent_descent_p)
18959 if (it->descent > it->max_descent)
18961 it->ascent += it->descent - it->max_descent;
18962 it->descent = it->max_descent;
18964 if (it->ascent > it->max_ascent)
18966 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18967 it->ascent = it->max_ascent;
18969 it->phys_ascent = min (it->phys_ascent, it->ascent);
18970 it->phys_descent = min (it->phys_descent, it->descent);
18971 extra_line_spacing = 0;
18974 /* If this is a space inside a region of text with
18975 `space-width' property, change its width. */
18976 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18977 if (stretched_p)
18978 it->pixel_width *= XFLOATINT (it->space_width);
18980 /* If face has a box, add the box thickness to the character
18981 height. If character has a box line to the left and/or
18982 right, add the box line width to the character's width. */
18983 if (face->box != FACE_NO_BOX)
18985 int thick = face->box_line_width;
18987 if (thick > 0)
18989 it->ascent += thick;
18990 it->descent += thick;
18992 else
18993 thick = -thick;
18995 if (it->start_of_box_run_p)
18996 it->pixel_width += thick;
18997 if (it->end_of_box_run_p)
18998 it->pixel_width += thick;
19001 /* If face has an overline, add the height of the overline
19002 (1 pixel) and a 1 pixel margin to the character height. */
19003 if (face->overline_p)
19004 it->ascent += 2;
19006 if (it->constrain_row_ascent_descent_p)
19008 if (it->ascent > it->max_ascent)
19009 it->ascent = it->max_ascent;
19010 if (it->descent > it->max_descent)
19011 it->descent = it->max_descent;
19014 take_vertical_position_into_account (it);
19016 /* If we have to actually produce glyphs, do it. */
19017 if (it->glyph_row)
19019 if (stretched_p)
19021 /* Translate a space with a `space-width' property
19022 into a stretch glyph. */
19023 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19024 / FONT_HEIGHT (font));
19025 append_stretch_glyph (it, it->object, it->pixel_width,
19026 it->ascent + it->descent, ascent);
19028 else
19029 append_glyph (it);
19031 /* If characters with lbearing or rbearing are displayed
19032 in this line, record that fact in a flag of the
19033 glyph row. This is used to optimize X output code. */
19034 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19035 it->glyph_row->contains_overlapping_glyphs_p = 1;
19038 else if (it->char_to_display == '\n')
19040 /* A newline has no width but we need the height of the line.
19041 But if previous part of the line set a height, don't
19042 increase that height */
19044 Lisp_Object height;
19046 it->override_ascent = -1;
19047 it->pixel_width = 0;
19048 it->nglyphs = 0;
19050 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19052 if (it->override_ascent >= 0)
19054 it->ascent = it->override_ascent;
19055 it->descent = it->override_descent;
19056 boff = it->override_boff;
19058 else
19060 it->ascent = FONT_BASE (font) + boff;
19061 it->descent = FONT_DESCENT (font) - boff;
19064 if (EQ (height, make_number(0)))
19066 if (it->descent > it->max_descent)
19068 it->ascent += it->descent - it->max_descent;
19069 it->descent = it->max_descent;
19071 if (it->ascent > it->max_ascent)
19073 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19074 it->ascent = it->max_ascent;
19076 it->phys_ascent = min (it->phys_ascent, it->ascent);
19077 it->phys_descent = min (it->phys_descent, it->descent);
19078 it->constrain_row_ascent_descent_p = 1;
19079 extra_line_spacing = 0;
19081 else
19083 Lisp_Object spacing;
19084 int total = 0;
19086 it->phys_ascent = it->ascent;
19087 it->phys_descent = it->descent;
19089 if ((it->max_ascent > 0 || it->max_descent > 0)
19090 && face->box != FACE_NO_BOX
19091 && face->box_line_width > 0)
19093 it->ascent += face->box_line_width;
19094 it->descent += face->box_line_width;
19096 if (!NILP (height)
19097 && XINT (height) > it->ascent + it->descent)
19098 it->ascent = XINT (height) - it->descent;
19100 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19101 if (INTEGERP (spacing))
19103 extra_line_spacing = XINT (spacing);
19104 if (total)
19105 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19109 else if (it->char_to_display == '\t')
19111 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19112 int x = it->current_x + it->continuation_lines_width;
19113 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19115 /* If the distance from the current position to the next tab
19116 stop is less than a canonical character width, use the
19117 tab stop after that. */
19118 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19119 next_tab_x += tab_width;
19121 it->pixel_width = next_tab_x - x;
19122 it->nglyphs = 1;
19123 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19124 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19126 if (it->glyph_row)
19128 append_stretch_glyph (it, it->object, it->pixel_width,
19129 it->ascent + it->descent, it->ascent);
19132 else
19134 /* A multi-byte character. Assume that the display width of the
19135 character is the width of the character multiplied by the
19136 width of the font. */
19138 /* If we found a font, this font should give us the right
19139 metrics. If we didn't find a font, use the frame's
19140 default font and calculate the width of the character
19141 from the charset width; this is what old redisplay code
19142 did. */
19144 pcm = rif->per_char_metric (font, &char2b,
19145 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19147 if (font_not_found_p || !pcm)
19149 int charset = CHAR_CHARSET (it->char_to_display);
19151 it->glyph_not_available_p = 1;
19152 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19153 * CHARSET_WIDTH (charset));
19154 it->phys_ascent = FONT_BASE (font) + boff;
19155 it->phys_descent = FONT_DESCENT (font) - boff;
19157 else
19159 it->pixel_width = pcm->width;
19160 it->phys_ascent = pcm->ascent + boff;
19161 it->phys_descent = pcm->descent - boff;
19162 if (it->glyph_row
19163 && (pcm->lbearing < 0
19164 || pcm->rbearing > pcm->width))
19165 it->glyph_row->contains_overlapping_glyphs_p = 1;
19167 it->nglyphs = 1;
19168 it->ascent = FONT_BASE (font) + boff;
19169 it->descent = FONT_DESCENT (font) - boff;
19170 if (face->box != FACE_NO_BOX)
19172 int thick = face->box_line_width;
19174 if (thick > 0)
19176 it->ascent += thick;
19177 it->descent += thick;
19179 else
19180 thick = - thick;
19182 if (it->start_of_box_run_p)
19183 it->pixel_width += thick;
19184 if (it->end_of_box_run_p)
19185 it->pixel_width += thick;
19188 /* If face has an overline, add the height of the overline
19189 (1 pixel) and a 1 pixel margin to the character height. */
19190 if (face->overline_p)
19191 it->ascent += 2;
19193 take_vertical_position_into_account (it);
19195 if (it->glyph_row)
19196 append_glyph (it);
19198 it->multibyte_p = saved_multibyte_p;
19200 else if (it->what == IT_COMPOSITION)
19202 /* Note: A composition is represented as one glyph in the
19203 glyph matrix. There are no padding glyphs. */
19204 XChar2b char2b;
19205 XFontStruct *font;
19206 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19207 XCharStruct *pcm;
19208 int font_not_found_p;
19209 struct font_info *font_info;
19210 int boff; /* baseline offset */
19211 struct composition *cmp = composition_table[it->cmp_id];
19213 /* Maybe translate single-byte characters to multibyte. */
19214 it->char_to_display = it->c;
19215 if (unibyte_display_via_language_environment
19216 && SINGLE_BYTE_CHAR_P (it->c)
19217 && (it->c >= 0240
19218 || (it->c >= 0200
19219 && !NILP (Vnonascii_translation_table))))
19221 it->char_to_display = unibyte_char_to_multibyte (it->c);
19224 /* Get face and font to use. Encode IT->char_to_display. */
19225 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19226 face = FACE_FROM_ID (it->f, it->face_id);
19227 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19228 &char2b, it->multibyte_p, 0);
19229 font = face->font;
19231 /* When no suitable font found, use the default font. */
19232 font_not_found_p = font == NULL;
19233 if (font_not_found_p)
19235 font = FRAME_FONT (it->f);
19236 boff = FRAME_BASELINE_OFFSET (it->f);
19237 font_info = NULL;
19239 else
19241 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19242 boff = font_info->baseline_offset;
19243 if (font_info->vertical_centering)
19244 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19247 /* There are no padding glyphs, so there is only one glyph to
19248 produce for the composition. Important is that pixel_width,
19249 ascent and descent are the values of what is drawn by
19250 draw_glyphs (i.e. the values of the overall glyphs composed). */
19251 it->nglyphs = 1;
19253 /* If we have not yet calculated pixel size data of glyphs of
19254 the composition for the current face font, calculate them
19255 now. Theoretically, we have to check all fonts for the
19256 glyphs, but that requires much time and memory space. So,
19257 here we check only the font of the first glyph. This leads
19258 to incorrect display very rarely, and C-l (recenter) can
19259 correct the display anyway. */
19260 if (cmp->font != (void *) font)
19262 /* Ascent and descent of the font of the first character of
19263 this composition (adjusted by baseline offset). Ascent
19264 and descent of overall glyphs should not be less than
19265 them respectively. */
19266 int font_ascent = FONT_BASE (font) + boff;
19267 int font_descent = FONT_DESCENT (font) - boff;
19268 /* Bounding box of the overall glyphs. */
19269 int leftmost, rightmost, lowest, highest;
19270 int i, width, ascent, descent;
19272 cmp->font = (void *) font;
19274 /* Initialize the bounding box. */
19275 if (font_info
19276 && (pcm = rif->per_char_metric (font, &char2b,
19277 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19279 width = pcm->width;
19280 ascent = pcm->ascent;
19281 descent = pcm->descent;
19283 else
19285 width = FONT_WIDTH (font);
19286 ascent = FONT_BASE (font);
19287 descent = FONT_DESCENT (font);
19290 rightmost = width;
19291 lowest = - descent + boff;
19292 highest = ascent + boff;
19293 leftmost = 0;
19295 if (font_info
19296 && font_info->default_ascent
19297 && CHAR_TABLE_P (Vuse_default_ascent)
19298 && !NILP (Faref (Vuse_default_ascent,
19299 make_number (it->char_to_display))))
19300 highest = font_info->default_ascent + boff;
19302 /* Draw the first glyph at the normal position. It may be
19303 shifted to right later if some other glyphs are drawn at
19304 the left. */
19305 cmp->offsets[0] = 0;
19306 cmp->offsets[1] = boff;
19308 /* Set cmp->offsets for the remaining glyphs. */
19309 for (i = 1; i < cmp->glyph_len; i++)
19311 int left, right, btm, top;
19312 int ch = COMPOSITION_GLYPH (cmp, i);
19313 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19315 face = FACE_FROM_ID (it->f, face_id);
19316 get_char_face_and_encoding (it->f, ch, face->id,
19317 &char2b, it->multibyte_p, 0);
19318 font = face->font;
19319 if (font == NULL)
19321 font = FRAME_FONT (it->f);
19322 boff = FRAME_BASELINE_OFFSET (it->f);
19323 font_info = NULL;
19325 else
19327 font_info
19328 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19329 boff = font_info->baseline_offset;
19330 if (font_info->vertical_centering)
19331 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19334 if (font_info
19335 && (pcm = rif->per_char_metric (font, &char2b,
19336 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19338 width = pcm->width;
19339 ascent = pcm->ascent;
19340 descent = pcm->descent;
19342 else
19344 width = FONT_WIDTH (font);
19345 ascent = 1;
19346 descent = 0;
19349 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19351 /* Relative composition with or without
19352 alternate chars. */
19353 left = (leftmost + rightmost - width) / 2;
19354 btm = - descent + boff;
19355 if (font_info && font_info->relative_compose
19356 && (! CHAR_TABLE_P (Vignore_relative_composition)
19357 || NILP (Faref (Vignore_relative_composition,
19358 make_number (ch)))))
19361 if (- descent >= font_info->relative_compose)
19362 /* One extra pixel between two glyphs. */
19363 btm = highest + 1;
19364 else if (ascent <= 0)
19365 /* One extra pixel between two glyphs. */
19366 btm = lowest - 1 - ascent - descent;
19369 else
19371 /* A composition rule is specified by an integer
19372 value that encodes global and new reference
19373 points (GREF and NREF). GREF and NREF are
19374 specified by numbers as below:
19376 0---1---2 -- ascent
19380 9--10--11 -- center
19382 ---3---4---5--- baseline
19384 6---7---8 -- descent
19386 int rule = COMPOSITION_RULE (cmp, i);
19387 int gref, nref, grefx, grefy, nrefx, nrefy;
19389 COMPOSITION_DECODE_RULE (rule, gref, nref);
19390 grefx = gref % 3, nrefx = nref % 3;
19391 grefy = gref / 3, nrefy = nref / 3;
19393 left = (leftmost
19394 + grefx * (rightmost - leftmost) / 2
19395 - nrefx * width / 2);
19396 btm = ((grefy == 0 ? highest
19397 : grefy == 1 ? 0
19398 : grefy == 2 ? lowest
19399 : (highest + lowest) / 2)
19400 - (nrefy == 0 ? ascent + descent
19401 : nrefy == 1 ? descent - boff
19402 : nrefy == 2 ? 0
19403 : (ascent + descent) / 2));
19406 cmp->offsets[i * 2] = left;
19407 cmp->offsets[i * 2 + 1] = btm + descent;
19409 /* Update the bounding box of the overall glyphs. */
19410 right = left + width;
19411 top = btm + descent + ascent;
19412 if (left < leftmost)
19413 leftmost = left;
19414 if (right > rightmost)
19415 rightmost = right;
19416 if (top > highest)
19417 highest = top;
19418 if (btm < lowest)
19419 lowest = btm;
19422 /* If there are glyphs whose x-offsets are negative,
19423 shift all glyphs to the right and make all x-offsets
19424 non-negative. */
19425 if (leftmost < 0)
19427 for (i = 0; i < cmp->glyph_len; i++)
19428 cmp->offsets[i * 2] -= leftmost;
19429 rightmost -= leftmost;
19432 cmp->pixel_width = rightmost;
19433 cmp->ascent = highest;
19434 cmp->descent = - lowest;
19435 if (cmp->ascent < font_ascent)
19436 cmp->ascent = font_ascent;
19437 if (cmp->descent < font_descent)
19438 cmp->descent = font_descent;
19441 it->pixel_width = cmp->pixel_width;
19442 it->ascent = it->phys_ascent = cmp->ascent;
19443 it->descent = it->phys_descent = cmp->descent;
19445 if (face->box != FACE_NO_BOX)
19447 int thick = face->box_line_width;
19449 if (thick > 0)
19451 it->ascent += thick;
19452 it->descent += thick;
19454 else
19455 thick = - thick;
19457 if (it->start_of_box_run_p)
19458 it->pixel_width += thick;
19459 if (it->end_of_box_run_p)
19460 it->pixel_width += thick;
19463 /* If face has an overline, add the height of the overline
19464 (1 pixel) and a 1 pixel margin to the character height. */
19465 if (face->overline_p)
19466 it->ascent += 2;
19468 take_vertical_position_into_account (it);
19470 if (it->glyph_row)
19471 append_composite_glyph (it);
19473 else if (it->what == IT_IMAGE)
19474 produce_image_glyph (it);
19475 else if (it->what == IT_STRETCH)
19476 produce_stretch_glyph (it);
19478 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19479 because this isn't true for images with `:ascent 100'. */
19480 xassert (it->ascent >= 0 && it->descent >= 0);
19481 if (it->area == TEXT_AREA)
19482 it->current_x += it->pixel_width;
19484 if (extra_line_spacing > 0)
19486 it->descent += extra_line_spacing;
19487 if (extra_line_spacing > it->max_extra_line_spacing)
19488 it->max_extra_line_spacing = extra_line_spacing;
19491 it->max_ascent = max (it->max_ascent, it->ascent);
19492 it->max_descent = max (it->max_descent, it->descent);
19493 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19494 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19497 /* EXPORT for RIF:
19498 Output LEN glyphs starting at START at the nominal cursor position.
19499 Advance the nominal cursor over the text. The global variable
19500 updated_window contains the window being updated, updated_row is
19501 the glyph row being updated, and updated_area is the area of that
19502 row being updated. */
19504 void
19505 x_write_glyphs (start, len)
19506 struct glyph *start;
19507 int len;
19509 int x, hpos;
19511 xassert (updated_window && updated_row);
19512 BLOCK_INPUT;
19514 /* Write glyphs. */
19516 hpos = start - updated_row->glyphs[updated_area];
19517 x = draw_glyphs (updated_window, output_cursor.x,
19518 updated_row, updated_area,
19519 hpos, hpos + len,
19520 DRAW_NORMAL_TEXT, 0);
19522 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19523 if (updated_area == TEXT_AREA
19524 && updated_window->phys_cursor_on_p
19525 && updated_window->phys_cursor.vpos == output_cursor.vpos
19526 && updated_window->phys_cursor.hpos >= hpos
19527 && updated_window->phys_cursor.hpos < hpos + len)
19528 updated_window->phys_cursor_on_p = 0;
19530 UNBLOCK_INPUT;
19532 /* Advance the output cursor. */
19533 output_cursor.hpos += len;
19534 output_cursor.x = x;
19538 /* EXPORT for RIF:
19539 Insert LEN glyphs from START at the nominal cursor position. */
19541 void
19542 x_insert_glyphs (start, len)
19543 struct glyph *start;
19544 int len;
19546 struct frame *f;
19547 struct window *w;
19548 int line_height, shift_by_width, shifted_region_width;
19549 struct glyph_row *row;
19550 struct glyph *glyph;
19551 int frame_x, frame_y, hpos;
19553 xassert (updated_window && updated_row);
19554 BLOCK_INPUT;
19555 w = updated_window;
19556 f = XFRAME (WINDOW_FRAME (w));
19558 /* Get the height of the line we are in. */
19559 row = updated_row;
19560 line_height = row->height;
19562 /* Get the width of the glyphs to insert. */
19563 shift_by_width = 0;
19564 for (glyph = start; glyph < start + len; ++glyph)
19565 shift_by_width += glyph->pixel_width;
19567 /* Get the width of the region to shift right. */
19568 shifted_region_width = (window_box_width (w, updated_area)
19569 - output_cursor.x
19570 - shift_by_width);
19572 /* Shift right. */
19573 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19574 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19576 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19577 line_height, shift_by_width);
19579 /* Write the glyphs. */
19580 hpos = start - row->glyphs[updated_area];
19581 draw_glyphs (w, output_cursor.x, row, updated_area,
19582 hpos, hpos + len,
19583 DRAW_NORMAL_TEXT, 0);
19585 /* Advance the output cursor. */
19586 output_cursor.hpos += len;
19587 output_cursor.x += shift_by_width;
19588 UNBLOCK_INPUT;
19592 /* EXPORT for RIF:
19593 Erase the current text line from the nominal cursor position
19594 (inclusive) to pixel column TO_X (exclusive). The idea is that
19595 everything from TO_X onward is already erased.
19597 TO_X is a pixel position relative to updated_area of
19598 updated_window. TO_X == -1 means clear to the end of this area. */
19600 void
19601 x_clear_end_of_line (to_x)
19602 int to_x;
19604 struct frame *f;
19605 struct window *w = updated_window;
19606 int max_x, min_y, max_y;
19607 int from_x, from_y, to_y;
19609 xassert (updated_window && updated_row);
19610 f = XFRAME (w->frame);
19612 if (updated_row->full_width_p)
19613 max_x = WINDOW_TOTAL_WIDTH (w);
19614 else
19615 max_x = window_box_width (w, updated_area);
19616 max_y = window_text_bottom_y (w);
19618 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19619 of window. For TO_X > 0, truncate to end of drawing area. */
19620 if (to_x == 0)
19621 return;
19622 else if (to_x < 0)
19623 to_x = max_x;
19624 else
19625 to_x = min (to_x, max_x);
19627 to_y = min (max_y, output_cursor.y + updated_row->height);
19629 /* Notice if the cursor will be cleared by this operation. */
19630 if (!updated_row->full_width_p)
19631 notice_overwritten_cursor (w, updated_area,
19632 output_cursor.x, -1,
19633 updated_row->y,
19634 MATRIX_ROW_BOTTOM_Y (updated_row));
19636 from_x = output_cursor.x;
19638 /* Translate to frame coordinates. */
19639 if (updated_row->full_width_p)
19641 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19642 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19644 else
19646 int area_left = window_box_left (w, updated_area);
19647 from_x += area_left;
19648 to_x += area_left;
19651 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19652 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19653 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19655 /* Prevent inadvertently clearing to end of the X window. */
19656 if (to_x > from_x && to_y > from_y)
19658 BLOCK_INPUT;
19659 rif->clear_frame_area (f, from_x, from_y,
19660 to_x - from_x, to_y - from_y);
19661 UNBLOCK_INPUT;
19665 #endif /* HAVE_WINDOW_SYSTEM */
19669 /***********************************************************************
19670 Cursor types
19671 ***********************************************************************/
19673 /* Value is the internal representation of the specified cursor type
19674 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19675 of the bar cursor. */
19677 static enum text_cursor_kinds
19678 get_specified_cursor_type (arg, width)
19679 Lisp_Object arg;
19680 int *width;
19682 enum text_cursor_kinds type;
19684 if (NILP (arg))
19685 return NO_CURSOR;
19687 if (EQ (arg, Qbox))
19688 return FILLED_BOX_CURSOR;
19690 if (EQ (arg, Qhollow))
19691 return HOLLOW_BOX_CURSOR;
19693 if (EQ (arg, Qbar))
19695 *width = 2;
19696 return BAR_CURSOR;
19699 if (CONSP (arg)
19700 && EQ (XCAR (arg), Qbar)
19701 && INTEGERP (XCDR (arg))
19702 && XINT (XCDR (arg)) >= 0)
19704 *width = XINT (XCDR (arg));
19705 return BAR_CURSOR;
19708 if (EQ (arg, Qhbar))
19710 *width = 2;
19711 return HBAR_CURSOR;
19714 if (CONSP (arg)
19715 && EQ (XCAR (arg), Qhbar)
19716 && INTEGERP (XCDR (arg))
19717 && XINT (XCDR (arg)) >= 0)
19719 *width = XINT (XCDR (arg));
19720 return HBAR_CURSOR;
19723 /* Treat anything unknown as "hollow box cursor".
19724 It was bad to signal an error; people have trouble fixing
19725 .Xdefaults with Emacs, when it has something bad in it. */
19726 type = HOLLOW_BOX_CURSOR;
19728 return type;
19731 /* Set the default cursor types for specified frame. */
19732 void
19733 set_frame_cursor_types (f, arg)
19734 struct frame *f;
19735 Lisp_Object arg;
19737 int width;
19738 Lisp_Object tem;
19740 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19741 FRAME_CURSOR_WIDTH (f) = width;
19743 /* By default, set up the blink-off state depending on the on-state. */
19745 tem = Fassoc (arg, Vblink_cursor_alist);
19746 if (!NILP (tem))
19748 FRAME_BLINK_OFF_CURSOR (f)
19749 = get_specified_cursor_type (XCDR (tem), &width);
19750 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19752 else
19753 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19757 /* Return the cursor we want to be displayed in window W. Return
19758 width of bar/hbar cursor through WIDTH arg. Return with
19759 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19760 (i.e. if the `system caret' should track this cursor).
19762 In a mini-buffer window, we want the cursor only to appear if we
19763 are reading input from this window. For the selected window, we
19764 want the cursor type given by the frame parameter or buffer local
19765 setting of cursor-type. If explicitly marked off, draw no cursor.
19766 In all other cases, we want a hollow box cursor. */
19768 static enum text_cursor_kinds
19769 get_window_cursor_type (w, glyph, width, active_cursor)
19770 struct window *w;
19771 struct glyph *glyph;
19772 int *width;
19773 int *active_cursor;
19775 struct frame *f = XFRAME (w->frame);
19776 struct buffer *b = XBUFFER (w->buffer);
19777 int cursor_type = DEFAULT_CURSOR;
19778 Lisp_Object alt_cursor;
19779 int non_selected = 0;
19781 *active_cursor = 1;
19783 /* Echo area */
19784 if (cursor_in_echo_area
19785 && FRAME_HAS_MINIBUF_P (f)
19786 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19788 if (w == XWINDOW (echo_area_window))
19790 *width = FRAME_CURSOR_WIDTH (f);
19791 return FRAME_DESIRED_CURSOR (f);
19794 *active_cursor = 0;
19795 non_selected = 1;
19798 /* Nonselected window or nonselected frame. */
19799 else if (w != XWINDOW (f->selected_window)
19800 #ifdef HAVE_WINDOW_SYSTEM
19801 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19802 #endif
19805 *active_cursor = 0;
19807 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19808 return NO_CURSOR;
19810 non_selected = 1;
19813 /* Never display a cursor in a window in which cursor-type is nil. */
19814 if (NILP (b->cursor_type))
19815 return NO_CURSOR;
19817 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19818 if (non_selected)
19820 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19821 return get_specified_cursor_type (alt_cursor, width);
19824 /* Get the normal cursor type for this window. */
19825 if (EQ (b->cursor_type, Qt))
19827 cursor_type = FRAME_DESIRED_CURSOR (f);
19828 *width = FRAME_CURSOR_WIDTH (f);
19830 else
19831 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19833 /* Use normal cursor if not blinked off. */
19834 if (!w->cursor_off_p)
19836 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19837 if (cursor_type == FILLED_BOX_CURSOR)
19838 cursor_type = HOLLOW_BOX_CURSOR;
19840 return cursor_type;
19843 /* Cursor is blinked off, so determine how to "toggle" it. */
19845 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19846 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19847 return get_specified_cursor_type (XCDR (alt_cursor), width);
19849 /* Then see if frame has specified a specific blink off cursor type. */
19850 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19852 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19853 return FRAME_BLINK_OFF_CURSOR (f);
19856 #if 0
19857 /* Some people liked having a permanently visible blinking cursor,
19858 while others had very strong opinions against it. So it was
19859 decided to remove it. KFS 2003-09-03 */
19861 /* Finally perform built-in cursor blinking:
19862 filled box <-> hollow box
19863 wide [h]bar <-> narrow [h]bar
19864 narrow [h]bar <-> no cursor
19865 other type <-> no cursor */
19867 if (cursor_type == FILLED_BOX_CURSOR)
19868 return HOLLOW_BOX_CURSOR;
19870 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19872 *width = 1;
19873 return cursor_type;
19875 #endif
19877 return NO_CURSOR;
19881 #ifdef HAVE_WINDOW_SYSTEM
19883 /* Notice when the text cursor of window W has been completely
19884 overwritten by a drawing operation that outputs glyphs in AREA
19885 starting at X0 and ending at X1 in the line starting at Y0 and
19886 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19887 the rest of the line after X0 has been written. Y coordinates
19888 are window-relative. */
19890 static void
19891 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19892 struct window *w;
19893 enum glyph_row_area area;
19894 int x0, y0, x1, y1;
19896 int cx0, cx1, cy0, cy1;
19897 struct glyph_row *row;
19899 if (!w->phys_cursor_on_p)
19900 return;
19901 if (area != TEXT_AREA)
19902 return;
19904 row = w->current_matrix->rows + w->phys_cursor.vpos;
19905 if (!row->displays_text_p)
19906 return;
19908 if (row->cursor_in_fringe_p)
19910 row->cursor_in_fringe_p = 0;
19911 draw_fringe_bitmap (w, row, 0);
19912 w->phys_cursor_on_p = 0;
19913 return;
19916 cx0 = w->phys_cursor.x;
19917 cx1 = cx0 + w->phys_cursor_width;
19918 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19919 return;
19921 /* The cursor image will be completely removed from the
19922 screen if the output area intersects the cursor area in
19923 y-direction. When we draw in [y0 y1[, and some part of
19924 the cursor is at y < y0, that part must have been drawn
19925 before. When scrolling, the cursor is erased before
19926 actually scrolling, so we don't come here. When not
19927 scrolling, the rows above the old cursor row must have
19928 changed, and in this case these rows must have written
19929 over the cursor image.
19931 Likewise if part of the cursor is below y1, with the
19932 exception of the cursor being in the first blank row at
19933 the buffer and window end because update_text_area
19934 doesn't draw that row. (Except when it does, but
19935 that's handled in update_text_area.) */
19937 cy0 = w->phys_cursor.y;
19938 cy1 = cy0 + w->phys_cursor_height;
19939 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19940 return;
19942 w->phys_cursor_on_p = 0;
19945 #endif /* HAVE_WINDOW_SYSTEM */
19948 /************************************************************************
19949 Mouse Face
19950 ************************************************************************/
19952 #ifdef HAVE_WINDOW_SYSTEM
19954 /* EXPORT for RIF:
19955 Fix the display of area AREA of overlapping row ROW in window W. */
19957 void
19958 x_fix_overlapping_area (w, row, area)
19959 struct window *w;
19960 struct glyph_row *row;
19961 enum glyph_row_area area;
19963 int i, x;
19965 BLOCK_INPUT;
19967 x = 0;
19968 for (i = 0; i < row->used[area];)
19970 if (row->glyphs[area][i].overlaps_vertically_p)
19972 int start = i, start_x = x;
19976 x += row->glyphs[area][i].pixel_width;
19977 ++i;
19979 while (i < row->used[area]
19980 && row->glyphs[area][i].overlaps_vertically_p);
19982 draw_glyphs (w, start_x, row, area,
19983 start, i,
19984 DRAW_NORMAL_TEXT, 1);
19986 else
19988 x += row->glyphs[area][i].pixel_width;
19989 ++i;
19993 UNBLOCK_INPUT;
19997 /* EXPORT:
19998 Draw the cursor glyph of window W in glyph row ROW. See the
19999 comment of draw_glyphs for the meaning of HL. */
20001 void
20002 draw_phys_cursor_glyph (w, row, hl)
20003 struct window *w;
20004 struct glyph_row *row;
20005 enum draw_glyphs_face hl;
20007 /* If cursor hpos is out of bounds, don't draw garbage. This can
20008 happen in mini-buffer windows when switching between echo area
20009 glyphs and mini-buffer. */
20010 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20012 int on_p = w->phys_cursor_on_p;
20013 int x1;
20014 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20015 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20016 hl, 0);
20017 w->phys_cursor_on_p = on_p;
20019 if (hl == DRAW_CURSOR)
20020 w->phys_cursor_width = x1 - w->phys_cursor.x;
20021 /* When we erase the cursor, and ROW is overlapped by other
20022 rows, make sure that these overlapping parts of other rows
20023 are redrawn. */
20024 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20026 if (row > w->current_matrix->rows
20027 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20028 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20030 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20031 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20032 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20038 /* EXPORT:
20039 Erase the image of a cursor of window W from the screen. */
20041 void
20042 erase_phys_cursor (w)
20043 struct window *w;
20045 struct frame *f = XFRAME (w->frame);
20046 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20047 int hpos = w->phys_cursor.hpos;
20048 int vpos = w->phys_cursor.vpos;
20049 int mouse_face_here_p = 0;
20050 struct glyph_matrix *active_glyphs = w->current_matrix;
20051 struct glyph_row *cursor_row;
20052 struct glyph *cursor_glyph;
20053 enum draw_glyphs_face hl;
20055 /* No cursor displayed or row invalidated => nothing to do on the
20056 screen. */
20057 if (w->phys_cursor_type == NO_CURSOR)
20058 goto mark_cursor_off;
20060 /* VPOS >= active_glyphs->nrows means that window has been resized.
20061 Don't bother to erase the cursor. */
20062 if (vpos >= active_glyphs->nrows)
20063 goto mark_cursor_off;
20065 /* If row containing cursor is marked invalid, there is nothing we
20066 can do. */
20067 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20068 if (!cursor_row->enabled_p)
20069 goto mark_cursor_off;
20071 /* If row is completely invisible, don't attempt to delete a cursor which
20072 isn't there. This can happen if cursor is at top of a window, and
20073 we switch to a buffer with a header line in that window. */
20074 if (cursor_row->visible_height <= 0)
20075 goto mark_cursor_off;
20077 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20078 if (cursor_row->cursor_in_fringe_p)
20080 cursor_row->cursor_in_fringe_p = 0;
20081 draw_fringe_bitmap (w, cursor_row, 0);
20082 goto mark_cursor_off;
20085 /* This can happen when the new row is shorter than the old one.
20086 In this case, either draw_glyphs or clear_end_of_line
20087 should have cleared the cursor. Note that we wouldn't be
20088 able to erase the cursor in this case because we don't have a
20089 cursor glyph at hand. */
20090 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20091 goto mark_cursor_off;
20093 /* If the cursor is in the mouse face area, redisplay that when
20094 we clear the cursor. */
20095 if (! NILP (dpyinfo->mouse_face_window)
20096 && w == XWINDOW (dpyinfo->mouse_face_window)
20097 && (vpos > dpyinfo->mouse_face_beg_row
20098 || (vpos == dpyinfo->mouse_face_beg_row
20099 && hpos >= dpyinfo->mouse_face_beg_col))
20100 && (vpos < dpyinfo->mouse_face_end_row
20101 || (vpos == dpyinfo->mouse_face_end_row
20102 && hpos < dpyinfo->mouse_face_end_col))
20103 /* Don't redraw the cursor's spot in mouse face if it is at the
20104 end of a line (on a newline). The cursor appears there, but
20105 mouse highlighting does not. */
20106 && cursor_row->used[TEXT_AREA] > hpos)
20107 mouse_face_here_p = 1;
20109 /* Maybe clear the display under the cursor. */
20110 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20112 int x, y;
20113 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20115 cursor_glyph = get_phys_cursor_glyph (w);
20116 if (cursor_glyph == NULL)
20117 goto mark_cursor_off;
20119 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20120 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20122 rif->clear_frame_area (f, x, y,
20123 cursor_glyph->pixel_width, cursor_row->visible_height);
20126 /* Erase the cursor by redrawing the character underneath it. */
20127 if (mouse_face_here_p)
20128 hl = DRAW_MOUSE_FACE;
20129 else
20130 hl = DRAW_NORMAL_TEXT;
20131 draw_phys_cursor_glyph (w, cursor_row, hl);
20133 mark_cursor_off:
20134 w->phys_cursor_on_p = 0;
20135 w->phys_cursor_type = NO_CURSOR;
20139 /* EXPORT:
20140 Display or clear cursor of window W. If ON is zero, clear the
20141 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20142 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20144 void
20145 display_and_set_cursor (w, on, hpos, vpos, x, y)
20146 struct window *w;
20147 int on, hpos, vpos, x, y;
20149 struct frame *f = XFRAME (w->frame);
20150 int new_cursor_type;
20151 int new_cursor_width;
20152 int active_cursor;
20153 struct glyph_row *glyph_row;
20154 struct glyph *glyph;
20156 /* This is pointless on invisible frames, and dangerous on garbaged
20157 windows and frames; in the latter case, the frame or window may
20158 be in the midst of changing its size, and x and y may be off the
20159 window. */
20160 if (! FRAME_VISIBLE_P (f)
20161 || FRAME_GARBAGED_P (f)
20162 || vpos >= w->current_matrix->nrows
20163 || hpos >= w->current_matrix->matrix_w)
20164 return;
20166 /* If cursor is off and we want it off, return quickly. */
20167 if (!on && !w->phys_cursor_on_p)
20168 return;
20170 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20171 /* If cursor row is not enabled, we don't really know where to
20172 display the cursor. */
20173 if (!glyph_row->enabled_p)
20175 w->phys_cursor_on_p = 0;
20176 return;
20179 glyph = NULL;
20180 if (!glyph_row->exact_window_width_line_p
20181 || hpos < glyph_row->used[TEXT_AREA])
20182 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20184 xassert (interrupt_input_blocked);
20186 /* Set new_cursor_type to the cursor we want to be displayed. */
20187 new_cursor_type = get_window_cursor_type (w, glyph,
20188 &new_cursor_width, &active_cursor);
20190 /* If cursor is currently being shown and we don't want it to be or
20191 it is in the wrong place, or the cursor type is not what we want,
20192 erase it. */
20193 if (w->phys_cursor_on_p
20194 && (!on
20195 || w->phys_cursor.x != x
20196 || w->phys_cursor.y != y
20197 || new_cursor_type != w->phys_cursor_type
20198 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20199 && new_cursor_width != w->phys_cursor_width)))
20200 erase_phys_cursor (w);
20202 /* Don't check phys_cursor_on_p here because that flag is only set
20203 to zero in some cases where we know that the cursor has been
20204 completely erased, to avoid the extra work of erasing the cursor
20205 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20206 still not be visible, or it has only been partly erased. */
20207 if (on)
20209 w->phys_cursor_ascent = glyph_row->ascent;
20210 w->phys_cursor_height = glyph_row->height;
20212 /* Set phys_cursor_.* before x_draw_.* is called because some
20213 of them may need the information. */
20214 w->phys_cursor.x = x;
20215 w->phys_cursor.y = glyph_row->y;
20216 w->phys_cursor.hpos = hpos;
20217 w->phys_cursor.vpos = vpos;
20220 rif->draw_window_cursor (w, glyph_row, x, y,
20221 new_cursor_type, new_cursor_width,
20222 on, active_cursor);
20226 /* Switch the display of W's cursor on or off, according to the value
20227 of ON. */
20229 static void
20230 update_window_cursor (w, on)
20231 struct window *w;
20232 int on;
20234 /* Don't update cursor in windows whose frame is in the process
20235 of being deleted. */
20236 if (w->current_matrix)
20238 BLOCK_INPUT;
20239 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20240 w->phys_cursor.x, w->phys_cursor.y);
20241 UNBLOCK_INPUT;
20246 /* Call update_window_cursor with parameter ON_P on all leaf windows
20247 in the window tree rooted at W. */
20249 static void
20250 update_cursor_in_window_tree (w, on_p)
20251 struct window *w;
20252 int on_p;
20254 while (w)
20256 if (!NILP (w->hchild))
20257 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20258 else if (!NILP (w->vchild))
20259 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20260 else
20261 update_window_cursor (w, on_p);
20263 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20268 /* EXPORT:
20269 Display the cursor on window W, or clear it, according to ON_P.
20270 Don't change the cursor's position. */
20272 void
20273 x_update_cursor (f, on_p)
20274 struct frame *f;
20275 int on_p;
20277 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20281 /* EXPORT:
20282 Clear the cursor of window W to background color, and mark the
20283 cursor as not shown. This is used when the text where the cursor
20284 is is about to be rewritten. */
20286 void
20287 x_clear_cursor (w)
20288 struct window *w;
20290 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20291 update_window_cursor (w, 0);
20295 /* EXPORT:
20296 Display the active region described by mouse_face_* according to DRAW. */
20298 void
20299 show_mouse_face (dpyinfo, draw)
20300 Display_Info *dpyinfo;
20301 enum draw_glyphs_face draw;
20303 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20304 struct frame *f = XFRAME (WINDOW_FRAME (w));
20306 if (/* If window is in the process of being destroyed, don't bother
20307 to do anything. */
20308 w->current_matrix != NULL
20309 /* Don't update mouse highlight if hidden */
20310 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20311 /* Recognize when we are called to operate on rows that don't exist
20312 anymore. This can happen when a window is split. */
20313 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20315 int phys_cursor_on_p = w->phys_cursor_on_p;
20316 struct glyph_row *row, *first, *last;
20318 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20319 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20321 for (row = first; row <= last && row->enabled_p; ++row)
20323 int start_hpos, end_hpos, start_x;
20325 /* For all but the first row, the highlight starts at column 0. */
20326 if (row == first)
20328 start_hpos = dpyinfo->mouse_face_beg_col;
20329 start_x = dpyinfo->mouse_face_beg_x;
20331 else
20333 start_hpos = 0;
20334 start_x = 0;
20337 if (row == last)
20338 end_hpos = dpyinfo->mouse_face_end_col;
20339 else
20340 end_hpos = row->used[TEXT_AREA];
20342 if (end_hpos > start_hpos)
20344 draw_glyphs (w, start_x, row, TEXT_AREA,
20345 start_hpos, end_hpos,
20346 draw, 0);
20348 row->mouse_face_p
20349 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20353 /* When we've written over the cursor, arrange for it to
20354 be displayed again. */
20355 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20357 BLOCK_INPUT;
20358 display_and_set_cursor (w, 1,
20359 w->phys_cursor.hpos, w->phys_cursor.vpos,
20360 w->phys_cursor.x, w->phys_cursor.y);
20361 UNBLOCK_INPUT;
20365 /* Change the mouse cursor. */
20366 if (draw == DRAW_NORMAL_TEXT)
20367 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20368 else if (draw == DRAW_MOUSE_FACE)
20369 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20370 else
20371 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20374 /* EXPORT:
20375 Clear out the mouse-highlighted active region.
20376 Redraw it un-highlighted first. Value is non-zero if mouse
20377 face was actually drawn unhighlighted. */
20380 clear_mouse_face (dpyinfo)
20381 Display_Info *dpyinfo;
20383 int cleared = 0;
20385 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20387 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20388 cleared = 1;
20391 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20392 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20393 dpyinfo->mouse_face_window = Qnil;
20394 dpyinfo->mouse_face_overlay = Qnil;
20395 return cleared;
20399 /* EXPORT:
20400 Non-zero if physical cursor of window W is within mouse face. */
20403 cursor_in_mouse_face_p (w)
20404 struct window *w;
20406 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20407 int in_mouse_face = 0;
20409 if (WINDOWP (dpyinfo->mouse_face_window)
20410 && XWINDOW (dpyinfo->mouse_face_window) == w)
20412 int hpos = w->phys_cursor.hpos;
20413 int vpos = w->phys_cursor.vpos;
20415 if (vpos >= dpyinfo->mouse_face_beg_row
20416 && vpos <= dpyinfo->mouse_face_end_row
20417 && (vpos > dpyinfo->mouse_face_beg_row
20418 || hpos >= dpyinfo->mouse_face_beg_col)
20419 && (vpos < dpyinfo->mouse_face_end_row
20420 || hpos < dpyinfo->mouse_face_end_col
20421 || dpyinfo->mouse_face_past_end))
20422 in_mouse_face = 1;
20425 return in_mouse_face;
20431 /* Find the glyph matrix position of buffer position CHARPOS in window
20432 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20433 current glyphs must be up to date. If CHARPOS is above window
20434 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20435 of last line in W. In the row containing CHARPOS, stop before glyphs
20436 having STOP as object. */
20438 #if 1 /* This is a version of fast_find_position that's more correct
20439 in the presence of hscrolling, for example. I didn't install
20440 it right away because the problem fixed is minor, it failed
20441 in 20.x as well, and I think it's too risky to install
20442 so near the release of 21.1. 2001-09-25 gerd. */
20444 static int
20445 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20446 struct window *w;
20447 int charpos;
20448 int *hpos, *vpos, *x, *y;
20449 Lisp_Object stop;
20451 struct glyph_row *row, *first;
20452 struct glyph *glyph, *end;
20453 int past_end = 0;
20455 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20456 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20458 *x = first->x;
20459 *y = first->y;
20460 *hpos = 0;
20461 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20462 return 1;
20465 row = row_containing_pos (w, charpos, first, NULL, 0);
20466 if (row == NULL)
20468 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20469 past_end = 1;
20472 *x = row->x;
20473 *y = row->y;
20474 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20476 glyph = row->glyphs[TEXT_AREA];
20477 end = glyph + row->used[TEXT_AREA];
20479 /* Skip over glyphs not having an object at the start of the row.
20480 These are special glyphs like truncation marks on terminal
20481 frames. */
20482 if (row->displays_text_p)
20483 while (glyph < end
20484 && INTEGERP (glyph->object)
20485 && !EQ (stop, glyph->object)
20486 && glyph->charpos < 0)
20488 *x += glyph->pixel_width;
20489 ++glyph;
20492 while (glyph < end
20493 && !INTEGERP (glyph->object)
20494 && !EQ (stop, glyph->object)
20495 && (!BUFFERP (glyph->object)
20496 || glyph->charpos < charpos))
20498 *x += glyph->pixel_width;
20499 ++glyph;
20502 *hpos = glyph - row->glyphs[TEXT_AREA];
20503 return !past_end;
20506 #else /* not 1 */
20508 static int
20509 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20510 struct window *w;
20511 int pos;
20512 int *hpos, *vpos, *x, *y;
20513 Lisp_Object stop;
20515 int i;
20516 int lastcol;
20517 int maybe_next_line_p = 0;
20518 int line_start_position;
20519 int yb = window_text_bottom_y (w);
20520 struct glyph_row *row, *best_row;
20521 int row_vpos, best_row_vpos;
20522 int current_x;
20524 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20525 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20527 while (row->y < yb)
20529 if (row->used[TEXT_AREA])
20530 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20531 else
20532 line_start_position = 0;
20534 if (line_start_position > pos)
20535 break;
20536 /* If the position sought is the end of the buffer,
20537 don't include the blank lines at the bottom of the window. */
20538 else if (line_start_position == pos
20539 && pos == BUF_ZV (XBUFFER (w->buffer)))
20541 maybe_next_line_p = 1;
20542 break;
20544 else if (line_start_position > 0)
20546 best_row = row;
20547 best_row_vpos = row_vpos;
20550 if (row->y + row->height >= yb)
20551 break;
20553 ++row;
20554 ++row_vpos;
20557 /* Find the right column within BEST_ROW. */
20558 lastcol = 0;
20559 current_x = best_row->x;
20560 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20562 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20563 int charpos = glyph->charpos;
20565 if (BUFFERP (glyph->object))
20567 if (charpos == pos)
20569 *hpos = i;
20570 *vpos = best_row_vpos;
20571 *x = current_x;
20572 *y = best_row->y;
20573 return 1;
20575 else if (charpos > pos)
20576 break;
20578 else if (EQ (glyph->object, stop))
20579 break;
20581 if (charpos > 0)
20582 lastcol = i;
20583 current_x += glyph->pixel_width;
20586 /* If we're looking for the end of the buffer,
20587 and we didn't find it in the line we scanned,
20588 use the start of the following line. */
20589 if (maybe_next_line_p)
20591 ++best_row;
20592 ++best_row_vpos;
20593 lastcol = 0;
20594 current_x = best_row->x;
20597 *vpos = best_row_vpos;
20598 *hpos = lastcol + 1;
20599 *x = current_x;
20600 *y = best_row->y;
20601 return 0;
20604 #endif /* not 1 */
20607 /* Find the position of the glyph for position POS in OBJECT in
20608 window W's current matrix, and return in *X, *Y the pixel
20609 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20611 RIGHT_P non-zero means return the position of the right edge of the
20612 glyph, RIGHT_P zero means return the left edge position.
20614 If no glyph for POS exists in the matrix, return the position of
20615 the glyph with the next smaller position that is in the matrix, if
20616 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20617 exists in the matrix, return the position of the glyph with the
20618 next larger position in OBJECT.
20620 Value is non-zero if a glyph was found. */
20622 static int
20623 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20624 struct window *w;
20625 int pos;
20626 Lisp_Object object;
20627 int *hpos, *vpos, *x, *y;
20628 int right_p;
20630 int yb = window_text_bottom_y (w);
20631 struct glyph_row *r;
20632 struct glyph *best_glyph = NULL;
20633 struct glyph_row *best_row = NULL;
20634 int best_x = 0;
20636 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20637 r->enabled_p && r->y < yb;
20638 ++r)
20640 struct glyph *g = r->glyphs[TEXT_AREA];
20641 struct glyph *e = g + r->used[TEXT_AREA];
20642 int gx;
20644 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20645 if (EQ (g->object, object))
20647 if (g->charpos == pos)
20649 best_glyph = g;
20650 best_x = gx;
20651 best_row = r;
20652 goto found;
20654 else if (best_glyph == NULL
20655 || ((abs (g->charpos - pos)
20656 < abs (best_glyph->charpos - pos))
20657 && (right_p
20658 ? g->charpos < pos
20659 : g->charpos > pos)))
20661 best_glyph = g;
20662 best_x = gx;
20663 best_row = r;
20668 found:
20670 if (best_glyph)
20672 *x = best_x;
20673 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20675 if (right_p)
20677 *x += best_glyph->pixel_width;
20678 ++*hpos;
20681 *y = best_row->y;
20682 *vpos = best_row - w->current_matrix->rows;
20685 return best_glyph != NULL;
20689 /* See if position X, Y is within a hot-spot of an image. */
20691 static int
20692 on_hot_spot_p (hot_spot, x, y)
20693 Lisp_Object hot_spot;
20694 int x, y;
20696 if (!CONSP (hot_spot))
20697 return 0;
20699 if (EQ (XCAR (hot_spot), Qrect))
20701 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20702 Lisp_Object rect = XCDR (hot_spot);
20703 Lisp_Object tem;
20704 if (!CONSP (rect))
20705 return 0;
20706 if (!CONSP (XCAR (rect)))
20707 return 0;
20708 if (!CONSP (XCDR (rect)))
20709 return 0;
20710 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20711 return 0;
20712 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20713 return 0;
20714 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20715 return 0;
20716 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20717 return 0;
20718 return 1;
20720 else if (EQ (XCAR (hot_spot), Qcircle))
20722 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20723 Lisp_Object circ = XCDR (hot_spot);
20724 Lisp_Object lr, lx0, ly0;
20725 if (CONSP (circ)
20726 && CONSP (XCAR (circ))
20727 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20728 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20729 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20731 double r = XFLOATINT (lr);
20732 double dx = XINT (lx0) - x;
20733 double dy = XINT (ly0) - y;
20734 return (dx * dx + dy * dy <= r * r);
20737 else if (EQ (XCAR (hot_spot), Qpoly))
20739 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20740 if (VECTORP (XCDR (hot_spot)))
20742 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20743 Lisp_Object *poly = v->contents;
20744 int n = v->size;
20745 int i;
20746 int inside = 0;
20747 Lisp_Object lx, ly;
20748 int x0, y0;
20750 /* Need an even number of coordinates, and at least 3 edges. */
20751 if (n < 6 || n & 1)
20752 return 0;
20754 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20755 If count is odd, we are inside polygon. Pixels on edges
20756 may or may not be included depending on actual geometry of the
20757 polygon. */
20758 if ((lx = poly[n-2], !INTEGERP (lx))
20759 || (ly = poly[n-1], !INTEGERP (lx)))
20760 return 0;
20761 x0 = XINT (lx), y0 = XINT (ly);
20762 for (i = 0; i < n; i += 2)
20764 int x1 = x0, y1 = y0;
20765 if ((lx = poly[i], !INTEGERP (lx))
20766 || (ly = poly[i+1], !INTEGERP (ly)))
20767 return 0;
20768 x0 = XINT (lx), y0 = XINT (ly);
20770 /* Does this segment cross the X line? */
20771 if (x0 >= x)
20773 if (x1 >= x)
20774 continue;
20776 else if (x1 < x)
20777 continue;
20778 if (y > y0 && y > y1)
20779 continue;
20780 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20781 inside = !inside;
20783 return inside;
20786 /* If we don't understand the format, pretend we're not in the hot-spot. */
20787 return 0;
20790 Lisp_Object
20791 find_hot_spot (map, x, y)
20792 Lisp_Object map;
20793 int x, y;
20795 while (CONSP (map))
20797 if (CONSP (XCAR (map))
20798 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20799 return XCAR (map);
20800 map = XCDR (map);
20803 return Qnil;
20806 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20807 3, 3, 0,
20808 doc: /* Lookup in image map MAP coordinates X and Y.
20809 An image map is an alist where each element has the format (AREA ID PLIST).
20810 An AREA is specified as either a rectangle, a circle, or a polygon:
20811 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20812 pixel coordinates of the upper left and bottom right corners.
20813 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20814 and the radius of the circle; r may be a float or integer.
20815 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20816 vector describes one corner in the polygon.
20817 Returns the alist element for the first matching AREA in MAP. */)
20818 (map, x, y)
20819 Lisp_Object map;
20820 Lisp_Object x, y;
20822 if (NILP (map))
20823 return Qnil;
20825 CHECK_NUMBER (x);
20826 CHECK_NUMBER (y);
20828 return find_hot_spot (map, XINT (x), XINT (y));
20832 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20833 static void
20834 define_frame_cursor1 (f, cursor, pointer)
20835 struct frame *f;
20836 Cursor cursor;
20837 Lisp_Object pointer;
20839 /* Do not change cursor shape while dragging mouse. */
20840 if (!NILP (do_mouse_tracking))
20841 return;
20843 if (!NILP (pointer))
20845 if (EQ (pointer, Qarrow))
20846 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20847 else if (EQ (pointer, Qhand))
20848 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20849 else if (EQ (pointer, Qtext))
20850 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20851 else if (EQ (pointer, intern ("hdrag")))
20852 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20853 #ifdef HAVE_X_WINDOWS
20854 else if (EQ (pointer, intern ("vdrag")))
20855 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20856 #endif
20857 else if (EQ (pointer, intern ("hourglass")))
20858 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20859 else if (EQ (pointer, Qmodeline))
20860 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20861 else
20862 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20865 if (cursor != No_Cursor)
20866 rif->define_frame_cursor (f, cursor);
20869 /* Take proper action when mouse has moved to the mode or header line
20870 or marginal area AREA of window W, x-position X and y-position Y.
20871 X is relative to the start of the text display area of W, so the
20872 width of bitmap areas and scroll bars must be subtracted to get a
20873 position relative to the start of the mode line. */
20875 static void
20876 note_mode_line_or_margin_highlight (w, x, y, area)
20877 struct window *w;
20878 int x, y;
20879 enum window_part area;
20881 struct frame *f = XFRAME (w->frame);
20882 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20883 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20884 Lisp_Object pointer = Qnil;
20885 int charpos, dx, dy, width, height;
20886 Lisp_Object string, object = Qnil;
20887 Lisp_Object pos, help;
20889 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20890 string = mode_line_string (w, area, &x, &y, &charpos,
20891 &object, &dx, &dy, &width, &height);
20892 else
20894 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20895 string = marginal_area_string (w, area, &x, &y, &charpos,
20896 &object, &dx, &dy, &width, &height);
20899 help = Qnil;
20901 if (IMAGEP (object))
20903 Lisp_Object image_map, hotspot;
20904 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
20905 !NILP (image_map))
20906 && (hotspot = find_hot_spot (image_map, dx, dy),
20907 CONSP (hotspot))
20908 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20910 Lisp_Object area_id, plist;
20912 area_id = XCAR (hotspot);
20913 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20914 If so, we could look for mouse-enter, mouse-leave
20915 properties in PLIST (and do something...). */
20916 hotspot = XCDR (hotspot);
20917 if (CONSP (hotspot)
20918 && (plist = XCAR (hotspot), CONSP (plist)))
20920 pointer = Fsafe_plist_get (plist, Qpointer);
20921 if (NILP (pointer))
20922 pointer = Qhand;
20923 help = Fsafe_plist_get (plist, Qhelp_echo);
20924 if (!NILP (help))
20926 help_echo_string = help;
20927 /* Is this correct? ++kfs */
20928 XSETWINDOW (help_echo_window, w);
20929 help_echo_object = w->buffer;
20930 help_echo_pos = charpos;
20933 if (NILP (pointer))
20934 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
20938 if (STRINGP (string))
20940 pos = make_number (charpos);
20941 /* If we're on a string with `help-echo' text property, arrange
20942 for the help to be displayed. This is done by setting the
20943 global variable help_echo_string to the help string. */
20944 help = Fget_text_property (pos, Qhelp_echo, string);
20945 if (!NILP (help))
20947 help_echo_string = help;
20948 XSETWINDOW (help_echo_window, w);
20949 help_echo_object = string;
20950 help_echo_pos = charpos;
20953 if (NILP (pointer))
20954 pointer = Fget_text_property (pos, Qpointer, string);
20956 /* Change the mouse pointer according to what is under X/Y. */
20957 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20959 Lisp_Object map;
20960 map = Fget_text_property (pos, Qlocal_map, string);
20961 if (!KEYMAPP (map))
20962 map = Fget_text_property (pos, Qkeymap, string);
20963 if (!KEYMAPP (map))
20964 cursor = dpyinfo->vertical_scroll_bar_cursor;
20968 define_frame_cursor1 (f, cursor, pointer);
20972 /* EXPORT:
20973 Take proper action when the mouse has moved to position X, Y on
20974 frame F as regards highlighting characters that have mouse-face
20975 properties. Also de-highlighting chars where the mouse was before.
20976 X and Y can be negative or out of range. */
20978 void
20979 note_mouse_highlight (f, x, y)
20980 struct frame *f;
20981 int x, y;
20983 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20984 enum window_part part;
20985 Lisp_Object window;
20986 struct window *w;
20987 Cursor cursor = No_Cursor;
20988 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20989 struct buffer *b;
20991 /* When a menu is active, don't highlight because this looks odd. */
20992 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20993 if (popup_activated ())
20994 return;
20995 #endif
20997 if (NILP (Vmouse_highlight)
20998 || !f->glyphs_initialized_p)
20999 return;
21001 dpyinfo->mouse_face_mouse_x = x;
21002 dpyinfo->mouse_face_mouse_y = y;
21003 dpyinfo->mouse_face_mouse_frame = f;
21005 if (dpyinfo->mouse_face_defer)
21006 return;
21008 if (gc_in_progress)
21010 dpyinfo->mouse_face_deferred_gc = 1;
21011 return;
21014 /* Which window is that in? */
21015 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21017 /* If we were displaying active text in another window, clear that.
21018 Also clear if we move out of text area in same window. */
21019 if (! EQ (window, dpyinfo->mouse_face_window)
21020 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21021 clear_mouse_face (dpyinfo);
21023 /* Not on a window -> return. */
21024 if (!WINDOWP (window))
21025 return;
21027 /* Reset help_echo_string. It will get recomputed below. */
21028 help_echo_string = Qnil;
21030 /* Convert to window-relative pixel coordinates. */
21031 w = XWINDOW (window);
21032 frame_to_window_pixel_xy (w, &x, &y);
21034 /* Handle tool-bar window differently since it doesn't display a
21035 buffer. */
21036 if (EQ (window, f->tool_bar_window))
21038 note_tool_bar_highlight (f, x, y);
21039 return;
21042 /* Mouse is on the mode, header line or margin? */
21043 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21044 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21046 note_mode_line_or_margin_highlight (w, x, y, part);
21047 return;
21050 if (part == ON_VERTICAL_BORDER)
21051 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21052 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21053 || part == ON_SCROLL_BAR)
21054 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21055 else
21056 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21058 /* Are we in a window whose display is up to date?
21059 And verify the buffer's text has not changed. */
21060 b = XBUFFER (w->buffer);
21061 if (part == ON_TEXT
21062 && EQ (w->window_end_valid, w->buffer)
21063 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21064 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21066 int hpos, vpos, pos, i, dx, dy, area;
21067 struct glyph *glyph;
21068 Lisp_Object object;
21069 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21070 Lisp_Object *overlay_vec = NULL;
21071 int noverlays;
21072 struct buffer *obuf;
21073 int obegv, ozv, same_region;
21075 /* Find the glyph under X/Y. */
21076 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21078 /* Look for :pointer property on image. */
21079 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21081 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21082 if (img != NULL && IMAGEP (img->spec))
21084 Lisp_Object image_map, hotspot;
21085 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21086 !NILP (image_map))
21087 && (hotspot = find_hot_spot (image_map,
21088 glyph->slice.x + dx,
21089 glyph->slice.y + dy),
21090 CONSP (hotspot))
21091 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21093 Lisp_Object area_id, plist;
21095 area_id = XCAR (hotspot);
21096 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21097 If so, we could look for mouse-enter, mouse-leave
21098 properties in PLIST (and do something...). */
21099 hotspot = XCDR (hotspot);
21100 if (CONSP (hotspot)
21101 && (plist = XCAR (hotspot), CONSP (plist)))
21103 pointer = Fsafe_plist_get (plist, Qpointer);
21104 if (NILP (pointer))
21105 pointer = Qhand;
21106 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21107 if (!NILP (help_echo_string))
21109 help_echo_window = window;
21110 help_echo_object = glyph->object;
21111 help_echo_pos = glyph->charpos;
21115 if (NILP (pointer))
21116 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21120 /* Clear mouse face if X/Y not over text. */
21121 if (glyph == NULL
21122 || area != TEXT_AREA
21123 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21125 if (clear_mouse_face (dpyinfo))
21126 cursor = No_Cursor;
21127 if (NILP (pointer))
21129 if (area != TEXT_AREA)
21130 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21131 else
21132 pointer = Vvoid_text_area_pointer;
21134 goto set_cursor;
21137 pos = glyph->charpos;
21138 object = glyph->object;
21139 if (!STRINGP (object) && !BUFFERP (object))
21140 goto set_cursor;
21142 /* If we get an out-of-range value, return now; avoid an error. */
21143 if (BUFFERP (object) && pos > BUF_Z (b))
21144 goto set_cursor;
21146 /* Make the window's buffer temporarily current for
21147 overlays_at and compute_char_face. */
21148 obuf = current_buffer;
21149 current_buffer = b;
21150 obegv = BEGV;
21151 ozv = ZV;
21152 BEGV = BEG;
21153 ZV = Z;
21155 /* Is this char mouse-active or does it have help-echo? */
21156 position = make_number (pos);
21158 if (BUFFERP (object))
21160 /* Put all the overlays we want in a vector in overlay_vec. */
21161 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21162 /* Sort overlays into increasing priority order. */
21163 noverlays = sort_overlays (overlay_vec, noverlays, w);
21165 else
21166 noverlays = 0;
21168 same_region = (EQ (window, dpyinfo->mouse_face_window)
21169 && vpos >= dpyinfo->mouse_face_beg_row
21170 && vpos <= dpyinfo->mouse_face_end_row
21171 && (vpos > dpyinfo->mouse_face_beg_row
21172 || hpos >= dpyinfo->mouse_face_beg_col)
21173 && (vpos < dpyinfo->mouse_face_end_row
21174 || hpos < dpyinfo->mouse_face_end_col
21175 || dpyinfo->mouse_face_past_end));
21177 if (same_region)
21178 cursor = No_Cursor;
21180 /* Check mouse-face highlighting. */
21181 if (! same_region
21182 /* If there exists an overlay with mouse-face overlapping
21183 the one we are currently highlighting, we have to
21184 check if we enter the overlapping overlay, and then
21185 highlight only that. */
21186 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21187 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21189 /* Find the highest priority overlay that has a mouse-face
21190 property. */
21191 overlay = Qnil;
21192 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21194 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21195 if (!NILP (mouse_face))
21196 overlay = overlay_vec[i];
21199 /* If we're actually highlighting the same overlay as
21200 before, there's no need to do that again. */
21201 if (!NILP (overlay)
21202 && EQ (overlay, dpyinfo->mouse_face_overlay))
21203 goto check_help_echo;
21205 dpyinfo->mouse_face_overlay = overlay;
21207 /* Clear the display of the old active region, if any. */
21208 if (clear_mouse_face (dpyinfo))
21209 cursor = No_Cursor;
21211 /* If no overlay applies, get a text property. */
21212 if (NILP (overlay))
21213 mouse_face = Fget_text_property (position, Qmouse_face, object);
21215 /* Handle the overlay case. */
21216 if (!NILP (overlay))
21218 /* Find the range of text around this char that
21219 should be active. */
21220 Lisp_Object before, after;
21221 int ignore;
21223 before = Foverlay_start (overlay);
21224 after = Foverlay_end (overlay);
21225 /* Record this as the current active region. */
21226 fast_find_position (w, XFASTINT (before),
21227 &dpyinfo->mouse_face_beg_col,
21228 &dpyinfo->mouse_face_beg_row,
21229 &dpyinfo->mouse_face_beg_x,
21230 &dpyinfo->mouse_face_beg_y, Qnil);
21232 dpyinfo->mouse_face_past_end
21233 = !fast_find_position (w, XFASTINT (after),
21234 &dpyinfo->mouse_face_end_col,
21235 &dpyinfo->mouse_face_end_row,
21236 &dpyinfo->mouse_face_end_x,
21237 &dpyinfo->mouse_face_end_y, Qnil);
21238 dpyinfo->mouse_face_window = window;
21240 dpyinfo->mouse_face_face_id
21241 = face_at_buffer_position (w, pos, 0, 0,
21242 &ignore, pos + 1,
21243 !dpyinfo->mouse_face_hidden);
21245 /* Display it as active. */
21246 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21247 cursor = No_Cursor;
21249 /* Handle the text property case. */
21250 else if (!NILP (mouse_face) && BUFFERP (object))
21252 /* Find the range of text around this char that
21253 should be active. */
21254 Lisp_Object before, after, beginning, end;
21255 int ignore;
21257 beginning = Fmarker_position (w->start);
21258 end = make_number (BUF_Z (XBUFFER (object))
21259 - XFASTINT (w->window_end_pos));
21260 before
21261 = Fprevious_single_property_change (make_number (pos + 1),
21262 Qmouse_face,
21263 object, beginning);
21264 after
21265 = Fnext_single_property_change (position, Qmouse_face,
21266 object, end);
21268 /* Record this as the current active region. */
21269 fast_find_position (w, XFASTINT (before),
21270 &dpyinfo->mouse_face_beg_col,
21271 &dpyinfo->mouse_face_beg_row,
21272 &dpyinfo->mouse_face_beg_x,
21273 &dpyinfo->mouse_face_beg_y, Qnil);
21274 dpyinfo->mouse_face_past_end
21275 = !fast_find_position (w, XFASTINT (after),
21276 &dpyinfo->mouse_face_end_col,
21277 &dpyinfo->mouse_face_end_row,
21278 &dpyinfo->mouse_face_end_x,
21279 &dpyinfo->mouse_face_end_y, Qnil);
21280 dpyinfo->mouse_face_window = window;
21282 if (BUFFERP (object))
21283 dpyinfo->mouse_face_face_id
21284 = face_at_buffer_position (w, pos, 0, 0,
21285 &ignore, pos + 1,
21286 !dpyinfo->mouse_face_hidden);
21288 /* Display it as active. */
21289 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21290 cursor = No_Cursor;
21292 else if (!NILP (mouse_face) && STRINGP (object))
21294 Lisp_Object b, e;
21295 int ignore;
21297 b = Fprevious_single_property_change (make_number (pos + 1),
21298 Qmouse_face,
21299 object, Qnil);
21300 e = Fnext_single_property_change (position, Qmouse_face,
21301 object, Qnil);
21302 if (NILP (b))
21303 b = make_number (0);
21304 if (NILP (e))
21305 e = make_number (SCHARS (object) - 1);
21306 fast_find_string_pos (w, XINT (b), object,
21307 &dpyinfo->mouse_face_beg_col,
21308 &dpyinfo->mouse_face_beg_row,
21309 &dpyinfo->mouse_face_beg_x,
21310 &dpyinfo->mouse_face_beg_y, 0);
21311 fast_find_string_pos (w, XINT (e), object,
21312 &dpyinfo->mouse_face_end_col,
21313 &dpyinfo->mouse_face_end_row,
21314 &dpyinfo->mouse_face_end_x,
21315 &dpyinfo->mouse_face_end_y, 1);
21316 dpyinfo->mouse_face_past_end = 0;
21317 dpyinfo->mouse_face_window = window;
21318 dpyinfo->mouse_face_face_id
21319 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21320 glyph->face_id, 1);
21321 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21322 cursor = No_Cursor;
21324 else if (STRINGP (object) && NILP (mouse_face))
21326 /* A string which doesn't have mouse-face, but
21327 the text ``under'' it might have. */
21328 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21329 int start = MATRIX_ROW_START_CHARPOS (r);
21331 pos = string_buffer_position (w, object, start);
21332 if (pos > 0)
21333 mouse_face = get_char_property_and_overlay (make_number (pos),
21334 Qmouse_face,
21335 w->buffer,
21336 &overlay);
21337 if (!NILP (mouse_face) && !NILP (overlay))
21339 Lisp_Object before = Foverlay_start (overlay);
21340 Lisp_Object after = Foverlay_end (overlay);
21341 int ignore;
21343 /* Note that we might not be able to find position
21344 BEFORE in the glyph matrix if the overlay is
21345 entirely covered by a `display' property. In
21346 this case, we overshoot. So let's stop in
21347 the glyph matrix before glyphs for OBJECT. */
21348 fast_find_position (w, XFASTINT (before),
21349 &dpyinfo->mouse_face_beg_col,
21350 &dpyinfo->mouse_face_beg_row,
21351 &dpyinfo->mouse_face_beg_x,
21352 &dpyinfo->mouse_face_beg_y,
21353 object);
21355 dpyinfo->mouse_face_past_end
21356 = !fast_find_position (w, XFASTINT (after),
21357 &dpyinfo->mouse_face_end_col,
21358 &dpyinfo->mouse_face_end_row,
21359 &dpyinfo->mouse_face_end_x,
21360 &dpyinfo->mouse_face_end_y,
21361 Qnil);
21362 dpyinfo->mouse_face_window = window;
21363 dpyinfo->mouse_face_face_id
21364 = face_at_buffer_position (w, pos, 0, 0,
21365 &ignore, pos + 1,
21366 !dpyinfo->mouse_face_hidden);
21368 /* Display it as active. */
21369 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21370 cursor = No_Cursor;
21375 check_help_echo:
21377 /* Look for a `help-echo' property. */
21378 if (NILP (help_echo_string)) {
21379 Lisp_Object help, overlay;
21381 /* Check overlays first. */
21382 help = overlay = Qnil;
21383 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21385 overlay = overlay_vec[i];
21386 help = Foverlay_get (overlay, Qhelp_echo);
21389 if (!NILP (help))
21391 help_echo_string = help;
21392 help_echo_window = window;
21393 help_echo_object = overlay;
21394 help_echo_pos = pos;
21396 else
21398 Lisp_Object object = glyph->object;
21399 int charpos = glyph->charpos;
21401 /* Try text properties. */
21402 if (STRINGP (object)
21403 && charpos >= 0
21404 && charpos < SCHARS (object))
21406 help = Fget_text_property (make_number (charpos),
21407 Qhelp_echo, object);
21408 if (NILP (help))
21410 /* If the string itself doesn't specify a help-echo,
21411 see if the buffer text ``under'' it does. */
21412 struct glyph_row *r
21413 = MATRIX_ROW (w->current_matrix, vpos);
21414 int start = MATRIX_ROW_START_CHARPOS (r);
21415 int pos = string_buffer_position (w, object, start);
21416 if (pos > 0)
21418 help = Fget_char_property (make_number (pos),
21419 Qhelp_echo, w->buffer);
21420 if (!NILP (help))
21422 charpos = pos;
21423 object = w->buffer;
21428 else if (BUFFERP (object)
21429 && charpos >= BEGV
21430 && charpos < ZV)
21431 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21432 object);
21434 if (!NILP (help))
21436 help_echo_string = help;
21437 help_echo_window = window;
21438 help_echo_object = object;
21439 help_echo_pos = charpos;
21444 /* Look for a `pointer' property. */
21445 if (NILP (pointer))
21447 /* Check overlays first. */
21448 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21449 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21451 if (NILP (pointer))
21453 Lisp_Object object = glyph->object;
21454 int charpos = glyph->charpos;
21456 /* Try text properties. */
21457 if (STRINGP (object)
21458 && charpos >= 0
21459 && charpos < SCHARS (object))
21461 pointer = Fget_text_property (make_number (charpos),
21462 Qpointer, object);
21463 if (NILP (pointer))
21465 /* If the string itself doesn't specify a pointer,
21466 see if the buffer text ``under'' it does. */
21467 struct glyph_row *r
21468 = MATRIX_ROW (w->current_matrix, vpos);
21469 int start = MATRIX_ROW_START_CHARPOS (r);
21470 int pos = string_buffer_position (w, object, start);
21471 if (pos > 0)
21472 pointer = Fget_char_property (make_number (pos),
21473 Qpointer, w->buffer);
21476 else if (BUFFERP (object)
21477 && charpos >= BEGV
21478 && charpos < ZV)
21479 pointer = Fget_text_property (make_number (charpos),
21480 Qpointer, object);
21484 BEGV = obegv;
21485 ZV = ozv;
21486 current_buffer = obuf;
21489 set_cursor:
21491 define_frame_cursor1 (f, cursor, pointer);
21495 /* EXPORT for RIF:
21496 Clear any mouse-face on window W. This function is part of the
21497 redisplay interface, and is called from try_window_id and similar
21498 functions to ensure the mouse-highlight is off. */
21500 void
21501 x_clear_window_mouse_face (w)
21502 struct window *w;
21504 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21505 Lisp_Object window;
21507 BLOCK_INPUT;
21508 XSETWINDOW (window, w);
21509 if (EQ (window, dpyinfo->mouse_face_window))
21510 clear_mouse_face (dpyinfo);
21511 UNBLOCK_INPUT;
21515 /* EXPORT:
21516 Just discard the mouse face information for frame F, if any.
21517 This is used when the size of F is changed. */
21519 void
21520 cancel_mouse_face (f)
21521 struct frame *f;
21523 Lisp_Object window;
21524 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21526 window = dpyinfo->mouse_face_window;
21527 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21529 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21530 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21531 dpyinfo->mouse_face_window = Qnil;
21536 #endif /* HAVE_WINDOW_SYSTEM */
21539 /***********************************************************************
21540 Exposure Events
21541 ***********************************************************************/
21543 #ifdef HAVE_WINDOW_SYSTEM
21545 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21546 which intersects rectangle R. R is in window-relative coordinates. */
21548 static void
21549 expose_area (w, row, r, area)
21550 struct window *w;
21551 struct glyph_row *row;
21552 XRectangle *r;
21553 enum glyph_row_area area;
21555 struct glyph *first = row->glyphs[area];
21556 struct glyph *end = row->glyphs[area] + row->used[area];
21557 struct glyph *last;
21558 int first_x, start_x, x;
21560 if (area == TEXT_AREA && row->fill_line_p)
21561 /* If row extends face to end of line write the whole line. */
21562 draw_glyphs (w, 0, row, area,
21563 0, row->used[area],
21564 DRAW_NORMAL_TEXT, 0);
21565 else
21567 /* Set START_X to the window-relative start position for drawing glyphs of
21568 AREA. The first glyph of the text area can be partially visible.
21569 The first glyphs of other areas cannot. */
21570 start_x = window_box_left_offset (w, area);
21571 x = start_x;
21572 if (area == TEXT_AREA)
21573 x += row->x;
21575 /* Find the first glyph that must be redrawn. */
21576 while (first < end
21577 && x + first->pixel_width < r->x)
21579 x += first->pixel_width;
21580 ++first;
21583 /* Find the last one. */
21584 last = first;
21585 first_x = x;
21586 while (last < end
21587 && x < r->x + r->width)
21589 x += last->pixel_width;
21590 ++last;
21593 /* Repaint. */
21594 if (last > first)
21595 draw_glyphs (w, first_x - start_x, row, area,
21596 first - row->glyphs[area], last - row->glyphs[area],
21597 DRAW_NORMAL_TEXT, 0);
21602 /* Redraw the parts of the glyph row ROW on window W intersecting
21603 rectangle R. R is in window-relative coordinates. Value is
21604 non-zero if mouse-face was overwritten. */
21606 static int
21607 expose_line (w, row, r)
21608 struct window *w;
21609 struct glyph_row *row;
21610 XRectangle *r;
21612 xassert (row->enabled_p);
21614 if (row->mode_line_p || w->pseudo_window_p)
21615 draw_glyphs (w, 0, row, TEXT_AREA,
21616 0, row->used[TEXT_AREA],
21617 DRAW_NORMAL_TEXT, 0);
21618 else
21620 if (row->used[LEFT_MARGIN_AREA])
21621 expose_area (w, row, r, LEFT_MARGIN_AREA);
21622 if (row->used[TEXT_AREA])
21623 expose_area (w, row, r, TEXT_AREA);
21624 if (row->used[RIGHT_MARGIN_AREA])
21625 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21626 draw_row_fringe_bitmaps (w, row);
21629 return row->mouse_face_p;
21633 /* Redraw those parts of glyphs rows during expose event handling that
21634 overlap other rows. Redrawing of an exposed line writes over parts
21635 of lines overlapping that exposed line; this function fixes that.
21637 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21638 row in W's current matrix that is exposed and overlaps other rows.
21639 LAST_OVERLAPPING_ROW is the last such row. */
21641 static void
21642 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21643 struct window *w;
21644 struct glyph_row *first_overlapping_row;
21645 struct glyph_row *last_overlapping_row;
21647 struct glyph_row *row;
21649 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21650 if (row->overlapping_p)
21652 xassert (row->enabled_p && !row->mode_line_p);
21654 if (row->used[LEFT_MARGIN_AREA])
21655 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21657 if (row->used[TEXT_AREA])
21658 x_fix_overlapping_area (w, row, TEXT_AREA);
21660 if (row->used[RIGHT_MARGIN_AREA])
21661 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21666 /* Return non-zero if W's cursor intersects rectangle R. */
21668 static int
21669 phys_cursor_in_rect_p (w, r)
21670 struct window *w;
21671 XRectangle *r;
21673 XRectangle cr, result;
21674 struct glyph *cursor_glyph;
21676 cursor_glyph = get_phys_cursor_glyph (w);
21677 if (cursor_glyph)
21679 /* r is relative to W's box, but w->phys_cursor.x is relative
21680 to left edge of W's TEXT area. Adjust it. */
21681 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21682 cr.y = w->phys_cursor.y;
21683 cr.width = cursor_glyph->pixel_width;
21684 cr.height = w->phys_cursor_height;
21685 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21686 I assume the effect is the same -- and this is portable. */
21687 return x_intersect_rectangles (&cr, r, &result);
21689 else
21690 return 0;
21694 /* EXPORT:
21695 Draw a vertical window border to the right of window W if W doesn't
21696 have vertical scroll bars. */
21698 void
21699 x_draw_vertical_border (w)
21700 struct window *w;
21702 /* We could do better, if we knew what type of scroll-bar the adjacent
21703 windows (on either side) have... But we don't :-(
21704 However, I think this works ok. ++KFS 2003-04-25 */
21706 /* Redraw borders between horizontally adjacent windows. Don't
21707 do it for frames with vertical scroll bars because either the
21708 right scroll bar of a window, or the left scroll bar of its
21709 neighbor will suffice as a border. */
21710 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21711 return;
21713 if (!WINDOW_RIGHTMOST_P (w)
21714 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21716 int x0, x1, y0, y1;
21718 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21719 y1 -= 1;
21721 rif->draw_vertical_window_border (w, x1, y0, y1);
21723 else if (!WINDOW_LEFTMOST_P (w)
21724 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21726 int x0, x1, y0, y1;
21728 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21729 y1 -= 1;
21731 rif->draw_vertical_window_border (w, x0, y0, y1);
21736 /* Redraw the part of window W intersection rectangle FR. Pixel
21737 coordinates in FR are frame-relative. Call this function with
21738 input blocked. Value is non-zero if the exposure overwrites
21739 mouse-face. */
21741 static int
21742 expose_window (w, fr)
21743 struct window *w;
21744 XRectangle *fr;
21746 struct frame *f = XFRAME (w->frame);
21747 XRectangle wr, r;
21748 int mouse_face_overwritten_p = 0;
21750 /* If window is not yet fully initialized, do nothing. This can
21751 happen when toolkit scroll bars are used and a window is split.
21752 Reconfiguring the scroll bar will generate an expose for a newly
21753 created window. */
21754 if (w->current_matrix == NULL)
21755 return 0;
21757 /* When we're currently updating the window, display and current
21758 matrix usually don't agree. Arrange for a thorough display
21759 later. */
21760 if (w == updated_window)
21762 SET_FRAME_GARBAGED (f);
21763 return 0;
21766 /* Frame-relative pixel rectangle of W. */
21767 wr.x = WINDOW_LEFT_EDGE_X (w);
21768 wr.y = WINDOW_TOP_EDGE_Y (w);
21769 wr.width = WINDOW_TOTAL_WIDTH (w);
21770 wr.height = WINDOW_TOTAL_HEIGHT (w);
21772 if (x_intersect_rectangles (fr, &wr, &r))
21774 int yb = window_text_bottom_y (w);
21775 struct glyph_row *row;
21776 int cursor_cleared_p;
21777 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21779 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21780 r.x, r.y, r.width, r.height));
21782 /* Convert to window coordinates. */
21783 r.x -= WINDOW_LEFT_EDGE_X (w);
21784 r.y -= WINDOW_TOP_EDGE_Y (w);
21786 /* Turn off the cursor. */
21787 if (!w->pseudo_window_p
21788 && phys_cursor_in_rect_p (w, &r))
21790 x_clear_cursor (w);
21791 cursor_cleared_p = 1;
21793 else
21794 cursor_cleared_p = 0;
21796 /* Update lines intersecting rectangle R. */
21797 first_overlapping_row = last_overlapping_row = NULL;
21798 for (row = w->current_matrix->rows;
21799 row->enabled_p;
21800 ++row)
21802 int y0 = row->y;
21803 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21805 if ((y0 >= r.y && y0 < r.y + r.height)
21806 || (y1 > r.y && y1 < r.y + r.height)
21807 || (r.y >= y0 && r.y < y1)
21808 || (r.y + r.height > y0 && r.y + r.height < y1))
21810 if (row->overlapping_p)
21812 if (first_overlapping_row == NULL)
21813 first_overlapping_row = row;
21814 last_overlapping_row = row;
21817 if (expose_line (w, row, &r))
21818 mouse_face_overwritten_p = 1;
21821 if (y1 >= yb)
21822 break;
21825 /* Display the mode line if there is one. */
21826 if (WINDOW_WANTS_MODELINE_P (w)
21827 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21828 row->enabled_p)
21829 && row->y < r.y + r.height)
21831 if (expose_line (w, row, &r))
21832 mouse_face_overwritten_p = 1;
21835 if (!w->pseudo_window_p)
21837 /* Fix the display of overlapping rows. */
21838 if (first_overlapping_row)
21839 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21841 /* Draw border between windows. */
21842 x_draw_vertical_border (w);
21844 /* Turn the cursor on again. */
21845 if (cursor_cleared_p)
21846 update_window_cursor (w, 1);
21850 #ifdef HAVE_CARBON
21851 /* Display scroll bar for this window. */
21852 if (!NILP (w->vertical_scroll_bar))
21854 /* ++KFS:
21855 If this doesn't work here (maybe some header files are missing),
21856 make a function in macterm.c and call it to do the job! */
21857 ControlHandle ch
21858 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21860 Draw1Control (ch);
21862 #endif
21864 return mouse_face_overwritten_p;
21869 /* Redraw (parts) of all windows in the window tree rooted at W that
21870 intersect R. R contains frame pixel coordinates. Value is
21871 non-zero if the exposure overwrites mouse-face. */
21873 static int
21874 expose_window_tree (w, r)
21875 struct window *w;
21876 XRectangle *r;
21878 struct frame *f = XFRAME (w->frame);
21879 int mouse_face_overwritten_p = 0;
21881 while (w && !FRAME_GARBAGED_P (f))
21883 if (!NILP (w->hchild))
21884 mouse_face_overwritten_p
21885 |= expose_window_tree (XWINDOW (w->hchild), r);
21886 else if (!NILP (w->vchild))
21887 mouse_face_overwritten_p
21888 |= expose_window_tree (XWINDOW (w->vchild), r);
21889 else
21890 mouse_face_overwritten_p |= expose_window (w, r);
21892 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21895 return mouse_face_overwritten_p;
21899 /* EXPORT:
21900 Redisplay an exposed area of frame F. X and Y are the upper-left
21901 corner of the exposed rectangle. W and H are width and height of
21902 the exposed area. All are pixel values. W or H zero means redraw
21903 the entire frame. */
21905 void
21906 expose_frame (f, x, y, w, h)
21907 struct frame *f;
21908 int x, y, w, h;
21910 XRectangle r;
21911 int mouse_face_overwritten_p = 0;
21913 TRACE ((stderr, "expose_frame "));
21915 /* No need to redraw if frame will be redrawn soon. */
21916 if (FRAME_GARBAGED_P (f))
21918 TRACE ((stderr, " garbaged\n"));
21919 return;
21922 #ifdef HAVE_CARBON
21923 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21924 or deactivated here, for unknown reasons, activated scroll bars
21925 are shown in deactivated frames in some instances. */
21926 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21927 activate_scroll_bars (f);
21928 else
21929 deactivate_scroll_bars (f);
21930 #endif
21932 /* If basic faces haven't been realized yet, there is no point in
21933 trying to redraw anything. This can happen when we get an expose
21934 event while Emacs is starting, e.g. by moving another window. */
21935 if (FRAME_FACE_CACHE (f) == NULL
21936 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21938 TRACE ((stderr, " no faces\n"));
21939 return;
21942 if (w == 0 || h == 0)
21944 r.x = r.y = 0;
21945 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21946 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21948 else
21950 r.x = x;
21951 r.y = y;
21952 r.width = w;
21953 r.height = h;
21956 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21957 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21959 if (WINDOWP (f->tool_bar_window))
21960 mouse_face_overwritten_p
21961 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21963 #ifdef HAVE_X_WINDOWS
21964 #ifndef MSDOS
21965 #ifndef USE_X_TOOLKIT
21966 if (WINDOWP (f->menu_bar_window))
21967 mouse_face_overwritten_p
21968 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21969 #endif /* not USE_X_TOOLKIT */
21970 #endif
21971 #endif
21973 /* Some window managers support a focus-follows-mouse style with
21974 delayed raising of frames. Imagine a partially obscured frame,
21975 and moving the mouse into partially obscured mouse-face on that
21976 frame. The visible part of the mouse-face will be highlighted,
21977 then the WM raises the obscured frame. With at least one WM, KDE
21978 2.1, Emacs is not getting any event for the raising of the frame
21979 (even tried with SubstructureRedirectMask), only Expose events.
21980 These expose events will draw text normally, i.e. not
21981 highlighted. Which means we must redo the highlight here.
21982 Subsume it under ``we love X''. --gerd 2001-08-15 */
21983 /* Included in Windows version because Windows most likely does not
21984 do the right thing if any third party tool offers
21985 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21986 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21988 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21989 if (f == dpyinfo->mouse_face_mouse_frame)
21991 int x = dpyinfo->mouse_face_mouse_x;
21992 int y = dpyinfo->mouse_face_mouse_y;
21993 clear_mouse_face (dpyinfo);
21994 note_mouse_highlight (f, x, y);
22000 /* EXPORT:
22001 Determine the intersection of two rectangles R1 and R2. Return
22002 the intersection in *RESULT. Value is non-zero if RESULT is not
22003 empty. */
22006 x_intersect_rectangles (r1, r2, result)
22007 XRectangle *r1, *r2, *result;
22009 XRectangle *left, *right;
22010 XRectangle *upper, *lower;
22011 int intersection_p = 0;
22013 /* Rearrange so that R1 is the left-most rectangle. */
22014 if (r1->x < r2->x)
22015 left = r1, right = r2;
22016 else
22017 left = r2, right = r1;
22019 /* X0 of the intersection is right.x0, if this is inside R1,
22020 otherwise there is no intersection. */
22021 if (right->x <= left->x + left->width)
22023 result->x = right->x;
22025 /* The right end of the intersection is the minimum of the
22026 the right ends of left and right. */
22027 result->width = (min (left->x + left->width, right->x + right->width)
22028 - result->x);
22030 /* Same game for Y. */
22031 if (r1->y < r2->y)
22032 upper = r1, lower = r2;
22033 else
22034 upper = r2, lower = r1;
22036 /* The upper end of the intersection is lower.y0, if this is inside
22037 of upper. Otherwise, there is no intersection. */
22038 if (lower->y <= upper->y + upper->height)
22040 result->y = lower->y;
22042 /* The lower end of the intersection is the minimum of the lower
22043 ends of upper and lower. */
22044 result->height = (min (lower->y + lower->height,
22045 upper->y + upper->height)
22046 - result->y);
22047 intersection_p = 1;
22051 return intersection_p;
22054 #endif /* HAVE_WINDOW_SYSTEM */
22057 /***********************************************************************
22058 Initialization
22059 ***********************************************************************/
22061 void
22062 syms_of_xdisp ()
22064 Vwith_echo_area_save_vector = Qnil;
22065 staticpro (&Vwith_echo_area_save_vector);
22067 Vmessage_stack = Qnil;
22068 staticpro (&Vmessage_stack);
22070 Qinhibit_redisplay = intern ("inhibit-redisplay");
22071 staticpro (&Qinhibit_redisplay);
22073 message_dolog_marker1 = Fmake_marker ();
22074 staticpro (&message_dolog_marker1);
22075 message_dolog_marker2 = Fmake_marker ();
22076 staticpro (&message_dolog_marker2);
22077 message_dolog_marker3 = Fmake_marker ();
22078 staticpro (&message_dolog_marker3);
22080 #if GLYPH_DEBUG
22081 defsubr (&Sdump_frame_glyph_matrix);
22082 defsubr (&Sdump_glyph_matrix);
22083 defsubr (&Sdump_glyph_row);
22084 defsubr (&Sdump_tool_bar_row);
22085 defsubr (&Strace_redisplay);
22086 defsubr (&Strace_to_stderr);
22087 #endif
22088 #ifdef HAVE_WINDOW_SYSTEM
22089 defsubr (&Stool_bar_lines_needed);
22090 defsubr (&Slookup_image_map);
22091 #endif
22092 defsubr (&Sformat_mode_line);
22094 staticpro (&Qmenu_bar_update_hook);
22095 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22097 staticpro (&Qoverriding_terminal_local_map);
22098 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22100 staticpro (&Qoverriding_local_map);
22101 Qoverriding_local_map = intern ("overriding-local-map");
22103 staticpro (&Qwindow_scroll_functions);
22104 Qwindow_scroll_functions = intern ("window-scroll-functions");
22106 staticpro (&Qredisplay_end_trigger_functions);
22107 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22109 staticpro (&Qinhibit_point_motion_hooks);
22110 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22112 QCdata = intern (":data");
22113 staticpro (&QCdata);
22114 Qdisplay = intern ("display");
22115 staticpro (&Qdisplay);
22116 Qspace_width = intern ("space-width");
22117 staticpro (&Qspace_width);
22118 Qraise = intern ("raise");
22119 staticpro (&Qraise);
22120 Qslice = intern ("slice");
22121 staticpro (&Qslice);
22122 Qspace = intern ("space");
22123 staticpro (&Qspace);
22124 Qmargin = intern ("margin");
22125 staticpro (&Qmargin);
22126 Qpointer = intern ("pointer");
22127 staticpro (&Qpointer);
22128 Qleft_margin = intern ("left-margin");
22129 staticpro (&Qleft_margin);
22130 Qright_margin = intern ("right-margin");
22131 staticpro (&Qright_margin);
22132 Qcenter = intern ("center");
22133 staticpro (&Qcenter);
22134 Qline_height = intern ("line-height");
22135 staticpro (&Qline_height);
22136 Qtotal = intern ("total");
22137 staticpro (&Qtotal);
22138 QCalign_to = intern (":align-to");
22139 staticpro (&QCalign_to);
22140 QCrelative_width = intern (":relative-width");
22141 staticpro (&QCrelative_width);
22142 QCrelative_height = intern (":relative-height");
22143 staticpro (&QCrelative_height);
22144 QCeval = intern (":eval");
22145 staticpro (&QCeval);
22146 QCpropertize = intern (":propertize");
22147 staticpro (&QCpropertize);
22148 QCfile = intern (":file");
22149 staticpro (&QCfile);
22150 Qfontified = intern ("fontified");
22151 staticpro (&Qfontified);
22152 Qfontification_functions = intern ("fontification-functions");
22153 staticpro (&Qfontification_functions);
22154 Qtrailing_whitespace = intern ("trailing-whitespace");
22155 staticpro (&Qtrailing_whitespace);
22156 Qimage = intern ("image");
22157 staticpro (&Qimage);
22158 QCmap = intern (":map");
22159 staticpro (&QCmap);
22160 QCpointer = intern (":pointer");
22161 staticpro (&QCpointer);
22162 Qrect = intern ("rect");
22163 staticpro (&Qrect);
22164 Qcircle = intern ("circle");
22165 staticpro (&Qcircle);
22166 Qpoly = intern ("poly");
22167 staticpro (&Qpoly);
22168 Qmessage_truncate_lines = intern ("message-truncate-lines");
22169 staticpro (&Qmessage_truncate_lines);
22170 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22171 staticpro (&Qcursor_in_non_selected_windows);
22172 Qgrow_only = intern ("grow-only");
22173 staticpro (&Qgrow_only);
22174 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22175 staticpro (&Qinhibit_menubar_update);
22176 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22177 staticpro (&Qinhibit_eval_during_redisplay);
22178 Qposition = intern ("position");
22179 staticpro (&Qposition);
22180 Qbuffer_position = intern ("buffer-position");
22181 staticpro (&Qbuffer_position);
22182 Qobject = intern ("object");
22183 staticpro (&Qobject);
22184 Qbar = intern ("bar");
22185 staticpro (&Qbar);
22186 Qhbar = intern ("hbar");
22187 staticpro (&Qhbar);
22188 Qbox = intern ("box");
22189 staticpro (&Qbox);
22190 Qhollow = intern ("hollow");
22191 staticpro (&Qhollow);
22192 Qhand = intern ("hand");
22193 staticpro (&Qhand);
22194 Qarrow = intern ("arrow");
22195 staticpro (&Qarrow);
22196 Qtext = intern ("text");
22197 staticpro (&Qtext);
22198 Qrisky_local_variable = intern ("risky-local-variable");
22199 staticpro (&Qrisky_local_variable);
22200 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22201 staticpro (&Qinhibit_free_realized_faces);
22203 list_of_error = Fcons (Fcons (intern ("error"),
22204 Fcons (intern ("void-variable"), Qnil)),
22205 Qnil);
22206 staticpro (&list_of_error);
22208 Qlast_arrow_position = intern ("last-arrow-position");
22209 staticpro (&Qlast_arrow_position);
22210 Qlast_arrow_string = intern ("last-arrow-string");
22211 staticpro (&Qlast_arrow_string);
22213 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22214 staticpro (&Qoverlay_arrow_string);
22215 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22216 staticpro (&Qoverlay_arrow_bitmap);
22218 echo_buffer[0] = echo_buffer[1] = Qnil;
22219 staticpro (&echo_buffer[0]);
22220 staticpro (&echo_buffer[1]);
22222 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22223 staticpro (&echo_area_buffer[0]);
22224 staticpro (&echo_area_buffer[1]);
22226 Vmessages_buffer_name = build_string ("*Messages*");
22227 staticpro (&Vmessages_buffer_name);
22229 mode_line_proptrans_alist = Qnil;
22230 staticpro (&mode_line_proptrans_alist);
22232 mode_line_string_list = Qnil;
22233 staticpro (&mode_line_string_list);
22235 help_echo_string = Qnil;
22236 staticpro (&help_echo_string);
22237 help_echo_object = Qnil;
22238 staticpro (&help_echo_object);
22239 help_echo_window = Qnil;
22240 staticpro (&help_echo_window);
22241 previous_help_echo_string = Qnil;
22242 staticpro (&previous_help_echo_string);
22243 help_echo_pos = -1;
22245 #ifdef HAVE_WINDOW_SYSTEM
22246 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22247 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22248 For example, if a block cursor is over a tab, it will be drawn as
22249 wide as that tab on the display. */);
22250 x_stretch_cursor_p = 0;
22251 #endif
22253 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22254 doc: /* *Non-nil means highlight trailing whitespace.
22255 The face used for trailing whitespace is `trailing-whitespace'. */);
22256 Vshow_trailing_whitespace = Qnil;
22258 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22259 doc: /* *The pointer shape to show in void text areas.
22260 Nil means to show the text pointer. Other options are `arrow', `text',
22261 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22262 Vvoid_text_area_pointer = Qarrow;
22264 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22265 doc: /* Non-nil means don't actually do any redisplay.
22266 This is used for internal purposes. */);
22267 Vinhibit_redisplay = Qnil;
22269 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22270 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22271 Vglobal_mode_string = Qnil;
22273 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22274 doc: /* Marker for where to display an arrow on top of the buffer text.
22275 This must be the beginning of a line in order to work.
22276 See also `overlay-arrow-string'. */);
22277 Voverlay_arrow_position = Qnil;
22279 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22280 doc: /* String to display as an arrow in non-window frames.
22281 See also `overlay-arrow-position'. */);
22282 Voverlay_arrow_string = Qnil;
22284 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22285 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22286 The symbols on this list are examined during redisplay to determine
22287 where to display overlay arrows. */);
22288 Voverlay_arrow_variable_list
22289 = Fcons (intern ("overlay-arrow-position"), Qnil);
22291 DEFVAR_INT ("scroll-step", &scroll_step,
22292 doc: /* *The number of lines to try scrolling a window by when point moves out.
22293 If that fails to bring point back on frame, point is centered instead.
22294 If this is zero, point is always centered after it moves off frame.
22295 If you want scrolling to always be a line at a time, you should set
22296 `scroll-conservatively' to a large value rather than set this to 1. */);
22298 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22299 doc: /* *Scroll up to this many lines, to bring point back on screen.
22300 A value of zero means to scroll the text to center point vertically
22301 in the window. */);
22302 scroll_conservatively = 0;
22304 DEFVAR_INT ("scroll-margin", &scroll_margin,
22305 doc: /* *Number of lines of margin at the top and bottom of a window.
22306 Recenter the window whenever point gets within this many lines
22307 of the top or bottom of the window. */);
22308 scroll_margin = 0;
22310 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22311 doc: /* Pixels per inch on current display.
22312 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22313 Vdisplay_pixels_per_inch = make_float (72.0);
22315 #if GLYPH_DEBUG
22316 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22317 #endif
22319 DEFVAR_BOOL ("truncate-partial-width-windows",
22320 &truncate_partial_width_windows,
22321 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22322 truncate_partial_width_windows = 1;
22324 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22325 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22326 Any other value means to use the appropriate face, `mode-line',
22327 `header-line', or `menu' respectively. */);
22328 mode_line_inverse_video = 1;
22330 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22331 doc: /* *Maximum buffer size for which line number should be displayed.
22332 If the buffer is bigger than this, the line number does not appear
22333 in the mode line. A value of nil means no limit. */);
22334 Vline_number_display_limit = Qnil;
22336 DEFVAR_INT ("line-number-display-limit-width",
22337 &line_number_display_limit_width,
22338 doc: /* *Maximum line width (in characters) for line number display.
22339 If the average length of the lines near point is bigger than this, then the
22340 line number may be omitted from the mode line. */);
22341 line_number_display_limit_width = 200;
22343 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22344 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22345 highlight_nonselected_windows = 0;
22347 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22348 doc: /* Non-nil if more than one frame is visible on this display.
22349 Minibuffer-only frames don't count, but iconified frames do.
22350 This variable is not guaranteed to be accurate except while processing
22351 `frame-title-format' and `icon-title-format'. */);
22353 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22354 doc: /* Template for displaying the title bar of visible frames.
22355 \(Assuming the window manager supports this feature.)
22356 This variable has the same structure as `mode-line-format' (which see),
22357 and is used only on frames for which no explicit name has been set
22358 \(see `modify-frame-parameters'). */);
22360 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22361 doc: /* Template for displaying the title bar of an iconified frame.
22362 \(Assuming the window manager supports this feature.)
22363 This variable has the same structure as `mode-line-format' (which see),
22364 and is used only on frames for which no explicit name has been set
22365 \(see `modify-frame-parameters'). */);
22366 Vicon_title_format
22367 = Vframe_title_format
22368 = Fcons (intern ("multiple-frames"),
22369 Fcons (build_string ("%b"),
22370 Fcons (Fcons (empty_string,
22371 Fcons (intern ("invocation-name"),
22372 Fcons (build_string ("@"),
22373 Fcons (intern ("system-name"),
22374 Qnil)))),
22375 Qnil)));
22377 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22378 doc: /* Maximum number of lines to keep in the message log buffer.
22379 If nil, disable message logging. If t, log messages but don't truncate
22380 the buffer when it becomes large. */);
22381 Vmessage_log_max = make_number (50);
22383 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22384 doc: /* Functions called before redisplay, if window sizes have changed.
22385 The value should be a list of functions that take one argument.
22386 Just before redisplay, for each frame, if any of its windows have changed
22387 size since the last redisplay, or have been split or deleted,
22388 all the functions in the list are called, with the frame as argument. */);
22389 Vwindow_size_change_functions = Qnil;
22391 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22392 doc: /* List of functions to call before redisplaying a window with scrolling.
22393 Each function is called with two arguments, the window
22394 and its new display-start position. Note that the value of `window-end'
22395 is not valid when these functions are called. */);
22396 Vwindow_scroll_functions = Qnil;
22398 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22399 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22400 mouse_autoselect_window = 0;
22402 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22403 doc: /* *Non-nil means automatically resize tool-bars.
22404 This increases a tool-bar's height if not all tool-bar items are visible.
22405 It decreases a tool-bar's height when it would display blank lines
22406 otherwise. */);
22407 auto_resize_tool_bars_p = 1;
22409 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22410 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22411 auto_raise_tool_bar_buttons_p = 1;
22413 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22414 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22415 make_cursor_line_fully_visible_p = 1;
22417 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22418 doc: /* *Margin around tool-bar buttons in pixels.
22419 If an integer, use that for both horizontal and vertical margins.
22420 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22421 HORZ specifying the horizontal margin, and VERT specifying the
22422 vertical margin. */);
22423 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22425 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22426 doc: /* *Relief thickness of tool-bar buttons. */);
22427 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22429 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22430 doc: /* List of functions to call to fontify regions of text.
22431 Each function is called with one argument POS. Functions must
22432 fontify a region starting at POS in the current buffer, and give
22433 fontified regions the property `fontified'. */);
22434 Vfontification_functions = Qnil;
22435 Fmake_variable_buffer_local (Qfontification_functions);
22437 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22438 &unibyte_display_via_language_environment,
22439 doc: /* *Non-nil means display unibyte text according to language environment.
22440 Specifically this means that unibyte non-ASCII characters
22441 are displayed by converting them to the equivalent multibyte characters
22442 according to the current language environment. As a result, they are
22443 displayed according to the current fontset. */);
22444 unibyte_display_via_language_environment = 0;
22446 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22447 doc: /* *Maximum height for resizing mini-windows.
22448 If a float, it specifies a fraction of the mini-window frame's height.
22449 If an integer, it specifies a number of lines. */);
22450 Vmax_mini_window_height = make_float (0.25);
22452 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22453 doc: /* *How to resize mini-windows.
22454 A value of nil means don't automatically resize mini-windows.
22455 A value of t means resize them to fit the text displayed in them.
22456 A value of `grow-only', the default, means let mini-windows grow
22457 only, until their display becomes empty, at which point the windows
22458 go back to their normal size. */);
22459 Vresize_mini_windows = Qgrow_only;
22461 DEFVAR_LISP ("cursor-in-non-selected-windows",
22462 &Vcursor_in_non_selected_windows,
22463 doc: /* *Cursor type to display in non-selected windows.
22464 t means to use hollow box cursor. See `cursor-type' for other values. */);
22465 Vcursor_in_non_selected_windows = Qt;
22467 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22468 doc: /* Alist specifying how to blink the cursor off.
22469 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22470 `cursor-type' frame-parameter or variable equals ON-STATE,
22471 comparing using `equal', Emacs uses OFF-STATE to specify
22472 how to blink it off. */);
22473 Vblink_cursor_alist = Qnil;
22475 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22476 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22477 automatic_hscrolling_p = 1;
22479 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22480 doc: /* *How many columns away from the window edge point is allowed to get
22481 before automatic hscrolling will horizontally scroll the window. */);
22482 hscroll_margin = 5;
22484 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22485 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22486 When point is less than `automatic-hscroll-margin' columns from the window
22487 edge, automatic hscrolling will scroll the window by the amount of columns
22488 determined by this variable. If its value is a positive integer, scroll that
22489 many columns. If it's a positive floating-point number, it specifies the
22490 fraction of the window's width to scroll. If it's nil or zero, point will be
22491 centered horizontally after the scroll. Any other value, including negative
22492 numbers, are treated as if the value were zero.
22494 Automatic hscrolling always moves point outside the scroll margin, so if
22495 point was more than scroll step columns inside the margin, the window will
22496 scroll more than the value given by the scroll step.
22498 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22499 and `scroll-right' overrides this variable's effect. */);
22500 Vhscroll_step = make_number (0);
22502 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22503 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22504 Bind this around calls to `message' to let it take effect. */);
22505 message_truncate_lines = 0;
22507 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22508 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22509 Can be used to update submenus whose contents should vary. */);
22510 Vmenu_bar_update_hook = Qnil;
22512 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22513 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22514 inhibit_menubar_update = 0;
22516 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22517 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22518 inhibit_eval_during_redisplay = 0;
22520 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22521 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22522 inhibit_free_realized_faces = 0;
22524 #if GLYPH_DEBUG
22525 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22526 doc: /* Inhibit try_window_id display optimization. */);
22527 inhibit_try_window_id = 0;
22529 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22530 doc: /* Inhibit try_window_reusing display optimization. */);
22531 inhibit_try_window_reusing = 0;
22533 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22534 doc: /* Inhibit try_cursor_movement display optimization. */);
22535 inhibit_try_cursor_movement = 0;
22536 #endif /* GLYPH_DEBUG */
22540 /* Initialize this module when Emacs starts. */
22542 void
22543 init_xdisp ()
22545 Lisp_Object root_window;
22546 struct window *mini_w;
22548 current_header_line_height = current_mode_line_height = -1;
22550 CHARPOS (this_line_start_pos) = 0;
22552 mini_w = XWINDOW (minibuf_window);
22553 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22555 if (!noninteractive)
22557 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22558 int i;
22560 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22561 set_window_height (root_window,
22562 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22564 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22565 set_window_height (minibuf_window, 1, 0);
22567 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22568 mini_w->total_cols = make_number (FRAME_COLS (f));
22570 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22571 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22572 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22574 /* The default ellipsis glyphs `...'. */
22575 for (i = 0; i < 3; ++i)
22576 default_invis_vector[i] = make_number ('.');
22580 /* Allocate the buffer for frame titles.
22581 Also used for `format-mode-line'. */
22582 int size = 100;
22583 frame_title_buf = (char *) xmalloc (size);
22584 frame_title_buf_end = frame_title_buf + size;
22585 frame_title_ptr = NULL;
22588 help_echo_showing_p = 0;
22592 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22593 (do not change this comment) */