(reveal-open-new-overlays): New extracted fun.
[emacs.git] / src / xdisp.c
blobc7d3cf88ecf984362b173e3bd595fc128a4f2279
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin around tool bar buttons in pixels. */
274 Lisp_Object Vtool_bar_button_margin;
276 /* Thickness of shadow to draw around tool bar buttons. */
278 EMACS_INT tool_bar_button_relief;
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
283 int auto_resize_tool_bars_p;
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
289 int x_stretch_cursor_p;
291 /* Non-nil means don't actually do any redisplay. */
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
297 int inhibit_eval_during_redisplay;
299 /* Names of text properties relevant for redisplay. */
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
304 /* Symbols used in text property values. */
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
318 /* Non-nil means highlight trailing whitespace. */
320 Lisp_Object Vshow_trailing_whitespace;
322 /* Non-nil means escape non-break space and hyphens. */
324 Lisp_Object Vnobreak_char_display;
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
338 #endif /* HAVE_WINDOW_SYSTEM */
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
344 Lisp_Object Vvoid_text_area_pointer;
346 /* Name of the face used to highlight trailing whitespace. */
348 Lisp_Object Qtrailing_whitespace;
350 /* Name and number of the face used to highlight escape glyphs. */
352 Lisp_Object Qescape_glyph;
354 /* Name and number of the face used to highlight non-breaking spaces. */
356 Lisp_Object Qnobreak_space;
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
361 Lisp_Object Qimage;
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
370 int noninteractive_need_newline;
372 /* Non-zero means print newline to message log before next message. */
374 static int message_log_need_newline;
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
388 static struct text_pos this_line_start_pos;
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
393 static struct text_pos this_line_end_pos;
395 /* The vertical positions and the height of this line. */
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
404 static int this_line_start_x;
406 /* Buffer that this_line_.* variables are referring to. */
408 static struct buffer *this_line_buffer;
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
413 int truncate_partial_width_windows;
415 /* A flag to control how to display unibyte 8-bit character. */
417 int unibyte_display_via_language_environment;
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
423 int multiple_frames;
425 Lisp_Object Vglobal_mode_string;
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
432 Lisp_Object Voverlay_arrow_variable_list;
434 /* Marker for where to display an arrow on top of the buffer text. */
436 Lisp_Object Voverlay_arrow_position;
438 /* String to display for the arrow. Only used on terminal frames. */
440 Lisp_Object Voverlay_arrow_string;
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
454 /* Like mode-line-format, but for the title bar on a visible frame. */
456 Lisp_Object Vframe_title_format;
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
460 Lisp_Object Vicon_title_format;
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
466 static Lisp_Object Vwindow_size_change_functions;
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
470 /* Nonzero if an overlay arrow has been displayed in this window. */
472 static int overlay_arrow_seen;
474 /* Nonzero means highlight the region even in nonselected windows. */
476 int highlight_nonselected_windows;
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
481 static EMACS_INT scroll_step;
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
486 static EMACS_INT scroll_conservatively;
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
493 EMACS_INT scroll_margin;
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
499 int buffer_shared;
501 /* Vector containing glyphs for an ellipsis `...'. */
503 static Lisp_Object default_invis_vector[3];
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
509 This variable is deprecated. */
511 int mode_line_inverse_video;
513 /* Prompt to display in front of the mini-buffer contents. */
515 Lisp_Object minibuf_prompt;
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
520 int minibuf_prompt_width;
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
526 Lisp_Object echo_area_window;
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
533 Lisp_Object Vmessage_stack;
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
538 int message_enable_multibyte;
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
542 int update_mode_lines;
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
547 int windows_or_buffers_changed;
549 /* Nonzero means a frame's cursor type has been changed. */
551 int cursor_type_changed;
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
556 int line_number_displayed;
558 /* Maximum buffer size for which to display line numbers. */
560 Lisp_Object Vline_number_display_limit;
562 /* Line width to consider when repositioning for line number display. */
564 static EMACS_INT line_number_display_limit_width;
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
569 Lisp_Object Vmessage_log_max;
571 /* The name of the *Messages* buffer, a string. */
573 static Lisp_Object Vmessages_buffer_name;
575 /* Index 0 is the buffer that holds the current (desired) echo area message,
576 or nil if none is desired right now.
578 Index 1 is the buffer that holds the previously displayed echo area message,
579 or nil to indicate no message. This is normally what's on the screen now.
581 These two can point to the same buffer. That happens when the last
582 message output by the user (or made by echoing) has been displayed. */
584 Lisp_Object echo_area_buffer[2];
586 /* Permanent pointers to the two buffers that are used for echo area
587 purposes. Once the two buffers are made, and their pointers are
588 placed here, these two slots remain unchanged unless those buffers
589 need to be created afresh. */
591 static Lisp_Object echo_buffer[2];
593 /* A vector saved used in with_area_buffer to reduce consing. */
595 static Lisp_Object Vwith_echo_area_save_vector;
597 /* Non-zero means display_echo_area should display the last echo area
598 message again. Set by redisplay_preserve_echo_area. */
600 static int display_last_displayed_message_p;
602 /* Nonzero if echo area is being used by print; zero if being used by
603 message. */
605 int message_buf_print;
607 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
609 Lisp_Object Qinhibit_menubar_update;
610 int inhibit_menubar_update;
612 /* Maximum height for resizing mini-windows. Either a float
613 specifying a fraction of the available height, or an integer
614 specifying a number of lines. */
616 Lisp_Object Vmax_mini_window_height;
618 /* Non-zero means messages should be displayed with truncated
619 lines instead of being continued. */
621 int message_truncate_lines;
622 Lisp_Object Qmessage_truncate_lines;
624 /* Set to 1 in clear_message to make redisplay_internal aware
625 of an emptied echo area. */
627 static int message_cleared_p;
629 /* How to blink the default frame cursor off. */
630 Lisp_Object Vblink_cursor_alist;
632 /* A scratch glyph row with contents used for generating truncation
633 glyphs. Also used in direct_output_for_insert. */
635 #define MAX_SCRATCH_GLYPHS 100
636 struct glyph_row scratch_glyph_row;
637 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
639 /* Ascent and height of the last line processed by move_it_to. */
641 static int last_max_ascent, last_height;
643 /* Non-zero if there's a help-echo in the echo area. */
645 int help_echo_showing_p;
647 /* If >= 0, computed, exact values of mode-line and header-line height
648 to use in the macros CURRENT_MODE_LINE_HEIGHT and
649 CURRENT_HEADER_LINE_HEIGHT. */
651 int current_mode_line_height, current_header_line_height;
653 /* The maximum distance to look ahead for text properties. Values
654 that are too small let us call compute_char_face and similar
655 functions too often which is expensive. Values that are too large
656 let us call compute_char_face and alike too often because we
657 might not be interested in text properties that far away. */
659 #define TEXT_PROP_DISTANCE_LIMIT 100
661 #if GLYPH_DEBUG
663 /* Variables to turn off display optimizations from Lisp. */
665 int inhibit_try_window_id, inhibit_try_window_reusing;
666 int inhibit_try_cursor_movement;
668 /* Non-zero means print traces of redisplay if compiled with
669 GLYPH_DEBUG != 0. */
671 int trace_redisplay_p;
673 #endif /* GLYPH_DEBUG */
675 #ifdef DEBUG_TRACE_MOVE
676 /* Non-zero means trace with TRACE_MOVE to stderr. */
677 int trace_move;
679 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
680 #else
681 #define TRACE_MOVE(x) (void) 0
682 #endif
684 /* Non-zero means automatically scroll windows horizontally to make
685 point visible. */
687 int automatic_hscrolling_p;
689 /* How close to the margin can point get before the window is scrolled
690 horizontally. */
691 EMACS_INT hscroll_margin;
693 /* How much to scroll horizontally when point is inside the above margin. */
694 Lisp_Object Vhscroll_step;
696 /* The variable `resize-mini-windows'. If nil, don't resize
697 mini-windows. If t, always resize them to fit the text they
698 display. If `grow-only', let mini-windows grow only until they
699 become empty. */
701 Lisp_Object Vresize_mini_windows;
703 /* Buffer being redisplayed -- for redisplay_window_error. */
705 struct buffer *displayed_buffer;
707 /* Value returned from text property handlers (see below). */
709 enum prop_handled
711 HANDLED_NORMALLY,
712 HANDLED_RECOMPUTE_PROPS,
713 HANDLED_OVERLAY_STRING_CONSUMED,
714 HANDLED_RETURN
717 /* A description of text properties that redisplay is interested
718 in. */
720 struct props
722 /* The name of the property. */
723 Lisp_Object *name;
725 /* A unique index for the property. */
726 enum prop_idx idx;
728 /* A handler function called to set up iterator IT from the property
729 at IT's current position. Value is used to steer handle_stop. */
730 enum prop_handled (*handler) P_ ((struct it *it));
733 static enum prop_handled handle_face_prop P_ ((struct it *));
734 static enum prop_handled handle_invisible_prop P_ ((struct it *));
735 static enum prop_handled handle_display_prop P_ ((struct it *));
736 static enum prop_handled handle_composition_prop P_ ((struct it *));
737 static enum prop_handled handle_overlay_change P_ ((struct it *));
738 static enum prop_handled handle_fontified_prop P_ ((struct it *));
740 /* Properties handled by iterators. */
742 static struct props it_props[] =
744 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
745 /* Handle `face' before `display' because some sub-properties of
746 `display' need to know the face. */
747 {&Qface, FACE_PROP_IDX, handle_face_prop},
748 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
749 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
750 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
751 {NULL, 0, NULL}
754 /* Value is the position described by X. If X is a marker, value is
755 the marker_position of X. Otherwise, value is X. */
757 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
759 /* Enumeration returned by some move_it_.* functions internally. */
761 enum move_it_result
763 /* Not used. Undefined value. */
764 MOVE_UNDEFINED,
766 /* Move ended at the requested buffer position or ZV. */
767 MOVE_POS_MATCH_OR_ZV,
769 /* Move ended at the requested X pixel position. */
770 MOVE_X_REACHED,
772 /* Move within a line ended at the end of a line that must be
773 continued. */
774 MOVE_LINE_CONTINUED,
776 /* Move within a line ended at the end of a line that would
777 be displayed truncated. */
778 MOVE_LINE_TRUNCATED,
780 /* Move within a line ended at a line end. */
781 MOVE_NEWLINE_OR_CR
784 /* This counter is used to clear the face cache every once in a while
785 in redisplay_internal. It is incremented for each redisplay.
786 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
787 cleared. */
789 #define CLEAR_FACE_CACHE_COUNT 500
790 static int clear_face_cache_count;
792 /* Similarly for the image cache. */
794 #ifdef HAVE_WINDOW_SYSTEM
795 #define CLEAR_IMAGE_CACHE_COUNT 101
796 static int clear_image_cache_count;
797 #endif
799 /* Record the previous terminal frame we displayed. */
801 static struct frame *previous_terminal_frame;
803 /* Non-zero while redisplay_internal is in progress. */
805 int redisplaying_p;
807 /* Non-zero means don't free realized faces. Bound while freeing
808 realized faces is dangerous because glyph matrices might still
809 reference them. */
811 int inhibit_free_realized_faces;
812 Lisp_Object Qinhibit_free_realized_faces;
814 /* If a string, XTread_socket generates an event to display that string.
815 (The display is done in read_char.) */
817 Lisp_Object help_echo_string;
818 Lisp_Object help_echo_window;
819 Lisp_Object help_echo_object;
820 int help_echo_pos;
822 /* Temporary variable for XTread_socket. */
824 Lisp_Object previous_help_echo_string;
826 /* Null glyph slice */
828 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
831 /* Function prototypes. */
833 static void setup_for_ellipsis P_ ((struct it *, int));
834 static void mark_window_display_accurate_1 P_ ((struct window *, int));
835 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
837 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
838 static int redisplay_mode_lines P_ ((Lisp_Object, int));
839 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
841 #if 0
842 static int invisible_text_between_p P_ ((struct it *, int, int));
843 #endif
845 static void pint2str P_ ((char *, int, int));
846 static void pint2hrstr P_ ((char *, int, int));
847 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
848 struct text_pos));
849 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
850 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
851 static void store_mode_line_noprop_char P_ ((char));
852 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
853 static void x_consider_frame_title P_ ((Lisp_Object));
854 static void handle_stop P_ ((struct it *));
855 static int tool_bar_lines_needed P_ ((struct frame *));
856 static int single_display_spec_intangible_p P_ ((Lisp_Object));
857 static void ensure_echo_area_buffers P_ ((void));
858 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
859 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
860 static int with_echo_area_buffer P_ ((struct window *, int,
861 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
862 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
863 static void clear_garbaged_frames P_ ((void));
864 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int display_echo_area P_ ((struct window *));
868 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
871 static int string_char_and_length P_ ((const unsigned char *, int, int *));
872 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
873 struct text_pos));
874 static int compute_window_start_on_continuation_line P_ ((struct window *));
875 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
876 static void insert_left_trunc_glyphs P_ ((struct it *));
877 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
878 Lisp_Object));
879 static void extend_face_to_end_of_line P_ ((struct it *));
880 static int append_space_for_newline P_ ((struct it *, int));
881 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
882 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
883 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
884 static int trailing_whitespace_p P_ ((int));
885 static int message_log_check_duplicate P_ ((int, int, int, int));
886 static void push_it P_ ((struct it *));
887 static void pop_it P_ ((struct it *));
888 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
889 static void select_frame_for_redisplay P_ ((Lisp_Object));
890 static void redisplay_internal P_ ((int));
891 static int echo_area_display P_ ((int));
892 static void redisplay_windows P_ ((Lisp_Object));
893 static void redisplay_window P_ ((Lisp_Object, int));
894 static Lisp_Object redisplay_window_error ();
895 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
896 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
897 static void update_menu_bar P_ ((struct frame *, int));
898 static int try_window_reusing_current_matrix P_ ((struct window *));
899 static int try_window_id P_ ((struct window *));
900 static int display_line P_ ((struct it *));
901 static int display_mode_lines P_ ((struct window *));
902 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
903 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
904 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
905 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
906 static void display_menu_bar P_ ((struct window *));
907 static int display_count_lines P_ ((int, int, int, int, int *));
908 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
909 int, int, struct it *, int, int, int, int));
910 static void compute_line_metrics P_ ((struct it *));
911 static void run_redisplay_end_trigger_hook P_ ((struct it *));
912 static int get_overlay_strings P_ ((struct it *, int));
913 static void next_overlay_string P_ ((struct it *));
914 static void reseat P_ ((struct it *, struct text_pos, int));
915 static void reseat_1 P_ ((struct it *, struct text_pos, int));
916 static void back_to_previous_visible_line_start P_ ((struct it *));
917 void reseat_at_previous_visible_line_start P_ ((struct it *));
918 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
919 static int next_element_from_ellipsis P_ ((struct it *));
920 static int next_element_from_display_vector P_ ((struct it *));
921 static int next_element_from_string P_ ((struct it *));
922 static int next_element_from_c_string P_ ((struct it *));
923 static int next_element_from_buffer P_ ((struct it *));
924 static int next_element_from_composition P_ ((struct it *));
925 static int next_element_from_image P_ ((struct it *));
926 static int next_element_from_stretch P_ ((struct it *));
927 static void load_overlay_strings P_ ((struct it *, int));
928 static int init_from_display_pos P_ ((struct it *, struct window *,
929 struct display_pos *));
930 static void reseat_to_string P_ ((struct it *, unsigned char *,
931 Lisp_Object, int, int, int, int));
932 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
933 int, int, int));
934 void move_it_vertically_backward P_ ((struct it *, int));
935 static void init_to_row_start P_ ((struct it *, struct window *,
936 struct glyph_row *));
937 static int init_to_row_end P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static void back_to_previous_line_start P_ ((struct it *));
940 static int forward_to_next_line_start P_ ((struct it *, int *));
941 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
942 Lisp_Object, int));
943 static struct text_pos string_pos P_ ((int, Lisp_Object));
944 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
945 static int number_of_chars P_ ((unsigned char *, int));
946 static void compute_stop_pos P_ ((struct it *));
947 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
948 Lisp_Object));
949 static int face_before_or_after_it_pos P_ ((struct it *, int));
950 static int next_overlay_change P_ ((int));
951 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
952 Lisp_Object, struct text_pos *,
953 int));
954 static int underlying_face_id P_ ((struct it *));
955 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
956 struct window *));
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
961 #ifdef HAVE_WINDOW_SYSTEM
963 static void update_tool_bar P_ ((struct frame *, int));
964 static void build_desired_tool_bar_string P_ ((struct frame *f));
965 static int redisplay_tool_bar P_ ((struct frame *));
966 static void display_tool_bar_line P_ ((struct it *));
967 static void notice_overwritten_cursor P_ ((struct window *,
968 enum glyph_row_area,
969 int, int, int, int));
973 #endif /* HAVE_WINDOW_SYSTEM */
976 /***********************************************************************
977 Window display dimensions
978 ***********************************************************************/
980 /* Return the bottom boundary y-position for text lines in window W.
981 This is the first y position at which a line cannot start.
982 It is relative to the top of the window.
984 This is the height of W minus the height of a mode line, if any. */
986 INLINE int
987 window_text_bottom_y (w)
988 struct window *w;
990 int height = WINDOW_TOTAL_HEIGHT (w);
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1001 INLINE int
1002 window_box_width (w, area)
1003 struct window *w;
1004 int area;
1006 int cols = XFASTINT (w->total_cols);
1007 int pixels = 0;
1009 if (!w->pseudo_window_p)
1011 cols -= WINDOW_SCROLL_BAR_COLS (w);
1013 if (area == TEXT_AREA)
1015 if (INTEGERP (w->left_margin_cols))
1016 cols -= XFASTINT (w->left_margin_cols);
1017 if (INTEGERP (w->right_margin_cols))
1018 cols -= XFASTINT (w->right_margin_cols);
1019 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1021 else if (area == LEFT_MARGIN_AREA)
1023 cols = (INTEGERP (w->left_margin_cols)
1024 ? XFASTINT (w->left_margin_cols) : 0);
1025 pixels = 0;
1027 else if (area == RIGHT_MARGIN_AREA)
1029 cols = (INTEGERP (w->right_margin_cols)
1030 ? XFASTINT (w->right_margin_cols) : 0);
1031 pixels = 0;
1035 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1039 /* Return the pixel height of the display area of window W, not
1040 including mode lines of W, if any. */
1042 INLINE int
1043 window_box_height (w)
1044 struct window *w;
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1049 xassert (height >= 0);
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1057 if (WINDOW_WANTS_MODELINE_P (w))
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1090 INLINE int
1091 window_box_left_offset (w, area)
1092 struct window *w;
1093 int area;
1095 int x;
1097 if (w->pseudo_window_p)
1098 return 0;
1100 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1102 if (area == TEXT_AREA)
1103 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1104 + window_box_width (w, LEFT_MARGIN_AREA));
1105 else if (area == RIGHT_MARGIN_AREA)
1106 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1107 + window_box_width (w, LEFT_MARGIN_AREA)
1108 + window_box_width (w, TEXT_AREA)
1109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1111 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1112 else if (area == LEFT_MARGIN_AREA
1113 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1114 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1116 return x;
1120 /* Return the window-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1124 INLINE int
1125 window_box_right_offset (w, area)
1126 struct window *w;
1127 int area;
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1136 INLINE int
1137 window_box_left (w, area)
1138 struct window *w;
1139 int area;
1141 struct frame *f = XFRAME (w->frame);
1142 int x;
1144 if (w->pseudo_window_p)
1145 return FRAME_INTERNAL_BORDER_WIDTH (f);
1147 x = (WINDOW_LEFT_EDGE_X (w)
1148 + window_box_left_offset (w, area));
1150 return x;
1154 /* Return the frame-relative coordinate of the right edge of display
1155 area AREA of window W. AREA < 0 means return the left edge of the
1156 whole window, to the left of the right fringe of W. */
1158 INLINE int
1159 window_box_right (w, area)
1160 struct window *w;
1161 int area;
1163 return window_box_left (w, area) + window_box_width (w, area);
1166 /* Get the bounding box of the display area AREA of window W, without
1167 mode lines, in frame-relative coordinates. AREA < 0 means the
1168 whole window, not including the left and right fringes of
1169 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1170 coordinates of the upper-left corner of the box. Return in
1171 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1173 INLINE void
1174 window_box (w, area, box_x, box_y, box_width, box_height)
1175 struct window *w;
1176 int area;
1177 int *box_x, *box_y, *box_width, *box_height;
1179 if (box_width)
1180 *box_width = window_box_width (w, area);
1181 if (box_height)
1182 *box_height = window_box_height (w);
1183 if (box_x)
1184 *box_x = window_box_left (w, area);
1185 if (box_y)
1187 *box_y = WINDOW_TOP_EDGE_Y (w);
1188 if (WINDOW_WANTS_HEADER_LINE_P (w))
1189 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines. AREA < 0 means the whole window, not including the
1196 left and right fringe of the window. Return in *TOP_LEFT_X
1197 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1198 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1199 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1200 box. */
1202 INLINE void
1203 window_box_edges (w, area, top_left_x, top_left_y,
1204 bottom_right_x, bottom_right_y)
1205 struct window *w;
1206 int area;
1207 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1209 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1210 bottom_right_y);
1211 *bottom_right_x += *top_left_x;
1212 *bottom_right_y += *top_left_y;
1217 /***********************************************************************
1218 Utilities
1219 ***********************************************************************/
1221 /* Return the bottom y-position of the line the iterator IT is in.
1222 This can modify IT's settings. */
1225 line_bottom_y (it)
1226 struct it *it;
1228 int line_height = it->max_ascent + it->max_descent;
1229 int line_top_y = it->current_y;
1231 if (line_height == 0)
1233 if (last_height)
1234 line_height = last_height;
1235 else if (IT_CHARPOS (*it) < ZV)
1237 move_it_by_lines (it, 1, 1);
1238 line_height = (it->max_ascent || it->max_descent
1239 ? it->max_ascent + it->max_descent
1240 : last_height);
1242 else
1244 struct glyph_row *row = it->glyph_row;
1246 /* Use the default character height. */
1247 it->glyph_row = NULL;
1248 it->what = IT_CHARACTER;
1249 it->c = ' ';
1250 it->len = 1;
1251 PRODUCE_GLYPHS (it);
1252 line_height = it->ascent + it->descent;
1253 it->glyph_row = row;
1257 return line_top_y + line_height;
1261 /* Return 1 if position CHARPOS is visible in window W.
1262 If visible, set *X and *Y to pixel coordinates of top left corner.
1263 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1264 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1265 and header-lines heights. */
1268 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1269 struct window *w;
1270 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1272 struct it it;
1273 struct text_pos top;
1274 int visible_p = 0;
1275 struct buffer *old_buffer = NULL;
1277 if (noninteractive)
1278 return visible_p;
1280 if (XBUFFER (w->buffer) != current_buffer)
1282 old_buffer = current_buffer;
1283 set_buffer_internal_1 (XBUFFER (w->buffer));
1286 SET_TEXT_POS_FROM_MARKER (top, w->start);
1288 /* Compute exact mode line heights, if requested. */
1289 if (exact_mode_line_heights_p)
1291 if (WINDOW_WANTS_MODELINE_P (w))
1292 current_mode_line_height
1293 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1294 current_buffer->mode_line_format);
1296 if (WINDOW_WANTS_HEADER_LINE_P (w))
1297 current_header_line_height
1298 = display_mode_line (w, HEADER_LINE_FACE_ID,
1299 current_buffer->header_line_format);
1302 start_display (&it, w, top);
1303 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1304 MOVE_TO_POS | MOVE_TO_Y);
1306 /* Note that we may overshoot because of invisible text. */
1307 if (IT_CHARPOS (it) >= charpos)
1309 int top_x = it.current_x;
1310 int top_y = it.current_y;
1311 int bottom_y = (last_height = 0, line_bottom_y (&it));
1312 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1314 if (top_y < window_top_y)
1315 visible_p = bottom_y > window_top_y;
1316 else if (top_y < it.last_visible_y)
1317 visible_p = 1;
1318 if (visible_p)
1320 *x = top_x;
1321 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1322 *rtop = max (0, window_top_y - top_y);
1323 *rbot = max (0, bottom_y - it.last_visible_y);
1326 else
1328 struct it it2;
1330 it2 = it;
1331 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1332 move_it_by_lines (&it, 1, 0);
1333 if (charpos < IT_CHARPOS (it))
1335 visible_p = 1;
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
1345 if (old_buffer)
1346 set_buffer_internal_1 (old_buffer);
1348 current_header_line_height = current_mode_line_height = -1;
1350 if (visible_p && XFASTINT (w->hscroll) > 0)
1351 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1353 return visible_p;
1357 /* Return the next character from STR which is MAXLEN bytes long.
1358 Return in *LEN the length of the character. This is like
1359 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1360 we find one, we return a `?', but with the length of the invalid
1361 character. */
1363 static INLINE int
1364 string_char_and_length (str, maxlen, len)
1365 const unsigned char *str;
1366 int maxlen, *len;
1368 int c;
1370 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1371 if (!CHAR_VALID_P (c, 1))
1372 /* We may not change the length here because other places in Emacs
1373 don't use this function, i.e. they silently accept invalid
1374 characters. */
1375 c = '?';
1377 return c;
1382 /* Given a position POS containing a valid character and byte position
1383 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1385 static struct text_pos
1386 string_pos_nchars_ahead (pos, string, nchars)
1387 struct text_pos pos;
1388 Lisp_Object string;
1389 int nchars;
1391 xassert (STRINGP (string) && nchars >= 0);
1393 if (STRING_MULTIBYTE (string))
1395 int rest = SBYTES (string) - BYTEPOS (pos);
1396 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1397 int len;
1399 while (nchars--)
1401 string_char_and_length (p, rest, &len);
1402 p += len, rest -= len;
1403 xassert (rest >= 0);
1404 CHARPOS (pos) += 1;
1405 BYTEPOS (pos) += len;
1408 else
1409 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1411 return pos;
1415 /* Value is the text position, i.e. character and byte position,
1416 for character position CHARPOS in STRING. */
1418 static INLINE struct text_pos
1419 string_pos (charpos, string)
1420 int charpos;
1421 Lisp_Object string;
1423 struct text_pos pos;
1424 xassert (STRINGP (string));
1425 xassert (charpos >= 0);
1426 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1427 return pos;
1431 /* Value is a text position, i.e. character and byte position, for
1432 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1433 means recognize multibyte characters. */
1435 static struct text_pos
1436 c_string_pos (charpos, s, multibyte_p)
1437 int charpos;
1438 unsigned char *s;
1439 int multibyte_p;
1441 struct text_pos pos;
1443 xassert (s != NULL);
1444 xassert (charpos >= 0);
1446 if (multibyte_p)
1448 int rest = strlen (s), len;
1450 SET_TEXT_POS (pos, 0, 0);
1451 while (charpos--)
1453 string_char_and_length (s, rest, &len);
1454 s += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
1460 else
1461 SET_TEXT_POS (pos, charpos, charpos);
1463 return pos;
1467 /* Value is the number of characters in C string S. MULTIBYTE_P
1468 non-zero means recognize multibyte characters. */
1470 static int
1471 number_of_chars (s, multibyte_p)
1472 unsigned char *s;
1473 int multibyte_p;
1475 int nchars;
1477 if (multibyte_p)
1479 int rest = strlen (s), len;
1480 unsigned char *p = (unsigned char *) s;
1482 for (nchars = 0; rest > 0; ++nchars)
1484 string_char_and_length (p, rest, &len);
1485 rest -= len, p += len;
1488 else
1489 nchars = strlen (s);
1491 return nchars;
1495 /* Compute byte position NEWPOS->bytepos corresponding to
1496 NEWPOS->charpos. POS is a known position in string STRING.
1497 NEWPOS->charpos must be >= POS.charpos. */
1499 static void
1500 compute_string_pos (newpos, pos, string)
1501 struct text_pos *newpos, pos;
1502 Lisp_Object string;
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1507 if (STRING_MULTIBYTE (string))
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
1514 /* EXPORT:
1515 Return an estimation of the pixel height of mode or top lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1519 estimate_mode_line_height (f, face_id)
1520 struct frame *f;
1521 enum face_id face_id;
1523 #ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1526 int height = FONT_HEIGHT (FRAME_FONT (f));
1528 /* This function is called so early when Emacs starts that the face
1529 cache and mode line face are not yet initialized. */
1530 if (FRAME_FACE_CACHE (f))
1532 struct face *face = FACE_FROM_ID (f, face_id);
1533 if (face)
1535 if (face->font)
1536 height = FONT_HEIGHT (face->font);
1537 if (face->box_line_width > 0)
1538 height += 2 * face->box_line_width;
1542 return height;
1544 #endif
1546 return 1;
1549 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1550 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1551 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1552 not force the value into range. */
1554 void
1555 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1556 FRAME_PTR f;
1557 register int pix_x, pix_y;
1558 int *x, *y;
1559 NativeRectangle *bounds;
1560 int noclip;
1563 #ifdef HAVE_WINDOW_SYSTEM
1564 if (FRAME_WINDOW_P (f))
1566 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1567 even for negative values. */
1568 if (pix_x < 0)
1569 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1570 if (pix_y < 0)
1571 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1573 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1574 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1576 if (bounds)
1577 STORE_NATIVE_RECT (*bounds,
1578 FRAME_COL_TO_PIXEL_X (f, pix_x),
1579 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1580 FRAME_COLUMN_WIDTH (f) - 1,
1581 FRAME_LINE_HEIGHT (f) - 1);
1583 if (!noclip)
1585 if (pix_x < 0)
1586 pix_x = 0;
1587 else if (pix_x > FRAME_TOTAL_COLS (f))
1588 pix_x = FRAME_TOTAL_COLS (f);
1590 if (pix_y < 0)
1591 pix_y = 0;
1592 else if (pix_y > FRAME_LINES (f))
1593 pix_y = FRAME_LINES (f);
1596 #endif
1598 *x = pix_x;
1599 *y = pix_y;
1603 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1604 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1605 can't tell the positions because W's display is not up to date,
1606 return 0. */
1609 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1610 struct window *w;
1611 int hpos, vpos;
1612 int *frame_x, *frame_y;
1614 #ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1617 int success_p;
1619 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1620 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1622 if (display_completed)
1624 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1625 struct glyph *glyph = row->glyphs[TEXT_AREA];
1626 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1628 hpos = row->x;
1629 vpos = row->y;
1630 while (glyph < end)
1632 hpos += glyph->pixel_width;
1633 ++glyph;
1636 /* If first glyph is partially visible, its first visible position is still 0. */
1637 if (hpos < 0)
1638 hpos = 0;
1640 success_p = 1;
1642 else
1644 hpos = vpos = 0;
1645 success_p = 0;
1648 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1649 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1650 return success_p;
1652 #endif
1654 *frame_x = hpos;
1655 *frame_y = vpos;
1656 return 1;
1660 #ifdef HAVE_WINDOW_SYSTEM
1662 /* Find the glyph under window-relative coordinates X/Y in window W.
1663 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1664 strings. Return in *HPOS and *VPOS the row and column number of
1665 the glyph found. Return in *AREA the glyph area containing X.
1666 Value is a pointer to the glyph found or null if X/Y is not on
1667 text, or we can't tell because W's current matrix is not up to
1668 date. */
1670 static struct glyph *
1671 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1672 struct window *w;
1673 int x, y;
1674 int *hpos, *vpos, *dx, *dy, *area;
1676 struct glyph *glyph, *end;
1677 struct glyph_row *row = NULL;
1678 int x0, i;
1680 /* Find row containing Y. Give up if some row is not enabled. */
1681 for (i = 0; i < w->current_matrix->nrows; ++i)
1683 row = MATRIX_ROW (w->current_matrix, i);
1684 if (!row->enabled_p)
1685 return NULL;
1686 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1687 break;
1690 *vpos = i;
1691 *hpos = 0;
1693 /* Give up if Y is not in the window. */
1694 if (i == w->current_matrix->nrows)
1695 return NULL;
1697 /* Get the glyph area containing X. */
1698 if (w->pseudo_window_p)
1700 *area = TEXT_AREA;
1701 x0 = 0;
1703 else
1705 if (x < window_box_left_offset (w, TEXT_AREA))
1707 *area = LEFT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1710 else if (x < window_box_right_offset (w, TEXT_AREA))
1712 *area = TEXT_AREA;
1713 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1715 else
1717 *area = RIGHT_MARGIN_AREA;
1718 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1722 /* Find glyph containing X. */
1723 glyph = row->glyphs[*area];
1724 end = glyph + row->used[*area];
1725 x -= x0;
1726 while (glyph < end && x >= glyph->pixel_width)
1728 x -= glyph->pixel_width;
1729 ++glyph;
1732 if (glyph == end)
1733 return NULL;
1735 if (dx)
1737 *dx = x;
1738 *dy = y - (row->y + row->ascent - glyph->ascent);
1741 *hpos = glyph - row->glyphs[*area];
1742 return glyph;
1746 /* EXPORT:
1747 Convert frame-relative x/y to coordinates relative to window W.
1748 Takes pseudo-windows into account. */
1750 void
1751 frame_to_window_pixel_xy (w, x, y)
1752 struct window *w;
1753 int *x, *y;
1755 if (w->pseudo_window_p)
1757 /* A pseudo-window is always full-width, and starts at the
1758 left edge of the frame, plus a frame border. */
1759 struct frame *f = XFRAME (w->frame);
1760 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1761 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1763 else
1765 *x -= WINDOW_LEFT_EDGE_X (w);
1766 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1770 /* EXPORT:
1771 Return in RECTS[] at most N clipping rectangles for glyph string S.
1772 Return the number of stored rectangles. */
1775 get_glyph_string_clip_rects (s, rects, n)
1776 struct glyph_string *s;
1777 NativeRectangle *rects;
1778 int n;
1780 XRectangle r;
1782 if (n <= 0)
1783 return 0;
1785 if (s->row->full_width_p)
1787 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1788 r.x = WINDOW_LEFT_EDGE_X (s->w);
1789 r.width = WINDOW_TOTAL_WIDTH (s->w);
1791 /* Unless displaying a mode or menu bar line, which are always
1792 fully visible, clip to the visible part of the row. */
1793 if (s->w->pseudo_window_p)
1794 r.height = s->row->visible_height;
1795 else
1796 r.height = s->height;
1798 else
1800 /* This is a text line that may be partially visible. */
1801 r.x = window_box_left (s->w, s->area);
1802 r.width = window_box_width (s->w, s->area);
1803 r.height = s->row->visible_height;
1806 if (s->clip_head)
1807 if (r.x < s->clip_head->x)
1809 if (r.width >= s->clip_head->x - r.x)
1810 r.width -= s->clip_head->x - r.x;
1811 else
1812 r.width = 0;
1813 r.x = s->clip_head->x;
1815 if (s->clip_tail)
1816 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1818 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1819 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1820 else
1821 r.width = 0;
1824 /* If S draws overlapping rows, it's sufficient to use the top and
1825 bottom of the window for clipping because this glyph string
1826 intentionally draws over other lines. */
1827 if (s->for_overlaps)
1829 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1830 r.height = window_text_bottom_y (s->w) - r.y;
1832 /* Alas, the above simple strategy does not work for the
1833 environments with anti-aliased text: if the same text is
1834 drawn onto the same place multiple times, it gets thicker.
1835 If the overlap we are processing is for the erased cursor, we
1836 take the intersection with the rectagle of the cursor. */
1837 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1839 XRectangle rc, r_save = r;
1841 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1842 rc.y = s->w->phys_cursor.y;
1843 rc.width = s->w->phys_cursor_width;
1844 rc.height = s->w->phys_cursor_height;
1846 x_intersect_rectangles (&r_save, &rc, &r);
1849 else
1851 /* Don't use S->y for clipping because it doesn't take partially
1852 visible lines into account. For example, it can be negative for
1853 partially visible lines at the top of a window. */
1854 if (!s->row->full_width_p
1855 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1856 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1857 else
1858 r.y = max (0, s->row->y);
1860 /* If drawing a tool-bar window, draw it over the internal border
1861 at the top of the window. */
1862 if (WINDOWP (s->f->tool_bar_window)
1863 && s->w == XWINDOW (s->f->tool_bar_window))
1864 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1867 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1869 /* If drawing the cursor, don't let glyph draw outside its
1870 advertised boundaries. Cleartype does this under some circumstances. */
1871 if (s->hl == DRAW_CURSOR)
1873 struct glyph *glyph = s->first_glyph;
1874 int height, max_y;
1876 if (s->x > r.x)
1878 r.width -= s->x - r.x;
1879 r.x = s->x;
1881 r.width = min (r.width, glyph->pixel_width);
1883 /* If r.y is below window bottom, ensure that we still see a cursor. */
1884 height = min (glyph->ascent + glyph->descent,
1885 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1886 max_y = window_text_bottom_y (s->w) - height;
1887 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1888 if (s->ybase - glyph->ascent > max_y)
1890 r.y = max_y;
1891 r.height = height;
1893 else
1895 /* Don't draw cursor glyph taller than our actual glyph. */
1896 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1897 if (height < r.height)
1899 max_y = r.y + r.height;
1900 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1901 r.height = min (max_y - r.y, height);
1906 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1907 || (s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1)
1909 #ifdef CONVERT_FROM_XRECT
1910 CONVERT_FROM_XRECT (r, *rects);
1911 #else
1912 *rects = r;
1913 #endif
1914 return 1;
1916 else
1918 /* If we are processing overlapping and allowed to return
1919 multiple clipping rectangles, we exclude the row of the glyph
1920 string from the clipping rectangle. This is to avoid drawing
1921 the same text on the environment with anti-aliasing. */
1922 #ifdef CONVERT_FROM_XRECT
1923 XRectangle rs[2];
1924 #else
1925 XRectangle *rs = rects;
1926 #endif
1927 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1929 if (s->for_overlaps & OVERLAPS_PRED)
1931 rs[i] = r;
1932 if (r.y + r.height > row_y)
1933 if (r.y < row_y)
1934 rs[i].height = row_y - r.y;
1935 else
1936 rs[i].height = 0;
1937 i++;
1939 if (s->for_overlaps & OVERLAPS_SUCC)
1941 rs[i] = r;
1942 if (r.y < row_y + s->row->visible_height)
1943 if (r.y + r.height > row_y + s->row->visible_height)
1945 rs[i].y = row_y + s->row->visible_height;
1946 rs[i].height = r.y + r.height - rs[i].y;
1948 else
1949 rs[i].height = 0;
1950 i++;
1953 n = i;
1954 #ifdef CONVERT_FROM_XRECT
1955 for (i = 0; i < n; i++)
1956 CONVERT_FROM_XRECT (rs[i], rects[i]);
1957 #endif
1958 return n;
1962 /* EXPORT:
1963 Return in *NR the clipping rectangle for glyph string S. */
1965 void
1966 get_glyph_string_clip_rect (s, nr)
1967 struct glyph_string *s;
1968 NativeRectangle *nr;
1970 get_glyph_string_clip_rects (s, nr, 1);
1974 /* EXPORT:
1975 Return the position and height of the phys cursor in window W.
1976 Set w->phys_cursor_width to width of phys cursor.
1980 get_phys_cursor_geometry (w, row, glyph, heightp)
1981 struct window *w;
1982 struct glyph_row *row;
1983 struct glyph *glyph;
1984 int *heightp;
1986 struct frame *f = XFRAME (WINDOW_FRAME (w));
1987 int y, wd, h, h0, y0;
1989 /* Compute the width of the rectangle to draw. If on a stretch
1990 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1991 rectangle as wide as the glyph, but use a canonical character
1992 width instead. */
1993 wd = glyph->pixel_width - 1;
1994 #ifdef HAVE_NTGUI
1995 wd++; /* Why? */
1996 #endif
1997 if (glyph->type == STRETCH_GLYPH
1998 && !x_stretch_cursor_p)
1999 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2000 w->phys_cursor_width = wd;
2002 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2004 /* If y is below window bottom, ensure that we still see a cursor. */
2005 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2007 h = max (h0, glyph->ascent + glyph->descent);
2008 h0 = min (h0, glyph->ascent + glyph->descent);
2010 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2011 if (y < y0)
2013 h = max (h - (y0 - y) + 1, h0);
2014 y = y0 - 1;
2016 else
2018 y0 = window_text_bottom_y (w) - h0;
2019 if (y > y0)
2021 h += y - y0;
2022 y = y0;
2026 *heightp = h - 1;
2027 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2031 * Remember which glyph the mouse is over.
2034 void
2035 remember_mouse_glyph (f, gx, gy, rect)
2036 struct frame *f;
2037 int gx, gy;
2038 NativeRectangle *rect;
2040 Lisp_Object window;
2041 struct window *w;
2042 struct glyph_row *r, *gr, *end_row;
2043 enum window_part part;
2044 enum glyph_row_area area;
2045 int x, y, width, height;
2047 /* Try to determine frame pixel position and size of the glyph under
2048 frame pixel coordinates X/Y on frame F. */
2050 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2051 if (NILP (window))
2053 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2054 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2055 goto virtual_glyph;
2058 w = XWINDOW (window);
2059 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2060 height = WINDOW_FRAME_LINE_HEIGHT (w);
2062 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2063 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2065 if (w->pseudo_window_p)
2067 area = TEXT_AREA;
2068 part = ON_MODE_LINE; /* Don't adjust margin. */
2069 goto text_glyph;
2072 switch (part)
2074 case ON_LEFT_MARGIN:
2075 area = LEFT_MARGIN_AREA;
2076 goto text_glyph;
2078 case ON_RIGHT_MARGIN:
2079 area = RIGHT_MARGIN_AREA;
2080 goto text_glyph;
2082 case ON_HEADER_LINE:
2083 case ON_MODE_LINE:
2084 gr = (part == ON_HEADER_LINE
2085 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2086 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2087 gy = gr->y;
2088 area = TEXT_AREA;
2089 goto text_glyph_row_found;
2091 case ON_TEXT:
2092 area = TEXT_AREA;
2094 text_glyph:
2095 gr = 0; gy = 0;
2096 for (; r <= end_row && r->enabled_p; ++r)
2097 if (r->y + r->height > y)
2099 gr = r; gy = r->y;
2100 break;
2103 text_glyph_row_found:
2104 if (gr && gy <= y)
2106 struct glyph *g = gr->glyphs[area];
2107 struct glyph *end = g + gr->used[area];
2109 height = gr->height;
2110 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2111 if (gx + g->pixel_width > x)
2112 break;
2114 if (g < end)
2116 if (g->type == IMAGE_GLYPH)
2118 /* Don't remember when mouse is over image, as
2119 image may have hot-spots. */
2120 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2121 return;
2123 width = g->pixel_width;
2125 else
2127 /* Use nominal char spacing at end of line. */
2128 x -= gx;
2129 gx += (x / width) * width;
2132 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2133 gx += window_box_left_offset (w, area);
2135 else
2137 /* Use nominal line height at end of window. */
2138 gx = (x / width) * width;
2139 y -= gy;
2140 gy += (y / height) * height;
2142 break;
2144 case ON_LEFT_FRINGE:
2145 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2146 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2147 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2148 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2149 goto row_glyph;
2151 case ON_RIGHT_FRINGE:
2152 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2153 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2154 : window_box_right_offset (w, TEXT_AREA));
2155 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2156 goto row_glyph;
2158 case ON_SCROLL_BAR:
2159 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2161 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2164 : 0)));
2165 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2167 row_glyph:
2168 gr = 0, gy = 0;
2169 for (; r <= end_row && r->enabled_p; ++r)
2170 if (r->y + r->height > y)
2172 gr = r; gy = r->y;
2173 break;
2176 if (gr && gy <= y)
2177 height = gr->height;
2178 else
2180 /* Use nominal line height at end of window. */
2181 y -= gy;
2182 gy += (y / height) * height;
2184 break;
2186 default:
2188 virtual_glyph:
2189 /* If there is no glyph under the mouse, then we divide the screen
2190 into a grid of the smallest glyph in the frame, and use that
2191 as our "glyph". */
2193 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2194 round down even for negative values. */
2195 if (gx < 0)
2196 gx -= width - 1;
2197 if (gy < 0)
2198 gy -= height - 1;
2200 gx = (gx / width) * width;
2201 gy = (gy / height) * height;
2203 goto store_rect;
2206 gx += WINDOW_LEFT_EDGE_X (w);
2207 gy += WINDOW_TOP_EDGE_Y (w);
2209 store_rect:
2210 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2212 /* Visible feedback for debugging. */
2213 #if 0
2214 #if HAVE_X_WINDOWS
2215 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2216 f->output_data.x->normal_gc,
2217 gx, gy, width, height);
2218 #endif
2219 #endif
2223 #endif /* HAVE_WINDOW_SYSTEM */
2226 /***********************************************************************
2227 Lisp form evaluation
2228 ***********************************************************************/
2230 /* Error handler for safe_eval and safe_call. */
2232 static Lisp_Object
2233 safe_eval_handler (arg)
2234 Lisp_Object arg;
2236 add_to_log ("Error during redisplay: %s", arg, Qnil);
2237 return Qnil;
2241 /* Evaluate SEXPR and return the result, or nil if something went
2242 wrong. Prevent redisplay during the evaluation. */
2244 Lisp_Object
2245 safe_eval (sexpr)
2246 Lisp_Object sexpr;
2248 Lisp_Object val;
2250 if (inhibit_eval_during_redisplay)
2251 val = Qnil;
2252 else
2254 int count = SPECPDL_INDEX ();
2255 struct gcpro gcpro1;
2257 GCPRO1 (sexpr);
2258 specbind (Qinhibit_redisplay, Qt);
2259 /* Use Qt to ensure debugger does not run,
2260 so there is no possibility of wanting to redisplay. */
2261 val = internal_condition_case_1 (Feval, sexpr, Qt,
2262 safe_eval_handler);
2263 UNGCPRO;
2264 val = unbind_to (count, val);
2267 return val;
2271 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2272 Return the result, or nil if something went wrong. Prevent
2273 redisplay during the evaluation. */
2275 Lisp_Object
2276 safe_call (nargs, args)
2277 int nargs;
2278 Lisp_Object *args;
2280 Lisp_Object val;
2282 if (inhibit_eval_during_redisplay)
2283 val = Qnil;
2284 else
2286 int count = SPECPDL_INDEX ();
2287 struct gcpro gcpro1;
2289 GCPRO1 (args[0]);
2290 gcpro1.nvars = nargs;
2291 specbind (Qinhibit_redisplay, Qt);
2292 /* Use Qt to ensure debugger does not run,
2293 so there is no possibility of wanting to redisplay. */
2294 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2295 safe_eval_handler);
2296 UNGCPRO;
2297 val = unbind_to (count, val);
2300 return val;
2304 /* Call function FN with one argument ARG.
2305 Return the result, or nil if something went wrong. */
2307 Lisp_Object
2308 safe_call1 (fn, arg)
2309 Lisp_Object fn, arg;
2311 Lisp_Object args[2];
2312 args[0] = fn;
2313 args[1] = arg;
2314 return safe_call (2, args);
2319 /***********************************************************************
2320 Debugging
2321 ***********************************************************************/
2323 #if 0
2325 /* Define CHECK_IT to perform sanity checks on iterators.
2326 This is for debugging. It is too slow to do unconditionally. */
2328 static void
2329 check_it (it)
2330 struct it *it;
2332 if (it->method == GET_FROM_STRING)
2334 xassert (STRINGP (it->string));
2335 xassert (IT_STRING_CHARPOS (*it) >= 0);
2337 else
2339 xassert (IT_STRING_CHARPOS (*it) < 0);
2340 if (it->method == GET_FROM_BUFFER)
2342 /* Check that character and byte positions agree. */
2343 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2347 if (it->dpvec)
2348 xassert (it->current.dpvec_index >= 0);
2349 else
2350 xassert (it->current.dpvec_index < 0);
2353 #define CHECK_IT(IT) check_it ((IT))
2355 #else /* not 0 */
2357 #define CHECK_IT(IT) (void) 0
2359 #endif /* not 0 */
2362 #if GLYPH_DEBUG
2364 /* Check that the window end of window W is what we expect it
2365 to be---the last row in the current matrix displaying text. */
2367 static void
2368 check_window_end (w)
2369 struct window *w;
2371 if (!MINI_WINDOW_P (w)
2372 && !NILP (w->window_end_valid))
2374 struct glyph_row *row;
2375 xassert ((row = MATRIX_ROW (w->current_matrix,
2376 XFASTINT (w->window_end_vpos)),
2377 !row->enabled_p
2378 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2379 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2383 #define CHECK_WINDOW_END(W) check_window_end ((W))
2385 #else /* not GLYPH_DEBUG */
2387 #define CHECK_WINDOW_END(W) (void) 0
2389 #endif /* not GLYPH_DEBUG */
2393 /***********************************************************************
2394 Iterator initialization
2395 ***********************************************************************/
2397 /* Initialize IT for displaying current_buffer in window W, starting
2398 at character position CHARPOS. CHARPOS < 0 means that no buffer
2399 position is specified which is useful when the iterator is assigned
2400 a position later. BYTEPOS is the byte position corresponding to
2401 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2403 If ROW is not null, calls to produce_glyphs with IT as parameter
2404 will produce glyphs in that row.
2406 BASE_FACE_ID is the id of a base face to use. It must be one of
2407 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2408 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2409 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2411 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2412 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2413 will be initialized to use the corresponding mode line glyph row of
2414 the desired matrix of W. */
2416 void
2417 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2418 struct it *it;
2419 struct window *w;
2420 int charpos, bytepos;
2421 struct glyph_row *row;
2422 enum face_id base_face_id;
2424 int highlight_region_p;
2426 /* Some precondition checks. */
2427 xassert (w != NULL && it != NULL);
2428 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2429 && charpos <= ZV));
2431 /* If face attributes have been changed since the last redisplay,
2432 free realized faces now because they depend on face definitions
2433 that might have changed. Don't free faces while there might be
2434 desired matrices pending which reference these faces. */
2435 if (face_change_count && !inhibit_free_realized_faces)
2437 face_change_count = 0;
2438 free_all_realized_faces (Qnil);
2441 /* Use one of the mode line rows of W's desired matrix if
2442 appropriate. */
2443 if (row == NULL)
2445 if (base_face_id == MODE_LINE_FACE_ID
2446 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2447 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2448 else if (base_face_id == HEADER_LINE_FACE_ID)
2449 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2452 /* Clear IT. */
2453 bzero (it, sizeof *it);
2454 it->current.overlay_string_index = -1;
2455 it->current.dpvec_index = -1;
2456 it->base_face_id = base_face_id;
2457 it->string = Qnil;
2458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2460 /* The window in which we iterate over current_buffer: */
2461 XSETWINDOW (it->window, w);
2462 it->w = w;
2463 it->f = XFRAME (w->frame);
2465 /* Extra space between lines (on window systems only). */
2466 if (base_face_id == DEFAULT_FACE_ID
2467 && FRAME_WINDOW_P (it->f))
2469 if (NATNUMP (current_buffer->extra_line_spacing))
2470 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2471 else if (FLOATP (current_buffer->extra_line_spacing))
2472 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2473 * FRAME_LINE_HEIGHT (it->f));
2474 else if (it->f->extra_line_spacing > 0)
2475 it->extra_line_spacing = it->f->extra_line_spacing;
2476 it->max_extra_line_spacing = 0;
2479 /* If realized faces have been removed, e.g. because of face
2480 attribute changes of named faces, recompute them. When running
2481 in batch mode, the face cache of Vterminal_frame is null. If
2482 we happen to get called, make a dummy face cache. */
2483 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2484 init_frame_faces (it->f);
2485 if (FRAME_FACE_CACHE (it->f)->used == 0)
2486 recompute_basic_faces (it->f);
2488 /* Current value of the `slice', `space-width', and 'height' properties. */
2489 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2490 it->space_width = Qnil;
2491 it->font_height = Qnil;
2492 it->override_ascent = -1;
2494 /* Are control characters displayed as `^C'? */
2495 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2497 /* -1 means everything between a CR and the following line end
2498 is invisible. >0 means lines indented more than this value are
2499 invisible. */
2500 it->selective = (INTEGERP (current_buffer->selective_display)
2501 ? XFASTINT (current_buffer->selective_display)
2502 : (!NILP (current_buffer->selective_display)
2503 ? -1 : 0));
2504 it->selective_display_ellipsis_p
2505 = !NILP (current_buffer->selective_display_ellipses);
2507 /* Display table to use. */
2508 it->dp = window_display_table (w);
2510 /* Are multibyte characters enabled in current_buffer? */
2511 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2513 /* Non-zero if we should highlight the region. */
2514 highlight_region_p
2515 = (!NILP (Vtransient_mark_mode)
2516 && !NILP (current_buffer->mark_active)
2517 && XMARKER (current_buffer->mark)->buffer != 0);
2519 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2520 start and end of a visible region in window IT->w. Set both to
2521 -1 to indicate no region. */
2522 if (highlight_region_p
2523 /* Maybe highlight only in selected window. */
2524 && (/* Either show region everywhere. */
2525 highlight_nonselected_windows
2526 /* Or show region in the selected window. */
2527 || w == XWINDOW (selected_window)
2528 /* Or show the region if we are in the mini-buffer and W is
2529 the window the mini-buffer refers to. */
2530 || (MINI_WINDOW_P (XWINDOW (selected_window))
2531 && WINDOWP (minibuf_selected_window)
2532 && w == XWINDOW (minibuf_selected_window))))
2534 int charpos = marker_position (current_buffer->mark);
2535 it->region_beg_charpos = min (PT, charpos);
2536 it->region_end_charpos = max (PT, charpos);
2538 else
2539 it->region_beg_charpos = it->region_end_charpos = -1;
2541 /* Get the position at which the redisplay_end_trigger hook should
2542 be run, if it is to be run at all. */
2543 if (MARKERP (w->redisplay_end_trigger)
2544 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2545 it->redisplay_end_trigger_charpos
2546 = marker_position (w->redisplay_end_trigger);
2547 else if (INTEGERP (w->redisplay_end_trigger))
2548 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2550 /* Correct bogus values of tab_width. */
2551 it->tab_width = XINT (current_buffer->tab_width);
2552 if (it->tab_width <= 0 || it->tab_width > 1000)
2553 it->tab_width = 8;
2555 /* Are lines in the display truncated? */
2556 it->truncate_lines_p
2557 = (base_face_id != DEFAULT_FACE_ID
2558 || XINT (it->w->hscroll)
2559 || (truncate_partial_width_windows
2560 && !WINDOW_FULL_WIDTH_P (it->w))
2561 || !NILP (current_buffer->truncate_lines));
2563 /* Get dimensions of truncation and continuation glyphs. These are
2564 displayed as fringe bitmaps under X, so we don't need them for such
2565 frames. */
2566 if (!FRAME_WINDOW_P (it->f))
2568 if (it->truncate_lines_p)
2570 /* We will need the truncation glyph. */
2571 xassert (it->glyph_row == NULL);
2572 produce_special_glyphs (it, IT_TRUNCATION);
2573 it->truncation_pixel_width = it->pixel_width;
2575 else
2577 /* We will need the continuation glyph. */
2578 xassert (it->glyph_row == NULL);
2579 produce_special_glyphs (it, IT_CONTINUATION);
2580 it->continuation_pixel_width = it->pixel_width;
2583 /* Reset these values to zero because the produce_special_glyphs
2584 above has changed them. */
2585 it->pixel_width = it->ascent = it->descent = 0;
2586 it->phys_ascent = it->phys_descent = 0;
2589 /* Set this after getting the dimensions of truncation and
2590 continuation glyphs, so that we don't produce glyphs when calling
2591 produce_special_glyphs, above. */
2592 it->glyph_row = row;
2593 it->area = TEXT_AREA;
2595 /* Get the dimensions of the display area. The display area
2596 consists of the visible window area plus a horizontally scrolled
2597 part to the left of the window. All x-values are relative to the
2598 start of this total display area. */
2599 if (base_face_id != DEFAULT_FACE_ID)
2601 /* Mode lines, menu bar in terminal frames. */
2602 it->first_visible_x = 0;
2603 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2605 else
2607 it->first_visible_x
2608 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2609 it->last_visible_x = (it->first_visible_x
2610 + window_box_width (w, TEXT_AREA));
2612 /* If we truncate lines, leave room for the truncator glyph(s) at
2613 the right margin. Otherwise, leave room for the continuation
2614 glyph(s). Truncation and continuation glyphs are not inserted
2615 for window-based redisplay. */
2616 if (!FRAME_WINDOW_P (it->f))
2618 if (it->truncate_lines_p)
2619 it->last_visible_x -= it->truncation_pixel_width;
2620 else
2621 it->last_visible_x -= it->continuation_pixel_width;
2624 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2625 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2628 /* Leave room for a border glyph. */
2629 if (!FRAME_WINDOW_P (it->f)
2630 && !WINDOW_RIGHTMOST_P (it->w))
2631 it->last_visible_x -= 1;
2633 it->last_visible_y = window_text_bottom_y (w);
2635 /* For mode lines and alike, arrange for the first glyph having a
2636 left box line if the face specifies a box. */
2637 if (base_face_id != DEFAULT_FACE_ID)
2639 struct face *face;
2641 it->face_id = base_face_id;
2643 /* If we have a boxed mode line, make the first character appear
2644 with a left box line. */
2645 face = FACE_FROM_ID (it->f, base_face_id);
2646 if (face->box != FACE_NO_BOX)
2647 it->start_of_box_run_p = 1;
2650 /* If a buffer position was specified, set the iterator there,
2651 getting overlays and face properties from that position. */
2652 if (charpos >= BUF_BEG (current_buffer))
2654 it->end_charpos = ZV;
2655 it->face_id = -1;
2656 IT_CHARPOS (*it) = charpos;
2658 /* Compute byte position if not specified. */
2659 if (bytepos < charpos)
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2661 else
2662 IT_BYTEPOS (*it) = bytepos;
2664 it->start = it->current;
2666 /* Compute faces etc. */
2667 reseat (it, it->current.pos, 1);
2670 CHECK_IT (it);
2674 /* Initialize IT for the display of window W with window start POS. */
2676 void
2677 start_display (it, w, pos)
2678 struct it *it;
2679 struct window *w;
2680 struct text_pos pos;
2682 struct glyph_row *row;
2683 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2685 row = w->desired_matrix->rows + first_vpos;
2686 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2687 it->first_vpos = first_vpos;
2689 /* Don't reseat to previous visible line start if current start
2690 position is in a string or image. */
2691 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2693 int start_at_line_beg_p;
2694 int first_y = it->current_y;
2696 /* If window start is not at a line start, skip forward to POS to
2697 get the correct continuation lines width. */
2698 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2699 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2700 if (!start_at_line_beg_p)
2702 int new_x;
2704 reseat_at_previous_visible_line_start (it);
2705 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2707 new_x = it->current_x + it->pixel_width;
2709 /* If lines are continued, this line may end in the middle
2710 of a multi-glyph character (e.g. a control character
2711 displayed as \003, or in the middle of an overlay
2712 string). In this case move_it_to above will not have
2713 taken us to the start of the continuation line but to the
2714 end of the continued line. */
2715 if (it->current_x > 0
2716 && !it->truncate_lines_p /* Lines are continued. */
2717 && (/* And glyph doesn't fit on the line. */
2718 new_x > it->last_visible_x
2719 /* Or it fits exactly and we're on a window
2720 system frame. */
2721 || (new_x == it->last_visible_x
2722 && FRAME_WINDOW_P (it->f))))
2724 if (it->current.dpvec_index >= 0
2725 || it->current.overlay_string_index >= 0)
2727 set_iterator_to_next (it, 1);
2728 move_it_in_display_line_to (it, -1, -1, 0);
2731 it->continuation_lines_width += it->current_x;
2734 /* We're starting a new display line, not affected by the
2735 height of the continued line, so clear the appropriate
2736 fields in the iterator structure. */
2737 it->max_ascent = it->max_descent = 0;
2738 it->max_phys_ascent = it->max_phys_descent = 0;
2740 it->current_y = first_y;
2741 it->vpos = 0;
2742 it->current_x = it->hpos = 0;
2746 #if 0 /* Don't assert the following because start_display is sometimes
2747 called intentionally with a window start that is not at a
2748 line start. Please leave this code in as a comment. */
2750 /* Window start should be on a line start, now. */
2751 xassert (it->continuation_lines_width
2752 || IT_CHARPOS (it) == BEGV
2753 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2754 #endif /* 0 */
2758 /* Return 1 if POS is a position in ellipses displayed for invisible
2759 text. W is the window we display, for text property lookup. */
2761 static int
2762 in_ellipses_for_invisible_text_p (pos, w)
2763 struct display_pos *pos;
2764 struct window *w;
2766 Lisp_Object prop, window;
2767 int ellipses_p = 0;
2768 int charpos = CHARPOS (pos->pos);
2770 /* If POS specifies a position in a display vector, this might
2771 be for an ellipsis displayed for invisible text. We won't
2772 get the iterator set up for delivering that ellipsis unless
2773 we make sure that it gets aware of the invisible text. */
2774 if (pos->dpvec_index >= 0
2775 && pos->overlay_string_index < 0
2776 && CHARPOS (pos->string_pos) < 0
2777 && charpos > BEGV
2778 && (XSETWINDOW (window, w),
2779 prop = Fget_char_property (make_number (charpos),
2780 Qinvisible, window),
2781 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2783 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2784 window);
2785 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2788 return ellipses_p;
2792 /* Initialize IT for stepping through current_buffer in window W,
2793 starting at position POS that includes overlay string and display
2794 vector/ control character translation position information. Value
2795 is zero if there are overlay strings with newlines at POS. */
2797 static int
2798 init_from_display_pos (it, w, pos)
2799 struct it *it;
2800 struct window *w;
2801 struct display_pos *pos;
2803 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2804 int i, overlay_strings_with_newlines = 0;
2806 /* If POS specifies a position in a display vector, this might
2807 be for an ellipsis displayed for invisible text. We won't
2808 get the iterator set up for delivering that ellipsis unless
2809 we make sure that it gets aware of the invisible text. */
2810 if (in_ellipses_for_invisible_text_p (pos, w))
2812 --charpos;
2813 bytepos = 0;
2816 /* Keep in mind: the call to reseat in init_iterator skips invisible
2817 text, so we might end up at a position different from POS. This
2818 is only a problem when POS is a row start after a newline and an
2819 overlay starts there with an after-string, and the overlay has an
2820 invisible property. Since we don't skip invisible text in
2821 display_line and elsewhere immediately after consuming the
2822 newline before the row start, such a POS will not be in a string,
2823 but the call to init_iterator below will move us to the
2824 after-string. */
2825 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2827 /* This only scans the current chunk -- it should scan all chunks.
2828 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2829 to 16 in 22.1 to make this a lesser problem. */
2830 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2832 const char *s = SDATA (it->overlay_strings[i]);
2833 const char *e = s + SBYTES (it->overlay_strings[i]);
2835 while (s < e && *s != '\n')
2836 ++s;
2838 if (s < e)
2840 overlay_strings_with_newlines = 1;
2841 break;
2845 /* If position is within an overlay string, set up IT to the right
2846 overlay string. */
2847 if (pos->overlay_string_index >= 0)
2849 int relative_index;
2851 /* If the first overlay string happens to have a `display'
2852 property for an image, the iterator will be set up for that
2853 image, and we have to undo that setup first before we can
2854 correct the overlay string index. */
2855 if (it->method == GET_FROM_IMAGE)
2856 pop_it (it);
2858 /* We already have the first chunk of overlay strings in
2859 IT->overlay_strings. Load more until the one for
2860 pos->overlay_string_index is in IT->overlay_strings. */
2861 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2863 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2864 it->current.overlay_string_index = 0;
2865 while (n--)
2867 load_overlay_strings (it, 0);
2868 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2872 it->current.overlay_string_index = pos->overlay_string_index;
2873 relative_index = (it->current.overlay_string_index
2874 % OVERLAY_STRING_CHUNK_SIZE);
2875 it->string = it->overlay_strings[relative_index];
2876 xassert (STRINGP (it->string));
2877 it->current.string_pos = pos->string_pos;
2878 it->method = GET_FROM_STRING;
2881 #if 0 /* This is bogus because POS not having an overlay string
2882 position does not mean it's after the string. Example: A
2883 line starting with a before-string and initialization of IT
2884 to the previous row's end position. */
2885 else if (it->current.overlay_string_index >= 0)
2887 /* If POS says we're already after an overlay string ending at
2888 POS, make sure to pop the iterator because it will be in
2889 front of that overlay string. When POS is ZV, we've thereby
2890 also ``processed'' overlay strings at ZV. */
2891 while (it->sp)
2892 pop_it (it);
2893 it->current.overlay_string_index = -1;
2894 it->method = GET_FROM_BUFFER;
2895 if (CHARPOS (pos->pos) == ZV)
2896 it->overlay_strings_at_end_processed_p = 1;
2898 #endif /* 0 */
2900 if (CHARPOS (pos->string_pos) >= 0)
2902 /* Recorded position is not in an overlay string, but in another
2903 string. This can only be a string from a `display' property.
2904 IT should already be filled with that string. */
2905 it->current.string_pos = pos->string_pos;
2906 xassert (STRINGP (it->string));
2909 /* Restore position in display vector translations, control
2910 character translations or ellipses. */
2911 if (pos->dpvec_index >= 0)
2913 if (it->dpvec == NULL)
2914 get_next_display_element (it);
2915 xassert (it->dpvec && it->current.dpvec_index == 0);
2916 it->current.dpvec_index = pos->dpvec_index;
2919 CHECK_IT (it);
2920 return !overlay_strings_with_newlines;
2924 /* Initialize IT for stepping through current_buffer in window W
2925 starting at ROW->start. */
2927 static void
2928 init_to_row_start (it, w, row)
2929 struct it *it;
2930 struct window *w;
2931 struct glyph_row *row;
2933 init_from_display_pos (it, w, &row->start);
2934 it->start = row->start;
2935 it->continuation_lines_width = row->continuation_lines_width;
2936 CHECK_IT (it);
2940 /* Initialize IT for stepping through current_buffer in window W
2941 starting in the line following ROW, i.e. starting at ROW->end.
2942 Value is zero if there are overlay strings with newlines at ROW's
2943 end position. */
2945 static int
2946 init_to_row_end (it, w, row)
2947 struct it *it;
2948 struct window *w;
2949 struct glyph_row *row;
2951 int success = 0;
2953 if (init_from_display_pos (it, w, &row->end))
2955 if (row->continued_p)
2956 it->continuation_lines_width
2957 = row->continuation_lines_width + row->pixel_width;
2958 CHECK_IT (it);
2959 success = 1;
2962 return success;
2968 /***********************************************************************
2969 Text properties
2970 ***********************************************************************/
2972 /* Called when IT reaches IT->stop_charpos. Handle text property and
2973 overlay changes. Set IT->stop_charpos to the next position where
2974 to stop. */
2976 static void
2977 handle_stop (it)
2978 struct it *it;
2980 enum prop_handled handled;
2981 int handle_overlay_change_p;
2982 struct props *p;
2984 it->dpvec = NULL;
2985 it->current.dpvec_index = -1;
2986 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2987 it->ignore_overlay_strings_at_pos_p = 0;
2989 /* Use face of preceding text for ellipsis (if invisible) */
2990 if (it->selective_display_ellipsis_p)
2991 it->saved_face_id = it->face_id;
2995 handled = HANDLED_NORMALLY;
2997 /* Call text property handlers. */
2998 for (p = it_props; p->handler; ++p)
3000 handled = p->handler (it);
3002 if (handled == HANDLED_RECOMPUTE_PROPS)
3003 break;
3004 else if (handled == HANDLED_RETURN)
3005 return;
3006 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3007 handle_overlay_change_p = 0;
3010 if (handled != HANDLED_RECOMPUTE_PROPS)
3012 /* Don't check for overlay strings below when set to deliver
3013 characters from a display vector. */
3014 if (it->method == GET_FROM_DISPLAY_VECTOR)
3015 handle_overlay_change_p = 0;
3017 /* Handle overlay changes. */
3018 if (handle_overlay_change_p)
3019 handled = handle_overlay_change (it);
3021 /* Determine where to stop next. */
3022 if (handled == HANDLED_NORMALLY)
3023 compute_stop_pos (it);
3026 while (handled == HANDLED_RECOMPUTE_PROPS);
3030 /* Compute IT->stop_charpos from text property and overlay change
3031 information for IT's current position. */
3033 static void
3034 compute_stop_pos (it)
3035 struct it *it;
3037 register INTERVAL iv, next_iv;
3038 Lisp_Object object, limit, position;
3040 /* If nowhere else, stop at the end. */
3041 it->stop_charpos = it->end_charpos;
3043 if (STRINGP (it->string))
3045 /* Strings are usually short, so don't limit the search for
3046 properties. */
3047 object = it->string;
3048 limit = Qnil;
3049 position = make_number (IT_STRING_CHARPOS (*it));
3051 else
3053 int charpos;
3055 /* If next overlay change is in front of the current stop pos
3056 (which is IT->end_charpos), stop there. Note: value of
3057 next_overlay_change is point-max if no overlay change
3058 follows. */
3059 charpos = next_overlay_change (IT_CHARPOS (*it));
3060 if (charpos < it->stop_charpos)
3061 it->stop_charpos = charpos;
3063 /* If showing the region, we have to stop at the region
3064 start or end because the face might change there. */
3065 if (it->region_beg_charpos > 0)
3067 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3068 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3069 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3070 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3073 /* Set up variables for computing the stop position from text
3074 property changes. */
3075 XSETBUFFER (object, current_buffer);
3076 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3077 position = make_number (IT_CHARPOS (*it));
3081 /* Get the interval containing IT's position. Value is a null
3082 interval if there isn't such an interval. */
3083 iv = validate_interval_range (object, &position, &position, 0);
3084 if (!NULL_INTERVAL_P (iv))
3086 Lisp_Object values_here[LAST_PROP_IDX];
3087 struct props *p;
3089 /* Get properties here. */
3090 for (p = it_props; p->handler; ++p)
3091 values_here[p->idx] = textget (iv->plist, *p->name);
3093 /* Look for an interval following iv that has different
3094 properties. */
3095 for (next_iv = next_interval (iv);
3096 (!NULL_INTERVAL_P (next_iv)
3097 && (NILP (limit)
3098 || XFASTINT (limit) > next_iv->position));
3099 next_iv = next_interval (next_iv))
3101 for (p = it_props; p->handler; ++p)
3103 Lisp_Object new_value;
3105 new_value = textget (next_iv->plist, *p->name);
3106 if (!EQ (values_here[p->idx], new_value))
3107 break;
3110 if (p->handler)
3111 break;
3114 if (!NULL_INTERVAL_P (next_iv))
3116 if (INTEGERP (limit)
3117 && next_iv->position >= XFASTINT (limit))
3118 /* No text property change up to limit. */
3119 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3120 else
3121 /* Text properties change in next_iv. */
3122 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3126 xassert (STRINGP (it->string)
3127 || (it->stop_charpos >= BEGV
3128 && it->stop_charpos >= IT_CHARPOS (*it)));
3132 /* Return the position of the next overlay change after POS in
3133 current_buffer. Value is point-max if no overlay change
3134 follows. This is like `next-overlay-change' but doesn't use
3135 xmalloc. */
3137 static int
3138 next_overlay_change (pos)
3139 int pos;
3141 int noverlays;
3142 int endpos;
3143 Lisp_Object *overlays;
3144 int i;
3146 /* Get all overlays at the given position. */
3147 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3149 /* If any of these overlays ends before endpos,
3150 use its ending point instead. */
3151 for (i = 0; i < noverlays; ++i)
3153 Lisp_Object oend;
3154 int oendpos;
3156 oend = OVERLAY_END (overlays[i]);
3157 oendpos = OVERLAY_POSITION (oend);
3158 endpos = min (endpos, oendpos);
3161 return endpos;
3166 /***********************************************************************
3167 Fontification
3168 ***********************************************************************/
3170 /* Handle changes in the `fontified' property of the current buffer by
3171 calling hook functions from Qfontification_functions to fontify
3172 regions of text. */
3174 static enum prop_handled
3175 handle_fontified_prop (it)
3176 struct it *it;
3178 Lisp_Object prop, pos;
3179 enum prop_handled handled = HANDLED_NORMALLY;
3181 if (!NILP (Vmemory_full))
3182 return handled;
3184 /* Get the value of the `fontified' property at IT's current buffer
3185 position. (The `fontified' property doesn't have a special
3186 meaning in strings.) If the value is nil, call functions from
3187 Qfontification_functions. */
3188 if (!STRINGP (it->string)
3189 && it->s == NULL
3190 && !NILP (Vfontification_functions)
3191 && !NILP (Vrun_hooks)
3192 && (pos = make_number (IT_CHARPOS (*it)),
3193 prop = Fget_char_property (pos, Qfontified, Qnil),
3194 NILP (prop)))
3196 int count = SPECPDL_INDEX ();
3197 Lisp_Object val;
3199 val = Vfontification_functions;
3200 specbind (Qfontification_functions, Qnil);
3202 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3203 safe_call1 (val, pos);
3204 else
3206 Lisp_Object globals, fn;
3207 struct gcpro gcpro1, gcpro2;
3209 globals = Qnil;
3210 GCPRO2 (val, globals);
3212 for (; CONSP (val); val = XCDR (val))
3214 fn = XCAR (val);
3216 if (EQ (fn, Qt))
3218 /* A value of t indicates this hook has a local
3219 binding; it means to run the global binding too.
3220 In a global value, t should not occur. If it
3221 does, we must ignore it to avoid an endless
3222 loop. */
3223 for (globals = Fdefault_value (Qfontification_functions);
3224 CONSP (globals);
3225 globals = XCDR (globals))
3227 fn = XCAR (globals);
3228 if (!EQ (fn, Qt))
3229 safe_call1 (fn, pos);
3232 else
3233 safe_call1 (fn, pos);
3236 UNGCPRO;
3239 unbind_to (count, Qnil);
3241 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3242 something. This avoids an endless loop if they failed to
3243 fontify the text for which reason ever. */
3244 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3245 handled = HANDLED_RECOMPUTE_PROPS;
3248 return handled;
3253 /***********************************************************************
3254 Faces
3255 ***********************************************************************/
3257 /* Set up iterator IT from face properties at its current position.
3258 Called from handle_stop. */
3260 static enum prop_handled
3261 handle_face_prop (it)
3262 struct it *it;
3264 int new_face_id, next_stop;
3266 if (!STRINGP (it->string))
3268 new_face_id
3269 = face_at_buffer_position (it->w,
3270 IT_CHARPOS (*it),
3271 it->region_beg_charpos,
3272 it->region_end_charpos,
3273 &next_stop,
3274 (IT_CHARPOS (*it)
3275 + TEXT_PROP_DISTANCE_LIMIT),
3278 /* Is this a start of a run of characters with box face?
3279 Caveat: this can be called for a freshly initialized
3280 iterator; face_id is -1 in this case. We know that the new
3281 face will not change until limit, i.e. if the new face has a
3282 box, all characters up to limit will have one. But, as
3283 usual, we don't know whether limit is really the end. */
3284 if (new_face_id != it->face_id)
3286 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3288 /* If new face has a box but old face has not, this is
3289 the start of a run of characters with box, i.e. it has
3290 a shadow on the left side. The value of face_id of the
3291 iterator will be -1 if this is the initial call that gets
3292 the face. In this case, we have to look in front of IT's
3293 position and see whether there is a face != new_face_id. */
3294 it->start_of_box_run_p
3295 = (new_face->box != FACE_NO_BOX
3296 && (it->face_id >= 0
3297 || IT_CHARPOS (*it) == BEG
3298 || new_face_id != face_before_it_pos (it)));
3299 it->face_box_p = new_face->box != FACE_NO_BOX;
3302 else
3304 int base_face_id, bufpos;
3306 if (it->current.overlay_string_index >= 0)
3307 bufpos = IT_CHARPOS (*it);
3308 else
3309 bufpos = 0;
3311 /* For strings from a buffer, i.e. overlay strings or strings
3312 from a `display' property, use the face at IT's current
3313 buffer position as the base face to merge with, so that
3314 overlay strings appear in the same face as surrounding
3315 text, unless they specify their own faces. */
3316 base_face_id = underlying_face_id (it);
3318 new_face_id = face_at_string_position (it->w,
3319 it->string,
3320 IT_STRING_CHARPOS (*it),
3321 bufpos,
3322 it->region_beg_charpos,
3323 it->region_end_charpos,
3324 &next_stop,
3325 base_face_id, 0);
3327 #if 0 /* This shouldn't be neccessary. Let's check it. */
3328 /* If IT is used to display a mode line we would really like to
3329 use the mode line face instead of the frame's default face. */
3330 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3331 && new_face_id == DEFAULT_FACE_ID)
3332 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3333 #endif
3335 /* Is this a start of a run of characters with box? Caveat:
3336 this can be called for a freshly allocated iterator; face_id
3337 is -1 is this case. We know that the new face will not
3338 change until the next check pos, i.e. if the new face has a
3339 box, all characters up to that position will have a
3340 box. But, as usual, we don't know whether that position
3341 is really the end. */
3342 if (new_face_id != it->face_id)
3344 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3347 /* If new face has a box but old face hasn't, this is the
3348 start of a run of characters with box, i.e. it has a
3349 shadow on the left side. */
3350 it->start_of_box_run_p
3351 = new_face->box && (old_face == NULL || !old_face->box);
3352 it->face_box_p = new_face->box != FACE_NO_BOX;
3356 it->face_id = new_face_id;
3357 return HANDLED_NORMALLY;
3361 /* Return the ID of the face ``underlying'' IT's current position,
3362 which is in a string. If the iterator is associated with a
3363 buffer, return the face at IT's current buffer position.
3364 Otherwise, use the iterator's base_face_id. */
3366 static int
3367 underlying_face_id (it)
3368 struct it *it;
3370 int face_id = it->base_face_id, i;
3372 xassert (STRINGP (it->string));
3374 for (i = it->sp - 1; i >= 0; --i)
3375 if (NILP (it->stack[i].string))
3376 face_id = it->stack[i].face_id;
3378 return face_id;
3382 /* Compute the face one character before or after the current position
3383 of IT. BEFORE_P non-zero means get the face in front of IT's
3384 position. Value is the id of the face. */
3386 static int
3387 face_before_or_after_it_pos (it, before_p)
3388 struct it *it;
3389 int before_p;
3391 int face_id, limit;
3392 int next_check_charpos;
3393 struct text_pos pos;
3395 xassert (it->s == NULL);
3397 if (STRINGP (it->string))
3399 int bufpos, base_face_id;
3401 /* No face change past the end of the string (for the case
3402 we are padding with spaces). No face change before the
3403 string start. */
3404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3405 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3406 return it->face_id;
3408 /* Set pos to the position before or after IT's current position. */
3409 if (before_p)
3410 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3411 else
3412 /* For composition, we must check the character after the
3413 composition. */
3414 pos = (it->what == IT_COMPOSITION
3415 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3416 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3418 if (it->current.overlay_string_index >= 0)
3419 bufpos = IT_CHARPOS (*it);
3420 else
3421 bufpos = 0;
3423 base_face_id = underlying_face_id (it);
3425 /* Get the face for ASCII, or unibyte. */
3426 face_id = face_at_string_position (it->w,
3427 it->string,
3428 CHARPOS (pos),
3429 bufpos,
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_check_charpos,
3433 base_face_id, 0);
3435 /* Correct the face for charsets different from ASCII. Do it
3436 for the multibyte case only. The face returned above is
3437 suitable for unibyte text if IT->string is unibyte. */
3438 if (STRING_MULTIBYTE (it->string))
3440 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3441 int rest = SBYTES (it->string) - BYTEPOS (pos);
3442 int c, len;
3443 struct face *face = FACE_FROM_ID (it->f, face_id);
3445 c = string_char_and_length (p, rest, &len);
3446 face_id = FACE_FOR_CHAR (it->f, face, c);
3449 else
3451 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3452 || (IT_CHARPOS (*it) <= BEGV && before_p))
3453 return it->face_id;
3455 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3456 pos = it->current.pos;
3458 if (before_p)
3459 DEC_TEXT_POS (pos, it->multibyte_p);
3460 else
3462 if (it->what == IT_COMPOSITION)
3463 /* For composition, we must check the position after the
3464 composition. */
3465 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3466 else
3467 INC_TEXT_POS (pos, it->multibyte_p);
3470 /* Determine face for CHARSET_ASCII, or unibyte. */
3471 face_id = face_at_buffer_position (it->w,
3472 CHARPOS (pos),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_check_charpos,
3476 limit, 0);
3478 /* Correct the face for charsets different from ASCII. Do it
3479 for the multibyte case only. The face returned above is
3480 suitable for unibyte text if current_buffer is unibyte. */
3481 if (it->multibyte_p)
3483 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3484 struct face *face = FACE_FROM_ID (it->f, face_id);
3485 face_id = FACE_FOR_CHAR (it->f, face, c);
3489 return face_id;
3494 /***********************************************************************
3495 Invisible text
3496 ***********************************************************************/
3498 /* Set up iterator IT from invisible properties at its current
3499 position. Called from handle_stop. */
3501 static enum prop_handled
3502 handle_invisible_prop (it)
3503 struct it *it;
3505 enum prop_handled handled = HANDLED_NORMALLY;
3507 if (STRINGP (it->string))
3509 extern Lisp_Object Qinvisible;
3510 Lisp_Object prop, end_charpos, limit, charpos;
3512 /* Get the value of the invisible text property at the
3513 current position. Value will be nil if there is no such
3514 property. */
3515 charpos = make_number (IT_STRING_CHARPOS (*it));
3516 prop = Fget_text_property (charpos, Qinvisible, it->string);
3518 if (!NILP (prop)
3519 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3521 handled = HANDLED_RECOMPUTE_PROPS;
3523 /* Get the position at which the next change of the
3524 invisible text property can be found in IT->string.
3525 Value will be nil if the property value is the same for
3526 all the rest of IT->string. */
3527 XSETINT (limit, SCHARS (it->string));
3528 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3529 it->string, limit);
3531 /* Text at current position is invisible. The next
3532 change in the property is at position end_charpos.
3533 Move IT's current position to that position. */
3534 if (INTEGERP (end_charpos)
3535 && XFASTINT (end_charpos) < XFASTINT (limit))
3537 struct text_pos old;
3538 old = it->current.string_pos;
3539 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3540 compute_string_pos (&it->current.string_pos, old, it->string);
3542 else
3544 /* The rest of the string is invisible. If this is an
3545 overlay string, proceed with the next overlay string
3546 or whatever comes and return a character from there. */
3547 if (it->current.overlay_string_index >= 0)
3549 next_overlay_string (it);
3550 /* Don't check for overlay strings when we just
3551 finished processing them. */
3552 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3554 else
3556 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3557 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3562 else
3564 int invis_p, newpos, next_stop, start_charpos;
3565 Lisp_Object pos, prop, overlay;
3567 /* First of all, is there invisible text at this position? */
3568 start_charpos = IT_CHARPOS (*it);
3569 pos = make_number (IT_CHARPOS (*it));
3570 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3571 &overlay);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3574 /* If we are on invisible text, skip over it. */
3575 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3577 /* Record whether we have to display an ellipsis for the
3578 invisible text. */
3579 int display_ellipsis_p = invis_p == 2;
3581 handled = HANDLED_RECOMPUTE_PROPS;
3583 /* Loop skipping over invisible text. The loop is left at
3584 ZV or with IT on the first char being visible again. */
3587 /* Try to skip some invisible text. Return value is the
3588 position reached which can be equal to IT's position
3589 if there is nothing invisible here. This skips both
3590 over invisible text properties and overlays with
3591 invisible property. */
3592 newpos = skip_invisible (IT_CHARPOS (*it),
3593 &next_stop, ZV, it->window);
3595 /* If we skipped nothing at all we weren't at invisible
3596 text in the first place. If everything to the end of
3597 the buffer was skipped, end the loop. */
3598 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3599 invis_p = 0;
3600 else
3602 /* We skipped some characters but not necessarily
3603 all there are. Check if we ended up on visible
3604 text. Fget_char_property returns the property of
3605 the char before the given position, i.e. if we
3606 get invis_p = 0, this means that the char at
3607 newpos is visible. */
3608 pos = make_number (newpos);
3609 prop = Fget_char_property (pos, Qinvisible, it->window);
3610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3613 /* If we ended up on invisible text, proceed to
3614 skip starting with next_stop. */
3615 if (invis_p)
3616 IT_CHARPOS (*it) = next_stop;
3618 while (invis_p);
3620 /* The position newpos is now either ZV or on visible text. */
3621 IT_CHARPOS (*it) = newpos;
3622 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3624 /* If there are before-strings at the start of invisible
3625 text, and the text is invisible because of a text
3626 property, arrange to show before-strings because 20.x did
3627 it that way. (If the text is invisible because of an
3628 overlay property instead of a text property, this is
3629 already handled in the overlay code.) */
3630 if (NILP (overlay)
3631 && get_overlay_strings (it, start_charpos))
3633 handled = HANDLED_RECOMPUTE_PROPS;
3634 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3636 else if (display_ellipsis_p)
3637 setup_for_ellipsis (it, 0);
3641 return handled;
3645 /* Make iterator IT return `...' next.
3646 Replaces LEN characters from buffer. */
3648 static void
3649 setup_for_ellipsis (it, len)
3650 struct it *it;
3651 int len;
3653 /* Use the display table definition for `...'. Invalid glyphs
3654 will be handled by the method returning elements from dpvec. */
3655 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3657 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3658 it->dpvec = v->contents;
3659 it->dpend = v->contents + v->size;
3661 else
3663 /* Default `...'. */
3664 it->dpvec = default_invis_vector;
3665 it->dpend = default_invis_vector + 3;
3668 it->dpvec_char_len = len;
3669 it->current.dpvec_index = 0;
3670 it->dpvec_face_id = -1;
3672 /* Remember the current face id in case glyphs specify faces.
3673 IT's face is restored in set_iterator_to_next.
3674 saved_face_id was set to preceding char's face in handle_stop. */
3675 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3676 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3678 it->method = GET_FROM_DISPLAY_VECTOR;
3679 it->ellipsis_p = 1;
3684 /***********************************************************************
3685 'display' property
3686 ***********************************************************************/
3688 /* Set up iterator IT from `display' property at its current position.
3689 Called from handle_stop.
3690 We return HANDLED_RETURN if some part of the display property
3691 overrides the display of the buffer text itself.
3692 Otherwise we return HANDLED_NORMALLY. */
3694 static enum prop_handled
3695 handle_display_prop (it)
3696 struct it *it;
3698 Lisp_Object prop, object;
3699 struct text_pos *position;
3700 /* Nonzero if some property replaces the display of the text itself. */
3701 int display_replaced_p = 0;
3703 if (STRINGP (it->string))
3705 object = it->string;
3706 position = &it->current.string_pos;
3708 else
3710 XSETWINDOW (object, it->w);
3711 position = &it->current.pos;
3714 /* Reset those iterator values set from display property values. */
3715 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3716 it->space_width = Qnil;
3717 it->font_height = Qnil;
3718 it->voffset = 0;
3720 /* We don't support recursive `display' properties, i.e. string
3721 values that have a string `display' property, that have a string
3722 `display' property etc. */
3723 if (!it->string_from_display_prop_p)
3724 it->area = TEXT_AREA;
3726 prop = Fget_char_property (make_number (position->charpos),
3727 Qdisplay, object);
3728 if (NILP (prop))
3729 return HANDLED_NORMALLY;
3731 if (!STRINGP (it->string))
3732 object = it->w->buffer;
3734 if (CONSP (prop)
3735 /* Simple properties. */
3736 && !EQ (XCAR (prop), Qimage)
3737 && !EQ (XCAR (prop), Qspace)
3738 && !EQ (XCAR (prop), Qwhen)
3739 && !EQ (XCAR (prop), Qslice)
3740 && !EQ (XCAR (prop), Qspace_width)
3741 && !EQ (XCAR (prop), Qheight)
3742 && !EQ (XCAR (prop), Qraise)
3743 /* Marginal area specifications. */
3744 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3745 && !EQ (XCAR (prop), Qleft_fringe)
3746 && !EQ (XCAR (prop), Qright_fringe)
3747 && !NILP (XCAR (prop)))
3749 for (; CONSP (prop); prop = XCDR (prop))
3751 if (handle_single_display_spec (it, XCAR (prop), object,
3752 position, display_replaced_p))
3753 display_replaced_p = 1;
3756 else if (VECTORP (prop))
3758 int i;
3759 for (i = 0; i < ASIZE (prop); ++i)
3760 if (handle_single_display_spec (it, AREF (prop, i), object,
3761 position, display_replaced_p))
3762 display_replaced_p = 1;
3764 else
3766 int ret = handle_single_display_spec (it, prop, object, position, 0);
3767 if (ret < 0) /* Replaced by "", i.e. nothing. */
3768 return HANDLED_RECOMPUTE_PROPS;
3769 if (ret)
3770 display_replaced_p = 1;
3773 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3777 /* Value is the position of the end of the `display' property starting
3778 at START_POS in OBJECT. */
3780 static struct text_pos
3781 display_prop_end (it, object, start_pos)
3782 struct it *it;
3783 Lisp_Object object;
3784 struct text_pos start_pos;
3786 Lisp_Object end;
3787 struct text_pos end_pos;
3789 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3790 Qdisplay, object, Qnil);
3791 CHARPOS (end_pos) = XFASTINT (end);
3792 if (STRINGP (object))
3793 compute_string_pos (&end_pos, start_pos, it->string);
3794 else
3795 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3797 return end_pos;
3801 /* Set up IT from a single `display' specification PROP. OBJECT
3802 is the object in which the `display' property was found. *POSITION
3803 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3804 means that we previously saw a display specification which already
3805 replaced text display with something else, for example an image;
3806 we ignore such properties after the first one has been processed.
3808 If PROP is a `space' or `image' specification, and in some other
3809 cases too, set *POSITION to the position where the `display'
3810 property ends.
3812 Value is non-zero if something was found which replaces the display
3813 of buffer or string text. Specifically, the value is -1 if that
3814 "something" is "nothing". */
3816 static int
3817 handle_single_display_spec (it, spec, object, position,
3818 display_replaced_before_p)
3819 struct it *it;
3820 Lisp_Object spec;
3821 Lisp_Object object;
3822 struct text_pos *position;
3823 int display_replaced_before_p;
3825 Lisp_Object form;
3826 Lisp_Object location, value;
3827 struct text_pos start_pos;
3828 int valid_p;
3830 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3831 If the result is non-nil, use VALUE instead of SPEC. */
3832 form = Qt;
3833 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3835 spec = XCDR (spec);
3836 if (!CONSP (spec))
3837 return 0;
3838 form = XCAR (spec);
3839 spec = XCDR (spec);
3842 if (!NILP (form) && !EQ (form, Qt))
3844 int count = SPECPDL_INDEX ();
3845 struct gcpro gcpro1;
3847 /* Bind `object' to the object having the `display' property, a
3848 buffer or string. Bind `position' to the position in the
3849 object where the property was found, and `buffer-position'
3850 to the current position in the buffer. */
3851 specbind (Qobject, object);
3852 specbind (Qposition, make_number (CHARPOS (*position)));
3853 specbind (Qbuffer_position,
3854 make_number (STRINGP (object)
3855 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3856 GCPRO1 (form);
3857 form = safe_eval (form);
3858 UNGCPRO;
3859 unbind_to (count, Qnil);
3862 if (NILP (form))
3863 return 0;
3865 /* Handle `(height HEIGHT)' specifications. */
3866 if (CONSP (spec)
3867 && EQ (XCAR (spec), Qheight)
3868 && CONSP (XCDR (spec)))
3870 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3871 return 0;
3873 it->font_height = XCAR (XCDR (spec));
3874 if (!NILP (it->font_height))
3876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3877 int new_height = -1;
3879 if (CONSP (it->font_height)
3880 && (EQ (XCAR (it->font_height), Qplus)
3881 || EQ (XCAR (it->font_height), Qminus))
3882 && CONSP (XCDR (it->font_height))
3883 && INTEGERP (XCAR (XCDR (it->font_height))))
3885 /* `(+ N)' or `(- N)' where N is an integer. */
3886 int steps = XINT (XCAR (XCDR (it->font_height)));
3887 if (EQ (XCAR (it->font_height), Qplus))
3888 steps = - steps;
3889 it->face_id = smaller_face (it->f, it->face_id, steps);
3891 else if (FUNCTIONP (it->font_height))
3893 /* Call function with current height as argument.
3894 Value is the new height. */
3895 Lisp_Object height;
3896 height = safe_call1 (it->font_height,
3897 face->lface[LFACE_HEIGHT_INDEX]);
3898 if (NUMBERP (height))
3899 new_height = XFLOATINT (height);
3901 else if (NUMBERP (it->font_height))
3903 /* Value is a multiple of the canonical char height. */
3904 struct face *face;
3906 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3907 new_height = (XFLOATINT (it->font_height)
3908 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3910 else
3912 /* Evaluate IT->font_height with `height' bound to the
3913 current specified height to get the new height. */
3914 int count = SPECPDL_INDEX ();
3916 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3917 value = safe_eval (it->font_height);
3918 unbind_to (count, Qnil);
3920 if (NUMBERP (value))
3921 new_height = XFLOATINT (value);
3924 if (new_height > 0)
3925 it->face_id = face_with_height (it->f, it->face_id, new_height);
3928 return 0;
3931 /* Handle `(space_width WIDTH)'. */
3932 if (CONSP (spec)
3933 && EQ (XCAR (spec), Qspace_width)
3934 && CONSP (XCDR (spec)))
3936 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3937 return 0;
3939 value = XCAR (XCDR (spec));
3940 if (NUMBERP (value) && XFLOATINT (value) > 0)
3941 it->space_width = value;
3943 return 0;
3946 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3947 if (CONSP (spec)
3948 && EQ (XCAR (spec), Qslice))
3950 Lisp_Object tem;
3952 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3953 return 0;
3955 if (tem = XCDR (spec), CONSP (tem))
3957 it->slice.x = XCAR (tem);
3958 if (tem = XCDR (tem), CONSP (tem))
3960 it->slice.y = XCAR (tem);
3961 if (tem = XCDR (tem), CONSP (tem))
3963 it->slice.width = XCAR (tem);
3964 if (tem = XCDR (tem), CONSP (tem))
3965 it->slice.height = XCAR (tem);
3970 return 0;
3973 /* Handle `(raise FACTOR)'. */
3974 if (CONSP (spec)
3975 && EQ (XCAR (spec), Qraise)
3976 && CONSP (XCDR (spec)))
3978 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3979 return 0;
3981 #ifdef HAVE_WINDOW_SYSTEM
3982 value = XCAR (XCDR (spec));
3983 if (NUMBERP (value))
3985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3986 it->voffset = - (XFLOATINT (value)
3987 * (FONT_HEIGHT (face->font)));
3989 #endif /* HAVE_WINDOW_SYSTEM */
3991 return 0;
3994 /* Don't handle the other kinds of display specifications
3995 inside a string that we got from a `display' property. */
3996 if (it->string_from_display_prop_p)
3997 return 0;
3999 /* Characters having this form of property are not displayed, so
4000 we have to find the end of the property. */
4001 start_pos = *position;
4002 *position = display_prop_end (it, object, start_pos);
4003 value = Qnil;
4005 /* Stop the scan at that end position--we assume that all
4006 text properties change there. */
4007 it->stop_charpos = position->charpos;
4009 /* Handle `(left-fringe BITMAP [FACE])'
4010 and `(right-fringe BITMAP [FACE])'. */
4011 if (CONSP (spec)
4012 && (EQ (XCAR (spec), Qleft_fringe)
4013 || EQ (XCAR (spec), Qright_fringe))
4014 && CONSP (XCDR (spec)))
4016 int face_id = DEFAULT_FACE_ID;
4017 int fringe_bitmap;
4019 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4020 /* If we return here, POSITION has been advanced
4021 across the text with this property. */
4022 return 0;
4024 #ifdef HAVE_WINDOW_SYSTEM
4025 value = XCAR (XCDR (spec));
4026 if (!SYMBOLP (value)
4027 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4028 /* If we return here, POSITION has been advanced
4029 across the text with this property. */
4030 return 0;
4032 if (CONSP (XCDR (XCDR (spec))))
4034 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4035 int face_id2 = lookup_derived_face (it->f, face_name,
4036 'A', FRINGE_FACE_ID, 0);
4037 if (face_id2 >= 0)
4038 face_id = face_id2;
4041 /* Save current settings of IT so that we can restore them
4042 when we are finished with the glyph property value. */
4044 push_it (it);
4046 it->area = TEXT_AREA;
4047 it->what = IT_IMAGE;
4048 it->image_id = -1; /* no image */
4049 it->position = start_pos;
4050 it->object = NILP (object) ? it->w->buffer : object;
4051 it->method = GET_FROM_IMAGE;
4052 it->face_id = face_id;
4054 /* Say that we haven't consumed the characters with
4055 `display' property yet. The call to pop_it in
4056 set_iterator_to_next will clean this up. */
4057 *position = start_pos;
4059 if (EQ (XCAR (spec), Qleft_fringe))
4061 it->left_user_fringe_bitmap = fringe_bitmap;
4062 it->left_user_fringe_face_id = face_id;
4064 else
4066 it->right_user_fringe_bitmap = fringe_bitmap;
4067 it->right_user_fringe_face_id = face_id;
4069 #endif /* HAVE_WINDOW_SYSTEM */
4070 return 1;
4073 /* Prepare to handle `((margin left-margin) ...)',
4074 `((margin right-margin) ...)' and `((margin nil) ...)'
4075 prefixes for display specifications. */
4076 location = Qunbound;
4077 if (CONSP (spec) && CONSP (XCAR (spec)))
4079 Lisp_Object tem;
4081 value = XCDR (spec);
4082 if (CONSP (value))
4083 value = XCAR (value);
4085 tem = XCAR (spec);
4086 if (EQ (XCAR (tem), Qmargin)
4087 && (tem = XCDR (tem),
4088 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4089 (NILP (tem)
4090 || EQ (tem, Qleft_margin)
4091 || EQ (tem, Qright_margin))))
4092 location = tem;
4095 if (EQ (location, Qunbound))
4097 location = Qnil;
4098 value = spec;
4101 /* After this point, VALUE is the property after any
4102 margin prefix has been stripped. It must be a string,
4103 an image specification, or `(space ...)'.
4105 LOCATION specifies where to display: `left-margin',
4106 `right-margin' or nil. */
4108 valid_p = (STRINGP (value)
4109 #ifdef HAVE_WINDOW_SYSTEM
4110 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4111 #endif /* not HAVE_WINDOW_SYSTEM */
4112 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4114 if (valid_p && !display_replaced_before_p)
4116 /* Save current settings of IT so that we can restore them
4117 when we are finished with the glyph property value. */
4118 push_it (it);
4120 if (NILP (location))
4121 it->area = TEXT_AREA;
4122 else if (EQ (location, Qleft_margin))
4123 it->area = LEFT_MARGIN_AREA;
4124 else
4125 it->area = RIGHT_MARGIN_AREA;
4127 if (STRINGP (value))
4129 if (SCHARS (value) == 0)
4131 pop_it (it);
4132 return -1; /* Replaced by "", i.e. nothing. */
4134 it->string = value;
4135 it->multibyte_p = STRING_MULTIBYTE (it->string);
4136 it->current.overlay_string_index = -1;
4137 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4138 it->end_charpos = it->string_nchars = SCHARS (it->string);
4139 it->method = GET_FROM_STRING;
4140 it->stop_charpos = 0;
4141 it->string_from_display_prop_p = 1;
4142 /* Say that we haven't consumed the characters with
4143 `display' property yet. The call to pop_it in
4144 set_iterator_to_next will clean this up. */
4145 *position = start_pos;
4147 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4149 it->method = GET_FROM_STRETCH;
4150 it->object = value;
4151 it->current.pos = it->position = start_pos;
4153 #ifdef HAVE_WINDOW_SYSTEM
4154 else
4156 it->what = IT_IMAGE;
4157 it->image_id = lookup_image (it->f, value);
4158 it->position = start_pos;
4159 it->object = NILP (object) ? it->w->buffer : object;
4160 it->method = GET_FROM_IMAGE;
4162 /* Say that we haven't consumed the characters with
4163 `display' property yet. The call to pop_it in
4164 set_iterator_to_next will clean this up. */
4165 *position = start_pos;
4167 #endif /* HAVE_WINDOW_SYSTEM */
4169 return 1;
4172 /* Invalid property or property not supported. Restore
4173 POSITION to what it was before. */
4174 *position = start_pos;
4175 return 0;
4179 /* Check if SPEC is a display specification value whose text should be
4180 treated as intangible. */
4182 static int
4183 single_display_spec_intangible_p (prop)
4184 Lisp_Object prop;
4186 /* Skip over `when FORM'. */
4187 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4189 prop = XCDR (prop);
4190 if (!CONSP (prop))
4191 return 0;
4192 prop = XCDR (prop);
4195 if (STRINGP (prop))
4196 return 1;
4198 if (!CONSP (prop))
4199 return 0;
4201 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4202 we don't need to treat text as intangible. */
4203 if (EQ (XCAR (prop), Qmargin))
4205 prop = XCDR (prop);
4206 if (!CONSP (prop))
4207 return 0;
4209 prop = XCDR (prop);
4210 if (!CONSP (prop)
4211 || EQ (XCAR (prop), Qleft_margin)
4212 || EQ (XCAR (prop), Qright_margin))
4213 return 0;
4216 return (CONSP (prop)
4217 && (EQ (XCAR (prop), Qimage)
4218 || EQ (XCAR (prop), Qspace)));
4222 /* Check if PROP is a display property value whose text should be
4223 treated as intangible. */
4226 display_prop_intangible_p (prop)
4227 Lisp_Object prop;
4229 if (CONSP (prop)
4230 && CONSP (XCAR (prop))
4231 && !EQ (Qmargin, XCAR (XCAR (prop))))
4233 /* A list of sub-properties. */
4234 while (CONSP (prop))
4236 if (single_display_spec_intangible_p (XCAR (prop)))
4237 return 1;
4238 prop = XCDR (prop);
4241 else if (VECTORP (prop))
4243 /* A vector of sub-properties. */
4244 int i;
4245 for (i = 0; i < ASIZE (prop); ++i)
4246 if (single_display_spec_intangible_p (AREF (prop, i)))
4247 return 1;
4249 else
4250 return single_display_spec_intangible_p (prop);
4252 return 0;
4256 /* Return 1 if PROP is a display sub-property value containing STRING. */
4258 static int
4259 single_display_spec_string_p (prop, string)
4260 Lisp_Object prop, string;
4262 if (EQ (string, prop))
4263 return 1;
4265 /* Skip over `when FORM'. */
4266 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4268 prop = XCDR (prop);
4269 if (!CONSP (prop))
4270 return 0;
4271 prop = XCDR (prop);
4274 if (CONSP (prop))
4275 /* Skip over `margin LOCATION'. */
4276 if (EQ (XCAR (prop), Qmargin))
4278 prop = XCDR (prop);
4279 if (!CONSP (prop))
4280 return 0;
4282 prop = XCDR (prop);
4283 if (!CONSP (prop))
4284 return 0;
4287 return CONSP (prop) && EQ (XCAR (prop), string);
4291 /* Return 1 if STRING appears in the `display' property PROP. */
4293 static int
4294 display_prop_string_p (prop, string)
4295 Lisp_Object prop, string;
4297 if (CONSP (prop)
4298 && CONSP (XCAR (prop))
4299 && !EQ (Qmargin, XCAR (XCAR (prop))))
4301 /* A list of sub-properties. */
4302 while (CONSP (prop))
4304 if (single_display_spec_string_p (XCAR (prop), string))
4305 return 1;
4306 prop = XCDR (prop);
4309 else if (VECTORP (prop))
4311 /* A vector of sub-properties. */
4312 int i;
4313 for (i = 0; i < ASIZE (prop); ++i)
4314 if (single_display_spec_string_p (AREF (prop, i), string))
4315 return 1;
4317 else
4318 return single_display_spec_string_p (prop, string);
4320 return 0;
4324 /* Determine from which buffer position in W's buffer STRING comes
4325 from. AROUND_CHARPOS is an approximate position where it could
4326 be from. Value is the buffer position or 0 if it couldn't be
4327 determined.
4329 W's buffer must be current.
4331 This function is necessary because we don't record buffer positions
4332 in glyphs generated from strings (to keep struct glyph small).
4333 This function may only use code that doesn't eval because it is
4334 called asynchronously from note_mouse_highlight. */
4337 string_buffer_position (w, string, around_charpos)
4338 struct window *w;
4339 Lisp_Object string;
4340 int around_charpos;
4342 Lisp_Object limit, prop, pos;
4343 const int MAX_DISTANCE = 1000;
4344 int found = 0;
4346 pos = make_number (around_charpos);
4347 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4348 while (!found && !EQ (pos, limit))
4350 prop = Fget_char_property (pos, Qdisplay, Qnil);
4351 if (!NILP (prop) && display_prop_string_p (prop, string))
4352 found = 1;
4353 else
4354 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4357 if (!found)
4359 pos = make_number (around_charpos);
4360 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4361 while (!found && !EQ (pos, limit))
4363 prop = Fget_char_property (pos, Qdisplay, Qnil);
4364 if (!NILP (prop) && display_prop_string_p (prop, string))
4365 found = 1;
4366 else
4367 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4368 limit);
4372 return found ? XINT (pos) : 0;
4377 /***********************************************************************
4378 `composition' property
4379 ***********************************************************************/
4381 /* Set up iterator IT from `composition' property at its current
4382 position. Called from handle_stop. */
4384 static enum prop_handled
4385 handle_composition_prop (it)
4386 struct it *it;
4388 Lisp_Object prop, string;
4389 int pos, pos_byte, end;
4390 enum prop_handled handled = HANDLED_NORMALLY;
4392 if (STRINGP (it->string))
4394 pos = IT_STRING_CHARPOS (*it);
4395 pos_byte = IT_STRING_BYTEPOS (*it);
4396 string = it->string;
4398 else
4400 pos = IT_CHARPOS (*it);
4401 pos_byte = IT_BYTEPOS (*it);
4402 string = Qnil;
4405 /* If there's a valid composition and point is not inside of the
4406 composition (in the case that the composition is from the current
4407 buffer), draw a glyph composed from the composition components. */
4408 if (find_composition (pos, -1, &pos, &end, &prop, string)
4409 && COMPOSITION_VALID_P (pos, end, prop)
4410 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4412 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4414 if (id >= 0)
4416 it->method = GET_FROM_COMPOSITION;
4417 it->cmp_id = id;
4418 it->cmp_len = COMPOSITION_LENGTH (prop);
4419 /* For a terminal, draw only the first character of the
4420 components. */
4421 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4422 it->len = (STRINGP (it->string)
4423 ? string_char_to_byte (it->string, end)
4424 : CHAR_TO_BYTE (end)) - pos_byte;
4425 it->stop_charpos = end;
4426 handled = HANDLED_RETURN;
4430 return handled;
4435 /***********************************************************************
4436 Overlay strings
4437 ***********************************************************************/
4439 /* The following structure is used to record overlay strings for
4440 later sorting in load_overlay_strings. */
4442 struct overlay_entry
4444 Lisp_Object overlay;
4445 Lisp_Object string;
4446 int priority;
4447 int after_string_p;
4451 /* Set up iterator IT from overlay strings at its current position.
4452 Called from handle_stop. */
4454 static enum prop_handled
4455 handle_overlay_change (it)
4456 struct it *it;
4458 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4459 return HANDLED_RECOMPUTE_PROPS;
4460 else
4461 return HANDLED_NORMALLY;
4465 /* Set up the next overlay string for delivery by IT, if there is an
4466 overlay string to deliver. Called by set_iterator_to_next when the
4467 end of the current overlay string is reached. If there are more
4468 overlay strings to display, IT->string and
4469 IT->current.overlay_string_index are set appropriately here.
4470 Otherwise IT->string is set to nil. */
4472 static void
4473 next_overlay_string (it)
4474 struct it *it;
4476 ++it->current.overlay_string_index;
4477 if (it->current.overlay_string_index == it->n_overlay_strings)
4479 /* No more overlay strings. Restore IT's settings to what
4480 they were before overlay strings were processed, and
4481 continue to deliver from current_buffer. */
4482 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4484 pop_it (it);
4485 xassert (it->stop_charpos >= BEGV
4486 && it->stop_charpos <= it->end_charpos);
4487 it->string = Qnil;
4488 it->current.overlay_string_index = -1;
4489 SET_TEXT_POS (it->current.string_pos, -1, -1);
4490 it->n_overlay_strings = 0;
4491 it->method = GET_FROM_BUFFER;
4493 /* If we're at the end of the buffer, record that we have
4494 processed the overlay strings there already, so that
4495 next_element_from_buffer doesn't try it again. */
4496 if (IT_CHARPOS (*it) >= it->end_charpos)
4497 it->overlay_strings_at_end_processed_p = 1;
4499 /* If we have to display `...' for invisible text, set
4500 the iterator up for that. */
4501 if (display_ellipsis_p)
4502 setup_for_ellipsis (it, 0);
4504 else
4506 /* There are more overlay strings to process. If
4507 IT->current.overlay_string_index has advanced to a position
4508 where we must load IT->overlay_strings with more strings, do
4509 it. */
4510 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4512 if (it->current.overlay_string_index && i == 0)
4513 load_overlay_strings (it, 0);
4515 /* Initialize IT to deliver display elements from the overlay
4516 string. */
4517 it->string = it->overlay_strings[i];
4518 it->multibyte_p = STRING_MULTIBYTE (it->string);
4519 SET_TEXT_POS (it->current.string_pos, 0, 0);
4520 it->method = GET_FROM_STRING;
4521 it->stop_charpos = 0;
4524 CHECK_IT (it);
4528 /* Compare two overlay_entry structures E1 and E2. Used as a
4529 comparison function for qsort in load_overlay_strings. Overlay
4530 strings for the same position are sorted so that
4532 1. All after-strings come in front of before-strings, except
4533 when they come from the same overlay.
4535 2. Within after-strings, strings are sorted so that overlay strings
4536 from overlays with higher priorities come first.
4538 2. Within before-strings, strings are sorted so that overlay
4539 strings from overlays with higher priorities come last.
4541 Value is analogous to strcmp. */
4544 static int
4545 compare_overlay_entries (e1, e2)
4546 void *e1, *e2;
4548 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4549 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4550 int result;
4552 if (entry1->after_string_p != entry2->after_string_p)
4554 /* Let after-strings appear in front of before-strings if
4555 they come from different overlays. */
4556 if (EQ (entry1->overlay, entry2->overlay))
4557 result = entry1->after_string_p ? 1 : -1;
4558 else
4559 result = entry1->after_string_p ? -1 : 1;
4561 else if (entry1->after_string_p)
4562 /* After-strings sorted in order of decreasing priority. */
4563 result = entry2->priority - entry1->priority;
4564 else
4565 /* Before-strings sorted in order of increasing priority. */
4566 result = entry1->priority - entry2->priority;
4568 return result;
4572 /* Load the vector IT->overlay_strings with overlay strings from IT's
4573 current buffer position, or from CHARPOS if that is > 0. Set
4574 IT->n_overlays to the total number of overlay strings found.
4576 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4577 a time. On entry into load_overlay_strings,
4578 IT->current.overlay_string_index gives the number of overlay
4579 strings that have already been loaded by previous calls to this
4580 function.
4582 IT->add_overlay_start contains an additional overlay start
4583 position to consider for taking overlay strings from, if non-zero.
4584 This position comes into play when the overlay has an `invisible'
4585 property, and both before and after-strings. When we've skipped to
4586 the end of the overlay, because of its `invisible' property, we
4587 nevertheless want its before-string to appear.
4588 IT->add_overlay_start will contain the overlay start position
4589 in this case.
4591 Overlay strings are sorted so that after-string strings come in
4592 front of before-string strings. Within before and after-strings,
4593 strings are sorted by overlay priority. See also function
4594 compare_overlay_entries. */
4596 static void
4597 load_overlay_strings (it, charpos)
4598 struct it *it;
4599 int charpos;
4601 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4602 Lisp_Object overlay, window, str, invisible;
4603 struct Lisp_Overlay *ov;
4604 int start, end;
4605 int size = 20;
4606 int n = 0, i, j, invis_p;
4607 struct overlay_entry *entries
4608 = (struct overlay_entry *) alloca (size * sizeof *entries);
4610 if (charpos <= 0)
4611 charpos = IT_CHARPOS (*it);
4613 /* Append the overlay string STRING of overlay OVERLAY to vector
4614 `entries' which has size `size' and currently contains `n'
4615 elements. AFTER_P non-zero means STRING is an after-string of
4616 OVERLAY. */
4617 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4618 do \
4620 Lisp_Object priority; \
4622 if (n == size) \
4624 int new_size = 2 * size; \
4625 struct overlay_entry *old = entries; \
4626 entries = \
4627 (struct overlay_entry *) alloca (new_size \
4628 * sizeof *entries); \
4629 bcopy (old, entries, size * sizeof *entries); \
4630 size = new_size; \
4633 entries[n].string = (STRING); \
4634 entries[n].overlay = (OVERLAY); \
4635 priority = Foverlay_get ((OVERLAY), Qpriority); \
4636 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4637 entries[n].after_string_p = (AFTER_P); \
4638 ++n; \
4640 while (0)
4642 /* Process overlay before the overlay center. */
4643 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4645 XSETMISC (overlay, ov);
4646 xassert (OVERLAYP (overlay));
4647 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4648 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4650 if (end < charpos)
4651 break;
4653 /* Skip this overlay if it doesn't start or end at IT's current
4654 position. */
4655 if (end != charpos && start != charpos)
4656 continue;
4658 /* Skip this overlay if it doesn't apply to IT->w. */
4659 window = Foverlay_get (overlay, Qwindow);
4660 if (WINDOWP (window) && XWINDOW (window) != it->w)
4661 continue;
4663 /* If the text ``under'' the overlay is invisible, both before-
4664 and after-strings from this overlay are visible; start and
4665 end position are indistinguishable. */
4666 invisible = Foverlay_get (overlay, Qinvisible);
4667 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4669 /* If overlay has a non-empty before-string, record it. */
4670 if ((start == charpos || (end == charpos && invis_p))
4671 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4672 && SCHARS (str))
4673 RECORD_OVERLAY_STRING (overlay, str, 0);
4675 /* If overlay has a non-empty after-string, record it. */
4676 if ((end == charpos || (start == charpos && invis_p))
4677 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4678 && SCHARS (str))
4679 RECORD_OVERLAY_STRING (overlay, str, 1);
4682 /* Process overlays after the overlay center. */
4683 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4685 XSETMISC (overlay, ov);
4686 xassert (OVERLAYP (overlay));
4687 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4688 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4690 if (start > charpos)
4691 break;
4693 /* Skip this overlay if it doesn't start or end at IT's current
4694 position. */
4695 if (end != charpos && start != charpos)
4696 continue;
4698 /* Skip this overlay if it doesn't apply to IT->w. */
4699 window = Foverlay_get (overlay, Qwindow);
4700 if (WINDOWP (window) && XWINDOW (window) != it->w)
4701 continue;
4703 /* If the text ``under'' the overlay is invisible, it has a zero
4704 dimension, and both before- and after-strings apply. */
4705 invisible = Foverlay_get (overlay, Qinvisible);
4706 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4708 /* If overlay has a non-empty before-string, record it. */
4709 if ((start == charpos || (end == charpos && invis_p))
4710 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4711 && SCHARS (str))
4712 RECORD_OVERLAY_STRING (overlay, str, 0);
4714 /* If overlay has a non-empty after-string, record it. */
4715 if ((end == charpos || (start == charpos && invis_p))
4716 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4717 && SCHARS (str))
4718 RECORD_OVERLAY_STRING (overlay, str, 1);
4721 #undef RECORD_OVERLAY_STRING
4723 /* Sort entries. */
4724 if (n > 1)
4725 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4727 /* Record the total number of strings to process. */
4728 it->n_overlay_strings = n;
4730 /* IT->current.overlay_string_index is the number of overlay strings
4731 that have already been consumed by IT. Copy some of the
4732 remaining overlay strings to IT->overlay_strings. */
4733 i = 0;
4734 j = it->current.overlay_string_index;
4735 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4736 it->overlay_strings[i++] = entries[j++].string;
4738 CHECK_IT (it);
4742 /* Get the first chunk of overlay strings at IT's current buffer
4743 position, or at CHARPOS if that is > 0. Value is non-zero if at
4744 least one overlay string was found. */
4746 static int
4747 get_overlay_strings (it, charpos)
4748 struct it *it;
4749 int charpos;
4751 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4752 process. This fills IT->overlay_strings with strings, and sets
4753 IT->n_overlay_strings to the total number of strings to process.
4754 IT->pos.overlay_string_index has to be set temporarily to zero
4755 because load_overlay_strings needs this; it must be set to -1
4756 when no overlay strings are found because a zero value would
4757 indicate a position in the first overlay string. */
4758 it->current.overlay_string_index = 0;
4759 load_overlay_strings (it, charpos);
4761 /* If we found overlay strings, set up IT to deliver display
4762 elements from the first one. Otherwise set up IT to deliver
4763 from current_buffer. */
4764 if (it->n_overlay_strings)
4766 /* Make sure we know settings in current_buffer, so that we can
4767 restore meaningful values when we're done with the overlay
4768 strings. */
4769 compute_stop_pos (it);
4770 xassert (it->face_id >= 0);
4772 /* Save IT's settings. They are restored after all overlay
4773 strings have been processed. */
4774 xassert (it->sp == 0);
4775 push_it (it);
4777 /* Set up IT to deliver display elements from the first overlay
4778 string. */
4779 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4780 it->string = it->overlay_strings[0];
4781 it->stop_charpos = 0;
4782 xassert (STRINGP (it->string));
4783 it->end_charpos = SCHARS (it->string);
4784 it->multibyte_p = STRING_MULTIBYTE (it->string);
4785 it->method = GET_FROM_STRING;
4787 else
4789 it->string = Qnil;
4790 it->current.overlay_string_index = -1;
4791 it->method = GET_FROM_BUFFER;
4794 CHECK_IT (it);
4796 /* Value is non-zero if we found at least one overlay string. */
4797 return STRINGP (it->string);
4802 /***********************************************************************
4803 Saving and restoring state
4804 ***********************************************************************/
4806 /* Save current settings of IT on IT->stack. Called, for example,
4807 before setting up IT for an overlay string, to be able to restore
4808 IT's settings to what they were after the overlay string has been
4809 processed. */
4811 static void
4812 push_it (it)
4813 struct it *it;
4815 struct iterator_stack_entry *p;
4817 xassert (it->sp < 2);
4818 p = it->stack + it->sp;
4820 p->stop_charpos = it->stop_charpos;
4821 xassert (it->face_id >= 0);
4822 p->face_id = it->face_id;
4823 p->string = it->string;
4824 p->pos = it->current;
4825 p->end_charpos = it->end_charpos;
4826 p->string_nchars = it->string_nchars;
4827 p->area = it->area;
4828 p->multibyte_p = it->multibyte_p;
4829 p->slice = it->slice;
4830 p->space_width = it->space_width;
4831 p->font_height = it->font_height;
4832 p->voffset = it->voffset;
4833 p->string_from_display_prop_p = it->string_from_display_prop_p;
4834 p->display_ellipsis_p = 0;
4835 ++it->sp;
4839 /* Restore IT's settings from IT->stack. Called, for example, when no
4840 more overlay strings must be processed, and we return to delivering
4841 display elements from a buffer, or when the end of a string from a
4842 `display' property is reached and we return to delivering display
4843 elements from an overlay string, or from a buffer. */
4845 static void
4846 pop_it (it)
4847 struct it *it;
4849 struct iterator_stack_entry *p;
4851 xassert (it->sp > 0);
4852 --it->sp;
4853 p = it->stack + it->sp;
4854 it->stop_charpos = p->stop_charpos;
4855 it->face_id = p->face_id;
4856 it->string = p->string;
4857 it->current = p->pos;
4858 it->end_charpos = p->end_charpos;
4859 it->string_nchars = p->string_nchars;
4860 it->area = p->area;
4861 it->multibyte_p = p->multibyte_p;
4862 it->slice = p->slice;
4863 it->space_width = p->space_width;
4864 it->font_height = p->font_height;
4865 it->voffset = p->voffset;
4866 it->string_from_display_prop_p = p->string_from_display_prop_p;
4871 /***********************************************************************
4872 Moving over lines
4873 ***********************************************************************/
4875 /* Set IT's current position to the previous line start. */
4877 static void
4878 back_to_previous_line_start (it)
4879 struct it *it;
4881 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4882 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4886 /* Move IT to the next line start.
4888 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4889 we skipped over part of the text (as opposed to moving the iterator
4890 continuously over the text). Otherwise, don't change the value
4891 of *SKIPPED_P.
4893 Newlines may come from buffer text, overlay strings, or strings
4894 displayed via the `display' property. That's the reason we can't
4895 simply use find_next_newline_no_quit.
4897 Note that this function may not skip over invisible text that is so
4898 because of text properties and immediately follows a newline. If
4899 it would, function reseat_at_next_visible_line_start, when called
4900 from set_iterator_to_next, would effectively make invisible
4901 characters following a newline part of the wrong glyph row, which
4902 leads to wrong cursor motion. */
4904 static int
4905 forward_to_next_line_start (it, skipped_p)
4906 struct it *it;
4907 int *skipped_p;
4909 int old_selective, newline_found_p, n;
4910 const int MAX_NEWLINE_DISTANCE = 500;
4912 /* If already on a newline, just consume it to avoid unintended
4913 skipping over invisible text below. */
4914 if (it->what == IT_CHARACTER
4915 && it->c == '\n'
4916 && CHARPOS (it->position) == IT_CHARPOS (*it))
4918 set_iterator_to_next (it, 0);
4919 it->c = 0;
4920 return 1;
4923 /* Don't handle selective display in the following. It's (a)
4924 unnecessary because it's done by the caller, and (b) leads to an
4925 infinite recursion because next_element_from_ellipsis indirectly
4926 calls this function. */
4927 old_selective = it->selective;
4928 it->selective = 0;
4930 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4931 from buffer text. */
4932 for (n = newline_found_p = 0;
4933 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4934 n += STRINGP (it->string) ? 0 : 1)
4936 if (!get_next_display_element (it))
4937 return 0;
4938 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4939 set_iterator_to_next (it, 0);
4942 /* If we didn't find a newline near enough, see if we can use a
4943 short-cut. */
4944 if (!newline_found_p)
4946 int start = IT_CHARPOS (*it);
4947 int limit = find_next_newline_no_quit (start, 1);
4948 Lisp_Object pos;
4950 xassert (!STRINGP (it->string));
4952 /* If there isn't any `display' property in sight, and no
4953 overlays, we can just use the position of the newline in
4954 buffer text. */
4955 if (it->stop_charpos >= limit
4956 || ((pos = Fnext_single_property_change (make_number (start),
4957 Qdisplay,
4958 Qnil, make_number (limit)),
4959 NILP (pos))
4960 && next_overlay_change (start) == ZV))
4962 IT_CHARPOS (*it) = limit;
4963 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4964 *skipped_p = newline_found_p = 1;
4966 else
4968 while (get_next_display_element (it)
4969 && !newline_found_p)
4971 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4972 set_iterator_to_next (it, 0);
4977 it->selective = old_selective;
4978 return newline_found_p;
4982 /* Set IT's current position to the previous visible line start. Skip
4983 invisible text that is so either due to text properties or due to
4984 selective display. Caution: this does not change IT->current_x and
4985 IT->hpos. */
4987 static void
4988 back_to_previous_visible_line_start (it)
4989 struct it *it;
4991 while (IT_CHARPOS (*it) > BEGV)
4993 back_to_previous_line_start (it);
4994 if (IT_CHARPOS (*it) <= BEGV)
4995 break;
4997 /* If selective > 0, then lines indented more than that values
4998 are invisible. */
4999 if (it->selective > 0
5000 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5001 (double) it->selective)) /* iftc */
5002 continue;
5004 /* Check the newline before point for invisibility. */
5006 Lisp_Object prop;
5007 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5008 Qinvisible, it->window);
5009 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5010 continue;
5013 /* If newline has a display property that replaces the newline with something
5014 else (image or text), find start of overlay or interval and continue search
5015 from that point. */
5016 if (IT_CHARPOS (*it) > BEGV)
5018 struct it it2 = *it;
5019 int pos;
5020 int beg, end;
5021 Lisp_Object val, overlay;
5023 pos = --IT_CHARPOS (it2);
5024 --IT_BYTEPOS (it2);
5025 it2.sp = 0;
5026 if (handle_display_prop (&it2) == HANDLED_RETURN
5027 && !NILP (val = get_char_property_and_overlay
5028 (make_number (pos), Qdisplay, Qnil, &overlay))
5029 && (OVERLAYP (overlay)
5030 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5031 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5033 if (beg < BEGV)
5034 beg = BEGV;
5035 IT_CHARPOS (*it) = beg;
5036 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5037 continue;
5041 break;
5044 xassert (IT_CHARPOS (*it) >= BEGV);
5045 xassert (IT_CHARPOS (*it) == BEGV
5046 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5047 CHECK_IT (it);
5051 /* Reseat iterator IT at the previous visible line start. Skip
5052 invisible text that is so either due to text properties or due to
5053 selective display. At the end, update IT's overlay information,
5054 face information etc. */
5056 void
5057 reseat_at_previous_visible_line_start (it)
5058 struct it *it;
5060 back_to_previous_visible_line_start (it);
5061 reseat (it, it->current.pos, 1);
5062 CHECK_IT (it);
5066 /* Reseat iterator IT on the next visible line start in the current
5067 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5068 preceding the line start. Skip over invisible text that is so
5069 because of selective display. Compute faces, overlays etc at the
5070 new position. Note that this function does not skip over text that
5071 is invisible because of text properties. */
5073 static void
5074 reseat_at_next_visible_line_start (it, on_newline_p)
5075 struct it *it;
5076 int on_newline_p;
5078 int newline_found_p, skipped_p = 0;
5080 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5082 /* Skip over lines that are invisible because they are indented
5083 more than the value of IT->selective. */
5084 if (it->selective > 0)
5085 while (IT_CHARPOS (*it) < ZV
5086 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5087 (double) it->selective)) /* iftc */
5089 xassert (IT_BYTEPOS (*it) == BEGV
5090 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5091 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5094 /* Position on the newline if that's what's requested. */
5095 if (on_newline_p && newline_found_p)
5097 if (STRINGP (it->string))
5099 if (IT_STRING_CHARPOS (*it) > 0)
5101 --IT_STRING_CHARPOS (*it);
5102 --IT_STRING_BYTEPOS (*it);
5105 else if (IT_CHARPOS (*it) > BEGV)
5107 --IT_CHARPOS (*it);
5108 --IT_BYTEPOS (*it);
5109 reseat (it, it->current.pos, 0);
5112 else if (skipped_p)
5113 reseat (it, it->current.pos, 0);
5115 CHECK_IT (it);
5120 /***********************************************************************
5121 Changing an iterator's position
5122 ***********************************************************************/
5124 /* Change IT's current position to POS in current_buffer. If FORCE_P
5125 is non-zero, always check for text properties at the new position.
5126 Otherwise, text properties are only looked up if POS >=
5127 IT->check_charpos of a property. */
5129 static void
5130 reseat (it, pos, force_p)
5131 struct it *it;
5132 struct text_pos pos;
5133 int force_p;
5135 int original_pos = IT_CHARPOS (*it);
5137 reseat_1 (it, pos, 0);
5139 /* Determine where to check text properties. Avoid doing it
5140 where possible because text property lookup is very expensive. */
5141 if (force_p
5142 || CHARPOS (pos) > it->stop_charpos
5143 || CHARPOS (pos) < original_pos)
5144 handle_stop (it);
5146 CHECK_IT (it);
5150 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5151 IT->stop_pos to POS, also. */
5153 static void
5154 reseat_1 (it, pos, set_stop_p)
5155 struct it *it;
5156 struct text_pos pos;
5157 int set_stop_p;
5159 /* Don't call this function when scanning a C string. */
5160 xassert (it->s == NULL);
5162 /* POS must be a reasonable value. */
5163 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5165 it->current.pos = it->position = pos;
5166 XSETBUFFER (it->object, current_buffer);
5167 it->end_charpos = ZV;
5168 it->dpvec = NULL;
5169 it->current.dpvec_index = -1;
5170 it->current.overlay_string_index = -1;
5171 IT_STRING_CHARPOS (*it) = -1;
5172 IT_STRING_BYTEPOS (*it) = -1;
5173 it->string = Qnil;
5174 it->method = GET_FROM_BUFFER;
5175 /* RMS: I added this to fix a bug in move_it_vertically_backward
5176 where it->area continued to relate to the starting point
5177 for the backward motion. Bug report from
5178 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5179 However, I am not sure whether reseat still does the right thing
5180 in general after this change. */
5181 it->area = TEXT_AREA;
5182 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5183 it->sp = 0;
5184 it->face_before_selective_p = 0;
5186 if (set_stop_p)
5187 it->stop_charpos = CHARPOS (pos);
5191 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5192 If S is non-null, it is a C string to iterate over. Otherwise,
5193 STRING gives a Lisp string to iterate over.
5195 If PRECISION > 0, don't return more then PRECISION number of
5196 characters from the string.
5198 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5199 characters have been returned. FIELD_WIDTH < 0 means an infinite
5200 field width.
5202 MULTIBYTE = 0 means disable processing of multibyte characters,
5203 MULTIBYTE > 0 means enable it,
5204 MULTIBYTE < 0 means use IT->multibyte_p.
5206 IT must be initialized via a prior call to init_iterator before
5207 calling this function. */
5209 static void
5210 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5211 struct it *it;
5212 unsigned char *s;
5213 Lisp_Object string;
5214 int charpos;
5215 int precision, field_width, multibyte;
5217 /* No region in strings. */
5218 it->region_beg_charpos = it->region_end_charpos = -1;
5220 /* No text property checks performed by default, but see below. */
5221 it->stop_charpos = -1;
5223 /* Set iterator position and end position. */
5224 bzero (&it->current, sizeof it->current);
5225 it->current.overlay_string_index = -1;
5226 it->current.dpvec_index = -1;
5227 xassert (charpos >= 0);
5229 /* If STRING is specified, use its multibyteness, otherwise use the
5230 setting of MULTIBYTE, if specified. */
5231 if (multibyte >= 0)
5232 it->multibyte_p = multibyte > 0;
5234 if (s == NULL)
5236 xassert (STRINGP (string));
5237 it->string = string;
5238 it->s = NULL;
5239 it->end_charpos = it->string_nchars = SCHARS (string);
5240 it->method = GET_FROM_STRING;
5241 it->current.string_pos = string_pos (charpos, string);
5243 else
5245 it->s = s;
5246 it->string = Qnil;
5248 /* Note that we use IT->current.pos, not it->current.string_pos,
5249 for displaying C strings. */
5250 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5251 if (it->multibyte_p)
5253 it->current.pos = c_string_pos (charpos, s, 1);
5254 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5256 else
5258 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5259 it->end_charpos = it->string_nchars = strlen (s);
5262 it->method = GET_FROM_C_STRING;
5265 /* PRECISION > 0 means don't return more than PRECISION characters
5266 from the string. */
5267 if (precision > 0 && it->end_charpos - charpos > precision)
5268 it->end_charpos = it->string_nchars = charpos + precision;
5270 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5271 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5272 FIELD_WIDTH < 0 means infinite field width. This is useful for
5273 padding with `-' at the end of a mode line. */
5274 if (field_width < 0)
5275 field_width = INFINITY;
5276 if (field_width > it->end_charpos - charpos)
5277 it->end_charpos = charpos + field_width;
5279 /* Use the standard display table for displaying strings. */
5280 if (DISP_TABLE_P (Vstandard_display_table))
5281 it->dp = XCHAR_TABLE (Vstandard_display_table);
5283 it->stop_charpos = charpos;
5284 CHECK_IT (it);
5289 /***********************************************************************
5290 Iteration
5291 ***********************************************************************/
5293 /* Map enum it_method value to corresponding next_element_from_* function. */
5295 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5297 next_element_from_buffer,
5298 next_element_from_display_vector,
5299 next_element_from_composition,
5300 next_element_from_string,
5301 next_element_from_c_string,
5302 next_element_from_image,
5303 next_element_from_stretch
5307 /* Load IT's display element fields with information about the next
5308 display element from the current position of IT. Value is zero if
5309 end of buffer (or C string) is reached. */
5312 get_next_display_element (it)
5313 struct it *it;
5315 /* Non-zero means that we found a display element. Zero means that
5316 we hit the end of what we iterate over. Performance note: the
5317 function pointer `method' used here turns out to be faster than
5318 using a sequence of if-statements. */
5319 int success_p;
5321 get_next:
5322 success_p = (*get_next_element[it->method]) (it);
5324 if (it->what == IT_CHARACTER)
5326 /* Map via display table or translate control characters.
5327 IT->c, IT->len etc. have been set to the next character by
5328 the function call above. If we have a display table, and it
5329 contains an entry for IT->c, translate it. Don't do this if
5330 IT->c itself comes from a display table, otherwise we could
5331 end up in an infinite recursion. (An alternative could be to
5332 count the recursion depth of this function and signal an
5333 error when a certain maximum depth is reached.) Is it worth
5334 it? */
5335 if (success_p && it->dpvec == NULL)
5337 Lisp_Object dv;
5339 if (it->dp
5340 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5341 VECTORP (dv)))
5343 struct Lisp_Vector *v = XVECTOR (dv);
5345 /* Return the first character from the display table
5346 entry, if not empty. If empty, don't display the
5347 current character. */
5348 if (v->size)
5350 it->dpvec_char_len = it->len;
5351 it->dpvec = v->contents;
5352 it->dpend = v->contents + v->size;
5353 it->current.dpvec_index = 0;
5354 it->dpvec_face_id = -1;
5355 it->saved_face_id = it->face_id;
5356 it->method = GET_FROM_DISPLAY_VECTOR;
5357 it->ellipsis_p = 0;
5359 else
5361 set_iterator_to_next (it, 0);
5363 goto get_next;
5366 /* Translate control characters into `\003' or `^C' form.
5367 Control characters coming from a display table entry are
5368 currently not translated because we use IT->dpvec to hold
5369 the translation. This could easily be changed but I
5370 don't believe that it is worth doing.
5372 If it->multibyte_p is nonzero, eight-bit characters and
5373 non-printable multibyte characters are also translated to
5374 octal form.
5376 If it->multibyte_p is zero, eight-bit characters that
5377 don't have corresponding multibyte char code are also
5378 translated to octal form. */
5379 else if ((it->c < ' '
5380 && (it->area != TEXT_AREA
5381 /* In mode line, treat \n like other crl chars. */
5382 || (it->c != '\t'
5383 && it->glyph_row && it->glyph_row->mode_line_p)
5384 || (it->c != '\n' && it->c != '\t')))
5385 || (it->multibyte_p
5386 ? ((it->c >= 127
5387 && it->len == 1)
5388 || !CHAR_PRINTABLE_P (it->c)
5389 || (!NILP (Vnobreak_char_display)
5390 && (it->c == 0x8a0 || it->c == 0x8ad
5391 || it->c == 0x920 || it->c == 0x92d
5392 || it->c == 0xe20 || it->c == 0xe2d
5393 || it->c == 0xf20 || it->c == 0xf2d)))
5394 : (it->c >= 127
5395 && (!unibyte_display_via_language_environment
5396 || it->c == unibyte_char_to_multibyte (it->c)))))
5398 /* IT->c is a control character which must be displayed
5399 either as '\003' or as `^C' where the '\\' and '^'
5400 can be defined in the display table. Fill
5401 IT->ctl_chars with glyphs for what we have to
5402 display. Then, set IT->dpvec to these glyphs. */
5403 GLYPH g;
5404 int ctl_len;
5405 int face_id, lface_id = 0 ;
5406 GLYPH escape_glyph;
5408 /* Handle control characters with ^. */
5410 if (it->c < 128 && it->ctl_arrow_p)
5412 g = '^'; /* default glyph for Control */
5413 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5414 if (it->dp
5415 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5416 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5418 g = XINT (DISP_CTRL_GLYPH (it->dp));
5419 lface_id = FAST_GLYPH_FACE (g);
5421 if (lface_id)
5423 g = FAST_GLYPH_CHAR (g);
5424 face_id = merge_faces (it->f, Qt, lface_id,
5425 it->face_id);
5427 else
5429 /* Merge the escape-glyph face into the current face. */
5430 face_id = merge_faces (it->f, Qescape_glyph, 0,
5431 it->face_id);
5434 XSETINT (it->ctl_chars[0], g);
5435 g = it->c ^ 0100;
5436 XSETINT (it->ctl_chars[1], g);
5437 ctl_len = 2;
5438 goto display_control;
5441 /* Handle non-break space in the mode where it only gets
5442 highlighting. */
5444 if (EQ (Vnobreak_char_display, Qt)
5445 && (it->c == 0x8a0 || it->c == 0x920
5446 || it->c == 0xe20 || it->c == 0xf20))
5448 /* Merge the no-break-space face into the current face. */
5449 face_id = merge_faces (it->f, Qnobreak_space, 0,
5450 it->face_id);
5452 g = it->c = ' ';
5453 XSETINT (it->ctl_chars[0], g);
5454 ctl_len = 1;
5455 goto display_control;
5458 /* Handle sequences that start with the "escape glyph". */
5460 /* the default escape glyph is \. */
5461 escape_glyph = '\\';
5463 if (it->dp
5464 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5465 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5467 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5468 lface_id = FAST_GLYPH_FACE (escape_glyph);
5470 if (lface_id)
5472 /* The display table specified a face.
5473 Merge it into face_id and also into escape_glyph. */
5474 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5475 face_id = merge_faces (it->f, Qt, lface_id,
5476 it->face_id);
5478 else
5480 /* Merge the escape-glyph face into the current face. */
5481 face_id = merge_faces (it->f, Qescape_glyph, 0,
5482 it->face_id);
5485 /* Handle soft hyphens in the mode where they only get
5486 highlighting. */
5488 if (EQ (Vnobreak_char_display, Qt)
5489 && (it->c == 0x8ad || it->c == 0x92d
5490 || it->c == 0xe2d || it->c == 0xf2d))
5492 g = it->c = '-';
5493 XSETINT (it->ctl_chars[0], g);
5494 ctl_len = 1;
5495 goto display_control;
5498 /* Handle non-break space and soft hyphen
5499 with the escape glyph. */
5501 if (it->c == 0x8a0 || it->c == 0x8ad
5502 || it->c == 0x920 || it->c == 0x92d
5503 || it->c == 0xe20 || it->c == 0xe2d
5504 || it->c == 0xf20 || it->c == 0xf2d)
5506 XSETINT (it->ctl_chars[0], escape_glyph);
5507 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5508 XSETINT (it->ctl_chars[1], g);
5509 ctl_len = 2;
5510 goto display_control;
5514 unsigned char str[MAX_MULTIBYTE_LENGTH];
5515 int len;
5516 int i;
5518 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5519 if (SINGLE_BYTE_CHAR_P (it->c))
5520 str[0] = it->c, len = 1;
5521 else
5523 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5524 if (len < 0)
5526 /* It's an invalid character, which shouldn't
5527 happen actually, but due to bugs it may
5528 happen. Let's print the char as is, there's
5529 not much meaningful we can do with it. */
5530 str[0] = it->c;
5531 str[1] = it->c >> 8;
5532 str[2] = it->c >> 16;
5533 str[3] = it->c >> 24;
5534 len = 4;
5538 for (i = 0; i < len; i++)
5540 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5541 /* Insert three more glyphs into IT->ctl_chars for
5542 the octal display of the character. */
5543 g = ((str[i] >> 6) & 7) + '0';
5544 XSETINT (it->ctl_chars[i * 4 + 1], g);
5545 g = ((str[i] >> 3) & 7) + '0';
5546 XSETINT (it->ctl_chars[i * 4 + 2], g);
5547 g = (str[i] & 7) + '0';
5548 XSETINT (it->ctl_chars[i * 4 + 3], g);
5550 ctl_len = len * 4;
5553 display_control:
5554 /* Set up IT->dpvec and return first character from it. */
5555 it->dpvec_char_len = it->len;
5556 it->dpvec = it->ctl_chars;
5557 it->dpend = it->dpvec + ctl_len;
5558 it->current.dpvec_index = 0;
5559 it->dpvec_face_id = face_id;
5560 it->saved_face_id = it->face_id;
5561 it->method = GET_FROM_DISPLAY_VECTOR;
5562 it->ellipsis_p = 0;
5563 goto get_next;
5567 /* Adjust face id for a multibyte character. There are no
5568 multibyte character in unibyte text. */
5569 if (it->multibyte_p
5570 && success_p
5571 && FRAME_WINDOW_P (it->f))
5573 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5574 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5578 /* Is this character the last one of a run of characters with
5579 box? If yes, set IT->end_of_box_run_p to 1. */
5580 if (it->face_box_p
5581 && it->s == NULL)
5583 int face_id;
5584 struct face *face;
5586 it->end_of_box_run_p
5587 = ((face_id = face_after_it_pos (it),
5588 face_id != it->face_id)
5589 && (face = FACE_FROM_ID (it->f, face_id),
5590 face->box == FACE_NO_BOX));
5593 /* Value is 0 if end of buffer or string reached. */
5594 return success_p;
5598 /* Move IT to the next display element.
5600 RESEAT_P non-zero means if called on a newline in buffer text,
5601 skip to the next visible line start.
5603 Functions get_next_display_element and set_iterator_to_next are
5604 separate because I find this arrangement easier to handle than a
5605 get_next_display_element function that also increments IT's
5606 position. The way it is we can first look at an iterator's current
5607 display element, decide whether it fits on a line, and if it does,
5608 increment the iterator position. The other way around we probably
5609 would either need a flag indicating whether the iterator has to be
5610 incremented the next time, or we would have to implement a
5611 decrement position function which would not be easy to write. */
5613 void
5614 set_iterator_to_next (it, reseat_p)
5615 struct it *it;
5616 int reseat_p;
5618 /* Reset flags indicating start and end of a sequence of characters
5619 with box. Reset them at the start of this function because
5620 moving the iterator to a new position might set them. */
5621 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5623 switch (it->method)
5625 case GET_FROM_BUFFER:
5626 /* The current display element of IT is a character from
5627 current_buffer. Advance in the buffer, and maybe skip over
5628 invisible lines that are so because of selective display. */
5629 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5630 reseat_at_next_visible_line_start (it, 0);
5631 else
5633 xassert (it->len != 0);
5634 IT_BYTEPOS (*it) += it->len;
5635 IT_CHARPOS (*it) += 1;
5636 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5638 break;
5640 case GET_FROM_COMPOSITION:
5641 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5642 if (STRINGP (it->string))
5644 IT_STRING_BYTEPOS (*it) += it->len;
5645 IT_STRING_CHARPOS (*it) += it->cmp_len;
5646 it->method = GET_FROM_STRING;
5647 goto consider_string_end;
5649 else
5651 IT_BYTEPOS (*it) += it->len;
5652 IT_CHARPOS (*it) += it->cmp_len;
5653 it->method = GET_FROM_BUFFER;
5655 break;
5657 case GET_FROM_C_STRING:
5658 /* Current display element of IT is from a C string. */
5659 IT_BYTEPOS (*it) += it->len;
5660 IT_CHARPOS (*it) += 1;
5661 break;
5663 case GET_FROM_DISPLAY_VECTOR:
5664 /* Current display element of IT is from a display table entry.
5665 Advance in the display table definition. Reset it to null if
5666 end reached, and continue with characters from buffers/
5667 strings. */
5668 ++it->current.dpvec_index;
5670 /* Restore face of the iterator to what they were before the
5671 display vector entry (these entries may contain faces). */
5672 it->face_id = it->saved_face_id;
5674 if (it->dpvec + it->current.dpvec_index == it->dpend)
5676 if (it->s)
5677 it->method = GET_FROM_C_STRING;
5678 else if (STRINGP (it->string))
5679 it->method = GET_FROM_STRING;
5680 else
5681 it->method = GET_FROM_BUFFER;
5683 it->dpvec = NULL;
5684 it->current.dpvec_index = -1;
5686 /* Skip over characters which were displayed via IT->dpvec. */
5687 if (it->dpvec_char_len < 0)
5688 reseat_at_next_visible_line_start (it, 1);
5689 else if (it->dpvec_char_len > 0)
5691 if (it->method == GET_FROM_STRING
5692 && it->n_overlay_strings > 0)
5693 it->ignore_overlay_strings_at_pos_p = 1;
5694 it->len = it->dpvec_char_len;
5695 set_iterator_to_next (it, reseat_p);
5698 /* Recheck faces after display vector */
5699 it->stop_charpos = IT_CHARPOS (*it);
5701 break;
5703 case GET_FROM_STRING:
5704 /* Current display element is a character from a Lisp string. */
5705 xassert (it->s == NULL && STRINGP (it->string));
5706 IT_STRING_BYTEPOS (*it) += it->len;
5707 IT_STRING_CHARPOS (*it) += 1;
5709 consider_string_end:
5711 if (it->current.overlay_string_index >= 0)
5713 /* IT->string is an overlay string. Advance to the
5714 next, if there is one. */
5715 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5716 next_overlay_string (it);
5718 else
5720 /* IT->string is not an overlay string. If we reached
5721 its end, and there is something on IT->stack, proceed
5722 with what is on the stack. This can be either another
5723 string, this time an overlay string, or a buffer. */
5724 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5725 && it->sp > 0)
5727 pop_it (it);
5728 if (STRINGP (it->string))
5729 goto consider_string_end;
5730 it->method = GET_FROM_BUFFER;
5733 break;
5735 case GET_FROM_IMAGE:
5736 case GET_FROM_STRETCH:
5737 /* The position etc with which we have to proceed are on
5738 the stack. The position may be at the end of a string,
5739 if the `display' property takes up the whole string. */
5740 xassert (it->sp > 0);
5741 pop_it (it);
5742 it->image_id = 0;
5743 if (STRINGP (it->string))
5745 it->method = GET_FROM_STRING;
5746 goto consider_string_end;
5748 it->method = GET_FROM_BUFFER;
5749 break;
5751 default:
5752 /* There are no other methods defined, so this should be a bug. */
5753 abort ();
5756 xassert (it->method != GET_FROM_STRING
5757 || (STRINGP (it->string)
5758 && IT_STRING_CHARPOS (*it) >= 0));
5761 /* Load IT's display element fields with information about the next
5762 display element which comes from a display table entry or from the
5763 result of translating a control character to one of the forms `^C'
5764 or `\003'.
5766 IT->dpvec holds the glyphs to return as characters.
5767 IT->saved_face_id holds the face id before the display vector--
5768 it is restored into IT->face_idin set_iterator_to_next. */
5770 static int
5771 next_element_from_display_vector (it)
5772 struct it *it;
5774 /* Precondition. */
5775 xassert (it->dpvec && it->current.dpvec_index >= 0);
5777 it->face_id = it->saved_face_id;
5779 if (INTEGERP (*it->dpvec)
5780 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5782 GLYPH g;
5784 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5785 it->c = FAST_GLYPH_CHAR (g);
5786 it->len = CHAR_BYTES (it->c);
5788 /* The entry may contain a face id to use. Such a face id is
5789 the id of a Lisp face, not a realized face. A face id of
5790 zero means no face is specified. */
5791 if (it->dpvec_face_id >= 0)
5792 it->face_id = it->dpvec_face_id;
5793 else
5795 int lface_id = FAST_GLYPH_FACE (g);
5796 if (lface_id > 0)
5797 it->face_id = merge_faces (it->f, Qt, lface_id,
5798 it->saved_face_id);
5801 else
5802 /* Display table entry is invalid. Return a space. */
5803 it->c = ' ', it->len = 1;
5805 /* Don't change position and object of the iterator here. They are
5806 still the values of the character that had this display table
5807 entry or was translated, and that's what we want. */
5808 it->what = IT_CHARACTER;
5809 return 1;
5813 /* Load IT with the next display element from Lisp string IT->string.
5814 IT->current.string_pos is the current position within the string.
5815 If IT->current.overlay_string_index >= 0, the Lisp string is an
5816 overlay string. */
5818 static int
5819 next_element_from_string (it)
5820 struct it *it;
5822 struct text_pos position;
5824 xassert (STRINGP (it->string));
5825 xassert (IT_STRING_CHARPOS (*it) >= 0);
5826 position = it->current.string_pos;
5828 /* Time to check for invisible text? */
5829 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5830 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5832 handle_stop (it);
5834 /* Since a handler may have changed IT->method, we must
5835 recurse here. */
5836 return get_next_display_element (it);
5839 if (it->current.overlay_string_index >= 0)
5841 /* Get the next character from an overlay string. In overlay
5842 strings, There is no field width or padding with spaces to
5843 do. */
5844 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5846 it->what = IT_EOB;
5847 return 0;
5849 else if (STRING_MULTIBYTE (it->string))
5851 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5852 const unsigned char *s = (SDATA (it->string)
5853 + IT_STRING_BYTEPOS (*it));
5854 it->c = string_char_and_length (s, remaining, &it->len);
5856 else
5858 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5859 it->len = 1;
5862 else
5864 /* Get the next character from a Lisp string that is not an
5865 overlay string. Such strings come from the mode line, for
5866 example. We may have to pad with spaces, or truncate the
5867 string. See also next_element_from_c_string. */
5868 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5870 it->what = IT_EOB;
5871 return 0;
5873 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5875 /* Pad with spaces. */
5876 it->c = ' ', it->len = 1;
5877 CHARPOS (position) = BYTEPOS (position) = -1;
5879 else if (STRING_MULTIBYTE (it->string))
5881 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5882 const unsigned char *s = (SDATA (it->string)
5883 + IT_STRING_BYTEPOS (*it));
5884 it->c = string_char_and_length (s, maxlen, &it->len);
5886 else
5888 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5889 it->len = 1;
5893 /* Record what we have and where it came from. Note that we store a
5894 buffer position in IT->position although it could arguably be a
5895 string position. */
5896 it->what = IT_CHARACTER;
5897 it->object = it->string;
5898 it->position = position;
5899 return 1;
5903 /* Load IT with next display element from C string IT->s.
5904 IT->string_nchars is the maximum number of characters to return
5905 from the string. IT->end_charpos may be greater than
5906 IT->string_nchars when this function is called, in which case we
5907 may have to return padding spaces. Value is zero if end of string
5908 reached, including padding spaces. */
5910 static int
5911 next_element_from_c_string (it)
5912 struct it *it;
5914 int success_p = 1;
5916 xassert (it->s);
5917 it->what = IT_CHARACTER;
5918 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5919 it->object = Qnil;
5921 /* IT's position can be greater IT->string_nchars in case a field
5922 width or precision has been specified when the iterator was
5923 initialized. */
5924 if (IT_CHARPOS (*it) >= it->end_charpos)
5926 /* End of the game. */
5927 it->what = IT_EOB;
5928 success_p = 0;
5930 else if (IT_CHARPOS (*it) >= it->string_nchars)
5932 /* Pad with spaces. */
5933 it->c = ' ', it->len = 1;
5934 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5936 else if (it->multibyte_p)
5938 /* Implementation note: The calls to strlen apparently aren't a
5939 performance problem because there is no noticeable performance
5940 difference between Emacs running in unibyte or multibyte mode. */
5941 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5942 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5943 maxlen, &it->len);
5945 else
5946 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5948 return success_p;
5952 /* Set up IT to return characters from an ellipsis, if appropriate.
5953 The definition of the ellipsis glyphs may come from a display table
5954 entry. This function Fills IT with the first glyph from the
5955 ellipsis if an ellipsis is to be displayed. */
5957 static int
5958 next_element_from_ellipsis (it)
5959 struct it *it;
5961 if (it->selective_display_ellipsis_p)
5962 setup_for_ellipsis (it, it->len);
5963 else
5965 /* The face at the current position may be different from the
5966 face we find after the invisible text. Remember what it
5967 was in IT->saved_face_id, and signal that it's there by
5968 setting face_before_selective_p. */
5969 it->saved_face_id = it->face_id;
5970 it->method = GET_FROM_BUFFER;
5971 reseat_at_next_visible_line_start (it, 1);
5972 it->face_before_selective_p = 1;
5975 return get_next_display_element (it);
5979 /* Deliver an image display element. The iterator IT is already
5980 filled with image information (done in handle_display_prop). Value
5981 is always 1. */
5984 static int
5985 next_element_from_image (it)
5986 struct it *it;
5988 it->what = IT_IMAGE;
5989 return 1;
5993 /* Fill iterator IT with next display element from a stretch glyph
5994 property. IT->object is the value of the text property. Value is
5995 always 1. */
5997 static int
5998 next_element_from_stretch (it)
5999 struct it *it;
6001 it->what = IT_STRETCH;
6002 return 1;
6006 /* Load IT with the next display element from current_buffer. Value
6007 is zero if end of buffer reached. IT->stop_charpos is the next
6008 position at which to stop and check for text properties or buffer
6009 end. */
6011 static int
6012 next_element_from_buffer (it)
6013 struct it *it;
6015 int success_p = 1;
6017 /* Check this assumption, otherwise, we would never enter the
6018 if-statement, below. */
6019 xassert (IT_CHARPOS (*it) >= BEGV
6020 && IT_CHARPOS (*it) <= it->stop_charpos);
6022 if (IT_CHARPOS (*it) >= it->stop_charpos)
6024 if (IT_CHARPOS (*it) >= it->end_charpos)
6026 int overlay_strings_follow_p;
6028 /* End of the game, except when overlay strings follow that
6029 haven't been returned yet. */
6030 if (it->overlay_strings_at_end_processed_p)
6031 overlay_strings_follow_p = 0;
6032 else
6034 it->overlay_strings_at_end_processed_p = 1;
6035 overlay_strings_follow_p = get_overlay_strings (it, 0);
6038 if (overlay_strings_follow_p)
6039 success_p = get_next_display_element (it);
6040 else
6042 it->what = IT_EOB;
6043 it->position = it->current.pos;
6044 success_p = 0;
6047 else
6049 handle_stop (it);
6050 return get_next_display_element (it);
6053 else
6055 /* No face changes, overlays etc. in sight, so just return a
6056 character from current_buffer. */
6057 unsigned char *p;
6059 /* Maybe run the redisplay end trigger hook. Performance note:
6060 This doesn't seem to cost measurable time. */
6061 if (it->redisplay_end_trigger_charpos
6062 && it->glyph_row
6063 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6064 run_redisplay_end_trigger_hook (it);
6066 /* Get the next character, maybe multibyte. */
6067 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6068 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6070 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6071 - IT_BYTEPOS (*it));
6072 it->c = string_char_and_length (p, maxlen, &it->len);
6074 else
6075 it->c = *p, it->len = 1;
6077 /* Record what we have and where it came from. */
6078 it->what = IT_CHARACTER;;
6079 it->object = it->w->buffer;
6080 it->position = it->current.pos;
6082 /* Normally we return the character found above, except when we
6083 really want to return an ellipsis for selective display. */
6084 if (it->selective)
6086 if (it->c == '\n')
6088 /* A value of selective > 0 means hide lines indented more
6089 than that number of columns. */
6090 if (it->selective > 0
6091 && IT_CHARPOS (*it) + 1 < ZV
6092 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6093 IT_BYTEPOS (*it) + 1,
6094 (double) it->selective)) /* iftc */
6096 success_p = next_element_from_ellipsis (it);
6097 it->dpvec_char_len = -1;
6100 else if (it->c == '\r' && it->selective == -1)
6102 /* A value of selective == -1 means that everything from the
6103 CR to the end of the line is invisible, with maybe an
6104 ellipsis displayed for it. */
6105 success_p = next_element_from_ellipsis (it);
6106 it->dpvec_char_len = -1;
6111 /* Value is zero if end of buffer reached. */
6112 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6113 return success_p;
6117 /* Run the redisplay end trigger hook for IT. */
6119 static void
6120 run_redisplay_end_trigger_hook (it)
6121 struct it *it;
6123 Lisp_Object args[3];
6125 /* IT->glyph_row should be non-null, i.e. we should be actually
6126 displaying something, or otherwise we should not run the hook. */
6127 xassert (it->glyph_row);
6129 /* Set up hook arguments. */
6130 args[0] = Qredisplay_end_trigger_functions;
6131 args[1] = it->window;
6132 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6133 it->redisplay_end_trigger_charpos = 0;
6135 /* Since we are *trying* to run these functions, don't try to run
6136 them again, even if they get an error. */
6137 it->w->redisplay_end_trigger = Qnil;
6138 Frun_hook_with_args (3, args);
6140 /* Notice if it changed the face of the character we are on. */
6141 handle_face_prop (it);
6145 /* Deliver a composition display element. The iterator IT is already
6146 filled with composition information (done in
6147 handle_composition_prop). Value is always 1. */
6149 static int
6150 next_element_from_composition (it)
6151 struct it *it;
6153 it->what = IT_COMPOSITION;
6154 it->position = (STRINGP (it->string)
6155 ? it->current.string_pos
6156 : it->current.pos);
6157 return 1;
6162 /***********************************************************************
6163 Moving an iterator without producing glyphs
6164 ***********************************************************************/
6166 /* Check if iterator is at a position corresponding to a valid buffer
6167 position after some move_it_ call. */
6169 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6170 ((it)->method == GET_FROM_STRING \
6171 ? IT_STRING_CHARPOS (*it) == 0 \
6172 : 1)
6175 /* Move iterator IT to a specified buffer or X position within one
6176 line on the display without producing glyphs.
6178 OP should be a bit mask including some or all of these bits:
6179 MOVE_TO_X: Stop on reaching x-position TO_X.
6180 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6181 Regardless of OP's value, stop in reaching the end of the display line.
6183 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6184 This means, in particular, that TO_X includes window's horizontal
6185 scroll amount.
6187 The return value has several possible values that
6188 say what condition caused the scan to stop:
6190 MOVE_POS_MATCH_OR_ZV
6191 - when TO_POS or ZV was reached.
6193 MOVE_X_REACHED
6194 -when TO_X was reached before TO_POS or ZV were reached.
6196 MOVE_LINE_CONTINUED
6197 - when we reached the end of the display area and the line must
6198 be continued.
6200 MOVE_LINE_TRUNCATED
6201 - when we reached the end of the display area and the line is
6202 truncated.
6204 MOVE_NEWLINE_OR_CR
6205 - when we stopped at a line end, i.e. a newline or a CR and selective
6206 display is on. */
6208 static enum move_it_result
6209 move_it_in_display_line_to (it, to_charpos, to_x, op)
6210 struct it *it;
6211 int to_charpos, to_x, op;
6213 enum move_it_result result = MOVE_UNDEFINED;
6214 struct glyph_row *saved_glyph_row;
6216 /* Don't produce glyphs in produce_glyphs. */
6217 saved_glyph_row = it->glyph_row;
6218 it->glyph_row = NULL;
6220 #define BUFFER_POS_REACHED_P() \
6221 ((op & MOVE_TO_POS) != 0 \
6222 && BUFFERP (it->object) \
6223 && IT_CHARPOS (*it) >= to_charpos \
6224 && (it->method == GET_FROM_BUFFER \
6225 || (it->method == GET_FROM_DISPLAY_VECTOR \
6226 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6229 while (1)
6231 int x, i, ascent = 0, descent = 0;
6233 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6234 if ((op & MOVE_TO_POS) != 0
6235 && BUFFERP (it->object)
6236 && it->method == GET_FROM_BUFFER
6237 && IT_CHARPOS (*it) > to_charpos)
6239 result = MOVE_POS_MATCH_OR_ZV;
6240 break;
6243 /* Stop when ZV reached.
6244 We used to stop here when TO_CHARPOS reached as well, but that is
6245 too soon if this glyph does not fit on this line. So we handle it
6246 explicitly below. */
6247 if (!get_next_display_element (it)
6248 || (it->truncate_lines_p
6249 && BUFFER_POS_REACHED_P ()))
6251 result = MOVE_POS_MATCH_OR_ZV;
6252 break;
6255 /* The call to produce_glyphs will get the metrics of the
6256 display element IT is loaded with. We record in x the
6257 x-position before this display element in case it does not
6258 fit on the line. */
6259 x = it->current_x;
6261 /* Remember the line height so far in case the next element doesn't
6262 fit on the line. */
6263 if (!it->truncate_lines_p)
6265 ascent = it->max_ascent;
6266 descent = it->max_descent;
6269 PRODUCE_GLYPHS (it);
6271 if (it->area != TEXT_AREA)
6273 set_iterator_to_next (it, 1);
6274 continue;
6277 /* The number of glyphs we get back in IT->nglyphs will normally
6278 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6279 character on a terminal frame, or (iii) a line end. For the
6280 second case, IT->nglyphs - 1 padding glyphs will be present
6281 (on X frames, there is only one glyph produced for a
6282 composite character.
6284 The behavior implemented below means, for continuation lines,
6285 that as many spaces of a TAB as fit on the current line are
6286 displayed there. For terminal frames, as many glyphs of a
6287 multi-glyph character are displayed in the current line, too.
6288 This is what the old redisplay code did, and we keep it that
6289 way. Under X, the whole shape of a complex character must
6290 fit on the line or it will be completely displayed in the
6291 next line.
6293 Note that both for tabs and padding glyphs, all glyphs have
6294 the same width. */
6295 if (it->nglyphs)
6297 /* More than one glyph or glyph doesn't fit on line. All
6298 glyphs have the same width. */
6299 int single_glyph_width = it->pixel_width / it->nglyphs;
6300 int new_x;
6301 int x_before_this_char = x;
6302 int hpos_before_this_char = it->hpos;
6304 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6306 new_x = x + single_glyph_width;
6308 /* We want to leave anything reaching TO_X to the caller. */
6309 if ((op & MOVE_TO_X) && new_x > to_x)
6311 if (BUFFER_POS_REACHED_P ())
6312 goto buffer_pos_reached;
6313 it->current_x = x;
6314 result = MOVE_X_REACHED;
6315 break;
6317 else if (/* Lines are continued. */
6318 !it->truncate_lines_p
6319 && (/* And glyph doesn't fit on the line. */
6320 new_x > it->last_visible_x
6321 /* Or it fits exactly and we're on a window
6322 system frame. */
6323 || (new_x == it->last_visible_x
6324 && FRAME_WINDOW_P (it->f))))
6326 if (/* IT->hpos == 0 means the very first glyph
6327 doesn't fit on the line, e.g. a wide image. */
6328 it->hpos == 0
6329 || (new_x == it->last_visible_x
6330 && FRAME_WINDOW_P (it->f)))
6332 ++it->hpos;
6333 it->current_x = new_x;
6335 /* The character's last glyph just barely fits
6336 in this row. */
6337 if (i == it->nglyphs - 1)
6339 /* If this is the destination position,
6340 return a position *before* it in this row,
6341 now that we know it fits in this row. */
6342 if (BUFFER_POS_REACHED_P ())
6344 it->hpos = hpos_before_this_char;
6345 it->current_x = x_before_this_char;
6346 result = MOVE_POS_MATCH_OR_ZV;
6347 break;
6350 set_iterator_to_next (it, 1);
6351 #ifdef HAVE_WINDOW_SYSTEM
6352 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6354 if (!get_next_display_element (it))
6356 result = MOVE_POS_MATCH_OR_ZV;
6357 break;
6359 if (BUFFER_POS_REACHED_P ())
6361 if (ITERATOR_AT_END_OF_LINE_P (it))
6362 result = MOVE_POS_MATCH_OR_ZV;
6363 else
6364 result = MOVE_LINE_CONTINUED;
6365 break;
6367 if (ITERATOR_AT_END_OF_LINE_P (it))
6369 result = MOVE_NEWLINE_OR_CR;
6370 break;
6373 #endif /* HAVE_WINDOW_SYSTEM */
6376 else
6378 it->current_x = x;
6379 it->max_ascent = ascent;
6380 it->max_descent = descent;
6383 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6384 IT_CHARPOS (*it)));
6385 result = MOVE_LINE_CONTINUED;
6386 break;
6388 else if (BUFFER_POS_REACHED_P ())
6389 goto buffer_pos_reached;
6390 else if (new_x > it->first_visible_x)
6392 /* Glyph is visible. Increment number of glyphs that
6393 would be displayed. */
6394 ++it->hpos;
6396 else
6398 /* Glyph is completely off the left margin of the display
6399 area. Nothing to do. */
6403 if (result != MOVE_UNDEFINED)
6404 break;
6406 else if (BUFFER_POS_REACHED_P ())
6408 buffer_pos_reached:
6409 it->current_x = x;
6410 it->max_ascent = ascent;
6411 it->max_descent = descent;
6412 result = MOVE_POS_MATCH_OR_ZV;
6413 break;
6415 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6417 /* Stop when TO_X specified and reached. This check is
6418 necessary here because of lines consisting of a line end,
6419 only. The line end will not produce any glyphs and we
6420 would never get MOVE_X_REACHED. */
6421 xassert (it->nglyphs == 0);
6422 result = MOVE_X_REACHED;
6423 break;
6426 /* Is this a line end? If yes, we're done. */
6427 if (ITERATOR_AT_END_OF_LINE_P (it))
6429 result = MOVE_NEWLINE_OR_CR;
6430 break;
6433 /* The current display element has been consumed. Advance
6434 to the next. */
6435 set_iterator_to_next (it, 1);
6437 /* Stop if lines are truncated and IT's current x-position is
6438 past the right edge of the window now. */
6439 if (it->truncate_lines_p
6440 && it->current_x >= it->last_visible_x)
6442 #ifdef HAVE_WINDOW_SYSTEM
6443 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6445 if (!get_next_display_element (it)
6446 || BUFFER_POS_REACHED_P ())
6448 result = MOVE_POS_MATCH_OR_ZV;
6449 break;
6451 if (ITERATOR_AT_END_OF_LINE_P (it))
6453 result = MOVE_NEWLINE_OR_CR;
6454 break;
6457 #endif /* HAVE_WINDOW_SYSTEM */
6458 result = MOVE_LINE_TRUNCATED;
6459 break;
6463 #undef BUFFER_POS_REACHED_P
6465 /* Restore the iterator settings altered at the beginning of this
6466 function. */
6467 it->glyph_row = saved_glyph_row;
6468 return result;
6472 /* Move IT forward until it satisfies one or more of the criteria in
6473 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6475 OP is a bit-mask that specifies where to stop, and in particular,
6476 which of those four position arguments makes a difference. See the
6477 description of enum move_operation_enum.
6479 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6480 screen line, this function will set IT to the next position >
6481 TO_CHARPOS. */
6483 void
6484 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6485 struct it *it;
6486 int to_charpos, to_x, to_y, to_vpos;
6487 int op;
6489 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6490 int line_height;
6491 int reached = 0;
6493 for (;;)
6495 if (op & MOVE_TO_VPOS)
6497 /* If no TO_CHARPOS and no TO_X specified, stop at the
6498 start of the line TO_VPOS. */
6499 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6501 if (it->vpos == to_vpos)
6503 reached = 1;
6504 break;
6506 else
6507 skip = move_it_in_display_line_to (it, -1, -1, 0);
6509 else
6511 /* TO_VPOS >= 0 means stop at TO_X in the line at
6512 TO_VPOS, or at TO_POS, whichever comes first. */
6513 if (it->vpos == to_vpos)
6515 reached = 2;
6516 break;
6519 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6521 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6523 reached = 3;
6524 break;
6526 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6528 /* We have reached TO_X but not in the line we want. */
6529 skip = move_it_in_display_line_to (it, to_charpos,
6530 -1, MOVE_TO_POS);
6531 if (skip == MOVE_POS_MATCH_OR_ZV)
6533 reached = 4;
6534 break;
6539 else if (op & MOVE_TO_Y)
6541 struct it it_backup;
6543 /* TO_Y specified means stop at TO_X in the line containing
6544 TO_Y---or at TO_CHARPOS if this is reached first. The
6545 problem is that we can't really tell whether the line
6546 contains TO_Y before we have completely scanned it, and
6547 this may skip past TO_X. What we do is to first scan to
6548 TO_X.
6550 If TO_X is not specified, use a TO_X of zero. The reason
6551 is to make the outcome of this function more predictable.
6552 If we didn't use TO_X == 0, we would stop at the end of
6553 the line which is probably not what a caller would expect
6554 to happen. */
6555 skip = move_it_in_display_line_to (it, to_charpos,
6556 ((op & MOVE_TO_X)
6557 ? to_x : 0),
6558 (MOVE_TO_X
6559 | (op & MOVE_TO_POS)));
6561 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6562 if (skip == MOVE_POS_MATCH_OR_ZV)
6564 reached = 5;
6565 break;
6568 /* If TO_X was reached, we would like to know whether TO_Y
6569 is in the line. This can only be said if we know the
6570 total line height which requires us to scan the rest of
6571 the line. */
6572 if (skip == MOVE_X_REACHED)
6574 it_backup = *it;
6575 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6576 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6577 op & MOVE_TO_POS);
6578 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6581 /* Now, decide whether TO_Y is in this line. */
6582 line_height = it->max_ascent + it->max_descent;
6583 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6585 if (to_y >= it->current_y
6586 && to_y < it->current_y + line_height)
6588 if (skip == MOVE_X_REACHED)
6589 /* If TO_Y is in this line and TO_X was reached above,
6590 we scanned too far. We have to restore IT's settings
6591 to the ones before skipping. */
6592 *it = it_backup;
6593 reached = 6;
6595 else if (skip == MOVE_X_REACHED)
6597 skip = skip2;
6598 if (skip == MOVE_POS_MATCH_OR_ZV)
6599 reached = 7;
6602 if (reached)
6603 break;
6605 else
6606 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6608 switch (skip)
6610 case MOVE_POS_MATCH_OR_ZV:
6611 reached = 8;
6612 goto out;
6614 case MOVE_NEWLINE_OR_CR:
6615 set_iterator_to_next (it, 1);
6616 it->continuation_lines_width = 0;
6617 break;
6619 case MOVE_LINE_TRUNCATED:
6620 it->continuation_lines_width = 0;
6621 reseat_at_next_visible_line_start (it, 0);
6622 if ((op & MOVE_TO_POS) != 0
6623 && IT_CHARPOS (*it) > to_charpos)
6625 reached = 9;
6626 goto out;
6628 break;
6630 case MOVE_LINE_CONTINUED:
6631 it->continuation_lines_width += it->current_x;
6632 break;
6634 default:
6635 abort ();
6638 /* Reset/increment for the next run. */
6639 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6640 it->current_x = it->hpos = 0;
6641 it->current_y += it->max_ascent + it->max_descent;
6642 ++it->vpos;
6643 last_height = it->max_ascent + it->max_descent;
6644 last_max_ascent = it->max_ascent;
6645 it->max_ascent = it->max_descent = 0;
6648 out:
6650 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6654 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6656 If DY > 0, move IT backward at least that many pixels. DY = 0
6657 means move IT backward to the preceding line start or BEGV. This
6658 function may move over more than DY pixels if IT->current_y - DY
6659 ends up in the middle of a line; in this case IT->current_y will be
6660 set to the top of the line moved to. */
6662 void
6663 move_it_vertically_backward (it, dy)
6664 struct it *it;
6665 int dy;
6667 int nlines, h;
6668 struct it it2, it3;
6669 int start_pos;
6671 move_further_back:
6672 xassert (dy >= 0);
6674 start_pos = IT_CHARPOS (*it);
6676 /* Estimate how many newlines we must move back. */
6677 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6679 /* Set the iterator's position that many lines back. */
6680 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6681 back_to_previous_visible_line_start (it);
6683 /* Reseat the iterator here. When moving backward, we don't want
6684 reseat to skip forward over invisible text, set up the iterator
6685 to deliver from overlay strings at the new position etc. So,
6686 use reseat_1 here. */
6687 reseat_1 (it, it->current.pos, 1);
6689 /* We are now surely at a line start. */
6690 it->current_x = it->hpos = 0;
6691 it->continuation_lines_width = 0;
6693 /* Move forward and see what y-distance we moved. First move to the
6694 start of the next line so that we get its height. We need this
6695 height to be able to tell whether we reached the specified
6696 y-distance. */
6697 it2 = *it;
6698 it2.max_ascent = it2.max_descent = 0;
6701 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6702 MOVE_TO_POS | MOVE_TO_VPOS);
6704 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6705 xassert (IT_CHARPOS (*it) >= BEGV);
6706 it3 = it2;
6708 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6709 xassert (IT_CHARPOS (*it) >= BEGV);
6710 /* H is the actual vertical distance from the position in *IT
6711 and the starting position. */
6712 h = it2.current_y - it->current_y;
6713 /* NLINES is the distance in number of lines. */
6714 nlines = it2.vpos - it->vpos;
6716 /* Correct IT's y and vpos position
6717 so that they are relative to the starting point. */
6718 it->vpos -= nlines;
6719 it->current_y -= h;
6721 if (dy == 0)
6723 /* DY == 0 means move to the start of the screen line. The
6724 value of nlines is > 0 if continuation lines were involved. */
6725 if (nlines > 0)
6726 move_it_by_lines (it, nlines, 1);
6727 #if 0
6728 /* I think this assert is bogus if buffer contains
6729 invisible text or images. KFS. */
6730 xassert (IT_CHARPOS (*it) <= start_pos);
6731 #endif
6733 else
6735 /* The y-position we try to reach, relative to *IT.
6736 Note that H has been subtracted in front of the if-statement. */
6737 int target_y = it->current_y + h - dy;
6738 int y0 = it3.current_y;
6739 int y1 = line_bottom_y (&it3);
6740 int line_height = y1 - y0;
6742 /* If we did not reach target_y, try to move further backward if
6743 we can. If we moved too far backward, try to move forward. */
6744 if (target_y < it->current_y
6745 /* This is heuristic. In a window that's 3 lines high, with
6746 a line height of 13 pixels each, recentering with point
6747 on the bottom line will try to move -39/2 = 19 pixels
6748 backward. Try to avoid moving into the first line. */
6749 && (it->current_y - target_y
6750 > min (window_box_height (it->w), line_height * 2 / 3))
6751 && IT_CHARPOS (*it) > BEGV)
6753 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6754 target_y - it->current_y));
6755 dy = it->current_y - target_y;
6756 goto move_further_back;
6758 else if (target_y >= it->current_y + line_height
6759 && IT_CHARPOS (*it) < ZV)
6761 /* Should move forward by at least one line, maybe more.
6763 Note: Calling move_it_by_lines can be expensive on
6764 terminal frames, where compute_motion is used (via
6765 vmotion) to do the job, when there are very long lines
6766 and truncate-lines is nil. That's the reason for
6767 treating terminal frames specially here. */
6769 if (!FRAME_WINDOW_P (it->f))
6770 move_it_vertically (it, target_y - (it->current_y + line_height));
6771 else
6775 move_it_by_lines (it, 1, 1);
6777 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6780 #if 0
6781 /* I think this assert is bogus if buffer contains
6782 invisible text or images. KFS. */
6783 xassert (IT_CHARPOS (*it) >= BEGV);
6784 #endif
6790 /* Move IT by a specified amount of pixel lines DY. DY negative means
6791 move backwards. DY = 0 means move to start of screen line. At the
6792 end, IT will be on the start of a screen line. */
6794 void
6795 move_it_vertically (it, dy)
6796 struct it *it;
6797 int dy;
6799 if (dy <= 0)
6800 move_it_vertically_backward (it, -dy);
6801 else
6803 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6804 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6805 MOVE_TO_POS | MOVE_TO_Y);
6806 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6808 /* If buffer ends in ZV without a newline, move to the start of
6809 the line to satisfy the post-condition. */
6810 if (IT_CHARPOS (*it) == ZV
6811 && ZV > BEGV
6812 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6813 move_it_by_lines (it, 0, 0);
6818 /* Move iterator IT past the end of the text line it is in. */
6820 void
6821 move_it_past_eol (it)
6822 struct it *it;
6824 enum move_it_result rc;
6826 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6827 if (rc == MOVE_NEWLINE_OR_CR)
6828 set_iterator_to_next (it, 0);
6832 #if 0 /* Currently not used. */
6834 /* Return non-zero if some text between buffer positions START_CHARPOS
6835 and END_CHARPOS is invisible. IT->window is the window for text
6836 property lookup. */
6838 static int
6839 invisible_text_between_p (it, start_charpos, end_charpos)
6840 struct it *it;
6841 int start_charpos, end_charpos;
6843 Lisp_Object prop, limit;
6844 int invisible_found_p;
6846 xassert (it != NULL && start_charpos <= end_charpos);
6848 /* Is text at START invisible? */
6849 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6850 it->window);
6851 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6852 invisible_found_p = 1;
6853 else
6855 limit = Fnext_single_char_property_change (make_number (start_charpos),
6856 Qinvisible, Qnil,
6857 make_number (end_charpos));
6858 invisible_found_p = XFASTINT (limit) < end_charpos;
6861 return invisible_found_p;
6864 #endif /* 0 */
6867 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6868 negative means move up. DVPOS == 0 means move to the start of the
6869 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6870 NEED_Y_P is zero, IT->current_y will be left unchanged.
6872 Further optimization ideas: If we would know that IT->f doesn't use
6873 a face with proportional font, we could be faster for
6874 truncate-lines nil. */
6876 void
6877 move_it_by_lines (it, dvpos, need_y_p)
6878 struct it *it;
6879 int dvpos, need_y_p;
6881 struct position pos;
6883 if (!FRAME_WINDOW_P (it->f))
6885 struct text_pos textpos;
6887 /* We can use vmotion on frames without proportional fonts. */
6888 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6889 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6890 reseat (it, textpos, 1);
6891 it->vpos += pos.vpos;
6892 it->current_y += pos.vpos;
6894 else if (dvpos == 0)
6896 /* DVPOS == 0 means move to the start of the screen line. */
6897 move_it_vertically_backward (it, 0);
6898 xassert (it->current_x == 0 && it->hpos == 0);
6899 /* Let next call to line_bottom_y calculate real line height */
6900 last_height = 0;
6902 else if (dvpos > 0)
6904 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6905 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6906 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6908 else
6910 struct it it2;
6911 int start_charpos, i;
6913 /* Start at the beginning of the screen line containing IT's
6914 position. This may actually move vertically backwards,
6915 in case of overlays, so adjust dvpos accordingly. */
6916 dvpos += it->vpos;
6917 move_it_vertically_backward (it, 0);
6918 dvpos -= it->vpos;
6920 /* Go back -DVPOS visible lines and reseat the iterator there. */
6921 start_charpos = IT_CHARPOS (*it);
6922 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6923 back_to_previous_visible_line_start (it);
6924 reseat (it, it->current.pos, 1);
6926 /* Move further back if we end up in a string or an image. */
6927 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6929 /* First try to move to start of display line. */
6930 dvpos += it->vpos;
6931 move_it_vertically_backward (it, 0);
6932 dvpos -= it->vpos;
6933 if (IT_POS_VALID_AFTER_MOVE_P (it))
6934 break;
6935 /* If start of line is still in string or image,
6936 move further back. */
6937 back_to_previous_visible_line_start (it);
6938 reseat (it, it->current.pos, 1);
6939 dvpos--;
6942 it->current_x = it->hpos = 0;
6944 /* Above call may have moved too far if continuation lines
6945 are involved. Scan forward and see if it did. */
6946 it2 = *it;
6947 it2.vpos = it2.current_y = 0;
6948 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6949 it->vpos -= it2.vpos;
6950 it->current_y -= it2.current_y;
6951 it->current_x = it->hpos = 0;
6953 /* If we moved too far back, move IT some lines forward. */
6954 if (it2.vpos > -dvpos)
6956 int delta = it2.vpos + dvpos;
6957 it2 = *it;
6958 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6959 /* Move back again if we got too far ahead. */
6960 if (IT_CHARPOS (*it) >= start_charpos)
6961 *it = it2;
6966 /* Return 1 if IT points into the middle of a display vector. */
6969 in_display_vector_p (it)
6970 struct it *it;
6972 return (it->method == GET_FROM_DISPLAY_VECTOR
6973 && it->current.dpvec_index > 0
6974 && it->dpvec + it->current.dpvec_index != it->dpend);
6978 /***********************************************************************
6979 Messages
6980 ***********************************************************************/
6983 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6984 to *Messages*. */
6986 void
6987 add_to_log (format, arg1, arg2)
6988 char *format;
6989 Lisp_Object arg1, arg2;
6991 Lisp_Object args[3];
6992 Lisp_Object msg, fmt;
6993 char *buffer;
6994 int len;
6995 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6996 USE_SAFE_ALLOCA;
6998 /* Do nothing if called asynchronously. Inserting text into
6999 a buffer may call after-change-functions and alike and
7000 that would means running Lisp asynchronously. */
7001 if (handling_signal)
7002 return;
7004 fmt = msg = Qnil;
7005 GCPRO4 (fmt, msg, arg1, arg2);
7007 args[0] = fmt = build_string (format);
7008 args[1] = arg1;
7009 args[2] = arg2;
7010 msg = Fformat (3, args);
7012 len = SBYTES (msg) + 1;
7013 SAFE_ALLOCA (buffer, char *, len);
7014 bcopy (SDATA (msg), buffer, len);
7016 message_dolog (buffer, len - 1, 1, 0);
7017 SAFE_FREE ();
7019 UNGCPRO;
7023 /* Output a newline in the *Messages* buffer if "needs" one. */
7025 void
7026 message_log_maybe_newline ()
7028 if (message_log_need_newline)
7029 message_dolog ("", 0, 1, 0);
7033 /* Add a string M of length NBYTES to the message log, optionally
7034 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7035 nonzero, means interpret the contents of M as multibyte. This
7036 function calls low-level routines in order to bypass text property
7037 hooks, etc. which might not be safe to run.
7039 This may GC (insert may run before/after change hooks),
7040 so the buffer M must NOT point to a Lisp string. */
7042 void
7043 message_dolog (m, nbytes, nlflag, multibyte)
7044 const char *m;
7045 int nbytes, nlflag, multibyte;
7047 if (!NILP (Vmemory_full))
7048 return;
7050 if (!NILP (Vmessage_log_max))
7052 struct buffer *oldbuf;
7053 Lisp_Object oldpoint, oldbegv, oldzv;
7054 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7055 int point_at_end = 0;
7056 int zv_at_end = 0;
7057 Lisp_Object old_deactivate_mark, tem;
7058 struct gcpro gcpro1;
7060 old_deactivate_mark = Vdeactivate_mark;
7061 oldbuf = current_buffer;
7062 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7063 current_buffer->undo_list = Qt;
7065 oldpoint = message_dolog_marker1;
7066 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7067 oldbegv = message_dolog_marker2;
7068 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7069 oldzv = message_dolog_marker3;
7070 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7071 GCPRO1 (old_deactivate_mark);
7073 if (PT == Z)
7074 point_at_end = 1;
7075 if (ZV == Z)
7076 zv_at_end = 1;
7078 BEGV = BEG;
7079 BEGV_BYTE = BEG_BYTE;
7080 ZV = Z;
7081 ZV_BYTE = Z_BYTE;
7082 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7084 /* Insert the string--maybe converting multibyte to single byte
7085 or vice versa, so that all the text fits the buffer. */
7086 if (multibyte
7087 && NILP (current_buffer->enable_multibyte_characters))
7089 int i, c, char_bytes;
7090 unsigned char work[1];
7092 /* Convert a multibyte string to single-byte
7093 for the *Message* buffer. */
7094 for (i = 0; i < nbytes; i += char_bytes)
7096 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7097 work[0] = (SINGLE_BYTE_CHAR_P (c)
7099 : multibyte_char_to_unibyte (c, Qnil));
7100 insert_1_both (work, 1, 1, 1, 0, 0);
7103 else if (! multibyte
7104 && ! NILP (current_buffer->enable_multibyte_characters))
7106 int i, c, char_bytes;
7107 unsigned char *msg = (unsigned char *) m;
7108 unsigned char str[MAX_MULTIBYTE_LENGTH];
7109 /* Convert a single-byte string to multibyte
7110 for the *Message* buffer. */
7111 for (i = 0; i < nbytes; i++)
7113 c = unibyte_char_to_multibyte (msg[i]);
7114 char_bytes = CHAR_STRING (c, str);
7115 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7118 else if (nbytes)
7119 insert_1 (m, nbytes, 1, 0, 0);
7121 if (nlflag)
7123 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7124 insert_1 ("\n", 1, 1, 0, 0);
7126 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7127 this_bol = PT;
7128 this_bol_byte = PT_BYTE;
7130 /* See if this line duplicates the previous one.
7131 If so, combine duplicates. */
7132 if (this_bol > BEG)
7134 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7135 prev_bol = PT;
7136 prev_bol_byte = PT_BYTE;
7138 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7139 this_bol, this_bol_byte);
7140 if (dup)
7142 del_range_both (prev_bol, prev_bol_byte,
7143 this_bol, this_bol_byte, 0);
7144 if (dup > 1)
7146 char dupstr[40];
7147 int duplen;
7149 /* If you change this format, don't forget to also
7150 change message_log_check_duplicate. */
7151 sprintf (dupstr, " [%d times]", dup);
7152 duplen = strlen (dupstr);
7153 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7154 insert_1 (dupstr, duplen, 1, 0, 1);
7159 /* If we have more than the desired maximum number of lines
7160 in the *Messages* buffer now, delete the oldest ones.
7161 This is safe because we don't have undo in this buffer. */
7163 if (NATNUMP (Vmessage_log_max))
7165 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7166 -XFASTINT (Vmessage_log_max) - 1, 0);
7167 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7170 BEGV = XMARKER (oldbegv)->charpos;
7171 BEGV_BYTE = marker_byte_position (oldbegv);
7173 if (zv_at_end)
7175 ZV = Z;
7176 ZV_BYTE = Z_BYTE;
7178 else
7180 ZV = XMARKER (oldzv)->charpos;
7181 ZV_BYTE = marker_byte_position (oldzv);
7184 if (point_at_end)
7185 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7186 else
7187 /* We can't do Fgoto_char (oldpoint) because it will run some
7188 Lisp code. */
7189 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7190 XMARKER (oldpoint)->bytepos);
7192 UNGCPRO;
7193 unchain_marker (XMARKER (oldpoint));
7194 unchain_marker (XMARKER (oldbegv));
7195 unchain_marker (XMARKER (oldzv));
7197 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7198 set_buffer_internal (oldbuf);
7199 if (NILP (tem))
7200 windows_or_buffers_changed = old_windows_or_buffers_changed;
7201 message_log_need_newline = !nlflag;
7202 Vdeactivate_mark = old_deactivate_mark;
7207 /* We are at the end of the buffer after just having inserted a newline.
7208 (Note: We depend on the fact we won't be crossing the gap.)
7209 Check to see if the most recent message looks a lot like the previous one.
7210 Return 0 if different, 1 if the new one should just replace it, or a
7211 value N > 1 if we should also append " [N times]". */
7213 static int
7214 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7215 int prev_bol, this_bol;
7216 int prev_bol_byte, this_bol_byte;
7218 int i;
7219 int len = Z_BYTE - 1 - this_bol_byte;
7220 int seen_dots = 0;
7221 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7222 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7224 for (i = 0; i < len; i++)
7226 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7227 seen_dots = 1;
7228 if (p1[i] != p2[i])
7229 return seen_dots;
7231 p1 += len;
7232 if (*p1 == '\n')
7233 return 2;
7234 if (*p1++ == ' ' && *p1++ == '[')
7236 int n = 0;
7237 while (*p1 >= '0' && *p1 <= '9')
7238 n = n * 10 + *p1++ - '0';
7239 if (strncmp (p1, " times]\n", 8) == 0)
7240 return n+1;
7242 return 0;
7246 /* Display an echo area message M with a specified length of NBYTES
7247 bytes. The string may include null characters. If M is 0, clear
7248 out any existing message, and let the mini-buffer text show
7249 through.
7251 This may GC, so the buffer M must NOT point to a Lisp string. */
7253 void
7254 message2 (m, nbytes, multibyte)
7255 const char *m;
7256 int nbytes;
7257 int multibyte;
7259 /* First flush out any partial line written with print. */
7260 message_log_maybe_newline ();
7261 if (m)
7262 message_dolog (m, nbytes, 1, multibyte);
7263 message2_nolog (m, nbytes, multibyte);
7267 /* The non-logging counterpart of message2. */
7269 void
7270 message2_nolog (m, nbytes, multibyte)
7271 const char *m;
7272 int nbytes, multibyte;
7274 struct frame *sf = SELECTED_FRAME ();
7275 message_enable_multibyte = multibyte;
7277 if (noninteractive)
7279 if (noninteractive_need_newline)
7280 putc ('\n', stderr);
7281 noninteractive_need_newline = 0;
7282 if (m)
7283 fwrite (m, nbytes, 1, stderr);
7284 if (cursor_in_echo_area == 0)
7285 fprintf (stderr, "\n");
7286 fflush (stderr);
7288 /* A null message buffer means that the frame hasn't really been
7289 initialized yet. Error messages get reported properly by
7290 cmd_error, so this must be just an informative message; toss it. */
7291 else if (INTERACTIVE
7292 && sf->glyphs_initialized_p
7293 && FRAME_MESSAGE_BUF (sf))
7295 Lisp_Object mini_window;
7296 struct frame *f;
7298 /* Get the frame containing the mini-buffer
7299 that the selected frame is using. */
7300 mini_window = FRAME_MINIBUF_WINDOW (sf);
7301 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7303 FRAME_SAMPLE_VISIBILITY (f);
7304 if (FRAME_VISIBLE_P (sf)
7305 && ! FRAME_VISIBLE_P (f))
7306 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7308 if (m)
7310 set_message (m, Qnil, nbytes, multibyte);
7311 if (minibuffer_auto_raise)
7312 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7314 else
7315 clear_message (1, 1);
7317 do_pending_window_change (0);
7318 echo_area_display (1);
7319 do_pending_window_change (0);
7320 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7321 (*frame_up_to_date_hook) (f);
7326 /* Display an echo area message M with a specified length of NBYTES
7327 bytes. The string may include null characters. If M is not a
7328 string, clear out any existing message, and let the mini-buffer
7329 text show through.
7331 This function cancels echoing. */
7333 void
7334 message3 (m, nbytes, multibyte)
7335 Lisp_Object m;
7336 int nbytes;
7337 int multibyte;
7339 struct gcpro gcpro1;
7341 GCPRO1 (m);
7342 clear_message (1,1);
7343 cancel_echoing ();
7345 /* First flush out any partial line written with print. */
7346 message_log_maybe_newline ();
7347 if (STRINGP (m))
7349 char *buffer;
7350 USE_SAFE_ALLOCA;
7352 SAFE_ALLOCA (buffer, char *, nbytes);
7353 bcopy (SDATA (m), buffer, nbytes);
7354 message_dolog (buffer, nbytes, 1, multibyte);
7355 SAFE_FREE ();
7357 message3_nolog (m, nbytes, multibyte);
7359 UNGCPRO;
7363 /* The non-logging version of message3.
7364 This does not cancel echoing, because it is used for echoing.
7365 Perhaps we need to make a separate function for echoing
7366 and make this cancel echoing. */
7368 void
7369 message3_nolog (m, nbytes, multibyte)
7370 Lisp_Object m;
7371 int nbytes, multibyte;
7373 struct frame *sf = SELECTED_FRAME ();
7374 message_enable_multibyte = multibyte;
7376 if (noninteractive)
7378 if (noninteractive_need_newline)
7379 putc ('\n', stderr);
7380 noninteractive_need_newline = 0;
7381 if (STRINGP (m))
7382 fwrite (SDATA (m), nbytes, 1, stderr);
7383 if (cursor_in_echo_area == 0)
7384 fprintf (stderr, "\n");
7385 fflush (stderr);
7387 /* A null message buffer means that the frame hasn't really been
7388 initialized yet. Error messages get reported properly by
7389 cmd_error, so this must be just an informative message; toss it. */
7390 else if (INTERACTIVE
7391 && sf->glyphs_initialized_p
7392 && FRAME_MESSAGE_BUF (sf))
7394 Lisp_Object mini_window;
7395 Lisp_Object frame;
7396 struct frame *f;
7398 /* Get the frame containing the mini-buffer
7399 that the selected frame is using. */
7400 mini_window = FRAME_MINIBUF_WINDOW (sf);
7401 frame = XWINDOW (mini_window)->frame;
7402 f = XFRAME (frame);
7404 FRAME_SAMPLE_VISIBILITY (f);
7405 if (FRAME_VISIBLE_P (sf)
7406 && !FRAME_VISIBLE_P (f))
7407 Fmake_frame_visible (frame);
7409 if (STRINGP (m) && SCHARS (m) > 0)
7411 set_message (NULL, m, nbytes, multibyte);
7412 if (minibuffer_auto_raise)
7413 Fraise_frame (frame);
7414 /* Assume we are not echoing.
7415 (If we are, echo_now will override this.) */
7416 echo_message_buffer = Qnil;
7418 else
7419 clear_message (1, 1);
7421 do_pending_window_change (0);
7422 echo_area_display (1);
7423 do_pending_window_change (0);
7424 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7425 (*frame_up_to_date_hook) (f);
7430 /* Display a null-terminated echo area message M. If M is 0, clear
7431 out any existing message, and let the mini-buffer text show through.
7433 The buffer M must continue to exist until after the echo area gets
7434 cleared or some other message gets displayed there. Do not pass
7435 text that is stored in a Lisp string. Do not pass text in a buffer
7436 that was alloca'd. */
7438 void
7439 message1 (m)
7440 char *m;
7442 message2 (m, (m ? strlen (m) : 0), 0);
7446 /* The non-logging counterpart of message1. */
7448 void
7449 message1_nolog (m)
7450 char *m;
7452 message2_nolog (m, (m ? strlen (m) : 0), 0);
7455 /* Display a message M which contains a single %s
7456 which gets replaced with STRING. */
7458 void
7459 message_with_string (m, string, log)
7460 char *m;
7461 Lisp_Object string;
7462 int log;
7464 CHECK_STRING (string);
7466 if (noninteractive)
7468 if (m)
7470 if (noninteractive_need_newline)
7471 putc ('\n', stderr);
7472 noninteractive_need_newline = 0;
7473 fprintf (stderr, m, SDATA (string));
7474 if (cursor_in_echo_area == 0)
7475 fprintf (stderr, "\n");
7476 fflush (stderr);
7479 else if (INTERACTIVE)
7481 /* The frame whose minibuffer we're going to display the message on.
7482 It may be larger than the selected frame, so we need
7483 to use its buffer, not the selected frame's buffer. */
7484 Lisp_Object mini_window;
7485 struct frame *f, *sf = SELECTED_FRAME ();
7487 /* Get the frame containing the minibuffer
7488 that the selected frame is using. */
7489 mini_window = FRAME_MINIBUF_WINDOW (sf);
7490 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7492 /* A null message buffer means that the frame hasn't really been
7493 initialized yet. Error messages get reported properly by
7494 cmd_error, so this must be just an informative message; toss it. */
7495 if (FRAME_MESSAGE_BUF (f))
7497 Lisp_Object args[2], message;
7498 struct gcpro gcpro1, gcpro2;
7500 args[0] = build_string (m);
7501 args[1] = message = string;
7502 GCPRO2 (args[0], message);
7503 gcpro1.nvars = 2;
7505 message = Fformat (2, args);
7507 if (log)
7508 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7509 else
7510 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7512 UNGCPRO;
7514 /* Print should start at the beginning of the message
7515 buffer next time. */
7516 message_buf_print = 0;
7522 /* Dump an informative message to the minibuf. If M is 0, clear out
7523 any existing message, and let the mini-buffer text show through. */
7525 /* VARARGS 1 */
7526 void
7527 message (m, a1, a2, a3)
7528 char *m;
7529 EMACS_INT a1, a2, a3;
7531 if (noninteractive)
7533 if (m)
7535 if (noninteractive_need_newline)
7536 putc ('\n', stderr);
7537 noninteractive_need_newline = 0;
7538 fprintf (stderr, m, a1, a2, a3);
7539 if (cursor_in_echo_area == 0)
7540 fprintf (stderr, "\n");
7541 fflush (stderr);
7544 else if (INTERACTIVE)
7546 /* The frame whose mini-buffer we're going to display the message
7547 on. It may be larger than the selected frame, so we need to
7548 use its buffer, not the selected frame's buffer. */
7549 Lisp_Object mini_window;
7550 struct frame *f, *sf = SELECTED_FRAME ();
7552 /* Get the frame containing the mini-buffer
7553 that the selected frame is using. */
7554 mini_window = FRAME_MINIBUF_WINDOW (sf);
7555 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7557 /* A null message buffer means that the frame hasn't really been
7558 initialized yet. Error messages get reported properly by
7559 cmd_error, so this must be just an informative message; toss
7560 it. */
7561 if (FRAME_MESSAGE_BUF (f))
7563 if (m)
7565 int len;
7566 #ifdef NO_ARG_ARRAY
7567 char *a[3];
7568 a[0] = (char *) a1;
7569 a[1] = (char *) a2;
7570 a[2] = (char *) a3;
7572 len = doprnt (FRAME_MESSAGE_BUF (f),
7573 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7574 #else
7575 len = doprnt (FRAME_MESSAGE_BUF (f),
7576 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7577 (char **) &a1);
7578 #endif /* NO_ARG_ARRAY */
7580 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7582 else
7583 message1 (0);
7585 /* Print should start at the beginning of the message
7586 buffer next time. */
7587 message_buf_print = 0;
7593 /* The non-logging version of message. */
7595 void
7596 message_nolog (m, a1, a2, a3)
7597 char *m;
7598 EMACS_INT a1, a2, a3;
7600 Lisp_Object old_log_max;
7601 old_log_max = Vmessage_log_max;
7602 Vmessage_log_max = Qnil;
7603 message (m, a1, a2, a3);
7604 Vmessage_log_max = old_log_max;
7608 /* Display the current message in the current mini-buffer. This is
7609 only called from error handlers in process.c, and is not time
7610 critical. */
7612 void
7613 update_echo_area ()
7615 if (!NILP (echo_area_buffer[0]))
7617 Lisp_Object string;
7618 string = Fcurrent_message ();
7619 message3 (string, SBYTES (string),
7620 !NILP (current_buffer->enable_multibyte_characters));
7625 /* Make sure echo area buffers in `echo_buffers' are live.
7626 If they aren't, make new ones. */
7628 static void
7629 ensure_echo_area_buffers ()
7631 int i;
7633 for (i = 0; i < 2; ++i)
7634 if (!BUFFERP (echo_buffer[i])
7635 || NILP (XBUFFER (echo_buffer[i])->name))
7637 char name[30];
7638 Lisp_Object old_buffer;
7639 int j;
7641 old_buffer = echo_buffer[i];
7642 sprintf (name, " *Echo Area %d*", i);
7643 echo_buffer[i] = Fget_buffer_create (build_string (name));
7644 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7646 for (j = 0; j < 2; ++j)
7647 if (EQ (old_buffer, echo_area_buffer[j]))
7648 echo_area_buffer[j] = echo_buffer[i];
7653 /* Call FN with args A1..A4 with either the current or last displayed
7654 echo_area_buffer as current buffer.
7656 WHICH zero means use the current message buffer
7657 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7658 from echo_buffer[] and clear it.
7660 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7661 suitable buffer from echo_buffer[] and clear it.
7663 Value is what FN returns. */
7665 static int
7666 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7667 struct window *w;
7668 int which;
7669 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7670 EMACS_INT a1;
7671 Lisp_Object a2;
7672 EMACS_INT a3, a4;
7674 Lisp_Object buffer;
7675 int this_one, the_other, clear_buffer_p, rc;
7676 int count = SPECPDL_INDEX ();
7678 /* If buffers aren't live, make new ones. */
7679 ensure_echo_area_buffers ();
7681 clear_buffer_p = 0;
7683 if (which == 0)
7684 this_one = 0, the_other = 1;
7685 else if (which > 0)
7686 this_one = 1, the_other = 0;
7688 /* Choose a suitable buffer from echo_buffer[] is we don't
7689 have one. */
7690 if (NILP (echo_area_buffer[this_one]))
7692 echo_area_buffer[this_one]
7693 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7694 ? echo_buffer[the_other]
7695 : echo_buffer[this_one]);
7696 clear_buffer_p = 1;
7699 buffer = echo_area_buffer[this_one];
7701 /* Don't get confused by reusing the buffer used for echoing
7702 for a different purpose. */
7703 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7704 cancel_echoing ();
7706 record_unwind_protect (unwind_with_echo_area_buffer,
7707 with_echo_area_buffer_unwind_data (w));
7709 /* Make the echo area buffer current. Note that for display
7710 purposes, it is not necessary that the displayed window's buffer
7711 == current_buffer, except for text property lookup. So, let's
7712 only set that buffer temporarily here without doing a full
7713 Fset_window_buffer. We must also change w->pointm, though,
7714 because otherwise an assertions in unshow_buffer fails, and Emacs
7715 aborts. */
7716 set_buffer_internal_1 (XBUFFER (buffer));
7717 if (w)
7719 w->buffer = buffer;
7720 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7723 current_buffer->undo_list = Qt;
7724 current_buffer->read_only = Qnil;
7725 specbind (Qinhibit_read_only, Qt);
7726 specbind (Qinhibit_modification_hooks, Qt);
7728 if (clear_buffer_p && Z > BEG)
7729 del_range (BEG, Z);
7731 xassert (BEGV >= BEG);
7732 xassert (ZV <= Z && ZV >= BEGV);
7734 rc = fn (a1, a2, a3, a4);
7736 xassert (BEGV >= BEG);
7737 xassert (ZV <= Z && ZV >= BEGV);
7739 unbind_to (count, Qnil);
7740 return rc;
7744 /* Save state that should be preserved around the call to the function
7745 FN called in with_echo_area_buffer. */
7747 static Lisp_Object
7748 with_echo_area_buffer_unwind_data (w)
7749 struct window *w;
7751 int i = 0;
7752 Lisp_Object vector;
7754 /* Reduce consing by keeping one vector in
7755 Vwith_echo_area_save_vector. */
7756 vector = Vwith_echo_area_save_vector;
7757 Vwith_echo_area_save_vector = Qnil;
7759 if (NILP (vector))
7760 vector = Fmake_vector (make_number (7), Qnil);
7762 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7763 AREF (vector, i) = Vdeactivate_mark, ++i;
7764 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7766 if (w)
7768 XSETWINDOW (AREF (vector, i), w); ++i;
7769 AREF (vector, i) = w->buffer; ++i;
7770 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7771 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7773 else
7775 int end = i + 4;
7776 for (; i < end; ++i)
7777 AREF (vector, i) = Qnil;
7780 xassert (i == ASIZE (vector));
7781 return vector;
7785 /* Restore global state from VECTOR which was created by
7786 with_echo_area_buffer_unwind_data. */
7788 static Lisp_Object
7789 unwind_with_echo_area_buffer (vector)
7790 Lisp_Object vector;
7792 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7793 Vdeactivate_mark = AREF (vector, 1);
7794 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7796 if (WINDOWP (AREF (vector, 3)))
7798 struct window *w;
7799 Lisp_Object buffer, charpos, bytepos;
7801 w = XWINDOW (AREF (vector, 3));
7802 buffer = AREF (vector, 4);
7803 charpos = AREF (vector, 5);
7804 bytepos = AREF (vector, 6);
7806 w->buffer = buffer;
7807 set_marker_both (w->pointm, buffer,
7808 XFASTINT (charpos), XFASTINT (bytepos));
7811 Vwith_echo_area_save_vector = vector;
7812 return Qnil;
7816 /* Set up the echo area for use by print functions. MULTIBYTE_P
7817 non-zero means we will print multibyte. */
7819 void
7820 setup_echo_area_for_printing (multibyte_p)
7821 int multibyte_p;
7823 /* If we can't find an echo area any more, exit. */
7824 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7825 Fkill_emacs (Qnil);
7827 ensure_echo_area_buffers ();
7829 if (!message_buf_print)
7831 /* A message has been output since the last time we printed.
7832 Choose a fresh echo area buffer. */
7833 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7834 echo_area_buffer[0] = echo_buffer[1];
7835 else
7836 echo_area_buffer[0] = echo_buffer[0];
7838 /* Switch to that buffer and clear it. */
7839 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7840 current_buffer->truncate_lines = Qnil;
7842 if (Z > BEG)
7844 int count = SPECPDL_INDEX ();
7845 specbind (Qinhibit_read_only, Qt);
7846 /* Note that undo recording is always disabled. */
7847 del_range (BEG, Z);
7848 unbind_to (count, Qnil);
7850 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7852 /* Set up the buffer for the multibyteness we need. */
7853 if (multibyte_p
7854 != !NILP (current_buffer->enable_multibyte_characters))
7855 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7857 /* Raise the frame containing the echo area. */
7858 if (minibuffer_auto_raise)
7860 struct frame *sf = SELECTED_FRAME ();
7861 Lisp_Object mini_window;
7862 mini_window = FRAME_MINIBUF_WINDOW (sf);
7863 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7866 message_log_maybe_newline ();
7867 message_buf_print = 1;
7869 else
7871 if (NILP (echo_area_buffer[0]))
7873 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7874 echo_area_buffer[0] = echo_buffer[1];
7875 else
7876 echo_area_buffer[0] = echo_buffer[0];
7879 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7881 /* Someone switched buffers between print requests. */
7882 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7883 current_buffer->truncate_lines = Qnil;
7889 /* Display an echo area message in window W. Value is non-zero if W's
7890 height is changed. If display_last_displayed_message_p is
7891 non-zero, display the message that was last displayed, otherwise
7892 display the current message. */
7894 static int
7895 display_echo_area (w)
7896 struct window *w;
7898 int i, no_message_p, window_height_changed_p, count;
7900 /* Temporarily disable garbage collections while displaying the echo
7901 area. This is done because a GC can print a message itself.
7902 That message would modify the echo area buffer's contents while a
7903 redisplay of the buffer is going on, and seriously confuse
7904 redisplay. */
7905 count = inhibit_garbage_collection ();
7907 /* If there is no message, we must call display_echo_area_1
7908 nevertheless because it resizes the window. But we will have to
7909 reset the echo_area_buffer in question to nil at the end because
7910 with_echo_area_buffer will sets it to an empty buffer. */
7911 i = display_last_displayed_message_p ? 1 : 0;
7912 no_message_p = NILP (echo_area_buffer[i]);
7914 window_height_changed_p
7915 = with_echo_area_buffer (w, display_last_displayed_message_p,
7916 display_echo_area_1,
7917 (EMACS_INT) w, Qnil, 0, 0);
7919 if (no_message_p)
7920 echo_area_buffer[i] = Qnil;
7922 unbind_to (count, Qnil);
7923 return window_height_changed_p;
7927 /* Helper for display_echo_area. Display the current buffer which
7928 contains the current echo area message in window W, a mini-window,
7929 a pointer to which is passed in A1. A2..A4 are currently not used.
7930 Change the height of W so that all of the message is displayed.
7931 Value is non-zero if height of W was changed. */
7933 static int
7934 display_echo_area_1 (a1, a2, a3, a4)
7935 EMACS_INT a1;
7936 Lisp_Object a2;
7937 EMACS_INT a3, a4;
7939 struct window *w = (struct window *) a1;
7940 Lisp_Object window;
7941 struct text_pos start;
7942 int window_height_changed_p = 0;
7944 /* Do this before displaying, so that we have a large enough glyph
7945 matrix for the display. If we can't get enough space for the
7946 whole text, display the last N lines. That works by setting w->start. */
7947 window_height_changed_p = resize_mini_window (w, 0);
7949 /* Use the starting position chosen by resize_mini_window. */
7950 SET_TEXT_POS_FROM_MARKER (start, w->start);
7952 /* Display. */
7953 clear_glyph_matrix (w->desired_matrix);
7954 XSETWINDOW (window, w);
7955 try_window (window, start, 0);
7957 return window_height_changed_p;
7961 /* Resize the echo area window to exactly the size needed for the
7962 currently displayed message, if there is one. If a mini-buffer
7963 is active, don't shrink it. */
7965 void
7966 resize_echo_area_exactly ()
7968 if (BUFFERP (echo_area_buffer[0])
7969 && WINDOWP (echo_area_window))
7971 struct window *w = XWINDOW (echo_area_window);
7972 int resized_p;
7973 Lisp_Object resize_exactly;
7975 if (minibuf_level == 0)
7976 resize_exactly = Qt;
7977 else
7978 resize_exactly = Qnil;
7980 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7981 (EMACS_INT) w, resize_exactly, 0, 0);
7982 if (resized_p)
7984 ++windows_or_buffers_changed;
7985 ++update_mode_lines;
7986 redisplay_internal (0);
7992 /* Callback function for with_echo_area_buffer, when used from
7993 resize_echo_area_exactly. A1 contains a pointer to the window to
7994 resize, EXACTLY non-nil means resize the mini-window exactly to the
7995 size of the text displayed. A3 and A4 are not used. Value is what
7996 resize_mini_window returns. */
7998 static int
7999 resize_mini_window_1 (a1, exactly, a3, a4)
8000 EMACS_INT a1;
8001 Lisp_Object exactly;
8002 EMACS_INT a3, a4;
8004 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8008 /* Resize mini-window W to fit the size of its contents. EXACT:P
8009 means size the window exactly to the size needed. Otherwise, it's
8010 only enlarged until W's buffer is empty.
8012 Set W->start to the right place to begin display. If the whole
8013 contents fit, start at the beginning. Otherwise, start so as
8014 to make the end of the contents appear. This is particularly
8015 important for y-or-n-p, but seems desirable generally.
8017 Value is non-zero if the window height has been changed. */
8020 resize_mini_window (w, exact_p)
8021 struct window *w;
8022 int exact_p;
8024 struct frame *f = XFRAME (w->frame);
8025 int window_height_changed_p = 0;
8027 xassert (MINI_WINDOW_P (w));
8029 /* By default, start display at the beginning. */
8030 set_marker_both (w->start, w->buffer,
8031 BUF_BEGV (XBUFFER (w->buffer)),
8032 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8034 /* Don't resize windows while redisplaying a window; it would
8035 confuse redisplay functions when the size of the window they are
8036 displaying changes from under them. Such a resizing can happen,
8037 for instance, when which-func prints a long message while
8038 we are running fontification-functions. We're running these
8039 functions with safe_call which binds inhibit-redisplay to t. */
8040 if (!NILP (Vinhibit_redisplay))
8041 return 0;
8043 /* Nil means don't try to resize. */
8044 if (NILP (Vresize_mini_windows)
8045 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8046 return 0;
8048 if (!FRAME_MINIBUF_ONLY_P (f))
8050 struct it it;
8051 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8052 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8053 int height, max_height;
8054 int unit = FRAME_LINE_HEIGHT (f);
8055 struct text_pos start;
8056 struct buffer *old_current_buffer = NULL;
8058 if (current_buffer != XBUFFER (w->buffer))
8060 old_current_buffer = current_buffer;
8061 set_buffer_internal (XBUFFER (w->buffer));
8064 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8066 /* Compute the max. number of lines specified by the user. */
8067 if (FLOATP (Vmax_mini_window_height))
8068 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8069 else if (INTEGERP (Vmax_mini_window_height))
8070 max_height = XINT (Vmax_mini_window_height);
8071 else
8072 max_height = total_height / 4;
8074 /* Correct that max. height if it's bogus. */
8075 max_height = max (1, max_height);
8076 max_height = min (total_height, max_height);
8078 /* Find out the height of the text in the window. */
8079 if (it.truncate_lines_p)
8080 height = 1;
8081 else
8083 last_height = 0;
8084 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8085 if (it.max_ascent == 0 && it.max_descent == 0)
8086 height = it.current_y + last_height;
8087 else
8088 height = it.current_y + it.max_ascent + it.max_descent;
8089 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8090 height = (height + unit - 1) / unit;
8093 /* Compute a suitable window start. */
8094 if (height > max_height)
8096 height = max_height;
8097 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8098 move_it_vertically_backward (&it, (height - 1) * unit);
8099 start = it.current.pos;
8101 else
8102 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8103 SET_MARKER_FROM_TEXT_POS (w->start, start);
8105 if (EQ (Vresize_mini_windows, Qgrow_only))
8107 /* Let it grow only, until we display an empty message, in which
8108 case the window shrinks again. */
8109 if (height > WINDOW_TOTAL_LINES (w))
8111 int old_height = WINDOW_TOTAL_LINES (w);
8112 freeze_window_starts (f, 1);
8113 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8114 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8116 else if (height < WINDOW_TOTAL_LINES (w)
8117 && (exact_p || BEGV == ZV))
8119 int old_height = WINDOW_TOTAL_LINES (w);
8120 freeze_window_starts (f, 0);
8121 shrink_mini_window (w);
8122 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8125 else
8127 /* Always resize to exact size needed. */
8128 if (height > WINDOW_TOTAL_LINES (w))
8130 int old_height = WINDOW_TOTAL_LINES (w);
8131 freeze_window_starts (f, 1);
8132 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8133 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8135 else if (height < WINDOW_TOTAL_LINES (w))
8137 int old_height = WINDOW_TOTAL_LINES (w);
8138 freeze_window_starts (f, 0);
8139 shrink_mini_window (w);
8141 if (height)
8143 freeze_window_starts (f, 1);
8144 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8147 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8151 if (old_current_buffer)
8152 set_buffer_internal (old_current_buffer);
8155 return window_height_changed_p;
8159 /* Value is the current message, a string, or nil if there is no
8160 current message. */
8162 Lisp_Object
8163 current_message ()
8165 Lisp_Object msg;
8167 if (NILP (echo_area_buffer[0]))
8168 msg = Qnil;
8169 else
8171 with_echo_area_buffer (0, 0, current_message_1,
8172 (EMACS_INT) &msg, Qnil, 0, 0);
8173 if (NILP (msg))
8174 echo_area_buffer[0] = Qnil;
8177 return msg;
8181 static int
8182 current_message_1 (a1, a2, a3, a4)
8183 EMACS_INT a1;
8184 Lisp_Object a2;
8185 EMACS_INT a3, a4;
8187 Lisp_Object *msg = (Lisp_Object *) a1;
8189 if (Z > BEG)
8190 *msg = make_buffer_string (BEG, Z, 1);
8191 else
8192 *msg = Qnil;
8193 return 0;
8197 /* Push the current message on Vmessage_stack for later restauration
8198 by restore_message. Value is non-zero if the current message isn't
8199 empty. This is a relatively infrequent operation, so it's not
8200 worth optimizing. */
8203 push_message ()
8205 Lisp_Object msg;
8206 msg = current_message ();
8207 Vmessage_stack = Fcons (msg, Vmessage_stack);
8208 return STRINGP (msg);
8212 /* Restore message display from the top of Vmessage_stack. */
8214 void
8215 restore_message ()
8217 Lisp_Object msg;
8219 xassert (CONSP (Vmessage_stack));
8220 msg = XCAR (Vmessage_stack);
8221 if (STRINGP (msg))
8222 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8223 else
8224 message3_nolog (msg, 0, 0);
8228 /* Handler for record_unwind_protect calling pop_message. */
8230 Lisp_Object
8231 pop_message_unwind (dummy)
8232 Lisp_Object dummy;
8234 pop_message ();
8235 return Qnil;
8238 /* Pop the top-most entry off Vmessage_stack. */
8240 void
8241 pop_message ()
8243 xassert (CONSP (Vmessage_stack));
8244 Vmessage_stack = XCDR (Vmessage_stack);
8248 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8249 exits. If the stack is not empty, we have a missing pop_message
8250 somewhere. */
8252 void
8253 check_message_stack ()
8255 if (!NILP (Vmessage_stack))
8256 abort ();
8260 /* Truncate to NCHARS what will be displayed in the echo area the next
8261 time we display it---but don't redisplay it now. */
8263 void
8264 truncate_echo_area (nchars)
8265 int nchars;
8267 if (nchars == 0)
8268 echo_area_buffer[0] = Qnil;
8269 /* A null message buffer means that the frame hasn't really been
8270 initialized yet. Error messages get reported properly by
8271 cmd_error, so this must be just an informative message; toss it. */
8272 else if (!noninteractive
8273 && INTERACTIVE
8274 && !NILP (echo_area_buffer[0]))
8276 struct frame *sf = SELECTED_FRAME ();
8277 if (FRAME_MESSAGE_BUF (sf))
8278 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8283 /* Helper function for truncate_echo_area. Truncate the current
8284 message to at most NCHARS characters. */
8286 static int
8287 truncate_message_1 (nchars, a2, a3, a4)
8288 EMACS_INT nchars;
8289 Lisp_Object a2;
8290 EMACS_INT a3, a4;
8292 if (BEG + nchars < Z)
8293 del_range (BEG + nchars, Z);
8294 if (Z == BEG)
8295 echo_area_buffer[0] = Qnil;
8296 return 0;
8300 /* Set the current message to a substring of S or STRING.
8302 If STRING is a Lisp string, set the message to the first NBYTES
8303 bytes from STRING. NBYTES zero means use the whole string. If
8304 STRING is multibyte, the message will be displayed multibyte.
8306 If S is not null, set the message to the first LEN bytes of S. LEN
8307 zero means use the whole string. MULTIBYTE_P non-zero means S is
8308 multibyte. Display the message multibyte in that case.
8310 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8311 to t before calling set_message_1 (which calls insert).
8314 void
8315 set_message (s, string, nbytes, multibyte_p)
8316 const char *s;
8317 Lisp_Object string;
8318 int nbytes, multibyte_p;
8320 message_enable_multibyte
8321 = ((s && multibyte_p)
8322 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8324 with_echo_area_buffer (0, 0, set_message_1,
8325 (EMACS_INT) s, string, nbytes, multibyte_p);
8326 message_buf_print = 0;
8327 help_echo_showing_p = 0;
8331 /* Helper function for set_message. Arguments have the same meaning
8332 as there, with A1 corresponding to S and A2 corresponding to STRING
8333 This function is called with the echo area buffer being
8334 current. */
8336 static int
8337 set_message_1 (a1, a2, nbytes, multibyte_p)
8338 EMACS_INT a1;
8339 Lisp_Object a2;
8340 EMACS_INT nbytes, multibyte_p;
8342 const char *s = (const char *) a1;
8343 Lisp_Object string = a2;
8345 /* Change multibyteness of the echo buffer appropriately. */
8346 if (message_enable_multibyte
8347 != !NILP (current_buffer->enable_multibyte_characters))
8348 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8350 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8352 /* Insert new message at BEG. */
8353 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8354 Ferase_buffer ();
8356 if (STRINGP (string))
8358 int nchars;
8360 if (nbytes == 0)
8361 nbytes = SBYTES (string);
8362 nchars = string_byte_to_char (string, nbytes);
8364 /* This function takes care of single/multibyte conversion. We
8365 just have to ensure that the echo area buffer has the right
8366 setting of enable_multibyte_characters. */
8367 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8369 else if (s)
8371 if (nbytes == 0)
8372 nbytes = strlen (s);
8374 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8376 /* Convert from multi-byte to single-byte. */
8377 int i, c, n;
8378 unsigned char work[1];
8380 /* Convert a multibyte string to single-byte. */
8381 for (i = 0; i < nbytes; i += n)
8383 c = string_char_and_length (s + i, nbytes - i, &n);
8384 work[0] = (SINGLE_BYTE_CHAR_P (c)
8386 : multibyte_char_to_unibyte (c, Qnil));
8387 insert_1_both (work, 1, 1, 1, 0, 0);
8390 else if (!multibyte_p
8391 && !NILP (current_buffer->enable_multibyte_characters))
8393 /* Convert from single-byte to multi-byte. */
8394 int i, c, n;
8395 const unsigned char *msg = (const unsigned char *) s;
8396 unsigned char str[MAX_MULTIBYTE_LENGTH];
8398 /* Convert a single-byte string to multibyte. */
8399 for (i = 0; i < nbytes; i++)
8401 c = unibyte_char_to_multibyte (msg[i]);
8402 n = CHAR_STRING (c, str);
8403 insert_1_both (str, 1, n, 1, 0, 0);
8406 else
8407 insert_1 (s, nbytes, 1, 0, 0);
8410 return 0;
8414 /* Clear messages. CURRENT_P non-zero means clear the current
8415 message. LAST_DISPLAYED_P non-zero means clear the message
8416 last displayed. */
8418 void
8419 clear_message (current_p, last_displayed_p)
8420 int current_p, last_displayed_p;
8422 if (current_p)
8424 echo_area_buffer[0] = Qnil;
8425 message_cleared_p = 1;
8428 if (last_displayed_p)
8429 echo_area_buffer[1] = Qnil;
8431 message_buf_print = 0;
8434 /* Clear garbaged frames.
8436 This function is used where the old redisplay called
8437 redraw_garbaged_frames which in turn called redraw_frame which in
8438 turn called clear_frame. The call to clear_frame was a source of
8439 flickering. I believe a clear_frame is not necessary. It should
8440 suffice in the new redisplay to invalidate all current matrices,
8441 and ensure a complete redisplay of all windows. */
8443 static void
8444 clear_garbaged_frames ()
8446 if (frame_garbaged)
8448 Lisp_Object tail, frame;
8449 int changed_count = 0;
8451 FOR_EACH_FRAME (tail, frame)
8453 struct frame *f = XFRAME (frame);
8455 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8457 if (f->resized_p)
8459 Fredraw_frame (frame);
8460 f->force_flush_display_p = 1;
8462 clear_current_matrices (f);
8463 changed_count++;
8464 f->garbaged = 0;
8465 f->resized_p = 0;
8469 frame_garbaged = 0;
8470 if (changed_count)
8471 ++windows_or_buffers_changed;
8476 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8477 is non-zero update selected_frame. Value is non-zero if the
8478 mini-windows height has been changed. */
8480 static int
8481 echo_area_display (update_frame_p)
8482 int update_frame_p;
8484 Lisp_Object mini_window;
8485 struct window *w;
8486 struct frame *f;
8487 int window_height_changed_p = 0;
8488 struct frame *sf = SELECTED_FRAME ();
8490 mini_window = FRAME_MINIBUF_WINDOW (sf);
8491 w = XWINDOW (mini_window);
8492 f = XFRAME (WINDOW_FRAME (w));
8494 /* Don't display if frame is invisible or not yet initialized. */
8495 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8496 return 0;
8498 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8499 #ifndef MAC_OS8
8500 #ifdef HAVE_WINDOW_SYSTEM
8501 /* When Emacs starts, selected_frame may be a visible terminal
8502 frame, even if we run under a window system. If we let this
8503 through, a message would be displayed on the terminal. */
8504 if (EQ (selected_frame, Vterminal_frame)
8505 && !NILP (Vwindow_system))
8506 return 0;
8507 #endif /* HAVE_WINDOW_SYSTEM */
8508 #endif
8510 /* Redraw garbaged frames. */
8511 if (frame_garbaged)
8512 clear_garbaged_frames ();
8514 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8516 echo_area_window = mini_window;
8517 window_height_changed_p = display_echo_area (w);
8518 w->must_be_updated_p = 1;
8520 /* Update the display, unless called from redisplay_internal.
8521 Also don't update the screen during redisplay itself. The
8522 update will happen at the end of redisplay, and an update
8523 here could cause confusion. */
8524 if (update_frame_p && !redisplaying_p)
8526 int n = 0;
8528 /* If the display update has been interrupted by pending
8529 input, update mode lines in the frame. Due to the
8530 pending input, it might have been that redisplay hasn't
8531 been called, so that mode lines above the echo area are
8532 garbaged. This looks odd, so we prevent it here. */
8533 if (!display_completed)
8534 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8536 if (window_height_changed_p
8537 /* Don't do this if Emacs is shutting down. Redisplay
8538 needs to run hooks. */
8539 && !NILP (Vrun_hooks))
8541 /* Must update other windows. Likewise as in other
8542 cases, don't let this update be interrupted by
8543 pending input. */
8544 int count = SPECPDL_INDEX ();
8545 specbind (Qredisplay_dont_pause, Qt);
8546 windows_or_buffers_changed = 1;
8547 redisplay_internal (0);
8548 unbind_to (count, Qnil);
8550 else if (FRAME_WINDOW_P (f) && n == 0)
8552 /* Window configuration is the same as before.
8553 Can do with a display update of the echo area,
8554 unless we displayed some mode lines. */
8555 update_single_window (w, 1);
8556 rif->flush_display (f);
8558 else
8559 update_frame (f, 1, 1);
8561 /* If cursor is in the echo area, make sure that the next
8562 redisplay displays the minibuffer, so that the cursor will
8563 be replaced with what the minibuffer wants. */
8564 if (cursor_in_echo_area)
8565 ++windows_or_buffers_changed;
8568 else if (!EQ (mini_window, selected_window))
8569 windows_or_buffers_changed++;
8571 /* The current message is now also the last one displayed. */
8572 echo_area_buffer[1] = echo_area_buffer[0];
8574 /* Prevent redisplay optimization in redisplay_internal by resetting
8575 this_line_start_pos. This is done because the mini-buffer now
8576 displays the message instead of its buffer text. */
8577 if (EQ (mini_window, selected_window))
8578 CHARPOS (this_line_start_pos) = 0;
8580 return window_height_changed_p;
8585 /***********************************************************************
8586 Mode Lines and Frame Titles
8587 ***********************************************************************/
8589 /* A buffer for constructing non-propertized mode-line strings and
8590 frame titles in it; allocated from the heap in init_xdisp and
8591 resized as needed in store_mode_line_noprop_char. */
8593 static char *mode_line_noprop_buf;
8595 /* The buffer's end, and a current output position in it. */
8597 static char *mode_line_noprop_buf_end;
8598 static char *mode_line_noprop_ptr;
8600 #define MODE_LINE_NOPROP_LEN(start) \
8601 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8603 static enum {
8604 MODE_LINE_DISPLAY = 0,
8605 MODE_LINE_TITLE,
8606 MODE_LINE_NOPROP,
8607 MODE_LINE_STRING
8608 } mode_line_target;
8610 /* Alist that caches the results of :propertize.
8611 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8612 static Lisp_Object mode_line_proptrans_alist;
8614 /* List of strings making up the mode-line. */
8615 static Lisp_Object mode_line_string_list;
8617 /* Base face property when building propertized mode line string. */
8618 static Lisp_Object mode_line_string_face;
8619 static Lisp_Object mode_line_string_face_prop;
8622 /* Unwind data for mode line strings */
8624 static Lisp_Object Vmode_line_unwind_vector;
8626 static Lisp_Object
8627 format_mode_line_unwind_data (obuf, save_proptrans)
8628 struct buffer *obuf;
8630 Lisp_Object vector;
8632 /* Reduce consing by keeping one vector in
8633 Vwith_echo_area_save_vector. */
8634 vector = Vmode_line_unwind_vector;
8635 Vmode_line_unwind_vector = Qnil;
8637 if (NILP (vector))
8638 vector = Fmake_vector (make_number (7), Qnil);
8640 AREF (vector, 0) = make_number (mode_line_target);
8641 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8642 AREF (vector, 2) = mode_line_string_list;
8643 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8644 AREF (vector, 4) = mode_line_string_face;
8645 AREF (vector, 5) = mode_line_string_face_prop;
8647 if (obuf)
8648 XSETBUFFER (AREF (vector, 6), obuf);
8649 else
8650 AREF (vector, 6) = Qnil;
8652 return vector;
8655 static Lisp_Object
8656 unwind_format_mode_line (vector)
8657 Lisp_Object vector;
8659 mode_line_target = XINT (AREF (vector, 0));
8660 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8661 mode_line_string_list = AREF (vector, 2);
8662 if (! EQ (AREF (vector, 3), Qt))
8663 mode_line_proptrans_alist = AREF (vector, 3);
8664 mode_line_string_face = AREF (vector, 4);
8665 mode_line_string_face_prop = AREF (vector, 5);
8667 if (!NILP (AREF (vector, 6)))
8669 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8670 AREF (vector, 6) = Qnil;
8673 Vmode_line_unwind_vector = vector;
8674 return Qnil;
8678 /* Store a single character C for the frame title in mode_line_noprop_buf.
8679 Re-allocate mode_line_noprop_buf if necessary. */
8681 static void
8682 #ifdef PROTOTYPES
8683 store_mode_line_noprop_char (char c)
8684 #else
8685 store_mode_line_noprop_char (c)
8686 char c;
8687 #endif
8689 /* If output position has reached the end of the allocated buffer,
8690 double the buffer's size. */
8691 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8693 int len = MODE_LINE_NOPROP_LEN (0);
8694 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8695 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8696 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8697 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8700 *mode_line_noprop_ptr++ = c;
8704 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8705 mode_line_noprop_ptr. STR is the string to store. Do not copy
8706 characters that yield more columns than PRECISION; PRECISION <= 0
8707 means copy the whole string. Pad with spaces until FIELD_WIDTH
8708 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8709 pad. Called from display_mode_element when it is used to build a
8710 frame title. */
8712 static int
8713 store_mode_line_noprop (str, field_width, precision)
8714 const unsigned char *str;
8715 int field_width, precision;
8717 int n = 0;
8718 int dummy, nbytes;
8720 /* Copy at most PRECISION chars from STR. */
8721 nbytes = strlen (str);
8722 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8723 while (nbytes--)
8724 store_mode_line_noprop_char (*str++);
8726 /* Fill up with spaces until FIELD_WIDTH reached. */
8727 while (field_width > 0
8728 && n < field_width)
8730 store_mode_line_noprop_char (' ');
8731 ++n;
8734 return n;
8737 /***********************************************************************
8738 Frame Titles
8739 ***********************************************************************/
8741 #ifdef HAVE_WINDOW_SYSTEM
8743 /* Set the title of FRAME, if it has changed. The title format is
8744 Vicon_title_format if FRAME is iconified, otherwise it is
8745 frame_title_format. */
8747 static void
8748 x_consider_frame_title (frame)
8749 Lisp_Object frame;
8751 struct frame *f = XFRAME (frame);
8753 if (FRAME_WINDOW_P (f)
8754 || FRAME_MINIBUF_ONLY_P (f)
8755 || f->explicit_name)
8757 /* Do we have more than one visible frame on this X display? */
8758 Lisp_Object tail;
8759 Lisp_Object fmt;
8760 int title_start;
8761 char *title;
8762 int len;
8763 struct it it;
8764 int count = SPECPDL_INDEX ();
8766 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8768 Lisp_Object other_frame = XCAR (tail);
8769 struct frame *tf = XFRAME (other_frame);
8771 if (tf != f
8772 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8773 && !FRAME_MINIBUF_ONLY_P (tf)
8774 && !EQ (other_frame, tip_frame)
8775 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8776 break;
8779 /* Set global variable indicating that multiple frames exist. */
8780 multiple_frames = CONSP (tail);
8782 /* Switch to the buffer of selected window of the frame. Set up
8783 mode_line_target so that display_mode_element will output into
8784 mode_line_noprop_buf; then display the title. */
8785 record_unwind_protect (unwind_format_mode_line,
8786 format_mode_line_unwind_data (current_buffer, 0));
8788 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8789 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8791 mode_line_target = MODE_LINE_TITLE;
8792 title_start = MODE_LINE_NOPROP_LEN (0);
8793 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8794 NULL, DEFAULT_FACE_ID);
8795 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8796 len = MODE_LINE_NOPROP_LEN (title_start);
8797 title = mode_line_noprop_buf + title_start;
8798 unbind_to (count, Qnil);
8800 /* Set the title only if it's changed. This avoids consing in
8801 the common case where it hasn't. (If it turns out that we've
8802 already wasted too much time by walking through the list with
8803 display_mode_element, then we might need to optimize at a
8804 higher level than this.) */
8805 if (! STRINGP (f->name)
8806 || SBYTES (f->name) != len
8807 || bcmp (title, SDATA (f->name), len) != 0)
8808 x_implicitly_set_name (f, make_string (title, len), Qnil);
8812 #endif /* not HAVE_WINDOW_SYSTEM */
8817 /***********************************************************************
8818 Menu Bars
8819 ***********************************************************************/
8822 /* Prepare for redisplay by updating menu-bar item lists when
8823 appropriate. This can call eval. */
8825 void
8826 prepare_menu_bars ()
8828 int all_windows;
8829 struct gcpro gcpro1, gcpro2;
8830 struct frame *f;
8831 Lisp_Object tooltip_frame;
8833 #ifdef HAVE_WINDOW_SYSTEM
8834 tooltip_frame = tip_frame;
8835 #else
8836 tooltip_frame = Qnil;
8837 #endif
8839 /* Update all frame titles based on their buffer names, etc. We do
8840 this before the menu bars so that the buffer-menu will show the
8841 up-to-date frame titles. */
8842 #ifdef HAVE_WINDOW_SYSTEM
8843 if (windows_or_buffers_changed || update_mode_lines)
8845 Lisp_Object tail, frame;
8847 FOR_EACH_FRAME (tail, frame)
8849 f = XFRAME (frame);
8850 if (!EQ (frame, tooltip_frame)
8851 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8852 x_consider_frame_title (frame);
8855 #endif /* HAVE_WINDOW_SYSTEM */
8857 /* Update the menu bar item lists, if appropriate. This has to be
8858 done before any actual redisplay or generation of display lines. */
8859 all_windows = (update_mode_lines
8860 || buffer_shared > 1
8861 || windows_or_buffers_changed);
8862 if (all_windows)
8864 Lisp_Object tail, frame;
8865 int count = SPECPDL_INDEX ();
8867 record_unwind_save_match_data ();
8869 FOR_EACH_FRAME (tail, frame)
8871 f = XFRAME (frame);
8873 /* Ignore tooltip frame. */
8874 if (EQ (frame, tooltip_frame))
8875 continue;
8877 /* If a window on this frame changed size, report that to
8878 the user and clear the size-change flag. */
8879 if (FRAME_WINDOW_SIZES_CHANGED (f))
8881 Lisp_Object functions;
8883 /* Clear flag first in case we get an error below. */
8884 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8885 functions = Vwindow_size_change_functions;
8886 GCPRO2 (tail, functions);
8888 while (CONSP (functions))
8890 call1 (XCAR (functions), frame);
8891 functions = XCDR (functions);
8893 UNGCPRO;
8896 GCPRO1 (tail);
8897 update_menu_bar (f, 0);
8898 #ifdef HAVE_WINDOW_SYSTEM
8899 update_tool_bar (f, 0);
8900 #endif
8901 UNGCPRO;
8904 unbind_to (count, Qnil);
8906 else
8908 struct frame *sf = SELECTED_FRAME ();
8909 update_menu_bar (sf, 1);
8910 #ifdef HAVE_WINDOW_SYSTEM
8911 update_tool_bar (sf, 1);
8912 #endif
8915 /* Motif needs this. See comment in xmenu.c. Turn it off when
8916 pending_menu_activation is not defined. */
8917 #ifdef USE_X_TOOLKIT
8918 pending_menu_activation = 0;
8919 #endif
8923 /* Update the menu bar item list for frame F. This has to be done
8924 before we start to fill in any display lines, because it can call
8925 eval.
8927 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8929 static void
8930 update_menu_bar (f, save_match_data)
8931 struct frame *f;
8932 int save_match_data;
8934 Lisp_Object window;
8935 register struct window *w;
8937 /* If called recursively during a menu update, do nothing. This can
8938 happen when, for instance, an activate-menubar-hook causes a
8939 redisplay. */
8940 if (inhibit_menubar_update)
8941 return;
8943 window = FRAME_SELECTED_WINDOW (f);
8944 w = XWINDOW (window);
8946 #if 0 /* The if statement below this if statement used to include the
8947 condition !NILP (w->update_mode_line), rather than using
8948 update_mode_lines directly, and this if statement may have
8949 been added to make that condition work. Now the if
8950 statement below matches its comment, this isn't needed. */
8951 if (update_mode_lines)
8952 w->update_mode_line = Qt;
8953 #endif
8955 if (FRAME_WINDOW_P (f)
8957 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8958 || defined (USE_GTK)
8959 FRAME_EXTERNAL_MENU_BAR (f)
8960 #else
8961 FRAME_MENU_BAR_LINES (f) > 0
8962 #endif
8963 : FRAME_MENU_BAR_LINES (f) > 0)
8965 /* If the user has switched buffers or windows, we need to
8966 recompute to reflect the new bindings. But we'll
8967 recompute when update_mode_lines is set too; that means
8968 that people can use force-mode-line-update to request
8969 that the menu bar be recomputed. The adverse effect on
8970 the rest of the redisplay algorithm is about the same as
8971 windows_or_buffers_changed anyway. */
8972 if (windows_or_buffers_changed
8973 /* This used to test w->update_mode_line, but we believe
8974 there is no need to recompute the menu in that case. */
8975 || update_mode_lines
8976 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8977 < BUF_MODIFF (XBUFFER (w->buffer)))
8978 != !NILP (w->last_had_star))
8979 || ((!NILP (Vtransient_mark_mode)
8980 && !NILP (XBUFFER (w->buffer)->mark_active))
8981 != !NILP (w->region_showing)))
8983 struct buffer *prev = current_buffer;
8984 int count = SPECPDL_INDEX ();
8986 specbind (Qinhibit_menubar_update, Qt);
8988 set_buffer_internal_1 (XBUFFER (w->buffer));
8989 if (save_match_data)
8990 record_unwind_save_match_data ();
8991 if (NILP (Voverriding_local_map_menu_flag))
8993 specbind (Qoverriding_terminal_local_map, Qnil);
8994 specbind (Qoverriding_local_map, Qnil);
8997 /* Run the Lucid hook. */
8998 safe_run_hooks (Qactivate_menubar_hook);
9000 /* If it has changed current-menubar from previous value,
9001 really recompute the menu-bar from the value. */
9002 if (! NILP (Vlucid_menu_bar_dirty_flag))
9003 call0 (Qrecompute_lucid_menubar);
9005 safe_run_hooks (Qmenu_bar_update_hook);
9006 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9008 /* Redisplay the menu bar in case we changed it. */
9009 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9010 || defined (USE_GTK)
9011 if (FRAME_WINDOW_P (f)
9012 #if defined (MAC_OS)
9013 /* All frames on Mac OS share the same menubar. So only the
9014 selected frame should be allowed to set it. */
9015 && f == SELECTED_FRAME ()
9016 #endif
9018 set_frame_menubar (f, 0, 0);
9019 else
9020 /* On a terminal screen, the menu bar is an ordinary screen
9021 line, and this makes it get updated. */
9022 w->update_mode_line = Qt;
9023 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9024 /* In the non-toolkit version, the menu bar is an ordinary screen
9025 line, and this makes it get updated. */
9026 w->update_mode_line = Qt;
9027 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9029 unbind_to (count, Qnil);
9030 set_buffer_internal_1 (prev);
9037 /***********************************************************************
9038 Output Cursor
9039 ***********************************************************************/
9041 #ifdef HAVE_WINDOW_SYSTEM
9043 /* EXPORT:
9044 Nominal cursor position -- where to draw output.
9045 HPOS and VPOS are window relative glyph matrix coordinates.
9046 X and Y are window relative pixel coordinates. */
9048 struct cursor_pos output_cursor;
9051 /* EXPORT:
9052 Set the global variable output_cursor to CURSOR. All cursor
9053 positions are relative to updated_window. */
9055 void
9056 set_output_cursor (cursor)
9057 struct cursor_pos *cursor;
9059 output_cursor.hpos = cursor->hpos;
9060 output_cursor.vpos = cursor->vpos;
9061 output_cursor.x = cursor->x;
9062 output_cursor.y = cursor->y;
9066 /* EXPORT for RIF:
9067 Set a nominal cursor position.
9069 HPOS and VPOS are column/row positions in a window glyph matrix. X
9070 and Y are window text area relative pixel positions.
9072 If this is done during an update, updated_window will contain the
9073 window that is being updated and the position is the future output
9074 cursor position for that window. If updated_window is null, use
9075 selected_window and display the cursor at the given position. */
9077 void
9078 x_cursor_to (vpos, hpos, y, x)
9079 int vpos, hpos, y, x;
9081 struct window *w;
9083 /* If updated_window is not set, work on selected_window. */
9084 if (updated_window)
9085 w = updated_window;
9086 else
9087 w = XWINDOW (selected_window);
9089 /* Set the output cursor. */
9090 output_cursor.hpos = hpos;
9091 output_cursor.vpos = vpos;
9092 output_cursor.x = x;
9093 output_cursor.y = y;
9095 /* If not called as part of an update, really display the cursor.
9096 This will also set the cursor position of W. */
9097 if (updated_window == NULL)
9099 BLOCK_INPUT;
9100 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9101 if (rif->flush_display_optional)
9102 rif->flush_display_optional (SELECTED_FRAME ());
9103 UNBLOCK_INPUT;
9107 #endif /* HAVE_WINDOW_SYSTEM */
9110 /***********************************************************************
9111 Tool-bars
9112 ***********************************************************************/
9114 #ifdef HAVE_WINDOW_SYSTEM
9116 /* Where the mouse was last time we reported a mouse event. */
9118 FRAME_PTR last_mouse_frame;
9120 /* Tool-bar item index of the item on which a mouse button was pressed
9121 or -1. */
9123 int last_tool_bar_item;
9126 /* Update the tool-bar item list for frame F. This has to be done
9127 before we start to fill in any display lines. Called from
9128 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9129 and restore it here. */
9131 static void
9132 update_tool_bar (f, save_match_data)
9133 struct frame *f;
9134 int save_match_data;
9136 #ifdef USE_GTK
9137 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9138 #else
9139 int do_update = WINDOWP (f->tool_bar_window)
9140 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9141 #endif
9143 if (do_update)
9145 Lisp_Object window;
9146 struct window *w;
9148 window = FRAME_SELECTED_WINDOW (f);
9149 w = XWINDOW (window);
9151 /* If the user has switched buffers or windows, we need to
9152 recompute to reflect the new bindings. But we'll
9153 recompute when update_mode_lines is set too; that means
9154 that people can use force-mode-line-update to request
9155 that the menu bar be recomputed. The adverse effect on
9156 the rest of the redisplay algorithm is about the same as
9157 windows_or_buffers_changed anyway. */
9158 if (windows_or_buffers_changed
9159 || !NILP (w->update_mode_line)
9160 || update_mode_lines
9161 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9162 < BUF_MODIFF (XBUFFER (w->buffer)))
9163 != !NILP (w->last_had_star))
9164 || ((!NILP (Vtransient_mark_mode)
9165 && !NILP (XBUFFER (w->buffer)->mark_active))
9166 != !NILP (w->region_showing)))
9168 struct buffer *prev = current_buffer;
9169 int count = SPECPDL_INDEX ();
9170 Lisp_Object new_tool_bar;
9171 int new_n_tool_bar;
9172 struct gcpro gcpro1;
9174 /* Set current_buffer to the buffer of the selected
9175 window of the frame, so that we get the right local
9176 keymaps. */
9177 set_buffer_internal_1 (XBUFFER (w->buffer));
9179 /* Save match data, if we must. */
9180 if (save_match_data)
9181 record_unwind_save_match_data ();
9183 /* Make sure that we don't accidentally use bogus keymaps. */
9184 if (NILP (Voverriding_local_map_menu_flag))
9186 specbind (Qoverriding_terminal_local_map, Qnil);
9187 specbind (Qoverriding_local_map, Qnil);
9190 GCPRO1 (new_tool_bar);
9192 /* Build desired tool-bar items from keymaps. */
9193 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9194 &new_n_tool_bar);
9196 /* Redisplay the tool-bar if we changed it. */
9197 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9199 /* Redisplay that happens asynchronously due to an expose event
9200 may access f->tool_bar_items. Make sure we update both
9201 variables within BLOCK_INPUT so no such event interrupts. */
9202 BLOCK_INPUT;
9203 f->tool_bar_items = new_tool_bar;
9204 f->n_tool_bar_items = new_n_tool_bar;
9205 w->update_mode_line = Qt;
9206 UNBLOCK_INPUT;
9209 UNGCPRO;
9211 unbind_to (count, Qnil);
9212 set_buffer_internal_1 (prev);
9218 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9219 F's desired tool-bar contents. F->tool_bar_items must have
9220 been set up previously by calling prepare_menu_bars. */
9222 static void
9223 build_desired_tool_bar_string (f)
9224 struct frame *f;
9226 int i, size, size_needed;
9227 struct gcpro gcpro1, gcpro2, gcpro3;
9228 Lisp_Object image, plist, props;
9230 image = plist = props = Qnil;
9231 GCPRO3 (image, plist, props);
9233 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9234 Otherwise, make a new string. */
9236 /* The size of the string we might be able to reuse. */
9237 size = (STRINGP (f->desired_tool_bar_string)
9238 ? SCHARS (f->desired_tool_bar_string)
9239 : 0);
9241 /* We need one space in the string for each image. */
9242 size_needed = f->n_tool_bar_items;
9244 /* Reuse f->desired_tool_bar_string, if possible. */
9245 if (size < size_needed || NILP (f->desired_tool_bar_string))
9246 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9247 make_number (' '));
9248 else
9250 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9251 Fremove_text_properties (make_number (0), make_number (size),
9252 props, f->desired_tool_bar_string);
9255 /* Put a `display' property on the string for the images to display,
9256 put a `menu_item' property on tool-bar items with a value that
9257 is the index of the item in F's tool-bar item vector. */
9258 for (i = 0; i < f->n_tool_bar_items; ++i)
9260 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9262 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9263 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9264 int hmargin, vmargin, relief, idx, end;
9265 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9267 /* If image is a vector, choose the image according to the
9268 button state. */
9269 image = PROP (TOOL_BAR_ITEM_IMAGES);
9270 if (VECTORP (image))
9272 if (enabled_p)
9273 idx = (selected_p
9274 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9275 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9276 else
9277 idx = (selected_p
9278 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9279 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9281 xassert (ASIZE (image) >= idx);
9282 image = AREF (image, idx);
9284 else
9285 idx = -1;
9287 /* Ignore invalid image specifications. */
9288 if (!valid_image_p (image))
9289 continue;
9291 /* Display the tool-bar button pressed, or depressed. */
9292 plist = Fcopy_sequence (XCDR (image));
9294 /* Compute margin and relief to draw. */
9295 relief = (tool_bar_button_relief >= 0
9296 ? tool_bar_button_relief
9297 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9298 hmargin = vmargin = relief;
9300 if (INTEGERP (Vtool_bar_button_margin)
9301 && XINT (Vtool_bar_button_margin) > 0)
9303 hmargin += XFASTINT (Vtool_bar_button_margin);
9304 vmargin += XFASTINT (Vtool_bar_button_margin);
9306 else if (CONSP (Vtool_bar_button_margin))
9308 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9309 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9310 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9312 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9313 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9314 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9317 if (auto_raise_tool_bar_buttons_p)
9319 /* Add a `:relief' property to the image spec if the item is
9320 selected. */
9321 if (selected_p)
9323 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9324 hmargin -= relief;
9325 vmargin -= relief;
9328 else
9330 /* If image is selected, display it pressed, i.e. with a
9331 negative relief. If it's not selected, display it with a
9332 raised relief. */
9333 plist = Fplist_put (plist, QCrelief,
9334 (selected_p
9335 ? make_number (-relief)
9336 : make_number (relief)));
9337 hmargin -= relief;
9338 vmargin -= relief;
9341 /* Put a margin around the image. */
9342 if (hmargin || vmargin)
9344 if (hmargin == vmargin)
9345 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9346 else
9347 plist = Fplist_put (plist, QCmargin,
9348 Fcons (make_number (hmargin),
9349 make_number (vmargin)));
9352 /* If button is not enabled, and we don't have special images
9353 for the disabled state, make the image appear disabled by
9354 applying an appropriate algorithm to it. */
9355 if (!enabled_p && idx < 0)
9356 plist = Fplist_put (plist, QCconversion, Qdisabled);
9358 /* Put a `display' text property on the string for the image to
9359 display. Put a `menu-item' property on the string that gives
9360 the start of this item's properties in the tool-bar items
9361 vector. */
9362 image = Fcons (Qimage, plist);
9363 props = list4 (Qdisplay, image,
9364 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9366 /* Let the last image hide all remaining spaces in the tool bar
9367 string. The string can be longer than needed when we reuse a
9368 previous string. */
9369 if (i + 1 == f->n_tool_bar_items)
9370 end = SCHARS (f->desired_tool_bar_string);
9371 else
9372 end = i + 1;
9373 Fadd_text_properties (make_number (i), make_number (end),
9374 props, f->desired_tool_bar_string);
9375 #undef PROP
9378 UNGCPRO;
9382 /* Display one line of the tool-bar of frame IT->f. */
9384 static void
9385 display_tool_bar_line (it)
9386 struct it *it;
9388 struct glyph_row *row = it->glyph_row;
9389 int max_x = it->last_visible_x;
9390 struct glyph *last;
9392 prepare_desired_row (row);
9393 row->y = it->current_y;
9395 /* Note that this isn't made use of if the face hasn't a box,
9396 so there's no need to check the face here. */
9397 it->start_of_box_run_p = 1;
9399 while (it->current_x < max_x)
9401 int x_before, x, n_glyphs_before, i, nglyphs;
9403 /* Get the next display element. */
9404 if (!get_next_display_element (it))
9405 break;
9407 /* Produce glyphs. */
9408 x_before = it->current_x;
9409 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9410 PRODUCE_GLYPHS (it);
9412 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9413 i = 0;
9414 x = x_before;
9415 while (i < nglyphs)
9417 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9419 if (x + glyph->pixel_width > max_x)
9421 /* Glyph doesn't fit on line. */
9422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9423 it->current_x = x;
9424 goto out;
9427 ++it->hpos;
9428 x += glyph->pixel_width;
9429 ++i;
9432 /* Stop at line ends. */
9433 if (ITERATOR_AT_END_OF_LINE_P (it))
9434 break;
9436 set_iterator_to_next (it, 1);
9439 out:;
9441 row->displays_text_p = row->used[TEXT_AREA] != 0;
9442 extend_face_to_end_of_line (it);
9443 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9444 last->right_box_line_p = 1;
9445 if (last == row->glyphs[TEXT_AREA])
9446 last->left_box_line_p = 1;
9447 compute_line_metrics (it);
9449 /* If line is empty, make it occupy the rest of the tool-bar. */
9450 if (!row->displays_text_p)
9452 row->height = row->phys_height = it->last_visible_y - row->y;
9453 row->ascent = row->phys_ascent = 0;
9454 row->extra_line_spacing = 0;
9457 row->full_width_p = 1;
9458 row->continued_p = 0;
9459 row->truncated_on_left_p = 0;
9460 row->truncated_on_right_p = 0;
9462 it->current_x = it->hpos = 0;
9463 it->current_y += row->height;
9464 ++it->vpos;
9465 ++it->glyph_row;
9469 /* Value is the number of screen lines needed to make all tool-bar
9470 items of frame F visible. */
9472 static int
9473 tool_bar_lines_needed (f)
9474 struct frame *f;
9476 struct window *w = XWINDOW (f->tool_bar_window);
9477 struct it it;
9479 /* Initialize an iterator for iteration over
9480 F->desired_tool_bar_string in the tool-bar window of frame F. */
9481 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9482 it.first_visible_x = 0;
9483 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9484 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9486 while (!ITERATOR_AT_END_P (&it))
9488 it.glyph_row = w->desired_matrix->rows;
9489 clear_glyph_row (it.glyph_row);
9490 display_tool_bar_line (&it);
9493 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9497 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9498 0, 1, 0,
9499 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9500 (frame)
9501 Lisp_Object frame;
9503 struct frame *f;
9504 struct window *w;
9505 int nlines = 0;
9507 if (NILP (frame))
9508 frame = selected_frame;
9509 else
9510 CHECK_FRAME (frame);
9511 f = XFRAME (frame);
9513 if (WINDOWP (f->tool_bar_window)
9514 || (w = XWINDOW (f->tool_bar_window),
9515 WINDOW_TOTAL_LINES (w) > 0))
9517 update_tool_bar (f, 1);
9518 if (f->n_tool_bar_items)
9520 build_desired_tool_bar_string (f);
9521 nlines = tool_bar_lines_needed (f);
9525 return make_number (nlines);
9529 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9530 height should be changed. */
9532 static int
9533 redisplay_tool_bar (f)
9534 struct frame *f;
9536 struct window *w;
9537 struct it it;
9538 struct glyph_row *row;
9539 int change_height_p = 0;
9541 #ifdef USE_GTK
9542 if (FRAME_EXTERNAL_TOOL_BAR (f))
9543 update_frame_tool_bar (f);
9544 return 0;
9545 #endif
9547 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9548 do anything. This means you must start with tool-bar-lines
9549 non-zero to get the auto-sizing effect. Or in other words, you
9550 can turn off tool-bars by specifying tool-bar-lines zero. */
9551 if (!WINDOWP (f->tool_bar_window)
9552 || (w = XWINDOW (f->tool_bar_window),
9553 WINDOW_TOTAL_LINES (w) == 0))
9554 return 0;
9556 /* Set up an iterator for the tool-bar window. */
9557 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9558 it.first_visible_x = 0;
9559 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9560 row = it.glyph_row;
9562 /* Build a string that represents the contents of the tool-bar. */
9563 build_desired_tool_bar_string (f);
9564 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9566 /* Display as many lines as needed to display all tool-bar items. */
9567 while (it.current_y < it.last_visible_y)
9568 display_tool_bar_line (&it);
9570 /* It doesn't make much sense to try scrolling in the tool-bar
9571 window, so don't do it. */
9572 w->desired_matrix->no_scrolling_p = 1;
9573 w->must_be_updated_p = 1;
9575 if (auto_resize_tool_bars_p)
9577 int nlines;
9579 /* If we couldn't display everything, change the tool-bar's
9580 height. */
9581 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9582 change_height_p = 1;
9584 /* If there are blank lines at the end, except for a partially
9585 visible blank line at the end that is smaller than
9586 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9587 row = it.glyph_row - 1;
9588 if (!row->displays_text_p
9589 && row->height >= FRAME_LINE_HEIGHT (f))
9590 change_height_p = 1;
9592 /* If row displays tool-bar items, but is partially visible,
9593 change the tool-bar's height. */
9594 if (row->displays_text_p
9595 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9596 change_height_p = 1;
9598 /* Resize windows as needed by changing the `tool-bar-lines'
9599 frame parameter. */
9600 if (change_height_p
9601 && (nlines = tool_bar_lines_needed (f),
9602 nlines != WINDOW_TOTAL_LINES (w)))
9604 extern Lisp_Object Qtool_bar_lines;
9605 Lisp_Object frame;
9606 int old_height = WINDOW_TOTAL_LINES (w);
9608 XSETFRAME (frame, f);
9609 clear_glyph_matrix (w->desired_matrix);
9610 Fmodify_frame_parameters (frame,
9611 Fcons (Fcons (Qtool_bar_lines,
9612 make_number (nlines)),
9613 Qnil));
9614 if (WINDOW_TOTAL_LINES (w) != old_height)
9615 fonts_changed_p = 1;
9619 return change_height_p;
9623 /* Get information about the tool-bar item which is displayed in GLYPH
9624 on frame F. Return in *PROP_IDX the index where tool-bar item
9625 properties start in F->tool_bar_items. Value is zero if
9626 GLYPH doesn't display a tool-bar item. */
9628 static int
9629 tool_bar_item_info (f, glyph, prop_idx)
9630 struct frame *f;
9631 struct glyph *glyph;
9632 int *prop_idx;
9634 Lisp_Object prop;
9635 int success_p;
9636 int charpos;
9638 /* This function can be called asynchronously, which means we must
9639 exclude any possibility that Fget_text_property signals an
9640 error. */
9641 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9642 charpos = max (0, charpos);
9644 /* Get the text property `menu-item' at pos. The value of that
9645 property is the start index of this item's properties in
9646 F->tool_bar_items. */
9647 prop = Fget_text_property (make_number (charpos),
9648 Qmenu_item, f->current_tool_bar_string);
9649 if (INTEGERP (prop))
9651 *prop_idx = XINT (prop);
9652 success_p = 1;
9654 else
9655 success_p = 0;
9657 return success_p;
9661 /* Get information about the tool-bar item at position X/Y on frame F.
9662 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9663 the current matrix of the tool-bar window of F, or NULL if not
9664 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9665 item in F->tool_bar_items. Value is
9667 -1 if X/Y is not on a tool-bar item
9668 0 if X/Y is on the same item that was highlighted before.
9669 1 otherwise. */
9671 static int
9672 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9673 struct frame *f;
9674 int x, y;
9675 struct glyph **glyph;
9676 int *hpos, *vpos, *prop_idx;
9678 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9679 struct window *w = XWINDOW (f->tool_bar_window);
9680 int area;
9682 /* Find the glyph under X/Y. */
9683 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9684 if (*glyph == NULL)
9685 return -1;
9687 /* Get the start of this tool-bar item's properties in
9688 f->tool_bar_items. */
9689 if (!tool_bar_item_info (f, *glyph, prop_idx))
9690 return -1;
9692 /* Is mouse on the highlighted item? */
9693 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9694 && *vpos >= dpyinfo->mouse_face_beg_row
9695 && *vpos <= dpyinfo->mouse_face_end_row
9696 && (*vpos > dpyinfo->mouse_face_beg_row
9697 || *hpos >= dpyinfo->mouse_face_beg_col)
9698 && (*vpos < dpyinfo->mouse_face_end_row
9699 || *hpos < dpyinfo->mouse_face_end_col
9700 || dpyinfo->mouse_face_past_end))
9701 return 0;
9703 return 1;
9707 /* EXPORT:
9708 Handle mouse button event on the tool-bar of frame F, at
9709 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9710 0 for button release. MODIFIERS is event modifiers for button
9711 release. */
9713 void
9714 handle_tool_bar_click (f, x, y, down_p, modifiers)
9715 struct frame *f;
9716 int x, y, down_p;
9717 unsigned int modifiers;
9719 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9720 struct window *w = XWINDOW (f->tool_bar_window);
9721 int hpos, vpos, prop_idx;
9722 struct glyph *glyph;
9723 Lisp_Object enabled_p;
9725 /* If not on the highlighted tool-bar item, return. */
9726 frame_to_window_pixel_xy (w, &x, &y);
9727 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9728 return;
9730 /* If item is disabled, do nothing. */
9731 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9732 if (NILP (enabled_p))
9733 return;
9735 if (down_p)
9737 /* Show item in pressed state. */
9738 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9739 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9740 last_tool_bar_item = prop_idx;
9742 else
9744 Lisp_Object key, frame;
9745 struct input_event event;
9746 EVENT_INIT (event);
9748 /* Show item in released state. */
9749 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9750 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9752 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9754 XSETFRAME (frame, f);
9755 event.kind = TOOL_BAR_EVENT;
9756 event.frame_or_window = frame;
9757 event.arg = frame;
9758 kbd_buffer_store_event (&event);
9760 event.kind = TOOL_BAR_EVENT;
9761 event.frame_or_window = frame;
9762 event.arg = key;
9763 event.modifiers = modifiers;
9764 kbd_buffer_store_event (&event);
9765 last_tool_bar_item = -1;
9770 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9771 tool-bar window-relative coordinates X/Y. Called from
9772 note_mouse_highlight. */
9774 static void
9775 note_tool_bar_highlight (f, x, y)
9776 struct frame *f;
9777 int x, y;
9779 Lisp_Object window = f->tool_bar_window;
9780 struct window *w = XWINDOW (window);
9781 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9782 int hpos, vpos;
9783 struct glyph *glyph;
9784 struct glyph_row *row;
9785 int i;
9786 Lisp_Object enabled_p;
9787 int prop_idx;
9788 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9789 int mouse_down_p, rc;
9791 /* Function note_mouse_highlight is called with negative x(y
9792 values when mouse moves outside of the frame. */
9793 if (x <= 0 || y <= 0)
9795 clear_mouse_face (dpyinfo);
9796 return;
9799 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9800 if (rc < 0)
9802 /* Not on tool-bar item. */
9803 clear_mouse_face (dpyinfo);
9804 return;
9806 else if (rc == 0)
9807 /* On same tool-bar item as before. */
9808 goto set_help_echo;
9810 clear_mouse_face (dpyinfo);
9812 /* Mouse is down, but on different tool-bar item? */
9813 mouse_down_p = (dpyinfo->grabbed
9814 && f == last_mouse_frame
9815 && FRAME_LIVE_P (f));
9816 if (mouse_down_p
9817 && last_tool_bar_item != prop_idx)
9818 return;
9820 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9821 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9823 /* If tool-bar item is not enabled, don't highlight it. */
9824 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9825 if (!NILP (enabled_p))
9827 /* Compute the x-position of the glyph. In front and past the
9828 image is a space. We include this in the highlighted area. */
9829 row = MATRIX_ROW (w->current_matrix, vpos);
9830 for (i = x = 0; i < hpos; ++i)
9831 x += row->glyphs[TEXT_AREA][i].pixel_width;
9833 /* Record this as the current active region. */
9834 dpyinfo->mouse_face_beg_col = hpos;
9835 dpyinfo->mouse_face_beg_row = vpos;
9836 dpyinfo->mouse_face_beg_x = x;
9837 dpyinfo->mouse_face_beg_y = row->y;
9838 dpyinfo->mouse_face_past_end = 0;
9840 dpyinfo->mouse_face_end_col = hpos + 1;
9841 dpyinfo->mouse_face_end_row = vpos;
9842 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9843 dpyinfo->mouse_face_end_y = row->y;
9844 dpyinfo->mouse_face_window = window;
9845 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9847 /* Display it as active. */
9848 show_mouse_face (dpyinfo, draw);
9849 dpyinfo->mouse_face_image_state = draw;
9852 set_help_echo:
9854 /* Set help_echo_string to a help string to display for this tool-bar item.
9855 XTread_socket does the rest. */
9856 help_echo_object = help_echo_window = Qnil;
9857 help_echo_pos = -1;
9858 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9859 if (NILP (help_echo_string))
9860 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9863 #endif /* HAVE_WINDOW_SYSTEM */
9867 /************************************************************************
9868 Horizontal scrolling
9869 ************************************************************************/
9871 static int hscroll_window_tree P_ ((Lisp_Object));
9872 static int hscroll_windows P_ ((Lisp_Object));
9874 /* For all leaf windows in the window tree rooted at WINDOW, set their
9875 hscroll value so that PT is (i) visible in the window, and (ii) so
9876 that it is not within a certain margin at the window's left and
9877 right border. Value is non-zero if any window's hscroll has been
9878 changed. */
9880 static int
9881 hscroll_window_tree (window)
9882 Lisp_Object window;
9884 int hscrolled_p = 0;
9885 int hscroll_relative_p = FLOATP (Vhscroll_step);
9886 int hscroll_step_abs = 0;
9887 double hscroll_step_rel = 0;
9889 if (hscroll_relative_p)
9891 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9892 if (hscroll_step_rel < 0)
9894 hscroll_relative_p = 0;
9895 hscroll_step_abs = 0;
9898 else if (INTEGERP (Vhscroll_step))
9900 hscroll_step_abs = XINT (Vhscroll_step);
9901 if (hscroll_step_abs < 0)
9902 hscroll_step_abs = 0;
9904 else
9905 hscroll_step_abs = 0;
9907 while (WINDOWP (window))
9909 struct window *w = XWINDOW (window);
9911 if (WINDOWP (w->hchild))
9912 hscrolled_p |= hscroll_window_tree (w->hchild);
9913 else if (WINDOWP (w->vchild))
9914 hscrolled_p |= hscroll_window_tree (w->vchild);
9915 else if (w->cursor.vpos >= 0)
9917 int h_margin;
9918 int text_area_width;
9919 struct glyph_row *current_cursor_row
9920 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9921 struct glyph_row *desired_cursor_row
9922 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9923 struct glyph_row *cursor_row
9924 = (desired_cursor_row->enabled_p
9925 ? desired_cursor_row
9926 : current_cursor_row);
9928 text_area_width = window_box_width (w, TEXT_AREA);
9930 /* Scroll when cursor is inside this scroll margin. */
9931 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9933 if ((XFASTINT (w->hscroll)
9934 && w->cursor.x <= h_margin)
9935 || (cursor_row->enabled_p
9936 && cursor_row->truncated_on_right_p
9937 && (w->cursor.x >= text_area_width - h_margin)))
9939 struct it it;
9940 int hscroll;
9941 struct buffer *saved_current_buffer;
9942 int pt;
9943 int wanted_x;
9945 /* Find point in a display of infinite width. */
9946 saved_current_buffer = current_buffer;
9947 current_buffer = XBUFFER (w->buffer);
9949 if (w == XWINDOW (selected_window))
9950 pt = BUF_PT (current_buffer);
9951 else
9953 pt = marker_position (w->pointm);
9954 pt = max (BEGV, pt);
9955 pt = min (ZV, pt);
9958 /* Move iterator to pt starting at cursor_row->start in
9959 a line with infinite width. */
9960 init_to_row_start (&it, w, cursor_row);
9961 it.last_visible_x = INFINITY;
9962 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9963 current_buffer = saved_current_buffer;
9965 /* Position cursor in window. */
9966 if (!hscroll_relative_p && hscroll_step_abs == 0)
9967 hscroll = max (0, (it.current_x
9968 - (ITERATOR_AT_END_OF_LINE_P (&it)
9969 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9970 : (text_area_width / 2))))
9971 / FRAME_COLUMN_WIDTH (it.f);
9972 else if (w->cursor.x >= text_area_width - h_margin)
9974 if (hscroll_relative_p)
9975 wanted_x = text_area_width * (1 - hscroll_step_rel)
9976 - h_margin;
9977 else
9978 wanted_x = text_area_width
9979 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9980 - h_margin;
9981 hscroll
9982 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9984 else
9986 if (hscroll_relative_p)
9987 wanted_x = text_area_width * hscroll_step_rel
9988 + h_margin;
9989 else
9990 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9991 + h_margin;
9992 hscroll
9993 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9995 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9997 /* Don't call Fset_window_hscroll if value hasn't
9998 changed because it will prevent redisplay
9999 optimizations. */
10000 if (XFASTINT (w->hscroll) != hscroll)
10002 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10003 w->hscroll = make_number (hscroll);
10004 hscrolled_p = 1;
10009 window = w->next;
10012 /* Value is non-zero if hscroll of any leaf window has been changed. */
10013 return hscrolled_p;
10017 /* Set hscroll so that cursor is visible and not inside horizontal
10018 scroll margins for all windows in the tree rooted at WINDOW. See
10019 also hscroll_window_tree above. Value is non-zero if any window's
10020 hscroll has been changed. If it has, desired matrices on the frame
10021 of WINDOW are cleared. */
10023 static int
10024 hscroll_windows (window)
10025 Lisp_Object window;
10027 int hscrolled_p;
10029 if (automatic_hscrolling_p)
10031 hscrolled_p = hscroll_window_tree (window);
10032 if (hscrolled_p)
10033 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10035 else
10036 hscrolled_p = 0;
10037 return hscrolled_p;
10042 /************************************************************************
10043 Redisplay
10044 ************************************************************************/
10046 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10047 to a non-zero value. This is sometimes handy to have in a debugger
10048 session. */
10050 #if GLYPH_DEBUG
10052 /* First and last unchanged row for try_window_id. */
10054 int debug_first_unchanged_at_end_vpos;
10055 int debug_last_unchanged_at_beg_vpos;
10057 /* Delta vpos and y. */
10059 int debug_dvpos, debug_dy;
10061 /* Delta in characters and bytes for try_window_id. */
10063 int debug_delta, debug_delta_bytes;
10065 /* Values of window_end_pos and window_end_vpos at the end of
10066 try_window_id. */
10068 EMACS_INT debug_end_pos, debug_end_vpos;
10070 /* Append a string to W->desired_matrix->method. FMT is a printf
10071 format string. A1...A9 are a supplement for a variable-length
10072 argument list. If trace_redisplay_p is non-zero also printf the
10073 resulting string to stderr. */
10075 static void
10076 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10077 struct window *w;
10078 char *fmt;
10079 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10081 char buffer[512];
10082 char *method = w->desired_matrix->method;
10083 int len = strlen (method);
10084 int size = sizeof w->desired_matrix->method;
10085 int remaining = size - len - 1;
10087 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10088 if (len && remaining)
10090 method[len] = '|';
10091 --remaining, ++len;
10094 strncpy (method + len, buffer, remaining);
10096 if (trace_redisplay_p)
10097 fprintf (stderr, "%p (%s): %s\n",
10099 ((BUFFERP (w->buffer)
10100 && STRINGP (XBUFFER (w->buffer)->name))
10101 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10102 : "no buffer"),
10103 buffer);
10106 #endif /* GLYPH_DEBUG */
10109 /* Value is non-zero if all changes in window W, which displays
10110 current_buffer, are in the text between START and END. START is a
10111 buffer position, END is given as a distance from Z. Used in
10112 redisplay_internal for display optimization. */
10114 static INLINE int
10115 text_outside_line_unchanged_p (w, start, end)
10116 struct window *w;
10117 int start, end;
10119 int unchanged_p = 1;
10121 /* If text or overlays have changed, see where. */
10122 if (XFASTINT (w->last_modified) < MODIFF
10123 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10125 /* Gap in the line? */
10126 if (GPT < start || Z - GPT < end)
10127 unchanged_p = 0;
10129 /* Changes start in front of the line, or end after it? */
10130 if (unchanged_p
10131 && (BEG_UNCHANGED < start - 1
10132 || END_UNCHANGED < end))
10133 unchanged_p = 0;
10135 /* If selective display, can't optimize if changes start at the
10136 beginning of the line. */
10137 if (unchanged_p
10138 && INTEGERP (current_buffer->selective_display)
10139 && XINT (current_buffer->selective_display) > 0
10140 && (BEG_UNCHANGED < start || GPT <= start))
10141 unchanged_p = 0;
10143 /* If there are overlays at the start or end of the line, these
10144 may have overlay strings with newlines in them. A change at
10145 START, for instance, may actually concern the display of such
10146 overlay strings as well, and they are displayed on different
10147 lines. So, quickly rule out this case. (For the future, it
10148 might be desirable to implement something more telling than
10149 just BEG/END_UNCHANGED.) */
10150 if (unchanged_p)
10152 if (BEG + BEG_UNCHANGED == start
10153 && overlay_touches_p (start))
10154 unchanged_p = 0;
10155 if (END_UNCHANGED == end
10156 && overlay_touches_p (Z - end))
10157 unchanged_p = 0;
10161 return unchanged_p;
10165 /* Do a frame update, taking possible shortcuts into account. This is
10166 the main external entry point for redisplay.
10168 If the last redisplay displayed an echo area message and that message
10169 is no longer requested, we clear the echo area or bring back the
10170 mini-buffer if that is in use. */
10172 void
10173 redisplay ()
10175 redisplay_internal (0);
10179 static Lisp_Object
10180 overlay_arrow_string_or_property (var)
10181 Lisp_Object var;
10183 Lisp_Object val;
10185 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10186 return val;
10188 return Voverlay_arrow_string;
10191 /* Return 1 if there are any overlay-arrows in current_buffer. */
10192 static int
10193 overlay_arrow_in_current_buffer_p ()
10195 Lisp_Object vlist;
10197 for (vlist = Voverlay_arrow_variable_list;
10198 CONSP (vlist);
10199 vlist = XCDR (vlist))
10201 Lisp_Object var = XCAR (vlist);
10202 Lisp_Object val;
10204 if (!SYMBOLP (var))
10205 continue;
10206 val = find_symbol_value (var);
10207 if (MARKERP (val)
10208 && current_buffer == XMARKER (val)->buffer)
10209 return 1;
10211 return 0;
10215 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10216 has changed. */
10218 static int
10219 overlay_arrows_changed_p ()
10221 Lisp_Object vlist;
10223 for (vlist = Voverlay_arrow_variable_list;
10224 CONSP (vlist);
10225 vlist = XCDR (vlist))
10227 Lisp_Object var = XCAR (vlist);
10228 Lisp_Object val, pstr;
10230 if (!SYMBOLP (var))
10231 continue;
10232 val = find_symbol_value (var);
10233 if (!MARKERP (val))
10234 continue;
10235 if (! EQ (COERCE_MARKER (val),
10236 Fget (var, Qlast_arrow_position))
10237 || ! (pstr = overlay_arrow_string_or_property (var),
10238 EQ (pstr, Fget (var, Qlast_arrow_string))))
10239 return 1;
10241 return 0;
10244 /* Mark overlay arrows to be updated on next redisplay. */
10246 static void
10247 update_overlay_arrows (up_to_date)
10248 int up_to_date;
10250 Lisp_Object vlist;
10252 for (vlist = Voverlay_arrow_variable_list;
10253 CONSP (vlist);
10254 vlist = XCDR (vlist))
10256 Lisp_Object var = XCAR (vlist);
10258 if (!SYMBOLP (var))
10259 continue;
10261 if (up_to_date > 0)
10263 Lisp_Object val = find_symbol_value (var);
10264 Fput (var, Qlast_arrow_position,
10265 COERCE_MARKER (val));
10266 Fput (var, Qlast_arrow_string,
10267 overlay_arrow_string_or_property (var));
10269 else if (up_to_date < 0
10270 || !NILP (Fget (var, Qlast_arrow_position)))
10272 Fput (var, Qlast_arrow_position, Qt);
10273 Fput (var, Qlast_arrow_string, Qt);
10279 /* Return overlay arrow string to display at row.
10280 Return integer (bitmap number) for arrow bitmap in left fringe.
10281 Return nil if no overlay arrow. */
10283 static Lisp_Object
10284 overlay_arrow_at_row (it, row)
10285 struct it *it;
10286 struct glyph_row *row;
10288 Lisp_Object vlist;
10290 for (vlist = Voverlay_arrow_variable_list;
10291 CONSP (vlist);
10292 vlist = XCDR (vlist))
10294 Lisp_Object var = XCAR (vlist);
10295 Lisp_Object val;
10297 if (!SYMBOLP (var))
10298 continue;
10300 val = find_symbol_value (var);
10302 if (MARKERP (val)
10303 && current_buffer == XMARKER (val)->buffer
10304 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10306 if (FRAME_WINDOW_P (it->f)
10307 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10309 #ifdef HAVE_WINDOW_SYSTEM
10310 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10312 int fringe_bitmap;
10313 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10314 return make_number (fringe_bitmap);
10316 #endif
10317 return make_number (-1); /* Use default arrow bitmap */
10319 return overlay_arrow_string_or_property (var);
10323 return Qnil;
10326 /* Return 1 if point moved out of or into a composition. Otherwise
10327 return 0. PREV_BUF and PREV_PT are the last point buffer and
10328 position. BUF and PT are the current point buffer and position. */
10331 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10332 struct buffer *prev_buf, *buf;
10333 int prev_pt, pt;
10335 int start, end;
10336 Lisp_Object prop;
10337 Lisp_Object buffer;
10339 XSETBUFFER (buffer, buf);
10340 /* Check a composition at the last point if point moved within the
10341 same buffer. */
10342 if (prev_buf == buf)
10344 if (prev_pt == pt)
10345 /* Point didn't move. */
10346 return 0;
10348 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10349 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10350 && COMPOSITION_VALID_P (start, end, prop)
10351 && start < prev_pt && end > prev_pt)
10352 /* The last point was within the composition. Return 1 iff
10353 point moved out of the composition. */
10354 return (pt <= start || pt >= end);
10357 /* Check a composition at the current point. */
10358 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10359 && find_composition (pt, -1, &start, &end, &prop, buffer)
10360 && COMPOSITION_VALID_P (start, end, prop)
10361 && start < pt && end > pt);
10365 /* Reconsider the setting of B->clip_changed which is displayed
10366 in window W. */
10368 static INLINE void
10369 reconsider_clip_changes (w, b)
10370 struct window *w;
10371 struct buffer *b;
10373 if (b->clip_changed
10374 && !NILP (w->window_end_valid)
10375 && w->current_matrix->buffer == b
10376 && w->current_matrix->zv == BUF_ZV (b)
10377 && w->current_matrix->begv == BUF_BEGV (b))
10378 b->clip_changed = 0;
10380 /* If display wasn't paused, and W is not a tool bar window, see if
10381 point has been moved into or out of a composition. In that case,
10382 we set b->clip_changed to 1 to force updating the screen. If
10383 b->clip_changed has already been set to 1, we can skip this
10384 check. */
10385 if (!b->clip_changed
10386 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10388 int pt;
10390 if (w == XWINDOW (selected_window))
10391 pt = BUF_PT (current_buffer);
10392 else
10393 pt = marker_position (w->pointm);
10395 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10396 || pt != XINT (w->last_point))
10397 && check_point_in_composition (w->current_matrix->buffer,
10398 XINT (w->last_point),
10399 XBUFFER (w->buffer), pt))
10400 b->clip_changed = 1;
10405 /* Select FRAME to forward the values of frame-local variables into C
10406 variables so that the redisplay routines can access those values
10407 directly. */
10409 static void
10410 select_frame_for_redisplay (frame)
10411 Lisp_Object frame;
10413 Lisp_Object tail, sym, val;
10414 Lisp_Object old = selected_frame;
10416 selected_frame = frame;
10418 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10419 if (CONSP (XCAR (tail))
10420 && (sym = XCAR (XCAR (tail)),
10421 SYMBOLP (sym))
10422 && (sym = indirect_variable (sym),
10423 val = SYMBOL_VALUE (sym),
10424 (BUFFER_LOCAL_VALUEP (val)
10425 || SOME_BUFFER_LOCAL_VALUEP (val)))
10426 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10427 /* Use find_symbol_value rather than Fsymbol_value
10428 to avoid an error if it is void. */
10429 find_symbol_value (sym);
10431 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10432 if (CONSP (XCAR (tail))
10433 && (sym = XCAR (XCAR (tail)),
10434 SYMBOLP (sym))
10435 && (sym = indirect_variable (sym),
10436 val = SYMBOL_VALUE (sym),
10437 (BUFFER_LOCAL_VALUEP (val)
10438 || SOME_BUFFER_LOCAL_VALUEP (val)))
10439 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10440 find_symbol_value (sym);
10444 #define STOP_POLLING \
10445 do { if (! polling_stopped_here) stop_polling (); \
10446 polling_stopped_here = 1; } while (0)
10448 #define RESUME_POLLING \
10449 do { if (polling_stopped_here) start_polling (); \
10450 polling_stopped_here = 0; } while (0)
10453 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10454 response to any user action; therefore, we should preserve the echo
10455 area. (Actually, our caller does that job.) Perhaps in the future
10456 avoid recentering windows if it is not necessary; currently that
10457 causes some problems. */
10459 static void
10460 redisplay_internal (preserve_echo_area)
10461 int preserve_echo_area;
10463 struct window *w = XWINDOW (selected_window);
10464 struct frame *f = XFRAME (w->frame);
10465 int pause;
10466 int must_finish = 0;
10467 struct text_pos tlbufpos, tlendpos;
10468 int number_of_visible_frames;
10469 int count;
10470 struct frame *sf = SELECTED_FRAME ();
10471 int polling_stopped_here = 0;
10473 /* Non-zero means redisplay has to consider all windows on all
10474 frames. Zero means, only selected_window is considered. */
10475 int consider_all_windows_p;
10477 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10479 /* No redisplay if running in batch mode or frame is not yet fully
10480 initialized, or redisplay is explicitly turned off by setting
10481 Vinhibit_redisplay. */
10482 if (noninteractive
10483 || !NILP (Vinhibit_redisplay)
10484 || !f->glyphs_initialized_p)
10485 return;
10487 /* The flag redisplay_performed_directly_p is set by
10488 direct_output_for_insert when it already did the whole screen
10489 update necessary. */
10490 if (redisplay_performed_directly_p)
10492 redisplay_performed_directly_p = 0;
10493 if (!hscroll_windows (selected_window))
10494 return;
10497 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10498 if (popup_activated ())
10499 return;
10500 #endif
10502 /* I don't think this happens but let's be paranoid. */
10503 if (redisplaying_p)
10504 return;
10506 /* Record a function that resets redisplaying_p to its old value
10507 when we leave this function. */
10508 count = SPECPDL_INDEX ();
10509 record_unwind_protect (unwind_redisplay,
10510 Fcons (make_number (redisplaying_p), selected_frame));
10511 ++redisplaying_p;
10512 specbind (Qinhibit_free_realized_faces, Qnil);
10515 Lisp_Object tail, frame;
10517 FOR_EACH_FRAME (tail, frame)
10519 struct frame *f = XFRAME (frame);
10520 f->already_hscrolled_p = 0;
10524 retry:
10525 pause = 0;
10526 reconsider_clip_changes (w, current_buffer);
10528 /* If new fonts have been loaded that make a glyph matrix adjustment
10529 necessary, do it. */
10530 if (fonts_changed_p)
10532 adjust_glyphs (NULL);
10533 ++windows_or_buffers_changed;
10534 fonts_changed_p = 0;
10537 /* If face_change_count is non-zero, init_iterator will free all
10538 realized faces, which includes the faces referenced from current
10539 matrices. So, we can't reuse current matrices in this case. */
10540 if (face_change_count)
10541 ++windows_or_buffers_changed;
10543 if (! FRAME_WINDOW_P (sf)
10544 && previous_terminal_frame != sf)
10546 /* Since frames on an ASCII terminal share the same display
10547 area, displaying a different frame means redisplay the whole
10548 thing. */
10549 windows_or_buffers_changed++;
10550 SET_FRAME_GARBAGED (sf);
10551 XSETFRAME (Vterminal_frame, sf);
10553 previous_terminal_frame = sf;
10555 /* Set the visible flags for all frames. Do this before checking
10556 for resized or garbaged frames; they want to know if their frames
10557 are visible. See the comment in frame.h for
10558 FRAME_SAMPLE_VISIBILITY. */
10560 Lisp_Object tail, frame;
10562 number_of_visible_frames = 0;
10564 FOR_EACH_FRAME (tail, frame)
10566 struct frame *f = XFRAME (frame);
10568 FRAME_SAMPLE_VISIBILITY (f);
10569 if (FRAME_VISIBLE_P (f))
10570 ++number_of_visible_frames;
10571 clear_desired_matrices (f);
10575 /* Notice any pending interrupt request to change frame size. */
10576 do_pending_window_change (1);
10578 /* Clear frames marked as garbaged. */
10579 if (frame_garbaged)
10580 clear_garbaged_frames ();
10582 /* Build menubar and tool-bar items. */
10583 if (NILP (Vmemory_full))
10584 prepare_menu_bars ();
10586 if (windows_or_buffers_changed)
10587 update_mode_lines++;
10589 /* Detect case that we need to write or remove a star in the mode line. */
10590 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10592 w->update_mode_line = Qt;
10593 if (buffer_shared > 1)
10594 update_mode_lines++;
10597 /* If %c is in the mode line, update it if needed. */
10598 if (!NILP (w->column_number_displayed)
10599 /* This alternative quickly identifies a common case
10600 where no change is needed. */
10601 && !(PT == XFASTINT (w->last_point)
10602 && XFASTINT (w->last_modified) >= MODIFF
10603 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10604 && (XFASTINT (w->column_number_displayed)
10605 != (int) current_column ())) /* iftc */
10606 w->update_mode_line = Qt;
10608 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10610 /* The variable buffer_shared is set in redisplay_window and
10611 indicates that we redisplay a buffer in different windows. See
10612 there. */
10613 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10614 || cursor_type_changed);
10616 /* If specs for an arrow have changed, do thorough redisplay
10617 to ensure we remove any arrow that should no longer exist. */
10618 if (overlay_arrows_changed_p ())
10619 consider_all_windows_p = windows_or_buffers_changed = 1;
10621 /* Normally the message* functions will have already displayed and
10622 updated the echo area, but the frame may have been trashed, or
10623 the update may have been preempted, so display the echo area
10624 again here. Checking message_cleared_p captures the case that
10625 the echo area should be cleared. */
10626 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10627 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10628 || (message_cleared_p
10629 && minibuf_level == 0
10630 /* If the mini-window is currently selected, this means the
10631 echo-area doesn't show through. */
10632 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10634 int window_height_changed_p = echo_area_display (0);
10635 must_finish = 1;
10637 /* If we don't display the current message, don't clear the
10638 message_cleared_p flag, because, if we did, we wouldn't clear
10639 the echo area in the next redisplay which doesn't preserve
10640 the echo area. */
10641 if (!display_last_displayed_message_p)
10642 message_cleared_p = 0;
10644 if (fonts_changed_p)
10645 goto retry;
10646 else if (window_height_changed_p)
10648 consider_all_windows_p = 1;
10649 ++update_mode_lines;
10650 ++windows_or_buffers_changed;
10652 /* If window configuration was changed, frames may have been
10653 marked garbaged. Clear them or we will experience
10654 surprises wrt scrolling. */
10655 if (frame_garbaged)
10656 clear_garbaged_frames ();
10659 else if (EQ (selected_window, minibuf_window)
10660 && (current_buffer->clip_changed
10661 || XFASTINT (w->last_modified) < MODIFF
10662 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10663 && resize_mini_window (w, 0))
10665 /* Resized active mini-window to fit the size of what it is
10666 showing if its contents might have changed. */
10667 must_finish = 1;
10668 consider_all_windows_p = 1;
10669 ++windows_or_buffers_changed;
10670 ++update_mode_lines;
10672 /* If window configuration was changed, frames may have been
10673 marked garbaged. Clear them or we will experience
10674 surprises wrt scrolling. */
10675 if (frame_garbaged)
10676 clear_garbaged_frames ();
10680 /* If showing the region, and mark has changed, we must redisplay
10681 the whole window. The assignment to this_line_start_pos prevents
10682 the optimization directly below this if-statement. */
10683 if (((!NILP (Vtransient_mark_mode)
10684 && !NILP (XBUFFER (w->buffer)->mark_active))
10685 != !NILP (w->region_showing))
10686 || (!NILP (w->region_showing)
10687 && !EQ (w->region_showing,
10688 Fmarker_position (XBUFFER (w->buffer)->mark))))
10689 CHARPOS (this_line_start_pos) = 0;
10691 /* Optimize the case that only the line containing the cursor in the
10692 selected window has changed. Variables starting with this_ are
10693 set in display_line and record information about the line
10694 containing the cursor. */
10695 tlbufpos = this_line_start_pos;
10696 tlendpos = this_line_end_pos;
10697 if (!consider_all_windows_p
10698 && CHARPOS (tlbufpos) > 0
10699 && NILP (w->update_mode_line)
10700 && !current_buffer->clip_changed
10701 && !current_buffer->prevent_redisplay_optimizations_p
10702 && FRAME_VISIBLE_P (XFRAME (w->frame))
10703 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10704 /* Make sure recorded data applies to current buffer, etc. */
10705 && this_line_buffer == current_buffer
10706 && current_buffer == XBUFFER (w->buffer)
10707 && NILP (w->force_start)
10708 && NILP (w->optional_new_start)
10709 /* Point must be on the line that we have info recorded about. */
10710 && PT >= CHARPOS (tlbufpos)
10711 && PT <= Z - CHARPOS (tlendpos)
10712 /* All text outside that line, including its final newline,
10713 must be unchanged */
10714 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10715 CHARPOS (tlendpos)))
10717 if (CHARPOS (tlbufpos) > BEGV
10718 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10719 && (CHARPOS (tlbufpos) == ZV
10720 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10721 /* Former continuation line has disappeared by becoming empty */
10722 goto cancel;
10723 else if (XFASTINT (w->last_modified) < MODIFF
10724 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10725 || MINI_WINDOW_P (w))
10727 /* We have to handle the case of continuation around a
10728 wide-column character (See the comment in indent.c around
10729 line 885).
10731 For instance, in the following case:
10733 -------- Insert --------
10734 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10735 J_I_ ==> J_I_ `^^' are cursors.
10736 ^^ ^^
10737 -------- --------
10739 As we have to redraw the line above, we should goto cancel. */
10741 struct it it;
10742 int line_height_before = this_line_pixel_height;
10744 /* Note that start_display will handle the case that the
10745 line starting at tlbufpos is a continuation lines. */
10746 start_display (&it, w, tlbufpos);
10748 /* Implementation note: It this still necessary? */
10749 if (it.current_x != this_line_start_x)
10750 goto cancel;
10752 TRACE ((stderr, "trying display optimization 1\n"));
10753 w->cursor.vpos = -1;
10754 overlay_arrow_seen = 0;
10755 it.vpos = this_line_vpos;
10756 it.current_y = this_line_y;
10757 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10758 display_line (&it);
10760 /* If line contains point, is not continued,
10761 and ends at same distance from eob as before, we win */
10762 if (w->cursor.vpos >= 0
10763 /* Line is not continued, otherwise this_line_start_pos
10764 would have been set to 0 in display_line. */
10765 && CHARPOS (this_line_start_pos)
10766 /* Line ends as before. */
10767 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10768 /* Line has same height as before. Otherwise other lines
10769 would have to be shifted up or down. */
10770 && this_line_pixel_height == line_height_before)
10772 /* If this is not the window's last line, we must adjust
10773 the charstarts of the lines below. */
10774 if (it.current_y < it.last_visible_y)
10776 struct glyph_row *row
10777 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10778 int delta, delta_bytes;
10780 if (Z - CHARPOS (tlendpos) == ZV)
10782 /* This line ends at end of (accessible part of)
10783 buffer. There is no newline to count. */
10784 delta = (Z
10785 - CHARPOS (tlendpos)
10786 - MATRIX_ROW_START_CHARPOS (row));
10787 delta_bytes = (Z_BYTE
10788 - BYTEPOS (tlendpos)
10789 - MATRIX_ROW_START_BYTEPOS (row));
10791 else
10793 /* This line ends in a newline. Must take
10794 account of the newline and the rest of the
10795 text that follows. */
10796 delta = (Z
10797 - CHARPOS (tlendpos)
10798 - MATRIX_ROW_START_CHARPOS (row));
10799 delta_bytes = (Z_BYTE
10800 - BYTEPOS (tlendpos)
10801 - MATRIX_ROW_START_BYTEPOS (row));
10804 increment_matrix_positions (w->current_matrix,
10805 this_line_vpos + 1,
10806 w->current_matrix->nrows,
10807 delta, delta_bytes);
10810 /* If this row displays text now but previously didn't,
10811 or vice versa, w->window_end_vpos may have to be
10812 adjusted. */
10813 if ((it.glyph_row - 1)->displays_text_p)
10815 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10816 XSETINT (w->window_end_vpos, this_line_vpos);
10818 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10819 && this_line_vpos > 0)
10820 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10821 w->window_end_valid = Qnil;
10823 /* Update hint: No need to try to scroll in update_window. */
10824 w->desired_matrix->no_scrolling_p = 1;
10826 #if GLYPH_DEBUG
10827 *w->desired_matrix->method = 0;
10828 debug_method_add (w, "optimization 1");
10829 #endif
10830 #ifdef HAVE_WINDOW_SYSTEM
10831 update_window_fringes (w, 0);
10832 #endif
10833 goto update;
10835 else
10836 goto cancel;
10838 else if (/* Cursor position hasn't changed. */
10839 PT == XFASTINT (w->last_point)
10840 /* Make sure the cursor was last displayed
10841 in this window. Otherwise we have to reposition it. */
10842 && 0 <= w->cursor.vpos
10843 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10845 if (!must_finish)
10847 do_pending_window_change (1);
10849 /* We used to always goto end_of_redisplay here, but this
10850 isn't enough if we have a blinking cursor. */
10851 if (w->cursor_off_p == w->last_cursor_off_p)
10852 goto end_of_redisplay;
10854 goto update;
10856 /* If highlighting the region, or if the cursor is in the echo area,
10857 then we can't just move the cursor. */
10858 else if (! (!NILP (Vtransient_mark_mode)
10859 && !NILP (current_buffer->mark_active))
10860 && (EQ (selected_window, current_buffer->last_selected_window)
10861 || highlight_nonselected_windows)
10862 && NILP (w->region_showing)
10863 && NILP (Vshow_trailing_whitespace)
10864 && !cursor_in_echo_area)
10866 struct it it;
10867 struct glyph_row *row;
10869 /* Skip from tlbufpos to PT and see where it is. Note that
10870 PT may be in invisible text. If so, we will end at the
10871 next visible position. */
10872 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10873 NULL, DEFAULT_FACE_ID);
10874 it.current_x = this_line_start_x;
10875 it.current_y = this_line_y;
10876 it.vpos = this_line_vpos;
10878 /* The call to move_it_to stops in front of PT, but
10879 moves over before-strings. */
10880 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10882 if (it.vpos == this_line_vpos
10883 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10884 row->enabled_p))
10886 xassert (this_line_vpos == it.vpos);
10887 xassert (this_line_y == it.current_y);
10888 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10889 #if GLYPH_DEBUG
10890 *w->desired_matrix->method = 0;
10891 debug_method_add (w, "optimization 3");
10892 #endif
10893 goto update;
10895 else
10896 goto cancel;
10899 cancel:
10900 /* Text changed drastically or point moved off of line. */
10901 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10904 CHARPOS (this_line_start_pos) = 0;
10905 consider_all_windows_p |= buffer_shared > 1;
10906 ++clear_face_cache_count;
10907 #ifdef HAVE_WINDOW_SYSTEM
10908 ++clear_image_cache_count;
10909 #endif
10911 /* Build desired matrices, and update the display. If
10912 consider_all_windows_p is non-zero, do it for all windows on all
10913 frames. Otherwise do it for selected_window, only. */
10915 if (consider_all_windows_p)
10917 Lisp_Object tail, frame;
10919 FOR_EACH_FRAME (tail, frame)
10920 XFRAME (frame)->updated_p = 0;
10922 /* Recompute # windows showing selected buffer. This will be
10923 incremented each time such a window is displayed. */
10924 buffer_shared = 0;
10926 FOR_EACH_FRAME (tail, frame)
10928 struct frame *f = XFRAME (frame);
10930 if (FRAME_WINDOW_P (f) || f == sf)
10932 if (! EQ (frame, selected_frame))
10933 /* Select the frame, for the sake of frame-local
10934 variables. */
10935 select_frame_for_redisplay (frame);
10937 /* Mark all the scroll bars to be removed; we'll redeem
10938 the ones we want when we redisplay their windows. */
10939 if (condemn_scroll_bars_hook)
10940 condemn_scroll_bars_hook (f);
10942 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10943 redisplay_windows (FRAME_ROOT_WINDOW (f));
10945 /* Any scroll bars which redisplay_windows should have
10946 nuked should now go away. */
10947 if (judge_scroll_bars_hook)
10948 judge_scroll_bars_hook (f);
10950 /* If fonts changed, display again. */
10951 /* ??? rms: I suspect it is a mistake to jump all the way
10952 back to retry here. It should just retry this frame. */
10953 if (fonts_changed_p)
10954 goto retry;
10956 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10958 /* See if we have to hscroll. */
10959 if (!f->already_hscrolled_p)
10961 f->already_hscrolled_p = 1;
10962 if (hscroll_windows (f->root_window))
10963 goto retry;
10966 /* Prevent various kinds of signals during display
10967 update. stdio is not robust about handling
10968 signals, which can cause an apparent I/O
10969 error. */
10970 if (interrupt_input)
10971 unrequest_sigio ();
10972 STOP_POLLING;
10974 /* Update the display. */
10975 set_window_update_flags (XWINDOW (f->root_window), 1);
10976 pause |= update_frame (f, 0, 0);
10977 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10978 if (pause)
10979 break;
10980 #endif
10982 f->updated_p = 1;
10987 if (!pause)
10989 /* Do the mark_window_display_accurate after all windows have
10990 been redisplayed because this call resets flags in buffers
10991 which are needed for proper redisplay. */
10992 FOR_EACH_FRAME (tail, frame)
10994 struct frame *f = XFRAME (frame);
10995 if (f->updated_p)
10997 mark_window_display_accurate (f->root_window, 1);
10998 if (frame_up_to_date_hook)
10999 frame_up_to_date_hook (f);
11004 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11006 Lisp_Object mini_window;
11007 struct frame *mini_frame;
11009 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11010 /* Use list_of_error, not Qerror, so that
11011 we catch only errors and don't run the debugger. */
11012 internal_condition_case_1 (redisplay_window_1, selected_window,
11013 list_of_error,
11014 redisplay_window_error);
11016 /* Compare desired and current matrices, perform output. */
11018 update:
11019 /* If fonts changed, display again. */
11020 if (fonts_changed_p)
11021 goto retry;
11023 /* Prevent various kinds of signals during display update.
11024 stdio is not robust about handling signals,
11025 which can cause an apparent I/O error. */
11026 if (interrupt_input)
11027 unrequest_sigio ();
11028 STOP_POLLING;
11030 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11032 if (hscroll_windows (selected_window))
11033 goto retry;
11035 XWINDOW (selected_window)->must_be_updated_p = 1;
11036 pause = update_frame (sf, 0, 0);
11039 /* We may have called echo_area_display at the top of this
11040 function. If the echo area is on another frame, that may
11041 have put text on a frame other than the selected one, so the
11042 above call to update_frame would not have caught it. Catch
11043 it here. */
11044 mini_window = FRAME_MINIBUF_WINDOW (sf);
11045 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11047 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11049 XWINDOW (mini_window)->must_be_updated_p = 1;
11050 pause |= update_frame (mini_frame, 0, 0);
11051 if (!pause && hscroll_windows (mini_window))
11052 goto retry;
11056 /* If display was paused because of pending input, make sure we do a
11057 thorough update the next time. */
11058 if (pause)
11060 /* Prevent the optimization at the beginning of
11061 redisplay_internal that tries a single-line update of the
11062 line containing the cursor in the selected window. */
11063 CHARPOS (this_line_start_pos) = 0;
11065 /* Let the overlay arrow be updated the next time. */
11066 update_overlay_arrows (0);
11068 /* If we pause after scrolling, some rows in the current
11069 matrices of some windows are not valid. */
11070 if (!WINDOW_FULL_WIDTH_P (w)
11071 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11072 update_mode_lines = 1;
11074 else
11076 if (!consider_all_windows_p)
11078 /* This has already been done above if
11079 consider_all_windows_p is set. */
11080 mark_window_display_accurate_1 (w, 1);
11082 /* Say overlay arrows are up to date. */
11083 update_overlay_arrows (1);
11085 if (frame_up_to_date_hook != 0)
11086 frame_up_to_date_hook (sf);
11089 update_mode_lines = 0;
11090 windows_or_buffers_changed = 0;
11091 cursor_type_changed = 0;
11094 /* Start SIGIO interrupts coming again. Having them off during the
11095 code above makes it less likely one will discard output, but not
11096 impossible, since there might be stuff in the system buffer here.
11097 But it is much hairier to try to do anything about that. */
11098 if (interrupt_input)
11099 request_sigio ();
11100 RESUME_POLLING;
11102 /* If a frame has become visible which was not before, redisplay
11103 again, so that we display it. Expose events for such a frame
11104 (which it gets when becoming visible) don't call the parts of
11105 redisplay constructing glyphs, so simply exposing a frame won't
11106 display anything in this case. So, we have to display these
11107 frames here explicitly. */
11108 if (!pause)
11110 Lisp_Object tail, frame;
11111 int new_count = 0;
11113 FOR_EACH_FRAME (tail, frame)
11115 int this_is_visible = 0;
11117 if (XFRAME (frame)->visible)
11118 this_is_visible = 1;
11119 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11120 if (XFRAME (frame)->visible)
11121 this_is_visible = 1;
11123 if (this_is_visible)
11124 new_count++;
11127 if (new_count != number_of_visible_frames)
11128 windows_or_buffers_changed++;
11131 /* Change frame size now if a change is pending. */
11132 do_pending_window_change (1);
11134 /* If we just did a pending size change, or have additional
11135 visible frames, redisplay again. */
11136 if (windows_or_buffers_changed && !pause)
11137 goto retry;
11139 /* Clear the face cache eventually. */
11140 if (consider_all_windows_p)
11142 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11144 clear_face_cache (0);
11145 clear_face_cache_count = 0;
11147 #ifdef HAVE_WINDOW_SYSTEM
11148 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11150 Lisp_Object tail, frame;
11151 FOR_EACH_FRAME (tail, frame)
11153 struct frame *f = XFRAME (frame);
11154 if (FRAME_WINDOW_P (f))
11155 clear_image_cache (f, 0);
11157 clear_image_cache_count = 0;
11159 #endif /* HAVE_WINDOW_SYSTEM */
11162 end_of_redisplay:
11163 unbind_to (count, Qnil);
11164 RESUME_POLLING;
11168 /* Redisplay, but leave alone any recent echo area message unless
11169 another message has been requested in its place.
11171 This is useful in situations where you need to redisplay but no
11172 user action has occurred, making it inappropriate for the message
11173 area to be cleared. See tracking_off and
11174 wait_reading_process_output for examples of these situations.
11176 FROM_WHERE is an integer saying from where this function was
11177 called. This is useful for debugging. */
11179 void
11180 redisplay_preserve_echo_area (from_where)
11181 int from_where;
11183 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11185 if (!NILP (echo_area_buffer[1]))
11187 /* We have a previously displayed message, but no current
11188 message. Redisplay the previous message. */
11189 display_last_displayed_message_p = 1;
11190 redisplay_internal (1);
11191 display_last_displayed_message_p = 0;
11193 else
11194 redisplay_internal (1);
11196 if (rif != NULL && rif->flush_display_optional)
11197 rif->flush_display_optional (NULL);
11201 /* Function registered with record_unwind_protect in
11202 redisplay_internal. Reset redisplaying_p to the value it had
11203 before redisplay_internal was called, and clear
11204 prevent_freeing_realized_faces_p. It also selects the previously
11205 selected frame. */
11207 static Lisp_Object
11208 unwind_redisplay (val)
11209 Lisp_Object val;
11211 Lisp_Object old_redisplaying_p, old_frame;
11213 old_redisplaying_p = XCAR (val);
11214 redisplaying_p = XFASTINT (old_redisplaying_p);
11215 old_frame = XCDR (val);
11216 if (! EQ (old_frame, selected_frame))
11217 select_frame_for_redisplay (old_frame);
11218 return Qnil;
11222 /* Mark the display of window W as accurate or inaccurate. If
11223 ACCURATE_P is non-zero mark display of W as accurate. If
11224 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11225 redisplay_internal is called. */
11227 static void
11228 mark_window_display_accurate_1 (w, accurate_p)
11229 struct window *w;
11230 int accurate_p;
11232 if (BUFFERP (w->buffer))
11234 struct buffer *b = XBUFFER (w->buffer);
11236 w->last_modified
11237 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11238 w->last_overlay_modified
11239 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11240 w->last_had_star
11241 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11243 if (accurate_p)
11245 b->clip_changed = 0;
11246 b->prevent_redisplay_optimizations_p = 0;
11248 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11249 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11250 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11251 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11253 w->current_matrix->buffer = b;
11254 w->current_matrix->begv = BUF_BEGV (b);
11255 w->current_matrix->zv = BUF_ZV (b);
11257 w->last_cursor = w->cursor;
11258 w->last_cursor_off_p = w->cursor_off_p;
11260 if (w == XWINDOW (selected_window))
11261 w->last_point = make_number (BUF_PT (b));
11262 else
11263 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11267 if (accurate_p)
11269 w->window_end_valid = w->buffer;
11270 #if 0 /* This is incorrect with variable-height lines. */
11271 xassert (XINT (w->window_end_vpos)
11272 < (WINDOW_TOTAL_LINES (w)
11273 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11274 #endif
11275 w->update_mode_line = Qnil;
11280 /* Mark the display of windows in the window tree rooted at WINDOW as
11281 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11282 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11283 be redisplayed the next time redisplay_internal is called. */
11285 void
11286 mark_window_display_accurate (window, accurate_p)
11287 Lisp_Object window;
11288 int accurate_p;
11290 struct window *w;
11292 for (; !NILP (window); window = w->next)
11294 w = XWINDOW (window);
11295 mark_window_display_accurate_1 (w, accurate_p);
11297 if (!NILP (w->vchild))
11298 mark_window_display_accurate (w->vchild, accurate_p);
11299 if (!NILP (w->hchild))
11300 mark_window_display_accurate (w->hchild, accurate_p);
11303 if (accurate_p)
11305 update_overlay_arrows (1);
11307 else
11309 /* Force a thorough redisplay the next time by setting
11310 last_arrow_position and last_arrow_string to t, which is
11311 unequal to any useful value of Voverlay_arrow_... */
11312 update_overlay_arrows (-1);
11317 /* Return value in display table DP (Lisp_Char_Table *) for character
11318 C. Since a display table doesn't have any parent, we don't have to
11319 follow parent. Do not call this function directly but use the
11320 macro DISP_CHAR_VECTOR. */
11322 Lisp_Object
11323 disp_char_vector (dp, c)
11324 struct Lisp_Char_Table *dp;
11325 int c;
11327 int code[4], i;
11328 Lisp_Object val;
11330 if (SINGLE_BYTE_CHAR_P (c))
11331 return (dp->contents[c]);
11333 SPLIT_CHAR (c, code[0], code[1], code[2]);
11334 if (code[1] < 32)
11335 code[1] = -1;
11336 else if (code[2] < 32)
11337 code[2] = -1;
11339 /* Here, the possible range of code[0] (== charset ID) is
11340 128..max_charset. Since the top level char table contains data
11341 for multibyte characters after 256th element, we must increment
11342 code[0] by 128 to get a correct index. */
11343 code[0] += 128;
11344 code[3] = -1; /* anchor */
11346 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11348 val = dp->contents[code[i]];
11349 if (!SUB_CHAR_TABLE_P (val))
11350 return (NILP (val) ? dp->defalt : val);
11353 /* Here, val is a sub char table. We return the default value of
11354 it. */
11355 return (dp->defalt);
11360 /***********************************************************************
11361 Window Redisplay
11362 ***********************************************************************/
11364 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11366 static void
11367 redisplay_windows (window)
11368 Lisp_Object window;
11370 while (!NILP (window))
11372 struct window *w = XWINDOW (window);
11374 if (!NILP (w->hchild))
11375 redisplay_windows (w->hchild);
11376 else if (!NILP (w->vchild))
11377 redisplay_windows (w->vchild);
11378 else
11380 displayed_buffer = XBUFFER (w->buffer);
11381 /* Use list_of_error, not Qerror, so that
11382 we catch only errors and don't run the debugger. */
11383 internal_condition_case_1 (redisplay_window_0, window,
11384 list_of_error,
11385 redisplay_window_error);
11388 window = w->next;
11392 static Lisp_Object
11393 redisplay_window_error ()
11395 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11396 return Qnil;
11399 static Lisp_Object
11400 redisplay_window_0 (window)
11401 Lisp_Object window;
11403 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11404 redisplay_window (window, 0);
11405 return Qnil;
11408 static Lisp_Object
11409 redisplay_window_1 (window)
11410 Lisp_Object window;
11412 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11413 redisplay_window (window, 1);
11414 return Qnil;
11418 /* Increment GLYPH until it reaches END or CONDITION fails while
11419 adding (GLYPH)->pixel_width to X. */
11421 #define SKIP_GLYPHS(glyph, end, x, condition) \
11422 do \
11424 (x) += (glyph)->pixel_width; \
11425 ++(glyph); \
11427 while ((glyph) < (end) && (condition))
11430 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11431 DELTA is the number of bytes by which positions recorded in ROW
11432 differ from current buffer positions. */
11434 void
11435 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11436 struct window *w;
11437 struct glyph_row *row;
11438 struct glyph_matrix *matrix;
11439 int delta, delta_bytes, dy, dvpos;
11441 struct glyph *glyph = row->glyphs[TEXT_AREA];
11442 struct glyph *end = glyph + row->used[TEXT_AREA];
11443 struct glyph *cursor = NULL;
11444 /* The first glyph that starts a sequence of glyphs from string. */
11445 struct glyph *string_start;
11446 /* The X coordinate of string_start. */
11447 int string_start_x;
11448 /* The last known character position. */
11449 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11450 /* The last known character position before string_start. */
11451 int string_before_pos;
11452 int x = row->x;
11453 int cursor_x = x;
11454 int cursor_from_overlay_pos = 0;
11455 int pt_old = PT - delta;
11457 /* Skip over glyphs not having an object at the start of the row.
11458 These are special glyphs like truncation marks on terminal
11459 frames. */
11460 if (row->displays_text_p)
11461 while (glyph < end
11462 && INTEGERP (glyph->object)
11463 && glyph->charpos < 0)
11465 x += glyph->pixel_width;
11466 ++glyph;
11469 string_start = NULL;
11470 while (glyph < end
11471 && !INTEGERP (glyph->object)
11472 && (!BUFFERP (glyph->object)
11473 || (last_pos = glyph->charpos) < pt_old))
11475 if (! STRINGP (glyph->object))
11477 string_start = NULL;
11478 x += glyph->pixel_width;
11479 ++glyph;
11480 if (cursor_from_overlay_pos
11481 && last_pos > cursor_from_overlay_pos)
11483 cursor_from_overlay_pos = 0;
11484 cursor = 0;
11487 else
11489 string_before_pos = last_pos;
11490 string_start = glyph;
11491 string_start_x = x;
11492 /* Skip all glyphs from string. */
11495 int pos;
11496 if ((cursor == NULL || glyph > cursor)
11497 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11498 Qcursor, (glyph)->object))
11499 && (pos = string_buffer_position (w, glyph->object,
11500 string_before_pos),
11501 (pos == 0 /* From overlay */
11502 || pos == pt_old)))
11504 /* Estimate overlay buffer position from the buffer
11505 positions of the glyphs before and after the overlay.
11506 Add 1 to last_pos so that if point corresponds to the
11507 glyph right after the overlay, we still use a 'cursor'
11508 property found in that overlay. */
11509 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11510 cursor = glyph;
11511 cursor_x = x;
11513 x += glyph->pixel_width;
11514 ++glyph;
11516 while (glyph < end && STRINGP (glyph->object));
11520 if (cursor != NULL)
11522 glyph = cursor;
11523 x = cursor_x;
11525 else if (row->ends_in_ellipsis_p && glyph == end)
11527 /* Scan back over the ellipsis glyphs, decrementing positions. */
11528 while (glyph > row->glyphs[TEXT_AREA]
11529 && (glyph - 1)->charpos == last_pos)
11530 glyph--, x -= glyph->pixel_width;
11531 /* That loop always goes one position too far,
11532 including the glyph before the ellipsis.
11533 So scan forward over that one. */
11534 x += glyph->pixel_width;
11535 glyph++;
11537 else if (string_start
11538 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11540 /* We may have skipped over point because the previous glyphs
11541 are from string. As there's no easy way to know the
11542 character position of the current glyph, find the correct
11543 glyph on point by scanning from string_start again. */
11544 Lisp_Object limit;
11545 Lisp_Object string;
11546 int pos;
11548 limit = make_number (pt_old + 1);
11549 end = glyph;
11550 glyph = string_start;
11551 x = string_start_x;
11552 string = glyph->object;
11553 pos = string_buffer_position (w, string, string_before_pos);
11554 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11555 because we always put cursor after overlay strings. */
11556 while (pos == 0 && glyph < end)
11558 string = glyph->object;
11559 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11560 if (glyph < end)
11561 pos = string_buffer_position (w, glyph->object, string_before_pos);
11564 while (glyph < end)
11566 pos = XINT (Fnext_single_char_property_change
11567 (make_number (pos), Qdisplay, Qnil, limit));
11568 if (pos > pt_old)
11569 break;
11570 /* Skip glyphs from the same string. */
11571 string = glyph->object;
11572 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11573 /* Skip glyphs from an overlay. */
11574 while (glyph < end
11575 && ! string_buffer_position (w, glyph->object, pos))
11577 string = glyph->object;
11578 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11583 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11584 w->cursor.x = x;
11585 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11586 w->cursor.y = row->y + dy;
11588 if (w == XWINDOW (selected_window))
11590 if (!row->continued_p
11591 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11592 && row->x == 0)
11594 this_line_buffer = XBUFFER (w->buffer);
11596 CHARPOS (this_line_start_pos)
11597 = MATRIX_ROW_START_CHARPOS (row) + delta;
11598 BYTEPOS (this_line_start_pos)
11599 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11601 CHARPOS (this_line_end_pos)
11602 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11603 BYTEPOS (this_line_end_pos)
11604 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11606 this_line_y = w->cursor.y;
11607 this_line_pixel_height = row->height;
11608 this_line_vpos = w->cursor.vpos;
11609 this_line_start_x = row->x;
11611 else
11612 CHARPOS (this_line_start_pos) = 0;
11617 /* Run window scroll functions, if any, for WINDOW with new window
11618 start STARTP. Sets the window start of WINDOW to that position.
11620 We assume that the window's buffer is really current. */
11622 static INLINE struct text_pos
11623 run_window_scroll_functions (window, startp)
11624 Lisp_Object window;
11625 struct text_pos startp;
11627 struct window *w = XWINDOW (window);
11628 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11630 if (current_buffer != XBUFFER (w->buffer))
11631 abort ();
11633 if (!NILP (Vwindow_scroll_functions))
11635 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11636 make_number (CHARPOS (startp)));
11637 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11638 /* In case the hook functions switch buffers. */
11639 if (current_buffer != XBUFFER (w->buffer))
11640 set_buffer_internal_1 (XBUFFER (w->buffer));
11643 return startp;
11647 /* Make sure the line containing the cursor is fully visible.
11648 A value of 1 means there is nothing to be done.
11649 (Either the line is fully visible, or it cannot be made so,
11650 or we cannot tell.)
11652 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11653 is higher than window.
11655 A value of 0 means the caller should do scrolling
11656 as if point had gone off the screen. */
11658 static int
11659 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11660 struct window *w;
11661 int force_p;
11662 int current_matrix_p;
11664 struct glyph_matrix *matrix;
11665 struct glyph_row *row;
11666 int window_height;
11668 if (!make_cursor_line_fully_visible_p)
11669 return 1;
11671 /* It's not always possible to find the cursor, e.g, when a window
11672 is full of overlay strings. Don't do anything in that case. */
11673 if (w->cursor.vpos < 0)
11674 return 1;
11676 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11677 row = MATRIX_ROW (matrix, w->cursor.vpos);
11679 /* If the cursor row is not partially visible, there's nothing to do. */
11680 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11681 return 1;
11683 /* If the row the cursor is in is taller than the window's height,
11684 it's not clear what to do, so do nothing. */
11685 window_height = window_box_height (w);
11686 if (row->height >= window_height)
11688 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11689 return 1;
11691 return 0;
11693 #if 0
11694 /* This code used to try to scroll the window just enough to make
11695 the line visible. It returned 0 to say that the caller should
11696 allocate larger glyph matrices. */
11698 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11700 int dy = row->height - row->visible_height;
11701 w->vscroll = 0;
11702 w->cursor.y += dy;
11703 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11705 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11707 int dy = - (row->height - row->visible_height);
11708 w->vscroll = dy;
11709 w->cursor.y += dy;
11710 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11713 /* When we change the cursor y-position of the selected window,
11714 change this_line_y as well so that the display optimization for
11715 the cursor line of the selected window in redisplay_internal uses
11716 the correct y-position. */
11717 if (w == XWINDOW (selected_window))
11718 this_line_y = w->cursor.y;
11720 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11721 redisplay with larger matrices. */
11722 if (matrix->nrows < required_matrix_height (w))
11724 fonts_changed_p = 1;
11725 return 0;
11728 return 1;
11729 #endif /* 0 */
11733 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11734 non-zero means only WINDOW is redisplayed in redisplay_internal.
11735 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11736 in redisplay_window to bring a partially visible line into view in
11737 the case that only the cursor has moved.
11739 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11740 last screen line's vertical height extends past the end of the screen.
11742 Value is
11744 1 if scrolling succeeded
11746 0 if scrolling didn't find point.
11748 -1 if new fonts have been loaded so that we must interrupt
11749 redisplay, adjust glyph matrices, and try again. */
11751 enum
11753 SCROLLING_SUCCESS,
11754 SCROLLING_FAILED,
11755 SCROLLING_NEED_LARGER_MATRICES
11758 static int
11759 try_scrolling (window, just_this_one_p, scroll_conservatively,
11760 scroll_step, temp_scroll_step, last_line_misfit)
11761 Lisp_Object window;
11762 int just_this_one_p;
11763 EMACS_INT scroll_conservatively, scroll_step;
11764 int temp_scroll_step;
11765 int last_line_misfit;
11767 struct window *w = XWINDOW (window);
11768 struct frame *f = XFRAME (w->frame);
11769 struct text_pos scroll_margin_pos;
11770 struct text_pos pos;
11771 struct text_pos startp;
11772 struct it it;
11773 Lisp_Object window_end;
11774 int this_scroll_margin;
11775 int dy = 0;
11776 int scroll_max;
11777 int rc;
11778 int amount_to_scroll = 0;
11779 Lisp_Object aggressive;
11780 int height;
11781 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11783 #if GLYPH_DEBUG
11784 debug_method_add (w, "try_scrolling");
11785 #endif
11787 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11789 /* Compute scroll margin height in pixels. We scroll when point is
11790 within this distance from the top or bottom of the window. */
11791 if (scroll_margin > 0)
11793 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11794 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11796 else
11797 this_scroll_margin = 0;
11799 /* Force scroll_conservatively to have a reasonable value so it doesn't
11800 cause an overflow while computing how much to scroll. */
11801 if (scroll_conservatively)
11802 scroll_conservatively = min (scroll_conservatively,
11803 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11805 /* Compute how much we should try to scroll maximally to bring point
11806 into view. */
11807 if (scroll_step || scroll_conservatively || temp_scroll_step)
11808 scroll_max = max (scroll_step,
11809 max (scroll_conservatively, temp_scroll_step));
11810 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11811 || NUMBERP (current_buffer->scroll_up_aggressively))
11812 /* We're trying to scroll because of aggressive scrolling
11813 but no scroll_step is set. Choose an arbitrary one. Maybe
11814 there should be a variable for this. */
11815 scroll_max = 10;
11816 else
11817 scroll_max = 0;
11818 scroll_max *= FRAME_LINE_HEIGHT (f);
11820 /* Decide whether we have to scroll down. Start at the window end
11821 and move this_scroll_margin up to find the position of the scroll
11822 margin. */
11823 window_end = Fwindow_end (window, Qt);
11825 too_near_end:
11827 CHARPOS (scroll_margin_pos) = XINT (window_end);
11828 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11830 if (this_scroll_margin || extra_scroll_margin_lines)
11832 start_display (&it, w, scroll_margin_pos);
11833 if (this_scroll_margin)
11834 move_it_vertically_backward (&it, this_scroll_margin);
11835 if (extra_scroll_margin_lines)
11836 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11837 scroll_margin_pos = it.current.pos;
11840 if (PT >= CHARPOS (scroll_margin_pos))
11842 int y0;
11844 /* Point is in the scroll margin at the bottom of the window, or
11845 below. Compute a new window start that makes point visible. */
11847 /* Compute the distance from the scroll margin to PT.
11848 Give up if the distance is greater than scroll_max. */
11849 start_display (&it, w, scroll_margin_pos);
11850 y0 = it.current_y;
11851 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11852 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11854 /* To make point visible, we have to move the window start
11855 down so that the line the cursor is in is visible, which
11856 means we have to add in the height of the cursor line. */
11857 dy = line_bottom_y (&it) - y0;
11859 if (dy > scroll_max)
11860 return SCROLLING_FAILED;
11862 /* Move the window start down. If scrolling conservatively,
11863 move it just enough down to make point visible. If
11864 scroll_step is set, move it down by scroll_step. */
11865 start_display (&it, w, startp);
11867 if (scroll_conservatively)
11868 /* Set AMOUNT_TO_SCROLL to at least one line,
11869 and at most scroll_conservatively lines. */
11870 amount_to_scroll
11871 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11872 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11873 else if (scroll_step || temp_scroll_step)
11874 amount_to_scroll = scroll_max;
11875 else
11877 aggressive = current_buffer->scroll_up_aggressively;
11878 height = WINDOW_BOX_TEXT_HEIGHT (w);
11879 if (NUMBERP (aggressive))
11881 double float_amount = XFLOATINT (aggressive) * height;
11882 amount_to_scroll = float_amount;
11883 if (amount_to_scroll == 0 && float_amount > 0)
11884 amount_to_scroll = 1;
11888 if (amount_to_scroll <= 0)
11889 return SCROLLING_FAILED;
11891 /* If moving by amount_to_scroll leaves STARTP unchanged,
11892 move it down one screen line. */
11894 move_it_vertically (&it, amount_to_scroll);
11895 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11896 move_it_by_lines (&it, 1, 1);
11897 startp = it.current.pos;
11899 else
11901 /* See if point is inside the scroll margin at the top of the
11902 window. */
11903 scroll_margin_pos = startp;
11904 if (this_scroll_margin)
11906 start_display (&it, w, startp);
11907 move_it_vertically (&it, this_scroll_margin);
11908 scroll_margin_pos = it.current.pos;
11911 if (PT < CHARPOS (scroll_margin_pos))
11913 /* Point is in the scroll margin at the top of the window or
11914 above what is displayed in the window. */
11915 int y0;
11917 /* Compute the vertical distance from PT to the scroll
11918 margin position. Give up if distance is greater than
11919 scroll_max. */
11920 SET_TEXT_POS (pos, PT, PT_BYTE);
11921 start_display (&it, w, pos);
11922 y0 = it.current_y;
11923 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11924 it.last_visible_y, -1,
11925 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11926 dy = it.current_y - y0;
11927 if (dy > scroll_max)
11928 return SCROLLING_FAILED;
11930 /* Compute new window start. */
11931 start_display (&it, w, startp);
11933 if (scroll_conservatively)
11934 amount_to_scroll
11935 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11936 else if (scroll_step || temp_scroll_step)
11937 amount_to_scroll = scroll_max;
11938 else
11940 aggressive = current_buffer->scroll_down_aggressively;
11941 height = WINDOW_BOX_TEXT_HEIGHT (w);
11942 if (NUMBERP (aggressive))
11944 double float_amount = XFLOATINT (aggressive) * height;
11945 amount_to_scroll = float_amount;
11946 if (amount_to_scroll == 0 && float_amount > 0)
11947 amount_to_scroll = 1;
11951 if (amount_to_scroll <= 0)
11952 return SCROLLING_FAILED;
11954 move_it_vertically_backward (&it, amount_to_scroll);
11955 startp = it.current.pos;
11959 /* Run window scroll functions. */
11960 startp = run_window_scroll_functions (window, startp);
11962 /* Display the window. Give up if new fonts are loaded, or if point
11963 doesn't appear. */
11964 if (!try_window (window, startp, 0))
11965 rc = SCROLLING_NEED_LARGER_MATRICES;
11966 else if (w->cursor.vpos < 0)
11968 clear_glyph_matrix (w->desired_matrix);
11969 rc = SCROLLING_FAILED;
11971 else
11973 /* Maybe forget recorded base line for line number display. */
11974 if (!just_this_one_p
11975 || current_buffer->clip_changed
11976 || BEG_UNCHANGED < CHARPOS (startp))
11977 w->base_line_number = Qnil;
11979 /* If cursor ends up on a partially visible line,
11980 treat that as being off the bottom of the screen. */
11981 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11983 clear_glyph_matrix (w->desired_matrix);
11984 ++extra_scroll_margin_lines;
11985 goto too_near_end;
11987 rc = SCROLLING_SUCCESS;
11990 return rc;
11994 /* Compute a suitable window start for window W if display of W starts
11995 on a continuation line. Value is non-zero if a new window start
11996 was computed.
11998 The new window start will be computed, based on W's width, starting
11999 from the start of the continued line. It is the start of the
12000 screen line with the minimum distance from the old start W->start. */
12002 static int
12003 compute_window_start_on_continuation_line (w)
12004 struct window *w;
12006 struct text_pos pos, start_pos;
12007 int window_start_changed_p = 0;
12009 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12011 /* If window start is on a continuation line... Window start may be
12012 < BEGV in case there's invisible text at the start of the
12013 buffer (M-x rmail, for example). */
12014 if (CHARPOS (start_pos) > BEGV
12015 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12017 struct it it;
12018 struct glyph_row *row;
12020 /* Handle the case that the window start is out of range. */
12021 if (CHARPOS (start_pos) < BEGV)
12022 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12023 else if (CHARPOS (start_pos) > ZV)
12024 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12026 /* Find the start of the continued line. This should be fast
12027 because scan_buffer is fast (newline cache). */
12028 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12029 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12030 row, DEFAULT_FACE_ID);
12031 reseat_at_previous_visible_line_start (&it);
12033 /* If the line start is "too far" away from the window start,
12034 say it takes too much time to compute a new window start. */
12035 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12036 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12038 int min_distance, distance;
12040 /* Move forward by display lines to find the new window
12041 start. If window width was enlarged, the new start can
12042 be expected to be > the old start. If window width was
12043 decreased, the new window start will be < the old start.
12044 So, we're looking for the display line start with the
12045 minimum distance from the old window start. */
12046 pos = it.current.pos;
12047 min_distance = INFINITY;
12048 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12049 distance < min_distance)
12051 min_distance = distance;
12052 pos = it.current.pos;
12053 move_it_by_lines (&it, 1, 0);
12056 /* Set the window start there. */
12057 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12058 window_start_changed_p = 1;
12062 return window_start_changed_p;
12066 /* Try cursor movement in case text has not changed in window WINDOW,
12067 with window start STARTP. Value is
12069 CURSOR_MOVEMENT_SUCCESS if successful
12071 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12073 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12074 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12075 we want to scroll as if scroll-step were set to 1. See the code.
12077 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12078 which case we have to abort this redisplay, and adjust matrices
12079 first. */
12081 enum
12083 CURSOR_MOVEMENT_SUCCESS,
12084 CURSOR_MOVEMENT_CANNOT_BE_USED,
12085 CURSOR_MOVEMENT_MUST_SCROLL,
12086 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12089 static int
12090 try_cursor_movement (window, startp, scroll_step)
12091 Lisp_Object window;
12092 struct text_pos startp;
12093 int *scroll_step;
12095 struct window *w = XWINDOW (window);
12096 struct frame *f = XFRAME (w->frame);
12097 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12099 #if GLYPH_DEBUG
12100 if (inhibit_try_cursor_movement)
12101 return rc;
12102 #endif
12104 /* Handle case where text has not changed, only point, and it has
12105 not moved off the frame. */
12106 if (/* Point may be in this window. */
12107 PT >= CHARPOS (startp)
12108 /* Selective display hasn't changed. */
12109 && !current_buffer->clip_changed
12110 /* Function force-mode-line-update is used to force a thorough
12111 redisplay. It sets either windows_or_buffers_changed or
12112 update_mode_lines. So don't take a shortcut here for these
12113 cases. */
12114 && !update_mode_lines
12115 && !windows_or_buffers_changed
12116 && !cursor_type_changed
12117 /* Can't use this case if highlighting a region. When a
12118 region exists, cursor movement has to do more than just
12119 set the cursor. */
12120 && !(!NILP (Vtransient_mark_mode)
12121 && !NILP (current_buffer->mark_active))
12122 && NILP (w->region_showing)
12123 && NILP (Vshow_trailing_whitespace)
12124 /* Right after splitting windows, last_point may be nil. */
12125 && INTEGERP (w->last_point)
12126 /* This code is not used for mini-buffer for the sake of the case
12127 of redisplaying to replace an echo area message; since in
12128 that case the mini-buffer contents per se are usually
12129 unchanged. This code is of no real use in the mini-buffer
12130 since the handling of this_line_start_pos, etc., in redisplay
12131 handles the same cases. */
12132 && !EQ (window, minibuf_window)
12133 /* When splitting windows or for new windows, it happens that
12134 redisplay is called with a nil window_end_vpos or one being
12135 larger than the window. This should really be fixed in
12136 window.c. I don't have this on my list, now, so we do
12137 approximately the same as the old redisplay code. --gerd. */
12138 && INTEGERP (w->window_end_vpos)
12139 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12140 && (FRAME_WINDOW_P (f)
12141 || !overlay_arrow_in_current_buffer_p ()))
12143 int this_scroll_margin, top_scroll_margin;
12144 struct glyph_row *row = NULL;
12146 #if GLYPH_DEBUG
12147 debug_method_add (w, "cursor movement");
12148 #endif
12150 /* Scroll if point within this distance from the top or bottom
12151 of the window. This is a pixel value. */
12152 this_scroll_margin = max (0, scroll_margin);
12153 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12154 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12156 top_scroll_margin = this_scroll_margin;
12157 if (WINDOW_WANTS_HEADER_LINE_P (w))
12158 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12160 /* Start with the row the cursor was displayed during the last
12161 not paused redisplay. Give up if that row is not valid. */
12162 if (w->last_cursor.vpos < 0
12163 || w->last_cursor.vpos >= w->current_matrix->nrows)
12164 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12165 else
12167 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12168 if (row->mode_line_p)
12169 ++row;
12170 if (!row->enabled_p)
12171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12174 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12176 int scroll_p = 0;
12177 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12179 if (PT > XFASTINT (w->last_point))
12181 /* Point has moved forward. */
12182 while (MATRIX_ROW_END_CHARPOS (row) < PT
12183 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12185 xassert (row->enabled_p);
12186 ++row;
12189 /* The end position of a row equals the start position
12190 of the next row. If PT is there, we would rather
12191 display it in the next line. */
12192 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12193 && MATRIX_ROW_END_CHARPOS (row) == PT
12194 && !cursor_row_p (w, row))
12195 ++row;
12197 /* If within the scroll margin, scroll. Note that
12198 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12199 the next line would be drawn, and that
12200 this_scroll_margin can be zero. */
12201 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12202 || PT > MATRIX_ROW_END_CHARPOS (row)
12203 /* Line is completely visible last line in window
12204 and PT is to be set in the next line. */
12205 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12206 && PT == MATRIX_ROW_END_CHARPOS (row)
12207 && !row->ends_at_zv_p
12208 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12209 scroll_p = 1;
12211 else if (PT < XFASTINT (w->last_point))
12213 /* Cursor has to be moved backward. Note that PT >=
12214 CHARPOS (startp) because of the outer if-statement. */
12215 while (!row->mode_line_p
12216 && (MATRIX_ROW_START_CHARPOS (row) > PT
12217 || (MATRIX_ROW_START_CHARPOS (row) == PT
12218 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12219 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12220 row > w->current_matrix->rows
12221 && (row-1)->ends_in_newline_from_string_p))))
12222 && (row->y > top_scroll_margin
12223 || CHARPOS (startp) == BEGV))
12225 xassert (row->enabled_p);
12226 --row;
12229 /* Consider the following case: Window starts at BEGV,
12230 there is invisible, intangible text at BEGV, so that
12231 display starts at some point START > BEGV. It can
12232 happen that we are called with PT somewhere between
12233 BEGV and START. Try to handle that case. */
12234 if (row < w->current_matrix->rows
12235 || row->mode_line_p)
12237 row = w->current_matrix->rows;
12238 if (row->mode_line_p)
12239 ++row;
12242 /* Due to newlines in overlay strings, we may have to
12243 skip forward over overlay strings. */
12244 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12245 && MATRIX_ROW_END_CHARPOS (row) == PT
12246 && !cursor_row_p (w, row))
12247 ++row;
12249 /* If within the scroll margin, scroll. */
12250 if (row->y < top_scroll_margin
12251 && CHARPOS (startp) != BEGV)
12252 scroll_p = 1;
12254 else
12256 /* Cursor did not move. So don't scroll even if cursor line
12257 is partially visible, as it was so before. */
12258 rc = CURSOR_MOVEMENT_SUCCESS;
12261 if (PT < MATRIX_ROW_START_CHARPOS (row)
12262 || PT > MATRIX_ROW_END_CHARPOS (row))
12264 /* if PT is not in the glyph row, give up. */
12265 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12267 else if (rc != CURSOR_MOVEMENT_SUCCESS
12268 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12269 && make_cursor_line_fully_visible_p)
12271 if (PT == MATRIX_ROW_END_CHARPOS (row)
12272 && !row->ends_at_zv_p
12273 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12274 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12275 else if (row->height > window_box_height (w))
12277 /* If we end up in a partially visible line, let's
12278 make it fully visible, except when it's taller
12279 than the window, in which case we can't do much
12280 about it. */
12281 *scroll_step = 1;
12282 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12284 else
12286 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12287 if (!cursor_row_fully_visible_p (w, 0, 1))
12288 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12289 else
12290 rc = CURSOR_MOVEMENT_SUCCESS;
12293 else if (scroll_p)
12294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12295 else
12297 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12298 rc = CURSOR_MOVEMENT_SUCCESS;
12303 return rc;
12306 void
12307 set_vertical_scroll_bar (w)
12308 struct window *w;
12310 int start, end, whole;
12312 /* Calculate the start and end positions for the current window.
12313 At some point, it would be nice to choose between scrollbars
12314 which reflect the whole buffer size, with special markers
12315 indicating narrowing, and scrollbars which reflect only the
12316 visible region.
12318 Note that mini-buffers sometimes aren't displaying any text. */
12319 if (!MINI_WINDOW_P (w)
12320 || (w == XWINDOW (minibuf_window)
12321 && NILP (echo_area_buffer[0])))
12323 struct buffer *buf = XBUFFER (w->buffer);
12324 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12325 start = marker_position (w->start) - BUF_BEGV (buf);
12326 /* I don't think this is guaranteed to be right. For the
12327 moment, we'll pretend it is. */
12328 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12330 if (end < start)
12331 end = start;
12332 if (whole < (end - start))
12333 whole = end - start;
12335 else
12336 start = end = whole = 0;
12338 /* Indicate what this scroll bar ought to be displaying now. */
12339 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12343 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12344 selected_window is redisplayed.
12346 We can return without actually redisplaying the window if
12347 fonts_changed_p is nonzero. In that case, redisplay_internal will
12348 retry. */
12350 static void
12351 redisplay_window (window, just_this_one_p)
12352 Lisp_Object window;
12353 int just_this_one_p;
12355 struct window *w = XWINDOW (window);
12356 struct frame *f = XFRAME (w->frame);
12357 struct buffer *buffer = XBUFFER (w->buffer);
12358 struct buffer *old = current_buffer;
12359 struct text_pos lpoint, opoint, startp;
12360 int update_mode_line;
12361 int tem;
12362 struct it it;
12363 /* Record it now because it's overwritten. */
12364 int current_matrix_up_to_date_p = 0;
12365 int used_current_matrix_p = 0;
12366 /* This is less strict than current_matrix_up_to_date_p.
12367 It indictes that the buffer contents and narrowing are unchanged. */
12368 int buffer_unchanged_p = 0;
12369 int temp_scroll_step = 0;
12370 int count = SPECPDL_INDEX ();
12371 int rc;
12372 int centering_position = -1;
12373 int last_line_misfit = 0;
12375 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12376 opoint = lpoint;
12378 /* W must be a leaf window here. */
12379 xassert (!NILP (w->buffer));
12380 #if GLYPH_DEBUG
12381 *w->desired_matrix->method = 0;
12382 #endif
12384 specbind (Qinhibit_point_motion_hooks, Qt);
12386 reconsider_clip_changes (w, buffer);
12388 /* Has the mode line to be updated? */
12389 update_mode_line = (!NILP (w->update_mode_line)
12390 || update_mode_lines
12391 || buffer->clip_changed
12392 || buffer->prevent_redisplay_optimizations_p);
12394 if (MINI_WINDOW_P (w))
12396 if (w == XWINDOW (echo_area_window)
12397 && !NILP (echo_area_buffer[0]))
12399 if (update_mode_line)
12400 /* We may have to update a tty frame's menu bar or a
12401 tool-bar. Example `M-x C-h C-h C-g'. */
12402 goto finish_menu_bars;
12403 else
12404 /* We've already displayed the echo area glyphs in this window. */
12405 goto finish_scroll_bars;
12407 else if ((w != XWINDOW (minibuf_window)
12408 || minibuf_level == 0)
12409 /* When buffer is nonempty, redisplay window normally. */
12410 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12411 /* Quail displays non-mini buffers in minibuffer window.
12412 In that case, redisplay the window normally. */
12413 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12415 /* W is a mini-buffer window, but it's not active, so clear
12416 it. */
12417 int yb = window_text_bottom_y (w);
12418 struct glyph_row *row;
12419 int y;
12421 for (y = 0, row = w->desired_matrix->rows;
12422 y < yb;
12423 y += row->height, ++row)
12424 blank_row (w, row, y);
12425 goto finish_scroll_bars;
12428 clear_glyph_matrix (w->desired_matrix);
12431 /* Otherwise set up data on this window; select its buffer and point
12432 value. */
12433 /* Really select the buffer, for the sake of buffer-local
12434 variables. */
12435 set_buffer_internal_1 (XBUFFER (w->buffer));
12436 SET_TEXT_POS (opoint, PT, PT_BYTE);
12438 current_matrix_up_to_date_p
12439 = (!NILP (w->window_end_valid)
12440 && !current_buffer->clip_changed
12441 && !current_buffer->prevent_redisplay_optimizations_p
12442 && XFASTINT (w->last_modified) >= MODIFF
12443 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12445 buffer_unchanged_p
12446 = (!NILP (w->window_end_valid)
12447 && !current_buffer->clip_changed
12448 && XFASTINT (w->last_modified) >= MODIFF
12449 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12451 /* When windows_or_buffers_changed is non-zero, we can't rely on
12452 the window end being valid, so set it to nil there. */
12453 if (windows_or_buffers_changed)
12455 /* If window starts on a continuation line, maybe adjust the
12456 window start in case the window's width changed. */
12457 if (XMARKER (w->start)->buffer == current_buffer)
12458 compute_window_start_on_continuation_line (w);
12460 w->window_end_valid = Qnil;
12463 /* Some sanity checks. */
12464 CHECK_WINDOW_END (w);
12465 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12466 abort ();
12467 if (BYTEPOS (opoint) < CHARPOS (opoint))
12468 abort ();
12470 /* If %c is in mode line, update it if needed. */
12471 if (!NILP (w->column_number_displayed)
12472 /* This alternative quickly identifies a common case
12473 where no change is needed. */
12474 && !(PT == XFASTINT (w->last_point)
12475 && XFASTINT (w->last_modified) >= MODIFF
12476 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12477 && (XFASTINT (w->column_number_displayed)
12478 != (int) current_column ())) /* iftc */
12479 update_mode_line = 1;
12481 /* Count number of windows showing the selected buffer. An indirect
12482 buffer counts as its base buffer. */
12483 if (!just_this_one_p)
12485 struct buffer *current_base, *window_base;
12486 current_base = current_buffer;
12487 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12488 if (current_base->base_buffer)
12489 current_base = current_base->base_buffer;
12490 if (window_base->base_buffer)
12491 window_base = window_base->base_buffer;
12492 if (current_base == window_base)
12493 buffer_shared++;
12496 /* Point refers normally to the selected window. For any other
12497 window, set up appropriate value. */
12498 if (!EQ (window, selected_window))
12500 int new_pt = XMARKER (w->pointm)->charpos;
12501 int new_pt_byte = marker_byte_position (w->pointm);
12502 if (new_pt < BEGV)
12504 new_pt = BEGV;
12505 new_pt_byte = BEGV_BYTE;
12506 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12508 else if (new_pt > (ZV - 1))
12510 new_pt = ZV;
12511 new_pt_byte = ZV_BYTE;
12512 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12515 /* We don't use SET_PT so that the point-motion hooks don't run. */
12516 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12519 /* If any of the character widths specified in the display table
12520 have changed, invalidate the width run cache. It's true that
12521 this may be a bit late to catch such changes, but the rest of
12522 redisplay goes (non-fatally) haywire when the display table is
12523 changed, so why should we worry about doing any better? */
12524 if (current_buffer->width_run_cache)
12526 struct Lisp_Char_Table *disptab = buffer_display_table ();
12528 if (! disptab_matches_widthtab (disptab,
12529 XVECTOR (current_buffer->width_table)))
12531 invalidate_region_cache (current_buffer,
12532 current_buffer->width_run_cache,
12533 BEG, Z);
12534 recompute_width_table (current_buffer, disptab);
12538 /* If window-start is screwed up, choose a new one. */
12539 if (XMARKER (w->start)->buffer != current_buffer)
12540 goto recenter;
12542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12544 /* If someone specified a new starting point but did not insist,
12545 check whether it can be used. */
12546 if (!NILP (w->optional_new_start)
12547 && CHARPOS (startp) >= BEGV
12548 && CHARPOS (startp) <= ZV)
12550 w->optional_new_start = Qnil;
12551 start_display (&it, w, startp);
12552 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12553 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12554 if (IT_CHARPOS (it) == PT)
12555 w->force_start = Qt;
12556 /* IT may overshoot PT if text at PT is invisible. */
12557 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12558 w->force_start = Qt;
12563 /* Handle case where place to start displaying has been specified,
12564 unless the specified location is outside the accessible range. */
12565 if (!NILP (w->force_start)
12566 || w->frozen_window_start_p)
12568 /* We set this later on if we have to adjust point. */
12569 int new_vpos = -1;
12570 int val;
12572 w->force_start = Qnil;
12573 w->vscroll = 0;
12574 w->window_end_valid = Qnil;
12576 /* Forget any recorded base line for line number display. */
12577 if (!buffer_unchanged_p)
12578 w->base_line_number = Qnil;
12580 /* Redisplay the mode line. Select the buffer properly for that.
12581 Also, run the hook window-scroll-functions
12582 because we have scrolled. */
12583 /* Note, we do this after clearing force_start because
12584 if there's an error, it is better to forget about force_start
12585 than to get into an infinite loop calling the hook functions
12586 and having them get more errors. */
12587 if (!update_mode_line
12588 || ! NILP (Vwindow_scroll_functions))
12590 update_mode_line = 1;
12591 w->update_mode_line = Qt;
12592 startp = run_window_scroll_functions (window, startp);
12595 w->last_modified = make_number (0);
12596 w->last_overlay_modified = make_number (0);
12597 if (CHARPOS (startp) < BEGV)
12598 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12599 else if (CHARPOS (startp) > ZV)
12600 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12602 /* Redisplay, then check if cursor has been set during the
12603 redisplay. Give up if new fonts were loaded. */
12604 val = try_window (window, startp, 1);
12605 if (!val)
12607 w->force_start = Qt;
12608 clear_glyph_matrix (w->desired_matrix);
12609 goto need_larger_matrices;
12611 /* Point was outside the scroll margins. */
12612 if (val < 0)
12613 new_vpos = window_box_height (w) / 2;
12615 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12617 /* If point does not appear, try to move point so it does
12618 appear. The desired matrix has been built above, so we
12619 can use it here. */
12620 new_vpos = window_box_height (w) / 2;
12623 if (!cursor_row_fully_visible_p (w, 0, 0))
12625 /* Point does appear, but on a line partly visible at end of window.
12626 Move it back to a fully-visible line. */
12627 new_vpos = window_box_height (w);
12630 /* If we need to move point for either of the above reasons,
12631 now actually do it. */
12632 if (new_vpos >= 0)
12634 struct glyph_row *row;
12636 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12637 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12638 ++row;
12640 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12641 MATRIX_ROW_START_BYTEPOS (row));
12643 if (w != XWINDOW (selected_window))
12644 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12645 else if (current_buffer == old)
12646 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12648 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12650 /* If we are highlighting the region, then we just changed
12651 the region, so redisplay to show it. */
12652 if (!NILP (Vtransient_mark_mode)
12653 && !NILP (current_buffer->mark_active))
12655 clear_glyph_matrix (w->desired_matrix);
12656 if (!try_window (window, startp, 0))
12657 goto need_larger_matrices;
12661 #if GLYPH_DEBUG
12662 debug_method_add (w, "forced window start");
12663 #endif
12664 goto done;
12667 /* Handle case where text has not changed, only point, and it has
12668 not moved off the frame, and we are not retrying after hscroll.
12669 (current_matrix_up_to_date_p is nonzero when retrying.) */
12670 if (current_matrix_up_to_date_p
12671 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12672 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12674 switch (rc)
12676 case CURSOR_MOVEMENT_SUCCESS:
12677 used_current_matrix_p = 1;
12678 goto done;
12680 #if 0 /* try_cursor_movement never returns this value. */
12681 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12682 goto need_larger_matrices;
12683 #endif
12685 case CURSOR_MOVEMENT_MUST_SCROLL:
12686 goto try_to_scroll;
12688 default:
12689 abort ();
12692 /* If current starting point was originally the beginning of a line
12693 but no longer is, find a new starting point. */
12694 else if (!NILP (w->start_at_line_beg)
12695 && !(CHARPOS (startp) <= BEGV
12696 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12698 #if GLYPH_DEBUG
12699 debug_method_add (w, "recenter 1");
12700 #endif
12701 goto recenter;
12704 /* Try scrolling with try_window_id. Value is > 0 if update has
12705 been done, it is -1 if we know that the same window start will
12706 not work. It is 0 if unsuccessful for some other reason. */
12707 else if ((tem = try_window_id (w)) != 0)
12709 #if GLYPH_DEBUG
12710 debug_method_add (w, "try_window_id %d", tem);
12711 #endif
12713 if (fonts_changed_p)
12714 goto need_larger_matrices;
12715 if (tem > 0)
12716 goto done;
12718 /* Otherwise try_window_id has returned -1 which means that we
12719 don't want the alternative below this comment to execute. */
12721 else if (CHARPOS (startp) >= BEGV
12722 && CHARPOS (startp) <= ZV
12723 && PT >= CHARPOS (startp)
12724 && (CHARPOS (startp) < ZV
12725 /* Avoid starting at end of buffer. */
12726 || CHARPOS (startp) == BEGV
12727 || (XFASTINT (w->last_modified) >= MODIFF
12728 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12730 #if GLYPH_DEBUG
12731 debug_method_add (w, "same window start");
12732 #endif
12734 /* Try to redisplay starting at same place as before.
12735 If point has not moved off frame, accept the results. */
12736 if (!current_matrix_up_to_date_p
12737 /* Don't use try_window_reusing_current_matrix in this case
12738 because a window scroll function can have changed the
12739 buffer. */
12740 || !NILP (Vwindow_scroll_functions)
12741 || MINI_WINDOW_P (w)
12742 || !(used_current_matrix_p
12743 = try_window_reusing_current_matrix (w)))
12745 IF_DEBUG (debug_method_add (w, "1"));
12746 if (try_window (window, startp, 1) < 0)
12747 /* -1 means we need to scroll.
12748 0 means we need new matrices, but fonts_changed_p
12749 is set in that case, so we will detect it below. */
12750 goto try_to_scroll;
12753 if (fonts_changed_p)
12754 goto need_larger_matrices;
12756 if (w->cursor.vpos >= 0)
12758 if (!just_this_one_p
12759 || current_buffer->clip_changed
12760 || BEG_UNCHANGED < CHARPOS (startp))
12761 /* Forget any recorded base line for line number display. */
12762 w->base_line_number = Qnil;
12764 if (!cursor_row_fully_visible_p (w, 1, 0))
12766 clear_glyph_matrix (w->desired_matrix);
12767 last_line_misfit = 1;
12769 /* Drop through and scroll. */
12770 else
12771 goto done;
12773 else
12774 clear_glyph_matrix (w->desired_matrix);
12777 try_to_scroll:
12779 w->last_modified = make_number (0);
12780 w->last_overlay_modified = make_number (0);
12782 /* Redisplay the mode line. Select the buffer properly for that. */
12783 if (!update_mode_line)
12785 update_mode_line = 1;
12786 w->update_mode_line = Qt;
12789 /* Try to scroll by specified few lines. */
12790 if ((scroll_conservatively
12791 || scroll_step
12792 || temp_scroll_step
12793 || NUMBERP (current_buffer->scroll_up_aggressively)
12794 || NUMBERP (current_buffer->scroll_down_aggressively))
12795 && !current_buffer->clip_changed
12796 && CHARPOS (startp) >= BEGV
12797 && CHARPOS (startp) <= ZV)
12799 /* The function returns -1 if new fonts were loaded, 1 if
12800 successful, 0 if not successful. */
12801 int rc = try_scrolling (window, just_this_one_p,
12802 scroll_conservatively,
12803 scroll_step,
12804 temp_scroll_step, last_line_misfit);
12805 switch (rc)
12807 case SCROLLING_SUCCESS:
12808 goto done;
12810 case SCROLLING_NEED_LARGER_MATRICES:
12811 goto need_larger_matrices;
12813 case SCROLLING_FAILED:
12814 break;
12816 default:
12817 abort ();
12821 /* Finally, just choose place to start which centers point */
12823 recenter:
12824 if (centering_position < 0)
12825 centering_position = window_box_height (w) / 2;
12827 #if GLYPH_DEBUG
12828 debug_method_add (w, "recenter");
12829 #endif
12831 /* w->vscroll = 0; */
12833 /* Forget any previously recorded base line for line number display. */
12834 if (!buffer_unchanged_p)
12835 w->base_line_number = Qnil;
12837 /* Move backward half the height of the window. */
12838 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12839 it.current_y = it.last_visible_y;
12840 move_it_vertically_backward (&it, centering_position);
12841 xassert (IT_CHARPOS (it) >= BEGV);
12843 /* The function move_it_vertically_backward may move over more
12844 than the specified y-distance. If it->w is small, e.g. a
12845 mini-buffer window, we may end up in front of the window's
12846 display area. Start displaying at the start of the line
12847 containing PT in this case. */
12848 if (it.current_y <= 0)
12850 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12851 move_it_vertically_backward (&it, 0);
12852 #if 0
12853 /* I think this assert is bogus if buffer contains
12854 invisible text or images. KFS. */
12855 xassert (IT_CHARPOS (it) <= PT);
12856 #endif
12857 it.current_y = 0;
12860 it.current_x = it.hpos = 0;
12862 /* Set startp here explicitly in case that helps avoid an infinite loop
12863 in case the window-scroll-functions functions get errors. */
12864 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12866 /* Run scroll hooks. */
12867 startp = run_window_scroll_functions (window, it.current.pos);
12869 /* Redisplay the window. */
12870 if (!current_matrix_up_to_date_p
12871 || windows_or_buffers_changed
12872 || cursor_type_changed
12873 /* Don't use try_window_reusing_current_matrix in this case
12874 because it can have changed the buffer. */
12875 || !NILP (Vwindow_scroll_functions)
12876 || !just_this_one_p
12877 || MINI_WINDOW_P (w)
12878 || !(used_current_matrix_p
12879 = try_window_reusing_current_matrix (w)))
12880 try_window (window, startp, 0);
12882 /* If new fonts have been loaded (due to fontsets), give up. We
12883 have to start a new redisplay since we need to re-adjust glyph
12884 matrices. */
12885 if (fonts_changed_p)
12886 goto need_larger_matrices;
12888 /* If cursor did not appear assume that the middle of the window is
12889 in the first line of the window. Do it again with the next line.
12890 (Imagine a window of height 100, displaying two lines of height
12891 60. Moving back 50 from it->last_visible_y will end in the first
12892 line.) */
12893 if (w->cursor.vpos < 0)
12895 if (!NILP (w->window_end_valid)
12896 && PT >= Z - XFASTINT (w->window_end_pos))
12898 clear_glyph_matrix (w->desired_matrix);
12899 move_it_by_lines (&it, 1, 0);
12900 try_window (window, it.current.pos, 0);
12902 else if (PT < IT_CHARPOS (it))
12904 clear_glyph_matrix (w->desired_matrix);
12905 move_it_by_lines (&it, -1, 0);
12906 try_window (window, it.current.pos, 0);
12908 else
12910 /* Not much we can do about it. */
12914 /* Consider the following case: Window starts at BEGV, there is
12915 invisible, intangible text at BEGV, so that display starts at
12916 some point START > BEGV. It can happen that we are called with
12917 PT somewhere between BEGV and START. Try to handle that case. */
12918 if (w->cursor.vpos < 0)
12920 struct glyph_row *row = w->current_matrix->rows;
12921 if (row->mode_line_p)
12922 ++row;
12923 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12926 if (!cursor_row_fully_visible_p (w, 0, 0))
12928 /* If vscroll is enabled, disable it and try again. */
12929 if (w->vscroll)
12931 w->vscroll = 0;
12932 clear_glyph_matrix (w->desired_matrix);
12933 goto recenter;
12936 /* If centering point failed to make the whole line visible,
12937 put point at the top instead. That has to make the whole line
12938 visible, if it can be done. */
12939 if (centering_position == 0)
12940 goto done;
12942 clear_glyph_matrix (w->desired_matrix);
12943 centering_position = 0;
12944 goto recenter;
12947 done:
12949 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12950 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12951 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12952 ? Qt : Qnil);
12954 /* Display the mode line, if we must. */
12955 if ((update_mode_line
12956 /* If window not full width, must redo its mode line
12957 if (a) the window to its side is being redone and
12958 (b) we do a frame-based redisplay. This is a consequence
12959 of how inverted lines are drawn in frame-based redisplay. */
12960 || (!just_this_one_p
12961 && !FRAME_WINDOW_P (f)
12962 && !WINDOW_FULL_WIDTH_P (w))
12963 /* Line number to display. */
12964 || INTEGERP (w->base_line_pos)
12965 /* Column number is displayed and different from the one displayed. */
12966 || (!NILP (w->column_number_displayed)
12967 && (XFASTINT (w->column_number_displayed)
12968 != (int) current_column ()))) /* iftc */
12969 /* This means that the window has a mode line. */
12970 && (WINDOW_WANTS_MODELINE_P (w)
12971 || WINDOW_WANTS_HEADER_LINE_P (w)))
12973 display_mode_lines (w);
12975 /* If mode line height has changed, arrange for a thorough
12976 immediate redisplay using the correct mode line height. */
12977 if (WINDOW_WANTS_MODELINE_P (w)
12978 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12980 fonts_changed_p = 1;
12981 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12982 = DESIRED_MODE_LINE_HEIGHT (w);
12985 /* If top line height has changed, arrange for a thorough
12986 immediate redisplay using the correct mode line height. */
12987 if (WINDOW_WANTS_HEADER_LINE_P (w)
12988 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12990 fonts_changed_p = 1;
12991 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12992 = DESIRED_HEADER_LINE_HEIGHT (w);
12995 if (fonts_changed_p)
12996 goto need_larger_matrices;
12999 if (!line_number_displayed
13000 && !BUFFERP (w->base_line_pos))
13002 w->base_line_pos = Qnil;
13003 w->base_line_number = Qnil;
13006 finish_menu_bars:
13008 /* When we reach a frame's selected window, redo the frame's menu bar. */
13009 if (update_mode_line
13010 && EQ (FRAME_SELECTED_WINDOW (f), window))
13012 int redisplay_menu_p = 0;
13013 int redisplay_tool_bar_p = 0;
13015 if (FRAME_WINDOW_P (f))
13017 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13018 || defined (USE_GTK)
13019 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13020 #else
13021 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13022 #endif
13024 else
13025 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13027 if (redisplay_menu_p)
13028 display_menu_bar (w);
13030 #ifdef HAVE_WINDOW_SYSTEM
13031 #ifdef USE_GTK
13032 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13033 #else
13034 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13035 && (FRAME_TOOL_BAR_LINES (f) > 0
13036 || auto_resize_tool_bars_p);
13038 #endif
13040 if (redisplay_tool_bar_p)
13041 redisplay_tool_bar (f);
13042 #endif
13045 #ifdef HAVE_WINDOW_SYSTEM
13046 if (FRAME_WINDOW_P (f)
13047 && update_window_fringes (w, (just_this_one_p
13048 || (!used_current_matrix_p && !overlay_arrow_seen)
13049 || w->pseudo_window_p)))
13051 update_begin (f);
13052 BLOCK_INPUT;
13053 if (draw_window_fringes (w, 1))
13054 x_draw_vertical_border (w);
13055 UNBLOCK_INPUT;
13056 update_end (f);
13058 #endif /* HAVE_WINDOW_SYSTEM */
13060 /* We go to this label, with fonts_changed_p nonzero,
13061 if it is necessary to try again using larger glyph matrices.
13062 We have to redeem the scroll bar even in this case,
13063 because the loop in redisplay_internal expects that. */
13064 need_larger_matrices:
13066 finish_scroll_bars:
13068 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13070 /* Set the thumb's position and size. */
13071 set_vertical_scroll_bar (w);
13073 /* Note that we actually used the scroll bar attached to this
13074 window, so it shouldn't be deleted at the end of redisplay. */
13075 redeem_scroll_bar_hook (w);
13078 /* Restore current_buffer and value of point in it. */
13079 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13080 set_buffer_internal_1 (old);
13081 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13083 unbind_to (count, Qnil);
13087 /* Build the complete desired matrix of WINDOW with a window start
13088 buffer position POS.
13090 Value is 1 if successful. It is zero if fonts were loaded during
13091 redisplay which makes re-adjusting glyph matrices necessary, and -1
13092 if point would appear in the scroll margins.
13093 (We check that only if CHECK_MARGINS is nonzero. */
13096 try_window (window, pos, check_margins)
13097 Lisp_Object window;
13098 struct text_pos pos;
13099 int check_margins;
13101 struct window *w = XWINDOW (window);
13102 struct it it;
13103 struct glyph_row *last_text_row = NULL;
13105 /* Make POS the new window start. */
13106 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13108 /* Mark cursor position as unknown. No overlay arrow seen. */
13109 w->cursor.vpos = -1;
13110 overlay_arrow_seen = 0;
13112 /* Initialize iterator and info to start at POS. */
13113 start_display (&it, w, pos);
13115 /* Display all lines of W. */
13116 while (it.current_y < it.last_visible_y)
13118 if (display_line (&it))
13119 last_text_row = it.glyph_row - 1;
13120 if (fonts_changed_p)
13121 return 0;
13124 /* Don't let the cursor end in the scroll margins. */
13125 if (check_margins
13126 && !MINI_WINDOW_P (w))
13128 int this_scroll_margin;
13130 this_scroll_margin = max (0, scroll_margin);
13131 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13132 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13134 if ((w->cursor.y < this_scroll_margin
13135 && CHARPOS (pos) > BEGV
13136 && IT_CHARPOS (it) < ZV)
13137 /* rms: considering make_cursor_line_fully_visible_p here
13138 seems to give wrong results. We don't want to recenter
13139 when the last line is partly visible, we want to allow
13140 that case to be handled in the usual way. */
13141 || (w->cursor.y + 1) > it.last_visible_y)
13143 w->cursor.vpos = -1;
13144 clear_glyph_matrix (w->desired_matrix);
13145 return -1;
13149 /* If bottom moved off end of frame, change mode line percentage. */
13150 if (XFASTINT (w->window_end_pos) <= 0
13151 && Z != IT_CHARPOS (it))
13152 w->update_mode_line = Qt;
13154 /* Set window_end_pos to the offset of the last character displayed
13155 on the window from the end of current_buffer. Set
13156 window_end_vpos to its row number. */
13157 if (last_text_row)
13159 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13160 w->window_end_bytepos
13161 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13162 w->window_end_pos
13163 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13164 w->window_end_vpos
13165 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13166 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13167 ->displays_text_p);
13169 else
13171 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13172 w->window_end_pos = make_number (Z - ZV);
13173 w->window_end_vpos = make_number (0);
13176 /* But that is not valid info until redisplay finishes. */
13177 w->window_end_valid = Qnil;
13178 return 1;
13183 /************************************************************************
13184 Window redisplay reusing current matrix when buffer has not changed
13185 ************************************************************************/
13187 /* Try redisplay of window W showing an unchanged buffer with a
13188 different window start than the last time it was displayed by
13189 reusing its current matrix. Value is non-zero if successful.
13190 W->start is the new window start. */
13192 static int
13193 try_window_reusing_current_matrix (w)
13194 struct window *w;
13196 struct frame *f = XFRAME (w->frame);
13197 struct glyph_row *row, *bottom_row;
13198 struct it it;
13199 struct run run;
13200 struct text_pos start, new_start;
13201 int nrows_scrolled, i;
13202 struct glyph_row *last_text_row;
13203 struct glyph_row *last_reused_text_row;
13204 struct glyph_row *start_row;
13205 int start_vpos, min_y, max_y;
13207 #if GLYPH_DEBUG
13208 if (inhibit_try_window_reusing)
13209 return 0;
13210 #endif
13212 if (/* This function doesn't handle terminal frames. */
13213 !FRAME_WINDOW_P (f)
13214 /* Don't try to reuse the display if windows have been split
13215 or such. */
13216 || windows_or_buffers_changed
13217 || cursor_type_changed)
13218 return 0;
13220 /* Can't do this if region may have changed. */
13221 if ((!NILP (Vtransient_mark_mode)
13222 && !NILP (current_buffer->mark_active))
13223 || !NILP (w->region_showing)
13224 || !NILP (Vshow_trailing_whitespace))
13225 return 0;
13227 /* If top-line visibility has changed, give up. */
13228 if (WINDOW_WANTS_HEADER_LINE_P (w)
13229 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13230 return 0;
13232 /* Give up if old or new display is scrolled vertically. We could
13233 make this function handle this, but right now it doesn't. */
13234 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13235 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13236 return 0;
13238 /* The variable new_start now holds the new window start. The old
13239 start `start' can be determined from the current matrix. */
13240 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13241 start = start_row->start.pos;
13242 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13244 /* Clear the desired matrix for the display below. */
13245 clear_glyph_matrix (w->desired_matrix);
13247 if (CHARPOS (new_start) <= CHARPOS (start))
13249 int first_row_y;
13251 /* Don't use this method if the display starts with an ellipsis
13252 displayed for invisible text. It's not easy to handle that case
13253 below, and it's certainly not worth the effort since this is
13254 not a frequent case. */
13255 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13256 return 0;
13258 IF_DEBUG (debug_method_add (w, "twu1"));
13260 /* Display up to a row that can be reused. The variable
13261 last_text_row is set to the last row displayed that displays
13262 text. Note that it.vpos == 0 if or if not there is a
13263 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13264 start_display (&it, w, new_start);
13265 first_row_y = it.current_y;
13266 w->cursor.vpos = -1;
13267 last_text_row = last_reused_text_row = NULL;
13269 while (it.current_y < it.last_visible_y
13270 && !fonts_changed_p)
13272 /* If we have reached into the characters in the START row,
13273 that means the line boundaries have changed. So we
13274 can't start copying with the row START. Maybe it will
13275 work to start copying with the following row. */
13276 while (IT_CHARPOS (it) > CHARPOS (start))
13278 /* Advance to the next row as the "start". */
13279 start_row++;
13280 start = start_row->start.pos;
13281 /* If there are no more rows to try, or just one, give up. */
13282 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13283 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13284 || CHARPOS (start) == ZV)
13286 clear_glyph_matrix (w->desired_matrix);
13287 return 0;
13290 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13292 /* If we have reached alignment,
13293 we can copy the rest of the rows. */
13294 if (IT_CHARPOS (it) == CHARPOS (start))
13295 break;
13297 if (display_line (&it))
13298 last_text_row = it.glyph_row - 1;
13301 /* A value of current_y < last_visible_y means that we stopped
13302 at the previous window start, which in turn means that we
13303 have at least one reusable row. */
13304 if (it.current_y < it.last_visible_y)
13306 /* IT.vpos always starts from 0; it counts text lines. */
13307 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13309 /* Find PT if not already found in the lines displayed. */
13310 if (w->cursor.vpos < 0)
13312 int dy = it.current_y - start_row->y;
13314 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13315 row = row_containing_pos (w, PT, row, NULL, dy);
13316 if (row)
13317 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13318 dy, nrows_scrolled);
13319 else
13321 clear_glyph_matrix (w->desired_matrix);
13322 return 0;
13326 /* Scroll the display. Do it before the current matrix is
13327 changed. The problem here is that update has not yet
13328 run, i.e. part of the current matrix is not up to date.
13329 scroll_run_hook will clear the cursor, and use the
13330 current matrix to get the height of the row the cursor is
13331 in. */
13332 run.current_y = start_row->y;
13333 run.desired_y = it.current_y;
13334 run.height = it.last_visible_y - it.current_y;
13336 if (run.height > 0 && run.current_y != run.desired_y)
13338 update_begin (f);
13339 rif->update_window_begin_hook (w);
13340 rif->clear_window_mouse_face (w);
13341 rif->scroll_run_hook (w, &run);
13342 rif->update_window_end_hook (w, 0, 0);
13343 update_end (f);
13346 /* Shift current matrix down by nrows_scrolled lines. */
13347 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13348 rotate_matrix (w->current_matrix,
13349 start_vpos,
13350 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13351 nrows_scrolled);
13353 /* Disable lines that must be updated. */
13354 for (i = 0; i < it.vpos; ++i)
13355 (start_row + i)->enabled_p = 0;
13357 /* Re-compute Y positions. */
13358 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13359 max_y = it.last_visible_y;
13360 for (row = start_row + nrows_scrolled;
13361 row < bottom_row;
13362 ++row)
13364 row->y = it.current_y;
13365 row->visible_height = row->height;
13367 if (row->y < min_y)
13368 row->visible_height -= min_y - row->y;
13369 if (row->y + row->height > max_y)
13370 row->visible_height -= row->y + row->height - max_y;
13371 row->redraw_fringe_bitmaps_p = 1;
13373 it.current_y += row->height;
13375 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13376 last_reused_text_row = row;
13377 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13378 break;
13381 /* Disable lines in the current matrix which are now
13382 below the window. */
13383 for (++row; row < bottom_row; ++row)
13384 row->enabled_p = row->mode_line_p = 0;
13387 /* Update window_end_pos etc.; last_reused_text_row is the last
13388 reused row from the current matrix containing text, if any.
13389 The value of last_text_row is the last displayed line
13390 containing text. */
13391 if (last_reused_text_row)
13393 w->window_end_bytepos
13394 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13395 w->window_end_pos
13396 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13397 w->window_end_vpos
13398 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13399 w->current_matrix));
13401 else if (last_text_row)
13403 w->window_end_bytepos
13404 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13405 w->window_end_pos
13406 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13407 w->window_end_vpos
13408 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13410 else
13412 /* This window must be completely empty. */
13413 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13414 w->window_end_pos = make_number (Z - ZV);
13415 w->window_end_vpos = make_number (0);
13417 w->window_end_valid = Qnil;
13419 /* Update hint: don't try scrolling again in update_window. */
13420 w->desired_matrix->no_scrolling_p = 1;
13422 #if GLYPH_DEBUG
13423 debug_method_add (w, "try_window_reusing_current_matrix 1");
13424 #endif
13425 return 1;
13427 else if (CHARPOS (new_start) > CHARPOS (start))
13429 struct glyph_row *pt_row, *row;
13430 struct glyph_row *first_reusable_row;
13431 struct glyph_row *first_row_to_display;
13432 int dy;
13433 int yb = window_text_bottom_y (w);
13435 /* Find the row starting at new_start, if there is one. Don't
13436 reuse a partially visible line at the end. */
13437 first_reusable_row = start_row;
13438 while (first_reusable_row->enabled_p
13439 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13440 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13441 < CHARPOS (new_start)))
13442 ++first_reusable_row;
13444 /* Give up if there is no row to reuse. */
13445 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13446 || !first_reusable_row->enabled_p
13447 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13448 != CHARPOS (new_start)))
13449 return 0;
13451 /* We can reuse fully visible rows beginning with
13452 first_reusable_row to the end of the window. Set
13453 first_row_to_display to the first row that cannot be reused.
13454 Set pt_row to the row containing point, if there is any. */
13455 pt_row = NULL;
13456 for (first_row_to_display = first_reusable_row;
13457 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13458 ++first_row_to_display)
13460 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13461 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13462 pt_row = first_row_to_display;
13465 /* Start displaying at the start of first_row_to_display. */
13466 xassert (first_row_to_display->y < yb);
13467 init_to_row_start (&it, w, first_row_to_display);
13469 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13470 - start_vpos);
13471 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13472 - nrows_scrolled);
13473 it.current_y = (first_row_to_display->y - first_reusable_row->y
13474 + WINDOW_HEADER_LINE_HEIGHT (w));
13476 /* Display lines beginning with first_row_to_display in the
13477 desired matrix. Set last_text_row to the last row displayed
13478 that displays text. */
13479 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13480 if (pt_row == NULL)
13481 w->cursor.vpos = -1;
13482 last_text_row = NULL;
13483 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13484 if (display_line (&it))
13485 last_text_row = it.glyph_row - 1;
13487 /* Give up If point isn't in a row displayed or reused. */
13488 if (w->cursor.vpos < 0)
13490 clear_glyph_matrix (w->desired_matrix);
13491 return 0;
13494 /* If point is in a reused row, adjust y and vpos of the cursor
13495 position. */
13496 if (pt_row)
13498 w->cursor.vpos -= nrows_scrolled;
13499 w->cursor.y -= first_reusable_row->y - start_row->y;
13502 /* Scroll the display. */
13503 run.current_y = first_reusable_row->y;
13504 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13505 run.height = it.last_visible_y - run.current_y;
13506 dy = run.current_y - run.desired_y;
13508 if (run.height)
13510 update_begin (f);
13511 rif->update_window_begin_hook (w);
13512 rif->clear_window_mouse_face (w);
13513 rif->scroll_run_hook (w, &run);
13514 rif->update_window_end_hook (w, 0, 0);
13515 update_end (f);
13518 /* Adjust Y positions of reused rows. */
13519 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13520 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13521 max_y = it.last_visible_y;
13522 for (row = first_reusable_row; row < first_row_to_display; ++row)
13524 row->y -= dy;
13525 row->visible_height = row->height;
13526 if (row->y < min_y)
13527 row->visible_height -= min_y - row->y;
13528 if (row->y + row->height > max_y)
13529 row->visible_height -= row->y + row->height - max_y;
13530 row->redraw_fringe_bitmaps_p = 1;
13533 /* Scroll the current matrix. */
13534 xassert (nrows_scrolled > 0);
13535 rotate_matrix (w->current_matrix,
13536 start_vpos,
13537 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13538 -nrows_scrolled);
13540 /* Disable rows not reused. */
13541 for (row -= nrows_scrolled; row < bottom_row; ++row)
13542 row->enabled_p = 0;
13544 /* Point may have moved to a different line, so we cannot assume that
13545 the previous cursor position is valid; locate the correct row. */
13546 if (pt_row)
13548 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13549 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13550 row++)
13552 w->cursor.vpos++;
13553 w->cursor.y = row->y;
13555 if (row < bottom_row)
13557 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13558 while (glyph->charpos < PT)
13560 w->cursor.hpos++;
13561 w->cursor.x += glyph->pixel_width;
13562 glyph++;
13567 /* Adjust window end. A null value of last_text_row means that
13568 the window end is in reused rows which in turn means that
13569 only its vpos can have changed. */
13570 if (last_text_row)
13572 w->window_end_bytepos
13573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13574 w->window_end_pos
13575 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13576 w->window_end_vpos
13577 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13579 else
13581 w->window_end_vpos
13582 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13585 w->window_end_valid = Qnil;
13586 w->desired_matrix->no_scrolling_p = 1;
13588 #if GLYPH_DEBUG
13589 debug_method_add (w, "try_window_reusing_current_matrix 2");
13590 #endif
13591 return 1;
13594 return 0;
13599 /************************************************************************
13600 Window redisplay reusing current matrix when buffer has changed
13601 ************************************************************************/
13603 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13604 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13605 int *, int *));
13606 static struct glyph_row *
13607 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13608 struct glyph_row *));
13611 /* Return the last row in MATRIX displaying text. If row START is
13612 non-null, start searching with that row. IT gives the dimensions
13613 of the display. Value is null if matrix is empty; otherwise it is
13614 a pointer to the row found. */
13616 static struct glyph_row *
13617 find_last_row_displaying_text (matrix, it, start)
13618 struct glyph_matrix *matrix;
13619 struct it *it;
13620 struct glyph_row *start;
13622 struct glyph_row *row, *row_found;
13624 /* Set row_found to the last row in IT->w's current matrix
13625 displaying text. The loop looks funny but think of partially
13626 visible lines. */
13627 row_found = NULL;
13628 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13629 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13631 xassert (row->enabled_p);
13632 row_found = row;
13633 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13634 break;
13635 ++row;
13638 return row_found;
13642 /* Return the last row in the current matrix of W that is not affected
13643 by changes at the start of current_buffer that occurred since W's
13644 current matrix was built. Value is null if no such row exists.
13646 BEG_UNCHANGED us the number of characters unchanged at the start of
13647 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13648 first changed character in current_buffer. Characters at positions <
13649 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13650 when the current matrix was built. */
13652 static struct glyph_row *
13653 find_last_unchanged_at_beg_row (w)
13654 struct window *w;
13656 int first_changed_pos = BEG + BEG_UNCHANGED;
13657 struct glyph_row *row;
13658 struct glyph_row *row_found = NULL;
13659 int yb = window_text_bottom_y (w);
13661 /* Find the last row displaying unchanged text. */
13662 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13663 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13664 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13666 if (/* If row ends before first_changed_pos, it is unchanged,
13667 except in some case. */
13668 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13669 /* When row ends in ZV and we write at ZV it is not
13670 unchanged. */
13671 && !row->ends_at_zv_p
13672 /* When first_changed_pos is the end of a continued line,
13673 row is not unchanged because it may be no longer
13674 continued. */
13675 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13676 && (row->continued_p
13677 || row->exact_window_width_line_p)))
13678 row_found = row;
13680 /* Stop if last visible row. */
13681 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13682 break;
13684 ++row;
13687 return row_found;
13691 /* Find the first glyph row in the current matrix of W that is not
13692 affected by changes at the end of current_buffer since the
13693 time W's current matrix was built.
13695 Return in *DELTA the number of chars by which buffer positions in
13696 unchanged text at the end of current_buffer must be adjusted.
13698 Return in *DELTA_BYTES the corresponding number of bytes.
13700 Value is null if no such row exists, i.e. all rows are affected by
13701 changes. */
13703 static struct glyph_row *
13704 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13705 struct window *w;
13706 int *delta, *delta_bytes;
13708 struct glyph_row *row;
13709 struct glyph_row *row_found = NULL;
13711 *delta = *delta_bytes = 0;
13713 /* Display must not have been paused, otherwise the current matrix
13714 is not up to date. */
13715 if (NILP (w->window_end_valid))
13716 abort ();
13718 /* A value of window_end_pos >= END_UNCHANGED means that the window
13719 end is in the range of changed text. If so, there is no
13720 unchanged row at the end of W's current matrix. */
13721 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13722 return NULL;
13724 /* Set row to the last row in W's current matrix displaying text. */
13725 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13727 /* If matrix is entirely empty, no unchanged row exists. */
13728 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13730 /* The value of row is the last glyph row in the matrix having a
13731 meaningful buffer position in it. The end position of row
13732 corresponds to window_end_pos. This allows us to translate
13733 buffer positions in the current matrix to current buffer
13734 positions for characters not in changed text. */
13735 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13736 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13737 int last_unchanged_pos, last_unchanged_pos_old;
13738 struct glyph_row *first_text_row
13739 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13741 *delta = Z - Z_old;
13742 *delta_bytes = Z_BYTE - Z_BYTE_old;
13744 /* Set last_unchanged_pos to the buffer position of the last
13745 character in the buffer that has not been changed. Z is the
13746 index + 1 of the last character in current_buffer, i.e. by
13747 subtracting END_UNCHANGED we get the index of the last
13748 unchanged character, and we have to add BEG to get its buffer
13749 position. */
13750 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13751 last_unchanged_pos_old = last_unchanged_pos - *delta;
13753 /* Search backward from ROW for a row displaying a line that
13754 starts at a minimum position >= last_unchanged_pos_old. */
13755 for (; row > first_text_row; --row)
13757 /* This used to abort, but it can happen.
13758 It is ok to just stop the search instead here. KFS. */
13759 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13760 break;
13762 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13763 row_found = row;
13767 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13768 abort ();
13770 return row_found;
13774 /* Make sure that glyph rows in the current matrix of window W
13775 reference the same glyph memory as corresponding rows in the
13776 frame's frame matrix. This function is called after scrolling W's
13777 current matrix on a terminal frame in try_window_id and
13778 try_window_reusing_current_matrix. */
13780 static void
13781 sync_frame_with_window_matrix_rows (w)
13782 struct window *w;
13784 struct frame *f = XFRAME (w->frame);
13785 struct glyph_row *window_row, *window_row_end, *frame_row;
13787 /* Preconditions: W must be a leaf window and full-width. Its frame
13788 must have a frame matrix. */
13789 xassert (NILP (w->hchild) && NILP (w->vchild));
13790 xassert (WINDOW_FULL_WIDTH_P (w));
13791 xassert (!FRAME_WINDOW_P (f));
13793 /* If W is a full-width window, glyph pointers in W's current matrix
13794 have, by definition, to be the same as glyph pointers in the
13795 corresponding frame matrix. Note that frame matrices have no
13796 marginal areas (see build_frame_matrix). */
13797 window_row = w->current_matrix->rows;
13798 window_row_end = window_row + w->current_matrix->nrows;
13799 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13800 while (window_row < window_row_end)
13802 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13803 struct glyph *end = window_row->glyphs[LAST_AREA];
13805 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13806 frame_row->glyphs[TEXT_AREA] = start;
13807 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13808 frame_row->glyphs[LAST_AREA] = end;
13810 /* Disable frame rows whose corresponding window rows have
13811 been disabled in try_window_id. */
13812 if (!window_row->enabled_p)
13813 frame_row->enabled_p = 0;
13815 ++window_row, ++frame_row;
13820 /* Find the glyph row in window W containing CHARPOS. Consider all
13821 rows between START and END (not inclusive). END null means search
13822 all rows to the end of the display area of W. Value is the row
13823 containing CHARPOS or null. */
13825 struct glyph_row *
13826 row_containing_pos (w, charpos, start, end, dy)
13827 struct window *w;
13828 int charpos;
13829 struct glyph_row *start, *end;
13830 int dy;
13832 struct glyph_row *row = start;
13833 int last_y;
13835 /* If we happen to start on a header-line, skip that. */
13836 if (row->mode_line_p)
13837 ++row;
13839 if ((end && row >= end) || !row->enabled_p)
13840 return NULL;
13842 last_y = window_text_bottom_y (w) - dy;
13844 while (1)
13846 /* Give up if we have gone too far. */
13847 if (end && row >= end)
13848 return NULL;
13849 /* This formerly returned if they were equal.
13850 I think that both quantities are of a "last plus one" type;
13851 if so, when they are equal, the row is within the screen. -- rms. */
13852 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13853 return NULL;
13855 /* If it is in this row, return this row. */
13856 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13857 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13858 /* The end position of a row equals the start
13859 position of the next row. If CHARPOS is there, we
13860 would rather display it in the next line, except
13861 when this line ends in ZV. */
13862 && !row->ends_at_zv_p
13863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13864 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13865 return row;
13866 ++row;
13871 /* Try to redisplay window W by reusing its existing display. W's
13872 current matrix must be up to date when this function is called,
13873 i.e. window_end_valid must not be nil.
13875 Value is
13877 1 if display has been updated
13878 0 if otherwise unsuccessful
13879 -1 if redisplay with same window start is known not to succeed
13881 The following steps are performed:
13883 1. Find the last row in the current matrix of W that is not
13884 affected by changes at the start of current_buffer. If no such row
13885 is found, give up.
13887 2. Find the first row in W's current matrix that is not affected by
13888 changes at the end of current_buffer. Maybe there is no such row.
13890 3. Display lines beginning with the row + 1 found in step 1 to the
13891 row found in step 2 or, if step 2 didn't find a row, to the end of
13892 the window.
13894 4. If cursor is not known to appear on the window, give up.
13896 5. If display stopped at the row found in step 2, scroll the
13897 display and current matrix as needed.
13899 6. Maybe display some lines at the end of W, if we must. This can
13900 happen under various circumstances, like a partially visible line
13901 becoming fully visible, or because newly displayed lines are displayed
13902 in smaller font sizes.
13904 7. Update W's window end information. */
13906 static int
13907 try_window_id (w)
13908 struct window *w;
13910 struct frame *f = XFRAME (w->frame);
13911 struct glyph_matrix *current_matrix = w->current_matrix;
13912 struct glyph_matrix *desired_matrix = w->desired_matrix;
13913 struct glyph_row *last_unchanged_at_beg_row;
13914 struct glyph_row *first_unchanged_at_end_row;
13915 struct glyph_row *row;
13916 struct glyph_row *bottom_row;
13917 int bottom_vpos;
13918 struct it it;
13919 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13920 struct text_pos start_pos;
13921 struct run run;
13922 int first_unchanged_at_end_vpos = 0;
13923 struct glyph_row *last_text_row, *last_text_row_at_end;
13924 struct text_pos start;
13925 int first_changed_charpos, last_changed_charpos;
13927 #if GLYPH_DEBUG
13928 if (inhibit_try_window_id)
13929 return 0;
13930 #endif
13932 /* This is handy for debugging. */
13933 #if 0
13934 #define GIVE_UP(X) \
13935 do { \
13936 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13937 return 0; \
13938 } while (0)
13939 #else
13940 #define GIVE_UP(X) return 0
13941 #endif
13943 SET_TEXT_POS_FROM_MARKER (start, w->start);
13945 /* Don't use this for mini-windows because these can show
13946 messages and mini-buffers, and we don't handle that here. */
13947 if (MINI_WINDOW_P (w))
13948 GIVE_UP (1);
13950 /* This flag is used to prevent redisplay optimizations. */
13951 if (windows_or_buffers_changed || cursor_type_changed)
13952 GIVE_UP (2);
13954 /* Verify that narrowing has not changed.
13955 Also verify that we were not told to prevent redisplay optimizations.
13956 It would be nice to further
13957 reduce the number of cases where this prevents try_window_id. */
13958 if (current_buffer->clip_changed
13959 || current_buffer->prevent_redisplay_optimizations_p)
13960 GIVE_UP (3);
13962 /* Window must either use window-based redisplay or be full width. */
13963 if (!FRAME_WINDOW_P (f)
13964 && (!line_ins_del_ok
13965 || !WINDOW_FULL_WIDTH_P (w)))
13966 GIVE_UP (4);
13968 /* Give up if point is not known NOT to appear in W. */
13969 if (PT < CHARPOS (start))
13970 GIVE_UP (5);
13972 /* Another way to prevent redisplay optimizations. */
13973 if (XFASTINT (w->last_modified) == 0)
13974 GIVE_UP (6);
13976 /* Verify that window is not hscrolled. */
13977 if (XFASTINT (w->hscroll) != 0)
13978 GIVE_UP (7);
13980 /* Verify that display wasn't paused. */
13981 if (NILP (w->window_end_valid))
13982 GIVE_UP (8);
13984 /* Can't use this if highlighting a region because a cursor movement
13985 will do more than just set the cursor. */
13986 if (!NILP (Vtransient_mark_mode)
13987 && !NILP (current_buffer->mark_active))
13988 GIVE_UP (9);
13990 /* Likewise if highlighting trailing whitespace. */
13991 if (!NILP (Vshow_trailing_whitespace))
13992 GIVE_UP (11);
13994 /* Likewise if showing a region. */
13995 if (!NILP (w->region_showing))
13996 GIVE_UP (10);
13998 /* Can use this if overlay arrow position and or string have changed. */
13999 if (overlay_arrows_changed_p ())
14000 GIVE_UP (12);
14003 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14004 only if buffer has really changed. The reason is that the gap is
14005 initially at Z for freshly visited files. The code below would
14006 set end_unchanged to 0 in that case. */
14007 if (MODIFF > SAVE_MODIFF
14008 /* This seems to happen sometimes after saving a buffer. */
14009 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14011 if (GPT - BEG < BEG_UNCHANGED)
14012 BEG_UNCHANGED = GPT - BEG;
14013 if (Z - GPT < END_UNCHANGED)
14014 END_UNCHANGED = Z - GPT;
14017 /* The position of the first and last character that has been changed. */
14018 first_changed_charpos = BEG + BEG_UNCHANGED;
14019 last_changed_charpos = Z - END_UNCHANGED;
14021 /* If window starts after a line end, and the last change is in
14022 front of that newline, then changes don't affect the display.
14023 This case happens with stealth-fontification. Note that although
14024 the display is unchanged, glyph positions in the matrix have to
14025 be adjusted, of course. */
14026 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14027 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14028 && ((last_changed_charpos < CHARPOS (start)
14029 && CHARPOS (start) == BEGV)
14030 || (last_changed_charpos < CHARPOS (start) - 1
14031 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14033 int Z_old, delta, Z_BYTE_old, delta_bytes;
14034 struct glyph_row *r0;
14036 /* Compute how many chars/bytes have been added to or removed
14037 from the buffer. */
14038 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14039 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14040 delta = Z - Z_old;
14041 delta_bytes = Z_BYTE - Z_BYTE_old;
14043 /* Give up if PT is not in the window. Note that it already has
14044 been checked at the start of try_window_id that PT is not in
14045 front of the window start. */
14046 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14047 GIVE_UP (13);
14049 /* If window start is unchanged, we can reuse the whole matrix
14050 as is, after adjusting glyph positions. No need to compute
14051 the window end again, since its offset from Z hasn't changed. */
14052 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14053 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14054 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14055 /* PT must not be in a partially visible line. */
14056 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14057 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14059 /* Adjust positions in the glyph matrix. */
14060 if (delta || delta_bytes)
14062 struct glyph_row *r1
14063 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14064 increment_matrix_positions (w->current_matrix,
14065 MATRIX_ROW_VPOS (r0, current_matrix),
14066 MATRIX_ROW_VPOS (r1, current_matrix),
14067 delta, delta_bytes);
14070 /* Set the cursor. */
14071 row = row_containing_pos (w, PT, r0, NULL, 0);
14072 if (row)
14073 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14074 else
14075 abort ();
14076 return 1;
14080 /* Handle the case that changes are all below what is displayed in
14081 the window, and that PT is in the window. This shortcut cannot
14082 be taken if ZV is visible in the window, and text has been added
14083 there that is visible in the window. */
14084 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14085 /* ZV is not visible in the window, or there are no
14086 changes at ZV, actually. */
14087 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14088 || first_changed_charpos == last_changed_charpos))
14090 struct glyph_row *r0;
14092 /* Give up if PT is not in the window. Note that it already has
14093 been checked at the start of try_window_id that PT is not in
14094 front of the window start. */
14095 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14096 GIVE_UP (14);
14098 /* If window start is unchanged, we can reuse the whole matrix
14099 as is, without changing glyph positions since no text has
14100 been added/removed in front of the window end. */
14101 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14102 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14103 /* PT must not be in a partially visible line. */
14104 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14105 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14107 /* We have to compute the window end anew since text
14108 can have been added/removed after it. */
14109 w->window_end_pos
14110 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14111 w->window_end_bytepos
14112 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14114 /* Set the cursor. */
14115 row = row_containing_pos (w, PT, r0, NULL, 0);
14116 if (row)
14117 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14118 else
14119 abort ();
14120 return 2;
14124 /* Give up if window start is in the changed area.
14126 The condition used to read
14128 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14130 but why that was tested escapes me at the moment. */
14131 if (CHARPOS (start) >= first_changed_charpos
14132 && CHARPOS (start) <= last_changed_charpos)
14133 GIVE_UP (15);
14135 /* Check that window start agrees with the start of the first glyph
14136 row in its current matrix. Check this after we know the window
14137 start is not in changed text, otherwise positions would not be
14138 comparable. */
14139 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14140 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14141 GIVE_UP (16);
14143 /* Give up if the window ends in strings. Overlay strings
14144 at the end are difficult to handle, so don't try. */
14145 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14146 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14147 GIVE_UP (20);
14149 /* Compute the position at which we have to start displaying new
14150 lines. Some of the lines at the top of the window might be
14151 reusable because they are not displaying changed text. Find the
14152 last row in W's current matrix not affected by changes at the
14153 start of current_buffer. Value is null if changes start in the
14154 first line of window. */
14155 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14156 if (last_unchanged_at_beg_row)
14158 /* Avoid starting to display in the moddle of a character, a TAB
14159 for instance. This is easier than to set up the iterator
14160 exactly, and it's not a frequent case, so the additional
14161 effort wouldn't really pay off. */
14162 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14163 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14164 && last_unchanged_at_beg_row > w->current_matrix->rows)
14165 --last_unchanged_at_beg_row;
14167 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14168 GIVE_UP (17);
14170 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14171 GIVE_UP (18);
14172 start_pos = it.current.pos;
14174 /* Start displaying new lines in the desired matrix at the same
14175 vpos we would use in the current matrix, i.e. below
14176 last_unchanged_at_beg_row. */
14177 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14178 current_matrix);
14179 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14180 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14182 xassert (it.hpos == 0 && it.current_x == 0);
14184 else
14186 /* There are no reusable lines at the start of the window.
14187 Start displaying in the first text line. */
14188 start_display (&it, w, start);
14189 it.vpos = it.first_vpos;
14190 start_pos = it.current.pos;
14193 /* Find the first row that is not affected by changes at the end of
14194 the buffer. Value will be null if there is no unchanged row, in
14195 which case we must redisplay to the end of the window. delta
14196 will be set to the value by which buffer positions beginning with
14197 first_unchanged_at_end_row have to be adjusted due to text
14198 changes. */
14199 first_unchanged_at_end_row
14200 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14201 IF_DEBUG (debug_delta = delta);
14202 IF_DEBUG (debug_delta_bytes = delta_bytes);
14204 /* Set stop_pos to the buffer position up to which we will have to
14205 display new lines. If first_unchanged_at_end_row != NULL, this
14206 is the buffer position of the start of the line displayed in that
14207 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14208 that we don't stop at a buffer position. */
14209 stop_pos = 0;
14210 if (first_unchanged_at_end_row)
14212 xassert (last_unchanged_at_beg_row == NULL
14213 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14215 /* If this is a continuation line, move forward to the next one
14216 that isn't. Changes in lines above affect this line.
14217 Caution: this may move first_unchanged_at_end_row to a row
14218 not displaying text. */
14219 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14220 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14221 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14222 < it.last_visible_y))
14223 ++first_unchanged_at_end_row;
14225 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14226 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14227 >= it.last_visible_y))
14228 first_unchanged_at_end_row = NULL;
14229 else
14231 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14232 + delta);
14233 first_unchanged_at_end_vpos
14234 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14235 xassert (stop_pos >= Z - END_UNCHANGED);
14238 else if (last_unchanged_at_beg_row == NULL)
14239 GIVE_UP (19);
14242 #if GLYPH_DEBUG
14244 /* Either there is no unchanged row at the end, or the one we have
14245 now displays text. This is a necessary condition for the window
14246 end pos calculation at the end of this function. */
14247 xassert (first_unchanged_at_end_row == NULL
14248 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14250 debug_last_unchanged_at_beg_vpos
14251 = (last_unchanged_at_beg_row
14252 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14253 : -1);
14254 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14256 #endif /* GLYPH_DEBUG != 0 */
14259 /* Display new lines. Set last_text_row to the last new line
14260 displayed which has text on it, i.e. might end up as being the
14261 line where the window_end_vpos is. */
14262 w->cursor.vpos = -1;
14263 last_text_row = NULL;
14264 overlay_arrow_seen = 0;
14265 while (it.current_y < it.last_visible_y
14266 && !fonts_changed_p
14267 && (first_unchanged_at_end_row == NULL
14268 || IT_CHARPOS (it) < stop_pos))
14270 if (display_line (&it))
14271 last_text_row = it.glyph_row - 1;
14274 if (fonts_changed_p)
14275 return -1;
14278 /* Compute differences in buffer positions, y-positions etc. for
14279 lines reused at the bottom of the window. Compute what we can
14280 scroll. */
14281 if (first_unchanged_at_end_row
14282 /* No lines reused because we displayed everything up to the
14283 bottom of the window. */
14284 && it.current_y < it.last_visible_y)
14286 dvpos = (it.vpos
14287 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14288 current_matrix));
14289 dy = it.current_y - first_unchanged_at_end_row->y;
14290 run.current_y = first_unchanged_at_end_row->y;
14291 run.desired_y = run.current_y + dy;
14292 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14294 else
14296 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14297 first_unchanged_at_end_row = NULL;
14299 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14302 /* Find the cursor if not already found. We have to decide whether
14303 PT will appear on this window (it sometimes doesn't, but this is
14304 not a very frequent case.) This decision has to be made before
14305 the current matrix is altered. A value of cursor.vpos < 0 means
14306 that PT is either in one of the lines beginning at
14307 first_unchanged_at_end_row or below the window. Don't care for
14308 lines that might be displayed later at the window end; as
14309 mentioned, this is not a frequent case. */
14310 if (w->cursor.vpos < 0)
14312 /* Cursor in unchanged rows at the top? */
14313 if (PT < CHARPOS (start_pos)
14314 && last_unchanged_at_beg_row)
14316 row = row_containing_pos (w, PT,
14317 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14318 last_unchanged_at_beg_row + 1, 0);
14319 if (row)
14320 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14323 /* Start from first_unchanged_at_end_row looking for PT. */
14324 else if (first_unchanged_at_end_row)
14326 row = row_containing_pos (w, PT - delta,
14327 first_unchanged_at_end_row, NULL, 0);
14328 if (row)
14329 set_cursor_from_row (w, row, w->current_matrix, delta,
14330 delta_bytes, dy, dvpos);
14333 /* Give up if cursor was not found. */
14334 if (w->cursor.vpos < 0)
14336 clear_glyph_matrix (w->desired_matrix);
14337 return -1;
14341 /* Don't let the cursor end in the scroll margins. */
14343 int this_scroll_margin, cursor_height;
14345 this_scroll_margin = max (0, scroll_margin);
14346 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14347 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14348 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14350 if ((w->cursor.y < this_scroll_margin
14351 && CHARPOS (start) > BEGV)
14352 /* Old redisplay didn't take scroll margin into account at the bottom,
14353 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14354 || (w->cursor.y + (make_cursor_line_fully_visible_p
14355 ? cursor_height + this_scroll_margin
14356 : 1)) > it.last_visible_y)
14358 w->cursor.vpos = -1;
14359 clear_glyph_matrix (w->desired_matrix);
14360 return -1;
14364 /* Scroll the display. Do it before changing the current matrix so
14365 that xterm.c doesn't get confused about where the cursor glyph is
14366 found. */
14367 if (dy && run.height)
14369 update_begin (f);
14371 if (FRAME_WINDOW_P (f))
14373 rif->update_window_begin_hook (w);
14374 rif->clear_window_mouse_face (w);
14375 rif->scroll_run_hook (w, &run);
14376 rif->update_window_end_hook (w, 0, 0);
14378 else
14380 /* Terminal frame. In this case, dvpos gives the number of
14381 lines to scroll by; dvpos < 0 means scroll up. */
14382 int first_unchanged_at_end_vpos
14383 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14384 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14385 int end = (WINDOW_TOP_EDGE_LINE (w)
14386 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14387 + window_internal_height (w));
14389 /* Perform the operation on the screen. */
14390 if (dvpos > 0)
14392 /* Scroll last_unchanged_at_beg_row to the end of the
14393 window down dvpos lines. */
14394 set_terminal_window (end);
14396 /* On dumb terminals delete dvpos lines at the end
14397 before inserting dvpos empty lines. */
14398 if (!scroll_region_ok)
14399 ins_del_lines (end - dvpos, -dvpos);
14401 /* Insert dvpos empty lines in front of
14402 last_unchanged_at_beg_row. */
14403 ins_del_lines (from, dvpos);
14405 else if (dvpos < 0)
14407 /* Scroll up last_unchanged_at_beg_vpos to the end of
14408 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14409 set_terminal_window (end);
14411 /* Delete dvpos lines in front of
14412 last_unchanged_at_beg_vpos. ins_del_lines will set
14413 the cursor to the given vpos and emit |dvpos| delete
14414 line sequences. */
14415 ins_del_lines (from + dvpos, dvpos);
14417 /* On a dumb terminal insert dvpos empty lines at the
14418 end. */
14419 if (!scroll_region_ok)
14420 ins_del_lines (end + dvpos, -dvpos);
14423 set_terminal_window (0);
14426 update_end (f);
14429 /* Shift reused rows of the current matrix to the right position.
14430 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14431 text. */
14432 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14433 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14434 if (dvpos < 0)
14436 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14437 bottom_vpos, dvpos);
14438 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14439 bottom_vpos, 0);
14441 else if (dvpos > 0)
14443 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14444 bottom_vpos, dvpos);
14445 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14446 first_unchanged_at_end_vpos + dvpos, 0);
14449 /* For frame-based redisplay, make sure that current frame and window
14450 matrix are in sync with respect to glyph memory. */
14451 if (!FRAME_WINDOW_P (f))
14452 sync_frame_with_window_matrix_rows (w);
14454 /* Adjust buffer positions in reused rows. */
14455 if (delta)
14456 increment_matrix_positions (current_matrix,
14457 first_unchanged_at_end_vpos + dvpos,
14458 bottom_vpos, delta, delta_bytes);
14460 /* Adjust Y positions. */
14461 if (dy)
14462 shift_glyph_matrix (w, current_matrix,
14463 first_unchanged_at_end_vpos + dvpos,
14464 bottom_vpos, dy);
14466 if (first_unchanged_at_end_row)
14468 first_unchanged_at_end_row += dvpos;
14469 if (first_unchanged_at_end_row->y >= it.last_visible_y
14470 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14471 first_unchanged_at_end_row = NULL;
14474 /* If scrolling up, there may be some lines to display at the end of
14475 the window. */
14476 last_text_row_at_end = NULL;
14477 if (dy < 0)
14479 /* Scrolling up can leave for example a partially visible line
14480 at the end of the window to be redisplayed. */
14481 /* Set last_row to the glyph row in the current matrix where the
14482 window end line is found. It has been moved up or down in
14483 the matrix by dvpos. */
14484 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14485 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14487 /* If last_row is the window end line, it should display text. */
14488 xassert (last_row->displays_text_p);
14490 /* If window end line was partially visible before, begin
14491 displaying at that line. Otherwise begin displaying with the
14492 line following it. */
14493 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14495 init_to_row_start (&it, w, last_row);
14496 it.vpos = last_vpos;
14497 it.current_y = last_row->y;
14499 else
14501 init_to_row_end (&it, w, last_row);
14502 it.vpos = 1 + last_vpos;
14503 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14504 ++last_row;
14507 /* We may start in a continuation line. If so, we have to
14508 get the right continuation_lines_width and current_x. */
14509 it.continuation_lines_width = last_row->continuation_lines_width;
14510 it.hpos = it.current_x = 0;
14512 /* Display the rest of the lines at the window end. */
14513 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14514 while (it.current_y < it.last_visible_y
14515 && !fonts_changed_p)
14517 /* Is it always sure that the display agrees with lines in
14518 the current matrix? I don't think so, so we mark rows
14519 displayed invalid in the current matrix by setting their
14520 enabled_p flag to zero. */
14521 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14522 if (display_line (&it))
14523 last_text_row_at_end = it.glyph_row - 1;
14527 /* Update window_end_pos and window_end_vpos. */
14528 if (first_unchanged_at_end_row
14529 && !last_text_row_at_end)
14531 /* Window end line if one of the preserved rows from the current
14532 matrix. Set row to the last row displaying text in current
14533 matrix starting at first_unchanged_at_end_row, after
14534 scrolling. */
14535 xassert (first_unchanged_at_end_row->displays_text_p);
14536 row = find_last_row_displaying_text (w->current_matrix, &it,
14537 first_unchanged_at_end_row);
14538 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14540 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14541 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14542 w->window_end_vpos
14543 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14544 xassert (w->window_end_bytepos >= 0);
14545 IF_DEBUG (debug_method_add (w, "A"));
14547 else if (last_text_row_at_end)
14549 w->window_end_pos
14550 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14551 w->window_end_bytepos
14552 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14553 w->window_end_vpos
14554 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14555 xassert (w->window_end_bytepos >= 0);
14556 IF_DEBUG (debug_method_add (w, "B"));
14558 else if (last_text_row)
14560 /* We have displayed either to the end of the window or at the
14561 end of the window, i.e. the last row with text is to be found
14562 in the desired matrix. */
14563 w->window_end_pos
14564 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14565 w->window_end_bytepos
14566 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14567 w->window_end_vpos
14568 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14569 xassert (w->window_end_bytepos >= 0);
14571 else if (first_unchanged_at_end_row == NULL
14572 && last_text_row == NULL
14573 && last_text_row_at_end == NULL)
14575 /* Displayed to end of window, but no line containing text was
14576 displayed. Lines were deleted at the end of the window. */
14577 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14578 int vpos = XFASTINT (w->window_end_vpos);
14579 struct glyph_row *current_row = current_matrix->rows + vpos;
14580 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14582 for (row = NULL;
14583 row == NULL && vpos >= first_vpos;
14584 --vpos, --current_row, --desired_row)
14586 if (desired_row->enabled_p)
14588 if (desired_row->displays_text_p)
14589 row = desired_row;
14591 else if (current_row->displays_text_p)
14592 row = current_row;
14595 xassert (row != NULL);
14596 w->window_end_vpos = make_number (vpos + 1);
14597 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14598 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14599 xassert (w->window_end_bytepos >= 0);
14600 IF_DEBUG (debug_method_add (w, "C"));
14602 else
14603 abort ();
14605 #if 0 /* This leads to problems, for instance when the cursor is
14606 at ZV, and the cursor line displays no text. */
14607 /* Disable rows below what's displayed in the window. This makes
14608 debugging easier. */
14609 enable_glyph_matrix_rows (current_matrix,
14610 XFASTINT (w->window_end_vpos) + 1,
14611 bottom_vpos, 0);
14612 #endif
14614 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14615 debug_end_vpos = XFASTINT (w->window_end_vpos));
14617 /* Record that display has not been completed. */
14618 w->window_end_valid = Qnil;
14619 w->desired_matrix->no_scrolling_p = 1;
14620 return 3;
14622 #undef GIVE_UP
14627 /***********************************************************************
14628 More debugging support
14629 ***********************************************************************/
14631 #if GLYPH_DEBUG
14633 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14634 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14635 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14638 /* Dump the contents of glyph matrix MATRIX on stderr.
14640 GLYPHS 0 means don't show glyph contents.
14641 GLYPHS 1 means show glyphs in short form
14642 GLYPHS > 1 means show glyphs in long form. */
14644 void
14645 dump_glyph_matrix (matrix, glyphs)
14646 struct glyph_matrix *matrix;
14647 int glyphs;
14649 int i;
14650 for (i = 0; i < matrix->nrows; ++i)
14651 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14655 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14656 the glyph row and area where the glyph comes from. */
14658 void
14659 dump_glyph (row, glyph, area)
14660 struct glyph_row *row;
14661 struct glyph *glyph;
14662 int area;
14664 if (glyph->type == CHAR_GLYPH)
14666 fprintf (stderr,
14667 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14668 glyph - row->glyphs[TEXT_AREA],
14669 'C',
14670 glyph->charpos,
14671 (BUFFERP (glyph->object)
14672 ? 'B'
14673 : (STRINGP (glyph->object)
14674 ? 'S'
14675 : '-')),
14676 glyph->pixel_width,
14677 glyph->u.ch,
14678 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14679 ? glyph->u.ch
14680 : '.'),
14681 glyph->face_id,
14682 glyph->left_box_line_p,
14683 glyph->right_box_line_p);
14685 else if (glyph->type == STRETCH_GLYPH)
14687 fprintf (stderr,
14688 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14689 glyph - row->glyphs[TEXT_AREA],
14690 'S',
14691 glyph->charpos,
14692 (BUFFERP (glyph->object)
14693 ? 'B'
14694 : (STRINGP (glyph->object)
14695 ? 'S'
14696 : '-')),
14697 glyph->pixel_width,
14699 '.',
14700 glyph->face_id,
14701 glyph->left_box_line_p,
14702 glyph->right_box_line_p);
14704 else if (glyph->type == IMAGE_GLYPH)
14706 fprintf (stderr,
14707 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14708 glyph - row->glyphs[TEXT_AREA],
14709 'I',
14710 glyph->charpos,
14711 (BUFFERP (glyph->object)
14712 ? 'B'
14713 : (STRINGP (glyph->object)
14714 ? 'S'
14715 : '-')),
14716 glyph->pixel_width,
14717 glyph->u.img_id,
14718 '.',
14719 glyph->face_id,
14720 glyph->left_box_line_p,
14721 glyph->right_box_line_p);
14726 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14727 GLYPHS 0 means don't show glyph contents.
14728 GLYPHS 1 means show glyphs in short form
14729 GLYPHS > 1 means show glyphs in long form. */
14731 void
14732 dump_glyph_row (row, vpos, glyphs)
14733 struct glyph_row *row;
14734 int vpos, glyphs;
14736 if (glyphs != 1)
14738 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14739 fprintf (stderr, "======================================================================\n");
14741 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14742 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14743 vpos,
14744 MATRIX_ROW_START_CHARPOS (row),
14745 MATRIX_ROW_END_CHARPOS (row),
14746 row->used[TEXT_AREA],
14747 row->contains_overlapping_glyphs_p,
14748 row->enabled_p,
14749 row->truncated_on_left_p,
14750 row->truncated_on_right_p,
14751 row->continued_p,
14752 MATRIX_ROW_CONTINUATION_LINE_P (row),
14753 row->displays_text_p,
14754 row->ends_at_zv_p,
14755 row->fill_line_p,
14756 row->ends_in_middle_of_char_p,
14757 row->starts_in_middle_of_char_p,
14758 row->mouse_face_p,
14759 row->x,
14760 row->y,
14761 row->pixel_width,
14762 row->height,
14763 row->visible_height,
14764 row->ascent,
14765 row->phys_ascent);
14766 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14767 row->end.overlay_string_index,
14768 row->continuation_lines_width);
14769 fprintf (stderr, "%9d %5d\n",
14770 CHARPOS (row->start.string_pos),
14771 CHARPOS (row->end.string_pos));
14772 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14773 row->end.dpvec_index);
14776 if (glyphs > 1)
14778 int area;
14780 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14782 struct glyph *glyph = row->glyphs[area];
14783 struct glyph *glyph_end = glyph + row->used[area];
14785 /* Glyph for a line end in text. */
14786 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14787 ++glyph_end;
14789 if (glyph < glyph_end)
14790 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14792 for (; glyph < glyph_end; ++glyph)
14793 dump_glyph (row, glyph, area);
14796 else if (glyphs == 1)
14798 int area;
14800 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14802 char *s = (char *) alloca (row->used[area] + 1);
14803 int i;
14805 for (i = 0; i < row->used[area]; ++i)
14807 struct glyph *glyph = row->glyphs[area] + i;
14808 if (glyph->type == CHAR_GLYPH
14809 && glyph->u.ch < 0x80
14810 && glyph->u.ch >= ' ')
14811 s[i] = glyph->u.ch;
14812 else
14813 s[i] = '.';
14816 s[i] = '\0';
14817 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14823 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14824 Sdump_glyph_matrix, 0, 1, "p",
14825 doc: /* Dump the current matrix of the selected window to stderr.
14826 Shows contents of glyph row structures. With non-nil
14827 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14828 glyphs in short form, otherwise show glyphs in long form. */)
14829 (glyphs)
14830 Lisp_Object glyphs;
14832 struct window *w = XWINDOW (selected_window);
14833 struct buffer *buffer = XBUFFER (w->buffer);
14835 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14836 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14837 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14838 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14839 fprintf (stderr, "=============================================\n");
14840 dump_glyph_matrix (w->current_matrix,
14841 NILP (glyphs) ? 0 : XINT (glyphs));
14842 return Qnil;
14846 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14847 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14850 struct frame *f = XFRAME (selected_frame);
14851 dump_glyph_matrix (f->current_matrix, 1);
14852 return Qnil;
14856 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14857 doc: /* Dump glyph row ROW to stderr.
14858 GLYPH 0 means don't dump glyphs.
14859 GLYPH 1 means dump glyphs in short form.
14860 GLYPH > 1 or omitted means dump glyphs in long form. */)
14861 (row, glyphs)
14862 Lisp_Object row, glyphs;
14864 struct glyph_matrix *matrix;
14865 int vpos;
14867 CHECK_NUMBER (row);
14868 matrix = XWINDOW (selected_window)->current_matrix;
14869 vpos = XINT (row);
14870 if (vpos >= 0 && vpos < matrix->nrows)
14871 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14872 vpos,
14873 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14874 return Qnil;
14878 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14879 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14880 GLYPH 0 means don't dump glyphs.
14881 GLYPH 1 means dump glyphs in short form.
14882 GLYPH > 1 or omitted means dump glyphs in long form. */)
14883 (row, glyphs)
14884 Lisp_Object row, glyphs;
14886 struct frame *sf = SELECTED_FRAME ();
14887 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14888 int vpos;
14890 CHECK_NUMBER (row);
14891 vpos = XINT (row);
14892 if (vpos >= 0 && vpos < m->nrows)
14893 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14894 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14895 return Qnil;
14899 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14900 doc: /* Toggle tracing of redisplay.
14901 With ARG, turn tracing on if and only if ARG is positive. */)
14902 (arg)
14903 Lisp_Object arg;
14905 if (NILP (arg))
14906 trace_redisplay_p = !trace_redisplay_p;
14907 else
14909 arg = Fprefix_numeric_value (arg);
14910 trace_redisplay_p = XINT (arg) > 0;
14913 return Qnil;
14917 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14918 doc: /* Like `format', but print result to stderr.
14919 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14920 (nargs, args)
14921 int nargs;
14922 Lisp_Object *args;
14924 Lisp_Object s = Fformat (nargs, args);
14925 fprintf (stderr, "%s", SDATA (s));
14926 return Qnil;
14929 #endif /* GLYPH_DEBUG */
14933 /***********************************************************************
14934 Building Desired Matrix Rows
14935 ***********************************************************************/
14937 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14938 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14940 static struct glyph_row *
14941 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14942 struct window *w;
14943 Lisp_Object overlay_arrow_string;
14945 struct frame *f = XFRAME (WINDOW_FRAME (w));
14946 struct buffer *buffer = XBUFFER (w->buffer);
14947 struct buffer *old = current_buffer;
14948 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14949 int arrow_len = SCHARS (overlay_arrow_string);
14950 const unsigned char *arrow_end = arrow_string + arrow_len;
14951 const unsigned char *p;
14952 struct it it;
14953 int multibyte_p;
14954 int n_glyphs_before;
14956 set_buffer_temp (buffer);
14957 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14958 it.glyph_row->used[TEXT_AREA] = 0;
14959 SET_TEXT_POS (it.position, 0, 0);
14961 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14962 p = arrow_string;
14963 while (p < arrow_end)
14965 Lisp_Object face, ilisp;
14967 /* Get the next character. */
14968 if (multibyte_p)
14969 it.c = string_char_and_length (p, arrow_len, &it.len);
14970 else
14971 it.c = *p, it.len = 1;
14972 p += it.len;
14974 /* Get its face. */
14975 ilisp = make_number (p - arrow_string);
14976 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14977 it.face_id = compute_char_face (f, it.c, face);
14979 /* Compute its width, get its glyphs. */
14980 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14981 SET_TEXT_POS (it.position, -1, -1);
14982 PRODUCE_GLYPHS (&it);
14984 /* If this character doesn't fit any more in the line, we have
14985 to remove some glyphs. */
14986 if (it.current_x > it.last_visible_x)
14988 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14989 break;
14993 set_buffer_temp (old);
14994 return it.glyph_row;
14998 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14999 glyphs are only inserted for terminal frames since we can't really
15000 win with truncation glyphs when partially visible glyphs are
15001 involved. Which glyphs to insert is determined by
15002 produce_special_glyphs. */
15004 static void
15005 insert_left_trunc_glyphs (it)
15006 struct it *it;
15008 struct it truncate_it;
15009 struct glyph *from, *end, *to, *toend;
15011 xassert (!FRAME_WINDOW_P (it->f));
15013 /* Get the truncation glyphs. */
15014 truncate_it = *it;
15015 truncate_it.current_x = 0;
15016 truncate_it.face_id = DEFAULT_FACE_ID;
15017 truncate_it.glyph_row = &scratch_glyph_row;
15018 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15019 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15020 truncate_it.object = make_number (0);
15021 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15023 /* Overwrite glyphs from IT with truncation glyphs. */
15024 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15025 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15026 to = it->glyph_row->glyphs[TEXT_AREA];
15027 toend = to + it->glyph_row->used[TEXT_AREA];
15029 while (from < end)
15030 *to++ = *from++;
15032 /* There may be padding glyphs left over. Overwrite them too. */
15033 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15035 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15036 while (from < end)
15037 *to++ = *from++;
15040 if (to > toend)
15041 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15045 /* Compute the pixel height and width of IT->glyph_row.
15047 Most of the time, ascent and height of a display line will be equal
15048 to the max_ascent and max_height values of the display iterator
15049 structure. This is not the case if
15051 1. We hit ZV without displaying anything. In this case, max_ascent
15052 and max_height will be zero.
15054 2. We have some glyphs that don't contribute to the line height.
15055 (The glyph row flag contributes_to_line_height_p is for future
15056 pixmap extensions).
15058 The first case is easily covered by using default values because in
15059 these cases, the line height does not really matter, except that it
15060 must not be zero. */
15062 static void
15063 compute_line_metrics (it)
15064 struct it *it;
15066 struct glyph_row *row = it->glyph_row;
15067 int area, i;
15069 if (FRAME_WINDOW_P (it->f))
15071 int i, min_y, max_y;
15073 /* The line may consist of one space only, that was added to
15074 place the cursor on it. If so, the row's height hasn't been
15075 computed yet. */
15076 if (row->height == 0)
15078 if (it->max_ascent + it->max_descent == 0)
15079 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15080 row->ascent = it->max_ascent;
15081 row->height = it->max_ascent + it->max_descent;
15082 row->phys_ascent = it->max_phys_ascent;
15083 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15084 row->extra_line_spacing = it->max_extra_line_spacing;
15087 /* Compute the width of this line. */
15088 row->pixel_width = row->x;
15089 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15090 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15092 xassert (row->pixel_width >= 0);
15093 xassert (row->ascent >= 0 && row->height > 0);
15095 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15096 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15098 /* If first line's physical ascent is larger than its logical
15099 ascent, use the physical ascent, and make the row taller.
15100 This makes accented characters fully visible. */
15101 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15102 && row->phys_ascent > row->ascent)
15104 row->height += row->phys_ascent - row->ascent;
15105 row->ascent = row->phys_ascent;
15108 /* Compute how much of the line is visible. */
15109 row->visible_height = row->height;
15111 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15112 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15114 if (row->y < min_y)
15115 row->visible_height -= min_y - row->y;
15116 if (row->y + row->height > max_y)
15117 row->visible_height -= row->y + row->height - max_y;
15119 else
15121 row->pixel_width = row->used[TEXT_AREA];
15122 if (row->continued_p)
15123 row->pixel_width -= it->continuation_pixel_width;
15124 else if (row->truncated_on_right_p)
15125 row->pixel_width -= it->truncation_pixel_width;
15126 row->ascent = row->phys_ascent = 0;
15127 row->height = row->phys_height = row->visible_height = 1;
15128 row->extra_line_spacing = 0;
15131 /* Compute a hash code for this row. */
15132 row->hash = 0;
15133 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15134 for (i = 0; i < row->used[area]; ++i)
15135 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15136 + row->glyphs[area][i].u.val
15137 + row->glyphs[area][i].face_id
15138 + row->glyphs[area][i].padding_p
15139 + (row->glyphs[area][i].type << 2));
15141 it->max_ascent = it->max_descent = 0;
15142 it->max_phys_ascent = it->max_phys_descent = 0;
15146 /* Append one space to the glyph row of iterator IT if doing a
15147 window-based redisplay. The space has the same face as
15148 IT->face_id. Value is non-zero if a space was added.
15150 This function is called to make sure that there is always one glyph
15151 at the end of a glyph row that the cursor can be set on under
15152 window-systems. (If there weren't such a glyph we would not know
15153 how wide and tall a box cursor should be displayed).
15155 At the same time this space let's a nicely handle clearing to the
15156 end of the line if the row ends in italic text. */
15158 static int
15159 append_space_for_newline (it, default_face_p)
15160 struct it *it;
15161 int default_face_p;
15163 if (FRAME_WINDOW_P (it->f))
15165 int n = it->glyph_row->used[TEXT_AREA];
15167 if (it->glyph_row->glyphs[TEXT_AREA] + n
15168 < it->glyph_row->glyphs[1 + TEXT_AREA])
15170 /* Save some values that must not be changed.
15171 Must save IT->c and IT->len because otherwise
15172 ITERATOR_AT_END_P wouldn't work anymore after
15173 append_space_for_newline has been called. */
15174 enum display_element_type saved_what = it->what;
15175 int saved_c = it->c, saved_len = it->len;
15176 int saved_x = it->current_x;
15177 int saved_face_id = it->face_id;
15178 struct text_pos saved_pos;
15179 Lisp_Object saved_object;
15180 struct face *face;
15182 saved_object = it->object;
15183 saved_pos = it->position;
15185 it->what = IT_CHARACTER;
15186 bzero (&it->position, sizeof it->position);
15187 it->object = make_number (0);
15188 it->c = ' ';
15189 it->len = 1;
15191 if (default_face_p)
15192 it->face_id = DEFAULT_FACE_ID;
15193 else if (it->face_before_selective_p)
15194 it->face_id = it->saved_face_id;
15195 face = FACE_FROM_ID (it->f, it->face_id);
15196 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15198 PRODUCE_GLYPHS (it);
15200 it->override_ascent = -1;
15201 it->constrain_row_ascent_descent_p = 0;
15202 it->current_x = saved_x;
15203 it->object = saved_object;
15204 it->position = saved_pos;
15205 it->what = saved_what;
15206 it->face_id = saved_face_id;
15207 it->len = saved_len;
15208 it->c = saved_c;
15209 return 1;
15213 return 0;
15217 /* Extend the face of the last glyph in the text area of IT->glyph_row
15218 to the end of the display line. Called from display_line.
15219 If the glyph row is empty, add a space glyph to it so that we
15220 know the face to draw. Set the glyph row flag fill_line_p. */
15222 static void
15223 extend_face_to_end_of_line (it)
15224 struct it *it;
15226 struct face *face;
15227 struct frame *f = it->f;
15229 /* If line is already filled, do nothing. */
15230 if (it->current_x >= it->last_visible_x)
15231 return;
15233 /* Face extension extends the background and box of IT->face_id
15234 to the end of the line. If the background equals the background
15235 of the frame, we don't have to do anything. */
15236 if (it->face_before_selective_p)
15237 face = FACE_FROM_ID (it->f, it->saved_face_id);
15238 else
15239 face = FACE_FROM_ID (f, it->face_id);
15241 if (FRAME_WINDOW_P (f)
15242 && face->box == FACE_NO_BOX
15243 && face->background == FRAME_BACKGROUND_PIXEL (f)
15244 && !face->stipple)
15245 return;
15247 /* Set the glyph row flag indicating that the face of the last glyph
15248 in the text area has to be drawn to the end of the text area. */
15249 it->glyph_row->fill_line_p = 1;
15251 /* If current character of IT is not ASCII, make sure we have the
15252 ASCII face. This will be automatically undone the next time
15253 get_next_display_element returns a multibyte character. Note
15254 that the character will always be single byte in unibyte text. */
15255 if (!SINGLE_BYTE_CHAR_P (it->c))
15257 it->face_id = FACE_FOR_CHAR (f, face, 0);
15260 if (FRAME_WINDOW_P (f))
15262 /* If the row is empty, add a space with the current face of IT,
15263 so that we know which face to draw. */
15264 if (it->glyph_row->used[TEXT_AREA] == 0)
15266 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15267 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15268 it->glyph_row->used[TEXT_AREA] = 1;
15271 else
15273 /* Save some values that must not be changed. */
15274 int saved_x = it->current_x;
15275 struct text_pos saved_pos;
15276 Lisp_Object saved_object;
15277 enum display_element_type saved_what = it->what;
15278 int saved_face_id = it->face_id;
15280 saved_object = it->object;
15281 saved_pos = it->position;
15283 it->what = IT_CHARACTER;
15284 bzero (&it->position, sizeof it->position);
15285 it->object = make_number (0);
15286 it->c = ' ';
15287 it->len = 1;
15288 it->face_id = face->id;
15290 PRODUCE_GLYPHS (it);
15292 while (it->current_x <= it->last_visible_x)
15293 PRODUCE_GLYPHS (it);
15295 /* Don't count these blanks really. It would let us insert a left
15296 truncation glyph below and make us set the cursor on them, maybe. */
15297 it->current_x = saved_x;
15298 it->object = saved_object;
15299 it->position = saved_pos;
15300 it->what = saved_what;
15301 it->face_id = saved_face_id;
15306 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15307 trailing whitespace. */
15309 static int
15310 trailing_whitespace_p (charpos)
15311 int charpos;
15313 int bytepos = CHAR_TO_BYTE (charpos);
15314 int c = 0;
15316 while (bytepos < ZV_BYTE
15317 && (c = FETCH_CHAR (bytepos),
15318 c == ' ' || c == '\t'))
15319 ++bytepos;
15321 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15323 if (bytepos != PT_BYTE)
15324 return 1;
15326 return 0;
15330 /* Highlight trailing whitespace, if any, in ROW. */
15332 void
15333 highlight_trailing_whitespace (f, row)
15334 struct frame *f;
15335 struct glyph_row *row;
15337 int used = row->used[TEXT_AREA];
15339 if (used)
15341 struct glyph *start = row->glyphs[TEXT_AREA];
15342 struct glyph *glyph = start + used - 1;
15344 /* Skip over glyphs inserted to display the cursor at the
15345 end of a line, for extending the face of the last glyph
15346 to the end of the line on terminals, and for truncation
15347 and continuation glyphs. */
15348 while (glyph >= start
15349 && glyph->type == CHAR_GLYPH
15350 && INTEGERP (glyph->object))
15351 --glyph;
15353 /* If last glyph is a space or stretch, and it's trailing
15354 whitespace, set the face of all trailing whitespace glyphs in
15355 IT->glyph_row to `trailing-whitespace'. */
15356 if (glyph >= start
15357 && BUFFERP (glyph->object)
15358 && (glyph->type == STRETCH_GLYPH
15359 || (glyph->type == CHAR_GLYPH
15360 && glyph->u.ch == ' '))
15361 && trailing_whitespace_p (glyph->charpos))
15363 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15364 if (face_id < 0)
15365 return;
15367 while (glyph >= start
15368 && BUFFERP (glyph->object)
15369 && (glyph->type == STRETCH_GLYPH
15370 || (glyph->type == CHAR_GLYPH
15371 && glyph->u.ch == ' ')))
15372 (glyph--)->face_id = face_id;
15378 /* Value is non-zero if glyph row ROW in window W should be
15379 used to hold the cursor. */
15381 static int
15382 cursor_row_p (w, row)
15383 struct window *w;
15384 struct glyph_row *row;
15386 int cursor_row_p = 1;
15388 if (PT == MATRIX_ROW_END_CHARPOS (row))
15390 /* If the row ends with a newline from a string, we don't want
15391 the cursor there, but we still want it at the start of the
15392 string if the string starts in this row.
15393 If the row is continued it doesn't end in a newline. */
15394 if (CHARPOS (row->end.string_pos) >= 0)
15395 cursor_row_p = (row->continued_p
15396 || PT >= MATRIX_ROW_START_CHARPOS (row));
15397 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15399 /* If the row ends in middle of a real character,
15400 and the line is continued, we want the cursor here.
15401 That's because MATRIX_ROW_END_CHARPOS would equal
15402 PT if PT is before the character. */
15403 if (!row->ends_in_ellipsis_p)
15404 cursor_row_p = row->continued_p;
15405 else
15406 /* If the row ends in an ellipsis, then
15407 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15408 We want that position to be displayed after the ellipsis. */
15409 cursor_row_p = 0;
15411 /* If the row ends at ZV, display the cursor at the end of that
15412 row instead of at the start of the row below. */
15413 else if (row->ends_at_zv_p)
15414 cursor_row_p = 1;
15415 else
15416 cursor_row_p = 0;
15419 return cursor_row_p;
15423 /* Construct the glyph row IT->glyph_row in the desired matrix of
15424 IT->w from text at the current position of IT. See dispextern.h
15425 for an overview of struct it. Value is non-zero if
15426 IT->glyph_row displays text, as opposed to a line displaying ZV
15427 only. */
15429 static int
15430 display_line (it)
15431 struct it *it;
15433 struct glyph_row *row = it->glyph_row;
15434 Lisp_Object overlay_arrow_string;
15436 /* We always start displaying at hpos zero even if hscrolled. */
15437 xassert (it->hpos == 0 && it->current_x == 0);
15439 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15440 >= it->w->desired_matrix->nrows)
15442 it->w->nrows_scale_factor++;
15443 fonts_changed_p = 1;
15444 return 0;
15447 /* Is IT->w showing the region? */
15448 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15450 /* Clear the result glyph row and enable it. */
15451 prepare_desired_row (row);
15453 row->y = it->current_y;
15454 row->start = it->start;
15455 row->continuation_lines_width = it->continuation_lines_width;
15456 row->displays_text_p = 1;
15457 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15458 it->starts_in_middle_of_char_p = 0;
15460 /* Arrange the overlays nicely for our purposes. Usually, we call
15461 display_line on only one line at a time, in which case this
15462 can't really hurt too much, or we call it on lines which appear
15463 one after another in the buffer, in which case all calls to
15464 recenter_overlay_lists but the first will be pretty cheap. */
15465 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15467 /* Move over display elements that are not visible because we are
15468 hscrolled. This may stop at an x-position < IT->first_visible_x
15469 if the first glyph is partially visible or if we hit a line end. */
15470 if (it->current_x < it->first_visible_x)
15472 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15473 MOVE_TO_POS | MOVE_TO_X);
15476 /* Get the initial row height. This is either the height of the
15477 text hscrolled, if there is any, or zero. */
15478 row->ascent = it->max_ascent;
15479 row->height = it->max_ascent + it->max_descent;
15480 row->phys_ascent = it->max_phys_ascent;
15481 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15482 row->extra_line_spacing = it->max_extra_line_spacing;
15484 /* Loop generating characters. The loop is left with IT on the next
15485 character to display. */
15486 while (1)
15488 int n_glyphs_before, hpos_before, x_before;
15489 int x, i, nglyphs;
15490 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15492 /* Retrieve the next thing to display. Value is zero if end of
15493 buffer reached. */
15494 if (!get_next_display_element (it))
15496 /* Maybe add a space at the end of this line that is used to
15497 display the cursor there under X. Set the charpos of the
15498 first glyph of blank lines not corresponding to any text
15499 to -1. */
15500 #ifdef HAVE_WINDOW_SYSTEM
15501 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15502 row->exact_window_width_line_p = 1;
15503 else
15504 #endif /* HAVE_WINDOW_SYSTEM */
15505 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15506 || row->used[TEXT_AREA] == 0)
15508 row->glyphs[TEXT_AREA]->charpos = -1;
15509 row->displays_text_p = 0;
15511 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15512 && (!MINI_WINDOW_P (it->w)
15513 || (minibuf_level && EQ (it->window, minibuf_window))))
15514 row->indicate_empty_line_p = 1;
15517 it->continuation_lines_width = 0;
15518 row->ends_at_zv_p = 1;
15519 break;
15522 /* Now, get the metrics of what we want to display. This also
15523 generates glyphs in `row' (which is IT->glyph_row). */
15524 n_glyphs_before = row->used[TEXT_AREA];
15525 x = it->current_x;
15527 /* Remember the line height so far in case the next element doesn't
15528 fit on the line. */
15529 if (!it->truncate_lines_p)
15531 ascent = it->max_ascent;
15532 descent = it->max_descent;
15533 phys_ascent = it->max_phys_ascent;
15534 phys_descent = it->max_phys_descent;
15537 PRODUCE_GLYPHS (it);
15539 /* If this display element was in marginal areas, continue with
15540 the next one. */
15541 if (it->area != TEXT_AREA)
15543 row->ascent = max (row->ascent, it->max_ascent);
15544 row->height = max (row->height, it->max_ascent + it->max_descent);
15545 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15546 row->phys_height = max (row->phys_height,
15547 it->max_phys_ascent + it->max_phys_descent);
15548 row->extra_line_spacing = max (row->extra_line_spacing,
15549 it->max_extra_line_spacing);
15550 set_iterator_to_next (it, 1);
15551 continue;
15554 /* Does the display element fit on the line? If we truncate
15555 lines, we should draw past the right edge of the window. If
15556 we don't truncate, we want to stop so that we can display the
15557 continuation glyph before the right margin. If lines are
15558 continued, there are two possible strategies for characters
15559 resulting in more than 1 glyph (e.g. tabs): Display as many
15560 glyphs as possible in this line and leave the rest for the
15561 continuation line, or display the whole element in the next
15562 line. Original redisplay did the former, so we do it also. */
15563 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15564 hpos_before = it->hpos;
15565 x_before = x;
15567 if (/* Not a newline. */
15568 nglyphs > 0
15569 /* Glyphs produced fit entirely in the line. */
15570 && it->current_x < it->last_visible_x)
15572 it->hpos += nglyphs;
15573 row->ascent = max (row->ascent, it->max_ascent);
15574 row->height = max (row->height, it->max_ascent + it->max_descent);
15575 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15576 row->phys_height = max (row->phys_height,
15577 it->max_phys_ascent + it->max_phys_descent);
15578 row->extra_line_spacing = max (row->extra_line_spacing,
15579 it->max_extra_line_spacing);
15580 if (it->current_x - it->pixel_width < it->first_visible_x)
15581 row->x = x - it->first_visible_x;
15583 else
15585 int new_x;
15586 struct glyph *glyph;
15588 for (i = 0; i < nglyphs; ++i, x = new_x)
15590 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15591 new_x = x + glyph->pixel_width;
15593 if (/* Lines are continued. */
15594 !it->truncate_lines_p
15595 && (/* Glyph doesn't fit on the line. */
15596 new_x > it->last_visible_x
15597 /* Or it fits exactly on a window system frame. */
15598 || (new_x == it->last_visible_x
15599 && FRAME_WINDOW_P (it->f))))
15601 /* End of a continued line. */
15603 if (it->hpos == 0
15604 || (new_x == it->last_visible_x
15605 && FRAME_WINDOW_P (it->f)))
15607 /* Current glyph is the only one on the line or
15608 fits exactly on the line. We must continue
15609 the line because we can't draw the cursor
15610 after the glyph. */
15611 row->continued_p = 1;
15612 it->current_x = new_x;
15613 it->continuation_lines_width += new_x;
15614 ++it->hpos;
15615 if (i == nglyphs - 1)
15617 set_iterator_to_next (it, 1);
15618 #ifdef HAVE_WINDOW_SYSTEM
15619 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15621 if (!get_next_display_element (it))
15623 row->exact_window_width_line_p = 1;
15624 it->continuation_lines_width = 0;
15625 row->continued_p = 0;
15626 row->ends_at_zv_p = 1;
15628 else if (ITERATOR_AT_END_OF_LINE_P (it))
15630 row->continued_p = 0;
15631 row->exact_window_width_line_p = 1;
15634 #endif /* HAVE_WINDOW_SYSTEM */
15637 else if (CHAR_GLYPH_PADDING_P (*glyph)
15638 && !FRAME_WINDOW_P (it->f))
15640 /* A padding glyph that doesn't fit on this line.
15641 This means the whole character doesn't fit
15642 on the line. */
15643 row->used[TEXT_AREA] = n_glyphs_before;
15645 /* Fill the rest of the row with continuation
15646 glyphs like in 20.x. */
15647 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15648 < row->glyphs[1 + TEXT_AREA])
15649 produce_special_glyphs (it, IT_CONTINUATION);
15651 row->continued_p = 1;
15652 it->current_x = x_before;
15653 it->continuation_lines_width += x_before;
15655 /* Restore the height to what it was before the
15656 element not fitting on the line. */
15657 it->max_ascent = ascent;
15658 it->max_descent = descent;
15659 it->max_phys_ascent = phys_ascent;
15660 it->max_phys_descent = phys_descent;
15662 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15664 /* A TAB that extends past the right edge of the
15665 window. This produces a single glyph on
15666 window system frames. We leave the glyph in
15667 this row and let it fill the row, but don't
15668 consume the TAB. */
15669 it->continuation_lines_width += it->last_visible_x;
15670 row->ends_in_middle_of_char_p = 1;
15671 row->continued_p = 1;
15672 glyph->pixel_width = it->last_visible_x - x;
15673 it->starts_in_middle_of_char_p = 1;
15675 else
15677 /* Something other than a TAB that draws past
15678 the right edge of the window. Restore
15679 positions to values before the element. */
15680 row->used[TEXT_AREA] = n_glyphs_before + i;
15682 /* Display continuation glyphs. */
15683 if (!FRAME_WINDOW_P (it->f))
15684 produce_special_glyphs (it, IT_CONTINUATION);
15685 row->continued_p = 1;
15687 it->current_x = x_before;
15688 it->continuation_lines_width += x;
15689 extend_face_to_end_of_line (it);
15691 if (nglyphs > 1 && i > 0)
15693 row->ends_in_middle_of_char_p = 1;
15694 it->starts_in_middle_of_char_p = 1;
15697 /* Restore the height to what it was before the
15698 element not fitting on the line. */
15699 it->max_ascent = ascent;
15700 it->max_descent = descent;
15701 it->max_phys_ascent = phys_ascent;
15702 it->max_phys_descent = phys_descent;
15705 break;
15707 else if (new_x > it->first_visible_x)
15709 /* Increment number of glyphs actually displayed. */
15710 ++it->hpos;
15712 if (x < it->first_visible_x)
15713 /* Glyph is partially visible, i.e. row starts at
15714 negative X position. */
15715 row->x = x - it->first_visible_x;
15717 else
15719 /* Glyph is completely off the left margin of the
15720 window. This should not happen because of the
15721 move_it_in_display_line at the start of this
15722 function, unless the text display area of the
15723 window is empty. */
15724 xassert (it->first_visible_x <= it->last_visible_x);
15728 row->ascent = max (row->ascent, it->max_ascent);
15729 row->height = max (row->height, it->max_ascent + it->max_descent);
15730 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15731 row->phys_height = max (row->phys_height,
15732 it->max_phys_ascent + it->max_phys_descent);
15733 row->extra_line_spacing = max (row->extra_line_spacing,
15734 it->max_extra_line_spacing);
15736 /* End of this display line if row is continued. */
15737 if (row->continued_p || row->ends_at_zv_p)
15738 break;
15741 at_end_of_line:
15742 /* Is this a line end? If yes, we're also done, after making
15743 sure that a non-default face is extended up to the right
15744 margin of the window. */
15745 if (ITERATOR_AT_END_OF_LINE_P (it))
15747 int used_before = row->used[TEXT_AREA];
15749 row->ends_in_newline_from_string_p = STRINGP (it->object);
15751 #ifdef HAVE_WINDOW_SYSTEM
15752 /* Add a space at the end of the line that is used to
15753 display the cursor there. */
15754 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15755 append_space_for_newline (it, 0);
15756 #endif /* HAVE_WINDOW_SYSTEM */
15758 /* Extend the face to the end of the line. */
15759 extend_face_to_end_of_line (it);
15761 /* Make sure we have the position. */
15762 if (used_before == 0)
15763 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15765 /* Consume the line end. This skips over invisible lines. */
15766 set_iterator_to_next (it, 1);
15767 it->continuation_lines_width = 0;
15768 break;
15771 /* Proceed with next display element. Note that this skips
15772 over lines invisible because of selective display. */
15773 set_iterator_to_next (it, 1);
15775 /* If we truncate lines, we are done when the last displayed
15776 glyphs reach past the right margin of the window. */
15777 if (it->truncate_lines_p
15778 && (FRAME_WINDOW_P (it->f)
15779 ? (it->current_x >= it->last_visible_x)
15780 : (it->current_x > it->last_visible_x)))
15782 /* Maybe add truncation glyphs. */
15783 if (!FRAME_WINDOW_P (it->f))
15785 int i, n;
15787 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15788 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15789 break;
15791 for (n = row->used[TEXT_AREA]; i < n; ++i)
15793 row->used[TEXT_AREA] = i;
15794 produce_special_glyphs (it, IT_TRUNCATION);
15797 #ifdef HAVE_WINDOW_SYSTEM
15798 else
15800 /* Don't truncate if we can overflow newline into fringe. */
15801 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15803 if (!get_next_display_element (it))
15805 it->continuation_lines_width = 0;
15806 row->ends_at_zv_p = 1;
15807 row->exact_window_width_line_p = 1;
15808 break;
15810 if (ITERATOR_AT_END_OF_LINE_P (it))
15812 row->exact_window_width_line_p = 1;
15813 goto at_end_of_line;
15817 #endif /* HAVE_WINDOW_SYSTEM */
15819 row->truncated_on_right_p = 1;
15820 it->continuation_lines_width = 0;
15821 reseat_at_next_visible_line_start (it, 0);
15822 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15823 it->hpos = hpos_before;
15824 it->current_x = x_before;
15825 break;
15829 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15830 at the left window margin. */
15831 if (it->first_visible_x
15832 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15834 if (!FRAME_WINDOW_P (it->f))
15835 insert_left_trunc_glyphs (it);
15836 row->truncated_on_left_p = 1;
15839 /* If the start of this line is the overlay arrow-position, then
15840 mark this glyph row as the one containing the overlay arrow.
15841 This is clearly a mess with variable size fonts. It would be
15842 better to let it be displayed like cursors under X. */
15843 if ((row->displays_text_p || !overlay_arrow_seen)
15844 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15845 !NILP (overlay_arrow_string)))
15847 /* Overlay arrow in window redisplay is a fringe bitmap. */
15848 if (STRINGP (overlay_arrow_string))
15850 struct glyph_row *arrow_row
15851 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15852 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15853 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15854 struct glyph *p = row->glyphs[TEXT_AREA];
15855 struct glyph *p2, *end;
15857 /* Copy the arrow glyphs. */
15858 while (glyph < arrow_end)
15859 *p++ = *glyph++;
15861 /* Throw away padding glyphs. */
15862 p2 = p;
15863 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15864 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15865 ++p2;
15866 if (p2 > p)
15868 while (p2 < end)
15869 *p++ = *p2++;
15870 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15873 else
15875 xassert (INTEGERP (overlay_arrow_string));
15876 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15878 overlay_arrow_seen = 1;
15881 /* Compute pixel dimensions of this line. */
15882 compute_line_metrics (it);
15884 /* Remember the position at which this line ends. */
15885 row->end = it->current;
15887 /* Record whether this row ends inside an ellipsis. */
15888 row->ends_in_ellipsis_p
15889 = (it->method == GET_FROM_DISPLAY_VECTOR
15890 && it->ellipsis_p);
15892 /* Save fringe bitmaps in this row. */
15893 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15894 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15895 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15896 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15898 it->left_user_fringe_bitmap = 0;
15899 it->left_user_fringe_face_id = 0;
15900 it->right_user_fringe_bitmap = 0;
15901 it->right_user_fringe_face_id = 0;
15903 /* Maybe set the cursor. */
15904 if (it->w->cursor.vpos < 0
15905 && PT >= MATRIX_ROW_START_CHARPOS (row)
15906 && PT <= MATRIX_ROW_END_CHARPOS (row)
15907 && cursor_row_p (it->w, row))
15908 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15910 /* Highlight trailing whitespace. */
15911 if (!NILP (Vshow_trailing_whitespace))
15912 highlight_trailing_whitespace (it->f, it->glyph_row);
15914 /* Prepare for the next line. This line starts horizontally at (X
15915 HPOS) = (0 0). Vertical positions are incremented. As a
15916 convenience for the caller, IT->glyph_row is set to the next
15917 row to be used. */
15918 it->current_x = it->hpos = 0;
15919 it->current_y += row->height;
15920 ++it->vpos;
15921 ++it->glyph_row;
15922 it->start = it->current;
15923 return row->displays_text_p;
15928 /***********************************************************************
15929 Menu Bar
15930 ***********************************************************************/
15932 /* Redisplay the menu bar in the frame for window W.
15934 The menu bar of X frames that don't have X toolkit support is
15935 displayed in a special window W->frame->menu_bar_window.
15937 The menu bar of terminal frames is treated specially as far as
15938 glyph matrices are concerned. Menu bar lines are not part of
15939 windows, so the update is done directly on the frame matrix rows
15940 for the menu bar. */
15942 static void
15943 display_menu_bar (w)
15944 struct window *w;
15946 struct frame *f = XFRAME (WINDOW_FRAME (w));
15947 struct it it;
15948 Lisp_Object items;
15949 int i;
15951 /* Don't do all this for graphical frames. */
15952 #ifdef HAVE_NTGUI
15953 if (!NILP (Vwindow_system))
15954 return;
15955 #endif
15956 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15957 if (FRAME_X_P (f))
15958 return;
15959 #endif
15960 #ifdef MAC_OS
15961 if (FRAME_MAC_P (f))
15962 return;
15963 #endif
15965 #ifdef USE_X_TOOLKIT
15966 xassert (!FRAME_WINDOW_P (f));
15967 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15968 it.first_visible_x = 0;
15969 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15970 #else /* not USE_X_TOOLKIT */
15971 if (FRAME_WINDOW_P (f))
15973 /* Menu bar lines are displayed in the desired matrix of the
15974 dummy window menu_bar_window. */
15975 struct window *menu_w;
15976 xassert (WINDOWP (f->menu_bar_window));
15977 menu_w = XWINDOW (f->menu_bar_window);
15978 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15979 MENU_FACE_ID);
15980 it.first_visible_x = 0;
15981 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15983 else
15985 /* This is a TTY frame, i.e. character hpos/vpos are used as
15986 pixel x/y. */
15987 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15988 MENU_FACE_ID);
15989 it.first_visible_x = 0;
15990 it.last_visible_x = FRAME_COLS (f);
15992 #endif /* not USE_X_TOOLKIT */
15994 if (! mode_line_inverse_video)
15995 /* Force the menu-bar to be displayed in the default face. */
15996 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15998 /* Clear all rows of the menu bar. */
15999 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16001 struct glyph_row *row = it.glyph_row + i;
16002 clear_glyph_row (row);
16003 row->enabled_p = 1;
16004 row->full_width_p = 1;
16007 /* Display all items of the menu bar. */
16008 items = FRAME_MENU_BAR_ITEMS (it.f);
16009 for (i = 0; i < XVECTOR (items)->size; i += 4)
16011 Lisp_Object string;
16013 /* Stop at nil string. */
16014 string = AREF (items, i + 1);
16015 if (NILP (string))
16016 break;
16018 /* Remember where item was displayed. */
16019 AREF (items, i + 3) = make_number (it.hpos);
16021 /* Display the item, pad with one space. */
16022 if (it.current_x < it.last_visible_x)
16023 display_string (NULL, string, Qnil, 0, 0, &it,
16024 SCHARS (string) + 1, 0, 0, -1);
16027 /* Fill out the line with spaces. */
16028 if (it.current_x < it.last_visible_x)
16029 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16031 /* Compute the total height of the lines. */
16032 compute_line_metrics (&it);
16037 /***********************************************************************
16038 Mode Line
16039 ***********************************************************************/
16041 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16042 FORCE is non-zero, redisplay mode lines unconditionally.
16043 Otherwise, redisplay only mode lines that are garbaged. Value is
16044 the number of windows whose mode lines were redisplayed. */
16046 static int
16047 redisplay_mode_lines (window, force)
16048 Lisp_Object window;
16049 int force;
16051 int nwindows = 0;
16053 while (!NILP (window))
16055 struct window *w = XWINDOW (window);
16057 if (WINDOWP (w->hchild))
16058 nwindows += redisplay_mode_lines (w->hchild, force);
16059 else if (WINDOWP (w->vchild))
16060 nwindows += redisplay_mode_lines (w->vchild, force);
16061 else if (force
16062 || FRAME_GARBAGED_P (XFRAME (w->frame))
16063 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16065 struct text_pos lpoint;
16066 struct buffer *old = current_buffer;
16068 /* Set the window's buffer for the mode line display. */
16069 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16070 set_buffer_internal_1 (XBUFFER (w->buffer));
16072 /* Point refers normally to the selected window. For any
16073 other window, set up appropriate value. */
16074 if (!EQ (window, selected_window))
16076 struct text_pos pt;
16078 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16079 if (CHARPOS (pt) < BEGV)
16080 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16081 else if (CHARPOS (pt) > (ZV - 1))
16082 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16083 else
16084 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16087 /* Display mode lines. */
16088 clear_glyph_matrix (w->desired_matrix);
16089 if (display_mode_lines (w))
16091 ++nwindows;
16092 w->must_be_updated_p = 1;
16095 /* Restore old settings. */
16096 set_buffer_internal_1 (old);
16097 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16100 window = w->next;
16103 return nwindows;
16107 /* Display the mode and/or top line of window W. Value is the number
16108 of mode lines displayed. */
16110 static int
16111 display_mode_lines (w)
16112 struct window *w;
16114 Lisp_Object old_selected_window, old_selected_frame;
16115 int n = 0;
16117 old_selected_frame = selected_frame;
16118 selected_frame = w->frame;
16119 old_selected_window = selected_window;
16120 XSETWINDOW (selected_window, w);
16122 /* These will be set while the mode line specs are processed. */
16123 line_number_displayed = 0;
16124 w->column_number_displayed = Qnil;
16126 if (WINDOW_WANTS_MODELINE_P (w))
16128 struct window *sel_w = XWINDOW (old_selected_window);
16130 /* Select mode line face based on the real selected window. */
16131 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16132 current_buffer->mode_line_format);
16133 ++n;
16136 if (WINDOW_WANTS_HEADER_LINE_P (w))
16138 display_mode_line (w, HEADER_LINE_FACE_ID,
16139 current_buffer->header_line_format);
16140 ++n;
16143 selected_frame = old_selected_frame;
16144 selected_window = old_selected_window;
16145 return n;
16149 /* Display mode or top line of window W. FACE_ID specifies which line
16150 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16151 FORMAT is the mode line format to display. Value is the pixel
16152 height of the mode line displayed. */
16154 static int
16155 display_mode_line (w, face_id, format)
16156 struct window *w;
16157 enum face_id face_id;
16158 Lisp_Object format;
16160 struct it it;
16161 struct face *face;
16162 int count = SPECPDL_INDEX ();
16164 init_iterator (&it, w, -1, -1, NULL, face_id);
16165 prepare_desired_row (it.glyph_row);
16167 it.glyph_row->mode_line_p = 1;
16169 if (! mode_line_inverse_video)
16170 /* Force the mode-line to be displayed in the default face. */
16171 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16173 record_unwind_protect (unwind_format_mode_line,
16174 format_mode_line_unwind_data (NULL, 0));
16176 mode_line_target = MODE_LINE_DISPLAY;
16178 /* Temporarily make frame's keyboard the current kboard so that
16179 kboard-local variables in the mode_line_format will get the right
16180 values. */
16181 push_frame_kboard (it.f);
16182 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16183 pop_frame_kboard ();
16185 unbind_to (count, Qnil);
16187 /* Fill up with spaces. */
16188 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16190 compute_line_metrics (&it);
16191 it.glyph_row->full_width_p = 1;
16192 it.glyph_row->continued_p = 0;
16193 it.glyph_row->truncated_on_left_p = 0;
16194 it.glyph_row->truncated_on_right_p = 0;
16196 /* Make a 3D mode-line have a shadow at its right end. */
16197 face = FACE_FROM_ID (it.f, face_id);
16198 extend_face_to_end_of_line (&it);
16199 if (face->box != FACE_NO_BOX)
16201 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16202 + it.glyph_row->used[TEXT_AREA] - 1);
16203 last->right_box_line_p = 1;
16206 return it.glyph_row->height;
16209 /* Move element ELT in LIST to the front of LIST.
16210 Return the updated list. */
16212 static Lisp_Object
16213 move_elt_to_front (elt, list)
16214 Lisp_Object elt, list;
16216 register Lisp_Object tail, prev;
16217 register Lisp_Object tem;
16219 tail = list;
16220 prev = Qnil;
16221 while (CONSP (tail))
16223 tem = XCAR (tail);
16225 if (EQ (elt, tem))
16227 /* Splice out the link TAIL. */
16228 if (NILP (prev))
16229 list = XCDR (tail);
16230 else
16231 Fsetcdr (prev, XCDR (tail));
16233 /* Now make it the first. */
16234 Fsetcdr (tail, list);
16235 return tail;
16237 else
16238 prev = tail;
16239 tail = XCDR (tail);
16240 QUIT;
16243 /* Not found--return unchanged LIST. */
16244 return list;
16247 /* Contribute ELT to the mode line for window IT->w. How it
16248 translates into text depends on its data type.
16250 IT describes the display environment in which we display, as usual.
16252 DEPTH is the depth in recursion. It is used to prevent
16253 infinite recursion here.
16255 FIELD_WIDTH is the number of characters the display of ELT should
16256 occupy in the mode line, and PRECISION is the maximum number of
16257 characters to display from ELT's representation. See
16258 display_string for details.
16260 Returns the hpos of the end of the text generated by ELT.
16262 PROPS is a property list to add to any string we encounter.
16264 If RISKY is nonzero, remove (disregard) any properties in any string
16265 we encounter, and ignore :eval and :propertize.
16267 The global variable `mode_line_target' determines whether the
16268 output is passed to `store_mode_line_noprop',
16269 `store_mode_line_string', or `display_string'. */
16271 static int
16272 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16273 struct it *it;
16274 int depth;
16275 int field_width, precision;
16276 Lisp_Object elt, props;
16277 int risky;
16279 int n = 0, field, prec;
16280 int literal = 0;
16282 tail_recurse:
16283 if (depth > 100)
16284 elt = build_string ("*too-deep*");
16286 depth++;
16288 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16290 case Lisp_String:
16292 /* A string: output it and check for %-constructs within it. */
16293 unsigned char c;
16294 int offset = 0;
16296 if (SCHARS (elt) > 0
16297 && (!NILP (props) || risky))
16299 Lisp_Object oprops, aelt;
16300 oprops = Ftext_properties_at (make_number (0), elt);
16302 /* If the starting string's properties are not what
16303 we want, translate the string. Also, if the string
16304 is risky, do that anyway. */
16306 if (NILP (Fequal (props, oprops)) || risky)
16308 /* If the starting string has properties,
16309 merge the specified ones onto the existing ones. */
16310 if (! NILP (oprops) && !risky)
16312 Lisp_Object tem;
16314 oprops = Fcopy_sequence (oprops);
16315 tem = props;
16316 while (CONSP (tem))
16318 oprops = Fplist_put (oprops, XCAR (tem),
16319 XCAR (XCDR (tem)));
16320 tem = XCDR (XCDR (tem));
16322 props = oprops;
16325 aelt = Fassoc (elt, mode_line_proptrans_alist);
16326 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16328 /* AELT is what we want. Move it to the front
16329 without consing. */
16330 elt = XCAR (aelt);
16331 mode_line_proptrans_alist
16332 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16334 else
16336 Lisp_Object tem;
16338 /* If AELT has the wrong props, it is useless.
16339 so get rid of it. */
16340 if (! NILP (aelt))
16341 mode_line_proptrans_alist
16342 = Fdelq (aelt, mode_line_proptrans_alist);
16344 elt = Fcopy_sequence (elt);
16345 Fset_text_properties (make_number (0), Flength (elt),
16346 props, elt);
16347 /* Add this item to mode_line_proptrans_alist. */
16348 mode_line_proptrans_alist
16349 = Fcons (Fcons (elt, props),
16350 mode_line_proptrans_alist);
16351 /* Truncate mode_line_proptrans_alist
16352 to at most 50 elements. */
16353 tem = Fnthcdr (make_number (50),
16354 mode_line_proptrans_alist);
16355 if (! NILP (tem))
16356 XSETCDR (tem, Qnil);
16361 offset = 0;
16363 if (literal)
16365 prec = precision - n;
16366 switch (mode_line_target)
16368 case MODE_LINE_NOPROP:
16369 case MODE_LINE_TITLE:
16370 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16371 break;
16372 case MODE_LINE_STRING:
16373 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16374 break;
16375 case MODE_LINE_DISPLAY:
16376 n += display_string (NULL, elt, Qnil, 0, 0, it,
16377 0, prec, 0, STRING_MULTIBYTE (elt));
16378 break;
16381 break;
16384 /* Handle the non-literal case. */
16386 while ((precision <= 0 || n < precision)
16387 && SREF (elt, offset) != 0
16388 && (mode_line_target != MODE_LINE_DISPLAY
16389 || it->current_x < it->last_visible_x))
16391 int last_offset = offset;
16393 /* Advance to end of string or next format specifier. */
16394 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16397 if (offset - 1 != last_offset)
16399 int nchars, nbytes;
16401 /* Output to end of string or up to '%'. Field width
16402 is length of string. Don't output more than
16403 PRECISION allows us. */
16404 offset--;
16406 prec = c_string_width (SDATA (elt) + last_offset,
16407 offset - last_offset, precision - n,
16408 &nchars, &nbytes);
16410 switch (mode_line_target)
16412 case MODE_LINE_NOPROP:
16413 case MODE_LINE_TITLE:
16414 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16415 break;
16416 case MODE_LINE_STRING:
16418 int bytepos = last_offset;
16419 int charpos = string_byte_to_char (elt, bytepos);
16420 int endpos = (precision <= 0
16421 ? string_byte_to_char (elt, offset)
16422 : charpos + nchars);
16424 n += store_mode_line_string (NULL,
16425 Fsubstring (elt, make_number (charpos),
16426 make_number (endpos)),
16427 0, 0, 0, Qnil);
16429 break;
16430 case MODE_LINE_DISPLAY:
16432 int bytepos = last_offset;
16433 int charpos = string_byte_to_char (elt, bytepos);
16434 n += display_string (NULL, elt, Qnil, 0, charpos,
16435 it, 0, prec, 0,
16436 STRING_MULTIBYTE (elt));
16438 break;
16441 else /* c == '%' */
16443 int percent_position = offset;
16445 /* Get the specified minimum width. Zero means
16446 don't pad. */
16447 field = 0;
16448 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16449 field = field * 10 + c - '0';
16451 /* Don't pad beyond the total padding allowed. */
16452 if (field_width - n > 0 && field > field_width - n)
16453 field = field_width - n;
16455 /* Note that either PRECISION <= 0 or N < PRECISION. */
16456 prec = precision - n;
16458 if (c == 'M')
16459 n += display_mode_element (it, depth, field, prec,
16460 Vglobal_mode_string, props,
16461 risky);
16462 else if (c != 0)
16464 int multibyte;
16465 int bytepos, charpos;
16466 unsigned char *spec;
16468 bytepos = percent_position;
16469 charpos = (STRING_MULTIBYTE (elt)
16470 ? string_byte_to_char (elt, bytepos)
16471 : bytepos);
16473 spec
16474 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16476 switch (mode_line_target)
16478 case MODE_LINE_NOPROP:
16479 case MODE_LINE_TITLE:
16480 n += store_mode_line_noprop (spec, field, prec);
16481 break;
16482 case MODE_LINE_STRING:
16484 int len = strlen (spec);
16485 Lisp_Object tem = make_string (spec, len);
16486 props = Ftext_properties_at (make_number (charpos), elt);
16487 /* Should only keep face property in props */
16488 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16490 break;
16491 case MODE_LINE_DISPLAY:
16493 int nglyphs_before, nwritten;
16495 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16496 nwritten = display_string (spec, Qnil, elt,
16497 charpos, 0, it,
16498 field, prec, 0,
16499 multibyte);
16501 /* Assign to the glyphs written above the
16502 string where the `%x' came from, position
16503 of the `%'. */
16504 if (nwritten > 0)
16506 struct glyph *glyph
16507 = (it->glyph_row->glyphs[TEXT_AREA]
16508 + nglyphs_before);
16509 int i;
16511 for (i = 0; i < nwritten; ++i)
16513 glyph[i].object = elt;
16514 glyph[i].charpos = charpos;
16517 n += nwritten;
16520 break;
16523 else /* c == 0 */
16524 break;
16528 break;
16530 case Lisp_Symbol:
16531 /* A symbol: process the value of the symbol recursively
16532 as if it appeared here directly. Avoid error if symbol void.
16533 Special case: if value of symbol is a string, output the string
16534 literally. */
16536 register Lisp_Object tem;
16538 /* If the variable is not marked as risky to set
16539 then its contents are risky to use. */
16540 if (NILP (Fget (elt, Qrisky_local_variable)))
16541 risky = 1;
16543 tem = Fboundp (elt);
16544 if (!NILP (tem))
16546 tem = Fsymbol_value (elt);
16547 /* If value is a string, output that string literally:
16548 don't check for % within it. */
16549 if (STRINGP (tem))
16550 literal = 1;
16552 if (!EQ (tem, elt))
16554 /* Give up right away for nil or t. */
16555 elt = tem;
16556 goto tail_recurse;
16560 break;
16562 case Lisp_Cons:
16564 register Lisp_Object car, tem;
16566 /* A cons cell: five distinct cases.
16567 If first element is :eval or :propertize, do something special.
16568 If first element is a string or a cons, process all the elements
16569 and effectively concatenate them.
16570 If first element is a negative number, truncate displaying cdr to
16571 at most that many characters. If positive, pad (with spaces)
16572 to at least that many characters.
16573 If first element is a symbol, process the cadr or caddr recursively
16574 according to whether the symbol's value is non-nil or nil. */
16575 car = XCAR (elt);
16576 if (EQ (car, QCeval))
16578 /* An element of the form (:eval FORM) means evaluate FORM
16579 and use the result as mode line elements. */
16581 if (risky)
16582 break;
16584 if (CONSP (XCDR (elt)))
16586 Lisp_Object spec;
16587 spec = safe_eval (XCAR (XCDR (elt)));
16588 n += display_mode_element (it, depth, field_width - n,
16589 precision - n, spec, props,
16590 risky);
16593 else if (EQ (car, QCpropertize))
16595 /* An element of the form (:propertize ELT PROPS...)
16596 means display ELT but applying properties PROPS. */
16598 if (risky)
16599 break;
16601 if (CONSP (XCDR (elt)))
16602 n += display_mode_element (it, depth, field_width - n,
16603 precision - n, XCAR (XCDR (elt)),
16604 XCDR (XCDR (elt)), risky);
16606 else if (SYMBOLP (car))
16608 tem = Fboundp (car);
16609 elt = XCDR (elt);
16610 if (!CONSP (elt))
16611 goto invalid;
16612 /* elt is now the cdr, and we know it is a cons cell.
16613 Use its car if CAR has a non-nil value. */
16614 if (!NILP (tem))
16616 tem = Fsymbol_value (car);
16617 if (!NILP (tem))
16619 elt = XCAR (elt);
16620 goto tail_recurse;
16623 /* Symbol's value is nil (or symbol is unbound)
16624 Get the cddr of the original list
16625 and if possible find the caddr and use that. */
16626 elt = XCDR (elt);
16627 if (NILP (elt))
16628 break;
16629 else if (!CONSP (elt))
16630 goto invalid;
16631 elt = XCAR (elt);
16632 goto tail_recurse;
16634 else if (INTEGERP (car))
16636 register int lim = XINT (car);
16637 elt = XCDR (elt);
16638 if (lim < 0)
16640 /* Negative int means reduce maximum width. */
16641 if (precision <= 0)
16642 precision = -lim;
16643 else
16644 precision = min (precision, -lim);
16646 else if (lim > 0)
16648 /* Padding specified. Don't let it be more than
16649 current maximum. */
16650 if (precision > 0)
16651 lim = min (precision, lim);
16653 /* If that's more padding than already wanted, queue it.
16654 But don't reduce padding already specified even if
16655 that is beyond the current truncation point. */
16656 field_width = max (lim, field_width);
16658 goto tail_recurse;
16660 else if (STRINGP (car) || CONSP (car))
16662 register int limit = 50;
16663 /* Limit is to protect against circular lists. */
16664 while (CONSP (elt)
16665 && --limit > 0
16666 && (precision <= 0 || n < precision))
16668 n += display_mode_element (it, depth,
16669 /* Do padding only after the last
16670 element in the list. */
16671 (! CONSP (XCDR (elt))
16672 ? field_width - n
16673 : 0),
16674 precision - n, XCAR (elt),
16675 props, risky);
16676 elt = XCDR (elt);
16680 break;
16682 default:
16683 invalid:
16684 elt = build_string ("*invalid*");
16685 goto tail_recurse;
16688 /* Pad to FIELD_WIDTH. */
16689 if (field_width > 0 && n < field_width)
16691 switch (mode_line_target)
16693 case MODE_LINE_NOPROP:
16694 case MODE_LINE_TITLE:
16695 n += store_mode_line_noprop ("", field_width - n, 0);
16696 break;
16697 case MODE_LINE_STRING:
16698 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16699 break;
16700 case MODE_LINE_DISPLAY:
16701 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16702 0, 0, 0);
16703 break;
16707 return n;
16710 /* Store a mode-line string element in mode_line_string_list.
16712 If STRING is non-null, display that C string. Otherwise, the Lisp
16713 string LISP_STRING is displayed.
16715 FIELD_WIDTH is the minimum number of output glyphs to produce.
16716 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16717 with spaces. FIELD_WIDTH <= 0 means don't pad.
16719 PRECISION is the maximum number of characters to output from
16720 STRING. PRECISION <= 0 means don't truncate the string.
16722 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16723 properties to the string.
16725 PROPS are the properties to add to the string.
16726 The mode_line_string_face face property is always added to the string.
16729 static int
16730 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16731 char *string;
16732 Lisp_Object lisp_string;
16733 int copy_string;
16734 int field_width;
16735 int precision;
16736 Lisp_Object props;
16738 int len;
16739 int n = 0;
16741 if (string != NULL)
16743 len = strlen (string);
16744 if (precision > 0 && len > precision)
16745 len = precision;
16746 lisp_string = make_string (string, len);
16747 if (NILP (props))
16748 props = mode_line_string_face_prop;
16749 else if (!NILP (mode_line_string_face))
16751 Lisp_Object face = Fplist_get (props, Qface);
16752 props = Fcopy_sequence (props);
16753 if (NILP (face))
16754 face = mode_line_string_face;
16755 else
16756 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16757 props = Fplist_put (props, Qface, face);
16759 Fadd_text_properties (make_number (0), make_number (len),
16760 props, lisp_string);
16762 else
16764 len = XFASTINT (Flength (lisp_string));
16765 if (precision > 0 && len > precision)
16767 len = precision;
16768 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16769 precision = -1;
16771 if (!NILP (mode_line_string_face))
16773 Lisp_Object face;
16774 if (NILP (props))
16775 props = Ftext_properties_at (make_number (0), lisp_string);
16776 face = Fplist_get (props, Qface);
16777 if (NILP (face))
16778 face = mode_line_string_face;
16779 else
16780 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16781 props = Fcons (Qface, Fcons (face, Qnil));
16782 if (copy_string)
16783 lisp_string = Fcopy_sequence (lisp_string);
16785 if (!NILP (props))
16786 Fadd_text_properties (make_number (0), make_number (len),
16787 props, lisp_string);
16790 if (len > 0)
16792 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16793 n += len;
16796 if (field_width > len)
16798 field_width -= len;
16799 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16800 if (!NILP (props))
16801 Fadd_text_properties (make_number (0), make_number (field_width),
16802 props, lisp_string);
16803 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16804 n += field_width;
16807 return n;
16811 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16812 1, 4, 0,
16813 doc: /* Format a string out of a mode line format specification.
16814 First arg FORMAT specifies the mode line format (see `mode-line-format'
16815 for details) to use.
16817 Optional second arg FACE specifies the face property to put
16818 on all characters for which no face is specified.
16819 t means whatever face the window's mode line currently uses
16820 \(either `mode-line' or `mode-line-inactive', depending).
16821 nil means the default is no face property.
16822 If FACE is an integer, the value string has no text properties.
16824 Optional third and fourth args WINDOW and BUFFER specify the window
16825 and buffer to use as the context for the formatting (defaults
16826 are the selected window and the window's buffer). */)
16827 (format, face, window, buffer)
16828 Lisp_Object format, face, window, buffer;
16830 struct it it;
16831 int len;
16832 struct window *w;
16833 struct buffer *old_buffer = NULL;
16834 int face_id = -1;
16835 int no_props = INTEGERP (face);
16836 int count = SPECPDL_INDEX ();
16837 Lisp_Object str;
16838 int string_start = 0;
16840 if (NILP (window))
16841 window = selected_window;
16842 CHECK_WINDOW (window);
16843 w = XWINDOW (window);
16845 if (NILP (buffer))
16846 buffer = w->buffer;
16847 CHECK_BUFFER (buffer);
16849 if (NILP (format))
16850 return build_string ("");
16852 if (no_props)
16853 face = Qnil;
16855 if (!NILP (face))
16857 if (EQ (face, Qt))
16858 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16859 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16862 if (face_id < 0)
16863 face_id = DEFAULT_FACE_ID;
16865 if (XBUFFER (buffer) != current_buffer)
16866 old_buffer = current_buffer;
16868 /* Save things including mode_line_proptrans_alist,
16869 and set that to nil so that we don't alter the outer value. */
16870 record_unwind_protect (unwind_format_mode_line,
16871 format_mode_line_unwind_data (old_buffer, 1));
16872 mode_line_proptrans_alist = Qnil;
16874 if (old_buffer)
16875 set_buffer_internal_1 (XBUFFER (buffer));
16877 init_iterator (&it, w, -1, -1, NULL, face_id);
16879 if (no_props)
16881 mode_line_target = MODE_LINE_NOPROP;
16882 mode_line_string_face_prop = Qnil;
16883 mode_line_string_list = Qnil;
16884 string_start = MODE_LINE_NOPROP_LEN (0);
16886 else
16888 mode_line_target = MODE_LINE_STRING;
16889 mode_line_string_list = Qnil;
16890 mode_line_string_face = face;
16891 mode_line_string_face_prop
16892 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16895 push_frame_kboard (it.f);
16896 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16897 pop_frame_kboard ();
16899 if (no_props)
16901 len = MODE_LINE_NOPROP_LEN (string_start);
16902 str = make_string (mode_line_noprop_buf + string_start, len);
16904 else
16906 mode_line_string_list = Fnreverse (mode_line_string_list);
16907 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16908 make_string ("", 0));
16911 unbind_to (count, Qnil);
16912 return str;
16915 /* Write a null-terminated, right justified decimal representation of
16916 the positive integer D to BUF using a minimal field width WIDTH. */
16918 static void
16919 pint2str (buf, width, d)
16920 register char *buf;
16921 register int width;
16922 register int d;
16924 register char *p = buf;
16926 if (d <= 0)
16927 *p++ = '0';
16928 else
16930 while (d > 0)
16932 *p++ = d % 10 + '0';
16933 d /= 10;
16937 for (width -= (int) (p - buf); width > 0; --width)
16938 *p++ = ' ';
16939 *p-- = '\0';
16940 while (p > buf)
16942 d = *buf;
16943 *buf++ = *p;
16944 *p-- = d;
16948 /* Write a null-terminated, right justified decimal and "human
16949 readable" representation of the nonnegative integer D to BUF using
16950 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16952 static const char power_letter[] =
16954 0, /* not used */
16955 'k', /* kilo */
16956 'M', /* mega */
16957 'G', /* giga */
16958 'T', /* tera */
16959 'P', /* peta */
16960 'E', /* exa */
16961 'Z', /* zetta */
16962 'Y' /* yotta */
16965 static void
16966 pint2hrstr (buf, width, d)
16967 char *buf;
16968 int width;
16969 int d;
16971 /* We aim to represent the nonnegative integer D as
16972 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16973 int quotient = d;
16974 int remainder = 0;
16975 /* -1 means: do not use TENTHS. */
16976 int tenths = -1;
16977 int exponent = 0;
16979 /* Length of QUOTIENT.TENTHS as a string. */
16980 int length;
16982 char * psuffix;
16983 char * p;
16985 if (1000 <= quotient)
16987 /* Scale to the appropriate EXPONENT. */
16990 remainder = quotient % 1000;
16991 quotient /= 1000;
16992 exponent++;
16994 while (1000 <= quotient);
16996 /* Round to nearest and decide whether to use TENTHS or not. */
16997 if (quotient <= 9)
16999 tenths = remainder / 100;
17000 if (50 <= remainder % 100)
17002 if (tenths < 9)
17003 tenths++;
17004 else
17006 quotient++;
17007 if (quotient == 10)
17008 tenths = -1;
17009 else
17010 tenths = 0;
17014 else
17015 if (500 <= remainder)
17017 if (quotient < 999)
17018 quotient++;
17019 else
17021 quotient = 1;
17022 exponent++;
17023 tenths = 0;
17028 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17029 if (tenths == -1 && quotient <= 99)
17030 if (quotient <= 9)
17031 length = 1;
17032 else
17033 length = 2;
17034 else
17035 length = 3;
17036 p = psuffix = buf + max (width, length);
17038 /* Print EXPONENT. */
17039 if (exponent)
17040 *psuffix++ = power_letter[exponent];
17041 *psuffix = '\0';
17043 /* Print TENTHS. */
17044 if (tenths >= 0)
17046 *--p = '0' + tenths;
17047 *--p = '.';
17050 /* Print QUOTIENT. */
17053 int digit = quotient % 10;
17054 *--p = '0' + digit;
17056 while ((quotient /= 10) != 0);
17058 /* Print leading spaces. */
17059 while (buf < p)
17060 *--p = ' ';
17063 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17064 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17065 type of CODING_SYSTEM. Return updated pointer into BUF. */
17067 static unsigned char invalid_eol_type[] = "(*invalid*)";
17069 static char *
17070 decode_mode_spec_coding (coding_system, buf, eol_flag)
17071 Lisp_Object coding_system;
17072 register char *buf;
17073 int eol_flag;
17075 Lisp_Object val;
17076 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17077 const unsigned char *eol_str;
17078 int eol_str_len;
17079 /* The EOL conversion we are using. */
17080 Lisp_Object eoltype;
17082 val = Fget (coding_system, Qcoding_system);
17083 eoltype = Qnil;
17085 if (!VECTORP (val)) /* Not yet decided. */
17087 if (multibyte)
17088 *buf++ = '-';
17089 if (eol_flag)
17090 eoltype = eol_mnemonic_undecided;
17091 /* Don't mention EOL conversion if it isn't decided. */
17093 else
17095 Lisp_Object eolvalue;
17097 eolvalue = Fget (coding_system, Qeol_type);
17099 if (multibyte)
17100 *buf++ = XFASTINT (AREF (val, 1));
17102 if (eol_flag)
17104 /* The EOL conversion that is normal on this system. */
17106 if (NILP (eolvalue)) /* Not yet decided. */
17107 eoltype = eol_mnemonic_undecided;
17108 else if (VECTORP (eolvalue)) /* Not yet decided. */
17109 eoltype = eol_mnemonic_undecided;
17110 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17111 eoltype = (XFASTINT (eolvalue) == 0
17112 ? eol_mnemonic_unix
17113 : (XFASTINT (eolvalue) == 1
17114 ? eol_mnemonic_dos : eol_mnemonic_mac));
17118 if (eol_flag)
17120 /* Mention the EOL conversion if it is not the usual one. */
17121 if (STRINGP (eoltype))
17123 eol_str = SDATA (eoltype);
17124 eol_str_len = SBYTES (eoltype);
17126 else if (INTEGERP (eoltype)
17127 && CHAR_VALID_P (XINT (eoltype), 0))
17129 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17130 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17131 eol_str = tmp;
17133 else
17135 eol_str = invalid_eol_type;
17136 eol_str_len = sizeof (invalid_eol_type) - 1;
17138 bcopy (eol_str, buf, eol_str_len);
17139 buf += eol_str_len;
17142 return buf;
17145 /* Return a string for the output of a mode line %-spec for window W,
17146 generated by character C. PRECISION >= 0 means don't return a
17147 string longer than that value. FIELD_WIDTH > 0 means pad the
17148 string returned with spaces to that value. Return 1 in *MULTIBYTE
17149 if the result is multibyte text.
17151 Note we operate on the current buffer for most purposes,
17152 the exception being w->base_line_pos. */
17154 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17156 static char *
17157 decode_mode_spec (w, c, field_width, precision, multibyte)
17158 struct window *w;
17159 register int c;
17160 int field_width, precision;
17161 int *multibyte;
17163 Lisp_Object obj;
17164 struct frame *f = XFRAME (WINDOW_FRAME (w));
17165 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17166 struct buffer *b = current_buffer;
17168 obj = Qnil;
17169 *multibyte = 0;
17171 switch (c)
17173 case '*':
17174 if (!NILP (b->read_only))
17175 return "%";
17176 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17177 return "*";
17178 return "-";
17180 case '+':
17181 /* This differs from %* only for a modified read-only buffer. */
17182 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17183 return "*";
17184 if (!NILP (b->read_only))
17185 return "%";
17186 return "-";
17188 case '&':
17189 /* This differs from %* in ignoring read-only-ness. */
17190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17191 return "*";
17192 return "-";
17194 case '%':
17195 return "%";
17197 case '[':
17199 int i;
17200 char *p;
17202 if (command_loop_level > 5)
17203 return "[[[... ";
17204 p = decode_mode_spec_buf;
17205 for (i = 0; i < command_loop_level; i++)
17206 *p++ = '[';
17207 *p = 0;
17208 return decode_mode_spec_buf;
17211 case ']':
17213 int i;
17214 char *p;
17216 if (command_loop_level > 5)
17217 return " ...]]]";
17218 p = decode_mode_spec_buf;
17219 for (i = 0; i < command_loop_level; i++)
17220 *p++ = ']';
17221 *p = 0;
17222 return decode_mode_spec_buf;
17225 case '-':
17227 register int i;
17229 /* Let lots_of_dashes be a string of infinite length. */
17230 if (mode_line_target == MODE_LINE_NOPROP ||
17231 mode_line_target == MODE_LINE_STRING)
17232 return "--";
17233 if (field_width <= 0
17234 || field_width > sizeof (lots_of_dashes))
17236 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17237 decode_mode_spec_buf[i] = '-';
17238 decode_mode_spec_buf[i] = '\0';
17239 return decode_mode_spec_buf;
17241 else
17242 return lots_of_dashes;
17245 case 'b':
17246 obj = b->name;
17247 break;
17249 case 'c':
17251 int col = (int) current_column (); /* iftc */
17252 w->column_number_displayed = make_number (col);
17253 pint2str (decode_mode_spec_buf, field_width, col);
17254 return decode_mode_spec_buf;
17257 case 'e':
17258 #ifndef SYSTEM_MALLOC
17260 if (NILP (Vmemory_full))
17261 return "";
17262 else
17263 return "!MEM FULL! ";
17265 #else
17266 return "";
17267 #endif
17269 case 'F':
17270 /* %F displays the frame name. */
17271 if (!NILP (f->title))
17272 return (char *) SDATA (f->title);
17273 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17274 return (char *) SDATA (f->name);
17275 return "Emacs";
17277 case 'f':
17278 obj = b->filename;
17279 break;
17281 case 'i':
17283 int size = ZV - BEGV;
17284 pint2str (decode_mode_spec_buf, field_width, size);
17285 return decode_mode_spec_buf;
17288 case 'I':
17290 int size = ZV - BEGV;
17291 pint2hrstr (decode_mode_spec_buf, field_width, size);
17292 return decode_mode_spec_buf;
17295 case 'l':
17297 int startpos = XMARKER (w->start)->charpos;
17298 int startpos_byte = marker_byte_position (w->start);
17299 int line, linepos, linepos_byte, topline;
17300 int nlines, junk;
17301 int height = WINDOW_TOTAL_LINES (w);
17303 /* If we decided that this buffer isn't suitable for line numbers,
17304 don't forget that too fast. */
17305 if (EQ (w->base_line_pos, w->buffer))
17306 goto no_value;
17307 /* But do forget it, if the window shows a different buffer now. */
17308 else if (BUFFERP (w->base_line_pos))
17309 w->base_line_pos = Qnil;
17311 /* If the buffer is very big, don't waste time. */
17312 if (INTEGERP (Vline_number_display_limit)
17313 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17315 w->base_line_pos = Qnil;
17316 w->base_line_number = Qnil;
17317 goto no_value;
17320 if (!NILP (w->base_line_number)
17321 && !NILP (w->base_line_pos)
17322 && XFASTINT (w->base_line_pos) <= startpos)
17324 line = XFASTINT (w->base_line_number);
17325 linepos = XFASTINT (w->base_line_pos);
17326 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17328 else
17330 line = 1;
17331 linepos = BUF_BEGV (b);
17332 linepos_byte = BUF_BEGV_BYTE (b);
17335 /* Count lines from base line to window start position. */
17336 nlines = display_count_lines (linepos, linepos_byte,
17337 startpos_byte,
17338 startpos, &junk);
17340 topline = nlines + line;
17342 /* Determine a new base line, if the old one is too close
17343 or too far away, or if we did not have one.
17344 "Too close" means it's plausible a scroll-down would
17345 go back past it. */
17346 if (startpos == BUF_BEGV (b))
17348 w->base_line_number = make_number (topline);
17349 w->base_line_pos = make_number (BUF_BEGV (b));
17351 else if (nlines < height + 25 || nlines > height * 3 + 50
17352 || linepos == BUF_BEGV (b))
17354 int limit = BUF_BEGV (b);
17355 int limit_byte = BUF_BEGV_BYTE (b);
17356 int position;
17357 int distance = (height * 2 + 30) * line_number_display_limit_width;
17359 if (startpos - distance > limit)
17361 limit = startpos - distance;
17362 limit_byte = CHAR_TO_BYTE (limit);
17365 nlines = display_count_lines (startpos, startpos_byte,
17366 limit_byte,
17367 - (height * 2 + 30),
17368 &position);
17369 /* If we couldn't find the lines we wanted within
17370 line_number_display_limit_width chars per line,
17371 give up on line numbers for this window. */
17372 if (position == limit_byte && limit == startpos - distance)
17374 w->base_line_pos = w->buffer;
17375 w->base_line_number = Qnil;
17376 goto no_value;
17379 w->base_line_number = make_number (topline - nlines);
17380 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17383 /* Now count lines from the start pos to point. */
17384 nlines = display_count_lines (startpos, startpos_byte,
17385 PT_BYTE, PT, &junk);
17387 /* Record that we did display the line number. */
17388 line_number_displayed = 1;
17390 /* Make the string to show. */
17391 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17392 return decode_mode_spec_buf;
17393 no_value:
17395 char* p = decode_mode_spec_buf;
17396 int pad = field_width - 2;
17397 while (pad-- > 0)
17398 *p++ = ' ';
17399 *p++ = '?';
17400 *p++ = '?';
17401 *p = '\0';
17402 return decode_mode_spec_buf;
17405 break;
17407 case 'm':
17408 obj = b->mode_name;
17409 break;
17411 case 'n':
17412 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17413 return " Narrow";
17414 break;
17416 case 'p':
17418 int pos = marker_position (w->start);
17419 int total = BUF_ZV (b) - BUF_BEGV (b);
17421 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17423 if (pos <= BUF_BEGV (b))
17424 return "All";
17425 else
17426 return "Bottom";
17428 else if (pos <= BUF_BEGV (b))
17429 return "Top";
17430 else
17432 if (total > 1000000)
17433 /* Do it differently for a large value, to avoid overflow. */
17434 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17435 else
17436 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17437 /* We can't normally display a 3-digit number,
17438 so get us a 2-digit number that is close. */
17439 if (total == 100)
17440 total = 99;
17441 sprintf (decode_mode_spec_buf, "%2d%%", total);
17442 return decode_mode_spec_buf;
17446 /* Display percentage of size above the bottom of the screen. */
17447 case 'P':
17449 int toppos = marker_position (w->start);
17450 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17451 int total = BUF_ZV (b) - BUF_BEGV (b);
17453 if (botpos >= BUF_ZV (b))
17455 if (toppos <= BUF_BEGV (b))
17456 return "All";
17457 else
17458 return "Bottom";
17460 else
17462 if (total > 1000000)
17463 /* Do it differently for a large value, to avoid overflow. */
17464 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17465 else
17466 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17467 /* We can't normally display a 3-digit number,
17468 so get us a 2-digit number that is close. */
17469 if (total == 100)
17470 total = 99;
17471 if (toppos <= BUF_BEGV (b))
17472 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17473 else
17474 sprintf (decode_mode_spec_buf, "%2d%%", total);
17475 return decode_mode_spec_buf;
17479 case 's':
17480 /* status of process */
17481 obj = Fget_buffer_process (Fcurrent_buffer ());
17482 if (NILP (obj))
17483 return "no process";
17484 #ifdef subprocesses
17485 obj = Fsymbol_name (Fprocess_status (obj));
17486 #endif
17487 break;
17489 case 't': /* indicate TEXT or BINARY */
17490 #ifdef MODE_LINE_BINARY_TEXT
17491 return MODE_LINE_BINARY_TEXT (b);
17492 #else
17493 return "T";
17494 #endif
17496 case 'z':
17497 /* coding-system (not including end-of-line format) */
17498 case 'Z':
17499 /* coding-system (including end-of-line type) */
17501 int eol_flag = (c == 'Z');
17502 char *p = decode_mode_spec_buf;
17504 if (! FRAME_WINDOW_P (f))
17506 /* No need to mention EOL here--the terminal never needs
17507 to do EOL conversion. */
17508 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17509 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17511 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17512 p, eol_flag);
17514 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17515 #ifdef subprocesses
17516 obj = Fget_buffer_process (Fcurrent_buffer ());
17517 if (PROCESSP (obj))
17519 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17520 p, eol_flag);
17521 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17522 p, eol_flag);
17524 #endif /* subprocesses */
17525 #endif /* 0 */
17526 *p = 0;
17527 return decode_mode_spec_buf;
17531 if (STRINGP (obj))
17533 *multibyte = STRING_MULTIBYTE (obj);
17534 return (char *) SDATA (obj);
17536 else
17537 return "";
17541 /* Count up to COUNT lines starting from START / START_BYTE.
17542 But don't go beyond LIMIT_BYTE.
17543 Return the number of lines thus found (always nonnegative).
17545 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17547 static int
17548 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17549 int start, start_byte, limit_byte, count;
17550 int *byte_pos_ptr;
17552 register unsigned char *cursor;
17553 unsigned char *base;
17555 register int ceiling;
17556 register unsigned char *ceiling_addr;
17557 int orig_count = count;
17559 /* If we are not in selective display mode,
17560 check only for newlines. */
17561 int selective_display = (!NILP (current_buffer->selective_display)
17562 && !INTEGERP (current_buffer->selective_display));
17564 if (count > 0)
17566 while (start_byte < limit_byte)
17568 ceiling = BUFFER_CEILING_OF (start_byte);
17569 ceiling = min (limit_byte - 1, ceiling);
17570 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17571 base = (cursor = BYTE_POS_ADDR (start_byte));
17572 while (1)
17574 if (selective_display)
17575 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17577 else
17578 while (*cursor != '\n' && ++cursor != ceiling_addr)
17581 if (cursor != ceiling_addr)
17583 if (--count == 0)
17585 start_byte += cursor - base + 1;
17586 *byte_pos_ptr = start_byte;
17587 return orig_count;
17589 else
17590 if (++cursor == ceiling_addr)
17591 break;
17593 else
17594 break;
17596 start_byte += cursor - base;
17599 else
17601 while (start_byte > limit_byte)
17603 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17604 ceiling = max (limit_byte, ceiling);
17605 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17606 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17607 while (1)
17609 if (selective_display)
17610 while (--cursor != ceiling_addr
17611 && *cursor != '\n' && *cursor != 015)
17613 else
17614 while (--cursor != ceiling_addr && *cursor != '\n')
17617 if (cursor != ceiling_addr)
17619 if (++count == 0)
17621 start_byte += cursor - base + 1;
17622 *byte_pos_ptr = start_byte;
17623 /* When scanning backwards, we should
17624 not count the newline posterior to which we stop. */
17625 return - orig_count - 1;
17628 else
17629 break;
17631 /* Here we add 1 to compensate for the last decrement
17632 of CURSOR, which took it past the valid range. */
17633 start_byte += cursor - base + 1;
17637 *byte_pos_ptr = limit_byte;
17639 if (count < 0)
17640 return - orig_count + count;
17641 return orig_count - count;
17647 /***********************************************************************
17648 Displaying strings
17649 ***********************************************************************/
17651 /* Display a NUL-terminated string, starting with index START.
17653 If STRING is non-null, display that C string. Otherwise, the Lisp
17654 string LISP_STRING is displayed.
17656 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17657 FACE_STRING. Display STRING or LISP_STRING with the face at
17658 FACE_STRING_POS in FACE_STRING:
17660 Display the string in the environment given by IT, but use the
17661 standard display table, temporarily.
17663 FIELD_WIDTH is the minimum number of output glyphs to produce.
17664 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17665 with spaces. If STRING has more characters, more than FIELD_WIDTH
17666 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17668 PRECISION is the maximum number of characters to output from
17669 STRING. PRECISION < 0 means don't truncate the string.
17671 This is roughly equivalent to printf format specifiers:
17673 FIELD_WIDTH PRECISION PRINTF
17674 ----------------------------------------
17675 -1 -1 %s
17676 -1 10 %.10s
17677 10 -1 %10s
17678 20 10 %20.10s
17680 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17681 display them, and < 0 means obey the current buffer's value of
17682 enable_multibyte_characters.
17684 Value is the number of glyphs produced. */
17686 static int
17687 display_string (string, lisp_string, face_string, face_string_pos,
17688 start, it, field_width, precision, max_x, multibyte)
17689 unsigned char *string;
17690 Lisp_Object lisp_string;
17691 Lisp_Object face_string;
17692 int face_string_pos;
17693 int start;
17694 struct it *it;
17695 int field_width, precision, max_x;
17696 int multibyte;
17698 int hpos_at_start = it->hpos;
17699 int saved_face_id = it->face_id;
17700 struct glyph_row *row = it->glyph_row;
17702 /* Initialize the iterator IT for iteration over STRING beginning
17703 with index START. */
17704 reseat_to_string (it, string, lisp_string, start,
17705 precision, field_width, multibyte);
17707 /* If displaying STRING, set up the face of the iterator
17708 from LISP_STRING, if that's given. */
17709 if (STRINGP (face_string))
17711 int endptr;
17712 struct face *face;
17714 it->face_id
17715 = face_at_string_position (it->w, face_string, face_string_pos,
17716 0, it->region_beg_charpos,
17717 it->region_end_charpos,
17718 &endptr, it->base_face_id, 0);
17719 face = FACE_FROM_ID (it->f, it->face_id);
17720 it->face_box_p = face->box != FACE_NO_BOX;
17723 /* Set max_x to the maximum allowed X position. Don't let it go
17724 beyond the right edge of the window. */
17725 if (max_x <= 0)
17726 max_x = it->last_visible_x;
17727 else
17728 max_x = min (max_x, it->last_visible_x);
17730 /* Skip over display elements that are not visible. because IT->w is
17731 hscrolled. */
17732 if (it->current_x < it->first_visible_x)
17733 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17734 MOVE_TO_POS | MOVE_TO_X);
17736 row->ascent = it->max_ascent;
17737 row->height = it->max_ascent + it->max_descent;
17738 row->phys_ascent = it->max_phys_ascent;
17739 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17740 row->extra_line_spacing = it->max_extra_line_spacing;
17742 /* This condition is for the case that we are called with current_x
17743 past last_visible_x. */
17744 while (it->current_x < max_x)
17746 int x_before, x, n_glyphs_before, i, nglyphs;
17748 /* Get the next display element. */
17749 if (!get_next_display_element (it))
17750 break;
17752 /* Produce glyphs. */
17753 x_before = it->current_x;
17754 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17755 PRODUCE_GLYPHS (it);
17757 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17758 i = 0;
17759 x = x_before;
17760 while (i < nglyphs)
17762 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17764 if (!it->truncate_lines_p
17765 && x + glyph->pixel_width > max_x)
17767 /* End of continued line or max_x reached. */
17768 if (CHAR_GLYPH_PADDING_P (*glyph))
17770 /* A wide character is unbreakable. */
17771 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17772 it->current_x = x_before;
17774 else
17776 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17777 it->current_x = x;
17779 break;
17781 else if (x + glyph->pixel_width > it->first_visible_x)
17783 /* Glyph is at least partially visible. */
17784 ++it->hpos;
17785 if (x < it->first_visible_x)
17786 it->glyph_row->x = x - it->first_visible_x;
17788 else
17790 /* Glyph is off the left margin of the display area.
17791 Should not happen. */
17792 abort ();
17795 row->ascent = max (row->ascent, it->max_ascent);
17796 row->height = max (row->height, it->max_ascent + it->max_descent);
17797 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17798 row->phys_height = max (row->phys_height,
17799 it->max_phys_ascent + it->max_phys_descent);
17800 row->extra_line_spacing = max (row->extra_line_spacing,
17801 it->max_extra_line_spacing);
17802 x += glyph->pixel_width;
17803 ++i;
17806 /* Stop if max_x reached. */
17807 if (i < nglyphs)
17808 break;
17810 /* Stop at line ends. */
17811 if (ITERATOR_AT_END_OF_LINE_P (it))
17813 it->continuation_lines_width = 0;
17814 break;
17817 set_iterator_to_next (it, 1);
17819 /* Stop if truncating at the right edge. */
17820 if (it->truncate_lines_p
17821 && it->current_x >= it->last_visible_x)
17823 /* Add truncation mark, but don't do it if the line is
17824 truncated at a padding space. */
17825 if (IT_CHARPOS (*it) < it->string_nchars)
17827 if (!FRAME_WINDOW_P (it->f))
17829 int i, n;
17831 if (it->current_x > it->last_visible_x)
17833 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17834 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17835 break;
17836 for (n = row->used[TEXT_AREA]; i < n; ++i)
17838 row->used[TEXT_AREA] = i;
17839 produce_special_glyphs (it, IT_TRUNCATION);
17842 produce_special_glyphs (it, IT_TRUNCATION);
17844 it->glyph_row->truncated_on_right_p = 1;
17846 break;
17850 /* Maybe insert a truncation at the left. */
17851 if (it->first_visible_x
17852 && IT_CHARPOS (*it) > 0)
17854 if (!FRAME_WINDOW_P (it->f))
17855 insert_left_trunc_glyphs (it);
17856 it->glyph_row->truncated_on_left_p = 1;
17859 it->face_id = saved_face_id;
17861 /* Value is number of columns displayed. */
17862 return it->hpos - hpos_at_start;
17867 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17868 appears as an element of LIST or as the car of an element of LIST.
17869 If PROPVAL is a list, compare each element against LIST in that
17870 way, and return 1/2 if any element of PROPVAL is found in LIST.
17871 Otherwise return 0. This function cannot quit.
17872 The return value is 2 if the text is invisible but with an ellipsis
17873 and 1 if it's invisible and without an ellipsis. */
17876 invisible_p (propval, list)
17877 register Lisp_Object propval;
17878 Lisp_Object list;
17880 register Lisp_Object tail, proptail;
17882 for (tail = list; CONSP (tail); tail = XCDR (tail))
17884 register Lisp_Object tem;
17885 tem = XCAR (tail);
17886 if (EQ (propval, tem))
17887 return 1;
17888 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17889 return NILP (XCDR (tem)) ? 1 : 2;
17892 if (CONSP (propval))
17894 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17896 Lisp_Object propelt;
17897 propelt = XCAR (proptail);
17898 for (tail = list; CONSP (tail); tail = XCDR (tail))
17900 register Lisp_Object tem;
17901 tem = XCAR (tail);
17902 if (EQ (propelt, tem))
17903 return 1;
17904 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17905 return NILP (XCDR (tem)) ? 1 : 2;
17910 return 0;
17913 /* Calculate a width or height in pixels from a specification using
17914 the following elements:
17916 SPEC ::=
17917 NUM - a (fractional) multiple of the default font width/height
17918 (NUM) - specifies exactly NUM pixels
17919 UNIT - a fixed number of pixels, see below.
17920 ELEMENT - size of a display element in pixels, see below.
17921 (NUM . SPEC) - equals NUM * SPEC
17922 (+ SPEC SPEC ...) - add pixel values
17923 (- SPEC SPEC ...) - subtract pixel values
17924 (- SPEC) - negate pixel value
17926 NUM ::=
17927 INT or FLOAT - a number constant
17928 SYMBOL - use symbol's (buffer local) variable binding.
17930 UNIT ::=
17931 in - pixels per inch *)
17932 mm - pixels per 1/1000 meter *)
17933 cm - pixels per 1/100 meter *)
17934 width - width of current font in pixels.
17935 height - height of current font in pixels.
17937 *) using the ratio(s) defined in display-pixels-per-inch.
17939 ELEMENT ::=
17941 left-fringe - left fringe width in pixels
17942 right-fringe - right fringe width in pixels
17944 left-margin - left margin width in pixels
17945 right-margin - right margin width in pixels
17947 scroll-bar - scroll-bar area width in pixels
17949 Examples:
17951 Pixels corresponding to 5 inches:
17952 (5 . in)
17954 Total width of non-text areas on left side of window (if scroll-bar is on left):
17955 '(space :width (+ left-fringe left-margin scroll-bar))
17957 Align to first text column (in header line):
17958 '(space :align-to 0)
17960 Align to middle of text area minus half the width of variable `my-image'
17961 containing a loaded image:
17962 '(space :align-to (0.5 . (- text my-image)))
17964 Width of left margin minus width of 1 character in the default font:
17965 '(space :width (- left-margin 1))
17967 Width of left margin minus width of 2 characters in the current font:
17968 '(space :width (- left-margin (2 . width)))
17970 Center 1 character over left-margin (in header line):
17971 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17973 Different ways to express width of left fringe plus left margin minus one pixel:
17974 '(space :width (- (+ left-fringe left-margin) (1)))
17975 '(space :width (+ left-fringe left-margin (- (1))))
17976 '(space :width (+ left-fringe left-margin (-1)))
17980 #define NUMVAL(X) \
17981 ((INTEGERP (X) || FLOATP (X)) \
17982 ? XFLOATINT (X) \
17983 : - 1)
17986 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17987 double *res;
17988 struct it *it;
17989 Lisp_Object prop;
17990 void *font;
17991 int width_p, *align_to;
17993 double pixels;
17995 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17996 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17998 if (NILP (prop))
17999 return OK_PIXELS (0);
18001 if (SYMBOLP (prop))
18003 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18005 char *unit = SDATA (SYMBOL_NAME (prop));
18007 if (unit[0] == 'i' && unit[1] == 'n')
18008 pixels = 1.0;
18009 else if (unit[0] == 'm' && unit[1] == 'm')
18010 pixels = 25.4;
18011 else if (unit[0] == 'c' && unit[1] == 'm')
18012 pixels = 2.54;
18013 else
18014 pixels = 0;
18015 if (pixels > 0)
18017 double ppi;
18018 #ifdef HAVE_WINDOW_SYSTEM
18019 if (FRAME_WINDOW_P (it->f)
18020 && (ppi = (width_p
18021 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18022 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18023 ppi > 0))
18024 return OK_PIXELS (ppi / pixels);
18025 #endif
18027 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18028 || (CONSP (Vdisplay_pixels_per_inch)
18029 && (ppi = (width_p
18030 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18031 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18032 ppi > 0)))
18033 return OK_PIXELS (ppi / pixels);
18035 return 0;
18039 #ifdef HAVE_WINDOW_SYSTEM
18040 if (EQ (prop, Qheight))
18041 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18042 if (EQ (prop, Qwidth))
18043 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18044 #else
18045 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18046 return OK_PIXELS (1);
18047 #endif
18049 if (EQ (prop, Qtext))
18050 return OK_PIXELS (width_p
18051 ? window_box_width (it->w, TEXT_AREA)
18052 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18054 if (align_to && *align_to < 0)
18056 *res = 0;
18057 if (EQ (prop, Qleft))
18058 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18059 if (EQ (prop, Qright))
18060 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18061 if (EQ (prop, Qcenter))
18062 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18063 + window_box_width (it->w, TEXT_AREA) / 2);
18064 if (EQ (prop, Qleft_fringe))
18065 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18066 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18067 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18068 if (EQ (prop, Qright_fringe))
18069 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18070 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18071 : window_box_right_offset (it->w, TEXT_AREA));
18072 if (EQ (prop, Qleft_margin))
18073 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18074 if (EQ (prop, Qright_margin))
18075 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18076 if (EQ (prop, Qscroll_bar))
18077 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18079 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18080 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18081 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18082 : 0)));
18084 else
18086 if (EQ (prop, Qleft_fringe))
18087 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18088 if (EQ (prop, Qright_fringe))
18089 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18090 if (EQ (prop, Qleft_margin))
18091 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18092 if (EQ (prop, Qright_margin))
18093 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18094 if (EQ (prop, Qscroll_bar))
18095 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18098 prop = Fbuffer_local_value (prop, it->w->buffer);
18101 if (INTEGERP (prop) || FLOATP (prop))
18103 int base_unit = (width_p
18104 ? FRAME_COLUMN_WIDTH (it->f)
18105 : FRAME_LINE_HEIGHT (it->f));
18106 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18109 if (CONSP (prop))
18111 Lisp_Object car = XCAR (prop);
18112 Lisp_Object cdr = XCDR (prop);
18114 if (SYMBOLP (car))
18116 #ifdef HAVE_WINDOW_SYSTEM
18117 if (valid_image_p (prop))
18119 int id = lookup_image (it->f, prop);
18120 struct image *img = IMAGE_FROM_ID (it->f, id);
18122 return OK_PIXELS (width_p ? img->width : img->height);
18124 #endif
18125 if (EQ (car, Qplus) || EQ (car, Qminus))
18127 int first = 1;
18128 double px;
18130 pixels = 0;
18131 while (CONSP (cdr))
18133 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18134 font, width_p, align_to))
18135 return 0;
18136 if (first)
18137 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18138 else
18139 pixels += px;
18140 cdr = XCDR (cdr);
18142 if (EQ (car, Qminus))
18143 pixels = -pixels;
18144 return OK_PIXELS (pixels);
18147 car = Fbuffer_local_value (car, it->w->buffer);
18150 if (INTEGERP (car) || FLOATP (car))
18152 double fact;
18153 pixels = XFLOATINT (car);
18154 if (NILP (cdr))
18155 return OK_PIXELS (pixels);
18156 if (calc_pixel_width_or_height (&fact, it, cdr,
18157 font, width_p, align_to))
18158 return OK_PIXELS (pixels * fact);
18159 return 0;
18162 return 0;
18165 return 0;
18169 /***********************************************************************
18170 Glyph Display
18171 ***********************************************************************/
18173 #ifdef HAVE_WINDOW_SYSTEM
18175 #if GLYPH_DEBUG
18177 void
18178 dump_glyph_string (s)
18179 struct glyph_string *s;
18181 fprintf (stderr, "glyph string\n");
18182 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18183 s->x, s->y, s->width, s->height);
18184 fprintf (stderr, " ybase = %d\n", s->ybase);
18185 fprintf (stderr, " hl = %d\n", s->hl);
18186 fprintf (stderr, " left overhang = %d, right = %d\n",
18187 s->left_overhang, s->right_overhang);
18188 fprintf (stderr, " nchars = %d\n", s->nchars);
18189 fprintf (stderr, " extends to end of line = %d\n",
18190 s->extends_to_end_of_line_p);
18191 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18192 fprintf (stderr, " bg width = %d\n", s->background_width);
18195 #endif /* GLYPH_DEBUG */
18197 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18198 of XChar2b structures for S; it can't be allocated in
18199 init_glyph_string because it must be allocated via `alloca'. W
18200 is the window on which S is drawn. ROW and AREA are the glyph row
18201 and area within the row from which S is constructed. START is the
18202 index of the first glyph structure covered by S. HL is a
18203 face-override for drawing S. */
18205 #ifdef HAVE_NTGUI
18206 #define OPTIONAL_HDC(hdc) hdc,
18207 #define DECLARE_HDC(hdc) HDC hdc;
18208 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18209 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18210 #endif
18212 #ifndef OPTIONAL_HDC
18213 #define OPTIONAL_HDC(hdc)
18214 #define DECLARE_HDC(hdc)
18215 #define ALLOCATE_HDC(hdc, f)
18216 #define RELEASE_HDC(hdc, f)
18217 #endif
18219 static void
18220 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18221 struct glyph_string *s;
18222 DECLARE_HDC (hdc)
18223 XChar2b *char2b;
18224 struct window *w;
18225 struct glyph_row *row;
18226 enum glyph_row_area area;
18227 int start;
18228 enum draw_glyphs_face hl;
18230 bzero (s, sizeof *s);
18231 s->w = w;
18232 s->f = XFRAME (w->frame);
18233 #ifdef HAVE_NTGUI
18234 s->hdc = hdc;
18235 #endif
18236 s->display = FRAME_X_DISPLAY (s->f);
18237 s->window = FRAME_X_WINDOW (s->f);
18238 s->char2b = char2b;
18239 s->hl = hl;
18240 s->row = row;
18241 s->area = area;
18242 s->first_glyph = row->glyphs[area] + start;
18243 s->height = row->height;
18244 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18246 /* Display the internal border below the tool-bar window. */
18247 if (WINDOWP (s->f->tool_bar_window)
18248 && s->w == XWINDOW (s->f->tool_bar_window))
18249 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18251 s->ybase = s->y + row->ascent;
18255 /* Append the list of glyph strings with head H and tail T to the list
18256 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18258 static INLINE void
18259 append_glyph_string_lists (head, tail, h, t)
18260 struct glyph_string **head, **tail;
18261 struct glyph_string *h, *t;
18263 if (h)
18265 if (*head)
18266 (*tail)->next = h;
18267 else
18268 *head = h;
18269 h->prev = *tail;
18270 *tail = t;
18275 /* Prepend the list of glyph strings with head H and tail T to the
18276 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18277 result. */
18279 static INLINE void
18280 prepend_glyph_string_lists (head, tail, h, t)
18281 struct glyph_string **head, **tail;
18282 struct glyph_string *h, *t;
18284 if (h)
18286 if (*head)
18287 (*head)->prev = t;
18288 else
18289 *tail = t;
18290 t->next = *head;
18291 *head = h;
18296 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18297 Set *HEAD and *TAIL to the resulting list. */
18299 static INLINE void
18300 append_glyph_string (head, tail, s)
18301 struct glyph_string **head, **tail;
18302 struct glyph_string *s;
18304 s->next = s->prev = NULL;
18305 append_glyph_string_lists (head, tail, s, s);
18309 /* Get face and two-byte form of character glyph GLYPH on frame F.
18310 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18311 a pointer to a realized face that is ready for display. */
18313 static INLINE struct face *
18314 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18315 struct frame *f;
18316 struct glyph *glyph;
18317 XChar2b *char2b;
18318 int *two_byte_p;
18320 struct face *face;
18322 xassert (glyph->type == CHAR_GLYPH);
18323 face = FACE_FROM_ID (f, glyph->face_id);
18325 if (two_byte_p)
18326 *two_byte_p = 0;
18328 if (!glyph->multibyte_p)
18330 /* Unibyte case. We don't have to encode, but we have to make
18331 sure to use a face suitable for unibyte. */
18332 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18334 else if (glyph->u.ch < 128
18335 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18337 /* Case of ASCII in a face known to fit ASCII. */
18338 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18340 else
18342 int c1, c2, charset;
18344 /* Split characters into bytes. If c2 is -1 afterwards, C is
18345 really a one-byte character so that byte1 is zero. */
18346 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18347 if (c2 > 0)
18348 STORE_XCHAR2B (char2b, c1, c2);
18349 else
18350 STORE_XCHAR2B (char2b, 0, c1);
18352 /* Maybe encode the character in *CHAR2B. */
18353 if (charset != CHARSET_ASCII)
18355 struct font_info *font_info
18356 = FONT_INFO_FROM_ID (f, face->font_info_id);
18357 if (font_info)
18358 glyph->font_type
18359 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18363 /* Make sure X resources of the face are allocated. */
18364 xassert (face != NULL);
18365 PREPARE_FACE_FOR_DISPLAY (f, face);
18366 return face;
18370 /* Fill glyph string S with composition components specified by S->cmp.
18372 FACES is an array of faces for all components of this composition.
18373 S->gidx is the index of the first component for S.
18375 OVERLAPS non-zero means S should draw the foreground only, and use
18376 its physical height for clipping. See also draw_glyphs.
18378 Value is the index of a component not in S. */
18380 static int
18381 fill_composite_glyph_string (s, faces, overlaps)
18382 struct glyph_string *s;
18383 struct face **faces;
18384 int overlaps;
18386 int i;
18388 xassert (s);
18390 s->for_overlaps = overlaps;
18392 s->face = faces[s->gidx];
18393 s->font = s->face->font;
18394 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18396 /* For all glyphs of this composition, starting at the offset
18397 S->gidx, until we reach the end of the definition or encounter a
18398 glyph that requires the different face, add it to S. */
18399 ++s->nchars;
18400 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18401 ++s->nchars;
18403 /* All glyph strings for the same composition has the same width,
18404 i.e. the width set for the first component of the composition. */
18406 s->width = s->first_glyph->pixel_width;
18408 /* If the specified font could not be loaded, use the frame's
18409 default font, but record the fact that we couldn't load it in
18410 the glyph string so that we can draw rectangles for the
18411 characters of the glyph string. */
18412 if (s->font == NULL)
18414 s->font_not_found_p = 1;
18415 s->font = FRAME_FONT (s->f);
18418 /* Adjust base line for subscript/superscript text. */
18419 s->ybase += s->first_glyph->voffset;
18421 xassert (s->face && s->face->gc);
18423 /* This glyph string must always be drawn with 16-bit functions. */
18424 s->two_byte_p = 1;
18426 return s->gidx + s->nchars;
18430 /* Fill glyph string S from a sequence of character glyphs.
18432 FACE_ID is the face id of the string. START is the index of the
18433 first glyph to consider, END is the index of the last + 1.
18434 OVERLAPS non-zero means S should draw the foreground only, and use
18435 its physical height for clipping. See also draw_glyphs.
18437 Value is the index of the first glyph not in S. */
18439 static int
18440 fill_glyph_string (s, face_id, start, end, overlaps)
18441 struct glyph_string *s;
18442 int face_id;
18443 int start, end, overlaps;
18445 struct glyph *glyph, *last;
18446 int voffset;
18447 int glyph_not_available_p;
18449 xassert (s->f == XFRAME (s->w->frame));
18450 xassert (s->nchars == 0);
18451 xassert (start >= 0 && end > start);
18453 s->for_overlaps = overlaps,
18454 glyph = s->row->glyphs[s->area] + start;
18455 last = s->row->glyphs[s->area] + end;
18456 voffset = glyph->voffset;
18458 glyph_not_available_p = glyph->glyph_not_available_p;
18460 while (glyph < last
18461 && glyph->type == CHAR_GLYPH
18462 && glyph->voffset == voffset
18463 /* Same face id implies same font, nowadays. */
18464 && glyph->face_id == face_id
18465 && glyph->glyph_not_available_p == glyph_not_available_p)
18467 int two_byte_p;
18469 s->face = get_glyph_face_and_encoding (s->f, glyph,
18470 s->char2b + s->nchars,
18471 &two_byte_p);
18472 s->two_byte_p = two_byte_p;
18473 ++s->nchars;
18474 xassert (s->nchars <= end - start);
18475 s->width += glyph->pixel_width;
18476 ++glyph;
18479 s->font = s->face->font;
18480 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18482 /* If the specified font could not be loaded, use the frame's font,
18483 but record the fact that we couldn't load it in
18484 S->font_not_found_p so that we can draw rectangles for the
18485 characters of the glyph string. */
18486 if (s->font == NULL || glyph_not_available_p)
18488 s->font_not_found_p = 1;
18489 s->font = FRAME_FONT (s->f);
18492 /* Adjust base line for subscript/superscript text. */
18493 s->ybase += voffset;
18495 xassert (s->face && s->face->gc);
18496 return glyph - s->row->glyphs[s->area];
18500 /* Fill glyph string S from image glyph S->first_glyph. */
18502 static void
18503 fill_image_glyph_string (s)
18504 struct glyph_string *s;
18506 xassert (s->first_glyph->type == IMAGE_GLYPH);
18507 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18508 xassert (s->img);
18509 s->slice = s->first_glyph->slice;
18510 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18511 s->font = s->face->font;
18512 s->width = s->first_glyph->pixel_width;
18514 /* Adjust base line for subscript/superscript text. */
18515 s->ybase += s->first_glyph->voffset;
18519 /* Fill glyph string S from a sequence of stretch glyphs.
18521 ROW is the glyph row in which the glyphs are found, AREA is the
18522 area within the row. START is the index of the first glyph to
18523 consider, END is the index of the last + 1.
18525 Value is the index of the first glyph not in S. */
18527 static int
18528 fill_stretch_glyph_string (s, row, area, start, end)
18529 struct glyph_string *s;
18530 struct glyph_row *row;
18531 enum glyph_row_area area;
18532 int start, end;
18534 struct glyph *glyph, *last;
18535 int voffset, face_id;
18537 xassert (s->first_glyph->type == STRETCH_GLYPH);
18539 glyph = s->row->glyphs[s->area] + start;
18540 last = s->row->glyphs[s->area] + end;
18541 face_id = glyph->face_id;
18542 s->face = FACE_FROM_ID (s->f, face_id);
18543 s->font = s->face->font;
18544 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18545 s->width = glyph->pixel_width;
18546 voffset = glyph->voffset;
18548 for (++glyph;
18549 (glyph < last
18550 && glyph->type == STRETCH_GLYPH
18551 && glyph->voffset == voffset
18552 && glyph->face_id == face_id);
18553 ++glyph)
18554 s->width += glyph->pixel_width;
18556 /* Adjust base line for subscript/superscript text. */
18557 s->ybase += voffset;
18559 /* The case that face->gc == 0 is handled when drawing the glyph
18560 string by calling PREPARE_FACE_FOR_DISPLAY. */
18561 xassert (s->face);
18562 return glyph - s->row->glyphs[s->area];
18566 /* EXPORT for RIF:
18567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18569 assumed to be zero. */
18571 void
18572 x_get_glyph_overhangs (glyph, f, left, right)
18573 struct glyph *glyph;
18574 struct frame *f;
18575 int *left, *right;
18577 *left = *right = 0;
18579 if (glyph->type == CHAR_GLYPH)
18581 XFontStruct *font;
18582 struct face *face;
18583 struct font_info *font_info;
18584 XChar2b char2b;
18585 XCharStruct *pcm;
18587 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18588 font = face->font;
18589 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18590 if (font /* ++KFS: Should this be font_info ? */
18591 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18593 if (pcm->rbearing > pcm->width)
18594 *right = pcm->rbearing - pcm->width;
18595 if (pcm->lbearing < 0)
18596 *left = -pcm->lbearing;
18602 /* Return the index of the first glyph preceding glyph string S that
18603 is overwritten by S because of S's left overhang. Value is -1
18604 if no glyphs are overwritten. */
18606 static int
18607 left_overwritten (s)
18608 struct glyph_string *s;
18610 int k;
18612 if (s->left_overhang)
18614 int x = 0, i;
18615 struct glyph *glyphs = s->row->glyphs[s->area];
18616 int first = s->first_glyph - glyphs;
18618 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18619 x -= glyphs[i].pixel_width;
18621 k = i + 1;
18623 else
18624 k = -1;
18626 return k;
18630 /* Return the index of the first glyph preceding glyph string S that
18631 is overwriting S because of its right overhang. Value is -1 if no
18632 glyph in front of S overwrites S. */
18634 static int
18635 left_overwriting (s)
18636 struct glyph_string *s;
18638 int i, k, x;
18639 struct glyph *glyphs = s->row->glyphs[s->area];
18640 int first = s->first_glyph - glyphs;
18642 k = -1;
18643 x = 0;
18644 for (i = first - 1; i >= 0; --i)
18646 int left, right;
18647 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18648 if (x + right > 0)
18649 k = i;
18650 x -= glyphs[i].pixel_width;
18653 return k;
18657 /* Return the index of the last glyph following glyph string S that is
18658 not overwritten by S because of S's right overhang. Value is -1 if
18659 no such glyph is found. */
18661 static int
18662 right_overwritten (s)
18663 struct glyph_string *s;
18665 int k = -1;
18667 if (s->right_overhang)
18669 int x = 0, i;
18670 struct glyph *glyphs = s->row->glyphs[s->area];
18671 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18672 int end = s->row->used[s->area];
18674 for (i = first; i < end && s->right_overhang > x; ++i)
18675 x += glyphs[i].pixel_width;
18677 k = i;
18680 return k;
18684 /* Return the index of the last glyph following glyph string S that
18685 overwrites S because of its left overhang. Value is negative
18686 if no such glyph is found. */
18688 static int
18689 right_overwriting (s)
18690 struct glyph_string *s;
18692 int i, k, x;
18693 int end = s->row->used[s->area];
18694 struct glyph *glyphs = s->row->glyphs[s->area];
18695 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18697 k = -1;
18698 x = 0;
18699 for (i = first; i < end; ++i)
18701 int left, right;
18702 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18703 if (x - left < 0)
18704 k = i;
18705 x += glyphs[i].pixel_width;
18708 return k;
18712 /* Get face and two-byte form of character C in face FACE_ID on frame
18713 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18714 means we want to display multibyte text. DISPLAY_P non-zero means
18715 make sure that X resources for the face returned are allocated.
18716 Value is a pointer to a realized face that is ready for display if
18717 DISPLAY_P is non-zero. */
18719 static INLINE struct face *
18720 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18721 struct frame *f;
18722 int c, face_id;
18723 XChar2b *char2b;
18724 int multibyte_p, display_p;
18726 struct face *face = FACE_FROM_ID (f, face_id);
18728 if (!multibyte_p)
18730 /* Unibyte case. We don't have to encode, but we have to make
18731 sure to use a face suitable for unibyte. */
18732 STORE_XCHAR2B (char2b, 0, c);
18733 face_id = FACE_FOR_CHAR (f, face, c);
18734 face = FACE_FROM_ID (f, face_id);
18736 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18738 /* Case of ASCII in a face known to fit ASCII. */
18739 STORE_XCHAR2B (char2b, 0, c);
18741 else
18743 int c1, c2, charset;
18745 /* Split characters into bytes. If c2 is -1 afterwards, C is
18746 really a one-byte character so that byte1 is zero. */
18747 SPLIT_CHAR (c, charset, c1, c2);
18748 if (c2 > 0)
18749 STORE_XCHAR2B (char2b, c1, c2);
18750 else
18751 STORE_XCHAR2B (char2b, 0, c1);
18753 /* Maybe encode the character in *CHAR2B. */
18754 if (face->font != NULL)
18756 struct font_info *font_info
18757 = FONT_INFO_FROM_ID (f, face->font_info_id);
18758 if (font_info)
18759 rif->encode_char (c, char2b, font_info, 0);
18763 /* Make sure X resources of the face are allocated. */
18764 #ifdef HAVE_X_WINDOWS
18765 if (display_p)
18766 #endif
18768 xassert (face != NULL);
18769 PREPARE_FACE_FOR_DISPLAY (f, face);
18772 return face;
18776 /* Set background width of glyph string S. START is the index of the
18777 first glyph following S. LAST_X is the right-most x-position + 1
18778 in the drawing area. */
18780 static INLINE void
18781 set_glyph_string_background_width (s, start, last_x)
18782 struct glyph_string *s;
18783 int start;
18784 int last_x;
18786 /* If the face of this glyph string has to be drawn to the end of
18787 the drawing area, set S->extends_to_end_of_line_p. */
18789 if (start == s->row->used[s->area]
18790 && s->area == TEXT_AREA
18791 && ((s->row->fill_line_p
18792 && (s->hl == DRAW_NORMAL_TEXT
18793 || s->hl == DRAW_IMAGE_RAISED
18794 || s->hl == DRAW_IMAGE_SUNKEN))
18795 || s->hl == DRAW_MOUSE_FACE))
18796 s->extends_to_end_of_line_p = 1;
18798 /* If S extends its face to the end of the line, set its
18799 background_width to the distance to the right edge of the drawing
18800 area. */
18801 if (s->extends_to_end_of_line_p)
18802 s->background_width = last_x - s->x + 1;
18803 else
18804 s->background_width = s->width;
18808 /* Compute overhangs and x-positions for glyph string S and its
18809 predecessors, or successors. X is the starting x-position for S.
18810 BACKWARD_P non-zero means process predecessors. */
18812 static void
18813 compute_overhangs_and_x (s, x, backward_p)
18814 struct glyph_string *s;
18815 int x;
18816 int backward_p;
18818 if (backward_p)
18820 while (s)
18822 if (rif->compute_glyph_string_overhangs)
18823 rif->compute_glyph_string_overhangs (s);
18824 x -= s->width;
18825 s->x = x;
18826 s = s->prev;
18829 else
18831 while (s)
18833 if (rif->compute_glyph_string_overhangs)
18834 rif->compute_glyph_string_overhangs (s);
18835 s->x = x;
18836 x += s->width;
18837 s = s->next;
18844 /* The following macros are only called from draw_glyphs below.
18845 They reference the following parameters of that function directly:
18846 `w', `row', `area', and `overlap_p'
18847 as well as the following local variables:
18848 `s', `f', and `hdc' (in W32) */
18850 #ifdef HAVE_NTGUI
18851 /* On W32, silently add local `hdc' variable to argument list of
18852 init_glyph_string. */
18853 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18854 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18855 #else
18856 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18857 init_glyph_string (s, char2b, w, row, area, start, hl)
18858 #endif
18860 /* Add a glyph string for a stretch glyph to the list of strings
18861 between HEAD and TAIL. START is the index of the stretch glyph in
18862 row area AREA of glyph row ROW. END is the index of the last glyph
18863 in that glyph row area. X is the current output position assigned
18864 to the new glyph string constructed. HL overrides that face of the
18865 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18866 is the right-most x-position of the drawing area. */
18868 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18869 and below -- keep them on one line. */
18870 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18871 do \
18873 s = (struct glyph_string *) alloca (sizeof *s); \
18874 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18875 START = fill_stretch_glyph_string (s, row, area, START, END); \
18876 append_glyph_string (&HEAD, &TAIL, s); \
18877 s->x = (X); \
18879 while (0)
18882 /* Add a glyph string for an image glyph to the list of strings
18883 between HEAD and TAIL. START is the index of the image glyph in
18884 row area AREA of glyph row ROW. END is the index of the last glyph
18885 in that glyph row area. X is the current output position assigned
18886 to the new glyph string constructed. HL overrides that face of the
18887 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18888 is the right-most x-position of the drawing area. */
18890 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18891 do \
18893 s = (struct glyph_string *) alloca (sizeof *s); \
18894 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18895 fill_image_glyph_string (s); \
18896 append_glyph_string (&HEAD, &TAIL, s); \
18897 ++START; \
18898 s->x = (X); \
18900 while (0)
18903 /* Add a glyph string for a sequence of character glyphs to the list
18904 of strings between HEAD and TAIL. START is the index of the first
18905 glyph in row area AREA of glyph row ROW that is part of the new
18906 glyph string. END is the index of the last glyph in that glyph row
18907 area. X is the current output position assigned to the new glyph
18908 string constructed. HL overrides that face of the glyph; e.g. it
18909 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18910 right-most x-position of the drawing area. */
18912 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18913 do \
18915 int c, face_id; \
18916 XChar2b *char2b; \
18918 c = (row)->glyphs[area][START].u.ch; \
18919 face_id = (row)->glyphs[area][START].face_id; \
18921 s = (struct glyph_string *) alloca (sizeof *s); \
18922 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18923 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18924 append_glyph_string (&HEAD, &TAIL, s); \
18925 s->x = (X); \
18926 START = fill_glyph_string (s, face_id, START, END, overlaps); \
18928 while (0)
18931 /* Add a glyph string for a composite sequence to the list of strings
18932 between HEAD and TAIL. START is the index of the first glyph in
18933 row area AREA of glyph row ROW that is part of the new glyph
18934 string. END is the index of the last glyph in that glyph row area.
18935 X is the current output position assigned to the new glyph string
18936 constructed. HL overrides that face of the glyph; e.g. it is
18937 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18938 x-position of the drawing area. */
18940 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18941 do { \
18942 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18943 int face_id = (row)->glyphs[area][START].face_id; \
18944 struct face *base_face = FACE_FROM_ID (f, face_id); \
18945 struct composition *cmp = composition_table[cmp_id]; \
18946 int glyph_len = cmp->glyph_len; \
18947 XChar2b *char2b; \
18948 struct face **faces; \
18949 struct glyph_string *first_s = NULL; \
18950 int n; \
18952 base_face = base_face->ascii_face; \
18953 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18954 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18955 /* At first, fill in `char2b' and `faces'. */ \
18956 for (n = 0; n < glyph_len; n++) \
18958 int c = COMPOSITION_GLYPH (cmp, n); \
18959 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18960 faces[n] = FACE_FROM_ID (f, this_face_id); \
18961 get_char_face_and_encoding (f, c, this_face_id, \
18962 char2b + n, 1, 1); \
18965 /* Make glyph_strings for each glyph sequence that is drawable by \
18966 the same face, and append them to HEAD/TAIL. */ \
18967 for (n = 0; n < cmp->glyph_len;) \
18969 s = (struct glyph_string *) alloca (sizeof *s); \
18970 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18971 append_glyph_string (&(HEAD), &(TAIL), s); \
18972 s->cmp = cmp; \
18973 s->gidx = n; \
18974 s->x = (X); \
18976 if (n == 0) \
18977 first_s = s; \
18979 n = fill_composite_glyph_string (s, faces, overlaps); \
18982 ++START; \
18983 s = first_s; \
18984 } while (0)
18987 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18988 of AREA of glyph row ROW on window W between indices START and END.
18989 HL overrides the face for drawing glyph strings, e.g. it is
18990 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18991 x-positions of the drawing area.
18993 This is an ugly monster macro construct because we must use alloca
18994 to allocate glyph strings (because draw_glyphs can be called
18995 asynchronously). */
18997 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18998 do \
19000 HEAD = TAIL = NULL; \
19001 while (START < END) \
19003 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19004 switch (first_glyph->type) \
19006 case CHAR_GLYPH: \
19007 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19008 HL, X, LAST_X); \
19009 break; \
19011 case COMPOSITE_GLYPH: \
19012 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19013 HL, X, LAST_X); \
19014 break; \
19016 case STRETCH_GLYPH: \
19017 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19018 HL, X, LAST_X); \
19019 break; \
19021 case IMAGE_GLYPH: \
19022 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19023 HL, X, LAST_X); \
19024 break; \
19026 default: \
19027 abort (); \
19030 set_glyph_string_background_width (s, START, LAST_X); \
19031 (X) += s->width; \
19034 while (0)
19037 /* Draw glyphs between START and END in AREA of ROW on window W,
19038 starting at x-position X. X is relative to AREA in W. HL is a
19039 face-override with the following meaning:
19041 DRAW_NORMAL_TEXT draw normally
19042 DRAW_CURSOR draw in cursor face
19043 DRAW_MOUSE_FACE draw in mouse face.
19044 DRAW_INVERSE_VIDEO draw in mode line face
19045 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19046 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19048 If OVERLAPS is non-zero, draw only the foreground of characters and
19049 clip to the physical height of ROW. Non-zero value also defines
19050 the overlapping part to be drawn:
19052 OVERLAPS_PRED overlap with preceding rows
19053 OVERLAPS_SUCC overlap with succeeding rows
19054 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19055 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19057 Value is the x-position reached, relative to AREA of W. */
19059 static int
19060 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19061 struct window *w;
19062 int x;
19063 struct glyph_row *row;
19064 enum glyph_row_area area;
19065 int start, end;
19066 enum draw_glyphs_face hl;
19067 int overlaps;
19069 struct glyph_string *head, *tail;
19070 struct glyph_string *s;
19071 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19072 int last_x, area_width;
19073 int x_reached;
19074 int i, j;
19075 struct frame *f = XFRAME (WINDOW_FRAME (w));
19076 DECLARE_HDC (hdc);
19078 ALLOCATE_HDC (hdc, f);
19080 /* Let's rather be paranoid than getting a SEGV. */
19081 end = min (end, row->used[area]);
19082 start = max (0, start);
19083 start = min (end, start);
19085 /* Translate X to frame coordinates. Set last_x to the right
19086 end of the drawing area. */
19087 if (row->full_width_p)
19089 /* X is relative to the left edge of W, without scroll bars
19090 or fringes. */
19091 x += WINDOW_LEFT_EDGE_X (w);
19092 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19094 else
19096 int area_left = window_box_left (w, area);
19097 x += area_left;
19098 area_width = window_box_width (w, area);
19099 last_x = area_left + area_width;
19102 /* Build a doubly-linked list of glyph_string structures between
19103 head and tail from what we have to draw. Note that the macro
19104 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19105 the reason we use a separate variable `i'. */
19106 i = start;
19107 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19108 if (tail)
19109 x_reached = tail->x + tail->background_width;
19110 else
19111 x_reached = x;
19113 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19114 the row, redraw some glyphs in front or following the glyph
19115 strings built above. */
19116 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19118 int dummy_x = 0;
19119 struct glyph_string *h, *t;
19121 /* Compute overhangs for all glyph strings. */
19122 if (rif->compute_glyph_string_overhangs)
19123 for (s = head; s; s = s->next)
19124 rif->compute_glyph_string_overhangs (s);
19126 /* Prepend glyph strings for glyphs in front of the first glyph
19127 string that are overwritten because of the first glyph
19128 string's left overhang. The background of all strings
19129 prepended must be drawn because the first glyph string
19130 draws over it. */
19131 i = left_overwritten (head);
19132 if (i >= 0)
19134 j = i;
19135 BUILD_GLYPH_STRINGS (j, start, h, t,
19136 DRAW_NORMAL_TEXT, dummy_x, last_x);
19137 start = i;
19138 compute_overhangs_and_x (t, head->x, 1);
19139 prepend_glyph_string_lists (&head, &tail, h, t);
19140 clip_head = head;
19143 /* Prepend glyph strings for glyphs in front of the first glyph
19144 string that overwrite that glyph string because of their
19145 right overhang. For these strings, only the foreground must
19146 be drawn, because it draws over the glyph string at `head'.
19147 The background must not be drawn because this would overwrite
19148 right overhangs of preceding glyphs for which no glyph
19149 strings exist. */
19150 i = left_overwriting (head);
19151 if (i >= 0)
19153 clip_head = head;
19154 BUILD_GLYPH_STRINGS (i, start, h, t,
19155 DRAW_NORMAL_TEXT, dummy_x, last_x);
19156 for (s = h; s; s = s->next)
19157 s->background_filled_p = 1;
19158 compute_overhangs_and_x (t, head->x, 1);
19159 prepend_glyph_string_lists (&head, &tail, h, t);
19162 /* Append glyphs strings for glyphs following the last glyph
19163 string tail that are overwritten by tail. The background of
19164 these strings has to be drawn because tail's foreground draws
19165 over it. */
19166 i = right_overwritten (tail);
19167 if (i >= 0)
19169 BUILD_GLYPH_STRINGS (end, i, h, t,
19170 DRAW_NORMAL_TEXT, x, last_x);
19171 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19172 append_glyph_string_lists (&head, &tail, h, t);
19173 clip_tail = tail;
19176 /* Append glyph strings for glyphs following the last glyph
19177 string tail that overwrite tail. The foreground of such
19178 glyphs has to be drawn because it writes into the background
19179 of tail. The background must not be drawn because it could
19180 paint over the foreground of following glyphs. */
19181 i = right_overwriting (tail);
19182 if (i >= 0)
19184 clip_tail = tail;
19185 BUILD_GLYPH_STRINGS (end, i, h, t,
19186 DRAW_NORMAL_TEXT, x, last_x);
19187 for (s = h; s; s = s->next)
19188 s->background_filled_p = 1;
19189 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19190 append_glyph_string_lists (&head, &tail, h, t);
19192 if (clip_head || clip_tail)
19193 for (s = head; s; s = s->next)
19195 s->clip_head = clip_head;
19196 s->clip_tail = clip_tail;
19200 /* Draw all strings. */
19201 for (s = head; s; s = s->next)
19202 rif->draw_glyph_string (s);
19204 if (area == TEXT_AREA
19205 && !row->full_width_p
19206 /* When drawing overlapping rows, only the glyph strings'
19207 foreground is drawn, which doesn't erase a cursor
19208 completely. */
19209 && !overlaps)
19211 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19212 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19213 : (tail ? tail->x + tail->background_width : x));
19215 int text_left = window_box_left (w, TEXT_AREA);
19216 x0 -= text_left;
19217 x1 -= text_left;
19219 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19220 row->y, MATRIX_ROW_BOTTOM_Y (row));
19223 /* Value is the x-position up to which drawn, relative to AREA of W.
19224 This doesn't include parts drawn because of overhangs. */
19225 if (row->full_width_p)
19226 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19227 else
19228 x_reached -= window_box_left (w, area);
19230 RELEASE_HDC (hdc, f);
19232 return x_reached;
19235 /* Expand row matrix if too narrow. Don't expand if area
19236 is not present. */
19238 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19240 if (!fonts_changed_p \
19241 && (it->glyph_row->glyphs[area] \
19242 < it->glyph_row->glyphs[area + 1])) \
19244 it->w->ncols_scale_factor++; \
19245 fonts_changed_p = 1; \
19249 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19250 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19252 static INLINE void
19253 append_glyph (it)
19254 struct it *it;
19256 struct glyph *glyph;
19257 enum glyph_row_area area = it->area;
19259 xassert (it->glyph_row);
19260 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19262 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19263 if (glyph < it->glyph_row->glyphs[area + 1])
19265 glyph->charpos = CHARPOS (it->position);
19266 glyph->object = it->object;
19267 glyph->pixel_width = it->pixel_width;
19268 glyph->ascent = it->ascent;
19269 glyph->descent = it->descent;
19270 glyph->voffset = it->voffset;
19271 glyph->type = CHAR_GLYPH;
19272 glyph->multibyte_p = it->multibyte_p;
19273 glyph->left_box_line_p = it->start_of_box_run_p;
19274 glyph->right_box_line_p = it->end_of_box_run_p;
19275 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19276 || it->phys_descent > it->descent);
19277 glyph->padding_p = 0;
19278 glyph->glyph_not_available_p = it->glyph_not_available_p;
19279 glyph->face_id = it->face_id;
19280 glyph->u.ch = it->char_to_display;
19281 glyph->slice = null_glyph_slice;
19282 glyph->font_type = FONT_TYPE_UNKNOWN;
19283 ++it->glyph_row->used[area];
19285 else
19286 IT_EXPAND_MATRIX_WIDTH (it, area);
19289 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19290 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19292 static INLINE void
19293 append_composite_glyph (it)
19294 struct it *it;
19296 struct glyph *glyph;
19297 enum glyph_row_area area = it->area;
19299 xassert (it->glyph_row);
19301 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19302 if (glyph < it->glyph_row->glyphs[area + 1])
19304 glyph->charpos = CHARPOS (it->position);
19305 glyph->object = it->object;
19306 glyph->pixel_width = it->pixel_width;
19307 glyph->ascent = it->ascent;
19308 glyph->descent = it->descent;
19309 glyph->voffset = it->voffset;
19310 glyph->type = COMPOSITE_GLYPH;
19311 glyph->multibyte_p = it->multibyte_p;
19312 glyph->left_box_line_p = it->start_of_box_run_p;
19313 glyph->right_box_line_p = it->end_of_box_run_p;
19314 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19315 || it->phys_descent > it->descent);
19316 glyph->padding_p = 0;
19317 glyph->glyph_not_available_p = 0;
19318 glyph->face_id = it->face_id;
19319 glyph->u.cmp_id = it->cmp_id;
19320 glyph->slice = null_glyph_slice;
19321 glyph->font_type = FONT_TYPE_UNKNOWN;
19322 ++it->glyph_row->used[area];
19324 else
19325 IT_EXPAND_MATRIX_WIDTH (it, area);
19329 /* Change IT->ascent and IT->height according to the setting of
19330 IT->voffset. */
19332 static INLINE void
19333 take_vertical_position_into_account (it)
19334 struct it *it;
19336 if (it->voffset)
19338 if (it->voffset < 0)
19339 /* Increase the ascent so that we can display the text higher
19340 in the line. */
19341 it->ascent -= it->voffset;
19342 else
19343 /* Increase the descent so that we can display the text lower
19344 in the line. */
19345 it->descent += it->voffset;
19350 /* Produce glyphs/get display metrics for the image IT is loaded with.
19351 See the description of struct display_iterator in dispextern.h for
19352 an overview of struct display_iterator. */
19354 static void
19355 produce_image_glyph (it)
19356 struct it *it;
19358 struct image *img;
19359 struct face *face;
19360 int glyph_ascent;
19361 struct glyph_slice slice;
19363 xassert (it->what == IT_IMAGE);
19365 face = FACE_FROM_ID (it->f, it->face_id);
19366 xassert (face);
19367 /* Make sure X resources of the face is loaded. */
19368 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19370 if (it->image_id < 0)
19372 /* Fringe bitmap. */
19373 it->ascent = it->phys_ascent = 0;
19374 it->descent = it->phys_descent = 0;
19375 it->pixel_width = 0;
19376 it->nglyphs = 0;
19377 return;
19380 img = IMAGE_FROM_ID (it->f, it->image_id);
19381 xassert (img);
19382 /* Make sure X resources of the image is loaded. */
19383 prepare_image_for_display (it->f, img);
19385 slice.x = slice.y = 0;
19386 slice.width = img->width;
19387 slice.height = img->height;
19389 if (INTEGERP (it->slice.x))
19390 slice.x = XINT (it->slice.x);
19391 else if (FLOATP (it->slice.x))
19392 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19394 if (INTEGERP (it->slice.y))
19395 slice.y = XINT (it->slice.y);
19396 else if (FLOATP (it->slice.y))
19397 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19399 if (INTEGERP (it->slice.width))
19400 slice.width = XINT (it->slice.width);
19401 else if (FLOATP (it->slice.width))
19402 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19404 if (INTEGERP (it->slice.height))
19405 slice.height = XINT (it->slice.height);
19406 else if (FLOATP (it->slice.height))
19407 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19409 if (slice.x >= img->width)
19410 slice.x = img->width;
19411 if (slice.y >= img->height)
19412 slice.y = img->height;
19413 if (slice.x + slice.width >= img->width)
19414 slice.width = img->width - slice.x;
19415 if (slice.y + slice.height > img->height)
19416 slice.height = img->height - slice.y;
19418 if (slice.width == 0 || slice.height == 0)
19419 return;
19421 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19423 it->descent = slice.height - glyph_ascent;
19424 if (slice.y == 0)
19425 it->descent += img->vmargin;
19426 if (slice.y + slice.height == img->height)
19427 it->descent += img->vmargin;
19428 it->phys_descent = it->descent;
19430 it->pixel_width = slice.width;
19431 if (slice.x == 0)
19432 it->pixel_width += img->hmargin;
19433 if (slice.x + slice.width == img->width)
19434 it->pixel_width += img->hmargin;
19436 /* It's quite possible for images to have an ascent greater than
19437 their height, so don't get confused in that case. */
19438 if (it->descent < 0)
19439 it->descent = 0;
19441 #if 0 /* this breaks image tiling */
19442 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19443 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19444 if (face_ascent > it->ascent)
19445 it->ascent = it->phys_ascent = face_ascent;
19446 #endif
19448 it->nglyphs = 1;
19450 if (face->box != FACE_NO_BOX)
19452 if (face->box_line_width > 0)
19454 if (slice.y == 0)
19455 it->ascent += face->box_line_width;
19456 if (slice.y + slice.height == img->height)
19457 it->descent += face->box_line_width;
19460 if (it->start_of_box_run_p && slice.x == 0)
19461 it->pixel_width += abs (face->box_line_width);
19462 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19463 it->pixel_width += abs (face->box_line_width);
19466 take_vertical_position_into_account (it);
19468 if (it->glyph_row)
19470 struct glyph *glyph;
19471 enum glyph_row_area area = it->area;
19473 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19474 if (glyph < it->glyph_row->glyphs[area + 1])
19476 glyph->charpos = CHARPOS (it->position);
19477 glyph->object = it->object;
19478 glyph->pixel_width = it->pixel_width;
19479 glyph->ascent = glyph_ascent;
19480 glyph->descent = it->descent;
19481 glyph->voffset = it->voffset;
19482 glyph->type = IMAGE_GLYPH;
19483 glyph->multibyte_p = it->multibyte_p;
19484 glyph->left_box_line_p = it->start_of_box_run_p;
19485 glyph->right_box_line_p = it->end_of_box_run_p;
19486 glyph->overlaps_vertically_p = 0;
19487 glyph->padding_p = 0;
19488 glyph->glyph_not_available_p = 0;
19489 glyph->face_id = it->face_id;
19490 glyph->u.img_id = img->id;
19491 glyph->slice = slice;
19492 glyph->font_type = FONT_TYPE_UNKNOWN;
19493 ++it->glyph_row->used[area];
19495 else
19496 IT_EXPAND_MATRIX_WIDTH (it, area);
19501 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19502 of the glyph, WIDTH and HEIGHT are the width and height of the
19503 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19505 static void
19506 append_stretch_glyph (it, object, width, height, ascent)
19507 struct it *it;
19508 Lisp_Object object;
19509 int width, height;
19510 int ascent;
19512 struct glyph *glyph;
19513 enum glyph_row_area area = it->area;
19515 xassert (ascent >= 0 && ascent <= height);
19517 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19518 if (glyph < it->glyph_row->glyphs[area + 1])
19520 glyph->charpos = CHARPOS (it->position);
19521 glyph->object = object;
19522 glyph->pixel_width = width;
19523 glyph->ascent = ascent;
19524 glyph->descent = height - ascent;
19525 glyph->voffset = it->voffset;
19526 glyph->type = STRETCH_GLYPH;
19527 glyph->multibyte_p = it->multibyte_p;
19528 glyph->left_box_line_p = it->start_of_box_run_p;
19529 glyph->right_box_line_p = it->end_of_box_run_p;
19530 glyph->overlaps_vertically_p = 0;
19531 glyph->padding_p = 0;
19532 glyph->glyph_not_available_p = 0;
19533 glyph->face_id = it->face_id;
19534 glyph->u.stretch.ascent = ascent;
19535 glyph->u.stretch.height = height;
19536 glyph->slice = null_glyph_slice;
19537 glyph->font_type = FONT_TYPE_UNKNOWN;
19538 ++it->glyph_row->used[area];
19540 else
19541 IT_EXPAND_MATRIX_WIDTH (it, area);
19545 /* Produce a stretch glyph for iterator IT. IT->object is the value
19546 of the glyph property displayed. The value must be a list
19547 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19548 being recognized:
19550 1. `:width WIDTH' specifies that the space should be WIDTH *
19551 canonical char width wide. WIDTH may be an integer or floating
19552 point number.
19554 2. `:relative-width FACTOR' specifies that the width of the stretch
19555 should be computed from the width of the first character having the
19556 `glyph' property, and should be FACTOR times that width.
19558 3. `:align-to HPOS' specifies that the space should be wide enough
19559 to reach HPOS, a value in canonical character units.
19561 Exactly one of the above pairs must be present.
19563 4. `:height HEIGHT' specifies that the height of the stretch produced
19564 should be HEIGHT, measured in canonical character units.
19566 5. `:relative-height FACTOR' specifies that the height of the
19567 stretch should be FACTOR times the height of the characters having
19568 the glyph property.
19570 Either none or exactly one of 4 or 5 must be present.
19572 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19573 of the stretch should be used for the ascent of the stretch.
19574 ASCENT must be in the range 0 <= ASCENT <= 100. */
19576 static void
19577 produce_stretch_glyph (it)
19578 struct it *it;
19580 /* (space :width WIDTH :height HEIGHT ...) */
19581 Lisp_Object prop, plist;
19582 int width = 0, height = 0, align_to = -1;
19583 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19584 int ascent = 0;
19585 double tem;
19586 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19587 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19589 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19591 /* List should start with `space'. */
19592 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19593 plist = XCDR (it->object);
19595 /* Compute the width of the stretch. */
19596 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19597 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19599 /* Absolute width `:width WIDTH' specified and valid. */
19600 zero_width_ok_p = 1;
19601 width = (int)tem;
19603 else if (prop = Fplist_get (plist, QCrelative_width),
19604 NUMVAL (prop) > 0)
19606 /* Relative width `:relative-width FACTOR' specified and valid.
19607 Compute the width of the characters having the `glyph'
19608 property. */
19609 struct it it2;
19610 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19612 it2 = *it;
19613 if (it->multibyte_p)
19615 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19616 - IT_BYTEPOS (*it));
19617 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19619 else
19620 it2.c = *p, it2.len = 1;
19622 it2.glyph_row = NULL;
19623 it2.what = IT_CHARACTER;
19624 x_produce_glyphs (&it2);
19625 width = NUMVAL (prop) * it2.pixel_width;
19627 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19628 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19630 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19631 align_to = (align_to < 0
19633 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19634 else if (align_to < 0)
19635 align_to = window_box_left_offset (it->w, TEXT_AREA);
19636 width = max (0, (int)tem + align_to - it->current_x);
19637 zero_width_ok_p = 1;
19639 else
19640 /* Nothing specified -> width defaults to canonical char width. */
19641 width = FRAME_COLUMN_WIDTH (it->f);
19643 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19644 width = 1;
19646 /* Compute height. */
19647 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19648 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19650 height = (int)tem;
19651 zero_height_ok_p = 1;
19653 else if (prop = Fplist_get (plist, QCrelative_height),
19654 NUMVAL (prop) > 0)
19655 height = FONT_HEIGHT (font) * NUMVAL (prop);
19656 else
19657 height = FONT_HEIGHT (font);
19659 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19660 height = 1;
19662 /* Compute percentage of height used for ascent. If
19663 `:ascent ASCENT' is present and valid, use that. Otherwise,
19664 derive the ascent from the font in use. */
19665 if (prop = Fplist_get (plist, QCascent),
19666 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19667 ascent = height * NUMVAL (prop) / 100.0;
19668 else if (!NILP (prop)
19669 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19670 ascent = min (max (0, (int)tem), height);
19671 else
19672 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19674 if (width > 0 && height > 0 && it->glyph_row)
19676 Lisp_Object object = it->stack[it->sp - 1].string;
19677 if (!STRINGP (object))
19678 object = it->w->buffer;
19679 append_stretch_glyph (it, object, width, height, ascent);
19682 it->pixel_width = width;
19683 it->ascent = it->phys_ascent = ascent;
19684 it->descent = it->phys_descent = height - it->ascent;
19685 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19687 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19689 if (face->box_line_width > 0)
19691 it->ascent += face->box_line_width;
19692 it->descent += face->box_line_width;
19695 if (it->start_of_box_run_p)
19696 it->pixel_width += abs (face->box_line_width);
19697 if (it->end_of_box_run_p)
19698 it->pixel_width += abs (face->box_line_width);
19701 take_vertical_position_into_account (it);
19704 /* Get line-height and line-spacing property at point.
19705 If line-height has format (HEIGHT TOTAL), return TOTAL
19706 in TOTAL_HEIGHT. */
19708 static Lisp_Object
19709 get_line_height_property (it, prop)
19710 struct it *it;
19711 Lisp_Object prop;
19713 Lisp_Object position;
19715 if (STRINGP (it->object))
19716 position = make_number (IT_STRING_CHARPOS (*it));
19717 else if (BUFFERP (it->object))
19718 position = make_number (IT_CHARPOS (*it));
19719 else
19720 return Qnil;
19722 return Fget_char_property (position, prop, it->object);
19725 /* Calculate line-height and line-spacing properties.
19726 An integer value specifies explicit pixel value.
19727 A float value specifies relative value to current face height.
19728 A cons (float . face-name) specifies relative value to
19729 height of specified face font.
19731 Returns height in pixels, or nil. */
19734 static Lisp_Object
19735 calc_line_height_property (it, val, font, boff, override)
19736 struct it *it;
19737 Lisp_Object val;
19738 XFontStruct *font;
19739 int boff, override;
19741 Lisp_Object face_name = Qnil;
19742 int ascent, descent, height;
19744 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19745 return val;
19747 if (CONSP (val))
19749 face_name = XCAR (val);
19750 val = XCDR (val);
19751 if (!NUMBERP (val))
19752 val = make_number (1);
19753 if (NILP (face_name))
19755 height = it->ascent + it->descent;
19756 goto scale;
19760 if (NILP (face_name))
19762 font = FRAME_FONT (it->f);
19763 boff = FRAME_BASELINE_OFFSET (it->f);
19765 else if (EQ (face_name, Qt))
19767 override = 0;
19769 else
19771 int face_id;
19772 struct face *face;
19773 struct font_info *font_info;
19775 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19776 if (face_id < 0)
19777 return make_number (-1);
19779 face = FACE_FROM_ID (it->f, face_id);
19780 font = face->font;
19781 if (font == NULL)
19782 return make_number (-1);
19784 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19785 boff = font_info->baseline_offset;
19786 if (font_info->vertical_centering)
19787 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19790 ascent = FONT_BASE (font) + boff;
19791 descent = FONT_DESCENT (font) - boff;
19793 if (override)
19795 it->override_ascent = ascent;
19796 it->override_descent = descent;
19797 it->override_boff = boff;
19800 height = ascent + descent;
19802 scale:
19803 if (FLOATP (val))
19804 height = (int)(XFLOAT_DATA (val) * height);
19805 else if (INTEGERP (val))
19806 height *= XINT (val);
19808 return make_number (height);
19812 /* RIF:
19813 Produce glyphs/get display metrics for the display element IT is
19814 loaded with. See the description of struct display_iterator in
19815 dispextern.h for an overview of struct display_iterator. */
19817 void
19818 x_produce_glyphs (it)
19819 struct it *it;
19821 int extra_line_spacing = it->extra_line_spacing;
19823 it->glyph_not_available_p = 0;
19825 if (it->what == IT_CHARACTER)
19827 XChar2b char2b;
19828 XFontStruct *font;
19829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19830 XCharStruct *pcm;
19831 int font_not_found_p;
19832 struct font_info *font_info;
19833 int boff; /* baseline offset */
19834 /* We may change it->multibyte_p upon unibyte<->multibyte
19835 conversion. So, save the current value now and restore it
19836 later.
19838 Note: It seems that we don't have to record multibyte_p in
19839 struct glyph because the character code itself tells if or
19840 not the character is multibyte. Thus, in the future, we must
19841 consider eliminating the field `multibyte_p' in the struct
19842 glyph. */
19843 int saved_multibyte_p = it->multibyte_p;
19845 /* Maybe translate single-byte characters to multibyte, or the
19846 other way. */
19847 it->char_to_display = it->c;
19848 if (!ASCII_BYTE_P (it->c))
19850 if (unibyte_display_via_language_environment
19851 && SINGLE_BYTE_CHAR_P (it->c)
19852 && (it->c >= 0240
19853 || !NILP (Vnonascii_translation_table)))
19855 it->char_to_display = unibyte_char_to_multibyte (it->c);
19856 it->multibyte_p = 1;
19857 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19858 face = FACE_FROM_ID (it->f, it->face_id);
19860 else if (!SINGLE_BYTE_CHAR_P (it->c)
19861 && !it->multibyte_p)
19863 it->multibyte_p = 1;
19864 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19865 face = FACE_FROM_ID (it->f, it->face_id);
19869 /* Get font to use. Encode IT->char_to_display. */
19870 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19871 &char2b, it->multibyte_p, 0);
19872 font = face->font;
19874 /* When no suitable font found, use the default font. */
19875 font_not_found_p = font == NULL;
19876 if (font_not_found_p)
19878 font = FRAME_FONT (it->f);
19879 boff = FRAME_BASELINE_OFFSET (it->f);
19880 font_info = NULL;
19882 else
19884 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19885 boff = font_info->baseline_offset;
19886 if (font_info->vertical_centering)
19887 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19890 if (it->char_to_display >= ' '
19891 && (!it->multibyte_p || it->char_to_display < 128))
19893 /* Either unibyte or ASCII. */
19894 int stretched_p;
19896 it->nglyphs = 1;
19898 pcm = rif->per_char_metric (font, &char2b,
19899 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19901 if (it->override_ascent >= 0)
19903 it->ascent = it->override_ascent;
19904 it->descent = it->override_descent;
19905 boff = it->override_boff;
19907 else
19909 it->ascent = FONT_BASE (font) + boff;
19910 it->descent = FONT_DESCENT (font) - boff;
19913 if (pcm)
19915 it->phys_ascent = pcm->ascent + boff;
19916 it->phys_descent = pcm->descent - boff;
19917 it->pixel_width = pcm->width;
19919 else
19921 it->glyph_not_available_p = 1;
19922 it->phys_ascent = it->ascent;
19923 it->phys_descent = it->descent;
19924 it->pixel_width = FONT_WIDTH (font);
19927 if (it->constrain_row_ascent_descent_p)
19929 if (it->descent > it->max_descent)
19931 it->ascent += it->descent - it->max_descent;
19932 it->descent = it->max_descent;
19934 if (it->ascent > it->max_ascent)
19936 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19937 it->ascent = it->max_ascent;
19939 it->phys_ascent = min (it->phys_ascent, it->ascent);
19940 it->phys_descent = min (it->phys_descent, it->descent);
19941 extra_line_spacing = 0;
19944 /* If this is a space inside a region of text with
19945 `space-width' property, change its width. */
19946 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19947 if (stretched_p)
19948 it->pixel_width *= XFLOATINT (it->space_width);
19950 /* If face has a box, add the box thickness to the character
19951 height. If character has a box line to the left and/or
19952 right, add the box line width to the character's width. */
19953 if (face->box != FACE_NO_BOX)
19955 int thick = face->box_line_width;
19957 if (thick > 0)
19959 it->ascent += thick;
19960 it->descent += thick;
19962 else
19963 thick = -thick;
19965 if (it->start_of_box_run_p)
19966 it->pixel_width += thick;
19967 if (it->end_of_box_run_p)
19968 it->pixel_width += thick;
19971 /* If face has an overline, add the height of the overline
19972 (1 pixel) and a 1 pixel margin to the character height. */
19973 if (face->overline_p)
19974 it->ascent += 2;
19976 if (it->constrain_row_ascent_descent_p)
19978 if (it->ascent > it->max_ascent)
19979 it->ascent = it->max_ascent;
19980 if (it->descent > it->max_descent)
19981 it->descent = it->max_descent;
19984 take_vertical_position_into_account (it);
19986 /* If we have to actually produce glyphs, do it. */
19987 if (it->glyph_row)
19989 if (stretched_p)
19991 /* Translate a space with a `space-width' property
19992 into a stretch glyph. */
19993 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19994 / FONT_HEIGHT (font));
19995 append_stretch_glyph (it, it->object, it->pixel_width,
19996 it->ascent + it->descent, ascent);
19998 else
19999 append_glyph (it);
20001 /* If characters with lbearing or rbearing are displayed
20002 in this line, record that fact in a flag of the
20003 glyph row. This is used to optimize X output code. */
20004 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20005 it->glyph_row->contains_overlapping_glyphs_p = 1;
20008 else if (it->char_to_display == '\n')
20010 /* A newline has no width but we need the height of the line.
20011 But if previous part of the line set a height, don't
20012 increase that height */
20014 Lisp_Object height;
20015 Lisp_Object total_height = Qnil;
20017 it->override_ascent = -1;
20018 it->pixel_width = 0;
20019 it->nglyphs = 0;
20021 height = get_line_height_property(it, Qline_height);
20022 /* Split (line-height total-height) list */
20023 if (CONSP (height)
20024 && CONSP (XCDR (height))
20025 && NILP (XCDR (XCDR (height))))
20027 total_height = XCAR (XCDR (height));
20028 height = XCAR (height);
20030 height = calc_line_height_property(it, height, font, boff, 1);
20032 if (it->override_ascent >= 0)
20034 it->ascent = it->override_ascent;
20035 it->descent = it->override_descent;
20036 boff = it->override_boff;
20038 else
20040 it->ascent = FONT_BASE (font) + boff;
20041 it->descent = FONT_DESCENT (font) - boff;
20044 if (EQ (height, Qt))
20046 if (it->descent > it->max_descent)
20048 it->ascent += it->descent - it->max_descent;
20049 it->descent = it->max_descent;
20051 if (it->ascent > it->max_ascent)
20053 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20054 it->ascent = it->max_ascent;
20056 it->phys_ascent = min (it->phys_ascent, it->ascent);
20057 it->phys_descent = min (it->phys_descent, it->descent);
20058 it->constrain_row_ascent_descent_p = 1;
20059 extra_line_spacing = 0;
20061 else
20063 Lisp_Object spacing;
20065 it->phys_ascent = it->ascent;
20066 it->phys_descent = it->descent;
20068 if ((it->max_ascent > 0 || it->max_descent > 0)
20069 && face->box != FACE_NO_BOX
20070 && face->box_line_width > 0)
20072 it->ascent += face->box_line_width;
20073 it->descent += face->box_line_width;
20075 if (!NILP (height)
20076 && XINT (height) > it->ascent + it->descent)
20077 it->ascent = XINT (height) - it->descent;
20079 if (!NILP (total_height))
20080 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20081 else
20083 spacing = get_line_height_property(it, Qline_spacing);
20084 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20086 if (INTEGERP (spacing))
20088 extra_line_spacing = XINT (spacing);
20089 if (!NILP (total_height))
20090 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20094 else if (it->char_to_display == '\t')
20096 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20097 int x = it->current_x + it->continuation_lines_width;
20098 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20100 /* If the distance from the current position to the next tab
20101 stop is less than a space character width, use the
20102 tab stop after that. */
20103 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20104 next_tab_x += tab_width;
20106 it->pixel_width = next_tab_x - x;
20107 it->nglyphs = 1;
20108 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20109 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20111 if (it->glyph_row)
20113 append_stretch_glyph (it, it->object, it->pixel_width,
20114 it->ascent + it->descent, it->ascent);
20117 else
20119 /* A multi-byte character. Assume that the display width of the
20120 character is the width of the character multiplied by the
20121 width of the font. */
20123 /* If we found a font, this font should give us the right
20124 metrics. If we didn't find a font, use the frame's
20125 default font and calculate the width of the character
20126 from the charset width; this is what old redisplay code
20127 did. */
20129 pcm = rif->per_char_metric (font, &char2b,
20130 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20132 if (font_not_found_p || !pcm)
20134 int charset = CHAR_CHARSET (it->char_to_display);
20136 it->glyph_not_available_p = 1;
20137 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20138 * CHARSET_WIDTH (charset));
20139 it->phys_ascent = FONT_BASE (font) + boff;
20140 it->phys_descent = FONT_DESCENT (font) - boff;
20142 else
20144 it->pixel_width = pcm->width;
20145 it->phys_ascent = pcm->ascent + boff;
20146 it->phys_descent = pcm->descent - boff;
20147 if (it->glyph_row
20148 && (pcm->lbearing < 0
20149 || pcm->rbearing > pcm->width))
20150 it->glyph_row->contains_overlapping_glyphs_p = 1;
20152 it->nglyphs = 1;
20153 it->ascent = FONT_BASE (font) + boff;
20154 it->descent = FONT_DESCENT (font) - boff;
20155 if (face->box != FACE_NO_BOX)
20157 int thick = face->box_line_width;
20159 if (thick > 0)
20161 it->ascent += thick;
20162 it->descent += thick;
20164 else
20165 thick = - thick;
20167 if (it->start_of_box_run_p)
20168 it->pixel_width += thick;
20169 if (it->end_of_box_run_p)
20170 it->pixel_width += thick;
20173 /* If face has an overline, add the height of the overline
20174 (1 pixel) and a 1 pixel margin to the character height. */
20175 if (face->overline_p)
20176 it->ascent += 2;
20178 take_vertical_position_into_account (it);
20180 if (it->glyph_row)
20181 append_glyph (it);
20183 it->multibyte_p = saved_multibyte_p;
20185 else if (it->what == IT_COMPOSITION)
20187 /* Note: A composition is represented as one glyph in the
20188 glyph matrix. There are no padding glyphs. */
20189 XChar2b char2b;
20190 XFontStruct *font;
20191 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20192 XCharStruct *pcm;
20193 int font_not_found_p;
20194 struct font_info *font_info;
20195 int boff; /* baseline offset */
20196 struct composition *cmp = composition_table[it->cmp_id];
20198 /* Maybe translate single-byte characters to multibyte. */
20199 it->char_to_display = it->c;
20200 if (unibyte_display_via_language_environment
20201 && SINGLE_BYTE_CHAR_P (it->c)
20202 && (it->c >= 0240
20203 || (it->c >= 0200
20204 && !NILP (Vnonascii_translation_table))))
20206 it->char_to_display = unibyte_char_to_multibyte (it->c);
20209 /* Get face and font to use. Encode IT->char_to_display. */
20210 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20211 face = FACE_FROM_ID (it->f, it->face_id);
20212 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20213 &char2b, it->multibyte_p, 0);
20214 font = face->font;
20216 /* When no suitable font found, use the default font. */
20217 font_not_found_p = font == NULL;
20218 if (font_not_found_p)
20220 font = FRAME_FONT (it->f);
20221 boff = FRAME_BASELINE_OFFSET (it->f);
20222 font_info = NULL;
20224 else
20226 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20227 boff = font_info->baseline_offset;
20228 if (font_info->vertical_centering)
20229 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20232 /* There are no padding glyphs, so there is only one glyph to
20233 produce for the composition. Important is that pixel_width,
20234 ascent and descent are the values of what is drawn by
20235 draw_glyphs (i.e. the values of the overall glyphs composed). */
20236 it->nglyphs = 1;
20238 /* If we have not yet calculated pixel size data of glyphs of
20239 the composition for the current face font, calculate them
20240 now. Theoretically, we have to check all fonts for the
20241 glyphs, but that requires much time and memory space. So,
20242 here we check only the font of the first glyph. This leads
20243 to incorrect display very rarely, and C-l (recenter) can
20244 correct the display anyway. */
20245 if (cmp->font != (void *) font)
20247 /* Ascent and descent of the font of the first character of
20248 this composition (adjusted by baseline offset). Ascent
20249 and descent of overall glyphs should not be less than
20250 them respectively. */
20251 int font_ascent = FONT_BASE (font) + boff;
20252 int font_descent = FONT_DESCENT (font) - boff;
20253 /* Bounding box of the overall glyphs. */
20254 int leftmost, rightmost, lowest, highest;
20255 int i, width, ascent, descent;
20257 cmp->font = (void *) font;
20259 /* Initialize the bounding box. */
20260 if (font_info
20261 && (pcm = rif->per_char_metric (font, &char2b,
20262 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20264 width = pcm->width;
20265 ascent = pcm->ascent;
20266 descent = pcm->descent;
20268 else
20270 width = FONT_WIDTH (font);
20271 ascent = FONT_BASE (font);
20272 descent = FONT_DESCENT (font);
20275 rightmost = width;
20276 lowest = - descent + boff;
20277 highest = ascent + boff;
20278 leftmost = 0;
20280 if (font_info
20281 && font_info->default_ascent
20282 && CHAR_TABLE_P (Vuse_default_ascent)
20283 && !NILP (Faref (Vuse_default_ascent,
20284 make_number (it->char_to_display))))
20285 highest = font_info->default_ascent + boff;
20287 /* Draw the first glyph at the normal position. It may be
20288 shifted to right later if some other glyphs are drawn at
20289 the left. */
20290 cmp->offsets[0] = 0;
20291 cmp->offsets[1] = boff;
20293 /* Set cmp->offsets for the remaining glyphs. */
20294 for (i = 1; i < cmp->glyph_len; i++)
20296 int left, right, btm, top;
20297 int ch = COMPOSITION_GLYPH (cmp, i);
20298 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20300 face = FACE_FROM_ID (it->f, face_id);
20301 get_char_face_and_encoding (it->f, ch, face->id,
20302 &char2b, it->multibyte_p, 0);
20303 font = face->font;
20304 if (font == NULL)
20306 font = FRAME_FONT (it->f);
20307 boff = FRAME_BASELINE_OFFSET (it->f);
20308 font_info = NULL;
20310 else
20312 font_info
20313 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20314 boff = font_info->baseline_offset;
20315 if (font_info->vertical_centering)
20316 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20319 if (font_info
20320 && (pcm = rif->per_char_metric (font, &char2b,
20321 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20323 width = pcm->width;
20324 ascent = pcm->ascent;
20325 descent = pcm->descent;
20327 else
20329 width = FONT_WIDTH (font);
20330 ascent = 1;
20331 descent = 0;
20334 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20336 /* Relative composition with or without
20337 alternate chars. */
20338 left = (leftmost + rightmost - width) / 2;
20339 btm = - descent + boff;
20340 if (font_info && font_info->relative_compose
20341 && (! CHAR_TABLE_P (Vignore_relative_composition)
20342 || NILP (Faref (Vignore_relative_composition,
20343 make_number (ch)))))
20346 if (- descent >= font_info->relative_compose)
20347 /* One extra pixel between two glyphs. */
20348 btm = highest + 1;
20349 else if (ascent <= 0)
20350 /* One extra pixel between two glyphs. */
20351 btm = lowest - 1 - ascent - descent;
20354 else
20356 /* A composition rule is specified by an integer
20357 value that encodes global and new reference
20358 points (GREF and NREF). GREF and NREF are
20359 specified by numbers as below:
20361 0---1---2 -- ascent
20365 9--10--11 -- center
20367 ---3---4---5--- baseline
20369 6---7---8 -- descent
20371 int rule = COMPOSITION_RULE (cmp, i);
20372 int gref, nref, grefx, grefy, nrefx, nrefy;
20374 COMPOSITION_DECODE_RULE (rule, gref, nref);
20375 grefx = gref % 3, nrefx = nref % 3;
20376 grefy = gref / 3, nrefy = nref / 3;
20378 left = (leftmost
20379 + grefx * (rightmost - leftmost) / 2
20380 - nrefx * width / 2);
20381 btm = ((grefy == 0 ? highest
20382 : grefy == 1 ? 0
20383 : grefy == 2 ? lowest
20384 : (highest + lowest) / 2)
20385 - (nrefy == 0 ? ascent + descent
20386 : nrefy == 1 ? descent - boff
20387 : nrefy == 2 ? 0
20388 : (ascent + descent) / 2));
20391 cmp->offsets[i * 2] = left;
20392 cmp->offsets[i * 2 + 1] = btm + descent;
20394 /* Update the bounding box of the overall glyphs. */
20395 right = left + width;
20396 top = btm + descent + ascent;
20397 if (left < leftmost)
20398 leftmost = left;
20399 if (right > rightmost)
20400 rightmost = right;
20401 if (top > highest)
20402 highest = top;
20403 if (btm < lowest)
20404 lowest = btm;
20407 /* If there are glyphs whose x-offsets are negative,
20408 shift all glyphs to the right and make all x-offsets
20409 non-negative. */
20410 if (leftmost < 0)
20412 for (i = 0; i < cmp->glyph_len; i++)
20413 cmp->offsets[i * 2] -= leftmost;
20414 rightmost -= leftmost;
20417 cmp->pixel_width = rightmost;
20418 cmp->ascent = highest;
20419 cmp->descent = - lowest;
20420 if (cmp->ascent < font_ascent)
20421 cmp->ascent = font_ascent;
20422 if (cmp->descent < font_descent)
20423 cmp->descent = font_descent;
20426 it->pixel_width = cmp->pixel_width;
20427 it->ascent = it->phys_ascent = cmp->ascent;
20428 it->descent = it->phys_descent = cmp->descent;
20430 if (face->box != FACE_NO_BOX)
20432 int thick = face->box_line_width;
20434 if (thick > 0)
20436 it->ascent += thick;
20437 it->descent += thick;
20439 else
20440 thick = - thick;
20442 if (it->start_of_box_run_p)
20443 it->pixel_width += thick;
20444 if (it->end_of_box_run_p)
20445 it->pixel_width += thick;
20448 /* If face has an overline, add the height of the overline
20449 (1 pixel) and a 1 pixel margin to the character height. */
20450 if (face->overline_p)
20451 it->ascent += 2;
20453 take_vertical_position_into_account (it);
20455 if (it->glyph_row)
20456 append_composite_glyph (it);
20458 else if (it->what == IT_IMAGE)
20459 produce_image_glyph (it);
20460 else if (it->what == IT_STRETCH)
20461 produce_stretch_glyph (it);
20463 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20464 because this isn't true for images with `:ascent 100'. */
20465 xassert (it->ascent >= 0 && it->descent >= 0);
20466 if (it->area == TEXT_AREA)
20467 it->current_x += it->pixel_width;
20469 if (extra_line_spacing > 0)
20471 it->descent += extra_line_spacing;
20472 if (extra_line_spacing > it->max_extra_line_spacing)
20473 it->max_extra_line_spacing = extra_line_spacing;
20476 it->max_ascent = max (it->max_ascent, it->ascent);
20477 it->max_descent = max (it->max_descent, it->descent);
20478 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20479 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20482 /* EXPORT for RIF:
20483 Output LEN glyphs starting at START at the nominal cursor position.
20484 Advance the nominal cursor over the text. The global variable
20485 updated_window contains the window being updated, updated_row is
20486 the glyph row being updated, and updated_area is the area of that
20487 row being updated. */
20489 void
20490 x_write_glyphs (start, len)
20491 struct glyph *start;
20492 int len;
20494 int x, hpos;
20496 xassert (updated_window && updated_row);
20497 BLOCK_INPUT;
20499 /* Write glyphs. */
20501 hpos = start - updated_row->glyphs[updated_area];
20502 x = draw_glyphs (updated_window, output_cursor.x,
20503 updated_row, updated_area,
20504 hpos, hpos + len,
20505 DRAW_NORMAL_TEXT, 0);
20507 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20508 if (updated_area == TEXT_AREA
20509 && updated_window->phys_cursor_on_p
20510 && updated_window->phys_cursor.vpos == output_cursor.vpos
20511 && updated_window->phys_cursor.hpos >= hpos
20512 && updated_window->phys_cursor.hpos < hpos + len)
20513 updated_window->phys_cursor_on_p = 0;
20515 UNBLOCK_INPUT;
20517 /* Advance the output cursor. */
20518 output_cursor.hpos += len;
20519 output_cursor.x = x;
20523 /* EXPORT for RIF:
20524 Insert LEN glyphs from START at the nominal cursor position. */
20526 void
20527 x_insert_glyphs (start, len)
20528 struct glyph *start;
20529 int len;
20531 struct frame *f;
20532 struct window *w;
20533 int line_height, shift_by_width, shifted_region_width;
20534 struct glyph_row *row;
20535 struct glyph *glyph;
20536 int frame_x, frame_y, hpos;
20538 xassert (updated_window && updated_row);
20539 BLOCK_INPUT;
20540 w = updated_window;
20541 f = XFRAME (WINDOW_FRAME (w));
20543 /* Get the height of the line we are in. */
20544 row = updated_row;
20545 line_height = row->height;
20547 /* Get the width of the glyphs to insert. */
20548 shift_by_width = 0;
20549 for (glyph = start; glyph < start + len; ++glyph)
20550 shift_by_width += glyph->pixel_width;
20552 /* Get the width of the region to shift right. */
20553 shifted_region_width = (window_box_width (w, updated_area)
20554 - output_cursor.x
20555 - shift_by_width);
20557 /* Shift right. */
20558 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20559 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20561 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20562 line_height, shift_by_width);
20564 /* Write the glyphs. */
20565 hpos = start - row->glyphs[updated_area];
20566 draw_glyphs (w, output_cursor.x, row, updated_area,
20567 hpos, hpos + len,
20568 DRAW_NORMAL_TEXT, 0);
20570 /* Advance the output cursor. */
20571 output_cursor.hpos += len;
20572 output_cursor.x += shift_by_width;
20573 UNBLOCK_INPUT;
20577 /* EXPORT for RIF:
20578 Erase the current text line from the nominal cursor position
20579 (inclusive) to pixel column TO_X (exclusive). The idea is that
20580 everything from TO_X onward is already erased.
20582 TO_X is a pixel position relative to updated_area of
20583 updated_window. TO_X == -1 means clear to the end of this area. */
20585 void
20586 x_clear_end_of_line (to_x)
20587 int to_x;
20589 struct frame *f;
20590 struct window *w = updated_window;
20591 int max_x, min_y, max_y;
20592 int from_x, from_y, to_y;
20594 xassert (updated_window && updated_row);
20595 f = XFRAME (w->frame);
20597 if (updated_row->full_width_p)
20598 max_x = WINDOW_TOTAL_WIDTH (w);
20599 else
20600 max_x = window_box_width (w, updated_area);
20601 max_y = window_text_bottom_y (w);
20603 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20604 of window. For TO_X > 0, truncate to end of drawing area. */
20605 if (to_x == 0)
20606 return;
20607 else if (to_x < 0)
20608 to_x = max_x;
20609 else
20610 to_x = min (to_x, max_x);
20612 to_y = min (max_y, output_cursor.y + updated_row->height);
20614 /* Notice if the cursor will be cleared by this operation. */
20615 if (!updated_row->full_width_p)
20616 notice_overwritten_cursor (w, updated_area,
20617 output_cursor.x, -1,
20618 updated_row->y,
20619 MATRIX_ROW_BOTTOM_Y (updated_row));
20621 from_x = output_cursor.x;
20623 /* Translate to frame coordinates. */
20624 if (updated_row->full_width_p)
20626 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20627 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20629 else
20631 int area_left = window_box_left (w, updated_area);
20632 from_x += area_left;
20633 to_x += area_left;
20636 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20637 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20638 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20640 /* Prevent inadvertently clearing to end of the X window. */
20641 if (to_x > from_x && to_y > from_y)
20643 BLOCK_INPUT;
20644 rif->clear_frame_area (f, from_x, from_y,
20645 to_x - from_x, to_y - from_y);
20646 UNBLOCK_INPUT;
20650 #endif /* HAVE_WINDOW_SYSTEM */
20654 /***********************************************************************
20655 Cursor types
20656 ***********************************************************************/
20658 /* Value is the internal representation of the specified cursor type
20659 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20660 of the bar cursor. */
20662 static enum text_cursor_kinds
20663 get_specified_cursor_type (arg, width)
20664 Lisp_Object arg;
20665 int *width;
20667 enum text_cursor_kinds type;
20669 if (NILP (arg))
20670 return NO_CURSOR;
20672 if (EQ (arg, Qbox))
20673 return FILLED_BOX_CURSOR;
20675 if (EQ (arg, Qhollow))
20676 return HOLLOW_BOX_CURSOR;
20678 if (EQ (arg, Qbar))
20680 *width = 2;
20681 return BAR_CURSOR;
20684 if (CONSP (arg)
20685 && EQ (XCAR (arg), Qbar)
20686 && INTEGERP (XCDR (arg))
20687 && XINT (XCDR (arg)) >= 0)
20689 *width = XINT (XCDR (arg));
20690 return BAR_CURSOR;
20693 if (EQ (arg, Qhbar))
20695 *width = 2;
20696 return HBAR_CURSOR;
20699 if (CONSP (arg)
20700 && EQ (XCAR (arg), Qhbar)
20701 && INTEGERP (XCDR (arg))
20702 && XINT (XCDR (arg)) >= 0)
20704 *width = XINT (XCDR (arg));
20705 return HBAR_CURSOR;
20708 /* Treat anything unknown as "hollow box cursor".
20709 It was bad to signal an error; people have trouble fixing
20710 .Xdefaults with Emacs, when it has something bad in it. */
20711 type = HOLLOW_BOX_CURSOR;
20713 return type;
20716 /* Set the default cursor types for specified frame. */
20717 void
20718 set_frame_cursor_types (f, arg)
20719 struct frame *f;
20720 Lisp_Object arg;
20722 int width;
20723 Lisp_Object tem;
20725 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20726 FRAME_CURSOR_WIDTH (f) = width;
20728 /* By default, set up the blink-off state depending on the on-state. */
20730 tem = Fassoc (arg, Vblink_cursor_alist);
20731 if (!NILP (tem))
20733 FRAME_BLINK_OFF_CURSOR (f)
20734 = get_specified_cursor_type (XCDR (tem), &width);
20735 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20737 else
20738 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20742 /* Return the cursor we want to be displayed in window W. Return
20743 width of bar/hbar cursor through WIDTH arg. Return with
20744 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20745 (i.e. if the `system caret' should track this cursor).
20747 In a mini-buffer window, we want the cursor only to appear if we
20748 are reading input from this window. For the selected window, we
20749 want the cursor type given by the frame parameter or buffer local
20750 setting of cursor-type. If explicitly marked off, draw no cursor.
20751 In all other cases, we want a hollow box cursor. */
20753 static enum text_cursor_kinds
20754 get_window_cursor_type (w, glyph, width, active_cursor)
20755 struct window *w;
20756 struct glyph *glyph;
20757 int *width;
20758 int *active_cursor;
20760 struct frame *f = XFRAME (w->frame);
20761 struct buffer *b = XBUFFER (w->buffer);
20762 int cursor_type = DEFAULT_CURSOR;
20763 Lisp_Object alt_cursor;
20764 int non_selected = 0;
20766 *active_cursor = 1;
20768 /* Echo area */
20769 if (cursor_in_echo_area
20770 && FRAME_HAS_MINIBUF_P (f)
20771 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20773 if (w == XWINDOW (echo_area_window))
20775 *width = FRAME_CURSOR_WIDTH (f);
20776 return FRAME_DESIRED_CURSOR (f);
20779 *active_cursor = 0;
20780 non_selected = 1;
20783 /* Nonselected window or nonselected frame. */
20784 else if (w != XWINDOW (f->selected_window)
20785 #ifdef HAVE_WINDOW_SYSTEM
20786 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20787 #endif
20790 *active_cursor = 0;
20792 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20793 return NO_CURSOR;
20795 non_selected = 1;
20798 /* Never display a cursor in a window in which cursor-type is nil. */
20799 if (NILP (b->cursor_type))
20800 return NO_CURSOR;
20802 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20803 if (non_selected)
20805 alt_cursor = b->cursor_in_non_selected_windows;
20806 return get_specified_cursor_type (alt_cursor, width);
20809 /* Get the normal cursor type for this window. */
20810 if (EQ (b->cursor_type, Qt))
20812 cursor_type = FRAME_DESIRED_CURSOR (f);
20813 *width = FRAME_CURSOR_WIDTH (f);
20815 else
20816 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20818 /* Use normal cursor if not blinked off. */
20819 if (!w->cursor_off_p)
20821 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20822 if (cursor_type == FILLED_BOX_CURSOR)
20823 cursor_type = HOLLOW_BOX_CURSOR;
20825 return cursor_type;
20828 /* Cursor is blinked off, so determine how to "toggle" it. */
20830 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20831 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20832 return get_specified_cursor_type (XCDR (alt_cursor), width);
20834 /* Then see if frame has specified a specific blink off cursor type. */
20835 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20837 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20838 return FRAME_BLINK_OFF_CURSOR (f);
20841 #if 0
20842 /* Some people liked having a permanently visible blinking cursor,
20843 while others had very strong opinions against it. So it was
20844 decided to remove it. KFS 2003-09-03 */
20846 /* Finally perform built-in cursor blinking:
20847 filled box <-> hollow box
20848 wide [h]bar <-> narrow [h]bar
20849 narrow [h]bar <-> no cursor
20850 other type <-> no cursor */
20852 if (cursor_type == FILLED_BOX_CURSOR)
20853 return HOLLOW_BOX_CURSOR;
20855 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20857 *width = 1;
20858 return cursor_type;
20860 #endif
20862 return NO_CURSOR;
20866 #ifdef HAVE_WINDOW_SYSTEM
20868 /* Notice when the text cursor of window W has been completely
20869 overwritten by a drawing operation that outputs glyphs in AREA
20870 starting at X0 and ending at X1 in the line starting at Y0 and
20871 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20872 the rest of the line after X0 has been written. Y coordinates
20873 are window-relative. */
20875 static void
20876 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20877 struct window *w;
20878 enum glyph_row_area area;
20879 int x0, y0, x1, y1;
20881 int cx0, cx1, cy0, cy1;
20882 struct glyph_row *row;
20884 if (!w->phys_cursor_on_p)
20885 return;
20886 if (area != TEXT_AREA)
20887 return;
20889 if (w->phys_cursor.vpos < 0
20890 || w->phys_cursor.vpos >= w->current_matrix->nrows
20891 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20892 !(row->enabled_p && row->displays_text_p)))
20893 return;
20895 if (row->cursor_in_fringe_p)
20897 row->cursor_in_fringe_p = 0;
20898 draw_fringe_bitmap (w, row, 0);
20899 w->phys_cursor_on_p = 0;
20900 return;
20903 cx0 = w->phys_cursor.x;
20904 cx1 = cx0 + w->phys_cursor_width;
20905 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20906 return;
20908 /* The cursor image will be completely removed from the
20909 screen if the output area intersects the cursor area in
20910 y-direction. When we draw in [y0 y1[, and some part of
20911 the cursor is at y < y0, that part must have been drawn
20912 before. When scrolling, the cursor is erased before
20913 actually scrolling, so we don't come here. When not
20914 scrolling, the rows above the old cursor row must have
20915 changed, and in this case these rows must have written
20916 over the cursor image.
20918 Likewise if part of the cursor is below y1, with the
20919 exception of the cursor being in the first blank row at
20920 the buffer and window end because update_text_area
20921 doesn't draw that row. (Except when it does, but
20922 that's handled in update_text_area.) */
20924 cy0 = w->phys_cursor.y;
20925 cy1 = cy0 + w->phys_cursor_height;
20926 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20927 return;
20929 w->phys_cursor_on_p = 0;
20932 #endif /* HAVE_WINDOW_SYSTEM */
20935 /************************************************************************
20936 Mouse Face
20937 ************************************************************************/
20939 #ifdef HAVE_WINDOW_SYSTEM
20941 /* EXPORT for RIF:
20942 Fix the display of area AREA of overlapping row ROW in window W
20943 with respect to the overlapping part OVERLAPS. */
20945 void
20946 x_fix_overlapping_area (w, row, area, overlaps)
20947 struct window *w;
20948 struct glyph_row *row;
20949 enum glyph_row_area area;
20950 int overlaps;
20952 int i, x;
20954 BLOCK_INPUT;
20956 x = 0;
20957 for (i = 0; i < row->used[area];)
20959 if (row->glyphs[area][i].overlaps_vertically_p)
20961 int start = i, start_x = x;
20965 x += row->glyphs[area][i].pixel_width;
20966 ++i;
20968 while (i < row->used[area]
20969 && row->glyphs[area][i].overlaps_vertically_p);
20971 draw_glyphs (w, start_x, row, area,
20972 start, i,
20973 DRAW_NORMAL_TEXT, overlaps);
20975 else
20977 x += row->glyphs[area][i].pixel_width;
20978 ++i;
20982 UNBLOCK_INPUT;
20986 /* EXPORT:
20987 Draw the cursor glyph of window W in glyph row ROW. See the
20988 comment of draw_glyphs for the meaning of HL. */
20990 void
20991 draw_phys_cursor_glyph (w, row, hl)
20992 struct window *w;
20993 struct glyph_row *row;
20994 enum draw_glyphs_face hl;
20996 /* If cursor hpos is out of bounds, don't draw garbage. This can
20997 happen in mini-buffer windows when switching between echo area
20998 glyphs and mini-buffer. */
20999 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21001 int on_p = w->phys_cursor_on_p;
21002 int x1;
21003 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21004 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21005 hl, 0);
21006 w->phys_cursor_on_p = on_p;
21008 if (hl == DRAW_CURSOR)
21009 w->phys_cursor_width = x1 - w->phys_cursor.x;
21010 /* When we erase the cursor, and ROW is overlapped by other
21011 rows, make sure that these overlapping parts of other rows
21012 are redrawn. */
21013 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21015 w->phys_cursor_width = x1 - w->phys_cursor.x;
21017 if (row > w->current_matrix->rows
21018 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21019 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21020 OVERLAPS_ERASED_CURSOR);
21022 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21023 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21024 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21025 OVERLAPS_ERASED_CURSOR);
21031 /* EXPORT:
21032 Erase the image of a cursor of window W from the screen. */
21034 void
21035 erase_phys_cursor (w)
21036 struct window *w;
21038 struct frame *f = XFRAME (w->frame);
21039 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21040 int hpos = w->phys_cursor.hpos;
21041 int vpos = w->phys_cursor.vpos;
21042 int mouse_face_here_p = 0;
21043 struct glyph_matrix *active_glyphs = w->current_matrix;
21044 struct glyph_row *cursor_row;
21045 struct glyph *cursor_glyph;
21046 enum draw_glyphs_face hl;
21048 /* No cursor displayed or row invalidated => nothing to do on the
21049 screen. */
21050 if (w->phys_cursor_type == NO_CURSOR)
21051 goto mark_cursor_off;
21053 /* VPOS >= active_glyphs->nrows means that window has been resized.
21054 Don't bother to erase the cursor. */
21055 if (vpos >= active_glyphs->nrows)
21056 goto mark_cursor_off;
21058 /* If row containing cursor is marked invalid, there is nothing we
21059 can do. */
21060 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21061 if (!cursor_row->enabled_p)
21062 goto mark_cursor_off;
21064 /* If line spacing is > 0, old cursor may only be partially visible in
21065 window after split-window. So adjust visible height. */
21066 cursor_row->visible_height = min (cursor_row->visible_height,
21067 window_text_bottom_y (w) - cursor_row->y);
21069 /* If row is completely invisible, don't attempt to delete a cursor which
21070 isn't there. This can happen if cursor is at top of a window, and
21071 we switch to a buffer with a header line in that window. */
21072 if (cursor_row->visible_height <= 0)
21073 goto mark_cursor_off;
21075 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21076 if (cursor_row->cursor_in_fringe_p)
21078 cursor_row->cursor_in_fringe_p = 0;
21079 draw_fringe_bitmap (w, cursor_row, 0);
21080 goto mark_cursor_off;
21083 /* This can happen when the new row is shorter than the old one.
21084 In this case, either draw_glyphs or clear_end_of_line
21085 should have cleared the cursor. Note that we wouldn't be
21086 able to erase the cursor in this case because we don't have a
21087 cursor glyph at hand. */
21088 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21089 goto mark_cursor_off;
21091 /* If the cursor is in the mouse face area, redisplay that when
21092 we clear the cursor. */
21093 if (! NILP (dpyinfo->mouse_face_window)
21094 && w == XWINDOW (dpyinfo->mouse_face_window)
21095 && (vpos > dpyinfo->mouse_face_beg_row
21096 || (vpos == dpyinfo->mouse_face_beg_row
21097 && hpos >= dpyinfo->mouse_face_beg_col))
21098 && (vpos < dpyinfo->mouse_face_end_row
21099 || (vpos == dpyinfo->mouse_face_end_row
21100 && hpos < dpyinfo->mouse_face_end_col))
21101 /* Don't redraw the cursor's spot in mouse face if it is at the
21102 end of a line (on a newline). The cursor appears there, but
21103 mouse highlighting does not. */
21104 && cursor_row->used[TEXT_AREA] > hpos)
21105 mouse_face_here_p = 1;
21107 /* Maybe clear the display under the cursor. */
21108 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21110 int x, y;
21111 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21112 int width;
21114 cursor_glyph = get_phys_cursor_glyph (w);
21115 if (cursor_glyph == NULL)
21116 goto mark_cursor_off;
21118 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21119 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21120 width = min (cursor_glyph->pixel_width,
21121 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21123 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21126 /* Erase the cursor by redrawing the character underneath it. */
21127 if (mouse_face_here_p)
21128 hl = DRAW_MOUSE_FACE;
21129 else
21130 hl = DRAW_NORMAL_TEXT;
21131 draw_phys_cursor_glyph (w, cursor_row, hl);
21133 mark_cursor_off:
21134 w->phys_cursor_on_p = 0;
21135 w->phys_cursor_type = NO_CURSOR;
21139 /* EXPORT:
21140 Display or clear cursor of window W. If ON is zero, clear the
21141 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21142 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21144 void
21145 display_and_set_cursor (w, on, hpos, vpos, x, y)
21146 struct window *w;
21147 int on, hpos, vpos, x, y;
21149 struct frame *f = XFRAME (w->frame);
21150 int new_cursor_type;
21151 int new_cursor_width;
21152 int active_cursor;
21153 struct glyph_row *glyph_row;
21154 struct glyph *glyph;
21156 /* This is pointless on invisible frames, and dangerous on garbaged
21157 windows and frames; in the latter case, the frame or window may
21158 be in the midst of changing its size, and x and y may be off the
21159 window. */
21160 if (! FRAME_VISIBLE_P (f)
21161 || FRAME_GARBAGED_P (f)
21162 || vpos >= w->current_matrix->nrows
21163 || hpos >= w->current_matrix->matrix_w)
21164 return;
21166 /* If cursor is off and we want it off, return quickly. */
21167 if (!on && !w->phys_cursor_on_p)
21168 return;
21170 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21171 /* If cursor row is not enabled, we don't really know where to
21172 display the cursor. */
21173 if (!glyph_row->enabled_p)
21175 w->phys_cursor_on_p = 0;
21176 return;
21179 glyph = NULL;
21180 if (!glyph_row->exact_window_width_line_p
21181 || hpos < glyph_row->used[TEXT_AREA])
21182 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21184 xassert (interrupt_input_blocked);
21186 /* Set new_cursor_type to the cursor we want to be displayed. */
21187 new_cursor_type = get_window_cursor_type (w, glyph,
21188 &new_cursor_width, &active_cursor);
21190 /* If cursor is currently being shown and we don't want it to be or
21191 it is in the wrong place, or the cursor type is not what we want,
21192 erase it. */
21193 if (w->phys_cursor_on_p
21194 && (!on
21195 || w->phys_cursor.x != x
21196 || w->phys_cursor.y != y
21197 || new_cursor_type != w->phys_cursor_type
21198 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21199 && new_cursor_width != w->phys_cursor_width)))
21200 erase_phys_cursor (w);
21202 /* Don't check phys_cursor_on_p here because that flag is only set
21203 to zero in some cases where we know that the cursor has been
21204 completely erased, to avoid the extra work of erasing the cursor
21205 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21206 still not be visible, or it has only been partly erased. */
21207 if (on)
21209 w->phys_cursor_ascent = glyph_row->ascent;
21210 w->phys_cursor_height = glyph_row->height;
21212 /* Set phys_cursor_.* before x_draw_.* is called because some
21213 of them may need the information. */
21214 w->phys_cursor.x = x;
21215 w->phys_cursor.y = glyph_row->y;
21216 w->phys_cursor.hpos = hpos;
21217 w->phys_cursor.vpos = vpos;
21220 rif->draw_window_cursor (w, glyph_row, x, y,
21221 new_cursor_type, new_cursor_width,
21222 on, active_cursor);
21226 /* Switch the display of W's cursor on or off, according to the value
21227 of ON. */
21229 static void
21230 update_window_cursor (w, on)
21231 struct window *w;
21232 int on;
21234 /* Don't update cursor in windows whose frame is in the process
21235 of being deleted. */
21236 if (w->current_matrix)
21238 BLOCK_INPUT;
21239 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21240 w->phys_cursor.x, w->phys_cursor.y);
21241 UNBLOCK_INPUT;
21246 /* Call update_window_cursor with parameter ON_P on all leaf windows
21247 in the window tree rooted at W. */
21249 static void
21250 update_cursor_in_window_tree (w, on_p)
21251 struct window *w;
21252 int on_p;
21254 while (w)
21256 if (!NILP (w->hchild))
21257 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21258 else if (!NILP (w->vchild))
21259 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21260 else
21261 update_window_cursor (w, on_p);
21263 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21268 /* EXPORT:
21269 Display the cursor on window W, or clear it, according to ON_P.
21270 Don't change the cursor's position. */
21272 void
21273 x_update_cursor (f, on_p)
21274 struct frame *f;
21275 int on_p;
21277 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21281 /* EXPORT:
21282 Clear the cursor of window W to background color, and mark the
21283 cursor as not shown. This is used when the text where the cursor
21284 is is about to be rewritten. */
21286 void
21287 x_clear_cursor (w)
21288 struct window *w;
21290 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21291 update_window_cursor (w, 0);
21295 /* EXPORT:
21296 Display the active region described by mouse_face_* according to DRAW. */
21298 void
21299 show_mouse_face (dpyinfo, draw)
21300 Display_Info *dpyinfo;
21301 enum draw_glyphs_face draw;
21303 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21304 struct frame *f = XFRAME (WINDOW_FRAME (w));
21306 if (/* If window is in the process of being destroyed, don't bother
21307 to do anything. */
21308 w->current_matrix != NULL
21309 /* Don't update mouse highlight if hidden */
21310 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21311 /* Recognize when we are called to operate on rows that don't exist
21312 anymore. This can happen when a window is split. */
21313 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21315 int phys_cursor_on_p = w->phys_cursor_on_p;
21316 struct glyph_row *row, *first, *last;
21318 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21319 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21321 for (row = first; row <= last && row->enabled_p; ++row)
21323 int start_hpos, end_hpos, start_x;
21325 /* For all but the first row, the highlight starts at column 0. */
21326 if (row == first)
21328 start_hpos = dpyinfo->mouse_face_beg_col;
21329 start_x = dpyinfo->mouse_face_beg_x;
21331 else
21333 start_hpos = 0;
21334 start_x = 0;
21337 if (row == last)
21338 end_hpos = dpyinfo->mouse_face_end_col;
21339 else
21341 end_hpos = row->used[TEXT_AREA];
21342 if (draw == DRAW_NORMAL_TEXT)
21343 row->fill_line_p = 1; /* Clear to end of line */
21346 if (end_hpos > start_hpos)
21348 draw_glyphs (w, start_x, row, TEXT_AREA,
21349 start_hpos, end_hpos,
21350 draw, 0);
21352 row->mouse_face_p
21353 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21357 /* When we've written over the cursor, arrange for it to
21358 be displayed again. */
21359 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21361 BLOCK_INPUT;
21362 display_and_set_cursor (w, 1,
21363 w->phys_cursor.hpos, w->phys_cursor.vpos,
21364 w->phys_cursor.x, w->phys_cursor.y);
21365 UNBLOCK_INPUT;
21369 /* Change the mouse cursor. */
21370 if (draw == DRAW_NORMAL_TEXT)
21371 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21372 else if (draw == DRAW_MOUSE_FACE)
21373 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21374 else
21375 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21378 /* EXPORT:
21379 Clear out the mouse-highlighted active region.
21380 Redraw it un-highlighted first. Value is non-zero if mouse
21381 face was actually drawn unhighlighted. */
21384 clear_mouse_face (dpyinfo)
21385 Display_Info *dpyinfo;
21387 int cleared = 0;
21389 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21391 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21392 cleared = 1;
21395 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21396 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21397 dpyinfo->mouse_face_window = Qnil;
21398 dpyinfo->mouse_face_overlay = Qnil;
21399 return cleared;
21403 /* EXPORT:
21404 Non-zero if physical cursor of window W is within mouse face. */
21407 cursor_in_mouse_face_p (w)
21408 struct window *w;
21410 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21411 int in_mouse_face = 0;
21413 if (WINDOWP (dpyinfo->mouse_face_window)
21414 && XWINDOW (dpyinfo->mouse_face_window) == w)
21416 int hpos = w->phys_cursor.hpos;
21417 int vpos = w->phys_cursor.vpos;
21419 if (vpos >= dpyinfo->mouse_face_beg_row
21420 && vpos <= dpyinfo->mouse_face_end_row
21421 && (vpos > dpyinfo->mouse_face_beg_row
21422 || hpos >= dpyinfo->mouse_face_beg_col)
21423 && (vpos < dpyinfo->mouse_face_end_row
21424 || hpos < dpyinfo->mouse_face_end_col
21425 || dpyinfo->mouse_face_past_end))
21426 in_mouse_face = 1;
21429 return in_mouse_face;
21435 /* Find the glyph matrix position of buffer position CHARPOS in window
21436 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21437 current glyphs must be up to date. If CHARPOS is above window
21438 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21439 of last line in W. In the row containing CHARPOS, stop before glyphs
21440 having STOP as object. */
21442 #if 1 /* This is a version of fast_find_position that's more correct
21443 in the presence of hscrolling, for example. I didn't install
21444 it right away because the problem fixed is minor, it failed
21445 in 20.x as well, and I think it's too risky to install
21446 so near the release of 21.1. 2001-09-25 gerd. */
21448 static int
21449 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21450 struct window *w;
21451 int charpos;
21452 int *hpos, *vpos, *x, *y;
21453 Lisp_Object stop;
21455 struct glyph_row *row, *first;
21456 struct glyph *glyph, *end;
21457 int past_end = 0;
21459 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21460 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21462 *x = first->x;
21463 *y = first->y;
21464 *hpos = 0;
21465 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21466 return 1;
21469 row = row_containing_pos (w, charpos, first, NULL, 0);
21470 if (row == NULL)
21472 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21473 past_end = 1;
21476 /* If whole rows or last part of a row came from a display overlay,
21477 row_containing_pos will skip over such rows because their end pos
21478 equals the start pos of the overlay or interval.
21480 Move back if we have a STOP object and previous row's
21481 end glyph came from STOP. */
21482 if (!NILP (stop))
21484 struct glyph_row *prev;
21485 while ((prev = row - 1, prev >= first)
21486 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21487 && prev->used[TEXT_AREA] > 0)
21489 struct glyph *beg = prev->glyphs[TEXT_AREA];
21490 glyph = beg + prev->used[TEXT_AREA];
21491 while (--glyph >= beg
21492 && INTEGERP (glyph->object));
21493 if (glyph < beg
21494 || !EQ (stop, glyph->object))
21495 break;
21496 row = prev;
21500 *x = row->x;
21501 *y = row->y;
21502 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21504 glyph = row->glyphs[TEXT_AREA];
21505 end = glyph + row->used[TEXT_AREA];
21507 /* Skip over glyphs not having an object at the start of the row.
21508 These are special glyphs like truncation marks on terminal
21509 frames. */
21510 if (row->displays_text_p)
21511 while (glyph < end
21512 && INTEGERP (glyph->object)
21513 && !EQ (stop, glyph->object)
21514 && glyph->charpos < 0)
21516 *x += glyph->pixel_width;
21517 ++glyph;
21520 while (glyph < end
21521 && !INTEGERP (glyph->object)
21522 && !EQ (stop, glyph->object)
21523 && (!BUFFERP (glyph->object)
21524 || glyph->charpos < charpos))
21526 *x += glyph->pixel_width;
21527 ++glyph;
21530 *hpos = glyph - row->glyphs[TEXT_AREA];
21531 return !past_end;
21534 #else /* not 1 */
21536 static int
21537 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21538 struct window *w;
21539 int pos;
21540 int *hpos, *vpos, *x, *y;
21541 Lisp_Object stop;
21543 int i;
21544 int lastcol;
21545 int maybe_next_line_p = 0;
21546 int line_start_position;
21547 int yb = window_text_bottom_y (w);
21548 struct glyph_row *row, *best_row;
21549 int row_vpos, best_row_vpos;
21550 int current_x;
21552 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21553 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21555 while (row->y < yb)
21557 if (row->used[TEXT_AREA])
21558 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21559 else
21560 line_start_position = 0;
21562 if (line_start_position > pos)
21563 break;
21564 /* If the position sought is the end of the buffer,
21565 don't include the blank lines at the bottom of the window. */
21566 else if (line_start_position == pos
21567 && pos == BUF_ZV (XBUFFER (w->buffer)))
21569 maybe_next_line_p = 1;
21570 break;
21572 else if (line_start_position > 0)
21574 best_row = row;
21575 best_row_vpos = row_vpos;
21578 if (row->y + row->height >= yb)
21579 break;
21581 ++row;
21582 ++row_vpos;
21585 /* Find the right column within BEST_ROW. */
21586 lastcol = 0;
21587 current_x = best_row->x;
21588 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21590 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21591 int charpos = glyph->charpos;
21593 if (BUFFERP (glyph->object))
21595 if (charpos == pos)
21597 *hpos = i;
21598 *vpos = best_row_vpos;
21599 *x = current_x;
21600 *y = best_row->y;
21601 return 1;
21603 else if (charpos > pos)
21604 break;
21606 else if (EQ (glyph->object, stop))
21607 break;
21609 if (charpos > 0)
21610 lastcol = i;
21611 current_x += glyph->pixel_width;
21614 /* If we're looking for the end of the buffer,
21615 and we didn't find it in the line we scanned,
21616 use the start of the following line. */
21617 if (maybe_next_line_p)
21619 ++best_row;
21620 ++best_row_vpos;
21621 lastcol = 0;
21622 current_x = best_row->x;
21625 *vpos = best_row_vpos;
21626 *hpos = lastcol + 1;
21627 *x = current_x;
21628 *y = best_row->y;
21629 return 0;
21632 #endif /* not 1 */
21635 /* Find the position of the glyph for position POS in OBJECT in
21636 window W's current matrix, and return in *X, *Y the pixel
21637 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21639 RIGHT_P non-zero means return the position of the right edge of the
21640 glyph, RIGHT_P zero means return the left edge position.
21642 If no glyph for POS exists in the matrix, return the position of
21643 the glyph with the next smaller position that is in the matrix, if
21644 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21645 exists in the matrix, return the position of the glyph with the
21646 next larger position in OBJECT.
21648 Value is non-zero if a glyph was found. */
21650 static int
21651 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21652 struct window *w;
21653 int pos;
21654 Lisp_Object object;
21655 int *hpos, *vpos, *x, *y;
21656 int right_p;
21658 int yb = window_text_bottom_y (w);
21659 struct glyph_row *r;
21660 struct glyph *best_glyph = NULL;
21661 struct glyph_row *best_row = NULL;
21662 int best_x = 0;
21664 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21665 r->enabled_p && r->y < yb;
21666 ++r)
21668 struct glyph *g = r->glyphs[TEXT_AREA];
21669 struct glyph *e = g + r->used[TEXT_AREA];
21670 int gx;
21672 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21673 if (EQ (g->object, object))
21675 if (g->charpos == pos)
21677 best_glyph = g;
21678 best_x = gx;
21679 best_row = r;
21680 goto found;
21682 else if (best_glyph == NULL
21683 || ((abs (g->charpos - pos)
21684 < abs (best_glyph->charpos - pos))
21685 && (right_p
21686 ? g->charpos < pos
21687 : g->charpos > pos)))
21689 best_glyph = g;
21690 best_x = gx;
21691 best_row = r;
21696 found:
21698 if (best_glyph)
21700 *x = best_x;
21701 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21703 if (right_p)
21705 *x += best_glyph->pixel_width;
21706 ++*hpos;
21709 *y = best_row->y;
21710 *vpos = best_row - w->current_matrix->rows;
21713 return best_glyph != NULL;
21717 /* See if position X, Y is within a hot-spot of an image. */
21719 static int
21720 on_hot_spot_p (hot_spot, x, y)
21721 Lisp_Object hot_spot;
21722 int x, y;
21724 if (!CONSP (hot_spot))
21725 return 0;
21727 if (EQ (XCAR (hot_spot), Qrect))
21729 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21730 Lisp_Object rect = XCDR (hot_spot);
21731 Lisp_Object tem;
21732 if (!CONSP (rect))
21733 return 0;
21734 if (!CONSP (XCAR (rect)))
21735 return 0;
21736 if (!CONSP (XCDR (rect)))
21737 return 0;
21738 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21739 return 0;
21740 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21741 return 0;
21742 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21743 return 0;
21744 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21745 return 0;
21746 return 1;
21748 else if (EQ (XCAR (hot_spot), Qcircle))
21750 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21751 Lisp_Object circ = XCDR (hot_spot);
21752 Lisp_Object lr, lx0, ly0;
21753 if (CONSP (circ)
21754 && CONSP (XCAR (circ))
21755 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21756 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21757 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21759 double r = XFLOATINT (lr);
21760 double dx = XINT (lx0) - x;
21761 double dy = XINT (ly0) - y;
21762 return (dx * dx + dy * dy <= r * r);
21765 else if (EQ (XCAR (hot_spot), Qpoly))
21767 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21768 if (VECTORP (XCDR (hot_spot)))
21770 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21771 Lisp_Object *poly = v->contents;
21772 int n = v->size;
21773 int i;
21774 int inside = 0;
21775 Lisp_Object lx, ly;
21776 int x0, y0;
21778 /* Need an even number of coordinates, and at least 3 edges. */
21779 if (n < 6 || n & 1)
21780 return 0;
21782 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21783 If count is odd, we are inside polygon. Pixels on edges
21784 may or may not be included depending on actual geometry of the
21785 polygon. */
21786 if ((lx = poly[n-2], !INTEGERP (lx))
21787 || (ly = poly[n-1], !INTEGERP (lx)))
21788 return 0;
21789 x0 = XINT (lx), y0 = XINT (ly);
21790 for (i = 0; i < n; i += 2)
21792 int x1 = x0, y1 = y0;
21793 if ((lx = poly[i], !INTEGERP (lx))
21794 || (ly = poly[i+1], !INTEGERP (ly)))
21795 return 0;
21796 x0 = XINT (lx), y0 = XINT (ly);
21798 /* Does this segment cross the X line? */
21799 if (x0 >= x)
21801 if (x1 >= x)
21802 continue;
21804 else if (x1 < x)
21805 continue;
21806 if (y > y0 && y > y1)
21807 continue;
21808 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21809 inside = !inside;
21811 return inside;
21814 /* If we don't understand the format, pretend we're not in the hot-spot. */
21815 return 0;
21818 Lisp_Object
21819 find_hot_spot (map, x, y)
21820 Lisp_Object map;
21821 int x, y;
21823 while (CONSP (map))
21825 if (CONSP (XCAR (map))
21826 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21827 return XCAR (map);
21828 map = XCDR (map);
21831 return Qnil;
21834 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21835 3, 3, 0,
21836 doc: /* Lookup in image map MAP coordinates X and Y.
21837 An image map is an alist where each element has the format (AREA ID PLIST).
21838 An AREA is specified as either a rectangle, a circle, or a polygon:
21839 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21840 pixel coordinates of the upper left and bottom right corners.
21841 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21842 and the radius of the circle; r may be a float or integer.
21843 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21844 vector describes one corner in the polygon.
21845 Returns the alist element for the first matching AREA in MAP. */)
21846 (map, x, y)
21847 Lisp_Object map;
21848 Lisp_Object x, y;
21850 if (NILP (map))
21851 return Qnil;
21853 CHECK_NUMBER (x);
21854 CHECK_NUMBER (y);
21856 return find_hot_spot (map, XINT (x), XINT (y));
21860 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21861 static void
21862 define_frame_cursor1 (f, cursor, pointer)
21863 struct frame *f;
21864 Cursor cursor;
21865 Lisp_Object pointer;
21867 /* Do not change cursor shape while dragging mouse. */
21868 if (!NILP (do_mouse_tracking))
21869 return;
21871 if (!NILP (pointer))
21873 if (EQ (pointer, Qarrow))
21874 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21875 else if (EQ (pointer, Qhand))
21876 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21877 else if (EQ (pointer, Qtext))
21878 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21879 else if (EQ (pointer, intern ("hdrag")))
21880 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21881 #ifdef HAVE_X_WINDOWS
21882 else if (EQ (pointer, intern ("vdrag")))
21883 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21884 #endif
21885 else if (EQ (pointer, intern ("hourglass")))
21886 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21887 else if (EQ (pointer, Qmodeline))
21888 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21889 else
21890 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21893 if (cursor != No_Cursor)
21894 rif->define_frame_cursor (f, cursor);
21897 /* Take proper action when mouse has moved to the mode or header line
21898 or marginal area AREA of window W, x-position X and y-position Y.
21899 X is relative to the start of the text display area of W, so the
21900 width of bitmap areas and scroll bars must be subtracted to get a
21901 position relative to the start of the mode line. */
21903 static void
21904 note_mode_line_or_margin_highlight (window, x, y, area)
21905 Lisp_Object window;
21906 int x, y;
21907 enum window_part area;
21909 struct window *w = XWINDOW (window);
21910 struct frame *f = XFRAME (w->frame);
21911 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21912 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21913 Lisp_Object pointer = Qnil;
21914 int charpos, dx, dy, width, height;
21915 Lisp_Object string, object = Qnil;
21916 Lisp_Object pos, help;
21918 Lisp_Object mouse_face;
21919 int original_x_pixel = x;
21920 struct glyph * glyph = NULL;
21921 struct glyph_row *row;
21923 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21925 int x0;
21926 struct glyph *end;
21928 string = mode_line_string (w, area, &x, &y, &charpos,
21929 &object, &dx, &dy, &width, &height);
21931 row = (area == ON_MODE_LINE
21932 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21933 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21935 /* Find glyph */
21936 if (row->mode_line_p && row->enabled_p)
21938 glyph = row->glyphs[TEXT_AREA];
21939 end = glyph + row->used[TEXT_AREA];
21941 for (x0 = original_x_pixel;
21942 glyph < end && x0 >= glyph->pixel_width;
21943 ++glyph)
21944 x0 -= glyph->pixel_width;
21946 if (glyph >= end)
21947 glyph = NULL;
21950 else
21952 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21953 string = marginal_area_string (w, area, &x, &y, &charpos,
21954 &object, &dx, &dy, &width, &height);
21957 help = Qnil;
21959 if (IMAGEP (object))
21961 Lisp_Object image_map, hotspot;
21962 if ((image_map = Fplist_get (XCDR (object), QCmap),
21963 !NILP (image_map))
21964 && (hotspot = find_hot_spot (image_map, dx, dy),
21965 CONSP (hotspot))
21966 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21968 Lisp_Object area_id, plist;
21970 area_id = XCAR (hotspot);
21971 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21972 If so, we could look for mouse-enter, mouse-leave
21973 properties in PLIST (and do something...). */
21974 hotspot = XCDR (hotspot);
21975 if (CONSP (hotspot)
21976 && (plist = XCAR (hotspot), CONSP (plist)))
21978 pointer = Fplist_get (plist, Qpointer);
21979 if (NILP (pointer))
21980 pointer = Qhand;
21981 help = Fplist_get (plist, Qhelp_echo);
21982 if (!NILP (help))
21984 help_echo_string = help;
21985 /* Is this correct? ++kfs */
21986 XSETWINDOW (help_echo_window, w);
21987 help_echo_object = w->buffer;
21988 help_echo_pos = charpos;
21992 if (NILP (pointer))
21993 pointer = Fplist_get (XCDR (object), QCpointer);
21996 if (STRINGP (string))
21998 pos = make_number (charpos);
21999 /* If we're on a string with `help-echo' text property, arrange
22000 for the help to be displayed. This is done by setting the
22001 global variable help_echo_string to the help string. */
22002 if (NILP (help))
22004 help = Fget_text_property (pos, Qhelp_echo, string);
22005 if (!NILP (help))
22007 help_echo_string = help;
22008 XSETWINDOW (help_echo_window, w);
22009 help_echo_object = string;
22010 help_echo_pos = charpos;
22014 if (NILP (pointer))
22015 pointer = Fget_text_property (pos, Qpointer, string);
22017 /* Change the mouse pointer according to what is under X/Y. */
22018 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22020 Lisp_Object map;
22021 map = Fget_text_property (pos, Qlocal_map, string);
22022 if (!KEYMAPP (map))
22023 map = Fget_text_property (pos, Qkeymap, string);
22024 if (!KEYMAPP (map))
22025 cursor = dpyinfo->vertical_scroll_bar_cursor;
22028 /* Change the mouse face according to what is under X/Y. */
22029 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22030 if (!NILP (mouse_face)
22031 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22032 && glyph)
22034 Lisp_Object b, e;
22036 struct glyph * tmp_glyph;
22038 int gpos;
22039 int gseq_length;
22040 int total_pixel_width;
22041 int ignore;
22043 int vpos, hpos;
22045 b = Fprevious_single_property_change (make_number (charpos + 1),
22046 Qmouse_face, string, Qnil);
22047 if (NILP (b))
22048 b = make_number (0);
22050 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22051 if (NILP (e))
22052 e = make_number (SCHARS (string));
22054 /* Calculate the position(glyph position: GPOS) of GLYPH in
22055 displayed string. GPOS is different from CHARPOS.
22057 CHARPOS is the position of glyph in internal string
22058 object. A mode line string format has structures which
22059 is converted to a flatten by emacs lisp interpreter.
22060 The internal string is an element of the structures.
22061 The displayed string is the flatten string. */
22062 for (tmp_glyph = glyph - 1, gpos = 0;
22063 tmp_glyph->charpos >= XINT (b);
22064 tmp_glyph--, gpos++)
22066 if (!EQ (tmp_glyph->object, glyph->object))
22067 break;
22070 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22071 displayed string holding GLYPH.
22073 GSEQ_LENGTH is different from SCHARS (STRING).
22074 SCHARS (STRING) returns the length of the internal string. */
22075 for (tmp_glyph = glyph, gseq_length = gpos;
22076 tmp_glyph->charpos < XINT (e);
22077 tmp_glyph++, gseq_length++)
22079 if (!EQ (tmp_glyph->object, glyph->object))
22080 break;
22083 total_pixel_width = 0;
22084 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22085 total_pixel_width += tmp_glyph->pixel_width;
22087 /* Pre calculation of re-rendering position */
22088 vpos = (x - gpos);
22089 hpos = (area == ON_MODE_LINE
22090 ? (w->current_matrix)->nrows - 1
22091 : 0);
22093 /* If the re-rendering position is included in the last
22094 re-rendering area, we should do nothing. */
22095 if ( EQ (window, dpyinfo->mouse_face_window)
22096 && dpyinfo->mouse_face_beg_col <= vpos
22097 && vpos < dpyinfo->mouse_face_end_col
22098 && dpyinfo->mouse_face_beg_row == hpos )
22099 return;
22101 if (clear_mouse_face (dpyinfo))
22102 cursor = No_Cursor;
22104 dpyinfo->mouse_face_beg_col = vpos;
22105 dpyinfo->mouse_face_beg_row = hpos;
22107 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22108 dpyinfo->mouse_face_beg_y = 0;
22110 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22111 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22113 dpyinfo->mouse_face_end_x = 0;
22114 dpyinfo->mouse_face_end_y = 0;
22116 dpyinfo->mouse_face_past_end = 0;
22117 dpyinfo->mouse_face_window = window;
22119 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22120 charpos,
22121 0, 0, 0, &ignore,
22122 glyph->face_id, 1);
22123 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22125 if (NILP (pointer))
22126 pointer = Qhand;
22128 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22129 clear_mouse_face (dpyinfo);
22131 define_frame_cursor1 (f, cursor, pointer);
22135 /* EXPORT:
22136 Take proper action when the mouse has moved to position X, Y on
22137 frame F as regards highlighting characters that have mouse-face
22138 properties. Also de-highlighting chars where the mouse was before.
22139 X and Y can be negative or out of range. */
22141 void
22142 note_mouse_highlight (f, x, y)
22143 struct frame *f;
22144 int x, y;
22146 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22147 enum window_part part;
22148 Lisp_Object window;
22149 struct window *w;
22150 Cursor cursor = No_Cursor;
22151 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22152 struct buffer *b;
22154 /* When a menu is active, don't highlight because this looks odd. */
22155 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22156 if (popup_activated ())
22157 return;
22158 #endif
22160 if (NILP (Vmouse_highlight)
22161 || !f->glyphs_initialized_p)
22162 return;
22164 dpyinfo->mouse_face_mouse_x = x;
22165 dpyinfo->mouse_face_mouse_y = y;
22166 dpyinfo->mouse_face_mouse_frame = f;
22168 if (dpyinfo->mouse_face_defer)
22169 return;
22171 if (gc_in_progress)
22173 dpyinfo->mouse_face_deferred_gc = 1;
22174 return;
22177 /* Which window is that in? */
22178 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22180 /* If we were displaying active text in another window, clear that.
22181 Also clear if we move out of text area in same window. */
22182 if (! EQ (window, dpyinfo->mouse_face_window)
22183 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22184 && !NILP (dpyinfo->mouse_face_window)))
22185 clear_mouse_face (dpyinfo);
22187 /* Not on a window -> return. */
22188 if (!WINDOWP (window))
22189 return;
22191 /* Reset help_echo_string. It will get recomputed below. */
22192 help_echo_string = Qnil;
22194 /* Convert to window-relative pixel coordinates. */
22195 w = XWINDOW (window);
22196 frame_to_window_pixel_xy (w, &x, &y);
22198 /* Handle tool-bar window differently since it doesn't display a
22199 buffer. */
22200 if (EQ (window, f->tool_bar_window))
22202 note_tool_bar_highlight (f, x, y);
22203 return;
22206 /* Mouse is on the mode, header line or margin? */
22207 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22208 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22210 note_mode_line_or_margin_highlight (window, x, y, part);
22211 return;
22214 if (part == ON_VERTICAL_BORDER)
22215 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22216 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22217 || part == ON_SCROLL_BAR)
22218 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22219 else
22220 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22222 /* Are we in a window whose display is up to date?
22223 And verify the buffer's text has not changed. */
22224 b = XBUFFER (w->buffer);
22225 if (part == ON_TEXT
22226 && EQ (w->window_end_valid, w->buffer)
22227 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22228 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22230 int hpos, vpos, pos, i, dx, dy, area;
22231 struct glyph *glyph;
22232 Lisp_Object object;
22233 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22234 Lisp_Object *overlay_vec = NULL;
22235 int noverlays;
22236 struct buffer *obuf;
22237 int obegv, ozv, same_region;
22239 /* Find the glyph under X/Y. */
22240 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22242 /* Look for :pointer property on image. */
22243 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22245 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22246 if (img != NULL && IMAGEP (img->spec))
22248 Lisp_Object image_map, hotspot;
22249 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22250 !NILP (image_map))
22251 && (hotspot = find_hot_spot (image_map,
22252 glyph->slice.x + dx,
22253 glyph->slice.y + dy),
22254 CONSP (hotspot))
22255 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22257 Lisp_Object area_id, plist;
22259 area_id = XCAR (hotspot);
22260 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22261 If so, we could look for mouse-enter, mouse-leave
22262 properties in PLIST (and do something...). */
22263 hotspot = XCDR (hotspot);
22264 if (CONSP (hotspot)
22265 && (plist = XCAR (hotspot), CONSP (plist)))
22267 pointer = Fplist_get (plist, Qpointer);
22268 if (NILP (pointer))
22269 pointer = Qhand;
22270 help_echo_string = Fplist_get (plist, Qhelp_echo);
22271 if (!NILP (help_echo_string))
22273 help_echo_window = window;
22274 help_echo_object = glyph->object;
22275 help_echo_pos = glyph->charpos;
22279 if (NILP (pointer))
22280 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22284 /* Clear mouse face if X/Y not over text. */
22285 if (glyph == NULL
22286 || area != TEXT_AREA
22287 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22289 if (clear_mouse_face (dpyinfo))
22290 cursor = No_Cursor;
22291 if (NILP (pointer))
22293 if (area != TEXT_AREA)
22294 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22295 else
22296 pointer = Vvoid_text_area_pointer;
22298 goto set_cursor;
22301 pos = glyph->charpos;
22302 object = glyph->object;
22303 if (!STRINGP (object) && !BUFFERP (object))
22304 goto set_cursor;
22306 /* If we get an out-of-range value, return now; avoid an error. */
22307 if (BUFFERP (object) && pos > BUF_Z (b))
22308 goto set_cursor;
22310 /* Make the window's buffer temporarily current for
22311 overlays_at and compute_char_face. */
22312 obuf = current_buffer;
22313 current_buffer = b;
22314 obegv = BEGV;
22315 ozv = ZV;
22316 BEGV = BEG;
22317 ZV = Z;
22319 /* Is this char mouse-active or does it have help-echo? */
22320 position = make_number (pos);
22322 if (BUFFERP (object))
22324 /* Put all the overlays we want in a vector in overlay_vec. */
22325 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22326 /* Sort overlays into increasing priority order. */
22327 noverlays = sort_overlays (overlay_vec, noverlays, w);
22329 else
22330 noverlays = 0;
22332 same_region = (EQ (window, dpyinfo->mouse_face_window)
22333 && vpos >= dpyinfo->mouse_face_beg_row
22334 && vpos <= dpyinfo->mouse_face_end_row
22335 && (vpos > dpyinfo->mouse_face_beg_row
22336 || hpos >= dpyinfo->mouse_face_beg_col)
22337 && (vpos < dpyinfo->mouse_face_end_row
22338 || hpos < dpyinfo->mouse_face_end_col
22339 || dpyinfo->mouse_face_past_end));
22341 if (same_region)
22342 cursor = No_Cursor;
22344 /* Check mouse-face highlighting. */
22345 if (! same_region
22346 /* If there exists an overlay with mouse-face overlapping
22347 the one we are currently highlighting, we have to
22348 check if we enter the overlapping overlay, and then
22349 highlight only that. */
22350 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22351 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22353 /* Find the highest priority overlay that has a mouse-face
22354 property. */
22355 overlay = Qnil;
22356 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22358 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22359 if (!NILP (mouse_face))
22360 overlay = overlay_vec[i];
22363 /* If we're actually highlighting the same overlay as
22364 before, there's no need to do that again. */
22365 if (!NILP (overlay)
22366 && EQ (overlay, dpyinfo->mouse_face_overlay))
22367 goto check_help_echo;
22369 dpyinfo->mouse_face_overlay = overlay;
22371 /* Clear the display of the old active region, if any. */
22372 if (clear_mouse_face (dpyinfo))
22373 cursor = No_Cursor;
22375 /* If no overlay applies, get a text property. */
22376 if (NILP (overlay))
22377 mouse_face = Fget_text_property (position, Qmouse_face, object);
22379 /* Handle the overlay case. */
22380 if (!NILP (overlay))
22382 /* Find the range of text around this char that
22383 should be active. */
22384 Lisp_Object before, after;
22385 int ignore;
22387 before = Foverlay_start (overlay);
22388 after = Foverlay_end (overlay);
22389 /* Record this as the current active region. */
22390 fast_find_position (w, XFASTINT (before),
22391 &dpyinfo->mouse_face_beg_col,
22392 &dpyinfo->mouse_face_beg_row,
22393 &dpyinfo->mouse_face_beg_x,
22394 &dpyinfo->mouse_face_beg_y, Qnil);
22396 dpyinfo->mouse_face_past_end
22397 = !fast_find_position (w, XFASTINT (after),
22398 &dpyinfo->mouse_face_end_col,
22399 &dpyinfo->mouse_face_end_row,
22400 &dpyinfo->mouse_face_end_x,
22401 &dpyinfo->mouse_face_end_y, Qnil);
22402 dpyinfo->mouse_face_window = window;
22404 dpyinfo->mouse_face_face_id
22405 = face_at_buffer_position (w, pos, 0, 0,
22406 &ignore, pos + 1,
22407 !dpyinfo->mouse_face_hidden);
22409 /* Display it as active. */
22410 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22411 cursor = No_Cursor;
22413 /* Handle the text property case. */
22414 else if (!NILP (mouse_face) && BUFFERP (object))
22416 /* Find the range of text around this char that
22417 should be active. */
22418 Lisp_Object before, after, beginning, end;
22419 int ignore;
22421 beginning = Fmarker_position (w->start);
22422 end = make_number (BUF_Z (XBUFFER (object))
22423 - XFASTINT (w->window_end_pos));
22424 before
22425 = Fprevious_single_property_change (make_number (pos + 1),
22426 Qmouse_face,
22427 object, beginning);
22428 after
22429 = Fnext_single_property_change (position, Qmouse_face,
22430 object, end);
22432 /* Record this as the current active region. */
22433 fast_find_position (w, XFASTINT (before),
22434 &dpyinfo->mouse_face_beg_col,
22435 &dpyinfo->mouse_face_beg_row,
22436 &dpyinfo->mouse_face_beg_x,
22437 &dpyinfo->mouse_face_beg_y, Qnil);
22438 dpyinfo->mouse_face_past_end
22439 = !fast_find_position (w, XFASTINT (after),
22440 &dpyinfo->mouse_face_end_col,
22441 &dpyinfo->mouse_face_end_row,
22442 &dpyinfo->mouse_face_end_x,
22443 &dpyinfo->mouse_face_end_y, Qnil);
22444 dpyinfo->mouse_face_window = window;
22446 if (BUFFERP (object))
22447 dpyinfo->mouse_face_face_id
22448 = face_at_buffer_position (w, pos, 0, 0,
22449 &ignore, pos + 1,
22450 !dpyinfo->mouse_face_hidden);
22452 /* Display it as active. */
22453 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22454 cursor = No_Cursor;
22456 else if (!NILP (mouse_face) && STRINGP (object))
22458 Lisp_Object b, e;
22459 int ignore;
22461 b = Fprevious_single_property_change (make_number (pos + 1),
22462 Qmouse_face,
22463 object, Qnil);
22464 e = Fnext_single_property_change (position, Qmouse_face,
22465 object, Qnil);
22466 if (NILP (b))
22467 b = make_number (0);
22468 if (NILP (e))
22469 e = make_number (SCHARS (object) - 1);
22471 fast_find_string_pos (w, XINT (b), object,
22472 &dpyinfo->mouse_face_beg_col,
22473 &dpyinfo->mouse_face_beg_row,
22474 &dpyinfo->mouse_face_beg_x,
22475 &dpyinfo->mouse_face_beg_y, 0);
22476 fast_find_string_pos (w, XINT (e), object,
22477 &dpyinfo->mouse_face_end_col,
22478 &dpyinfo->mouse_face_end_row,
22479 &dpyinfo->mouse_face_end_x,
22480 &dpyinfo->mouse_face_end_y, 1);
22481 dpyinfo->mouse_face_past_end = 0;
22482 dpyinfo->mouse_face_window = window;
22483 dpyinfo->mouse_face_face_id
22484 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22485 glyph->face_id, 1);
22486 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22487 cursor = No_Cursor;
22489 else if (STRINGP (object) && NILP (mouse_face))
22491 /* A string which doesn't have mouse-face, but
22492 the text ``under'' it might have. */
22493 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22494 int start = MATRIX_ROW_START_CHARPOS (r);
22496 pos = string_buffer_position (w, object, start);
22497 if (pos > 0)
22498 mouse_face = get_char_property_and_overlay (make_number (pos),
22499 Qmouse_face,
22500 w->buffer,
22501 &overlay);
22502 if (!NILP (mouse_face) && !NILP (overlay))
22504 Lisp_Object before = Foverlay_start (overlay);
22505 Lisp_Object after = Foverlay_end (overlay);
22506 int ignore;
22508 /* Note that we might not be able to find position
22509 BEFORE in the glyph matrix if the overlay is
22510 entirely covered by a `display' property. In
22511 this case, we overshoot. So let's stop in
22512 the glyph matrix before glyphs for OBJECT. */
22513 fast_find_position (w, XFASTINT (before),
22514 &dpyinfo->mouse_face_beg_col,
22515 &dpyinfo->mouse_face_beg_row,
22516 &dpyinfo->mouse_face_beg_x,
22517 &dpyinfo->mouse_face_beg_y,
22518 object);
22520 dpyinfo->mouse_face_past_end
22521 = !fast_find_position (w, XFASTINT (after),
22522 &dpyinfo->mouse_face_end_col,
22523 &dpyinfo->mouse_face_end_row,
22524 &dpyinfo->mouse_face_end_x,
22525 &dpyinfo->mouse_face_end_y,
22526 Qnil);
22527 dpyinfo->mouse_face_window = window;
22528 dpyinfo->mouse_face_face_id
22529 = face_at_buffer_position (w, pos, 0, 0,
22530 &ignore, pos + 1,
22531 !dpyinfo->mouse_face_hidden);
22533 /* Display it as active. */
22534 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22535 cursor = No_Cursor;
22540 check_help_echo:
22542 /* Look for a `help-echo' property. */
22543 if (NILP (help_echo_string)) {
22544 Lisp_Object help, overlay;
22546 /* Check overlays first. */
22547 help = overlay = Qnil;
22548 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22550 overlay = overlay_vec[i];
22551 help = Foverlay_get (overlay, Qhelp_echo);
22554 if (!NILP (help))
22556 help_echo_string = help;
22557 help_echo_window = window;
22558 help_echo_object = overlay;
22559 help_echo_pos = pos;
22561 else
22563 Lisp_Object object = glyph->object;
22564 int charpos = glyph->charpos;
22566 /* Try text properties. */
22567 if (STRINGP (object)
22568 && charpos >= 0
22569 && charpos < SCHARS (object))
22571 help = Fget_text_property (make_number (charpos),
22572 Qhelp_echo, object);
22573 if (NILP (help))
22575 /* If the string itself doesn't specify a help-echo,
22576 see if the buffer text ``under'' it does. */
22577 struct glyph_row *r
22578 = MATRIX_ROW (w->current_matrix, vpos);
22579 int start = MATRIX_ROW_START_CHARPOS (r);
22580 int pos = string_buffer_position (w, object, start);
22581 if (pos > 0)
22583 help = Fget_char_property (make_number (pos),
22584 Qhelp_echo, w->buffer);
22585 if (!NILP (help))
22587 charpos = pos;
22588 object = w->buffer;
22593 else if (BUFFERP (object)
22594 && charpos >= BEGV
22595 && charpos < ZV)
22596 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22597 object);
22599 if (!NILP (help))
22601 help_echo_string = help;
22602 help_echo_window = window;
22603 help_echo_object = object;
22604 help_echo_pos = charpos;
22609 /* Look for a `pointer' property. */
22610 if (NILP (pointer))
22612 /* Check overlays first. */
22613 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22614 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22616 if (NILP (pointer))
22618 Lisp_Object object = glyph->object;
22619 int charpos = glyph->charpos;
22621 /* Try text properties. */
22622 if (STRINGP (object)
22623 && charpos >= 0
22624 && charpos < SCHARS (object))
22626 pointer = Fget_text_property (make_number (charpos),
22627 Qpointer, object);
22628 if (NILP (pointer))
22630 /* If the string itself doesn't specify a pointer,
22631 see if the buffer text ``under'' it does. */
22632 struct glyph_row *r
22633 = MATRIX_ROW (w->current_matrix, vpos);
22634 int start = MATRIX_ROW_START_CHARPOS (r);
22635 int pos = string_buffer_position (w, object, start);
22636 if (pos > 0)
22637 pointer = Fget_char_property (make_number (pos),
22638 Qpointer, w->buffer);
22641 else if (BUFFERP (object)
22642 && charpos >= BEGV
22643 && charpos < ZV)
22644 pointer = Fget_text_property (make_number (charpos),
22645 Qpointer, object);
22649 BEGV = obegv;
22650 ZV = ozv;
22651 current_buffer = obuf;
22654 set_cursor:
22656 define_frame_cursor1 (f, cursor, pointer);
22660 /* EXPORT for RIF:
22661 Clear any mouse-face on window W. This function is part of the
22662 redisplay interface, and is called from try_window_id and similar
22663 functions to ensure the mouse-highlight is off. */
22665 void
22666 x_clear_window_mouse_face (w)
22667 struct window *w;
22669 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22670 Lisp_Object window;
22672 BLOCK_INPUT;
22673 XSETWINDOW (window, w);
22674 if (EQ (window, dpyinfo->mouse_face_window))
22675 clear_mouse_face (dpyinfo);
22676 UNBLOCK_INPUT;
22680 /* EXPORT:
22681 Just discard the mouse face information for frame F, if any.
22682 This is used when the size of F is changed. */
22684 void
22685 cancel_mouse_face (f)
22686 struct frame *f;
22688 Lisp_Object window;
22689 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22691 window = dpyinfo->mouse_face_window;
22692 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22696 dpyinfo->mouse_face_window = Qnil;
22701 #endif /* HAVE_WINDOW_SYSTEM */
22704 /***********************************************************************
22705 Exposure Events
22706 ***********************************************************************/
22708 #ifdef HAVE_WINDOW_SYSTEM
22710 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22711 which intersects rectangle R. R is in window-relative coordinates. */
22713 static void
22714 expose_area (w, row, r, area)
22715 struct window *w;
22716 struct glyph_row *row;
22717 XRectangle *r;
22718 enum glyph_row_area area;
22720 struct glyph *first = row->glyphs[area];
22721 struct glyph *end = row->glyphs[area] + row->used[area];
22722 struct glyph *last;
22723 int first_x, start_x, x;
22725 if (area == TEXT_AREA && row->fill_line_p)
22726 /* If row extends face to end of line write the whole line. */
22727 draw_glyphs (w, 0, row, area,
22728 0, row->used[area],
22729 DRAW_NORMAL_TEXT, 0);
22730 else
22732 /* Set START_X to the window-relative start position for drawing glyphs of
22733 AREA. The first glyph of the text area can be partially visible.
22734 The first glyphs of other areas cannot. */
22735 start_x = window_box_left_offset (w, area);
22736 x = start_x;
22737 if (area == TEXT_AREA)
22738 x += row->x;
22740 /* Find the first glyph that must be redrawn. */
22741 while (first < end
22742 && x + first->pixel_width < r->x)
22744 x += first->pixel_width;
22745 ++first;
22748 /* Find the last one. */
22749 last = first;
22750 first_x = x;
22751 while (last < end
22752 && x < r->x + r->width)
22754 x += last->pixel_width;
22755 ++last;
22758 /* Repaint. */
22759 if (last > first)
22760 draw_glyphs (w, first_x - start_x, row, area,
22761 first - row->glyphs[area], last - row->glyphs[area],
22762 DRAW_NORMAL_TEXT, 0);
22767 /* Redraw the parts of the glyph row ROW on window W intersecting
22768 rectangle R. R is in window-relative coordinates. Value is
22769 non-zero if mouse-face was overwritten. */
22771 static int
22772 expose_line (w, row, r)
22773 struct window *w;
22774 struct glyph_row *row;
22775 XRectangle *r;
22777 xassert (row->enabled_p);
22779 if (row->mode_line_p || w->pseudo_window_p)
22780 draw_glyphs (w, 0, row, TEXT_AREA,
22781 0, row->used[TEXT_AREA],
22782 DRAW_NORMAL_TEXT, 0);
22783 else
22785 if (row->used[LEFT_MARGIN_AREA])
22786 expose_area (w, row, r, LEFT_MARGIN_AREA);
22787 if (row->used[TEXT_AREA])
22788 expose_area (w, row, r, TEXT_AREA);
22789 if (row->used[RIGHT_MARGIN_AREA])
22790 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22791 draw_row_fringe_bitmaps (w, row);
22794 return row->mouse_face_p;
22798 /* Redraw those parts of glyphs rows during expose event handling that
22799 overlap other rows. Redrawing of an exposed line writes over parts
22800 of lines overlapping that exposed line; this function fixes that.
22802 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22803 row in W's current matrix that is exposed and overlaps other rows.
22804 LAST_OVERLAPPING_ROW is the last such row. */
22806 static void
22807 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22808 struct window *w;
22809 struct glyph_row *first_overlapping_row;
22810 struct glyph_row *last_overlapping_row;
22812 struct glyph_row *row;
22814 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22815 if (row->overlapping_p)
22817 xassert (row->enabled_p && !row->mode_line_p);
22819 if (row->used[LEFT_MARGIN_AREA])
22820 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22822 if (row->used[TEXT_AREA])
22823 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22825 if (row->used[RIGHT_MARGIN_AREA])
22826 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22831 /* Return non-zero if W's cursor intersects rectangle R. */
22833 static int
22834 phys_cursor_in_rect_p (w, r)
22835 struct window *w;
22836 XRectangle *r;
22838 XRectangle cr, result;
22839 struct glyph *cursor_glyph;
22841 cursor_glyph = get_phys_cursor_glyph (w);
22842 if (cursor_glyph)
22844 /* r is relative to W's box, but w->phys_cursor.x is relative
22845 to left edge of W's TEXT area. Adjust it. */
22846 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22847 cr.y = w->phys_cursor.y;
22848 cr.width = cursor_glyph->pixel_width;
22849 cr.height = w->phys_cursor_height;
22850 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22851 I assume the effect is the same -- and this is portable. */
22852 return x_intersect_rectangles (&cr, r, &result);
22854 else
22855 return 0;
22859 /* EXPORT:
22860 Draw a vertical window border to the right of window W if W doesn't
22861 have vertical scroll bars. */
22863 void
22864 x_draw_vertical_border (w)
22865 struct window *w;
22867 /* We could do better, if we knew what type of scroll-bar the adjacent
22868 windows (on either side) have... But we don't :-(
22869 However, I think this works ok. ++KFS 2003-04-25 */
22871 /* Redraw borders between horizontally adjacent windows. Don't
22872 do it for frames with vertical scroll bars because either the
22873 right scroll bar of a window, or the left scroll bar of its
22874 neighbor will suffice as a border. */
22875 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22876 return;
22878 if (!WINDOW_RIGHTMOST_P (w)
22879 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22881 int x0, x1, y0, y1;
22883 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22884 y1 -= 1;
22886 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22887 x1 -= 1;
22889 rif->draw_vertical_window_border (w, x1, y0, y1);
22891 else if (!WINDOW_LEFTMOST_P (w)
22892 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22894 int x0, x1, y0, y1;
22896 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22897 y1 -= 1;
22899 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22900 x0 -= 1;
22902 rif->draw_vertical_window_border (w, x0, y0, y1);
22907 /* Redraw the part of window W intersection rectangle FR. Pixel
22908 coordinates in FR are frame-relative. Call this function with
22909 input blocked. Value is non-zero if the exposure overwrites
22910 mouse-face. */
22912 static int
22913 expose_window (w, fr)
22914 struct window *w;
22915 XRectangle *fr;
22917 struct frame *f = XFRAME (w->frame);
22918 XRectangle wr, r;
22919 int mouse_face_overwritten_p = 0;
22921 /* If window is not yet fully initialized, do nothing. This can
22922 happen when toolkit scroll bars are used and a window is split.
22923 Reconfiguring the scroll bar will generate an expose for a newly
22924 created window. */
22925 if (w->current_matrix == NULL)
22926 return 0;
22928 /* When we're currently updating the window, display and current
22929 matrix usually don't agree. Arrange for a thorough display
22930 later. */
22931 if (w == updated_window)
22933 SET_FRAME_GARBAGED (f);
22934 return 0;
22937 /* Frame-relative pixel rectangle of W. */
22938 wr.x = WINDOW_LEFT_EDGE_X (w);
22939 wr.y = WINDOW_TOP_EDGE_Y (w);
22940 wr.width = WINDOW_TOTAL_WIDTH (w);
22941 wr.height = WINDOW_TOTAL_HEIGHT (w);
22943 if (x_intersect_rectangles (fr, &wr, &r))
22945 int yb = window_text_bottom_y (w);
22946 struct glyph_row *row;
22947 int cursor_cleared_p;
22948 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22950 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22951 r.x, r.y, r.width, r.height));
22953 /* Convert to window coordinates. */
22954 r.x -= WINDOW_LEFT_EDGE_X (w);
22955 r.y -= WINDOW_TOP_EDGE_Y (w);
22957 /* Turn off the cursor. */
22958 if (!w->pseudo_window_p
22959 && phys_cursor_in_rect_p (w, &r))
22961 x_clear_cursor (w);
22962 cursor_cleared_p = 1;
22964 else
22965 cursor_cleared_p = 0;
22967 /* Update lines intersecting rectangle R. */
22968 first_overlapping_row = last_overlapping_row = NULL;
22969 for (row = w->current_matrix->rows;
22970 row->enabled_p;
22971 ++row)
22973 int y0 = row->y;
22974 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22976 if ((y0 >= r.y && y0 < r.y + r.height)
22977 || (y1 > r.y && y1 < r.y + r.height)
22978 || (r.y >= y0 && r.y < y1)
22979 || (r.y + r.height > y0 && r.y + r.height < y1))
22981 /* A header line may be overlapping, but there is no need
22982 to fix overlapping areas for them. KFS 2005-02-12 */
22983 if (row->overlapping_p && !row->mode_line_p)
22985 if (first_overlapping_row == NULL)
22986 first_overlapping_row = row;
22987 last_overlapping_row = row;
22990 if (expose_line (w, row, &r))
22991 mouse_face_overwritten_p = 1;
22994 if (y1 >= yb)
22995 break;
22998 /* Display the mode line if there is one. */
22999 if (WINDOW_WANTS_MODELINE_P (w)
23000 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23001 row->enabled_p)
23002 && row->y < r.y + r.height)
23004 if (expose_line (w, row, &r))
23005 mouse_face_overwritten_p = 1;
23008 if (!w->pseudo_window_p)
23010 /* Fix the display of overlapping rows. */
23011 if (first_overlapping_row)
23012 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23014 /* Draw border between windows. */
23015 x_draw_vertical_border (w);
23017 /* Turn the cursor on again. */
23018 if (cursor_cleared_p)
23019 update_window_cursor (w, 1);
23023 return mouse_face_overwritten_p;
23028 /* Redraw (parts) of all windows in the window tree rooted at W that
23029 intersect R. R contains frame pixel coordinates. Value is
23030 non-zero if the exposure overwrites mouse-face. */
23032 static int
23033 expose_window_tree (w, r)
23034 struct window *w;
23035 XRectangle *r;
23037 struct frame *f = XFRAME (w->frame);
23038 int mouse_face_overwritten_p = 0;
23040 while (w && !FRAME_GARBAGED_P (f))
23042 if (!NILP (w->hchild))
23043 mouse_face_overwritten_p
23044 |= expose_window_tree (XWINDOW (w->hchild), r);
23045 else if (!NILP (w->vchild))
23046 mouse_face_overwritten_p
23047 |= expose_window_tree (XWINDOW (w->vchild), r);
23048 else
23049 mouse_face_overwritten_p |= expose_window (w, r);
23051 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23054 return mouse_face_overwritten_p;
23058 /* EXPORT:
23059 Redisplay an exposed area of frame F. X and Y are the upper-left
23060 corner of the exposed rectangle. W and H are width and height of
23061 the exposed area. All are pixel values. W or H zero means redraw
23062 the entire frame. */
23064 void
23065 expose_frame (f, x, y, w, h)
23066 struct frame *f;
23067 int x, y, w, h;
23069 XRectangle r;
23070 int mouse_face_overwritten_p = 0;
23072 TRACE ((stderr, "expose_frame "));
23074 /* No need to redraw if frame will be redrawn soon. */
23075 if (FRAME_GARBAGED_P (f))
23077 TRACE ((stderr, " garbaged\n"));
23078 return;
23081 /* If basic faces haven't been realized yet, there is no point in
23082 trying to redraw anything. This can happen when we get an expose
23083 event while Emacs is starting, e.g. by moving another window. */
23084 if (FRAME_FACE_CACHE (f) == NULL
23085 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23087 TRACE ((stderr, " no faces\n"));
23088 return;
23091 if (w == 0 || h == 0)
23093 r.x = r.y = 0;
23094 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23095 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23097 else
23099 r.x = x;
23100 r.y = y;
23101 r.width = w;
23102 r.height = h;
23105 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23106 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23108 if (WINDOWP (f->tool_bar_window))
23109 mouse_face_overwritten_p
23110 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23112 #ifdef HAVE_X_WINDOWS
23113 #ifndef MSDOS
23114 #ifndef USE_X_TOOLKIT
23115 if (WINDOWP (f->menu_bar_window))
23116 mouse_face_overwritten_p
23117 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23118 #endif /* not USE_X_TOOLKIT */
23119 #endif
23120 #endif
23122 /* Some window managers support a focus-follows-mouse style with
23123 delayed raising of frames. Imagine a partially obscured frame,
23124 and moving the mouse into partially obscured mouse-face on that
23125 frame. The visible part of the mouse-face will be highlighted,
23126 then the WM raises the obscured frame. With at least one WM, KDE
23127 2.1, Emacs is not getting any event for the raising of the frame
23128 (even tried with SubstructureRedirectMask), only Expose events.
23129 These expose events will draw text normally, i.e. not
23130 highlighted. Which means we must redo the highlight here.
23131 Subsume it under ``we love X''. --gerd 2001-08-15 */
23132 /* Included in Windows version because Windows most likely does not
23133 do the right thing if any third party tool offers
23134 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23135 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23137 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23138 if (f == dpyinfo->mouse_face_mouse_frame)
23140 int x = dpyinfo->mouse_face_mouse_x;
23141 int y = dpyinfo->mouse_face_mouse_y;
23142 clear_mouse_face (dpyinfo);
23143 note_mouse_highlight (f, x, y);
23149 /* EXPORT:
23150 Determine the intersection of two rectangles R1 and R2. Return
23151 the intersection in *RESULT. Value is non-zero if RESULT is not
23152 empty. */
23155 x_intersect_rectangles (r1, r2, result)
23156 XRectangle *r1, *r2, *result;
23158 XRectangle *left, *right;
23159 XRectangle *upper, *lower;
23160 int intersection_p = 0;
23162 /* Rearrange so that R1 is the left-most rectangle. */
23163 if (r1->x < r2->x)
23164 left = r1, right = r2;
23165 else
23166 left = r2, right = r1;
23168 /* X0 of the intersection is right.x0, if this is inside R1,
23169 otherwise there is no intersection. */
23170 if (right->x <= left->x + left->width)
23172 result->x = right->x;
23174 /* The right end of the intersection is the minimum of the
23175 the right ends of left and right. */
23176 result->width = (min (left->x + left->width, right->x + right->width)
23177 - result->x);
23179 /* Same game for Y. */
23180 if (r1->y < r2->y)
23181 upper = r1, lower = r2;
23182 else
23183 upper = r2, lower = r1;
23185 /* The upper end of the intersection is lower.y0, if this is inside
23186 of upper. Otherwise, there is no intersection. */
23187 if (lower->y <= upper->y + upper->height)
23189 result->y = lower->y;
23191 /* The lower end of the intersection is the minimum of the lower
23192 ends of upper and lower. */
23193 result->height = (min (lower->y + lower->height,
23194 upper->y + upper->height)
23195 - result->y);
23196 intersection_p = 1;
23200 return intersection_p;
23203 #endif /* HAVE_WINDOW_SYSTEM */
23206 /***********************************************************************
23207 Initialization
23208 ***********************************************************************/
23210 void
23211 syms_of_xdisp ()
23213 Vwith_echo_area_save_vector = Qnil;
23214 staticpro (&Vwith_echo_area_save_vector);
23216 Vmessage_stack = Qnil;
23217 staticpro (&Vmessage_stack);
23219 Qinhibit_redisplay = intern ("inhibit-redisplay");
23220 staticpro (&Qinhibit_redisplay);
23222 message_dolog_marker1 = Fmake_marker ();
23223 staticpro (&message_dolog_marker1);
23224 message_dolog_marker2 = Fmake_marker ();
23225 staticpro (&message_dolog_marker2);
23226 message_dolog_marker3 = Fmake_marker ();
23227 staticpro (&message_dolog_marker3);
23229 #if GLYPH_DEBUG
23230 defsubr (&Sdump_frame_glyph_matrix);
23231 defsubr (&Sdump_glyph_matrix);
23232 defsubr (&Sdump_glyph_row);
23233 defsubr (&Sdump_tool_bar_row);
23234 defsubr (&Strace_redisplay);
23235 defsubr (&Strace_to_stderr);
23236 #endif
23237 #ifdef HAVE_WINDOW_SYSTEM
23238 defsubr (&Stool_bar_lines_needed);
23239 defsubr (&Slookup_image_map);
23240 #endif
23241 defsubr (&Sformat_mode_line);
23243 staticpro (&Qmenu_bar_update_hook);
23244 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23246 staticpro (&Qoverriding_terminal_local_map);
23247 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23249 staticpro (&Qoverriding_local_map);
23250 Qoverriding_local_map = intern ("overriding-local-map");
23252 staticpro (&Qwindow_scroll_functions);
23253 Qwindow_scroll_functions = intern ("window-scroll-functions");
23255 staticpro (&Qredisplay_end_trigger_functions);
23256 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23258 staticpro (&Qinhibit_point_motion_hooks);
23259 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23261 QCdata = intern (":data");
23262 staticpro (&QCdata);
23263 Qdisplay = intern ("display");
23264 staticpro (&Qdisplay);
23265 Qspace_width = intern ("space-width");
23266 staticpro (&Qspace_width);
23267 Qraise = intern ("raise");
23268 staticpro (&Qraise);
23269 Qslice = intern ("slice");
23270 staticpro (&Qslice);
23271 Qspace = intern ("space");
23272 staticpro (&Qspace);
23273 Qmargin = intern ("margin");
23274 staticpro (&Qmargin);
23275 Qpointer = intern ("pointer");
23276 staticpro (&Qpointer);
23277 Qleft_margin = intern ("left-margin");
23278 staticpro (&Qleft_margin);
23279 Qright_margin = intern ("right-margin");
23280 staticpro (&Qright_margin);
23281 Qcenter = intern ("center");
23282 staticpro (&Qcenter);
23283 Qline_height = intern ("line-height");
23284 staticpro (&Qline_height);
23285 QCalign_to = intern (":align-to");
23286 staticpro (&QCalign_to);
23287 QCrelative_width = intern (":relative-width");
23288 staticpro (&QCrelative_width);
23289 QCrelative_height = intern (":relative-height");
23290 staticpro (&QCrelative_height);
23291 QCeval = intern (":eval");
23292 staticpro (&QCeval);
23293 QCpropertize = intern (":propertize");
23294 staticpro (&QCpropertize);
23295 QCfile = intern (":file");
23296 staticpro (&QCfile);
23297 Qfontified = intern ("fontified");
23298 staticpro (&Qfontified);
23299 Qfontification_functions = intern ("fontification-functions");
23300 staticpro (&Qfontification_functions);
23301 Qtrailing_whitespace = intern ("trailing-whitespace");
23302 staticpro (&Qtrailing_whitespace);
23303 Qescape_glyph = intern ("escape-glyph");
23304 staticpro (&Qescape_glyph);
23305 Qnobreak_space = intern ("nobreak-space");
23306 staticpro (&Qnobreak_space);
23307 Qimage = intern ("image");
23308 staticpro (&Qimage);
23309 QCmap = intern (":map");
23310 staticpro (&QCmap);
23311 QCpointer = intern (":pointer");
23312 staticpro (&QCpointer);
23313 Qrect = intern ("rect");
23314 staticpro (&Qrect);
23315 Qcircle = intern ("circle");
23316 staticpro (&Qcircle);
23317 Qpoly = intern ("poly");
23318 staticpro (&Qpoly);
23319 Qmessage_truncate_lines = intern ("message-truncate-lines");
23320 staticpro (&Qmessage_truncate_lines);
23321 Qgrow_only = intern ("grow-only");
23322 staticpro (&Qgrow_only);
23323 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23324 staticpro (&Qinhibit_menubar_update);
23325 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23326 staticpro (&Qinhibit_eval_during_redisplay);
23327 Qposition = intern ("position");
23328 staticpro (&Qposition);
23329 Qbuffer_position = intern ("buffer-position");
23330 staticpro (&Qbuffer_position);
23331 Qobject = intern ("object");
23332 staticpro (&Qobject);
23333 Qbar = intern ("bar");
23334 staticpro (&Qbar);
23335 Qhbar = intern ("hbar");
23336 staticpro (&Qhbar);
23337 Qbox = intern ("box");
23338 staticpro (&Qbox);
23339 Qhollow = intern ("hollow");
23340 staticpro (&Qhollow);
23341 Qhand = intern ("hand");
23342 staticpro (&Qhand);
23343 Qarrow = intern ("arrow");
23344 staticpro (&Qarrow);
23345 Qtext = intern ("text");
23346 staticpro (&Qtext);
23347 Qrisky_local_variable = intern ("risky-local-variable");
23348 staticpro (&Qrisky_local_variable);
23349 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23350 staticpro (&Qinhibit_free_realized_faces);
23352 list_of_error = Fcons (Fcons (intern ("error"),
23353 Fcons (intern ("void-variable"), Qnil)),
23354 Qnil);
23355 staticpro (&list_of_error);
23357 Qlast_arrow_position = intern ("last-arrow-position");
23358 staticpro (&Qlast_arrow_position);
23359 Qlast_arrow_string = intern ("last-arrow-string");
23360 staticpro (&Qlast_arrow_string);
23362 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23363 staticpro (&Qoverlay_arrow_string);
23364 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23365 staticpro (&Qoverlay_arrow_bitmap);
23367 echo_buffer[0] = echo_buffer[1] = Qnil;
23368 staticpro (&echo_buffer[0]);
23369 staticpro (&echo_buffer[1]);
23371 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23372 staticpro (&echo_area_buffer[0]);
23373 staticpro (&echo_area_buffer[1]);
23375 Vmessages_buffer_name = build_string ("*Messages*");
23376 staticpro (&Vmessages_buffer_name);
23378 mode_line_proptrans_alist = Qnil;
23379 staticpro (&mode_line_proptrans_alist);
23380 mode_line_string_list = Qnil;
23381 staticpro (&mode_line_string_list);
23382 mode_line_string_face = Qnil;
23383 staticpro (&mode_line_string_face);
23384 mode_line_string_face_prop = Qnil;
23385 staticpro (&mode_line_string_face_prop);
23386 Vmode_line_unwind_vector = Qnil;
23387 staticpro (&Vmode_line_unwind_vector);
23389 help_echo_string = Qnil;
23390 staticpro (&help_echo_string);
23391 help_echo_object = Qnil;
23392 staticpro (&help_echo_object);
23393 help_echo_window = Qnil;
23394 staticpro (&help_echo_window);
23395 previous_help_echo_string = Qnil;
23396 staticpro (&previous_help_echo_string);
23397 help_echo_pos = -1;
23399 #ifdef HAVE_WINDOW_SYSTEM
23400 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23401 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23402 For example, if a block cursor is over a tab, it will be drawn as
23403 wide as that tab on the display. */);
23404 x_stretch_cursor_p = 0;
23405 #endif
23407 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23408 doc: /* *Non-nil means highlight trailing whitespace.
23409 The face used for trailing whitespace is `trailing-whitespace'. */);
23410 Vshow_trailing_whitespace = Qnil;
23412 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23413 doc: /* *Control highlighting of nobreak space and soft hyphen.
23414 A value of t means highlight the character itself (for nobreak space,
23415 use face `nobreak-space').
23416 A value of nil means no highlighting.
23417 Other values mean display the escape glyph followed by an ordinary
23418 space or ordinary hyphen. */);
23419 Vnobreak_char_display = Qt;
23421 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23422 doc: /* *The pointer shape to show in void text areas.
23423 A value of nil means to show the text pointer. Other options are `arrow',
23424 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23425 Vvoid_text_area_pointer = Qarrow;
23427 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23428 doc: /* Non-nil means don't actually do any redisplay.
23429 This is used for internal purposes. */);
23430 Vinhibit_redisplay = Qnil;
23432 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23433 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23434 Vglobal_mode_string = Qnil;
23436 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23437 doc: /* Marker for where to display an arrow on top of the buffer text.
23438 This must be the beginning of a line in order to work.
23439 See also `overlay-arrow-string'. */);
23440 Voverlay_arrow_position = Qnil;
23442 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23443 doc: /* String to display as an arrow in non-window frames.
23444 See also `overlay-arrow-position'. */);
23445 Voverlay_arrow_string = build_string ("=>");
23447 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23448 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23449 The symbols on this list are examined during redisplay to determine
23450 where to display overlay arrows. */);
23451 Voverlay_arrow_variable_list
23452 = Fcons (intern ("overlay-arrow-position"), Qnil);
23454 DEFVAR_INT ("scroll-step", &scroll_step,
23455 doc: /* *The number of lines to try scrolling a window by when point moves out.
23456 If that fails to bring point back on frame, point is centered instead.
23457 If this is zero, point is always centered after it moves off frame.
23458 If you want scrolling to always be a line at a time, you should set
23459 `scroll-conservatively' to a large value rather than set this to 1. */);
23461 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23462 doc: /* *Scroll up to this many lines, to bring point back on screen.
23463 A value of zero means to scroll the text to center point vertically
23464 in the window. */);
23465 scroll_conservatively = 0;
23467 DEFVAR_INT ("scroll-margin", &scroll_margin,
23468 doc: /* *Number of lines of margin at the top and bottom of a window.
23469 Recenter the window whenever point gets within this many lines
23470 of the top or bottom of the window. */);
23471 scroll_margin = 0;
23473 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23474 doc: /* Pixels per inch value for non-window system displays.
23475 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23476 Vdisplay_pixels_per_inch = make_float (72.0);
23478 #if GLYPH_DEBUG
23479 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23480 #endif
23482 DEFVAR_BOOL ("truncate-partial-width-windows",
23483 &truncate_partial_width_windows,
23484 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23485 truncate_partial_width_windows = 1;
23487 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23488 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23489 Any other value means to use the appropriate face, `mode-line',
23490 `header-line', or `menu' respectively. */);
23491 mode_line_inverse_video = 1;
23493 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23494 doc: /* *Maximum buffer size for which line number should be displayed.
23495 If the buffer is bigger than this, the line number does not appear
23496 in the mode line. A value of nil means no limit. */);
23497 Vline_number_display_limit = Qnil;
23499 DEFVAR_INT ("line-number-display-limit-width",
23500 &line_number_display_limit_width,
23501 doc: /* *Maximum line width (in characters) for line number display.
23502 If the average length of the lines near point is bigger than this, then the
23503 line number may be omitted from the mode line. */);
23504 line_number_display_limit_width = 200;
23506 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23507 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23508 highlight_nonselected_windows = 0;
23510 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23511 doc: /* Non-nil if more than one frame is visible on this display.
23512 Minibuffer-only frames don't count, but iconified frames do.
23513 This variable is not guaranteed to be accurate except while processing
23514 `frame-title-format' and `icon-title-format'. */);
23516 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23517 doc: /* Template for displaying the title bar of visible frames.
23518 \(Assuming the window manager supports this feature.)
23519 This variable has the same structure as `mode-line-format' (which see),
23520 and is used only on frames for which no explicit name has been set
23521 \(see `modify-frame-parameters'). */);
23523 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23524 doc: /* Template for displaying the title bar of an iconified frame.
23525 \(Assuming the window manager supports this feature.)
23526 This variable has the same structure as `mode-line-format' (which see),
23527 and is used only on frames for which no explicit name has been set
23528 \(see `modify-frame-parameters'). */);
23529 Vicon_title_format
23530 = Vframe_title_format
23531 = Fcons (intern ("multiple-frames"),
23532 Fcons (build_string ("%b"),
23533 Fcons (Fcons (empty_string,
23534 Fcons (intern ("invocation-name"),
23535 Fcons (build_string ("@"),
23536 Fcons (intern ("system-name"),
23537 Qnil)))),
23538 Qnil)));
23540 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23541 doc: /* Maximum number of lines to keep in the message log buffer.
23542 If nil, disable message logging. If t, log messages but don't truncate
23543 the buffer when it becomes large. */);
23544 Vmessage_log_max = make_number (50);
23546 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23547 doc: /* Functions called before redisplay, if window sizes have changed.
23548 The value should be a list of functions that take one argument.
23549 Just before redisplay, for each frame, if any of its windows have changed
23550 size since the last redisplay, or have been split or deleted,
23551 all the functions in the list are called, with the frame as argument. */);
23552 Vwindow_size_change_functions = Qnil;
23554 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23555 doc: /* List of functions to call before redisplaying a window with scrolling.
23556 Each function is called with two arguments, the window
23557 and its new display-start position. Note that the value of `window-end'
23558 is not valid when these functions are called. */);
23559 Vwindow_scroll_functions = Qnil;
23561 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23562 doc: /* Functions called when redisplay of a window reaches the end trigger.
23563 Each function is called with two arguments, the window and the end trigger value.
23564 See `set-window-redisplay-end-trigger'. */);
23565 Vredisplay_end_trigger_functions = Qnil;
23567 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23568 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23569 mouse_autoselect_window = 0;
23571 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23572 doc: /* *Non-nil means automatically resize tool-bars.
23573 This increases a tool-bar's height if not all tool-bar items are visible.
23574 It decreases a tool-bar's height when it would display blank lines
23575 otherwise. */);
23576 auto_resize_tool_bars_p = 1;
23578 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23579 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23580 auto_raise_tool_bar_buttons_p = 1;
23582 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23583 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23584 make_cursor_line_fully_visible_p = 1;
23586 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23587 doc: /* *Margin around tool-bar buttons in pixels.
23588 If an integer, use that for both horizontal and vertical margins.
23589 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23590 HORZ specifying the horizontal margin, and VERT specifying the
23591 vertical margin. */);
23592 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23594 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23595 doc: /* *Relief thickness of tool-bar buttons. */);
23596 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23598 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23599 doc: /* List of functions to call to fontify regions of text.
23600 Each function is called with one argument POS. Functions must
23601 fontify a region starting at POS in the current buffer, and give
23602 fontified regions the property `fontified'. */);
23603 Vfontification_functions = Qnil;
23604 Fmake_variable_buffer_local (Qfontification_functions);
23606 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23607 &unibyte_display_via_language_environment,
23608 doc: /* *Non-nil means display unibyte text according to language environment.
23609 Specifically this means that unibyte non-ASCII characters
23610 are displayed by converting them to the equivalent multibyte characters
23611 according to the current language environment. As a result, they are
23612 displayed according to the current fontset. */);
23613 unibyte_display_via_language_environment = 0;
23615 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23616 doc: /* *Maximum height for resizing mini-windows.
23617 If a float, it specifies a fraction of the mini-window frame's height.
23618 If an integer, it specifies a number of lines. */);
23619 Vmax_mini_window_height = make_float (0.25);
23621 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23622 doc: /* *How to resize mini-windows.
23623 A value of nil means don't automatically resize mini-windows.
23624 A value of t means resize them to fit the text displayed in them.
23625 A value of `grow-only', the default, means let mini-windows grow
23626 only, until their display becomes empty, at which point the windows
23627 go back to their normal size. */);
23628 Vresize_mini_windows = Qgrow_only;
23630 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23631 doc: /* Alist specifying how to blink the cursor off.
23632 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23633 `cursor-type' frame-parameter or variable equals ON-STATE,
23634 comparing using `equal', Emacs uses OFF-STATE to specify
23635 how to blink it off. ON-STATE and OFF-STATE are values for
23636 the `cursor-type' frame parameter.
23638 If a frame's ON-STATE has no entry in this list,
23639 the frame's other specifications determine how to blink the cursor off. */);
23640 Vblink_cursor_alist = Qnil;
23642 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23643 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23644 automatic_hscrolling_p = 1;
23646 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23647 doc: /* *How many columns away from the window edge point is allowed to get
23648 before automatic hscrolling will horizontally scroll the window. */);
23649 hscroll_margin = 5;
23651 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23652 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23653 When point is less than `automatic-hscroll-margin' columns from the window
23654 edge, automatic hscrolling will scroll the window by the amount of columns
23655 determined by this variable. If its value is a positive integer, scroll that
23656 many columns. If it's a positive floating-point number, it specifies the
23657 fraction of the window's width to scroll. If it's nil or zero, point will be
23658 centered horizontally after the scroll. Any other value, including negative
23659 numbers, are treated as if the value were zero.
23661 Automatic hscrolling always moves point outside the scroll margin, so if
23662 point was more than scroll step columns inside the margin, the window will
23663 scroll more than the value given by the scroll step.
23665 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23666 and `scroll-right' overrides this variable's effect. */);
23667 Vhscroll_step = make_number (0);
23669 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23670 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23671 Bind this around calls to `message' to let it take effect. */);
23672 message_truncate_lines = 0;
23674 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23675 doc: /* Normal hook run to update the menu bar definitions.
23676 Redisplay runs this hook before it redisplays the menu bar.
23677 This is used to update submenus such as Buffers,
23678 whose contents depend on various data. */);
23679 Vmenu_bar_update_hook = Qnil;
23681 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23682 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23683 inhibit_menubar_update = 0;
23685 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23686 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23687 inhibit_eval_during_redisplay = 0;
23689 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23690 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23691 inhibit_free_realized_faces = 0;
23693 #if GLYPH_DEBUG
23694 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23695 doc: /* Inhibit try_window_id display optimization. */);
23696 inhibit_try_window_id = 0;
23698 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23699 doc: /* Inhibit try_window_reusing display optimization. */);
23700 inhibit_try_window_reusing = 0;
23702 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23703 doc: /* Inhibit try_cursor_movement display optimization. */);
23704 inhibit_try_cursor_movement = 0;
23705 #endif /* GLYPH_DEBUG */
23709 /* Initialize this module when Emacs starts. */
23711 void
23712 init_xdisp ()
23714 Lisp_Object root_window;
23715 struct window *mini_w;
23717 current_header_line_height = current_mode_line_height = -1;
23719 CHARPOS (this_line_start_pos) = 0;
23721 mini_w = XWINDOW (minibuf_window);
23722 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23724 if (!noninteractive)
23726 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23727 int i;
23729 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23730 set_window_height (root_window,
23731 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23733 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23734 set_window_height (minibuf_window, 1, 0);
23736 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23737 mini_w->total_cols = make_number (FRAME_COLS (f));
23739 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23740 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23741 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23743 /* The default ellipsis glyphs `...'. */
23744 for (i = 0; i < 3; ++i)
23745 default_invis_vector[i] = make_number ('.');
23749 /* Allocate the buffer for frame titles.
23750 Also used for `format-mode-line'. */
23751 int size = 100;
23752 mode_line_noprop_buf = (char *) xmalloc (size);
23753 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23754 mode_line_noprop_ptr = mode_line_noprop_buf;
23755 mode_line_target = MODE_LINE_DISPLAY;
23758 help_echo_showing_p = 0;
23762 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23763 (do not change this comment) */