Add example of grep output with context lines.
[emacs.git] / src / xdisp.c
blob09cf8a7f5de989a9d1bac7998aebe743fe5ade99
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 Vnobreak_char_display;
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 /* Name and number of the face used to highlight non-breaking spaces. */
355 Lisp_Object Qnobreak_space;
357 /* The symbol `image' which is the car of the lists used to represent
358 images in Lisp. */
360 Lisp_Object Qimage;
362 /* The image map types. */
363 Lisp_Object QCmap, QCpointer;
364 Lisp_Object Qrect, Qcircle, Qpoly;
366 /* Non-zero means print newline to stdout before next mini-buffer
367 message. */
369 int noninteractive_need_newline;
371 /* Non-zero means print newline to message log before next message. */
373 static int message_log_need_newline;
375 /* Three markers that message_dolog uses.
376 It could allocate them itself, but that causes trouble
377 in handling memory-full errors. */
378 static Lisp_Object message_dolog_marker1;
379 static Lisp_Object message_dolog_marker2;
380 static Lisp_Object message_dolog_marker3;
382 /* The buffer position of the first character appearing entirely or
383 partially on the line of the selected window which contains the
384 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
385 redisplay optimization in redisplay_internal. */
387 static struct text_pos this_line_start_pos;
389 /* Number of characters past the end of the line above, including the
390 terminating newline. */
392 static struct text_pos this_line_end_pos;
394 /* The vertical positions and the height of this line. */
396 static int this_line_vpos;
397 static int this_line_y;
398 static int this_line_pixel_height;
400 /* X position at which this display line starts. Usually zero;
401 negative if first character is partially visible. */
403 static int this_line_start_x;
405 /* Buffer that this_line_.* variables are referring to. */
407 static struct buffer *this_line_buffer;
409 /* Nonzero means truncate lines in all windows less wide than the
410 frame. */
412 int truncate_partial_width_windows;
414 /* A flag to control how to display unibyte 8-bit character. */
416 int unibyte_display_via_language_environment;
418 /* Nonzero means we have more than one non-mini-buffer-only frame.
419 Not guaranteed to be accurate except while parsing
420 frame-title-format. */
422 int multiple_frames;
424 Lisp_Object Vglobal_mode_string;
427 /* List of variables (symbols) which hold markers for overlay arrows.
428 The symbols on this list are examined during redisplay to determine
429 where to display overlay arrows. */
431 Lisp_Object Voverlay_arrow_variable_list;
433 /* Marker for where to display an arrow on top of the buffer text. */
435 Lisp_Object Voverlay_arrow_position;
437 /* String to display for the arrow. Only used on terminal frames. */
439 Lisp_Object Voverlay_arrow_string;
441 /* Values of those variables at last redisplay are stored as
442 properties on `overlay-arrow-position' symbol. However, if
443 Voverlay_arrow_position is a marker, last-arrow-position is its
444 numerical position. */
446 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
449 properties on a symbol in overlay-arrow-variable-list. */
451 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453 /* Like mode-line-format, but for the title bar on a visible frame. */
455 Lisp_Object Vframe_title_format;
457 /* Like mode-line-format, but for the title bar on an iconified frame. */
459 Lisp_Object Vicon_title_format;
461 /* List of functions to call when a window's size changes. These
462 functions get one arg, a frame on which one or more windows' sizes
463 have changed. */
465 static Lisp_Object Vwindow_size_change_functions;
467 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469 /* Nonzero if an overlay arrow has been displayed in this window. */
471 static int overlay_arrow_seen;
473 /* Nonzero means highlight the region even in nonselected windows. */
475 int highlight_nonselected_windows;
477 /* If cursor motion alone moves point off frame, try scrolling this
478 many lines up or down if that will bring it back. */
480 static EMACS_INT scroll_step;
482 /* Nonzero means scroll just far enough to bring point back on the
483 screen, when appropriate. */
485 static EMACS_INT scroll_conservatively;
487 /* Recenter the window whenever point gets within this many lines of
488 the top or bottom of the window. This value is translated into a
489 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
490 that there is really a fixed pixel height scroll margin. */
492 EMACS_INT scroll_margin;
494 /* Number of windows showing the buffer of the selected window (or
495 another buffer with the same base buffer). keyboard.c refers to
496 this. */
498 int buffer_shared;
500 /* Vector containing glyphs for an ellipsis `...'. */
502 static Lisp_Object default_invis_vector[3];
504 /* Zero means display the mode-line/header-line/menu-bar in the default face
505 (this slightly odd definition is for compatibility with previous versions
506 of emacs), non-zero means display them using their respective faces.
508 This variable is deprecated. */
510 int mode_line_inverse_video;
512 /* Prompt to display in front of the mini-buffer contents. */
514 Lisp_Object minibuf_prompt;
516 /* Width of current mini-buffer prompt. Only set after display_line
517 of the line that contains the prompt. */
519 int minibuf_prompt_width;
521 /* This is the window where the echo area message was displayed. It
522 is always a mini-buffer window, but it may not be the same window
523 currently active as a mini-buffer. */
525 Lisp_Object echo_area_window;
527 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
528 pushes the current message and the value of
529 message_enable_multibyte on the stack, the function restore_message
530 pops the stack and displays MESSAGE again. */
532 Lisp_Object Vmessage_stack;
534 /* Nonzero means multibyte characters were enabled when the echo area
535 message was specified. */
537 int message_enable_multibyte;
539 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541 int update_mode_lines;
543 /* Nonzero if window sizes or contents have changed since last
544 redisplay that finished. */
546 int windows_or_buffers_changed;
548 /* Nonzero means a frame's cursor type has been changed. */
550 int cursor_type_changed;
552 /* Nonzero after display_mode_line if %l was used and it displayed a
553 line number. */
555 int line_number_displayed;
557 /* Maximum buffer size for which to display line numbers. */
559 Lisp_Object Vline_number_display_limit;
561 /* Line width to consider when repositioning for line number display. */
563 static EMACS_INT line_number_display_limit_width;
565 /* Number of lines to keep in the message log buffer. t means
566 infinite. nil means don't log at all. */
568 Lisp_Object Vmessage_log_max;
570 /* The name of the *Messages* buffer, a string. */
572 static Lisp_Object Vmessages_buffer_name;
574 /* Index 0 is the buffer that holds the current (desired) echo area message,
575 or nil if none is desired right now.
577 Index 1 is the buffer that holds the previously displayed echo area message,
578 or nil to indicate no message. This is normally what's on the screen now.
580 These two can point to the same buffer. That happens when the last
581 message output by the user (or made by echoing) has been displayed. */
583 Lisp_Object echo_area_buffer[2];
585 /* Permanent pointers to the two buffers that are used for echo area
586 purposes. Once the two buffers are made, and their pointers are
587 placed here, these two slots remain unchanged unless those buffers
588 need to be created afresh. */
590 static Lisp_Object echo_buffer[2];
592 /* A vector saved used in with_area_buffer to reduce consing. */
594 static Lisp_Object Vwith_echo_area_save_vector;
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
599 static int display_last_displayed_message_p;
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
604 int message_buf_print;
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
611 /* Maximum height for resizing mini-windows. Either a float
612 specifying a fraction of the available height, or an integer
613 specifying a number of lines. */
615 Lisp_Object Vmax_mini_window_height;
617 /* Non-zero means messages should be displayed with truncated
618 lines instead of being continued. */
620 int message_truncate_lines;
621 Lisp_Object Qmessage_truncate_lines;
623 /* Set to 1 in clear_message to make redisplay_internal aware
624 of an emptied echo area. */
626 static int message_cleared_p;
628 /* How to blink the default frame cursor off. */
629 Lisp_Object Vblink_cursor_alist;
631 /* A scratch glyph row with contents used for generating truncation
632 glyphs. Also used in direct_output_for_insert. */
634 #define MAX_SCRATCH_GLYPHS 100
635 struct glyph_row scratch_glyph_row;
636 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
638 /* Ascent and height of the last line processed by move_it_to. */
640 static int last_max_ascent, last_height;
642 /* Non-zero if there's a help-echo in the echo area. */
644 int help_echo_showing_p;
646 /* If >= 0, computed, exact values of mode-line and header-line height
647 to use in the macros CURRENT_MODE_LINE_HEIGHT and
648 CURRENT_HEADER_LINE_HEIGHT. */
650 int current_mode_line_height, current_header_line_height;
652 /* The maximum distance to look ahead for text properties. Values
653 that are too small let us call compute_char_face and similar
654 functions too often which is expensive. Values that are too large
655 let us call compute_char_face and alike too often because we
656 might not be interested in text properties that far away. */
658 #define TEXT_PROP_DISTANCE_LIMIT 100
660 #if GLYPH_DEBUG
662 /* Variables to turn off display optimizations from Lisp. */
664 int inhibit_try_window_id, inhibit_try_window_reusing;
665 int inhibit_try_cursor_movement;
667 /* Non-zero means print traces of redisplay if compiled with
668 GLYPH_DEBUG != 0. */
670 int trace_redisplay_p;
672 #endif /* GLYPH_DEBUG */
674 #ifdef DEBUG_TRACE_MOVE
675 /* Non-zero means trace with TRACE_MOVE to stderr. */
676 int trace_move;
678 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
679 #else
680 #define TRACE_MOVE(x) (void) 0
681 #endif
683 /* Non-zero means automatically scroll windows horizontally to make
684 point visible. */
686 int automatic_hscrolling_p;
688 /* How close to the margin can point get before the window is scrolled
689 horizontally. */
690 EMACS_INT hscroll_margin;
692 /* How much to scroll horizontally when point is inside the above margin. */
693 Lisp_Object Vhscroll_step;
695 /* The variable `resize-mini-windows'. If nil, don't resize
696 mini-windows. If t, always resize them to fit the text they
697 display. If `grow-only', let mini-windows grow only until they
698 become empty. */
700 Lisp_Object Vresize_mini_windows;
702 /* Buffer being redisplayed -- for redisplay_window_error. */
704 struct buffer *displayed_buffer;
706 /* Value returned from text property handlers (see below). */
708 enum prop_handled
710 HANDLED_NORMALLY,
711 HANDLED_RECOMPUTE_PROPS,
712 HANDLED_OVERLAY_STRING_CONSUMED,
713 HANDLED_RETURN
716 /* A description of text properties that redisplay is interested
717 in. */
719 struct props
721 /* The name of the property. */
722 Lisp_Object *name;
724 /* A unique index for the property. */
725 enum prop_idx idx;
727 /* A handler function called to set up iterator IT from the property
728 at IT's current position. Value is used to steer handle_stop. */
729 enum prop_handled (*handler) P_ ((struct it *it));
732 static enum prop_handled handle_face_prop P_ ((struct it *));
733 static enum prop_handled handle_invisible_prop P_ ((struct it *));
734 static enum prop_handled handle_display_prop P_ ((struct it *));
735 static enum prop_handled handle_composition_prop P_ ((struct it *));
736 static enum prop_handled handle_overlay_change P_ ((struct it *));
737 static enum prop_handled handle_fontified_prop P_ ((struct it *));
739 /* Properties handled by iterators. */
741 static struct props it_props[] =
743 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
744 /* Handle `face' before `display' because some sub-properties of
745 `display' need to know the face. */
746 {&Qface, FACE_PROP_IDX, handle_face_prop},
747 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
748 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
749 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
750 {NULL, 0, NULL}
753 /* Value is the position described by X. If X is a marker, value is
754 the marker_position of X. Otherwise, value is X. */
756 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
758 /* Enumeration returned by some move_it_.* functions internally. */
760 enum move_it_result
762 /* Not used. Undefined value. */
763 MOVE_UNDEFINED,
765 /* Move ended at the requested buffer position or ZV. */
766 MOVE_POS_MATCH_OR_ZV,
768 /* Move ended at the requested X pixel position. */
769 MOVE_X_REACHED,
771 /* Move within a line ended at the end of a line that must be
772 continued. */
773 MOVE_LINE_CONTINUED,
775 /* Move within a line ended at the end of a line that would
776 be displayed truncated. */
777 MOVE_LINE_TRUNCATED,
779 /* Move within a line ended at a line end. */
780 MOVE_NEWLINE_OR_CR
783 /* This counter is used to clear the face cache every once in a while
784 in redisplay_internal. It is incremented for each redisplay.
785 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
786 cleared. */
788 #define CLEAR_FACE_CACHE_COUNT 500
789 static int clear_face_cache_count;
791 /* Similarly for the image cache. */
793 #ifdef HAVE_WINDOW_SYSTEM
794 #define CLEAR_IMAGE_CACHE_COUNT 101
795 static int clear_image_cache_count;
796 #endif
798 /* Record the previous terminal frame we displayed. */
800 static struct frame *previous_terminal_frame;
802 /* Non-zero while redisplay_internal is in progress. */
804 int redisplaying_p;
806 /* Non-zero means don't free realized faces. Bound while freeing
807 realized faces is dangerous because glyph matrices might still
808 reference them. */
810 int inhibit_free_realized_faces;
811 Lisp_Object Qinhibit_free_realized_faces;
813 /* If a string, XTread_socket generates an event to display that string.
814 (The display is done in read_char.) */
816 Lisp_Object help_echo_string;
817 Lisp_Object help_echo_window;
818 Lisp_Object help_echo_object;
819 int help_echo_pos;
821 /* Temporary variable for XTread_socket. */
823 Lisp_Object previous_help_echo_string;
825 /* Null glyph slice */
827 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
830 /* Function prototypes. */
832 static void setup_for_ellipsis P_ ((struct it *, int));
833 static void mark_window_display_accurate_1 P_ ((struct window *, int));
834 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
835 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
837 static int redisplay_mode_lines P_ ((Lisp_Object, int));
838 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
840 #if 0
841 static int invisible_text_between_p P_ ((struct it *, int, int));
842 #endif
844 static void pint2str P_ ((char *, int, int));
845 static void pint2hrstr P_ ((char *, int, int));
846 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
847 struct text_pos));
848 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
849 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
850 static void store_mode_line_noprop_char P_ ((char));
851 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
852 static void x_consider_frame_title P_ ((Lisp_Object));
853 static void handle_stop P_ ((struct it *));
854 static int tool_bar_lines_needed P_ ((struct frame *));
855 static int single_display_spec_intangible_p P_ ((Lisp_Object));
856 static void ensure_echo_area_buffers P_ ((void));
857 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
858 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
859 static int with_echo_area_buffer P_ ((struct window *, int,
860 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
861 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static void clear_garbaged_frames P_ ((void));
863 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int display_echo_area P_ ((struct window *));
867 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
870 static int string_char_and_length P_ ((const unsigned char *, int, int *));
871 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
872 struct text_pos));
873 static int compute_window_start_on_continuation_line P_ ((struct window *));
874 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
875 static void insert_left_trunc_glyphs P_ ((struct it *));
876 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
877 Lisp_Object));
878 static void extend_face_to_end_of_line P_ ((struct it *));
879 static int append_space_for_newline P_ ((struct it *, int));
880 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
881 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
882 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
883 static int trailing_whitespace_p P_ ((int));
884 static int message_log_check_duplicate P_ ((int, int, int, int));
885 static void push_it P_ ((struct it *));
886 static void pop_it P_ ((struct it *));
887 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
888 static void select_frame_for_redisplay P_ ((Lisp_Object));
889 static void redisplay_internal P_ ((int));
890 static int echo_area_display P_ ((int));
891 static void redisplay_windows P_ ((Lisp_Object));
892 static void redisplay_window P_ ((Lisp_Object, int));
893 static Lisp_Object redisplay_window_error ();
894 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
895 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
896 static void update_menu_bar P_ ((struct frame *, int));
897 static int try_window_reusing_current_matrix P_ ((struct window *));
898 static int try_window_id P_ ((struct window *));
899 static int display_line P_ ((struct it *));
900 static int display_mode_lines P_ ((struct window *));
901 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
902 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
903 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
904 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
905 static void display_menu_bar P_ ((struct window *));
906 static int display_count_lines P_ ((int, int, int, int, int *));
907 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
908 int, int, struct it *, int, int, int, int));
909 static void compute_line_metrics P_ ((struct it *));
910 static void run_redisplay_end_trigger_hook P_ ((struct it *));
911 static int get_overlay_strings P_ ((struct it *, int));
912 static void next_overlay_string P_ ((struct it *));
913 static void reseat P_ ((struct it *, struct text_pos, int));
914 static void reseat_1 P_ ((struct it *, struct text_pos, int));
915 static void back_to_previous_visible_line_start P_ ((struct it *));
916 void reseat_at_previous_visible_line_start P_ ((struct it *));
917 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
918 static int next_element_from_ellipsis P_ ((struct it *));
919 static int next_element_from_display_vector P_ ((struct it *));
920 static int next_element_from_string P_ ((struct it *));
921 static int next_element_from_c_string P_ ((struct it *));
922 static int next_element_from_buffer P_ ((struct it *));
923 static int next_element_from_composition P_ ((struct it *));
924 static int next_element_from_image P_ ((struct it *));
925 static int next_element_from_stretch P_ ((struct it *));
926 static void load_overlay_strings P_ ((struct it *, int));
927 static int init_from_display_pos P_ ((struct it *, struct window *,
928 struct display_pos *));
929 static void reseat_to_string P_ ((struct it *, unsigned char *,
930 Lisp_Object, int, int, int, int));
931 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
932 int, int, int));
933 void move_it_vertically_backward P_ ((struct it *, int));
934 static void init_to_row_start P_ ((struct it *, struct window *,
935 struct glyph_row *));
936 static int init_to_row_end P_ ((struct it *, struct window *,
937 struct glyph_row *));
938 static void back_to_previous_line_start P_ ((struct it *));
939 static int forward_to_next_line_start P_ ((struct it *, int *));
940 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
941 Lisp_Object, int));
942 static struct text_pos string_pos P_ ((int, Lisp_Object));
943 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
944 static int number_of_chars P_ ((unsigned char *, int));
945 static void compute_stop_pos P_ ((struct it *));
946 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
947 Lisp_Object));
948 static int face_before_or_after_it_pos P_ ((struct it *, int));
949 static int next_overlay_change P_ ((int));
950 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
951 Lisp_Object, struct text_pos *,
952 int));
953 static int underlying_face_id P_ ((struct it *));
954 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
955 struct window *));
957 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
958 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960 #ifdef HAVE_WINDOW_SYSTEM
962 static void update_tool_bar P_ ((struct frame *, int));
963 static void build_desired_tool_bar_string P_ ((struct frame *f));
964 static int redisplay_tool_bar P_ ((struct frame *));
965 static void display_tool_bar_line P_ ((struct it *));
966 static void notice_overwritten_cursor P_ ((struct window *,
967 enum glyph_row_area,
968 int, int, int, int));
972 #endif /* HAVE_WINDOW_SYSTEM */
975 /***********************************************************************
976 Window display dimensions
977 ***********************************************************************/
979 /* Return the bottom boundary y-position for text lines in window W.
980 This is the first y position at which a line cannot start.
981 It is relative to the top of the window.
983 This is the height of W minus the height of a mode line, if any. */
985 INLINE int
986 window_text_bottom_y (w)
987 struct window *w;
989 int height = WINDOW_TOTAL_HEIGHT (w);
991 if (WINDOW_WANTS_MODELINE_P (w))
992 height -= CURRENT_MODE_LINE_HEIGHT (w);
993 return height;
996 /* Return the pixel width of display area AREA of window W. AREA < 0
997 means return the total width of W, not including fringes to
998 the left and right of the window. */
1000 INLINE int
1001 window_box_width (w, area)
1002 struct window *w;
1003 int area;
1005 int cols = XFASTINT (w->total_cols);
1006 int pixels = 0;
1008 if (!w->pseudo_window_p)
1010 cols -= WINDOW_SCROLL_BAR_COLS (w);
1012 if (area == TEXT_AREA)
1014 if (INTEGERP (w->left_margin_cols))
1015 cols -= XFASTINT (w->left_margin_cols);
1016 if (INTEGERP (w->right_margin_cols))
1017 cols -= XFASTINT (w->right_margin_cols);
1018 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1020 else if (area == LEFT_MARGIN_AREA)
1022 cols = (INTEGERP (w->left_margin_cols)
1023 ? XFASTINT (w->left_margin_cols) : 0);
1024 pixels = 0;
1026 else if (area == RIGHT_MARGIN_AREA)
1028 cols = (INTEGERP (w->right_margin_cols)
1029 ? XFASTINT (w->right_margin_cols) : 0);
1030 pixels = 0;
1034 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1038 /* Return the pixel height of the display area of window W, not
1039 including mode lines of W, if any. */
1041 INLINE int
1042 window_box_height (w)
1043 struct window *w;
1045 struct frame *f = XFRAME (w->frame);
1046 int height = WINDOW_TOTAL_HEIGHT (w);
1048 xassert (height >= 0);
1050 /* Note: the code below that determines the mode-line/header-line
1051 height is essentially the same as that contained in the macro
1052 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1053 the appropriate glyph row has its `mode_line_p' flag set,
1054 and if it doesn't, uses estimate_mode_line_height instead. */
1056 if (WINDOW_WANTS_MODELINE_P (w))
1058 struct glyph_row *ml_row
1059 = (w->current_matrix && w->current_matrix->rows
1060 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1061 : 0);
1062 if (ml_row && ml_row->mode_line_p)
1063 height -= ml_row->height;
1064 else
1065 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 struct glyph_row *hl_row
1071 = (w->current_matrix && w->current_matrix->rows
1072 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1073 : 0);
1074 if (hl_row && hl_row->mode_line_p)
1075 height -= hl_row->height;
1076 else
1077 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1080 /* With a very small font and a mode-line that's taller than
1081 default, we might end up with a negative height. */
1082 return max (0, height);
1085 /* Return the window-relative coordinate of the left edge of display
1086 area AREA of window W. AREA < 0 means return the left edge of the
1087 whole window, to the right of the left fringe of W. */
1089 INLINE int
1090 window_box_left_offset (w, area)
1091 struct window *w;
1092 int area;
1094 int x;
1096 if (w->pseudo_window_p)
1097 return 0;
1099 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1101 if (area == TEXT_AREA)
1102 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1103 + window_box_width (w, LEFT_MARGIN_AREA));
1104 else if (area == RIGHT_MARGIN_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA)
1107 + window_box_width (w, TEXT_AREA)
1108 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1110 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1111 else if (area == LEFT_MARGIN_AREA
1112 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1113 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1115 return x;
1119 /* Return the window-relative coordinate of the right edge of display
1120 area AREA of window W. AREA < 0 means return the left edge of the
1121 whole window, to the left of the right fringe of W. */
1123 INLINE int
1124 window_box_right_offset (w, area)
1125 struct window *w;
1126 int area;
1128 return window_box_left_offset (w, area) + window_box_width (w, area);
1131 /* Return the frame-relative coordinate of the left edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the right of the left fringe of W. */
1135 INLINE int
1136 window_box_left (w, area)
1137 struct window *w;
1138 int area;
1140 struct frame *f = XFRAME (w->frame);
1141 int x;
1143 if (w->pseudo_window_p)
1144 return FRAME_INTERNAL_BORDER_WIDTH (f);
1146 x = (WINDOW_LEFT_EDGE_X (w)
1147 + window_box_left_offset (w, area));
1149 return x;
1153 /* Return the frame-relative coordinate of the right edge of display
1154 area AREA of window W. AREA < 0 means return the left edge of the
1155 whole window, to the left of the right fringe of W. */
1157 INLINE int
1158 window_box_right (w, area)
1159 struct window *w;
1160 int area;
1162 return window_box_left (w, area) + window_box_width (w, area);
1165 /* Get the bounding box of the display area AREA of window W, without
1166 mode lines, in frame-relative coordinates. AREA < 0 means the
1167 whole window, not including the left and right fringes of
1168 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1169 coordinates of the upper-left corner of the box. Return in
1170 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1172 INLINE void
1173 window_box (w, area, box_x, box_y, box_width, box_height)
1174 struct window *w;
1175 int area;
1176 int *box_x, *box_y, *box_width, *box_height;
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines. AREA < 0 means the whole window, not including the
1195 left and right fringe of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1201 INLINE void
1202 window_box_edges (w, area, top_left_x, top_left_y,
1203 bottom_right_x, bottom_right_y)
1204 struct window *w;
1205 int area;
1206 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1208 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1209 bottom_right_y);
1210 *bottom_right_x += *top_left_x;
1211 *bottom_right_y += *top_left_y;
1216 /***********************************************************************
1217 Utilities
1218 ***********************************************************************/
1220 /* Return the bottom y-position of the line the iterator IT is in.
1221 This can modify IT's settings. */
1224 line_bottom_y (it)
1225 struct it *it;
1227 int line_height = it->max_ascent + it->max_descent;
1228 int line_top_y = it->current_y;
1230 if (line_height == 0)
1232 if (last_height)
1233 line_height = last_height;
1234 else if (IT_CHARPOS (*it) < ZV)
1236 move_it_by_lines (it, 1, 1);
1237 line_height = (it->max_ascent || it->max_descent
1238 ? it->max_ascent + it->max_descent
1239 : last_height);
1241 else
1243 struct glyph_row *row = it->glyph_row;
1245 /* Use the default character height. */
1246 it->glyph_row = NULL;
1247 it->what = IT_CHARACTER;
1248 it->c = ' ';
1249 it->len = 1;
1250 PRODUCE_GLYPHS (it);
1251 line_height = it->ascent + it->descent;
1252 it->glyph_row = row;
1256 return line_top_y + line_height;
1260 /* Return 1 if position CHARPOS is visible in window W.
1261 If visible, set *X and *Y to pixel coordinates of top left corner.
1262 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1263 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1264 and header-lines heights. */
1267 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1268 struct window *w;
1269 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1271 struct it it;
1272 struct text_pos top;
1273 int visible_p = 0;
1274 struct buffer *old_buffer = NULL;
1276 if (noninteractive)
1277 return visible_p;
1279 if (XBUFFER (w->buffer) != current_buffer)
1281 old_buffer = current_buffer;
1282 set_buffer_internal_1 (XBUFFER (w->buffer));
1285 SET_TEXT_POS_FROM_MARKER (top, w->start);
1287 /* Compute exact mode line heights, if requested. */
1288 if (exact_mode_line_heights_p)
1290 if (WINDOW_WANTS_MODELINE_P (w))
1291 current_mode_line_height
1292 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1293 current_buffer->mode_line_format);
1295 if (WINDOW_WANTS_HEADER_LINE_P (w))
1296 current_header_line_height
1297 = display_mode_line (w, HEADER_LINE_FACE_ID,
1298 current_buffer->header_line_format);
1301 start_display (&it, w, top);
1302 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1303 MOVE_TO_POS | MOVE_TO_Y);
1305 /* Note that we may overshoot because of invisible text. */
1306 if (IT_CHARPOS (it) >= charpos)
1308 int top_x = it.current_x;
1309 int top_y = it.current_y;
1310 int bottom_y = (last_height = 0, line_bottom_y (&it));
1311 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1313 if (top_y < window_top_y)
1314 visible_p = bottom_y > window_top_y;
1315 else if (top_y < it.last_visible_y)
1316 visible_p = 1;
1317 if (visible_p)
1319 *x = top_x;
1320 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1321 *rtop = max (0, window_top_y - top_y);
1322 *rbot = max (0, bottom_y - it.last_visible_y);
1325 else
1327 struct it it2;
1329 it2 = it;
1330 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1331 move_it_by_lines (&it, 1, 0);
1332 if (charpos < IT_CHARPOS (it))
1334 visible_p = 1;
1335 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1336 *x = it2.current_x;
1337 *y = it2.current_y + it2.max_ascent - it2.ascent;
1338 *rtop = max (0, -it2.current_y);
1339 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1340 - it.last_visible_y));
1344 if (old_buffer)
1345 set_buffer_internal_1 (old_buffer);
1347 current_header_line_height = current_mode_line_height = -1;
1349 return visible_p;
1353 /* Return the next character from STR which is MAXLEN bytes long.
1354 Return in *LEN the length of the character. This is like
1355 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1356 we find one, we return a `?', but with the length of the invalid
1357 character. */
1359 static INLINE int
1360 string_char_and_length (str, maxlen, len)
1361 const unsigned char *str;
1362 int maxlen, *len;
1364 int c;
1366 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1367 if (!CHAR_VALID_P (c, 1))
1368 /* We may not change the length here because other places in Emacs
1369 don't use this function, i.e. they silently accept invalid
1370 characters. */
1371 c = '?';
1373 return c;
1378 /* Given a position POS containing a valid character and byte position
1379 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1381 static struct text_pos
1382 string_pos_nchars_ahead (pos, string, nchars)
1383 struct text_pos pos;
1384 Lisp_Object string;
1385 int nchars;
1387 xassert (STRINGP (string) && nchars >= 0);
1389 if (STRING_MULTIBYTE (string))
1391 int rest = SBYTES (string) - BYTEPOS (pos);
1392 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1393 int len;
1395 while (nchars--)
1397 string_char_and_length (p, rest, &len);
1398 p += len, rest -= len;
1399 xassert (rest >= 0);
1400 CHARPOS (pos) += 1;
1401 BYTEPOS (pos) += len;
1404 else
1405 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1407 return pos;
1411 /* Value is the text position, i.e. character and byte position,
1412 for character position CHARPOS in STRING. */
1414 static INLINE struct text_pos
1415 string_pos (charpos, string)
1416 int charpos;
1417 Lisp_Object string;
1419 struct text_pos pos;
1420 xassert (STRINGP (string));
1421 xassert (charpos >= 0);
1422 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1423 return pos;
1427 /* Value is a text position, i.e. character and byte position, for
1428 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1429 means recognize multibyte characters. */
1431 static struct text_pos
1432 c_string_pos (charpos, s, multibyte_p)
1433 int charpos;
1434 unsigned char *s;
1435 int multibyte_p;
1437 struct text_pos pos;
1439 xassert (s != NULL);
1440 xassert (charpos >= 0);
1442 if (multibyte_p)
1444 int rest = strlen (s), len;
1446 SET_TEXT_POS (pos, 0, 0);
1447 while (charpos--)
1449 string_char_and_length (s, rest, &len);
1450 s += len, rest -= len;
1451 xassert (rest >= 0);
1452 CHARPOS (pos) += 1;
1453 BYTEPOS (pos) += len;
1456 else
1457 SET_TEXT_POS (pos, charpos, charpos);
1459 return pos;
1463 /* Value is the number of characters in C string S. MULTIBYTE_P
1464 non-zero means recognize multibyte characters. */
1466 static int
1467 number_of_chars (s, multibyte_p)
1468 unsigned char *s;
1469 int multibyte_p;
1471 int nchars;
1473 if (multibyte_p)
1475 int rest = strlen (s), len;
1476 unsigned char *p = (unsigned char *) s;
1478 for (nchars = 0; rest > 0; ++nchars)
1480 string_char_and_length (p, rest, &len);
1481 rest -= len, p += len;
1484 else
1485 nchars = strlen (s);
1487 return nchars;
1491 /* Compute byte position NEWPOS->bytepos corresponding to
1492 NEWPOS->charpos. POS is a known position in string STRING.
1493 NEWPOS->charpos must be >= POS.charpos. */
1495 static void
1496 compute_string_pos (newpos, pos, string)
1497 struct text_pos *newpos, pos;
1498 Lisp_Object string;
1500 xassert (STRINGP (string));
1501 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1503 if (STRING_MULTIBYTE (string))
1504 *newpos = string_pos_nchars_ahead (pos, string,
1505 CHARPOS (*newpos) - CHARPOS (pos));
1506 else
1507 BYTEPOS (*newpos) = CHARPOS (*newpos);
1510 /* EXPORT:
1511 Return an estimation of the pixel height of mode or top lines on
1512 frame F. FACE_ID specifies what line's height to estimate. */
1515 estimate_mode_line_height (f, face_id)
1516 struct frame *f;
1517 enum face_id face_id;
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 if (FRAME_WINDOW_P (f))
1522 int height = FONT_HEIGHT (FRAME_FONT (f));
1524 /* This function is called so early when Emacs starts that the face
1525 cache and mode line face are not yet initialized. */
1526 if (FRAME_FACE_CACHE (f))
1528 struct face *face = FACE_FROM_ID (f, face_id);
1529 if (face)
1531 if (face->font)
1532 height = FONT_HEIGHT (face->font);
1533 if (face->box_line_width > 0)
1534 height += 2 * face->box_line_width;
1538 return height;
1540 #endif
1542 return 1;
1545 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1546 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1547 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1548 not force the value into range. */
1550 void
1551 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1552 FRAME_PTR f;
1553 register int pix_x, pix_y;
1554 int *x, *y;
1555 NativeRectangle *bounds;
1556 int noclip;
1559 #ifdef HAVE_WINDOW_SYSTEM
1560 if (FRAME_WINDOW_P (f))
1562 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1563 even for negative values. */
1564 if (pix_x < 0)
1565 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1566 if (pix_y < 0)
1567 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1569 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1570 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1572 if (bounds)
1573 STORE_NATIVE_RECT (*bounds,
1574 FRAME_COL_TO_PIXEL_X (f, pix_x),
1575 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1576 FRAME_COLUMN_WIDTH (f) - 1,
1577 FRAME_LINE_HEIGHT (f) - 1);
1579 if (!noclip)
1581 if (pix_x < 0)
1582 pix_x = 0;
1583 else if (pix_x > FRAME_TOTAL_COLS (f))
1584 pix_x = FRAME_TOTAL_COLS (f);
1586 if (pix_y < 0)
1587 pix_y = 0;
1588 else if (pix_y > FRAME_LINES (f))
1589 pix_y = FRAME_LINES (f);
1592 #endif
1594 *x = pix_x;
1595 *y = pix_y;
1599 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1600 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1601 can't tell the positions because W's display is not up to date,
1602 return 0. */
1605 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1606 struct window *w;
1607 int hpos, vpos;
1608 int *frame_x, *frame_y;
1610 #ifdef HAVE_WINDOW_SYSTEM
1611 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1613 int success_p;
1615 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1616 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1618 if (display_completed)
1620 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1621 struct glyph *glyph = row->glyphs[TEXT_AREA];
1622 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1624 hpos = row->x;
1625 vpos = row->y;
1626 while (glyph < end)
1628 hpos += glyph->pixel_width;
1629 ++glyph;
1632 /* If first glyph is partially visible, its first visible position is still 0. */
1633 if (hpos < 0)
1634 hpos = 0;
1636 success_p = 1;
1638 else
1640 hpos = vpos = 0;
1641 success_p = 0;
1644 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1645 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1646 return success_p;
1648 #endif
1650 *frame_x = hpos;
1651 *frame_y = vpos;
1652 return 1;
1656 #ifdef HAVE_WINDOW_SYSTEM
1658 /* Find the glyph under window-relative coordinates X/Y in window W.
1659 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1660 strings. Return in *HPOS and *VPOS the row and column number of
1661 the glyph found. Return in *AREA the glyph area containing X.
1662 Value is a pointer to the glyph found or null if X/Y is not on
1663 text, or we can't tell because W's current matrix is not up to
1664 date. */
1666 static struct glyph *
1667 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1668 struct window *w;
1669 int x, y;
1670 int *hpos, *vpos, *dx, *dy, *area;
1672 struct glyph *glyph, *end;
1673 struct glyph_row *row = NULL;
1674 int x0, i;
1676 /* Find row containing Y. Give up if some row is not enabled. */
1677 for (i = 0; i < w->current_matrix->nrows; ++i)
1679 row = MATRIX_ROW (w->current_matrix, i);
1680 if (!row->enabled_p)
1681 return NULL;
1682 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1683 break;
1686 *vpos = i;
1687 *hpos = 0;
1689 /* Give up if Y is not in the window. */
1690 if (i == w->current_matrix->nrows)
1691 return NULL;
1693 /* Get the glyph area containing X. */
1694 if (w->pseudo_window_p)
1696 *area = TEXT_AREA;
1697 x0 = 0;
1699 else
1701 if (x < window_box_left_offset (w, TEXT_AREA))
1703 *area = LEFT_MARGIN_AREA;
1704 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1706 else if (x < window_box_right_offset (w, TEXT_AREA))
1708 *area = TEXT_AREA;
1709 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1711 else
1713 *area = RIGHT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1718 /* Find glyph containing X. */
1719 glyph = row->glyphs[*area];
1720 end = glyph + row->used[*area];
1721 x -= x0;
1722 while (glyph < end && x >= glyph->pixel_width)
1724 x -= glyph->pixel_width;
1725 ++glyph;
1728 if (glyph == end)
1729 return NULL;
1731 if (dx)
1733 *dx = x;
1734 *dy = y - (row->y + row->ascent - glyph->ascent);
1737 *hpos = glyph - row->glyphs[*area];
1738 return glyph;
1742 /* EXPORT:
1743 Convert frame-relative x/y to coordinates relative to window W.
1744 Takes pseudo-windows into account. */
1746 void
1747 frame_to_window_pixel_xy (w, x, y)
1748 struct window *w;
1749 int *x, *y;
1751 if (w->pseudo_window_p)
1753 /* A pseudo-window is always full-width, and starts at the
1754 left edge of the frame, plus a frame border. */
1755 struct frame *f = XFRAME (w->frame);
1756 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1757 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1759 else
1761 *x -= WINDOW_LEFT_EDGE_X (w);
1762 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1766 /* EXPORT:
1767 Return in *R the clipping rectangle for glyph string S. */
1769 void
1770 get_glyph_string_clip_rect (s, nr)
1771 struct glyph_string *s;
1772 NativeRectangle *nr;
1774 XRectangle r;
1776 if (s->row->full_width_p)
1778 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1779 r.x = WINDOW_LEFT_EDGE_X (s->w);
1780 r.width = WINDOW_TOTAL_WIDTH (s->w);
1782 /* Unless displaying a mode or menu bar line, which are always
1783 fully visible, clip to the visible part of the row. */
1784 if (s->w->pseudo_window_p)
1785 r.height = s->row->visible_height;
1786 else
1787 r.height = s->height;
1789 else
1791 /* This is a text line that may be partially visible. */
1792 r.x = window_box_left (s->w, s->area);
1793 r.width = window_box_width (s->w, s->area);
1794 r.height = s->row->visible_height;
1797 if (s->clip_head)
1798 if (r.x < s->clip_head->x)
1800 if (r.width >= s->clip_head->x - r.x)
1801 r.width -= s->clip_head->x - r.x;
1802 else
1803 r.width = 0;
1804 r.x = s->clip_head->x;
1806 if (s->clip_tail)
1807 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1809 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1810 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1811 else
1812 r.width = 0;
1815 /* If S draws overlapping rows, it's sufficient to use the top and
1816 bottom of the window for clipping because this glyph string
1817 intentionally draws over other lines. */
1818 if (s->for_overlaps_p)
1820 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1821 r.height = window_text_bottom_y (s->w) - r.y;
1823 else
1825 /* Don't use S->y for clipping because it doesn't take partially
1826 visible lines into account. For example, it can be negative for
1827 partially visible lines at the top of a window. */
1828 if (!s->row->full_width_p
1829 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1830 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1831 else
1832 r.y = max (0, s->row->y);
1834 /* If drawing a tool-bar window, draw it over the internal border
1835 at the top of the window. */
1836 if (WINDOWP (s->f->tool_bar_window)
1837 && s->w == XWINDOW (s->f->tool_bar_window))
1838 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1841 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1843 /* If drawing the cursor, don't let glyph draw outside its
1844 advertised boundaries. Cleartype does this under some circumstances. */
1845 if (s->hl == DRAW_CURSOR)
1847 struct glyph *glyph = s->first_glyph;
1848 int height, max_y;
1850 if (s->x > r.x)
1852 r.width -= s->x - r.x;
1853 r.x = s->x;
1855 r.width = min (r.width, glyph->pixel_width);
1857 /* If r.y is below window bottom, ensure that we still see a cursor. */
1858 height = min (glyph->ascent + glyph->descent,
1859 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1860 max_y = window_text_bottom_y (s->w) - height;
1861 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1862 if (s->ybase - glyph->ascent > max_y)
1864 r.y = max_y;
1865 r.height = height;
1867 else
1869 /* Don't draw cursor glyph taller than our actual glyph. */
1870 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1871 if (height < r.height)
1873 max_y = r.y + r.height;
1874 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1875 r.height = min (max_y - r.y, height);
1880 #ifdef CONVERT_FROM_XRECT
1881 CONVERT_FROM_XRECT (r, *nr);
1882 #else
1883 *nr = r;
1884 #endif
1888 /* EXPORT:
1889 Return the position and height of the phys cursor in window W.
1890 Set w->phys_cursor_width to width of phys cursor.
1894 get_phys_cursor_geometry (w, row, glyph, heightp)
1895 struct window *w;
1896 struct glyph_row *row;
1897 struct glyph *glyph;
1898 int *heightp;
1900 struct frame *f = XFRAME (WINDOW_FRAME (w));
1901 int y, wd, h, h0, y0;
1903 /* Compute the width of the rectangle to draw. If on a stretch
1904 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1905 rectangle as wide as the glyph, but use a canonical character
1906 width instead. */
1907 wd = glyph->pixel_width - 1;
1908 #ifdef HAVE_NTGUI
1909 wd++; /* Why? */
1910 #endif
1911 if (glyph->type == STRETCH_GLYPH
1912 && !x_stretch_cursor_p)
1913 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1914 w->phys_cursor_width = wd;
1916 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1918 /* If y is below window bottom, ensure that we still see a cursor. */
1919 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1921 h = max (h0, glyph->ascent + glyph->descent);
1922 h0 = min (h0, glyph->ascent + glyph->descent);
1924 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1925 if (y < y0)
1927 h = max (h - (y0 - y) + 1, h0);
1928 y = y0 - 1;
1930 else
1932 y0 = window_text_bottom_y (w) - h0;
1933 if (y > y0)
1935 h += y - y0;
1936 y = y0;
1940 *heightp = h - 1;
1941 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1945 #endif /* HAVE_WINDOW_SYSTEM */
1948 /***********************************************************************
1949 Lisp form evaluation
1950 ***********************************************************************/
1952 /* Error handler for safe_eval and safe_call. */
1954 static Lisp_Object
1955 safe_eval_handler (arg)
1956 Lisp_Object arg;
1958 add_to_log ("Error during redisplay: %s", arg, Qnil);
1959 return Qnil;
1963 /* Evaluate SEXPR and return the result, or nil if something went
1964 wrong. Prevent redisplay during the evaluation. */
1966 Lisp_Object
1967 safe_eval (sexpr)
1968 Lisp_Object sexpr;
1970 Lisp_Object val;
1972 if (inhibit_eval_during_redisplay)
1973 val = Qnil;
1974 else
1976 int count = SPECPDL_INDEX ();
1977 struct gcpro gcpro1;
1979 GCPRO1 (sexpr);
1980 specbind (Qinhibit_redisplay, Qt);
1981 /* Use Qt to ensure debugger does not run,
1982 so there is no possibility of wanting to redisplay. */
1983 val = internal_condition_case_1 (Feval, sexpr, Qt,
1984 safe_eval_handler);
1985 UNGCPRO;
1986 val = unbind_to (count, val);
1989 return val;
1993 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1994 Return the result, or nil if something went wrong. Prevent
1995 redisplay during the evaluation. */
1997 Lisp_Object
1998 safe_call (nargs, args)
1999 int nargs;
2000 Lisp_Object *args;
2002 Lisp_Object val;
2004 if (inhibit_eval_during_redisplay)
2005 val = Qnil;
2006 else
2008 int count = SPECPDL_INDEX ();
2009 struct gcpro gcpro1;
2011 GCPRO1 (args[0]);
2012 gcpro1.nvars = nargs;
2013 specbind (Qinhibit_redisplay, Qt);
2014 /* Use Qt to ensure debugger does not run,
2015 so there is no possibility of wanting to redisplay. */
2016 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2017 safe_eval_handler);
2018 UNGCPRO;
2019 val = unbind_to (count, val);
2022 return val;
2026 /* Call function FN with one argument ARG.
2027 Return the result, or nil if something went wrong. */
2029 Lisp_Object
2030 safe_call1 (fn, arg)
2031 Lisp_Object fn, arg;
2033 Lisp_Object args[2];
2034 args[0] = fn;
2035 args[1] = arg;
2036 return safe_call (2, args);
2041 /***********************************************************************
2042 Debugging
2043 ***********************************************************************/
2045 #if 0
2047 /* Define CHECK_IT to perform sanity checks on iterators.
2048 This is for debugging. It is too slow to do unconditionally. */
2050 static void
2051 check_it (it)
2052 struct it *it;
2054 if (it->method == GET_FROM_STRING)
2056 xassert (STRINGP (it->string));
2057 xassert (IT_STRING_CHARPOS (*it) >= 0);
2059 else
2061 xassert (IT_STRING_CHARPOS (*it) < 0);
2062 if (it->method == GET_FROM_BUFFER)
2064 /* Check that character and byte positions agree. */
2065 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2069 if (it->dpvec)
2070 xassert (it->current.dpvec_index >= 0);
2071 else
2072 xassert (it->current.dpvec_index < 0);
2075 #define CHECK_IT(IT) check_it ((IT))
2077 #else /* not 0 */
2079 #define CHECK_IT(IT) (void) 0
2081 #endif /* not 0 */
2084 #if GLYPH_DEBUG
2086 /* Check that the window end of window W is what we expect it
2087 to be---the last row in the current matrix displaying text. */
2089 static void
2090 check_window_end (w)
2091 struct window *w;
2093 if (!MINI_WINDOW_P (w)
2094 && !NILP (w->window_end_valid))
2096 struct glyph_row *row;
2097 xassert ((row = MATRIX_ROW (w->current_matrix,
2098 XFASTINT (w->window_end_vpos)),
2099 !row->enabled_p
2100 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2101 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2105 #define CHECK_WINDOW_END(W) check_window_end ((W))
2107 #else /* not GLYPH_DEBUG */
2109 #define CHECK_WINDOW_END(W) (void) 0
2111 #endif /* not GLYPH_DEBUG */
2115 /***********************************************************************
2116 Iterator initialization
2117 ***********************************************************************/
2119 /* Initialize IT for displaying current_buffer in window W, starting
2120 at character position CHARPOS. CHARPOS < 0 means that no buffer
2121 position is specified which is useful when the iterator is assigned
2122 a position later. BYTEPOS is the byte position corresponding to
2123 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2125 If ROW is not null, calls to produce_glyphs with IT as parameter
2126 will produce glyphs in that row.
2128 BASE_FACE_ID is the id of a base face to use. It must be one of
2129 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2131 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2133 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2134 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2135 will be initialized to use the corresponding mode line glyph row of
2136 the desired matrix of W. */
2138 void
2139 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2140 struct it *it;
2141 struct window *w;
2142 int charpos, bytepos;
2143 struct glyph_row *row;
2144 enum face_id base_face_id;
2146 int highlight_region_p;
2148 /* Some precondition checks. */
2149 xassert (w != NULL && it != NULL);
2150 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2151 && charpos <= ZV));
2153 /* If face attributes have been changed since the last redisplay,
2154 free realized faces now because they depend on face definitions
2155 that might have changed. Don't free faces while there might be
2156 desired matrices pending which reference these faces. */
2157 if (face_change_count && !inhibit_free_realized_faces)
2159 face_change_count = 0;
2160 free_all_realized_faces (Qnil);
2163 /* Use one of the mode line rows of W's desired matrix if
2164 appropriate. */
2165 if (row == NULL)
2167 if (base_face_id == MODE_LINE_FACE_ID
2168 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2169 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2170 else if (base_face_id == HEADER_LINE_FACE_ID)
2171 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2174 /* Clear IT. */
2175 bzero (it, sizeof *it);
2176 it->current.overlay_string_index = -1;
2177 it->current.dpvec_index = -1;
2178 it->base_face_id = base_face_id;
2179 it->string = Qnil;
2180 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2182 /* The window in which we iterate over current_buffer: */
2183 XSETWINDOW (it->window, w);
2184 it->w = w;
2185 it->f = XFRAME (w->frame);
2187 /* Extra space between lines (on window systems only). */
2188 if (base_face_id == DEFAULT_FACE_ID
2189 && FRAME_WINDOW_P (it->f))
2191 if (NATNUMP (current_buffer->extra_line_spacing))
2192 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2193 else if (FLOATP (current_buffer->extra_line_spacing))
2194 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2195 * FRAME_LINE_HEIGHT (it->f));
2196 else if (it->f->extra_line_spacing > 0)
2197 it->extra_line_spacing = it->f->extra_line_spacing;
2198 it->max_extra_line_spacing = 0;
2201 /* If realized faces have been removed, e.g. because of face
2202 attribute changes of named faces, recompute them. When running
2203 in batch mode, the face cache of Vterminal_frame is null. If
2204 we happen to get called, make a dummy face cache. */
2205 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2206 init_frame_faces (it->f);
2207 if (FRAME_FACE_CACHE (it->f)->used == 0)
2208 recompute_basic_faces (it->f);
2210 /* Current value of the `slice', `space-width', and 'height' properties. */
2211 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2212 it->space_width = Qnil;
2213 it->font_height = Qnil;
2214 it->override_ascent = -1;
2216 /* Are control characters displayed as `^C'? */
2217 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2219 /* -1 means everything between a CR and the following line end
2220 is invisible. >0 means lines indented more than this value are
2221 invisible. */
2222 it->selective = (INTEGERP (current_buffer->selective_display)
2223 ? XFASTINT (current_buffer->selective_display)
2224 : (!NILP (current_buffer->selective_display)
2225 ? -1 : 0));
2226 it->selective_display_ellipsis_p
2227 = !NILP (current_buffer->selective_display_ellipses);
2229 /* Display table to use. */
2230 it->dp = window_display_table (w);
2232 /* Are multibyte characters enabled in current_buffer? */
2233 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2235 /* Non-zero if we should highlight the region. */
2236 highlight_region_p
2237 = (!NILP (Vtransient_mark_mode)
2238 && !NILP (current_buffer->mark_active)
2239 && XMARKER (current_buffer->mark)->buffer != 0);
2241 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2242 start and end of a visible region in window IT->w. Set both to
2243 -1 to indicate no region. */
2244 if (highlight_region_p
2245 /* Maybe highlight only in selected window. */
2246 && (/* Either show region everywhere. */
2247 highlight_nonselected_windows
2248 /* Or show region in the selected window. */
2249 || w == XWINDOW (selected_window)
2250 /* Or show the region if we are in the mini-buffer and W is
2251 the window the mini-buffer refers to. */
2252 || (MINI_WINDOW_P (XWINDOW (selected_window))
2253 && WINDOWP (minibuf_selected_window)
2254 && w == XWINDOW (minibuf_selected_window))))
2256 int charpos = marker_position (current_buffer->mark);
2257 it->region_beg_charpos = min (PT, charpos);
2258 it->region_end_charpos = max (PT, charpos);
2260 else
2261 it->region_beg_charpos = it->region_end_charpos = -1;
2263 /* Get the position at which the redisplay_end_trigger hook should
2264 be run, if it is to be run at all. */
2265 if (MARKERP (w->redisplay_end_trigger)
2266 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2267 it->redisplay_end_trigger_charpos
2268 = marker_position (w->redisplay_end_trigger);
2269 else if (INTEGERP (w->redisplay_end_trigger))
2270 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2272 /* Correct bogus values of tab_width. */
2273 it->tab_width = XINT (current_buffer->tab_width);
2274 if (it->tab_width <= 0 || it->tab_width > 1000)
2275 it->tab_width = 8;
2277 /* Are lines in the display truncated? */
2278 it->truncate_lines_p
2279 = (base_face_id != DEFAULT_FACE_ID
2280 || XINT (it->w->hscroll)
2281 || (truncate_partial_width_windows
2282 && !WINDOW_FULL_WIDTH_P (it->w))
2283 || !NILP (current_buffer->truncate_lines));
2285 /* Get dimensions of truncation and continuation glyphs. These are
2286 displayed as fringe bitmaps under X, so we don't need them for such
2287 frames. */
2288 if (!FRAME_WINDOW_P (it->f))
2290 if (it->truncate_lines_p)
2292 /* We will need the truncation glyph. */
2293 xassert (it->glyph_row == NULL);
2294 produce_special_glyphs (it, IT_TRUNCATION);
2295 it->truncation_pixel_width = it->pixel_width;
2297 else
2299 /* We will need the continuation glyph. */
2300 xassert (it->glyph_row == NULL);
2301 produce_special_glyphs (it, IT_CONTINUATION);
2302 it->continuation_pixel_width = it->pixel_width;
2305 /* Reset these values to zero because the produce_special_glyphs
2306 above has changed them. */
2307 it->pixel_width = it->ascent = it->descent = 0;
2308 it->phys_ascent = it->phys_descent = 0;
2311 /* Set this after getting the dimensions of truncation and
2312 continuation glyphs, so that we don't produce glyphs when calling
2313 produce_special_glyphs, above. */
2314 it->glyph_row = row;
2315 it->area = TEXT_AREA;
2317 /* Get the dimensions of the display area. The display area
2318 consists of the visible window area plus a horizontally scrolled
2319 part to the left of the window. All x-values are relative to the
2320 start of this total display area. */
2321 if (base_face_id != DEFAULT_FACE_ID)
2323 /* Mode lines, menu bar in terminal frames. */
2324 it->first_visible_x = 0;
2325 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2327 else
2329 it->first_visible_x
2330 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2331 it->last_visible_x = (it->first_visible_x
2332 + window_box_width (w, TEXT_AREA));
2334 /* If we truncate lines, leave room for the truncator glyph(s) at
2335 the right margin. Otherwise, leave room for the continuation
2336 glyph(s). Truncation and continuation glyphs are not inserted
2337 for window-based redisplay. */
2338 if (!FRAME_WINDOW_P (it->f))
2340 if (it->truncate_lines_p)
2341 it->last_visible_x -= it->truncation_pixel_width;
2342 else
2343 it->last_visible_x -= it->continuation_pixel_width;
2346 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2347 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2350 /* Leave room for a border glyph. */
2351 if (!FRAME_WINDOW_P (it->f)
2352 && !WINDOW_RIGHTMOST_P (it->w))
2353 it->last_visible_x -= 1;
2355 it->last_visible_y = window_text_bottom_y (w);
2357 /* For mode lines and alike, arrange for the first glyph having a
2358 left box line if the face specifies a box. */
2359 if (base_face_id != DEFAULT_FACE_ID)
2361 struct face *face;
2363 it->face_id = base_face_id;
2365 /* If we have a boxed mode line, make the first character appear
2366 with a left box line. */
2367 face = FACE_FROM_ID (it->f, base_face_id);
2368 if (face->box != FACE_NO_BOX)
2369 it->start_of_box_run_p = 1;
2372 /* If a buffer position was specified, set the iterator there,
2373 getting overlays and face properties from that position. */
2374 if (charpos >= BUF_BEG (current_buffer))
2376 it->end_charpos = ZV;
2377 it->face_id = -1;
2378 IT_CHARPOS (*it) = charpos;
2380 /* Compute byte position if not specified. */
2381 if (bytepos < charpos)
2382 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2383 else
2384 IT_BYTEPOS (*it) = bytepos;
2386 it->start = it->current;
2388 /* Compute faces etc. */
2389 reseat (it, it->current.pos, 1);
2392 CHECK_IT (it);
2396 /* Initialize IT for the display of window W with window start POS. */
2398 void
2399 start_display (it, w, pos)
2400 struct it *it;
2401 struct window *w;
2402 struct text_pos pos;
2404 struct glyph_row *row;
2405 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2407 row = w->desired_matrix->rows + first_vpos;
2408 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2409 it->first_vpos = first_vpos;
2411 /* Don't reseat to previous visible line start if current start
2412 position is in a string or image. */
2413 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2415 int start_at_line_beg_p;
2416 int first_y = it->current_y;
2418 /* If window start is not at a line start, skip forward to POS to
2419 get the correct continuation lines width. */
2420 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2421 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2422 if (!start_at_line_beg_p)
2424 int new_x;
2426 reseat_at_previous_visible_line_start (it);
2427 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2429 new_x = it->current_x + it->pixel_width;
2431 /* If lines are continued, this line may end in the middle
2432 of a multi-glyph character (e.g. a control character
2433 displayed as \003, or in the middle of an overlay
2434 string). In this case move_it_to above will not have
2435 taken us to the start of the continuation line but to the
2436 end of the continued line. */
2437 if (it->current_x > 0
2438 && !it->truncate_lines_p /* Lines are continued. */
2439 && (/* And glyph doesn't fit on the line. */
2440 new_x > it->last_visible_x
2441 /* Or it fits exactly and we're on a window
2442 system frame. */
2443 || (new_x == it->last_visible_x
2444 && FRAME_WINDOW_P (it->f))))
2446 if (it->current.dpvec_index >= 0
2447 || it->current.overlay_string_index >= 0)
2449 set_iterator_to_next (it, 1);
2450 move_it_in_display_line_to (it, -1, -1, 0);
2453 it->continuation_lines_width += it->current_x;
2456 /* We're starting a new display line, not affected by the
2457 height of the continued line, so clear the appropriate
2458 fields in the iterator structure. */
2459 it->max_ascent = it->max_descent = 0;
2460 it->max_phys_ascent = it->max_phys_descent = 0;
2462 it->current_y = first_y;
2463 it->vpos = 0;
2464 it->current_x = it->hpos = 0;
2468 #if 0 /* Don't assert the following because start_display is sometimes
2469 called intentionally with a window start that is not at a
2470 line start. Please leave this code in as a comment. */
2472 /* Window start should be on a line start, now. */
2473 xassert (it->continuation_lines_width
2474 || IT_CHARPOS (it) == BEGV
2475 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2476 #endif /* 0 */
2480 /* Return 1 if POS is a position in ellipses displayed for invisible
2481 text. W is the window we display, for text property lookup. */
2483 static int
2484 in_ellipses_for_invisible_text_p (pos, w)
2485 struct display_pos *pos;
2486 struct window *w;
2488 Lisp_Object prop, window;
2489 int ellipses_p = 0;
2490 int charpos = CHARPOS (pos->pos);
2492 /* If POS specifies a position in a display vector, this might
2493 be for an ellipsis displayed for invisible text. We won't
2494 get the iterator set up for delivering that ellipsis unless
2495 we make sure that it gets aware of the invisible text. */
2496 if (pos->dpvec_index >= 0
2497 && pos->overlay_string_index < 0
2498 && CHARPOS (pos->string_pos) < 0
2499 && charpos > BEGV
2500 && (XSETWINDOW (window, w),
2501 prop = Fget_char_property (make_number (charpos),
2502 Qinvisible, window),
2503 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2505 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2506 window);
2507 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2510 return ellipses_p;
2514 /* Initialize IT for stepping through current_buffer in window W,
2515 starting at position POS that includes overlay string and display
2516 vector/ control character translation position information. Value
2517 is zero if there are overlay strings with newlines at POS. */
2519 static int
2520 init_from_display_pos (it, w, pos)
2521 struct it *it;
2522 struct window *w;
2523 struct display_pos *pos;
2525 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2526 int i, overlay_strings_with_newlines = 0;
2528 /* If POS specifies a position in a display vector, this might
2529 be for an ellipsis displayed for invisible text. We won't
2530 get the iterator set up for delivering that ellipsis unless
2531 we make sure that it gets aware of the invisible text. */
2532 if (in_ellipses_for_invisible_text_p (pos, w))
2534 --charpos;
2535 bytepos = 0;
2538 /* Keep in mind: the call to reseat in init_iterator skips invisible
2539 text, so we might end up at a position different from POS. This
2540 is only a problem when POS is a row start after a newline and an
2541 overlay starts there with an after-string, and the overlay has an
2542 invisible property. Since we don't skip invisible text in
2543 display_line and elsewhere immediately after consuming the
2544 newline before the row start, such a POS will not be in a string,
2545 but the call to init_iterator below will move us to the
2546 after-string. */
2547 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2549 /* This only scans the current chunk -- it should scan all chunks.
2550 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2551 to 16 in 22.1 to make this a lesser problem. */
2552 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2554 const char *s = SDATA (it->overlay_strings[i]);
2555 const char *e = s + SBYTES (it->overlay_strings[i]);
2557 while (s < e && *s != '\n')
2558 ++s;
2560 if (s < e)
2562 overlay_strings_with_newlines = 1;
2563 break;
2567 /* If position is within an overlay string, set up IT to the right
2568 overlay string. */
2569 if (pos->overlay_string_index >= 0)
2571 int relative_index;
2573 /* If the first overlay string happens to have a `display'
2574 property for an image, the iterator will be set up for that
2575 image, and we have to undo that setup first before we can
2576 correct the overlay string index. */
2577 if (it->method == GET_FROM_IMAGE)
2578 pop_it (it);
2580 /* We already have the first chunk of overlay strings in
2581 IT->overlay_strings. Load more until the one for
2582 pos->overlay_string_index is in IT->overlay_strings. */
2583 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2585 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2586 it->current.overlay_string_index = 0;
2587 while (n--)
2589 load_overlay_strings (it, 0);
2590 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2594 it->current.overlay_string_index = pos->overlay_string_index;
2595 relative_index = (it->current.overlay_string_index
2596 % OVERLAY_STRING_CHUNK_SIZE);
2597 it->string = it->overlay_strings[relative_index];
2598 xassert (STRINGP (it->string));
2599 it->current.string_pos = pos->string_pos;
2600 it->method = GET_FROM_STRING;
2603 #if 0 /* This is bogus because POS not having an overlay string
2604 position does not mean it's after the string. Example: A
2605 line starting with a before-string and initialization of IT
2606 to the previous row's end position. */
2607 else if (it->current.overlay_string_index >= 0)
2609 /* If POS says we're already after an overlay string ending at
2610 POS, make sure to pop the iterator because it will be in
2611 front of that overlay string. When POS is ZV, we've thereby
2612 also ``processed'' overlay strings at ZV. */
2613 while (it->sp)
2614 pop_it (it);
2615 it->current.overlay_string_index = -1;
2616 it->method = GET_FROM_BUFFER;
2617 if (CHARPOS (pos->pos) == ZV)
2618 it->overlay_strings_at_end_processed_p = 1;
2620 #endif /* 0 */
2622 if (CHARPOS (pos->string_pos) >= 0)
2624 /* Recorded position is not in an overlay string, but in another
2625 string. This can only be a string from a `display' property.
2626 IT should already be filled with that string. */
2627 it->current.string_pos = pos->string_pos;
2628 xassert (STRINGP (it->string));
2631 /* Restore position in display vector translations, control
2632 character translations or ellipses. */
2633 if (pos->dpvec_index >= 0)
2635 if (it->dpvec == NULL)
2636 get_next_display_element (it);
2637 xassert (it->dpvec && it->current.dpvec_index == 0);
2638 it->current.dpvec_index = pos->dpvec_index;
2641 CHECK_IT (it);
2642 return !overlay_strings_with_newlines;
2646 /* Initialize IT for stepping through current_buffer in window W
2647 starting at ROW->start. */
2649 static void
2650 init_to_row_start (it, w, row)
2651 struct it *it;
2652 struct window *w;
2653 struct glyph_row *row;
2655 init_from_display_pos (it, w, &row->start);
2656 it->start = row->start;
2657 it->continuation_lines_width = row->continuation_lines_width;
2658 CHECK_IT (it);
2662 /* Initialize IT for stepping through current_buffer in window W
2663 starting in the line following ROW, i.e. starting at ROW->end.
2664 Value is zero if there are overlay strings with newlines at ROW's
2665 end position. */
2667 static int
2668 init_to_row_end (it, w, row)
2669 struct it *it;
2670 struct window *w;
2671 struct glyph_row *row;
2673 int success = 0;
2675 if (init_from_display_pos (it, w, &row->end))
2677 if (row->continued_p)
2678 it->continuation_lines_width
2679 = row->continuation_lines_width + row->pixel_width;
2680 CHECK_IT (it);
2681 success = 1;
2684 return success;
2690 /***********************************************************************
2691 Text properties
2692 ***********************************************************************/
2694 /* Called when IT reaches IT->stop_charpos. Handle text property and
2695 overlay changes. Set IT->stop_charpos to the next position where
2696 to stop. */
2698 static void
2699 handle_stop (it)
2700 struct it *it;
2702 enum prop_handled handled;
2703 int handle_overlay_change_p = 1;
2704 struct props *p;
2706 it->dpvec = NULL;
2707 it->current.dpvec_index = -1;
2709 /* Use face of preceding text for ellipsis (if invisible) */
2710 if (it->selective_display_ellipsis_p)
2711 it->saved_face_id = it->face_id;
2715 handled = HANDLED_NORMALLY;
2717 /* Call text property handlers. */
2718 for (p = it_props; p->handler; ++p)
2720 handled = p->handler (it);
2722 if (handled == HANDLED_RECOMPUTE_PROPS)
2723 break;
2724 else if (handled == HANDLED_RETURN)
2725 return;
2726 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2727 handle_overlay_change_p = 0;
2730 if (handled != HANDLED_RECOMPUTE_PROPS)
2732 /* Don't check for overlay strings below when set to deliver
2733 characters from a display vector. */
2734 if (it->method == GET_FROM_DISPLAY_VECTOR)
2735 handle_overlay_change_p = 0;
2737 /* Handle overlay changes. */
2738 if (handle_overlay_change_p)
2739 handled = handle_overlay_change (it);
2741 /* Determine where to stop next. */
2742 if (handled == HANDLED_NORMALLY)
2743 compute_stop_pos (it);
2746 while (handled == HANDLED_RECOMPUTE_PROPS);
2750 /* Compute IT->stop_charpos from text property and overlay change
2751 information for IT's current position. */
2753 static void
2754 compute_stop_pos (it)
2755 struct it *it;
2757 register INTERVAL iv, next_iv;
2758 Lisp_Object object, limit, position;
2760 /* If nowhere else, stop at the end. */
2761 it->stop_charpos = it->end_charpos;
2763 if (STRINGP (it->string))
2765 /* Strings are usually short, so don't limit the search for
2766 properties. */
2767 object = it->string;
2768 limit = Qnil;
2769 position = make_number (IT_STRING_CHARPOS (*it));
2771 else
2773 int charpos;
2775 /* If next overlay change is in front of the current stop pos
2776 (which is IT->end_charpos), stop there. Note: value of
2777 next_overlay_change is point-max if no overlay change
2778 follows. */
2779 charpos = next_overlay_change (IT_CHARPOS (*it));
2780 if (charpos < it->stop_charpos)
2781 it->stop_charpos = charpos;
2783 /* If showing the region, we have to stop at the region
2784 start or end because the face might change there. */
2785 if (it->region_beg_charpos > 0)
2787 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2788 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2789 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2790 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2793 /* Set up variables for computing the stop position from text
2794 property changes. */
2795 XSETBUFFER (object, current_buffer);
2796 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2797 position = make_number (IT_CHARPOS (*it));
2801 /* Get the interval containing IT's position. Value is a null
2802 interval if there isn't such an interval. */
2803 iv = validate_interval_range (object, &position, &position, 0);
2804 if (!NULL_INTERVAL_P (iv))
2806 Lisp_Object values_here[LAST_PROP_IDX];
2807 struct props *p;
2809 /* Get properties here. */
2810 for (p = it_props; p->handler; ++p)
2811 values_here[p->idx] = textget (iv->plist, *p->name);
2813 /* Look for an interval following iv that has different
2814 properties. */
2815 for (next_iv = next_interval (iv);
2816 (!NULL_INTERVAL_P (next_iv)
2817 && (NILP (limit)
2818 || XFASTINT (limit) > next_iv->position));
2819 next_iv = next_interval (next_iv))
2821 for (p = it_props; p->handler; ++p)
2823 Lisp_Object new_value;
2825 new_value = textget (next_iv->plist, *p->name);
2826 if (!EQ (values_here[p->idx], new_value))
2827 break;
2830 if (p->handler)
2831 break;
2834 if (!NULL_INTERVAL_P (next_iv))
2836 if (INTEGERP (limit)
2837 && next_iv->position >= XFASTINT (limit))
2838 /* No text property change up to limit. */
2839 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2840 else
2841 /* Text properties change in next_iv. */
2842 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2846 xassert (STRINGP (it->string)
2847 || (it->stop_charpos >= BEGV
2848 && it->stop_charpos >= IT_CHARPOS (*it)));
2852 /* Return the position of the next overlay change after POS in
2853 current_buffer. Value is point-max if no overlay change
2854 follows. This is like `next-overlay-change' but doesn't use
2855 xmalloc. */
2857 static int
2858 next_overlay_change (pos)
2859 int pos;
2861 int noverlays;
2862 int endpos;
2863 Lisp_Object *overlays;
2864 int i;
2866 /* Get all overlays at the given position. */
2867 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2869 /* If any of these overlays ends before endpos,
2870 use its ending point instead. */
2871 for (i = 0; i < noverlays; ++i)
2873 Lisp_Object oend;
2874 int oendpos;
2876 oend = OVERLAY_END (overlays[i]);
2877 oendpos = OVERLAY_POSITION (oend);
2878 endpos = min (endpos, oendpos);
2881 return endpos;
2886 /***********************************************************************
2887 Fontification
2888 ***********************************************************************/
2890 /* Handle changes in the `fontified' property of the current buffer by
2891 calling hook functions from Qfontification_functions to fontify
2892 regions of text. */
2894 static enum prop_handled
2895 handle_fontified_prop (it)
2896 struct it *it;
2898 Lisp_Object prop, pos;
2899 enum prop_handled handled = HANDLED_NORMALLY;
2901 /* Get the value of the `fontified' property at IT's current buffer
2902 position. (The `fontified' property doesn't have a special
2903 meaning in strings.) If the value is nil, call functions from
2904 Qfontification_functions. */
2905 if (!STRINGP (it->string)
2906 && it->s == NULL
2907 && !NILP (Vfontification_functions)
2908 && !NILP (Vrun_hooks)
2909 && (pos = make_number (IT_CHARPOS (*it)),
2910 prop = Fget_char_property (pos, Qfontified, Qnil),
2911 NILP (prop)))
2913 int count = SPECPDL_INDEX ();
2914 Lisp_Object val;
2916 val = Vfontification_functions;
2917 specbind (Qfontification_functions, Qnil);
2919 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2920 safe_call1 (val, pos);
2921 else
2923 Lisp_Object globals, fn;
2924 struct gcpro gcpro1, gcpro2;
2926 globals = Qnil;
2927 GCPRO2 (val, globals);
2929 for (; CONSP (val); val = XCDR (val))
2931 fn = XCAR (val);
2933 if (EQ (fn, Qt))
2935 /* A value of t indicates this hook has a local
2936 binding; it means to run the global binding too.
2937 In a global value, t should not occur. If it
2938 does, we must ignore it to avoid an endless
2939 loop. */
2940 for (globals = Fdefault_value (Qfontification_functions);
2941 CONSP (globals);
2942 globals = XCDR (globals))
2944 fn = XCAR (globals);
2945 if (!EQ (fn, Qt))
2946 safe_call1 (fn, pos);
2949 else
2950 safe_call1 (fn, pos);
2953 UNGCPRO;
2956 unbind_to (count, Qnil);
2958 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2959 something. This avoids an endless loop if they failed to
2960 fontify the text for which reason ever. */
2961 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2962 handled = HANDLED_RECOMPUTE_PROPS;
2965 return handled;
2970 /***********************************************************************
2971 Faces
2972 ***********************************************************************/
2974 /* Set up iterator IT from face properties at its current position.
2975 Called from handle_stop. */
2977 static enum prop_handled
2978 handle_face_prop (it)
2979 struct it *it;
2981 int new_face_id, next_stop;
2983 if (!STRINGP (it->string))
2985 new_face_id
2986 = face_at_buffer_position (it->w,
2987 IT_CHARPOS (*it),
2988 it->region_beg_charpos,
2989 it->region_end_charpos,
2990 &next_stop,
2991 (IT_CHARPOS (*it)
2992 + TEXT_PROP_DISTANCE_LIMIT),
2995 /* Is this a start of a run of characters with box face?
2996 Caveat: this can be called for a freshly initialized
2997 iterator; face_id is -1 in this case. We know that the new
2998 face will not change until limit, i.e. if the new face has a
2999 box, all characters up to limit will have one. But, as
3000 usual, we don't know whether limit is really the end. */
3001 if (new_face_id != it->face_id)
3003 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3005 /* If new face has a box but old face has not, this is
3006 the start of a run of characters with box, i.e. it has
3007 a shadow on the left side. The value of face_id of the
3008 iterator will be -1 if this is the initial call that gets
3009 the face. In this case, we have to look in front of IT's
3010 position and see whether there is a face != new_face_id. */
3011 it->start_of_box_run_p
3012 = (new_face->box != FACE_NO_BOX
3013 && (it->face_id >= 0
3014 || IT_CHARPOS (*it) == BEG
3015 || new_face_id != face_before_it_pos (it)));
3016 it->face_box_p = new_face->box != FACE_NO_BOX;
3019 else
3021 int base_face_id, bufpos;
3023 if (it->current.overlay_string_index >= 0)
3024 bufpos = IT_CHARPOS (*it);
3025 else
3026 bufpos = 0;
3028 /* For strings from a buffer, i.e. overlay strings or strings
3029 from a `display' property, use the face at IT's current
3030 buffer position as the base face to merge with, so that
3031 overlay strings appear in the same face as surrounding
3032 text, unless they specify their own faces. */
3033 base_face_id = underlying_face_id (it);
3035 new_face_id = face_at_string_position (it->w,
3036 it->string,
3037 IT_STRING_CHARPOS (*it),
3038 bufpos,
3039 it->region_beg_charpos,
3040 it->region_end_charpos,
3041 &next_stop,
3042 base_face_id, 0);
3044 #if 0 /* This shouldn't be neccessary. Let's check it. */
3045 /* If IT is used to display a mode line we would really like to
3046 use the mode line face instead of the frame's default face. */
3047 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3048 && new_face_id == DEFAULT_FACE_ID)
3049 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3050 #endif
3052 /* Is this a start of a run of characters with box? Caveat:
3053 this can be called for a freshly allocated iterator; face_id
3054 is -1 is this case. We know that the new face will not
3055 change until the next check pos, i.e. if the new face has a
3056 box, all characters up to that position will have a
3057 box. But, as usual, we don't know whether that position
3058 is really the end. */
3059 if (new_face_id != it->face_id)
3061 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3062 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3064 /* If new face has a box but old face hasn't, this is the
3065 start of a run of characters with box, i.e. it has a
3066 shadow on the left side. */
3067 it->start_of_box_run_p
3068 = new_face->box && (old_face == NULL || !old_face->box);
3069 it->face_box_p = new_face->box != FACE_NO_BOX;
3073 it->face_id = new_face_id;
3074 return HANDLED_NORMALLY;
3078 /* Return the ID of the face ``underlying'' IT's current position,
3079 which is in a string. If the iterator is associated with a
3080 buffer, return the face at IT's current buffer position.
3081 Otherwise, use the iterator's base_face_id. */
3083 static int
3084 underlying_face_id (it)
3085 struct it *it;
3087 int face_id = it->base_face_id, i;
3089 xassert (STRINGP (it->string));
3091 for (i = it->sp - 1; i >= 0; --i)
3092 if (NILP (it->stack[i].string))
3093 face_id = it->stack[i].face_id;
3095 return face_id;
3099 /* Compute the face one character before or after the current position
3100 of IT. BEFORE_P non-zero means get the face in front of IT's
3101 position. Value is the id of the face. */
3103 static int
3104 face_before_or_after_it_pos (it, before_p)
3105 struct it *it;
3106 int before_p;
3108 int face_id, limit;
3109 int next_check_charpos;
3110 struct text_pos pos;
3112 xassert (it->s == NULL);
3114 if (STRINGP (it->string))
3116 int bufpos, base_face_id;
3118 /* No face change past the end of the string (for the case
3119 we are padding with spaces). No face change before the
3120 string start. */
3121 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3122 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3123 return it->face_id;
3125 /* Set pos to the position before or after IT's current position. */
3126 if (before_p)
3127 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3128 else
3129 /* For composition, we must check the character after the
3130 composition. */
3131 pos = (it->what == IT_COMPOSITION
3132 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3133 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3135 if (it->current.overlay_string_index >= 0)
3136 bufpos = IT_CHARPOS (*it);
3137 else
3138 bufpos = 0;
3140 base_face_id = underlying_face_id (it);
3142 /* Get the face for ASCII, or unibyte. */
3143 face_id = face_at_string_position (it->w,
3144 it->string,
3145 CHARPOS (pos),
3146 bufpos,
3147 it->region_beg_charpos,
3148 it->region_end_charpos,
3149 &next_check_charpos,
3150 base_face_id, 0);
3152 /* Correct the face for charsets different from ASCII. Do it
3153 for the multibyte case only. The face returned above is
3154 suitable for unibyte text if IT->string is unibyte. */
3155 if (STRING_MULTIBYTE (it->string))
3157 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3158 int rest = SBYTES (it->string) - BYTEPOS (pos);
3159 int c, len;
3160 struct face *face = FACE_FROM_ID (it->f, face_id);
3162 c = string_char_and_length (p, rest, &len);
3163 face_id = FACE_FOR_CHAR (it->f, face, c);
3166 else
3168 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3169 || (IT_CHARPOS (*it) <= BEGV && before_p))
3170 return it->face_id;
3172 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3173 pos = it->current.pos;
3175 if (before_p)
3176 DEC_TEXT_POS (pos, it->multibyte_p);
3177 else
3179 if (it->what == IT_COMPOSITION)
3180 /* For composition, we must check the position after the
3181 composition. */
3182 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3183 else
3184 INC_TEXT_POS (pos, it->multibyte_p);
3187 /* Determine face for CHARSET_ASCII, or unibyte. */
3188 face_id = face_at_buffer_position (it->w,
3189 CHARPOS (pos),
3190 it->region_beg_charpos,
3191 it->region_end_charpos,
3192 &next_check_charpos,
3193 limit, 0);
3195 /* Correct the face for charsets different from ASCII. Do it
3196 for the multibyte case only. The face returned above is
3197 suitable for unibyte text if current_buffer is unibyte. */
3198 if (it->multibyte_p)
3200 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3201 struct face *face = FACE_FROM_ID (it->f, face_id);
3202 face_id = FACE_FOR_CHAR (it->f, face, c);
3206 return face_id;
3211 /***********************************************************************
3212 Invisible text
3213 ***********************************************************************/
3215 /* Set up iterator IT from invisible properties at its current
3216 position. Called from handle_stop. */
3218 static enum prop_handled
3219 handle_invisible_prop (it)
3220 struct it *it;
3222 enum prop_handled handled = HANDLED_NORMALLY;
3224 if (STRINGP (it->string))
3226 extern Lisp_Object Qinvisible;
3227 Lisp_Object prop, end_charpos, limit, charpos;
3229 /* Get the value of the invisible text property at the
3230 current position. Value will be nil if there is no such
3231 property. */
3232 charpos = make_number (IT_STRING_CHARPOS (*it));
3233 prop = Fget_text_property (charpos, Qinvisible, it->string);
3235 if (!NILP (prop)
3236 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3238 handled = HANDLED_RECOMPUTE_PROPS;
3240 /* Get the position at which the next change of the
3241 invisible text property can be found in IT->string.
3242 Value will be nil if the property value is the same for
3243 all the rest of IT->string. */
3244 XSETINT (limit, SCHARS (it->string));
3245 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3246 it->string, limit);
3248 /* Text at current position is invisible. The next
3249 change in the property is at position end_charpos.
3250 Move IT's current position to that position. */
3251 if (INTEGERP (end_charpos)
3252 && XFASTINT (end_charpos) < XFASTINT (limit))
3254 struct text_pos old;
3255 old = it->current.string_pos;
3256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3257 compute_string_pos (&it->current.string_pos, old, it->string);
3259 else
3261 /* The rest of the string is invisible. If this is an
3262 overlay string, proceed with the next overlay string
3263 or whatever comes and return a character from there. */
3264 if (it->current.overlay_string_index >= 0)
3266 next_overlay_string (it);
3267 /* Don't check for overlay strings when we just
3268 finished processing them. */
3269 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3271 else
3273 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3274 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3279 else
3281 int invis_p, newpos, next_stop, start_charpos;
3282 Lisp_Object pos, prop, overlay;
3284 /* First of all, is there invisible text at this position? */
3285 start_charpos = IT_CHARPOS (*it);
3286 pos = make_number (IT_CHARPOS (*it));
3287 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3288 &overlay);
3289 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3291 /* If we are on invisible text, skip over it. */
3292 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3294 /* Record whether we have to display an ellipsis for the
3295 invisible text. */
3296 int display_ellipsis_p = invis_p == 2;
3298 handled = HANDLED_RECOMPUTE_PROPS;
3300 /* Loop skipping over invisible text. The loop is left at
3301 ZV or with IT on the first char being visible again. */
3304 /* Try to skip some invisible text. Return value is the
3305 position reached which can be equal to IT's position
3306 if there is nothing invisible here. This skips both
3307 over invisible text properties and overlays with
3308 invisible property. */
3309 newpos = skip_invisible (IT_CHARPOS (*it),
3310 &next_stop, ZV, it->window);
3312 /* If we skipped nothing at all we weren't at invisible
3313 text in the first place. If everything to the end of
3314 the buffer was skipped, end the loop. */
3315 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3316 invis_p = 0;
3317 else
3319 /* We skipped some characters but not necessarily
3320 all there are. Check if we ended up on visible
3321 text. Fget_char_property returns the property of
3322 the char before the given position, i.e. if we
3323 get invis_p = 0, this means that the char at
3324 newpos is visible. */
3325 pos = make_number (newpos);
3326 prop = Fget_char_property (pos, Qinvisible, it->window);
3327 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3330 /* If we ended up on invisible text, proceed to
3331 skip starting with next_stop. */
3332 if (invis_p)
3333 IT_CHARPOS (*it) = next_stop;
3335 while (invis_p);
3337 /* The position newpos is now either ZV or on visible text. */
3338 IT_CHARPOS (*it) = newpos;
3339 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3341 /* If there are before-strings at the start of invisible
3342 text, and the text is invisible because of a text
3343 property, arrange to show before-strings because 20.x did
3344 it that way. (If the text is invisible because of an
3345 overlay property instead of a text property, this is
3346 already handled in the overlay code.) */
3347 if (NILP (overlay)
3348 && get_overlay_strings (it, start_charpos))
3350 handled = HANDLED_RECOMPUTE_PROPS;
3351 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3353 else if (display_ellipsis_p)
3354 setup_for_ellipsis (it, 0);
3358 return handled;
3362 /* Make iterator IT return `...' next.
3363 Replaces LEN characters from buffer. */
3365 static void
3366 setup_for_ellipsis (it, len)
3367 struct it *it;
3368 int len;
3370 /* Use the display table definition for `...'. Invalid glyphs
3371 will be handled by the method returning elements from dpvec. */
3372 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3374 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3375 it->dpvec = v->contents;
3376 it->dpend = v->contents + v->size;
3378 else
3380 /* Default `...'. */
3381 it->dpvec = default_invis_vector;
3382 it->dpend = default_invis_vector + 3;
3385 it->dpvec_char_len = len;
3386 it->current.dpvec_index = 0;
3387 it->dpvec_face_id = -1;
3389 /* Remember the current face id in case glyphs specify faces.
3390 IT's face is restored in set_iterator_to_next.
3391 saved_face_id was set to preceding char's face in handle_stop. */
3392 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3393 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3395 it->method = GET_FROM_DISPLAY_VECTOR;
3396 it->ellipsis_p = 1;
3401 /***********************************************************************
3402 'display' property
3403 ***********************************************************************/
3405 /* Set up iterator IT from `display' property at its current position.
3406 Called from handle_stop.
3407 We return HANDLED_RETURN if some part of the display property
3408 overrides the display of the buffer text itself.
3409 Otherwise we return HANDLED_NORMALLY. */
3411 static enum prop_handled
3412 handle_display_prop (it)
3413 struct it *it;
3415 Lisp_Object prop, object;
3416 struct text_pos *position;
3417 /* Nonzero if some property replaces the display of the text itself. */
3418 int display_replaced_p = 0;
3420 if (STRINGP (it->string))
3422 object = it->string;
3423 position = &it->current.string_pos;
3425 else
3427 object = it->w->buffer;
3428 position = &it->current.pos;
3431 /* Reset those iterator values set from display property values. */
3432 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3433 it->space_width = Qnil;
3434 it->font_height = Qnil;
3435 it->voffset = 0;
3437 /* We don't support recursive `display' properties, i.e. string
3438 values that have a string `display' property, that have a string
3439 `display' property etc. */
3440 if (!it->string_from_display_prop_p)
3441 it->area = TEXT_AREA;
3443 prop = Fget_char_property (make_number (position->charpos),
3444 Qdisplay, object);
3445 if (NILP (prop))
3446 return HANDLED_NORMALLY;
3448 if (CONSP (prop)
3449 /* Simple properties. */
3450 && !EQ (XCAR (prop), Qimage)
3451 && !EQ (XCAR (prop), Qspace)
3452 && !EQ (XCAR (prop), Qwhen)
3453 && !EQ (XCAR (prop), Qslice)
3454 && !EQ (XCAR (prop), Qspace_width)
3455 && !EQ (XCAR (prop), Qheight)
3456 && !EQ (XCAR (prop), Qraise)
3457 /* Marginal area specifications. */
3458 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3459 && !EQ (XCAR (prop), Qleft_fringe)
3460 && !EQ (XCAR (prop), Qright_fringe)
3461 && !NILP (XCAR (prop)))
3463 for (; CONSP (prop); prop = XCDR (prop))
3465 if (handle_single_display_spec (it, XCAR (prop), object,
3466 position, display_replaced_p))
3467 display_replaced_p = 1;
3470 else if (VECTORP (prop))
3472 int i;
3473 for (i = 0; i < ASIZE (prop); ++i)
3474 if (handle_single_display_spec (it, AREF (prop, i), object,
3475 position, display_replaced_p))
3476 display_replaced_p = 1;
3478 else
3480 int ret = handle_single_display_spec (it, prop, object, position, 0);
3481 if (ret < 0) /* Replaced by "", i.e. nothing. */
3482 return HANDLED_RECOMPUTE_PROPS;
3483 if (ret)
3484 display_replaced_p = 1;
3487 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3491 /* Value is the position of the end of the `display' property starting
3492 at START_POS in OBJECT. */
3494 static struct text_pos
3495 display_prop_end (it, object, start_pos)
3496 struct it *it;
3497 Lisp_Object object;
3498 struct text_pos start_pos;
3500 Lisp_Object end;
3501 struct text_pos end_pos;
3503 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3504 Qdisplay, object, Qnil);
3505 CHARPOS (end_pos) = XFASTINT (end);
3506 if (STRINGP (object))
3507 compute_string_pos (&end_pos, start_pos, it->string);
3508 else
3509 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3511 return end_pos;
3515 /* Set up IT from a single `display' specification PROP. OBJECT
3516 is the object in which the `display' property was found. *POSITION
3517 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3518 means that we previously saw a display specification which already
3519 replaced text display with something else, for example an image;
3520 we ignore such properties after the first one has been processed.
3522 If PROP is a `space' or `image' specification, and in some other
3523 cases too, set *POSITION to the position where the `display'
3524 property ends.
3526 Value is non-zero if something was found which replaces the display
3527 of buffer or string text. Specifically, the value is -1 if that
3528 "something" is "nothing". */
3530 static int
3531 handle_single_display_spec (it, spec, object, position,
3532 display_replaced_before_p)
3533 struct it *it;
3534 Lisp_Object spec;
3535 Lisp_Object object;
3536 struct text_pos *position;
3537 int display_replaced_before_p;
3539 Lisp_Object form;
3540 Lisp_Object location, value;
3541 struct text_pos start_pos;
3542 int valid_p;
3544 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3545 If the result is non-nil, use VALUE instead of SPEC. */
3546 form = Qt;
3547 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3549 spec = XCDR (spec);
3550 if (!CONSP (spec))
3551 return 0;
3552 form = XCAR (spec);
3553 spec = XCDR (spec);
3556 if (!NILP (form) && !EQ (form, Qt))
3558 int count = SPECPDL_INDEX ();
3559 struct gcpro gcpro1;
3561 /* Bind `object' to the object having the `display' property, a
3562 buffer or string. Bind `position' to the position in the
3563 object where the property was found, and `buffer-position'
3564 to the current position in the buffer. */
3565 specbind (Qobject, object);
3566 specbind (Qposition, make_number (CHARPOS (*position)));
3567 specbind (Qbuffer_position,
3568 make_number (STRINGP (object)
3569 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3570 GCPRO1 (form);
3571 form = safe_eval (form);
3572 UNGCPRO;
3573 unbind_to (count, Qnil);
3576 if (NILP (form))
3577 return 0;
3579 /* Handle `(height HEIGHT)' specifications. */
3580 if (CONSP (spec)
3581 && EQ (XCAR (spec), Qheight)
3582 && CONSP (XCDR (spec)))
3584 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3585 return 0;
3587 it->font_height = XCAR (XCDR (spec));
3588 if (!NILP (it->font_height))
3590 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3591 int new_height = -1;
3593 if (CONSP (it->font_height)
3594 && (EQ (XCAR (it->font_height), Qplus)
3595 || EQ (XCAR (it->font_height), Qminus))
3596 && CONSP (XCDR (it->font_height))
3597 && INTEGERP (XCAR (XCDR (it->font_height))))
3599 /* `(+ N)' or `(- N)' where N is an integer. */
3600 int steps = XINT (XCAR (XCDR (it->font_height)));
3601 if (EQ (XCAR (it->font_height), Qplus))
3602 steps = - steps;
3603 it->face_id = smaller_face (it->f, it->face_id, steps);
3605 else if (FUNCTIONP (it->font_height))
3607 /* Call function with current height as argument.
3608 Value is the new height. */
3609 Lisp_Object height;
3610 height = safe_call1 (it->font_height,
3611 face->lface[LFACE_HEIGHT_INDEX]);
3612 if (NUMBERP (height))
3613 new_height = XFLOATINT (height);
3615 else if (NUMBERP (it->font_height))
3617 /* Value is a multiple of the canonical char height. */
3618 struct face *face;
3620 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3621 new_height = (XFLOATINT (it->font_height)
3622 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3624 else
3626 /* Evaluate IT->font_height with `height' bound to the
3627 current specified height to get the new height. */
3628 int count = SPECPDL_INDEX ();
3630 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3631 value = safe_eval (it->font_height);
3632 unbind_to (count, Qnil);
3634 if (NUMBERP (value))
3635 new_height = XFLOATINT (value);
3638 if (new_height > 0)
3639 it->face_id = face_with_height (it->f, it->face_id, new_height);
3642 return 0;
3645 /* Handle `(space_width WIDTH)'. */
3646 if (CONSP (spec)
3647 && EQ (XCAR (spec), Qspace_width)
3648 && CONSP (XCDR (spec)))
3650 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3651 return 0;
3653 value = XCAR (XCDR (spec));
3654 if (NUMBERP (value) && XFLOATINT (value) > 0)
3655 it->space_width = value;
3657 return 0;
3660 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3661 if (CONSP (spec)
3662 && EQ (XCAR (spec), Qslice))
3664 Lisp_Object tem;
3666 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3667 return 0;
3669 if (tem = XCDR (spec), CONSP (tem))
3671 it->slice.x = XCAR (tem);
3672 if (tem = XCDR (tem), CONSP (tem))
3674 it->slice.y = XCAR (tem);
3675 if (tem = XCDR (tem), CONSP (tem))
3677 it->slice.width = XCAR (tem);
3678 if (tem = XCDR (tem), CONSP (tem))
3679 it->slice.height = XCAR (tem);
3684 return 0;
3687 /* Handle `(raise FACTOR)'. */
3688 if (CONSP (spec)
3689 && EQ (XCAR (spec), Qraise)
3690 && CONSP (XCDR (spec)))
3692 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3693 return 0;
3695 #ifdef HAVE_WINDOW_SYSTEM
3696 value = XCAR (XCDR (spec));
3697 if (NUMBERP (value))
3699 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3700 it->voffset = - (XFLOATINT (value)
3701 * (FONT_HEIGHT (face->font)));
3703 #endif /* HAVE_WINDOW_SYSTEM */
3705 return 0;
3708 /* Don't handle the other kinds of display specifications
3709 inside a string that we got from a `display' property. */
3710 if (it->string_from_display_prop_p)
3711 return 0;
3713 /* Characters having this form of property are not displayed, so
3714 we have to find the end of the property. */
3715 start_pos = *position;
3716 *position = display_prop_end (it, object, start_pos);
3717 value = Qnil;
3719 /* Stop the scan at that end position--we assume that all
3720 text properties change there. */
3721 it->stop_charpos = position->charpos;
3723 /* Handle `(left-fringe BITMAP [FACE])'
3724 and `(right-fringe BITMAP [FACE])'. */
3725 if (CONSP (spec)
3726 && (EQ (XCAR (spec), Qleft_fringe)
3727 || EQ (XCAR (spec), Qright_fringe))
3728 && CONSP (XCDR (spec)))
3730 int face_id = DEFAULT_FACE_ID;
3731 int fringe_bitmap;
3733 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3734 /* If we return here, POSITION has been advanced
3735 across the text with this property. */
3736 return 0;
3738 #ifdef HAVE_WINDOW_SYSTEM
3739 value = XCAR (XCDR (spec));
3740 if (!SYMBOLP (value)
3741 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3742 /* If we return here, POSITION has been advanced
3743 across the text with this property. */
3744 return 0;
3746 if (CONSP (XCDR (XCDR (spec))))
3748 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3749 int face_id2 = lookup_derived_face (it->f, face_name,
3750 'A', FRINGE_FACE_ID, 0);
3751 if (face_id2 >= 0)
3752 face_id = face_id2;
3755 /* Save current settings of IT so that we can restore them
3756 when we are finished with the glyph property value. */
3758 push_it (it);
3760 it->area = TEXT_AREA;
3761 it->what = IT_IMAGE;
3762 it->image_id = -1; /* no image */
3763 it->position = start_pos;
3764 it->object = NILP (object) ? it->w->buffer : object;
3765 it->method = GET_FROM_IMAGE;
3766 it->face_id = face_id;
3768 /* Say that we haven't consumed the characters with
3769 `display' property yet. The call to pop_it in
3770 set_iterator_to_next will clean this up. */
3771 *position = start_pos;
3773 if (EQ (XCAR (spec), Qleft_fringe))
3775 it->left_user_fringe_bitmap = fringe_bitmap;
3776 it->left_user_fringe_face_id = face_id;
3778 else
3780 it->right_user_fringe_bitmap = fringe_bitmap;
3781 it->right_user_fringe_face_id = face_id;
3783 #endif /* HAVE_WINDOW_SYSTEM */
3784 return 1;
3787 /* Prepare to handle `((margin left-margin) ...)',
3788 `((margin right-margin) ...)' and `((margin nil) ...)'
3789 prefixes for display specifications. */
3790 location = Qunbound;
3791 if (CONSP (spec) && CONSP (XCAR (spec)))
3793 Lisp_Object tem;
3795 value = XCDR (spec);
3796 if (CONSP (value))
3797 value = XCAR (value);
3799 tem = XCAR (spec);
3800 if (EQ (XCAR (tem), Qmargin)
3801 && (tem = XCDR (tem),
3802 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3803 (NILP (tem)
3804 || EQ (tem, Qleft_margin)
3805 || EQ (tem, Qright_margin))))
3806 location = tem;
3809 if (EQ (location, Qunbound))
3811 location = Qnil;
3812 value = spec;
3815 /* After this point, VALUE is the property after any
3816 margin prefix has been stripped. It must be a string,
3817 an image specification, or `(space ...)'.
3819 LOCATION specifies where to display: `left-margin',
3820 `right-margin' or nil. */
3822 valid_p = (STRINGP (value)
3823 #ifdef HAVE_WINDOW_SYSTEM
3824 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3825 #endif /* not HAVE_WINDOW_SYSTEM */
3826 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3828 if (valid_p && !display_replaced_before_p)
3830 /* Save current settings of IT so that we can restore them
3831 when we are finished with the glyph property value. */
3832 push_it (it);
3834 if (NILP (location))
3835 it->area = TEXT_AREA;
3836 else if (EQ (location, Qleft_margin))
3837 it->area = LEFT_MARGIN_AREA;
3838 else
3839 it->area = RIGHT_MARGIN_AREA;
3841 if (STRINGP (value))
3843 if (SCHARS (value) == 0)
3845 pop_it (it);
3846 return -1; /* Replaced by "", i.e. nothing. */
3848 it->string = value;
3849 it->multibyte_p = STRING_MULTIBYTE (it->string);
3850 it->current.overlay_string_index = -1;
3851 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3852 it->end_charpos = it->string_nchars = SCHARS (it->string);
3853 it->method = GET_FROM_STRING;
3854 it->stop_charpos = 0;
3855 it->string_from_display_prop_p = 1;
3856 /* Say that we haven't consumed the characters with
3857 `display' property yet. The call to pop_it in
3858 set_iterator_to_next will clean this up. */
3859 *position = start_pos;
3861 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3863 it->method = GET_FROM_STRETCH;
3864 it->object = value;
3865 it->current.pos = it->position = start_pos;
3867 #ifdef HAVE_WINDOW_SYSTEM
3868 else
3870 it->what = IT_IMAGE;
3871 it->image_id = lookup_image (it->f, value);
3872 it->position = start_pos;
3873 it->object = NILP (object) ? it->w->buffer : object;
3874 it->method = GET_FROM_IMAGE;
3876 /* Say that we haven't consumed the characters with
3877 `display' property yet. The call to pop_it in
3878 set_iterator_to_next will clean this up. */
3879 *position = start_pos;
3881 #endif /* HAVE_WINDOW_SYSTEM */
3883 return 1;
3886 /* Invalid property or property not supported. Restore
3887 POSITION to what it was before. */
3888 *position = start_pos;
3889 return 0;
3893 /* Check if SPEC is a display specification value whose text should be
3894 treated as intangible. */
3896 static int
3897 single_display_spec_intangible_p (prop)
3898 Lisp_Object prop;
3900 /* Skip over `when FORM'. */
3901 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3903 prop = XCDR (prop);
3904 if (!CONSP (prop))
3905 return 0;
3906 prop = XCDR (prop);
3909 if (STRINGP (prop))
3910 return 1;
3912 if (!CONSP (prop))
3913 return 0;
3915 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3916 we don't need to treat text as intangible. */
3917 if (EQ (XCAR (prop), Qmargin))
3919 prop = XCDR (prop);
3920 if (!CONSP (prop))
3921 return 0;
3923 prop = XCDR (prop);
3924 if (!CONSP (prop)
3925 || EQ (XCAR (prop), Qleft_margin)
3926 || EQ (XCAR (prop), Qright_margin))
3927 return 0;
3930 return (CONSP (prop)
3931 && (EQ (XCAR (prop), Qimage)
3932 || EQ (XCAR (prop), Qspace)));
3936 /* Check if PROP is a display property value whose text should be
3937 treated as intangible. */
3940 display_prop_intangible_p (prop)
3941 Lisp_Object prop;
3943 if (CONSP (prop)
3944 && CONSP (XCAR (prop))
3945 && !EQ (Qmargin, XCAR (XCAR (prop))))
3947 /* A list of sub-properties. */
3948 while (CONSP (prop))
3950 if (single_display_spec_intangible_p (XCAR (prop)))
3951 return 1;
3952 prop = XCDR (prop);
3955 else if (VECTORP (prop))
3957 /* A vector of sub-properties. */
3958 int i;
3959 for (i = 0; i < ASIZE (prop); ++i)
3960 if (single_display_spec_intangible_p (AREF (prop, i)))
3961 return 1;
3963 else
3964 return single_display_spec_intangible_p (prop);
3966 return 0;
3970 /* Return 1 if PROP is a display sub-property value containing STRING. */
3972 static int
3973 single_display_spec_string_p (prop, string)
3974 Lisp_Object prop, string;
3976 if (EQ (string, prop))
3977 return 1;
3979 /* Skip over `when FORM'. */
3980 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3982 prop = XCDR (prop);
3983 if (!CONSP (prop))
3984 return 0;
3985 prop = XCDR (prop);
3988 if (CONSP (prop))
3989 /* Skip over `margin LOCATION'. */
3990 if (EQ (XCAR (prop), Qmargin))
3992 prop = XCDR (prop);
3993 if (!CONSP (prop))
3994 return 0;
3996 prop = XCDR (prop);
3997 if (!CONSP (prop))
3998 return 0;
4001 return CONSP (prop) && EQ (XCAR (prop), string);
4005 /* Return 1 if STRING appears in the `display' property PROP. */
4007 static int
4008 display_prop_string_p (prop, string)
4009 Lisp_Object prop, string;
4011 if (CONSP (prop)
4012 && CONSP (XCAR (prop))
4013 && !EQ (Qmargin, XCAR (XCAR (prop))))
4015 /* A list of sub-properties. */
4016 while (CONSP (prop))
4018 if (single_display_spec_string_p (XCAR (prop), string))
4019 return 1;
4020 prop = XCDR (prop);
4023 else if (VECTORP (prop))
4025 /* A vector of sub-properties. */
4026 int i;
4027 for (i = 0; i < ASIZE (prop); ++i)
4028 if (single_display_spec_string_p (AREF (prop, i), string))
4029 return 1;
4031 else
4032 return single_display_spec_string_p (prop, string);
4034 return 0;
4038 /* Determine from which buffer position in W's buffer STRING comes
4039 from. AROUND_CHARPOS is an approximate position where it could
4040 be from. Value is the buffer position or 0 if it couldn't be
4041 determined.
4043 W's buffer must be current.
4045 This function is necessary because we don't record buffer positions
4046 in glyphs generated from strings (to keep struct glyph small).
4047 This function may only use code that doesn't eval because it is
4048 called asynchronously from note_mouse_highlight. */
4051 string_buffer_position (w, string, around_charpos)
4052 struct window *w;
4053 Lisp_Object string;
4054 int around_charpos;
4056 Lisp_Object limit, prop, pos;
4057 const int MAX_DISTANCE = 1000;
4058 int found = 0;
4060 pos = make_number (around_charpos);
4061 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4062 while (!found && !EQ (pos, limit))
4064 prop = Fget_char_property (pos, Qdisplay, Qnil);
4065 if (!NILP (prop) && display_prop_string_p (prop, string))
4066 found = 1;
4067 else
4068 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4071 if (!found)
4073 pos = make_number (around_charpos);
4074 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4075 while (!found && !EQ (pos, limit))
4077 prop = Fget_char_property (pos, Qdisplay, Qnil);
4078 if (!NILP (prop) && display_prop_string_p (prop, string))
4079 found = 1;
4080 else
4081 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4082 limit);
4086 return found ? XINT (pos) : 0;
4091 /***********************************************************************
4092 `composition' property
4093 ***********************************************************************/
4095 /* Set up iterator IT from `composition' property at its current
4096 position. Called from handle_stop. */
4098 static enum prop_handled
4099 handle_composition_prop (it)
4100 struct it *it;
4102 Lisp_Object prop, string;
4103 int pos, pos_byte, end;
4104 enum prop_handled handled = HANDLED_NORMALLY;
4106 if (STRINGP (it->string))
4108 pos = IT_STRING_CHARPOS (*it);
4109 pos_byte = IT_STRING_BYTEPOS (*it);
4110 string = it->string;
4112 else
4114 pos = IT_CHARPOS (*it);
4115 pos_byte = IT_BYTEPOS (*it);
4116 string = Qnil;
4119 /* If there's a valid composition and point is not inside of the
4120 composition (in the case that the composition is from the current
4121 buffer), draw a glyph composed from the composition components. */
4122 if (find_composition (pos, -1, &pos, &end, &prop, string)
4123 && COMPOSITION_VALID_P (pos, end, prop)
4124 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4126 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4128 if (id >= 0)
4130 it->method = GET_FROM_COMPOSITION;
4131 it->cmp_id = id;
4132 it->cmp_len = COMPOSITION_LENGTH (prop);
4133 /* For a terminal, draw only the first character of the
4134 components. */
4135 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4136 it->len = (STRINGP (it->string)
4137 ? string_char_to_byte (it->string, end)
4138 : CHAR_TO_BYTE (end)) - pos_byte;
4139 it->stop_charpos = end;
4140 handled = HANDLED_RETURN;
4144 return handled;
4149 /***********************************************************************
4150 Overlay strings
4151 ***********************************************************************/
4153 /* The following structure is used to record overlay strings for
4154 later sorting in load_overlay_strings. */
4156 struct overlay_entry
4158 Lisp_Object overlay;
4159 Lisp_Object string;
4160 int priority;
4161 int after_string_p;
4165 /* Set up iterator IT from overlay strings at its current position.
4166 Called from handle_stop. */
4168 static enum prop_handled
4169 handle_overlay_change (it)
4170 struct it *it;
4172 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4173 return HANDLED_RECOMPUTE_PROPS;
4174 else
4175 return HANDLED_NORMALLY;
4179 /* Set up the next overlay string for delivery by IT, if there is an
4180 overlay string to deliver. Called by set_iterator_to_next when the
4181 end of the current overlay string is reached. If there are more
4182 overlay strings to display, IT->string and
4183 IT->current.overlay_string_index are set appropriately here.
4184 Otherwise IT->string is set to nil. */
4186 static void
4187 next_overlay_string (it)
4188 struct it *it;
4190 ++it->current.overlay_string_index;
4191 if (it->current.overlay_string_index == it->n_overlay_strings)
4193 /* No more overlay strings. Restore IT's settings to what
4194 they were before overlay strings were processed, and
4195 continue to deliver from current_buffer. */
4196 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4198 pop_it (it);
4199 xassert (it->stop_charpos >= BEGV
4200 && it->stop_charpos <= it->end_charpos);
4201 it->string = Qnil;
4202 it->current.overlay_string_index = -1;
4203 SET_TEXT_POS (it->current.string_pos, -1, -1);
4204 it->n_overlay_strings = 0;
4205 it->method = GET_FROM_BUFFER;
4207 /* If we're at the end of the buffer, record that we have
4208 processed the overlay strings there already, so that
4209 next_element_from_buffer doesn't try it again. */
4210 if (IT_CHARPOS (*it) >= it->end_charpos)
4211 it->overlay_strings_at_end_processed_p = 1;
4213 /* If we have to display `...' for invisible text, set
4214 the iterator up for that. */
4215 if (display_ellipsis_p)
4216 setup_for_ellipsis (it, 0);
4218 else
4220 /* There are more overlay strings to process. If
4221 IT->current.overlay_string_index has advanced to a position
4222 where we must load IT->overlay_strings with more strings, do
4223 it. */
4224 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4226 if (it->current.overlay_string_index && i == 0)
4227 load_overlay_strings (it, 0);
4229 /* Initialize IT to deliver display elements from the overlay
4230 string. */
4231 it->string = it->overlay_strings[i];
4232 it->multibyte_p = STRING_MULTIBYTE (it->string);
4233 SET_TEXT_POS (it->current.string_pos, 0, 0);
4234 it->method = GET_FROM_STRING;
4235 it->stop_charpos = 0;
4238 CHECK_IT (it);
4242 /* Compare two overlay_entry structures E1 and E2. Used as a
4243 comparison function for qsort in load_overlay_strings. Overlay
4244 strings for the same position are sorted so that
4246 1. All after-strings come in front of before-strings, except
4247 when they come from the same overlay.
4249 2. Within after-strings, strings are sorted so that overlay strings
4250 from overlays with higher priorities come first.
4252 2. Within before-strings, strings are sorted so that overlay
4253 strings from overlays with higher priorities come last.
4255 Value is analogous to strcmp. */
4258 static int
4259 compare_overlay_entries (e1, e2)
4260 void *e1, *e2;
4262 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4263 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4264 int result;
4266 if (entry1->after_string_p != entry2->after_string_p)
4268 /* Let after-strings appear in front of before-strings if
4269 they come from different overlays. */
4270 if (EQ (entry1->overlay, entry2->overlay))
4271 result = entry1->after_string_p ? 1 : -1;
4272 else
4273 result = entry1->after_string_p ? -1 : 1;
4275 else if (entry1->after_string_p)
4276 /* After-strings sorted in order of decreasing priority. */
4277 result = entry2->priority - entry1->priority;
4278 else
4279 /* Before-strings sorted in order of increasing priority. */
4280 result = entry1->priority - entry2->priority;
4282 return result;
4286 /* Load the vector IT->overlay_strings with overlay strings from IT's
4287 current buffer position, or from CHARPOS if that is > 0. Set
4288 IT->n_overlays to the total number of overlay strings found.
4290 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4291 a time. On entry into load_overlay_strings,
4292 IT->current.overlay_string_index gives the number of overlay
4293 strings that have already been loaded by previous calls to this
4294 function.
4296 IT->add_overlay_start contains an additional overlay start
4297 position to consider for taking overlay strings from, if non-zero.
4298 This position comes into play when the overlay has an `invisible'
4299 property, and both before and after-strings. When we've skipped to
4300 the end of the overlay, because of its `invisible' property, we
4301 nevertheless want its before-string to appear.
4302 IT->add_overlay_start will contain the overlay start position
4303 in this case.
4305 Overlay strings are sorted so that after-string strings come in
4306 front of before-string strings. Within before and after-strings,
4307 strings are sorted by overlay priority. See also function
4308 compare_overlay_entries. */
4310 static void
4311 load_overlay_strings (it, charpos)
4312 struct it *it;
4313 int charpos;
4315 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4316 Lisp_Object overlay, window, str, invisible;
4317 struct Lisp_Overlay *ov;
4318 int start, end;
4319 int size = 20;
4320 int n = 0, i, j, invis_p;
4321 struct overlay_entry *entries
4322 = (struct overlay_entry *) alloca (size * sizeof *entries);
4324 if (charpos <= 0)
4325 charpos = IT_CHARPOS (*it);
4327 /* Append the overlay string STRING of overlay OVERLAY to vector
4328 `entries' which has size `size' and currently contains `n'
4329 elements. AFTER_P non-zero means STRING is an after-string of
4330 OVERLAY. */
4331 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4332 do \
4334 Lisp_Object priority; \
4336 if (n == size) \
4338 int new_size = 2 * size; \
4339 struct overlay_entry *old = entries; \
4340 entries = \
4341 (struct overlay_entry *) alloca (new_size \
4342 * sizeof *entries); \
4343 bcopy (old, entries, size * sizeof *entries); \
4344 size = new_size; \
4347 entries[n].string = (STRING); \
4348 entries[n].overlay = (OVERLAY); \
4349 priority = Foverlay_get ((OVERLAY), Qpriority); \
4350 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4351 entries[n].after_string_p = (AFTER_P); \
4352 ++n; \
4354 while (0)
4356 /* Process overlay before the overlay center. */
4357 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4359 XSETMISC (overlay, ov);
4360 xassert (OVERLAYP (overlay));
4361 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4362 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4364 if (end < charpos)
4365 break;
4367 /* Skip this overlay if it doesn't start or end at IT's current
4368 position. */
4369 if (end != charpos && start != charpos)
4370 continue;
4372 /* Skip this overlay if it doesn't apply to IT->w. */
4373 window = Foverlay_get (overlay, Qwindow);
4374 if (WINDOWP (window) && XWINDOW (window) != it->w)
4375 continue;
4377 /* If the text ``under'' the overlay is invisible, both before-
4378 and after-strings from this overlay are visible; start and
4379 end position are indistinguishable. */
4380 invisible = Foverlay_get (overlay, Qinvisible);
4381 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4383 /* If overlay has a non-empty before-string, record it. */
4384 if ((start == charpos || (end == charpos && invis_p))
4385 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4386 && SCHARS (str))
4387 RECORD_OVERLAY_STRING (overlay, str, 0);
4389 /* If overlay has a non-empty after-string, record it. */
4390 if ((end == charpos || (start == charpos && invis_p))
4391 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4392 && SCHARS (str))
4393 RECORD_OVERLAY_STRING (overlay, str, 1);
4396 /* Process overlays after the overlay center. */
4397 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4399 XSETMISC (overlay, ov);
4400 xassert (OVERLAYP (overlay));
4401 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4402 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4404 if (start > charpos)
4405 break;
4407 /* Skip this overlay if it doesn't start or end at IT's current
4408 position. */
4409 if (end != charpos && start != charpos)
4410 continue;
4412 /* Skip this overlay if it doesn't apply to IT->w. */
4413 window = Foverlay_get (overlay, Qwindow);
4414 if (WINDOWP (window) && XWINDOW (window) != it->w)
4415 continue;
4417 /* If the text ``under'' the overlay is invisible, it has a zero
4418 dimension, and both before- and after-strings apply. */
4419 invisible = Foverlay_get (overlay, Qinvisible);
4420 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4422 /* If overlay has a non-empty before-string, record it. */
4423 if ((start == charpos || (end == charpos && invis_p))
4424 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4425 && SCHARS (str))
4426 RECORD_OVERLAY_STRING (overlay, str, 0);
4428 /* If overlay has a non-empty after-string, record it. */
4429 if ((end == charpos || (start == charpos && invis_p))
4430 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4431 && SCHARS (str))
4432 RECORD_OVERLAY_STRING (overlay, str, 1);
4435 #undef RECORD_OVERLAY_STRING
4437 /* Sort entries. */
4438 if (n > 1)
4439 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4441 /* Record the total number of strings to process. */
4442 it->n_overlay_strings = n;
4444 /* IT->current.overlay_string_index is the number of overlay strings
4445 that have already been consumed by IT. Copy some of the
4446 remaining overlay strings to IT->overlay_strings. */
4447 i = 0;
4448 j = it->current.overlay_string_index;
4449 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4450 it->overlay_strings[i++] = entries[j++].string;
4452 CHECK_IT (it);
4456 /* Get the first chunk of overlay strings at IT's current buffer
4457 position, or at CHARPOS if that is > 0. Value is non-zero if at
4458 least one overlay string was found. */
4460 static int
4461 get_overlay_strings (it, charpos)
4462 struct it *it;
4463 int charpos;
4465 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4466 process. This fills IT->overlay_strings with strings, and sets
4467 IT->n_overlay_strings to the total number of strings to process.
4468 IT->pos.overlay_string_index has to be set temporarily to zero
4469 because load_overlay_strings needs this; it must be set to -1
4470 when no overlay strings are found because a zero value would
4471 indicate a position in the first overlay string. */
4472 it->current.overlay_string_index = 0;
4473 load_overlay_strings (it, charpos);
4475 /* If we found overlay strings, set up IT to deliver display
4476 elements from the first one. Otherwise set up IT to deliver
4477 from current_buffer. */
4478 if (it->n_overlay_strings)
4480 /* Make sure we know settings in current_buffer, so that we can
4481 restore meaningful values when we're done with the overlay
4482 strings. */
4483 compute_stop_pos (it);
4484 xassert (it->face_id >= 0);
4486 /* Save IT's settings. They are restored after all overlay
4487 strings have been processed. */
4488 xassert (it->sp == 0);
4489 push_it (it);
4491 /* Set up IT to deliver display elements from the first overlay
4492 string. */
4493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4494 it->string = it->overlay_strings[0];
4495 it->stop_charpos = 0;
4496 xassert (STRINGP (it->string));
4497 it->end_charpos = SCHARS (it->string);
4498 it->multibyte_p = STRING_MULTIBYTE (it->string);
4499 it->method = GET_FROM_STRING;
4501 else
4503 it->string = Qnil;
4504 it->current.overlay_string_index = -1;
4505 it->method = GET_FROM_BUFFER;
4508 CHECK_IT (it);
4510 /* Value is non-zero if we found at least one overlay string. */
4511 return STRINGP (it->string);
4516 /***********************************************************************
4517 Saving and restoring state
4518 ***********************************************************************/
4520 /* Save current settings of IT on IT->stack. Called, for example,
4521 before setting up IT for an overlay string, to be able to restore
4522 IT's settings to what they were after the overlay string has been
4523 processed. */
4525 static void
4526 push_it (it)
4527 struct it *it;
4529 struct iterator_stack_entry *p;
4531 xassert (it->sp < 2);
4532 p = it->stack + it->sp;
4534 p->stop_charpos = it->stop_charpos;
4535 xassert (it->face_id >= 0);
4536 p->face_id = it->face_id;
4537 p->string = it->string;
4538 p->pos = it->current;
4539 p->end_charpos = it->end_charpos;
4540 p->string_nchars = it->string_nchars;
4541 p->area = it->area;
4542 p->multibyte_p = it->multibyte_p;
4543 p->slice = it->slice;
4544 p->space_width = it->space_width;
4545 p->font_height = it->font_height;
4546 p->voffset = it->voffset;
4547 p->string_from_display_prop_p = it->string_from_display_prop_p;
4548 p->display_ellipsis_p = 0;
4549 ++it->sp;
4553 /* Restore IT's settings from IT->stack. Called, for example, when no
4554 more overlay strings must be processed, and we return to delivering
4555 display elements from a buffer, or when the end of a string from a
4556 `display' property is reached and we return to delivering display
4557 elements from an overlay string, or from a buffer. */
4559 static void
4560 pop_it (it)
4561 struct it *it;
4563 struct iterator_stack_entry *p;
4565 xassert (it->sp > 0);
4566 --it->sp;
4567 p = it->stack + it->sp;
4568 it->stop_charpos = p->stop_charpos;
4569 it->face_id = p->face_id;
4570 it->string = p->string;
4571 it->current = p->pos;
4572 it->end_charpos = p->end_charpos;
4573 it->string_nchars = p->string_nchars;
4574 it->area = p->area;
4575 it->multibyte_p = p->multibyte_p;
4576 it->slice = p->slice;
4577 it->space_width = p->space_width;
4578 it->font_height = p->font_height;
4579 it->voffset = p->voffset;
4580 it->string_from_display_prop_p = p->string_from_display_prop_p;
4585 /***********************************************************************
4586 Moving over lines
4587 ***********************************************************************/
4589 /* Set IT's current position to the previous line start. */
4591 static void
4592 back_to_previous_line_start (it)
4593 struct it *it;
4595 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4596 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4600 /* Move IT to the next line start.
4602 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4603 we skipped over part of the text (as opposed to moving the iterator
4604 continuously over the text). Otherwise, don't change the value
4605 of *SKIPPED_P.
4607 Newlines may come from buffer text, overlay strings, or strings
4608 displayed via the `display' property. That's the reason we can't
4609 simply use find_next_newline_no_quit.
4611 Note that this function may not skip over invisible text that is so
4612 because of text properties and immediately follows a newline. If
4613 it would, function reseat_at_next_visible_line_start, when called
4614 from set_iterator_to_next, would effectively make invisible
4615 characters following a newline part of the wrong glyph row, which
4616 leads to wrong cursor motion. */
4618 static int
4619 forward_to_next_line_start (it, skipped_p)
4620 struct it *it;
4621 int *skipped_p;
4623 int old_selective, newline_found_p, n;
4624 const int MAX_NEWLINE_DISTANCE = 500;
4626 /* If already on a newline, just consume it to avoid unintended
4627 skipping over invisible text below. */
4628 if (it->what == IT_CHARACTER
4629 && it->c == '\n'
4630 && CHARPOS (it->position) == IT_CHARPOS (*it))
4632 set_iterator_to_next (it, 0);
4633 it->c = 0;
4634 return 1;
4637 /* Don't handle selective display in the following. It's (a)
4638 unnecessary because it's done by the caller, and (b) leads to an
4639 infinite recursion because next_element_from_ellipsis indirectly
4640 calls this function. */
4641 old_selective = it->selective;
4642 it->selective = 0;
4644 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4645 from buffer text. */
4646 for (n = newline_found_p = 0;
4647 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4648 n += STRINGP (it->string) ? 0 : 1)
4650 if (!get_next_display_element (it))
4651 return 0;
4652 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4653 set_iterator_to_next (it, 0);
4656 /* If we didn't find a newline near enough, see if we can use a
4657 short-cut. */
4658 if (!newline_found_p)
4660 int start = IT_CHARPOS (*it);
4661 int limit = find_next_newline_no_quit (start, 1);
4662 Lisp_Object pos;
4664 xassert (!STRINGP (it->string));
4666 /* If there isn't any `display' property in sight, and no
4667 overlays, we can just use the position of the newline in
4668 buffer text. */
4669 if (it->stop_charpos >= limit
4670 || ((pos = Fnext_single_property_change (make_number (start),
4671 Qdisplay,
4672 Qnil, make_number (limit)),
4673 NILP (pos))
4674 && next_overlay_change (start) == ZV))
4676 IT_CHARPOS (*it) = limit;
4677 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4678 *skipped_p = newline_found_p = 1;
4680 else
4682 while (get_next_display_element (it)
4683 && !newline_found_p)
4685 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4686 set_iterator_to_next (it, 0);
4691 it->selective = old_selective;
4692 return newline_found_p;
4696 /* Set IT's current position to the previous visible line start. Skip
4697 invisible text that is so either due to text properties or due to
4698 selective display. Caution: this does not change IT->current_x and
4699 IT->hpos. */
4701 static void
4702 back_to_previous_visible_line_start (it)
4703 struct it *it;
4705 while (IT_CHARPOS (*it) > BEGV)
4707 back_to_previous_line_start (it);
4708 if (IT_CHARPOS (*it) <= BEGV)
4709 break;
4711 /* If selective > 0, then lines indented more than that values
4712 are invisible. */
4713 if (it->selective > 0
4714 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4715 (double) it->selective)) /* iftc */
4716 continue;
4718 /* Check the newline before point for invisibility. */
4720 Lisp_Object prop;
4721 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4722 Qinvisible, it->window);
4723 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4724 continue;
4727 /* If newline has a display property that replaces the newline with something
4728 else (image or text), find start of overlay or interval and continue search
4729 from that point. */
4730 if (IT_CHARPOS (*it) > BEGV)
4732 struct it it2 = *it;
4733 int pos;
4734 int beg, end;
4735 Lisp_Object val, overlay;
4737 pos = --IT_CHARPOS (it2);
4738 --IT_BYTEPOS (it2);
4739 it2.sp = 0;
4740 if (handle_display_prop (&it2) == HANDLED_RETURN
4741 && !NILP (val = get_char_property_and_overlay
4742 (make_number (pos), Qdisplay, Qnil, &overlay))
4743 && (OVERLAYP (overlay)
4744 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4745 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4747 if (beg < BEGV)
4748 beg = BEGV;
4749 IT_CHARPOS (*it) = beg;
4750 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4751 continue;
4755 break;
4758 xassert (IT_CHARPOS (*it) >= BEGV);
4759 xassert (IT_CHARPOS (*it) == BEGV
4760 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4761 CHECK_IT (it);
4765 /* Reseat iterator IT at the previous visible line start. Skip
4766 invisible text that is so either due to text properties or due to
4767 selective display. At the end, update IT's overlay information,
4768 face information etc. */
4770 void
4771 reseat_at_previous_visible_line_start (it)
4772 struct it *it;
4774 back_to_previous_visible_line_start (it);
4775 reseat (it, it->current.pos, 1);
4776 CHECK_IT (it);
4780 /* Reseat iterator IT on the next visible line start in the current
4781 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4782 preceding the line start. Skip over invisible text that is so
4783 because of selective display. Compute faces, overlays etc at the
4784 new position. Note that this function does not skip over text that
4785 is invisible because of text properties. */
4787 static void
4788 reseat_at_next_visible_line_start (it, on_newline_p)
4789 struct it *it;
4790 int on_newline_p;
4792 int newline_found_p, skipped_p = 0;
4794 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4796 /* Skip over lines that are invisible because they are indented
4797 more than the value of IT->selective. */
4798 if (it->selective > 0)
4799 while (IT_CHARPOS (*it) < ZV
4800 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4801 (double) it->selective)) /* iftc */
4803 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4804 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4807 /* Position on the newline if that's what's requested. */
4808 if (on_newline_p && newline_found_p)
4810 if (STRINGP (it->string))
4812 if (IT_STRING_CHARPOS (*it) > 0)
4814 --IT_STRING_CHARPOS (*it);
4815 --IT_STRING_BYTEPOS (*it);
4818 else if (IT_CHARPOS (*it) > BEGV)
4820 --IT_CHARPOS (*it);
4821 --IT_BYTEPOS (*it);
4822 reseat (it, it->current.pos, 0);
4825 else if (skipped_p)
4826 reseat (it, it->current.pos, 0);
4828 CHECK_IT (it);
4833 /***********************************************************************
4834 Changing an iterator's position
4835 ***********************************************************************/
4837 /* Change IT's current position to POS in current_buffer. If FORCE_P
4838 is non-zero, always check for text properties at the new position.
4839 Otherwise, text properties are only looked up if POS >=
4840 IT->check_charpos of a property. */
4842 static void
4843 reseat (it, pos, force_p)
4844 struct it *it;
4845 struct text_pos pos;
4846 int force_p;
4848 int original_pos = IT_CHARPOS (*it);
4850 reseat_1 (it, pos, 0);
4852 /* Determine where to check text properties. Avoid doing it
4853 where possible because text property lookup is very expensive. */
4854 if (force_p
4855 || CHARPOS (pos) > it->stop_charpos
4856 || CHARPOS (pos) < original_pos)
4857 handle_stop (it);
4859 CHECK_IT (it);
4863 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4864 IT->stop_pos to POS, also. */
4866 static void
4867 reseat_1 (it, pos, set_stop_p)
4868 struct it *it;
4869 struct text_pos pos;
4870 int set_stop_p;
4872 /* Don't call this function when scanning a C string. */
4873 xassert (it->s == NULL);
4875 /* POS must be a reasonable value. */
4876 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4878 it->current.pos = it->position = pos;
4879 XSETBUFFER (it->object, current_buffer);
4880 it->end_charpos = ZV;
4881 it->dpvec = NULL;
4882 it->current.dpvec_index = -1;
4883 it->current.overlay_string_index = -1;
4884 IT_STRING_CHARPOS (*it) = -1;
4885 IT_STRING_BYTEPOS (*it) = -1;
4886 it->string = Qnil;
4887 it->method = GET_FROM_BUFFER;
4888 /* RMS: I added this to fix a bug in move_it_vertically_backward
4889 where it->area continued to relate to the starting point
4890 for the backward motion. Bug report from
4891 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4892 However, I am not sure whether reseat still does the right thing
4893 in general after this change. */
4894 it->area = TEXT_AREA;
4895 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4896 it->sp = 0;
4897 it->face_before_selective_p = 0;
4899 if (set_stop_p)
4900 it->stop_charpos = CHARPOS (pos);
4904 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4905 If S is non-null, it is a C string to iterate over. Otherwise,
4906 STRING gives a Lisp string to iterate over.
4908 If PRECISION > 0, don't return more then PRECISION number of
4909 characters from the string.
4911 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4912 characters have been returned. FIELD_WIDTH < 0 means an infinite
4913 field width.
4915 MULTIBYTE = 0 means disable processing of multibyte characters,
4916 MULTIBYTE > 0 means enable it,
4917 MULTIBYTE < 0 means use IT->multibyte_p.
4919 IT must be initialized via a prior call to init_iterator before
4920 calling this function. */
4922 static void
4923 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4924 struct it *it;
4925 unsigned char *s;
4926 Lisp_Object string;
4927 int charpos;
4928 int precision, field_width, multibyte;
4930 /* No region in strings. */
4931 it->region_beg_charpos = it->region_end_charpos = -1;
4933 /* No text property checks performed by default, but see below. */
4934 it->stop_charpos = -1;
4936 /* Set iterator position and end position. */
4937 bzero (&it->current, sizeof it->current);
4938 it->current.overlay_string_index = -1;
4939 it->current.dpvec_index = -1;
4940 xassert (charpos >= 0);
4942 /* If STRING is specified, use its multibyteness, otherwise use the
4943 setting of MULTIBYTE, if specified. */
4944 if (multibyte >= 0)
4945 it->multibyte_p = multibyte > 0;
4947 if (s == NULL)
4949 xassert (STRINGP (string));
4950 it->string = string;
4951 it->s = NULL;
4952 it->end_charpos = it->string_nchars = SCHARS (string);
4953 it->method = GET_FROM_STRING;
4954 it->current.string_pos = string_pos (charpos, string);
4956 else
4958 it->s = s;
4959 it->string = Qnil;
4961 /* Note that we use IT->current.pos, not it->current.string_pos,
4962 for displaying C strings. */
4963 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4964 if (it->multibyte_p)
4966 it->current.pos = c_string_pos (charpos, s, 1);
4967 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4969 else
4971 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4972 it->end_charpos = it->string_nchars = strlen (s);
4975 it->method = GET_FROM_C_STRING;
4978 /* PRECISION > 0 means don't return more than PRECISION characters
4979 from the string. */
4980 if (precision > 0 && it->end_charpos - charpos > precision)
4981 it->end_charpos = it->string_nchars = charpos + precision;
4983 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4984 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4985 FIELD_WIDTH < 0 means infinite field width. This is useful for
4986 padding with `-' at the end of a mode line. */
4987 if (field_width < 0)
4988 field_width = INFINITY;
4989 if (field_width > it->end_charpos - charpos)
4990 it->end_charpos = charpos + field_width;
4992 /* Use the standard display table for displaying strings. */
4993 if (DISP_TABLE_P (Vstandard_display_table))
4994 it->dp = XCHAR_TABLE (Vstandard_display_table);
4996 it->stop_charpos = charpos;
4997 CHECK_IT (it);
5002 /***********************************************************************
5003 Iteration
5004 ***********************************************************************/
5006 /* Map enum it_method value to corresponding next_element_from_* function. */
5008 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5010 next_element_from_buffer,
5011 next_element_from_display_vector,
5012 next_element_from_composition,
5013 next_element_from_string,
5014 next_element_from_c_string,
5015 next_element_from_image,
5016 next_element_from_stretch
5020 /* Load IT's display element fields with information about the next
5021 display element from the current position of IT. Value is zero if
5022 end of buffer (or C string) is reached. */
5025 get_next_display_element (it)
5026 struct it *it;
5028 /* Non-zero means that we found a display element. Zero means that
5029 we hit the end of what we iterate over. Performance note: the
5030 function pointer `method' used here turns out to be faster than
5031 using a sequence of if-statements. */
5032 int success_p;
5034 get_next:
5035 success_p = (*get_next_element[it->method]) (it);
5037 if (it->what == IT_CHARACTER)
5039 /* Map via display table or translate control characters.
5040 IT->c, IT->len etc. have been set to the next character by
5041 the function call above. If we have a display table, and it
5042 contains an entry for IT->c, translate it. Don't do this if
5043 IT->c itself comes from a display table, otherwise we could
5044 end up in an infinite recursion. (An alternative could be to
5045 count the recursion depth of this function and signal an
5046 error when a certain maximum depth is reached.) Is it worth
5047 it? */
5048 if (success_p && it->dpvec == NULL)
5050 Lisp_Object dv;
5052 if (it->dp
5053 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5054 VECTORP (dv)))
5056 struct Lisp_Vector *v = XVECTOR (dv);
5058 /* Return the first character from the display table
5059 entry, if not empty. If empty, don't display the
5060 current character. */
5061 if (v->size)
5063 it->dpvec_char_len = it->len;
5064 it->dpvec = v->contents;
5065 it->dpend = v->contents + v->size;
5066 it->current.dpvec_index = 0;
5067 it->dpvec_face_id = -1;
5068 it->saved_face_id = it->face_id;
5069 it->method = GET_FROM_DISPLAY_VECTOR;
5070 it->ellipsis_p = 0;
5072 else
5074 set_iterator_to_next (it, 0);
5076 goto get_next;
5079 /* Translate control characters into `\003' or `^C' form.
5080 Control characters coming from a display table entry are
5081 currently not translated because we use IT->dpvec to hold
5082 the translation. This could easily be changed but I
5083 don't believe that it is worth doing.
5085 If it->multibyte_p is nonzero, eight-bit characters and
5086 non-printable multibyte characters are also translated to
5087 octal form.
5089 If it->multibyte_p is zero, eight-bit characters that
5090 don't have corresponding multibyte char code are also
5091 translated to octal form. */
5092 else if ((it->c < ' '
5093 && (it->area != TEXT_AREA
5094 /* In mode line, treat \n like other crl chars. */
5095 || (it->c != '\t'
5096 && it->glyph_row && it->glyph_row->mode_line_p)
5097 || (it->c != '\n' && it->c != '\t')))
5098 || (it->multibyte_p
5099 ? ((it->c >= 127
5100 && it->len == 1)
5101 || !CHAR_PRINTABLE_P (it->c)
5102 || (!NILP (Vnobreak_char_display)
5103 && (it->c == 0x8a0 || it->c == 0x8ad
5104 || it->c == 0x920 || it->c == 0x92d
5105 || it->c == 0xe20 || it->c == 0xe2d
5106 || it->c == 0xf20 || it->c == 0xf2d)))
5107 : (it->c >= 127
5108 && (!unibyte_display_via_language_environment
5109 || it->c == unibyte_char_to_multibyte (it->c)))))
5111 /* IT->c is a control character which must be displayed
5112 either as '\003' or as `^C' where the '\\' and '^'
5113 can be defined in the display table. Fill
5114 IT->ctl_chars with glyphs for what we have to
5115 display. Then, set IT->dpvec to these glyphs. */
5116 GLYPH g;
5117 int ctl_len;
5118 int face_id, lface_id = 0 ;
5119 GLYPH escape_glyph;
5121 /* Handle control characters with ^. */
5123 if (it->c < 128 && it->ctl_arrow_p)
5125 g = '^'; /* default glyph for Control */
5126 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5127 if (it->dp
5128 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5129 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5131 g = XINT (DISP_CTRL_GLYPH (it->dp));
5132 lface_id = FAST_GLYPH_FACE (g);
5134 if (lface_id)
5136 g = FAST_GLYPH_CHAR (g);
5137 face_id = merge_faces (it->f, Qt, lface_id,
5138 it->face_id);
5140 else
5142 /* Merge the escape-glyph face into the current face. */
5143 face_id = merge_faces (it->f, Qescape_glyph, 0,
5144 it->face_id);
5147 XSETINT (it->ctl_chars[0], g);
5148 g = it->c ^ 0100;
5149 XSETINT (it->ctl_chars[1], g);
5150 ctl_len = 2;
5151 goto display_control;
5154 /* Handle non-break space in the mode where it only gets
5155 highlighting. */
5157 if (EQ (Vnobreak_char_display, Qt)
5158 && (it->c == 0x8a0 || it->c == 0x920
5159 || it->c == 0xe20 || it->c == 0xf20))
5161 /* Merge the no-break-space face into the current face. */
5162 face_id = merge_faces (it->f, Qnobreak_space, 0,
5163 it->face_id);
5165 g = it->c = ' ';
5166 XSETINT (it->ctl_chars[0], g);
5167 ctl_len = 1;
5168 goto display_control;
5171 /* Handle sequences that start with the "escape glyph". */
5173 /* the default escape glyph is \. */
5174 escape_glyph = '\\';
5176 if (it->dp
5177 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5178 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5180 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5181 lface_id = FAST_GLYPH_FACE (escape_glyph);
5183 if (lface_id)
5185 /* The display table specified a face.
5186 Merge it into face_id and also into escape_glyph. */
5187 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5188 face_id = merge_faces (it->f, Qt, lface_id,
5189 it->face_id);
5191 else
5193 /* Merge the escape-glyph face into the current face. */
5194 face_id = merge_faces (it->f, Qescape_glyph, 0,
5195 it->face_id);
5198 /* Handle soft hyphens in the mode where they only get
5199 highlighting. */
5201 if (EQ (Vnobreak_char_display, Qt)
5202 && (it->c == 0x8ad || it->c == 0x92d
5203 || it->c == 0xe2d || it->c == 0xf2d))
5205 g = it->c = '-';
5206 XSETINT (it->ctl_chars[0], g);
5207 ctl_len = 1;
5208 goto display_control;
5211 /* Handle non-break space and soft hyphen
5212 with the escape glyph. */
5214 if (it->c == 0x8a0 || it->c == 0x8ad
5215 || it->c == 0x920 || it->c == 0x92d
5216 || it->c == 0xe20 || it->c == 0xe2d
5217 || it->c == 0xf20 || it->c == 0xf2d)
5219 XSETINT (it->ctl_chars[0], escape_glyph);
5220 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5221 XSETINT (it->ctl_chars[1], g);
5222 ctl_len = 2;
5223 goto display_control;
5227 unsigned char str[MAX_MULTIBYTE_LENGTH];
5228 int len;
5229 int i;
5231 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5232 if (SINGLE_BYTE_CHAR_P (it->c))
5233 str[0] = it->c, len = 1;
5234 else
5236 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5237 if (len < 0)
5239 /* It's an invalid character, which shouldn't
5240 happen actually, but due to bugs it may
5241 happen. Let's print the char as is, there's
5242 not much meaningful we can do with it. */
5243 str[0] = it->c;
5244 str[1] = it->c >> 8;
5245 str[2] = it->c >> 16;
5246 str[3] = it->c >> 24;
5247 len = 4;
5251 for (i = 0; i < len; i++)
5253 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5254 /* Insert three more glyphs into IT->ctl_chars for
5255 the octal display of the character. */
5256 g = ((str[i] >> 6) & 7) + '0';
5257 XSETINT (it->ctl_chars[i * 4 + 1], g);
5258 g = ((str[i] >> 3) & 7) + '0';
5259 XSETINT (it->ctl_chars[i * 4 + 2], g);
5260 g = (str[i] & 7) + '0';
5261 XSETINT (it->ctl_chars[i * 4 + 3], g);
5263 ctl_len = len * 4;
5266 display_control:
5267 /* Set up IT->dpvec and return first character from it. */
5268 it->dpvec_char_len = it->len;
5269 it->dpvec = it->ctl_chars;
5270 it->dpend = it->dpvec + ctl_len;
5271 it->current.dpvec_index = 0;
5272 it->dpvec_face_id = face_id;
5273 it->saved_face_id = it->face_id;
5274 it->method = GET_FROM_DISPLAY_VECTOR;
5275 it->ellipsis_p = 0;
5276 goto get_next;
5280 /* Adjust face id for a multibyte character. There are no
5281 multibyte character in unibyte text. */
5282 if (it->multibyte_p
5283 && success_p
5284 && FRAME_WINDOW_P (it->f))
5286 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5287 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5291 /* Is this character the last one of a run of characters with
5292 box? If yes, set IT->end_of_box_run_p to 1. */
5293 if (it->face_box_p
5294 && it->s == NULL)
5296 int face_id;
5297 struct face *face;
5299 it->end_of_box_run_p
5300 = ((face_id = face_after_it_pos (it),
5301 face_id != it->face_id)
5302 && (face = FACE_FROM_ID (it->f, face_id),
5303 face->box == FACE_NO_BOX));
5306 /* Value is 0 if end of buffer or string reached. */
5307 return success_p;
5311 /* Move IT to the next display element.
5313 RESEAT_P non-zero means if called on a newline in buffer text,
5314 skip to the next visible line start.
5316 Functions get_next_display_element and set_iterator_to_next are
5317 separate because I find this arrangement easier to handle than a
5318 get_next_display_element function that also increments IT's
5319 position. The way it is we can first look at an iterator's current
5320 display element, decide whether it fits on a line, and if it does,
5321 increment the iterator position. The other way around we probably
5322 would either need a flag indicating whether the iterator has to be
5323 incremented the next time, or we would have to implement a
5324 decrement position function which would not be easy to write. */
5326 void
5327 set_iterator_to_next (it, reseat_p)
5328 struct it *it;
5329 int reseat_p;
5331 /* Reset flags indicating start and end of a sequence of characters
5332 with box. Reset them at the start of this function because
5333 moving the iterator to a new position might set them. */
5334 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5336 switch (it->method)
5338 case GET_FROM_BUFFER:
5339 /* The current display element of IT is a character from
5340 current_buffer. Advance in the buffer, and maybe skip over
5341 invisible lines that are so because of selective display. */
5342 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5343 reseat_at_next_visible_line_start (it, 0);
5344 else
5346 xassert (it->len != 0);
5347 IT_BYTEPOS (*it) += it->len;
5348 IT_CHARPOS (*it) += 1;
5349 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5351 break;
5353 case GET_FROM_COMPOSITION:
5354 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5355 if (STRINGP (it->string))
5357 IT_STRING_BYTEPOS (*it) += it->len;
5358 IT_STRING_CHARPOS (*it) += it->cmp_len;
5359 it->method = GET_FROM_STRING;
5360 goto consider_string_end;
5362 else
5364 IT_BYTEPOS (*it) += it->len;
5365 IT_CHARPOS (*it) += it->cmp_len;
5366 it->method = GET_FROM_BUFFER;
5368 break;
5370 case GET_FROM_C_STRING:
5371 /* Current display element of IT is from a C string. */
5372 IT_BYTEPOS (*it) += it->len;
5373 IT_CHARPOS (*it) += 1;
5374 break;
5376 case GET_FROM_DISPLAY_VECTOR:
5377 /* Current display element of IT is from a display table entry.
5378 Advance in the display table definition. Reset it to null if
5379 end reached, and continue with characters from buffers/
5380 strings. */
5381 ++it->current.dpvec_index;
5383 /* Restore face of the iterator to what they were before the
5384 display vector entry (these entries may contain faces). */
5385 it->face_id = it->saved_face_id;
5387 if (it->dpvec + it->current.dpvec_index == it->dpend)
5389 if (it->s)
5390 it->method = GET_FROM_C_STRING;
5391 else if (STRINGP (it->string))
5392 it->method = GET_FROM_STRING;
5393 else
5394 it->method = GET_FROM_BUFFER;
5396 it->dpvec = NULL;
5397 it->current.dpvec_index = -1;
5399 /* Skip over characters which were displayed via IT->dpvec. */
5400 if (it->dpvec_char_len < 0)
5401 reseat_at_next_visible_line_start (it, 1);
5402 else if (it->dpvec_char_len > 0)
5404 it->len = it->dpvec_char_len;
5405 set_iterator_to_next (it, reseat_p);
5408 /* Recheck faces after display vector */
5409 it->stop_charpos = IT_CHARPOS (*it);
5411 break;
5413 case GET_FROM_STRING:
5414 /* Current display element is a character from a Lisp string. */
5415 xassert (it->s == NULL && STRINGP (it->string));
5416 IT_STRING_BYTEPOS (*it) += it->len;
5417 IT_STRING_CHARPOS (*it) += 1;
5419 consider_string_end:
5421 if (it->current.overlay_string_index >= 0)
5423 /* IT->string is an overlay string. Advance to the
5424 next, if there is one. */
5425 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5426 next_overlay_string (it);
5428 else
5430 /* IT->string is not an overlay string. If we reached
5431 its end, and there is something on IT->stack, proceed
5432 with what is on the stack. This can be either another
5433 string, this time an overlay string, or a buffer. */
5434 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5435 && it->sp > 0)
5437 pop_it (it);
5438 if (STRINGP (it->string))
5439 goto consider_string_end;
5440 it->method = GET_FROM_BUFFER;
5443 break;
5445 case GET_FROM_IMAGE:
5446 case GET_FROM_STRETCH:
5447 /* The position etc with which we have to proceed are on
5448 the stack. The position may be at the end of a string,
5449 if the `display' property takes up the whole string. */
5450 xassert (it->sp > 0);
5451 pop_it (it);
5452 it->image_id = 0;
5453 if (STRINGP (it->string))
5455 it->method = GET_FROM_STRING;
5456 goto consider_string_end;
5458 it->method = GET_FROM_BUFFER;
5459 break;
5461 default:
5462 /* There are no other methods defined, so this should be a bug. */
5463 abort ();
5466 xassert (it->method != GET_FROM_STRING
5467 || (STRINGP (it->string)
5468 && IT_STRING_CHARPOS (*it) >= 0));
5471 /* Load IT's display element fields with information about the next
5472 display element which comes from a display table entry or from the
5473 result of translating a control character to one of the forms `^C'
5474 or `\003'.
5476 IT->dpvec holds the glyphs to return as characters.
5477 IT->saved_face_id holds the face id before the display vector--
5478 it is restored into IT->face_idin set_iterator_to_next. */
5480 static int
5481 next_element_from_display_vector (it)
5482 struct it *it;
5484 /* Precondition. */
5485 xassert (it->dpvec && it->current.dpvec_index >= 0);
5487 it->face_id = it->saved_face_id;
5489 if (INTEGERP (*it->dpvec)
5490 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5492 GLYPH g;
5494 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5495 it->c = FAST_GLYPH_CHAR (g);
5496 it->len = CHAR_BYTES (it->c);
5498 /* The entry may contain a face id to use. Such a face id is
5499 the id of a Lisp face, not a realized face. A face id of
5500 zero means no face is specified. */
5501 if (it->dpvec_face_id >= 0)
5502 it->face_id = it->dpvec_face_id;
5503 else
5505 int lface_id = FAST_GLYPH_FACE (g);
5506 if (lface_id > 0)
5507 it->face_id = merge_faces (it->f, Qt, lface_id,
5508 it->saved_face_id);
5511 else
5512 /* Display table entry is invalid. Return a space. */
5513 it->c = ' ', it->len = 1;
5515 /* Don't change position and object of the iterator here. They are
5516 still the values of the character that had this display table
5517 entry or was translated, and that's what we want. */
5518 it->what = IT_CHARACTER;
5519 return 1;
5523 /* Load IT with the next display element from Lisp string IT->string.
5524 IT->current.string_pos is the current position within the string.
5525 If IT->current.overlay_string_index >= 0, the Lisp string is an
5526 overlay string. */
5528 static int
5529 next_element_from_string (it)
5530 struct it *it;
5532 struct text_pos position;
5534 xassert (STRINGP (it->string));
5535 xassert (IT_STRING_CHARPOS (*it) >= 0);
5536 position = it->current.string_pos;
5538 /* Time to check for invisible text? */
5539 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5540 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5542 handle_stop (it);
5544 /* Since a handler may have changed IT->method, we must
5545 recurse here. */
5546 return get_next_display_element (it);
5549 if (it->current.overlay_string_index >= 0)
5551 /* Get the next character from an overlay string. In overlay
5552 strings, There is no field width or padding with spaces to
5553 do. */
5554 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5556 it->what = IT_EOB;
5557 return 0;
5559 else if (STRING_MULTIBYTE (it->string))
5561 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5562 const unsigned char *s = (SDATA (it->string)
5563 + IT_STRING_BYTEPOS (*it));
5564 it->c = string_char_and_length (s, remaining, &it->len);
5566 else
5568 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5569 it->len = 1;
5572 else
5574 /* Get the next character from a Lisp string that is not an
5575 overlay string. Such strings come from the mode line, for
5576 example. We may have to pad with spaces, or truncate the
5577 string. See also next_element_from_c_string. */
5578 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5580 it->what = IT_EOB;
5581 return 0;
5583 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5585 /* Pad with spaces. */
5586 it->c = ' ', it->len = 1;
5587 CHARPOS (position) = BYTEPOS (position) = -1;
5589 else if (STRING_MULTIBYTE (it->string))
5591 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5592 const unsigned char *s = (SDATA (it->string)
5593 + IT_STRING_BYTEPOS (*it));
5594 it->c = string_char_and_length (s, maxlen, &it->len);
5596 else
5598 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5599 it->len = 1;
5603 /* Record what we have and where it came from. Note that we store a
5604 buffer position in IT->position although it could arguably be a
5605 string position. */
5606 it->what = IT_CHARACTER;
5607 it->object = it->string;
5608 it->position = position;
5609 return 1;
5613 /* Load IT with next display element from C string IT->s.
5614 IT->string_nchars is the maximum number of characters to return
5615 from the string. IT->end_charpos may be greater than
5616 IT->string_nchars when this function is called, in which case we
5617 may have to return padding spaces. Value is zero if end of string
5618 reached, including padding spaces. */
5620 static int
5621 next_element_from_c_string (it)
5622 struct it *it;
5624 int success_p = 1;
5626 xassert (it->s);
5627 it->what = IT_CHARACTER;
5628 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5629 it->object = Qnil;
5631 /* IT's position can be greater IT->string_nchars in case a field
5632 width or precision has been specified when the iterator was
5633 initialized. */
5634 if (IT_CHARPOS (*it) >= it->end_charpos)
5636 /* End of the game. */
5637 it->what = IT_EOB;
5638 success_p = 0;
5640 else if (IT_CHARPOS (*it) >= it->string_nchars)
5642 /* Pad with spaces. */
5643 it->c = ' ', it->len = 1;
5644 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5646 else if (it->multibyte_p)
5648 /* Implementation note: The calls to strlen apparently aren't a
5649 performance problem because there is no noticeable performance
5650 difference between Emacs running in unibyte or multibyte mode. */
5651 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5652 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5653 maxlen, &it->len);
5655 else
5656 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5658 return success_p;
5662 /* Set up IT to return characters from an ellipsis, if appropriate.
5663 The definition of the ellipsis glyphs may come from a display table
5664 entry. This function Fills IT with the first glyph from the
5665 ellipsis if an ellipsis is to be displayed. */
5667 static int
5668 next_element_from_ellipsis (it)
5669 struct it *it;
5671 if (it->selective_display_ellipsis_p)
5672 setup_for_ellipsis (it, it->len);
5673 else
5675 /* The face at the current position may be different from the
5676 face we find after the invisible text. Remember what it
5677 was in IT->saved_face_id, and signal that it's there by
5678 setting face_before_selective_p. */
5679 it->saved_face_id = it->face_id;
5680 it->method = GET_FROM_BUFFER;
5681 reseat_at_next_visible_line_start (it, 1);
5682 it->face_before_selective_p = 1;
5685 return get_next_display_element (it);
5689 /* Deliver an image display element. The iterator IT is already
5690 filled with image information (done in handle_display_prop). Value
5691 is always 1. */
5694 static int
5695 next_element_from_image (it)
5696 struct it *it;
5698 it->what = IT_IMAGE;
5699 return 1;
5703 /* Fill iterator IT with next display element from a stretch glyph
5704 property. IT->object is the value of the text property. Value is
5705 always 1. */
5707 static int
5708 next_element_from_stretch (it)
5709 struct it *it;
5711 it->what = IT_STRETCH;
5712 return 1;
5716 /* Load IT with the next display element from current_buffer. Value
5717 is zero if end of buffer reached. IT->stop_charpos is the next
5718 position at which to stop and check for text properties or buffer
5719 end. */
5721 static int
5722 next_element_from_buffer (it)
5723 struct it *it;
5725 int success_p = 1;
5727 /* Check this assumption, otherwise, we would never enter the
5728 if-statement, below. */
5729 xassert (IT_CHARPOS (*it) >= BEGV
5730 && IT_CHARPOS (*it) <= it->stop_charpos);
5732 if (IT_CHARPOS (*it) >= it->stop_charpos)
5734 if (IT_CHARPOS (*it) >= it->end_charpos)
5736 int overlay_strings_follow_p;
5738 /* End of the game, except when overlay strings follow that
5739 haven't been returned yet. */
5740 if (it->overlay_strings_at_end_processed_p)
5741 overlay_strings_follow_p = 0;
5742 else
5744 it->overlay_strings_at_end_processed_p = 1;
5745 overlay_strings_follow_p = get_overlay_strings (it, 0);
5748 if (overlay_strings_follow_p)
5749 success_p = get_next_display_element (it);
5750 else
5752 it->what = IT_EOB;
5753 it->position = it->current.pos;
5754 success_p = 0;
5757 else
5759 handle_stop (it);
5760 return get_next_display_element (it);
5763 else
5765 /* No face changes, overlays etc. in sight, so just return a
5766 character from current_buffer. */
5767 unsigned char *p;
5769 /* Maybe run the redisplay end trigger hook. Performance note:
5770 This doesn't seem to cost measurable time. */
5771 if (it->redisplay_end_trigger_charpos
5772 && it->glyph_row
5773 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5774 run_redisplay_end_trigger_hook (it);
5776 /* Get the next character, maybe multibyte. */
5777 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5778 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5780 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5781 - IT_BYTEPOS (*it));
5782 it->c = string_char_and_length (p, maxlen, &it->len);
5784 else
5785 it->c = *p, it->len = 1;
5787 /* Record what we have and where it came from. */
5788 it->what = IT_CHARACTER;;
5789 it->object = it->w->buffer;
5790 it->position = it->current.pos;
5792 /* Normally we return the character found above, except when we
5793 really want to return an ellipsis for selective display. */
5794 if (it->selective)
5796 if (it->c == '\n')
5798 /* A value of selective > 0 means hide lines indented more
5799 than that number of columns. */
5800 if (it->selective > 0
5801 && IT_CHARPOS (*it) + 1 < ZV
5802 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5803 IT_BYTEPOS (*it) + 1,
5804 (double) it->selective)) /* iftc */
5806 success_p = next_element_from_ellipsis (it);
5807 it->dpvec_char_len = -1;
5810 else if (it->c == '\r' && it->selective == -1)
5812 /* A value of selective == -1 means that everything from the
5813 CR to the end of the line is invisible, with maybe an
5814 ellipsis displayed for it. */
5815 success_p = next_element_from_ellipsis (it);
5816 it->dpvec_char_len = -1;
5821 /* Value is zero if end of buffer reached. */
5822 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5823 return success_p;
5827 /* Run the redisplay end trigger hook for IT. */
5829 static void
5830 run_redisplay_end_trigger_hook (it)
5831 struct it *it;
5833 Lisp_Object args[3];
5835 /* IT->glyph_row should be non-null, i.e. we should be actually
5836 displaying something, or otherwise we should not run the hook. */
5837 xassert (it->glyph_row);
5839 /* Set up hook arguments. */
5840 args[0] = Qredisplay_end_trigger_functions;
5841 args[1] = it->window;
5842 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5843 it->redisplay_end_trigger_charpos = 0;
5845 /* Since we are *trying* to run these functions, don't try to run
5846 them again, even if they get an error. */
5847 it->w->redisplay_end_trigger = Qnil;
5848 Frun_hook_with_args (3, args);
5850 /* Notice if it changed the face of the character we are on. */
5851 handle_face_prop (it);
5855 /* Deliver a composition display element. The iterator IT is already
5856 filled with composition information (done in
5857 handle_composition_prop). Value is always 1. */
5859 static int
5860 next_element_from_composition (it)
5861 struct it *it;
5863 it->what = IT_COMPOSITION;
5864 it->position = (STRINGP (it->string)
5865 ? it->current.string_pos
5866 : it->current.pos);
5867 return 1;
5872 /***********************************************************************
5873 Moving an iterator without producing glyphs
5874 ***********************************************************************/
5876 /* Check if iterator is at a position corresponding to a valid buffer
5877 position after some move_it_ call. */
5879 #define IT_POS_VALID_AFTER_MOVE_P(it) \
5880 ((it)->method == GET_FROM_STRING \
5881 ? IT_STRING_CHARPOS (*it) == 0 \
5882 : 1)
5885 /* Move iterator IT to a specified buffer or X position within one
5886 line on the display without producing glyphs.
5888 OP should be a bit mask including some or all of these bits:
5889 MOVE_TO_X: Stop on reaching x-position TO_X.
5890 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5891 Regardless of OP's value, stop in reaching the end of the display line.
5893 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5894 This means, in particular, that TO_X includes window's horizontal
5895 scroll amount.
5897 The return value has several possible values that
5898 say what condition caused the scan to stop:
5900 MOVE_POS_MATCH_OR_ZV
5901 - when TO_POS or ZV was reached.
5903 MOVE_X_REACHED
5904 -when TO_X was reached before TO_POS or ZV were reached.
5906 MOVE_LINE_CONTINUED
5907 - when we reached the end of the display area and the line must
5908 be continued.
5910 MOVE_LINE_TRUNCATED
5911 - when we reached the end of the display area and the line is
5912 truncated.
5914 MOVE_NEWLINE_OR_CR
5915 - when we stopped at a line end, i.e. a newline or a CR and selective
5916 display is on. */
5918 static enum move_it_result
5919 move_it_in_display_line_to (it, to_charpos, to_x, op)
5920 struct it *it;
5921 int to_charpos, to_x, op;
5923 enum move_it_result result = MOVE_UNDEFINED;
5924 struct glyph_row *saved_glyph_row;
5926 /* Don't produce glyphs in produce_glyphs. */
5927 saved_glyph_row = it->glyph_row;
5928 it->glyph_row = NULL;
5930 #define BUFFER_POS_REACHED_P() \
5931 ((op & MOVE_TO_POS) != 0 \
5932 && BUFFERP (it->object) \
5933 && IT_CHARPOS (*it) >= to_charpos \
5934 && (it->method == GET_FROM_BUFFER \
5935 || (it->method == GET_FROM_DISPLAY_VECTOR \
5936 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5939 while (1)
5941 int x, i, ascent = 0, descent = 0;
5943 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5944 if ((op & MOVE_TO_POS) != 0
5945 && BUFFERP (it->object)
5946 && it->method == GET_FROM_BUFFER
5947 && IT_CHARPOS (*it) > to_charpos)
5949 result = MOVE_POS_MATCH_OR_ZV;
5950 break;
5953 /* Stop when ZV reached.
5954 We used to stop here when TO_CHARPOS reached as well, but that is
5955 too soon if this glyph does not fit on this line. So we handle it
5956 explicitly below. */
5957 if (!get_next_display_element (it)
5958 || (it->truncate_lines_p
5959 && BUFFER_POS_REACHED_P ()))
5961 result = MOVE_POS_MATCH_OR_ZV;
5962 break;
5965 /* The call to produce_glyphs will get the metrics of the
5966 display element IT is loaded with. We record in x the
5967 x-position before this display element in case it does not
5968 fit on the line. */
5969 x = it->current_x;
5971 /* Remember the line height so far in case the next element doesn't
5972 fit on the line. */
5973 if (!it->truncate_lines_p)
5975 ascent = it->max_ascent;
5976 descent = it->max_descent;
5979 PRODUCE_GLYPHS (it);
5981 if (it->area != TEXT_AREA)
5983 set_iterator_to_next (it, 1);
5984 continue;
5987 /* The number of glyphs we get back in IT->nglyphs will normally
5988 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5989 character on a terminal frame, or (iii) a line end. For the
5990 second case, IT->nglyphs - 1 padding glyphs will be present
5991 (on X frames, there is only one glyph produced for a
5992 composite character.
5994 The behavior implemented below means, for continuation lines,
5995 that as many spaces of a TAB as fit on the current line are
5996 displayed there. For terminal frames, as many glyphs of a
5997 multi-glyph character are displayed in the current line, too.
5998 This is what the old redisplay code did, and we keep it that
5999 way. Under X, the whole shape of a complex character must
6000 fit on the line or it will be completely displayed in the
6001 next line.
6003 Note that both for tabs and padding glyphs, all glyphs have
6004 the same width. */
6005 if (it->nglyphs)
6007 /* More than one glyph or glyph doesn't fit on line. All
6008 glyphs have the same width. */
6009 int single_glyph_width = it->pixel_width / it->nglyphs;
6010 int new_x;
6012 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6014 new_x = x + single_glyph_width;
6016 /* We want to leave anything reaching TO_X to the caller. */
6017 if ((op & MOVE_TO_X) && new_x > to_x)
6019 if (BUFFER_POS_REACHED_P ())
6020 goto buffer_pos_reached;
6021 it->current_x = x;
6022 result = MOVE_X_REACHED;
6023 break;
6025 else if (/* Lines are continued. */
6026 !it->truncate_lines_p
6027 && (/* And glyph doesn't fit on the line. */
6028 new_x > it->last_visible_x
6029 /* Or it fits exactly and we're on a window
6030 system frame. */
6031 || (new_x == it->last_visible_x
6032 && FRAME_WINDOW_P (it->f))))
6034 if (/* IT->hpos == 0 means the very first glyph
6035 doesn't fit on the line, e.g. a wide image. */
6036 it->hpos == 0
6037 || (new_x == it->last_visible_x
6038 && FRAME_WINDOW_P (it->f)))
6040 ++it->hpos;
6041 it->current_x = new_x;
6042 if (i == it->nglyphs - 1)
6044 set_iterator_to_next (it, 1);
6045 #ifdef HAVE_WINDOW_SYSTEM
6046 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6048 if (!get_next_display_element (it))
6050 result = MOVE_POS_MATCH_OR_ZV;
6051 break;
6053 if (BUFFER_POS_REACHED_P ())
6055 if (ITERATOR_AT_END_OF_LINE_P (it))
6056 result = MOVE_POS_MATCH_OR_ZV;
6057 else
6058 result = MOVE_LINE_CONTINUED;
6059 break;
6061 if (ITERATOR_AT_END_OF_LINE_P (it))
6063 result = MOVE_NEWLINE_OR_CR;
6064 break;
6067 #endif /* HAVE_WINDOW_SYSTEM */
6070 else
6072 it->current_x = x;
6073 it->max_ascent = ascent;
6074 it->max_descent = descent;
6077 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6078 IT_CHARPOS (*it)));
6079 result = MOVE_LINE_CONTINUED;
6080 break;
6082 else if (BUFFER_POS_REACHED_P ())
6083 goto buffer_pos_reached;
6084 else if (new_x > it->first_visible_x)
6086 /* Glyph is visible. Increment number of glyphs that
6087 would be displayed. */
6088 ++it->hpos;
6090 else
6092 /* Glyph is completely off the left margin of the display
6093 area. Nothing to do. */
6097 if (result != MOVE_UNDEFINED)
6098 break;
6100 else if (BUFFER_POS_REACHED_P ())
6102 buffer_pos_reached:
6103 it->current_x = x;
6104 it->max_ascent = ascent;
6105 it->max_descent = descent;
6106 result = MOVE_POS_MATCH_OR_ZV;
6107 break;
6109 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6111 /* Stop when TO_X specified and reached. This check is
6112 necessary here because of lines consisting of a line end,
6113 only. The line end will not produce any glyphs and we
6114 would never get MOVE_X_REACHED. */
6115 xassert (it->nglyphs == 0);
6116 result = MOVE_X_REACHED;
6117 break;
6120 /* Is this a line end? If yes, we're done. */
6121 if (ITERATOR_AT_END_OF_LINE_P (it))
6123 result = MOVE_NEWLINE_OR_CR;
6124 break;
6127 /* The current display element has been consumed. Advance
6128 to the next. */
6129 set_iterator_to_next (it, 1);
6131 /* Stop if lines are truncated and IT's current x-position is
6132 past the right edge of the window now. */
6133 if (it->truncate_lines_p
6134 && it->current_x >= it->last_visible_x)
6136 #ifdef HAVE_WINDOW_SYSTEM
6137 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6139 if (!get_next_display_element (it)
6140 || BUFFER_POS_REACHED_P ())
6142 result = MOVE_POS_MATCH_OR_ZV;
6143 break;
6145 if (ITERATOR_AT_END_OF_LINE_P (it))
6147 result = MOVE_NEWLINE_OR_CR;
6148 break;
6151 #endif /* HAVE_WINDOW_SYSTEM */
6152 result = MOVE_LINE_TRUNCATED;
6153 break;
6157 #undef BUFFER_POS_REACHED_P
6159 /* Restore the iterator settings altered at the beginning of this
6160 function. */
6161 it->glyph_row = saved_glyph_row;
6162 return result;
6166 /* Move IT forward until it satisfies one or more of the criteria in
6167 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6169 OP is a bit-mask that specifies where to stop, and in particular,
6170 which of those four position arguments makes a difference. See the
6171 description of enum move_operation_enum.
6173 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6174 screen line, this function will set IT to the next position >
6175 TO_CHARPOS. */
6177 void
6178 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6179 struct it *it;
6180 int to_charpos, to_x, to_y, to_vpos;
6181 int op;
6183 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6184 int line_height;
6185 int reached = 0;
6187 for (;;)
6189 if (op & MOVE_TO_VPOS)
6191 /* If no TO_CHARPOS and no TO_X specified, stop at the
6192 start of the line TO_VPOS. */
6193 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6195 if (it->vpos == to_vpos)
6197 reached = 1;
6198 break;
6200 else
6201 skip = move_it_in_display_line_to (it, -1, -1, 0);
6203 else
6205 /* TO_VPOS >= 0 means stop at TO_X in the line at
6206 TO_VPOS, or at TO_POS, whichever comes first. */
6207 if (it->vpos == to_vpos)
6209 reached = 2;
6210 break;
6213 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6215 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6217 reached = 3;
6218 break;
6220 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6222 /* We have reached TO_X but not in the line we want. */
6223 skip = move_it_in_display_line_to (it, to_charpos,
6224 -1, MOVE_TO_POS);
6225 if (skip == MOVE_POS_MATCH_OR_ZV)
6227 reached = 4;
6228 break;
6233 else if (op & MOVE_TO_Y)
6235 struct it it_backup;
6237 /* TO_Y specified means stop at TO_X in the line containing
6238 TO_Y---or at TO_CHARPOS if this is reached first. The
6239 problem is that we can't really tell whether the line
6240 contains TO_Y before we have completely scanned it, and
6241 this may skip past TO_X. What we do is to first scan to
6242 TO_X.
6244 If TO_X is not specified, use a TO_X of zero. The reason
6245 is to make the outcome of this function more predictable.
6246 If we didn't use TO_X == 0, we would stop at the end of
6247 the line which is probably not what a caller would expect
6248 to happen. */
6249 skip = move_it_in_display_line_to (it, to_charpos,
6250 ((op & MOVE_TO_X)
6251 ? to_x : 0),
6252 (MOVE_TO_X
6253 | (op & MOVE_TO_POS)));
6255 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6256 if (skip == MOVE_POS_MATCH_OR_ZV)
6258 reached = 5;
6259 break;
6262 /* If TO_X was reached, we would like to know whether TO_Y
6263 is in the line. This can only be said if we know the
6264 total line height which requires us to scan the rest of
6265 the line. */
6266 if (skip == MOVE_X_REACHED)
6268 it_backup = *it;
6269 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6270 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6271 op & MOVE_TO_POS);
6272 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6275 /* Now, decide whether TO_Y is in this line. */
6276 line_height = it->max_ascent + it->max_descent;
6277 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6279 if (to_y >= it->current_y
6280 && to_y < it->current_y + line_height)
6282 if (skip == MOVE_X_REACHED)
6283 /* If TO_Y is in this line and TO_X was reached above,
6284 we scanned too far. We have to restore IT's settings
6285 to the ones before skipping. */
6286 *it = it_backup;
6287 reached = 6;
6289 else if (skip == MOVE_X_REACHED)
6291 skip = skip2;
6292 if (skip == MOVE_POS_MATCH_OR_ZV)
6293 reached = 7;
6296 if (reached)
6297 break;
6299 else
6300 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6302 switch (skip)
6304 case MOVE_POS_MATCH_OR_ZV:
6305 reached = 8;
6306 goto out;
6308 case MOVE_NEWLINE_OR_CR:
6309 set_iterator_to_next (it, 1);
6310 it->continuation_lines_width = 0;
6311 break;
6313 case MOVE_LINE_TRUNCATED:
6314 it->continuation_lines_width = 0;
6315 reseat_at_next_visible_line_start (it, 0);
6316 if ((op & MOVE_TO_POS) != 0
6317 && IT_CHARPOS (*it) > to_charpos)
6319 reached = 9;
6320 goto out;
6322 break;
6324 case MOVE_LINE_CONTINUED:
6325 it->continuation_lines_width += it->current_x;
6326 break;
6328 default:
6329 abort ();
6332 /* Reset/increment for the next run. */
6333 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6334 it->current_x = it->hpos = 0;
6335 it->current_y += it->max_ascent + it->max_descent;
6336 ++it->vpos;
6337 last_height = it->max_ascent + it->max_descent;
6338 last_max_ascent = it->max_ascent;
6339 it->max_ascent = it->max_descent = 0;
6342 out:
6344 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6348 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6350 If DY > 0, move IT backward at least that many pixels. DY = 0
6351 means move IT backward to the preceding line start or BEGV. This
6352 function may move over more than DY pixels if IT->current_y - DY
6353 ends up in the middle of a line; in this case IT->current_y will be
6354 set to the top of the line moved to. */
6356 void
6357 move_it_vertically_backward (it, dy)
6358 struct it *it;
6359 int dy;
6361 int nlines, h;
6362 struct it it2, it3;
6363 int start_pos;
6365 move_further_back:
6366 xassert (dy >= 0);
6368 start_pos = IT_CHARPOS (*it);
6370 /* Estimate how many newlines we must move back. */
6371 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6373 /* Set the iterator's position that many lines back. */
6374 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6375 back_to_previous_visible_line_start (it);
6377 /* Reseat the iterator here. When moving backward, we don't want
6378 reseat to skip forward over invisible text, set up the iterator
6379 to deliver from overlay strings at the new position etc. So,
6380 use reseat_1 here. */
6381 reseat_1 (it, it->current.pos, 1);
6383 /* We are now surely at a line start. */
6384 it->current_x = it->hpos = 0;
6385 it->continuation_lines_width = 0;
6387 /* Move forward and see what y-distance we moved. First move to the
6388 start of the next line so that we get its height. We need this
6389 height to be able to tell whether we reached the specified
6390 y-distance. */
6391 it2 = *it;
6392 it2.max_ascent = it2.max_descent = 0;
6395 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6396 MOVE_TO_POS | MOVE_TO_VPOS);
6398 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6399 xassert (IT_CHARPOS (*it) >= BEGV);
6400 it3 = it2;
6402 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6403 xassert (IT_CHARPOS (*it) >= BEGV);
6404 /* H is the actual vertical distance from the position in *IT
6405 and the starting position. */
6406 h = it2.current_y - it->current_y;
6407 /* NLINES is the distance in number of lines. */
6408 nlines = it2.vpos - it->vpos;
6410 /* Correct IT's y and vpos position
6411 so that they are relative to the starting point. */
6412 it->vpos -= nlines;
6413 it->current_y -= h;
6415 if (dy == 0)
6417 /* DY == 0 means move to the start of the screen line. The
6418 value of nlines is > 0 if continuation lines were involved. */
6419 if (nlines > 0)
6420 move_it_by_lines (it, nlines, 1);
6421 #if 0
6422 /* I think this assert is bogus if buffer contains
6423 invisible text or images. KFS. */
6424 xassert (IT_CHARPOS (*it) <= start_pos);
6425 #endif
6427 else
6429 /* The y-position we try to reach, relative to *IT.
6430 Note that H has been subtracted in front of the if-statement. */
6431 int target_y = it->current_y + h - dy;
6432 int y0 = it3.current_y;
6433 int y1 = line_bottom_y (&it3);
6434 int line_height = y1 - y0;
6436 /* If we did not reach target_y, try to move further backward if
6437 we can. If we moved too far backward, try to move forward. */
6438 if (target_y < it->current_y
6439 /* This is heuristic. In a window that's 3 lines high, with
6440 a line height of 13 pixels each, recentering with point
6441 on the bottom line will try to move -39/2 = 19 pixels
6442 backward. Try to avoid moving into the first line. */
6443 && (it->current_y - target_y
6444 > min (window_box_height (it->w), line_height * 2 / 3))
6445 && IT_CHARPOS (*it) > BEGV)
6447 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6448 target_y - it->current_y));
6449 dy = it->current_y - target_y;
6450 goto move_further_back;
6452 else if (target_y >= it->current_y + line_height
6453 && IT_CHARPOS (*it) < ZV)
6455 /* Should move forward by at least one line, maybe more.
6457 Note: Calling move_it_by_lines can be expensive on
6458 terminal frames, where compute_motion is used (via
6459 vmotion) to do the job, when there are very long lines
6460 and truncate-lines is nil. That's the reason for
6461 treating terminal frames specially here. */
6463 if (!FRAME_WINDOW_P (it->f))
6464 move_it_vertically (it, target_y - (it->current_y + line_height));
6465 else
6469 move_it_by_lines (it, 1, 1);
6471 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6474 #if 0
6475 /* I think this assert is bogus if buffer contains
6476 invisible text or images. KFS. */
6477 xassert (IT_CHARPOS (*it) >= BEGV);
6478 #endif
6484 /* Move IT by a specified amount of pixel lines DY. DY negative means
6485 move backwards. DY = 0 means move to start of screen line. At the
6486 end, IT will be on the start of a screen line. */
6488 void
6489 move_it_vertically (it, dy)
6490 struct it *it;
6491 int dy;
6493 if (dy <= 0)
6494 move_it_vertically_backward (it, -dy);
6495 else
6497 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6498 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6499 MOVE_TO_POS | MOVE_TO_Y);
6500 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6502 /* If buffer ends in ZV without a newline, move to the start of
6503 the line to satisfy the post-condition. */
6504 if (IT_CHARPOS (*it) == ZV
6505 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6506 move_it_by_lines (it, 0, 0);
6511 /* Move iterator IT past the end of the text line it is in. */
6513 void
6514 move_it_past_eol (it)
6515 struct it *it;
6517 enum move_it_result rc;
6519 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6520 if (rc == MOVE_NEWLINE_OR_CR)
6521 set_iterator_to_next (it, 0);
6525 #if 0 /* Currently not used. */
6527 /* Return non-zero if some text between buffer positions START_CHARPOS
6528 and END_CHARPOS is invisible. IT->window is the window for text
6529 property lookup. */
6531 static int
6532 invisible_text_between_p (it, start_charpos, end_charpos)
6533 struct it *it;
6534 int start_charpos, end_charpos;
6536 Lisp_Object prop, limit;
6537 int invisible_found_p;
6539 xassert (it != NULL && start_charpos <= end_charpos);
6541 /* Is text at START invisible? */
6542 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6543 it->window);
6544 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6545 invisible_found_p = 1;
6546 else
6548 limit = Fnext_single_char_property_change (make_number (start_charpos),
6549 Qinvisible, Qnil,
6550 make_number (end_charpos));
6551 invisible_found_p = XFASTINT (limit) < end_charpos;
6554 return invisible_found_p;
6557 #endif /* 0 */
6560 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6561 negative means move up. DVPOS == 0 means move to the start of the
6562 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6563 NEED_Y_P is zero, IT->current_y will be left unchanged.
6565 Further optimization ideas: If we would know that IT->f doesn't use
6566 a face with proportional font, we could be faster for
6567 truncate-lines nil. */
6569 void
6570 move_it_by_lines (it, dvpos, need_y_p)
6571 struct it *it;
6572 int dvpos, need_y_p;
6574 struct position pos;
6576 if (!FRAME_WINDOW_P (it->f))
6578 struct text_pos textpos;
6580 /* We can use vmotion on frames without proportional fonts. */
6581 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6582 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6583 reseat (it, textpos, 1);
6584 it->vpos += pos.vpos;
6585 it->current_y += pos.vpos;
6587 else if (dvpos == 0)
6589 /* DVPOS == 0 means move to the start of the screen line. */
6590 move_it_vertically_backward (it, 0);
6591 xassert (it->current_x == 0 && it->hpos == 0);
6592 /* Let next call to line_bottom_y calculate real line height */
6593 last_height = 0;
6595 else if (dvpos > 0)
6597 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6598 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6599 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6601 else
6603 struct it it2;
6604 int start_charpos, i;
6606 /* Start at the beginning of the screen line containing IT's
6607 position. This may actually move vertically backwards,
6608 in case of overlays, so adjust dvpos accordingly. */
6609 dvpos += it->vpos;
6610 move_it_vertically_backward (it, 0);
6611 dvpos -= it->vpos;
6613 /* Go back -DVPOS visible lines and reseat the iterator there. */
6614 start_charpos = IT_CHARPOS (*it);
6615 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6616 back_to_previous_visible_line_start (it);
6617 reseat (it, it->current.pos, 1);
6619 /* Move further back if we end up in a string or an image. */
6620 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6622 /* First try to move to start of display line. */
6623 dvpos += it->vpos;
6624 move_it_vertically_backward (it, 0);
6625 dvpos -= it->vpos;
6626 if (IT_POS_VALID_AFTER_MOVE_P (it))
6627 break;
6628 /* If start of line is still in string or image,
6629 move further back. */
6630 back_to_previous_visible_line_start (it);
6631 reseat (it, it->current.pos, 1);
6632 dvpos--;
6635 it->current_x = it->hpos = 0;
6637 /* Above call may have moved too far if continuation lines
6638 are involved. Scan forward and see if it did. */
6639 it2 = *it;
6640 it2.vpos = it2.current_y = 0;
6641 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6642 it->vpos -= it2.vpos;
6643 it->current_y -= it2.current_y;
6644 it->current_x = it->hpos = 0;
6646 /* If we moved too far back, move IT some lines forward. */
6647 if (it2.vpos > -dvpos)
6649 int delta = it2.vpos + dvpos;
6650 it2 = *it;
6651 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6652 /* Move back again if we got too far ahead. */
6653 if (IT_CHARPOS (*it) >= start_charpos)
6654 *it = it2;
6659 /* Return 1 if IT points into the middle of a display vector. */
6662 in_display_vector_p (it)
6663 struct it *it;
6665 return (it->method == GET_FROM_DISPLAY_VECTOR
6666 && it->current.dpvec_index > 0
6667 && it->dpvec + it->current.dpvec_index != it->dpend);
6671 /***********************************************************************
6672 Messages
6673 ***********************************************************************/
6676 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6677 to *Messages*. */
6679 void
6680 add_to_log (format, arg1, arg2)
6681 char *format;
6682 Lisp_Object arg1, arg2;
6684 Lisp_Object args[3];
6685 Lisp_Object msg, fmt;
6686 char *buffer;
6687 int len;
6688 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6689 USE_SAFE_ALLOCA;
6691 /* Do nothing if called asynchronously. Inserting text into
6692 a buffer may call after-change-functions and alike and
6693 that would means running Lisp asynchronously. */
6694 if (handling_signal)
6695 return;
6697 fmt = msg = Qnil;
6698 GCPRO4 (fmt, msg, arg1, arg2);
6700 args[0] = fmt = build_string (format);
6701 args[1] = arg1;
6702 args[2] = arg2;
6703 msg = Fformat (3, args);
6705 len = SBYTES (msg) + 1;
6706 SAFE_ALLOCA (buffer, char *, len);
6707 bcopy (SDATA (msg), buffer, len);
6709 message_dolog (buffer, len - 1, 1, 0);
6710 SAFE_FREE ();
6712 UNGCPRO;
6716 /* Output a newline in the *Messages* buffer if "needs" one. */
6718 void
6719 message_log_maybe_newline ()
6721 if (message_log_need_newline)
6722 message_dolog ("", 0, 1, 0);
6726 /* Add a string M of length NBYTES to the message log, optionally
6727 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6728 nonzero, means interpret the contents of M as multibyte. This
6729 function calls low-level routines in order to bypass text property
6730 hooks, etc. which might not be safe to run. */
6732 void
6733 message_dolog (m, nbytes, nlflag, multibyte)
6734 const char *m;
6735 int nbytes, nlflag, multibyte;
6737 if (!NILP (Vmemory_full))
6738 return;
6740 if (!NILP (Vmessage_log_max))
6742 struct buffer *oldbuf;
6743 Lisp_Object oldpoint, oldbegv, oldzv;
6744 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6745 int point_at_end = 0;
6746 int zv_at_end = 0;
6747 Lisp_Object old_deactivate_mark, tem;
6748 struct gcpro gcpro1;
6750 old_deactivate_mark = Vdeactivate_mark;
6751 oldbuf = current_buffer;
6752 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6753 current_buffer->undo_list = Qt;
6755 oldpoint = message_dolog_marker1;
6756 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6757 oldbegv = message_dolog_marker2;
6758 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6759 oldzv = message_dolog_marker3;
6760 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6761 GCPRO1 (old_deactivate_mark);
6763 if (PT == Z)
6764 point_at_end = 1;
6765 if (ZV == Z)
6766 zv_at_end = 1;
6768 BEGV = BEG;
6769 BEGV_BYTE = BEG_BYTE;
6770 ZV = Z;
6771 ZV_BYTE = Z_BYTE;
6772 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6774 /* Insert the string--maybe converting multibyte to single byte
6775 or vice versa, so that all the text fits the buffer. */
6776 if (multibyte
6777 && NILP (current_buffer->enable_multibyte_characters))
6779 int i, c, char_bytes;
6780 unsigned char work[1];
6782 /* Convert a multibyte string to single-byte
6783 for the *Message* buffer. */
6784 for (i = 0; i < nbytes; i += char_bytes)
6786 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6787 work[0] = (SINGLE_BYTE_CHAR_P (c)
6789 : multibyte_char_to_unibyte (c, Qnil));
6790 insert_1_both (work, 1, 1, 1, 0, 0);
6793 else if (! multibyte
6794 && ! NILP (current_buffer->enable_multibyte_characters))
6796 int i, c, char_bytes;
6797 unsigned char *msg = (unsigned char *) m;
6798 unsigned char str[MAX_MULTIBYTE_LENGTH];
6799 /* Convert a single-byte string to multibyte
6800 for the *Message* buffer. */
6801 for (i = 0; i < nbytes; i++)
6803 c = unibyte_char_to_multibyte (msg[i]);
6804 char_bytes = CHAR_STRING (c, str);
6805 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6808 else if (nbytes)
6809 insert_1 (m, nbytes, 1, 0, 0);
6811 if (nlflag)
6813 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6814 insert_1 ("\n", 1, 1, 0, 0);
6816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6817 this_bol = PT;
6818 this_bol_byte = PT_BYTE;
6820 /* See if this line duplicates the previous one.
6821 If so, combine duplicates. */
6822 if (this_bol > BEG)
6824 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6825 prev_bol = PT;
6826 prev_bol_byte = PT_BYTE;
6828 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6829 this_bol, this_bol_byte);
6830 if (dup)
6832 del_range_both (prev_bol, prev_bol_byte,
6833 this_bol, this_bol_byte, 0);
6834 if (dup > 1)
6836 char dupstr[40];
6837 int duplen;
6839 /* If you change this format, don't forget to also
6840 change message_log_check_duplicate. */
6841 sprintf (dupstr, " [%d times]", dup);
6842 duplen = strlen (dupstr);
6843 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6844 insert_1 (dupstr, duplen, 1, 0, 1);
6849 /* If we have more than the desired maximum number of lines
6850 in the *Messages* buffer now, delete the oldest ones.
6851 This is safe because we don't have undo in this buffer. */
6853 if (NATNUMP (Vmessage_log_max))
6855 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6856 -XFASTINT (Vmessage_log_max) - 1, 0);
6857 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6860 BEGV = XMARKER (oldbegv)->charpos;
6861 BEGV_BYTE = marker_byte_position (oldbegv);
6863 if (zv_at_end)
6865 ZV = Z;
6866 ZV_BYTE = Z_BYTE;
6868 else
6870 ZV = XMARKER (oldzv)->charpos;
6871 ZV_BYTE = marker_byte_position (oldzv);
6874 if (point_at_end)
6875 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6876 else
6877 /* We can't do Fgoto_char (oldpoint) because it will run some
6878 Lisp code. */
6879 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6880 XMARKER (oldpoint)->bytepos);
6882 UNGCPRO;
6883 unchain_marker (XMARKER (oldpoint));
6884 unchain_marker (XMARKER (oldbegv));
6885 unchain_marker (XMARKER (oldzv));
6887 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6888 set_buffer_internal (oldbuf);
6889 if (NILP (tem))
6890 windows_or_buffers_changed = old_windows_or_buffers_changed;
6891 message_log_need_newline = !nlflag;
6892 Vdeactivate_mark = old_deactivate_mark;
6897 /* We are at the end of the buffer after just having inserted a newline.
6898 (Note: We depend on the fact we won't be crossing the gap.)
6899 Check to see if the most recent message looks a lot like the previous one.
6900 Return 0 if different, 1 if the new one should just replace it, or a
6901 value N > 1 if we should also append " [N times]". */
6903 static int
6904 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6905 int prev_bol, this_bol;
6906 int prev_bol_byte, this_bol_byte;
6908 int i;
6909 int len = Z_BYTE - 1 - this_bol_byte;
6910 int seen_dots = 0;
6911 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6912 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6914 for (i = 0; i < len; i++)
6916 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6917 seen_dots = 1;
6918 if (p1[i] != p2[i])
6919 return seen_dots;
6921 p1 += len;
6922 if (*p1 == '\n')
6923 return 2;
6924 if (*p1++ == ' ' && *p1++ == '[')
6926 int n = 0;
6927 while (*p1 >= '0' && *p1 <= '9')
6928 n = n * 10 + *p1++ - '0';
6929 if (strncmp (p1, " times]\n", 8) == 0)
6930 return n+1;
6932 return 0;
6936 /* Display an echo area message M with a specified length of NBYTES
6937 bytes. The string may include null characters. If M is 0, clear
6938 out any existing message, and let the mini-buffer text show
6939 through.
6941 The buffer M must continue to exist until after the echo area gets
6942 cleared or some other message gets displayed there. This means do
6943 not pass text that is stored in a Lisp string; do not pass text in
6944 a buffer that was alloca'd. */
6946 void
6947 message2 (m, nbytes, multibyte)
6948 const char *m;
6949 int nbytes;
6950 int multibyte;
6952 /* First flush out any partial line written with print. */
6953 message_log_maybe_newline ();
6954 if (m)
6955 message_dolog (m, nbytes, 1, multibyte);
6956 message2_nolog (m, nbytes, multibyte);
6960 /* The non-logging counterpart of message2. */
6962 void
6963 message2_nolog (m, nbytes, multibyte)
6964 const char *m;
6965 int nbytes, multibyte;
6967 struct frame *sf = SELECTED_FRAME ();
6968 message_enable_multibyte = multibyte;
6970 if (noninteractive)
6972 if (noninteractive_need_newline)
6973 putc ('\n', stderr);
6974 noninteractive_need_newline = 0;
6975 if (m)
6976 fwrite (m, nbytes, 1, stderr);
6977 if (cursor_in_echo_area == 0)
6978 fprintf (stderr, "\n");
6979 fflush (stderr);
6981 /* A null message buffer means that the frame hasn't really been
6982 initialized yet. Error messages get reported properly by
6983 cmd_error, so this must be just an informative message; toss it. */
6984 else if (INTERACTIVE
6985 && sf->glyphs_initialized_p
6986 && FRAME_MESSAGE_BUF (sf))
6988 Lisp_Object mini_window;
6989 struct frame *f;
6991 /* Get the frame containing the mini-buffer
6992 that the selected frame is using. */
6993 mini_window = FRAME_MINIBUF_WINDOW (sf);
6994 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6996 FRAME_SAMPLE_VISIBILITY (f);
6997 if (FRAME_VISIBLE_P (sf)
6998 && ! FRAME_VISIBLE_P (f))
6999 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7001 if (m)
7003 set_message (m, Qnil, nbytes, multibyte);
7004 if (minibuffer_auto_raise)
7005 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7007 else
7008 clear_message (1, 1);
7010 do_pending_window_change (0);
7011 echo_area_display (1);
7012 do_pending_window_change (0);
7013 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7014 (*frame_up_to_date_hook) (f);
7019 /* Display an echo area message M with a specified length of NBYTES
7020 bytes. The string may include null characters. If M is not a
7021 string, clear out any existing message, and let the mini-buffer
7022 text show through.
7024 This function cancels echoing. */
7026 void
7027 message3 (m, nbytes, multibyte)
7028 Lisp_Object m;
7029 int nbytes;
7030 int multibyte;
7032 struct gcpro gcpro1;
7034 GCPRO1 (m);
7035 clear_message (1,1);
7036 cancel_echoing ();
7038 /* First flush out any partial line written with print. */
7039 message_log_maybe_newline ();
7040 if (STRINGP (m))
7041 message_dolog (SDATA (m), nbytes, 1, multibyte);
7042 message3_nolog (m, nbytes, multibyte);
7044 UNGCPRO;
7048 /* The non-logging version of message3.
7049 This does not cancel echoing, because it is used for echoing.
7050 Perhaps we need to make a separate function for echoing
7051 and make this cancel echoing. */
7053 void
7054 message3_nolog (m, nbytes, multibyte)
7055 Lisp_Object m;
7056 int nbytes, multibyte;
7058 struct frame *sf = SELECTED_FRAME ();
7059 message_enable_multibyte = multibyte;
7061 if (noninteractive)
7063 if (noninteractive_need_newline)
7064 putc ('\n', stderr);
7065 noninteractive_need_newline = 0;
7066 if (STRINGP (m))
7067 fwrite (SDATA (m), nbytes, 1, stderr);
7068 if (cursor_in_echo_area == 0)
7069 fprintf (stderr, "\n");
7070 fflush (stderr);
7072 /* A null message buffer means that the frame hasn't really been
7073 initialized yet. Error messages get reported properly by
7074 cmd_error, so this must be just an informative message; toss it. */
7075 else if (INTERACTIVE
7076 && sf->glyphs_initialized_p
7077 && FRAME_MESSAGE_BUF (sf))
7079 Lisp_Object mini_window;
7080 Lisp_Object frame;
7081 struct frame *f;
7083 /* Get the frame containing the mini-buffer
7084 that the selected frame is using. */
7085 mini_window = FRAME_MINIBUF_WINDOW (sf);
7086 frame = XWINDOW (mini_window)->frame;
7087 f = XFRAME (frame);
7089 FRAME_SAMPLE_VISIBILITY (f);
7090 if (FRAME_VISIBLE_P (sf)
7091 && !FRAME_VISIBLE_P (f))
7092 Fmake_frame_visible (frame);
7094 if (STRINGP (m) && SCHARS (m) > 0)
7096 set_message (NULL, m, nbytes, multibyte);
7097 if (minibuffer_auto_raise)
7098 Fraise_frame (frame);
7100 else
7101 clear_message (1, 1);
7103 do_pending_window_change (0);
7104 echo_area_display (1);
7105 do_pending_window_change (0);
7106 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7107 (*frame_up_to_date_hook) (f);
7112 /* Display a null-terminated echo area message M. If M is 0, clear
7113 out any existing message, and let the mini-buffer text show through.
7115 The buffer M must continue to exist until after the echo area gets
7116 cleared or some other message gets displayed there. Do not pass
7117 text that is stored in a Lisp string. Do not pass text in a buffer
7118 that was alloca'd. */
7120 void
7121 message1 (m)
7122 char *m;
7124 message2 (m, (m ? strlen (m) : 0), 0);
7128 /* The non-logging counterpart of message1. */
7130 void
7131 message1_nolog (m)
7132 char *m;
7134 message2_nolog (m, (m ? strlen (m) : 0), 0);
7137 /* Display a message M which contains a single %s
7138 which gets replaced with STRING. */
7140 void
7141 message_with_string (m, string, log)
7142 char *m;
7143 Lisp_Object string;
7144 int log;
7146 CHECK_STRING (string);
7148 if (noninteractive)
7150 if (m)
7152 if (noninteractive_need_newline)
7153 putc ('\n', stderr);
7154 noninteractive_need_newline = 0;
7155 fprintf (stderr, m, SDATA (string));
7156 if (cursor_in_echo_area == 0)
7157 fprintf (stderr, "\n");
7158 fflush (stderr);
7161 else if (INTERACTIVE)
7163 /* The frame whose minibuffer we're going to display the message on.
7164 It may be larger than the selected frame, so we need
7165 to use its buffer, not the selected frame's buffer. */
7166 Lisp_Object mini_window;
7167 struct frame *f, *sf = SELECTED_FRAME ();
7169 /* Get the frame containing the minibuffer
7170 that the selected frame is using. */
7171 mini_window = FRAME_MINIBUF_WINDOW (sf);
7172 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7174 /* A null message buffer means that the frame hasn't really been
7175 initialized yet. Error messages get reported properly by
7176 cmd_error, so this must be just an informative message; toss it. */
7177 if (FRAME_MESSAGE_BUF (f))
7179 Lisp_Object args[2], message;
7180 struct gcpro gcpro1, gcpro2;
7182 args[0] = build_string (m);
7183 args[1] = message = string;
7184 GCPRO2 (args[0], message);
7185 gcpro1.nvars = 2;
7187 message = Fformat (2, args);
7189 if (log)
7190 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7191 else
7192 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7194 UNGCPRO;
7196 /* Print should start at the beginning of the message
7197 buffer next time. */
7198 message_buf_print = 0;
7204 /* Dump an informative message to the minibuf. If M is 0, clear out
7205 any existing message, and let the mini-buffer text show through. */
7207 /* VARARGS 1 */
7208 void
7209 message (m, a1, a2, a3)
7210 char *m;
7211 EMACS_INT a1, a2, a3;
7213 if (noninteractive)
7215 if (m)
7217 if (noninteractive_need_newline)
7218 putc ('\n', stderr);
7219 noninteractive_need_newline = 0;
7220 fprintf (stderr, m, a1, a2, a3);
7221 if (cursor_in_echo_area == 0)
7222 fprintf (stderr, "\n");
7223 fflush (stderr);
7226 else if (INTERACTIVE)
7228 /* The frame whose mini-buffer we're going to display the message
7229 on. It may be larger than the selected frame, so we need to
7230 use its buffer, not the selected frame's buffer. */
7231 Lisp_Object mini_window;
7232 struct frame *f, *sf = SELECTED_FRAME ();
7234 /* Get the frame containing the mini-buffer
7235 that the selected frame is using. */
7236 mini_window = FRAME_MINIBUF_WINDOW (sf);
7237 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7239 /* A null message buffer means that the frame hasn't really been
7240 initialized yet. Error messages get reported properly by
7241 cmd_error, so this must be just an informative message; toss
7242 it. */
7243 if (FRAME_MESSAGE_BUF (f))
7245 if (m)
7247 int len;
7248 #ifdef NO_ARG_ARRAY
7249 char *a[3];
7250 a[0] = (char *) a1;
7251 a[1] = (char *) a2;
7252 a[2] = (char *) a3;
7254 len = doprnt (FRAME_MESSAGE_BUF (f),
7255 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7256 #else
7257 len = doprnt (FRAME_MESSAGE_BUF (f),
7258 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7259 (char **) &a1);
7260 #endif /* NO_ARG_ARRAY */
7262 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7264 else
7265 message1 (0);
7267 /* Print should start at the beginning of the message
7268 buffer next time. */
7269 message_buf_print = 0;
7275 /* The non-logging version of message. */
7277 void
7278 message_nolog (m, a1, a2, a3)
7279 char *m;
7280 EMACS_INT a1, a2, a3;
7282 Lisp_Object old_log_max;
7283 old_log_max = Vmessage_log_max;
7284 Vmessage_log_max = Qnil;
7285 message (m, a1, a2, a3);
7286 Vmessage_log_max = old_log_max;
7290 /* Display the current message in the current mini-buffer. This is
7291 only called from error handlers in process.c, and is not time
7292 critical. */
7294 void
7295 update_echo_area ()
7297 if (!NILP (echo_area_buffer[0]))
7299 Lisp_Object string;
7300 string = Fcurrent_message ();
7301 message3 (string, SBYTES (string),
7302 !NILP (current_buffer->enable_multibyte_characters));
7307 /* Make sure echo area buffers in `echo_buffers' are live.
7308 If they aren't, make new ones. */
7310 static void
7311 ensure_echo_area_buffers ()
7313 int i;
7315 for (i = 0; i < 2; ++i)
7316 if (!BUFFERP (echo_buffer[i])
7317 || NILP (XBUFFER (echo_buffer[i])->name))
7319 char name[30];
7320 Lisp_Object old_buffer;
7321 int j;
7323 old_buffer = echo_buffer[i];
7324 sprintf (name, " *Echo Area %d*", i);
7325 echo_buffer[i] = Fget_buffer_create (build_string (name));
7326 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7328 for (j = 0; j < 2; ++j)
7329 if (EQ (old_buffer, echo_area_buffer[j]))
7330 echo_area_buffer[j] = echo_buffer[i];
7335 /* Call FN with args A1..A4 with either the current or last displayed
7336 echo_area_buffer as current buffer.
7338 WHICH zero means use the current message buffer
7339 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7340 from echo_buffer[] and clear it.
7342 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7343 suitable buffer from echo_buffer[] and clear it.
7345 Value is what FN returns. */
7347 static int
7348 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7349 struct window *w;
7350 int which;
7351 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7352 EMACS_INT a1;
7353 Lisp_Object a2;
7354 EMACS_INT a3, a4;
7356 Lisp_Object buffer;
7357 int this_one, the_other, clear_buffer_p, rc;
7358 int count = SPECPDL_INDEX ();
7360 /* If buffers aren't live, make new ones. */
7361 ensure_echo_area_buffers ();
7363 clear_buffer_p = 0;
7365 if (which == 0)
7366 this_one = 0, the_other = 1;
7367 else if (which > 0)
7368 this_one = 1, the_other = 0;
7370 /* Choose a suitable buffer from echo_buffer[] is we don't
7371 have one. */
7372 if (NILP (echo_area_buffer[this_one]))
7374 echo_area_buffer[this_one]
7375 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7376 ? echo_buffer[the_other]
7377 : echo_buffer[this_one]);
7378 clear_buffer_p = 1;
7381 buffer = echo_area_buffer[this_one];
7383 /* Don't get confused by reusing the buffer used for echoing
7384 for a different purpose. */
7385 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7386 cancel_echoing ();
7388 record_unwind_protect (unwind_with_echo_area_buffer,
7389 with_echo_area_buffer_unwind_data (w));
7391 /* Make the echo area buffer current. Note that for display
7392 purposes, it is not necessary that the displayed window's buffer
7393 == current_buffer, except for text property lookup. So, let's
7394 only set that buffer temporarily here without doing a full
7395 Fset_window_buffer. We must also change w->pointm, though,
7396 because otherwise an assertions in unshow_buffer fails, and Emacs
7397 aborts. */
7398 set_buffer_internal_1 (XBUFFER (buffer));
7399 if (w)
7401 w->buffer = buffer;
7402 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7405 current_buffer->undo_list = Qt;
7406 current_buffer->read_only = Qnil;
7407 specbind (Qinhibit_read_only, Qt);
7408 specbind (Qinhibit_modification_hooks, Qt);
7410 if (clear_buffer_p && Z > BEG)
7411 del_range (BEG, Z);
7413 xassert (BEGV >= BEG);
7414 xassert (ZV <= Z && ZV >= BEGV);
7416 rc = fn (a1, a2, a3, a4);
7418 xassert (BEGV >= BEG);
7419 xassert (ZV <= Z && ZV >= BEGV);
7421 unbind_to (count, Qnil);
7422 return rc;
7426 /* Save state that should be preserved around the call to the function
7427 FN called in with_echo_area_buffer. */
7429 static Lisp_Object
7430 with_echo_area_buffer_unwind_data (w)
7431 struct window *w;
7433 int i = 0;
7434 Lisp_Object vector;
7436 /* Reduce consing by keeping one vector in
7437 Vwith_echo_area_save_vector. */
7438 vector = Vwith_echo_area_save_vector;
7439 Vwith_echo_area_save_vector = Qnil;
7441 if (NILP (vector))
7442 vector = Fmake_vector (make_number (7), Qnil);
7444 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7445 AREF (vector, i) = Vdeactivate_mark, ++i;
7446 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7448 if (w)
7450 XSETWINDOW (AREF (vector, i), w); ++i;
7451 AREF (vector, i) = w->buffer; ++i;
7452 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7453 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7455 else
7457 int end = i + 4;
7458 for (; i < end; ++i)
7459 AREF (vector, i) = Qnil;
7462 xassert (i == ASIZE (vector));
7463 return vector;
7467 /* Restore global state from VECTOR which was created by
7468 with_echo_area_buffer_unwind_data. */
7470 static Lisp_Object
7471 unwind_with_echo_area_buffer (vector)
7472 Lisp_Object vector;
7474 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7475 Vdeactivate_mark = AREF (vector, 1);
7476 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7478 if (WINDOWP (AREF (vector, 3)))
7480 struct window *w;
7481 Lisp_Object buffer, charpos, bytepos;
7483 w = XWINDOW (AREF (vector, 3));
7484 buffer = AREF (vector, 4);
7485 charpos = AREF (vector, 5);
7486 bytepos = AREF (vector, 6);
7488 w->buffer = buffer;
7489 set_marker_both (w->pointm, buffer,
7490 XFASTINT (charpos), XFASTINT (bytepos));
7493 Vwith_echo_area_save_vector = vector;
7494 return Qnil;
7498 /* Set up the echo area for use by print functions. MULTIBYTE_P
7499 non-zero means we will print multibyte. */
7501 void
7502 setup_echo_area_for_printing (multibyte_p)
7503 int multibyte_p;
7505 /* If we can't find an echo area any more, exit. */
7506 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7507 Fkill_emacs (Qnil);
7509 ensure_echo_area_buffers ();
7511 if (!message_buf_print)
7513 /* A message has been output since the last time we printed.
7514 Choose a fresh echo area buffer. */
7515 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7516 echo_area_buffer[0] = echo_buffer[1];
7517 else
7518 echo_area_buffer[0] = echo_buffer[0];
7520 /* Switch to that buffer and clear it. */
7521 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7522 current_buffer->truncate_lines = Qnil;
7524 if (Z > BEG)
7526 int count = SPECPDL_INDEX ();
7527 specbind (Qinhibit_read_only, Qt);
7528 /* Note that undo recording is always disabled. */
7529 del_range (BEG, Z);
7530 unbind_to (count, Qnil);
7532 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7534 /* Set up the buffer for the multibyteness we need. */
7535 if (multibyte_p
7536 != !NILP (current_buffer->enable_multibyte_characters))
7537 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7539 /* Raise the frame containing the echo area. */
7540 if (minibuffer_auto_raise)
7542 struct frame *sf = SELECTED_FRAME ();
7543 Lisp_Object mini_window;
7544 mini_window = FRAME_MINIBUF_WINDOW (sf);
7545 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7548 message_log_maybe_newline ();
7549 message_buf_print = 1;
7551 else
7553 if (NILP (echo_area_buffer[0]))
7555 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7556 echo_area_buffer[0] = echo_buffer[1];
7557 else
7558 echo_area_buffer[0] = echo_buffer[0];
7561 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7563 /* Someone switched buffers between print requests. */
7564 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7565 current_buffer->truncate_lines = Qnil;
7571 /* Display an echo area message in window W. Value is non-zero if W's
7572 height is changed. If display_last_displayed_message_p is
7573 non-zero, display the message that was last displayed, otherwise
7574 display the current message. */
7576 static int
7577 display_echo_area (w)
7578 struct window *w;
7580 int i, no_message_p, window_height_changed_p, count;
7582 /* Temporarily disable garbage collections while displaying the echo
7583 area. This is done because a GC can print a message itself.
7584 That message would modify the echo area buffer's contents while a
7585 redisplay of the buffer is going on, and seriously confuse
7586 redisplay. */
7587 count = inhibit_garbage_collection ();
7589 /* If there is no message, we must call display_echo_area_1
7590 nevertheless because it resizes the window. But we will have to
7591 reset the echo_area_buffer in question to nil at the end because
7592 with_echo_area_buffer will sets it to an empty buffer. */
7593 i = display_last_displayed_message_p ? 1 : 0;
7594 no_message_p = NILP (echo_area_buffer[i]);
7596 window_height_changed_p
7597 = with_echo_area_buffer (w, display_last_displayed_message_p,
7598 display_echo_area_1,
7599 (EMACS_INT) w, Qnil, 0, 0);
7601 if (no_message_p)
7602 echo_area_buffer[i] = Qnil;
7604 unbind_to (count, Qnil);
7605 return window_height_changed_p;
7609 /* Helper for display_echo_area. Display the current buffer which
7610 contains the current echo area message in window W, a mini-window,
7611 a pointer to which is passed in A1. A2..A4 are currently not used.
7612 Change the height of W so that all of the message is displayed.
7613 Value is non-zero if height of W was changed. */
7615 static int
7616 display_echo_area_1 (a1, a2, a3, a4)
7617 EMACS_INT a1;
7618 Lisp_Object a2;
7619 EMACS_INT a3, a4;
7621 struct window *w = (struct window *) a1;
7622 Lisp_Object window;
7623 struct text_pos start;
7624 int window_height_changed_p = 0;
7626 /* Do this before displaying, so that we have a large enough glyph
7627 matrix for the display. */
7628 window_height_changed_p = resize_mini_window (w, 0);
7630 /* Display. */
7631 clear_glyph_matrix (w->desired_matrix);
7632 XSETWINDOW (window, w);
7633 SET_TEXT_POS (start, BEG, BEG_BYTE);
7634 try_window (window, start, 0);
7636 return window_height_changed_p;
7640 /* Resize the echo area window to exactly the size needed for the
7641 currently displayed message, if there is one. If a mini-buffer
7642 is active, don't shrink it. */
7644 void
7645 resize_echo_area_exactly ()
7647 if (BUFFERP (echo_area_buffer[0])
7648 && WINDOWP (echo_area_window))
7650 struct window *w = XWINDOW (echo_area_window);
7651 int resized_p;
7652 Lisp_Object resize_exactly;
7654 if (minibuf_level == 0)
7655 resize_exactly = Qt;
7656 else
7657 resize_exactly = Qnil;
7659 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7660 (EMACS_INT) w, resize_exactly, 0, 0);
7661 if (resized_p)
7663 ++windows_or_buffers_changed;
7664 ++update_mode_lines;
7665 redisplay_internal (0);
7671 /* Callback function for with_echo_area_buffer, when used from
7672 resize_echo_area_exactly. A1 contains a pointer to the window to
7673 resize, EXACTLY non-nil means resize the mini-window exactly to the
7674 size of the text displayed. A3 and A4 are not used. Value is what
7675 resize_mini_window returns. */
7677 static int
7678 resize_mini_window_1 (a1, exactly, a3, a4)
7679 EMACS_INT a1;
7680 Lisp_Object exactly;
7681 EMACS_INT a3, a4;
7683 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7687 /* Resize mini-window W to fit the size of its contents. EXACT:P
7688 means size the window exactly to the size needed. Otherwise, it's
7689 only enlarged until W's buffer is empty. Value is non-zero if
7690 the window height has been changed. */
7693 resize_mini_window (w, exact_p)
7694 struct window *w;
7695 int exact_p;
7697 struct frame *f = XFRAME (w->frame);
7698 int window_height_changed_p = 0;
7700 xassert (MINI_WINDOW_P (w));
7702 /* Don't resize windows while redisplaying a window; it would
7703 confuse redisplay functions when the size of the window they are
7704 displaying changes from under them. Such a resizing can happen,
7705 for instance, when which-func prints a long message while
7706 we are running fontification-functions. We're running these
7707 functions with safe_call which binds inhibit-redisplay to t. */
7708 if (!NILP (Vinhibit_redisplay))
7709 return 0;
7711 /* Nil means don't try to resize. */
7712 if (NILP (Vresize_mini_windows)
7713 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7714 return 0;
7716 if (!FRAME_MINIBUF_ONLY_P (f))
7718 struct it it;
7719 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7720 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7721 int height, max_height;
7722 int unit = FRAME_LINE_HEIGHT (f);
7723 struct text_pos start;
7724 struct buffer *old_current_buffer = NULL;
7726 if (current_buffer != XBUFFER (w->buffer))
7728 old_current_buffer = current_buffer;
7729 set_buffer_internal (XBUFFER (w->buffer));
7732 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7734 /* Compute the max. number of lines specified by the user. */
7735 if (FLOATP (Vmax_mini_window_height))
7736 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7737 else if (INTEGERP (Vmax_mini_window_height))
7738 max_height = XINT (Vmax_mini_window_height);
7739 else
7740 max_height = total_height / 4;
7742 /* Correct that max. height if it's bogus. */
7743 max_height = max (1, max_height);
7744 max_height = min (total_height, max_height);
7746 /* Find out the height of the text in the window. */
7747 if (it.truncate_lines_p)
7748 height = 1;
7749 else
7751 last_height = 0;
7752 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7753 if (it.max_ascent == 0 && it.max_descent == 0)
7754 height = it.current_y + last_height;
7755 else
7756 height = it.current_y + it.max_ascent + it.max_descent;
7757 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7758 height = (height + unit - 1) / unit;
7761 /* Compute a suitable window start. */
7762 if (height > max_height)
7764 height = max_height;
7765 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7766 move_it_vertically_backward (&it, (height - 1) * unit);
7767 start = it.current.pos;
7769 else
7770 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7771 SET_MARKER_FROM_TEXT_POS (w->start, start);
7773 if (EQ (Vresize_mini_windows, Qgrow_only))
7775 /* Let it grow only, until we display an empty message, in which
7776 case the window shrinks again. */
7777 if (height > WINDOW_TOTAL_LINES (w))
7779 int old_height = WINDOW_TOTAL_LINES (w);
7780 freeze_window_starts (f, 1);
7781 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7782 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7784 else if (height < WINDOW_TOTAL_LINES (w)
7785 && (exact_p || BEGV == ZV))
7787 int old_height = WINDOW_TOTAL_LINES (w);
7788 freeze_window_starts (f, 0);
7789 shrink_mini_window (w);
7790 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7793 else
7795 /* Always resize to exact size needed. */
7796 if (height > WINDOW_TOTAL_LINES (w))
7798 int old_height = WINDOW_TOTAL_LINES (w);
7799 freeze_window_starts (f, 1);
7800 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7801 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7803 else if (height < WINDOW_TOTAL_LINES (w))
7805 int old_height = WINDOW_TOTAL_LINES (w);
7806 freeze_window_starts (f, 0);
7807 shrink_mini_window (w);
7809 if (height)
7811 freeze_window_starts (f, 1);
7812 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7815 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7819 if (old_current_buffer)
7820 set_buffer_internal (old_current_buffer);
7823 return window_height_changed_p;
7827 /* Value is the current message, a string, or nil if there is no
7828 current message. */
7830 Lisp_Object
7831 current_message ()
7833 Lisp_Object msg;
7835 if (NILP (echo_area_buffer[0]))
7836 msg = Qnil;
7837 else
7839 with_echo_area_buffer (0, 0, current_message_1,
7840 (EMACS_INT) &msg, Qnil, 0, 0);
7841 if (NILP (msg))
7842 echo_area_buffer[0] = Qnil;
7845 return msg;
7849 static int
7850 current_message_1 (a1, a2, a3, a4)
7851 EMACS_INT a1;
7852 Lisp_Object a2;
7853 EMACS_INT a3, a4;
7855 Lisp_Object *msg = (Lisp_Object *) a1;
7857 if (Z > BEG)
7858 *msg = make_buffer_string (BEG, Z, 1);
7859 else
7860 *msg = Qnil;
7861 return 0;
7865 /* Push the current message on Vmessage_stack for later restauration
7866 by restore_message. Value is non-zero if the current message isn't
7867 empty. This is a relatively infrequent operation, so it's not
7868 worth optimizing. */
7871 push_message ()
7873 Lisp_Object msg;
7874 msg = current_message ();
7875 Vmessage_stack = Fcons (msg, Vmessage_stack);
7876 return STRINGP (msg);
7880 /* Restore message display from the top of Vmessage_stack. */
7882 void
7883 restore_message ()
7885 Lisp_Object msg;
7887 xassert (CONSP (Vmessage_stack));
7888 msg = XCAR (Vmessage_stack);
7889 if (STRINGP (msg))
7890 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7891 else
7892 message3_nolog (msg, 0, 0);
7896 /* Handler for record_unwind_protect calling pop_message. */
7898 Lisp_Object
7899 pop_message_unwind (dummy)
7900 Lisp_Object dummy;
7902 pop_message ();
7903 return Qnil;
7906 /* Pop the top-most entry off Vmessage_stack. */
7908 void
7909 pop_message ()
7911 xassert (CONSP (Vmessage_stack));
7912 Vmessage_stack = XCDR (Vmessage_stack);
7916 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7917 exits. If the stack is not empty, we have a missing pop_message
7918 somewhere. */
7920 void
7921 check_message_stack ()
7923 if (!NILP (Vmessage_stack))
7924 abort ();
7928 /* Truncate to NCHARS what will be displayed in the echo area the next
7929 time we display it---but don't redisplay it now. */
7931 void
7932 truncate_echo_area (nchars)
7933 int nchars;
7935 if (nchars == 0)
7936 echo_area_buffer[0] = Qnil;
7937 /* A null message buffer means that the frame hasn't really been
7938 initialized yet. Error messages get reported properly by
7939 cmd_error, so this must be just an informative message; toss it. */
7940 else if (!noninteractive
7941 && INTERACTIVE
7942 && !NILP (echo_area_buffer[0]))
7944 struct frame *sf = SELECTED_FRAME ();
7945 if (FRAME_MESSAGE_BUF (sf))
7946 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7951 /* Helper function for truncate_echo_area. Truncate the current
7952 message to at most NCHARS characters. */
7954 static int
7955 truncate_message_1 (nchars, a2, a3, a4)
7956 EMACS_INT nchars;
7957 Lisp_Object a2;
7958 EMACS_INT a3, a4;
7960 if (BEG + nchars < Z)
7961 del_range (BEG + nchars, Z);
7962 if (Z == BEG)
7963 echo_area_buffer[0] = Qnil;
7964 return 0;
7968 /* Set the current message to a substring of S or STRING.
7970 If STRING is a Lisp string, set the message to the first NBYTES
7971 bytes from STRING. NBYTES zero means use the whole string. If
7972 STRING is multibyte, the message will be displayed multibyte.
7974 If S is not null, set the message to the first LEN bytes of S. LEN
7975 zero means use the whole string. MULTIBYTE_P non-zero means S is
7976 multibyte. Display the message multibyte in that case. */
7978 void
7979 set_message (s, string, nbytes, multibyte_p)
7980 const char *s;
7981 Lisp_Object string;
7982 int nbytes, multibyte_p;
7984 message_enable_multibyte
7985 = ((s && multibyte_p)
7986 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7988 with_echo_area_buffer (0, 0, set_message_1,
7989 (EMACS_INT) s, string, nbytes, multibyte_p);
7990 message_buf_print = 0;
7991 help_echo_showing_p = 0;
7995 /* Helper function for set_message. Arguments have the same meaning
7996 as there, with A1 corresponding to S and A2 corresponding to STRING
7997 This function is called with the echo area buffer being
7998 current. */
8000 static int
8001 set_message_1 (a1, a2, nbytes, multibyte_p)
8002 EMACS_INT a1;
8003 Lisp_Object a2;
8004 EMACS_INT nbytes, multibyte_p;
8006 const char *s = (const char *) a1;
8007 Lisp_Object string = a2;
8009 /* Change multibyteness of the echo buffer appropriately. */
8010 if (message_enable_multibyte
8011 != !NILP (current_buffer->enable_multibyte_characters))
8012 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8014 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8016 /* Insert new message at BEG. */
8017 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8018 Ferase_buffer ();
8020 if (STRINGP (string))
8022 int nchars;
8024 if (nbytes == 0)
8025 nbytes = SBYTES (string);
8026 nchars = string_byte_to_char (string, nbytes);
8028 /* This function takes care of single/multibyte conversion. We
8029 just have to ensure that the echo area buffer has the right
8030 setting of enable_multibyte_characters. */
8031 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8033 else if (s)
8035 if (nbytes == 0)
8036 nbytes = strlen (s);
8038 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8040 /* Convert from multi-byte to single-byte. */
8041 int i, c, n;
8042 unsigned char work[1];
8044 /* Convert a multibyte string to single-byte. */
8045 for (i = 0; i < nbytes; i += n)
8047 c = string_char_and_length (s + i, nbytes - i, &n);
8048 work[0] = (SINGLE_BYTE_CHAR_P (c)
8050 : multibyte_char_to_unibyte (c, Qnil));
8051 insert_1_both (work, 1, 1, 1, 0, 0);
8054 else if (!multibyte_p
8055 && !NILP (current_buffer->enable_multibyte_characters))
8057 /* Convert from single-byte to multi-byte. */
8058 int i, c, n;
8059 const unsigned char *msg = (const unsigned char *) s;
8060 unsigned char str[MAX_MULTIBYTE_LENGTH];
8062 /* Convert a single-byte string to multibyte. */
8063 for (i = 0; i < nbytes; i++)
8065 c = unibyte_char_to_multibyte (msg[i]);
8066 n = CHAR_STRING (c, str);
8067 insert_1_both (str, 1, n, 1, 0, 0);
8070 else
8071 insert_1 (s, nbytes, 1, 0, 0);
8074 return 0;
8078 /* Clear messages. CURRENT_P non-zero means clear the current
8079 message. LAST_DISPLAYED_P non-zero means clear the message
8080 last displayed. */
8082 void
8083 clear_message (current_p, last_displayed_p)
8084 int current_p, last_displayed_p;
8086 if (current_p)
8088 echo_area_buffer[0] = Qnil;
8089 message_cleared_p = 1;
8092 if (last_displayed_p)
8093 echo_area_buffer[1] = Qnil;
8095 message_buf_print = 0;
8098 /* Clear garbaged frames.
8100 This function is used where the old redisplay called
8101 redraw_garbaged_frames which in turn called redraw_frame which in
8102 turn called clear_frame. The call to clear_frame was a source of
8103 flickering. I believe a clear_frame is not necessary. It should
8104 suffice in the new redisplay to invalidate all current matrices,
8105 and ensure a complete redisplay of all windows. */
8107 static void
8108 clear_garbaged_frames ()
8110 if (frame_garbaged)
8112 Lisp_Object tail, frame;
8113 int changed_count = 0;
8115 FOR_EACH_FRAME (tail, frame)
8117 struct frame *f = XFRAME (frame);
8119 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8121 if (f->resized_p)
8123 Fredraw_frame (frame);
8124 f->force_flush_display_p = 1;
8126 clear_current_matrices (f);
8127 changed_count++;
8128 f->garbaged = 0;
8129 f->resized_p = 0;
8133 frame_garbaged = 0;
8134 if (changed_count)
8135 ++windows_or_buffers_changed;
8140 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8141 is non-zero update selected_frame. Value is non-zero if the
8142 mini-windows height has been changed. */
8144 static int
8145 echo_area_display (update_frame_p)
8146 int update_frame_p;
8148 Lisp_Object mini_window;
8149 struct window *w;
8150 struct frame *f;
8151 int window_height_changed_p = 0;
8152 struct frame *sf = SELECTED_FRAME ();
8154 mini_window = FRAME_MINIBUF_WINDOW (sf);
8155 w = XWINDOW (mini_window);
8156 f = XFRAME (WINDOW_FRAME (w));
8158 /* Don't display if frame is invisible or not yet initialized. */
8159 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8160 return 0;
8162 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8163 #ifndef MAC_OS8
8164 #ifdef HAVE_WINDOW_SYSTEM
8165 /* When Emacs starts, selected_frame may be a visible terminal
8166 frame, even if we run under a window system. If we let this
8167 through, a message would be displayed on the terminal. */
8168 if (EQ (selected_frame, Vterminal_frame)
8169 && !NILP (Vwindow_system))
8170 return 0;
8171 #endif /* HAVE_WINDOW_SYSTEM */
8172 #endif
8174 /* Redraw garbaged frames. */
8175 if (frame_garbaged)
8176 clear_garbaged_frames ();
8178 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8180 echo_area_window = mini_window;
8181 window_height_changed_p = display_echo_area (w);
8182 w->must_be_updated_p = 1;
8184 /* Update the display, unless called from redisplay_internal.
8185 Also don't update the screen during redisplay itself. The
8186 update will happen at the end of redisplay, and an update
8187 here could cause confusion. */
8188 if (update_frame_p && !redisplaying_p)
8190 int n = 0;
8192 /* If the display update has been interrupted by pending
8193 input, update mode lines in the frame. Due to the
8194 pending input, it might have been that redisplay hasn't
8195 been called, so that mode lines above the echo area are
8196 garbaged. This looks odd, so we prevent it here. */
8197 if (!display_completed)
8198 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8200 if (window_height_changed_p
8201 /* Don't do this if Emacs is shutting down. Redisplay
8202 needs to run hooks. */
8203 && !NILP (Vrun_hooks))
8205 /* Must update other windows. Likewise as in other
8206 cases, don't let this update be interrupted by
8207 pending input. */
8208 int count = SPECPDL_INDEX ();
8209 specbind (Qredisplay_dont_pause, Qt);
8210 windows_or_buffers_changed = 1;
8211 redisplay_internal (0);
8212 unbind_to (count, Qnil);
8214 else if (FRAME_WINDOW_P (f) && n == 0)
8216 /* Window configuration is the same as before.
8217 Can do with a display update of the echo area,
8218 unless we displayed some mode lines. */
8219 update_single_window (w, 1);
8220 rif->flush_display (f);
8222 else
8223 update_frame (f, 1, 1);
8225 /* If cursor is in the echo area, make sure that the next
8226 redisplay displays the minibuffer, so that the cursor will
8227 be replaced with what the minibuffer wants. */
8228 if (cursor_in_echo_area)
8229 ++windows_or_buffers_changed;
8232 else if (!EQ (mini_window, selected_window))
8233 windows_or_buffers_changed++;
8235 /* The current message is now also the last one displayed. */
8236 echo_area_buffer[1] = echo_area_buffer[0];
8238 /* Prevent redisplay optimization in redisplay_internal by resetting
8239 this_line_start_pos. This is done because the mini-buffer now
8240 displays the message instead of its buffer text. */
8241 if (EQ (mini_window, selected_window))
8242 CHARPOS (this_line_start_pos) = 0;
8244 return window_height_changed_p;
8249 /***********************************************************************
8250 Mode Lines and Frame Titles
8251 ***********************************************************************/
8253 /* A buffer for constructing non-propertized mode-line strings and
8254 frame titles in it; allocated from the heap in init_xdisp and
8255 resized as needed in store_mode_line_noprop_char. */
8257 static char *mode_line_noprop_buf;
8259 /* The buffer's end, and a current output position in it. */
8261 static char *mode_line_noprop_buf_end;
8262 static char *mode_line_noprop_ptr;
8264 #define MODE_LINE_NOPROP_LEN(start) \
8265 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8267 static enum {
8268 MODE_LINE_DISPLAY = 0,
8269 MODE_LINE_TITLE,
8270 MODE_LINE_NOPROP,
8271 MODE_LINE_STRING
8272 } mode_line_target;
8274 /* Alist that caches the results of :propertize.
8275 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8276 static Lisp_Object mode_line_proptrans_alist;
8278 /* List of strings making up the mode-line. */
8279 static Lisp_Object mode_line_string_list;
8281 /* Base face property when building propertized mode line string. */
8282 static Lisp_Object mode_line_string_face;
8283 static Lisp_Object mode_line_string_face_prop;
8286 /* Unwind data for mode line strings */
8288 static Lisp_Object Vmode_line_unwind_vector;
8290 static Lisp_Object
8291 format_mode_line_unwind_data (obuf)
8292 struct buffer *obuf;
8294 Lisp_Object vector;
8296 /* Reduce consing by keeping one vector in
8297 Vwith_echo_area_save_vector. */
8298 vector = Vmode_line_unwind_vector;
8299 Vmode_line_unwind_vector = Qnil;
8301 if (NILP (vector))
8302 vector = Fmake_vector (make_number (7), Qnil);
8304 AREF (vector, 0) = make_number (mode_line_target);
8305 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8306 AREF (vector, 2) = mode_line_string_list;
8307 AREF (vector, 3) = mode_line_proptrans_alist;
8308 AREF (vector, 4) = mode_line_string_face;
8309 AREF (vector, 5) = mode_line_string_face_prop;
8311 if (obuf)
8312 XSETBUFFER (AREF (vector, 6), obuf);
8313 else
8314 AREF (vector, 6) = Qnil;
8316 return vector;
8319 static Lisp_Object
8320 unwind_format_mode_line (vector)
8321 Lisp_Object vector;
8323 mode_line_target = XINT (AREF (vector, 0));
8324 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8325 mode_line_string_list = AREF (vector, 2);
8326 mode_line_proptrans_alist = AREF (vector, 3);
8327 mode_line_string_face = AREF (vector, 4);
8328 mode_line_string_face_prop = AREF (vector, 5);
8330 if (!NILP (AREF (vector, 6)))
8332 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8333 AREF (vector, 6) = Qnil;
8336 Vmode_line_unwind_vector = vector;
8337 return Qnil;
8341 /* Store a single character C for the frame title in mode_line_noprop_buf.
8342 Re-allocate mode_line_noprop_buf if necessary. */
8344 static void
8345 #ifdef PROTOTYPES
8346 store_mode_line_noprop_char (char c)
8347 #else
8348 store_mode_line_noprop_char (c)
8349 char c;
8350 #endif
8352 /* If output position has reached the end of the allocated buffer,
8353 double the buffer's size. */
8354 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8356 int len = MODE_LINE_NOPROP_LEN (0);
8357 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8358 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8359 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8360 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8363 *mode_line_noprop_ptr++ = c;
8367 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8368 mode_line_noprop_ptr. STR is the string to store. Do not copy
8369 characters that yield more columns than PRECISION; PRECISION <= 0
8370 means copy the whole string. Pad with spaces until FIELD_WIDTH
8371 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8372 pad. Called from display_mode_element when it is used to build a
8373 frame title. */
8375 static int
8376 store_mode_line_noprop (str, field_width, precision)
8377 const unsigned char *str;
8378 int field_width, precision;
8380 int n = 0;
8381 int dummy, nbytes;
8383 /* Copy at most PRECISION chars from STR. */
8384 nbytes = strlen (str);
8385 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8386 while (nbytes--)
8387 store_mode_line_noprop_char (*str++);
8389 /* Fill up with spaces until FIELD_WIDTH reached. */
8390 while (field_width > 0
8391 && n < field_width)
8393 store_mode_line_noprop_char (' ');
8394 ++n;
8397 return n;
8400 /***********************************************************************
8401 Frame Titles
8402 ***********************************************************************/
8404 #ifdef HAVE_WINDOW_SYSTEM
8406 /* Set the title of FRAME, if it has changed. The title format is
8407 Vicon_title_format if FRAME is iconified, otherwise it is
8408 frame_title_format. */
8410 static void
8411 x_consider_frame_title (frame)
8412 Lisp_Object frame;
8414 struct frame *f = XFRAME (frame);
8416 if (FRAME_WINDOW_P (f)
8417 || FRAME_MINIBUF_ONLY_P (f)
8418 || f->explicit_name)
8420 /* Do we have more than one visible frame on this X display? */
8421 Lisp_Object tail;
8422 Lisp_Object fmt;
8423 int title_start;
8424 char *title;
8425 int len;
8426 struct it it;
8427 int count = SPECPDL_INDEX ();
8429 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8431 Lisp_Object other_frame = XCAR (tail);
8432 struct frame *tf = XFRAME (other_frame);
8434 if (tf != f
8435 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8436 && !FRAME_MINIBUF_ONLY_P (tf)
8437 && !EQ (other_frame, tip_frame)
8438 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8439 break;
8442 /* Set global variable indicating that multiple frames exist. */
8443 multiple_frames = CONSP (tail);
8445 /* Switch to the buffer of selected window of the frame. Set up
8446 mode_line_target so that display_mode_element will output into
8447 mode_line_noprop_buf; then display the title. */
8448 record_unwind_protect (unwind_format_mode_line,
8449 format_mode_line_unwind_data (current_buffer));
8451 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8452 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8454 mode_line_target = MODE_LINE_TITLE;
8455 title_start = MODE_LINE_NOPROP_LEN (0);
8456 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8457 NULL, DEFAULT_FACE_ID);
8458 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8459 len = MODE_LINE_NOPROP_LEN (title_start);
8460 title = mode_line_noprop_buf + title_start;
8461 unbind_to (count, Qnil);
8463 /* Set the title only if it's changed. This avoids consing in
8464 the common case where it hasn't. (If it turns out that we've
8465 already wasted too much time by walking through the list with
8466 display_mode_element, then we might need to optimize at a
8467 higher level than this.) */
8468 if (! STRINGP (f->name)
8469 || SBYTES (f->name) != len
8470 || bcmp (title, SDATA (f->name), len) != 0)
8471 x_implicitly_set_name (f, make_string (title, len), Qnil);
8475 #endif /* not HAVE_WINDOW_SYSTEM */
8480 /***********************************************************************
8481 Menu Bars
8482 ***********************************************************************/
8485 /* Prepare for redisplay by updating menu-bar item lists when
8486 appropriate. This can call eval. */
8488 void
8489 prepare_menu_bars ()
8491 int all_windows;
8492 struct gcpro gcpro1, gcpro2;
8493 struct frame *f;
8494 Lisp_Object tooltip_frame;
8496 #ifdef HAVE_WINDOW_SYSTEM
8497 tooltip_frame = tip_frame;
8498 #else
8499 tooltip_frame = Qnil;
8500 #endif
8502 /* Update all frame titles based on their buffer names, etc. We do
8503 this before the menu bars so that the buffer-menu will show the
8504 up-to-date frame titles. */
8505 #ifdef HAVE_WINDOW_SYSTEM
8506 if (windows_or_buffers_changed || update_mode_lines)
8508 Lisp_Object tail, frame;
8510 FOR_EACH_FRAME (tail, frame)
8512 f = XFRAME (frame);
8513 if (!EQ (frame, tooltip_frame)
8514 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8515 x_consider_frame_title (frame);
8518 #endif /* HAVE_WINDOW_SYSTEM */
8520 /* Update the menu bar item lists, if appropriate. This has to be
8521 done before any actual redisplay or generation of display lines. */
8522 all_windows = (update_mode_lines
8523 || buffer_shared > 1
8524 || windows_or_buffers_changed);
8525 if (all_windows)
8527 Lisp_Object tail, frame;
8528 int count = SPECPDL_INDEX ();
8530 record_unwind_save_match_data ();
8532 FOR_EACH_FRAME (tail, frame)
8534 f = XFRAME (frame);
8536 /* Ignore tooltip frame. */
8537 if (EQ (frame, tooltip_frame))
8538 continue;
8540 /* If a window on this frame changed size, report that to
8541 the user and clear the size-change flag. */
8542 if (FRAME_WINDOW_SIZES_CHANGED (f))
8544 Lisp_Object functions;
8546 /* Clear flag first in case we get an error below. */
8547 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8548 functions = Vwindow_size_change_functions;
8549 GCPRO2 (tail, functions);
8551 while (CONSP (functions))
8553 call1 (XCAR (functions), frame);
8554 functions = XCDR (functions);
8556 UNGCPRO;
8559 GCPRO1 (tail);
8560 update_menu_bar (f, 0);
8561 #ifdef HAVE_WINDOW_SYSTEM
8562 update_tool_bar (f, 0);
8563 #endif
8564 UNGCPRO;
8567 unbind_to (count, Qnil);
8569 else
8571 struct frame *sf = SELECTED_FRAME ();
8572 update_menu_bar (sf, 1);
8573 #ifdef HAVE_WINDOW_SYSTEM
8574 update_tool_bar (sf, 1);
8575 #endif
8578 /* Motif needs this. See comment in xmenu.c. Turn it off when
8579 pending_menu_activation is not defined. */
8580 #ifdef USE_X_TOOLKIT
8581 pending_menu_activation = 0;
8582 #endif
8586 /* Update the menu bar item list for frame F. This has to be done
8587 before we start to fill in any display lines, because it can call
8588 eval.
8590 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8592 static void
8593 update_menu_bar (f, save_match_data)
8594 struct frame *f;
8595 int save_match_data;
8597 Lisp_Object window;
8598 register struct window *w;
8600 /* If called recursively during a menu update, do nothing. This can
8601 happen when, for instance, an activate-menubar-hook causes a
8602 redisplay. */
8603 if (inhibit_menubar_update)
8604 return;
8606 window = FRAME_SELECTED_WINDOW (f);
8607 w = XWINDOW (window);
8609 #if 0 /* The if statement below this if statement used to include the
8610 condition !NILP (w->update_mode_line), rather than using
8611 update_mode_lines directly, and this if statement may have
8612 been added to make that condition work. Now the if
8613 statement below matches its comment, this isn't needed. */
8614 if (update_mode_lines)
8615 w->update_mode_line = Qt;
8616 #endif
8618 if (FRAME_WINDOW_P (f)
8620 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8621 || defined (USE_GTK)
8622 FRAME_EXTERNAL_MENU_BAR (f)
8623 #else
8624 FRAME_MENU_BAR_LINES (f) > 0
8625 #endif
8626 : FRAME_MENU_BAR_LINES (f) > 0)
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 /* This used to test w->update_mode_line, but we believe
8637 there is no need to recompute the menu in that case. */
8638 || update_mode_lines
8639 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8640 < BUF_MODIFF (XBUFFER (w->buffer)))
8641 != !NILP (w->last_had_star))
8642 || ((!NILP (Vtransient_mark_mode)
8643 && !NILP (XBUFFER (w->buffer)->mark_active))
8644 != !NILP (w->region_showing)))
8646 struct buffer *prev = current_buffer;
8647 int count = SPECPDL_INDEX ();
8649 specbind (Qinhibit_menubar_update, Qt);
8651 set_buffer_internal_1 (XBUFFER (w->buffer));
8652 if (save_match_data)
8653 record_unwind_save_match_data ();
8654 if (NILP (Voverriding_local_map_menu_flag))
8656 specbind (Qoverriding_terminal_local_map, Qnil);
8657 specbind (Qoverriding_local_map, Qnil);
8660 /* Run the Lucid hook. */
8661 safe_run_hooks (Qactivate_menubar_hook);
8663 /* If it has changed current-menubar from previous value,
8664 really recompute the menu-bar from the value. */
8665 if (! NILP (Vlucid_menu_bar_dirty_flag))
8666 call0 (Qrecompute_lucid_menubar);
8668 safe_run_hooks (Qmenu_bar_update_hook);
8669 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8671 /* Redisplay the menu bar in case we changed it. */
8672 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8673 || defined (USE_GTK)
8674 if (FRAME_WINDOW_P (f)
8675 #if defined (MAC_OS)
8676 /* All frames on Mac OS share the same menubar. So only the
8677 selected frame should be allowed to set it. */
8678 && f == SELECTED_FRAME ()
8679 #endif
8681 set_frame_menubar (f, 0, 0);
8682 else
8683 /* On a terminal screen, the menu bar is an ordinary screen
8684 line, and this makes it get updated. */
8685 w->update_mode_line = Qt;
8686 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8687 /* In the non-toolkit version, the menu bar is an ordinary screen
8688 line, and this makes it get updated. */
8689 w->update_mode_line = Qt;
8690 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8692 unbind_to (count, Qnil);
8693 set_buffer_internal_1 (prev);
8700 /***********************************************************************
8701 Output Cursor
8702 ***********************************************************************/
8704 #ifdef HAVE_WINDOW_SYSTEM
8706 /* EXPORT:
8707 Nominal cursor position -- where to draw output.
8708 HPOS and VPOS are window relative glyph matrix coordinates.
8709 X and Y are window relative pixel coordinates. */
8711 struct cursor_pos output_cursor;
8714 /* EXPORT:
8715 Set the global variable output_cursor to CURSOR. All cursor
8716 positions are relative to updated_window. */
8718 void
8719 set_output_cursor (cursor)
8720 struct cursor_pos *cursor;
8722 output_cursor.hpos = cursor->hpos;
8723 output_cursor.vpos = cursor->vpos;
8724 output_cursor.x = cursor->x;
8725 output_cursor.y = cursor->y;
8729 /* EXPORT for RIF:
8730 Set a nominal cursor position.
8732 HPOS and VPOS are column/row positions in a window glyph matrix. X
8733 and Y are window text area relative pixel positions.
8735 If this is done during an update, updated_window will contain the
8736 window that is being updated and the position is the future output
8737 cursor position for that window. If updated_window is null, use
8738 selected_window and display the cursor at the given position. */
8740 void
8741 x_cursor_to (vpos, hpos, y, x)
8742 int vpos, hpos, y, x;
8744 struct window *w;
8746 /* If updated_window is not set, work on selected_window. */
8747 if (updated_window)
8748 w = updated_window;
8749 else
8750 w = XWINDOW (selected_window);
8752 /* Set the output cursor. */
8753 output_cursor.hpos = hpos;
8754 output_cursor.vpos = vpos;
8755 output_cursor.x = x;
8756 output_cursor.y = y;
8758 /* If not called as part of an update, really display the cursor.
8759 This will also set the cursor position of W. */
8760 if (updated_window == NULL)
8762 BLOCK_INPUT;
8763 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8764 if (rif->flush_display_optional)
8765 rif->flush_display_optional (SELECTED_FRAME ());
8766 UNBLOCK_INPUT;
8770 #endif /* HAVE_WINDOW_SYSTEM */
8773 /***********************************************************************
8774 Tool-bars
8775 ***********************************************************************/
8777 #ifdef HAVE_WINDOW_SYSTEM
8779 /* Where the mouse was last time we reported a mouse event. */
8781 FRAME_PTR last_mouse_frame;
8783 /* Tool-bar item index of the item on which a mouse button was pressed
8784 or -1. */
8786 int last_tool_bar_item;
8789 /* Update the tool-bar item list for frame F. This has to be done
8790 before we start to fill in any display lines. Called from
8791 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8792 and restore it here. */
8794 static void
8795 update_tool_bar (f, save_match_data)
8796 struct frame *f;
8797 int save_match_data;
8799 #ifdef USE_GTK
8800 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8801 #else
8802 int do_update = WINDOWP (f->tool_bar_window)
8803 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8804 #endif
8806 if (do_update)
8808 Lisp_Object window;
8809 struct window *w;
8811 window = FRAME_SELECTED_WINDOW (f);
8812 w = XWINDOW (window);
8814 /* If the user has switched buffers or windows, we need to
8815 recompute to reflect the new bindings. But we'll
8816 recompute when update_mode_lines is set too; that means
8817 that people can use force-mode-line-update to request
8818 that the menu bar be recomputed. The adverse effect on
8819 the rest of the redisplay algorithm is about the same as
8820 windows_or_buffers_changed anyway. */
8821 if (windows_or_buffers_changed
8822 || !NILP (w->update_mode_line)
8823 || update_mode_lines
8824 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8825 < BUF_MODIFF (XBUFFER (w->buffer)))
8826 != !NILP (w->last_had_star))
8827 || ((!NILP (Vtransient_mark_mode)
8828 && !NILP (XBUFFER (w->buffer)->mark_active))
8829 != !NILP (w->region_showing)))
8831 struct buffer *prev = current_buffer;
8832 int count = SPECPDL_INDEX ();
8833 Lisp_Object new_tool_bar;
8834 int new_n_tool_bar;
8835 struct gcpro gcpro1;
8837 /* Set current_buffer to the buffer of the selected
8838 window of the frame, so that we get the right local
8839 keymaps. */
8840 set_buffer_internal_1 (XBUFFER (w->buffer));
8842 /* Save match data, if we must. */
8843 if (save_match_data)
8844 record_unwind_save_match_data ();
8846 /* Make sure that we don't accidentally use bogus keymaps. */
8847 if (NILP (Voverriding_local_map_menu_flag))
8849 specbind (Qoverriding_terminal_local_map, Qnil);
8850 specbind (Qoverriding_local_map, Qnil);
8853 GCPRO1 (new_tool_bar);
8855 /* Build desired tool-bar items from keymaps. */
8856 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8857 &new_n_tool_bar);
8859 /* Redisplay the tool-bar if we changed it. */
8860 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8862 /* Redisplay that happens asynchronously due to an expose event
8863 may access f->tool_bar_items. Make sure we update both
8864 variables within BLOCK_INPUT so no such event interrupts. */
8865 BLOCK_INPUT;
8866 f->tool_bar_items = new_tool_bar;
8867 f->n_tool_bar_items = new_n_tool_bar;
8868 w->update_mode_line = Qt;
8869 UNBLOCK_INPUT;
8872 UNGCPRO;
8874 unbind_to (count, Qnil);
8875 set_buffer_internal_1 (prev);
8881 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8882 F's desired tool-bar contents. F->tool_bar_items must have
8883 been set up previously by calling prepare_menu_bars. */
8885 static void
8886 build_desired_tool_bar_string (f)
8887 struct frame *f;
8889 int i, size, size_needed;
8890 struct gcpro gcpro1, gcpro2, gcpro3;
8891 Lisp_Object image, plist, props;
8893 image = plist = props = Qnil;
8894 GCPRO3 (image, plist, props);
8896 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8897 Otherwise, make a new string. */
8899 /* The size of the string we might be able to reuse. */
8900 size = (STRINGP (f->desired_tool_bar_string)
8901 ? SCHARS (f->desired_tool_bar_string)
8902 : 0);
8904 /* We need one space in the string for each image. */
8905 size_needed = f->n_tool_bar_items;
8907 /* Reuse f->desired_tool_bar_string, if possible. */
8908 if (size < size_needed || NILP (f->desired_tool_bar_string))
8909 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8910 make_number (' '));
8911 else
8913 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8914 Fremove_text_properties (make_number (0), make_number (size),
8915 props, f->desired_tool_bar_string);
8918 /* Put a `display' property on the string for the images to display,
8919 put a `menu_item' property on tool-bar items with a value that
8920 is the index of the item in F's tool-bar item vector. */
8921 for (i = 0; i < f->n_tool_bar_items; ++i)
8923 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8925 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8926 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8927 int hmargin, vmargin, relief, idx, end;
8928 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8930 /* If image is a vector, choose the image according to the
8931 button state. */
8932 image = PROP (TOOL_BAR_ITEM_IMAGES);
8933 if (VECTORP (image))
8935 if (enabled_p)
8936 idx = (selected_p
8937 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8938 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8939 else
8940 idx = (selected_p
8941 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8942 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8944 xassert (ASIZE (image) >= idx);
8945 image = AREF (image, idx);
8947 else
8948 idx = -1;
8950 /* Ignore invalid image specifications. */
8951 if (!valid_image_p (image))
8952 continue;
8954 /* Display the tool-bar button pressed, or depressed. */
8955 plist = Fcopy_sequence (XCDR (image));
8957 /* Compute margin and relief to draw. */
8958 relief = (tool_bar_button_relief >= 0
8959 ? tool_bar_button_relief
8960 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8961 hmargin = vmargin = relief;
8963 if (INTEGERP (Vtool_bar_button_margin)
8964 && XINT (Vtool_bar_button_margin) > 0)
8966 hmargin += XFASTINT (Vtool_bar_button_margin);
8967 vmargin += XFASTINT (Vtool_bar_button_margin);
8969 else if (CONSP (Vtool_bar_button_margin))
8971 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8972 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8973 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8975 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8976 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8977 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8980 if (auto_raise_tool_bar_buttons_p)
8982 /* Add a `:relief' property to the image spec if the item is
8983 selected. */
8984 if (selected_p)
8986 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8987 hmargin -= relief;
8988 vmargin -= relief;
8991 else
8993 /* If image is selected, display it pressed, i.e. with a
8994 negative relief. If it's not selected, display it with a
8995 raised relief. */
8996 plist = Fplist_put (plist, QCrelief,
8997 (selected_p
8998 ? make_number (-relief)
8999 : make_number (relief)));
9000 hmargin -= relief;
9001 vmargin -= relief;
9004 /* Put a margin around the image. */
9005 if (hmargin || vmargin)
9007 if (hmargin == vmargin)
9008 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9009 else
9010 plist = Fplist_put (plist, QCmargin,
9011 Fcons (make_number (hmargin),
9012 make_number (vmargin)));
9015 /* If button is not enabled, and we don't have special images
9016 for the disabled state, make the image appear disabled by
9017 applying an appropriate algorithm to it. */
9018 if (!enabled_p && idx < 0)
9019 plist = Fplist_put (plist, QCconversion, Qdisabled);
9021 /* Put a `display' text property on the string for the image to
9022 display. Put a `menu-item' property on the string that gives
9023 the start of this item's properties in the tool-bar items
9024 vector. */
9025 image = Fcons (Qimage, plist);
9026 props = list4 (Qdisplay, image,
9027 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9029 /* Let the last image hide all remaining spaces in the tool bar
9030 string. The string can be longer than needed when we reuse a
9031 previous string. */
9032 if (i + 1 == f->n_tool_bar_items)
9033 end = SCHARS (f->desired_tool_bar_string);
9034 else
9035 end = i + 1;
9036 Fadd_text_properties (make_number (i), make_number (end),
9037 props, f->desired_tool_bar_string);
9038 #undef PROP
9041 UNGCPRO;
9045 /* Display one line of the tool-bar of frame IT->f. */
9047 static void
9048 display_tool_bar_line (it)
9049 struct it *it;
9051 struct glyph_row *row = it->glyph_row;
9052 int max_x = it->last_visible_x;
9053 struct glyph *last;
9055 prepare_desired_row (row);
9056 row->y = it->current_y;
9058 /* Note that this isn't made use of if the face hasn't a box,
9059 so there's no need to check the face here. */
9060 it->start_of_box_run_p = 1;
9062 while (it->current_x < max_x)
9064 int x_before, x, n_glyphs_before, i, nglyphs;
9066 /* Get the next display element. */
9067 if (!get_next_display_element (it))
9068 break;
9070 /* Produce glyphs. */
9071 x_before = it->current_x;
9072 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9073 PRODUCE_GLYPHS (it);
9075 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9076 i = 0;
9077 x = x_before;
9078 while (i < nglyphs)
9080 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9082 if (x + glyph->pixel_width > max_x)
9084 /* Glyph doesn't fit on line. */
9085 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9086 it->current_x = x;
9087 goto out;
9090 ++it->hpos;
9091 x += glyph->pixel_width;
9092 ++i;
9095 /* Stop at line ends. */
9096 if (ITERATOR_AT_END_OF_LINE_P (it))
9097 break;
9099 set_iterator_to_next (it, 1);
9102 out:;
9104 row->displays_text_p = row->used[TEXT_AREA] != 0;
9105 extend_face_to_end_of_line (it);
9106 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9107 last->right_box_line_p = 1;
9108 if (last == row->glyphs[TEXT_AREA])
9109 last->left_box_line_p = 1;
9110 compute_line_metrics (it);
9112 /* If line is empty, make it occupy the rest of the tool-bar. */
9113 if (!row->displays_text_p)
9115 row->height = row->phys_height = it->last_visible_y - row->y;
9116 row->ascent = row->phys_ascent = 0;
9117 row->extra_line_spacing = 0;
9120 row->full_width_p = 1;
9121 row->continued_p = 0;
9122 row->truncated_on_left_p = 0;
9123 row->truncated_on_right_p = 0;
9125 it->current_x = it->hpos = 0;
9126 it->current_y += row->height;
9127 ++it->vpos;
9128 ++it->glyph_row;
9132 /* Value is the number of screen lines needed to make all tool-bar
9133 items of frame F visible. */
9135 static int
9136 tool_bar_lines_needed (f)
9137 struct frame *f;
9139 struct window *w = XWINDOW (f->tool_bar_window);
9140 struct it it;
9142 /* Initialize an iterator for iteration over
9143 F->desired_tool_bar_string in the tool-bar window of frame F. */
9144 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9145 it.first_visible_x = 0;
9146 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9147 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9149 while (!ITERATOR_AT_END_P (&it))
9151 it.glyph_row = w->desired_matrix->rows;
9152 clear_glyph_row (it.glyph_row);
9153 display_tool_bar_line (&it);
9156 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9160 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9161 0, 1, 0,
9162 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9163 (frame)
9164 Lisp_Object frame;
9166 struct frame *f;
9167 struct window *w;
9168 int nlines = 0;
9170 if (NILP (frame))
9171 frame = selected_frame;
9172 else
9173 CHECK_FRAME (frame);
9174 f = XFRAME (frame);
9176 if (WINDOWP (f->tool_bar_window)
9177 || (w = XWINDOW (f->tool_bar_window),
9178 WINDOW_TOTAL_LINES (w) > 0))
9180 update_tool_bar (f, 1);
9181 if (f->n_tool_bar_items)
9183 build_desired_tool_bar_string (f);
9184 nlines = tool_bar_lines_needed (f);
9188 return make_number (nlines);
9192 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9193 height should be changed. */
9195 static int
9196 redisplay_tool_bar (f)
9197 struct frame *f;
9199 struct window *w;
9200 struct it it;
9201 struct glyph_row *row;
9202 int change_height_p = 0;
9204 #ifdef USE_GTK
9205 if (FRAME_EXTERNAL_TOOL_BAR (f))
9206 update_frame_tool_bar (f);
9207 return 0;
9208 #endif
9210 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9211 do anything. This means you must start with tool-bar-lines
9212 non-zero to get the auto-sizing effect. Or in other words, you
9213 can turn off tool-bars by specifying tool-bar-lines zero. */
9214 if (!WINDOWP (f->tool_bar_window)
9215 || (w = XWINDOW (f->tool_bar_window),
9216 WINDOW_TOTAL_LINES (w) == 0))
9217 return 0;
9219 /* Set up an iterator for the tool-bar window. */
9220 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9221 it.first_visible_x = 0;
9222 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9223 row = it.glyph_row;
9225 /* Build a string that represents the contents of the tool-bar. */
9226 build_desired_tool_bar_string (f);
9227 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9229 /* Display as many lines as needed to display all tool-bar items. */
9230 while (it.current_y < it.last_visible_y)
9231 display_tool_bar_line (&it);
9233 /* It doesn't make much sense to try scrolling in the tool-bar
9234 window, so don't do it. */
9235 w->desired_matrix->no_scrolling_p = 1;
9236 w->must_be_updated_p = 1;
9238 if (auto_resize_tool_bars_p)
9240 int nlines;
9242 /* If we couldn't display everything, change the tool-bar's
9243 height. */
9244 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9245 change_height_p = 1;
9247 /* If there are blank lines at the end, except for a partially
9248 visible blank line at the end that is smaller than
9249 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9250 row = it.glyph_row - 1;
9251 if (!row->displays_text_p
9252 && row->height >= FRAME_LINE_HEIGHT (f))
9253 change_height_p = 1;
9255 /* If row displays tool-bar items, but is partially visible,
9256 change the tool-bar's height. */
9257 if (row->displays_text_p
9258 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9259 change_height_p = 1;
9261 /* Resize windows as needed by changing the `tool-bar-lines'
9262 frame parameter. */
9263 if (change_height_p
9264 && (nlines = tool_bar_lines_needed (f),
9265 nlines != WINDOW_TOTAL_LINES (w)))
9267 extern Lisp_Object Qtool_bar_lines;
9268 Lisp_Object frame;
9269 int old_height = WINDOW_TOTAL_LINES (w);
9271 XSETFRAME (frame, f);
9272 clear_glyph_matrix (w->desired_matrix);
9273 Fmodify_frame_parameters (frame,
9274 Fcons (Fcons (Qtool_bar_lines,
9275 make_number (nlines)),
9276 Qnil));
9277 if (WINDOW_TOTAL_LINES (w) != old_height)
9278 fonts_changed_p = 1;
9282 return change_height_p;
9286 /* Get information about the tool-bar item which is displayed in GLYPH
9287 on frame F. Return in *PROP_IDX the index where tool-bar item
9288 properties start in F->tool_bar_items. Value is zero if
9289 GLYPH doesn't display a tool-bar item. */
9291 static int
9292 tool_bar_item_info (f, glyph, prop_idx)
9293 struct frame *f;
9294 struct glyph *glyph;
9295 int *prop_idx;
9297 Lisp_Object prop;
9298 int success_p;
9299 int charpos;
9301 /* This function can be called asynchronously, which means we must
9302 exclude any possibility that Fget_text_property signals an
9303 error. */
9304 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9305 charpos = max (0, charpos);
9307 /* Get the text property `menu-item' at pos. The value of that
9308 property is the start index of this item's properties in
9309 F->tool_bar_items. */
9310 prop = Fget_text_property (make_number (charpos),
9311 Qmenu_item, f->current_tool_bar_string);
9312 if (INTEGERP (prop))
9314 *prop_idx = XINT (prop);
9315 success_p = 1;
9317 else
9318 success_p = 0;
9320 return success_p;
9324 /* Get information about the tool-bar item at position X/Y on frame F.
9325 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9326 the current matrix of the tool-bar window of F, or NULL if not
9327 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9328 item in F->tool_bar_items. Value is
9330 -1 if X/Y is not on a tool-bar item
9331 0 if X/Y is on the same item that was highlighted before.
9332 1 otherwise. */
9334 static int
9335 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9336 struct frame *f;
9337 int x, y;
9338 struct glyph **glyph;
9339 int *hpos, *vpos, *prop_idx;
9341 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9342 struct window *w = XWINDOW (f->tool_bar_window);
9343 int area;
9345 /* Find the glyph under X/Y. */
9346 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9347 if (*glyph == NULL)
9348 return -1;
9350 /* Get the start of this tool-bar item's properties in
9351 f->tool_bar_items. */
9352 if (!tool_bar_item_info (f, *glyph, prop_idx))
9353 return -1;
9355 /* Is mouse on the highlighted item? */
9356 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9357 && *vpos >= dpyinfo->mouse_face_beg_row
9358 && *vpos <= dpyinfo->mouse_face_end_row
9359 && (*vpos > dpyinfo->mouse_face_beg_row
9360 || *hpos >= dpyinfo->mouse_face_beg_col)
9361 && (*vpos < dpyinfo->mouse_face_end_row
9362 || *hpos < dpyinfo->mouse_face_end_col
9363 || dpyinfo->mouse_face_past_end))
9364 return 0;
9366 return 1;
9370 /* EXPORT:
9371 Handle mouse button event on the tool-bar of frame F, at
9372 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9373 0 for button release. MODIFIERS is event modifiers for button
9374 release. */
9376 void
9377 handle_tool_bar_click (f, x, y, down_p, modifiers)
9378 struct frame *f;
9379 int x, y, down_p;
9380 unsigned int modifiers;
9382 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9383 struct window *w = XWINDOW (f->tool_bar_window);
9384 int hpos, vpos, prop_idx;
9385 struct glyph *glyph;
9386 Lisp_Object enabled_p;
9388 /* If not on the highlighted tool-bar item, return. */
9389 frame_to_window_pixel_xy (w, &x, &y);
9390 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9391 return;
9393 /* If item is disabled, do nothing. */
9394 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9395 if (NILP (enabled_p))
9396 return;
9398 if (down_p)
9400 /* Show item in pressed state. */
9401 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9402 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9403 last_tool_bar_item = prop_idx;
9405 else
9407 Lisp_Object key, frame;
9408 struct input_event event;
9409 EVENT_INIT (event);
9411 /* Show item in released state. */
9412 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9413 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9415 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9417 XSETFRAME (frame, f);
9418 event.kind = TOOL_BAR_EVENT;
9419 event.frame_or_window = frame;
9420 event.arg = frame;
9421 kbd_buffer_store_event (&event);
9423 event.kind = TOOL_BAR_EVENT;
9424 event.frame_or_window = frame;
9425 event.arg = key;
9426 event.modifiers = modifiers;
9427 kbd_buffer_store_event (&event);
9428 last_tool_bar_item = -1;
9433 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9434 tool-bar window-relative coordinates X/Y. Called from
9435 note_mouse_highlight. */
9437 static void
9438 note_tool_bar_highlight (f, x, y)
9439 struct frame *f;
9440 int x, y;
9442 Lisp_Object window = f->tool_bar_window;
9443 struct window *w = XWINDOW (window);
9444 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9445 int hpos, vpos;
9446 struct glyph *glyph;
9447 struct glyph_row *row;
9448 int i;
9449 Lisp_Object enabled_p;
9450 int prop_idx;
9451 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9452 int mouse_down_p, rc;
9454 /* Function note_mouse_highlight is called with negative x(y
9455 values when mouse moves outside of the frame. */
9456 if (x <= 0 || y <= 0)
9458 clear_mouse_face (dpyinfo);
9459 return;
9462 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9463 if (rc < 0)
9465 /* Not on tool-bar item. */
9466 clear_mouse_face (dpyinfo);
9467 return;
9469 else if (rc == 0)
9470 /* On same tool-bar item as before. */
9471 goto set_help_echo;
9473 clear_mouse_face (dpyinfo);
9475 /* Mouse is down, but on different tool-bar item? */
9476 mouse_down_p = (dpyinfo->grabbed
9477 && f == last_mouse_frame
9478 && FRAME_LIVE_P (f));
9479 if (mouse_down_p
9480 && last_tool_bar_item != prop_idx)
9481 return;
9483 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9484 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9486 /* If tool-bar item is not enabled, don't highlight it. */
9487 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9488 if (!NILP (enabled_p))
9490 /* Compute the x-position of the glyph. In front and past the
9491 image is a space. We include this in the highlighted area. */
9492 row = MATRIX_ROW (w->current_matrix, vpos);
9493 for (i = x = 0; i < hpos; ++i)
9494 x += row->glyphs[TEXT_AREA][i].pixel_width;
9496 /* Record this as the current active region. */
9497 dpyinfo->mouse_face_beg_col = hpos;
9498 dpyinfo->mouse_face_beg_row = vpos;
9499 dpyinfo->mouse_face_beg_x = x;
9500 dpyinfo->mouse_face_beg_y = row->y;
9501 dpyinfo->mouse_face_past_end = 0;
9503 dpyinfo->mouse_face_end_col = hpos + 1;
9504 dpyinfo->mouse_face_end_row = vpos;
9505 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9506 dpyinfo->mouse_face_end_y = row->y;
9507 dpyinfo->mouse_face_window = window;
9508 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9510 /* Display it as active. */
9511 show_mouse_face (dpyinfo, draw);
9512 dpyinfo->mouse_face_image_state = draw;
9515 set_help_echo:
9517 /* Set help_echo_string to a help string to display for this tool-bar item.
9518 XTread_socket does the rest. */
9519 help_echo_object = help_echo_window = Qnil;
9520 help_echo_pos = -1;
9521 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9522 if (NILP (help_echo_string))
9523 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9526 #endif /* HAVE_WINDOW_SYSTEM */
9530 /************************************************************************
9531 Horizontal scrolling
9532 ************************************************************************/
9534 static int hscroll_window_tree P_ ((Lisp_Object));
9535 static int hscroll_windows P_ ((Lisp_Object));
9537 /* For all leaf windows in the window tree rooted at WINDOW, set their
9538 hscroll value so that PT is (i) visible in the window, and (ii) so
9539 that it is not within a certain margin at the window's left and
9540 right border. Value is non-zero if any window's hscroll has been
9541 changed. */
9543 static int
9544 hscroll_window_tree (window)
9545 Lisp_Object window;
9547 int hscrolled_p = 0;
9548 int hscroll_relative_p = FLOATP (Vhscroll_step);
9549 int hscroll_step_abs = 0;
9550 double hscroll_step_rel = 0;
9552 if (hscroll_relative_p)
9554 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9555 if (hscroll_step_rel < 0)
9557 hscroll_relative_p = 0;
9558 hscroll_step_abs = 0;
9561 else if (INTEGERP (Vhscroll_step))
9563 hscroll_step_abs = XINT (Vhscroll_step);
9564 if (hscroll_step_abs < 0)
9565 hscroll_step_abs = 0;
9567 else
9568 hscroll_step_abs = 0;
9570 while (WINDOWP (window))
9572 struct window *w = XWINDOW (window);
9574 if (WINDOWP (w->hchild))
9575 hscrolled_p |= hscroll_window_tree (w->hchild);
9576 else if (WINDOWP (w->vchild))
9577 hscrolled_p |= hscroll_window_tree (w->vchild);
9578 else if (w->cursor.vpos >= 0)
9580 int h_margin;
9581 int text_area_width;
9582 struct glyph_row *current_cursor_row
9583 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9584 struct glyph_row *desired_cursor_row
9585 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9586 struct glyph_row *cursor_row
9587 = (desired_cursor_row->enabled_p
9588 ? desired_cursor_row
9589 : current_cursor_row);
9591 text_area_width = window_box_width (w, TEXT_AREA);
9593 /* Scroll when cursor is inside this scroll margin. */
9594 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9596 if ((XFASTINT (w->hscroll)
9597 && w->cursor.x <= h_margin)
9598 || (cursor_row->enabled_p
9599 && cursor_row->truncated_on_right_p
9600 && (w->cursor.x >= text_area_width - h_margin)))
9602 struct it it;
9603 int hscroll;
9604 struct buffer *saved_current_buffer;
9605 int pt;
9606 int wanted_x;
9608 /* Find point in a display of infinite width. */
9609 saved_current_buffer = current_buffer;
9610 current_buffer = XBUFFER (w->buffer);
9612 if (w == XWINDOW (selected_window))
9613 pt = BUF_PT (current_buffer);
9614 else
9616 pt = marker_position (w->pointm);
9617 pt = max (BEGV, pt);
9618 pt = min (ZV, pt);
9621 /* Move iterator to pt starting at cursor_row->start in
9622 a line with infinite width. */
9623 init_to_row_start (&it, w, cursor_row);
9624 it.last_visible_x = INFINITY;
9625 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9626 current_buffer = saved_current_buffer;
9628 /* Position cursor in window. */
9629 if (!hscroll_relative_p && hscroll_step_abs == 0)
9630 hscroll = max (0, (it.current_x
9631 - (ITERATOR_AT_END_OF_LINE_P (&it)
9632 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9633 : (text_area_width / 2))))
9634 / FRAME_COLUMN_WIDTH (it.f);
9635 else if (w->cursor.x >= text_area_width - h_margin)
9637 if (hscroll_relative_p)
9638 wanted_x = text_area_width * (1 - hscroll_step_rel)
9639 - h_margin;
9640 else
9641 wanted_x = text_area_width
9642 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9643 - h_margin;
9644 hscroll
9645 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9647 else
9649 if (hscroll_relative_p)
9650 wanted_x = text_area_width * hscroll_step_rel
9651 + h_margin;
9652 else
9653 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9654 + h_margin;
9655 hscroll
9656 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9658 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9660 /* Don't call Fset_window_hscroll if value hasn't
9661 changed because it will prevent redisplay
9662 optimizations. */
9663 if (XFASTINT (w->hscroll) != hscroll)
9665 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9666 w->hscroll = make_number (hscroll);
9667 hscrolled_p = 1;
9672 window = w->next;
9675 /* Value is non-zero if hscroll of any leaf window has been changed. */
9676 return hscrolled_p;
9680 /* Set hscroll so that cursor is visible and not inside horizontal
9681 scroll margins for all windows in the tree rooted at WINDOW. See
9682 also hscroll_window_tree above. Value is non-zero if any window's
9683 hscroll has been changed. If it has, desired matrices on the frame
9684 of WINDOW are cleared. */
9686 static int
9687 hscroll_windows (window)
9688 Lisp_Object window;
9690 int hscrolled_p;
9692 if (automatic_hscrolling_p)
9694 hscrolled_p = hscroll_window_tree (window);
9695 if (hscrolled_p)
9696 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9698 else
9699 hscrolled_p = 0;
9700 return hscrolled_p;
9705 /************************************************************************
9706 Redisplay
9707 ************************************************************************/
9709 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9710 to a non-zero value. This is sometimes handy to have in a debugger
9711 session. */
9713 #if GLYPH_DEBUG
9715 /* First and last unchanged row for try_window_id. */
9717 int debug_first_unchanged_at_end_vpos;
9718 int debug_last_unchanged_at_beg_vpos;
9720 /* Delta vpos and y. */
9722 int debug_dvpos, debug_dy;
9724 /* Delta in characters and bytes for try_window_id. */
9726 int debug_delta, debug_delta_bytes;
9728 /* Values of window_end_pos and window_end_vpos at the end of
9729 try_window_id. */
9731 EMACS_INT debug_end_pos, debug_end_vpos;
9733 /* Append a string to W->desired_matrix->method. FMT is a printf
9734 format string. A1...A9 are a supplement for a variable-length
9735 argument list. If trace_redisplay_p is non-zero also printf the
9736 resulting string to stderr. */
9738 static void
9739 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9740 struct window *w;
9741 char *fmt;
9742 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9744 char buffer[512];
9745 char *method = w->desired_matrix->method;
9746 int len = strlen (method);
9747 int size = sizeof w->desired_matrix->method;
9748 int remaining = size - len - 1;
9750 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9751 if (len && remaining)
9753 method[len] = '|';
9754 --remaining, ++len;
9757 strncpy (method + len, buffer, remaining);
9759 if (trace_redisplay_p)
9760 fprintf (stderr, "%p (%s): %s\n",
9762 ((BUFFERP (w->buffer)
9763 && STRINGP (XBUFFER (w->buffer)->name))
9764 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9765 : "no buffer"),
9766 buffer);
9769 #endif /* GLYPH_DEBUG */
9772 /* Value is non-zero if all changes in window W, which displays
9773 current_buffer, are in the text between START and END. START is a
9774 buffer position, END is given as a distance from Z. Used in
9775 redisplay_internal for display optimization. */
9777 static INLINE int
9778 text_outside_line_unchanged_p (w, start, end)
9779 struct window *w;
9780 int start, end;
9782 int unchanged_p = 1;
9784 /* If text or overlays have changed, see where. */
9785 if (XFASTINT (w->last_modified) < MODIFF
9786 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9788 /* Gap in the line? */
9789 if (GPT < start || Z - GPT < end)
9790 unchanged_p = 0;
9792 /* Changes start in front of the line, or end after it? */
9793 if (unchanged_p
9794 && (BEG_UNCHANGED < start - 1
9795 || END_UNCHANGED < end))
9796 unchanged_p = 0;
9798 /* If selective display, can't optimize if changes start at the
9799 beginning of the line. */
9800 if (unchanged_p
9801 && INTEGERP (current_buffer->selective_display)
9802 && XINT (current_buffer->selective_display) > 0
9803 && (BEG_UNCHANGED < start || GPT <= start))
9804 unchanged_p = 0;
9806 /* If there are overlays at the start or end of the line, these
9807 may have overlay strings with newlines in them. A change at
9808 START, for instance, may actually concern the display of such
9809 overlay strings as well, and they are displayed on different
9810 lines. So, quickly rule out this case. (For the future, it
9811 might be desirable to implement something more telling than
9812 just BEG/END_UNCHANGED.) */
9813 if (unchanged_p)
9815 if (BEG + BEG_UNCHANGED == start
9816 && overlay_touches_p (start))
9817 unchanged_p = 0;
9818 if (END_UNCHANGED == end
9819 && overlay_touches_p (Z - end))
9820 unchanged_p = 0;
9824 return unchanged_p;
9828 /* Do a frame update, taking possible shortcuts into account. This is
9829 the main external entry point for redisplay.
9831 If the last redisplay displayed an echo area message and that message
9832 is no longer requested, we clear the echo area or bring back the
9833 mini-buffer if that is in use. */
9835 void
9836 redisplay ()
9838 redisplay_internal (0);
9842 static Lisp_Object
9843 overlay_arrow_string_or_property (var)
9844 Lisp_Object var;
9846 Lisp_Object val;
9848 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9849 return val;
9851 return Voverlay_arrow_string;
9854 /* Return 1 if there are any overlay-arrows in current_buffer. */
9855 static int
9856 overlay_arrow_in_current_buffer_p ()
9858 Lisp_Object vlist;
9860 for (vlist = Voverlay_arrow_variable_list;
9861 CONSP (vlist);
9862 vlist = XCDR (vlist))
9864 Lisp_Object var = XCAR (vlist);
9865 Lisp_Object val;
9867 if (!SYMBOLP (var))
9868 continue;
9869 val = find_symbol_value (var);
9870 if (MARKERP (val)
9871 && current_buffer == XMARKER (val)->buffer)
9872 return 1;
9874 return 0;
9878 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9879 has changed. */
9881 static int
9882 overlay_arrows_changed_p ()
9884 Lisp_Object vlist;
9886 for (vlist = Voverlay_arrow_variable_list;
9887 CONSP (vlist);
9888 vlist = XCDR (vlist))
9890 Lisp_Object var = XCAR (vlist);
9891 Lisp_Object val, pstr;
9893 if (!SYMBOLP (var))
9894 continue;
9895 val = find_symbol_value (var);
9896 if (!MARKERP (val))
9897 continue;
9898 if (! EQ (COERCE_MARKER (val),
9899 Fget (var, Qlast_arrow_position))
9900 || ! (pstr = overlay_arrow_string_or_property (var),
9901 EQ (pstr, Fget (var, Qlast_arrow_string))))
9902 return 1;
9904 return 0;
9907 /* Mark overlay arrows to be updated on next redisplay. */
9909 static void
9910 update_overlay_arrows (up_to_date)
9911 int up_to_date;
9913 Lisp_Object vlist;
9915 for (vlist = Voverlay_arrow_variable_list;
9916 CONSP (vlist);
9917 vlist = XCDR (vlist))
9919 Lisp_Object var = XCAR (vlist);
9921 if (!SYMBOLP (var))
9922 continue;
9924 if (up_to_date > 0)
9926 Lisp_Object val = find_symbol_value (var);
9927 Fput (var, Qlast_arrow_position,
9928 COERCE_MARKER (val));
9929 Fput (var, Qlast_arrow_string,
9930 overlay_arrow_string_or_property (var));
9932 else if (up_to_date < 0
9933 || !NILP (Fget (var, Qlast_arrow_position)))
9935 Fput (var, Qlast_arrow_position, Qt);
9936 Fput (var, Qlast_arrow_string, Qt);
9942 /* Return overlay arrow string to display at row.
9943 Return integer (bitmap number) for arrow bitmap in left fringe.
9944 Return nil if no overlay arrow. */
9946 static Lisp_Object
9947 overlay_arrow_at_row (it, row)
9948 struct it *it;
9949 struct glyph_row *row;
9951 Lisp_Object vlist;
9953 for (vlist = Voverlay_arrow_variable_list;
9954 CONSP (vlist);
9955 vlist = XCDR (vlist))
9957 Lisp_Object var = XCAR (vlist);
9958 Lisp_Object val;
9960 if (!SYMBOLP (var))
9961 continue;
9963 val = find_symbol_value (var);
9965 if (MARKERP (val)
9966 && current_buffer == XMARKER (val)->buffer
9967 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9969 if (FRAME_WINDOW_P (it->f)
9970 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9972 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9974 int fringe_bitmap;
9975 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9976 return make_number (fringe_bitmap);
9978 return make_number (-1); /* Use default arrow bitmap */
9980 return overlay_arrow_string_or_property (var);
9984 return Qnil;
9987 /* Return 1 if point moved out of or into a composition. Otherwise
9988 return 0. PREV_BUF and PREV_PT are the last point buffer and
9989 position. BUF and PT are the current point buffer and position. */
9992 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9993 struct buffer *prev_buf, *buf;
9994 int prev_pt, pt;
9996 int start, end;
9997 Lisp_Object prop;
9998 Lisp_Object buffer;
10000 XSETBUFFER (buffer, buf);
10001 /* Check a composition at the last point if point moved within the
10002 same buffer. */
10003 if (prev_buf == buf)
10005 if (prev_pt == pt)
10006 /* Point didn't move. */
10007 return 0;
10009 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10010 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10011 && COMPOSITION_VALID_P (start, end, prop)
10012 && start < prev_pt && end > prev_pt)
10013 /* The last point was within the composition. Return 1 iff
10014 point moved out of the composition. */
10015 return (pt <= start || pt >= end);
10018 /* Check a composition at the current point. */
10019 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10020 && find_composition (pt, -1, &start, &end, &prop, buffer)
10021 && COMPOSITION_VALID_P (start, end, prop)
10022 && start < pt && end > pt);
10026 /* Reconsider the setting of B->clip_changed which is displayed
10027 in window W. */
10029 static INLINE void
10030 reconsider_clip_changes (w, b)
10031 struct window *w;
10032 struct buffer *b;
10034 if (b->clip_changed
10035 && !NILP (w->window_end_valid)
10036 && w->current_matrix->buffer == b
10037 && w->current_matrix->zv == BUF_ZV (b)
10038 && w->current_matrix->begv == BUF_BEGV (b))
10039 b->clip_changed = 0;
10041 /* If display wasn't paused, and W is not a tool bar window, see if
10042 point has been moved into or out of a composition. In that case,
10043 we set b->clip_changed to 1 to force updating the screen. If
10044 b->clip_changed has already been set to 1, we can skip this
10045 check. */
10046 if (!b->clip_changed
10047 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10049 int pt;
10051 if (w == XWINDOW (selected_window))
10052 pt = BUF_PT (current_buffer);
10053 else
10054 pt = marker_position (w->pointm);
10056 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10057 || pt != XINT (w->last_point))
10058 && check_point_in_composition (w->current_matrix->buffer,
10059 XINT (w->last_point),
10060 XBUFFER (w->buffer), pt))
10061 b->clip_changed = 1;
10066 /* Select FRAME to forward the values of frame-local variables into C
10067 variables so that the redisplay routines can access those values
10068 directly. */
10070 static void
10071 select_frame_for_redisplay (frame)
10072 Lisp_Object frame;
10074 Lisp_Object tail, sym, val;
10075 Lisp_Object old = selected_frame;
10077 selected_frame = frame;
10079 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10080 if (CONSP (XCAR (tail))
10081 && (sym = XCAR (XCAR (tail)),
10082 SYMBOLP (sym))
10083 && (sym = indirect_variable (sym),
10084 val = SYMBOL_VALUE (sym),
10085 (BUFFER_LOCAL_VALUEP (val)
10086 || SOME_BUFFER_LOCAL_VALUEP (val)))
10087 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10088 /* Use find_symbol_value rather than Fsymbol_value
10089 to avoid an error if it is void. */
10090 find_symbol_value (sym);
10092 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10093 if (CONSP (XCAR (tail))
10094 && (sym = XCAR (XCAR (tail)),
10095 SYMBOLP (sym))
10096 && (sym = indirect_variable (sym),
10097 val = SYMBOL_VALUE (sym),
10098 (BUFFER_LOCAL_VALUEP (val)
10099 || SOME_BUFFER_LOCAL_VALUEP (val)))
10100 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10101 find_symbol_value (sym);
10105 #define STOP_POLLING \
10106 do { if (! polling_stopped_here) stop_polling (); \
10107 polling_stopped_here = 1; } while (0)
10109 #define RESUME_POLLING \
10110 do { if (polling_stopped_here) start_polling (); \
10111 polling_stopped_here = 0; } while (0)
10114 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10115 response to any user action; therefore, we should preserve the echo
10116 area. (Actually, our caller does that job.) Perhaps in the future
10117 avoid recentering windows if it is not necessary; currently that
10118 causes some problems. */
10120 static void
10121 redisplay_internal (preserve_echo_area)
10122 int preserve_echo_area;
10124 struct window *w = XWINDOW (selected_window);
10125 struct frame *f = XFRAME (w->frame);
10126 int pause;
10127 int must_finish = 0;
10128 struct text_pos tlbufpos, tlendpos;
10129 int number_of_visible_frames;
10130 int count;
10131 struct frame *sf = SELECTED_FRAME ();
10132 int polling_stopped_here = 0;
10134 /* Non-zero means redisplay has to consider all windows on all
10135 frames. Zero means, only selected_window is considered. */
10136 int consider_all_windows_p;
10138 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10140 /* No redisplay if running in batch mode or frame is not yet fully
10141 initialized, or redisplay is explicitly turned off by setting
10142 Vinhibit_redisplay. */
10143 if (noninteractive
10144 || !NILP (Vinhibit_redisplay)
10145 || !f->glyphs_initialized_p)
10146 return;
10148 /* The flag redisplay_performed_directly_p is set by
10149 direct_output_for_insert when it already did the whole screen
10150 update necessary. */
10151 if (redisplay_performed_directly_p)
10153 redisplay_performed_directly_p = 0;
10154 if (!hscroll_windows (selected_window))
10155 return;
10158 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10159 if (popup_activated ())
10160 return;
10161 #endif
10163 /* I don't think this happens but let's be paranoid. */
10164 if (redisplaying_p)
10165 return;
10167 /* Record a function that resets redisplaying_p to its old value
10168 when we leave this function. */
10169 count = SPECPDL_INDEX ();
10170 record_unwind_protect (unwind_redisplay,
10171 Fcons (make_number (redisplaying_p), selected_frame));
10172 ++redisplaying_p;
10173 specbind (Qinhibit_free_realized_faces, Qnil);
10176 Lisp_Object tail, frame;
10178 FOR_EACH_FRAME (tail, frame)
10180 struct frame *f = XFRAME (frame);
10181 f->already_hscrolled_p = 0;
10185 retry:
10186 pause = 0;
10187 reconsider_clip_changes (w, current_buffer);
10189 /* If new fonts have been loaded that make a glyph matrix adjustment
10190 necessary, do it. */
10191 if (fonts_changed_p)
10193 adjust_glyphs (NULL);
10194 ++windows_or_buffers_changed;
10195 fonts_changed_p = 0;
10198 /* If face_change_count is non-zero, init_iterator will free all
10199 realized faces, which includes the faces referenced from current
10200 matrices. So, we can't reuse current matrices in this case. */
10201 if (face_change_count)
10202 ++windows_or_buffers_changed;
10204 if (! FRAME_WINDOW_P (sf)
10205 && previous_terminal_frame != sf)
10207 /* Since frames on an ASCII terminal share the same display
10208 area, displaying a different frame means redisplay the whole
10209 thing. */
10210 windows_or_buffers_changed++;
10211 SET_FRAME_GARBAGED (sf);
10212 XSETFRAME (Vterminal_frame, sf);
10214 previous_terminal_frame = sf;
10216 /* Set the visible flags for all frames. Do this before checking
10217 for resized or garbaged frames; they want to know if their frames
10218 are visible. See the comment in frame.h for
10219 FRAME_SAMPLE_VISIBILITY. */
10221 Lisp_Object tail, frame;
10223 number_of_visible_frames = 0;
10225 FOR_EACH_FRAME (tail, frame)
10227 struct frame *f = XFRAME (frame);
10229 FRAME_SAMPLE_VISIBILITY (f);
10230 if (FRAME_VISIBLE_P (f))
10231 ++number_of_visible_frames;
10232 clear_desired_matrices (f);
10236 /* Notice any pending interrupt request to change frame size. */
10237 do_pending_window_change (1);
10239 /* Clear frames marked as garbaged. */
10240 if (frame_garbaged)
10241 clear_garbaged_frames ();
10243 /* Build menubar and tool-bar items. */
10244 prepare_menu_bars ();
10246 if (windows_or_buffers_changed)
10247 update_mode_lines++;
10249 /* Detect case that we need to write or remove a star in the mode line. */
10250 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10252 w->update_mode_line = Qt;
10253 if (buffer_shared > 1)
10254 update_mode_lines++;
10257 /* If %c is in the mode line, update it if needed. */
10258 if (!NILP (w->column_number_displayed)
10259 /* This alternative quickly identifies a common case
10260 where no change is needed. */
10261 && !(PT == XFASTINT (w->last_point)
10262 && XFASTINT (w->last_modified) >= MODIFF
10263 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10264 && (XFASTINT (w->column_number_displayed)
10265 != (int) current_column ())) /* iftc */
10266 w->update_mode_line = Qt;
10268 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10270 /* The variable buffer_shared is set in redisplay_window and
10271 indicates that we redisplay a buffer in different windows. See
10272 there. */
10273 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10274 || cursor_type_changed);
10276 /* If specs for an arrow have changed, do thorough redisplay
10277 to ensure we remove any arrow that should no longer exist. */
10278 if (overlay_arrows_changed_p ())
10279 consider_all_windows_p = windows_or_buffers_changed = 1;
10281 /* Normally the message* functions will have already displayed and
10282 updated the echo area, but the frame may have been trashed, or
10283 the update may have been preempted, so display the echo area
10284 again here. Checking message_cleared_p captures the case that
10285 the echo area should be cleared. */
10286 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10287 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10288 || (message_cleared_p
10289 && minibuf_level == 0
10290 /* If the mini-window is currently selected, this means the
10291 echo-area doesn't show through. */
10292 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10294 int window_height_changed_p = echo_area_display (0);
10295 must_finish = 1;
10297 /* If we don't display the current message, don't clear the
10298 message_cleared_p flag, because, if we did, we wouldn't clear
10299 the echo area in the next redisplay which doesn't preserve
10300 the echo area. */
10301 if (!display_last_displayed_message_p)
10302 message_cleared_p = 0;
10304 if (fonts_changed_p)
10305 goto retry;
10306 else if (window_height_changed_p)
10308 consider_all_windows_p = 1;
10309 ++update_mode_lines;
10310 ++windows_or_buffers_changed;
10312 /* If window configuration was changed, frames may have been
10313 marked garbaged. Clear them or we will experience
10314 surprises wrt scrolling. */
10315 if (frame_garbaged)
10316 clear_garbaged_frames ();
10319 else if (EQ (selected_window, minibuf_window)
10320 && (current_buffer->clip_changed
10321 || XFASTINT (w->last_modified) < MODIFF
10322 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10323 && resize_mini_window (w, 0))
10325 /* Resized active mini-window to fit the size of what it is
10326 showing if its contents might have changed. */
10327 must_finish = 1;
10328 consider_all_windows_p = 1;
10329 ++windows_or_buffers_changed;
10330 ++update_mode_lines;
10332 /* If window configuration was changed, frames may have been
10333 marked garbaged. Clear them or we will experience
10334 surprises wrt scrolling. */
10335 if (frame_garbaged)
10336 clear_garbaged_frames ();
10340 /* If showing the region, and mark has changed, we must redisplay
10341 the whole window. The assignment to this_line_start_pos prevents
10342 the optimization directly below this if-statement. */
10343 if (((!NILP (Vtransient_mark_mode)
10344 && !NILP (XBUFFER (w->buffer)->mark_active))
10345 != !NILP (w->region_showing))
10346 || (!NILP (w->region_showing)
10347 && !EQ (w->region_showing,
10348 Fmarker_position (XBUFFER (w->buffer)->mark))))
10349 CHARPOS (this_line_start_pos) = 0;
10351 /* Optimize the case that only the line containing the cursor in the
10352 selected window has changed. Variables starting with this_ are
10353 set in display_line and record information about the line
10354 containing the cursor. */
10355 tlbufpos = this_line_start_pos;
10356 tlendpos = this_line_end_pos;
10357 if (!consider_all_windows_p
10358 && CHARPOS (tlbufpos) > 0
10359 && NILP (w->update_mode_line)
10360 && !current_buffer->clip_changed
10361 && !current_buffer->prevent_redisplay_optimizations_p
10362 && FRAME_VISIBLE_P (XFRAME (w->frame))
10363 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10364 /* Make sure recorded data applies to current buffer, etc. */
10365 && this_line_buffer == current_buffer
10366 && current_buffer == XBUFFER (w->buffer)
10367 && NILP (w->force_start)
10368 && NILP (w->optional_new_start)
10369 /* Point must be on the line that we have info recorded about. */
10370 && PT >= CHARPOS (tlbufpos)
10371 && PT <= Z - CHARPOS (tlendpos)
10372 /* All text outside that line, including its final newline,
10373 must be unchanged */
10374 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10375 CHARPOS (tlendpos)))
10377 if (CHARPOS (tlbufpos) > BEGV
10378 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10379 && (CHARPOS (tlbufpos) == ZV
10380 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10381 /* Former continuation line has disappeared by becoming empty */
10382 goto cancel;
10383 else if (XFASTINT (w->last_modified) < MODIFF
10384 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10385 || MINI_WINDOW_P (w))
10387 /* We have to handle the case of continuation around a
10388 wide-column character (See the comment in indent.c around
10389 line 885).
10391 For instance, in the following case:
10393 -------- Insert --------
10394 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10395 J_I_ ==> J_I_ `^^' are cursors.
10396 ^^ ^^
10397 -------- --------
10399 As we have to redraw the line above, we should goto cancel. */
10401 struct it it;
10402 int line_height_before = this_line_pixel_height;
10404 /* Note that start_display will handle the case that the
10405 line starting at tlbufpos is a continuation lines. */
10406 start_display (&it, w, tlbufpos);
10408 /* Implementation note: It this still necessary? */
10409 if (it.current_x != this_line_start_x)
10410 goto cancel;
10412 TRACE ((stderr, "trying display optimization 1\n"));
10413 w->cursor.vpos = -1;
10414 overlay_arrow_seen = 0;
10415 it.vpos = this_line_vpos;
10416 it.current_y = this_line_y;
10417 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10418 display_line (&it);
10420 /* If line contains point, is not continued,
10421 and ends at same distance from eob as before, we win */
10422 if (w->cursor.vpos >= 0
10423 /* Line is not continued, otherwise this_line_start_pos
10424 would have been set to 0 in display_line. */
10425 && CHARPOS (this_line_start_pos)
10426 /* Line ends as before. */
10427 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10428 /* Line has same height as before. Otherwise other lines
10429 would have to be shifted up or down. */
10430 && this_line_pixel_height == line_height_before)
10432 /* If this is not the window's last line, we must adjust
10433 the charstarts of the lines below. */
10434 if (it.current_y < it.last_visible_y)
10436 struct glyph_row *row
10437 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10438 int delta, delta_bytes;
10440 if (Z - CHARPOS (tlendpos) == ZV)
10442 /* This line ends at end of (accessible part of)
10443 buffer. There is no newline to count. */
10444 delta = (Z
10445 - CHARPOS (tlendpos)
10446 - MATRIX_ROW_START_CHARPOS (row));
10447 delta_bytes = (Z_BYTE
10448 - BYTEPOS (tlendpos)
10449 - MATRIX_ROW_START_BYTEPOS (row));
10451 else
10453 /* This line ends in a newline. Must take
10454 account of the newline and the rest of the
10455 text that follows. */
10456 delta = (Z
10457 - CHARPOS (tlendpos)
10458 - MATRIX_ROW_START_CHARPOS (row));
10459 delta_bytes = (Z_BYTE
10460 - BYTEPOS (tlendpos)
10461 - MATRIX_ROW_START_BYTEPOS (row));
10464 increment_matrix_positions (w->current_matrix,
10465 this_line_vpos + 1,
10466 w->current_matrix->nrows,
10467 delta, delta_bytes);
10470 /* If this row displays text now but previously didn't,
10471 or vice versa, w->window_end_vpos may have to be
10472 adjusted. */
10473 if ((it.glyph_row - 1)->displays_text_p)
10475 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10476 XSETINT (w->window_end_vpos, this_line_vpos);
10478 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10479 && this_line_vpos > 0)
10480 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10481 w->window_end_valid = Qnil;
10483 /* Update hint: No need to try to scroll in update_window. */
10484 w->desired_matrix->no_scrolling_p = 1;
10486 #if GLYPH_DEBUG
10487 *w->desired_matrix->method = 0;
10488 debug_method_add (w, "optimization 1");
10489 #endif
10490 #ifdef HAVE_WINDOW_SYSTEM
10491 update_window_fringes (w, 0);
10492 #endif
10493 goto update;
10495 else
10496 goto cancel;
10498 else if (/* Cursor position hasn't changed. */
10499 PT == XFASTINT (w->last_point)
10500 /* Make sure the cursor was last displayed
10501 in this window. Otherwise we have to reposition it. */
10502 && 0 <= w->cursor.vpos
10503 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10505 if (!must_finish)
10507 do_pending_window_change (1);
10509 /* We used to always goto end_of_redisplay here, but this
10510 isn't enough if we have a blinking cursor. */
10511 if (w->cursor_off_p == w->last_cursor_off_p)
10512 goto end_of_redisplay;
10514 goto update;
10516 /* If highlighting the region, or if the cursor is in the echo area,
10517 then we can't just move the cursor. */
10518 else if (! (!NILP (Vtransient_mark_mode)
10519 && !NILP (current_buffer->mark_active))
10520 && (EQ (selected_window, current_buffer->last_selected_window)
10521 || highlight_nonselected_windows)
10522 && NILP (w->region_showing)
10523 && NILP (Vshow_trailing_whitespace)
10524 && !cursor_in_echo_area)
10526 struct it it;
10527 struct glyph_row *row;
10529 /* Skip from tlbufpos to PT and see where it is. Note that
10530 PT may be in invisible text. If so, we will end at the
10531 next visible position. */
10532 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10533 NULL, DEFAULT_FACE_ID);
10534 it.current_x = this_line_start_x;
10535 it.current_y = this_line_y;
10536 it.vpos = this_line_vpos;
10538 /* The call to move_it_to stops in front of PT, but
10539 moves over before-strings. */
10540 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10542 if (it.vpos == this_line_vpos
10543 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10544 row->enabled_p))
10546 xassert (this_line_vpos == it.vpos);
10547 xassert (this_line_y == it.current_y);
10548 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10549 #if GLYPH_DEBUG
10550 *w->desired_matrix->method = 0;
10551 debug_method_add (w, "optimization 3");
10552 #endif
10553 goto update;
10555 else
10556 goto cancel;
10559 cancel:
10560 /* Text changed drastically or point moved off of line. */
10561 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10564 CHARPOS (this_line_start_pos) = 0;
10565 consider_all_windows_p |= buffer_shared > 1;
10566 ++clear_face_cache_count;
10567 #ifdef HAVE_WINDOW_SYSTEM
10568 ++clear_image_cache_count;
10569 #endif
10571 /* Build desired matrices, and update the display. If
10572 consider_all_windows_p is non-zero, do it for all windows on all
10573 frames. Otherwise do it for selected_window, only. */
10575 if (consider_all_windows_p)
10577 Lisp_Object tail, frame;
10578 int i, n = 0, size = 50;
10579 struct frame **updated
10580 = (struct frame **) alloca (size * sizeof *updated);
10582 /* Recompute # windows showing selected buffer. This will be
10583 incremented each time such a window is displayed. */
10584 buffer_shared = 0;
10586 FOR_EACH_FRAME (tail, frame)
10588 struct frame *f = XFRAME (frame);
10590 if (FRAME_WINDOW_P (f) || f == sf)
10592 if (! EQ (frame, selected_frame))
10593 /* Select the frame, for the sake of frame-local
10594 variables. */
10595 select_frame_for_redisplay (frame);
10597 /* Mark all the scroll bars to be removed; we'll redeem
10598 the ones we want when we redisplay their windows. */
10599 if (condemn_scroll_bars_hook)
10600 condemn_scroll_bars_hook (f);
10602 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10603 redisplay_windows (FRAME_ROOT_WINDOW (f));
10605 /* Any scroll bars which redisplay_windows should have
10606 nuked should now go away. */
10607 if (judge_scroll_bars_hook)
10608 judge_scroll_bars_hook (f);
10610 /* If fonts changed, display again. */
10611 /* ??? rms: I suspect it is a mistake to jump all the way
10612 back to retry here. It should just retry this frame. */
10613 if (fonts_changed_p)
10614 goto retry;
10616 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10618 /* See if we have to hscroll. */
10619 if (!f->already_hscrolled_p)
10621 f->already_hscrolled_p = 1;
10622 if (hscroll_windows (f->root_window))
10623 goto retry;
10626 /* Prevent various kinds of signals during display
10627 update. stdio is not robust about handling
10628 signals, which can cause an apparent I/O
10629 error. */
10630 if (interrupt_input)
10631 unrequest_sigio ();
10632 STOP_POLLING;
10634 /* Update the display. */
10635 set_window_update_flags (XWINDOW (f->root_window), 1);
10636 pause |= update_frame (f, 0, 0);
10637 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10638 if (pause)
10639 break;
10640 #endif
10642 if (n == size)
10644 int nbytes = size * sizeof *updated;
10645 struct frame **p = (struct frame **) alloca (2 * nbytes);
10646 bcopy (updated, p, nbytes);
10647 size *= 2;
10650 updated[n++] = f;
10655 if (!pause)
10657 /* Do the mark_window_display_accurate after all windows have
10658 been redisplayed because this call resets flags in buffers
10659 which are needed for proper redisplay. */
10660 for (i = 0; i < n; ++i)
10662 struct frame *f = updated[i];
10663 mark_window_display_accurate (f->root_window, 1);
10664 if (frame_up_to_date_hook)
10665 frame_up_to_date_hook (f);
10669 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10671 Lisp_Object mini_window;
10672 struct frame *mini_frame;
10674 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10675 /* Use list_of_error, not Qerror, so that
10676 we catch only errors and don't run the debugger. */
10677 internal_condition_case_1 (redisplay_window_1, selected_window,
10678 list_of_error,
10679 redisplay_window_error);
10681 /* Compare desired and current matrices, perform output. */
10683 update:
10684 /* If fonts changed, display again. */
10685 if (fonts_changed_p)
10686 goto retry;
10688 /* Prevent various kinds of signals during display update.
10689 stdio is not robust about handling signals,
10690 which can cause an apparent I/O error. */
10691 if (interrupt_input)
10692 unrequest_sigio ();
10693 STOP_POLLING;
10695 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10697 if (hscroll_windows (selected_window))
10698 goto retry;
10700 XWINDOW (selected_window)->must_be_updated_p = 1;
10701 pause = update_frame (sf, 0, 0);
10704 /* We may have called echo_area_display at the top of this
10705 function. If the echo area is on another frame, that may
10706 have put text on a frame other than the selected one, so the
10707 above call to update_frame would not have caught it. Catch
10708 it here. */
10709 mini_window = FRAME_MINIBUF_WINDOW (sf);
10710 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10712 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10714 XWINDOW (mini_window)->must_be_updated_p = 1;
10715 pause |= update_frame (mini_frame, 0, 0);
10716 if (!pause && hscroll_windows (mini_window))
10717 goto retry;
10721 /* If display was paused because of pending input, make sure we do a
10722 thorough update the next time. */
10723 if (pause)
10725 /* Prevent the optimization at the beginning of
10726 redisplay_internal that tries a single-line update of the
10727 line containing the cursor in the selected window. */
10728 CHARPOS (this_line_start_pos) = 0;
10730 /* Let the overlay arrow be updated the next time. */
10731 update_overlay_arrows (0);
10733 /* If we pause after scrolling, some rows in the current
10734 matrices of some windows are not valid. */
10735 if (!WINDOW_FULL_WIDTH_P (w)
10736 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10737 update_mode_lines = 1;
10739 else
10741 if (!consider_all_windows_p)
10743 /* This has already been done above if
10744 consider_all_windows_p is set. */
10745 mark_window_display_accurate_1 (w, 1);
10747 /* Say overlay arrows are up to date. */
10748 update_overlay_arrows (1);
10750 if (frame_up_to_date_hook != 0)
10751 frame_up_to_date_hook (sf);
10754 update_mode_lines = 0;
10755 windows_or_buffers_changed = 0;
10756 cursor_type_changed = 0;
10759 /* Start SIGIO interrupts coming again. Having them off during the
10760 code above makes it less likely one will discard output, but not
10761 impossible, since there might be stuff in the system buffer here.
10762 But it is much hairier to try to do anything about that. */
10763 if (interrupt_input)
10764 request_sigio ();
10765 RESUME_POLLING;
10767 /* If a frame has become visible which was not before, redisplay
10768 again, so that we display it. Expose events for such a frame
10769 (which it gets when becoming visible) don't call the parts of
10770 redisplay constructing glyphs, so simply exposing a frame won't
10771 display anything in this case. So, we have to display these
10772 frames here explicitly. */
10773 if (!pause)
10775 Lisp_Object tail, frame;
10776 int new_count = 0;
10778 FOR_EACH_FRAME (tail, frame)
10780 int this_is_visible = 0;
10782 if (XFRAME (frame)->visible)
10783 this_is_visible = 1;
10784 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10785 if (XFRAME (frame)->visible)
10786 this_is_visible = 1;
10788 if (this_is_visible)
10789 new_count++;
10792 if (new_count != number_of_visible_frames)
10793 windows_or_buffers_changed++;
10796 /* Change frame size now if a change is pending. */
10797 do_pending_window_change (1);
10799 /* If we just did a pending size change, or have additional
10800 visible frames, redisplay again. */
10801 if (windows_or_buffers_changed && !pause)
10802 goto retry;
10804 /* Clear the face cache eventually. */
10805 if (consider_all_windows_p)
10807 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10809 clear_face_cache (0);
10810 clear_face_cache_count = 0;
10812 #ifdef HAVE_WINDOW_SYSTEM
10813 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10815 Lisp_Object tail, frame;
10816 FOR_EACH_FRAME (tail, frame)
10818 struct frame *f = XFRAME (frame);
10819 if (FRAME_WINDOW_P (f))
10820 clear_image_cache (f, 0);
10822 clear_image_cache_count = 0;
10824 #endif /* HAVE_WINDOW_SYSTEM */
10827 end_of_redisplay:
10828 unbind_to (count, Qnil);
10829 RESUME_POLLING;
10833 /* Redisplay, but leave alone any recent echo area message unless
10834 another message has been requested in its place.
10836 This is useful in situations where you need to redisplay but no
10837 user action has occurred, making it inappropriate for the message
10838 area to be cleared. See tracking_off and
10839 wait_reading_process_output for examples of these situations.
10841 FROM_WHERE is an integer saying from where this function was
10842 called. This is useful for debugging. */
10844 void
10845 redisplay_preserve_echo_area (from_where)
10846 int from_where;
10848 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10850 if (!NILP (echo_area_buffer[1]))
10852 /* We have a previously displayed message, but no current
10853 message. Redisplay the previous message. */
10854 display_last_displayed_message_p = 1;
10855 redisplay_internal (1);
10856 display_last_displayed_message_p = 0;
10858 else
10859 redisplay_internal (1);
10861 if (rif != NULL && rif->flush_display_optional)
10862 rif->flush_display_optional (NULL);
10866 /* Function registered with record_unwind_protect in
10867 redisplay_internal. Reset redisplaying_p to the value it had
10868 before redisplay_internal was called, and clear
10869 prevent_freeing_realized_faces_p. It also selects the previously
10870 selected frame. */
10872 static Lisp_Object
10873 unwind_redisplay (val)
10874 Lisp_Object val;
10876 Lisp_Object old_redisplaying_p, old_frame;
10878 old_redisplaying_p = XCAR (val);
10879 redisplaying_p = XFASTINT (old_redisplaying_p);
10880 old_frame = XCDR (val);
10881 if (! EQ (old_frame, selected_frame))
10882 select_frame_for_redisplay (old_frame);
10883 return Qnil;
10887 /* Mark the display of window W as accurate or inaccurate. If
10888 ACCURATE_P is non-zero mark display of W as accurate. If
10889 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10890 redisplay_internal is called. */
10892 static void
10893 mark_window_display_accurate_1 (w, accurate_p)
10894 struct window *w;
10895 int accurate_p;
10897 if (BUFFERP (w->buffer))
10899 struct buffer *b = XBUFFER (w->buffer);
10901 w->last_modified
10902 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10903 w->last_overlay_modified
10904 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10905 w->last_had_star
10906 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10908 if (accurate_p)
10910 b->clip_changed = 0;
10911 b->prevent_redisplay_optimizations_p = 0;
10913 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10914 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10915 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10916 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10918 w->current_matrix->buffer = b;
10919 w->current_matrix->begv = BUF_BEGV (b);
10920 w->current_matrix->zv = BUF_ZV (b);
10922 w->last_cursor = w->cursor;
10923 w->last_cursor_off_p = w->cursor_off_p;
10925 if (w == XWINDOW (selected_window))
10926 w->last_point = make_number (BUF_PT (b));
10927 else
10928 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10932 if (accurate_p)
10934 w->window_end_valid = w->buffer;
10935 #if 0 /* This is incorrect with variable-height lines. */
10936 xassert (XINT (w->window_end_vpos)
10937 < (WINDOW_TOTAL_LINES (w)
10938 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10939 #endif
10940 w->update_mode_line = Qnil;
10945 /* Mark the display of windows in the window tree rooted at WINDOW as
10946 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10947 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10948 be redisplayed the next time redisplay_internal is called. */
10950 void
10951 mark_window_display_accurate (window, accurate_p)
10952 Lisp_Object window;
10953 int accurate_p;
10955 struct window *w;
10957 for (; !NILP (window); window = w->next)
10959 w = XWINDOW (window);
10960 mark_window_display_accurate_1 (w, accurate_p);
10962 if (!NILP (w->vchild))
10963 mark_window_display_accurate (w->vchild, accurate_p);
10964 if (!NILP (w->hchild))
10965 mark_window_display_accurate (w->hchild, accurate_p);
10968 if (accurate_p)
10970 update_overlay_arrows (1);
10972 else
10974 /* Force a thorough redisplay the next time by setting
10975 last_arrow_position and last_arrow_string to t, which is
10976 unequal to any useful value of Voverlay_arrow_... */
10977 update_overlay_arrows (-1);
10982 /* Return value in display table DP (Lisp_Char_Table *) for character
10983 C. Since a display table doesn't have any parent, we don't have to
10984 follow parent. Do not call this function directly but use the
10985 macro DISP_CHAR_VECTOR. */
10987 Lisp_Object
10988 disp_char_vector (dp, c)
10989 struct Lisp_Char_Table *dp;
10990 int c;
10992 int code[4], i;
10993 Lisp_Object val;
10995 if (SINGLE_BYTE_CHAR_P (c))
10996 return (dp->contents[c]);
10998 SPLIT_CHAR (c, code[0], code[1], code[2]);
10999 if (code[1] < 32)
11000 code[1] = -1;
11001 else if (code[2] < 32)
11002 code[2] = -1;
11004 /* Here, the possible range of code[0] (== charset ID) is
11005 128..max_charset. Since the top level char table contains data
11006 for multibyte characters after 256th element, we must increment
11007 code[0] by 128 to get a correct index. */
11008 code[0] += 128;
11009 code[3] = -1; /* anchor */
11011 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11013 val = dp->contents[code[i]];
11014 if (!SUB_CHAR_TABLE_P (val))
11015 return (NILP (val) ? dp->defalt : val);
11018 /* Here, val is a sub char table. We return the default value of
11019 it. */
11020 return (dp->defalt);
11025 /***********************************************************************
11026 Window Redisplay
11027 ***********************************************************************/
11029 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11031 static void
11032 redisplay_windows (window)
11033 Lisp_Object window;
11035 while (!NILP (window))
11037 struct window *w = XWINDOW (window);
11039 if (!NILP (w->hchild))
11040 redisplay_windows (w->hchild);
11041 else if (!NILP (w->vchild))
11042 redisplay_windows (w->vchild);
11043 else
11045 displayed_buffer = XBUFFER (w->buffer);
11046 /* Use list_of_error, not Qerror, so that
11047 we catch only errors and don't run the debugger. */
11048 internal_condition_case_1 (redisplay_window_0, window,
11049 list_of_error,
11050 redisplay_window_error);
11053 window = w->next;
11057 static Lisp_Object
11058 redisplay_window_error ()
11060 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11061 return Qnil;
11064 static Lisp_Object
11065 redisplay_window_0 (window)
11066 Lisp_Object window;
11068 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11069 redisplay_window (window, 0);
11070 return Qnil;
11073 static Lisp_Object
11074 redisplay_window_1 (window)
11075 Lisp_Object window;
11077 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11078 redisplay_window (window, 1);
11079 return Qnil;
11083 /* Increment GLYPH until it reaches END or CONDITION fails while
11084 adding (GLYPH)->pixel_width to X. */
11086 #define SKIP_GLYPHS(glyph, end, x, condition) \
11087 do \
11089 (x) += (glyph)->pixel_width; \
11090 ++(glyph); \
11092 while ((glyph) < (end) && (condition))
11095 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11096 DELTA is the number of bytes by which positions recorded in ROW
11097 differ from current buffer positions. */
11099 void
11100 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11101 struct window *w;
11102 struct glyph_row *row;
11103 struct glyph_matrix *matrix;
11104 int delta, delta_bytes, dy, dvpos;
11106 struct glyph *glyph = row->glyphs[TEXT_AREA];
11107 struct glyph *end = glyph + row->used[TEXT_AREA];
11108 struct glyph *cursor = NULL;
11109 /* The first glyph that starts a sequence of glyphs from string. */
11110 struct glyph *string_start;
11111 /* The X coordinate of string_start. */
11112 int string_start_x;
11113 /* The last known character position. */
11114 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11115 /* The last known character position before string_start. */
11116 int string_before_pos;
11117 int x = row->x;
11118 int cursor_x = x;
11119 int cursor_from_overlay_pos = 0;
11120 int pt_old = PT - delta;
11122 /* Skip over glyphs not having an object at the start of the row.
11123 These are special glyphs like truncation marks on terminal
11124 frames. */
11125 if (row->displays_text_p)
11126 while (glyph < end
11127 && INTEGERP (glyph->object)
11128 && glyph->charpos < 0)
11130 x += glyph->pixel_width;
11131 ++glyph;
11134 string_start = NULL;
11135 while (glyph < end
11136 && !INTEGERP (glyph->object)
11137 && (!BUFFERP (glyph->object)
11138 || (last_pos = glyph->charpos) < pt_old))
11140 if (! STRINGP (glyph->object))
11142 string_start = NULL;
11143 x += glyph->pixel_width;
11144 ++glyph;
11145 if (cursor_from_overlay_pos
11146 && last_pos > cursor_from_overlay_pos)
11148 cursor_from_overlay_pos = 0;
11149 cursor = 0;
11152 else
11154 string_before_pos = last_pos;
11155 string_start = glyph;
11156 string_start_x = x;
11157 /* Skip all glyphs from string. */
11160 int pos;
11161 if ((cursor == NULL || glyph > cursor)
11162 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11163 Qcursor, (glyph)->object))
11164 && (pos = string_buffer_position (w, glyph->object,
11165 string_before_pos),
11166 (pos == 0 /* From overlay */
11167 || pos == pt_old)))
11169 /* Estimate overlay buffer position from the buffer
11170 positions of the glyphs before and after the overlay.
11171 Add 1 to last_pos so that if point corresponds to the
11172 glyph right after the overlay, we still use a 'cursor'
11173 property found in that overlay. */
11174 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11175 cursor = glyph;
11176 cursor_x = x;
11178 x += glyph->pixel_width;
11179 ++glyph;
11181 while (glyph < end && STRINGP (glyph->object));
11185 if (cursor != NULL)
11187 glyph = cursor;
11188 x = cursor_x;
11190 else if (row->ends_in_ellipsis_p && glyph == end)
11192 /* Scan back over the ellipsis glyphs, decrementing positions. */
11193 while (glyph > row->glyphs[TEXT_AREA]
11194 && (glyph - 1)->charpos == last_pos)
11195 glyph--, x -= glyph->pixel_width;
11196 /* That loop always goes one position too far,
11197 including the glyph before the ellipsis.
11198 So scan forward over that one. */
11199 x += glyph->pixel_width;
11200 glyph++;
11202 else if (string_start
11203 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11205 /* We may have skipped over point because the previous glyphs
11206 are from string. As there's no easy way to know the
11207 character position of the current glyph, find the correct
11208 glyph on point by scanning from string_start again. */
11209 Lisp_Object limit;
11210 Lisp_Object string;
11211 int pos;
11213 limit = make_number (pt_old + 1);
11214 end = glyph;
11215 glyph = string_start;
11216 x = string_start_x;
11217 string = glyph->object;
11218 pos = string_buffer_position (w, string, string_before_pos);
11219 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11220 because we always put cursor after overlay strings. */
11221 while (pos == 0 && glyph < end)
11223 string = glyph->object;
11224 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11225 if (glyph < end)
11226 pos = string_buffer_position (w, glyph->object, string_before_pos);
11229 while (glyph < end)
11231 pos = XINT (Fnext_single_char_property_change
11232 (make_number (pos), Qdisplay, Qnil, limit));
11233 if (pos > pt_old)
11234 break;
11235 /* Skip glyphs from the same string. */
11236 string = glyph->object;
11237 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11238 /* Skip glyphs from an overlay. */
11239 while (glyph < end
11240 && ! string_buffer_position (w, glyph->object, pos))
11242 string = glyph->object;
11243 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11248 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11249 w->cursor.x = x;
11250 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11251 w->cursor.y = row->y + dy;
11253 if (w == XWINDOW (selected_window))
11255 if (!row->continued_p
11256 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11257 && row->x == 0)
11259 this_line_buffer = XBUFFER (w->buffer);
11261 CHARPOS (this_line_start_pos)
11262 = MATRIX_ROW_START_CHARPOS (row) + delta;
11263 BYTEPOS (this_line_start_pos)
11264 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11266 CHARPOS (this_line_end_pos)
11267 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11268 BYTEPOS (this_line_end_pos)
11269 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11271 this_line_y = w->cursor.y;
11272 this_line_pixel_height = row->height;
11273 this_line_vpos = w->cursor.vpos;
11274 this_line_start_x = row->x;
11276 else
11277 CHARPOS (this_line_start_pos) = 0;
11282 /* Run window scroll functions, if any, for WINDOW with new window
11283 start STARTP. Sets the window start of WINDOW to that position.
11285 We assume that the window's buffer is really current. */
11287 static INLINE struct text_pos
11288 run_window_scroll_functions (window, startp)
11289 Lisp_Object window;
11290 struct text_pos startp;
11292 struct window *w = XWINDOW (window);
11293 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11295 if (current_buffer != XBUFFER (w->buffer))
11296 abort ();
11298 if (!NILP (Vwindow_scroll_functions))
11300 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11301 make_number (CHARPOS (startp)));
11302 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11303 /* In case the hook functions switch buffers. */
11304 if (current_buffer != XBUFFER (w->buffer))
11305 set_buffer_internal_1 (XBUFFER (w->buffer));
11308 return startp;
11312 /* Make sure the line containing the cursor is fully visible.
11313 A value of 1 means there is nothing to be done.
11314 (Either the line is fully visible, or it cannot be made so,
11315 or we cannot tell.)
11317 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11318 is higher than window.
11320 A value of 0 means the caller should do scrolling
11321 as if point had gone off the screen. */
11323 static int
11324 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11325 struct window *w;
11326 int force_p;
11328 struct glyph_matrix *matrix;
11329 struct glyph_row *row;
11330 int window_height;
11332 if (!make_cursor_line_fully_visible_p)
11333 return 1;
11335 /* It's not always possible to find the cursor, e.g, when a window
11336 is full of overlay strings. Don't do anything in that case. */
11337 if (w->cursor.vpos < 0)
11338 return 1;
11340 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11341 row = MATRIX_ROW (matrix, w->cursor.vpos);
11343 /* If the cursor row is not partially visible, there's nothing to do. */
11344 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11345 return 1;
11347 /* If the row the cursor is in is taller than the window's height,
11348 it's not clear what to do, so do nothing. */
11349 window_height = window_box_height (w);
11350 if (row->height >= window_height)
11352 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11353 return 1;
11355 return 0;
11357 #if 0
11358 /* This code used to try to scroll the window just enough to make
11359 the line visible. It returned 0 to say that the caller should
11360 allocate larger glyph matrices. */
11362 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11364 int dy = row->height - row->visible_height;
11365 w->vscroll = 0;
11366 w->cursor.y += dy;
11367 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11369 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11371 int dy = - (row->height - row->visible_height);
11372 w->vscroll = dy;
11373 w->cursor.y += dy;
11374 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11377 /* When we change the cursor y-position of the selected window,
11378 change this_line_y as well so that the display optimization for
11379 the cursor line of the selected window in redisplay_internal uses
11380 the correct y-position. */
11381 if (w == XWINDOW (selected_window))
11382 this_line_y = w->cursor.y;
11384 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11385 redisplay with larger matrices. */
11386 if (matrix->nrows < required_matrix_height (w))
11388 fonts_changed_p = 1;
11389 return 0;
11392 return 1;
11393 #endif /* 0 */
11397 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11398 non-zero means only WINDOW is redisplayed in redisplay_internal.
11399 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11400 in redisplay_window to bring a partially visible line into view in
11401 the case that only the cursor has moved.
11403 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11404 last screen line's vertical height extends past the end of the screen.
11406 Value is
11408 1 if scrolling succeeded
11410 0 if scrolling didn't find point.
11412 -1 if new fonts have been loaded so that we must interrupt
11413 redisplay, adjust glyph matrices, and try again. */
11415 enum
11417 SCROLLING_SUCCESS,
11418 SCROLLING_FAILED,
11419 SCROLLING_NEED_LARGER_MATRICES
11422 static int
11423 try_scrolling (window, just_this_one_p, scroll_conservatively,
11424 scroll_step, temp_scroll_step, last_line_misfit)
11425 Lisp_Object window;
11426 int just_this_one_p;
11427 EMACS_INT scroll_conservatively, scroll_step;
11428 int temp_scroll_step;
11429 int last_line_misfit;
11431 struct window *w = XWINDOW (window);
11432 struct frame *f = XFRAME (w->frame);
11433 struct text_pos scroll_margin_pos;
11434 struct text_pos pos;
11435 struct text_pos startp;
11436 struct it it;
11437 Lisp_Object window_end;
11438 int this_scroll_margin;
11439 int dy = 0;
11440 int scroll_max;
11441 int rc;
11442 int amount_to_scroll = 0;
11443 Lisp_Object aggressive;
11444 int height;
11445 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11447 #if GLYPH_DEBUG
11448 debug_method_add (w, "try_scrolling");
11449 #endif
11451 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11453 /* Compute scroll margin height in pixels. We scroll when point is
11454 within this distance from the top or bottom of the window. */
11455 if (scroll_margin > 0)
11457 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11458 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11460 else
11461 this_scroll_margin = 0;
11463 /* Force scroll_conservatively to have a reasonable value so it doesn't
11464 cause an overflow while computing how much to scroll. */
11465 if (scroll_conservatively)
11466 scroll_conservatively = min (scroll_conservatively,
11467 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11469 /* Compute how much we should try to scroll maximally to bring point
11470 into view. */
11471 if (scroll_step || scroll_conservatively || temp_scroll_step)
11472 scroll_max = max (scroll_step,
11473 max (scroll_conservatively, temp_scroll_step));
11474 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11475 || NUMBERP (current_buffer->scroll_up_aggressively))
11476 /* We're trying to scroll because of aggressive scrolling
11477 but no scroll_step is set. Choose an arbitrary one. Maybe
11478 there should be a variable for this. */
11479 scroll_max = 10;
11480 else
11481 scroll_max = 0;
11482 scroll_max *= FRAME_LINE_HEIGHT (f);
11484 /* Decide whether we have to scroll down. Start at the window end
11485 and move this_scroll_margin up to find the position of the scroll
11486 margin. */
11487 window_end = Fwindow_end (window, Qt);
11489 too_near_end:
11491 CHARPOS (scroll_margin_pos) = XINT (window_end);
11492 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11494 if (this_scroll_margin || extra_scroll_margin_lines)
11496 start_display (&it, w, scroll_margin_pos);
11497 if (this_scroll_margin)
11498 move_it_vertically_backward (&it, this_scroll_margin);
11499 if (extra_scroll_margin_lines)
11500 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11501 scroll_margin_pos = it.current.pos;
11504 if (PT >= CHARPOS (scroll_margin_pos))
11506 int y0;
11508 /* Point is in the scroll margin at the bottom of the window, or
11509 below. Compute a new window start that makes point visible. */
11511 /* Compute the distance from the scroll margin to PT.
11512 Give up if the distance is greater than scroll_max. */
11513 start_display (&it, w, scroll_margin_pos);
11514 y0 = it.current_y;
11515 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11516 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11518 /* To make point visible, we have to move the window start
11519 down so that the line the cursor is in is visible, which
11520 means we have to add in the height of the cursor line. */
11521 dy = line_bottom_y (&it) - y0;
11523 if (dy > scroll_max)
11524 return SCROLLING_FAILED;
11526 /* Move the window start down. If scrolling conservatively,
11527 move it just enough down to make point visible. If
11528 scroll_step is set, move it down by scroll_step. */
11529 start_display (&it, w, startp);
11531 if (scroll_conservatively)
11532 /* Set AMOUNT_TO_SCROLL to at least one line,
11533 and at most scroll_conservatively lines. */
11534 amount_to_scroll
11535 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11536 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11537 else if (scroll_step || temp_scroll_step)
11538 amount_to_scroll = scroll_max;
11539 else
11541 aggressive = current_buffer->scroll_up_aggressively;
11542 height = WINDOW_BOX_TEXT_HEIGHT (w);
11543 if (NUMBERP (aggressive))
11545 double float_amount = XFLOATINT (aggressive) * height;
11546 amount_to_scroll = float_amount;
11547 if (amount_to_scroll == 0 && float_amount > 0)
11548 amount_to_scroll = 1;
11552 if (amount_to_scroll <= 0)
11553 return SCROLLING_FAILED;
11555 /* If moving by amount_to_scroll leaves STARTP unchanged,
11556 move it down one screen line. */
11558 move_it_vertically (&it, amount_to_scroll);
11559 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11560 move_it_by_lines (&it, 1, 1);
11561 startp = it.current.pos;
11563 else
11565 /* See if point is inside the scroll margin at the top of the
11566 window. */
11567 scroll_margin_pos = startp;
11568 if (this_scroll_margin)
11570 start_display (&it, w, startp);
11571 move_it_vertically (&it, this_scroll_margin);
11572 scroll_margin_pos = it.current.pos;
11575 if (PT < CHARPOS (scroll_margin_pos))
11577 /* Point is in the scroll margin at the top of the window or
11578 above what is displayed in the window. */
11579 int y0;
11581 /* Compute the vertical distance from PT to the scroll
11582 margin position. Give up if distance is greater than
11583 scroll_max. */
11584 SET_TEXT_POS (pos, PT, PT_BYTE);
11585 start_display (&it, w, pos);
11586 y0 = it.current_y;
11587 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11588 it.last_visible_y, -1,
11589 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11590 dy = it.current_y - y0;
11591 if (dy > scroll_max)
11592 return SCROLLING_FAILED;
11594 /* Compute new window start. */
11595 start_display (&it, w, startp);
11597 if (scroll_conservatively)
11598 amount_to_scroll
11599 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11600 else if (scroll_step || temp_scroll_step)
11601 amount_to_scroll = scroll_max;
11602 else
11604 aggressive = current_buffer->scroll_down_aggressively;
11605 height = WINDOW_BOX_TEXT_HEIGHT (w);
11606 if (NUMBERP (aggressive))
11608 double float_amount = XFLOATINT (aggressive) * height;
11609 amount_to_scroll = float_amount;
11610 if (amount_to_scroll == 0 && float_amount > 0)
11611 amount_to_scroll = 1;
11615 if (amount_to_scroll <= 0)
11616 return SCROLLING_FAILED;
11618 move_it_vertically_backward (&it, amount_to_scroll);
11619 startp = it.current.pos;
11623 /* Run window scroll functions. */
11624 startp = run_window_scroll_functions (window, startp);
11626 /* Display the window. Give up if new fonts are loaded, or if point
11627 doesn't appear. */
11628 if (!try_window (window, startp, 0))
11629 rc = SCROLLING_NEED_LARGER_MATRICES;
11630 else if (w->cursor.vpos < 0)
11632 clear_glyph_matrix (w->desired_matrix);
11633 rc = SCROLLING_FAILED;
11635 else
11637 /* Maybe forget recorded base line for line number display. */
11638 if (!just_this_one_p
11639 || current_buffer->clip_changed
11640 || BEG_UNCHANGED < CHARPOS (startp))
11641 w->base_line_number = Qnil;
11643 /* If cursor ends up on a partially visible line,
11644 treat that as being off the bottom of the screen. */
11645 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11647 clear_glyph_matrix (w->desired_matrix);
11648 ++extra_scroll_margin_lines;
11649 goto too_near_end;
11651 rc = SCROLLING_SUCCESS;
11654 return rc;
11658 /* Compute a suitable window start for window W if display of W starts
11659 on a continuation line. Value is non-zero if a new window start
11660 was computed.
11662 The new window start will be computed, based on W's width, starting
11663 from the start of the continued line. It is the start of the
11664 screen line with the minimum distance from the old start W->start. */
11666 static int
11667 compute_window_start_on_continuation_line (w)
11668 struct window *w;
11670 struct text_pos pos, start_pos;
11671 int window_start_changed_p = 0;
11673 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11675 /* If window start is on a continuation line... Window start may be
11676 < BEGV in case there's invisible text at the start of the
11677 buffer (M-x rmail, for example). */
11678 if (CHARPOS (start_pos) > BEGV
11679 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11681 struct it it;
11682 struct glyph_row *row;
11684 /* Handle the case that the window start is out of range. */
11685 if (CHARPOS (start_pos) < BEGV)
11686 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11687 else if (CHARPOS (start_pos) > ZV)
11688 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11690 /* Find the start of the continued line. This should be fast
11691 because scan_buffer is fast (newline cache). */
11692 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11693 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11694 row, DEFAULT_FACE_ID);
11695 reseat_at_previous_visible_line_start (&it);
11697 /* If the line start is "too far" away from the window start,
11698 say it takes too much time to compute a new window start. */
11699 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11700 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11702 int min_distance, distance;
11704 /* Move forward by display lines to find the new window
11705 start. If window width was enlarged, the new start can
11706 be expected to be > the old start. If window width was
11707 decreased, the new window start will be < the old start.
11708 So, we're looking for the display line start with the
11709 minimum distance from the old window start. */
11710 pos = it.current.pos;
11711 min_distance = INFINITY;
11712 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11713 distance < min_distance)
11715 min_distance = distance;
11716 pos = it.current.pos;
11717 move_it_by_lines (&it, 1, 0);
11720 /* Set the window start there. */
11721 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11722 window_start_changed_p = 1;
11726 return window_start_changed_p;
11730 /* Try cursor movement in case text has not changed in window WINDOW,
11731 with window start STARTP. Value is
11733 CURSOR_MOVEMENT_SUCCESS if successful
11735 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11737 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11738 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11739 we want to scroll as if scroll-step were set to 1. See the code.
11741 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11742 which case we have to abort this redisplay, and adjust matrices
11743 first. */
11745 enum
11747 CURSOR_MOVEMENT_SUCCESS,
11748 CURSOR_MOVEMENT_CANNOT_BE_USED,
11749 CURSOR_MOVEMENT_MUST_SCROLL,
11750 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11753 static int
11754 try_cursor_movement (window, startp, scroll_step)
11755 Lisp_Object window;
11756 struct text_pos startp;
11757 int *scroll_step;
11759 struct window *w = XWINDOW (window);
11760 struct frame *f = XFRAME (w->frame);
11761 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11763 #if GLYPH_DEBUG
11764 if (inhibit_try_cursor_movement)
11765 return rc;
11766 #endif
11768 /* Handle case where text has not changed, only point, and it has
11769 not moved off the frame. */
11770 if (/* Point may be in this window. */
11771 PT >= CHARPOS (startp)
11772 /* Selective display hasn't changed. */
11773 && !current_buffer->clip_changed
11774 /* Function force-mode-line-update is used to force a thorough
11775 redisplay. It sets either windows_or_buffers_changed or
11776 update_mode_lines. So don't take a shortcut here for these
11777 cases. */
11778 && !update_mode_lines
11779 && !windows_or_buffers_changed
11780 && !cursor_type_changed
11781 /* Can't use this case if highlighting a region. When a
11782 region exists, cursor movement has to do more than just
11783 set the cursor. */
11784 && !(!NILP (Vtransient_mark_mode)
11785 && !NILP (current_buffer->mark_active))
11786 && NILP (w->region_showing)
11787 && NILP (Vshow_trailing_whitespace)
11788 /* Right after splitting windows, last_point may be nil. */
11789 && INTEGERP (w->last_point)
11790 /* This code is not used for mini-buffer for the sake of the case
11791 of redisplaying to replace an echo area message; since in
11792 that case the mini-buffer contents per se are usually
11793 unchanged. This code is of no real use in the mini-buffer
11794 since the handling of this_line_start_pos, etc., in redisplay
11795 handles the same cases. */
11796 && !EQ (window, minibuf_window)
11797 /* When splitting windows or for new windows, it happens that
11798 redisplay is called with a nil window_end_vpos or one being
11799 larger than the window. This should really be fixed in
11800 window.c. I don't have this on my list, now, so we do
11801 approximately the same as the old redisplay code. --gerd. */
11802 && INTEGERP (w->window_end_vpos)
11803 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11804 && (FRAME_WINDOW_P (f)
11805 || !overlay_arrow_in_current_buffer_p ()))
11807 int this_scroll_margin, top_scroll_margin;
11808 struct glyph_row *row = NULL;
11810 #if GLYPH_DEBUG
11811 debug_method_add (w, "cursor movement");
11812 #endif
11814 /* Scroll if point within this distance from the top or bottom
11815 of the window. This is a pixel value. */
11816 this_scroll_margin = max (0, scroll_margin);
11817 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11818 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11820 top_scroll_margin = this_scroll_margin;
11821 if (WINDOW_WANTS_HEADER_LINE_P (w))
11822 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11824 /* Start with the row the cursor was displayed during the last
11825 not paused redisplay. Give up if that row is not valid. */
11826 if (w->last_cursor.vpos < 0
11827 || w->last_cursor.vpos >= w->current_matrix->nrows)
11828 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11829 else
11831 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11832 if (row->mode_line_p)
11833 ++row;
11834 if (!row->enabled_p)
11835 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11838 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11840 int scroll_p = 0;
11841 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11843 if (PT > XFASTINT (w->last_point))
11845 /* Point has moved forward. */
11846 while (MATRIX_ROW_END_CHARPOS (row) < PT
11847 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11849 xassert (row->enabled_p);
11850 ++row;
11853 /* The end position of a row equals the start position
11854 of the next row. If PT is there, we would rather
11855 display it in the next line. */
11856 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11857 && MATRIX_ROW_END_CHARPOS (row) == PT
11858 && !cursor_row_p (w, row))
11859 ++row;
11861 /* If within the scroll margin, scroll. Note that
11862 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11863 the next line would be drawn, and that
11864 this_scroll_margin can be zero. */
11865 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11866 || PT > MATRIX_ROW_END_CHARPOS (row)
11867 /* Line is completely visible last line in window
11868 and PT is to be set in the next line. */
11869 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11870 && PT == MATRIX_ROW_END_CHARPOS (row)
11871 && !row->ends_at_zv_p
11872 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11873 scroll_p = 1;
11875 else if (PT < XFASTINT (w->last_point))
11877 /* Cursor has to be moved backward. Note that PT >=
11878 CHARPOS (startp) because of the outer if-statement. */
11879 while (!row->mode_line_p
11880 && (MATRIX_ROW_START_CHARPOS (row) > PT
11881 || (MATRIX_ROW_START_CHARPOS (row) == PT
11882 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11883 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11884 row > w->current_matrix->rows
11885 && (row-1)->ends_in_newline_from_string_p))))
11886 && (row->y > top_scroll_margin
11887 || CHARPOS (startp) == BEGV))
11889 xassert (row->enabled_p);
11890 --row;
11893 /* Consider the following case: Window starts at BEGV,
11894 there is invisible, intangible text at BEGV, so that
11895 display starts at some point START > BEGV. It can
11896 happen that we are called with PT somewhere between
11897 BEGV and START. Try to handle that case. */
11898 if (row < w->current_matrix->rows
11899 || row->mode_line_p)
11901 row = w->current_matrix->rows;
11902 if (row->mode_line_p)
11903 ++row;
11906 /* Due to newlines in overlay strings, we may have to
11907 skip forward over overlay strings. */
11908 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11909 && MATRIX_ROW_END_CHARPOS (row) == PT
11910 && !cursor_row_p (w, row))
11911 ++row;
11913 /* If within the scroll margin, scroll. */
11914 if (row->y < top_scroll_margin
11915 && CHARPOS (startp) != BEGV)
11916 scroll_p = 1;
11918 else
11920 /* Cursor did not move. So don't scroll even if cursor line
11921 is partially visible, as it was so before. */
11922 rc = CURSOR_MOVEMENT_SUCCESS;
11925 if (PT < MATRIX_ROW_START_CHARPOS (row)
11926 || PT > MATRIX_ROW_END_CHARPOS (row))
11928 /* if PT is not in the glyph row, give up. */
11929 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11931 else if (rc != CURSOR_MOVEMENT_SUCCESS
11932 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11933 && make_cursor_line_fully_visible_p)
11935 if (PT == MATRIX_ROW_END_CHARPOS (row)
11936 && !row->ends_at_zv_p
11937 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11938 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11939 else if (row->height > window_box_height (w))
11941 /* If we end up in a partially visible line, let's
11942 make it fully visible, except when it's taller
11943 than the window, in which case we can't do much
11944 about it. */
11945 *scroll_step = 1;
11946 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11948 else
11950 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11951 if (!cursor_row_fully_visible_p (w, 0, 1))
11952 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11953 else
11954 rc = CURSOR_MOVEMENT_SUCCESS;
11957 else if (scroll_p)
11958 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11959 else
11961 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11962 rc = CURSOR_MOVEMENT_SUCCESS;
11967 return rc;
11970 void
11971 set_vertical_scroll_bar (w)
11972 struct window *w;
11974 int start, end, whole;
11976 /* Calculate the start and end positions for the current window.
11977 At some point, it would be nice to choose between scrollbars
11978 which reflect the whole buffer size, with special markers
11979 indicating narrowing, and scrollbars which reflect only the
11980 visible region.
11982 Note that mini-buffers sometimes aren't displaying any text. */
11983 if (!MINI_WINDOW_P (w)
11984 || (w == XWINDOW (minibuf_window)
11985 && NILP (echo_area_buffer[0])))
11987 struct buffer *buf = XBUFFER (w->buffer);
11988 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11989 start = marker_position (w->start) - BUF_BEGV (buf);
11990 /* I don't think this is guaranteed to be right. For the
11991 moment, we'll pretend it is. */
11992 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11994 if (end < start)
11995 end = start;
11996 if (whole < (end - start))
11997 whole = end - start;
11999 else
12000 start = end = whole = 0;
12002 /* Indicate what this scroll bar ought to be displaying now. */
12003 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12007 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12008 selected_window is redisplayed.
12010 We can return without actually redisplaying the window if
12011 fonts_changed_p is nonzero. In that case, redisplay_internal will
12012 retry. */
12014 static void
12015 redisplay_window (window, just_this_one_p)
12016 Lisp_Object window;
12017 int just_this_one_p;
12019 struct window *w = XWINDOW (window);
12020 struct frame *f = XFRAME (w->frame);
12021 struct buffer *buffer = XBUFFER (w->buffer);
12022 struct buffer *old = current_buffer;
12023 struct text_pos lpoint, opoint, startp;
12024 int update_mode_line;
12025 int tem;
12026 struct it it;
12027 /* Record it now because it's overwritten. */
12028 int current_matrix_up_to_date_p = 0;
12029 int used_current_matrix_p = 0;
12030 /* This is less strict than current_matrix_up_to_date_p.
12031 It indictes that the buffer contents and narrowing are unchanged. */
12032 int buffer_unchanged_p = 0;
12033 int temp_scroll_step = 0;
12034 int count = SPECPDL_INDEX ();
12035 int rc;
12036 int centering_position = -1;
12037 int last_line_misfit = 0;
12039 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12040 opoint = lpoint;
12042 /* W must be a leaf window here. */
12043 xassert (!NILP (w->buffer));
12044 #if GLYPH_DEBUG
12045 *w->desired_matrix->method = 0;
12046 #endif
12048 specbind (Qinhibit_point_motion_hooks, Qt);
12050 reconsider_clip_changes (w, buffer);
12052 /* Has the mode line to be updated? */
12053 update_mode_line = (!NILP (w->update_mode_line)
12054 || update_mode_lines
12055 || buffer->clip_changed
12056 || buffer->prevent_redisplay_optimizations_p);
12058 if (MINI_WINDOW_P (w))
12060 if (w == XWINDOW (echo_area_window)
12061 && !NILP (echo_area_buffer[0]))
12063 if (update_mode_line)
12064 /* We may have to update a tty frame's menu bar or a
12065 tool-bar. Example `M-x C-h C-h C-g'. */
12066 goto finish_menu_bars;
12067 else
12068 /* We've already displayed the echo area glyphs in this window. */
12069 goto finish_scroll_bars;
12071 else if ((w != XWINDOW (minibuf_window)
12072 || minibuf_level == 0)
12073 /* When buffer is nonempty, redisplay window normally. */
12074 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12075 /* Quail displays non-mini buffers in minibuffer window.
12076 In that case, redisplay the window normally. */
12077 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12079 /* W is a mini-buffer window, but it's not active, so clear
12080 it. */
12081 int yb = window_text_bottom_y (w);
12082 struct glyph_row *row;
12083 int y;
12085 for (y = 0, row = w->desired_matrix->rows;
12086 y < yb;
12087 y += row->height, ++row)
12088 blank_row (w, row, y);
12089 goto finish_scroll_bars;
12092 clear_glyph_matrix (w->desired_matrix);
12095 /* Otherwise set up data on this window; select its buffer and point
12096 value. */
12097 /* Really select the buffer, for the sake of buffer-local
12098 variables. */
12099 set_buffer_internal_1 (XBUFFER (w->buffer));
12100 SET_TEXT_POS (opoint, PT, PT_BYTE);
12102 current_matrix_up_to_date_p
12103 = (!NILP (w->window_end_valid)
12104 && !current_buffer->clip_changed
12105 && !current_buffer->prevent_redisplay_optimizations_p
12106 && XFASTINT (w->last_modified) >= MODIFF
12107 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12109 buffer_unchanged_p
12110 = (!NILP (w->window_end_valid)
12111 && !current_buffer->clip_changed
12112 && XFASTINT (w->last_modified) >= MODIFF
12113 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12115 /* When windows_or_buffers_changed is non-zero, we can't rely on
12116 the window end being valid, so set it to nil there. */
12117 if (windows_or_buffers_changed)
12119 /* If window starts on a continuation line, maybe adjust the
12120 window start in case the window's width changed. */
12121 if (XMARKER (w->start)->buffer == current_buffer)
12122 compute_window_start_on_continuation_line (w);
12124 w->window_end_valid = Qnil;
12127 /* Some sanity checks. */
12128 CHECK_WINDOW_END (w);
12129 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12130 abort ();
12131 if (BYTEPOS (opoint) < CHARPOS (opoint))
12132 abort ();
12134 /* If %c is in mode line, update it if needed. */
12135 if (!NILP (w->column_number_displayed)
12136 /* This alternative quickly identifies a common case
12137 where no change is needed. */
12138 && !(PT == XFASTINT (w->last_point)
12139 && XFASTINT (w->last_modified) >= MODIFF
12140 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12141 && (XFASTINT (w->column_number_displayed)
12142 != (int) current_column ())) /* iftc */
12143 update_mode_line = 1;
12145 /* Count number of windows showing the selected buffer. An indirect
12146 buffer counts as its base buffer. */
12147 if (!just_this_one_p)
12149 struct buffer *current_base, *window_base;
12150 current_base = current_buffer;
12151 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12152 if (current_base->base_buffer)
12153 current_base = current_base->base_buffer;
12154 if (window_base->base_buffer)
12155 window_base = window_base->base_buffer;
12156 if (current_base == window_base)
12157 buffer_shared++;
12160 /* Point refers normally to the selected window. For any other
12161 window, set up appropriate value. */
12162 if (!EQ (window, selected_window))
12164 int new_pt = XMARKER (w->pointm)->charpos;
12165 int new_pt_byte = marker_byte_position (w->pointm);
12166 if (new_pt < BEGV)
12168 new_pt = BEGV;
12169 new_pt_byte = BEGV_BYTE;
12170 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12172 else if (new_pt > (ZV - 1))
12174 new_pt = ZV;
12175 new_pt_byte = ZV_BYTE;
12176 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12179 /* We don't use SET_PT so that the point-motion hooks don't run. */
12180 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12183 /* If any of the character widths specified in the display table
12184 have changed, invalidate the width run cache. It's true that
12185 this may be a bit late to catch such changes, but the rest of
12186 redisplay goes (non-fatally) haywire when the display table is
12187 changed, so why should we worry about doing any better? */
12188 if (current_buffer->width_run_cache)
12190 struct Lisp_Char_Table *disptab = buffer_display_table ();
12192 if (! disptab_matches_widthtab (disptab,
12193 XVECTOR (current_buffer->width_table)))
12195 invalidate_region_cache (current_buffer,
12196 current_buffer->width_run_cache,
12197 BEG, Z);
12198 recompute_width_table (current_buffer, disptab);
12202 /* If window-start is screwed up, choose a new one. */
12203 if (XMARKER (w->start)->buffer != current_buffer)
12204 goto recenter;
12206 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12208 /* If someone specified a new starting point but did not insist,
12209 check whether it can be used. */
12210 if (!NILP (w->optional_new_start)
12211 && CHARPOS (startp) >= BEGV
12212 && CHARPOS (startp) <= ZV)
12214 w->optional_new_start = Qnil;
12215 start_display (&it, w, startp);
12216 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12217 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12218 if (IT_CHARPOS (it) == PT)
12219 w->force_start = Qt;
12220 /* IT may overshoot PT if text at PT is invisible. */
12221 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12222 w->force_start = Qt;
12227 /* Handle case where place to start displaying has been specified,
12228 unless the specified location is outside the accessible range. */
12229 if (!NILP (w->force_start)
12230 || w->frozen_window_start_p)
12232 /* We set this later on if we have to adjust point. */
12233 int new_vpos = -1;
12234 int val;
12236 w->force_start = Qnil;
12237 w->vscroll = 0;
12238 w->window_end_valid = Qnil;
12240 /* Forget any recorded base line for line number display. */
12241 if (!buffer_unchanged_p)
12242 w->base_line_number = Qnil;
12244 /* Redisplay the mode line. Select the buffer properly for that.
12245 Also, run the hook window-scroll-functions
12246 because we have scrolled. */
12247 /* Note, we do this after clearing force_start because
12248 if there's an error, it is better to forget about force_start
12249 than to get into an infinite loop calling the hook functions
12250 and having them get more errors. */
12251 if (!update_mode_line
12252 || ! NILP (Vwindow_scroll_functions))
12254 update_mode_line = 1;
12255 w->update_mode_line = Qt;
12256 startp = run_window_scroll_functions (window, startp);
12259 w->last_modified = make_number (0);
12260 w->last_overlay_modified = make_number (0);
12261 if (CHARPOS (startp) < BEGV)
12262 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12263 else if (CHARPOS (startp) > ZV)
12264 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12266 /* Redisplay, then check if cursor has been set during the
12267 redisplay. Give up if new fonts were loaded. */
12268 val = try_window (window, startp, 1);
12269 if (!val)
12271 w->force_start = Qt;
12272 clear_glyph_matrix (w->desired_matrix);
12273 goto need_larger_matrices;
12275 /* Point was outside the scroll margins. */
12276 if (val < 0)
12277 new_vpos = window_box_height (w) / 2;
12279 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12281 /* If point does not appear, try to move point so it does
12282 appear. The desired matrix has been built above, so we
12283 can use it here. */
12284 new_vpos = window_box_height (w) / 2;
12287 if (!cursor_row_fully_visible_p (w, 0, 0))
12289 /* Point does appear, but on a line partly visible at end of window.
12290 Move it back to a fully-visible line. */
12291 new_vpos = window_box_height (w);
12294 /* If we need to move point for either of the above reasons,
12295 now actually do it. */
12296 if (new_vpos >= 0)
12298 struct glyph_row *row;
12300 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12301 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12302 ++row;
12304 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12305 MATRIX_ROW_START_BYTEPOS (row));
12307 if (w != XWINDOW (selected_window))
12308 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12309 else if (current_buffer == old)
12310 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12312 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12314 /* If we are highlighting the region, then we just changed
12315 the region, so redisplay to show it. */
12316 if (!NILP (Vtransient_mark_mode)
12317 && !NILP (current_buffer->mark_active))
12319 clear_glyph_matrix (w->desired_matrix);
12320 if (!try_window (window, startp, 0))
12321 goto need_larger_matrices;
12325 #if GLYPH_DEBUG
12326 debug_method_add (w, "forced window start");
12327 #endif
12328 goto done;
12331 /* Handle case where text has not changed, only point, and it has
12332 not moved off the frame, and we are not retrying after hscroll.
12333 (current_matrix_up_to_date_p is nonzero when retrying.) */
12334 if (current_matrix_up_to_date_p
12335 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12336 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12338 switch (rc)
12340 case CURSOR_MOVEMENT_SUCCESS:
12341 used_current_matrix_p = 1;
12342 goto done;
12344 #if 0 /* try_cursor_movement never returns this value. */
12345 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12346 goto need_larger_matrices;
12347 #endif
12349 case CURSOR_MOVEMENT_MUST_SCROLL:
12350 goto try_to_scroll;
12352 default:
12353 abort ();
12356 /* If current starting point was originally the beginning of a line
12357 but no longer is, find a new starting point. */
12358 else if (!NILP (w->start_at_line_beg)
12359 && !(CHARPOS (startp) <= BEGV
12360 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12362 #if GLYPH_DEBUG
12363 debug_method_add (w, "recenter 1");
12364 #endif
12365 goto recenter;
12368 /* Try scrolling with try_window_id. Value is > 0 if update has
12369 been done, it is -1 if we know that the same window start will
12370 not work. It is 0 if unsuccessful for some other reason. */
12371 else if ((tem = try_window_id (w)) != 0)
12373 #if GLYPH_DEBUG
12374 debug_method_add (w, "try_window_id %d", tem);
12375 #endif
12377 if (fonts_changed_p)
12378 goto need_larger_matrices;
12379 if (tem > 0)
12380 goto done;
12382 /* Otherwise try_window_id has returned -1 which means that we
12383 don't want the alternative below this comment to execute. */
12385 else if (CHARPOS (startp) >= BEGV
12386 && CHARPOS (startp) <= ZV
12387 && PT >= CHARPOS (startp)
12388 && (CHARPOS (startp) < ZV
12389 /* Avoid starting at end of buffer. */
12390 || CHARPOS (startp) == BEGV
12391 || (XFASTINT (w->last_modified) >= MODIFF
12392 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12394 #if GLYPH_DEBUG
12395 debug_method_add (w, "same window start");
12396 #endif
12398 /* Try to redisplay starting at same place as before.
12399 If point has not moved off frame, accept the results. */
12400 if (!current_matrix_up_to_date_p
12401 /* Don't use try_window_reusing_current_matrix in this case
12402 because a window scroll function can have changed the
12403 buffer. */
12404 || !NILP (Vwindow_scroll_functions)
12405 || MINI_WINDOW_P (w)
12406 || !(used_current_matrix_p
12407 = try_window_reusing_current_matrix (w)))
12409 IF_DEBUG (debug_method_add (w, "1"));
12410 if (try_window (window, startp, 1) < 0)
12411 /* -1 means we need to scroll.
12412 0 means we need new matrices, but fonts_changed_p
12413 is set in that case, so we will detect it below. */
12414 goto try_to_scroll;
12417 if (fonts_changed_p)
12418 goto need_larger_matrices;
12420 if (w->cursor.vpos >= 0)
12422 if (!just_this_one_p
12423 || current_buffer->clip_changed
12424 || BEG_UNCHANGED < CHARPOS (startp))
12425 /* Forget any recorded base line for line number display. */
12426 w->base_line_number = Qnil;
12428 if (!cursor_row_fully_visible_p (w, 1, 0))
12430 clear_glyph_matrix (w->desired_matrix);
12431 last_line_misfit = 1;
12433 /* Drop through and scroll. */
12434 else
12435 goto done;
12437 else
12438 clear_glyph_matrix (w->desired_matrix);
12441 try_to_scroll:
12443 w->last_modified = make_number (0);
12444 w->last_overlay_modified = make_number (0);
12446 /* Redisplay the mode line. Select the buffer properly for that. */
12447 if (!update_mode_line)
12449 update_mode_line = 1;
12450 w->update_mode_line = Qt;
12453 /* Try to scroll by specified few lines. */
12454 if ((scroll_conservatively
12455 || scroll_step
12456 || temp_scroll_step
12457 || NUMBERP (current_buffer->scroll_up_aggressively)
12458 || NUMBERP (current_buffer->scroll_down_aggressively))
12459 && !current_buffer->clip_changed
12460 && CHARPOS (startp) >= BEGV
12461 && CHARPOS (startp) <= ZV)
12463 /* The function returns -1 if new fonts were loaded, 1 if
12464 successful, 0 if not successful. */
12465 int rc = try_scrolling (window, just_this_one_p,
12466 scroll_conservatively,
12467 scroll_step,
12468 temp_scroll_step, last_line_misfit);
12469 switch (rc)
12471 case SCROLLING_SUCCESS:
12472 goto done;
12474 case SCROLLING_NEED_LARGER_MATRICES:
12475 goto need_larger_matrices;
12477 case SCROLLING_FAILED:
12478 break;
12480 default:
12481 abort ();
12485 /* Finally, just choose place to start which centers point */
12487 recenter:
12488 if (centering_position < 0)
12489 centering_position = window_box_height (w) / 2;
12491 #if GLYPH_DEBUG
12492 debug_method_add (w, "recenter");
12493 #endif
12495 /* w->vscroll = 0; */
12497 /* Forget any previously recorded base line for line number display. */
12498 if (!buffer_unchanged_p)
12499 w->base_line_number = Qnil;
12501 /* Move backward half the height of the window. */
12502 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12503 it.current_y = it.last_visible_y;
12504 move_it_vertically_backward (&it, centering_position);
12505 xassert (IT_CHARPOS (it) >= BEGV);
12507 /* The function move_it_vertically_backward may move over more
12508 than the specified y-distance. If it->w is small, e.g. a
12509 mini-buffer window, we may end up in front of the window's
12510 display area. Start displaying at the start of the line
12511 containing PT in this case. */
12512 if (it.current_y <= 0)
12514 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12515 move_it_vertically_backward (&it, 0);
12516 #if 0
12517 /* I think this assert is bogus if buffer contains
12518 invisible text or images. KFS. */
12519 xassert (IT_CHARPOS (it) <= PT);
12520 #endif
12521 it.current_y = 0;
12524 it.current_x = it.hpos = 0;
12526 /* Set startp here explicitly in case that helps avoid an infinite loop
12527 in case the window-scroll-functions functions get errors. */
12528 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12530 /* Run scroll hooks. */
12531 startp = run_window_scroll_functions (window, it.current.pos);
12533 /* Redisplay the window. */
12534 if (!current_matrix_up_to_date_p
12535 || windows_or_buffers_changed
12536 || cursor_type_changed
12537 /* Don't use try_window_reusing_current_matrix in this case
12538 because it can have changed the buffer. */
12539 || !NILP (Vwindow_scroll_functions)
12540 || !just_this_one_p
12541 || MINI_WINDOW_P (w)
12542 || !(used_current_matrix_p
12543 = try_window_reusing_current_matrix (w)))
12544 try_window (window, startp, 0);
12546 /* If new fonts have been loaded (due to fontsets), give up. We
12547 have to start a new redisplay since we need to re-adjust glyph
12548 matrices. */
12549 if (fonts_changed_p)
12550 goto need_larger_matrices;
12552 /* If cursor did not appear assume that the middle of the window is
12553 in the first line of the window. Do it again with the next line.
12554 (Imagine a window of height 100, displaying two lines of height
12555 60. Moving back 50 from it->last_visible_y will end in the first
12556 line.) */
12557 if (w->cursor.vpos < 0)
12559 if (!NILP (w->window_end_valid)
12560 && PT >= Z - XFASTINT (w->window_end_pos))
12562 clear_glyph_matrix (w->desired_matrix);
12563 move_it_by_lines (&it, 1, 0);
12564 try_window (window, it.current.pos, 0);
12566 else if (PT < IT_CHARPOS (it))
12568 clear_glyph_matrix (w->desired_matrix);
12569 move_it_by_lines (&it, -1, 0);
12570 try_window (window, it.current.pos, 0);
12572 else
12574 /* Not much we can do about it. */
12578 /* Consider the following case: Window starts at BEGV, there is
12579 invisible, intangible text at BEGV, so that display starts at
12580 some point START > BEGV. It can happen that we are called with
12581 PT somewhere between BEGV and START. Try to handle that case. */
12582 if (w->cursor.vpos < 0)
12584 struct glyph_row *row = w->current_matrix->rows;
12585 if (row->mode_line_p)
12586 ++row;
12587 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12590 if (!cursor_row_fully_visible_p (w, 0, 0))
12592 /* If vscroll is enabled, disable it and try again. */
12593 if (w->vscroll)
12595 w->vscroll = 0;
12596 clear_glyph_matrix (w->desired_matrix);
12597 goto recenter;
12600 /* If centering point failed to make the whole line visible,
12601 put point at the top instead. That has to make the whole line
12602 visible, if it can be done. */
12603 if (centering_position == 0)
12604 goto done;
12606 clear_glyph_matrix (w->desired_matrix);
12607 centering_position = 0;
12608 goto recenter;
12611 done:
12613 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12614 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12615 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12616 ? Qt : Qnil);
12618 /* Display the mode line, if we must. */
12619 if ((update_mode_line
12620 /* If window not full width, must redo its mode line
12621 if (a) the window to its side is being redone and
12622 (b) we do a frame-based redisplay. This is a consequence
12623 of how inverted lines are drawn in frame-based redisplay. */
12624 || (!just_this_one_p
12625 && !FRAME_WINDOW_P (f)
12626 && !WINDOW_FULL_WIDTH_P (w))
12627 /* Line number to display. */
12628 || INTEGERP (w->base_line_pos)
12629 /* Column number is displayed and different from the one displayed. */
12630 || (!NILP (w->column_number_displayed)
12631 && (XFASTINT (w->column_number_displayed)
12632 != (int) current_column ()))) /* iftc */
12633 /* This means that the window has a mode line. */
12634 && (WINDOW_WANTS_MODELINE_P (w)
12635 || WINDOW_WANTS_HEADER_LINE_P (w)))
12637 display_mode_lines (w);
12639 /* If mode line height has changed, arrange for a thorough
12640 immediate redisplay using the correct mode line height. */
12641 if (WINDOW_WANTS_MODELINE_P (w)
12642 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12644 fonts_changed_p = 1;
12645 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12646 = DESIRED_MODE_LINE_HEIGHT (w);
12649 /* If top line height has changed, arrange for a thorough
12650 immediate redisplay using the correct mode line height. */
12651 if (WINDOW_WANTS_HEADER_LINE_P (w)
12652 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12654 fonts_changed_p = 1;
12655 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12656 = DESIRED_HEADER_LINE_HEIGHT (w);
12659 if (fonts_changed_p)
12660 goto need_larger_matrices;
12663 if (!line_number_displayed
12664 && !BUFFERP (w->base_line_pos))
12666 w->base_line_pos = Qnil;
12667 w->base_line_number = Qnil;
12670 finish_menu_bars:
12672 /* When we reach a frame's selected window, redo the frame's menu bar. */
12673 if (update_mode_line
12674 && EQ (FRAME_SELECTED_WINDOW (f), window))
12676 int redisplay_menu_p = 0;
12677 int redisplay_tool_bar_p = 0;
12679 if (FRAME_WINDOW_P (f))
12681 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12682 || defined (USE_GTK)
12683 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12684 #else
12685 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12686 #endif
12688 else
12689 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12691 if (redisplay_menu_p)
12692 display_menu_bar (w);
12694 #ifdef HAVE_WINDOW_SYSTEM
12695 #ifdef USE_GTK
12696 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12697 #else
12698 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12699 && (FRAME_TOOL_BAR_LINES (f) > 0
12700 || auto_resize_tool_bars_p);
12702 #endif
12704 if (redisplay_tool_bar_p)
12705 redisplay_tool_bar (f);
12706 #endif
12709 #ifdef HAVE_WINDOW_SYSTEM
12710 if (FRAME_WINDOW_P (f)
12711 && update_window_fringes (w, 0)
12712 && !just_this_one_p
12713 && (used_current_matrix_p || overlay_arrow_seen)
12714 && !w->pseudo_window_p)
12716 update_begin (f);
12717 BLOCK_INPUT;
12718 if (draw_window_fringes (w, 1))
12719 x_draw_vertical_border (w);
12720 UNBLOCK_INPUT;
12721 update_end (f);
12723 #endif /* HAVE_WINDOW_SYSTEM */
12725 /* We go to this label, with fonts_changed_p nonzero,
12726 if it is necessary to try again using larger glyph matrices.
12727 We have to redeem the scroll bar even in this case,
12728 because the loop in redisplay_internal expects that. */
12729 need_larger_matrices:
12731 finish_scroll_bars:
12733 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12735 /* Set the thumb's position and size. */
12736 set_vertical_scroll_bar (w);
12738 /* Note that we actually used the scroll bar attached to this
12739 window, so it shouldn't be deleted at the end of redisplay. */
12740 redeem_scroll_bar_hook (w);
12743 /* Restore current_buffer and value of point in it. */
12744 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12745 set_buffer_internal_1 (old);
12746 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12748 unbind_to (count, Qnil);
12752 /* Build the complete desired matrix of WINDOW with a window start
12753 buffer position POS.
12755 Value is 1 if successful. It is zero if fonts were loaded during
12756 redisplay which makes re-adjusting glyph matrices necessary, and -1
12757 if point would appear in the scroll margins.
12758 (We check that only if CHECK_MARGINS is nonzero. */
12761 try_window (window, pos, check_margins)
12762 Lisp_Object window;
12763 struct text_pos pos;
12764 int check_margins;
12766 struct window *w = XWINDOW (window);
12767 struct it it;
12768 struct glyph_row *last_text_row = NULL;
12770 /* Make POS the new window start. */
12771 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12773 /* Mark cursor position as unknown. No overlay arrow seen. */
12774 w->cursor.vpos = -1;
12775 overlay_arrow_seen = 0;
12777 /* Initialize iterator and info to start at POS. */
12778 start_display (&it, w, pos);
12780 /* Display all lines of W. */
12781 while (it.current_y < it.last_visible_y)
12783 if (display_line (&it))
12784 last_text_row = it.glyph_row - 1;
12785 if (fonts_changed_p)
12786 return 0;
12789 /* Don't let the cursor end in the scroll margins. */
12790 if (check_margins
12791 && !MINI_WINDOW_P (w))
12793 int this_scroll_margin, cursor_height;
12795 this_scroll_margin = max (0, scroll_margin);
12796 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12797 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12798 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12800 if ((w->cursor.y < this_scroll_margin
12801 && CHARPOS (pos) > BEGV)
12802 /* rms: considering make_cursor_line_fully_visible_p here
12803 seems to give wrong results. We don't want to recenter
12804 when the last line is partly visible, we want to allow
12805 that case to be handled in the usual way. */
12806 || (w->cursor.y + 1) > it.last_visible_y)
12808 w->cursor.vpos = -1;
12809 clear_glyph_matrix (w->desired_matrix);
12810 return -1;
12814 /* If bottom moved off end of frame, change mode line percentage. */
12815 if (XFASTINT (w->window_end_pos) <= 0
12816 && Z != IT_CHARPOS (it))
12817 w->update_mode_line = Qt;
12819 /* Set window_end_pos to the offset of the last character displayed
12820 on the window from the end of current_buffer. Set
12821 window_end_vpos to its row number. */
12822 if (last_text_row)
12824 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (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));
12831 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12832 ->displays_text_p);
12834 else
12836 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12837 w->window_end_pos = make_number (Z - ZV);
12838 w->window_end_vpos = make_number (0);
12841 /* But that is not valid info until redisplay finishes. */
12842 w->window_end_valid = Qnil;
12843 return 1;
12848 /************************************************************************
12849 Window redisplay reusing current matrix when buffer has not changed
12850 ************************************************************************/
12852 /* Try redisplay of window W showing an unchanged buffer with a
12853 different window start than the last time it was displayed by
12854 reusing its current matrix. Value is non-zero if successful.
12855 W->start is the new window start. */
12857 static int
12858 try_window_reusing_current_matrix (w)
12859 struct window *w;
12861 struct frame *f = XFRAME (w->frame);
12862 struct glyph_row *row, *bottom_row;
12863 struct it it;
12864 struct run run;
12865 struct text_pos start, new_start;
12866 int nrows_scrolled, i;
12867 struct glyph_row *last_text_row;
12868 struct glyph_row *last_reused_text_row;
12869 struct glyph_row *start_row;
12870 int start_vpos, min_y, max_y;
12872 #if GLYPH_DEBUG
12873 if (inhibit_try_window_reusing)
12874 return 0;
12875 #endif
12877 if (/* This function doesn't handle terminal frames. */
12878 !FRAME_WINDOW_P (f)
12879 /* Don't try to reuse the display if windows have been split
12880 or such. */
12881 || windows_or_buffers_changed
12882 || cursor_type_changed)
12883 return 0;
12885 /* Can't do this if region may have changed. */
12886 if ((!NILP (Vtransient_mark_mode)
12887 && !NILP (current_buffer->mark_active))
12888 || !NILP (w->region_showing)
12889 || !NILP (Vshow_trailing_whitespace))
12890 return 0;
12892 /* If top-line visibility has changed, give up. */
12893 if (WINDOW_WANTS_HEADER_LINE_P (w)
12894 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12895 return 0;
12897 /* Give up if old or new display is scrolled vertically. We could
12898 make this function handle this, but right now it doesn't. */
12899 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12900 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12901 return 0;
12903 /* The variable new_start now holds the new window start. The old
12904 start `start' can be determined from the current matrix. */
12905 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12906 start = start_row->start.pos;
12907 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12909 /* Clear the desired matrix for the display below. */
12910 clear_glyph_matrix (w->desired_matrix);
12912 if (CHARPOS (new_start) <= CHARPOS (start))
12914 int first_row_y;
12916 /* Don't use this method if the display starts with an ellipsis
12917 displayed for invisible text. It's not easy to handle that case
12918 below, and it's certainly not worth the effort since this is
12919 not a frequent case. */
12920 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12921 return 0;
12923 IF_DEBUG (debug_method_add (w, "twu1"));
12925 /* Display up to a row that can be reused. The variable
12926 last_text_row is set to the last row displayed that displays
12927 text. Note that it.vpos == 0 if or if not there is a
12928 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12929 start_display (&it, w, new_start);
12930 first_row_y = it.current_y;
12931 w->cursor.vpos = -1;
12932 last_text_row = last_reused_text_row = NULL;
12934 while (it.current_y < it.last_visible_y
12935 && !fonts_changed_p)
12937 /* If we have reached into the characters in the START row,
12938 that means the line boundaries have changed. So we
12939 can't start copying with the row START. Maybe it will
12940 work to start copying with the following row. */
12941 while (IT_CHARPOS (it) > CHARPOS (start))
12943 /* Advance to the next row as the "start". */
12944 start_row++;
12945 start = start_row->start.pos;
12946 /* If there are no more rows to try, or just one, give up. */
12947 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12948 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12949 || CHARPOS (start) == ZV)
12951 clear_glyph_matrix (w->desired_matrix);
12952 return 0;
12955 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12957 /* If we have reached alignment,
12958 we can copy the rest of the rows. */
12959 if (IT_CHARPOS (it) == CHARPOS (start))
12960 break;
12962 if (display_line (&it))
12963 last_text_row = it.glyph_row - 1;
12966 /* A value of current_y < last_visible_y means that we stopped
12967 at the previous window start, which in turn means that we
12968 have at least one reusable row. */
12969 if (it.current_y < it.last_visible_y)
12971 /* IT.vpos always starts from 0; it counts text lines. */
12972 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12974 /* Find PT if not already found in the lines displayed. */
12975 if (w->cursor.vpos < 0)
12977 int dy = it.current_y - start_row->y;
12979 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12980 row = row_containing_pos (w, PT, row, NULL, dy);
12981 if (row)
12982 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12983 dy, nrows_scrolled);
12984 else
12986 clear_glyph_matrix (w->desired_matrix);
12987 return 0;
12991 /* Scroll the display. Do it before the current matrix is
12992 changed. The problem here is that update has not yet
12993 run, i.e. part of the current matrix is not up to date.
12994 scroll_run_hook will clear the cursor, and use the
12995 current matrix to get the height of the row the cursor is
12996 in. */
12997 run.current_y = start_row->y;
12998 run.desired_y = it.current_y;
12999 run.height = it.last_visible_y - it.current_y;
13001 if (run.height > 0 && run.current_y != run.desired_y)
13003 update_begin (f);
13004 rif->update_window_begin_hook (w);
13005 rif->clear_window_mouse_face (w);
13006 rif->scroll_run_hook (w, &run);
13007 rif->update_window_end_hook (w, 0, 0);
13008 update_end (f);
13011 /* Shift current matrix down by nrows_scrolled lines. */
13012 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13013 rotate_matrix (w->current_matrix,
13014 start_vpos,
13015 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13016 nrows_scrolled);
13018 /* Disable lines that must be updated. */
13019 for (i = 0; i < it.vpos; ++i)
13020 (start_row + i)->enabled_p = 0;
13022 /* Re-compute Y positions. */
13023 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13024 max_y = it.last_visible_y;
13025 for (row = start_row + nrows_scrolled;
13026 row < bottom_row;
13027 ++row)
13029 row->y = it.current_y;
13030 row->visible_height = row->height;
13032 if (row->y < min_y)
13033 row->visible_height -= min_y - row->y;
13034 if (row->y + row->height > max_y)
13035 row->visible_height -= row->y + row->height - max_y;
13036 row->redraw_fringe_bitmaps_p = 1;
13038 it.current_y += row->height;
13040 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13041 last_reused_text_row = row;
13042 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13043 break;
13046 /* Disable lines in the current matrix which are now
13047 below the window. */
13048 for (++row; row < bottom_row; ++row)
13049 row->enabled_p = 0;
13052 /* Update window_end_pos etc.; last_reused_text_row is the last
13053 reused row from the current matrix containing text, if any.
13054 The value of last_text_row is the last displayed line
13055 containing text. */
13056 if (last_reused_text_row)
13058 w->window_end_bytepos
13059 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13060 w->window_end_pos
13061 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13062 w->window_end_vpos
13063 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13064 w->current_matrix));
13066 else if (last_text_row)
13068 w->window_end_bytepos
13069 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13070 w->window_end_pos
13071 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13072 w->window_end_vpos
13073 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13075 else
13077 /* This window must be completely empty. */
13078 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13079 w->window_end_pos = make_number (Z - ZV);
13080 w->window_end_vpos = make_number (0);
13082 w->window_end_valid = Qnil;
13084 /* Update hint: don't try scrolling again in update_window. */
13085 w->desired_matrix->no_scrolling_p = 1;
13087 #if GLYPH_DEBUG
13088 debug_method_add (w, "try_window_reusing_current_matrix 1");
13089 #endif
13090 return 1;
13092 else if (CHARPOS (new_start) > CHARPOS (start))
13094 struct glyph_row *pt_row, *row;
13095 struct glyph_row *first_reusable_row;
13096 struct glyph_row *first_row_to_display;
13097 int dy;
13098 int yb = window_text_bottom_y (w);
13100 /* Find the row starting at new_start, if there is one. Don't
13101 reuse a partially visible line at the end. */
13102 first_reusable_row = start_row;
13103 while (first_reusable_row->enabled_p
13104 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13105 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13106 < CHARPOS (new_start)))
13107 ++first_reusable_row;
13109 /* Give up if there is no row to reuse. */
13110 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13111 || !first_reusable_row->enabled_p
13112 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13113 != CHARPOS (new_start)))
13114 return 0;
13116 /* We can reuse fully visible rows beginning with
13117 first_reusable_row to the end of the window. Set
13118 first_row_to_display to the first row that cannot be reused.
13119 Set pt_row to the row containing point, if there is any. */
13120 pt_row = NULL;
13121 for (first_row_to_display = first_reusable_row;
13122 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13123 ++first_row_to_display)
13125 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13126 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13127 pt_row = first_row_to_display;
13130 /* Start displaying at the start of first_row_to_display. */
13131 xassert (first_row_to_display->y < yb);
13132 init_to_row_start (&it, w, first_row_to_display);
13134 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13135 - start_vpos);
13136 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13137 - nrows_scrolled);
13138 it.current_y = (first_row_to_display->y - first_reusable_row->y
13139 + WINDOW_HEADER_LINE_HEIGHT (w));
13141 /* Display lines beginning with first_row_to_display in the
13142 desired matrix. Set last_text_row to the last row displayed
13143 that displays text. */
13144 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13145 if (pt_row == NULL)
13146 w->cursor.vpos = -1;
13147 last_text_row = NULL;
13148 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13149 if (display_line (&it))
13150 last_text_row = it.glyph_row - 1;
13152 /* Give up If point isn't in a row displayed or reused. */
13153 if (w->cursor.vpos < 0)
13155 clear_glyph_matrix (w->desired_matrix);
13156 return 0;
13159 /* If point is in a reused row, adjust y and vpos of the cursor
13160 position. */
13161 if (pt_row)
13163 w->cursor.vpos -= nrows_scrolled;
13164 w->cursor.y -= first_reusable_row->y - start_row->y;
13167 /* Scroll the display. */
13168 run.current_y = first_reusable_row->y;
13169 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13170 run.height = it.last_visible_y - run.current_y;
13171 dy = run.current_y - run.desired_y;
13173 if (run.height)
13175 update_begin (f);
13176 rif->update_window_begin_hook (w);
13177 rif->clear_window_mouse_face (w);
13178 rif->scroll_run_hook (w, &run);
13179 rif->update_window_end_hook (w, 0, 0);
13180 update_end (f);
13183 /* Adjust Y positions of reused rows. */
13184 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13185 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13186 max_y = it.last_visible_y;
13187 for (row = first_reusable_row; row < first_row_to_display; ++row)
13189 row->y -= dy;
13190 row->visible_height = row->height;
13191 if (row->y < min_y)
13192 row->visible_height -= min_y - row->y;
13193 if (row->y + row->height > max_y)
13194 row->visible_height -= row->y + row->height - max_y;
13195 row->redraw_fringe_bitmaps_p = 1;
13198 /* Scroll the current matrix. */
13199 xassert (nrows_scrolled > 0);
13200 rotate_matrix (w->current_matrix,
13201 start_vpos,
13202 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13203 -nrows_scrolled);
13205 /* Disable rows not reused. */
13206 for (row -= nrows_scrolled; row < bottom_row; ++row)
13207 row->enabled_p = 0;
13209 /* Point may have moved to a different line, so we cannot assume that
13210 the previous cursor position is valid; locate the correct row. */
13211 if (pt_row)
13213 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13214 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13215 row++)
13217 w->cursor.vpos++;
13218 w->cursor.y = row->y;
13220 if (row < bottom_row)
13222 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13223 while (glyph->charpos < PT)
13225 w->cursor.hpos++;
13226 w->cursor.x += glyph->pixel_width;
13227 glyph++;
13232 /* Adjust window end. A null value of last_text_row means that
13233 the window end is in reused rows which in turn means that
13234 only its vpos can have changed. */
13235 if (last_text_row)
13237 w->window_end_bytepos
13238 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13239 w->window_end_pos
13240 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13241 w->window_end_vpos
13242 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13244 else
13246 w->window_end_vpos
13247 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13250 w->window_end_valid = Qnil;
13251 w->desired_matrix->no_scrolling_p = 1;
13253 #if GLYPH_DEBUG
13254 debug_method_add (w, "try_window_reusing_current_matrix 2");
13255 #endif
13256 return 1;
13259 return 0;
13264 /************************************************************************
13265 Window redisplay reusing current matrix when buffer has changed
13266 ************************************************************************/
13268 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13269 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13270 int *, int *));
13271 static struct glyph_row *
13272 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13273 struct glyph_row *));
13276 /* Return the last row in MATRIX displaying text. If row START is
13277 non-null, start searching with that row. IT gives the dimensions
13278 of the display. Value is null if matrix is empty; otherwise it is
13279 a pointer to the row found. */
13281 static struct glyph_row *
13282 find_last_row_displaying_text (matrix, it, start)
13283 struct glyph_matrix *matrix;
13284 struct it *it;
13285 struct glyph_row *start;
13287 struct glyph_row *row, *row_found;
13289 /* Set row_found to the last row in IT->w's current matrix
13290 displaying text. The loop looks funny but think of partially
13291 visible lines. */
13292 row_found = NULL;
13293 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13294 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13296 xassert (row->enabled_p);
13297 row_found = row;
13298 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13299 break;
13300 ++row;
13303 return row_found;
13307 /* Return the last row in the current matrix of W that is not affected
13308 by changes at the start of current_buffer that occurred since W's
13309 current matrix was built. Value is null if no such row exists.
13311 BEG_UNCHANGED us the number of characters unchanged at the start of
13312 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13313 first changed character in current_buffer. Characters at positions <
13314 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13315 when the current matrix was built. */
13317 static struct glyph_row *
13318 find_last_unchanged_at_beg_row (w)
13319 struct window *w;
13321 int first_changed_pos = BEG + BEG_UNCHANGED;
13322 struct glyph_row *row;
13323 struct glyph_row *row_found = NULL;
13324 int yb = window_text_bottom_y (w);
13326 /* Find the last row displaying unchanged text. */
13327 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13328 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13329 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13331 if (/* If row ends before first_changed_pos, it is unchanged,
13332 except in some case. */
13333 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13334 /* When row ends in ZV and we write at ZV it is not
13335 unchanged. */
13336 && !row->ends_at_zv_p
13337 /* When first_changed_pos is the end of a continued line,
13338 row is not unchanged because it may be no longer
13339 continued. */
13340 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13341 && (row->continued_p
13342 || row->exact_window_width_line_p)))
13343 row_found = row;
13345 /* Stop if last visible row. */
13346 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13347 break;
13349 ++row;
13352 return row_found;
13356 /* Find the first glyph row in the current matrix of W that is not
13357 affected by changes at the end of current_buffer since the
13358 time W's current matrix was built.
13360 Return in *DELTA the number of chars by which buffer positions in
13361 unchanged text at the end of current_buffer must be adjusted.
13363 Return in *DELTA_BYTES the corresponding number of bytes.
13365 Value is null if no such row exists, i.e. all rows are affected by
13366 changes. */
13368 static struct glyph_row *
13369 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13370 struct window *w;
13371 int *delta, *delta_bytes;
13373 struct glyph_row *row;
13374 struct glyph_row *row_found = NULL;
13376 *delta = *delta_bytes = 0;
13378 /* Display must not have been paused, otherwise the current matrix
13379 is not up to date. */
13380 if (NILP (w->window_end_valid))
13381 abort ();
13383 /* A value of window_end_pos >= END_UNCHANGED means that the window
13384 end is in the range of changed text. If so, there is no
13385 unchanged row at the end of W's current matrix. */
13386 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13387 return NULL;
13389 /* Set row to the last row in W's current matrix displaying text. */
13390 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13392 /* If matrix is entirely empty, no unchanged row exists. */
13393 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13395 /* The value of row is the last glyph row in the matrix having a
13396 meaningful buffer position in it. The end position of row
13397 corresponds to window_end_pos. This allows us to translate
13398 buffer positions in the current matrix to current buffer
13399 positions for characters not in changed text. */
13400 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13401 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13402 int last_unchanged_pos, last_unchanged_pos_old;
13403 struct glyph_row *first_text_row
13404 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13406 *delta = Z - Z_old;
13407 *delta_bytes = Z_BYTE - Z_BYTE_old;
13409 /* Set last_unchanged_pos to the buffer position of the last
13410 character in the buffer that has not been changed. Z is the
13411 index + 1 of the last character in current_buffer, i.e. by
13412 subtracting END_UNCHANGED we get the index of the last
13413 unchanged character, and we have to add BEG to get its buffer
13414 position. */
13415 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13416 last_unchanged_pos_old = last_unchanged_pos - *delta;
13418 /* Search backward from ROW for a row displaying a line that
13419 starts at a minimum position >= last_unchanged_pos_old. */
13420 for (; row > first_text_row; --row)
13422 /* This used to abort, but it can happen.
13423 It is ok to just stop the search instead here. KFS. */
13424 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13425 break;
13427 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13428 row_found = row;
13432 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13433 abort ();
13435 return row_found;
13439 /* Make sure that glyph rows in the current matrix of window W
13440 reference the same glyph memory as corresponding rows in the
13441 frame's frame matrix. This function is called after scrolling W's
13442 current matrix on a terminal frame in try_window_id and
13443 try_window_reusing_current_matrix. */
13445 static void
13446 sync_frame_with_window_matrix_rows (w)
13447 struct window *w;
13449 struct frame *f = XFRAME (w->frame);
13450 struct glyph_row *window_row, *window_row_end, *frame_row;
13452 /* Preconditions: W must be a leaf window and full-width. Its frame
13453 must have a frame matrix. */
13454 xassert (NILP (w->hchild) && NILP (w->vchild));
13455 xassert (WINDOW_FULL_WIDTH_P (w));
13456 xassert (!FRAME_WINDOW_P (f));
13458 /* If W is a full-width window, glyph pointers in W's current matrix
13459 have, by definition, to be the same as glyph pointers in the
13460 corresponding frame matrix. Note that frame matrices have no
13461 marginal areas (see build_frame_matrix). */
13462 window_row = w->current_matrix->rows;
13463 window_row_end = window_row + w->current_matrix->nrows;
13464 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13465 while (window_row < window_row_end)
13467 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13468 struct glyph *end = window_row->glyphs[LAST_AREA];
13470 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13471 frame_row->glyphs[TEXT_AREA] = start;
13472 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13473 frame_row->glyphs[LAST_AREA] = end;
13475 /* Disable frame rows whose corresponding window rows have
13476 been disabled in try_window_id. */
13477 if (!window_row->enabled_p)
13478 frame_row->enabled_p = 0;
13480 ++window_row, ++frame_row;
13485 /* Find the glyph row in window W containing CHARPOS. Consider all
13486 rows between START and END (not inclusive). END null means search
13487 all rows to the end of the display area of W. Value is the row
13488 containing CHARPOS or null. */
13490 struct glyph_row *
13491 row_containing_pos (w, charpos, start, end, dy)
13492 struct window *w;
13493 int charpos;
13494 struct glyph_row *start, *end;
13495 int dy;
13497 struct glyph_row *row = start;
13498 int last_y;
13500 /* If we happen to start on a header-line, skip that. */
13501 if (row->mode_line_p)
13502 ++row;
13504 if ((end && row >= end) || !row->enabled_p)
13505 return NULL;
13507 last_y = window_text_bottom_y (w) - dy;
13509 while (1)
13511 /* Give up if we have gone too far. */
13512 if (end && row >= end)
13513 return NULL;
13514 /* This formerly returned if they were equal.
13515 I think that both quantities are of a "last plus one" type;
13516 if so, when they are equal, the row is within the screen. -- rms. */
13517 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13518 return NULL;
13520 /* If it is in this row, return this row. */
13521 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13522 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13523 /* The end position of a row equals the start
13524 position of the next row. If CHARPOS is there, we
13525 would rather display it in the next line, except
13526 when this line ends in ZV. */
13527 && !row->ends_at_zv_p
13528 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13529 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13530 return row;
13531 ++row;
13536 /* Try to redisplay window W by reusing its existing display. W's
13537 current matrix must be up to date when this function is called,
13538 i.e. window_end_valid must not be nil.
13540 Value is
13542 1 if display has been updated
13543 0 if otherwise unsuccessful
13544 -1 if redisplay with same window start is known not to succeed
13546 The following steps are performed:
13548 1. Find the last row in the current matrix of W that is not
13549 affected by changes at the start of current_buffer. If no such row
13550 is found, give up.
13552 2. Find the first row in W's current matrix that is not affected by
13553 changes at the end of current_buffer. Maybe there is no such row.
13555 3. Display lines beginning with the row + 1 found in step 1 to the
13556 row found in step 2 or, if step 2 didn't find a row, to the end of
13557 the window.
13559 4. If cursor is not known to appear on the window, give up.
13561 5. If display stopped at the row found in step 2, scroll the
13562 display and current matrix as needed.
13564 6. Maybe display some lines at the end of W, if we must. This can
13565 happen under various circumstances, like a partially visible line
13566 becoming fully visible, or because newly displayed lines are displayed
13567 in smaller font sizes.
13569 7. Update W's window end information. */
13571 static int
13572 try_window_id (w)
13573 struct window *w;
13575 struct frame *f = XFRAME (w->frame);
13576 struct glyph_matrix *current_matrix = w->current_matrix;
13577 struct glyph_matrix *desired_matrix = w->desired_matrix;
13578 struct glyph_row *last_unchanged_at_beg_row;
13579 struct glyph_row *first_unchanged_at_end_row;
13580 struct glyph_row *row;
13581 struct glyph_row *bottom_row;
13582 int bottom_vpos;
13583 struct it it;
13584 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13585 struct text_pos start_pos;
13586 struct run run;
13587 int first_unchanged_at_end_vpos = 0;
13588 struct glyph_row *last_text_row, *last_text_row_at_end;
13589 struct text_pos start;
13590 int first_changed_charpos, last_changed_charpos;
13592 #if GLYPH_DEBUG
13593 if (inhibit_try_window_id)
13594 return 0;
13595 #endif
13597 /* This is handy for debugging. */
13598 #if 0
13599 #define GIVE_UP(X) \
13600 do { \
13601 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13602 return 0; \
13603 } while (0)
13604 #else
13605 #define GIVE_UP(X) return 0
13606 #endif
13608 SET_TEXT_POS_FROM_MARKER (start, w->start);
13610 /* Don't use this for mini-windows because these can show
13611 messages and mini-buffers, and we don't handle that here. */
13612 if (MINI_WINDOW_P (w))
13613 GIVE_UP (1);
13615 /* This flag is used to prevent redisplay optimizations. */
13616 if (windows_or_buffers_changed || cursor_type_changed)
13617 GIVE_UP (2);
13619 /* Verify that narrowing has not changed.
13620 Also verify that we were not told to prevent redisplay optimizations.
13621 It would be nice to further
13622 reduce the number of cases where this prevents try_window_id. */
13623 if (current_buffer->clip_changed
13624 || current_buffer->prevent_redisplay_optimizations_p)
13625 GIVE_UP (3);
13627 /* Window must either use window-based redisplay or be full width. */
13628 if (!FRAME_WINDOW_P (f)
13629 && (!line_ins_del_ok
13630 || !WINDOW_FULL_WIDTH_P (w)))
13631 GIVE_UP (4);
13633 /* Give up if point is not known NOT to appear in W. */
13634 if (PT < CHARPOS (start))
13635 GIVE_UP (5);
13637 /* Another way to prevent redisplay optimizations. */
13638 if (XFASTINT (w->last_modified) == 0)
13639 GIVE_UP (6);
13641 /* Verify that window is not hscrolled. */
13642 if (XFASTINT (w->hscroll) != 0)
13643 GIVE_UP (7);
13645 /* Verify that display wasn't paused. */
13646 if (NILP (w->window_end_valid))
13647 GIVE_UP (8);
13649 /* Can't use this if highlighting a region because a cursor movement
13650 will do more than just set the cursor. */
13651 if (!NILP (Vtransient_mark_mode)
13652 && !NILP (current_buffer->mark_active))
13653 GIVE_UP (9);
13655 /* Likewise if highlighting trailing whitespace. */
13656 if (!NILP (Vshow_trailing_whitespace))
13657 GIVE_UP (11);
13659 /* Likewise if showing a region. */
13660 if (!NILP (w->region_showing))
13661 GIVE_UP (10);
13663 /* Can use this if overlay arrow position and or string have changed. */
13664 if (overlay_arrows_changed_p ())
13665 GIVE_UP (12);
13668 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13669 only if buffer has really changed. The reason is that the gap is
13670 initially at Z for freshly visited files. The code below would
13671 set end_unchanged to 0 in that case. */
13672 if (MODIFF > SAVE_MODIFF
13673 /* This seems to happen sometimes after saving a buffer. */
13674 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13676 if (GPT - BEG < BEG_UNCHANGED)
13677 BEG_UNCHANGED = GPT - BEG;
13678 if (Z - GPT < END_UNCHANGED)
13679 END_UNCHANGED = Z - GPT;
13682 /* The position of the first and last character that has been changed. */
13683 first_changed_charpos = BEG + BEG_UNCHANGED;
13684 last_changed_charpos = Z - END_UNCHANGED;
13686 /* If window starts after a line end, and the last change is in
13687 front of that newline, then changes don't affect the display.
13688 This case happens with stealth-fontification. Note that although
13689 the display is unchanged, glyph positions in the matrix have to
13690 be adjusted, of course. */
13691 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13692 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13693 && ((last_changed_charpos < CHARPOS (start)
13694 && CHARPOS (start) == BEGV)
13695 || (last_changed_charpos < CHARPOS (start) - 1
13696 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13698 int Z_old, delta, Z_BYTE_old, delta_bytes;
13699 struct glyph_row *r0;
13701 /* Compute how many chars/bytes have been added to or removed
13702 from the buffer. */
13703 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13704 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13705 delta = Z - Z_old;
13706 delta_bytes = Z_BYTE - Z_BYTE_old;
13708 /* Give up if PT is not in the window. Note that it already has
13709 been checked at the start of try_window_id that PT is not in
13710 front of the window start. */
13711 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13712 GIVE_UP (13);
13714 /* If window start is unchanged, we can reuse the whole matrix
13715 as is, after adjusting glyph positions. No need to compute
13716 the window end again, since its offset from Z hasn't changed. */
13717 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13718 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13719 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13720 /* PT must not be in a partially visible line. */
13721 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13722 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13724 /* Adjust positions in the glyph matrix. */
13725 if (delta || delta_bytes)
13727 struct glyph_row *r1
13728 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13729 increment_matrix_positions (w->current_matrix,
13730 MATRIX_ROW_VPOS (r0, current_matrix),
13731 MATRIX_ROW_VPOS (r1, current_matrix),
13732 delta, delta_bytes);
13735 /* Set the cursor. */
13736 row = row_containing_pos (w, PT, r0, NULL, 0);
13737 if (row)
13738 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13739 else
13740 abort ();
13741 return 1;
13745 /* Handle the case that changes are all below what is displayed in
13746 the window, and that PT is in the window. This shortcut cannot
13747 be taken if ZV is visible in the window, and text has been added
13748 there that is visible in the window. */
13749 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13750 /* ZV is not visible in the window, or there are no
13751 changes at ZV, actually. */
13752 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13753 || first_changed_charpos == last_changed_charpos))
13755 struct glyph_row *r0;
13757 /* Give up if PT is not in the window. Note that it already has
13758 been checked at the start of try_window_id that PT is not in
13759 front of the window start. */
13760 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13761 GIVE_UP (14);
13763 /* If window start is unchanged, we can reuse the whole matrix
13764 as is, without changing glyph positions since no text has
13765 been added/removed in front of the window end. */
13766 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13767 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13768 /* PT must not be in a partially visible line. */
13769 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13770 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13772 /* We have to compute the window end anew since text
13773 can have been added/removed after it. */
13774 w->window_end_pos
13775 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13776 w->window_end_bytepos
13777 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13779 /* Set the cursor. */
13780 row = row_containing_pos (w, PT, r0, NULL, 0);
13781 if (row)
13782 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13783 else
13784 abort ();
13785 return 2;
13789 /* Give up if window start is in the changed area.
13791 The condition used to read
13793 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13795 but why that was tested escapes me at the moment. */
13796 if (CHARPOS (start) >= first_changed_charpos
13797 && CHARPOS (start) <= last_changed_charpos)
13798 GIVE_UP (15);
13800 /* Check that window start agrees with the start of the first glyph
13801 row in its current matrix. Check this after we know the window
13802 start is not in changed text, otherwise positions would not be
13803 comparable. */
13804 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13805 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13806 GIVE_UP (16);
13808 /* Give up if the window ends in strings. Overlay strings
13809 at the end are difficult to handle, so don't try. */
13810 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13811 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13812 GIVE_UP (20);
13814 /* Compute the position at which we have to start displaying new
13815 lines. Some of the lines at the top of the window might be
13816 reusable because they are not displaying changed text. Find the
13817 last row in W's current matrix not affected by changes at the
13818 start of current_buffer. Value is null if changes start in the
13819 first line of window. */
13820 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13821 if (last_unchanged_at_beg_row)
13823 /* Avoid starting to display in the moddle of a character, a TAB
13824 for instance. This is easier than to set up the iterator
13825 exactly, and it's not a frequent case, so the additional
13826 effort wouldn't really pay off. */
13827 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13828 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13829 && last_unchanged_at_beg_row > w->current_matrix->rows)
13830 --last_unchanged_at_beg_row;
13832 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13833 GIVE_UP (17);
13835 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13836 GIVE_UP (18);
13837 start_pos = it.current.pos;
13839 /* Start displaying new lines in the desired matrix at the same
13840 vpos we would use in the current matrix, i.e. below
13841 last_unchanged_at_beg_row. */
13842 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13843 current_matrix);
13844 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13845 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13847 xassert (it.hpos == 0 && it.current_x == 0);
13849 else
13851 /* There are no reusable lines at the start of the window.
13852 Start displaying in the first text line. */
13853 start_display (&it, w, start);
13854 it.vpos = it.first_vpos;
13855 start_pos = it.current.pos;
13858 /* Find the first row that is not affected by changes at the end of
13859 the buffer. Value will be null if there is no unchanged row, in
13860 which case we must redisplay to the end of the window. delta
13861 will be set to the value by which buffer positions beginning with
13862 first_unchanged_at_end_row have to be adjusted due to text
13863 changes. */
13864 first_unchanged_at_end_row
13865 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13866 IF_DEBUG (debug_delta = delta);
13867 IF_DEBUG (debug_delta_bytes = delta_bytes);
13869 /* Set stop_pos to the buffer position up to which we will have to
13870 display new lines. If first_unchanged_at_end_row != NULL, this
13871 is the buffer position of the start of the line displayed in that
13872 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13873 that we don't stop at a buffer position. */
13874 stop_pos = 0;
13875 if (first_unchanged_at_end_row)
13877 xassert (last_unchanged_at_beg_row == NULL
13878 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13880 /* If this is a continuation line, move forward to the next one
13881 that isn't. Changes in lines above affect this line.
13882 Caution: this may move first_unchanged_at_end_row to a row
13883 not displaying text. */
13884 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13885 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13886 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13887 < it.last_visible_y))
13888 ++first_unchanged_at_end_row;
13890 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13891 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13892 >= it.last_visible_y))
13893 first_unchanged_at_end_row = NULL;
13894 else
13896 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13897 + delta);
13898 first_unchanged_at_end_vpos
13899 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13900 xassert (stop_pos >= Z - END_UNCHANGED);
13903 else if (last_unchanged_at_beg_row == NULL)
13904 GIVE_UP (19);
13907 #if GLYPH_DEBUG
13909 /* Either there is no unchanged row at the end, or the one we have
13910 now displays text. This is a necessary condition for the window
13911 end pos calculation at the end of this function. */
13912 xassert (first_unchanged_at_end_row == NULL
13913 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13915 debug_last_unchanged_at_beg_vpos
13916 = (last_unchanged_at_beg_row
13917 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13918 : -1);
13919 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13921 #endif /* GLYPH_DEBUG != 0 */
13924 /* Display new lines. Set last_text_row to the last new line
13925 displayed which has text on it, i.e. might end up as being the
13926 line where the window_end_vpos is. */
13927 w->cursor.vpos = -1;
13928 last_text_row = NULL;
13929 overlay_arrow_seen = 0;
13930 while (it.current_y < it.last_visible_y
13931 && !fonts_changed_p
13932 && (first_unchanged_at_end_row == NULL
13933 || IT_CHARPOS (it) < stop_pos))
13935 if (display_line (&it))
13936 last_text_row = it.glyph_row - 1;
13939 if (fonts_changed_p)
13940 return -1;
13943 /* Compute differences in buffer positions, y-positions etc. for
13944 lines reused at the bottom of the window. Compute what we can
13945 scroll. */
13946 if (first_unchanged_at_end_row
13947 /* No lines reused because we displayed everything up to the
13948 bottom of the window. */
13949 && it.current_y < it.last_visible_y)
13951 dvpos = (it.vpos
13952 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13953 current_matrix));
13954 dy = it.current_y - first_unchanged_at_end_row->y;
13955 run.current_y = first_unchanged_at_end_row->y;
13956 run.desired_y = run.current_y + dy;
13957 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13959 else
13961 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13962 first_unchanged_at_end_row = NULL;
13964 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13967 /* Find the cursor if not already found. We have to decide whether
13968 PT will appear on this window (it sometimes doesn't, but this is
13969 not a very frequent case.) This decision has to be made before
13970 the current matrix is altered. A value of cursor.vpos < 0 means
13971 that PT is either in one of the lines beginning at
13972 first_unchanged_at_end_row or below the window. Don't care for
13973 lines that might be displayed later at the window end; as
13974 mentioned, this is not a frequent case. */
13975 if (w->cursor.vpos < 0)
13977 /* Cursor in unchanged rows at the top? */
13978 if (PT < CHARPOS (start_pos)
13979 && last_unchanged_at_beg_row)
13981 row = row_containing_pos (w, PT,
13982 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13983 last_unchanged_at_beg_row + 1, 0);
13984 if (row)
13985 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13988 /* Start from first_unchanged_at_end_row looking for PT. */
13989 else if (first_unchanged_at_end_row)
13991 row = row_containing_pos (w, PT - delta,
13992 first_unchanged_at_end_row, NULL, 0);
13993 if (row)
13994 set_cursor_from_row (w, row, w->current_matrix, delta,
13995 delta_bytes, dy, dvpos);
13998 /* Give up if cursor was not found. */
13999 if (w->cursor.vpos < 0)
14001 clear_glyph_matrix (w->desired_matrix);
14002 return -1;
14006 /* Don't let the cursor end in the scroll margins. */
14008 int this_scroll_margin, cursor_height;
14010 this_scroll_margin = max (0, scroll_margin);
14011 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14012 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14013 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14015 if ((w->cursor.y < this_scroll_margin
14016 && CHARPOS (start) > BEGV)
14017 /* Old redisplay didn't take scroll margin into account at the bottom,
14018 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14019 || (w->cursor.y + (make_cursor_line_fully_visible_p
14020 ? cursor_height + this_scroll_margin
14021 : 1)) > it.last_visible_y)
14023 w->cursor.vpos = -1;
14024 clear_glyph_matrix (w->desired_matrix);
14025 return -1;
14029 /* Scroll the display. Do it before changing the current matrix so
14030 that xterm.c doesn't get confused about where the cursor glyph is
14031 found. */
14032 if (dy && run.height)
14034 update_begin (f);
14036 if (FRAME_WINDOW_P (f))
14038 rif->update_window_begin_hook (w);
14039 rif->clear_window_mouse_face (w);
14040 rif->scroll_run_hook (w, &run);
14041 rif->update_window_end_hook (w, 0, 0);
14043 else
14045 /* Terminal frame. In this case, dvpos gives the number of
14046 lines to scroll by; dvpos < 0 means scroll up. */
14047 int first_unchanged_at_end_vpos
14048 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14049 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14050 int end = (WINDOW_TOP_EDGE_LINE (w)
14051 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14052 + window_internal_height (w));
14054 /* Perform the operation on the screen. */
14055 if (dvpos > 0)
14057 /* Scroll last_unchanged_at_beg_row to the end of the
14058 window down dvpos lines. */
14059 set_terminal_window (end);
14061 /* On dumb terminals delete dvpos lines at the end
14062 before inserting dvpos empty lines. */
14063 if (!scroll_region_ok)
14064 ins_del_lines (end - dvpos, -dvpos);
14066 /* Insert dvpos empty lines in front of
14067 last_unchanged_at_beg_row. */
14068 ins_del_lines (from, dvpos);
14070 else if (dvpos < 0)
14072 /* Scroll up last_unchanged_at_beg_vpos to the end of
14073 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14074 set_terminal_window (end);
14076 /* Delete dvpos lines in front of
14077 last_unchanged_at_beg_vpos. ins_del_lines will set
14078 the cursor to the given vpos and emit |dvpos| delete
14079 line sequences. */
14080 ins_del_lines (from + dvpos, dvpos);
14082 /* On a dumb terminal insert dvpos empty lines at the
14083 end. */
14084 if (!scroll_region_ok)
14085 ins_del_lines (end + dvpos, -dvpos);
14088 set_terminal_window (0);
14091 update_end (f);
14094 /* Shift reused rows of the current matrix to the right position.
14095 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14096 text. */
14097 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14098 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14099 if (dvpos < 0)
14101 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14102 bottom_vpos, dvpos);
14103 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14104 bottom_vpos, 0);
14106 else if (dvpos > 0)
14108 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14109 bottom_vpos, dvpos);
14110 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14111 first_unchanged_at_end_vpos + dvpos, 0);
14114 /* For frame-based redisplay, make sure that current frame and window
14115 matrix are in sync with respect to glyph memory. */
14116 if (!FRAME_WINDOW_P (f))
14117 sync_frame_with_window_matrix_rows (w);
14119 /* Adjust buffer positions in reused rows. */
14120 if (delta)
14121 increment_matrix_positions (current_matrix,
14122 first_unchanged_at_end_vpos + dvpos,
14123 bottom_vpos, delta, delta_bytes);
14125 /* Adjust Y positions. */
14126 if (dy)
14127 shift_glyph_matrix (w, current_matrix,
14128 first_unchanged_at_end_vpos + dvpos,
14129 bottom_vpos, dy);
14131 if (first_unchanged_at_end_row)
14133 first_unchanged_at_end_row += dvpos;
14134 if (first_unchanged_at_end_row->y >= it.last_visible_y
14135 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14136 first_unchanged_at_end_row = NULL;
14139 /* If scrolling up, there may be some lines to display at the end of
14140 the window. */
14141 last_text_row_at_end = NULL;
14142 if (dy < 0)
14144 /* Scrolling up can leave for example a partially visible line
14145 at the end of the window to be redisplayed. */
14146 /* Set last_row to the glyph row in the current matrix where the
14147 window end line is found. It has been moved up or down in
14148 the matrix by dvpos. */
14149 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14150 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14152 /* If last_row is the window end line, it should display text. */
14153 xassert (last_row->displays_text_p);
14155 /* If window end line was partially visible before, begin
14156 displaying at that line. Otherwise begin displaying with the
14157 line following it. */
14158 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14160 init_to_row_start (&it, w, last_row);
14161 it.vpos = last_vpos;
14162 it.current_y = last_row->y;
14164 else
14166 init_to_row_end (&it, w, last_row);
14167 it.vpos = 1 + last_vpos;
14168 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14169 ++last_row;
14172 /* We may start in a continuation line. If so, we have to
14173 get the right continuation_lines_width and current_x. */
14174 it.continuation_lines_width = last_row->continuation_lines_width;
14175 it.hpos = it.current_x = 0;
14177 /* Display the rest of the lines at the window end. */
14178 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14179 while (it.current_y < it.last_visible_y
14180 && !fonts_changed_p)
14182 /* Is it always sure that the display agrees with lines in
14183 the current matrix? I don't think so, so we mark rows
14184 displayed invalid in the current matrix by setting their
14185 enabled_p flag to zero. */
14186 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14187 if (display_line (&it))
14188 last_text_row_at_end = it.glyph_row - 1;
14192 /* Update window_end_pos and window_end_vpos. */
14193 if (first_unchanged_at_end_row
14194 && !last_text_row_at_end)
14196 /* Window end line if one of the preserved rows from the current
14197 matrix. Set row to the last row displaying text in current
14198 matrix starting at first_unchanged_at_end_row, after
14199 scrolling. */
14200 xassert (first_unchanged_at_end_row->displays_text_p);
14201 row = find_last_row_displaying_text (w->current_matrix, &it,
14202 first_unchanged_at_end_row);
14203 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14205 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14206 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14207 w->window_end_vpos
14208 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14209 xassert (w->window_end_bytepos >= 0);
14210 IF_DEBUG (debug_method_add (w, "A"));
14212 else if (last_text_row_at_end)
14214 w->window_end_pos
14215 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14216 w->window_end_bytepos
14217 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14218 w->window_end_vpos
14219 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14220 xassert (w->window_end_bytepos >= 0);
14221 IF_DEBUG (debug_method_add (w, "B"));
14223 else if (last_text_row)
14225 /* We have displayed either to the end of the window or at the
14226 end of the window, i.e. the last row with text is to be found
14227 in the desired matrix. */
14228 w->window_end_pos
14229 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14230 w->window_end_bytepos
14231 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14232 w->window_end_vpos
14233 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14234 xassert (w->window_end_bytepos >= 0);
14236 else if (first_unchanged_at_end_row == NULL
14237 && last_text_row == NULL
14238 && last_text_row_at_end == NULL)
14240 /* Displayed to end of window, but no line containing text was
14241 displayed. Lines were deleted at the end of the window. */
14242 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14243 int vpos = XFASTINT (w->window_end_vpos);
14244 struct glyph_row *current_row = current_matrix->rows + vpos;
14245 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14247 for (row = NULL;
14248 row == NULL && vpos >= first_vpos;
14249 --vpos, --current_row, --desired_row)
14251 if (desired_row->enabled_p)
14253 if (desired_row->displays_text_p)
14254 row = desired_row;
14256 else if (current_row->displays_text_p)
14257 row = current_row;
14260 xassert (row != NULL);
14261 w->window_end_vpos = make_number (vpos + 1);
14262 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14263 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14264 xassert (w->window_end_bytepos >= 0);
14265 IF_DEBUG (debug_method_add (w, "C"));
14267 else
14268 abort ();
14270 #if 0 /* This leads to problems, for instance when the cursor is
14271 at ZV, and the cursor line displays no text. */
14272 /* Disable rows below what's displayed in the window. This makes
14273 debugging easier. */
14274 enable_glyph_matrix_rows (current_matrix,
14275 XFASTINT (w->window_end_vpos) + 1,
14276 bottom_vpos, 0);
14277 #endif
14279 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14280 debug_end_vpos = XFASTINT (w->window_end_vpos));
14282 /* Record that display has not been completed. */
14283 w->window_end_valid = Qnil;
14284 w->desired_matrix->no_scrolling_p = 1;
14285 return 3;
14287 #undef GIVE_UP
14292 /***********************************************************************
14293 More debugging support
14294 ***********************************************************************/
14296 #if GLYPH_DEBUG
14298 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14299 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14300 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14303 /* Dump the contents of glyph matrix MATRIX on stderr.
14305 GLYPHS 0 means don't show glyph contents.
14306 GLYPHS 1 means show glyphs in short form
14307 GLYPHS > 1 means show glyphs in long form. */
14309 void
14310 dump_glyph_matrix (matrix, glyphs)
14311 struct glyph_matrix *matrix;
14312 int glyphs;
14314 int i;
14315 for (i = 0; i < matrix->nrows; ++i)
14316 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14320 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14321 the glyph row and area where the glyph comes from. */
14323 void
14324 dump_glyph (row, glyph, area)
14325 struct glyph_row *row;
14326 struct glyph *glyph;
14327 int area;
14329 if (glyph->type == CHAR_GLYPH)
14331 fprintf (stderr,
14332 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14333 glyph - row->glyphs[TEXT_AREA],
14334 'C',
14335 glyph->charpos,
14336 (BUFFERP (glyph->object)
14337 ? 'B'
14338 : (STRINGP (glyph->object)
14339 ? 'S'
14340 : '-')),
14341 glyph->pixel_width,
14342 glyph->u.ch,
14343 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14344 ? glyph->u.ch
14345 : '.'),
14346 glyph->face_id,
14347 glyph->left_box_line_p,
14348 glyph->right_box_line_p);
14350 else if (glyph->type == STRETCH_GLYPH)
14352 fprintf (stderr,
14353 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14354 glyph - row->glyphs[TEXT_AREA],
14355 'S',
14356 glyph->charpos,
14357 (BUFFERP (glyph->object)
14358 ? 'B'
14359 : (STRINGP (glyph->object)
14360 ? 'S'
14361 : '-')),
14362 glyph->pixel_width,
14364 '.',
14365 glyph->face_id,
14366 glyph->left_box_line_p,
14367 glyph->right_box_line_p);
14369 else if (glyph->type == IMAGE_GLYPH)
14371 fprintf (stderr,
14372 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14373 glyph - row->glyphs[TEXT_AREA],
14374 'I',
14375 glyph->charpos,
14376 (BUFFERP (glyph->object)
14377 ? 'B'
14378 : (STRINGP (glyph->object)
14379 ? 'S'
14380 : '-')),
14381 glyph->pixel_width,
14382 glyph->u.img_id,
14383 '.',
14384 glyph->face_id,
14385 glyph->left_box_line_p,
14386 glyph->right_box_line_p);
14391 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14392 GLYPHS 0 means don't show glyph contents.
14393 GLYPHS 1 means show glyphs in short form
14394 GLYPHS > 1 means show glyphs in long form. */
14396 void
14397 dump_glyph_row (row, vpos, glyphs)
14398 struct glyph_row *row;
14399 int vpos, glyphs;
14401 if (glyphs != 1)
14403 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14404 fprintf (stderr, "======================================================================\n");
14406 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14407 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14408 vpos,
14409 MATRIX_ROW_START_CHARPOS (row),
14410 MATRIX_ROW_END_CHARPOS (row),
14411 row->used[TEXT_AREA],
14412 row->contains_overlapping_glyphs_p,
14413 row->enabled_p,
14414 row->truncated_on_left_p,
14415 row->truncated_on_right_p,
14416 row->continued_p,
14417 MATRIX_ROW_CONTINUATION_LINE_P (row),
14418 row->displays_text_p,
14419 row->ends_at_zv_p,
14420 row->fill_line_p,
14421 row->ends_in_middle_of_char_p,
14422 row->starts_in_middle_of_char_p,
14423 row->mouse_face_p,
14424 row->x,
14425 row->y,
14426 row->pixel_width,
14427 row->height,
14428 row->visible_height,
14429 row->ascent,
14430 row->phys_ascent);
14431 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14432 row->end.overlay_string_index,
14433 row->continuation_lines_width);
14434 fprintf (stderr, "%9d %5d\n",
14435 CHARPOS (row->start.string_pos),
14436 CHARPOS (row->end.string_pos));
14437 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14438 row->end.dpvec_index);
14441 if (glyphs > 1)
14443 int area;
14445 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14447 struct glyph *glyph = row->glyphs[area];
14448 struct glyph *glyph_end = glyph + row->used[area];
14450 /* Glyph for a line end in text. */
14451 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14452 ++glyph_end;
14454 if (glyph < glyph_end)
14455 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14457 for (; glyph < glyph_end; ++glyph)
14458 dump_glyph (row, glyph, area);
14461 else if (glyphs == 1)
14463 int area;
14465 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14467 char *s = (char *) alloca (row->used[area] + 1);
14468 int i;
14470 for (i = 0; i < row->used[area]; ++i)
14472 struct glyph *glyph = row->glyphs[area] + i;
14473 if (glyph->type == CHAR_GLYPH
14474 && glyph->u.ch < 0x80
14475 && glyph->u.ch >= ' ')
14476 s[i] = glyph->u.ch;
14477 else
14478 s[i] = '.';
14481 s[i] = '\0';
14482 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14488 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14489 Sdump_glyph_matrix, 0, 1, "p",
14490 doc: /* Dump the current matrix of the selected window to stderr.
14491 Shows contents of glyph row structures. With non-nil
14492 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14493 glyphs in short form, otherwise show glyphs in long form. */)
14494 (glyphs)
14495 Lisp_Object glyphs;
14497 struct window *w = XWINDOW (selected_window);
14498 struct buffer *buffer = XBUFFER (w->buffer);
14500 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14501 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14502 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14503 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14504 fprintf (stderr, "=============================================\n");
14505 dump_glyph_matrix (w->current_matrix,
14506 NILP (glyphs) ? 0 : XINT (glyphs));
14507 return Qnil;
14511 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14512 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14515 struct frame *f = XFRAME (selected_frame);
14516 dump_glyph_matrix (f->current_matrix, 1);
14517 return Qnil;
14521 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14522 doc: /* Dump glyph row ROW to stderr.
14523 GLYPH 0 means don't dump glyphs.
14524 GLYPH 1 means dump glyphs in short form.
14525 GLYPH > 1 or omitted means dump glyphs in long form. */)
14526 (row, glyphs)
14527 Lisp_Object row, glyphs;
14529 struct glyph_matrix *matrix;
14530 int vpos;
14532 CHECK_NUMBER (row);
14533 matrix = XWINDOW (selected_window)->current_matrix;
14534 vpos = XINT (row);
14535 if (vpos >= 0 && vpos < matrix->nrows)
14536 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14537 vpos,
14538 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14539 return Qnil;
14543 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14544 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14545 GLYPH 0 means don't dump glyphs.
14546 GLYPH 1 means dump glyphs in short form.
14547 GLYPH > 1 or omitted means dump glyphs in long form. */)
14548 (row, glyphs)
14549 Lisp_Object row, glyphs;
14551 struct frame *sf = SELECTED_FRAME ();
14552 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14553 int vpos;
14555 CHECK_NUMBER (row);
14556 vpos = XINT (row);
14557 if (vpos >= 0 && vpos < m->nrows)
14558 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14559 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14560 return Qnil;
14564 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14565 doc: /* Toggle tracing of redisplay.
14566 With ARG, turn tracing on if and only if ARG is positive. */)
14567 (arg)
14568 Lisp_Object arg;
14570 if (NILP (arg))
14571 trace_redisplay_p = !trace_redisplay_p;
14572 else
14574 arg = Fprefix_numeric_value (arg);
14575 trace_redisplay_p = XINT (arg) > 0;
14578 return Qnil;
14582 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14583 doc: /* Like `format', but print result to stderr.
14584 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14585 (nargs, args)
14586 int nargs;
14587 Lisp_Object *args;
14589 Lisp_Object s = Fformat (nargs, args);
14590 fprintf (stderr, "%s", SDATA (s));
14591 return Qnil;
14594 #endif /* GLYPH_DEBUG */
14598 /***********************************************************************
14599 Building Desired Matrix Rows
14600 ***********************************************************************/
14602 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14603 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14605 static struct glyph_row *
14606 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14607 struct window *w;
14608 Lisp_Object overlay_arrow_string;
14610 struct frame *f = XFRAME (WINDOW_FRAME (w));
14611 struct buffer *buffer = XBUFFER (w->buffer);
14612 struct buffer *old = current_buffer;
14613 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14614 int arrow_len = SCHARS (overlay_arrow_string);
14615 const unsigned char *arrow_end = arrow_string + arrow_len;
14616 const unsigned char *p;
14617 struct it it;
14618 int multibyte_p;
14619 int n_glyphs_before;
14621 set_buffer_temp (buffer);
14622 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14623 it.glyph_row->used[TEXT_AREA] = 0;
14624 SET_TEXT_POS (it.position, 0, 0);
14626 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14627 p = arrow_string;
14628 while (p < arrow_end)
14630 Lisp_Object face, ilisp;
14632 /* Get the next character. */
14633 if (multibyte_p)
14634 it.c = string_char_and_length (p, arrow_len, &it.len);
14635 else
14636 it.c = *p, it.len = 1;
14637 p += it.len;
14639 /* Get its face. */
14640 ilisp = make_number (p - arrow_string);
14641 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14642 it.face_id = compute_char_face (f, it.c, face);
14644 /* Compute its width, get its glyphs. */
14645 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14646 SET_TEXT_POS (it.position, -1, -1);
14647 PRODUCE_GLYPHS (&it);
14649 /* If this character doesn't fit any more in the line, we have
14650 to remove some glyphs. */
14651 if (it.current_x > it.last_visible_x)
14653 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14654 break;
14658 set_buffer_temp (old);
14659 return it.glyph_row;
14663 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14664 glyphs are only inserted for terminal frames since we can't really
14665 win with truncation glyphs when partially visible glyphs are
14666 involved. Which glyphs to insert is determined by
14667 produce_special_glyphs. */
14669 static void
14670 insert_left_trunc_glyphs (it)
14671 struct it *it;
14673 struct it truncate_it;
14674 struct glyph *from, *end, *to, *toend;
14676 xassert (!FRAME_WINDOW_P (it->f));
14678 /* Get the truncation glyphs. */
14679 truncate_it = *it;
14680 truncate_it.current_x = 0;
14681 truncate_it.face_id = DEFAULT_FACE_ID;
14682 truncate_it.glyph_row = &scratch_glyph_row;
14683 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14684 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14685 truncate_it.object = make_number (0);
14686 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14688 /* Overwrite glyphs from IT with truncation glyphs. */
14689 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14690 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14691 to = it->glyph_row->glyphs[TEXT_AREA];
14692 toend = to + it->glyph_row->used[TEXT_AREA];
14694 while (from < end)
14695 *to++ = *from++;
14697 /* There may be padding glyphs left over. Overwrite them too. */
14698 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14700 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14701 while (from < end)
14702 *to++ = *from++;
14705 if (to > toend)
14706 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14710 /* Compute the pixel height and width of IT->glyph_row.
14712 Most of the time, ascent and height of a display line will be equal
14713 to the max_ascent and max_height values of the display iterator
14714 structure. This is not the case if
14716 1. We hit ZV without displaying anything. In this case, max_ascent
14717 and max_height will be zero.
14719 2. We have some glyphs that don't contribute to the line height.
14720 (The glyph row flag contributes_to_line_height_p is for future
14721 pixmap extensions).
14723 The first case is easily covered by using default values because in
14724 these cases, the line height does not really matter, except that it
14725 must not be zero. */
14727 static void
14728 compute_line_metrics (it)
14729 struct it *it;
14731 struct glyph_row *row = it->glyph_row;
14732 int area, i;
14734 if (FRAME_WINDOW_P (it->f))
14736 int i, min_y, max_y;
14738 /* The line may consist of one space only, that was added to
14739 place the cursor on it. If so, the row's height hasn't been
14740 computed yet. */
14741 if (row->height == 0)
14743 if (it->max_ascent + it->max_descent == 0)
14744 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14745 row->ascent = it->max_ascent;
14746 row->height = it->max_ascent + it->max_descent;
14747 row->phys_ascent = it->max_phys_ascent;
14748 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14749 row->extra_line_spacing = it->max_extra_line_spacing;
14752 /* Compute the width of this line. */
14753 row->pixel_width = row->x;
14754 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14755 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14757 xassert (row->pixel_width >= 0);
14758 xassert (row->ascent >= 0 && row->height > 0);
14760 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14761 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14763 /* If first line's physical ascent is larger than its logical
14764 ascent, use the physical ascent, and make the row taller.
14765 This makes accented characters fully visible. */
14766 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14767 && row->phys_ascent > row->ascent)
14769 row->height += row->phys_ascent - row->ascent;
14770 row->ascent = row->phys_ascent;
14773 /* Compute how much of the line is visible. */
14774 row->visible_height = row->height;
14776 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14777 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14779 if (row->y < min_y)
14780 row->visible_height -= min_y - row->y;
14781 if (row->y + row->height > max_y)
14782 row->visible_height -= row->y + row->height - max_y;
14784 else
14786 row->pixel_width = row->used[TEXT_AREA];
14787 if (row->continued_p)
14788 row->pixel_width -= it->continuation_pixel_width;
14789 else if (row->truncated_on_right_p)
14790 row->pixel_width -= it->truncation_pixel_width;
14791 row->ascent = row->phys_ascent = 0;
14792 row->height = row->phys_height = row->visible_height = 1;
14793 row->extra_line_spacing = 0;
14796 /* Compute a hash code for this row. */
14797 row->hash = 0;
14798 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14799 for (i = 0; i < row->used[area]; ++i)
14800 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14801 + row->glyphs[area][i].u.val
14802 + row->glyphs[area][i].face_id
14803 + row->glyphs[area][i].padding_p
14804 + (row->glyphs[area][i].type << 2));
14806 it->max_ascent = it->max_descent = 0;
14807 it->max_phys_ascent = it->max_phys_descent = 0;
14811 /* Append one space to the glyph row of iterator IT if doing a
14812 window-based redisplay. The space has the same face as
14813 IT->face_id. Value is non-zero if a space was added.
14815 This function is called to make sure that there is always one glyph
14816 at the end of a glyph row that the cursor can be set on under
14817 window-systems. (If there weren't such a glyph we would not know
14818 how wide and tall a box cursor should be displayed).
14820 At the same time this space let's a nicely handle clearing to the
14821 end of the line if the row ends in italic text. */
14823 static int
14824 append_space_for_newline (it, default_face_p)
14825 struct it *it;
14826 int default_face_p;
14828 if (FRAME_WINDOW_P (it->f))
14830 int n = it->glyph_row->used[TEXT_AREA];
14832 if (it->glyph_row->glyphs[TEXT_AREA] + n
14833 < it->glyph_row->glyphs[1 + TEXT_AREA])
14835 /* Save some values that must not be changed.
14836 Must save IT->c and IT->len because otherwise
14837 ITERATOR_AT_END_P wouldn't work anymore after
14838 append_space_for_newline has been called. */
14839 enum display_element_type saved_what = it->what;
14840 int saved_c = it->c, saved_len = it->len;
14841 int saved_x = it->current_x;
14842 int saved_face_id = it->face_id;
14843 struct text_pos saved_pos;
14844 Lisp_Object saved_object;
14845 struct face *face;
14847 saved_object = it->object;
14848 saved_pos = it->position;
14850 it->what = IT_CHARACTER;
14851 bzero (&it->position, sizeof it->position);
14852 it->object = make_number (0);
14853 it->c = ' ';
14854 it->len = 1;
14856 if (default_face_p)
14857 it->face_id = DEFAULT_FACE_ID;
14858 else if (it->face_before_selective_p)
14859 it->face_id = it->saved_face_id;
14860 face = FACE_FROM_ID (it->f, it->face_id);
14861 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14863 PRODUCE_GLYPHS (it);
14865 it->override_ascent = -1;
14866 it->constrain_row_ascent_descent_p = 0;
14867 it->current_x = saved_x;
14868 it->object = saved_object;
14869 it->position = saved_pos;
14870 it->what = saved_what;
14871 it->face_id = saved_face_id;
14872 it->len = saved_len;
14873 it->c = saved_c;
14874 return 1;
14878 return 0;
14882 /* Extend the face of the last glyph in the text area of IT->glyph_row
14883 to the end of the display line. Called from display_line.
14884 If the glyph row is empty, add a space glyph to it so that we
14885 know the face to draw. Set the glyph row flag fill_line_p. */
14887 static void
14888 extend_face_to_end_of_line (it)
14889 struct it *it;
14891 struct face *face;
14892 struct frame *f = it->f;
14894 /* If line is already filled, do nothing. */
14895 if (it->current_x >= it->last_visible_x)
14896 return;
14898 /* Face extension extends the background and box of IT->face_id
14899 to the end of the line. If the background equals the background
14900 of the frame, we don't have to do anything. */
14901 if (it->face_before_selective_p)
14902 face = FACE_FROM_ID (it->f, it->saved_face_id);
14903 else
14904 face = FACE_FROM_ID (f, it->face_id);
14906 if (FRAME_WINDOW_P (f)
14907 && face->box == FACE_NO_BOX
14908 && face->background == FRAME_BACKGROUND_PIXEL (f)
14909 && !face->stipple)
14910 return;
14912 /* Set the glyph row flag indicating that the face of the last glyph
14913 in the text area has to be drawn to the end of the text area. */
14914 it->glyph_row->fill_line_p = 1;
14916 /* If current character of IT is not ASCII, make sure we have the
14917 ASCII face. This will be automatically undone the next time
14918 get_next_display_element returns a multibyte character. Note
14919 that the character will always be single byte in unibyte text. */
14920 if (!SINGLE_BYTE_CHAR_P (it->c))
14922 it->face_id = FACE_FOR_CHAR (f, face, 0);
14925 if (FRAME_WINDOW_P (f))
14927 /* If the row is empty, add a space with the current face of IT,
14928 so that we know which face to draw. */
14929 if (it->glyph_row->used[TEXT_AREA] == 0)
14931 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14932 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14933 it->glyph_row->used[TEXT_AREA] = 1;
14936 else
14938 /* Save some values that must not be changed. */
14939 int saved_x = it->current_x;
14940 struct text_pos saved_pos;
14941 Lisp_Object saved_object;
14942 enum display_element_type saved_what = it->what;
14943 int saved_face_id = it->face_id;
14945 saved_object = it->object;
14946 saved_pos = it->position;
14948 it->what = IT_CHARACTER;
14949 bzero (&it->position, sizeof it->position);
14950 it->object = make_number (0);
14951 it->c = ' ';
14952 it->len = 1;
14953 it->face_id = face->id;
14955 PRODUCE_GLYPHS (it);
14957 while (it->current_x <= it->last_visible_x)
14958 PRODUCE_GLYPHS (it);
14960 /* Don't count these blanks really. It would let us insert a left
14961 truncation glyph below and make us set the cursor on them, maybe. */
14962 it->current_x = saved_x;
14963 it->object = saved_object;
14964 it->position = saved_pos;
14965 it->what = saved_what;
14966 it->face_id = saved_face_id;
14971 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14972 trailing whitespace. */
14974 static int
14975 trailing_whitespace_p (charpos)
14976 int charpos;
14978 int bytepos = CHAR_TO_BYTE (charpos);
14979 int c = 0;
14981 while (bytepos < ZV_BYTE
14982 && (c = FETCH_CHAR (bytepos),
14983 c == ' ' || c == '\t'))
14984 ++bytepos;
14986 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14988 if (bytepos != PT_BYTE)
14989 return 1;
14991 return 0;
14995 /* Highlight trailing whitespace, if any, in ROW. */
14997 void
14998 highlight_trailing_whitespace (f, row)
14999 struct frame *f;
15000 struct glyph_row *row;
15002 int used = row->used[TEXT_AREA];
15004 if (used)
15006 struct glyph *start = row->glyphs[TEXT_AREA];
15007 struct glyph *glyph = start + used - 1;
15009 /* Skip over glyphs inserted to display the cursor at the
15010 end of a line, for extending the face of the last glyph
15011 to the end of the line on terminals, and for truncation
15012 and continuation glyphs. */
15013 while (glyph >= start
15014 && glyph->type == CHAR_GLYPH
15015 && INTEGERP (glyph->object))
15016 --glyph;
15018 /* If last glyph is a space or stretch, and it's trailing
15019 whitespace, set the face of all trailing whitespace glyphs in
15020 IT->glyph_row to `trailing-whitespace'. */
15021 if (glyph >= start
15022 && BUFFERP (glyph->object)
15023 && (glyph->type == STRETCH_GLYPH
15024 || (glyph->type == CHAR_GLYPH
15025 && glyph->u.ch == ' '))
15026 && trailing_whitespace_p (glyph->charpos))
15028 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15029 if (face_id < 0)
15030 return;
15032 while (glyph >= start
15033 && BUFFERP (glyph->object)
15034 && (glyph->type == STRETCH_GLYPH
15035 || (glyph->type == CHAR_GLYPH
15036 && glyph->u.ch == ' ')))
15037 (glyph--)->face_id = face_id;
15043 /* Value is non-zero if glyph row ROW in window W should be
15044 used to hold the cursor. */
15046 static int
15047 cursor_row_p (w, row)
15048 struct window *w;
15049 struct glyph_row *row;
15051 int cursor_row_p = 1;
15053 if (PT == MATRIX_ROW_END_CHARPOS (row))
15055 /* If the row ends with a newline from a string, we don't want
15056 the cursor there, but we still want it at the start of the
15057 string if the string starts in this row.
15058 If the row is continued it doesn't end in a newline. */
15059 if (CHARPOS (row->end.string_pos) >= 0)
15060 cursor_row_p = (row->continued_p
15061 || PT >= MATRIX_ROW_START_CHARPOS (row));
15062 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15064 /* If the row ends in middle of a real character,
15065 and the line is continued, we want the cursor here.
15066 That's because MATRIX_ROW_END_CHARPOS would equal
15067 PT if PT is before the character. */
15068 if (!row->ends_in_ellipsis_p)
15069 cursor_row_p = row->continued_p;
15070 else
15071 /* If the row ends in an ellipsis, then
15072 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15073 We want that position to be displayed after the ellipsis. */
15074 cursor_row_p = 0;
15076 /* If the row ends at ZV, display the cursor at the end of that
15077 row instead of at the start of the row below. */
15078 else if (row->ends_at_zv_p)
15079 cursor_row_p = 1;
15080 else
15081 cursor_row_p = 0;
15084 return cursor_row_p;
15088 /* Construct the glyph row IT->glyph_row in the desired matrix of
15089 IT->w from text at the current position of IT. See dispextern.h
15090 for an overview of struct it. Value is non-zero if
15091 IT->glyph_row displays text, as opposed to a line displaying ZV
15092 only. */
15094 static int
15095 display_line (it)
15096 struct it *it;
15098 struct glyph_row *row = it->glyph_row;
15099 Lisp_Object overlay_arrow_string;
15101 /* We always start displaying at hpos zero even if hscrolled. */
15102 xassert (it->hpos == 0 && it->current_x == 0);
15104 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15105 >= it->w->desired_matrix->nrows)
15107 it->w->nrows_scale_factor++;
15108 fonts_changed_p = 1;
15109 return 0;
15112 /* Is IT->w showing the region? */
15113 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15115 /* Clear the result glyph row and enable it. */
15116 prepare_desired_row (row);
15118 row->y = it->current_y;
15119 row->start = it->start;
15120 row->continuation_lines_width = it->continuation_lines_width;
15121 row->displays_text_p = 1;
15122 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15123 it->starts_in_middle_of_char_p = 0;
15125 /* Arrange the overlays nicely for our purposes. Usually, we call
15126 display_line on only one line at a time, in which case this
15127 can't really hurt too much, or we call it on lines which appear
15128 one after another in the buffer, in which case all calls to
15129 recenter_overlay_lists but the first will be pretty cheap. */
15130 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15132 /* Move over display elements that are not visible because we are
15133 hscrolled. This may stop at an x-position < IT->first_visible_x
15134 if the first glyph is partially visible or if we hit a line end. */
15135 if (it->current_x < it->first_visible_x)
15137 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15138 MOVE_TO_POS | MOVE_TO_X);
15141 /* Get the initial row height. This is either the height of the
15142 text hscrolled, if there is any, or zero. */
15143 row->ascent = it->max_ascent;
15144 row->height = it->max_ascent + it->max_descent;
15145 row->phys_ascent = it->max_phys_ascent;
15146 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15147 row->extra_line_spacing = it->max_extra_line_spacing;
15149 /* Loop generating characters. The loop is left with IT on the next
15150 character to display. */
15151 while (1)
15153 int n_glyphs_before, hpos_before, x_before;
15154 int x, i, nglyphs;
15155 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15157 /* Retrieve the next thing to display. Value is zero if end of
15158 buffer reached. */
15159 if (!get_next_display_element (it))
15161 /* Maybe add a space at the end of this line that is used to
15162 display the cursor there under X. Set the charpos of the
15163 first glyph of blank lines not corresponding to any text
15164 to -1. */
15165 #ifdef HAVE_WINDOW_SYSTEM
15166 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15167 row->exact_window_width_line_p = 1;
15168 else
15169 #endif /* HAVE_WINDOW_SYSTEM */
15170 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15171 || row->used[TEXT_AREA] == 0)
15173 row->glyphs[TEXT_AREA]->charpos = -1;
15174 row->displays_text_p = 0;
15176 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15177 && (!MINI_WINDOW_P (it->w)
15178 || (minibuf_level && EQ (it->window, minibuf_window))))
15179 row->indicate_empty_line_p = 1;
15182 it->continuation_lines_width = 0;
15183 row->ends_at_zv_p = 1;
15184 break;
15187 /* Now, get the metrics of what we want to display. This also
15188 generates glyphs in `row' (which is IT->glyph_row). */
15189 n_glyphs_before = row->used[TEXT_AREA];
15190 x = it->current_x;
15192 /* Remember the line height so far in case the next element doesn't
15193 fit on the line. */
15194 if (!it->truncate_lines_p)
15196 ascent = it->max_ascent;
15197 descent = it->max_descent;
15198 phys_ascent = it->max_phys_ascent;
15199 phys_descent = it->max_phys_descent;
15202 PRODUCE_GLYPHS (it);
15204 /* If this display element was in marginal areas, continue with
15205 the next one. */
15206 if (it->area != TEXT_AREA)
15208 row->ascent = max (row->ascent, it->max_ascent);
15209 row->height = max (row->height, it->max_ascent + it->max_descent);
15210 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15211 row->phys_height = max (row->phys_height,
15212 it->max_phys_ascent + it->max_phys_descent);
15213 row->extra_line_spacing = max (row->extra_line_spacing,
15214 it->max_extra_line_spacing);
15215 set_iterator_to_next (it, 1);
15216 continue;
15219 /* Does the display element fit on the line? If we truncate
15220 lines, we should draw past the right edge of the window. If
15221 we don't truncate, we want to stop so that we can display the
15222 continuation glyph before the right margin. If lines are
15223 continued, there are two possible strategies for characters
15224 resulting in more than 1 glyph (e.g. tabs): Display as many
15225 glyphs as possible in this line and leave the rest for the
15226 continuation line, or display the whole element in the next
15227 line. Original redisplay did the former, so we do it also. */
15228 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15229 hpos_before = it->hpos;
15230 x_before = x;
15232 if (/* Not a newline. */
15233 nglyphs > 0
15234 /* Glyphs produced fit entirely in the line. */
15235 && it->current_x < it->last_visible_x)
15237 it->hpos += nglyphs;
15238 row->ascent = max (row->ascent, it->max_ascent);
15239 row->height = max (row->height, it->max_ascent + it->max_descent);
15240 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15241 row->phys_height = max (row->phys_height,
15242 it->max_phys_ascent + it->max_phys_descent);
15243 row->extra_line_spacing = max (row->extra_line_spacing,
15244 it->max_extra_line_spacing);
15245 if (it->current_x - it->pixel_width < it->first_visible_x)
15246 row->x = x - it->first_visible_x;
15248 else
15250 int new_x;
15251 struct glyph *glyph;
15253 for (i = 0; i < nglyphs; ++i, x = new_x)
15255 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15256 new_x = x + glyph->pixel_width;
15258 if (/* Lines are continued. */
15259 !it->truncate_lines_p
15260 && (/* Glyph doesn't fit on the line. */
15261 new_x > it->last_visible_x
15262 /* Or it fits exactly on a window system frame. */
15263 || (new_x == it->last_visible_x
15264 && FRAME_WINDOW_P (it->f))))
15266 /* End of a continued line. */
15268 if (it->hpos == 0
15269 || (new_x == it->last_visible_x
15270 && FRAME_WINDOW_P (it->f)))
15272 /* Current glyph is the only one on the line or
15273 fits exactly on the line. We must continue
15274 the line because we can't draw the cursor
15275 after the glyph. */
15276 row->continued_p = 1;
15277 it->current_x = new_x;
15278 it->continuation_lines_width += new_x;
15279 ++it->hpos;
15280 if (i == nglyphs - 1)
15282 set_iterator_to_next (it, 1);
15283 #ifdef HAVE_WINDOW_SYSTEM
15284 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15286 if (!get_next_display_element (it))
15288 row->exact_window_width_line_p = 1;
15289 it->continuation_lines_width = 0;
15290 row->continued_p = 0;
15291 row->ends_at_zv_p = 1;
15293 else if (ITERATOR_AT_END_OF_LINE_P (it))
15295 row->continued_p = 0;
15296 row->exact_window_width_line_p = 1;
15299 #endif /* HAVE_WINDOW_SYSTEM */
15302 else if (CHAR_GLYPH_PADDING_P (*glyph)
15303 && !FRAME_WINDOW_P (it->f))
15305 /* A padding glyph that doesn't fit on this line.
15306 This means the whole character doesn't fit
15307 on the line. */
15308 row->used[TEXT_AREA] = n_glyphs_before;
15310 /* Fill the rest of the row with continuation
15311 glyphs like in 20.x. */
15312 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15313 < row->glyphs[1 + TEXT_AREA])
15314 produce_special_glyphs (it, IT_CONTINUATION);
15316 row->continued_p = 1;
15317 it->current_x = x_before;
15318 it->continuation_lines_width += x_before;
15320 /* Restore the height to what it was before the
15321 element not fitting on the line. */
15322 it->max_ascent = ascent;
15323 it->max_descent = descent;
15324 it->max_phys_ascent = phys_ascent;
15325 it->max_phys_descent = phys_descent;
15327 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15329 /* A TAB that extends past the right edge of the
15330 window. This produces a single glyph on
15331 window system frames. We leave the glyph in
15332 this row and let it fill the row, but don't
15333 consume the TAB. */
15334 it->continuation_lines_width += it->last_visible_x;
15335 row->ends_in_middle_of_char_p = 1;
15336 row->continued_p = 1;
15337 glyph->pixel_width = it->last_visible_x - x;
15338 it->starts_in_middle_of_char_p = 1;
15340 else
15342 /* Something other than a TAB that draws past
15343 the right edge of the window. Restore
15344 positions to values before the element. */
15345 row->used[TEXT_AREA] = n_glyphs_before + i;
15347 /* Display continuation glyphs. */
15348 if (!FRAME_WINDOW_P (it->f))
15349 produce_special_glyphs (it, IT_CONTINUATION);
15350 row->continued_p = 1;
15352 it->continuation_lines_width += x;
15354 if (nglyphs > 1 && i > 0)
15356 row->ends_in_middle_of_char_p = 1;
15357 it->starts_in_middle_of_char_p = 1;
15360 /* Restore the height to what it was before the
15361 element not fitting on the line. */
15362 it->max_ascent = ascent;
15363 it->max_descent = descent;
15364 it->max_phys_ascent = phys_ascent;
15365 it->max_phys_descent = phys_descent;
15368 break;
15370 else if (new_x > it->first_visible_x)
15372 /* Increment number of glyphs actually displayed. */
15373 ++it->hpos;
15375 if (x < it->first_visible_x)
15376 /* Glyph is partially visible, i.e. row starts at
15377 negative X position. */
15378 row->x = x - it->first_visible_x;
15380 else
15382 /* Glyph is completely off the left margin of the
15383 window. This should not happen because of the
15384 move_it_in_display_line at the start of this
15385 function, unless the text display area of the
15386 window is empty. */
15387 xassert (it->first_visible_x <= it->last_visible_x);
15391 row->ascent = max (row->ascent, it->max_ascent);
15392 row->height = max (row->height, it->max_ascent + it->max_descent);
15393 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15394 row->phys_height = max (row->phys_height,
15395 it->max_phys_ascent + it->max_phys_descent);
15396 row->extra_line_spacing = max (row->extra_line_spacing,
15397 it->max_extra_line_spacing);
15399 /* End of this display line if row is continued. */
15400 if (row->continued_p || row->ends_at_zv_p)
15401 break;
15404 at_end_of_line:
15405 /* Is this a line end? If yes, we're also done, after making
15406 sure that a non-default face is extended up to the right
15407 margin of the window. */
15408 if (ITERATOR_AT_END_OF_LINE_P (it))
15410 int used_before = row->used[TEXT_AREA];
15412 row->ends_in_newline_from_string_p = STRINGP (it->object);
15414 #ifdef HAVE_WINDOW_SYSTEM
15415 /* Add a space at the end of the line that is used to
15416 display the cursor there. */
15417 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15418 append_space_for_newline (it, 0);
15419 #endif /* HAVE_WINDOW_SYSTEM */
15421 /* Extend the face to the end of the line. */
15422 extend_face_to_end_of_line (it);
15424 /* Make sure we have the position. */
15425 if (used_before == 0)
15426 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15428 /* Consume the line end. This skips over invisible lines. */
15429 set_iterator_to_next (it, 1);
15430 it->continuation_lines_width = 0;
15431 break;
15434 /* Proceed with next display element. Note that this skips
15435 over lines invisible because of selective display. */
15436 set_iterator_to_next (it, 1);
15438 /* If we truncate lines, we are done when the last displayed
15439 glyphs reach past the right margin of the window. */
15440 if (it->truncate_lines_p
15441 && (FRAME_WINDOW_P (it->f)
15442 ? (it->current_x >= it->last_visible_x)
15443 : (it->current_x > it->last_visible_x)))
15445 /* Maybe add truncation glyphs. */
15446 if (!FRAME_WINDOW_P (it->f))
15448 int i, n;
15450 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15451 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15452 break;
15454 for (n = row->used[TEXT_AREA]; i < n; ++i)
15456 row->used[TEXT_AREA] = i;
15457 produce_special_glyphs (it, IT_TRUNCATION);
15460 #ifdef HAVE_WINDOW_SYSTEM
15461 else
15463 /* Don't truncate if we can overflow newline into fringe. */
15464 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15466 if (!get_next_display_element (it))
15468 it->continuation_lines_width = 0;
15469 row->ends_at_zv_p = 1;
15470 row->exact_window_width_line_p = 1;
15471 break;
15473 if (ITERATOR_AT_END_OF_LINE_P (it))
15475 row->exact_window_width_line_p = 1;
15476 goto at_end_of_line;
15480 #endif /* HAVE_WINDOW_SYSTEM */
15482 row->truncated_on_right_p = 1;
15483 it->continuation_lines_width = 0;
15484 reseat_at_next_visible_line_start (it, 0);
15485 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15486 it->hpos = hpos_before;
15487 it->current_x = x_before;
15488 break;
15492 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15493 at the left window margin. */
15494 if (it->first_visible_x
15495 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15497 if (!FRAME_WINDOW_P (it->f))
15498 insert_left_trunc_glyphs (it);
15499 row->truncated_on_left_p = 1;
15502 /* If the start of this line is the overlay arrow-position, then
15503 mark this glyph row as the one containing the overlay arrow.
15504 This is clearly a mess with variable size fonts. It would be
15505 better to let it be displayed like cursors under X. */
15506 if ((row->displays_text_p || !overlay_arrow_seen)
15507 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15508 !NILP (overlay_arrow_string)))
15510 /* Overlay arrow in window redisplay is a fringe bitmap. */
15511 if (STRINGP (overlay_arrow_string))
15513 struct glyph_row *arrow_row
15514 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15515 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15516 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15517 struct glyph *p = row->glyphs[TEXT_AREA];
15518 struct glyph *p2, *end;
15520 /* Copy the arrow glyphs. */
15521 while (glyph < arrow_end)
15522 *p++ = *glyph++;
15524 /* Throw away padding glyphs. */
15525 p2 = p;
15526 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15527 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15528 ++p2;
15529 if (p2 > p)
15531 while (p2 < end)
15532 *p++ = *p2++;
15533 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15536 else
15538 xassert (INTEGERP (overlay_arrow_string));
15539 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15541 overlay_arrow_seen = 1;
15544 /* Compute pixel dimensions of this line. */
15545 compute_line_metrics (it);
15547 /* Remember the position at which this line ends. */
15548 row->end = it->current;
15550 /* Record whether this row ends inside an ellipsis. */
15551 row->ends_in_ellipsis_p
15552 = (it->method == GET_FROM_DISPLAY_VECTOR
15553 && it->ellipsis_p);
15555 /* Save fringe bitmaps in this row. */
15556 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15557 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15558 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15559 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15561 it->left_user_fringe_bitmap = 0;
15562 it->left_user_fringe_face_id = 0;
15563 it->right_user_fringe_bitmap = 0;
15564 it->right_user_fringe_face_id = 0;
15566 /* Maybe set the cursor. */
15567 if (it->w->cursor.vpos < 0
15568 && PT >= MATRIX_ROW_START_CHARPOS (row)
15569 && PT <= MATRIX_ROW_END_CHARPOS (row)
15570 && cursor_row_p (it->w, row))
15571 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15573 /* Highlight trailing whitespace. */
15574 if (!NILP (Vshow_trailing_whitespace))
15575 highlight_trailing_whitespace (it->f, it->glyph_row);
15577 /* Prepare for the next line. This line starts horizontally at (X
15578 HPOS) = (0 0). Vertical positions are incremented. As a
15579 convenience for the caller, IT->glyph_row is set to the next
15580 row to be used. */
15581 it->current_x = it->hpos = 0;
15582 it->current_y += row->height;
15583 ++it->vpos;
15584 ++it->glyph_row;
15585 it->start = it->current;
15586 return row->displays_text_p;
15591 /***********************************************************************
15592 Menu Bar
15593 ***********************************************************************/
15595 /* Redisplay the menu bar in the frame for window W.
15597 The menu bar of X frames that don't have X toolkit support is
15598 displayed in a special window W->frame->menu_bar_window.
15600 The menu bar of terminal frames is treated specially as far as
15601 glyph matrices are concerned. Menu bar lines are not part of
15602 windows, so the update is done directly on the frame matrix rows
15603 for the menu bar. */
15605 static void
15606 display_menu_bar (w)
15607 struct window *w;
15609 struct frame *f = XFRAME (WINDOW_FRAME (w));
15610 struct it it;
15611 Lisp_Object items;
15612 int i;
15614 /* Don't do all this for graphical frames. */
15615 #ifdef HAVE_NTGUI
15616 if (!NILP (Vwindow_system))
15617 return;
15618 #endif
15619 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15620 if (FRAME_X_P (f))
15621 return;
15622 #endif
15623 #ifdef MAC_OS
15624 if (FRAME_MAC_P (f))
15625 return;
15626 #endif
15628 #ifdef USE_X_TOOLKIT
15629 xassert (!FRAME_WINDOW_P (f));
15630 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15631 it.first_visible_x = 0;
15632 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15633 #else /* not USE_X_TOOLKIT */
15634 if (FRAME_WINDOW_P (f))
15636 /* Menu bar lines are displayed in the desired matrix of the
15637 dummy window menu_bar_window. */
15638 struct window *menu_w;
15639 xassert (WINDOWP (f->menu_bar_window));
15640 menu_w = XWINDOW (f->menu_bar_window);
15641 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15642 MENU_FACE_ID);
15643 it.first_visible_x = 0;
15644 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15646 else
15648 /* This is a TTY frame, i.e. character hpos/vpos are used as
15649 pixel x/y. */
15650 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15651 MENU_FACE_ID);
15652 it.first_visible_x = 0;
15653 it.last_visible_x = FRAME_COLS (f);
15655 #endif /* not USE_X_TOOLKIT */
15657 if (! mode_line_inverse_video)
15658 /* Force the menu-bar to be displayed in the default face. */
15659 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15661 /* Clear all rows of the menu bar. */
15662 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15664 struct glyph_row *row = it.glyph_row + i;
15665 clear_glyph_row (row);
15666 row->enabled_p = 1;
15667 row->full_width_p = 1;
15670 /* Display all items of the menu bar. */
15671 items = FRAME_MENU_BAR_ITEMS (it.f);
15672 for (i = 0; i < XVECTOR (items)->size; i += 4)
15674 Lisp_Object string;
15676 /* Stop at nil string. */
15677 string = AREF (items, i + 1);
15678 if (NILP (string))
15679 break;
15681 /* Remember where item was displayed. */
15682 AREF (items, i + 3) = make_number (it.hpos);
15684 /* Display the item, pad with one space. */
15685 if (it.current_x < it.last_visible_x)
15686 display_string (NULL, string, Qnil, 0, 0, &it,
15687 SCHARS (string) + 1, 0, 0, -1);
15690 /* Fill out the line with spaces. */
15691 if (it.current_x < it.last_visible_x)
15692 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15694 /* Compute the total height of the lines. */
15695 compute_line_metrics (&it);
15700 /***********************************************************************
15701 Mode Line
15702 ***********************************************************************/
15704 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15705 FORCE is non-zero, redisplay mode lines unconditionally.
15706 Otherwise, redisplay only mode lines that are garbaged. Value is
15707 the number of windows whose mode lines were redisplayed. */
15709 static int
15710 redisplay_mode_lines (window, force)
15711 Lisp_Object window;
15712 int force;
15714 int nwindows = 0;
15716 while (!NILP (window))
15718 struct window *w = XWINDOW (window);
15720 if (WINDOWP (w->hchild))
15721 nwindows += redisplay_mode_lines (w->hchild, force);
15722 else if (WINDOWP (w->vchild))
15723 nwindows += redisplay_mode_lines (w->vchild, force);
15724 else if (force
15725 || FRAME_GARBAGED_P (XFRAME (w->frame))
15726 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15728 struct text_pos lpoint;
15729 struct buffer *old = current_buffer;
15731 /* Set the window's buffer for the mode line display. */
15732 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15733 set_buffer_internal_1 (XBUFFER (w->buffer));
15735 /* Point refers normally to the selected window. For any
15736 other window, set up appropriate value. */
15737 if (!EQ (window, selected_window))
15739 struct text_pos pt;
15741 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15742 if (CHARPOS (pt) < BEGV)
15743 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15744 else if (CHARPOS (pt) > (ZV - 1))
15745 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15746 else
15747 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15750 /* Display mode lines. */
15751 clear_glyph_matrix (w->desired_matrix);
15752 if (display_mode_lines (w))
15754 ++nwindows;
15755 w->must_be_updated_p = 1;
15758 /* Restore old settings. */
15759 set_buffer_internal_1 (old);
15760 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15763 window = w->next;
15766 return nwindows;
15770 /* Display the mode and/or top line of window W. Value is the number
15771 of mode lines displayed. */
15773 static int
15774 display_mode_lines (w)
15775 struct window *w;
15777 Lisp_Object old_selected_window, old_selected_frame;
15778 int n = 0;
15780 old_selected_frame = selected_frame;
15781 selected_frame = w->frame;
15782 old_selected_window = selected_window;
15783 XSETWINDOW (selected_window, w);
15785 /* These will be set while the mode line specs are processed. */
15786 line_number_displayed = 0;
15787 w->column_number_displayed = Qnil;
15789 if (WINDOW_WANTS_MODELINE_P (w))
15791 struct window *sel_w = XWINDOW (old_selected_window);
15793 /* Select mode line face based on the real selected window. */
15794 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15795 current_buffer->mode_line_format);
15796 ++n;
15799 if (WINDOW_WANTS_HEADER_LINE_P (w))
15801 display_mode_line (w, HEADER_LINE_FACE_ID,
15802 current_buffer->header_line_format);
15803 ++n;
15806 selected_frame = old_selected_frame;
15807 selected_window = old_selected_window;
15808 return n;
15812 /* Display mode or top line of window W. FACE_ID specifies which line
15813 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15814 FORMAT is the mode line format to display. Value is the pixel
15815 height of the mode line displayed. */
15817 static int
15818 display_mode_line (w, face_id, format)
15819 struct window *w;
15820 enum face_id face_id;
15821 Lisp_Object format;
15823 struct it it;
15824 struct face *face;
15825 int count = SPECPDL_INDEX ();
15827 init_iterator (&it, w, -1, -1, NULL, face_id);
15828 prepare_desired_row (it.glyph_row);
15830 it.glyph_row->mode_line_p = 1;
15832 if (! mode_line_inverse_video)
15833 /* Force the mode-line to be displayed in the default face. */
15834 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15836 record_unwind_protect (unwind_format_mode_line,
15837 format_mode_line_unwind_data (NULL));
15839 mode_line_target = MODE_LINE_DISPLAY;
15841 /* Temporarily make frame's keyboard the current kboard so that
15842 kboard-local variables in the mode_line_format will get the right
15843 values. */
15844 push_frame_kboard (it.f);
15845 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15846 pop_frame_kboard ();
15848 unbind_to (count, Qnil);
15850 /* Fill up with spaces. */
15851 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15853 compute_line_metrics (&it);
15854 it.glyph_row->full_width_p = 1;
15855 it.glyph_row->continued_p = 0;
15856 it.glyph_row->truncated_on_left_p = 0;
15857 it.glyph_row->truncated_on_right_p = 0;
15859 /* Make a 3D mode-line have a shadow at its right end. */
15860 face = FACE_FROM_ID (it.f, face_id);
15861 extend_face_to_end_of_line (&it);
15862 if (face->box != FACE_NO_BOX)
15864 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15865 + it.glyph_row->used[TEXT_AREA] - 1);
15866 last->right_box_line_p = 1;
15869 return it.glyph_row->height;
15872 /* Contribute ELT to the mode line for window IT->w. How it
15873 translates into text depends on its data type.
15875 IT describes the display environment in which we display, as usual.
15877 DEPTH is the depth in recursion. It is used to prevent
15878 infinite recursion here.
15880 FIELD_WIDTH is the number of characters the display of ELT should
15881 occupy in the mode line, and PRECISION is the maximum number of
15882 characters to display from ELT's representation. See
15883 display_string for details.
15885 Returns the hpos of the end of the text generated by ELT.
15887 PROPS is a property list to add to any string we encounter.
15889 If RISKY is nonzero, remove (disregard) any properties in any string
15890 we encounter, and ignore :eval and :propertize.
15892 The global variable `mode_line_target' determines whether the
15893 output is passed to `store_mode_line_noprop',
15894 `store_mode_line_string', or `display_string'. */
15896 static int
15897 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15898 struct it *it;
15899 int depth;
15900 int field_width, precision;
15901 Lisp_Object elt, props;
15902 int risky;
15904 int n = 0, field, prec;
15905 int literal = 0;
15907 tail_recurse:
15908 if (depth > 100)
15909 elt = build_string ("*too-deep*");
15911 depth++;
15913 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15915 case Lisp_String:
15917 /* A string: output it and check for %-constructs within it. */
15918 unsigned char c;
15919 const unsigned char *this, *lisp_string;
15921 if (!NILP (props) || risky)
15923 Lisp_Object oprops, aelt;
15924 oprops = Ftext_properties_at (make_number (0), elt);
15926 /* If the starting string's properties are not what
15927 we want, translate the string. Also, if the string
15928 is risky, do that anyway. */
15930 if (NILP (Fequal (props, oprops)) || risky)
15932 /* If the starting string has properties,
15933 merge the specified ones onto the existing ones. */
15934 if (! NILP (oprops) && !risky)
15936 Lisp_Object tem;
15938 oprops = Fcopy_sequence (oprops);
15939 tem = props;
15940 while (CONSP (tem))
15942 oprops = Fplist_put (oprops, XCAR (tem),
15943 XCAR (XCDR (tem)));
15944 tem = XCDR (XCDR (tem));
15946 props = oprops;
15949 aelt = Fassoc (elt, mode_line_proptrans_alist);
15950 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15952 mode_line_proptrans_alist
15953 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15954 elt = XCAR (aelt);
15956 else
15958 Lisp_Object tem;
15960 elt = Fcopy_sequence (elt);
15961 Fset_text_properties (make_number (0), Flength (elt),
15962 props, elt);
15963 /* Add this item to mode_line_proptrans_alist. */
15964 mode_line_proptrans_alist
15965 = Fcons (Fcons (elt, props),
15966 mode_line_proptrans_alist);
15967 /* Truncate mode_line_proptrans_alist
15968 to at most 50 elements. */
15969 tem = Fnthcdr (make_number (50),
15970 mode_line_proptrans_alist);
15971 if (! NILP (tem))
15972 XSETCDR (tem, Qnil);
15977 this = SDATA (elt);
15978 lisp_string = this;
15980 if (literal)
15982 prec = precision - n;
15983 switch (mode_line_target)
15985 case MODE_LINE_NOPROP:
15986 case MODE_LINE_TITLE:
15987 n += store_mode_line_noprop (SDATA (elt), -1, prec);
15988 break;
15989 case MODE_LINE_STRING:
15990 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15991 break;
15992 case MODE_LINE_DISPLAY:
15993 n += display_string (NULL, elt, Qnil, 0, 0, it,
15994 0, prec, 0, STRING_MULTIBYTE (elt));
15995 break;
15998 break;
16001 while ((precision <= 0 || n < precision)
16002 && *this
16003 && (mode_line_target != MODE_LINE_DISPLAY
16004 || it->current_x < it->last_visible_x))
16006 const unsigned char *last = this;
16008 /* Advance to end of string or next format specifier. */
16009 while ((c = *this++) != '\0' && c != '%')
16012 if (this - 1 != last)
16014 int nchars, nbytes;
16016 /* Output to end of string or up to '%'. Field width
16017 is length of string. Don't output more than
16018 PRECISION allows us. */
16019 --this;
16021 prec = c_string_width (last, this - last, precision - n,
16022 &nchars, &nbytes);
16024 switch (mode_line_target)
16026 case MODE_LINE_NOPROP:
16027 case MODE_LINE_TITLE:
16028 n += store_mode_line_noprop (last, 0, prec);
16029 break;
16030 case MODE_LINE_STRING:
16032 int bytepos = last - lisp_string;
16033 int charpos = string_byte_to_char (elt, bytepos);
16034 int endpos = (precision <= 0
16035 ? string_byte_to_char (elt,
16036 this - lisp_string)
16037 : charpos + nchars);
16039 n += store_mode_line_string (NULL,
16040 Fsubstring (elt, make_number (charpos),
16041 make_number (endpos)),
16042 0, 0, 0, Qnil);
16044 break;
16045 case MODE_LINE_DISPLAY:
16047 int bytepos = last - lisp_string;
16048 int charpos = string_byte_to_char (elt, bytepos);
16049 n += display_string (NULL, elt, Qnil, 0, charpos,
16050 it, 0, prec, 0,
16051 STRING_MULTIBYTE (elt));
16053 break;
16056 else /* c == '%' */
16058 const unsigned char *percent_position = this;
16060 /* Get the specified minimum width. Zero means
16061 don't pad. */
16062 field = 0;
16063 while ((c = *this++) >= '0' && c <= '9')
16064 field = field * 10 + c - '0';
16066 /* Don't pad beyond the total padding allowed. */
16067 if (field_width - n > 0 && field > field_width - n)
16068 field = field_width - n;
16070 /* Note that either PRECISION <= 0 or N < PRECISION. */
16071 prec = precision - n;
16073 if (c == 'M')
16074 n += display_mode_element (it, depth, field, prec,
16075 Vglobal_mode_string, props,
16076 risky);
16077 else if (c != 0)
16079 int multibyte;
16080 int bytepos, charpos;
16081 unsigned char *spec;
16083 bytepos = percent_position - lisp_string;
16084 charpos = (STRING_MULTIBYTE (elt)
16085 ? string_byte_to_char (elt, bytepos)
16086 : bytepos);
16088 spec
16089 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16091 switch (mode_line_target)
16093 case MODE_LINE_NOPROP:
16094 case MODE_LINE_TITLE:
16095 n += store_mode_line_noprop (spec, field, prec);
16096 break;
16097 case MODE_LINE_STRING:
16099 int len = strlen (spec);
16100 Lisp_Object tem = make_string (spec, len);
16101 props = Ftext_properties_at (make_number (charpos), elt);
16102 /* Should only keep face property in props */
16103 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16105 break;
16106 case MODE_LINE_DISPLAY:
16108 int nglyphs_before, nwritten;
16110 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16111 nwritten = display_string (spec, Qnil, elt,
16112 charpos, 0, it,
16113 field, prec, 0,
16114 multibyte);
16116 /* Assign to the glyphs written above the
16117 string where the `%x' came from, position
16118 of the `%'. */
16119 if (nwritten > 0)
16121 struct glyph *glyph
16122 = (it->glyph_row->glyphs[TEXT_AREA]
16123 + nglyphs_before);
16124 int i;
16126 for (i = 0; i < nwritten; ++i)
16128 glyph[i].object = elt;
16129 glyph[i].charpos = charpos;
16132 n += nwritten;
16135 break;
16138 else /* c == 0 */
16139 break;
16143 break;
16145 case Lisp_Symbol:
16146 /* A symbol: process the value of the symbol recursively
16147 as if it appeared here directly. Avoid error if symbol void.
16148 Special case: if value of symbol is a string, output the string
16149 literally. */
16151 register Lisp_Object tem;
16153 /* If the variable is not marked as risky to set
16154 then its contents are risky to use. */
16155 if (NILP (Fget (elt, Qrisky_local_variable)))
16156 risky = 1;
16158 tem = Fboundp (elt);
16159 if (!NILP (tem))
16161 tem = Fsymbol_value (elt);
16162 /* If value is a string, output that string literally:
16163 don't check for % within it. */
16164 if (STRINGP (tem))
16165 literal = 1;
16167 if (!EQ (tem, elt))
16169 /* Give up right away for nil or t. */
16170 elt = tem;
16171 goto tail_recurse;
16175 break;
16177 case Lisp_Cons:
16179 register Lisp_Object car, tem;
16181 /* A cons cell: five distinct cases.
16182 If first element is :eval or :propertize, do something special.
16183 If first element is a string or a cons, process all the elements
16184 and effectively concatenate them.
16185 If first element is a negative number, truncate displaying cdr to
16186 at most that many characters. If positive, pad (with spaces)
16187 to at least that many characters.
16188 If first element is a symbol, process the cadr or caddr recursively
16189 according to whether the symbol's value is non-nil or nil. */
16190 car = XCAR (elt);
16191 if (EQ (car, QCeval))
16193 /* An element of the form (:eval FORM) means evaluate FORM
16194 and use the result as mode line elements. */
16196 if (risky)
16197 break;
16199 if (CONSP (XCDR (elt)))
16201 Lisp_Object spec;
16202 spec = safe_eval (XCAR (XCDR (elt)));
16203 n += display_mode_element (it, depth, field_width - n,
16204 precision - n, spec, props,
16205 risky);
16208 else if (EQ (car, QCpropertize))
16210 /* An element of the form (:propertize ELT PROPS...)
16211 means display ELT but applying properties PROPS. */
16213 if (risky)
16214 break;
16216 if (CONSP (XCDR (elt)))
16217 n += display_mode_element (it, depth, field_width - n,
16218 precision - n, XCAR (XCDR (elt)),
16219 XCDR (XCDR (elt)), risky);
16221 else if (SYMBOLP (car))
16223 tem = Fboundp (car);
16224 elt = XCDR (elt);
16225 if (!CONSP (elt))
16226 goto invalid;
16227 /* elt is now the cdr, and we know it is a cons cell.
16228 Use its car if CAR has a non-nil value. */
16229 if (!NILP (tem))
16231 tem = Fsymbol_value (car);
16232 if (!NILP (tem))
16234 elt = XCAR (elt);
16235 goto tail_recurse;
16238 /* Symbol's value is nil (or symbol is unbound)
16239 Get the cddr of the original list
16240 and if possible find the caddr and use that. */
16241 elt = XCDR (elt);
16242 if (NILP (elt))
16243 break;
16244 else if (!CONSP (elt))
16245 goto invalid;
16246 elt = XCAR (elt);
16247 goto tail_recurse;
16249 else if (INTEGERP (car))
16251 register int lim = XINT (car);
16252 elt = XCDR (elt);
16253 if (lim < 0)
16255 /* Negative int means reduce maximum width. */
16256 if (precision <= 0)
16257 precision = -lim;
16258 else
16259 precision = min (precision, -lim);
16261 else if (lim > 0)
16263 /* Padding specified. Don't let it be more than
16264 current maximum. */
16265 if (precision > 0)
16266 lim = min (precision, lim);
16268 /* If that's more padding than already wanted, queue it.
16269 But don't reduce padding already specified even if
16270 that is beyond the current truncation point. */
16271 field_width = max (lim, field_width);
16273 goto tail_recurse;
16275 else if (STRINGP (car) || CONSP (car))
16277 register int limit = 50;
16278 /* Limit is to protect against circular lists. */
16279 while (CONSP (elt)
16280 && --limit > 0
16281 && (precision <= 0 || n < precision))
16283 n += display_mode_element (it, depth,
16284 /* Do padding only after the last
16285 element in the list. */
16286 (! CONSP (XCDR (elt))
16287 ? field_width - n
16288 : 0),
16289 precision - n, XCAR (elt),
16290 props, risky);
16291 elt = XCDR (elt);
16295 break;
16297 default:
16298 invalid:
16299 elt = build_string ("*invalid*");
16300 goto tail_recurse;
16303 /* Pad to FIELD_WIDTH. */
16304 if (field_width > 0 && n < field_width)
16306 switch (mode_line_target)
16308 case MODE_LINE_NOPROP:
16309 case MODE_LINE_TITLE:
16310 n += store_mode_line_noprop ("", field_width - n, 0);
16311 break;
16312 case MODE_LINE_STRING:
16313 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16314 break;
16315 case MODE_LINE_DISPLAY:
16316 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16317 0, 0, 0);
16318 break;
16322 return n;
16325 /* Store a mode-line string element in mode_line_string_list.
16327 If STRING is non-null, display that C string. Otherwise, the Lisp
16328 string LISP_STRING is displayed.
16330 FIELD_WIDTH is the minimum number of output glyphs to produce.
16331 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16332 with spaces. FIELD_WIDTH <= 0 means don't pad.
16334 PRECISION is the maximum number of characters to output from
16335 STRING. PRECISION <= 0 means don't truncate the string.
16337 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16338 properties to the string.
16340 PROPS are the properties to add to the string.
16341 The mode_line_string_face face property is always added to the string.
16344 static int
16345 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16346 char *string;
16347 Lisp_Object lisp_string;
16348 int copy_string;
16349 int field_width;
16350 int precision;
16351 Lisp_Object props;
16353 int len;
16354 int n = 0;
16356 if (string != NULL)
16358 len = strlen (string);
16359 if (precision > 0 && len > precision)
16360 len = precision;
16361 lisp_string = make_string (string, len);
16362 if (NILP (props))
16363 props = mode_line_string_face_prop;
16364 else if (!NILP (mode_line_string_face))
16366 Lisp_Object face = Fplist_get (props, Qface);
16367 props = Fcopy_sequence (props);
16368 if (NILP (face))
16369 face = mode_line_string_face;
16370 else
16371 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16372 props = Fplist_put (props, Qface, face);
16374 Fadd_text_properties (make_number (0), make_number (len),
16375 props, lisp_string);
16377 else
16379 len = XFASTINT (Flength (lisp_string));
16380 if (precision > 0 && len > precision)
16382 len = precision;
16383 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16384 precision = -1;
16386 if (!NILP (mode_line_string_face))
16388 Lisp_Object face;
16389 if (NILP (props))
16390 props = Ftext_properties_at (make_number (0), lisp_string);
16391 face = Fplist_get (props, Qface);
16392 if (NILP (face))
16393 face = mode_line_string_face;
16394 else
16395 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16396 props = Fcons (Qface, Fcons (face, Qnil));
16397 if (copy_string)
16398 lisp_string = Fcopy_sequence (lisp_string);
16400 if (!NILP (props))
16401 Fadd_text_properties (make_number (0), make_number (len),
16402 props, lisp_string);
16405 if (len > 0)
16407 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16408 n += len;
16411 if (field_width > len)
16413 field_width -= len;
16414 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16415 if (!NILP (props))
16416 Fadd_text_properties (make_number (0), make_number (field_width),
16417 props, lisp_string);
16418 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16419 n += field_width;
16422 return n;
16426 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16427 1, 4, 0,
16428 doc: /* Format a string out of a mode line format specification.
16429 First arg FORMAT specifies the mode line format (see `mode-line-format'
16430 for details) to use.
16432 Optional second arg FACE specifies the face property to put
16433 on all characters for which no face is specified.
16434 t means whatever face the window's mode line currently uses
16435 \(either `mode-line' or `mode-line-inactive', depending).
16436 nil means the default is no face property.
16437 If FACE is an integer, the value string has no text properties.
16439 Optional third and fourth args WINDOW and BUFFER specify the window
16440 and buffer to use as the context for the formatting (defaults
16441 are the selected window and the window's buffer). */)
16442 (format, face, window, buffer)
16443 Lisp_Object format, face, window, buffer;
16445 struct it it;
16446 int len;
16447 struct window *w;
16448 struct buffer *old_buffer = NULL;
16449 int face_id = -1;
16450 int no_props = INTEGERP (face);
16451 int count = SPECPDL_INDEX ();
16452 Lisp_Object str;
16453 int string_start = 0;
16455 if (NILP (window))
16456 window = selected_window;
16457 CHECK_WINDOW (window);
16458 w = XWINDOW (window);
16460 if (NILP (buffer))
16461 buffer = w->buffer;
16462 CHECK_BUFFER (buffer);
16464 if (NILP (format))
16465 return build_string ("");
16467 if (no_props)
16468 face = Qnil;
16470 if (!NILP (face))
16472 if (EQ (face, Qt))
16473 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16474 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16477 if (face_id < 0)
16478 face_id = DEFAULT_FACE_ID;
16480 if (XBUFFER (buffer) != current_buffer)
16481 old_buffer = current_buffer;
16483 record_unwind_protect (unwind_format_mode_line,
16484 format_mode_line_unwind_data (old_buffer));
16486 if (old_buffer)
16487 set_buffer_internal_1 (XBUFFER (buffer));
16489 init_iterator (&it, w, -1, -1, NULL, face_id);
16491 if (no_props)
16493 mode_line_target = MODE_LINE_NOPROP;
16494 mode_line_string_face_prop = Qnil;
16495 mode_line_string_list = Qnil;
16496 string_start = MODE_LINE_NOPROP_LEN (0);
16498 else
16500 mode_line_target = MODE_LINE_STRING;
16501 mode_line_string_list = Qnil;
16502 mode_line_string_face = face;
16503 mode_line_string_face_prop
16504 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16507 push_frame_kboard (it.f);
16508 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16509 pop_frame_kboard ();
16511 if (no_props)
16513 len = MODE_LINE_NOPROP_LEN (string_start);
16514 str = make_string (mode_line_noprop_buf + string_start, len);
16516 else
16518 mode_line_string_list = Fnreverse (mode_line_string_list);
16519 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16520 make_string ("", 0));
16523 unbind_to (count, Qnil);
16524 return str;
16527 /* Write a null-terminated, right justified decimal representation of
16528 the positive integer D to BUF using a minimal field width WIDTH. */
16530 static void
16531 pint2str (buf, width, d)
16532 register char *buf;
16533 register int width;
16534 register int d;
16536 register char *p = buf;
16538 if (d <= 0)
16539 *p++ = '0';
16540 else
16542 while (d > 0)
16544 *p++ = d % 10 + '0';
16545 d /= 10;
16549 for (width -= (int) (p - buf); width > 0; --width)
16550 *p++ = ' ';
16551 *p-- = '\0';
16552 while (p > buf)
16554 d = *buf;
16555 *buf++ = *p;
16556 *p-- = d;
16560 /* Write a null-terminated, right justified decimal and "human
16561 readable" representation of the nonnegative integer D to BUF using
16562 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16564 static const char power_letter[] =
16566 0, /* not used */
16567 'k', /* kilo */
16568 'M', /* mega */
16569 'G', /* giga */
16570 'T', /* tera */
16571 'P', /* peta */
16572 'E', /* exa */
16573 'Z', /* zetta */
16574 'Y' /* yotta */
16577 static void
16578 pint2hrstr (buf, width, d)
16579 char *buf;
16580 int width;
16581 int d;
16583 /* We aim to represent the nonnegative integer D as
16584 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16585 int quotient = d;
16586 int remainder = 0;
16587 /* -1 means: do not use TENTHS. */
16588 int tenths = -1;
16589 int exponent = 0;
16591 /* Length of QUOTIENT.TENTHS as a string. */
16592 int length;
16594 char * psuffix;
16595 char * p;
16597 if (1000 <= quotient)
16599 /* Scale to the appropriate EXPONENT. */
16602 remainder = quotient % 1000;
16603 quotient /= 1000;
16604 exponent++;
16606 while (1000 <= quotient);
16608 /* Round to nearest and decide whether to use TENTHS or not. */
16609 if (quotient <= 9)
16611 tenths = remainder / 100;
16612 if (50 <= remainder % 100)
16614 if (tenths < 9)
16615 tenths++;
16616 else
16618 quotient++;
16619 if (quotient == 10)
16620 tenths = -1;
16621 else
16622 tenths = 0;
16626 else
16627 if (500 <= remainder)
16629 if (quotient < 999)
16630 quotient++;
16631 else
16633 quotient = 1;
16634 exponent++;
16635 tenths = 0;
16640 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16641 if (tenths == -1 && quotient <= 99)
16642 if (quotient <= 9)
16643 length = 1;
16644 else
16645 length = 2;
16646 else
16647 length = 3;
16648 p = psuffix = buf + max (width, length);
16650 /* Print EXPONENT. */
16651 if (exponent)
16652 *psuffix++ = power_letter[exponent];
16653 *psuffix = '\0';
16655 /* Print TENTHS. */
16656 if (tenths >= 0)
16658 *--p = '0' + tenths;
16659 *--p = '.';
16662 /* Print QUOTIENT. */
16665 int digit = quotient % 10;
16666 *--p = '0' + digit;
16668 while ((quotient /= 10) != 0);
16670 /* Print leading spaces. */
16671 while (buf < p)
16672 *--p = ' ';
16675 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16676 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16677 type of CODING_SYSTEM. Return updated pointer into BUF. */
16679 static unsigned char invalid_eol_type[] = "(*invalid*)";
16681 static char *
16682 decode_mode_spec_coding (coding_system, buf, eol_flag)
16683 Lisp_Object coding_system;
16684 register char *buf;
16685 int eol_flag;
16687 Lisp_Object val;
16688 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16689 const unsigned char *eol_str;
16690 int eol_str_len;
16691 /* The EOL conversion we are using. */
16692 Lisp_Object eoltype;
16694 val = Fget (coding_system, Qcoding_system);
16695 eoltype = Qnil;
16697 if (!VECTORP (val)) /* Not yet decided. */
16699 if (multibyte)
16700 *buf++ = '-';
16701 if (eol_flag)
16702 eoltype = eol_mnemonic_undecided;
16703 /* Don't mention EOL conversion if it isn't decided. */
16705 else
16707 Lisp_Object eolvalue;
16709 eolvalue = Fget (coding_system, Qeol_type);
16711 if (multibyte)
16712 *buf++ = XFASTINT (AREF (val, 1));
16714 if (eol_flag)
16716 /* The EOL conversion that is normal on this system. */
16718 if (NILP (eolvalue)) /* Not yet decided. */
16719 eoltype = eol_mnemonic_undecided;
16720 else if (VECTORP (eolvalue)) /* Not yet decided. */
16721 eoltype = eol_mnemonic_undecided;
16722 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16723 eoltype = (XFASTINT (eolvalue) == 0
16724 ? eol_mnemonic_unix
16725 : (XFASTINT (eolvalue) == 1
16726 ? eol_mnemonic_dos : eol_mnemonic_mac));
16730 if (eol_flag)
16732 /* Mention the EOL conversion if it is not the usual one. */
16733 if (STRINGP (eoltype))
16735 eol_str = SDATA (eoltype);
16736 eol_str_len = SBYTES (eoltype);
16738 else if (INTEGERP (eoltype)
16739 && CHAR_VALID_P (XINT (eoltype), 0))
16741 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16742 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16743 eol_str = tmp;
16745 else
16747 eol_str = invalid_eol_type;
16748 eol_str_len = sizeof (invalid_eol_type) - 1;
16750 bcopy (eol_str, buf, eol_str_len);
16751 buf += eol_str_len;
16754 return buf;
16757 /* Return a string for the output of a mode line %-spec for window W,
16758 generated by character C. PRECISION >= 0 means don't return a
16759 string longer than that value. FIELD_WIDTH > 0 means pad the
16760 string returned with spaces to that value. Return 1 in *MULTIBYTE
16761 if the result is multibyte text.
16763 Note we operate on the current buffer for most purposes,
16764 the exception being w->base_line_pos. */
16766 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16768 static char *
16769 decode_mode_spec (w, c, field_width, precision, multibyte)
16770 struct window *w;
16771 register int c;
16772 int field_width, precision;
16773 int *multibyte;
16775 Lisp_Object obj;
16776 struct frame *f = XFRAME (WINDOW_FRAME (w));
16777 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16778 struct buffer *b = current_buffer;
16780 obj = Qnil;
16781 *multibyte = 0;
16783 switch (c)
16785 case '*':
16786 if (!NILP (b->read_only))
16787 return "%";
16788 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16789 return "*";
16790 return "-";
16792 case '+':
16793 /* This differs from %* only for a modified read-only buffer. */
16794 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16795 return "*";
16796 if (!NILP (b->read_only))
16797 return "%";
16798 return "-";
16800 case '&':
16801 /* This differs from %* in ignoring read-only-ness. */
16802 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16803 return "*";
16804 return "-";
16806 case '%':
16807 return "%";
16809 case '[':
16811 int i;
16812 char *p;
16814 if (command_loop_level > 5)
16815 return "[[[... ";
16816 p = decode_mode_spec_buf;
16817 for (i = 0; i < command_loop_level; i++)
16818 *p++ = '[';
16819 *p = 0;
16820 return decode_mode_spec_buf;
16823 case ']':
16825 int i;
16826 char *p;
16828 if (command_loop_level > 5)
16829 return " ...]]]";
16830 p = decode_mode_spec_buf;
16831 for (i = 0; i < command_loop_level; i++)
16832 *p++ = ']';
16833 *p = 0;
16834 return decode_mode_spec_buf;
16837 case '-':
16839 register int i;
16841 /* Let lots_of_dashes be a string of infinite length. */
16842 if (mode_line_target == MODE_LINE_NOPROP ||
16843 mode_line_target == MODE_LINE_STRING)
16844 return "--";
16845 if (field_width <= 0
16846 || field_width > sizeof (lots_of_dashes))
16848 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16849 decode_mode_spec_buf[i] = '-';
16850 decode_mode_spec_buf[i] = '\0';
16851 return decode_mode_spec_buf;
16853 else
16854 return lots_of_dashes;
16857 case 'b':
16858 obj = b->name;
16859 break;
16861 case 'c':
16863 int col = (int) current_column (); /* iftc */
16864 w->column_number_displayed = make_number (col);
16865 pint2str (decode_mode_spec_buf, field_width, col);
16866 return decode_mode_spec_buf;
16869 case 'F':
16870 /* %F displays the frame name. */
16871 if (!NILP (f->title))
16872 return (char *) SDATA (f->title);
16873 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16874 return (char *) SDATA (f->name);
16875 return "Emacs";
16877 case 'f':
16878 obj = b->filename;
16879 break;
16881 case 'i':
16883 int size = ZV - BEGV;
16884 pint2str (decode_mode_spec_buf, field_width, size);
16885 return decode_mode_spec_buf;
16888 case 'I':
16890 int size = ZV - BEGV;
16891 pint2hrstr (decode_mode_spec_buf, field_width, size);
16892 return decode_mode_spec_buf;
16895 case 'l':
16897 int startpos = XMARKER (w->start)->charpos;
16898 int startpos_byte = marker_byte_position (w->start);
16899 int line, linepos, linepos_byte, topline;
16900 int nlines, junk;
16901 int height = WINDOW_TOTAL_LINES (w);
16903 /* If we decided that this buffer isn't suitable for line numbers,
16904 don't forget that too fast. */
16905 if (EQ (w->base_line_pos, w->buffer))
16906 goto no_value;
16907 /* But do forget it, if the window shows a different buffer now. */
16908 else if (BUFFERP (w->base_line_pos))
16909 w->base_line_pos = Qnil;
16911 /* If the buffer is very big, don't waste time. */
16912 if (INTEGERP (Vline_number_display_limit)
16913 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16915 w->base_line_pos = Qnil;
16916 w->base_line_number = Qnil;
16917 goto no_value;
16920 if (!NILP (w->base_line_number)
16921 && !NILP (w->base_line_pos)
16922 && XFASTINT (w->base_line_pos) <= startpos)
16924 line = XFASTINT (w->base_line_number);
16925 linepos = XFASTINT (w->base_line_pos);
16926 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16928 else
16930 line = 1;
16931 linepos = BUF_BEGV (b);
16932 linepos_byte = BUF_BEGV_BYTE (b);
16935 /* Count lines from base line to window start position. */
16936 nlines = display_count_lines (linepos, linepos_byte,
16937 startpos_byte,
16938 startpos, &junk);
16940 topline = nlines + line;
16942 /* Determine a new base line, if the old one is too close
16943 or too far away, or if we did not have one.
16944 "Too close" means it's plausible a scroll-down would
16945 go back past it. */
16946 if (startpos == BUF_BEGV (b))
16948 w->base_line_number = make_number (topline);
16949 w->base_line_pos = make_number (BUF_BEGV (b));
16951 else if (nlines < height + 25 || nlines > height * 3 + 50
16952 || linepos == BUF_BEGV (b))
16954 int limit = BUF_BEGV (b);
16955 int limit_byte = BUF_BEGV_BYTE (b);
16956 int position;
16957 int distance = (height * 2 + 30) * line_number_display_limit_width;
16959 if (startpos - distance > limit)
16961 limit = startpos - distance;
16962 limit_byte = CHAR_TO_BYTE (limit);
16965 nlines = display_count_lines (startpos, startpos_byte,
16966 limit_byte,
16967 - (height * 2 + 30),
16968 &position);
16969 /* If we couldn't find the lines we wanted within
16970 line_number_display_limit_width chars per line,
16971 give up on line numbers for this window. */
16972 if (position == limit_byte && limit == startpos - distance)
16974 w->base_line_pos = w->buffer;
16975 w->base_line_number = Qnil;
16976 goto no_value;
16979 w->base_line_number = make_number (topline - nlines);
16980 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16983 /* Now count lines from the start pos to point. */
16984 nlines = display_count_lines (startpos, startpos_byte,
16985 PT_BYTE, PT, &junk);
16987 /* Record that we did display the line number. */
16988 line_number_displayed = 1;
16990 /* Make the string to show. */
16991 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16992 return decode_mode_spec_buf;
16993 no_value:
16995 char* p = decode_mode_spec_buf;
16996 int pad = field_width - 2;
16997 while (pad-- > 0)
16998 *p++ = ' ';
16999 *p++ = '?';
17000 *p++ = '?';
17001 *p = '\0';
17002 return decode_mode_spec_buf;
17005 break;
17007 case 'm':
17008 obj = b->mode_name;
17009 break;
17011 case 'n':
17012 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17013 return " Narrow";
17014 break;
17016 case 'p':
17018 int pos = marker_position (w->start);
17019 int total = BUF_ZV (b) - BUF_BEGV (b);
17021 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17023 if (pos <= BUF_BEGV (b))
17024 return "All";
17025 else
17026 return "Bottom";
17028 else if (pos <= BUF_BEGV (b))
17029 return "Top";
17030 else
17032 if (total > 1000000)
17033 /* Do it differently for a large value, to avoid overflow. */
17034 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17035 else
17036 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17037 /* We can't normally display a 3-digit number,
17038 so get us a 2-digit number that is close. */
17039 if (total == 100)
17040 total = 99;
17041 sprintf (decode_mode_spec_buf, "%2d%%", total);
17042 return decode_mode_spec_buf;
17046 /* Display percentage of size above the bottom of the screen. */
17047 case 'P':
17049 int toppos = marker_position (w->start);
17050 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17051 int total = BUF_ZV (b) - BUF_BEGV (b);
17053 if (botpos >= BUF_ZV (b))
17055 if (toppos <= BUF_BEGV (b))
17056 return "All";
17057 else
17058 return "Bottom";
17060 else
17062 if (total > 1000000)
17063 /* Do it differently for a large value, to avoid overflow. */
17064 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17065 else
17066 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17067 /* We can't normally display a 3-digit number,
17068 so get us a 2-digit number that is close. */
17069 if (total == 100)
17070 total = 99;
17071 if (toppos <= BUF_BEGV (b))
17072 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17073 else
17074 sprintf (decode_mode_spec_buf, "%2d%%", total);
17075 return decode_mode_spec_buf;
17079 case 's':
17080 /* status of process */
17081 obj = Fget_buffer_process (Fcurrent_buffer ());
17082 if (NILP (obj))
17083 return "no process";
17084 #ifdef subprocesses
17085 obj = Fsymbol_name (Fprocess_status (obj));
17086 #endif
17087 break;
17089 case 't': /* indicate TEXT or BINARY */
17090 #ifdef MODE_LINE_BINARY_TEXT
17091 return MODE_LINE_BINARY_TEXT (b);
17092 #else
17093 return "T";
17094 #endif
17096 case 'z':
17097 /* coding-system (not including end-of-line format) */
17098 case 'Z':
17099 /* coding-system (including end-of-line type) */
17101 int eol_flag = (c == 'Z');
17102 char *p = decode_mode_spec_buf;
17104 if (! FRAME_WINDOW_P (f))
17106 /* No need to mention EOL here--the terminal never needs
17107 to do EOL conversion. */
17108 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17109 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17111 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17112 p, eol_flag);
17114 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17115 #ifdef subprocesses
17116 obj = Fget_buffer_process (Fcurrent_buffer ());
17117 if (PROCESSP (obj))
17119 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17120 p, eol_flag);
17121 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17122 p, eol_flag);
17124 #endif /* subprocesses */
17125 #endif /* 0 */
17126 *p = 0;
17127 return decode_mode_spec_buf;
17131 if (STRINGP (obj))
17133 *multibyte = STRING_MULTIBYTE (obj);
17134 return (char *) SDATA (obj);
17136 else
17137 return "";
17141 /* Count up to COUNT lines starting from START / START_BYTE.
17142 But don't go beyond LIMIT_BYTE.
17143 Return the number of lines thus found (always nonnegative).
17145 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17147 static int
17148 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17149 int start, start_byte, limit_byte, count;
17150 int *byte_pos_ptr;
17152 register unsigned char *cursor;
17153 unsigned char *base;
17155 register int ceiling;
17156 register unsigned char *ceiling_addr;
17157 int orig_count = count;
17159 /* If we are not in selective display mode,
17160 check only for newlines. */
17161 int selective_display = (!NILP (current_buffer->selective_display)
17162 && !INTEGERP (current_buffer->selective_display));
17164 if (count > 0)
17166 while (start_byte < limit_byte)
17168 ceiling = BUFFER_CEILING_OF (start_byte);
17169 ceiling = min (limit_byte - 1, ceiling);
17170 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17171 base = (cursor = BYTE_POS_ADDR (start_byte));
17172 while (1)
17174 if (selective_display)
17175 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17177 else
17178 while (*cursor != '\n' && ++cursor != ceiling_addr)
17181 if (cursor != ceiling_addr)
17183 if (--count == 0)
17185 start_byte += cursor - base + 1;
17186 *byte_pos_ptr = start_byte;
17187 return orig_count;
17189 else
17190 if (++cursor == ceiling_addr)
17191 break;
17193 else
17194 break;
17196 start_byte += cursor - base;
17199 else
17201 while (start_byte > limit_byte)
17203 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17204 ceiling = max (limit_byte, ceiling);
17205 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17206 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17207 while (1)
17209 if (selective_display)
17210 while (--cursor != ceiling_addr
17211 && *cursor != '\n' && *cursor != 015)
17213 else
17214 while (--cursor != ceiling_addr && *cursor != '\n')
17217 if (cursor != ceiling_addr)
17219 if (++count == 0)
17221 start_byte += cursor - base + 1;
17222 *byte_pos_ptr = start_byte;
17223 /* When scanning backwards, we should
17224 not count the newline posterior to which we stop. */
17225 return - orig_count - 1;
17228 else
17229 break;
17231 /* Here we add 1 to compensate for the last decrement
17232 of CURSOR, which took it past the valid range. */
17233 start_byte += cursor - base + 1;
17237 *byte_pos_ptr = limit_byte;
17239 if (count < 0)
17240 return - orig_count + count;
17241 return orig_count - count;
17247 /***********************************************************************
17248 Displaying strings
17249 ***********************************************************************/
17251 /* Display a NUL-terminated string, starting with index START.
17253 If STRING is non-null, display that C string. Otherwise, the Lisp
17254 string LISP_STRING is displayed.
17256 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17257 FACE_STRING. Display STRING or LISP_STRING with the face at
17258 FACE_STRING_POS in FACE_STRING:
17260 Display the string in the environment given by IT, but use the
17261 standard display table, temporarily.
17263 FIELD_WIDTH is the minimum number of output glyphs to produce.
17264 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17265 with spaces. If STRING has more characters, more than FIELD_WIDTH
17266 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17268 PRECISION is the maximum number of characters to output from
17269 STRING. PRECISION < 0 means don't truncate the string.
17271 This is roughly equivalent to printf format specifiers:
17273 FIELD_WIDTH PRECISION PRINTF
17274 ----------------------------------------
17275 -1 -1 %s
17276 -1 10 %.10s
17277 10 -1 %10s
17278 20 10 %20.10s
17280 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17281 display them, and < 0 means obey the current buffer's value of
17282 enable_multibyte_characters.
17284 Value is the number of glyphs produced. */
17286 static int
17287 display_string (string, lisp_string, face_string, face_string_pos,
17288 start, it, field_width, precision, max_x, multibyte)
17289 unsigned char *string;
17290 Lisp_Object lisp_string;
17291 Lisp_Object face_string;
17292 int face_string_pos;
17293 int start;
17294 struct it *it;
17295 int field_width, precision, max_x;
17296 int multibyte;
17298 int hpos_at_start = it->hpos;
17299 int saved_face_id = it->face_id;
17300 struct glyph_row *row = it->glyph_row;
17302 /* Initialize the iterator IT for iteration over STRING beginning
17303 with index START. */
17304 reseat_to_string (it, string, lisp_string, start,
17305 precision, field_width, multibyte);
17307 /* If displaying STRING, set up the face of the iterator
17308 from LISP_STRING, if that's given. */
17309 if (STRINGP (face_string))
17311 int endptr;
17312 struct face *face;
17314 it->face_id
17315 = face_at_string_position (it->w, face_string, face_string_pos,
17316 0, it->region_beg_charpos,
17317 it->region_end_charpos,
17318 &endptr, it->base_face_id, 0);
17319 face = FACE_FROM_ID (it->f, it->face_id);
17320 it->face_box_p = face->box != FACE_NO_BOX;
17323 /* Set max_x to the maximum allowed X position. Don't let it go
17324 beyond the right edge of the window. */
17325 if (max_x <= 0)
17326 max_x = it->last_visible_x;
17327 else
17328 max_x = min (max_x, it->last_visible_x);
17330 /* Skip over display elements that are not visible. because IT->w is
17331 hscrolled. */
17332 if (it->current_x < it->first_visible_x)
17333 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17334 MOVE_TO_POS | MOVE_TO_X);
17336 row->ascent = it->max_ascent;
17337 row->height = it->max_ascent + it->max_descent;
17338 row->phys_ascent = it->max_phys_ascent;
17339 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17340 row->extra_line_spacing = it->max_extra_line_spacing;
17342 /* This condition is for the case that we are called with current_x
17343 past last_visible_x. */
17344 while (it->current_x < max_x)
17346 int x_before, x, n_glyphs_before, i, nglyphs;
17348 /* Get the next display element. */
17349 if (!get_next_display_element (it))
17350 break;
17352 /* Produce glyphs. */
17353 x_before = it->current_x;
17354 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17355 PRODUCE_GLYPHS (it);
17357 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17358 i = 0;
17359 x = x_before;
17360 while (i < nglyphs)
17362 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17364 if (!it->truncate_lines_p
17365 && x + glyph->pixel_width > max_x)
17367 /* End of continued line or max_x reached. */
17368 if (CHAR_GLYPH_PADDING_P (*glyph))
17370 /* A wide character is unbreakable. */
17371 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17372 it->current_x = x_before;
17374 else
17376 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17377 it->current_x = x;
17379 break;
17381 else if (x + glyph->pixel_width > it->first_visible_x)
17383 /* Glyph is at least partially visible. */
17384 ++it->hpos;
17385 if (x < it->first_visible_x)
17386 it->glyph_row->x = x - it->first_visible_x;
17388 else
17390 /* Glyph is off the left margin of the display area.
17391 Should not happen. */
17392 abort ();
17395 row->ascent = max (row->ascent, it->max_ascent);
17396 row->height = max (row->height, it->max_ascent + it->max_descent);
17397 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17398 row->phys_height = max (row->phys_height,
17399 it->max_phys_ascent + it->max_phys_descent);
17400 row->extra_line_spacing = max (row->extra_line_spacing,
17401 it->max_extra_line_spacing);
17402 x += glyph->pixel_width;
17403 ++i;
17406 /* Stop if max_x reached. */
17407 if (i < nglyphs)
17408 break;
17410 /* Stop at line ends. */
17411 if (ITERATOR_AT_END_OF_LINE_P (it))
17413 it->continuation_lines_width = 0;
17414 break;
17417 set_iterator_to_next (it, 1);
17419 /* Stop if truncating at the right edge. */
17420 if (it->truncate_lines_p
17421 && it->current_x >= it->last_visible_x)
17423 /* Add truncation mark, but don't do it if the line is
17424 truncated at a padding space. */
17425 if (IT_CHARPOS (*it) < it->string_nchars)
17427 if (!FRAME_WINDOW_P (it->f))
17429 int i, n;
17431 if (it->current_x > it->last_visible_x)
17433 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17434 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17435 break;
17436 for (n = row->used[TEXT_AREA]; i < n; ++i)
17438 row->used[TEXT_AREA] = i;
17439 produce_special_glyphs (it, IT_TRUNCATION);
17442 produce_special_glyphs (it, IT_TRUNCATION);
17444 it->glyph_row->truncated_on_right_p = 1;
17446 break;
17450 /* Maybe insert a truncation at the left. */
17451 if (it->first_visible_x
17452 && IT_CHARPOS (*it) > 0)
17454 if (!FRAME_WINDOW_P (it->f))
17455 insert_left_trunc_glyphs (it);
17456 it->glyph_row->truncated_on_left_p = 1;
17459 it->face_id = saved_face_id;
17461 /* Value is number of columns displayed. */
17462 return it->hpos - hpos_at_start;
17467 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17468 appears as an element of LIST or as the car of an element of LIST.
17469 If PROPVAL is a list, compare each element against LIST in that
17470 way, and return 1/2 if any element of PROPVAL is found in LIST.
17471 Otherwise return 0. This function cannot quit.
17472 The return value is 2 if the text is invisible but with an ellipsis
17473 and 1 if it's invisible and without an ellipsis. */
17476 invisible_p (propval, list)
17477 register Lisp_Object propval;
17478 Lisp_Object list;
17480 register Lisp_Object tail, proptail;
17482 for (tail = list; CONSP (tail); tail = XCDR (tail))
17484 register Lisp_Object tem;
17485 tem = XCAR (tail);
17486 if (EQ (propval, tem))
17487 return 1;
17488 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17489 return NILP (XCDR (tem)) ? 1 : 2;
17492 if (CONSP (propval))
17494 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17496 Lisp_Object propelt;
17497 propelt = XCAR (proptail);
17498 for (tail = list; CONSP (tail); tail = XCDR (tail))
17500 register Lisp_Object tem;
17501 tem = XCAR (tail);
17502 if (EQ (propelt, tem))
17503 return 1;
17504 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17505 return NILP (XCDR (tem)) ? 1 : 2;
17510 return 0;
17513 /* Calculate a width or height in pixels from a specification using
17514 the following elements:
17516 SPEC ::=
17517 NUM - a (fractional) multiple of the default font width/height
17518 (NUM) - specifies exactly NUM pixels
17519 UNIT - a fixed number of pixels, see below.
17520 ELEMENT - size of a display element in pixels, see below.
17521 (NUM . SPEC) - equals NUM * SPEC
17522 (+ SPEC SPEC ...) - add pixel values
17523 (- SPEC SPEC ...) - subtract pixel values
17524 (- SPEC) - negate pixel value
17526 NUM ::=
17527 INT or FLOAT - a number constant
17528 SYMBOL - use symbol's (buffer local) variable binding.
17530 UNIT ::=
17531 in - pixels per inch *)
17532 mm - pixels per 1/1000 meter *)
17533 cm - pixels per 1/100 meter *)
17534 width - width of current font in pixels.
17535 height - height of current font in pixels.
17537 *) using the ratio(s) defined in display-pixels-per-inch.
17539 ELEMENT ::=
17541 left-fringe - left fringe width in pixels
17542 right-fringe - right fringe width in pixels
17544 left-margin - left margin width in pixels
17545 right-margin - right margin width in pixels
17547 scroll-bar - scroll-bar area width in pixels
17549 Examples:
17551 Pixels corresponding to 5 inches:
17552 (5 . in)
17554 Total width of non-text areas on left side of window (if scroll-bar is on left):
17555 '(space :width (+ left-fringe left-margin scroll-bar))
17557 Align to first text column (in header line):
17558 '(space :align-to 0)
17560 Align to middle of text area minus half the width of variable `my-image'
17561 containing a loaded image:
17562 '(space :align-to (0.5 . (- text my-image)))
17564 Width of left margin minus width of 1 character in the default font:
17565 '(space :width (- left-margin 1))
17567 Width of left margin minus width of 2 characters in the current font:
17568 '(space :width (- left-margin (2 . width)))
17570 Center 1 character over left-margin (in header line):
17571 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17573 Different ways to express width of left fringe plus left margin minus one pixel:
17574 '(space :width (- (+ left-fringe left-margin) (1)))
17575 '(space :width (+ left-fringe left-margin (- (1))))
17576 '(space :width (+ left-fringe left-margin (-1)))
17580 #define NUMVAL(X) \
17581 ((INTEGERP (X) || FLOATP (X)) \
17582 ? XFLOATINT (X) \
17583 : - 1)
17586 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17587 double *res;
17588 struct it *it;
17589 Lisp_Object prop;
17590 void *font;
17591 int width_p, *align_to;
17593 double pixels;
17595 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17596 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17598 if (NILP (prop))
17599 return OK_PIXELS (0);
17601 if (SYMBOLP (prop))
17603 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17605 char *unit = SDATA (SYMBOL_NAME (prop));
17607 if (unit[0] == 'i' && unit[1] == 'n')
17608 pixels = 1.0;
17609 else if (unit[0] == 'm' && unit[1] == 'm')
17610 pixels = 25.4;
17611 else if (unit[0] == 'c' && unit[1] == 'm')
17612 pixels = 2.54;
17613 else
17614 pixels = 0;
17615 if (pixels > 0)
17617 double ppi;
17618 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17619 || (CONSP (Vdisplay_pixels_per_inch)
17620 && (ppi = (width_p
17621 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17622 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17623 ppi > 0)))
17624 return OK_PIXELS (ppi / pixels);
17626 return 0;
17630 #ifdef HAVE_WINDOW_SYSTEM
17631 if (EQ (prop, Qheight))
17632 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17633 if (EQ (prop, Qwidth))
17634 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17635 #else
17636 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17637 return OK_PIXELS (1);
17638 #endif
17640 if (EQ (prop, Qtext))
17641 return OK_PIXELS (width_p
17642 ? window_box_width (it->w, TEXT_AREA)
17643 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17645 if (align_to && *align_to < 0)
17647 *res = 0;
17648 if (EQ (prop, Qleft))
17649 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17650 if (EQ (prop, Qright))
17651 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17652 if (EQ (prop, Qcenter))
17653 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17654 + window_box_width (it->w, TEXT_AREA) / 2);
17655 if (EQ (prop, Qleft_fringe))
17656 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17657 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17658 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17659 if (EQ (prop, Qright_fringe))
17660 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17661 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17662 : window_box_right_offset (it->w, TEXT_AREA));
17663 if (EQ (prop, Qleft_margin))
17664 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17665 if (EQ (prop, Qright_margin))
17666 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17667 if (EQ (prop, Qscroll_bar))
17668 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17670 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17671 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17672 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17673 : 0)));
17675 else
17677 if (EQ (prop, Qleft_fringe))
17678 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17679 if (EQ (prop, Qright_fringe))
17680 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17681 if (EQ (prop, Qleft_margin))
17682 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17683 if (EQ (prop, Qright_margin))
17684 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17685 if (EQ (prop, Qscroll_bar))
17686 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17689 prop = Fbuffer_local_value (prop, it->w->buffer);
17692 if (INTEGERP (prop) || FLOATP (prop))
17694 int base_unit = (width_p
17695 ? FRAME_COLUMN_WIDTH (it->f)
17696 : FRAME_LINE_HEIGHT (it->f));
17697 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17700 if (CONSP (prop))
17702 Lisp_Object car = XCAR (prop);
17703 Lisp_Object cdr = XCDR (prop);
17705 if (SYMBOLP (car))
17707 #ifdef HAVE_WINDOW_SYSTEM
17708 if (valid_image_p (prop))
17710 int id = lookup_image (it->f, prop);
17711 struct image *img = IMAGE_FROM_ID (it->f, id);
17713 return OK_PIXELS (width_p ? img->width : img->height);
17715 #endif
17716 if (EQ (car, Qplus) || EQ (car, Qminus))
17718 int first = 1;
17719 double px;
17721 pixels = 0;
17722 while (CONSP (cdr))
17724 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17725 font, width_p, align_to))
17726 return 0;
17727 if (first)
17728 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17729 else
17730 pixels += px;
17731 cdr = XCDR (cdr);
17733 if (EQ (car, Qminus))
17734 pixels = -pixels;
17735 return OK_PIXELS (pixels);
17738 car = Fbuffer_local_value (car, it->w->buffer);
17741 if (INTEGERP (car) || FLOATP (car))
17743 double fact;
17744 pixels = XFLOATINT (car);
17745 if (NILP (cdr))
17746 return OK_PIXELS (pixels);
17747 if (calc_pixel_width_or_height (&fact, it, cdr,
17748 font, width_p, align_to))
17749 return OK_PIXELS (pixels * fact);
17750 return 0;
17753 return 0;
17756 return 0;
17760 /***********************************************************************
17761 Glyph Display
17762 ***********************************************************************/
17764 #ifdef HAVE_WINDOW_SYSTEM
17766 #if GLYPH_DEBUG
17768 void
17769 dump_glyph_string (s)
17770 struct glyph_string *s;
17772 fprintf (stderr, "glyph string\n");
17773 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17774 s->x, s->y, s->width, s->height);
17775 fprintf (stderr, " ybase = %d\n", s->ybase);
17776 fprintf (stderr, " hl = %d\n", s->hl);
17777 fprintf (stderr, " left overhang = %d, right = %d\n",
17778 s->left_overhang, s->right_overhang);
17779 fprintf (stderr, " nchars = %d\n", s->nchars);
17780 fprintf (stderr, " extends to end of line = %d\n",
17781 s->extends_to_end_of_line_p);
17782 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17783 fprintf (stderr, " bg width = %d\n", s->background_width);
17786 #endif /* GLYPH_DEBUG */
17788 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17789 of XChar2b structures for S; it can't be allocated in
17790 init_glyph_string because it must be allocated via `alloca'. W
17791 is the window on which S is drawn. ROW and AREA are the glyph row
17792 and area within the row from which S is constructed. START is the
17793 index of the first glyph structure covered by S. HL is a
17794 face-override for drawing S. */
17796 #ifdef HAVE_NTGUI
17797 #define OPTIONAL_HDC(hdc) hdc,
17798 #define DECLARE_HDC(hdc) HDC hdc;
17799 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17800 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17801 #endif
17803 #ifndef OPTIONAL_HDC
17804 #define OPTIONAL_HDC(hdc)
17805 #define DECLARE_HDC(hdc)
17806 #define ALLOCATE_HDC(hdc, f)
17807 #define RELEASE_HDC(hdc, f)
17808 #endif
17810 static void
17811 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17812 struct glyph_string *s;
17813 DECLARE_HDC (hdc)
17814 XChar2b *char2b;
17815 struct window *w;
17816 struct glyph_row *row;
17817 enum glyph_row_area area;
17818 int start;
17819 enum draw_glyphs_face hl;
17821 bzero (s, sizeof *s);
17822 s->w = w;
17823 s->f = XFRAME (w->frame);
17824 #ifdef HAVE_NTGUI
17825 s->hdc = hdc;
17826 #endif
17827 s->display = FRAME_X_DISPLAY (s->f);
17828 s->window = FRAME_X_WINDOW (s->f);
17829 s->char2b = char2b;
17830 s->hl = hl;
17831 s->row = row;
17832 s->area = area;
17833 s->first_glyph = row->glyphs[area] + start;
17834 s->height = row->height;
17835 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17837 /* Display the internal border below the tool-bar window. */
17838 if (WINDOWP (s->f->tool_bar_window)
17839 && s->w == XWINDOW (s->f->tool_bar_window))
17840 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17842 s->ybase = s->y + row->ascent;
17846 /* Append the list of glyph strings with head H and tail T to the list
17847 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17849 static INLINE void
17850 append_glyph_string_lists (head, tail, h, t)
17851 struct glyph_string **head, **tail;
17852 struct glyph_string *h, *t;
17854 if (h)
17856 if (*head)
17857 (*tail)->next = h;
17858 else
17859 *head = h;
17860 h->prev = *tail;
17861 *tail = t;
17866 /* Prepend the list of glyph strings with head H and tail T to the
17867 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17868 result. */
17870 static INLINE void
17871 prepend_glyph_string_lists (head, tail, h, t)
17872 struct glyph_string **head, **tail;
17873 struct glyph_string *h, *t;
17875 if (h)
17877 if (*head)
17878 (*head)->prev = t;
17879 else
17880 *tail = t;
17881 t->next = *head;
17882 *head = h;
17887 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17888 Set *HEAD and *TAIL to the resulting list. */
17890 static INLINE void
17891 append_glyph_string (head, tail, s)
17892 struct glyph_string **head, **tail;
17893 struct glyph_string *s;
17895 s->next = s->prev = NULL;
17896 append_glyph_string_lists (head, tail, s, s);
17900 /* Get face and two-byte form of character glyph GLYPH on frame F.
17901 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17902 a pointer to a realized face that is ready for display. */
17904 static INLINE struct face *
17905 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17906 struct frame *f;
17907 struct glyph *glyph;
17908 XChar2b *char2b;
17909 int *two_byte_p;
17911 struct face *face;
17913 xassert (glyph->type == CHAR_GLYPH);
17914 face = FACE_FROM_ID (f, glyph->face_id);
17916 if (two_byte_p)
17917 *two_byte_p = 0;
17919 if (!glyph->multibyte_p)
17921 /* Unibyte case. We don't have to encode, but we have to make
17922 sure to use a face suitable for unibyte. */
17923 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17925 else if (glyph->u.ch < 128
17926 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17928 /* Case of ASCII in a face known to fit ASCII. */
17929 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17931 else
17933 int c1, c2, charset;
17935 /* Split characters into bytes. If c2 is -1 afterwards, C is
17936 really a one-byte character so that byte1 is zero. */
17937 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17938 if (c2 > 0)
17939 STORE_XCHAR2B (char2b, c1, c2);
17940 else
17941 STORE_XCHAR2B (char2b, 0, c1);
17943 /* Maybe encode the character in *CHAR2B. */
17944 if (charset != CHARSET_ASCII)
17946 struct font_info *font_info
17947 = FONT_INFO_FROM_ID (f, face->font_info_id);
17948 if (font_info)
17949 glyph->font_type
17950 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17954 /* Make sure X resources of the face are allocated. */
17955 xassert (face != NULL);
17956 PREPARE_FACE_FOR_DISPLAY (f, face);
17957 return face;
17961 /* Fill glyph string S with composition components specified by S->cmp.
17963 FACES is an array of faces for all components of this composition.
17964 S->gidx is the index of the first component for S.
17965 OVERLAPS_P non-zero means S should draw the foreground only, and
17966 use its physical height for clipping.
17968 Value is the index of a component not in S. */
17970 static int
17971 fill_composite_glyph_string (s, faces, overlaps_p)
17972 struct glyph_string *s;
17973 struct face **faces;
17974 int overlaps_p;
17976 int i;
17978 xassert (s);
17980 s->for_overlaps_p = overlaps_p;
17982 s->face = faces[s->gidx];
17983 s->font = s->face->font;
17984 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17986 /* For all glyphs of this composition, starting at the offset
17987 S->gidx, until we reach the end of the definition or encounter a
17988 glyph that requires the different face, add it to S. */
17989 ++s->nchars;
17990 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17991 ++s->nchars;
17993 /* All glyph strings for the same composition has the same width,
17994 i.e. the width set for the first component of the composition. */
17996 s->width = s->first_glyph->pixel_width;
17998 /* If the specified font could not be loaded, use the frame's
17999 default font, but record the fact that we couldn't load it in
18000 the glyph string so that we can draw rectangles for the
18001 characters of the glyph string. */
18002 if (s->font == NULL)
18004 s->font_not_found_p = 1;
18005 s->font = FRAME_FONT (s->f);
18008 /* Adjust base line for subscript/superscript text. */
18009 s->ybase += s->first_glyph->voffset;
18011 xassert (s->face && s->face->gc);
18013 /* This glyph string must always be drawn with 16-bit functions. */
18014 s->two_byte_p = 1;
18016 return s->gidx + s->nchars;
18020 /* Fill glyph string S from a sequence of character glyphs.
18022 FACE_ID is the face id of the string. START is the index of the
18023 first glyph to consider, END is the index of the last + 1.
18024 OVERLAPS_P non-zero means S should draw the foreground only, and
18025 use its physical height for clipping.
18027 Value is the index of the first glyph not in S. */
18029 static int
18030 fill_glyph_string (s, face_id, start, end, overlaps_p)
18031 struct glyph_string *s;
18032 int face_id;
18033 int start, end, overlaps_p;
18035 struct glyph *glyph, *last;
18036 int voffset;
18037 int glyph_not_available_p;
18039 xassert (s->f == XFRAME (s->w->frame));
18040 xassert (s->nchars == 0);
18041 xassert (start >= 0 && end > start);
18043 s->for_overlaps_p = overlaps_p,
18044 glyph = s->row->glyphs[s->area] + start;
18045 last = s->row->glyphs[s->area] + end;
18046 voffset = glyph->voffset;
18048 glyph_not_available_p = glyph->glyph_not_available_p;
18050 while (glyph < last
18051 && glyph->type == CHAR_GLYPH
18052 && glyph->voffset == voffset
18053 /* Same face id implies same font, nowadays. */
18054 && glyph->face_id == face_id
18055 && glyph->glyph_not_available_p == glyph_not_available_p)
18057 int two_byte_p;
18059 s->face = get_glyph_face_and_encoding (s->f, glyph,
18060 s->char2b + s->nchars,
18061 &two_byte_p);
18062 s->two_byte_p = two_byte_p;
18063 ++s->nchars;
18064 xassert (s->nchars <= end - start);
18065 s->width += glyph->pixel_width;
18066 ++glyph;
18069 s->font = s->face->font;
18070 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18072 /* If the specified font could not be loaded, use the frame's font,
18073 but record the fact that we couldn't load it in
18074 S->font_not_found_p so that we can draw rectangles for the
18075 characters of the glyph string. */
18076 if (s->font == NULL || glyph_not_available_p)
18078 s->font_not_found_p = 1;
18079 s->font = FRAME_FONT (s->f);
18082 /* Adjust base line for subscript/superscript text. */
18083 s->ybase += voffset;
18085 xassert (s->face && s->face->gc);
18086 return glyph - s->row->glyphs[s->area];
18090 /* Fill glyph string S from image glyph S->first_glyph. */
18092 static void
18093 fill_image_glyph_string (s)
18094 struct glyph_string *s;
18096 xassert (s->first_glyph->type == IMAGE_GLYPH);
18097 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18098 xassert (s->img);
18099 s->slice = s->first_glyph->slice;
18100 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18101 s->font = s->face->font;
18102 s->width = s->first_glyph->pixel_width;
18104 /* Adjust base line for subscript/superscript text. */
18105 s->ybase += s->first_glyph->voffset;
18109 /* Fill glyph string S from a sequence of stretch glyphs.
18111 ROW is the glyph row in which the glyphs are found, AREA is the
18112 area within the row. START is the index of the first glyph to
18113 consider, END is the index of the last + 1.
18115 Value is the index of the first glyph not in S. */
18117 static int
18118 fill_stretch_glyph_string (s, row, area, start, end)
18119 struct glyph_string *s;
18120 struct glyph_row *row;
18121 enum glyph_row_area area;
18122 int start, end;
18124 struct glyph *glyph, *last;
18125 int voffset, face_id;
18127 xassert (s->first_glyph->type == STRETCH_GLYPH);
18129 glyph = s->row->glyphs[s->area] + start;
18130 last = s->row->glyphs[s->area] + end;
18131 face_id = glyph->face_id;
18132 s->face = FACE_FROM_ID (s->f, face_id);
18133 s->font = s->face->font;
18134 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18135 s->width = glyph->pixel_width;
18136 voffset = glyph->voffset;
18138 for (++glyph;
18139 (glyph < last
18140 && glyph->type == STRETCH_GLYPH
18141 && glyph->voffset == voffset
18142 && glyph->face_id == face_id);
18143 ++glyph)
18144 s->width += glyph->pixel_width;
18146 /* Adjust base line for subscript/superscript text. */
18147 s->ybase += voffset;
18149 /* The case that face->gc == 0 is handled when drawing the glyph
18150 string by calling PREPARE_FACE_FOR_DISPLAY. */
18151 xassert (s->face);
18152 return glyph - s->row->glyphs[s->area];
18156 /* EXPORT for RIF:
18157 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18158 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18159 assumed to be zero. */
18161 void
18162 x_get_glyph_overhangs (glyph, f, left, right)
18163 struct glyph *glyph;
18164 struct frame *f;
18165 int *left, *right;
18167 *left = *right = 0;
18169 if (glyph->type == CHAR_GLYPH)
18171 XFontStruct *font;
18172 struct face *face;
18173 struct font_info *font_info;
18174 XChar2b char2b;
18175 XCharStruct *pcm;
18177 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18178 font = face->font;
18179 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18180 if (font /* ++KFS: Should this be font_info ? */
18181 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18183 if (pcm->rbearing > pcm->width)
18184 *right = pcm->rbearing - pcm->width;
18185 if (pcm->lbearing < 0)
18186 *left = -pcm->lbearing;
18192 /* Return the index of the first glyph preceding glyph string S that
18193 is overwritten by S because of S's left overhang. Value is -1
18194 if no glyphs are overwritten. */
18196 static int
18197 left_overwritten (s)
18198 struct glyph_string *s;
18200 int k;
18202 if (s->left_overhang)
18204 int x = 0, i;
18205 struct glyph *glyphs = s->row->glyphs[s->area];
18206 int first = s->first_glyph - glyphs;
18208 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18209 x -= glyphs[i].pixel_width;
18211 k = i + 1;
18213 else
18214 k = -1;
18216 return k;
18220 /* Return the index of the first glyph preceding glyph string S that
18221 is overwriting S because of its right overhang. Value is -1 if no
18222 glyph in front of S overwrites S. */
18224 static int
18225 left_overwriting (s)
18226 struct glyph_string *s;
18228 int i, k, x;
18229 struct glyph *glyphs = s->row->glyphs[s->area];
18230 int first = s->first_glyph - glyphs;
18232 k = -1;
18233 x = 0;
18234 for (i = first - 1; i >= 0; --i)
18236 int left, right;
18237 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18238 if (x + right > 0)
18239 k = i;
18240 x -= glyphs[i].pixel_width;
18243 return k;
18247 /* Return the index of the last glyph following glyph string S that is
18248 not overwritten by S because of S's right overhang. Value is -1 if
18249 no such glyph is found. */
18251 static int
18252 right_overwritten (s)
18253 struct glyph_string *s;
18255 int k = -1;
18257 if (s->right_overhang)
18259 int x = 0, i;
18260 struct glyph *glyphs = s->row->glyphs[s->area];
18261 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18262 int end = s->row->used[s->area];
18264 for (i = first; i < end && s->right_overhang > x; ++i)
18265 x += glyphs[i].pixel_width;
18267 k = i;
18270 return k;
18274 /* Return the index of the last glyph following glyph string S that
18275 overwrites S because of its left overhang. Value is negative
18276 if no such glyph is found. */
18278 static int
18279 right_overwriting (s)
18280 struct glyph_string *s;
18282 int i, k, x;
18283 int end = s->row->used[s->area];
18284 struct glyph *glyphs = s->row->glyphs[s->area];
18285 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18287 k = -1;
18288 x = 0;
18289 for (i = first; i < end; ++i)
18291 int left, right;
18292 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18293 if (x - left < 0)
18294 k = i;
18295 x += glyphs[i].pixel_width;
18298 return k;
18302 /* Get face and two-byte form of character C in face FACE_ID on frame
18303 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18304 means we want to display multibyte text. DISPLAY_P non-zero means
18305 make sure that X resources for the face returned are allocated.
18306 Value is a pointer to a realized face that is ready for display if
18307 DISPLAY_P is non-zero. */
18309 static INLINE struct face *
18310 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18311 struct frame *f;
18312 int c, face_id;
18313 XChar2b *char2b;
18314 int multibyte_p, display_p;
18316 struct face *face = FACE_FROM_ID (f, face_id);
18318 if (!multibyte_p)
18320 /* Unibyte case. We don't have to encode, but we have to make
18321 sure to use a face suitable for unibyte. */
18322 STORE_XCHAR2B (char2b, 0, c);
18323 face_id = FACE_FOR_CHAR (f, face, c);
18324 face = FACE_FROM_ID (f, face_id);
18326 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18328 /* Case of ASCII in a face known to fit ASCII. */
18329 STORE_XCHAR2B (char2b, 0, c);
18331 else
18333 int c1, c2, charset;
18335 /* Split characters into bytes. If c2 is -1 afterwards, C is
18336 really a one-byte character so that byte1 is zero. */
18337 SPLIT_CHAR (c, charset, c1, c2);
18338 if (c2 > 0)
18339 STORE_XCHAR2B (char2b, c1, c2);
18340 else
18341 STORE_XCHAR2B (char2b, 0, c1);
18343 /* Maybe encode the character in *CHAR2B. */
18344 if (face->font != NULL)
18346 struct font_info *font_info
18347 = FONT_INFO_FROM_ID (f, face->font_info_id);
18348 if (font_info)
18349 rif->encode_char (c, char2b, font_info, 0);
18353 /* Make sure X resources of the face are allocated. */
18354 #ifdef HAVE_X_WINDOWS
18355 if (display_p)
18356 #endif
18358 xassert (face != NULL);
18359 PREPARE_FACE_FOR_DISPLAY (f, face);
18362 return face;
18366 /* Set background width of glyph string S. START is the index of the
18367 first glyph following S. LAST_X is the right-most x-position + 1
18368 in the drawing area. */
18370 static INLINE void
18371 set_glyph_string_background_width (s, start, last_x)
18372 struct glyph_string *s;
18373 int start;
18374 int last_x;
18376 /* If the face of this glyph string has to be drawn to the end of
18377 the drawing area, set S->extends_to_end_of_line_p. */
18378 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18380 if (start == s->row->used[s->area]
18381 && s->area == TEXT_AREA
18382 && ((s->hl == DRAW_NORMAL_TEXT
18383 && (s->row->fill_line_p
18384 || s->face->background != default_face->background
18385 || s->face->stipple != default_face->stipple
18386 || s->row->mouse_face_p))
18387 || s->hl == DRAW_MOUSE_FACE
18388 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18389 && s->row->fill_line_p)))
18390 s->extends_to_end_of_line_p = 1;
18392 /* If S extends its face to the end of the line, set its
18393 background_width to the distance to the right edge of the drawing
18394 area. */
18395 if (s->extends_to_end_of_line_p)
18396 s->background_width = last_x - s->x + 1;
18397 else
18398 s->background_width = s->width;
18402 /* Compute overhangs and x-positions for glyph string S and its
18403 predecessors, or successors. X is the starting x-position for S.
18404 BACKWARD_P non-zero means process predecessors. */
18406 static void
18407 compute_overhangs_and_x (s, x, backward_p)
18408 struct glyph_string *s;
18409 int x;
18410 int backward_p;
18412 if (backward_p)
18414 while (s)
18416 if (rif->compute_glyph_string_overhangs)
18417 rif->compute_glyph_string_overhangs (s);
18418 x -= s->width;
18419 s->x = x;
18420 s = s->prev;
18423 else
18425 while (s)
18427 if (rif->compute_glyph_string_overhangs)
18428 rif->compute_glyph_string_overhangs (s);
18429 s->x = x;
18430 x += s->width;
18431 s = s->next;
18438 /* The following macros are only called from draw_glyphs below.
18439 They reference the following parameters of that function directly:
18440 `w', `row', `area', and `overlap_p'
18441 as well as the following local variables:
18442 `s', `f', and `hdc' (in W32) */
18444 #ifdef HAVE_NTGUI
18445 /* On W32, silently add local `hdc' variable to argument list of
18446 init_glyph_string. */
18447 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18448 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18449 #else
18450 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18451 init_glyph_string (s, char2b, w, row, area, start, hl)
18452 #endif
18454 /* Add a glyph string for a stretch glyph to the list of strings
18455 between HEAD and TAIL. START is the index of the stretch glyph in
18456 row area AREA of glyph row ROW. END is the index of the last glyph
18457 in that glyph row area. X is the current output position assigned
18458 to the new glyph string constructed. HL overrides that face of the
18459 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18460 is the right-most x-position of the drawing area. */
18462 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18463 and below -- keep them on one line. */
18464 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18465 do \
18467 s = (struct glyph_string *) alloca (sizeof *s); \
18468 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18469 START = fill_stretch_glyph_string (s, row, area, START, END); \
18470 append_glyph_string (&HEAD, &TAIL, s); \
18471 s->x = (X); \
18473 while (0)
18476 /* Add a glyph string for an image glyph to the list of strings
18477 between HEAD and TAIL. START is the index of the image glyph in
18478 row area AREA of glyph row ROW. END is the index of the last glyph
18479 in that glyph row area. X is the current output position assigned
18480 to the new glyph string constructed. HL overrides that face of the
18481 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18482 is the right-most x-position of the drawing area. */
18484 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18485 do \
18487 s = (struct glyph_string *) alloca (sizeof *s); \
18488 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18489 fill_image_glyph_string (s); \
18490 append_glyph_string (&HEAD, &TAIL, s); \
18491 ++START; \
18492 s->x = (X); \
18494 while (0)
18497 /* Add a glyph string for a sequence of character glyphs to the list
18498 of strings between HEAD and TAIL. START is the index of the first
18499 glyph in row area AREA of glyph row ROW that is part of the new
18500 glyph string. END is the index of the last glyph in that glyph row
18501 area. X is the current output position assigned to the new glyph
18502 string constructed. HL overrides that face of the glyph; e.g. it
18503 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18504 right-most x-position of the drawing area. */
18506 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18507 do \
18509 int c, face_id; \
18510 XChar2b *char2b; \
18512 c = (row)->glyphs[area][START].u.ch; \
18513 face_id = (row)->glyphs[area][START].face_id; \
18515 s = (struct glyph_string *) alloca (sizeof *s); \
18516 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18517 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18518 append_glyph_string (&HEAD, &TAIL, s); \
18519 s->x = (X); \
18520 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18522 while (0)
18525 /* Add a glyph string for a composite sequence to the list of strings
18526 between HEAD and TAIL. START is the index of the first glyph in
18527 row area AREA of glyph row ROW that is part of the new glyph
18528 string. END is the index of the last glyph in that glyph row area.
18529 X is the current output position assigned to the new glyph string
18530 constructed. HL overrides that face of the glyph; e.g. it is
18531 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18532 x-position of the drawing area. */
18534 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18535 do { \
18536 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18537 int face_id = (row)->glyphs[area][START].face_id; \
18538 struct face *base_face = FACE_FROM_ID (f, face_id); \
18539 struct composition *cmp = composition_table[cmp_id]; \
18540 int glyph_len = cmp->glyph_len; \
18541 XChar2b *char2b; \
18542 struct face **faces; \
18543 struct glyph_string *first_s = NULL; \
18544 int n; \
18546 base_face = base_face->ascii_face; \
18547 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18548 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18549 /* At first, fill in `char2b' and `faces'. */ \
18550 for (n = 0; n < glyph_len; n++) \
18552 int c = COMPOSITION_GLYPH (cmp, n); \
18553 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18554 faces[n] = FACE_FROM_ID (f, this_face_id); \
18555 get_char_face_and_encoding (f, c, this_face_id, \
18556 char2b + n, 1, 1); \
18559 /* Make glyph_strings for each glyph sequence that is drawable by \
18560 the same face, and append them to HEAD/TAIL. */ \
18561 for (n = 0; n < cmp->glyph_len;) \
18563 s = (struct glyph_string *) alloca (sizeof *s); \
18564 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18565 append_glyph_string (&(HEAD), &(TAIL), s); \
18566 s->cmp = cmp; \
18567 s->gidx = n; \
18568 s->x = (X); \
18570 if (n == 0) \
18571 first_s = s; \
18573 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18576 ++START; \
18577 s = first_s; \
18578 } while (0)
18581 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18582 of AREA of glyph row ROW on window W between indices START and END.
18583 HL overrides the face for drawing glyph strings, e.g. it is
18584 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18585 x-positions of the drawing area.
18587 This is an ugly monster macro construct because we must use alloca
18588 to allocate glyph strings (because draw_glyphs can be called
18589 asynchronously). */
18591 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18592 do \
18594 HEAD = TAIL = NULL; \
18595 while (START < END) \
18597 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18598 switch (first_glyph->type) \
18600 case CHAR_GLYPH: \
18601 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18602 HL, X, LAST_X); \
18603 break; \
18605 case COMPOSITE_GLYPH: \
18606 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18607 HL, X, LAST_X); \
18608 break; \
18610 case STRETCH_GLYPH: \
18611 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18612 HL, X, LAST_X); \
18613 break; \
18615 case IMAGE_GLYPH: \
18616 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18617 HL, X, LAST_X); \
18618 break; \
18620 default: \
18621 abort (); \
18624 set_glyph_string_background_width (s, START, LAST_X); \
18625 (X) += s->width; \
18628 while (0)
18631 /* Draw glyphs between START and END in AREA of ROW on window W,
18632 starting at x-position X. X is relative to AREA in W. HL is a
18633 face-override with the following meaning:
18635 DRAW_NORMAL_TEXT draw normally
18636 DRAW_CURSOR draw in cursor face
18637 DRAW_MOUSE_FACE draw in mouse face.
18638 DRAW_INVERSE_VIDEO draw in mode line face
18639 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18640 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18642 If OVERLAPS_P is non-zero, draw only the foreground of characters
18643 and clip to the physical height of ROW.
18645 Value is the x-position reached, relative to AREA of W. */
18647 static int
18648 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18649 struct window *w;
18650 int x;
18651 struct glyph_row *row;
18652 enum glyph_row_area area;
18653 int start, end;
18654 enum draw_glyphs_face hl;
18655 int overlaps_p;
18657 struct glyph_string *head, *tail;
18658 struct glyph_string *s;
18659 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18660 int last_x, area_width;
18661 int x_reached;
18662 int i, j;
18663 struct frame *f = XFRAME (WINDOW_FRAME (w));
18664 DECLARE_HDC (hdc);
18666 ALLOCATE_HDC (hdc, f);
18668 /* Let's rather be paranoid than getting a SEGV. */
18669 end = min (end, row->used[area]);
18670 start = max (0, start);
18671 start = min (end, start);
18673 /* Translate X to frame coordinates. Set last_x to the right
18674 end of the drawing area. */
18675 if (row->full_width_p)
18677 /* X is relative to the left edge of W, without scroll bars
18678 or fringes. */
18679 x += WINDOW_LEFT_EDGE_X (w);
18680 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18682 else
18684 int area_left = window_box_left (w, area);
18685 x += area_left;
18686 area_width = window_box_width (w, area);
18687 last_x = area_left + area_width;
18690 /* Build a doubly-linked list of glyph_string structures between
18691 head and tail from what we have to draw. Note that the macro
18692 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18693 the reason we use a separate variable `i'. */
18694 i = start;
18695 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18696 if (tail)
18697 x_reached = tail->x + tail->background_width;
18698 else
18699 x_reached = x;
18701 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18702 the row, redraw some glyphs in front or following the glyph
18703 strings built above. */
18704 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18706 int dummy_x = 0;
18707 struct glyph_string *h, *t;
18709 /* Compute overhangs for all glyph strings. */
18710 if (rif->compute_glyph_string_overhangs)
18711 for (s = head; s; s = s->next)
18712 rif->compute_glyph_string_overhangs (s);
18714 /* Prepend glyph strings for glyphs in front of the first glyph
18715 string that are overwritten because of the first glyph
18716 string's left overhang. The background of all strings
18717 prepended must be drawn because the first glyph string
18718 draws over it. */
18719 i = left_overwritten (head);
18720 if (i >= 0)
18722 j = i;
18723 BUILD_GLYPH_STRINGS (j, start, h, t,
18724 DRAW_NORMAL_TEXT, dummy_x, last_x);
18725 start = i;
18726 compute_overhangs_and_x (t, head->x, 1);
18727 prepend_glyph_string_lists (&head, &tail, h, t);
18728 clip_head = head;
18731 /* Prepend glyph strings for glyphs in front of the first glyph
18732 string that overwrite that glyph string because of their
18733 right overhang. For these strings, only the foreground must
18734 be drawn, because it draws over the glyph string at `head'.
18735 The background must not be drawn because this would overwrite
18736 right overhangs of preceding glyphs for which no glyph
18737 strings exist. */
18738 i = left_overwriting (head);
18739 if (i >= 0)
18741 clip_head = head;
18742 BUILD_GLYPH_STRINGS (i, start, h, t,
18743 DRAW_NORMAL_TEXT, dummy_x, last_x);
18744 for (s = h; s; s = s->next)
18745 s->background_filled_p = 1;
18746 compute_overhangs_and_x (t, head->x, 1);
18747 prepend_glyph_string_lists (&head, &tail, h, t);
18750 /* Append glyphs strings for glyphs following the last glyph
18751 string tail that are overwritten by tail. The background of
18752 these strings has to be drawn because tail's foreground draws
18753 over it. */
18754 i = right_overwritten (tail);
18755 if (i >= 0)
18757 BUILD_GLYPH_STRINGS (end, i, h, t,
18758 DRAW_NORMAL_TEXT, x, last_x);
18759 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18760 append_glyph_string_lists (&head, &tail, h, t);
18761 clip_tail = tail;
18764 /* Append glyph strings for glyphs following the last glyph
18765 string tail that overwrite tail. The foreground of such
18766 glyphs has to be drawn because it writes into the background
18767 of tail. The background must not be drawn because it could
18768 paint over the foreground of following glyphs. */
18769 i = right_overwriting (tail);
18770 if (i >= 0)
18772 clip_tail = tail;
18773 BUILD_GLYPH_STRINGS (end, i, h, t,
18774 DRAW_NORMAL_TEXT, x, last_x);
18775 for (s = h; s; s = s->next)
18776 s->background_filled_p = 1;
18777 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18778 append_glyph_string_lists (&head, &tail, h, t);
18780 if (clip_head || clip_tail)
18781 for (s = head; s; s = s->next)
18783 s->clip_head = clip_head;
18784 s->clip_tail = clip_tail;
18788 /* Draw all strings. */
18789 for (s = head; s; s = s->next)
18790 rif->draw_glyph_string (s);
18792 if (area == TEXT_AREA
18793 && !row->full_width_p
18794 /* When drawing overlapping rows, only the glyph strings'
18795 foreground is drawn, which doesn't erase a cursor
18796 completely. */
18797 && !overlaps_p)
18799 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18800 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18801 : (tail ? tail->x + tail->background_width : x));
18803 int text_left = window_box_left (w, TEXT_AREA);
18804 x0 -= text_left;
18805 x1 -= text_left;
18807 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18808 row->y, MATRIX_ROW_BOTTOM_Y (row));
18811 /* Value is the x-position up to which drawn, relative to AREA of W.
18812 This doesn't include parts drawn because of overhangs. */
18813 if (row->full_width_p)
18814 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18815 else
18816 x_reached -= window_box_left (w, area);
18818 RELEASE_HDC (hdc, f);
18820 return x_reached;
18823 /* Expand row matrix if too narrow. Don't expand if area
18824 is not present. */
18826 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18828 if (!fonts_changed_p \
18829 && (it->glyph_row->glyphs[area] \
18830 < it->glyph_row->glyphs[area + 1])) \
18832 it->w->ncols_scale_factor++; \
18833 fonts_changed_p = 1; \
18837 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18838 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18840 static INLINE void
18841 append_glyph (it)
18842 struct it *it;
18844 struct glyph *glyph;
18845 enum glyph_row_area area = it->area;
18847 xassert (it->glyph_row);
18848 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18850 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18851 if (glyph < it->glyph_row->glyphs[area + 1])
18853 glyph->charpos = CHARPOS (it->position);
18854 glyph->object = it->object;
18855 glyph->pixel_width = it->pixel_width;
18856 glyph->ascent = it->ascent;
18857 glyph->descent = it->descent;
18858 glyph->voffset = it->voffset;
18859 glyph->type = CHAR_GLYPH;
18860 glyph->multibyte_p = it->multibyte_p;
18861 glyph->left_box_line_p = it->start_of_box_run_p;
18862 glyph->right_box_line_p = it->end_of_box_run_p;
18863 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18864 || it->phys_descent > it->descent);
18865 glyph->padding_p = 0;
18866 glyph->glyph_not_available_p = it->glyph_not_available_p;
18867 glyph->face_id = it->face_id;
18868 glyph->u.ch = it->char_to_display;
18869 glyph->slice = null_glyph_slice;
18870 glyph->font_type = FONT_TYPE_UNKNOWN;
18871 ++it->glyph_row->used[area];
18873 else
18874 IT_EXPAND_MATRIX_WIDTH (it, area);
18877 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18878 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18880 static INLINE void
18881 append_composite_glyph (it)
18882 struct it *it;
18884 struct glyph *glyph;
18885 enum glyph_row_area area = it->area;
18887 xassert (it->glyph_row);
18889 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18890 if (glyph < it->glyph_row->glyphs[area + 1])
18892 glyph->charpos = CHARPOS (it->position);
18893 glyph->object = it->object;
18894 glyph->pixel_width = it->pixel_width;
18895 glyph->ascent = it->ascent;
18896 glyph->descent = it->descent;
18897 glyph->voffset = it->voffset;
18898 glyph->type = COMPOSITE_GLYPH;
18899 glyph->multibyte_p = it->multibyte_p;
18900 glyph->left_box_line_p = it->start_of_box_run_p;
18901 glyph->right_box_line_p = it->end_of_box_run_p;
18902 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18903 || it->phys_descent > it->descent);
18904 glyph->padding_p = 0;
18905 glyph->glyph_not_available_p = 0;
18906 glyph->face_id = it->face_id;
18907 glyph->u.cmp_id = it->cmp_id;
18908 glyph->slice = null_glyph_slice;
18909 glyph->font_type = FONT_TYPE_UNKNOWN;
18910 ++it->glyph_row->used[area];
18912 else
18913 IT_EXPAND_MATRIX_WIDTH (it, area);
18917 /* Change IT->ascent and IT->height according to the setting of
18918 IT->voffset. */
18920 static INLINE void
18921 take_vertical_position_into_account (it)
18922 struct it *it;
18924 if (it->voffset)
18926 if (it->voffset < 0)
18927 /* Increase the ascent so that we can display the text higher
18928 in the line. */
18929 it->ascent -= it->voffset;
18930 else
18931 /* Increase the descent so that we can display the text lower
18932 in the line. */
18933 it->descent += it->voffset;
18938 /* Produce glyphs/get display metrics for the image IT is loaded with.
18939 See the description of struct display_iterator in dispextern.h for
18940 an overview of struct display_iterator. */
18942 static void
18943 produce_image_glyph (it)
18944 struct it *it;
18946 struct image *img;
18947 struct face *face;
18948 int glyph_ascent;
18949 struct glyph_slice slice;
18951 xassert (it->what == IT_IMAGE);
18953 face = FACE_FROM_ID (it->f, it->face_id);
18954 xassert (face);
18955 /* Make sure X resources of the face is loaded. */
18956 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18958 if (it->image_id < 0)
18960 /* Fringe bitmap. */
18961 it->ascent = it->phys_ascent = 0;
18962 it->descent = it->phys_descent = 0;
18963 it->pixel_width = 0;
18964 it->nglyphs = 0;
18965 return;
18968 img = IMAGE_FROM_ID (it->f, it->image_id);
18969 xassert (img);
18970 /* Make sure X resources of the image is loaded. */
18971 prepare_image_for_display (it->f, img);
18973 slice.x = slice.y = 0;
18974 slice.width = img->width;
18975 slice.height = img->height;
18977 if (INTEGERP (it->slice.x))
18978 slice.x = XINT (it->slice.x);
18979 else if (FLOATP (it->slice.x))
18980 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18982 if (INTEGERP (it->slice.y))
18983 slice.y = XINT (it->slice.y);
18984 else if (FLOATP (it->slice.y))
18985 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18987 if (INTEGERP (it->slice.width))
18988 slice.width = XINT (it->slice.width);
18989 else if (FLOATP (it->slice.width))
18990 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18992 if (INTEGERP (it->slice.height))
18993 slice.height = XINT (it->slice.height);
18994 else if (FLOATP (it->slice.height))
18995 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18997 if (slice.x >= img->width)
18998 slice.x = img->width;
18999 if (slice.y >= img->height)
19000 slice.y = img->height;
19001 if (slice.x + slice.width >= img->width)
19002 slice.width = img->width - slice.x;
19003 if (slice.y + slice.height > img->height)
19004 slice.height = img->height - slice.y;
19006 if (slice.width == 0 || slice.height == 0)
19007 return;
19009 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19011 it->descent = slice.height - glyph_ascent;
19012 if (slice.y == 0)
19013 it->descent += img->vmargin;
19014 if (slice.y + slice.height == img->height)
19015 it->descent += img->vmargin;
19016 it->phys_descent = it->descent;
19018 it->pixel_width = slice.width;
19019 if (slice.x == 0)
19020 it->pixel_width += img->hmargin;
19021 if (slice.x + slice.width == img->width)
19022 it->pixel_width += img->hmargin;
19024 /* It's quite possible for images to have an ascent greater than
19025 their height, so don't get confused in that case. */
19026 if (it->descent < 0)
19027 it->descent = 0;
19029 #if 0 /* this breaks image tiling */
19030 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19031 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19032 if (face_ascent > it->ascent)
19033 it->ascent = it->phys_ascent = face_ascent;
19034 #endif
19036 it->nglyphs = 1;
19038 if (face->box != FACE_NO_BOX)
19040 if (face->box_line_width > 0)
19042 if (slice.y == 0)
19043 it->ascent += face->box_line_width;
19044 if (slice.y + slice.height == img->height)
19045 it->descent += face->box_line_width;
19048 if (it->start_of_box_run_p && slice.x == 0)
19049 it->pixel_width += abs (face->box_line_width);
19050 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19051 it->pixel_width += abs (face->box_line_width);
19054 take_vertical_position_into_account (it);
19056 if (it->glyph_row)
19058 struct glyph *glyph;
19059 enum glyph_row_area area = it->area;
19061 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19062 if (glyph < it->glyph_row->glyphs[area + 1])
19064 glyph->charpos = CHARPOS (it->position);
19065 glyph->object = it->object;
19066 glyph->pixel_width = it->pixel_width;
19067 glyph->ascent = glyph_ascent;
19068 glyph->descent = it->descent;
19069 glyph->voffset = it->voffset;
19070 glyph->type = IMAGE_GLYPH;
19071 glyph->multibyte_p = it->multibyte_p;
19072 glyph->left_box_line_p = it->start_of_box_run_p;
19073 glyph->right_box_line_p = it->end_of_box_run_p;
19074 glyph->overlaps_vertically_p = 0;
19075 glyph->padding_p = 0;
19076 glyph->glyph_not_available_p = 0;
19077 glyph->face_id = it->face_id;
19078 glyph->u.img_id = img->id;
19079 glyph->slice = slice;
19080 glyph->font_type = FONT_TYPE_UNKNOWN;
19081 ++it->glyph_row->used[area];
19083 else
19084 IT_EXPAND_MATRIX_WIDTH (it, area);
19089 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19090 of the glyph, WIDTH and HEIGHT are the width and height of the
19091 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19093 static void
19094 append_stretch_glyph (it, object, width, height, ascent)
19095 struct it *it;
19096 Lisp_Object object;
19097 int width, height;
19098 int ascent;
19100 struct glyph *glyph;
19101 enum glyph_row_area area = it->area;
19103 xassert (ascent >= 0 && ascent <= height);
19105 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19106 if (glyph < it->glyph_row->glyphs[area + 1])
19108 glyph->charpos = CHARPOS (it->position);
19109 glyph->object = object;
19110 glyph->pixel_width = width;
19111 glyph->ascent = ascent;
19112 glyph->descent = height - ascent;
19113 glyph->voffset = it->voffset;
19114 glyph->type = STRETCH_GLYPH;
19115 glyph->multibyte_p = it->multibyte_p;
19116 glyph->left_box_line_p = it->start_of_box_run_p;
19117 glyph->right_box_line_p = it->end_of_box_run_p;
19118 glyph->overlaps_vertically_p = 0;
19119 glyph->padding_p = 0;
19120 glyph->glyph_not_available_p = 0;
19121 glyph->face_id = it->face_id;
19122 glyph->u.stretch.ascent = ascent;
19123 glyph->u.stretch.height = height;
19124 glyph->slice = null_glyph_slice;
19125 glyph->font_type = FONT_TYPE_UNKNOWN;
19126 ++it->glyph_row->used[area];
19128 else
19129 IT_EXPAND_MATRIX_WIDTH (it, area);
19133 /* Produce a stretch glyph for iterator IT. IT->object is the value
19134 of the glyph property displayed. The value must be a list
19135 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19136 being recognized:
19138 1. `:width WIDTH' specifies that the space should be WIDTH *
19139 canonical char width wide. WIDTH may be an integer or floating
19140 point number.
19142 2. `:relative-width FACTOR' specifies that the width of the stretch
19143 should be computed from the width of the first character having the
19144 `glyph' property, and should be FACTOR times that width.
19146 3. `:align-to HPOS' specifies that the space should be wide enough
19147 to reach HPOS, a value in canonical character units.
19149 Exactly one of the above pairs must be present.
19151 4. `:height HEIGHT' specifies that the height of the stretch produced
19152 should be HEIGHT, measured in canonical character units.
19154 5. `:relative-height FACTOR' specifies that the height of the
19155 stretch should be FACTOR times the height of the characters having
19156 the glyph property.
19158 Either none or exactly one of 4 or 5 must be present.
19160 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19161 of the stretch should be used for the ascent of the stretch.
19162 ASCENT must be in the range 0 <= ASCENT <= 100. */
19164 static void
19165 produce_stretch_glyph (it)
19166 struct it *it;
19168 /* (space :width WIDTH :height HEIGHT ...) */
19169 Lisp_Object prop, plist;
19170 int width = 0, height = 0, align_to = -1;
19171 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19172 int ascent = 0;
19173 double tem;
19174 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19175 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19177 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19179 /* List should start with `space'. */
19180 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19181 plist = XCDR (it->object);
19183 /* Compute the width of the stretch. */
19184 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19185 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19187 /* Absolute width `:width WIDTH' specified and valid. */
19188 zero_width_ok_p = 1;
19189 width = (int)tem;
19191 else if (prop = Fplist_get (plist, QCrelative_width),
19192 NUMVAL (prop) > 0)
19194 /* Relative width `:relative-width FACTOR' specified and valid.
19195 Compute the width of the characters having the `glyph'
19196 property. */
19197 struct it it2;
19198 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19200 it2 = *it;
19201 if (it->multibyte_p)
19203 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19204 - IT_BYTEPOS (*it));
19205 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19207 else
19208 it2.c = *p, it2.len = 1;
19210 it2.glyph_row = NULL;
19211 it2.what = IT_CHARACTER;
19212 x_produce_glyphs (&it2);
19213 width = NUMVAL (prop) * it2.pixel_width;
19215 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19216 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19218 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19219 align_to = (align_to < 0
19221 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19222 else if (align_to < 0)
19223 align_to = window_box_left_offset (it->w, TEXT_AREA);
19224 width = max (0, (int)tem + align_to - it->current_x);
19225 zero_width_ok_p = 1;
19227 else
19228 /* Nothing specified -> width defaults to canonical char width. */
19229 width = FRAME_COLUMN_WIDTH (it->f);
19231 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19232 width = 1;
19234 /* Compute height. */
19235 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19236 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19238 height = (int)tem;
19239 zero_height_ok_p = 1;
19241 else if (prop = Fplist_get (plist, QCrelative_height),
19242 NUMVAL (prop) > 0)
19243 height = FONT_HEIGHT (font) * NUMVAL (prop);
19244 else
19245 height = FONT_HEIGHT (font);
19247 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19248 height = 1;
19250 /* Compute percentage of height used for ascent. If
19251 `:ascent ASCENT' is present and valid, use that. Otherwise,
19252 derive the ascent from the font in use. */
19253 if (prop = Fplist_get (plist, QCascent),
19254 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19255 ascent = height * NUMVAL (prop) / 100.0;
19256 else if (!NILP (prop)
19257 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19258 ascent = min (max (0, (int)tem), height);
19259 else
19260 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19262 if (width > 0 && height > 0 && it->glyph_row)
19264 Lisp_Object object = it->stack[it->sp - 1].string;
19265 if (!STRINGP (object))
19266 object = it->w->buffer;
19267 append_stretch_glyph (it, object, width, height, ascent);
19270 it->pixel_width = width;
19271 it->ascent = it->phys_ascent = ascent;
19272 it->descent = it->phys_descent = height - it->ascent;
19273 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19275 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19277 if (face->box_line_width > 0)
19279 it->ascent += face->box_line_width;
19280 it->descent += face->box_line_width;
19283 if (it->start_of_box_run_p)
19284 it->pixel_width += abs (face->box_line_width);
19285 if (it->end_of_box_run_p)
19286 it->pixel_width += abs (face->box_line_width);
19289 take_vertical_position_into_account (it);
19292 /* Get line-height and line-spacing property at point.
19293 If line-height has format (HEIGHT TOTAL), return TOTAL
19294 in TOTAL_HEIGHT. */
19296 static Lisp_Object
19297 get_line_height_property (it, prop)
19298 struct it *it;
19299 Lisp_Object prop;
19301 Lisp_Object position;
19303 if (STRINGP (it->object))
19304 position = make_number (IT_STRING_CHARPOS (*it));
19305 else if (BUFFERP (it->object))
19306 position = make_number (IT_CHARPOS (*it));
19307 else
19308 return Qnil;
19310 return Fget_char_property (position, prop, it->object);
19313 /* Calculate line-height and line-spacing properties.
19314 An integer value specifies explicit pixel value.
19315 A float value specifies relative value to current face height.
19316 A cons (float . face-name) specifies relative value to
19317 height of specified face font.
19319 Returns height in pixels, or nil. */
19322 static Lisp_Object
19323 calc_line_height_property (it, val, font, boff, override)
19324 struct it *it;
19325 Lisp_Object val;
19326 XFontStruct *font;
19327 int boff, override;
19329 Lisp_Object face_name = Qnil;
19330 int ascent, descent, height;
19332 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19333 return val;
19335 if (CONSP (val))
19337 face_name = XCAR (val);
19338 val = XCDR (val);
19339 if (!NUMBERP (val))
19340 val = make_number (1);
19341 if (NILP (face_name))
19343 height = it->ascent + it->descent;
19344 goto scale;
19348 if (NILP (face_name))
19350 font = FRAME_FONT (it->f);
19351 boff = FRAME_BASELINE_OFFSET (it->f);
19353 else if (EQ (face_name, Qt))
19355 override = 0;
19357 else
19359 int face_id;
19360 struct face *face;
19361 struct font_info *font_info;
19363 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19364 if (face_id < 0)
19365 return make_number (-1);
19367 face = FACE_FROM_ID (it->f, face_id);
19368 font = face->font;
19369 if (font == NULL)
19370 return make_number (-1);
19372 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19373 boff = font_info->baseline_offset;
19374 if (font_info->vertical_centering)
19375 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19378 ascent = FONT_BASE (font) + boff;
19379 descent = FONT_DESCENT (font) - boff;
19381 if (override)
19383 it->override_ascent = ascent;
19384 it->override_descent = descent;
19385 it->override_boff = boff;
19388 height = ascent + descent;
19390 scale:
19391 if (FLOATP (val))
19392 height = (int)(XFLOAT_DATA (val) * height);
19393 else if (INTEGERP (val))
19394 height *= XINT (val);
19396 return make_number (height);
19400 /* RIF:
19401 Produce glyphs/get display metrics for the display element IT is
19402 loaded with. See the description of struct display_iterator in
19403 dispextern.h for an overview of struct display_iterator. */
19405 void
19406 x_produce_glyphs (it)
19407 struct it *it;
19409 int extra_line_spacing = it->extra_line_spacing;
19411 it->glyph_not_available_p = 0;
19413 if (it->what == IT_CHARACTER)
19415 XChar2b char2b;
19416 XFontStruct *font;
19417 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19418 XCharStruct *pcm;
19419 int font_not_found_p;
19420 struct font_info *font_info;
19421 int boff; /* baseline offset */
19422 /* We may change it->multibyte_p upon unibyte<->multibyte
19423 conversion. So, save the current value now and restore it
19424 later.
19426 Note: It seems that we don't have to record multibyte_p in
19427 struct glyph because the character code itself tells if or
19428 not the character is multibyte. Thus, in the future, we must
19429 consider eliminating the field `multibyte_p' in the struct
19430 glyph. */
19431 int saved_multibyte_p = it->multibyte_p;
19433 /* Maybe translate single-byte characters to multibyte, or the
19434 other way. */
19435 it->char_to_display = it->c;
19436 if (!ASCII_BYTE_P (it->c))
19438 if (unibyte_display_via_language_environment
19439 && SINGLE_BYTE_CHAR_P (it->c)
19440 && (it->c >= 0240
19441 || !NILP (Vnonascii_translation_table)))
19443 it->char_to_display = unibyte_char_to_multibyte (it->c);
19444 it->multibyte_p = 1;
19445 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19446 face = FACE_FROM_ID (it->f, it->face_id);
19448 else if (!SINGLE_BYTE_CHAR_P (it->c)
19449 && !it->multibyte_p)
19451 it->multibyte_p = 1;
19452 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19453 face = FACE_FROM_ID (it->f, it->face_id);
19457 /* Get font to use. Encode IT->char_to_display. */
19458 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19459 &char2b, it->multibyte_p, 0);
19460 font = face->font;
19462 /* When no suitable font found, use the default font. */
19463 font_not_found_p = font == NULL;
19464 if (font_not_found_p)
19466 font = FRAME_FONT (it->f);
19467 boff = FRAME_BASELINE_OFFSET (it->f);
19468 font_info = NULL;
19470 else
19472 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19473 boff = font_info->baseline_offset;
19474 if (font_info->vertical_centering)
19475 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19478 if (it->char_to_display >= ' '
19479 && (!it->multibyte_p || it->char_to_display < 128))
19481 /* Either unibyte or ASCII. */
19482 int stretched_p;
19484 it->nglyphs = 1;
19486 pcm = rif->per_char_metric (font, &char2b,
19487 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19489 if (it->override_ascent >= 0)
19491 it->ascent = it->override_ascent;
19492 it->descent = it->override_descent;
19493 boff = it->override_boff;
19495 else
19497 it->ascent = FONT_BASE (font) + boff;
19498 it->descent = FONT_DESCENT (font) - boff;
19501 if (pcm)
19503 it->phys_ascent = pcm->ascent + boff;
19504 it->phys_descent = pcm->descent - boff;
19505 it->pixel_width = pcm->width;
19507 else
19509 it->glyph_not_available_p = 1;
19510 it->phys_ascent = it->ascent;
19511 it->phys_descent = it->descent;
19512 it->pixel_width = FONT_WIDTH (font);
19515 if (it->constrain_row_ascent_descent_p)
19517 if (it->descent > it->max_descent)
19519 it->ascent += it->descent - it->max_descent;
19520 it->descent = it->max_descent;
19522 if (it->ascent > it->max_ascent)
19524 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19525 it->ascent = it->max_ascent;
19527 it->phys_ascent = min (it->phys_ascent, it->ascent);
19528 it->phys_descent = min (it->phys_descent, it->descent);
19529 extra_line_spacing = 0;
19532 /* If this is a space inside a region of text with
19533 `space-width' property, change its width. */
19534 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19535 if (stretched_p)
19536 it->pixel_width *= XFLOATINT (it->space_width);
19538 /* If face has a box, add the box thickness to the character
19539 height. If character has a box line to the left and/or
19540 right, add the box line width to the character's width. */
19541 if (face->box != FACE_NO_BOX)
19543 int thick = face->box_line_width;
19545 if (thick > 0)
19547 it->ascent += thick;
19548 it->descent += thick;
19550 else
19551 thick = -thick;
19553 if (it->start_of_box_run_p)
19554 it->pixel_width += thick;
19555 if (it->end_of_box_run_p)
19556 it->pixel_width += thick;
19559 /* If face has an overline, add the height of the overline
19560 (1 pixel) and a 1 pixel margin to the character height. */
19561 if (face->overline_p)
19562 it->ascent += 2;
19564 if (it->constrain_row_ascent_descent_p)
19566 if (it->ascent > it->max_ascent)
19567 it->ascent = it->max_ascent;
19568 if (it->descent > it->max_descent)
19569 it->descent = it->max_descent;
19572 take_vertical_position_into_account (it);
19574 /* If we have to actually produce glyphs, do it. */
19575 if (it->glyph_row)
19577 if (stretched_p)
19579 /* Translate a space with a `space-width' property
19580 into a stretch glyph. */
19581 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19582 / FONT_HEIGHT (font));
19583 append_stretch_glyph (it, it->object, it->pixel_width,
19584 it->ascent + it->descent, ascent);
19586 else
19587 append_glyph (it);
19589 /* If characters with lbearing or rbearing are displayed
19590 in this line, record that fact in a flag of the
19591 glyph row. This is used to optimize X output code. */
19592 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19593 it->glyph_row->contains_overlapping_glyphs_p = 1;
19596 else if (it->char_to_display == '\n')
19598 /* A newline has no width but we need the height of the line.
19599 But if previous part of the line set a height, don't
19600 increase that height */
19602 Lisp_Object height;
19603 Lisp_Object total_height = Qnil;
19605 it->override_ascent = -1;
19606 it->pixel_width = 0;
19607 it->nglyphs = 0;
19609 height = get_line_height_property(it, Qline_height);
19610 /* Split (line-height total-height) list */
19611 if (CONSP (height)
19612 && CONSP (XCDR (height))
19613 && NILP (XCDR (XCDR (height))))
19615 total_height = XCAR (XCDR (height));
19616 height = XCAR (height);
19618 height = calc_line_height_property(it, height, font, boff, 1);
19620 if (it->override_ascent >= 0)
19622 it->ascent = it->override_ascent;
19623 it->descent = it->override_descent;
19624 boff = it->override_boff;
19626 else
19628 it->ascent = FONT_BASE (font) + boff;
19629 it->descent = FONT_DESCENT (font) - boff;
19632 if (EQ (height, Qt))
19634 if (it->descent > it->max_descent)
19636 it->ascent += it->descent - it->max_descent;
19637 it->descent = it->max_descent;
19639 if (it->ascent > it->max_ascent)
19641 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19642 it->ascent = it->max_ascent;
19644 it->phys_ascent = min (it->phys_ascent, it->ascent);
19645 it->phys_descent = min (it->phys_descent, it->descent);
19646 it->constrain_row_ascent_descent_p = 1;
19647 extra_line_spacing = 0;
19649 else
19651 Lisp_Object spacing;
19653 it->phys_ascent = it->ascent;
19654 it->phys_descent = it->descent;
19656 if ((it->max_ascent > 0 || it->max_descent > 0)
19657 && face->box != FACE_NO_BOX
19658 && face->box_line_width > 0)
19660 it->ascent += face->box_line_width;
19661 it->descent += face->box_line_width;
19663 if (!NILP (height)
19664 && XINT (height) > it->ascent + it->descent)
19665 it->ascent = XINT (height) - it->descent;
19667 if (!NILP (total_height))
19668 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19669 else
19671 spacing = get_line_height_property(it, Qline_spacing);
19672 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19674 if (INTEGERP (spacing))
19676 extra_line_spacing = XINT (spacing);
19677 if (!NILP (total_height))
19678 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19682 else if (it->char_to_display == '\t')
19684 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19685 int x = it->current_x + it->continuation_lines_width;
19686 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19688 /* If the distance from the current position to the next tab
19689 stop is less than a space character width, use the
19690 tab stop after that. */
19691 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19692 next_tab_x += tab_width;
19694 it->pixel_width = next_tab_x - x;
19695 it->nglyphs = 1;
19696 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19697 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19699 if (it->glyph_row)
19701 append_stretch_glyph (it, it->object, it->pixel_width,
19702 it->ascent + it->descent, it->ascent);
19705 else
19707 /* A multi-byte character. Assume that the display width of the
19708 character is the width of the character multiplied by the
19709 width of the font. */
19711 /* If we found a font, this font should give us the right
19712 metrics. If we didn't find a font, use the frame's
19713 default font and calculate the width of the character
19714 from the charset width; this is what old redisplay code
19715 did. */
19717 pcm = rif->per_char_metric (font, &char2b,
19718 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19720 if (font_not_found_p || !pcm)
19722 int charset = CHAR_CHARSET (it->char_to_display);
19724 it->glyph_not_available_p = 1;
19725 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19726 * CHARSET_WIDTH (charset));
19727 it->phys_ascent = FONT_BASE (font) + boff;
19728 it->phys_descent = FONT_DESCENT (font) - boff;
19730 else
19732 it->pixel_width = pcm->width;
19733 it->phys_ascent = pcm->ascent + boff;
19734 it->phys_descent = pcm->descent - boff;
19735 if (it->glyph_row
19736 && (pcm->lbearing < 0
19737 || pcm->rbearing > pcm->width))
19738 it->glyph_row->contains_overlapping_glyphs_p = 1;
19740 it->nglyphs = 1;
19741 it->ascent = FONT_BASE (font) + boff;
19742 it->descent = FONT_DESCENT (font) - boff;
19743 if (face->box != FACE_NO_BOX)
19745 int thick = face->box_line_width;
19747 if (thick > 0)
19749 it->ascent += thick;
19750 it->descent += thick;
19752 else
19753 thick = - thick;
19755 if (it->start_of_box_run_p)
19756 it->pixel_width += thick;
19757 if (it->end_of_box_run_p)
19758 it->pixel_width += thick;
19761 /* If face has an overline, add the height of the overline
19762 (1 pixel) and a 1 pixel margin to the character height. */
19763 if (face->overline_p)
19764 it->ascent += 2;
19766 take_vertical_position_into_account (it);
19768 if (it->glyph_row)
19769 append_glyph (it);
19771 it->multibyte_p = saved_multibyte_p;
19773 else if (it->what == IT_COMPOSITION)
19775 /* Note: A composition is represented as one glyph in the
19776 glyph matrix. There are no padding glyphs. */
19777 XChar2b char2b;
19778 XFontStruct *font;
19779 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19780 XCharStruct *pcm;
19781 int font_not_found_p;
19782 struct font_info *font_info;
19783 int boff; /* baseline offset */
19784 struct composition *cmp = composition_table[it->cmp_id];
19786 /* Maybe translate single-byte characters to multibyte. */
19787 it->char_to_display = it->c;
19788 if (unibyte_display_via_language_environment
19789 && SINGLE_BYTE_CHAR_P (it->c)
19790 && (it->c >= 0240
19791 || (it->c >= 0200
19792 && !NILP (Vnonascii_translation_table))))
19794 it->char_to_display = unibyte_char_to_multibyte (it->c);
19797 /* Get face and font to use. Encode IT->char_to_display. */
19798 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19799 face = FACE_FROM_ID (it->f, it->face_id);
19800 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19801 &char2b, it->multibyte_p, 0);
19802 font = face->font;
19804 /* When no suitable font found, use the default font. */
19805 font_not_found_p = font == NULL;
19806 if (font_not_found_p)
19808 font = FRAME_FONT (it->f);
19809 boff = FRAME_BASELINE_OFFSET (it->f);
19810 font_info = NULL;
19812 else
19814 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19815 boff = font_info->baseline_offset;
19816 if (font_info->vertical_centering)
19817 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19820 /* There are no padding glyphs, so there is only one glyph to
19821 produce for the composition. Important is that pixel_width,
19822 ascent and descent are the values of what is drawn by
19823 draw_glyphs (i.e. the values of the overall glyphs composed). */
19824 it->nglyphs = 1;
19826 /* If we have not yet calculated pixel size data of glyphs of
19827 the composition for the current face font, calculate them
19828 now. Theoretically, we have to check all fonts for the
19829 glyphs, but that requires much time and memory space. So,
19830 here we check only the font of the first glyph. This leads
19831 to incorrect display very rarely, and C-l (recenter) can
19832 correct the display anyway. */
19833 if (cmp->font != (void *) font)
19835 /* Ascent and descent of the font of the first character of
19836 this composition (adjusted by baseline offset). Ascent
19837 and descent of overall glyphs should not be less than
19838 them respectively. */
19839 int font_ascent = FONT_BASE (font) + boff;
19840 int font_descent = FONT_DESCENT (font) - boff;
19841 /* Bounding box of the overall glyphs. */
19842 int leftmost, rightmost, lowest, highest;
19843 int i, width, ascent, descent;
19845 cmp->font = (void *) font;
19847 /* Initialize the bounding box. */
19848 if (font_info
19849 && (pcm = rif->per_char_metric (font, &char2b,
19850 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19852 width = pcm->width;
19853 ascent = pcm->ascent;
19854 descent = pcm->descent;
19856 else
19858 width = FONT_WIDTH (font);
19859 ascent = FONT_BASE (font);
19860 descent = FONT_DESCENT (font);
19863 rightmost = width;
19864 lowest = - descent + boff;
19865 highest = ascent + boff;
19866 leftmost = 0;
19868 if (font_info
19869 && font_info->default_ascent
19870 && CHAR_TABLE_P (Vuse_default_ascent)
19871 && !NILP (Faref (Vuse_default_ascent,
19872 make_number (it->char_to_display))))
19873 highest = font_info->default_ascent + boff;
19875 /* Draw the first glyph at the normal position. It may be
19876 shifted to right later if some other glyphs are drawn at
19877 the left. */
19878 cmp->offsets[0] = 0;
19879 cmp->offsets[1] = boff;
19881 /* Set cmp->offsets for the remaining glyphs. */
19882 for (i = 1; i < cmp->glyph_len; i++)
19884 int left, right, btm, top;
19885 int ch = COMPOSITION_GLYPH (cmp, i);
19886 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19888 face = FACE_FROM_ID (it->f, face_id);
19889 get_char_face_and_encoding (it->f, ch, face->id,
19890 &char2b, it->multibyte_p, 0);
19891 font = face->font;
19892 if (font == NULL)
19894 font = FRAME_FONT (it->f);
19895 boff = FRAME_BASELINE_OFFSET (it->f);
19896 font_info = NULL;
19898 else
19900 font_info
19901 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19902 boff = font_info->baseline_offset;
19903 if (font_info->vertical_centering)
19904 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19907 if (font_info
19908 && (pcm = rif->per_char_metric (font, &char2b,
19909 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19911 width = pcm->width;
19912 ascent = pcm->ascent;
19913 descent = pcm->descent;
19915 else
19917 width = FONT_WIDTH (font);
19918 ascent = 1;
19919 descent = 0;
19922 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19924 /* Relative composition with or without
19925 alternate chars. */
19926 left = (leftmost + rightmost - width) / 2;
19927 btm = - descent + boff;
19928 if (font_info && font_info->relative_compose
19929 && (! CHAR_TABLE_P (Vignore_relative_composition)
19930 || NILP (Faref (Vignore_relative_composition,
19931 make_number (ch)))))
19934 if (- descent >= font_info->relative_compose)
19935 /* One extra pixel between two glyphs. */
19936 btm = highest + 1;
19937 else if (ascent <= 0)
19938 /* One extra pixel between two glyphs. */
19939 btm = lowest - 1 - ascent - descent;
19942 else
19944 /* A composition rule is specified by an integer
19945 value that encodes global and new reference
19946 points (GREF and NREF). GREF and NREF are
19947 specified by numbers as below:
19949 0---1---2 -- ascent
19953 9--10--11 -- center
19955 ---3---4---5--- baseline
19957 6---7---8 -- descent
19959 int rule = COMPOSITION_RULE (cmp, i);
19960 int gref, nref, grefx, grefy, nrefx, nrefy;
19962 COMPOSITION_DECODE_RULE (rule, gref, nref);
19963 grefx = gref % 3, nrefx = nref % 3;
19964 grefy = gref / 3, nrefy = nref / 3;
19966 left = (leftmost
19967 + grefx * (rightmost - leftmost) / 2
19968 - nrefx * width / 2);
19969 btm = ((grefy == 0 ? highest
19970 : grefy == 1 ? 0
19971 : grefy == 2 ? lowest
19972 : (highest + lowest) / 2)
19973 - (nrefy == 0 ? ascent + descent
19974 : nrefy == 1 ? descent - boff
19975 : nrefy == 2 ? 0
19976 : (ascent + descent) / 2));
19979 cmp->offsets[i * 2] = left;
19980 cmp->offsets[i * 2 + 1] = btm + descent;
19982 /* Update the bounding box of the overall glyphs. */
19983 right = left + width;
19984 top = btm + descent + ascent;
19985 if (left < leftmost)
19986 leftmost = left;
19987 if (right > rightmost)
19988 rightmost = right;
19989 if (top > highest)
19990 highest = top;
19991 if (btm < lowest)
19992 lowest = btm;
19995 /* If there are glyphs whose x-offsets are negative,
19996 shift all glyphs to the right and make all x-offsets
19997 non-negative. */
19998 if (leftmost < 0)
20000 for (i = 0; i < cmp->glyph_len; i++)
20001 cmp->offsets[i * 2] -= leftmost;
20002 rightmost -= leftmost;
20005 cmp->pixel_width = rightmost;
20006 cmp->ascent = highest;
20007 cmp->descent = - lowest;
20008 if (cmp->ascent < font_ascent)
20009 cmp->ascent = font_ascent;
20010 if (cmp->descent < font_descent)
20011 cmp->descent = font_descent;
20014 it->pixel_width = cmp->pixel_width;
20015 it->ascent = it->phys_ascent = cmp->ascent;
20016 it->descent = it->phys_descent = cmp->descent;
20018 if (face->box != FACE_NO_BOX)
20020 int thick = face->box_line_width;
20022 if (thick > 0)
20024 it->ascent += thick;
20025 it->descent += thick;
20027 else
20028 thick = - thick;
20030 if (it->start_of_box_run_p)
20031 it->pixel_width += thick;
20032 if (it->end_of_box_run_p)
20033 it->pixel_width += thick;
20036 /* If face has an overline, add the height of the overline
20037 (1 pixel) and a 1 pixel margin to the character height. */
20038 if (face->overline_p)
20039 it->ascent += 2;
20041 take_vertical_position_into_account (it);
20043 if (it->glyph_row)
20044 append_composite_glyph (it);
20046 else if (it->what == IT_IMAGE)
20047 produce_image_glyph (it);
20048 else if (it->what == IT_STRETCH)
20049 produce_stretch_glyph (it);
20051 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20052 because this isn't true for images with `:ascent 100'. */
20053 xassert (it->ascent >= 0 && it->descent >= 0);
20054 if (it->area == TEXT_AREA)
20055 it->current_x += it->pixel_width;
20057 if (extra_line_spacing > 0)
20059 it->descent += extra_line_spacing;
20060 if (extra_line_spacing > it->max_extra_line_spacing)
20061 it->max_extra_line_spacing = extra_line_spacing;
20064 it->max_ascent = max (it->max_ascent, it->ascent);
20065 it->max_descent = max (it->max_descent, it->descent);
20066 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20067 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20070 /* EXPORT for RIF:
20071 Output LEN glyphs starting at START at the nominal cursor position.
20072 Advance the nominal cursor over the text. The global variable
20073 updated_window contains the window being updated, updated_row is
20074 the glyph row being updated, and updated_area is the area of that
20075 row being updated. */
20077 void
20078 x_write_glyphs (start, len)
20079 struct glyph *start;
20080 int len;
20082 int x, hpos;
20084 xassert (updated_window && updated_row);
20085 BLOCK_INPUT;
20087 /* Write glyphs. */
20089 hpos = start - updated_row->glyphs[updated_area];
20090 x = draw_glyphs (updated_window, output_cursor.x,
20091 updated_row, updated_area,
20092 hpos, hpos + len,
20093 DRAW_NORMAL_TEXT, 0);
20095 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20096 if (updated_area == TEXT_AREA
20097 && updated_window->phys_cursor_on_p
20098 && updated_window->phys_cursor.vpos == output_cursor.vpos
20099 && updated_window->phys_cursor.hpos >= hpos
20100 && updated_window->phys_cursor.hpos < hpos + len)
20101 updated_window->phys_cursor_on_p = 0;
20103 UNBLOCK_INPUT;
20105 /* Advance the output cursor. */
20106 output_cursor.hpos += len;
20107 output_cursor.x = x;
20111 /* EXPORT for RIF:
20112 Insert LEN glyphs from START at the nominal cursor position. */
20114 void
20115 x_insert_glyphs (start, len)
20116 struct glyph *start;
20117 int len;
20119 struct frame *f;
20120 struct window *w;
20121 int line_height, shift_by_width, shifted_region_width;
20122 struct glyph_row *row;
20123 struct glyph *glyph;
20124 int frame_x, frame_y, hpos;
20126 xassert (updated_window && updated_row);
20127 BLOCK_INPUT;
20128 w = updated_window;
20129 f = XFRAME (WINDOW_FRAME (w));
20131 /* Get the height of the line we are in. */
20132 row = updated_row;
20133 line_height = row->height;
20135 /* Get the width of the glyphs to insert. */
20136 shift_by_width = 0;
20137 for (glyph = start; glyph < start + len; ++glyph)
20138 shift_by_width += glyph->pixel_width;
20140 /* Get the width of the region to shift right. */
20141 shifted_region_width = (window_box_width (w, updated_area)
20142 - output_cursor.x
20143 - shift_by_width);
20145 /* Shift right. */
20146 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20147 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20149 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20150 line_height, shift_by_width);
20152 /* Write the glyphs. */
20153 hpos = start - row->glyphs[updated_area];
20154 draw_glyphs (w, output_cursor.x, row, updated_area,
20155 hpos, hpos + len,
20156 DRAW_NORMAL_TEXT, 0);
20158 /* Advance the output cursor. */
20159 output_cursor.hpos += len;
20160 output_cursor.x += shift_by_width;
20161 UNBLOCK_INPUT;
20165 /* EXPORT for RIF:
20166 Erase the current text line from the nominal cursor position
20167 (inclusive) to pixel column TO_X (exclusive). The idea is that
20168 everything from TO_X onward is already erased.
20170 TO_X is a pixel position relative to updated_area of
20171 updated_window. TO_X == -1 means clear to the end of this area. */
20173 void
20174 x_clear_end_of_line (to_x)
20175 int to_x;
20177 struct frame *f;
20178 struct window *w = updated_window;
20179 int max_x, min_y, max_y;
20180 int from_x, from_y, to_y;
20182 xassert (updated_window && updated_row);
20183 f = XFRAME (w->frame);
20185 if (updated_row->full_width_p)
20186 max_x = WINDOW_TOTAL_WIDTH (w);
20187 else
20188 max_x = window_box_width (w, updated_area);
20189 max_y = window_text_bottom_y (w);
20191 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20192 of window. For TO_X > 0, truncate to end of drawing area. */
20193 if (to_x == 0)
20194 return;
20195 else if (to_x < 0)
20196 to_x = max_x;
20197 else
20198 to_x = min (to_x, max_x);
20200 to_y = min (max_y, output_cursor.y + updated_row->height);
20202 /* Notice if the cursor will be cleared by this operation. */
20203 if (!updated_row->full_width_p)
20204 notice_overwritten_cursor (w, updated_area,
20205 output_cursor.x, -1,
20206 updated_row->y,
20207 MATRIX_ROW_BOTTOM_Y (updated_row));
20209 from_x = output_cursor.x;
20211 /* Translate to frame coordinates. */
20212 if (updated_row->full_width_p)
20214 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20215 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20217 else
20219 int area_left = window_box_left (w, updated_area);
20220 from_x += area_left;
20221 to_x += area_left;
20224 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20225 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20226 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20228 /* Prevent inadvertently clearing to end of the X window. */
20229 if (to_x > from_x && to_y > from_y)
20231 BLOCK_INPUT;
20232 rif->clear_frame_area (f, from_x, from_y,
20233 to_x - from_x, to_y - from_y);
20234 UNBLOCK_INPUT;
20238 #endif /* HAVE_WINDOW_SYSTEM */
20242 /***********************************************************************
20243 Cursor types
20244 ***********************************************************************/
20246 /* Value is the internal representation of the specified cursor type
20247 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20248 of the bar cursor. */
20250 static enum text_cursor_kinds
20251 get_specified_cursor_type (arg, width)
20252 Lisp_Object arg;
20253 int *width;
20255 enum text_cursor_kinds type;
20257 if (NILP (arg))
20258 return NO_CURSOR;
20260 if (EQ (arg, Qbox))
20261 return FILLED_BOX_CURSOR;
20263 if (EQ (arg, Qhollow))
20264 return HOLLOW_BOX_CURSOR;
20266 if (EQ (arg, Qbar))
20268 *width = 2;
20269 return BAR_CURSOR;
20272 if (CONSP (arg)
20273 && EQ (XCAR (arg), Qbar)
20274 && INTEGERP (XCDR (arg))
20275 && XINT (XCDR (arg)) >= 0)
20277 *width = XINT (XCDR (arg));
20278 return BAR_CURSOR;
20281 if (EQ (arg, Qhbar))
20283 *width = 2;
20284 return HBAR_CURSOR;
20287 if (CONSP (arg)
20288 && EQ (XCAR (arg), Qhbar)
20289 && INTEGERP (XCDR (arg))
20290 && XINT (XCDR (arg)) >= 0)
20292 *width = XINT (XCDR (arg));
20293 return HBAR_CURSOR;
20296 /* Treat anything unknown as "hollow box cursor".
20297 It was bad to signal an error; people have trouble fixing
20298 .Xdefaults with Emacs, when it has something bad in it. */
20299 type = HOLLOW_BOX_CURSOR;
20301 return type;
20304 /* Set the default cursor types for specified frame. */
20305 void
20306 set_frame_cursor_types (f, arg)
20307 struct frame *f;
20308 Lisp_Object arg;
20310 int width;
20311 Lisp_Object tem;
20313 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20314 FRAME_CURSOR_WIDTH (f) = width;
20316 /* By default, set up the blink-off state depending on the on-state. */
20318 tem = Fassoc (arg, Vblink_cursor_alist);
20319 if (!NILP (tem))
20321 FRAME_BLINK_OFF_CURSOR (f)
20322 = get_specified_cursor_type (XCDR (tem), &width);
20323 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20325 else
20326 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20330 /* Return the cursor we want to be displayed in window W. Return
20331 width of bar/hbar cursor through WIDTH arg. Return with
20332 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20333 (i.e. if the `system caret' should track this cursor).
20335 In a mini-buffer window, we want the cursor only to appear if we
20336 are reading input from this window. For the selected window, we
20337 want the cursor type given by the frame parameter or buffer local
20338 setting of cursor-type. If explicitly marked off, draw no cursor.
20339 In all other cases, we want a hollow box cursor. */
20341 static enum text_cursor_kinds
20342 get_window_cursor_type (w, glyph, width, active_cursor)
20343 struct window *w;
20344 struct glyph *glyph;
20345 int *width;
20346 int *active_cursor;
20348 struct frame *f = XFRAME (w->frame);
20349 struct buffer *b = XBUFFER (w->buffer);
20350 int cursor_type = DEFAULT_CURSOR;
20351 Lisp_Object alt_cursor;
20352 int non_selected = 0;
20354 *active_cursor = 1;
20356 /* Echo area */
20357 if (cursor_in_echo_area
20358 && FRAME_HAS_MINIBUF_P (f)
20359 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20361 if (w == XWINDOW (echo_area_window))
20363 *width = FRAME_CURSOR_WIDTH (f);
20364 return FRAME_DESIRED_CURSOR (f);
20367 *active_cursor = 0;
20368 non_selected = 1;
20371 /* Nonselected window or nonselected frame. */
20372 else if (w != XWINDOW (f->selected_window)
20373 #ifdef HAVE_WINDOW_SYSTEM
20374 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20375 #endif
20378 *active_cursor = 0;
20380 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20381 return NO_CURSOR;
20383 non_selected = 1;
20386 /* Never display a cursor in a window in which cursor-type is nil. */
20387 if (NILP (b->cursor_type))
20388 return NO_CURSOR;
20390 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20391 if (non_selected)
20393 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20394 return get_specified_cursor_type (alt_cursor, width);
20397 /* Get the normal cursor type for this window. */
20398 if (EQ (b->cursor_type, Qt))
20400 cursor_type = FRAME_DESIRED_CURSOR (f);
20401 *width = FRAME_CURSOR_WIDTH (f);
20403 else
20404 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20406 /* Use normal cursor if not blinked off. */
20407 if (!w->cursor_off_p)
20409 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20410 if (cursor_type == FILLED_BOX_CURSOR)
20411 cursor_type = HOLLOW_BOX_CURSOR;
20413 return cursor_type;
20416 /* Cursor is blinked off, so determine how to "toggle" it. */
20418 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20419 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20420 return get_specified_cursor_type (XCDR (alt_cursor), width);
20422 /* Then see if frame has specified a specific blink off cursor type. */
20423 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20425 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20426 return FRAME_BLINK_OFF_CURSOR (f);
20429 #if 0
20430 /* Some people liked having a permanently visible blinking cursor,
20431 while others had very strong opinions against it. So it was
20432 decided to remove it. KFS 2003-09-03 */
20434 /* Finally perform built-in cursor blinking:
20435 filled box <-> hollow box
20436 wide [h]bar <-> narrow [h]bar
20437 narrow [h]bar <-> no cursor
20438 other type <-> no cursor */
20440 if (cursor_type == FILLED_BOX_CURSOR)
20441 return HOLLOW_BOX_CURSOR;
20443 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20445 *width = 1;
20446 return cursor_type;
20448 #endif
20450 return NO_CURSOR;
20454 #ifdef HAVE_WINDOW_SYSTEM
20456 /* Notice when the text cursor of window W has been completely
20457 overwritten by a drawing operation that outputs glyphs in AREA
20458 starting at X0 and ending at X1 in the line starting at Y0 and
20459 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20460 the rest of the line after X0 has been written. Y coordinates
20461 are window-relative. */
20463 static void
20464 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20465 struct window *w;
20466 enum glyph_row_area area;
20467 int x0, y0, x1, y1;
20469 int cx0, cx1, cy0, cy1;
20470 struct glyph_row *row;
20472 if (!w->phys_cursor_on_p)
20473 return;
20474 if (area != TEXT_AREA)
20475 return;
20477 if (w->phys_cursor.vpos < 0
20478 || w->phys_cursor.vpos >= w->current_matrix->nrows
20479 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20480 !(row->enabled_p && row->displays_text_p)))
20481 return;
20483 if (row->cursor_in_fringe_p)
20485 row->cursor_in_fringe_p = 0;
20486 draw_fringe_bitmap (w, row, 0);
20487 w->phys_cursor_on_p = 0;
20488 return;
20491 cx0 = w->phys_cursor.x;
20492 cx1 = cx0 + w->phys_cursor_width;
20493 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20494 return;
20496 /* The cursor image will be completely removed from the
20497 screen if the output area intersects the cursor area in
20498 y-direction. When we draw in [y0 y1[, and some part of
20499 the cursor is at y < y0, that part must have been drawn
20500 before. When scrolling, the cursor is erased before
20501 actually scrolling, so we don't come here. When not
20502 scrolling, the rows above the old cursor row must have
20503 changed, and in this case these rows must have written
20504 over the cursor image.
20506 Likewise if part of the cursor is below y1, with the
20507 exception of the cursor being in the first blank row at
20508 the buffer and window end because update_text_area
20509 doesn't draw that row. (Except when it does, but
20510 that's handled in update_text_area.) */
20512 cy0 = w->phys_cursor.y;
20513 cy1 = cy0 + w->phys_cursor_height;
20514 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20515 return;
20517 w->phys_cursor_on_p = 0;
20520 #endif /* HAVE_WINDOW_SYSTEM */
20523 /************************************************************************
20524 Mouse Face
20525 ************************************************************************/
20527 #ifdef HAVE_WINDOW_SYSTEM
20529 /* EXPORT for RIF:
20530 Fix the display of area AREA of overlapping row ROW in window W. */
20532 void
20533 x_fix_overlapping_area (w, row, area)
20534 struct window *w;
20535 struct glyph_row *row;
20536 enum glyph_row_area area;
20538 int i, x;
20540 BLOCK_INPUT;
20542 x = 0;
20543 for (i = 0; i < row->used[area];)
20545 if (row->glyphs[area][i].overlaps_vertically_p)
20547 int start = i, start_x = x;
20551 x += row->glyphs[area][i].pixel_width;
20552 ++i;
20554 while (i < row->used[area]
20555 && row->glyphs[area][i].overlaps_vertically_p);
20557 draw_glyphs (w, start_x, row, area,
20558 start, i,
20559 DRAW_NORMAL_TEXT, 1);
20561 else
20563 x += row->glyphs[area][i].pixel_width;
20564 ++i;
20568 UNBLOCK_INPUT;
20572 /* EXPORT:
20573 Draw the cursor glyph of window W in glyph row ROW. See the
20574 comment of draw_glyphs for the meaning of HL. */
20576 void
20577 draw_phys_cursor_glyph (w, row, hl)
20578 struct window *w;
20579 struct glyph_row *row;
20580 enum draw_glyphs_face hl;
20582 /* If cursor hpos is out of bounds, don't draw garbage. This can
20583 happen in mini-buffer windows when switching between echo area
20584 glyphs and mini-buffer. */
20585 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20587 int on_p = w->phys_cursor_on_p;
20588 int x1;
20589 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20590 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20591 hl, 0);
20592 w->phys_cursor_on_p = on_p;
20594 if (hl == DRAW_CURSOR)
20595 w->phys_cursor_width = x1 - w->phys_cursor.x;
20596 /* When we erase the cursor, and ROW is overlapped by other
20597 rows, make sure that these overlapping parts of other rows
20598 are redrawn. */
20599 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20601 if (row > w->current_matrix->rows
20602 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20603 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20605 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20606 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20607 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20613 /* EXPORT:
20614 Erase the image of a cursor of window W from the screen. */
20616 void
20617 erase_phys_cursor (w)
20618 struct window *w;
20620 struct frame *f = XFRAME (w->frame);
20621 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20622 int hpos = w->phys_cursor.hpos;
20623 int vpos = w->phys_cursor.vpos;
20624 int mouse_face_here_p = 0;
20625 struct glyph_matrix *active_glyphs = w->current_matrix;
20626 struct glyph_row *cursor_row;
20627 struct glyph *cursor_glyph;
20628 enum draw_glyphs_face hl;
20630 /* No cursor displayed or row invalidated => nothing to do on the
20631 screen. */
20632 if (w->phys_cursor_type == NO_CURSOR)
20633 goto mark_cursor_off;
20635 /* VPOS >= active_glyphs->nrows means that window has been resized.
20636 Don't bother to erase the cursor. */
20637 if (vpos >= active_glyphs->nrows)
20638 goto mark_cursor_off;
20640 /* If row containing cursor is marked invalid, there is nothing we
20641 can do. */
20642 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20643 if (!cursor_row->enabled_p)
20644 goto mark_cursor_off;
20646 /* If line spacing is > 0, old cursor may only be partially visible in
20647 window after split-window. So adjust visible height. */
20648 cursor_row->visible_height = min (cursor_row->visible_height,
20649 window_text_bottom_y (w) - cursor_row->y);
20651 /* If row is completely invisible, don't attempt to delete a cursor which
20652 isn't there. This can happen if cursor is at top of a window, and
20653 we switch to a buffer with a header line in that window. */
20654 if (cursor_row->visible_height <= 0)
20655 goto mark_cursor_off;
20657 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20658 if (cursor_row->cursor_in_fringe_p)
20660 cursor_row->cursor_in_fringe_p = 0;
20661 draw_fringe_bitmap (w, cursor_row, 0);
20662 goto mark_cursor_off;
20665 /* This can happen when the new row is shorter than the old one.
20666 In this case, either draw_glyphs or clear_end_of_line
20667 should have cleared the cursor. Note that we wouldn't be
20668 able to erase the cursor in this case because we don't have a
20669 cursor glyph at hand. */
20670 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20671 goto mark_cursor_off;
20673 /* If the cursor is in the mouse face area, redisplay that when
20674 we clear the cursor. */
20675 if (! NILP (dpyinfo->mouse_face_window)
20676 && w == XWINDOW (dpyinfo->mouse_face_window)
20677 && (vpos > dpyinfo->mouse_face_beg_row
20678 || (vpos == dpyinfo->mouse_face_beg_row
20679 && hpos >= dpyinfo->mouse_face_beg_col))
20680 && (vpos < dpyinfo->mouse_face_end_row
20681 || (vpos == dpyinfo->mouse_face_end_row
20682 && hpos < dpyinfo->mouse_face_end_col))
20683 /* Don't redraw the cursor's spot in mouse face if it is at the
20684 end of a line (on a newline). The cursor appears there, but
20685 mouse highlighting does not. */
20686 && cursor_row->used[TEXT_AREA] > hpos)
20687 mouse_face_here_p = 1;
20689 /* Maybe clear the display under the cursor. */
20690 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20692 int x, y;
20693 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20694 int width;
20696 cursor_glyph = get_phys_cursor_glyph (w);
20697 if (cursor_glyph == NULL)
20698 goto mark_cursor_off;
20700 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20701 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20702 width = min (cursor_glyph->pixel_width,
20703 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20705 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20708 /* Erase the cursor by redrawing the character underneath it. */
20709 if (mouse_face_here_p)
20710 hl = DRAW_MOUSE_FACE;
20711 else
20712 hl = DRAW_NORMAL_TEXT;
20713 draw_phys_cursor_glyph (w, cursor_row, hl);
20715 mark_cursor_off:
20716 w->phys_cursor_on_p = 0;
20717 w->phys_cursor_type = NO_CURSOR;
20721 /* EXPORT:
20722 Display or clear cursor of window W. If ON is zero, clear the
20723 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20724 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20726 void
20727 display_and_set_cursor (w, on, hpos, vpos, x, y)
20728 struct window *w;
20729 int on, hpos, vpos, x, y;
20731 struct frame *f = XFRAME (w->frame);
20732 int new_cursor_type;
20733 int new_cursor_width;
20734 int active_cursor;
20735 struct glyph_row *glyph_row;
20736 struct glyph *glyph;
20738 /* This is pointless on invisible frames, and dangerous on garbaged
20739 windows and frames; in the latter case, the frame or window may
20740 be in the midst of changing its size, and x and y may be off the
20741 window. */
20742 if (! FRAME_VISIBLE_P (f)
20743 || FRAME_GARBAGED_P (f)
20744 || vpos >= w->current_matrix->nrows
20745 || hpos >= w->current_matrix->matrix_w)
20746 return;
20748 /* If cursor is off and we want it off, return quickly. */
20749 if (!on && !w->phys_cursor_on_p)
20750 return;
20752 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20753 /* If cursor row is not enabled, we don't really know where to
20754 display the cursor. */
20755 if (!glyph_row->enabled_p)
20757 w->phys_cursor_on_p = 0;
20758 return;
20761 glyph = NULL;
20762 if (!glyph_row->exact_window_width_line_p
20763 || hpos < glyph_row->used[TEXT_AREA])
20764 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20766 xassert (interrupt_input_blocked);
20768 /* Set new_cursor_type to the cursor we want to be displayed. */
20769 new_cursor_type = get_window_cursor_type (w, glyph,
20770 &new_cursor_width, &active_cursor);
20772 /* If cursor is currently being shown and we don't want it to be or
20773 it is in the wrong place, or the cursor type is not what we want,
20774 erase it. */
20775 if (w->phys_cursor_on_p
20776 && (!on
20777 || w->phys_cursor.x != x
20778 || w->phys_cursor.y != y
20779 || new_cursor_type != w->phys_cursor_type
20780 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20781 && new_cursor_width != w->phys_cursor_width)))
20782 erase_phys_cursor (w);
20784 /* Don't check phys_cursor_on_p here because that flag is only set
20785 to zero in some cases where we know that the cursor has been
20786 completely erased, to avoid the extra work of erasing the cursor
20787 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20788 still not be visible, or it has only been partly erased. */
20789 if (on)
20791 w->phys_cursor_ascent = glyph_row->ascent;
20792 w->phys_cursor_height = glyph_row->height;
20794 /* Set phys_cursor_.* before x_draw_.* is called because some
20795 of them may need the information. */
20796 w->phys_cursor.x = x;
20797 w->phys_cursor.y = glyph_row->y;
20798 w->phys_cursor.hpos = hpos;
20799 w->phys_cursor.vpos = vpos;
20802 rif->draw_window_cursor (w, glyph_row, x, y,
20803 new_cursor_type, new_cursor_width,
20804 on, active_cursor);
20808 /* Switch the display of W's cursor on or off, according to the value
20809 of ON. */
20811 static void
20812 update_window_cursor (w, on)
20813 struct window *w;
20814 int on;
20816 /* Don't update cursor in windows whose frame is in the process
20817 of being deleted. */
20818 if (w->current_matrix)
20820 BLOCK_INPUT;
20821 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20822 w->phys_cursor.x, w->phys_cursor.y);
20823 UNBLOCK_INPUT;
20828 /* Call update_window_cursor with parameter ON_P on all leaf windows
20829 in the window tree rooted at W. */
20831 static void
20832 update_cursor_in_window_tree (w, on_p)
20833 struct window *w;
20834 int on_p;
20836 while (w)
20838 if (!NILP (w->hchild))
20839 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20840 else if (!NILP (w->vchild))
20841 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20842 else
20843 update_window_cursor (w, on_p);
20845 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20850 /* EXPORT:
20851 Display the cursor on window W, or clear it, according to ON_P.
20852 Don't change the cursor's position. */
20854 void
20855 x_update_cursor (f, on_p)
20856 struct frame *f;
20857 int on_p;
20859 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20863 /* EXPORT:
20864 Clear the cursor of window W to background color, and mark the
20865 cursor as not shown. This is used when the text where the cursor
20866 is is about to be rewritten. */
20868 void
20869 x_clear_cursor (w)
20870 struct window *w;
20872 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20873 update_window_cursor (w, 0);
20877 /* EXPORT:
20878 Display the active region described by mouse_face_* according to DRAW. */
20880 void
20881 show_mouse_face (dpyinfo, draw)
20882 Display_Info *dpyinfo;
20883 enum draw_glyphs_face draw;
20885 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20886 struct frame *f = XFRAME (WINDOW_FRAME (w));
20888 if (/* If window is in the process of being destroyed, don't bother
20889 to do anything. */
20890 w->current_matrix != NULL
20891 /* Don't update mouse highlight if hidden */
20892 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20893 /* Recognize when we are called to operate on rows that don't exist
20894 anymore. This can happen when a window is split. */
20895 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20897 int phys_cursor_on_p = w->phys_cursor_on_p;
20898 struct glyph_row *row, *first, *last;
20900 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20901 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20903 for (row = first; row <= last && row->enabled_p; ++row)
20905 int start_hpos, end_hpos, start_x;
20907 /* For all but the first row, the highlight starts at column 0. */
20908 if (row == first)
20910 start_hpos = dpyinfo->mouse_face_beg_col;
20911 start_x = dpyinfo->mouse_face_beg_x;
20913 else
20915 start_hpos = 0;
20916 start_x = 0;
20919 if (row == last)
20920 end_hpos = dpyinfo->mouse_face_end_col;
20921 else
20922 end_hpos = row->used[TEXT_AREA];
20924 if (end_hpos > start_hpos)
20926 draw_glyphs (w, start_x, row, TEXT_AREA,
20927 start_hpos, end_hpos,
20928 draw, 0);
20930 row->mouse_face_p
20931 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20935 /* When we've written over the cursor, arrange for it to
20936 be displayed again. */
20937 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20939 BLOCK_INPUT;
20940 display_and_set_cursor (w, 1,
20941 w->phys_cursor.hpos, w->phys_cursor.vpos,
20942 w->phys_cursor.x, w->phys_cursor.y);
20943 UNBLOCK_INPUT;
20947 /* Change the mouse cursor. */
20948 if (draw == DRAW_NORMAL_TEXT)
20949 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20950 else if (draw == DRAW_MOUSE_FACE)
20951 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20952 else
20953 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20956 /* EXPORT:
20957 Clear out the mouse-highlighted active region.
20958 Redraw it un-highlighted first. Value is non-zero if mouse
20959 face was actually drawn unhighlighted. */
20962 clear_mouse_face (dpyinfo)
20963 Display_Info *dpyinfo;
20965 int cleared = 0;
20967 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20969 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20970 cleared = 1;
20973 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20974 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20975 dpyinfo->mouse_face_window = Qnil;
20976 dpyinfo->mouse_face_overlay = Qnil;
20977 return cleared;
20981 /* EXPORT:
20982 Non-zero if physical cursor of window W is within mouse face. */
20985 cursor_in_mouse_face_p (w)
20986 struct window *w;
20988 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20989 int in_mouse_face = 0;
20991 if (WINDOWP (dpyinfo->mouse_face_window)
20992 && XWINDOW (dpyinfo->mouse_face_window) == w)
20994 int hpos = w->phys_cursor.hpos;
20995 int vpos = w->phys_cursor.vpos;
20997 if (vpos >= dpyinfo->mouse_face_beg_row
20998 && vpos <= dpyinfo->mouse_face_end_row
20999 && (vpos > dpyinfo->mouse_face_beg_row
21000 || hpos >= dpyinfo->mouse_face_beg_col)
21001 && (vpos < dpyinfo->mouse_face_end_row
21002 || hpos < dpyinfo->mouse_face_end_col
21003 || dpyinfo->mouse_face_past_end))
21004 in_mouse_face = 1;
21007 return in_mouse_face;
21013 /* Find the glyph matrix position of buffer position CHARPOS in window
21014 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21015 current glyphs must be up to date. If CHARPOS is above window
21016 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21017 of last line in W. In the row containing CHARPOS, stop before glyphs
21018 having STOP as object. */
21020 #if 1 /* This is a version of fast_find_position that's more correct
21021 in the presence of hscrolling, for example. I didn't install
21022 it right away because the problem fixed is minor, it failed
21023 in 20.x as well, and I think it's too risky to install
21024 so near the release of 21.1. 2001-09-25 gerd. */
21026 static int
21027 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21028 struct window *w;
21029 int charpos;
21030 int *hpos, *vpos, *x, *y;
21031 Lisp_Object stop;
21033 struct glyph_row *row, *first;
21034 struct glyph *glyph, *end;
21035 int past_end = 0;
21037 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21038 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21040 *x = first->x;
21041 *y = first->y;
21042 *hpos = 0;
21043 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21044 return 1;
21047 row = row_containing_pos (w, charpos, first, NULL, 0);
21048 if (row == NULL)
21050 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21051 past_end = 1;
21054 /* If whole rows or last part of a row came from a display overlay,
21055 row_containing_pos will skip over such rows because their end pos
21056 equals the start pos of the overlay or interval.
21058 Move back if we have a STOP object and previous row's
21059 end glyph came from STOP. */
21060 if (!NILP (stop))
21062 struct glyph_row *prev;
21063 while ((prev = row - 1, prev >= first)
21064 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21065 && prev->used[TEXT_AREA] > 0)
21067 struct glyph *beg = prev->glyphs[TEXT_AREA];
21068 glyph = beg + prev->used[TEXT_AREA];
21069 while (--glyph >= beg
21070 && INTEGERP (glyph->object));
21071 if (glyph < beg
21072 || !EQ (stop, glyph->object))
21073 break;
21074 row = prev;
21078 *x = row->x;
21079 *y = row->y;
21080 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21082 glyph = row->glyphs[TEXT_AREA];
21083 end = glyph + row->used[TEXT_AREA];
21085 /* Skip over glyphs not having an object at the start of the row.
21086 These are special glyphs like truncation marks on terminal
21087 frames. */
21088 if (row->displays_text_p)
21089 while (glyph < end
21090 && INTEGERP (glyph->object)
21091 && !EQ (stop, glyph->object)
21092 && glyph->charpos < 0)
21094 *x += glyph->pixel_width;
21095 ++glyph;
21098 while (glyph < end
21099 && !INTEGERP (glyph->object)
21100 && !EQ (stop, glyph->object)
21101 && (!BUFFERP (glyph->object)
21102 || glyph->charpos < charpos))
21104 *x += glyph->pixel_width;
21105 ++glyph;
21108 *hpos = glyph - row->glyphs[TEXT_AREA];
21109 return !past_end;
21112 #else /* not 1 */
21114 static int
21115 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21116 struct window *w;
21117 int pos;
21118 int *hpos, *vpos, *x, *y;
21119 Lisp_Object stop;
21121 int i;
21122 int lastcol;
21123 int maybe_next_line_p = 0;
21124 int line_start_position;
21125 int yb = window_text_bottom_y (w);
21126 struct glyph_row *row, *best_row;
21127 int row_vpos, best_row_vpos;
21128 int current_x;
21130 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21131 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21133 while (row->y < yb)
21135 if (row->used[TEXT_AREA])
21136 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21137 else
21138 line_start_position = 0;
21140 if (line_start_position > pos)
21141 break;
21142 /* If the position sought is the end of the buffer,
21143 don't include the blank lines at the bottom of the window. */
21144 else if (line_start_position == pos
21145 && pos == BUF_ZV (XBUFFER (w->buffer)))
21147 maybe_next_line_p = 1;
21148 break;
21150 else if (line_start_position > 0)
21152 best_row = row;
21153 best_row_vpos = row_vpos;
21156 if (row->y + row->height >= yb)
21157 break;
21159 ++row;
21160 ++row_vpos;
21163 /* Find the right column within BEST_ROW. */
21164 lastcol = 0;
21165 current_x = best_row->x;
21166 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21168 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21169 int charpos = glyph->charpos;
21171 if (BUFFERP (glyph->object))
21173 if (charpos == pos)
21175 *hpos = i;
21176 *vpos = best_row_vpos;
21177 *x = current_x;
21178 *y = best_row->y;
21179 return 1;
21181 else if (charpos > pos)
21182 break;
21184 else if (EQ (glyph->object, stop))
21185 break;
21187 if (charpos > 0)
21188 lastcol = i;
21189 current_x += glyph->pixel_width;
21192 /* If we're looking for the end of the buffer,
21193 and we didn't find it in the line we scanned,
21194 use the start of the following line. */
21195 if (maybe_next_line_p)
21197 ++best_row;
21198 ++best_row_vpos;
21199 lastcol = 0;
21200 current_x = best_row->x;
21203 *vpos = best_row_vpos;
21204 *hpos = lastcol + 1;
21205 *x = current_x;
21206 *y = best_row->y;
21207 return 0;
21210 #endif /* not 1 */
21213 /* Find the position of the glyph for position POS in OBJECT in
21214 window W's current matrix, and return in *X, *Y the pixel
21215 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21217 RIGHT_P non-zero means return the position of the right edge of the
21218 glyph, RIGHT_P zero means return the left edge position.
21220 If no glyph for POS exists in the matrix, return the position of
21221 the glyph with the next smaller position that is in the matrix, if
21222 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21223 exists in the matrix, return the position of the glyph with the
21224 next larger position in OBJECT.
21226 Value is non-zero if a glyph was found. */
21228 static int
21229 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21230 struct window *w;
21231 int pos;
21232 Lisp_Object object;
21233 int *hpos, *vpos, *x, *y;
21234 int right_p;
21236 int yb = window_text_bottom_y (w);
21237 struct glyph_row *r;
21238 struct glyph *best_glyph = NULL;
21239 struct glyph_row *best_row = NULL;
21240 int best_x = 0;
21242 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21243 r->enabled_p && r->y < yb;
21244 ++r)
21246 struct glyph *g = r->glyphs[TEXT_AREA];
21247 struct glyph *e = g + r->used[TEXT_AREA];
21248 int gx;
21250 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21251 if (EQ (g->object, object))
21253 if (g->charpos == pos)
21255 best_glyph = g;
21256 best_x = gx;
21257 best_row = r;
21258 goto found;
21260 else if (best_glyph == NULL
21261 || ((abs (g->charpos - pos)
21262 < abs (best_glyph->charpos - pos))
21263 && (right_p
21264 ? g->charpos < pos
21265 : g->charpos > pos)))
21267 best_glyph = g;
21268 best_x = gx;
21269 best_row = r;
21274 found:
21276 if (best_glyph)
21278 *x = best_x;
21279 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21281 if (right_p)
21283 *x += best_glyph->pixel_width;
21284 ++*hpos;
21287 *y = best_row->y;
21288 *vpos = best_row - w->current_matrix->rows;
21291 return best_glyph != NULL;
21295 /* See if position X, Y is within a hot-spot of an image. */
21297 static int
21298 on_hot_spot_p (hot_spot, x, y)
21299 Lisp_Object hot_spot;
21300 int x, y;
21302 if (!CONSP (hot_spot))
21303 return 0;
21305 if (EQ (XCAR (hot_spot), Qrect))
21307 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21308 Lisp_Object rect = XCDR (hot_spot);
21309 Lisp_Object tem;
21310 if (!CONSP (rect))
21311 return 0;
21312 if (!CONSP (XCAR (rect)))
21313 return 0;
21314 if (!CONSP (XCDR (rect)))
21315 return 0;
21316 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21317 return 0;
21318 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21319 return 0;
21320 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21321 return 0;
21322 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21323 return 0;
21324 return 1;
21326 else if (EQ (XCAR (hot_spot), Qcircle))
21328 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21329 Lisp_Object circ = XCDR (hot_spot);
21330 Lisp_Object lr, lx0, ly0;
21331 if (CONSP (circ)
21332 && CONSP (XCAR (circ))
21333 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21334 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21335 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21337 double r = XFLOATINT (lr);
21338 double dx = XINT (lx0) - x;
21339 double dy = XINT (ly0) - y;
21340 return (dx * dx + dy * dy <= r * r);
21343 else if (EQ (XCAR (hot_spot), Qpoly))
21345 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21346 if (VECTORP (XCDR (hot_spot)))
21348 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21349 Lisp_Object *poly = v->contents;
21350 int n = v->size;
21351 int i;
21352 int inside = 0;
21353 Lisp_Object lx, ly;
21354 int x0, y0;
21356 /* Need an even number of coordinates, and at least 3 edges. */
21357 if (n < 6 || n & 1)
21358 return 0;
21360 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21361 If count is odd, we are inside polygon. Pixels on edges
21362 may or may not be included depending on actual geometry of the
21363 polygon. */
21364 if ((lx = poly[n-2], !INTEGERP (lx))
21365 || (ly = poly[n-1], !INTEGERP (lx)))
21366 return 0;
21367 x0 = XINT (lx), y0 = XINT (ly);
21368 for (i = 0; i < n; i += 2)
21370 int x1 = x0, y1 = y0;
21371 if ((lx = poly[i], !INTEGERP (lx))
21372 || (ly = poly[i+1], !INTEGERP (ly)))
21373 return 0;
21374 x0 = XINT (lx), y0 = XINT (ly);
21376 /* Does this segment cross the X line? */
21377 if (x0 >= x)
21379 if (x1 >= x)
21380 continue;
21382 else if (x1 < x)
21383 continue;
21384 if (y > y0 && y > y1)
21385 continue;
21386 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21387 inside = !inside;
21389 return inside;
21392 /* If we don't understand the format, pretend we're not in the hot-spot. */
21393 return 0;
21396 Lisp_Object
21397 find_hot_spot (map, x, y)
21398 Lisp_Object map;
21399 int x, y;
21401 while (CONSP (map))
21403 if (CONSP (XCAR (map))
21404 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21405 return XCAR (map);
21406 map = XCDR (map);
21409 return Qnil;
21412 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21413 3, 3, 0,
21414 doc: /* Lookup in image map MAP coordinates X and Y.
21415 An image map is an alist where each element has the format (AREA ID PLIST).
21416 An AREA is specified as either a rectangle, a circle, or a polygon:
21417 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21418 pixel coordinates of the upper left and bottom right corners.
21419 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21420 and the radius of the circle; r may be a float or integer.
21421 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21422 vector describes one corner in the polygon.
21423 Returns the alist element for the first matching AREA in MAP. */)
21424 (map, x, y)
21425 Lisp_Object map;
21426 Lisp_Object x, y;
21428 if (NILP (map))
21429 return Qnil;
21431 CHECK_NUMBER (x);
21432 CHECK_NUMBER (y);
21434 return find_hot_spot (map, XINT (x), XINT (y));
21438 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21439 static void
21440 define_frame_cursor1 (f, cursor, pointer)
21441 struct frame *f;
21442 Cursor cursor;
21443 Lisp_Object pointer;
21445 /* Do not change cursor shape while dragging mouse. */
21446 if (!NILP (do_mouse_tracking))
21447 return;
21449 if (!NILP (pointer))
21451 if (EQ (pointer, Qarrow))
21452 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21453 else if (EQ (pointer, Qhand))
21454 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21455 else if (EQ (pointer, Qtext))
21456 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21457 else if (EQ (pointer, intern ("hdrag")))
21458 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21459 #ifdef HAVE_X_WINDOWS
21460 else if (EQ (pointer, intern ("vdrag")))
21461 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21462 #endif
21463 else if (EQ (pointer, intern ("hourglass")))
21464 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21465 else if (EQ (pointer, Qmodeline))
21466 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21467 else
21468 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21471 if (cursor != No_Cursor)
21472 rif->define_frame_cursor (f, cursor);
21475 /* Take proper action when mouse has moved to the mode or header line
21476 or marginal area AREA of window W, x-position X and y-position Y.
21477 X is relative to the start of the text display area of W, so the
21478 width of bitmap areas and scroll bars must be subtracted to get a
21479 position relative to the start of the mode line. */
21481 static void
21482 note_mode_line_or_margin_highlight (window, x, y, area)
21483 Lisp_Object window;
21484 int x, y;
21485 enum window_part area;
21487 struct window *w = XWINDOW (window);
21488 struct frame *f = XFRAME (w->frame);
21489 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21490 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21491 Lisp_Object pointer = Qnil;
21492 int charpos, dx, dy, width, height;
21493 Lisp_Object string, object = Qnil;
21494 Lisp_Object pos, help;
21496 Lisp_Object mouse_face;
21497 int original_x_pixel = x;
21498 struct glyph * glyph = NULL;
21499 struct glyph_row *row;
21501 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21503 int x0;
21504 struct glyph *end;
21506 string = mode_line_string (w, area, &x, &y, &charpos,
21507 &object, &dx, &dy, &width, &height);
21509 row = (area == ON_MODE_LINE
21510 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21511 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21513 /* Find glyph */
21514 if (row->mode_line_p && row->enabled_p)
21516 glyph = row->glyphs[TEXT_AREA];
21517 end = glyph + row->used[TEXT_AREA];
21519 for (x0 = original_x_pixel;
21520 glyph < end && x0 >= glyph->pixel_width;
21521 ++glyph)
21522 x0 -= glyph->pixel_width;
21524 if (glyph >= end)
21525 glyph = NULL;
21528 else
21530 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21531 string = marginal_area_string (w, area, &x, &y, &charpos,
21532 &object, &dx, &dy, &width, &height);
21535 help = Qnil;
21537 if (IMAGEP (object))
21539 Lisp_Object image_map, hotspot;
21540 if ((image_map = Fplist_get (XCDR (object), QCmap),
21541 !NILP (image_map))
21542 && (hotspot = find_hot_spot (image_map, dx, dy),
21543 CONSP (hotspot))
21544 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21546 Lisp_Object area_id, plist;
21548 area_id = XCAR (hotspot);
21549 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21550 If so, we could look for mouse-enter, mouse-leave
21551 properties in PLIST (and do something...). */
21552 hotspot = XCDR (hotspot);
21553 if (CONSP (hotspot)
21554 && (plist = XCAR (hotspot), CONSP (plist)))
21556 pointer = Fplist_get (plist, Qpointer);
21557 if (NILP (pointer))
21558 pointer = Qhand;
21559 help = Fplist_get (plist, Qhelp_echo);
21560 if (!NILP (help))
21562 help_echo_string = help;
21563 /* Is this correct? ++kfs */
21564 XSETWINDOW (help_echo_window, w);
21565 help_echo_object = w->buffer;
21566 help_echo_pos = charpos;
21570 if (NILP (pointer))
21571 pointer = Fplist_get (XCDR (object), QCpointer);
21574 if (STRINGP (string))
21576 pos = make_number (charpos);
21577 /* If we're on a string with `help-echo' text property, arrange
21578 for the help to be displayed. This is done by setting the
21579 global variable help_echo_string to the help string. */
21580 if (NILP (help))
21582 help = Fget_text_property (pos, Qhelp_echo, string);
21583 if (!NILP (help))
21585 help_echo_string = help;
21586 XSETWINDOW (help_echo_window, w);
21587 help_echo_object = string;
21588 help_echo_pos = charpos;
21592 if (NILP (pointer))
21593 pointer = Fget_text_property (pos, Qpointer, string);
21595 /* Change the mouse pointer according to what is under X/Y. */
21596 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21598 Lisp_Object map;
21599 map = Fget_text_property (pos, Qlocal_map, string);
21600 if (!KEYMAPP (map))
21601 map = Fget_text_property (pos, Qkeymap, string);
21602 if (!KEYMAPP (map))
21603 cursor = dpyinfo->vertical_scroll_bar_cursor;
21606 /* Change the mouse face according to what is under X/Y. */
21607 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21608 if (!NILP (mouse_face)
21609 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21610 && glyph)
21612 Lisp_Object b, e;
21614 struct glyph * tmp_glyph;
21616 int gpos;
21617 int gseq_length;
21618 int total_pixel_width;
21619 int ignore;
21621 int vpos, hpos;
21623 b = Fprevious_single_property_change (make_number (charpos + 1),
21624 Qmouse_face, string, Qnil);
21625 if (NILP (b))
21626 b = make_number (0);
21628 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21629 if (NILP (e))
21630 e = make_number (SCHARS (string));
21632 /* Calculate the position(glyph position: GPOS) of GLYPH in
21633 displayed string. GPOS is different from CHARPOS.
21635 CHARPOS is the position of glyph in internal string
21636 object. A mode line string format has structures which
21637 is converted to a flatten by emacs lisp interpreter.
21638 The internal string is an element of the structures.
21639 The displayed string is the flatten string. */
21640 for (tmp_glyph = glyph - 1, gpos = 0;
21641 tmp_glyph->charpos >= XINT (b);
21642 tmp_glyph--, gpos++)
21644 if (!EQ (tmp_glyph->object, glyph->object))
21645 break;
21648 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21649 displayed string holding GLYPH.
21651 GSEQ_LENGTH is different from SCHARS (STRING).
21652 SCHARS (STRING) returns the length of the internal string. */
21653 for (tmp_glyph = glyph, gseq_length = gpos;
21654 tmp_glyph->charpos < XINT (e);
21655 tmp_glyph++, gseq_length++)
21657 if (!EQ (tmp_glyph->object, glyph->object))
21658 break;
21661 total_pixel_width = 0;
21662 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21663 total_pixel_width += tmp_glyph->pixel_width;
21665 /* Pre calculation of re-rendering position */
21666 vpos = (x - gpos);
21667 hpos = (area == ON_MODE_LINE
21668 ? (w->current_matrix)->nrows - 1
21669 : 0);
21671 /* If the re-rendering position is included in the last
21672 re-rendering area, we should do nothing. */
21673 if ( EQ (window, dpyinfo->mouse_face_window)
21674 && dpyinfo->mouse_face_beg_col <= vpos
21675 && vpos < dpyinfo->mouse_face_end_col
21676 && dpyinfo->mouse_face_beg_row == hpos )
21677 return;
21679 if (clear_mouse_face (dpyinfo))
21680 cursor = No_Cursor;
21682 dpyinfo->mouse_face_beg_col = vpos;
21683 dpyinfo->mouse_face_beg_row = hpos;
21685 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21686 dpyinfo->mouse_face_beg_y = 0;
21688 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21689 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21691 dpyinfo->mouse_face_end_x = 0;
21692 dpyinfo->mouse_face_end_y = 0;
21694 dpyinfo->mouse_face_past_end = 0;
21695 dpyinfo->mouse_face_window = window;
21697 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21698 charpos,
21699 0, 0, 0, &ignore,
21700 glyph->face_id, 1);
21701 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21703 if (NILP (pointer))
21704 pointer = Qhand;
21706 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21707 clear_mouse_face (dpyinfo);
21709 define_frame_cursor1 (f, cursor, pointer);
21713 /* EXPORT:
21714 Take proper action when the mouse has moved to position X, Y on
21715 frame F as regards highlighting characters that have mouse-face
21716 properties. Also de-highlighting chars where the mouse was before.
21717 X and Y can be negative or out of range. */
21719 void
21720 note_mouse_highlight (f, x, y)
21721 struct frame *f;
21722 int x, y;
21724 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21725 enum window_part part;
21726 Lisp_Object window;
21727 struct window *w;
21728 Cursor cursor = No_Cursor;
21729 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21730 struct buffer *b;
21732 /* When a menu is active, don't highlight because this looks odd. */
21733 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21734 if (popup_activated ())
21735 return;
21736 #endif
21738 if (NILP (Vmouse_highlight)
21739 || !f->glyphs_initialized_p)
21740 return;
21742 dpyinfo->mouse_face_mouse_x = x;
21743 dpyinfo->mouse_face_mouse_y = y;
21744 dpyinfo->mouse_face_mouse_frame = f;
21746 if (dpyinfo->mouse_face_defer)
21747 return;
21749 if (gc_in_progress)
21751 dpyinfo->mouse_face_deferred_gc = 1;
21752 return;
21755 /* Which window is that in? */
21756 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21758 /* If we were displaying active text in another window, clear that.
21759 Also clear if we move out of text area in same window. */
21760 if (! EQ (window, dpyinfo->mouse_face_window)
21761 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21762 && !NILP (dpyinfo->mouse_face_window)))
21763 clear_mouse_face (dpyinfo);
21765 /* Not on a window -> return. */
21766 if (!WINDOWP (window))
21767 return;
21769 /* Reset help_echo_string. It will get recomputed below. */
21770 help_echo_string = Qnil;
21772 /* Convert to window-relative pixel coordinates. */
21773 w = XWINDOW (window);
21774 frame_to_window_pixel_xy (w, &x, &y);
21776 /* Handle tool-bar window differently since it doesn't display a
21777 buffer. */
21778 if (EQ (window, f->tool_bar_window))
21780 note_tool_bar_highlight (f, x, y);
21781 return;
21784 /* Mouse is on the mode, header line or margin? */
21785 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21786 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21788 note_mode_line_or_margin_highlight (window, x, y, part);
21789 return;
21792 if (part == ON_VERTICAL_BORDER)
21793 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21794 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21795 || part == ON_SCROLL_BAR)
21796 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21797 else
21798 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21800 /* Are we in a window whose display is up to date?
21801 And verify the buffer's text has not changed. */
21802 b = XBUFFER (w->buffer);
21803 if (part == ON_TEXT
21804 && EQ (w->window_end_valid, w->buffer)
21805 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21806 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21808 int hpos, vpos, pos, i, dx, dy, area;
21809 struct glyph *glyph;
21810 Lisp_Object object;
21811 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21812 Lisp_Object *overlay_vec = NULL;
21813 int noverlays;
21814 struct buffer *obuf;
21815 int obegv, ozv, same_region;
21817 /* Find the glyph under X/Y. */
21818 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21820 /* Look for :pointer property on image. */
21821 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21823 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21824 if (img != NULL && IMAGEP (img->spec))
21826 Lisp_Object image_map, hotspot;
21827 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21828 !NILP (image_map))
21829 && (hotspot = find_hot_spot (image_map,
21830 glyph->slice.x + dx,
21831 glyph->slice.y + dy),
21832 CONSP (hotspot))
21833 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21835 Lisp_Object area_id, plist;
21837 area_id = XCAR (hotspot);
21838 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21839 If so, we could look for mouse-enter, mouse-leave
21840 properties in PLIST (and do something...). */
21841 hotspot = XCDR (hotspot);
21842 if (CONSP (hotspot)
21843 && (plist = XCAR (hotspot), CONSP (plist)))
21845 pointer = Fplist_get (plist, Qpointer);
21846 if (NILP (pointer))
21847 pointer = Qhand;
21848 help_echo_string = Fplist_get (plist, Qhelp_echo);
21849 if (!NILP (help_echo_string))
21851 help_echo_window = window;
21852 help_echo_object = glyph->object;
21853 help_echo_pos = glyph->charpos;
21857 if (NILP (pointer))
21858 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21862 /* Clear mouse face if X/Y not over text. */
21863 if (glyph == NULL
21864 || area != TEXT_AREA
21865 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21867 if (clear_mouse_face (dpyinfo))
21868 cursor = No_Cursor;
21869 if (NILP (pointer))
21871 if (area != TEXT_AREA)
21872 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21873 else
21874 pointer = Vvoid_text_area_pointer;
21876 goto set_cursor;
21879 pos = glyph->charpos;
21880 object = glyph->object;
21881 if (!STRINGP (object) && !BUFFERP (object))
21882 goto set_cursor;
21884 /* If we get an out-of-range value, return now; avoid an error. */
21885 if (BUFFERP (object) && pos > BUF_Z (b))
21886 goto set_cursor;
21888 /* Make the window's buffer temporarily current for
21889 overlays_at and compute_char_face. */
21890 obuf = current_buffer;
21891 current_buffer = b;
21892 obegv = BEGV;
21893 ozv = ZV;
21894 BEGV = BEG;
21895 ZV = Z;
21897 /* Is this char mouse-active or does it have help-echo? */
21898 position = make_number (pos);
21900 if (BUFFERP (object))
21902 /* Put all the overlays we want in a vector in overlay_vec. */
21903 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21904 /* Sort overlays into increasing priority order. */
21905 noverlays = sort_overlays (overlay_vec, noverlays, w);
21907 else
21908 noverlays = 0;
21910 same_region = (EQ (window, dpyinfo->mouse_face_window)
21911 && vpos >= dpyinfo->mouse_face_beg_row
21912 && vpos <= dpyinfo->mouse_face_end_row
21913 && (vpos > dpyinfo->mouse_face_beg_row
21914 || hpos >= dpyinfo->mouse_face_beg_col)
21915 && (vpos < dpyinfo->mouse_face_end_row
21916 || hpos < dpyinfo->mouse_face_end_col
21917 || dpyinfo->mouse_face_past_end));
21919 if (same_region)
21920 cursor = No_Cursor;
21922 /* Check mouse-face highlighting. */
21923 if (! same_region
21924 /* If there exists an overlay with mouse-face overlapping
21925 the one we are currently highlighting, we have to
21926 check if we enter the overlapping overlay, and then
21927 highlight only that. */
21928 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21929 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21931 /* Find the highest priority overlay that has a mouse-face
21932 property. */
21933 overlay = Qnil;
21934 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21936 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21937 if (!NILP (mouse_face))
21938 overlay = overlay_vec[i];
21941 /* If we're actually highlighting the same overlay as
21942 before, there's no need to do that again. */
21943 if (!NILP (overlay)
21944 && EQ (overlay, dpyinfo->mouse_face_overlay))
21945 goto check_help_echo;
21947 dpyinfo->mouse_face_overlay = overlay;
21949 /* Clear the display of the old active region, if any. */
21950 if (clear_mouse_face (dpyinfo))
21951 cursor = No_Cursor;
21953 /* If no overlay applies, get a text property. */
21954 if (NILP (overlay))
21955 mouse_face = Fget_text_property (position, Qmouse_face, object);
21957 /* Handle the overlay case. */
21958 if (!NILP (overlay))
21960 /* Find the range of text around this char that
21961 should be active. */
21962 Lisp_Object before, after;
21963 int ignore;
21965 before = Foverlay_start (overlay);
21966 after = Foverlay_end (overlay);
21967 /* Record this as the current active region. */
21968 fast_find_position (w, XFASTINT (before),
21969 &dpyinfo->mouse_face_beg_col,
21970 &dpyinfo->mouse_face_beg_row,
21971 &dpyinfo->mouse_face_beg_x,
21972 &dpyinfo->mouse_face_beg_y, Qnil);
21974 dpyinfo->mouse_face_past_end
21975 = !fast_find_position (w, XFASTINT (after),
21976 &dpyinfo->mouse_face_end_col,
21977 &dpyinfo->mouse_face_end_row,
21978 &dpyinfo->mouse_face_end_x,
21979 &dpyinfo->mouse_face_end_y, Qnil);
21980 dpyinfo->mouse_face_window = window;
21982 dpyinfo->mouse_face_face_id
21983 = face_at_buffer_position (w, pos, 0, 0,
21984 &ignore, pos + 1,
21985 !dpyinfo->mouse_face_hidden);
21987 /* Display it as active. */
21988 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21989 cursor = No_Cursor;
21991 /* Handle the text property case. */
21992 else if (!NILP (mouse_face) && BUFFERP (object))
21994 /* Find the range of text around this char that
21995 should be active. */
21996 Lisp_Object before, after, beginning, end;
21997 int ignore;
21999 beginning = Fmarker_position (w->start);
22000 end = make_number (BUF_Z (XBUFFER (object))
22001 - XFASTINT (w->window_end_pos));
22002 before
22003 = Fprevious_single_property_change (make_number (pos + 1),
22004 Qmouse_face,
22005 object, beginning);
22006 after
22007 = Fnext_single_property_change (position, Qmouse_face,
22008 object, end);
22010 /* Record this as the current active region. */
22011 fast_find_position (w, XFASTINT (before),
22012 &dpyinfo->mouse_face_beg_col,
22013 &dpyinfo->mouse_face_beg_row,
22014 &dpyinfo->mouse_face_beg_x,
22015 &dpyinfo->mouse_face_beg_y, Qnil);
22016 dpyinfo->mouse_face_past_end
22017 = !fast_find_position (w, XFASTINT (after),
22018 &dpyinfo->mouse_face_end_col,
22019 &dpyinfo->mouse_face_end_row,
22020 &dpyinfo->mouse_face_end_x,
22021 &dpyinfo->mouse_face_end_y, Qnil);
22022 dpyinfo->mouse_face_window = window;
22024 if (BUFFERP (object))
22025 dpyinfo->mouse_face_face_id
22026 = face_at_buffer_position (w, pos, 0, 0,
22027 &ignore, pos + 1,
22028 !dpyinfo->mouse_face_hidden);
22030 /* Display it as active. */
22031 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22032 cursor = No_Cursor;
22034 else if (!NILP (mouse_face) && STRINGP (object))
22036 Lisp_Object b, e;
22037 int ignore;
22039 b = Fprevious_single_property_change (make_number (pos + 1),
22040 Qmouse_face,
22041 object, Qnil);
22042 e = Fnext_single_property_change (position, Qmouse_face,
22043 object, Qnil);
22044 if (NILP (b))
22045 b = make_number (0);
22046 if (NILP (e))
22047 e = make_number (SCHARS (object) - 1);
22049 fast_find_string_pos (w, XINT (b), object,
22050 &dpyinfo->mouse_face_beg_col,
22051 &dpyinfo->mouse_face_beg_row,
22052 &dpyinfo->mouse_face_beg_x,
22053 &dpyinfo->mouse_face_beg_y, 0);
22054 fast_find_string_pos (w, XINT (e), object,
22055 &dpyinfo->mouse_face_end_col,
22056 &dpyinfo->mouse_face_end_row,
22057 &dpyinfo->mouse_face_end_x,
22058 &dpyinfo->mouse_face_end_y, 1);
22059 dpyinfo->mouse_face_past_end = 0;
22060 dpyinfo->mouse_face_window = window;
22061 dpyinfo->mouse_face_face_id
22062 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22063 glyph->face_id, 1);
22064 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22065 cursor = No_Cursor;
22067 else if (STRINGP (object) && NILP (mouse_face))
22069 /* A string which doesn't have mouse-face, but
22070 the text ``under'' it might have. */
22071 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22072 int start = MATRIX_ROW_START_CHARPOS (r);
22074 pos = string_buffer_position (w, object, start);
22075 if (pos > 0)
22076 mouse_face = get_char_property_and_overlay (make_number (pos),
22077 Qmouse_face,
22078 w->buffer,
22079 &overlay);
22080 if (!NILP (mouse_face) && !NILP (overlay))
22082 Lisp_Object before = Foverlay_start (overlay);
22083 Lisp_Object after = Foverlay_end (overlay);
22084 int ignore;
22086 /* Note that we might not be able to find position
22087 BEFORE in the glyph matrix if the overlay is
22088 entirely covered by a `display' property. In
22089 this case, we overshoot. So let's stop in
22090 the glyph matrix before glyphs for OBJECT. */
22091 fast_find_position (w, XFASTINT (before),
22092 &dpyinfo->mouse_face_beg_col,
22093 &dpyinfo->mouse_face_beg_row,
22094 &dpyinfo->mouse_face_beg_x,
22095 &dpyinfo->mouse_face_beg_y,
22096 object);
22098 dpyinfo->mouse_face_past_end
22099 = !fast_find_position (w, XFASTINT (after),
22100 &dpyinfo->mouse_face_end_col,
22101 &dpyinfo->mouse_face_end_row,
22102 &dpyinfo->mouse_face_end_x,
22103 &dpyinfo->mouse_face_end_y,
22104 Qnil);
22105 dpyinfo->mouse_face_window = window;
22106 dpyinfo->mouse_face_face_id
22107 = face_at_buffer_position (w, pos, 0, 0,
22108 &ignore, pos + 1,
22109 !dpyinfo->mouse_face_hidden);
22111 /* Display it as active. */
22112 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22113 cursor = No_Cursor;
22118 check_help_echo:
22120 /* Look for a `help-echo' property. */
22121 if (NILP (help_echo_string)) {
22122 Lisp_Object help, overlay;
22124 /* Check overlays first. */
22125 help = overlay = Qnil;
22126 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22128 overlay = overlay_vec[i];
22129 help = Foverlay_get (overlay, Qhelp_echo);
22132 if (!NILP (help))
22134 help_echo_string = help;
22135 help_echo_window = window;
22136 help_echo_object = overlay;
22137 help_echo_pos = pos;
22139 else
22141 Lisp_Object object = glyph->object;
22142 int charpos = glyph->charpos;
22144 /* Try text properties. */
22145 if (STRINGP (object)
22146 && charpos >= 0
22147 && charpos < SCHARS (object))
22149 help = Fget_text_property (make_number (charpos),
22150 Qhelp_echo, object);
22151 if (NILP (help))
22153 /* If the string itself doesn't specify a help-echo,
22154 see if the buffer text ``under'' it does. */
22155 struct glyph_row *r
22156 = MATRIX_ROW (w->current_matrix, vpos);
22157 int start = MATRIX_ROW_START_CHARPOS (r);
22158 int pos = string_buffer_position (w, object, start);
22159 if (pos > 0)
22161 help = Fget_char_property (make_number (pos),
22162 Qhelp_echo, w->buffer);
22163 if (!NILP (help))
22165 charpos = pos;
22166 object = w->buffer;
22171 else if (BUFFERP (object)
22172 && charpos >= BEGV
22173 && charpos < ZV)
22174 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22175 object);
22177 if (!NILP (help))
22179 help_echo_string = help;
22180 help_echo_window = window;
22181 help_echo_object = object;
22182 help_echo_pos = charpos;
22187 /* Look for a `pointer' property. */
22188 if (NILP (pointer))
22190 /* Check overlays first. */
22191 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22192 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22194 if (NILP (pointer))
22196 Lisp_Object object = glyph->object;
22197 int charpos = glyph->charpos;
22199 /* Try text properties. */
22200 if (STRINGP (object)
22201 && charpos >= 0
22202 && charpos < SCHARS (object))
22204 pointer = Fget_text_property (make_number (charpos),
22205 Qpointer, object);
22206 if (NILP (pointer))
22208 /* If the string itself doesn't specify a pointer,
22209 see if the buffer text ``under'' it does. */
22210 struct glyph_row *r
22211 = MATRIX_ROW (w->current_matrix, vpos);
22212 int start = MATRIX_ROW_START_CHARPOS (r);
22213 int pos = string_buffer_position (w, object, start);
22214 if (pos > 0)
22215 pointer = Fget_char_property (make_number (pos),
22216 Qpointer, w->buffer);
22219 else if (BUFFERP (object)
22220 && charpos >= BEGV
22221 && charpos < ZV)
22222 pointer = Fget_text_property (make_number (charpos),
22223 Qpointer, object);
22227 BEGV = obegv;
22228 ZV = ozv;
22229 current_buffer = obuf;
22232 set_cursor:
22234 define_frame_cursor1 (f, cursor, pointer);
22238 /* EXPORT for RIF:
22239 Clear any mouse-face on window W. This function is part of the
22240 redisplay interface, and is called from try_window_id and similar
22241 functions to ensure the mouse-highlight is off. */
22243 void
22244 x_clear_window_mouse_face (w)
22245 struct window *w;
22247 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22248 Lisp_Object window;
22250 BLOCK_INPUT;
22251 XSETWINDOW (window, w);
22252 if (EQ (window, dpyinfo->mouse_face_window))
22253 clear_mouse_face (dpyinfo);
22254 UNBLOCK_INPUT;
22258 /* EXPORT:
22259 Just discard the mouse face information for frame F, if any.
22260 This is used when the size of F is changed. */
22262 void
22263 cancel_mouse_face (f)
22264 struct frame *f;
22266 Lisp_Object window;
22267 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22269 window = dpyinfo->mouse_face_window;
22270 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22272 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22273 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22274 dpyinfo->mouse_face_window = Qnil;
22279 #endif /* HAVE_WINDOW_SYSTEM */
22282 /***********************************************************************
22283 Exposure Events
22284 ***********************************************************************/
22286 #ifdef HAVE_WINDOW_SYSTEM
22288 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22289 which intersects rectangle R. R is in window-relative coordinates. */
22291 static void
22292 expose_area (w, row, r, area)
22293 struct window *w;
22294 struct glyph_row *row;
22295 XRectangle *r;
22296 enum glyph_row_area area;
22298 struct glyph *first = row->glyphs[area];
22299 struct glyph *end = row->glyphs[area] + row->used[area];
22300 struct glyph *last;
22301 int first_x, start_x, x;
22303 if (area == TEXT_AREA && row->fill_line_p)
22304 /* If row extends face to end of line write the whole line. */
22305 draw_glyphs (w, 0, row, area,
22306 0, row->used[area],
22307 DRAW_NORMAL_TEXT, 0);
22308 else
22310 /* Set START_X to the window-relative start position for drawing glyphs of
22311 AREA. The first glyph of the text area can be partially visible.
22312 The first glyphs of other areas cannot. */
22313 start_x = window_box_left_offset (w, area);
22314 x = start_x;
22315 if (area == TEXT_AREA)
22316 x += row->x;
22318 /* Find the first glyph that must be redrawn. */
22319 while (first < end
22320 && x + first->pixel_width < r->x)
22322 x += first->pixel_width;
22323 ++first;
22326 /* Find the last one. */
22327 last = first;
22328 first_x = x;
22329 while (last < end
22330 && x < r->x + r->width)
22332 x += last->pixel_width;
22333 ++last;
22336 /* Repaint. */
22337 if (last > first)
22338 draw_glyphs (w, first_x - start_x, row, area,
22339 first - row->glyphs[area], last - row->glyphs[area],
22340 DRAW_NORMAL_TEXT, 0);
22345 /* Redraw the parts of the glyph row ROW on window W intersecting
22346 rectangle R. R is in window-relative coordinates. Value is
22347 non-zero if mouse-face was overwritten. */
22349 static int
22350 expose_line (w, row, r)
22351 struct window *w;
22352 struct glyph_row *row;
22353 XRectangle *r;
22355 xassert (row->enabled_p);
22357 if (row->mode_line_p || w->pseudo_window_p)
22358 draw_glyphs (w, 0, row, TEXT_AREA,
22359 0, row->used[TEXT_AREA],
22360 DRAW_NORMAL_TEXT, 0);
22361 else
22363 if (row->used[LEFT_MARGIN_AREA])
22364 expose_area (w, row, r, LEFT_MARGIN_AREA);
22365 if (row->used[TEXT_AREA])
22366 expose_area (w, row, r, TEXT_AREA);
22367 if (row->used[RIGHT_MARGIN_AREA])
22368 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22369 draw_row_fringe_bitmaps (w, row);
22372 return row->mouse_face_p;
22376 /* Redraw those parts of glyphs rows during expose event handling that
22377 overlap other rows. Redrawing of an exposed line writes over parts
22378 of lines overlapping that exposed line; this function fixes that.
22380 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22381 row in W's current matrix that is exposed and overlaps other rows.
22382 LAST_OVERLAPPING_ROW is the last such row. */
22384 static void
22385 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22386 struct window *w;
22387 struct glyph_row *first_overlapping_row;
22388 struct glyph_row *last_overlapping_row;
22390 struct glyph_row *row;
22392 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22393 if (row->overlapping_p)
22395 xassert (row->enabled_p && !row->mode_line_p);
22397 if (row->used[LEFT_MARGIN_AREA])
22398 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22400 if (row->used[TEXT_AREA])
22401 x_fix_overlapping_area (w, row, TEXT_AREA);
22403 if (row->used[RIGHT_MARGIN_AREA])
22404 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22409 /* Return non-zero if W's cursor intersects rectangle R. */
22411 static int
22412 phys_cursor_in_rect_p (w, r)
22413 struct window *w;
22414 XRectangle *r;
22416 XRectangle cr, result;
22417 struct glyph *cursor_glyph;
22419 cursor_glyph = get_phys_cursor_glyph (w);
22420 if (cursor_glyph)
22422 /* r is relative to W's box, but w->phys_cursor.x is relative
22423 to left edge of W's TEXT area. Adjust it. */
22424 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22425 cr.y = w->phys_cursor.y;
22426 cr.width = cursor_glyph->pixel_width;
22427 cr.height = w->phys_cursor_height;
22428 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22429 I assume the effect is the same -- and this is portable. */
22430 return x_intersect_rectangles (&cr, r, &result);
22432 else
22433 return 0;
22437 /* EXPORT:
22438 Draw a vertical window border to the right of window W if W doesn't
22439 have vertical scroll bars. */
22441 void
22442 x_draw_vertical_border (w)
22443 struct window *w;
22445 /* We could do better, if we knew what type of scroll-bar the adjacent
22446 windows (on either side) have... But we don't :-(
22447 However, I think this works ok. ++KFS 2003-04-25 */
22449 /* Redraw borders between horizontally adjacent windows. Don't
22450 do it for frames with vertical scroll bars because either the
22451 right scroll bar of a window, or the left scroll bar of its
22452 neighbor will suffice as a border. */
22453 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22454 return;
22456 if (!WINDOW_RIGHTMOST_P (w)
22457 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22459 int x0, x1, y0, y1;
22461 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22462 y1 -= 1;
22464 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22465 x1 -= 1;
22467 rif->draw_vertical_window_border (w, x1, y0, y1);
22469 else if (!WINDOW_LEFTMOST_P (w)
22470 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22472 int x0, x1, y0, y1;
22474 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22475 y1 -= 1;
22477 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22478 x0 -= 1;
22480 rif->draw_vertical_window_border (w, x0, y0, y1);
22485 /* Redraw the part of window W intersection rectangle FR. Pixel
22486 coordinates in FR are frame-relative. Call this function with
22487 input blocked. Value is non-zero if the exposure overwrites
22488 mouse-face. */
22490 static int
22491 expose_window (w, fr)
22492 struct window *w;
22493 XRectangle *fr;
22495 struct frame *f = XFRAME (w->frame);
22496 XRectangle wr, r;
22497 int mouse_face_overwritten_p = 0;
22499 /* If window is not yet fully initialized, do nothing. This can
22500 happen when toolkit scroll bars are used and a window is split.
22501 Reconfiguring the scroll bar will generate an expose for a newly
22502 created window. */
22503 if (w->current_matrix == NULL)
22504 return 0;
22506 /* When we're currently updating the window, display and current
22507 matrix usually don't agree. Arrange for a thorough display
22508 later. */
22509 if (w == updated_window)
22511 SET_FRAME_GARBAGED (f);
22512 return 0;
22515 /* Frame-relative pixel rectangle of W. */
22516 wr.x = WINDOW_LEFT_EDGE_X (w);
22517 wr.y = WINDOW_TOP_EDGE_Y (w);
22518 wr.width = WINDOW_TOTAL_WIDTH (w);
22519 wr.height = WINDOW_TOTAL_HEIGHT (w);
22521 if (x_intersect_rectangles (fr, &wr, &r))
22523 int yb = window_text_bottom_y (w);
22524 struct glyph_row *row;
22525 int cursor_cleared_p;
22526 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22528 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22529 r.x, r.y, r.width, r.height));
22531 /* Convert to window coordinates. */
22532 r.x -= WINDOW_LEFT_EDGE_X (w);
22533 r.y -= WINDOW_TOP_EDGE_Y (w);
22535 /* Turn off the cursor. */
22536 if (!w->pseudo_window_p
22537 && phys_cursor_in_rect_p (w, &r))
22539 x_clear_cursor (w);
22540 cursor_cleared_p = 1;
22542 else
22543 cursor_cleared_p = 0;
22545 /* Update lines intersecting rectangle R. */
22546 first_overlapping_row = last_overlapping_row = NULL;
22547 for (row = w->current_matrix->rows;
22548 row->enabled_p;
22549 ++row)
22551 int y0 = row->y;
22552 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22554 if ((y0 >= r.y && y0 < r.y + r.height)
22555 || (y1 > r.y && y1 < r.y + r.height)
22556 || (r.y >= y0 && r.y < y1)
22557 || (r.y + r.height > y0 && r.y + r.height < y1))
22559 /* A header line may be overlapping, but there is no need
22560 to fix overlapping areas for them. KFS 2005-02-12 */
22561 if (row->overlapping_p && !row->mode_line_p)
22563 if (first_overlapping_row == NULL)
22564 first_overlapping_row = row;
22565 last_overlapping_row = row;
22568 if (expose_line (w, row, &r))
22569 mouse_face_overwritten_p = 1;
22572 if (y1 >= yb)
22573 break;
22576 /* Display the mode line if there is one. */
22577 if (WINDOW_WANTS_MODELINE_P (w)
22578 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22579 row->enabled_p)
22580 && row->y < r.y + r.height)
22582 if (expose_line (w, row, &r))
22583 mouse_face_overwritten_p = 1;
22586 if (!w->pseudo_window_p)
22588 /* Fix the display of overlapping rows. */
22589 if (first_overlapping_row)
22590 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22592 /* Draw border between windows. */
22593 x_draw_vertical_border (w);
22595 /* Turn the cursor on again. */
22596 if (cursor_cleared_p)
22597 update_window_cursor (w, 1);
22601 return mouse_face_overwritten_p;
22606 /* Redraw (parts) of all windows in the window tree rooted at W that
22607 intersect R. R contains frame pixel coordinates. Value is
22608 non-zero if the exposure overwrites mouse-face. */
22610 static int
22611 expose_window_tree (w, r)
22612 struct window *w;
22613 XRectangle *r;
22615 struct frame *f = XFRAME (w->frame);
22616 int mouse_face_overwritten_p = 0;
22618 while (w && !FRAME_GARBAGED_P (f))
22620 if (!NILP (w->hchild))
22621 mouse_face_overwritten_p
22622 |= expose_window_tree (XWINDOW (w->hchild), r);
22623 else if (!NILP (w->vchild))
22624 mouse_face_overwritten_p
22625 |= expose_window_tree (XWINDOW (w->vchild), r);
22626 else
22627 mouse_face_overwritten_p |= expose_window (w, r);
22629 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22632 return mouse_face_overwritten_p;
22636 /* EXPORT:
22637 Redisplay an exposed area of frame F. X and Y are the upper-left
22638 corner of the exposed rectangle. W and H are width and height of
22639 the exposed area. All are pixel values. W or H zero means redraw
22640 the entire frame. */
22642 void
22643 expose_frame (f, x, y, w, h)
22644 struct frame *f;
22645 int x, y, w, h;
22647 XRectangle r;
22648 int mouse_face_overwritten_p = 0;
22650 TRACE ((stderr, "expose_frame "));
22652 /* No need to redraw if frame will be redrawn soon. */
22653 if (FRAME_GARBAGED_P (f))
22655 TRACE ((stderr, " garbaged\n"));
22656 return;
22659 /* If basic faces haven't been realized yet, there is no point in
22660 trying to redraw anything. This can happen when we get an expose
22661 event while Emacs is starting, e.g. by moving another window. */
22662 if (FRAME_FACE_CACHE (f) == NULL
22663 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22665 TRACE ((stderr, " no faces\n"));
22666 return;
22669 if (w == 0 || h == 0)
22671 r.x = r.y = 0;
22672 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22673 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22675 else
22677 r.x = x;
22678 r.y = y;
22679 r.width = w;
22680 r.height = h;
22683 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22684 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22686 if (WINDOWP (f->tool_bar_window))
22687 mouse_face_overwritten_p
22688 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22690 #ifdef HAVE_X_WINDOWS
22691 #ifndef MSDOS
22692 #ifndef USE_X_TOOLKIT
22693 if (WINDOWP (f->menu_bar_window))
22694 mouse_face_overwritten_p
22695 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22696 #endif /* not USE_X_TOOLKIT */
22697 #endif
22698 #endif
22700 /* Some window managers support a focus-follows-mouse style with
22701 delayed raising of frames. Imagine a partially obscured frame,
22702 and moving the mouse into partially obscured mouse-face on that
22703 frame. The visible part of the mouse-face will be highlighted,
22704 then the WM raises the obscured frame. With at least one WM, KDE
22705 2.1, Emacs is not getting any event for the raising of the frame
22706 (even tried with SubstructureRedirectMask), only Expose events.
22707 These expose events will draw text normally, i.e. not
22708 highlighted. Which means we must redo the highlight here.
22709 Subsume it under ``we love X''. --gerd 2001-08-15 */
22710 /* Included in Windows version because Windows most likely does not
22711 do the right thing if any third party tool offers
22712 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22713 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22715 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22716 if (f == dpyinfo->mouse_face_mouse_frame)
22718 int x = dpyinfo->mouse_face_mouse_x;
22719 int y = dpyinfo->mouse_face_mouse_y;
22720 clear_mouse_face (dpyinfo);
22721 note_mouse_highlight (f, x, y);
22727 /* EXPORT:
22728 Determine the intersection of two rectangles R1 and R2. Return
22729 the intersection in *RESULT. Value is non-zero if RESULT is not
22730 empty. */
22733 x_intersect_rectangles (r1, r2, result)
22734 XRectangle *r1, *r2, *result;
22736 XRectangle *left, *right;
22737 XRectangle *upper, *lower;
22738 int intersection_p = 0;
22740 /* Rearrange so that R1 is the left-most rectangle. */
22741 if (r1->x < r2->x)
22742 left = r1, right = r2;
22743 else
22744 left = r2, right = r1;
22746 /* X0 of the intersection is right.x0, if this is inside R1,
22747 otherwise there is no intersection. */
22748 if (right->x <= left->x + left->width)
22750 result->x = right->x;
22752 /* The right end of the intersection is the minimum of the
22753 the right ends of left and right. */
22754 result->width = (min (left->x + left->width, right->x + right->width)
22755 - result->x);
22757 /* Same game for Y. */
22758 if (r1->y < r2->y)
22759 upper = r1, lower = r2;
22760 else
22761 upper = r2, lower = r1;
22763 /* The upper end of the intersection is lower.y0, if this is inside
22764 of upper. Otherwise, there is no intersection. */
22765 if (lower->y <= upper->y + upper->height)
22767 result->y = lower->y;
22769 /* The lower end of the intersection is the minimum of the lower
22770 ends of upper and lower. */
22771 result->height = (min (lower->y + lower->height,
22772 upper->y + upper->height)
22773 - result->y);
22774 intersection_p = 1;
22778 return intersection_p;
22781 #endif /* HAVE_WINDOW_SYSTEM */
22784 /***********************************************************************
22785 Initialization
22786 ***********************************************************************/
22788 void
22789 syms_of_xdisp ()
22791 Vwith_echo_area_save_vector = Qnil;
22792 staticpro (&Vwith_echo_area_save_vector);
22794 Vmessage_stack = Qnil;
22795 staticpro (&Vmessage_stack);
22797 Qinhibit_redisplay = intern ("inhibit-redisplay");
22798 staticpro (&Qinhibit_redisplay);
22800 message_dolog_marker1 = Fmake_marker ();
22801 staticpro (&message_dolog_marker1);
22802 message_dolog_marker2 = Fmake_marker ();
22803 staticpro (&message_dolog_marker2);
22804 message_dolog_marker3 = Fmake_marker ();
22805 staticpro (&message_dolog_marker3);
22807 #if GLYPH_DEBUG
22808 defsubr (&Sdump_frame_glyph_matrix);
22809 defsubr (&Sdump_glyph_matrix);
22810 defsubr (&Sdump_glyph_row);
22811 defsubr (&Sdump_tool_bar_row);
22812 defsubr (&Strace_redisplay);
22813 defsubr (&Strace_to_stderr);
22814 #endif
22815 #ifdef HAVE_WINDOW_SYSTEM
22816 defsubr (&Stool_bar_lines_needed);
22817 defsubr (&Slookup_image_map);
22818 #endif
22819 defsubr (&Sformat_mode_line);
22821 staticpro (&Qmenu_bar_update_hook);
22822 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22824 staticpro (&Qoverriding_terminal_local_map);
22825 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22827 staticpro (&Qoverriding_local_map);
22828 Qoverriding_local_map = intern ("overriding-local-map");
22830 staticpro (&Qwindow_scroll_functions);
22831 Qwindow_scroll_functions = intern ("window-scroll-functions");
22833 staticpro (&Qredisplay_end_trigger_functions);
22834 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22836 staticpro (&Qinhibit_point_motion_hooks);
22837 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22839 QCdata = intern (":data");
22840 staticpro (&QCdata);
22841 Qdisplay = intern ("display");
22842 staticpro (&Qdisplay);
22843 Qspace_width = intern ("space-width");
22844 staticpro (&Qspace_width);
22845 Qraise = intern ("raise");
22846 staticpro (&Qraise);
22847 Qslice = intern ("slice");
22848 staticpro (&Qslice);
22849 Qspace = intern ("space");
22850 staticpro (&Qspace);
22851 Qmargin = intern ("margin");
22852 staticpro (&Qmargin);
22853 Qpointer = intern ("pointer");
22854 staticpro (&Qpointer);
22855 Qleft_margin = intern ("left-margin");
22856 staticpro (&Qleft_margin);
22857 Qright_margin = intern ("right-margin");
22858 staticpro (&Qright_margin);
22859 Qcenter = intern ("center");
22860 staticpro (&Qcenter);
22861 Qline_height = intern ("line-height");
22862 staticpro (&Qline_height);
22863 QCalign_to = intern (":align-to");
22864 staticpro (&QCalign_to);
22865 QCrelative_width = intern (":relative-width");
22866 staticpro (&QCrelative_width);
22867 QCrelative_height = intern (":relative-height");
22868 staticpro (&QCrelative_height);
22869 QCeval = intern (":eval");
22870 staticpro (&QCeval);
22871 QCpropertize = intern (":propertize");
22872 staticpro (&QCpropertize);
22873 QCfile = intern (":file");
22874 staticpro (&QCfile);
22875 Qfontified = intern ("fontified");
22876 staticpro (&Qfontified);
22877 Qfontification_functions = intern ("fontification-functions");
22878 staticpro (&Qfontification_functions);
22879 Qtrailing_whitespace = intern ("trailing-whitespace");
22880 staticpro (&Qtrailing_whitespace);
22881 Qescape_glyph = intern ("escape-glyph");
22882 staticpro (&Qescape_glyph);
22883 Qnobreak_space = intern ("nobreak-space");
22884 staticpro (&Qnobreak_space);
22885 Qimage = intern ("image");
22886 staticpro (&Qimage);
22887 QCmap = intern (":map");
22888 staticpro (&QCmap);
22889 QCpointer = intern (":pointer");
22890 staticpro (&QCpointer);
22891 Qrect = intern ("rect");
22892 staticpro (&Qrect);
22893 Qcircle = intern ("circle");
22894 staticpro (&Qcircle);
22895 Qpoly = intern ("poly");
22896 staticpro (&Qpoly);
22897 Qmessage_truncate_lines = intern ("message-truncate-lines");
22898 staticpro (&Qmessage_truncate_lines);
22899 Qgrow_only = intern ("grow-only");
22900 staticpro (&Qgrow_only);
22901 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22902 staticpro (&Qinhibit_menubar_update);
22903 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22904 staticpro (&Qinhibit_eval_during_redisplay);
22905 Qposition = intern ("position");
22906 staticpro (&Qposition);
22907 Qbuffer_position = intern ("buffer-position");
22908 staticpro (&Qbuffer_position);
22909 Qobject = intern ("object");
22910 staticpro (&Qobject);
22911 Qbar = intern ("bar");
22912 staticpro (&Qbar);
22913 Qhbar = intern ("hbar");
22914 staticpro (&Qhbar);
22915 Qbox = intern ("box");
22916 staticpro (&Qbox);
22917 Qhollow = intern ("hollow");
22918 staticpro (&Qhollow);
22919 Qhand = intern ("hand");
22920 staticpro (&Qhand);
22921 Qarrow = intern ("arrow");
22922 staticpro (&Qarrow);
22923 Qtext = intern ("text");
22924 staticpro (&Qtext);
22925 Qrisky_local_variable = intern ("risky-local-variable");
22926 staticpro (&Qrisky_local_variable);
22927 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22928 staticpro (&Qinhibit_free_realized_faces);
22930 list_of_error = Fcons (Fcons (intern ("error"),
22931 Fcons (intern ("void-variable"), Qnil)),
22932 Qnil);
22933 staticpro (&list_of_error);
22935 Qlast_arrow_position = intern ("last-arrow-position");
22936 staticpro (&Qlast_arrow_position);
22937 Qlast_arrow_string = intern ("last-arrow-string");
22938 staticpro (&Qlast_arrow_string);
22940 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22941 staticpro (&Qoverlay_arrow_string);
22942 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22943 staticpro (&Qoverlay_arrow_bitmap);
22945 echo_buffer[0] = echo_buffer[1] = Qnil;
22946 staticpro (&echo_buffer[0]);
22947 staticpro (&echo_buffer[1]);
22949 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22950 staticpro (&echo_area_buffer[0]);
22951 staticpro (&echo_area_buffer[1]);
22953 Vmessages_buffer_name = build_string ("*Messages*");
22954 staticpro (&Vmessages_buffer_name);
22956 mode_line_proptrans_alist = Qnil;
22957 staticpro (&mode_line_proptrans_alist);
22958 mode_line_string_list = Qnil;
22959 staticpro (&mode_line_string_list);
22960 mode_line_string_face = Qnil;
22961 staticpro (&mode_line_string_face);
22962 mode_line_string_face_prop = Qnil;
22963 staticpro (&mode_line_string_face_prop);
22964 Vmode_line_unwind_vector = Qnil;
22965 staticpro (&Vmode_line_unwind_vector);
22967 help_echo_string = Qnil;
22968 staticpro (&help_echo_string);
22969 help_echo_object = Qnil;
22970 staticpro (&help_echo_object);
22971 help_echo_window = Qnil;
22972 staticpro (&help_echo_window);
22973 previous_help_echo_string = Qnil;
22974 staticpro (&previous_help_echo_string);
22975 help_echo_pos = -1;
22977 #ifdef HAVE_WINDOW_SYSTEM
22978 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22979 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22980 For example, if a block cursor is over a tab, it will be drawn as
22981 wide as that tab on the display. */);
22982 x_stretch_cursor_p = 0;
22983 #endif
22985 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22986 doc: /* *Non-nil means highlight trailing whitespace.
22987 The face used for trailing whitespace is `trailing-whitespace'. */);
22988 Vshow_trailing_whitespace = Qnil;
22990 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
22991 doc: /* *Control highlighting of nobreak space and soft hyphen.
22992 A value of t means highlight the character itself (for nobreak space,
22993 use face `nobreak-space').
22994 A value of nil means no highlighting.
22995 Other values mean display the escape glyph followed by an ordinary
22996 space or ordinary hyphen. */);
22997 Vnobreak_char_display = Qt;
22999 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23000 doc: /* *The pointer shape to show in void text areas.
23001 A value of nil means to show the text pointer. Other options are `arrow',
23002 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23003 Vvoid_text_area_pointer = Qarrow;
23005 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23006 doc: /* Non-nil means don't actually do any redisplay.
23007 This is used for internal purposes. */);
23008 Vinhibit_redisplay = Qnil;
23010 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23011 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23012 Vglobal_mode_string = Qnil;
23014 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23015 doc: /* Marker for where to display an arrow on top of the buffer text.
23016 This must be the beginning of a line in order to work.
23017 See also `overlay-arrow-string'. */);
23018 Voverlay_arrow_position = Qnil;
23020 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23021 doc: /* String to display as an arrow in non-window frames.
23022 See also `overlay-arrow-position'. */);
23023 Voverlay_arrow_string = build_string ("=>");
23025 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23026 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23027 The symbols on this list are examined during redisplay to determine
23028 where to display overlay arrows. */);
23029 Voverlay_arrow_variable_list
23030 = Fcons (intern ("overlay-arrow-position"), Qnil);
23032 DEFVAR_INT ("scroll-step", &scroll_step,
23033 doc: /* *The number of lines to try scrolling a window by when point moves out.
23034 If that fails to bring point back on frame, point is centered instead.
23035 If this is zero, point is always centered after it moves off frame.
23036 If you want scrolling to always be a line at a time, you should set
23037 `scroll-conservatively' to a large value rather than set this to 1. */);
23039 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23040 doc: /* *Scroll up to this many lines, to bring point back on screen.
23041 A value of zero means to scroll the text to center point vertically
23042 in the window. */);
23043 scroll_conservatively = 0;
23045 DEFVAR_INT ("scroll-margin", &scroll_margin,
23046 doc: /* *Number of lines of margin at the top and bottom of a window.
23047 Recenter the window whenever point gets within this many lines
23048 of the top or bottom of the window. */);
23049 scroll_margin = 0;
23051 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23052 doc: /* Pixels per inch on current display.
23053 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23054 Vdisplay_pixels_per_inch = make_float (72.0);
23056 #if GLYPH_DEBUG
23057 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23058 #endif
23060 DEFVAR_BOOL ("truncate-partial-width-windows",
23061 &truncate_partial_width_windows,
23062 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23063 truncate_partial_width_windows = 1;
23065 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23066 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23067 Any other value means to use the appropriate face, `mode-line',
23068 `header-line', or `menu' respectively. */);
23069 mode_line_inverse_video = 1;
23071 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23072 doc: /* *Maximum buffer size for which line number should be displayed.
23073 If the buffer is bigger than this, the line number does not appear
23074 in the mode line. A value of nil means no limit. */);
23075 Vline_number_display_limit = Qnil;
23077 DEFVAR_INT ("line-number-display-limit-width",
23078 &line_number_display_limit_width,
23079 doc: /* *Maximum line width (in characters) for line number display.
23080 If the average length of the lines near point is bigger than this, then the
23081 line number may be omitted from the mode line. */);
23082 line_number_display_limit_width = 200;
23084 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23085 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23086 highlight_nonselected_windows = 0;
23088 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23089 doc: /* Non-nil if more than one frame is visible on this display.
23090 Minibuffer-only frames don't count, but iconified frames do.
23091 This variable is not guaranteed to be accurate except while processing
23092 `frame-title-format' and `icon-title-format'. */);
23094 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23095 doc: /* Template for displaying the title bar of visible frames.
23096 \(Assuming the window manager supports this feature.)
23097 This variable has the same structure as `mode-line-format' (which see),
23098 and is used only on frames for which no explicit name has been set
23099 \(see `modify-frame-parameters'). */);
23101 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23102 doc: /* Template for displaying the title bar of an iconified frame.
23103 \(Assuming the window manager supports this feature.)
23104 This variable has the same structure as `mode-line-format' (which see),
23105 and is used only on frames for which no explicit name has been set
23106 \(see `modify-frame-parameters'). */);
23107 Vicon_title_format
23108 = Vframe_title_format
23109 = Fcons (intern ("multiple-frames"),
23110 Fcons (build_string ("%b"),
23111 Fcons (Fcons (empty_string,
23112 Fcons (intern ("invocation-name"),
23113 Fcons (build_string ("@"),
23114 Fcons (intern ("system-name"),
23115 Qnil)))),
23116 Qnil)));
23118 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23119 doc: /* Maximum number of lines to keep in the message log buffer.
23120 If nil, disable message logging. If t, log messages but don't truncate
23121 the buffer when it becomes large. */);
23122 Vmessage_log_max = make_number (50);
23124 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23125 doc: /* Functions called before redisplay, if window sizes have changed.
23126 The value should be a list of functions that take one argument.
23127 Just before redisplay, for each frame, if any of its windows have changed
23128 size since the last redisplay, or have been split or deleted,
23129 all the functions in the list are called, with the frame as argument. */);
23130 Vwindow_size_change_functions = Qnil;
23132 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23133 doc: /* List of functions to call before redisplaying a window with scrolling.
23134 Each function is called with two arguments, the window
23135 and its new display-start position. Note that the value of `window-end'
23136 is not valid when these functions are called. */);
23137 Vwindow_scroll_functions = Qnil;
23139 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23140 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23141 mouse_autoselect_window = 0;
23143 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23144 doc: /* *Non-nil means automatically resize tool-bars.
23145 This increases a tool-bar's height if not all tool-bar items are visible.
23146 It decreases a tool-bar's height when it would display blank lines
23147 otherwise. */);
23148 auto_resize_tool_bars_p = 1;
23150 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23151 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23152 auto_raise_tool_bar_buttons_p = 1;
23154 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23155 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23156 make_cursor_line_fully_visible_p = 1;
23158 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23159 doc: /* *Margin around tool-bar buttons in pixels.
23160 If an integer, use that for both horizontal and vertical margins.
23161 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23162 HORZ specifying the horizontal margin, and VERT specifying the
23163 vertical margin. */);
23164 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23166 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23167 doc: /* *Relief thickness of tool-bar buttons. */);
23168 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23170 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23171 doc: /* List of functions to call to fontify regions of text.
23172 Each function is called with one argument POS. Functions must
23173 fontify a region starting at POS in the current buffer, and give
23174 fontified regions the property `fontified'. */);
23175 Vfontification_functions = Qnil;
23176 Fmake_variable_buffer_local (Qfontification_functions);
23178 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23179 &unibyte_display_via_language_environment,
23180 doc: /* *Non-nil means display unibyte text according to language environment.
23181 Specifically this means that unibyte non-ASCII characters
23182 are displayed by converting them to the equivalent multibyte characters
23183 according to the current language environment. As a result, they are
23184 displayed according to the current fontset. */);
23185 unibyte_display_via_language_environment = 0;
23187 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23188 doc: /* *Maximum height for resizing mini-windows.
23189 If a float, it specifies a fraction of the mini-window frame's height.
23190 If an integer, it specifies a number of lines. */);
23191 Vmax_mini_window_height = make_float (0.25);
23193 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23194 doc: /* *How to resize mini-windows.
23195 A value of nil means don't automatically resize mini-windows.
23196 A value of t means resize them to fit the text displayed in them.
23197 A value of `grow-only', the default, means let mini-windows grow
23198 only, until their display becomes empty, at which point the windows
23199 go back to their normal size. */);
23200 Vresize_mini_windows = Qgrow_only;
23202 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23203 doc: /* Alist specifying how to blink the cursor off.
23204 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23205 `cursor-type' frame-parameter or variable equals ON-STATE,
23206 comparing using `equal', Emacs uses OFF-STATE to specify
23207 how to blink it off. */);
23208 Vblink_cursor_alist = Qnil;
23210 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23211 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23212 automatic_hscrolling_p = 1;
23214 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23215 doc: /* *How many columns away from the window edge point is allowed to get
23216 before automatic hscrolling will horizontally scroll the window. */);
23217 hscroll_margin = 5;
23219 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23220 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23221 When point is less than `automatic-hscroll-margin' columns from the window
23222 edge, automatic hscrolling will scroll the window by the amount of columns
23223 determined by this variable. If its value is a positive integer, scroll that
23224 many columns. If it's a positive floating-point number, it specifies the
23225 fraction of the window's width to scroll. If it's nil or zero, point will be
23226 centered horizontally after the scroll. Any other value, including negative
23227 numbers, are treated as if the value were zero.
23229 Automatic hscrolling always moves point outside the scroll margin, so if
23230 point was more than scroll step columns inside the margin, the window will
23231 scroll more than the value given by the scroll step.
23233 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23234 and `scroll-right' overrides this variable's effect. */);
23235 Vhscroll_step = make_number (0);
23237 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23238 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23239 Bind this around calls to `message' to let it take effect. */);
23240 message_truncate_lines = 0;
23242 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23243 doc: /* Normal hook run to update the menu bar definitions.
23244 Redisplay runs this hook before it redisplays the menu bar.
23245 This is used to update submenus such as Buffers,
23246 whose contents depend on various data. */);
23247 Vmenu_bar_update_hook = Qnil;
23249 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23250 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23251 inhibit_menubar_update = 0;
23253 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23254 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23255 inhibit_eval_during_redisplay = 0;
23257 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23258 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23259 inhibit_free_realized_faces = 0;
23261 #if GLYPH_DEBUG
23262 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23263 doc: /* Inhibit try_window_id display optimization. */);
23264 inhibit_try_window_id = 0;
23266 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23267 doc: /* Inhibit try_window_reusing display optimization. */);
23268 inhibit_try_window_reusing = 0;
23270 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23271 doc: /* Inhibit try_cursor_movement display optimization. */);
23272 inhibit_try_cursor_movement = 0;
23273 #endif /* GLYPH_DEBUG */
23277 /* Initialize this module when Emacs starts. */
23279 void
23280 init_xdisp ()
23282 Lisp_Object root_window;
23283 struct window *mini_w;
23285 current_header_line_height = current_mode_line_height = -1;
23287 CHARPOS (this_line_start_pos) = 0;
23289 mini_w = XWINDOW (minibuf_window);
23290 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23292 if (!noninteractive)
23294 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23295 int i;
23297 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23298 set_window_height (root_window,
23299 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23301 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23302 set_window_height (minibuf_window, 1, 0);
23304 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23305 mini_w->total_cols = make_number (FRAME_COLS (f));
23307 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23308 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23309 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23311 /* The default ellipsis glyphs `...'. */
23312 for (i = 0; i < 3; ++i)
23313 default_invis_vector[i] = make_number ('.');
23317 /* Allocate the buffer for frame titles.
23318 Also used for `format-mode-line'. */
23319 int size = 100;
23320 mode_line_noprop_buf = (char *) xmalloc (size);
23321 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23322 mode_line_noprop_ptr = mode_line_noprop_buf;
23323 mode_line_target = MODE_LINE_DISPLAY;
23326 help_echo_showing_p = 0;
23330 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23331 (do not change this comment) */