(string-to-float): Replace `string-to-int' by `string-to-number'.
[emacs.git] / src / xdisp.c
blob56c686aed478a61fa565e5b6df350fc0cb72560c
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 an overlay arrow has been displayed 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 /* Index 0 is the buffer that holds the current (desired) echo area message,
571 or nil if none is desired right now.
573 Index 1 is the buffer that holds the previously displayed echo area message,
574 or nil to indicate no message. This is normally what's on the screen now.
576 These two can point to the same buffer. That happens when the last
577 message output by the user (or made by echoing) has been displayed. */
579 Lisp_Object echo_area_buffer[2];
581 /* Permanent pointers to the two buffers that are used for echo area
582 purposes. Once the two buffers are made, and their pointers are
583 placed here, these two slots remain unchanged unless those buffers
584 need to be created afresh. */
586 static Lisp_Object echo_buffer[2];
588 /* A vector saved used in with_area_buffer to reduce consing. */
590 static Lisp_Object Vwith_echo_area_save_vector;
592 /* Non-zero means display_echo_area should display the last echo area
593 message again. Set by redisplay_preserve_echo_area. */
595 static int display_last_displayed_message_p;
597 /* Nonzero if echo area is being used by print; zero if being used by
598 message. */
600 int message_buf_print;
602 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
604 Lisp_Object Qinhibit_menubar_update;
605 int inhibit_menubar_update;
607 /* Maximum height for resizing mini-windows. Either a float
608 specifying a fraction of the available height, or an integer
609 specifying a number of lines. */
611 Lisp_Object Vmax_mini_window_height;
613 /* Non-zero means messages should be displayed with truncated
614 lines instead of being continued. */
616 int message_truncate_lines;
617 Lisp_Object Qmessage_truncate_lines;
619 /* Set to 1 in clear_message to make redisplay_internal aware
620 of an emptied echo area. */
622 static int message_cleared_p;
624 /* How to blink the default frame cursor off. */
625 Lisp_Object Vblink_cursor_alist;
627 /* A scratch glyph row with contents used for generating truncation
628 glyphs. Also used in direct_output_for_insert. */
630 #define MAX_SCRATCH_GLYPHS 100
631 struct glyph_row scratch_glyph_row;
632 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
634 /* Ascent and height of the last line processed by move_it_to. */
636 static int last_max_ascent, last_height;
638 /* Non-zero if there's a help-echo in the echo area. */
640 int help_echo_showing_p;
642 /* If >= 0, computed, exact values of mode-line and header-line height
643 to use in the macros CURRENT_MODE_LINE_HEIGHT and
644 CURRENT_HEADER_LINE_HEIGHT. */
646 int current_mode_line_height, current_header_line_height;
648 /* The maximum distance to look ahead for text properties. Values
649 that are too small let us call compute_char_face and similar
650 functions too often which is expensive. Values that are too large
651 let us call compute_char_face and alike too often because we
652 might not be interested in text properties that far away. */
654 #define TEXT_PROP_DISTANCE_LIMIT 100
656 #if GLYPH_DEBUG
658 /* Variables to turn off display optimizations from Lisp. */
660 int inhibit_try_window_id, inhibit_try_window_reusing;
661 int inhibit_try_cursor_movement;
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG != 0. */
666 int trace_redisplay_p;
668 #endif /* GLYPH_DEBUG */
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
679 /* Non-zero means automatically scroll windows horizontally to make
680 point visible. */
682 int automatic_hscrolling_p;
684 /* How close to the margin can point get before the window is scrolled
685 horizontally. */
686 EMACS_INT hscroll_margin;
688 /* How much to scroll horizontally when point is inside the above margin. */
689 Lisp_Object Vhscroll_step;
691 /* The variable `resize-mini-windows'. If nil, don't resize
692 mini-windows. If t, always resize them to fit the text they
693 display. If `grow-only', let mini-windows grow only until they
694 become empty. */
696 Lisp_Object Vresize_mini_windows;
698 /* Buffer being redisplayed -- for redisplay_window_error. */
700 struct buffer *displayed_buffer;
702 /* Value returned from text property handlers (see below). */
704 enum prop_handled
706 HANDLED_NORMALLY,
707 HANDLED_RECOMPUTE_PROPS,
708 HANDLED_OVERLAY_STRING_CONSUMED,
709 HANDLED_RETURN
712 /* A description of text properties that redisplay is interested
713 in. */
715 struct props
717 /* The name of the property. */
718 Lisp_Object *name;
720 /* A unique index for the property. */
721 enum prop_idx idx;
723 /* A handler function called to set up iterator IT from the property
724 at IT's current position. Value is used to steer handle_stop. */
725 enum prop_handled (*handler) P_ ((struct it *it));
728 static enum prop_handled handle_face_prop P_ ((struct it *));
729 static enum prop_handled handle_invisible_prop P_ ((struct it *));
730 static enum prop_handled handle_display_prop P_ ((struct it *));
731 static enum prop_handled handle_composition_prop P_ ((struct it *));
732 static enum prop_handled handle_overlay_change P_ ((struct it *));
733 static enum prop_handled handle_fontified_prop P_ ((struct it *));
735 /* Properties handled by iterators. */
737 static struct props it_props[] =
739 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
740 /* Handle `face' before `display' because some sub-properties of
741 `display' need to know the face. */
742 {&Qface, FACE_PROP_IDX, handle_face_prop},
743 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
744 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
745 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
746 {NULL, 0, NULL}
749 /* Value is the position described by X. If X is a marker, value is
750 the marker_position of X. Otherwise, value is X. */
752 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
754 /* Enumeration returned by some move_it_.* functions internally. */
756 enum move_it_result
758 /* Not used. Undefined value. */
759 MOVE_UNDEFINED,
761 /* Move ended at the requested buffer position or ZV. */
762 MOVE_POS_MATCH_OR_ZV,
764 /* Move ended at the requested X pixel position. */
765 MOVE_X_REACHED,
767 /* Move within a line ended at the end of a line that must be
768 continued. */
769 MOVE_LINE_CONTINUED,
771 /* Move within a line ended at the end of a line that would
772 be displayed truncated. */
773 MOVE_LINE_TRUNCATED,
775 /* Move within a line ended at a line end. */
776 MOVE_NEWLINE_OR_CR
779 /* This counter is used to clear the face cache every once in a while
780 in redisplay_internal. It is incremented for each redisplay.
781 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
782 cleared. */
784 #define CLEAR_FACE_CACHE_COUNT 500
785 static int clear_face_cache_count;
787 /* Similarly for the image cache. */
789 #ifdef HAVE_WINDOW_SYSTEM
790 #define CLEAR_IMAGE_CACHE_COUNT 101
791 static int clear_image_cache_count;
792 #endif
794 /* Record the previous terminal frame we displayed. */
796 static struct frame *previous_terminal_frame;
798 /* Non-zero while redisplay_internal is in progress. */
800 int redisplaying_p;
802 /* Non-zero means don't free realized faces. Bound while freeing
803 realized faces is dangerous because glyph matrices might still
804 reference them. */
806 int inhibit_free_realized_faces;
807 Lisp_Object Qinhibit_free_realized_faces;
809 /* If a string, XTread_socket generates an event to display that string.
810 (The display is done in read_char.) */
812 Lisp_Object help_echo_string;
813 Lisp_Object help_echo_window;
814 Lisp_Object help_echo_object;
815 int help_echo_pos;
817 /* Temporary variable for XTread_socket. */
819 Lisp_Object previous_help_echo_string;
821 /* Null glyph slice */
823 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
826 /* Function prototypes. */
828 static void setup_for_ellipsis P_ ((struct it *, int));
829 static void mark_window_display_accurate_1 P_ ((struct window *, int));
830 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
831 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
832 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
833 static int redisplay_mode_lines P_ ((Lisp_Object, int));
834 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
836 #if 0
837 static int invisible_text_between_p P_ ((struct it *, int, int));
838 #endif
840 static void pint2str P_ ((char *, int, int));
841 static void pint2hrstr P_ ((char *, int, int));
842 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
843 struct text_pos));
844 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
845 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
846 static void store_frame_title_char P_ ((char));
847 static int store_frame_title P_ ((const unsigned char *, int, int));
848 static void x_consider_frame_title P_ ((Lisp_Object));
849 static void handle_stop P_ ((struct it *));
850 static int tool_bar_lines_needed P_ ((struct frame *));
851 static int single_display_spec_intangible_p P_ ((Lisp_Object));
852 static void ensure_echo_area_buffers P_ ((void));
853 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
854 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
855 static int with_echo_area_buffer P_ ((struct window *, int,
856 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
857 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
858 static void clear_garbaged_frames P_ ((void));
859 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
860 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
861 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static int display_echo_area P_ ((struct window *));
863 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
866 static int string_char_and_length P_ ((const unsigned char *, int, int *));
867 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
868 struct text_pos));
869 static int compute_window_start_on_continuation_line P_ ((struct window *));
870 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
871 static void insert_left_trunc_glyphs P_ ((struct it *));
872 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
873 Lisp_Object));
874 static void extend_face_to_end_of_line P_ ((struct it *));
875 static int append_space_for_newline P_ ((struct it *, int));
876 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
877 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
878 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
879 static int trailing_whitespace_p P_ ((int));
880 static int message_log_check_duplicate P_ ((int, int, int, int));
881 static void push_it P_ ((struct it *));
882 static void pop_it P_ ((struct it *));
883 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
884 static void select_frame_for_redisplay P_ ((Lisp_Object));
885 static void redisplay_internal P_ ((int));
886 static int echo_area_display P_ ((int));
887 static void redisplay_windows P_ ((Lisp_Object));
888 static void redisplay_window P_ ((Lisp_Object, int));
889 static Lisp_Object redisplay_window_error ();
890 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
891 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
892 static void update_menu_bar P_ ((struct frame *, int));
893 static int try_window_reusing_current_matrix P_ ((struct window *));
894 static int try_window_id P_ ((struct window *));
895 static int display_line P_ ((struct it *));
896 static int display_mode_lines P_ ((struct window *));
897 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
898 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
899 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
900 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
901 static void display_menu_bar P_ ((struct window *));
902 static int display_count_lines P_ ((int, int, int, int, int *));
903 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
904 int, int, struct it *, int, int, int, int));
905 static void compute_line_metrics P_ ((struct it *));
906 static void run_redisplay_end_trigger_hook P_ ((struct it *));
907 static int get_overlay_strings P_ ((struct it *, int));
908 static void next_overlay_string P_ ((struct it *));
909 static void reseat P_ ((struct it *, struct text_pos, int));
910 static void reseat_1 P_ ((struct it *, struct text_pos, int));
911 static void back_to_previous_visible_line_start P_ ((struct it *));
912 void reseat_at_previous_visible_line_start P_ ((struct it *));
913 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
914 static int next_element_from_ellipsis P_ ((struct it *));
915 static int next_element_from_display_vector P_ ((struct it *));
916 static int next_element_from_string P_ ((struct it *));
917 static int next_element_from_c_string P_ ((struct it *));
918 static int next_element_from_buffer P_ ((struct it *));
919 static int next_element_from_composition P_ ((struct it *));
920 static int next_element_from_image P_ ((struct it *));
921 static int next_element_from_stretch P_ ((struct it *));
922 static void load_overlay_strings P_ ((struct it *, int));
923 static int init_from_display_pos P_ ((struct it *, struct window *,
924 struct display_pos *));
925 static void reseat_to_string P_ ((struct it *, unsigned char *,
926 Lisp_Object, int, int, int, int));
927 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
928 int, int, int));
929 void move_it_vertically_backward P_ ((struct it *, int));
930 static void init_to_row_start P_ ((struct it *, struct window *,
931 struct glyph_row *));
932 static int init_to_row_end P_ ((struct it *, struct window *,
933 struct glyph_row *));
934 static void back_to_previous_line_start P_ ((struct it *));
935 static int forward_to_next_line_start P_ ((struct it *, int *));
936 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
937 Lisp_Object, int));
938 static struct text_pos string_pos P_ ((int, Lisp_Object));
939 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
940 static int number_of_chars P_ ((unsigned char *, int));
941 static void compute_stop_pos P_ ((struct it *));
942 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
943 Lisp_Object));
944 static int face_before_or_after_it_pos P_ ((struct it *, int));
945 static int next_overlay_change P_ ((int));
946 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
947 Lisp_Object, struct text_pos *,
948 int));
949 static int underlying_face_id P_ ((struct it *));
950 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
951 struct window *));
953 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
954 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
956 #ifdef HAVE_WINDOW_SYSTEM
958 static void update_tool_bar P_ ((struct frame *, int));
959 static void build_desired_tool_bar_string P_ ((struct frame *f));
960 static int redisplay_tool_bar P_ ((struct frame *));
961 static void display_tool_bar_line P_ ((struct it *));
962 static void notice_overwritten_cursor P_ ((struct window *,
963 enum glyph_row_area,
964 int, int, int, int));
968 #endif /* HAVE_WINDOW_SYSTEM */
971 /***********************************************************************
972 Window display dimensions
973 ***********************************************************************/
975 /* Return the bottom boundary y-position for text lines in window W.
976 This is the first y position at which a line cannot start.
977 It is relative to the top of the window.
979 This is the height of W minus the height of a mode line, if any. */
981 INLINE int
982 window_text_bottom_y (w)
983 struct window *w;
985 int height = WINDOW_TOTAL_HEIGHT (w);
987 if (WINDOW_WANTS_MODELINE_P (w))
988 height -= CURRENT_MODE_LINE_HEIGHT (w);
989 return height;
992 /* Return the pixel width of display area AREA of window W. AREA < 0
993 means return the total width of W, not including fringes to
994 the left and right of the window. */
996 INLINE int
997 window_box_width (w, area)
998 struct window *w;
999 int area;
1001 int cols = XFASTINT (w->total_cols);
1002 int pixels = 0;
1004 if (!w->pseudo_window_p)
1006 cols -= WINDOW_SCROLL_BAR_COLS (w);
1008 if (area == TEXT_AREA)
1010 if (INTEGERP (w->left_margin_cols))
1011 cols -= XFASTINT (w->left_margin_cols);
1012 if (INTEGERP (w->right_margin_cols))
1013 cols -= XFASTINT (w->right_margin_cols);
1014 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1016 else if (area == LEFT_MARGIN_AREA)
1018 cols = (INTEGERP (w->left_margin_cols)
1019 ? XFASTINT (w->left_margin_cols) : 0);
1020 pixels = 0;
1022 else if (area == RIGHT_MARGIN_AREA)
1024 cols = (INTEGERP (w->right_margin_cols)
1025 ? XFASTINT (w->right_margin_cols) : 0);
1026 pixels = 0;
1030 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1034 /* Return the pixel height of the display area of window W, not
1035 including mode lines of W, if any. */
1037 INLINE int
1038 window_box_height (w)
1039 struct window *w;
1041 struct frame *f = XFRAME (w->frame);
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1044 xassert (height >= 0);
1046 /* Note: the code below that determines the mode-line/header-line
1047 height is essentially the same as that contained in the macro
1048 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1049 the appropriate glyph row has its `mode_line_p' flag set,
1050 and if it doesn't, uses estimate_mode_line_height instead. */
1052 if (WINDOW_WANTS_MODELINE_P (w))
1054 struct glyph_row *ml_row
1055 = (w->current_matrix && w->current_matrix->rows
1056 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1057 : 0);
1058 if (ml_row && ml_row->mode_line_p)
1059 height -= ml_row->height;
1060 else
1061 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1064 if (WINDOW_WANTS_HEADER_LINE_P (w))
1066 struct glyph_row *hl_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (hl_row && hl_row->mode_line_p)
1071 height -= hl_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1076 /* With a very small font and a mode-line that's taller than
1077 default, we might end up with a negative height. */
1078 return max (0, height);
1081 /* Return the window-relative coordinate of the left edge of display
1082 area AREA of window W. AREA < 0 means return the left edge of the
1083 whole window, to the right of the left fringe of W. */
1085 INLINE int
1086 window_box_left_offset (w, area)
1087 struct window *w;
1088 int area;
1090 int x;
1092 if (w->pseudo_window_p)
1093 return 0;
1095 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1097 if (area == TEXT_AREA)
1098 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1099 + window_box_width (w, LEFT_MARGIN_AREA));
1100 else if (area == RIGHT_MARGIN_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA)
1103 + window_box_width (w, TEXT_AREA)
1104 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1106 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1107 else if (area == LEFT_MARGIN_AREA
1108 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1109 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1111 return x;
1115 /* Return the window-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the left edge of the
1117 whole window, to the left of the right fringe of W. */
1119 INLINE int
1120 window_box_right_offset (w, area)
1121 struct window *w;
1122 int area;
1124 return window_box_left_offset (w, area) + window_box_width (w, area);
1127 /* Return the frame-relative coordinate of the left edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the right of the left fringe of W. */
1131 INLINE int
1132 window_box_left (w, area)
1133 struct window *w;
1134 int area;
1136 struct frame *f = XFRAME (w->frame);
1137 int x;
1139 if (w->pseudo_window_p)
1140 return FRAME_INTERNAL_BORDER_WIDTH (f);
1142 x = (WINDOW_LEFT_EDGE_X (w)
1143 + window_box_left_offset (w, area));
1145 return x;
1149 /* Return the frame-relative coordinate of the right edge of display
1150 area AREA of window W. AREA < 0 means return the left edge of the
1151 whole window, to the left of the right fringe of W. */
1153 INLINE int
1154 window_box_right (w, area)
1155 struct window *w;
1156 int area;
1158 return window_box_left (w, area) + window_box_width (w, area);
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines, in frame-relative coordinates. AREA < 0 means the
1163 whole window, not including the left and right fringes of
1164 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1165 coordinates of the upper-left corner of the box. Return in
1166 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168 INLINE void
1169 window_box (w, area, box_x, box_y, box_width, box_height)
1170 struct window *w;
1171 int area;
1172 int *box_x, *box_y, *box_width, *box_height;
1174 if (box_width)
1175 *box_width = window_box_width (w, area);
1176 if (box_height)
1177 *box_height = window_box_height (w);
1178 if (box_x)
1179 *box_x = window_box_left (w, area);
1180 if (box_y)
1182 *box_y = WINDOW_TOP_EDGE_Y (w);
1183 if (WINDOW_WANTS_HEADER_LINE_P (w))
1184 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines. AREA < 0 means the whole window, not including the
1191 left and right fringe of the window. Return in *TOP_LEFT_X
1192 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1193 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1194 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1195 box. */
1197 INLINE void
1198 window_box_edges (w, area, top_left_x, top_left_y,
1199 bottom_right_x, bottom_right_y)
1200 struct window *w;
1201 int area;
1202 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1204 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1205 bottom_right_y);
1206 *bottom_right_x += *top_left_x;
1207 *bottom_right_y += *top_left_y;
1212 /***********************************************************************
1213 Utilities
1214 ***********************************************************************/
1216 /* Return the bottom y-position of the line the iterator IT is in.
1217 This can modify IT's settings. */
1220 line_bottom_y (it)
1221 struct it *it;
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1226 if (line_height == 0)
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1232 move_it_by_lines (it, 1, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1237 else
1239 struct glyph_row *row = it->glyph_row;
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1252 return line_top_y + line_height;
1256 /* Return 1 if position CHARPOS is visible in window W.
1257 If visible, set *X and *Y to pixel coordinates of top left corner.
1258 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1259 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1260 and header-lines heights. */
1263 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1264 struct window *w;
1265 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1267 struct it it;
1268 struct text_pos top;
1269 int visible_p = 0;
1270 struct buffer *old_buffer = NULL;
1272 if (noninteractive)
1273 return visible_p;
1275 if (XBUFFER (w->buffer) != current_buffer)
1277 old_buffer = current_buffer;
1278 set_buffer_internal_1 (XBUFFER (w->buffer));
1281 SET_TEXT_POS_FROM_MARKER (top, w->start);
1283 /* Compute exact mode line heights, if requested. */
1284 if (exact_mode_line_heights_p)
1286 if (WINDOW_WANTS_MODELINE_P (w))
1287 current_mode_line_height
1288 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1289 current_buffer->mode_line_format);
1291 if (WINDOW_WANTS_HEADER_LINE_P (w))
1292 current_header_line_height
1293 = display_mode_line (w, HEADER_LINE_FACE_ID,
1294 current_buffer->header_line_format);
1297 start_display (&it, w, top);
1298 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1299 MOVE_TO_POS | MOVE_TO_Y);
1301 /* Note that we may overshoot because of invisible text. */
1302 if (IT_CHARPOS (it) >= charpos)
1304 int top_x = it.current_x;
1305 int top_y = it.current_y;
1306 int bottom_y = (last_height = 0, line_bottom_y (&it));
1307 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1309 if (top_y < window_top_y)
1310 visible_p = bottom_y > window_top_y;
1311 else if (top_y < it.last_visible_y)
1312 visible_p = 1;
1313 if (visible_p)
1315 *x = top_x;
1316 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1317 *rtop = max (0, window_top_y - top_y);
1318 *rbot = max (0, bottom_y - it.last_visible_y);
1321 else
1323 struct it it2;
1325 it2 = it;
1326 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1327 move_it_by_lines (&it, 1, 0);
1328 if (charpos < IT_CHARPOS (it))
1330 visible_p = 1;
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1340 if (old_buffer)
1341 set_buffer_internal_1 (old_buffer);
1343 current_header_line_height = current_mode_line_height = -1;
1345 return visible_p;
1349 /* Return the next character from STR which is MAXLEN bytes long.
1350 Return in *LEN the length of the character. This is like
1351 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1352 we find one, we return a `?', but with the length of the invalid
1353 character. */
1355 static INLINE int
1356 string_char_and_length (str, maxlen, len)
1357 const unsigned char *str;
1358 int maxlen, *len;
1360 int c;
1362 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1363 if (!CHAR_VALID_P (c, 1))
1364 /* We may not change the length here because other places in Emacs
1365 don't use this function, i.e. they silently accept invalid
1366 characters. */
1367 c = '?';
1369 return c;
1374 /* Given a position POS containing a valid character and byte position
1375 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1377 static struct text_pos
1378 string_pos_nchars_ahead (pos, string, nchars)
1379 struct text_pos pos;
1380 Lisp_Object string;
1381 int nchars;
1383 xassert (STRINGP (string) && nchars >= 0);
1385 if (STRING_MULTIBYTE (string))
1387 int rest = SBYTES (string) - BYTEPOS (pos);
1388 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1389 int len;
1391 while (nchars--)
1393 string_char_and_length (p, rest, &len);
1394 p += len, rest -= len;
1395 xassert (rest >= 0);
1396 CHARPOS (pos) += 1;
1397 BYTEPOS (pos) += len;
1400 else
1401 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1403 return pos;
1407 /* Value is the text position, i.e. character and byte position,
1408 for character position CHARPOS in STRING. */
1410 static INLINE struct text_pos
1411 string_pos (charpos, string)
1412 int charpos;
1413 Lisp_Object string;
1415 struct text_pos pos;
1416 xassert (STRINGP (string));
1417 xassert (charpos >= 0);
1418 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1419 return pos;
1423 /* Value is a text position, i.e. character and byte position, for
1424 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1425 means recognize multibyte characters. */
1427 static struct text_pos
1428 c_string_pos (charpos, s, multibyte_p)
1429 int charpos;
1430 unsigned char *s;
1431 int multibyte_p;
1433 struct text_pos pos;
1435 xassert (s != NULL);
1436 xassert (charpos >= 0);
1438 if (multibyte_p)
1440 int rest = strlen (s), len;
1442 SET_TEXT_POS (pos, 0, 0);
1443 while (charpos--)
1445 string_char_and_length (s, rest, &len);
1446 s += len, rest -= len;
1447 xassert (rest >= 0);
1448 CHARPOS (pos) += 1;
1449 BYTEPOS (pos) += len;
1452 else
1453 SET_TEXT_POS (pos, charpos, charpos);
1455 return pos;
1459 /* Value is the number of characters in C string S. MULTIBYTE_P
1460 non-zero means recognize multibyte characters. */
1462 static int
1463 number_of_chars (s, multibyte_p)
1464 unsigned char *s;
1465 int multibyte_p;
1467 int nchars;
1469 if (multibyte_p)
1471 int rest = strlen (s), len;
1472 unsigned char *p = (unsigned char *) s;
1474 for (nchars = 0; rest > 0; ++nchars)
1476 string_char_and_length (p, rest, &len);
1477 rest -= len, p += len;
1480 else
1481 nchars = strlen (s);
1483 return nchars;
1487 /* Compute byte position NEWPOS->bytepos corresponding to
1488 NEWPOS->charpos. POS is a known position in string STRING.
1489 NEWPOS->charpos must be >= POS.charpos. */
1491 static void
1492 compute_string_pos (newpos, pos, string)
1493 struct text_pos *newpos, pos;
1494 Lisp_Object string;
1496 xassert (STRINGP (string));
1497 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1499 if (STRING_MULTIBYTE (string))
1500 *newpos = string_pos_nchars_ahead (pos, string,
1501 CHARPOS (*newpos) - CHARPOS (pos));
1502 else
1503 BYTEPOS (*newpos) = CHARPOS (*newpos);
1506 /* EXPORT:
1507 Return an estimation of the pixel height of mode or top lines on
1508 frame F. FACE_ID specifies what line's height to estimate. */
1511 estimate_mode_line_height (f, face_id)
1512 struct frame *f;
1513 enum face_id face_id;
1515 #ifdef HAVE_WINDOW_SYSTEM
1516 if (FRAME_WINDOW_P (f))
1518 int height = FONT_HEIGHT (FRAME_FONT (f));
1520 /* This function is called so early when Emacs starts that the face
1521 cache and mode line face are not yet initialized. */
1522 if (FRAME_FACE_CACHE (f))
1524 struct face *face = FACE_FROM_ID (f, face_id);
1525 if (face)
1527 if (face->font)
1528 height = FONT_HEIGHT (face->font);
1529 if (face->box_line_width > 0)
1530 height += 2 * face->box_line_width;
1534 return height;
1536 #endif
1538 return 1;
1541 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1542 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1543 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1544 not force the value into range. */
1546 void
1547 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1548 FRAME_PTR f;
1549 register int pix_x, pix_y;
1550 int *x, *y;
1551 NativeRectangle *bounds;
1552 int noclip;
1555 #ifdef HAVE_WINDOW_SYSTEM
1556 if (FRAME_WINDOW_P (f))
1558 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1559 even for negative values. */
1560 if (pix_x < 0)
1561 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1562 if (pix_y < 0)
1563 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1565 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1566 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1568 if (bounds)
1569 STORE_NATIVE_RECT (*bounds,
1570 FRAME_COL_TO_PIXEL_X (f, pix_x),
1571 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1572 FRAME_COLUMN_WIDTH (f) - 1,
1573 FRAME_LINE_HEIGHT (f) - 1);
1575 if (!noclip)
1577 if (pix_x < 0)
1578 pix_x = 0;
1579 else if (pix_x > FRAME_TOTAL_COLS (f))
1580 pix_x = FRAME_TOTAL_COLS (f);
1582 if (pix_y < 0)
1583 pix_y = 0;
1584 else if (pix_y > FRAME_LINES (f))
1585 pix_y = FRAME_LINES (f);
1588 #endif
1590 *x = pix_x;
1591 *y = pix_y;
1595 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1596 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1597 can't tell the positions because W's display is not up to date,
1598 return 0. */
1601 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1602 struct window *w;
1603 int hpos, vpos;
1604 int *frame_x, *frame_y;
1606 #ifdef HAVE_WINDOW_SYSTEM
1607 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1609 int success_p;
1611 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1612 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1614 if (display_completed)
1616 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1617 struct glyph *glyph = row->glyphs[TEXT_AREA];
1618 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1620 hpos = row->x;
1621 vpos = row->y;
1622 while (glyph < end)
1624 hpos += glyph->pixel_width;
1625 ++glyph;
1628 /* If first glyph is partially visible, its first visible position is still 0. */
1629 if (hpos < 0)
1630 hpos = 0;
1632 success_p = 1;
1634 else
1636 hpos = vpos = 0;
1637 success_p = 0;
1640 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1641 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1642 return success_p;
1644 #endif
1646 *frame_x = hpos;
1647 *frame_y = vpos;
1648 return 1;
1652 #ifdef HAVE_WINDOW_SYSTEM
1654 /* Find the glyph under window-relative coordinates X/Y in window W.
1655 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1656 strings. Return in *HPOS and *VPOS the row and column number of
1657 the glyph found. Return in *AREA the glyph area containing X.
1658 Value is a pointer to the glyph found or null if X/Y is not on
1659 text, or we can't tell because W's current matrix is not up to
1660 date. */
1662 static struct glyph *
1663 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1664 struct window *w;
1665 int x, y;
1666 int *hpos, *vpos, *dx, *dy, *area;
1668 struct glyph *glyph, *end;
1669 struct glyph_row *row = NULL;
1670 int x0, i;
1672 /* Find row containing Y. Give up if some row is not enabled. */
1673 for (i = 0; i < w->current_matrix->nrows; ++i)
1675 row = MATRIX_ROW (w->current_matrix, i);
1676 if (!row->enabled_p)
1677 return NULL;
1678 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1679 break;
1682 *vpos = i;
1683 *hpos = 0;
1685 /* Give up if Y is not in the window. */
1686 if (i == w->current_matrix->nrows)
1687 return NULL;
1689 /* Get the glyph area containing X. */
1690 if (w->pseudo_window_p)
1692 *area = TEXT_AREA;
1693 x0 = 0;
1695 else
1697 if (x < window_box_left_offset (w, TEXT_AREA))
1699 *area = LEFT_MARGIN_AREA;
1700 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1702 else if (x < window_box_right_offset (w, TEXT_AREA))
1704 *area = TEXT_AREA;
1705 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1707 else
1709 *area = RIGHT_MARGIN_AREA;
1710 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1714 /* Find glyph containing X. */
1715 glyph = row->glyphs[*area];
1716 end = glyph + row->used[*area];
1717 x -= x0;
1718 while (glyph < end && x >= glyph->pixel_width)
1720 x -= glyph->pixel_width;
1721 ++glyph;
1724 if (glyph == end)
1725 return NULL;
1727 if (dx)
1729 *dx = x;
1730 *dy = y - (row->y + row->ascent - glyph->ascent);
1733 *hpos = glyph - row->glyphs[*area];
1734 return glyph;
1738 /* EXPORT:
1739 Convert frame-relative x/y to coordinates relative to window W.
1740 Takes pseudo-windows into account. */
1742 void
1743 frame_to_window_pixel_xy (w, x, y)
1744 struct window *w;
1745 int *x, *y;
1747 if (w->pseudo_window_p)
1749 /* A pseudo-window is always full-width, and starts at the
1750 left edge of the frame, plus a frame border. */
1751 struct frame *f = XFRAME (w->frame);
1752 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1753 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1755 else
1757 *x -= WINDOW_LEFT_EDGE_X (w);
1758 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 /* EXPORT:
1763 Return in *R the clipping rectangle for glyph string S. */
1765 void
1766 get_glyph_string_clip_rect (s, nr)
1767 struct glyph_string *s;
1768 NativeRectangle *nr;
1770 XRectangle r;
1772 if (s->row->full_width_p)
1774 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1775 r.x = WINDOW_LEFT_EDGE_X (s->w);
1776 r.width = WINDOW_TOTAL_WIDTH (s->w);
1778 /* Unless displaying a mode or menu bar line, which are always
1779 fully visible, clip to the visible part of the row. */
1780 if (s->w->pseudo_window_p)
1781 r.height = s->row->visible_height;
1782 else
1783 r.height = s->height;
1785 else
1787 /* This is a text line that may be partially visible. */
1788 r.x = window_box_left (s->w, s->area);
1789 r.width = window_box_width (s->w, s->area);
1790 r.height = s->row->visible_height;
1793 if (s->clip_head)
1794 if (r.x < s->clip_head->x)
1796 if (r.width >= s->clip_head->x - r.x)
1797 r.width -= s->clip_head->x - r.x;
1798 else
1799 r.width = 0;
1800 r.x = s->clip_head->x;
1802 if (s->clip_tail)
1803 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1805 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1806 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1807 else
1808 r.width = 0;
1811 /* If S draws overlapping rows, it's sufficient to use the top and
1812 bottom of the window for clipping because this glyph string
1813 intentionally draws over other lines. */
1814 if (s->for_overlaps_p)
1816 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1817 r.height = window_text_bottom_y (s->w) - r.y;
1819 else
1821 /* Don't use S->y for clipping because it doesn't take partially
1822 visible lines into account. For example, it can be negative for
1823 partially visible lines at the top of a window. */
1824 if (!s->row->full_width_p
1825 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1826 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1827 else
1828 r.y = max (0, s->row->y);
1830 /* If drawing a tool-bar window, draw it over the internal border
1831 at the top of the window. */
1832 if (WINDOWP (s->f->tool_bar_window)
1833 && s->w == XWINDOW (s->f->tool_bar_window))
1834 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1837 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1839 /* If drawing the cursor, don't let glyph draw outside its
1840 advertised boundaries. Cleartype does this under some circumstances. */
1841 if (s->hl == DRAW_CURSOR)
1843 struct glyph *glyph = s->first_glyph;
1844 int height, max_y;
1846 if (s->x > r.x)
1848 r.width -= s->x - r.x;
1849 r.x = s->x;
1851 r.width = min (r.width, glyph->pixel_width);
1853 /* If r.y is below window bottom, ensure that we still see a cursor. */
1854 height = min (glyph->ascent + glyph->descent,
1855 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1856 max_y = window_text_bottom_y (s->w) - height;
1857 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1858 if (s->ybase - glyph->ascent > max_y)
1860 r.y = max_y;
1861 r.height = height;
1863 else
1865 /* Don't draw cursor glyph taller than our actual glyph. */
1866 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1867 if (height < r.height)
1869 max_y = r.y + r.height;
1870 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1871 r.height = min (max_y - r.y, height);
1876 #ifdef CONVERT_FROM_XRECT
1877 CONVERT_FROM_XRECT (r, *nr);
1878 #else
1879 *nr = r;
1880 #endif
1884 /* EXPORT:
1885 Return the position and height of the phys cursor in window W.
1886 Set w->phys_cursor_width to width of phys cursor.
1890 get_phys_cursor_geometry (w, row, glyph, heightp)
1891 struct window *w;
1892 struct glyph_row *row;
1893 struct glyph *glyph;
1894 int *heightp;
1896 struct frame *f = XFRAME (WINDOW_FRAME (w));
1897 int x, y, wd, h, h0, y0;
1899 /* Compute the width of the rectangle to draw. If on a stretch
1900 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1901 rectangle as wide as the glyph, but use a canonical character
1902 width instead. */
1903 wd = glyph->pixel_width - 1;
1904 #ifdef HAVE_NTGUI
1905 wd++; /* Why? */
1906 #endif
1907 if (glyph->type == STRETCH_GLYPH
1908 && !x_stretch_cursor_p)
1909 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1910 w->phys_cursor_width = wd;
1912 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1914 /* If y is below window bottom, ensure that we still see a cursor. */
1915 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1917 h = max (h0, glyph->ascent + glyph->descent);
1918 h0 = min (h0, glyph->ascent + glyph->descent);
1920 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1921 if (y < y0)
1923 h = max (h - (y0 - y) + 1, h0);
1924 y = y0 - 1;
1926 else
1928 y0 = window_text_bottom_y (w) - h0;
1929 if (y > y0)
1931 h += y - y0;
1932 y = y0;
1936 *heightp = h - 1;
1937 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1941 #endif /* HAVE_WINDOW_SYSTEM */
1944 /***********************************************************************
1945 Lisp form evaluation
1946 ***********************************************************************/
1948 /* Error handler for safe_eval and safe_call. */
1950 static Lisp_Object
1951 safe_eval_handler (arg)
1952 Lisp_Object arg;
1954 add_to_log ("Error during redisplay: %s", arg, Qnil);
1955 return Qnil;
1959 /* Evaluate SEXPR and return the result, or nil if something went
1960 wrong. Prevent redisplay during the evaluation. */
1962 Lisp_Object
1963 safe_eval (sexpr)
1964 Lisp_Object sexpr;
1966 Lisp_Object val;
1968 if (inhibit_eval_during_redisplay)
1969 val = Qnil;
1970 else
1972 int count = SPECPDL_INDEX ();
1973 struct gcpro gcpro1;
1975 GCPRO1 (sexpr);
1976 specbind (Qinhibit_redisplay, Qt);
1977 /* Use Qt to ensure debugger does not run,
1978 so there is no possibility of wanting to redisplay. */
1979 val = internal_condition_case_1 (Feval, sexpr, Qt,
1980 safe_eval_handler);
1981 UNGCPRO;
1982 val = unbind_to (count, val);
1985 return val;
1989 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1990 Return the result, or nil if something went wrong. Prevent
1991 redisplay during the evaluation. */
1993 Lisp_Object
1994 safe_call (nargs, args)
1995 int nargs;
1996 Lisp_Object *args;
1998 Lisp_Object val;
2000 if (inhibit_eval_during_redisplay)
2001 val = Qnil;
2002 else
2004 int count = SPECPDL_INDEX ();
2005 struct gcpro gcpro1;
2007 GCPRO1 (args[0]);
2008 gcpro1.nvars = nargs;
2009 specbind (Qinhibit_redisplay, Qt);
2010 /* Use Qt to ensure debugger does not run,
2011 so there is no possibility of wanting to redisplay. */
2012 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2013 safe_eval_handler);
2014 UNGCPRO;
2015 val = unbind_to (count, val);
2018 return val;
2022 /* Call function FN with one argument ARG.
2023 Return the result, or nil if something went wrong. */
2025 Lisp_Object
2026 safe_call1 (fn, arg)
2027 Lisp_Object fn, arg;
2029 Lisp_Object args[2];
2030 args[0] = fn;
2031 args[1] = arg;
2032 return safe_call (2, args);
2037 /***********************************************************************
2038 Debugging
2039 ***********************************************************************/
2041 #if 0
2043 /* Define CHECK_IT to perform sanity checks on iterators.
2044 This is for debugging. It is too slow to do unconditionally. */
2046 static void
2047 check_it (it)
2048 struct it *it;
2050 if (it->method == GET_FROM_STRING)
2052 xassert (STRINGP (it->string));
2053 xassert (IT_STRING_CHARPOS (*it) >= 0);
2055 else
2057 xassert (IT_STRING_CHARPOS (*it) < 0);
2058 if (it->method == GET_FROM_BUFFER)
2060 /* Check that character and byte positions agree. */
2061 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2065 if (it->dpvec)
2066 xassert (it->current.dpvec_index >= 0);
2067 else
2068 xassert (it->current.dpvec_index < 0);
2071 #define CHECK_IT(IT) check_it ((IT))
2073 #else /* not 0 */
2075 #define CHECK_IT(IT) (void) 0
2077 #endif /* not 0 */
2080 #if GLYPH_DEBUG
2082 /* Check that the window end of window W is what we expect it
2083 to be---the last row in the current matrix displaying text. */
2085 static void
2086 check_window_end (w)
2087 struct window *w;
2089 if (!MINI_WINDOW_P (w)
2090 && !NILP (w->window_end_valid))
2092 struct glyph_row *row;
2093 xassert ((row = MATRIX_ROW (w->current_matrix,
2094 XFASTINT (w->window_end_vpos)),
2095 !row->enabled_p
2096 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2097 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2101 #define CHECK_WINDOW_END(W) check_window_end ((W))
2103 #else /* not GLYPH_DEBUG */
2105 #define CHECK_WINDOW_END(W) (void) 0
2107 #endif /* not GLYPH_DEBUG */
2111 /***********************************************************************
2112 Iterator initialization
2113 ***********************************************************************/
2115 /* Initialize IT for displaying current_buffer in window W, starting
2116 at character position CHARPOS. CHARPOS < 0 means that no buffer
2117 position is specified which is useful when the iterator is assigned
2118 a position later. BYTEPOS is the byte position corresponding to
2119 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2121 If ROW is not null, calls to produce_glyphs with IT as parameter
2122 will produce glyphs in that row.
2124 BASE_FACE_ID is the id of a base face to use. It must be one of
2125 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2126 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2127 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2129 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2131 will be initialized to use the corresponding mode line glyph row of
2132 the desired matrix of W. */
2134 void
2135 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2136 struct it *it;
2137 struct window *w;
2138 int charpos, bytepos;
2139 struct glyph_row *row;
2140 enum face_id base_face_id;
2142 int highlight_region_p;
2144 /* Some precondition checks. */
2145 xassert (w != NULL && it != NULL);
2146 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2147 && charpos <= ZV));
2149 /* If face attributes have been changed since the last redisplay,
2150 free realized faces now because they depend on face definitions
2151 that might have changed. Don't free faces while there might be
2152 desired matrices pending which reference these faces. */
2153 if (face_change_count && !inhibit_free_realized_faces)
2155 face_change_count = 0;
2156 free_all_realized_faces (Qnil);
2159 /* Use one of the mode line rows of W's desired matrix if
2160 appropriate. */
2161 if (row == NULL)
2163 if (base_face_id == MODE_LINE_FACE_ID
2164 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2165 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2166 else if (base_face_id == HEADER_LINE_FACE_ID)
2167 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2170 /* Clear IT. */
2171 bzero (it, sizeof *it);
2172 it->current.overlay_string_index = -1;
2173 it->current.dpvec_index = -1;
2174 it->base_face_id = base_face_id;
2175 it->string = Qnil;
2176 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2178 /* The window in which we iterate over current_buffer: */
2179 XSETWINDOW (it->window, w);
2180 it->w = w;
2181 it->f = XFRAME (w->frame);
2183 /* Extra space between lines (on window systems only). */
2184 if (base_face_id == DEFAULT_FACE_ID
2185 && FRAME_WINDOW_P (it->f))
2187 if (NATNUMP (current_buffer->extra_line_spacing))
2188 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2189 else if (FLOATP (current_buffer->extra_line_spacing))
2190 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2191 * FRAME_LINE_HEIGHT (it->f));
2192 else if (it->f->extra_line_spacing > 0)
2193 it->extra_line_spacing = it->f->extra_line_spacing;
2194 it->max_extra_line_spacing = 0;
2197 /* If realized faces have been removed, e.g. because of face
2198 attribute changes of named faces, recompute them. When running
2199 in batch mode, the face cache of Vterminal_frame is null. If
2200 we happen to get called, make a dummy face cache. */
2201 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2202 init_frame_faces (it->f);
2203 if (FRAME_FACE_CACHE (it->f)->used == 0)
2204 recompute_basic_faces (it->f);
2206 /* Current value of the `slice', `space-width', and 'height' properties. */
2207 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2208 it->space_width = Qnil;
2209 it->font_height = Qnil;
2210 it->override_ascent = -1;
2212 /* Are control characters displayed as `^C'? */
2213 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2215 /* -1 means everything between a CR and the following line end
2216 is invisible. >0 means lines indented more than this value are
2217 invisible. */
2218 it->selective = (INTEGERP (current_buffer->selective_display)
2219 ? XFASTINT (current_buffer->selective_display)
2220 : (!NILP (current_buffer->selective_display)
2221 ? -1 : 0));
2222 it->selective_display_ellipsis_p
2223 = !NILP (current_buffer->selective_display_ellipses);
2225 /* Display table to use. */
2226 it->dp = window_display_table (w);
2228 /* Are multibyte characters enabled in current_buffer? */
2229 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2231 /* Non-zero if we should highlight the region. */
2232 highlight_region_p
2233 = (!NILP (Vtransient_mark_mode)
2234 && !NILP (current_buffer->mark_active)
2235 && XMARKER (current_buffer->mark)->buffer != 0);
2237 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2238 start and end of a visible region in window IT->w. Set both to
2239 -1 to indicate no region. */
2240 if (highlight_region_p
2241 /* Maybe highlight only in selected window. */
2242 && (/* Either show region everywhere. */
2243 highlight_nonselected_windows
2244 /* Or show region in the selected window. */
2245 || w == XWINDOW (selected_window)
2246 /* Or show the region if we are in the mini-buffer and W is
2247 the window the mini-buffer refers to. */
2248 || (MINI_WINDOW_P (XWINDOW (selected_window))
2249 && WINDOWP (minibuf_selected_window)
2250 && w == XWINDOW (minibuf_selected_window))))
2252 int charpos = marker_position (current_buffer->mark);
2253 it->region_beg_charpos = min (PT, charpos);
2254 it->region_end_charpos = max (PT, charpos);
2256 else
2257 it->region_beg_charpos = it->region_end_charpos = -1;
2259 /* Get the position at which the redisplay_end_trigger hook should
2260 be run, if it is to be run at all. */
2261 if (MARKERP (w->redisplay_end_trigger)
2262 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2263 it->redisplay_end_trigger_charpos
2264 = marker_position (w->redisplay_end_trigger);
2265 else if (INTEGERP (w->redisplay_end_trigger))
2266 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2268 /* Correct bogus values of tab_width. */
2269 it->tab_width = XINT (current_buffer->tab_width);
2270 if (it->tab_width <= 0 || it->tab_width > 1000)
2271 it->tab_width = 8;
2273 /* Are lines in the display truncated? */
2274 it->truncate_lines_p
2275 = (base_face_id != DEFAULT_FACE_ID
2276 || XINT (it->w->hscroll)
2277 || (truncate_partial_width_windows
2278 && !WINDOW_FULL_WIDTH_P (it->w))
2279 || !NILP (current_buffer->truncate_lines));
2281 /* Get dimensions of truncation and continuation glyphs. These are
2282 displayed as fringe bitmaps under X, so we don't need them for such
2283 frames. */
2284 if (!FRAME_WINDOW_P (it->f))
2286 if (it->truncate_lines_p)
2288 /* We will need the truncation glyph. */
2289 xassert (it->glyph_row == NULL);
2290 produce_special_glyphs (it, IT_TRUNCATION);
2291 it->truncation_pixel_width = it->pixel_width;
2293 else
2295 /* We will need the continuation glyph. */
2296 xassert (it->glyph_row == NULL);
2297 produce_special_glyphs (it, IT_CONTINUATION);
2298 it->continuation_pixel_width = it->pixel_width;
2301 /* Reset these values to zero because the produce_special_glyphs
2302 above has changed them. */
2303 it->pixel_width = it->ascent = it->descent = 0;
2304 it->phys_ascent = it->phys_descent = 0;
2307 /* Set this after getting the dimensions of truncation and
2308 continuation glyphs, so that we don't produce glyphs when calling
2309 produce_special_glyphs, above. */
2310 it->glyph_row = row;
2311 it->area = TEXT_AREA;
2313 /* Get the dimensions of the display area. The display area
2314 consists of the visible window area plus a horizontally scrolled
2315 part to the left of the window. All x-values are relative to the
2316 start of this total display area. */
2317 if (base_face_id != DEFAULT_FACE_ID)
2319 /* Mode lines, menu bar in terminal frames. */
2320 it->first_visible_x = 0;
2321 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2323 else
2325 it->first_visible_x
2326 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2327 it->last_visible_x = (it->first_visible_x
2328 + window_box_width (w, TEXT_AREA));
2330 /* If we truncate lines, leave room for the truncator glyph(s) at
2331 the right margin. Otherwise, leave room for the continuation
2332 glyph(s). Truncation and continuation glyphs are not inserted
2333 for window-based redisplay. */
2334 if (!FRAME_WINDOW_P (it->f))
2336 if (it->truncate_lines_p)
2337 it->last_visible_x -= it->truncation_pixel_width;
2338 else
2339 it->last_visible_x -= it->continuation_pixel_width;
2342 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2343 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2346 /* Leave room for a border glyph. */
2347 if (!FRAME_WINDOW_P (it->f)
2348 && !WINDOW_RIGHTMOST_P (it->w))
2349 it->last_visible_x -= 1;
2351 it->last_visible_y = window_text_bottom_y (w);
2353 /* For mode lines and alike, arrange for the first glyph having a
2354 left box line if the face specifies a box. */
2355 if (base_face_id != DEFAULT_FACE_ID)
2357 struct face *face;
2359 it->face_id = base_face_id;
2361 /* If we have a boxed mode line, make the first character appear
2362 with a left box line. */
2363 face = FACE_FROM_ID (it->f, base_face_id);
2364 if (face->box != FACE_NO_BOX)
2365 it->start_of_box_run_p = 1;
2368 /* If a buffer position was specified, set the iterator there,
2369 getting overlays and face properties from that position. */
2370 if (charpos >= BUF_BEG (current_buffer))
2372 it->end_charpos = ZV;
2373 it->face_id = -1;
2374 IT_CHARPOS (*it) = charpos;
2376 /* Compute byte position if not specified. */
2377 if (bytepos < charpos)
2378 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2379 else
2380 IT_BYTEPOS (*it) = bytepos;
2382 it->start = it->current;
2384 /* Compute faces etc. */
2385 reseat (it, it->current.pos, 1);
2388 CHECK_IT (it);
2392 /* Initialize IT for the display of window W with window start POS. */
2394 void
2395 start_display (it, w, pos)
2396 struct it *it;
2397 struct window *w;
2398 struct text_pos pos;
2400 struct glyph_row *row;
2401 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2403 row = w->desired_matrix->rows + first_vpos;
2404 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2405 it->first_vpos = first_vpos;
2407 if (!it->truncate_lines_p)
2409 int start_at_line_beg_p;
2410 int first_y = it->current_y;
2412 /* If window start is not at a line start, skip forward to POS to
2413 get the correct continuation lines width. */
2414 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2415 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2416 if (!start_at_line_beg_p)
2418 int new_x;
2420 reseat_at_previous_visible_line_start (it);
2421 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2423 new_x = it->current_x + it->pixel_width;
2425 /* If lines are continued, this line may end in the middle
2426 of a multi-glyph character (e.g. a control character
2427 displayed as \003, or in the middle of an overlay
2428 string). In this case move_it_to above will not have
2429 taken us to the start of the continuation line but to the
2430 end of the continued line. */
2431 if (it->current_x > 0
2432 && !it->truncate_lines_p /* Lines are continued. */
2433 && (/* And glyph doesn't fit on the line. */
2434 new_x > it->last_visible_x
2435 /* Or it fits exactly and we're on a window
2436 system frame. */
2437 || (new_x == it->last_visible_x
2438 && FRAME_WINDOW_P (it->f))))
2440 if (it->current.dpvec_index >= 0
2441 || it->current.overlay_string_index >= 0)
2443 set_iterator_to_next (it, 1);
2444 move_it_in_display_line_to (it, -1, -1, 0);
2447 it->continuation_lines_width += it->current_x;
2450 /* We're starting a new display line, not affected by the
2451 height of the continued line, so clear the appropriate
2452 fields in the iterator structure. */
2453 it->max_ascent = it->max_descent = 0;
2454 it->max_phys_ascent = it->max_phys_descent = 0;
2456 it->current_y = first_y;
2457 it->vpos = 0;
2458 it->current_x = it->hpos = 0;
2462 #if 0 /* Don't assert the following because start_display is sometimes
2463 called intentionally with a window start that is not at a
2464 line start. Please leave this code in as a comment. */
2466 /* Window start should be on a line start, now. */
2467 xassert (it->continuation_lines_width
2468 || IT_CHARPOS (it) == BEGV
2469 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2470 #endif /* 0 */
2474 /* Return 1 if POS is a position in ellipses displayed for invisible
2475 text. W is the window we display, for text property lookup. */
2477 static int
2478 in_ellipses_for_invisible_text_p (pos, w)
2479 struct display_pos *pos;
2480 struct window *w;
2482 Lisp_Object prop, window;
2483 int ellipses_p = 0;
2484 int charpos = CHARPOS (pos->pos);
2486 /* If POS specifies a position in a display vector, this might
2487 be for an ellipsis displayed for invisible text. We won't
2488 get the iterator set up for delivering that ellipsis unless
2489 we make sure that it gets aware of the invisible text. */
2490 if (pos->dpvec_index >= 0
2491 && pos->overlay_string_index < 0
2492 && CHARPOS (pos->string_pos) < 0
2493 && charpos > BEGV
2494 && (XSETWINDOW (window, w),
2495 prop = Fget_char_property (make_number (charpos),
2496 Qinvisible, window),
2497 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2499 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2500 window);
2501 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2504 return ellipses_p;
2508 /* Initialize IT for stepping through current_buffer in window W,
2509 starting at position POS that includes overlay string and display
2510 vector/ control character translation position information. Value
2511 is zero if there are overlay strings with newlines at POS. */
2513 static int
2514 init_from_display_pos (it, w, pos)
2515 struct it *it;
2516 struct window *w;
2517 struct display_pos *pos;
2519 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2520 int i, overlay_strings_with_newlines = 0;
2522 /* If POS specifies a position in a display vector, this might
2523 be for an ellipsis displayed for invisible text. We won't
2524 get the iterator set up for delivering that ellipsis unless
2525 we make sure that it gets aware of the invisible text. */
2526 if (in_ellipses_for_invisible_text_p (pos, w))
2528 --charpos;
2529 bytepos = 0;
2532 /* Keep in mind: the call to reseat in init_iterator skips invisible
2533 text, so we might end up at a position different from POS. This
2534 is only a problem when POS is a row start after a newline and an
2535 overlay starts there with an after-string, and the overlay has an
2536 invisible property. Since we don't skip invisible text in
2537 display_line and elsewhere immediately after consuming the
2538 newline before the row start, such a POS will not be in a string,
2539 but the call to init_iterator below will move us to the
2540 after-string. */
2541 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2543 /* This only scans the current chunk -- it should scan all chunks.
2544 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2545 to 16 in 22.1 to make this a lesser problem. */
2546 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2548 const char *s = SDATA (it->overlay_strings[i]);
2549 const char *e = s + SBYTES (it->overlay_strings[i]);
2551 while (s < e && *s != '\n')
2552 ++s;
2554 if (s < e)
2556 overlay_strings_with_newlines = 1;
2557 break;
2561 /* If position is within an overlay string, set up IT to the right
2562 overlay string. */
2563 if (pos->overlay_string_index >= 0)
2565 int relative_index;
2567 /* If the first overlay string happens to have a `display'
2568 property for an image, the iterator will be set up for that
2569 image, and we have to undo that setup first before we can
2570 correct the overlay string index. */
2571 if (it->method == GET_FROM_IMAGE)
2572 pop_it (it);
2574 /* We already have the first chunk of overlay strings in
2575 IT->overlay_strings. Load more until the one for
2576 pos->overlay_string_index is in IT->overlay_strings. */
2577 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2579 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2580 it->current.overlay_string_index = 0;
2581 while (n--)
2583 load_overlay_strings (it, 0);
2584 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2588 it->current.overlay_string_index = pos->overlay_string_index;
2589 relative_index = (it->current.overlay_string_index
2590 % OVERLAY_STRING_CHUNK_SIZE);
2591 it->string = it->overlay_strings[relative_index];
2592 xassert (STRINGP (it->string));
2593 it->current.string_pos = pos->string_pos;
2594 it->method = GET_FROM_STRING;
2597 #if 0 /* This is bogus because POS not having an overlay string
2598 position does not mean it's after the string. Example: A
2599 line starting with a before-string and initialization of IT
2600 to the previous row's end position. */
2601 else if (it->current.overlay_string_index >= 0)
2603 /* If POS says we're already after an overlay string ending at
2604 POS, make sure to pop the iterator because it will be in
2605 front of that overlay string. When POS is ZV, we've thereby
2606 also ``processed'' overlay strings at ZV. */
2607 while (it->sp)
2608 pop_it (it);
2609 it->current.overlay_string_index = -1;
2610 it->method = GET_FROM_BUFFER;
2611 if (CHARPOS (pos->pos) == ZV)
2612 it->overlay_strings_at_end_processed_p = 1;
2614 #endif /* 0 */
2616 if (CHARPOS (pos->string_pos) >= 0)
2618 /* Recorded position is not in an overlay string, but in another
2619 string. This can only be a string from a `display' property.
2620 IT should already be filled with that string. */
2621 it->current.string_pos = pos->string_pos;
2622 xassert (STRINGP (it->string));
2625 /* Restore position in display vector translations, control
2626 character translations or ellipses. */
2627 if (pos->dpvec_index >= 0)
2629 if (it->dpvec == NULL)
2630 get_next_display_element (it);
2631 xassert (it->dpvec && it->current.dpvec_index == 0);
2632 it->current.dpvec_index = pos->dpvec_index;
2635 CHECK_IT (it);
2636 return !overlay_strings_with_newlines;
2640 /* Initialize IT for stepping through current_buffer in window W
2641 starting at ROW->start. */
2643 static void
2644 init_to_row_start (it, w, row)
2645 struct it *it;
2646 struct window *w;
2647 struct glyph_row *row;
2649 init_from_display_pos (it, w, &row->start);
2650 it->start = row->start;
2651 it->continuation_lines_width = row->continuation_lines_width;
2652 CHECK_IT (it);
2656 /* Initialize IT for stepping through current_buffer in window W
2657 starting in the line following ROW, i.e. starting at ROW->end.
2658 Value is zero if there are overlay strings with newlines at ROW's
2659 end position. */
2661 static int
2662 init_to_row_end (it, w, row)
2663 struct it *it;
2664 struct window *w;
2665 struct glyph_row *row;
2667 int success = 0;
2669 if (init_from_display_pos (it, w, &row->end))
2671 if (row->continued_p)
2672 it->continuation_lines_width
2673 = row->continuation_lines_width + row->pixel_width;
2674 CHECK_IT (it);
2675 success = 1;
2678 return success;
2684 /***********************************************************************
2685 Text properties
2686 ***********************************************************************/
2688 /* Called when IT reaches IT->stop_charpos. Handle text property and
2689 overlay changes. Set IT->stop_charpos to the next position where
2690 to stop. */
2692 static void
2693 handle_stop (it)
2694 struct it *it;
2696 enum prop_handled handled;
2697 int handle_overlay_change_p = 1;
2698 struct props *p;
2700 it->dpvec = NULL;
2701 it->current.dpvec_index = -1;
2703 /* Use face of preceding text for ellipsis (if invisible) */
2704 if (it->selective_display_ellipsis_p)
2705 it->saved_face_id = it->face_id;
2709 handled = HANDLED_NORMALLY;
2711 /* Call text property handlers. */
2712 for (p = it_props; p->handler; ++p)
2714 handled = p->handler (it);
2716 if (handled == HANDLED_RECOMPUTE_PROPS)
2717 break;
2718 else if (handled == HANDLED_RETURN)
2719 return;
2720 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2721 handle_overlay_change_p = 0;
2724 if (handled != HANDLED_RECOMPUTE_PROPS)
2726 /* Don't check for overlay strings below when set to deliver
2727 characters from a display vector. */
2728 if (it->method == GET_FROM_DISPLAY_VECTOR)
2729 handle_overlay_change_p = 0;
2731 /* Handle overlay changes. */
2732 if (handle_overlay_change_p)
2733 handled = handle_overlay_change (it);
2735 /* Determine where to stop next. */
2736 if (handled == HANDLED_NORMALLY)
2737 compute_stop_pos (it);
2740 while (handled == HANDLED_RECOMPUTE_PROPS);
2744 /* Compute IT->stop_charpos from text property and overlay change
2745 information for IT's current position. */
2747 static void
2748 compute_stop_pos (it)
2749 struct it *it;
2751 register INTERVAL iv, next_iv;
2752 Lisp_Object object, limit, position;
2754 /* If nowhere else, stop at the end. */
2755 it->stop_charpos = it->end_charpos;
2757 if (STRINGP (it->string))
2759 /* Strings are usually short, so don't limit the search for
2760 properties. */
2761 object = it->string;
2762 limit = Qnil;
2763 position = make_number (IT_STRING_CHARPOS (*it));
2765 else
2767 int charpos;
2769 /* If next overlay change is in front of the current stop pos
2770 (which is IT->end_charpos), stop there. Note: value of
2771 next_overlay_change is point-max if no overlay change
2772 follows. */
2773 charpos = next_overlay_change (IT_CHARPOS (*it));
2774 if (charpos < it->stop_charpos)
2775 it->stop_charpos = charpos;
2777 /* If showing the region, we have to stop at the region
2778 start or end because the face might change there. */
2779 if (it->region_beg_charpos > 0)
2781 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2782 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2783 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2784 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2787 /* Set up variables for computing the stop position from text
2788 property changes. */
2789 XSETBUFFER (object, current_buffer);
2790 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2791 position = make_number (IT_CHARPOS (*it));
2795 /* Get the interval containing IT's position. Value is a null
2796 interval if there isn't such an interval. */
2797 iv = validate_interval_range (object, &position, &position, 0);
2798 if (!NULL_INTERVAL_P (iv))
2800 Lisp_Object values_here[LAST_PROP_IDX];
2801 struct props *p;
2803 /* Get properties here. */
2804 for (p = it_props; p->handler; ++p)
2805 values_here[p->idx] = textget (iv->plist, *p->name);
2807 /* Look for an interval following iv that has different
2808 properties. */
2809 for (next_iv = next_interval (iv);
2810 (!NULL_INTERVAL_P (next_iv)
2811 && (NILP (limit)
2812 || XFASTINT (limit) > next_iv->position));
2813 next_iv = next_interval (next_iv))
2815 for (p = it_props; p->handler; ++p)
2817 Lisp_Object new_value;
2819 new_value = textget (next_iv->plist, *p->name);
2820 if (!EQ (values_here[p->idx], new_value))
2821 break;
2824 if (p->handler)
2825 break;
2828 if (!NULL_INTERVAL_P (next_iv))
2830 if (INTEGERP (limit)
2831 && next_iv->position >= XFASTINT (limit))
2832 /* No text property change up to limit. */
2833 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2834 else
2835 /* Text properties change in next_iv. */
2836 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2840 xassert (STRINGP (it->string)
2841 || (it->stop_charpos >= BEGV
2842 && it->stop_charpos >= IT_CHARPOS (*it)));
2846 /* Return the position of the next overlay change after POS in
2847 current_buffer. Value is point-max if no overlay change
2848 follows. This is like `next-overlay-change' but doesn't use
2849 xmalloc. */
2851 static int
2852 next_overlay_change (pos)
2853 int pos;
2855 int noverlays;
2856 int endpos;
2857 Lisp_Object *overlays;
2858 int i;
2860 /* Get all overlays at the given position. */
2861 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2863 /* If any of these overlays ends before endpos,
2864 use its ending point instead. */
2865 for (i = 0; i < noverlays; ++i)
2867 Lisp_Object oend;
2868 int oendpos;
2870 oend = OVERLAY_END (overlays[i]);
2871 oendpos = OVERLAY_POSITION (oend);
2872 endpos = min (endpos, oendpos);
2875 return endpos;
2880 /***********************************************************************
2881 Fontification
2882 ***********************************************************************/
2884 /* Handle changes in the `fontified' property of the current buffer by
2885 calling hook functions from Qfontification_functions to fontify
2886 regions of text. */
2888 static enum prop_handled
2889 handle_fontified_prop (it)
2890 struct it *it;
2892 Lisp_Object prop, pos;
2893 enum prop_handled handled = HANDLED_NORMALLY;
2895 /* Get the value of the `fontified' property at IT's current buffer
2896 position. (The `fontified' property doesn't have a special
2897 meaning in strings.) If the value is nil, call functions from
2898 Qfontification_functions. */
2899 if (!STRINGP (it->string)
2900 && it->s == NULL
2901 && !NILP (Vfontification_functions)
2902 && !NILP (Vrun_hooks)
2903 && (pos = make_number (IT_CHARPOS (*it)),
2904 prop = Fget_char_property (pos, Qfontified, Qnil),
2905 NILP (prop)))
2907 int count = SPECPDL_INDEX ();
2908 Lisp_Object val;
2910 val = Vfontification_functions;
2911 specbind (Qfontification_functions, Qnil);
2913 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2914 safe_call1 (val, pos);
2915 else
2917 Lisp_Object globals, fn;
2918 struct gcpro gcpro1, gcpro2;
2920 globals = Qnil;
2921 GCPRO2 (val, globals);
2923 for (; CONSP (val); val = XCDR (val))
2925 fn = XCAR (val);
2927 if (EQ (fn, Qt))
2929 /* A value of t indicates this hook has a local
2930 binding; it means to run the global binding too.
2931 In a global value, t should not occur. If it
2932 does, we must ignore it to avoid an endless
2933 loop. */
2934 for (globals = Fdefault_value (Qfontification_functions);
2935 CONSP (globals);
2936 globals = XCDR (globals))
2938 fn = XCAR (globals);
2939 if (!EQ (fn, Qt))
2940 safe_call1 (fn, pos);
2943 else
2944 safe_call1 (fn, pos);
2947 UNGCPRO;
2950 unbind_to (count, Qnil);
2952 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2953 something. This avoids an endless loop if they failed to
2954 fontify the text for which reason ever. */
2955 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2956 handled = HANDLED_RECOMPUTE_PROPS;
2959 return handled;
2964 /***********************************************************************
2965 Faces
2966 ***********************************************************************/
2968 /* Set up iterator IT from face properties at its current position.
2969 Called from handle_stop. */
2971 static enum prop_handled
2972 handle_face_prop (it)
2973 struct it *it;
2975 int new_face_id, next_stop;
2977 if (!STRINGP (it->string))
2979 new_face_id
2980 = face_at_buffer_position (it->w,
2981 IT_CHARPOS (*it),
2982 it->region_beg_charpos,
2983 it->region_end_charpos,
2984 &next_stop,
2985 (IT_CHARPOS (*it)
2986 + TEXT_PROP_DISTANCE_LIMIT),
2989 /* Is this a start of a run of characters with box face?
2990 Caveat: this can be called for a freshly initialized
2991 iterator; face_id is -1 in this case. We know that the new
2992 face will not change until limit, i.e. if the new face has a
2993 box, all characters up to limit will have one. But, as
2994 usual, we don't know whether limit is really the end. */
2995 if (new_face_id != it->face_id)
2997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2999 /* If new face has a box but old face has not, this is
3000 the start of a run of characters with box, i.e. it has
3001 a shadow on the left side. The value of face_id of the
3002 iterator will be -1 if this is the initial call that gets
3003 the face. In this case, we have to look in front of IT's
3004 position and see whether there is a face != new_face_id. */
3005 it->start_of_box_run_p
3006 = (new_face->box != FACE_NO_BOX
3007 && (it->face_id >= 0
3008 || IT_CHARPOS (*it) == BEG
3009 || new_face_id != face_before_it_pos (it)));
3010 it->face_box_p = new_face->box != FACE_NO_BOX;
3013 else
3015 int base_face_id, bufpos;
3017 if (it->current.overlay_string_index >= 0)
3018 bufpos = IT_CHARPOS (*it);
3019 else
3020 bufpos = 0;
3022 /* For strings from a buffer, i.e. overlay strings or strings
3023 from a `display' property, use the face at IT's current
3024 buffer position as the base face to merge with, so that
3025 overlay strings appear in the same face as surrounding
3026 text, unless they specify their own faces. */
3027 base_face_id = underlying_face_id (it);
3029 new_face_id = face_at_string_position (it->w,
3030 it->string,
3031 IT_STRING_CHARPOS (*it),
3032 bufpos,
3033 it->region_beg_charpos,
3034 it->region_end_charpos,
3035 &next_stop,
3036 base_face_id, 0);
3038 #if 0 /* This shouldn't be neccessary. Let's check it. */
3039 /* If IT is used to display a mode line we would really like to
3040 use the mode line face instead of the frame's default face. */
3041 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3042 && new_face_id == DEFAULT_FACE_ID)
3043 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3044 #endif
3046 /* Is this a start of a run of characters with box? Caveat:
3047 this can be called for a freshly allocated iterator; face_id
3048 is -1 is this case. We know that the new face will not
3049 change until the next check pos, i.e. if the new face has a
3050 box, all characters up to that position will have a
3051 box. But, as usual, we don't know whether that position
3052 is really the end. */
3053 if (new_face_id != it->face_id)
3055 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3056 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3058 /* If new face has a box but old face hasn't, this is the
3059 start of a run of characters with box, i.e. it has a
3060 shadow on the left side. */
3061 it->start_of_box_run_p
3062 = new_face->box && (old_face == NULL || !old_face->box);
3063 it->face_box_p = new_face->box != FACE_NO_BOX;
3067 it->face_id = new_face_id;
3068 return HANDLED_NORMALLY;
3072 /* Return the ID of the face ``underlying'' IT's current position,
3073 which is in a string. If the iterator is associated with a
3074 buffer, return the face at IT's current buffer position.
3075 Otherwise, use the iterator's base_face_id. */
3077 static int
3078 underlying_face_id (it)
3079 struct it *it;
3081 int face_id = it->base_face_id, i;
3083 xassert (STRINGP (it->string));
3085 for (i = it->sp - 1; i >= 0; --i)
3086 if (NILP (it->stack[i].string))
3087 face_id = it->stack[i].face_id;
3089 return face_id;
3093 /* Compute the face one character before or after the current position
3094 of IT. BEFORE_P non-zero means get the face in front of IT's
3095 position. Value is the id of the face. */
3097 static int
3098 face_before_or_after_it_pos (it, before_p)
3099 struct it *it;
3100 int before_p;
3102 int face_id, limit;
3103 int next_check_charpos;
3104 struct text_pos pos;
3106 xassert (it->s == NULL);
3108 if (STRINGP (it->string))
3110 int bufpos, base_face_id;
3112 /* No face change past the end of the string (for the case
3113 we are padding with spaces). No face change before the
3114 string start. */
3115 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3116 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3117 return it->face_id;
3119 /* Set pos to the position before or after IT's current position. */
3120 if (before_p)
3121 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3122 else
3123 /* For composition, we must check the character after the
3124 composition. */
3125 pos = (it->what == IT_COMPOSITION
3126 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3127 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3129 if (it->current.overlay_string_index >= 0)
3130 bufpos = IT_CHARPOS (*it);
3131 else
3132 bufpos = 0;
3134 base_face_id = underlying_face_id (it);
3136 /* Get the face for ASCII, or unibyte. */
3137 face_id = face_at_string_position (it->w,
3138 it->string,
3139 CHARPOS (pos),
3140 bufpos,
3141 it->region_beg_charpos,
3142 it->region_end_charpos,
3143 &next_check_charpos,
3144 base_face_id, 0);
3146 /* Correct the face for charsets different from ASCII. Do it
3147 for the multibyte case only. The face returned above is
3148 suitable for unibyte text if IT->string is unibyte. */
3149 if (STRING_MULTIBYTE (it->string))
3151 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3152 int rest = SBYTES (it->string) - BYTEPOS (pos);
3153 int c, len;
3154 struct face *face = FACE_FROM_ID (it->f, face_id);
3156 c = string_char_and_length (p, rest, &len);
3157 face_id = FACE_FOR_CHAR (it->f, face, c);
3160 else
3162 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3163 || (IT_CHARPOS (*it) <= BEGV && before_p))
3164 return it->face_id;
3166 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3167 pos = it->current.pos;
3169 if (before_p)
3170 DEC_TEXT_POS (pos, it->multibyte_p);
3171 else
3173 if (it->what == IT_COMPOSITION)
3174 /* For composition, we must check the position after the
3175 composition. */
3176 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3177 else
3178 INC_TEXT_POS (pos, it->multibyte_p);
3181 /* Determine face for CHARSET_ASCII, or unibyte. */
3182 face_id = face_at_buffer_position (it->w,
3183 CHARPOS (pos),
3184 it->region_beg_charpos,
3185 it->region_end_charpos,
3186 &next_check_charpos,
3187 limit, 0);
3189 /* Correct the face for charsets different from ASCII. Do it
3190 for the multibyte case only. The face returned above is
3191 suitable for unibyte text if current_buffer is unibyte. */
3192 if (it->multibyte_p)
3194 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3195 struct face *face = FACE_FROM_ID (it->f, face_id);
3196 face_id = FACE_FOR_CHAR (it->f, face, c);
3200 return face_id;
3205 /***********************************************************************
3206 Invisible text
3207 ***********************************************************************/
3209 /* Set up iterator IT from invisible properties at its current
3210 position. Called from handle_stop. */
3212 static enum prop_handled
3213 handle_invisible_prop (it)
3214 struct it *it;
3216 enum prop_handled handled = HANDLED_NORMALLY;
3218 if (STRINGP (it->string))
3220 extern Lisp_Object Qinvisible;
3221 Lisp_Object prop, end_charpos, limit, charpos;
3223 /* Get the value of the invisible text property at the
3224 current position. Value will be nil if there is no such
3225 property. */
3226 charpos = make_number (IT_STRING_CHARPOS (*it));
3227 prop = Fget_text_property (charpos, Qinvisible, it->string);
3229 if (!NILP (prop)
3230 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3232 handled = HANDLED_RECOMPUTE_PROPS;
3234 /* Get the position at which the next change of the
3235 invisible text property can be found in IT->string.
3236 Value will be nil if the property value is the same for
3237 all the rest of IT->string. */
3238 XSETINT (limit, SCHARS (it->string));
3239 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3240 it->string, limit);
3242 /* Text at current position is invisible. The next
3243 change in the property is at position end_charpos.
3244 Move IT's current position to that position. */
3245 if (INTEGERP (end_charpos)
3246 && XFASTINT (end_charpos) < XFASTINT (limit))
3248 struct text_pos old;
3249 old = it->current.string_pos;
3250 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3251 compute_string_pos (&it->current.string_pos, old, it->string);
3253 else
3255 /* The rest of the string is invisible. If this is an
3256 overlay string, proceed with the next overlay string
3257 or whatever comes and return a character from there. */
3258 if (it->current.overlay_string_index >= 0)
3260 next_overlay_string (it);
3261 /* Don't check for overlay strings when we just
3262 finished processing them. */
3263 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3265 else
3267 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3268 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3273 else
3275 int invis_p, newpos, next_stop, start_charpos;
3276 Lisp_Object pos, prop, overlay;
3278 /* First of all, is there invisible text at this position? */
3279 start_charpos = IT_CHARPOS (*it);
3280 pos = make_number (IT_CHARPOS (*it));
3281 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3282 &overlay);
3283 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3285 /* If we are on invisible text, skip over it. */
3286 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3288 /* Record whether we have to display an ellipsis for the
3289 invisible text. */
3290 int display_ellipsis_p = invis_p == 2;
3292 handled = HANDLED_RECOMPUTE_PROPS;
3294 /* Loop skipping over invisible text. The loop is left at
3295 ZV or with IT on the first char being visible again. */
3298 /* Try to skip some invisible text. Return value is the
3299 position reached which can be equal to IT's position
3300 if there is nothing invisible here. This skips both
3301 over invisible text properties and overlays with
3302 invisible property. */
3303 newpos = skip_invisible (IT_CHARPOS (*it),
3304 &next_stop, ZV, it->window);
3306 /* If we skipped nothing at all we weren't at invisible
3307 text in the first place. If everything to the end of
3308 the buffer was skipped, end the loop. */
3309 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3310 invis_p = 0;
3311 else
3313 /* We skipped some characters but not necessarily
3314 all there are. Check if we ended up on visible
3315 text. Fget_char_property returns the property of
3316 the char before the given position, i.e. if we
3317 get invis_p = 0, this means that the char at
3318 newpos is visible. */
3319 pos = make_number (newpos);
3320 prop = Fget_char_property (pos, Qinvisible, it->window);
3321 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3324 /* If we ended up on invisible text, proceed to
3325 skip starting with next_stop. */
3326 if (invis_p)
3327 IT_CHARPOS (*it) = next_stop;
3329 while (invis_p);
3331 /* The position newpos is now either ZV or on visible text. */
3332 IT_CHARPOS (*it) = newpos;
3333 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3335 /* If there are before-strings at the start of invisible
3336 text, and the text is invisible because of a text
3337 property, arrange to show before-strings because 20.x did
3338 it that way. (If the text is invisible because of an
3339 overlay property instead of a text property, this is
3340 already handled in the overlay code.) */
3341 if (NILP (overlay)
3342 && get_overlay_strings (it, start_charpos))
3344 handled = HANDLED_RECOMPUTE_PROPS;
3345 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3347 else if (display_ellipsis_p)
3348 setup_for_ellipsis (it, 0);
3352 return handled;
3356 /* Make iterator IT return `...' next.
3357 Replaces LEN characters from buffer. */
3359 static void
3360 setup_for_ellipsis (it, len)
3361 struct it *it;
3362 int len;
3364 /* Use the display table definition for `...'. Invalid glyphs
3365 will be handled by the method returning elements from dpvec. */
3366 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3368 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3369 it->dpvec = v->contents;
3370 it->dpend = v->contents + v->size;
3372 else
3374 /* Default `...'. */
3375 it->dpvec = default_invis_vector;
3376 it->dpend = default_invis_vector + 3;
3379 it->dpvec_char_len = len;
3380 it->current.dpvec_index = 0;
3381 it->dpvec_face_id = -1;
3383 /* Remember the current face id in case glyphs specify faces.
3384 IT's face is restored in set_iterator_to_next.
3385 saved_face_id was set to preceding char's face in handle_stop. */
3386 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3387 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3389 it->method = GET_FROM_DISPLAY_VECTOR;
3390 it->ellipsis_p = 1;
3395 /***********************************************************************
3396 'display' property
3397 ***********************************************************************/
3399 /* Set up iterator IT from `display' property at its current position.
3400 Called from handle_stop.
3401 We return HANDLED_RETURN if some part of the display property
3402 overrides the display of the buffer text itself.
3403 Otherwise we return HANDLED_NORMALLY. */
3405 static enum prop_handled
3406 handle_display_prop (it)
3407 struct it *it;
3409 Lisp_Object prop, object;
3410 struct text_pos *position;
3411 /* Nonzero if some property replaces the display of the text itself. */
3412 int display_replaced_p = 0;
3414 if (STRINGP (it->string))
3416 object = it->string;
3417 position = &it->current.string_pos;
3419 else
3421 object = it->w->buffer;
3422 position = &it->current.pos;
3425 /* Reset those iterator values set from display property values. */
3426 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3427 it->space_width = Qnil;
3428 it->font_height = Qnil;
3429 it->voffset = 0;
3431 /* We don't support recursive `display' properties, i.e. string
3432 values that have a string `display' property, that have a string
3433 `display' property etc. */
3434 if (!it->string_from_display_prop_p)
3435 it->area = TEXT_AREA;
3437 prop = Fget_char_property (make_number (position->charpos),
3438 Qdisplay, object);
3439 if (NILP (prop))
3440 return HANDLED_NORMALLY;
3442 if (CONSP (prop)
3443 /* Simple properties. */
3444 && !EQ (XCAR (prop), Qimage)
3445 && !EQ (XCAR (prop), Qspace)
3446 && !EQ (XCAR (prop), Qwhen)
3447 && !EQ (XCAR (prop), Qslice)
3448 && !EQ (XCAR (prop), Qspace_width)
3449 && !EQ (XCAR (prop), Qheight)
3450 && !EQ (XCAR (prop), Qraise)
3451 /* Marginal area specifications. */
3452 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3453 && !EQ (XCAR (prop), Qleft_fringe)
3454 && !EQ (XCAR (prop), Qright_fringe)
3455 && !NILP (XCAR (prop)))
3457 for (; CONSP (prop); prop = XCDR (prop))
3459 if (handle_single_display_spec (it, XCAR (prop), object,
3460 position, display_replaced_p))
3461 display_replaced_p = 1;
3464 else if (VECTORP (prop))
3466 int i;
3467 for (i = 0; i < ASIZE (prop); ++i)
3468 if (handle_single_display_spec (it, AREF (prop, i), object,
3469 position, display_replaced_p))
3470 display_replaced_p = 1;
3472 else
3474 if (handle_single_display_spec (it, prop, object, position, 0))
3475 display_replaced_p = 1;
3478 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3482 /* Value is the position of the end of the `display' property starting
3483 at START_POS in OBJECT. */
3485 static struct text_pos
3486 display_prop_end (it, object, start_pos)
3487 struct it *it;
3488 Lisp_Object object;
3489 struct text_pos start_pos;
3491 Lisp_Object end;
3492 struct text_pos end_pos;
3494 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3495 Qdisplay, object, Qnil);
3496 CHARPOS (end_pos) = XFASTINT (end);
3497 if (STRINGP (object))
3498 compute_string_pos (&end_pos, start_pos, it->string);
3499 else
3500 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3502 return end_pos;
3506 /* Set up IT from a single `display' specification PROP. OBJECT
3507 is the object in which the `display' property was found. *POSITION
3508 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3509 means that we previously saw a display specification which already
3510 replaced text display with something else, for example an image;
3511 we ignore such properties after the first one has been processed.
3513 If PROP is a `space' or `image' specification, and in some other
3514 cases too, set *POSITION to the position where the `display'
3515 property ends.
3517 Value is non-zero if something was found which replaces the display
3518 of buffer or string text. */
3520 static int
3521 handle_single_display_spec (it, spec, object, position,
3522 display_replaced_before_p)
3523 struct it *it;
3524 Lisp_Object spec;
3525 Lisp_Object object;
3526 struct text_pos *position;
3527 int display_replaced_before_p;
3529 Lisp_Object form;
3530 Lisp_Object location, value;
3531 struct text_pos start_pos;
3532 int valid_p;
3534 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3535 If the result is non-nil, use VALUE instead of SPEC. */
3536 form = Qt;
3537 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3539 spec = XCDR (spec);
3540 if (!CONSP (spec))
3541 return 0;
3542 form = XCAR (spec);
3543 spec = XCDR (spec);
3546 if (!NILP (form) && !EQ (form, Qt))
3548 int count = SPECPDL_INDEX ();
3549 struct gcpro gcpro1;
3551 /* Bind `object' to the object having the `display' property, a
3552 buffer or string. Bind `position' to the position in the
3553 object where the property was found, and `buffer-position'
3554 to the current position in the buffer. */
3555 specbind (Qobject, object);
3556 specbind (Qposition, make_number (CHARPOS (*position)));
3557 specbind (Qbuffer_position,
3558 make_number (STRINGP (object)
3559 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3560 GCPRO1 (form);
3561 form = safe_eval (form);
3562 UNGCPRO;
3563 unbind_to (count, Qnil);
3566 if (NILP (form))
3567 return 0;
3569 /* Handle `(height HEIGHT)' specifications. */
3570 if (CONSP (spec)
3571 && EQ (XCAR (spec), Qheight)
3572 && CONSP (XCDR (spec)))
3574 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3575 return 0;
3577 it->font_height = XCAR (XCDR (spec));
3578 if (!NILP (it->font_height))
3580 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3581 int new_height = -1;
3583 if (CONSP (it->font_height)
3584 && (EQ (XCAR (it->font_height), Qplus)
3585 || EQ (XCAR (it->font_height), Qminus))
3586 && CONSP (XCDR (it->font_height))
3587 && INTEGERP (XCAR (XCDR (it->font_height))))
3589 /* `(+ N)' or `(- N)' where N is an integer. */
3590 int steps = XINT (XCAR (XCDR (it->font_height)));
3591 if (EQ (XCAR (it->font_height), Qplus))
3592 steps = - steps;
3593 it->face_id = smaller_face (it->f, it->face_id, steps);
3595 else if (FUNCTIONP (it->font_height))
3597 /* Call function with current height as argument.
3598 Value is the new height. */
3599 Lisp_Object height;
3600 height = safe_call1 (it->font_height,
3601 face->lface[LFACE_HEIGHT_INDEX]);
3602 if (NUMBERP (height))
3603 new_height = XFLOATINT (height);
3605 else if (NUMBERP (it->font_height))
3607 /* Value is a multiple of the canonical char height. */
3608 struct face *face;
3610 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3611 new_height = (XFLOATINT (it->font_height)
3612 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3614 else
3616 /* Evaluate IT->font_height with `height' bound to the
3617 current specified height to get the new height. */
3618 int count = SPECPDL_INDEX ();
3620 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3621 value = safe_eval (it->font_height);
3622 unbind_to (count, Qnil);
3624 if (NUMBERP (value))
3625 new_height = XFLOATINT (value);
3628 if (new_height > 0)
3629 it->face_id = face_with_height (it->f, it->face_id, new_height);
3632 return 0;
3635 /* Handle `(space_width WIDTH)'. */
3636 if (CONSP (spec)
3637 && EQ (XCAR (spec), Qspace_width)
3638 && CONSP (XCDR (spec)))
3640 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3641 return 0;
3643 value = XCAR (XCDR (spec));
3644 if (NUMBERP (value) && XFLOATINT (value) > 0)
3645 it->space_width = value;
3647 return 0;
3650 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3651 if (CONSP (spec)
3652 && EQ (XCAR (spec), Qslice))
3654 Lisp_Object tem;
3656 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3657 return 0;
3659 if (tem = XCDR (spec), CONSP (tem))
3661 it->slice.x = XCAR (tem);
3662 if (tem = XCDR (tem), CONSP (tem))
3664 it->slice.y = XCAR (tem);
3665 if (tem = XCDR (tem), CONSP (tem))
3667 it->slice.width = XCAR (tem);
3668 if (tem = XCDR (tem), CONSP (tem))
3669 it->slice.height = XCAR (tem);
3674 return 0;
3677 /* Handle `(raise FACTOR)'. */
3678 if (CONSP (spec)
3679 && EQ (XCAR (spec), Qraise)
3680 && CONSP (XCDR (spec)))
3682 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3683 return 0;
3685 #ifdef HAVE_WINDOW_SYSTEM
3686 value = XCAR (XCDR (spec));
3687 if (NUMBERP (value))
3689 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3690 it->voffset = - (XFLOATINT (value)
3691 * (FONT_HEIGHT (face->font)));
3693 #endif /* HAVE_WINDOW_SYSTEM */
3695 return 0;
3698 /* Don't handle the other kinds of display specifications
3699 inside a string that we got from a `display' property. */
3700 if (it->string_from_display_prop_p)
3701 return 0;
3703 /* Characters having this form of property are not displayed, so
3704 we have to find the end of the property. */
3705 start_pos = *position;
3706 *position = display_prop_end (it, object, start_pos);
3707 value = Qnil;
3709 /* Stop the scan at that end position--we assume that all
3710 text properties change there. */
3711 it->stop_charpos = position->charpos;
3713 /* Handle `(left-fringe BITMAP [FACE])'
3714 and `(right-fringe BITMAP [FACE])'. */
3715 if (CONSP (spec)
3716 && (EQ (XCAR (spec), Qleft_fringe)
3717 || EQ (XCAR (spec), Qright_fringe))
3718 && CONSP (XCDR (spec)))
3720 int face_id = DEFAULT_FACE_ID;
3721 int fringe_bitmap;
3723 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3724 /* If we return here, POSITION has been advanced
3725 across the text with this property. */
3726 return 0;
3728 #ifdef HAVE_WINDOW_SYSTEM
3729 value = XCAR (XCDR (spec));
3730 if (!SYMBOLP (value)
3731 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3732 /* If we return here, POSITION has been advanced
3733 across the text with this property. */
3734 return 0;
3736 if (CONSP (XCDR (XCDR (spec))))
3738 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3739 int face_id2 = lookup_derived_face (it->f, face_name,
3740 'A', FRINGE_FACE_ID, 0);
3741 if (face_id2 >= 0)
3742 face_id = face_id2;
3745 /* Save current settings of IT so that we can restore them
3746 when we are finished with the glyph property value. */
3748 push_it (it);
3750 it->area = TEXT_AREA;
3751 it->what = IT_IMAGE;
3752 it->image_id = -1; /* no image */
3753 it->position = start_pos;
3754 it->object = NILP (object) ? it->w->buffer : object;
3755 it->method = GET_FROM_IMAGE;
3756 it->face_id = face_id;
3758 /* Say that we haven't consumed the characters with
3759 `display' property yet. The call to pop_it in
3760 set_iterator_to_next will clean this up. */
3761 *position = start_pos;
3763 if (EQ (XCAR (spec), Qleft_fringe))
3765 it->left_user_fringe_bitmap = fringe_bitmap;
3766 it->left_user_fringe_face_id = face_id;
3768 else
3770 it->right_user_fringe_bitmap = fringe_bitmap;
3771 it->right_user_fringe_face_id = face_id;
3773 #endif /* HAVE_WINDOW_SYSTEM */
3774 return 1;
3777 /* Prepare to handle `((margin left-margin) ...)',
3778 `((margin right-margin) ...)' and `((margin nil) ...)'
3779 prefixes for display specifications. */
3780 location = Qunbound;
3781 if (CONSP (spec) && CONSP (XCAR (spec)))
3783 Lisp_Object tem;
3785 value = XCDR (spec);
3786 if (CONSP (value))
3787 value = XCAR (value);
3789 tem = XCAR (spec);
3790 if (EQ (XCAR (tem), Qmargin)
3791 && (tem = XCDR (tem),
3792 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3793 (NILP (tem)
3794 || EQ (tem, Qleft_margin)
3795 || EQ (tem, Qright_margin))))
3796 location = tem;
3799 if (EQ (location, Qunbound))
3801 location = Qnil;
3802 value = spec;
3805 /* After this point, VALUE is the property after any
3806 margin prefix has been stripped. It must be a string,
3807 an image specification, or `(space ...)'.
3809 LOCATION specifies where to display: `left-margin',
3810 `right-margin' or nil. */
3812 valid_p = (STRINGP (value)
3813 #ifdef HAVE_WINDOW_SYSTEM
3814 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3815 #endif /* not HAVE_WINDOW_SYSTEM */
3816 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3818 if (valid_p && !display_replaced_before_p)
3820 /* Save current settings of IT so that we can restore them
3821 when we are finished with the glyph property value. */
3822 push_it (it);
3824 if (NILP (location))
3825 it->area = TEXT_AREA;
3826 else if (EQ (location, Qleft_margin))
3827 it->area = LEFT_MARGIN_AREA;
3828 else
3829 it->area = RIGHT_MARGIN_AREA;
3831 if (STRINGP (value))
3833 it->string = value;
3834 it->multibyte_p = STRING_MULTIBYTE (it->string);
3835 it->current.overlay_string_index = -1;
3836 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3837 it->end_charpos = it->string_nchars = SCHARS (it->string);
3838 it->method = GET_FROM_STRING;
3839 it->stop_charpos = 0;
3840 it->string_from_display_prop_p = 1;
3841 /* Say that we haven't consumed the characters with
3842 `display' property yet. The call to pop_it in
3843 set_iterator_to_next will clean this up. */
3844 *position = start_pos;
3846 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3848 it->method = GET_FROM_STRETCH;
3849 it->object = value;
3850 it->current.pos = it->position = start_pos;
3852 #ifdef HAVE_WINDOW_SYSTEM
3853 else
3855 it->what = IT_IMAGE;
3856 it->image_id = lookup_image (it->f, value);
3857 it->position = start_pos;
3858 it->object = NILP (object) ? it->w->buffer : object;
3859 it->method = GET_FROM_IMAGE;
3861 /* Say that we haven't consumed the characters with
3862 `display' property yet. The call to pop_it in
3863 set_iterator_to_next will clean this up. */
3864 *position = start_pos;
3866 #endif /* HAVE_WINDOW_SYSTEM */
3868 return 1;
3871 /* Invalid property or property not supported. Restore
3872 POSITION to what it was before. */
3873 *position = start_pos;
3874 return 0;
3878 /* Check if SPEC is a display specification value whose text should be
3879 treated as intangible. */
3881 static int
3882 single_display_spec_intangible_p (prop)
3883 Lisp_Object prop;
3885 /* Skip over `when FORM'. */
3886 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3888 prop = XCDR (prop);
3889 if (!CONSP (prop))
3890 return 0;
3891 prop = XCDR (prop);
3894 if (STRINGP (prop))
3895 return 1;
3897 if (!CONSP (prop))
3898 return 0;
3900 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3901 we don't need to treat text as intangible. */
3902 if (EQ (XCAR (prop), Qmargin))
3904 prop = XCDR (prop);
3905 if (!CONSP (prop))
3906 return 0;
3908 prop = XCDR (prop);
3909 if (!CONSP (prop)
3910 || EQ (XCAR (prop), Qleft_margin)
3911 || EQ (XCAR (prop), Qright_margin))
3912 return 0;
3915 return (CONSP (prop)
3916 && (EQ (XCAR (prop), Qimage)
3917 || EQ (XCAR (prop), Qspace)));
3921 /* Check if PROP is a display property value whose text should be
3922 treated as intangible. */
3925 display_prop_intangible_p (prop)
3926 Lisp_Object prop;
3928 if (CONSP (prop)
3929 && CONSP (XCAR (prop))
3930 && !EQ (Qmargin, XCAR (XCAR (prop))))
3932 /* A list of sub-properties. */
3933 while (CONSP (prop))
3935 if (single_display_spec_intangible_p (XCAR (prop)))
3936 return 1;
3937 prop = XCDR (prop);
3940 else if (VECTORP (prop))
3942 /* A vector of sub-properties. */
3943 int i;
3944 for (i = 0; i < ASIZE (prop); ++i)
3945 if (single_display_spec_intangible_p (AREF (prop, i)))
3946 return 1;
3948 else
3949 return single_display_spec_intangible_p (prop);
3951 return 0;
3955 /* Return 1 if PROP is a display sub-property value containing STRING. */
3957 static int
3958 single_display_spec_string_p (prop, string)
3959 Lisp_Object prop, string;
3961 if (EQ (string, prop))
3962 return 1;
3964 /* Skip over `when FORM'. */
3965 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3967 prop = XCDR (prop);
3968 if (!CONSP (prop))
3969 return 0;
3970 prop = XCDR (prop);
3973 if (CONSP (prop))
3974 /* Skip over `margin LOCATION'. */
3975 if (EQ (XCAR (prop), Qmargin))
3977 prop = XCDR (prop);
3978 if (!CONSP (prop))
3979 return 0;
3981 prop = XCDR (prop);
3982 if (!CONSP (prop))
3983 return 0;
3986 return CONSP (prop) && EQ (XCAR (prop), string);
3990 /* Return 1 if STRING appears in the `display' property PROP. */
3992 static int
3993 display_prop_string_p (prop, string)
3994 Lisp_Object prop, string;
3996 if (CONSP (prop)
3997 && CONSP (XCAR (prop))
3998 && !EQ (Qmargin, XCAR (XCAR (prop))))
4000 /* A list of sub-properties. */
4001 while (CONSP (prop))
4003 if (single_display_spec_string_p (XCAR (prop), string))
4004 return 1;
4005 prop = XCDR (prop);
4008 else if (VECTORP (prop))
4010 /* A vector of sub-properties. */
4011 int i;
4012 for (i = 0; i < ASIZE (prop); ++i)
4013 if (single_display_spec_string_p (AREF (prop, i), string))
4014 return 1;
4016 else
4017 return single_display_spec_string_p (prop, string);
4019 return 0;
4023 /* Determine from which buffer position in W's buffer STRING comes
4024 from. AROUND_CHARPOS is an approximate position where it could
4025 be from. Value is the buffer position or 0 if it couldn't be
4026 determined.
4028 W's buffer must be current.
4030 This function is necessary because we don't record buffer positions
4031 in glyphs generated from strings (to keep struct glyph small).
4032 This function may only use code that doesn't eval because it is
4033 called asynchronously from note_mouse_highlight. */
4036 string_buffer_position (w, string, around_charpos)
4037 struct window *w;
4038 Lisp_Object string;
4039 int around_charpos;
4041 Lisp_Object limit, prop, pos;
4042 const int MAX_DISTANCE = 1000;
4043 int found = 0;
4045 pos = make_number (around_charpos);
4046 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
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 = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4056 if (!found)
4058 pos = make_number (around_charpos);
4059 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4060 while (!found && !EQ (pos, limit))
4062 prop = Fget_char_property (pos, Qdisplay, Qnil);
4063 if (!NILP (prop) && display_prop_string_p (prop, string))
4064 found = 1;
4065 else
4066 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4067 limit);
4071 return found ? XINT (pos) : 0;
4076 /***********************************************************************
4077 `composition' property
4078 ***********************************************************************/
4080 /* Set up iterator IT from `composition' property at its current
4081 position. Called from handle_stop. */
4083 static enum prop_handled
4084 handle_composition_prop (it)
4085 struct it *it;
4087 Lisp_Object prop, string;
4088 int pos, pos_byte, end;
4089 enum prop_handled handled = HANDLED_NORMALLY;
4091 if (STRINGP (it->string))
4093 pos = IT_STRING_CHARPOS (*it);
4094 pos_byte = IT_STRING_BYTEPOS (*it);
4095 string = it->string;
4097 else
4099 pos = IT_CHARPOS (*it);
4100 pos_byte = IT_BYTEPOS (*it);
4101 string = Qnil;
4104 /* If there's a valid composition and point is not inside of the
4105 composition (in the case that the composition is from the current
4106 buffer), draw a glyph composed from the composition components. */
4107 if (find_composition (pos, -1, &pos, &end, &prop, string)
4108 && COMPOSITION_VALID_P (pos, end, prop)
4109 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4111 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4113 if (id >= 0)
4115 it->method = GET_FROM_COMPOSITION;
4116 it->cmp_id = id;
4117 it->cmp_len = COMPOSITION_LENGTH (prop);
4118 /* For a terminal, draw only the first character of the
4119 components. */
4120 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4121 it->len = (STRINGP (it->string)
4122 ? string_char_to_byte (it->string, end)
4123 : CHAR_TO_BYTE (end)) - pos_byte;
4124 it->stop_charpos = end;
4125 handled = HANDLED_RETURN;
4129 return handled;
4134 /***********************************************************************
4135 Overlay strings
4136 ***********************************************************************/
4138 /* The following structure is used to record overlay strings for
4139 later sorting in load_overlay_strings. */
4141 struct overlay_entry
4143 Lisp_Object overlay;
4144 Lisp_Object string;
4145 int priority;
4146 int after_string_p;
4150 /* Set up iterator IT from overlay strings at its current position.
4151 Called from handle_stop. */
4153 static enum prop_handled
4154 handle_overlay_change (it)
4155 struct it *it;
4157 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4158 return HANDLED_RECOMPUTE_PROPS;
4159 else
4160 return HANDLED_NORMALLY;
4164 /* Set up the next overlay string for delivery by IT, if there is an
4165 overlay string to deliver. Called by set_iterator_to_next when the
4166 end of the current overlay string is reached. If there are more
4167 overlay strings to display, IT->string and
4168 IT->current.overlay_string_index are set appropriately here.
4169 Otherwise IT->string is set to nil. */
4171 static void
4172 next_overlay_string (it)
4173 struct it *it;
4175 ++it->current.overlay_string_index;
4176 if (it->current.overlay_string_index == it->n_overlay_strings)
4178 /* No more overlay strings. Restore IT's settings to what
4179 they were before overlay strings were processed, and
4180 continue to deliver from current_buffer. */
4181 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4183 pop_it (it);
4184 xassert (it->stop_charpos >= BEGV
4185 && it->stop_charpos <= it->end_charpos);
4186 it->string = Qnil;
4187 it->current.overlay_string_index = -1;
4188 SET_TEXT_POS (it->current.string_pos, -1, -1);
4189 it->n_overlay_strings = 0;
4190 it->method = GET_FROM_BUFFER;
4192 /* If we're at the end of the buffer, record that we have
4193 processed the overlay strings there already, so that
4194 next_element_from_buffer doesn't try it again. */
4195 if (IT_CHARPOS (*it) >= it->end_charpos)
4196 it->overlay_strings_at_end_processed_p = 1;
4198 /* If we have to display `...' for invisible text, set
4199 the iterator up for that. */
4200 if (display_ellipsis_p)
4201 setup_for_ellipsis (it, 0);
4203 else
4205 /* There are more overlay strings to process. If
4206 IT->current.overlay_string_index has advanced to a position
4207 where we must load IT->overlay_strings with more strings, do
4208 it. */
4209 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4211 if (it->current.overlay_string_index && i == 0)
4212 load_overlay_strings (it, 0);
4214 /* Initialize IT to deliver display elements from the overlay
4215 string. */
4216 it->string = it->overlay_strings[i];
4217 it->multibyte_p = STRING_MULTIBYTE (it->string);
4218 SET_TEXT_POS (it->current.string_pos, 0, 0);
4219 it->method = GET_FROM_STRING;
4220 it->stop_charpos = 0;
4223 CHECK_IT (it);
4227 /* Compare two overlay_entry structures E1 and E2. Used as a
4228 comparison function for qsort in load_overlay_strings. Overlay
4229 strings for the same position are sorted so that
4231 1. All after-strings come in front of before-strings, except
4232 when they come from the same overlay.
4234 2. Within after-strings, strings are sorted so that overlay strings
4235 from overlays with higher priorities come first.
4237 2. Within before-strings, strings are sorted so that overlay
4238 strings from overlays with higher priorities come last.
4240 Value is analogous to strcmp. */
4243 static int
4244 compare_overlay_entries (e1, e2)
4245 void *e1, *e2;
4247 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4248 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4249 int result;
4251 if (entry1->after_string_p != entry2->after_string_p)
4253 /* Let after-strings appear in front of before-strings if
4254 they come from different overlays. */
4255 if (EQ (entry1->overlay, entry2->overlay))
4256 result = entry1->after_string_p ? 1 : -1;
4257 else
4258 result = entry1->after_string_p ? -1 : 1;
4260 else if (entry1->after_string_p)
4261 /* After-strings sorted in order of decreasing priority. */
4262 result = entry2->priority - entry1->priority;
4263 else
4264 /* Before-strings sorted in order of increasing priority. */
4265 result = entry1->priority - entry2->priority;
4267 return result;
4271 /* Load the vector IT->overlay_strings with overlay strings from IT's
4272 current buffer position, or from CHARPOS if that is > 0. Set
4273 IT->n_overlays to the total number of overlay strings found.
4275 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4276 a time. On entry into load_overlay_strings,
4277 IT->current.overlay_string_index gives the number of overlay
4278 strings that have already been loaded by previous calls to this
4279 function.
4281 IT->add_overlay_start contains an additional overlay start
4282 position to consider for taking overlay strings from, if non-zero.
4283 This position comes into play when the overlay has an `invisible'
4284 property, and both before and after-strings. When we've skipped to
4285 the end of the overlay, because of its `invisible' property, we
4286 nevertheless want its before-string to appear.
4287 IT->add_overlay_start will contain the overlay start position
4288 in this case.
4290 Overlay strings are sorted so that after-string strings come in
4291 front of before-string strings. Within before and after-strings,
4292 strings are sorted by overlay priority. See also function
4293 compare_overlay_entries. */
4295 static void
4296 load_overlay_strings (it, charpos)
4297 struct it *it;
4298 int charpos;
4300 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4301 Lisp_Object overlay, window, str, invisible;
4302 struct Lisp_Overlay *ov;
4303 int start, end;
4304 int size = 20;
4305 int n = 0, i, j, invis_p;
4306 struct overlay_entry *entries
4307 = (struct overlay_entry *) alloca (size * sizeof *entries);
4309 if (charpos <= 0)
4310 charpos = IT_CHARPOS (*it);
4312 /* Append the overlay string STRING of overlay OVERLAY to vector
4313 `entries' which has size `size' and currently contains `n'
4314 elements. AFTER_P non-zero means STRING is an after-string of
4315 OVERLAY. */
4316 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4317 do \
4319 Lisp_Object priority; \
4321 if (n == size) \
4323 int new_size = 2 * size; \
4324 struct overlay_entry *old = entries; \
4325 entries = \
4326 (struct overlay_entry *) alloca (new_size \
4327 * sizeof *entries); \
4328 bcopy (old, entries, size * sizeof *entries); \
4329 size = new_size; \
4332 entries[n].string = (STRING); \
4333 entries[n].overlay = (OVERLAY); \
4334 priority = Foverlay_get ((OVERLAY), Qpriority); \
4335 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4336 entries[n].after_string_p = (AFTER_P); \
4337 ++n; \
4339 while (0)
4341 /* Process overlay before the overlay center. */
4342 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4344 XSETMISC (overlay, ov);
4345 xassert (OVERLAYP (overlay));
4346 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4347 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4349 if (end < charpos)
4350 break;
4352 /* Skip this overlay if it doesn't start or end at IT's current
4353 position. */
4354 if (end != charpos && start != charpos)
4355 continue;
4357 /* Skip this overlay if it doesn't apply to IT->w. */
4358 window = Foverlay_get (overlay, Qwindow);
4359 if (WINDOWP (window) && XWINDOW (window) != it->w)
4360 continue;
4362 /* If the text ``under'' the overlay is invisible, both before-
4363 and after-strings from this overlay are visible; start and
4364 end position are indistinguishable. */
4365 invisible = Foverlay_get (overlay, Qinvisible);
4366 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4368 /* If overlay has a non-empty before-string, record it. */
4369 if ((start == charpos || (end == charpos && invis_p))
4370 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4371 && SCHARS (str))
4372 RECORD_OVERLAY_STRING (overlay, str, 0);
4374 /* If overlay has a non-empty after-string, record it. */
4375 if ((end == charpos || (start == charpos && invis_p))
4376 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4377 && SCHARS (str))
4378 RECORD_OVERLAY_STRING (overlay, str, 1);
4381 /* Process overlays after the overlay center. */
4382 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4384 XSETMISC (overlay, ov);
4385 xassert (OVERLAYP (overlay));
4386 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4387 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4389 if (start > charpos)
4390 break;
4392 /* Skip this overlay if it doesn't start or end at IT's current
4393 position. */
4394 if (end != charpos && start != charpos)
4395 continue;
4397 /* Skip this overlay if it doesn't apply to IT->w. */
4398 window = Foverlay_get (overlay, Qwindow);
4399 if (WINDOWP (window) && XWINDOW (window) != it->w)
4400 continue;
4402 /* If the text ``under'' the overlay is invisible, it has a zero
4403 dimension, and both before- and after-strings apply. */
4404 invisible = Foverlay_get (overlay, Qinvisible);
4405 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4407 /* If overlay has a non-empty before-string, record it. */
4408 if ((start == charpos || (end == charpos && invis_p))
4409 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4410 && SCHARS (str))
4411 RECORD_OVERLAY_STRING (overlay, str, 0);
4413 /* If overlay has a non-empty after-string, record it. */
4414 if ((end == charpos || (start == charpos && invis_p))
4415 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4416 && SCHARS (str))
4417 RECORD_OVERLAY_STRING (overlay, str, 1);
4420 #undef RECORD_OVERLAY_STRING
4422 /* Sort entries. */
4423 if (n > 1)
4424 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4426 /* Record the total number of strings to process. */
4427 it->n_overlay_strings = n;
4429 /* IT->current.overlay_string_index is the number of overlay strings
4430 that have already been consumed by IT. Copy some of the
4431 remaining overlay strings to IT->overlay_strings. */
4432 i = 0;
4433 j = it->current.overlay_string_index;
4434 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4435 it->overlay_strings[i++] = entries[j++].string;
4437 CHECK_IT (it);
4441 /* Get the first chunk of overlay strings at IT's current buffer
4442 position, or at CHARPOS if that is > 0. Value is non-zero if at
4443 least one overlay string was found. */
4445 static int
4446 get_overlay_strings (it, charpos)
4447 struct it *it;
4448 int charpos;
4450 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4451 process. This fills IT->overlay_strings with strings, and sets
4452 IT->n_overlay_strings to the total number of strings to process.
4453 IT->pos.overlay_string_index has to be set temporarily to zero
4454 because load_overlay_strings needs this; it must be set to -1
4455 when no overlay strings are found because a zero value would
4456 indicate a position in the first overlay string. */
4457 it->current.overlay_string_index = 0;
4458 load_overlay_strings (it, charpos);
4460 /* If we found overlay strings, set up IT to deliver display
4461 elements from the first one. Otherwise set up IT to deliver
4462 from current_buffer. */
4463 if (it->n_overlay_strings)
4465 /* Make sure we know settings in current_buffer, so that we can
4466 restore meaningful values when we're done with the overlay
4467 strings. */
4468 compute_stop_pos (it);
4469 xassert (it->face_id >= 0);
4471 /* Save IT's settings. They are restored after all overlay
4472 strings have been processed. */
4473 xassert (it->sp == 0);
4474 push_it (it);
4476 /* Set up IT to deliver display elements from the first overlay
4477 string. */
4478 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4479 it->string = it->overlay_strings[0];
4480 it->stop_charpos = 0;
4481 xassert (STRINGP (it->string));
4482 it->end_charpos = SCHARS (it->string);
4483 it->multibyte_p = STRING_MULTIBYTE (it->string);
4484 it->method = GET_FROM_STRING;
4486 else
4488 it->string = Qnil;
4489 it->current.overlay_string_index = -1;
4490 it->method = GET_FROM_BUFFER;
4493 CHECK_IT (it);
4495 /* Value is non-zero if we found at least one overlay string. */
4496 return STRINGP (it->string);
4501 /***********************************************************************
4502 Saving and restoring state
4503 ***********************************************************************/
4505 /* Save current settings of IT on IT->stack. Called, for example,
4506 before setting up IT for an overlay string, to be able to restore
4507 IT's settings to what they were after the overlay string has been
4508 processed. */
4510 static void
4511 push_it (it)
4512 struct it *it;
4514 struct iterator_stack_entry *p;
4516 xassert (it->sp < 2);
4517 p = it->stack + it->sp;
4519 p->stop_charpos = it->stop_charpos;
4520 xassert (it->face_id >= 0);
4521 p->face_id = it->face_id;
4522 p->string = it->string;
4523 p->pos = it->current;
4524 p->end_charpos = it->end_charpos;
4525 p->string_nchars = it->string_nchars;
4526 p->area = it->area;
4527 p->multibyte_p = it->multibyte_p;
4528 p->slice = it->slice;
4529 p->space_width = it->space_width;
4530 p->font_height = it->font_height;
4531 p->voffset = it->voffset;
4532 p->string_from_display_prop_p = it->string_from_display_prop_p;
4533 p->display_ellipsis_p = 0;
4534 ++it->sp;
4538 /* Restore IT's settings from IT->stack. Called, for example, when no
4539 more overlay strings must be processed, and we return to delivering
4540 display elements from a buffer, or when the end of a string from a
4541 `display' property is reached and we return to delivering display
4542 elements from an overlay string, or from a buffer. */
4544 static void
4545 pop_it (it)
4546 struct it *it;
4548 struct iterator_stack_entry *p;
4550 xassert (it->sp > 0);
4551 --it->sp;
4552 p = it->stack + it->sp;
4553 it->stop_charpos = p->stop_charpos;
4554 it->face_id = p->face_id;
4555 it->string = p->string;
4556 it->current = p->pos;
4557 it->end_charpos = p->end_charpos;
4558 it->string_nchars = p->string_nchars;
4559 it->area = p->area;
4560 it->multibyte_p = p->multibyte_p;
4561 it->slice = p->slice;
4562 it->space_width = p->space_width;
4563 it->font_height = p->font_height;
4564 it->voffset = p->voffset;
4565 it->string_from_display_prop_p = p->string_from_display_prop_p;
4570 /***********************************************************************
4571 Moving over lines
4572 ***********************************************************************/
4574 /* Set IT's current position to the previous line start. */
4576 static void
4577 back_to_previous_line_start (it)
4578 struct it *it;
4580 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4581 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4585 /* Move IT to the next line start.
4587 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4588 we skipped over part of the text (as opposed to moving the iterator
4589 continuously over the text). Otherwise, don't change the value
4590 of *SKIPPED_P.
4592 Newlines may come from buffer text, overlay strings, or strings
4593 displayed via the `display' property. That's the reason we can't
4594 simply use find_next_newline_no_quit.
4596 Note that this function may not skip over invisible text that is so
4597 because of text properties and immediately follows a newline. If
4598 it would, function reseat_at_next_visible_line_start, when called
4599 from set_iterator_to_next, would effectively make invisible
4600 characters following a newline part of the wrong glyph row, which
4601 leads to wrong cursor motion. */
4603 static int
4604 forward_to_next_line_start (it, skipped_p)
4605 struct it *it;
4606 int *skipped_p;
4608 int old_selective, newline_found_p, n;
4609 const int MAX_NEWLINE_DISTANCE = 500;
4611 /* If already on a newline, just consume it to avoid unintended
4612 skipping over invisible text below. */
4613 if (it->what == IT_CHARACTER
4614 && it->c == '\n'
4615 && CHARPOS (it->position) == IT_CHARPOS (*it))
4617 set_iterator_to_next (it, 0);
4618 it->c = 0;
4619 return 1;
4622 /* Don't handle selective display in the following. It's (a)
4623 unnecessary because it's done by the caller, and (b) leads to an
4624 infinite recursion because next_element_from_ellipsis indirectly
4625 calls this function. */
4626 old_selective = it->selective;
4627 it->selective = 0;
4629 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4630 from buffer text. */
4631 for (n = newline_found_p = 0;
4632 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4633 n += STRINGP (it->string) ? 0 : 1)
4635 if (!get_next_display_element (it))
4636 return 0;
4637 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4638 set_iterator_to_next (it, 0);
4641 /* If we didn't find a newline near enough, see if we can use a
4642 short-cut. */
4643 if (!newline_found_p)
4645 int start = IT_CHARPOS (*it);
4646 int limit = find_next_newline_no_quit (start, 1);
4647 Lisp_Object pos;
4649 xassert (!STRINGP (it->string));
4651 /* If there isn't any `display' property in sight, and no
4652 overlays, we can just use the position of the newline in
4653 buffer text. */
4654 if (it->stop_charpos >= limit
4655 || ((pos = Fnext_single_property_change (make_number (start),
4656 Qdisplay,
4657 Qnil, make_number (limit)),
4658 NILP (pos))
4659 && next_overlay_change (start) == ZV))
4661 IT_CHARPOS (*it) = limit;
4662 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4663 *skipped_p = newline_found_p = 1;
4665 else
4667 while (get_next_display_element (it)
4668 && !newline_found_p)
4670 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4671 set_iterator_to_next (it, 0);
4676 it->selective = old_selective;
4677 return newline_found_p;
4681 /* Set IT's current position to the previous visible line start. Skip
4682 invisible text that is so either due to text properties or due to
4683 selective display. Caution: this does not change IT->current_x and
4684 IT->hpos. */
4686 static void
4687 back_to_previous_visible_line_start (it)
4688 struct it *it;
4690 while (IT_CHARPOS (*it) > BEGV)
4692 back_to_previous_line_start (it);
4693 if (IT_CHARPOS (*it) <= BEGV)
4694 break;
4696 /* If selective > 0, then lines indented more than that values
4697 are invisible. */
4698 if (it->selective > 0
4699 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4700 (double) it->selective)) /* iftc */
4701 continue;
4703 /* Check the newline before point for invisibility. */
4705 Lisp_Object prop;
4706 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4707 Qinvisible, it->window);
4708 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4709 continue;
4712 /* If newline has a display property that replaces the newline with something
4713 else (image or text), find start of overlay or interval and continue search
4714 from that point. */
4715 if (IT_CHARPOS (*it) > BEGV)
4717 struct it it2 = *it;
4718 int pos;
4719 int beg, end;
4720 Lisp_Object val, overlay;
4722 pos = --IT_CHARPOS (it2);
4723 --IT_BYTEPOS (it2);
4724 it2.sp = 0;
4725 if (handle_display_prop (&it2) == HANDLED_RETURN
4726 && !NILP (val = get_char_property_and_overlay
4727 (make_number (pos), Qdisplay, Qnil, &overlay))
4728 && (OVERLAYP (overlay)
4729 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4730 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4732 if (beg < BEGV)
4733 beg = BEGV;
4734 IT_CHARPOS (*it) = beg;
4735 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4736 continue;
4740 break;
4743 xassert (IT_CHARPOS (*it) >= BEGV);
4744 xassert (IT_CHARPOS (*it) == BEGV
4745 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4746 CHECK_IT (it);
4750 /* Reseat iterator IT at the previous visible line start. Skip
4751 invisible text that is so either due to text properties or due to
4752 selective display. At the end, update IT's overlay information,
4753 face information etc. */
4755 void
4756 reseat_at_previous_visible_line_start (it)
4757 struct it *it;
4759 back_to_previous_visible_line_start (it);
4760 reseat (it, it->current.pos, 1);
4761 CHECK_IT (it);
4765 /* Reseat iterator IT on the next visible line start in the current
4766 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4767 preceding the line start. Skip over invisible text that is so
4768 because of selective display. Compute faces, overlays etc at the
4769 new position. Note that this function does not skip over text that
4770 is invisible because of text properties. */
4772 static void
4773 reseat_at_next_visible_line_start (it, on_newline_p)
4774 struct it *it;
4775 int on_newline_p;
4777 int newline_found_p, skipped_p = 0;
4779 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4781 /* Skip over lines that are invisible because they are indented
4782 more than the value of IT->selective. */
4783 if (it->selective > 0)
4784 while (IT_CHARPOS (*it) < ZV
4785 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4786 (double) it->selective)) /* iftc */
4788 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4789 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4792 /* Position on the newline if that's what's requested. */
4793 if (on_newline_p && newline_found_p)
4795 if (STRINGP (it->string))
4797 if (IT_STRING_CHARPOS (*it) > 0)
4799 --IT_STRING_CHARPOS (*it);
4800 --IT_STRING_BYTEPOS (*it);
4803 else if (IT_CHARPOS (*it) > BEGV)
4805 --IT_CHARPOS (*it);
4806 --IT_BYTEPOS (*it);
4807 reseat (it, it->current.pos, 0);
4810 else if (skipped_p)
4811 reseat (it, it->current.pos, 0);
4813 CHECK_IT (it);
4818 /***********************************************************************
4819 Changing an iterator's position
4820 ***********************************************************************/
4822 /* Change IT's current position to POS in current_buffer. If FORCE_P
4823 is non-zero, always check for text properties at the new position.
4824 Otherwise, text properties are only looked up if POS >=
4825 IT->check_charpos of a property. */
4827 static void
4828 reseat (it, pos, force_p)
4829 struct it *it;
4830 struct text_pos pos;
4831 int force_p;
4833 int original_pos = IT_CHARPOS (*it);
4835 reseat_1 (it, pos, 0);
4837 /* Determine where to check text properties. Avoid doing it
4838 where possible because text property lookup is very expensive. */
4839 if (force_p
4840 || CHARPOS (pos) > it->stop_charpos
4841 || CHARPOS (pos) < original_pos)
4842 handle_stop (it);
4844 CHECK_IT (it);
4848 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4849 IT->stop_pos to POS, also. */
4851 static void
4852 reseat_1 (it, pos, set_stop_p)
4853 struct it *it;
4854 struct text_pos pos;
4855 int set_stop_p;
4857 /* Don't call this function when scanning a C string. */
4858 xassert (it->s == NULL);
4860 /* POS must be a reasonable value. */
4861 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4863 it->current.pos = it->position = pos;
4864 XSETBUFFER (it->object, current_buffer);
4865 it->end_charpos = ZV;
4866 it->dpvec = NULL;
4867 it->current.dpvec_index = -1;
4868 it->current.overlay_string_index = -1;
4869 IT_STRING_CHARPOS (*it) = -1;
4870 IT_STRING_BYTEPOS (*it) = -1;
4871 it->string = Qnil;
4872 it->method = GET_FROM_BUFFER;
4873 /* RMS: I added this to fix a bug in move_it_vertically_backward
4874 where it->area continued to relate to the starting point
4875 for the backward motion. Bug report from
4876 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4877 However, I am not sure whether reseat still does the right thing
4878 in general after this change. */
4879 it->area = TEXT_AREA;
4880 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4881 it->sp = 0;
4882 it->face_before_selective_p = 0;
4884 if (set_stop_p)
4885 it->stop_charpos = CHARPOS (pos);
4889 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4890 If S is non-null, it is a C string to iterate over. Otherwise,
4891 STRING gives a Lisp string to iterate over.
4893 If PRECISION > 0, don't return more then PRECISION number of
4894 characters from the string.
4896 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4897 characters have been returned. FIELD_WIDTH < 0 means an infinite
4898 field width.
4900 MULTIBYTE = 0 means disable processing of multibyte characters,
4901 MULTIBYTE > 0 means enable it,
4902 MULTIBYTE < 0 means use IT->multibyte_p.
4904 IT must be initialized via a prior call to init_iterator before
4905 calling this function. */
4907 static void
4908 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4909 struct it *it;
4910 unsigned char *s;
4911 Lisp_Object string;
4912 int charpos;
4913 int precision, field_width, multibyte;
4915 /* No region in strings. */
4916 it->region_beg_charpos = it->region_end_charpos = -1;
4918 /* No text property checks performed by default, but see below. */
4919 it->stop_charpos = -1;
4921 /* Set iterator position and end position. */
4922 bzero (&it->current, sizeof it->current);
4923 it->current.overlay_string_index = -1;
4924 it->current.dpvec_index = -1;
4925 xassert (charpos >= 0);
4927 /* If STRING is specified, use its multibyteness, otherwise use the
4928 setting of MULTIBYTE, if specified. */
4929 if (multibyte >= 0)
4930 it->multibyte_p = multibyte > 0;
4932 if (s == NULL)
4934 xassert (STRINGP (string));
4935 it->string = string;
4936 it->s = NULL;
4937 it->end_charpos = it->string_nchars = SCHARS (string);
4938 it->method = GET_FROM_STRING;
4939 it->current.string_pos = string_pos (charpos, string);
4941 else
4943 it->s = s;
4944 it->string = Qnil;
4946 /* Note that we use IT->current.pos, not it->current.string_pos,
4947 for displaying C strings. */
4948 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4949 if (it->multibyte_p)
4951 it->current.pos = c_string_pos (charpos, s, 1);
4952 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4954 else
4956 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4957 it->end_charpos = it->string_nchars = strlen (s);
4960 it->method = GET_FROM_C_STRING;
4963 /* PRECISION > 0 means don't return more than PRECISION characters
4964 from the string. */
4965 if (precision > 0 && it->end_charpos - charpos > precision)
4966 it->end_charpos = it->string_nchars = charpos + precision;
4968 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4969 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4970 FIELD_WIDTH < 0 means infinite field width. This is useful for
4971 padding with `-' at the end of a mode line. */
4972 if (field_width < 0)
4973 field_width = INFINITY;
4974 if (field_width > it->end_charpos - charpos)
4975 it->end_charpos = charpos + field_width;
4977 /* Use the standard display table for displaying strings. */
4978 if (DISP_TABLE_P (Vstandard_display_table))
4979 it->dp = XCHAR_TABLE (Vstandard_display_table);
4981 it->stop_charpos = charpos;
4982 CHECK_IT (it);
4987 /***********************************************************************
4988 Iteration
4989 ***********************************************************************/
4991 /* Map enum it_method value to corresponding next_element_from_* function. */
4993 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
4995 next_element_from_buffer,
4996 next_element_from_display_vector,
4997 next_element_from_composition,
4998 next_element_from_string,
4999 next_element_from_c_string,
5000 next_element_from_image,
5001 next_element_from_stretch
5005 /* Load IT's display element fields with information about the next
5006 display element from the current position of IT. Value is zero if
5007 end of buffer (or C string) is reached. */
5010 get_next_display_element (it)
5011 struct it *it;
5013 /* Non-zero means that we found a display element. Zero means that
5014 we hit the end of what we iterate over. Performance note: the
5015 function pointer `method' used here turns out to be faster than
5016 using a sequence of if-statements. */
5017 int success_p;
5019 get_next:
5020 success_p = (*get_next_element[it->method]) (it);
5022 if (it->what == IT_CHARACTER)
5024 /* Map via display table or translate control characters.
5025 IT->c, IT->len etc. have been set to the next character by
5026 the function call above. If we have a display table, and it
5027 contains an entry for IT->c, translate it. Don't do this if
5028 IT->c itself comes from a display table, otherwise we could
5029 end up in an infinite recursion. (An alternative could be to
5030 count the recursion depth of this function and signal an
5031 error when a certain maximum depth is reached.) Is it worth
5032 it? */
5033 if (success_p && it->dpvec == NULL)
5035 Lisp_Object dv;
5037 if (it->dp
5038 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5039 VECTORP (dv)))
5041 struct Lisp_Vector *v = XVECTOR (dv);
5043 /* Return the first character from the display table
5044 entry, if not empty. If empty, don't display the
5045 current character. */
5046 if (v->size)
5048 it->dpvec_char_len = it->len;
5049 it->dpvec = v->contents;
5050 it->dpend = v->contents + v->size;
5051 it->current.dpvec_index = 0;
5052 it->dpvec_face_id = -1;
5053 it->saved_face_id = it->face_id;
5054 it->method = GET_FROM_DISPLAY_VECTOR;
5055 it->ellipsis_p = 0;
5057 else
5059 set_iterator_to_next (it, 0);
5061 goto get_next;
5064 /* Translate control characters into `\003' or `^C' form.
5065 Control characters coming from a display table entry are
5066 currently not translated because we use IT->dpvec to hold
5067 the translation. This could easily be changed but I
5068 don't believe that it is worth doing.
5070 If it->multibyte_p is nonzero, eight-bit characters and
5071 non-printable multibyte characters are also translated to
5072 octal form.
5074 If it->multibyte_p is zero, eight-bit characters that
5075 don't have corresponding multibyte char code are also
5076 translated to octal form. */
5077 else if ((it->c < ' '
5078 && (it->area != TEXT_AREA
5079 /* In mode line, treat \n like other crl chars. */
5080 || (it->c != '\t'
5081 && it->glyph_row && it->glyph_row->mode_line_p)
5082 || (it->c != '\n' && it->c != '\t')))
5083 || (it->multibyte_p
5084 ? ((it->c >= 127
5085 && it->len == 1)
5086 || !CHAR_PRINTABLE_P (it->c)
5087 || (!NILP (Vshow_nonbreak_escape)
5088 && (it->c == 0x8ad || it->c == 0x8a0
5089 || it->c == 0xf2d || it->c == 0xf20)))
5090 : (it->c >= 127
5091 && (!unibyte_display_via_language_environment
5092 || it->c == unibyte_char_to_multibyte (it->c)))))
5094 /* IT->c is a control character which must be displayed
5095 either as '\003' or as `^C' where the '\\' and '^'
5096 can be defined in the display table. Fill
5097 IT->ctl_chars with glyphs for what we have to
5098 display. Then, set IT->dpvec to these glyphs. */
5099 GLYPH g;
5100 int ctl_len;
5101 int face_id, lface_id = 0 ;
5102 GLYPH escape_glyph;
5104 if (it->c < 128 && it->ctl_arrow_p)
5106 g = '^'; /* default glyph for Control */
5107 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5108 if (it->dp
5109 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5110 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5112 g = XINT (DISP_CTRL_GLYPH (it->dp));
5113 lface_id = FAST_GLYPH_FACE (g);
5115 if (lface_id)
5117 g = FAST_GLYPH_CHAR (g);
5118 face_id = merge_faces (it->f, Qt, lface_id,
5119 it->face_id);
5121 else
5123 /* Merge the escape-glyph face into the current face. */
5124 face_id = merge_faces (it->f, Qescape_glyph, 0,
5125 it->face_id);
5128 XSETINT (it->ctl_chars[0], g);
5129 g = it->c ^ 0100;
5130 XSETINT (it->ctl_chars[1], g);
5131 ctl_len = 2;
5132 goto display_control;
5135 escape_glyph = '\\'; /* default for Octal display */
5136 if (it->dp
5137 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5138 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5140 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5141 lface_id = FAST_GLYPH_FACE (escape_glyph);
5143 if (lface_id)
5145 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5146 face_id = merge_faces (it->f, Qt, lface_id,
5147 it->face_id);
5149 else
5151 /* Merge the escape-glyph face into the current face. */
5152 face_id = merge_faces (it->f, Qescape_glyph, 0,
5153 it->face_id);
5156 if (it->c == 0x8a0 || it->c == 0x8ad
5157 || it->c == 0xf20 || it->c == 0xf2d)
5159 XSETINT (it->ctl_chars[0], escape_glyph);
5160 g = it->c;
5161 XSETINT (it->ctl_chars[1], g);
5162 ctl_len = 2;
5163 goto display_control;
5167 unsigned char str[MAX_MULTIBYTE_LENGTH];
5168 int len;
5169 int i;
5171 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5172 if (SINGLE_BYTE_CHAR_P (it->c))
5173 str[0] = it->c, len = 1;
5174 else
5176 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5177 if (len < 0)
5179 /* It's an invalid character, which shouldn't
5180 happen actually, but due to bugs it may
5181 happen. Let's print the char as is, there's
5182 not much meaningful we can do with it. */
5183 str[0] = it->c;
5184 str[1] = it->c >> 8;
5185 str[2] = it->c >> 16;
5186 str[3] = it->c >> 24;
5187 len = 4;
5191 for (i = 0; i < len; i++)
5193 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5194 /* Insert three more glyphs into IT->ctl_chars for
5195 the octal display of the character. */
5196 g = ((str[i] >> 6) & 7) + '0';
5197 XSETINT (it->ctl_chars[i * 4 + 1], g);
5198 g = ((str[i] >> 3) & 7) + '0';
5199 XSETINT (it->ctl_chars[i * 4 + 2], g);
5200 g = (str[i] & 7) + '0';
5201 XSETINT (it->ctl_chars[i * 4 + 3], g);
5203 ctl_len = len * 4;
5206 display_control:
5207 /* Set up IT->dpvec and return first character from it. */
5208 it->dpvec_char_len = it->len;
5209 it->dpvec = it->ctl_chars;
5210 it->dpend = it->dpvec + ctl_len;
5211 it->current.dpvec_index = 0;
5212 it->dpvec_face_id = face_id;
5213 it->saved_face_id = it->face_id;
5214 it->method = GET_FROM_DISPLAY_VECTOR;
5215 it->ellipsis_p = 0;
5216 goto get_next;
5220 /* Adjust face id for a multibyte character. There are no
5221 multibyte character in unibyte text. */
5222 if (it->multibyte_p
5223 && success_p
5224 && FRAME_WINDOW_P (it->f))
5226 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5227 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5231 /* Is this character the last one of a run of characters with
5232 box? If yes, set IT->end_of_box_run_p to 1. */
5233 if (it->face_box_p
5234 && it->s == NULL)
5236 int face_id;
5237 struct face *face;
5239 it->end_of_box_run_p
5240 = ((face_id = face_after_it_pos (it),
5241 face_id != it->face_id)
5242 && (face = FACE_FROM_ID (it->f, face_id),
5243 face->box == FACE_NO_BOX));
5246 /* Value is 0 if end of buffer or string reached. */
5247 return success_p;
5251 /* Move IT to the next display element.
5253 RESEAT_P non-zero means if called on a newline in buffer text,
5254 skip to the next visible line start.
5256 Functions get_next_display_element and set_iterator_to_next are
5257 separate because I find this arrangement easier to handle than a
5258 get_next_display_element function that also increments IT's
5259 position. The way it is we can first look at an iterator's current
5260 display element, decide whether it fits on a line, and if it does,
5261 increment the iterator position. The other way around we probably
5262 would either need a flag indicating whether the iterator has to be
5263 incremented the next time, or we would have to implement a
5264 decrement position function which would not be easy to write. */
5266 void
5267 set_iterator_to_next (it, reseat_p)
5268 struct it *it;
5269 int reseat_p;
5271 /* Reset flags indicating start and end of a sequence of characters
5272 with box. Reset them at the start of this function because
5273 moving the iterator to a new position might set them. */
5274 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5276 switch (it->method)
5278 case GET_FROM_BUFFER:
5279 /* The current display element of IT is a character from
5280 current_buffer. Advance in the buffer, and maybe skip over
5281 invisible lines that are so because of selective display. */
5282 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5283 reseat_at_next_visible_line_start (it, 0);
5284 else
5286 xassert (it->len != 0);
5287 IT_BYTEPOS (*it) += it->len;
5288 IT_CHARPOS (*it) += 1;
5289 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5291 break;
5293 case GET_FROM_COMPOSITION:
5294 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5295 if (STRINGP (it->string))
5297 IT_STRING_BYTEPOS (*it) += it->len;
5298 IT_STRING_CHARPOS (*it) += it->cmp_len;
5299 it->method = GET_FROM_STRING;
5300 goto consider_string_end;
5302 else
5304 IT_BYTEPOS (*it) += it->len;
5305 IT_CHARPOS (*it) += it->cmp_len;
5306 it->method = GET_FROM_BUFFER;
5308 break;
5310 case GET_FROM_C_STRING:
5311 /* Current display element of IT is from a C string. */
5312 IT_BYTEPOS (*it) += it->len;
5313 IT_CHARPOS (*it) += 1;
5314 break;
5316 case GET_FROM_DISPLAY_VECTOR:
5317 /* Current display element of IT is from a display table entry.
5318 Advance in the display table definition. Reset it to null if
5319 end reached, and continue with characters from buffers/
5320 strings. */
5321 ++it->current.dpvec_index;
5323 /* Restore face of the iterator to what they were before the
5324 display vector entry (these entries may contain faces). */
5325 it->face_id = it->saved_face_id;
5327 if (it->dpvec + it->current.dpvec_index == it->dpend)
5329 if (it->s)
5330 it->method = GET_FROM_C_STRING;
5331 else if (STRINGP (it->string))
5332 it->method = GET_FROM_STRING;
5333 else
5334 it->method = GET_FROM_BUFFER;
5336 it->dpvec = NULL;
5337 it->current.dpvec_index = -1;
5339 /* Skip over characters which were displayed via IT->dpvec. */
5340 if (it->dpvec_char_len < 0)
5341 reseat_at_next_visible_line_start (it, 1);
5342 else if (it->dpvec_char_len > 0)
5344 it->len = it->dpvec_char_len;
5345 set_iterator_to_next (it, reseat_p);
5348 /* Recheck faces after display vector */
5349 it->stop_charpos = IT_CHARPOS (*it);
5351 break;
5353 case GET_FROM_STRING:
5354 /* Current display element is a character from a Lisp string. */
5355 xassert (it->s == NULL && STRINGP (it->string));
5356 IT_STRING_BYTEPOS (*it) += it->len;
5357 IT_STRING_CHARPOS (*it) += 1;
5359 consider_string_end:
5361 if (it->current.overlay_string_index >= 0)
5363 /* IT->string is an overlay string. Advance to the
5364 next, if there is one. */
5365 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5366 next_overlay_string (it);
5368 else
5370 /* IT->string is not an overlay string. If we reached
5371 its end, and there is something on IT->stack, proceed
5372 with what is on the stack. This can be either another
5373 string, this time an overlay string, or a buffer. */
5374 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5375 && it->sp > 0)
5377 pop_it (it);
5378 if (STRINGP (it->string))
5379 goto consider_string_end;
5380 it->method = GET_FROM_BUFFER;
5383 break;
5385 case GET_FROM_IMAGE:
5386 case GET_FROM_STRETCH:
5387 /* The position etc with which we have to proceed are on
5388 the stack. The position may be at the end of a string,
5389 if the `display' property takes up the whole string. */
5390 xassert (it->sp > 0);
5391 pop_it (it);
5392 it->image_id = 0;
5393 if (STRINGP (it->string))
5395 it->method = GET_FROM_STRING;
5396 goto consider_string_end;
5398 it->method = GET_FROM_BUFFER;
5399 break;
5401 default:
5402 /* There are no other methods defined, so this should be a bug. */
5403 abort ();
5406 xassert (it->method != GET_FROM_STRING
5407 || (STRINGP (it->string)
5408 && IT_STRING_CHARPOS (*it) >= 0));
5411 /* Load IT's display element fields with information about the next
5412 display element which comes from a display table entry or from the
5413 result of translating a control character to one of the forms `^C'
5414 or `\003'.
5416 IT->dpvec holds the glyphs to return as characters.
5417 IT->saved_face_id holds the face id before the display vector--
5418 it is restored into IT->face_idin set_iterator_to_next. */
5420 static int
5421 next_element_from_display_vector (it)
5422 struct it *it;
5424 /* Precondition. */
5425 xassert (it->dpvec && it->current.dpvec_index >= 0);
5427 it->face_id = it->saved_face_id;
5429 if (INTEGERP (*it->dpvec)
5430 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5432 GLYPH g;
5434 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5435 it->c = FAST_GLYPH_CHAR (g);
5436 it->len = CHAR_BYTES (it->c);
5438 /* The entry may contain a face id to use. Such a face id is
5439 the id of a Lisp face, not a realized face. A face id of
5440 zero means no face is specified. */
5441 if (it->dpvec_face_id >= 0)
5442 it->face_id = it->dpvec_face_id;
5443 else
5445 int lface_id = FAST_GLYPH_FACE (g);
5446 if (lface_id > 0)
5447 it->face_id = merge_faces (it->f, Qt, lface_id,
5448 it->saved_face_id);
5451 else
5452 /* Display table entry is invalid. Return a space. */
5453 it->c = ' ', it->len = 1;
5455 /* Don't change position and object of the iterator here. They are
5456 still the values of the character that had this display table
5457 entry or was translated, and that's what we want. */
5458 it->what = IT_CHARACTER;
5459 return 1;
5463 /* Load IT with the next display element from Lisp string IT->string.
5464 IT->current.string_pos is the current position within the string.
5465 If IT->current.overlay_string_index >= 0, the Lisp string is an
5466 overlay string. */
5468 static int
5469 next_element_from_string (it)
5470 struct it *it;
5472 struct text_pos position;
5474 xassert (STRINGP (it->string));
5475 xassert (IT_STRING_CHARPOS (*it) >= 0);
5476 position = it->current.string_pos;
5478 /* Time to check for invisible text? */
5479 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5480 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5482 handle_stop (it);
5484 /* Since a handler may have changed IT->method, we must
5485 recurse here. */
5486 return get_next_display_element (it);
5489 if (it->current.overlay_string_index >= 0)
5491 /* Get the next character from an overlay string. In overlay
5492 strings, There is no field width or padding with spaces to
5493 do. */
5494 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5496 it->what = IT_EOB;
5497 return 0;
5499 else if (STRING_MULTIBYTE (it->string))
5501 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5502 const unsigned char *s = (SDATA (it->string)
5503 + IT_STRING_BYTEPOS (*it));
5504 it->c = string_char_and_length (s, remaining, &it->len);
5506 else
5508 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5509 it->len = 1;
5512 else
5514 /* Get the next character from a Lisp string that is not an
5515 overlay string. Such strings come from the mode line, for
5516 example. We may have to pad with spaces, or truncate the
5517 string. See also next_element_from_c_string. */
5518 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5520 it->what = IT_EOB;
5521 return 0;
5523 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5525 /* Pad with spaces. */
5526 it->c = ' ', it->len = 1;
5527 CHARPOS (position) = BYTEPOS (position) = -1;
5529 else if (STRING_MULTIBYTE (it->string))
5531 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5532 const unsigned char *s = (SDATA (it->string)
5533 + IT_STRING_BYTEPOS (*it));
5534 it->c = string_char_and_length (s, maxlen, &it->len);
5536 else
5538 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5539 it->len = 1;
5543 /* Record what we have and where it came from. Note that we store a
5544 buffer position in IT->position although it could arguably be a
5545 string position. */
5546 it->what = IT_CHARACTER;
5547 it->object = it->string;
5548 it->position = position;
5549 return 1;
5553 /* Load IT with next display element from C string IT->s.
5554 IT->string_nchars is the maximum number of characters to return
5555 from the string. IT->end_charpos may be greater than
5556 IT->string_nchars when this function is called, in which case we
5557 may have to return padding spaces. Value is zero if end of string
5558 reached, including padding spaces. */
5560 static int
5561 next_element_from_c_string (it)
5562 struct it *it;
5564 int success_p = 1;
5566 xassert (it->s);
5567 it->what = IT_CHARACTER;
5568 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5569 it->object = Qnil;
5571 /* IT's position can be greater IT->string_nchars in case a field
5572 width or precision has been specified when the iterator was
5573 initialized. */
5574 if (IT_CHARPOS (*it) >= it->end_charpos)
5576 /* End of the game. */
5577 it->what = IT_EOB;
5578 success_p = 0;
5580 else if (IT_CHARPOS (*it) >= it->string_nchars)
5582 /* Pad with spaces. */
5583 it->c = ' ', it->len = 1;
5584 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5586 else if (it->multibyte_p)
5588 /* Implementation note: The calls to strlen apparently aren't a
5589 performance problem because there is no noticeable performance
5590 difference between Emacs running in unibyte or multibyte mode. */
5591 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5592 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5593 maxlen, &it->len);
5595 else
5596 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5598 return success_p;
5602 /* Set up IT to return characters from an ellipsis, if appropriate.
5603 The definition of the ellipsis glyphs may come from a display table
5604 entry. This function Fills IT with the first glyph from the
5605 ellipsis if an ellipsis is to be displayed. */
5607 static int
5608 next_element_from_ellipsis (it)
5609 struct it *it;
5611 if (it->selective_display_ellipsis_p)
5612 setup_for_ellipsis (it, it->len);
5613 else
5615 /* The face at the current position may be different from the
5616 face we find after the invisible text. Remember what it
5617 was in IT->saved_face_id, and signal that it's there by
5618 setting face_before_selective_p. */
5619 it->saved_face_id = it->face_id;
5620 it->method = GET_FROM_BUFFER;
5621 reseat_at_next_visible_line_start (it, 1);
5622 it->face_before_selective_p = 1;
5625 return get_next_display_element (it);
5629 /* Deliver an image display element. The iterator IT is already
5630 filled with image information (done in handle_display_prop). Value
5631 is always 1. */
5634 static int
5635 next_element_from_image (it)
5636 struct it *it;
5638 it->what = IT_IMAGE;
5639 return 1;
5643 /* Fill iterator IT with next display element from a stretch glyph
5644 property. IT->object is the value of the text property. Value is
5645 always 1. */
5647 static int
5648 next_element_from_stretch (it)
5649 struct it *it;
5651 it->what = IT_STRETCH;
5652 return 1;
5656 /* Load IT with the next display element from current_buffer. Value
5657 is zero if end of buffer reached. IT->stop_charpos is the next
5658 position at which to stop and check for text properties or buffer
5659 end. */
5661 static int
5662 next_element_from_buffer (it)
5663 struct it *it;
5665 int success_p = 1;
5667 /* Check this assumption, otherwise, we would never enter the
5668 if-statement, below. */
5669 xassert (IT_CHARPOS (*it) >= BEGV
5670 && IT_CHARPOS (*it) <= it->stop_charpos);
5672 if (IT_CHARPOS (*it) >= it->stop_charpos)
5674 if (IT_CHARPOS (*it) >= it->end_charpos)
5676 int overlay_strings_follow_p;
5678 /* End of the game, except when overlay strings follow that
5679 haven't been returned yet. */
5680 if (it->overlay_strings_at_end_processed_p)
5681 overlay_strings_follow_p = 0;
5682 else
5684 it->overlay_strings_at_end_processed_p = 1;
5685 overlay_strings_follow_p = get_overlay_strings (it, 0);
5688 if (overlay_strings_follow_p)
5689 success_p = get_next_display_element (it);
5690 else
5692 it->what = IT_EOB;
5693 it->position = it->current.pos;
5694 success_p = 0;
5697 else
5699 handle_stop (it);
5700 return get_next_display_element (it);
5703 else
5705 /* No face changes, overlays etc. in sight, so just return a
5706 character from current_buffer. */
5707 unsigned char *p;
5709 /* Maybe run the redisplay end trigger hook. Performance note:
5710 This doesn't seem to cost measurable time. */
5711 if (it->redisplay_end_trigger_charpos
5712 && it->glyph_row
5713 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5714 run_redisplay_end_trigger_hook (it);
5716 /* Get the next character, maybe multibyte. */
5717 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5718 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5720 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5721 - IT_BYTEPOS (*it));
5722 it->c = string_char_and_length (p, maxlen, &it->len);
5724 else
5725 it->c = *p, it->len = 1;
5727 /* Record what we have and where it came from. */
5728 it->what = IT_CHARACTER;;
5729 it->object = it->w->buffer;
5730 it->position = it->current.pos;
5732 /* Normally we return the character found above, except when we
5733 really want to return an ellipsis for selective display. */
5734 if (it->selective)
5736 if (it->c == '\n')
5738 /* A value of selective > 0 means hide lines indented more
5739 than that number of columns. */
5740 if (it->selective > 0
5741 && IT_CHARPOS (*it) + 1 < ZV
5742 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5743 IT_BYTEPOS (*it) + 1,
5744 (double) it->selective)) /* iftc */
5746 success_p = next_element_from_ellipsis (it);
5747 it->dpvec_char_len = -1;
5750 else if (it->c == '\r' && it->selective == -1)
5752 /* A value of selective == -1 means that everything from the
5753 CR to the end of the line is invisible, with maybe an
5754 ellipsis displayed for it. */
5755 success_p = next_element_from_ellipsis (it);
5756 it->dpvec_char_len = -1;
5761 /* Value is zero if end of buffer reached. */
5762 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5763 return success_p;
5767 /* Run the redisplay end trigger hook for IT. */
5769 static void
5770 run_redisplay_end_trigger_hook (it)
5771 struct it *it;
5773 Lisp_Object args[3];
5775 /* IT->glyph_row should be non-null, i.e. we should be actually
5776 displaying something, or otherwise we should not run the hook. */
5777 xassert (it->glyph_row);
5779 /* Set up hook arguments. */
5780 args[0] = Qredisplay_end_trigger_functions;
5781 args[1] = it->window;
5782 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5783 it->redisplay_end_trigger_charpos = 0;
5785 /* Since we are *trying* to run these functions, don't try to run
5786 them again, even if they get an error. */
5787 it->w->redisplay_end_trigger = Qnil;
5788 Frun_hook_with_args (3, args);
5790 /* Notice if it changed the face of the character we are on. */
5791 handle_face_prop (it);
5795 /* Deliver a composition display element. The iterator IT is already
5796 filled with composition information (done in
5797 handle_composition_prop). Value is always 1. */
5799 static int
5800 next_element_from_composition (it)
5801 struct it *it;
5803 it->what = IT_COMPOSITION;
5804 it->position = (STRINGP (it->string)
5805 ? it->current.string_pos
5806 : it->current.pos);
5807 return 1;
5812 /***********************************************************************
5813 Moving an iterator without producing glyphs
5814 ***********************************************************************/
5816 /* Move iterator IT to a specified buffer or X position within one
5817 line on the display without producing glyphs.
5819 OP should be a bit mask including some or all of these bits:
5820 MOVE_TO_X: Stop on reaching x-position TO_X.
5821 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5822 Regardless of OP's value, stop in reaching the end of the display line.
5824 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5825 This means, in particular, that TO_X includes window's horizontal
5826 scroll amount.
5828 The return value has several possible values that
5829 say what condition caused the scan to stop:
5831 MOVE_POS_MATCH_OR_ZV
5832 - when TO_POS or ZV was reached.
5834 MOVE_X_REACHED
5835 -when TO_X was reached before TO_POS or ZV were reached.
5837 MOVE_LINE_CONTINUED
5838 - when we reached the end of the display area and the line must
5839 be continued.
5841 MOVE_LINE_TRUNCATED
5842 - when we reached the end of the display area and the line is
5843 truncated.
5845 MOVE_NEWLINE_OR_CR
5846 - when we stopped at a line end, i.e. a newline or a CR and selective
5847 display is on. */
5849 static enum move_it_result
5850 move_it_in_display_line_to (it, to_charpos, to_x, op)
5851 struct it *it;
5852 int to_charpos, to_x, op;
5854 enum move_it_result result = MOVE_UNDEFINED;
5855 struct glyph_row *saved_glyph_row;
5857 /* Don't produce glyphs in produce_glyphs. */
5858 saved_glyph_row = it->glyph_row;
5859 it->glyph_row = NULL;
5861 #define BUFFER_POS_REACHED_P() \
5862 ((op & MOVE_TO_POS) != 0 \
5863 && BUFFERP (it->object) \
5864 && IT_CHARPOS (*it) >= to_charpos \
5865 && (it->method == GET_FROM_BUFFER \
5866 || (it->method == GET_FROM_DISPLAY_VECTOR \
5867 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5870 while (1)
5872 int x, i, ascent = 0, descent = 0;
5874 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5875 if ((op & MOVE_TO_POS) != 0
5876 && BUFFERP (it->object)
5877 && it->method == GET_FROM_BUFFER
5878 && IT_CHARPOS (*it) > to_charpos)
5880 result = MOVE_POS_MATCH_OR_ZV;
5881 break;
5884 /* Stop when ZV reached.
5885 We used to stop here when TO_CHARPOS reached as well, but that is
5886 too soon if this glyph does not fit on this line. So we handle it
5887 explicitly below. */
5888 if (!get_next_display_element (it)
5889 || (it->truncate_lines_p
5890 && BUFFER_POS_REACHED_P ()))
5892 result = MOVE_POS_MATCH_OR_ZV;
5893 break;
5896 /* The call to produce_glyphs will get the metrics of the
5897 display element IT is loaded with. We record in x the
5898 x-position before this display element in case it does not
5899 fit on the line. */
5900 x = it->current_x;
5902 /* Remember the line height so far in case the next element doesn't
5903 fit on the line. */
5904 if (!it->truncate_lines_p)
5906 ascent = it->max_ascent;
5907 descent = it->max_descent;
5910 PRODUCE_GLYPHS (it);
5912 if (it->area != TEXT_AREA)
5914 set_iterator_to_next (it, 1);
5915 continue;
5918 /* The number of glyphs we get back in IT->nglyphs will normally
5919 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5920 character on a terminal frame, or (iii) a line end. For the
5921 second case, IT->nglyphs - 1 padding glyphs will be present
5922 (on X frames, there is only one glyph produced for a
5923 composite character.
5925 The behavior implemented below means, for continuation lines,
5926 that as many spaces of a TAB as fit on the current line are
5927 displayed there. For terminal frames, as many glyphs of a
5928 multi-glyph character are displayed in the current line, too.
5929 This is what the old redisplay code did, and we keep it that
5930 way. Under X, the whole shape of a complex character must
5931 fit on the line or it will be completely displayed in the
5932 next line.
5934 Note that both for tabs and padding glyphs, all glyphs have
5935 the same width. */
5936 if (it->nglyphs)
5938 /* More than one glyph or glyph doesn't fit on line. All
5939 glyphs have the same width. */
5940 int single_glyph_width = it->pixel_width / it->nglyphs;
5941 int new_x;
5943 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5945 new_x = x + single_glyph_width;
5947 /* We want to leave anything reaching TO_X to the caller. */
5948 if ((op & MOVE_TO_X) && new_x > to_x)
5950 if (BUFFER_POS_REACHED_P ())
5951 goto buffer_pos_reached;
5952 it->current_x = x;
5953 result = MOVE_X_REACHED;
5954 break;
5956 else if (/* Lines are continued. */
5957 !it->truncate_lines_p
5958 && (/* And glyph doesn't fit on the line. */
5959 new_x > it->last_visible_x
5960 /* Or it fits exactly and we're on a window
5961 system frame. */
5962 || (new_x == it->last_visible_x
5963 && FRAME_WINDOW_P (it->f))))
5965 if (/* IT->hpos == 0 means the very first glyph
5966 doesn't fit on the line, e.g. a wide image. */
5967 it->hpos == 0
5968 || (new_x == it->last_visible_x
5969 && FRAME_WINDOW_P (it->f)))
5971 ++it->hpos;
5972 it->current_x = new_x;
5973 if (i == it->nglyphs - 1)
5975 set_iterator_to_next (it, 1);
5976 #ifdef HAVE_WINDOW_SYSTEM
5977 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5979 if (!get_next_display_element (it))
5981 result = MOVE_POS_MATCH_OR_ZV;
5982 break;
5984 if (BUFFER_POS_REACHED_P ())
5986 if (ITERATOR_AT_END_OF_LINE_P (it))
5987 result = MOVE_POS_MATCH_OR_ZV;
5988 else
5989 result = MOVE_LINE_CONTINUED;
5990 break;
5992 if (ITERATOR_AT_END_OF_LINE_P (it))
5994 result = MOVE_NEWLINE_OR_CR;
5995 break;
5998 #endif /* HAVE_WINDOW_SYSTEM */
6001 else
6003 it->current_x = x;
6004 it->max_ascent = ascent;
6005 it->max_descent = descent;
6008 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6009 IT_CHARPOS (*it)));
6010 result = MOVE_LINE_CONTINUED;
6011 break;
6013 else if (BUFFER_POS_REACHED_P ())
6014 goto buffer_pos_reached;
6015 else if (new_x > it->first_visible_x)
6017 /* Glyph is visible. Increment number of glyphs that
6018 would be displayed. */
6019 ++it->hpos;
6021 else
6023 /* Glyph is completely off the left margin of the display
6024 area. Nothing to do. */
6028 if (result != MOVE_UNDEFINED)
6029 break;
6031 else if (BUFFER_POS_REACHED_P ())
6033 buffer_pos_reached:
6034 it->current_x = x;
6035 it->max_ascent = ascent;
6036 it->max_descent = descent;
6037 result = MOVE_POS_MATCH_OR_ZV;
6038 break;
6040 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6042 /* Stop when TO_X specified and reached. This check is
6043 necessary here because of lines consisting of a line end,
6044 only. The line end will not produce any glyphs and we
6045 would never get MOVE_X_REACHED. */
6046 xassert (it->nglyphs == 0);
6047 result = MOVE_X_REACHED;
6048 break;
6051 /* Is this a line end? If yes, we're done. */
6052 if (ITERATOR_AT_END_OF_LINE_P (it))
6054 result = MOVE_NEWLINE_OR_CR;
6055 break;
6058 /* The current display element has been consumed. Advance
6059 to the next. */
6060 set_iterator_to_next (it, 1);
6062 /* Stop if lines are truncated and IT's current x-position is
6063 past the right edge of the window now. */
6064 if (it->truncate_lines_p
6065 && it->current_x >= it->last_visible_x)
6067 #ifdef HAVE_WINDOW_SYSTEM
6068 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6070 if (!get_next_display_element (it)
6071 || BUFFER_POS_REACHED_P ())
6073 result = MOVE_POS_MATCH_OR_ZV;
6074 break;
6076 if (ITERATOR_AT_END_OF_LINE_P (it))
6078 result = MOVE_NEWLINE_OR_CR;
6079 break;
6082 #endif /* HAVE_WINDOW_SYSTEM */
6083 result = MOVE_LINE_TRUNCATED;
6084 break;
6088 #undef BUFFER_POS_REACHED_P
6090 /* Restore the iterator settings altered at the beginning of this
6091 function. */
6092 it->glyph_row = saved_glyph_row;
6093 return result;
6097 /* Move IT forward until it satisfies one or more of the criteria in
6098 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6100 OP is a bit-mask that specifies where to stop, and in particular,
6101 which of those four position arguments makes a difference. See the
6102 description of enum move_operation_enum.
6104 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6105 screen line, this function will set IT to the next position >
6106 TO_CHARPOS. */
6108 void
6109 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6110 struct it *it;
6111 int to_charpos, to_x, to_y, to_vpos;
6112 int op;
6114 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6115 int line_height;
6116 int reached = 0;
6118 for (;;)
6120 if (op & MOVE_TO_VPOS)
6122 /* If no TO_CHARPOS and no TO_X specified, stop at the
6123 start of the line TO_VPOS. */
6124 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6126 if (it->vpos == to_vpos)
6128 reached = 1;
6129 break;
6131 else
6132 skip = move_it_in_display_line_to (it, -1, -1, 0);
6134 else
6136 /* TO_VPOS >= 0 means stop at TO_X in the line at
6137 TO_VPOS, or at TO_POS, whichever comes first. */
6138 if (it->vpos == to_vpos)
6140 reached = 2;
6141 break;
6144 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6146 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6148 reached = 3;
6149 break;
6151 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6153 /* We have reached TO_X but not in the line we want. */
6154 skip = move_it_in_display_line_to (it, to_charpos,
6155 -1, MOVE_TO_POS);
6156 if (skip == MOVE_POS_MATCH_OR_ZV)
6158 reached = 4;
6159 break;
6164 else if (op & MOVE_TO_Y)
6166 struct it it_backup;
6168 /* TO_Y specified means stop at TO_X in the line containing
6169 TO_Y---or at TO_CHARPOS if this is reached first. The
6170 problem is that we can't really tell whether the line
6171 contains TO_Y before we have completely scanned it, and
6172 this may skip past TO_X. What we do is to first scan to
6173 TO_X.
6175 If TO_X is not specified, use a TO_X of zero. The reason
6176 is to make the outcome of this function more predictable.
6177 If we didn't use TO_X == 0, we would stop at the end of
6178 the line which is probably not what a caller would expect
6179 to happen. */
6180 skip = move_it_in_display_line_to (it, to_charpos,
6181 ((op & MOVE_TO_X)
6182 ? to_x : 0),
6183 (MOVE_TO_X
6184 | (op & MOVE_TO_POS)));
6186 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6187 if (skip == MOVE_POS_MATCH_OR_ZV)
6189 reached = 5;
6190 break;
6193 /* If TO_X was reached, we would like to know whether TO_Y
6194 is in the line. This can only be said if we know the
6195 total line height which requires us to scan the rest of
6196 the line. */
6197 if (skip == MOVE_X_REACHED)
6199 it_backup = *it;
6200 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6201 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6202 op & MOVE_TO_POS);
6203 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6206 /* Now, decide whether TO_Y is in this line. */
6207 line_height = it->max_ascent + it->max_descent;
6208 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6210 if (to_y >= it->current_y
6211 && to_y < it->current_y + line_height)
6213 if (skip == MOVE_X_REACHED)
6214 /* If TO_Y is in this line and TO_X was reached above,
6215 we scanned too far. We have to restore IT's settings
6216 to the ones before skipping. */
6217 *it = it_backup;
6218 reached = 6;
6220 else if (skip == MOVE_X_REACHED)
6222 skip = skip2;
6223 if (skip == MOVE_POS_MATCH_OR_ZV)
6224 reached = 7;
6227 if (reached)
6228 break;
6230 else
6231 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6233 switch (skip)
6235 case MOVE_POS_MATCH_OR_ZV:
6236 reached = 8;
6237 goto out;
6239 case MOVE_NEWLINE_OR_CR:
6240 set_iterator_to_next (it, 1);
6241 it->continuation_lines_width = 0;
6242 break;
6244 case MOVE_LINE_TRUNCATED:
6245 it->continuation_lines_width = 0;
6246 reseat_at_next_visible_line_start (it, 0);
6247 if ((op & MOVE_TO_POS) != 0
6248 && IT_CHARPOS (*it) > to_charpos)
6250 reached = 9;
6251 goto out;
6253 break;
6255 case MOVE_LINE_CONTINUED:
6256 it->continuation_lines_width += it->current_x;
6257 break;
6259 default:
6260 abort ();
6263 /* Reset/increment for the next run. */
6264 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6265 it->current_x = it->hpos = 0;
6266 it->current_y += it->max_ascent + it->max_descent;
6267 ++it->vpos;
6268 last_height = it->max_ascent + it->max_descent;
6269 last_max_ascent = it->max_ascent;
6270 it->max_ascent = it->max_descent = 0;
6273 out:
6275 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6279 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6281 If DY > 0, move IT backward at least that many pixels. DY = 0
6282 means move IT backward to the preceding line start or BEGV. This
6283 function may move over more than DY pixels if IT->current_y - DY
6284 ends up in the middle of a line; in this case IT->current_y will be
6285 set to the top of the line moved to. */
6287 void
6288 move_it_vertically_backward (it, dy)
6289 struct it *it;
6290 int dy;
6292 int nlines, h;
6293 struct it it2, it3;
6294 int start_pos;
6296 move_further_back:
6297 xassert (dy >= 0);
6299 start_pos = IT_CHARPOS (*it);
6301 /* Estimate how many newlines we must move back. */
6302 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6304 /* Set the iterator's position that many lines back. */
6305 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6306 back_to_previous_visible_line_start (it);
6308 /* Reseat the iterator here. When moving backward, we don't want
6309 reseat to skip forward over invisible text, set up the iterator
6310 to deliver from overlay strings at the new position etc. So,
6311 use reseat_1 here. */
6312 reseat_1 (it, it->current.pos, 1);
6314 /* We are now surely at a line start. */
6315 it->current_x = it->hpos = 0;
6316 it->continuation_lines_width = 0;
6318 /* Move forward and see what y-distance we moved. First move to the
6319 start of the next line so that we get its height. We need this
6320 height to be able to tell whether we reached the specified
6321 y-distance. */
6322 it2 = *it;
6323 it2.max_ascent = it2.max_descent = 0;
6324 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6325 MOVE_TO_POS | MOVE_TO_VPOS);
6326 xassert (IT_CHARPOS (*it) >= BEGV);
6327 it3 = it2;
6329 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6330 xassert (IT_CHARPOS (*it) >= BEGV);
6331 /* H is the actual vertical distance from the position in *IT
6332 and the starting position. */
6333 h = it2.current_y - it->current_y;
6334 /* NLINES is the distance in number of lines. */
6335 nlines = it2.vpos - it->vpos;
6337 /* Correct IT's y and vpos position
6338 so that they are relative to the starting point. */
6339 it->vpos -= nlines;
6340 it->current_y -= h;
6342 if (dy == 0)
6344 /* DY == 0 means move to the start of the screen line. The
6345 value of nlines is > 0 if continuation lines were involved. */
6346 if (nlines > 0)
6347 move_it_by_lines (it, nlines, 1);
6348 #if 0
6349 /* I think this assert is bogus if buffer contains
6350 invisible text or images. KFS. */
6351 xassert (IT_CHARPOS (*it) <= start_pos);
6352 #endif
6354 else
6356 /* The y-position we try to reach, relative to *IT.
6357 Note that H has been subtracted in front of the if-statement. */
6358 int target_y = it->current_y + h - dy;
6359 int y0 = it3.current_y;
6360 int y1 = line_bottom_y (&it3);
6361 int line_height = y1 - y0;
6363 /* If we did not reach target_y, try to move further backward if
6364 we can. If we moved too far backward, try to move forward. */
6365 if (target_y < it->current_y
6366 /* This is heuristic. In a window that's 3 lines high, with
6367 a line height of 13 pixels each, recentering with point
6368 on the bottom line will try to move -39/2 = 19 pixels
6369 backward. Try to avoid moving into the first line. */
6370 && (it->current_y - target_y
6371 > min (window_box_height (it->w), line_height * 2 / 3))
6372 && IT_CHARPOS (*it) > BEGV)
6374 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6375 target_y - it->current_y));
6376 dy = it->current_y - target_y;
6377 goto move_further_back;
6379 else if (target_y >= it->current_y + line_height
6380 && IT_CHARPOS (*it) < ZV)
6382 /* Should move forward by at least one line, maybe more.
6384 Note: Calling move_it_by_lines can be expensive on
6385 terminal frames, where compute_motion is used (via
6386 vmotion) to do the job, when there are very long lines
6387 and truncate-lines is nil. That's the reason for
6388 treating terminal frames specially here. */
6390 if (!FRAME_WINDOW_P (it->f))
6391 move_it_vertically (it, target_y - (it->current_y + line_height));
6392 else
6396 move_it_by_lines (it, 1, 1);
6398 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6401 #if 0
6402 /* I think this assert is bogus if buffer contains
6403 invisible text or images. KFS. */
6404 xassert (IT_CHARPOS (*it) >= BEGV);
6405 #endif
6411 /* Move IT by a specified amount of pixel lines DY. DY negative means
6412 move backwards. DY = 0 means move to start of screen line. At the
6413 end, IT will be on the start of a screen line. */
6415 void
6416 move_it_vertically (it, dy)
6417 struct it *it;
6418 int dy;
6420 if (dy <= 0)
6421 move_it_vertically_backward (it, -dy);
6422 else
6424 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6425 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6426 MOVE_TO_POS | MOVE_TO_Y);
6427 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6429 /* If buffer ends in ZV without a newline, move to the start of
6430 the line to satisfy the post-condition. */
6431 if (IT_CHARPOS (*it) == ZV
6432 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6433 move_it_by_lines (it, 0, 0);
6438 /* Move iterator IT past the end of the text line it is in. */
6440 void
6441 move_it_past_eol (it)
6442 struct it *it;
6444 enum move_it_result rc;
6446 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6447 if (rc == MOVE_NEWLINE_OR_CR)
6448 set_iterator_to_next (it, 0);
6452 #if 0 /* Currently not used. */
6454 /* Return non-zero if some text between buffer positions START_CHARPOS
6455 and END_CHARPOS is invisible. IT->window is the window for text
6456 property lookup. */
6458 static int
6459 invisible_text_between_p (it, start_charpos, end_charpos)
6460 struct it *it;
6461 int start_charpos, end_charpos;
6463 Lisp_Object prop, limit;
6464 int invisible_found_p;
6466 xassert (it != NULL && start_charpos <= end_charpos);
6468 /* Is text at START invisible? */
6469 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6470 it->window);
6471 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6472 invisible_found_p = 1;
6473 else
6475 limit = Fnext_single_char_property_change (make_number (start_charpos),
6476 Qinvisible, Qnil,
6477 make_number (end_charpos));
6478 invisible_found_p = XFASTINT (limit) < end_charpos;
6481 return invisible_found_p;
6484 #endif /* 0 */
6487 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6488 negative means move up. DVPOS == 0 means move to the start of the
6489 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6490 NEED_Y_P is zero, IT->current_y will be left unchanged.
6492 Further optimization ideas: If we would know that IT->f doesn't use
6493 a face with proportional font, we could be faster for
6494 truncate-lines nil. */
6496 void
6497 move_it_by_lines (it, dvpos, need_y_p)
6498 struct it *it;
6499 int dvpos, need_y_p;
6501 struct position pos;
6503 if (!FRAME_WINDOW_P (it->f))
6505 struct text_pos textpos;
6507 /* We can use vmotion on frames without proportional fonts. */
6508 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6509 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6510 reseat (it, textpos, 1);
6511 it->vpos += pos.vpos;
6512 it->current_y += pos.vpos;
6514 else if (dvpos == 0)
6516 /* DVPOS == 0 means move to the start of the screen line. */
6517 move_it_vertically_backward (it, 0);
6518 xassert (it->current_x == 0 && it->hpos == 0);
6519 /* Let next call to line_bottom_y calculate real line height */
6520 last_height = 0;
6522 else if (dvpos > 0)
6523 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6524 else
6526 struct it it2;
6527 int start_charpos, i;
6529 /* Start at the beginning of the screen line containing IT's
6530 position. */
6531 move_it_vertically_backward (it, 0);
6533 /* Go back -DVPOS visible lines and reseat the iterator there. */
6534 start_charpos = IT_CHARPOS (*it);
6535 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6536 back_to_previous_visible_line_start (it);
6537 reseat (it, it->current.pos, 1);
6538 it->current_x = it->hpos = 0;
6540 /* Above call may have moved too far if continuation lines
6541 are involved. Scan forward and see if it did. */
6542 it2 = *it;
6543 it2.vpos = it2.current_y = 0;
6544 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6545 it->vpos -= it2.vpos;
6546 it->current_y -= it2.current_y;
6547 it->current_x = it->hpos = 0;
6549 /* If we moved too far back, move IT some lines forward. */
6550 if (it2.vpos > -dvpos)
6552 int delta = it2.vpos + dvpos;
6553 it2 = *it;
6554 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6555 /* Move back again if we got too far ahead. */
6556 if (IT_CHARPOS (*it) >= start_charpos)
6557 *it = it2;
6562 /* Return 1 if IT points into the middle of a display vector. */
6565 in_display_vector_p (it)
6566 struct it *it;
6568 return (it->method == GET_FROM_DISPLAY_VECTOR
6569 && it->current.dpvec_index > 0
6570 && it->dpvec + it->current.dpvec_index != it->dpend);
6574 /***********************************************************************
6575 Messages
6576 ***********************************************************************/
6579 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6580 to *Messages*. */
6582 void
6583 add_to_log (format, arg1, arg2)
6584 char *format;
6585 Lisp_Object arg1, arg2;
6587 Lisp_Object args[3];
6588 Lisp_Object msg, fmt;
6589 char *buffer;
6590 int len;
6591 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6592 USE_SAFE_ALLOCA;
6594 /* Do nothing if called asynchronously. Inserting text into
6595 a buffer may call after-change-functions and alike and
6596 that would means running Lisp asynchronously. */
6597 if (handling_signal)
6598 return;
6600 fmt = msg = Qnil;
6601 GCPRO4 (fmt, msg, arg1, arg2);
6603 args[0] = fmt = build_string (format);
6604 args[1] = arg1;
6605 args[2] = arg2;
6606 msg = Fformat (3, args);
6608 len = SBYTES (msg) + 1;
6609 SAFE_ALLOCA (buffer, char *, len);
6610 bcopy (SDATA (msg), buffer, len);
6612 message_dolog (buffer, len - 1, 1, 0);
6613 SAFE_FREE ();
6615 UNGCPRO;
6619 /* Output a newline in the *Messages* buffer if "needs" one. */
6621 void
6622 message_log_maybe_newline ()
6624 if (message_log_need_newline)
6625 message_dolog ("", 0, 1, 0);
6629 /* Add a string M of length NBYTES to the message log, optionally
6630 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6631 nonzero, means interpret the contents of M as multibyte. This
6632 function calls low-level routines in order to bypass text property
6633 hooks, etc. which might not be safe to run. */
6635 void
6636 message_dolog (m, nbytes, nlflag, multibyte)
6637 const char *m;
6638 int nbytes, nlflag, multibyte;
6640 if (!NILP (Vmemory_full))
6641 return;
6643 if (!NILP (Vmessage_log_max))
6645 struct buffer *oldbuf;
6646 Lisp_Object oldpoint, oldbegv, oldzv;
6647 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6648 int point_at_end = 0;
6649 int zv_at_end = 0;
6650 Lisp_Object old_deactivate_mark, tem;
6651 struct gcpro gcpro1;
6653 old_deactivate_mark = Vdeactivate_mark;
6654 oldbuf = current_buffer;
6655 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6656 current_buffer->undo_list = Qt;
6658 oldpoint = message_dolog_marker1;
6659 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6660 oldbegv = message_dolog_marker2;
6661 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6662 oldzv = message_dolog_marker3;
6663 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6664 GCPRO1 (old_deactivate_mark);
6666 if (PT == Z)
6667 point_at_end = 1;
6668 if (ZV == Z)
6669 zv_at_end = 1;
6671 BEGV = BEG;
6672 BEGV_BYTE = BEG_BYTE;
6673 ZV = Z;
6674 ZV_BYTE = Z_BYTE;
6675 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6677 /* Insert the string--maybe converting multibyte to single byte
6678 or vice versa, so that all the text fits the buffer. */
6679 if (multibyte
6680 && NILP (current_buffer->enable_multibyte_characters))
6682 int i, c, char_bytes;
6683 unsigned char work[1];
6685 /* Convert a multibyte string to single-byte
6686 for the *Message* buffer. */
6687 for (i = 0; i < nbytes; i += char_bytes)
6689 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6690 work[0] = (SINGLE_BYTE_CHAR_P (c)
6692 : multibyte_char_to_unibyte (c, Qnil));
6693 insert_1_both (work, 1, 1, 1, 0, 0);
6696 else if (! multibyte
6697 && ! NILP (current_buffer->enable_multibyte_characters))
6699 int i, c, char_bytes;
6700 unsigned char *msg = (unsigned char *) m;
6701 unsigned char str[MAX_MULTIBYTE_LENGTH];
6702 /* Convert a single-byte string to multibyte
6703 for the *Message* buffer. */
6704 for (i = 0; i < nbytes; i++)
6706 c = unibyte_char_to_multibyte (msg[i]);
6707 char_bytes = CHAR_STRING (c, str);
6708 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6711 else if (nbytes)
6712 insert_1 (m, nbytes, 1, 0, 0);
6714 if (nlflag)
6716 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6717 insert_1 ("\n", 1, 1, 0, 0);
6719 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6720 this_bol = PT;
6721 this_bol_byte = PT_BYTE;
6723 /* See if this line duplicates the previous one.
6724 If so, combine duplicates. */
6725 if (this_bol > BEG)
6727 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6728 prev_bol = PT;
6729 prev_bol_byte = PT_BYTE;
6731 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6732 this_bol, this_bol_byte);
6733 if (dup)
6735 del_range_both (prev_bol, prev_bol_byte,
6736 this_bol, this_bol_byte, 0);
6737 if (dup > 1)
6739 char dupstr[40];
6740 int duplen;
6742 /* If you change this format, don't forget to also
6743 change message_log_check_duplicate. */
6744 sprintf (dupstr, " [%d times]", dup);
6745 duplen = strlen (dupstr);
6746 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6747 insert_1 (dupstr, duplen, 1, 0, 1);
6752 /* If we have more than the desired maximum number of lines
6753 in the *Messages* buffer now, delete the oldest ones.
6754 This is safe because we don't have undo in this buffer. */
6756 if (NATNUMP (Vmessage_log_max))
6758 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6759 -XFASTINT (Vmessage_log_max) - 1, 0);
6760 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6763 BEGV = XMARKER (oldbegv)->charpos;
6764 BEGV_BYTE = marker_byte_position (oldbegv);
6766 if (zv_at_end)
6768 ZV = Z;
6769 ZV_BYTE = Z_BYTE;
6771 else
6773 ZV = XMARKER (oldzv)->charpos;
6774 ZV_BYTE = marker_byte_position (oldzv);
6777 if (point_at_end)
6778 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6779 else
6780 /* We can't do Fgoto_char (oldpoint) because it will run some
6781 Lisp code. */
6782 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6783 XMARKER (oldpoint)->bytepos);
6785 UNGCPRO;
6786 unchain_marker (XMARKER (oldpoint));
6787 unchain_marker (XMARKER (oldbegv));
6788 unchain_marker (XMARKER (oldzv));
6790 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6791 set_buffer_internal (oldbuf);
6792 if (NILP (tem))
6793 windows_or_buffers_changed = old_windows_or_buffers_changed;
6794 message_log_need_newline = !nlflag;
6795 Vdeactivate_mark = old_deactivate_mark;
6800 /* We are at the end of the buffer after just having inserted a newline.
6801 (Note: We depend on the fact we won't be crossing the gap.)
6802 Check to see if the most recent message looks a lot like the previous one.
6803 Return 0 if different, 1 if the new one should just replace it, or a
6804 value N > 1 if we should also append " [N times]". */
6806 static int
6807 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6808 int prev_bol, this_bol;
6809 int prev_bol_byte, this_bol_byte;
6811 int i;
6812 int len = Z_BYTE - 1 - this_bol_byte;
6813 int seen_dots = 0;
6814 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6815 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6817 for (i = 0; i < len; i++)
6819 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6820 seen_dots = 1;
6821 if (p1[i] != p2[i])
6822 return seen_dots;
6824 p1 += len;
6825 if (*p1 == '\n')
6826 return 2;
6827 if (*p1++ == ' ' && *p1++ == '[')
6829 int n = 0;
6830 while (*p1 >= '0' && *p1 <= '9')
6831 n = n * 10 + *p1++ - '0';
6832 if (strncmp (p1, " times]\n", 8) == 0)
6833 return n+1;
6835 return 0;
6839 /* Display an echo area message M with a specified length of NBYTES
6840 bytes. The string may include null characters. If M is 0, clear
6841 out any existing message, and let the mini-buffer text show
6842 through.
6844 The buffer M must continue to exist until after the echo area gets
6845 cleared or some other message gets displayed there. This means do
6846 not pass text that is stored in a Lisp string; do not pass text in
6847 a buffer that was alloca'd. */
6849 void
6850 message2 (m, nbytes, multibyte)
6851 const char *m;
6852 int nbytes;
6853 int multibyte;
6855 /* First flush out any partial line written with print. */
6856 message_log_maybe_newline ();
6857 if (m)
6858 message_dolog (m, nbytes, 1, multibyte);
6859 message2_nolog (m, nbytes, multibyte);
6863 /* The non-logging counterpart of message2. */
6865 void
6866 message2_nolog (m, nbytes, multibyte)
6867 const char *m;
6868 int nbytes, multibyte;
6870 struct frame *sf = SELECTED_FRAME ();
6871 message_enable_multibyte = multibyte;
6873 if (noninteractive)
6875 if (noninteractive_need_newline)
6876 putc ('\n', stderr);
6877 noninteractive_need_newline = 0;
6878 if (m)
6879 fwrite (m, nbytes, 1, stderr);
6880 if (cursor_in_echo_area == 0)
6881 fprintf (stderr, "\n");
6882 fflush (stderr);
6884 /* A null message buffer means that the frame hasn't really been
6885 initialized yet. Error messages get reported properly by
6886 cmd_error, so this must be just an informative message; toss it. */
6887 else if (INTERACTIVE
6888 && sf->glyphs_initialized_p
6889 && FRAME_MESSAGE_BUF (sf))
6891 Lisp_Object mini_window;
6892 struct frame *f;
6894 /* Get the frame containing the mini-buffer
6895 that the selected frame is using. */
6896 mini_window = FRAME_MINIBUF_WINDOW (sf);
6897 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6899 FRAME_SAMPLE_VISIBILITY (f);
6900 if (FRAME_VISIBLE_P (sf)
6901 && ! FRAME_VISIBLE_P (f))
6902 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6904 if (m)
6906 set_message (m, Qnil, nbytes, multibyte);
6907 if (minibuffer_auto_raise)
6908 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6910 else
6911 clear_message (1, 1);
6913 do_pending_window_change (0);
6914 echo_area_display (1);
6915 do_pending_window_change (0);
6916 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6917 (*frame_up_to_date_hook) (f);
6922 /* Display an echo area message M with a specified length of NBYTES
6923 bytes. The string may include null characters. If M is not a
6924 string, clear out any existing message, and let the mini-buffer
6925 text show through. */
6927 void
6928 message3 (m, nbytes, multibyte)
6929 Lisp_Object m;
6930 int nbytes;
6931 int multibyte;
6933 struct gcpro gcpro1;
6935 GCPRO1 (m);
6936 clear_message (1,1);
6938 /* First flush out any partial line written with print. */
6939 message_log_maybe_newline ();
6940 if (STRINGP (m))
6941 message_dolog (SDATA (m), nbytes, 1, multibyte);
6942 message3_nolog (m, nbytes, multibyte);
6944 UNGCPRO;
6948 /* The non-logging version of message3. */
6950 void
6951 message3_nolog (m, nbytes, multibyte)
6952 Lisp_Object m;
6953 int nbytes, multibyte;
6955 struct frame *sf = SELECTED_FRAME ();
6956 message_enable_multibyte = multibyte;
6958 if (noninteractive)
6960 if (noninteractive_need_newline)
6961 putc ('\n', stderr);
6962 noninteractive_need_newline = 0;
6963 if (STRINGP (m))
6964 fwrite (SDATA (m), nbytes, 1, stderr);
6965 if (cursor_in_echo_area == 0)
6966 fprintf (stderr, "\n");
6967 fflush (stderr);
6969 /* A null message buffer means that the frame hasn't really been
6970 initialized yet. Error messages get reported properly by
6971 cmd_error, so this must be just an informative message; toss it. */
6972 else if (INTERACTIVE
6973 && sf->glyphs_initialized_p
6974 && FRAME_MESSAGE_BUF (sf))
6976 Lisp_Object mini_window;
6977 Lisp_Object frame;
6978 struct frame *f;
6980 /* Get the frame containing the mini-buffer
6981 that the selected frame is using. */
6982 mini_window = FRAME_MINIBUF_WINDOW (sf);
6983 frame = XWINDOW (mini_window)->frame;
6984 f = XFRAME (frame);
6986 FRAME_SAMPLE_VISIBILITY (f);
6987 if (FRAME_VISIBLE_P (sf)
6988 && !FRAME_VISIBLE_P (f))
6989 Fmake_frame_visible (frame);
6991 if (STRINGP (m) && SCHARS (m) > 0)
6993 set_message (NULL, m, nbytes, multibyte);
6994 if (minibuffer_auto_raise)
6995 Fraise_frame (frame);
6997 else
6998 clear_message (1, 1);
7000 do_pending_window_change (0);
7001 echo_area_display (1);
7002 do_pending_window_change (0);
7003 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7004 (*frame_up_to_date_hook) (f);
7009 /* Display a null-terminated echo area message M. If M is 0, clear
7010 out any existing message, and let the mini-buffer text show through.
7012 The buffer M must continue to exist until after the echo area gets
7013 cleared or some other message gets displayed there. Do not pass
7014 text that is stored in a Lisp string. Do not pass text in a buffer
7015 that was alloca'd. */
7017 void
7018 message1 (m)
7019 char *m;
7021 message2 (m, (m ? strlen (m) : 0), 0);
7025 /* The non-logging counterpart of message1. */
7027 void
7028 message1_nolog (m)
7029 char *m;
7031 message2_nolog (m, (m ? strlen (m) : 0), 0);
7034 /* Display a message M which contains a single %s
7035 which gets replaced with STRING. */
7037 void
7038 message_with_string (m, string, log)
7039 char *m;
7040 Lisp_Object string;
7041 int log;
7043 CHECK_STRING (string);
7045 if (noninteractive)
7047 if (m)
7049 if (noninteractive_need_newline)
7050 putc ('\n', stderr);
7051 noninteractive_need_newline = 0;
7052 fprintf (stderr, m, SDATA (string));
7053 if (cursor_in_echo_area == 0)
7054 fprintf (stderr, "\n");
7055 fflush (stderr);
7058 else if (INTERACTIVE)
7060 /* The frame whose minibuffer we're going to display the message on.
7061 It may be larger than the selected frame, so we need
7062 to use its buffer, not the selected frame's buffer. */
7063 Lisp_Object mini_window;
7064 struct frame *f, *sf = SELECTED_FRAME ();
7066 /* Get the frame containing the minibuffer
7067 that the selected frame is using. */
7068 mini_window = FRAME_MINIBUF_WINDOW (sf);
7069 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7071 /* A null message buffer means that the frame hasn't really been
7072 initialized yet. Error messages get reported properly by
7073 cmd_error, so this must be just an informative message; toss it. */
7074 if (FRAME_MESSAGE_BUF (f))
7076 Lisp_Object args[2], message;
7077 struct gcpro gcpro1, gcpro2;
7079 args[0] = build_string (m);
7080 args[1] = message = string;
7081 GCPRO2 (args[0], message);
7082 gcpro1.nvars = 2;
7084 message = Fformat (2, args);
7086 if (log)
7087 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7088 else
7089 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7091 UNGCPRO;
7093 /* Print should start at the beginning of the message
7094 buffer next time. */
7095 message_buf_print = 0;
7101 /* Dump an informative message to the minibuf. If M is 0, clear out
7102 any existing message, and let the mini-buffer text show through. */
7104 /* VARARGS 1 */
7105 void
7106 message (m, a1, a2, a3)
7107 char *m;
7108 EMACS_INT a1, a2, a3;
7110 if (noninteractive)
7112 if (m)
7114 if (noninteractive_need_newline)
7115 putc ('\n', stderr);
7116 noninteractive_need_newline = 0;
7117 fprintf (stderr, m, a1, a2, a3);
7118 if (cursor_in_echo_area == 0)
7119 fprintf (stderr, "\n");
7120 fflush (stderr);
7123 else if (INTERACTIVE)
7125 /* The frame whose mini-buffer we're going to display the message
7126 on. It may be larger than the selected frame, so we need to
7127 use its buffer, not the selected frame's buffer. */
7128 Lisp_Object mini_window;
7129 struct frame *f, *sf = SELECTED_FRAME ();
7131 /* Get the frame containing the mini-buffer
7132 that the selected frame is using. */
7133 mini_window = FRAME_MINIBUF_WINDOW (sf);
7134 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7136 /* A null message buffer means that the frame hasn't really been
7137 initialized yet. Error messages get reported properly by
7138 cmd_error, so this must be just an informative message; toss
7139 it. */
7140 if (FRAME_MESSAGE_BUF (f))
7142 if (m)
7144 int len;
7145 #ifdef NO_ARG_ARRAY
7146 char *a[3];
7147 a[0] = (char *) a1;
7148 a[1] = (char *) a2;
7149 a[2] = (char *) a3;
7151 len = doprnt (FRAME_MESSAGE_BUF (f),
7152 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7153 #else
7154 len = doprnt (FRAME_MESSAGE_BUF (f),
7155 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7156 (char **) &a1);
7157 #endif /* NO_ARG_ARRAY */
7159 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7161 else
7162 message1 (0);
7164 /* Print should start at the beginning of the message
7165 buffer next time. */
7166 message_buf_print = 0;
7172 /* The non-logging version of message. */
7174 void
7175 message_nolog (m, a1, a2, a3)
7176 char *m;
7177 EMACS_INT a1, a2, a3;
7179 Lisp_Object old_log_max;
7180 old_log_max = Vmessage_log_max;
7181 Vmessage_log_max = Qnil;
7182 message (m, a1, a2, a3);
7183 Vmessage_log_max = old_log_max;
7187 /* Display the current message in the current mini-buffer. This is
7188 only called from error handlers in process.c, and is not time
7189 critical. */
7191 void
7192 update_echo_area ()
7194 if (!NILP (echo_area_buffer[0]))
7196 Lisp_Object string;
7197 string = Fcurrent_message ();
7198 message3 (string, SBYTES (string),
7199 !NILP (current_buffer->enable_multibyte_characters));
7204 /* Make sure echo area buffers in `echo_buffers' are live.
7205 If they aren't, make new ones. */
7207 static void
7208 ensure_echo_area_buffers ()
7210 int i;
7212 for (i = 0; i < 2; ++i)
7213 if (!BUFFERP (echo_buffer[i])
7214 || NILP (XBUFFER (echo_buffer[i])->name))
7216 char name[30];
7217 Lisp_Object old_buffer;
7218 int j;
7220 old_buffer = echo_buffer[i];
7221 sprintf (name, " *Echo Area %d*", i);
7222 echo_buffer[i] = Fget_buffer_create (build_string (name));
7223 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7225 for (j = 0; j < 2; ++j)
7226 if (EQ (old_buffer, echo_area_buffer[j]))
7227 echo_area_buffer[j] = echo_buffer[i];
7232 /* Call FN with args A1..A4 with either the current or last displayed
7233 echo_area_buffer as current buffer.
7235 WHICH zero means use the current message buffer
7236 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7237 from echo_buffer[] and clear it.
7239 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7240 suitable buffer from echo_buffer[] and clear it.
7242 Value is what FN returns. */
7244 static int
7245 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7246 struct window *w;
7247 int which;
7248 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7249 EMACS_INT a1;
7250 Lisp_Object a2;
7251 EMACS_INT a3, a4;
7253 Lisp_Object buffer;
7254 int this_one, the_other, clear_buffer_p, rc;
7255 int count = SPECPDL_INDEX ();
7257 /* If buffers aren't live, make new ones. */
7258 ensure_echo_area_buffers ();
7260 clear_buffer_p = 0;
7262 if (which == 0)
7263 this_one = 0, the_other = 1;
7264 else if (which > 0)
7265 this_one = 1, the_other = 0;
7267 /* Choose a suitable buffer from echo_buffer[] is we don't
7268 have one. */
7269 if (NILP (echo_area_buffer[this_one]))
7271 echo_area_buffer[this_one]
7272 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7273 ? echo_buffer[the_other]
7274 : echo_buffer[this_one]);
7275 clear_buffer_p = 1;
7278 buffer = echo_area_buffer[this_one];
7280 /* Don't get confused by reusing the buffer used for echoing
7281 for a different purpose. */
7282 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7283 cancel_echoing ();
7285 record_unwind_protect (unwind_with_echo_area_buffer,
7286 with_echo_area_buffer_unwind_data (w));
7288 /* Make the echo area buffer current. Note that for display
7289 purposes, it is not necessary that the displayed window's buffer
7290 == current_buffer, except for text property lookup. So, let's
7291 only set that buffer temporarily here without doing a full
7292 Fset_window_buffer. We must also change w->pointm, though,
7293 because otherwise an assertions in unshow_buffer fails, and Emacs
7294 aborts. */
7295 set_buffer_internal_1 (XBUFFER (buffer));
7296 if (w)
7298 w->buffer = buffer;
7299 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7302 current_buffer->undo_list = Qt;
7303 current_buffer->read_only = Qnil;
7304 specbind (Qinhibit_read_only, Qt);
7305 specbind (Qinhibit_modification_hooks, Qt);
7307 if (clear_buffer_p && Z > BEG)
7308 del_range (BEG, Z);
7310 xassert (BEGV >= BEG);
7311 xassert (ZV <= Z && ZV >= BEGV);
7313 rc = fn (a1, a2, a3, a4);
7315 xassert (BEGV >= BEG);
7316 xassert (ZV <= Z && ZV >= BEGV);
7318 unbind_to (count, Qnil);
7319 return rc;
7323 /* Save state that should be preserved around the call to the function
7324 FN called in with_echo_area_buffer. */
7326 static Lisp_Object
7327 with_echo_area_buffer_unwind_data (w)
7328 struct window *w;
7330 int i = 0;
7331 Lisp_Object vector;
7333 /* Reduce consing by keeping one vector in
7334 Vwith_echo_area_save_vector. */
7335 vector = Vwith_echo_area_save_vector;
7336 Vwith_echo_area_save_vector = Qnil;
7338 if (NILP (vector))
7339 vector = Fmake_vector (make_number (7), Qnil);
7341 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7342 AREF (vector, i) = Vdeactivate_mark, ++i;
7343 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7345 if (w)
7347 XSETWINDOW (AREF (vector, i), w); ++i;
7348 AREF (vector, i) = w->buffer; ++i;
7349 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7350 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7352 else
7354 int end = i + 4;
7355 for (; i < end; ++i)
7356 AREF (vector, i) = Qnil;
7359 xassert (i == ASIZE (vector));
7360 return vector;
7364 /* Restore global state from VECTOR which was created by
7365 with_echo_area_buffer_unwind_data. */
7367 static Lisp_Object
7368 unwind_with_echo_area_buffer (vector)
7369 Lisp_Object vector;
7371 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7372 Vdeactivate_mark = AREF (vector, 1);
7373 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7375 if (WINDOWP (AREF (vector, 3)))
7377 struct window *w;
7378 Lisp_Object buffer, charpos, bytepos;
7380 w = XWINDOW (AREF (vector, 3));
7381 buffer = AREF (vector, 4);
7382 charpos = AREF (vector, 5);
7383 bytepos = AREF (vector, 6);
7385 w->buffer = buffer;
7386 set_marker_both (w->pointm, buffer,
7387 XFASTINT (charpos), XFASTINT (bytepos));
7390 Vwith_echo_area_save_vector = vector;
7391 return Qnil;
7395 /* Set up the echo area for use by print functions. MULTIBYTE_P
7396 non-zero means we will print multibyte. */
7398 void
7399 setup_echo_area_for_printing (multibyte_p)
7400 int multibyte_p;
7402 /* If we can't find an echo area any more, exit. */
7403 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7404 Fkill_emacs (Qnil);
7406 ensure_echo_area_buffers ();
7408 if (!message_buf_print)
7410 /* A message has been output since the last time we printed.
7411 Choose a fresh echo area buffer. */
7412 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7413 echo_area_buffer[0] = echo_buffer[1];
7414 else
7415 echo_area_buffer[0] = echo_buffer[0];
7417 /* Switch to that buffer and clear it. */
7418 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7419 current_buffer->truncate_lines = Qnil;
7421 if (Z > BEG)
7423 int count = SPECPDL_INDEX ();
7424 specbind (Qinhibit_read_only, Qt);
7425 /* Note that undo recording is always disabled. */
7426 del_range (BEG, Z);
7427 unbind_to (count, Qnil);
7429 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7431 /* Set up the buffer for the multibyteness we need. */
7432 if (multibyte_p
7433 != !NILP (current_buffer->enable_multibyte_characters))
7434 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7436 /* Raise the frame containing the echo area. */
7437 if (minibuffer_auto_raise)
7439 struct frame *sf = SELECTED_FRAME ();
7440 Lisp_Object mini_window;
7441 mini_window = FRAME_MINIBUF_WINDOW (sf);
7442 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7445 message_log_maybe_newline ();
7446 message_buf_print = 1;
7448 else
7450 if (NILP (echo_area_buffer[0]))
7452 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7453 echo_area_buffer[0] = echo_buffer[1];
7454 else
7455 echo_area_buffer[0] = echo_buffer[0];
7458 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7460 /* Someone switched buffers between print requests. */
7461 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7462 current_buffer->truncate_lines = Qnil;
7468 /* Display an echo area message in window W. Value is non-zero if W's
7469 height is changed. If display_last_displayed_message_p is
7470 non-zero, display the message that was last displayed, otherwise
7471 display the current message. */
7473 static int
7474 display_echo_area (w)
7475 struct window *w;
7477 int i, no_message_p, window_height_changed_p, count;
7479 /* Temporarily disable garbage collections while displaying the echo
7480 area. This is done because a GC can print a message itself.
7481 That message would modify the echo area buffer's contents while a
7482 redisplay of the buffer is going on, and seriously confuse
7483 redisplay. */
7484 count = inhibit_garbage_collection ();
7486 /* If there is no message, we must call display_echo_area_1
7487 nevertheless because it resizes the window. But we will have to
7488 reset the echo_area_buffer in question to nil at the end because
7489 with_echo_area_buffer will sets it to an empty buffer. */
7490 i = display_last_displayed_message_p ? 1 : 0;
7491 no_message_p = NILP (echo_area_buffer[i]);
7493 window_height_changed_p
7494 = with_echo_area_buffer (w, display_last_displayed_message_p,
7495 display_echo_area_1,
7496 (EMACS_INT) w, Qnil, 0, 0);
7498 if (no_message_p)
7499 echo_area_buffer[i] = Qnil;
7501 unbind_to (count, Qnil);
7502 return window_height_changed_p;
7506 /* Helper for display_echo_area. Display the current buffer which
7507 contains the current echo area message in window W, a mini-window,
7508 a pointer to which is passed in A1. A2..A4 are currently not used.
7509 Change the height of W so that all of the message is displayed.
7510 Value is non-zero if height of W was changed. */
7512 static int
7513 display_echo_area_1 (a1, a2, a3, a4)
7514 EMACS_INT a1;
7515 Lisp_Object a2;
7516 EMACS_INT a3, a4;
7518 struct window *w = (struct window *) a1;
7519 Lisp_Object window;
7520 struct text_pos start;
7521 int window_height_changed_p = 0;
7523 /* Do this before displaying, so that we have a large enough glyph
7524 matrix for the display. */
7525 window_height_changed_p = resize_mini_window (w, 0);
7527 /* Display. */
7528 clear_glyph_matrix (w->desired_matrix);
7529 XSETWINDOW (window, w);
7530 SET_TEXT_POS (start, BEG, BEG_BYTE);
7531 try_window (window, start);
7533 return window_height_changed_p;
7537 /* Resize the echo area window to exactly the size needed for the
7538 currently displayed message, if there is one. If a mini-buffer
7539 is active, don't shrink it. */
7541 void
7542 resize_echo_area_exactly ()
7544 if (BUFFERP (echo_area_buffer[0])
7545 && WINDOWP (echo_area_window))
7547 struct window *w = XWINDOW (echo_area_window);
7548 int resized_p;
7549 Lisp_Object resize_exactly;
7551 if (minibuf_level == 0)
7552 resize_exactly = Qt;
7553 else
7554 resize_exactly = Qnil;
7556 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7557 (EMACS_INT) w, resize_exactly, 0, 0);
7558 if (resized_p)
7560 ++windows_or_buffers_changed;
7561 ++update_mode_lines;
7562 redisplay_internal (0);
7568 /* Callback function for with_echo_area_buffer, when used from
7569 resize_echo_area_exactly. A1 contains a pointer to the window to
7570 resize, EXACTLY non-nil means resize the mini-window exactly to the
7571 size of the text displayed. A3 and A4 are not used. Value is what
7572 resize_mini_window returns. */
7574 static int
7575 resize_mini_window_1 (a1, exactly, a3, a4)
7576 EMACS_INT a1;
7577 Lisp_Object exactly;
7578 EMACS_INT a3, a4;
7580 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7584 /* Resize mini-window W to fit the size of its contents. EXACT:P
7585 means size the window exactly to the size needed. Otherwise, it's
7586 only enlarged until W's buffer is empty. Value is non-zero if
7587 the window height has been changed. */
7590 resize_mini_window (w, exact_p)
7591 struct window *w;
7592 int exact_p;
7594 struct frame *f = XFRAME (w->frame);
7595 int window_height_changed_p = 0;
7597 xassert (MINI_WINDOW_P (w));
7599 /* Don't resize windows while redisplaying a window; it would
7600 confuse redisplay functions when the size of the window they are
7601 displaying changes from under them. Such a resizing can happen,
7602 for instance, when which-func prints a long message while
7603 we are running fontification-functions. We're running these
7604 functions with safe_call which binds inhibit-redisplay to t. */
7605 if (!NILP (Vinhibit_redisplay))
7606 return 0;
7608 /* Nil means don't try to resize. */
7609 if (NILP (Vresize_mini_windows)
7610 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7611 return 0;
7613 if (!FRAME_MINIBUF_ONLY_P (f))
7615 struct it it;
7616 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7617 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7618 int height, max_height;
7619 int unit = FRAME_LINE_HEIGHT (f);
7620 struct text_pos start;
7621 struct buffer *old_current_buffer = NULL;
7623 if (current_buffer != XBUFFER (w->buffer))
7625 old_current_buffer = current_buffer;
7626 set_buffer_internal (XBUFFER (w->buffer));
7629 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7631 /* Compute the max. number of lines specified by the user. */
7632 if (FLOATP (Vmax_mini_window_height))
7633 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7634 else if (INTEGERP (Vmax_mini_window_height))
7635 max_height = XINT (Vmax_mini_window_height);
7636 else
7637 max_height = total_height / 4;
7639 /* Correct that max. height if it's bogus. */
7640 max_height = max (1, max_height);
7641 max_height = min (total_height, max_height);
7643 /* Find out the height of the text in the window. */
7644 if (it.truncate_lines_p)
7645 height = 1;
7646 else
7648 last_height = 0;
7649 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7650 if (it.max_ascent == 0 && it.max_descent == 0)
7651 height = it.current_y + last_height;
7652 else
7653 height = it.current_y + it.max_ascent + it.max_descent;
7654 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7655 height = (height + unit - 1) / unit;
7658 /* Compute a suitable window start. */
7659 if (height > max_height)
7661 height = max_height;
7662 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7663 move_it_vertically_backward (&it, (height - 1) * unit);
7664 start = it.current.pos;
7666 else
7667 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7668 SET_MARKER_FROM_TEXT_POS (w->start, start);
7670 if (EQ (Vresize_mini_windows, Qgrow_only))
7672 /* Let it grow only, until we display an empty message, in which
7673 case the window shrinks again. */
7674 if (height > WINDOW_TOTAL_LINES (w))
7676 int old_height = WINDOW_TOTAL_LINES (w);
7677 freeze_window_starts (f, 1);
7678 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7679 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7681 else if (height < WINDOW_TOTAL_LINES (w)
7682 && (exact_p || BEGV == ZV))
7684 int old_height = WINDOW_TOTAL_LINES (w);
7685 freeze_window_starts (f, 0);
7686 shrink_mini_window (w);
7687 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7690 else
7692 /* Always resize to exact size needed. */
7693 if (height > WINDOW_TOTAL_LINES (w))
7695 int old_height = WINDOW_TOTAL_LINES (w);
7696 freeze_window_starts (f, 1);
7697 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7698 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7700 else if (height < WINDOW_TOTAL_LINES (w))
7702 int old_height = WINDOW_TOTAL_LINES (w);
7703 freeze_window_starts (f, 0);
7704 shrink_mini_window (w);
7706 if (height)
7708 freeze_window_starts (f, 1);
7709 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7712 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7716 if (old_current_buffer)
7717 set_buffer_internal (old_current_buffer);
7720 return window_height_changed_p;
7724 /* Value is the current message, a string, or nil if there is no
7725 current message. */
7727 Lisp_Object
7728 current_message ()
7730 Lisp_Object msg;
7732 if (NILP (echo_area_buffer[0]))
7733 msg = Qnil;
7734 else
7736 with_echo_area_buffer (0, 0, current_message_1,
7737 (EMACS_INT) &msg, Qnil, 0, 0);
7738 if (NILP (msg))
7739 echo_area_buffer[0] = Qnil;
7742 return msg;
7746 static int
7747 current_message_1 (a1, a2, a3, a4)
7748 EMACS_INT a1;
7749 Lisp_Object a2;
7750 EMACS_INT a3, a4;
7752 Lisp_Object *msg = (Lisp_Object *) a1;
7754 if (Z > BEG)
7755 *msg = make_buffer_string (BEG, Z, 1);
7756 else
7757 *msg = Qnil;
7758 return 0;
7762 /* Push the current message on Vmessage_stack for later restauration
7763 by restore_message. Value is non-zero if the current message isn't
7764 empty. This is a relatively infrequent operation, so it's not
7765 worth optimizing. */
7768 push_message ()
7770 Lisp_Object msg;
7771 msg = current_message ();
7772 Vmessage_stack = Fcons (msg, Vmessage_stack);
7773 return STRINGP (msg);
7777 /* Restore message display from the top of Vmessage_stack. */
7779 void
7780 restore_message ()
7782 Lisp_Object msg;
7784 xassert (CONSP (Vmessage_stack));
7785 msg = XCAR (Vmessage_stack);
7786 if (STRINGP (msg))
7787 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7788 else
7789 message3_nolog (msg, 0, 0);
7793 /* Handler for record_unwind_protect calling pop_message. */
7795 Lisp_Object
7796 pop_message_unwind (dummy)
7797 Lisp_Object dummy;
7799 pop_message ();
7800 return Qnil;
7803 /* Pop the top-most entry off Vmessage_stack. */
7805 void
7806 pop_message ()
7808 xassert (CONSP (Vmessage_stack));
7809 Vmessage_stack = XCDR (Vmessage_stack);
7813 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7814 exits. If the stack is not empty, we have a missing pop_message
7815 somewhere. */
7817 void
7818 check_message_stack ()
7820 if (!NILP (Vmessage_stack))
7821 abort ();
7825 /* Truncate to NCHARS what will be displayed in the echo area the next
7826 time we display it---but don't redisplay it now. */
7828 void
7829 truncate_echo_area (nchars)
7830 int nchars;
7832 if (nchars == 0)
7833 echo_area_buffer[0] = Qnil;
7834 /* A null message buffer means that the frame hasn't really been
7835 initialized yet. Error messages get reported properly by
7836 cmd_error, so this must be just an informative message; toss it. */
7837 else if (!noninteractive
7838 && INTERACTIVE
7839 && !NILP (echo_area_buffer[0]))
7841 struct frame *sf = SELECTED_FRAME ();
7842 if (FRAME_MESSAGE_BUF (sf))
7843 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7848 /* Helper function for truncate_echo_area. Truncate the current
7849 message to at most NCHARS characters. */
7851 static int
7852 truncate_message_1 (nchars, a2, a3, a4)
7853 EMACS_INT nchars;
7854 Lisp_Object a2;
7855 EMACS_INT a3, a4;
7857 if (BEG + nchars < Z)
7858 del_range (BEG + nchars, Z);
7859 if (Z == BEG)
7860 echo_area_buffer[0] = Qnil;
7861 return 0;
7865 /* Set the current message to a substring of S or STRING.
7867 If STRING is a Lisp string, set the message to the first NBYTES
7868 bytes from STRING. NBYTES zero means use the whole string. If
7869 STRING is multibyte, the message will be displayed multibyte.
7871 If S is not null, set the message to the first LEN bytes of S. LEN
7872 zero means use the whole string. MULTIBYTE_P non-zero means S is
7873 multibyte. Display the message multibyte in that case. */
7875 void
7876 set_message (s, string, nbytes, multibyte_p)
7877 const char *s;
7878 Lisp_Object string;
7879 int nbytes, multibyte_p;
7881 message_enable_multibyte
7882 = ((s && multibyte_p)
7883 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7885 with_echo_area_buffer (0, 0, set_message_1,
7886 (EMACS_INT) s, string, nbytes, multibyte_p);
7887 message_buf_print = 0;
7888 help_echo_showing_p = 0;
7892 /* Helper function for set_message. Arguments have the same meaning
7893 as there, with A1 corresponding to S and A2 corresponding to STRING
7894 This function is called with the echo area buffer being
7895 current. */
7897 static int
7898 set_message_1 (a1, a2, nbytes, multibyte_p)
7899 EMACS_INT a1;
7900 Lisp_Object a2;
7901 EMACS_INT nbytes, multibyte_p;
7903 const char *s = (const char *) a1;
7904 Lisp_Object string = a2;
7906 /* Change multibyteness of the echo buffer appropriately. */
7907 if (message_enable_multibyte
7908 != !NILP (current_buffer->enable_multibyte_characters))
7909 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7911 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7913 /* Insert new message at BEG. */
7914 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7915 Ferase_buffer ();
7917 if (STRINGP (string))
7919 int nchars;
7921 if (nbytes == 0)
7922 nbytes = SBYTES (string);
7923 nchars = string_byte_to_char (string, nbytes);
7925 /* This function takes care of single/multibyte conversion. We
7926 just have to ensure that the echo area buffer has the right
7927 setting of enable_multibyte_characters. */
7928 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7930 else if (s)
7932 if (nbytes == 0)
7933 nbytes = strlen (s);
7935 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7937 /* Convert from multi-byte to single-byte. */
7938 int i, c, n;
7939 unsigned char work[1];
7941 /* Convert a multibyte string to single-byte. */
7942 for (i = 0; i < nbytes; i += n)
7944 c = string_char_and_length (s + i, nbytes - i, &n);
7945 work[0] = (SINGLE_BYTE_CHAR_P (c)
7947 : multibyte_char_to_unibyte (c, Qnil));
7948 insert_1_both (work, 1, 1, 1, 0, 0);
7951 else if (!multibyte_p
7952 && !NILP (current_buffer->enable_multibyte_characters))
7954 /* Convert from single-byte to multi-byte. */
7955 int i, c, n;
7956 const unsigned char *msg = (const unsigned char *) s;
7957 unsigned char str[MAX_MULTIBYTE_LENGTH];
7959 /* Convert a single-byte string to multibyte. */
7960 for (i = 0; i < nbytes; i++)
7962 c = unibyte_char_to_multibyte (msg[i]);
7963 n = CHAR_STRING (c, str);
7964 insert_1_both (str, 1, n, 1, 0, 0);
7967 else
7968 insert_1 (s, nbytes, 1, 0, 0);
7971 return 0;
7975 /* Clear messages. CURRENT_P non-zero means clear the current
7976 message. LAST_DISPLAYED_P non-zero means clear the message
7977 last displayed. */
7979 void
7980 clear_message (current_p, last_displayed_p)
7981 int current_p, last_displayed_p;
7983 if (current_p)
7985 echo_area_buffer[0] = Qnil;
7986 message_cleared_p = 1;
7989 if (last_displayed_p)
7990 echo_area_buffer[1] = Qnil;
7992 message_buf_print = 0;
7995 /* Clear garbaged frames.
7997 This function is used where the old redisplay called
7998 redraw_garbaged_frames which in turn called redraw_frame which in
7999 turn called clear_frame. The call to clear_frame was a source of
8000 flickering. I believe a clear_frame is not necessary. It should
8001 suffice in the new redisplay to invalidate all current matrices,
8002 and ensure a complete redisplay of all windows. */
8004 static void
8005 clear_garbaged_frames ()
8007 if (frame_garbaged)
8009 Lisp_Object tail, frame;
8010 int changed_count = 0;
8012 FOR_EACH_FRAME (tail, frame)
8014 struct frame *f = XFRAME (frame);
8016 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8018 if (f->resized_p)
8020 Fredraw_frame (frame);
8021 f->force_flush_display_p = 1;
8023 clear_current_matrices (f);
8024 changed_count++;
8025 f->garbaged = 0;
8026 f->resized_p = 0;
8030 frame_garbaged = 0;
8031 if (changed_count)
8032 ++windows_or_buffers_changed;
8037 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8038 is non-zero update selected_frame. Value is non-zero if the
8039 mini-windows height has been changed. */
8041 static int
8042 echo_area_display (update_frame_p)
8043 int update_frame_p;
8045 Lisp_Object mini_window;
8046 struct window *w;
8047 struct frame *f;
8048 int window_height_changed_p = 0;
8049 struct frame *sf = SELECTED_FRAME ();
8051 mini_window = FRAME_MINIBUF_WINDOW (sf);
8052 w = XWINDOW (mini_window);
8053 f = XFRAME (WINDOW_FRAME (w));
8055 /* Don't display if frame is invisible or not yet initialized. */
8056 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8057 return 0;
8059 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8060 #ifndef MAC_OS8
8061 #ifdef HAVE_WINDOW_SYSTEM
8062 /* When Emacs starts, selected_frame may be a visible terminal
8063 frame, even if we run under a window system. If we let this
8064 through, a message would be displayed on the terminal. */
8065 if (EQ (selected_frame, Vterminal_frame)
8066 && !NILP (Vwindow_system))
8067 return 0;
8068 #endif /* HAVE_WINDOW_SYSTEM */
8069 #endif
8071 /* Redraw garbaged frames. */
8072 if (frame_garbaged)
8073 clear_garbaged_frames ();
8075 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8077 echo_area_window = mini_window;
8078 window_height_changed_p = display_echo_area (w);
8079 w->must_be_updated_p = 1;
8081 /* Update the display, unless called from redisplay_internal.
8082 Also don't update the screen during redisplay itself. The
8083 update will happen at the end of redisplay, and an update
8084 here could cause confusion. */
8085 if (update_frame_p && !redisplaying_p)
8087 int n = 0;
8089 /* If the display update has been interrupted by pending
8090 input, update mode lines in the frame. Due to the
8091 pending input, it might have been that redisplay hasn't
8092 been called, so that mode lines above the echo area are
8093 garbaged. This looks odd, so we prevent it here. */
8094 if (!display_completed)
8095 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8097 if (window_height_changed_p
8098 /* Don't do this if Emacs is shutting down. Redisplay
8099 needs to run hooks. */
8100 && !NILP (Vrun_hooks))
8102 /* Must update other windows. Likewise as in other
8103 cases, don't let this update be interrupted by
8104 pending input. */
8105 int count = SPECPDL_INDEX ();
8106 specbind (Qredisplay_dont_pause, Qt);
8107 windows_or_buffers_changed = 1;
8108 redisplay_internal (0);
8109 unbind_to (count, Qnil);
8111 else if (FRAME_WINDOW_P (f) && n == 0)
8113 /* Window configuration is the same as before.
8114 Can do with a display update of the echo area,
8115 unless we displayed some mode lines. */
8116 update_single_window (w, 1);
8117 rif->flush_display (f);
8119 else
8120 update_frame (f, 1, 1);
8122 /* If cursor is in the echo area, make sure that the next
8123 redisplay displays the minibuffer, so that the cursor will
8124 be replaced with what the minibuffer wants. */
8125 if (cursor_in_echo_area)
8126 ++windows_or_buffers_changed;
8129 else if (!EQ (mini_window, selected_window))
8130 windows_or_buffers_changed++;
8132 /* The current message is now also the last one displayed. */
8133 echo_area_buffer[1] = echo_area_buffer[0];
8135 /* Prevent redisplay optimization in redisplay_internal by resetting
8136 this_line_start_pos. This is done because the mini-buffer now
8137 displays the message instead of its buffer text. */
8138 if (EQ (mini_window, selected_window))
8139 CHARPOS (this_line_start_pos) = 0;
8141 return window_height_changed_p;
8146 /***********************************************************************
8147 Frame Titles
8148 ***********************************************************************/
8151 /* The frame title buffering code is also used by Fformat_mode_line.
8152 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8154 /* A buffer for constructing frame titles in it; allocated from the
8155 heap in init_xdisp and resized as needed in store_frame_title_char. */
8157 static char *frame_title_buf;
8159 /* The buffer's end, and a current output position in it. */
8161 static char *frame_title_buf_end;
8162 static char *frame_title_ptr;
8165 /* Store a single character C for the frame title in frame_title_buf.
8166 Re-allocate frame_title_buf if necessary. */
8168 static void
8169 #ifdef PROTOTYPES
8170 store_frame_title_char (char c)
8171 #else
8172 store_frame_title_char (c)
8173 char c;
8174 #endif
8176 /* If output position has reached the end of the allocated buffer,
8177 double the buffer's size. */
8178 if (frame_title_ptr == frame_title_buf_end)
8180 int len = frame_title_ptr - frame_title_buf;
8181 int new_size = 2 * len * sizeof *frame_title_buf;
8182 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8183 frame_title_buf_end = frame_title_buf + new_size;
8184 frame_title_ptr = frame_title_buf + len;
8187 *frame_title_ptr++ = c;
8191 /* Store part of a frame title in frame_title_buf, beginning at
8192 frame_title_ptr. STR is the string to store. Do not copy
8193 characters that yield more columns than PRECISION; PRECISION <= 0
8194 means copy the whole string. Pad with spaces until FIELD_WIDTH
8195 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8196 pad. Called from display_mode_element when it is used to build a
8197 frame title. */
8199 static int
8200 store_frame_title (str, field_width, precision)
8201 const unsigned char *str;
8202 int field_width, precision;
8204 int n = 0;
8205 int dummy, nbytes;
8207 /* Copy at most PRECISION chars from STR. */
8208 nbytes = strlen (str);
8209 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8210 while (nbytes--)
8211 store_frame_title_char (*str++);
8213 /* Fill up with spaces until FIELD_WIDTH reached. */
8214 while (field_width > 0
8215 && n < field_width)
8217 store_frame_title_char (' ');
8218 ++n;
8221 return n;
8224 #ifdef HAVE_WINDOW_SYSTEM
8226 /* Set the title of FRAME, if it has changed. The title format is
8227 Vicon_title_format if FRAME is iconified, otherwise it is
8228 frame_title_format. */
8230 static void
8231 x_consider_frame_title (frame)
8232 Lisp_Object frame;
8234 struct frame *f = XFRAME (frame);
8236 if (FRAME_WINDOW_P (f)
8237 || FRAME_MINIBUF_ONLY_P (f)
8238 || f->explicit_name)
8240 /* Do we have more than one visible frame on this X display? */
8241 Lisp_Object tail;
8242 Lisp_Object fmt;
8243 struct buffer *obuf;
8244 int len;
8245 struct it it;
8247 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8249 Lisp_Object other_frame = XCAR (tail);
8250 struct frame *tf = XFRAME (other_frame);
8252 if (tf != f
8253 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8254 && !FRAME_MINIBUF_ONLY_P (tf)
8255 && !EQ (other_frame, tip_frame)
8256 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8257 break;
8260 /* Set global variable indicating that multiple frames exist. */
8261 multiple_frames = CONSP (tail);
8263 /* Switch to the buffer of selected window of the frame. Set up
8264 frame_title_ptr so that display_mode_element will output into it;
8265 then display the title. */
8266 obuf = current_buffer;
8267 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8268 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8269 frame_title_ptr = frame_title_buf;
8270 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8271 NULL, DEFAULT_FACE_ID);
8272 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8273 len = frame_title_ptr - frame_title_buf;
8274 frame_title_ptr = NULL;
8275 set_buffer_internal_1 (obuf);
8277 /* Set the title only if it's changed. This avoids consing in
8278 the common case where it hasn't. (If it turns out that we've
8279 already wasted too much time by walking through the list with
8280 display_mode_element, then we might need to optimize at a
8281 higher level than this.) */
8282 if (! STRINGP (f->name)
8283 || SBYTES (f->name) != len
8284 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8285 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8289 #endif /* not HAVE_WINDOW_SYSTEM */
8294 /***********************************************************************
8295 Menu Bars
8296 ***********************************************************************/
8299 /* Prepare for redisplay by updating menu-bar item lists when
8300 appropriate. This can call eval. */
8302 void
8303 prepare_menu_bars ()
8305 int all_windows;
8306 struct gcpro gcpro1, gcpro2;
8307 struct frame *f;
8308 Lisp_Object tooltip_frame;
8310 #ifdef HAVE_WINDOW_SYSTEM
8311 tooltip_frame = tip_frame;
8312 #else
8313 tooltip_frame = Qnil;
8314 #endif
8316 /* Update all frame titles based on their buffer names, etc. We do
8317 this before the menu bars so that the buffer-menu will show the
8318 up-to-date frame titles. */
8319 #ifdef HAVE_WINDOW_SYSTEM
8320 if (windows_or_buffers_changed || update_mode_lines)
8322 Lisp_Object tail, frame;
8324 FOR_EACH_FRAME (tail, frame)
8326 f = XFRAME (frame);
8327 if (!EQ (frame, tooltip_frame)
8328 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8329 x_consider_frame_title (frame);
8332 #endif /* HAVE_WINDOW_SYSTEM */
8334 /* Update the menu bar item lists, if appropriate. This has to be
8335 done before any actual redisplay or generation of display lines. */
8336 all_windows = (update_mode_lines
8337 || buffer_shared > 1
8338 || windows_or_buffers_changed);
8339 if (all_windows)
8341 Lisp_Object tail, frame;
8342 int count = SPECPDL_INDEX ();
8344 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8346 FOR_EACH_FRAME (tail, frame)
8348 f = XFRAME (frame);
8350 /* Ignore tooltip frame. */
8351 if (EQ (frame, tooltip_frame))
8352 continue;
8354 /* If a window on this frame changed size, report that to
8355 the user and clear the size-change flag. */
8356 if (FRAME_WINDOW_SIZES_CHANGED (f))
8358 Lisp_Object functions;
8360 /* Clear flag first in case we get an error below. */
8361 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8362 functions = Vwindow_size_change_functions;
8363 GCPRO2 (tail, functions);
8365 while (CONSP (functions))
8367 call1 (XCAR (functions), frame);
8368 functions = XCDR (functions);
8370 UNGCPRO;
8373 GCPRO1 (tail);
8374 update_menu_bar (f, 0);
8375 #ifdef HAVE_WINDOW_SYSTEM
8376 update_tool_bar (f, 0);
8377 #endif
8378 UNGCPRO;
8381 unbind_to (count, Qnil);
8383 else
8385 struct frame *sf = SELECTED_FRAME ();
8386 update_menu_bar (sf, 1);
8387 #ifdef HAVE_WINDOW_SYSTEM
8388 update_tool_bar (sf, 1);
8389 #endif
8392 /* Motif needs this. See comment in xmenu.c. Turn it off when
8393 pending_menu_activation is not defined. */
8394 #ifdef USE_X_TOOLKIT
8395 pending_menu_activation = 0;
8396 #endif
8400 /* Update the menu bar item list for frame F. This has to be done
8401 before we start to fill in any display lines, because it can call
8402 eval.
8404 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8406 static void
8407 update_menu_bar (f, save_match_data)
8408 struct frame *f;
8409 int save_match_data;
8411 Lisp_Object window;
8412 register struct window *w;
8414 /* If called recursively during a menu update, do nothing. This can
8415 happen when, for instance, an activate-menubar-hook causes a
8416 redisplay. */
8417 if (inhibit_menubar_update)
8418 return;
8420 window = FRAME_SELECTED_WINDOW (f);
8421 w = XWINDOW (window);
8423 #if 0 /* The if statement below this if statement used to include the
8424 condition !NILP (w->update_mode_line), rather than using
8425 update_mode_lines directly, and this if statement may have
8426 been added to make that condition work. Now the if
8427 statement below matches its comment, this isn't needed. */
8428 if (update_mode_lines)
8429 w->update_mode_line = Qt;
8430 #endif
8432 if (FRAME_WINDOW_P (f)
8434 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8435 || defined (USE_GTK)
8436 FRAME_EXTERNAL_MENU_BAR (f)
8437 #else
8438 FRAME_MENU_BAR_LINES (f) > 0
8439 #endif
8440 : FRAME_MENU_BAR_LINES (f) > 0)
8442 /* If the user has switched buffers or windows, we need to
8443 recompute to reflect the new bindings. But we'll
8444 recompute when update_mode_lines is set too; that means
8445 that people can use force-mode-line-update to request
8446 that the menu bar be recomputed. The adverse effect on
8447 the rest of the redisplay algorithm is about the same as
8448 windows_or_buffers_changed anyway. */
8449 if (windows_or_buffers_changed
8450 /* This used to test w->update_mode_line, but we believe
8451 there is no need to recompute the menu in that case. */
8452 || update_mode_lines
8453 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8454 < BUF_MODIFF (XBUFFER (w->buffer)))
8455 != !NILP (w->last_had_star))
8456 || ((!NILP (Vtransient_mark_mode)
8457 && !NILP (XBUFFER (w->buffer)->mark_active))
8458 != !NILP (w->region_showing)))
8460 struct buffer *prev = current_buffer;
8461 int count = SPECPDL_INDEX ();
8463 specbind (Qinhibit_menubar_update, Qt);
8465 set_buffer_internal_1 (XBUFFER (w->buffer));
8466 if (save_match_data)
8467 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8468 if (NILP (Voverriding_local_map_menu_flag))
8470 specbind (Qoverriding_terminal_local_map, Qnil);
8471 specbind (Qoverriding_local_map, Qnil);
8474 /* Run the Lucid hook. */
8475 safe_run_hooks (Qactivate_menubar_hook);
8477 /* If it has changed current-menubar from previous value,
8478 really recompute the menu-bar from the value. */
8479 if (! NILP (Vlucid_menu_bar_dirty_flag))
8480 call0 (Qrecompute_lucid_menubar);
8482 safe_run_hooks (Qmenu_bar_update_hook);
8483 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8485 /* Redisplay the menu bar in case we changed it. */
8486 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8487 || defined (USE_GTK)
8488 if (FRAME_WINDOW_P (f)
8489 #if defined (MAC_OS)
8490 /* All frames on Mac OS share the same menubar. So only the
8491 selected frame should be allowed to set it. */
8492 && f == SELECTED_FRAME ()
8493 #endif
8495 set_frame_menubar (f, 0, 0);
8496 else
8497 /* On a terminal screen, the menu bar is an ordinary screen
8498 line, and this makes it get updated. */
8499 w->update_mode_line = Qt;
8500 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8501 /* In the non-toolkit version, the menu bar is an ordinary screen
8502 line, and this makes it get updated. */
8503 w->update_mode_line = Qt;
8504 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8506 unbind_to (count, Qnil);
8507 set_buffer_internal_1 (prev);
8514 /***********************************************************************
8515 Output Cursor
8516 ***********************************************************************/
8518 #ifdef HAVE_WINDOW_SYSTEM
8520 /* EXPORT:
8521 Nominal cursor position -- where to draw output.
8522 HPOS and VPOS are window relative glyph matrix coordinates.
8523 X and Y are window relative pixel coordinates. */
8525 struct cursor_pos output_cursor;
8528 /* EXPORT:
8529 Set the global variable output_cursor to CURSOR. All cursor
8530 positions are relative to updated_window. */
8532 void
8533 set_output_cursor (cursor)
8534 struct cursor_pos *cursor;
8536 output_cursor.hpos = cursor->hpos;
8537 output_cursor.vpos = cursor->vpos;
8538 output_cursor.x = cursor->x;
8539 output_cursor.y = cursor->y;
8543 /* EXPORT for RIF:
8544 Set a nominal cursor position.
8546 HPOS and VPOS are column/row positions in a window glyph matrix. X
8547 and Y are window text area relative pixel positions.
8549 If this is done during an update, updated_window will contain the
8550 window that is being updated and the position is the future output
8551 cursor position for that window. If updated_window is null, use
8552 selected_window and display the cursor at the given position. */
8554 void
8555 x_cursor_to (vpos, hpos, y, x)
8556 int vpos, hpos, y, x;
8558 struct window *w;
8560 /* If updated_window is not set, work on selected_window. */
8561 if (updated_window)
8562 w = updated_window;
8563 else
8564 w = XWINDOW (selected_window);
8566 /* Set the output cursor. */
8567 output_cursor.hpos = hpos;
8568 output_cursor.vpos = vpos;
8569 output_cursor.x = x;
8570 output_cursor.y = y;
8572 /* If not called as part of an update, really display the cursor.
8573 This will also set the cursor position of W. */
8574 if (updated_window == NULL)
8576 BLOCK_INPUT;
8577 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8578 if (rif->flush_display_optional)
8579 rif->flush_display_optional (SELECTED_FRAME ());
8580 UNBLOCK_INPUT;
8584 #endif /* HAVE_WINDOW_SYSTEM */
8587 /***********************************************************************
8588 Tool-bars
8589 ***********************************************************************/
8591 #ifdef HAVE_WINDOW_SYSTEM
8593 /* Where the mouse was last time we reported a mouse event. */
8595 FRAME_PTR last_mouse_frame;
8597 /* Tool-bar item index of the item on which a mouse button was pressed
8598 or -1. */
8600 int last_tool_bar_item;
8603 /* Update the tool-bar item list for frame F. This has to be done
8604 before we start to fill in any display lines. Called from
8605 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8606 and restore it here. */
8608 static void
8609 update_tool_bar (f, save_match_data)
8610 struct frame *f;
8611 int save_match_data;
8613 #ifdef USE_GTK
8614 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8615 #else
8616 int do_update = WINDOWP (f->tool_bar_window)
8617 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8618 #endif
8620 if (do_update)
8622 Lisp_Object window;
8623 struct window *w;
8625 window = FRAME_SELECTED_WINDOW (f);
8626 w = XWINDOW (window);
8628 /* If the user has switched buffers or windows, we need to
8629 recompute to reflect the new bindings. But we'll
8630 recompute when update_mode_lines is set too; that means
8631 that people can use force-mode-line-update to request
8632 that the menu bar be recomputed. The adverse effect on
8633 the rest of the redisplay algorithm is about the same as
8634 windows_or_buffers_changed anyway. */
8635 if (windows_or_buffers_changed
8636 || !NILP (w->update_mode_line)
8637 || update_mode_lines
8638 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8639 < BUF_MODIFF (XBUFFER (w->buffer)))
8640 != !NILP (w->last_had_star))
8641 || ((!NILP (Vtransient_mark_mode)
8642 && !NILP (XBUFFER (w->buffer)->mark_active))
8643 != !NILP (w->region_showing)))
8645 struct buffer *prev = current_buffer;
8646 int count = SPECPDL_INDEX ();
8647 Lisp_Object new_tool_bar;
8648 int new_n_tool_bar;
8649 struct gcpro gcpro1;
8651 /* Set current_buffer to the buffer of the selected
8652 window of the frame, so that we get the right local
8653 keymaps. */
8654 set_buffer_internal_1 (XBUFFER (w->buffer));
8656 /* Save match data, if we must. */
8657 if (save_match_data)
8658 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8660 /* Make sure that we don't accidentally use bogus keymaps. */
8661 if (NILP (Voverriding_local_map_menu_flag))
8663 specbind (Qoverriding_terminal_local_map, Qnil);
8664 specbind (Qoverriding_local_map, Qnil);
8667 GCPRO1 (new_tool_bar);
8669 /* Build desired tool-bar items from keymaps. */
8670 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8671 &new_n_tool_bar);
8673 /* Redisplay the tool-bar if we changed it. */
8674 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8676 /* Redisplay that happens asynchronously due to an expose event
8677 may access f->tool_bar_items. Make sure we update both
8678 variables within BLOCK_INPUT so no such event interrupts. */
8679 BLOCK_INPUT;
8680 f->tool_bar_items = new_tool_bar;
8681 f->n_tool_bar_items = new_n_tool_bar;
8682 w->update_mode_line = Qt;
8683 UNBLOCK_INPUT;
8686 UNGCPRO;
8688 unbind_to (count, Qnil);
8689 set_buffer_internal_1 (prev);
8695 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8696 F's desired tool-bar contents. F->tool_bar_items must have
8697 been set up previously by calling prepare_menu_bars. */
8699 static void
8700 build_desired_tool_bar_string (f)
8701 struct frame *f;
8703 int i, size, size_needed;
8704 struct gcpro gcpro1, gcpro2, gcpro3;
8705 Lisp_Object image, plist, props;
8707 image = plist = props = Qnil;
8708 GCPRO3 (image, plist, props);
8710 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8711 Otherwise, make a new string. */
8713 /* The size of the string we might be able to reuse. */
8714 size = (STRINGP (f->desired_tool_bar_string)
8715 ? SCHARS (f->desired_tool_bar_string)
8716 : 0);
8718 /* We need one space in the string for each image. */
8719 size_needed = f->n_tool_bar_items;
8721 /* Reuse f->desired_tool_bar_string, if possible. */
8722 if (size < size_needed || NILP (f->desired_tool_bar_string))
8723 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8724 make_number (' '));
8725 else
8727 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8728 Fremove_text_properties (make_number (0), make_number (size),
8729 props, f->desired_tool_bar_string);
8732 /* Put a `display' property on the string for the images to display,
8733 put a `menu_item' property on tool-bar items with a value that
8734 is the index of the item in F's tool-bar item vector. */
8735 for (i = 0; i < f->n_tool_bar_items; ++i)
8737 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8739 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8740 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8741 int hmargin, vmargin, relief, idx, end;
8742 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8744 /* If image is a vector, choose the image according to the
8745 button state. */
8746 image = PROP (TOOL_BAR_ITEM_IMAGES);
8747 if (VECTORP (image))
8749 if (enabled_p)
8750 idx = (selected_p
8751 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8752 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8753 else
8754 idx = (selected_p
8755 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8756 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8758 xassert (ASIZE (image) >= idx);
8759 image = AREF (image, idx);
8761 else
8762 idx = -1;
8764 /* Ignore invalid image specifications. */
8765 if (!valid_image_p (image))
8766 continue;
8768 /* Display the tool-bar button pressed, or depressed. */
8769 plist = Fcopy_sequence (XCDR (image));
8771 /* Compute margin and relief to draw. */
8772 relief = (tool_bar_button_relief >= 0
8773 ? tool_bar_button_relief
8774 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8775 hmargin = vmargin = relief;
8777 if (INTEGERP (Vtool_bar_button_margin)
8778 && XINT (Vtool_bar_button_margin) > 0)
8780 hmargin += XFASTINT (Vtool_bar_button_margin);
8781 vmargin += XFASTINT (Vtool_bar_button_margin);
8783 else if (CONSP (Vtool_bar_button_margin))
8785 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8786 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8787 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8789 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8790 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8791 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8794 if (auto_raise_tool_bar_buttons_p)
8796 /* Add a `:relief' property to the image spec if the item is
8797 selected. */
8798 if (selected_p)
8800 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8801 hmargin -= relief;
8802 vmargin -= relief;
8805 else
8807 /* If image is selected, display it pressed, i.e. with a
8808 negative relief. If it's not selected, display it with a
8809 raised relief. */
8810 plist = Fplist_put (plist, QCrelief,
8811 (selected_p
8812 ? make_number (-relief)
8813 : make_number (relief)));
8814 hmargin -= relief;
8815 vmargin -= relief;
8818 /* Put a margin around the image. */
8819 if (hmargin || vmargin)
8821 if (hmargin == vmargin)
8822 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8823 else
8824 plist = Fplist_put (plist, QCmargin,
8825 Fcons (make_number (hmargin),
8826 make_number (vmargin)));
8829 /* If button is not enabled, and we don't have special images
8830 for the disabled state, make the image appear disabled by
8831 applying an appropriate algorithm to it. */
8832 if (!enabled_p && idx < 0)
8833 plist = Fplist_put (plist, QCconversion, Qdisabled);
8835 /* Put a `display' text property on the string for the image to
8836 display. Put a `menu-item' property on the string that gives
8837 the start of this item's properties in the tool-bar items
8838 vector. */
8839 image = Fcons (Qimage, plist);
8840 props = list4 (Qdisplay, image,
8841 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8843 /* Let the last image hide all remaining spaces in the tool bar
8844 string. The string can be longer than needed when we reuse a
8845 previous string. */
8846 if (i + 1 == f->n_tool_bar_items)
8847 end = SCHARS (f->desired_tool_bar_string);
8848 else
8849 end = i + 1;
8850 Fadd_text_properties (make_number (i), make_number (end),
8851 props, f->desired_tool_bar_string);
8852 #undef PROP
8855 UNGCPRO;
8859 /* Display one line of the tool-bar of frame IT->f. */
8861 static void
8862 display_tool_bar_line (it)
8863 struct it *it;
8865 struct glyph_row *row = it->glyph_row;
8866 int max_x = it->last_visible_x;
8867 struct glyph *last;
8869 prepare_desired_row (row);
8870 row->y = it->current_y;
8872 /* Note that this isn't made use of if the face hasn't a box,
8873 so there's no need to check the face here. */
8874 it->start_of_box_run_p = 1;
8876 while (it->current_x < max_x)
8878 int x_before, x, n_glyphs_before, i, nglyphs;
8880 /* Get the next display element. */
8881 if (!get_next_display_element (it))
8882 break;
8884 /* Produce glyphs. */
8885 x_before = it->current_x;
8886 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8887 PRODUCE_GLYPHS (it);
8889 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8890 i = 0;
8891 x = x_before;
8892 while (i < nglyphs)
8894 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8896 if (x + glyph->pixel_width > max_x)
8898 /* Glyph doesn't fit on line. */
8899 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8900 it->current_x = x;
8901 goto out;
8904 ++it->hpos;
8905 x += glyph->pixel_width;
8906 ++i;
8909 /* Stop at line ends. */
8910 if (ITERATOR_AT_END_OF_LINE_P (it))
8911 break;
8913 set_iterator_to_next (it, 1);
8916 out:;
8918 row->displays_text_p = row->used[TEXT_AREA] != 0;
8919 extend_face_to_end_of_line (it);
8920 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8921 last->right_box_line_p = 1;
8922 if (last == row->glyphs[TEXT_AREA])
8923 last->left_box_line_p = 1;
8924 compute_line_metrics (it);
8926 /* If line is empty, make it occupy the rest of the tool-bar. */
8927 if (!row->displays_text_p)
8929 row->height = row->phys_height = it->last_visible_y - row->y;
8930 row->ascent = row->phys_ascent = 0;
8931 row->extra_line_spacing = 0;
8934 row->full_width_p = 1;
8935 row->continued_p = 0;
8936 row->truncated_on_left_p = 0;
8937 row->truncated_on_right_p = 0;
8939 it->current_x = it->hpos = 0;
8940 it->current_y += row->height;
8941 ++it->vpos;
8942 ++it->glyph_row;
8946 /* Value is the number of screen lines needed to make all tool-bar
8947 items of frame F visible. */
8949 static int
8950 tool_bar_lines_needed (f)
8951 struct frame *f;
8953 struct window *w = XWINDOW (f->tool_bar_window);
8954 struct it it;
8956 /* Initialize an iterator for iteration over
8957 F->desired_tool_bar_string in the tool-bar window of frame F. */
8958 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8959 it.first_visible_x = 0;
8960 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8961 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8963 while (!ITERATOR_AT_END_P (&it))
8965 it.glyph_row = w->desired_matrix->rows;
8966 clear_glyph_row (it.glyph_row);
8967 display_tool_bar_line (&it);
8970 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8974 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8975 0, 1, 0,
8976 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8977 (frame)
8978 Lisp_Object frame;
8980 struct frame *f;
8981 struct window *w;
8982 int nlines = 0;
8984 if (NILP (frame))
8985 frame = selected_frame;
8986 else
8987 CHECK_FRAME (frame);
8988 f = XFRAME (frame);
8990 if (WINDOWP (f->tool_bar_window)
8991 || (w = XWINDOW (f->tool_bar_window),
8992 WINDOW_TOTAL_LINES (w) > 0))
8994 update_tool_bar (f, 1);
8995 if (f->n_tool_bar_items)
8997 build_desired_tool_bar_string (f);
8998 nlines = tool_bar_lines_needed (f);
9002 return make_number (nlines);
9006 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9007 height should be changed. */
9009 static int
9010 redisplay_tool_bar (f)
9011 struct frame *f;
9013 struct window *w;
9014 struct it it;
9015 struct glyph_row *row;
9016 int change_height_p = 0;
9018 #ifdef USE_GTK
9019 if (FRAME_EXTERNAL_TOOL_BAR (f))
9020 update_frame_tool_bar (f);
9021 return 0;
9022 #endif
9024 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9025 do anything. This means you must start with tool-bar-lines
9026 non-zero to get the auto-sizing effect. Or in other words, you
9027 can turn off tool-bars by specifying tool-bar-lines zero. */
9028 if (!WINDOWP (f->tool_bar_window)
9029 || (w = XWINDOW (f->tool_bar_window),
9030 WINDOW_TOTAL_LINES (w) == 0))
9031 return 0;
9033 /* Set up an iterator for the tool-bar window. */
9034 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9035 it.first_visible_x = 0;
9036 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9037 row = it.glyph_row;
9039 /* Build a string that represents the contents of the tool-bar. */
9040 build_desired_tool_bar_string (f);
9041 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9043 /* Display as many lines as needed to display all tool-bar items. */
9044 while (it.current_y < it.last_visible_y)
9045 display_tool_bar_line (&it);
9047 /* It doesn't make much sense to try scrolling in the tool-bar
9048 window, so don't do it. */
9049 w->desired_matrix->no_scrolling_p = 1;
9050 w->must_be_updated_p = 1;
9052 if (auto_resize_tool_bars_p)
9054 int nlines;
9056 /* If we couldn't display everything, change the tool-bar's
9057 height. */
9058 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9059 change_height_p = 1;
9061 /* If there are blank lines at the end, except for a partially
9062 visible blank line at the end that is smaller than
9063 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9064 row = it.glyph_row - 1;
9065 if (!row->displays_text_p
9066 && row->height >= FRAME_LINE_HEIGHT (f))
9067 change_height_p = 1;
9069 /* If row displays tool-bar items, but is partially visible,
9070 change the tool-bar's height. */
9071 if (row->displays_text_p
9072 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9073 change_height_p = 1;
9075 /* Resize windows as needed by changing the `tool-bar-lines'
9076 frame parameter. */
9077 if (change_height_p
9078 && (nlines = tool_bar_lines_needed (f),
9079 nlines != WINDOW_TOTAL_LINES (w)))
9081 extern Lisp_Object Qtool_bar_lines;
9082 Lisp_Object frame;
9083 int old_height = WINDOW_TOTAL_LINES (w);
9085 XSETFRAME (frame, f);
9086 clear_glyph_matrix (w->desired_matrix);
9087 Fmodify_frame_parameters (frame,
9088 Fcons (Fcons (Qtool_bar_lines,
9089 make_number (nlines)),
9090 Qnil));
9091 if (WINDOW_TOTAL_LINES (w) != old_height)
9092 fonts_changed_p = 1;
9096 return change_height_p;
9100 /* Get information about the tool-bar item which is displayed in GLYPH
9101 on frame F. Return in *PROP_IDX the index where tool-bar item
9102 properties start in F->tool_bar_items. Value is zero if
9103 GLYPH doesn't display a tool-bar item. */
9105 static int
9106 tool_bar_item_info (f, glyph, prop_idx)
9107 struct frame *f;
9108 struct glyph *glyph;
9109 int *prop_idx;
9111 Lisp_Object prop;
9112 int success_p;
9113 int charpos;
9115 /* This function can be called asynchronously, which means we must
9116 exclude any possibility that Fget_text_property signals an
9117 error. */
9118 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9119 charpos = max (0, charpos);
9121 /* Get the text property `menu-item' at pos. The value of that
9122 property is the start index of this item's properties in
9123 F->tool_bar_items. */
9124 prop = Fget_text_property (make_number (charpos),
9125 Qmenu_item, f->current_tool_bar_string);
9126 if (INTEGERP (prop))
9128 *prop_idx = XINT (prop);
9129 success_p = 1;
9131 else
9132 success_p = 0;
9134 return success_p;
9138 /* Get information about the tool-bar item at position X/Y on frame F.
9139 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9140 the current matrix of the tool-bar window of F, or NULL if not
9141 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9142 item in F->tool_bar_items. Value is
9144 -1 if X/Y is not on a tool-bar item
9145 0 if X/Y is on the same item that was highlighted before.
9146 1 otherwise. */
9148 static int
9149 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9150 struct frame *f;
9151 int x, y;
9152 struct glyph **glyph;
9153 int *hpos, *vpos, *prop_idx;
9155 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9156 struct window *w = XWINDOW (f->tool_bar_window);
9157 int area;
9159 /* Find the glyph under X/Y. */
9160 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9161 if (*glyph == NULL)
9162 return -1;
9164 /* Get the start of this tool-bar item's properties in
9165 f->tool_bar_items. */
9166 if (!tool_bar_item_info (f, *glyph, prop_idx))
9167 return -1;
9169 /* Is mouse on the highlighted item? */
9170 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9171 && *vpos >= dpyinfo->mouse_face_beg_row
9172 && *vpos <= dpyinfo->mouse_face_end_row
9173 && (*vpos > dpyinfo->mouse_face_beg_row
9174 || *hpos >= dpyinfo->mouse_face_beg_col)
9175 && (*vpos < dpyinfo->mouse_face_end_row
9176 || *hpos < dpyinfo->mouse_face_end_col
9177 || dpyinfo->mouse_face_past_end))
9178 return 0;
9180 return 1;
9184 /* EXPORT:
9185 Handle mouse button event on the tool-bar of frame F, at
9186 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9187 0 for button release. MODIFIERS is event modifiers for button
9188 release. */
9190 void
9191 handle_tool_bar_click (f, x, y, down_p, modifiers)
9192 struct frame *f;
9193 int x, y, down_p;
9194 unsigned int modifiers;
9196 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9197 struct window *w = XWINDOW (f->tool_bar_window);
9198 int hpos, vpos, prop_idx;
9199 struct glyph *glyph;
9200 Lisp_Object enabled_p;
9202 /* If not on the highlighted tool-bar item, return. */
9203 frame_to_window_pixel_xy (w, &x, &y);
9204 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9205 return;
9207 /* If item is disabled, do nothing. */
9208 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9209 if (NILP (enabled_p))
9210 return;
9212 if (down_p)
9214 /* Show item in pressed state. */
9215 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9216 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9217 last_tool_bar_item = prop_idx;
9219 else
9221 Lisp_Object key, frame;
9222 struct input_event event;
9223 EVENT_INIT (event);
9225 /* Show item in released state. */
9226 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9227 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9229 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9231 XSETFRAME (frame, f);
9232 event.kind = TOOL_BAR_EVENT;
9233 event.frame_or_window = frame;
9234 event.arg = frame;
9235 kbd_buffer_store_event (&event);
9237 event.kind = TOOL_BAR_EVENT;
9238 event.frame_or_window = frame;
9239 event.arg = key;
9240 event.modifiers = modifiers;
9241 kbd_buffer_store_event (&event);
9242 last_tool_bar_item = -1;
9247 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9248 tool-bar window-relative coordinates X/Y. Called from
9249 note_mouse_highlight. */
9251 static void
9252 note_tool_bar_highlight (f, x, y)
9253 struct frame *f;
9254 int x, y;
9256 Lisp_Object window = f->tool_bar_window;
9257 struct window *w = XWINDOW (window);
9258 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9259 int hpos, vpos;
9260 struct glyph *glyph;
9261 struct glyph_row *row;
9262 int i;
9263 Lisp_Object enabled_p;
9264 int prop_idx;
9265 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9266 int mouse_down_p, rc;
9268 /* Function note_mouse_highlight is called with negative x(y
9269 values when mouse moves outside of the frame. */
9270 if (x <= 0 || y <= 0)
9272 clear_mouse_face (dpyinfo);
9273 return;
9276 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9277 if (rc < 0)
9279 /* Not on tool-bar item. */
9280 clear_mouse_face (dpyinfo);
9281 return;
9283 else if (rc == 0)
9284 /* On same tool-bar item as before. */
9285 goto set_help_echo;
9287 clear_mouse_face (dpyinfo);
9289 /* Mouse is down, but on different tool-bar item? */
9290 mouse_down_p = (dpyinfo->grabbed
9291 && f == last_mouse_frame
9292 && FRAME_LIVE_P (f));
9293 if (mouse_down_p
9294 && last_tool_bar_item != prop_idx)
9295 return;
9297 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9298 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9300 /* If tool-bar item is not enabled, don't highlight it. */
9301 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9302 if (!NILP (enabled_p))
9304 /* Compute the x-position of the glyph. In front and past the
9305 image is a space. We include this in the highlighted area. */
9306 row = MATRIX_ROW (w->current_matrix, vpos);
9307 for (i = x = 0; i < hpos; ++i)
9308 x += row->glyphs[TEXT_AREA][i].pixel_width;
9310 /* Record this as the current active region. */
9311 dpyinfo->mouse_face_beg_col = hpos;
9312 dpyinfo->mouse_face_beg_row = vpos;
9313 dpyinfo->mouse_face_beg_x = x;
9314 dpyinfo->mouse_face_beg_y = row->y;
9315 dpyinfo->mouse_face_past_end = 0;
9317 dpyinfo->mouse_face_end_col = hpos + 1;
9318 dpyinfo->mouse_face_end_row = vpos;
9319 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9320 dpyinfo->mouse_face_end_y = row->y;
9321 dpyinfo->mouse_face_window = window;
9322 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9324 /* Display it as active. */
9325 show_mouse_face (dpyinfo, draw);
9326 dpyinfo->mouse_face_image_state = draw;
9329 set_help_echo:
9331 /* Set help_echo_string to a help string to display for this tool-bar item.
9332 XTread_socket does the rest. */
9333 help_echo_object = help_echo_window = Qnil;
9334 help_echo_pos = -1;
9335 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9336 if (NILP (help_echo_string))
9337 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9340 #endif /* HAVE_WINDOW_SYSTEM */
9344 /************************************************************************
9345 Horizontal scrolling
9346 ************************************************************************/
9348 static int hscroll_window_tree P_ ((Lisp_Object));
9349 static int hscroll_windows P_ ((Lisp_Object));
9351 /* For all leaf windows in the window tree rooted at WINDOW, set their
9352 hscroll value so that PT is (i) visible in the window, and (ii) so
9353 that it is not within a certain margin at the window's left and
9354 right border. Value is non-zero if any window's hscroll has been
9355 changed. */
9357 static int
9358 hscroll_window_tree (window)
9359 Lisp_Object window;
9361 int hscrolled_p = 0;
9362 int hscroll_relative_p = FLOATP (Vhscroll_step);
9363 int hscroll_step_abs = 0;
9364 double hscroll_step_rel = 0;
9366 if (hscroll_relative_p)
9368 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9369 if (hscroll_step_rel < 0)
9371 hscroll_relative_p = 0;
9372 hscroll_step_abs = 0;
9375 else if (INTEGERP (Vhscroll_step))
9377 hscroll_step_abs = XINT (Vhscroll_step);
9378 if (hscroll_step_abs < 0)
9379 hscroll_step_abs = 0;
9381 else
9382 hscroll_step_abs = 0;
9384 while (WINDOWP (window))
9386 struct window *w = XWINDOW (window);
9388 if (WINDOWP (w->hchild))
9389 hscrolled_p |= hscroll_window_tree (w->hchild);
9390 else if (WINDOWP (w->vchild))
9391 hscrolled_p |= hscroll_window_tree (w->vchild);
9392 else if (w->cursor.vpos >= 0)
9394 int h_margin;
9395 int text_area_width;
9396 struct glyph_row *current_cursor_row
9397 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9398 struct glyph_row *desired_cursor_row
9399 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9400 struct glyph_row *cursor_row
9401 = (desired_cursor_row->enabled_p
9402 ? desired_cursor_row
9403 : current_cursor_row);
9405 text_area_width = window_box_width (w, TEXT_AREA);
9407 /* Scroll when cursor is inside this scroll margin. */
9408 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9410 if ((XFASTINT (w->hscroll)
9411 && w->cursor.x <= h_margin)
9412 || (cursor_row->enabled_p
9413 && cursor_row->truncated_on_right_p
9414 && (w->cursor.x >= text_area_width - h_margin)))
9416 struct it it;
9417 int hscroll;
9418 struct buffer *saved_current_buffer;
9419 int pt;
9420 int wanted_x;
9422 /* Find point in a display of infinite width. */
9423 saved_current_buffer = current_buffer;
9424 current_buffer = XBUFFER (w->buffer);
9426 if (w == XWINDOW (selected_window))
9427 pt = BUF_PT (current_buffer);
9428 else
9430 pt = marker_position (w->pointm);
9431 pt = max (BEGV, pt);
9432 pt = min (ZV, pt);
9435 /* Move iterator to pt starting at cursor_row->start in
9436 a line with infinite width. */
9437 init_to_row_start (&it, w, cursor_row);
9438 it.last_visible_x = INFINITY;
9439 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9440 current_buffer = saved_current_buffer;
9442 /* Position cursor in window. */
9443 if (!hscroll_relative_p && hscroll_step_abs == 0)
9444 hscroll = max (0, (it.current_x
9445 - (ITERATOR_AT_END_OF_LINE_P (&it)
9446 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9447 : (text_area_width / 2))))
9448 / FRAME_COLUMN_WIDTH (it.f);
9449 else if (w->cursor.x >= text_area_width - h_margin)
9451 if (hscroll_relative_p)
9452 wanted_x = text_area_width * (1 - hscroll_step_rel)
9453 - h_margin;
9454 else
9455 wanted_x = text_area_width
9456 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9457 - h_margin;
9458 hscroll
9459 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9461 else
9463 if (hscroll_relative_p)
9464 wanted_x = text_area_width * hscroll_step_rel
9465 + h_margin;
9466 else
9467 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9468 + h_margin;
9469 hscroll
9470 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9472 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9474 /* Don't call Fset_window_hscroll if value hasn't
9475 changed because it will prevent redisplay
9476 optimizations. */
9477 if (XFASTINT (w->hscroll) != hscroll)
9479 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9480 w->hscroll = make_number (hscroll);
9481 hscrolled_p = 1;
9486 window = w->next;
9489 /* Value is non-zero if hscroll of any leaf window has been changed. */
9490 return hscrolled_p;
9494 /* Set hscroll so that cursor is visible and not inside horizontal
9495 scroll margins for all windows in the tree rooted at WINDOW. See
9496 also hscroll_window_tree above. Value is non-zero if any window's
9497 hscroll has been changed. If it has, desired matrices on the frame
9498 of WINDOW are cleared. */
9500 static int
9501 hscroll_windows (window)
9502 Lisp_Object window;
9504 int hscrolled_p;
9506 if (automatic_hscrolling_p)
9508 hscrolled_p = hscroll_window_tree (window);
9509 if (hscrolled_p)
9510 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9512 else
9513 hscrolled_p = 0;
9514 return hscrolled_p;
9519 /************************************************************************
9520 Redisplay
9521 ************************************************************************/
9523 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9524 to a non-zero value. This is sometimes handy to have in a debugger
9525 session. */
9527 #if GLYPH_DEBUG
9529 /* First and last unchanged row for try_window_id. */
9531 int debug_first_unchanged_at_end_vpos;
9532 int debug_last_unchanged_at_beg_vpos;
9534 /* Delta vpos and y. */
9536 int debug_dvpos, debug_dy;
9538 /* Delta in characters and bytes for try_window_id. */
9540 int debug_delta, debug_delta_bytes;
9542 /* Values of window_end_pos and window_end_vpos at the end of
9543 try_window_id. */
9545 EMACS_INT debug_end_pos, debug_end_vpos;
9547 /* Append a string to W->desired_matrix->method. FMT is a printf
9548 format string. A1...A9 are a supplement for a variable-length
9549 argument list. If trace_redisplay_p is non-zero also printf the
9550 resulting string to stderr. */
9552 static void
9553 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9554 struct window *w;
9555 char *fmt;
9556 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9558 char buffer[512];
9559 char *method = w->desired_matrix->method;
9560 int len = strlen (method);
9561 int size = sizeof w->desired_matrix->method;
9562 int remaining = size - len - 1;
9564 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9565 if (len && remaining)
9567 method[len] = '|';
9568 --remaining, ++len;
9571 strncpy (method + len, buffer, remaining);
9573 if (trace_redisplay_p)
9574 fprintf (stderr, "%p (%s): %s\n",
9576 ((BUFFERP (w->buffer)
9577 && STRINGP (XBUFFER (w->buffer)->name))
9578 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9579 : "no buffer"),
9580 buffer);
9583 #endif /* GLYPH_DEBUG */
9586 /* Value is non-zero if all changes in window W, which displays
9587 current_buffer, are in the text between START and END. START is a
9588 buffer position, END is given as a distance from Z. Used in
9589 redisplay_internal for display optimization. */
9591 static INLINE int
9592 text_outside_line_unchanged_p (w, start, end)
9593 struct window *w;
9594 int start, end;
9596 int unchanged_p = 1;
9598 /* If text or overlays have changed, see where. */
9599 if (XFASTINT (w->last_modified) < MODIFF
9600 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9602 /* Gap in the line? */
9603 if (GPT < start || Z - GPT < end)
9604 unchanged_p = 0;
9606 /* Changes start in front of the line, or end after it? */
9607 if (unchanged_p
9608 && (BEG_UNCHANGED < start - 1
9609 || END_UNCHANGED < end))
9610 unchanged_p = 0;
9612 /* If selective display, can't optimize if changes start at the
9613 beginning of the line. */
9614 if (unchanged_p
9615 && INTEGERP (current_buffer->selective_display)
9616 && XINT (current_buffer->selective_display) > 0
9617 && (BEG_UNCHANGED < start || GPT <= start))
9618 unchanged_p = 0;
9620 /* If there are overlays at the start or end of the line, these
9621 may have overlay strings with newlines in them. A change at
9622 START, for instance, may actually concern the display of such
9623 overlay strings as well, and they are displayed on different
9624 lines. So, quickly rule out this case. (For the future, it
9625 might be desirable to implement something more telling than
9626 just BEG/END_UNCHANGED.) */
9627 if (unchanged_p)
9629 if (BEG + BEG_UNCHANGED == start
9630 && overlay_touches_p (start))
9631 unchanged_p = 0;
9632 if (END_UNCHANGED == end
9633 && overlay_touches_p (Z - end))
9634 unchanged_p = 0;
9638 return unchanged_p;
9642 /* Do a frame update, taking possible shortcuts into account. This is
9643 the main external entry point for redisplay.
9645 If the last redisplay displayed an echo area message and that message
9646 is no longer requested, we clear the echo area or bring back the
9647 mini-buffer if that is in use. */
9649 void
9650 redisplay ()
9652 redisplay_internal (0);
9656 static Lisp_Object
9657 overlay_arrow_string_or_property (var)
9658 Lisp_Object var;
9660 Lisp_Object val;
9662 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9663 return val;
9665 return Voverlay_arrow_string;
9668 /* Return 1 if there are any overlay-arrows in current_buffer. */
9669 static int
9670 overlay_arrow_in_current_buffer_p ()
9672 Lisp_Object vlist;
9674 for (vlist = Voverlay_arrow_variable_list;
9675 CONSP (vlist);
9676 vlist = XCDR (vlist))
9678 Lisp_Object var = XCAR (vlist);
9679 Lisp_Object val;
9681 if (!SYMBOLP (var))
9682 continue;
9683 val = find_symbol_value (var);
9684 if (MARKERP (val)
9685 && current_buffer == XMARKER (val)->buffer)
9686 return 1;
9688 return 0;
9692 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9693 has changed. */
9695 static int
9696 overlay_arrows_changed_p ()
9698 Lisp_Object vlist;
9700 for (vlist = Voverlay_arrow_variable_list;
9701 CONSP (vlist);
9702 vlist = XCDR (vlist))
9704 Lisp_Object var = XCAR (vlist);
9705 Lisp_Object val, pstr;
9707 if (!SYMBOLP (var))
9708 continue;
9709 val = find_symbol_value (var);
9710 if (!MARKERP (val))
9711 continue;
9712 if (! EQ (COERCE_MARKER (val),
9713 Fget (var, Qlast_arrow_position))
9714 || ! (pstr = overlay_arrow_string_or_property (var),
9715 EQ (pstr, Fget (var, Qlast_arrow_string))))
9716 return 1;
9718 return 0;
9721 /* Mark overlay arrows to be updated on next redisplay. */
9723 static void
9724 update_overlay_arrows (up_to_date)
9725 int up_to_date;
9727 Lisp_Object vlist;
9729 for (vlist = Voverlay_arrow_variable_list;
9730 CONSP (vlist);
9731 vlist = XCDR (vlist))
9733 Lisp_Object var = XCAR (vlist);
9735 if (!SYMBOLP (var))
9736 continue;
9738 if (up_to_date > 0)
9740 Lisp_Object val = find_symbol_value (var);
9741 Fput (var, Qlast_arrow_position,
9742 COERCE_MARKER (val));
9743 Fput (var, Qlast_arrow_string,
9744 overlay_arrow_string_or_property (var));
9746 else if (up_to_date < 0
9747 || !NILP (Fget (var, Qlast_arrow_position)))
9749 Fput (var, Qlast_arrow_position, Qt);
9750 Fput (var, Qlast_arrow_string, Qt);
9756 /* Return overlay arrow string to display at row.
9757 Return integer (bitmap number) for arrow bitmap in left fringe.
9758 Return nil if no overlay arrow. */
9760 static Lisp_Object
9761 overlay_arrow_at_row (it, row)
9762 struct it *it;
9763 struct glyph_row *row;
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 if (FRAME_WINDOW_P (it->f)
9784 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9786 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9788 int fringe_bitmap;
9789 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9790 return make_number (fringe_bitmap);
9792 return make_number (-1); /* Use default arrow bitmap */
9794 return overlay_arrow_string_or_property (var);
9798 return Qnil;
9801 /* Return 1 if point moved out of or into a composition. Otherwise
9802 return 0. PREV_BUF and PREV_PT are the last point buffer and
9803 position. BUF and PT are the current point buffer and position. */
9806 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9807 struct buffer *prev_buf, *buf;
9808 int prev_pt, pt;
9810 int start, end;
9811 Lisp_Object prop;
9812 Lisp_Object buffer;
9814 XSETBUFFER (buffer, buf);
9815 /* Check a composition at the last point if point moved within the
9816 same buffer. */
9817 if (prev_buf == buf)
9819 if (prev_pt == pt)
9820 /* Point didn't move. */
9821 return 0;
9823 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9824 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9825 && COMPOSITION_VALID_P (start, end, prop)
9826 && start < prev_pt && end > prev_pt)
9827 /* The last point was within the composition. Return 1 iff
9828 point moved out of the composition. */
9829 return (pt <= start || pt >= end);
9832 /* Check a composition at the current point. */
9833 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9834 && find_composition (pt, -1, &start, &end, &prop, buffer)
9835 && COMPOSITION_VALID_P (start, end, prop)
9836 && start < pt && end > pt);
9840 /* Reconsider the setting of B->clip_changed which is displayed
9841 in window W. */
9843 static INLINE void
9844 reconsider_clip_changes (w, b)
9845 struct window *w;
9846 struct buffer *b;
9848 if (b->clip_changed
9849 && !NILP (w->window_end_valid)
9850 && w->current_matrix->buffer == b
9851 && w->current_matrix->zv == BUF_ZV (b)
9852 && w->current_matrix->begv == BUF_BEGV (b))
9853 b->clip_changed = 0;
9855 /* If display wasn't paused, and W is not a tool bar window, see if
9856 point has been moved into or out of a composition. In that case,
9857 we set b->clip_changed to 1 to force updating the screen. If
9858 b->clip_changed has already been set to 1, we can skip this
9859 check. */
9860 if (!b->clip_changed
9861 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9863 int pt;
9865 if (w == XWINDOW (selected_window))
9866 pt = BUF_PT (current_buffer);
9867 else
9868 pt = marker_position (w->pointm);
9870 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9871 || pt != XINT (w->last_point))
9872 && check_point_in_composition (w->current_matrix->buffer,
9873 XINT (w->last_point),
9874 XBUFFER (w->buffer), pt))
9875 b->clip_changed = 1;
9880 /* Select FRAME to forward the values of frame-local variables into C
9881 variables so that the redisplay routines can access those values
9882 directly. */
9884 static void
9885 select_frame_for_redisplay (frame)
9886 Lisp_Object frame;
9888 Lisp_Object tail, sym, val;
9889 Lisp_Object old = selected_frame;
9891 selected_frame = frame;
9893 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9894 if (CONSP (XCAR (tail))
9895 && (sym = XCAR (XCAR (tail)),
9896 SYMBOLP (sym))
9897 && (sym = indirect_variable (sym),
9898 val = SYMBOL_VALUE (sym),
9899 (BUFFER_LOCAL_VALUEP (val)
9900 || SOME_BUFFER_LOCAL_VALUEP (val)))
9901 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9902 Fsymbol_value (sym);
9904 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9905 if (CONSP (XCAR (tail))
9906 && (sym = XCAR (XCAR (tail)),
9907 SYMBOLP (sym))
9908 && (sym = indirect_variable (sym),
9909 val = SYMBOL_VALUE (sym),
9910 (BUFFER_LOCAL_VALUEP (val)
9911 || SOME_BUFFER_LOCAL_VALUEP (val)))
9912 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9913 Fsymbol_value (sym);
9917 #define STOP_POLLING \
9918 do { if (! polling_stopped_here) stop_polling (); \
9919 polling_stopped_here = 1; } while (0)
9921 #define RESUME_POLLING \
9922 do { if (polling_stopped_here) start_polling (); \
9923 polling_stopped_here = 0; } while (0)
9926 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9927 response to any user action; therefore, we should preserve the echo
9928 area. (Actually, our caller does that job.) Perhaps in the future
9929 avoid recentering windows if it is not necessary; currently that
9930 causes some problems. */
9932 static void
9933 redisplay_internal (preserve_echo_area)
9934 int preserve_echo_area;
9936 struct window *w = XWINDOW (selected_window);
9937 struct frame *f = XFRAME (w->frame);
9938 int pause;
9939 int must_finish = 0;
9940 struct text_pos tlbufpos, tlendpos;
9941 int number_of_visible_frames;
9942 int count;
9943 struct frame *sf = SELECTED_FRAME ();
9944 int polling_stopped_here = 0;
9946 /* Non-zero means redisplay has to consider all windows on all
9947 frames. Zero means, only selected_window is considered. */
9948 int consider_all_windows_p;
9950 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9952 /* No redisplay if running in batch mode or frame is not yet fully
9953 initialized, or redisplay is explicitly turned off by setting
9954 Vinhibit_redisplay. */
9955 if (noninteractive
9956 || !NILP (Vinhibit_redisplay)
9957 || !f->glyphs_initialized_p)
9958 return;
9960 /* The flag redisplay_performed_directly_p is set by
9961 direct_output_for_insert when it already did the whole screen
9962 update necessary. */
9963 if (redisplay_performed_directly_p)
9965 redisplay_performed_directly_p = 0;
9966 if (!hscroll_windows (selected_window))
9967 return;
9970 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9971 if (popup_activated ())
9972 return;
9973 #endif
9975 /* I don't think this happens but let's be paranoid. */
9976 if (redisplaying_p)
9977 return;
9979 /* Record a function that resets redisplaying_p to its old value
9980 when we leave this function. */
9981 count = SPECPDL_INDEX ();
9982 record_unwind_protect (unwind_redisplay,
9983 Fcons (make_number (redisplaying_p), selected_frame));
9984 ++redisplaying_p;
9985 specbind (Qinhibit_free_realized_faces, Qnil);
9987 retry:
9988 pause = 0;
9989 reconsider_clip_changes (w, current_buffer);
9991 /* If new fonts have been loaded that make a glyph matrix adjustment
9992 necessary, do it. */
9993 if (fonts_changed_p)
9995 adjust_glyphs (NULL);
9996 ++windows_or_buffers_changed;
9997 fonts_changed_p = 0;
10000 /* If face_change_count is non-zero, init_iterator will free all
10001 realized faces, which includes the faces referenced from current
10002 matrices. So, we can't reuse current matrices in this case. */
10003 if (face_change_count)
10004 ++windows_or_buffers_changed;
10006 if (! FRAME_WINDOW_P (sf)
10007 && previous_terminal_frame != sf)
10009 /* Since frames on an ASCII terminal share the same display
10010 area, displaying a different frame means redisplay the whole
10011 thing. */
10012 windows_or_buffers_changed++;
10013 SET_FRAME_GARBAGED (sf);
10014 XSETFRAME (Vterminal_frame, sf);
10016 previous_terminal_frame = sf;
10018 /* Set the visible flags for all frames. Do this before checking
10019 for resized or garbaged frames; they want to know if their frames
10020 are visible. See the comment in frame.h for
10021 FRAME_SAMPLE_VISIBILITY. */
10023 Lisp_Object tail, frame;
10025 number_of_visible_frames = 0;
10027 FOR_EACH_FRAME (tail, frame)
10029 struct frame *f = XFRAME (frame);
10031 FRAME_SAMPLE_VISIBILITY (f);
10032 if (FRAME_VISIBLE_P (f))
10033 ++number_of_visible_frames;
10034 clear_desired_matrices (f);
10038 /* Notice any pending interrupt request to change frame size. */
10039 do_pending_window_change (1);
10041 /* Clear frames marked as garbaged. */
10042 if (frame_garbaged)
10043 clear_garbaged_frames ();
10045 /* Build menubar and tool-bar items. */
10046 prepare_menu_bars ();
10048 if (windows_or_buffers_changed)
10049 update_mode_lines++;
10051 /* Detect case that we need to write or remove a star in the mode line. */
10052 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10054 w->update_mode_line = Qt;
10055 if (buffer_shared > 1)
10056 update_mode_lines++;
10059 /* If %c is in the mode line, update it if needed. */
10060 if (!NILP (w->column_number_displayed)
10061 /* This alternative quickly identifies a common case
10062 where no change is needed. */
10063 && !(PT == XFASTINT (w->last_point)
10064 && XFASTINT (w->last_modified) >= MODIFF
10065 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10066 && (XFASTINT (w->column_number_displayed)
10067 != (int) current_column ())) /* iftc */
10068 w->update_mode_line = Qt;
10070 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10072 /* The variable buffer_shared is set in redisplay_window and
10073 indicates that we redisplay a buffer in different windows. See
10074 there. */
10075 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10076 || cursor_type_changed);
10078 /* If specs for an arrow have changed, do thorough redisplay
10079 to ensure we remove any arrow that should no longer exist. */
10080 if (overlay_arrows_changed_p ())
10081 consider_all_windows_p = windows_or_buffers_changed = 1;
10083 /* Normally the message* functions will have already displayed and
10084 updated the echo area, but the frame may have been trashed, or
10085 the update may have been preempted, so display the echo area
10086 again here. Checking message_cleared_p captures the case that
10087 the echo area should be cleared. */
10088 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10089 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10090 || (message_cleared_p
10091 && minibuf_level == 0
10092 /* If the mini-window is currently selected, this means the
10093 echo-area doesn't show through. */
10094 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10096 int window_height_changed_p = echo_area_display (0);
10097 must_finish = 1;
10099 /* If we don't display the current message, don't clear the
10100 message_cleared_p flag, because, if we did, we wouldn't clear
10101 the echo area in the next redisplay which doesn't preserve
10102 the echo area. */
10103 if (!display_last_displayed_message_p)
10104 message_cleared_p = 0;
10106 if (fonts_changed_p)
10107 goto retry;
10108 else if (window_height_changed_p)
10110 consider_all_windows_p = 1;
10111 ++update_mode_lines;
10112 ++windows_or_buffers_changed;
10114 /* If window configuration was changed, frames may have been
10115 marked garbaged. Clear them or we will experience
10116 surprises wrt scrolling. */
10117 if (frame_garbaged)
10118 clear_garbaged_frames ();
10121 else if (EQ (selected_window, minibuf_window)
10122 && (current_buffer->clip_changed
10123 || XFASTINT (w->last_modified) < MODIFF
10124 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10125 && resize_mini_window (w, 0))
10127 /* Resized active mini-window to fit the size of what it is
10128 showing if its contents might have changed. */
10129 must_finish = 1;
10130 consider_all_windows_p = 1;
10131 ++windows_or_buffers_changed;
10132 ++update_mode_lines;
10134 /* If window configuration was changed, frames may have been
10135 marked garbaged. Clear them or we will experience
10136 surprises wrt scrolling. */
10137 if (frame_garbaged)
10138 clear_garbaged_frames ();
10142 /* If showing the region, and mark has changed, we must redisplay
10143 the whole window. The assignment to this_line_start_pos prevents
10144 the optimization directly below this if-statement. */
10145 if (((!NILP (Vtransient_mark_mode)
10146 && !NILP (XBUFFER (w->buffer)->mark_active))
10147 != !NILP (w->region_showing))
10148 || (!NILP (w->region_showing)
10149 && !EQ (w->region_showing,
10150 Fmarker_position (XBUFFER (w->buffer)->mark))))
10151 CHARPOS (this_line_start_pos) = 0;
10153 /* Optimize the case that only the line containing the cursor in the
10154 selected window has changed. Variables starting with this_ are
10155 set in display_line and record information about the line
10156 containing the cursor. */
10157 tlbufpos = this_line_start_pos;
10158 tlendpos = this_line_end_pos;
10159 if (!consider_all_windows_p
10160 && CHARPOS (tlbufpos) > 0
10161 && NILP (w->update_mode_line)
10162 && !current_buffer->clip_changed
10163 && !current_buffer->prevent_redisplay_optimizations_p
10164 && FRAME_VISIBLE_P (XFRAME (w->frame))
10165 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10166 /* Make sure recorded data applies to current buffer, etc. */
10167 && this_line_buffer == current_buffer
10168 && current_buffer == XBUFFER (w->buffer)
10169 && NILP (w->force_start)
10170 && NILP (w->optional_new_start)
10171 /* Point must be on the line that we have info recorded about. */
10172 && PT >= CHARPOS (tlbufpos)
10173 && PT <= Z - CHARPOS (tlendpos)
10174 /* All text outside that line, including its final newline,
10175 must be unchanged */
10176 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10177 CHARPOS (tlendpos)))
10179 if (CHARPOS (tlbufpos) > BEGV
10180 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10181 && (CHARPOS (tlbufpos) == ZV
10182 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10183 /* Former continuation line has disappeared by becoming empty */
10184 goto cancel;
10185 else if (XFASTINT (w->last_modified) < MODIFF
10186 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10187 || MINI_WINDOW_P (w))
10189 /* We have to handle the case of continuation around a
10190 wide-column character (See the comment in indent.c around
10191 line 885).
10193 For instance, in the following case:
10195 -------- Insert --------
10196 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10197 J_I_ ==> J_I_ `^^' are cursors.
10198 ^^ ^^
10199 -------- --------
10201 As we have to redraw the line above, we should goto cancel. */
10203 struct it it;
10204 int line_height_before = this_line_pixel_height;
10206 /* Note that start_display will handle the case that the
10207 line starting at tlbufpos is a continuation lines. */
10208 start_display (&it, w, tlbufpos);
10210 /* Implementation note: It this still necessary? */
10211 if (it.current_x != this_line_start_x)
10212 goto cancel;
10214 TRACE ((stderr, "trying display optimization 1\n"));
10215 w->cursor.vpos = -1;
10216 overlay_arrow_seen = 0;
10217 it.vpos = this_line_vpos;
10218 it.current_y = this_line_y;
10219 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10220 display_line (&it);
10222 /* If line contains point, is not continued,
10223 and ends at same distance from eob as before, we win */
10224 if (w->cursor.vpos >= 0
10225 /* Line is not continued, otherwise this_line_start_pos
10226 would have been set to 0 in display_line. */
10227 && CHARPOS (this_line_start_pos)
10228 /* Line ends as before. */
10229 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10230 /* Line has same height as before. Otherwise other lines
10231 would have to be shifted up or down. */
10232 && this_line_pixel_height == line_height_before)
10234 /* If this is not the window's last line, we must adjust
10235 the charstarts of the lines below. */
10236 if (it.current_y < it.last_visible_y)
10238 struct glyph_row *row
10239 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10240 int delta, delta_bytes;
10242 if (Z - CHARPOS (tlendpos) == ZV)
10244 /* This line ends at end of (accessible part of)
10245 buffer. There is no newline to count. */
10246 delta = (Z
10247 - CHARPOS (tlendpos)
10248 - MATRIX_ROW_START_CHARPOS (row));
10249 delta_bytes = (Z_BYTE
10250 - BYTEPOS (tlendpos)
10251 - MATRIX_ROW_START_BYTEPOS (row));
10253 else
10255 /* This line ends in a newline. Must take
10256 account of the newline and the rest of the
10257 text that follows. */
10258 delta = (Z
10259 - CHARPOS (tlendpos)
10260 - MATRIX_ROW_START_CHARPOS (row));
10261 delta_bytes = (Z_BYTE
10262 - BYTEPOS (tlendpos)
10263 - MATRIX_ROW_START_BYTEPOS (row));
10266 increment_matrix_positions (w->current_matrix,
10267 this_line_vpos + 1,
10268 w->current_matrix->nrows,
10269 delta, delta_bytes);
10272 /* If this row displays text now but previously didn't,
10273 or vice versa, w->window_end_vpos may have to be
10274 adjusted. */
10275 if ((it.glyph_row - 1)->displays_text_p)
10277 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10278 XSETINT (w->window_end_vpos, this_line_vpos);
10280 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10281 && this_line_vpos > 0)
10282 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10283 w->window_end_valid = Qnil;
10285 /* Update hint: No need to try to scroll in update_window. */
10286 w->desired_matrix->no_scrolling_p = 1;
10288 #if GLYPH_DEBUG
10289 *w->desired_matrix->method = 0;
10290 debug_method_add (w, "optimization 1");
10291 #endif
10292 #ifdef HAVE_WINDOW_SYSTEM
10293 update_window_fringes (w, 0);
10294 #endif
10295 goto update;
10297 else
10298 goto cancel;
10300 else if (/* Cursor position hasn't changed. */
10301 PT == XFASTINT (w->last_point)
10302 /* Make sure the cursor was last displayed
10303 in this window. Otherwise we have to reposition it. */
10304 && 0 <= w->cursor.vpos
10305 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10307 if (!must_finish)
10309 do_pending_window_change (1);
10311 /* We used to always goto end_of_redisplay here, but this
10312 isn't enough if we have a blinking cursor. */
10313 if (w->cursor_off_p == w->last_cursor_off_p)
10314 goto end_of_redisplay;
10316 goto update;
10318 /* If highlighting the region, or if the cursor is in the echo area,
10319 then we can't just move the cursor. */
10320 else if (! (!NILP (Vtransient_mark_mode)
10321 && !NILP (current_buffer->mark_active))
10322 && (EQ (selected_window, current_buffer->last_selected_window)
10323 || highlight_nonselected_windows)
10324 && NILP (w->region_showing)
10325 && NILP (Vshow_trailing_whitespace)
10326 && !cursor_in_echo_area)
10328 struct it it;
10329 struct glyph_row *row;
10331 /* Skip from tlbufpos to PT and see where it is. Note that
10332 PT may be in invisible text. If so, we will end at the
10333 next visible position. */
10334 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10335 NULL, DEFAULT_FACE_ID);
10336 it.current_x = this_line_start_x;
10337 it.current_y = this_line_y;
10338 it.vpos = this_line_vpos;
10340 /* The call to move_it_to stops in front of PT, but
10341 moves over before-strings. */
10342 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10344 if (it.vpos == this_line_vpos
10345 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10346 row->enabled_p))
10348 xassert (this_line_vpos == it.vpos);
10349 xassert (this_line_y == it.current_y);
10350 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10351 #if GLYPH_DEBUG
10352 *w->desired_matrix->method = 0;
10353 debug_method_add (w, "optimization 3");
10354 #endif
10355 goto update;
10357 else
10358 goto cancel;
10361 cancel:
10362 /* Text changed drastically or point moved off of line. */
10363 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10366 CHARPOS (this_line_start_pos) = 0;
10367 consider_all_windows_p |= buffer_shared > 1;
10368 ++clear_face_cache_count;
10369 #ifdef HAVE_WINDOW_SYSTEM
10370 ++clear_image_cache_count;
10371 #endif
10373 /* Build desired matrices, and update the display. If
10374 consider_all_windows_p is non-zero, do it for all windows on all
10375 frames. Otherwise do it for selected_window, only. */
10377 if (consider_all_windows_p)
10379 Lisp_Object tail, frame;
10380 int i, n = 0, size = 50;
10381 struct frame **updated
10382 = (struct frame **) alloca (size * sizeof *updated);
10384 /* Recompute # windows showing selected buffer. This will be
10385 incremented each time such a window is displayed. */
10386 buffer_shared = 0;
10388 FOR_EACH_FRAME (tail, frame)
10390 struct frame *f = XFRAME (frame);
10392 if (FRAME_WINDOW_P (f) || f == sf)
10394 if (! EQ (frame, selected_frame))
10395 /* Select the frame, for the sake of frame-local
10396 variables. */
10397 select_frame_for_redisplay (frame);
10399 /* Mark all the scroll bars to be removed; we'll redeem
10400 the ones we want when we redisplay their windows. */
10401 if (condemn_scroll_bars_hook)
10402 condemn_scroll_bars_hook (f);
10404 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10405 redisplay_windows (FRAME_ROOT_WINDOW (f));
10407 /* Any scroll bars which redisplay_windows should have
10408 nuked should now go away. */
10409 if (judge_scroll_bars_hook)
10410 judge_scroll_bars_hook (f);
10412 /* If fonts changed, display again. */
10413 /* ??? rms: I suspect it is a mistake to jump all the way
10414 back to retry here. It should just retry this frame. */
10415 if (fonts_changed_p)
10416 goto retry;
10418 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10420 /* See if we have to hscroll. */
10421 if (hscroll_windows (f->root_window))
10422 goto retry;
10424 /* Prevent various kinds of signals during display
10425 update. stdio is not robust about handling
10426 signals, which can cause an apparent I/O
10427 error. */
10428 if (interrupt_input)
10429 unrequest_sigio ();
10430 STOP_POLLING;
10432 /* Update the display. */
10433 set_window_update_flags (XWINDOW (f->root_window), 1);
10434 pause |= update_frame (f, 0, 0);
10435 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10436 if (pause)
10437 break;
10438 #endif
10440 if (n == size)
10442 int nbytes = size * sizeof *updated;
10443 struct frame **p = (struct frame **) alloca (2 * nbytes);
10444 bcopy (updated, p, nbytes);
10445 size *= 2;
10448 updated[n++] = f;
10453 if (!pause)
10455 /* Do the mark_window_display_accurate after all windows have
10456 been redisplayed because this call resets flags in buffers
10457 which are needed for proper redisplay. */
10458 for (i = 0; i < n; ++i)
10460 struct frame *f = updated[i];
10461 mark_window_display_accurate (f->root_window, 1);
10462 if (frame_up_to_date_hook)
10463 frame_up_to_date_hook (f);
10467 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10469 Lisp_Object mini_window;
10470 struct frame *mini_frame;
10472 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10473 /* Use list_of_error, not Qerror, so that
10474 we catch only errors and don't run the debugger. */
10475 internal_condition_case_1 (redisplay_window_1, selected_window,
10476 list_of_error,
10477 redisplay_window_error);
10479 /* Compare desired and current matrices, perform output. */
10481 update:
10482 /* If fonts changed, display again. */
10483 if (fonts_changed_p)
10484 goto retry;
10486 /* Prevent various kinds of signals during display update.
10487 stdio is not robust about handling signals,
10488 which can cause an apparent I/O error. */
10489 if (interrupt_input)
10490 unrequest_sigio ();
10491 STOP_POLLING;
10493 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10495 if (hscroll_windows (selected_window))
10496 goto retry;
10498 XWINDOW (selected_window)->must_be_updated_p = 1;
10499 pause = update_frame (sf, 0, 0);
10502 /* We may have called echo_area_display at the top of this
10503 function. If the echo area is on another frame, that may
10504 have put text on a frame other than the selected one, so the
10505 above call to update_frame would not have caught it. Catch
10506 it here. */
10507 mini_window = FRAME_MINIBUF_WINDOW (sf);
10508 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10510 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10512 XWINDOW (mini_window)->must_be_updated_p = 1;
10513 pause |= update_frame (mini_frame, 0, 0);
10514 if (!pause && hscroll_windows (mini_window))
10515 goto retry;
10519 /* If display was paused because of pending input, make sure we do a
10520 thorough update the next time. */
10521 if (pause)
10523 /* Prevent the optimization at the beginning of
10524 redisplay_internal that tries a single-line update of the
10525 line containing the cursor in the selected window. */
10526 CHARPOS (this_line_start_pos) = 0;
10528 /* Let the overlay arrow be updated the next time. */
10529 update_overlay_arrows (0);
10531 /* If we pause after scrolling, some rows in the current
10532 matrices of some windows are not valid. */
10533 if (!WINDOW_FULL_WIDTH_P (w)
10534 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10535 update_mode_lines = 1;
10537 else
10539 if (!consider_all_windows_p)
10541 /* This has already been done above if
10542 consider_all_windows_p is set. */
10543 mark_window_display_accurate_1 (w, 1);
10545 /* Say overlay arrows are up to date. */
10546 update_overlay_arrows (1);
10548 if (frame_up_to_date_hook != 0)
10549 frame_up_to_date_hook (sf);
10552 update_mode_lines = 0;
10553 windows_or_buffers_changed = 0;
10554 cursor_type_changed = 0;
10557 /* Start SIGIO interrupts coming again. Having them off during the
10558 code above makes it less likely one will discard output, but not
10559 impossible, since there might be stuff in the system buffer here.
10560 But it is much hairier to try to do anything about that. */
10561 if (interrupt_input)
10562 request_sigio ();
10563 RESUME_POLLING;
10565 /* If a frame has become visible which was not before, redisplay
10566 again, so that we display it. Expose events for such a frame
10567 (which it gets when becoming visible) don't call the parts of
10568 redisplay constructing glyphs, so simply exposing a frame won't
10569 display anything in this case. So, we have to display these
10570 frames here explicitly. */
10571 if (!pause)
10573 Lisp_Object tail, frame;
10574 int new_count = 0;
10576 FOR_EACH_FRAME (tail, frame)
10578 int this_is_visible = 0;
10580 if (XFRAME (frame)->visible)
10581 this_is_visible = 1;
10582 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10583 if (XFRAME (frame)->visible)
10584 this_is_visible = 1;
10586 if (this_is_visible)
10587 new_count++;
10590 if (new_count != number_of_visible_frames)
10591 windows_or_buffers_changed++;
10594 /* Change frame size now if a change is pending. */
10595 do_pending_window_change (1);
10597 /* If we just did a pending size change, or have additional
10598 visible frames, redisplay again. */
10599 if (windows_or_buffers_changed && !pause)
10600 goto retry;
10602 /* Clear the face cache eventually. */
10603 if (consider_all_windows_p)
10605 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10607 clear_face_cache (0);
10608 clear_face_cache_count = 0;
10610 #ifdef HAVE_WINDOW_SYSTEM
10611 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10613 Lisp_Object tail, frame;
10614 FOR_EACH_FRAME (tail, frame)
10616 struct frame *f = XFRAME (frame);
10617 if (FRAME_WINDOW_P (f))
10618 clear_image_cache (f, 0);
10620 clear_image_cache_count = 0;
10622 #endif /* HAVE_WINDOW_SYSTEM */
10625 end_of_redisplay:
10626 unbind_to (count, Qnil);
10627 RESUME_POLLING;
10631 /* Redisplay, but leave alone any recent echo area message unless
10632 another message has been requested in its place.
10634 This is useful in situations where you need to redisplay but no
10635 user action has occurred, making it inappropriate for the message
10636 area to be cleared. See tracking_off and
10637 wait_reading_process_output for examples of these situations.
10639 FROM_WHERE is an integer saying from where this function was
10640 called. This is useful for debugging. */
10642 void
10643 redisplay_preserve_echo_area (from_where)
10644 int from_where;
10646 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10648 if (!NILP (echo_area_buffer[1]))
10650 /* We have a previously displayed message, but no current
10651 message. Redisplay the previous message. */
10652 display_last_displayed_message_p = 1;
10653 redisplay_internal (1);
10654 display_last_displayed_message_p = 0;
10656 else
10657 redisplay_internal (1);
10659 if (rif != NULL && rif->flush_display_optional)
10660 rif->flush_display_optional (NULL);
10664 /* Function registered with record_unwind_protect in
10665 redisplay_internal. Reset redisplaying_p to the value it had
10666 before redisplay_internal was called, and clear
10667 prevent_freeing_realized_faces_p. It also selects the previously
10668 selected frame. */
10670 static Lisp_Object
10671 unwind_redisplay (val)
10672 Lisp_Object val;
10674 Lisp_Object old_redisplaying_p, old_frame;
10676 old_redisplaying_p = XCAR (val);
10677 redisplaying_p = XFASTINT (old_redisplaying_p);
10678 old_frame = XCDR (val);
10679 if (! EQ (old_frame, selected_frame))
10680 select_frame_for_redisplay (old_frame);
10681 return Qnil;
10685 /* Mark the display of window W as accurate or inaccurate. If
10686 ACCURATE_P is non-zero mark display of W as accurate. If
10687 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10688 redisplay_internal is called. */
10690 static void
10691 mark_window_display_accurate_1 (w, accurate_p)
10692 struct window *w;
10693 int accurate_p;
10695 if (BUFFERP (w->buffer))
10697 struct buffer *b = XBUFFER (w->buffer);
10699 w->last_modified
10700 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10701 w->last_overlay_modified
10702 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10703 w->last_had_star
10704 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10706 if (accurate_p)
10708 b->clip_changed = 0;
10709 b->prevent_redisplay_optimizations_p = 0;
10711 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10712 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10713 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10714 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10716 w->current_matrix->buffer = b;
10717 w->current_matrix->begv = BUF_BEGV (b);
10718 w->current_matrix->zv = BUF_ZV (b);
10720 w->last_cursor = w->cursor;
10721 w->last_cursor_off_p = w->cursor_off_p;
10723 if (w == XWINDOW (selected_window))
10724 w->last_point = make_number (BUF_PT (b));
10725 else
10726 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10730 if (accurate_p)
10732 w->window_end_valid = w->buffer;
10733 #if 0 /* This is incorrect with variable-height lines. */
10734 xassert (XINT (w->window_end_vpos)
10735 < (WINDOW_TOTAL_LINES (w)
10736 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10737 #endif
10738 w->update_mode_line = Qnil;
10743 /* Mark the display of windows in the window tree rooted at WINDOW as
10744 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10745 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10746 be redisplayed the next time redisplay_internal is called. */
10748 void
10749 mark_window_display_accurate (window, accurate_p)
10750 Lisp_Object window;
10751 int accurate_p;
10753 struct window *w;
10755 for (; !NILP (window); window = w->next)
10757 w = XWINDOW (window);
10758 mark_window_display_accurate_1 (w, accurate_p);
10760 if (!NILP (w->vchild))
10761 mark_window_display_accurate (w->vchild, accurate_p);
10762 if (!NILP (w->hchild))
10763 mark_window_display_accurate (w->hchild, accurate_p);
10766 if (accurate_p)
10768 update_overlay_arrows (1);
10770 else
10772 /* Force a thorough redisplay the next time by setting
10773 last_arrow_position and last_arrow_string to t, which is
10774 unequal to any useful value of Voverlay_arrow_... */
10775 update_overlay_arrows (-1);
10780 /* Return value in display table DP (Lisp_Char_Table *) for character
10781 C. Since a display table doesn't have any parent, we don't have to
10782 follow parent. Do not call this function directly but use the
10783 macro DISP_CHAR_VECTOR. */
10785 Lisp_Object
10786 disp_char_vector (dp, c)
10787 struct Lisp_Char_Table *dp;
10788 int c;
10790 int code[4], i;
10791 Lisp_Object val;
10793 if (SINGLE_BYTE_CHAR_P (c))
10794 return (dp->contents[c]);
10796 SPLIT_CHAR (c, code[0], code[1], code[2]);
10797 if (code[1] < 32)
10798 code[1] = -1;
10799 else if (code[2] < 32)
10800 code[2] = -1;
10802 /* Here, the possible range of code[0] (== charset ID) is
10803 128..max_charset. Since the top level char table contains data
10804 for multibyte characters after 256th element, we must increment
10805 code[0] by 128 to get a correct index. */
10806 code[0] += 128;
10807 code[3] = -1; /* anchor */
10809 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10811 val = dp->contents[code[i]];
10812 if (!SUB_CHAR_TABLE_P (val))
10813 return (NILP (val) ? dp->defalt : val);
10816 /* Here, val is a sub char table. We return the default value of
10817 it. */
10818 return (dp->defalt);
10823 /***********************************************************************
10824 Window Redisplay
10825 ***********************************************************************/
10827 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10829 static void
10830 redisplay_windows (window)
10831 Lisp_Object window;
10833 while (!NILP (window))
10835 struct window *w = XWINDOW (window);
10837 if (!NILP (w->hchild))
10838 redisplay_windows (w->hchild);
10839 else if (!NILP (w->vchild))
10840 redisplay_windows (w->vchild);
10841 else
10843 displayed_buffer = XBUFFER (w->buffer);
10844 /* Use list_of_error, not Qerror, so that
10845 we catch only errors and don't run the debugger. */
10846 internal_condition_case_1 (redisplay_window_0, window,
10847 list_of_error,
10848 redisplay_window_error);
10851 window = w->next;
10855 static Lisp_Object
10856 redisplay_window_error ()
10858 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10859 return Qnil;
10862 static Lisp_Object
10863 redisplay_window_0 (window)
10864 Lisp_Object window;
10866 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10867 redisplay_window (window, 0);
10868 return Qnil;
10871 static Lisp_Object
10872 redisplay_window_1 (window)
10873 Lisp_Object window;
10875 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10876 redisplay_window (window, 1);
10877 return Qnil;
10881 /* Increment GLYPH until it reaches END or CONDITION fails while
10882 adding (GLYPH)->pixel_width to X. */
10884 #define SKIP_GLYPHS(glyph, end, x, condition) \
10885 do \
10887 (x) += (glyph)->pixel_width; \
10888 ++(glyph); \
10890 while ((glyph) < (end) && (condition))
10893 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10894 DELTA is the number of bytes by which positions recorded in ROW
10895 differ from current buffer positions. */
10897 void
10898 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10899 struct window *w;
10900 struct glyph_row *row;
10901 struct glyph_matrix *matrix;
10902 int delta, delta_bytes, dy, dvpos;
10904 struct glyph *glyph = row->glyphs[TEXT_AREA];
10905 struct glyph *end = glyph + row->used[TEXT_AREA];
10906 struct glyph *cursor = NULL;
10907 /* The first glyph that starts a sequence of glyphs from string. */
10908 struct glyph *string_start;
10909 /* The X coordinate of string_start. */
10910 int string_start_x;
10911 /* The last known character position. */
10912 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10913 /* The last known character position before string_start. */
10914 int string_before_pos;
10915 int x = row->x;
10916 int cursor_x = x;
10917 int cursor_from_overlay_pos = 0;
10918 int pt_old = PT - delta;
10920 /* Skip over glyphs not having an object at the start of the row.
10921 These are special glyphs like truncation marks on terminal
10922 frames. */
10923 if (row->displays_text_p)
10924 while (glyph < end
10925 && INTEGERP (glyph->object)
10926 && glyph->charpos < 0)
10928 x += glyph->pixel_width;
10929 ++glyph;
10932 string_start = NULL;
10933 while (glyph < end
10934 && !INTEGERP (glyph->object)
10935 && (!BUFFERP (glyph->object)
10936 || (last_pos = glyph->charpos) < pt_old))
10938 if (! STRINGP (glyph->object))
10940 string_start = NULL;
10941 x += glyph->pixel_width;
10942 ++glyph;
10943 if (cursor_from_overlay_pos
10944 && last_pos > cursor_from_overlay_pos)
10946 cursor_from_overlay_pos = 0;
10947 cursor = 0;
10950 else
10952 string_before_pos = last_pos;
10953 string_start = glyph;
10954 string_start_x = x;
10955 /* Skip all glyphs from string. */
10958 int pos;
10959 if ((cursor == NULL || glyph > cursor)
10960 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10961 Qcursor, (glyph)->object))
10962 && (pos = string_buffer_position (w, glyph->object,
10963 string_before_pos),
10964 (pos == 0 /* From overlay */
10965 || pos == pt_old)))
10967 /* Estimate overlay buffer position from the buffer
10968 positions of the glyphs before and after the overlay.
10969 Add 1 to last_pos so that if point corresponds to the
10970 glyph right after the overlay, we still use a 'cursor'
10971 property found in that overlay. */
10972 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10973 cursor = glyph;
10974 cursor_x = x;
10976 x += glyph->pixel_width;
10977 ++glyph;
10979 while (glyph < end && STRINGP (glyph->object));
10983 if (cursor != NULL)
10985 glyph = cursor;
10986 x = cursor_x;
10988 else if (row->ends_in_ellipsis_p && glyph == end)
10990 /* Scan back over the ellipsis glyphs, decrementing positions. */
10991 while (glyph > row->glyphs[TEXT_AREA]
10992 && (glyph - 1)->charpos == last_pos)
10993 glyph--, x -= glyph->pixel_width;
10994 /* That loop always goes one position too far,
10995 including the glyph before the ellipsis.
10996 So scan forward over that one. */
10997 x += glyph->pixel_width;
10998 glyph++;
11000 else if (string_start
11001 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11003 /* We may have skipped over point because the previous glyphs
11004 are from string. As there's no easy way to know the
11005 character position of the current glyph, find the correct
11006 glyph on point by scanning from string_start again. */
11007 Lisp_Object limit;
11008 Lisp_Object string;
11009 int pos;
11011 limit = make_number (pt_old + 1);
11012 end = glyph;
11013 glyph = string_start;
11014 x = string_start_x;
11015 string = glyph->object;
11016 pos = string_buffer_position (w, string, string_before_pos);
11017 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11018 because we always put cursor after overlay strings. */
11019 while (pos == 0 && glyph < end)
11021 string = glyph->object;
11022 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11023 if (glyph < end)
11024 pos = string_buffer_position (w, glyph->object, string_before_pos);
11027 while (glyph < end)
11029 pos = XINT (Fnext_single_char_property_change
11030 (make_number (pos), Qdisplay, Qnil, limit));
11031 if (pos > pt_old)
11032 break;
11033 /* Skip glyphs from the same string. */
11034 string = glyph->object;
11035 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11036 /* Skip glyphs from an overlay. */
11037 while (glyph < end
11038 && ! string_buffer_position (w, glyph->object, pos))
11040 string = glyph->object;
11041 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11046 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11047 w->cursor.x = x;
11048 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11049 w->cursor.y = row->y + dy;
11051 if (w == XWINDOW (selected_window))
11053 if (!row->continued_p
11054 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11055 && row->x == 0)
11057 this_line_buffer = XBUFFER (w->buffer);
11059 CHARPOS (this_line_start_pos)
11060 = MATRIX_ROW_START_CHARPOS (row) + delta;
11061 BYTEPOS (this_line_start_pos)
11062 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11064 CHARPOS (this_line_end_pos)
11065 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11066 BYTEPOS (this_line_end_pos)
11067 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11069 this_line_y = w->cursor.y;
11070 this_line_pixel_height = row->height;
11071 this_line_vpos = w->cursor.vpos;
11072 this_line_start_x = row->x;
11074 else
11075 CHARPOS (this_line_start_pos) = 0;
11080 /* Run window scroll functions, if any, for WINDOW with new window
11081 start STARTP. Sets the window start of WINDOW to that position.
11083 We assume that the window's buffer is really current. */
11085 static INLINE struct text_pos
11086 run_window_scroll_functions (window, startp)
11087 Lisp_Object window;
11088 struct text_pos startp;
11090 struct window *w = XWINDOW (window);
11091 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11093 if (current_buffer != XBUFFER (w->buffer))
11094 abort ();
11096 if (!NILP (Vwindow_scroll_functions))
11098 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11099 make_number (CHARPOS (startp)));
11100 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11101 /* In case the hook functions switch buffers. */
11102 if (current_buffer != XBUFFER (w->buffer))
11103 set_buffer_internal_1 (XBUFFER (w->buffer));
11106 return startp;
11110 /* Make sure the line containing the cursor is fully visible.
11111 A value of 1 means there is nothing to be done.
11112 (Either the line is fully visible, or it cannot be made so,
11113 or we cannot tell.)
11115 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11116 is higher than window.
11118 A value of 0 means the caller should do scrolling
11119 as if point had gone off the screen. */
11121 static int
11122 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11123 struct window *w;
11124 int force_p;
11126 struct glyph_matrix *matrix;
11127 struct glyph_row *row;
11128 int window_height;
11130 if (!make_cursor_line_fully_visible_p)
11131 return 1;
11133 /* It's not always possible to find the cursor, e.g, when a window
11134 is full of overlay strings. Don't do anything in that case. */
11135 if (w->cursor.vpos < 0)
11136 return 1;
11138 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11139 row = MATRIX_ROW (matrix, w->cursor.vpos);
11141 /* If the cursor row is not partially visible, there's nothing to do. */
11142 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11143 return 1;
11145 /* If the row the cursor is in is taller than the window's height,
11146 it's not clear what to do, so do nothing. */
11147 window_height = window_box_height (w);
11148 if (row->height >= window_height)
11150 if (!force_p || w->vscroll)
11151 return 1;
11153 return 0;
11155 #if 0
11156 /* This code used to try to scroll the window just enough to make
11157 the line visible. It returned 0 to say that the caller should
11158 allocate larger glyph matrices. */
11160 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11162 int dy = row->height - row->visible_height;
11163 w->vscroll = 0;
11164 w->cursor.y += dy;
11165 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11167 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11169 int dy = - (row->height - row->visible_height);
11170 w->vscroll = dy;
11171 w->cursor.y += dy;
11172 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11175 /* When we change the cursor y-position of the selected window,
11176 change this_line_y as well so that the display optimization for
11177 the cursor line of the selected window in redisplay_internal uses
11178 the correct y-position. */
11179 if (w == XWINDOW (selected_window))
11180 this_line_y = w->cursor.y;
11182 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11183 redisplay with larger matrices. */
11184 if (matrix->nrows < required_matrix_height (w))
11186 fonts_changed_p = 1;
11187 return 0;
11190 return 1;
11191 #endif /* 0 */
11195 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11196 non-zero means only WINDOW is redisplayed in redisplay_internal.
11197 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11198 in redisplay_window to bring a partially visible line into view in
11199 the case that only the cursor has moved.
11201 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11202 last screen line's vertical height extends past the end of the screen.
11204 Value is
11206 1 if scrolling succeeded
11208 0 if scrolling didn't find point.
11210 -1 if new fonts have been loaded so that we must interrupt
11211 redisplay, adjust glyph matrices, and try again. */
11213 enum
11215 SCROLLING_SUCCESS,
11216 SCROLLING_FAILED,
11217 SCROLLING_NEED_LARGER_MATRICES
11220 static int
11221 try_scrolling (window, just_this_one_p, scroll_conservatively,
11222 scroll_step, temp_scroll_step, last_line_misfit)
11223 Lisp_Object window;
11224 int just_this_one_p;
11225 EMACS_INT scroll_conservatively, scroll_step;
11226 int temp_scroll_step;
11227 int last_line_misfit;
11229 struct window *w = XWINDOW (window);
11230 struct frame *f = XFRAME (w->frame);
11231 struct text_pos scroll_margin_pos;
11232 struct text_pos pos;
11233 struct text_pos startp;
11234 struct it it;
11235 Lisp_Object window_end;
11236 int this_scroll_margin;
11237 int dy = 0;
11238 int scroll_max;
11239 int rc;
11240 int amount_to_scroll = 0;
11241 Lisp_Object aggressive;
11242 int height;
11243 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11245 #if GLYPH_DEBUG
11246 debug_method_add (w, "try_scrolling");
11247 #endif
11249 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11251 /* Compute scroll margin height in pixels. We scroll when point is
11252 within this distance from the top or bottom of the window. */
11253 if (scroll_margin > 0)
11255 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11256 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11258 else
11259 this_scroll_margin = 0;
11261 /* Force scroll_conservatively to have a reasonable value so it doesn't
11262 cause an overflow while computing how much to scroll. */
11263 if (scroll_conservatively)
11264 scroll_conservatively = min (scroll_conservatively,
11265 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11267 /* Compute how much we should try to scroll maximally to bring point
11268 into view. */
11269 if (scroll_step || scroll_conservatively || temp_scroll_step)
11270 scroll_max = max (scroll_step,
11271 max (scroll_conservatively, temp_scroll_step));
11272 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11273 || NUMBERP (current_buffer->scroll_up_aggressively))
11274 /* We're trying to scroll because of aggressive scrolling
11275 but no scroll_step is set. Choose an arbitrary one. Maybe
11276 there should be a variable for this. */
11277 scroll_max = 10;
11278 else
11279 scroll_max = 0;
11280 scroll_max *= FRAME_LINE_HEIGHT (f);
11282 /* Decide whether we have to scroll down. Start at the window end
11283 and move this_scroll_margin up to find the position of the scroll
11284 margin. */
11285 window_end = Fwindow_end (window, Qt);
11287 too_near_end:
11289 CHARPOS (scroll_margin_pos) = XINT (window_end);
11290 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11292 if (this_scroll_margin || extra_scroll_margin_lines)
11294 start_display (&it, w, scroll_margin_pos);
11295 if (this_scroll_margin)
11296 move_it_vertically_backward (&it, this_scroll_margin);
11297 if (extra_scroll_margin_lines)
11298 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11299 scroll_margin_pos = it.current.pos;
11302 if (PT >= CHARPOS (scroll_margin_pos))
11304 int y0;
11306 /* Point is in the scroll margin at the bottom of the window, or
11307 below. Compute a new window start that makes point visible. */
11309 /* Compute the distance from the scroll margin to PT.
11310 Give up if the distance is greater than scroll_max. */
11311 start_display (&it, w, scroll_margin_pos);
11312 y0 = it.current_y;
11313 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11314 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11316 /* To make point visible, we have to move the window start
11317 down so that the line the cursor is in is visible, which
11318 means we have to add in the height of the cursor line. */
11319 dy = line_bottom_y (&it) - y0;
11321 if (dy > scroll_max)
11322 return SCROLLING_FAILED;
11324 /* Move the window start down. If scrolling conservatively,
11325 move it just enough down to make point visible. If
11326 scroll_step is set, move it down by scroll_step. */
11327 start_display (&it, w, startp);
11329 if (scroll_conservatively)
11330 /* Set AMOUNT_TO_SCROLL to at least one line,
11331 and at most scroll_conservatively lines. */
11332 amount_to_scroll
11333 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11334 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11335 else if (scroll_step || temp_scroll_step)
11336 amount_to_scroll = scroll_max;
11337 else
11339 aggressive = current_buffer->scroll_up_aggressively;
11340 height = WINDOW_BOX_TEXT_HEIGHT (w);
11341 if (NUMBERP (aggressive))
11343 double float_amount = XFLOATINT (aggressive) * height;
11344 amount_to_scroll = float_amount;
11345 if (amount_to_scroll == 0 && float_amount > 0)
11346 amount_to_scroll = 1;
11350 if (amount_to_scroll <= 0)
11351 return SCROLLING_FAILED;
11353 /* If moving by amount_to_scroll leaves STARTP unchanged,
11354 move it down one screen line. */
11356 move_it_vertically (&it, amount_to_scroll);
11357 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11358 move_it_by_lines (&it, 1, 1);
11359 startp = it.current.pos;
11361 else
11363 /* See if point is inside the scroll margin at the top of the
11364 window. */
11365 scroll_margin_pos = startp;
11366 if (this_scroll_margin)
11368 start_display (&it, w, startp);
11369 move_it_vertically (&it, this_scroll_margin);
11370 scroll_margin_pos = it.current.pos;
11373 if (PT < CHARPOS (scroll_margin_pos))
11375 /* Point is in the scroll margin at the top of the window or
11376 above what is displayed in the window. */
11377 int y0;
11379 /* Compute the vertical distance from PT to the scroll
11380 margin position. Give up if distance is greater than
11381 scroll_max. */
11382 SET_TEXT_POS (pos, PT, PT_BYTE);
11383 start_display (&it, w, pos);
11384 y0 = it.current_y;
11385 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11386 it.last_visible_y, -1,
11387 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11388 dy = it.current_y - y0;
11389 if (dy > scroll_max)
11390 return SCROLLING_FAILED;
11392 /* Compute new window start. */
11393 start_display (&it, w, startp);
11395 if (scroll_conservatively)
11396 amount_to_scroll
11397 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11398 else if (scroll_step || temp_scroll_step)
11399 amount_to_scroll = scroll_max;
11400 else
11402 aggressive = current_buffer->scroll_down_aggressively;
11403 height = WINDOW_BOX_TEXT_HEIGHT (w);
11404 if (NUMBERP (aggressive))
11406 double float_amount = XFLOATINT (aggressive) * height;
11407 amount_to_scroll = float_amount;
11408 if (amount_to_scroll == 0 && float_amount > 0)
11409 amount_to_scroll = 1;
11413 if (amount_to_scroll <= 0)
11414 return SCROLLING_FAILED;
11416 move_it_vertically_backward (&it, amount_to_scroll);
11417 startp = it.current.pos;
11421 /* Run window scroll functions. */
11422 startp = run_window_scroll_functions (window, startp);
11424 /* Display the window. Give up if new fonts are loaded, or if point
11425 doesn't appear. */
11426 if (!try_window (window, startp))
11427 rc = SCROLLING_NEED_LARGER_MATRICES;
11428 else if (w->cursor.vpos < 0)
11430 clear_glyph_matrix (w->desired_matrix);
11431 rc = SCROLLING_FAILED;
11433 else
11435 /* Maybe forget recorded base line for line number display. */
11436 if (!just_this_one_p
11437 || current_buffer->clip_changed
11438 || BEG_UNCHANGED < CHARPOS (startp))
11439 w->base_line_number = Qnil;
11441 /* If cursor ends up on a partially visible line,
11442 treat that as being off the bottom of the screen. */
11443 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11445 clear_glyph_matrix (w->desired_matrix);
11446 ++extra_scroll_margin_lines;
11447 goto too_near_end;
11449 rc = SCROLLING_SUCCESS;
11452 return rc;
11456 /* Compute a suitable window start for window W if display of W starts
11457 on a continuation line. Value is non-zero if a new window start
11458 was computed.
11460 The new window start will be computed, based on W's width, starting
11461 from the start of the continued line. It is the start of the
11462 screen line with the minimum distance from the old start W->start. */
11464 static int
11465 compute_window_start_on_continuation_line (w)
11466 struct window *w;
11468 struct text_pos pos, start_pos;
11469 int window_start_changed_p = 0;
11471 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11473 /* If window start is on a continuation line... Window start may be
11474 < BEGV in case there's invisible text at the start of the
11475 buffer (M-x rmail, for example). */
11476 if (CHARPOS (start_pos) > BEGV
11477 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11479 struct it it;
11480 struct glyph_row *row;
11482 /* Handle the case that the window start is out of range. */
11483 if (CHARPOS (start_pos) < BEGV)
11484 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11485 else if (CHARPOS (start_pos) > ZV)
11486 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11488 /* Find the start of the continued line. This should be fast
11489 because scan_buffer is fast (newline cache). */
11490 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11491 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11492 row, DEFAULT_FACE_ID);
11493 reseat_at_previous_visible_line_start (&it);
11495 /* If the line start is "too far" away from the window start,
11496 say it takes too much time to compute a new window start. */
11497 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11498 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11500 int min_distance, distance;
11502 /* Move forward by display lines to find the new window
11503 start. If window width was enlarged, the new start can
11504 be expected to be > the old start. If window width was
11505 decreased, the new window start will be < the old start.
11506 So, we're looking for the display line start with the
11507 minimum distance from the old window start. */
11508 pos = it.current.pos;
11509 min_distance = INFINITY;
11510 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11511 distance < min_distance)
11513 min_distance = distance;
11514 pos = it.current.pos;
11515 move_it_by_lines (&it, 1, 0);
11518 /* Set the window start there. */
11519 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11520 window_start_changed_p = 1;
11524 return window_start_changed_p;
11528 /* Try cursor movement in case text has not changed in window WINDOW,
11529 with window start STARTP. Value is
11531 CURSOR_MOVEMENT_SUCCESS if successful
11533 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11535 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11536 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11537 we want to scroll as if scroll-step were set to 1. See the code.
11539 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11540 which case we have to abort this redisplay, and adjust matrices
11541 first. */
11543 enum
11545 CURSOR_MOVEMENT_SUCCESS,
11546 CURSOR_MOVEMENT_CANNOT_BE_USED,
11547 CURSOR_MOVEMENT_MUST_SCROLL,
11548 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11551 static int
11552 try_cursor_movement (window, startp, scroll_step)
11553 Lisp_Object window;
11554 struct text_pos startp;
11555 int *scroll_step;
11557 struct window *w = XWINDOW (window);
11558 struct frame *f = XFRAME (w->frame);
11559 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11561 #if GLYPH_DEBUG
11562 if (inhibit_try_cursor_movement)
11563 return rc;
11564 #endif
11566 /* Handle case where text has not changed, only point, and it has
11567 not moved off the frame. */
11568 if (/* Point may be in this window. */
11569 PT >= CHARPOS (startp)
11570 /* Selective display hasn't changed. */
11571 && !current_buffer->clip_changed
11572 /* Function force-mode-line-update is used to force a thorough
11573 redisplay. It sets either windows_or_buffers_changed or
11574 update_mode_lines. So don't take a shortcut here for these
11575 cases. */
11576 && !update_mode_lines
11577 && !windows_or_buffers_changed
11578 && !cursor_type_changed
11579 /* Can't use this case if highlighting a region. When a
11580 region exists, cursor movement has to do more than just
11581 set the cursor. */
11582 && !(!NILP (Vtransient_mark_mode)
11583 && !NILP (current_buffer->mark_active))
11584 && NILP (w->region_showing)
11585 && NILP (Vshow_trailing_whitespace)
11586 /* Right after splitting windows, last_point may be nil. */
11587 && INTEGERP (w->last_point)
11588 /* This code is not used for mini-buffer for the sake of the case
11589 of redisplaying to replace an echo area message; since in
11590 that case the mini-buffer contents per se are usually
11591 unchanged. This code is of no real use in the mini-buffer
11592 since the handling of this_line_start_pos, etc., in redisplay
11593 handles the same cases. */
11594 && !EQ (window, minibuf_window)
11595 /* When splitting windows or for new windows, it happens that
11596 redisplay is called with a nil window_end_vpos or one being
11597 larger than the window. This should really be fixed in
11598 window.c. I don't have this on my list, now, so we do
11599 approximately the same as the old redisplay code. --gerd. */
11600 && INTEGERP (w->window_end_vpos)
11601 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11602 && (FRAME_WINDOW_P (f)
11603 || !overlay_arrow_in_current_buffer_p ()))
11605 int this_scroll_margin, top_scroll_margin;
11606 struct glyph_row *row = NULL;
11608 #if GLYPH_DEBUG
11609 debug_method_add (w, "cursor movement");
11610 #endif
11612 /* Scroll if point within this distance from the top or bottom
11613 of the window. This is a pixel value. */
11614 this_scroll_margin = max (0, scroll_margin);
11615 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11616 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11618 top_scroll_margin = this_scroll_margin;
11619 if (WINDOW_WANTS_HEADER_LINE_P (w))
11620 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11622 /* Start with the row the cursor was displayed during the last
11623 not paused redisplay. Give up if that row is not valid. */
11624 if (w->last_cursor.vpos < 0
11625 || w->last_cursor.vpos >= w->current_matrix->nrows)
11626 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11627 else
11629 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11630 if (row->mode_line_p)
11631 ++row;
11632 if (!row->enabled_p)
11633 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11636 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11638 int scroll_p = 0;
11639 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11641 if (PT > XFASTINT (w->last_point))
11643 /* Point has moved forward. */
11644 while (MATRIX_ROW_END_CHARPOS (row) < PT
11645 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11647 xassert (row->enabled_p);
11648 ++row;
11651 /* The end position of a row equals the start position
11652 of the next row. If PT is there, we would rather
11653 display it in the next line. */
11654 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11655 && MATRIX_ROW_END_CHARPOS (row) == PT
11656 && !cursor_row_p (w, row))
11657 ++row;
11659 /* If within the scroll margin, scroll. Note that
11660 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11661 the next line would be drawn, and that
11662 this_scroll_margin can be zero. */
11663 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11664 || PT > MATRIX_ROW_END_CHARPOS (row)
11665 /* Line is completely visible last line in window
11666 and PT is to be set in the next line. */
11667 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11668 && PT == MATRIX_ROW_END_CHARPOS (row)
11669 && !row->ends_at_zv_p
11670 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11671 scroll_p = 1;
11673 else if (PT < XFASTINT (w->last_point))
11675 /* Cursor has to be moved backward. Note that PT >=
11676 CHARPOS (startp) because of the outer if-statement. */
11677 while (!row->mode_line_p
11678 && (MATRIX_ROW_START_CHARPOS (row) > PT
11679 || (MATRIX_ROW_START_CHARPOS (row) == PT
11680 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11681 && (row->y > top_scroll_margin
11682 || CHARPOS (startp) == BEGV))
11684 xassert (row->enabled_p);
11685 --row;
11688 /* Consider the following case: Window starts at BEGV,
11689 there is invisible, intangible text at BEGV, so that
11690 display starts at some point START > BEGV. It can
11691 happen that we are called with PT somewhere between
11692 BEGV and START. Try to handle that case. */
11693 if (row < w->current_matrix->rows
11694 || row->mode_line_p)
11696 row = w->current_matrix->rows;
11697 if (row->mode_line_p)
11698 ++row;
11701 /* Due to newlines in overlay strings, we may have to
11702 skip forward over overlay strings. */
11703 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11704 && MATRIX_ROW_END_CHARPOS (row) == PT
11705 && !cursor_row_p (w, row))
11706 ++row;
11708 /* If within the scroll margin, scroll. */
11709 if (row->y < top_scroll_margin
11710 && CHARPOS (startp) != BEGV)
11711 scroll_p = 1;
11713 else
11715 /* Cursor did not move. So don't scroll even if cursor line
11716 is partially visible, as it was so before. */
11717 rc = CURSOR_MOVEMENT_SUCCESS;
11720 if (PT < MATRIX_ROW_START_CHARPOS (row)
11721 || PT > MATRIX_ROW_END_CHARPOS (row))
11723 /* if PT is not in the glyph row, give up. */
11724 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11726 else if (rc != CURSOR_MOVEMENT_SUCCESS
11727 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11728 && make_cursor_line_fully_visible_p)
11730 if (PT == MATRIX_ROW_END_CHARPOS (row)
11731 && !row->ends_at_zv_p
11732 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11733 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11734 else if (row->height > window_box_height (w))
11736 /* If we end up in a partially visible line, let's
11737 make it fully visible, except when it's taller
11738 than the window, in which case we can't do much
11739 about it. */
11740 *scroll_step = 1;
11741 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11743 else
11745 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11746 if (!cursor_row_fully_visible_p (w, 0, 1))
11747 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11748 else
11749 rc = CURSOR_MOVEMENT_SUCCESS;
11752 else if (scroll_p)
11753 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11754 else
11756 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11757 rc = CURSOR_MOVEMENT_SUCCESS;
11762 return rc;
11765 void
11766 set_vertical_scroll_bar (w)
11767 struct window *w;
11769 int start, end, whole;
11771 /* Calculate the start and end positions for the current window.
11772 At some point, it would be nice to choose between scrollbars
11773 which reflect the whole buffer size, with special markers
11774 indicating narrowing, and scrollbars which reflect only the
11775 visible region.
11777 Note that mini-buffers sometimes aren't displaying any text. */
11778 if (!MINI_WINDOW_P (w)
11779 || (w == XWINDOW (minibuf_window)
11780 && NILP (echo_area_buffer[0])))
11782 struct buffer *buf = XBUFFER (w->buffer);
11783 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11784 start = marker_position (w->start) - BUF_BEGV (buf);
11785 /* I don't think this is guaranteed to be right. For the
11786 moment, we'll pretend it is. */
11787 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11789 if (end < start)
11790 end = start;
11791 if (whole < (end - start))
11792 whole = end - start;
11794 else
11795 start = end = whole = 0;
11797 /* Indicate what this scroll bar ought to be displaying now. */
11798 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11802 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11803 selected_window is redisplayed.
11805 We can return without actually redisplaying the window if
11806 fonts_changed_p is nonzero. In that case, redisplay_internal will
11807 retry. */
11809 static void
11810 redisplay_window (window, just_this_one_p)
11811 Lisp_Object window;
11812 int just_this_one_p;
11814 struct window *w = XWINDOW (window);
11815 struct frame *f = XFRAME (w->frame);
11816 struct buffer *buffer = XBUFFER (w->buffer);
11817 struct buffer *old = current_buffer;
11818 struct text_pos lpoint, opoint, startp;
11819 int update_mode_line;
11820 int tem;
11821 struct it it;
11822 /* Record it now because it's overwritten. */
11823 int current_matrix_up_to_date_p = 0;
11824 int used_current_matrix_p = 0;
11825 /* This is less strict than current_matrix_up_to_date_p.
11826 It indictes that the buffer contents and narrowing are unchanged. */
11827 int buffer_unchanged_p = 0;
11828 int temp_scroll_step = 0;
11829 int count = SPECPDL_INDEX ();
11830 int rc;
11831 int centering_position = -1;
11832 int last_line_misfit = 0;
11834 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11835 opoint = lpoint;
11837 /* W must be a leaf window here. */
11838 xassert (!NILP (w->buffer));
11839 #if GLYPH_DEBUG
11840 *w->desired_matrix->method = 0;
11841 #endif
11843 specbind (Qinhibit_point_motion_hooks, Qt);
11845 reconsider_clip_changes (w, buffer);
11847 /* Has the mode line to be updated? */
11848 update_mode_line = (!NILP (w->update_mode_line)
11849 || update_mode_lines
11850 || buffer->clip_changed
11851 || buffer->prevent_redisplay_optimizations_p);
11853 if (MINI_WINDOW_P (w))
11855 if (w == XWINDOW (echo_area_window)
11856 && !NILP (echo_area_buffer[0]))
11858 if (update_mode_line)
11859 /* We may have to update a tty frame's menu bar or a
11860 tool-bar. Example `M-x C-h C-h C-g'. */
11861 goto finish_menu_bars;
11862 else
11863 /* We've already displayed the echo area glyphs in this window. */
11864 goto finish_scroll_bars;
11866 else if ((w != XWINDOW (minibuf_window)
11867 || minibuf_level == 0)
11868 /* When buffer is nonempty, redisplay window normally. */
11869 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11870 /* Quail displays non-mini buffers in minibuffer window.
11871 In that case, redisplay the window normally. */
11872 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11874 /* W is a mini-buffer window, but it's not active, so clear
11875 it. */
11876 int yb = window_text_bottom_y (w);
11877 struct glyph_row *row;
11878 int y;
11880 for (y = 0, row = w->desired_matrix->rows;
11881 y < yb;
11882 y += row->height, ++row)
11883 blank_row (w, row, y);
11884 goto finish_scroll_bars;
11887 clear_glyph_matrix (w->desired_matrix);
11890 /* Otherwise set up data on this window; select its buffer and point
11891 value. */
11892 /* Really select the buffer, for the sake of buffer-local
11893 variables. */
11894 set_buffer_internal_1 (XBUFFER (w->buffer));
11895 SET_TEXT_POS (opoint, PT, PT_BYTE);
11897 current_matrix_up_to_date_p
11898 = (!NILP (w->window_end_valid)
11899 && !current_buffer->clip_changed
11900 && !current_buffer->prevent_redisplay_optimizations_p
11901 && XFASTINT (w->last_modified) >= MODIFF
11902 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11904 buffer_unchanged_p
11905 = (!NILP (w->window_end_valid)
11906 && !current_buffer->clip_changed
11907 && XFASTINT (w->last_modified) >= MODIFF
11908 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11910 /* When windows_or_buffers_changed is non-zero, we can't rely on
11911 the window end being valid, so set it to nil there. */
11912 if (windows_or_buffers_changed)
11914 /* If window starts on a continuation line, maybe adjust the
11915 window start in case the window's width changed. */
11916 if (XMARKER (w->start)->buffer == current_buffer)
11917 compute_window_start_on_continuation_line (w);
11919 w->window_end_valid = Qnil;
11922 /* Some sanity checks. */
11923 CHECK_WINDOW_END (w);
11924 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11925 abort ();
11926 if (BYTEPOS (opoint) < CHARPOS (opoint))
11927 abort ();
11929 /* If %c is in mode line, update it if needed. */
11930 if (!NILP (w->column_number_displayed)
11931 /* This alternative quickly identifies a common case
11932 where no change is needed. */
11933 && !(PT == XFASTINT (w->last_point)
11934 && XFASTINT (w->last_modified) >= MODIFF
11935 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11936 && (XFASTINT (w->column_number_displayed)
11937 != (int) current_column ())) /* iftc */
11938 update_mode_line = 1;
11940 /* Count number of windows showing the selected buffer. An indirect
11941 buffer counts as its base buffer. */
11942 if (!just_this_one_p)
11944 struct buffer *current_base, *window_base;
11945 current_base = current_buffer;
11946 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11947 if (current_base->base_buffer)
11948 current_base = current_base->base_buffer;
11949 if (window_base->base_buffer)
11950 window_base = window_base->base_buffer;
11951 if (current_base == window_base)
11952 buffer_shared++;
11955 /* Point refers normally to the selected window. For any other
11956 window, set up appropriate value. */
11957 if (!EQ (window, selected_window))
11959 int new_pt = XMARKER (w->pointm)->charpos;
11960 int new_pt_byte = marker_byte_position (w->pointm);
11961 if (new_pt < BEGV)
11963 new_pt = BEGV;
11964 new_pt_byte = BEGV_BYTE;
11965 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11967 else if (new_pt > (ZV - 1))
11969 new_pt = ZV;
11970 new_pt_byte = ZV_BYTE;
11971 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11974 /* We don't use SET_PT so that the point-motion hooks don't run. */
11975 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11978 /* If any of the character widths specified in the display table
11979 have changed, invalidate the width run cache. It's true that
11980 this may be a bit late to catch such changes, but the rest of
11981 redisplay goes (non-fatally) haywire when the display table is
11982 changed, so why should we worry about doing any better? */
11983 if (current_buffer->width_run_cache)
11985 struct Lisp_Char_Table *disptab = buffer_display_table ();
11987 if (! disptab_matches_widthtab (disptab,
11988 XVECTOR (current_buffer->width_table)))
11990 invalidate_region_cache (current_buffer,
11991 current_buffer->width_run_cache,
11992 BEG, Z);
11993 recompute_width_table (current_buffer, disptab);
11997 /* If window-start is screwed up, choose a new one. */
11998 if (XMARKER (w->start)->buffer != current_buffer)
11999 goto recenter;
12001 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12003 /* If someone specified a new starting point but did not insist,
12004 check whether it can be used. */
12005 if (!NILP (w->optional_new_start)
12006 && CHARPOS (startp) >= BEGV
12007 && CHARPOS (startp) <= ZV)
12009 w->optional_new_start = Qnil;
12010 start_display (&it, w, startp);
12011 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12012 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12013 if (IT_CHARPOS (it) == PT)
12014 w->force_start = Qt;
12015 /* IT may overshoot PT if text at PT is invisible. */
12016 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12017 w->force_start = Qt;
12022 /* Handle case where place to start displaying has been specified,
12023 unless the specified location is outside the accessible range. */
12024 if (!NILP (w->force_start)
12025 || w->frozen_window_start_p)
12027 /* We set this later on if we have to adjust point. */
12028 int new_vpos = -1;
12030 w->force_start = Qnil;
12031 w->vscroll = 0;
12032 w->window_end_valid = Qnil;
12034 /* Forget any recorded base line for line number display. */
12035 if (!buffer_unchanged_p)
12036 w->base_line_number = Qnil;
12038 /* Redisplay the mode line. Select the buffer properly for that.
12039 Also, run the hook window-scroll-functions
12040 because we have scrolled. */
12041 /* Note, we do this after clearing force_start because
12042 if there's an error, it is better to forget about force_start
12043 than to get into an infinite loop calling the hook functions
12044 and having them get more errors. */
12045 if (!update_mode_line
12046 || ! NILP (Vwindow_scroll_functions))
12048 update_mode_line = 1;
12049 w->update_mode_line = Qt;
12050 startp = run_window_scroll_functions (window, startp);
12053 w->last_modified = make_number (0);
12054 w->last_overlay_modified = make_number (0);
12055 if (CHARPOS (startp) < BEGV)
12056 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12057 else if (CHARPOS (startp) > ZV)
12058 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12060 /* Redisplay, then check if cursor has been set during the
12061 redisplay. Give up if new fonts were loaded. */
12062 if (!try_window (window, startp))
12064 w->force_start = Qt;
12065 clear_glyph_matrix (w->desired_matrix);
12066 goto need_larger_matrices;
12069 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12071 /* If point does not appear, try to move point so it does
12072 appear. The desired matrix has been built above, so we
12073 can use it here. */
12074 new_vpos = window_box_height (w) / 2;
12077 if (!cursor_row_fully_visible_p (w, 0, 0))
12079 /* Point does appear, but on a line partly visible at end of window.
12080 Move it back to a fully-visible line. */
12081 new_vpos = window_box_height (w);
12084 /* If we need to move point for either of the above reasons,
12085 now actually do it. */
12086 if (new_vpos >= 0)
12088 struct glyph_row *row;
12090 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12091 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12092 ++row;
12094 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12095 MATRIX_ROW_START_BYTEPOS (row));
12097 if (w != XWINDOW (selected_window))
12098 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12099 else if (current_buffer == old)
12100 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12102 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12104 /* If we are highlighting the region, then we just changed
12105 the region, so redisplay to show it. */
12106 if (!NILP (Vtransient_mark_mode)
12107 && !NILP (current_buffer->mark_active))
12109 clear_glyph_matrix (w->desired_matrix);
12110 if (!try_window (window, startp))
12111 goto need_larger_matrices;
12115 #if GLYPH_DEBUG
12116 debug_method_add (w, "forced window start");
12117 #endif
12118 goto done;
12121 /* Handle case where text has not changed, only point, and it has
12122 not moved off the frame, and we are not retrying after hscroll.
12123 (current_matrix_up_to_date_p is nonzero when retrying.) */
12124 if (current_matrix_up_to_date_p
12125 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12126 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12128 switch (rc)
12130 case CURSOR_MOVEMENT_SUCCESS:
12131 used_current_matrix_p = 1;
12132 goto done;
12134 #if 0 /* try_cursor_movement never returns this value. */
12135 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12136 goto need_larger_matrices;
12137 #endif
12139 case CURSOR_MOVEMENT_MUST_SCROLL:
12140 goto try_to_scroll;
12142 default:
12143 abort ();
12146 /* If current starting point was originally the beginning of a line
12147 but no longer is, find a new starting point. */
12148 else if (!NILP (w->start_at_line_beg)
12149 && !(CHARPOS (startp) <= BEGV
12150 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12152 #if GLYPH_DEBUG
12153 debug_method_add (w, "recenter 1");
12154 #endif
12155 goto recenter;
12158 /* Try scrolling with try_window_id. Value is > 0 if update has
12159 been done, it is -1 if we know that the same window start will
12160 not work. It is 0 if unsuccessful for some other reason. */
12161 else if ((tem = try_window_id (w)) != 0)
12163 #if GLYPH_DEBUG
12164 debug_method_add (w, "try_window_id %d", tem);
12165 #endif
12167 if (fonts_changed_p)
12168 goto need_larger_matrices;
12169 if (tem > 0)
12170 goto done;
12172 /* Otherwise try_window_id has returned -1 which means that we
12173 don't want the alternative below this comment to execute. */
12175 else if (CHARPOS (startp) >= BEGV
12176 && CHARPOS (startp) <= ZV
12177 && PT >= CHARPOS (startp)
12178 && (CHARPOS (startp) < ZV
12179 /* Avoid starting at end of buffer. */
12180 || CHARPOS (startp) == BEGV
12181 || (XFASTINT (w->last_modified) >= MODIFF
12182 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12184 #if GLYPH_DEBUG
12185 debug_method_add (w, "same window start");
12186 #endif
12188 /* Try to redisplay starting at same place as before.
12189 If point has not moved off frame, accept the results. */
12190 if (!current_matrix_up_to_date_p
12191 /* Don't use try_window_reusing_current_matrix in this case
12192 because a window scroll function can have changed the
12193 buffer. */
12194 || !NILP (Vwindow_scroll_functions)
12195 || MINI_WINDOW_P (w)
12196 || !(used_current_matrix_p
12197 = try_window_reusing_current_matrix (w)))
12199 IF_DEBUG (debug_method_add (w, "1"));
12200 try_window (window, startp);
12203 if (fonts_changed_p)
12204 goto need_larger_matrices;
12206 if (w->cursor.vpos >= 0)
12208 if (!just_this_one_p
12209 || current_buffer->clip_changed
12210 || BEG_UNCHANGED < CHARPOS (startp))
12211 /* Forget any recorded base line for line number display. */
12212 w->base_line_number = Qnil;
12214 if (!cursor_row_fully_visible_p (w, 1, 0))
12216 clear_glyph_matrix (w->desired_matrix);
12217 last_line_misfit = 1;
12219 /* Drop through and scroll. */
12220 else
12221 goto done;
12223 else
12224 clear_glyph_matrix (w->desired_matrix);
12227 try_to_scroll:
12229 w->last_modified = make_number (0);
12230 w->last_overlay_modified = make_number (0);
12232 /* Redisplay the mode line. Select the buffer properly for that. */
12233 if (!update_mode_line)
12235 update_mode_line = 1;
12236 w->update_mode_line = Qt;
12239 /* Try to scroll by specified few lines. */
12240 if ((scroll_conservatively
12241 || scroll_step
12242 || temp_scroll_step
12243 || NUMBERP (current_buffer->scroll_up_aggressively)
12244 || NUMBERP (current_buffer->scroll_down_aggressively))
12245 && !current_buffer->clip_changed
12246 && CHARPOS (startp) >= BEGV
12247 && CHARPOS (startp) <= ZV)
12249 /* The function returns -1 if new fonts were loaded, 1 if
12250 successful, 0 if not successful. */
12251 int rc = try_scrolling (window, just_this_one_p,
12252 scroll_conservatively,
12253 scroll_step,
12254 temp_scroll_step, last_line_misfit);
12255 switch (rc)
12257 case SCROLLING_SUCCESS:
12258 goto done;
12260 case SCROLLING_NEED_LARGER_MATRICES:
12261 goto need_larger_matrices;
12263 case SCROLLING_FAILED:
12264 break;
12266 default:
12267 abort ();
12271 /* Finally, just choose place to start which centers point */
12273 recenter:
12274 if (centering_position < 0)
12275 centering_position = window_box_height (w) / 2;
12277 #if GLYPH_DEBUG
12278 debug_method_add (w, "recenter");
12279 #endif
12281 /* w->vscroll = 0; */
12283 /* Forget any previously recorded base line for line number display. */
12284 if (!buffer_unchanged_p)
12285 w->base_line_number = Qnil;
12287 /* Move backward half the height of the window. */
12288 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12289 it.current_y = it.last_visible_y;
12290 move_it_vertically_backward (&it, centering_position);
12291 xassert (IT_CHARPOS (it) >= BEGV);
12293 /* The function move_it_vertically_backward may move over more
12294 than the specified y-distance. If it->w is small, e.g. a
12295 mini-buffer window, we may end up in front of the window's
12296 display area. Start displaying at the start of the line
12297 containing PT in this case. */
12298 if (it.current_y <= 0)
12300 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12301 move_it_vertically_backward (&it, 0);
12302 #if 0
12303 /* I think this assert is bogus if buffer contains
12304 invisible text or images. KFS. */
12305 xassert (IT_CHARPOS (it) <= PT);
12306 #endif
12307 it.current_y = 0;
12310 it.current_x = it.hpos = 0;
12312 /* Set startp here explicitly in case that helps avoid an infinite loop
12313 in case the window-scroll-functions functions get errors. */
12314 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12316 /* Run scroll hooks. */
12317 startp = run_window_scroll_functions (window, it.current.pos);
12319 /* Redisplay the window. */
12320 if (!current_matrix_up_to_date_p
12321 || windows_or_buffers_changed
12322 || cursor_type_changed
12323 /* Don't use try_window_reusing_current_matrix in this case
12324 because it can have changed the buffer. */
12325 || !NILP (Vwindow_scroll_functions)
12326 || !just_this_one_p
12327 || MINI_WINDOW_P (w)
12328 || !(used_current_matrix_p
12329 = try_window_reusing_current_matrix (w)))
12330 try_window (window, startp);
12332 /* If new fonts have been loaded (due to fontsets), give up. We
12333 have to start a new redisplay since we need to re-adjust glyph
12334 matrices. */
12335 if (fonts_changed_p)
12336 goto need_larger_matrices;
12338 /* If cursor did not appear assume that the middle of the window is
12339 in the first line of the window. Do it again with the next line.
12340 (Imagine a window of height 100, displaying two lines of height
12341 60. Moving back 50 from it->last_visible_y will end in the first
12342 line.) */
12343 if (w->cursor.vpos < 0)
12345 if (!NILP (w->window_end_valid)
12346 && PT >= Z - XFASTINT (w->window_end_pos))
12348 clear_glyph_matrix (w->desired_matrix);
12349 move_it_by_lines (&it, 1, 0);
12350 try_window (window, it.current.pos);
12352 else if (PT < IT_CHARPOS (it))
12354 clear_glyph_matrix (w->desired_matrix);
12355 move_it_by_lines (&it, -1, 0);
12356 try_window (window, it.current.pos);
12358 else
12360 /* Not much we can do about it. */
12364 /* Consider the following case: Window starts at BEGV, there is
12365 invisible, intangible text at BEGV, so that display starts at
12366 some point START > BEGV. It can happen that we are called with
12367 PT somewhere between BEGV and START. Try to handle that case. */
12368 if (w->cursor.vpos < 0)
12370 struct glyph_row *row = w->current_matrix->rows;
12371 if (row->mode_line_p)
12372 ++row;
12373 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12376 if (!cursor_row_fully_visible_p (w, 0, 0))
12378 /* If vscroll is enabled, disable it and try again. */
12379 if (w->vscroll)
12381 w->vscroll = 0;
12382 clear_glyph_matrix (w->desired_matrix);
12383 goto recenter;
12386 /* If centering point failed to make the whole line visible,
12387 put point at the top instead. That has to make the whole line
12388 visible, if it can be done. */
12389 if (centering_position == 0)
12390 goto done;
12392 clear_glyph_matrix (w->desired_matrix);
12393 centering_position = 0;
12394 goto recenter;
12397 done:
12399 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12400 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12401 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12402 ? Qt : Qnil);
12404 /* Display the mode line, if we must. */
12405 if ((update_mode_line
12406 /* If window not full width, must redo its mode line
12407 if (a) the window to its side is being redone and
12408 (b) we do a frame-based redisplay. This is a consequence
12409 of how inverted lines are drawn in frame-based redisplay. */
12410 || (!just_this_one_p
12411 && !FRAME_WINDOW_P (f)
12412 && !WINDOW_FULL_WIDTH_P (w))
12413 /* Line number to display. */
12414 || INTEGERP (w->base_line_pos)
12415 /* Column number is displayed and different from the one displayed. */
12416 || (!NILP (w->column_number_displayed)
12417 && (XFASTINT (w->column_number_displayed)
12418 != (int) current_column ()))) /* iftc */
12419 /* This means that the window has a mode line. */
12420 && (WINDOW_WANTS_MODELINE_P (w)
12421 || WINDOW_WANTS_HEADER_LINE_P (w)))
12423 display_mode_lines (w);
12425 /* If mode line height has changed, arrange for a thorough
12426 immediate redisplay using the correct mode line height. */
12427 if (WINDOW_WANTS_MODELINE_P (w)
12428 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12430 fonts_changed_p = 1;
12431 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12432 = DESIRED_MODE_LINE_HEIGHT (w);
12435 /* If top line height has changed, arrange for a thorough
12436 immediate redisplay using the correct mode line height. */
12437 if (WINDOW_WANTS_HEADER_LINE_P (w)
12438 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12440 fonts_changed_p = 1;
12441 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12442 = DESIRED_HEADER_LINE_HEIGHT (w);
12445 if (fonts_changed_p)
12446 goto need_larger_matrices;
12449 if (!line_number_displayed
12450 && !BUFFERP (w->base_line_pos))
12452 w->base_line_pos = Qnil;
12453 w->base_line_number = Qnil;
12456 finish_menu_bars:
12458 /* When we reach a frame's selected window, redo the frame's menu bar. */
12459 if (update_mode_line
12460 && EQ (FRAME_SELECTED_WINDOW (f), window))
12462 int redisplay_menu_p = 0;
12463 int redisplay_tool_bar_p = 0;
12465 if (FRAME_WINDOW_P (f))
12467 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12468 || defined (USE_GTK)
12469 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12470 #else
12471 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12472 #endif
12474 else
12475 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12477 if (redisplay_menu_p)
12478 display_menu_bar (w);
12480 #ifdef HAVE_WINDOW_SYSTEM
12481 #ifdef USE_GTK
12482 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12483 #else
12484 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12485 && (FRAME_TOOL_BAR_LINES (f) > 0
12486 || auto_resize_tool_bars_p);
12488 #endif
12490 if (redisplay_tool_bar_p)
12491 redisplay_tool_bar (f);
12492 #endif
12495 #ifdef HAVE_WINDOW_SYSTEM
12496 if (FRAME_WINDOW_P (f)
12497 && update_window_fringes (w, 0)
12498 && !just_this_one_p
12499 && (used_current_matrix_p || overlay_arrow_seen)
12500 && !w->pseudo_window_p)
12502 update_begin (f);
12503 BLOCK_INPUT;
12504 if (draw_window_fringes (w, 1))
12505 x_draw_vertical_border (w);
12506 UNBLOCK_INPUT;
12507 update_end (f);
12509 #endif /* HAVE_WINDOW_SYSTEM */
12511 /* We go to this label, with fonts_changed_p nonzero,
12512 if it is necessary to try again using larger glyph matrices.
12513 We have to redeem the scroll bar even in this case,
12514 because the loop in redisplay_internal expects that. */
12515 need_larger_matrices:
12517 finish_scroll_bars:
12519 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12521 /* Set the thumb's position and size. */
12522 set_vertical_scroll_bar (w);
12524 /* Note that we actually used the scroll bar attached to this
12525 window, so it shouldn't be deleted at the end of redisplay. */
12526 redeem_scroll_bar_hook (w);
12529 /* Restore current_buffer and value of point in it. */
12530 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12531 set_buffer_internal_1 (old);
12532 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12534 unbind_to (count, Qnil);
12538 /* Build the complete desired matrix of WINDOW with a window start
12539 buffer position POS. Value is non-zero if successful. It is zero
12540 if fonts were loaded during redisplay which makes re-adjusting
12541 glyph matrices necessary. */
12544 try_window (window, pos)
12545 Lisp_Object window;
12546 struct text_pos pos;
12548 struct window *w = XWINDOW (window);
12549 struct it it;
12550 struct glyph_row *last_text_row = NULL;
12552 /* Make POS the new window start. */
12553 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12555 /* Mark cursor position as unknown. No overlay arrow seen. */
12556 w->cursor.vpos = -1;
12557 overlay_arrow_seen = 0;
12559 /* Initialize iterator and info to start at POS. */
12560 start_display (&it, w, pos);
12562 /* Display all lines of W. */
12563 while (it.current_y < it.last_visible_y)
12565 if (display_line (&it))
12566 last_text_row = it.glyph_row - 1;
12567 if (fonts_changed_p)
12568 return 0;
12571 /* If bottom moved off end of frame, change mode line percentage. */
12572 if (XFASTINT (w->window_end_pos) <= 0
12573 && Z != IT_CHARPOS (it))
12574 w->update_mode_line = Qt;
12576 /* Set window_end_pos to the offset of the last character displayed
12577 on the window from the end of current_buffer. Set
12578 window_end_vpos to its row number. */
12579 if (last_text_row)
12581 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12582 w->window_end_bytepos
12583 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12584 w->window_end_pos
12585 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12586 w->window_end_vpos
12587 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12588 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12589 ->displays_text_p);
12591 else
12593 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12594 w->window_end_pos = make_number (Z - ZV);
12595 w->window_end_vpos = make_number (0);
12598 /* But that is not valid info until redisplay finishes. */
12599 w->window_end_valid = Qnil;
12600 return 1;
12605 /************************************************************************
12606 Window redisplay reusing current matrix when buffer has not changed
12607 ************************************************************************/
12609 /* Try redisplay of window W showing an unchanged buffer with a
12610 different window start than the last time it was displayed by
12611 reusing its current matrix. Value is non-zero if successful.
12612 W->start is the new window start. */
12614 static int
12615 try_window_reusing_current_matrix (w)
12616 struct window *w;
12618 struct frame *f = XFRAME (w->frame);
12619 struct glyph_row *row, *bottom_row;
12620 struct it it;
12621 struct run run;
12622 struct text_pos start, new_start;
12623 int nrows_scrolled, i;
12624 struct glyph_row *last_text_row;
12625 struct glyph_row *last_reused_text_row;
12626 struct glyph_row *start_row;
12627 int start_vpos, min_y, max_y;
12629 #if GLYPH_DEBUG
12630 if (inhibit_try_window_reusing)
12631 return 0;
12632 #endif
12634 if (/* This function doesn't handle terminal frames. */
12635 !FRAME_WINDOW_P (f)
12636 /* Don't try to reuse the display if windows have been split
12637 or such. */
12638 || windows_or_buffers_changed
12639 || cursor_type_changed)
12640 return 0;
12642 /* Can't do this if region may have changed. */
12643 if ((!NILP (Vtransient_mark_mode)
12644 && !NILP (current_buffer->mark_active))
12645 || !NILP (w->region_showing)
12646 || !NILP (Vshow_trailing_whitespace))
12647 return 0;
12649 /* If top-line visibility has changed, give up. */
12650 if (WINDOW_WANTS_HEADER_LINE_P (w)
12651 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12652 return 0;
12654 /* Give up if old or new display is scrolled vertically. We could
12655 make this function handle this, but right now it doesn't. */
12656 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12657 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12658 return 0;
12660 /* The variable new_start now holds the new window start. The old
12661 start `start' can be determined from the current matrix. */
12662 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12663 start = start_row->start.pos;
12664 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12666 /* Clear the desired matrix for the display below. */
12667 clear_glyph_matrix (w->desired_matrix);
12669 if (CHARPOS (new_start) <= CHARPOS (start))
12671 int first_row_y;
12673 /* Don't use this method if the display starts with an ellipsis
12674 displayed for invisible text. It's not easy to handle that case
12675 below, and it's certainly not worth the effort since this is
12676 not a frequent case. */
12677 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12678 return 0;
12680 IF_DEBUG (debug_method_add (w, "twu1"));
12682 /* Display up to a row that can be reused. The variable
12683 last_text_row is set to the last row displayed that displays
12684 text. Note that it.vpos == 0 if or if not there is a
12685 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12686 start_display (&it, w, new_start);
12687 first_row_y = it.current_y;
12688 w->cursor.vpos = -1;
12689 last_text_row = last_reused_text_row = NULL;
12691 while (it.current_y < it.last_visible_y
12692 && !fonts_changed_p)
12694 /* If we have reached into the characters in the START row,
12695 that means the line boundaries have changed. So we
12696 can't start copying with the row START. Maybe it will
12697 work to start copying with the following row. */
12698 while (IT_CHARPOS (it) > CHARPOS (start))
12700 /* Advance to the next row as the "start". */
12701 start_row++;
12702 start = start_row->start.pos;
12703 /* If there are no more rows to try, or just one, give up. */
12704 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12705 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12706 || CHARPOS (start) == ZV)
12708 clear_glyph_matrix (w->desired_matrix);
12709 return 0;
12712 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12714 /* If we have reached alignment,
12715 we can copy the rest of the rows. */
12716 if (IT_CHARPOS (it) == CHARPOS (start))
12717 break;
12719 if (display_line (&it))
12720 last_text_row = it.glyph_row - 1;
12723 /* A value of current_y < last_visible_y means that we stopped
12724 at the previous window start, which in turn means that we
12725 have at least one reusable row. */
12726 if (it.current_y < it.last_visible_y)
12728 /* IT.vpos always starts from 0; it counts text lines. */
12729 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12731 /* Find PT if not already found in the lines displayed. */
12732 if (w->cursor.vpos < 0)
12734 int dy = it.current_y - start_row->y;
12736 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12737 row = row_containing_pos (w, PT, row, NULL, dy);
12738 if (row)
12739 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12740 dy, nrows_scrolled);
12741 else
12743 clear_glyph_matrix (w->desired_matrix);
12744 return 0;
12748 /* Scroll the display. Do it before the current matrix is
12749 changed. The problem here is that update has not yet
12750 run, i.e. part of the current matrix is not up to date.
12751 scroll_run_hook will clear the cursor, and use the
12752 current matrix to get the height of the row the cursor is
12753 in. */
12754 run.current_y = start_row->y;
12755 run.desired_y = it.current_y;
12756 run.height = it.last_visible_y - it.current_y;
12758 if (run.height > 0 && run.current_y != run.desired_y)
12760 update_begin (f);
12761 rif->update_window_begin_hook (w);
12762 rif->clear_window_mouse_face (w);
12763 rif->scroll_run_hook (w, &run);
12764 rif->update_window_end_hook (w, 0, 0);
12765 update_end (f);
12768 /* Shift current matrix down by nrows_scrolled lines. */
12769 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12770 rotate_matrix (w->current_matrix,
12771 start_vpos,
12772 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12773 nrows_scrolled);
12775 /* Disable lines that must be updated. */
12776 for (i = 0; i < it.vpos; ++i)
12777 (start_row + i)->enabled_p = 0;
12779 /* Re-compute Y positions. */
12780 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12781 max_y = it.last_visible_y;
12782 for (row = start_row + nrows_scrolled;
12783 row < bottom_row;
12784 ++row)
12786 row->y = it.current_y;
12787 row->visible_height = row->height;
12789 if (row->y < min_y)
12790 row->visible_height -= min_y - row->y;
12791 if (row->y + row->height > max_y)
12792 row->visible_height -= row->y + row->height - max_y;
12793 row->redraw_fringe_bitmaps_p = 1;
12795 it.current_y += row->height;
12797 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12798 last_reused_text_row = row;
12799 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12800 break;
12803 /* Disable lines in the current matrix which are now
12804 below the window. */
12805 for (++row; row < bottom_row; ++row)
12806 row->enabled_p = 0;
12809 /* Update window_end_pos etc.; last_reused_text_row is the last
12810 reused row from the current matrix containing text, if any.
12811 The value of last_text_row is the last displayed line
12812 containing text. */
12813 if (last_reused_text_row)
12815 w->window_end_bytepos
12816 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12817 w->window_end_pos
12818 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12819 w->window_end_vpos
12820 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12821 w->current_matrix));
12823 else if (last_text_row)
12825 w->window_end_bytepos
12826 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12827 w->window_end_pos
12828 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12829 w->window_end_vpos
12830 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12832 else
12834 /* This window must be completely empty. */
12835 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12836 w->window_end_pos = make_number (Z - ZV);
12837 w->window_end_vpos = make_number (0);
12839 w->window_end_valid = Qnil;
12841 /* Update hint: don't try scrolling again in update_window. */
12842 w->desired_matrix->no_scrolling_p = 1;
12844 #if GLYPH_DEBUG
12845 debug_method_add (w, "try_window_reusing_current_matrix 1");
12846 #endif
12847 return 1;
12849 else if (CHARPOS (new_start) > CHARPOS (start))
12851 struct glyph_row *pt_row, *row;
12852 struct glyph_row *first_reusable_row;
12853 struct glyph_row *first_row_to_display;
12854 int dy;
12855 int yb = window_text_bottom_y (w);
12857 /* Find the row starting at new_start, if there is one. Don't
12858 reuse a partially visible line at the end. */
12859 first_reusable_row = start_row;
12860 while (first_reusable_row->enabled_p
12861 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12862 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12863 < CHARPOS (new_start)))
12864 ++first_reusable_row;
12866 /* Give up if there is no row to reuse. */
12867 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12868 || !first_reusable_row->enabled_p
12869 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12870 != CHARPOS (new_start)))
12871 return 0;
12873 /* We can reuse fully visible rows beginning with
12874 first_reusable_row to the end of the window. Set
12875 first_row_to_display to the first row that cannot be reused.
12876 Set pt_row to the row containing point, if there is any. */
12877 pt_row = NULL;
12878 for (first_row_to_display = first_reusable_row;
12879 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12880 ++first_row_to_display)
12882 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12883 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12884 pt_row = first_row_to_display;
12887 /* Start displaying at the start of first_row_to_display. */
12888 xassert (first_row_to_display->y < yb);
12889 init_to_row_start (&it, w, first_row_to_display);
12891 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12892 - start_vpos);
12893 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12894 - nrows_scrolled);
12895 it.current_y = (first_row_to_display->y - first_reusable_row->y
12896 + WINDOW_HEADER_LINE_HEIGHT (w));
12898 /* Display lines beginning with first_row_to_display in the
12899 desired matrix. Set last_text_row to the last row displayed
12900 that displays text. */
12901 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12902 if (pt_row == NULL)
12903 w->cursor.vpos = -1;
12904 last_text_row = NULL;
12905 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12906 if (display_line (&it))
12907 last_text_row = it.glyph_row - 1;
12909 /* Give up If point isn't in a row displayed or reused. */
12910 if (w->cursor.vpos < 0)
12912 clear_glyph_matrix (w->desired_matrix);
12913 return 0;
12916 /* If point is in a reused row, adjust y and vpos of the cursor
12917 position. */
12918 if (pt_row)
12920 w->cursor.vpos -= nrows_scrolled;
12921 w->cursor.y -= first_reusable_row->y - start_row->y;
12924 /* Scroll the display. */
12925 run.current_y = first_reusable_row->y;
12926 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12927 run.height = it.last_visible_y - run.current_y;
12928 dy = run.current_y - run.desired_y;
12930 if (run.height)
12932 update_begin (f);
12933 rif->update_window_begin_hook (w);
12934 rif->clear_window_mouse_face (w);
12935 rif->scroll_run_hook (w, &run);
12936 rif->update_window_end_hook (w, 0, 0);
12937 update_end (f);
12940 /* Adjust Y positions of reused rows. */
12941 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12942 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12943 max_y = it.last_visible_y;
12944 for (row = first_reusable_row; row < first_row_to_display; ++row)
12946 row->y -= dy;
12947 row->visible_height = row->height;
12948 if (row->y < min_y)
12949 row->visible_height -= min_y - row->y;
12950 if (row->y + row->height > max_y)
12951 row->visible_height -= row->y + row->height - max_y;
12952 row->redraw_fringe_bitmaps_p = 1;
12955 /* Scroll the current matrix. */
12956 xassert (nrows_scrolled > 0);
12957 rotate_matrix (w->current_matrix,
12958 start_vpos,
12959 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12960 -nrows_scrolled);
12962 /* Disable rows not reused. */
12963 for (row -= nrows_scrolled; row < bottom_row; ++row)
12964 row->enabled_p = 0;
12966 /* Point may have moved to a different line, so we cannot assume that
12967 the previous cursor position is valid; locate the correct row. */
12968 if (pt_row)
12970 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12971 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12972 row++)
12974 w->cursor.vpos++;
12975 w->cursor.y = row->y;
12977 if (row < bottom_row)
12979 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12980 while (glyph->charpos < PT)
12982 w->cursor.hpos++;
12983 w->cursor.x += glyph->pixel_width;
12984 glyph++;
12989 /* Adjust window end. A null value of last_text_row means that
12990 the window end is in reused rows which in turn means that
12991 only its vpos can have changed. */
12992 if (last_text_row)
12994 w->window_end_bytepos
12995 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12996 w->window_end_pos
12997 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12998 w->window_end_vpos
12999 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13001 else
13003 w->window_end_vpos
13004 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13007 w->window_end_valid = Qnil;
13008 w->desired_matrix->no_scrolling_p = 1;
13010 #if GLYPH_DEBUG
13011 debug_method_add (w, "try_window_reusing_current_matrix 2");
13012 #endif
13013 return 1;
13016 return 0;
13021 /************************************************************************
13022 Window redisplay reusing current matrix when buffer has changed
13023 ************************************************************************/
13025 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13026 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13027 int *, int *));
13028 static struct glyph_row *
13029 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13030 struct glyph_row *));
13033 /* Return the last row in MATRIX displaying text. If row START is
13034 non-null, start searching with that row. IT gives the dimensions
13035 of the display. Value is null if matrix is empty; otherwise it is
13036 a pointer to the row found. */
13038 static struct glyph_row *
13039 find_last_row_displaying_text (matrix, it, start)
13040 struct glyph_matrix *matrix;
13041 struct it *it;
13042 struct glyph_row *start;
13044 struct glyph_row *row, *row_found;
13046 /* Set row_found to the last row in IT->w's current matrix
13047 displaying text. The loop looks funny but think of partially
13048 visible lines. */
13049 row_found = NULL;
13050 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13051 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13053 xassert (row->enabled_p);
13054 row_found = row;
13055 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13056 break;
13057 ++row;
13060 return row_found;
13064 /* Return the last row in the current matrix of W that is not affected
13065 by changes at the start of current_buffer that occurred since W's
13066 current matrix was built. Value is null if no such row exists.
13068 BEG_UNCHANGED us the number of characters unchanged at the start of
13069 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13070 first changed character in current_buffer. Characters at positions <
13071 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13072 when the current matrix was built. */
13074 static struct glyph_row *
13075 find_last_unchanged_at_beg_row (w)
13076 struct window *w;
13078 int first_changed_pos = BEG + BEG_UNCHANGED;
13079 struct glyph_row *row;
13080 struct glyph_row *row_found = NULL;
13081 int yb = window_text_bottom_y (w);
13083 /* Find the last row displaying unchanged text. */
13084 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13085 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13086 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13088 if (/* If row ends before first_changed_pos, it is unchanged,
13089 except in some case. */
13090 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13091 /* When row ends in ZV and we write at ZV it is not
13092 unchanged. */
13093 && !row->ends_at_zv_p
13094 /* When first_changed_pos is the end of a continued line,
13095 row is not unchanged because it may be no longer
13096 continued. */
13097 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13098 && (row->continued_p
13099 || row->exact_window_width_line_p)))
13100 row_found = row;
13102 /* Stop if last visible row. */
13103 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13104 break;
13106 ++row;
13109 return row_found;
13113 /* Find the first glyph row in the current matrix of W that is not
13114 affected by changes at the end of current_buffer since the
13115 time W's current matrix was built.
13117 Return in *DELTA the number of chars by which buffer positions in
13118 unchanged text at the end of current_buffer must be adjusted.
13120 Return in *DELTA_BYTES the corresponding number of bytes.
13122 Value is null if no such row exists, i.e. all rows are affected by
13123 changes. */
13125 static struct glyph_row *
13126 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13127 struct window *w;
13128 int *delta, *delta_bytes;
13130 struct glyph_row *row;
13131 struct glyph_row *row_found = NULL;
13133 *delta = *delta_bytes = 0;
13135 /* Display must not have been paused, otherwise the current matrix
13136 is not up to date. */
13137 if (NILP (w->window_end_valid))
13138 abort ();
13140 /* A value of window_end_pos >= END_UNCHANGED means that the window
13141 end is in the range of changed text. If so, there is no
13142 unchanged row at the end of W's current matrix. */
13143 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13144 return NULL;
13146 /* Set row to the last row in W's current matrix displaying text. */
13147 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13149 /* If matrix is entirely empty, no unchanged row exists. */
13150 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13152 /* The value of row is the last glyph row in the matrix having a
13153 meaningful buffer position in it. The end position of row
13154 corresponds to window_end_pos. This allows us to translate
13155 buffer positions in the current matrix to current buffer
13156 positions for characters not in changed text. */
13157 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13158 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13159 int last_unchanged_pos, last_unchanged_pos_old;
13160 struct glyph_row *first_text_row
13161 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13163 *delta = Z - Z_old;
13164 *delta_bytes = Z_BYTE - Z_BYTE_old;
13166 /* Set last_unchanged_pos to the buffer position of the last
13167 character in the buffer that has not been changed. Z is the
13168 index + 1 of the last character in current_buffer, i.e. by
13169 subtracting END_UNCHANGED we get the index of the last
13170 unchanged character, and we have to add BEG to get its buffer
13171 position. */
13172 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13173 last_unchanged_pos_old = last_unchanged_pos - *delta;
13175 /* Search backward from ROW for a row displaying a line that
13176 starts at a minimum position >= last_unchanged_pos_old. */
13177 for (; row > first_text_row; --row)
13179 /* This used to abort, but it can happen.
13180 It is ok to just stop the search instead here. KFS. */
13181 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13182 break;
13184 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13185 row_found = row;
13189 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13190 abort ();
13192 return row_found;
13196 /* Make sure that glyph rows in the current matrix of window W
13197 reference the same glyph memory as corresponding rows in the
13198 frame's frame matrix. This function is called after scrolling W's
13199 current matrix on a terminal frame in try_window_id and
13200 try_window_reusing_current_matrix. */
13202 static void
13203 sync_frame_with_window_matrix_rows (w)
13204 struct window *w;
13206 struct frame *f = XFRAME (w->frame);
13207 struct glyph_row *window_row, *window_row_end, *frame_row;
13209 /* Preconditions: W must be a leaf window and full-width. Its frame
13210 must have a frame matrix. */
13211 xassert (NILP (w->hchild) && NILP (w->vchild));
13212 xassert (WINDOW_FULL_WIDTH_P (w));
13213 xassert (!FRAME_WINDOW_P (f));
13215 /* If W is a full-width window, glyph pointers in W's current matrix
13216 have, by definition, to be the same as glyph pointers in the
13217 corresponding frame matrix. Note that frame matrices have no
13218 marginal areas (see build_frame_matrix). */
13219 window_row = w->current_matrix->rows;
13220 window_row_end = window_row + w->current_matrix->nrows;
13221 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13222 while (window_row < window_row_end)
13224 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13225 struct glyph *end = window_row->glyphs[LAST_AREA];
13227 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13228 frame_row->glyphs[TEXT_AREA] = start;
13229 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13230 frame_row->glyphs[LAST_AREA] = end;
13232 /* Disable frame rows whose corresponding window rows have
13233 been disabled in try_window_id. */
13234 if (!window_row->enabled_p)
13235 frame_row->enabled_p = 0;
13237 ++window_row, ++frame_row;
13242 /* Find the glyph row in window W containing CHARPOS. Consider all
13243 rows between START and END (not inclusive). END null means search
13244 all rows to the end of the display area of W. Value is the row
13245 containing CHARPOS or null. */
13247 struct glyph_row *
13248 row_containing_pos (w, charpos, start, end, dy)
13249 struct window *w;
13250 int charpos;
13251 struct glyph_row *start, *end;
13252 int dy;
13254 struct glyph_row *row = start;
13255 int last_y;
13257 /* If we happen to start on a header-line, skip that. */
13258 if (row->mode_line_p)
13259 ++row;
13261 if ((end && row >= end) || !row->enabled_p)
13262 return NULL;
13264 last_y = window_text_bottom_y (w) - dy;
13266 while (1)
13268 /* Give up if we have gone too far. */
13269 if (end && row >= end)
13270 return NULL;
13271 /* This formerly returned if they were equal.
13272 I think that both quantities are of a "last plus one" type;
13273 if so, when they are equal, the row is within the screen. -- rms. */
13274 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13275 return NULL;
13277 /* If it is in this row, return this row. */
13278 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13279 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13280 /* The end position of a row equals the start
13281 position of the next row. If CHARPOS is there, we
13282 would rather display it in the next line, except
13283 when this line ends in ZV. */
13284 && !row->ends_at_zv_p
13285 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13286 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13287 return row;
13288 ++row;
13293 /* Try to redisplay window W by reusing its existing display. W's
13294 current matrix must be up to date when this function is called,
13295 i.e. window_end_valid must not be nil.
13297 Value is
13299 1 if display has been updated
13300 0 if otherwise unsuccessful
13301 -1 if redisplay with same window start is known not to succeed
13303 The following steps are performed:
13305 1. Find the last row in the current matrix of W that is not
13306 affected by changes at the start of current_buffer. If no such row
13307 is found, give up.
13309 2. Find the first row in W's current matrix that is not affected by
13310 changes at the end of current_buffer. Maybe there is no such row.
13312 3. Display lines beginning with the row + 1 found in step 1 to the
13313 row found in step 2 or, if step 2 didn't find a row, to the end of
13314 the window.
13316 4. If cursor is not known to appear on the window, give up.
13318 5. If display stopped at the row found in step 2, scroll the
13319 display and current matrix as needed.
13321 6. Maybe display some lines at the end of W, if we must. This can
13322 happen under various circumstances, like a partially visible line
13323 becoming fully visible, or because newly displayed lines are displayed
13324 in smaller font sizes.
13326 7. Update W's window end information. */
13328 static int
13329 try_window_id (w)
13330 struct window *w;
13332 struct frame *f = XFRAME (w->frame);
13333 struct glyph_matrix *current_matrix = w->current_matrix;
13334 struct glyph_matrix *desired_matrix = w->desired_matrix;
13335 struct glyph_row *last_unchanged_at_beg_row;
13336 struct glyph_row *first_unchanged_at_end_row;
13337 struct glyph_row *row;
13338 struct glyph_row *bottom_row;
13339 int bottom_vpos;
13340 struct it it;
13341 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13342 struct text_pos start_pos;
13343 struct run run;
13344 int first_unchanged_at_end_vpos = 0;
13345 struct glyph_row *last_text_row, *last_text_row_at_end;
13346 struct text_pos start;
13347 int first_changed_charpos, last_changed_charpos;
13349 #if GLYPH_DEBUG
13350 if (inhibit_try_window_id)
13351 return 0;
13352 #endif
13354 /* This is handy for debugging. */
13355 #if 0
13356 #define GIVE_UP(X) \
13357 do { \
13358 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13359 return 0; \
13360 } while (0)
13361 #else
13362 #define GIVE_UP(X) return 0
13363 #endif
13365 SET_TEXT_POS_FROM_MARKER (start, w->start);
13367 /* Don't use this for mini-windows because these can show
13368 messages and mini-buffers, and we don't handle that here. */
13369 if (MINI_WINDOW_P (w))
13370 GIVE_UP (1);
13372 /* This flag is used to prevent redisplay optimizations. */
13373 if (windows_or_buffers_changed || cursor_type_changed)
13374 GIVE_UP (2);
13376 /* Verify that narrowing has not changed.
13377 Also verify that we were not told to prevent redisplay optimizations.
13378 It would be nice to further
13379 reduce the number of cases where this prevents try_window_id. */
13380 if (current_buffer->clip_changed
13381 || current_buffer->prevent_redisplay_optimizations_p)
13382 GIVE_UP (3);
13384 /* Window must either use window-based redisplay or be full width. */
13385 if (!FRAME_WINDOW_P (f)
13386 && (!line_ins_del_ok
13387 || !WINDOW_FULL_WIDTH_P (w)))
13388 GIVE_UP (4);
13390 /* Give up if point is not known NOT to appear in W. */
13391 if (PT < CHARPOS (start))
13392 GIVE_UP (5);
13394 /* Another way to prevent redisplay optimizations. */
13395 if (XFASTINT (w->last_modified) == 0)
13396 GIVE_UP (6);
13398 /* Verify that window is not hscrolled. */
13399 if (XFASTINT (w->hscroll) != 0)
13400 GIVE_UP (7);
13402 /* Verify that display wasn't paused. */
13403 if (NILP (w->window_end_valid))
13404 GIVE_UP (8);
13406 /* Can't use this if highlighting a region because a cursor movement
13407 will do more than just set the cursor. */
13408 if (!NILP (Vtransient_mark_mode)
13409 && !NILP (current_buffer->mark_active))
13410 GIVE_UP (9);
13412 /* Likewise if highlighting trailing whitespace. */
13413 if (!NILP (Vshow_trailing_whitespace))
13414 GIVE_UP (11);
13416 /* Likewise if showing a region. */
13417 if (!NILP (w->region_showing))
13418 GIVE_UP (10);
13420 /* Can use this if overlay arrow position and or string have changed. */
13421 if (overlay_arrows_changed_p ())
13422 GIVE_UP (12);
13425 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13426 only if buffer has really changed. The reason is that the gap is
13427 initially at Z for freshly visited files. The code below would
13428 set end_unchanged to 0 in that case. */
13429 if (MODIFF > SAVE_MODIFF
13430 /* This seems to happen sometimes after saving a buffer. */
13431 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13433 if (GPT - BEG < BEG_UNCHANGED)
13434 BEG_UNCHANGED = GPT - BEG;
13435 if (Z - GPT < END_UNCHANGED)
13436 END_UNCHANGED = Z - GPT;
13439 /* The position of the first and last character that has been changed. */
13440 first_changed_charpos = BEG + BEG_UNCHANGED;
13441 last_changed_charpos = Z - END_UNCHANGED;
13443 /* If window starts after a line end, and the last change is in
13444 front of that newline, then changes don't affect the display.
13445 This case happens with stealth-fontification. Note that although
13446 the display is unchanged, glyph positions in the matrix have to
13447 be adjusted, of course. */
13448 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13449 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13450 && ((last_changed_charpos < CHARPOS (start)
13451 && CHARPOS (start) == BEGV)
13452 || (last_changed_charpos < CHARPOS (start) - 1
13453 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13455 int Z_old, delta, Z_BYTE_old, delta_bytes;
13456 struct glyph_row *r0;
13458 /* Compute how many chars/bytes have been added to or removed
13459 from the buffer. */
13460 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13461 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13462 delta = Z - Z_old;
13463 delta_bytes = Z_BYTE - Z_BYTE_old;
13465 /* Give up if PT is not in the window. Note that it already has
13466 been checked at the start of try_window_id that PT is not in
13467 front of the window start. */
13468 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13469 GIVE_UP (13);
13471 /* If window start is unchanged, we can reuse the whole matrix
13472 as is, after adjusting glyph positions. No need to compute
13473 the window end again, since its offset from Z hasn't changed. */
13474 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13475 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13476 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13477 /* PT must not be in a partially visible line. */
13478 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13479 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13481 /* Adjust positions in the glyph matrix. */
13482 if (delta || delta_bytes)
13484 struct glyph_row *r1
13485 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13486 increment_matrix_positions (w->current_matrix,
13487 MATRIX_ROW_VPOS (r0, current_matrix),
13488 MATRIX_ROW_VPOS (r1, current_matrix),
13489 delta, delta_bytes);
13492 /* Set the cursor. */
13493 row = row_containing_pos (w, PT, r0, NULL, 0);
13494 if (row)
13495 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13496 else
13497 abort ();
13498 return 1;
13502 /* Handle the case that changes are all below what is displayed in
13503 the window, and that PT is in the window. This shortcut cannot
13504 be taken if ZV is visible in the window, and text has been added
13505 there that is visible in the window. */
13506 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13507 /* ZV is not visible in the window, or there are no
13508 changes at ZV, actually. */
13509 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13510 || first_changed_charpos == last_changed_charpos))
13512 struct glyph_row *r0;
13514 /* Give up if PT is not in the window. Note that it already has
13515 been checked at the start of try_window_id that PT is not in
13516 front of the window start. */
13517 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13518 GIVE_UP (14);
13520 /* If window start is unchanged, we can reuse the whole matrix
13521 as is, without changing glyph positions since no text has
13522 been added/removed in front of the window end. */
13523 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13524 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13525 /* PT must not be in a partially visible line. */
13526 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13527 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13529 /* We have to compute the window end anew since text
13530 can have been added/removed after it. */
13531 w->window_end_pos
13532 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13533 w->window_end_bytepos
13534 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13536 /* Set the cursor. */
13537 row = row_containing_pos (w, PT, r0, NULL, 0);
13538 if (row)
13539 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13540 else
13541 abort ();
13542 return 2;
13546 /* Give up if window start is in the changed area.
13548 The condition used to read
13550 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13552 but why that was tested escapes me at the moment. */
13553 if (CHARPOS (start) >= first_changed_charpos
13554 && CHARPOS (start) <= last_changed_charpos)
13555 GIVE_UP (15);
13557 /* Check that window start agrees with the start of the first glyph
13558 row in its current matrix. Check this after we know the window
13559 start is not in changed text, otherwise positions would not be
13560 comparable. */
13561 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13562 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13563 GIVE_UP (16);
13565 /* Give up if the window ends in strings. Overlay strings
13566 at the end are difficult to handle, so don't try. */
13567 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13568 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13569 GIVE_UP (20);
13571 /* Compute the position at which we have to start displaying new
13572 lines. Some of the lines at the top of the window might be
13573 reusable because they are not displaying changed text. Find the
13574 last row in W's current matrix not affected by changes at the
13575 start of current_buffer. Value is null if changes start in the
13576 first line of window. */
13577 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13578 if (last_unchanged_at_beg_row)
13580 /* Avoid starting to display in the moddle of a character, a TAB
13581 for instance. This is easier than to set up the iterator
13582 exactly, and it's not a frequent case, so the additional
13583 effort wouldn't really pay off. */
13584 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13585 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13586 && last_unchanged_at_beg_row > w->current_matrix->rows)
13587 --last_unchanged_at_beg_row;
13589 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13590 GIVE_UP (17);
13592 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13593 GIVE_UP (18);
13594 start_pos = it.current.pos;
13596 /* Start displaying new lines in the desired matrix at the same
13597 vpos we would use in the current matrix, i.e. below
13598 last_unchanged_at_beg_row. */
13599 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13600 current_matrix);
13601 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13602 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13604 xassert (it.hpos == 0 && it.current_x == 0);
13606 else
13608 /* There are no reusable lines at the start of the window.
13609 Start displaying in the first text line. */
13610 start_display (&it, w, start);
13611 it.vpos = it.first_vpos;
13612 start_pos = it.current.pos;
13615 /* Find the first row that is not affected by changes at the end of
13616 the buffer. Value will be null if there is no unchanged row, in
13617 which case we must redisplay to the end of the window. delta
13618 will be set to the value by which buffer positions beginning with
13619 first_unchanged_at_end_row have to be adjusted due to text
13620 changes. */
13621 first_unchanged_at_end_row
13622 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13623 IF_DEBUG (debug_delta = delta);
13624 IF_DEBUG (debug_delta_bytes = delta_bytes);
13626 /* Set stop_pos to the buffer position up to which we will have to
13627 display new lines. If first_unchanged_at_end_row != NULL, this
13628 is the buffer position of the start of the line displayed in that
13629 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13630 that we don't stop at a buffer position. */
13631 stop_pos = 0;
13632 if (first_unchanged_at_end_row)
13634 xassert (last_unchanged_at_beg_row == NULL
13635 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13637 /* If this is a continuation line, move forward to the next one
13638 that isn't. Changes in lines above affect this line.
13639 Caution: this may move first_unchanged_at_end_row to a row
13640 not displaying text. */
13641 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13642 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13643 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13644 < it.last_visible_y))
13645 ++first_unchanged_at_end_row;
13647 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13648 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13649 >= it.last_visible_y))
13650 first_unchanged_at_end_row = NULL;
13651 else
13653 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13654 + delta);
13655 first_unchanged_at_end_vpos
13656 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13657 xassert (stop_pos >= Z - END_UNCHANGED);
13660 else if (last_unchanged_at_beg_row == NULL)
13661 GIVE_UP (19);
13664 #if GLYPH_DEBUG
13666 /* Either there is no unchanged row at the end, or the one we have
13667 now displays text. This is a necessary condition for the window
13668 end pos calculation at the end of this function. */
13669 xassert (first_unchanged_at_end_row == NULL
13670 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13672 debug_last_unchanged_at_beg_vpos
13673 = (last_unchanged_at_beg_row
13674 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13675 : -1);
13676 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13678 #endif /* GLYPH_DEBUG != 0 */
13681 /* Display new lines. Set last_text_row to the last new line
13682 displayed which has text on it, i.e. might end up as being the
13683 line where the window_end_vpos is. */
13684 w->cursor.vpos = -1;
13685 last_text_row = NULL;
13686 overlay_arrow_seen = 0;
13687 while (it.current_y < it.last_visible_y
13688 && !fonts_changed_p
13689 && (first_unchanged_at_end_row == NULL
13690 || IT_CHARPOS (it) < stop_pos))
13692 if (display_line (&it))
13693 last_text_row = it.glyph_row - 1;
13696 if (fonts_changed_p)
13697 return -1;
13700 /* Compute differences in buffer positions, y-positions etc. for
13701 lines reused at the bottom of the window. Compute what we can
13702 scroll. */
13703 if (first_unchanged_at_end_row
13704 /* No lines reused because we displayed everything up to the
13705 bottom of the window. */
13706 && it.current_y < it.last_visible_y)
13708 dvpos = (it.vpos
13709 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13710 current_matrix));
13711 dy = it.current_y - first_unchanged_at_end_row->y;
13712 run.current_y = first_unchanged_at_end_row->y;
13713 run.desired_y = run.current_y + dy;
13714 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13716 else
13718 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13719 first_unchanged_at_end_row = NULL;
13721 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13724 /* Find the cursor if not already found. We have to decide whether
13725 PT will appear on this window (it sometimes doesn't, but this is
13726 not a very frequent case.) This decision has to be made before
13727 the current matrix is altered. A value of cursor.vpos < 0 means
13728 that PT is either in one of the lines beginning at
13729 first_unchanged_at_end_row or below the window. Don't care for
13730 lines that might be displayed later at the window end; as
13731 mentioned, this is not a frequent case. */
13732 if (w->cursor.vpos < 0)
13734 /* Cursor in unchanged rows at the top? */
13735 if (PT < CHARPOS (start_pos)
13736 && last_unchanged_at_beg_row)
13738 row = row_containing_pos (w, PT,
13739 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13740 last_unchanged_at_beg_row + 1, 0);
13741 if (row)
13742 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13745 /* Start from first_unchanged_at_end_row looking for PT. */
13746 else if (first_unchanged_at_end_row)
13748 row = row_containing_pos (w, PT - delta,
13749 first_unchanged_at_end_row, NULL, 0);
13750 if (row)
13751 set_cursor_from_row (w, row, w->current_matrix, delta,
13752 delta_bytes, dy, dvpos);
13755 /* Give up if cursor was not found. */
13756 if (w->cursor.vpos < 0)
13758 clear_glyph_matrix (w->desired_matrix);
13759 return -1;
13763 /* Don't let the cursor end in the scroll margins. */
13765 int this_scroll_margin, cursor_height;
13767 this_scroll_margin = max (0, scroll_margin);
13768 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13769 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13770 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13772 if ((w->cursor.y < this_scroll_margin
13773 && CHARPOS (start) > BEGV)
13774 /* Old redisplay didn't take scroll margin into account at the bottom,
13775 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13776 || (w->cursor.y + (make_cursor_line_fully_visible_p
13777 ? cursor_height + this_scroll_margin
13778 : 1)) > it.last_visible_y)
13780 w->cursor.vpos = -1;
13781 clear_glyph_matrix (w->desired_matrix);
13782 return -1;
13786 /* Scroll the display. Do it before changing the current matrix so
13787 that xterm.c doesn't get confused about where the cursor glyph is
13788 found. */
13789 if (dy && run.height)
13791 update_begin (f);
13793 if (FRAME_WINDOW_P (f))
13795 rif->update_window_begin_hook (w);
13796 rif->clear_window_mouse_face (w);
13797 rif->scroll_run_hook (w, &run);
13798 rif->update_window_end_hook (w, 0, 0);
13800 else
13802 /* Terminal frame. In this case, dvpos gives the number of
13803 lines to scroll by; dvpos < 0 means scroll up. */
13804 int first_unchanged_at_end_vpos
13805 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13806 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13807 int end = (WINDOW_TOP_EDGE_LINE (w)
13808 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13809 + window_internal_height (w));
13811 /* Perform the operation on the screen. */
13812 if (dvpos > 0)
13814 /* Scroll last_unchanged_at_beg_row to the end of the
13815 window down dvpos lines. */
13816 set_terminal_window (end);
13818 /* On dumb terminals delete dvpos lines at the end
13819 before inserting dvpos empty lines. */
13820 if (!scroll_region_ok)
13821 ins_del_lines (end - dvpos, -dvpos);
13823 /* Insert dvpos empty lines in front of
13824 last_unchanged_at_beg_row. */
13825 ins_del_lines (from, dvpos);
13827 else if (dvpos < 0)
13829 /* Scroll up last_unchanged_at_beg_vpos to the end of
13830 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13831 set_terminal_window (end);
13833 /* Delete dvpos lines in front of
13834 last_unchanged_at_beg_vpos. ins_del_lines will set
13835 the cursor to the given vpos and emit |dvpos| delete
13836 line sequences. */
13837 ins_del_lines (from + dvpos, dvpos);
13839 /* On a dumb terminal insert dvpos empty lines at the
13840 end. */
13841 if (!scroll_region_ok)
13842 ins_del_lines (end + dvpos, -dvpos);
13845 set_terminal_window (0);
13848 update_end (f);
13851 /* Shift reused rows of the current matrix to the right position.
13852 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13853 text. */
13854 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13855 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13856 if (dvpos < 0)
13858 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13859 bottom_vpos, dvpos);
13860 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13861 bottom_vpos, 0);
13863 else if (dvpos > 0)
13865 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13866 bottom_vpos, dvpos);
13867 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13868 first_unchanged_at_end_vpos + dvpos, 0);
13871 /* For frame-based redisplay, make sure that current frame and window
13872 matrix are in sync with respect to glyph memory. */
13873 if (!FRAME_WINDOW_P (f))
13874 sync_frame_with_window_matrix_rows (w);
13876 /* Adjust buffer positions in reused rows. */
13877 if (delta)
13878 increment_matrix_positions (current_matrix,
13879 first_unchanged_at_end_vpos + dvpos,
13880 bottom_vpos, delta, delta_bytes);
13882 /* Adjust Y positions. */
13883 if (dy)
13884 shift_glyph_matrix (w, current_matrix,
13885 first_unchanged_at_end_vpos + dvpos,
13886 bottom_vpos, dy);
13888 if (first_unchanged_at_end_row)
13890 first_unchanged_at_end_row += dvpos;
13891 if (first_unchanged_at_end_row->y >= it.last_visible_y
13892 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
13893 first_unchanged_at_end_row = NULL;
13896 /* If scrolling up, there may be some lines to display at the end of
13897 the window. */
13898 last_text_row_at_end = NULL;
13899 if (dy < 0)
13901 /* Scrolling up can leave for example a partially visible line
13902 at the end of the window to be redisplayed. */
13903 /* Set last_row to the glyph row in the current matrix where the
13904 window end line is found. It has been moved up or down in
13905 the matrix by dvpos. */
13906 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13907 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13909 /* If last_row is the window end line, it should display text. */
13910 xassert (last_row->displays_text_p);
13912 /* If window end line was partially visible before, begin
13913 displaying at that line. Otherwise begin displaying with the
13914 line following it. */
13915 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13917 init_to_row_start (&it, w, last_row);
13918 it.vpos = last_vpos;
13919 it.current_y = last_row->y;
13921 else
13923 init_to_row_end (&it, w, last_row);
13924 it.vpos = 1 + last_vpos;
13925 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13926 ++last_row;
13929 /* We may start in a continuation line. If so, we have to
13930 get the right continuation_lines_width and current_x. */
13931 it.continuation_lines_width = last_row->continuation_lines_width;
13932 it.hpos = it.current_x = 0;
13934 /* Display the rest of the lines at the window end. */
13935 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13936 while (it.current_y < it.last_visible_y
13937 && !fonts_changed_p)
13939 /* Is it always sure that the display agrees with lines in
13940 the current matrix? I don't think so, so we mark rows
13941 displayed invalid in the current matrix by setting their
13942 enabled_p flag to zero. */
13943 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13944 if (display_line (&it))
13945 last_text_row_at_end = it.glyph_row - 1;
13949 /* Update window_end_pos and window_end_vpos. */
13950 if (first_unchanged_at_end_row
13951 && !last_text_row_at_end)
13953 /* Window end line if one of the preserved rows from the current
13954 matrix. Set row to the last row displaying text in current
13955 matrix starting at first_unchanged_at_end_row, after
13956 scrolling. */
13957 xassert (first_unchanged_at_end_row->displays_text_p);
13958 row = find_last_row_displaying_text (w->current_matrix, &it,
13959 first_unchanged_at_end_row);
13960 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13962 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13963 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13964 w->window_end_vpos
13965 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13966 xassert (w->window_end_bytepos >= 0);
13967 IF_DEBUG (debug_method_add (w, "A"));
13969 else if (last_text_row_at_end)
13971 w->window_end_pos
13972 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13973 w->window_end_bytepos
13974 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13975 w->window_end_vpos
13976 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13977 xassert (w->window_end_bytepos >= 0);
13978 IF_DEBUG (debug_method_add (w, "B"));
13980 else if (last_text_row)
13982 /* We have displayed either to the end of the window or at the
13983 end of the window, i.e. the last row with text is to be found
13984 in the desired matrix. */
13985 w->window_end_pos
13986 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13987 w->window_end_bytepos
13988 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13989 w->window_end_vpos
13990 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13991 xassert (w->window_end_bytepos >= 0);
13993 else if (first_unchanged_at_end_row == NULL
13994 && last_text_row == NULL
13995 && last_text_row_at_end == NULL)
13997 /* Displayed to end of window, but no line containing text was
13998 displayed. Lines were deleted at the end of the window. */
13999 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14000 int vpos = XFASTINT (w->window_end_vpos);
14001 struct glyph_row *current_row = current_matrix->rows + vpos;
14002 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14004 for (row = NULL;
14005 row == NULL && vpos >= first_vpos;
14006 --vpos, --current_row, --desired_row)
14008 if (desired_row->enabled_p)
14010 if (desired_row->displays_text_p)
14011 row = desired_row;
14013 else if (current_row->displays_text_p)
14014 row = current_row;
14017 xassert (row != NULL);
14018 w->window_end_vpos = make_number (vpos + 1);
14019 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14020 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14021 xassert (w->window_end_bytepos >= 0);
14022 IF_DEBUG (debug_method_add (w, "C"));
14024 else
14025 abort ();
14027 #if 0 /* This leads to problems, for instance when the cursor is
14028 at ZV, and the cursor line displays no text. */
14029 /* Disable rows below what's displayed in the window. This makes
14030 debugging easier. */
14031 enable_glyph_matrix_rows (current_matrix,
14032 XFASTINT (w->window_end_vpos) + 1,
14033 bottom_vpos, 0);
14034 #endif
14036 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14037 debug_end_vpos = XFASTINT (w->window_end_vpos));
14039 /* Record that display has not been completed. */
14040 w->window_end_valid = Qnil;
14041 w->desired_matrix->no_scrolling_p = 1;
14042 return 3;
14044 #undef GIVE_UP
14049 /***********************************************************************
14050 More debugging support
14051 ***********************************************************************/
14053 #if GLYPH_DEBUG
14055 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14056 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14057 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14060 /* Dump the contents of glyph matrix MATRIX on stderr.
14062 GLYPHS 0 means don't show glyph contents.
14063 GLYPHS 1 means show glyphs in short form
14064 GLYPHS > 1 means show glyphs in long form. */
14066 void
14067 dump_glyph_matrix (matrix, glyphs)
14068 struct glyph_matrix *matrix;
14069 int glyphs;
14071 int i;
14072 for (i = 0; i < matrix->nrows; ++i)
14073 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14077 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14078 the glyph row and area where the glyph comes from. */
14080 void
14081 dump_glyph (row, glyph, area)
14082 struct glyph_row *row;
14083 struct glyph *glyph;
14084 int area;
14086 if (glyph->type == CHAR_GLYPH)
14088 fprintf (stderr,
14089 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14090 glyph - row->glyphs[TEXT_AREA],
14091 'C',
14092 glyph->charpos,
14093 (BUFFERP (glyph->object)
14094 ? 'B'
14095 : (STRINGP (glyph->object)
14096 ? 'S'
14097 : '-')),
14098 glyph->pixel_width,
14099 glyph->u.ch,
14100 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14101 ? glyph->u.ch
14102 : '.'),
14103 glyph->face_id,
14104 glyph->left_box_line_p,
14105 glyph->right_box_line_p);
14107 else if (glyph->type == STRETCH_GLYPH)
14109 fprintf (stderr,
14110 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14111 glyph - row->glyphs[TEXT_AREA],
14112 'S',
14113 glyph->charpos,
14114 (BUFFERP (glyph->object)
14115 ? 'B'
14116 : (STRINGP (glyph->object)
14117 ? 'S'
14118 : '-')),
14119 glyph->pixel_width,
14121 '.',
14122 glyph->face_id,
14123 glyph->left_box_line_p,
14124 glyph->right_box_line_p);
14126 else if (glyph->type == IMAGE_GLYPH)
14128 fprintf (stderr,
14129 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14130 glyph - row->glyphs[TEXT_AREA],
14131 'I',
14132 glyph->charpos,
14133 (BUFFERP (glyph->object)
14134 ? 'B'
14135 : (STRINGP (glyph->object)
14136 ? 'S'
14137 : '-')),
14138 glyph->pixel_width,
14139 glyph->u.img_id,
14140 '.',
14141 glyph->face_id,
14142 glyph->left_box_line_p,
14143 glyph->right_box_line_p);
14148 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14149 GLYPHS 0 means don't show glyph contents.
14150 GLYPHS 1 means show glyphs in short form
14151 GLYPHS > 1 means show glyphs in long form. */
14153 void
14154 dump_glyph_row (row, vpos, glyphs)
14155 struct glyph_row *row;
14156 int vpos, glyphs;
14158 if (glyphs != 1)
14160 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14161 fprintf (stderr, "======================================================================\n");
14163 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14164 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14165 vpos,
14166 MATRIX_ROW_START_CHARPOS (row),
14167 MATRIX_ROW_END_CHARPOS (row),
14168 row->used[TEXT_AREA],
14169 row->contains_overlapping_glyphs_p,
14170 row->enabled_p,
14171 row->truncated_on_left_p,
14172 row->truncated_on_right_p,
14173 row->continued_p,
14174 MATRIX_ROW_CONTINUATION_LINE_P (row),
14175 row->displays_text_p,
14176 row->ends_at_zv_p,
14177 row->fill_line_p,
14178 row->ends_in_middle_of_char_p,
14179 row->starts_in_middle_of_char_p,
14180 row->mouse_face_p,
14181 row->x,
14182 row->y,
14183 row->pixel_width,
14184 row->height,
14185 row->visible_height,
14186 row->ascent,
14187 row->phys_ascent);
14188 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14189 row->end.overlay_string_index,
14190 row->continuation_lines_width);
14191 fprintf (stderr, "%9d %5d\n",
14192 CHARPOS (row->start.string_pos),
14193 CHARPOS (row->end.string_pos));
14194 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14195 row->end.dpvec_index);
14198 if (glyphs > 1)
14200 int area;
14202 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14204 struct glyph *glyph = row->glyphs[area];
14205 struct glyph *glyph_end = glyph + row->used[area];
14207 /* Glyph for a line end in text. */
14208 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14209 ++glyph_end;
14211 if (glyph < glyph_end)
14212 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14214 for (; glyph < glyph_end; ++glyph)
14215 dump_glyph (row, glyph, area);
14218 else if (glyphs == 1)
14220 int area;
14222 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14224 char *s = (char *) alloca (row->used[area] + 1);
14225 int i;
14227 for (i = 0; i < row->used[area]; ++i)
14229 struct glyph *glyph = row->glyphs[area] + i;
14230 if (glyph->type == CHAR_GLYPH
14231 && glyph->u.ch < 0x80
14232 && glyph->u.ch >= ' ')
14233 s[i] = glyph->u.ch;
14234 else
14235 s[i] = '.';
14238 s[i] = '\0';
14239 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14245 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14246 Sdump_glyph_matrix, 0, 1, "p",
14247 doc: /* Dump the current matrix of the selected window to stderr.
14248 Shows contents of glyph row structures. With non-nil
14249 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14250 glyphs in short form, otherwise show glyphs in long form. */)
14251 (glyphs)
14252 Lisp_Object glyphs;
14254 struct window *w = XWINDOW (selected_window);
14255 struct buffer *buffer = XBUFFER (w->buffer);
14257 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14258 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14259 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14260 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14261 fprintf (stderr, "=============================================\n");
14262 dump_glyph_matrix (w->current_matrix,
14263 NILP (glyphs) ? 0 : XINT (glyphs));
14264 return Qnil;
14268 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14269 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14272 struct frame *f = XFRAME (selected_frame);
14273 dump_glyph_matrix (f->current_matrix, 1);
14274 return Qnil;
14278 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14279 doc: /* Dump glyph row ROW to stderr.
14280 GLYPH 0 means don't dump glyphs.
14281 GLYPH 1 means dump glyphs in short form.
14282 GLYPH > 1 or omitted means dump glyphs in long form. */)
14283 (row, glyphs)
14284 Lisp_Object row, glyphs;
14286 struct glyph_matrix *matrix;
14287 int vpos;
14289 CHECK_NUMBER (row);
14290 matrix = XWINDOW (selected_window)->current_matrix;
14291 vpos = XINT (row);
14292 if (vpos >= 0 && vpos < matrix->nrows)
14293 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14294 vpos,
14295 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14296 return Qnil;
14300 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14301 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14302 GLYPH 0 means don't dump glyphs.
14303 GLYPH 1 means dump glyphs in short form.
14304 GLYPH > 1 or omitted means dump glyphs in long form. */)
14305 (row, glyphs)
14306 Lisp_Object row, glyphs;
14308 struct frame *sf = SELECTED_FRAME ();
14309 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14310 int vpos;
14312 CHECK_NUMBER (row);
14313 vpos = XINT (row);
14314 if (vpos >= 0 && vpos < m->nrows)
14315 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14316 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14317 return Qnil;
14321 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14322 doc: /* Toggle tracing of redisplay.
14323 With ARG, turn tracing on if and only if ARG is positive. */)
14324 (arg)
14325 Lisp_Object arg;
14327 if (NILP (arg))
14328 trace_redisplay_p = !trace_redisplay_p;
14329 else
14331 arg = Fprefix_numeric_value (arg);
14332 trace_redisplay_p = XINT (arg) > 0;
14335 return Qnil;
14339 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14340 doc: /* Like `format', but print result to stderr.
14341 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14342 (nargs, args)
14343 int nargs;
14344 Lisp_Object *args;
14346 Lisp_Object s = Fformat (nargs, args);
14347 fprintf (stderr, "%s", SDATA (s));
14348 return Qnil;
14351 #endif /* GLYPH_DEBUG */
14355 /***********************************************************************
14356 Building Desired Matrix Rows
14357 ***********************************************************************/
14359 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14360 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14362 static struct glyph_row *
14363 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14364 struct window *w;
14365 Lisp_Object overlay_arrow_string;
14367 struct frame *f = XFRAME (WINDOW_FRAME (w));
14368 struct buffer *buffer = XBUFFER (w->buffer);
14369 struct buffer *old = current_buffer;
14370 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14371 int arrow_len = SCHARS (overlay_arrow_string);
14372 const unsigned char *arrow_end = arrow_string + arrow_len;
14373 const unsigned char *p;
14374 struct it it;
14375 int multibyte_p;
14376 int n_glyphs_before;
14378 set_buffer_temp (buffer);
14379 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14380 it.glyph_row->used[TEXT_AREA] = 0;
14381 SET_TEXT_POS (it.position, 0, 0);
14383 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14384 p = arrow_string;
14385 while (p < arrow_end)
14387 Lisp_Object face, ilisp;
14389 /* Get the next character. */
14390 if (multibyte_p)
14391 it.c = string_char_and_length (p, arrow_len, &it.len);
14392 else
14393 it.c = *p, it.len = 1;
14394 p += it.len;
14396 /* Get its face. */
14397 ilisp = make_number (p - arrow_string);
14398 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14399 it.face_id = compute_char_face (f, it.c, face);
14401 /* Compute its width, get its glyphs. */
14402 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14403 SET_TEXT_POS (it.position, -1, -1);
14404 PRODUCE_GLYPHS (&it);
14406 /* If this character doesn't fit any more in the line, we have
14407 to remove some glyphs. */
14408 if (it.current_x > it.last_visible_x)
14410 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14411 break;
14415 set_buffer_temp (old);
14416 return it.glyph_row;
14420 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14421 glyphs are only inserted for terminal frames since we can't really
14422 win with truncation glyphs when partially visible glyphs are
14423 involved. Which glyphs to insert is determined by
14424 produce_special_glyphs. */
14426 static void
14427 insert_left_trunc_glyphs (it)
14428 struct it *it;
14430 struct it truncate_it;
14431 struct glyph *from, *end, *to, *toend;
14433 xassert (!FRAME_WINDOW_P (it->f));
14435 /* Get the truncation glyphs. */
14436 truncate_it = *it;
14437 truncate_it.current_x = 0;
14438 truncate_it.face_id = DEFAULT_FACE_ID;
14439 truncate_it.glyph_row = &scratch_glyph_row;
14440 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14441 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14442 truncate_it.object = make_number (0);
14443 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14445 /* Overwrite glyphs from IT with truncation glyphs. */
14446 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14447 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14448 to = it->glyph_row->glyphs[TEXT_AREA];
14449 toend = to + it->glyph_row->used[TEXT_AREA];
14451 while (from < end)
14452 *to++ = *from++;
14454 /* There may be padding glyphs left over. Overwrite them too. */
14455 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14457 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14458 while (from < end)
14459 *to++ = *from++;
14462 if (to > toend)
14463 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14467 /* Compute the pixel height and width of IT->glyph_row.
14469 Most of the time, ascent and height of a display line will be equal
14470 to the max_ascent and max_height values of the display iterator
14471 structure. This is not the case if
14473 1. We hit ZV without displaying anything. In this case, max_ascent
14474 and max_height will be zero.
14476 2. We have some glyphs that don't contribute to the line height.
14477 (The glyph row flag contributes_to_line_height_p is for future
14478 pixmap extensions).
14480 The first case is easily covered by using default values because in
14481 these cases, the line height does not really matter, except that it
14482 must not be zero. */
14484 static void
14485 compute_line_metrics (it)
14486 struct it *it;
14488 struct glyph_row *row = it->glyph_row;
14489 int area, i;
14491 if (FRAME_WINDOW_P (it->f))
14493 int i, min_y, max_y;
14495 /* The line may consist of one space only, that was added to
14496 place the cursor on it. If so, the row's height hasn't been
14497 computed yet. */
14498 if (row->height == 0)
14500 if (it->max_ascent + it->max_descent == 0)
14501 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14502 row->ascent = it->max_ascent;
14503 row->height = it->max_ascent + it->max_descent;
14504 row->phys_ascent = it->max_phys_ascent;
14505 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14506 row->extra_line_spacing = it->max_extra_line_spacing;
14509 /* Compute the width of this line. */
14510 row->pixel_width = row->x;
14511 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14512 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14514 xassert (row->pixel_width >= 0);
14515 xassert (row->ascent >= 0 && row->height > 0);
14517 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14518 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14520 /* If first line's physical ascent is larger than its logical
14521 ascent, use the physical ascent, and make the row taller.
14522 This makes accented characters fully visible. */
14523 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14524 && row->phys_ascent > row->ascent)
14526 row->height += row->phys_ascent - row->ascent;
14527 row->ascent = row->phys_ascent;
14530 /* Compute how much of the line is visible. */
14531 row->visible_height = row->height;
14533 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14534 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14536 if (row->y < min_y)
14537 row->visible_height -= min_y - row->y;
14538 if (row->y + row->height > max_y)
14539 row->visible_height -= row->y + row->height - max_y;
14541 else
14543 row->pixel_width = row->used[TEXT_AREA];
14544 if (row->continued_p)
14545 row->pixel_width -= it->continuation_pixel_width;
14546 else if (row->truncated_on_right_p)
14547 row->pixel_width -= it->truncation_pixel_width;
14548 row->ascent = row->phys_ascent = 0;
14549 row->height = row->phys_height = row->visible_height = 1;
14550 row->extra_line_spacing = 0;
14553 /* Compute a hash code for this row. */
14554 row->hash = 0;
14555 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14556 for (i = 0; i < row->used[area]; ++i)
14557 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14558 + row->glyphs[area][i].u.val
14559 + row->glyphs[area][i].face_id
14560 + row->glyphs[area][i].padding_p
14561 + (row->glyphs[area][i].type << 2));
14563 it->max_ascent = it->max_descent = 0;
14564 it->max_phys_ascent = it->max_phys_descent = 0;
14568 /* Append one space to the glyph row of iterator IT if doing a
14569 window-based redisplay. The space has the same face as
14570 IT->face_id. Value is non-zero if a space was added.
14572 This function is called to make sure that there is always one glyph
14573 at the end of a glyph row that the cursor can be set on under
14574 window-systems. (If there weren't such a glyph we would not know
14575 how wide and tall a box cursor should be displayed).
14577 At the same time this space let's a nicely handle clearing to the
14578 end of the line if the row ends in italic text. */
14580 static int
14581 append_space_for_newline (it, default_face_p)
14582 struct it *it;
14583 int default_face_p;
14585 if (FRAME_WINDOW_P (it->f))
14587 int n = it->glyph_row->used[TEXT_AREA];
14589 if (it->glyph_row->glyphs[TEXT_AREA] + n
14590 < it->glyph_row->glyphs[1 + TEXT_AREA])
14592 /* Save some values that must not be changed.
14593 Must save IT->c and IT->len because otherwise
14594 ITERATOR_AT_END_P wouldn't work anymore after
14595 append_space_for_newline has been called. */
14596 enum display_element_type saved_what = it->what;
14597 int saved_c = it->c, saved_len = it->len;
14598 int saved_x = it->current_x;
14599 int saved_face_id = it->face_id;
14600 struct text_pos saved_pos;
14601 Lisp_Object saved_object;
14602 struct face *face;
14604 saved_object = it->object;
14605 saved_pos = it->position;
14607 it->what = IT_CHARACTER;
14608 bzero (&it->position, sizeof it->position);
14609 it->object = make_number (0);
14610 it->c = ' ';
14611 it->len = 1;
14613 if (default_face_p)
14614 it->face_id = DEFAULT_FACE_ID;
14615 else if (it->face_before_selective_p)
14616 it->face_id = it->saved_face_id;
14617 face = FACE_FROM_ID (it->f, it->face_id);
14618 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14620 PRODUCE_GLYPHS (it);
14622 it->override_ascent = -1;
14623 it->constrain_row_ascent_descent_p = 0;
14624 it->current_x = saved_x;
14625 it->object = saved_object;
14626 it->position = saved_pos;
14627 it->what = saved_what;
14628 it->face_id = saved_face_id;
14629 it->len = saved_len;
14630 it->c = saved_c;
14631 return 1;
14635 return 0;
14639 /* Extend the face of the last glyph in the text area of IT->glyph_row
14640 to the end of the display line. Called from display_line.
14641 If the glyph row is empty, add a space glyph to it so that we
14642 know the face to draw. Set the glyph row flag fill_line_p. */
14644 static void
14645 extend_face_to_end_of_line (it)
14646 struct it *it;
14648 struct face *face;
14649 struct frame *f = it->f;
14651 /* If line is already filled, do nothing. */
14652 if (it->current_x >= it->last_visible_x)
14653 return;
14655 /* Face extension extends the background and box of IT->face_id
14656 to the end of the line. If the background equals the background
14657 of the frame, we don't have to do anything. */
14658 if (it->face_before_selective_p)
14659 face = FACE_FROM_ID (it->f, it->saved_face_id);
14660 else
14661 face = FACE_FROM_ID (f, it->face_id);
14663 if (FRAME_WINDOW_P (f)
14664 && face->box == FACE_NO_BOX
14665 && face->background == FRAME_BACKGROUND_PIXEL (f)
14666 && !face->stipple)
14667 return;
14669 /* Set the glyph row flag indicating that the face of the last glyph
14670 in the text area has to be drawn to the end of the text area. */
14671 it->glyph_row->fill_line_p = 1;
14673 /* If current character of IT is not ASCII, make sure we have the
14674 ASCII face. This will be automatically undone the next time
14675 get_next_display_element returns a multibyte character. Note
14676 that the character will always be single byte in unibyte text. */
14677 if (!SINGLE_BYTE_CHAR_P (it->c))
14679 it->face_id = FACE_FOR_CHAR (f, face, 0);
14682 if (FRAME_WINDOW_P (f))
14684 /* If the row is empty, add a space with the current face of IT,
14685 so that we know which face to draw. */
14686 if (it->glyph_row->used[TEXT_AREA] == 0)
14688 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14689 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14690 it->glyph_row->used[TEXT_AREA] = 1;
14693 else
14695 /* Save some values that must not be changed. */
14696 int saved_x = it->current_x;
14697 struct text_pos saved_pos;
14698 Lisp_Object saved_object;
14699 enum display_element_type saved_what = it->what;
14700 int saved_face_id = it->face_id;
14702 saved_object = it->object;
14703 saved_pos = it->position;
14705 it->what = IT_CHARACTER;
14706 bzero (&it->position, sizeof it->position);
14707 it->object = make_number (0);
14708 it->c = ' ';
14709 it->len = 1;
14710 it->face_id = face->id;
14712 PRODUCE_GLYPHS (it);
14714 while (it->current_x <= it->last_visible_x)
14715 PRODUCE_GLYPHS (it);
14717 /* Don't count these blanks really. It would let us insert a left
14718 truncation glyph below and make us set the cursor on them, maybe. */
14719 it->current_x = saved_x;
14720 it->object = saved_object;
14721 it->position = saved_pos;
14722 it->what = saved_what;
14723 it->face_id = saved_face_id;
14728 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14729 trailing whitespace. */
14731 static int
14732 trailing_whitespace_p (charpos)
14733 int charpos;
14735 int bytepos = CHAR_TO_BYTE (charpos);
14736 int c = 0;
14738 while (bytepos < ZV_BYTE
14739 && (c = FETCH_CHAR (bytepos),
14740 c == ' ' || c == '\t'))
14741 ++bytepos;
14743 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14745 if (bytepos != PT_BYTE)
14746 return 1;
14748 return 0;
14752 /* Highlight trailing whitespace, if any, in ROW. */
14754 void
14755 highlight_trailing_whitespace (f, row)
14756 struct frame *f;
14757 struct glyph_row *row;
14759 int used = row->used[TEXT_AREA];
14761 if (used)
14763 struct glyph *start = row->glyphs[TEXT_AREA];
14764 struct glyph *glyph = start + used - 1;
14766 /* Skip over glyphs inserted to display the cursor at the
14767 end of a line, for extending the face of the last glyph
14768 to the end of the line on terminals, and for truncation
14769 and continuation glyphs. */
14770 while (glyph >= start
14771 && glyph->type == CHAR_GLYPH
14772 && INTEGERP (glyph->object))
14773 --glyph;
14775 /* If last glyph is a space or stretch, and it's trailing
14776 whitespace, set the face of all trailing whitespace glyphs in
14777 IT->glyph_row to `trailing-whitespace'. */
14778 if (glyph >= start
14779 && BUFFERP (glyph->object)
14780 && (glyph->type == STRETCH_GLYPH
14781 || (glyph->type == CHAR_GLYPH
14782 && glyph->u.ch == ' '))
14783 && trailing_whitespace_p (glyph->charpos))
14785 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14786 if (face_id < 0)
14787 return;
14789 while (glyph >= start
14790 && BUFFERP (glyph->object)
14791 && (glyph->type == STRETCH_GLYPH
14792 || (glyph->type == CHAR_GLYPH
14793 && glyph->u.ch == ' ')))
14794 (glyph--)->face_id = face_id;
14800 /* Value is non-zero if glyph row ROW in window W should be
14801 used to hold the cursor. */
14803 static int
14804 cursor_row_p (w, row)
14805 struct window *w;
14806 struct glyph_row *row;
14808 int cursor_row_p = 1;
14810 if (PT == MATRIX_ROW_END_CHARPOS (row))
14812 /* If the row ends with a newline from a string, we don't want
14813 the cursor there (if the row is continued it doesn't end in a
14814 newline). */
14815 if (CHARPOS (row->end.string_pos) >= 0)
14816 cursor_row_p = row->continued_p;
14817 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14819 /* If the row ends in middle of a real character,
14820 and the line is continued, we want the cursor here.
14821 That's because MATRIX_ROW_END_CHARPOS would equal
14822 PT if PT is before the character. */
14823 if (!row->ends_in_ellipsis_p)
14824 cursor_row_p = row->continued_p;
14825 else
14826 /* If the row ends in an ellipsis, then
14827 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14828 We want that position to be displayed after the ellipsis. */
14829 cursor_row_p = 0;
14831 /* If the row ends at ZV, display the cursor at the end of that
14832 row instead of at the start of the row below. */
14833 else if (row->ends_at_zv_p)
14834 cursor_row_p = 1;
14835 else
14836 cursor_row_p = 0;
14839 return cursor_row_p;
14843 /* Construct the glyph row IT->glyph_row in the desired matrix of
14844 IT->w from text at the current position of IT. See dispextern.h
14845 for an overview of struct it. Value is non-zero if
14846 IT->glyph_row displays text, as opposed to a line displaying ZV
14847 only. */
14849 static int
14850 display_line (it)
14851 struct it *it;
14853 struct glyph_row *row = it->glyph_row;
14854 Lisp_Object overlay_arrow_string;
14856 /* We always start displaying at hpos zero even if hscrolled. */
14857 xassert (it->hpos == 0 && it->current_x == 0);
14859 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14860 >= it->w->desired_matrix->nrows)
14862 it->w->nrows_scale_factor++;
14863 fonts_changed_p = 1;
14864 return 0;
14867 /* Is IT->w showing the region? */
14868 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14870 /* Clear the result glyph row and enable it. */
14871 prepare_desired_row (row);
14873 row->y = it->current_y;
14874 row->start = it->start;
14875 row->continuation_lines_width = it->continuation_lines_width;
14876 row->displays_text_p = 1;
14877 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14878 it->starts_in_middle_of_char_p = 0;
14880 /* Arrange the overlays nicely for our purposes. Usually, we call
14881 display_line on only one line at a time, in which case this
14882 can't really hurt too much, or we call it on lines which appear
14883 one after another in the buffer, in which case all calls to
14884 recenter_overlay_lists but the first will be pretty cheap. */
14885 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14887 /* Move over display elements that are not visible because we are
14888 hscrolled. This may stop at an x-position < IT->first_visible_x
14889 if the first glyph is partially visible or if we hit a line end. */
14890 if (it->current_x < it->first_visible_x)
14892 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14893 MOVE_TO_POS | MOVE_TO_X);
14896 /* Get the initial row height. This is either the height of the
14897 text hscrolled, if there is any, or zero. */
14898 row->ascent = it->max_ascent;
14899 row->height = it->max_ascent + it->max_descent;
14900 row->phys_ascent = it->max_phys_ascent;
14901 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14902 row->extra_line_spacing = it->max_extra_line_spacing;
14904 /* Loop generating characters. The loop is left with IT on the next
14905 character to display. */
14906 while (1)
14908 int n_glyphs_before, hpos_before, x_before;
14909 int x, i, nglyphs;
14910 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14912 /* Retrieve the next thing to display. Value is zero if end of
14913 buffer reached. */
14914 if (!get_next_display_element (it))
14916 /* Maybe add a space at the end of this line that is used to
14917 display the cursor there under X. Set the charpos of the
14918 first glyph of blank lines not corresponding to any text
14919 to -1. */
14920 #ifdef HAVE_WINDOW_SYSTEM
14921 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14922 row->exact_window_width_line_p = 1;
14923 else
14924 #endif /* HAVE_WINDOW_SYSTEM */
14925 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14926 || row->used[TEXT_AREA] == 0)
14928 row->glyphs[TEXT_AREA]->charpos = -1;
14929 row->displays_text_p = 0;
14931 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14932 && (!MINI_WINDOW_P (it->w)
14933 || (minibuf_level && EQ (it->window, minibuf_window))))
14934 row->indicate_empty_line_p = 1;
14937 it->continuation_lines_width = 0;
14938 row->ends_at_zv_p = 1;
14939 break;
14942 /* Now, get the metrics of what we want to display. This also
14943 generates glyphs in `row' (which is IT->glyph_row). */
14944 n_glyphs_before = row->used[TEXT_AREA];
14945 x = it->current_x;
14947 /* Remember the line height so far in case the next element doesn't
14948 fit on the line. */
14949 if (!it->truncate_lines_p)
14951 ascent = it->max_ascent;
14952 descent = it->max_descent;
14953 phys_ascent = it->max_phys_ascent;
14954 phys_descent = it->max_phys_descent;
14957 PRODUCE_GLYPHS (it);
14959 /* If this display element was in marginal areas, continue with
14960 the next one. */
14961 if (it->area != TEXT_AREA)
14963 row->ascent = max (row->ascent, it->max_ascent);
14964 row->height = max (row->height, it->max_ascent + it->max_descent);
14965 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14966 row->phys_height = max (row->phys_height,
14967 it->max_phys_ascent + it->max_phys_descent);
14968 row->extra_line_spacing = max (row->extra_line_spacing,
14969 it->max_extra_line_spacing);
14970 set_iterator_to_next (it, 1);
14971 continue;
14974 /* Does the display element fit on the line? If we truncate
14975 lines, we should draw past the right edge of the window. If
14976 we don't truncate, we want to stop so that we can display the
14977 continuation glyph before the right margin. If lines are
14978 continued, there are two possible strategies for characters
14979 resulting in more than 1 glyph (e.g. tabs): Display as many
14980 glyphs as possible in this line and leave the rest for the
14981 continuation line, or display the whole element in the next
14982 line. Original redisplay did the former, so we do it also. */
14983 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14984 hpos_before = it->hpos;
14985 x_before = x;
14987 if (/* Not a newline. */
14988 nglyphs > 0
14989 /* Glyphs produced fit entirely in the line. */
14990 && it->current_x < it->last_visible_x)
14992 it->hpos += nglyphs;
14993 row->ascent = max (row->ascent, it->max_ascent);
14994 row->height = max (row->height, it->max_ascent + it->max_descent);
14995 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14996 row->phys_height = max (row->phys_height,
14997 it->max_phys_ascent + it->max_phys_descent);
14998 row->extra_line_spacing = max (row->extra_line_spacing,
14999 it->max_extra_line_spacing);
15000 if (it->current_x - it->pixel_width < it->first_visible_x)
15001 row->x = x - it->first_visible_x;
15003 else
15005 int new_x;
15006 struct glyph *glyph;
15008 for (i = 0; i < nglyphs; ++i, x = new_x)
15010 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15011 new_x = x + glyph->pixel_width;
15013 if (/* Lines are continued. */
15014 !it->truncate_lines_p
15015 && (/* Glyph doesn't fit on the line. */
15016 new_x > it->last_visible_x
15017 /* Or it fits exactly on a window system frame. */
15018 || (new_x == it->last_visible_x
15019 && FRAME_WINDOW_P (it->f))))
15021 /* End of a continued line. */
15023 if (it->hpos == 0
15024 || (new_x == it->last_visible_x
15025 && FRAME_WINDOW_P (it->f)))
15027 /* Current glyph is the only one on the line or
15028 fits exactly on the line. We must continue
15029 the line because we can't draw the cursor
15030 after the glyph. */
15031 row->continued_p = 1;
15032 it->current_x = new_x;
15033 it->continuation_lines_width += new_x;
15034 ++it->hpos;
15035 if (i == nglyphs - 1)
15037 set_iterator_to_next (it, 1);
15038 #ifdef HAVE_WINDOW_SYSTEM
15039 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15041 if (!get_next_display_element (it))
15043 row->exact_window_width_line_p = 1;
15044 it->continuation_lines_width = 0;
15045 row->continued_p = 0;
15046 row->ends_at_zv_p = 1;
15048 else if (ITERATOR_AT_END_OF_LINE_P (it))
15050 row->continued_p = 0;
15051 row->exact_window_width_line_p = 1;
15054 #endif /* HAVE_WINDOW_SYSTEM */
15057 else if (CHAR_GLYPH_PADDING_P (*glyph)
15058 && !FRAME_WINDOW_P (it->f))
15060 /* A padding glyph that doesn't fit on this line.
15061 This means the whole character doesn't fit
15062 on the line. */
15063 row->used[TEXT_AREA] = n_glyphs_before;
15065 /* Fill the rest of the row with continuation
15066 glyphs like in 20.x. */
15067 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15068 < row->glyphs[1 + TEXT_AREA])
15069 produce_special_glyphs (it, IT_CONTINUATION);
15071 row->continued_p = 1;
15072 it->current_x = x_before;
15073 it->continuation_lines_width += x_before;
15075 /* Restore the height to what it was before the
15076 element not fitting on the line. */
15077 it->max_ascent = ascent;
15078 it->max_descent = descent;
15079 it->max_phys_ascent = phys_ascent;
15080 it->max_phys_descent = phys_descent;
15082 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15084 /* A TAB that extends past the right edge of the
15085 window. This produces a single glyph on
15086 window system frames. We leave the glyph in
15087 this row and let it fill the row, but don't
15088 consume the TAB. */
15089 it->continuation_lines_width += it->last_visible_x;
15090 row->ends_in_middle_of_char_p = 1;
15091 row->continued_p = 1;
15092 glyph->pixel_width = it->last_visible_x - x;
15093 it->starts_in_middle_of_char_p = 1;
15095 else
15097 /* Something other than a TAB that draws past
15098 the right edge of the window. Restore
15099 positions to values before the element. */
15100 row->used[TEXT_AREA] = n_glyphs_before + i;
15102 /* Display continuation glyphs. */
15103 if (!FRAME_WINDOW_P (it->f))
15104 produce_special_glyphs (it, IT_CONTINUATION);
15105 row->continued_p = 1;
15107 it->continuation_lines_width += x;
15109 if (nglyphs > 1 && i > 0)
15111 row->ends_in_middle_of_char_p = 1;
15112 it->starts_in_middle_of_char_p = 1;
15115 /* Restore the height to what it was before the
15116 element not fitting on the line. */
15117 it->max_ascent = ascent;
15118 it->max_descent = descent;
15119 it->max_phys_ascent = phys_ascent;
15120 it->max_phys_descent = phys_descent;
15123 break;
15125 else if (new_x > it->first_visible_x)
15127 /* Increment number of glyphs actually displayed. */
15128 ++it->hpos;
15130 if (x < it->first_visible_x)
15131 /* Glyph is partially visible, i.e. row starts at
15132 negative X position. */
15133 row->x = x - it->first_visible_x;
15135 else
15137 /* Glyph is completely off the left margin of the
15138 window. This should not happen because of the
15139 move_it_in_display_line at the start of this
15140 function, unless the text display area of the
15141 window is empty. */
15142 xassert (it->first_visible_x <= it->last_visible_x);
15146 row->ascent = max (row->ascent, it->max_ascent);
15147 row->height = max (row->height, it->max_ascent + it->max_descent);
15148 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15149 row->phys_height = max (row->phys_height,
15150 it->max_phys_ascent + it->max_phys_descent);
15151 row->extra_line_spacing = max (row->extra_line_spacing,
15152 it->max_extra_line_spacing);
15154 /* End of this display line if row is continued. */
15155 if (row->continued_p || row->ends_at_zv_p)
15156 break;
15159 at_end_of_line:
15160 /* Is this a line end? If yes, we're also done, after making
15161 sure that a non-default face is extended up to the right
15162 margin of the window. */
15163 if (ITERATOR_AT_END_OF_LINE_P (it))
15165 int used_before = row->used[TEXT_AREA];
15167 row->ends_in_newline_from_string_p = STRINGP (it->object);
15169 #ifdef HAVE_WINDOW_SYSTEM
15170 /* Add a space at the end of the line that is used to
15171 display the cursor there. */
15172 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15173 append_space_for_newline (it, 0);
15174 #endif /* HAVE_WINDOW_SYSTEM */
15176 /* Extend the face to the end of the line. */
15177 extend_face_to_end_of_line (it);
15179 /* Make sure we have the position. */
15180 if (used_before == 0)
15181 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15183 /* Consume the line end. This skips over invisible lines. */
15184 set_iterator_to_next (it, 1);
15185 it->continuation_lines_width = 0;
15186 break;
15189 /* Proceed with next display element. Note that this skips
15190 over lines invisible because of selective display. */
15191 set_iterator_to_next (it, 1);
15193 /* If we truncate lines, we are done when the last displayed
15194 glyphs reach past the right margin of the window. */
15195 if (it->truncate_lines_p
15196 && (FRAME_WINDOW_P (it->f)
15197 ? (it->current_x >= it->last_visible_x)
15198 : (it->current_x > it->last_visible_x)))
15200 /* Maybe add truncation glyphs. */
15201 if (!FRAME_WINDOW_P (it->f))
15203 int i, n;
15205 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15206 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15207 break;
15209 for (n = row->used[TEXT_AREA]; i < n; ++i)
15211 row->used[TEXT_AREA] = i;
15212 produce_special_glyphs (it, IT_TRUNCATION);
15215 #ifdef HAVE_WINDOW_SYSTEM
15216 else
15218 /* Don't truncate if we can overflow newline into fringe. */
15219 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15221 if (!get_next_display_element (it))
15223 it->continuation_lines_width = 0;
15224 row->ends_at_zv_p = 1;
15225 row->exact_window_width_line_p = 1;
15226 break;
15228 if (ITERATOR_AT_END_OF_LINE_P (it))
15230 row->exact_window_width_line_p = 1;
15231 goto at_end_of_line;
15235 #endif /* HAVE_WINDOW_SYSTEM */
15237 row->truncated_on_right_p = 1;
15238 it->continuation_lines_width = 0;
15239 reseat_at_next_visible_line_start (it, 0);
15240 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15241 it->hpos = hpos_before;
15242 it->current_x = x_before;
15243 break;
15247 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15248 at the left window margin. */
15249 if (it->first_visible_x
15250 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15252 if (!FRAME_WINDOW_P (it->f))
15253 insert_left_trunc_glyphs (it);
15254 row->truncated_on_left_p = 1;
15257 /* If the start of this line is the overlay arrow-position, then
15258 mark this glyph row as the one containing the overlay arrow.
15259 This is clearly a mess with variable size fonts. It would be
15260 better to let it be displayed like cursors under X. */
15261 if ((row->displays_text_p || !overlay_arrow_seen)
15262 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15263 !NILP (overlay_arrow_string)))
15265 /* Overlay arrow in window redisplay is a fringe bitmap. */
15266 if (STRINGP (overlay_arrow_string))
15268 struct glyph_row *arrow_row
15269 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15270 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15271 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15272 struct glyph *p = row->glyphs[TEXT_AREA];
15273 struct glyph *p2, *end;
15275 /* Copy the arrow glyphs. */
15276 while (glyph < arrow_end)
15277 *p++ = *glyph++;
15279 /* Throw away padding glyphs. */
15280 p2 = p;
15281 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15282 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15283 ++p2;
15284 if (p2 > p)
15286 while (p2 < end)
15287 *p++ = *p2++;
15288 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15291 else
15293 xassert (INTEGERP (overlay_arrow_string));
15294 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15296 overlay_arrow_seen = 1;
15299 /* Compute pixel dimensions of this line. */
15300 compute_line_metrics (it);
15302 /* Remember the position at which this line ends. */
15303 row->end = it->current;
15305 /* Record whether this row ends inside an ellipsis. */
15306 row->ends_in_ellipsis_p
15307 = (it->method == GET_FROM_DISPLAY_VECTOR
15308 && it->ellipsis_p);
15310 /* Save fringe bitmaps in this row. */
15311 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15312 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15313 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15314 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15316 it->left_user_fringe_bitmap = 0;
15317 it->left_user_fringe_face_id = 0;
15318 it->right_user_fringe_bitmap = 0;
15319 it->right_user_fringe_face_id = 0;
15321 /* Maybe set the cursor. */
15322 if (it->w->cursor.vpos < 0
15323 && PT >= MATRIX_ROW_START_CHARPOS (row)
15324 && PT <= MATRIX_ROW_END_CHARPOS (row)
15325 && cursor_row_p (it->w, row))
15326 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15328 /* Highlight trailing whitespace. */
15329 if (!NILP (Vshow_trailing_whitespace))
15330 highlight_trailing_whitespace (it->f, it->glyph_row);
15332 /* Prepare for the next line. This line starts horizontally at (X
15333 HPOS) = (0 0). Vertical positions are incremented. As a
15334 convenience for the caller, IT->glyph_row is set to the next
15335 row to be used. */
15336 it->current_x = it->hpos = 0;
15337 it->current_y += row->height;
15338 ++it->vpos;
15339 ++it->glyph_row;
15340 it->start = it->current;
15341 return row->displays_text_p;
15346 /***********************************************************************
15347 Menu Bar
15348 ***********************************************************************/
15350 /* Redisplay the menu bar in the frame for window W.
15352 The menu bar of X frames that don't have X toolkit support is
15353 displayed in a special window W->frame->menu_bar_window.
15355 The menu bar of terminal frames is treated specially as far as
15356 glyph matrices are concerned. Menu bar lines are not part of
15357 windows, so the update is done directly on the frame matrix rows
15358 for the menu bar. */
15360 static void
15361 display_menu_bar (w)
15362 struct window *w;
15364 struct frame *f = XFRAME (WINDOW_FRAME (w));
15365 struct it it;
15366 Lisp_Object items;
15367 int i;
15369 /* Don't do all this for graphical frames. */
15370 #ifdef HAVE_NTGUI
15371 if (!NILP (Vwindow_system))
15372 return;
15373 #endif
15374 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15375 if (FRAME_X_P (f))
15376 return;
15377 #endif
15378 #ifdef MAC_OS
15379 if (FRAME_MAC_P (f))
15380 return;
15381 #endif
15383 #ifdef USE_X_TOOLKIT
15384 xassert (!FRAME_WINDOW_P (f));
15385 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15386 it.first_visible_x = 0;
15387 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15388 #else /* not USE_X_TOOLKIT */
15389 if (FRAME_WINDOW_P (f))
15391 /* Menu bar lines are displayed in the desired matrix of the
15392 dummy window menu_bar_window. */
15393 struct window *menu_w;
15394 xassert (WINDOWP (f->menu_bar_window));
15395 menu_w = XWINDOW (f->menu_bar_window);
15396 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15397 MENU_FACE_ID);
15398 it.first_visible_x = 0;
15399 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15401 else
15403 /* This is a TTY frame, i.e. character hpos/vpos are used as
15404 pixel x/y. */
15405 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15406 MENU_FACE_ID);
15407 it.first_visible_x = 0;
15408 it.last_visible_x = FRAME_COLS (f);
15410 #endif /* not USE_X_TOOLKIT */
15412 if (! mode_line_inverse_video)
15413 /* Force the menu-bar to be displayed in the default face. */
15414 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15416 /* Clear all rows of the menu bar. */
15417 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15419 struct glyph_row *row = it.glyph_row + i;
15420 clear_glyph_row (row);
15421 row->enabled_p = 1;
15422 row->full_width_p = 1;
15425 /* Display all items of the menu bar. */
15426 items = FRAME_MENU_BAR_ITEMS (it.f);
15427 for (i = 0; i < XVECTOR (items)->size; i += 4)
15429 Lisp_Object string;
15431 /* Stop at nil string. */
15432 string = AREF (items, i + 1);
15433 if (NILP (string))
15434 break;
15436 /* Remember where item was displayed. */
15437 AREF (items, i + 3) = make_number (it.hpos);
15439 /* Display the item, pad with one space. */
15440 if (it.current_x < it.last_visible_x)
15441 display_string (NULL, string, Qnil, 0, 0, &it,
15442 SCHARS (string) + 1, 0, 0, -1);
15445 /* Fill out the line with spaces. */
15446 if (it.current_x < it.last_visible_x)
15447 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15449 /* Compute the total height of the lines. */
15450 compute_line_metrics (&it);
15455 /***********************************************************************
15456 Mode Line
15457 ***********************************************************************/
15459 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15460 FORCE is non-zero, redisplay mode lines unconditionally.
15461 Otherwise, redisplay only mode lines that are garbaged. Value is
15462 the number of windows whose mode lines were redisplayed. */
15464 static int
15465 redisplay_mode_lines (window, force)
15466 Lisp_Object window;
15467 int force;
15469 int nwindows = 0;
15471 while (!NILP (window))
15473 struct window *w = XWINDOW (window);
15475 if (WINDOWP (w->hchild))
15476 nwindows += redisplay_mode_lines (w->hchild, force);
15477 else if (WINDOWP (w->vchild))
15478 nwindows += redisplay_mode_lines (w->vchild, force);
15479 else if (force
15480 || FRAME_GARBAGED_P (XFRAME (w->frame))
15481 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15483 struct text_pos lpoint;
15484 struct buffer *old = current_buffer;
15486 /* Set the window's buffer for the mode line display. */
15487 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15488 set_buffer_internal_1 (XBUFFER (w->buffer));
15490 /* Point refers normally to the selected window. For any
15491 other window, set up appropriate value. */
15492 if (!EQ (window, selected_window))
15494 struct text_pos pt;
15496 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15497 if (CHARPOS (pt) < BEGV)
15498 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15499 else if (CHARPOS (pt) > (ZV - 1))
15500 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15501 else
15502 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15505 /* Display mode lines. */
15506 clear_glyph_matrix (w->desired_matrix);
15507 if (display_mode_lines (w))
15509 ++nwindows;
15510 w->must_be_updated_p = 1;
15513 /* Restore old settings. */
15514 set_buffer_internal_1 (old);
15515 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15518 window = w->next;
15521 return nwindows;
15525 /* Display the mode and/or top line of window W. Value is the number
15526 of mode lines displayed. */
15528 static int
15529 display_mode_lines (w)
15530 struct window *w;
15532 Lisp_Object old_selected_window, old_selected_frame;
15533 int n = 0;
15535 old_selected_frame = selected_frame;
15536 selected_frame = w->frame;
15537 old_selected_window = selected_window;
15538 XSETWINDOW (selected_window, w);
15540 /* These will be set while the mode line specs are processed. */
15541 line_number_displayed = 0;
15542 w->column_number_displayed = Qnil;
15544 if (WINDOW_WANTS_MODELINE_P (w))
15546 struct window *sel_w = XWINDOW (old_selected_window);
15548 /* Select mode line face based on the real selected window. */
15549 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15550 current_buffer->mode_line_format);
15551 ++n;
15554 if (WINDOW_WANTS_HEADER_LINE_P (w))
15556 display_mode_line (w, HEADER_LINE_FACE_ID,
15557 current_buffer->header_line_format);
15558 ++n;
15561 selected_frame = old_selected_frame;
15562 selected_window = old_selected_window;
15563 return n;
15567 /* Display mode or top line of window W. FACE_ID specifies which line
15568 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15569 FORMAT is the mode line format to display. Value is the pixel
15570 height of the mode line displayed. */
15572 static int
15573 display_mode_line (w, face_id, format)
15574 struct window *w;
15575 enum face_id face_id;
15576 Lisp_Object format;
15578 struct it it;
15579 struct face *face;
15581 init_iterator (&it, w, -1, -1, NULL, face_id);
15582 prepare_desired_row (it.glyph_row);
15584 it.glyph_row->mode_line_p = 1;
15586 if (! mode_line_inverse_video)
15587 /* Force the mode-line to be displayed in the default face. */
15588 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15590 /* Temporarily make frame's keyboard the current kboard so that
15591 kboard-local variables in the mode_line_format will get the right
15592 values. */
15593 push_frame_kboard (it.f);
15594 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15595 pop_frame_kboard ();
15597 /* Fill up with spaces. */
15598 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15600 compute_line_metrics (&it);
15601 it.glyph_row->full_width_p = 1;
15602 it.glyph_row->continued_p = 0;
15603 it.glyph_row->truncated_on_left_p = 0;
15604 it.glyph_row->truncated_on_right_p = 0;
15606 /* Make a 3D mode-line have a shadow at its right end. */
15607 face = FACE_FROM_ID (it.f, face_id);
15608 extend_face_to_end_of_line (&it);
15609 if (face->box != FACE_NO_BOX)
15611 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15612 + it.glyph_row->used[TEXT_AREA] - 1);
15613 last->right_box_line_p = 1;
15616 return it.glyph_row->height;
15619 /* Alist that caches the results of :propertize.
15620 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15621 Lisp_Object mode_line_proptrans_alist;
15623 /* List of strings making up the mode-line. */
15624 Lisp_Object mode_line_string_list;
15626 /* Base face property when building propertized mode line string. */
15627 static Lisp_Object mode_line_string_face;
15628 static Lisp_Object mode_line_string_face_prop;
15631 /* Contribute ELT to the mode line for window IT->w. How it
15632 translates into text depends on its data type.
15634 IT describes the display environment in which we display, as usual.
15636 DEPTH is the depth in recursion. It is used to prevent
15637 infinite recursion here.
15639 FIELD_WIDTH is the number of characters the display of ELT should
15640 occupy in the mode line, and PRECISION is the maximum number of
15641 characters to display from ELT's representation. See
15642 display_string for details.
15644 Returns the hpos of the end of the text generated by ELT.
15646 PROPS is a property list to add to any string we encounter.
15648 If RISKY is nonzero, remove (disregard) any properties in any string
15649 we encounter, and ignore :eval and :propertize.
15651 If the global variable `frame_title_ptr' is non-NULL, then the output
15652 is passed to `store_frame_title' instead of `display_string'. */
15654 static int
15655 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15656 struct it *it;
15657 int depth;
15658 int field_width, precision;
15659 Lisp_Object elt, props;
15660 int risky;
15662 int n = 0, field, prec;
15663 int literal = 0;
15665 tail_recurse:
15666 if (depth > 100)
15667 elt = build_string ("*too-deep*");
15669 depth++;
15671 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15673 case Lisp_String:
15675 /* A string: output it and check for %-constructs within it. */
15676 unsigned char c;
15677 const unsigned char *this, *lisp_string;
15679 if (!NILP (props) || risky)
15681 Lisp_Object oprops, aelt;
15682 oprops = Ftext_properties_at (make_number (0), elt);
15684 /* If the starting string's properties are not what
15685 we want, translate the string. Also, if the string
15686 is risky, do that anyway. */
15688 if (NILP (Fequal (props, oprops)) || risky)
15690 /* If the starting string has properties,
15691 merge the specified ones onto the existing ones. */
15692 if (! NILP (oprops) && !risky)
15694 Lisp_Object tem;
15696 oprops = Fcopy_sequence (oprops);
15697 tem = props;
15698 while (CONSP (tem))
15700 oprops = Fplist_put (oprops, XCAR (tem),
15701 XCAR (XCDR (tem)));
15702 tem = XCDR (XCDR (tem));
15704 props = oprops;
15707 aelt = Fassoc (elt, mode_line_proptrans_alist);
15708 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15710 mode_line_proptrans_alist
15711 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15712 elt = XCAR (aelt);
15714 else
15716 Lisp_Object tem;
15718 elt = Fcopy_sequence (elt);
15719 Fset_text_properties (make_number (0), Flength (elt),
15720 props, elt);
15721 /* Add this item to mode_line_proptrans_alist. */
15722 mode_line_proptrans_alist
15723 = Fcons (Fcons (elt, props),
15724 mode_line_proptrans_alist);
15725 /* Truncate mode_line_proptrans_alist
15726 to at most 50 elements. */
15727 tem = Fnthcdr (make_number (50),
15728 mode_line_proptrans_alist);
15729 if (! NILP (tem))
15730 XSETCDR (tem, Qnil);
15735 this = SDATA (elt);
15736 lisp_string = this;
15738 if (literal)
15740 prec = precision - n;
15741 if (frame_title_ptr)
15742 n += store_frame_title (SDATA (elt), -1, prec);
15743 else if (!NILP (mode_line_string_list))
15744 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15745 else
15746 n += display_string (NULL, elt, Qnil, 0, 0, it,
15747 0, prec, 0, STRING_MULTIBYTE (elt));
15749 break;
15752 while ((precision <= 0 || n < precision)
15753 && *this
15754 && (frame_title_ptr
15755 || !NILP (mode_line_string_list)
15756 || it->current_x < it->last_visible_x))
15758 const unsigned char *last = this;
15760 /* Advance to end of string or next format specifier. */
15761 while ((c = *this++) != '\0' && c != '%')
15764 if (this - 1 != last)
15766 int nchars, nbytes;
15768 /* Output to end of string or up to '%'. Field width
15769 is length of string. Don't output more than
15770 PRECISION allows us. */
15771 --this;
15773 prec = c_string_width (last, this - last, precision - n,
15774 &nchars, &nbytes);
15776 if (frame_title_ptr)
15777 n += store_frame_title (last, 0, prec);
15778 else if (!NILP (mode_line_string_list))
15780 int bytepos = last - lisp_string;
15781 int charpos = string_byte_to_char (elt, bytepos);
15782 int endpos = (precision <= 0
15783 ? string_byte_to_char (elt,
15784 this - lisp_string)
15785 : charpos + nchars);
15787 n += store_mode_line_string (NULL,
15788 Fsubstring (elt, make_number (charpos),
15789 make_number (endpos)),
15790 0, 0, 0, Qnil);
15792 else
15794 int bytepos = last - lisp_string;
15795 int charpos = string_byte_to_char (elt, bytepos);
15796 n += display_string (NULL, elt, Qnil, 0, charpos,
15797 it, 0, prec, 0,
15798 STRING_MULTIBYTE (elt));
15801 else /* c == '%' */
15803 const unsigned char *percent_position = this;
15805 /* Get the specified minimum width. Zero means
15806 don't pad. */
15807 field = 0;
15808 while ((c = *this++) >= '0' && c <= '9')
15809 field = field * 10 + c - '0';
15811 /* Don't pad beyond the total padding allowed. */
15812 if (field_width - n > 0 && field > field_width - n)
15813 field = field_width - n;
15815 /* Note that either PRECISION <= 0 or N < PRECISION. */
15816 prec = precision - n;
15818 if (c == 'M')
15819 n += display_mode_element (it, depth, field, prec,
15820 Vglobal_mode_string, props,
15821 risky);
15822 else if (c != 0)
15824 int multibyte;
15825 int bytepos, charpos;
15826 unsigned char *spec;
15828 bytepos = percent_position - lisp_string;
15829 charpos = (STRING_MULTIBYTE (elt)
15830 ? string_byte_to_char (elt, bytepos)
15831 : bytepos);
15833 spec
15834 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15836 if (frame_title_ptr)
15837 n += store_frame_title (spec, field, prec);
15838 else if (!NILP (mode_line_string_list))
15840 int len = strlen (spec);
15841 Lisp_Object tem = make_string (spec, len);
15842 props = Ftext_properties_at (make_number (charpos), elt);
15843 /* Should only keep face property in props */
15844 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15846 else
15848 int nglyphs_before, nwritten;
15850 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15851 nwritten = display_string (spec, Qnil, elt,
15852 charpos, 0, it,
15853 field, prec, 0,
15854 multibyte);
15856 /* Assign to the glyphs written above the
15857 string where the `%x' came from, position
15858 of the `%'. */
15859 if (nwritten > 0)
15861 struct glyph *glyph
15862 = (it->glyph_row->glyphs[TEXT_AREA]
15863 + nglyphs_before);
15864 int i;
15866 for (i = 0; i < nwritten; ++i)
15868 glyph[i].object = elt;
15869 glyph[i].charpos = charpos;
15872 n += nwritten;
15876 else /* c == 0 */
15877 break;
15881 break;
15883 case Lisp_Symbol:
15884 /* A symbol: process the value of the symbol recursively
15885 as if it appeared here directly. Avoid error if symbol void.
15886 Special case: if value of symbol is a string, output the string
15887 literally. */
15889 register Lisp_Object tem;
15891 /* If the variable is not marked as risky to set
15892 then its contents are risky to use. */
15893 if (NILP (Fget (elt, Qrisky_local_variable)))
15894 risky = 1;
15896 tem = Fboundp (elt);
15897 if (!NILP (tem))
15899 tem = Fsymbol_value (elt);
15900 /* If value is a string, output that string literally:
15901 don't check for % within it. */
15902 if (STRINGP (tem))
15903 literal = 1;
15905 if (!EQ (tem, elt))
15907 /* Give up right away for nil or t. */
15908 elt = tem;
15909 goto tail_recurse;
15913 break;
15915 case Lisp_Cons:
15917 register Lisp_Object car, tem;
15919 /* A cons cell: five distinct cases.
15920 If first element is :eval or :propertize, do something special.
15921 If first element is a string or a cons, process all the elements
15922 and effectively concatenate them.
15923 If first element is a negative number, truncate displaying cdr to
15924 at most that many characters. If positive, pad (with spaces)
15925 to at least that many characters.
15926 If first element is a symbol, process the cadr or caddr recursively
15927 according to whether the symbol's value is non-nil or nil. */
15928 car = XCAR (elt);
15929 if (EQ (car, QCeval))
15931 /* An element of the form (:eval FORM) means evaluate FORM
15932 and use the result as mode line elements. */
15934 if (risky)
15935 break;
15937 if (CONSP (XCDR (elt)))
15939 Lisp_Object spec;
15940 spec = safe_eval (XCAR (XCDR (elt)));
15941 n += display_mode_element (it, depth, field_width - n,
15942 precision - n, spec, props,
15943 risky);
15946 else if (EQ (car, QCpropertize))
15948 /* An element of the form (:propertize ELT PROPS...)
15949 means display ELT but applying properties PROPS. */
15951 if (risky)
15952 break;
15954 if (CONSP (XCDR (elt)))
15955 n += display_mode_element (it, depth, field_width - n,
15956 precision - n, XCAR (XCDR (elt)),
15957 XCDR (XCDR (elt)), risky);
15959 else if (SYMBOLP (car))
15961 tem = Fboundp (car);
15962 elt = XCDR (elt);
15963 if (!CONSP (elt))
15964 goto invalid;
15965 /* elt is now the cdr, and we know it is a cons cell.
15966 Use its car if CAR has a non-nil value. */
15967 if (!NILP (tem))
15969 tem = Fsymbol_value (car);
15970 if (!NILP (tem))
15972 elt = XCAR (elt);
15973 goto tail_recurse;
15976 /* Symbol's value is nil (or symbol is unbound)
15977 Get the cddr of the original list
15978 and if possible find the caddr and use that. */
15979 elt = XCDR (elt);
15980 if (NILP (elt))
15981 break;
15982 else if (!CONSP (elt))
15983 goto invalid;
15984 elt = XCAR (elt);
15985 goto tail_recurse;
15987 else if (INTEGERP (car))
15989 register int lim = XINT (car);
15990 elt = XCDR (elt);
15991 if (lim < 0)
15993 /* Negative int means reduce maximum width. */
15994 if (precision <= 0)
15995 precision = -lim;
15996 else
15997 precision = min (precision, -lim);
15999 else if (lim > 0)
16001 /* Padding specified. Don't let it be more than
16002 current maximum. */
16003 if (precision > 0)
16004 lim = min (precision, lim);
16006 /* If that's more padding than already wanted, queue it.
16007 But don't reduce padding already specified even if
16008 that is beyond the current truncation point. */
16009 field_width = max (lim, field_width);
16011 goto tail_recurse;
16013 else if (STRINGP (car) || CONSP (car))
16015 register int limit = 50;
16016 /* Limit is to protect against circular lists. */
16017 while (CONSP (elt)
16018 && --limit > 0
16019 && (precision <= 0 || n < precision))
16021 n += display_mode_element (it, depth, field_width - n,
16022 precision - n, XCAR (elt),
16023 props, risky);
16024 elt = XCDR (elt);
16028 break;
16030 default:
16031 invalid:
16032 elt = build_string ("*invalid*");
16033 goto tail_recurse;
16036 /* Pad to FIELD_WIDTH. */
16037 if (field_width > 0 && n < field_width)
16039 if (frame_title_ptr)
16040 n += store_frame_title ("", field_width - n, 0);
16041 else if (!NILP (mode_line_string_list))
16042 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16043 else
16044 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16045 0, 0, 0);
16048 return n;
16051 /* Store a mode-line string element in mode_line_string_list.
16053 If STRING is non-null, display that C string. Otherwise, the Lisp
16054 string LISP_STRING is displayed.
16056 FIELD_WIDTH is the minimum number of output glyphs to produce.
16057 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16058 with spaces. FIELD_WIDTH <= 0 means don't pad.
16060 PRECISION is the maximum number of characters to output from
16061 STRING. PRECISION <= 0 means don't truncate the string.
16063 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16064 properties to the string.
16066 PROPS are the properties to add to the string.
16067 The mode_line_string_face face property is always added to the string.
16070 static int
16071 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16072 char *string;
16073 Lisp_Object lisp_string;
16074 int copy_string;
16075 int field_width;
16076 int precision;
16077 Lisp_Object props;
16079 int len;
16080 int n = 0;
16082 if (string != NULL)
16084 len = strlen (string);
16085 if (precision > 0 && len > precision)
16086 len = precision;
16087 lisp_string = make_string (string, len);
16088 if (NILP (props))
16089 props = mode_line_string_face_prop;
16090 else if (!NILP (mode_line_string_face))
16092 Lisp_Object face = Fplist_get (props, Qface);
16093 props = Fcopy_sequence (props);
16094 if (NILP (face))
16095 face = mode_line_string_face;
16096 else
16097 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16098 props = Fplist_put (props, Qface, face);
16100 Fadd_text_properties (make_number (0), make_number (len),
16101 props, lisp_string);
16103 else
16105 len = XFASTINT (Flength (lisp_string));
16106 if (precision > 0 && len > precision)
16108 len = precision;
16109 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16110 precision = -1;
16112 if (!NILP (mode_line_string_face))
16114 Lisp_Object face;
16115 if (NILP (props))
16116 props = Ftext_properties_at (make_number (0), lisp_string);
16117 face = Fplist_get (props, Qface);
16118 if (NILP (face))
16119 face = mode_line_string_face;
16120 else
16121 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16122 props = Fcons (Qface, Fcons (face, Qnil));
16123 if (copy_string)
16124 lisp_string = Fcopy_sequence (lisp_string);
16126 if (!NILP (props))
16127 Fadd_text_properties (make_number (0), make_number (len),
16128 props, lisp_string);
16131 if (len > 0)
16133 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16134 n += len;
16137 if (field_width > len)
16139 field_width -= len;
16140 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16141 if (!NILP (props))
16142 Fadd_text_properties (make_number (0), make_number (field_width),
16143 props, lisp_string);
16144 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16145 n += field_width;
16148 return n;
16152 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16153 1, 4, 0,
16154 doc: /* Format a string out of a mode line format specification.
16155 First arg FORMAT specifies the mode line format (see `mode-line-format'
16156 for details) to use.
16158 Optional second arg FACE specifies the face property to put
16159 on all characters for which no face is specified.
16160 t means whatever face the window's mode line currently uses
16161 \(either `mode-line' or `mode-line-inactive', depending).
16162 nil means the default is no face property.
16163 If FACE is an integer, the value string has no text properties.
16165 Optional third and fourth args WINDOW and BUFFER specify the window
16166 and buffer to use as the context for the formatting (defaults
16167 are the selected window and the window's buffer). */)
16168 (format, face, window, buffer)
16169 Lisp_Object format, face, window, buffer;
16171 struct it it;
16172 int len;
16173 struct window *w;
16174 struct buffer *old_buffer = NULL;
16175 int face_id = -1;
16176 int no_props = INTEGERP (face);
16178 if (NILP (window))
16179 window = selected_window;
16180 CHECK_WINDOW (window);
16181 w = XWINDOW (window);
16183 if (NILP (buffer))
16184 buffer = w->buffer;
16185 CHECK_BUFFER (buffer);
16187 if (NILP (format))
16188 return build_string ("");
16190 if (no_props)
16191 face = Qnil;
16193 if (!NILP (face))
16195 if (EQ (face, Qt))
16196 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16197 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16200 if (face_id < 0)
16201 face_id = DEFAULT_FACE_ID;
16203 if (XBUFFER (buffer) != current_buffer)
16205 old_buffer = current_buffer;
16206 set_buffer_internal_1 (XBUFFER (buffer));
16209 init_iterator (&it, w, -1, -1, NULL, face_id);
16211 if (!no_props)
16213 mode_line_string_face = face;
16214 mode_line_string_face_prop
16215 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16217 /* We need a dummy last element in mode_line_string_list to
16218 indicate we are building the propertized mode-line string.
16219 Using mode_line_string_face_prop here GC protects it. */
16220 mode_line_string_list
16221 = Fcons (mode_line_string_face_prop, Qnil);
16222 frame_title_ptr = NULL;
16224 else
16226 mode_line_string_face_prop = Qnil;
16227 mode_line_string_list = Qnil;
16228 frame_title_ptr = frame_title_buf;
16231 push_frame_kboard (it.f);
16232 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16233 pop_frame_kboard ();
16235 if (old_buffer)
16236 set_buffer_internal_1 (old_buffer);
16238 if (!no_props)
16240 Lisp_Object str;
16241 mode_line_string_list = Fnreverse (mode_line_string_list);
16242 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16243 make_string ("", 0));
16244 mode_line_string_face_prop = Qnil;
16245 mode_line_string_list = Qnil;
16246 return str;
16249 len = frame_title_ptr - frame_title_buf;
16250 if (len > 0 && frame_title_ptr[-1] == '-')
16252 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16253 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16255 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16256 if (len > frame_title_ptr - frame_title_buf)
16257 len = frame_title_ptr - frame_title_buf;
16260 frame_title_ptr = NULL;
16261 return make_string (frame_title_buf, len);
16264 /* Write a null-terminated, right justified decimal representation of
16265 the positive integer D to BUF using a minimal field width WIDTH. */
16267 static void
16268 pint2str (buf, width, d)
16269 register char *buf;
16270 register int width;
16271 register int d;
16273 register char *p = buf;
16275 if (d <= 0)
16276 *p++ = '0';
16277 else
16279 while (d > 0)
16281 *p++ = d % 10 + '0';
16282 d /= 10;
16286 for (width -= (int) (p - buf); width > 0; --width)
16287 *p++ = ' ';
16288 *p-- = '\0';
16289 while (p > buf)
16291 d = *buf;
16292 *buf++ = *p;
16293 *p-- = d;
16297 /* Write a null-terminated, right justified decimal and "human
16298 readable" representation of the nonnegative integer D to BUF using
16299 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16301 static const char power_letter[] =
16303 0, /* not used */
16304 'k', /* kilo */
16305 'M', /* mega */
16306 'G', /* giga */
16307 'T', /* tera */
16308 'P', /* peta */
16309 'E', /* exa */
16310 'Z', /* zetta */
16311 'Y' /* yotta */
16314 static void
16315 pint2hrstr (buf, width, d)
16316 char *buf;
16317 int width;
16318 int d;
16320 /* We aim to represent the nonnegative integer D as
16321 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16322 int quotient = d;
16323 int remainder = 0;
16324 /* -1 means: do not use TENTHS. */
16325 int tenths = -1;
16326 int exponent = 0;
16328 /* Length of QUOTIENT.TENTHS as a string. */
16329 int length;
16331 char * psuffix;
16332 char * p;
16334 if (1000 <= quotient)
16336 /* Scale to the appropriate EXPONENT. */
16339 remainder = quotient % 1000;
16340 quotient /= 1000;
16341 exponent++;
16343 while (1000 <= quotient);
16345 /* Round to nearest and decide whether to use TENTHS or not. */
16346 if (quotient <= 9)
16348 tenths = remainder / 100;
16349 if (50 <= remainder % 100)
16351 if (tenths < 9)
16352 tenths++;
16353 else
16355 quotient++;
16356 if (quotient == 10)
16357 tenths = -1;
16358 else
16359 tenths = 0;
16363 else
16364 if (500 <= remainder)
16366 if (quotient < 999)
16367 quotient++;
16368 else
16370 quotient = 1;
16371 exponent++;
16372 tenths = 0;
16377 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16378 if (tenths == -1 && quotient <= 99)
16379 if (quotient <= 9)
16380 length = 1;
16381 else
16382 length = 2;
16383 else
16384 length = 3;
16385 p = psuffix = buf + max (width, length);
16387 /* Print EXPONENT. */
16388 if (exponent)
16389 *psuffix++ = power_letter[exponent];
16390 *psuffix = '\0';
16392 /* Print TENTHS. */
16393 if (tenths >= 0)
16395 *--p = '0' + tenths;
16396 *--p = '.';
16399 /* Print QUOTIENT. */
16402 int digit = quotient % 10;
16403 *--p = '0' + digit;
16405 while ((quotient /= 10) != 0);
16407 /* Print leading spaces. */
16408 while (buf < p)
16409 *--p = ' ';
16412 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16413 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16414 type of CODING_SYSTEM. Return updated pointer into BUF. */
16416 static unsigned char invalid_eol_type[] = "(*invalid*)";
16418 static char *
16419 decode_mode_spec_coding (coding_system, buf, eol_flag)
16420 Lisp_Object coding_system;
16421 register char *buf;
16422 int eol_flag;
16424 Lisp_Object val;
16425 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16426 const unsigned char *eol_str;
16427 int eol_str_len;
16428 /* The EOL conversion we are using. */
16429 Lisp_Object eoltype;
16431 val = Fget (coding_system, Qcoding_system);
16432 eoltype = Qnil;
16434 if (!VECTORP (val)) /* Not yet decided. */
16436 if (multibyte)
16437 *buf++ = '-';
16438 if (eol_flag)
16439 eoltype = eol_mnemonic_undecided;
16440 /* Don't mention EOL conversion if it isn't decided. */
16442 else
16444 Lisp_Object eolvalue;
16446 eolvalue = Fget (coding_system, Qeol_type);
16448 if (multibyte)
16449 *buf++ = XFASTINT (AREF (val, 1));
16451 if (eol_flag)
16453 /* The EOL conversion that is normal on this system. */
16455 if (NILP (eolvalue)) /* Not yet decided. */
16456 eoltype = eol_mnemonic_undecided;
16457 else if (VECTORP (eolvalue)) /* Not yet decided. */
16458 eoltype = eol_mnemonic_undecided;
16459 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16460 eoltype = (XFASTINT (eolvalue) == 0
16461 ? eol_mnemonic_unix
16462 : (XFASTINT (eolvalue) == 1
16463 ? eol_mnemonic_dos : eol_mnemonic_mac));
16467 if (eol_flag)
16469 /* Mention the EOL conversion if it is not the usual one. */
16470 if (STRINGP (eoltype))
16472 eol_str = SDATA (eoltype);
16473 eol_str_len = SBYTES (eoltype);
16475 else if (INTEGERP (eoltype)
16476 && CHAR_VALID_P (XINT (eoltype), 0))
16478 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16479 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16480 eol_str = tmp;
16482 else
16484 eol_str = invalid_eol_type;
16485 eol_str_len = sizeof (invalid_eol_type) - 1;
16487 bcopy (eol_str, buf, eol_str_len);
16488 buf += eol_str_len;
16491 return buf;
16494 /* Return a string for the output of a mode line %-spec for window W,
16495 generated by character C. PRECISION >= 0 means don't return a
16496 string longer than that value. FIELD_WIDTH > 0 means pad the
16497 string returned with spaces to that value. Return 1 in *MULTIBYTE
16498 if the result is multibyte text.
16500 Note we operate on the current buffer for most purposes,
16501 the exception being w->base_line_pos. */
16503 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16505 static char *
16506 decode_mode_spec (w, c, field_width, precision, multibyte)
16507 struct window *w;
16508 register int c;
16509 int field_width, precision;
16510 int *multibyte;
16512 Lisp_Object obj;
16513 struct frame *f = XFRAME (WINDOW_FRAME (w));
16514 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16515 struct buffer *b = current_buffer;
16517 obj = Qnil;
16518 *multibyte = 0;
16520 switch (c)
16522 case '*':
16523 if (!NILP (b->read_only))
16524 return "%";
16525 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16526 return "*";
16527 return "-";
16529 case '+':
16530 /* This differs from %* only for a modified read-only buffer. */
16531 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16532 return "*";
16533 if (!NILP (b->read_only))
16534 return "%";
16535 return "-";
16537 case '&':
16538 /* This differs from %* in ignoring read-only-ness. */
16539 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16540 return "*";
16541 return "-";
16543 case '%':
16544 return "%";
16546 case '[':
16548 int i;
16549 char *p;
16551 if (command_loop_level > 5)
16552 return "[[[... ";
16553 p = decode_mode_spec_buf;
16554 for (i = 0; i < command_loop_level; i++)
16555 *p++ = '[';
16556 *p = 0;
16557 return decode_mode_spec_buf;
16560 case ']':
16562 int i;
16563 char *p;
16565 if (command_loop_level > 5)
16566 return " ...]]]";
16567 p = decode_mode_spec_buf;
16568 for (i = 0; i < command_loop_level; i++)
16569 *p++ = ']';
16570 *p = 0;
16571 return decode_mode_spec_buf;
16574 case '-':
16576 register int i;
16578 /* Let lots_of_dashes be a string of infinite length. */
16579 if (!NILP (mode_line_string_list))
16580 return "--";
16581 if (field_width <= 0
16582 || field_width > sizeof (lots_of_dashes))
16584 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16585 decode_mode_spec_buf[i] = '-';
16586 decode_mode_spec_buf[i] = '\0';
16587 return decode_mode_spec_buf;
16589 else
16590 return lots_of_dashes;
16593 case 'b':
16594 obj = b->name;
16595 break;
16597 case 'c':
16599 int col = (int) current_column (); /* iftc */
16600 w->column_number_displayed = make_number (col);
16601 pint2str (decode_mode_spec_buf, field_width, col);
16602 return decode_mode_spec_buf;
16605 case 'F':
16606 /* %F displays the frame name. */
16607 if (!NILP (f->title))
16608 return (char *) SDATA (f->title);
16609 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16610 return (char *) SDATA (f->name);
16611 return "Emacs";
16613 case 'f':
16614 obj = b->filename;
16615 break;
16617 case 'i':
16619 int size = ZV - BEGV;
16620 pint2str (decode_mode_spec_buf, field_width, size);
16621 return decode_mode_spec_buf;
16624 case 'I':
16626 int size = ZV - BEGV;
16627 pint2hrstr (decode_mode_spec_buf, field_width, size);
16628 return decode_mode_spec_buf;
16631 case 'l':
16633 int startpos = XMARKER (w->start)->charpos;
16634 int startpos_byte = marker_byte_position (w->start);
16635 int line, linepos, linepos_byte, topline;
16636 int nlines, junk;
16637 int height = WINDOW_TOTAL_LINES (w);
16639 /* If we decided that this buffer isn't suitable for line numbers,
16640 don't forget that too fast. */
16641 if (EQ (w->base_line_pos, w->buffer))
16642 goto no_value;
16643 /* But do forget it, if the window shows a different buffer now. */
16644 else if (BUFFERP (w->base_line_pos))
16645 w->base_line_pos = Qnil;
16647 /* If the buffer is very big, don't waste time. */
16648 if (INTEGERP (Vline_number_display_limit)
16649 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16651 w->base_line_pos = Qnil;
16652 w->base_line_number = Qnil;
16653 goto no_value;
16656 if (!NILP (w->base_line_number)
16657 && !NILP (w->base_line_pos)
16658 && XFASTINT (w->base_line_pos) <= startpos)
16660 line = XFASTINT (w->base_line_number);
16661 linepos = XFASTINT (w->base_line_pos);
16662 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16664 else
16666 line = 1;
16667 linepos = BUF_BEGV (b);
16668 linepos_byte = BUF_BEGV_BYTE (b);
16671 /* Count lines from base line to window start position. */
16672 nlines = display_count_lines (linepos, linepos_byte,
16673 startpos_byte,
16674 startpos, &junk);
16676 topline = nlines + line;
16678 /* Determine a new base line, if the old one is too close
16679 or too far away, or if we did not have one.
16680 "Too close" means it's plausible a scroll-down would
16681 go back past it. */
16682 if (startpos == BUF_BEGV (b))
16684 w->base_line_number = make_number (topline);
16685 w->base_line_pos = make_number (BUF_BEGV (b));
16687 else if (nlines < height + 25 || nlines > height * 3 + 50
16688 || linepos == BUF_BEGV (b))
16690 int limit = BUF_BEGV (b);
16691 int limit_byte = BUF_BEGV_BYTE (b);
16692 int position;
16693 int distance = (height * 2 + 30) * line_number_display_limit_width;
16695 if (startpos - distance > limit)
16697 limit = startpos - distance;
16698 limit_byte = CHAR_TO_BYTE (limit);
16701 nlines = display_count_lines (startpos, startpos_byte,
16702 limit_byte,
16703 - (height * 2 + 30),
16704 &position);
16705 /* If we couldn't find the lines we wanted within
16706 line_number_display_limit_width chars per line,
16707 give up on line numbers for this window. */
16708 if (position == limit_byte && limit == startpos - distance)
16710 w->base_line_pos = w->buffer;
16711 w->base_line_number = Qnil;
16712 goto no_value;
16715 w->base_line_number = make_number (topline - nlines);
16716 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16719 /* Now count lines from the start pos to point. */
16720 nlines = display_count_lines (startpos, startpos_byte,
16721 PT_BYTE, PT, &junk);
16723 /* Record that we did display the line number. */
16724 line_number_displayed = 1;
16726 /* Make the string to show. */
16727 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16728 return decode_mode_spec_buf;
16729 no_value:
16731 char* p = decode_mode_spec_buf;
16732 int pad = field_width - 2;
16733 while (pad-- > 0)
16734 *p++ = ' ';
16735 *p++ = '?';
16736 *p++ = '?';
16737 *p = '\0';
16738 return decode_mode_spec_buf;
16741 break;
16743 case 'm':
16744 obj = b->mode_name;
16745 break;
16747 case 'n':
16748 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16749 return " Narrow";
16750 break;
16752 case 'p':
16754 int pos = marker_position (w->start);
16755 int total = BUF_ZV (b) - BUF_BEGV (b);
16757 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16759 if (pos <= BUF_BEGV (b))
16760 return "All";
16761 else
16762 return "Bottom";
16764 else if (pos <= BUF_BEGV (b))
16765 return "Top";
16766 else
16768 if (total > 1000000)
16769 /* Do it differently for a large value, to avoid overflow. */
16770 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16771 else
16772 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16773 /* We can't normally display a 3-digit number,
16774 so get us a 2-digit number that is close. */
16775 if (total == 100)
16776 total = 99;
16777 sprintf (decode_mode_spec_buf, "%2d%%", total);
16778 return decode_mode_spec_buf;
16782 /* Display percentage of size above the bottom of the screen. */
16783 case 'P':
16785 int toppos = marker_position (w->start);
16786 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16787 int total = BUF_ZV (b) - BUF_BEGV (b);
16789 if (botpos >= BUF_ZV (b))
16791 if (toppos <= BUF_BEGV (b))
16792 return "All";
16793 else
16794 return "Bottom";
16796 else
16798 if (total > 1000000)
16799 /* Do it differently for a large value, to avoid overflow. */
16800 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16801 else
16802 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16803 /* We can't normally display a 3-digit number,
16804 so get us a 2-digit number that is close. */
16805 if (total == 100)
16806 total = 99;
16807 if (toppos <= BUF_BEGV (b))
16808 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16809 else
16810 sprintf (decode_mode_spec_buf, "%2d%%", total);
16811 return decode_mode_spec_buf;
16815 case 's':
16816 /* status of process */
16817 obj = Fget_buffer_process (Fcurrent_buffer ());
16818 if (NILP (obj))
16819 return "no process";
16820 #ifdef subprocesses
16821 obj = Fsymbol_name (Fprocess_status (obj));
16822 #endif
16823 break;
16825 case 't': /* indicate TEXT or BINARY */
16826 #ifdef MODE_LINE_BINARY_TEXT
16827 return MODE_LINE_BINARY_TEXT (b);
16828 #else
16829 return "T";
16830 #endif
16832 case 'z':
16833 /* coding-system (not including end-of-line format) */
16834 case 'Z':
16835 /* coding-system (including end-of-line type) */
16837 int eol_flag = (c == 'Z');
16838 char *p = decode_mode_spec_buf;
16840 if (! FRAME_WINDOW_P (f))
16842 /* No need to mention EOL here--the terminal never needs
16843 to do EOL conversion. */
16844 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16845 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16847 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16848 p, eol_flag);
16850 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16851 #ifdef subprocesses
16852 obj = Fget_buffer_process (Fcurrent_buffer ());
16853 if (PROCESSP (obj))
16855 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16856 p, eol_flag);
16857 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16858 p, eol_flag);
16860 #endif /* subprocesses */
16861 #endif /* 0 */
16862 *p = 0;
16863 return decode_mode_spec_buf;
16867 if (STRINGP (obj))
16869 *multibyte = STRING_MULTIBYTE (obj);
16870 return (char *) SDATA (obj);
16872 else
16873 return "";
16877 /* Count up to COUNT lines starting from START / START_BYTE.
16878 But don't go beyond LIMIT_BYTE.
16879 Return the number of lines thus found (always nonnegative).
16881 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16883 static int
16884 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16885 int start, start_byte, limit_byte, count;
16886 int *byte_pos_ptr;
16888 register unsigned char *cursor;
16889 unsigned char *base;
16891 register int ceiling;
16892 register unsigned char *ceiling_addr;
16893 int orig_count = count;
16895 /* If we are not in selective display mode,
16896 check only for newlines. */
16897 int selective_display = (!NILP (current_buffer->selective_display)
16898 && !INTEGERP (current_buffer->selective_display));
16900 if (count > 0)
16902 while (start_byte < limit_byte)
16904 ceiling = BUFFER_CEILING_OF (start_byte);
16905 ceiling = min (limit_byte - 1, ceiling);
16906 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16907 base = (cursor = BYTE_POS_ADDR (start_byte));
16908 while (1)
16910 if (selective_display)
16911 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16913 else
16914 while (*cursor != '\n' && ++cursor != ceiling_addr)
16917 if (cursor != ceiling_addr)
16919 if (--count == 0)
16921 start_byte += cursor - base + 1;
16922 *byte_pos_ptr = start_byte;
16923 return orig_count;
16925 else
16926 if (++cursor == ceiling_addr)
16927 break;
16929 else
16930 break;
16932 start_byte += cursor - base;
16935 else
16937 while (start_byte > limit_byte)
16939 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16940 ceiling = max (limit_byte, ceiling);
16941 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16942 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16943 while (1)
16945 if (selective_display)
16946 while (--cursor != ceiling_addr
16947 && *cursor != '\n' && *cursor != 015)
16949 else
16950 while (--cursor != ceiling_addr && *cursor != '\n')
16953 if (cursor != ceiling_addr)
16955 if (++count == 0)
16957 start_byte += cursor - base + 1;
16958 *byte_pos_ptr = start_byte;
16959 /* When scanning backwards, we should
16960 not count the newline posterior to which we stop. */
16961 return - orig_count - 1;
16964 else
16965 break;
16967 /* Here we add 1 to compensate for the last decrement
16968 of CURSOR, which took it past the valid range. */
16969 start_byte += cursor - base + 1;
16973 *byte_pos_ptr = limit_byte;
16975 if (count < 0)
16976 return - orig_count + count;
16977 return orig_count - count;
16983 /***********************************************************************
16984 Displaying strings
16985 ***********************************************************************/
16987 /* Display a NUL-terminated string, starting with index START.
16989 If STRING is non-null, display that C string. Otherwise, the Lisp
16990 string LISP_STRING is displayed.
16992 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16993 FACE_STRING. Display STRING or LISP_STRING with the face at
16994 FACE_STRING_POS in FACE_STRING:
16996 Display the string in the environment given by IT, but use the
16997 standard display table, temporarily.
16999 FIELD_WIDTH is the minimum number of output glyphs to produce.
17000 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17001 with spaces. If STRING has more characters, more than FIELD_WIDTH
17002 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17004 PRECISION is the maximum number of characters to output from
17005 STRING. PRECISION < 0 means don't truncate the string.
17007 This is roughly equivalent to printf format specifiers:
17009 FIELD_WIDTH PRECISION PRINTF
17010 ----------------------------------------
17011 -1 -1 %s
17012 -1 10 %.10s
17013 10 -1 %10s
17014 20 10 %20.10s
17016 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17017 display them, and < 0 means obey the current buffer's value of
17018 enable_multibyte_characters.
17020 Value is the number of glyphs produced. */
17022 static int
17023 display_string (string, lisp_string, face_string, face_string_pos,
17024 start, it, field_width, precision, max_x, multibyte)
17025 unsigned char *string;
17026 Lisp_Object lisp_string;
17027 Lisp_Object face_string;
17028 int face_string_pos;
17029 int start;
17030 struct it *it;
17031 int field_width, precision, max_x;
17032 int multibyte;
17034 int hpos_at_start = it->hpos;
17035 int saved_face_id = it->face_id;
17036 struct glyph_row *row = it->glyph_row;
17038 /* Initialize the iterator IT for iteration over STRING beginning
17039 with index START. */
17040 reseat_to_string (it, string, lisp_string, start,
17041 precision, field_width, multibyte);
17043 /* If displaying STRING, set up the face of the iterator
17044 from LISP_STRING, if that's given. */
17045 if (STRINGP (face_string))
17047 int endptr;
17048 struct face *face;
17050 it->face_id
17051 = face_at_string_position (it->w, face_string, face_string_pos,
17052 0, it->region_beg_charpos,
17053 it->region_end_charpos,
17054 &endptr, it->base_face_id, 0);
17055 face = FACE_FROM_ID (it->f, it->face_id);
17056 it->face_box_p = face->box != FACE_NO_BOX;
17059 /* Set max_x to the maximum allowed X position. Don't let it go
17060 beyond the right edge of the window. */
17061 if (max_x <= 0)
17062 max_x = it->last_visible_x;
17063 else
17064 max_x = min (max_x, it->last_visible_x);
17066 /* Skip over display elements that are not visible. because IT->w is
17067 hscrolled. */
17068 if (it->current_x < it->first_visible_x)
17069 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17070 MOVE_TO_POS | MOVE_TO_X);
17072 row->ascent = it->max_ascent;
17073 row->height = it->max_ascent + it->max_descent;
17074 row->phys_ascent = it->max_phys_ascent;
17075 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17076 row->extra_line_spacing = it->max_extra_line_spacing;
17078 /* This condition is for the case that we are called with current_x
17079 past last_visible_x. */
17080 while (it->current_x < max_x)
17082 int x_before, x, n_glyphs_before, i, nglyphs;
17084 /* Get the next display element. */
17085 if (!get_next_display_element (it))
17086 break;
17088 /* Produce glyphs. */
17089 x_before = it->current_x;
17090 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17091 PRODUCE_GLYPHS (it);
17093 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17094 i = 0;
17095 x = x_before;
17096 while (i < nglyphs)
17098 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17100 if (!it->truncate_lines_p
17101 && x + glyph->pixel_width > max_x)
17103 /* End of continued line or max_x reached. */
17104 if (CHAR_GLYPH_PADDING_P (*glyph))
17106 /* A wide character is unbreakable. */
17107 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17108 it->current_x = x_before;
17110 else
17112 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17113 it->current_x = x;
17115 break;
17117 else if (x + glyph->pixel_width > it->first_visible_x)
17119 /* Glyph is at least partially visible. */
17120 ++it->hpos;
17121 if (x < it->first_visible_x)
17122 it->glyph_row->x = x - it->first_visible_x;
17124 else
17126 /* Glyph is off the left margin of the display area.
17127 Should not happen. */
17128 abort ();
17131 row->ascent = max (row->ascent, it->max_ascent);
17132 row->height = max (row->height, it->max_ascent + it->max_descent);
17133 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17134 row->phys_height = max (row->phys_height,
17135 it->max_phys_ascent + it->max_phys_descent);
17136 row->extra_line_spacing = max (row->extra_line_spacing,
17137 it->max_extra_line_spacing);
17138 x += glyph->pixel_width;
17139 ++i;
17142 /* Stop if max_x reached. */
17143 if (i < nglyphs)
17144 break;
17146 /* Stop at line ends. */
17147 if (ITERATOR_AT_END_OF_LINE_P (it))
17149 it->continuation_lines_width = 0;
17150 break;
17153 set_iterator_to_next (it, 1);
17155 /* Stop if truncating at the right edge. */
17156 if (it->truncate_lines_p
17157 && it->current_x >= it->last_visible_x)
17159 /* Add truncation mark, but don't do it if the line is
17160 truncated at a padding space. */
17161 if (IT_CHARPOS (*it) < it->string_nchars)
17163 if (!FRAME_WINDOW_P (it->f))
17165 int i, n;
17167 if (it->current_x > it->last_visible_x)
17169 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17170 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17171 break;
17172 for (n = row->used[TEXT_AREA]; i < n; ++i)
17174 row->used[TEXT_AREA] = i;
17175 produce_special_glyphs (it, IT_TRUNCATION);
17178 produce_special_glyphs (it, IT_TRUNCATION);
17180 it->glyph_row->truncated_on_right_p = 1;
17182 break;
17186 /* Maybe insert a truncation at the left. */
17187 if (it->first_visible_x
17188 && IT_CHARPOS (*it) > 0)
17190 if (!FRAME_WINDOW_P (it->f))
17191 insert_left_trunc_glyphs (it);
17192 it->glyph_row->truncated_on_left_p = 1;
17195 it->face_id = saved_face_id;
17197 /* Value is number of columns displayed. */
17198 return it->hpos - hpos_at_start;
17203 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17204 appears as an element of LIST or as the car of an element of LIST.
17205 If PROPVAL is a list, compare each element against LIST in that
17206 way, and return 1/2 if any element of PROPVAL is found in LIST.
17207 Otherwise return 0. This function cannot quit.
17208 The return value is 2 if the text is invisible but with an ellipsis
17209 and 1 if it's invisible and without an ellipsis. */
17212 invisible_p (propval, list)
17213 register Lisp_Object propval;
17214 Lisp_Object list;
17216 register Lisp_Object tail, proptail;
17218 for (tail = list; CONSP (tail); tail = XCDR (tail))
17220 register Lisp_Object tem;
17221 tem = XCAR (tail);
17222 if (EQ (propval, tem))
17223 return 1;
17224 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17225 return NILP (XCDR (tem)) ? 1 : 2;
17228 if (CONSP (propval))
17230 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17232 Lisp_Object propelt;
17233 propelt = XCAR (proptail);
17234 for (tail = list; CONSP (tail); tail = XCDR (tail))
17236 register Lisp_Object tem;
17237 tem = XCAR (tail);
17238 if (EQ (propelt, tem))
17239 return 1;
17240 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17241 return NILP (XCDR (tem)) ? 1 : 2;
17246 return 0;
17249 /* Calculate a width or height in pixels from a specification using
17250 the following elements:
17252 SPEC ::=
17253 NUM - a (fractional) multiple of the default font width/height
17254 (NUM) - specifies exactly NUM pixels
17255 UNIT - a fixed number of pixels, see below.
17256 ELEMENT - size of a display element in pixels, see below.
17257 (NUM . SPEC) - equals NUM * SPEC
17258 (+ SPEC SPEC ...) - add pixel values
17259 (- SPEC SPEC ...) - subtract pixel values
17260 (- SPEC) - negate pixel value
17262 NUM ::=
17263 INT or FLOAT - a number constant
17264 SYMBOL - use symbol's (buffer local) variable binding.
17266 UNIT ::=
17267 in - pixels per inch *)
17268 mm - pixels per 1/1000 meter *)
17269 cm - pixels per 1/100 meter *)
17270 width - width of current font in pixels.
17271 height - height of current font in pixels.
17273 *) using the ratio(s) defined in display-pixels-per-inch.
17275 ELEMENT ::=
17277 left-fringe - left fringe width in pixels
17278 right-fringe - right fringe width in pixels
17280 left-margin - left margin width in pixels
17281 right-margin - right margin width in pixels
17283 scroll-bar - scroll-bar area width in pixels
17285 Examples:
17287 Pixels corresponding to 5 inches:
17288 (5 . in)
17290 Total width of non-text areas on left side of window (if scroll-bar is on left):
17291 '(space :width (+ left-fringe left-margin scroll-bar))
17293 Align to first text column (in header line):
17294 '(space :align-to 0)
17296 Align to middle of text area minus half the width of variable `my-image'
17297 containing a loaded image:
17298 '(space :align-to (0.5 . (- text my-image)))
17300 Width of left margin minus width of 1 character in the default font:
17301 '(space :width (- left-margin 1))
17303 Width of left margin minus width of 2 characters in the current font:
17304 '(space :width (- left-margin (2 . width)))
17306 Center 1 character over left-margin (in header line):
17307 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17309 Different ways to express width of left fringe plus left margin minus one pixel:
17310 '(space :width (- (+ left-fringe left-margin) (1)))
17311 '(space :width (+ left-fringe left-margin (- (1))))
17312 '(space :width (+ left-fringe left-margin (-1)))
17316 #define NUMVAL(X) \
17317 ((INTEGERP (X) || FLOATP (X)) \
17318 ? XFLOATINT (X) \
17319 : - 1)
17322 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17323 double *res;
17324 struct it *it;
17325 Lisp_Object prop;
17326 void *font;
17327 int width_p, *align_to;
17329 double pixels;
17331 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17332 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17334 if (NILP (prop))
17335 return OK_PIXELS (0);
17337 if (SYMBOLP (prop))
17339 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17341 char *unit = SDATA (SYMBOL_NAME (prop));
17343 if (unit[0] == 'i' && unit[1] == 'n')
17344 pixels = 1.0;
17345 else if (unit[0] == 'm' && unit[1] == 'm')
17346 pixels = 25.4;
17347 else if (unit[0] == 'c' && unit[1] == 'm')
17348 pixels = 2.54;
17349 else
17350 pixels = 0;
17351 if (pixels > 0)
17353 double ppi;
17354 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17355 || (CONSP (Vdisplay_pixels_per_inch)
17356 && (ppi = (width_p
17357 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17358 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17359 ppi > 0)))
17360 return OK_PIXELS (ppi / pixels);
17362 return 0;
17366 #ifdef HAVE_WINDOW_SYSTEM
17367 if (EQ (prop, Qheight))
17368 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17369 if (EQ (prop, Qwidth))
17370 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17371 #else
17372 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17373 return OK_PIXELS (1);
17374 #endif
17376 if (EQ (prop, Qtext))
17377 return OK_PIXELS (width_p
17378 ? window_box_width (it->w, TEXT_AREA)
17379 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17381 if (align_to && *align_to < 0)
17383 *res = 0;
17384 if (EQ (prop, Qleft))
17385 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17386 if (EQ (prop, Qright))
17387 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17388 if (EQ (prop, Qcenter))
17389 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17390 + window_box_width (it->w, TEXT_AREA) / 2);
17391 if (EQ (prop, Qleft_fringe))
17392 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17394 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17395 if (EQ (prop, Qright_fringe))
17396 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17397 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17398 : window_box_right_offset (it->w, TEXT_AREA));
17399 if (EQ (prop, Qleft_margin))
17400 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17401 if (EQ (prop, Qright_margin))
17402 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17403 if (EQ (prop, Qscroll_bar))
17404 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17406 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17407 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17408 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17409 : 0)));
17411 else
17413 if (EQ (prop, Qleft_fringe))
17414 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17415 if (EQ (prop, Qright_fringe))
17416 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17417 if (EQ (prop, Qleft_margin))
17418 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17419 if (EQ (prop, Qright_margin))
17420 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17421 if (EQ (prop, Qscroll_bar))
17422 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17425 prop = Fbuffer_local_value (prop, it->w->buffer);
17428 if (INTEGERP (prop) || FLOATP (prop))
17430 int base_unit = (width_p
17431 ? FRAME_COLUMN_WIDTH (it->f)
17432 : FRAME_LINE_HEIGHT (it->f));
17433 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17436 if (CONSP (prop))
17438 Lisp_Object car = XCAR (prop);
17439 Lisp_Object cdr = XCDR (prop);
17441 if (SYMBOLP (car))
17443 #ifdef HAVE_WINDOW_SYSTEM
17444 if (valid_image_p (prop))
17446 int id = lookup_image (it->f, prop);
17447 struct image *img = IMAGE_FROM_ID (it->f, id);
17449 return OK_PIXELS (width_p ? img->width : img->height);
17451 #endif
17452 if (EQ (car, Qplus) || EQ (car, Qminus))
17454 int first = 1;
17455 double px;
17457 pixels = 0;
17458 while (CONSP (cdr))
17460 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17461 font, width_p, align_to))
17462 return 0;
17463 if (first)
17464 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17465 else
17466 pixels += px;
17467 cdr = XCDR (cdr);
17469 if (EQ (car, Qminus))
17470 pixels = -pixels;
17471 return OK_PIXELS (pixels);
17474 car = Fbuffer_local_value (car, it->w->buffer);
17477 if (INTEGERP (car) || FLOATP (car))
17479 double fact;
17480 pixels = XFLOATINT (car);
17481 if (NILP (cdr))
17482 return OK_PIXELS (pixels);
17483 if (calc_pixel_width_or_height (&fact, it, cdr,
17484 font, width_p, align_to))
17485 return OK_PIXELS (pixels * fact);
17486 return 0;
17489 return 0;
17492 return 0;
17496 /***********************************************************************
17497 Glyph Display
17498 ***********************************************************************/
17500 #ifdef HAVE_WINDOW_SYSTEM
17502 #if GLYPH_DEBUG
17504 void
17505 dump_glyph_string (s)
17506 struct glyph_string *s;
17508 fprintf (stderr, "glyph string\n");
17509 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17510 s->x, s->y, s->width, s->height);
17511 fprintf (stderr, " ybase = %d\n", s->ybase);
17512 fprintf (stderr, " hl = %d\n", s->hl);
17513 fprintf (stderr, " left overhang = %d, right = %d\n",
17514 s->left_overhang, s->right_overhang);
17515 fprintf (stderr, " nchars = %d\n", s->nchars);
17516 fprintf (stderr, " extends to end of line = %d\n",
17517 s->extends_to_end_of_line_p);
17518 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17519 fprintf (stderr, " bg width = %d\n", s->background_width);
17522 #endif /* GLYPH_DEBUG */
17524 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17525 of XChar2b structures for S; it can't be allocated in
17526 init_glyph_string because it must be allocated via `alloca'. W
17527 is the window on which S is drawn. ROW and AREA are the glyph row
17528 and area within the row from which S is constructed. START is the
17529 index of the first glyph structure covered by S. HL is a
17530 face-override for drawing S. */
17532 #ifdef HAVE_NTGUI
17533 #define OPTIONAL_HDC(hdc) hdc,
17534 #define DECLARE_HDC(hdc) HDC hdc;
17535 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17536 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17537 #endif
17539 #ifndef OPTIONAL_HDC
17540 #define OPTIONAL_HDC(hdc)
17541 #define DECLARE_HDC(hdc)
17542 #define ALLOCATE_HDC(hdc, f)
17543 #define RELEASE_HDC(hdc, f)
17544 #endif
17546 static void
17547 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17548 struct glyph_string *s;
17549 DECLARE_HDC (hdc)
17550 XChar2b *char2b;
17551 struct window *w;
17552 struct glyph_row *row;
17553 enum glyph_row_area area;
17554 int start;
17555 enum draw_glyphs_face hl;
17557 bzero (s, sizeof *s);
17558 s->w = w;
17559 s->f = XFRAME (w->frame);
17560 #ifdef HAVE_NTGUI
17561 s->hdc = hdc;
17562 #endif
17563 s->display = FRAME_X_DISPLAY (s->f);
17564 s->window = FRAME_X_WINDOW (s->f);
17565 s->char2b = char2b;
17566 s->hl = hl;
17567 s->row = row;
17568 s->area = area;
17569 s->first_glyph = row->glyphs[area] + start;
17570 s->height = row->height;
17571 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17573 /* Display the internal border below the tool-bar window. */
17574 if (WINDOWP (s->f->tool_bar_window)
17575 && s->w == XWINDOW (s->f->tool_bar_window))
17576 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17578 s->ybase = s->y + row->ascent;
17582 /* Append the list of glyph strings with head H and tail T to the list
17583 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17585 static INLINE void
17586 append_glyph_string_lists (head, tail, h, t)
17587 struct glyph_string **head, **tail;
17588 struct glyph_string *h, *t;
17590 if (h)
17592 if (*head)
17593 (*tail)->next = h;
17594 else
17595 *head = h;
17596 h->prev = *tail;
17597 *tail = t;
17602 /* Prepend the list of glyph strings with head H and tail T to the
17603 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17604 result. */
17606 static INLINE void
17607 prepend_glyph_string_lists (head, tail, h, t)
17608 struct glyph_string **head, **tail;
17609 struct glyph_string *h, *t;
17611 if (h)
17613 if (*head)
17614 (*head)->prev = t;
17615 else
17616 *tail = t;
17617 t->next = *head;
17618 *head = h;
17623 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17624 Set *HEAD and *TAIL to the resulting list. */
17626 static INLINE void
17627 append_glyph_string (head, tail, s)
17628 struct glyph_string **head, **tail;
17629 struct glyph_string *s;
17631 s->next = s->prev = NULL;
17632 append_glyph_string_lists (head, tail, s, s);
17636 /* Get face and two-byte form of character glyph GLYPH on frame F.
17637 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17638 a pointer to a realized face that is ready for display. */
17640 static INLINE struct face *
17641 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17642 struct frame *f;
17643 struct glyph *glyph;
17644 XChar2b *char2b;
17645 int *two_byte_p;
17647 struct face *face;
17649 xassert (glyph->type == CHAR_GLYPH);
17650 face = FACE_FROM_ID (f, glyph->face_id);
17652 if (two_byte_p)
17653 *two_byte_p = 0;
17655 if (!glyph->multibyte_p)
17657 /* Unibyte case. We don't have to encode, but we have to make
17658 sure to use a face suitable for unibyte. */
17659 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17661 else if (glyph->u.ch < 128
17662 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17664 /* Case of ASCII in a face known to fit ASCII. */
17665 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17667 else
17669 int c1, c2, charset;
17671 /* Split characters into bytes. If c2 is -1 afterwards, C is
17672 really a one-byte character so that byte1 is zero. */
17673 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17674 if (c2 > 0)
17675 STORE_XCHAR2B (char2b, c1, c2);
17676 else
17677 STORE_XCHAR2B (char2b, 0, c1);
17679 /* Maybe encode the character in *CHAR2B. */
17680 if (charset != CHARSET_ASCII)
17682 struct font_info *font_info
17683 = FONT_INFO_FROM_ID (f, face->font_info_id);
17684 if (font_info)
17685 glyph->font_type
17686 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17690 /* Make sure X resources of the face are allocated. */
17691 xassert (face != NULL);
17692 PREPARE_FACE_FOR_DISPLAY (f, face);
17693 return face;
17697 /* Fill glyph string S with composition components specified by S->cmp.
17699 FACES is an array of faces for all components of this composition.
17700 S->gidx is the index of the first component for S.
17701 OVERLAPS_P non-zero means S should draw the foreground only, and
17702 use its physical height for clipping.
17704 Value is the index of a component not in S. */
17706 static int
17707 fill_composite_glyph_string (s, faces, overlaps_p)
17708 struct glyph_string *s;
17709 struct face **faces;
17710 int overlaps_p;
17712 int i;
17714 xassert (s);
17716 s->for_overlaps_p = overlaps_p;
17718 s->face = faces[s->gidx];
17719 s->font = s->face->font;
17720 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17722 /* For all glyphs of this composition, starting at the offset
17723 S->gidx, until we reach the end of the definition or encounter a
17724 glyph that requires the different face, add it to S. */
17725 ++s->nchars;
17726 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17727 ++s->nchars;
17729 /* All glyph strings for the same composition has the same width,
17730 i.e. the width set for the first component of the composition. */
17732 s->width = s->first_glyph->pixel_width;
17734 /* If the specified font could not be loaded, use the frame's
17735 default font, but record the fact that we couldn't load it in
17736 the glyph string so that we can draw rectangles for the
17737 characters of the glyph string. */
17738 if (s->font == NULL)
17740 s->font_not_found_p = 1;
17741 s->font = FRAME_FONT (s->f);
17744 /* Adjust base line for subscript/superscript text. */
17745 s->ybase += s->first_glyph->voffset;
17747 xassert (s->face && s->face->gc);
17749 /* This glyph string must always be drawn with 16-bit functions. */
17750 s->two_byte_p = 1;
17752 return s->gidx + s->nchars;
17756 /* Fill glyph string S from a sequence of character glyphs.
17758 FACE_ID is the face id of the string. START is the index of the
17759 first glyph to consider, END is the index of the last + 1.
17760 OVERLAPS_P non-zero means S should draw the foreground only, and
17761 use its physical height for clipping.
17763 Value is the index of the first glyph not in S. */
17765 static int
17766 fill_glyph_string (s, face_id, start, end, overlaps_p)
17767 struct glyph_string *s;
17768 int face_id;
17769 int start, end, overlaps_p;
17771 struct glyph *glyph, *last;
17772 int voffset;
17773 int glyph_not_available_p;
17775 xassert (s->f == XFRAME (s->w->frame));
17776 xassert (s->nchars == 0);
17777 xassert (start >= 0 && end > start);
17779 s->for_overlaps_p = overlaps_p,
17780 glyph = s->row->glyphs[s->area] + start;
17781 last = s->row->glyphs[s->area] + end;
17782 voffset = glyph->voffset;
17784 glyph_not_available_p = glyph->glyph_not_available_p;
17786 while (glyph < last
17787 && glyph->type == CHAR_GLYPH
17788 && glyph->voffset == voffset
17789 /* Same face id implies same font, nowadays. */
17790 && glyph->face_id == face_id
17791 && glyph->glyph_not_available_p == glyph_not_available_p)
17793 int two_byte_p;
17795 s->face = get_glyph_face_and_encoding (s->f, glyph,
17796 s->char2b + s->nchars,
17797 &two_byte_p);
17798 s->two_byte_p = two_byte_p;
17799 ++s->nchars;
17800 xassert (s->nchars <= end - start);
17801 s->width += glyph->pixel_width;
17802 ++glyph;
17805 s->font = s->face->font;
17806 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17808 /* If the specified font could not be loaded, use the frame's font,
17809 but record the fact that we couldn't load it in
17810 S->font_not_found_p so that we can draw rectangles for the
17811 characters of the glyph string. */
17812 if (s->font == NULL || glyph_not_available_p)
17814 s->font_not_found_p = 1;
17815 s->font = FRAME_FONT (s->f);
17818 /* Adjust base line for subscript/superscript text. */
17819 s->ybase += voffset;
17821 xassert (s->face && s->face->gc);
17822 return glyph - s->row->glyphs[s->area];
17826 /* Fill glyph string S from image glyph S->first_glyph. */
17828 static void
17829 fill_image_glyph_string (s)
17830 struct glyph_string *s;
17832 xassert (s->first_glyph->type == IMAGE_GLYPH);
17833 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17834 xassert (s->img);
17835 s->slice = s->first_glyph->slice;
17836 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17837 s->font = s->face->font;
17838 s->width = s->first_glyph->pixel_width;
17840 /* Adjust base line for subscript/superscript text. */
17841 s->ybase += s->first_glyph->voffset;
17845 /* Fill glyph string S from a sequence of stretch glyphs.
17847 ROW is the glyph row in which the glyphs are found, AREA is the
17848 area within the row. START is the index of the first glyph to
17849 consider, END is the index of the last + 1.
17851 Value is the index of the first glyph not in S. */
17853 static int
17854 fill_stretch_glyph_string (s, row, area, start, end)
17855 struct glyph_string *s;
17856 struct glyph_row *row;
17857 enum glyph_row_area area;
17858 int start, end;
17860 struct glyph *glyph, *last;
17861 int voffset, face_id;
17863 xassert (s->first_glyph->type == STRETCH_GLYPH);
17865 glyph = s->row->glyphs[s->area] + start;
17866 last = s->row->glyphs[s->area] + end;
17867 face_id = glyph->face_id;
17868 s->face = FACE_FROM_ID (s->f, face_id);
17869 s->font = s->face->font;
17870 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17871 s->width = glyph->pixel_width;
17872 voffset = glyph->voffset;
17874 for (++glyph;
17875 (glyph < last
17876 && glyph->type == STRETCH_GLYPH
17877 && glyph->voffset == voffset
17878 && glyph->face_id == face_id);
17879 ++glyph)
17880 s->width += glyph->pixel_width;
17882 /* Adjust base line for subscript/superscript text. */
17883 s->ybase += voffset;
17885 /* The case that face->gc == 0 is handled when drawing the glyph
17886 string by calling PREPARE_FACE_FOR_DISPLAY. */
17887 xassert (s->face);
17888 return glyph - s->row->glyphs[s->area];
17892 /* EXPORT for RIF:
17893 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17894 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17895 assumed to be zero. */
17897 void
17898 x_get_glyph_overhangs (glyph, f, left, right)
17899 struct glyph *glyph;
17900 struct frame *f;
17901 int *left, *right;
17903 *left = *right = 0;
17905 if (glyph->type == CHAR_GLYPH)
17907 XFontStruct *font;
17908 struct face *face;
17909 struct font_info *font_info;
17910 XChar2b char2b;
17911 XCharStruct *pcm;
17913 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17914 font = face->font;
17915 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17916 if (font /* ++KFS: Should this be font_info ? */
17917 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17919 if (pcm->rbearing > pcm->width)
17920 *right = pcm->rbearing - pcm->width;
17921 if (pcm->lbearing < 0)
17922 *left = -pcm->lbearing;
17928 /* Return the index of the first glyph preceding glyph string S that
17929 is overwritten by S because of S's left overhang. Value is -1
17930 if no glyphs are overwritten. */
17932 static int
17933 left_overwritten (s)
17934 struct glyph_string *s;
17936 int k;
17938 if (s->left_overhang)
17940 int x = 0, i;
17941 struct glyph *glyphs = s->row->glyphs[s->area];
17942 int first = s->first_glyph - glyphs;
17944 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17945 x -= glyphs[i].pixel_width;
17947 k = i + 1;
17949 else
17950 k = -1;
17952 return k;
17956 /* Return the index of the first glyph preceding glyph string S that
17957 is overwriting S because of its right overhang. Value is -1 if no
17958 glyph in front of S overwrites S. */
17960 static int
17961 left_overwriting (s)
17962 struct glyph_string *s;
17964 int i, k, x;
17965 struct glyph *glyphs = s->row->glyphs[s->area];
17966 int first = s->first_glyph - glyphs;
17968 k = -1;
17969 x = 0;
17970 for (i = first - 1; i >= 0; --i)
17972 int left, right;
17973 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17974 if (x + right > 0)
17975 k = i;
17976 x -= glyphs[i].pixel_width;
17979 return k;
17983 /* Return the index of the last glyph following glyph string S that is
17984 not overwritten by S because of S's right overhang. Value is -1 if
17985 no such glyph is found. */
17987 static int
17988 right_overwritten (s)
17989 struct glyph_string *s;
17991 int k = -1;
17993 if (s->right_overhang)
17995 int x = 0, i;
17996 struct glyph *glyphs = s->row->glyphs[s->area];
17997 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17998 int end = s->row->used[s->area];
18000 for (i = first; i < end && s->right_overhang > x; ++i)
18001 x += glyphs[i].pixel_width;
18003 k = i;
18006 return k;
18010 /* Return the index of the last glyph following glyph string S that
18011 overwrites S because of its left overhang. Value is negative
18012 if no such glyph is found. */
18014 static int
18015 right_overwriting (s)
18016 struct glyph_string *s;
18018 int i, k, x;
18019 int end = s->row->used[s->area];
18020 struct glyph *glyphs = s->row->glyphs[s->area];
18021 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18023 k = -1;
18024 x = 0;
18025 for (i = first; i < end; ++i)
18027 int left, right;
18028 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18029 if (x - left < 0)
18030 k = i;
18031 x += glyphs[i].pixel_width;
18034 return k;
18038 /* Get face and two-byte form of character C in face FACE_ID on frame
18039 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18040 means we want to display multibyte text. DISPLAY_P non-zero means
18041 make sure that X resources for the face returned are allocated.
18042 Value is a pointer to a realized face that is ready for display if
18043 DISPLAY_P is non-zero. */
18045 static INLINE struct face *
18046 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18047 struct frame *f;
18048 int c, face_id;
18049 XChar2b *char2b;
18050 int multibyte_p, display_p;
18052 struct face *face = FACE_FROM_ID (f, face_id);
18054 if (!multibyte_p)
18056 /* Unibyte case. We don't have to encode, but we have to make
18057 sure to use a face suitable for unibyte. */
18058 STORE_XCHAR2B (char2b, 0, c);
18059 face_id = FACE_FOR_CHAR (f, face, c);
18060 face = FACE_FROM_ID (f, face_id);
18062 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18064 /* Case of ASCII in a face known to fit ASCII. */
18065 STORE_XCHAR2B (char2b, 0, c);
18067 else
18069 int c1, c2, charset;
18071 /* Split characters into bytes. If c2 is -1 afterwards, C is
18072 really a one-byte character so that byte1 is zero. */
18073 SPLIT_CHAR (c, charset, c1, c2);
18074 if (c2 > 0)
18075 STORE_XCHAR2B (char2b, c1, c2);
18076 else
18077 STORE_XCHAR2B (char2b, 0, c1);
18079 /* Maybe encode the character in *CHAR2B. */
18080 if (face->font != NULL)
18082 struct font_info *font_info
18083 = FONT_INFO_FROM_ID (f, face->font_info_id);
18084 if (font_info)
18085 rif->encode_char (c, char2b, font_info, 0);
18089 /* Make sure X resources of the face are allocated. */
18090 #ifdef HAVE_X_WINDOWS
18091 if (display_p)
18092 #endif
18094 xassert (face != NULL);
18095 PREPARE_FACE_FOR_DISPLAY (f, face);
18098 return face;
18102 /* Set background width of glyph string S. START is the index of the
18103 first glyph following S. LAST_X is the right-most x-position + 1
18104 in the drawing area. */
18106 static INLINE void
18107 set_glyph_string_background_width (s, start, last_x)
18108 struct glyph_string *s;
18109 int start;
18110 int last_x;
18112 /* If the face of this glyph string has to be drawn to the end of
18113 the drawing area, set S->extends_to_end_of_line_p. */
18114 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18116 if (start == s->row->used[s->area]
18117 && s->area == TEXT_AREA
18118 && ((s->hl == DRAW_NORMAL_TEXT
18119 && (s->row->fill_line_p
18120 || s->face->background != default_face->background
18121 || s->face->stipple != default_face->stipple
18122 || s->row->mouse_face_p))
18123 || s->hl == DRAW_MOUSE_FACE
18124 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18125 && s->row->fill_line_p)))
18126 s->extends_to_end_of_line_p = 1;
18128 /* If S extends its face to the end of the line, set its
18129 background_width to the distance to the right edge of the drawing
18130 area. */
18131 if (s->extends_to_end_of_line_p)
18132 s->background_width = last_x - s->x + 1;
18133 else
18134 s->background_width = s->width;
18138 /* Compute overhangs and x-positions for glyph string S and its
18139 predecessors, or successors. X is the starting x-position for S.
18140 BACKWARD_P non-zero means process predecessors. */
18142 static void
18143 compute_overhangs_and_x (s, x, backward_p)
18144 struct glyph_string *s;
18145 int x;
18146 int backward_p;
18148 if (backward_p)
18150 while (s)
18152 if (rif->compute_glyph_string_overhangs)
18153 rif->compute_glyph_string_overhangs (s);
18154 x -= s->width;
18155 s->x = x;
18156 s = s->prev;
18159 else
18161 while (s)
18163 if (rif->compute_glyph_string_overhangs)
18164 rif->compute_glyph_string_overhangs (s);
18165 s->x = x;
18166 x += s->width;
18167 s = s->next;
18174 /* The following macros are only called from draw_glyphs below.
18175 They reference the following parameters of that function directly:
18176 `w', `row', `area', and `overlap_p'
18177 as well as the following local variables:
18178 `s', `f', and `hdc' (in W32) */
18180 #ifdef HAVE_NTGUI
18181 /* On W32, silently add local `hdc' variable to argument list of
18182 init_glyph_string. */
18183 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18184 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18185 #else
18186 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18187 init_glyph_string (s, char2b, w, row, area, start, hl)
18188 #endif
18190 /* Add a glyph string for a stretch glyph to the list of strings
18191 between HEAD and TAIL. START is the index of the stretch glyph in
18192 row area AREA of glyph row ROW. END is the index of the last glyph
18193 in that glyph row area. X is the current output position assigned
18194 to the new glyph string constructed. HL overrides that face of the
18195 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18196 is the right-most x-position of the drawing area. */
18198 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18199 and below -- keep them on one line. */
18200 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18201 do \
18203 s = (struct glyph_string *) alloca (sizeof *s); \
18204 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18205 START = fill_stretch_glyph_string (s, row, area, START, END); \
18206 append_glyph_string (&HEAD, &TAIL, s); \
18207 s->x = (X); \
18209 while (0)
18212 /* Add a glyph string for an image glyph to the list of strings
18213 between HEAD and TAIL. START is the index of the image glyph in
18214 row area AREA of glyph row ROW. END is the index of the last glyph
18215 in that glyph row area. X is the current output position assigned
18216 to the new glyph string constructed. HL overrides that face of the
18217 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18218 is the right-most x-position of the drawing area. */
18220 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18221 do \
18223 s = (struct glyph_string *) alloca (sizeof *s); \
18224 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18225 fill_image_glyph_string (s); \
18226 append_glyph_string (&HEAD, &TAIL, s); \
18227 ++START; \
18228 s->x = (X); \
18230 while (0)
18233 /* Add a glyph string for a sequence of character glyphs to the list
18234 of strings between HEAD and TAIL. START is the index of the first
18235 glyph in row area AREA of glyph row ROW that is part of the new
18236 glyph string. END is the index of the last glyph in that glyph row
18237 area. X is the current output position assigned to the new glyph
18238 string constructed. HL overrides that face of the glyph; e.g. it
18239 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18240 right-most x-position of the drawing area. */
18242 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18243 do \
18245 int c, face_id; \
18246 XChar2b *char2b; \
18248 c = (row)->glyphs[area][START].u.ch; \
18249 face_id = (row)->glyphs[area][START].face_id; \
18251 s = (struct glyph_string *) alloca (sizeof *s); \
18252 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18253 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18254 append_glyph_string (&HEAD, &TAIL, s); \
18255 s->x = (X); \
18256 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18258 while (0)
18261 /* Add a glyph string for a composite sequence to the list of strings
18262 between HEAD and TAIL. START is the index of the first glyph in
18263 row area AREA of glyph row ROW that is part of the new glyph
18264 string. END is the index of the last glyph in that glyph row area.
18265 X is the current output position assigned to the new glyph string
18266 constructed. HL overrides that face of the glyph; e.g. it is
18267 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18268 x-position of the drawing area. */
18270 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18271 do { \
18272 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18273 int face_id = (row)->glyphs[area][START].face_id; \
18274 struct face *base_face = FACE_FROM_ID (f, face_id); \
18275 struct composition *cmp = composition_table[cmp_id]; \
18276 int glyph_len = cmp->glyph_len; \
18277 XChar2b *char2b; \
18278 struct face **faces; \
18279 struct glyph_string *first_s = NULL; \
18280 int n; \
18282 base_face = base_face->ascii_face; \
18283 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18284 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18285 /* At first, fill in `char2b' and `faces'. */ \
18286 for (n = 0; n < glyph_len; n++) \
18288 int c = COMPOSITION_GLYPH (cmp, n); \
18289 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18290 faces[n] = FACE_FROM_ID (f, this_face_id); \
18291 get_char_face_and_encoding (f, c, this_face_id, \
18292 char2b + n, 1, 1); \
18295 /* Make glyph_strings for each glyph sequence that is drawable by \
18296 the same face, and append them to HEAD/TAIL. */ \
18297 for (n = 0; n < cmp->glyph_len;) \
18299 s = (struct glyph_string *) alloca (sizeof *s); \
18300 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18301 append_glyph_string (&(HEAD), &(TAIL), s); \
18302 s->cmp = cmp; \
18303 s->gidx = n; \
18304 s->x = (X); \
18306 if (n == 0) \
18307 first_s = s; \
18309 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18312 ++START; \
18313 s = first_s; \
18314 } while (0)
18317 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18318 of AREA of glyph row ROW on window W between indices START and END.
18319 HL overrides the face for drawing glyph strings, e.g. it is
18320 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18321 x-positions of the drawing area.
18323 This is an ugly monster macro construct because we must use alloca
18324 to allocate glyph strings (because draw_glyphs can be called
18325 asynchronously). */
18327 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18328 do \
18330 HEAD = TAIL = NULL; \
18331 while (START < END) \
18333 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18334 switch (first_glyph->type) \
18336 case CHAR_GLYPH: \
18337 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18338 HL, X, LAST_X); \
18339 break; \
18341 case COMPOSITE_GLYPH: \
18342 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18343 HL, X, LAST_X); \
18344 break; \
18346 case STRETCH_GLYPH: \
18347 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18348 HL, X, LAST_X); \
18349 break; \
18351 case IMAGE_GLYPH: \
18352 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18353 HL, X, LAST_X); \
18354 break; \
18356 default: \
18357 abort (); \
18360 set_glyph_string_background_width (s, START, LAST_X); \
18361 (X) += s->width; \
18364 while (0)
18367 /* Draw glyphs between START and END in AREA of ROW on window W,
18368 starting at x-position X. X is relative to AREA in W. HL is a
18369 face-override with the following meaning:
18371 DRAW_NORMAL_TEXT draw normally
18372 DRAW_CURSOR draw in cursor face
18373 DRAW_MOUSE_FACE draw in mouse face.
18374 DRAW_INVERSE_VIDEO draw in mode line face
18375 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18376 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18378 If OVERLAPS_P is non-zero, draw only the foreground of characters
18379 and clip to the physical height of ROW.
18381 Value is the x-position reached, relative to AREA of W. */
18383 static int
18384 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18385 struct window *w;
18386 int x;
18387 struct glyph_row *row;
18388 enum glyph_row_area area;
18389 int start, end;
18390 enum draw_glyphs_face hl;
18391 int overlaps_p;
18393 struct glyph_string *head, *tail;
18394 struct glyph_string *s;
18395 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18396 int last_x, area_width;
18397 int x_reached;
18398 int i, j;
18399 struct frame *f = XFRAME (WINDOW_FRAME (w));
18400 DECLARE_HDC (hdc);
18402 ALLOCATE_HDC (hdc, f);
18404 /* Let's rather be paranoid than getting a SEGV. */
18405 end = min (end, row->used[area]);
18406 start = max (0, start);
18407 start = min (end, start);
18409 /* Translate X to frame coordinates. Set last_x to the right
18410 end of the drawing area. */
18411 if (row->full_width_p)
18413 /* X is relative to the left edge of W, without scroll bars
18414 or fringes. */
18415 x += WINDOW_LEFT_EDGE_X (w);
18416 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18418 else
18420 int area_left = window_box_left (w, area);
18421 x += area_left;
18422 area_width = window_box_width (w, area);
18423 last_x = area_left + area_width;
18426 /* Build a doubly-linked list of glyph_string structures between
18427 head and tail from what we have to draw. Note that the macro
18428 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18429 the reason we use a separate variable `i'. */
18430 i = start;
18431 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18432 if (tail)
18433 x_reached = tail->x + tail->background_width;
18434 else
18435 x_reached = x;
18437 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18438 the row, redraw some glyphs in front or following the glyph
18439 strings built above. */
18440 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18442 int dummy_x = 0;
18443 struct glyph_string *h, *t;
18445 /* Compute overhangs for all glyph strings. */
18446 if (rif->compute_glyph_string_overhangs)
18447 for (s = head; s; s = s->next)
18448 rif->compute_glyph_string_overhangs (s);
18450 /* Prepend glyph strings for glyphs in front of the first glyph
18451 string that are overwritten because of the first glyph
18452 string's left overhang. The background of all strings
18453 prepended must be drawn because the first glyph string
18454 draws over it. */
18455 i = left_overwritten (head);
18456 if (i >= 0)
18458 j = i;
18459 BUILD_GLYPH_STRINGS (j, start, h, t,
18460 DRAW_NORMAL_TEXT, dummy_x, last_x);
18461 start = i;
18462 compute_overhangs_and_x (t, head->x, 1);
18463 prepend_glyph_string_lists (&head, &tail, h, t);
18464 clip_head = head;
18467 /* Prepend glyph strings for glyphs in front of the first glyph
18468 string that overwrite that glyph string because of their
18469 right overhang. For these strings, only the foreground must
18470 be drawn, because it draws over the glyph string at `head'.
18471 The background must not be drawn because this would overwrite
18472 right overhangs of preceding glyphs for which no glyph
18473 strings exist. */
18474 i = left_overwriting (head);
18475 if (i >= 0)
18477 clip_head = head;
18478 BUILD_GLYPH_STRINGS (i, start, h, t,
18479 DRAW_NORMAL_TEXT, dummy_x, last_x);
18480 for (s = h; s; s = s->next)
18481 s->background_filled_p = 1;
18482 compute_overhangs_and_x (t, head->x, 1);
18483 prepend_glyph_string_lists (&head, &tail, h, t);
18486 /* Append glyphs strings for glyphs following the last glyph
18487 string tail that are overwritten by tail. The background of
18488 these strings has to be drawn because tail's foreground draws
18489 over it. */
18490 i = right_overwritten (tail);
18491 if (i >= 0)
18493 BUILD_GLYPH_STRINGS (end, i, h, t,
18494 DRAW_NORMAL_TEXT, x, last_x);
18495 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18496 append_glyph_string_lists (&head, &tail, h, t);
18497 clip_tail = tail;
18500 /* Append glyph strings for glyphs following the last glyph
18501 string tail that overwrite tail. The foreground of such
18502 glyphs has to be drawn because it writes into the background
18503 of tail. The background must not be drawn because it could
18504 paint over the foreground of following glyphs. */
18505 i = right_overwriting (tail);
18506 if (i >= 0)
18508 clip_tail = tail;
18509 BUILD_GLYPH_STRINGS (end, i, h, t,
18510 DRAW_NORMAL_TEXT, x, last_x);
18511 for (s = h; s; s = s->next)
18512 s->background_filled_p = 1;
18513 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18514 append_glyph_string_lists (&head, &tail, h, t);
18516 if (clip_head || clip_tail)
18517 for (s = head; s; s = s->next)
18519 s->clip_head = clip_head;
18520 s->clip_tail = clip_tail;
18524 /* Draw all strings. */
18525 for (s = head; s; s = s->next)
18526 rif->draw_glyph_string (s);
18528 if (area == TEXT_AREA
18529 && !row->full_width_p
18530 /* When drawing overlapping rows, only the glyph strings'
18531 foreground is drawn, which doesn't erase a cursor
18532 completely. */
18533 && !overlaps_p)
18535 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18536 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18537 : (tail ? tail->x + tail->background_width : x));
18539 int text_left = window_box_left (w, TEXT_AREA);
18540 x0 -= text_left;
18541 x1 -= text_left;
18543 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18544 row->y, MATRIX_ROW_BOTTOM_Y (row));
18547 /* Value is the x-position up to which drawn, relative to AREA of W.
18548 This doesn't include parts drawn because of overhangs. */
18549 if (row->full_width_p)
18550 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18551 else
18552 x_reached -= window_box_left (w, area);
18554 RELEASE_HDC (hdc, f);
18556 return x_reached;
18559 /* Expand row matrix if too narrow. Don't expand if area
18560 is not present. */
18562 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18564 if (!fonts_changed_p \
18565 && (it->glyph_row->glyphs[area] \
18566 < it->glyph_row->glyphs[area + 1])) \
18568 it->w->ncols_scale_factor++; \
18569 fonts_changed_p = 1; \
18573 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18574 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18576 static INLINE void
18577 append_glyph (it)
18578 struct it *it;
18580 struct glyph *glyph;
18581 enum glyph_row_area area = it->area;
18583 xassert (it->glyph_row);
18584 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18586 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18587 if (glyph < it->glyph_row->glyphs[area + 1])
18589 glyph->charpos = CHARPOS (it->position);
18590 glyph->object = it->object;
18591 glyph->pixel_width = it->pixel_width;
18592 glyph->ascent = it->ascent;
18593 glyph->descent = it->descent;
18594 glyph->voffset = it->voffset;
18595 glyph->type = CHAR_GLYPH;
18596 glyph->multibyte_p = it->multibyte_p;
18597 glyph->left_box_line_p = it->start_of_box_run_p;
18598 glyph->right_box_line_p = it->end_of_box_run_p;
18599 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18600 || it->phys_descent > it->descent);
18601 glyph->padding_p = 0;
18602 glyph->glyph_not_available_p = it->glyph_not_available_p;
18603 glyph->face_id = it->face_id;
18604 glyph->u.ch = it->char_to_display;
18605 glyph->slice = null_glyph_slice;
18606 glyph->font_type = FONT_TYPE_UNKNOWN;
18607 ++it->glyph_row->used[area];
18609 else
18610 IT_EXPAND_MATRIX_WIDTH (it, area);
18613 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18614 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18616 static INLINE void
18617 append_composite_glyph (it)
18618 struct it *it;
18620 struct glyph *glyph;
18621 enum glyph_row_area area = it->area;
18623 xassert (it->glyph_row);
18625 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18626 if (glyph < it->glyph_row->glyphs[area + 1])
18628 glyph->charpos = CHARPOS (it->position);
18629 glyph->object = it->object;
18630 glyph->pixel_width = it->pixel_width;
18631 glyph->ascent = it->ascent;
18632 glyph->descent = it->descent;
18633 glyph->voffset = it->voffset;
18634 glyph->type = COMPOSITE_GLYPH;
18635 glyph->multibyte_p = it->multibyte_p;
18636 glyph->left_box_line_p = it->start_of_box_run_p;
18637 glyph->right_box_line_p = it->end_of_box_run_p;
18638 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18639 || it->phys_descent > it->descent);
18640 glyph->padding_p = 0;
18641 glyph->glyph_not_available_p = 0;
18642 glyph->face_id = it->face_id;
18643 glyph->u.cmp_id = it->cmp_id;
18644 glyph->slice = null_glyph_slice;
18645 glyph->font_type = FONT_TYPE_UNKNOWN;
18646 ++it->glyph_row->used[area];
18648 else
18649 IT_EXPAND_MATRIX_WIDTH (it, area);
18653 /* Change IT->ascent and IT->height according to the setting of
18654 IT->voffset. */
18656 static INLINE void
18657 take_vertical_position_into_account (it)
18658 struct it *it;
18660 if (it->voffset)
18662 if (it->voffset < 0)
18663 /* Increase the ascent so that we can display the text higher
18664 in the line. */
18665 it->ascent -= it->voffset;
18666 else
18667 /* Increase the descent so that we can display the text lower
18668 in the line. */
18669 it->descent += it->voffset;
18674 /* Produce glyphs/get display metrics for the image IT is loaded with.
18675 See the description of struct display_iterator in dispextern.h for
18676 an overview of struct display_iterator. */
18678 static void
18679 produce_image_glyph (it)
18680 struct it *it;
18682 struct image *img;
18683 struct face *face;
18684 int glyph_ascent;
18685 struct glyph_slice slice;
18687 xassert (it->what == IT_IMAGE);
18689 face = FACE_FROM_ID (it->f, it->face_id);
18690 xassert (face);
18691 /* Make sure X resources of the face is loaded. */
18692 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18694 if (it->image_id < 0)
18696 /* Fringe bitmap. */
18697 it->ascent = it->phys_ascent = 0;
18698 it->descent = it->phys_descent = 0;
18699 it->pixel_width = 0;
18700 it->nglyphs = 0;
18701 return;
18704 img = IMAGE_FROM_ID (it->f, it->image_id);
18705 xassert (img);
18706 /* Make sure X resources of the image is loaded. */
18707 prepare_image_for_display (it->f, img);
18709 slice.x = slice.y = 0;
18710 slice.width = img->width;
18711 slice.height = img->height;
18713 if (INTEGERP (it->slice.x))
18714 slice.x = XINT (it->slice.x);
18715 else if (FLOATP (it->slice.x))
18716 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18718 if (INTEGERP (it->slice.y))
18719 slice.y = XINT (it->slice.y);
18720 else if (FLOATP (it->slice.y))
18721 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18723 if (INTEGERP (it->slice.width))
18724 slice.width = XINT (it->slice.width);
18725 else if (FLOATP (it->slice.width))
18726 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18728 if (INTEGERP (it->slice.height))
18729 slice.height = XINT (it->slice.height);
18730 else if (FLOATP (it->slice.height))
18731 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18733 if (slice.x >= img->width)
18734 slice.x = img->width;
18735 if (slice.y >= img->height)
18736 slice.y = img->height;
18737 if (slice.x + slice.width >= img->width)
18738 slice.width = img->width - slice.x;
18739 if (slice.y + slice.height > img->height)
18740 slice.height = img->height - slice.y;
18742 if (slice.width == 0 || slice.height == 0)
18743 return;
18745 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18747 it->descent = slice.height - glyph_ascent;
18748 if (slice.y == 0)
18749 it->descent += img->vmargin;
18750 if (slice.y + slice.height == img->height)
18751 it->descent += img->vmargin;
18752 it->phys_descent = it->descent;
18754 it->pixel_width = slice.width;
18755 if (slice.x == 0)
18756 it->pixel_width += img->hmargin;
18757 if (slice.x + slice.width == img->width)
18758 it->pixel_width += img->hmargin;
18760 /* It's quite possible for images to have an ascent greater than
18761 their height, so don't get confused in that case. */
18762 if (it->descent < 0)
18763 it->descent = 0;
18765 #if 0 /* this breaks image tiling */
18766 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18767 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18768 if (face_ascent > it->ascent)
18769 it->ascent = it->phys_ascent = face_ascent;
18770 #endif
18772 it->nglyphs = 1;
18774 if (face->box != FACE_NO_BOX)
18776 if (face->box_line_width > 0)
18778 if (slice.y == 0)
18779 it->ascent += face->box_line_width;
18780 if (slice.y + slice.height == img->height)
18781 it->descent += face->box_line_width;
18784 if (it->start_of_box_run_p && slice.x == 0)
18785 it->pixel_width += abs (face->box_line_width);
18786 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18787 it->pixel_width += abs (face->box_line_width);
18790 take_vertical_position_into_account (it);
18792 if (it->glyph_row)
18794 struct glyph *glyph;
18795 enum glyph_row_area area = it->area;
18797 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18798 if (glyph < it->glyph_row->glyphs[area + 1])
18800 glyph->charpos = CHARPOS (it->position);
18801 glyph->object = it->object;
18802 glyph->pixel_width = it->pixel_width;
18803 glyph->ascent = glyph_ascent;
18804 glyph->descent = it->descent;
18805 glyph->voffset = it->voffset;
18806 glyph->type = IMAGE_GLYPH;
18807 glyph->multibyte_p = it->multibyte_p;
18808 glyph->left_box_line_p = it->start_of_box_run_p;
18809 glyph->right_box_line_p = it->end_of_box_run_p;
18810 glyph->overlaps_vertically_p = 0;
18811 glyph->padding_p = 0;
18812 glyph->glyph_not_available_p = 0;
18813 glyph->face_id = it->face_id;
18814 glyph->u.img_id = img->id;
18815 glyph->slice = slice;
18816 glyph->font_type = FONT_TYPE_UNKNOWN;
18817 ++it->glyph_row->used[area];
18819 else
18820 IT_EXPAND_MATRIX_WIDTH (it, area);
18825 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18826 of the glyph, WIDTH and HEIGHT are the width and height of the
18827 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18829 static void
18830 append_stretch_glyph (it, object, width, height, ascent)
18831 struct it *it;
18832 Lisp_Object object;
18833 int width, height;
18834 int ascent;
18836 struct glyph *glyph;
18837 enum glyph_row_area area = it->area;
18839 xassert (ascent >= 0 && ascent <= height);
18841 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18842 if (glyph < it->glyph_row->glyphs[area + 1])
18844 glyph->charpos = CHARPOS (it->position);
18845 glyph->object = object;
18846 glyph->pixel_width = width;
18847 glyph->ascent = ascent;
18848 glyph->descent = height - ascent;
18849 glyph->voffset = it->voffset;
18850 glyph->type = STRETCH_GLYPH;
18851 glyph->multibyte_p = it->multibyte_p;
18852 glyph->left_box_line_p = it->start_of_box_run_p;
18853 glyph->right_box_line_p = it->end_of_box_run_p;
18854 glyph->overlaps_vertically_p = 0;
18855 glyph->padding_p = 0;
18856 glyph->glyph_not_available_p = 0;
18857 glyph->face_id = it->face_id;
18858 glyph->u.stretch.ascent = ascent;
18859 glyph->u.stretch.height = height;
18860 glyph->slice = null_glyph_slice;
18861 glyph->font_type = FONT_TYPE_UNKNOWN;
18862 ++it->glyph_row->used[area];
18864 else
18865 IT_EXPAND_MATRIX_WIDTH (it, area);
18869 /* Produce a stretch glyph for iterator IT. IT->object is the value
18870 of the glyph property displayed. The value must be a list
18871 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18872 being recognized:
18874 1. `:width WIDTH' specifies that the space should be WIDTH *
18875 canonical char width wide. WIDTH may be an integer or floating
18876 point number.
18878 2. `:relative-width FACTOR' specifies that the width of the stretch
18879 should be computed from the width of the first character having the
18880 `glyph' property, and should be FACTOR times that width.
18882 3. `:align-to HPOS' specifies that the space should be wide enough
18883 to reach HPOS, a value in canonical character units.
18885 Exactly one of the above pairs must be present.
18887 4. `:height HEIGHT' specifies that the height of the stretch produced
18888 should be HEIGHT, measured in canonical character units.
18890 5. `:relative-height FACTOR' specifies that the height of the
18891 stretch should be FACTOR times the height of the characters having
18892 the glyph property.
18894 Either none or exactly one of 4 or 5 must be present.
18896 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18897 of the stretch should be used for the ascent of the stretch.
18898 ASCENT must be in the range 0 <= ASCENT <= 100. */
18900 static void
18901 produce_stretch_glyph (it)
18902 struct it *it;
18904 /* (space :width WIDTH :height HEIGHT ...) */
18905 Lisp_Object prop, plist;
18906 int width = 0, height = 0, align_to = -1;
18907 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18908 int ascent = 0;
18909 double tem;
18910 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18911 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18913 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18915 /* List should start with `space'. */
18916 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18917 plist = XCDR (it->object);
18919 /* Compute the width of the stretch. */
18920 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18921 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18923 /* Absolute width `:width WIDTH' specified and valid. */
18924 zero_width_ok_p = 1;
18925 width = (int)tem;
18927 else if (prop = Fplist_get (plist, QCrelative_width),
18928 NUMVAL (prop) > 0)
18930 /* Relative width `:relative-width FACTOR' specified and valid.
18931 Compute the width of the characters having the `glyph'
18932 property. */
18933 struct it it2;
18934 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18936 it2 = *it;
18937 if (it->multibyte_p)
18939 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18940 - IT_BYTEPOS (*it));
18941 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18943 else
18944 it2.c = *p, it2.len = 1;
18946 it2.glyph_row = NULL;
18947 it2.what = IT_CHARACTER;
18948 x_produce_glyphs (&it2);
18949 width = NUMVAL (prop) * it2.pixel_width;
18951 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18952 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18954 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18955 align_to = (align_to < 0
18957 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18958 else if (align_to < 0)
18959 align_to = window_box_left_offset (it->w, TEXT_AREA);
18960 width = max (0, (int)tem + align_to - it->current_x);
18961 zero_width_ok_p = 1;
18963 else
18964 /* Nothing specified -> width defaults to canonical char width. */
18965 width = FRAME_COLUMN_WIDTH (it->f);
18967 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18968 width = 1;
18970 /* Compute height. */
18971 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18972 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18974 height = (int)tem;
18975 zero_height_ok_p = 1;
18977 else if (prop = Fplist_get (plist, QCrelative_height),
18978 NUMVAL (prop) > 0)
18979 height = FONT_HEIGHT (font) * NUMVAL (prop);
18980 else
18981 height = FONT_HEIGHT (font);
18983 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18984 height = 1;
18986 /* Compute percentage of height used for ascent. If
18987 `:ascent ASCENT' is present and valid, use that. Otherwise,
18988 derive the ascent from the font in use. */
18989 if (prop = Fplist_get (plist, QCascent),
18990 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18991 ascent = height * NUMVAL (prop) / 100.0;
18992 else if (!NILP (prop)
18993 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18994 ascent = min (max (0, (int)tem), height);
18995 else
18996 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18998 if (width > 0 && height > 0 && it->glyph_row)
19000 Lisp_Object object = it->stack[it->sp - 1].string;
19001 if (!STRINGP (object))
19002 object = it->w->buffer;
19003 append_stretch_glyph (it, object, width, height, ascent);
19006 it->pixel_width = width;
19007 it->ascent = it->phys_ascent = ascent;
19008 it->descent = it->phys_descent = height - it->ascent;
19009 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19011 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19013 if (face->box_line_width > 0)
19015 it->ascent += face->box_line_width;
19016 it->descent += face->box_line_width;
19019 if (it->start_of_box_run_p)
19020 it->pixel_width += abs (face->box_line_width);
19021 if (it->end_of_box_run_p)
19022 it->pixel_width += abs (face->box_line_width);
19025 take_vertical_position_into_account (it);
19028 /* Get line-height and line-spacing property at point.
19029 If line-height has format (HEIGHT TOTAL), return TOTAL
19030 in TOTAL_HEIGHT. */
19032 static Lisp_Object
19033 get_line_height_property (it, prop)
19034 struct it *it;
19035 Lisp_Object prop;
19037 Lisp_Object position, val;
19039 if (STRINGP (it->object))
19040 position = make_number (IT_STRING_CHARPOS (*it));
19041 else if (BUFFERP (it->object))
19042 position = make_number (IT_CHARPOS (*it));
19043 else
19044 return Qnil;
19046 return Fget_char_property (position, prop, it->object);
19049 /* Calculate line-height and line-spacing properties.
19050 An integer value specifies explicit pixel value.
19051 A float value specifies relative value to current face height.
19052 A cons (float . face-name) specifies relative value to
19053 height of specified face font.
19055 Returns height in pixels, or nil. */
19058 static Lisp_Object
19059 calc_line_height_property (it, val, font, boff, override)
19060 struct it *it;
19061 Lisp_Object val;
19062 XFontStruct *font;
19063 int boff, override;
19065 Lisp_Object face_name = Qnil;
19066 int ascent, descent, height;
19068 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19069 return val;
19071 if (CONSP (val))
19073 face_name = XCAR (val);
19074 val = XCDR (val);
19075 if (!NUMBERP (val))
19076 val = make_number (1);
19077 if (NILP (face_name))
19079 height = it->ascent + it->descent;
19080 goto scale;
19084 if (NILP (face_name))
19086 font = FRAME_FONT (it->f);
19087 boff = FRAME_BASELINE_OFFSET (it->f);
19089 else if (EQ (face_name, Qt))
19091 override = 0;
19093 else
19095 int face_id;
19096 struct face *face;
19097 struct font_info *font_info;
19099 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19100 if (face_id < 0)
19101 return make_number (-1);
19103 face = FACE_FROM_ID (it->f, face_id);
19104 font = face->font;
19105 if (font == NULL)
19106 return make_number (-1);
19108 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19109 boff = font_info->baseline_offset;
19110 if (font_info->vertical_centering)
19111 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19114 ascent = FONT_BASE (font) + boff;
19115 descent = FONT_DESCENT (font) - boff;
19117 if (override)
19119 it->override_ascent = ascent;
19120 it->override_descent = descent;
19121 it->override_boff = boff;
19124 height = ascent + descent;
19126 scale:
19127 if (FLOATP (val))
19128 height = (int)(XFLOAT_DATA (val) * height);
19129 else if (INTEGERP (val))
19130 height *= XINT (val);
19132 return make_number (height);
19136 /* RIF:
19137 Produce glyphs/get display metrics for the display element IT is
19138 loaded with. See the description of struct display_iterator in
19139 dispextern.h for an overview of struct display_iterator. */
19141 void
19142 x_produce_glyphs (it)
19143 struct it *it;
19145 int extra_line_spacing = it->extra_line_spacing;
19147 it->glyph_not_available_p = 0;
19149 if (it->what == IT_CHARACTER)
19151 XChar2b char2b;
19152 XFontStruct *font;
19153 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19154 XCharStruct *pcm;
19155 int font_not_found_p;
19156 struct font_info *font_info;
19157 int boff; /* baseline offset */
19158 /* We may change it->multibyte_p upon unibyte<->multibyte
19159 conversion. So, save the current value now and restore it
19160 later.
19162 Note: It seems that we don't have to record multibyte_p in
19163 struct glyph because the character code itself tells if or
19164 not the character is multibyte. Thus, in the future, we must
19165 consider eliminating the field `multibyte_p' in the struct
19166 glyph. */
19167 int saved_multibyte_p = it->multibyte_p;
19169 /* Maybe translate single-byte characters to multibyte, or the
19170 other way. */
19171 it->char_to_display = it->c;
19172 if (!ASCII_BYTE_P (it->c))
19174 if (unibyte_display_via_language_environment
19175 && SINGLE_BYTE_CHAR_P (it->c)
19176 && (it->c >= 0240
19177 || !NILP (Vnonascii_translation_table)))
19179 it->char_to_display = unibyte_char_to_multibyte (it->c);
19180 it->multibyte_p = 1;
19181 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19182 face = FACE_FROM_ID (it->f, it->face_id);
19184 else if (!SINGLE_BYTE_CHAR_P (it->c)
19185 && !it->multibyte_p)
19187 it->multibyte_p = 1;
19188 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19189 face = FACE_FROM_ID (it->f, it->face_id);
19193 /* Get font to use. Encode IT->char_to_display. */
19194 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19195 &char2b, it->multibyte_p, 0);
19196 font = face->font;
19198 /* When no suitable font found, use the default font. */
19199 font_not_found_p = font == NULL;
19200 if (font_not_found_p)
19202 font = FRAME_FONT (it->f);
19203 boff = FRAME_BASELINE_OFFSET (it->f);
19204 font_info = NULL;
19206 else
19208 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19209 boff = font_info->baseline_offset;
19210 if (font_info->vertical_centering)
19211 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19214 if (it->char_to_display >= ' '
19215 && (!it->multibyte_p || it->char_to_display < 128))
19217 /* Either unibyte or ASCII. */
19218 int stretched_p;
19220 it->nglyphs = 1;
19222 pcm = rif->per_char_metric (font, &char2b,
19223 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19225 if (it->override_ascent >= 0)
19227 it->ascent = it->override_ascent;
19228 it->descent = it->override_descent;
19229 boff = it->override_boff;
19231 else
19233 it->ascent = FONT_BASE (font) + boff;
19234 it->descent = FONT_DESCENT (font) - boff;
19237 if (pcm)
19239 it->phys_ascent = pcm->ascent + boff;
19240 it->phys_descent = pcm->descent - boff;
19241 it->pixel_width = pcm->width;
19243 else
19245 it->glyph_not_available_p = 1;
19246 it->phys_ascent = it->ascent;
19247 it->phys_descent = it->descent;
19248 it->pixel_width = FONT_WIDTH (font);
19251 if (it->constrain_row_ascent_descent_p)
19253 if (it->descent > it->max_descent)
19255 it->ascent += it->descent - it->max_descent;
19256 it->descent = it->max_descent;
19258 if (it->ascent > it->max_ascent)
19260 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19261 it->ascent = it->max_ascent;
19263 it->phys_ascent = min (it->phys_ascent, it->ascent);
19264 it->phys_descent = min (it->phys_descent, it->descent);
19265 extra_line_spacing = 0;
19268 /* If this is a space inside a region of text with
19269 `space-width' property, change its width. */
19270 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19271 if (stretched_p)
19272 it->pixel_width *= XFLOATINT (it->space_width);
19274 /* If face has a box, add the box thickness to the character
19275 height. If character has a box line to the left and/or
19276 right, add the box line width to the character's width. */
19277 if (face->box != FACE_NO_BOX)
19279 int thick = face->box_line_width;
19281 if (thick > 0)
19283 it->ascent += thick;
19284 it->descent += thick;
19286 else
19287 thick = -thick;
19289 if (it->start_of_box_run_p)
19290 it->pixel_width += thick;
19291 if (it->end_of_box_run_p)
19292 it->pixel_width += thick;
19295 /* If face has an overline, add the height of the overline
19296 (1 pixel) and a 1 pixel margin to the character height. */
19297 if (face->overline_p)
19298 it->ascent += 2;
19300 if (it->constrain_row_ascent_descent_p)
19302 if (it->ascent > it->max_ascent)
19303 it->ascent = it->max_ascent;
19304 if (it->descent > it->max_descent)
19305 it->descent = it->max_descent;
19308 take_vertical_position_into_account (it);
19310 /* If we have to actually produce glyphs, do it. */
19311 if (it->glyph_row)
19313 if (stretched_p)
19315 /* Translate a space with a `space-width' property
19316 into a stretch glyph. */
19317 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19318 / FONT_HEIGHT (font));
19319 append_stretch_glyph (it, it->object, it->pixel_width,
19320 it->ascent + it->descent, ascent);
19322 else
19323 append_glyph (it);
19325 /* If characters with lbearing or rbearing are displayed
19326 in this line, record that fact in a flag of the
19327 glyph row. This is used to optimize X output code. */
19328 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19329 it->glyph_row->contains_overlapping_glyphs_p = 1;
19332 else if (it->char_to_display == '\n')
19334 /* A newline has no width but we need the height of the line.
19335 But if previous part of the line set a height, don't
19336 increase that height */
19338 Lisp_Object height;
19339 Lisp_Object total_height = Qnil;
19341 it->override_ascent = -1;
19342 it->pixel_width = 0;
19343 it->nglyphs = 0;
19345 height = get_line_height_property(it, Qline_height);
19346 /* Split (line-height total-height) list */
19347 if (CONSP (height)
19348 && CONSP (XCDR (height))
19349 && NILP (XCDR (XCDR (height))))
19351 total_height = XCAR (XCDR (height));
19352 height = XCAR (height);
19354 height = calc_line_height_property(it, height, font, boff, 1);
19356 if (it->override_ascent >= 0)
19358 it->ascent = it->override_ascent;
19359 it->descent = it->override_descent;
19360 boff = it->override_boff;
19362 else
19364 it->ascent = FONT_BASE (font) + boff;
19365 it->descent = FONT_DESCENT (font) - boff;
19368 if (EQ (height, Qt))
19370 if (it->descent > it->max_descent)
19372 it->ascent += it->descent - it->max_descent;
19373 it->descent = it->max_descent;
19375 if (it->ascent > it->max_ascent)
19377 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19378 it->ascent = it->max_ascent;
19380 it->phys_ascent = min (it->phys_ascent, it->ascent);
19381 it->phys_descent = min (it->phys_descent, it->descent);
19382 it->constrain_row_ascent_descent_p = 1;
19383 extra_line_spacing = 0;
19385 else
19387 Lisp_Object spacing;
19388 int total = 0;
19390 it->phys_ascent = it->ascent;
19391 it->phys_descent = it->descent;
19393 if ((it->max_ascent > 0 || it->max_descent > 0)
19394 && face->box != FACE_NO_BOX
19395 && face->box_line_width > 0)
19397 it->ascent += face->box_line_width;
19398 it->descent += face->box_line_width;
19400 if (!NILP (height)
19401 && XINT (height) > it->ascent + it->descent)
19402 it->ascent = XINT (height) - it->descent;
19404 if (!NILP (total_height))
19405 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19406 else
19408 spacing = get_line_height_property(it, Qline_spacing);
19409 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19411 if (INTEGERP (spacing))
19413 extra_line_spacing = XINT (spacing);
19414 if (!NILP (total_height))
19415 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19419 else if (it->char_to_display == '\t')
19421 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19422 int x = it->current_x + it->continuation_lines_width;
19423 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19425 /* If the distance from the current position to the next tab
19426 stop is less than a space character width, use the
19427 tab stop after that. */
19428 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19429 next_tab_x += tab_width;
19431 it->pixel_width = next_tab_x - x;
19432 it->nglyphs = 1;
19433 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19434 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19436 if (it->glyph_row)
19438 append_stretch_glyph (it, it->object, it->pixel_width,
19439 it->ascent + it->descent, it->ascent);
19442 else
19444 /* A multi-byte character. Assume that the display width of the
19445 character is the width of the character multiplied by the
19446 width of the font. */
19448 /* If we found a font, this font should give us the right
19449 metrics. If we didn't find a font, use the frame's
19450 default font and calculate the width of the character
19451 from the charset width; this is what old redisplay code
19452 did. */
19454 pcm = rif->per_char_metric (font, &char2b,
19455 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19457 if (font_not_found_p || !pcm)
19459 int charset = CHAR_CHARSET (it->char_to_display);
19461 it->glyph_not_available_p = 1;
19462 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19463 * CHARSET_WIDTH (charset));
19464 it->phys_ascent = FONT_BASE (font) + boff;
19465 it->phys_descent = FONT_DESCENT (font) - boff;
19467 else
19469 it->pixel_width = pcm->width;
19470 it->phys_ascent = pcm->ascent + boff;
19471 it->phys_descent = pcm->descent - boff;
19472 if (it->glyph_row
19473 && (pcm->lbearing < 0
19474 || pcm->rbearing > pcm->width))
19475 it->glyph_row->contains_overlapping_glyphs_p = 1;
19477 it->nglyphs = 1;
19478 it->ascent = FONT_BASE (font) + boff;
19479 it->descent = FONT_DESCENT (font) - boff;
19480 if (face->box != FACE_NO_BOX)
19482 int thick = face->box_line_width;
19484 if (thick > 0)
19486 it->ascent += thick;
19487 it->descent += thick;
19489 else
19490 thick = - thick;
19492 if (it->start_of_box_run_p)
19493 it->pixel_width += thick;
19494 if (it->end_of_box_run_p)
19495 it->pixel_width += thick;
19498 /* If face has an overline, add the height of the overline
19499 (1 pixel) and a 1 pixel margin to the character height. */
19500 if (face->overline_p)
19501 it->ascent += 2;
19503 take_vertical_position_into_account (it);
19505 if (it->glyph_row)
19506 append_glyph (it);
19508 it->multibyte_p = saved_multibyte_p;
19510 else if (it->what == IT_COMPOSITION)
19512 /* Note: A composition is represented as one glyph in the
19513 glyph matrix. There are no padding glyphs. */
19514 XChar2b char2b;
19515 XFontStruct *font;
19516 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19517 XCharStruct *pcm;
19518 int font_not_found_p;
19519 struct font_info *font_info;
19520 int boff; /* baseline offset */
19521 struct composition *cmp = composition_table[it->cmp_id];
19523 /* Maybe translate single-byte characters to multibyte. */
19524 it->char_to_display = it->c;
19525 if (unibyte_display_via_language_environment
19526 && SINGLE_BYTE_CHAR_P (it->c)
19527 && (it->c >= 0240
19528 || (it->c >= 0200
19529 && !NILP (Vnonascii_translation_table))))
19531 it->char_to_display = unibyte_char_to_multibyte (it->c);
19534 /* Get face and font to use. Encode IT->char_to_display. */
19535 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19536 face = FACE_FROM_ID (it->f, it->face_id);
19537 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19538 &char2b, it->multibyte_p, 0);
19539 font = face->font;
19541 /* When no suitable font found, use the default font. */
19542 font_not_found_p = font == NULL;
19543 if (font_not_found_p)
19545 font = FRAME_FONT (it->f);
19546 boff = FRAME_BASELINE_OFFSET (it->f);
19547 font_info = NULL;
19549 else
19551 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19552 boff = font_info->baseline_offset;
19553 if (font_info->vertical_centering)
19554 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19557 /* There are no padding glyphs, so there is only one glyph to
19558 produce for the composition. Important is that pixel_width,
19559 ascent and descent are the values of what is drawn by
19560 draw_glyphs (i.e. the values of the overall glyphs composed). */
19561 it->nglyphs = 1;
19563 /* If we have not yet calculated pixel size data of glyphs of
19564 the composition for the current face font, calculate them
19565 now. Theoretically, we have to check all fonts for the
19566 glyphs, but that requires much time and memory space. So,
19567 here we check only the font of the first glyph. This leads
19568 to incorrect display very rarely, and C-l (recenter) can
19569 correct the display anyway. */
19570 if (cmp->font != (void *) font)
19572 /* Ascent and descent of the font of the first character of
19573 this composition (adjusted by baseline offset). Ascent
19574 and descent of overall glyphs should not be less than
19575 them respectively. */
19576 int font_ascent = FONT_BASE (font) + boff;
19577 int font_descent = FONT_DESCENT (font) - boff;
19578 /* Bounding box of the overall glyphs. */
19579 int leftmost, rightmost, lowest, highest;
19580 int i, width, ascent, descent;
19582 cmp->font = (void *) font;
19584 /* Initialize the bounding box. */
19585 if (font_info
19586 && (pcm = rif->per_char_metric (font, &char2b,
19587 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19589 width = pcm->width;
19590 ascent = pcm->ascent;
19591 descent = pcm->descent;
19593 else
19595 width = FONT_WIDTH (font);
19596 ascent = FONT_BASE (font);
19597 descent = FONT_DESCENT (font);
19600 rightmost = width;
19601 lowest = - descent + boff;
19602 highest = ascent + boff;
19603 leftmost = 0;
19605 if (font_info
19606 && font_info->default_ascent
19607 && CHAR_TABLE_P (Vuse_default_ascent)
19608 && !NILP (Faref (Vuse_default_ascent,
19609 make_number (it->char_to_display))))
19610 highest = font_info->default_ascent + boff;
19612 /* Draw the first glyph at the normal position. It may be
19613 shifted to right later if some other glyphs are drawn at
19614 the left. */
19615 cmp->offsets[0] = 0;
19616 cmp->offsets[1] = boff;
19618 /* Set cmp->offsets for the remaining glyphs. */
19619 for (i = 1; i < cmp->glyph_len; i++)
19621 int left, right, btm, top;
19622 int ch = COMPOSITION_GLYPH (cmp, i);
19623 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19625 face = FACE_FROM_ID (it->f, face_id);
19626 get_char_face_and_encoding (it->f, ch, face->id,
19627 &char2b, it->multibyte_p, 0);
19628 font = face->font;
19629 if (font == NULL)
19631 font = FRAME_FONT (it->f);
19632 boff = FRAME_BASELINE_OFFSET (it->f);
19633 font_info = NULL;
19635 else
19637 font_info
19638 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19639 boff = font_info->baseline_offset;
19640 if (font_info->vertical_centering)
19641 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19644 if (font_info
19645 && (pcm = rif->per_char_metric (font, &char2b,
19646 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19648 width = pcm->width;
19649 ascent = pcm->ascent;
19650 descent = pcm->descent;
19652 else
19654 width = FONT_WIDTH (font);
19655 ascent = 1;
19656 descent = 0;
19659 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19661 /* Relative composition with or without
19662 alternate chars. */
19663 left = (leftmost + rightmost - width) / 2;
19664 btm = - descent + boff;
19665 if (font_info && font_info->relative_compose
19666 && (! CHAR_TABLE_P (Vignore_relative_composition)
19667 || NILP (Faref (Vignore_relative_composition,
19668 make_number (ch)))))
19671 if (- descent >= font_info->relative_compose)
19672 /* One extra pixel between two glyphs. */
19673 btm = highest + 1;
19674 else if (ascent <= 0)
19675 /* One extra pixel between two glyphs. */
19676 btm = lowest - 1 - ascent - descent;
19679 else
19681 /* A composition rule is specified by an integer
19682 value that encodes global and new reference
19683 points (GREF and NREF). GREF and NREF are
19684 specified by numbers as below:
19686 0---1---2 -- ascent
19690 9--10--11 -- center
19692 ---3---4---5--- baseline
19694 6---7---8 -- descent
19696 int rule = COMPOSITION_RULE (cmp, i);
19697 int gref, nref, grefx, grefy, nrefx, nrefy;
19699 COMPOSITION_DECODE_RULE (rule, gref, nref);
19700 grefx = gref % 3, nrefx = nref % 3;
19701 grefy = gref / 3, nrefy = nref / 3;
19703 left = (leftmost
19704 + grefx * (rightmost - leftmost) / 2
19705 - nrefx * width / 2);
19706 btm = ((grefy == 0 ? highest
19707 : grefy == 1 ? 0
19708 : grefy == 2 ? lowest
19709 : (highest + lowest) / 2)
19710 - (nrefy == 0 ? ascent + descent
19711 : nrefy == 1 ? descent - boff
19712 : nrefy == 2 ? 0
19713 : (ascent + descent) / 2));
19716 cmp->offsets[i * 2] = left;
19717 cmp->offsets[i * 2 + 1] = btm + descent;
19719 /* Update the bounding box of the overall glyphs. */
19720 right = left + width;
19721 top = btm + descent + ascent;
19722 if (left < leftmost)
19723 leftmost = left;
19724 if (right > rightmost)
19725 rightmost = right;
19726 if (top > highest)
19727 highest = top;
19728 if (btm < lowest)
19729 lowest = btm;
19732 /* If there are glyphs whose x-offsets are negative,
19733 shift all glyphs to the right and make all x-offsets
19734 non-negative. */
19735 if (leftmost < 0)
19737 for (i = 0; i < cmp->glyph_len; i++)
19738 cmp->offsets[i * 2] -= leftmost;
19739 rightmost -= leftmost;
19742 cmp->pixel_width = rightmost;
19743 cmp->ascent = highest;
19744 cmp->descent = - lowest;
19745 if (cmp->ascent < font_ascent)
19746 cmp->ascent = font_ascent;
19747 if (cmp->descent < font_descent)
19748 cmp->descent = font_descent;
19751 it->pixel_width = cmp->pixel_width;
19752 it->ascent = it->phys_ascent = cmp->ascent;
19753 it->descent = it->phys_descent = cmp->descent;
19755 if (face->box != FACE_NO_BOX)
19757 int thick = face->box_line_width;
19759 if (thick > 0)
19761 it->ascent += thick;
19762 it->descent += thick;
19764 else
19765 thick = - thick;
19767 if (it->start_of_box_run_p)
19768 it->pixel_width += thick;
19769 if (it->end_of_box_run_p)
19770 it->pixel_width += thick;
19773 /* If face has an overline, add the height of the overline
19774 (1 pixel) and a 1 pixel margin to the character height. */
19775 if (face->overline_p)
19776 it->ascent += 2;
19778 take_vertical_position_into_account (it);
19780 if (it->glyph_row)
19781 append_composite_glyph (it);
19783 else if (it->what == IT_IMAGE)
19784 produce_image_glyph (it);
19785 else if (it->what == IT_STRETCH)
19786 produce_stretch_glyph (it);
19788 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19789 because this isn't true for images with `:ascent 100'. */
19790 xassert (it->ascent >= 0 && it->descent >= 0);
19791 if (it->area == TEXT_AREA)
19792 it->current_x += it->pixel_width;
19794 if (extra_line_spacing > 0)
19796 it->descent += extra_line_spacing;
19797 if (extra_line_spacing > it->max_extra_line_spacing)
19798 it->max_extra_line_spacing = extra_line_spacing;
19801 it->max_ascent = max (it->max_ascent, it->ascent);
19802 it->max_descent = max (it->max_descent, it->descent);
19803 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19804 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19807 /* EXPORT for RIF:
19808 Output LEN glyphs starting at START at the nominal cursor position.
19809 Advance the nominal cursor over the text. The global variable
19810 updated_window contains the window being updated, updated_row is
19811 the glyph row being updated, and updated_area is the area of that
19812 row being updated. */
19814 void
19815 x_write_glyphs (start, len)
19816 struct glyph *start;
19817 int len;
19819 int x, hpos;
19821 xassert (updated_window && updated_row);
19822 BLOCK_INPUT;
19824 /* Write glyphs. */
19826 hpos = start - updated_row->glyphs[updated_area];
19827 x = draw_glyphs (updated_window, output_cursor.x,
19828 updated_row, updated_area,
19829 hpos, hpos + len,
19830 DRAW_NORMAL_TEXT, 0);
19832 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19833 if (updated_area == TEXT_AREA
19834 && updated_window->phys_cursor_on_p
19835 && updated_window->phys_cursor.vpos == output_cursor.vpos
19836 && updated_window->phys_cursor.hpos >= hpos
19837 && updated_window->phys_cursor.hpos < hpos + len)
19838 updated_window->phys_cursor_on_p = 0;
19840 UNBLOCK_INPUT;
19842 /* Advance the output cursor. */
19843 output_cursor.hpos += len;
19844 output_cursor.x = x;
19848 /* EXPORT for RIF:
19849 Insert LEN glyphs from START at the nominal cursor position. */
19851 void
19852 x_insert_glyphs (start, len)
19853 struct glyph *start;
19854 int len;
19856 struct frame *f;
19857 struct window *w;
19858 int line_height, shift_by_width, shifted_region_width;
19859 struct glyph_row *row;
19860 struct glyph *glyph;
19861 int frame_x, frame_y, hpos;
19863 xassert (updated_window && updated_row);
19864 BLOCK_INPUT;
19865 w = updated_window;
19866 f = XFRAME (WINDOW_FRAME (w));
19868 /* Get the height of the line we are in. */
19869 row = updated_row;
19870 line_height = row->height;
19872 /* Get the width of the glyphs to insert. */
19873 shift_by_width = 0;
19874 for (glyph = start; glyph < start + len; ++glyph)
19875 shift_by_width += glyph->pixel_width;
19877 /* Get the width of the region to shift right. */
19878 shifted_region_width = (window_box_width (w, updated_area)
19879 - output_cursor.x
19880 - shift_by_width);
19882 /* Shift right. */
19883 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19884 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19886 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19887 line_height, shift_by_width);
19889 /* Write the glyphs. */
19890 hpos = start - row->glyphs[updated_area];
19891 draw_glyphs (w, output_cursor.x, row, updated_area,
19892 hpos, hpos + len,
19893 DRAW_NORMAL_TEXT, 0);
19895 /* Advance the output cursor. */
19896 output_cursor.hpos += len;
19897 output_cursor.x += shift_by_width;
19898 UNBLOCK_INPUT;
19902 /* EXPORT for RIF:
19903 Erase the current text line from the nominal cursor position
19904 (inclusive) to pixel column TO_X (exclusive). The idea is that
19905 everything from TO_X onward is already erased.
19907 TO_X is a pixel position relative to updated_area of
19908 updated_window. TO_X == -1 means clear to the end of this area. */
19910 void
19911 x_clear_end_of_line (to_x)
19912 int to_x;
19914 struct frame *f;
19915 struct window *w = updated_window;
19916 int max_x, min_y, max_y;
19917 int from_x, from_y, to_y;
19919 xassert (updated_window && updated_row);
19920 f = XFRAME (w->frame);
19922 if (updated_row->full_width_p)
19923 max_x = WINDOW_TOTAL_WIDTH (w);
19924 else
19925 max_x = window_box_width (w, updated_area);
19926 max_y = window_text_bottom_y (w);
19928 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19929 of window. For TO_X > 0, truncate to end of drawing area. */
19930 if (to_x == 0)
19931 return;
19932 else if (to_x < 0)
19933 to_x = max_x;
19934 else
19935 to_x = min (to_x, max_x);
19937 to_y = min (max_y, output_cursor.y + updated_row->height);
19939 /* Notice if the cursor will be cleared by this operation. */
19940 if (!updated_row->full_width_p)
19941 notice_overwritten_cursor (w, updated_area,
19942 output_cursor.x, -1,
19943 updated_row->y,
19944 MATRIX_ROW_BOTTOM_Y (updated_row));
19946 from_x = output_cursor.x;
19948 /* Translate to frame coordinates. */
19949 if (updated_row->full_width_p)
19951 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19952 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19954 else
19956 int area_left = window_box_left (w, updated_area);
19957 from_x += area_left;
19958 to_x += area_left;
19961 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19962 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19963 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19965 /* Prevent inadvertently clearing to end of the X window. */
19966 if (to_x > from_x && to_y > from_y)
19968 BLOCK_INPUT;
19969 rif->clear_frame_area (f, from_x, from_y,
19970 to_x - from_x, to_y - from_y);
19971 UNBLOCK_INPUT;
19975 #endif /* HAVE_WINDOW_SYSTEM */
19979 /***********************************************************************
19980 Cursor types
19981 ***********************************************************************/
19983 /* Value is the internal representation of the specified cursor type
19984 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19985 of the bar cursor. */
19987 static enum text_cursor_kinds
19988 get_specified_cursor_type (arg, width)
19989 Lisp_Object arg;
19990 int *width;
19992 enum text_cursor_kinds type;
19994 if (NILP (arg))
19995 return NO_CURSOR;
19997 if (EQ (arg, Qbox))
19998 return FILLED_BOX_CURSOR;
20000 if (EQ (arg, Qhollow))
20001 return HOLLOW_BOX_CURSOR;
20003 if (EQ (arg, Qbar))
20005 *width = 2;
20006 return BAR_CURSOR;
20009 if (CONSP (arg)
20010 && EQ (XCAR (arg), Qbar)
20011 && INTEGERP (XCDR (arg))
20012 && XINT (XCDR (arg)) >= 0)
20014 *width = XINT (XCDR (arg));
20015 return BAR_CURSOR;
20018 if (EQ (arg, Qhbar))
20020 *width = 2;
20021 return HBAR_CURSOR;
20024 if (CONSP (arg)
20025 && EQ (XCAR (arg), Qhbar)
20026 && INTEGERP (XCDR (arg))
20027 && XINT (XCDR (arg)) >= 0)
20029 *width = XINT (XCDR (arg));
20030 return HBAR_CURSOR;
20033 /* Treat anything unknown as "hollow box cursor".
20034 It was bad to signal an error; people have trouble fixing
20035 .Xdefaults with Emacs, when it has something bad in it. */
20036 type = HOLLOW_BOX_CURSOR;
20038 return type;
20041 /* Set the default cursor types for specified frame. */
20042 void
20043 set_frame_cursor_types (f, arg)
20044 struct frame *f;
20045 Lisp_Object arg;
20047 int width;
20048 Lisp_Object tem;
20050 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20051 FRAME_CURSOR_WIDTH (f) = width;
20053 /* By default, set up the blink-off state depending on the on-state. */
20055 tem = Fassoc (arg, Vblink_cursor_alist);
20056 if (!NILP (tem))
20058 FRAME_BLINK_OFF_CURSOR (f)
20059 = get_specified_cursor_type (XCDR (tem), &width);
20060 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20062 else
20063 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20067 /* Return the cursor we want to be displayed in window W. Return
20068 width of bar/hbar cursor through WIDTH arg. Return with
20069 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20070 (i.e. if the `system caret' should track this cursor).
20072 In a mini-buffer window, we want the cursor only to appear if we
20073 are reading input from this window. For the selected window, we
20074 want the cursor type given by the frame parameter or buffer local
20075 setting of cursor-type. If explicitly marked off, draw no cursor.
20076 In all other cases, we want a hollow box cursor. */
20078 static enum text_cursor_kinds
20079 get_window_cursor_type (w, glyph, width, active_cursor)
20080 struct window *w;
20081 struct glyph *glyph;
20082 int *width;
20083 int *active_cursor;
20085 struct frame *f = XFRAME (w->frame);
20086 struct buffer *b = XBUFFER (w->buffer);
20087 int cursor_type = DEFAULT_CURSOR;
20088 Lisp_Object alt_cursor;
20089 int non_selected = 0;
20091 *active_cursor = 1;
20093 /* Echo area */
20094 if (cursor_in_echo_area
20095 && FRAME_HAS_MINIBUF_P (f)
20096 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20098 if (w == XWINDOW (echo_area_window))
20100 *width = FRAME_CURSOR_WIDTH (f);
20101 return FRAME_DESIRED_CURSOR (f);
20104 *active_cursor = 0;
20105 non_selected = 1;
20108 /* Nonselected window or nonselected frame. */
20109 else if (w != XWINDOW (f->selected_window)
20110 #ifdef HAVE_WINDOW_SYSTEM
20111 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20112 #endif
20115 *active_cursor = 0;
20117 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20118 return NO_CURSOR;
20120 non_selected = 1;
20123 /* Never display a cursor in a window in which cursor-type is nil. */
20124 if (NILP (b->cursor_type))
20125 return NO_CURSOR;
20127 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20128 if (non_selected)
20130 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20131 return get_specified_cursor_type (alt_cursor, width);
20134 /* Get the normal cursor type for this window. */
20135 if (EQ (b->cursor_type, Qt))
20137 cursor_type = FRAME_DESIRED_CURSOR (f);
20138 *width = FRAME_CURSOR_WIDTH (f);
20140 else
20141 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20143 /* Use normal cursor if not blinked off. */
20144 if (!w->cursor_off_p)
20146 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20147 if (cursor_type == FILLED_BOX_CURSOR)
20148 cursor_type = HOLLOW_BOX_CURSOR;
20150 return cursor_type;
20153 /* Cursor is blinked off, so determine how to "toggle" it. */
20155 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20156 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20157 return get_specified_cursor_type (XCDR (alt_cursor), width);
20159 /* Then see if frame has specified a specific blink off cursor type. */
20160 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20162 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20163 return FRAME_BLINK_OFF_CURSOR (f);
20166 #if 0
20167 /* Some people liked having a permanently visible blinking cursor,
20168 while others had very strong opinions against it. So it was
20169 decided to remove it. KFS 2003-09-03 */
20171 /* Finally perform built-in cursor blinking:
20172 filled box <-> hollow box
20173 wide [h]bar <-> narrow [h]bar
20174 narrow [h]bar <-> no cursor
20175 other type <-> no cursor */
20177 if (cursor_type == FILLED_BOX_CURSOR)
20178 return HOLLOW_BOX_CURSOR;
20180 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20182 *width = 1;
20183 return cursor_type;
20185 #endif
20187 return NO_CURSOR;
20191 #ifdef HAVE_WINDOW_SYSTEM
20193 /* Notice when the text cursor of window W has been completely
20194 overwritten by a drawing operation that outputs glyphs in AREA
20195 starting at X0 and ending at X1 in the line starting at Y0 and
20196 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20197 the rest of the line after X0 has been written. Y coordinates
20198 are window-relative. */
20200 static void
20201 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20202 struct window *w;
20203 enum glyph_row_area area;
20204 int x0, y0, x1, y1;
20206 int cx0, cx1, cy0, cy1;
20207 struct glyph_row *row;
20209 if (!w->phys_cursor_on_p)
20210 return;
20211 if (area != TEXT_AREA)
20212 return;
20214 if (w->phys_cursor.vpos < 0
20215 || w->phys_cursor.vpos >= w->current_matrix->nrows
20216 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20217 !(row->enabled_p && row->displays_text_p)))
20218 return;
20220 if (row->cursor_in_fringe_p)
20222 row->cursor_in_fringe_p = 0;
20223 draw_fringe_bitmap (w, row, 0);
20224 w->phys_cursor_on_p = 0;
20225 return;
20228 cx0 = w->phys_cursor.x;
20229 cx1 = cx0 + w->phys_cursor_width;
20230 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20231 return;
20233 /* The cursor image will be completely removed from the
20234 screen if the output area intersects the cursor area in
20235 y-direction. When we draw in [y0 y1[, and some part of
20236 the cursor is at y < y0, that part must have been drawn
20237 before. When scrolling, the cursor is erased before
20238 actually scrolling, so we don't come here. When not
20239 scrolling, the rows above the old cursor row must have
20240 changed, and in this case these rows must have written
20241 over the cursor image.
20243 Likewise if part of the cursor is below y1, with the
20244 exception of the cursor being in the first blank row at
20245 the buffer and window end because update_text_area
20246 doesn't draw that row. (Except when it does, but
20247 that's handled in update_text_area.) */
20249 cy0 = w->phys_cursor.y;
20250 cy1 = cy0 + w->phys_cursor_height;
20251 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20252 return;
20254 w->phys_cursor_on_p = 0;
20257 #endif /* HAVE_WINDOW_SYSTEM */
20260 /************************************************************************
20261 Mouse Face
20262 ************************************************************************/
20264 #ifdef HAVE_WINDOW_SYSTEM
20266 /* EXPORT for RIF:
20267 Fix the display of area AREA of overlapping row ROW in window W. */
20269 void
20270 x_fix_overlapping_area (w, row, area)
20271 struct window *w;
20272 struct glyph_row *row;
20273 enum glyph_row_area area;
20275 int i, x;
20277 BLOCK_INPUT;
20279 x = 0;
20280 for (i = 0; i < row->used[area];)
20282 if (row->glyphs[area][i].overlaps_vertically_p)
20284 int start = i, start_x = x;
20288 x += row->glyphs[area][i].pixel_width;
20289 ++i;
20291 while (i < row->used[area]
20292 && row->glyphs[area][i].overlaps_vertically_p);
20294 draw_glyphs (w, start_x, row, area,
20295 start, i,
20296 DRAW_NORMAL_TEXT, 1);
20298 else
20300 x += row->glyphs[area][i].pixel_width;
20301 ++i;
20305 UNBLOCK_INPUT;
20309 /* EXPORT:
20310 Draw the cursor glyph of window W in glyph row ROW. See the
20311 comment of draw_glyphs for the meaning of HL. */
20313 void
20314 draw_phys_cursor_glyph (w, row, hl)
20315 struct window *w;
20316 struct glyph_row *row;
20317 enum draw_glyphs_face hl;
20319 /* If cursor hpos is out of bounds, don't draw garbage. This can
20320 happen in mini-buffer windows when switching between echo area
20321 glyphs and mini-buffer. */
20322 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20324 int on_p = w->phys_cursor_on_p;
20325 int x1;
20326 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20327 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20328 hl, 0);
20329 w->phys_cursor_on_p = on_p;
20331 if (hl == DRAW_CURSOR)
20332 w->phys_cursor_width = x1 - w->phys_cursor.x;
20333 /* When we erase the cursor, and ROW is overlapped by other
20334 rows, make sure that these overlapping parts of other rows
20335 are redrawn. */
20336 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20338 if (row > w->current_matrix->rows
20339 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20340 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20342 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20343 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20344 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20350 /* EXPORT:
20351 Erase the image of a cursor of window W from the screen. */
20353 void
20354 erase_phys_cursor (w)
20355 struct window *w;
20357 struct frame *f = XFRAME (w->frame);
20358 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20359 int hpos = w->phys_cursor.hpos;
20360 int vpos = w->phys_cursor.vpos;
20361 int mouse_face_here_p = 0;
20362 struct glyph_matrix *active_glyphs = w->current_matrix;
20363 struct glyph_row *cursor_row;
20364 struct glyph *cursor_glyph;
20365 enum draw_glyphs_face hl;
20367 /* No cursor displayed or row invalidated => nothing to do on the
20368 screen. */
20369 if (w->phys_cursor_type == NO_CURSOR)
20370 goto mark_cursor_off;
20372 /* VPOS >= active_glyphs->nrows means that window has been resized.
20373 Don't bother to erase the cursor. */
20374 if (vpos >= active_glyphs->nrows)
20375 goto mark_cursor_off;
20377 /* If row containing cursor is marked invalid, there is nothing we
20378 can do. */
20379 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20380 if (!cursor_row->enabled_p)
20381 goto mark_cursor_off;
20383 /* If line spacing is > 0, old cursor may only be partially visible in
20384 window after split-window. So adjust visible height. */
20385 cursor_row->visible_height = min (cursor_row->visible_height,
20386 window_text_bottom_y (w) - cursor_row->y);
20388 /* If row is completely invisible, don't attempt to delete a cursor which
20389 isn't there. This can happen if cursor is at top of a window, and
20390 we switch to a buffer with a header line in that window. */
20391 if (cursor_row->visible_height <= 0)
20392 goto mark_cursor_off;
20394 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20395 if (cursor_row->cursor_in_fringe_p)
20397 cursor_row->cursor_in_fringe_p = 0;
20398 draw_fringe_bitmap (w, cursor_row, 0);
20399 goto mark_cursor_off;
20402 /* This can happen when the new row is shorter than the old one.
20403 In this case, either draw_glyphs or clear_end_of_line
20404 should have cleared the cursor. Note that we wouldn't be
20405 able to erase the cursor in this case because we don't have a
20406 cursor glyph at hand. */
20407 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20408 goto mark_cursor_off;
20410 /* If the cursor is in the mouse face area, redisplay that when
20411 we clear the cursor. */
20412 if (! NILP (dpyinfo->mouse_face_window)
20413 && w == XWINDOW (dpyinfo->mouse_face_window)
20414 && (vpos > dpyinfo->mouse_face_beg_row
20415 || (vpos == dpyinfo->mouse_face_beg_row
20416 && hpos >= dpyinfo->mouse_face_beg_col))
20417 && (vpos < dpyinfo->mouse_face_end_row
20418 || (vpos == dpyinfo->mouse_face_end_row
20419 && hpos < dpyinfo->mouse_face_end_col))
20420 /* Don't redraw the cursor's spot in mouse face if it is at the
20421 end of a line (on a newline). The cursor appears there, but
20422 mouse highlighting does not. */
20423 && cursor_row->used[TEXT_AREA] > hpos)
20424 mouse_face_here_p = 1;
20426 /* Maybe clear the display under the cursor. */
20427 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20429 int x, y;
20430 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20431 int width;
20433 cursor_glyph = get_phys_cursor_glyph (w);
20434 if (cursor_glyph == NULL)
20435 goto mark_cursor_off;
20437 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20438 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20439 width = min (cursor_glyph->pixel_width,
20440 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20442 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20445 /* Erase the cursor by redrawing the character underneath it. */
20446 if (mouse_face_here_p)
20447 hl = DRAW_MOUSE_FACE;
20448 else
20449 hl = DRAW_NORMAL_TEXT;
20450 draw_phys_cursor_glyph (w, cursor_row, hl);
20452 mark_cursor_off:
20453 w->phys_cursor_on_p = 0;
20454 w->phys_cursor_type = NO_CURSOR;
20458 /* EXPORT:
20459 Display or clear cursor of window W. If ON is zero, clear the
20460 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20461 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20463 void
20464 display_and_set_cursor (w, on, hpos, vpos, x, y)
20465 struct window *w;
20466 int on, hpos, vpos, x, y;
20468 struct frame *f = XFRAME (w->frame);
20469 int new_cursor_type;
20470 int new_cursor_width;
20471 int active_cursor;
20472 struct glyph_row *glyph_row;
20473 struct glyph *glyph;
20475 /* This is pointless on invisible frames, and dangerous on garbaged
20476 windows and frames; in the latter case, the frame or window may
20477 be in the midst of changing its size, and x and y may be off the
20478 window. */
20479 if (! FRAME_VISIBLE_P (f)
20480 || FRAME_GARBAGED_P (f)
20481 || vpos >= w->current_matrix->nrows
20482 || hpos >= w->current_matrix->matrix_w)
20483 return;
20485 /* If cursor is off and we want it off, return quickly. */
20486 if (!on && !w->phys_cursor_on_p)
20487 return;
20489 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20490 /* If cursor row is not enabled, we don't really know where to
20491 display the cursor. */
20492 if (!glyph_row->enabled_p)
20494 w->phys_cursor_on_p = 0;
20495 return;
20498 glyph = NULL;
20499 if (!glyph_row->exact_window_width_line_p
20500 || hpos < glyph_row->used[TEXT_AREA])
20501 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20503 xassert (interrupt_input_blocked);
20505 /* Set new_cursor_type to the cursor we want to be displayed. */
20506 new_cursor_type = get_window_cursor_type (w, glyph,
20507 &new_cursor_width, &active_cursor);
20509 /* If cursor is currently being shown and we don't want it to be or
20510 it is in the wrong place, or the cursor type is not what we want,
20511 erase it. */
20512 if (w->phys_cursor_on_p
20513 && (!on
20514 || w->phys_cursor.x != x
20515 || w->phys_cursor.y != y
20516 || new_cursor_type != w->phys_cursor_type
20517 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20518 && new_cursor_width != w->phys_cursor_width)))
20519 erase_phys_cursor (w);
20521 /* Don't check phys_cursor_on_p here because that flag is only set
20522 to zero in some cases where we know that the cursor has been
20523 completely erased, to avoid the extra work of erasing the cursor
20524 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20525 still not be visible, or it has only been partly erased. */
20526 if (on)
20528 w->phys_cursor_ascent = glyph_row->ascent;
20529 w->phys_cursor_height = glyph_row->height;
20531 /* Set phys_cursor_.* before x_draw_.* is called because some
20532 of them may need the information. */
20533 w->phys_cursor.x = x;
20534 w->phys_cursor.y = glyph_row->y;
20535 w->phys_cursor.hpos = hpos;
20536 w->phys_cursor.vpos = vpos;
20539 rif->draw_window_cursor (w, glyph_row, x, y,
20540 new_cursor_type, new_cursor_width,
20541 on, active_cursor);
20545 /* Switch the display of W's cursor on or off, according to the value
20546 of ON. */
20548 static void
20549 update_window_cursor (w, on)
20550 struct window *w;
20551 int on;
20553 /* Don't update cursor in windows whose frame is in the process
20554 of being deleted. */
20555 if (w->current_matrix)
20557 BLOCK_INPUT;
20558 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20559 w->phys_cursor.x, w->phys_cursor.y);
20560 UNBLOCK_INPUT;
20565 /* Call update_window_cursor with parameter ON_P on all leaf windows
20566 in the window tree rooted at W. */
20568 static void
20569 update_cursor_in_window_tree (w, on_p)
20570 struct window *w;
20571 int on_p;
20573 while (w)
20575 if (!NILP (w->hchild))
20576 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20577 else if (!NILP (w->vchild))
20578 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20579 else
20580 update_window_cursor (w, on_p);
20582 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20587 /* EXPORT:
20588 Display the cursor on window W, or clear it, according to ON_P.
20589 Don't change the cursor's position. */
20591 void
20592 x_update_cursor (f, on_p)
20593 struct frame *f;
20594 int on_p;
20596 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20600 /* EXPORT:
20601 Clear the cursor of window W to background color, and mark the
20602 cursor as not shown. This is used when the text where the cursor
20603 is is about to be rewritten. */
20605 void
20606 x_clear_cursor (w)
20607 struct window *w;
20609 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20610 update_window_cursor (w, 0);
20614 /* EXPORT:
20615 Display the active region described by mouse_face_* according to DRAW. */
20617 void
20618 show_mouse_face (dpyinfo, draw)
20619 Display_Info *dpyinfo;
20620 enum draw_glyphs_face draw;
20622 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20623 struct frame *f = XFRAME (WINDOW_FRAME (w));
20625 if (/* If window is in the process of being destroyed, don't bother
20626 to do anything. */
20627 w->current_matrix != NULL
20628 /* Don't update mouse highlight if hidden */
20629 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20630 /* Recognize when we are called to operate on rows that don't exist
20631 anymore. This can happen when a window is split. */
20632 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20634 int phys_cursor_on_p = w->phys_cursor_on_p;
20635 struct glyph_row *row, *first, *last;
20637 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20638 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20640 for (row = first; row <= last && row->enabled_p; ++row)
20642 int start_hpos, end_hpos, start_x;
20644 /* For all but the first row, the highlight starts at column 0. */
20645 if (row == first)
20647 start_hpos = dpyinfo->mouse_face_beg_col;
20648 start_x = dpyinfo->mouse_face_beg_x;
20650 else
20652 start_hpos = 0;
20653 start_x = 0;
20656 if (row == last)
20657 end_hpos = dpyinfo->mouse_face_end_col;
20658 else
20659 end_hpos = row->used[TEXT_AREA];
20661 if (end_hpos > start_hpos)
20663 draw_glyphs (w, start_x, row, TEXT_AREA,
20664 start_hpos, end_hpos,
20665 draw, 0);
20667 row->mouse_face_p
20668 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20672 /* When we've written over the cursor, arrange for it to
20673 be displayed again. */
20674 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20676 BLOCK_INPUT;
20677 display_and_set_cursor (w, 1,
20678 w->phys_cursor.hpos, w->phys_cursor.vpos,
20679 w->phys_cursor.x, w->phys_cursor.y);
20680 UNBLOCK_INPUT;
20684 /* Change the mouse cursor. */
20685 if (draw == DRAW_NORMAL_TEXT)
20686 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20687 else if (draw == DRAW_MOUSE_FACE)
20688 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20689 else
20690 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20693 /* EXPORT:
20694 Clear out the mouse-highlighted active region.
20695 Redraw it un-highlighted first. Value is non-zero if mouse
20696 face was actually drawn unhighlighted. */
20699 clear_mouse_face (dpyinfo)
20700 Display_Info *dpyinfo;
20702 int cleared = 0;
20704 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20706 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20707 cleared = 1;
20710 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20711 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20712 dpyinfo->mouse_face_window = Qnil;
20713 dpyinfo->mouse_face_overlay = Qnil;
20714 return cleared;
20718 /* EXPORT:
20719 Non-zero if physical cursor of window W is within mouse face. */
20722 cursor_in_mouse_face_p (w)
20723 struct window *w;
20725 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20726 int in_mouse_face = 0;
20728 if (WINDOWP (dpyinfo->mouse_face_window)
20729 && XWINDOW (dpyinfo->mouse_face_window) == w)
20731 int hpos = w->phys_cursor.hpos;
20732 int vpos = w->phys_cursor.vpos;
20734 if (vpos >= dpyinfo->mouse_face_beg_row
20735 && vpos <= dpyinfo->mouse_face_end_row
20736 && (vpos > dpyinfo->mouse_face_beg_row
20737 || hpos >= dpyinfo->mouse_face_beg_col)
20738 && (vpos < dpyinfo->mouse_face_end_row
20739 || hpos < dpyinfo->mouse_face_end_col
20740 || dpyinfo->mouse_face_past_end))
20741 in_mouse_face = 1;
20744 return in_mouse_face;
20750 /* Find the glyph matrix position of buffer position CHARPOS in window
20751 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20752 current glyphs must be up to date. If CHARPOS is above window
20753 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20754 of last line in W. In the row containing CHARPOS, stop before glyphs
20755 having STOP as object. */
20757 #if 1 /* This is a version of fast_find_position that's more correct
20758 in the presence of hscrolling, for example. I didn't install
20759 it right away because the problem fixed is minor, it failed
20760 in 20.x as well, and I think it's too risky to install
20761 so near the release of 21.1. 2001-09-25 gerd. */
20763 static int
20764 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20765 struct window *w;
20766 int charpos;
20767 int *hpos, *vpos, *x, *y;
20768 Lisp_Object stop;
20770 struct glyph_row *row, *first;
20771 struct glyph *glyph, *end;
20772 int past_end = 0;
20774 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20775 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20777 *x = first->x;
20778 *y = first->y;
20779 *hpos = 0;
20780 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20781 return 1;
20784 row = row_containing_pos (w, charpos, first, NULL, 0);
20785 if (row == NULL)
20787 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20788 past_end = 1;
20791 /* If whole rows or last part of a row came from a display overlay,
20792 row_containing_pos will skip over such rows because their end pos
20793 equals the start pos of the overlay or interval.
20795 Move back if we have a STOP object and previous row's
20796 end glyph came from STOP. */
20797 if (!NILP (stop))
20799 struct glyph_row *prev;
20800 while ((prev = row - 1, prev >= first)
20801 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20802 && prev->used[TEXT_AREA] > 0)
20804 struct glyph *beg = prev->glyphs[TEXT_AREA];
20805 glyph = beg + prev->used[TEXT_AREA];
20806 while (--glyph >= beg
20807 && INTEGERP (glyph->object));
20808 if (glyph < beg
20809 || !EQ (stop, glyph->object))
20810 break;
20811 row = prev;
20815 *x = row->x;
20816 *y = row->y;
20817 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20819 glyph = row->glyphs[TEXT_AREA];
20820 end = glyph + row->used[TEXT_AREA];
20822 /* Skip over glyphs not having an object at the start of the row.
20823 These are special glyphs like truncation marks on terminal
20824 frames. */
20825 if (row->displays_text_p)
20826 while (glyph < end
20827 && INTEGERP (glyph->object)
20828 && !EQ (stop, glyph->object)
20829 && glyph->charpos < 0)
20831 *x += glyph->pixel_width;
20832 ++glyph;
20835 while (glyph < end
20836 && !INTEGERP (glyph->object)
20837 && !EQ (stop, glyph->object)
20838 && (!BUFFERP (glyph->object)
20839 || glyph->charpos < charpos))
20841 *x += glyph->pixel_width;
20842 ++glyph;
20845 *hpos = glyph - row->glyphs[TEXT_AREA];
20846 return !past_end;
20849 #else /* not 1 */
20851 static int
20852 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20853 struct window *w;
20854 int pos;
20855 int *hpos, *vpos, *x, *y;
20856 Lisp_Object stop;
20858 int i;
20859 int lastcol;
20860 int maybe_next_line_p = 0;
20861 int line_start_position;
20862 int yb = window_text_bottom_y (w);
20863 struct glyph_row *row, *best_row;
20864 int row_vpos, best_row_vpos;
20865 int current_x;
20867 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20868 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20870 while (row->y < yb)
20872 if (row->used[TEXT_AREA])
20873 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20874 else
20875 line_start_position = 0;
20877 if (line_start_position > pos)
20878 break;
20879 /* If the position sought is the end of the buffer,
20880 don't include the blank lines at the bottom of the window. */
20881 else if (line_start_position == pos
20882 && pos == BUF_ZV (XBUFFER (w->buffer)))
20884 maybe_next_line_p = 1;
20885 break;
20887 else if (line_start_position > 0)
20889 best_row = row;
20890 best_row_vpos = row_vpos;
20893 if (row->y + row->height >= yb)
20894 break;
20896 ++row;
20897 ++row_vpos;
20900 /* Find the right column within BEST_ROW. */
20901 lastcol = 0;
20902 current_x = best_row->x;
20903 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20905 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20906 int charpos = glyph->charpos;
20908 if (BUFFERP (glyph->object))
20910 if (charpos == pos)
20912 *hpos = i;
20913 *vpos = best_row_vpos;
20914 *x = current_x;
20915 *y = best_row->y;
20916 return 1;
20918 else if (charpos > pos)
20919 break;
20921 else if (EQ (glyph->object, stop))
20922 break;
20924 if (charpos > 0)
20925 lastcol = i;
20926 current_x += glyph->pixel_width;
20929 /* If we're looking for the end of the buffer,
20930 and we didn't find it in the line we scanned,
20931 use the start of the following line. */
20932 if (maybe_next_line_p)
20934 ++best_row;
20935 ++best_row_vpos;
20936 lastcol = 0;
20937 current_x = best_row->x;
20940 *vpos = best_row_vpos;
20941 *hpos = lastcol + 1;
20942 *x = current_x;
20943 *y = best_row->y;
20944 return 0;
20947 #endif /* not 1 */
20950 /* Find the position of the glyph for position POS in OBJECT in
20951 window W's current matrix, and return in *X, *Y the pixel
20952 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20954 RIGHT_P non-zero means return the position of the right edge of the
20955 glyph, RIGHT_P zero means return the left edge position.
20957 If no glyph for POS exists in the matrix, return the position of
20958 the glyph with the next smaller position that is in the matrix, if
20959 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20960 exists in the matrix, return the position of the glyph with the
20961 next larger position in OBJECT.
20963 Value is non-zero if a glyph was found. */
20965 static int
20966 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20967 struct window *w;
20968 int pos;
20969 Lisp_Object object;
20970 int *hpos, *vpos, *x, *y;
20971 int right_p;
20973 int yb = window_text_bottom_y (w);
20974 struct glyph_row *r;
20975 struct glyph *best_glyph = NULL;
20976 struct glyph_row *best_row = NULL;
20977 int best_x = 0;
20979 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20980 r->enabled_p && r->y < yb;
20981 ++r)
20983 struct glyph *g = r->glyphs[TEXT_AREA];
20984 struct glyph *e = g + r->used[TEXT_AREA];
20985 int gx;
20987 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20988 if (EQ (g->object, object))
20990 if (g->charpos == pos)
20992 best_glyph = g;
20993 best_x = gx;
20994 best_row = r;
20995 goto found;
20997 else if (best_glyph == NULL
20998 || ((abs (g->charpos - pos)
20999 < abs (best_glyph->charpos - pos))
21000 && (right_p
21001 ? g->charpos < pos
21002 : g->charpos > pos)))
21004 best_glyph = g;
21005 best_x = gx;
21006 best_row = r;
21011 found:
21013 if (best_glyph)
21015 *x = best_x;
21016 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21018 if (right_p)
21020 *x += best_glyph->pixel_width;
21021 ++*hpos;
21024 *y = best_row->y;
21025 *vpos = best_row - w->current_matrix->rows;
21028 return best_glyph != NULL;
21032 /* See if position X, Y is within a hot-spot of an image. */
21034 static int
21035 on_hot_spot_p (hot_spot, x, y)
21036 Lisp_Object hot_spot;
21037 int x, y;
21039 if (!CONSP (hot_spot))
21040 return 0;
21042 if (EQ (XCAR (hot_spot), Qrect))
21044 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21045 Lisp_Object rect = XCDR (hot_spot);
21046 Lisp_Object tem;
21047 if (!CONSP (rect))
21048 return 0;
21049 if (!CONSP (XCAR (rect)))
21050 return 0;
21051 if (!CONSP (XCDR (rect)))
21052 return 0;
21053 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21054 return 0;
21055 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21056 return 0;
21057 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21058 return 0;
21059 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21060 return 0;
21061 return 1;
21063 else if (EQ (XCAR (hot_spot), Qcircle))
21065 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21066 Lisp_Object circ = XCDR (hot_spot);
21067 Lisp_Object lr, lx0, ly0;
21068 if (CONSP (circ)
21069 && CONSP (XCAR (circ))
21070 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21071 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21072 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21074 double r = XFLOATINT (lr);
21075 double dx = XINT (lx0) - x;
21076 double dy = XINT (ly0) - y;
21077 return (dx * dx + dy * dy <= r * r);
21080 else if (EQ (XCAR (hot_spot), Qpoly))
21082 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21083 if (VECTORP (XCDR (hot_spot)))
21085 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21086 Lisp_Object *poly = v->contents;
21087 int n = v->size;
21088 int i;
21089 int inside = 0;
21090 Lisp_Object lx, ly;
21091 int x0, y0;
21093 /* Need an even number of coordinates, and at least 3 edges. */
21094 if (n < 6 || n & 1)
21095 return 0;
21097 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21098 If count is odd, we are inside polygon. Pixels on edges
21099 may or may not be included depending on actual geometry of the
21100 polygon. */
21101 if ((lx = poly[n-2], !INTEGERP (lx))
21102 || (ly = poly[n-1], !INTEGERP (lx)))
21103 return 0;
21104 x0 = XINT (lx), y0 = XINT (ly);
21105 for (i = 0; i < n; i += 2)
21107 int x1 = x0, y1 = y0;
21108 if ((lx = poly[i], !INTEGERP (lx))
21109 || (ly = poly[i+1], !INTEGERP (ly)))
21110 return 0;
21111 x0 = XINT (lx), y0 = XINT (ly);
21113 /* Does this segment cross the X line? */
21114 if (x0 >= x)
21116 if (x1 >= x)
21117 continue;
21119 else if (x1 < x)
21120 continue;
21121 if (y > y0 && y > y1)
21122 continue;
21123 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21124 inside = !inside;
21126 return inside;
21129 /* If we don't understand the format, pretend we're not in the hot-spot. */
21130 return 0;
21133 Lisp_Object
21134 find_hot_spot (map, x, y)
21135 Lisp_Object map;
21136 int x, y;
21138 while (CONSP (map))
21140 if (CONSP (XCAR (map))
21141 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21142 return XCAR (map);
21143 map = XCDR (map);
21146 return Qnil;
21149 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21150 3, 3, 0,
21151 doc: /* Lookup in image map MAP coordinates X and Y.
21152 An image map is an alist where each element has the format (AREA ID PLIST).
21153 An AREA is specified as either a rectangle, a circle, or a polygon:
21154 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21155 pixel coordinates of the upper left and bottom right corners.
21156 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21157 and the radius of the circle; r may be a float or integer.
21158 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21159 vector describes one corner in the polygon.
21160 Returns the alist element for the first matching AREA in MAP. */)
21161 (map, x, y)
21162 Lisp_Object map;
21163 Lisp_Object x, y;
21165 if (NILP (map))
21166 return Qnil;
21168 CHECK_NUMBER (x);
21169 CHECK_NUMBER (y);
21171 return find_hot_spot (map, XINT (x), XINT (y));
21175 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21176 static void
21177 define_frame_cursor1 (f, cursor, pointer)
21178 struct frame *f;
21179 Cursor cursor;
21180 Lisp_Object pointer;
21182 /* Do not change cursor shape while dragging mouse. */
21183 if (!NILP (do_mouse_tracking))
21184 return;
21186 if (!NILP (pointer))
21188 if (EQ (pointer, Qarrow))
21189 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21190 else if (EQ (pointer, Qhand))
21191 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21192 else if (EQ (pointer, Qtext))
21193 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21194 else if (EQ (pointer, intern ("hdrag")))
21195 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21196 #ifdef HAVE_X_WINDOWS
21197 else if (EQ (pointer, intern ("vdrag")))
21198 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21199 #endif
21200 else if (EQ (pointer, intern ("hourglass")))
21201 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21202 else if (EQ (pointer, Qmodeline))
21203 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21204 else
21205 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21208 if (cursor != No_Cursor)
21209 rif->define_frame_cursor (f, cursor);
21212 /* Take proper action when mouse has moved to the mode or header line
21213 or marginal area AREA of window W, x-position X and y-position Y.
21214 X is relative to the start of the text display area of W, so the
21215 width of bitmap areas and scroll bars must be subtracted to get a
21216 position relative to the start of the mode line. */
21218 static void
21219 note_mode_line_or_margin_highlight (w, x, y, area)
21220 struct window *w;
21221 int x, y;
21222 enum window_part area;
21224 struct frame *f = XFRAME (w->frame);
21225 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21226 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21227 Lisp_Object pointer = Qnil;
21228 int charpos, dx, dy, width, height;
21229 Lisp_Object string, object = Qnil;
21230 Lisp_Object pos, help;
21232 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21233 string = mode_line_string (w, area, &x, &y, &charpos,
21234 &object, &dx, &dy, &width, &height);
21235 else
21237 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21238 string = marginal_area_string (w, area, &x, &y, &charpos,
21239 &object, &dx, &dy, &width, &height);
21242 help = Qnil;
21244 if (IMAGEP (object))
21246 Lisp_Object image_map, hotspot;
21247 if ((image_map = Fplist_get (XCDR (object), QCmap),
21248 !NILP (image_map))
21249 && (hotspot = find_hot_spot (image_map, dx, dy),
21250 CONSP (hotspot))
21251 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21253 Lisp_Object area_id, plist;
21255 area_id = XCAR (hotspot);
21256 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21257 If so, we could look for mouse-enter, mouse-leave
21258 properties in PLIST (and do something...). */
21259 hotspot = XCDR (hotspot);
21260 if (CONSP (hotspot)
21261 && (plist = XCAR (hotspot), CONSP (plist)))
21263 pointer = Fplist_get (plist, Qpointer);
21264 if (NILP (pointer))
21265 pointer = Qhand;
21266 help = Fplist_get (plist, Qhelp_echo);
21267 if (!NILP (help))
21269 help_echo_string = help;
21270 /* Is this correct? ++kfs */
21271 XSETWINDOW (help_echo_window, w);
21272 help_echo_object = w->buffer;
21273 help_echo_pos = charpos;
21277 if (NILP (pointer))
21278 pointer = Fplist_get (XCDR (object), QCpointer);
21281 if (STRINGP (string))
21283 pos = make_number (charpos);
21284 /* If we're on a string with `help-echo' text property, arrange
21285 for the help to be displayed. This is done by setting the
21286 global variable help_echo_string to the help string. */
21287 if (NILP (help))
21289 help = Fget_text_property (pos, Qhelp_echo, string);
21290 if (!NILP (help))
21292 help_echo_string = help;
21293 XSETWINDOW (help_echo_window, w);
21294 help_echo_object = string;
21295 help_echo_pos = charpos;
21299 if (NILP (pointer))
21300 pointer = Fget_text_property (pos, Qpointer, string);
21302 /* Change the mouse pointer according to what is under X/Y. */
21303 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21305 Lisp_Object map;
21306 map = Fget_text_property (pos, Qlocal_map, string);
21307 if (!KEYMAPP (map))
21308 map = Fget_text_property (pos, Qkeymap, string);
21309 if (!KEYMAPP (map))
21310 cursor = dpyinfo->vertical_scroll_bar_cursor;
21314 define_frame_cursor1 (f, cursor, pointer);
21318 /* EXPORT:
21319 Take proper action when the mouse has moved to position X, Y on
21320 frame F as regards highlighting characters that have mouse-face
21321 properties. Also de-highlighting chars where the mouse was before.
21322 X and Y can be negative or out of range. */
21324 void
21325 note_mouse_highlight (f, x, y)
21326 struct frame *f;
21327 int x, y;
21329 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21330 enum window_part part;
21331 Lisp_Object window;
21332 struct window *w;
21333 Cursor cursor = No_Cursor;
21334 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21335 struct buffer *b;
21337 /* When a menu is active, don't highlight because this looks odd. */
21338 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21339 if (popup_activated ())
21340 return;
21341 #endif
21343 if (NILP (Vmouse_highlight)
21344 || !f->glyphs_initialized_p)
21345 return;
21347 dpyinfo->mouse_face_mouse_x = x;
21348 dpyinfo->mouse_face_mouse_y = y;
21349 dpyinfo->mouse_face_mouse_frame = f;
21351 if (dpyinfo->mouse_face_defer)
21352 return;
21354 if (gc_in_progress)
21356 dpyinfo->mouse_face_deferred_gc = 1;
21357 return;
21360 /* Which window is that in? */
21361 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21363 /* If we were displaying active text in another window, clear that.
21364 Also clear if we move out of text area in same window. */
21365 if (! EQ (window, dpyinfo->mouse_face_window)
21366 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21367 clear_mouse_face (dpyinfo);
21369 /* Not on a window -> return. */
21370 if (!WINDOWP (window))
21371 return;
21373 /* Reset help_echo_string. It will get recomputed below. */
21374 help_echo_string = Qnil;
21376 /* Convert to window-relative pixel coordinates. */
21377 w = XWINDOW (window);
21378 frame_to_window_pixel_xy (w, &x, &y);
21380 /* Handle tool-bar window differently since it doesn't display a
21381 buffer. */
21382 if (EQ (window, f->tool_bar_window))
21384 note_tool_bar_highlight (f, x, y);
21385 return;
21388 /* Mouse is on the mode, header line or margin? */
21389 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21390 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21392 note_mode_line_or_margin_highlight (w, x, y, part);
21393 return;
21396 if (part == ON_VERTICAL_BORDER)
21397 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21398 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21399 || part == ON_SCROLL_BAR)
21400 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21401 else
21402 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21404 /* Are we in a window whose display is up to date?
21405 And verify the buffer's text has not changed. */
21406 b = XBUFFER (w->buffer);
21407 if (part == ON_TEXT
21408 && EQ (w->window_end_valid, w->buffer)
21409 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21410 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21412 int hpos, vpos, pos, i, dx, dy, area;
21413 struct glyph *glyph;
21414 Lisp_Object object;
21415 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21416 Lisp_Object *overlay_vec = NULL;
21417 int noverlays;
21418 struct buffer *obuf;
21419 int obegv, ozv, same_region;
21421 /* Find the glyph under X/Y. */
21422 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21424 /* Look for :pointer property on image. */
21425 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21427 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21428 if (img != NULL && IMAGEP (img->spec))
21430 Lisp_Object image_map, hotspot;
21431 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21432 !NILP (image_map))
21433 && (hotspot = find_hot_spot (image_map,
21434 glyph->slice.x + dx,
21435 glyph->slice.y + dy),
21436 CONSP (hotspot))
21437 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21439 Lisp_Object area_id, plist;
21441 area_id = XCAR (hotspot);
21442 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21443 If so, we could look for mouse-enter, mouse-leave
21444 properties in PLIST (and do something...). */
21445 hotspot = XCDR (hotspot);
21446 if (CONSP (hotspot)
21447 && (plist = XCAR (hotspot), CONSP (plist)))
21449 pointer = Fplist_get (plist, Qpointer);
21450 if (NILP (pointer))
21451 pointer = Qhand;
21452 help_echo_string = Fplist_get (plist, Qhelp_echo);
21453 if (!NILP (help_echo_string))
21455 help_echo_window = window;
21456 help_echo_object = glyph->object;
21457 help_echo_pos = glyph->charpos;
21461 if (NILP (pointer))
21462 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21466 /* Clear mouse face if X/Y not over text. */
21467 if (glyph == NULL
21468 || area != TEXT_AREA
21469 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21471 if (clear_mouse_face (dpyinfo))
21472 cursor = No_Cursor;
21473 if (NILP (pointer))
21475 if (area != TEXT_AREA)
21476 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21477 else
21478 pointer = Vvoid_text_area_pointer;
21480 goto set_cursor;
21483 pos = glyph->charpos;
21484 object = glyph->object;
21485 if (!STRINGP (object) && !BUFFERP (object))
21486 goto set_cursor;
21488 /* If we get an out-of-range value, return now; avoid an error. */
21489 if (BUFFERP (object) && pos > BUF_Z (b))
21490 goto set_cursor;
21492 /* Make the window's buffer temporarily current for
21493 overlays_at and compute_char_face. */
21494 obuf = current_buffer;
21495 current_buffer = b;
21496 obegv = BEGV;
21497 ozv = ZV;
21498 BEGV = BEG;
21499 ZV = Z;
21501 /* Is this char mouse-active or does it have help-echo? */
21502 position = make_number (pos);
21504 if (BUFFERP (object))
21506 /* Put all the overlays we want in a vector in overlay_vec. */
21507 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21508 /* Sort overlays into increasing priority order. */
21509 noverlays = sort_overlays (overlay_vec, noverlays, w);
21511 else
21512 noverlays = 0;
21514 same_region = (EQ (window, dpyinfo->mouse_face_window)
21515 && vpos >= dpyinfo->mouse_face_beg_row
21516 && vpos <= dpyinfo->mouse_face_end_row
21517 && (vpos > dpyinfo->mouse_face_beg_row
21518 || hpos >= dpyinfo->mouse_face_beg_col)
21519 && (vpos < dpyinfo->mouse_face_end_row
21520 || hpos < dpyinfo->mouse_face_end_col
21521 || dpyinfo->mouse_face_past_end));
21523 if (same_region)
21524 cursor = No_Cursor;
21526 /* Check mouse-face highlighting. */
21527 if (! same_region
21528 /* If there exists an overlay with mouse-face overlapping
21529 the one we are currently highlighting, we have to
21530 check if we enter the overlapping overlay, and then
21531 highlight only that. */
21532 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21533 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21535 /* Find the highest priority overlay that has a mouse-face
21536 property. */
21537 overlay = Qnil;
21538 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21540 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21541 if (!NILP (mouse_face))
21542 overlay = overlay_vec[i];
21545 /* If we're actually highlighting the same overlay as
21546 before, there's no need to do that again. */
21547 if (!NILP (overlay)
21548 && EQ (overlay, dpyinfo->mouse_face_overlay))
21549 goto check_help_echo;
21551 dpyinfo->mouse_face_overlay = overlay;
21553 /* Clear the display of the old active region, if any. */
21554 if (clear_mouse_face (dpyinfo))
21555 cursor = No_Cursor;
21557 /* If no overlay applies, get a text property. */
21558 if (NILP (overlay))
21559 mouse_face = Fget_text_property (position, Qmouse_face, object);
21561 /* Handle the overlay case. */
21562 if (!NILP (overlay))
21564 /* Find the range of text around this char that
21565 should be active. */
21566 Lisp_Object before, after;
21567 int ignore;
21569 before = Foverlay_start (overlay);
21570 after = Foverlay_end (overlay);
21571 /* Record this as the current active region. */
21572 fast_find_position (w, XFASTINT (before),
21573 &dpyinfo->mouse_face_beg_col,
21574 &dpyinfo->mouse_face_beg_row,
21575 &dpyinfo->mouse_face_beg_x,
21576 &dpyinfo->mouse_face_beg_y, Qnil);
21578 dpyinfo->mouse_face_past_end
21579 = !fast_find_position (w, XFASTINT (after),
21580 &dpyinfo->mouse_face_end_col,
21581 &dpyinfo->mouse_face_end_row,
21582 &dpyinfo->mouse_face_end_x,
21583 &dpyinfo->mouse_face_end_y, Qnil);
21584 dpyinfo->mouse_face_window = window;
21586 dpyinfo->mouse_face_face_id
21587 = face_at_buffer_position (w, pos, 0, 0,
21588 &ignore, pos + 1,
21589 !dpyinfo->mouse_face_hidden);
21591 /* Display it as active. */
21592 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21593 cursor = No_Cursor;
21595 /* Handle the text property case. */
21596 else if (!NILP (mouse_face) && BUFFERP (object))
21598 /* Find the range of text around this char that
21599 should be active. */
21600 Lisp_Object before, after, beginning, end;
21601 int ignore;
21603 beginning = Fmarker_position (w->start);
21604 end = make_number (BUF_Z (XBUFFER (object))
21605 - XFASTINT (w->window_end_pos));
21606 before
21607 = Fprevious_single_property_change (make_number (pos + 1),
21608 Qmouse_face,
21609 object, beginning);
21610 after
21611 = Fnext_single_property_change (position, Qmouse_face,
21612 object, end);
21614 /* Record this as the current active region. */
21615 fast_find_position (w, XFASTINT (before),
21616 &dpyinfo->mouse_face_beg_col,
21617 &dpyinfo->mouse_face_beg_row,
21618 &dpyinfo->mouse_face_beg_x,
21619 &dpyinfo->mouse_face_beg_y, Qnil);
21620 dpyinfo->mouse_face_past_end
21621 = !fast_find_position (w, XFASTINT (after),
21622 &dpyinfo->mouse_face_end_col,
21623 &dpyinfo->mouse_face_end_row,
21624 &dpyinfo->mouse_face_end_x,
21625 &dpyinfo->mouse_face_end_y, Qnil);
21626 dpyinfo->mouse_face_window = window;
21628 if (BUFFERP (object))
21629 dpyinfo->mouse_face_face_id
21630 = face_at_buffer_position (w, pos, 0, 0,
21631 &ignore, pos + 1,
21632 !dpyinfo->mouse_face_hidden);
21634 /* Display it as active. */
21635 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21636 cursor = No_Cursor;
21638 else if (!NILP (mouse_face) && STRINGP (object))
21640 Lisp_Object b, e;
21641 int ignore;
21643 b = Fprevious_single_property_change (make_number (pos + 1),
21644 Qmouse_face,
21645 object, Qnil);
21646 e = Fnext_single_property_change (position, Qmouse_face,
21647 object, Qnil);
21648 if (NILP (b))
21649 b = make_number (0);
21650 if (NILP (e))
21651 e = make_number (SCHARS (object) - 1);
21652 fast_find_string_pos (w, XINT (b), object,
21653 &dpyinfo->mouse_face_beg_col,
21654 &dpyinfo->mouse_face_beg_row,
21655 &dpyinfo->mouse_face_beg_x,
21656 &dpyinfo->mouse_face_beg_y, 0);
21657 fast_find_string_pos (w, XINT (e), object,
21658 &dpyinfo->mouse_face_end_col,
21659 &dpyinfo->mouse_face_end_row,
21660 &dpyinfo->mouse_face_end_x,
21661 &dpyinfo->mouse_face_end_y, 1);
21662 dpyinfo->mouse_face_past_end = 0;
21663 dpyinfo->mouse_face_window = window;
21664 dpyinfo->mouse_face_face_id
21665 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21666 glyph->face_id, 1);
21667 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21668 cursor = No_Cursor;
21670 else if (STRINGP (object) && NILP (mouse_face))
21672 /* A string which doesn't have mouse-face, but
21673 the text ``under'' it might have. */
21674 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21675 int start = MATRIX_ROW_START_CHARPOS (r);
21677 pos = string_buffer_position (w, object, start);
21678 if (pos > 0)
21679 mouse_face = get_char_property_and_overlay (make_number (pos),
21680 Qmouse_face,
21681 w->buffer,
21682 &overlay);
21683 if (!NILP (mouse_face) && !NILP (overlay))
21685 Lisp_Object before = Foverlay_start (overlay);
21686 Lisp_Object after = Foverlay_end (overlay);
21687 int ignore;
21689 /* Note that we might not be able to find position
21690 BEFORE in the glyph matrix if the overlay is
21691 entirely covered by a `display' property. In
21692 this case, we overshoot. So let's stop in
21693 the glyph matrix before glyphs for OBJECT. */
21694 fast_find_position (w, XFASTINT (before),
21695 &dpyinfo->mouse_face_beg_col,
21696 &dpyinfo->mouse_face_beg_row,
21697 &dpyinfo->mouse_face_beg_x,
21698 &dpyinfo->mouse_face_beg_y,
21699 object);
21701 dpyinfo->mouse_face_past_end
21702 = !fast_find_position (w, XFASTINT (after),
21703 &dpyinfo->mouse_face_end_col,
21704 &dpyinfo->mouse_face_end_row,
21705 &dpyinfo->mouse_face_end_x,
21706 &dpyinfo->mouse_face_end_y,
21707 Qnil);
21708 dpyinfo->mouse_face_window = window;
21709 dpyinfo->mouse_face_face_id
21710 = face_at_buffer_position (w, pos, 0, 0,
21711 &ignore, pos + 1,
21712 !dpyinfo->mouse_face_hidden);
21714 /* Display it as active. */
21715 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21716 cursor = No_Cursor;
21721 check_help_echo:
21723 /* Look for a `help-echo' property. */
21724 if (NILP (help_echo_string)) {
21725 Lisp_Object help, overlay;
21727 /* Check overlays first. */
21728 help = overlay = Qnil;
21729 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21731 overlay = overlay_vec[i];
21732 help = Foverlay_get (overlay, Qhelp_echo);
21735 if (!NILP (help))
21737 help_echo_string = help;
21738 help_echo_window = window;
21739 help_echo_object = overlay;
21740 help_echo_pos = pos;
21742 else
21744 Lisp_Object object = glyph->object;
21745 int charpos = glyph->charpos;
21747 /* Try text properties. */
21748 if (STRINGP (object)
21749 && charpos >= 0
21750 && charpos < SCHARS (object))
21752 help = Fget_text_property (make_number (charpos),
21753 Qhelp_echo, object);
21754 if (NILP (help))
21756 /* If the string itself doesn't specify a help-echo,
21757 see if the buffer text ``under'' it does. */
21758 struct glyph_row *r
21759 = MATRIX_ROW (w->current_matrix, vpos);
21760 int start = MATRIX_ROW_START_CHARPOS (r);
21761 int pos = string_buffer_position (w, object, start);
21762 if (pos > 0)
21764 help = Fget_char_property (make_number (pos),
21765 Qhelp_echo, w->buffer);
21766 if (!NILP (help))
21768 charpos = pos;
21769 object = w->buffer;
21774 else if (BUFFERP (object)
21775 && charpos >= BEGV
21776 && charpos < ZV)
21777 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21778 object);
21780 if (!NILP (help))
21782 help_echo_string = help;
21783 help_echo_window = window;
21784 help_echo_object = object;
21785 help_echo_pos = charpos;
21790 /* Look for a `pointer' property. */
21791 if (NILP (pointer))
21793 /* Check overlays first. */
21794 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21795 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21797 if (NILP (pointer))
21799 Lisp_Object object = glyph->object;
21800 int charpos = glyph->charpos;
21802 /* Try text properties. */
21803 if (STRINGP (object)
21804 && charpos >= 0
21805 && charpos < SCHARS (object))
21807 pointer = Fget_text_property (make_number (charpos),
21808 Qpointer, object);
21809 if (NILP (pointer))
21811 /* If the string itself doesn't specify a pointer,
21812 see if the buffer text ``under'' it does. */
21813 struct glyph_row *r
21814 = MATRIX_ROW (w->current_matrix, vpos);
21815 int start = MATRIX_ROW_START_CHARPOS (r);
21816 int pos = string_buffer_position (w, object, start);
21817 if (pos > 0)
21818 pointer = Fget_char_property (make_number (pos),
21819 Qpointer, w->buffer);
21822 else if (BUFFERP (object)
21823 && charpos >= BEGV
21824 && charpos < ZV)
21825 pointer = Fget_text_property (make_number (charpos),
21826 Qpointer, object);
21830 BEGV = obegv;
21831 ZV = ozv;
21832 current_buffer = obuf;
21835 set_cursor:
21837 define_frame_cursor1 (f, cursor, pointer);
21841 /* EXPORT for RIF:
21842 Clear any mouse-face on window W. This function is part of the
21843 redisplay interface, and is called from try_window_id and similar
21844 functions to ensure the mouse-highlight is off. */
21846 void
21847 x_clear_window_mouse_face (w)
21848 struct window *w;
21850 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21851 Lisp_Object window;
21853 BLOCK_INPUT;
21854 XSETWINDOW (window, w);
21855 if (EQ (window, dpyinfo->mouse_face_window))
21856 clear_mouse_face (dpyinfo);
21857 UNBLOCK_INPUT;
21861 /* EXPORT:
21862 Just discard the mouse face information for frame F, if any.
21863 This is used when the size of F is changed. */
21865 void
21866 cancel_mouse_face (f)
21867 struct frame *f;
21869 Lisp_Object window;
21870 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21872 window = dpyinfo->mouse_face_window;
21873 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21875 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21876 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21877 dpyinfo->mouse_face_window = Qnil;
21882 #endif /* HAVE_WINDOW_SYSTEM */
21885 /***********************************************************************
21886 Exposure Events
21887 ***********************************************************************/
21889 #ifdef HAVE_WINDOW_SYSTEM
21891 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21892 which intersects rectangle R. R is in window-relative coordinates. */
21894 static void
21895 expose_area (w, row, r, area)
21896 struct window *w;
21897 struct glyph_row *row;
21898 XRectangle *r;
21899 enum glyph_row_area area;
21901 struct glyph *first = row->glyphs[area];
21902 struct glyph *end = row->glyphs[area] + row->used[area];
21903 struct glyph *last;
21904 int first_x, start_x, x;
21906 if (area == TEXT_AREA && row->fill_line_p)
21907 /* If row extends face to end of line write the whole line. */
21908 draw_glyphs (w, 0, row, area,
21909 0, row->used[area],
21910 DRAW_NORMAL_TEXT, 0);
21911 else
21913 /* Set START_X to the window-relative start position for drawing glyphs of
21914 AREA. The first glyph of the text area can be partially visible.
21915 The first glyphs of other areas cannot. */
21916 start_x = window_box_left_offset (w, area);
21917 x = start_x;
21918 if (area == TEXT_AREA)
21919 x += row->x;
21921 /* Find the first glyph that must be redrawn. */
21922 while (first < end
21923 && x + first->pixel_width < r->x)
21925 x += first->pixel_width;
21926 ++first;
21929 /* Find the last one. */
21930 last = first;
21931 first_x = x;
21932 while (last < end
21933 && x < r->x + r->width)
21935 x += last->pixel_width;
21936 ++last;
21939 /* Repaint. */
21940 if (last > first)
21941 draw_glyphs (w, first_x - start_x, row, area,
21942 first - row->glyphs[area], last - row->glyphs[area],
21943 DRAW_NORMAL_TEXT, 0);
21948 /* Redraw the parts of the glyph row ROW on window W intersecting
21949 rectangle R. R is in window-relative coordinates. Value is
21950 non-zero if mouse-face was overwritten. */
21952 static int
21953 expose_line (w, row, r)
21954 struct window *w;
21955 struct glyph_row *row;
21956 XRectangle *r;
21958 xassert (row->enabled_p);
21960 if (row->mode_line_p || w->pseudo_window_p)
21961 draw_glyphs (w, 0, row, TEXT_AREA,
21962 0, row->used[TEXT_AREA],
21963 DRAW_NORMAL_TEXT, 0);
21964 else
21966 if (row->used[LEFT_MARGIN_AREA])
21967 expose_area (w, row, r, LEFT_MARGIN_AREA);
21968 if (row->used[TEXT_AREA])
21969 expose_area (w, row, r, TEXT_AREA);
21970 if (row->used[RIGHT_MARGIN_AREA])
21971 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21972 draw_row_fringe_bitmaps (w, row);
21975 return row->mouse_face_p;
21979 /* Redraw those parts of glyphs rows during expose event handling that
21980 overlap other rows. Redrawing of an exposed line writes over parts
21981 of lines overlapping that exposed line; this function fixes that.
21983 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21984 row in W's current matrix that is exposed and overlaps other rows.
21985 LAST_OVERLAPPING_ROW is the last such row. */
21987 static void
21988 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21989 struct window *w;
21990 struct glyph_row *first_overlapping_row;
21991 struct glyph_row *last_overlapping_row;
21993 struct glyph_row *row;
21995 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21996 if (row->overlapping_p)
21998 xassert (row->enabled_p && !row->mode_line_p);
22000 if (row->used[LEFT_MARGIN_AREA])
22001 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22003 if (row->used[TEXT_AREA])
22004 x_fix_overlapping_area (w, row, TEXT_AREA);
22006 if (row->used[RIGHT_MARGIN_AREA])
22007 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22012 /* Return non-zero if W's cursor intersects rectangle R. */
22014 static int
22015 phys_cursor_in_rect_p (w, r)
22016 struct window *w;
22017 XRectangle *r;
22019 XRectangle cr, result;
22020 struct glyph *cursor_glyph;
22022 cursor_glyph = get_phys_cursor_glyph (w);
22023 if (cursor_glyph)
22025 /* r is relative to W's box, but w->phys_cursor.x is relative
22026 to left edge of W's TEXT area. Adjust it. */
22027 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22028 cr.y = w->phys_cursor.y;
22029 cr.width = cursor_glyph->pixel_width;
22030 cr.height = w->phys_cursor_height;
22031 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22032 I assume the effect is the same -- and this is portable. */
22033 return x_intersect_rectangles (&cr, r, &result);
22035 else
22036 return 0;
22040 /* EXPORT:
22041 Draw a vertical window border to the right of window W if W doesn't
22042 have vertical scroll bars. */
22044 void
22045 x_draw_vertical_border (w)
22046 struct window *w;
22048 /* We could do better, if we knew what type of scroll-bar the adjacent
22049 windows (on either side) have... But we don't :-(
22050 However, I think this works ok. ++KFS 2003-04-25 */
22052 /* Redraw borders between horizontally adjacent windows. Don't
22053 do it for frames with vertical scroll bars because either the
22054 right scroll bar of a window, or the left scroll bar of its
22055 neighbor will suffice as a border. */
22056 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22057 return;
22059 if (!WINDOW_RIGHTMOST_P (w)
22060 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22062 int x0, x1, y0, y1;
22064 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22065 y1 -= 1;
22067 rif->draw_vertical_window_border (w, x1, y0, y1);
22069 else if (!WINDOW_LEFTMOST_P (w)
22070 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22072 int x0, x1, y0, y1;
22074 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22075 y1 -= 1;
22077 rif->draw_vertical_window_border (w, x0, y0, y1);
22082 /* Redraw the part of window W intersection rectangle FR. Pixel
22083 coordinates in FR are frame-relative. Call this function with
22084 input blocked. Value is non-zero if the exposure overwrites
22085 mouse-face. */
22087 static int
22088 expose_window (w, fr)
22089 struct window *w;
22090 XRectangle *fr;
22092 struct frame *f = XFRAME (w->frame);
22093 XRectangle wr, r;
22094 int mouse_face_overwritten_p = 0;
22096 /* If window is not yet fully initialized, do nothing. This can
22097 happen when toolkit scroll bars are used and a window is split.
22098 Reconfiguring the scroll bar will generate an expose for a newly
22099 created window. */
22100 if (w->current_matrix == NULL)
22101 return 0;
22103 /* When we're currently updating the window, display and current
22104 matrix usually don't agree. Arrange for a thorough display
22105 later. */
22106 if (w == updated_window)
22108 SET_FRAME_GARBAGED (f);
22109 return 0;
22112 /* Frame-relative pixel rectangle of W. */
22113 wr.x = WINDOW_LEFT_EDGE_X (w);
22114 wr.y = WINDOW_TOP_EDGE_Y (w);
22115 wr.width = WINDOW_TOTAL_WIDTH (w);
22116 wr.height = WINDOW_TOTAL_HEIGHT (w);
22118 if (x_intersect_rectangles (fr, &wr, &r))
22120 int yb = window_text_bottom_y (w);
22121 struct glyph_row *row;
22122 int cursor_cleared_p;
22123 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22125 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22126 r.x, r.y, r.width, r.height));
22128 /* Convert to window coordinates. */
22129 r.x -= WINDOW_LEFT_EDGE_X (w);
22130 r.y -= WINDOW_TOP_EDGE_Y (w);
22132 /* Turn off the cursor. */
22133 if (!w->pseudo_window_p
22134 && phys_cursor_in_rect_p (w, &r))
22136 x_clear_cursor (w);
22137 cursor_cleared_p = 1;
22139 else
22140 cursor_cleared_p = 0;
22142 /* Update lines intersecting rectangle R. */
22143 first_overlapping_row = last_overlapping_row = NULL;
22144 for (row = w->current_matrix->rows;
22145 row->enabled_p;
22146 ++row)
22148 int y0 = row->y;
22149 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22151 if ((y0 >= r.y && y0 < r.y + r.height)
22152 || (y1 > r.y && y1 < r.y + r.height)
22153 || (r.y >= y0 && r.y < y1)
22154 || (r.y + r.height > y0 && r.y + r.height < y1))
22156 /* A header line may be overlapping, but there is no need
22157 to fix overlapping areas for them. KFS 2005-02-12 */
22158 if (row->overlapping_p && !row->mode_line_p)
22160 if (first_overlapping_row == NULL)
22161 first_overlapping_row = row;
22162 last_overlapping_row = row;
22165 if (expose_line (w, row, &r))
22166 mouse_face_overwritten_p = 1;
22169 if (y1 >= yb)
22170 break;
22173 /* Display the mode line if there is one. */
22174 if (WINDOW_WANTS_MODELINE_P (w)
22175 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22176 row->enabled_p)
22177 && row->y < r.y + r.height)
22179 if (expose_line (w, row, &r))
22180 mouse_face_overwritten_p = 1;
22183 if (!w->pseudo_window_p)
22185 /* Fix the display of overlapping rows. */
22186 if (first_overlapping_row)
22187 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22189 /* Draw border between windows. */
22190 x_draw_vertical_border (w);
22192 /* Turn the cursor on again. */
22193 if (cursor_cleared_p)
22194 update_window_cursor (w, 1);
22198 return mouse_face_overwritten_p;
22203 /* Redraw (parts) of all windows in the window tree rooted at W that
22204 intersect R. R contains frame pixel coordinates. Value is
22205 non-zero if the exposure overwrites mouse-face. */
22207 static int
22208 expose_window_tree (w, r)
22209 struct window *w;
22210 XRectangle *r;
22212 struct frame *f = XFRAME (w->frame);
22213 int mouse_face_overwritten_p = 0;
22215 while (w && !FRAME_GARBAGED_P (f))
22217 if (!NILP (w->hchild))
22218 mouse_face_overwritten_p
22219 |= expose_window_tree (XWINDOW (w->hchild), r);
22220 else if (!NILP (w->vchild))
22221 mouse_face_overwritten_p
22222 |= expose_window_tree (XWINDOW (w->vchild), r);
22223 else
22224 mouse_face_overwritten_p |= expose_window (w, r);
22226 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22229 return mouse_face_overwritten_p;
22233 /* EXPORT:
22234 Redisplay an exposed area of frame F. X and Y are the upper-left
22235 corner of the exposed rectangle. W and H are width and height of
22236 the exposed area. All are pixel values. W or H zero means redraw
22237 the entire frame. */
22239 void
22240 expose_frame (f, x, y, w, h)
22241 struct frame *f;
22242 int x, y, w, h;
22244 XRectangle r;
22245 int mouse_face_overwritten_p = 0;
22247 TRACE ((stderr, "expose_frame "));
22249 /* No need to redraw if frame will be redrawn soon. */
22250 if (FRAME_GARBAGED_P (f))
22252 TRACE ((stderr, " garbaged\n"));
22253 return;
22256 /* If basic faces haven't been realized yet, there is no point in
22257 trying to redraw anything. This can happen when we get an expose
22258 event while Emacs is starting, e.g. by moving another window. */
22259 if (FRAME_FACE_CACHE (f) == NULL
22260 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22262 TRACE ((stderr, " no faces\n"));
22263 return;
22266 if (w == 0 || h == 0)
22268 r.x = r.y = 0;
22269 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22270 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22272 else
22274 r.x = x;
22275 r.y = y;
22276 r.width = w;
22277 r.height = h;
22280 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22281 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22283 if (WINDOWP (f->tool_bar_window))
22284 mouse_face_overwritten_p
22285 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22287 #ifdef HAVE_X_WINDOWS
22288 #ifndef MSDOS
22289 #ifndef USE_X_TOOLKIT
22290 if (WINDOWP (f->menu_bar_window))
22291 mouse_face_overwritten_p
22292 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22293 #endif /* not USE_X_TOOLKIT */
22294 #endif
22295 #endif
22297 /* Some window managers support a focus-follows-mouse style with
22298 delayed raising of frames. Imagine a partially obscured frame,
22299 and moving the mouse into partially obscured mouse-face on that
22300 frame. The visible part of the mouse-face will be highlighted,
22301 then the WM raises the obscured frame. With at least one WM, KDE
22302 2.1, Emacs is not getting any event for the raising of the frame
22303 (even tried with SubstructureRedirectMask), only Expose events.
22304 These expose events will draw text normally, i.e. not
22305 highlighted. Which means we must redo the highlight here.
22306 Subsume it under ``we love X''. --gerd 2001-08-15 */
22307 /* Included in Windows version because Windows most likely does not
22308 do the right thing if any third party tool offers
22309 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22310 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22312 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22313 if (f == dpyinfo->mouse_face_mouse_frame)
22315 int x = dpyinfo->mouse_face_mouse_x;
22316 int y = dpyinfo->mouse_face_mouse_y;
22317 clear_mouse_face (dpyinfo);
22318 note_mouse_highlight (f, x, y);
22324 /* EXPORT:
22325 Determine the intersection of two rectangles R1 and R2. Return
22326 the intersection in *RESULT. Value is non-zero if RESULT is not
22327 empty. */
22330 x_intersect_rectangles (r1, r2, result)
22331 XRectangle *r1, *r2, *result;
22333 XRectangle *left, *right;
22334 XRectangle *upper, *lower;
22335 int intersection_p = 0;
22337 /* Rearrange so that R1 is the left-most rectangle. */
22338 if (r1->x < r2->x)
22339 left = r1, right = r2;
22340 else
22341 left = r2, right = r1;
22343 /* X0 of the intersection is right.x0, if this is inside R1,
22344 otherwise there is no intersection. */
22345 if (right->x <= left->x + left->width)
22347 result->x = right->x;
22349 /* The right end of the intersection is the minimum of the
22350 the right ends of left and right. */
22351 result->width = (min (left->x + left->width, right->x + right->width)
22352 - result->x);
22354 /* Same game for Y. */
22355 if (r1->y < r2->y)
22356 upper = r1, lower = r2;
22357 else
22358 upper = r2, lower = r1;
22360 /* The upper end of the intersection is lower.y0, if this is inside
22361 of upper. Otherwise, there is no intersection. */
22362 if (lower->y <= upper->y + upper->height)
22364 result->y = lower->y;
22366 /* The lower end of the intersection is the minimum of the lower
22367 ends of upper and lower. */
22368 result->height = (min (lower->y + lower->height,
22369 upper->y + upper->height)
22370 - result->y);
22371 intersection_p = 1;
22375 return intersection_p;
22378 #endif /* HAVE_WINDOW_SYSTEM */
22381 /***********************************************************************
22382 Initialization
22383 ***********************************************************************/
22385 void
22386 syms_of_xdisp ()
22388 Vwith_echo_area_save_vector = Qnil;
22389 staticpro (&Vwith_echo_area_save_vector);
22391 Vmessage_stack = Qnil;
22392 staticpro (&Vmessage_stack);
22394 Qinhibit_redisplay = intern ("inhibit-redisplay");
22395 staticpro (&Qinhibit_redisplay);
22397 message_dolog_marker1 = Fmake_marker ();
22398 staticpro (&message_dolog_marker1);
22399 message_dolog_marker2 = Fmake_marker ();
22400 staticpro (&message_dolog_marker2);
22401 message_dolog_marker3 = Fmake_marker ();
22402 staticpro (&message_dolog_marker3);
22404 #if GLYPH_DEBUG
22405 defsubr (&Sdump_frame_glyph_matrix);
22406 defsubr (&Sdump_glyph_matrix);
22407 defsubr (&Sdump_glyph_row);
22408 defsubr (&Sdump_tool_bar_row);
22409 defsubr (&Strace_redisplay);
22410 defsubr (&Strace_to_stderr);
22411 #endif
22412 #ifdef HAVE_WINDOW_SYSTEM
22413 defsubr (&Stool_bar_lines_needed);
22414 defsubr (&Slookup_image_map);
22415 #endif
22416 defsubr (&Sformat_mode_line);
22418 staticpro (&Qmenu_bar_update_hook);
22419 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22421 staticpro (&Qoverriding_terminal_local_map);
22422 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22424 staticpro (&Qoverriding_local_map);
22425 Qoverriding_local_map = intern ("overriding-local-map");
22427 staticpro (&Qwindow_scroll_functions);
22428 Qwindow_scroll_functions = intern ("window-scroll-functions");
22430 staticpro (&Qredisplay_end_trigger_functions);
22431 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22433 staticpro (&Qinhibit_point_motion_hooks);
22434 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22436 QCdata = intern (":data");
22437 staticpro (&QCdata);
22438 Qdisplay = intern ("display");
22439 staticpro (&Qdisplay);
22440 Qspace_width = intern ("space-width");
22441 staticpro (&Qspace_width);
22442 Qraise = intern ("raise");
22443 staticpro (&Qraise);
22444 Qslice = intern ("slice");
22445 staticpro (&Qslice);
22446 Qspace = intern ("space");
22447 staticpro (&Qspace);
22448 Qmargin = intern ("margin");
22449 staticpro (&Qmargin);
22450 Qpointer = intern ("pointer");
22451 staticpro (&Qpointer);
22452 Qleft_margin = intern ("left-margin");
22453 staticpro (&Qleft_margin);
22454 Qright_margin = intern ("right-margin");
22455 staticpro (&Qright_margin);
22456 Qcenter = intern ("center");
22457 staticpro (&Qcenter);
22458 Qline_height = intern ("line-height");
22459 staticpro (&Qline_height);
22460 QCalign_to = intern (":align-to");
22461 staticpro (&QCalign_to);
22462 QCrelative_width = intern (":relative-width");
22463 staticpro (&QCrelative_width);
22464 QCrelative_height = intern (":relative-height");
22465 staticpro (&QCrelative_height);
22466 QCeval = intern (":eval");
22467 staticpro (&QCeval);
22468 QCpropertize = intern (":propertize");
22469 staticpro (&QCpropertize);
22470 QCfile = intern (":file");
22471 staticpro (&QCfile);
22472 Qfontified = intern ("fontified");
22473 staticpro (&Qfontified);
22474 Qfontification_functions = intern ("fontification-functions");
22475 staticpro (&Qfontification_functions);
22476 Qtrailing_whitespace = intern ("trailing-whitespace");
22477 staticpro (&Qtrailing_whitespace);
22478 Qescape_glyph = intern ("escape-glyph");
22479 staticpro (&Qescape_glyph);
22480 Qimage = intern ("image");
22481 staticpro (&Qimage);
22482 QCmap = intern (":map");
22483 staticpro (&QCmap);
22484 QCpointer = intern (":pointer");
22485 staticpro (&QCpointer);
22486 Qrect = intern ("rect");
22487 staticpro (&Qrect);
22488 Qcircle = intern ("circle");
22489 staticpro (&Qcircle);
22490 Qpoly = intern ("poly");
22491 staticpro (&Qpoly);
22492 Qmessage_truncate_lines = intern ("message-truncate-lines");
22493 staticpro (&Qmessage_truncate_lines);
22494 Qgrow_only = intern ("grow-only");
22495 staticpro (&Qgrow_only);
22496 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22497 staticpro (&Qinhibit_menubar_update);
22498 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22499 staticpro (&Qinhibit_eval_during_redisplay);
22500 Qposition = intern ("position");
22501 staticpro (&Qposition);
22502 Qbuffer_position = intern ("buffer-position");
22503 staticpro (&Qbuffer_position);
22504 Qobject = intern ("object");
22505 staticpro (&Qobject);
22506 Qbar = intern ("bar");
22507 staticpro (&Qbar);
22508 Qhbar = intern ("hbar");
22509 staticpro (&Qhbar);
22510 Qbox = intern ("box");
22511 staticpro (&Qbox);
22512 Qhollow = intern ("hollow");
22513 staticpro (&Qhollow);
22514 Qhand = intern ("hand");
22515 staticpro (&Qhand);
22516 Qarrow = intern ("arrow");
22517 staticpro (&Qarrow);
22518 Qtext = intern ("text");
22519 staticpro (&Qtext);
22520 Qrisky_local_variable = intern ("risky-local-variable");
22521 staticpro (&Qrisky_local_variable);
22522 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22523 staticpro (&Qinhibit_free_realized_faces);
22525 list_of_error = Fcons (Fcons (intern ("error"),
22526 Fcons (intern ("void-variable"), Qnil)),
22527 Qnil);
22528 staticpro (&list_of_error);
22530 Qlast_arrow_position = intern ("last-arrow-position");
22531 staticpro (&Qlast_arrow_position);
22532 Qlast_arrow_string = intern ("last-arrow-string");
22533 staticpro (&Qlast_arrow_string);
22535 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22536 staticpro (&Qoverlay_arrow_string);
22537 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22538 staticpro (&Qoverlay_arrow_bitmap);
22540 echo_buffer[0] = echo_buffer[1] = Qnil;
22541 staticpro (&echo_buffer[0]);
22542 staticpro (&echo_buffer[1]);
22544 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22545 staticpro (&echo_area_buffer[0]);
22546 staticpro (&echo_area_buffer[1]);
22548 Vmessages_buffer_name = build_string ("*Messages*");
22549 staticpro (&Vmessages_buffer_name);
22551 mode_line_proptrans_alist = Qnil;
22552 staticpro (&mode_line_proptrans_alist);
22554 mode_line_string_list = Qnil;
22555 staticpro (&mode_line_string_list);
22557 help_echo_string = Qnil;
22558 staticpro (&help_echo_string);
22559 help_echo_object = Qnil;
22560 staticpro (&help_echo_object);
22561 help_echo_window = Qnil;
22562 staticpro (&help_echo_window);
22563 previous_help_echo_string = Qnil;
22564 staticpro (&previous_help_echo_string);
22565 help_echo_pos = -1;
22567 #ifdef HAVE_WINDOW_SYSTEM
22568 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22569 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22570 For example, if a block cursor is over a tab, it will be drawn as
22571 wide as that tab on the display. */);
22572 x_stretch_cursor_p = 0;
22573 #endif
22575 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22576 doc: /* *Non-nil means highlight trailing whitespace.
22577 The face used for trailing whitespace is `trailing-whitespace'. */);
22578 Vshow_trailing_whitespace = Qnil;
22580 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22581 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22582 Vshow_nonbreak_escape = Qt;
22584 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22585 doc: /* *The pointer shape to show in void text areas.
22586 Nil means to show the text pointer. Other options are `arrow', `text',
22587 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22588 Vvoid_text_area_pointer = Qarrow;
22590 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22591 doc: /* Non-nil means don't actually do any redisplay.
22592 This is used for internal purposes. */);
22593 Vinhibit_redisplay = Qnil;
22595 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22596 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22597 Vglobal_mode_string = Qnil;
22599 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22600 doc: /* Marker for where to display an arrow on top of the buffer text.
22601 This must be the beginning of a line in order to work.
22602 See also `overlay-arrow-string'. */);
22603 Voverlay_arrow_position = Qnil;
22605 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22606 doc: /* String to display as an arrow in non-window frames.
22607 See also `overlay-arrow-position'. */);
22608 Voverlay_arrow_string = build_string ("=>");
22610 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22611 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22612 The symbols on this list are examined during redisplay to determine
22613 where to display overlay arrows. */);
22614 Voverlay_arrow_variable_list
22615 = Fcons (intern ("overlay-arrow-position"), Qnil);
22617 DEFVAR_INT ("scroll-step", &scroll_step,
22618 doc: /* *The number of lines to try scrolling a window by when point moves out.
22619 If that fails to bring point back on frame, point is centered instead.
22620 If this is zero, point is always centered after it moves off frame.
22621 If you want scrolling to always be a line at a time, you should set
22622 `scroll-conservatively' to a large value rather than set this to 1. */);
22624 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22625 doc: /* *Scroll up to this many lines, to bring point back on screen.
22626 A value of zero means to scroll the text to center point vertically
22627 in the window. */);
22628 scroll_conservatively = 0;
22630 DEFVAR_INT ("scroll-margin", &scroll_margin,
22631 doc: /* *Number of lines of margin at the top and bottom of a window.
22632 Recenter the window whenever point gets within this many lines
22633 of the top or bottom of the window. */);
22634 scroll_margin = 0;
22636 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22637 doc: /* Pixels per inch on current display.
22638 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22639 Vdisplay_pixels_per_inch = make_float (72.0);
22641 #if GLYPH_DEBUG
22642 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22643 #endif
22645 DEFVAR_BOOL ("truncate-partial-width-windows",
22646 &truncate_partial_width_windows,
22647 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22648 truncate_partial_width_windows = 1;
22650 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22651 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22652 Any other value means to use the appropriate face, `mode-line',
22653 `header-line', or `menu' respectively. */);
22654 mode_line_inverse_video = 1;
22656 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22657 doc: /* *Maximum buffer size for which line number should be displayed.
22658 If the buffer is bigger than this, the line number does not appear
22659 in the mode line. A value of nil means no limit. */);
22660 Vline_number_display_limit = Qnil;
22662 DEFVAR_INT ("line-number-display-limit-width",
22663 &line_number_display_limit_width,
22664 doc: /* *Maximum line width (in characters) for line number display.
22665 If the average length of the lines near point is bigger than this, then the
22666 line number may be omitted from the mode line. */);
22667 line_number_display_limit_width = 200;
22669 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22670 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22671 highlight_nonselected_windows = 0;
22673 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22674 doc: /* Non-nil if more than one frame is visible on this display.
22675 Minibuffer-only frames don't count, but iconified frames do.
22676 This variable is not guaranteed to be accurate except while processing
22677 `frame-title-format' and `icon-title-format'. */);
22679 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22680 doc: /* Template for displaying the title bar of visible frames.
22681 \(Assuming the window manager supports this feature.)
22682 This variable has the same structure as `mode-line-format' (which see),
22683 and is used only on frames for which no explicit name has been set
22684 \(see `modify-frame-parameters'). */);
22686 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22687 doc: /* Template for displaying the title bar of an iconified frame.
22688 \(Assuming the window manager supports this feature.)
22689 This variable has the same structure as `mode-line-format' (which see),
22690 and is used only on frames for which no explicit name has been set
22691 \(see `modify-frame-parameters'). */);
22692 Vicon_title_format
22693 = Vframe_title_format
22694 = Fcons (intern ("multiple-frames"),
22695 Fcons (build_string ("%b"),
22696 Fcons (Fcons (empty_string,
22697 Fcons (intern ("invocation-name"),
22698 Fcons (build_string ("@"),
22699 Fcons (intern ("system-name"),
22700 Qnil)))),
22701 Qnil)));
22703 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22704 doc: /* Maximum number of lines to keep in the message log buffer.
22705 If nil, disable message logging. If t, log messages but don't truncate
22706 the buffer when it becomes large. */);
22707 Vmessage_log_max = make_number (50);
22709 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22710 doc: /* Functions called before redisplay, if window sizes have changed.
22711 The value should be a list of functions that take one argument.
22712 Just before redisplay, for each frame, if any of its windows have changed
22713 size since the last redisplay, or have been split or deleted,
22714 all the functions in the list are called, with the frame as argument. */);
22715 Vwindow_size_change_functions = Qnil;
22717 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22718 doc: /* List of functions to call before redisplaying a window with scrolling.
22719 Each function is called with two arguments, the window
22720 and its new display-start position. Note that the value of `window-end'
22721 is not valid when these functions are called. */);
22722 Vwindow_scroll_functions = Qnil;
22724 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22725 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22726 mouse_autoselect_window = 0;
22728 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22729 doc: /* *Non-nil means automatically resize tool-bars.
22730 This increases a tool-bar's height if not all tool-bar items are visible.
22731 It decreases a tool-bar's height when it would display blank lines
22732 otherwise. */);
22733 auto_resize_tool_bars_p = 1;
22735 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22736 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22737 auto_raise_tool_bar_buttons_p = 1;
22739 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22740 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22741 make_cursor_line_fully_visible_p = 1;
22743 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22744 doc: /* *Margin around tool-bar buttons in pixels.
22745 If an integer, use that for both horizontal and vertical margins.
22746 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22747 HORZ specifying the horizontal margin, and VERT specifying the
22748 vertical margin. */);
22749 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22751 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22752 doc: /* *Relief thickness of tool-bar buttons. */);
22753 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22755 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22756 doc: /* List of functions to call to fontify regions of text.
22757 Each function is called with one argument POS. Functions must
22758 fontify a region starting at POS in the current buffer, and give
22759 fontified regions the property `fontified'. */);
22760 Vfontification_functions = Qnil;
22761 Fmake_variable_buffer_local (Qfontification_functions);
22763 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22764 &unibyte_display_via_language_environment,
22765 doc: /* *Non-nil means display unibyte text according to language environment.
22766 Specifically this means that unibyte non-ASCII characters
22767 are displayed by converting them to the equivalent multibyte characters
22768 according to the current language environment. As a result, they are
22769 displayed according to the current fontset. */);
22770 unibyte_display_via_language_environment = 0;
22772 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22773 doc: /* *Maximum height for resizing mini-windows.
22774 If a float, it specifies a fraction of the mini-window frame's height.
22775 If an integer, it specifies a number of lines. */);
22776 Vmax_mini_window_height = make_float (0.25);
22778 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22779 doc: /* *How to resize mini-windows.
22780 A value of nil means don't automatically resize mini-windows.
22781 A value of t means resize them to fit the text displayed in them.
22782 A value of `grow-only', the default, means let mini-windows grow
22783 only, until their display becomes empty, at which point the windows
22784 go back to their normal size. */);
22785 Vresize_mini_windows = Qgrow_only;
22787 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22788 doc: /* Alist specifying how to blink the cursor off.
22789 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22790 `cursor-type' frame-parameter or variable equals ON-STATE,
22791 comparing using `equal', Emacs uses OFF-STATE to specify
22792 how to blink it off. */);
22793 Vblink_cursor_alist = Qnil;
22795 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22796 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22797 automatic_hscrolling_p = 1;
22799 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22800 doc: /* *How many columns away from the window edge point is allowed to get
22801 before automatic hscrolling will horizontally scroll the window. */);
22802 hscroll_margin = 5;
22804 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22805 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22806 When point is less than `automatic-hscroll-margin' columns from the window
22807 edge, automatic hscrolling will scroll the window by the amount of columns
22808 determined by this variable. If its value is a positive integer, scroll that
22809 many columns. If it's a positive floating-point number, it specifies the
22810 fraction of the window's width to scroll. If it's nil or zero, point will be
22811 centered horizontally after the scroll. Any other value, including negative
22812 numbers, are treated as if the value were zero.
22814 Automatic hscrolling always moves point outside the scroll margin, so if
22815 point was more than scroll step columns inside the margin, the window will
22816 scroll more than the value given by the scroll step.
22818 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22819 and `scroll-right' overrides this variable's effect. */);
22820 Vhscroll_step = make_number (0);
22822 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22823 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22824 Bind this around calls to `message' to let it take effect. */);
22825 message_truncate_lines = 0;
22827 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22828 doc: /* Normal hook run to update the menu bar definitions.
22829 Redisplay runs this hook before it redisplays the menu bar.
22830 This is used to update submenus such as Buffers,
22831 whose contents depend on various data. */);
22832 Vmenu_bar_update_hook = Qnil;
22834 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22835 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22836 inhibit_menubar_update = 0;
22838 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22839 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22840 inhibit_eval_during_redisplay = 0;
22842 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22843 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22844 inhibit_free_realized_faces = 0;
22846 #if GLYPH_DEBUG
22847 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22848 doc: /* Inhibit try_window_id display optimization. */);
22849 inhibit_try_window_id = 0;
22851 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22852 doc: /* Inhibit try_window_reusing display optimization. */);
22853 inhibit_try_window_reusing = 0;
22855 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22856 doc: /* Inhibit try_cursor_movement display optimization. */);
22857 inhibit_try_cursor_movement = 0;
22858 #endif /* GLYPH_DEBUG */
22862 /* Initialize this module when Emacs starts. */
22864 void
22865 init_xdisp ()
22867 Lisp_Object root_window;
22868 struct window *mini_w;
22870 current_header_line_height = current_mode_line_height = -1;
22872 CHARPOS (this_line_start_pos) = 0;
22874 mini_w = XWINDOW (minibuf_window);
22875 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22877 if (!noninteractive)
22879 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22880 int i;
22882 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22883 set_window_height (root_window,
22884 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22886 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22887 set_window_height (minibuf_window, 1, 0);
22889 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22890 mini_w->total_cols = make_number (FRAME_COLS (f));
22892 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22893 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22894 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22896 /* The default ellipsis glyphs `...'. */
22897 for (i = 0; i < 3; ++i)
22898 default_invis_vector[i] = make_number ('.');
22902 /* Allocate the buffer for frame titles.
22903 Also used for `format-mode-line'. */
22904 int size = 100;
22905 frame_title_buf = (char *) xmalloc (size);
22906 frame_title_buf_end = frame_title_buf + size;
22907 frame_title_ptr = NULL;
22910 help_echo_showing_p = 0;
22914 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22915 (do not change this comment) */