(syms_of_buffer): Fix typo.
[emacs.git] / src / xdisp.c
blobdd10551d5fae14f20e92cbc510a479bd25a84150
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
207 #define INFINITY 10000000
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
215 extern int interrupt_input;
216 extern int command_loop_level;
218 extern Lisp_Object do_mouse_tracking;
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
248 Lisp_Object Qrisky_local_variable;
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
253 /* Functions called to fontify regions of text. */
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
265 int auto_raise_tool_bar_buttons_p;
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
269 int make_cursor_line_fully_visible_p;
271 /* Margin around tool bar buttons in pixels. */
273 Lisp_Object Vtool_bar_button_margin;
275 /* Thickness of shadow to draw around tool bar buttons. */
277 EMACS_INT tool_bar_button_relief;
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
282 int auto_resize_tool_bars_p;
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
288 int x_stretch_cursor_p;
290 /* Non-nil means don't actually do any redisplay. */
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296 int inhibit_eval_during_redisplay;
298 /* Names of text properties relevant for redisplay. */
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
303 /* Symbols used in text property values. */
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
317 /* Non-nil means highlight trailing whitespace. */
319 Lisp_Object Vshow_trailing_whitespace;
321 /* Non-nil means escape non-break space and hyphens. */
323 Lisp_Object Vshow_nonbreak_escape;
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
337 #endif /* HAVE_WINDOW_SYSTEM */
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
343 Lisp_Object Vvoid_text_area_pointer;
345 /* Name of the face used to highlight trailing whitespace. */
347 Lisp_Object Qtrailing_whitespace;
349 /* Name and number of the face used to highlight escape glyphs. */
351 Lisp_Object Qescape_glyph;
353 /* The symbol `image' which is the car of the lists used to represent
354 images in Lisp. */
356 Lisp_Object Qimage;
358 /* The image map types. */
359 Lisp_Object QCmap, QCpointer;
360 Lisp_Object Qrect, Qcircle, Qpoly;
362 /* Non-zero means print newline to stdout before next mini-buffer
363 message. */
365 int noninteractive_need_newline;
367 /* Non-zero means print newline to message log before next message. */
369 static int message_log_need_newline;
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
383 static struct text_pos this_line_start_pos;
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
388 static struct text_pos this_line_end_pos;
390 /* The vertical positions and the height of this line. */
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
399 static int this_line_start_x;
401 /* Buffer that this_line_.* variables are referring to. */
403 static struct buffer *this_line_buffer;
405 /* Nonzero means truncate lines in all windows less wide than the
406 frame. */
408 int truncate_partial_width_windows;
410 /* A flag to control how to display unibyte 8-bit character. */
412 int unibyte_display_via_language_environment;
414 /* Nonzero means we have more than one non-mini-buffer-only frame.
415 Not guaranteed to be accurate except while parsing
416 frame-title-format. */
418 int multiple_frames;
420 Lisp_Object Vglobal_mode_string;
423 /* List of variables (symbols) which hold markers for overlay arrows.
424 The symbols on this list are examined during redisplay to determine
425 where to display overlay arrows. */
427 Lisp_Object Voverlay_arrow_variable_list;
429 /* Marker for where to display an arrow on top of the buffer text. */
431 Lisp_Object Voverlay_arrow_position;
433 /* String to display for the arrow. Only used on terminal frames. */
435 Lisp_Object Voverlay_arrow_string;
437 /* Values of those variables at last redisplay are stored as
438 properties on `overlay-arrow-position' symbol. However, if
439 Voverlay_arrow_position is a marker, last-arrow-position is its
440 numerical position. */
442 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
444 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
445 properties on a symbol in overlay-arrow-variable-list. */
447 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
449 /* Like mode-line-format, but for the title bar on a visible frame. */
451 Lisp_Object Vframe_title_format;
453 /* Like mode-line-format, but for the title bar on an iconified frame. */
455 Lisp_Object Vicon_title_format;
457 /* List of functions to call when a window's size changes. These
458 functions get one arg, a frame on which one or more windows' sizes
459 have changed. */
461 static Lisp_Object Vwindow_size_change_functions;
463 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
465 /* Nonzero if overlay arrow has been displayed once in this window. */
467 static int overlay_arrow_seen;
469 /* Nonzero means highlight the region even in nonselected windows. */
471 int highlight_nonselected_windows;
473 /* If cursor motion alone moves point off frame, try scrolling this
474 many lines up or down if that will bring it back. */
476 static EMACS_INT scroll_step;
478 /* Nonzero means scroll just far enough to bring point back on the
479 screen, when appropriate. */
481 static EMACS_INT scroll_conservatively;
483 /* Recenter the window whenever point gets within this many lines of
484 the top or bottom of the window. This value is translated into a
485 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
486 that there is really a fixed pixel height scroll margin. */
488 EMACS_INT scroll_margin;
490 /* Number of windows showing the buffer of the selected window (or
491 another buffer with the same base buffer). keyboard.c refers to
492 this. */
494 int buffer_shared;
496 /* Vector containing glyphs for an ellipsis `...'. */
498 static Lisp_Object default_invis_vector[3];
500 /* Zero means display the mode-line/header-line/menu-bar in the default face
501 (this slightly odd definition is for compatibility with previous versions
502 of emacs), non-zero means display them using their respective faces.
504 This variable is deprecated. */
506 int mode_line_inverse_video;
508 /* Prompt to display in front of the mini-buffer contents. */
510 Lisp_Object minibuf_prompt;
512 /* Width of current mini-buffer prompt. Only set after display_line
513 of the line that contains the prompt. */
515 int minibuf_prompt_width;
517 /* This is the window where the echo area message was displayed. It
518 is always a mini-buffer window, but it may not be the same window
519 currently active as a mini-buffer. */
521 Lisp_Object echo_area_window;
523 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
524 pushes the current message and the value of
525 message_enable_multibyte on the stack, the function restore_message
526 pops the stack and displays MESSAGE again. */
528 Lisp_Object Vmessage_stack;
530 /* Nonzero means multibyte characters were enabled when the echo area
531 message was specified. */
533 int message_enable_multibyte;
535 /* Nonzero if we should redraw the mode lines on the next redisplay. */
537 int update_mode_lines;
539 /* Nonzero if window sizes or contents have changed since last
540 redisplay that finished. */
542 int windows_or_buffers_changed;
544 /* Nonzero means a frame's cursor type has been changed. */
546 int cursor_type_changed;
548 /* Nonzero after display_mode_line if %l was used and it displayed a
549 line number. */
551 int line_number_displayed;
553 /* Maximum buffer size for which to display line numbers. */
555 Lisp_Object Vline_number_display_limit;
557 /* Line width to consider when repositioning for line number display. */
559 static EMACS_INT line_number_display_limit_width;
561 /* Number of lines to keep in the message log buffer. t means
562 infinite. nil means don't log at all. */
564 Lisp_Object Vmessage_log_max;
566 /* The name of the *Messages* buffer, a string. */
568 static Lisp_Object Vmessages_buffer_name;
570 /* Current, index 0, and last displayed echo area message. Either
571 buffers from echo_buffers, or nil to indicate no message. */
573 Lisp_Object echo_area_buffer[2];
575 /* The buffers referenced from echo_area_buffer. */
577 static Lisp_Object echo_buffer[2];
579 /* A vector saved used in with_area_buffer to reduce consing. */
581 static Lisp_Object Vwith_echo_area_save_vector;
583 /* Non-zero means display_echo_area should display the last echo area
584 message again. Set by redisplay_preserve_echo_area. */
586 static int display_last_displayed_message_p;
588 /* Nonzero if echo area is being used by print; zero if being used by
589 message. */
591 int message_buf_print;
593 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
595 Lisp_Object Qinhibit_menubar_update;
596 int inhibit_menubar_update;
598 /* Maximum height for resizing mini-windows. Either a float
599 specifying a fraction of the available height, or an integer
600 specifying a number of lines. */
602 Lisp_Object Vmax_mini_window_height;
604 /* Non-zero means messages should be displayed with truncated
605 lines instead of being continued. */
607 int message_truncate_lines;
608 Lisp_Object Qmessage_truncate_lines;
610 /* Set to 1 in clear_message to make redisplay_internal aware
611 of an emptied echo area. */
613 static int message_cleared_p;
615 /* Non-zero means we want a hollow cursor in windows that are not
616 selected. Zero means there's no cursor in such windows. */
618 Lisp_Object Vcursor_in_non_selected_windows;
619 Lisp_Object Qcursor_in_non_selected_windows;
621 /* How to blink the default frame cursor off. */
622 Lisp_Object Vblink_cursor_alist;
624 /* A scratch glyph row with contents used for generating truncation
625 glyphs. Also used in direct_output_for_insert. */
627 #define MAX_SCRATCH_GLYPHS 100
628 struct glyph_row scratch_glyph_row;
629 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
631 /* Ascent and height of the last line processed by move_it_to. */
633 static int last_max_ascent, last_height;
635 /* Non-zero if there's a help-echo in the echo area. */
637 int help_echo_showing_p;
639 /* If >= 0, computed, exact values of mode-line and header-line height
640 to use in the macros CURRENT_MODE_LINE_HEIGHT and
641 CURRENT_HEADER_LINE_HEIGHT. */
643 int current_mode_line_height, current_header_line_height;
645 /* The maximum distance to look ahead for text properties. Values
646 that are too small let us call compute_char_face and similar
647 functions too often which is expensive. Values that are too large
648 let us call compute_char_face and alike too often because we
649 might not be interested in text properties that far away. */
651 #define TEXT_PROP_DISTANCE_LIMIT 100
653 #if GLYPH_DEBUG
655 /* Variables to turn off display optimizations from Lisp. */
657 int inhibit_try_window_id, inhibit_try_window_reusing;
658 int inhibit_try_cursor_movement;
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG != 0. */
663 int trace_redisplay_p;
665 #endif /* GLYPH_DEBUG */
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
676 /* Non-zero means automatically scroll windows horizontally to make
677 point visible. */
679 int automatic_hscrolling_p;
681 /* How close to the margin can point get before the window is scrolled
682 horizontally. */
683 EMACS_INT hscroll_margin;
685 /* How much to scroll horizontally when point is inside the above margin. */
686 Lisp_Object Vhscroll_step;
688 /* The variable `resize-mini-windows'. If nil, don't resize
689 mini-windows. If t, always resize them to fit the text they
690 display. If `grow-only', let mini-windows grow only until they
691 become empty. */
693 Lisp_Object Vresize_mini_windows;
695 /* Buffer being redisplayed -- for redisplay_window_error. */
697 struct buffer *displayed_buffer;
699 /* Value returned from text property handlers (see below). */
701 enum prop_handled
703 HANDLED_NORMALLY,
704 HANDLED_RECOMPUTE_PROPS,
705 HANDLED_OVERLAY_STRING_CONSUMED,
706 HANDLED_RETURN
709 /* A description of text properties that redisplay is interested
710 in. */
712 struct props
714 /* The name of the property. */
715 Lisp_Object *name;
717 /* A unique index for the property. */
718 enum prop_idx idx;
720 /* A handler function called to set up iterator IT from the property
721 at IT's current position. Value is used to steer handle_stop. */
722 enum prop_handled (*handler) P_ ((struct it *it));
725 static enum prop_handled handle_face_prop P_ ((struct it *));
726 static enum prop_handled handle_invisible_prop P_ ((struct it *));
727 static enum prop_handled handle_display_prop P_ ((struct it *));
728 static enum prop_handled handle_composition_prop P_ ((struct it *));
729 static enum prop_handled handle_overlay_change P_ ((struct it *));
730 static enum prop_handled handle_fontified_prop P_ ((struct it *));
732 /* Properties handled by iterators. */
734 static struct props it_props[] =
736 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
737 /* Handle `face' before `display' because some sub-properties of
738 `display' need to know the face. */
739 {&Qface, FACE_PROP_IDX, handle_face_prop},
740 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
741 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
742 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
743 {NULL, 0, NULL}
746 /* Value is the position described by X. If X is a marker, value is
747 the marker_position of X. Otherwise, value is X. */
749 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
751 /* Enumeration returned by some move_it_.* functions internally. */
753 enum move_it_result
755 /* Not used. Undefined value. */
756 MOVE_UNDEFINED,
758 /* Move ended at the requested buffer position or ZV. */
759 MOVE_POS_MATCH_OR_ZV,
761 /* Move ended at the requested X pixel position. */
762 MOVE_X_REACHED,
764 /* Move within a line ended at the end of a line that must be
765 continued. */
766 MOVE_LINE_CONTINUED,
768 /* Move within a line ended at the end of a line that would
769 be displayed truncated. */
770 MOVE_LINE_TRUNCATED,
772 /* Move within a line ended at a line end. */
773 MOVE_NEWLINE_OR_CR
776 /* This counter is used to clear the face cache every once in a while
777 in redisplay_internal. It is incremented for each redisplay.
778 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
779 cleared. */
781 #define CLEAR_FACE_CACHE_COUNT 500
782 static int clear_face_cache_count;
784 /* Record the previous terminal frame we displayed. */
786 static struct frame *previous_terminal_frame;
788 /* Non-zero while redisplay_internal is in progress. */
790 int redisplaying_p;
792 /* Non-zero means don't free realized faces. Bound while freeing
793 realized faces is dangerous because glyph matrices might still
794 reference them. */
796 int inhibit_free_realized_faces;
797 Lisp_Object Qinhibit_free_realized_faces;
799 /* If a string, XTread_socket generates an event to display that string.
800 (The display is done in read_char.) */
802 Lisp_Object help_echo_string;
803 Lisp_Object help_echo_window;
804 Lisp_Object help_echo_object;
805 int help_echo_pos;
807 /* Temporary variable for XTread_socket. */
809 Lisp_Object previous_help_echo_string;
811 /* Null glyph slice */
813 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
816 /* Function prototypes. */
818 static void setup_for_ellipsis P_ ((struct it *, int));
819 static void mark_window_display_accurate_1 P_ ((struct window *, int));
820 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
821 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
822 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
823 static int redisplay_mode_lines P_ ((Lisp_Object, int));
824 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
826 #if 0
827 static int invisible_text_between_p P_ ((struct it *, int, int));
828 #endif
830 static void pint2str P_ ((char *, int, int));
831 static void pint2hrstr P_ ((char *, int, int));
832 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
833 struct text_pos));
834 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
835 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
836 static void store_frame_title_char P_ ((char));
837 static int store_frame_title P_ ((const unsigned char *, int, int));
838 static void x_consider_frame_title P_ ((Lisp_Object));
839 static void handle_stop P_ ((struct it *));
840 static int tool_bar_lines_needed P_ ((struct frame *));
841 static int single_display_spec_intangible_p P_ ((Lisp_Object));
842 static void ensure_echo_area_buffers P_ ((void));
843 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
844 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
845 static int with_echo_area_buffer P_ ((struct window *, int,
846 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
847 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static void clear_garbaged_frames P_ ((void));
849 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
850 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
851 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int display_echo_area P_ ((struct window *));
853 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
854 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
855 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
856 static int string_char_and_length P_ ((const unsigned char *, int, int *));
857 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
858 struct text_pos));
859 static int compute_window_start_on_continuation_line P_ ((struct window *));
860 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
861 static void insert_left_trunc_glyphs P_ ((struct it *));
862 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
863 Lisp_Object));
864 static void extend_face_to_end_of_line P_ ((struct it *));
865 static int append_space_for_newline P_ ((struct it *, int));
866 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
867 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
868 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
869 static int trailing_whitespace_p P_ ((int));
870 static int message_log_check_duplicate P_ ((int, int, int, int));
871 static void push_it P_ ((struct it *));
872 static void pop_it P_ ((struct it *));
873 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
874 static void select_frame_for_redisplay P_ ((Lisp_Object));
875 static void redisplay_internal P_ ((int));
876 static int echo_area_display P_ ((int));
877 static void redisplay_windows P_ ((Lisp_Object));
878 static void redisplay_window P_ ((Lisp_Object, int));
879 static Lisp_Object redisplay_window_error ();
880 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
881 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
882 static void update_menu_bar P_ ((struct frame *, int));
883 static int try_window_reusing_current_matrix P_ ((struct window *));
884 static int try_window_id P_ ((struct window *));
885 static int display_line P_ ((struct it *));
886 static int display_mode_lines P_ ((struct window *));
887 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
888 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
889 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
890 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
891 static void display_menu_bar P_ ((struct window *));
892 static int display_count_lines P_ ((int, int, int, int, int *));
893 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
894 int, int, struct it *, int, int, int, int));
895 static void compute_line_metrics P_ ((struct it *));
896 static void run_redisplay_end_trigger_hook P_ ((struct it *));
897 static int get_overlay_strings P_ ((struct it *, int));
898 static void next_overlay_string P_ ((struct it *));
899 static void reseat P_ ((struct it *, struct text_pos, int));
900 static void reseat_1 P_ ((struct it *, struct text_pos, int));
901 static void back_to_previous_visible_line_start P_ ((struct it *));
902 void reseat_at_previous_visible_line_start P_ ((struct it *));
903 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
904 static int next_element_from_ellipsis P_ ((struct it *));
905 static int next_element_from_display_vector P_ ((struct it *));
906 static int next_element_from_string P_ ((struct it *));
907 static int next_element_from_c_string P_ ((struct it *));
908 static int next_element_from_buffer P_ ((struct it *));
909 static int next_element_from_composition P_ ((struct it *));
910 static int next_element_from_image P_ ((struct it *));
911 static int next_element_from_stretch P_ ((struct it *));
912 static void load_overlay_strings P_ ((struct it *, int));
913 static int init_from_display_pos P_ ((struct it *, struct window *,
914 struct display_pos *));
915 static void reseat_to_string P_ ((struct it *, unsigned char *,
916 Lisp_Object, int, int, int, int));
917 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
918 int, int, int));
919 void move_it_vertically_backward P_ ((struct it *, int));
920 static void init_to_row_start P_ ((struct it *, struct window *,
921 struct glyph_row *));
922 static int init_to_row_end P_ ((struct it *, struct window *,
923 struct glyph_row *));
924 static void back_to_previous_line_start P_ ((struct it *));
925 static int forward_to_next_line_start P_ ((struct it *, int *));
926 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
927 Lisp_Object, int));
928 static struct text_pos string_pos P_ ((int, Lisp_Object));
929 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
930 static int number_of_chars P_ ((unsigned char *, int));
931 static void compute_stop_pos P_ ((struct it *));
932 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
933 Lisp_Object));
934 static int face_before_or_after_it_pos P_ ((struct it *, int));
935 static int next_overlay_change P_ ((int));
936 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
937 Lisp_Object, struct text_pos *,
938 int));
939 static int underlying_face_id P_ ((struct it *));
940 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
941 struct window *));
943 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
944 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
946 #ifdef HAVE_WINDOW_SYSTEM
948 static void update_tool_bar P_ ((struct frame *, int));
949 static void build_desired_tool_bar_string P_ ((struct frame *f));
950 static int redisplay_tool_bar P_ ((struct frame *));
951 static void display_tool_bar_line P_ ((struct it *));
952 static void notice_overwritten_cursor P_ ((struct window *,
953 enum glyph_row_area,
954 int, int, int, int));
958 #endif /* HAVE_WINDOW_SYSTEM */
961 /***********************************************************************
962 Window display dimensions
963 ***********************************************************************/
965 /* Return the bottom boundary y-position for text lines in window W.
966 This is the first y position at which a line cannot start.
967 It is relative to the top of the window.
969 This is the height of W minus the height of a mode line, if any. */
971 INLINE int
972 window_text_bottom_y (w)
973 struct window *w;
975 int height = WINDOW_TOTAL_HEIGHT (w);
977 if (WINDOW_WANTS_MODELINE_P (w))
978 height -= CURRENT_MODE_LINE_HEIGHT (w);
979 return height;
982 /* Return the pixel width of display area AREA of window W. AREA < 0
983 means return the total width of W, not including fringes to
984 the left and right of the window. */
986 INLINE int
987 window_box_width (w, area)
988 struct window *w;
989 int area;
991 int cols = XFASTINT (w->total_cols);
992 int pixels = 0;
994 if (!w->pseudo_window_p)
996 cols -= WINDOW_SCROLL_BAR_COLS (w);
998 if (area == TEXT_AREA)
1000 if (INTEGERP (w->left_margin_cols))
1001 cols -= XFASTINT (w->left_margin_cols);
1002 if (INTEGERP (w->right_margin_cols))
1003 cols -= XFASTINT (w->right_margin_cols);
1004 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1006 else if (area == LEFT_MARGIN_AREA)
1008 cols = (INTEGERP (w->left_margin_cols)
1009 ? XFASTINT (w->left_margin_cols) : 0);
1010 pixels = 0;
1012 else if (area == RIGHT_MARGIN_AREA)
1014 cols = (INTEGERP (w->right_margin_cols)
1015 ? XFASTINT (w->right_margin_cols) : 0);
1016 pixels = 0;
1020 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1024 /* Return the pixel height of the display area of window W, not
1025 including mode lines of W, if any. */
1027 INLINE int
1028 window_box_height (w)
1029 struct window *w;
1031 struct frame *f = XFRAME (w->frame);
1032 int height = WINDOW_TOTAL_HEIGHT (w);
1034 xassert (height >= 0);
1036 /* Note: the code below that determines the mode-line/header-line
1037 height is essentially the same as that contained in the macro
1038 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1039 the appropriate glyph row has its `mode_line_p' flag set,
1040 and if it doesn't, uses estimate_mode_line_height instead. */
1042 if (WINDOW_WANTS_MODELINE_P (w))
1044 struct glyph_row *ml_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (ml_row && ml_row->mode_line_p)
1049 height -= ml_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1054 if (WINDOW_WANTS_HEADER_LINE_P (w))
1056 struct glyph_row *hl_row
1057 = (w->current_matrix && w->current_matrix->rows
1058 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1059 : 0);
1060 if (hl_row && hl_row->mode_line_p)
1061 height -= hl_row->height;
1062 else
1063 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1066 /* With a very small font and a mode-line that's taller than
1067 default, we might end up with a negative height. */
1068 return max (0, height);
1071 /* Return the window-relative coordinate of the left edge of display
1072 area AREA of window W. AREA < 0 means return the left edge of the
1073 whole window, to the right of the left fringe of W. */
1075 INLINE int
1076 window_box_left_offset (w, area)
1077 struct window *w;
1078 int area;
1080 int x;
1082 if (w->pseudo_window_p)
1083 return 0;
1085 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1087 if (area == TEXT_AREA)
1088 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1089 + window_box_width (w, LEFT_MARGIN_AREA));
1090 else if (area == RIGHT_MARGIN_AREA)
1091 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1092 + window_box_width (w, LEFT_MARGIN_AREA)
1093 + window_box_width (w, TEXT_AREA)
1094 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1096 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1097 else if (area == LEFT_MARGIN_AREA
1098 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1099 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1101 return x;
1105 /* Return the window-relative coordinate of the right edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the left of the right fringe of W. */
1109 INLINE int
1110 window_box_right_offset (w, area)
1111 struct window *w;
1112 int area;
1114 return window_box_left_offset (w, area) + window_box_width (w, area);
1117 /* Return the frame-relative coordinate of the left edge of display
1118 area AREA of window W. AREA < 0 means return the left edge of the
1119 whole window, to the right of the left fringe of W. */
1121 INLINE int
1122 window_box_left (w, area)
1123 struct window *w;
1124 int area;
1126 struct frame *f = XFRAME (w->frame);
1127 int x;
1129 if (w->pseudo_window_p)
1130 return FRAME_INTERNAL_BORDER_WIDTH (f);
1132 x = (WINDOW_LEFT_EDGE_X (w)
1133 + window_box_left_offset (w, area));
1135 return x;
1139 /* Return the frame-relative coordinate of the right edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the left of the right fringe of W. */
1143 INLINE int
1144 window_box_right (w, area)
1145 struct window *w;
1146 int area;
1148 return window_box_left (w, area) + window_box_width (w, area);
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines, in frame-relative coordinates. AREA < 0 means the
1153 whole window, not including the left and right fringes of
1154 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1155 coordinates of the upper-left corner of the box. Return in
1156 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1158 INLINE void
1159 window_box (w, area, box_x, box_y, box_width, box_height)
1160 struct window *w;
1161 int area;
1162 int *box_x, *box_y, *box_width, *box_height;
1164 if (box_width)
1165 *box_width = window_box_width (w, area);
1166 if (box_height)
1167 *box_height = window_box_height (w);
1168 if (box_x)
1169 *box_x = window_box_left (w, area);
1170 if (box_y)
1172 *box_y = WINDOW_TOP_EDGE_Y (w);
1173 if (WINDOW_WANTS_HEADER_LINE_P (w))
1174 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1179 /* Get the bounding box of the display area AREA of window W, without
1180 mode lines. AREA < 0 means the whole window, not including the
1181 left and right fringe of the window. Return in *TOP_LEFT_X
1182 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1183 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1184 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1185 box. */
1187 INLINE void
1188 window_box_edges (w, area, top_left_x, top_left_y,
1189 bottom_right_x, bottom_right_y)
1190 struct window *w;
1191 int area;
1192 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1194 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1195 bottom_right_y);
1196 *bottom_right_x += *top_left_x;
1197 *bottom_right_y += *top_left_y;
1202 /***********************************************************************
1203 Utilities
1204 ***********************************************************************/
1206 /* Return the bottom y-position of the line the iterator IT is in.
1207 This can modify IT's settings. */
1210 line_bottom_y (it)
1211 struct it *it;
1213 int line_height = it->max_ascent + it->max_descent;
1214 int line_top_y = it->current_y;
1216 if (line_height == 0)
1218 if (last_height)
1219 line_height = last_height;
1220 else if (IT_CHARPOS (*it) < ZV)
1222 move_it_by_lines (it, 1, 1);
1223 line_height = (it->max_ascent || it->max_descent
1224 ? it->max_ascent + it->max_descent
1225 : last_height);
1227 else
1229 struct glyph_row *row = it->glyph_row;
1231 /* Use the default character height. */
1232 it->glyph_row = NULL;
1233 it->what = IT_CHARACTER;
1234 it->c = ' ';
1235 it->len = 1;
1236 PRODUCE_GLYPHS (it);
1237 line_height = it->ascent + it->descent;
1238 it->glyph_row = row;
1242 return line_top_y + line_height;
1246 /* Return 1 if position CHARPOS is visible in window W.
1247 If visible, set *X and *Y to pixel coordinates of top left corner.
1248 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1249 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1250 and header-lines heights. */
1253 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1254 struct window *w;
1255 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1257 struct it it;
1258 struct text_pos top;
1259 int visible_p = 0;
1260 struct buffer *old_buffer = NULL;
1262 if (noninteractive)
1263 return visible_p;
1265 if (XBUFFER (w->buffer) != current_buffer)
1267 old_buffer = current_buffer;
1268 set_buffer_internal_1 (XBUFFER (w->buffer));
1271 SET_TEXT_POS_FROM_MARKER (top, w->start);
1273 /* Compute exact mode line heights, if requested. */
1274 if (exact_mode_line_heights_p)
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 current_buffer->mode_line_format);
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 current_buffer->header_line_format);
1287 start_display (&it, w, top);
1288 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1289 MOVE_TO_POS | MOVE_TO_Y);
1291 /* Note that we may overshoot because of invisible text. */
1292 if (IT_CHARPOS (it) >= charpos)
1294 int top_y = it.current_y;
1295 int bottom_y = (last_height = 0, line_bottom_y (&it));
1296 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1298 if (top_y < window_top_y)
1299 visible_p = bottom_y > window_top_y;
1300 else if (top_y < it.last_visible_y)
1301 visible_p = 1;
1302 if (visible_p && x)
1304 *x = it.current_x;
1305 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1306 if (rtop)
1308 *rtop = max (0, window_top_y - top_y);
1309 *rbot = max (0, bottom_y - it.last_visible_y);
1313 else
1315 struct it it2;
1317 it2 = it;
1318 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1319 move_it_by_lines (&it, 1, 0);
1320 if (charpos < IT_CHARPOS (it))
1322 visible_p = 1;
1323 if (x)
1325 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1326 *x = it2.current_x;
1327 *y = it2.current_y + it2.max_ascent - it2.ascent;
1328 if (rtop)
1330 *rtop = max (0, -it2.current_y);
1331 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1332 - it.last_visible_y));
1338 if (old_buffer)
1339 set_buffer_internal_1 (old_buffer);
1341 current_header_line_height = current_mode_line_height = -1;
1343 return visible_p;
1347 /* Return the next character from STR which is MAXLEN bytes long.
1348 Return in *LEN the length of the character. This is like
1349 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1350 we find one, we return a `?', but with the length of the invalid
1351 character. */
1353 static INLINE int
1354 string_char_and_length (str, maxlen, len)
1355 const unsigned char *str;
1356 int maxlen, *len;
1358 int c;
1360 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1361 if (!CHAR_VALID_P (c, 1))
1362 /* We may not change the length here because other places in Emacs
1363 don't use this function, i.e. they silently accept invalid
1364 characters. */
1365 c = '?';
1367 return c;
1372 /* Given a position POS containing a valid character and byte position
1373 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1375 static struct text_pos
1376 string_pos_nchars_ahead (pos, string, nchars)
1377 struct text_pos pos;
1378 Lisp_Object string;
1379 int nchars;
1381 xassert (STRINGP (string) && nchars >= 0);
1383 if (STRING_MULTIBYTE (string))
1385 int rest = SBYTES (string) - BYTEPOS (pos);
1386 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1387 int len;
1389 while (nchars--)
1391 string_char_and_length (p, rest, &len);
1392 p += len, rest -= len;
1393 xassert (rest >= 0);
1394 CHARPOS (pos) += 1;
1395 BYTEPOS (pos) += len;
1398 else
1399 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1401 return pos;
1405 /* Value is the text position, i.e. character and byte position,
1406 for character position CHARPOS in STRING. */
1408 static INLINE struct text_pos
1409 string_pos (charpos, string)
1410 int charpos;
1411 Lisp_Object string;
1413 struct text_pos pos;
1414 xassert (STRINGP (string));
1415 xassert (charpos >= 0);
1416 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1417 return pos;
1421 /* Value is a text position, i.e. character and byte position, for
1422 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1423 means recognize multibyte characters. */
1425 static struct text_pos
1426 c_string_pos (charpos, s, multibyte_p)
1427 int charpos;
1428 unsigned char *s;
1429 int multibyte_p;
1431 struct text_pos pos;
1433 xassert (s != NULL);
1434 xassert (charpos >= 0);
1436 if (multibyte_p)
1438 int rest = strlen (s), len;
1440 SET_TEXT_POS (pos, 0, 0);
1441 while (charpos--)
1443 string_char_and_length (s, rest, &len);
1444 s += len, rest -= len;
1445 xassert (rest >= 0);
1446 CHARPOS (pos) += 1;
1447 BYTEPOS (pos) += len;
1450 else
1451 SET_TEXT_POS (pos, charpos, charpos);
1453 return pos;
1457 /* Value is the number of characters in C string S. MULTIBYTE_P
1458 non-zero means recognize multibyte characters. */
1460 static int
1461 number_of_chars (s, multibyte_p)
1462 unsigned char *s;
1463 int multibyte_p;
1465 int nchars;
1467 if (multibyte_p)
1469 int rest = strlen (s), len;
1470 unsigned char *p = (unsigned char *) s;
1472 for (nchars = 0; rest > 0; ++nchars)
1474 string_char_and_length (p, rest, &len);
1475 rest -= len, p += len;
1478 else
1479 nchars = strlen (s);
1481 return nchars;
1485 /* Compute byte position NEWPOS->bytepos corresponding to
1486 NEWPOS->charpos. POS is a known position in string STRING.
1487 NEWPOS->charpos must be >= POS.charpos. */
1489 static void
1490 compute_string_pos (newpos, pos, string)
1491 struct text_pos *newpos, pos;
1492 Lisp_Object string;
1494 xassert (STRINGP (string));
1495 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1497 if (STRING_MULTIBYTE (string))
1498 *newpos = string_pos_nchars_ahead (pos, string,
1499 CHARPOS (*newpos) - CHARPOS (pos));
1500 else
1501 BYTEPOS (*newpos) = CHARPOS (*newpos);
1504 /* EXPORT:
1505 Return an estimation of the pixel height of mode or top lines on
1506 frame F. FACE_ID specifies what line's height to estimate. */
1509 estimate_mode_line_height (f, face_id)
1510 struct frame *f;
1511 enum face_id face_id;
1513 #ifdef HAVE_WINDOW_SYSTEM
1514 if (FRAME_WINDOW_P (f))
1516 int height = FONT_HEIGHT (FRAME_FONT (f));
1518 /* This function is called so early when Emacs starts that the face
1519 cache and mode line face are not yet initialized. */
1520 if (FRAME_FACE_CACHE (f))
1522 struct face *face = FACE_FROM_ID (f, face_id);
1523 if (face)
1525 if (face->font)
1526 height = FONT_HEIGHT (face->font);
1527 if (face->box_line_width > 0)
1528 height += 2 * face->box_line_width;
1532 return height;
1534 #endif
1536 return 1;
1539 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1540 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1541 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1542 not force the value into range. */
1544 void
1545 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1546 FRAME_PTR f;
1547 register int pix_x, pix_y;
1548 int *x, *y;
1549 NativeRectangle *bounds;
1550 int noclip;
1553 #ifdef HAVE_WINDOW_SYSTEM
1554 if (FRAME_WINDOW_P (f))
1556 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1557 even for negative values. */
1558 if (pix_x < 0)
1559 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1560 if (pix_y < 0)
1561 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1563 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1564 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1566 if (bounds)
1567 STORE_NATIVE_RECT (*bounds,
1568 FRAME_COL_TO_PIXEL_X (f, pix_x),
1569 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1570 FRAME_COLUMN_WIDTH (f) - 1,
1571 FRAME_LINE_HEIGHT (f) - 1);
1573 if (!noclip)
1575 if (pix_x < 0)
1576 pix_x = 0;
1577 else if (pix_x > FRAME_TOTAL_COLS (f))
1578 pix_x = FRAME_TOTAL_COLS (f);
1580 if (pix_y < 0)
1581 pix_y = 0;
1582 else if (pix_y > FRAME_LINES (f))
1583 pix_y = FRAME_LINES (f);
1586 #endif
1588 *x = pix_x;
1589 *y = pix_y;
1593 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1594 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1595 can't tell the positions because W's display is not up to date,
1596 return 0. */
1599 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1600 struct window *w;
1601 int hpos, vpos;
1602 int *frame_x, *frame_y;
1604 #ifdef HAVE_WINDOW_SYSTEM
1605 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1607 int success_p;
1609 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1610 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1612 if (display_completed)
1614 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1615 struct glyph *glyph = row->glyphs[TEXT_AREA];
1616 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1618 hpos = row->x;
1619 vpos = row->y;
1620 while (glyph < end)
1622 hpos += glyph->pixel_width;
1623 ++glyph;
1626 /* If first glyph is partially visible, its first visible position is still 0. */
1627 if (hpos < 0)
1628 hpos = 0;
1630 success_p = 1;
1632 else
1634 hpos = vpos = 0;
1635 success_p = 0;
1638 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1639 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1640 return success_p;
1642 #endif
1644 *frame_x = hpos;
1645 *frame_y = vpos;
1646 return 1;
1650 #ifdef HAVE_WINDOW_SYSTEM
1652 /* Find the glyph under window-relative coordinates X/Y in window W.
1653 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1654 strings. Return in *HPOS and *VPOS the row and column number of
1655 the glyph found. Return in *AREA the glyph area containing X.
1656 Value is a pointer to the glyph found or null if X/Y is not on
1657 text, or we can't tell because W's current matrix is not up to
1658 date. */
1660 static struct glyph *
1661 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1662 struct window *w;
1663 int x, y;
1664 int *hpos, *vpos, *dx, *dy, *area;
1666 struct glyph *glyph, *end;
1667 struct glyph_row *row = NULL;
1668 int x0, i;
1670 /* Find row containing Y. Give up if some row is not enabled. */
1671 for (i = 0; i < w->current_matrix->nrows; ++i)
1673 row = MATRIX_ROW (w->current_matrix, i);
1674 if (!row->enabled_p)
1675 return NULL;
1676 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1677 break;
1680 *vpos = i;
1681 *hpos = 0;
1683 /* Give up if Y is not in the window. */
1684 if (i == w->current_matrix->nrows)
1685 return NULL;
1687 /* Get the glyph area containing X. */
1688 if (w->pseudo_window_p)
1690 *area = TEXT_AREA;
1691 x0 = 0;
1693 else
1695 if (x < window_box_left_offset (w, TEXT_AREA))
1697 *area = LEFT_MARGIN_AREA;
1698 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1700 else if (x < window_box_right_offset (w, TEXT_AREA))
1702 *area = TEXT_AREA;
1703 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1705 else
1707 *area = RIGHT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1712 /* Find glyph containing X. */
1713 glyph = row->glyphs[*area];
1714 end = glyph + row->used[*area];
1715 x -= x0;
1716 while (glyph < end && x >= glyph->pixel_width)
1718 x -= glyph->pixel_width;
1719 ++glyph;
1722 if (glyph == end)
1723 return NULL;
1725 if (dx)
1727 *dx = x;
1728 *dy = y - (row->y + row->ascent - glyph->ascent);
1731 *hpos = glyph - row->glyphs[*area];
1732 return glyph;
1736 /* EXPORT:
1737 Convert frame-relative x/y to coordinates relative to window W.
1738 Takes pseudo-windows into account. */
1740 void
1741 frame_to_window_pixel_xy (w, x, y)
1742 struct window *w;
1743 int *x, *y;
1745 if (w->pseudo_window_p)
1747 /* A pseudo-window is always full-width, and starts at the
1748 left edge of the frame, plus a frame border. */
1749 struct frame *f = XFRAME (w->frame);
1750 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1751 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1753 else
1755 *x -= WINDOW_LEFT_EDGE_X (w);
1756 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1760 /* EXPORT:
1761 Return in *R the clipping rectangle for glyph string S. */
1763 void
1764 get_glyph_string_clip_rect (s, nr)
1765 struct glyph_string *s;
1766 NativeRectangle *nr;
1768 XRectangle r;
1770 if (s->row->full_width_p)
1772 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1773 r.x = WINDOW_LEFT_EDGE_X (s->w);
1774 r.width = WINDOW_TOTAL_WIDTH (s->w);
1776 /* Unless displaying a mode or menu bar line, which are always
1777 fully visible, clip to the visible part of the row. */
1778 if (s->w->pseudo_window_p)
1779 r.height = s->row->visible_height;
1780 else
1781 r.height = s->height;
1783 else
1785 /* This is a text line that may be partially visible. */
1786 r.x = window_box_left (s->w, s->area);
1787 r.width = window_box_width (s->w, s->area);
1788 r.height = s->row->visible_height;
1791 if (s->clip_head)
1792 if (r.x < s->clip_head->x)
1794 if (r.width >= s->clip_head->x - r.x)
1795 r.width -= s->clip_head->x - r.x;
1796 else
1797 r.width = 0;
1798 r.x = s->clip_head->x;
1800 if (s->clip_tail)
1801 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1803 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1804 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1805 else
1806 r.width = 0;
1809 /* If S draws overlapping rows, it's sufficient to use the top and
1810 bottom of the window for clipping because this glyph string
1811 intentionally draws over other lines. */
1812 if (s->for_overlaps_p)
1814 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1815 r.height = window_text_bottom_y (s->w) - r.y;
1817 else
1819 /* Don't use S->y for clipping because it doesn't take partially
1820 visible lines into account. For example, it can be negative for
1821 partially visible lines at the top of a window. */
1822 if (!s->row->full_width_p
1823 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1824 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1825 else
1826 r.y = max (0, s->row->y);
1828 /* If drawing a tool-bar window, draw it over the internal border
1829 at the top of the window. */
1830 if (WINDOWP (s->f->tool_bar_window)
1831 && s->w == XWINDOW (s->f->tool_bar_window))
1832 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1835 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1837 /* If drawing the cursor, don't let glyph draw outside its
1838 advertised boundaries. Cleartype does this under some circumstances. */
1839 if (s->hl == DRAW_CURSOR)
1841 struct glyph *glyph = s->first_glyph;
1842 int height, max_y;
1844 if (s->x > r.x)
1846 r.width -= s->x - r.x;
1847 r.x = s->x;
1849 r.width = min (r.width, glyph->pixel_width);
1851 /* If r.y is below window bottom, ensure that we still see a cursor. */
1852 height = min (glyph->ascent + glyph->descent,
1853 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1854 max_y = window_text_bottom_y (s->w) - height;
1855 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1856 if (s->ybase - glyph->ascent > max_y)
1858 r.y = max_y;
1859 r.height = height;
1861 else
1863 /* Don't draw cursor glyph taller than our actual glyph. */
1864 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1865 if (height < r.height)
1867 max_y = r.y + r.height;
1868 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1869 r.height = min (max_y - r.y, height);
1874 #ifdef CONVERT_FROM_XRECT
1875 CONVERT_FROM_XRECT (r, *nr);
1876 #else
1877 *nr = r;
1878 #endif
1882 /* EXPORT:
1883 Return the position and height of the phys cursor in window W.
1884 Set w->phys_cursor_width to width of phys cursor.
1888 get_phys_cursor_geometry (w, row, glyph, heightp)
1889 struct window *w;
1890 struct glyph_row *row;
1891 struct glyph *glyph;
1892 int *heightp;
1894 struct frame *f = XFRAME (WINDOW_FRAME (w));
1895 int x, y, wd, h, h0, y0;
1897 /* Compute the width of the rectangle to draw. If on a stretch
1898 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1899 rectangle as wide as the glyph, but use a canonical character
1900 width instead. */
1901 wd = glyph->pixel_width - 1;
1902 #ifdef HAVE_NTGUI
1903 wd++; /* Why? */
1904 #endif
1905 if (glyph->type == STRETCH_GLYPH
1906 && !x_stretch_cursor_p)
1907 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1908 w->phys_cursor_width = wd;
1910 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1912 /* If y is below window bottom, ensure that we still see a cursor. */
1913 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1915 h = max (h0, glyph->ascent + glyph->descent);
1916 h0 = min (h0, glyph->ascent + glyph->descent);
1918 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1919 if (y < y0)
1921 h = max (h - (y0 - y) + 1, h0);
1922 y = y0 - 1;
1924 else
1926 y0 = window_text_bottom_y (w) - h0;
1927 if (y > y0)
1929 h += y - y0;
1930 y = y0;
1934 *heightp = h - 1;
1935 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1939 #endif /* HAVE_WINDOW_SYSTEM */
1942 /***********************************************************************
1943 Lisp form evaluation
1944 ***********************************************************************/
1946 /* Error handler for safe_eval and safe_call. */
1948 static Lisp_Object
1949 safe_eval_handler (arg)
1950 Lisp_Object arg;
1952 add_to_log ("Error during redisplay: %s", arg, Qnil);
1953 return Qnil;
1957 /* Evaluate SEXPR and return the result, or nil if something went
1958 wrong. Prevent redisplay during the evaluation. */
1960 Lisp_Object
1961 safe_eval (sexpr)
1962 Lisp_Object sexpr;
1964 Lisp_Object val;
1966 if (inhibit_eval_during_redisplay)
1967 val = Qnil;
1968 else
1970 int count = SPECPDL_INDEX ();
1971 struct gcpro gcpro1;
1973 GCPRO1 (sexpr);
1974 specbind (Qinhibit_redisplay, Qt);
1975 /* Use Qt to ensure debugger does not run,
1976 so there is no possibility of wanting to redisplay. */
1977 val = internal_condition_case_1 (Feval, sexpr, Qt,
1978 safe_eval_handler);
1979 UNGCPRO;
1980 val = unbind_to (count, val);
1983 return val;
1987 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1988 Return the result, or nil if something went wrong. Prevent
1989 redisplay during the evaluation. */
1991 Lisp_Object
1992 safe_call (nargs, args)
1993 int nargs;
1994 Lisp_Object *args;
1996 Lisp_Object val;
1998 if (inhibit_eval_during_redisplay)
1999 val = Qnil;
2000 else
2002 int count = SPECPDL_INDEX ();
2003 struct gcpro gcpro1;
2005 GCPRO1 (args[0]);
2006 gcpro1.nvars = nargs;
2007 specbind (Qinhibit_redisplay, Qt);
2008 /* Use Qt to ensure debugger does not run,
2009 so there is no possibility of wanting to redisplay. */
2010 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2011 safe_eval_handler);
2012 UNGCPRO;
2013 val = unbind_to (count, val);
2016 return val;
2020 /* Call function FN with one argument ARG.
2021 Return the result, or nil if something went wrong. */
2023 Lisp_Object
2024 safe_call1 (fn, arg)
2025 Lisp_Object fn, arg;
2027 Lisp_Object args[2];
2028 args[0] = fn;
2029 args[1] = arg;
2030 return safe_call (2, args);
2035 /***********************************************************************
2036 Debugging
2037 ***********************************************************************/
2039 #if 0
2041 /* Define CHECK_IT to perform sanity checks on iterators.
2042 This is for debugging. It is too slow to do unconditionally. */
2044 static void
2045 check_it (it)
2046 struct it *it;
2048 if (it->method == GET_FROM_STRING)
2050 xassert (STRINGP (it->string));
2051 xassert (IT_STRING_CHARPOS (*it) >= 0);
2053 else
2055 xassert (IT_STRING_CHARPOS (*it) < 0);
2056 if (it->method == GET_FROM_BUFFER)
2058 /* Check that character and byte positions agree. */
2059 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2063 if (it->dpvec)
2064 xassert (it->current.dpvec_index >= 0);
2065 else
2066 xassert (it->current.dpvec_index < 0);
2069 #define CHECK_IT(IT) check_it ((IT))
2071 #else /* not 0 */
2073 #define CHECK_IT(IT) (void) 0
2075 #endif /* not 0 */
2078 #if GLYPH_DEBUG
2080 /* Check that the window end of window W is what we expect it
2081 to be---the last row in the current matrix displaying text. */
2083 static void
2084 check_window_end (w)
2085 struct window *w;
2087 if (!MINI_WINDOW_P (w)
2088 && !NILP (w->window_end_valid))
2090 struct glyph_row *row;
2091 xassert ((row = MATRIX_ROW (w->current_matrix,
2092 XFASTINT (w->window_end_vpos)),
2093 !row->enabled_p
2094 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2095 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2099 #define CHECK_WINDOW_END(W) check_window_end ((W))
2101 #else /* not GLYPH_DEBUG */
2103 #define CHECK_WINDOW_END(W) (void) 0
2105 #endif /* not GLYPH_DEBUG */
2109 /***********************************************************************
2110 Iterator initialization
2111 ***********************************************************************/
2113 /* Initialize IT for displaying current_buffer in window W, starting
2114 at character position CHARPOS. CHARPOS < 0 means that no buffer
2115 position is specified which is useful when the iterator is assigned
2116 a position later. BYTEPOS is the byte position corresponding to
2117 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2119 If ROW is not null, calls to produce_glyphs with IT as parameter
2120 will produce glyphs in that row.
2122 BASE_FACE_ID is the id of a base face to use. It must be one of
2123 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2124 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2125 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2127 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2128 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2129 will be initialized to use the corresponding mode line glyph row of
2130 the desired matrix of W. */
2132 void
2133 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2134 struct it *it;
2135 struct window *w;
2136 int charpos, bytepos;
2137 struct glyph_row *row;
2138 enum face_id base_face_id;
2140 int highlight_region_p;
2142 /* Some precondition checks. */
2143 xassert (w != NULL && it != NULL);
2144 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2145 && charpos <= ZV));
2147 /* If face attributes have been changed since the last redisplay,
2148 free realized faces now because they depend on face definitions
2149 that might have changed. Don't free faces while there might be
2150 desired matrices pending which reference these faces. */
2151 if (face_change_count && !inhibit_free_realized_faces)
2153 face_change_count = 0;
2154 free_all_realized_faces (Qnil);
2157 /* Use one of the mode line rows of W's desired matrix if
2158 appropriate. */
2159 if (row == NULL)
2161 if (base_face_id == MODE_LINE_FACE_ID
2162 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2163 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2164 else if (base_face_id == HEADER_LINE_FACE_ID)
2165 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2168 /* Clear IT. */
2169 bzero (it, sizeof *it);
2170 it->current.overlay_string_index = -1;
2171 it->current.dpvec_index = -1;
2172 it->base_face_id = base_face_id;
2173 it->string = Qnil;
2174 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2176 /* The window in which we iterate over current_buffer: */
2177 XSETWINDOW (it->window, w);
2178 it->w = w;
2179 it->f = XFRAME (w->frame);
2181 /* Extra space between lines (on window systems only). */
2182 if (base_face_id == DEFAULT_FACE_ID
2183 && FRAME_WINDOW_P (it->f))
2185 if (NATNUMP (current_buffer->extra_line_spacing))
2186 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2187 else if (FLOATP (current_buffer->extra_line_spacing))
2188 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2189 * FRAME_LINE_HEIGHT (it->f));
2190 else if (it->f->extra_line_spacing > 0)
2191 it->extra_line_spacing = it->f->extra_line_spacing;
2192 it->max_extra_line_spacing = 0;
2195 /* If realized faces have been removed, e.g. because of face
2196 attribute changes of named faces, recompute them. When running
2197 in batch mode, the face cache of Vterminal_frame is null. If
2198 we happen to get called, make a dummy face cache. */
2199 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2200 init_frame_faces (it->f);
2201 if (FRAME_FACE_CACHE (it->f)->used == 0)
2202 recompute_basic_faces (it->f);
2204 /* Current value of the `slice', `space-width', and 'height' properties. */
2205 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2206 it->space_width = Qnil;
2207 it->font_height = Qnil;
2208 it->override_ascent = -1;
2210 /* Are control characters displayed as `^C'? */
2211 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2213 /* -1 means everything between a CR and the following line end
2214 is invisible. >0 means lines indented more than this value are
2215 invisible. */
2216 it->selective = (INTEGERP (current_buffer->selective_display)
2217 ? XFASTINT (current_buffer->selective_display)
2218 : (!NILP (current_buffer->selective_display)
2219 ? -1 : 0));
2220 it->selective_display_ellipsis_p
2221 = !NILP (current_buffer->selective_display_ellipses);
2223 /* Display table to use. */
2224 it->dp = window_display_table (w);
2226 /* Are multibyte characters enabled in current_buffer? */
2227 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2229 /* Non-zero if we should highlight the region. */
2230 highlight_region_p
2231 = (!NILP (Vtransient_mark_mode)
2232 && !NILP (current_buffer->mark_active)
2233 && XMARKER (current_buffer->mark)->buffer != 0);
2235 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2236 start and end of a visible region in window IT->w. Set both to
2237 -1 to indicate no region. */
2238 if (highlight_region_p
2239 /* Maybe highlight only in selected window. */
2240 && (/* Either show region everywhere. */
2241 highlight_nonselected_windows
2242 /* Or show region in the selected window. */
2243 || w == XWINDOW (selected_window)
2244 /* Or show the region if we are in the mini-buffer and W is
2245 the window the mini-buffer refers to. */
2246 || (MINI_WINDOW_P (XWINDOW (selected_window))
2247 && WINDOWP (minibuf_selected_window)
2248 && w == XWINDOW (minibuf_selected_window))))
2250 int charpos = marker_position (current_buffer->mark);
2251 it->region_beg_charpos = min (PT, charpos);
2252 it->region_end_charpos = max (PT, charpos);
2254 else
2255 it->region_beg_charpos = it->region_end_charpos = -1;
2257 /* Get the position at which the redisplay_end_trigger hook should
2258 be run, if it is to be run at all. */
2259 if (MARKERP (w->redisplay_end_trigger)
2260 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2261 it->redisplay_end_trigger_charpos
2262 = marker_position (w->redisplay_end_trigger);
2263 else if (INTEGERP (w->redisplay_end_trigger))
2264 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2266 /* Correct bogus values of tab_width. */
2267 it->tab_width = XINT (current_buffer->tab_width);
2268 if (it->tab_width <= 0 || it->tab_width > 1000)
2269 it->tab_width = 8;
2271 /* Are lines in the display truncated? */
2272 it->truncate_lines_p
2273 = (base_face_id != DEFAULT_FACE_ID
2274 || XINT (it->w->hscroll)
2275 || (truncate_partial_width_windows
2276 && !WINDOW_FULL_WIDTH_P (it->w))
2277 || !NILP (current_buffer->truncate_lines));
2279 /* Get dimensions of truncation and continuation glyphs. These are
2280 displayed as fringe bitmaps under X, so we don't need them for such
2281 frames. */
2282 if (!FRAME_WINDOW_P (it->f))
2284 if (it->truncate_lines_p)
2286 /* We will need the truncation glyph. */
2287 xassert (it->glyph_row == NULL);
2288 produce_special_glyphs (it, IT_TRUNCATION);
2289 it->truncation_pixel_width = it->pixel_width;
2291 else
2293 /* We will need the continuation glyph. */
2294 xassert (it->glyph_row == NULL);
2295 produce_special_glyphs (it, IT_CONTINUATION);
2296 it->continuation_pixel_width = it->pixel_width;
2299 /* Reset these values to zero because the produce_special_glyphs
2300 above has changed them. */
2301 it->pixel_width = it->ascent = it->descent = 0;
2302 it->phys_ascent = it->phys_descent = 0;
2305 /* Set this after getting the dimensions of truncation and
2306 continuation glyphs, so that we don't produce glyphs when calling
2307 produce_special_glyphs, above. */
2308 it->glyph_row = row;
2309 it->area = TEXT_AREA;
2311 /* Get the dimensions of the display area. The display area
2312 consists of the visible window area plus a horizontally scrolled
2313 part to the left of the window. All x-values are relative to the
2314 start of this total display area. */
2315 if (base_face_id != DEFAULT_FACE_ID)
2317 /* Mode lines, menu bar in terminal frames. */
2318 it->first_visible_x = 0;
2319 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2321 else
2323 it->first_visible_x
2324 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2325 it->last_visible_x = (it->first_visible_x
2326 + window_box_width (w, TEXT_AREA));
2328 /* If we truncate lines, leave room for the truncator glyph(s) at
2329 the right margin. Otherwise, leave room for the continuation
2330 glyph(s). Truncation and continuation glyphs are not inserted
2331 for window-based redisplay. */
2332 if (!FRAME_WINDOW_P (it->f))
2334 if (it->truncate_lines_p)
2335 it->last_visible_x -= it->truncation_pixel_width;
2336 else
2337 it->last_visible_x -= it->continuation_pixel_width;
2340 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2341 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2344 /* Leave room for a border glyph. */
2345 if (!FRAME_WINDOW_P (it->f)
2346 && !WINDOW_RIGHTMOST_P (it->w))
2347 it->last_visible_x -= 1;
2349 it->last_visible_y = window_text_bottom_y (w);
2351 /* For mode lines and alike, arrange for the first glyph having a
2352 left box line if the face specifies a box. */
2353 if (base_face_id != DEFAULT_FACE_ID)
2355 struct face *face;
2357 it->face_id = base_face_id;
2359 /* If we have a boxed mode line, make the first character appear
2360 with a left box line. */
2361 face = FACE_FROM_ID (it->f, base_face_id);
2362 if (face->box != FACE_NO_BOX)
2363 it->start_of_box_run_p = 1;
2366 /* If a buffer position was specified, set the iterator there,
2367 getting overlays and face properties from that position. */
2368 if (charpos >= BUF_BEG (current_buffer))
2370 it->end_charpos = ZV;
2371 it->face_id = -1;
2372 IT_CHARPOS (*it) = charpos;
2374 /* Compute byte position if not specified. */
2375 if (bytepos < charpos)
2376 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2377 else
2378 IT_BYTEPOS (*it) = bytepos;
2380 it->start = it->current;
2382 /* Compute faces etc. */
2383 reseat (it, it->current.pos, 1);
2386 CHECK_IT (it);
2390 /* Initialize IT for the display of window W with window start POS. */
2392 void
2393 start_display (it, w, pos)
2394 struct it *it;
2395 struct window *w;
2396 struct text_pos pos;
2398 struct glyph_row *row;
2399 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2401 row = w->desired_matrix->rows + first_vpos;
2402 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2403 it->first_vpos = first_vpos;
2405 if (!it->truncate_lines_p)
2407 int start_at_line_beg_p;
2408 int first_y = it->current_y;
2410 /* If window start is not at a line start, skip forward to POS to
2411 get the correct continuation lines width. */
2412 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2413 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2414 if (!start_at_line_beg_p)
2416 int new_x;
2418 reseat_at_previous_visible_line_start (it);
2419 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2421 new_x = it->current_x + it->pixel_width;
2423 /* If lines are continued, this line may end in the middle
2424 of a multi-glyph character (e.g. a control character
2425 displayed as \003, or in the middle of an overlay
2426 string). In this case move_it_to above will not have
2427 taken us to the start of the continuation line but to the
2428 end of the continued line. */
2429 if (it->current_x > 0
2430 && !it->truncate_lines_p /* Lines are continued. */
2431 && (/* And glyph doesn't fit on the line. */
2432 new_x > it->last_visible_x
2433 /* Or it fits exactly and we're on a window
2434 system frame. */
2435 || (new_x == it->last_visible_x
2436 && FRAME_WINDOW_P (it->f))))
2438 if (it->current.dpvec_index >= 0
2439 || it->current.overlay_string_index >= 0)
2441 set_iterator_to_next (it, 1);
2442 move_it_in_display_line_to (it, -1, -1, 0);
2445 it->continuation_lines_width += it->current_x;
2448 /* We're starting a new display line, not affected by the
2449 height of the continued line, so clear the appropriate
2450 fields in the iterator structure. */
2451 it->max_ascent = it->max_descent = 0;
2452 it->max_phys_ascent = it->max_phys_descent = 0;
2454 it->current_y = first_y;
2455 it->vpos = 0;
2456 it->current_x = it->hpos = 0;
2460 #if 0 /* Don't assert the following because start_display is sometimes
2461 called intentionally with a window start that is not at a
2462 line start. Please leave this code in as a comment. */
2464 /* Window start should be on a line start, now. */
2465 xassert (it->continuation_lines_width
2466 || IT_CHARPOS (it) == BEGV
2467 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2468 #endif /* 0 */
2472 /* Return 1 if POS is a position in ellipses displayed for invisible
2473 text. W is the window we display, for text property lookup. */
2475 static int
2476 in_ellipses_for_invisible_text_p (pos, w)
2477 struct display_pos *pos;
2478 struct window *w;
2480 Lisp_Object prop, window;
2481 int ellipses_p = 0;
2482 int charpos = CHARPOS (pos->pos);
2484 /* If POS specifies a position in a display vector, this might
2485 be for an ellipsis displayed for invisible text. We won't
2486 get the iterator set up for delivering that ellipsis unless
2487 we make sure that it gets aware of the invisible text. */
2488 if (pos->dpvec_index >= 0
2489 && pos->overlay_string_index < 0
2490 && CHARPOS (pos->string_pos) < 0
2491 && charpos > BEGV
2492 && (XSETWINDOW (window, w),
2493 prop = Fget_char_property (make_number (charpos),
2494 Qinvisible, window),
2495 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2497 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2498 window);
2499 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2502 return ellipses_p;
2506 /* Initialize IT for stepping through current_buffer in window W,
2507 starting at position POS that includes overlay string and display
2508 vector/ control character translation position information. Value
2509 is zero if there are overlay strings with newlines at POS. */
2511 static int
2512 init_from_display_pos (it, w, pos)
2513 struct it *it;
2514 struct window *w;
2515 struct display_pos *pos;
2517 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2518 int i, overlay_strings_with_newlines = 0;
2520 /* If POS specifies a position in a display vector, this might
2521 be for an ellipsis displayed for invisible text. We won't
2522 get the iterator set up for delivering that ellipsis unless
2523 we make sure that it gets aware of the invisible text. */
2524 if (in_ellipses_for_invisible_text_p (pos, w))
2526 --charpos;
2527 bytepos = 0;
2530 /* Keep in mind: the call to reseat in init_iterator skips invisible
2531 text, so we might end up at a position different from POS. This
2532 is only a problem when POS is a row start after a newline and an
2533 overlay starts there with an after-string, and the overlay has an
2534 invisible property. Since we don't skip invisible text in
2535 display_line and elsewhere immediately after consuming the
2536 newline before the row start, such a POS will not be in a string,
2537 but the call to init_iterator below will move us to the
2538 after-string. */
2539 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2541 for (i = 0; i < it->n_overlay_strings; ++i)
2543 const char *s = SDATA (it->overlay_strings[i]);
2544 const char *e = s + SBYTES (it->overlay_strings[i]);
2546 while (s < e && *s != '\n')
2547 ++s;
2549 if (s < e)
2551 overlay_strings_with_newlines = 1;
2552 break;
2556 /* If position is within an overlay string, set up IT to the right
2557 overlay string. */
2558 if (pos->overlay_string_index >= 0)
2560 int relative_index;
2562 /* If the first overlay string happens to have a `display'
2563 property for an image, the iterator will be set up for that
2564 image, and we have to undo that setup first before we can
2565 correct the overlay string index. */
2566 if (it->method == GET_FROM_IMAGE)
2567 pop_it (it);
2569 /* We already have the first chunk of overlay strings in
2570 IT->overlay_strings. Load more until the one for
2571 pos->overlay_string_index is in IT->overlay_strings. */
2572 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2574 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2575 it->current.overlay_string_index = 0;
2576 while (n--)
2578 load_overlay_strings (it, 0);
2579 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2583 it->current.overlay_string_index = pos->overlay_string_index;
2584 relative_index = (it->current.overlay_string_index
2585 % OVERLAY_STRING_CHUNK_SIZE);
2586 it->string = it->overlay_strings[relative_index];
2587 xassert (STRINGP (it->string));
2588 it->current.string_pos = pos->string_pos;
2589 it->method = GET_FROM_STRING;
2592 #if 0 /* This is bogus because POS not having an overlay string
2593 position does not mean it's after the string. Example: A
2594 line starting with a before-string and initialization of IT
2595 to the previous row's end position. */
2596 else if (it->current.overlay_string_index >= 0)
2598 /* If POS says we're already after an overlay string ending at
2599 POS, make sure to pop the iterator because it will be in
2600 front of that overlay string. When POS is ZV, we've thereby
2601 also ``processed'' overlay strings at ZV. */
2602 while (it->sp)
2603 pop_it (it);
2604 it->current.overlay_string_index = -1;
2605 it->method = GET_FROM_BUFFER;
2606 if (CHARPOS (pos->pos) == ZV)
2607 it->overlay_strings_at_end_processed_p = 1;
2609 #endif /* 0 */
2611 if (CHARPOS (pos->string_pos) >= 0)
2613 /* Recorded position is not in an overlay string, but in another
2614 string. This can only be a string from a `display' property.
2615 IT should already be filled with that string. */
2616 it->current.string_pos = pos->string_pos;
2617 xassert (STRINGP (it->string));
2620 /* Restore position in display vector translations, control
2621 character translations or ellipses. */
2622 if (pos->dpvec_index >= 0)
2624 if (it->dpvec == NULL)
2625 get_next_display_element (it);
2626 xassert (it->dpvec && it->current.dpvec_index == 0);
2627 it->current.dpvec_index = pos->dpvec_index;
2630 CHECK_IT (it);
2631 return !overlay_strings_with_newlines;
2635 /* Initialize IT for stepping through current_buffer in window W
2636 starting at ROW->start. */
2638 static void
2639 init_to_row_start (it, w, row)
2640 struct it *it;
2641 struct window *w;
2642 struct glyph_row *row;
2644 init_from_display_pos (it, w, &row->start);
2645 it->start = row->start;
2646 it->continuation_lines_width = row->continuation_lines_width;
2647 CHECK_IT (it);
2651 /* Initialize IT for stepping through current_buffer in window W
2652 starting in the line following ROW, i.e. starting at ROW->end.
2653 Value is zero if there are overlay strings with newlines at ROW's
2654 end position. */
2656 static int
2657 init_to_row_end (it, w, row)
2658 struct it *it;
2659 struct window *w;
2660 struct glyph_row *row;
2662 int success = 0;
2664 if (init_from_display_pos (it, w, &row->end))
2666 if (row->continued_p)
2667 it->continuation_lines_width
2668 = row->continuation_lines_width + row->pixel_width;
2669 CHECK_IT (it);
2670 success = 1;
2673 return success;
2679 /***********************************************************************
2680 Text properties
2681 ***********************************************************************/
2683 /* Called when IT reaches IT->stop_charpos. Handle text property and
2684 overlay changes. Set IT->stop_charpos to the next position where
2685 to stop. */
2687 static void
2688 handle_stop (it)
2689 struct it *it;
2691 enum prop_handled handled;
2692 int handle_overlay_change_p = 1;
2693 struct props *p;
2695 it->dpvec = NULL;
2696 it->current.dpvec_index = -1;
2700 handled = HANDLED_NORMALLY;
2702 /* Call text property handlers. */
2703 for (p = it_props; p->handler; ++p)
2705 handled = p->handler (it);
2707 if (handled == HANDLED_RECOMPUTE_PROPS)
2708 break;
2709 else if (handled == HANDLED_RETURN)
2710 return;
2711 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2712 handle_overlay_change_p = 0;
2715 if (handled != HANDLED_RECOMPUTE_PROPS)
2717 /* Don't check for overlay strings below when set to deliver
2718 characters from a display vector. */
2719 if (it->method == GET_FROM_DISPLAY_VECTOR)
2720 handle_overlay_change_p = 0;
2722 /* Handle overlay changes. */
2723 if (handle_overlay_change_p)
2724 handled = handle_overlay_change (it);
2726 /* Determine where to stop next. */
2727 if (handled == HANDLED_NORMALLY)
2728 compute_stop_pos (it);
2731 while (handled == HANDLED_RECOMPUTE_PROPS);
2735 /* Compute IT->stop_charpos from text property and overlay change
2736 information for IT's current position. */
2738 static void
2739 compute_stop_pos (it)
2740 struct it *it;
2742 register INTERVAL iv, next_iv;
2743 Lisp_Object object, limit, position;
2745 /* If nowhere else, stop at the end. */
2746 it->stop_charpos = it->end_charpos;
2748 if (STRINGP (it->string))
2750 /* Strings are usually short, so don't limit the search for
2751 properties. */
2752 object = it->string;
2753 limit = Qnil;
2754 position = make_number (IT_STRING_CHARPOS (*it));
2756 else
2758 int charpos;
2760 /* If next overlay change is in front of the current stop pos
2761 (which is IT->end_charpos), stop there. Note: value of
2762 next_overlay_change is point-max if no overlay change
2763 follows. */
2764 charpos = next_overlay_change (IT_CHARPOS (*it));
2765 if (charpos < it->stop_charpos)
2766 it->stop_charpos = charpos;
2768 /* If showing the region, we have to stop at the region
2769 start or end because the face might change there. */
2770 if (it->region_beg_charpos > 0)
2772 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2773 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2774 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2775 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2778 /* Set up variables for computing the stop position from text
2779 property changes. */
2780 XSETBUFFER (object, current_buffer);
2781 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2782 position = make_number (IT_CHARPOS (*it));
2786 /* Get the interval containing IT's position. Value is a null
2787 interval if there isn't such an interval. */
2788 iv = validate_interval_range (object, &position, &position, 0);
2789 if (!NULL_INTERVAL_P (iv))
2791 Lisp_Object values_here[LAST_PROP_IDX];
2792 struct props *p;
2794 /* Get properties here. */
2795 for (p = it_props; p->handler; ++p)
2796 values_here[p->idx] = textget (iv->plist, *p->name);
2798 /* Look for an interval following iv that has different
2799 properties. */
2800 for (next_iv = next_interval (iv);
2801 (!NULL_INTERVAL_P (next_iv)
2802 && (NILP (limit)
2803 || XFASTINT (limit) > next_iv->position));
2804 next_iv = next_interval (next_iv))
2806 for (p = it_props; p->handler; ++p)
2808 Lisp_Object new_value;
2810 new_value = textget (next_iv->plist, *p->name);
2811 if (!EQ (values_here[p->idx], new_value))
2812 break;
2815 if (p->handler)
2816 break;
2819 if (!NULL_INTERVAL_P (next_iv))
2821 if (INTEGERP (limit)
2822 && next_iv->position >= XFASTINT (limit))
2823 /* No text property change up to limit. */
2824 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2825 else
2826 /* Text properties change in next_iv. */
2827 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2831 xassert (STRINGP (it->string)
2832 || (it->stop_charpos >= BEGV
2833 && it->stop_charpos >= IT_CHARPOS (*it)));
2837 /* Return the position of the next overlay change after POS in
2838 current_buffer. Value is point-max if no overlay change
2839 follows. This is like `next-overlay-change' but doesn't use
2840 xmalloc. */
2842 static int
2843 next_overlay_change (pos)
2844 int pos;
2846 int noverlays;
2847 int endpos;
2848 Lisp_Object *overlays;
2849 int i;
2851 /* Get all overlays at the given position. */
2852 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2854 /* If any of these overlays ends before endpos,
2855 use its ending point instead. */
2856 for (i = 0; i < noverlays; ++i)
2858 Lisp_Object oend;
2859 int oendpos;
2861 oend = OVERLAY_END (overlays[i]);
2862 oendpos = OVERLAY_POSITION (oend);
2863 endpos = min (endpos, oendpos);
2866 return endpos;
2871 /***********************************************************************
2872 Fontification
2873 ***********************************************************************/
2875 /* Handle changes in the `fontified' property of the current buffer by
2876 calling hook functions from Qfontification_functions to fontify
2877 regions of text. */
2879 static enum prop_handled
2880 handle_fontified_prop (it)
2881 struct it *it;
2883 Lisp_Object prop, pos;
2884 enum prop_handled handled = HANDLED_NORMALLY;
2886 /* Get the value of the `fontified' property at IT's current buffer
2887 position. (The `fontified' property doesn't have a special
2888 meaning in strings.) If the value is nil, call functions from
2889 Qfontification_functions. */
2890 if (!STRINGP (it->string)
2891 && it->s == NULL
2892 && !NILP (Vfontification_functions)
2893 && !NILP (Vrun_hooks)
2894 && (pos = make_number (IT_CHARPOS (*it)),
2895 prop = Fget_char_property (pos, Qfontified, Qnil),
2896 NILP (prop)))
2898 int count = SPECPDL_INDEX ();
2899 Lisp_Object val;
2901 val = Vfontification_functions;
2902 specbind (Qfontification_functions, Qnil);
2904 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2905 safe_call1 (val, pos);
2906 else
2908 Lisp_Object globals, fn;
2909 struct gcpro gcpro1, gcpro2;
2911 globals = Qnil;
2912 GCPRO2 (val, globals);
2914 for (; CONSP (val); val = XCDR (val))
2916 fn = XCAR (val);
2918 if (EQ (fn, Qt))
2920 /* A value of t indicates this hook has a local
2921 binding; it means to run the global binding too.
2922 In a global value, t should not occur. If it
2923 does, we must ignore it to avoid an endless
2924 loop. */
2925 for (globals = Fdefault_value (Qfontification_functions);
2926 CONSP (globals);
2927 globals = XCDR (globals))
2929 fn = XCAR (globals);
2930 if (!EQ (fn, Qt))
2931 safe_call1 (fn, pos);
2934 else
2935 safe_call1 (fn, pos);
2938 UNGCPRO;
2941 unbind_to (count, Qnil);
2943 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2944 something. This avoids an endless loop if they failed to
2945 fontify the text for which reason ever. */
2946 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2947 handled = HANDLED_RECOMPUTE_PROPS;
2950 return handled;
2955 /***********************************************************************
2956 Faces
2957 ***********************************************************************/
2959 /* Set up iterator IT from face properties at its current position.
2960 Called from handle_stop. */
2962 static enum prop_handled
2963 handle_face_prop (it)
2964 struct it *it;
2966 int new_face_id, next_stop;
2968 if (!STRINGP (it->string))
2970 new_face_id
2971 = face_at_buffer_position (it->w,
2972 IT_CHARPOS (*it),
2973 it->region_beg_charpos,
2974 it->region_end_charpos,
2975 &next_stop,
2976 (IT_CHARPOS (*it)
2977 + TEXT_PROP_DISTANCE_LIMIT),
2980 /* Is this a start of a run of characters with box face?
2981 Caveat: this can be called for a freshly initialized
2982 iterator; face_id is -1 in this case. We know that the new
2983 face will not change until limit, i.e. if the new face has a
2984 box, all characters up to limit will have one. But, as
2985 usual, we don't know whether limit is really the end. */
2986 if (new_face_id != it->face_id)
2988 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2990 /* If new face has a box but old face has not, this is
2991 the start of a run of characters with box, i.e. it has
2992 a shadow on the left side. The value of face_id of the
2993 iterator will be -1 if this is the initial call that gets
2994 the face. In this case, we have to look in front of IT's
2995 position and see whether there is a face != new_face_id. */
2996 it->start_of_box_run_p
2997 = (new_face->box != FACE_NO_BOX
2998 && (it->face_id >= 0
2999 || IT_CHARPOS (*it) == BEG
3000 || new_face_id != face_before_it_pos (it)));
3001 it->face_box_p = new_face->box != FACE_NO_BOX;
3004 else
3006 int base_face_id, bufpos;
3008 if (it->current.overlay_string_index >= 0)
3009 bufpos = IT_CHARPOS (*it);
3010 else
3011 bufpos = 0;
3013 /* For strings from a buffer, i.e. overlay strings or strings
3014 from a `display' property, use the face at IT's current
3015 buffer position as the base face to merge with, so that
3016 overlay strings appear in the same face as surrounding
3017 text, unless they specify their own faces. */
3018 base_face_id = underlying_face_id (it);
3020 new_face_id = face_at_string_position (it->w,
3021 it->string,
3022 IT_STRING_CHARPOS (*it),
3023 bufpos,
3024 it->region_beg_charpos,
3025 it->region_end_charpos,
3026 &next_stop,
3027 base_face_id, 0);
3029 #if 0 /* This shouldn't be neccessary. Let's check it. */
3030 /* If IT is used to display a mode line we would really like to
3031 use the mode line face instead of the frame's default face. */
3032 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3033 && new_face_id == DEFAULT_FACE_ID)
3034 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3035 #endif
3037 /* Is this a start of a run of characters with box? Caveat:
3038 this can be called for a freshly allocated iterator; face_id
3039 is -1 is this case. We know that the new face will not
3040 change until the next check pos, i.e. if the new face has a
3041 box, all characters up to that position will have a
3042 box. But, as usual, we don't know whether that position
3043 is really the end. */
3044 if (new_face_id != it->face_id)
3046 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3047 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3049 /* If new face has a box but old face hasn't, this is the
3050 start of a run of characters with box, i.e. it has a
3051 shadow on the left side. */
3052 it->start_of_box_run_p
3053 = new_face->box && (old_face == NULL || !old_face->box);
3054 it->face_box_p = new_face->box != FACE_NO_BOX;
3058 it->face_id = new_face_id;
3059 return HANDLED_NORMALLY;
3063 /* Return the ID of the face ``underlying'' IT's current position,
3064 which is in a string. If the iterator is associated with a
3065 buffer, return the face at IT's current buffer position.
3066 Otherwise, use the iterator's base_face_id. */
3068 static int
3069 underlying_face_id (it)
3070 struct it *it;
3072 int face_id = it->base_face_id, i;
3074 xassert (STRINGP (it->string));
3076 for (i = it->sp - 1; i >= 0; --i)
3077 if (NILP (it->stack[i].string))
3078 face_id = it->stack[i].face_id;
3080 return face_id;
3084 /* Compute the face one character before or after the current position
3085 of IT. BEFORE_P non-zero means get the face in front of IT's
3086 position. Value is the id of the face. */
3088 static int
3089 face_before_or_after_it_pos (it, before_p)
3090 struct it *it;
3091 int before_p;
3093 int face_id, limit;
3094 int next_check_charpos;
3095 struct text_pos pos;
3097 xassert (it->s == NULL);
3099 if (STRINGP (it->string))
3101 int bufpos, base_face_id;
3103 /* No face change past the end of the string (for the case
3104 we are padding with spaces). No face change before the
3105 string start. */
3106 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3107 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3108 return it->face_id;
3110 /* Set pos to the position before or after IT's current position. */
3111 if (before_p)
3112 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3113 else
3114 /* For composition, we must check the character after the
3115 composition. */
3116 pos = (it->what == IT_COMPOSITION
3117 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3118 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3120 if (it->current.overlay_string_index >= 0)
3121 bufpos = IT_CHARPOS (*it);
3122 else
3123 bufpos = 0;
3125 base_face_id = underlying_face_id (it);
3127 /* Get the face for ASCII, or unibyte. */
3128 face_id = face_at_string_position (it->w,
3129 it->string,
3130 CHARPOS (pos),
3131 bufpos,
3132 it->region_beg_charpos,
3133 it->region_end_charpos,
3134 &next_check_charpos,
3135 base_face_id, 0);
3137 /* Correct the face for charsets different from ASCII. Do it
3138 for the multibyte case only. The face returned above is
3139 suitable for unibyte text if IT->string is unibyte. */
3140 if (STRING_MULTIBYTE (it->string))
3142 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3143 int rest = SBYTES (it->string) - BYTEPOS (pos);
3144 int c, len;
3145 struct face *face = FACE_FROM_ID (it->f, face_id);
3147 c = string_char_and_length (p, rest, &len);
3148 face_id = FACE_FOR_CHAR (it->f, face, c);
3151 else
3153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3154 || (IT_CHARPOS (*it) <= BEGV && before_p))
3155 return it->face_id;
3157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3158 pos = it->current.pos;
3160 if (before_p)
3161 DEC_TEXT_POS (pos, it->multibyte_p);
3162 else
3164 if (it->what == IT_COMPOSITION)
3165 /* For composition, we must check the position after the
3166 composition. */
3167 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3168 else
3169 INC_TEXT_POS (pos, it->multibyte_p);
3172 /* Determine face for CHARSET_ASCII, or unibyte. */
3173 face_id = face_at_buffer_position (it->w,
3174 CHARPOS (pos),
3175 it->region_beg_charpos,
3176 it->region_end_charpos,
3177 &next_check_charpos,
3178 limit, 0);
3180 /* Correct the face for charsets different from ASCII. Do it
3181 for the multibyte case only. The face returned above is
3182 suitable for unibyte text if current_buffer is unibyte. */
3183 if (it->multibyte_p)
3185 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3186 struct face *face = FACE_FROM_ID (it->f, face_id);
3187 face_id = FACE_FOR_CHAR (it->f, face, c);
3191 return face_id;
3196 /***********************************************************************
3197 Invisible text
3198 ***********************************************************************/
3200 /* Set up iterator IT from invisible properties at its current
3201 position. Called from handle_stop. */
3203 static enum prop_handled
3204 handle_invisible_prop (it)
3205 struct it *it;
3207 enum prop_handled handled = HANDLED_NORMALLY;
3209 if (STRINGP (it->string))
3211 extern Lisp_Object Qinvisible;
3212 Lisp_Object prop, end_charpos, limit, charpos;
3214 /* Get the value of the invisible text property at the
3215 current position. Value will be nil if there is no such
3216 property. */
3217 charpos = make_number (IT_STRING_CHARPOS (*it));
3218 prop = Fget_text_property (charpos, Qinvisible, it->string);
3220 if (!NILP (prop)
3221 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3223 handled = HANDLED_RECOMPUTE_PROPS;
3225 /* Get the position at which the next change of the
3226 invisible text property can be found in IT->string.
3227 Value will be nil if the property value is the same for
3228 all the rest of IT->string. */
3229 XSETINT (limit, SCHARS (it->string));
3230 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3231 it->string, limit);
3233 /* Text at current position is invisible. The next
3234 change in the property is at position end_charpos.
3235 Move IT's current position to that position. */
3236 if (INTEGERP (end_charpos)
3237 && XFASTINT (end_charpos) < XFASTINT (limit))
3239 struct text_pos old;
3240 old = it->current.string_pos;
3241 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3242 compute_string_pos (&it->current.string_pos, old, it->string);
3244 else
3246 /* The rest of the string is invisible. If this is an
3247 overlay string, proceed with the next overlay string
3248 or whatever comes and return a character from there. */
3249 if (it->current.overlay_string_index >= 0)
3251 next_overlay_string (it);
3252 /* Don't check for overlay strings when we just
3253 finished processing them. */
3254 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3256 else
3258 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3259 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3264 else
3266 int invis_p, newpos, next_stop, start_charpos;
3267 Lisp_Object pos, prop, overlay;
3269 /* First of all, is there invisible text at this position? */
3270 start_charpos = IT_CHARPOS (*it);
3271 pos = make_number (IT_CHARPOS (*it));
3272 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3273 &overlay);
3274 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3276 /* If we are on invisible text, skip over it. */
3277 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3279 /* Record whether we have to display an ellipsis for the
3280 invisible text. */
3281 int display_ellipsis_p = invis_p == 2;
3283 handled = HANDLED_RECOMPUTE_PROPS;
3285 /* Loop skipping over invisible text. The loop is left at
3286 ZV or with IT on the first char being visible again. */
3289 /* Try to skip some invisible text. Return value is the
3290 position reached which can be equal to IT's position
3291 if there is nothing invisible here. This skips both
3292 over invisible text properties and overlays with
3293 invisible property. */
3294 newpos = skip_invisible (IT_CHARPOS (*it),
3295 &next_stop, ZV, it->window);
3297 /* If we skipped nothing at all we weren't at invisible
3298 text in the first place. If everything to the end of
3299 the buffer was skipped, end the loop. */
3300 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3301 invis_p = 0;
3302 else
3304 /* We skipped some characters but not necessarily
3305 all there are. Check if we ended up on visible
3306 text. Fget_char_property returns the property of
3307 the char before the given position, i.e. if we
3308 get invis_p = 0, this means that the char at
3309 newpos is visible. */
3310 pos = make_number (newpos);
3311 prop = Fget_char_property (pos, Qinvisible, it->window);
3312 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3315 /* If we ended up on invisible text, proceed to
3316 skip starting with next_stop. */
3317 if (invis_p)
3318 IT_CHARPOS (*it) = next_stop;
3320 while (invis_p);
3322 /* The position newpos is now either ZV or on visible text. */
3323 IT_CHARPOS (*it) = newpos;
3324 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3326 /* If there are before-strings at the start of invisible
3327 text, and the text is invisible because of a text
3328 property, arrange to show before-strings because 20.x did
3329 it that way. (If the text is invisible because of an
3330 overlay property instead of a text property, this is
3331 already handled in the overlay code.) */
3332 if (NILP (overlay)
3333 && get_overlay_strings (it, start_charpos))
3335 handled = HANDLED_RECOMPUTE_PROPS;
3336 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3338 else if (display_ellipsis_p)
3339 setup_for_ellipsis (it, 0);
3343 return handled;
3347 /* Make iterator IT return `...' next.
3348 Replaces LEN characters from buffer. */
3350 static void
3351 setup_for_ellipsis (it, len)
3352 struct it *it;
3353 int len;
3355 /* Use the display table definition for `...'. Invalid glyphs
3356 will be handled by the method returning elements from dpvec. */
3357 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3359 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3360 it->dpvec = v->contents;
3361 it->dpend = v->contents + v->size;
3363 else
3365 /* Default `...'. */
3366 it->dpvec = default_invis_vector;
3367 it->dpend = default_invis_vector + 3;
3370 it->dpvec_char_len = len;
3371 it->current.dpvec_index = 0;
3372 it->dpvec_face_id = -1;
3374 /* Remember the current face id in case glyphs specify faces.
3375 IT's face is restored in set_iterator_to_next. */
3376 it->saved_face_id = it->face_id;
3377 it->method = GET_FROM_DISPLAY_VECTOR;
3378 it->ellipsis_p = 1;
3383 /***********************************************************************
3384 'display' property
3385 ***********************************************************************/
3387 /* Set up iterator IT from `display' property at its current position.
3388 Called from handle_stop.
3389 We return HANDLED_RETURN if some part of the display property
3390 overrides the display of the buffer text itself.
3391 Otherwise we return HANDLED_NORMALLY. */
3393 static enum prop_handled
3394 handle_display_prop (it)
3395 struct it *it;
3397 Lisp_Object prop, object;
3398 struct text_pos *position;
3399 /* Nonzero if some property replaces the display of the text itself. */
3400 int display_replaced_p = 0;
3402 if (STRINGP (it->string))
3404 object = it->string;
3405 position = &it->current.string_pos;
3407 else
3409 object = it->w->buffer;
3410 position = &it->current.pos;
3413 /* Reset those iterator values set from display property values. */
3414 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3415 it->space_width = Qnil;
3416 it->font_height = Qnil;
3417 it->voffset = 0;
3419 /* We don't support recursive `display' properties, i.e. string
3420 values that have a string `display' property, that have a string
3421 `display' property etc. */
3422 if (!it->string_from_display_prop_p)
3423 it->area = TEXT_AREA;
3425 prop = Fget_char_property (make_number (position->charpos),
3426 Qdisplay, object);
3427 if (NILP (prop))
3428 return HANDLED_NORMALLY;
3430 if (CONSP (prop)
3431 /* Simple properties. */
3432 && !EQ (XCAR (prop), Qimage)
3433 && !EQ (XCAR (prop), Qspace)
3434 && !EQ (XCAR (prop), Qwhen)
3435 && !EQ (XCAR (prop), Qslice)
3436 && !EQ (XCAR (prop), Qspace_width)
3437 && !EQ (XCAR (prop), Qheight)
3438 && !EQ (XCAR (prop), Qraise)
3439 /* Marginal area specifications. */
3440 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3441 && !EQ (XCAR (prop), Qleft_fringe)
3442 && !EQ (XCAR (prop), Qright_fringe)
3443 && !NILP (XCAR (prop)))
3445 for (; CONSP (prop); prop = XCDR (prop))
3447 if (handle_single_display_spec (it, XCAR (prop), object,
3448 position, display_replaced_p))
3449 display_replaced_p = 1;
3452 else if (VECTORP (prop))
3454 int i;
3455 for (i = 0; i < ASIZE (prop); ++i)
3456 if (handle_single_display_spec (it, AREF (prop, i), object,
3457 position, display_replaced_p))
3458 display_replaced_p = 1;
3460 else
3462 if (handle_single_display_spec (it, prop, object, position, 0))
3463 display_replaced_p = 1;
3466 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3470 /* Value is the position of the end of the `display' property starting
3471 at START_POS in OBJECT. */
3473 static struct text_pos
3474 display_prop_end (it, object, start_pos)
3475 struct it *it;
3476 Lisp_Object object;
3477 struct text_pos start_pos;
3479 Lisp_Object end;
3480 struct text_pos end_pos;
3482 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3483 Qdisplay, object, Qnil);
3484 CHARPOS (end_pos) = XFASTINT (end);
3485 if (STRINGP (object))
3486 compute_string_pos (&end_pos, start_pos, it->string);
3487 else
3488 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3490 return end_pos;
3494 /* Set up IT from a single `display' specification PROP. OBJECT
3495 is the object in which the `display' property was found. *POSITION
3496 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3497 means that we previously saw a display specification which already
3498 replaced text display with something else, for example an image;
3499 we ignore such properties after the first one has been processed.
3501 If PROP is a `space' or `image' specification, and in some other
3502 cases too, set *POSITION to the position where the `display'
3503 property ends.
3505 Value is non-zero if something was found which replaces the display
3506 of buffer or string text. */
3508 static int
3509 handle_single_display_spec (it, spec, object, position,
3510 display_replaced_before_p)
3511 struct it *it;
3512 Lisp_Object spec;
3513 Lisp_Object object;
3514 struct text_pos *position;
3515 int display_replaced_before_p;
3517 Lisp_Object form;
3518 Lisp_Object location, value;
3519 struct text_pos start_pos;
3520 int valid_p;
3522 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3523 If the result is non-nil, use VALUE instead of SPEC. */
3524 form = Qt;
3525 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3527 spec = XCDR (spec);
3528 if (!CONSP (spec))
3529 return 0;
3530 form = XCAR (spec);
3531 spec = XCDR (spec);
3534 if (!NILP (form) && !EQ (form, Qt))
3536 int count = SPECPDL_INDEX ();
3537 struct gcpro gcpro1;
3539 /* Bind `object' to the object having the `display' property, a
3540 buffer or string. Bind `position' to the position in the
3541 object where the property was found, and `buffer-position'
3542 to the current position in the buffer. */
3543 specbind (Qobject, object);
3544 specbind (Qposition, make_number (CHARPOS (*position)));
3545 specbind (Qbuffer_position,
3546 make_number (STRINGP (object)
3547 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3548 GCPRO1 (form);
3549 form = safe_eval (form);
3550 UNGCPRO;
3551 unbind_to (count, Qnil);
3554 if (NILP (form))
3555 return 0;
3557 /* Handle `(height HEIGHT)' specifications. */
3558 if (CONSP (spec)
3559 && EQ (XCAR (spec), Qheight)
3560 && CONSP (XCDR (spec)))
3562 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3563 return 0;
3565 it->font_height = XCAR (XCDR (spec));
3566 if (!NILP (it->font_height))
3568 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3569 int new_height = -1;
3571 if (CONSP (it->font_height)
3572 && (EQ (XCAR (it->font_height), Qplus)
3573 || EQ (XCAR (it->font_height), Qminus))
3574 && CONSP (XCDR (it->font_height))
3575 && INTEGERP (XCAR (XCDR (it->font_height))))
3577 /* `(+ N)' or `(- N)' where N is an integer. */
3578 int steps = XINT (XCAR (XCDR (it->font_height)));
3579 if (EQ (XCAR (it->font_height), Qplus))
3580 steps = - steps;
3581 it->face_id = smaller_face (it->f, it->face_id, steps);
3583 else if (FUNCTIONP (it->font_height))
3585 /* Call function with current height as argument.
3586 Value is the new height. */
3587 Lisp_Object height;
3588 height = safe_call1 (it->font_height,
3589 face->lface[LFACE_HEIGHT_INDEX]);
3590 if (NUMBERP (height))
3591 new_height = XFLOATINT (height);
3593 else if (NUMBERP (it->font_height))
3595 /* Value is a multiple of the canonical char height. */
3596 struct face *face;
3598 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3599 new_height = (XFLOATINT (it->font_height)
3600 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3602 else
3604 /* Evaluate IT->font_height with `height' bound to the
3605 current specified height to get the new height. */
3606 int count = SPECPDL_INDEX ();
3608 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3609 value = safe_eval (it->font_height);
3610 unbind_to (count, Qnil);
3612 if (NUMBERP (value))
3613 new_height = XFLOATINT (value);
3616 if (new_height > 0)
3617 it->face_id = face_with_height (it->f, it->face_id, new_height);
3620 return 0;
3623 /* Handle `(space_width WIDTH)'. */
3624 if (CONSP (spec)
3625 && EQ (XCAR (spec), Qspace_width)
3626 && CONSP (XCDR (spec)))
3628 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3629 return 0;
3631 value = XCAR (XCDR (spec));
3632 if (NUMBERP (value) && XFLOATINT (value) > 0)
3633 it->space_width = value;
3635 return 0;
3638 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3639 if (CONSP (spec)
3640 && EQ (XCAR (spec), Qslice))
3642 Lisp_Object tem;
3644 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3645 return 0;
3647 if (tem = XCDR (spec), CONSP (tem))
3649 it->slice.x = XCAR (tem);
3650 if (tem = XCDR (tem), CONSP (tem))
3652 it->slice.y = XCAR (tem);
3653 if (tem = XCDR (tem), CONSP (tem))
3655 it->slice.width = XCAR (tem);
3656 if (tem = XCDR (tem), CONSP (tem))
3657 it->slice.height = XCAR (tem);
3662 return 0;
3665 /* Handle `(raise FACTOR)'. */
3666 if (CONSP (spec)
3667 && EQ (XCAR (spec), Qraise)
3668 && CONSP (XCDR (spec)))
3670 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3671 return 0;
3673 #ifdef HAVE_WINDOW_SYSTEM
3674 value = XCAR (XCDR (spec));
3675 if (NUMBERP (value))
3677 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3678 it->voffset = - (XFLOATINT (value)
3679 * (FONT_HEIGHT (face->font)));
3681 #endif /* HAVE_WINDOW_SYSTEM */
3683 return 0;
3686 /* Don't handle the other kinds of display specifications
3687 inside a string that we got from a `display' property. */
3688 if (it->string_from_display_prop_p)
3689 return 0;
3691 /* Characters having this form of property are not displayed, so
3692 we have to find the end of the property. */
3693 start_pos = *position;
3694 *position = display_prop_end (it, object, start_pos);
3695 value = Qnil;
3697 /* Stop the scan at that end position--we assume that all
3698 text properties change there. */
3699 it->stop_charpos = position->charpos;
3701 /* Handle `(left-fringe BITMAP [FACE])'
3702 and `(right-fringe BITMAP [FACE])'. */
3703 if (CONSP (spec)
3704 && (EQ (XCAR (spec), Qleft_fringe)
3705 || EQ (XCAR (spec), Qright_fringe))
3706 && CONSP (XCDR (spec)))
3708 int face_id = DEFAULT_FACE_ID;
3709 int fringe_bitmap;
3711 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3712 /* If we return here, POSITION has been advanced
3713 across the text with this property. */
3714 return 0;
3716 #ifdef HAVE_WINDOW_SYSTEM
3717 value = XCAR (XCDR (spec));
3718 if (!SYMBOLP (value)
3719 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3720 /* If we return here, POSITION has been advanced
3721 across the text with this property. */
3722 return 0;
3724 if (CONSP (XCDR (XCDR (spec))))
3726 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3727 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3728 if (face_id2 >= 0)
3729 face_id = face_id2;
3732 /* Save current settings of IT so that we can restore them
3733 when we are finished with the glyph property value. */
3735 push_it (it);
3737 it->area = TEXT_AREA;
3738 it->what = IT_IMAGE;
3739 it->image_id = -1; /* no image */
3740 it->position = start_pos;
3741 it->object = NILP (object) ? it->w->buffer : object;
3742 it->method = GET_FROM_IMAGE;
3743 it->face_id = face_id;
3745 /* Say that we haven't consumed the characters with
3746 `display' property yet. The call to pop_it in
3747 set_iterator_to_next will clean this up. */
3748 *position = start_pos;
3750 if (EQ (XCAR (spec), Qleft_fringe))
3752 it->left_user_fringe_bitmap = fringe_bitmap;
3753 it->left_user_fringe_face_id = face_id;
3755 else
3757 it->right_user_fringe_bitmap = fringe_bitmap;
3758 it->right_user_fringe_face_id = face_id;
3760 #endif /* HAVE_WINDOW_SYSTEM */
3761 return 1;
3764 /* Prepare to handle `((margin left-margin) ...)',
3765 `((margin right-margin) ...)' and `((margin nil) ...)'
3766 prefixes for display specifications. */
3767 location = Qunbound;
3768 if (CONSP (spec) && CONSP (XCAR (spec)))
3770 Lisp_Object tem;
3772 value = XCDR (spec);
3773 if (CONSP (value))
3774 value = XCAR (value);
3776 tem = XCAR (spec);
3777 if (EQ (XCAR (tem), Qmargin)
3778 && (tem = XCDR (tem),
3779 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3780 (NILP (tem)
3781 || EQ (tem, Qleft_margin)
3782 || EQ (tem, Qright_margin))))
3783 location = tem;
3786 if (EQ (location, Qunbound))
3788 location = Qnil;
3789 value = spec;
3792 /* After this point, VALUE is the property after any
3793 margin prefix has been stripped. It must be a string,
3794 an image specification, or `(space ...)'.
3796 LOCATION specifies where to display: `left-margin',
3797 `right-margin' or nil. */
3799 valid_p = (STRINGP (value)
3800 #ifdef HAVE_WINDOW_SYSTEM
3801 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3802 #endif /* not HAVE_WINDOW_SYSTEM */
3803 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3805 if (valid_p && !display_replaced_before_p)
3807 /* Save current settings of IT so that we can restore them
3808 when we are finished with the glyph property value. */
3809 push_it (it);
3811 if (NILP (location))
3812 it->area = TEXT_AREA;
3813 else if (EQ (location, Qleft_margin))
3814 it->area = LEFT_MARGIN_AREA;
3815 else
3816 it->area = RIGHT_MARGIN_AREA;
3818 if (STRINGP (value))
3820 it->string = value;
3821 it->multibyte_p = STRING_MULTIBYTE (it->string);
3822 it->current.overlay_string_index = -1;
3823 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3824 it->end_charpos = it->string_nchars = SCHARS (it->string);
3825 it->method = GET_FROM_STRING;
3826 it->stop_charpos = 0;
3827 it->string_from_display_prop_p = 1;
3828 /* Say that we haven't consumed the characters with
3829 `display' property yet. The call to pop_it in
3830 set_iterator_to_next will clean this up. */
3831 *position = start_pos;
3833 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3835 it->method = GET_FROM_STRETCH;
3836 it->object = value;
3837 it->current.pos = it->position = start_pos;
3839 #ifdef HAVE_WINDOW_SYSTEM
3840 else
3842 it->what = IT_IMAGE;
3843 it->image_id = lookup_image (it->f, value);
3844 it->position = start_pos;
3845 it->object = NILP (object) ? it->w->buffer : object;
3846 it->method = GET_FROM_IMAGE;
3848 /* Say that we haven't consumed the characters with
3849 `display' property yet. The call to pop_it in
3850 set_iterator_to_next will clean this up. */
3851 *position = start_pos;
3853 #endif /* HAVE_WINDOW_SYSTEM */
3855 return 1;
3858 /* Invalid property or property not supported. Restore
3859 POSITION to what it was before. */
3860 *position = start_pos;
3861 return 0;
3865 /* Check if SPEC is a display specification value whose text should be
3866 treated as intangible. */
3868 static int
3869 single_display_spec_intangible_p (prop)
3870 Lisp_Object prop;
3872 /* Skip over `when FORM'. */
3873 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3875 prop = XCDR (prop);
3876 if (!CONSP (prop))
3877 return 0;
3878 prop = XCDR (prop);
3881 if (STRINGP (prop))
3882 return 1;
3884 if (!CONSP (prop))
3885 return 0;
3887 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3888 we don't need to treat text as intangible. */
3889 if (EQ (XCAR (prop), Qmargin))
3891 prop = XCDR (prop);
3892 if (!CONSP (prop))
3893 return 0;
3895 prop = XCDR (prop);
3896 if (!CONSP (prop)
3897 || EQ (XCAR (prop), Qleft_margin)
3898 || EQ (XCAR (prop), Qright_margin))
3899 return 0;
3902 return (CONSP (prop)
3903 && (EQ (XCAR (prop), Qimage)
3904 || EQ (XCAR (prop), Qspace)));
3908 /* Check if PROP is a display property value whose text should be
3909 treated as intangible. */
3912 display_prop_intangible_p (prop)
3913 Lisp_Object prop;
3915 if (CONSP (prop)
3916 && CONSP (XCAR (prop))
3917 && !EQ (Qmargin, XCAR (XCAR (prop))))
3919 /* A list of sub-properties. */
3920 while (CONSP (prop))
3922 if (single_display_spec_intangible_p (XCAR (prop)))
3923 return 1;
3924 prop = XCDR (prop);
3927 else if (VECTORP (prop))
3929 /* A vector of sub-properties. */
3930 int i;
3931 for (i = 0; i < ASIZE (prop); ++i)
3932 if (single_display_spec_intangible_p (AREF (prop, i)))
3933 return 1;
3935 else
3936 return single_display_spec_intangible_p (prop);
3938 return 0;
3942 /* Return 1 if PROP is a display sub-property value containing STRING. */
3944 static int
3945 single_display_spec_string_p (prop, string)
3946 Lisp_Object prop, string;
3948 if (EQ (string, prop))
3949 return 1;
3951 /* Skip over `when FORM'. */
3952 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3954 prop = XCDR (prop);
3955 if (!CONSP (prop))
3956 return 0;
3957 prop = XCDR (prop);
3960 if (CONSP (prop))
3961 /* Skip over `margin LOCATION'. */
3962 if (EQ (XCAR (prop), Qmargin))
3964 prop = XCDR (prop);
3965 if (!CONSP (prop))
3966 return 0;
3968 prop = XCDR (prop);
3969 if (!CONSP (prop))
3970 return 0;
3973 return CONSP (prop) && EQ (XCAR (prop), string);
3977 /* Return 1 if STRING appears in the `display' property PROP. */
3979 static int
3980 display_prop_string_p (prop, string)
3981 Lisp_Object prop, string;
3983 if (CONSP (prop)
3984 && CONSP (XCAR (prop))
3985 && !EQ (Qmargin, XCAR (XCAR (prop))))
3987 /* A list of sub-properties. */
3988 while (CONSP (prop))
3990 if (single_display_spec_string_p (XCAR (prop), string))
3991 return 1;
3992 prop = XCDR (prop);
3995 else if (VECTORP (prop))
3997 /* A vector of sub-properties. */
3998 int i;
3999 for (i = 0; i < ASIZE (prop); ++i)
4000 if (single_display_spec_string_p (AREF (prop, i), string))
4001 return 1;
4003 else
4004 return single_display_spec_string_p (prop, string);
4006 return 0;
4010 /* Determine from which buffer position in W's buffer STRING comes
4011 from. AROUND_CHARPOS is an approximate position where it could
4012 be from. Value is the buffer position or 0 if it couldn't be
4013 determined.
4015 W's buffer must be current.
4017 This function is necessary because we don't record buffer positions
4018 in glyphs generated from strings (to keep struct glyph small).
4019 This function may only use code that doesn't eval because it is
4020 called asynchronously from note_mouse_highlight. */
4023 string_buffer_position (w, string, around_charpos)
4024 struct window *w;
4025 Lisp_Object string;
4026 int around_charpos;
4028 Lisp_Object limit, prop, pos;
4029 const int MAX_DISTANCE = 1000;
4030 int found = 0;
4032 pos = make_number (around_charpos);
4033 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4034 while (!found && !EQ (pos, limit))
4036 prop = Fget_char_property (pos, Qdisplay, Qnil);
4037 if (!NILP (prop) && display_prop_string_p (prop, string))
4038 found = 1;
4039 else
4040 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4043 if (!found)
4045 pos = make_number (around_charpos);
4046 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4047 while (!found && !EQ (pos, limit))
4049 prop = Fget_char_property (pos, Qdisplay, Qnil);
4050 if (!NILP (prop) && display_prop_string_p (prop, string))
4051 found = 1;
4052 else
4053 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4054 limit);
4058 return found ? XINT (pos) : 0;
4063 /***********************************************************************
4064 `composition' property
4065 ***********************************************************************/
4067 /* Set up iterator IT from `composition' property at its current
4068 position. Called from handle_stop. */
4070 static enum prop_handled
4071 handle_composition_prop (it)
4072 struct it *it;
4074 Lisp_Object prop, string;
4075 int pos, pos_byte, end;
4076 enum prop_handled handled = HANDLED_NORMALLY;
4078 if (STRINGP (it->string))
4080 pos = IT_STRING_CHARPOS (*it);
4081 pos_byte = IT_STRING_BYTEPOS (*it);
4082 string = it->string;
4084 else
4086 pos = IT_CHARPOS (*it);
4087 pos_byte = IT_BYTEPOS (*it);
4088 string = Qnil;
4091 /* If there's a valid composition and point is not inside of the
4092 composition (in the case that the composition is from the current
4093 buffer), draw a glyph composed from the composition components. */
4094 if (find_composition (pos, -1, &pos, &end, &prop, string)
4095 && COMPOSITION_VALID_P (pos, end, prop)
4096 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4098 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4100 if (id >= 0)
4102 it->method = GET_FROM_COMPOSITION;
4103 it->cmp_id = id;
4104 it->cmp_len = COMPOSITION_LENGTH (prop);
4105 /* For a terminal, draw only the first character of the
4106 components. */
4107 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4108 it->len = (STRINGP (it->string)
4109 ? string_char_to_byte (it->string, end)
4110 : CHAR_TO_BYTE (end)) - pos_byte;
4111 it->stop_charpos = end;
4112 handled = HANDLED_RETURN;
4116 return handled;
4121 /***********************************************************************
4122 Overlay strings
4123 ***********************************************************************/
4125 /* The following structure is used to record overlay strings for
4126 later sorting in load_overlay_strings. */
4128 struct overlay_entry
4130 Lisp_Object overlay;
4131 Lisp_Object string;
4132 int priority;
4133 int after_string_p;
4137 /* Set up iterator IT from overlay strings at its current position.
4138 Called from handle_stop. */
4140 static enum prop_handled
4141 handle_overlay_change (it)
4142 struct it *it;
4144 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4145 return HANDLED_RECOMPUTE_PROPS;
4146 else
4147 return HANDLED_NORMALLY;
4151 /* Set up the next overlay string for delivery by IT, if there is an
4152 overlay string to deliver. Called by set_iterator_to_next when the
4153 end of the current overlay string is reached. If there are more
4154 overlay strings to display, IT->string and
4155 IT->current.overlay_string_index are set appropriately here.
4156 Otherwise IT->string is set to nil. */
4158 static void
4159 next_overlay_string (it)
4160 struct it *it;
4162 ++it->current.overlay_string_index;
4163 if (it->current.overlay_string_index == it->n_overlay_strings)
4165 /* No more overlay strings. Restore IT's settings to what
4166 they were before overlay strings were processed, and
4167 continue to deliver from current_buffer. */
4168 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4170 pop_it (it);
4171 xassert (it->stop_charpos >= BEGV
4172 && it->stop_charpos <= it->end_charpos);
4173 it->string = Qnil;
4174 it->current.overlay_string_index = -1;
4175 SET_TEXT_POS (it->current.string_pos, -1, -1);
4176 it->n_overlay_strings = 0;
4177 it->method = GET_FROM_BUFFER;
4179 /* If we're at the end of the buffer, record that we have
4180 processed the overlay strings there already, so that
4181 next_element_from_buffer doesn't try it again. */
4182 if (IT_CHARPOS (*it) >= it->end_charpos)
4183 it->overlay_strings_at_end_processed_p = 1;
4185 /* If we have to display `...' for invisible text, set
4186 the iterator up for that. */
4187 if (display_ellipsis_p)
4188 setup_for_ellipsis (it, 0);
4190 else
4192 /* There are more overlay strings to process. If
4193 IT->current.overlay_string_index has advanced to a position
4194 where we must load IT->overlay_strings with more strings, do
4195 it. */
4196 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4198 if (it->current.overlay_string_index && i == 0)
4199 load_overlay_strings (it, 0);
4201 /* Initialize IT to deliver display elements from the overlay
4202 string. */
4203 it->string = it->overlay_strings[i];
4204 it->multibyte_p = STRING_MULTIBYTE (it->string);
4205 SET_TEXT_POS (it->current.string_pos, 0, 0);
4206 it->method = GET_FROM_STRING;
4207 it->stop_charpos = 0;
4210 CHECK_IT (it);
4214 /* Compare two overlay_entry structures E1 and E2. Used as a
4215 comparison function for qsort in load_overlay_strings. Overlay
4216 strings for the same position are sorted so that
4218 1. All after-strings come in front of before-strings, except
4219 when they come from the same overlay.
4221 2. Within after-strings, strings are sorted so that overlay strings
4222 from overlays with higher priorities come first.
4224 2. Within before-strings, strings are sorted so that overlay
4225 strings from overlays with higher priorities come last.
4227 Value is analogous to strcmp. */
4230 static int
4231 compare_overlay_entries (e1, e2)
4232 void *e1, *e2;
4234 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4235 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4236 int result;
4238 if (entry1->after_string_p != entry2->after_string_p)
4240 /* Let after-strings appear in front of before-strings if
4241 they come from different overlays. */
4242 if (EQ (entry1->overlay, entry2->overlay))
4243 result = entry1->after_string_p ? 1 : -1;
4244 else
4245 result = entry1->after_string_p ? -1 : 1;
4247 else if (entry1->after_string_p)
4248 /* After-strings sorted in order of decreasing priority. */
4249 result = entry2->priority - entry1->priority;
4250 else
4251 /* Before-strings sorted in order of increasing priority. */
4252 result = entry1->priority - entry2->priority;
4254 return result;
4258 /* Load the vector IT->overlay_strings with overlay strings from IT's
4259 current buffer position, or from CHARPOS if that is > 0. Set
4260 IT->n_overlays to the total number of overlay strings found.
4262 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4263 a time. On entry into load_overlay_strings,
4264 IT->current.overlay_string_index gives the number of overlay
4265 strings that have already been loaded by previous calls to this
4266 function.
4268 IT->add_overlay_start contains an additional overlay start
4269 position to consider for taking overlay strings from, if non-zero.
4270 This position comes into play when the overlay has an `invisible'
4271 property, and both before and after-strings. When we've skipped to
4272 the end of the overlay, because of its `invisible' property, we
4273 nevertheless want its before-string to appear.
4274 IT->add_overlay_start will contain the overlay start position
4275 in this case.
4277 Overlay strings are sorted so that after-string strings come in
4278 front of before-string strings. Within before and after-strings,
4279 strings are sorted by overlay priority. See also function
4280 compare_overlay_entries. */
4282 static void
4283 load_overlay_strings (it, charpos)
4284 struct it *it;
4285 int charpos;
4287 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4288 Lisp_Object overlay, window, str, invisible;
4289 struct Lisp_Overlay *ov;
4290 int start, end;
4291 int size = 20;
4292 int n = 0, i, j, invis_p;
4293 struct overlay_entry *entries
4294 = (struct overlay_entry *) alloca (size * sizeof *entries);
4296 if (charpos <= 0)
4297 charpos = IT_CHARPOS (*it);
4299 /* Append the overlay string STRING of overlay OVERLAY to vector
4300 `entries' which has size `size' and currently contains `n'
4301 elements. AFTER_P non-zero means STRING is an after-string of
4302 OVERLAY. */
4303 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4304 do \
4306 Lisp_Object priority; \
4308 if (n == size) \
4310 int new_size = 2 * size; \
4311 struct overlay_entry *old = entries; \
4312 entries = \
4313 (struct overlay_entry *) alloca (new_size \
4314 * sizeof *entries); \
4315 bcopy (old, entries, size * sizeof *entries); \
4316 size = new_size; \
4319 entries[n].string = (STRING); \
4320 entries[n].overlay = (OVERLAY); \
4321 priority = Foverlay_get ((OVERLAY), Qpriority); \
4322 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4323 entries[n].after_string_p = (AFTER_P); \
4324 ++n; \
4326 while (0)
4328 /* Process overlay before the overlay center. */
4329 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4331 XSETMISC (overlay, ov);
4332 xassert (OVERLAYP (overlay));
4333 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4334 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4336 if (end < charpos)
4337 break;
4339 /* Skip this overlay if it doesn't start or end at IT's current
4340 position. */
4341 if (end != charpos && start != charpos)
4342 continue;
4344 /* Skip this overlay if it doesn't apply to IT->w. */
4345 window = Foverlay_get (overlay, Qwindow);
4346 if (WINDOWP (window) && XWINDOW (window) != it->w)
4347 continue;
4349 /* If the text ``under'' the overlay is invisible, both before-
4350 and after-strings from this overlay are visible; start and
4351 end position are indistinguishable. */
4352 invisible = Foverlay_get (overlay, Qinvisible);
4353 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4355 /* If overlay has a non-empty before-string, record it. */
4356 if ((start == charpos || (end == charpos && invis_p))
4357 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4358 && SCHARS (str))
4359 RECORD_OVERLAY_STRING (overlay, str, 0);
4361 /* If overlay has a non-empty after-string, record it. */
4362 if ((end == charpos || (start == charpos && invis_p))
4363 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4364 && SCHARS (str))
4365 RECORD_OVERLAY_STRING (overlay, str, 1);
4368 /* Process overlays after the overlay center. */
4369 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4371 XSETMISC (overlay, ov);
4372 xassert (OVERLAYP (overlay));
4373 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4374 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4376 if (start > charpos)
4377 break;
4379 /* Skip this overlay if it doesn't start or end at IT's current
4380 position. */
4381 if (end != charpos && start != charpos)
4382 continue;
4384 /* Skip this overlay if it doesn't apply to IT->w. */
4385 window = Foverlay_get (overlay, Qwindow);
4386 if (WINDOWP (window) && XWINDOW (window) != it->w)
4387 continue;
4389 /* If the text ``under'' the overlay is invisible, it has a zero
4390 dimension, and both before- and after-strings apply. */
4391 invisible = Foverlay_get (overlay, Qinvisible);
4392 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4394 /* If overlay has a non-empty before-string, record it. */
4395 if ((start == charpos || (end == charpos && invis_p))
4396 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4397 && SCHARS (str))
4398 RECORD_OVERLAY_STRING (overlay, str, 0);
4400 /* If overlay has a non-empty after-string, record it. */
4401 if ((end == charpos || (start == charpos && invis_p))
4402 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4403 && SCHARS (str))
4404 RECORD_OVERLAY_STRING (overlay, str, 1);
4407 #undef RECORD_OVERLAY_STRING
4409 /* Sort entries. */
4410 if (n > 1)
4411 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4413 /* Record the total number of strings to process. */
4414 it->n_overlay_strings = n;
4416 /* IT->current.overlay_string_index is the number of overlay strings
4417 that have already been consumed by IT. Copy some of the
4418 remaining overlay strings to IT->overlay_strings. */
4419 i = 0;
4420 j = it->current.overlay_string_index;
4421 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4422 it->overlay_strings[i++] = entries[j++].string;
4424 CHECK_IT (it);
4428 /* Get the first chunk of overlay strings at IT's current buffer
4429 position, or at CHARPOS if that is > 0. Value is non-zero if at
4430 least one overlay string was found. */
4432 static int
4433 get_overlay_strings (it, charpos)
4434 struct it *it;
4435 int charpos;
4437 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4438 process. This fills IT->overlay_strings with strings, and sets
4439 IT->n_overlay_strings to the total number of strings to process.
4440 IT->pos.overlay_string_index has to be set temporarily to zero
4441 because load_overlay_strings needs this; it must be set to -1
4442 when no overlay strings are found because a zero value would
4443 indicate a position in the first overlay string. */
4444 it->current.overlay_string_index = 0;
4445 load_overlay_strings (it, charpos);
4447 /* If we found overlay strings, set up IT to deliver display
4448 elements from the first one. Otherwise set up IT to deliver
4449 from current_buffer. */
4450 if (it->n_overlay_strings)
4452 /* Make sure we know settings in current_buffer, so that we can
4453 restore meaningful values when we're done with the overlay
4454 strings. */
4455 compute_stop_pos (it);
4456 xassert (it->face_id >= 0);
4458 /* Save IT's settings. They are restored after all overlay
4459 strings have been processed. */
4460 xassert (it->sp == 0);
4461 push_it (it);
4463 /* Set up IT to deliver display elements from the first overlay
4464 string. */
4465 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4466 it->string = it->overlay_strings[0];
4467 it->stop_charpos = 0;
4468 xassert (STRINGP (it->string));
4469 it->end_charpos = SCHARS (it->string);
4470 it->multibyte_p = STRING_MULTIBYTE (it->string);
4471 it->method = GET_FROM_STRING;
4473 else
4475 it->string = Qnil;
4476 it->current.overlay_string_index = -1;
4477 it->method = GET_FROM_BUFFER;
4480 CHECK_IT (it);
4482 /* Value is non-zero if we found at least one overlay string. */
4483 return STRINGP (it->string);
4488 /***********************************************************************
4489 Saving and restoring state
4490 ***********************************************************************/
4492 /* Save current settings of IT on IT->stack. Called, for example,
4493 before setting up IT for an overlay string, to be able to restore
4494 IT's settings to what they were after the overlay string has been
4495 processed. */
4497 static void
4498 push_it (it)
4499 struct it *it;
4501 struct iterator_stack_entry *p;
4503 xassert (it->sp < 2);
4504 p = it->stack + it->sp;
4506 p->stop_charpos = it->stop_charpos;
4507 xassert (it->face_id >= 0);
4508 p->face_id = it->face_id;
4509 p->string = it->string;
4510 p->pos = it->current;
4511 p->end_charpos = it->end_charpos;
4512 p->string_nchars = it->string_nchars;
4513 p->area = it->area;
4514 p->multibyte_p = it->multibyte_p;
4515 p->slice = it->slice;
4516 p->space_width = it->space_width;
4517 p->font_height = it->font_height;
4518 p->voffset = it->voffset;
4519 p->string_from_display_prop_p = it->string_from_display_prop_p;
4520 p->display_ellipsis_p = 0;
4521 ++it->sp;
4525 /* Restore IT's settings from IT->stack. Called, for example, when no
4526 more overlay strings must be processed, and we return to delivering
4527 display elements from a buffer, or when the end of a string from a
4528 `display' property is reached and we return to delivering display
4529 elements from an overlay string, or from a buffer. */
4531 static void
4532 pop_it (it)
4533 struct it *it;
4535 struct iterator_stack_entry *p;
4537 xassert (it->sp > 0);
4538 --it->sp;
4539 p = it->stack + it->sp;
4540 it->stop_charpos = p->stop_charpos;
4541 it->face_id = p->face_id;
4542 it->string = p->string;
4543 it->current = p->pos;
4544 it->end_charpos = p->end_charpos;
4545 it->string_nchars = p->string_nchars;
4546 it->area = p->area;
4547 it->multibyte_p = p->multibyte_p;
4548 it->slice = p->slice;
4549 it->space_width = p->space_width;
4550 it->font_height = p->font_height;
4551 it->voffset = p->voffset;
4552 it->string_from_display_prop_p = p->string_from_display_prop_p;
4557 /***********************************************************************
4558 Moving over lines
4559 ***********************************************************************/
4561 /* Set IT's current position to the previous line start. */
4563 static void
4564 back_to_previous_line_start (it)
4565 struct it *it;
4567 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4568 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4572 /* Move IT to the next line start.
4574 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4575 we skipped over part of the text (as opposed to moving the iterator
4576 continuously over the text). Otherwise, don't change the value
4577 of *SKIPPED_P.
4579 Newlines may come from buffer text, overlay strings, or strings
4580 displayed via the `display' property. That's the reason we can't
4581 simply use find_next_newline_no_quit.
4583 Note that this function may not skip over invisible text that is so
4584 because of text properties and immediately follows a newline. If
4585 it would, function reseat_at_next_visible_line_start, when called
4586 from set_iterator_to_next, would effectively make invisible
4587 characters following a newline part of the wrong glyph row, which
4588 leads to wrong cursor motion. */
4590 static int
4591 forward_to_next_line_start (it, skipped_p)
4592 struct it *it;
4593 int *skipped_p;
4595 int old_selective, newline_found_p, n;
4596 const int MAX_NEWLINE_DISTANCE = 500;
4598 /* If already on a newline, just consume it to avoid unintended
4599 skipping over invisible text below. */
4600 if (it->what == IT_CHARACTER
4601 && it->c == '\n'
4602 && CHARPOS (it->position) == IT_CHARPOS (*it))
4604 set_iterator_to_next (it, 0);
4605 it->c = 0;
4606 return 1;
4609 /* Don't handle selective display in the following. It's (a)
4610 unnecessary because it's done by the caller, and (b) leads to an
4611 infinite recursion because next_element_from_ellipsis indirectly
4612 calls this function. */
4613 old_selective = it->selective;
4614 it->selective = 0;
4616 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4617 from buffer text. */
4618 for (n = newline_found_p = 0;
4619 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4620 n += STRINGP (it->string) ? 0 : 1)
4622 if (!get_next_display_element (it))
4623 return 0;
4624 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4625 set_iterator_to_next (it, 0);
4628 /* If we didn't find a newline near enough, see if we can use a
4629 short-cut. */
4630 if (!newline_found_p)
4632 int start = IT_CHARPOS (*it);
4633 int limit = find_next_newline_no_quit (start, 1);
4634 Lisp_Object pos;
4636 xassert (!STRINGP (it->string));
4638 /* If there isn't any `display' property in sight, and no
4639 overlays, we can just use the position of the newline in
4640 buffer text. */
4641 if (it->stop_charpos >= limit
4642 || ((pos = Fnext_single_property_change (make_number (start),
4643 Qdisplay,
4644 Qnil, make_number (limit)),
4645 NILP (pos))
4646 && next_overlay_change (start) == ZV))
4648 IT_CHARPOS (*it) = limit;
4649 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4650 *skipped_p = newline_found_p = 1;
4652 else
4654 while (get_next_display_element (it)
4655 && !newline_found_p)
4657 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4658 set_iterator_to_next (it, 0);
4663 it->selective = old_selective;
4664 return newline_found_p;
4668 /* Set IT's current position to the previous visible line start. Skip
4669 invisible text that is so either due to text properties or due to
4670 selective display. Caution: this does not change IT->current_x and
4671 IT->hpos. */
4673 static void
4674 back_to_previous_visible_line_start (it)
4675 struct it *it;
4677 while (IT_CHARPOS (*it) > BEGV)
4679 back_to_previous_line_start (it);
4680 if (IT_CHARPOS (*it) <= BEGV)
4681 break;
4683 /* If selective > 0, then lines indented more than that values
4684 are invisible. */
4685 if (it->selective > 0
4686 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4687 (double) it->selective)) /* iftc */
4688 continue;
4690 /* Check the newline before point for invisibility. */
4692 Lisp_Object prop;
4693 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4694 Qinvisible, it->window);
4695 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4696 continue;
4699 /* If newline has a display property that replaces the newline with something
4700 else (image or text), find start of overlay or interval and continue search
4701 from that point. */
4702 if (IT_CHARPOS (*it) > BEGV)
4704 struct it it2 = *it;
4705 int pos;
4706 int beg, end;
4707 Lisp_Object val, overlay;
4709 pos = --IT_CHARPOS (it2);
4710 --IT_BYTEPOS (it2);
4711 it2.sp = 0;
4712 if (handle_display_prop (&it2) == HANDLED_RETURN
4713 && !NILP (val = get_char_property_and_overlay
4714 (make_number (pos), Qdisplay, Qnil, &overlay))
4715 && (OVERLAYP (overlay)
4716 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4717 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4719 if (beg < BEGV)
4720 beg = BEGV;
4721 IT_CHARPOS (*it) = beg;
4722 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4723 continue;
4727 break;
4730 xassert (IT_CHARPOS (*it) >= BEGV);
4731 xassert (IT_CHARPOS (*it) == BEGV
4732 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4733 CHECK_IT (it);
4737 /* Reseat iterator IT at the previous visible line start. Skip
4738 invisible text that is so either due to text properties or due to
4739 selective display. At the end, update IT's overlay information,
4740 face information etc. */
4742 void
4743 reseat_at_previous_visible_line_start (it)
4744 struct it *it;
4746 back_to_previous_visible_line_start (it);
4747 reseat (it, it->current.pos, 1);
4748 CHECK_IT (it);
4752 /* Reseat iterator IT on the next visible line start in the current
4753 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4754 preceding the line start. Skip over invisible text that is so
4755 because of selective display. Compute faces, overlays etc at the
4756 new position. Note that this function does not skip over text that
4757 is invisible because of text properties. */
4759 static void
4760 reseat_at_next_visible_line_start (it, on_newline_p)
4761 struct it *it;
4762 int on_newline_p;
4764 int newline_found_p, skipped_p = 0;
4766 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4768 /* Skip over lines that are invisible because they are indented
4769 more than the value of IT->selective. */
4770 if (it->selective > 0)
4771 while (IT_CHARPOS (*it) < ZV
4772 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4773 (double) it->selective)) /* iftc */
4775 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4776 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4779 /* Position on the newline if that's what's requested. */
4780 if (on_newline_p && newline_found_p)
4782 if (STRINGP (it->string))
4784 if (IT_STRING_CHARPOS (*it) > 0)
4786 --IT_STRING_CHARPOS (*it);
4787 --IT_STRING_BYTEPOS (*it);
4790 else if (IT_CHARPOS (*it) > BEGV)
4792 --IT_CHARPOS (*it);
4793 --IT_BYTEPOS (*it);
4794 reseat (it, it->current.pos, 0);
4797 else if (skipped_p)
4798 reseat (it, it->current.pos, 0);
4800 CHECK_IT (it);
4805 /***********************************************************************
4806 Changing an iterator's position
4807 ***********************************************************************/
4809 /* Change IT's current position to POS in current_buffer. If FORCE_P
4810 is non-zero, always check for text properties at the new position.
4811 Otherwise, text properties are only looked up if POS >=
4812 IT->check_charpos of a property. */
4814 static void
4815 reseat (it, pos, force_p)
4816 struct it *it;
4817 struct text_pos pos;
4818 int force_p;
4820 int original_pos = IT_CHARPOS (*it);
4822 reseat_1 (it, pos, 0);
4824 /* Determine where to check text properties. Avoid doing it
4825 where possible because text property lookup is very expensive. */
4826 if (force_p
4827 || CHARPOS (pos) > it->stop_charpos
4828 || CHARPOS (pos) < original_pos)
4829 handle_stop (it);
4831 CHECK_IT (it);
4835 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4836 IT->stop_pos to POS, also. */
4838 static void
4839 reseat_1 (it, pos, set_stop_p)
4840 struct it *it;
4841 struct text_pos pos;
4842 int set_stop_p;
4844 /* Don't call this function when scanning a C string. */
4845 xassert (it->s == NULL);
4847 /* POS must be a reasonable value. */
4848 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4850 it->current.pos = it->position = pos;
4851 XSETBUFFER (it->object, current_buffer);
4852 it->end_charpos = ZV;
4853 it->dpvec = NULL;
4854 it->current.dpvec_index = -1;
4855 it->current.overlay_string_index = -1;
4856 IT_STRING_CHARPOS (*it) = -1;
4857 IT_STRING_BYTEPOS (*it) = -1;
4858 it->string = Qnil;
4859 it->method = GET_FROM_BUFFER;
4860 /* RMS: I added this to fix a bug in move_it_vertically_backward
4861 where it->area continued to relate to the starting point
4862 for the backward motion. Bug report from
4863 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4864 However, I am not sure whether reseat still does the right thing
4865 in general after this change. */
4866 it->area = TEXT_AREA;
4867 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4868 it->sp = 0;
4869 it->face_before_selective_p = 0;
4871 if (set_stop_p)
4872 it->stop_charpos = CHARPOS (pos);
4876 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4877 If S is non-null, it is a C string to iterate over. Otherwise,
4878 STRING gives a Lisp string to iterate over.
4880 If PRECISION > 0, don't return more then PRECISION number of
4881 characters from the string.
4883 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4884 characters have been returned. FIELD_WIDTH < 0 means an infinite
4885 field width.
4887 MULTIBYTE = 0 means disable processing of multibyte characters,
4888 MULTIBYTE > 0 means enable it,
4889 MULTIBYTE < 0 means use IT->multibyte_p.
4891 IT must be initialized via a prior call to init_iterator before
4892 calling this function. */
4894 static void
4895 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4896 struct it *it;
4897 unsigned char *s;
4898 Lisp_Object string;
4899 int charpos;
4900 int precision, field_width, multibyte;
4902 /* No region in strings. */
4903 it->region_beg_charpos = it->region_end_charpos = -1;
4905 /* No text property checks performed by default, but see below. */
4906 it->stop_charpos = -1;
4908 /* Set iterator position and end position. */
4909 bzero (&it->current, sizeof it->current);
4910 it->current.overlay_string_index = -1;
4911 it->current.dpvec_index = -1;
4912 xassert (charpos >= 0);
4914 /* If STRING is specified, use its multibyteness, otherwise use the
4915 setting of MULTIBYTE, if specified. */
4916 if (multibyte >= 0)
4917 it->multibyte_p = multibyte > 0;
4919 if (s == NULL)
4921 xassert (STRINGP (string));
4922 it->string = string;
4923 it->s = NULL;
4924 it->end_charpos = it->string_nchars = SCHARS (string);
4925 it->method = GET_FROM_STRING;
4926 it->current.string_pos = string_pos (charpos, string);
4928 else
4930 it->s = s;
4931 it->string = Qnil;
4933 /* Note that we use IT->current.pos, not it->current.string_pos,
4934 for displaying C strings. */
4935 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4936 if (it->multibyte_p)
4938 it->current.pos = c_string_pos (charpos, s, 1);
4939 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4941 else
4943 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4944 it->end_charpos = it->string_nchars = strlen (s);
4947 it->method = GET_FROM_C_STRING;
4950 /* PRECISION > 0 means don't return more than PRECISION characters
4951 from the string. */
4952 if (precision > 0 && it->end_charpos - charpos > precision)
4953 it->end_charpos = it->string_nchars = charpos + precision;
4955 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4956 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4957 FIELD_WIDTH < 0 means infinite field width. This is useful for
4958 padding with `-' at the end of a mode line. */
4959 if (field_width < 0)
4960 field_width = INFINITY;
4961 if (field_width > it->end_charpos - charpos)
4962 it->end_charpos = charpos + field_width;
4964 /* Use the standard display table for displaying strings. */
4965 if (DISP_TABLE_P (Vstandard_display_table))
4966 it->dp = XCHAR_TABLE (Vstandard_display_table);
4968 it->stop_charpos = charpos;
4969 CHECK_IT (it);
4974 /***********************************************************************
4975 Iteration
4976 ***********************************************************************/
4978 /* Map enum it_method value to corresponding next_element_from_* function. */
4980 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
4982 next_element_from_buffer,
4983 next_element_from_display_vector,
4984 next_element_from_composition,
4985 next_element_from_string,
4986 next_element_from_c_string,
4987 next_element_from_image,
4988 next_element_from_stretch
4992 /* Load IT's display element fields with information about the next
4993 display element from the current position of IT. Value is zero if
4994 end of buffer (or C string) is reached. */
4997 get_next_display_element (it)
4998 struct it *it;
5000 /* Non-zero means that we found a display element. Zero means that
5001 we hit the end of what we iterate over. Performance note: the
5002 function pointer `method' used here turns out to be faster than
5003 using a sequence of if-statements. */
5004 int success_p;
5006 get_next:
5007 success_p = (*get_next_element[it->method]) (it);
5009 if (it->what == IT_CHARACTER)
5011 /* Map via display table or translate control characters.
5012 IT->c, IT->len etc. have been set to the next character by
5013 the function call above. If we have a display table, and it
5014 contains an entry for IT->c, translate it. Don't do this if
5015 IT->c itself comes from a display table, otherwise we could
5016 end up in an infinite recursion. (An alternative could be to
5017 count the recursion depth of this function and signal an
5018 error when a certain maximum depth is reached.) Is it worth
5019 it? */
5020 if (success_p && it->dpvec == NULL)
5022 Lisp_Object dv;
5024 if (it->dp
5025 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5026 VECTORP (dv)))
5028 struct Lisp_Vector *v = XVECTOR (dv);
5030 /* Return the first character from the display table
5031 entry, if not empty. If empty, don't display the
5032 current character. */
5033 if (v->size)
5035 it->dpvec_char_len = it->len;
5036 it->dpvec = v->contents;
5037 it->dpend = v->contents + v->size;
5038 it->current.dpvec_index = 0;
5039 it->dpvec_face_id = -1;
5040 it->saved_face_id = it->face_id;
5041 it->method = GET_FROM_DISPLAY_VECTOR;
5042 it->ellipsis_p = 0;
5044 else
5046 set_iterator_to_next (it, 0);
5048 goto get_next;
5051 /* Translate control characters into `\003' or `^C' form.
5052 Control characters coming from a display table entry are
5053 currently not translated because we use IT->dpvec to hold
5054 the translation. This could easily be changed but I
5055 don't believe that it is worth doing.
5057 If it->multibyte_p is nonzero, eight-bit characters and
5058 non-printable multibyte characters are also translated to
5059 octal form.
5061 If it->multibyte_p is zero, eight-bit characters that
5062 don't have corresponding multibyte char code are also
5063 translated to octal form. */
5064 else if ((it->c < ' '
5065 && (it->area != TEXT_AREA
5066 /* In mode line, treat \n like other crl chars. */
5067 || (it->c != '\t'
5068 && it->glyph_row && it->glyph_row->mode_line_p)
5069 || (it->c != '\n' && it->c != '\t')))
5070 || (it->multibyte_p
5071 ? ((it->c >= 127
5072 && it->len == 1)
5073 || !CHAR_PRINTABLE_P (it->c)
5074 || (!NILP (Vshow_nonbreak_escape)
5075 && (it->c == 0x8ad || it->c == 0x8a0)))
5076 : (it->c >= 127
5077 && (!unibyte_display_via_language_environment
5078 || it->c == unibyte_char_to_multibyte (it->c)))))
5080 /* IT->c is a control character which must be displayed
5081 either as '\003' or as `^C' where the '\\' and '^'
5082 can be defined in the display table. Fill
5083 IT->ctl_chars with glyphs for what we have to
5084 display. Then, set IT->dpvec to these glyphs. */
5085 GLYPH g;
5086 int ctl_len;
5087 int face_id, lface_id;
5088 GLYPH escape_glyph;
5090 if (it->c < 128 && it->ctl_arrow_p)
5092 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5093 if (it->dp
5094 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5095 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5097 g = XINT (DISP_CTRL_GLYPH (it->dp));
5098 lface_id = FAST_GLYPH_FACE (g);
5099 if (lface_id)
5101 g = FAST_GLYPH_CHAR (g);
5102 face_id = merge_faces (it->f, Qt, lface_id,
5103 it->face_id);
5106 else
5108 /* Merge the escape-glyph face into the current face. */
5109 face_id = merge_faces (it->f, Qescape_glyph, 0,
5110 it->face_id);
5111 g = '^';
5114 XSETINT (it->ctl_chars[0], g);
5115 g = it->c ^ 0100;
5116 XSETINT (it->ctl_chars[1], g);
5117 ctl_len = 2;
5118 goto display_control;
5121 if (it->dp
5122 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5123 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5125 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5126 lface_id = FAST_GLYPH_FACE (escape_glyph);
5127 if (lface_id)
5129 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5130 face_id = merge_faces (it->f, Qt, lface_id,
5131 it->face_id);
5134 else
5136 /* Merge the escape-glyph face into the current face. */
5137 face_id = merge_faces (it->f, Qescape_glyph, 0,
5138 it->face_id);
5139 escape_glyph = '\\';
5142 if (it->c == 0x8a0 || it->c == 0x8ad)
5144 XSETINT (it->ctl_chars[0], escape_glyph);
5145 g = it->c == 0x8ad ? '-' : ' ';
5146 XSETINT (it->ctl_chars[1], g);
5147 ctl_len = 2;
5148 goto display_control;
5152 unsigned char str[MAX_MULTIBYTE_LENGTH];
5153 int len;
5154 int i;
5156 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5157 if (SINGLE_BYTE_CHAR_P (it->c))
5158 str[0] = it->c, len = 1;
5159 else
5161 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5162 if (len < 0)
5164 /* It's an invalid character, which shouldn't
5165 happen actually, but due to bugs it may
5166 happen. Let's print the char as is, there's
5167 not much meaningful we can do with it. */
5168 str[0] = it->c;
5169 str[1] = it->c >> 8;
5170 str[2] = it->c >> 16;
5171 str[3] = it->c >> 24;
5172 len = 4;
5176 for (i = 0; i < len; i++)
5178 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5179 /* Insert three more glyphs into IT->ctl_chars for
5180 the octal display of the character. */
5181 g = ((str[i] >> 6) & 7) + '0';
5182 XSETINT (it->ctl_chars[i * 4 + 1], g);
5183 g = ((str[i] >> 3) & 7) + '0';
5184 XSETINT (it->ctl_chars[i * 4 + 2], g);
5185 g = (str[i] & 7) + '0';
5186 XSETINT (it->ctl_chars[i * 4 + 3], g);
5188 ctl_len = len * 4;
5191 display_control:
5192 /* Set up IT->dpvec and return first character from it. */
5193 it->dpvec_char_len = it->len;
5194 it->dpvec = it->ctl_chars;
5195 it->dpend = it->dpvec + ctl_len;
5196 it->current.dpvec_index = 0;
5197 it->dpvec_face_id = face_id;
5198 it->saved_face_id = it->face_id;
5199 it->method = GET_FROM_DISPLAY_VECTOR;
5200 it->ellipsis_p = 0;
5201 goto get_next;
5205 /* Adjust face id for a multibyte character. There are no
5206 multibyte character in unibyte text. */
5207 if (it->multibyte_p
5208 && success_p
5209 && FRAME_WINDOW_P (it->f))
5211 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5212 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5216 /* Is this character the last one of a run of characters with
5217 box? If yes, set IT->end_of_box_run_p to 1. */
5218 if (it->face_box_p
5219 && it->s == NULL)
5221 int face_id;
5222 struct face *face;
5224 it->end_of_box_run_p
5225 = ((face_id = face_after_it_pos (it),
5226 face_id != it->face_id)
5227 && (face = FACE_FROM_ID (it->f, face_id),
5228 face->box == FACE_NO_BOX));
5231 /* Value is 0 if end of buffer or string reached. */
5232 return success_p;
5236 /* Move IT to the next display element.
5238 RESEAT_P non-zero means if called on a newline in buffer text,
5239 skip to the next visible line start.
5241 Functions get_next_display_element and set_iterator_to_next are
5242 separate because I find this arrangement easier to handle than a
5243 get_next_display_element function that also increments IT's
5244 position. The way it is we can first look at an iterator's current
5245 display element, decide whether it fits on a line, and if it does,
5246 increment the iterator position. The other way around we probably
5247 would either need a flag indicating whether the iterator has to be
5248 incremented the next time, or we would have to implement a
5249 decrement position function which would not be easy to write. */
5251 void
5252 set_iterator_to_next (it, reseat_p)
5253 struct it *it;
5254 int reseat_p;
5256 /* Reset flags indicating start and end of a sequence of characters
5257 with box. Reset them at the start of this function because
5258 moving the iterator to a new position might set them. */
5259 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5261 switch (it->method)
5263 case GET_FROM_BUFFER:
5264 /* The current display element of IT is a character from
5265 current_buffer. Advance in the buffer, and maybe skip over
5266 invisible lines that are so because of selective display. */
5267 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5268 reseat_at_next_visible_line_start (it, 0);
5269 else
5271 xassert (it->len != 0);
5272 IT_BYTEPOS (*it) += it->len;
5273 IT_CHARPOS (*it) += 1;
5274 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5276 break;
5278 case GET_FROM_COMPOSITION:
5279 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5280 if (STRINGP (it->string))
5282 IT_STRING_BYTEPOS (*it) += it->len;
5283 IT_STRING_CHARPOS (*it) += it->cmp_len;
5284 it->method = GET_FROM_STRING;
5285 goto consider_string_end;
5287 else
5289 IT_BYTEPOS (*it) += it->len;
5290 IT_CHARPOS (*it) += it->cmp_len;
5291 it->method = GET_FROM_BUFFER;
5293 break;
5295 case GET_FROM_C_STRING:
5296 /* Current display element of IT is from a C string. */
5297 IT_BYTEPOS (*it) += it->len;
5298 IT_CHARPOS (*it) += 1;
5299 break;
5301 case GET_FROM_DISPLAY_VECTOR:
5302 /* Current display element of IT is from a display table entry.
5303 Advance in the display table definition. Reset it to null if
5304 end reached, and continue with characters from buffers/
5305 strings. */
5306 ++it->current.dpvec_index;
5308 /* Restore face of the iterator to what they were before the
5309 display vector entry (these entries may contain faces). */
5310 it->face_id = it->saved_face_id;
5312 if (it->dpvec + it->current.dpvec_index == it->dpend)
5314 if (it->s)
5315 it->method = GET_FROM_C_STRING;
5316 else if (STRINGP (it->string))
5317 it->method = GET_FROM_STRING;
5318 else
5319 it->method = GET_FROM_BUFFER;
5321 it->dpvec = NULL;
5322 it->current.dpvec_index = -1;
5324 /* Skip over characters which were displayed via IT->dpvec. */
5325 if (it->dpvec_char_len < 0)
5326 reseat_at_next_visible_line_start (it, 1);
5327 else if (it->dpvec_char_len > 0)
5329 it->len = it->dpvec_char_len;
5330 set_iterator_to_next (it, reseat_p);
5333 /* Recheck faces after display vector */
5334 it->stop_charpos = IT_CHARPOS (*it);
5336 break;
5338 case GET_FROM_STRING:
5339 /* Current display element is a character from a Lisp string. */
5340 xassert (it->s == NULL && STRINGP (it->string));
5341 IT_STRING_BYTEPOS (*it) += it->len;
5342 IT_STRING_CHARPOS (*it) += 1;
5344 consider_string_end:
5346 if (it->current.overlay_string_index >= 0)
5348 /* IT->string is an overlay string. Advance to the
5349 next, if there is one. */
5350 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5351 next_overlay_string (it);
5353 else
5355 /* IT->string is not an overlay string. If we reached
5356 its end, and there is something on IT->stack, proceed
5357 with what is on the stack. This can be either another
5358 string, this time an overlay string, or a buffer. */
5359 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5360 && it->sp > 0)
5362 pop_it (it);
5363 if (STRINGP (it->string))
5364 goto consider_string_end;
5365 it->method = GET_FROM_BUFFER;
5368 break;
5370 case GET_FROM_IMAGE:
5371 case GET_FROM_STRETCH:
5372 /* The position etc with which we have to proceed are on
5373 the stack. The position may be at the end of a string,
5374 if the `display' property takes up the whole string. */
5375 xassert (it->sp > 0);
5376 pop_it (it);
5377 it->image_id = 0;
5378 if (STRINGP (it->string))
5380 it->method = GET_FROM_STRING;
5381 goto consider_string_end;
5383 it->method = GET_FROM_BUFFER;
5384 break;
5386 default:
5387 /* There are no other methods defined, so this should be a bug. */
5388 abort ();
5391 xassert (it->method != GET_FROM_STRING
5392 || (STRINGP (it->string)
5393 && IT_STRING_CHARPOS (*it) >= 0));
5396 /* Load IT's display element fields with information about the next
5397 display element which comes from a display table entry or from the
5398 result of translating a control character to one of the forms `^C'
5399 or `\003'.
5401 IT->dpvec holds the glyphs to return as characters.
5402 IT->saved_face_id holds the face id before the display vector--
5403 it is restored into IT->face_idin set_iterator_to_next. */
5405 static int
5406 next_element_from_display_vector (it)
5407 struct it *it;
5409 /* Precondition. */
5410 xassert (it->dpvec && it->current.dpvec_index >= 0);
5412 if (INTEGERP (*it->dpvec)
5413 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5415 GLYPH g;
5417 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5418 it->c = FAST_GLYPH_CHAR (g);
5419 it->len = CHAR_BYTES (it->c);
5421 /* The entry may contain a face id to use. Such a face id is
5422 the id of a Lisp face, not a realized face. A face id of
5423 zero means no face is specified. */
5424 if (it->dpvec_face_id >= 0)
5425 it->face_id = it->dpvec_face_id;
5426 else
5428 int lface_id = FAST_GLYPH_FACE (g);
5429 if (lface_id > 0)
5430 it->face_id = merge_faces (it->f, Qt, lface_id,
5431 it->saved_face_id);
5434 else
5435 /* Display table entry is invalid. Return a space. */
5436 it->c = ' ', it->len = 1;
5438 /* Don't change position and object of the iterator here. They are
5439 still the values of the character that had this display table
5440 entry or was translated, and that's what we want. */
5441 it->what = IT_CHARACTER;
5442 return 1;
5446 /* Load IT with the next display element from Lisp string IT->string.
5447 IT->current.string_pos is the current position within the string.
5448 If IT->current.overlay_string_index >= 0, the Lisp string is an
5449 overlay string. */
5451 static int
5452 next_element_from_string (it)
5453 struct it *it;
5455 struct text_pos position;
5457 xassert (STRINGP (it->string));
5458 xassert (IT_STRING_CHARPOS (*it) >= 0);
5459 position = it->current.string_pos;
5461 /* Time to check for invisible text? */
5462 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5463 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5465 handle_stop (it);
5467 /* Since a handler may have changed IT->method, we must
5468 recurse here. */
5469 return get_next_display_element (it);
5472 if (it->current.overlay_string_index >= 0)
5474 /* Get the next character from an overlay string. In overlay
5475 strings, There is no field width or padding with spaces to
5476 do. */
5477 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5479 it->what = IT_EOB;
5480 return 0;
5482 else if (STRING_MULTIBYTE (it->string))
5484 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5485 const unsigned char *s = (SDATA (it->string)
5486 + IT_STRING_BYTEPOS (*it));
5487 it->c = string_char_and_length (s, remaining, &it->len);
5489 else
5491 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5492 it->len = 1;
5495 else
5497 /* Get the next character from a Lisp string that is not an
5498 overlay string. Such strings come from the mode line, for
5499 example. We may have to pad with spaces, or truncate the
5500 string. See also next_element_from_c_string. */
5501 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5503 it->what = IT_EOB;
5504 return 0;
5506 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5508 /* Pad with spaces. */
5509 it->c = ' ', it->len = 1;
5510 CHARPOS (position) = BYTEPOS (position) = -1;
5512 else if (STRING_MULTIBYTE (it->string))
5514 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5515 const unsigned char *s = (SDATA (it->string)
5516 + IT_STRING_BYTEPOS (*it));
5517 it->c = string_char_and_length (s, maxlen, &it->len);
5519 else
5521 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5522 it->len = 1;
5526 /* Record what we have and where it came from. Note that we store a
5527 buffer position in IT->position although it could arguably be a
5528 string position. */
5529 it->what = IT_CHARACTER;
5530 it->object = it->string;
5531 it->position = position;
5532 return 1;
5536 /* Load IT with next display element from C string IT->s.
5537 IT->string_nchars is the maximum number of characters to return
5538 from the string. IT->end_charpos may be greater than
5539 IT->string_nchars when this function is called, in which case we
5540 may have to return padding spaces. Value is zero if end of string
5541 reached, including padding spaces. */
5543 static int
5544 next_element_from_c_string (it)
5545 struct it *it;
5547 int success_p = 1;
5549 xassert (it->s);
5550 it->what = IT_CHARACTER;
5551 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5552 it->object = Qnil;
5554 /* IT's position can be greater IT->string_nchars in case a field
5555 width or precision has been specified when the iterator was
5556 initialized. */
5557 if (IT_CHARPOS (*it) >= it->end_charpos)
5559 /* End of the game. */
5560 it->what = IT_EOB;
5561 success_p = 0;
5563 else if (IT_CHARPOS (*it) >= it->string_nchars)
5565 /* Pad with spaces. */
5566 it->c = ' ', it->len = 1;
5567 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5569 else if (it->multibyte_p)
5571 /* Implementation note: The calls to strlen apparently aren't a
5572 performance problem because there is no noticeable performance
5573 difference between Emacs running in unibyte or multibyte mode. */
5574 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5575 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5576 maxlen, &it->len);
5578 else
5579 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5581 return success_p;
5585 /* Set up IT to return characters from an ellipsis, if appropriate.
5586 The definition of the ellipsis glyphs may come from a display table
5587 entry. This function Fills IT with the first glyph from the
5588 ellipsis if an ellipsis is to be displayed. */
5590 static int
5591 next_element_from_ellipsis (it)
5592 struct it *it;
5594 if (it->selective_display_ellipsis_p)
5595 setup_for_ellipsis (it, it->len);
5596 else
5598 /* The face at the current position may be different from the
5599 face we find after the invisible text. Remember what it
5600 was in IT->saved_face_id, and signal that it's there by
5601 setting face_before_selective_p. */
5602 it->saved_face_id = it->face_id;
5603 it->method = GET_FROM_BUFFER;
5604 reseat_at_next_visible_line_start (it, 1);
5605 it->face_before_selective_p = 1;
5608 return get_next_display_element (it);
5612 /* Deliver an image display element. The iterator IT is already
5613 filled with image information (done in handle_display_prop). Value
5614 is always 1. */
5617 static int
5618 next_element_from_image (it)
5619 struct it *it;
5621 it->what = IT_IMAGE;
5622 return 1;
5626 /* Fill iterator IT with next display element from a stretch glyph
5627 property. IT->object is the value of the text property. Value is
5628 always 1. */
5630 static int
5631 next_element_from_stretch (it)
5632 struct it *it;
5634 it->what = IT_STRETCH;
5635 return 1;
5639 /* Load IT with the next display element from current_buffer. Value
5640 is zero if end of buffer reached. IT->stop_charpos is the next
5641 position at which to stop and check for text properties or buffer
5642 end. */
5644 static int
5645 next_element_from_buffer (it)
5646 struct it *it;
5648 int success_p = 1;
5650 /* Check this assumption, otherwise, we would never enter the
5651 if-statement, below. */
5652 xassert (IT_CHARPOS (*it) >= BEGV
5653 && IT_CHARPOS (*it) <= it->stop_charpos);
5655 if (IT_CHARPOS (*it) >= it->stop_charpos)
5657 if (IT_CHARPOS (*it) >= it->end_charpos)
5659 int overlay_strings_follow_p;
5661 /* End of the game, except when overlay strings follow that
5662 haven't been returned yet. */
5663 if (it->overlay_strings_at_end_processed_p)
5664 overlay_strings_follow_p = 0;
5665 else
5667 it->overlay_strings_at_end_processed_p = 1;
5668 overlay_strings_follow_p = get_overlay_strings (it, 0);
5671 if (overlay_strings_follow_p)
5672 success_p = get_next_display_element (it);
5673 else
5675 it->what = IT_EOB;
5676 it->position = it->current.pos;
5677 success_p = 0;
5680 else
5682 handle_stop (it);
5683 return get_next_display_element (it);
5686 else
5688 /* No face changes, overlays etc. in sight, so just return a
5689 character from current_buffer. */
5690 unsigned char *p;
5692 /* Maybe run the redisplay end trigger hook. Performance note:
5693 This doesn't seem to cost measurable time. */
5694 if (it->redisplay_end_trigger_charpos
5695 && it->glyph_row
5696 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5697 run_redisplay_end_trigger_hook (it);
5699 /* Get the next character, maybe multibyte. */
5700 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5701 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5703 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5704 - IT_BYTEPOS (*it));
5705 it->c = string_char_and_length (p, maxlen, &it->len);
5707 else
5708 it->c = *p, it->len = 1;
5710 /* Record what we have and where it came from. */
5711 it->what = IT_CHARACTER;;
5712 it->object = it->w->buffer;
5713 it->position = it->current.pos;
5715 /* Normally we return the character found above, except when we
5716 really want to return an ellipsis for selective display. */
5717 if (it->selective)
5719 if (it->c == '\n')
5721 /* A value of selective > 0 means hide lines indented more
5722 than that number of columns. */
5723 if (it->selective > 0
5724 && IT_CHARPOS (*it) + 1 < ZV
5725 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5726 IT_BYTEPOS (*it) + 1,
5727 (double) it->selective)) /* iftc */
5729 success_p = next_element_from_ellipsis (it);
5730 it->dpvec_char_len = -1;
5733 else if (it->c == '\r' && it->selective == -1)
5735 /* A value of selective == -1 means that everything from the
5736 CR to the end of the line is invisible, with maybe an
5737 ellipsis displayed for it. */
5738 success_p = next_element_from_ellipsis (it);
5739 it->dpvec_char_len = -1;
5744 /* Value is zero if end of buffer reached. */
5745 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5746 return success_p;
5750 /* Run the redisplay end trigger hook for IT. */
5752 static void
5753 run_redisplay_end_trigger_hook (it)
5754 struct it *it;
5756 Lisp_Object args[3];
5758 /* IT->glyph_row should be non-null, i.e. we should be actually
5759 displaying something, or otherwise we should not run the hook. */
5760 xassert (it->glyph_row);
5762 /* Set up hook arguments. */
5763 args[0] = Qredisplay_end_trigger_functions;
5764 args[1] = it->window;
5765 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5766 it->redisplay_end_trigger_charpos = 0;
5768 /* Since we are *trying* to run these functions, don't try to run
5769 them again, even if they get an error. */
5770 it->w->redisplay_end_trigger = Qnil;
5771 Frun_hook_with_args (3, args);
5773 /* Notice if it changed the face of the character we are on. */
5774 handle_face_prop (it);
5778 /* Deliver a composition display element. The iterator IT is already
5779 filled with composition information (done in
5780 handle_composition_prop). Value is always 1. */
5782 static int
5783 next_element_from_composition (it)
5784 struct it *it;
5786 it->what = IT_COMPOSITION;
5787 it->position = (STRINGP (it->string)
5788 ? it->current.string_pos
5789 : it->current.pos);
5790 return 1;
5795 /***********************************************************************
5796 Moving an iterator without producing glyphs
5797 ***********************************************************************/
5799 /* Move iterator IT to a specified buffer or X position within one
5800 line on the display without producing glyphs.
5802 OP should be a bit mask including some or all of these bits:
5803 MOVE_TO_X: Stop on reaching x-position TO_X.
5804 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5805 Regardless of OP's value, stop in reaching the end of the display line.
5807 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5808 This means, in particular, that TO_X includes window's horizontal
5809 scroll amount.
5811 The return value has several possible values that
5812 say what condition caused the scan to stop:
5814 MOVE_POS_MATCH_OR_ZV
5815 - when TO_POS or ZV was reached.
5817 MOVE_X_REACHED
5818 -when TO_X was reached before TO_POS or ZV were reached.
5820 MOVE_LINE_CONTINUED
5821 - when we reached the end of the display area and the line must
5822 be continued.
5824 MOVE_LINE_TRUNCATED
5825 - when we reached the end of the display area and the line is
5826 truncated.
5828 MOVE_NEWLINE_OR_CR
5829 - when we stopped at a line end, i.e. a newline or a CR and selective
5830 display is on. */
5832 static enum move_it_result
5833 move_it_in_display_line_to (it, to_charpos, to_x, op)
5834 struct it *it;
5835 int to_charpos, to_x, op;
5837 enum move_it_result result = MOVE_UNDEFINED;
5838 struct glyph_row *saved_glyph_row;
5840 /* Don't produce glyphs in produce_glyphs. */
5841 saved_glyph_row = it->glyph_row;
5842 it->glyph_row = NULL;
5844 #define BUFFER_POS_REACHED_P() \
5845 ((op & MOVE_TO_POS) != 0 \
5846 && BUFFERP (it->object) \
5847 && IT_CHARPOS (*it) >= to_charpos \
5848 && (it->method == GET_FROM_BUFFER || \
5849 (it->method == GET_FROM_DISPLAY_VECTOR && \
5850 it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5853 while (1)
5855 int x, i, ascent = 0, descent = 0;
5857 /* Stop when ZV reached.
5858 We used to stop here when TO_CHARPOS reached as well, but that is
5859 too soon if this glyph does not fit on this line. So we handle it
5860 explicitly below. */
5861 if (!get_next_display_element (it)
5862 || (it->truncate_lines_p
5863 && BUFFER_POS_REACHED_P ()))
5865 result = MOVE_POS_MATCH_OR_ZV;
5866 break;
5869 /* The call to produce_glyphs will get the metrics of the
5870 display element IT is loaded with. We record in x the
5871 x-position before this display element in case it does not
5872 fit on the line. */
5873 x = it->current_x;
5875 /* Remember the line height so far in case the next element doesn't
5876 fit on the line. */
5877 if (!it->truncate_lines_p)
5879 ascent = it->max_ascent;
5880 descent = it->max_descent;
5883 PRODUCE_GLYPHS (it);
5885 if (it->area != TEXT_AREA)
5887 set_iterator_to_next (it, 1);
5888 continue;
5891 /* The number of glyphs we get back in IT->nglyphs will normally
5892 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5893 character on a terminal frame, or (iii) a line end. For the
5894 second case, IT->nglyphs - 1 padding glyphs will be present
5895 (on X frames, there is only one glyph produced for a
5896 composite character.
5898 The behavior implemented below means, for continuation lines,
5899 that as many spaces of a TAB as fit on the current line are
5900 displayed there. For terminal frames, as many glyphs of a
5901 multi-glyph character are displayed in the current line, too.
5902 This is what the old redisplay code did, and we keep it that
5903 way. Under X, the whole shape of a complex character must
5904 fit on the line or it will be completely displayed in the
5905 next line.
5907 Note that both for tabs and padding glyphs, all glyphs have
5908 the same width. */
5909 if (it->nglyphs)
5911 /* More than one glyph or glyph doesn't fit on line. All
5912 glyphs have the same width. */
5913 int single_glyph_width = it->pixel_width / it->nglyphs;
5914 int new_x;
5916 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5918 new_x = x + single_glyph_width;
5920 /* We want to leave anything reaching TO_X to the caller. */
5921 if ((op & MOVE_TO_X) && new_x > to_x)
5923 if (BUFFER_POS_REACHED_P ())
5924 goto buffer_pos_reached;
5925 it->current_x = x;
5926 result = MOVE_X_REACHED;
5927 break;
5929 else if (/* Lines are continued. */
5930 !it->truncate_lines_p
5931 && (/* And glyph doesn't fit on the line. */
5932 new_x > it->last_visible_x
5933 /* Or it fits exactly and we're on a window
5934 system frame. */
5935 || (new_x == it->last_visible_x
5936 && FRAME_WINDOW_P (it->f))))
5938 if (/* IT->hpos == 0 means the very first glyph
5939 doesn't fit on the line, e.g. a wide image. */
5940 it->hpos == 0
5941 || (new_x == it->last_visible_x
5942 && FRAME_WINDOW_P (it->f)))
5944 ++it->hpos;
5945 it->current_x = new_x;
5946 if (i == it->nglyphs - 1)
5948 set_iterator_to_next (it, 1);
5949 #ifdef HAVE_WINDOW_SYSTEM
5950 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5952 if (!get_next_display_element (it))
5954 result = MOVE_POS_MATCH_OR_ZV;
5955 break;
5957 if (BUFFER_POS_REACHED_P ())
5959 if (ITERATOR_AT_END_OF_LINE_P (it))
5960 result = MOVE_POS_MATCH_OR_ZV;
5961 else
5962 result = MOVE_LINE_CONTINUED;
5963 break;
5965 if (ITERATOR_AT_END_OF_LINE_P (it))
5967 result = MOVE_NEWLINE_OR_CR;
5968 break;
5971 #endif /* HAVE_WINDOW_SYSTEM */
5974 else
5976 it->current_x = x;
5977 it->max_ascent = ascent;
5978 it->max_descent = descent;
5981 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5982 IT_CHARPOS (*it)));
5983 result = MOVE_LINE_CONTINUED;
5984 break;
5986 else if (BUFFER_POS_REACHED_P ())
5987 goto buffer_pos_reached;
5988 else if (new_x > it->first_visible_x)
5990 /* Glyph is visible. Increment number of glyphs that
5991 would be displayed. */
5992 ++it->hpos;
5994 else
5996 /* Glyph is completely off the left margin of the display
5997 area. Nothing to do. */
6001 if (result != MOVE_UNDEFINED)
6002 break;
6004 else if (BUFFER_POS_REACHED_P ())
6006 buffer_pos_reached:
6007 it->current_x = x;
6008 it->max_ascent = ascent;
6009 it->max_descent = descent;
6010 result = MOVE_POS_MATCH_OR_ZV;
6011 break;
6013 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6015 /* Stop when TO_X specified and reached. This check is
6016 necessary here because of lines consisting of a line end,
6017 only. The line end will not produce any glyphs and we
6018 would never get MOVE_X_REACHED. */
6019 xassert (it->nglyphs == 0);
6020 result = MOVE_X_REACHED;
6021 break;
6024 /* Is this a line end? If yes, we're done. */
6025 if (ITERATOR_AT_END_OF_LINE_P (it))
6027 result = MOVE_NEWLINE_OR_CR;
6028 break;
6031 /* The current display element has been consumed. Advance
6032 to the next. */
6033 set_iterator_to_next (it, 1);
6035 /* Stop if lines are truncated and IT's current x-position is
6036 past the right edge of the window now. */
6037 if (it->truncate_lines_p
6038 && it->current_x >= it->last_visible_x)
6040 #ifdef HAVE_WINDOW_SYSTEM
6041 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6043 if (!get_next_display_element (it)
6044 || BUFFER_POS_REACHED_P ())
6046 result = MOVE_POS_MATCH_OR_ZV;
6047 break;
6049 if (ITERATOR_AT_END_OF_LINE_P (it))
6051 result = MOVE_NEWLINE_OR_CR;
6052 break;
6055 #endif /* HAVE_WINDOW_SYSTEM */
6056 result = MOVE_LINE_TRUNCATED;
6057 break;
6061 #undef BUFFER_POS_REACHED_P
6063 /* Restore the iterator settings altered at the beginning of this
6064 function. */
6065 it->glyph_row = saved_glyph_row;
6066 return result;
6070 /* Move IT forward until it satisfies one or more of the criteria in
6071 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6073 OP is a bit-mask that specifies where to stop, and in particular,
6074 which of those four position arguments makes a difference. See the
6075 description of enum move_operation_enum.
6077 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6078 screen line, this function will set IT to the next position >
6079 TO_CHARPOS. */
6081 void
6082 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6083 struct it *it;
6084 int to_charpos, to_x, to_y, to_vpos;
6085 int op;
6087 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6088 int line_height;
6089 int reached = 0;
6091 for (;;)
6093 if (op & MOVE_TO_VPOS)
6095 /* If no TO_CHARPOS and no TO_X specified, stop at the
6096 start of the line TO_VPOS. */
6097 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6099 if (it->vpos == to_vpos)
6101 reached = 1;
6102 break;
6104 else
6105 skip = move_it_in_display_line_to (it, -1, -1, 0);
6107 else
6109 /* TO_VPOS >= 0 means stop at TO_X in the line at
6110 TO_VPOS, or at TO_POS, whichever comes first. */
6111 if (it->vpos == to_vpos)
6113 reached = 2;
6114 break;
6117 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6119 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6121 reached = 3;
6122 break;
6124 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6126 /* We have reached TO_X but not in the line we want. */
6127 skip = move_it_in_display_line_to (it, to_charpos,
6128 -1, MOVE_TO_POS);
6129 if (skip == MOVE_POS_MATCH_OR_ZV)
6131 reached = 4;
6132 break;
6137 else if (op & MOVE_TO_Y)
6139 struct it it_backup;
6141 /* TO_Y specified means stop at TO_X in the line containing
6142 TO_Y---or at TO_CHARPOS if this is reached first. The
6143 problem is that we can't really tell whether the line
6144 contains TO_Y before we have completely scanned it, and
6145 this may skip past TO_X. What we do is to first scan to
6146 TO_X.
6148 If TO_X is not specified, use a TO_X of zero. The reason
6149 is to make the outcome of this function more predictable.
6150 If we didn't use TO_X == 0, we would stop at the end of
6151 the line which is probably not what a caller would expect
6152 to happen. */
6153 skip = move_it_in_display_line_to (it, to_charpos,
6154 ((op & MOVE_TO_X)
6155 ? to_x : 0),
6156 (MOVE_TO_X
6157 | (op & MOVE_TO_POS)));
6159 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6160 if (skip == MOVE_POS_MATCH_OR_ZV)
6162 reached = 5;
6163 break;
6166 /* If TO_X was reached, we would like to know whether TO_Y
6167 is in the line. This can only be said if we know the
6168 total line height which requires us to scan the rest of
6169 the line. */
6170 if (skip == MOVE_X_REACHED)
6172 it_backup = *it;
6173 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6174 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6175 op & MOVE_TO_POS);
6176 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6179 /* Now, decide whether TO_Y is in this line. */
6180 line_height = it->max_ascent + it->max_descent;
6181 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6183 if (to_y >= it->current_y
6184 && to_y < it->current_y + line_height)
6186 if (skip == MOVE_X_REACHED)
6187 /* If TO_Y is in this line and TO_X was reached above,
6188 we scanned too far. We have to restore IT's settings
6189 to the ones before skipping. */
6190 *it = it_backup;
6191 reached = 6;
6193 else if (skip == MOVE_X_REACHED)
6195 skip = skip2;
6196 if (skip == MOVE_POS_MATCH_OR_ZV)
6197 reached = 7;
6200 if (reached)
6201 break;
6203 else
6204 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6206 switch (skip)
6208 case MOVE_POS_MATCH_OR_ZV:
6209 reached = 8;
6210 goto out;
6212 case MOVE_NEWLINE_OR_CR:
6213 set_iterator_to_next (it, 1);
6214 it->continuation_lines_width = 0;
6215 break;
6217 case MOVE_LINE_TRUNCATED:
6218 it->continuation_lines_width = 0;
6219 reseat_at_next_visible_line_start (it, 0);
6220 if ((op & MOVE_TO_POS) != 0
6221 && IT_CHARPOS (*it) > to_charpos)
6223 reached = 9;
6224 goto out;
6226 break;
6228 case MOVE_LINE_CONTINUED:
6229 it->continuation_lines_width += it->current_x;
6230 break;
6232 default:
6233 abort ();
6236 /* Reset/increment for the next run. */
6237 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6238 it->current_x = it->hpos = 0;
6239 it->current_y += it->max_ascent + it->max_descent;
6240 ++it->vpos;
6241 last_height = it->max_ascent + it->max_descent;
6242 last_max_ascent = it->max_ascent;
6243 it->max_ascent = it->max_descent = 0;
6246 out:
6248 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6252 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6254 If DY > 0, move IT backward at least that many pixels. DY = 0
6255 means move IT backward to the preceding line start or BEGV. This
6256 function may move over more than DY pixels if IT->current_y - DY
6257 ends up in the middle of a line; in this case IT->current_y will be
6258 set to the top of the line moved to. */
6260 void
6261 move_it_vertically_backward (it, dy)
6262 struct it *it;
6263 int dy;
6265 int nlines, h;
6266 struct it it2, it3;
6267 int start_pos;
6269 move_further_back:
6270 xassert (dy >= 0);
6272 start_pos = IT_CHARPOS (*it);
6274 /* Estimate how many newlines we must move back. */
6275 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6277 /* Set the iterator's position that many lines back. */
6278 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6279 back_to_previous_visible_line_start (it);
6281 /* Reseat the iterator here. When moving backward, we don't want
6282 reseat to skip forward over invisible text, set up the iterator
6283 to deliver from overlay strings at the new position etc. So,
6284 use reseat_1 here. */
6285 reseat_1 (it, it->current.pos, 1);
6287 /* We are now surely at a line start. */
6288 it->current_x = it->hpos = 0;
6289 it->continuation_lines_width = 0;
6291 /* Move forward and see what y-distance we moved. First move to the
6292 start of the next line so that we get its height. We need this
6293 height to be able to tell whether we reached the specified
6294 y-distance. */
6295 it2 = *it;
6296 it2.max_ascent = it2.max_descent = 0;
6297 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6298 MOVE_TO_POS | MOVE_TO_VPOS);
6299 xassert (IT_CHARPOS (*it) >= BEGV);
6300 it3 = it2;
6302 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6303 xassert (IT_CHARPOS (*it) >= BEGV);
6304 /* H is the actual vertical distance from the position in *IT
6305 and the starting position. */
6306 h = it2.current_y - it->current_y;
6307 /* NLINES is the distance in number of lines. */
6308 nlines = it2.vpos - it->vpos;
6310 /* Correct IT's y and vpos position
6311 so that they are relative to the starting point. */
6312 it->vpos -= nlines;
6313 it->current_y -= h;
6315 if (dy == 0)
6317 /* DY == 0 means move to the start of the screen line. The
6318 value of nlines is > 0 if continuation lines were involved. */
6319 if (nlines > 0)
6320 move_it_by_lines (it, nlines, 1);
6321 #if 0
6322 /* I think this assert is bogus if buffer contains
6323 invisible text or images. KFS. */
6324 xassert (IT_CHARPOS (*it) <= start_pos);
6325 #endif
6327 else
6329 /* The y-position we try to reach, relative to *IT.
6330 Note that H has been subtracted in front of the if-statement. */
6331 int target_y = it->current_y + h - dy;
6332 int y0 = it3.current_y;
6333 int y1 = line_bottom_y (&it3);
6334 int line_height = y1 - y0;
6336 /* If we did not reach target_y, try to move further backward if
6337 we can. If we moved too far backward, try to move forward. */
6338 if (target_y < it->current_y
6339 /* This is heuristic. In a window that's 3 lines high, with
6340 a line height of 13 pixels each, recentering with point
6341 on the bottom line will try to move -39/2 = 19 pixels
6342 backward. Try to avoid moving into the first line. */
6343 && (it->current_y - target_y
6344 > min (window_box_height (it->w), line_height * 2 / 3))
6345 && IT_CHARPOS (*it) > BEGV)
6347 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6348 target_y - it->current_y));
6349 dy = it->current_y - target_y;
6350 goto move_further_back;
6352 else if (target_y >= it->current_y + line_height
6353 && IT_CHARPOS (*it) < ZV)
6355 /* Should move forward by at least one line, maybe more.
6357 Note: Calling move_it_by_lines can be expensive on
6358 terminal frames, where compute_motion is used (via
6359 vmotion) to do the job, when there are very long lines
6360 and truncate-lines is nil. That's the reason for
6361 treating terminal frames specially here. */
6363 if (!FRAME_WINDOW_P (it->f))
6364 move_it_vertically (it, target_y - (it->current_y + line_height));
6365 else
6369 move_it_by_lines (it, 1, 1);
6371 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6374 #if 0
6375 /* I think this assert is bogus if buffer contains
6376 invisible text or images. KFS. */
6377 xassert (IT_CHARPOS (*it) >= BEGV);
6378 #endif
6384 /* Move IT by a specified amount of pixel lines DY. DY negative means
6385 move backwards. DY = 0 means move to start of screen line. At the
6386 end, IT will be on the start of a screen line. */
6388 void
6389 move_it_vertically (it, dy)
6390 struct it *it;
6391 int dy;
6393 if (dy <= 0)
6394 move_it_vertically_backward (it, -dy);
6395 else
6397 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6398 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6399 MOVE_TO_POS | MOVE_TO_Y);
6400 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6402 /* If buffer ends in ZV without a newline, move to the start of
6403 the line to satisfy the post-condition. */
6404 if (IT_CHARPOS (*it) == ZV
6405 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6406 move_it_by_lines (it, 0, 0);
6411 /* Move iterator IT past the end of the text line it is in. */
6413 void
6414 move_it_past_eol (it)
6415 struct it *it;
6417 enum move_it_result rc;
6419 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6420 if (rc == MOVE_NEWLINE_OR_CR)
6421 set_iterator_to_next (it, 0);
6425 #if 0 /* Currently not used. */
6427 /* Return non-zero if some text between buffer positions START_CHARPOS
6428 and END_CHARPOS is invisible. IT->window is the window for text
6429 property lookup. */
6431 static int
6432 invisible_text_between_p (it, start_charpos, end_charpos)
6433 struct it *it;
6434 int start_charpos, end_charpos;
6436 Lisp_Object prop, limit;
6437 int invisible_found_p;
6439 xassert (it != NULL && start_charpos <= end_charpos);
6441 /* Is text at START invisible? */
6442 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6443 it->window);
6444 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6445 invisible_found_p = 1;
6446 else
6448 limit = Fnext_single_char_property_change (make_number (start_charpos),
6449 Qinvisible, Qnil,
6450 make_number (end_charpos));
6451 invisible_found_p = XFASTINT (limit) < end_charpos;
6454 return invisible_found_p;
6457 #endif /* 0 */
6460 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6461 negative means move up. DVPOS == 0 means move to the start of the
6462 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6463 NEED_Y_P is zero, IT->current_y will be left unchanged.
6465 Further optimization ideas: If we would know that IT->f doesn't use
6466 a face with proportional font, we could be faster for
6467 truncate-lines nil. */
6469 void
6470 move_it_by_lines (it, dvpos, need_y_p)
6471 struct it *it;
6472 int dvpos, need_y_p;
6474 struct position pos;
6476 if (!FRAME_WINDOW_P (it->f))
6478 struct text_pos textpos;
6480 /* We can use vmotion on frames without proportional fonts. */
6481 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6482 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6483 reseat (it, textpos, 1);
6484 it->vpos += pos.vpos;
6485 it->current_y += pos.vpos;
6487 else if (dvpos == 0)
6489 /* DVPOS == 0 means move to the start of the screen line. */
6490 move_it_vertically_backward (it, 0);
6491 xassert (it->current_x == 0 && it->hpos == 0);
6492 /* Let next call to line_bottom_y calculate real line height */
6493 last_height = 0;
6495 else if (dvpos > 0)
6496 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6497 else
6499 struct it it2;
6500 int start_charpos, i;
6502 /* Start at the beginning of the screen line containing IT's
6503 position. */
6504 move_it_vertically_backward (it, 0);
6506 /* Go back -DVPOS visible lines and reseat the iterator there. */
6507 start_charpos = IT_CHARPOS (*it);
6508 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6509 back_to_previous_visible_line_start (it);
6510 reseat (it, it->current.pos, 1);
6511 it->current_x = it->hpos = 0;
6513 /* Above call may have moved too far if continuation lines
6514 are involved. Scan forward and see if it did. */
6515 it2 = *it;
6516 it2.vpos = it2.current_y = 0;
6517 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6518 it->vpos -= it2.vpos;
6519 it->current_y -= it2.current_y;
6520 it->current_x = it->hpos = 0;
6522 /* If we moved too far back, move IT some lines forward. */
6523 if (it2.vpos > -dvpos)
6525 int delta = it2.vpos + dvpos;
6526 it2 = *it;
6527 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6528 /* Move back again if we got too far ahead. */
6529 if (IT_CHARPOS (*it) >= start_charpos)
6530 *it = it2;
6535 /* Return 1 if IT points into the middle of a display vector. */
6538 in_display_vector_p (it)
6539 struct it *it;
6541 return (it->method == GET_FROM_DISPLAY_VECTOR
6542 && it->current.dpvec_index > 0
6543 && it->dpvec + it->current.dpvec_index != it->dpend);
6547 /***********************************************************************
6548 Messages
6549 ***********************************************************************/
6552 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6553 to *Messages*. */
6555 void
6556 add_to_log (format, arg1, arg2)
6557 char *format;
6558 Lisp_Object arg1, arg2;
6560 Lisp_Object args[3];
6561 Lisp_Object msg, fmt;
6562 char *buffer;
6563 int len;
6564 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6565 USE_SAFE_ALLOCA;
6567 /* Do nothing if called asynchronously. Inserting text into
6568 a buffer may call after-change-functions and alike and
6569 that would means running Lisp asynchronously. */
6570 if (handling_signal)
6571 return;
6573 fmt = msg = Qnil;
6574 GCPRO4 (fmt, msg, arg1, arg2);
6576 args[0] = fmt = build_string (format);
6577 args[1] = arg1;
6578 args[2] = arg2;
6579 msg = Fformat (3, args);
6581 len = SBYTES (msg) + 1;
6582 SAFE_ALLOCA (buffer, char *, len);
6583 bcopy (SDATA (msg), buffer, len);
6585 message_dolog (buffer, len - 1, 1, 0);
6586 SAFE_FREE ();
6588 UNGCPRO;
6592 /* Output a newline in the *Messages* buffer if "needs" one. */
6594 void
6595 message_log_maybe_newline ()
6597 if (message_log_need_newline)
6598 message_dolog ("", 0, 1, 0);
6602 /* Add a string M of length NBYTES to the message log, optionally
6603 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6604 nonzero, means interpret the contents of M as multibyte. This
6605 function calls low-level routines in order to bypass text property
6606 hooks, etc. which might not be safe to run. */
6608 void
6609 message_dolog (m, nbytes, nlflag, multibyte)
6610 const char *m;
6611 int nbytes, nlflag, multibyte;
6613 if (!NILP (Vmemory_full))
6614 return;
6616 if (!NILP (Vmessage_log_max))
6618 struct buffer *oldbuf;
6619 Lisp_Object oldpoint, oldbegv, oldzv;
6620 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6621 int point_at_end = 0;
6622 int zv_at_end = 0;
6623 Lisp_Object old_deactivate_mark, tem;
6624 struct gcpro gcpro1;
6626 old_deactivate_mark = Vdeactivate_mark;
6627 oldbuf = current_buffer;
6628 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6629 current_buffer->undo_list = Qt;
6631 oldpoint = message_dolog_marker1;
6632 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6633 oldbegv = message_dolog_marker2;
6634 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6635 oldzv = message_dolog_marker3;
6636 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6637 GCPRO1 (old_deactivate_mark);
6639 if (PT == Z)
6640 point_at_end = 1;
6641 if (ZV == Z)
6642 zv_at_end = 1;
6644 BEGV = BEG;
6645 BEGV_BYTE = BEG_BYTE;
6646 ZV = Z;
6647 ZV_BYTE = Z_BYTE;
6648 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6650 /* Insert the string--maybe converting multibyte to single byte
6651 or vice versa, so that all the text fits the buffer. */
6652 if (multibyte
6653 && NILP (current_buffer->enable_multibyte_characters))
6655 int i, c, char_bytes;
6656 unsigned char work[1];
6658 /* Convert a multibyte string to single-byte
6659 for the *Message* buffer. */
6660 for (i = 0; i < nbytes; i += char_bytes)
6662 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6663 work[0] = (SINGLE_BYTE_CHAR_P (c)
6665 : multibyte_char_to_unibyte (c, Qnil));
6666 insert_1_both (work, 1, 1, 1, 0, 0);
6669 else if (! multibyte
6670 && ! NILP (current_buffer->enable_multibyte_characters))
6672 int i, c, char_bytes;
6673 unsigned char *msg = (unsigned char *) m;
6674 unsigned char str[MAX_MULTIBYTE_LENGTH];
6675 /* Convert a single-byte string to multibyte
6676 for the *Message* buffer. */
6677 for (i = 0; i < nbytes; i++)
6679 c = unibyte_char_to_multibyte (msg[i]);
6680 char_bytes = CHAR_STRING (c, str);
6681 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6684 else if (nbytes)
6685 insert_1 (m, nbytes, 1, 0, 0);
6687 if (nlflag)
6689 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6690 insert_1 ("\n", 1, 1, 0, 0);
6692 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6693 this_bol = PT;
6694 this_bol_byte = PT_BYTE;
6696 /* See if this line duplicates the previous one.
6697 If so, combine duplicates. */
6698 if (this_bol > BEG)
6700 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6701 prev_bol = PT;
6702 prev_bol_byte = PT_BYTE;
6704 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6705 this_bol, this_bol_byte);
6706 if (dup)
6708 del_range_both (prev_bol, prev_bol_byte,
6709 this_bol, this_bol_byte, 0);
6710 if (dup > 1)
6712 char dupstr[40];
6713 int duplen;
6715 /* If you change this format, don't forget to also
6716 change message_log_check_duplicate. */
6717 sprintf (dupstr, " [%d times]", dup);
6718 duplen = strlen (dupstr);
6719 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6720 insert_1 (dupstr, duplen, 1, 0, 1);
6725 /* If we have more than the desired maximum number of lines
6726 in the *Messages* buffer now, delete the oldest ones.
6727 This is safe because we don't have undo in this buffer. */
6729 if (NATNUMP (Vmessage_log_max))
6731 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6732 -XFASTINT (Vmessage_log_max) - 1, 0);
6733 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6736 BEGV = XMARKER (oldbegv)->charpos;
6737 BEGV_BYTE = marker_byte_position (oldbegv);
6739 if (zv_at_end)
6741 ZV = Z;
6742 ZV_BYTE = Z_BYTE;
6744 else
6746 ZV = XMARKER (oldzv)->charpos;
6747 ZV_BYTE = marker_byte_position (oldzv);
6750 if (point_at_end)
6751 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6752 else
6753 /* We can't do Fgoto_char (oldpoint) because it will run some
6754 Lisp code. */
6755 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6756 XMARKER (oldpoint)->bytepos);
6758 UNGCPRO;
6759 unchain_marker (XMARKER (oldpoint));
6760 unchain_marker (XMARKER (oldbegv));
6761 unchain_marker (XMARKER (oldzv));
6763 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6764 set_buffer_internal (oldbuf);
6765 if (NILP (tem))
6766 windows_or_buffers_changed = old_windows_or_buffers_changed;
6767 message_log_need_newline = !nlflag;
6768 Vdeactivate_mark = old_deactivate_mark;
6773 /* We are at the end of the buffer after just having inserted a newline.
6774 (Note: We depend on the fact we won't be crossing the gap.)
6775 Check to see if the most recent message looks a lot like the previous one.
6776 Return 0 if different, 1 if the new one should just replace it, or a
6777 value N > 1 if we should also append " [N times]". */
6779 static int
6780 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6781 int prev_bol, this_bol;
6782 int prev_bol_byte, this_bol_byte;
6784 int i;
6785 int len = Z_BYTE - 1 - this_bol_byte;
6786 int seen_dots = 0;
6787 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6788 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6790 for (i = 0; i < len; i++)
6792 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6793 seen_dots = 1;
6794 if (p1[i] != p2[i])
6795 return seen_dots;
6797 p1 += len;
6798 if (*p1 == '\n')
6799 return 2;
6800 if (*p1++ == ' ' && *p1++ == '[')
6802 int n = 0;
6803 while (*p1 >= '0' && *p1 <= '9')
6804 n = n * 10 + *p1++ - '0';
6805 if (strncmp (p1, " times]\n", 8) == 0)
6806 return n+1;
6808 return 0;
6812 /* Display an echo area message M with a specified length of NBYTES
6813 bytes. The string may include null characters. If M is 0, clear
6814 out any existing message, and let the mini-buffer text show
6815 through.
6817 The buffer M must continue to exist until after the echo area gets
6818 cleared or some other message gets displayed there. This means do
6819 not pass text that is stored in a Lisp string; do not pass text in
6820 a buffer that was alloca'd. */
6822 void
6823 message2 (m, nbytes, multibyte)
6824 const char *m;
6825 int nbytes;
6826 int multibyte;
6828 /* First flush out any partial line written with print. */
6829 message_log_maybe_newline ();
6830 if (m)
6831 message_dolog (m, nbytes, 1, multibyte);
6832 message2_nolog (m, nbytes, multibyte);
6836 /* The non-logging counterpart of message2. */
6838 void
6839 message2_nolog (m, nbytes, multibyte)
6840 const char *m;
6841 int nbytes, multibyte;
6843 struct frame *sf = SELECTED_FRAME ();
6844 message_enable_multibyte = multibyte;
6846 if (noninteractive)
6848 if (noninteractive_need_newline)
6849 putc ('\n', stderr);
6850 noninteractive_need_newline = 0;
6851 if (m)
6852 fwrite (m, nbytes, 1, stderr);
6853 if (cursor_in_echo_area == 0)
6854 fprintf (stderr, "\n");
6855 fflush (stderr);
6857 /* A null message buffer means that the frame hasn't really been
6858 initialized yet. Error messages get reported properly by
6859 cmd_error, so this must be just an informative message; toss it. */
6860 else if (INTERACTIVE
6861 && sf->glyphs_initialized_p
6862 && FRAME_MESSAGE_BUF (sf))
6864 Lisp_Object mini_window;
6865 struct frame *f;
6867 /* Get the frame containing the mini-buffer
6868 that the selected frame is using. */
6869 mini_window = FRAME_MINIBUF_WINDOW (sf);
6870 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6872 FRAME_SAMPLE_VISIBILITY (f);
6873 if (FRAME_VISIBLE_P (sf)
6874 && ! FRAME_VISIBLE_P (f))
6875 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6877 if (m)
6879 set_message (m, Qnil, nbytes, multibyte);
6880 if (minibuffer_auto_raise)
6881 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6883 else
6884 clear_message (1, 1);
6886 do_pending_window_change (0);
6887 echo_area_display (1);
6888 do_pending_window_change (0);
6889 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6890 (*frame_up_to_date_hook) (f);
6895 /* Display an echo area message M with a specified length of NBYTES
6896 bytes. The string may include null characters. If M is not a
6897 string, clear out any existing message, and let the mini-buffer
6898 text show through. */
6900 void
6901 message3 (m, nbytes, multibyte)
6902 Lisp_Object m;
6903 int nbytes;
6904 int multibyte;
6906 struct gcpro gcpro1;
6908 GCPRO1 (m);
6909 clear_message (1,1);
6911 /* First flush out any partial line written with print. */
6912 message_log_maybe_newline ();
6913 if (STRINGP (m))
6914 message_dolog (SDATA (m), nbytes, 1, multibyte);
6915 message3_nolog (m, nbytes, multibyte);
6917 UNGCPRO;
6921 /* The non-logging version of message3. */
6923 void
6924 message3_nolog (m, nbytes, multibyte)
6925 Lisp_Object m;
6926 int nbytes, multibyte;
6928 struct frame *sf = SELECTED_FRAME ();
6929 message_enable_multibyte = multibyte;
6931 if (noninteractive)
6933 if (noninteractive_need_newline)
6934 putc ('\n', stderr);
6935 noninteractive_need_newline = 0;
6936 if (STRINGP (m))
6937 fwrite (SDATA (m), nbytes, 1, stderr);
6938 if (cursor_in_echo_area == 0)
6939 fprintf (stderr, "\n");
6940 fflush (stderr);
6942 /* A null message buffer means that the frame hasn't really been
6943 initialized yet. Error messages get reported properly by
6944 cmd_error, so this must be just an informative message; toss it. */
6945 else if (INTERACTIVE
6946 && sf->glyphs_initialized_p
6947 && FRAME_MESSAGE_BUF (sf))
6949 Lisp_Object mini_window;
6950 Lisp_Object frame;
6951 struct frame *f;
6953 /* Get the frame containing the mini-buffer
6954 that the selected frame is using. */
6955 mini_window = FRAME_MINIBUF_WINDOW (sf);
6956 frame = XWINDOW (mini_window)->frame;
6957 f = XFRAME (frame);
6959 FRAME_SAMPLE_VISIBILITY (f);
6960 if (FRAME_VISIBLE_P (sf)
6961 && !FRAME_VISIBLE_P (f))
6962 Fmake_frame_visible (frame);
6964 if (STRINGP (m) && SCHARS (m) > 0)
6966 set_message (NULL, m, nbytes, multibyte);
6967 if (minibuffer_auto_raise)
6968 Fraise_frame (frame);
6970 else
6971 clear_message (1, 1);
6973 do_pending_window_change (0);
6974 echo_area_display (1);
6975 do_pending_window_change (0);
6976 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6977 (*frame_up_to_date_hook) (f);
6982 /* Display a null-terminated echo area message M. If M is 0, clear
6983 out any existing message, and let the mini-buffer text show through.
6985 The buffer M must continue to exist until after the echo area gets
6986 cleared or some other message gets displayed there. Do not pass
6987 text that is stored in a Lisp string. Do not pass text in a buffer
6988 that was alloca'd. */
6990 void
6991 message1 (m)
6992 char *m;
6994 message2 (m, (m ? strlen (m) : 0), 0);
6998 /* The non-logging counterpart of message1. */
7000 void
7001 message1_nolog (m)
7002 char *m;
7004 message2_nolog (m, (m ? strlen (m) : 0), 0);
7007 /* Display a message M which contains a single %s
7008 which gets replaced with STRING. */
7010 void
7011 message_with_string (m, string, log)
7012 char *m;
7013 Lisp_Object string;
7014 int log;
7016 CHECK_STRING (string);
7018 if (noninteractive)
7020 if (m)
7022 if (noninteractive_need_newline)
7023 putc ('\n', stderr);
7024 noninteractive_need_newline = 0;
7025 fprintf (stderr, m, SDATA (string));
7026 if (cursor_in_echo_area == 0)
7027 fprintf (stderr, "\n");
7028 fflush (stderr);
7031 else if (INTERACTIVE)
7033 /* The frame whose minibuffer we're going to display the message on.
7034 It may be larger than the selected frame, so we need
7035 to use its buffer, not the selected frame's buffer. */
7036 Lisp_Object mini_window;
7037 struct frame *f, *sf = SELECTED_FRAME ();
7039 /* Get the frame containing the minibuffer
7040 that the selected frame is using. */
7041 mini_window = FRAME_MINIBUF_WINDOW (sf);
7042 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7044 /* A null message buffer means that the frame hasn't really been
7045 initialized yet. Error messages get reported properly by
7046 cmd_error, so this must be just an informative message; toss it. */
7047 if (FRAME_MESSAGE_BUF (f))
7049 Lisp_Object args[2], message;
7050 struct gcpro gcpro1, gcpro2;
7052 args[0] = build_string (m);
7053 args[1] = message = string;
7054 GCPRO2 (args[0], message);
7055 gcpro1.nvars = 2;
7057 message = Fformat (2, args);
7059 if (log)
7060 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7061 else
7062 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7064 UNGCPRO;
7066 /* Print should start at the beginning of the message
7067 buffer next time. */
7068 message_buf_print = 0;
7074 /* Dump an informative message to the minibuf. If M is 0, clear out
7075 any existing message, and let the mini-buffer text show through. */
7077 /* VARARGS 1 */
7078 void
7079 message (m, a1, a2, a3)
7080 char *m;
7081 EMACS_INT a1, a2, a3;
7083 if (noninteractive)
7085 if (m)
7087 if (noninteractive_need_newline)
7088 putc ('\n', stderr);
7089 noninteractive_need_newline = 0;
7090 fprintf (stderr, m, a1, a2, a3);
7091 if (cursor_in_echo_area == 0)
7092 fprintf (stderr, "\n");
7093 fflush (stderr);
7096 else if (INTERACTIVE)
7098 /* The frame whose mini-buffer we're going to display the message
7099 on. It may be larger than the selected frame, so we need to
7100 use its buffer, not the selected frame's buffer. */
7101 Lisp_Object mini_window;
7102 struct frame *f, *sf = SELECTED_FRAME ();
7104 /* Get the frame containing the mini-buffer
7105 that the selected frame is using. */
7106 mini_window = FRAME_MINIBUF_WINDOW (sf);
7107 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7109 /* A null message buffer means that the frame hasn't really been
7110 initialized yet. Error messages get reported properly by
7111 cmd_error, so this must be just an informative message; toss
7112 it. */
7113 if (FRAME_MESSAGE_BUF (f))
7115 if (m)
7117 int len;
7118 #ifdef NO_ARG_ARRAY
7119 char *a[3];
7120 a[0] = (char *) a1;
7121 a[1] = (char *) a2;
7122 a[2] = (char *) a3;
7124 len = doprnt (FRAME_MESSAGE_BUF (f),
7125 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7126 #else
7127 len = doprnt (FRAME_MESSAGE_BUF (f),
7128 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7129 (char **) &a1);
7130 #endif /* NO_ARG_ARRAY */
7132 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7134 else
7135 message1 (0);
7137 /* Print should start at the beginning of the message
7138 buffer next time. */
7139 message_buf_print = 0;
7145 /* The non-logging version of message. */
7147 void
7148 message_nolog (m, a1, a2, a3)
7149 char *m;
7150 EMACS_INT a1, a2, a3;
7152 Lisp_Object old_log_max;
7153 old_log_max = Vmessage_log_max;
7154 Vmessage_log_max = Qnil;
7155 message (m, a1, a2, a3);
7156 Vmessage_log_max = old_log_max;
7160 /* Display the current message in the current mini-buffer. This is
7161 only called from error handlers in process.c, and is not time
7162 critical. */
7164 void
7165 update_echo_area ()
7167 if (!NILP (echo_area_buffer[0]))
7169 Lisp_Object string;
7170 string = Fcurrent_message ();
7171 message3 (string, SBYTES (string),
7172 !NILP (current_buffer->enable_multibyte_characters));
7177 /* Make sure echo area buffers in `echo_buffers' are live.
7178 If they aren't, make new ones. */
7180 static void
7181 ensure_echo_area_buffers ()
7183 int i;
7185 for (i = 0; i < 2; ++i)
7186 if (!BUFFERP (echo_buffer[i])
7187 || NILP (XBUFFER (echo_buffer[i])->name))
7189 char name[30];
7190 Lisp_Object old_buffer;
7191 int j;
7193 old_buffer = echo_buffer[i];
7194 sprintf (name, " *Echo Area %d*", i);
7195 echo_buffer[i] = Fget_buffer_create (build_string (name));
7196 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7198 for (j = 0; j < 2; ++j)
7199 if (EQ (old_buffer, echo_area_buffer[j]))
7200 echo_area_buffer[j] = echo_buffer[i];
7205 /* Call FN with args A1..A4 with either the current or last displayed
7206 echo_area_buffer as current buffer.
7208 WHICH zero means use the current message buffer
7209 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7210 from echo_buffer[] and clear it.
7212 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7213 suitable buffer from echo_buffer[] and clear it.
7215 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7216 that the current message becomes the last displayed one, make
7217 choose a suitable buffer for echo_area_buffer[0], and clear it.
7219 Value is what FN returns. */
7221 static int
7222 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7223 struct window *w;
7224 int which;
7225 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7226 EMACS_INT a1;
7227 Lisp_Object a2;
7228 EMACS_INT a3, a4;
7230 Lisp_Object buffer;
7231 int this_one, the_other, clear_buffer_p, rc;
7232 int count = SPECPDL_INDEX ();
7234 /* If buffers aren't live, make new ones. */
7235 ensure_echo_area_buffers ();
7237 clear_buffer_p = 0;
7239 if (which == 0)
7240 this_one = 0, the_other = 1;
7241 else if (which > 0)
7242 this_one = 1, the_other = 0;
7243 else
7245 this_one = 0, the_other = 1;
7246 clear_buffer_p = 1;
7248 /* We need a fresh one in case the current echo buffer equals
7249 the one containing the last displayed echo area message. */
7250 if (!NILP (echo_area_buffer[this_one])
7251 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7252 echo_area_buffer[this_one] = Qnil;
7255 /* Choose a suitable buffer from echo_buffer[] is we don't
7256 have one. */
7257 if (NILP (echo_area_buffer[this_one]))
7259 echo_area_buffer[this_one]
7260 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7261 ? echo_buffer[the_other]
7262 : echo_buffer[this_one]);
7263 clear_buffer_p = 1;
7266 buffer = echo_area_buffer[this_one];
7268 /* Don't get confused by reusing the buffer used for echoing
7269 for a different purpose. */
7270 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7271 cancel_echoing ();
7273 record_unwind_protect (unwind_with_echo_area_buffer,
7274 with_echo_area_buffer_unwind_data (w));
7276 /* Make the echo area buffer current. Note that for display
7277 purposes, it is not necessary that the displayed window's buffer
7278 == current_buffer, except for text property lookup. So, let's
7279 only set that buffer temporarily here without doing a full
7280 Fset_window_buffer. We must also change w->pointm, though,
7281 because otherwise an assertions in unshow_buffer fails, and Emacs
7282 aborts. */
7283 set_buffer_internal_1 (XBUFFER (buffer));
7284 if (w)
7286 w->buffer = buffer;
7287 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7290 current_buffer->undo_list = Qt;
7291 current_buffer->read_only = Qnil;
7292 specbind (Qinhibit_read_only, Qt);
7293 specbind (Qinhibit_modification_hooks, Qt);
7295 if (clear_buffer_p && Z > BEG)
7296 del_range (BEG, Z);
7298 xassert (BEGV >= BEG);
7299 xassert (ZV <= Z && ZV >= BEGV);
7301 rc = fn (a1, a2, a3, a4);
7303 xassert (BEGV >= BEG);
7304 xassert (ZV <= Z && ZV >= BEGV);
7306 unbind_to (count, Qnil);
7307 return rc;
7311 /* Save state that should be preserved around the call to the function
7312 FN called in with_echo_area_buffer. */
7314 static Lisp_Object
7315 with_echo_area_buffer_unwind_data (w)
7316 struct window *w;
7318 int i = 0;
7319 Lisp_Object vector;
7321 /* Reduce consing by keeping one vector in
7322 Vwith_echo_area_save_vector. */
7323 vector = Vwith_echo_area_save_vector;
7324 Vwith_echo_area_save_vector = Qnil;
7326 if (NILP (vector))
7327 vector = Fmake_vector (make_number (7), Qnil);
7329 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7330 AREF (vector, i) = Vdeactivate_mark, ++i;
7331 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7333 if (w)
7335 XSETWINDOW (AREF (vector, i), w); ++i;
7336 AREF (vector, i) = w->buffer; ++i;
7337 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7338 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7340 else
7342 int end = i + 4;
7343 for (; i < end; ++i)
7344 AREF (vector, i) = Qnil;
7347 xassert (i == ASIZE (vector));
7348 return vector;
7352 /* Restore global state from VECTOR which was created by
7353 with_echo_area_buffer_unwind_data. */
7355 static Lisp_Object
7356 unwind_with_echo_area_buffer (vector)
7357 Lisp_Object vector;
7359 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7360 Vdeactivate_mark = AREF (vector, 1);
7361 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7363 if (WINDOWP (AREF (vector, 3)))
7365 struct window *w;
7366 Lisp_Object buffer, charpos, bytepos;
7368 w = XWINDOW (AREF (vector, 3));
7369 buffer = AREF (vector, 4);
7370 charpos = AREF (vector, 5);
7371 bytepos = AREF (vector, 6);
7373 w->buffer = buffer;
7374 set_marker_both (w->pointm, buffer,
7375 XFASTINT (charpos), XFASTINT (bytepos));
7378 Vwith_echo_area_save_vector = vector;
7379 return Qnil;
7383 /* Set up the echo area for use by print functions. MULTIBYTE_P
7384 non-zero means we will print multibyte. */
7386 void
7387 setup_echo_area_for_printing (multibyte_p)
7388 int multibyte_p;
7390 /* If we can't find an echo area any more, exit. */
7391 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7392 Fkill_emacs (Qnil);
7394 ensure_echo_area_buffers ();
7396 if (!message_buf_print)
7398 /* A message has been output since the last time we printed.
7399 Choose a fresh echo area buffer. */
7400 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7401 echo_area_buffer[0] = echo_buffer[1];
7402 else
7403 echo_area_buffer[0] = echo_buffer[0];
7405 /* Switch to that buffer and clear it. */
7406 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7407 current_buffer->truncate_lines = Qnil;
7409 if (Z > BEG)
7411 int count = SPECPDL_INDEX ();
7412 specbind (Qinhibit_read_only, Qt);
7413 /* Note that undo recording is always disabled. */
7414 del_range (BEG, Z);
7415 unbind_to (count, Qnil);
7417 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7419 /* Set up the buffer for the multibyteness we need. */
7420 if (multibyte_p
7421 != !NILP (current_buffer->enable_multibyte_characters))
7422 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7424 /* Raise the frame containing the echo area. */
7425 if (minibuffer_auto_raise)
7427 struct frame *sf = SELECTED_FRAME ();
7428 Lisp_Object mini_window;
7429 mini_window = FRAME_MINIBUF_WINDOW (sf);
7430 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7433 message_log_maybe_newline ();
7434 message_buf_print = 1;
7436 else
7438 if (NILP (echo_area_buffer[0]))
7440 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7441 echo_area_buffer[0] = echo_buffer[1];
7442 else
7443 echo_area_buffer[0] = echo_buffer[0];
7446 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7448 /* Someone switched buffers between print requests. */
7449 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7450 current_buffer->truncate_lines = Qnil;
7456 /* Display an echo area message in window W. Value is non-zero if W's
7457 height is changed. If display_last_displayed_message_p is
7458 non-zero, display the message that was last displayed, otherwise
7459 display the current message. */
7461 static int
7462 display_echo_area (w)
7463 struct window *w;
7465 int i, no_message_p, window_height_changed_p, count;
7467 /* Temporarily disable garbage collections while displaying the echo
7468 area. This is done because a GC can print a message itself.
7469 That message would modify the echo area buffer's contents while a
7470 redisplay of the buffer is going on, and seriously confuse
7471 redisplay. */
7472 count = inhibit_garbage_collection ();
7474 /* If there is no message, we must call display_echo_area_1
7475 nevertheless because it resizes the window. But we will have to
7476 reset the echo_area_buffer in question to nil at the end because
7477 with_echo_area_buffer will sets it to an empty buffer. */
7478 i = display_last_displayed_message_p ? 1 : 0;
7479 no_message_p = NILP (echo_area_buffer[i]);
7481 window_height_changed_p
7482 = with_echo_area_buffer (w, display_last_displayed_message_p,
7483 display_echo_area_1,
7484 (EMACS_INT) w, Qnil, 0, 0);
7486 if (no_message_p)
7487 echo_area_buffer[i] = Qnil;
7489 unbind_to (count, Qnil);
7490 return window_height_changed_p;
7494 /* Helper for display_echo_area. Display the current buffer which
7495 contains the current echo area message in window W, a mini-window,
7496 a pointer to which is passed in A1. A2..A4 are currently not used.
7497 Change the height of W so that all of the message is displayed.
7498 Value is non-zero if height of W was changed. */
7500 static int
7501 display_echo_area_1 (a1, a2, a3, a4)
7502 EMACS_INT a1;
7503 Lisp_Object a2;
7504 EMACS_INT a3, a4;
7506 struct window *w = (struct window *) a1;
7507 Lisp_Object window;
7508 struct text_pos start;
7509 int window_height_changed_p = 0;
7511 /* Do this before displaying, so that we have a large enough glyph
7512 matrix for the display. */
7513 window_height_changed_p = resize_mini_window (w, 0);
7515 /* Display. */
7516 clear_glyph_matrix (w->desired_matrix);
7517 XSETWINDOW (window, w);
7518 SET_TEXT_POS (start, BEG, BEG_BYTE);
7519 try_window (window, start);
7521 return window_height_changed_p;
7525 /* Resize the echo area window to exactly the size needed for the
7526 currently displayed message, if there is one. If a mini-buffer
7527 is active, don't shrink it. */
7529 void
7530 resize_echo_area_exactly ()
7532 if (BUFFERP (echo_area_buffer[0])
7533 && WINDOWP (echo_area_window))
7535 struct window *w = XWINDOW (echo_area_window);
7536 int resized_p;
7537 Lisp_Object resize_exactly;
7539 if (minibuf_level == 0)
7540 resize_exactly = Qt;
7541 else
7542 resize_exactly = Qnil;
7544 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7545 (EMACS_INT) w, resize_exactly, 0, 0);
7546 if (resized_p)
7548 ++windows_or_buffers_changed;
7549 ++update_mode_lines;
7550 redisplay_internal (0);
7556 /* Callback function for with_echo_area_buffer, when used from
7557 resize_echo_area_exactly. A1 contains a pointer to the window to
7558 resize, EXACTLY non-nil means resize the mini-window exactly to the
7559 size of the text displayed. A3 and A4 are not used. Value is what
7560 resize_mini_window returns. */
7562 static int
7563 resize_mini_window_1 (a1, exactly, a3, a4)
7564 EMACS_INT a1;
7565 Lisp_Object exactly;
7566 EMACS_INT a3, a4;
7568 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7572 /* Resize mini-window W to fit the size of its contents. EXACT:P
7573 means size the window exactly to the size needed. Otherwise, it's
7574 only enlarged until W's buffer is empty. Value is non-zero if
7575 the window height has been changed. */
7578 resize_mini_window (w, exact_p)
7579 struct window *w;
7580 int exact_p;
7582 struct frame *f = XFRAME (w->frame);
7583 int window_height_changed_p = 0;
7585 xassert (MINI_WINDOW_P (w));
7587 /* Don't resize windows while redisplaying a window; it would
7588 confuse redisplay functions when the size of the window they are
7589 displaying changes from under them. Such a resizing can happen,
7590 for instance, when which-func prints a long message while
7591 we are running fontification-functions. We're running these
7592 functions with safe_call which binds inhibit-redisplay to t. */
7593 if (!NILP (Vinhibit_redisplay))
7594 return 0;
7596 /* Nil means don't try to resize. */
7597 if (NILP (Vresize_mini_windows)
7598 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7599 return 0;
7601 if (!FRAME_MINIBUF_ONLY_P (f))
7603 struct it it;
7604 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7605 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7606 int height, max_height;
7607 int unit = FRAME_LINE_HEIGHT (f);
7608 struct text_pos start;
7609 struct buffer *old_current_buffer = NULL;
7611 if (current_buffer != XBUFFER (w->buffer))
7613 old_current_buffer = current_buffer;
7614 set_buffer_internal (XBUFFER (w->buffer));
7617 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7619 /* Compute the max. number of lines specified by the user. */
7620 if (FLOATP (Vmax_mini_window_height))
7621 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7622 else if (INTEGERP (Vmax_mini_window_height))
7623 max_height = XINT (Vmax_mini_window_height);
7624 else
7625 max_height = total_height / 4;
7627 /* Correct that max. height if it's bogus. */
7628 max_height = max (1, max_height);
7629 max_height = min (total_height, max_height);
7631 /* Find out the height of the text in the window. */
7632 if (it.truncate_lines_p)
7633 height = 1;
7634 else
7636 last_height = 0;
7637 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7638 if (it.max_ascent == 0 && it.max_descent == 0)
7639 height = it.current_y + last_height;
7640 else
7641 height = it.current_y + it.max_ascent + it.max_descent;
7642 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7643 height = (height + unit - 1) / unit;
7646 /* Compute a suitable window start. */
7647 if (height > max_height)
7649 height = max_height;
7650 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7651 move_it_vertically_backward (&it, (height - 1) * unit);
7652 start = it.current.pos;
7654 else
7655 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7656 SET_MARKER_FROM_TEXT_POS (w->start, start);
7658 if (EQ (Vresize_mini_windows, Qgrow_only))
7660 /* Let it grow only, until we display an empty message, in which
7661 case the window shrinks again. */
7662 if (height > WINDOW_TOTAL_LINES (w))
7664 int old_height = WINDOW_TOTAL_LINES (w);
7665 freeze_window_starts (f, 1);
7666 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7667 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7669 else if (height < WINDOW_TOTAL_LINES (w)
7670 && (exact_p || BEGV == ZV))
7672 int old_height = WINDOW_TOTAL_LINES (w);
7673 freeze_window_starts (f, 0);
7674 shrink_mini_window (w);
7675 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7678 else
7680 /* Always resize to exact size needed. */
7681 if (height > WINDOW_TOTAL_LINES (w))
7683 int old_height = WINDOW_TOTAL_LINES (w);
7684 freeze_window_starts (f, 1);
7685 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7686 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7688 else if (height < WINDOW_TOTAL_LINES (w))
7690 int old_height = WINDOW_TOTAL_LINES (w);
7691 freeze_window_starts (f, 0);
7692 shrink_mini_window (w);
7694 if (height)
7696 freeze_window_starts (f, 1);
7697 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7700 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7704 if (old_current_buffer)
7705 set_buffer_internal (old_current_buffer);
7708 return window_height_changed_p;
7712 /* Value is the current message, a string, or nil if there is no
7713 current message. */
7715 Lisp_Object
7716 current_message ()
7718 Lisp_Object msg;
7720 if (NILP (echo_area_buffer[0]))
7721 msg = Qnil;
7722 else
7724 with_echo_area_buffer (0, 0, current_message_1,
7725 (EMACS_INT) &msg, Qnil, 0, 0);
7726 if (NILP (msg))
7727 echo_area_buffer[0] = Qnil;
7730 return msg;
7734 static int
7735 current_message_1 (a1, a2, a3, a4)
7736 EMACS_INT a1;
7737 Lisp_Object a2;
7738 EMACS_INT a3, a4;
7740 Lisp_Object *msg = (Lisp_Object *) a1;
7742 if (Z > BEG)
7743 *msg = make_buffer_string (BEG, Z, 1);
7744 else
7745 *msg = Qnil;
7746 return 0;
7750 /* Push the current message on Vmessage_stack for later restauration
7751 by restore_message. Value is non-zero if the current message isn't
7752 empty. This is a relatively infrequent operation, so it's not
7753 worth optimizing. */
7756 push_message ()
7758 Lisp_Object msg;
7759 msg = current_message ();
7760 Vmessage_stack = Fcons (msg, Vmessage_stack);
7761 return STRINGP (msg);
7765 /* Restore message display from the top of Vmessage_stack. */
7767 void
7768 restore_message ()
7770 Lisp_Object msg;
7772 xassert (CONSP (Vmessage_stack));
7773 msg = XCAR (Vmessage_stack);
7774 if (STRINGP (msg))
7775 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7776 else
7777 message3_nolog (msg, 0, 0);
7781 /* Handler for record_unwind_protect calling pop_message. */
7783 Lisp_Object
7784 pop_message_unwind (dummy)
7785 Lisp_Object dummy;
7787 pop_message ();
7788 return Qnil;
7791 /* Pop the top-most entry off Vmessage_stack. */
7793 void
7794 pop_message ()
7796 xassert (CONSP (Vmessage_stack));
7797 Vmessage_stack = XCDR (Vmessage_stack);
7801 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7802 exits. If the stack is not empty, we have a missing pop_message
7803 somewhere. */
7805 void
7806 check_message_stack ()
7808 if (!NILP (Vmessage_stack))
7809 abort ();
7813 /* Truncate to NCHARS what will be displayed in the echo area the next
7814 time we display it---but don't redisplay it now. */
7816 void
7817 truncate_echo_area (nchars)
7818 int nchars;
7820 if (nchars == 0)
7821 echo_area_buffer[0] = Qnil;
7822 /* A null message buffer means that the frame hasn't really been
7823 initialized yet. Error messages get reported properly by
7824 cmd_error, so this must be just an informative message; toss it. */
7825 else if (!noninteractive
7826 && INTERACTIVE
7827 && !NILP (echo_area_buffer[0]))
7829 struct frame *sf = SELECTED_FRAME ();
7830 if (FRAME_MESSAGE_BUF (sf))
7831 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7836 /* Helper function for truncate_echo_area. Truncate the current
7837 message to at most NCHARS characters. */
7839 static int
7840 truncate_message_1 (nchars, a2, a3, a4)
7841 EMACS_INT nchars;
7842 Lisp_Object a2;
7843 EMACS_INT a3, a4;
7845 if (BEG + nchars < Z)
7846 del_range (BEG + nchars, Z);
7847 if (Z == BEG)
7848 echo_area_buffer[0] = Qnil;
7849 return 0;
7853 /* Set the current message to a substring of S or STRING.
7855 If STRING is a Lisp string, set the message to the first NBYTES
7856 bytes from STRING. NBYTES zero means use the whole string. If
7857 STRING is multibyte, the message will be displayed multibyte.
7859 If S is not null, set the message to the first LEN bytes of S. LEN
7860 zero means use the whole string. MULTIBYTE_P non-zero means S is
7861 multibyte. Display the message multibyte in that case. */
7863 void
7864 set_message (s, string, nbytes, multibyte_p)
7865 const char *s;
7866 Lisp_Object string;
7867 int nbytes, multibyte_p;
7869 message_enable_multibyte
7870 = ((s && multibyte_p)
7871 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7873 with_echo_area_buffer (0, -1, set_message_1,
7874 (EMACS_INT) s, string, nbytes, multibyte_p);
7875 message_buf_print = 0;
7876 help_echo_showing_p = 0;
7880 /* Helper function for set_message. Arguments have the same meaning
7881 as there, with A1 corresponding to S and A2 corresponding to STRING
7882 This function is called with the echo area buffer being
7883 current. */
7885 static int
7886 set_message_1 (a1, a2, nbytes, multibyte_p)
7887 EMACS_INT a1;
7888 Lisp_Object a2;
7889 EMACS_INT nbytes, multibyte_p;
7891 const char *s = (const char *) a1;
7892 Lisp_Object string = a2;
7894 xassert (BEG == Z);
7896 /* Change multibyteness of the echo buffer appropriately. */
7897 if (message_enable_multibyte
7898 != !NILP (current_buffer->enable_multibyte_characters))
7899 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7901 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7903 /* Insert new message at BEG. */
7904 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7906 if (STRINGP (string))
7908 int nchars;
7910 if (nbytes == 0)
7911 nbytes = SBYTES (string);
7912 nchars = string_byte_to_char (string, nbytes);
7914 /* This function takes care of single/multibyte conversion. We
7915 just have to ensure that the echo area buffer has the right
7916 setting of enable_multibyte_characters. */
7917 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7919 else if (s)
7921 if (nbytes == 0)
7922 nbytes = strlen (s);
7924 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7926 /* Convert from multi-byte to single-byte. */
7927 int i, c, n;
7928 unsigned char work[1];
7930 /* Convert a multibyte string to single-byte. */
7931 for (i = 0; i < nbytes; i += n)
7933 c = string_char_and_length (s + i, nbytes - i, &n);
7934 work[0] = (SINGLE_BYTE_CHAR_P (c)
7936 : multibyte_char_to_unibyte (c, Qnil));
7937 insert_1_both (work, 1, 1, 1, 0, 0);
7940 else if (!multibyte_p
7941 && !NILP (current_buffer->enable_multibyte_characters))
7943 /* Convert from single-byte to multi-byte. */
7944 int i, c, n;
7945 const unsigned char *msg = (const unsigned char *) s;
7946 unsigned char str[MAX_MULTIBYTE_LENGTH];
7948 /* Convert a single-byte string to multibyte. */
7949 for (i = 0; i < nbytes; i++)
7951 c = unibyte_char_to_multibyte (msg[i]);
7952 n = CHAR_STRING (c, str);
7953 insert_1_both (str, 1, n, 1, 0, 0);
7956 else
7957 insert_1 (s, nbytes, 1, 0, 0);
7960 return 0;
7964 /* Clear messages. CURRENT_P non-zero means clear the current
7965 message. LAST_DISPLAYED_P non-zero means clear the message
7966 last displayed. */
7968 void
7969 clear_message (current_p, last_displayed_p)
7970 int current_p, last_displayed_p;
7972 if (current_p)
7974 echo_area_buffer[0] = Qnil;
7975 message_cleared_p = 1;
7978 if (last_displayed_p)
7979 echo_area_buffer[1] = Qnil;
7981 message_buf_print = 0;
7984 /* Clear garbaged frames.
7986 This function is used where the old redisplay called
7987 redraw_garbaged_frames which in turn called redraw_frame which in
7988 turn called clear_frame. The call to clear_frame was a source of
7989 flickering. I believe a clear_frame is not necessary. It should
7990 suffice in the new redisplay to invalidate all current matrices,
7991 and ensure a complete redisplay of all windows. */
7993 static void
7994 clear_garbaged_frames ()
7996 if (frame_garbaged)
7998 Lisp_Object tail, frame;
7999 int changed_count = 0;
8001 FOR_EACH_FRAME (tail, frame)
8003 struct frame *f = XFRAME (frame);
8005 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8007 if (f->resized_p)
8009 Fredraw_frame (frame);
8010 f->force_flush_display_p = 1;
8012 clear_current_matrices (f);
8013 changed_count++;
8014 f->garbaged = 0;
8015 f->resized_p = 0;
8019 frame_garbaged = 0;
8020 if (changed_count)
8021 ++windows_or_buffers_changed;
8026 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8027 is non-zero update selected_frame. Value is non-zero if the
8028 mini-windows height has been changed. */
8030 static int
8031 echo_area_display (update_frame_p)
8032 int update_frame_p;
8034 Lisp_Object mini_window;
8035 struct window *w;
8036 struct frame *f;
8037 int window_height_changed_p = 0;
8038 struct frame *sf = SELECTED_FRAME ();
8040 mini_window = FRAME_MINIBUF_WINDOW (sf);
8041 w = XWINDOW (mini_window);
8042 f = XFRAME (WINDOW_FRAME (w));
8044 /* Don't display if frame is invisible or not yet initialized. */
8045 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8046 return 0;
8048 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8049 #ifndef MAC_OS8
8050 #ifdef HAVE_WINDOW_SYSTEM
8051 /* When Emacs starts, selected_frame may be a visible terminal
8052 frame, even if we run under a window system. If we let this
8053 through, a message would be displayed on the terminal. */
8054 if (EQ (selected_frame, Vterminal_frame)
8055 && !NILP (Vwindow_system))
8056 return 0;
8057 #endif /* HAVE_WINDOW_SYSTEM */
8058 #endif
8060 /* Redraw garbaged frames. */
8061 if (frame_garbaged)
8062 clear_garbaged_frames ();
8064 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8066 echo_area_window = mini_window;
8067 window_height_changed_p = display_echo_area (w);
8068 w->must_be_updated_p = 1;
8070 /* Update the display, unless called from redisplay_internal.
8071 Also don't update the screen during redisplay itself. The
8072 update will happen at the end of redisplay, and an update
8073 here could cause confusion. */
8074 if (update_frame_p && !redisplaying_p)
8076 int n = 0;
8078 /* If the display update has been interrupted by pending
8079 input, update mode lines in the frame. Due to the
8080 pending input, it might have been that redisplay hasn't
8081 been called, so that mode lines above the echo area are
8082 garbaged. This looks odd, so we prevent it here. */
8083 if (!display_completed)
8084 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8086 if (window_height_changed_p
8087 /* Don't do this if Emacs is shutting down. Redisplay
8088 needs to run hooks. */
8089 && !NILP (Vrun_hooks))
8091 /* Must update other windows. Likewise as in other
8092 cases, don't let this update be interrupted by
8093 pending input. */
8094 int count = SPECPDL_INDEX ();
8095 specbind (Qredisplay_dont_pause, Qt);
8096 windows_or_buffers_changed = 1;
8097 redisplay_internal (0);
8098 unbind_to (count, Qnil);
8100 else if (FRAME_WINDOW_P (f) && n == 0)
8102 /* Window configuration is the same as before.
8103 Can do with a display update of the echo area,
8104 unless we displayed some mode lines. */
8105 update_single_window (w, 1);
8106 rif->flush_display (f);
8108 else
8109 update_frame (f, 1, 1);
8111 /* If cursor is in the echo area, make sure that the next
8112 redisplay displays the minibuffer, so that the cursor will
8113 be replaced with what the minibuffer wants. */
8114 if (cursor_in_echo_area)
8115 ++windows_or_buffers_changed;
8118 else if (!EQ (mini_window, selected_window))
8119 windows_or_buffers_changed++;
8121 /* Last displayed message is now the current message. */
8122 echo_area_buffer[1] = echo_area_buffer[0];
8123 /* Inform read_char that we're not echoing. */
8124 echo_message_buffer = Qnil;
8126 /* Prevent redisplay optimization in redisplay_internal by resetting
8127 this_line_start_pos. This is done because the mini-buffer now
8128 displays the message instead of its buffer text. */
8129 if (EQ (mini_window, selected_window))
8130 CHARPOS (this_line_start_pos) = 0;
8132 return window_height_changed_p;
8137 /***********************************************************************
8138 Frame Titles
8139 ***********************************************************************/
8142 /* The frame title buffering code is also used by Fformat_mode_line.
8143 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8145 /* A buffer for constructing frame titles in it; allocated from the
8146 heap in init_xdisp and resized as needed in store_frame_title_char. */
8148 static char *frame_title_buf;
8150 /* The buffer's end, and a current output position in it. */
8152 static char *frame_title_buf_end;
8153 static char *frame_title_ptr;
8156 /* Store a single character C for the frame title in frame_title_buf.
8157 Re-allocate frame_title_buf if necessary. */
8159 static void
8160 #ifdef PROTOTYPES
8161 store_frame_title_char (char c)
8162 #else
8163 store_frame_title_char (c)
8164 char c;
8165 #endif
8167 /* If output position has reached the end of the allocated buffer,
8168 double the buffer's size. */
8169 if (frame_title_ptr == frame_title_buf_end)
8171 int len = frame_title_ptr - frame_title_buf;
8172 int new_size = 2 * len * sizeof *frame_title_buf;
8173 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8174 frame_title_buf_end = frame_title_buf + new_size;
8175 frame_title_ptr = frame_title_buf + len;
8178 *frame_title_ptr++ = c;
8182 /* Store part of a frame title in frame_title_buf, beginning at
8183 frame_title_ptr. STR is the string to store. Do not copy
8184 characters that yield more columns than PRECISION; PRECISION <= 0
8185 means copy the whole string. Pad with spaces until FIELD_WIDTH
8186 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8187 pad. Called from display_mode_element when it is used to build a
8188 frame title. */
8190 static int
8191 store_frame_title (str, field_width, precision)
8192 const unsigned char *str;
8193 int field_width, precision;
8195 int n = 0;
8196 int dummy, nbytes;
8198 /* Copy at most PRECISION chars from STR. */
8199 nbytes = strlen (str);
8200 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8201 while (nbytes--)
8202 store_frame_title_char (*str++);
8204 /* Fill up with spaces until FIELD_WIDTH reached. */
8205 while (field_width > 0
8206 && n < field_width)
8208 store_frame_title_char (' ');
8209 ++n;
8212 return n;
8215 #ifdef HAVE_WINDOW_SYSTEM
8217 /* Set the title of FRAME, if it has changed. The title format is
8218 Vicon_title_format if FRAME is iconified, otherwise it is
8219 frame_title_format. */
8221 static void
8222 x_consider_frame_title (frame)
8223 Lisp_Object frame;
8225 struct frame *f = XFRAME (frame);
8227 if (FRAME_WINDOW_P (f)
8228 || FRAME_MINIBUF_ONLY_P (f)
8229 || f->explicit_name)
8231 /* Do we have more than one visible frame on this X display? */
8232 Lisp_Object tail;
8233 Lisp_Object fmt;
8234 struct buffer *obuf;
8235 int len;
8236 struct it it;
8238 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8240 Lisp_Object other_frame = XCAR (tail);
8241 struct frame *tf = XFRAME (other_frame);
8243 if (tf != f
8244 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8245 && !FRAME_MINIBUF_ONLY_P (tf)
8246 && !EQ (other_frame, tip_frame)
8247 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8248 break;
8251 /* Set global variable indicating that multiple frames exist. */
8252 multiple_frames = CONSP (tail);
8254 /* Switch to the buffer of selected window of the frame. Set up
8255 frame_title_ptr so that display_mode_element will output into it;
8256 then display the title. */
8257 obuf = current_buffer;
8258 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8259 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8260 frame_title_ptr = frame_title_buf;
8261 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8262 NULL, DEFAULT_FACE_ID);
8263 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8264 len = frame_title_ptr - frame_title_buf;
8265 frame_title_ptr = NULL;
8266 set_buffer_internal_1 (obuf);
8268 /* Set the title only if it's changed. This avoids consing in
8269 the common case where it hasn't. (If it turns out that we've
8270 already wasted too much time by walking through the list with
8271 display_mode_element, then we might need to optimize at a
8272 higher level than this.) */
8273 if (! STRINGP (f->name)
8274 || SBYTES (f->name) != len
8275 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8276 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8280 #endif /* not HAVE_WINDOW_SYSTEM */
8285 /***********************************************************************
8286 Menu Bars
8287 ***********************************************************************/
8290 /* Prepare for redisplay by updating menu-bar item lists when
8291 appropriate. This can call eval. */
8293 void
8294 prepare_menu_bars ()
8296 int all_windows;
8297 struct gcpro gcpro1, gcpro2;
8298 struct frame *f;
8299 Lisp_Object tooltip_frame;
8301 #ifdef HAVE_WINDOW_SYSTEM
8302 tooltip_frame = tip_frame;
8303 #else
8304 tooltip_frame = Qnil;
8305 #endif
8307 /* Update all frame titles based on their buffer names, etc. We do
8308 this before the menu bars so that the buffer-menu will show the
8309 up-to-date frame titles. */
8310 #ifdef HAVE_WINDOW_SYSTEM
8311 if (windows_or_buffers_changed || update_mode_lines)
8313 Lisp_Object tail, frame;
8315 FOR_EACH_FRAME (tail, frame)
8317 f = XFRAME (frame);
8318 if (!EQ (frame, tooltip_frame)
8319 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8320 x_consider_frame_title (frame);
8323 #endif /* HAVE_WINDOW_SYSTEM */
8325 /* Update the menu bar item lists, if appropriate. This has to be
8326 done before any actual redisplay or generation of display lines. */
8327 all_windows = (update_mode_lines
8328 || buffer_shared > 1
8329 || windows_or_buffers_changed);
8330 if (all_windows)
8332 Lisp_Object tail, frame;
8333 int count = SPECPDL_INDEX ();
8335 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8337 FOR_EACH_FRAME (tail, frame)
8339 f = XFRAME (frame);
8341 /* Ignore tooltip frame. */
8342 if (EQ (frame, tooltip_frame))
8343 continue;
8345 /* If a window on this frame changed size, report that to
8346 the user and clear the size-change flag. */
8347 if (FRAME_WINDOW_SIZES_CHANGED (f))
8349 Lisp_Object functions;
8351 /* Clear flag first in case we get an error below. */
8352 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8353 functions = Vwindow_size_change_functions;
8354 GCPRO2 (tail, functions);
8356 while (CONSP (functions))
8358 call1 (XCAR (functions), frame);
8359 functions = XCDR (functions);
8361 UNGCPRO;
8364 GCPRO1 (tail);
8365 update_menu_bar (f, 0);
8366 #ifdef HAVE_WINDOW_SYSTEM
8367 update_tool_bar (f, 0);
8368 #endif
8369 UNGCPRO;
8372 unbind_to (count, Qnil);
8374 else
8376 struct frame *sf = SELECTED_FRAME ();
8377 update_menu_bar (sf, 1);
8378 #ifdef HAVE_WINDOW_SYSTEM
8379 update_tool_bar (sf, 1);
8380 #endif
8383 /* Motif needs this. See comment in xmenu.c. Turn it off when
8384 pending_menu_activation is not defined. */
8385 #ifdef USE_X_TOOLKIT
8386 pending_menu_activation = 0;
8387 #endif
8391 /* Update the menu bar item list for frame F. This has to be done
8392 before we start to fill in any display lines, because it can call
8393 eval.
8395 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8397 static void
8398 update_menu_bar (f, save_match_data)
8399 struct frame *f;
8400 int save_match_data;
8402 Lisp_Object window;
8403 register struct window *w;
8405 /* If called recursively during a menu update, do nothing. This can
8406 happen when, for instance, an activate-menubar-hook causes a
8407 redisplay. */
8408 if (inhibit_menubar_update)
8409 return;
8411 window = FRAME_SELECTED_WINDOW (f);
8412 w = XWINDOW (window);
8414 #if 0 /* The if statement below this if statement used to include the
8415 condition !NILP (w->update_mode_line), rather than using
8416 update_mode_lines directly, and this if statement may have
8417 been added to make that condition work. Now the if
8418 statement below matches its comment, this isn't needed. */
8419 if (update_mode_lines)
8420 w->update_mode_line = Qt;
8421 #endif
8423 if (FRAME_WINDOW_P (f)
8425 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8426 || defined (USE_GTK)
8427 FRAME_EXTERNAL_MENU_BAR (f)
8428 #else
8429 FRAME_MENU_BAR_LINES (f) > 0
8430 #endif
8431 : FRAME_MENU_BAR_LINES (f) > 0)
8433 /* If the user has switched buffers or windows, we need to
8434 recompute to reflect the new bindings. But we'll
8435 recompute when update_mode_lines is set too; that means
8436 that people can use force-mode-line-update to request
8437 that the menu bar be recomputed. The adverse effect on
8438 the rest of the redisplay algorithm is about the same as
8439 windows_or_buffers_changed anyway. */
8440 if (windows_or_buffers_changed
8441 /* This used to test w->update_mode_line, but we believe
8442 there is no need to recompute the menu in that case. */
8443 || update_mode_lines
8444 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8445 < BUF_MODIFF (XBUFFER (w->buffer)))
8446 != !NILP (w->last_had_star))
8447 || ((!NILP (Vtransient_mark_mode)
8448 && !NILP (XBUFFER (w->buffer)->mark_active))
8449 != !NILP (w->region_showing)))
8451 struct buffer *prev = current_buffer;
8452 int count = SPECPDL_INDEX ();
8454 specbind (Qinhibit_menubar_update, Qt);
8456 set_buffer_internal_1 (XBUFFER (w->buffer));
8457 if (save_match_data)
8458 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8459 if (NILP (Voverriding_local_map_menu_flag))
8461 specbind (Qoverriding_terminal_local_map, Qnil);
8462 specbind (Qoverriding_local_map, Qnil);
8465 /* Run the Lucid hook. */
8466 safe_run_hooks (Qactivate_menubar_hook);
8468 /* If it has changed current-menubar from previous value,
8469 really recompute the menu-bar from the value. */
8470 if (! NILP (Vlucid_menu_bar_dirty_flag))
8471 call0 (Qrecompute_lucid_menubar);
8473 safe_run_hooks (Qmenu_bar_update_hook);
8474 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8476 /* Redisplay the menu bar in case we changed it. */
8477 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8478 || defined (USE_GTK)
8479 if (FRAME_WINDOW_P (f)
8480 #if defined (MAC_OS)
8481 /* All frames on Mac OS share the same menubar. So only the
8482 selected frame should be allowed to set it. */
8483 && f == SELECTED_FRAME ()
8484 #endif
8486 set_frame_menubar (f, 0, 0);
8487 else
8488 /* On a terminal screen, the menu bar is an ordinary screen
8489 line, and this makes it get updated. */
8490 w->update_mode_line = Qt;
8491 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8492 /* In the non-toolkit version, the menu bar is an ordinary screen
8493 line, and this makes it get updated. */
8494 w->update_mode_line = Qt;
8495 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8497 unbind_to (count, Qnil);
8498 set_buffer_internal_1 (prev);
8505 /***********************************************************************
8506 Output Cursor
8507 ***********************************************************************/
8509 #ifdef HAVE_WINDOW_SYSTEM
8511 /* EXPORT:
8512 Nominal cursor position -- where to draw output.
8513 HPOS and VPOS are window relative glyph matrix coordinates.
8514 X and Y are window relative pixel coordinates. */
8516 struct cursor_pos output_cursor;
8519 /* EXPORT:
8520 Set the global variable output_cursor to CURSOR. All cursor
8521 positions are relative to updated_window. */
8523 void
8524 set_output_cursor (cursor)
8525 struct cursor_pos *cursor;
8527 output_cursor.hpos = cursor->hpos;
8528 output_cursor.vpos = cursor->vpos;
8529 output_cursor.x = cursor->x;
8530 output_cursor.y = cursor->y;
8534 /* EXPORT for RIF:
8535 Set a nominal cursor position.
8537 HPOS and VPOS are column/row positions in a window glyph matrix. X
8538 and Y are window text area relative pixel positions.
8540 If this is done during an update, updated_window will contain the
8541 window that is being updated and the position is the future output
8542 cursor position for that window. If updated_window is null, use
8543 selected_window and display the cursor at the given position. */
8545 void
8546 x_cursor_to (vpos, hpos, y, x)
8547 int vpos, hpos, y, x;
8549 struct window *w;
8551 /* If updated_window is not set, work on selected_window. */
8552 if (updated_window)
8553 w = updated_window;
8554 else
8555 w = XWINDOW (selected_window);
8557 /* Set the output cursor. */
8558 output_cursor.hpos = hpos;
8559 output_cursor.vpos = vpos;
8560 output_cursor.x = x;
8561 output_cursor.y = y;
8563 /* If not called as part of an update, really display the cursor.
8564 This will also set the cursor position of W. */
8565 if (updated_window == NULL)
8567 BLOCK_INPUT;
8568 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8569 if (rif->flush_display_optional)
8570 rif->flush_display_optional (SELECTED_FRAME ());
8571 UNBLOCK_INPUT;
8575 #endif /* HAVE_WINDOW_SYSTEM */
8578 /***********************************************************************
8579 Tool-bars
8580 ***********************************************************************/
8582 #ifdef HAVE_WINDOW_SYSTEM
8584 /* Where the mouse was last time we reported a mouse event. */
8586 FRAME_PTR last_mouse_frame;
8588 /* Tool-bar item index of the item on which a mouse button was pressed
8589 or -1. */
8591 int last_tool_bar_item;
8594 /* Update the tool-bar item list for frame F. This has to be done
8595 before we start to fill in any display lines. Called from
8596 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8597 and restore it here. */
8599 static void
8600 update_tool_bar (f, save_match_data)
8601 struct frame *f;
8602 int save_match_data;
8604 #ifdef USE_GTK
8605 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8606 #else
8607 int do_update = WINDOWP (f->tool_bar_window)
8608 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8609 #endif
8611 if (do_update)
8613 Lisp_Object window;
8614 struct window *w;
8616 window = FRAME_SELECTED_WINDOW (f);
8617 w = XWINDOW (window);
8619 /* If the user has switched buffers or windows, we need to
8620 recompute to reflect the new bindings. But we'll
8621 recompute when update_mode_lines is set too; that means
8622 that people can use force-mode-line-update to request
8623 that the menu bar be recomputed. The adverse effect on
8624 the rest of the redisplay algorithm is about the same as
8625 windows_or_buffers_changed anyway. */
8626 if (windows_or_buffers_changed
8627 || !NILP (w->update_mode_line)
8628 || update_mode_lines
8629 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8630 < BUF_MODIFF (XBUFFER (w->buffer)))
8631 != !NILP (w->last_had_star))
8632 || ((!NILP (Vtransient_mark_mode)
8633 && !NILP (XBUFFER (w->buffer)->mark_active))
8634 != !NILP (w->region_showing)))
8636 struct buffer *prev = current_buffer;
8637 int count = SPECPDL_INDEX ();
8638 Lisp_Object new_tool_bar;
8639 int new_n_tool_bar;
8640 struct gcpro gcpro1;
8642 /* Set current_buffer to the buffer of the selected
8643 window of the frame, so that we get the right local
8644 keymaps. */
8645 set_buffer_internal_1 (XBUFFER (w->buffer));
8647 /* Save match data, if we must. */
8648 if (save_match_data)
8649 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8651 /* Make sure that we don't accidentally use bogus keymaps. */
8652 if (NILP (Voverriding_local_map_menu_flag))
8654 specbind (Qoverriding_terminal_local_map, Qnil);
8655 specbind (Qoverriding_local_map, Qnil);
8658 GCPRO1 (new_tool_bar);
8660 /* Build desired tool-bar items from keymaps. */
8661 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8662 &new_n_tool_bar);
8664 /* Redisplay the tool-bar if we changed it. */
8665 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8667 /* Redisplay that happens asynchronously due to an expose event
8668 may access f->tool_bar_items. Make sure we update both
8669 variables within BLOCK_INPUT so no such event interrupts. */
8670 BLOCK_INPUT;
8671 f->tool_bar_items = new_tool_bar;
8672 f->n_tool_bar_items = new_n_tool_bar;
8673 w->update_mode_line = Qt;
8674 UNBLOCK_INPUT;
8677 UNGCPRO;
8679 unbind_to (count, Qnil);
8680 set_buffer_internal_1 (prev);
8686 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8687 F's desired tool-bar contents. F->tool_bar_items must have
8688 been set up previously by calling prepare_menu_bars. */
8690 static void
8691 build_desired_tool_bar_string (f)
8692 struct frame *f;
8694 int i, size, size_needed;
8695 struct gcpro gcpro1, gcpro2, gcpro3;
8696 Lisp_Object image, plist, props;
8698 image = plist = props = Qnil;
8699 GCPRO3 (image, plist, props);
8701 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8702 Otherwise, make a new string. */
8704 /* The size of the string we might be able to reuse. */
8705 size = (STRINGP (f->desired_tool_bar_string)
8706 ? SCHARS (f->desired_tool_bar_string)
8707 : 0);
8709 /* We need one space in the string for each image. */
8710 size_needed = f->n_tool_bar_items;
8712 /* Reuse f->desired_tool_bar_string, if possible. */
8713 if (size < size_needed || NILP (f->desired_tool_bar_string))
8714 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8715 make_number (' '));
8716 else
8718 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8719 Fremove_text_properties (make_number (0), make_number (size),
8720 props, f->desired_tool_bar_string);
8723 /* Put a `display' property on the string for the images to display,
8724 put a `menu_item' property on tool-bar items with a value that
8725 is the index of the item in F's tool-bar item vector. */
8726 for (i = 0; i < f->n_tool_bar_items; ++i)
8728 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8730 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8731 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8732 int hmargin, vmargin, relief, idx, end;
8733 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8735 /* If image is a vector, choose the image according to the
8736 button state. */
8737 image = PROP (TOOL_BAR_ITEM_IMAGES);
8738 if (VECTORP (image))
8740 if (enabled_p)
8741 idx = (selected_p
8742 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8743 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8744 else
8745 idx = (selected_p
8746 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8747 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8749 xassert (ASIZE (image) >= idx);
8750 image = AREF (image, idx);
8752 else
8753 idx = -1;
8755 /* Ignore invalid image specifications. */
8756 if (!valid_image_p (image))
8757 continue;
8759 /* Display the tool-bar button pressed, or depressed. */
8760 plist = Fcopy_sequence (XCDR (image));
8762 /* Compute margin and relief to draw. */
8763 relief = (tool_bar_button_relief >= 0
8764 ? tool_bar_button_relief
8765 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8766 hmargin = vmargin = relief;
8768 if (INTEGERP (Vtool_bar_button_margin)
8769 && XINT (Vtool_bar_button_margin) > 0)
8771 hmargin += XFASTINT (Vtool_bar_button_margin);
8772 vmargin += XFASTINT (Vtool_bar_button_margin);
8774 else if (CONSP (Vtool_bar_button_margin))
8776 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8777 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8778 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8780 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8781 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8782 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8785 if (auto_raise_tool_bar_buttons_p)
8787 /* Add a `:relief' property to the image spec if the item is
8788 selected. */
8789 if (selected_p)
8791 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8792 hmargin -= relief;
8793 vmargin -= relief;
8796 else
8798 /* If image is selected, display it pressed, i.e. with a
8799 negative relief. If it's not selected, display it with a
8800 raised relief. */
8801 plist = Fplist_put (plist, QCrelief,
8802 (selected_p
8803 ? make_number (-relief)
8804 : make_number (relief)));
8805 hmargin -= relief;
8806 vmargin -= relief;
8809 /* Put a margin around the image. */
8810 if (hmargin || vmargin)
8812 if (hmargin == vmargin)
8813 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8814 else
8815 plist = Fplist_put (plist, QCmargin,
8816 Fcons (make_number (hmargin),
8817 make_number (vmargin)));
8820 /* If button is not enabled, and we don't have special images
8821 for the disabled state, make the image appear disabled by
8822 applying an appropriate algorithm to it. */
8823 if (!enabled_p && idx < 0)
8824 plist = Fplist_put (plist, QCconversion, Qdisabled);
8826 /* Put a `display' text property on the string for the image to
8827 display. Put a `menu-item' property on the string that gives
8828 the start of this item's properties in the tool-bar items
8829 vector. */
8830 image = Fcons (Qimage, plist);
8831 props = list4 (Qdisplay, image,
8832 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8834 /* Let the last image hide all remaining spaces in the tool bar
8835 string. The string can be longer than needed when we reuse a
8836 previous string. */
8837 if (i + 1 == f->n_tool_bar_items)
8838 end = SCHARS (f->desired_tool_bar_string);
8839 else
8840 end = i + 1;
8841 Fadd_text_properties (make_number (i), make_number (end),
8842 props, f->desired_tool_bar_string);
8843 #undef PROP
8846 UNGCPRO;
8850 /* Display one line of the tool-bar of frame IT->f. */
8852 static void
8853 display_tool_bar_line (it)
8854 struct it *it;
8856 struct glyph_row *row = it->glyph_row;
8857 int max_x = it->last_visible_x;
8858 struct glyph *last;
8860 prepare_desired_row (row);
8861 row->y = it->current_y;
8863 /* Note that this isn't made use of if the face hasn't a box,
8864 so there's no need to check the face here. */
8865 it->start_of_box_run_p = 1;
8867 while (it->current_x < max_x)
8869 int x_before, x, n_glyphs_before, i, nglyphs;
8871 /* Get the next display element. */
8872 if (!get_next_display_element (it))
8873 break;
8875 /* Produce glyphs. */
8876 x_before = it->current_x;
8877 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8878 PRODUCE_GLYPHS (it);
8880 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8881 i = 0;
8882 x = x_before;
8883 while (i < nglyphs)
8885 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8887 if (x + glyph->pixel_width > max_x)
8889 /* Glyph doesn't fit on line. */
8890 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8891 it->current_x = x;
8892 goto out;
8895 ++it->hpos;
8896 x += glyph->pixel_width;
8897 ++i;
8900 /* Stop at line ends. */
8901 if (ITERATOR_AT_END_OF_LINE_P (it))
8902 break;
8904 set_iterator_to_next (it, 1);
8907 out:;
8909 row->displays_text_p = row->used[TEXT_AREA] != 0;
8910 extend_face_to_end_of_line (it);
8911 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8912 last->right_box_line_p = 1;
8913 if (last == row->glyphs[TEXT_AREA])
8914 last->left_box_line_p = 1;
8915 compute_line_metrics (it);
8917 /* If line is empty, make it occupy the rest of the tool-bar. */
8918 if (!row->displays_text_p)
8920 row->height = row->phys_height = it->last_visible_y - row->y;
8921 row->ascent = row->phys_ascent = 0;
8922 row->extra_line_spacing = 0;
8925 row->full_width_p = 1;
8926 row->continued_p = 0;
8927 row->truncated_on_left_p = 0;
8928 row->truncated_on_right_p = 0;
8930 it->current_x = it->hpos = 0;
8931 it->current_y += row->height;
8932 ++it->vpos;
8933 ++it->glyph_row;
8937 /* Value is the number of screen lines needed to make all tool-bar
8938 items of frame F visible. */
8940 static int
8941 tool_bar_lines_needed (f)
8942 struct frame *f;
8944 struct window *w = XWINDOW (f->tool_bar_window);
8945 struct it it;
8947 /* Initialize an iterator for iteration over
8948 F->desired_tool_bar_string in the tool-bar window of frame F. */
8949 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8950 it.first_visible_x = 0;
8951 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8952 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8954 while (!ITERATOR_AT_END_P (&it))
8956 it.glyph_row = w->desired_matrix->rows;
8957 clear_glyph_row (it.glyph_row);
8958 display_tool_bar_line (&it);
8961 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8965 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8966 0, 1, 0,
8967 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8968 (frame)
8969 Lisp_Object frame;
8971 struct frame *f;
8972 struct window *w;
8973 int nlines = 0;
8975 if (NILP (frame))
8976 frame = selected_frame;
8977 else
8978 CHECK_FRAME (frame);
8979 f = XFRAME (frame);
8981 if (WINDOWP (f->tool_bar_window)
8982 || (w = XWINDOW (f->tool_bar_window),
8983 WINDOW_TOTAL_LINES (w) > 0))
8985 update_tool_bar (f, 1);
8986 if (f->n_tool_bar_items)
8988 build_desired_tool_bar_string (f);
8989 nlines = tool_bar_lines_needed (f);
8993 return make_number (nlines);
8997 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8998 height should be changed. */
9000 static int
9001 redisplay_tool_bar (f)
9002 struct frame *f;
9004 struct window *w;
9005 struct it it;
9006 struct glyph_row *row;
9007 int change_height_p = 0;
9009 #ifdef USE_GTK
9010 if (FRAME_EXTERNAL_TOOL_BAR (f))
9011 update_frame_tool_bar (f);
9012 return 0;
9013 #endif
9015 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9016 do anything. This means you must start with tool-bar-lines
9017 non-zero to get the auto-sizing effect. Or in other words, you
9018 can turn off tool-bars by specifying tool-bar-lines zero. */
9019 if (!WINDOWP (f->tool_bar_window)
9020 || (w = XWINDOW (f->tool_bar_window),
9021 WINDOW_TOTAL_LINES (w) == 0))
9022 return 0;
9024 /* Set up an iterator for the tool-bar window. */
9025 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9026 it.first_visible_x = 0;
9027 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9028 row = it.glyph_row;
9030 /* Build a string that represents the contents of the tool-bar. */
9031 build_desired_tool_bar_string (f);
9032 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9034 /* Display as many lines as needed to display all tool-bar items. */
9035 while (it.current_y < it.last_visible_y)
9036 display_tool_bar_line (&it);
9038 /* It doesn't make much sense to try scrolling in the tool-bar
9039 window, so don't do it. */
9040 w->desired_matrix->no_scrolling_p = 1;
9041 w->must_be_updated_p = 1;
9043 if (auto_resize_tool_bars_p)
9045 int nlines;
9047 /* If we couldn't display everything, change the tool-bar's
9048 height. */
9049 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9050 change_height_p = 1;
9052 /* If there are blank lines at the end, except for a partially
9053 visible blank line at the end that is smaller than
9054 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9055 row = it.glyph_row - 1;
9056 if (!row->displays_text_p
9057 && row->height >= FRAME_LINE_HEIGHT (f))
9058 change_height_p = 1;
9060 /* If row displays tool-bar items, but is partially visible,
9061 change the tool-bar's height. */
9062 if (row->displays_text_p
9063 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9064 change_height_p = 1;
9066 /* Resize windows as needed by changing the `tool-bar-lines'
9067 frame parameter. */
9068 if (change_height_p
9069 && (nlines = tool_bar_lines_needed (f),
9070 nlines != WINDOW_TOTAL_LINES (w)))
9072 extern Lisp_Object Qtool_bar_lines;
9073 Lisp_Object frame;
9074 int old_height = WINDOW_TOTAL_LINES (w);
9076 XSETFRAME (frame, f);
9077 clear_glyph_matrix (w->desired_matrix);
9078 Fmodify_frame_parameters (frame,
9079 Fcons (Fcons (Qtool_bar_lines,
9080 make_number (nlines)),
9081 Qnil));
9082 if (WINDOW_TOTAL_LINES (w) != old_height)
9083 fonts_changed_p = 1;
9087 return change_height_p;
9091 /* Get information about the tool-bar item which is displayed in GLYPH
9092 on frame F. Return in *PROP_IDX the index where tool-bar item
9093 properties start in F->tool_bar_items. Value is zero if
9094 GLYPH doesn't display a tool-bar item. */
9096 static int
9097 tool_bar_item_info (f, glyph, prop_idx)
9098 struct frame *f;
9099 struct glyph *glyph;
9100 int *prop_idx;
9102 Lisp_Object prop;
9103 int success_p;
9104 int charpos;
9106 /* This function can be called asynchronously, which means we must
9107 exclude any possibility that Fget_text_property signals an
9108 error. */
9109 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9110 charpos = max (0, charpos);
9112 /* Get the text property `menu-item' at pos. The value of that
9113 property is the start index of this item's properties in
9114 F->tool_bar_items. */
9115 prop = Fget_text_property (make_number (charpos),
9116 Qmenu_item, f->current_tool_bar_string);
9117 if (INTEGERP (prop))
9119 *prop_idx = XINT (prop);
9120 success_p = 1;
9122 else
9123 success_p = 0;
9125 return success_p;
9129 /* Get information about the tool-bar item at position X/Y on frame F.
9130 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9131 the current matrix of the tool-bar window of F, or NULL if not
9132 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9133 item in F->tool_bar_items. Value is
9135 -1 if X/Y is not on a tool-bar item
9136 0 if X/Y is on the same item that was highlighted before.
9137 1 otherwise. */
9139 static int
9140 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9141 struct frame *f;
9142 int x, y;
9143 struct glyph **glyph;
9144 int *hpos, *vpos, *prop_idx;
9146 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9147 struct window *w = XWINDOW (f->tool_bar_window);
9148 int area;
9150 /* Find the glyph under X/Y. */
9151 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9152 if (*glyph == NULL)
9153 return -1;
9155 /* Get the start of this tool-bar item's properties in
9156 f->tool_bar_items. */
9157 if (!tool_bar_item_info (f, *glyph, prop_idx))
9158 return -1;
9160 /* Is mouse on the highlighted item? */
9161 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9162 && *vpos >= dpyinfo->mouse_face_beg_row
9163 && *vpos <= dpyinfo->mouse_face_end_row
9164 && (*vpos > dpyinfo->mouse_face_beg_row
9165 || *hpos >= dpyinfo->mouse_face_beg_col)
9166 && (*vpos < dpyinfo->mouse_face_end_row
9167 || *hpos < dpyinfo->mouse_face_end_col
9168 || dpyinfo->mouse_face_past_end))
9169 return 0;
9171 return 1;
9175 /* EXPORT:
9176 Handle mouse button event on the tool-bar of frame F, at
9177 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9178 0 for button release. MODIFIERS is event modifiers for button
9179 release. */
9181 void
9182 handle_tool_bar_click (f, x, y, down_p, modifiers)
9183 struct frame *f;
9184 int x, y, down_p;
9185 unsigned int modifiers;
9187 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9188 struct window *w = XWINDOW (f->tool_bar_window);
9189 int hpos, vpos, prop_idx;
9190 struct glyph *glyph;
9191 Lisp_Object enabled_p;
9193 /* If not on the highlighted tool-bar item, return. */
9194 frame_to_window_pixel_xy (w, &x, &y);
9195 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9196 return;
9198 /* If item is disabled, do nothing. */
9199 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9200 if (NILP (enabled_p))
9201 return;
9203 if (down_p)
9205 /* Show item in pressed state. */
9206 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9207 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9208 last_tool_bar_item = prop_idx;
9210 else
9212 Lisp_Object key, frame;
9213 struct input_event event;
9214 EVENT_INIT (event);
9216 /* Show item in released state. */
9217 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9218 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9220 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9222 XSETFRAME (frame, f);
9223 event.kind = TOOL_BAR_EVENT;
9224 event.frame_or_window = frame;
9225 event.arg = frame;
9226 kbd_buffer_store_event (&event);
9228 event.kind = TOOL_BAR_EVENT;
9229 event.frame_or_window = frame;
9230 event.arg = key;
9231 event.modifiers = modifiers;
9232 kbd_buffer_store_event (&event);
9233 last_tool_bar_item = -1;
9238 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9239 tool-bar window-relative coordinates X/Y. Called from
9240 note_mouse_highlight. */
9242 static void
9243 note_tool_bar_highlight (f, x, y)
9244 struct frame *f;
9245 int x, y;
9247 Lisp_Object window = f->tool_bar_window;
9248 struct window *w = XWINDOW (window);
9249 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9250 int hpos, vpos;
9251 struct glyph *glyph;
9252 struct glyph_row *row;
9253 int i;
9254 Lisp_Object enabled_p;
9255 int prop_idx;
9256 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9257 int mouse_down_p, rc;
9259 /* Function note_mouse_highlight is called with negative x(y
9260 values when mouse moves outside of the frame. */
9261 if (x <= 0 || y <= 0)
9263 clear_mouse_face (dpyinfo);
9264 return;
9267 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9268 if (rc < 0)
9270 /* Not on tool-bar item. */
9271 clear_mouse_face (dpyinfo);
9272 return;
9274 else if (rc == 0)
9275 /* On same tool-bar item as before. */
9276 goto set_help_echo;
9278 clear_mouse_face (dpyinfo);
9280 /* Mouse is down, but on different tool-bar item? */
9281 mouse_down_p = (dpyinfo->grabbed
9282 && f == last_mouse_frame
9283 && FRAME_LIVE_P (f));
9284 if (mouse_down_p
9285 && last_tool_bar_item != prop_idx)
9286 return;
9288 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9289 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9291 /* If tool-bar item is not enabled, don't highlight it. */
9292 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9293 if (!NILP (enabled_p))
9295 /* Compute the x-position of the glyph. In front and past the
9296 image is a space. We include this in the highlighted area. */
9297 row = MATRIX_ROW (w->current_matrix, vpos);
9298 for (i = x = 0; i < hpos; ++i)
9299 x += row->glyphs[TEXT_AREA][i].pixel_width;
9301 /* Record this as the current active region. */
9302 dpyinfo->mouse_face_beg_col = hpos;
9303 dpyinfo->mouse_face_beg_row = vpos;
9304 dpyinfo->mouse_face_beg_x = x;
9305 dpyinfo->mouse_face_beg_y = row->y;
9306 dpyinfo->mouse_face_past_end = 0;
9308 dpyinfo->mouse_face_end_col = hpos + 1;
9309 dpyinfo->mouse_face_end_row = vpos;
9310 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9311 dpyinfo->mouse_face_end_y = row->y;
9312 dpyinfo->mouse_face_window = window;
9313 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9315 /* Display it as active. */
9316 show_mouse_face (dpyinfo, draw);
9317 dpyinfo->mouse_face_image_state = draw;
9320 set_help_echo:
9322 /* Set help_echo_string to a help string to display for this tool-bar item.
9323 XTread_socket does the rest. */
9324 help_echo_object = help_echo_window = Qnil;
9325 help_echo_pos = -1;
9326 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9327 if (NILP (help_echo_string))
9328 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9331 #endif /* HAVE_WINDOW_SYSTEM */
9335 /************************************************************************
9336 Horizontal scrolling
9337 ************************************************************************/
9339 static int hscroll_window_tree P_ ((Lisp_Object));
9340 static int hscroll_windows P_ ((Lisp_Object));
9342 /* For all leaf windows in the window tree rooted at WINDOW, set their
9343 hscroll value so that PT is (i) visible in the window, and (ii) so
9344 that it is not within a certain margin at the window's left and
9345 right border. Value is non-zero if any window's hscroll has been
9346 changed. */
9348 static int
9349 hscroll_window_tree (window)
9350 Lisp_Object window;
9352 int hscrolled_p = 0;
9353 int hscroll_relative_p = FLOATP (Vhscroll_step);
9354 int hscroll_step_abs = 0;
9355 double hscroll_step_rel = 0;
9357 if (hscroll_relative_p)
9359 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9360 if (hscroll_step_rel < 0)
9362 hscroll_relative_p = 0;
9363 hscroll_step_abs = 0;
9366 else if (INTEGERP (Vhscroll_step))
9368 hscroll_step_abs = XINT (Vhscroll_step);
9369 if (hscroll_step_abs < 0)
9370 hscroll_step_abs = 0;
9372 else
9373 hscroll_step_abs = 0;
9375 while (WINDOWP (window))
9377 struct window *w = XWINDOW (window);
9379 if (WINDOWP (w->hchild))
9380 hscrolled_p |= hscroll_window_tree (w->hchild);
9381 else if (WINDOWP (w->vchild))
9382 hscrolled_p |= hscroll_window_tree (w->vchild);
9383 else if (w->cursor.vpos >= 0)
9385 int h_margin;
9386 int text_area_width;
9387 struct glyph_row *current_cursor_row
9388 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9389 struct glyph_row *desired_cursor_row
9390 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9391 struct glyph_row *cursor_row
9392 = (desired_cursor_row->enabled_p
9393 ? desired_cursor_row
9394 : current_cursor_row);
9396 text_area_width = window_box_width (w, TEXT_AREA);
9398 /* Scroll when cursor is inside this scroll margin. */
9399 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9401 if ((XFASTINT (w->hscroll)
9402 && w->cursor.x <= h_margin)
9403 || (cursor_row->enabled_p
9404 && cursor_row->truncated_on_right_p
9405 && (w->cursor.x >= text_area_width - h_margin)))
9407 struct it it;
9408 int hscroll;
9409 struct buffer *saved_current_buffer;
9410 int pt;
9411 int wanted_x;
9413 /* Find point in a display of infinite width. */
9414 saved_current_buffer = current_buffer;
9415 current_buffer = XBUFFER (w->buffer);
9417 if (w == XWINDOW (selected_window))
9418 pt = BUF_PT (current_buffer);
9419 else
9421 pt = marker_position (w->pointm);
9422 pt = max (BEGV, pt);
9423 pt = min (ZV, pt);
9426 /* Move iterator to pt starting at cursor_row->start in
9427 a line with infinite width. */
9428 init_to_row_start (&it, w, cursor_row);
9429 it.last_visible_x = INFINITY;
9430 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9431 current_buffer = saved_current_buffer;
9433 /* Position cursor in window. */
9434 if (!hscroll_relative_p && hscroll_step_abs == 0)
9435 hscroll = max (0, (it.current_x
9436 - (ITERATOR_AT_END_OF_LINE_P (&it)
9437 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9438 : (text_area_width / 2))))
9439 / FRAME_COLUMN_WIDTH (it.f);
9440 else if (w->cursor.x >= text_area_width - h_margin)
9442 if (hscroll_relative_p)
9443 wanted_x = text_area_width * (1 - hscroll_step_rel)
9444 - h_margin;
9445 else
9446 wanted_x = text_area_width
9447 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9448 - h_margin;
9449 hscroll
9450 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9452 else
9454 if (hscroll_relative_p)
9455 wanted_x = text_area_width * hscroll_step_rel
9456 + h_margin;
9457 else
9458 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9459 + h_margin;
9460 hscroll
9461 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9463 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9465 /* Don't call Fset_window_hscroll if value hasn't
9466 changed because it will prevent redisplay
9467 optimizations. */
9468 if (XFASTINT (w->hscroll) != hscroll)
9470 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9471 w->hscroll = make_number (hscroll);
9472 hscrolled_p = 1;
9477 window = w->next;
9480 /* Value is non-zero if hscroll of any leaf window has been changed. */
9481 return hscrolled_p;
9485 /* Set hscroll so that cursor is visible and not inside horizontal
9486 scroll margins for all windows in the tree rooted at WINDOW. See
9487 also hscroll_window_tree above. Value is non-zero if any window's
9488 hscroll has been changed. If it has, desired matrices on the frame
9489 of WINDOW are cleared. */
9491 static int
9492 hscroll_windows (window)
9493 Lisp_Object window;
9495 int hscrolled_p;
9497 if (automatic_hscrolling_p)
9499 hscrolled_p = hscroll_window_tree (window);
9500 if (hscrolled_p)
9501 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9503 else
9504 hscrolled_p = 0;
9505 return hscrolled_p;
9510 /************************************************************************
9511 Redisplay
9512 ************************************************************************/
9514 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9515 to a non-zero value. This is sometimes handy to have in a debugger
9516 session. */
9518 #if GLYPH_DEBUG
9520 /* First and last unchanged row for try_window_id. */
9522 int debug_first_unchanged_at_end_vpos;
9523 int debug_last_unchanged_at_beg_vpos;
9525 /* Delta vpos and y. */
9527 int debug_dvpos, debug_dy;
9529 /* Delta in characters and bytes for try_window_id. */
9531 int debug_delta, debug_delta_bytes;
9533 /* Values of window_end_pos and window_end_vpos at the end of
9534 try_window_id. */
9536 EMACS_INT debug_end_pos, debug_end_vpos;
9538 /* Append a string to W->desired_matrix->method. FMT is a printf
9539 format string. A1...A9 are a supplement for a variable-length
9540 argument list. If trace_redisplay_p is non-zero also printf the
9541 resulting string to stderr. */
9543 static void
9544 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9545 struct window *w;
9546 char *fmt;
9547 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9549 char buffer[512];
9550 char *method = w->desired_matrix->method;
9551 int len = strlen (method);
9552 int size = sizeof w->desired_matrix->method;
9553 int remaining = size - len - 1;
9555 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9556 if (len && remaining)
9558 method[len] = '|';
9559 --remaining, ++len;
9562 strncpy (method + len, buffer, remaining);
9564 if (trace_redisplay_p)
9565 fprintf (stderr, "%p (%s): %s\n",
9567 ((BUFFERP (w->buffer)
9568 && STRINGP (XBUFFER (w->buffer)->name))
9569 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9570 : "no buffer"),
9571 buffer);
9574 #endif /* GLYPH_DEBUG */
9577 /* Value is non-zero if all changes in window W, which displays
9578 current_buffer, are in the text between START and END. START is a
9579 buffer position, END is given as a distance from Z. Used in
9580 redisplay_internal for display optimization. */
9582 static INLINE int
9583 text_outside_line_unchanged_p (w, start, end)
9584 struct window *w;
9585 int start, end;
9587 int unchanged_p = 1;
9589 /* If text or overlays have changed, see where. */
9590 if (XFASTINT (w->last_modified) < MODIFF
9591 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9593 /* Gap in the line? */
9594 if (GPT < start || Z - GPT < end)
9595 unchanged_p = 0;
9597 /* Changes start in front of the line, or end after it? */
9598 if (unchanged_p
9599 && (BEG_UNCHANGED < start - 1
9600 || END_UNCHANGED < end))
9601 unchanged_p = 0;
9603 /* If selective display, can't optimize if changes start at the
9604 beginning of the line. */
9605 if (unchanged_p
9606 && INTEGERP (current_buffer->selective_display)
9607 && XINT (current_buffer->selective_display) > 0
9608 && (BEG_UNCHANGED < start || GPT <= start))
9609 unchanged_p = 0;
9611 /* If there are overlays at the start or end of the line, these
9612 may have overlay strings with newlines in them. A change at
9613 START, for instance, may actually concern the display of such
9614 overlay strings as well, and they are displayed on different
9615 lines. So, quickly rule out this case. (For the future, it
9616 might be desirable to implement something more telling than
9617 just BEG/END_UNCHANGED.) */
9618 if (unchanged_p)
9620 if (BEG + BEG_UNCHANGED == start
9621 && overlay_touches_p (start))
9622 unchanged_p = 0;
9623 if (END_UNCHANGED == end
9624 && overlay_touches_p (Z - end))
9625 unchanged_p = 0;
9629 return unchanged_p;
9633 /* Do a frame update, taking possible shortcuts into account. This is
9634 the main external entry point for redisplay.
9636 If the last redisplay displayed an echo area message and that message
9637 is no longer requested, we clear the echo area or bring back the
9638 mini-buffer if that is in use. */
9640 void
9641 redisplay ()
9643 redisplay_internal (0);
9647 static Lisp_Object
9648 overlay_arrow_string_or_property (var, pbitmap)
9649 Lisp_Object var;
9650 int *pbitmap;
9652 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9653 Lisp_Object bitmap;
9655 if (pbitmap)
9657 *pbitmap = 0;
9658 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9659 *pbitmap = XINT (bitmap);
9662 if (!NILP (pstr))
9663 return pstr;
9664 return Voverlay_arrow_string;
9667 /* Return 1 if there are any overlay-arrows in current_buffer. */
9668 static int
9669 overlay_arrow_in_current_buffer_p ()
9671 Lisp_Object vlist;
9673 for (vlist = Voverlay_arrow_variable_list;
9674 CONSP (vlist);
9675 vlist = XCDR (vlist))
9677 Lisp_Object var = XCAR (vlist);
9678 Lisp_Object val;
9680 if (!SYMBOLP (var))
9681 continue;
9682 val = find_symbol_value (var);
9683 if (MARKERP (val)
9684 && current_buffer == XMARKER (val)->buffer)
9685 return 1;
9687 return 0;
9691 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9692 has changed. */
9694 static int
9695 overlay_arrows_changed_p ()
9697 Lisp_Object vlist;
9699 for (vlist = Voverlay_arrow_variable_list;
9700 CONSP (vlist);
9701 vlist = XCDR (vlist))
9703 Lisp_Object var = XCAR (vlist);
9704 Lisp_Object val, pstr;
9706 if (!SYMBOLP (var))
9707 continue;
9708 val = find_symbol_value (var);
9709 if (!MARKERP (val))
9710 continue;
9711 if (! EQ (COERCE_MARKER (val),
9712 Fget (var, Qlast_arrow_position))
9713 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9714 EQ (pstr, Fget (var, Qlast_arrow_string))))
9715 return 1;
9717 return 0;
9720 /* Mark overlay arrows to be updated on next redisplay. */
9722 static void
9723 update_overlay_arrows (up_to_date)
9724 int up_to_date;
9726 Lisp_Object vlist;
9728 for (vlist = Voverlay_arrow_variable_list;
9729 CONSP (vlist);
9730 vlist = XCDR (vlist))
9732 Lisp_Object var = XCAR (vlist);
9734 if (!SYMBOLP (var))
9735 continue;
9737 if (up_to_date > 0)
9739 Lisp_Object val = find_symbol_value (var);
9740 Fput (var, Qlast_arrow_position,
9741 COERCE_MARKER (val));
9742 Fput (var, Qlast_arrow_string,
9743 overlay_arrow_string_or_property (var, 0));
9745 else if (up_to_date < 0
9746 || !NILP (Fget (var, Qlast_arrow_position)))
9748 Fput (var, Qlast_arrow_position, Qt);
9749 Fput (var, Qlast_arrow_string, Qt);
9755 /* Return overlay arrow string to display at row.
9756 Return t if display as bitmap in left fringe.
9757 Return nil if no overlay arrow. */
9759 static Lisp_Object
9760 overlay_arrow_at_row (it, row, pbitmap)
9761 struct it *it;
9762 struct glyph_row *row;
9763 int *pbitmap;
9765 Lisp_Object vlist;
9767 for (vlist = Voverlay_arrow_variable_list;
9768 CONSP (vlist);
9769 vlist = XCDR (vlist))
9771 Lisp_Object var = XCAR (vlist);
9772 Lisp_Object val;
9774 if (!SYMBOLP (var))
9775 continue;
9777 val = find_symbol_value (var);
9779 if (MARKERP (val)
9780 && current_buffer == XMARKER (val)->buffer
9781 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9783 val = overlay_arrow_string_or_property (var, pbitmap);
9784 if (FRAME_WINDOW_P (it->f)
9785 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9786 return Qt;
9787 if (STRINGP (val))
9788 return val;
9789 break;
9793 *pbitmap = 0;
9794 return Qnil;
9797 /* Return 1 if point moved out of or into a composition. Otherwise
9798 return 0. PREV_BUF and PREV_PT are the last point buffer and
9799 position. BUF and PT are the current point buffer and position. */
9802 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9803 struct buffer *prev_buf, *buf;
9804 int prev_pt, pt;
9806 int start, end;
9807 Lisp_Object prop;
9808 Lisp_Object buffer;
9810 XSETBUFFER (buffer, buf);
9811 /* Check a composition at the last point if point moved within the
9812 same buffer. */
9813 if (prev_buf == buf)
9815 if (prev_pt == pt)
9816 /* Point didn't move. */
9817 return 0;
9819 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9820 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9821 && COMPOSITION_VALID_P (start, end, prop)
9822 && start < prev_pt && end > prev_pt)
9823 /* The last point was within the composition. Return 1 iff
9824 point moved out of the composition. */
9825 return (pt <= start || pt >= end);
9828 /* Check a composition at the current point. */
9829 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9830 && find_composition (pt, -1, &start, &end, &prop, buffer)
9831 && COMPOSITION_VALID_P (start, end, prop)
9832 && start < pt && end > pt);
9836 /* Reconsider the setting of B->clip_changed which is displayed
9837 in window W. */
9839 static INLINE void
9840 reconsider_clip_changes (w, b)
9841 struct window *w;
9842 struct buffer *b;
9844 if (b->clip_changed
9845 && !NILP (w->window_end_valid)
9846 && w->current_matrix->buffer == b
9847 && w->current_matrix->zv == BUF_ZV (b)
9848 && w->current_matrix->begv == BUF_BEGV (b))
9849 b->clip_changed = 0;
9851 /* If display wasn't paused, and W is not a tool bar window, see if
9852 point has been moved into or out of a composition. In that case,
9853 we set b->clip_changed to 1 to force updating the screen. If
9854 b->clip_changed has already been set to 1, we can skip this
9855 check. */
9856 if (!b->clip_changed
9857 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9859 int pt;
9861 if (w == XWINDOW (selected_window))
9862 pt = BUF_PT (current_buffer);
9863 else
9864 pt = marker_position (w->pointm);
9866 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9867 || pt != XINT (w->last_point))
9868 && check_point_in_composition (w->current_matrix->buffer,
9869 XINT (w->last_point),
9870 XBUFFER (w->buffer), pt))
9871 b->clip_changed = 1;
9876 /* Select FRAME to forward the values of frame-local variables into C
9877 variables so that the redisplay routines can access those values
9878 directly. */
9880 static void
9881 select_frame_for_redisplay (frame)
9882 Lisp_Object frame;
9884 Lisp_Object tail, sym, val;
9885 Lisp_Object old = selected_frame;
9887 selected_frame = frame;
9889 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9890 if (CONSP (XCAR (tail))
9891 && (sym = XCAR (XCAR (tail)),
9892 SYMBOLP (sym))
9893 && (sym = indirect_variable (sym),
9894 val = SYMBOL_VALUE (sym),
9895 (BUFFER_LOCAL_VALUEP (val)
9896 || SOME_BUFFER_LOCAL_VALUEP (val)))
9897 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9898 Fsymbol_value (sym);
9900 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9901 if (CONSP (XCAR (tail))
9902 && (sym = XCAR (XCAR (tail)),
9903 SYMBOLP (sym))
9904 && (sym = indirect_variable (sym),
9905 val = SYMBOL_VALUE (sym),
9906 (BUFFER_LOCAL_VALUEP (val)
9907 || SOME_BUFFER_LOCAL_VALUEP (val)))
9908 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9909 Fsymbol_value (sym);
9913 #define STOP_POLLING \
9914 do { if (! polling_stopped_here) stop_polling (); \
9915 polling_stopped_here = 1; } while (0)
9917 #define RESUME_POLLING \
9918 do { if (polling_stopped_here) start_polling (); \
9919 polling_stopped_here = 0; } while (0)
9922 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9923 response to any user action; therefore, we should preserve the echo
9924 area. (Actually, our caller does that job.) Perhaps in the future
9925 avoid recentering windows if it is not necessary; currently that
9926 causes some problems. */
9928 static void
9929 redisplay_internal (preserve_echo_area)
9930 int preserve_echo_area;
9932 struct window *w = XWINDOW (selected_window);
9933 struct frame *f = XFRAME (w->frame);
9934 int pause;
9935 int must_finish = 0;
9936 struct text_pos tlbufpos, tlendpos;
9937 int number_of_visible_frames;
9938 int count;
9939 struct frame *sf = SELECTED_FRAME ();
9940 int polling_stopped_here = 0;
9942 /* Non-zero means redisplay has to consider all windows on all
9943 frames. Zero means, only selected_window is considered. */
9944 int consider_all_windows_p;
9946 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9948 /* No redisplay if running in batch mode or frame is not yet fully
9949 initialized, or redisplay is explicitly turned off by setting
9950 Vinhibit_redisplay. */
9951 if (noninteractive
9952 || !NILP (Vinhibit_redisplay)
9953 || !f->glyphs_initialized_p)
9954 return;
9956 /* The flag redisplay_performed_directly_p is set by
9957 direct_output_for_insert when it already did the whole screen
9958 update necessary. */
9959 if (redisplay_performed_directly_p)
9961 redisplay_performed_directly_p = 0;
9962 if (!hscroll_windows (selected_window))
9963 return;
9966 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9967 if (popup_activated ())
9968 return;
9969 #endif
9971 /* I don't think this happens but let's be paranoid. */
9972 if (redisplaying_p)
9973 return;
9975 /* Record a function that resets redisplaying_p to its old value
9976 when we leave this function. */
9977 count = SPECPDL_INDEX ();
9978 record_unwind_protect (unwind_redisplay,
9979 Fcons (make_number (redisplaying_p), selected_frame));
9980 ++redisplaying_p;
9981 specbind (Qinhibit_free_realized_faces, Qnil);
9983 retry:
9984 pause = 0;
9985 reconsider_clip_changes (w, current_buffer);
9987 /* If new fonts have been loaded that make a glyph matrix adjustment
9988 necessary, do it. */
9989 if (fonts_changed_p)
9991 adjust_glyphs (NULL);
9992 ++windows_or_buffers_changed;
9993 fonts_changed_p = 0;
9996 /* If face_change_count is non-zero, init_iterator will free all
9997 realized faces, which includes the faces referenced from current
9998 matrices. So, we can't reuse current matrices in this case. */
9999 if (face_change_count)
10000 ++windows_or_buffers_changed;
10002 if (! FRAME_WINDOW_P (sf)
10003 && previous_terminal_frame != sf)
10005 /* Since frames on an ASCII terminal share the same display
10006 area, displaying a different frame means redisplay the whole
10007 thing. */
10008 windows_or_buffers_changed++;
10009 SET_FRAME_GARBAGED (sf);
10010 XSETFRAME (Vterminal_frame, sf);
10012 previous_terminal_frame = sf;
10014 /* Set the visible flags for all frames. Do this before checking
10015 for resized or garbaged frames; they want to know if their frames
10016 are visible. See the comment in frame.h for
10017 FRAME_SAMPLE_VISIBILITY. */
10019 Lisp_Object tail, frame;
10021 number_of_visible_frames = 0;
10023 FOR_EACH_FRAME (tail, frame)
10025 struct frame *f = XFRAME (frame);
10027 FRAME_SAMPLE_VISIBILITY (f);
10028 if (FRAME_VISIBLE_P (f))
10029 ++number_of_visible_frames;
10030 clear_desired_matrices (f);
10034 /* Notice any pending interrupt request to change frame size. */
10035 do_pending_window_change (1);
10037 /* Clear frames marked as garbaged. */
10038 if (frame_garbaged)
10039 clear_garbaged_frames ();
10041 /* Build menubar and tool-bar items. */
10042 prepare_menu_bars ();
10044 if (windows_or_buffers_changed)
10045 update_mode_lines++;
10047 /* Detect case that we need to write or remove a star in the mode line. */
10048 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10050 w->update_mode_line = Qt;
10051 if (buffer_shared > 1)
10052 update_mode_lines++;
10055 /* If %c is in the mode line, update it if needed. */
10056 if (!NILP (w->column_number_displayed)
10057 /* This alternative quickly identifies a common case
10058 where no change is needed. */
10059 && !(PT == XFASTINT (w->last_point)
10060 && XFASTINT (w->last_modified) >= MODIFF
10061 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10062 && (XFASTINT (w->column_number_displayed)
10063 != (int) current_column ())) /* iftc */
10064 w->update_mode_line = Qt;
10066 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10068 /* The variable buffer_shared is set in redisplay_window and
10069 indicates that we redisplay a buffer in different windows. See
10070 there. */
10071 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10072 || cursor_type_changed);
10074 /* If specs for an arrow have changed, do thorough redisplay
10075 to ensure we remove any arrow that should no longer exist. */
10076 if (overlay_arrows_changed_p ())
10077 consider_all_windows_p = windows_or_buffers_changed = 1;
10079 /* Normally the message* functions will have already displayed and
10080 updated the echo area, but the frame may have been trashed, or
10081 the update may have been preempted, so display the echo area
10082 again here. Checking message_cleared_p captures the case that
10083 the echo area should be cleared. */
10084 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10085 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10086 || (message_cleared_p
10087 && minibuf_level == 0
10088 /* If the mini-window is currently selected, this means the
10089 echo-area doesn't show through. */
10090 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10092 int window_height_changed_p = echo_area_display (0);
10093 must_finish = 1;
10095 /* If we don't display the current message, don't clear the
10096 message_cleared_p flag, because, if we did, we wouldn't clear
10097 the echo area in the next redisplay which doesn't preserve
10098 the echo area. */
10099 if (!display_last_displayed_message_p)
10100 message_cleared_p = 0;
10102 if (fonts_changed_p)
10103 goto retry;
10104 else if (window_height_changed_p)
10106 consider_all_windows_p = 1;
10107 ++update_mode_lines;
10108 ++windows_or_buffers_changed;
10110 /* If window configuration was changed, frames may have been
10111 marked garbaged. Clear them or we will experience
10112 surprises wrt scrolling. */
10113 if (frame_garbaged)
10114 clear_garbaged_frames ();
10117 else if (EQ (selected_window, minibuf_window)
10118 && (current_buffer->clip_changed
10119 || XFASTINT (w->last_modified) < MODIFF
10120 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10121 && resize_mini_window (w, 0))
10123 /* Resized active mini-window to fit the size of what it is
10124 showing if its contents might have changed. */
10125 must_finish = 1;
10126 consider_all_windows_p = 1;
10127 ++windows_or_buffers_changed;
10128 ++update_mode_lines;
10130 /* If window configuration was changed, frames may have been
10131 marked garbaged. Clear them or we will experience
10132 surprises wrt scrolling. */
10133 if (frame_garbaged)
10134 clear_garbaged_frames ();
10138 /* If showing the region, and mark has changed, we must redisplay
10139 the whole window. The assignment to this_line_start_pos prevents
10140 the optimization directly below this if-statement. */
10141 if (((!NILP (Vtransient_mark_mode)
10142 && !NILP (XBUFFER (w->buffer)->mark_active))
10143 != !NILP (w->region_showing))
10144 || (!NILP (w->region_showing)
10145 && !EQ (w->region_showing,
10146 Fmarker_position (XBUFFER (w->buffer)->mark))))
10147 CHARPOS (this_line_start_pos) = 0;
10149 /* Optimize the case that only the line containing the cursor in the
10150 selected window has changed. Variables starting with this_ are
10151 set in display_line and record information about the line
10152 containing the cursor. */
10153 tlbufpos = this_line_start_pos;
10154 tlendpos = this_line_end_pos;
10155 if (!consider_all_windows_p
10156 && CHARPOS (tlbufpos) > 0
10157 && NILP (w->update_mode_line)
10158 && !current_buffer->clip_changed
10159 && !current_buffer->prevent_redisplay_optimizations_p
10160 && FRAME_VISIBLE_P (XFRAME (w->frame))
10161 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10162 /* Make sure recorded data applies to current buffer, etc. */
10163 && this_line_buffer == current_buffer
10164 && current_buffer == XBUFFER (w->buffer)
10165 && NILP (w->force_start)
10166 && NILP (w->optional_new_start)
10167 /* Point must be on the line that we have info recorded about. */
10168 && PT >= CHARPOS (tlbufpos)
10169 && PT <= Z - CHARPOS (tlendpos)
10170 /* All text outside that line, including its final newline,
10171 must be unchanged */
10172 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10173 CHARPOS (tlendpos)))
10175 if (CHARPOS (tlbufpos) > BEGV
10176 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10177 && (CHARPOS (tlbufpos) == ZV
10178 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10179 /* Former continuation line has disappeared by becoming empty */
10180 goto cancel;
10181 else if (XFASTINT (w->last_modified) < MODIFF
10182 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10183 || MINI_WINDOW_P (w))
10185 /* We have to handle the case of continuation around a
10186 wide-column character (See the comment in indent.c around
10187 line 885).
10189 For instance, in the following case:
10191 -------- Insert --------
10192 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10193 J_I_ ==> J_I_ `^^' are cursors.
10194 ^^ ^^
10195 -------- --------
10197 As we have to redraw the line above, we should goto cancel. */
10199 struct it it;
10200 int line_height_before = this_line_pixel_height;
10202 /* Note that start_display will handle the case that the
10203 line starting at tlbufpos is a continuation lines. */
10204 start_display (&it, w, tlbufpos);
10206 /* Implementation note: It this still necessary? */
10207 if (it.current_x != this_line_start_x)
10208 goto cancel;
10210 TRACE ((stderr, "trying display optimization 1\n"));
10211 w->cursor.vpos = -1;
10212 overlay_arrow_seen = 0;
10213 it.vpos = this_line_vpos;
10214 it.current_y = this_line_y;
10215 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10216 display_line (&it);
10218 /* If line contains point, is not continued,
10219 and ends at same distance from eob as before, we win */
10220 if (w->cursor.vpos >= 0
10221 /* Line is not continued, otherwise this_line_start_pos
10222 would have been set to 0 in display_line. */
10223 && CHARPOS (this_line_start_pos)
10224 /* Line ends as before. */
10225 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10226 /* Line has same height as before. Otherwise other lines
10227 would have to be shifted up or down. */
10228 && this_line_pixel_height == line_height_before)
10230 /* If this is not the window's last line, we must adjust
10231 the charstarts of the lines below. */
10232 if (it.current_y < it.last_visible_y)
10234 struct glyph_row *row
10235 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10236 int delta, delta_bytes;
10238 if (Z - CHARPOS (tlendpos) == ZV)
10240 /* This line ends at end of (accessible part of)
10241 buffer. There is no newline to count. */
10242 delta = (Z
10243 - CHARPOS (tlendpos)
10244 - MATRIX_ROW_START_CHARPOS (row));
10245 delta_bytes = (Z_BYTE
10246 - BYTEPOS (tlendpos)
10247 - MATRIX_ROW_START_BYTEPOS (row));
10249 else
10251 /* This line ends in a newline. Must take
10252 account of the newline and the rest of the
10253 text that follows. */
10254 delta = (Z
10255 - CHARPOS (tlendpos)
10256 - MATRIX_ROW_START_CHARPOS (row));
10257 delta_bytes = (Z_BYTE
10258 - BYTEPOS (tlendpos)
10259 - MATRIX_ROW_START_BYTEPOS (row));
10262 increment_matrix_positions (w->current_matrix,
10263 this_line_vpos + 1,
10264 w->current_matrix->nrows,
10265 delta, delta_bytes);
10268 /* If this row displays text now but previously didn't,
10269 or vice versa, w->window_end_vpos may have to be
10270 adjusted. */
10271 if ((it.glyph_row - 1)->displays_text_p)
10273 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10274 XSETINT (w->window_end_vpos, this_line_vpos);
10276 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10277 && this_line_vpos > 0)
10278 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10279 w->window_end_valid = Qnil;
10281 /* Update hint: No need to try to scroll in update_window. */
10282 w->desired_matrix->no_scrolling_p = 1;
10284 #if GLYPH_DEBUG
10285 *w->desired_matrix->method = 0;
10286 debug_method_add (w, "optimization 1");
10287 #endif
10288 #ifdef HAVE_WINDOW_SYSTEM
10289 update_window_fringes (w, 0);
10290 #endif
10291 goto update;
10293 else
10294 goto cancel;
10296 else if (/* Cursor position hasn't changed. */
10297 PT == XFASTINT (w->last_point)
10298 /* Make sure the cursor was last displayed
10299 in this window. Otherwise we have to reposition it. */
10300 && 0 <= w->cursor.vpos
10301 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10303 if (!must_finish)
10305 do_pending_window_change (1);
10307 /* We used to always goto end_of_redisplay here, but this
10308 isn't enough if we have a blinking cursor. */
10309 if (w->cursor_off_p == w->last_cursor_off_p)
10310 goto end_of_redisplay;
10312 goto update;
10314 /* If highlighting the region, or if the cursor is in the echo area,
10315 then we can't just move the cursor. */
10316 else if (! (!NILP (Vtransient_mark_mode)
10317 && !NILP (current_buffer->mark_active))
10318 && (EQ (selected_window, current_buffer->last_selected_window)
10319 || highlight_nonselected_windows)
10320 && NILP (w->region_showing)
10321 && NILP (Vshow_trailing_whitespace)
10322 && !cursor_in_echo_area)
10324 struct it it;
10325 struct glyph_row *row;
10327 /* Skip from tlbufpos to PT and see where it is. Note that
10328 PT may be in invisible text. If so, we will end at the
10329 next visible position. */
10330 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10331 NULL, DEFAULT_FACE_ID);
10332 it.current_x = this_line_start_x;
10333 it.current_y = this_line_y;
10334 it.vpos = this_line_vpos;
10336 /* The call to move_it_to stops in front of PT, but
10337 moves over before-strings. */
10338 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10340 if (it.vpos == this_line_vpos
10341 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10342 row->enabled_p))
10344 xassert (this_line_vpos == it.vpos);
10345 xassert (this_line_y == it.current_y);
10346 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10347 #if GLYPH_DEBUG
10348 *w->desired_matrix->method = 0;
10349 debug_method_add (w, "optimization 3");
10350 #endif
10351 goto update;
10353 else
10354 goto cancel;
10357 cancel:
10358 /* Text changed drastically or point moved off of line. */
10359 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10362 CHARPOS (this_line_start_pos) = 0;
10363 consider_all_windows_p |= buffer_shared > 1;
10364 ++clear_face_cache_count;
10367 /* Build desired matrices, and update the display. If
10368 consider_all_windows_p is non-zero, do it for all windows on all
10369 frames. Otherwise do it for selected_window, only. */
10371 if (consider_all_windows_p)
10373 Lisp_Object tail, frame;
10374 int i, n = 0, size = 50;
10375 struct frame **updated
10376 = (struct frame **) alloca (size * sizeof *updated);
10378 /* Clear the face cache eventually. */
10379 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10381 clear_face_cache (0);
10382 clear_face_cache_count = 0;
10385 /* Recompute # windows showing selected buffer. This will be
10386 incremented each time such a window is displayed. */
10387 buffer_shared = 0;
10389 FOR_EACH_FRAME (tail, frame)
10391 struct frame *f = XFRAME (frame);
10393 if (FRAME_WINDOW_P (f) || f == sf)
10395 if (! EQ (frame, selected_frame))
10396 /* Select the frame, for the sake of frame-local
10397 variables. */
10398 select_frame_for_redisplay (frame);
10400 #ifdef HAVE_WINDOW_SYSTEM
10401 if (clear_face_cache_count % 50 == 0
10402 && FRAME_WINDOW_P (f))
10403 clear_image_cache (f, 0);
10404 #endif /* HAVE_WINDOW_SYSTEM */
10406 /* Mark all the scroll bars to be removed; we'll redeem
10407 the ones we want when we redisplay their windows. */
10408 if (condemn_scroll_bars_hook)
10409 condemn_scroll_bars_hook (f);
10411 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10412 redisplay_windows (FRAME_ROOT_WINDOW (f));
10414 /* Any scroll bars which redisplay_windows should have
10415 nuked should now go away. */
10416 if (judge_scroll_bars_hook)
10417 judge_scroll_bars_hook (f);
10419 /* If fonts changed, display again. */
10420 /* ??? rms: I suspect it is a mistake to jump all the way
10421 back to retry here. It should just retry this frame. */
10422 if (fonts_changed_p)
10423 goto retry;
10425 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10427 /* See if we have to hscroll. */
10428 if (hscroll_windows (f->root_window))
10429 goto retry;
10431 /* Prevent various kinds of signals during display
10432 update. stdio is not robust about handling
10433 signals, which can cause an apparent I/O
10434 error. */
10435 if (interrupt_input)
10436 unrequest_sigio ();
10437 STOP_POLLING;
10439 /* Update the display. */
10440 set_window_update_flags (XWINDOW (f->root_window), 1);
10441 pause |= update_frame (f, 0, 0);
10442 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10443 if (pause)
10444 break;
10445 #endif
10447 if (n == size)
10449 int nbytes = size * sizeof *updated;
10450 struct frame **p = (struct frame **) alloca (2 * nbytes);
10451 bcopy (updated, p, nbytes);
10452 size *= 2;
10455 updated[n++] = f;
10460 if (!pause)
10462 /* Do the mark_window_display_accurate after all windows have
10463 been redisplayed because this call resets flags in buffers
10464 which are needed for proper redisplay. */
10465 for (i = 0; i < n; ++i)
10467 struct frame *f = updated[i];
10468 mark_window_display_accurate (f->root_window, 1);
10469 if (frame_up_to_date_hook)
10470 frame_up_to_date_hook (f);
10474 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10476 Lisp_Object mini_window;
10477 struct frame *mini_frame;
10479 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10480 /* Use list_of_error, not Qerror, so that
10481 we catch only errors and don't run the debugger. */
10482 internal_condition_case_1 (redisplay_window_1, selected_window,
10483 list_of_error,
10484 redisplay_window_error);
10486 /* Compare desired and current matrices, perform output. */
10488 update:
10489 /* If fonts changed, display again. */
10490 if (fonts_changed_p)
10491 goto retry;
10493 /* Prevent various kinds of signals during display update.
10494 stdio is not robust about handling signals,
10495 which can cause an apparent I/O error. */
10496 if (interrupt_input)
10497 unrequest_sigio ();
10498 STOP_POLLING;
10500 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10502 if (hscroll_windows (selected_window))
10503 goto retry;
10505 XWINDOW (selected_window)->must_be_updated_p = 1;
10506 pause = update_frame (sf, 0, 0);
10509 /* We may have called echo_area_display at the top of this
10510 function. If the echo area is on another frame, that may
10511 have put text on a frame other than the selected one, so the
10512 above call to update_frame would not have caught it. Catch
10513 it here. */
10514 mini_window = FRAME_MINIBUF_WINDOW (sf);
10515 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10517 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10519 XWINDOW (mini_window)->must_be_updated_p = 1;
10520 pause |= update_frame (mini_frame, 0, 0);
10521 if (!pause && hscroll_windows (mini_window))
10522 goto retry;
10526 /* If display was paused because of pending input, make sure we do a
10527 thorough update the next time. */
10528 if (pause)
10530 /* Prevent the optimization at the beginning of
10531 redisplay_internal that tries a single-line update of the
10532 line containing the cursor in the selected window. */
10533 CHARPOS (this_line_start_pos) = 0;
10535 /* Let the overlay arrow be updated the next time. */
10536 update_overlay_arrows (0);
10538 /* If we pause after scrolling, some rows in the current
10539 matrices of some windows are not valid. */
10540 if (!WINDOW_FULL_WIDTH_P (w)
10541 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10542 update_mode_lines = 1;
10544 else
10546 if (!consider_all_windows_p)
10548 /* This has already been done above if
10549 consider_all_windows_p is set. */
10550 mark_window_display_accurate_1 (w, 1);
10552 /* Say overlay arrows are up to date. */
10553 update_overlay_arrows (1);
10555 if (frame_up_to_date_hook != 0)
10556 frame_up_to_date_hook (sf);
10559 update_mode_lines = 0;
10560 windows_or_buffers_changed = 0;
10561 cursor_type_changed = 0;
10564 /* Start SIGIO interrupts coming again. Having them off during the
10565 code above makes it less likely one will discard output, but not
10566 impossible, since there might be stuff in the system buffer here.
10567 But it is much hairier to try to do anything about that. */
10568 if (interrupt_input)
10569 request_sigio ();
10570 RESUME_POLLING;
10572 /* If a frame has become visible which was not before, redisplay
10573 again, so that we display it. Expose events for such a frame
10574 (which it gets when becoming visible) don't call the parts of
10575 redisplay constructing glyphs, so simply exposing a frame won't
10576 display anything in this case. So, we have to display these
10577 frames here explicitly. */
10578 if (!pause)
10580 Lisp_Object tail, frame;
10581 int new_count = 0;
10583 FOR_EACH_FRAME (tail, frame)
10585 int this_is_visible = 0;
10587 if (XFRAME (frame)->visible)
10588 this_is_visible = 1;
10589 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10590 if (XFRAME (frame)->visible)
10591 this_is_visible = 1;
10593 if (this_is_visible)
10594 new_count++;
10597 if (new_count != number_of_visible_frames)
10598 windows_or_buffers_changed++;
10601 /* Change frame size now if a change is pending. */
10602 do_pending_window_change (1);
10604 /* If we just did a pending size change, or have additional
10605 visible frames, redisplay again. */
10606 if (windows_or_buffers_changed && !pause)
10607 goto retry;
10609 end_of_redisplay:
10610 unbind_to (count, Qnil);
10611 RESUME_POLLING;
10615 /* Redisplay, but leave alone any recent echo area message unless
10616 another message has been requested in its place.
10618 This is useful in situations where you need to redisplay but no
10619 user action has occurred, making it inappropriate for the message
10620 area to be cleared. See tracking_off and
10621 wait_reading_process_output for examples of these situations.
10623 FROM_WHERE is an integer saying from where this function was
10624 called. This is useful for debugging. */
10626 void
10627 redisplay_preserve_echo_area (from_where)
10628 int from_where;
10630 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10632 if (!NILP (echo_area_buffer[1]))
10634 /* We have a previously displayed message, but no current
10635 message. Redisplay the previous message. */
10636 display_last_displayed_message_p = 1;
10637 redisplay_internal (1);
10638 display_last_displayed_message_p = 0;
10640 else
10641 redisplay_internal (1);
10643 if (rif != NULL && rif->flush_display_optional)
10644 rif->flush_display_optional (NULL);
10648 /* Function registered with record_unwind_protect in
10649 redisplay_internal. Reset redisplaying_p to the value it had
10650 before redisplay_internal was called, and clear
10651 prevent_freeing_realized_faces_p. It also selects the previously
10652 selected frame. */
10654 static Lisp_Object
10655 unwind_redisplay (val)
10656 Lisp_Object val;
10658 Lisp_Object old_redisplaying_p, old_frame;
10660 old_redisplaying_p = XCAR (val);
10661 redisplaying_p = XFASTINT (old_redisplaying_p);
10662 old_frame = XCDR (val);
10663 if (! EQ (old_frame, selected_frame))
10664 select_frame_for_redisplay (old_frame);
10665 return Qnil;
10669 /* Mark the display of window W as accurate or inaccurate. If
10670 ACCURATE_P is non-zero mark display of W as accurate. If
10671 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10672 redisplay_internal is called. */
10674 static void
10675 mark_window_display_accurate_1 (w, accurate_p)
10676 struct window *w;
10677 int accurate_p;
10679 if (BUFFERP (w->buffer))
10681 struct buffer *b = XBUFFER (w->buffer);
10683 w->last_modified
10684 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10685 w->last_overlay_modified
10686 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10687 w->last_had_star
10688 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10690 if (accurate_p)
10692 b->clip_changed = 0;
10693 b->prevent_redisplay_optimizations_p = 0;
10695 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10696 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10697 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10698 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10700 w->current_matrix->buffer = b;
10701 w->current_matrix->begv = BUF_BEGV (b);
10702 w->current_matrix->zv = BUF_ZV (b);
10704 w->last_cursor = w->cursor;
10705 w->last_cursor_off_p = w->cursor_off_p;
10707 if (w == XWINDOW (selected_window))
10708 w->last_point = make_number (BUF_PT (b));
10709 else
10710 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10714 if (accurate_p)
10716 w->window_end_valid = w->buffer;
10717 #if 0 /* This is incorrect with variable-height lines. */
10718 xassert (XINT (w->window_end_vpos)
10719 < (WINDOW_TOTAL_LINES (w)
10720 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10721 #endif
10722 w->update_mode_line = Qnil;
10727 /* Mark the display of windows in the window tree rooted at WINDOW as
10728 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10729 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10730 be redisplayed the next time redisplay_internal is called. */
10732 void
10733 mark_window_display_accurate (window, accurate_p)
10734 Lisp_Object window;
10735 int accurate_p;
10737 struct window *w;
10739 for (; !NILP (window); window = w->next)
10741 w = XWINDOW (window);
10742 mark_window_display_accurate_1 (w, accurate_p);
10744 if (!NILP (w->vchild))
10745 mark_window_display_accurate (w->vchild, accurate_p);
10746 if (!NILP (w->hchild))
10747 mark_window_display_accurate (w->hchild, accurate_p);
10750 if (accurate_p)
10752 update_overlay_arrows (1);
10754 else
10756 /* Force a thorough redisplay the next time by setting
10757 last_arrow_position and last_arrow_string to t, which is
10758 unequal to any useful value of Voverlay_arrow_... */
10759 update_overlay_arrows (-1);
10764 /* Return value in display table DP (Lisp_Char_Table *) for character
10765 C. Since a display table doesn't have any parent, we don't have to
10766 follow parent. Do not call this function directly but use the
10767 macro DISP_CHAR_VECTOR. */
10769 Lisp_Object
10770 disp_char_vector (dp, c)
10771 struct Lisp_Char_Table *dp;
10772 int c;
10774 int code[4], i;
10775 Lisp_Object val;
10777 if (SINGLE_BYTE_CHAR_P (c))
10778 return (dp->contents[c]);
10780 SPLIT_CHAR (c, code[0], code[1], code[2]);
10781 if (code[1] < 32)
10782 code[1] = -1;
10783 else if (code[2] < 32)
10784 code[2] = -1;
10786 /* Here, the possible range of code[0] (== charset ID) is
10787 128..max_charset. Since the top level char table contains data
10788 for multibyte characters after 256th element, we must increment
10789 code[0] by 128 to get a correct index. */
10790 code[0] += 128;
10791 code[3] = -1; /* anchor */
10793 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10795 val = dp->contents[code[i]];
10796 if (!SUB_CHAR_TABLE_P (val))
10797 return (NILP (val) ? dp->defalt : val);
10800 /* Here, val is a sub char table. We return the default value of
10801 it. */
10802 return (dp->defalt);
10807 /***********************************************************************
10808 Window Redisplay
10809 ***********************************************************************/
10811 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10813 static void
10814 redisplay_windows (window)
10815 Lisp_Object window;
10817 while (!NILP (window))
10819 struct window *w = XWINDOW (window);
10821 if (!NILP (w->hchild))
10822 redisplay_windows (w->hchild);
10823 else if (!NILP (w->vchild))
10824 redisplay_windows (w->vchild);
10825 else
10827 displayed_buffer = XBUFFER (w->buffer);
10828 /* Use list_of_error, not Qerror, so that
10829 we catch only errors and don't run the debugger. */
10830 internal_condition_case_1 (redisplay_window_0, window,
10831 list_of_error,
10832 redisplay_window_error);
10835 window = w->next;
10839 static Lisp_Object
10840 redisplay_window_error ()
10842 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10843 return Qnil;
10846 static Lisp_Object
10847 redisplay_window_0 (window)
10848 Lisp_Object window;
10850 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10851 redisplay_window (window, 0);
10852 return Qnil;
10855 static Lisp_Object
10856 redisplay_window_1 (window)
10857 Lisp_Object window;
10859 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10860 redisplay_window (window, 1);
10861 return Qnil;
10865 /* Increment GLYPH until it reaches END or CONDITION fails while
10866 adding (GLYPH)->pixel_width to X. */
10868 #define SKIP_GLYPHS(glyph, end, x, condition) \
10869 do \
10871 (x) += (glyph)->pixel_width; \
10872 ++(glyph); \
10874 while ((glyph) < (end) && (condition))
10877 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10878 DELTA is the number of bytes by which positions recorded in ROW
10879 differ from current buffer positions. */
10881 void
10882 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10883 struct window *w;
10884 struct glyph_row *row;
10885 struct glyph_matrix *matrix;
10886 int delta, delta_bytes, dy, dvpos;
10888 struct glyph *glyph = row->glyphs[TEXT_AREA];
10889 struct glyph *end = glyph + row->used[TEXT_AREA];
10890 struct glyph *cursor = NULL;
10891 /* The first glyph that starts a sequence of glyphs from string. */
10892 struct glyph *string_start;
10893 /* The X coordinate of string_start. */
10894 int string_start_x;
10895 /* The last known character position. */
10896 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10897 /* The last known character position before string_start. */
10898 int string_before_pos;
10899 int x = row->x;
10900 int cursor_x = x;
10901 int cursor_from_overlay_pos = 0;
10902 int pt_old = PT - delta;
10904 /* Skip over glyphs not having an object at the start of the row.
10905 These are special glyphs like truncation marks on terminal
10906 frames. */
10907 if (row->displays_text_p)
10908 while (glyph < end
10909 && INTEGERP (glyph->object)
10910 && glyph->charpos < 0)
10912 x += glyph->pixel_width;
10913 ++glyph;
10916 string_start = NULL;
10917 while (glyph < end
10918 && !INTEGERP (glyph->object)
10919 && (!BUFFERP (glyph->object)
10920 || (last_pos = glyph->charpos) < pt_old))
10922 if (! STRINGP (glyph->object))
10924 string_start = NULL;
10925 x += glyph->pixel_width;
10926 ++glyph;
10927 if (cursor_from_overlay_pos
10928 && last_pos > cursor_from_overlay_pos)
10930 cursor_from_overlay_pos = 0;
10931 cursor = 0;
10934 else
10936 string_before_pos = last_pos;
10937 string_start = glyph;
10938 string_start_x = x;
10939 /* Skip all glyphs from string. */
10942 int pos;
10943 if ((cursor == NULL || glyph > cursor)
10944 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10945 Qcursor, (glyph)->object))
10946 && (pos = string_buffer_position (w, glyph->object,
10947 string_before_pos),
10948 (pos == 0 /* From overlay */
10949 || pos == pt_old)))
10951 /* Estimate overlay buffer position from the buffer
10952 positions of the glyphs before and after the overlay.
10953 Add 1 to last_pos so that if point corresponds to the
10954 glyph right after the overlay, we still use a 'cursor'
10955 property found in that overlay. */
10956 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10957 cursor = glyph;
10958 cursor_x = x;
10960 x += glyph->pixel_width;
10961 ++glyph;
10963 while (glyph < end && STRINGP (glyph->object));
10967 if (cursor != NULL)
10969 glyph = cursor;
10970 x = cursor_x;
10972 else if (row->ends_in_ellipsis_p && glyph == end)
10974 /* Scan back over the ellipsis glyphs, decrementing positions. */
10975 while (glyph > row->glyphs[TEXT_AREA]
10976 && (glyph - 1)->charpos == last_pos)
10977 glyph--, x -= glyph->pixel_width;
10978 /* That loop always goes one position too far,
10979 including the glyph before the ellipsis.
10980 So scan forward over that one. */
10981 x += glyph->pixel_width;
10982 glyph++;
10984 else if (string_start
10985 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10987 /* We may have skipped over point because the previous glyphs
10988 are from string. As there's no easy way to know the
10989 character position of the current glyph, find the correct
10990 glyph on point by scanning from string_start again. */
10991 Lisp_Object limit;
10992 Lisp_Object string;
10993 int pos;
10995 limit = make_number (pt_old + 1);
10996 end = glyph;
10997 glyph = string_start;
10998 x = string_start_x;
10999 string = glyph->object;
11000 pos = string_buffer_position (w, string, string_before_pos);
11001 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11002 because we always put cursor after overlay strings. */
11003 while (pos == 0 && glyph < end)
11005 string = glyph->object;
11006 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11007 if (glyph < end)
11008 pos = string_buffer_position (w, glyph->object, string_before_pos);
11011 while (glyph < end)
11013 pos = XINT (Fnext_single_char_property_change
11014 (make_number (pos), Qdisplay, Qnil, limit));
11015 if (pos > pt_old)
11016 break;
11017 /* Skip glyphs from the same string. */
11018 string = glyph->object;
11019 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11020 /* Skip glyphs from an overlay. */
11021 while (glyph < end
11022 && ! string_buffer_position (w, glyph->object, pos))
11024 string = glyph->object;
11025 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11030 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11031 w->cursor.x = x;
11032 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11033 w->cursor.y = row->y + dy;
11035 if (w == XWINDOW (selected_window))
11037 if (!row->continued_p
11038 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11039 && row->x == 0)
11041 this_line_buffer = XBUFFER (w->buffer);
11043 CHARPOS (this_line_start_pos)
11044 = MATRIX_ROW_START_CHARPOS (row) + delta;
11045 BYTEPOS (this_line_start_pos)
11046 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11048 CHARPOS (this_line_end_pos)
11049 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11050 BYTEPOS (this_line_end_pos)
11051 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11053 this_line_y = w->cursor.y;
11054 this_line_pixel_height = row->height;
11055 this_line_vpos = w->cursor.vpos;
11056 this_line_start_x = row->x;
11058 else
11059 CHARPOS (this_line_start_pos) = 0;
11064 /* Run window scroll functions, if any, for WINDOW with new window
11065 start STARTP. Sets the window start of WINDOW to that position.
11067 We assume that the window's buffer is really current. */
11069 static INLINE struct text_pos
11070 run_window_scroll_functions (window, startp)
11071 Lisp_Object window;
11072 struct text_pos startp;
11074 struct window *w = XWINDOW (window);
11075 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11077 if (current_buffer != XBUFFER (w->buffer))
11078 abort ();
11080 if (!NILP (Vwindow_scroll_functions))
11082 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11083 make_number (CHARPOS (startp)));
11084 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11085 /* In case the hook functions switch buffers. */
11086 if (current_buffer != XBUFFER (w->buffer))
11087 set_buffer_internal_1 (XBUFFER (w->buffer));
11090 return startp;
11094 /* Make sure the line containing the cursor is fully visible.
11095 A value of 1 means there is nothing to be done.
11096 (Either the line is fully visible, or it cannot be made so,
11097 or we cannot tell.)
11099 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11100 is higher than window.
11102 A value of 0 means the caller should do scrolling
11103 as if point had gone off the screen. */
11105 static int
11106 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11107 struct window *w;
11108 int force_p;
11110 struct glyph_matrix *matrix;
11111 struct glyph_row *row;
11112 int window_height;
11114 if (!make_cursor_line_fully_visible_p)
11115 return 1;
11117 /* It's not always possible to find the cursor, e.g, when a window
11118 is full of overlay strings. Don't do anything in that case. */
11119 if (w->cursor.vpos < 0)
11120 return 1;
11122 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11123 row = MATRIX_ROW (matrix, w->cursor.vpos);
11125 /* If the cursor row is not partially visible, there's nothing to do. */
11126 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11127 return 1;
11129 /* If the row the cursor is in is taller than the window's height,
11130 it's not clear what to do, so do nothing. */
11131 window_height = window_box_height (w);
11132 if (row->height >= window_height)
11134 if (!force_p || w->vscroll)
11135 return 1;
11137 return 0;
11139 #if 0
11140 /* This code used to try to scroll the window just enough to make
11141 the line visible. It returned 0 to say that the caller should
11142 allocate larger glyph matrices. */
11144 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11146 int dy = row->height - row->visible_height;
11147 w->vscroll = 0;
11148 w->cursor.y += dy;
11149 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11151 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11153 int dy = - (row->height - row->visible_height);
11154 w->vscroll = dy;
11155 w->cursor.y += dy;
11156 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11159 /* When we change the cursor y-position of the selected window,
11160 change this_line_y as well so that the display optimization for
11161 the cursor line of the selected window in redisplay_internal uses
11162 the correct y-position. */
11163 if (w == XWINDOW (selected_window))
11164 this_line_y = w->cursor.y;
11166 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11167 redisplay with larger matrices. */
11168 if (matrix->nrows < required_matrix_height (w))
11170 fonts_changed_p = 1;
11171 return 0;
11174 return 1;
11175 #endif /* 0 */
11179 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11180 non-zero means only WINDOW is redisplayed in redisplay_internal.
11181 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11182 in redisplay_window to bring a partially visible line into view in
11183 the case that only the cursor has moved.
11185 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11186 last screen line's vertical height extends past the end of the screen.
11188 Value is
11190 1 if scrolling succeeded
11192 0 if scrolling didn't find point.
11194 -1 if new fonts have been loaded so that we must interrupt
11195 redisplay, adjust glyph matrices, and try again. */
11197 enum
11199 SCROLLING_SUCCESS,
11200 SCROLLING_FAILED,
11201 SCROLLING_NEED_LARGER_MATRICES
11204 static int
11205 try_scrolling (window, just_this_one_p, scroll_conservatively,
11206 scroll_step, temp_scroll_step, last_line_misfit)
11207 Lisp_Object window;
11208 int just_this_one_p;
11209 EMACS_INT scroll_conservatively, scroll_step;
11210 int temp_scroll_step;
11211 int last_line_misfit;
11213 struct window *w = XWINDOW (window);
11214 struct frame *f = XFRAME (w->frame);
11215 struct text_pos scroll_margin_pos;
11216 struct text_pos pos;
11217 struct text_pos startp;
11218 struct it it;
11219 Lisp_Object window_end;
11220 int this_scroll_margin;
11221 int dy = 0;
11222 int scroll_max;
11223 int rc;
11224 int amount_to_scroll = 0;
11225 Lisp_Object aggressive;
11226 int height;
11227 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11229 #if GLYPH_DEBUG
11230 debug_method_add (w, "try_scrolling");
11231 #endif
11233 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11235 /* Compute scroll margin height in pixels. We scroll when point is
11236 within this distance from the top or bottom of the window. */
11237 if (scroll_margin > 0)
11239 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11240 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11242 else
11243 this_scroll_margin = 0;
11245 /* Force scroll_conservatively to have a reasonable value so it doesn't
11246 cause an overflow while computing how much to scroll. */
11247 if (scroll_conservatively)
11248 scroll_conservatively = min (scroll_conservatively,
11249 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11251 /* Compute how much we should try to scroll maximally to bring point
11252 into view. */
11253 if (scroll_step || scroll_conservatively || temp_scroll_step)
11254 scroll_max = max (scroll_step,
11255 max (scroll_conservatively, temp_scroll_step));
11256 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11257 || NUMBERP (current_buffer->scroll_up_aggressively))
11258 /* We're trying to scroll because of aggressive scrolling
11259 but no scroll_step is set. Choose an arbitrary one. Maybe
11260 there should be a variable for this. */
11261 scroll_max = 10;
11262 else
11263 scroll_max = 0;
11264 scroll_max *= FRAME_LINE_HEIGHT (f);
11266 /* Decide whether we have to scroll down. Start at the window end
11267 and move this_scroll_margin up to find the position of the scroll
11268 margin. */
11269 window_end = Fwindow_end (window, Qt);
11271 too_near_end:
11273 CHARPOS (scroll_margin_pos) = XINT (window_end);
11274 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11276 if (this_scroll_margin || extra_scroll_margin_lines)
11278 start_display (&it, w, scroll_margin_pos);
11279 if (this_scroll_margin)
11280 move_it_vertically_backward (&it, this_scroll_margin);
11281 if (extra_scroll_margin_lines)
11282 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11283 scroll_margin_pos = it.current.pos;
11286 if (PT >= CHARPOS (scroll_margin_pos))
11288 int y0;
11290 /* Point is in the scroll margin at the bottom of the window, or
11291 below. Compute a new window start that makes point visible. */
11293 /* Compute the distance from the scroll margin to PT.
11294 Give up if the distance is greater than scroll_max. */
11295 start_display (&it, w, scroll_margin_pos);
11296 y0 = it.current_y;
11297 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11298 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11300 /* To make point visible, we have to move the window start
11301 down so that the line the cursor is in is visible, which
11302 means we have to add in the height of the cursor line. */
11303 dy = line_bottom_y (&it) - y0;
11305 if (dy > scroll_max)
11306 return SCROLLING_FAILED;
11308 /* Move the window start down. If scrolling conservatively,
11309 move it just enough down to make point visible. If
11310 scroll_step is set, move it down by scroll_step. */
11311 start_display (&it, w, startp);
11313 if (scroll_conservatively)
11314 /* Set AMOUNT_TO_SCROLL to at least one line,
11315 and at most scroll_conservatively lines. */
11316 amount_to_scroll
11317 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11318 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11319 else if (scroll_step || temp_scroll_step)
11320 amount_to_scroll = scroll_max;
11321 else
11323 aggressive = current_buffer->scroll_up_aggressively;
11324 height = WINDOW_BOX_TEXT_HEIGHT (w);
11325 if (NUMBERP (aggressive))
11327 double float_amount = XFLOATINT (aggressive) * height;
11328 amount_to_scroll = float_amount;
11329 if (amount_to_scroll == 0 && float_amount > 0)
11330 amount_to_scroll = 1;
11334 if (amount_to_scroll <= 0)
11335 return SCROLLING_FAILED;
11337 /* If moving by amount_to_scroll leaves STARTP unchanged,
11338 move it down one screen line. */
11340 move_it_vertically (&it, amount_to_scroll);
11341 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11342 move_it_by_lines (&it, 1, 1);
11343 startp = it.current.pos;
11345 else
11347 /* See if point is inside the scroll margin at the top of the
11348 window. */
11349 scroll_margin_pos = startp;
11350 if (this_scroll_margin)
11352 start_display (&it, w, startp);
11353 move_it_vertically (&it, this_scroll_margin);
11354 scroll_margin_pos = it.current.pos;
11357 if (PT < CHARPOS (scroll_margin_pos))
11359 /* Point is in the scroll margin at the top of the window or
11360 above what is displayed in the window. */
11361 int y0;
11363 /* Compute the vertical distance from PT to the scroll
11364 margin position. Give up if distance is greater than
11365 scroll_max. */
11366 SET_TEXT_POS (pos, PT, PT_BYTE);
11367 start_display (&it, w, pos);
11368 y0 = it.current_y;
11369 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11370 it.last_visible_y, -1,
11371 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11372 dy = it.current_y - y0;
11373 if (dy > scroll_max)
11374 return SCROLLING_FAILED;
11376 /* Compute new window start. */
11377 start_display (&it, w, startp);
11379 if (scroll_conservatively)
11380 amount_to_scroll
11381 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11382 else if (scroll_step || temp_scroll_step)
11383 amount_to_scroll = scroll_max;
11384 else
11386 aggressive = current_buffer->scroll_down_aggressively;
11387 height = WINDOW_BOX_TEXT_HEIGHT (w);
11388 if (NUMBERP (aggressive))
11390 double float_amount = XFLOATINT (aggressive) * height;
11391 amount_to_scroll = float_amount;
11392 if (amount_to_scroll == 0 && float_amount > 0)
11393 amount_to_scroll = 1;
11397 if (amount_to_scroll <= 0)
11398 return SCROLLING_FAILED;
11400 move_it_vertically_backward (&it, amount_to_scroll);
11401 startp = it.current.pos;
11405 /* Run window scroll functions. */
11406 startp = run_window_scroll_functions (window, startp);
11408 /* Display the window. Give up if new fonts are loaded, or if point
11409 doesn't appear. */
11410 if (!try_window (window, startp))
11411 rc = SCROLLING_NEED_LARGER_MATRICES;
11412 else if (w->cursor.vpos < 0)
11414 clear_glyph_matrix (w->desired_matrix);
11415 rc = SCROLLING_FAILED;
11417 else
11419 /* Maybe forget recorded base line for line number display. */
11420 if (!just_this_one_p
11421 || current_buffer->clip_changed
11422 || BEG_UNCHANGED < CHARPOS (startp))
11423 w->base_line_number = Qnil;
11425 /* If cursor ends up on a partially visible line,
11426 treat that as being off the bottom of the screen. */
11427 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11429 clear_glyph_matrix (w->desired_matrix);
11430 ++extra_scroll_margin_lines;
11431 goto too_near_end;
11433 rc = SCROLLING_SUCCESS;
11436 return rc;
11440 /* Compute a suitable window start for window W if display of W starts
11441 on a continuation line. Value is non-zero if a new window start
11442 was computed.
11444 The new window start will be computed, based on W's width, starting
11445 from the start of the continued line. It is the start of the
11446 screen line with the minimum distance from the old start W->start. */
11448 static int
11449 compute_window_start_on_continuation_line (w)
11450 struct window *w;
11452 struct text_pos pos, start_pos;
11453 int window_start_changed_p = 0;
11455 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11457 /* If window start is on a continuation line... Window start may be
11458 < BEGV in case there's invisible text at the start of the
11459 buffer (M-x rmail, for example). */
11460 if (CHARPOS (start_pos) > BEGV
11461 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11463 struct it it;
11464 struct glyph_row *row;
11466 /* Handle the case that the window start is out of range. */
11467 if (CHARPOS (start_pos) < BEGV)
11468 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11469 else if (CHARPOS (start_pos) > ZV)
11470 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11472 /* Find the start of the continued line. This should be fast
11473 because scan_buffer is fast (newline cache). */
11474 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11475 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11476 row, DEFAULT_FACE_ID);
11477 reseat_at_previous_visible_line_start (&it);
11479 /* If the line start is "too far" away from the window start,
11480 say it takes too much time to compute a new window start. */
11481 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11482 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11484 int min_distance, distance;
11486 /* Move forward by display lines to find the new window
11487 start. If window width was enlarged, the new start can
11488 be expected to be > the old start. If window width was
11489 decreased, the new window start will be < the old start.
11490 So, we're looking for the display line start with the
11491 minimum distance from the old window start. */
11492 pos = it.current.pos;
11493 min_distance = INFINITY;
11494 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11495 distance < min_distance)
11497 min_distance = distance;
11498 pos = it.current.pos;
11499 move_it_by_lines (&it, 1, 0);
11502 /* Set the window start there. */
11503 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11504 window_start_changed_p = 1;
11508 return window_start_changed_p;
11512 /* Try cursor movement in case text has not changed in window WINDOW,
11513 with window start STARTP. Value is
11515 CURSOR_MOVEMENT_SUCCESS if successful
11517 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11519 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11520 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11521 we want to scroll as if scroll-step were set to 1. See the code.
11523 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11524 which case we have to abort this redisplay, and adjust matrices
11525 first. */
11527 enum
11529 CURSOR_MOVEMENT_SUCCESS,
11530 CURSOR_MOVEMENT_CANNOT_BE_USED,
11531 CURSOR_MOVEMENT_MUST_SCROLL,
11532 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11535 static int
11536 try_cursor_movement (window, startp, scroll_step)
11537 Lisp_Object window;
11538 struct text_pos startp;
11539 int *scroll_step;
11541 struct window *w = XWINDOW (window);
11542 struct frame *f = XFRAME (w->frame);
11543 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11545 #if GLYPH_DEBUG
11546 if (inhibit_try_cursor_movement)
11547 return rc;
11548 #endif
11550 /* Handle case where text has not changed, only point, and it has
11551 not moved off the frame. */
11552 if (/* Point may be in this window. */
11553 PT >= CHARPOS (startp)
11554 /* Selective display hasn't changed. */
11555 && !current_buffer->clip_changed
11556 /* Function force-mode-line-update is used to force a thorough
11557 redisplay. It sets either windows_or_buffers_changed or
11558 update_mode_lines. So don't take a shortcut here for these
11559 cases. */
11560 && !update_mode_lines
11561 && !windows_or_buffers_changed
11562 && !cursor_type_changed
11563 /* Can't use this case if highlighting a region. When a
11564 region exists, cursor movement has to do more than just
11565 set the cursor. */
11566 && !(!NILP (Vtransient_mark_mode)
11567 && !NILP (current_buffer->mark_active))
11568 && NILP (w->region_showing)
11569 && NILP (Vshow_trailing_whitespace)
11570 /* Right after splitting windows, last_point may be nil. */
11571 && INTEGERP (w->last_point)
11572 /* This code is not used for mini-buffer for the sake of the case
11573 of redisplaying to replace an echo area message; since in
11574 that case the mini-buffer contents per se are usually
11575 unchanged. This code is of no real use in the mini-buffer
11576 since the handling of this_line_start_pos, etc., in redisplay
11577 handles the same cases. */
11578 && !EQ (window, minibuf_window)
11579 /* When splitting windows or for new windows, it happens that
11580 redisplay is called with a nil window_end_vpos or one being
11581 larger than the window. This should really be fixed in
11582 window.c. I don't have this on my list, now, so we do
11583 approximately the same as the old redisplay code. --gerd. */
11584 && INTEGERP (w->window_end_vpos)
11585 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11586 && (FRAME_WINDOW_P (f)
11587 || !overlay_arrow_in_current_buffer_p ()))
11589 int this_scroll_margin, top_scroll_margin;
11590 struct glyph_row *row = NULL;
11592 #if GLYPH_DEBUG
11593 debug_method_add (w, "cursor movement");
11594 #endif
11596 /* Scroll if point within this distance from the top or bottom
11597 of the window. This is a pixel value. */
11598 this_scroll_margin = max (0, scroll_margin);
11599 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11600 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11602 top_scroll_margin = this_scroll_margin;
11603 if (WINDOW_WANTS_HEADER_LINE_P (w))
11604 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11606 /* Start with the row the cursor was displayed during the last
11607 not paused redisplay. Give up if that row is not valid. */
11608 if (w->last_cursor.vpos < 0
11609 || w->last_cursor.vpos >= w->current_matrix->nrows)
11610 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11611 else
11613 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11614 if (row->mode_line_p)
11615 ++row;
11616 if (!row->enabled_p)
11617 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11620 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11622 int scroll_p = 0;
11623 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11625 if (PT > XFASTINT (w->last_point))
11627 /* Point has moved forward. */
11628 while (MATRIX_ROW_END_CHARPOS (row) < PT
11629 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11631 xassert (row->enabled_p);
11632 ++row;
11635 /* The end position of a row equals the start position
11636 of the next row. If PT is there, we would rather
11637 display it in the next line. */
11638 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11639 && MATRIX_ROW_END_CHARPOS (row) == PT
11640 && !cursor_row_p (w, row))
11641 ++row;
11643 /* If within the scroll margin, scroll. Note that
11644 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11645 the next line would be drawn, and that
11646 this_scroll_margin can be zero. */
11647 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11648 || PT > MATRIX_ROW_END_CHARPOS (row)
11649 /* Line is completely visible last line in window
11650 and PT is to be set in the next line. */
11651 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11652 && PT == MATRIX_ROW_END_CHARPOS (row)
11653 && !row->ends_at_zv_p
11654 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11655 scroll_p = 1;
11657 else if (PT < XFASTINT (w->last_point))
11659 /* Cursor has to be moved backward. Note that PT >=
11660 CHARPOS (startp) because of the outer if-statement. */
11661 while (!row->mode_line_p
11662 && (MATRIX_ROW_START_CHARPOS (row) > PT
11663 || (MATRIX_ROW_START_CHARPOS (row) == PT
11664 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11665 && (row->y > top_scroll_margin
11666 || CHARPOS (startp) == BEGV))
11668 xassert (row->enabled_p);
11669 --row;
11672 /* Consider the following case: Window starts at BEGV,
11673 there is invisible, intangible text at BEGV, so that
11674 display starts at some point START > BEGV. It can
11675 happen that we are called with PT somewhere between
11676 BEGV and START. Try to handle that case. */
11677 if (row < w->current_matrix->rows
11678 || row->mode_line_p)
11680 row = w->current_matrix->rows;
11681 if (row->mode_line_p)
11682 ++row;
11685 /* Due to newlines in overlay strings, we may have to
11686 skip forward over overlay strings. */
11687 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11688 && MATRIX_ROW_END_CHARPOS (row) == PT
11689 && !cursor_row_p (w, row))
11690 ++row;
11692 /* If within the scroll margin, scroll. */
11693 if (row->y < top_scroll_margin
11694 && CHARPOS (startp) != BEGV)
11695 scroll_p = 1;
11697 else
11699 /* Cursor did not move. So don't scroll even if cursor line
11700 is partially visible, as it was so before. */
11701 rc = CURSOR_MOVEMENT_SUCCESS;
11704 if (PT < MATRIX_ROW_START_CHARPOS (row)
11705 || PT > MATRIX_ROW_END_CHARPOS (row))
11707 /* if PT is not in the glyph row, give up. */
11708 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11710 else if (rc != CURSOR_MOVEMENT_SUCCESS
11711 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11712 && make_cursor_line_fully_visible_p)
11714 if (PT == MATRIX_ROW_END_CHARPOS (row)
11715 && !row->ends_at_zv_p
11716 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11717 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11718 else if (row->height > window_box_height (w))
11720 /* If we end up in a partially visible line, let's
11721 make it fully visible, except when it's taller
11722 than the window, in which case we can't do much
11723 about it. */
11724 *scroll_step = 1;
11725 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11727 else
11729 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11730 if (!cursor_row_fully_visible_p (w, 0, 1))
11731 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11732 else
11733 rc = CURSOR_MOVEMENT_SUCCESS;
11736 else if (scroll_p)
11737 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11738 else
11740 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11741 rc = CURSOR_MOVEMENT_SUCCESS;
11746 return rc;
11749 void
11750 set_vertical_scroll_bar (w)
11751 struct window *w;
11753 int start, end, whole;
11755 /* Calculate the start and end positions for the current window.
11756 At some point, it would be nice to choose between scrollbars
11757 which reflect the whole buffer size, with special markers
11758 indicating narrowing, and scrollbars which reflect only the
11759 visible region.
11761 Note that mini-buffers sometimes aren't displaying any text. */
11762 if (!MINI_WINDOW_P (w)
11763 || (w == XWINDOW (minibuf_window)
11764 && NILP (echo_area_buffer[0])))
11766 struct buffer *buf = XBUFFER (w->buffer);
11767 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11768 start = marker_position (w->start) - BUF_BEGV (buf);
11769 /* I don't think this is guaranteed to be right. For the
11770 moment, we'll pretend it is. */
11771 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11773 if (end < start)
11774 end = start;
11775 if (whole < (end - start))
11776 whole = end - start;
11778 else
11779 start = end = whole = 0;
11781 /* Indicate what this scroll bar ought to be displaying now. */
11782 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11786 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11787 selected_window is redisplayed.
11789 We can return without actually redisplaying the window if
11790 fonts_changed_p is nonzero. In that case, redisplay_internal will
11791 retry. */
11793 static void
11794 redisplay_window (window, just_this_one_p)
11795 Lisp_Object window;
11796 int just_this_one_p;
11798 struct window *w = XWINDOW (window);
11799 struct frame *f = XFRAME (w->frame);
11800 struct buffer *buffer = XBUFFER (w->buffer);
11801 struct buffer *old = current_buffer;
11802 struct text_pos lpoint, opoint, startp;
11803 int update_mode_line;
11804 int tem;
11805 struct it it;
11806 /* Record it now because it's overwritten. */
11807 int current_matrix_up_to_date_p = 0;
11808 int used_current_matrix_p = 0;
11809 /* This is less strict than current_matrix_up_to_date_p.
11810 It indictes that the buffer contents and narrowing are unchanged. */
11811 int buffer_unchanged_p = 0;
11812 int temp_scroll_step = 0;
11813 int count = SPECPDL_INDEX ();
11814 int rc;
11815 int centering_position = -1;
11816 int last_line_misfit = 0;
11818 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11819 opoint = lpoint;
11821 /* W must be a leaf window here. */
11822 xassert (!NILP (w->buffer));
11823 #if GLYPH_DEBUG
11824 *w->desired_matrix->method = 0;
11825 #endif
11827 specbind (Qinhibit_point_motion_hooks, Qt);
11829 reconsider_clip_changes (w, buffer);
11831 /* Has the mode line to be updated? */
11832 update_mode_line = (!NILP (w->update_mode_line)
11833 || update_mode_lines
11834 || buffer->clip_changed
11835 || buffer->prevent_redisplay_optimizations_p);
11837 if (MINI_WINDOW_P (w))
11839 if (w == XWINDOW (echo_area_window)
11840 && !NILP (echo_area_buffer[0]))
11842 if (update_mode_line)
11843 /* We may have to update a tty frame's menu bar or a
11844 tool-bar. Example `M-x C-h C-h C-g'. */
11845 goto finish_menu_bars;
11846 else
11847 /* We've already displayed the echo area glyphs in this window. */
11848 goto finish_scroll_bars;
11850 else if ((w != XWINDOW (minibuf_window)
11851 || minibuf_level == 0)
11852 /* When buffer is nonempty, redisplay window normally. */
11853 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11854 /* Quail displays non-mini buffers in minibuffer window.
11855 In that case, redisplay the window normally. */
11856 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11858 /* W is a mini-buffer window, but it's not active, so clear
11859 it. */
11860 int yb = window_text_bottom_y (w);
11861 struct glyph_row *row;
11862 int y;
11864 for (y = 0, row = w->desired_matrix->rows;
11865 y < yb;
11866 y += row->height, ++row)
11867 blank_row (w, row, y);
11868 goto finish_scroll_bars;
11871 clear_glyph_matrix (w->desired_matrix);
11874 /* Otherwise set up data on this window; select its buffer and point
11875 value. */
11876 /* Really select the buffer, for the sake of buffer-local
11877 variables. */
11878 set_buffer_internal_1 (XBUFFER (w->buffer));
11879 SET_TEXT_POS (opoint, PT, PT_BYTE);
11881 current_matrix_up_to_date_p
11882 = (!NILP (w->window_end_valid)
11883 && !current_buffer->clip_changed
11884 && !current_buffer->prevent_redisplay_optimizations_p
11885 && XFASTINT (w->last_modified) >= MODIFF
11886 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11888 buffer_unchanged_p
11889 = (!NILP (w->window_end_valid)
11890 && !current_buffer->clip_changed
11891 && XFASTINT (w->last_modified) >= MODIFF
11892 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11894 /* When windows_or_buffers_changed is non-zero, we can't rely on
11895 the window end being valid, so set it to nil there. */
11896 if (windows_or_buffers_changed)
11898 /* If window starts on a continuation line, maybe adjust the
11899 window start in case the window's width changed. */
11900 if (XMARKER (w->start)->buffer == current_buffer)
11901 compute_window_start_on_continuation_line (w);
11903 w->window_end_valid = Qnil;
11906 /* Some sanity checks. */
11907 CHECK_WINDOW_END (w);
11908 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11909 abort ();
11910 if (BYTEPOS (opoint) < CHARPOS (opoint))
11911 abort ();
11913 /* If %c is in mode line, update it if needed. */
11914 if (!NILP (w->column_number_displayed)
11915 /* This alternative quickly identifies a common case
11916 where no change is needed. */
11917 && !(PT == XFASTINT (w->last_point)
11918 && XFASTINT (w->last_modified) >= MODIFF
11919 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11920 && (XFASTINT (w->column_number_displayed)
11921 != (int) current_column ())) /* iftc */
11922 update_mode_line = 1;
11924 /* Count number of windows showing the selected buffer. An indirect
11925 buffer counts as its base buffer. */
11926 if (!just_this_one_p)
11928 struct buffer *current_base, *window_base;
11929 current_base = current_buffer;
11930 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11931 if (current_base->base_buffer)
11932 current_base = current_base->base_buffer;
11933 if (window_base->base_buffer)
11934 window_base = window_base->base_buffer;
11935 if (current_base == window_base)
11936 buffer_shared++;
11939 /* Point refers normally to the selected window. For any other
11940 window, set up appropriate value. */
11941 if (!EQ (window, selected_window))
11943 int new_pt = XMARKER (w->pointm)->charpos;
11944 int new_pt_byte = marker_byte_position (w->pointm);
11945 if (new_pt < BEGV)
11947 new_pt = BEGV;
11948 new_pt_byte = BEGV_BYTE;
11949 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11951 else if (new_pt > (ZV - 1))
11953 new_pt = ZV;
11954 new_pt_byte = ZV_BYTE;
11955 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11958 /* We don't use SET_PT so that the point-motion hooks don't run. */
11959 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11962 /* If any of the character widths specified in the display table
11963 have changed, invalidate the width run cache. It's true that
11964 this may be a bit late to catch such changes, but the rest of
11965 redisplay goes (non-fatally) haywire when the display table is
11966 changed, so why should we worry about doing any better? */
11967 if (current_buffer->width_run_cache)
11969 struct Lisp_Char_Table *disptab = buffer_display_table ();
11971 if (! disptab_matches_widthtab (disptab,
11972 XVECTOR (current_buffer->width_table)))
11974 invalidate_region_cache (current_buffer,
11975 current_buffer->width_run_cache,
11976 BEG, Z);
11977 recompute_width_table (current_buffer, disptab);
11981 /* If window-start is screwed up, choose a new one. */
11982 if (XMARKER (w->start)->buffer != current_buffer)
11983 goto recenter;
11985 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11987 /* If someone specified a new starting point but did not insist,
11988 check whether it can be used. */
11989 if (!NILP (w->optional_new_start)
11990 && CHARPOS (startp) >= BEGV
11991 && CHARPOS (startp) <= ZV)
11993 w->optional_new_start = Qnil;
11994 start_display (&it, w, startp);
11995 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11996 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11997 if (IT_CHARPOS (it) == PT)
11998 w->force_start = Qt;
11999 /* IT may overshoot PT if text at PT is invisible. */
12000 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12001 w->force_start = Qt;
12006 /* Handle case where place to start displaying has been specified,
12007 unless the specified location is outside the accessible range. */
12008 if (!NILP (w->force_start)
12009 || w->frozen_window_start_p)
12011 /* We set this later on if we have to adjust point. */
12012 int new_vpos = -1;
12014 w->force_start = Qnil;
12015 w->vscroll = 0;
12016 w->window_end_valid = Qnil;
12018 /* Forget any recorded base line for line number display. */
12019 if (!buffer_unchanged_p)
12020 w->base_line_number = Qnil;
12022 /* Redisplay the mode line. Select the buffer properly for that.
12023 Also, run the hook window-scroll-functions
12024 because we have scrolled. */
12025 /* Note, we do this after clearing force_start because
12026 if there's an error, it is better to forget about force_start
12027 than to get into an infinite loop calling the hook functions
12028 and having them get more errors. */
12029 if (!update_mode_line
12030 || ! NILP (Vwindow_scroll_functions))
12032 update_mode_line = 1;
12033 w->update_mode_line = Qt;
12034 startp = run_window_scroll_functions (window, startp);
12037 w->last_modified = make_number (0);
12038 w->last_overlay_modified = make_number (0);
12039 if (CHARPOS (startp) < BEGV)
12040 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12041 else if (CHARPOS (startp) > ZV)
12042 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12044 /* Redisplay, then check if cursor has been set during the
12045 redisplay. Give up if new fonts were loaded. */
12046 if (!try_window (window, startp))
12048 w->force_start = Qt;
12049 clear_glyph_matrix (w->desired_matrix);
12050 goto need_larger_matrices;
12053 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12055 /* If point does not appear, try to move point so it does
12056 appear. The desired matrix has been built above, so we
12057 can use it here. */
12058 new_vpos = window_box_height (w) / 2;
12061 if (!cursor_row_fully_visible_p (w, 0, 0))
12063 /* Point does appear, but on a line partly visible at end of window.
12064 Move it back to a fully-visible line. */
12065 new_vpos = window_box_height (w);
12068 /* If we need to move point for either of the above reasons,
12069 now actually do it. */
12070 if (new_vpos >= 0)
12072 struct glyph_row *row;
12074 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12075 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12076 ++row;
12078 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12079 MATRIX_ROW_START_BYTEPOS (row));
12081 if (w != XWINDOW (selected_window))
12082 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12083 else if (current_buffer == old)
12084 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12086 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12088 /* If we are highlighting the region, then we just changed
12089 the region, so redisplay to show it. */
12090 if (!NILP (Vtransient_mark_mode)
12091 && !NILP (current_buffer->mark_active))
12093 clear_glyph_matrix (w->desired_matrix);
12094 if (!try_window (window, startp))
12095 goto need_larger_matrices;
12099 #if GLYPH_DEBUG
12100 debug_method_add (w, "forced window start");
12101 #endif
12102 goto done;
12105 /* Handle case where text has not changed, only point, and it has
12106 not moved off the frame, and we are not retrying after hscroll.
12107 (current_matrix_up_to_date_p is nonzero when retrying.) */
12108 if (current_matrix_up_to_date_p
12109 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12110 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12112 switch (rc)
12114 case CURSOR_MOVEMENT_SUCCESS:
12115 used_current_matrix_p = 1;
12116 goto done;
12118 #if 0 /* try_cursor_movement never returns this value. */
12119 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12120 goto need_larger_matrices;
12121 #endif
12123 case CURSOR_MOVEMENT_MUST_SCROLL:
12124 goto try_to_scroll;
12126 default:
12127 abort ();
12130 /* If current starting point was originally the beginning of a line
12131 but no longer is, find a new starting point. */
12132 else if (!NILP (w->start_at_line_beg)
12133 && !(CHARPOS (startp) <= BEGV
12134 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12136 #if GLYPH_DEBUG
12137 debug_method_add (w, "recenter 1");
12138 #endif
12139 goto recenter;
12142 /* Try scrolling with try_window_id. Value is > 0 if update has
12143 been done, it is -1 if we know that the same window start will
12144 not work. It is 0 if unsuccessful for some other reason. */
12145 else if ((tem = try_window_id (w)) != 0)
12147 #if GLYPH_DEBUG
12148 debug_method_add (w, "try_window_id %d", tem);
12149 #endif
12151 if (fonts_changed_p)
12152 goto need_larger_matrices;
12153 if (tem > 0)
12154 goto done;
12156 /* Otherwise try_window_id has returned -1 which means that we
12157 don't want the alternative below this comment to execute. */
12159 else if (CHARPOS (startp) >= BEGV
12160 && CHARPOS (startp) <= ZV
12161 && PT >= CHARPOS (startp)
12162 && (CHARPOS (startp) < ZV
12163 /* Avoid starting at end of buffer. */
12164 || CHARPOS (startp) == BEGV
12165 || (XFASTINT (w->last_modified) >= MODIFF
12166 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12168 #if GLYPH_DEBUG
12169 debug_method_add (w, "same window start");
12170 #endif
12172 /* Try to redisplay starting at same place as before.
12173 If point has not moved off frame, accept the results. */
12174 if (!current_matrix_up_to_date_p
12175 /* Don't use try_window_reusing_current_matrix in this case
12176 because a window scroll function can have changed the
12177 buffer. */
12178 || !NILP (Vwindow_scroll_functions)
12179 || MINI_WINDOW_P (w)
12180 || !(used_current_matrix_p
12181 = try_window_reusing_current_matrix (w)))
12183 IF_DEBUG (debug_method_add (w, "1"));
12184 try_window (window, startp);
12187 if (fonts_changed_p)
12188 goto need_larger_matrices;
12190 if (w->cursor.vpos >= 0)
12192 if (!just_this_one_p
12193 || current_buffer->clip_changed
12194 || BEG_UNCHANGED < CHARPOS (startp))
12195 /* Forget any recorded base line for line number display. */
12196 w->base_line_number = Qnil;
12198 if (!cursor_row_fully_visible_p (w, 1, 0))
12200 clear_glyph_matrix (w->desired_matrix);
12201 last_line_misfit = 1;
12203 /* Drop through and scroll. */
12204 else
12205 goto done;
12207 else
12208 clear_glyph_matrix (w->desired_matrix);
12211 try_to_scroll:
12213 w->last_modified = make_number (0);
12214 w->last_overlay_modified = make_number (0);
12216 /* Redisplay the mode line. Select the buffer properly for that. */
12217 if (!update_mode_line)
12219 update_mode_line = 1;
12220 w->update_mode_line = Qt;
12223 /* Try to scroll by specified few lines. */
12224 if ((scroll_conservatively
12225 || scroll_step
12226 || temp_scroll_step
12227 || NUMBERP (current_buffer->scroll_up_aggressively)
12228 || NUMBERP (current_buffer->scroll_down_aggressively))
12229 && !current_buffer->clip_changed
12230 && CHARPOS (startp) >= BEGV
12231 && CHARPOS (startp) <= ZV)
12233 /* The function returns -1 if new fonts were loaded, 1 if
12234 successful, 0 if not successful. */
12235 int rc = try_scrolling (window, just_this_one_p,
12236 scroll_conservatively,
12237 scroll_step,
12238 temp_scroll_step, last_line_misfit);
12239 switch (rc)
12241 case SCROLLING_SUCCESS:
12242 goto done;
12244 case SCROLLING_NEED_LARGER_MATRICES:
12245 goto need_larger_matrices;
12247 case SCROLLING_FAILED:
12248 break;
12250 default:
12251 abort ();
12255 /* Finally, just choose place to start which centers point */
12257 recenter:
12258 if (centering_position < 0)
12259 centering_position = window_box_height (w) / 2;
12261 #if GLYPH_DEBUG
12262 debug_method_add (w, "recenter");
12263 #endif
12265 /* w->vscroll = 0; */
12267 /* Forget any previously recorded base line for line number display. */
12268 if (!buffer_unchanged_p)
12269 w->base_line_number = Qnil;
12271 /* Move backward half the height of the window. */
12272 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12273 it.current_y = it.last_visible_y;
12274 move_it_vertically_backward (&it, centering_position);
12275 xassert (IT_CHARPOS (it) >= BEGV);
12277 /* The function move_it_vertically_backward may move over more
12278 than the specified y-distance. If it->w is small, e.g. a
12279 mini-buffer window, we may end up in front of the window's
12280 display area. Start displaying at the start of the line
12281 containing PT in this case. */
12282 if (it.current_y <= 0)
12284 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12285 move_it_vertically_backward (&it, 0);
12286 xassert (IT_CHARPOS (it) <= PT);
12287 it.current_y = 0;
12290 it.current_x = it.hpos = 0;
12292 /* Set startp here explicitly in case that helps avoid an infinite loop
12293 in case the window-scroll-functions functions get errors. */
12294 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12296 /* Run scroll hooks. */
12297 startp = run_window_scroll_functions (window, it.current.pos);
12299 /* Redisplay the window. */
12300 if (!current_matrix_up_to_date_p
12301 || windows_or_buffers_changed
12302 || cursor_type_changed
12303 /* Don't use try_window_reusing_current_matrix in this case
12304 because it can have changed the buffer. */
12305 || !NILP (Vwindow_scroll_functions)
12306 || !just_this_one_p
12307 || MINI_WINDOW_P (w)
12308 || !(used_current_matrix_p
12309 = try_window_reusing_current_matrix (w)))
12310 try_window (window, startp);
12312 /* If new fonts have been loaded (due to fontsets), give up. We
12313 have to start a new redisplay since we need to re-adjust glyph
12314 matrices. */
12315 if (fonts_changed_p)
12316 goto need_larger_matrices;
12318 /* If cursor did not appear assume that the middle of the window is
12319 in the first line of the window. Do it again with the next line.
12320 (Imagine a window of height 100, displaying two lines of height
12321 60. Moving back 50 from it->last_visible_y will end in the first
12322 line.) */
12323 if (w->cursor.vpos < 0)
12325 if (!NILP (w->window_end_valid)
12326 && PT >= Z - XFASTINT (w->window_end_pos))
12328 clear_glyph_matrix (w->desired_matrix);
12329 move_it_by_lines (&it, 1, 0);
12330 try_window (window, it.current.pos);
12332 else if (PT < IT_CHARPOS (it))
12334 clear_glyph_matrix (w->desired_matrix);
12335 move_it_by_lines (&it, -1, 0);
12336 try_window (window, it.current.pos);
12338 else
12340 /* Not much we can do about it. */
12344 /* Consider the following case: Window starts at BEGV, there is
12345 invisible, intangible text at BEGV, so that display starts at
12346 some point START > BEGV. It can happen that we are called with
12347 PT somewhere between BEGV and START. Try to handle that case. */
12348 if (w->cursor.vpos < 0)
12350 struct glyph_row *row = w->current_matrix->rows;
12351 if (row->mode_line_p)
12352 ++row;
12353 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12356 if (!cursor_row_fully_visible_p (w, 0, 0))
12358 /* If vscroll is enabled, disable it and try again. */
12359 if (w->vscroll)
12361 w->vscroll = 0;
12362 clear_glyph_matrix (w->desired_matrix);
12363 goto recenter;
12366 /* If centering point failed to make the whole line visible,
12367 put point at the top instead. That has to make the whole line
12368 visible, if it can be done. */
12369 if (centering_position == 0)
12370 goto done;
12372 clear_glyph_matrix (w->desired_matrix);
12373 centering_position = 0;
12374 goto recenter;
12377 done:
12379 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12380 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12381 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12382 ? Qt : Qnil);
12384 /* Display the mode line, if we must. */
12385 if ((update_mode_line
12386 /* If window not full width, must redo its mode line
12387 if (a) the window to its side is being redone and
12388 (b) we do a frame-based redisplay. This is a consequence
12389 of how inverted lines are drawn in frame-based redisplay. */
12390 || (!just_this_one_p
12391 && !FRAME_WINDOW_P (f)
12392 && !WINDOW_FULL_WIDTH_P (w))
12393 /* Line number to display. */
12394 || INTEGERP (w->base_line_pos)
12395 /* Column number is displayed and different from the one displayed. */
12396 || (!NILP (w->column_number_displayed)
12397 && (XFASTINT (w->column_number_displayed)
12398 != (int) current_column ()))) /* iftc */
12399 /* This means that the window has a mode line. */
12400 && (WINDOW_WANTS_MODELINE_P (w)
12401 || WINDOW_WANTS_HEADER_LINE_P (w)))
12403 display_mode_lines (w);
12405 /* If mode line height has changed, arrange for a thorough
12406 immediate redisplay using the correct mode line height. */
12407 if (WINDOW_WANTS_MODELINE_P (w)
12408 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12410 fonts_changed_p = 1;
12411 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12412 = DESIRED_MODE_LINE_HEIGHT (w);
12415 /* If top line height has changed, arrange for a thorough
12416 immediate redisplay using the correct mode line height. */
12417 if (WINDOW_WANTS_HEADER_LINE_P (w)
12418 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12420 fonts_changed_p = 1;
12421 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12422 = DESIRED_HEADER_LINE_HEIGHT (w);
12425 if (fonts_changed_p)
12426 goto need_larger_matrices;
12429 if (!line_number_displayed
12430 && !BUFFERP (w->base_line_pos))
12432 w->base_line_pos = Qnil;
12433 w->base_line_number = Qnil;
12436 finish_menu_bars:
12438 /* When we reach a frame's selected window, redo the frame's menu bar. */
12439 if (update_mode_line
12440 && EQ (FRAME_SELECTED_WINDOW (f), window))
12442 int redisplay_menu_p = 0;
12443 int redisplay_tool_bar_p = 0;
12445 if (FRAME_WINDOW_P (f))
12447 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12448 || defined (USE_GTK)
12449 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12450 #else
12451 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12452 #endif
12454 else
12455 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12457 if (redisplay_menu_p)
12458 display_menu_bar (w);
12460 #ifdef HAVE_WINDOW_SYSTEM
12461 #ifdef USE_GTK
12462 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12463 #else
12464 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12465 && (FRAME_TOOL_BAR_LINES (f) > 0
12466 || auto_resize_tool_bars_p);
12468 #endif
12470 if (redisplay_tool_bar_p)
12471 redisplay_tool_bar (f);
12472 #endif
12475 #ifdef HAVE_WINDOW_SYSTEM
12476 if (FRAME_WINDOW_P (f)
12477 && update_window_fringes (w, 0)
12478 && !just_this_one_p
12479 && (used_current_matrix_p || overlay_arrow_seen)
12480 && !w->pseudo_window_p)
12482 update_begin (f);
12483 BLOCK_INPUT;
12484 if (draw_window_fringes (w, 1))
12485 x_draw_vertical_border (w);
12486 UNBLOCK_INPUT;
12487 update_end (f);
12489 #endif /* HAVE_WINDOW_SYSTEM */
12491 /* We go to this label, with fonts_changed_p nonzero,
12492 if it is necessary to try again using larger glyph matrices.
12493 We have to redeem the scroll bar even in this case,
12494 because the loop in redisplay_internal expects that. */
12495 need_larger_matrices:
12497 finish_scroll_bars:
12499 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12501 /* Set the thumb's position and size. */
12502 set_vertical_scroll_bar (w);
12504 /* Note that we actually used the scroll bar attached to this
12505 window, so it shouldn't be deleted at the end of redisplay. */
12506 redeem_scroll_bar_hook (w);
12509 /* Restore current_buffer and value of point in it. */
12510 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12511 set_buffer_internal_1 (old);
12512 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12514 unbind_to (count, Qnil);
12518 /* Build the complete desired matrix of WINDOW with a window start
12519 buffer position POS. Value is non-zero if successful. It is zero
12520 if fonts were loaded during redisplay which makes re-adjusting
12521 glyph matrices necessary. */
12524 try_window (window, pos)
12525 Lisp_Object window;
12526 struct text_pos pos;
12528 struct window *w = XWINDOW (window);
12529 struct it it;
12530 struct glyph_row *last_text_row = NULL;
12532 /* Make POS the new window start. */
12533 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12535 /* Mark cursor position as unknown. No overlay arrow seen. */
12536 w->cursor.vpos = -1;
12537 overlay_arrow_seen = 0;
12539 /* Initialize iterator and info to start at POS. */
12540 start_display (&it, w, pos);
12542 /* Display all lines of W. */
12543 while (it.current_y < it.last_visible_y)
12545 if (display_line (&it))
12546 last_text_row = it.glyph_row - 1;
12547 if (fonts_changed_p)
12548 return 0;
12551 /* If bottom moved off end of frame, change mode line percentage. */
12552 if (XFASTINT (w->window_end_pos) <= 0
12553 && Z != IT_CHARPOS (it))
12554 w->update_mode_line = Qt;
12556 /* Set window_end_pos to the offset of the last character displayed
12557 on the window from the end of current_buffer. Set
12558 window_end_vpos to its row number. */
12559 if (last_text_row)
12561 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12562 w->window_end_bytepos
12563 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12564 w->window_end_pos
12565 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12566 w->window_end_vpos
12567 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12568 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12569 ->displays_text_p);
12571 else
12573 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12574 w->window_end_pos = make_number (Z - ZV);
12575 w->window_end_vpos = make_number (0);
12578 /* But that is not valid info until redisplay finishes. */
12579 w->window_end_valid = Qnil;
12580 return 1;
12585 /************************************************************************
12586 Window redisplay reusing current matrix when buffer has not changed
12587 ************************************************************************/
12589 /* Try redisplay of window W showing an unchanged buffer with a
12590 different window start than the last time it was displayed by
12591 reusing its current matrix. Value is non-zero if successful.
12592 W->start is the new window start. */
12594 static int
12595 try_window_reusing_current_matrix (w)
12596 struct window *w;
12598 struct frame *f = XFRAME (w->frame);
12599 struct glyph_row *row, *bottom_row;
12600 struct it it;
12601 struct run run;
12602 struct text_pos start, new_start;
12603 int nrows_scrolled, i;
12604 struct glyph_row *last_text_row;
12605 struct glyph_row *last_reused_text_row;
12606 struct glyph_row *start_row;
12607 int start_vpos, min_y, max_y;
12609 #if GLYPH_DEBUG
12610 if (inhibit_try_window_reusing)
12611 return 0;
12612 #endif
12614 if (/* This function doesn't handle terminal frames. */
12615 !FRAME_WINDOW_P (f)
12616 /* Don't try to reuse the display if windows have been split
12617 or such. */
12618 || windows_or_buffers_changed
12619 || cursor_type_changed)
12620 return 0;
12622 /* Can't do this if region may have changed. */
12623 if ((!NILP (Vtransient_mark_mode)
12624 && !NILP (current_buffer->mark_active))
12625 || !NILP (w->region_showing)
12626 || !NILP (Vshow_trailing_whitespace))
12627 return 0;
12629 /* If top-line visibility has changed, give up. */
12630 if (WINDOW_WANTS_HEADER_LINE_P (w)
12631 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12632 return 0;
12634 /* Give up if old or new display is scrolled vertically. We could
12635 make this function handle this, but right now it doesn't. */
12636 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12637 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12638 return 0;
12640 /* The variable new_start now holds the new window start. The old
12641 start `start' can be determined from the current matrix. */
12642 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12643 start = start_row->start.pos;
12644 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12646 /* Clear the desired matrix for the display below. */
12647 clear_glyph_matrix (w->desired_matrix);
12649 if (CHARPOS (new_start) <= CHARPOS (start))
12651 int first_row_y;
12653 /* Don't use this method if the display starts with an ellipsis
12654 displayed for invisible text. It's not easy to handle that case
12655 below, and it's certainly not worth the effort since this is
12656 not a frequent case. */
12657 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12658 return 0;
12660 IF_DEBUG (debug_method_add (w, "twu1"));
12662 /* Display up to a row that can be reused. The variable
12663 last_text_row is set to the last row displayed that displays
12664 text. Note that it.vpos == 0 if or if not there is a
12665 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12666 start_display (&it, w, new_start);
12667 first_row_y = it.current_y;
12668 w->cursor.vpos = -1;
12669 last_text_row = last_reused_text_row = NULL;
12671 while (it.current_y < it.last_visible_y
12672 && !fonts_changed_p)
12674 /* If we have reached into the characters in the START row,
12675 that means the line boundaries have changed. So we
12676 can't start copying with the row START. Maybe it will
12677 work to start copying with the following row. */
12678 while (IT_CHARPOS (it) > CHARPOS (start))
12680 /* Advance to the next row as the "start". */
12681 start_row++;
12682 start = start_row->start.pos;
12683 /* If there are no more rows to try, or just one, give up. */
12684 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12685 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12686 || CHARPOS (start) == ZV)
12688 clear_glyph_matrix (w->desired_matrix);
12689 return 0;
12692 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12694 /* If we have reached alignment,
12695 we can copy the rest of the rows. */
12696 if (IT_CHARPOS (it) == CHARPOS (start))
12697 break;
12699 if (display_line (&it))
12700 last_text_row = it.glyph_row - 1;
12703 /* A value of current_y < last_visible_y means that we stopped
12704 at the previous window start, which in turn means that we
12705 have at least one reusable row. */
12706 if (it.current_y < it.last_visible_y)
12708 /* IT.vpos always starts from 0; it counts text lines. */
12709 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12711 /* Find PT if not already found in the lines displayed. */
12712 if (w->cursor.vpos < 0)
12714 int dy = it.current_y - start_row->y;
12716 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12717 row = row_containing_pos (w, PT, row, NULL, dy);
12718 if (row)
12719 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12720 dy, nrows_scrolled);
12721 else
12723 clear_glyph_matrix (w->desired_matrix);
12724 return 0;
12728 /* Scroll the display. Do it before the current matrix is
12729 changed. The problem here is that update has not yet
12730 run, i.e. part of the current matrix is not up to date.
12731 scroll_run_hook will clear the cursor, and use the
12732 current matrix to get the height of the row the cursor is
12733 in. */
12734 run.current_y = start_row->y;
12735 run.desired_y = it.current_y;
12736 run.height = it.last_visible_y - it.current_y;
12738 if (run.height > 0 && run.current_y != run.desired_y)
12740 update_begin (f);
12741 rif->update_window_begin_hook (w);
12742 rif->clear_window_mouse_face (w);
12743 rif->scroll_run_hook (w, &run);
12744 rif->update_window_end_hook (w, 0, 0);
12745 update_end (f);
12748 /* Shift current matrix down by nrows_scrolled lines. */
12749 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12750 rotate_matrix (w->current_matrix,
12751 start_vpos,
12752 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12753 nrows_scrolled);
12755 /* Disable lines that must be updated. */
12756 for (i = 0; i < it.vpos; ++i)
12757 (start_row + i)->enabled_p = 0;
12759 /* Re-compute Y positions. */
12760 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12761 max_y = it.last_visible_y;
12762 for (row = start_row + nrows_scrolled;
12763 row < bottom_row;
12764 ++row)
12766 row->y = it.current_y;
12767 row->visible_height = row->height;
12769 if (row->y < min_y)
12770 row->visible_height -= min_y - row->y;
12771 if (row->y + row->height > max_y)
12772 row->visible_height -= row->y + row->height - max_y;
12773 row->redraw_fringe_bitmaps_p = 1;
12775 it.current_y += row->height;
12777 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12778 last_reused_text_row = row;
12779 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12780 break;
12783 /* Disable lines in the current matrix which are now
12784 below the window. */
12785 for (++row; row < bottom_row; ++row)
12786 row->enabled_p = 0;
12789 /* Update window_end_pos etc.; last_reused_text_row is the last
12790 reused row from the current matrix containing text, if any.
12791 The value of last_text_row is the last displayed line
12792 containing text. */
12793 if (last_reused_text_row)
12795 w->window_end_bytepos
12796 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12797 w->window_end_pos
12798 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12799 w->window_end_vpos
12800 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12801 w->current_matrix));
12803 else if (last_text_row)
12805 w->window_end_bytepos
12806 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12807 w->window_end_pos
12808 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12809 w->window_end_vpos
12810 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12812 else
12814 /* This window must be completely empty. */
12815 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12816 w->window_end_pos = make_number (Z - ZV);
12817 w->window_end_vpos = make_number (0);
12819 w->window_end_valid = Qnil;
12821 /* Update hint: don't try scrolling again in update_window. */
12822 w->desired_matrix->no_scrolling_p = 1;
12824 #if GLYPH_DEBUG
12825 debug_method_add (w, "try_window_reusing_current_matrix 1");
12826 #endif
12827 return 1;
12829 else if (CHARPOS (new_start) > CHARPOS (start))
12831 struct glyph_row *pt_row, *row;
12832 struct glyph_row *first_reusable_row;
12833 struct glyph_row *first_row_to_display;
12834 int dy;
12835 int yb = window_text_bottom_y (w);
12837 /* Find the row starting at new_start, if there is one. Don't
12838 reuse a partially visible line at the end. */
12839 first_reusable_row = start_row;
12840 while (first_reusable_row->enabled_p
12841 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12842 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12843 < CHARPOS (new_start)))
12844 ++first_reusable_row;
12846 /* Give up if there is no row to reuse. */
12847 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12848 || !first_reusable_row->enabled_p
12849 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12850 != CHARPOS (new_start)))
12851 return 0;
12853 /* We can reuse fully visible rows beginning with
12854 first_reusable_row to the end of the window. Set
12855 first_row_to_display to the first row that cannot be reused.
12856 Set pt_row to the row containing point, if there is any. */
12857 pt_row = NULL;
12858 for (first_row_to_display = first_reusable_row;
12859 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12860 ++first_row_to_display)
12862 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12863 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12864 pt_row = first_row_to_display;
12867 /* Start displaying at the start of first_row_to_display. */
12868 xassert (first_row_to_display->y < yb);
12869 init_to_row_start (&it, w, first_row_to_display);
12871 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12872 - start_vpos);
12873 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12874 - nrows_scrolled);
12875 it.current_y = (first_row_to_display->y - first_reusable_row->y
12876 + WINDOW_HEADER_LINE_HEIGHT (w));
12878 /* Display lines beginning with first_row_to_display in the
12879 desired matrix. Set last_text_row to the last row displayed
12880 that displays text. */
12881 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12882 if (pt_row == NULL)
12883 w->cursor.vpos = -1;
12884 last_text_row = NULL;
12885 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12886 if (display_line (&it))
12887 last_text_row = it.glyph_row - 1;
12889 /* Give up If point isn't in a row displayed or reused. */
12890 if (w->cursor.vpos < 0)
12892 clear_glyph_matrix (w->desired_matrix);
12893 return 0;
12896 /* If point is in a reused row, adjust y and vpos of the cursor
12897 position. */
12898 if (pt_row)
12900 w->cursor.vpos -= nrows_scrolled;
12901 w->cursor.y -= first_reusable_row->y - start_row->y;
12904 /* Scroll the display. */
12905 run.current_y = first_reusable_row->y;
12906 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12907 run.height = it.last_visible_y - run.current_y;
12908 dy = run.current_y - run.desired_y;
12910 if (run.height)
12912 update_begin (f);
12913 rif->update_window_begin_hook (w);
12914 rif->clear_window_mouse_face (w);
12915 rif->scroll_run_hook (w, &run);
12916 rif->update_window_end_hook (w, 0, 0);
12917 update_end (f);
12920 /* Adjust Y positions of reused rows. */
12921 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12922 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12923 max_y = it.last_visible_y;
12924 for (row = first_reusable_row; row < first_row_to_display; ++row)
12926 row->y -= dy;
12927 row->visible_height = row->height;
12928 if (row->y < min_y)
12929 row->visible_height -= min_y - row->y;
12930 if (row->y + row->height > max_y)
12931 row->visible_height -= row->y + row->height - max_y;
12932 row->redraw_fringe_bitmaps_p = 1;
12935 /* Scroll the current matrix. */
12936 xassert (nrows_scrolled > 0);
12937 rotate_matrix (w->current_matrix,
12938 start_vpos,
12939 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12940 -nrows_scrolled);
12942 /* Disable rows not reused. */
12943 for (row -= nrows_scrolled; row < bottom_row; ++row)
12944 row->enabled_p = 0;
12946 /* Point may have moved to a different line, so we cannot assume that
12947 the previous cursor position is valid; locate the correct row. */
12948 if (pt_row)
12950 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12951 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12952 row++)
12954 w->cursor.vpos++;
12955 w->cursor.y = row->y;
12957 if (row < bottom_row)
12959 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12960 while (glyph->charpos < PT)
12962 w->cursor.hpos++;
12963 w->cursor.x += glyph->pixel_width;
12964 glyph++;
12969 /* Adjust window end. A null value of last_text_row means that
12970 the window end is in reused rows which in turn means that
12971 only its vpos can have changed. */
12972 if (last_text_row)
12974 w->window_end_bytepos
12975 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12976 w->window_end_pos
12977 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12978 w->window_end_vpos
12979 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12981 else
12983 w->window_end_vpos
12984 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12987 w->window_end_valid = Qnil;
12988 w->desired_matrix->no_scrolling_p = 1;
12990 #if GLYPH_DEBUG
12991 debug_method_add (w, "try_window_reusing_current_matrix 2");
12992 #endif
12993 return 1;
12996 return 0;
13001 /************************************************************************
13002 Window redisplay reusing current matrix when buffer has changed
13003 ************************************************************************/
13005 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13006 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13007 int *, int *));
13008 static struct glyph_row *
13009 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13010 struct glyph_row *));
13013 /* Return the last row in MATRIX displaying text. If row START is
13014 non-null, start searching with that row. IT gives the dimensions
13015 of the display. Value is null if matrix is empty; otherwise it is
13016 a pointer to the row found. */
13018 static struct glyph_row *
13019 find_last_row_displaying_text (matrix, it, start)
13020 struct glyph_matrix *matrix;
13021 struct it *it;
13022 struct glyph_row *start;
13024 struct glyph_row *row, *row_found;
13026 /* Set row_found to the last row in IT->w's current matrix
13027 displaying text. The loop looks funny but think of partially
13028 visible lines. */
13029 row_found = NULL;
13030 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13031 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13033 xassert (row->enabled_p);
13034 row_found = row;
13035 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13036 break;
13037 ++row;
13040 return row_found;
13044 /* Return the last row in the current matrix of W that is not affected
13045 by changes at the start of current_buffer that occurred since W's
13046 current matrix was built. Value is null if no such row exists.
13048 BEG_UNCHANGED us the number of characters unchanged at the start of
13049 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13050 first changed character in current_buffer. Characters at positions <
13051 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13052 when the current matrix was built. */
13054 static struct glyph_row *
13055 find_last_unchanged_at_beg_row (w)
13056 struct window *w;
13058 int first_changed_pos = BEG + BEG_UNCHANGED;
13059 struct glyph_row *row;
13060 struct glyph_row *row_found = NULL;
13061 int yb = window_text_bottom_y (w);
13063 /* Find the last row displaying unchanged text. */
13064 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13065 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13066 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13068 if (/* If row ends before first_changed_pos, it is unchanged,
13069 except in some case. */
13070 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13071 /* When row ends in ZV and we write at ZV it is not
13072 unchanged. */
13073 && !row->ends_at_zv_p
13074 /* When first_changed_pos is the end of a continued line,
13075 row is not unchanged because it may be no longer
13076 continued. */
13077 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13078 && (row->continued_p
13079 || row->exact_window_width_line_p)))
13080 row_found = row;
13082 /* Stop if last visible row. */
13083 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13084 break;
13086 ++row;
13089 return row_found;
13093 /* Find the first glyph row in the current matrix of W that is not
13094 affected by changes at the end of current_buffer since the
13095 time W's current matrix was built.
13097 Return in *DELTA the number of chars by which buffer positions in
13098 unchanged text at the end of current_buffer must be adjusted.
13100 Return in *DELTA_BYTES the corresponding number of bytes.
13102 Value is null if no such row exists, i.e. all rows are affected by
13103 changes. */
13105 static struct glyph_row *
13106 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13107 struct window *w;
13108 int *delta, *delta_bytes;
13110 struct glyph_row *row;
13111 struct glyph_row *row_found = NULL;
13113 *delta = *delta_bytes = 0;
13115 /* Display must not have been paused, otherwise the current matrix
13116 is not up to date. */
13117 if (NILP (w->window_end_valid))
13118 abort ();
13120 /* A value of window_end_pos >= END_UNCHANGED means that the window
13121 end is in the range of changed text. If so, there is no
13122 unchanged row at the end of W's current matrix. */
13123 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13124 return NULL;
13126 /* Set row to the last row in W's current matrix displaying text. */
13127 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13129 /* If matrix is entirely empty, no unchanged row exists. */
13130 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13132 /* The value of row is the last glyph row in the matrix having a
13133 meaningful buffer position in it. The end position of row
13134 corresponds to window_end_pos. This allows us to translate
13135 buffer positions in the current matrix to current buffer
13136 positions for characters not in changed text. */
13137 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13138 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13139 int last_unchanged_pos, last_unchanged_pos_old;
13140 struct glyph_row *first_text_row
13141 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13143 *delta = Z - Z_old;
13144 *delta_bytes = Z_BYTE - Z_BYTE_old;
13146 /* Set last_unchanged_pos to the buffer position of the last
13147 character in the buffer that has not been changed. Z is the
13148 index + 1 of the last character in current_buffer, i.e. by
13149 subtracting END_UNCHANGED we get the index of the last
13150 unchanged character, and we have to add BEG to get its buffer
13151 position. */
13152 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13153 last_unchanged_pos_old = last_unchanged_pos - *delta;
13155 /* Search backward from ROW for a row displaying a line that
13156 starts at a minimum position >= last_unchanged_pos_old. */
13157 for (; row > first_text_row; --row)
13159 /* This used to abort, but it can happen.
13160 It is ok to just stop the search instead here. KFS. */
13161 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13162 break;
13164 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13165 row_found = row;
13169 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13170 abort ();
13172 return row_found;
13176 /* Make sure that glyph rows in the current matrix of window W
13177 reference the same glyph memory as corresponding rows in the
13178 frame's frame matrix. This function is called after scrolling W's
13179 current matrix on a terminal frame in try_window_id and
13180 try_window_reusing_current_matrix. */
13182 static void
13183 sync_frame_with_window_matrix_rows (w)
13184 struct window *w;
13186 struct frame *f = XFRAME (w->frame);
13187 struct glyph_row *window_row, *window_row_end, *frame_row;
13189 /* Preconditions: W must be a leaf window and full-width. Its frame
13190 must have a frame matrix. */
13191 xassert (NILP (w->hchild) && NILP (w->vchild));
13192 xassert (WINDOW_FULL_WIDTH_P (w));
13193 xassert (!FRAME_WINDOW_P (f));
13195 /* If W is a full-width window, glyph pointers in W's current matrix
13196 have, by definition, to be the same as glyph pointers in the
13197 corresponding frame matrix. Note that frame matrices have no
13198 marginal areas (see build_frame_matrix). */
13199 window_row = w->current_matrix->rows;
13200 window_row_end = window_row + w->current_matrix->nrows;
13201 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13202 while (window_row < window_row_end)
13204 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13205 struct glyph *end = window_row->glyphs[LAST_AREA];
13207 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13208 frame_row->glyphs[TEXT_AREA] = start;
13209 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13210 frame_row->glyphs[LAST_AREA] = end;
13212 /* Disable frame rows whose corresponding window rows have
13213 been disabled in try_window_id. */
13214 if (!window_row->enabled_p)
13215 frame_row->enabled_p = 0;
13217 ++window_row, ++frame_row;
13222 /* Find the glyph row in window W containing CHARPOS. Consider all
13223 rows between START and END (not inclusive). END null means search
13224 all rows to the end of the display area of W. Value is the row
13225 containing CHARPOS or null. */
13227 struct glyph_row *
13228 row_containing_pos (w, charpos, start, end, dy)
13229 struct window *w;
13230 int charpos;
13231 struct glyph_row *start, *end;
13232 int dy;
13234 struct glyph_row *row = start;
13235 int last_y;
13237 /* If we happen to start on a header-line, skip that. */
13238 if (row->mode_line_p)
13239 ++row;
13241 if ((end && row >= end) || !row->enabled_p)
13242 return NULL;
13244 last_y = window_text_bottom_y (w) - dy;
13246 while (1)
13248 /* Give up if we have gone too far. */
13249 if (end && row >= end)
13250 return NULL;
13251 /* This formerly returned if they were equal.
13252 I think that both quantities are of a "last plus one" type;
13253 if so, when they are equal, the row is within the screen. -- rms. */
13254 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13255 return NULL;
13257 /* If it is in this row, return this row. */
13258 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13259 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13260 /* The end position of a row equals the start
13261 position of the next row. If CHARPOS is there, we
13262 would rather display it in the next line, except
13263 when this line ends in ZV. */
13264 && !row->ends_at_zv_p
13265 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13266 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13267 return row;
13268 ++row;
13273 /* Try to redisplay window W by reusing its existing display. W's
13274 current matrix must be up to date when this function is called,
13275 i.e. window_end_valid must not be nil.
13277 Value is
13279 1 if display has been updated
13280 0 if otherwise unsuccessful
13281 -1 if redisplay with same window start is known not to succeed
13283 The following steps are performed:
13285 1. Find the last row in the current matrix of W that is not
13286 affected by changes at the start of current_buffer. If no such row
13287 is found, give up.
13289 2. Find the first row in W's current matrix that is not affected by
13290 changes at the end of current_buffer. Maybe there is no such row.
13292 3. Display lines beginning with the row + 1 found in step 1 to the
13293 row found in step 2 or, if step 2 didn't find a row, to the end of
13294 the window.
13296 4. If cursor is not known to appear on the window, give up.
13298 5. If display stopped at the row found in step 2, scroll the
13299 display and current matrix as needed.
13301 6. Maybe display some lines at the end of W, if we must. This can
13302 happen under various circumstances, like a partially visible line
13303 becoming fully visible, or because newly displayed lines are displayed
13304 in smaller font sizes.
13306 7. Update W's window end information. */
13308 static int
13309 try_window_id (w)
13310 struct window *w;
13312 struct frame *f = XFRAME (w->frame);
13313 struct glyph_matrix *current_matrix = w->current_matrix;
13314 struct glyph_matrix *desired_matrix = w->desired_matrix;
13315 struct glyph_row *last_unchanged_at_beg_row;
13316 struct glyph_row *first_unchanged_at_end_row;
13317 struct glyph_row *row;
13318 struct glyph_row *bottom_row;
13319 int bottom_vpos;
13320 struct it it;
13321 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13322 struct text_pos start_pos;
13323 struct run run;
13324 int first_unchanged_at_end_vpos = 0;
13325 struct glyph_row *last_text_row, *last_text_row_at_end;
13326 struct text_pos start;
13327 int first_changed_charpos, last_changed_charpos;
13329 #if GLYPH_DEBUG
13330 if (inhibit_try_window_id)
13331 return 0;
13332 #endif
13334 /* This is handy for debugging. */
13335 #if 0
13336 #define GIVE_UP(X) \
13337 do { \
13338 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13339 return 0; \
13340 } while (0)
13341 #else
13342 #define GIVE_UP(X) return 0
13343 #endif
13345 SET_TEXT_POS_FROM_MARKER (start, w->start);
13347 /* Don't use this for mini-windows because these can show
13348 messages and mini-buffers, and we don't handle that here. */
13349 if (MINI_WINDOW_P (w))
13350 GIVE_UP (1);
13352 /* This flag is used to prevent redisplay optimizations. */
13353 if (windows_or_buffers_changed || cursor_type_changed)
13354 GIVE_UP (2);
13356 /* Verify that narrowing has not changed.
13357 Also verify that we were not told to prevent redisplay optimizations.
13358 It would be nice to further
13359 reduce the number of cases where this prevents try_window_id. */
13360 if (current_buffer->clip_changed
13361 || current_buffer->prevent_redisplay_optimizations_p)
13362 GIVE_UP (3);
13364 /* Window must either use window-based redisplay or be full width. */
13365 if (!FRAME_WINDOW_P (f)
13366 && (!line_ins_del_ok
13367 || !WINDOW_FULL_WIDTH_P (w)))
13368 GIVE_UP (4);
13370 /* Give up if point is not known NOT to appear in W. */
13371 if (PT < CHARPOS (start))
13372 GIVE_UP (5);
13374 /* Another way to prevent redisplay optimizations. */
13375 if (XFASTINT (w->last_modified) == 0)
13376 GIVE_UP (6);
13378 /* Verify that window is not hscrolled. */
13379 if (XFASTINT (w->hscroll) != 0)
13380 GIVE_UP (7);
13382 /* Verify that display wasn't paused. */
13383 if (NILP (w->window_end_valid))
13384 GIVE_UP (8);
13386 /* Can't use this if highlighting a region because a cursor movement
13387 will do more than just set the cursor. */
13388 if (!NILP (Vtransient_mark_mode)
13389 && !NILP (current_buffer->mark_active))
13390 GIVE_UP (9);
13392 /* Likewise if highlighting trailing whitespace. */
13393 if (!NILP (Vshow_trailing_whitespace))
13394 GIVE_UP (11);
13396 /* Likewise if showing a region. */
13397 if (!NILP (w->region_showing))
13398 GIVE_UP (10);
13400 /* Can use this if overlay arrow position and or string have changed. */
13401 if (overlay_arrows_changed_p ())
13402 GIVE_UP (12);
13405 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13406 only if buffer has really changed. The reason is that the gap is
13407 initially at Z for freshly visited files. The code below would
13408 set end_unchanged to 0 in that case. */
13409 if (MODIFF > SAVE_MODIFF
13410 /* This seems to happen sometimes after saving a buffer. */
13411 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13413 if (GPT - BEG < BEG_UNCHANGED)
13414 BEG_UNCHANGED = GPT - BEG;
13415 if (Z - GPT < END_UNCHANGED)
13416 END_UNCHANGED = Z - GPT;
13419 /* The position of the first and last character that has been changed. */
13420 first_changed_charpos = BEG + BEG_UNCHANGED;
13421 last_changed_charpos = Z - END_UNCHANGED;
13423 /* If window starts after a line end, and the last change is in
13424 front of that newline, then changes don't affect the display.
13425 This case happens with stealth-fontification. Note that although
13426 the display is unchanged, glyph positions in the matrix have to
13427 be adjusted, of course. */
13428 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13429 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13430 && ((last_changed_charpos < CHARPOS (start)
13431 && CHARPOS (start) == BEGV)
13432 || (last_changed_charpos < CHARPOS (start) - 1
13433 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13435 int Z_old, delta, Z_BYTE_old, delta_bytes;
13436 struct glyph_row *r0;
13438 /* Compute how many chars/bytes have been added to or removed
13439 from the buffer. */
13440 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13441 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13442 delta = Z - Z_old;
13443 delta_bytes = Z_BYTE - Z_BYTE_old;
13445 /* Give up if PT is not in the window. Note that it already has
13446 been checked at the start of try_window_id that PT is not in
13447 front of the window start. */
13448 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13449 GIVE_UP (13);
13451 /* If window start is unchanged, we can reuse the whole matrix
13452 as is, after adjusting glyph positions. No need to compute
13453 the window end again, since its offset from Z hasn't changed. */
13454 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13455 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13456 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13457 /* PT must not be in a partially visible line. */
13458 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13459 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13461 /* Adjust positions in the glyph matrix. */
13462 if (delta || delta_bytes)
13464 struct glyph_row *r1
13465 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13466 increment_matrix_positions (w->current_matrix,
13467 MATRIX_ROW_VPOS (r0, current_matrix),
13468 MATRIX_ROW_VPOS (r1, current_matrix),
13469 delta, delta_bytes);
13472 /* Set the cursor. */
13473 row = row_containing_pos (w, PT, r0, NULL, 0);
13474 if (row)
13475 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13476 else
13477 abort ();
13478 return 1;
13482 /* Handle the case that changes are all below what is displayed in
13483 the window, and that PT is in the window. This shortcut cannot
13484 be taken if ZV is visible in the window, and text has been added
13485 there that is visible in the window. */
13486 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13487 /* ZV is not visible in the window, or there are no
13488 changes at ZV, actually. */
13489 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13490 || first_changed_charpos == last_changed_charpos))
13492 struct glyph_row *r0;
13494 /* Give up if PT is not in the window. Note that it already has
13495 been checked at the start of try_window_id that PT is not in
13496 front of the window start. */
13497 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13498 GIVE_UP (14);
13500 /* If window start is unchanged, we can reuse the whole matrix
13501 as is, without changing glyph positions since no text has
13502 been added/removed in front of the window end. */
13503 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13504 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13505 /* PT must not be in a partially visible line. */
13506 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13507 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13509 /* We have to compute the window end anew since text
13510 can have been added/removed after it. */
13511 w->window_end_pos
13512 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13513 w->window_end_bytepos
13514 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13516 /* Set the cursor. */
13517 row = row_containing_pos (w, PT, r0, NULL, 0);
13518 if (row)
13519 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13520 else
13521 abort ();
13522 return 2;
13526 /* Give up if window start is in the changed area.
13528 The condition used to read
13530 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13532 but why that was tested escapes me at the moment. */
13533 if (CHARPOS (start) >= first_changed_charpos
13534 && CHARPOS (start) <= last_changed_charpos)
13535 GIVE_UP (15);
13537 /* Check that window start agrees with the start of the first glyph
13538 row in its current matrix. Check this after we know the window
13539 start is not in changed text, otherwise positions would not be
13540 comparable. */
13541 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13542 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13543 GIVE_UP (16);
13545 /* Give up if the window ends in strings. Overlay strings
13546 at the end are difficult to handle, so don't try. */
13547 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13548 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13549 GIVE_UP (20);
13551 /* Compute the position at which we have to start displaying new
13552 lines. Some of the lines at the top of the window might be
13553 reusable because they are not displaying changed text. Find the
13554 last row in W's current matrix not affected by changes at the
13555 start of current_buffer. Value is null if changes start in the
13556 first line of window. */
13557 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13558 if (last_unchanged_at_beg_row)
13560 /* Avoid starting to display in the moddle of a character, a TAB
13561 for instance. This is easier than to set up the iterator
13562 exactly, and it's not a frequent case, so the additional
13563 effort wouldn't really pay off. */
13564 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13565 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13566 && last_unchanged_at_beg_row > w->current_matrix->rows)
13567 --last_unchanged_at_beg_row;
13569 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13570 GIVE_UP (17);
13572 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13573 GIVE_UP (18);
13574 start_pos = it.current.pos;
13576 /* Start displaying new lines in the desired matrix at the same
13577 vpos we would use in the current matrix, i.e. below
13578 last_unchanged_at_beg_row. */
13579 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13580 current_matrix);
13581 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13582 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13584 xassert (it.hpos == 0 && it.current_x == 0);
13586 else
13588 /* There are no reusable lines at the start of the window.
13589 Start displaying in the first text line. */
13590 start_display (&it, w, start);
13591 it.vpos = it.first_vpos;
13592 start_pos = it.current.pos;
13595 /* Find the first row that is not affected by changes at the end of
13596 the buffer. Value will be null if there is no unchanged row, in
13597 which case we must redisplay to the end of the window. delta
13598 will be set to the value by which buffer positions beginning with
13599 first_unchanged_at_end_row have to be adjusted due to text
13600 changes. */
13601 first_unchanged_at_end_row
13602 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13603 IF_DEBUG (debug_delta = delta);
13604 IF_DEBUG (debug_delta_bytes = delta_bytes);
13606 /* Set stop_pos to the buffer position up to which we will have to
13607 display new lines. If first_unchanged_at_end_row != NULL, this
13608 is the buffer position of the start of the line displayed in that
13609 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13610 that we don't stop at a buffer position. */
13611 stop_pos = 0;
13612 if (first_unchanged_at_end_row)
13614 xassert (last_unchanged_at_beg_row == NULL
13615 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13617 /* If this is a continuation line, move forward to the next one
13618 that isn't. Changes in lines above affect this line.
13619 Caution: this may move first_unchanged_at_end_row to a row
13620 not displaying text. */
13621 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13622 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13623 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13624 < it.last_visible_y))
13625 ++first_unchanged_at_end_row;
13627 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13628 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13629 >= it.last_visible_y))
13630 first_unchanged_at_end_row = NULL;
13631 else
13633 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13634 + delta);
13635 first_unchanged_at_end_vpos
13636 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13637 xassert (stop_pos >= Z - END_UNCHANGED);
13640 else if (last_unchanged_at_beg_row == NULL)
13641 GIVE_UP (19);
13644 #if GLYPH_DEBUG
13646 /* Either there is no unchanged row at the end, or the one we have
13647 now displays text. This is a necessary condition for the window
13648 end pos calculation at the end of this function. */
13649 xassert (first_unchanged_at_end_row == NULL
13650 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13652 debug_last_unchanged_at_beg_vpos
13653 = (last_unchanged_at_beg_row
13654 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13655 : -1);
13656 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13658 #endif /* GLYPH_DEBUG != 0 */
13661 /* Display new lines. Set last_text_row to the last new line
13662 displayed which has text on it, i.e. might end up as being the
13663 line where the window_end_vpos is. */
13664 w->cursor.vpos = -1;
13665 last_text_row = NULL;
13666 overlay_arrow_seen = 0;
13667 while (it.current_y < it.last_visible_y
13668 && !fonts_changed_p
13669 && (first_unchanged_at_end_row == NULL
13670 || IT_CHARPOS (it) < stop_pos))
13672 if (display_line (&it))
13673 last_text_row = it.glyph_row - 1;
13676 if (fonts_changed_p)
13677 return -1;
13680 /* Compute differences in buffer positions, y-positions etc. for
13681 lines reused at the bottom of the window. Compute what we can
13682 scroll. */
13683 if (first_unchanged_at_end_row
13684 /* No lines reused because we displayed everything up to the
13685 bottom of the window. */
13686 && it.current_y < it.last_visible_y)
13688 dvpos = (it.vpos
13689 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13690 current_matrix));
13691 dy = it.current_y - first_unchanged_at_end_row->y;
13692 run.current_y = first_unchanged_at_end_row->y;
13693 run.desired_y = run.current_y + dy;
13694 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13696 else
13698 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13699 first_unchanged_at_end_row = NULL;
13701 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13704 /* Find the cursor if not already found. We have to decide whether
13705 PT will appear on this window (it sometimes doesn't, but this is
13706 not a very frequent case.) This decision has to be made before
13707 the current matrix is altered. A value of cursor.vpos < 0 means
13708 that PT is either in one of the lines beginning at
13709 first_unchanged_at_end_row or below the window. Don't care for
13710 lines that might be displayed later at the window end; as
13711 mentioned, this is not a frequent case. */
13712 if (w->cursor.vpos < 0)
13714 /* Cursor in unchanged rows at the top? */
13715 if (PT < CHARPOS (start_pos)
13716 && last_unchanged_at_beg_row)
13718 row = row_containing_pos (w, PT,
13719 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13720 last_unchanged_at_beg_row + 1, 0);
13721 if (row)
13722 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13725 /* Start from first_unchanged_at_end_row looking for PT. */
13726 else if (first_unchanged_at_end_row)
13728 row = row_containing_pos (w, PT - delta,
13729 first_unchanged_at_end_row, NULL, 0);
13730 if (row)
13731 set_cursor_from_row (w, row, w->current_matrix, delta,
13732 delta_bytes, dy, dvpos);
13735 /* Give up if cursor was not found. */
13736 if (w->cursor.vpos < 0)
13738 clear_glyph_matrix (w->desired_matrix);
13739 return -1;
13743 /* Don't let the cursor end in the scroll margins. */
13745 int this_scroll_margin, cursor_height;
13747 this_scroll_margin = max (0, scroll_margin);
13748 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13749 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13750 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13752 if ((w->cursor.y < this_scroll_margin
13753 && CHARPOS (start) > BEGV)
13754 /* Old redisplay didn't take scroll margin into account at the bottom,
13755 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13756 || (w->cursor.y + (make_cursor_line_fully_visible_p
13757 ? cursor_height + this_scroll_margin
13758 : 1)) > it.last_visible_y)
13760 w->cursor.vpos = -1;
13761 clear_glyph_matrix (w->desired_matrix);
13762 return -1;
13766 /* Scroll the display. Do it before changing the current matrix so
13767 that xterm.c doesn't get confused about where the cursor glyph is
13768 found. */
13769 if (dy && run.height)
13771 update_begin (f);
13773 if (FRAME_WINDOW_P (f))
13775 rif->update_window_begin_hook (w);
13776 rif->clear_window_mouse_face (w);
13777 rif->scroll_run_hook (w, &run);
13778 rif->update_window_end_hook (w, 0, 0);
13780 else
13782 /* Terminal frame. In this case, dvpos gives the number of
13783 lines to scroll by; dvpos < 0 means scroll up. */
13784 int first_unchanged_at_end_vpos
13785 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13786 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13787 int end = (WINDOW_TOP_EDGE_LINE (w)
13788 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13789 + window_internal_height (w));
13791 /* Perform the operation on the screen. */
13792 if (dvpos > 0)
13794 /* Scroll last_unchanged_at_beg_row to the end of the
13795 window down dvpos lines. */
13796 set_terminal_window (end);
13798 /* On dumb terminals delete dvpos lines at the end
13799 before inserting dvpos empty lines. */
13800 if (!scroll_region_ok)
13801 ins_del_lines (end - dvpos, -dvpos);
13803 /* Insert dvpos empty lines in front of
13804 last_unchanged_at_beg_row. */
13805 ins_del_lines (from, dvpos);
13807 else if (dvpos < 0)
13809 /* Scroll up last_unchanged_at_beg_vpos to the end of
13810 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13811 set_terminal_window (end);
13813 /* Delete dvpos lines in front of
13814 last_unchanged_at_beg_vpos. ins_del_lines will set
13815 the cursor to the given vpos and emit |dvpos| delete
13816 line sequences. */
13817 ins_del_lines (from + dvpos, dvpos);
13819 /* On a dumb terminal insert dvpos empty lines at the
13820 end. */
13821 if (!scroll_region_ok)
13822 ins_del_lines (end + dvpos, -dvpos);
13825 set_terminal_window (0);
13828 update_end (f);
13831 /* Shift reused rows of the current matrix to the right position.
13832 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13833 text. */
13834 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13835 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13836 if (dvpos < 0)
13838 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13839 bottom_vpos, dvpos);
13840 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13841 bottom_vpos, 0);
13843 else if (dvpos > 0)
13845 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13846 bottom_vpos, dvpos);
13847 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13848 first_unchanged_at_end_vpos + dvpos, 0);
13851 /* For frame-based redisplay, make sure that current frame and window
13852 matrix are in sync with respect to glyph memory. */
13853 if (!FRAME_WINDOW_P (f))
13854 sync_frame_with_window_matrix_rows (w);
13856 /* Adjust buffer positions in reused rows. */
13857 if (delta)
13858 increment_matrix_positions (current_matrix,
13859 first_unchanged_at_end_vpos + dvpos,
13860 bottom_vpos, delta, delta_bytes);
13862 /* Adjust Y positions. */
13863 if (dy)
13864 shift_glyph_matrix (w, current_matrix,
13865 first_unchanged_at_end_vpos + dvpos,
13866 bottom_vpos, dy);
13868 if (first_unchanged_at_end_row)
13870 first_unchanged_at_end_row += dvpos;
13871 if (first_unchanged_at_end_row->y >= it.last_visible_y
13872 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
13873 first_unchanged_at_end_row = NULL;
13876 /* If scrolling up, there may be some lines to display at the end of
13877 the window. */
13878 last_text_row_at_end = NULL;
13879 if (dy < 0)
13881 /* Scrolling up can leave for example a partially visible line
13882 at the end of the window to be redisplayed. */
13883 /* Set last_row to the glyph row in the current matrix where the
13884 window end line is found. It has been moved up or down in
13885 the matrix by dvpos. */
13886 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13887 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13889 /* If last_row is the window end line, it should display text. */
13890 xassert (last_row->displays_text_p);
13892 /* If window end line was partially visible before, begin
13893 displaying at that line. Otherwise begin displaying with the
13894 line following it. */
13895 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13897 init_to_row_start (&it, w, last_row);
13898 it.vpos = last_vpos;
13899 it.current_y = last_row->y;
13901 else
13903 init_to_row_end (&it, w, last_row);
13904 it.vpos = 1 + last_vpos;
13905 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13906 ++last_row;
13909 /* We may start in a continuation line. If so, we have to
13910 get the right continuation_lines_width and current_x. */
13911 it.continuation_lines_width = last_row->continuation_lines_width;
13912 it.hpos = it.current_x = 0;
13914 /* Display the rest of the lines at the window end. */
13915 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13916 while (it.current_y < it.last_visible_y
13917 && !fonts_changed_p)
13919 /* Is it always sure that the display agrees with lines in
13920 the current matrix? I don't think so, so we mark rows
13921 displayed invalid in the current matrix by setting their
13922 enabled_p flag to zero. */
13923 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13924 if (display_line (&it))
13925 last_text_row_at_end = it.glyph_row - 1;
13929 /* Update window_end_pos and window_end_vpos. */
13930 if (first_unchanged_at_end_row
13931 && !last_text_row_at_end)
13933 /* Window end line if one of the preserved rows from the current
13934 matrix. Set row to the last row displaying text in current
13935 matrix starting at first_unchanged_at_end_row, after
13936 scrolling. */
13937 xassert (first_unchanged_at_end_row->displays_text_p);
13938 row = find_last_row_displaying_text (w->current_matrix, &it,
13939 first_unchanged_at_end_row);
13940 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13942 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13943 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13944 w->window_end_vpos
13945 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13946 xassert (w->window_end_bytepos >= 0);
13947 IF_DEBUG (debug_method_add (w, "A"));
13949 else if (last_text_row_at_end)
13951 w->window_end_pos
13952 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13953 w->window_end_bytepos
13954 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13955 w->window_end_vpos
13956 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13957 xassert (w->window_end_bytepos >= 0);
13958 IF_DEBUG (debug_method_add (w, "B"));
13960 else if (last_text_row)
13962 /* We have displayed either to the end of the window or at the
13963 end of the window, i.e. the last row with text is to be found
13964 in the desired matrix. */
13965 w->window_end_pos
13966 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13967 w->window_end_bytepos
13968 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13969 w->window_end_vpos
13970 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13971 xassert (w->window_end_bytepos >= 0);
13973 else if (first_unchanged_at_end_row == NULL
13974 && last_text_row == NULL
13975 && last_text_row_at_end == NULL)
13977 /* Displayed to end of window, but no line containing text was
13978 displayed. Lines were deleted at the end of the window. */
13979 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13980 int vpos = XFASTINT (w->window_end_vpos);
13981 struct glyph_row *current_row = current_matrix->rows + vpos;
13982 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13984 for (row = NULL;
13985 row == NULL && vpos >= first_vpos;
13986 --vpos, --current_row, --desired_row)
13988 if (desired_row->enabled_p)
13990 if (desired_row->displays_text_p)
13991 row = desired_row;
13993 else if (current_row->displays_text_p)
13994 row = current_row;
13997 xassert (row != NULL);
13998 w->window_end_vpos = make_number (vpos + 1);
13999 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14000 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14001 xassert (w->window_end_bytepos >= 0);
14002 IF_DEBUG (debug_method_add (w, "C"));
14004 else
14005 abort ();
14007 #if 0 /* This leads to problems, for instance when the cursor is
14008 at ZV, and the cursor line displays no text. */
14009 /* Disable rows below what's displayed in the window. This makes
14010 debugging easier. */
14011 enable_glyph_matrix_rows (current_matrix,
14012 XFASTINT (w->window_end_vpos) + 1,
14013 bottom_vpos, 0);
14014 #endif
14016 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14017 debug_end_vpos = XFASTINT (w->window_end_vpos));
14019 /* Record that display has not been completed. */
14020 w->window_end_valid = Qnil;
14021 w->desired_matrix->no_scrolling_p = 1;
14022 return 3;
14024 #undef GIVE_UP
14029 /***********************************************************************
14030 More debugging support
14031 ***********************************************************************/
14033 #if GLYPH_DEBUG
14035 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14036 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14037 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14040 /* Dump the contents of glyph matrix MATRIX on stderr.
14042 GLYPHS 0 means don't show glyph contents.
14043 GLYPHS 1 means show glyphs in short form
14044 GLYPHS > 1 means show glyphs in long form. */
14046 void
14047 dump_glyph_matrix (matrix, glyphs)
14048 struct glyph_matrix *matrix;
14049 int glyphs;
14051 int i;
14052 for (i = 0; i < matrix->nrows; ++i)
14053 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14057 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14058 the glyph row and area where the glyph comes from. */
14060 void
14061 dump_glyph (row, glyph, area)
14062 struct glyph_row *row;
14063 struct glyph *glyph;
14064 int area;
14066 if (glyph->type == CHAR_GLYPH)
14068 fprintf (stderr,
14069 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14070 glyph - row->glyphs[TEXT_AREA],
14071 'C',
14072 glyph->charpos,
14073 (BUFFERP (glyph->object)
14074 ? 'B'
14075 : (STRINGP (glyph->object)
14076 ? 'S'
14077 : '-')),
14078 glyph->pixel_width,
14079 glyph->u.ch,
14080 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14081 ? glyph->u.ch
14082 : '.'),
14083 glyph->face_id,
14084 glyph->left_box_line_p,
14085 glyph->right_box_line_p);
14087 else if (glyph->type == STRETCH_GLYPH)
14089 fprintf (stderr,
14090 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14091 glyph - row->glyphs[TEXT_AREA],
14092 'S',
14093 glyph->charpos,
14094 (BUFFERP (glyph->object)
14095 ? 'B'
14096 : (STRINGP (glyph->object)
14097 ? 'S'
14098 : '-')),
14099 glyph->pixel_width,
14101 '.',
14102 glyph->face_id,
14103 glyph->left_box_line_p,
14104 glyph->right_box_line_p);
14106 else if (glyph->type == IMAGE_GLYPH)
14108 fprintf (stderr,
14109 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14110 glyph - row->glyphs[TEXT_AREA],
14111 'I',
14112 glyph->charpos,
14113 (BUFFERP (glyph->object)
14114 ? 'B'
14115 : (STRINGP (glyph->object)
14116 ? 'S'
14117 : '-')),
14118 glyph->pixel_width,
14119 glyph->u.img_id,
14120 '.',
14121 glyph->face_id,
14122 glyph->left_box_line_p,
14123 glyph->right_box_line_p);
14128 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14129 GLYPHS 0 means don't show glyph contents.
14130 GLYPHS 1 means show glyphs in short form
14131 GLYPHS > 1 means show glyphs in long form. */
14133 void
14134 dump_glyph_row (row, vpos, glyphs)
14135 struct glyph_row *row;
14136 int vpos, glyphs;
14138 if (glyphs != 1)
14140 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
14141 fprintf (stderr, "=======================================================================\n");
14143 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14144 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14145 vpos,
14146 MATRIX_ROW_START_CHARPOS (row),
14147 MATRIX_ROW_END_CHARPOS (row),
14148 row->used[TEXT_AREA],
14149 row->contains_overlapping_glyphs_p,
14150 row->enabled_p,
14151 row->truncated_on_left_p,
14152 row->truncated_on_right_p,
14153 row->overlay_arrow_p,
14154 row->continued_p,
14155 MATRIX_ROW_CONTINUATION_LINE_P (row),
14156 row->displays_text_p,
14157 row->ends_at_zv_p,
14158 row->fill_line_p,
14159 row->ends_in_middle_of_char_p,
14160 row->starts_in_middle_of_char_p,
14161 row->mouse_face_p,
14162 row->x,
14163 row->y,
14164 row->pixel_width,
14165 row->height,
14166 row->visible_height,
14167 row->ascent,
14168 row->phys_ascent);
14169 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14170 row->end.overlay_string_index,
14171 row->continuation_lines_width);
14172 fprintf (stderr, "%9d %5d\n",
14173 CHARPOS (row->start.string_pos),
14174 CHARPOS (row->end.string_pos));
14175 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14176 row->end.dpvec_index);
14179 if (glyphs > 1)
14181 int area;
14183 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14185 struct glyph *glyph = row->glyphs[area];
14186 struct glyph *glyph_end = glyph + row->used[area];
14188 /* Glyph for a line end in text. */
14189 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14190 ++glyph_end;
14192 if (glyph < glyph_end)
14193 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14195 for (; glyph < glyph_end; ++glyph)
14196 dump_glyph (row, glyph, area);
14199 else if (glyphs == 1)
14201 int area;
14203 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14205 char *s = (char *) alloca (row->used[area] + 1);
14206 int i;
14208 for (i = 0; i < row->used[area]; ++i)
14210 struct glyph *glyph = row->glyphs[area] + i;
14211 if (glyph->type == CHAR_GLYPH
14212 && glyph->u.ch < 0x80
14213 && glyph->u.ch >= ' ')
14214 s[i] = glyph->u.ch;
14215 else
14216 s[i] = '.';
14219 s[i] = '\0';
14220 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14226 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14227 Sdump_glyph_matrix, 0, 1, "p",
14228 doc: /* Dump the current matrix of the selected window to stderr.
14229 Shows contents of glyph row structures. With non-nil
14230 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14231 glyphs in short form, otherwise show glyphs in long form. */)
14232 (glyphs)
14233 Lisp_Object glyphs;
14235 struct window *w = XWINDOW (selected_window);
14236 struct buffer *buffer = XBUFFER (w->buffer);
14238 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14239 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14240 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14241 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14242 fprintf (stderr, "=============================================\n");
14243 dump_glyph_matrix (w->current_matrix,
14244 NILP (glyphs) ? 0 : XINT (glyphs));
14245 return Qnil;
14249 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14250 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14253 struct frame *f = XFRAME (selected_frame);
14254 dump_glyph_matrix (f->current_matrix, 1);
14255 return Qnil;
14259 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14260 doc: /* Dump glyph row ROW to stderr.
14261 GLYPH 0 means don't dump glyphs.
14262 GLYPH 1 means dump glyphs in short form.
14263 GLYPH > 1 or omitted means dump glyphs in long form. */)
14264 (row, glyphs)
14265 Lisp_Object row, glyphs;
14267 struct glyph_matrix *matrix;
14268 int vpos;
14270 CHECK_NUMBER (row);
14271 matrix = XWINDOW (selected_window)->current_matrix;
14272 vpos = XINT (row);
14273 if (vpos >= 0 && vpos < matrix->nrows)
14274 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14275 vpos,
14276 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14277 return Qnil;
14281 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14282 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14283 GLYPH 0 means don't dump glyphs.
14284 GLYPH 1 means dump glyphs in short form.
14285 GLYPH > 1 or omitted means dump glyphs in long form. */)
14286 (row, glyphs)
14287 Lisp_Object row, glyphs;
14289 struct frame *sf = SELECTED_FRAME ();
14290 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14291 int vpos;
14293 CHECK_NUMBER (row);
14294 vpos = XINT (row);
14295 if (vpos >= 0 && vpos < m->nrows)
14296 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14297 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14298 return Qnil;
14302 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14303 doc: /* Toggle tracing of redisplay.
14304 With ARG, turn tracing on if and only if ARG is positive. */)
14305 (arg)
14306 Lisp_Object arg;
14308 if (NILP (arg))
14309 trace_redisplay_p = !trace_redisplay_p;
14310 else
14312 arg = Fprefix_numeric_value (arg);
14313 trace_redisplay_p = XINT (arg) > 0;
14316 return Qnil;
14320 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14321 doc: /* Like `format', but print result to stderr.
14322 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14323 (nargs, args)
14324 int nargs;
14325 Lisp_Object *args;
14327 Lisp_Object s = Fformat (nargs, args);
14328 fprintf (stderr, "%s", SDATA (s));
14329 return Qnil;
14332 #endif /* GLYPH_DEBUG */
14336 /***********************************************************************
14337 Building Desired Matrix Rows
14338 ***********************************************************************/
14340 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14341 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14343 static struct glyph_row *
14344 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14345 struct window *w;
14346 Lisp_Object overlay_arrow_string;
14348 struct frame *f = XFRAME (WINDOW_FRAME (w));
14349 struct buffer *buffer = XBUFFER (w->buffer);
14350 struct buffer *old = current_buffer;
14351 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14352 int arrow_len = SCHARS (overlay_arrow_string);
14353 const unsigned char *arrow_end = arrow_string + arrow_len;
14354 const unsigned char *p;
14355 struct it it;
14356 int multibyte_p;
14357 int n_glyphs_before;
14359 set_buffer_temp (buffer);
14360 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14361 it.glyph_row->used[TEXT_AREA] = 0;
14362 SET_TEXT_POS (it.position, 0, 0);
14364 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14365 p = arrow_string;
14366 while (p < arrow_end)
14368 Lisp_Object face, ilisp;
14370 /* Get the next character. */
14371 if (multibyte_p)
14372 it.c = string_char_and_length (p, arrow_len, &it.len);
14373 else
14374 it.c = *p, it.len = 1;
14375 p += it.len;
14377 /* Get its face. */
14378 ilisp = make_number (p - arrow_string);
14379 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14380 it.face_id = compute_char_face (f, it.c, face);
14382 /* Compute its width, get its glyphs. */
14383 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14384 SET_TEXT_POS (it.position, -1, -1);
14385 PRODUCE_GLYPHS (&it);
14387 /* If this character doesn't fit any more in the line, we have
14388 to remove some glyphs. */
14389 if (it.current_x > it.last_visible_x)
14391 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14392 break;
14396 set_buffer_temp (old);
14397 return it.glyph_row;
14401 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14402 glyphs are only inserted for terminal frames since we can't really
14403 win with truncation glyphs when partially visible glyphs are
14404 involved. Which glyphs to insert is determined by
14405 produce_special_glyphs. */
14407 static void
14408 insert_left_trunc_glyphs (it)
14409 struct it *it;
14411 struct it truncate_it;
14412 struct glyph *from, *end, *to, *toend;
14414 xassert (!FRAME_WINDOW_P (it->f));
14416 /* Get the truncation glyphs. */
14417 truncate_it = *it;
14418 truncate_it.current_x = 0;
14419 truncate_it.face_id = DEFAULT_FACE_ID;
14420 truncate_it.glyph_row = &scratch_glyph_row;
14421 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14422 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14423 truncate_it.object = make_number (0);
14424 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14426 /* Overwrite glyphs from IT with truncation glyphs. */
14427 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14428 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14429 to = it->glyph_row->glyphs[TEXT_AREA];
14430 toend = to + it->glyph_row->used[TEXT_AREA];
14432 while (from < end)
14433 *to++ = *from++;
14435 /* There may be padding glyphs left over. Overwrite them too. */
14436 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14438 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14439 while (from < end)
14440 *to++ = *from++;
14443 if (to > toend)
14444 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14448 /* Compute the pixel height and width of IT->glyph_row.
14450 Most of the time, ascent and height of a display line will be equal
14451 to the max_ascent and max_height values of the display iterator
14452 structure. This is not the case if
14454 1. We hit ZV without displaying anything. In this case, max_ascent
14455 and max_height will be zero.
14457 2. We have some glyphs that don't contribute to the line height.
14458 (The glyph row flag contributes_to_line_height_p is for future
14459 pixmap extensions).
14461 The first case is easily covered by using default values because in
14462 these cases, the line height does not really matter, except that it
14463 must not be zero. */
14465 static void
14466 compute_line_metrics (it)
14467 struct it *it;
14469 struct glyph_row *row = it->glyph_row;
14470 int area, i;
14472 if (FRAME_WINDOW_P (it->f))
14474 int i, min_y, max_y;
14476 /* The line may consist of one space only, that was added to
14477 place the cursor on it. If so, the row's height hasn't been
14478 computed yet. */
14479 if (row->height == 0)
14481 if (it->max_ascent + it->max_descent == 0)
14482 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14483 row->ascent = it->max_ascent;
14484 row->height = it->max_ascent + it->max_descent;
14485 row->phys_ascent = it->max_phys_ascent;
14486 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14487 row->extra_line_spacing = it->max_extra_line_spacing;
14490 /* Compute the width of this line. */
14491 row->pixel_width = row->x;
14492 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14493 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14495 xassert (row->pixel_width >= 0);
14496 xassert (row->ascent >= 0 && row->height > 0);
14498 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14499 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14501 /* If first line's physical ascent is larger than its logical
14502 ascent, use the physical ascent, and make the row taller.
14503 This makes accented characters fully visible. */
14504 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14505 && row->phys_ascent > row->ascent)
14507 row->height += row->phys_ascent - row->ascent;
14508 row->ascent = row->phys_ascent;
14511 /* Compute how much of the line is visible. */
14512 row->visible_height = row->height;
14514 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14515 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14517 if (row->y < min_y)
14518 row->visible_height -= min_y - row->y;
14519 if (row->y + row->height > max_y)
14520 row->visible_height -= row->y + row->height - max_y;
14522 else
14524 row->pixel_width = row->used[TEXT_AREA];
14525 if (row->continued_p)
14526 row->pixel_width -= it->continuation_pixel_width;
14527 else if (row->truncated_on_right_p)
14528 row->pixel_width -= it->truncation_pixel_width;
14529 row->ascent = row->phys_ascent = 0;
14530 row->height = row->phys_height = row->visible_height = 1;
14531 row->extra_line_spacing = 0;
14534 /* Compute a hash code for this row. */
14535 row->hash = 0;
14536 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14537 for (i = 0; i < row->used[area]; ++i)
14538 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14539 + row->glyphs[area][i].u.val
14540 + row->glyphs[area][i].face_id
14541 + row->glyphs[area][i].padding_p
14542 + (row->glyphs[area][i].type << 2));
14544 it->max_ascent = it->max_descent = 0;
14545 it->max_phys_ascent = it->max_phys_descent = 0;
14549 /* Append one space to the glyph row of iterator IT if doing a
14550 window-based redisplay. The space has the same face as
14551 IT->face_id. Value is non-zero if a space was added.
14553 This function is called to make sure that there is always one glyph
14554 at the end of a glyph row that the cursor can be set on under
14555 window-systems. (If there weren't such a glyph we would not know
14556 how wide and tall a box cursor should be displayed).
14558 At the same time this space let's a nicely handle clearing to the
14559 end of the line if the row ends in italic text. */
14561 static int
14562 append_space_for_newline (it, default_face_p)
14563 struct it *it;
14564 int default_face_p;
14566 if (FRAME_WINDOW_P (it->f))
14568 int n = it->glyph_row->used[TEXT_AREA];
14570 if (it->glyph_row->glyphs[TEXT_AREA] + n
14571 < it->glyph_row->glyphs[1 + TEXT_AREA])
14573 /* Save some values that must not be changed.
14574 Must save IT->c and IT->len because otherwise
14575 ITERATOR_AT_END_P wouldn't work anymore after
14576 append_space_for_newline has been called. */
14577 enum display_element_type saved_what = it->what;
14578 int saved_c = it->c, saved_len = it->len;
14579 int saved_x = it->current_x;
14580 int saved_face_id = it->face_id;
14581 struct text_pos saved_pos;
14582 Lisp_Object saved_object;
14583 struct face *face;
14585 saved_object = it->object;
14586 saved_pos = it->position;
14588 it->what = IT_CHARACTER;
14589 bzero (&it->position, sizeof it->position);
14590 it->object = make_number (0);
14591 it->c = ' ';
14592 it->len = 1;
14594 if (default_face_p)
14595 it->face_id = DEFAULT_FACE_ID;
14596 else if (it->face_before_selective_p)
14597 it->face_id = it->saved_face_id;
14598 face = FACE_FROM_ID (it->f, it->face_id);
14599 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14601 PRODUCE_GLYPHS (it);
14603 it->override_ascent = -1;
14604 it->constrain_row_ascent_descent_p = 0;
14605 it->current_x = saved_x;
14606 it->object = saved_object;
14607 it->position = saved_pos;
14608 it->what = saved_what;
14609 it->face_id = saved_face_id;
14610 it->len = saved_len;
14611 it->c = saved_c;
14612 return 1;
14616 return 0;
14620 /* Extend the face of the last glyph in the text area of IT->glyph_row
14621 to the end of the display line. Called from display_line.
14622 If the glyph row is empty, add a space glyph to it so that we
14623 know the face to draw. Set the glyph row flag fill_line_p. */
14625 static void
14626 extend_face_to_end_of_line (it)
14627 struct it *it;
14629 struct face *face;
14630 struct frame *f = it->f;
14632 /* If line is already filled, do nothing. */
14633 if (it->current_x >= it->last_visible_x)
14634 return;
14636 /* Face extension extends the background and box of IT->face_id
14637 to the end of the line. If the background equals the background
14638 of the frame, we don't have to do anything. */
14639 if (it->face_before_selective_p)
14640 face = FACE_FROM_ID (it->f, it->saved_face_id);
14641 else
14642 face = FACE_FROM_ID (f, it->face_id);
14644 if (FRAME_WINDOW_P (f)
14645 && face->box == FACE_NO_BOX
14646 && face->background == FRAME_BACKGROUND_PIXEL (f)
14647 && !face->stipple)
14648 return;
14650 /* Set the glyph row flag indicating that the face of the last glyph
14651 in the text area has to be drawn to the end of the text area. */
14652 it->glyph_row->fill_line_p = 1;
14654 /* If current character of IT is not ASCII, make sure we have the
14655 ASCII face. This will be automatically undone the next time
14656 get_next_display_element returns a multibyte character. Note
14657 that the character will always be single byte in unibyte text. */
14658 if (!SINGLE_BYTE_CHAR_P (it->c))
14660 it->face_id = FACE_FOR_CHAR (f, face, 0);
14663 if (FRAME_WINDOW_P (f))
14665 /* If the row is empty, add a space with the current face of IT,
14666 so that we know which face to draw. */
14667 if (it->glyph_row->used[TEXT_AREA] == 0)
14669 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14670 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14671 it->glyph_row->used[TEXT_AREA] = 1;
14674 else
14676 /* Save some values that must not be changed. */
14677 int saved_x = it->current_x;
14678 struct text_pos saved_pos;
14679 Lisp_Object saved_object;
14680 enum display_element_type saved_what = it->what;
14681 int saved_face_id = it->face_id;
14683 saved_object = it->object;
14684 saved_pos = it->position;
14686 it->what = IT_CHARACTER;
14687 bzero (&it->position, sizeof it->position);
14688 it->object = make_number (0);
14689 it->c = ' ';
14690 it->len = 1;
14691 it->face_id = face->id;
14693 PRODUCE_GLYPHS (it);
14695 while (it->current_x <= it->last_visible_x)
14696 PRODUCE_GLYPHS (it);
14698 /* Don't count these blanks really. It would let us insert a left
14699 truncation glyph below and make us set the cursor on them, maybe. */
14700 it->current_x = saved_x;
14701 it->object = saved_object;
14702 it->position = saved_pos;
14703 it->what = saved_what;
14704 it->face_id = saved_face_id;
14709 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14710 trailing whitespace. */
14712 static int
14713 trailing_whitespace_p (charpos)
14714 int charpos;
14716 int bytepos = CHAR_TO_BYTE (charpos);
14717 int c = 0;
14719 while (bytepos < ZV_BYTE
14720 && (c = FETCH_CHAR (bytepos),
14721 c == ' ' || c == '\t'))
14722 ++bytepos;
14724 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14726 if (bytepos != PT_BYTE)
14727 return 1;
14729 return 0;
14733 /* Highlight trailing whitespace, if any, in ROW. */
14735 void
14736 highlight_trailing_whitespace (f, row)
14737 struct frame *f;
14738 struct glyph_row *row;
14740 int used = row->used[TEXT_AREA];
14742 if (used)
14744 struct glyph *start = row->glyphs[TEXT_AREA];
14745 struct glyph *glyph = start + used - 1;
14747 /* Skip over glyphs inserted to display the cursor at the
14748 end of a line, for extending the face of the last glyph
14749 to the end of the line on terminals, and for truncation
14750 and continuation glyphs. */
14751 while (glyph >= start
14752 && glyph->type == CHAR_GLYPH
14753 && INTEGERP (glyph->object))
14754 --glyph;
14756 /* If last glyph is a space or stretch, and it's trailing
14757 whitespace, set the face of all trailing whitespace glyphs in
14758 IT->glyph_row to `trailing-whitespace'. */
14759 if (glyph >= start
14760 && BUFFERP (glyph->object)
14761 && (glyph->type == STRETCH_GLYPH
14762 || (glyph->type == CHAR_GLYPH
14763 && glyph->u.ch == ' '))
14764 && trailing_whitespace_p (glyph->charpos))
14766 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14767 if (face_id < 0)
14768 return;
14770 while (glyph >= start
14771 && BUFFERP (glyph->object)
14772 && (glyph->type == STRETCH_GLYPH
14773 || (glyph->type == CHAR_GLYPH
14774 && glyph->u.ch == ' ')))
14775 (glyph--)->face_id = face_id;
14781 /* Value is non-zero if glyph row ROW in window W should be
14782 used to hold the cursor. */
14784 static int
14785 cursor_row_p (w, row)
14786 struct window *w;
14787 struct glyph_row *row;
14789 int cursor_row_p = 1;
14791 if (PT == MATRIX_ROW_END_CHARPOS (row))
14793 /* If the row ends with a newline from a string, we don't want
14794 the cursor there (if the row is continued it doesn't end in a
14795 newline). */
14796 if (CHARPOS (row->end.string_pos) >= 0)
14797 cursor_row_p = row->continued_p;
14798 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14800 /* If the row ends in middle of a real character,
14801 and the line is continued, we want the cursor here.
14802 That's because MATRIX_ROW_END_CHARPOS would equal
14803 PT if PT is before the character. */
14804 if (!row->ends_in_ellipsis_p)
14805 cursor_row_p = row->continued_p;
14806 else
14807 /* If the row ends in an ellipsis, then
14808 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14809 We want that position to be displayed after the ellipsis. */
14810 cursor_row_p = 0;
14812 /* If the row ends at ZV, display the cursor at the end of that
14813 row instead of at the start of the row below. */
14814 else if (row->ends_at_zv_p)
14815 cursor_row_p = 1;
14816 else
14817 cursor_row_p = 0;
14820 return cursor_row_p;
14824 /* Construct the glyph row IT->glyph_row in the desired matrix of
14825 IT->w from text at the current position of IT. See dispextern.h
14826 for an overview of struct it. Value is non-zero if
14827 IT->glyph_row displays text, as opposed to a line displaying ZV
14828 only. */
14830 static int
14831 display_line (it)
14832 struct it *it;
14834 struct glyph_row *row = it->glyph_row;
14835 int overlay_arrow_bitmap;
14836 Lisp_Object overlay_arrow_string;
14838 /* We always start displaying at hpos zero even if hscrolled. */
14839 xassert (it->hpos == 0 && it->current_x == 0);
14841 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14842 >= it->w->desired_matrix->nrows)
14844 it->w->nrows_scale_factor++;
14845 fonts_changed_p = 1;
14846 return 0;
14849 /* Is IT->w showing the region? */
14850 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14852 /* Clear the result glyph row and enable it. */
14853 prepare_desired_row (row);
14855 row->y = it->current_y;
14856 row->start = it->start;
14857 row->continuation_lines_width = it->continuation_lines_width;
14858 row->displays_text_p = 1;
14859 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14860 it->starts_in_middle_of_char_p = 0;
14862 /* Arrange the overlays nicely for our purposes. Usually, we call
14863 display_line on only one line at a time, in which case this
14864 can't really hurt too much, or we call it on lines which appear
14865 one after another in the buffer, in which case all calls to
14866 recenter_overlay_lists but the first will be pretty cheap. */
14867 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14869 /* Move over display elements that are not visible because we are
14870 hscrolled. This may stop at an x-position < IT->first_visible_x
14871 if the first glyph is partially visible or if we hit a line end. */
14872 if (it->current_x < it->first_visible_x)
14874 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14875 MOVE_TO_POS | MOVE_TO_X);
14878 /* Get the initial row height. This is either the height of the
14879 text hscrolled, if there is any, or zero. */
14880 row->ascent = it->max_ascent;
14881 row->height = it->max_ascent + it->max_descent;
14882 row->phys_ascent = it->max_phys_ascent;
14883 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14884 row->extra_line_spacing = it->max_extra_line_spacing;
14886 /* Loop generating characters. The loop is left with IT on the next
14887 character to display. */
14888 while (1)
14890 int n_glyphs_before, hpos_before, x_before;
14891 int x, i, nglyphs;
14892 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14894 /* Retrieve the next thing to display. Value is zero if end of
14895 buffer reached. */
14896 if (!get_next_display_element (it))
14898 /* Maybe add a space at the end of this line that is used to
14899 display the cursor there under X. Set the charpos of the
14900 first glyph of blank lines not corresponding to any text
14901 to -1. */
14902 #ifdef HAVE_WINDOW_SYSTEM
14903 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14904 row->exact_window_width_line_p = 1;
14905 else
14906 #endif /* HAVE_WINDOW_SYSTEM */
14907 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14908 || row->used[TEXT_AREA] == 0)
14910 row->glyphs[TEXT_AREA]->charpos = -1;
14911 row->displays_text_p = 0;
14913 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14914 && (!MINI_WINDOW_P (it->w)
14915 || (minibuf_level && EQ (it->window, minibuf_window))))
14916 row->indicate_empty_line_p = 1;
14919 it->continuation_lines_width = 0;
14920 row->ends_at_zv_p = 1;
14921 break;
14924 /* Now, get the metrics of what we want to display. This also
14925 generates glyphs in `row' (which is IT->glyph_row). */
14926 n_glyphs_before = row->used[TEXT_AREA];
14927 x = it->current_x;
14929 /* Remember the line height so far in case the next element doesn't
14930 fit on the line. */
14931 if (!it->truncate_lines_p)
14933 ascent = it->max_ascent;
14934 descent = it->max_descent;
14935 phys_ascent = it->max_phys_ascent;
14936 phys_descent = it->max_phys_descent;
14939 PRODUCE_GLYPHS (it);
14941 /* If this display element was in marginal areas, continue with
14942 the next one. */
14943 if (it->area != TEXT_AREA)
14945 row->ascent = max (row->ascent, it->max_ascent);
14946 row->height = max (row->height, it->max_ascent + it->max_descent);
14947 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14948 row->phys_height = max (row->phys_height,
14949 it->max_phys_ascent + it->max_phys_descent);
14950 row->extra_line_spacing = max (row->extra_line_spacing,
14951 it->max_extra_line_spacing);
14952 set_iterator_to_next (it, 1);
14953 continue;
14956 /* Does the display element fit on the line? If we truncate
14957 lines, we should draw past the right edge of the window. If
14958 we don't truncate, we want to stop so that we can display the
14959 continuation glyph before the right margin. If lines are
14960 continued, there are two possible strategies for characters
14961 resulting in more than 1 glyph (e.g. tabs): Display as many
14962 glyphs as possible in this line and leave the rest for the
14963 continuation line, or display the whole element in the next
14964 line. Original redisplay did the former, so we do it also. */
14965 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14966 hpos_before = it->hpos;
14967 x_before = x;
14969 if (/* Not a newline. */
14970 nglyphs > 0
14971 /* Glyphs produced fit entirely in the line. */
14972 && it->current_x < it->last_visible_x)
14974 it->hpos += nglyphs;
14975 row->ascent = max (row->ascent, it->max_ascent);
14976 row->height = max (row->height, it->max_ascent + it->max_descent);
14977 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14978 row->phys_height = max (row->phys_height,
14979 it->max_phys_ascent + it->max_phys_descent);
14980 row->extra_line_spacing = max (row->extra_line_spacing,
14981 it->max_extra_line_spacing);
14982 if (it->current_x - it->pixel_width < it->first_visible_x)
14983 row->x = x - it->first_visible_x;
14985 else
14987 int new_x;
14988 struct glyph *glyph;
14990 for (i = 0; i < nglyphs; ++i, x = new_x)
14992 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14993 new_x = x + glyph->pixel_width;
14995 if (/* Lines are continued. */
14996 !it->truncate_lines_p
14997 && (/* Glyph doesn't fit on the line. */
14998 new_x > it->last_visible_x
14999 /* Or it fits exactly on a window system frame. */
15000 || (new_x == it->last_visible_x
15001 && FRAME_WINDOW_P (it->f))))
15003 /* End of a continued line. */
15005 if (it->hpos == 0
15006 || (new_x == it->last_visible_x
15007 && FRAME_WINDOW_P (it->f)))
15009 /* Current glyph is the only one on the line or
15010 fits exactly on the line. We must continue
15011 the line because we can't draw the cursor
15012 after the glyph. */
15013 row->continued_p = 1;
15014 it->current_x = new_x;
15015 it->continuation_lines_width += new_x;
15016 ++it->hpos;
15017 if (i == nglyphs - 1)
15019 set_iterator_to_next (it, 1);
15020 #ifdef HAVE_WINDOW_SYSTEM
15021 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15023 if (!get_next_display_element (it))
15025 row->exact_window_width_line_p = 1;
15026 it->continuation_lines_width = 0;
15027 row->continued_p = 0;
15028 row->ends_at_zv_p = 1;
15030 else if (ITERATOR_AT_END_OF_LINE_P (it))
15032 row->continued_p = 0;
15033 row->exact_window_width_line_p = 1;
15036 #endif /* HAVE_WINDOW_SYSTEM */
15039 else if (CHAR_GLYPH_PADDING_P (*glyph)
15040 && !FRAME_WINDOW_P (it->f))
15042 /* A padding glyph that doesn't fit on this line.
15043 This means the whole character doesn't fit
15044 on the line. */
15045 row->used[TEXT_AREA] = n_glyphs_before;
15047 /* Fill the rest of the row with continuation
15048 glyphs like in 20.x. */
15049 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15050 < row->glyphs[1 + TEXT_AREA])
15051 produce_special_glyphs (it, IT_CONTINUATION);
15053 row->continued_p = 1;
15054 it->current_x = x_before;
15055 it->continuation_lines_width += x_before;
15057 /* Restore the height to what it was before the
15058 element not fitting on the line. */
15059 it->max_ascent = ascent;
15060 it->max_descent = descent;
15061 it->max_phys_ascent = phys_ascent;
15062 it->max_phys_descent = phys_descent;
15064 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15066 /* A TAB that extends past the right edge of the
15067 window. This produces a single glyph on
15068 window system frames. We leave the glyph in
15069 this row and let it fill the row, but don't
15070 consume the TAB. */
15071 it->continuation_lines_width += it->last_visible_x;
15072 row->ends_in_middle_of_char_p = 1;
15073 row->continued_p = 1;
15074 glyph->pixel_width = it->last_visible_x - x;
15075 it->starts_in_middle_of_char_p = 1;
15077 else
15079 /* Something other than a TAB that draws past
15080 the right edge of the window. Restore
15081 positions to values before the element. */
15082 row->used[TEXT_AREA] = n_glyphs_before + i;
15084 /* Display continuation glyphs. */
15085 if (!FRAME_WINDOW_P (it->f))
15086 produce_special_glyphs (it, IT_CONTINUATION);
15087 row->continued_p = 1;
15089 it->continuation_lines_width += x;
15091 if (nglyphs > 1 && i > 0)
15093 row->ends_in_middle_of_char_p = 1;
15094 it->starts_in_middle_of_char_p = 1;
15097 /* Restore the height to what it was before the
15098 element not fitting on the line. */
15099 it->max_ascent = ascent;
15100 it->max_descent = descent;
15101 it->max_phys_ascent = phys_ascent;
15102 it->max_phys_descent = phys_descent;
15105 break;
15107 else if (new_x > it->first_visible_x)
15109 /* Increment number of glyphs actually displayed. */
15110 ++it->hpos;
15112 if (x < it->first_visible_x)
15113 /* Glyph is partially visible, i.e. row starts at
15114 negative X position. */
15115 row->x = x - it->first_visible_x;
15117 else
15119 /* Glyph is completely off the left margin of the
15120 window. This should not happen because of the
15121 move_it_in_display_line at the start of this
15122 function, unless the text display area of the
15123 window is empty. */
15124 xassert (it->first_visible_x <= it->last_visible_x);
15128 row->ascent = max (row->ascent, it->max_ascent);
15129 row->height = max (row->height, it->max_ascent + it->max_descent);
15130 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15131 row->phys_height = max (row->phys_height,
15132 it->max_phys_ascent + it->max_phys_descent);
15133 row->extra_line_spacing = max (row->extra_line_spacing,
15134 it->max_extra_line_spacing);
15136 /* End of this display line if row is continued. */
15137 if (row->continued_p || row->ends_at_zv_p)
15138 break;
15141 at_end_of_line:
15142 /* Is this a line end? If yes, we're also done, after making
15143 sure that a non-default face is extended up to the right
15144 margin of the window. */
15145 if (ITERATOR_AT_END_OF_LINE_P (it))
15147 int used_before = row->used[TEXT_AREA];
15149 row->ends_in_newline_from_string_p = STRINGP (it->object);
15151 #ifdef HAVE_WINDOW_SYSTEM
15152 /* Add a space at the end of the line that is used to
15153 display the cursor there. */
15154 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15155 append_space_for_newline (it, 0);
15156 #endif /* HAVE_WINDOW_SYSTEM */
15158 /* Extend the face to the end of the line. */
15159 extend_face_to_end_of_line (it);
15161 /* Make sure we have the position. */
15162 if (used_before == 0)
15163 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15165 /* Consume the line end. This skips over invisible lines. */
15166 set_iterator_to_next (it, 1);
15167 it->continuation_lines_width = 0;
15168 break;
15171 /* Proceed with next display element. Note that this skips
15172 over lines invisible because of selective display. */
15173 set_iterator_to_next (it, 1);
15175 /* If we truncate lines, we are done when the last displayed
15176 glyphs reach past the right margin of the window. */
15177 if (it->truncate_lines_p
15178 && (FRAME_WINDOW_P (it->f)
15179 ? (it->current_x >= it->last_visible_x)
15180 : (it->current_x > it->last_visible_x)))
15182 /* Maybe add truncation glyphs. */
15183 if (!FRAME_WINDOW_P (it->f))
15185 int i, n;
15187 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15188 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15189 break;
15191 for (n = row->used[TEXT_AREA]; i < n; ++i)
15193 row->used[TEXT_AREA] = i;
15194 produce_special_glyphs (it, IT_TRUNCATION);
15197 #ifdef HAVE_WINDOW_SYSTEM
15198 else
15200 /* Don't truncate if we can overflow newline into fringe. */
15201 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15203 if (!get_next_display_element (it))
15205 it->continuation_lines_width = 0;
15206 row->ends_at_zv_p = 1;
15207 row->exact_window_width_line_p = 1;
15208 break;
15210 if (ITERATOR_AT_END_OF_LINE_P (it))
15212 row->exact_window_width_line_p = 1;
15213 goto at_end_of_line;
15217 #endif /* HAVE_WINDOW_SYSTEM */
15219 row->truncated_on_right_p = 1;
15220 it->continuation_lines_width = 0;
15221 reseat_at_next_visible_line_start (it, 0);
15222 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15223 it->hpos = hpos_before;
15224 it->current_x = x_before;
15225 break;
15229 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15230 at the left window margin. */
15231 if (it->first_visible_x
15232 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15234 if (!FRAME_WINDOW_P (it->f))
15235 insert_left_trunc_glyphs (it);
15236 row->truncated_on_left_p = 1;
15239 /* If the start of this line is the overlay arrow-position, then
15240 mark this glyph row as the one containing the overlay arrow.
15241 This is clearly a mess with variable size fonts. It would be
15242 better to let it be displayed like cursors under X. */
15243 if (! overlay_arrow_seen
15244 && (overlay_arrow_string
15245 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15246 !NILP (overlay_arrow_string)))
15248 /* Overlay arrow in window redisplay is a fringe bitmap. */
15249 if (STRINGP (overlay_arrow_string))
15251 struct glyph_row *arrow_row
15252 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15253 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15254 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15255 struct glyph *p = row->glyphs[TEXT_AREA];
15256 struct glyph *p2, *end;
15258 /* Copy the arrow glyphs. */
15259 while (glyph < arrow_end)
15260 *p++ = *glyph++;
15262 /* Throw away padding glyphs. */
15263 p2 = p;
15264 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15265 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15266 ++p2;
15267 if (p2 > p)
15269 while (p2 < end)
15270 *p++ = *p2++;
15271 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15274 else
15276 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15277 row->overlay_arrow_p = 1;
15279 overlay_arrow_seen = 1;
15282 /* Compute pixel dimensions of this line. */
15283 compute_line_metrics (it);
15285 /* Remember the position at which this line ends. */
15286 row->end = it->current;
15288 /* Record whether this row ends inside an ellipsis. */
15289 row->ends_in_ellipsis_p
15290 = (it->method == GET_FROM_DISPLAY_VECTOR
15291 && it->ellipsis_p);
15293 /* Save fringe bitmaps in this row. */
15294 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15295 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15296 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15297 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15299 it->left_user_fringe_bitmap = 0;
15300 it->left_user_fringe_face_id = 0;
15301 it->right_user_fringe_bitmap = 0;
15302 it->right_user_fringe_face_id = 0;
15304 /* Maybe set the cursor. */
15305 if (it->w->cursor.vpos < 0
15306 && PT >= MATRIX_ROW_START_CHARPOS (row)
15307 && PT <= MATRIX_ROW_END_CHARPOS (row)
15308 && cursor_row_p (it->w, row))
15309 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15311 /* Highlight trailing whitespace. */
15312 if (!NILP (Vshow_trailing_whitespace))
15313 highlight_trailing_whitespace (it->f, it->glyph_row);
15315 /* Prepare for the next line. This line starts horizontally at (X
15316 HPOS) = (0 0). Vertical positions are incremented. As a
15317 convenience for the caller, IT->glyph_row is set to the next
15318 row to be used. */
15319 it->current_x = it->hpos = 0;
15320 it->current_y += row->height;
15321 ++it->vpos;
15322 ++it->glyph_row;
15323 it->start = it->current;
15324 return row->displays_text_p;
15329 /***********************************************************************
15330 Menu Bar
15331 ***********************************************************************/
15333 /* Redisplay the menu bar in the frame for window W.
15335 The menu bar of X frames that don't have X toolkit support is
15336 displayed in a special window W->frame->menu_bar_window.
15338 The menu bar of terminal frames is treated specially as far as
15339 glyph matrices are concerned. Menu bar lines are not part of
15340 windows, so the update is done directly on the frame matrix rows
15341 for the menu bar. */
15343 static void
15344 display_menu_bar (w)
15345 struct window *w;
15347 struct frame *f = XFRAME (WINDOW_FRAME (w));
15348 struct it it;
15349 Lisp_Object items;
15350 int i;
15352 /* Don't do all this for graphical frames. */
15353 #ifdef HAVE_NTGUI
15354 if (!NILP (Vwindow_system))
15355 return;
15356 #endif
15357 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15358 if (FRAME_X_P (f))
15359 return;
15360 #endif
15361 #ifdef MAC_OS
15362 if (FRAME_MAC_P (f))
15363 return;
15364 #endif
15366 #ifdef USE_X_TOOLKIT
15367 xassert (!FRAME_WINDOW_P (f));
15368 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15369 it.first_visible_x = 0;
15370 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15371 #else /* not USE_X_TOOLKIT */
15372 if (FRAME_WINDOW_P (f))
15374 /* Menu bar lines are displayed in the desired matrix of the
15375 dummy window menu_bar_window. */
15376 struct window *menu_w;
15377 xassert (WINDOWP (f->menu_bar_window));
15378 menu_w = XWINDOW (f->menu_bar_window);
15379 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15380 MENU_FACE_ID);
15381 it.first_visible_x = 0;
15382 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15384 else
15386 /* This is a TTY frame, i.e. character hpos/vpos are used as
15387 pixel x/y. */
15388 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15389 MENU_FACE_ID);
15390 it.first_visible_x = 0;
15391 it.last_visible_x = FRAME_COLS (f);
15393 #endif /* not USE_X_TOOLKIT */
15395 if (! mode_line_inverse_video)
15396 /* Force the menu-bar to be displayed in the default face. */
15397 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15399 /* Clear all rows of the menu bar. */
15400 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15402 struct glyph_row *row = it.glyph_row + i;
15403 clear_glyph_row (row);
15404 row->enabled_p = 1;
15405 row->full_width_p = 1;
15408 /* Display all items of the menu bar. */
15409 items = FRAME_MENU_BAR_ITEMS (it.f);
15410 for (i = 0; i < XVECTOR (items)->size; i += 4)
15412 Lisp_Object string;
15414 /* Stop at nil string. */
15415 string = AREF (items, i + 1);
15416 if (NILP (string))
15417 break;
15419 /* Remember where item was displayed. */
15420 AREF (items, i + 3) = make_number (it.hpos);
15422 /* Display the item, pad with one space. */
15423 if (it.current_x < it.last_visible_x)
15424 display_string (NULL, string, Qnil, 0, 0, &it,
15425 SCHARS (string) + 1, 0, 0, -1);
15428 /* Fill out the line with spaces. */
15429 if (it.current_x < it.last_visible_x)
15430 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15432 /* Compute the total height of the lines. */
15433 compute_line_metrics (&it);
15438 /***********************************************************************
15439 Mode Line
15440 ***********************************************************************/
15442 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15443 FORCE is non-zero, redisplay mode lines unconditionally.
15444 Otherwise, redisplay only mode lines that are garbaged. Value is
15445 the number of windows whose mode lines were redisplayed. */
15447 static int
15448 redisplay_mode_lines (window, force)
15449 Lisp_Object window;
15450 int force;
15452 int nwindows = 0;
15454 while (!NILP (window))
15456 struct window *w = XWINDOW (window);
15458 if (WINDOWP (w->hchild))
15459 nwindows += redisplay_mode_lines (w->hchild, force);
15460 else if (WINDOWP (w->vchild))
15461 nwindows += redisplay_mode_lines (w->vchild, force);
15462 else if (force
15463 || FRAME_GARBAGED_P (XFRAME (w->frame))
15464 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15466 struct text_pos lpoint;
15467 struct buffer *old = current_buffer;
15469 /* Set the window's buffer for the mode line display. */
15470 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15471 set_buffer_internal_1 (XBUFFER (w->buffer));
15473 /* Point refers normally to the selected window. For any
15474 other window, set up appropriate value. */
15475 if (!EQ (window, selected_window))
15477 struct text_pos pt;
15479 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15480 if (CHARPOS (pt) < BEGV)
15481 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15482 else if (CHARPOS (pt) > (ZV - 1))
15483 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15484 else
15485 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15488 /* Display mode lines. */
15489 clear_glyph_matrix (w->desired_matrix);
15490 if (display_mode_lines (w))
15492 ++nwindows;
15493 w->must_be_updated_p = 1;
15496 /* Restore old settings. */
15497 set_buffer_internal_1 (old);
15498 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15501 window = w->next;
15504 return nwindows;
15508 /* Display the mode and/or top line of window W. Value is the number
15509 of mode lines displayed. */
15511 static int
15512 display_mode_lines (w)
15513 struct window *w;
15515 Lisp_Object old_selected_window, old_selected_frame;
15516 int n = 0;
15518 old_selected_frame = selected_frame;
15519 selected_frame = w->frame;
15520 old_selected_window = selected_window;
15521 XSETWINDOW (selected_window, w);
15523 /* These will be set while the mode line specs are processed. */
15524 line_number_displayed = 0;
15525 w->column_number_displayed = Qnil;
15527 if (WINDOW_WANTS_MODELINE_P (w))
15529 struct window *sel_w = XWINDOW (old_selected_window);
15531 /* Select mode line face based on the real selected window. */
15532 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15533 current_buffer->mode_line_format);
15534 ++n;
15537 if (WINDOW_WANTS_HEADER_LINE_P (w))
15539 display_mode_line (w, HEADER_LINE_FACE_ID,
15540 current_buffer->header_line_format);
15541 ++n;
15544 selected_frame = old_selected_frame;
15545 selected_window = old_selected_window;
15546 return n;
15550 /* Display mode or top line of window W. FACE_ID specifies which line
15551 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15552 FORMAT is the mode line format to display. Value is the pixel
15553 height of the mode line displayed. */
15555 static int
15556 display_mode_line (w, face_id, format)
15557 struct window *w;
15558 enum face_id face_id;
15559 Lisp_Object format;
15561 struct it it;
15562 struct face *face;
15564 init_iterator (&it, w, -1, -1, NULL, face_id);
15565 prepare_desired_row (it.glyph_row);
15567 it.glyph_row->mode_line_p = 1;
15569 if (! mode_line_inverse_video)
15570 /* Force the mode-line to be displayed in the default face. */
15571 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15573 /* Temporarily make frame's keyboard the current kboard so that
15574 kboard-local variables in the mode_line_format will get the right
15575 values. */
15576 push_frame_kboard (it.f);
15577 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15578 pop_frame_kboard ();
15580 /* Fill up with spaces. */
15581 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15583 compute_line_metrics (&it);
15584 it.glyph_row->full_width_p = 1;
15585 it.glyph_row->continued_p = 0;
15586 it.glyph_row->truncated_on_left_p = 0;
15587 it.glyph_row->truncated_on_right_p = 0;
15589 /* Make a 3D mode-line have a shadow at its right end. */
15590 face = FACE_FROM_ID (it.f, face_id);
15591 extend_face_to_end_of_line (&it);
15592 if (face->box != FACE_NO_BOX)
15594 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15595 + it.glyph_row->used[TEXT_AREA] - 1);
15596 last->right_box_line_p = 1;
15599 return it.glyph_row->height;
15602 /* Alist that caches the results of :propertize.
15603 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15604 Lisp_Object mode_line_proptrans_alist;
15606 /* List of strings making up the mode-line. */
15607 Lisp_Object mode_line_string_list;
15609 /* Base face property when building propertized mode line string. */
15610 static Lisp_Object mode_line_string_face;
15611 static Lisp_Object mode_line_string_face_prop;
15614 /* Contribute ELT to the mode line for window IT->w. How it
15615 translates into text depends on its data type.
15617 IT describes the display environment in which we display, as usual.
15619 DEPTH is the depth in recursion. It is used to prevent
15620 infinite recursion here.
15622 FIELD_WIDTH is the number of characters the display of ELT should
15623 occupy in the mode line, and PRECISION is the maximum number of
15624 characters to display from ELT's representation. See
15625 display_string for details.
15627 Returns the hpos of the end of the text generated by ELT.
15629 PROPS is a property list to add to any string we encounter.
15631 If RISKY is nonzero, remove (disregard) any properties in any string
15632 we encounter, and ignore :eval and :propertize.
15634 If the global variable `frame_title_ptr' is non-NULL, then the output
15635 is passed to `store_frame_title' instead of `display_string'. */
15637 static int
15638 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15639 struct it *it;
15640 int depth;
15641 int field_width, precision;
15642 Lisp_Object elt, props;
15643 int risky;
15645 int n = 0, field, prec;
15646 int literal = 0;
15648 tail_recurse:
15649 if (depth > 100)
15650 elt = build_string ("*too-deep*");
15652 depth++;
15654 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15656 case Lisp_String:
15658 /* A string: output it and check for %-constructs within it. */
15659 unsigned char c;
15660 const unsigned char *this, *lisp_string;
15662 if (!NILP (props) || risky)
15664 Lisp_Object oprops, aelt;
15665 oprops = Ftext_properties_at (make_number (0), elt);
15667 /* If the starting string's properties are not what
15668 we want, translate the string. Also, if the string
15669 is risky, do that anyway. */
15671 if (NILP (Fequal (props, oprops)) || risky)
15673 /* If the starting string has properties,
15674 merge the specified ones onto the existing ones. */
15675 if (! NILP (oprops) && !risky)
15677 Lisp_Object tem;
15679 oprops = Fcopy_sequence (oprops);
15680 tem = props;
15681 while (CONSP (tem))
15683 oprops = Fplist_put (oprops, XCAR (tem),
15684 XCAR (XCDR (tem)));
15685 tem = XCDR (XCDR (tem));
15687 props = oprops;
15690 aelt = Fassoc (elt, mode_line_proptrans_alist);
15691 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15693 mode_line_proptrans_alist
15694 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15695 elt = XCAR (aelt);
15697 else
15699 Lisp_Object tem;
15701 elt = Fcopy_sequence (elt);
15702 Fset_text_properties (make_number (0), Flength (elt),
15703 props, elt);
15704 /* Add this item to mode_line_proptrans_alist. */
15705 mode_line_proptrans_alist
15706 = Fcons (Fcons (elt, props),
15707 mode_line_proptrans_alist);
15708 /* Truncate mode_line_proptrans_alist
15709 to at most 50 elements. */
15710 tem = Fnthcdr (make_number (50),
15711 mode_line_proptrans_alist);
15712 if (! NILP (tem))
15713 XSETCDR (tem, Qnil);
15718 this = SDATA (elt);
15719 lisp_string = this;
15721 if (literal)
15723 prec = precision - n;
15724 if (frame_title_ptr)
15725 n += store_frame_title (SDATA (elt), -1, prec);
15726 else if (!NILP (mode_line_string_list))
15727 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15728 else
15729 n += display_string (NULL, elt, Qnil, 0, 0, it,
15730 0, prec, 0, STRING_MULTIBYTE (elt));
15732 break;
15735 while ((precision <= 0 || n < precision)
15736 && *this
15737 && (frame_title_ptr
15738 || !NILP (mode_line_string_list)
15739 || it->current_x < it->last_visible_x))
15741 const unsigned char *last = this;
15743 /* Advance to end of string or next format specifier. */
15744 while ((c = *this++) != '\0' && c != '%')
15747 if (this - 1 != last)
15749 int nchars, nbytes;
15751 /* Output to end of string or up to '%'. Field width
15752 is length of string. Don't output more than
15753 PRECISION allows us. */
15754 --this;
15756 prec = c_string_width (last, this - last, precision - n,
15757 &nchars, &nbytes);
15759 if (frame_title_ptr)
15760 n += store_frame_title (last, 0, prec);
15761 else if (!NILP (mode_line_string_list))
15763 int bytepos = last - lisp_string;
15764 int charpos = string_byte_to_char (elt, bytepos);
15765 int endpos = (precision <= 0
15766 ? string_byte_to_char (elt,
15767 this - lisp_string)
15768 : charpos + nchars);
15770 n += store_mode_line_string (NULL,
15771 Fsubstring (elt, make_number (charpos),
15772 make_number (endpos)),
15773 0, 0, 0, Qnil);
15775 else
15777 int bytepos = last - lisp_string;
15778 int charpos = string_byte_to_char (elt, bytepos);
15779 n += display_string (NULL, elt, Qnil, 0, charpos,
15780 it, 0, prec, 0,
15781 STRING_MULTIBYTE (elt));
15784 else /* c == '%' */
15786 const unsigned char *percent_position = this;
15788 /* Get the specified minimum width. Zero means
15789 don't pad. */
15790 field = 0;
15791 while ((c = *this++) >= '0' && c <= '9')
15792 field = field * 10 + c - '0';
15794 /* Don't pad beyond the total padding allowed. */
15795 if (field_width - n > 0 && field > field_width - n)
15796 field = field_width - n;
15798 /* Note that either PRECISION <= 0 or N < PRECISION. */
15799 prec = precision - n;
15801 if (c == 'M')
15802 n += display_mode_element (it, depth, field, prec,
15803 Vglobal_mode_string, props,
15804 risky);
15805 else if (c != 0)
15807 int multibyte;
15808 int bytepos, charpos;
15809 unsigned char *spec;
15811 bytepos = percent_position - lisp_string;
15812 charpos = (STRING_MULTIBYTE (elt)
15813 ? string_byte_to_char (elt, bytepos)
15814 : bytepos);
15816 spec
15817 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15819 if (frame_title_ptr)
15820 n += store_frame_title (spec, field, prec);
15821 else if (!NILP (mode_line_string_list))
15823 int len = strlen (spec);
15824 Lisp_Object tem = make_string (spec, len);
15825 props = Ftext_properties_at (make_number (charpos), elt);
15826 /* Should only keep face property in props */
15827 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15829 else
15831 int nglyphs_before, nwritten;
15833 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15834 nwritten = display_string (spec, Qnil, elt,
15835 charpos, 0, it,
15836 field, prec, 0,
15837 multibyte);
15839 /* Assign to the glyphs written above the
15840 string where the `%x' came from, position
15841 of the `%'. */
15842 if (nwritten > 0)
15844 struct glyph *glyph
15845 = (it->glyph_row->glyphs[TEXT_AREA]
15846 + nglyphs_before);
15847 int i;
15849 for (i = 0; i < nwritten; ++i)
15851 glyph[i].object = elt;
15852 glyph[i].charpos = charpos;
15855 n += nwritten;
15859 else /* c == 0 */
15860 break;
15864 break;
15866 case Lisp_Symbol:
15867 /* A symbol: process the value of the symbol recursively
15868 as if it appeared here directly. Avoid error if symbol void.
15869 Special case: if value of symbol is a string, output the string
15870 literally. */
15872 register Lisp_Object tem;
15874 /* If the variable is not marked as risky to set
15875 then its contents are risky to use. */
15876 if (NILP (Fget (elt, Qrisky_local_variable)))
15877 risky = 1;
15879 tem = Fboundp (elt);
15880 if (!NILP (tem))
15882 tem = Fsymbol_value (elt);
15883 /* If value is a string, output that string literally:
15884 don't check for % within it. */
15885 if (STRINGP (tem))
15886 literal = 1;
15888 if (!EQ (tem, elt))
15890 /* Give up right away for nil or t. */
15891 elt = tem;
15892 goto tail_recurse;
15896 break;
15898 case Lisp_Cons:
15900 register Lisp_Object car, tem;
15902 /* A cons cell: five distinct cases.
15903 If first element is :eval or :propertize, do something special.
15904 If first element is a string or a cons, process all the elements
15905 and effectively concatenate them.
15906 If first element is a negative number, truncate displaying cdr to
15907 at most that many characters. If positive, pad (with spaces)
15908 to at least that many characters.
15909 If first element is a symbol, process the cadr or caddr recursively
15910 according to whether the symbol's value is non-nil or nil. */
15911 car = XCAR (elt);
15912 if (EQ (car, QCeval))
15914 /* An element of the form (:eval FORM) means evaluate FORM
15915 and use the result as mode line elements. */
15917 if (risky)
15918 break;
15920 if (CONSP (XCDR (elt)))
15922 Lisp_Object spec;
15923 spec = safe_eval (XCAR (XCDR (elt)));
15924 n += display_mode_element (it, depth, field_width - n,
15925 precision - n, spec, props,
15926 risky);
15929 else if (EQ (car, QCpropertize))
15931 /* An element of the form (:propertize ELT PROPS...)
15932 means display ELT but applying properties PROPS. */
15934 if (risky)
15935 break;
15937 if (CONSP (XCDR (elt)))
15938 n += display_mode_element (it, depth, field_width - n,
15939 precision - n, XCAR (XCDR (elt)),
15940 XCDR (XCDR (elt)), risky);
15942 else if (SYMBOLP (car))
15944 tem = Fboundp (car);
15945 elt = XCDR (elt);
15946 if (!CONSP (elt))
15947 goto invalid;
15948 /* elt is now the cdr, and we know it is a cons cell.
15949 Use its car if CAR has a non-nil value. */
15950 if (!NILP (tem))
15952 tem = Fsymbol_value (car);
15953 if (!NILP (tem))
15955 elt = XCAR (elt);
15956 goto tail_recurse;
15959 /* Symbol's value is nil (or symbol is unbound)
15960 Get the cddr of the original list
15961 and if possible find the caddr and use that. */
15962 elt = XCDR (elt);
15963 if (NILP (elt))
15964 break;
15965 else if (!CONSP (elt))
15966 goto invalid;
15967 elt = XCAR (elt);
15968 goto tail_recurse;
15970 else if (INTEGERP (car))
15972 register int lim = XINT (car);
15973 elt = XCDR (elt);
15974 if (lim < 0)
15976 /* Negative int means reduce maximum width. */
15977 if (precision <= 0)
15978 precision = -lim;
15979 else
15980 precision = min (precision, -lim);
15982 else if (lim > 0)
15984 /* Padding specified. Don't let it be more than
15985 current maximum. */
15986 if (precision > 0)
15987 lim = min (precision, lim);
15989 /* If that's more padding than already wanted, queue it.
15990 But don't reduce padding already specified even if
15991 that is beyond the current truncation point. */
15992 field_width = max (lim, field_width);
15994 goto tail_recurse;
15996 else if (STRINGP (car) || CONSP (car))
15998 register int limit = 50;
15999 /* Limit is to protect against circular lists. */
16000 while (CONSP (elt)
16001 && --limit > 0
16002 && (precision <= 0 || n < precision))
16004 n += display_mode_element (it, depth, field_width - n,
16005 precision - n, XCAR (elt),
16006 props, risky);
16007 elt = XCDR (elt);
16011 break;
16013 default:
16014 invalid:
16015 elt = build_string ("*invalid*");
16016 goto tail_recurse;
16019 /* Pad to FIELD_WIDTH. */
16020 if (field_width > 0 && n < field_width)
16022 if (frame_title_ptr)
16023 n += store_frame_title ("", field_width - n, 0);
16024 else if (!NILP (mode_line_string_list))
16025 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16026 else
16027 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16028 0, 0, 0);
16031 return n;
16034 /* Store a mode-line string element in mode_line_string_list.
16036 If STRING is non-null, display that C string. Otherwise, the Lisp
16037 string LISP_STRING is displayed.
16039 FIELD_WIDTH is the minimum number of output glyphs to produce.
16040 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16041 with spaces. FIELD_WIDTH <= 0 means don't pad.
16043 PRECISION is the maximum number of characters to output from
16044 STRING. PRECISION <= 0 means don't truncate the string.
16046 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16047 properties to the string.
16049 PROPS are the properties to add to the string.
16050 The mode_line_string_face face property is always added to the string.
16053 static int
16054 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16055 char *string;
16056 Lisp_Object lisp_string;
16057 int copy_string;
16058 int field_width;
16059 int precision;
16060 Lisp_Object props;
16062 int len;
16063 int n = 0;
16065 if (string != NULL)
16067 len = strlen (string);
16068 if (precision > 0 && len > precision)
16069 len = precision;
16070 lisp_string = make_string (string, len);
16071 if (NILP (props))
16072 props = mode_line_string_face_prop;
16073 else if (!NILP (mode_line_string_face))
16075 Lisp_Object face = Fsafe_plist_get (props, Qface);
16076 props = Fcopy_sequence (props);
16077 if (NILP (face))
16078 face = mode_line_string_face;
16079 else
16080 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16081 props = Fplist_put (props, Qface, face);
16083 Fadd_text_properties (make_number (0), make_number (len),
16084 props, lisp_string);
16086 else
16088 len = XFASTINT (Flength (lisp_string));
16089 if (precision > 0 && len > precision)
16091 len = precision;
16092 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16093 precision = -1;
16095 if (!NILP (mode_line_string_face))
16097 Lisp_Object face;
16098 if (NILP (props))
16099 props = Ftext_properties_at (make_number (0), lisp_string);
16100 face = Fsafe_plist_get (props, Qface);
16101 if (NILP (face))
16102 face = mode_line_string_face;
16103 else
16104 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16105 props = Fcons (Qface, Fcons (face, Qnil));
16106 if (copy_string)
16107 lisp_string = Fcopy_sequence (lisp_string);
16109 if (!NILP (props))
16110 Fadd_text_properties (make_number (0), make_number (len),
16111 props, lisp_string);
16114 if (len > 0)
16116 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16117 n += len;
16120 if (field_width > len)
16122 field_width -= len;
16123 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16124 if (!NILP (props))
16125 Fadd_text_properties (make_number (0), make_number (field_width),
16126 props, lisp_string);
16127 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16128 n += field_width;
16131 return n;
16135 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16136 1, 4, 0,
16137 doc: /* Format a string out of a mode line format specification.
16138 First arg FORMAT specifies the mode line format (see `mode-line-format'
16139 for details) to use.
16141 Optional second arg FACE specifies the face property to put
16142 on all characters for which no face is specified.
16143 t means whatever face the window's mode line currently uses
16144 \(either `mode-line' or `mode-line-inactive', depending).
16145 nil means the default is no face property.
16146 If FACE is an integer, the value string has no text properties.
16148 Optional third and fourth args WINDOW and BUFFER specify the window
16149 and buffer to use as the context for the formatting (defaults
16150 are the selected window and the window's buffer). */)
16151 (format, face, window, buffer)
16152 Lisp_Object format, face, window, buffer;
16154 struct it it;
16155 int len;
16156 struct window *w;
16157 struct buffer *old_buffer = NULL;
16158 int face_id = -1;
16159 int no_props = INTEGERP (face);
16161 if (NILP (window))
16162 window = selected_window;
16163 CHECK_WINDOW (window);
16164 w = XWINDOW (window);
16166 if (NILP (buffer))
16167 buffer = w->buffer;
16168 CHECK_BUFFER (buffer);
16170 if (NILP (format))
16171 return build_string ("");
16173 if (no_props)
16174 face = Qnil;
16176 if (!NILP (face))
16178 if (EQ (face, Qt))
16179 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16180 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16183 if (face_id < 0)
16184 face_id = DEFAULT_FACE_ID;
16186 if (XBUFFER (buffer) != current_buffer)
16188 old_buffer = current_buffer;
16189 set_buffer_internal_1 (XBUFFER (buffer));
16192 init_iterator (&it, w, -1, -1, NULL, face_id);
16194 if (!no_props)
16196 mode_line_string_face = face;
16197 mode_line_string_face_prop
16198 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16200 /* We need a dummy last element in mode_line_string_list to
16201 indicate we are building the propertized mode-line string.
16202 Using mode_line_string_face_prop here GC protects it. */
16203 mode_line_string_list
16204 = Fcons (mode_line_string_face_prop, Qnil);
16205 frame_title_ptr = NULL;
16207 else
16209 mode_line_string_face_prop = Qnil;
16210 mode_line_string_list = Qnil;
16211 frame_title_ptr = frame_title_buf;
16214 push_frame_kboard (it.f);
16215 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16216 pop_frame_kboard ();
16218 if (old_buffer)
16219 set_buffer_internal_1 (old_buffer);
16221 if (!no_props)
16223 Lisp_Object str;
16224 mode_line_string_list = Fnreverse (mode_line_string_list);
16225 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16226 make_string ("", 0));
16227 mode_line_string_face_prop = Qnil;
16228 mode_line_string_list = Qnil;
16229 return str;
16232 len = frame_title_ptr - frame_title_buf;
16233 if (len > 0 && frame_title_ptr[-1] == '-')
16235 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16236 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16238 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16239 if (len > frame_title_ptr - frame_title_buf)
16240 len = frame_title_ptr - frame_title_buf;
16243 frame_title_ptr = NULL;
16244 return make_string (frame_title_buf, len);
16247 /* Write a null-terminated, right justified decimal representation of
16248 the positive integer D to BUF using a minimal field width WIDTH. */
16250 static void
16251 pint2str (buf, width, d)
16252 register char *buf;
16253 register int width;
16254 register int d;
16256 register char *p = buf;
16258 if (d <= 0)
16259 *p++ = '0';
16260 else
16262 while (d > 0)
16264 *p++ = d % 10 + '0';
16265 d /= 10;
16269 for (width -= (int) (p - buf); width > 0; --width)
16270 *p++ = ' ';
16271 *p-- = '\0';
16272 while (p > buf)
16274 d = *buf;
16275 *buf++ = *p;
16276 *p-- = d;
16280 /* Write a null-terminated, right justified decimal and "human
16281 readable" representation of the nonnegative integer D to BUF using
16282 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16284 static const char power_letter[] =
16286 0, /* not used */
16287 'k', /* kilo */
16288 'M', /* mega */
16289 'G', /* giga */
16290 'T', /* tera */
16291 'P', /* peta */
16292 'E', /* exa */
16293 'Z', /* zetta */
16294 'Y' /* yotta */
16297 static void
16298 pint2hrstr (buf, width, d)
16299 char *buf;
16300 int width;
16301 int d;
16303 /* We aim to represent the nonnegative integer D as
16304 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16305 int quotient = d;
16306 int remainder = 0;
16307 /* -1 means: do not use TENTHS. */
16308 int tenths = -1;
16309 int exponent = 0;
16311 /* Length of QUOTIENT.TENTHS as a string. */
16312 int length;
16314 char * psuffix;
16315 char * p;
16317 if (1000 <= quotient)
16319 /* Scale to the appropriate EXPONENT. */
16322 remainder = quotient % 1000;
16323 quotient /= 1000;
16324 exponent++;
16326 while (1000 <= quotient);
16328 /* Round to nearest and decide whether to use TENTHS or not. */
16329 if (quotient <= 9)
16331 tenths = remainder / 100;
16332 if (50 <= remainder % 100)
16334 if (tenths < 9)
16335 tenths++;
16336 else
16338 quotient++;
16339 if (quotient == 10)
16340 tenths = -1;
16341 else
16342 tenths = 0;
16346 else
16347 if (500 <= remainder)
16349 if (quotient < 999)
16350 quotient++;
16351 else
16353 quotient = 1;
16354 exponent++;
16355 tenths = 0;
16360 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16361 if (tenths == -1 && quotient <= 99)
16362 if (quotient <= 9)
16363 length = 1;
16364 else
16365 length = 2;
16366 else
16367 length = 3;
16368 p = psuffix = buf + max (width, length);
16370 /* Print EXPONENT. */
16371 if (exponent)
16372 *psuffix++ = power_letter[exponent];
16373 *psuffix = '\0';
16375 /* Print TENTHS. */
16376 if (tenths >= 0)
16378 *--p = '0' + tenths;
16379 *--p = '.';
16382 /* Print QUOTIENT. */
16385 int digit = quotient % 10;
16386 *--p = '0' + digit;
16388 while ((quotient /= 10) != 0);
16390 /* Print leading spaces. */
16391 while (buf < p)
16392 *--p = ' ';
16395 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16396 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16397 type of CODING_SYSTEM. Return updated pointer into BUF. */
16399 static unsigned char invalid_eol_type[] = "(*invalid*)";
16401 static char *
16402 decode_mode_spec_coding (coding_system, buf, eol_flag)
16403 Lisp_Object coding_system;
16404 register char *buf;
16405 int eol_flag;
16407 Lisp_Object val;
16408 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16409 const unsigned char *eol_str;
16410 int eol_str_len;
16411 /* The EOL conversion we are using. */
16412 Lisp_Object eoltype;
16414 val = Fget (coding_system, Qcoding_system);
16415 eoltype = Qnil;
16417 if (!VECTORP (val)) /* Not yet decided. */
16419 if (multibyte)
16420 *buf++ = '-';
16421 if (eol_flag)
16422 eoltype = eol_mnemonic_undecided;
16423 /* Don't mention EOL conversion if it isn't decided. */
16425 else
16427 Lisp_Object eolvalue;
16429 eolvalue = Fget (coding_system, Qeol_type);
16431 if (multibyte)
16432 *buf++ = XFASTINT (AREF (val, 1));
16434 if (eol_flag)
16436 /* The EOL conversion that is normal on this system. */
16438 if (NILP (eolvalue)) /* Not yet decided. */
16439 eoltype = eol_mnemonic_undecided;
16440 else if (VECTORP (eolvalue)) /* Not yet decided. */
16441 eoltype = eol_mnemonic_undecided;
16442 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16443 eoltype = (XFASTINT (eolvalue) == 0
16444 ? eol_mnemonic_unix
16445 : (XFASTINT (eolvalue) == 1
16446 ? eol_mnemonic_dos : eol_mnemonic_mac));
16450 if (eol_flag)
16452 /* Mention the EOL conversion if it is not the usual one. */
16453 if (STRINGP (eoltype))
16455 eol_str = SDATA (eoltype);
16456 eol_str_len = SBYTES (eoltype);
16458 else if (INTEGERP (eoltype)
16459 && CHAR_VALID_P (XINT (eoltype), 0))
16461 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16462 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16463 eol_str = tmp;
16465 else
16467 eol_str = invalid_eol_type;
16468 eol_str_len = sizeof (invalid_eol_type) - 1;
16470 bcopy (eol_str, buf, eol_str_len);
16471 buf += eol_str_len;
16474 return buf;
16477 /* Return a string for the output of a mode line %-spec for window W,
16478 generated by character C. PRECISION >= 0 means don't return a
16479 string longer than that value. FIELD_WIDTH > 0 means pad the
16480 string returned with spaces to that value. Return 1 in *MULTIBYTE
16481 if the result is multibyte text.
16483 Note we operate on the current buffer for most purposes,
16484 the exception being w->base_line_pos. */
16486 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16488 static char *
16489 decode_mode_spec (w, c, field_width, precision, multibyte)
16490 struct window *w;
16491 register int c;
16492 int field_width, precision;
16493 int *multibyte;
16495 Lisp_Object obj;
16496 struct frame *f = XFRAME (WINDOW_FRAME (w));
16497 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16498 struct buffer *b = current_buffer;
16500 obj = Qnil;
16501 *multibyte = 0;
16503 switch (c)
16505 case '*':
16506 if (!NILP (b->read_only))
16507 return "%";
16508 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16509 return "*";
16510 return "-";
16512 case '+':
16513 /* This differs from %* only for a modified read-only buffer. */
16514 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16515 return "*";
16516 if (!NILP (b->read_only))
16517 return "%";
16518 return "-";
16520 case '&':
16521 /* This differs from %* in ignoring read-only-ness. */
16522 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16523 return "*";
16524 return "-";
16526 case '%':
16527 return "%";
16529 case '[':
16531 int i;
16532 char *p;
16534 if (command_loop_level > 5)
16535 return "[[[... ";
16536 p = decode_mode_spec_buf;
16537 for (i = 0; i < command_loop_level; i++)
16538 *p++ = '[';
16539 *p = 0;
16540 return decode_mode_spec_buf;
16543 case ']':
16545 int i;
16546 char *p;
16548 if (command_loop_level > 5)
16549 return " ...]]]";
16550 p = decode_mode_spec_buf;
16551 for (i = 0; i < command_loop_level; i++)
16552 *p++ = ']';
16553 *p = 0;
16554 return decode_mode_spec_buf;
16557 case '-':
16559 register int i;
16561 /* Let lots_of_dashes be a string of infinite length. */
16562 if (!NILP (mode_line_string_list))
16563 return "--";
16564 if (field_width <= 0
16565 || field_width > sizeof (lots_of_dashes))
16567 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16568 decode_mode_spec_buf[i] = '-';
16569 decode_mode_spec_buf[i] = '\0';
16570 return decode_mode_spec_buf;
16572 else
16573 return lots_of_dashes;
16576 case 'b':
16577 obj = b->name;
16578 break;
16580 case 'c':
16582 int col = (int) current_column (); /* iftc */
16583 w->column_number_displayed = make_number (col);
16584 pint2str (decode_mode_spec_buf, field_width, col);
16585 return decode_mode_spec_buf;
16588 case 'F':
16589 /* %F displays the frame name. */
16590 if (!NILP (f->title))
16591 return (char *) SDATA (f->title);
16592 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16593 return (char *) SDATA (f->name);
16594 return "Emacs";
16596 case 'f':
16597 obj = b->filename;
16598 break;
16600 case 'i':
16602 int size = ZV - BEGV;
16603 pint2str (decode_mode_spec_buf, field_width, size);
16604 return decode_mode_spec_buf;
16607 case 'I':
16609 int size = ZV - BEGV;
16610 pint2hrstr (decode_mode_spec_buf, field_width, size);
16611 return decode_mode_spec_buf;
16614 case 'l':
16616 int startpos = XMARKER (w->start)->charpos;
16617 int startpos_byte = marker_byte_position (w->start);
16618 int line, linepos, linepos_byte, topline;
16619 int nlines, junk;
16620 int height = WINDOW_TOTAL_LINES (w);
16622 /* If we decided that this buffer isn't suitable for line numbers,
16623 don't forget that too fast. */
16624 if (EQ (w->base_line_pos, w->buffer))
16625 goto no_value;
16626 /* But do forget it, if the window shows a different buffer now. */
16627 else if (BUFFERP (w->base_line_pos))
16628 w->base_line_pos = Qnil;
16630 /* If the buffer is very big, don't waste time. */
16631 if (INTEGERP (Vline_number_display_limit)
16632 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16634 w->base_line_pos = Qnil;
16635 w->base_line_number = Qnil;
16636 goto no_value;
16639 if (!NILP (w->base_line_number)
16640 && !NILP (w->base_line_pos)
16641 && XFASTINT (w->base_line_pos) <= startpos)
16643 line = XFASTINT (w->base_line_number);
16644 linepos = XFASTINT (w->base_line_pos);
16645 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16647 else
16649 line = 1;
16650 linepos = BUF_BEGV (b);
16651 linepos_byte = BUF_BEGV_BYTE (b);
16654 /* Count lines from base line to window start position. */
16655 nlines = display_count_lines (linepos, linepos_byte,
16656 startpos_byte,
16657 startpos, &junk);
16659 topline = nlines + line;
16661 /* Determine a new base line, if the old one is too close
16662 or too far away, or if we did not have one.
16663 "Too close" means it's plausible a scroll-down would
16664 go back past it. */
16665 if (startpos == BUF_BEGV (b))
16667 w->base_line_number = make_number (topline);
16668 w->base_line_pos = make_number (BUF_BEGV (b));
16670 else if (nlines < height + 25 || nlines > height * 3 + 50
16671 || linepos == BUF_BEGV (b))
16673 int limit = BUF_BEGV (b);
16674 int limit_byte = BUF_BEGV_BYTE (b);
16675 int position;
16676 int distance = (height * 2 + 30) * line_number_display_limit_width;
16678 if (startpos - distance > limit)
16680 limit = startpos - distance;
16681 limit_byte = CHAR_TO_BYTE (limit);
16684 nlines = display_count_lines (startpos, startpos_byte,
16685 limit_byte,
16686 - (height * 2 + 30),
16687 &position);
16688 /* If we couldn't find the lines we wanted within
16689 line_number_display_limit_width chars per line,
16690 give up on line numbers for this window. */
16691 if (position == limit_byte && limit == startpos - distance)
16693 w->base_line_pos = w->buffer;
16694 w->base_line_number = Qnil;
16695 goto no_value;
16698 w->base_line_number = make_number (topline - nlines);
16699 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16702 /* Now count lines from the start pos to point. */
16703 nlines = display_count_lines (startpos, startpos_byte,
16704 PT_BYTE, PT, &junk);
16706 /* Record that we did display the line number. */
16707 line_number_displayed = 1;
16709 /* Make the string to show. */
16710 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16711 return decode_mode_spec_buf;
16712 no_value:
16714 char* p = decode_mode_spec_buf;
16715 int pad = field_width - 2;
16716 while (pad-- > 0)
16717 *p++ = ' ';
16718 *p++ = '?';
16719 *p++ = '?';
16720 *p = '\0';
16721 return decode_mode_spec_buf;
16724 break;
16726 case 'm':
16727 obj = b->mode_name;
16728 break;
16730 case 'n':
16731 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16732 return " Narrow";
16733 break;
16735 case 'p':
16737 int pos = marker_position (w->start);
16738 int total = BUF_ZV (b) - BUF_BEGV (b);
16740 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16742 if (pos <= BUF_BEGV (b))
16743 return "All";
16744 else
16745 return "Bottom";
16747 else if (pos <= BUF_BEGV (b))
16748 return "Top";
16749 else
16751 if (total > 1000000)
16752 /* Do it differently for a large value, to avoid overflow. */
16753 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16754 else
16755 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16756 /* We can't normally display a 3-digit number,
16757 so get us a 2-digit number that is close. */
16758 if (total == 100)
16759 total = 99;
16760 sprintf (decode_mode_spec_buf, "%2d%%", total);
16761 return decode_mode_spec_buf;
16765 /* Display percentage of size above the bottom of the screen. */
16766 case 'P':
16768 int toppos = marker_position (w->start);
16769 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16770 int total = BUF_ZV (b) - BUF_BEGV (b);
16772 if (botpos >= BUF_ZV (b))
16774 if (toppos <= BUF_BEGV (b))
16775 return "All";
16776 else
16777 return "Bottom";
16779 else
16781 if (total > 1000000)
16782 /* Do it differently for a large value, to avoid overflow. */
16783 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16784 else
16785 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16786 /* We can't normally display a 3-digit number,
16787 so get us a 2-digit number that is close. */
16788 if (total == 100)
16789 total = 99;
16790 if (toppos <= BUF_BEGV (b))
16791 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16792 else
16793 sprintf (decode_mode_spec_buf, "%2d%%", total);
16794 return decode_mode_spec_buf;
16798 case 's':
16799 /* status of process */
16800 obj = Fget_buffer_process (Fcurrent_buffer ());
16801 if (NILP (obj))
16802 return "no process";
16803 #ifdef subprocesses
16804 obj = Fsymbol_name (Fprocess_status (obj));
16805 #endif
16806 break;
16808 case 't': /* indicate TEXT or BINARY */
16809 #ifdef MODE_LINE_BINARY_TEXT
16810 return MODE_LINE_BINARY_TEXT (b);
16811 #else
16812 return "T";
16813 #endif
16815 case 'z':
16816 /* coding-system (not including end-of-line format) */
16817 case 'Z':
16818 /* coding-system (including end-of-line type) */
16820 int eol_flag = (c == 'Z');
16821 char *p = decode_mode_spec_buf;
16823 if (! FRAME_WINDOW_P (f))
16825 /* No need to mention EOL here--the terminal never needs
16826 to do EOL conversion. */
16827 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16828 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16830 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16831 p, eol_flag);
16833 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16834 #ifdef subprocesses
16835 obj = Fget_buffer_process (Fcurrent_buffer ());
16836 if (PROCESSP (obj))
16838 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16839 p, eol_flag);
16840 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16841 p, eol_flag);
16843 #endif /* subprocesses */
16844 #endif /* 0 */
16845 *p = 0;
16846 return decode_mode_spec_buf;
16850 if (STRINGP (obj))
16852 *multibyte = STRING_MULTIBYTE (obj);
16853 return (char *) SDATA (obj);
16855 else
16856 return "";
16860 /* Count up to COUNT lines starting from START / START_BYTE.
16861 But don't go beyond LIMIT_BYTE.
16862 Return the number of lines thus found (always nonnegative).
16864 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16866 static int
16867 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16868 int start, start_byte, limit_byte, count;
16869 int *byte_pos_ptr;
16871 register unsigned char *cursor;
16872 unsigned char *base;
16874 register int ceiling;
16875 register unsigned char *ceiling_addr;
16876 int orig_count = count;
16878 /* If we are not in selective display mode,
16879 check only for newlines. */
16880 int selective_display = (!NILP (current_buffer->selective_display)
16881 && !INTEGERP (current_buffer->selective_display));
16883 if (count > 0)
16885 while (start_byte < limit_byte)
16887 ceiling = BUFFER_CEILING_OF (start_byte);
16888 ceiling = min (limit_byte - 1, ceiling);
16889 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16890 base = (cursor = BYTE_POS_ADDR (start_byte));
16891 while (1)
16893 if (selective_display)
16894 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16896 else
16897 while (*cursor != '\n' && ++cursor != ceiling_addr)
16900 if (cursor != ceiling_addr)
16902 if (--count == 0)
16904 start_byte += cursor - base + 1;
16905 *byte_pos_ptr = start_byte;
16906 return orig_count;
16908 else
16909 if (++cursor == ceiling_addr)
16910 break;
16912 else
16913 break;
16915 start_byte += cursor - base;
16918 else
16920 while (start_byte > limit_byte)
16922 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16923 ceiling = max (limit_byte, ceiling);
16924 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16925 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16926 while (1)
16928 if (selective_display)
16929 while (--cursor != ceiling_addr
16930 && *cursor != '\n' && *cursor != 015)
16932 else
16933 while (--cursor != ceiling_addr && *cursor != '\n')
16936 if (cursor != ceiling_addr)
16938 if (++count == 0)
16940 start_byte += cursor - base + 1;
16941 *byte_pos_ptr = start_byte;
16942 /* When scanning backwards, we should
16943 not count the newline posterior to which we stop. */
16944 return - orig_count - 1;
16947 else
16948 break;
16950 /* Here we add 1 to compensate for the last decrement
16951 of CURSOR, which took it past the valid range. */
16952 start_byte += cursor - base + 1;
16956 *byte_pos_ptr = limit_byte;
16958 if (count < 0)
16959 return - orig_count + count;
16960 return orig_count - count;
16966 /***********************************************************************
16967 Displaying strings
16968 ***********************************************************************/
16970 /* Display a NUL-terminated string, starting with index START.
16972 If STRING is non-null, display that C string. Otherwise, the Lisp
16973 string LISP_STRING is displayed.
16975 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16976 FACE_STRING. Display STRING or LISP_STRING with the face at
16977 FACE_STRING_POS in FACE_STRING:
16979 Display the string in the environment given by IT, but use the
16980 standard display table, temporarily.
16982 FIELD_WIDTH is the minimum number of output glyphs to produce.
16983 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16984 with spaces. If STRING has more characters, more than FIELD_WIDTH
16985 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16987 PRECISION is the maximum number of characters to output from
16988 STRING. PRECISION < 0 means don't truncate the string.
16990 This is roughly equivalent to printf format specifiers:
16992 FIELD_WIDTH PRECISION PRINTF
16993 ----------------------------------------
16994 -1 -1 %s
16995 -1 10 %.10s
16996 10 -1 %10s
16997 20 10 %20.10s
16999 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17000 display them, and < 0 means obey the current buffer's value of
17001 enable_multibyte_characters.
17003 Value is the number of glyphs produced. */
17005 static int
17006 display_string (string, lisp_string, face_string, face_string_pos,
17007 start, it, field_width, precision, max_x, multibyte)
17008 unsigned char *string;
17009 Lisp_Object lisp_string;
17010 Lisp_Object face_string;
17011 int face_string_pos;
17012 int start;
17013 struct it *it;
17014 int field_width, precision, max_x;
17015 int multibyte;
17017 int hpos_at_start = it->hpos;
17018 int saved_face_id = it->face_id;
17019 struct glyph_row *row = it->glyph_row;
17021 /* Initialize the iterator IT for iteration over STRING beginning
17022 with index START. */
17023 reseat_to_string (it, string, lisp_string, start,
17024 precision, field_width, multibyte);
17026 /* If displaying STRING, set up the face of the iterator
17027 from LISP_STRING, if that's given. */
17028 if (STRINGP (face_string))
17030 int endptr;
17031 struct face *face;
17033 it->face_id
17034 = face_at_string_position (it->w, face_string, face_string_pos,
17035 0, it->region_beg_charpos,
17036 it->region_end_charpos,
17037 &endptr, it->base_face_id, 0);
17038 face = FACE_FROM_ID (it->f, it->face_id);
17039 it->face_box_p = face->box != FACE_NO_BOX;
17042 /* Set max_x to the maximum allowed X position. Don't let it go
17043 beyond the right edge of the window. */
17044 if (max_x <= 0)
17045 max_x = it->last_visible_x;
17046 else
17047 max_x = min (max_x, it->last_visible_x);
17049 /* Skip over display elements that are not visible. because IT->w is
17050 hscrolled. */
17051 if (it->current_x < it->first_visible_x)
17052 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17053 MOVE_TO_POS | MOVE_TO_X);
17055 row->ascent = it->max_ascent;
17056 row->height = it->max_ascent + it->max_descent;
17057 row->phys_ascent = it->max_phys_ascent;
17058 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17059 row->extra_line_spacing = it->max_extra_line_spacing;
17061 /* This condition is for the case that we are called with current_x
17062 past last_visible_x. */
17063 while (it->current_x < max_x)
17065 int x_before, x, n_glyphs_before, i, nglyphs;
17067 /* Get the next display element. */
17068 if (!get_next_display_element (it))
17069 break;
17071 /* Produce glyphs. */
17072 x_before = it->current_x;
17073 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17074 PRODUCE_GLYPHS (it);
17076 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17077 i = 0;
17078 x = x_before;
17079 while (i < nglyphs)
17081 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17083 if (!it->truncate_lines_p
17084 && x + glyph->pixel_width > max_x)
17086 /* End of continued line or max_x reached. */
17087 if (CHAR_GLYPH_PADDING_P (*glyph))
17089 /* A wide character is unbreakable. */
17090 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17091 it->current_x = x_before;
17093 else
17095 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17096 it->current_x = x;
17098 break;
17100 else if (x + glyph->pixel_width > it->first_visible_x)
17102 /* Glyph is at least partially visible. */
17103 ++it->hpos;
17104 if (x < it->first_visible_x)
17105 it->glyph_row->x = x - it->first_visible_x;
17107 else
17109 /* Glyph is off the left margin of the display area.
17110 Should not happen. */
17111 abort ();
17114 row->ascent = max (row->ascent, it->max_ascent);
17115 row->height = max (row->height, it->max_ascent + it->max_descent);
17116 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17117 row->phys_height = max (row->phys_height,
17118 it->max_phys_ascent + it->max_phys_descent);
17119 row->extra_line_spacing = max (row->extra_line_spacing,
17120 it->max_extra_line_spacing);
17121 x += glyph->pixel_width;
17122 ++i;
17125 /* Stop if max_x reached. */
17126 if (i < nglyphs)
17127 break;
17129 /* Stop at line ends. */
17130 if (ITERATOR_AT_END_OF_LINE_P (it))
17132 it->continuation_lines_width = 0;
17133 break;
17136 set_iterator_to_next (it, 1);
17138 /* Stop if truncating at the right edge. */
17139 if (it->truncate_lines_p
17140 && it->current_x >= it->last_visible_x)
17142 /* Add truncation mark, but don't do it if the line is
17143 truncated at a padding space. */
17144 if (IT_CHARPOS (*it) < it->string_nchars)
17146 if (!FRAME_WINDOW_P (it->f))
17148 int i, n;
17150 if (it->current_x > it->last_visible_x)
17152 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17153 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17154 break;
17155 for (n = row->used[TEXT_AREA]; i < n; ++i)
17157 row->used[TEXT_AREA] = i;
17158 produce_special_glyphs (it, IT_TRUNCATION);
17161 produce_special_glyphs (it, IT_TRUNCATION);
17163 it->glyph_row->truncated_on_right_p = 1;
17165 break;
17169 /* Maybe insert a truncation at the left. */
17170 if (it->first_visible_x
17171 && IT_CHARPOS (*it) > 0)
17173 if (!FRAME_WINDOW_P (it->f))
17174 insert_left_trunc_glyphs (it);
17175 it->glyph_row->truncated_on_left_p = 1;
17178 it->face_id = saved_face_id;
17180 /* Value is number of columns displayed. */
17181 return it->hpos - hpos_at_start;
17186 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17187 appears as an element of LIST or as the car of an element of LIST.
17188 If PROPVAL is a list, compare each element against LIST in that
17189 way, and return 1/2 if any element of PROPVAL is found in LIST.
17190 Otherwise return 0. This function cannot quit.
17191 The return value is 2 if the text is invisible but with an ellipsis
17192 and 1 if it's invisible and without an ellipsis. */
17195 invisible_p (propval, list)
17196 register Lisp_Object propval;
17197 Lisp_Object list;
17199 register Lisp_Object tail, proptail;
17201 for (tail = list; CONSP (tail); tail = XCDR (tail))
17203 register Lisp_Object tem;
17204 tem = XCAR (tail);
17205 if (EQ (propval, tem))
17206 return 1;
17207 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17208 return NILP (XCDR (tem)) ? 1 : 2;
17211 if (CONSP (propval))
17213 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17215 Lisp_Object propelt;
17216 propelt = XCAR (proptail);
17217 for (tail = list; CONSP (tail); tail = XCDR (tail))
17219 register Lisp_Object tem;
17220 tem = XCAR (tail);
17221 if (EQ (propelt, tem))
17222 return 1;
17223 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17224 return NILP (XCDR (tem)) ? 1 : 2;
17229 return 0;
17232 /* Calculate a width or height in pixels from a specification using
17233 the following elements:
17235 SPEC ::=
17236 NUM - a (fractional) multiple of the default font width/height
17237 (NUM) - specifies exactly NUM pixels
17238 UNIT - a fixed number of pixels, see below.
17239 ELEMENT - size of a display element in pixels, see below.
17240 (NUM . SPEC) - equals NUM * SPEC
17241 (+ SPEC SPEC ...) - add pixel values
17242 (- SPEC SPEC ...) - subtract pixel values
17243 (- SPEC) - negate pixel value
17245 NUM ::=
17246 INT or FLOAT - a number constant
17247 SYMBOL - use symbol's (buffer local) variable binding.
17249 UNIT ::=
17250 in - pixels per inch *)
17251 mm - pixels per 1/1000 meter *)
17252 cm - pixels per 1/100 meter *)
17253 width - width of current font in pixels.
17254 height - height of current font in pixels.
17256 *) using the ratio(s) defined in display-pixels-per-inch.
17258 ELEMENT ::=
17260 left-fringe - left fringe width in pixels
17261 right-fringe - right fringe width in pixels
17263 left-margin - left margin width in pixels
17264 right-margin - right margin width in pixels
17266 scroll-bar - scroll-bar area width in pixels
17268 Examples:
17270 Pixels corresponding to 5 inches:
17271 (5 . in)
17273 Total width of non-text areas on left side of window (if scroll-bar is on left):
17274 '(space :width (+ left-fringe left-margin scroll-bar))
17276 Align to first text column (in header line):
17277 '(space :align-to 0)
17279 Align to middle of text area minus half the width of variable `my-image'
17280 containing a loaded image:
17281 '(space :align-to (0.5 . (- text my-image)))
17283 Width of left margin minus width of 1 character in the default font:
17284 '(space :width (- left-margin 1))
17286 Width of left margin minus width of 2 characters in the current font:
17287 '(space :width (- left-margin (2 . width)))
17289 Center 1 character over left-margin (in header line):
17290 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17292 Different ways to express width of left fringe plus left margin minus one pixel:
17293 '(space :width (- (+ left-fringe left-margin) (1)))
17294 '(space :width (+ left-fringe left-margin (- (1))))
17295 '(space :width (+ left-fringe left-margin (-1)))
17299 #define NUMVAL(X) \
17300 ((INTEGERP (X) || FLOATP (X)) \
17301 ? XFLOATINT (X) \
17302 : - 1)
17305 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17306 double *res;
17307 struct it *it;
17308 Lisp_Object prop;
17309 void *font;
17310 int width_p, *align_to;
17312 double pixels;
17314 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17315 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17317 if (NILP (prop))
17318 return OK_PIXELS (0);
17320 if (SYMBOLP (prop))
17322 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17324 char *unit = SDATA (SYMBOL_NAME (prop));
17326 if (unit[0] == 'i' && unit[1] == 'n')
17327 pixels = 1.0;
17328 else if (unit[0] == 'm' && unit[1] == 'm')
17329 pixels = 25.4;
17330 else if (unit[0] == 'c' && unit[1] == 'm')
17331 pixels = 2.54;
17332 else
17333 pixels = 0;
17334 if (pixels > 0)
17336 double ppi;
17337 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17338 || (CONSP (Vdisplay_pixels_per_inch)
17339 && (ppi = (width_p
17340 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17341 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17342 ppi > 0)))
17343 return OK_PIXELS (ppi / pixels);
17345 return 0;
17349 #ifdef HAVE_WINDOW_SYSTEM
17350 if (EQ (prop, Qheight))
17351 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17352 if (EQ (prop, Qwidth))
17353 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17354 #else
17355 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17356 return OK_PIXELS (1);
17357 #endif
17359 if (EQ (prop, Qtext))
17360 return OK_PIXELS (width_p
17361 ? window_box_width (it->w, TEXT_AREA)
17362 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17364 if (align_to && *align_to < 0)
17366 *res = 0;
17367 if (EQ (prop, Qleft))
17368 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17369 if (EQ (prop, Qright))
17370 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17371 if (EQ (prop, Qcenter))
17372 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17373 + window_box_width (it->w, TEXT_AREA) / 2);
17374 if (EQ (prop, Qleft_fringe))
17375 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17376 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17377 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17378 if (EQ (prop, Qright_fringe))
17379 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17380 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17381 : window_box_right_offset (it->w, TEXT_AREA));
17382 if (EQ (prop, Qleft_margin))
17383 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17384 if (EQ (prop, Qright_margin))
17385 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17386 if (EQ (prop, Qscroll_bar))
17387 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17389 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17390 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17391 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17392 : 0)));
17394 else
17396 if (EQ (prop, Qleft_fringe))
17397 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17398 if (EQ (prop, Qright_fringe))
17399 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17400 if (EQ (prop, Qleft_margin))
17401 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17402 if (EQ (prop, Qright_margin))
17403 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17404 if (EQ (prop, Qscroll_bar))
17405 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17408 prop = Fbuffer_local_value (prop, it->w->buffer);
17411 if (INTEGERP (prop) || FLOATP (prop))
17413 int base_unit = (width_p
17414 ? FRAME_COLUMN_WIDTH (it->f)
17415 : FRAME_LINE_HEIGHT (it->f));
17416 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17419 if (CONSP (prop))
17421 Lisp_Object car = XCAR (prop);
17422 Lisp_Object cdr = XCDR (prop);
17424 if (SYMBOLP (car))
17426 #ifdef HAVE_WINDOW_SYSTEM
17427 if (valid_image_p (prop))
17429 int id = lookup_image (it->f, prop);
17430 struct image *img = IMAGE_FROM_ID (it->f, id);
17432 return OK_PIXELS (width_p ? img->width : img->height);
17434 #endif
17435 if (EQ (car, Qplus) || EQ (car, Qminus))
17437 int first = 1;
17438 double px;
17440 pixels = 0;
17441 while (CONSP (cdr))
17443 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17444 font, width_p, align_to))
17445 return 0;
17446 if (first)
17447 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17448 else
17449 pixels += px;
17450 cdr = XCDR (cdr);
17452 if (EQ (car, Qminus))
17453 pixels = -pixels;
17454 return OK_PIXELS (pixels);
17457 car = Fbuffer_local_value (car, it->w->buffer);
17460 if (INTEGERP (car) || FLOATP (car))
17462 double fact;
17463 pixels = XFLOATINT (car);
17464 if (NILP (cdr))
17465 return OK_PIXELS (pixels);
17466 if (calc_pixel_width_or_height (&fact, it, cdr,
17467 font, width_p, align_to))
17468 return OK_PIXELS (pixels * fact);
17469 return 0;
17472 return 0;
17475 return 0;
17479 /***********************************************************************
17480 Glyph Display
17481 ***********************************************************************/
17483 #ifdef HAVE_WINDOW_SYSTEM
17485 #if GLYPH_DEBUG
17487 void
17488 dump_glyph_string (s)
17489 struct glyph_string *s;
17491 fprintf (stderr, "glyph string\n");
17492 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17493 s->x, s->y, s->width, s->height);
17494 fprintf (stderr, " ybase = %d\n", s->ybase);
17495 fprintf (stderr, " hl = %d\n", s->hl);
17496 fprintf (stderr, " left overhang = %d, right = %d\n",
17497 s->left_overhang, s->right_overhang);
17498 fprintf (stderr, " nchars = %d\n", s->nchars);
17499 fprintf (stderr, " extends to end of line = %d\n",
17500 s->extends_to_end_of_line_p);
17501 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17502 fprintf (stderr, " bg width = %d\n", s->background_width);
17505 #endif /* GLYPH_DEBUG */
17507 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17508 of XChar2b structures for S; it can't be allocated in
17509 init_glyph_string because it must be allocated via `alloca'. W
17510 is the window on which S is drawn. ROW and AREA are the glyph row
17511 and area within the row from which S is constructed. START is the
17512 index of the first glyph structure covered by S. HL is a
17513 face-override for drawing S. */
17515 #ifdef HAVE_NTGUI
17516 #define OPTIONAL_HDC(hdc) hdc,
17517 #define DECLARE_HDC(hdc) HDC hdc;
17518 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17519 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17520 #endif
17522 #ifndef OPTIONAL_HDC
17523 #define OPTIONAL_HDC(hdc)
17524 #define DECLARE_HDC(hdc)
17525 #define ALLOCATE_HDC(hdc, f)
17526 #define RELEASE_HDC(hdc, f)
17527 #endif
17529 static void
17530 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17531 struct glyph_string *s;
17532 DECLARE_HDC (hdc)
17533 XChar2b *char2b;
17534 struct window *w;
17535 struct glyph_row *row;
17536 enum glyph_row_area area;
17537 int start;
17538 enum draw_glyphs_face hl;
17540 bzero (s, sizeof *s);
17541 s->w = w;
17542 s->f = XFRAME (w->frame);
17543 #ifdef HAVE_NTGUI
17544 s->hdc = hdc;
17545 #endif
17546 s->display = FRAME_X_DISPLAY (s->f);
17547 s->window = FRAME_X_WINDOW (s->f);
17548 s->char2b = char2b;
17549 s->hl = hl;
17550 s->row = row;
17551 s->area = area;
17552 s->first_glyph = row->glyphs[area] + start;
17553 s->height = row->height;
17554 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17556 /* Display the internal border below the tool-bar window. */
17557 if (WINDOWP (s->f->tool_bar_window)
17558 && s->w == XWINDOW (s->f->tool_bar_window))
17559 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17561 s->ybase = s->y + row->ascent;
17565 /* Append the list of glyph strings with head H and tail T to the list
17566 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17568 static INLINE void
17569 append_glyph_string_lists (head, tail, h, t)
17570 struct glyph_string **head, **tail;
17571 struct glyph_string *h, *t;
17573 if (h)
17575 if (*head)
17576 (*tail)->next = h;
17577 else
17578 *head = h;
17579 h->prev = *tail;
17580 *tail = t;
17585 /* Prepend the list of glyph strings with head H and tail T to the
17586 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17587 result. */
17589 static INLINE void
17590 prepend_glyph_string_lists (head, tail, h, t)
17591 struct glyph_string **head, **tail;
17592 struct glyph_string *h, *t;
17594 if (h)
17596 if (*head)
17597 (*head)->prev = t;
17598 else
17599 *tail = t;
17600 t->next = *head;
17601 *head = h;
17606 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17607 Set *HEAD and *TAIL to the resulting list. */
17609 static INLINE void
17610 append_glyph_string (head, tail, s)
17611 struct glyph_string **head, **tail;
17612 struct glyph_string *s;
17614 s->next = s->prev = NULL;
17615 append_glyph_string_lists (head, tail, s, s);
17619 /* Get face and two-byte form of character glyph GLYPH on frame F.
17620 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17621 a pointer to a realized face that is ready for display. */
17623 static INLINE struct face *
17624 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17625 struct frame *f;
17626 struct glyph *glyph;
17627 XChar2b *char2b;
17628 int *two_byte_p;
17630 struct face *face;
17632 xassert (glyph->type == CHAR_GLYPH);
17633 face = FACE_FROM_ID (f, glyph->face_id);
17635 if (two_byte_p)
17636 *two_byte_p = 0;
17638 if (!glyph->multibyte_p)
17640 /* Unibyte case. We don't have to encode, but we have to make
17641 sure to use a face suitable for unibyte. */
17642 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17644 else if (glyph->u.ch < 128
17645 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17647 /* Case of ASCII in a face known to fit ASCII. */
17648 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17650 else
17652 int c1, c2, charset;
17654 /* Split characters into bytes. If c2 is -1 afterwards, C is
17655 really a one-byte character so that byte1 is zero. */
17656 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17657 if (c2 > 0)
17658 STORE_XCHAR2B (char2b, c1, c2);
17659 else
17660 STORE_XCHAR2B (char2b, 0, c1);
17662 /* Maybe encode the character in *CHAR2B. */
17663 if (charset != CHARSET_ASCII)
17665 struct font_info *font_info
17666 = FONT_INFO_FROM_ID (f, face->font_info_id);
17667 if (font_info)
17668 glyph->font_type
17669 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17673 /* Make sure X resources of the face are allocated. */
17674 xassert (face != NULL);
17675 PREPARE_FACE_FOR_DISPLAY (f, face);
17676 return face;
17680 /* Fill glyph string S with composition components specified by S->cmp.
17682 FACES is an array of faces for all components of this composition.
17683 S->gidx is the index of the first component for S.
17684 OVERLAPS_P non-zero means S should draw the foreground only, and
17685 use its physical height for clipping.
17687 Value is the index of a component not in S. */
17689 static int
17690 fill_composite_glyph_string (s, faces, overlaps_p)
17691 struct glyph_string *s;
17692 struct face **faces;
17693 int overlaps_p;
17695 int i;
17697 xassert (s);
17699 s->for_overlaps_p = overlaps_p;
17701 s->face = faces[s->gidx];
17702 s->font = s->face->font;
17703 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17705 /* For all glyphs of this composition, starting at the offset
17706 S->gidx, until we reach the end of the definition or encounter a
17707 glyph that requires the different face, add it to S. */
17708 ++s->nchars;
17709 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17710 ++s->nchars;
17712 /* All glyph strings for the same composition has the same width,
17713 i.e. the width set for the first component of the composition. */
17715 s->width = s->first_glyph->pixel_width;
17717 /* If the specified font could not be loaded, use the frame's
17718 default font, but record the fact that we couldn't load it in
17719 the glyph string so that we can draw rectangles for the
17720 characters of the glyph string. */
17721 if (s->font == NULL)
17723 s->font_not_found_p = 1;
17724 s->font = FRAME_FONT (s->f);
17727 /* Adjust base line for subscript/superscript text. */
17728 s->ybase += s->first_glyph->voffset;
17730 xassert (s->face && s->face->gc);
17732 /* This glyph string must always be drawn with 16-bit functions. */
17733 s->two_byte_p = 1;
17735 return s->gidx + s->nchars;
17739 /* Fill glyph string S from a sequence of character glyphs.
17741 FACE_ID is the face id of the string. START is the index of the
17742 first glyph to consider, END is the index of the last + 1.
17743 OVERLAPS_P non-zero means S should draw the foreground only, and
17744 use its physical height for clipping.
17746 Value is the index of the first glyph not in S. */
17748 static int
17749 fill_glyph_string (s, face_id, start, end, overlaps_p)
17750 struct glyph_string *s;
17751 int face_id;
17752 int start, end, overlaps_p;
17754 struct glyph *glyph, *last;
17755 int voffset;
17756 int glyph_not_available_p;
17758 xassert (s->f == XFRAME (s->w->frame));
17759 xassert (s->nchars == 0);
17760 xassert (start >= 0 && end > start);
17762 s->for_overlaps_p = overlaps_p,
17763 glyph = s->row->glyphs[s->area] + start;
17764 last = s->row->glyphs[s->area] + end;
17765 voffset = glyph->voffset;
17767 glyph_not_available_p = glyph->glyph_not_available_p;
17769 while (glyph < last
17770 && glyph->type == CHAR_GLYPH
17771 && glyph->voffset == voffset
17772 /* Same face id implies same font, nowadays. */
17773 && glyph->face_id == face_id
17774 && glyph->glyph_not_available_p == glyph_not_available_p)
17776 int two_byte_p;
17778 s->face = get_glyph_face_and_encoding (s->f, glyph,
17779 s->char2b + s->nchars,
17780 &two_byte_p);
17781 s->two_byte_p = two_byte_p;
17782 ++s->nchars;
17783 xassert (s->nchars <= end - start);
17784 s->width += glyph->pixel_width;
17785 ++glyph;
17788 s->font = s->face->font;
17789 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17791 /* If the specified font could not be loaded, use the frame's font,
17792 but record the fact that we couldn't load it in
17793 S->font_not_found_p so that we can draw rectangles for the
17794 characters of the glyph string. */
17795 if (s->font == NULL || glyph_not_available_p)
17797 s->font_not_found_p = 1;
17798 s->font = FRAME_FONT (s->f);
17801 /* Adjust base line for subscript/superscript text. */
17802 s->ybase += voffset;
17804 xassert (s->face && s->face->gc);
17805 return glyph - s->row->glyphs[s->area];
17809 /* Fill glyph string S from image glyph S->first_glyph. */
17811 static void
17812 fill_image_glyph_string (s)
17813 struct glyph_string *s;
17815 xassert (s->first_glyph->type == IMAGE_GLYPH);
17816 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17817 xassert (s->img);
17818 s->slice = s->first_glyph->slice;
17819 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17820 s->font = s->face->font;
17821 s->width = s->first_glyph->pixel_width;
17823 /* Adjust base line for subscript/superscript text. */
17824 s->ybase += s->first_glyph->voffset;
17828 /* Fill glyph string S from a sequence of stretch glyphs.
17830 ROW is the glyph row in which the glyphs are found, AREA is the
17831 area within the row. START is the index of the first glyph to
17832 consider, END is the index of the last + 1.
17834 Value is the index of the first glyph not in S. */
17836 static int
17837 fill_stretch_glyph_string (s, row, area, start, end)
17838 struct glyph_string *s;
17839 struct glyph_row *row;
17840 enum glyph_row_area area;
17841 int start, end;
17843 struct glyph *glyph, *last;
17844 int voffset, face_id;
17846 xassert (s->first_glyph->type == STRETCH_GLYPH);
17848 glyph = s->row->glyphs[s->area] + start;
17849 last = s->row->glyphs[s->area] + end;
17850 face_id = glyph->face_id;
17851 s->face = FACE_FROM_ID (s->f, face_id);
17852 s->font = s->face->font;
17853 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17854 s->width = glyph->pixel_width;
17855 voffset = glyph->voffset;
17857 for (++glyph;
17858 (glyph < last
17859 && glyph->type == STRETCH_GLYPH
17860 && glyph->voffset == voffset
17861 && glyph->face_id == face_id);
17862 ++glyph)
17863 s->width += glyph->pixel_width;
17865 /* Adjust base line for subscript/superscript text. */
17866 s->ybase += voffset;
17868 /* The case that face->gc == 0 is handled when drawing the glyph
17869 string by calling PREPARE_FACE_FOR_DISPLAY. */
17870 xassert (s->face);
17871 return glyph - s->row->glyphs[s->area];
17875 /* EXPORT for RIF:
17876 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17877 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17878 assumed to be zero. */
17880 void
17881 x_get_glyph_overhangs (glyph, f, left, right)
17882 struct glyph *glyph;
17883 struct frame *f;
17884 int *left, *right;
17886 *left = *right = 0;
17888 if (glyph->type == CHAR_GLYPH)
17890 XFontStruct *font;
17891 struct face *face;
17892 struct font_info *font_info;
17893 XChar2b char2b;
17894 XCharStruct *pcm;
17896 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17897 font = face->font;
17898 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17899 if (font /* ++KFS: Should this be font_info ? */
17900 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17902 if (pcm->rbearing > pcm->width)
17903 *right = pcm->rbearing - pcm->width;
17904 if (pcm->lbearing < 0)
17905 *left = -pcm->lbearing;
17911 /* Return the index of the first glyph preceding glyph string S that
17912 is overwritten by S because of S's left overhang. Value is -1
17913 if no glyphs are overwritten. */
17915 static int
17916 left_overwritten (s)
17917 struct glyph_string *s;
17919 int k;
17921 if (s->left_overhang)
17923 int x = 0, i;
17924 struct glyph *glyphs = s->row->glyphs[s->area];
17925 int first = s->first_glyph - glyphs;
17927 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17928 x -= glyphs[i].pixel_width;
17930 k = i + 1;
17932 else
17933 k = -1;
17935 return k;
17939 /* Return the index of the first glyph preceding glyph string S that
17940 is overwriting S because of its right overhang. Value is -1 if no
17941 glyph in front of S overwrites S. */
17943 static int
17944 left_overwriting (s)
17945 struct glyph_string *s;
17947 int i, k, x;
17948 struct glyph *glyphs = s->row->glyphs[s->area];
17949 int first = s->first_glyph - glyphs;
17951 k = -1;
17952 x = 0;
17953 for (i = first - 1; i >= 0; --i)
17955 int left, right;
17956 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17957 if (x + right > 0)
17958 k = i;
17959 x -= glyphs[i].pixel_width;
17962 return k;
17966 /* Return the index of the last glyph following glyph string S that is
17967 not overwritten by S because of S's right overhang. Value is -1 if
17968 no such glyph is found. */
17970 static int
17971 right_overwritten (s)
17972 struct glyph_string *s;
17974 int k = -1;
17976 if (s->right_overhang)
17978 int x = 0, i;
17979 struct glyph *glyphs = s->row->glyphs[s->area];
17980 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17981 int end = s->row->used[s->area];
17983 for (i = first; i < end && s->right_overhang > x; ++i)
17984 x += glyphs[i].pixel_width;
17986 k = i;
17989 return k;
17993 /* Return the index of the last glyph following glyph string S that
17994 overwrites S because of its left overhang. Value is negative
17995 if no such glyph is found. */
17997 static int
17998 right_overwriting (s)
17999 struct glyph_string *s;
18001 int i, k, x;
18002 int end = s->row->used[s->area];
18003 struct glyph *glyphs = s->row->glyphs[s->area];
18004 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18006 k = -1;
18007 x = 0;
18008 for (i = first; i < end; ++i)
18010 int left, right;
18011 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18012 if (x - left < 0)
18013 k = i;
18014 x += glyphs[i].pixel_width;
18017 return k;
18021 /* Get face and two-byte form of character C in face FACE_ID on frame
18022 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18023 means we want to display multibyte text. DISPLAY_P non-zero means
18024 make sure that X resources for the face returned are allocated.
18025 Value is a pointer to a realized face that is ready for display if
18026 DISPLAY_P is non-zero. */
18028 static INLINE struct face *
18029 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18030 struct frame *f;
18031 int c, face_id;
18032 XChar2b *char2b;
18033 int multibyte_p, display_p;
18035 struct face *face = FACE_FROM_ID (f, face_id);
18037 if (!multibyte_p)
18039 /* Unibyte case. We don't have to encode, but we have to make
18040 sure to use a face suitable for unibyte. */
18041 STORE_XCHAR2B (char2b, 0, c);
18042 face_id = FACE_FOR_CHAR (f, face, c);
18043 face = FACE_FROM_ID (f, face_id);
18045 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18047 /* Case of ASCII in a face known to fit ASCII. */
18048 STORE_XCHAR2B (char2b, 0, c);
18050 else
18052 int c1, c2, charset;
18054 /* Split characters into bytes. If c2 is -1 afterwards, C is
18055 really a one-byte character so that byte1 is zero. */
18056 SPLIT_CHAR (c, charset, c1, c2);
18057 if (c2 > 0)
18058 STORE_XCHAR2B (char2b, c1, c2);
18059 else
18060 STORE_XCHAR2B (char2b, 0, c1);
18062 /* Maybe encode the character in *CHAR2B. */
18063 if (face->font != NULL)
18065 struct font_info *font_info
18066 = FONT_INFO_FROM_ID (f, face->font_info_id);
18067 if (font_info)
18068 rif->encode_char (c, char2b, font_info, 0);
18072 /* Make sure X resources of the face are allocated. */
18073 #ifdef HAVE_X_WINDOWS
18074 if (display_p)
18075 #endif
18077 xassert (face != NULL);
18078 PREPARE_FACE_FOR_DISPLAY (f, face);
18081 return face;
18085 /* Set background width of glyph string S. START is the index of the
18086 first glyph following S. LAST_X is the right-most x-position + 1
18087 in the drawing area. */
18089 static INLINE void
18090 set_glyph_string_background_width (s, start, last_x)
18091 struct glyph_string *s;
18092 int start;
18093 int last_x;
18095 /* If the face of this glyph string has to be drawn to the end of
18096 the drawing area, set S->extends_to_end_of_line_p. */
18097 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18099 if (start == s->row->used[s->area]
18100 && s->area == TEXT_AREA
18101 && ((s->hl == DRAW_NORMAL_TEXT
18102 && (s->row->fill_line_p
18103 || s->face->background != default_face->background
18104 || s->face->stipple != default_face->stipple
18105 || s->row->mouse_face_p))
18106 || s->hl == DRAW_MOUSE_FACE
18107 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18108 && s->row->fill_line_p)))
18109 s->extends_to_end_of_line_p = 1;
18111 /* If S extends its face to the end of the line, set its
18112 background_width to the distance to the right edge of the drawing
18113 area. */
18114 if (s->extends_to_end_of_line_p)
18115 s->background_width = last_x - s->x + 1;
18116 else
18117 s->background_width = s->width;
18121 /* Compute overhangs and x-positions for glyph string S and its
18122 predecessors, or successors. X is the starting x-position for S.
18123 BACKWARD_P non-zero means process predecessors. */
18125 static void
18126 compute_overhangs_and_x (s, x, backward_p)
18127 struct glyph_string *s;
18128 int x;
18129 int backward_p;
18131 if (backward_p)
18133 while (s)
18135 if (rif->compute_glyph_string_overhangs)
18136 rif->compute_glyph_string_overhangs (s);
18137 x -= s->width;
18138 s->x = x;
18139 s = s->prev;
18142 else
18144 while (s)
18146 if (rif->compute_glyph_string_overhangs)
18147 rif->compute_glyph_string_overhangs (s);
18148 s->x = x;
18149 x += s->width;
18150 s = s->next;
18157 /* The following macros are only called from draw_glyphs below.
18158 They reference the following parameters of that function directly:
18159 `w', `row', `area', and `overlap_p'
18160 as well as the following local variables:
18161 `s', `f', and `hdc' (in W32) */
18163 #ifdef HAVE_NTGUI
18164 /* On W32, silently add local `hdc' variable to argument list of
18165 init_glyph_string. */
18166 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18167 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18168 #else
18169 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18170 init_glyph_string (s, char2b, w, row, area, start, hl)
18171 #endif
18173 /* Add a glyph string for a stretch glyph to the list of strings
18174 between HEAD and TAIL. START is the index of the stretch glyph in
18175 row area AREA of glyph row ROW. END is the index of the last glyph
18176 in that glyph row area. X is the current output position assigned
18177 to the new glyph string constructed. HL overrides that face of the
18178 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18179 is the right-most x-position of the drawing area. */
18181 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18182 and below -- keep them on one line. */
18183 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18184 do \
18186 s = (struct glyph_string *) alloca (sizeof *s); \
18187 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18188 START = fill_stretch_glyph_string (s, row, area, START, END); \
18189 append_glyph_string (&HEAD, &TAIL, s); \
18190 s->x = (X); \
18192 while (0)
18195 /* Add a glyph string for an image glyph to the list of strings
18196 between HEAD and TAIL. START is the index of the image glyph in
18197 row area AREA of glyph row ROW. END is the index of the last glyph
18198 in that glyph row area. X is the current output position assigned
18199 to the new glyph string constructed. HL overrides that face of the
18200 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18201 is the right-most x-position of the drawing area. */
18203 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18204 do \
18206 s = (struct glyph_string *) alloca (sizeof *s); \
18207 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18208 fill_image_glyph_string (s); \
18209 append_glyph_string (&HEAD, &TAIL, s); \
18210 ++START; \
18211 s->x = (X); \
18213 while (0)
18216 /* Add a glyph string for a sequence of character glyphs to the list
18217 of strings between HEAD and TAIL. START is the index of the first
18218 glyph in row area AREA of glyph row ROW that is part of the new
18219 glyph string. END is the index of the last glyph in that glyph row
18220 area. X is the current output position assigned to the new glyph
18221 string constructed. HL overrides that face of the glyph; e.g. it
18222 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18223 right-most x-position of the drawing area. */
18225 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18226 do \
18228 int c, face_id; \
18229 XChar2b *char2b; \
18231 c = (row)->glyphs[area][START].u.ch; \
18232 face_id = (row)->glyphs[area][START].face_id; \
18234 s = (struct glyph_string *) alloca (sizeof *s); \
18235 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18236 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18237 append_glyph_string (&HEAD, &TAIL, s); \
18238 s->x = (X); \
18239 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18241 while (0)
18244 /* Add a glyph string for a composite sequence to the list of strings
18245 between HEAD and TAIL. START is the index of the first glyph in
18246 row area AREA of glyph row ROW that is part of the new glyph
18247 string. END is the index of the last glyph in that glyph row area.
18248 X is the current output position assigned to the new glyph string
18249 constructed. HL overrides that face of the glyph; e.g. it is
18250 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18251 x-position of the drawing area. */
18253 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18254 do { \
18255 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18256 int face_id = (row)->glyphs[area][START].face_id; \
18257 struct face *base_face = FACE_FROM_ID (f, face_id); \
18258 struct composition *cmp = composition_table[cmp_id]; \
18259 int glyph_len = cmp->glyph_len; \
18260 XChar2b *char2b; \
18261 struct face **faces; \
18262 struct glyph_string *first_s = NULL; \
18263 int n; \
18265 base_face = base_face->ascii_face; \
18266 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18267 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18268 /* At first, fill in `char2b' and `faces'. */ \
18269 for (n = 0; n < glyph_len; n++) \
18271 int c = COMPOSITION_GLYPH (cmp, n); \
18272 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18273 faces[n] = FACE_FROM_ID (f, this_face_id); \
18274 get_char_face_and_encoding (f, c, this_face_id, \
18275 char2b + n, 1, 1); \
18278 /* Make glyph_strings for each glyph sequence that is drawable by \
18279 the same face, and append them to HEAD/TAIL. */ \
18280 for (n = 0; n < cmp->glyph_len;) \
18282 s = (struct glyph_string *) alloca (sizeof *s); \
18283 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18284 append_glyph_string (&(HEAD), &(TAIL), s); \
18285 s->cmp = cmp; \
18286 s->gidx = n; \
18287 s->x = (X); \
18289 if (n == 0) \
18290 first_s = s; \
18292 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18295 ++START; \
18296 s = first_s; \
18297 } while (0)
18300 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18301 of AREA of glyph row ROW on window W between indices START and END.
18302 HL overrides the face for drawing glyph strings, e.g. it is
18303 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18304 x-positions of the drawing area.
18306 This is an ugly monster macro construct because we must use alloca
18307 to allocate glyph strings (because draw_glyphs can be called
18308 asynchronously). */
18310 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18311 do \
18313 HEAD = TAIL = NULL; \
18314 while (START < END) \
18316 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18317 switch (first_glyph->type) \
18319 case CHAR_GLYPH: \
18320 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18321 HL, X, LAST_X); \
18322 break; \
18324 case COMPOSITE_GLYPH: \
18325 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18326 HL, X, LAST_X); \
18327 break; \
18329 case STRETCH_GLYPH: \
18330 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18331 HL, X, LAST_X); \
18332 break; \
18334 case IMAGE_GLYPH: \
18335 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18336 HL, X, LAST_X); \
18337 break; \
18339 default: \
18340 abort (); \
18343 set_glyph_string_background_width (s, START, LAST_X); \
18344 (X) += s->width; \
18347 while (0)
18350 /* Draw glyphs between START and END in AREA of ROW on window W,
18351 starting at x-position X. X is relative to AREA in W. HL is a
18352 face-override with the following meaning:
18354 DRAW_NORMAL_TEXT draw normally
18355 DRAW_CURSOR draw in cursor face
18356 DRAW_MOUSE_FACE draw in mouse face.
18357 DRAW_INVERSE_VIDEO draw in mode line face
18358 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18359 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18361 If OVERLAPS_P is non-zero, draw only the foreground of characters
18362 and clip to the physical height of ROW.
18364 Value is the x-position reached, relative to AREA of W. */
18366 static int
18367 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18368 struct window *w;
18369 int x;
18370 struct glyph_row *row;
18371 enum glyph_row_area area;
18372 int start, end;
18373 enum draw_glyphs_face hl;
18374 int overlaps_p;
18376 struct glyph_string *head, *tail;
18377 struct glyph_string *s;
18378 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18379 int last_x, area_width;
18380 int x_reached;
18381 int i, j;
18382 struct frame *f = XFRAME (WINDOW_FRAME (w));
18383 DECLARE_HDC (hdc);
18385 ALLOCATE_HDC (hdc, f);
18387 /* Let's rather be paranoid than getting a SEGV. */
18388 end = min (end, row->used[area]);
18389 start = max (0, start);
18390 start = min (end, start);
18392 /* Translate X to frame coordinates. Set last_x to the right
18393 end of the drawing area. */
18394 if (row->full_width_p)
18396 /* X is relative to the left edge of W, without scroll bars
18397 or fringes. */
18398 x += WINDOW_LEFT_EDGE_X (w);
18399 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18401 else
18403 int area_left = window_box_left (w, area);
18404 x += area_left;
18405 area_width = window_box_width (w, area);
18406 last_x = area_left + area_width;
18409 /* Build a doubly-linked list of glyph_string structures between
18410 head and tail from what we have to draw. Note that the macro
18411 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18412 the reason we use a separate variable `i'. */
18413 i = start;
18414 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18415 if (tail)
18416 x_reached = tail->x + tail->background_width;
18417 else
18418 x_reached = x;
18420 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18421 the row, redraw some glyphs in front or following the glyph
18422 strings built above. */
18423 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18425 int dummy_x = 0;
18426 struct glyph_string *h, *t;
18428 /* Compute overhangs for all glyph strings. */
18429 if (rif->compute_glyph_string_overhangs)
18430 for (s = head; s; s = s->next)
18431 rif->compute_glyph_string_overhangs (s);
18433 /* Prepend glyph strings for glyphs in front of the first glyph
18434 string that are overwritten because of the first glyph
18435 string's left overhang. The background of all strings
18436 prepended must be drawn because the first glyph string
18437 draws over it. */
18438 i = left_overwritten (head);
18439 if (i >= 0)
18441 j = i;
18442 BUILD_GLYPH_STRINGS (j, start, h, t,
18443 DRAW_NORMAL_TEXT, dummy_x, last_x);
18444 start = i;
18445 compute_overhangs_and_x (t, head->x, 1);
18446 prepend_glyph_string_lists (&head, &tail, h, t);
18447 clip_head = head;
18450 /* Prepend glyph strings for glyphs in front of the first glyph
18451 string that overwrite that glyph string because of their
18452 right overhang. For these strings, only the foreground must
18453 be drawn, because it draws over the glyph string at `head'.
18454 The background must not be drawn because this would overwrite
18455 right overhangs of preceding glyphs for which no glyph
18456 strings exist. */
18457 i = left_overwriting (head);
18458 if (i >= 0)
18460 clip_head = head;
18461 BUILD_GLYPH_STRINGS (i, start, h, t,
18462 DRAW_NORMAL_TEXT, dummy_x, last_x);
18463 for (s = h; s; s = s->next)
18464 s->background_filled_p = 1;
18465 compute_overhangs_and_x (t, head->x, 1);
18466 prepend_glyph_string_lists (&head, &tail, h, t);
18469 /* Append glyphs strings for glyphs following the last glyph
18470 string tail that are overwritten by tail. The background of
18471 these strings has to be drawn because tail's foreground draws
18472 over it. */
18473 i = right_overwritten (tail);
18474 if (i >= 0)
18476 BUILD_GLYPH_STRINGS (end, i, h, t,
18477 DRAW_NORMAL_TEXT, x, last_x);
18478 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18479 append_glyph_string_lists (&head, &tail, h, t);
18480 clip_tail = tail;
18483 /* Append glyph strings for glyphs following the last glyph
18484 string tail that overwrite tail. The foreground of such
18485 glyphs has to be drawn because it writes into the background
18486 of tail. The background must not be drawn because it could
18487 paint over the foreground of following glyphs. */
18488 i = right_overwriting (tail);
18489 if (i >= 0)
18491 clip_tail = tail;
18492 BUILD_GLYPH_STRINGS (end, i, h, t,
18493 DRAW_NORMAL_TEXT, x, last_x);
18494 for (s = h; s; s = s->next)
18495 s->background_filled_p = 1;
18496 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18497 append_glyph_string_lists (&head, &tail, h, t);
18499 if (clip_head || clip_tail)
18500 for (s = head; s; s = s->next)
18502 s->clip_head = clip_head;
18503 s->clip_tail = clip_tail;
18507 /* Draw all strings. */
18508 for (s = head; s; s = s->next)
18509 rif->draw_glyph_string (s);
18511 if (area == TEXT_AREA
18512 && !row->full_width_p
18513 /* When drawing overlapping rows, only the glyph strings'
18514 foreground is drawn, which doesn't erase a cursor
18515 completely. */
18516 && !overlaps_p)
18518 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18519 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18520 : (tail ? tail->x + tail->background_width : x));
18522 int text_left = window_box_left (w, TEXT_AREA);
18523 x0 -= text_left;
18524 x1 -= text_left;
18526 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18527 row->y, MATRIX_ROW_BOTTOM_Y (row));
18530 /* Value is the x-position up to which drawn, relative to AREA of W.
18531 This doesn't include parts drawn because of overhangs. */
18532 if (row->full_width_p)
18533 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18534 else
18535 x_reached -= window_box_left (w, area);
18537 RELEASE_HDC (hdc, f);
18539 return x_reached;
18542 /* Expand row matrix if too narrow. Don't expand if area
18543 is not present. */
18545 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18547 if (!fonts_changed_p \
18548 && (it->glyph_row->glyphs[area] \
18549 < it->glyph_row->glyphs[area + 1])) \
18551 it->w->ncols_scale_factor++; \
18552 fonts_changed_p = 1; \
18556 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18557 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18559 static INLINE void
18560 append_glyph (it)
18561 struct it *it;
18563 struct glyph *glyph;
18564 enum glyph_row_area area = it->area;
18566 xassert (it->glyph_row);
18567 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18569 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18570 if (glyph < it->glyph_row->glyphs[area + 1])
18572 glyph->charpos = CHARPOS (it->position);
18573 glyph->object = it->object;
18574 glyph->pixel_width = it->pixel_width;
18575 glyph->ascent = it->ascent;
18576 glyph->descent = it->descent;
18577 glyph->voffset = it->voffset;
18578 glyph->type = CHAR_GLYPH;
18579 glyph->multibyte_p = it->multibyte_p;
18580 glyph->left_box_line_p = it->start_of_box_run_p;
18581 glyph->right_box_line_p = it->end_of_box_run_p;
18582 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18583 || it->phys_descent > it->descent);
18584 glyph->padding_p = 0;
18585 glyph->glyph_not_available_p = it->glyph_not_available_p;
18586 glyph->face_id = it->face_id;
18587 glyph->u.ch = it->char_to_display;
18588 glyph->slice = null_glyph_slice;
18589 glyph->font_type = FONT_TYPE_UNKNOWN;
18590 ++it->glyph_row->used[area];
18592 else
18593 IT_EXPAND_MATRIX_WIDTH (it, area);
18596 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18597 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18599 static INLINE void
18600 append_composite_glyph (it)
18601 struct it *it;
18603 struct glyph *glyph;
18604 enum glyph_row_area area = it->area;
18606 xassert (it->glyph_row);
18608 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18609 if (glyph < it->glyph_row->glyphs[area + 1])
18611 glyph->charpos = CHARPOS (it->position);
18612 glyph->object = it->object;
18613 glyph->pixel_width = it->pixel_width;
18614 glyph->ascent = it->ascent;
18615 glyph->descent = it->descent;
18616 glyph->voffset = it->voffset;
18617 glyph->type = COMPOSITE_GLYPH;
18618 glyph->multibyte_p = it->multibyte_p;
18619 glyph->left_box_line_p = it->start_of_box_run_p;
18620 glyph->right_box_line_p = it->end_of_box_run_p;
18621 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18622 || it->phys_descent > it->descent);
18623 glyph->padding_p = 0;
18624 glyph->glyph_not_available_p = 0;
18625 glyph->face_id = it->face_id;
18626 glyph->u.cmp_id = it->cmp_id;
18627 glyph->slice = null_glyph_slice;
18628 glyph->font_type = FONT_TYPE_UNKNOWN;
18629 ++it->glyph_row->used[area];
18631 else
18632 IT_EXPAND_MATRIX_WIDTH (it, area);
18636 /* Change IT->ascent and IT->height according to the setting of
18637 IT->voffset. */
18639 static INLINE void
18640 take_vertical_position_into_account (it)
18641 struct it *it;
18643 if (it->voffset)
18645 if (it->voffset < 0)
18646 /* Increase the ascent so that we can display the text higher
18647 in the line. */
18648 it->ascent -= it->voffset;
18649 else
18650 /* Increase the descent so that we can display the text lower
18651 in the line. */
18652 it->descent += it->voffset;
18657 /* Produce glyphs/get display metrics for the image IT is loaded with.
18658 See the description of struct display_iterator in dispextern.h for
18659 an overview of struct display_iterator. */
18661 static void
18662 produce_image_glyph (it)
18663 struct it *it;
18665 struct image *img;
18666 struct face *face;
18667 int glyph_ascent;
18668 struct glyph_slice slice;
18670 xassert (it->what == IT_IMAGE);
18672 face = FACE_FROM_ID (it->f, it->face_id);
18673 xassert (face);
18674 /* Make sure X resources of the face is loaded. */
18675 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18677 if (it->image_id < 0)
18679 /* Fringe bitmap. */
18680 it->ascent = it->phys_ascent = 0;
18681 it->descent = it->phys_descent = 0;
18682 it->pixel_width = 0;
18683 it->nglyphs = 0;
18684 return;
18687 img = IMAGE_FROM_ID (it->f, it->image_id);
18688 xassert (img);
18689 /* Make sure X resources of the image is loaded. */
18690 prepare_image_for_display (it->f, img);
18692 slice.x = slice.y = 0;
18693 slice.width = img->width;
18694 slice.height = img->height;
18696 if (INTEGERP (it->slice.x))
18697 slice.x = XINT (it->slice.x);
18698 else if (FLOATP (it->slice.x))
18699 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18701 if (INTEGERP (it->slice.y))
18702 slice.y = XINT (it->slice.y);
18703 else if (FLOATP (it->slice.y))
18704 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18706 if (INTEGERP (it->slice.width))
18707 slice.width = XINT (it->slice.width);
18708 else if (FLOATP (it->slice.width))
18709 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18711 if (INTEGERP (it->slice.height))
18712 slice.height = XINT (it->slice.height);
18713 else if (FLOATP (it->slice.height))
18714 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18716 if (slice.x >= img->width)
18717 slice.x = img->width;
18718 if (slice.y >= img->height)
18719 slice.y = img->height;
18720 if (slice.x + slice.width >= img->width)
18721 slice.width = img->width - slice.x;
18722 if (slice.y + slice.height > img->height)
18723 slice.height = img->height - slice.y;
18725 if (slice.width == 0 || slice.height == 0)
18726 return;
18728 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18730 it->descent = slice.height - glyph_ascent;
18731 if (slice.y == 0)
18732 it->descent += img->vmargin;
18733 if (slice.y + slice.height == img->height)
18734 it->descent += img->vmargin;
18735 it->phys_descent = it->descent;
18737 it->pixel_width = slice.width;
18738 if (slice.x == 0)
18739 it->pixel_width += img->hmargin;
18740 if (slice.x + slice.width == img->width)
18741 it->pixel_width += img->hmargin;
18743 /* It's quite possible for images to have an ascent greater than
18744 their height, so don't get confused in that case. */
18745 if (it->descent < 0)
18746 it->descent = 0;
18748 #if 0 /* this breaks image tiling */
18749 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18750 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18751 if (face_ascent > it->ascent)
18752 it->ascent = it->phys_ascent = face_ascent;
18753 #endif
18755 it->nglyphs = 1;
18757 if (face->box != FACE_NO_BOX)
18759 if (face->box_line_width > 0)
18761 if (slice.y == 0)
18762 it->ascent += face->box_line_width;
18763 if (slice.y + slice.height == img->height)
18764 it->descent += face->box_line_width;
18767 if (it->start_of_box_run_p && slice.x == 0)
18768 it->pixel_width += abs (face->box_line_width);
18769 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18770 it->pixel_width += abs (face->box_line_width);
18773 take_vertical_position_into_account (it);
18775 if (it->glyph_row)
18777 struct glyph *glyph;
18778 enum glyph_row_area area = it->area;
18780 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18781 if (glyph < it->glyph_row->glyphs[area + 1])
18783 glyph->charpos = CHARPOS (it->position);
18784 glyph->object = it->object;
18785 glyph->pixel_width = it->pixel_width;
18786 glyph->ascent = glyph_ascent;
18787 glyph->descent = it->descent;
18788 glyph->voffset = it->voffset;
18789 glyph->type = IMAGE_GLYPH;
18790 glyph->multibyte_p = it->multibyte_p;
18791 glyph->left_box_line_p = it->start_of_box_run_p;
18792 glyph->right_box_line_p = it->end_of_box_run_p;
18793 glyph->overlaps_vertically_p = 0;
18794 glyph->padding_p = 0;
18795 glyph->glyph_not_available_p = 0;
18796 glyph->face_id = it->face_id;
18797 glyph->u.img_id = img->id;
18798 glyph->slice = slice;
18799 glyph->font_type = FONT_TYPE_UNKNOWN;
18800 ++it->glyph_row->used[area];
18802 else
18803 IT_EXPAND_MATRIX_WIDTH (it, area);
18808 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18809 of the glyph, WIDTH and HEIGHT are the width and height of the
18810 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18812 static void
18813 append_stretch_glyph (it, object, width, height, ascent)
18814 struct it *it;
18815 Lisp_Object object;
18816 int width, height;
18817 int ascent;
18819 struct glyph *glyph;
18820 enum glyph_row_area area = it->area;
18822 xassert (ascent >= 0 && ascent <= height);
18824 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18825 if (glyph < it->glyph_row->glyphs[area + 1])
18827 glyph->charpos = CHARPOS (it->position);
18828 glyph->object = object;
18829 glyph->pixel_width = width;
18830 glyph->ascent = ascent;
18831 glyph->descent = height - ascent;
18832 glyph->voffset = it->voffset;
18833 glyph->type = STRETCH_GLYPH;
18834 glyph->multibyte_p = it->multibyte_p;
18835 glyph->left_box_line_p = it->start_of_box_run_p;
18836 glyph->right_box_line_p = it->end_of_box_run_p;
18837 glyph->overlaps_vertically_p = 0;
18838 glyph->padding_p = 0;
18839 glyph->glyph_not_available_p = 0;
18840 glyph->face_id = it->face_id;
18841 glyph->u.stretch.ascent = ascent;
18842 glyph->u.stretch.height = height;
18843 glyph->slice = null_glyph_slice;
18844 glyph->font_type = FONT_TYPE_UNKNOWN;
18845 ++it->glyph_row->used[area];
18847 else
18848 IT_EXPAND_MATRIX_WIDTH (it, area);
18852 /* Produce a stretch glyph for iterator IT. IT->object is the value
18853 of the glyph property displayed. The value must be a list
18854 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18855 being recognized:
18857 1. `:width WIDTH' specifies that the space should be WIDTH *
18858 canonical char width wide. WIDTH may be an integer or floating
18859 point number.
18861 2. `:relative-width FACTOR' specifies that the width of the stretch
18862 should be computed from the width of the first character having the
18863 `glyph' property, and should be FACTOR times that width.
18865 3. `:align-to HPOS' specifies that the space should be wide enough
18866 to reach HPOS, a value in canonical character units.
18868 Exactly one of the above pairs must be present.
18870 4. `:height HEIGHT' specifies that the height of the stretch produced
18871 should be HEIGHT, measured in canonical character units.
18873 5. `:relative-height FACTOR' specifies that the height of the
18874 stretch should be FACTOR times the height of the characters having
18875 the glyph property.
18877 Either none or exactly one of 4 or 5 must be present.
18879 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18880 of the stretch should be used for the ascent of the stretch.
18881 ASCENT must be in the range 0 <= ASCENT <= 100. */
18883 static void
18884 produce_stretch_glyph (it)
18885 struct it *it;
18887 /* (space :width WIDTH :height HEIGHT ...) */
18888 Lisp_Object prop, plist;
18889 int width = 0, height = 0, align_to = -1;
18890 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18891 int ascent = 0;
18892 double tem;
18893 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18894 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18896 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18898 /* List should start with `space'. */
18899 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18900 plist = XCDR (it->object);
18902 /* Compute the width of the stretch. */
18903 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18904 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18906 /* Absolute width `:width WIDTH' specified and valid. */
18907 zero_width_ok_p = 1;
18908 width = (int)tem;
18910 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18911 NUMVAL (prop) > 0)
18913 /* Relative width `:relative-width FACTOR' specified and valid.
18914 Compute the width of the characters having the `glyph'
18915 property. */
18916 struct it it2;
18917 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18919 it2 = *it;
18920 if (it->multibyte_p)
18922 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18923 - IT_BYTEPOS (*it));
18924 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18926 else
18927 it2.c = *p, it2.len = 1;
18929 it2.glyph_row = NULL;
18930 it2.what = IT_CHARACTER;
18931 x_produce_glyphs (&it2);
18932 width = NUMVAL (prop) * it2.pixel_width;
18934 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18935 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18937 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18938 align_to = (align_to < 0
18940 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18941 else if (align_to < 0)
18942 align_to = window_box_left_offset (it->w, TEXT_AREA);
18943 width = max (0, (int)tem + align_to - it->current_x);
18944 zero_width_ok_p = 1;
18946 else
18947 /* Nothing specified -> width defaults to canonical char width. */
18948 width = FRAME_COLUMN_WIDTH (it->f);
18950 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18951 width = 1;
18953 /* Compute height. */
18954 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18955 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18957 height = (int)tem;
18958 zero_height_ok_p = 1;
18960 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18961 NUMVAL (prop) > 0)
18962 height = FONT_HEIGHT (font) * NUMVAL (prop);
18963 else
18964 height = FONT_HEIGHT (font);
18966 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18967 height = 1;
18969 /* Compute percentage of height used for ascent. If
18970 `:ascent ASCENT' is present and valid, use that. Otherwise,
18971 derive the ascent from the font in use. */
18972 if (prop = Fsafe_plist_get (plist, QCascent),
18973 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18974 ascent = height * NUMVAL (prop) / 100.0;
18975 else if (!NILP (prop)
18976 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18977 ascent = min (max (0, (int)tem), height);
18978 else
18979 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18981 if (width > 0 && height > 0 && it->glyph_row)
18983 Lisp_Object object = it->stack[it->sp - 1].string;
18984 if (!STRINGP (object))
18985 object = it->w->buffer;
18986 append_stretch_glyph (it, object, width, height, ascent);
18989 it->pixel_width = width;
18990 it->ascent = it->phys_ascent = ascent;
18991 it->descent = it->phys_descent = height - it->ascent;
18992 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18994 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18996 if (face->box_line_width > 0)
18998 it->ascent += face->box_line_width;
18999 it->descent += face->box_line_width;
19002 if (it->start_of_box_run_p)
19003 it->pixel_width += abs (face->box_line_width);
19004 if (it->end_of_box_run_p)
19005 it->pixel_width += abs (face->box_line_width);
19008 take_vertical_position_into_account (it);
19011 /* Get line-height and line-spacing property at point.
19012 If line-height has format (HEIGHT TOTAL), return TOTAL
19013 in TOTAL_HEIGHT. */
19015 static Lisp_Object
19016 get_line_height_property (it, prop)
19017 struct it *it;
19018 Lisp_Object prop;
19020 Lisp_Object position, val;
19022 if (STRINGP (it->object))
19023 position = make_number (IT_STRING_CHARPOS (*it));
19024 else if (BUFFERP (it->object))
19025 position = make_number (IT_CHARPOS (*it));
19026 else
19027 return Qnil;
19029 return Fget_char_property (position, prop, it->object);
19032 /* Calculate line-height and line-spacing properties.
19033 An integer value specifies explicit pixel value.
19034 A float value specifies relative value to current face height.
19035 A cons (float . face-name) specifies relative value to
19036 height of specified face font.
19038 Returns height in pixels, or nil. */
19041 static Lisp_Object
19042 calc_line_height_property (it, val, font, boff, override)
19043 struct it *it;
19044 Lisp_Object val;
19045 XFontStruct *font;
19046 int boff, override;
19048 Lisp_Object face_name = Qnil;
19049 int ascent, descent, height;
19051 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19052 return val;
19054 if (CONSP (val))
19056 face_name = XCAR (val);
19057 val = XCDR (val);
19058 if (!NUMBERP (val))
19059 val = make_number (1);
19060 if (NILP (face_name))
19062 height = it->ascent + it->descent;
19063 goto scale;
19067 if (NILP (face_name))
19069 font = FRAME_FONT (it->f);
19070 boff = FRAME_BASELINE_OFFSET (it->f);
19072 else if (EQ (face_name, Qt))
19074 override = 0;
19076 else
19078 int face_id;
19079 struct face *face;
19080 struct font_info *font_info;
19082 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19083 if (face_id < 0)
19084 return make_number (-1);
19086 face = FACE_FROM_ID (it->f, face_id);
19087 font = face->font;
19088 if (font == NULL)
19089 return make_number (-1);
19091 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19092 boff = font_info->baseline_offset;
19093 if (font_info->vertical_centering)
19094 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19097 ascent = FONT_BASE (font) + boff;
19098 descent = FONT_DESCENT (font) - boff;
19100 if (override)
19102 it->override_ascent = ascent;
19103 it->override_descent = descent;
19104 it->override_boff = boff;
19107 height = ascent + descent;
19109 scale:
19110 if (FLOATP (val))
19111 height = (int)(XFLOAT_DATA (val) * height);
19112 else if (INTEGERP (val))
19113 height *= XINT (val);
19115 return make_number (height);
19119 /* RIF:
19120 Produce glyphs/get display metrics for the display element IT is
19121 loaded with. See the description of struct display_iterator in
19122 dispextern.h for an overview of struct display_iterator. */
19124 void
19125 x_produce_glyphs (it)
19126 struct it *it;
19128 int extra_line_spacing = it->extra_line_spacing;
19130 it->glyph_not_available_p = 0;
19132 if (it->what == IT_CHARACTER)
19134 XChar2b char2b;
19135 XFontStruct *font;
19136 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19137 XCharStruct *pcm;
19138 int font_not_found_p;
19139 struct font_info *font_info;
19140 int boff; /* baseline offset */
19141 /* We may change it->multibyte_p upon unibyte<->multibyte
19142 conversion. So, save the current value now and restore it
19143 later.
19145 Note: It seems that we don't have to record multibyte_p in
19146 struct glyph because the character code itself tells if or
19147 not the character is multibyte. Thus, in the future, we must
19148 consider eliminating the field `multibyte_p' in the struct
19149 glyph. */
19150 int saved_multibyte_p = it->multibyte_p;
19152 /* Maybe translate single-byte characters to multibyte, or the
19153 other way. */
19154 it->char_to_display = it->c;
19155 if (!ASCII_BYTE_P (it->c))
19157 if (unibyte_display_via_language_environment
19158 && SINGLE_BYTE_CHAR_P (it->c)
19159 && (it->c >= 0240
19160 || !NILP (Vnonascii_translation_table)))
19162 it->char_to_display = unibyte_char_to_multibyte (it->c);
19163 it->multibyte_p = 1;
19164 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19165 face = FACE_FROM_ID (it->f, it->face_id);
19167 else if (!SINGLE_BYTE_CHAR_P (it->c)
19168 && !it->multibyte_p)
19170 it->multibyte_p = 1;
19171 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19172 face = FACE_FROM_ID (it->f, it->face_id);
19176 /* Get font to use. Encode IT->char_to_display. */
19177 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19178 &char2b, it->multibyte_p, 0);
19179 font = face->font;
19181 /* When no suitable font found, use the default font. */
19182 font_not_found_p = font == NULL;
19183 if (font_not_found_p)
19185 font = FRAME_FONT (it->f);
19186 boff = FRAME_BASELINE_OFFSET (it->f);
19187 font_info = NULL;
19189 else
19191 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19192 boff = font_info->baseline_offset;
19193 if (font_info->vertical_centering)
19194 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19197 if (it->char_to_display >= ' '
19198 && (!it->multibyte_p || it->char_to_display < 128))
19200 /* Either unibyte or ASCII. */
19201 int stretched_p;
19203 it->nglyphs = 1;
19205 pcm = rif->per_char_metric (font, &char2b,
19206 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19208 if (it->override_ascent >= 0)
19210 it->ascent = it->override_ascent;
19211 it->descent = it->override_descent;
19212 boff = it->override_boff;
19214 else
19216 it->ascent = FONT_BASE (font) + boff;
19217 it->descent = FONT_DESCENT (font) - boff;
19220 if (pcm)
19222 it->phys_ascent = pcm->ascent + boff;
19223 it->phys_descent = pcm->descent - boff;
19224 it->pixel_width = pcm->width;
19226 else
19228 it->glyph_not_available_p = 1;
19229 it->phys_ascent = it->ascent;
19230 it->phys_descent = it->descent;
19231 it->pixel_width = FONT_WIDTH (font);
19234 if (it->constrain_row_ascent_descent_p)
19236 if (it->descent > it->max_descent)
19238 it->ascent += it->descent - it->max_descent;
19239 it->descent = it->max_descent;
19241 if (it->ascent > it->max_ascent)
19243 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19244 it->ascent = it->max_ascent;
19246 it->phys_ascent = min (it->phys_ascent, it->ascent);
19247 it->phys_descent = min (it->phys_descent, it->descent);
19248 extra_line_spacing = 0;
19251 /* If this is a space inside a region of text with
19252 `space-width' property, change its width. */
19253 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19254 if (stretched_p)
19255 it->pixel_width *= XFLOATINT (it->space_width);
19257 /* If face has a box, add the box thickness to the character
19258 height. If character has a box line to the left and/or
19259 right, add the box line width to the character's width. */
19260 if (face->box != FACE_NO_BOX)
19262 int thick = face->box_line_width;
19264 if (thick > 0)
19266 it->ascent += thick;
19267 it->descent += thick;
19269 else
19270 thick = -thick;
19272 if (it->start_of_box_run_p)
19273 it->pixel_width += thick;
19274 if (it->end_of_box_run_p)
19275 it->pixel_width += thick;
19278 /* If face has an overline, add the height of the overline
19279 (1 pixel) and a 1 pixel margin to the character height. */
19280 if (face->overline_p)
19281 it->ascent += 2;
19283 if (it->constrain_row_ascent_descent_p)
19285 if (it->ascent > it->max_ascent)
19286 it->ascent = it->max_ascent;
19287 if (it->descent > it->max_descent)
19288 it->descent = it->max_descent;
19291 take_vertical_position_into_account (it);
19293 /* If we have to actually produce glyphs, do it. */
19294 if (it->glyph_row)
19296 if (stretched_p)
19298 /* Translate a space with a `space-width' property
19299 into a stretch glyph. */
19300 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19301 / FONT_HEIGHT (font));
19302 append_stretch_glyph (it, it->object, it->pixel_width,
19303 it->ascent + it->descent, ascent);
19305 else
19306 append_glyph (it);
19308 /* If characters with lbearing or rbearing are displayed
19309 in this line, record that fact in a flag of the
19310 glyph row. This is used to optimize X output code. */
19311 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19312 it->glyph_row->contains_overlapping_glyphs_p = 1;
19315 else if (it->char_to_display == '\n')
19317 /* A newline has no width but we need the height of the line.
19318 But if previous part of the line set a height, don't
19319 increase that height */
19321 Lisp_Object height;
19322 Lisp_Object total_height = Qnil;
19324 it->override_ascent = -1;
19325 it->pixel_width = 0;
19326 it->nglyphs = 0;
19328 height = get_line_height_property(it, Qline_height);
19329 /* Split (line-height total-height) list */
19330 if (CONSP (height)
19331 && CONSP (XCDR (height))
19332 && NILP (XCDR (XCDR (height))))
19334 total_height = XCAR (XCDR (height));
19335 height = XCAR (height);
19337 height = calc_line_height_property(it, height, font, boff, 1);
19339 if (it->override_ascent >= 0)
19341 it->ascent = it->override_ascent;
19342 it->descent = it->override_descent;
19343 boff = it->override_boff;
19345 else
19347 it->ascent = FONT_BASE (font) + boff;
19348 it->descent = FONT_DESCENT (font) - boff;
19351 if (EQ (height, Qt))
19353 if (it->descent > it->max_descent)
19355 it->ascent += it->descent - it->max_descent;
19356 it->descent = it->max_descent;
19358 if (it->ascent > it->max_ascent)
19360 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19361 it->ascent = it->max_ascent;
19363 it->phys_ascent = min (it->phys_ascent, it->ascent);
19364 it->phys_descent = min (it->phys_descent, it->descent);
19365 it->constrain_row_ascent_descent_p = 1;
19366 extra_line_spacing = 0;
19368 else
19370 Lisp_Object spacing;
19371 int total = 0;
19373 it->phys_ascent = it->ascent;
19374 it->phys_descent = it->descent;
19376 if ((it->max_ascent > 0 || it->max_descent > 0)
19377 && face->box != FACE_NO_BOX
19378 && face->box_line_width > 0)
19380 it->ascent += face->box_line_width;
19381 it->descent += face->box_line_width;
19383 if (!NILP (height)
19384 && XINT (height) > it->ascent + it->descent)
19385 it->ascent = XINT (height) - it->descent;
19387 if (!NILP (total_height))
19388 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19389 else
19391 spacing = get_line_height_property(it, Qline_spacing);
19392 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19394 if (INTEGERP (spacing))
19396 extra_line_spacing = XINT (spacing);
19397 if (!NILP (total_height))
19398 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19402 else if (it->char_to_display == '\t')
19404 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19405 int x = it->current_x + it->continuation_lines_width;
19406 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19408 /* If the distance from the current position to the next tab
19409 stop is less than a space character width, use the
19410 tab stop after that. */
19411 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19412 next_tab_x += tab_width;
19414 it->pixel_width = next_tab_x - x;
19415 it->nglyphs = 1;
19416 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19417 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19419 if (it->glyph_row)
19421 append_stretch_glyph (it, it->object, it->pixel_width,
19422 it->ascent + it->descent, it->ascent);
19425 else
19427 /* A multi-byte character. Assume that the display width of the
19428 character is the width of the character multiplied by the
19429 width of the font. */
19431 /* If we found a font, this font should give us the right
19432 metrics. If we didn't find a font, use the frame's
19433 default font and calculate the width of the character
19434 from the charset width; this is what old redisplay code
19435 did. */
19437 pcm = rif->per_char_metric (font, &char2b,
19438 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19440 if (font_not_found_p || !pcm)
19442 int charset = CHAR_CHARSET (it->char_to_display);
19444 it->glyph_not_available_p = 1;
19445 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19446 * CHARSET_WIDTH (charset));
19447 it->phys_ascent = FONT_BASE (font) + boff;
19448 it->phys_descent = FONT_DESCENT (font) - boff;
19450 else
19452 it->pixel_width = pcm->width;
19453 it->phys_ascent = pcm->ascent + boff;
19454 it->phys_descent = pcm->descent - boff;
19455 if (it->glyph_row
19456 && (pcm->lbearing < 0
19457 || pcm->rbearing > pcm->width))
19458 it->glyph_row->contains_overlapping_glyphs_p = 1;
19460 it->nglyphs = 1;
19461 it->ascent = FONT_BASE (font) + boff;
19462 it->descent = FONT_DESCENT (font) - boff;
19463 if (face->box != FACE_NO_BOX)
19465 int thick = face->box_line_width;
19467 if (thick > 0)
19469 it->ascent += thick;
19470 it->descent += thick;
19472 else
19473 thick = - thick;
19475 if (it->start_of_box_run_p)
19476 it->pixel_width += thick;
19477 if (it->end_of_box_run_p)
19478 it->pixel_width += thick;
19481 /* If face has an overline, add the height of the overline
19482 (1 pixel) and a 1 pixel margin to the character height. */
19483 if (face->overline_p)
19484 it->ascent += 2;
19486 take_vertical_position_into_account (it);
19488 if (it->glyph_row)
19489 append_glyph (it);
19491 it->multibyte_p = saved_multibyte_p;
19493 else if (it->what == IT_COMPOSITION)
19495 /* Note: A composition is represented as one glyph in the
19496 glyph matrix. There are no padding glyphs. */
19497 XChar2b char2b;
19498 XFontStruct *font;
19499 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19500 XCharStruct *pcm;
19501 int font_not_found_p;
19502 struct font_info *font_info;
19503 int boff; /* baseline offset */
19504 struct composition *cmp = composition_table[it->cmp_id];
19506 /* Maybe translate single-byte characters to multibyte. */
19507 it->char_to_display = it->c;
19508 if (unibyte_display_via_language_environment
19509 && SINGLE_BYTE_CHAR_P (it->c)
19510 && (it->c >= 0240
19511 || (it->c >= 0200
19512 && !NILP (Vnonascii_translation_table))))
19514 it->char_to_display = unibyte_char_to_multibyte (it->c);
19517 /* Get face and font to use. Encode IT->char_to_display. */
19518 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19519 face = FACE_FROM_ID (it->f, it->face_id);
19520 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19521 &char2b, it->multibyte_p, 0);
19522 font = face->font;
19524 /* When no suitable font found, use the default font. */
19525 font_not_found_p = font == NULL;
19526 if (font_not_found_p)
19528 font = FRAME_FONT (it->f);
19529 boff = FRAME_BASELINE_OFFSET (it->f);
19530 font_info = NULL;
19532 else
19534 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19535 boff = font_info->baseline_offset;
19536 if (font_info->vertical_centering)
19537 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19540 /* There are no padding glyphs, so there is only one glyph to
19541 produce for the composition. Important is that pixel_width,
19542 ascent and descent are the values of what is drawn by
19543 draw_glyphs (i.e. the values of the overall glyphs composed). */
19544 it->nglyphs = 1;
19546 /* If we have not yet calculated pixel size data of glyphs of
19547 the composition for the current face font, calculate them
19548 now. Theoretically, we have to check all fonts for the
19549 glyphs, but that requires much time and memory space. So,
19550 here we check only the font of the first glyph. This leads
19551 to incorrect display very rarely, and C-l (recenter) can
19552 correct the display anyway. */
19553 if (cmp->font != (void *) font)
19555 /* Ascent and descent of the font of the first character of
19556 this composition (adjusted by baseline offset). Ascent
19557 and descent of overall glyphs should not be less than
19558 them respectively. */
19559 int font_ascent = FONT_BASE (font) + boff;
19560 int font_descent = FONT_DESCENT (font) - boff;
19561 /* Bounding box of the overall glyphs. */
19562 int leftmost, rightmost, lowest, highest;
19563 int i, width, ascent, descent;
19565 cmp->font = (void *) font;
19567 /* Initialize the bounding box. */
19568 if (font_info
19569 && (pcm = rif->per_char_metric (font, &char2b,
19570 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19572 width = pcm->width;
19573 ascent = pcm->ascent;
19574 descent = pcm->descent;
19576 else
19578 width = FONT_WIDTH (font);
19579 ascent = FONT_BASE (font);
19580 descent = FONT_DESCENT (font);
19583 rightmost = width;
19584 lowest = - descent + boff;
19585 highest = ascent + boff;
19586 leftmost = 0;
19588 if (font_info
19589 && font_info->default_ascent
19590 && CHAR_TABLE_P (Vuse_default_ascent)
19591 && !NILP (Faref (Vuse_default_ascent,
19592 make_number (it->char_to_display))))
19593 highest = font_info->default_ascent + boff;
19595 /* Draw the first glyph at the normal position. It may be
19596 shifted to right later if some other glyphs are drawn at
19597 the left. */
19598 cmp->offsets[0] = 0;
19599 cmp->offsets[1] = boff;
19601 /* Set cmp->offsets for the remaining glyphs. */
19602 for (i = 1; i < cmp->glyph_len; i++)
19604 int left, right, btm, top;
19605 int ch = COMPOSITION_GLYPH (cmp, i);
19606 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19608 face = FACE_FROM_ID (it->f, face_id);
19609 get_char_face_and_encoding (it->f, ch, face->id,
19610 &char2b, it->multibyte_p, 0);
19611 font = face->font;
19612 if (font == NULL)
19614 font = FRAME_FONT (it->f);
19615 boff = FRAME_BASELINE_OFFSET (it->f);
19616 font_info = NULL;
19618 else
19620 font_info
19621 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19622 boff = font_info->baseline_offset;
19623 if (font_info->vertical_centering)
19624 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19627 if (font_info
19628 && (pcm = rif->per_char_metric (font, &char2b,
19629 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19631 width = pcm->width;
19632 ascent = pcm->ascent;
19633 descent = pcm->descent;
19635 else
19637 width = FONT_WIDTH (font);
19638 ascent = 1;
19639 descent = 0;
19642 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19644 /* Relative composition with or without
19645 alternate chars. */
19646 left = (leftmost + rightmost - width) / 2;
19647 btm = - descent + boff;
19648 if (font_info && font_info->relative_compose
19649 && (! CHAR_TABLE_P (Vignore_relative_composition)
19650 || NILP (Faref (Vignore_relative_composition,
19651 make_number (ch)))))
19654 if (- descent >= font_info->relative_compose)
19655 /* One extra pixel between two glyphs. */
19656 btm = highest + 1;
19657 else if (ascent <= 0)
19658 /* One extra pixel between two glyphs. */
19659 btm = lowest - 1 - ascent - descent;
19662 else
19664 /* A composition rule is specified by an integer
19665 value that encodes global and new reference
19666 points (GREF and NREF). GREF and NREF are
19667 specified by numbers as below:
19669 0---1---2 -- ascent
19673 9--10--11 -- center
19675 ---3---4---5--- baseline
19677 6---7---8 -- descent
19679 int rule = COMPOSITION_RULE (cmp, i);
19680 int gref, nref, grefx, grefy, nrefx, nrefy;
19682 COMPOSITION_DECODE_RULE (rule, gref, nref);
19683 grefx = gref % 3, nrefx = nref % 3;
19684 grefy = gref / 3, nrefy = nref / 3;
19686 left = (leftmost
19687 + grefx * (rightmost - leftmost) / 2
19688 - nrefx * width / 2);
19689 btm = ((grefy == 0 ? highest
19690 : grefy == 1 ? 0
19691 : grefy == 2 ? lowest
19692 : (highest + lowest) / 2)
19693 - (nrefy == 0 ? ascent + descent
19694 : nrefy == 1 ? descent - boff
19695 : nrefy == 2 ? 0
19696 : (ascent + descent) / 2));
19699 cmp->offsets[i * 2] = left;
19700 cmp->offsets[i * 2 + 1] = btm + descent;
19702 /* Update the bounding box of the overall glyphs. */
19703 right = left + width;
19704 top = btm + descent + ascent;
19705 if (left < leftmost)
19706 leftmost = left;
19707 if (right > rightmost)
19708 rightmost = right;
19709 if (top > highest)
19710 highest = top;
19711 if (btm < lowest)
19712 lowest = btm;
19715 /* If there are glyphs whose x-offsets are negative,
19716 shift all glyphs to the right and make all x-offsets
19717 non-negative. */
19718 if (leftmost < 0)
19720 for (i = 0; i < cmp->glyph_len; i++)
19721 cmp->offsets[i * 2] -= leftmost;
19722 rightmost -= leftmost;
19725 cmp->pixel_width = rightmost;
19726 cmp->ascent = highest;
19727 cmp->descent = - lowest;
19728 if (cmp->ascent < font_ascent)
19729 cmp->ascent = font_ascent;
19730 if (cmp->descent < font_descent)
19731 cmp->descent = font_descent;
19734 it->pixel_width = cmp->pixel_width;
19735 it->ascent = it->phys_ascent = cmp->ascent;
19736 it->descent = it->phys_descent = cmp->descent;
19738 if (face->box != FACE_NO_BOX)
19740 int thick = face->box_line_width;
19742 if (thick > 0)
19744 it->ascent += thick;
19745 it->descent += thick;
19747 else
19748 thick = - thick;
19750 if (it->start_of_box_run_p)
19751 it->pixel_width += thick;
19752 if (it->end_of_box_run_p)
19753 it->pixel_width += thick;
19756 /* If face has an overline, add the height of the overline
19757 (1 pixel) and a 1 pixel margin to the character height. */
19758 if (face->overline_p)
19759 it->ascent += 2;
19761 take_vertical_position_into_account (it);
19763 if (it->glyph_row)
19764 append_composite_glyph (it);
19766 else if (it->what == IT_IMAGE)
19767 produce_image_glyph (it);
19768 else if (it->what == IT_STRETCH)
19769 produce_stretch_glyph (it);
19771 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19772 because this isn't true for images with `:ascent 100'. */
19773 xassert (it->ascent >= 0 && it->descent >= 0);
19774 if (it->area == TEXT_AREA)
19775 it->current_x += it->pixel_width;
19777 if (extra_line_spacing > 0)
19779 it->descent += extra_line_spacing;
19780 if (extra_line_spacing > it->max_extra_line_spacing)
19781 it->max_extra_line_spacing = extra_line_spacing;
19784 it->max_ascent = max (it->max_ascent, it->ascent);
19785 it->max_descent = max (it->max_descent, it->descent);
19786 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19787 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19790 /* EXPORT for RIF:
19791 Output LEN glyphs starting at START at the nominal cursor position.
19792 Advance the nominal cursor over the text. The global variable
19793 updated_window contains the window being updated, updated_row is
19794 the glyph row being updated, and updated_area is the area of that
19795 row being updated. */
19797 void
19798 x_write_glyphs (start, len)
19799 struct glyph *start;
19800 int len;
19802 int x, hpos;
19804 xassert (updated_window && updated_row);
19805 BLOCK_INPUT;
19807 /* Write glyphs. */
19809 hpos = start - updated_row->glyphs[updated_area];
19810 x = draw_glyphs (updated_window, output_cursor.x,
19811 updated_row, updated_area,
19812 hpos, hpos + len,
19813 DRAW_NORMAL_TEXT, 0);
19815 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19816 if (updated_area == TEXT_AREA
19817 && updated_window->phys_cursor_on_p
19818 && updated_window->phys_cursor.vpos == output_cursor.vpos
19819 && updated_window->phys_cursor.hpos >= hpos
19820 && updated_window->phys_cursor.hpos < hpos + len)
19821 updated_window->phys_cursor_on_p = 0;
19823 UNBLOCK_INPUT;
19825 /* Advance the output cursor. */
19826 output_cursor.hpos += len;
19827 output_cursor.x = x;
19831 /* EXPORT for RIF:
19832 Insert LEN glyphs from START at the nominal cursor position. */
19834 void
19835 x_insert_glyphs (start, len)
19836 struct glyph *start;
19837 int len;
19839 struct frame *f;
19840 struct window *w;
19841 int line_height, shift_by_width, shifted_region_width;
19842 struct glyph_row *row;
19843 struct glyph *glyph;
19844 int frame_x, frame_y, hpos;
19846 xassert (updated_window && updated_row);
19847 BLOCK_INPUT;
19848 w = updated_window;
19849 f = XFRAME (WINDOW_FRAME (w));
19851 /* Get the height of the line we are in. */
19852 row = updated_row;
19853 line_height = row->height;
19855 /* Get the width of the glyphs to insert. */
19856 shift_by_width = 0;
19857 for (glyph = start; glyph < start + len; ++glyph)
19858 shift_by_width += glyph->pixel_width;
19860 /* Get the width of the region to shift right. */
19861 shifted_region_width = (window_box_width (w, updated_area)
19862 - output_cursor.x
19863 - shift_by_width);
19865 /* Shift right. */
19866 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19867 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19869 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19870 line_height, shift_by_width);
19872 /* Write the glyphs. */
19873 hpos = start - row->glyphs[updated_area];
19874 draw_glyphs (w, output_cursor.x, row, updated_area,
19875 hpos, hpos + len,
19876 DRAW_NORMAL_TEXT, 0);
19878 /* Advance the output cursor. */
19879 output_cursor.hpos += len;
19880 output_cursor.x += shift_by_width;
19881 UNBLOCK_INPUT;
19885 /* EXPORT for RIF:
19886 Erase the current text line from the nominal cursor position
19887 (inclusive) to pixel column TO_X (exclusive). The idea is that
19888 everything from TO_X onward is already erased.
19890 TO_X is a pixel position relative to updated_area of
19891 updated_window. TO_X == -1 means clear to the end of this area. */
19893 void
19894 x_clear_end_of_line (to_x)
19895 int to_x;
19897 struct frame *f;
19898 struct window *w = updated_window;
19899 int max_x, min_y, max_y;
19900 int from_x, from_y, to_y;
19902 xassert (updated_window && updated_row);
19903 f = XFRAME (w->frame);
19905 if (updated_row->full_width_p)
19906 max_x = WINDOW_TOTAL_WIDTH (w);
19907 else
19908 max_x = window_box_width (w, updated_area);
19909 max_y = window_text_bottom_y (w);
19911 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19912 of window. For TO_X > 0, truncate to end of drawing area. */
19913 if (to_x == 0)
19914 return;
19915 else if (to_x < 0)
19916 to_x = max_x;
19917 else
19918 to_x = min (to_x, max_x);
19920 to_y = min (max_y, output_cursor.y + updated_row->height);
19922 /* Notice if the cursor will be cleared by this operation. */
19923 if (!updated_row->full_width_p)
19924 notice_overwritten_cursor (w, updated_area,
19925 output_cursor.x, -1,
19926 updated_row->y,
19927 MATRIX_ROW_BOTTOM_Y (updated_row));
19929 from_x = output_cursor.x;
19931 /* Translate to frame coordinates. */
19932 if (updated_row->full_width_p)
19934 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19935 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19937 else
19939 int area_left = window_box_left (w, updated_area);
19940 from_x += area_left;
19941 to_x += area_left;
19944 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19945 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19946 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19948 /* Prevent inadvertently clearing to end of the X window. */
19949 if (to_x > from_x && to_y > from_y)
19951 BLOCK_INPUT;
19952 rif->clear_frame_area (f, from_x, from_y,
19953 to_x - from_x, to_y - from_y);
19954 UNBLOCK_INPUT;
19958 #endif /* HAVE_WINDOW_SYSTEM */
19962 /***********************************************************************
19963 Cursor types
19964 ***********************************************************************/
19966 /* Value is the internal representation of the specified cursor type
19967 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19968 of the bar cursor. */
19970 static enum text_cursor_kinds
19971 get_specified_cursor_type (arg, width)
19972 Lisp_Object arg;
19973 int *width;
19975 enum text_cursor_kinds type;
19977 if (NILP (arg))
19978 return NO_CURSOR;
19980 if (EQ (arg, Qbox))
19981 return FILLED_BOX_CURSOR;
19983 if (EQ (arg, Qhollow))
19984 return HOLLOW_BOX_CURSOR;
19986 if (EQ (arg, Qbar))
19988 *width = 2;
19989 return BAR_CURSOR;
19992 if (CONSP (arg)
19993 && EQ (XCAR (arg), Qbar)
19994 && INTEGERP (XCDR (arg))
19995 && XINT (XCDR (arg)) >= 0)
19997 *width = XINT (XCDR (arg));
19998 return BAR_CURSOR;
20001 if (EQ (arg, Qhbar))
20003 *width = 2;
20004 return HBAR_CURSOR;
20007 if (CONSP (arg)
20008 && EQ (XCAR (arg), Qhbar)
20009 && INTEGERP (XCDR (arg))
20010 && XINT (XCDR (arg)) >= 0)
20012 *width = XINT (XCDR (arg));
20013 return HBAR_CURSOR;
20016 /* Treat anything unknown as "hollow box cursor".
20017 It was bad to signal an error; people have trouble fixing
20018 .Xdefaults with Emacs, when it has something bad in it. */
20019 type = HOLLOW_BOX_CURSOR;
20021 return type;
20024 /* Set the default cursor types for specified frame. */
20025 void
20026 set_frame_cursor_types (f, arg)
20027 struct frame *f;
20028 Lisp_Object arg;
20030 int width;
20031 Lisp_Object tem;
20033 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20034 FRAME_CURSOR_WIDTH (f) = width;
20036 /* By default, set up the blink-off state depending on the on-state. */
20038 tem = Fassoc (arg, Vblink_cursor_alist);
20039 if (!NILP (tem))
20041 FRAME_BLINK_OFF_CURSOR (f)
20042 = get_specified_cursor_type (XCDR (tem), &width);
20043 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20045 else
20046 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20050 /* Return the cursor we want to be displayed in window W. Return
20051 width of bar/hbar cursor through WIDTH arg. Return with
20052 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20053 (i.e. if the `system caret' should track this cursor).
20055 In a mini-buffer window, we want the cursor only to appear if we
20056 are reading input from this window. For the selected window, we
20057 want the cursor type given by the frame parameter or buffer local
20058 setting of cursor-type. If explicitly marked off, draw no cursor.
20059 In all other cases, we want a hollow box cursor. */
20061 static enum text_cursor_kinds
20062 get_window_cursor_type (w, glyph, width, active_cursor)
20063 struct window *w;
20064 struct glyph *glyph;
20065 int *width;
20066 int *active_cursor;
20068 struct frame *f = XFRAME (w->frame);
20069 struct buffer *b = XBUFFER (w->buffer);
20070 int cursor_type = DEFAULT_CURSOR;
20071 Lisp_Object alt_cursor;
20072 int non_selected = 0;
20074 *active_cursor = 1;
20076 /* Echo area */
20077 if (cursor_in_echo_area
20078 && FRAME_HAS_MINIBUF_P (f)
20079 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20081 if (w == XWINDOW (echo_area_window))
20083 *width = FRAME_CURSOR_WIDTH (f);
20084 return FRAME_DESIRED_CURSOR (f);
20087 *active_cursor = 0;
20088 non_selected = 1;
20091 /* Nonselected window or nonselected frame. */
20092 else if (w != XWINDOW (f->selected_window)
20093 #ifdef HAVE_WINDOW_SYSTEM
20094 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20095 #endif
20098 *active_cursor = 0;
20100 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20101 return NO_CURSOR;
20103 non_selected = 1;
20106 /* Never display a cursor in a window in which cursor-type is nil. */
20107 if (NILP (b->cursor_type))
20108 return NO_CURSOR;
20110 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20111 if (non_selected)
20113 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
20114 return get_specified_cursor_type (alt_cursor, width);
20117 /* Get the normal cursor type for this window. */
20118 if (EQ (b->cursor_type, Qt))
20120 cursor_type = FRAME_DESIRED_CURSOR (f);
20121 *width = FRAME_CURSOR_WIDTH (f);
20123 else
20124 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20126 /* Use normal cursor if not blinked off. */
20127 if (!w->cursor_off_p)
20129 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20130 if (cursor_type == FILLED_BOX_CURSOR)
20131 cursor_type = HOLLOW_BOX_CURSOR;
20133 return cursor_type;
20136 /* Cursor is blinked off, so determine how to "toggle" it. */
20138 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20139 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20140 return get_specified_cursor_type (XCDR (alt_cursor), width);
20142 /* Then see if frame has specified a specific blink off cursor type. */
20143 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20145 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20146 return FRAME_BLINK_OFF_CURSOR (f);
20149 #if 0
20150 /* Some people liked having a permanently visible blinking cursor,
20151 while others had very strong opinions against it. So it was
20152 decided to remove it. KFS 2003-09-03 */
20154 /* Finally perform built-in cursor blinking:
20155 filled box <-> hollow box
20156 wide [h]bar <-> narrow [h]bar
20157 narrow [h]bar <-> no cursor
20158 other type <-> no cursor */
20160 if (cursor_type == FILLED_BOX_CURSOR)
20161 return HOLLOW_BOX_CURSOR;
20163 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20165 *width = 1;
20166 return cursor_type;
20168 #endif
20170 return NO_CURSOR;
20174 #ifdef HAVE_WINDOW_SYSTEM
20176 /* Notice when the text cursor of window W has been completely
20177 overwritten by a drawing operation that outputs glyphs in AREA
20178 starting at X0 and ending at X1 in the line starting at Y0 and
20179 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20180 the rest of the line after X0 has been written. Y coordinates
20181 are window-relative. */
20183 static void
20184 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20185 struct window *w;
20186 enum glyph_row_area area;
20187 int x0, y0, x1, y1;
20189 int cx0, cx1, cy0, cy1;
20190 struct glyph_row *row;
20192 if (!w->phys_cursor_on_p)
20193 return;
20194 if (area != TEXT_AREA)
20195 return;
20197 row = w->current_matrix->rows + w->phys_cursor.vpos;
20198 if (!row->displays_text_p)
20199 return;
20201 if (row->cursor_in_fringe_p)
20203 row->cursor_in_fringe_p = 0;
20204 draw_fringe_bitmap (w, row, 0);
20205 w->phys_cursor_on_p = 0;
20206 return;
20209 cx0 = w->phys_cursor.x;
20210 cx1 = cx0 + w->phys_cursor_width;
20211 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20212 return;
20214 /* The cursor image will be completely removed from the
20215 screen if the output area intersects the cursor area in
20216 y-direction. When we draw in [y0 y1[, and some part of
20217 the cursor is at y < y0, that part must have been drawn
20218 before. When scrolling, the cursor is erased before
20219 actually scrolling, so we don't come here. When not
20220 scrolling, the rows above the old cursor row must have
20221 changed, and in this case these rows must have written
20222 over the cursor image.
20224 Likewise if part of the cursor is below y1, with the
20225 exception of the cursor being in the first blank row at
20226 the buffer and window end because update_text_area
20227 doesn't draw that row. (Except when it does, but
20228 that's handled in update_text_area.) */
20230 cy0 = w->phys_cursor.y;
20231 cy1 = cy0 + w->phys_cursor_height;
20232 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20233 return;
20235 w->phys_cursor_on_p = 0;
20238 #endif /* HAVE_WINDOW_SYSTEM */
20241 /************************************************************************
20242 Mouse Face
20243 ************************************************************************/
20245 #ifdef HAVE_WINDOW_SYSTEM
20247 /* EXPORT for RIF:
20248 Fix the display of area AREA of overlapping row ROW in window W. */
20250 void
20251 x_fix_overlapping_area (w, row, area)
20252 struct window *w;
20253 struct glyph_row *row;
20254 enum glyph_row_area area;
20256 int i, x;
20258 BLOCK_INPUT;
20260 x = 0;
20261 for (i = 0; i < row->used[area];)
20263 if (row->glyphs[area][i].overlaps_vertically_p)
20265 int start = i, start_x = x;
20269 x += row->glyphs[area][i].pixel_width;
20270 ++i;
20272 while (i < row->used[area]
20273 && row->glyphs[area][i].overlaps_vertically_p);
20275 draw_glyphs (w, start_x, row, area,
20276 start, i,
20277 DRAW_NORMAL_TEXT, 1);
20279 else
20281 x += row->glyphs[area][i].pixel_width;
20282 ++i;
20286 UNBLOCK_INPUT;
20290 /* EXPORT:
20291 Draw the cursor glyph of window W in glyph row ROW. See the
20292 comment of draw_glyphs for the meaning of HL. */
20294 void
20295 draw_phys_cursor_glyph (w, row, hl)
20296 struct window *w;
20297 struct glyph_row *row;
20298 enum draw_glyphs_face hl;
20300 /* If cursor hpos is out of bounds, don't draw garbage. This can
20301 happen in mini-buffer windows when switching between echo area
20302 glyphs and mini-buffer. */
20303 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20305 int on_p = w->phys_cursor_on_p;
20306 int x1;
20307 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20308 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20309 hl, 0);
20310 w->phys_cursor_on_p = on_p;
20312 if (hl == DRAW_CURSOR)
20313 w->phys_cursor_width = x1 - w->phys_cursor.x;
20314 /* When we erase the cursor, and ROW is overlapped by other
20315 rows, make sure that these overlapping parts of other rows
20316 are redrawn. */
20317 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20319 if (row > w->current_matrix->rows
20320 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20321 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20323 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20324 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20325 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20331 /* EXPORT:
20332 Erase the image of a cursor of window W from the screen. */
20334 void
20335 erase_phys_cursor (w)
20336 struct window *w;
20338 struct frame *f = XFRAME (w->frame);
20339 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20340 int hpos = w->phys_cursor.hpos;
20341 int vpos = w->phys_cursor.vpos;
20342 int mouse_face_here_p = 0;
20343 struct glyph_matrix *active_glyphs = w->current_matrix;
20344 struct glyph_row *cursor_row;
20345 struct glyph *cursor_glyph;
20346 enum draw_glyphs_face hl;
20348 /* No cursor displayed or row invalidated => nothing to do on the
20349 screen. */
20350 if (w->phys_cursor_type == NO_CURSOR)
20351 goto mark_cursor_off;
20353 /* VPOS >= active_glyphs->nrows means that window has been resized.
20354 Don't bother to erase the cursor. */
20355 if (vpos >= active_glyphs->nrows)
20356 goto mark_cursor_off;
20358 /* If row containing cursor is marked invalid, there is nothing we
20359 can do. */
20360 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20361 if (!cursor_row->enabled_p)
20362 goto mark_cursor_off;
20364 /* If line spacing is > 0, old cursor may only be partially visible in
20365 window after split-window. So adjust visible height. */
20366 cursor_row->visible_height = min (cursor_row->visible_height,
20367 window_text_bottom_y (w) - cursor_row->y);
20369 /* If row is completely invisible, don't attempt to delete a cursor which
20370 isn't there. This can happen if cursor is at top of a window, and
20371 we switch to a buffer with a header line in that window. */
20372 if (cursor_row->visible_height <= 0)
20373 goto mark_cursor_off;
20375 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20376 if (cursor_row->cursor_in_fringe_p)
20378 cursor_row->cursor_in_fringe_p = 0;
20379 draw_fringe_bitmap (w, cursor_row, 0);
20380 goto mark_cursor_off;
20383 /* This can happen when the new row is shorter than the old one.
20384 In this case, either draw_glyphs or clear_end_of_line
20385 should have cleared the cursor. Note that we wouldn't be
20386 able to erase the cursor in this case because we don't have a
20387 cursor glyph at hand. */
20388 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20389 goto mark_cursor_off;
20391 /* If the cursor is in the mouse face area, redisplay that when
20392 we clear the cursor. */
20393 if (! NILP (dpyinfo->mouse_face_window)
20394 && w == XWINDOW (dpyinfo->mouse_face_window)
20395 && (vpos > dpyinfo->mouse_face_beg_row
20396 || (vpos == dpyinfo->mouse_face_beg_row
20397 && hpos >= dpyinfo->mouse_face_beg_col))
20398 && (vpos < dpyinfo->mouse_face_end_row
20399 || (vpos == dpyinfo->mouse_face_end_row
20400 && hpos < dpyinfo->mouse_face_end_col))
20401 /* Don't redraw the cursor's spot in mouse face if it is at the
20402 end of a line (on a newline). The cursor appears there, but
20403 mouse highlighting does not. */
20404 && cursor_row->used[TEXT_AREA] > hpos)
20405 mouse_face_here_p = 1;
20407 /* Maybe clear the display under the cursor. */
20408 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20410 int x, y;
20411 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20412 int width;
20414 cursor_glyph = get_phys_cursor_glyph (w);
20415 if (cursor_glyph == NULL)
20416 goto mark_cursor_off;
20418 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20419 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20420 width = min (cursor_glyph->pixel_width,
20421 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20423 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20426 /* Erase the cursor by redrawing the character underneath it. */
20427 if (mouse_face_here_p)
20428 hl = DRAW_MOUSE_FACE;
20429 else
20430 hl = DRAW_NORMAL_TEXT;
20431 draw_phys_cursor_glyph (w, cursor_row, hl);
20433 mark_cursor_off:
20434 w->phys_cursor_on_p = 0;
20435 w->phys_cursor_type = NO_CURSOR;
20439 /* EXPORT:
20440 Display or clear cursor of window W. If ON is zero, clear the
20441 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20442 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20444 void
20445 display_and_set_cursor (w, on, hpos, vpos, x, y)
20446 struct window *w;
20447 int on, hpos, vpos, x, y;
20449 struct frame *f = XFRAME (w->frame);
20450 int new_cursor_type;
20451 int new_cursor_width;
20452 int active_cursor;
20453 struct glyph_row *glyph_row;
20454 struct glyph *glyph;
20456 /* This is pointless on invisible frames, and dangerous on garbaged
20457 windows and frames; in the latter case, the frame or window may
20458 be in the midst of changing its size, and x and y may be off the
20459 window. */
20460 if (! FRAME_VISIBLE_P (f)
20461 || FRAME_GARBAGED_P (f)
20462 || vpos >= w->current_matrix->nrows
20463 || hpos >= w->current_matrix->matrix_w)
20464 return;
20466 /* If cursor is off and we want it off, return quickly. */
20467 if (!on && !w->phys_cursor_on_p)
20468 return;
20470 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20471 /* If cursor row is not enabled, we don't really know where to
20472 display the cursor. */
20473 if (!glyph_row->enabled_p)
20475 w->phys_cursor_on_p = 0;
20476 return;
20479 glyph = NULL;
20480 if (!glyph_row->exact_window_width_line_p
20481 || hpos < glyph_row->used[TEXT_AREA])
20482 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20484 xassert (interrupt_input_blocked);
20486 /* Set new_cursor_type to the cursor we want to be displayed. */
20487 new_cursor_type = get_window_cursor_type (w, glyph,
20488 &new_cursor_width, &active_cursor);
20490 /* If cursor is currently being shown and we don't want it to be or
20491 it is in the wrong place, or the cursor type is not what we want,
20492 erase it. */
20493 if (w->phys_cursor_on_p
20494 && (!on
20495 || w->phys_cursor.x != x
20496 || w->phys_cursor.y != y
20497 || new_cursor_type != w->phys_cursor_type
20498 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20499 && new_cursor_width != w->phys_cursor_width)))
20500 erase_phys_cursor (w);
20502 /* Don't check phys_cursor_on_p here because that flag is only set
20503 to zero in some cases where we know that the cursor has been
20504 completely erased, to avoid the extra work of erasing the cursor
20505 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20506 still not be visible, or it has only been partly erased. */
20507 if (on)
20509 w->phys_cursor_ascent = glyph_row->ascent;
20510 w->phys_cursor_height = glyph_row->height;
20512 /* Set phys_cursor_.* before x_draw_.* is called because some
20513 of them may need the information. */
20514 w->phys_cursor.x = x;
20515 w->phys_cursor.y = glyph_row->y;
20516 w->phys_cursor.hpos = hpos;
20517 w->phys_cursor.vpos = vpos;
20520 rif->draw_window_cursor (w, glyph_row, x, y,
20521 new_cursor_type, new_cursor_width,
20522 on, active_cursor);
20526 /* Switch the display of W's cursor on or off, according to the value
20527 of ON. */
20529 static void
20530 update_window_cursor (w, on)
20531 struct window *w;
20532 int on;
20534 /* Don't update cursor in windows whose frame is in the process
20535 of being deleted. */
20536 if (w->current_matrix)
20538 BLOCK_INPUT;
20539 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20540 w->phys_cursor.x, w->phys_cursor.y);
20541 UNBLOCK_INPUT;
20546 /* Call update_window_cursor with parameter ON_P on all leaf windows
20547 in the window tree rooted at W. */
20549 static void
20550 update_cursor_in_window_tree (w, on_p)
20551 struct window *w;
20552 int on_p;
20554 while (w)
20556 if (!NILP (w->hchild))
20557 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20558 else if (!NILP (w->vchild))
20559 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20560 else
20561 update_window_cursor (w, on_p);
20563 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20568 /* EXPORT:
20569 Display the cursor on window W, or clear it, according to ON_P.
20570 Don't change the cursor's position. */
20572 void
20573 x_update_cursor (f, on_p)
20574 struct frame *f;
20575 int on_p;
20577 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20581 /* EXPORT:
20582 Clear the cursor of window W to background color, and mark the
20583 cursor as not shown. This is used when the text where the cursor
20584 is is about to be rewritten. */
20586 void
20587 x_clear_cursor (w)
20588 struct window *w;
20590 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20591 update_window_cursor (w, 0);
20595 /* EXPORT:
20596 Display the active region described by mouse_face_* according to DRAW. */
20598 void
20599 show_mouse_face (dpyinfo, draw)
20600 Display_Info *dpyinfo;
20601 enum draw_glyphs_face draw;
20603 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20604 struct frame *f = XFRAME (WINDOW_FRAME (w));
20606 if (/* If window is in the process of being destroyed, don't bother
20607 to do anything. */
20608 w->current_matrix != NULL
20609 /* Don't update mouse highlight if hidden */
20610 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20611 /* Recognize when we are called to operate on rows that don't exist
20612 anymore. This can happen when a window is split. */
20613 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20615 int phys_cursor_on_p = w->phys_cursor_on_p;
20616 struct glyph_row *row, *first, *last;
20618 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20619 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20621 for (row = first; row <= last && row->enabled_p; ++row)
20623 int start_hpos, end_hpos, start_x;
20625 /* For all but the first row, the highlight starts at column 0. */
20626 if (row == first)
20628 start_hpos = dpyinfo->mouse_face_beg_col;
20629 start_x = dpyinfo->mouse_face_beg_x;
20631 else
20633 start_hpos = 0;
20634 start_x = 0;
20637 if (row == last)
20638 end_hpos = dpyinfo->mouse_face_end_col;
20639 else
20640 end_hpos = row->used[TEXT_AREA];
20642 if (end_hpos > start_hpos)
20644 draw_glyphs (w, start_x, row, TEXT_AREA,
20645 start_hpos, end_hpos,
20646 draw, 0);
20648 row->mouse_face_p
20649 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20653 /* When we've written over the cursor, arrange for it to
20654 be displayed again. */
20655 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20657 BLOCK_INPUT;
20658 display_and_set_cursor (w, 1,
20659 w->phys_cursor.hpos, w->phys_cursor.vpos,
20660 w->phys_cursor.x, w->phys_cursor.y);
20661 UNBLOCK_INPUT;
20665 /* Change the mouse cursor. */
20666 if (draw == DRAW_NORMAL_TEXT)
20667 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20668 else if (draw == DRAW_MOUSE_FACE)
20669 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20670 else
20671 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20674 /* EXPORT:
20675 Clear out the mouse-highlighted active region.
20676 Redraw it un-highlighted first. Value is non-zero if mouse
20677 face was actually drawn unhighlighted. */
20680 clear_mouse_face (dpyinfo)
20681 Display_Info *dpyinfo;
20683 int cleared = 0;
20685 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20687 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20688 cleared = 1;
20691 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20692 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20693 dpyinfo->mouse_face_window = Qnil;
20694 dpyinfo->mouse_face_overlay = Qnil;
20695 return cleared;
20699 /* EXPORT:
20700 Non-zero if physical cursor of window W is within mouse face. */
20703 cursor_in_mouse_face_p (w)
20704 struct window *w;
20706 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20707 int in_mouse_face = 0;
20709 if (WINDOWP (dpyinfo->mouse_face_window)
20710 && XWINDOW (dpyinfo->mouse_face_window) == w)
20712 int hpos = w->phys_cursor.hpos;
20713 int vpos = w->phys_cursor.vpos;
20715 if (vpos >= dpyinfo->mouse_face_beg_row
20716 && vpos <= dpyinfo->mouse_face_end_row
20717 && (vpos > dpyinfo->mouse_face_beg_row
20718 || hpos >= dpyinfo->mouse_face_beg_col)
20719 && (vpos < dpyinfo->mouse_face_end_row
20720 || hpos < dpyinfo->mouse_face_end_col
20721 || dpyinfo->mouse_face_past_end))
20722 in_mouse_face = 1;
20725 return in_mouse_face;
20731 /* Find the glyph matrix position of buffer position CHARPOS in window
20732 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20733 current glyphs must be up to date. If CHARPOS is above window
20734 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20735 of last line in W. In the row containing CHARPOS, stop before glyphs
20736 having STOP as object. */
20738 #if 1 /* This is a version of fast_find_position that's more correct
20739 in the presence of hscrolling, for example. I didn't install
20740 it right away because the problem fixed is minor, it failed
20741 in 20.x as well, and I think it's too risky to install
20742 so near the release of 21.1. 2001-09-25 gerd. */
20744 static int
20745 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20746 struct window *w;
20747 int charpos;
20748 int *hpos, *vpos, *x, *y;
20749 Lisp_Object stop;
20751 struct glyph_row *row, *first;
20752 struct glyph *glyph, *end;
20753 int past_end = 0;
20755 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20756 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20758 *x = first->x;
20759 *y = first->y;
20760 *hpos = 0;
20761 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20762 return 1;
20765 row = row_containing_pos (w, charpos, first, NULL, 0);
20766 if (row == NULL)
20768 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20769 past_end = 1;
20772 /* If whole rows or last part of a row came from a display overlay,
20773 row_containing_pos will skip over such rows because their end pos
20774 equals the start pos of the overlay or interval. Backtrack if we
20775 have a STOP object and previous row's end glyph came from STOP. */
20776 if (!NILP (stop))
20778 struct glyph_row *prev;
20779 while ((prev = row - 1, prev >= first)
20780 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20781 && prev->used[TEXT_AREA] > 0)
20783 end = prev->glyphs[TEXT_AREA];
20784 glyph = end + prev->used[TEXT_AREA];
20785 while (--glyph >= end
20786 && INTEGERP (glyph->object));
20787 if (glyph < end
20788 || !EQ (stop, glyph->object))
20789 break;
20790 row = prev;
20794 *x = row->x;
20795 *y = row->y;
20796 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20798 glyph = row->glyphs[TEXT_AREA];
20799 end = glyph + row->used[TEXT_AREA];
20801 /* Skip over glyphs not having an object at the start of the row.
20802 These are special glyphs like truncation marks on terminal
20803 frames. */
20804 if (row->displays_text_p)
20805 while (glyph < end
20806 && INTEGERP (glyph->object)
20807 && !EQ (stop, glyph->object)
20808 && glyph->charpos < 0)
20810 *x += glyph->pixel_width;
20811 ++glyph;
20814 while (glyph < end
20815 && !INTEGERP (glyph->object)
20816 && !EQ (stop, glyph->object)
20817 && (!BUFFERP (glyph->object)
20818 || glyph->charpos < charpos))
20820 *x += glyph->pixel_width;
20821 ++glyph;
20824 *hpos = glyph - row->glyphs[TEXT_AREA];
20825 return !past_end;
20828 #else /* not 1 */
20830 static int
20831 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20832 struct window *w;
20833 int pos;
20834 int *hpos, *vpos, *x, *y;
20835 Lisp_Object stop;
20837 int i;
20838 int lastcol;
20839 int maybe_next_line_p = 0;
20840 int line_start_position;
20841 int yb = window_text_bottom_y (w);
20842 struct glyph_row *row, *best_row;
20843 int row_vpos, best_row_vpos;
20844 int current_x;
20846 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20847 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20849 while (row->y < yb)
20851 if (row->used[TEXT_AREA])
20852 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20853 else
20854 line_start_position = 0;
20856 if (line_start_position > pos)
20857 break;
20858 /* If the position sought is the end of the buffer,
20859 don't include the blank lines at the bottom of the window. */
20860 else if (line_start_position == pos
20861 && pos == BUF_ZV (XBUFFER (w->buffer)))
20863 maybe_next_line_p = 1;
20864 break;
20866 else if (line_start_position > 0)
20868 best_row = row;
20869 best_row_vpos = row_vpos;
20872 if (row->y + row->height >= yb)
20873 break;
20875 ++row;
20876 ++row_vpos;
20879 /* Find the right column within BEST_ROW. */
20880 lastcol = 0;
20881 current_x = best_row->x;
20882 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20884 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20885 int charpos = glyph->charpos;
20887 if (BUFFERP (glyph->object))
20889 if (charpos == pos)
20891 *hpos = i;
20892 *vpos = best_row_vpos;
20893 *x = current_x;
20894 *y = best_row->y;
20895 return 1;
20897 else if (charpos > pos)
20898 break;
20900 else if (EQ (glyph->object, stop))
20901 break;
20903 if (charpos > 0)
20904 lastcol = i;
20905 current_x += glyph->pixel_width;
20908 /* If we're looking for the end of the buffer,
20909 and we didn't find it in the line we scanned,
20910 use the start of the following line. */
20911 if (maybe_next_line_p)
20913 ++best_row;
20914 ++best_row_vpos;
20915 lastcol = 0;
20916 current_x = best_row->x;
20919 *vpos = best_row_vpos;
20920 *hpos = lastcol + 1;
20921 *x = current_x;
20922 *y = best_row->y;
20923 return 0;
20926 #endif /* not 1 */
20929 /* Find the position of the glyph for position POS in OBJECT in
20930 window W's current matrix, and return in *X, *Y the pixel
20931 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20933 RIGHT_P non-zero means return the position of the right edge of the
20934 glyph, RIGHT_P zero means return the left edge position.
20936 If no glyph for POS exists in the matrix, return the position of
20937 the glyph with the next smaller position that is in the matrix, if
20938 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20939 exists in the matrix, return the position of the glyph with the
20940 next larger position in OBJECT.
20942 Value is non-zero if a glyph was found. */
20944 static int
20945 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20946 struct window *w;
20947 int pos;
20948 Lisp_Object object;
20949 int *hpos, *vpos, *x, *y;
20950 int right_p;
20952 int yb = window_text_bottom_y (w);
20953 struct glyph_row *r;
20954 struct glyph *best_glyph = NULL;
20955 struct glyph_row *best_row = NULL;
20956 int best_x = 0;
20958 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20959 r->enabled_p && r->y < yb;
20960 ++r)
20962 struct glyph *g = r->glyphs[TEXT_AREA];
20963 struct glyph *e = g + r->used[TEXT_AREA];
20964 int gx;
20966 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20967 if (EQ (g->object, object))
20969 if (g->charpos == pos)
20971 best_glyph = g;
20972 best_x = gx;
20973 best_row = r;
20974 goto found;
20976 else if (best_glyph == NULL
20977 || ((abs (g->charpos - pos)
20978 < abs (best_glyph->charpos - pos))
20979 && (right_p
20980 ? g->charpos < pos
20981 : g->charpos > pos)))
20983 best_glyph = g;
20984 best_x = gx;
20985 best_row = r;
20990 found:
20992 if (best_glyph)
20994 *x = best_x;
20995 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20997 if (right_p)
20999 *x += best_glyph->pixel_width;
21000 ++*hpos;
21003 *y = best_row->y;
21004 *vpos = best_row - w->current_matrix->rows;
21007 return best_glyph != NULL;
21011 /* See if position X, Y is within a hot-spot of an image. */
21013 static int
21014 on_hot_spot_p (hot_spot, x, y)
21015 Lisp_Object hot_spot;
21016 int x, y;
21018 if (!CONSP (hot_spot))
21019 return 0;
21021 if (EQ (XCAR (hot_spot), Qrect))
21023 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21024 Lisp_Object rect = XCDR (hot_spot);
21025 Lisp_Object tem;
21026 if (!CONSP (rect))
21027 return 0;
21028 if (!CONSP (XCAR (rect)))
21029 return 0;
21030 if (!CONSP (XCDR (rect)))
21031 return 0;
21032 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21033 return 0;
21034 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21035 return 0;
21036 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21037 return 0;
21038 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21039 return 0;
21040 return 1;
21042 else if (EQ (XCAR (hot_spot), Qcircle))
21044 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21045 Lisp_Object circ = XCDR (hot_spot);
21046 Lisp_Object lr, lx0, ly0;
21047 if (CONSP (circ)
21048 && CONSP (XCAR (circ))
21049 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21050 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21051 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21053 double r = XFLOATINT (lr);
21054 double dx = XINT (lx0) - x;
21055 double dy = XINT (ly0) - y;
21056 return (dx * dx + dy * dy <= r * r);
21059 else if (EQ (XCAR (hot_spot), Qpoly))
21061 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21062 if (VECTORP (XCDR (hot_spot)))
21064 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21065 Lisp_Object *poly = v->contents;
21066 int n = v->size;
21067 int i;
21068 int inside = 0;
21069 Lisp_Object lx, ly;
21070 int x0, y0;
21072 /* Need an even number of coordinates, and at least 3 edges. */
21073 if (n < 6 || n & 1)
21074 return 0;
21076 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21077 If count is odd, we are inside polygon. Pixels on edges
21078 may or may not be included depending on actual geometry of the
21079 polygon. */
21080 if ((lx = poly[n-2], !INTEGERP (lx))
21081 || (ly = poly[n-1], !INTEGERP (lx)))
21082 return 0;
21083 x0 = XINT (lx), y0 = XINT (ly);
21084 for (i = 0; i < n; i += 2)
21086 int x1 = x0, y1 = y0;
21087 if ((lx = poly[i], !INTEGERP (lx))
21088 || (ly = poly[i+1], !INTEGERP (ly)))
21089 return 0;
21090 x0 = XINT (lx), y0 = XINT (ly);
21092 /* Does this segment cross the X line? */
21093 if (x0 >= x)
21095 if (x1 >= x)
21096 continue;
21098 else if (x1 < x)
21099 continue;
21100 if (y > y0 && y > y1)
21101 continue;
21102 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21103 inside = !inside;
21105 return inside;
21108 /* If we don't understand the format, pretend we're not in the hot-spot. */
21109 return 0;
21112 Lisp_Object
21113 find_hot_spot (map, x, y)
21114 Lisp_Object map;
21115 int x, y;
21117 while (CONSP (map))
21119 if (CONSP (XCAR (map))
21120 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21121 return XCAR (map);
21122 map = XCDR (map);
21125 return Qnil;
21128 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21129 3, 3, 0,
21130 doc: /* Lookup in image map MAP coordinates X and Y.
21131 An image map is an alist where each element has the format (AREA ID PLIST).
21132 An AREA is specified as either a rectangle, a circle, or a polygon:
21133 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21134 pixel coordinates of the upper left and bottom right corners.
21135 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21136 and the radius of the circle; r may be a float or integer.
21137 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21138 vector describes one corner in the polygon.
21139 Returns the alist element for the first matching AREA in MAP. */)
21140 (map, x, y)
21141 Lisp_Object map;
21142 Lisp_Object x, y;
21144 if (NILP (map))
21145 return Qnil;
21147 CHECK_NUMBER (x);
21148 CHECK_NUMBER (y);
21150 return find_hot_spot (map, XINT (x), XINT (y));
21154 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21155 static void
21156 define_frame_cursor1 (f, cursor, pointer)
21157 struct frame *f;
21158 Cursor cursor;
21159 Lisp_Object pointer;
21161 /* Do not change cursor shape while dragging mouse. */
21162 if (!NILP (do_mouse_tracking))
21163 return;
21165 if (!NILP (pointer))
21167 if (EQ (pointer, Qarrow))
21168 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21169 else if (EQ (pointer, Qhand))
21170 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21171 else if (EQ (pointer, Qtext))
21172 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21173 else if (EQ (pointer, intern ("hdrag")))
21174 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21175 #ifdef HAVE_X_WINDOWS
21176 else if (EQ (pointer, intern ("vdrag")))
21177 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21178 #endif
21179 else if (EQ (pointer, intern ("hourglass")))
21180 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21181 else if (EQ (pointer, Qmodeline))
21182 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21183 else
21184 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21187 if (cursor != No_Cursor)
21188 rif->define_frame_cursor (f, cursor);
21191 /* Take proper action when mouse has moved to the mode or header line
21192 or marginal area AREA of window W, x-position X and y-position Y.
21193 X is relative to the start of the text display area of W, so the
21194 width of bitmap areas and scroll bars must be subtracted to get a
21195 position relative to the start of the mode line. */
21197 static void
21198 note_mode_line_or_margin_highlight (w, x, y, area)
21199 struct window *w;
21200 int x, y;
21201 enum window_part area;
21203 struct frame *f = XFRAME (w->frame);
21204 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21205 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21206 Lisp_Object pointer = Qnil;
21207 int charpos, dx, dy, width, height;
21208 Lisp_Object string, object = Qnil;
21209 Lisp_Object pos, help;
21211 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21212 string = mode_line_string (w, area, &x, &y, &charpos,
21213 &object, &dx, &dy, &width, &height);
21214 else
21216 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21217 string = marginal_area_string (w, area, &x, &y, &charpos,
21218 &object, &dx, &dy, &width, &height);
21221 help = Qnil;
21223 if (IMAGEP (object))
21225 Lisp_Object image_map, hotspot;
21226 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21227 !NILP (image_map))
21228 && (hotspot = find_hot_spot (image_map, dx, dy),
21229 CONSP (hotspot))
21230 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21232 Lisp_Object area_id, plist;
21234 area_id = XCAR (hotspot);
21235 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21236 If so, we could look for mouse-enter, mouse-leave
21237 properties in PLIST (and do something...). */
21238 hotspot = XCDR (hotspot);
21239 if (CONSP (hotspot)
21240 && (plist = XCAR (hotspot), CONSP (plist)))
21242 pointer = Fsafe_plist_get (plist, Qpointer);
21243 if (NILP (pointer))
21244 pointer = Qhand;
21245 help = Fsafe_plist_get (plist, Qhelp_echo);
21246 if (!NILP (help))
21248 help_echo_string = help;
21249 /* Is this correct? ++kfs */
21250 XSETWINDOW (help_echo_window, w);
21251 help_echo_object = w->buffer;
21252 help_echo_pos = charpos;
21256 if (NILP (pointer))
21257 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21260 if (STRINGP (string))
21262 pos = make_number (charpos);
21263 /* If we're on a string with `help-echo' text property, arrange
21264 for the help to be displayed. This is done by setting the
21265 global variable help_echo_string to the help string. */
21266 if (NILP (help))
21268 help = Fget_text_property (pos, Qhelp_echo, string);
21269 if (!NILP (help))
21271 help_echo_string = help;
21272 XSETWINDOW (help_echo_window, w);
21273 help_echo_object = string;
21274 help_echo_pos = charpos;
21278 if (NILP (pointer))
21279 pointer = Fget_text_property (pos, Qpointer, string);
21281 /* Change the mouse pointer according to what is under X/Y. */
21282 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21284 Lisp_Object map;
21285 map = Fget_text_property (pos, Qlocal_map, string);
21286 if (!KEYMAPP (map))
21287 map = Fget_text_property (pos, Qkeymap, string);
21288 if (!KEYMAPP (map))
21289 cursor = dpyinfo->vertical_scroll_bar_cursor;
21293 define_frame_cursor1 (f, cursor, pointer);
21297 /* EXPORT:
21298 Take proper action when the mouse has moved to position X, Y on
21299 frame F as regards highlighting characters that have mouse-face
21300 properties. Also de-highlighting chars where the mouse was before.
21301 X and Y can be negative or out of range. */
21303 void
21304 note_mouse_highlight (f, x, y)
21305 struct frame *f;
21306 int x, y;
21308 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21309 enum window_part part;
21310 Lisp_Object window;
21311 struct window *w;
21312 Cursor cursor = No_Cursor;
21313 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21314 struct buffer *b;
21316 /* When a menu is active, don't highlight because this looks odd. */
21317 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21318 if (popup_activated ())
21319 return;
21320 #endif
21322 if (NILP (Vmouse_highlight)
21323 || !f->glyphs_initialized_p)
21324 return;
21326 dpyinfo->mouse_face_mouse_x = x;
21327 dpyinfo->mouse_face_mouse_y = y;
21328 dpyinfo->mouse_face_mouse_frame = f;
21330 if (dpyinfo->mouse_face_defer)
21331 return;
21333 if (gc_in_progress)
21335 dpyinfo->mouse_face_deferred_gc = 1;
21336 return;
21339 /* Which window is that in? */
21340 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21342 /* If we were displaying active text in another window, clear that.
21343 Also clear if we move out of text area in same window. */
21344 if (! EQ (window, dpyinfo->mouse_face_window)
21345 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21346 clear_mouse_face (dpyinfo);
21348 /* Not on a window -> return. */
21349 if (!WINDOWP (window))
21350 return;
21352 /* Reset help_echo_string. It will get recomputed below. */
21353 help_echo_string = Qnil;
21355 /* Convert to window-relative pixel coordinates. */
21356 w = XWINDOW (window);
21357 frame_to_window_pixel_xy (w, &x, &y);
21359 /* Handle tool-bar window differently since it doesn't display a
21360 buffer. */
21361 if (EQ (window, f->tool_bar_window))
21363 note_tool_bar_highlight (f, x, y);
21364 return;
21367 /* Mouse is on the mode, header line or margin? */
21368 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21369 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21371 note_mode_line_or_margin_highlight (w, x, y, part);
21372 return;
21375 if (part == ON_VERTICAL_BORDER)
21376 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21377 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21378 || part == ON_SCROLL_BAR)
21379 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21380 else
21381 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21383 /* Are we in a window whose display is up to date?
21384 And verify the buffer's text has not changed. */
21385 b = XBUFFER (w->buffer);
21386 if (part == ON_TEXT
21387 && EQ (w->window_end_valid, w->buffer)
21388 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21389 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21391 int hpos, vpos, pos, i, dx, dy, area;
21392 struct glyph *glyph;
21393 Lisp_Object object;
21394 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21395 Lisp_Object *overlay_vec = NULL;
21396 int noverlays;
21397 struct buffer *obuf;
21398 int obegv, ozv, same_region;
21400 /* Find the glyph under X/Y. */
21401 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21403 /* Look for :pointer property on image. */
21404 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21406 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21407 if (img != NULL && IMAGEP (img->spec))
21409 Lisp_Object image_map, hotspot;
21410 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21411 !NILP (image_map))
21412 && (hotspot = find_hot_spot (image_map,
21413 glyph->slice.x + dx,
21414 glyph->slice.y + dy),
21415 CONSP (hotspot))
21416 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21418 Lisp_Object area_id, plist;
21420 area_id = XCAR (hotspot);
21421 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21422 If so, we could look for mouse-enter, mouse-leave
21423 properties in PLIST (and do something...). */
21424 hotspot = XCDR (hotspot);
21425 if (CONSP (hotspot)
21426 && (plist = XCAR (hotspot), CONSP (plist)))
21428 pointer = Fsafe_plist_get (plist, Qpointer);
21429 if (NILP (pointer))
21430 pointer = Qhand;
21431 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21432 if (!NILP (help_echo_string))
21434 help_echo_window = window;
21435 help_echo_object = glyph->object;
21436 help_echo_pos = glyph->charpos;
21440 if (NILP (pointer))
21441 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21445 /* Clear mouse face if X/Y not over text. */
21446 if (glyph == NULL
21447 || area != TEXT_AREA
21448 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21450 if (clear_mouse_face (dpyinfo))
21451 cursor = No_Cursor;
21452 if (NILP (pointer))
21454 if (area != TEXT_AREA)
21455 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21456 else
21457 pointer = Vvoid_text_area_pointer;
21459 goto set_cursor;
21462 pos = glyph->charpos;
21463 object = glyph->object;
21464 if (!STRINGP (object) && !BUFFERP (object))
21465 goto set_cursor;
21467 /* If we get an out-of-range value, return now; avoid an error. */
21468 if (BUFFERP (object) && pos > BUF_Z (b))
21469 goto set_cursor;
21471 /* Make the window's buffer temporarily current for
21472 overlays_at and compute_char_face. */
21473 obuf = current_buffer;
21474 current_buffer = b;
21475 obegv = BEGV;
21476 ozv = ZV;
21477 BEGV = BEG;
21478 ZV = Z;
21480 /* Is this char mouse-active or does it have help-echo? */
21481 position = make_number (pos);
21483 if (BUFFERP (object))
21485 /* Put all the overlays we want in a vector in overlay_vec. */
21486 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21487 /* Sort overlays into increasing priority order. */
21488 noverlays = sort_overlays (overlay_vec, noverlays, w);
21490 else
21491 noverlays = 0;
21493 same_region = (EQ (window, dpyinfo->mouse_face_window)
21494 && vpos >= dpyinfo->mouse_face_beg_row
21495 && vpos <= dpyinfo->mouse_face_end_row
21496 && (vpos > dpyinfo->mouse_face_beg_row
21497 || hpos >= dpyinfo->mouse_face_beg_col)
21498 && (vpos < dpyinfo->mouse_face_end_row
21499 || hpos < dpyinfo->mouse_face_end_col
21500 || dpyinfo->mouse_face_past_end));
21502 if (same_region)
21503 cursor = No_Cursor;
21505 /* Check mouse-face highlighting. */
21506 if (! same_region
21507 /* If there exists an overlay with mouse-face overlapping
21508 the one we are currently highlighting, we have to
21509 check if we enter the overlapping overlay, and then
21510 highlight only that. */
21511 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21512 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21514 /* Find the highest priority overlay that has a mouse-face
21515 property. */
21516 overlay = Qnil;
21517 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21519 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21520 if (!NILP (mouse_face))
21521 overlay = overlay_vec[i];
21524 /* If we're actually highlighting the same overlay as
21525 before, there's no need to do that again. */
21526 if (!NILP (overlay)
21527 && EQ (overlay, dpyinfo->mouse_face_overlay))
21528 goto check_help_echo;
21530 dpyinfo->mouse_face_overlay = overlay;
21532 /* Clear the display of the old active region, if any. */
21533 if (clear_mouse_face (dpyinfo))
21534 cursor = No_Cursor;
21536 /* If no overlay applies, get a text property. */
21537 if (NILP (overlay))
21538 mouse_face = Fget_text_property (position, Qmouse_face, object);
21540 /* Handle the overlay case. */
21541 if (!NILP (overlay))
21543 /* Find the range of text around this char that
21544 should be active. */
21545 Lisp_Object before, after;
21546 int ignore;
21548 before = Foverlay_start (overlay);
21549 after = Foverlay_end (overlay);
21550 /* Record this as the current active region. */
21551 fast_find_position (w, XFASTINT (before),
21552 &dpyinfo->mouse_face_beg_col,
21553 &dpyinfo->mouse_face_beg_row,
21554 &dpyinfo->mouse_face_beg_x,
21555 &dpyinfo->mouse_face_beg_y, Qnil);
21557 dpyinfo->mouse_face_past_end
21558 = !fast_find_position (w, XFASTINT (after),
21559 &dpyinfo->mouse_face_end_col,
21560 &dpyinfo->mouse_face_end_row,
21561 &dpyinfo->mouse_face_end_x,
21562 &dpyinfo->mouse_face_end_y, Qnil);
21563 dpyinfo->mouse_face_window = window;
21565 dpyinfo->mouse_face_face_id
21566 = face_at_buffer_position (w, pos, 0, 0,
21567 &ignore, pos + 1,
21568 !dpyinfo->mouse_face_hidden);
21570 /* Display it as active. */
21571 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21572 cursor = No_Cursor;
21574 /* Handle the text property case. */
21575 else if (!NILP (mouse_face) && BUFFERP (object))
21577 /* Find the range of text around this char that
21578 should be active. */
21579 Lisp_Object before, after, beginning, end;
21580 int ignore;
21582 beginning = Fmarker_position (w->start);
21583 end = make_number (BUF_Z (XBUFFER (object))
21584 - XFASTINT (w->window_end_pos));
21585 before
21586 = Fprevious_single_property_change (make_number (pos + 1),
21587 Qmouse_face,
21588 object, beginning);
21589 after
21590 = Fnext_single_property_change (position, Qmouse_face,
21591 object, end);
21593 /* Record this as the current active region. */
21594 fast_find_position (w, XFASTINT (before),
21595 &dpyinfo->mouse_face_beg_col,
21596 &dpyinfo->mouse_face_beg_row,
21597 &dpyinfo->mouse_face_beg_x,
21598 &dpyinfo->mouse_face_beg_y, Qnil);
21599 dpyinfo->mouse_face_past_end
21600 = !fast_find_position (w, XFASTINT (after),
21601 &dpyinfo->mouse_face_end_col,
21602 &dpyinfo->mouse_face_end_row,
21603 &dpyinfo->mouse_face_end_x,
21604 &dpyinfo->mouse_face_end_y, Qnil);
21605 dpyinfo->mouse_face_window = window;
21607 if (BUFFERP (object))
21608 dpyinfo->mouse_face_face_id
21609 = face_at_buffer_position (w, pos, 0, 0,
21610 &ignore, pos + 1,
21611 !dpyinfo->mouse_face_hidden);
21613 /* Display it as active. */
21614 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21615 cursor = No_Cursor;
21617 else if (!NILP (mouse_face) && STRINGP (object))
21619 Lisp_Object b, e;
21620 int ignore;
21622 b = Fprevious_single_property_change (make_number (pos + 1),
21623 Qmouse_face,
21624 object, Qnil);
21625 e = Fnext_single_property_change (position, Qmouse_face,
21626 object, Qnil);
21627 if (NILP (b))
21628 b = make_number (0);
21629 if (NILP (e))
21630 e = make_number (SCHARS (object) - 1);
21631 fast_find_string_pos (w, XINT (b), object,
21632 &dpyinfo->mouse_face_beg_col,
21633 &dpyinfo->mouse_face_beg_row,
21634 &dpyinfo->mouse_face_beg_x,
21635 &dpyinfo->mouse_face_beg_y, 0);
21636 fast_find_string_pos (w, XINT (e), object,
21637 &dpyinfo->mouse_face_end_col,
21638 &dpyinfo->mouse_face_end_row,
21639 &dpyinfo->mouse_face_end_x,
21640 &dpyinfo->mouse_face_end_y, 1);
21641 dpyinfo->mouse_face_past_end = 0;
21642 dpyinfo->mouse_face_window = window;
21643 dpyinfo->mouse_face_face_id
21644 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21645 glyph->face_id, 1);
21646 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21647 cursor = No_Cursor;
21649 else if (STRINGP (object) && NILP (mouse_face))
21651 /* A string which doesn't have mouse-face, but
21652 the text ``under'' it might have. */
21653 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21654 int start = MATRIX_ROW_START_CHARPOS (r);
21656 pos = string_buffer_position (w, object, start);
21657 if (pos > 0)
21658 mouse_face = get_char_property_and_overlay (make_number (pos),
21659 Qmouse_face,
21660 w->buffer,
21661 &overlay);
21662 if (!NILP (mouse_face) && !NILP (overlay))
21664 Lisp_Object before = Foverlay_start (overlay);
21665 Lisp_Object after = Foverlay_end (overlay);
21666 int ignore;
21668 /* Note that we might not be able to find position
21669 BEFORE in the glyph matrix if the overlay is
21670 entirely covered by a `display' property. In
21671 this case, we overshoot. So let's stop in
21672 the glyph matrix before glyphs for OBJECT. */
21673 fast_find_position (w, XFASTINT (before),
21674 &dpyinfo->mouse_face_beg_col,
21675 &dpyinfo->mouse_face_beg_row,
21676 &dpyinfo->mouse_face_beg_x,
21677 &dpyinfo->mouse_face_beg_y,
21678 object);
21680 dpyinfo->mouse_face_past_end
21681 = !fast_find_position (w, XFASTINT (after),
21682 &dpyinfo->mouse_face_end_col,
21683 &dpyinfo->mouse_face_end_row,
21684 &dpyinfo->mouse_face_end_x,
21685 &dpyinfo->mouse_face_end_y,
21686 Qnil);
21687 dpyinfo->mouse_face_window = window;
21688 dpyinfo->mouse_face_face_id
21689 = face_at_buffer_position (w, pos, 0, 0,
21690 &ignore, pos + 1,
21691 !dpyinfo->mouse_face_hidden);
21693 /* Display it as active. */
21694 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21695 cursor = No_Cursor;
21700 check_help_echo:
21702 /* Look for a `help-echo' property. */
21703 if (NILP (help_echo_string)) {
21704 Lisp_Object help, overlay;
21706 /* Check overlays first. */
21707 help = overlay = Qnil;
21708 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21710 overlay = overlay_vec[i];
21711 help = Foverlay_get (overlay, Qhelp_echo);
21714 if (!NILP (help))
21716 help_echo_string = help;
21717 help_echo_window = window;
21718 help_echo_object = overlay;
21719 help_echo_pos = pos;
21721 else
21723 Lisp_Object object = glyph->object;
21724 int charpos = glyph->charpos;
21726 /* Try text properties. */
21727 if (STRINGP (object)
21728 && charpos >= 0
21729 && charpos < SCHARS (object))
21731 help = Fget_text_property (make_number (charpos),
21732 Qhelp_echo, object);
21733 if (NILP (help))
21735 /* If the string itself doesn't specify a help-echo,
21736 see if the buffer text ``under'' it does. */
21737 struct glyph_row *r
21738 = MATRIX_ROW (w->current_matrix, vpos);
21739 int start = MATRIX_ROW_START_CHARPOS (r);
21740 int pos = string_buffer_position (w, object, start);
21741 if (pos > 0)
21743 help = Fget_char_property (make_number (pos),
21744 Qhelp_echo, w->buffer);
21745 if (!NILP (help))
21747 charpos = pos;
21748 object = w->buffer;
21753 else if (BUFFERP (object)
21754 && charpos >= BEGV
21755 && charpos < ZV)
21756 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21757 object);
21759 if (!NILP (help))
21761 help_echo_string = help;
21762 help_echo_window = window;
21763 help_echo_object = object;
21764 help_echo_pos = charpos;
21769 /* Look for a `pointer' property. */
21770 if (NILP (pointer))
21772 /* Check overlays first. */
21773 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21774 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21776 if (NILP (pointer))
21778 Lisp_Object object = glyph->object;
21779 int charpos = glyph->charpos;
21781 /* Try text properties. */
21782 if (STRINGP (object)
21783 && charpos >= 0
21784 && charpos < SCHARS (object))
21786 pointer = Fget_text_property (make_number (charpos),
21787 Qpointer, object);
21788 if (NILP (pointer))
21790 /* If the string itself doesn't specify a pointer,
21791 see if the buffer text ``under'' it does. */
21792 struct glyph_row *r
21793 = MATRIX_ROW (w->current_matrix, vpos);
21794 int start = MATRIX_ROW_START_CHARPOS (r);
21795 int pos = string_buffer_position (w, object, start);
21796 if (pos > 0)
21797 pointer = Fget_char_property (make_number (pos),
21798 Qpointer, w->buffer);
21801 else if (BUFFERP (object)
21802 && charpos >= BEGV
21803 && charpos < ZV)
21804 pointer = Fget_text_property (make_number (charpos),
21805 Qpointer, object);
21809 BEGV = obegv;
21810 ZV = ozv;
21811 current_buffer = obuf;
21814 set_cursor:
21816 define_frame_cursor1 (f, cursor, pointer);
21820 /* EXPORT for RIF:
21821 Clear any mouse-face on window W. This function is part of the
21822 redisplay interface, and is called from try_window_id and similar
21823 functions to ensure the mouse-highlight is off. */
21825 void
21826 x_clear_window_mouse_face (w)
21827 struct window *w;
21829 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21830 Lisp_Object window;
21832 BLOCK_INPUT;
21833 XSETWINDOW (window, w);
21834 if (EQ (window, dpyinfo->mouse_face_window))
21835 clear_mouse_face (dpyinfo);
21836 UNBLOCK_INPUT;
21840 /* EXPORT:
21841 Just discard the mouse face information for frame F, if any.
21842 This is used when the size of F is changed. */
21844 void
21845 cancel_mouse_face (f)
21846 struct frame *f;
21848 Lisp_Object window;
21849 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21851 window = dpyinfo->mouse_face_window;
21852 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21854 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21855 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21856 dpyinfo->mouse_face_window = Qnil;
21861 #endif /* HAVE_WINDOW_SYSTEM */
21864 /***********************************************************************
21865 Exposure Events
21866 ***********************************************************************/
21868 #ifdef HAVE_WINDOW_SYSTEM
21870 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21871 which intersects rectangle R. R is in window-relative coordinates. */
21873 static void
21874 expose_area (w, row, r, area)
21875 struct window *w;
21876 struct glyph_row *row;
21877 XRectangle *r;
21878 enum glyph_row_area area;
21880 struct glyph *first = row->glyphs[area];
21881 struct glyph *end = row->glyphs[area] + row->used[area];
21882 struct glyph *last;
21883 int first_x, start_x, x;
21885 if (area == TEXT_AREA && row->fill_line_p)
21886 /* If row extends face to end of line write the whole line. */
21887 draw_glyphs (w, 0, row, area,
21888 0, row->used[area],
21889 DRAW_NORMAL_TEXT, 0);
21890 else
21892 /* Set START_X to the window-relative start position for drawing glyphs of
21893 AREA. The first glyph of the text area can be partially visible.
21894 The first glyphs of other areas cannot. */
21895 start_x = window_box_left_offset (w, area);
21896 x = start_x;
21897 if (area == TEXT_AREA)
21898 x += row->x;
21900 /* Find the first glyph that must be redrawn. */
21901 while (first < end
21902 && x + first->pixel_width < r->x)
21904 x += first->pixel_width;
21905 ++first;
21908 /* Find the last one. */
21909 last = first;
21910 first_x = x;
21911 while (last < end
21912 && x < r->x + r->width)
21914 x += last->pixel_width;
21915 ++last;
21918 /* Repaint. */
21919 if (last > first)
21920 draw_glyphs (w, first_x - start_x, row, area,
21921 first - row->glyphs[area], last - row->glyphs[area],
21922 DRAW_NORMAL_TEXT, 0);
21927 /* Redraw the parts of the glyph row ROW on window W intersecting
21928 rectangle R. R is in window-relative coordinates. Value is
21929 non-zero if mouse-face was overwritten. */
21931 static int
21932 expose_line (w, row, r)
21933 struct window *w;
21934 struct glyph_row *row;
21935 XRectangle *r;
21937 xassert (row->enabled_p);
21939 if (row->mode_line_p || w->pseudo_window_p)
21940 draw_glyphs (w, 0, row, TEXT_AREA,
21941 0, row->used[TEXT_AREA],
21942 DRAW_NORMAL_TEXT, 0);
21943 else
21945 if (row->used[LEFT_MARGIN_AREA])
21946 expose_area (w, row, r, LEFT_MARGIN_AREA);
21947 if (row->used[TEXT_AREA])
21948 expose_area (w, row, r, TEXT_AREA);
21949 if (row->used[RIGHT_MARGIN_AREA])
21950 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21951 draw_row_fringe_bitmaps (w, row);
21954 return row->mouse_face_p;
21958 /* Redraw those parts of glyphs rows during expose event handling that
21959 overlap other rows. Redrawing of an exposed line writes over parts
21960 of lines overlapping that exposed line; this function fixes that.
21962 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21963 row in W's current matrix that is exposed and overlaps other rows.
21964 LAST_OVERLAPPING_ROW is the last such row. */
21966 static void
21967 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21968 struct window *w;
21969 struct glyph_row *first_overlapping_row;
21970 struct glyph_row *last_overlapping_row;
21972 struct glyph_row *row;
21974 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21975 if (row->overlapping_p)
21977 xassert (row->enabled_p && !row->mode_line_p);
21979 if (row->used[LEFT_MARGIN_AREA])
21980 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21982 if (row->used[TEXT_AREA])
21983 x_fix_overlapping_area (w, row, TEXT_AREA);
21985 if (row->used[RIGHT_MARGIN_AREA])
21986 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21991 /* Return non-zero if W's cursor intersects rectangle R. */
21993 static int
21994 phys_cursor_in_rect_p (w, r)
21995 struct window *w;
21996 XRectangle *r;
21998 XRectangle cr, result;
21999 struct glyph *cursor_glyph;
22001 cursor_glyph = get_phys_cursor_glyph (w);
22002 if (cursor_glyph)
22004 /* r is relative to W's box, but w->phys_cursor.x is relative
22005 to left edge of W's TEXT area. Adjust it. */
22006 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22007 cr.y = w->phys_cursor.y;
22008 cr.width = cursor_glyph->pixel_width;
22009 cr.height = w->phys_cursor_height;
22010 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22011 I assume the effect is the same -- and this is portable. */
22012 return x_intersect_rectangles (&cr, r, &result);
22014 else
22015 return 0;
22019 /* EXPORT:
22020 Draw a vertical window border to the right of window W if W doesn't
22021 have vertical scroll bars. */
22023 void
22024 x_draw_vertical_border (w)
22025 struct window *w;
22027 /* We could do better, if we knew what type of scroll-bar the adjacent
22028 windows (on either side) have... But we don't :-(
22029 However, I think this works ok. ++KFS 2003-04-25 */
22031 /* Redraw borders between horizontally adjacent windows. Don't
22032 do it for frames with vertical scroll bars because either the
22033 right scroll bar of a window, or the left scroll bar of its
22034 neighbor will suffice as a border. */
22035 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22036 return;
22038 if (!WINDOW_RIGHTMOST_P (w)
22039 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22041 int x0, x1, y0, y1;
22043 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22044 y1 -= 1;
22046 rif->draw_vertical_window_border (w, x1, y0, y1);
22048 else if (!WINDOW_LEFTMOST_P (w)
22049 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22051 int x0, x1, y0, y1;
22053 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22054 y1 -= 1;
22056 rif->draw_vertical_window_border (w, x0, y0, y1);
22061 /* Redraw the part of window W intersection rectangle FR. Pixel
22062 coordinates in FR are frame-relative. Call this function with
22063 input blocked. Value is non-zero if the exposure overwrites
22064 mouse-face. */
22066 static int
22067 expose_window (w, fr)
22068 struct window *w;
22069 XRectangle *fr;
22071 struct frame *f = XFRAME (w->frame);
22072 XRectangle wr, r;
22073 int mouse_face_overwritten_p = 0;
22075 /* If window is not yet fully initialized, do nothing. This can
22076 happen when toolkit scroll bars are used and a window is split.
22077 Reconfiguring the scroll bar will generate an expose for a newly
22078 created window. */
22079 if (w->current_matrix == NULL)
22080 return 0;
22082 /* When we're currently updating the window, display and current
22083 matrix usually don't agree. Arrange for a thorough display
22084 later. */
22085 if (w == updated_window)
22087 SET_FRAME_GARBAGED (f);
22088 return 0;
22091 /* Frame-relative pixel rectangle of W. */
22092 wr.x = WINDOW_LEFT_EDGE_X (w);
22093 wr.y = WINDOW_TOP_EDGE_Y (w);
22094 wr.width = WINDOW_TOTAL_WIDTH (w);
22095 wr.height = WINDOW_TOTAL_HEIGHT (w);
22097 if (x_intersect_rectangles (fr, &wr, &r))
22099 int yb = window_text_bottom_y (w);
22100 struct glyph_row *row;
22101 int cursor_cleared_p;
22102 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22104 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22105 r.x, r.y, r.width, r.height));
22107 /* Convert to window coordinates. */
22108 r.x -= WINDOW_LEFT_EDGE_X (w);
22109 r.y -= WINDOW_TOP_EDGE_Y (w);
22111 /* Turn off the cursor. */
22112 if (!w->pseudo_window_p
22113 && phys_cursor_in_rect_p (w, &r))
22115 x_clear_cursor (w);
22116 cursor_cleared_p = 1;
22118 else
22119 cursor_cleared_p = 0;
22121 /* Update lines intersecting rectangle R. */
22122 first_overlapping_row = last_overlapping_row = NULL;
22123 for (row = w->current_matrix->rows;
22124 row->enabled_p;
22125 ++row)
22127 int y0 = row->y;
22128 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22130 if ((y0 >= r.y && y0 < r.y + r.height)
22131 || (y1 > r.y && y1 < r.y + r.height)
22132 || (r.y >= y0 && r.y < y1)
22133 || (r.y + r.height > y0 && r.y + r.height < y1))
22135 /* A header line may be overlapping, but there is no need
22136 to fix overlapping areas for them. KFS 2005-02-12 */
22137 if (row->overlapping_p && !row->mode_line_p)
22139 if (first_overlapping_row == NULL)
22140 first_overlapping_row = row;
22141 last_overlapping_row = row;
22144 if (expose_line (w, row, &r))
22145 mouse_face_overwritten_p = 1;
22148 if (y1 >= yb)
22149 break;
22152 /* Display the mode line if there is one. */
22153 if (WINDOW_WANTS_MODELINE_P (w)
22154 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22155 row->enabled_p)
22156 && row->y < r.y + r.height)
22158 if (expose_line (w, row, &r))
22159 mouse_face_overwritten_p = 1;
22162 if (!w->pseudo_window_p)
22164 /* Fix the display of overlapping rows. */
22165 if (first_overlapping_row)
22166 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22168 /* Draw border between windows. */
22169 x_draw_vertical_border (w);
22171 /* Turn the cursor on again. */
22172 if (cursor_cleared_p)
22173 update_window_cursor (w, 1);
22177 return mouse_face_overwritten_p;
22182 /* Redraw (parts) of all windows in the window tree rooted at W that
22183 intersect R. R contains frame pixel coordinates. Value is
22184 non-zero if the exposure overwrites mouse-face. */
22186 static int
22187 expose_window_tree (w, r)
22188 struct window *w;
22189 XRectangle *r;
22191 struct frame *f = XFRAME (w->frame);
22192 int mouse_face_overwritten_p = 0;
22194 while (w && !FRAME_GARBAGED_P (f))
22196 if (!NILP (w->hchild))
22197 mouse_face_overwritten_p
22198 |= expose_window_tree (XWINDOW (w->hchild), r);
22199 else if (!NILP (w->vchild))
22200 mouse_face_overwritten_p
22201 |= expose_window_tree (XWINDOW (w->vchild), r);
22202 else
22203 mouse_face_overwritten_p |= expose_window (w, r);
22205 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22208 return mouse_face_overwritten_p;
22212 /* EXPORT:
22213 Redisplay an exposed area of frame F. X and Y are the upper-left
22214 corner of the exposed rectangle. W and H are width and height of
22215 the exposed area. All are pixel values. W or H zero means redraw
22216 the entire frame. */
22218 void
22219 expose_frame (f, x, y, w, h)
22220 struct frame *f;
22221 int x, y, w, h;
22223 XRectangle r;
22224 int mouse_face_overwritten_p = 0;
22226 TRACE ((stderr, "expose_frame "));
22228 /* No need to redraw if frame will be redrawn soon. */
22229 if (FRAME_GARBAGED_P (f))
22231 TRACE ((stderr, " garbaged\n"));
22232 return;
22235 /* If basic faces haven't been realized yet, there is no point in
22236 trying to redraw anything. This can happen when we get an expose
22237 event while Emacs is starting, e.g. by moving another window. */
22238 if (FRAME_FACE_CACHE (f) == NULL
22239 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22241 TRACE ((stderr, " no faces\n"));
22242 return;
22245 if (w == 0 || h == 0)
22247 r.x = r.y = 0;
22248 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22249 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22251 else
22253 r.x = x;
22254 r.y = y;
22255 r.width = w;
22256 r.height = h;
22259 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22260 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22262 if (WINDOWP (f->tool_bar_window))
22263 mouse_face_overwritten_p
22264 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22266 #ifdef HAVE_X_WINDOWS
22267 #ifndef MSDOS
22268 #ifndef USE_X_TOOLKIT
22269 if (WINDOWP (f->menu_bar_window))
22270 mouse_face_overwritten_p
22271 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22272 #endif /* not USE_X_TOOLKIT */
22273 #endif
22274 #endif
22276 /* Some window managers support a focus-follows-mouse style with
22277 delayed raising of frames. Imagine a partially obscured frame,
22278 and moving the mouse into partially obscured mouse-face on that
22279 frame. The visible part of the mouse-face will be highlighted,
22280 then the WM raises the obscured frame. With at least one WM, KDE
22281 2.1, Emacs is not getting any event for the raising of the frame
22282 (even tried with SubstructureRedirectMask), only Expose events.
22283 These expose events will draw text normally, i.e. not
22284 highlighted. Which means we must redo the highlight here.
22285 Subsume it under ``we love X''. --gerd 2001-08-15 */
22286 /* Included in Windows version because Windows most likely does not
22287 do the right thing if any third party tool offers
22288 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22289 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22291 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22292 if (f == dpyinfo->mouse_face_mouse_frame)
22294 int x = dpyinfo->mouse_face_mouse_x;
22295 int y = dpyinfo->mouse_face_mouse_y;
22296 clear_mouse_face (dpyinfo);
22297 note_mouse_highlight (f, x, y);
22303 /* EXPORT:
22304 Determine the intersection of two rectangles R1 and R2. Return
22305 the intersection in *RESULT. Value is non-zero if RESULT is not
22306 empty. */
22309 x_intersect_rectangles (r1, r2, result)
22310 XRectangle *r1, *r2, *result;
22312 XRectangle *left, *right;
22313 XRectangle *upper, *lower;
22314 int intersection_p = 0;
22316 /* Rearrange so that R1 is the left-most rectangle. */
22317 if (r1->x < r2->x)
22318 left = r1, right = r2;
22319 else
22320 left = r2, right = r1;
22322 /* X0 of the intersection is right.x0, if this is inside R1,
22323 otherwise there is no intersection. */
22324 if (right->x <= left->x + left->width)
22326 result->x = right->x;
22328 /* The right end of the intersection is the minimum of the
22329 the right ends of left and right. */
22330 result->width = (min (left->x + left->width, right->x + right->width)
22331 - result->x);
22333 /* Same game for Y. */
22334 if (r1->y < r2->y)
22335 upper = r1, lower = r2;
22336 else
22337 upper = r2, lower = r1;
22339 /* The upper end of the intersection is lower.y0, if this is inside
22340 of upper. Otherwise, there is no intersection. */
22341 if (lower->y <= upper->y + upper->height)
22343 result->y = lower->y;
22345 /* The lower end of the intersection is the minimum of the lower
22346 ends of upper and lower. */
22347 result->height = (min (lower->y + lower->height,
22348 upper->y + upper->height)
22349 - result->y);
22350 intersection_p = 1;
22354 return intersection_p;
22357 #endif /* HAVE_WINDOW_SYSTEM */
22360 /***********************************************************************
22361 Initialization
22362 ***********************************************************************/
22364 void
22365 syms_of_xdisp ()
22367 Vwith_echo_area_save_vector = Qnil;
22368 staticpro (&Vwith_echo_area_save_vector);
22370 Vmessage_stack = Qnil;
22371 staticpro (&Vmessage_stack);
22373 Qinhibit_redisplay = intern ("inhibit-redisplay");
22374 staticpro (&Qinhibit_redisplay);
22376 message_dolog_marker1 = Fmake_marker ();
22377 staticpro (&message_dolog_marker1);
22378 message_dolog_marker2 = Fmake_marker ();
22379 staticpro (&message_dolog_marker2);
22380 message_dolog_marker3 = Fmake_marker ();
22381 staticpro (&message_dolog_marker3);
22383 #if GLYPH_DEBUG
22384 defsubr (&Sdump_frame_glyph_matrix);
22385 defsubr (&Sdump_glyph_matrix);
22386 defsubr (&Sdump_glyph_row);
22387 defsubr (&Sdump_tool_bar_row);
22388 defsubr (&Strace_redisplay);
22389 defsubr (&Strace_to_stderr);
22390 #endif
22391 #ifdef HAVE_WINDOW_SYSTEM
22392 defsubr (&Stool_bar_lines_needed);
22393 defsubr (&Slookup_image_map);
22394 #endif
22395 defsubr (&Sformat_mode_line);
22397 staticpro (&Qmenu_bar_update_hook);
22398 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22400 staticpro (&Qoverriding_terminal_local_map);
22401 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22403 staticpro (&Qoverriding_local_map);
22404 Qoverriding_local_map = intern ("overriding-local-map");
22406 staticpro (&Qwindow_scroll_functions);
22407 Qwindow_scroll_functions = intern ("window-scroll-functions");
22409 staticpro (&Qredisplay_end_trigger_functions);
22410 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22412 staticpro (&Qinhibit_point_motion_hooks);
22413 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22415 QCdata = intern (":data");
22416 staticpro (&QCdata);
22417 Qdisplay = intern ("display");
22418 staticpro (&Qdisplay);
22419 Qspace_width = intern ("space-width");
22420 staticpro (&Qspace_width);
22421 Qraise = intern ("raise");
22422 staticpro (&Qraise);
22423 Qslice = intern ("slice");
22424 staticpro (&Qslice);
22425 Qspace = intern ("space");
22426 staticpro (&Qspace);
22427 Qmargin = intern ("margin");
22428 staticpro (&Qmargin);
22429 Qpointer = intern ("pointer");
22430 staticpro (&Qpointer);
22431 Qleft_margin = intern ("left-margin");
22432 staticpro (&Qleft_margin);
22433 Qright_margin = intern ("right-margin");
22434 staticpro (&Qright_margin);
22435 Qcenter = intern ("center");
22436 staticpro (&Qcenter);
22437 Qline_height = intern ("line-height");
22438 staticpro (&Qline_height);
22439 QCalign_to = intern (":align-to");
22440 staticpro (&QCalign_to);
22441 QCrelative_width = intern (":relative-width");
22442 staticpro (&QCrelative_width);
22443 QCrelative_height = intern (":relative-height");
22444 staticpro (&QCrelative_height);
22445 QCeval = intern (":eval");
22446 staticpro (&QCeval);
22447 QCpropertize = intern (":propertize");
22448 staticpro (&QCpropertize);
22449 QCfile = intern (":file");
22450 staticpro (&QCfile);
22451 Qfontified = intern ("fontified");
22452 staticpro (&Qfontified);
22453 Qfontification_functions = intern ("fontification-functions");
22454 staticpro (&Qfontification_functions);
22455 Qtrailing_whitespace = intern ("trailing-whitespace");
22456 staticpro (&Qtrailing_whitespace);
22457 Qescape_glyph = intern ("escape-glyph");
22458 staticpro (&Qescape_glyph);
22459 Qimage = intern ("image");
22460 staticpro (&Qimage);
22461 QCmap = intern (":map");
22462 staticpro (&QCmap);
22463 QCpointer = intern (":pointer");
22464 staticpro (&QCpointer);
22465 Qrect = intern ("rect");
22466 staticpro (&Qrect);
22467 Qcircle = intern ("circle");
22468 staticpro (&Qcircle);
22469 Qpoly = intern ("poly");
22470 staticpro (&Qpoly);
22471 Qmessage_truncate_lines = intern ("message-truncate-lines");
22472 staticpro (&Qmessage_truncate_lines);
22473 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22474 staticpro (&Qcursor_in_non_selected_windows);
22475 Qgrow_only = intern ("grow-only");
22476 staticpro (&Qgrow_only);
22477 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22478 staticpro (&Qinhibit_menubar_update);
22479 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22480 staticpro (&Qinhibit_eval_during_redisplay);
22481 Qposition = intern ("position");
22482 staticpro (&Qposition);
22483 Qbuffer_position = intern ("buffer-position");
22484 staticpro (&Qbuffer_position);
22485 Qobject = intern ("object");
22486 staticpro (&Qobject);
22487 Qbar = intern ("bar");
22488 staticpro (&Qbar);
22489 Qhbar = intern ("hbar");
22490 staticpro (&Qhbar);
22491 Qbox = intern ("box");
22492 staticpro (&Qbox);
22493 Qhollow = intern ("hollow");
22494 staticpro (&Qhollow);
22495 Qhand = intern ("hand");
22496 staticpro (&Qhand);
22497 Qarrow = intern ("arrow");
22498 staticpro (&Qarrow);
22499 Qtext = intern ("text");
22500 staticpro (&Qtext);
22501 Qrisky_local_variable = intern ("risky-local-variable");
22502 staticpro (&Qrisky_local_variable);
22503 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22504 staticpro (&Qinhibit_free_realized_faces);
22506 list_of_error = Fcons (Fcons (intern ("error"),
22507 Fcons (intern ("void-variable"), Qnil)),
22508 Qnil);
22509 staticpro (&list_of_error);
22511 Qlast_arrow_position = intern ("last-arrow-position");
22512 staticpro (&Qlast_arrow_position);
22513 Qlast_arrow_string = intern ("last-arrow-string");
22514 staticpro (&Qlast_arrow_string);
22516 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22517 staticpro (&Qoverlay_arrow_string);
22518 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22519 staticpro (&Qoverlay_arrow_bitmap);
22521 echo_buffer[0] = echo_buffer[1] = Qnil;
22522 staticpro (&echo_buffer[0]);
22523 staticpro (&echo_buffer[1]);
22525 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22526 staticpro (&echo_area_buffer[0]);
22527 staticpro (&echo_area_buffer[1]);
22529 Vmessages_buffer_name = build_string ("*Messages*");
22530 staticpro (&Vmessages_buffer_name);
22532 mode_line_proptrans_alist = Qnil;
22533 staticpro (&mode_line_proptrans_alist);
22535 mode_line_string_list = Qnil;
22536 staticpro (&mode_line_string_list);
22538 help_echo_string = Qnil;
22539 staticpro (&help_echo_string);
22540 help_echo_object = Qnil;
22541 staticpro (&help_echo_object);
22542 help_echo_window = Qnil;
22543 staticpro (&help_echo_window);
22544 previous_help_echo_string = Qnil;
22545 staticpro (&previous_help_echo_string);
22546 help_echo_pos = -1;
22548 #ifdef HAVE_WINDOW_SYSTEM
22549 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22550 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22551 For example, if a block cursor is over a tab, it will be drawn as
22552 wide as that tab on the display. */);
22553 x_stretch_cursor_p = 0;
22554 #endif
22556 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22557 doc: /* *Non-nil means highlight trailing whitespace.
22558 The face used for trailing whitespace is `trailing-whitespace'. */);
22559 Vshow_trailing_whitespace = Qnil;
22561 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22562 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22563 Vshow_nonbreak_escape = Qt;
22565 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22566 doc: /* *The pointer shape to show in void text areas.
22567 Nil means to show the text pointer. Other options are `arrow', `text',
22568 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22569 Vvoid_text_area_pointer = Qarrow;
22571 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22572 doc: /* Non-nil means don't actually do any redisplay.
22573 This is used for internal purposes. */);
22574 Vinhibit_redisplay = Qnil;
22576 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22577 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22578 Vglobal_mode_string = Qnil;
22580 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22581 doc: /* Marker for where to display an arrow on top of the buffer text.
22582 This must be the beginning of a line in order to work.
22583 See also `overlay-arrow-string'. */);
22584 Voverlay_arrow_position = Qnil;
22586 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22587 doc: /* String to display as an arrow in non-window frames.
22588 See also `overlay-arrow-position'. */);
22589 Voverlay_arrow_string = Qnil;
22591 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22592 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22593 The symbols on this list are examined during redisplay to determine
22594 where to display overlay arrows. */);
22595 Voverlay_arrow_variable_list
22596 = Fcons (intern ("overlay-arrow-position"), Qnil);
22598 DEFVAR_INT ("scroll-step", &scroll_step,
22599 doc: /* *The number of lines to try scrolling a window by when point moves out.
22600 If that fails to bring point back on frame, point is centered instead.
22601 If this is zero, point is always centered after it moves off frame.
22602 If you want scrolling to always be a line at a time, you should set
22603 `scroll-conservatively' to a large value rather than set this to 1. */);
22605 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22606 doc: /* *Scroll up to this many lines, to bring point back on screen.
22607 A value of zero means to scroll the text to center point vertically
22608 in the window. */);
22609 scroll_conservatively = 0;
22611 DEFVAR_INT ("scroll-margin", &scroll_margin,
22612 doc: /* *Number of lines of margin at the top and bottom of a window.
22613 Recenter the window whenever point gets within this many lines
22614 of the top or bottom of the window. */);
22615 scroll_margin = 0;
22617 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22618 doc: /* Pixels per inch on current display.
22619 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22620 Vdisplay_pixels_per_inch = make_float (72.0);
22622 #if GLYPH_DEBUG
22623 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22624 #endif
22626 DEFVAR_BOOL ("truncate-partial-width-windows",
22627 &truncate_partial_width_windows,
22628 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22629 truncate_partial_width_windows = 1;
22631 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22632 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22633 Any other value means to use the appropriate face, `mode-line',
22634 `header-line', or `menu' respectively. */);
22635 mode_line_inverse_video = 1;
22637 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22638 doc: /* *Maximum buffer size for which line number should be displayed.
22639 If the buffer is bigger than this, the line number does not appear
22640 in the mode line. A value of nil means no limit. */);
22641 Vline_number_display_limit = Qnil;
22643 DEFVAR_INT ("line-number-display-limit-width",
22644 &line_number_display_limit_width,
22645 doc: /* *Maximum line width (in characters) for line number display.
22646 If the average length of the lines near point is bigger than this, then the
22647 line number may be omitted from the mode line. */);
22648 line_number_display_limit_width = 200;
22650 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22651 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22652 highlight_nonselected_windows = 0;
22654 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22655 doc: /* Non-nil if more than one frame is visible on this display.
22656 Minibuffer-only frames don't count, but iconified frames do.
22657 This variable is not guaranteed to be accurate except while processing
22658 `frame-title-format' and `icon-title-format'. */);
22660 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22661 doc: /* Template for displaying the title bar of visible frames.
22662 \(Assuming the window manager supports this feature.)
22663 This variable has the same structure as `mode-line-format' (which see),
22664 and is used only on frames for which no explicit name has been set
22665 \(see `modify-frame-parameters'). */);
22667 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22668 doc: /* Template for displaying the title bar of an iconified frame.
22669 \(Assuming the window manager supports this feature.)
22670 This variable has the same structure as `mode-line-format' (which see),
22671 and is used only on frames for which no explicit name has been set
22672 \(see `modify-frame-parameters'). */);
22673 Vicon_title_format
22674 = Vframe_title_format
22675 = Fcons (intern ("multiple-frames"),
22676 Fcons (build_string ("%b"),
22677 Fcons (Fcons (empty_string,
22678 Fcons (intern ("invocation-name"),
22679 Fcons (build_string ("@"),
22680 Fcons (intern ("system-name"),
22681 Qnil)))),
22682 Qnil)));
22684 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22685 doc: /* Maximum number of lines to keep in the message log buffer.
22686 If nil, disable message logging. If t, log messages but don't truncate
22687 the buffer when it becomes large. */);
22688 Vmessage_log_max = make_number (50);
22690 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22691 doc: /* Functions called before redisplay, if window sizes have changed.
22692 The value should be a list of functions that take one argument.
22693 Just before redisplay, for each frame, if any of its windows have changed
22694 size since the last redisplay, or have been split or deleted,
22695 all the functions in the list are called, with the frame as argument. */);
22696 Vwindow_size_change_functions = Qnil;
22698 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22699 doc: /* List of functions to call before redisplaying a window with scrolling.
22700 Each function is called with two arguments, the window
22701 and its new display-start position. Note that the value of `window-end'
22702 is not valid when these functions are called. */);
22703 Vwindow_scroll_functions = Qnil;
22705 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22706 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22707 mouse_autoselect_window = 0;
22709 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22710 doc: /* *Non-nil means automatically resize tool-bars.
22711 This increases a tool-bar's height if not all tool-bar items are visible.
22712 It decreases a tool-bar's height when it would display blank lines
22713 otherwise. */);
22714 auto_resize_tool_bars_p = 1;
22716 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22717 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22718 auto_raise_tool_bar_buttons_p = 1;
22720 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22721 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22722 make_cursor_line_fully_visible_p = 1;
22724 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22725 doc: /* *Margin around tool-bar buttons in pixels.
22726 If an integer, use that for both horizontal and vertical margins.
22727 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22728 HORZ specifying the horizontal margin, and VERT specifying the
22729 vertical margin. */);
22730 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22732 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22733 doc: /* *Relief thickness of tool-bar buttons. */);
22734 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22736 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22737 doc: /* List of functions to call to fontify regions of text.
22738 Each function is called with one argument POS. Functions must
22739 fontify a region starting at POS in the current buffer, and give
22740 fontified regions the property `fontified'. */);
22741 Vfontification_functions = Qnil;
22742 Fmake_variable_buffer_local (Qfontification_functions);
22744 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22745 &unibyte_display_via_language_environment,
22746 doc: /* *Non-nil means display unibyte text according to language environment.
22747 Specifically this means that unibyte non-ASCII characters
22748 are displayed by converting them to the equivalent multibyte characters
22749 according to the current language environment. As a result, they are
22750 displayed according to the current fontset. */);
22751 unibyte_display_via_language_environment = 0;
22753 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22754 doc: /* *Maximum height for resizing mini-windows.
22755 If a float, it specifies a fraction of the mini-window frame's height.
22756 If an integer, it specifies a number of lines. */);
22757 Vmax_mini_window_height = make_float (0.25);
22759 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22760 doc: /* *How to resize mini-windows.
22761 A value of nil means don't automatically resize mini-windows.
22762 A value of t means resize them to fit the text displayed in them.
22763 A value of `grow-only', the default, means let mini-windows grow
22764 only, until their display becomes empty, at which point the windows
22765 go back to their normal size. */);
22766 Vresize_mini_windows = Qgrow_only;
22768 DEFVAR_LISP ("cursor-in-non-selected-windows",
22769 &Vcursor_in_non_selected_windows,
22770 doc: /* *Cursor type to display in non-selected windows.
22771 t means to use hollow box cursor. See `cursor-type' for other values. */);
22772 Vcursor_in_non_selected_windows = Qt;
22774 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22775 doc: /* Alist specifying how to blink the cursor off.
22776 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22777 `cursor-type' frame-parameter or variable equals ON-STATE,
22778 comparing using `equal', Emacs uses OFF-STATE to specify
22779 how to blink it off. */);
22780 Vblink_cursor_alist = Qnil;
22782 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22783 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22784 automatic_hscrolling_p = 1;
22786 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22787 doc: /* *How many columns away from the window edge point is allowed to get
22788 before automatic hscrolling will horizontally scroll the window. */);
22789 hscroll_margin = 5;
22791 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22792 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22793 When point is less than `automatic-hscroll-margin' columns from the window
22794 edge, automatic hscrolling will scroll the window by the amount of columns
22795 determined by this variable. If its value is a positive integer, scroll that
22796 many columns. If it's a positive floating-point number, it specifies the
22797 fraction of the window's width to scroll. If it's nil or zero, point will be
22798 centered horizontally after the scroll. Any other value, including negative
22799 numbers, are treated as if the value were zero.
22801 Automatic hscrolling always moves point outside the scroll margin, so if
22802 point was more than scroll step columns inside the margin, the window will
22803 scroll more than the value given by the scroll step.
22805 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22806 and `scroll-right' overrides this variable's effect. */);
22807 Vhscroll_step = make_number (0);
22809 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22810 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22811 Bind this around calls to `message' to let it take effect. */);
22812 message_truncate_lines = 0;
22814 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22815 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22816 Can be used to update submenus whose contents should vary. */);
22817 Vmenu_bar_update_hook = Qnil;
22819 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22820 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22821 inhibit_menubar_update = 0;
22823 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22824 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22825 inhibit_eval_during_redisplay = 0;
22827 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22828 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22829 inhibit_free_realized_faces = 0;
22831 #if GLYPH_DEBUG
22832 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22833 doc: /* Inhibit try_window_id display optimization. */);
22834 inhibit_try_window_id = 0;
22836 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22837 doc: /* Inhibit try_window_reusing display optimization. */);
22838 inhibit_try_window_reusing = 0;
22840 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22841 doc: /* Inhibit try_cursor_movement display optimization. */);
22842 inhibit_try_cursor_movement = 0;
22843 #endif /* GLYPH_DEBUG */
22847 /* Initialize this module when Emacs starts. */
22849 void
22850 init_xdisp ()
22852 Lisp_Object root_window;
22853 struct window *mini_w;
22855 current_header_line_height = current_mode_line_height = -1;
22857 CHARPOS (this_line_start_pos) = 0;
22859 mini_w = XWINDOW (minibuf_window);
22860 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22862 if (!noninteractive)
22864 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22865 int i;
22867 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22868 set_window_height (root_window,
22869 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22871 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22872 set_window_height (minibuf_window, 1, 0);
22874 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22875 mini_w->total_cols = make_number (FRAME_COLS (f));
22877 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22878 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22879 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22881 /* The default ellipsis glyphs `...'. */
22882 for (i = 0; i < 3; ++i)
22883 default_invis_vector[i] = make_number ('.');
22887 /* Allocate the buffer for frame titles.
22888 Also used for `format-mode-line'. */
22889 int size = 100;
22890 frame_title_buf = (char *) xmalloc (size);
22891 frame_title_buf_end = frame_title_buf + size;
22892 frame_title_ptr = NULL;
22895 help_echo_showing_p = 0;
22899 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22900 (do not change this comment) */