(url-open-stream): Really use asynchronous connections (accidentally
[emacs.git] / src / xdisp.c
blobb85fdb4d71e0ed814135b5f4612edb9864ad2f73
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
289 int auto_resize_tool_bars_p;
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
295 int x_stretch_cursor_p;
297 /* Non-nil means don't actually do any redisplay. */
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
303 int inhibit_eval_during_redisplay;
305 /* Names of text properties relevant for redisplay. */
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
310 /* Symbols used in text property values. */
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
324 /* Non-nil means highlight trailing whitespace. */
326 Lisp_Object Vshow_trailing_whitespace;
328 /* Non-nil means escape non-break space and hyphens. */
330 Lisp_Object Vnobreak_char_display;
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
344 #endif /* HAVE_WINDOW_SYSTEM */
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
350 Lisp_Object Vvoid_text_area_pointer;
352 /* Name of the face used to highlight trailing whitespace. */
354 Lisp_Object Qtrailing_whitespace;
356 /* Name and number of the face used to highlight escape glyphs. */
358 Lisp_Object Qescape_glyph;
360 /* Name and number of the face used to highlight non-breaking spaces. */
362 Lisp_Object Qnobreak_space;
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
367 Lisp_Object Qimage;
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
376 int noninteractive_need_newline;
378 /* Non-zero means print newline to message log before next message. */
380 static int message_log_need_newline;
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
394 static struct text_pos this_line_start_pos;
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
399 static struct text_pos this_line_end_pos;
401 /* The vertical positions and the height of this line. */
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
410 static int this_line_start_x;
412 /* Buffer that this_line_.* variables are referring to. */
414 static struct buffer *this_line_buffer;
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
419 int truncate_partial_width_windows;
421 /* A flag to control how to display unibyte 8-bit character. */
423 int unibyte_display_via_language_environment;
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
429 int multiple_frames;
431 Lisp_Object Vglobal_mode_string;
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
438 Lisp_Object Voverlay_arrow_variable_list;
440 /* Marker for where to display an arrow on top of the buffer text. */
442 Lisp_Object Voverlay_arrow_position;
444 /* String to display for the arrow. Only used on terminal frames. */
446 Lisp_Object Voverlay_arrow_string;
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
460 /* Like mode-line-format, but for the title bar on a visible frame. */
462 Lisp_Object Vframe_title_format;
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
466 Lisp_Object Vicon_title_format;
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
472 static Lisp_Object Vwindow_size_change_functions;
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
476 /* Nonzero if an overlay arrow has been displayed in this window. */
478 static int overlay_arrow_seen;
480 /* Nonzero means highlight the region even in nonselected windows. */
482 int highlight_nonselected_windows;
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
487 static EMACS_INT scroll_step;
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
492 static EMACS_INT scroll_conservatively;
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
499 EMACS_INT scroll_margin;
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
505 int buffer_shared;
507 /* Vector containing glyphs for an ellipsis `...'. */
509 static Lisp_Object default_invis_vector[3];
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
515 This variable is deprecated. */
517 int mode_line_inverse_video;
519 /* Prompt to display in front of the mini-buffer contents. */
521 Lisp_Object minibuf_prompt;
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
526 int minibuf_prompt_width;
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
532 Lisp_Object echo_area_window;
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
539 Lisp_Object Vmessage_stack;
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
544 int message_enable_multibyte;
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548 int update_mode_lines;
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
553 int windows_or_buffers_changed;
555 /* Nonzero means a frame's cursor type has been changed. */
557 int cursor_type_changed;
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
562 int line_number_displayed;
564 /* Maximum buffer size for which to display line numbers. */
566 Lisp_Object Vline_number_display_limit;
568 /* Line width to consider when repositioning for line number display. */
570 static EMACS_INT line_number_display_limit_width;
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
575 Lisp_Object Vmessage_log_max;
577 /* The name of the *Messages* buffer, a string. */
579 static Lisp_Object Vmessages_buffer_name;
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
590 Lisp_Object echo_area_buffer[2];
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
597 static Lisp_Object echo_buffer[2];
599 /* A vector saved used in with_area_buffer to reduce consing. */
601 static Lisp_Object Vwith_echo_area_save_vector;
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
606 static int display_last_displayed_message_p;
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
611 int message_buf_print;
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
618 /* When evaluating expressions from menu bar items (enable conditions,
619 for instance), this is the frame they are being processed for. */
621 Lisp_Object Vmenu_updating_frame;
623 /* Maximum height for resizing mini-windows. Either a float
624 specifying a fraction of the available height, or an integer
625 specifying a number of lines. */
627 Lisp_Object Vmax_mini_window_height;
629 /* Non-zero means messages should be displayed with truncated
630 lines instead of being continued. */
632 int message_truncate_lines;
633 Lisp_Object Qmessage_truncate_lines;
635 /* Set to 1 in clear_message to make redisplay_internal aware
636 of an emptied echo area. */
638 static int message_cleared_p;
640 /* How to blink the default frame cursor off. */
641 Lisp_Object Vblink_cursor_alist;
643 /* A scratch glyph row with contents used for generating truncation
644 glyphs. Also used in direct_output_for_insert. */
646 #define MAX_SCRATCH_GLYPHS 100
647 struct glyph_row scratch_glyph_row;
648 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
650 /* Ascent and height of the last line processed by move_it_to. */
652 static int last_max_ascent, last_height;
654 /* Non-zero if there's a help-echo in the echo area. */
656 int help_echo_showing_p;
658 /* If >= 0, computed, exact values of mode-line and header-line height
659 to use in the macros CURRENT_MODE_LINE_HEIGHT and
660 CURRENT_HEADER_LINE_HEIGHT. */
662 int current_mode_line_height, current_header_line_height;
664 /* The maximum distance to look ahead for text properties. Values
665 that are too small let us call compute_char_face and similar
666 functions too often which is expensive. Values that are too large
667 let us call compute_char_face and alike too often because we
668 might not be interested in text properties that far away. */
670 #define TEXT_PROP_DISTANCE_LIMIT 100
672 #if GLYPH_DEBUG
674 /* Variables to turn off display optimizations from Lisp. */
676 int inhibit_try_window_id, inhibit_try_window_reusing;
677 int inhibit_try_cursor_movement;
679 /* Non-zero means print traces of redisplay if compiled with
680 GLYPH_DEBUG != 0. */
682 int trace_redisplay_p;
684 #endif /* GLYPH_DEBUG */
686 #ifdef DEBUG_TRACE_MOVE
687 /* Non-zero means trace with TRACE_MOVE to stderr. */
688 int trace_move;
690 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
691 #else
692 #define TRACE_MOVE(x) (void) 0
693 #endif
695 /* Non-zero means automatically scroll windows horizontally to make
696 point visible. */
698 int automatic_hscrolling_p;
700 /* How close to the margin can point get before the window is scrolled
701 horizontally. */
702 EMACS_INT hscroll_margin;
704 /* How much to scroll horizontally when point is inside the above margin. */
705 Lisp_Object Vhscroll_step;
707 /* The variable `resize-mini-windows'. If nil, don't resize
708 mini-windows. If t, always resize them to fit the text they
709 display. If `grow-only', let mini-windows grow only until they
710 become empty. */
712 Lisp_Object Vresize_mini_windows;
714 /* Buffer being redisplayed -- for redisplay_window_error. */
716 struct buffer *displayed_buffer;
718 /* Space between overline and text. */
720 EMACS_INT overline_margin;
722 /* Value returned from text property handlers (see below). */
724 enum prop_handled
726 HANDLED_NORMALLY,
727 HANDLED_RECOMPUTE_PROPS,
728 HANDLED_OVERLAY_STRING_CONSUMED,
729 HANDLED_RETURN
732 /* A description of text properties that redisplay is interested
733 in. */
735 struct props
737 /* The name of the property. */
738 Lisp_Object *name;
740 /* A unique index for the property. */
741 enum prop_idx idx;
743 /* A handler function called to set up iterator IT from the property
744 at IT's current position. Value is used to steer handle_stop. */
745 enum prop_handled (*handler) P_ ((struct it *it));
748 static enum prop_handled handle_face_prop P_ ((struct it *));
749 static enum prop_handled handle_invisible_prop P_ ((struct it *));
750 static enum prop_handled handle_display_prop P_ ((struct it *));
751 static enum prop_handled handle_composition_prop P_ ((struct it *));
752 static enum prop_handled handle_overlay_change P_ ((struct it *));
753 static enum prop_handled handle_fontified_prop P_ ((struct it *));
755 /* Properties handled by iterators. */
757 static struct props it_props[] =
759 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
760 /* Handle `face' before `display' because some sub-properties of
761 `display' need to know the face. */
762 {&Qface, FACE_PROP_IDX, handle_face_prop},
763 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
764 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
765 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
766 {NULL, 0, NULL}
769 /* Value is the position described by X. If X is a marker, value is
770 the marker_position of X. Otherwise, value is X. */
772 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
774 /* Enumeration returned by some move_it_.* functions internally. */
776 enum move_it_result
778 /* Not used. Undefined value. */
779 MOVE_UNDEFINED,
781 /* Move ended at the requested buffer position or ZV. */
782 MOVE_POS_MATCH_OR_ZV,
784 /* Move ended at the requested X pixel position. */
785 MOVE_X_REACHED,
787 /* Move within a line ended at the end of a line that must be
788 continued. */
789 MOVE_LINE_CONTINUED,
791 /* Move within a line ended at the end of a line that would
792 be displayed truncated. */
793 MOVE_LINE_TRUNCATED,
795 /* Move within a line ended at a line end. */
796 MOVE_NEWLINE_OR_CR
799 /* This counter is used to clear the face cache every once in a while
800 in redisplay_internal. It is incremented for each redisplay.
801 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
802 cleared. */
804 #define CLEAR_FACE_CACHE_COUNT 500
805 static int clear_face_cache_count;
807 /* Similarly for the image cache. */
809 #ifdef HAVE_WINDOW_SYSTEM
810 #define CLEAR_IMAGE_CACHE_COUNT 101
811 static int clear_image_cache_count;
812 #endif
814 /* Record the previous terminal frame we displayed. */
816 static struct frame *previous_terminal_frame;
818 /* Non-zero while redisplay_internal is in progress. */
820 int redisplaying_p;
822 /* Non-zero means don't free realized faces. Bound while freeing
823 realized faces is dangerous because glyph matrices might still
824 reference them. */
826 int inhibit_free_realized_faces;
827 Lisp_Object Qinhibit_free_realized_faces;
829 /* If a string, XTread_socket generates an event to display that string.
830 (The display is done in read_char.) */
832 Lisp_Object help_echo_string;
833 Lisp_Object help_echo_window;
834 Lisp_Object help_echo_object;
835 int help_echo_pos;
837 /* Temporary variable for XTread_socket. */
839 Lisp_Object previous_help_echo_string;
841 /* Null glyph slice */
843 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
846 /* Function prototypes. */
848 static void setup_for_ellipsis P_ ((struct it *, int));
849 static void mark_window_display_accurate_1 P_ ((struct window *, int));
850 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
851 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
852 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
853 static int redisplay_mode_lines P_ ((Lisp_Object, int));
854 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
856 #if 0
857 static int invisible_text_between_p P_ ((struct it *, int, int));
858 #endif
860 static void pint2str P_ ((char *, int, int));
861 static void pint2hrstr P_ ((char *, int, int));
862 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
863 struct text_pos));
864 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
865 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
866 static void store_mode_line_noprop_char P_ ((char));
867 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
868 static void x_consider_frame_title P_ ((Lisp_Object));
869 static void handle_stop P_ ((struct it *));
870 static int tool_bar_lines_needed P_ ((struct frame *, int *));
871 static int single_display_spec_intangible_p P_ ((Lisp_Object));
872 static void ensure_echo_area_buffers P_ ((void));
873 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
874 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
875 static int with_echo_area_buffer P_ ((struct window *, int,
876 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
877 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static void clear_garbaged_frames P_ ((void));
879 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
881 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int display_echo_area P_ ((struct window *));
883 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
885 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
886 static int string_char_and_length P_ ((const unsigned char *, int, int *));
887 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
888 struct text_pos));
889 static int compute_window_start_on_continuation_line P_ ((struct window *));
890 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
891 static void insert_left_trunc_glyphs P_ ((struct it *));
892 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
893 Lisp_Object));
894 static void extend_face_to_end_of_line P_ ((struct it *));
895 static int append_space_for_newline P_ ((struct it *, int));
896 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
897 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
898 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
899 static int trailing_whitespace_p P_ ((int));
900 static int message_log_check_duplicate P_ ((int, int, int, int));
901 static void push_it P_ ((struct it *));
902 static void pop_it P_ ((struct it *));
903 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
904 static void select_frame_for_redisplay P_ ((Lisp_Object));
905 static void redisplay_internal P_ ((int));
906 static int echo_area_display P_ ((int));
907 static void redisplay_windows P_ ((Lisp_Object));
908 static void redisplay_window P_ ((Lisp_Object, int));
909 static Lisp_Object redisplay_window_error ();
910 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
911 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
912 static int update_menu_bar P_ ((struct frame *, int, int));
913 static int try_window_reusing_current_matrix P_ ((struct window *));
914 static int try_window_id P_ ((struct window *));
915 static int display_line P_ ((struct it *));
916 static int display_mode_lines P_ ((struct window *));
917 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
918 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
919 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
920 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
921 static void display_menu_bar P_ ((struct window *));
922 static int display_count_lines P_ ((int, int, int, int, int *));
923 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
924 int, int, struct it *, int, int, int, int));
925 static void compute_line_metrics P_ ((struct it *));
926 static void run_redisplay_end_trigger_hook P_ ((struct it *));
927 static int get_overlay_strings P_ ((struct it *, int));
928 static int get_overlay_strings_1 P_ ((struct it *, int, int));
929 static void next_overlay_string P_ ((struct it *));
930 static void reseat P_ ((struct it *, struct text_pos, int));
931 static void reseat_1 P_ ((struct it *, struct text_pos, int));
932 static void back_to_previous_visible_line_start P_ ((struct it *));
933 void reseat_at_previous_visible_line_start P_ ((struct it *));
934 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
935 static int next_element_from_ellipsis P_ ((struct it *));
936 static int next_element_from_display_vector P_ ((struct it *));
937 static int next_element_from_string P_ ((struct it *));
938 static int next_element_from_c_string P_ ((struct it *));
939 static int next_element_from_buffer P_ ((struct it *));
940 static int next_element_from_composition P_ ((struct it *));
941 static int next_element_from_image P_ ((struct it *));
942 static int next_element_from_stretch P_ ((struct it *));
943 static void load_overlay_strings P_ ((struct it *, int));
944 static int init_from_display_pos P_ ((struct it *, struct window *,
945 struct display_pos *));
946 static void reseat_to_string P_ ((struct it *, unsigned char *,
947 Lisp_Object, int, int, int, int));
948 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
949 int, int, int));
950 void move_it_vertically_backward P_ ((struct it *, int));
951 static void init_to_row_start P_ ((struct it *, struct window *,
952 struct glyph_row *));
953 static int init_to_row_end P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static void back_to_previous_line_start P_ ((struct it *));
956 static int forward_to_next_line_start P_ ((struct it *, int *));
957 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
958 Lisp_Object, int));
959 static struct text_pos string_pos P_ ((int, Lisp_Object));
960 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
961 static int number_of_chars P_ ((unsigned char *, int));
962 static void compute_stop_pos P_ ((struct it *));
963 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
964 Lisp_Object));
965 static int face_before_or_after_it_pos P_ ((struct it *, int));
966 static int next_overlay_change P_ ((int));
967 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
968 Lisp_Object, struct text_pos *,
969 int));
970 static int underlying_face_id P_ ((struct it *));
971 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
972 struct window *));
974 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
975 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
977 #ifdef HAVE_WINDOW_SYSTEM
979 static void update_tool_bar P_ ((struct frame *, int));
980 static void build_desired_tool_bar_string P_ ((struct frame *f));
981 static int redisplay_tool_bar P_ ((struct frame *));
982 static void display_tool_bar_line P_ ((struct it *, int));
983 static void notice_overwritten_cursor P_ ((struct window *,
984 enum glyph_row_area,
985 int, int, int, int));
989 #endif /* HAVE_WINDOW_SYSTEM */
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
1000 This is the height of W minus the height of a mode line, if any. */
1002 INLINE int
1003 window_text_bottom_y (w)
1004 struct window *w;
1006 int height = WINDOW_TOTAL_HEIGHT (w);
1008 if (WINDOW_WANTS_MODELINE_P (w))
1009 height -= CURRENT_MODE_LINE_HEIGHT (w);
1010 return height;
1013 /* Return the pixel width of display area AREA of window W. AREA < 0
1014 means return the total width of W, not including fringes to
1015 the left and right of the window. */
1017 INLINE int
1018 window_box_width (w, area)
1019 struct window *w;
1020 int area;
1022 int cols = XFASTINT (w->total_cols);
1023 int pixels = 0;
1025 if (!w->pseudo_window_p)
1027 cols -= WINDOW_SCROLL_BAR_COLS (w);
1029 if (area == TEXT_AREA)
1031 if (INTEGERP (w->left_margin_cols))
1032 cols -= XFASTINT (w->left_margin_cols);
1033 if (INTEGERP (w->right_margin_cols))
1034 cols -= XFASTINT (w->right_margin_cols);
1035 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1037 else if (area == LEFT_MARGIN_AREA)
1039 cols = (INTEGERP (w->left_margin_cols)
1040 ? XFASTINT (w->left_margin_cols) : 0);
1041 pixels = 0;
1043 else if (area == RIGHT_MARGIN_AREA)
1045 cols = (INTEGERP (w->right_margin_cols)
1046 ? XFASTINT (w->right_margin_cols) : 0);
1047 pixels = 0;
1051 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1055 /* Return the pixel height of the display area of window W, not
1056 including mode lines of W, if any. */
1058 INLINE int
1059 window_box_height (w)
1060 struct window *w;
1062 struct frame *f = XFRAME (w->frame);
1063 int height = WINDOW_TOTAL_HEIGHT (w);
1065 xassert (height >= 0);
1067 /* Note: the code below that determines the mode-line/header-line
1068 height is essentially the same as that contained in the macro
1069 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1070 the appropriate glyph row has its `mode_line_p' flag set,
1071 and if it doesn't, uses estimate_mode_line_height instead. */
1073 if (WINDOW_WANTS_MODELINE_P (w))
1075 struct glyph_row *ml_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (ml_row && ml_row->mode_line_p)
1080 height -= ml_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1085 if (WINDOW_WANTS_HEADER_LINE_P (w))
1087 struct glyph_row *hl_row
1088 = (w->current_matrix && w->current_matrix->rows
1089 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1090 : 0);
1091 if (hl_row && hl_row->mode_line_p)
1092 height -= hl_row->height;
1093 else
1094 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1097 /* With a very small font and a mode-line that's taller than
1098 default, we might end up with a negative height. */
1099 return max (0, height);
1102 /* Return the window-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1106 INLINE int
1107 window_box_left_offset (w, area)
1108 struct window *w;
1109 int area;
1111 int x;
1113 if (w->pseudo_window_p)
1114 return 0;
1116 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1118 if (area == TEXT_AREA)
1119 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1120 + window_box_width (w, LEFT_MARGIN_AREA));
1121 else if (area == RIGHT_MARGIN_AREA)
1122 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1123 + window_box_width (w, LEFT_MARGIN_AREA)
1124 + window_box_width (w, TEXT_AREA)
1125 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1127 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1128 else if (area == LEFT_MARGIN_AREA
1129 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1130 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1132 return x;
1136 /* Return the window-relative coordinate of the right edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the left of the right fringe of W. */
1140 INLINE int
1141 window_box_right_offset (w, area)
1142 struct window *w;
1143 int area;
1145 return window_box_left_offset (w, area) + window_box_width (w, area);
1148 /* Return the frame-relative coordinate of the left edge of display
1149 area AREA of window W. AREA < 0 means return the left edge of the
1150 whole window, to the right of the left fringe of W. */
1152 INLINE int
1153 window_box_left (w, area)
1154 struct window *w;
1155 int area;
1157 struct frame *f = XFRAME (w->frame);
1158 int x;
1160 if (w->pseudo_window_p)
1161 return FRAME_INTERNAL_BORDER_WIDTH (f);
1163 x = (WINDOW_LEFT_EDGE_X (w)
1164 + window_box_left_offset (w, area));
1166 return x;
1170 /* Return the frame-relative coordinate of the right edge of display
1171 area AREA of window W. AREA < 0 means return the left edge of the
1172 whole window, to the left of the right fringe of W. */
1174 INLINE int
1175 window_box_right (w, area)
1176 struct window *w;
1177 int area;
1179 return window_box_left (w, area) + window_box_width (w, area);
1182 /* Get the bounding box of the display area AREA of window W, without
1183 mode lines, in frame-relative coordinates. AREA < 0 means the
1184 whole window, not including the left and right fringes of
1185 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1186 coordinates of the upper-left corner of the box. Return in
1187 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1189 INLINE void
1190 window_box (w, area, box_x, box_y, box_width, box_height)
1191 struct window *w;
1192 int area;
1193 int *box_x, *box_y, *box_width, *box_height;
1195 if (box_width)
1196 *box_width = window_box_width (w, area);
1197 if (box_height)
1198 *box_height = window_box_height (w);
1199 if (box_x)
1200 *box_x = window_box_left (w, area);
1201 if (box_y)
1203 *box_y = WINDOW_TOP_EDGE_Y (w);
1204 if (WINDOW_WANTS_HEADER_LINE_P (w))
1205 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1210 /* Get the bounding box of the display area AREA of window W, without
1211 mode lines. AREA < 0 means the whole window, not including the
1212 left and right fringe of the window. Return in *TOP_LEFT_X
1213 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1214 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1215 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1216 box. */
1218 INLINE void
1219 window_box_edges (w, area, top_left_x, top_left_y,
1220 bottom_right_x, bottom_right_y)
1221 struct window *w;
1222 int area;
1223 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1225 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1226 bottom_right_y);
1227 *bottom_right_x += *top_left_x;
1228 *bottom_right_y += *top_left_y;
1233 /***********************************************************************
1234 Utilities
1235 ***********************************************************************/
1237 /* Return the bottom y-position of the line the iterator IT is in.
1238 This can modify IT's settings. */
1241 line_bottom_y (it)
1242 struct it *it;
1244 int line_height = it->max_ascent + it->max_descent;
1245 int line_top_y = it->current_y;
1247 if (line_height == 0)
1249 if (last_height)
1250 line_height = last_height;
1251 else if (IT_CHARPOS (*it) < ZV)
1253 move_it_by_lines (it, 1, 1);
1254 line_height = (it->max_ascent || it->max_descent
1255 ? it->max_ascent + it->max_descent
1256 : last_height);
1258 else
1260 struct glyph_row *row = it->glyph_row;
1262 /* Use the default character height. */
1263 it->glyph_row = NULL;
1264 it->what = IT_CHARACTER;
1265 it->c = ' ';
1266 it->len = 1;
1267 PRODUCE_GLYPHS (it);
1268 line_height = it->ascent + it->descent;
1269 it->glyph_row = row;
1273 return line_top_y + line_height;
1277 /* Return 1 if position CHARPOS is visible in window W.
1278 CHARPOS < 0 means return info about WINDOW_END position.
1279 If visible, set *X and *Y to pixel coordinates of top left corner.
1280 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1281 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1284 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1285 struct window *w;
1286 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1288 struct it it;
1289 struct text_pos top;
1290 int visible_p = 0;
1291 struct buffer *old_buffer = NULL;
1293 if (noninteractive)
1294 return visible_p;
1296 if (XBUFFER (w->buffer) != current_buffer)
1298 old_buffer = current_buffer;
1299 set_buffer_internal_1 (XBUFFER (w->buffer));
1302 SET_TEXT_POS_FROM_MARKER (top, w->start);
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 current_buffer->mode_line_format);
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 current_buffer->header_line_format);
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1319 /* Note that we may overshoot because of invisible text. */
1320 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1322 int top_x = it.current_x;
1323 int top_y = it.current_y;
1324 int bottom_y = (last_height = 0, line_bottom_y (&it));
1325 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1327 if (top_y < window_top_y)
1328 visible_p = bottom_y > window_top_y;
1329 else if (top_y < it.last_visible_y)
1330 visible_p = 1;
1331 if (visible_p)
1333 *x = top_x;
1334 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1335 *rtop = max (0, window_top_y - top_y);
1336 *rbot = max (0, bottom_y - it.last_visible_y);
1337 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1338 - max (top_y, window_top_y)));
1339 *vpos = it.vpos;
1342 else
1344 struct it it2;
1346 it2 = it;
1347 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1348 move_it_by_lines (&it, 1, 0);
1349 if (charpos < IT_CHARPOS (it)
1350 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1352 visible_p = 1;
1353 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1354 *x = it2.current_x;
1355 *y = it2.current_y + it2.max_ascent - it2.ascent;
1356 *rtop = max (0, -it2.current_y);
1357 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1358 - it.last_visible_y));
1359 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1360 it.last_visible_y)
1361 - max (it2.current_y,
1362 WINDOW_HEADER_LINE_HEIGHT (w))));
1363 *vpos = it2.vpos;
1367 if (old_buffer)
1368 set_buffer_internal_1 (old_buffer);
1370 current_header_line_height = current_mode_line_height = -1;
1372 if (visible_p && XFASTINT (w->hscroll) > 0)
1373 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1375 #if 0
1376 /* Debugging code. */
1377 if (visible_p)
1378 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1379 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1380 else
1381 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1382 #endif
1384 return visible_p;
1388 /* Return the next character from STR which is MAXLEN bytes long.
1389 Return in *LEN the length of the character. This is like
1390 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1391 we find one, we return a `?', but with the length of the invalid
1392 character. */
1394 static INLINE int
1395 string_char_and_length (str, maxlen, len)
1396 const unsigned char *str;
1397 int maxlen, *len;
1399 int c;
1401 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1402 if (!CHAR_VALID_P (c, 1))
1403 /* We may not change the length here because other places in Emacs
1404 don't use this function, i.e. they silently accept invalid
1405 characters. */
1406 c = '?';
1408 return c;
1413 /* Given a position POS containing a valid character and byte position
1414 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1416 static struct text_pos
1417 string_pos_nchars_ahead (pos, string, nchars)
1418 struct text_pos pos;
1419 Lisp_Object string;
1420 int nchars;
1422 xassert (STRINGP (string) && nchars >= 0);
1424 if (STRING_MULTIBYTE (string))
1426 int rest = SBYTES (string) - BYTEPOS (pos);
1427 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1428 int len;
1430 while (nchars--)
1432 string_char_and_length (p, rest, &len);
1433 p += len, rest -= len;
1434 xassert (rest >= 0);
1435 CHARPOS (pos) += 1;
1436 BYTEPOS (pos) += len;
1439 else
1440 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1442 return pos;
1446 /* Value is the text position, i.e. character and byte position,
1447 for character position CHARPOS in STRING. */
1449 static INLINE struct text_pos
1450 string_pos (charpos, string)
1451 int charpos;
1452 Lisp_Object string;
1454 struct text_pos pos;
1455 xassert (STRINGP (string));
1456 xassert (charpos >= 0);
1457 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1458 return pos;
1462 /* Value is a text position, i.e. character and byte position, for
1463 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1464 means recognize multibyte characters. */
1466 static struct text_pos
1467 c_string_pos (charpos, s, multibyte_p)
1468 int charpos;
1469 unsigned char *s;
1470 int multibyte_p;
1472 struct text_pos pos;
1474 xassert (s != NULL);
1475 xassert (charpos >= 0);
1477 if (multibyte_p)
1479 int rest = strlen (s), len;
1481 SET_TEXT_POS (pos, 0, 0);
1482 while (charpos--)
1484 string_char_and_length (s, rest, &len);
1485 s += len, rest -= len;
1486 xassert (rest >= 0);
1487 CHARPOS (pos) += 1;
1488 BYTEPOS (pos) += len;
1491 else
1492 SET_TEXT_POS (pos, charpos, charpos);
1494 return pos;
1498 /* Value is the number of characters in C string S. MULTIBYTE_P
1499 non-zero means recognize multibyte characters. */
1501 static int
1502 number_of_chars (s, multibyte_p)
1503 unsigned char *s;
1504 int multibyte_p;
1506 int nchars;
1508 if (multibyte_p)
1510 int rest = strlen (s), len;
1511 unsigned char *p = (unsigned char *) s;
1513 for (nchars = 0; rest > 0; ++nchars)
1515 string_char_and_length (p, rest, &len);
1516 rest -= len, p += len;
1519 else
1520 nchars = strlen (s);
1522 return nchars;
1526 /* Compute byte position NEWPOS->bytepos corresponding to
1527 NEWPOS->charpos. POS is a known position in string STRING.
1528 NEWPOS->charpos must be >= POS.charpos. */
1530 static void
1531 compute_string_pos (newpos, pos, string)
1532 struct text_pos *newpos, pos;
1533 Lisp_Object string;
1535 xassert (STRINGP (string));
1536 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1538 if (STRING_MULTIBYTE (string))
1539 *newpos = string_pos_nchars_ahead (pos, string,
1540 CHARPOS (*newpos) - CHARPOS (pos));
1541 else
1542 BYTEPOS (*newpos) = CHARPOS (*newpos);
1545 /* EXPORT:
1546 Return an estimation of the pixel height of mode or top lines on
1547 frame F. FACE_ID specifies what line's height to estimate. */
1550 estimate_mode_line_height (f, face_id)
1551 struct frame *f;
1552 enum face_id face_id;
1554 #ifdef HAVE_WINDOW_SYSTEM
1555 if (FRAME_WINDOW_P (f))
1557 int height = FONT_HEIGHT (FRAME_FONT (f));
1559 /* This function is called so early when Emacs starts that the face
1560 cache and mode line face are not yet initialized. */
1561 if (FRAME_FACE_CACHE (f))
1563 struct face *face = FACE_FROM_ID (f, face_id);
1564 if (face)
1566 if (face->font)
1567 height = FONT_HEIGHT (face->font);
1568 if (face->box_line_width > 0)
1569 height += 2 * face->box_line_width;
1573 return height;
1575 #endif
1577 return 1;
1580 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1581 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1582 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1583 not force the value into range. */
1585 void
1586 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1587 FRAME_PTR f;
1588 register int pix_x, pix_y;
1589 int *x, *y;
1590 NativeRectangle *bounds;
1591 int noclip;
1594 #ifdef HAVE_WINDOW_SYSTEM
1595 if (FRAME_WINDOW_P (f))
1597 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1598 even for negative values. */
1599 if (pix_x < 0)
1600 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1601 if (pix_y < 0)
1602 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1604 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1605 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1607 if (bounds)
1608 STORE_NATIVE_RECT (*bounds,
1609 FRAME_COL_TO_PIXEL_X (f, pix_x),
1610 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1611 FRAME_COLUMN_WIDTH (f) - 1,
1612 FRAME_LINE_HEIGHT (f) - 1);
1614 if (!noclip)
1616 if (pix_x < 0)
1617 pix_x = 0;
1618 else if (pix_x > FRAME_TOTAL_COLS (f))
1619 pix_x = FRAME_TOTAL_COLS (f);
1621 if (pix_y < 0)
1622 pix_y = 0;
1623 else if (pix_y > FRAME_LINES (f))
1624 pix_y = FRAME_LINES (f);
1627 #endif
1629 *x = pix_x;
1630 *y = pix_y;
1634 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1635 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1636 can't tell the positions because W's display is not up to date,
1637 return 0. */
1640 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1641 struct window *w;
1642 int hpos, vpos;
1643 int *frame_x, *frame_y;
1645 #ifdef HAVE_WINDOW_SYSTEM
1646 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1648 int success_p;
1650 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1651 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1653 if (display_completed)
1655 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1656 struct glyph *glyph = row->glyphs[TEXT_AREA];
1657 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1659 hpos = row->x;
1660 vpos = row->y;
1661 while (glyph < end)
1663 hpos += glyph->pixel_width;
1664 ++glyph;
1667 /* If first glyph is partially visible, its first visible position is still 0. */
1668 if (hpos < 0)
1669 hpos = 0;
1671 success_p = 1;
1673 else
1675 hpos = vpos = 0;
1676 success_p = 0;
1679 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1680 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1681 return success_p;
1683 #endif
1685 *frame_x = hpos;
1686 *frame_y = vpos;
1687 return 1;
1691 #ifdef HAVE_WINDOW_SYSTEM
1693 /* Find the glyph under window-relative coordinates X/Y in window W.
1694 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1695 strings. Return in *HPOS and *VPOS the row and column number of
1696 the glyph found. Return in *AREA the glyph area containing X.
1697 Value is a pointer to the glyph found or null if X/Y is not on
1698 text, or we can't tell because W's current matrix is not up to
1699 date. */
1701 static struct glyph *
1702 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1703 struct window *w;
1704 int x, y;
1705 int *hpos, *vpos, *dx, *dy, *area;
1707 struct glyph *glyph, *end;
1708 struct glyph_row *row = NULL;
1709 int x0, i;
1711 /* Find row containing Y. Give up if some row is not enabled. */
1712 for (i = 0; i < w->current_matrix->nrows; ++i)
1714 row = MATRIX_ROW (w->current_matrix, i);
1715 if (!row->enabled_p)
1716 return NULL;
1717 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1718 break;
1721 *vpos = i;
1722 *hpos = 0;
1724 /* Give up if Y is not in the window. */
1725 if (i == w->current_matrix->nrows)
1726 return NULL;
1728 /* Get the glyph area containing X. */
1729 if (w->pseudo_window_p)
1731 *area = TEXT_AREA;
1732 x0 = 0;
1734 else
1736 if (x < window_box_left_offset (w, TEXT_AREA))
1738 *area = LEFT_MARGIN_AREA;
1739 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1741 else if (x < window_box_right_offset (w, TEXT_AREA))
1743 *area = TEXT_AREA;
1744 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1746 else
1748 *area = RIGHT_MARGIN_AREA;
1749 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1753 /* Find glyph containing X. */
1754 glyph = row->glyphs[*area];
1755 end = glyph + row->used[*area];
1756 x -= x0;
1757 while (glyph < end && x >= glyph->pixel_width)
1759 x -= glyph->pixel_width;
1760 ++glyph;
1763 if (glyph == end)
1764 return NULL;
1766 if (dx)
1768 *dx = x;
1769 *dy = y - (row->y + row->ascent - glyph->ascent);
1772 *hpos = glyph - row->glyphs[*area];
1773 return glyph;
1777 /* EXPORT:
1778 Convert frame-relative x/y to coordinates relative to window W.
1779 Takes pseudo-windows into account. */
1781 void
1782 frame_to_window_pixel_xy (w, x, y)
1783 struct window *w;
1784 int *x, *y;
1786 if (w->pseudo_window_p)
1788 /* A pseudo-window is always full-width, and starts at the
1789 left edge of the frame, plus a frame border. */
1790 struct frame *f = XFRAME (w->frame);
1791 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1792 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1794 else
1796 *x -= WINDOW_LEFT_EDGE_X (w);
1797 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1801 /* EXPORT:
1802 Return in RECTS[] at most N clipping rectangles for glyph string S.
1803 Return the number of stored rectangles. */
1806 get_glyph_string_clip_rects (s, rects, n)
1807 struct glyph_string *s;
1808 NativeRectangle *rects;
1809 int n;
1811 XRectangle r;
1813 if (n <= 0)
1814 return 0;
1816 if (s->row->full_width_p)
1818 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1819 r.x = WINDOW_LEFT_EDGE_X (s->w);
1820 r.width = WINDOW_TOTAL_WIDTH (s->w);
1822 /* Unless displaying a mode or menu bar line, which are always
1823 fully visible, clip to the visible part of the row. */
1824 if (s->w->pseudo_window_p)
1825 r.height = s->row->visible_height;
1826 else
1827 r.height = s->height;
1829 else
1831 /* This is a text line that may be partially visible. */
1832 r.x = window_box_left (s->w, s->area);
1833 r.width = window_box_width (s->w, s->area);
1834 r.height = s->row->visible_height;
1837 if (s->clip_head)
1838 if (r.x < s->clip_head->x)
1840 if (r.width >= s->clip_head->x - r.x)
1841 r.width -= s->clip_head->x - r.x;
1842 else
1843 r.width = 0;
1844 r.x = s->clip_head->x;
1846 if (s->clip_tail)
1847 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1849 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1850 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1851 else
1852 r.width = 0;
1855 /* If S draws overlapping rows, it's sufficient to use the top and
1856 bottom of the window for clipping because this glyph string
1857 intentionally draws over other lines. */
1858 if (s->for_overlaps)
1860 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1861 r.height = window_text_bottom_y (s->w) - r.y;
1863 /* Alas, the above simple strategy does not work for the
1864 environments with anti-aliased text: if the same text is
1865 drawn onto the same place multiple times, it gets thicker.
1866 If the overlap we are processing is for the erased cursor, we
1867 take the intersection with the rectagle of the cursor. */
1868 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1870 XRectangle rc, r_save = r;
1872 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1873 rc.y = s->w->phys_cursor.y;
1874 rc.width = s->w->phys_cursor_width;
1875 rc.height = s->w->phys_cursor_height;
1877 x_intersect_rectangles (&r_save, &rc, &r);
1880 else
1882 /* Don't use S->y for clipping because it doesn't take partially
1883 visible lines into account. For example, it can be negative for
1884 partially visible lines at the top of a window. */
1885 if (!s->row->full_width_p
1886 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1887 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1888 else
1889 r.y = max (0, s->row->y);
1891 /* If drawing a tool-bar window, draw it over the internal border
1892 at the top of the window. */
1893 if (WINDOWP (s->f->tool_bar_window)
1894 && s->w == XWINDOW (s->f->tool_bar_window))
1895 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1898 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1900 /* If drawing the cursor, don't let glyph draw outside its
1901 advertised boundaries. Cleartype does this under some circumstances. */
1902 if (s->hl == DRAW_CURSOR)
1904 struct glyph *glyph = s->first_glyph;
1905 int height, max_y;
1907 if (s->x > r.x)
1909 r.width -= s->x - r.x;
1910 r.x = s->x;
1912 r.width = min (r.width, glyph->pixel_width);
1914 /* If r.y is below window bottom, ensure that we still see a cursor. */
1915 height = min (glyph->ascent + glyph->descent,
1916 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1917 max_y = window_text_bottom_y (s->w) - height;
1918 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1919 if (s->ybase - glyph->ascent > max_y)
1921 r.y = max_y;
1922 r.height = height;
1924 else
1926 /* Don't draw cursor glyph taller than our actual glyph. */
1927 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1928 if (height < r.height)
1930 max_y = r.y + r.height;
1931 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1932 r.height = min (max_y - r.y, height);
1937 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1938 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1940 #ifdef CONVERT_FROM_XRECT
1941 CONVERT_FROM_XRECT (r, *rects);
1942 #else
1943 *rects = r;
1944 #endif
1945 return 1;
1947 else
1949 /* If we are processing overlapping and allowed to return
1950 multiple clipping rectangles, we exclude the row of the glyph
1951 string from the clipping rectangle. This is to avoid drawing
1952 the same text on the environment with anti-aliasing. */
1953 #ifdef CONVERT_FROM_XRECT
1954 XRectangle rs[2];
1955 #else
1956 XRectangle *rs = rects;
1957 #endif
1958 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1960 if (s->for_overlaps & OVERLAPS_PRED)
1962 rs[i] = r;
1963 if (r.y + r.height > row_y)
1965 if (r.y < row_y)
1966 rs[i].height = row_y - r.y;
1967 else
1968 rs[i].height = 0;
1970 i++;
1972 if (s->for_overlaps & OVERLAPS_SUCC)
1974 rs[i] = r;
1975 if (r.y < row_y + s->row->visible_height)
1977 if (r.y + r.height > row_y + s->row->visible_height)
1979 rs[i].y = row_y + s->row->visible_height;
1980 rs[i].height = r.y + r.height - rs[i].y;
1982 else
1983 rs[i].height = 0;
1985 i++;
1988 n = i;
1989 #ifdef CONVERT_FROM_XRECT
1990 for (i = 0; i < n; i++)
1991 CONVERT_FROM_XRECT (rs[i], rects[i]);
1992 #endif
1993 return n;
1997 /* EXPORT:
1998 Return in *NR the clipping rectangle for glyph string S. */
2000 void
2001 get_glyph_string_clip_rect (s, nr)
2002 struct glyph_string *s;
2003 NativeRectangle *nr;
2005 get_glyph_string_clip_rects (s, nr, 1);
2009 /* EXPORT:
2010 Return the position and height of the phys cursor in window W.
2011 Set w->phys_cursor_width to width of phys cursor.
2014 void
2015 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2016 struct window *w;
2017 struct glyph_row *row;
2018 struct glyph *glyph;
2019 int *xp, *yp, *heightp;
2021 struct frame *f = XFRAME (WINDOW_FRAME (w));
2022 int x, y, wd, h, h0, y0;
2024 /* Compute the width of the rectangle to draw. If on a stretch
2025 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2026 rectangle as wide as the glyph, but use a canonical character
2027 width instead. */
2028 wd = glyph->pixel_width - 1;
2029 #ifdef HAVE_NTGUI
2030 wd++; /* Why? */
2031 #endif
2033 x = w->phys_cursor.x;
2034 if (x < 0)
2036 wd += x;
2037 x = 0;
2040 if (glyph->type == STRETCH_GLYPH
2041 && !x_stretch_cursor_p)
2042 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2043 w->phys_cursor_width = wd;
2045 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2047 /* If y is below window bottom, ensure that we still see a cursor. */
2048 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2050 h = max (h0, glyph->ascent + glyph->descent);
2051 h0 = min (h0, glyph->ascent + glyph->descent);
2053 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2054 if (y < y0)
2056 h = max (h - (y0 - y) + 1, h0);
2057 y = y0 - 1;
2059 else
2061 y0 = window_text_bottom_y (w) - h0;
2062 if (y > y0)
2064 h += y - y0;
2065 y = y0;
2069 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2070 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2071 *heightp = h;
2075 * Remember which glyph the mouse is over.
2078 void
2079 remember_mouse_glyph (f, gx, gy, rect)
2080 struct frame *f;
2081 int gx, gy;
2082 NativeRectangle *rect;
2084 Lisp_Object window;
2085 struct window *w;
2086 struct glyph_row *r, *gr, *end_row;
2087 enum window_part part;
2088 enum glyph_row_area area;
2089 int x, y, width, height;
2091 /* Try to determine frame pixel position and size of the glyph under
2092 frame pixel coordinates X/Y on frame F. */
2094 if (!f->glyphs_initialized_p
2095 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2096 NILP (window)))
2098 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2099 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2100 goto virtual_glyph;
2103 w = XWINDOW (window);
2104 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2105 height = WINDOW_FRAME_LINE_HEIGHT (w);
2107 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2108 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2110 if (w->pseudo_window_p)
2112 area = TEXT_AREA;
2113 part = ON_MODE_LINE; /* Don't adjust margin. */
2114 goto text_glyph;
2117 switch (part)
2119 case ON_LEFT_MARGIN:
2120 area = LEFT_MARGIN_AREA;
2121 goto text_glyph;
2123 case ON_RIGHT_MARGIN:
2124 area = RIGHT_MARGIN_AREA;
2125 goto text_glyph;
2127 case ON_HEADER_LINE:
2128 case ON_MODE_LINE:
2129 gr = (part == ON_HEADER_LINE
2130 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2131 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2132 gy = gr->y;
2133 area = TEXT_AREA;
2134 goto text_glyph_row_found;
2136 case ON_TEXT:
2137 area = TEXT_AREA;
2139 text_glyph:
2140 gr = 0; gy = 0;
2141 for (; r <= end_row && r->enabled_p; ++r)
2142 if (r->y + r->height > y)
2144 gr = r; gy = r->y;
2145 break;
2148 text_glyph_row_found:
2149 if (gr && gy <= y)
2151 struct glyph *g = gr->glyphs[area];
2152 struct glyph *end = g + gr->used[area];
2154 height = gr->height;
2155 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2156 if (gx + g->pixel_width > x)
2157 break;
2159 if (g < end)
2161 if (g->type == IMAGE_GLYPH)
2163 /* Don't remember when mouse is over image, as
2164 image may have hot-spots. */
2165 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2166 return;
2168 width = g->pixel_width;
2170 else
2172 /* Use nominal char spacing at end of line. */
2173 x -= gx;
2174 gx += (x / width) * width;
2177 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2178 gx += window_box_left_offset (w, area);
2180 else
2182 /* Use nominal line height at end of window. */
2183 gx = (x / width) * width;
2184 y -= gy;
2185 gy += (y / height) * height;
2187 break;
2189 case ON_LEFT_FRINGE:
2190 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2191 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2192 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2193 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2194 goto row_glyph;
2196 case ON_RIGHT_FRINGE:
2197 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2198 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2199 : window_box_right_offset (w, TEXT_AREA));
2200 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2201 goto row_glyph;
2203 case ON_SCROLL_BAR:
2204 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2206 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2207 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2208 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2209 : 0)));
2210 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2212 row_glyph:
2213 gr = 0, gy = 0;
2214 for (; r <= end_row && r->enabled_p; ++r)
2215 if (r->y + r->height > y)
2217 gr = r; gy = r->y;
2218 break;
2221 if (gr && gy <= y)
2222 height = gr->height;
2223 else
2225 /* Use nominal line height at end of window. */
2226 y -= gy;
2227 gy += (y / height) * height;
2229 break;
2231 default:
2233 virtual_glyph:
2234 /* If there is no glyph under the mouse, then we divide the screen
2235 into a grid of the smallest glyph in the frame, and use that
2236 as our "glyph". */
2238 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2239 round down even for negative values. */
2240 if (gx < 0)
2241 gx -= width - 1;
2242 if (gy < 0)
2243 gy -= height - 1;
2245 gx = (gx / width) * width;
2246 gy = (gy / height) * height;
2248 goto store_rect;
2251 gx += WINDOW_LEFT_EDGE_X (w);
2252 gy += WINDOW_TOP_EDGE_Y (w);
2254 store_rect:
2255 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2257 /* Visible feedback for debugging. */
2258 #if 0
2259 #if HAVE_X_WINDOWS
2260 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2261 f->output_data.x->normal_gc,
2262 gx, gy, width, height);
2263 #endif
2264 #endif
2268 #endif /* HAVE_WINDOW_SYSTEM */
2271 /***********************************************************************
2272 Lisp form evaluation
2273 ***********************************************************************/
2275 /* Error handler for safe_eval and safe_call. */
2277 static Lisp_Object
2278 safe_eval_handler (arg)
2279 Lisp_Object arg;
2281 add_to_log ("Error during redisplay: %s", arg, Qnil);
2282 return Qnil;
2286 /* Evaluate SEXPR and return the result, or nil if something went
2287 wrong. Prevent redisplay during the evaluation. */
2289 Lisp_Object
2290 safe_eval (sexpr)
2291 Lisp_Object sexpr;
2293 Lisp_Object val;
2295 if (inhibit_eval_during_redisplay)
2296 val = Qnil;
2297 else
2299 int count = SPECPDL_INDEX ();
2300 struct gcpro gcpro1;
2302 GCPRO1 (sexpr);
2303 specbind (Qinhibit_redisplay, Qt);
2304 /* Use Qt to ensure debugger does not run,
2305 so there is no possibility of wanting to redisplay. */
2306 val = internal_condition_case_1 (Feval, sexpr, Qt,
2307 safe_eval_handler);
2308 UNGCPRO;
2309 val = unbind_to (count, val);
2312 return val;
2316 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2317 Return the result, or nil if something went wrong. Prevent
2318 redisplay during the evaluation. */
2320 Lisp_Object
2321 safe_call (nargs, args)
2322 int nargs;
2323 Lisp_Object *args;
2325 Lisp_Object val;
2327 if (inhibit_eval_during_redisplay)
2328 val = Qnil;
2329 else
2331 int count = SPECPDL_INDEX ();
2332 struct gcpro gcpro1;
2334 GCPRO1 (args[0]);
2335 gcpro1.nvars = nargs;
2336 specbind (Qinhibit_redisplay, Qt);
2337 /* Use Qt to ensure debugger does not run,
2338 so there is no possibility of wanting to redisplay. */
2339 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2340 safe_eval_handler);
2341 UNGCPRO;
2342 val = unbind_to (count, val);
2345 return val;
2349 /* Call function FN with one argument ARG.
2350 Return the result, or nil if something went wrong. */
2352 Lisp_Object
2353 safe_call1 (fn, arg)
2354 Lisp_Object fn, arg;
2356 Lisp_Object args[2];
2357 args[0] = fn;
2358 args[1] = arg;
2359 return safe_call (2, args);
2364 /***********************************************************************
2365 Debugging
2366 ***********************************************************************/
2368 #if 0
2370 /* Define CHECK_IT to perform sanity checks on iterators.
2371 This is for debugging. It is too slow to do unconditionally. */
2373 static void
2374 check_it (it)
2375 struct it *it;
2377 if (it->method == GET_FROM_STRING)
2379 xassert (STRINGP (it->string));
2380 xassert (IT_STRING_CHARPOS (*it) >= 0);
2382 else
2384 xassert (IT_STRING_CHARPOS (*it) < 0);
2385 if (it->method == GET_FROM_BUFFER)
2387 /* Check that character and byte positions agree. */
2388 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2392 if (it->dpvec)
2393 xassert (it->current.dpvec_index >= 0);
2394 else
2395 xassert (it->current.dpvec_index < 0);
2398 #define CHECK_IT(IT) check_it ((IT))
2400 #else /* not 0 */
2402 #define CHECK_IT(IT) (void) 0
2404 #endif /* not 0 */
2407 #if GLYPH_DEBUG
2409 /* Check that the window end of window W is what we expect it
2410 to be---the last row in the current matrix displaying text. */
2412 static void
2413 check_window_end (w)
2414 struct window *w;
2416 if (!MINI_WINDOW_P (w)
2417 && !NILP (w->window_end_valid))
2419 struct glyph_row *row;
2420 xassert ((row = MATRIX_ROW (w->current_matrix,
2421 XFASTINT (w->window_end_vpos)),
2422 !row->enabled_p
2423 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2424 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2428 #define CHECK_WINDOW_END(W) check_window_end ((W))
2430 #else /* not GLYPH_DEBUG */
2432 #define CHECK_WINDOW_END(W) (void) 0
2434 #endif /* not GLYPH_DEBUG */
2438 /***********************************************************************
2439 Iterator initialization
2440 ***********************************************************************/
2442 /* Initialize IT for displaying current_buffer in window W, starting
2443 at character position CHARPOS. CHARPOS < 0 means that no buffer
2444 position is specified which is useful when the iterator is assigned
2445 a position later. BYTEPOS is the byte position corresponding to
2446 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2448 If ROW is not null, calls to produce_glyphs with IT as parameter
2449 will produce glyphs in that row.
2451 BASE_FACE_ID is the id of a base face to use. It must be one of
2452 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2453 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2454 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2456 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2457 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2458 will be initialized to use the corresponding mode line glyph row of
2459 the desired matrix of W. */
2461 void
2462 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2463 struct it *it;
2464 struct window *w;
2465 int charpos, bytepos;
2466 struct glyph_row *row;
2467 enum face_id base_face_id;
2469 int highlight_region_p;
2471 /* Some precondition checks. */
2472 xassert (w != NULL && it != NULL);
2473 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2474 && charpos <= ZV));
2476 /* If face attributes have been changed since the last redisplay,
2477 free realized faces now because they depend on face definitions
2478 that might have changed. Don't free faces while there might be
2479 desired matrices pending which reference these faces. */
2480 if (face_change_count && !inhibit_free_realized_faces)
2482 face_change_count = 0;
2483 free_all_realized_faces (Qnil);
2486 /* Use one of the mode line rows of W's desired matrix if
2487 appropriate. */
2488 if (row == NULL)
2490 if (base_face_id == MODE_LINE_FACE_ID
2491 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2492 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2493 else if (base_face_id == HEADER_LINE_FACE_ID)
2494 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2497 /* Clear IT. */
2498 bzero (it, sizeof *it);
2499 it->current.overlay_string_index = -1;
2500 it->current.dpvec_index = -1;
2501 it->base_face_id = base_face_id;
2502 it->string = Qnil;
2503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2505 /* The window in which we iterate over current_buffer: */
2506 XSETWINDOW (it->window, w);
2507 it->w = w;
2508 it->f = XFRAME (w->frame);
2510 /* Extra space between lines (on window systems only). */
2511 if (base_face_id == DEFAULT_FACE_ID
2512 && FRAME_WINDOW_P (it->f))
2514 if (NATNUMP (current_buffer->extra_line_spacing))
2515 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2516 else if (FLOATP (current_buffer->extra_line_spacing))
2517 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2518 * FRAME_LINE_HEIGHT (it->f));
2519 else if (it->f->extra_line_spacing > 0)
2520 it->extra_line_spacing = it->f->extra_line_spacing;
2521 it->max_extra_line_spacing = 0;
2524 /* If realized faces have been removed, e.g. because of face
2525 attribute changes of named faces, recompute them. When running
2526 in batch mode, the face cache of Vterminal_frame is null. If
2527 we happen to get called, make a dummy face cache. */
2528 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2529 init_frame_faces (it->f);
2530 if (FRAME_FACE_CACHE (it->f)->used == 0)
2531 recompute_basic_faces (it->f);
2533 /* Current value of the `slice', `space-width', and 'height' properties. */
2534 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2535 it->space_width = Qnil;
2536 it->font_height = Qnil;
2537 it->override_ascent = -1;
2539 /* Are control characters displayed as `^C'? */
2540 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2542 /* -1 means everything between a CR and the following line end
2543 is invisible. >0 means lines indented more than this value are
2544 invisible. */
2545 it->selective = (INTEGERP (current_buffer->selective_display)
2546 ? XFASTINT (current_buffer->selective_display)
2547 : (!NILP (current_buffer->selective_display)
2548 ? -1 : 0));
2549 it->selective_display_ellipsis_p
2550 = !NILP (current_buffer->selective_display_ellipses);
2552 /* Display table to use. */
2553 it->dp = window_display_table (w);
2555 /* Are multibyte characters enabled in current_buffer? */
2556 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2558 /* Non-zero if we should highlight the region. */
2559 highlight_region_p
2560 = (!NILP (Vtransient_mark_mode)
2561 && !NILP (current_buffer->mark_active)
2562 && XMARKER (current_buffer->mark)->buffer != 0);
2564 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2565 start and end of a visible region in window IT->w. Set both to
2566 -1 to indicate no region. */
2567 if (highlight_region_p
2568 /* Maybe highlight only in selected window. */
2569 && (/* Either show region everywhere. */
2570 highlight_nonselected_windows
2571 /* Or show region in the selected window. */
2572 || w == XWINDOW (selected_window)
2573 /* Or show the region if we are in the mini-buffer and W is
2574 the window the mini-buffer refers to. */
2575 || (MINI_WINDOW_P (XWINDOW (selected_window))
2576 && WINDOWP (minibuf_selected_window)
2577 && w == XWINDOW (minibuf_selected_window))))
2579 int charpos = marker_position (current_buffer->mark);
2580 it->region_beg_charpos = min (PT, charpos);
2581 it->region_end_charpos = max (PT, charpos);
2583 else
2584 it->region_beg_charpos = it->region_end_charpos = -1;
2586 /* Get the position at which the redisplay_end_trigger hook should
2587 be run, if it is to be run at all. */
2588 if (MARKERP (w->redisplay_end_trigger)
2589 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2590 it->redisplay_end_trigger_charpos
2591 = marker_position (w->redisplay_end_trigger);
2592 else if (INTEGERP (w->redisplay_end_trigger))
2593 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2595 /* Correct bogus values of tab_width. */
2596 it->tab_width = XINT (current_buffer->tab_width);
2597 if (it->tab_width <= 0 || it->tab_width > 1000)
2598 it->tab_width = 8;
2600 /* Are lines in the display truncated? */
2601 it->truncate_lines_p
2602 = (base_face_id != DEFAULT_FACE_ID
2603 || XINT (it->w->hscroll)
2604 || (truncate_partial_width_windows
2605 && !WINDOW_FULL_WIDTH_P (it->w))
2606 || !NILP (current_buffer->truncate_lines));
2608 /* Get dimensions of truncation and continuation glyphs. These are
2609 displayed as fringe bitmaps under X, so we don't need them for such
2610 frames. */
2611 if (!FRAME_WINDOW_P (it->f))
2613 if (it->truncate_lines_p)
2615 /* We will need the truncation glyph. */
2616 xassert (it->glyph_row == NULL);
2617 produce_special_glyphs (it, IT_TRUNCATION);
2618 it->truncation_pixel_width = it->pixel_width;
2620 else
2622 /* We will need the continuation glyph. */
2623 xassert (it->glyph_row == NULL);
2624 produce_special_glyphs (it, IT_CONTINUATION);
2625 it->continuation_pixel_width = it->pixel_width;
2628 /* Reset these values to zero because the produce_special_glyphs
2629 above has changed them. */
2630 it->pixel_width = it->ascent = it->descent = 0;
2631 it->phys_ascent = it->phys_descent = 0;
2634 /* Set this after getting the dimensions of truncation and
2635 continuation glyphs, so that we don't produce glyphs when calling
2636 produce_special_glyphs, above. */
2637 it->glyph_row = row;
2638 it->area = TEXT_AREA;
2640 /* Get the dimensions of the display area. The display area
2641 consists of the visible window area plus a horizontally scrolled
2642 part to the left of the window. All x-values are relative to the
2643 start of this total display area. */
2644 if (base_face_id != DEFAULT_FACE_ID)
2646 /* Mode lines, menu bar in terminal frames. */
2647 it->first_visible_x = 0;
2648 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2650 else
2652 it->first_visible_x
2653 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2654 it->last_visible_x = (it->first_visible_x
2655 + window_box_width (w, TEXT_AREA));
2657 /* If we truncate lines, leave room for the truncator glyph(s) at
2658 the right margin. Otherwise, leave room for the continuation
2659 glyph(s). Truncation and continuation glyphs are not inserted
2660 for window-based redisplay. */
2661 if (!FRAME_WINDOW_P (it->f))
2663 if (it->truncate_lines_p)
2664 it->last_visible_x -= it->truncation_pixel_width;
2665 else
2666 it->last_visible_x -= it->continuation_pixel_width;
2669 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2670 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2673 /* Leave room for a border glyph. */
2674 if (!FRAME_WINDOW_P (it->f)
2675 && !WINDOW_RIGHTMOST_P (it->w))
2676 it->last_visible_x -= 1;
2678 it->last_visible_y = window_text_bottom_y (w);
2680 /* For mode lines and alike, arrange for the first glyph having a
2681 left box line if the face specifies a box. */
2682 if (base_face_id != DEFAULT_FACE_ID)
2684 struct face *face;
2686 it->face_id = base_face_id;
2688 /* If we have a boxed mode line, make the first character appear
2689 with a left box line. */
2690 face = FACE_FROM_ID (it->f, base_face_id);
2691 if (face->box != FACE_NO_BOX)
2692 it->start_of_box_run_p = 1;
2695 /* If a buffer position was specified, set the iterator there,
2696 getting overlays and face properties from that position. */
2697 if (charpos >= BUF_BEG (current_buffer))
2699 it->end_charpos = ZV;
2700 it->face_id = -1;
2701 IT_CHARPOS (*it) = charpos;
2703 /* Compute byte position if not specified. */
2704 if (bytepos < charpos)
2705 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2706 else
2707 IT_BYTEPOS (*it) = bytepos;
2709 it->start = it->current;
2711 /* Compute faces etc. */
2712 reseat (it, it->current.pos, 1);
2715 CHECK_IT (it);
2719 /* Initialize IT for the display of window W with window start POS. */
2721 void
2722 start_display (it, w, pos)
2723 struct it *it;
2724 struct window *w;
2725 struct text_pos pos;
2727 struct glyph_row *row;
2728 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2730 row = w->desired_matrix->rows + first_vpos;
2731 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2732 it->first_vpos = first_vpos;
2734 /* Don't reseat to previous visible line start if current start
2735 position is in a string or image. */
2736 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2738 int start_at_line_beg_p;
2739 int first_y = it->current_y;
2741 /* If window start is not at a line start, skip forward to POS to
2742 get the correct continuation lines width. */
2743 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2744 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2745 if (!start_at_line_beg_p)
2747 int new_x;
2749 reseat_at_previous_visible_line_start (it);
2750 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2752 new_x = it->current_x + it->pixel_width;
2754 /* If lines are continued, this line may end in the middle
2755 of a multi-glyph character (e.g. a control character
2756 displayed as \003, or in the middle of an overlay
2757 string). In this case move_it_to above will not have
2758 taken us to the start of the continuation line but to the
2759 end of the continued line. */
2760 if (it->current_x > 0
2761 && !it->truncate_lines_p /* Lines are continued. */
2762 && (/* And glyph doesn't fit on the line. */
2763 new_x > it->last_visible_x
2764 /* Or it fits exactly and we're on a window
2765 system frame. */
2766 || (new_x == it->last_visible_x
2767 && FRAME_WINDOW_P (it->f))))
2769 if (it->current.dpvec_index >= 0
2770 || it->current.overlay_string_index >= 0)
2772 set_iterator_to_next (it, 1);
2773 move_it_in_display_line_to (it, -1, -1, 0);
2776 it->continuation_lines_width += it->current_x;
2779 /* We're starting a new display line, not affected by the
2780 height of the continued line, so clear the appropriate
2781 fields in the iterator structure. */
2782 it->max_ascent = it->max_descent = 0;
2783 it->max_phys_ascent = it->max_phys_descent = 0;
2785 it->current_y = first_y;
2786 it->vpos = 0;
2787 it->current_x = it->hpos = 0;
2791 #if 0 /* Don't assert the following because start_display is sometimes
2792 called intentionally with a window start that is not at a
2793 line start. Please leave this code in as a comment. */
2795 /* Window start should be on a line start, now. */
2796 xassert (it->continuation_lines_width
2797 || IT_CHARPOS (it) == BEGV
2798 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2799 #endif /* 0 */
2803 /* Return 1 if POS is a position in ellipses displayed for invisible
2804 text. W is the window we display, for text property lookup. */
2806 static int
2807 in_ellipses_for_invisible_text_p (pos, w)
2808 struct display_pos *pos;
2809 struct window *w;
2811 Lisp_Object prop, window;
2812 int ellipses_p = 0;
2813 int charpos = CHARPOS (pos->pos);
2815 /* If POS specifies a position in a display vector, this might
2816 be for an ellipsis displayed for invisible text. We won't
2817 get the iterator set up for delivering that ellipsis unless
2818 we make sure that it gets aware of the invisible text. */
2819 if (pos->dpvec_index >= 0
2820 && pos->overlay_string_index < 0
2821 && CHARPOS (pos->string_pos) < 0
2822 && charpos > BEGV
2823 && (XSETWINDOW (window, w),
2824 prop = Fget_char_property (make_number (charpos),
2825 Qinvisible, window),
2826 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2828 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2829 window);
2830 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2833 return ellipses_p;
2837 /* Initialize IT for stepping through current_buffer in window W,
2838 starting at position POS that includes overlay string and display
2839 vector/ control character translation position information. Value
2840 is zero if there are overlay strings with newlines at POS. */
2842 static int
2843 init_from_display_pos (it, w, pos)
2844 struct it *it;
2845 struct window *w;
2846 struct display_pos *pos;
2848 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2849 int i, overlay_strings_with_newlines = 0;
2851 /* If POS specifies a position in a display vector, this might
2852 be for an ellipsis displayed for invisible text. We won't
2853 get the iterator set up for delivering that ellipsis unless
2854 we make sure that it gets aware of the invisible text. */
2855 if (in_ellipses_for_invisible_text_p (pos, w))
2857 --charpos;
2858 bytepos = 0;
2861 /* Keep in mind: the call to reseat in init_iterator skips invisible
2862 text, so we might end up at a position different from POS. This
2863 is only a problem when POS is a row start after a newline and an
2864 overlay starts there with an after-string, and the overlay has an
2865 invisible property. Since we don't skip invisible text in
2866 display_line and elsewhere immediately after consuming the
2867 newline before the row start, such a POS will not be in a string,
2868 but the call to init_iterator below will move us to the
2869 after-string. */
2870 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2872 /* This only scans the current chunk -- it should scan all chunks.
2873 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2874 to 16 in 22.1 to make this a lesser problem. */
2875 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2877 const char *s = SDATA (it->overlay_strings[i]);
2878 const char *e = s + SBYTES (it->overlay_strings[i]);
2880 while (s < e && *s != '\n')
2881 ++s;
2883 if (s < e)
2885 overlay_strings_with_newlines = 1;
2886 break;
2890 /* If position is within an overlay string, set up IT to the right
2891 overlay string. */
2892 if (pos->overlay_string_index >= 0)
2894 int relative_index;
2896 /* If the first overlay string happens to have a `display'
2897 property for an image, the iterator will be set up for that
2898 image, and we have to undo that setup first before we can
2899 correct the overlay string index. */
2900 if (it->method == GET_FROM_IMAGE)
2901 pop_it (it);
2903 /* We already have the first chunk of overlay strings in
2904 IT->overlay_strings. Load more until the one for
2905 pos->overlay_string_index is in IT->overlay_strings. */
2906 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2908 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2909 it->current.overlay_string_index = 0;
2910 while (n--)
2912 load_overlay_strings (it, 0);
2913 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2917 it->current.overlay_string_index = pos->overlay_string_index;
2918 relative_index = (it->current.overlay_string_index
2919 % OVERLAY_STRING_CHUNK_SIZE);
2920 it->string = it->overlay_strings[relative_index];
2921 xassert (STRINGP (it->string));
2922 it->current.string_pos = pos->string_pos;
2923 it->method = GET_FROM_STRING;
2926 #if 0 /* This is bogus because POS not having an overlay string
2927 position does not mean it's after the string. Example: A
2928 line starting with a before-string and initialization of IT
2929 to the previous row's end position. */
2930 else if (it->current.overlay_string_index >= 0)
2932 /* If POS says we're already after an overlay string ending at
2933 POS, make sure to pop the iterator because it will be in
2934 front of that overlay string. When POS is ZV, we've thereby
2935 also ``processed'' overlay strings at ZV. */
2936 while (it->sp)
2937 pop_it (it);
2938 xassert (it->current.overlay_string_index == -1);
2939 xassert (it->method == GET_FROM_BUFFER);
2940 if (CHARPOS (pos->pos) == ZV)
2941 it->overlay_strings_at_end_processed_p = 1;
2943 #endif /* 0 */
2945 if (CHARPOS (pos->string_pos) >= 0)
2947 /* Recorded position is not in an overlay string, but in another
2948 string. This can only be a string from a `display' property.
2949 IT should already be filled with that string. */
2950 it->current.string_pos = pos->string_pos;
2951 xassert (STRINGP (it->string));
2954 /* Restore position in display vector translations, control
2955 character translations or ellipses. */
2956 if (pos->dpvec_index >= 0)
2958 if (it->dpvec == NULL)
2959 get_next_display_element (it);
2960 xassert (it->dpvec && it->current.dpvec_index == 0);
2961 it->current.dpvec_index = pos->dpvec_index;
2964 CHECK_IT (it);
2965 return !overlay_strings_with_newlines;
2969 /* Initialize IT for stepping through current_buffer in window W
2970 starting at ROW->start. */
2972 static void
2973 init_to_row_start (it, w, row)
2974 struct it *it;
2975 struct window *w;
2976 struct glyph_row *row;
2978 init_from_display_pos (it, w, &row->start);
2979 it->start = row->start;
2980 it->continuation_lines_width = row->continuation_lines_width;
2981 CHECK_IT (it);
2985 /* Initialize IT for stepping through current_buffer in window W
2986 starting in the line following ROW, i.e. starting at ROW->end.
2987 Value is zero if there are overlay strings with newlines at ROW's
2988 end position. */
2990 static int
2991 init_to_row_end (it, w, row)
2992 struct it *it;
2993 struct window *w;
2994 struct glyph_row *row;
2996 int success = 0;
2998 if (init_from_display_pos (it, w, &row->end))
3000 if (row->continued_p)
3001 it->continuation_lines_width
3002 = row->continuation_lines_width + row->pixel_width;
3003 CHECK_IT (it);
3004 success = 1;
3007 return success;
3013 /***********************************************************************
3014 Text properties
3015 ***********************************************************************/
3017 /* Called when IT reaches IT->stop_charpos. Handle text property and
3018 overlay changes. Set IT->stop_charpos to the next position where
3019 to stop. */
3021 static void
3022 handle_stop (it)
3023 struct it *it;
3025 enum prop_handled handled;
3026 int handle_overlay_change_p;
3027 struct props *p;
3029 it->dpvec = NULL;
3030 it->current.dpvec_index = -1;
3031 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3032 it->ignore_overlay_strings_at_pos_p = 0;
3034 /* Use face of preceding text for ellipsis (if invisible) */
3035 if (it->selective_display_ellipsis_p)
3036 it->saved_face_id = it->face_id;
3040 handled = HANDLED_NORMALLY;
3042 /* Call text property handlers. */
3043 for (p = it_props; p->handler; ++p)
3045 handled = p->handler (it);
3047 if (handled == HANDLED_RECOMPUTE_PROPS)
3048 break;
3049 else if (handled == HANDLED_RETURN)
3051 /* We still want to show before and after strings from
3052 overlays even if the actual buffer text is replaced. */
3053 if (!handle_overlay_change_p || it->sp > 1)
3054 return;
3055 if (!get_overlay_strings_1 (it, 0, 0))
3056 return;
3057 it->ignore_overlay_strings_at_pos_p = 1;
3058 it->string_from_display_prop_p = 0;
3059 handle_overlay_change_p = 0;
3060 handled = HANDLED_RECOMPUTE_PROPS;
3061 break;
3063 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3064 handle_overlay_change_p = 0;
3067 if (handled != HANDLED_RECOMPUTE_PROPS)
3069 /* Don't check for overlay strings below when set to deliver
3070 characters from a display vector. */
3071 if (it->method == GET_FROM_DISPLAY_VECTOR)
3072 handle_overlay_change_p = 0;
3074 /* Handle overlay changes. */
3075 if (handle_overlay_change_p)
3076 handled = handle_overlay_change (it);
3078 /* Determine where to stop next. */
3079 if (handled == HANDLED_NORMALLY)
3080 compute_stop_pos (it);
3083 while (handled == HANDLED_RECOMPUTE_PROPS);
3087 /* Compute IT->stop_charpos from text property and overlay change
3088 information for IT's current position. */
3090 static void
3091 compute_stop_pos (it)
3092 struct it *it;
3094 register INTERVAL iv, next_iv;
3095 Lisp_Object object, limit, position;
3097 /* If nowhere else, stop at the end. */
3098 it->stop_charpos = it->end_charpos;
3100 if (STRINGP (it->string))
3102 /* Strings are usually short, so don't limit the search for
3103 properties. */
3104 object = it->string;
3105 limit = Qnil;
3106 position = make_number (IT_STRING_CHARPOS (*it));
3108 else
3110 int charpos;
3112 /* If next overlay change is in front of the current stop pos
3113 (which is IT->end_charpos), stop there. Note: value of
3114 next_overlay_change is point-max if no overlay change
3115 follows. */
3116 charpos = next_overlay_change (IT_CHARPOS (*it));
3117 if (charpos < it->stop_charpos)
3118 it->stop_charpos = charpos;
3120 /* If showing the region, we have to stop at the region
3121 start or end because the face might change there. */
3122 if (it->region_beg_charpos > 0)
3124 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3125 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3126 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3127 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3130 /* Set up variables for computing the stop position from text
3131 property changes. */
3132 XSETBUFFER (object, current_buffer);
3133 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3134 position = make_number (IT_CHARPOS (*it));
3138 /* Get the interval containing IT's position. Value is a null
3139 interval if there isn't such an interval. */
3140 iv = validate_interval_range (object, &position, &position, 0);
3141 if (!NULL_INTERVAL_P (iv))
3143 Lisp_Object values_here[LAST_PROP_IDX];
3144 struct props *p;
3146 /* Get properties here. */
3147 for (p = it_props; p->handler; ++p)
3148 values_here[p->idx] = textget (iv->plist, *p->name);
3150 /* Look for an interval following iv that has different
3151 properties. */
3152 for (next_iv = next_interval (iv);
3153 (!NULL_INTERVAL_P (next_iv)
3154 && (NILP (limit)
3155 || XFASTINT (limit) > next_iv->position));
3156 next_iv = next_interval (next_iv))
3158 for (p = it_props; p->handler; ++p)
3160 Lisp_Object new_value;
3162 new_value = textget (next_iv->plist, *p->name);
3163 if (!EQ (values_here[p->idx], new_value))
3164 break;
3167 if (p->handler)
3168 break;
3171 if (!NULL_INTERVAL_P (next_iv))
3173 if (INTEGERP (limit)
3174 && next_iv->position >= XFASTINT (limit))
3175 /* No text property change up to limit. */
3176 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3177 else
3178 /* Text properties change in next_iv. */
3179 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3183 xassert (STRINGP (it->string)
3184 || (it->stop_charpos >= BEGV
3185 && it->stop_charpos >= IT_CHARPOS (*it)));
3189 /* Return the position of the next overlay change after POS in
3190 current_buffer. Value is point-max if no overlay change
3191 follows. This is like `next-overlay-change' but doesn't use
3192 xmalloc. */
3194 static int
3195 next_overlay_change (pos)
3196 int pos;
3198 int noverlays;
3199 int endpos;
3200 Lisp_Object *overlays;
3201 int i;
3203 /* Get all overlays at the given position. */
3204 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3206 /* If any of these overlays ends before endpos,
3207 use its ending point instead. */
3208 for (i = 0; i < noverlays; ++i)
3210 Lisp_Object oend;
3211 int oendpos;
3213 oend = OVERLAY_END (overlays[i]);
3214 oendpos = OVERLAY_POSITION (oend);
3215 endpos = min (endpos, oendpos);
3218 return endpos;
3223 /***********************************************************************
3224 Fontification
3225 ***********************************************************************/
3227 /* Handle changes in the `fontified' property of the current buffer by
3228 calling hook functions from Qfontification_functions to fontify
3229 regions of text. */
3231 static enum prop_handled
3232 handle_fontified_prop (it)
3233 struct it *it;
3235 Lisp_Object prop, pos;
3236 enum prop_handled handled = HANDLED_NORMALLY;
3238 if (!NILP (Vmemory_full))
3239 return handled;
3241 /* Get the value of the `fontified' property at IT's current buffer
3242 position. (The `fontified' property doesn't have a special
3243 meaning in strings.) If the value is nil, call functions from
3244 Qfontification_functions. */
3245 if (!STRINGP (it->string)
3246 && it->s == NULL
3247 && !NILP (Vfontification_functions)
3248 && !NILP (Vrun_hooks)
3249 && (pos = make_number (IT_CHARPOS (*it)),
3250 prop = Fget_char_property (pos, Qfontified, Qnil),
3251 /* Ignore the special cased nil value always present at EOB since
3252 no amount of fontifying will be able to change it. */
3253 NILP (prop) && IT_CHARPOS (*it) < Z))
3255 int count = SPECPDL_INDEX ();
3256 Lisp_Object val;
3258 val = Vfontification_functions;
3259 specbind (Qfontification_functions, Qnil);
3261 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3262 safe_call1 (val, pos);
3263 else
3265 Lisp_Object globals, fn;
3266 struct gcpro gcpro1, gcpro2;
3268 globals = Qnil;
3269 GCPRO2 (val, globals);
3271 for (; CONSP (val); val = XCDR (val))
3273 fn = XCAR (val);
3275 if (EQ (fn, Qt))
3277 /* A value of t indicates this hook has a local
3278 binding; it means to run the global binding too.
3279 In a global value, t should not occur. If it
3280 does, we must ignore it to avoid an endless
3281 loop. */
3282 for (globals = Fdefault_value (Qfontification_functions);
3283 CONSP (globals);
3284 globals = XCDR (globals))
3286 fn = XCAR (globals);
3287 if (!EQ (fn, Qt))
3288 safe_call1 (fn, pos);
3291 else
3292 safe_call1 (fn, pos);
3295 UNGCPRO;
3298 unbind_to (count, Qnil);
3300 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3301 something. This avoids an endless loop if they failed to
3302 fontify the text for which reason ever. */
3303 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3304 handled = HANDLED_RECOMPUTE_PROPS;
3307 return handled;
3312 /***********************************************************************
3313 Faces
3314 ***********************************************************************/
3316 /* Set up iterator IT from face properties at its current position.
3317 Called from handle_stop. */
3319 static enum prop_handled
3320 handle_face_prop (it)
3321 struct it *it;
3323 int new_face_id, next_stop;
3325 if (!STRINGP (it->string))
3327 new_face_id
3328 = face_at_buffer_position (it->w,
3329 IT_CHARPOS (*it),
3330 it->region_beg_charpos,
3331 it->region_end_charpos,
3332 &next_stop,
3333 (IT_CHARPOS (*it)
3334 + TEXT_PROP_DISTANCE_LIMIT),
3337 /* Is this a start of a run of characters with box face?
3338 Caveat: this can be called for a freshly initialized
3339 iterator; face_id is -1 in this case. We know that the new
3340 face will not change until limit, i.e. if the new face has a
3341 box, all characters up to limit will have one. But, as
3342 usual, we don't know whether limit is really the end. */
3343 if (new_face_id != it->face_id)
3345 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3347 /* If new face has a box but old face has not, this is
3348 the start of a run of characters with box, i.e. it has
3349 a shadow on the left side. The value of face_id of the
3350 iterator will be -1 if this is the initial call that gets
3351 the face. In this case, we have to look in front of IT's
3352 position and see whether there is a face != new_face_id. */
3353 it->start_of_box_run_p
3354 = (new_face->box != FACE_NO_BOX
3355 && (it->face_id >= 0
3356 || IT_CHARPOS (*it) == BEG
3357 || new_face_id != face_before_it_pos (it)));
3358 it->face_box_p = new_face->box != FACE_NO_BOX;
3361 else
3363 int base_face_id, bufpos;
3365 if (it->current.overlay_string_index >= 0)
3366 bufpos = IT_CHARPOS (*it);
3367 else
3368 bufpos = 0;
3370 /* For strings from a buffer, i.e. overlay strings or strings
3371 from a `display' property, use the face at IT's current
3372 buffer position as the base face to merge with, so that
3373 overlay strings appear in the same face as surrounding
3374 text, unless they specify their own faces. */
3375 base_face_id = underlying_face_id (it);
3377 new_face_id = face_at_string_position (it->w,
3378 it->string,
3379 IT_STRING_CHARPOS (*it),
3380 bufpos,
3381 it->region_beg_charpos,
3382 it->region_end_charpos,
3383 &next_stop,
3384 base_face_id, 0);
3386 #if 0 /* This shouldn't be neccessary. Let's check it. */
3387 /* If IT is used to display a mode line we would really like to
3388 use the mode line face instead of the frame's default face. */
3389 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3390 && new_face_id == DEFAULT_FACE_ID)
3391 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3392 #endif
3394 /* Is this a start of a run of characters with box? Caveat:
3395 this can be called for a freshly allocated iterator; face_id
3396 is -1 is this case. We know that the new face will not
3397 change until the next check pos, i.e. if the new face has a
3398 box, all characters up to that position will have a
3399 box. But, as usual, we don't know whether that position
3400 is really the end. */
3401 if (new_face_id != it->face_id)
3403 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3404 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3406 /* If new face has a box but old face hasn't, this is the
3407 start of a run of characters with box, i.e. it has a
3408 shadow on the left side. */
3409 it->start_of_box_run_p
3410 = new_face->box && (old_face == NULL || !old_face->box);
3411 it->face_box_p = new_face->box != FACE_NO_BOX;
3415 it->face_id = new_face_id;
3416 return HANDLED_NORMALLY;
3420 /* Return the ID of the face ``underlying'' IT's current position,
3421 which is in a string. If the iterator is associated with a
3422 buffer, return the face at IT's current buffer position.
3423 Otherwise, use the iterator's base_face_id. */
3425 static int
3426 underlying_face_id (it)
3427 struct it *it;
3429 int face_id = it->base_face_id, i;
3431 xassert (STRINGP (it->string));
3433 for (i = it->sp - 1; i >= 0; --i)
3434 if (NILP (it->stack[i].string))
3435 face_id = it->stack[i].face_id;
3437 return face_id;
3441 /* Compute the face one character before or after the current position
3442 of IT. BEFORE_P non-zero means get the face in front of IT's
3443 position. Value is the id of the face. */
3445 static int
3446 face_before_or_after_it_pos (it, before_p)
3447 struct it *it;
3448 int before_p;
3450 int face_id, limit;
3451 int next_check_charpos;
3452 struct text_pos pos;
3454 xassert (it->s == NULL);
3456 if (STRINGP (it->string))
3458 int bufpos, base_face_id;
3460 /* No face change past the end of the string (for the case
3461 we are padding with spaces). No face change before the
3462 string start. */
3463 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3464 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3465 return it->face_id;
3467 /* Set pos to the position before or after IT's current position. */
3468 if (before_p)
3469 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3470 else
3471 /* For composition, we must check the character after the
3472 composition. */
3473 pos = (it->what == IT_COMPOSITION
3474 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3475 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3477 if (it->current.overlay_string_index >= 0)
3478 bufpos = IT_CHARPOS (*it);
3479 else
3480 bufpos = 0;
3482 base_face_id = underlying_face_id (it);
3484 /* Get the face for ASCII, or unibyte. */
3485 face_id = face_at_string_position (it->w,
3486 it->string,
3487 CHARPOS (pos),
3488 bufpos,
3489 it->region_beg_charpos,
3490 it->region_end_charpos,
3491 &next_check_charpos,
3492 base_face_id, 0);
3494 /* Correct the face for charsets different from ASCII. Do it
3495 for the multibyte case only. The face returned above is
3496 suitable for unibyte text if IT->string is unibyte. */
3497 if (STRING_MULTIBYTE (it->string))
3499 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3500 int rest = SBYTES (it->string) - BYTEPOS (pos);
3501 int c, len;
3502 struct face *face = FACE_FROM_ID (it->f, face_id);
3504 c = string_char_and_length (p, rest, &len);
3505 face_id = FACE_FOR_CHAR (it->f, face, c);
3508 else
3510 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3511 || (IT_CHARPOS (*it) <= BEGV && before_p))
3512 return it->face_id;
3514 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3515 pos = it->current.pos;
3517 if (before_p)
3518 DEC_TEXT_POS (pos, it->multibyte_p);
3519 else
3521 if (it->what == IT_COMPOSITION)
3522 /* For composition, we must check the position after the
3523 composition. */
3524 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3525 else
3526 INC_TEXT_POS (pos, it->multibyte_p);
3529 /* Determine face for CHARSET_ASCII, or unibyte. */
3530 face_id = face_at_buffer_position (it->w,
3531 CHARPOS (pos),
3532 it->region_beg_charpos,
3533 it->region_end_charpos,
3534 &next_check_charpos,
3535 limit, 0);
3537 /* Correct the face for charsets different from ASCII. Do it
3538 for the multibyte case only. The face returned above is
3539 suitable for unibyte text if current_buffer is unibyte. */
3540 if (it->multibyte_p)
3542 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3543 struct face *face = FACE_FROM_ID (it->f, face_id);
3544 face_id = FACE_FOR_CHAR (it->f, face, c);
3548 return face_id;
3553 /***********************************************************************
3554 Invisible text
3555 ***********************************************************************/
3557 /* Set up iterator IT from invisible properties at its current
3558 position. Called from handle_stop. */
3560 static enum prop_handled
3561 handle_invisible_prop (it)
3562 struct it *it;
3564 enum prop_handled handled = HANDLED_NORMALLY;
3566 if (STRINGP (it->string))
3568 extern Lisp_Object Qinvisible;
3569 Lisp_Object prop, end_charpos, limit, charpos;
3571 /* Get the value of the invisible text property at the
3572 current position. Value will be nil if there is no such
3573 property. */
3574 charpos = make_number (IT_STRING_CHARPOS (*it));
3575 prop = Fget_text_property (charpos, Qinvisible, it->string);
3577 if (!NILP (prop)
3578 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3580 handled = HANDLED_RECOMPUTE_PROPS;
3582 /* Get the position at which the next change of the
3583 invisible text property can be found in IT->string.
3584 Value will be nil if the property value is the same for
3585 all the rest of IT->string. */
3586 XSETINT (limit, SCHARS (it->string));
3587 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3588 it->string, limit);
3590 /* Text at current position is invisible. The next
3591 change in the property is at position end_charpos.
3592 Move IT's current position to that position. */
3593 if (INTEGERP (end_charpos)
3594 && XFASTINT (end_charpos) < XFASTINT (limit))
3596 struct text_pos old;
3597 old = it->current.string_pos;
3598 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3599 compute_string_pos (&it->current.string_pos, old, it->string);
3601 else
3603 /* The rest of the string is invisible. If this is an
3604 overlay string, proceed with the next overlay string
3605 or whatever comes and return a character from there. */
3606 if (it->current.overlay_string_index >= 0)
3608 next_overlay_string (it);
3609 /* Don't check for overlay strings when we just
3610 finished processing them. */
3611 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3613 else
3615 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3616 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3621 else
3623 int invis_p, newpos, next_stop, start_charpos;
3624 Lisp_Object pos, prop, overlay;
3626 /* First of all, is there invisible text at this position? */
3627 start_charpos = IT_CHARPOS (*it);
3628 pos = make_number (IT_CHARPOS (*it));
3629 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3630 &overlay);
3631 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3633 /* If we are on invisible text, skip over it. */
3634 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3636 /* Record whether we have to display an ellipsis for the
3637 invisible text. */
3638 int display_ellipsis_p = invis_p == 2;
3640 handled = HANDLED_RECOMPUTE_PROPS;
3642 /* Loop skipping over invisible text. The loop is left at
3643 ZV or with IT on the first char being visible again. */
3646 /* Try to skip some invisible text. Return value is the
3647 position reached which can be equal to IT's position
3648 if there is nothing invisible here. This skips both
3649 over invisible text properties and overlays with
3650 invisible property. */
3651 newpos = skip_invisible (IT_CHARPOS (*it),
3652 &next_stop, ZV, it->window);
3654 /* If we skipped nothing at all we weren't at invisible
3655 text in the first place. If everything to the end of
3656 the buffer was skipped, end the loop. */
3657 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3658 invis_p = 0;
3659 else
3661 /* We skipped some characters but not necessarily
3662 all there are. Check if we ended up on visible
3663 text. Fget_char_property returns the property of
3664 the char before the given position, i.e. if we
3665 get invis_p = 0, this means that the char at
3666 newpos is visible. */
3667 pos = make_number (newpos);
3668 prop = Fget_char_property (pos, Qinvisible, it->window);
3669 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3672 /* If we ended up on invisible text, proceed to
3673 skip starting with next_stop. */
3674 if (invis_p)
3675 IT_CHARPOS (*it) = next_stop;
3677 /* If there are adjacent invisible texts, don't lose the
3678 second one's ellipsis. */
3679 if (invis_p == 2)
3680 display_ellipsis_p = 1;
3682 while (invis_p);
3684 /* The position newpos is now either ZV or on visible text. */
3685 IT_CHARPOS (*it) = newpos;
3686 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3688 /* If there are before-strings at the start of invisible
3689 text, and the text is invisible because of a text
3690 property, arrange to show before-strings because 20.x did
3691 it that way. (If the text is invisible because of an
3692 overlay property instead of a text property, this is
3693 already handled in the overlay code.) */
3694 if (NILP (overlay)
3695 && get_overlay_strings (it, start_charpos))
3697 handled = HANDLED_RECOMPUTE_PROPS;
3698 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3700 else if (display_ellipsis_p)
3702 /* Make sure that the glyphs of the ellipsis will get
3703 correct `charpos' values. If we would not update
3704 it->position here, the glyphs would belong to the
3705 last visible character _before_ the invisible
3706 text, which confuses `set_cursor_from_row'.
3708 We use the last invisible position instead of the
3709 first because this way the cursor is always drawn on
3710 the first "." of the ellipsis, whenever PT is inside
3711 the invisible text. Otherwise the cursor would be
3712 placed _after_ the ellipsis when the point is after the
3713 first invisible character. */
3714 if (!STRINGP (it->object))
3716 it->position.charpos = IT_CHARPOS (*it) - 1;
3717 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3719 setup_for_ellipsis (it, 0);
3724 return handled;
3728 /* Make iterator IT return `...' next.
3729 Replaces LEN characters from buffer. */
3731 static void
3732 setup_for_ellipsis (it, len)
3733 struct it *it;
3734 int len;
3736 /* Use the display table definition for `...'. Invalid glyphs
3737 will be handled by the method returning elements from dpvec. */
3738 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3740 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3741 it->dpvec = v->contents;
3742 it->dpend = v->contents + v->size;
3744 else
3746 /* Default `...'. */
3747 it->dpvec = default_invis_vector;
3748 it->dpend = default_invis_vector + 3;
3751 it->dpvec_char_len = len;
3752 it->current.dpvec_index = 0;
3753 it->dpvec_face_id = -1;
3755 /* Remember the current face id in case glyphs specify faces.
3756 IT's face is restored in set_iterator_to_next.
3757 saved_face_id was set to preceding char's face in handle_stop. */
3758 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3759 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3761 it->method = GET_FROM_DISPLAY_VECTOR;
3762 it->ellipsis_p = 1;
3767 /***********************************************************************
3768 'display' property
3769 ***********************************************************************/
3771 /* Set up iterator IT from `display' property at its current position.
3772 Called from handle_stop.
3773 We return HANDLED_RETURN if some part of the display property
3774 overrides the display of the buffer text itself.
3775 Otherwise we return HANDLED_NORMALLY. */
3777 static enum prop_handled
3778 handle_display_prop (it)
3779 struct it *it;
3781 Lisp_Object prop, object;
3782 struct text_pos *position;
3783 /* Nonzero if some property replaces the display of the text itself. */
3784 int display_replaced_p = 0;
3786 if (STRINGP (it->string))
3788 object = it->string;
3789 position = &it->current.string_pos;
3791 else
3793 XSETWINDOW (object, it->w);
3794 position = &it->current.pos;
3797 /* Reset those iterator values set from display property values. */
3798 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3799 it->space_width = Qnil;
3800 it->font_height = Qnil;
3801 it->voffset = 0;
3803 /* We don't support recursive `display' properties, i.e. string
3804 values that have a string `display' property, that have a string
3805 `display' property etc. */
3806 if (!it->string_from_display_prop_p)
3807 it->area = TEXT_AREA;
3809 prop = Fget_char_property (make_number (position->charpos),
3810 Qdisplay, object);
3811 if (NILP (prop))
3812 return HANDLED_NORMALLY;
3814 if (!STRINGP (it->string))
3815 object = it->w->buffer;
3817 if (CONSP (prop)
3818 /* Simple properties. */
3819 && !EQ (XCAR (prop), Qimage)
3820 && !EQ (XCAR (prop), Qspace)
3821 && !EQ (XCAR (prop), Qwhen)
3822 && !EQ (XCAR (prop), Qslice)
3823 && !EQ (XCAR (prop), Qspace_width)
3824 && !EQ (XCAR (prop), Qheight)
3825 && !EQ (XCAR (prop), Qraise)
3826 /* Marginal area specifications. */
3827 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3828 && !EQ (XCAR (prop), Qleft_fringe)
3829 && !EQ (XCAR (prop), Qright_fringe)
3830 && !NILP (XCAR (prop)))
3832 for (; CONSP (prop); prop = XCDR (prop))
3834 if (handle_single_display_spec (it, XCAR (prop), object,
3835 position, display_replaced_p))
3836 display_replaced_p = 1;
3839 else if (VECTORP (prop))
3841 int i;
3842 for (i = 0; i < ASIZE (prop); ++i)
3843 if (handle_single_display_spec (it, AREF (prop, i), object,
3844 position, display_replaced_p))
3845 display_replaced_p = 1;
3847 else
3849 int ret = handle_single_display_spec (it, prop, object, position, 0);
3850 if (ret < 0) /* Replaced by "", i.e. nothing. */
3851 return HANDLED_RECOMPUTE_PROPS;
3852 if (ret)
3853 display_replaced_p = 1;
3856 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3860 /* Value is the position of the end of the `display' property starting
3861 at START_POS in OBJECT. */
3863 static struct text_pos
3864 display_prop_end (it, object, start_pos)
3865 struct it *it;
3866 Lisp_Object object;
3867 struct text_pos start_pos;
3869 Lisp_Object end;
3870 struct text_pos end_pos;
3872 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3873 Qdisplay, object, Qnil);
3874 CHARPOS (end_pos) = XFASTINT (end);
3875 if (STRINGP (object))
3876 compute_string_pos (&end_pos, start_pos, it->string);
3877 else
3878 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3880 return end_pos;
3884 /* Set up IT from a single `display' specification PROP. OBJECT
3885 is the object in which the `display' property was found. *POSITION
3886 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3887 means that we previously saw a display specification which already
3888 replaced text display with something else, for example an image;
3889 we ignore such properties after the first one has been processed.
3891 If PROP is a `space' or `image' specification, and in some other
3892 cases too, set *POSITION to the position where the `display'
3893 property ends.
3895 Value is non-zero if something was found which replaces the display
3896 of buffer or string text. Specifically, the value is -1 if that
3897 "something" is "nothing". */
3899 static int
3900 handle_single_display_spec (it, spec, object, position,
3901 display_replaced_before_p)
3902 struct it *it;
3903 Lisp_Object spec;
3904 Lisp_Object object;
3905 struct text_pos *position;
3906 int display_replaced_before_p;
3908 Lisp_Object form;
3909 Lisp_Object location, value;
3910 struct text_pos start_pos, save_pos;
3911 int valid_p;
3913 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3914 If the result is non-nil, use VALUE instead of SPEC. */
3915 form = Qt;
3916 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3918 spec = XCDR (spec);
3919 if (!CONSP (spec))
3920 return 0;
3921 form = XCAR (spec);
3922 spec = XCDR (spec);
3925 if (!NILP (form) && !EQ (form, Qt))
3927 int count = SPECPDL_INDEX ();
3928 struct gcpro gcpro1;
3930 /* Bind `object' to the object having the `display' property, a
3931 buffer or string. Bind `position' to the position in the
3932 object where the property was found, and `buffer-position'
3933 to the current position in the buffer. */
3934 specbind (Qobject, object);
3935 specbind (Qposition, make_number (CHARPOS (*position)));
3936 specbind (Qbuffer_position,
3937 make_number (STRINGP (object)
3938 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3939 GCPRO1 (form);
3940 form = safe_eval (form);
3941 UNGCPRO;
3942 unbind_to (count, Qnil);
3945 if (NILP (form))
3946 return 0;
3948 /* Handle `(height HEIGHT)' specifications. */
3949 if (CONSP (spec)
3950 && EQ (XCAR (spec), Qheight)
3951 && CONSP (XCDR (spec)))
3953 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3954 return 0;
3956 it->font_height = XCAR (XCDR (spec));
3957 if (!NILP (it->font_height))
3959 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3960 int new_height = -1;
3962 if (CONSP (it->font_height)
3963 && (EQ (XCAR (it->font_height), Qplus)
3964 || EQ (XCAR (it->font_height), Qminus))
3965 && CONSP (XCDR (it->font_height))
3966 && INTEGERP (XCAR (XCDR (it->font_height))))
3968 /* `(+ N)' or `(- N)' where N is an integer. */
3969 int steps = XINT (XCAR (XCDR (it->font_height)));
3970 if (EQ (XCAR (it->font_height), Qplus))
3971 steps = - steps;
3972 it->face_id = smaller_face (it->f, it->face_id, steps);
3974 else if (FUNCTIONP (it->font_height))
3976 /* Call function with current height as argument.
3977 Value is the new height. */
3978 Lisp_Object height;
3979 height = safe_call1 (it->font_height,
3980 face->lface[LFACE_HEIGHT_INDEX]);
3981 if (NUMBERP (height))
3982 new_height = XFLOATINT (height);
3984 else if (NUMBERP (it->font_height))
3986 /* Value is a multiple of the canonical char height. */
3987 struct face *face;
3989 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3990 new_height = (XFLOATINT (it->font_height)
3991 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3993 else
3995 /* Evaluate IT->font_height with `height' bound to the
3996 current specified height to get the new height. */
3997 int count = SPECPDL_INDEX ();
3999 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4000 value = safe_eval (it->font_height);
4001 unbind_to (count, Qnil);
4003 if (NUMBERP (value))
4004 new_height = XFLOATINT (value);
4007 if (new_height > 0)
4008 it->face_id = face_with_height (it->f, it->face_id, new_height);
4011 return 0;
4014 /* Handle `(space_width WIDTH)'. */
4015 if (CONSP (spec)
4016 && EQ (XCAR (spec), Qspace_width)
4017 && CONSP (XCDR (spec)))
4019 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4020 return 0;
4022 value = XCAR (XCDR (spec));
4023 if (NUMBERP (value) && XFLOATINT (value) > 0)
4024 it->space_width = value;
4026 return 0;
4029 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4030 if (CONSP (spec)
4031 && EQ (XCAR (spec), Qslice))
4033 Lisp_Object tem;
4035 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4036 return 0;
4038 if (tem = XCDR (spec), CONSP (tem))
4040 it->slice.x = XCAR (tem);
4041 if (tem = XCDR (tem), CONSP (tem))
4043 it->slice.y = XCAR (tem);
4044 if (tem = XCDR (tem), CONSP (tem))
4046 it->slice.width = XCAR (tem);
4047 if (tem = XCDR (tem), CONSP (tem))
4048 it->slice.height = XCAR (tem);
4053 return 0;
4056 /* Handle `(raise FACTOR)'. */
4057 if (CONSP (spec)
4058 && EQ (XCAR (spec), Qraise)
4059 && CONSP (XCDR (spec)))
4061 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4062 return 0;
4064 #ifdef HAVE_WINDOW_SYSTEM
4065 value = XCAR (XCDR (spec));
4066 if (NUMBERP (value))
4068 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4069 it->voffset = - (XFLOATINT (value)
4070 * (FONT_HEIGHT (face->font)));
4072 #endif /* HAVE_WINDOW_SYSTEM */
4074 return 0;
4077 /* Don't handle the other kinds of display specifications
4078 inside a string that we got from a `display' property. */
4079 if (it->string_from_display_prop_p)
4080 return 0;
4082 /* Characters having this form of property are not displayed, so
4083 we have to find the end of the property. */
4084 start_pos = *position;
4085 *position = display_prop_end (it, object, start_pos);
4086 value = Qnil;
4088 /* Stop the scan at that end position--we assume that all
4089 text properties change there. */
4090 it->stop_charpos = position->charpos;
4092 /* Handle `(left-fringe BITMAP [FACE])'
4093 and `(right-fringe BITMAP [FACE])'. */
4094 if (CONSP (spec)
4095 && (EQ (XCAR (spec), Qleft_fringe)
4096 || EQ (XCAR (spec), Qright_fringe))
4097 && CONSP (XCDR (spec)))
4099 int face_id = DEFAULT_FACE_ID;
4100 int fringe_bitmap;
4102 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4103 /* If we return here, POSITION has been advanced
4104 across the text with this property. */
4105 return 0;
4107 #ifdef HAVE_WINDOW_SYSTEM
4108 value = XCAR (XCDR (spec));
4109 if (!SYMBOLP (value)
4110 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4111 /* If we return here, POSITION has been advanced
4112 across the text with this property. */
4113 return 0;
4115 if (CONSP (XCDR (XCDR (spec))))
4117 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4118 int face_id2 = lookup_derived_face (it->f, face_name,
4119 'A', FRINGE_FACE_ID, 0);
4120 if (face_id2 >= 0)
4121 face_id = face_id2;
4124 /* Save current settings of IT so that we can restore them
4125 when we are finished with the glyph property value. */
4127 save_pos = it->position;
4128 it->position = *position;
4129 push_it (it);
4130 it->position = save_pos;
4132 it->area = TEXT_AREA;
4133 it->what = IT_IMAGE;
4134 it->image_id = -1; /* no image */
4135 it->position = start_pos;
4136 it->object = NILP (object) ? it->w->buffer : object;
4137 it->method = GET_FROM_IMAGE;
4138 it->face_id = face_id;
4140 /* Say that we haven't consumed the characters with
4141 `display' property yet. The call to pop_it in
4142 set_iterator_to_next will clean this up. */
4143 *position = start_pos;
4145 if (EQ (XCAR (spec), Qleft_fringe))
4147 it->left_user_fringe_bitmap = fringe_bitmap;
4148 it->left_user_fringe_face_id = face_id;
4150 else
4152 it->right_user_fringe_bitmap = fringe_bitmap;
4153 it->right_user_fringe_face_id = face_id;
4155 #endif /* HAVE_WINDOW_SYSTEM */
4156 return 1;
4159 /* Prepare to handle `((margin left-margin) ...)',
4160 `((margin right-margin) ...)' and `((margin nil) ...)'
4161 prefixes for display specifications. */
4162 location = Qunbound;
4163 if (CONSP (spec) && CONSP (XCAR (spec)))
4165 Lisp_Object tem;
4167 value = XCDR (spec);
4168 if (CONSP (value))
4169 value = XCAR (value);
4171 tem = XCAR (spec);
4172 if (EQ (XCAR (tem), Qmargin)
4173 && (tem = XCDR (tem),
4174 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4175 (NILP (tem)
4176 || EQ (tem, Qleft_margin)
4177 || EQ (tem, Qright_margin))))
4178 location = tem;
4181 if (EQ (location, Qunbound))
4183 location = Qnil;
4184 value = spec;
4187 /* After this point, VALUE is the property after any
4188 margin prefix has been stripped. It must be a string,
4189 an image specification, or `(space ...)'.
4191 LOCATION specifies where to display: `left-margin',
4192 `right-margin' or nil. */
4194 valid_p = (STRINGP (value)
4195 #ifdef HAVE_WINDOW_SYSTEM
4196 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4197 #endif /* not HAVE_WINDOW_SYSTEM */
4198 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4200 if (valid_p && !display_replaced_before_p)
4202 /* Save current settings of IT so that we can restore them
4203 when we are finished with the glyph property value. */
4204 save_pos = it->position;
4205 it->position = *position;
4206 push_it (it);
4207 it->position = save_pos;
4209 if (NILP (location))
4210 it->area = TEXT_AREA;
4211 else if (EQ (location, Qleft_margin))
4212 it->area = LEFT_MARGIN_AREA;
4213 else
4214 it->area = RIGHT_MARGIN_AREA;
4216 if (STRINGP (value))
4218 if (SCHARS (value) == 0)
4220 pop_it (it);
4221 return -1; /* Replaced by "", i.e. nothing. */
4223 it->string = value;
4224 it->multibyte_p = STRING_MULTIBYTE (it->string);
4225 it->current.overlay_string_index = -1;
4226 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4227 it->end_charpos = it->string_nchars = SCHARS (it->string);
4228 it->method = GET_FROM_STRING;
4229 it->stop_charpos = 0;
4230 it->string_from_display_prop_p = 1;
4231 /* Say that we haven't consumed the characters with
4232 `display' property yet. The call to pop_it in
4233 set_iterator_to_next will clean this up. */
4234 *position = start_pos;
4236 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4238 it->method = GET_FROM_STRETCH;
4239 it->object = value;
4240 *position = it->position = start_pos;
4242 #ifdef HAVE_WINDOW_SYSTEM
4243 else
4245 it->what = IT_IMAGE;
4246 it->image_id = lookup_image (it->f, value);
4247 it->position = start_pos;
4248 it->object = NILP (object) ? it->w->buffer : object;
4249 it->method = GET_FROM_IMAGE;
4251 /* Say that we haven't consumed the characters with
4252 `display' property yet. The call to pop_it in
4253 set_iterator_to_next will clean this up. */
4254 *position = start_pos;
4256 #endif /* HAVE_WINDOW_SYSTEM */
4258 return 1;
4261 /* Invalid property or property not supported. Restore
4262 POSITION to what it was before. */
4263 *position = start_pos;
4264 return 0;
4268 /* Check if SPEC is a display specification value whose text should be
4269 treated as intangible. */
4271 static int
4272 single_display_spec_intangible_p (prop)
4273 Lisp_Object prop;
4275 /* Skip over `when FORM'. */
4276 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4278 prop = XCDR (prop);
4279 if (!CONSP (prop))
4280 return 0;
4281 prop = XCDR (prop);
4284 if (STRINGP (prop))
4285 return 1;
4287 if (!CONSP (prop))
4288 return 0;
4290 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4291 we don't need to treat text as intangible. */
4292 if (EQ (XCAR (prop), Qmargin))
4294 prop = XCDR (prop);
4295 if (!CONSP (prop))
4296 return 0;
4298 prop = XCDR (prop);
4299 if (!CONSP (prop)
4300 || EQ (XCAR (prop), Qleft_margin)
4301 || EQ (XCAR (prop), Qright_margin))
4302 return 0;
4305 return (CONSP (prop)
4306 && (EQ (XCAR (prop), Qimage)
4307 || EQ (XCAR (prop), Qspace)));
4311 /* Check if PROP is a display property value whose text should be
4312 treated as intangible. */
4315 display_prop_intangible_p (prop)
4316 Lisp_Object prop;
4318 if (CONSP (prop)
4319 && CONSP (XCAR (prop))
4320 && !EQ (Qmargin, XCAR (XCAR (prop))))
4322 /* A list of sub-properties. */
4323 while (CONSP (prop))
4325 if (single_display_spec_intangible_p (XCAR (prop)))
4326 return 1;
4327 prop = XCDR (prop);
4330 else if (VECTORP (prop))
4332 /* A vector of sub-properties. */
4333 int i;
4334 for (i = 0; i < ASIZE (prop); ++i)
4335 if (single_display_spec_intangible_p (AREF (prop, i)))
4336 return 1;
4338 else
4339 return single_display_spec_intangible_p (prop);
4341 return 0;
4345 /* Return 1 if PROP is a display sub-property value containing STRING. */
4347 static int
4348 single_display_spec_string_p (prop, string)
4349 Lisp_Object prop, string;
4351 if (EQ (string, prop))
4352 return 1;
4354 /* Skip over `when FORM'. */
4355 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4357 prop = XCDR (prop);
4358 if (!CONSP (prop))
4359 return 0;
4360 prop = XCDR (prop);
4363 if (CONSP (prop))
4364 /* Skip over `margin LOCATION'. */
4365 if (EQ (XCAR (prop), Qmargin))
4367 prop = XCDR (prop);
4368 if (!CONSP (prop))
4369 return 0;
4371 prop = XCDR (prop);
4372 if (!CONSP (prop))
4373 return 0;
4376 return CONSP (prop) && EQ (XCAR (prop), string);
4380 /* Return 1 if STRING appears in the `display' property PROP. */
4382 static int
4383 display_prop_string_p (prop, string)
4384 Lisp_Object prop, string;
4386 if (CONSP (prop)
4387 && CONSP (XCAR (prop))
4388 && !EQ (Qmargin, XCAR (XCAR (prop))))
4390 /* A list of sub-properties. */
4391 while (CONSP (prop))
4393 if (single_display_spec_string_p (XCAR (prop), string))
4394 return 1;
4395 prop = XCDR (prop);
4398 else if (VECTORP (prop))
4400 /* A vector of sub-properties. */
4401 int i;
4402 for (i = 0; i < ASIZE (prop); ++i)
4403 if (single_display_spec_string_p (AREF (prop, i), string))
4404 return 1;
4406 else
4407 return single_display_spec_string_p (prop, string);
4409 return 0;
4413 /* Determine from which buffer position in W's buffer STRING comes
4414 from. AROUND_CHARPOS is an approximate position where it could
4415 be from. Value is the buffer position or 0 if it couldn't be
4416 determined.
4418 W's buffer must be current.
4420 This function is necessary because we don't record buffer positions
4421 in glyphs generated from strings (to keep struct glyph small).
4422 This function may only use code that doesn't eval because it is
4423 called asynchronously from note_mouse_highlight. */
4426 string_buffer_position (w, string, around_charpos)
4427 struct window *w;
4428 Lisp_Object string;
4429 int around_charpos;
4431 Lisp_Object limit, prop, pos;
4432 const int MAX_DISTANCE = 1000;
4433 int found = 0;
4435 pos = make_number (around_charpos);
4436 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4437 while (!found && !EQ (pos, limit))
4439 prop = Fget_char_property (pos, Qdisplay, Qnil);
4440 if (!NILP (prop) && display_prop_string_p (prop, string))
4441 found = 1;
4442 else
4443 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4446 if (!found)
4448 pos = make_number (around_charpos);
4449 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4450 while (!found && !EQ (pos, limit))
4452 prop = Fget_char_property (pos, Qdisplay, Qnil);
4453 if (!NILP (prop) && display_prop_string_p (prop, string))
4454 found = 1;
4455 else
4456 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4457 limit);
4461 return found ? XINT (pos) : 0;
4466 /***********************************************************************
4467 `composition' property
4468 ***********************************************************************/
4470 /* Set up iterator IT from `composition' property at its current
4471 position. Called from handle_stop. */
4473 static enum prop_handled
4474 handle_composition_prop (it)
4475 struct it *it;
4477 Lisp_Object prop, string;
4478 int pos, pos_byte, end;
4479 enum prop_handled handled = HANDLED_NORMALLY;
4481 if (STRINGP (it->string))
4483 pos = IT_STRING_CHARPOS (*it);
4484 pos_byte = IT_STRING_BYTEPOS (*it);
4485 string = it->string;
4487 else
4489 pos = IT_CHARPOS (*it);
4490 pos_byte = IT_BYTEPOS (*it);
4491 string = Qnil;
4494 /* If there's a valid composition and point is not inside of the
4495 composition (in the case that the composition is from the current
4496 buffer), draw a glyph composed from the composition components. */
4497 if (find_composition (pos, -1, &pos, &end, &prop, string)
4498 && COMPOSITION_VALID_P (pos, end, prop)
4499 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4501 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4503 if (id >= 0)
4505 struct composition *cmp = composition_table[id];
4507 if (cmp->glyph_len == 0)
4509 /* No glyph. */
4510 if (STRINGP (it->string))
4512 IT_STRING_CHARPOS (*it) = end;
4513 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4514 end);
4516 else
4518 IT_CHARPOS (*it) = end;
4519 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4521 return HANDLED_RECOMPUTE_PROPS;
4524 it->stop_charpos = end;
4525 push_it (it);
4527 it->method = GET_FROM_COMPOSITION;
4528 it->cmp_id = id;
4529 it->cmp_len = COMPOSITION_LENGTH (prop);
4530 /* For a terminal, draw only the first character of the
4531 components. */
4532 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4533 it->len = (STRINGP (it->string)
4534 ? string_char_to_byte (it->string, end)
4535 : CHAR_TO_BYTE (end)) - pos_byte;
4536 handled = HANDLED_RETURN;
4540 return handled;
4545 /***********************************************************************
4546 Overlay strings
4547 ***********************************************************************/
4549 /* The following structure is used to record overlay strings for
4550 later sorting in load_overlay_strings. */
4552 struct overlay_entry
4554 Lisp_Object overlay;
4555 Lisp_Object string;
4556 int priority;
4557 int after_string_p;
4561 /* Set up iterator IT from overlay strings at its current position.
4562 Called from handle_stop. */
4564 static enum prop_handled
4565 handle_overlay_change (it)
4566 struct it *it;
4568 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4569 return HANDLED_RECOMPUTE_PROPS;
4570 else
4571 return HANDLED_NORMALLY;
4575 /* Set up the next overlay string for delivery by IT, if there is an
4576 overlay string to deliver. Called by set_iterator_to_next when the
4577 end of the current overlay string is reached. If there are more
4578 overlay strings to display, IT->string and
4579 IT->current.overlay_string_index are set appropriately here.
4580 Otherwise IT->string is set to nil. */
4582 static void
4583 next_overlay_string (it)
4584 struct it *it;
4586 ++it->current.overlay_string_index;
4587 if (it->current.overlay_string_index == it->n_overlay_strings)
4589 /* No more overlay strings. Restore IT's settings to what
4590 they were before overlay strings were processed, and
4591 continue to deliver from current_buffer. */
4592 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4594 pop_it (it);
4595 xassert (it->sp > 0
4596 || it->method == GET_FROM_COMPOSITION
4597 || (NILP (it->string)
4598 && it->method == GET_FROM_BUFFER
4599 && it->stop_charpos >= BEGV
4600 && it->stop_charpos <= it->end_charpos));
4601 it->current.overlay_string_index = -1;
4602 it->n_overlay_strings = 0;
4604 /* If we're at the end of the buffer, record that we have
4605 processed the overlay strings there already, so that
4606 next_element_from_buffer doesn't try it again. */
4607 if (IT_CHARPOS (*it) >= it->end_charpos)
4608 it->overlay_strings_at_end_processed_p = 1;
4610 /* If we have to display `...' for invisible text, set
4611 the iterator up for that. */
4612 if (display_ellipsis_p)
4613 setup_for_ellipsis (it, 0);
4615 else
4617 /* There are more overlay strings to process. If
4618 IT->current.overlay_string_index has advanced to a position
4619 where we must load IT->overlay_strings with more strings, do
4620 it. */
4621 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4623 if (it->current.overlay_string_index && i == 0)
4624 load_overlay_strings (it, 0);
4626 /* Initialize IT to deliver display elements from the overlay
4627 string. */
4628 it->string = it->overlay_strings[i];
4629 it->multibyte_p = STRING_MULTIBYTE (it->string);
4630 SET_TEXT_POS (it->current.string_pos, 0, 0);
4631 it->method = GET_FROM_STRING;
4632 it->stop_charpos = 0;
4635 CHECK_IT (it);
4639 /* Compare two overlay_entry structures E1 and E2. Used as a
4640 comparison function for qsort in load_overlay_strings. Overlay
4641 strings for the same position are sorted so that
4643 1. All after-strings come in front of before-strings, except
4644 when they come from the same overlay.
4646 2. Within after-strings, strings are sorted so that overlay strings
4647 from overlays with higher priorities come first.
4649 2. Within before-strings, strings are sorted so that overlay
4650 strings from overlays with higher priorities come last.
4652 Value is analogous to strcmp. */
4655 static int
4656 compare_overlay_entries (e1, e2)
4657 void *e1, *e2;
4659 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4660 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4661 int result;
4663 if (entry1->after_string_p != entry2->after_string_p)
4665 /* Let after-strings appear in front of before-strings if
4666 they come from different overlays. */
4667 if (EQ (entry1->overlay, entry2->overlay))
4668 result = entry1->after_string_p ? 1 : -1;
4669 else
4670 result = entry1->after_string_p ? -1 : 1;
4672 else if (entry1->after_string_p)
4673 /* After-strings sorted in order of decreasing priority. */
4674 result = entry2->priority - entry1->priority;
4675 else
4676 /* Before-strings sorted in order of increasing priority. */
4677 result = entry1->priority - entry2->priority;
4679 return result;
4683 /* Load the vector IT->overlay_strings with overlay strings from IT's
4684 current buffer position, or from CHARPOS if that is > 0. Set
4685 IT->n_overlays to the total number of overlay strings found.
4687 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4688 a time. On entry into load_overlay_strings,
4689 IT->current.overlay_string_index gives the number of overlay
4690 strings that have already been loaded by previous calls to this
4691 function.
4693 IT->add_overlay_start contains an additional overlay start
4694 position to consider for taking overlay strings from, if non-zero.
4695 This position comes into play when the overlay has an `invisible'
4696 property, and both before and after-strings. When we've skipped to
4697 the end of the overlay, because of its `invisible' property, we
4698 nevertheless want its before-string to appear.
4699 IT->add_overlay_start will contain the overlay start position
4700 in this case.
4702 Overlay strings are sorted so that after-string strings come in
4703 front of before-string strings. Within before and after-strings,
4704 strings are sorted by overlay priority. See also function
4705 compare_overlay_entries. */
4707 static void
4708 load_overlay_strings (it, charpos)
4709 struct it *it;
4710 int charpos;
4712 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4713 Lisp_Object overlay, window, str, invisible;
4714 struct Lisp_Overlay *ov;
4715 int start, end;
4716 int size = 20;
4717 int n = 0, i, j, invis_p;
4718 struct overlay_entry *entries
4719 = (struct overlay_entry *) alloca (size * sizeof *entries);
4721 if (charpos <= 0)
4722 charpos = IT_CHARPOS (*it);
4724 /* Append the overlay string STRING of overlay OVERLAY to vector
4725 `entries' which has size `size' and currently contains `n'
4726 elements. AFTER_P non-zero means STRING is an after-string of
4727 OVERLAY. */
4728 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4729 do \
4731 Lisp_Object priority; \
4733 if (n == size) \
4735 int new_size = 2 * size; \
4736 struct overlay_entry *old = entries; \
4737 entries = \
4738 (struct overlay_entry *) alloca (new_size \
4739 * sizeof *entries); \
4740 bcopy (old, entries, size * sizeof *entries); \
4741 size = new_size; \
4744 entries[n].string = (STRING); \
4745 entries[n].overlay = (OVERLAY); \
4746 priority = Foverlay_get ((OVERLAY), Qpriority); \
4747 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4748 entries[n].after_string_p = (AFTER_P); \
4749 ++n; \
4751 while (0)
4753 /* Process overlay before the overlay center. */
4754 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4756 XSETMISC (overlay, ov);
4757 xassert (OVERLAYP (overlay));
4758 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4759 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4761 if (end < charpos)
4762 break;
4764 /* Skip this overlay if it doesn't start or end at IT's current
4765 position. */
4766 if (end != charpos && start != charpos)
4767 continue;
4769 /* Skip this overlay if it doesn't apply to IT->w. */
4770 window = Foverlay_get (overlay, Qwindow);
4771 if (WINDOWP (window) && XWINDOW (window) != it->w)
4772 continue;
4774 /* If the text ``under'' the overlay is invisible, both before-
4775 and after-strings from this overlay are visible; start and
4776 end position are indistinguishable. */
4777 invisible = Foverlay_get (overlay, Qinvisible);
4778 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4780 /* If overlay has a non-empty before-string, record it. */
4781 if ((start == charpos || (end == charpos && invis_p))
4782 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4783 && SCHARS (str))
4784 RECORD_OVERLAY_STRING (overlay, str, 0);
4786 /* If overlay has a non-empty after-string, record it. */
4787 if ((end == charpos || (start == charpos && invis_p))
4788 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4789 && SCHARS (str))
4790 RECORD_OVERLAY_STRING (overlay, str, 1);
4793 /* Process overlays after the overlay center. */
4794 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4796 XSETMISC (overlay, ov);
4797 xassert (OVERLAYP (overlay));
4798 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4799 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4801 if (start > charpos)
4802 break;
4804 /* Skip this overlay if it doesn't start or end at IT's current
4805 position. */
4806 if (end != charpos && start != charpos)
4807 continue;
4809 /* Skip this overlay if it doesn't apply to IT->w. */
4810 window = Foverlay_get (overlay, Qwindow);
4811 if (WINDOWP (window) && XWINDOW (window) != it->w)
4812 continue;
4814 /* If the text ``under'' the overlay is invisible, it has a zero
4815 dimension, and both before- and after-strings apply. */
4816 invisible = Foverlay_get (overlay, Qinvisible);
4817 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4819 /* If overlay has a non-empty before-string, record it. */
4820 if ((start == charpos || (end == charpos && invis_p))
4821 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4822 && SCHARS (str))
4823 RECORD_OVERLAY_STRING (overlay, str, 0);
4825 /* If overlay has a non-empty after-string, record it. */
4826 if ((end == charpos || (start == charpos && invis_p))
4827 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4828 && SCHARS (str))
4829 RECORD_OVERLAY_STRING (overlay, str, 1);
4832 #undef RECORD_OVERLAY_STRING
4834 /* Sort entries. */
4835 if (n > 1)
4836 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4838 /* Record the total number of strings to process. */
4839 it->n_overlay_strings = n;
4841 /* IT->current.overlay_string_index is the number of overlay strings
4842 that have already been consumed by IT. Copy some of the
4843 remaining overlay strings to IT->overlay_strings. */
4844 i = 0;
4845 j = it->current.overlay_string_index;
4846 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4847 it->overlay_strings[i++] = entries[j++].string;
4849 CHECK_IT (it);
4853 /* Get the first chunk of overlay strings at IT's current buffer
4854 position, or at CHARPOS if that is > 0. Value is non-zero if at
4855 least one overlay string was found. */
4857 static int
4858 get_overlay_strings_1 (it, charpos, compute_stop_p)
4859 struct it *it;
4860 int charpos;
4862 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4863 process. This fills IT->overlay_strings with strings, and sets
4864 IT->n_overlay_strings to the total number of strings to process.
4865 IT->pos.overlay_string_index has to be set temporarily to zero
4866 because load_overlay_strings needs this; it must be set to -1
4867 when no overlay strings are found because a zero value would
4868 indicate a position in the first overlay string. */
4869 it->current.overlay_string_index = 0;
4870 load_overlay_strings (it, charpos);
4872 /* If we found overlay strings, set up IT to deliver display
4873 elements from the first one. Otherwise set up IT to deliver
4874 from current_buffer. */
4875 if (it->n_overlay_strings)
4877 /* Make sure we know settings in current_buffer, so that we can
4878 restore meaningful values when we're done with the overlay
4879 strings. */
4880 if (compute_stop_p)
4881 compute_stop_pos (it);
4882 xassert (it->face_id >= 0);
4884 /* Save IT's settings. They are restored after all overlay
4885 strings have been processed. */
4886 xassert (!compute_stop_p || it->sp == 0);
4887 push_it (it);
4889 /* Set up IT to deliver display elements from the first overlay
4890 string. */
4891 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4892 it->string = it->overlay_strings[0];
4893 it->stop_charpos = 0;
4894 xassert (STRINGP (it->string));
4895 it->end_charpos = SCHARS (it->string);
4896 it->multibyte_p = STRING_MULTIBYTE (it->string);
4897 it->method = GET_FROM_STRING;
4898 return 1;
4901 it->current.overlay_string_index = -1;
4902 return 0;
4905 static int
4906 get_overlay_strings (it, charpos)
4907 struct it *it;
4908 int charpos;
4910 it->string = Qnil;
4911 it->method = GET_FROM_BUFFER;
4913 (void) get_overlay_strings_1 (it, charpos, 1);
4915 CHECK_IT (it);
4917 /* Value is non-zero if we found at least one overlay string. */
4918 return STRINGP (it->string);
4923 /***********************************************************************
4924 Saving and restoring state
4925 ***********************************************************************/
4927 /* Save current settings of IT on IT->stack. Called, for example,
4928 before setting up IT for an overlay string, to be able to restore
4929 IT's settings to what they were after the overlay string has been
4930 processed. */
4932 static void
4933 push_it (it)
4934 struct it *it;
4936 struct iterator_stack_entry *p;
4938 xassert (it->sp < IT_STACK_SIZE);
4939 p = it->stack + it->sp;
4941 p->stop_charpos = it->stop_charpos;
4942 xassert (it->face_id >= 0);
4943 p->face_id = it->face_id;
4944 p->string = it->string;
4945 p->method = it->method;
4946 switch (p->method)
4948 case GET_FROM_IMAGE:
4949 p->u.image.object = it->object;
4950 p->u.image.image_id = it->image_id;
4951 p->u.image.slice = it->slice;
4952 break;
4953 case GET_FROM_COMPOSITION:
4954 p->u.comp.object = it->object;
4955 p->u.comp.c = it->c;
4956 p->u.comp.len = it->len;
4957 p->u.comp.cmp_id = it->cmp_id;
4958 p->u.comp.cmp_len = it->cmp_len;
4959 break;
4960 case GET_FROM_STRETCH:
4961 p->u.stretch.object = it->object;
4962 break;
4964 p->position = it->position;
4965 p->current = it->current;
4966 p->end_charpos = it->end_charpos;
4967 p->string_nchars = it->string_nchars;
4968 p->area = it->area;
4969 p->multibyte_p = it->multibyte_p;
4970 p->space_width = it->space_width;
4971 p->font_height = it->font_height;
4972 p->voffset = it->voffset;
4973 p->string_from_display_prop_p = it->string_from_display_prop_p;
4974 p->display_ellipsis_p = 0;
4975 ++it->sp;
4979 /* Restore IT's settings from IT->stack. Called, for example, when no
4980 more overlay strings must be processed, and we return to delivering
4981 display elements from a buffer, or when the end of a string from a
4982 `display' property is reached and we return to delivering display
4983 elements from an overlay string, or from a buffer. */
4985 static void
4986 pop_it (it)
4987 struct it *it;
4989 struct iterator_stack_entry *p;
4991 xassert (it->sp > 0);
4992 --it->sp;
4993 p = it->stack + it->sp;
4994 it->stop_charpos = p->stop_charpos;
4995 it->face_id = p->face_id;
4996 it->current = p->current;
4997 it->position = p->position;
4998 it->string = p->string;
4999 if (NILP (it->string))
5000 SET_TEXT_POS (it->current.string_pos, -1, -1);
5001 it->method = p->method;
5002 switch (it->method)
5004 case GET_FROM_IMAGE:
5005 it->image_id = p->u.image.image_id;
5006 it->object = p->u.image.object;
5007 it->slice = p->u.image.slice;
5008 break;
5009 case GET_FROM_COMPOSITION:
5010 it->object = p->u.comp.object;
5011 it->c = p->u.comp.c;
5012 it->len = p->u.comp.len;
5013 it->cmp_id = p->u.comp.cmp_id;
5014 it->cmp_len = p->u.comp.cmp_len;
5015 break;
5016 case GET_FROM_STRETCH:
5017 it->object = p->u.comp.object;
5018 break;
5019 case GET_FROM_BUFFER:
5020 it->object = it->w->buffer;
5021 break;
5022 case GET_FROM_STRING:
5023 it->object = it->string;
5024 break;
5026 it->end_charpos = p->end_charpos;
5027 it->string_nchars = p->string_nchars;
5028 it->area = p->area;
5029 it->multibyte_p = p->multibyte_p;
5030 it->space_width = p->space_width;
5031 it->font_height = p->font_height;
5032 it->voffset = p->voffset;
5033 it->string_from_display_prop_p = p->string_from_display_prop_p;
5038 /***********************************************************************
5039 Moving over lines
5040 ***********************************************************************/
5042 /* Set IT's current position to the previous line start. */
5044 static void
5045 back_to_previous_line_start (it)
5046 struct it *it;
5048 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5049 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5053 /* Move IT to the next line start.
5055 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5056 we skipped over part of the text (as opposed to moving the iterator
5057 continuously over the text). Otherwise, don't change the value
5058 of *SKIPPED_P.
5060 Newlines may come from buffer text, overlay strings, or strings
5061 displayed via the `display' property. That's the reason we can't
5062 simply use find_next_newline_no_quit.
5064 Note that this function may not skip over invisible text that is so
5065 because of text properties and immediately follows a newline. If
5066 it would, function reseat_at_next_visible_line_start, when called
5067 from set_iterator_to_next, would effectively make invisible
5068 characters following a newline part of the wrong glyph row, which
5069 leads to wrong cursor motion. */
5071 static int
5072 forward_to_next_line_start (it, skipped_p)
5073 struct it *it;
5074 int *skipped_p;
5076 int old_selective, newline_found_p, n;
5077 const int MAX_NEWLINE_DISTANCE = 500;
5079 /* If already on a newline, just consume it to avoid unintended
5080 skipping over invisible text below. */
5081 if (it->what == IT_CHARACTER
5082 && it->c == '\n'
5083 && CHARPOS (it->position) == IT_CHARPOS (*it))
5085 set_iterator_to_next (it, 0);
5086 it->c = 0;
5087 return 1;
5090 /* Don't handle selective display in the following. It's (a)
5091 unnecessary because it's done by the caller, and (b) leads to an
5092 infinite recursion because next_element_from_ellipsis indirectly
5093 calls this function. */
5094 old_selective = it->selective;
5095 it->selective = 0;
5097 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5098 from buffer text. */
5099 for (n = newline_found_p = 0;
5100 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5101 n += STRINGP (it->string) ? 0 : 1)
5103 if (!get_next_display_element (it))
5104 return 0;
5105 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5106 set_iterator_to_next (it, 0);
5109 /* If we didn't find a newline near enough, see if we can use a
5110 short-cut. */
5111 if (!newline_found_p)
5113 int start = IT_CHARPOS (*it);
5114 int limit = find_next_newline_no_quit (start, 1);
5115 Lisp_Object pos;
5117 xassert (!STRINGP (it->string));
5119 /* If there isn't any `display' property in sight, and no
5120 overlays, we can just use the position of the newline in
5121 buffer text. */
5122 if (it->stop_charpos >= limit
5123 || ((pos = Fnext_single_property_change (make_number (start),
5124 Qdisplay,
5125 Qnil, make_number (limit)),
5126 NILP (pos))
5127 && next_overlay_change (start) == ZV))
5129 IT_CHARPOS (*it) = limit;
5130 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5131 *skipped_p = newline_found_p = 1;
5133 else
5135 while (get_next_display_element (it)
5136 && !newline_found_p)
5138 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5139 set_iterator_to_next (it, 0);
5144 it->selective = old_selective;
5145 return newline_found_p;
5149 /* Set IT's current position to the previous visible line start. Skip
5150 invisible text that is so either due to text properties or due to
5151 selective display. Caution: this does not change IT->current_x and
5152 IT->hpos. */
5154 static void
5155 back_to_previous_visible_line_start (it)
5156 struct it *it;
5158 while (IT_CHARPOS (*it) > BEGV)
5160 back_to_previous_line_start (it);
5162 if (IT_CHARPOS (*it) <= BEGV)
5163 break;
5165 /* If selective > 0, then lines indented more than that values
5166 are invisible. */
5167 if (it->selective > 0
5168 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5169 (double) it->selective)) /* iftc */
5170 continue;
5172 /* Check the newline before point for invisibility. */
5174 Lisp_Object prop;
5175 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5176 Qinvisible, it->window);
5177 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5178 continue;
5181 if (IT_CHARPOS (*it) <= BEGV)
5182 break;
5185 struct it it2;
5186 int pos;
5187 int beg, end;
5188 Lisp_Object val, overlay;
5190 /* If newline is part of a composition, continue from start of composition */
5191 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5192 && beg < IT_CHARPOS (*it))
5193 goto replaced;
5195 /* If newline is replaced by a display property, find start of overlay
5196 or interval and continue search from that point. */
5197 it2 = *it;
5198 pos = --IT_CHARPOS (it2);
5199 --IT_BYTEPOS (it2);
5200 it2.sp = 0;
5201 if (handle_display_prop (&it2) == HANDLED_RETURN
5202 && !NILP (val = get_char_property_and_overlay
5203 (make_number (pos), Qdisplay, Qnil, &overlay))
5204 && (OVERLAYP (overlay)
5205 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5206 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5207 goto replaced;
5209 /* Newline is not replaced by anything -- so we are done. */
5210 break;
5212 replaced:
5213 if (beg < BEGV)
5214 beg = BEGV;
5215 IT_CHARPOS (*it) = beg;
5216 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5220 it->continuation_lines_width = 0;
5222 xassert (IT_CHARPOS (*it) >= BEGV);
5223 xassert (IT_CHARPOS (*it) == BEGV
5224 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5225 CHECK_IT (it);
5229 /* Reseat iterator IT at the previous visible line start. Skip
5230 invisible text that is so either due to text properties or due to
5231 selective display. At the end, update IT's overlay information,
5232 face information etc. */
5234 void
5235 reseat_at_previous_visible_line_start (it)
5236 struct it *it;
5238 back_to_previous_visible_line_start (it);
5239 reseat (it, it->current.pos, 1);
5240 CHECK_IT (it);
5244 /* Reseat iterator IT on the next visible line start in the current
5245 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5246 preceding the line start. Skip over invisible text that is so
5247 because of selective display. Compute faces, overlays etc at the
5248 new position. Note that this function does not skip over text that
5249 is invisible because of text properties. */
5251 static void
5252 reseat_at_next_visible_line_start (it, on_newline_p)
5253 struct it *it;
5254 int on_newline_p;
5256 int newline_found_p, skipped_p = 0;
5258 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5260 /* Skip over lines that are invisible because they are indented
5261 more than the value of IT->selective. */
5262 if (it->selective > 0)
5263 while (IT_CHARPOS (*it) < ZV
5264 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5265 (double) it->selective)) /* iftc */
5267 xassert (IT_BYTEPOS (*it) == BEGV
5268 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5269 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5272 /* Position on the newline if that's what's requested. */
5273 if (on_newline_p && newline_found_p)
5275 if (STRINGP (it->string))
5277 if (IT_STRING_CHARPOS (*it) > 0)
5279 --IT_STRING_CHARPOS (*it);
5280 --IT_STRING_BYTEPOS (*it);
5283 else if (IT_CHARPOS (*it) > BEGV)
5285 --IT_CHARPOS (*it);
5286 --IT_BYTEPOS (*it);
5287 reseat (it, it->current.pos, 0);
5290 else if (skipped_p)
5291 reseat (it, it->current.pos, 0);
5293 CHECK_IT (it);
5298 /***********************************************************************
5299 Changing an iterator's position
5300 ***********************************************************************/
5302 /* Change IT's current position to POS in current_buffer. If FORCE_P
5303 is non-zero, always check for text properties at the new position.
5304 Otherwise, text properties are only looked up if POS >=
5305 IT->check_charpos of a property. */
5307 static void
5308 reseat (it, pos, force_p)
5309 struct it *it;
5310 struct text_pos pos;
5311 int force_p;
5313 int original_pos = IT_CHARPOS (*it);
5315 reseat_1 (it, pos, 0);
5317 /* Determine where to check text properties. Avoid doing it
5318 where possible because text property lookup is very expensive. */
5319 if (force_p
5320 || CHARPOS (pos) > it->stop_charpos
5321 || CHARPOS (pos) < original_pos)
5322 handle_stop (it);
5324 CHECK_IT (it);
5328 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5329 IT->stop_pos to POS, also. */
5331 static void
5332 reseat_1 (it, pos, set_stop_p)
5333 struct it *it;
5334 struct text_pos pos;
5335 int set_stop_p;
5337 /* Don't call this function when scanning a C string. */
5338 xassert (it->s == NULL);
5340 /* POS must be a reasonable value. */
5341 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5343 it->current.pos = it->position = pos;
5344 it->end_charpos = ZV;
5345 it->dpvec = NULL;
5346 it->current.dpvec_index = -1;
5347 it->current.overlay_string_index = -1;
5348 IT_STRING_CHARPOS (*it) = -1;
5349 IT_STRING_BYTEPOS (*it) = -1;
5350 it->string = Qnil;
5351 it->method = GET_FROM_BUFFER;
5352 it->object = it->w->buffer;
5353 it->area = TEXT_AREA;
5354 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5355 it->sp = 0;
5356 it->string_from_display_prop_p = 0;
5357 it->face_before_selective_p = 0;
5359 if (set_stop_p)
5360 it->stop_charpos = CHARPOS (pos);
5364 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5365 If S is non-null, it is a C string to iterate over. Otherwise,
5366 STRING gives a Lisp string to iterate over.
5368 If PRECISION > 0, don't return more then PRECISION number of
5369 characters from the string.
5371 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5372 characters have been returned. FIELD_WIDTH < 0 means an infinite
5373 field width.
5375 MULTIBYTE = 0 means disable processing of multibyte characters,
5376 MULTIBYTE > 0 means enable it,
5377 MULTIBYTE < 0 means use IT->multibyte_p.
5379 IT must be initialized via a prior call to init_iterator before
5380 calling this function. */
5382 static void
5383 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5384 struct it *it;
5385 unsigned char *s;
5386 Lisp_Object string;
5387 int charpos;
5388 int precision, field_width, multibyte;
5390 /* No region in strings. */
5391 it->region_beg_charpos = it->region_end_charpos = -1;
5393 /* No text property checks performed by default, but see below. */
5394 it->stop_charpos = -1;
5396 /* Set iterator position and end position. */
5397 bzero (&it->current, sizeof it->current);
5398 it->current.overlay_string_index = -1;
5399 it->current.dpvec_index = -1;
5400 xassert (charpos >= 0);
5402 /* If STRING is specified, use its multibyteness, otherwise use the
5403 setting of MULTIBYTE, if specified. */
5404 if (multibyte >= 0)
5405 it->multibyte_p = multibyte > 0;
5407 if (s == NULL)
5409 xassert (STRINGP (string));
5410 it->string = string;
5411 it->s = NULL;
5412 it->end_charpos = it->string_nchars = SCHARS (string);
5413 it->method = GET_FROM_STRING;
5414 it->current.string_pos = string_pos (charpos, string);
5416 else
5418 it->s = s;
5419 it->string = Qnil;
5421 /* Note that we use IT->current.pos, not it->current.string_pos,
5422 for displaying C strings. */
5423 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5424 if (it->multibyte_p)
5426 it->current.pos = c_string_pos (charpos, s, 1);
5427 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5429 else
5431 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5432 it->end_charpos = it->string_nchars = strlen (s);
5435 it->method = GET_FROM_C_STRING;
5438 /* PRECISION > 0 means don't return more than PRECISION characters
5439 from the string. */
5440 if (precision > 0 && it->end_charpos - charpos > precision)
5441 it->end_charpos = it->string_nchars = charpos + precision;
5443 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5444 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5445 FIELD_WIDTH < 0 means infinite field width. This is useful for
5446 padding with `-' at the end of a mode line. */
5447 if (field_width < 0)
5448 field_width = INFINITY;
5449 if (field_width > it->end_charpos - charpos)
5450 it->end_charpos = charpos + field_width;
5452 /* Use the standard display table for displaying strings. */
5453 if (DISP_TABLE_P (Vstandard_display_table))
5454 it->dp = XCHAR_TABLE (Vstandard_display_table);
5456 it->stop_charpos = charpos;
5457 CHECK_IT (it);
5462 /***********************************************************************
5463 Iteration
5464 ***********************************************************************/
5466 /* Map enum it_method value to corresponding next_element_from_* function. */
5468 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5470 next_element_from_buffer,
5471 next_element_from_display_vector,
5472 next_element_from_composition,
5473 next_element_from_string,
5474 next_element_from_c_string,
5475 next_element_from_image,
5476 next_element_from_stretch
5480 /* Load IT's display element fields with information about the next
5481 display element from the current position of IT. Value is zero if
5482 end of buffer (or C string) is reached. */
5484 static struct frame *last_escape_glyph_frame = NULL;
5485 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5486 static int last_escape_glyph_merged_face_id = 0;
5489 get_next_display_element (it)
5490 struct it *it;
5492 /* Non-zero means that we found a display element. Zero means that
5493 we hit the end of what we iterate over. Performance note: the
5494 function pointer `method' used here turns out to be faster than
5495 using a sequence of if-statements. */
5496 int success_p;
5498 get_next:
5499 success_p = (*get_next_element[it->method]) (it);
5501 if (it->what == IT_CHARACTER)
5503 /* Map via display table or translate control characters.
5504 IT->c, IT->len etc. have been set to the next character by
5505 the function call above. If we have a display table, and it
5506 contains an entry for IT->c, translate it. Don't do this if
5507 IT->c itself comes from a display table, otherwise we could
5508 end up in an infinite recursion. (An alternative could be to
5509 count the recursion depth of this function and signal an
5510 error when a certain maximum depth is reached.) Is it worth
5511 it? */
5512 if (success_p && it->dpvec == NULL)
5514 Lisp_Object dv;
5516 if (it->dp
5517 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5518 VECTORP (dv)))
5520 struct Lisp_Vector *v = XVECTOR (dv);
5522 /* Return the first character from the display table
5523 entry, if not empty. If empty, don't display the
5524 current character. */
5525 if (v->size)
5527 it->dpvec_char_len = it->len;
5528 it->dpvec = v->contents;
5529 it->dpend = v->contents + v->size;
5530 it->current.dpvec_index = 0;
5531 it->dpvec_face_id = -1;
5532 it->saved_face_id = it->face_id;
5533 it->method = GET_FROM_DISPLAY_VECTOR;
5534 it->ellipsis_p = 0;
5536 else
5538 set_iterator_to_next (it, 0);
5540 goto get_next;
5543 /* Translate control characters into `\003' or `^C' form.
5544 Control characters coming from a display table entry are
5545 currently not translated because we use IT->dpvec to hold
5546 the translation. This could easily be changed but I
5547 don't believe that it is worth doing.
5549 If it->multibyte_p is nonzero, eight-bit characters and
5550 non-printable multibyte characters are also translated to
5551 octal form.
5553 If it->multibyte_p is zero, eight-bit characters that
5554 don't have corresponding multibyte char code are also
5555 translated to octal form. */
5556 else if ((it->c < ' '
5557 && (it->area != TEXT_AREA
5558 /* In mode line, treat \n like other crl chars. */
5559 || (it->c != '\t'
5560 && it->glyph_row && it->glyph_row->mode_line_p)
5561 || (it->c != '\n' && it->c != '\t')))
5562 || (it->multibyte_p
5563 ? ((it->c >= 127
5564 && it->len == 1)
5565 || !CHAR_PRINTABLE_P (it->c)
5566 || (!NILP (Vnobreak_char_display)
5567 && (it->c == 0x8a0 || it->c == 0x8ad
5568 || it->c == 0x920 || it->c == 0x92d
5569 || it->c == 0xe20 || it->c == 0xe2d
5570 || it->c == 0xf20 || it->c == 0xf2d)))
5571 : (it->c >= 127
5572 && (!unibyte_display_via_language_environment
5573 || it->c == unibyte_char_to_multibyte (it->c)))))
5575 /* IT->c is a control character which must be displayed
5576 either as '\003' or as `^C' where the '\\' and '^'
5577 can be defined in the display table. Fill
5578 IT->ctl_chars with glyphs for what we have to
5579 display. Then, set IT->dpvec to these glyphs. */
5580 GLYPH g;
5581 int ctl_len;
5582 int face_id, lface_id = 0 ;
5583 GLYPH escape_glyph;
5585 /* Handle control characters with ^. */
5587 if (it->c < 128 && it->ctl_arrow_p)
5589 g = '^'; /* default glyph for Control */
5590 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5591 if (it->dp
5592 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5593 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5595 g = XINT (DISP_CTRL_GLYPH (it->dp));
5596 lface_id = FAST_GLYPH_FACE (g);
5598 if (lface_id)
5600 g = FAST_GLYPH_CHAR (g);
5601 face_id = merge_faces (it->f, Qt, lface_id,
5602 it->face_id);
5604 else if (it->f == last_escape_glyph_frame
5605 && it->face_id == last_escape_glyph_face_id)
5607 face_id = last_escape_glyph_merged_face_id;
5609 else
5611 /* Merge the escape-glyph face into the current face. */
5612 face_id = merge_faces (it->f, Qescape_glyph, 0,
5613 it->face_id);
5614 last_escape_glyph_frame = it->f;
5615 last_escape_glyph_face_id = it->face_id;
5616 last_escape_glyph_merged_face_id = face_id;
5619 XSETINT (it->ctl_chars[0], g);
5620 g = it->c ^ 0100;
5621 XSETINT (it->ctl_chars[1], g);
5622 ctl_len = 2;
5623 goto display_control;
5626 /* Handle non-break space in the mode where it only gets
5627 highlighting. */
5629 if (EQ (Vnobreak_char_display, Qt)
5630 && (it->c == 0x8a0 || it->c == 0x920
5631 || it->c == 0xe20 || it->c == 0xf20))
5633 /* Merge the no-break-space face into the current face. */
5634 face_id = merge_faces (it->f, Qnobreak_space, 0,
5635 it->face_id);
5637 g = it->c = ' ';
5638 XSETINT (it->ctl_chars[0], g);
5639 ctl_len = 1;
5640 goto display_control;
5643 /* Handle sequences that start with the "escape glyph". */
5645 /* the default escape glyph is \. */
5646 escape_glyph = '\\';
5648 if (it->dp
5649 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5650 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5652 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5653 lface_id = FAST_GLYPH_FACE (escape_glyph);
5655 if (lface_id)
5657 /* The display table specified a face.
5658 Merge it into face_id and also into escape_glyph. */
5659 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5660 face_id = merge_faces (it->f, Qt, lface_id,
5661 it->face_id);
5663 else if (it->f == last_escape_glyph_frame
5664 && it->face_id == last_escape_glyph_face_id)
5666 face_id = last_escape_glyph_merged_face_id;
5668 else
5670 /* Merge the escape-glyph face into the current face. */
5671 face_id = merge_faces (it->f, Qescape_glyph, 0,
5672 it->face_id);
5673 last_escape_glyph_frame = it->f;
5674 last_escape_glyph_face_id = it->face_id;
5675 last_escape_glyph_merged_face_id = face_id;
5678 /* Handle soft hyphens in the mode where they only get
5679 highlighting. */
5681 if (EQ (Vnobreak_char_display, Qt)
5682 && (it->c == 0x8ad || it->c == 0x92d
5683 || it->c == 0xe2d || it->c == 0xf2d))
5685 g = it->c = '-';
5686 XSETINT (it->ctl_chars[0], g);
5687 ctl_len = 1;
5688 goto display_control;
5691 /* Handle non-break space and soft hyphen
5692 with the escape glyph. */
5694 if (it->c == 0x8a0 || it->c == 0x8ad
5695 || it->c == 0x920 || it->c == 0x92d
5696 || it->c == 0xe20 || it->c == 0xe2d
5697 || it->c == 0xf20 || it->c == 0xf2d)
5699 XSETINT (it->ctl_chars[0], escape_glyph);
5700 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5701 XSETINT (it->ctl_chars[1], g);
5702 ctl_len = 2;
5703 goto display_control;
5707 unsigned char str[MAX_MULTIBYTE_LENGTH];
5708 int len;
5709 int i;
5711 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5712 if (SINGLE_BYTE_CHAR_P (it->c))
5713 str[0] = it->c, len = 1;
5714 else
5716 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5717 if (len < 0)
5719 /* It's an invalid character, which shouldn't
5720 happen actually, but due to bugs it may
5721 happen. Let's print the char as is, there's
5722 not much meaningful we can do with it. */
5723 str[0] = it->c;
5724 str[1] = it->c >> 8;
5725 str[2] = it->c >> 16;
5726 str[3] = it->c >> 24;
5727 len = 4;
5731 for (i = 0; i < len; i++)
5733 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5734 /* Insert three more glyphs into IT->ctl_chars for
5735 the octal display of the character. */
5736 g = ((str[i] >> 6) & 7) + '0';
5737 XSETINT (it->ctl_chars[i * 4 + 1], g);
5738 g = ((str[i] >> 3) & 7) + '0';
5739 XSETINT (it->ctl_chars[i * 4 + 2], g);
5740 g = (str[i] & 7) + '0';
5741 XSETINT (it->ctl_chars[i * 4 + 3], g);
5743 ctl_len = len * 4;
5746 display_control:
5747 /* Set up IT->dpvec and return first character from it. */
5748 it->dpvec_char_len = it->len;
5749 it->dpvec = it->ctl_chars;
5750 it->dpend = it->dpvec + ctl_len;
5751 it->current.dpvec_index = 0;
5752 it->dpvec_face_id = face_id;
5753 it->saved_face_id = it->face_id;
5754 it->method = GET_FROM_DISPLAY_VECTOR;
5755 it->ellipsis_p = 0;
5756 goto get_next;
5760 /* Adjust face id for a multibyte character. There are no
5761 multibyte character in unibyte text. */
5762 if (it->multibyte_p
5763 && success_p
5764 && FRAME_WINDOW_P (it->f))
5766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5767 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5771 /* Is this character the last one of a run of characters with
5772 box? If yes, set IT->end_of_box_run_p to 1. */
5773 if (it->face_box_p
5774 && it->s == NULL)
5776 int face_id;
5777 struct face *face;
5779 it->end_of_box_run_p
5780 = ((face_id = face_after_it_pos (it),
5781 face_id != it->face_id)
5782 && (face = FACE_FROM_ID (it->f, face_id),
5783 face->box == FACE_NO_BOX));
5786 /* Value is 0 if end of buffer or string reached. */
5787 return success_p;
5791 /* Move IT to the next display element.
5793 RESEAT_P non-zero means if called on a newline in buffer text,
5794 skip to the next visible line start.
5796 Functions get_next_display_element and set_iterator_to_next are
5797 separate because I find this arrangement easier to handle than a
5798 get_next_display_element function that also increments IT's
5799 position. The way it is we can first look at an iterator's current
5800 display element, decide whether it fits on a line, and if it does,
5801 increment the iterator position. The other way around we probably
5802 would either need a flag indicating whether the iterator has to be
5803 incremented the next time, or we would have to implement a
5804 decrement position function which would not be easy to write. */
5806 void
5807 set_iterator_to_next (it, reseat_p)
5808 struct it *it;
5809 int reseat_p;
5811 /* Reset flags indicating start and end of a sequence of characters
5812 with box. Reset them at the start of this function because
5813 moving the iterator to a new position might set them. */
5814 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5816 switch (it->method)
5818 case GET_FROM_BUFFER:
5819 /* The current display element of IT is a character from
5820 current_buffer. Advance in the buffer, and maybe skip over
5821 invisible lines that are so because of selective display. */
5822 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5823 reseat_at_next_visible_line_start (it, 0);
5824 else
5826 xassert (it->len != 0);
5827 IT_BYTEPOS (*it) += it->len;
5828 IT_CHARPOS (*it) += 1;
5829 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5831 break;
5833 case GET_FROM_COMPOSITION:
5834 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5835 xassert (it->sp > 0);
5836 pop_it (it);
5837 if (it->method == GET_FROM_STRING)
5839 IT_STRING_BYTEPOS (*it) += it->len;
5840 IT_STRING_CHARPOS (*it) += it->cmp_len;
5841 goto consider_string_end;
5843 else if (it->method == GET_FROM_BUFFER)
5845 IT_BYTEPOS (*it) += it->len;
5846 IT_CHARPOS (*it) += it->cmp_len;
5848 break;
5850 case GET_FROM_C_STRING:
5851 /* Current display element of IT is from a C string. */
5852 IT_BYTEPOS (*it) += it->len;
5853 IT_CHARPOS (*it) += 1;
5854 break;
5856 case GET_FROM_DISPLAY_VECTOR:
5857 /* Current display element of IT is from a display table entry.
5858 Advance in the display table definition. Reset it to null if
5859 end reached, and continue with characters from buffers/
5860 strings. */
5861 ++it->current.dpvec_index;
5863 /* Restore face of the iterator to what they were before the
5864 display vector entry (these entries may contain faces). */
5865 it->face_id = it->saved_face_id;
5867 if (it->dpvec + it->current.dpvec_index == it->dpend)
5869 int recheck_faces = it->ellipsis_p;
5871 if (it->s)
5872 it->method = GET_FROM_C_STRING;
5873 else if (STRINGP (it->string))
5874 it->method = GET_FROM_STRING;
5875 else
5877 it->method = GET_FROM_BUFFER;
5878 it->object = it->w->buffer;
5881 it->dpvec = NULL;
5882 it->current.dpvec_index = -1;
5884 /* Skip over characters which were displayed via IT->dpvec. */
5885 if (it->dpvec_char_len < 0)
5886 reseat_at_next_visible_line_start (it, 1);
5887 else if (it->dpvec_char_len > 0)
5889 if (it->method == GET_FROM_STRING
5890 && it->n_overlay_strings > 0)
5891 it->ignore_overlay_strings_at_pos_p = 1;
5892 it->len = it->dpvec_char_len;
5893 set_iterator_to_next (it, reseat_p);
5896 /* Maybe recheck faces after display vector */
5897 if (recheck_faces)
5898 it->stop_charpos = IT_CHARPOS (*it);
5900 break;
5902 case GET_FROM_STRING:
5903 /* Current display element is a character from a Lisp string. */
5904 xassert (it->s == NULL && STRINGP (it->string));
5905 IT_STRING_BYTEPOS (*it) += it->len;
5906 IT_STRING_CHARPOS (*it) += 1;
5908 consider_string_end:
5910 if (it->current.overlay_string_index >= 0)
5912 /* IT->string is an overlay string. Advance to the
5913 next, if there is one. */
5914 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5915 next_overlay_string (it);
5917 else
5919 /* IT->string is not an overlay string. If we reached
5920 its end, and there is something on IT->stack, proceed
5921 with what is on the stack. This can be either another
5922 string, this time an overlay string, or a buffer. */
5923 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5924 && it->sp > 0)
5926 pop_it (it);
5927 if (it->method == GET_FROM_STRING)
5928 goto consider_string_end;
5931 break;
5933 case GET_FROM_IMAGE:
5934 case GET_FROM_STRETCH:
5935 /* The position etc with which we have to proceed are on
5936 the stack. The position may be at the end of a string,
5937 if the `display' property takes up the whole string. */
5938 xassert (it->sp > 0);
5939 pop_it (it);
5940 if (it->method == GET_FROM_STRING)
5941 goto consider_string_end;
5942 break;
5944 default:
5945 /* There are no other methods defined, so this should be a bug. */
5946 abort ();
5949 xassert (it->method != GET_FROM_STRING
5950 || (STRINGP (it->string)
5951 && IT_STRING_CHARPOS (*it) >= 0));
5954 /* Load IT's display element fields with information about the next
5955 display element which comes from a display table entry or from the
5956 result of translating a control character to one of the forms `^C'
5957 or `\003'.
5959 IT->dpvec holds the glyphs to return as characters.
5960 IT->saved_face_id holds the face id before the display vector--
5961 it is restored into IT->face_idin set_iterator_to_next. */
5963 static int
5964 next_element_from_display_vector (it)
5965 struct it *it;
5967 /* Precondition. */
5968 xassert (it->dpvec && it->current.dpvec_index >= 0);
5970 it->face_id = it->saved_face_id;
5972 if (INTEGERP (*it->dpvec)
5973 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5975 GLYPH g;
5977 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5978 it->c = FAST_GLYPH_CHAR (g);
5979 it->len = CHAR_BYTES (it->c);
5981 /* The entry may contain a face id to use. Such a face id is
5982 the id of a Lisp face, not a realized face. A face id of
5983 zero means no face is specified. */
5984 if (it->dpvec_face_id >= 0)
5985 it->face_id = it->dpvec_face_id;
5986 else
5988 int lface_id = FAST_GLYPH_FACE (g);
5989 if (lface_id > 0)
5990 it->face_id = merge_faces (it->f, Qt, lface_id,
5991 it->saved_face_id);
5994 else
5995 /* Display table entry is invalid. Return a space. */
5996 it->c = ' ', it->len = 1;
5998 /* Don't change position and object of the iterator here. They are
5999 still the values of the character that had this display table
6000 entry or was translated, and that's what we want. */
6001 it->what = IT_CHARACTER;
6002 return 1;
6006 /* Load IT with the next display element from Lisp string IT->string.
6007 IT->current.string_pos is the current position within the string.
6008 If IT->current.overlay_string_index >= 0, the Lisp string is an
6009 overlay string. */
6011 static int
6012 next_element_from_string (it)
6013 struct it *it;
6015 struct text_pos position;
6017 xassert (STRINGP (it->string));
6018 xassert (IT_STRING_CHARPOS (*it) >= 0);
6019 position = it->current.string_pos;
6021 /* Time to check for invisible text? */
6022 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6023 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6025 handle_stop (it);
6027 /* Since a handler may have changed IT->method, we must
6028 recurse here. */
6029 return get_next_display_element (it);
6032 if (it->current.overlay_string_index >= 0)
6034 /* Get the next character from an overlay string. In overlay
6035 strings, There is no field width or padding with spaces to
6036 do. */
6037 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6039 it->what = IT_EOB;
6040 return 0;
6042 else if (STRING_MULTIBYTE (it->string))
6044 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6045 const unsigned char *s = (SDATA (it->string)
6046 + IT_STRING_BYTEPOS (*it));
6047 it->c = string_char_and_length (s, remaining, &it->len);
6049 else
6051 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6052 it->len = 1;
6055 else
6057 /* Get the next character from a Lisp string that is not an
6058 overlay string. Such strings come from the mode line, for
6059 example. We may have to pad with spaces, or truncate the
6060 string. See also next_element_from_c_string. */
6061 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6063 it->what = IT_EOB;
6064 return 0;
6066 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6068 /* Pad with spaces. */
6069 it->c = ' ', it->len = 1;
6070 CHARPOS (position) = BYTEPOS (position) = -1;
6072 else if (STRING_MULTIBYTE (it->string))
6074 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6075 const unsigned char *s = (SDATA (it->string)
6076 + IT_STRING_BYTEPOS (*it));
6077 it->c = string_char_and_length (s, maxlen, &it->len);
6079 else
6081 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6082 it->len = 1;
6086 /* Record what we have and where it came from. */
6087 it->what = IT_CHARACTER;
6088 it->object = it->string;
6089 it->position = position;
6090 return 1;
6094 /* Load IT with next display element from C string IT->s.
6095 IT->string_nchars is the maximum number of characters to return
6096 from the string. IT->end_charpos may be greater than
6097 IT->string_nchars when this function is called, in which case we
6098 may have to return padding spaces. Value is zero if end of string
6099 reached, including padding spaces. */
6101 static int
6102 next_element_from_c_string (it)
6103 struct it *it;
6105 int success_p = 1;
6107 xassert (it->s);
6108 it->what = IT_CHARACTER;
6109 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6110 it->object = Qnil;
6112 /* IT's position can be greater IT->string_nchars in case a field
6113 width or precision has been specified when the iterator was
6114 initialized. */
6115 if (IT_CHARPOS (*it) >= it->end_charpos)
6117 /* End of the game. */
6118 it->what = IT_EOB;
6119 success_p = 0;
6121 else if (IT_CHARPOS (*it) >= it->string_nchars)
6123 /* Pad with spaces. */
6124 it->c = ' ', it->len = 1;
6125 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6127 else if (it->multibyte_p)
6129 /* Implementation note: The calls to strlen apparently aren't a
6130 performance problem because there is no noticeable performance
6131 difference between Emacs running in unibyte or multibyte mode. */
6132 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6133 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6134 maxlen, &it->len);
6136 else
6137 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6139 return success_p;
6143 /* Set up IT to return characters from an ellipsis, if appropriate.
6144 The definition of the ellipsis glyphs may come from a display table
6145 entry. This function Fills IT with the first glyph from the
6146 ellipsis if an ellipsis is to be displayed. */
6148 static int
6149 next_element_from_ellipsis (it)
6150 struct it *it;
6152 if (it->selective_display_ellipsis_p)
6153 setup_for_ellipsis (it, it->len);
6154 else
6156 /* The face at the current position may be different from the
6157 face we find after the invisible text. Remember what it
6158 was in IT->saved_face_id, and signal that it's there by
6159 setting face_before_selective_p. */
6160 it->saved_face_id = it->face_id;
6161 it->method = GET_FROM_BUFFER;
6162 it->object = it->w->buffer;
6163 reseat_at_next_visible_line_start (it, 1);
6164 it->face_before_selective_p = 1;
6167 return get_next_display_element (it);
6171 /* Deliver an image display element. The iterator IT is already
6172 filled with image information (done in handle_display_prop). Value
6173 is always 1. */
6176 static int
6177 next_element_from_image (it)
6178 struct it *it;
6180 it->what = IT_IMAGE;
6181 return 1;
6185 /* Fill iterator IT with next display element from a stretch glyph
6186 property. IT->object is the value of the text property. Value is
6187 always 1. */
6189 static int
6190 next_element_from_stretch (it)
6191 struct it *it;
6193 it->what = IT_STRETCH;
6194 return 1;
6198 /* Load IT with the next display element from current_buffer. Value
6199 is zero if end of buffer reached. IT->stop_charpos is the next
6200 position at which to stop and check for text properties or buffer
6201 end. */
6203 static int
6204 next_element_from_buffer (it)
6205 struct it *it;
6207 int success_p = 1;
6209 /* Check this assumption, otherwise, we would never enter the
6210 if-statement, below. */
6211 xassert (IT_CHARPOS (*it) >= BEGV
6212 && IT_CHARPOS (*it) <= it->stop_charpos);
6214 if (IT_CHARPOS (*it) >= it->stop_charpos)
6216 if (IT_CHARPOS (*it) >= it->end_charpos)
6218 int overlay_strings_follow_p;
6220 /* End of the game, except when overlay strings follow that
6221 haven't been returned yet. */
6222 if (it->overlay_strings_at_end_processed_p)
6223 overlay_strings_follow_p = 0;
6224 else
6226 it->overlay_strings_at_end_processed_p = 1;
6227 overlay_strings_follow_p = get_overlay_strings (it, 0);
6230 if (overlay_strings_follow_p)
6231 success_p = get_next_display_element (it);
6232 else
6234 it->what = IT_EOB;
6235 it->position = it->current.pos;
6236 success_p = 0;
6239 else
6241 handle_stop (it);
6242 return get_next_display_element (it);
6245 else
6247 /* No face changes, overlays etc. in sight, so just return a
6248 character from current_buffer. */
6249 unsigned char *p;
6251 /* Maybe run the redisplay end trigger hook. Performance note:
6252 This doesn't seem to cost measurable time. */
6253 if (it->redisplay_end_trigger_charpos
6254 && it->glyph_row
6255 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6256 run_redisplay_end_trigger_hook (it);
6258 /* Get the next character, maybe multibyte. */
6259 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6260 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6262 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6263 - IT_BYTEPOS (*it));
6264 it->c = string_char_and_length (p, maxlen, &it->len);
6266 else
6267 it->c = *p, it->len = 1;
6269 /* Record what we have and where it came from. */
6270 it->what = IT_CHARACTER;;
6271 it->object = it->w->buffer;
6272 it->position = it->current.pos;
6274 /* Normally we return the character found above, except when we
6275 really want to return an ellipsis for selective display. */
6276 if (it->selective)
6278 if (it->c == '\n')
6280 /* A value of selective > 0 means hide lines indented more
6281 than that number of columns. */
6282 if (it->selective > 0
6283 && IT_CHARPOS (*it) + 1 < ZV
6284 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6285 IT_BYTEPOS (*it) + 1,
6286 (double) it->selective)) /* iftc */
6288 success_p = next_element_from_ellipsis (it);
6289 it->dpvec_char_len = -1;
6292 else if (it->c == '\r' && it->selective == -1)
6294 /* A value of selective == -1 means that everything from the
6295 CR to the end of the line is invisible, with maybe an
6296 ellipsis displayed for it. */
6297 success_p = next_element_from_ellipsis (it);
6298 it->dpvec_char_len = -1;
6303 /* Value is zero if end of buffer reached. */
6304 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6305 return success_p;
6309 /* Run the redisplay end trigger hook for IT. */
6311 static void
6312 run_redisplay_end_trigger_hook (it)
6313 struct it *it;
6315 Lisp_Object args[3];
6317 /* IT->glyph_row should be non-null, i.e. we should be actually
6318 displaying something, or otherwise we should not run the hook. */
6319 xassert (it->glyph_row);
6321 /* Set up hook arguments. */
6322 args[0] = Qredisplay_end_trigger_functions;
6323 args[1] = it->window;
6324 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6325 it->redisplay_end_trigger_charpos = 0;
6327 /* Since we are *trying* to run these functions, don't try to run
6328 them again, even if they get an error. */
6329 it->w->redisplay_end_trigger = Qnil;
6330 Frun_hook_with_args (3, args);
6332 /* Notice if it changed the face of the character we are on. */
6333 handle_face_prop (it);
6337 /* Deliver a composition display element. The iterator IT is already
6338 filled with composition information (done in
6339 handle_composition_prop). Value is always 1. */
6341 static int
6342 next_element_from_composition (it)
6343 struct it *it;
6345 it->what = IT_COMPOSITION;
6346 it->position = (STRINGP (it->string)
6347 ? it->current.string_pos
6348 : it->current.pos);
6349 if (STRINGP (it->string))
6350 it->object = it->string;
6351 else
6352 it->object = it->w->buffer;
6353 return 1;
6358 /***********************************************************************
6359 Moving an iterator without producing glyphs
6360 ***********************************************************************/
6362 /* Check if iterator is at a position corresponding to a valid buffer
6363 position after some move_it_ call. */
6365 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6366 ((it)->method == GET_FROM_STRING \
6367 ? IT_STRING_CHARPOS (*it) == 0 \
6368 : 1)
6371 /* Move iterator IT to a specified buffer or X position within one
6372 line on the display without producing glyphs.
6374 OP should be a bit mask including some or all of these bits:
6375 MOVE_TO_X: Stop on reaching x-position TO_X.
6376 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6377 Regardless of OP's value, stop in reaching the end of the display line.
6379 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6380 This means, in particular, that TO_X includes window's horizontal
6381 scroll amount.
6383 The return value has several possible values that
6384 say what condition caused the scan to stop:
6386 MOVE_POS_MATCH_OR_ZV
6387 - when TO_POS or ZV was reached.
6389 MOVE_X_REACHED
6390 -when TO_X was reached before TO_POS or ZV were reached.
6392 MOVE_LINE_CONTINUED
6393 - when we reached the end of the display area and the line must
6394 be continued.
6396 MOVE_LINE_TRUNCATED
6397 - when we reached the end of the display area and the line is
6398 truncated.
6400 MOVE_NEWLINE_OR_CR
6401 - when we stopped at a line end, i.e. a newline or a CR and selective
6402 display is on. */
6404 static enum move_it_result
6405 move_it_in_display_line_to (it, to_charpos, to_x, op)
6406 struct it *it;
6407 int to_charpos, to_x, op;
6409 enum move_it_result result = MOVE_UNDEFINED;
6410 struct glyph_row *saved_glyph_row;
6412 /* Don't produce glyphs in produce_glyphs. */
6413 saved_glyph_row = it->glyph_row;
6414 it->glyph_row = NULL;
6416 #define BUFFER_POS_REACHED_P() \
6417 ((op & MOVE_TO_POS) != 0 \
6418 && BUFFERP (it->object) \
6419 && IT_CHARPOS (*it) >= to_charpos \
6420 && (it->method == GET_FROM_BUFFER \
6421 || (it->method == GET_FROM_DISPLAY_VECTOR \
6422 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6425 while (1)
6427 int x, i, ascent = 0, descent = 0;
6429 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6430 if ((op & MOVE_TO_POS) != 0
6431 && BUFFERP (it->object)
6432 && it->method == GET_FROM_BUFFER
6433 && IT_CHARPOS (*it) > to_charpos)
6435 result = MOVE_POS_MATCH_OR_ZV;
6436 break;
6439 /* Stop when ZV reached.
6440 We used to stop here when TO_CHARPOS reached as well, but that is
6441 too soon if this glyph does not fit on this line. So we handle it
6442 explicitly below. */
6443 if (!get_next_display_element (it)
6444 || (it->truncate_lines_p
6445 && BUFFER_POS_REACHED_P ()))
6447 result = MOVE_POS_MATCH_OR_ZV;
6448 break;
6451 /* The call to produce_glyphs will get the metrics of the
6452 display element IT is loaded with. We record in x the
6453 x-position before this display element in case it does not
6454 fit on the line. */
6455 x = it->current_x;
6457 /* Remember the line height so far in case the next element doesn't
6458 fit on the line. */
6459 if (!it->truncate_lines_p)
6461 ascent = it->max_ascent;
6462 descent = it->max_descent;
6465 PRODUCE_GLYPHS (it);
6467 if (it->area != TEXT_AREA)
6469 set_iterator_to_next (it, 1);
6470 continue;
6473 /* The number of glyphs we get back in IT->nglyphs will normally
6474 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6475 character on a terminal frame, or (iii) a line end. For the
6476 second case, IT->nglyphs - 1 padding glyphs will be present
6477 (on X frames, there is only one glyph produced for a
6478 composite character.
6480 The behavior implemented below means, for continuation lines,
6481 that as many spaces of a TAB as fit on the current line are
6482 displayed there. For terminal frames, as many glyphs of a
6483 multi-glyph character are displayed in the current line, too.
6484 This is what the old redisplay code did, and we keep it that
6485 way. Under X, the whole shape of a complex character must
6486 fit on the line or it will be completely displayed in the
6487 next line.
6489 Note that both for tabs and padding glyphs, all glyphs have
6490 the same width. */
6491 if (it->nglyphs)
6493 /* More than one glyph or glyph doesn't fit on line. All
6494 glyphs have the same width. */
6495 int single_glyph_width = it->pixel_width / it->nglyphs;
6496 int new_x;
6497 int x_before_this_char = x;
6498 int hpos_before_this_char = it->hpos;
6500 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6502 new_x = x + single_glyph_width;
6504 /* We want to leave anything reaching TO_X to the caller. */
6505 if ((op & MOVE_TO_X) && new_x > to_x)
6507 if (BUFFER_POS_REACHED_P ())
6508 goto buffer_pos_reached;
6509 it->current_x = x;
6510 result = MOVE_X_REACHED;
6511 break;
6513 else if (/* Lines are continued. */
6514 !it->truncate_lines_p
6515 && (/* And glyph doesn't fit on the line. */
6516 new_x > it->last_visible_x
6517 /* Or it fits exactly and we're on a window
6518 system frame. */
6519 || (new_x == it->last_visible_x
6520 && FRAME_WINDOW_P (it->f))))
6522 if (/* IT->hpos == 0 means the very first glyph
6523 doesn't fit on the line, e.g. a wide image. */
6524 it->hpos == 0
6525 || (new_x == it->last_visible_x
6526 && FRAME_WINDOW_P (it->f)))
6528 ++it->hpos;
6529 it->current_x = new_x;
6531 /* The character's last glyph just barely fits
6532 in this row. */
6533 if (i == it->nglyphs - 1)
6535 /* If this is the destination position,
6536 return a position *before* it in this row,
6537 now that we know it fits in this row. */
6538 if (BUFFER_POS_REACHED_P ())
6540 it->hpos = hpos_before_this_char;
6541 it->current_x = x_before_this_char;
6542 result = MOVE_POS_MATCH_OR_ZV;
6543 break;
6546 set_iterator_to_next (it, 1);
6547 #ifdef HAVE_WINDOW_SYSTEM
6548 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6550 if (!get_next_display_element (it))
6552 result = MOVE_POS_MATCH_OR_ZV;
6553 break;
6555 if (BUFFER_POS_REACHED_P ())
6557 if (ITERATOR_AT_END_OF_LINE_P (it))
6558 result = MOVE_POS_MATCH_OR_ZV;
6559 else
6560 result = MOVE_LINE_CONTINUED;
6561 break;
6563 if (ITERATOR_AT_END_OF_LINE_P (it))
6565 result = MOVE_NEWLINE_OR_CR;
6566 break;
6569 #endif /* HAVE_WINDOW_SYSTEM */
6572 else
6574 it->current_x = x;
6575 it->max_ascent = ascent;
6576 it->max_descent = descent;
6579 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6580 IT_CHARPOS (*it)));
6581 result = MOVE_LINE_CONTINUED;
6582 break;
6584 else if (BUFFER_POS_REACHED_P ())
6585 goto buffer_pos_reached;
6586 else if (new_x > it->first_visible_x)
6588 /* Glyph is visible. Increment number of glyphs that
6589 would be displayed. */
6590 ++it->hpos;
6592 else
6594 /* Glyph is completely off the left margin of the display
6595 area. Nothing to do. */
6599 if (result != MOVE_UNDEFINED)
6600 break;
6602 else if (BUFFER_POS_REACHED_P ())
6604 buffer_pos_reached:
6605 it->current_x = x;
6606 it->max_ascent = ascent;
6607 it->max_descent = descent;
6608 result = MOVE_POS_MATCH_OR_ZV;
6609 break;
6611 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6613 /* Stop when TO_X specified and reached. This check is
6614 necessary here because of lines consisting of a line end,
6615 only. The line end will not produce any glyphs and we
6616 would never get MOVE_X_REACHED. */
6617 xassert (it->nglyphs == 0);
6618 result = MOVE_X_REACHED;
6619 break;
6622 /* Is this a line end? If yes, we're done. */
6623 if (ITERATOR_AT_END_OF_LINE_P (it))
6625 result = MOVE_NEWLINE_OR_CR;
6626 break;
6629 /* The current display element has been consumed. Advance
6630 to the next. */
6631 set_iterator_to_next (it, 1);
6633 /* Stop if lines are truncated and IT's current x-position is
6634 past the right edge of the window now. */
6635 if (it->truncate_lines_p
6636 && it->current_x >= it->last_visible_x)
6638 #ifdef HAVE_WINDOW_SYSTEM
6639 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6641 if (!get_next_display_element (it)
6642 || BUFFER_POS_REACHED_P ())
6644 result = MOVE_POS_MATCH_OR_ZV;
6645 break;
6647 if (ITERATOR_AT_END_OF_LINE_P (it))
6649 result = MOVE_NEWLINE_OR_CR;
6650 break;
6653 #endif /* HAVE_WINDOW_SYSTEM */
6654 result = MOVE_LINE_TRUNCATED;
6655 break;
6659 #undef BUFFER_POS_REACHED_P
6661 /* Restore the iterator settings altered at the beginning of this
6662 function. */
6663 it->glyph_row = saved_glyph_row;
6664 return result;
6668 /* Move IT forward until it satisfies one or more of the criteria in
6669 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6671 OP is a bit-mask that specifies where to stop, and in particular,
6672 which of those four position arguments makes a difference. See the
6673 description of enum move_operation_enum.
6675 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6676 screen line, this function will set IT to the next position >
6677 TO_CHARPOS. */
6679 void
6680 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6681 struct it *it;
6682 int to_charpos, to_x, to_y, to_vpos;
6683 int op;
6685 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6686 int line_height;
6687 int reached = 0;
6689 for (;;)
6691 if (op & MOVE_TO_VPOS)
6693 /* If no TO_CHARPOS and no TO_X specified, stop at the
6694 start of the line TO_VPOS. */
6695 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6697 if (it->vpos == to_vpos)
6699 reached = 1;
6700 break;
6702 else
6703 skip = move_it_in_display_line_to (it, -1, -1, 0);
6705 else
6707 /* TO_VPOS >= 0 means stop at TO_X in the line at
6708 TO_VPOS, or at TO_POS, whichever comes first. */
6709 if (it->vpos == to_vpos)
6711 reached = 2;
6712 break;
6715 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6717 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6719 reached = 3;
6720 break;
6722 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6724 /* We have reached TO_X but not in the line we want. */
6725 skip = move_it_in_display_line_to (it, to_charpos,
6726 -1, MOVE_TO_POS);
6727 if (skip == MOVE_POS_MATCH_OR_ZV)
6729 reached = 4;
6730 break;
6735 else if (op & MOVE_TO_Y)
6737 struct it it_backup;
6739 /* TO_Y specified means stop at TO_X in the line containing
6740 TO_Y---or at TO_CHARPOS if this is reached first. The
6741 problem is that we can't really tell whether the line
6742 contains TO_Y before we have completely scanned it, and
6743 this may skip past TO_X. What we do is to first scan to
6744 TO_X.
6746 If TO_X is not specified, use a TO_X of zero. The reason
6747 is to make the outcome of this function more predictable.
6748 If we didn't use TO_X == 0, we would stop at the end of
6749 the line which is probably not what a caller would expect
6750 to happen. */
6751 skip = move_it_in_display_line_to (it, to_charpos,
6752 ((op & MOVE_TO_X)
6753 ? to_x : 0),
6754 (MOVE_TO_X
6755 | (op & MOVE_TO_POS)));
6757 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6758 if (skip == MOVE_POS_MATCH_OR_ZV)
6760 reached = 5;
6761 break;
6764 /* If TO_X was reached, we would like to know whether TO_Y
6765 is in the line. This can only be said if we know the
6766 total line height which requires us to scan the rest of
6767 the line. */
6768 if (skip == MOVE_X_REACHED)
6770 it_backup = *it;
6771 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6772 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6773 op & MOVE_TO_POS);
6774 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6777 /* Now, decide whether TO_Y is in this line. */
6778 line_height = it->max_ascent + it->max_descent;
6779 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6781 if (to_y >= it->current_y
6782 && to_y < it->current_y + line_height)
6784 if (skip == MOVE_X_REACHED)
6785 /* If TO_Y is in this line and TO_X was reached above,
6786 we scanned too far. We have to restore IT's settings
6787 to the ones before skipping. */
6788 *it = it_backup;
6789 reached = 6;
6791 else if (skip == MOVE_X_REACHED)
6793 skip = skip2;
6794 if (skip == MOVE_POS_MATCH_OR_ZV)
6795 reached = 7;
6798 if (reached)
6799 break;
6801 else if (BUFFERP (it->object)
6802 && it->method == GET_FROM_BUFFER
6803 && IT_CHARPOS (*it) >= to_charpos)
6804 skip = MOVE_POS_MATCH_OR_ZV;
6805 else
6806 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6808 switch (skip)
6810 case MOVE_POS_MATCH_OR_ZV:
6811 reached = 8;
6812 goto out;
6814 case MOVE_NEWLINE_OR_CR:
6815 set_iterator_to_next (it, 1);
6816 it->continuation_lines_width = 0;
6817 break;
6819 case MOVE_LINE_TRUNCATED:
6820 it->continuation_lines_width = 0;
6821 reseat_at_next_visible_line_start (it, 0);
6822 if ((op & MOVE_TO_POS) != 0
6823 && IT_CHARPOS (*it) > to_charpos)
6825 reached = 9;
6826 goto out;
6828 break;
6830 case MOVE_LINE_CONTINUED:
6831 it->continuation_lines_width += it->current_x;
6832 break;
6834 default:
6835 abort ();
6838 /* Reset/increment for the next run. */
6839 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6840 it->current_x = it->hpos = 0;
6841 it->current_y += it->max_ascent + it->max_descent;
6842 ++it->vpos;
6843 last_height = it->max_ascent + it->max_descent;
6844 last_max_ascent = it->max_ascent;
6845 it->max_ascent = it->max_descent = 0;
6848 out:
6850 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6854 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6856 If DY > 0, move IT backward at least that many pixels. DY = 0
6857 means move IT backward to the preceding line start or BEGV. This
6858 function may move over more than DY pixels if IT->current_y - DY
6859 ends up in the middle of a line; in this case IT->current_y will be
6860 set to the top of the line moved to. */
6862 void
6863 move_it_vertically_backward (it, dy)
6864 struct it *it;
6865 int dy;
6867 int nlines, h;
6868 struct it it2, it3;
6869 int start_pos;
6871 move_further_back:
6872 xassert (dy >= 0);
6874 start_pos = IT_CHARPOS (*it);
6876 /* Estimate how many newlines we must move back. */
6877 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6879 /* Set the iterator's position that many lines back. */
6880 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6881 back_to_previous_visible_line_start (it);
6883 /* Reseat the iterator here. When moving backward, we don't want
6884 reseat to skip forward over invisible text, set up the iterator
6885 to deliver from overlay strings at the new position etc. So,
6886 use reseat_1 here. */
6887 reseat_1 (it, it->current.pos, 1);
6889 /* We are now surely at a line start. */
6890 it->current_x = it->hpos = 0;
6891 it->continuation_lines_width = 0;
6893 /* Move forward and see what y-distance we moved. First move to the
6894 start of the next line so that we get its height. We need this
6895 height to be able to tell whether we reached the specified
6896 y-distance. */
6897 it2 = *it;
6898 it2.max_ascent = it2.max_descent = 0;
6901 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6902 MOVE_TO_POS | MOVE_TO_VPOS);
6904 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6905 xassert (IT_CHARPOS (*it) >= BEGV);
6906 it3 = it2;
6908 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6909 xassert (IT_CHARPOS (*it) >= BEGV);
6910 /* H is the actual vertical distance from the position in *IT
6911 and the starting position. */
6912 h = it2.current_y - it->current_y;
6913 /* NLINES is the distance in number of lines. */
6914 nlines = it2.vpos - it->vpos;
6916 /* Correct IT's y and vpos position
6917 so that they are relative to the starting point. */
6918 it->vpos -= nlines;
6919 it->current_y -= h;
6921 if (dy == 0)
6923 /* DY == 0 means move to the start of the screen line. The
6924 value of nlines is > 0 if continuation lines were involved. */
6925 if (nlines > 0)
6926 move_it_by_lines (it, nlines, 1);
6927 #if 0
6928 /* I think this assert is bogus if buffer contains
6929 invisible text or images. KFS. */
6930 xassert (IT_CHARPOS (*it) <= start_pos);
6931 #endif
6933 else
6935 /* The y-position we try to reach, relative to *IT.
6936 Note that H has been subtracted in front of the if-statement. */
6937 int target_y = it->current_y + h - dy;
6938 int y0 = it3.current_y;
6939 int y1 = line_bottom_y (&it3);
6940 int line_height = y1 - y0;
6942 /* If we did not reach target_y, try to move further backward if
6943 we can. If we moved too far backward, try to move forward. */
6944 if (target_y < it->current_y
6945 /* This is heuristic. In a window that's 3 lines high, with
6946 a line height of 13 pixels each, recentering with point
6947 on the bottom line will try to move -39/2 = 19 pixels
6948 backward. Try to avoid moving into the first line. */
6949 && (it->current_y - target_y
6950 > min (window_box_height (it->w), line_height * 2 / 3))
6951 && IT_CHARPOS (*it) > BEGV)
6953 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6954 target_y - it->current_y));
6955 dy = it->current_y - target_y;
6956 goto move_further_back;
6958 else if (target_y >= it->current_y + line_height
6959 && IT_CHARPOS (*it) < ZV)
6961 /* Should move forward by at least one line, maybe more.
6963 Note: Calling move_it_by_lines can be expensive on
6964 terminal frames, where compute_motion is used (via
6965 vmotion) to do the job, when there are very long lines
6966 and truncate-lines is nil. That's the reason for
6967 treating terminal frames specially here. */
6969 if (!FRAME_WINDOW_P (it->f))
6970 move_it_vertically (it, target_y - (it->current_y + line_height));
6971 else
6975 move_it_by_lines (it, 1, 1);
6977 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6980 #if 0
6981 /* I think this assert is bogus if buffer contains
6982 invisible text or images. KFS. */
6983 xassert (IT_CHARPOS (*it) >= BEGV);
6984 #endif
6990 /* Move IT by a specified amount of pixel lines DY. DY negative means
6991 move backwards. DY = 0 means move to start of screen line. At the
6992 end, IT will be on the start of a screen line. */
6994 void
6995 move_it_vertically (it, dy)
6996 struct it *it;
6997 int dy;
6999 if (dy <= 0)
7000 move_it_vertically_backward (it, -dy);
7001 else
7003 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7004 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7005 MOVE_TO_POS | MOVE_TO_Y);
7006 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7008 /* If buffer ends in ZV without a newline, move to the start of
7009 the line to satisfy the post-condition. */
7010 if (IT_CHARPOS (*it) == ZV
7011 && ZV > BEGV
7012 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7013 move_it_by_lines (it, 0, 0);
7018 /* Move iterator IT past the end of the text line it is in. */
7020 void
7021 move_it_past_eol (it)
7022 struct it *it;
7024 enum move_it_result rc;
7026 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7027 if (rc == MOVE_NEWLINE_OR_CR)
7028 set_iterator_to_next (it, 0);
7032 #if 0 /* Currently not used. */
7034 /* Return non-zero if some text between buffer positions START_CHARPOS
7035 and END_CHARPOS is invisible. IT->window is the window for text
7036 property lookup. */
7038 static int
7039 invisible_text_between_p (it, start_charpos, end_charpos)
7040 struct it *it;
7041 int start_charpos, end_charpos;
7043 Lisp_Object prop, limit;
7044 int invisible_found_p;
7046 xassert (it != NULL && start_charpos <= end_charpos);
7048 /* Is text at START invisible? */
7049 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7050 it->window);
7051 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7052 invisible_found_p = 1;
7053 else
7055 limit = Fnext_single_char_property_change (make_number (start_charpos),
7056 Qinvisible, Qnil,
7057 make_number (end_charpos));
7058 invisible_found_p = XFASTINT (limit) < end_charpos;
7061 return invisible_found_p;
7064 #endif /* 0 */
7067 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7068 negative means move up. DVPOS == 0 means move to the start of the
7069 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7070 NEED_Y_P is zero, IT->current_y will be left unchanged.
7072 Further optimization ideas: If we would know that IT->f doesn't use
7073 a face with proportional font, we could be faster for
7074 truncate-lines nil. */
7076 void
7077 move_it_by_lines (it, dvpos, need_y_p)
7078 struct it *it;
7079 int dvpos, need_y_p;
7081 struct position pos;
7083 if (!FRAME_WINDOW_P (it->f))
7085 struct text_pos textpos;
7087 /* We can use vmotion on frames without proportional fonts. */
7088 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7089 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7090 reseat (it, textpos, 1);
7091 it->vpos += pos.vpos;
7092 it->current_y += pos.vpos;
7094 else if (dvpos == 0)
7096 /* DVPOS == 0 means move to the start of the screen line. */
7097 move_it_vertically_backward (it, 0);
7098 xassert (it->current_x == 0 && it->hpos == 0);
7099 /* Let next call to line_bottom_y calculate real line height */
7100 last_height = 0;
7102 else if (dvpos > 0)
7104 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7105 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7106 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7108 else
7110 struct it it2;
7111 int start_charpos, i;
7113 /* Start at the beginning of the screen line containing IT's
7114 position. This may actually move vertically backwards,
7115 in case of overlays, so adjust dvpos accordingly. */
7116 dvpos += it->vpos;
7117 move_it_vertically_backward (it, 0);
7118 dvpos -= it->vpos;
7120 /* Go back -DVPOS visible lines and reseat the iterator there. */
7121 start_charpos = IT_CHARPOS (*it);
7122 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7123 back_to_previous_visible_line_start (it);
7124 reseat (it, it->current.pos, 1);
7126 /* Move further back if we end up in a string or an image. */
7127 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7129 /* First try to move to start of display line. */
7130 dvpos += it->vpos;
7131 move_it_vertically_backward (it, 0);
7132 dvpos -= it->vpos;
7133 if (IT_POS_VALID_AFTER_MOVE_P (it))
7134 break;
7135 /* If start of line is still in string or image,
7136 move further back. */
7137 back_to_previous_visible_line_start (it);
7138 reseat (it, it->current.pos, 1);
7139 dvpos--;
7142 it->current_x = it->hpos = 0;
7144 /* Above call may have moved too far if continuation lines
7145 are involved. Scan forward and see if it did. */
7146 it2 = *it;
7147 it2.vpos = it2.current_y = 0;
7148 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7149 it->vpos -= it2.vpos;
7150 it->current_y -= it2.current_y;
7151 it->current_x = it->hpos = 0;
7153 /* If we moved too far back, move IT some lines forward. */
7154 if (it2.vpos > -dvpos)
7156 int delta = it2.vpos + dvpos;
7157 it2 = *it;
7158 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7159 /* Move back again if we got too far ahead. */
7160 if (IT_CHARPOS (*it) >= start_charpos)
7161 *it = it2;
7166 /* Return 1 if IT points into the middle of a display vector. */
7169 in_display_vector_p (it)
7170 struct it *it;
7172 return (it->method == GET_FROM_DISPLAY_VECTOR
7173 && it->current.dpvec_index > 0
7174 && it->dpvec + it->current.dpvec_index != it->dpend);
7178 /***********************************************************************
7179 Messages
7180 ***********************************************************************/
7183 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7184 to *Messages*. */
7186 void
7187 add_to_log (format, arg1, arg2)
7188 char *format;
7189 Lisp_Object arg1, arg2;
7191 Lisp_Object args[3];
7192 Lisp_Object msg, fmt;
7193 char *buffer;
7194 int len;
7195 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7196 USE_SAFE_ALLOCA;
7198 /* Do nothing if called asynchronously. Inserting text into
7199 a buffer may call after-change-functions and alike and
7200 that would means running Lisp asynchronously. */
7201 if (handling_signal)
7202 return;
7204 fmt = msg = Qnil;
7205 GCPRO4 (fmt, msg, arg1, arg2);
7207 args[0] = fmt = build_string (format);
7208 args[1] = arg1;
7209 args[2] = arg2;
7210 msg = Fformat (3, args);
7212 len = SBYTES (msg) + 1;
7213 SAFE_ALLOCA (buffer, char *, len);
7214 bcopy (SDATA (msg), buffer, len);
7216 message_dolog (buffer, len - 1, 1, 0);
7217 SAFE_FREE ();
7219 UNGCPRO;
7223 /* Output a newline in the *Messages* buffer if "needs" one. */
7225 void
7226 message_log_maybe_newline ()
7228 if (message_log_need_newline)
7229 message_dolog ("", 0, 1, 0);
7233 /* Add a string M of length NBYTES to the message log, optionally
7234 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7235 nonzero, means interpret the contents of M as multibyte. This
7236 function calls low-level routines in order to bypass text property
7237 hooks, etc. which might not be safe to run.
7239 This may GC (insert may run before/after change hooks),
7240 so the buffer M must NOT point to a Lisp string. */
7242 void
7243 message_dolog (m, nbytes, nlflag, multibyte)
7244 const char *m;
7245 int nbytes, nlflag, multibyte;
7247 if (!NILP (Vmemory_full))
7248 return;
7250 if (!NILP (Vmessage_log_max))
7252 struct buffer *oldbuf;
7253 Lisp_Object oldpoint, oldbegv, oldzv;
7254 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7255 int point_at_end = 0;
7256 int zv_at_end = 0;
7257 Lisp_Object old_deactivate_mark, tem;
7258 struct gcpro gcpro1;
7260 old_deactivate_mark = Vdeactivate_mark;
7261 oldbuf = current_buffer;
7262 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7263 current_buffer->undo_list = Qt;
7265 oldpoint = message_dolog_marker1;
7266 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7267 oldbegv = message_dolog_marker2;
7268 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7269 oldzv = message_dolog_marker3;
7270 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7271 GCPRO1 (old_deactivate_mark);
7273 if (PT == Z)
7274 point_at_end = 1;
7275 if (ZV == Z)
7276 zv_at_end = 1;
7278 BEGV = BEG;
7279 BEGV_BYTE = BEG_BYTE;
7280 ZV = Z;
7281 ZV_BYTE = Z_BYTE;
7282 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7284 /* Insert the string--maybe converting multibyte to single byte
7285 or vice versa, so that all the text fits the buffer. */
7286 if (multibyte
7287 && NILP (current_buffer->enable_multibyte_characters))
7289 int i, c, char_bytes;
7290 unsigned char work[1];
7292 /* Convert a multibyte string to single-byte
7293 for the *Message* buffer. */
7294 for (i = 0; i < nbytes; i += char_bytes)
7296 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7297 work[0] = (SINGLE_BYTE_CHAR_P (c)
7299 : multibyte_char_to_unibyte (c, Qnil));
7300 insert_1_both (work, 1, 1, 1, 0, 0);
7303 else if (! multibyte
7304 && ! NILP (current_buffer->enable_multibyte_characters))
7306 int i, c, char_bytes;
7307 unsigned char *msg = (unsigned char *) m;
7308 unsigned char str[MAX_MULTIBYTE_LENGTH];
7309 /* Convert a single-byte string to multibyte
7310 for the *Message* buffer. */
7311 for (i = 0; i < nbytes; i++)
7313 c = unibyte_char_to_multibyte (msg[i]);
7314 char_bytes = CHAR_STRING (c, str);
7315 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7318 else if (nbytes)
7319 insert_1 (m, nbytes, 1, 0, 0);
7321 if (nlflag)
7323 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7324 insert_1 ("\n", 1, 1, 0, 0);
7326 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7327 this_bol = PT;
7328 this_bol_byte = PT_BYTE;
7330 /* See if this line duplicates the previous one.
7331 If so, combine duplicates. */
7332 if (this_bol > BEG)
7334 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7335 prev_bol = PT;
7336 prev_bol_byte = PT_BYTE;
7338 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7339 this_bol, this_bol_byte);
7340 if (dup)
7342 del_range_both (prev_bol, prev_bol_byte,
7343 this_bol, this_bol_byte, 0);
7344 if (dup > 1)
7346 char dupstr[40];
7347 int duplen;
7349 /* If you change this format, don't forget to also
7350 change message_log_check_duplicate. */
7351 sprintf (dupstr, " [%d times]", dup);
7352 duplen = strlen (dupstr);
7353 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7354 insert_1 (dupstr, duplen, 1, 0, 1);
7359 /* If we have more than the desired maximum number of lines
7360 in the *Messages* buffer now, delete the oldest ones.
7361 This is safe because we don't have undo in this buffer. */
7363 if (NATNUMP (Vmessage_log_max))
7365 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7366 -XFASTINT (Vmessage_log_max) - 1, 0);
7367 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7370 BEGV = XMARKER (oldbegv)->charpos;
7371 BEGV_BYTE = marker_byte_position (oldbegv);
7373 if (zv_at_end)
7375 ZV = Z;
7376 ZV_BYTE = Z_BYTE;
7378 else
7380 ZV = XMARKER (oldzv)->charpos;
7381 ZV_BYTE = marker_byte_position (oldzv);
7384 if (point_at_end)
7385 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7386 else
7387 /* We can't do Fgoto_char (oldpoint) because it will run some
7388 Lisp code. */
7389 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7390 XMARKER (oldpoint)->bytepos);
7392 UNGCPRO;
7393 unchain_marker (XMARKER (oldpoint));
7394 unchain_marker (XMARKER (oldbegv));
7395 unchain_marker (XMARKER (oldzv));
7397 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7398 set_buffer_internal (oldbuf);
7399 if (NILP (tem))
7400 windows_or_buffers_changed = old_windows_or_buffers_changed;
7401 message_log_need_newline = !nlflag;
7402 Vdeactivate_mark = old_deactivate_mark;
7407 /* We are at the end of the buffer after just having inserted a newline.
7408 (Note: We depend on the fact we won't be crossing the gap.)
7409 Check to see if the most recent message looks a lot like the previous one.
7410 Return 0 if different, 1 if the new one should just replace it, or a
7411 value N > 1 if we should also append " [N times]". */
7413 static int
7414 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7415 int prev_bol, this_bol;
7416 int prev_bol_byte, this_bol_byte;
7418 int i;
7419 int len = Z_BYTE - 1 - this_bol_byte;
7420 int seen_dots = 0;
7421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7424 for (i = 0; i < len; i++)
7426 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7427 seen_dots = 1;
7428 if (p1[i] != p2[i])
7429 return seen_dots;
7431 p1 += len;
7432 if (*p1 == '\n')
7433 return 2;
7434 if (*p1++ == ' ' && *p1++ == '[')
7436 int n = 0;
7437 while (*p1 >= '0' && *p1 <= '9')
7438 n = n * 10 + *p1++ - '0';
7439 if (strncmp (p1, " times]\n", 8) == 0)
7440 return n+1;
7442 return 0;
7446 /* Display an echo area message M with a specified length of NBYTES
7447 bytes. The string may include null characters. If M is 0, clear
7448 out any existing message, and let the mini-buffer text show
7449 through.
7451 This may GC, so the buffer M must NOT point to a Lisp string. */
7453 void
7454 message2 (m, nbytes, multibyte)
7455 const char *m;
7456 int nbytes;
7457 int multibyte;
7459 /* First flush out any partial line written with print. */
7460 message_log_maybe_newline ();
7461 if (m)
7462 message_dolog (m, nbytes, 1, multibyte);
7463 message2_nolog (m, nbytes, multibyte);
7467 /* The non-logging counterpart of message2. */
7469 void
7470 message2_nolog (m, nbytes, multibyte)
7471 const char *m;
7472 int nbytes, multibyte;
7474 struct frame *sf = SELECTED_FRAME ();
7475 message_enable_multibyte = multibyte;
7477 if (noninteractive)
7479 if (noninteractive_need_newline)
7480 putc ('\n', stderr);
7481 noninteractive_need_newline = 0;
7482 if (m)
7483 fwrite (m, nbytes, 1, stderr);
7484 if (cursor_in_echo_area == 0)
7485 fprintf (stderr, "\n");
7486 fflush (stderr);
7488 /* A null message buffer means that the frame hasn't really been
7489 initialized yet. Error messages get reported properly by
7490 cmd_error, so this must be just an informative message; toss it. */
7491 else if (INTERACTIVE
7492 && sf->glyphs_initialized_p
7493 && FRAME_MESSAGE_BUF (sf))
7495 Lisp_Object mini_window;
7496 struct frame *f;
7498 /* Get the frame containing the mini-buffer
7499 that the selected frame is using. */
7500 mini_window = FRAME_MINIBUF_WINDOW (sf);
7501 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7503 FRAME_SAMPLE_VISIBILITY (f);
7504 if (FRAME_VISIBLE_P (sf)
7505 && ! FRAME_VISIBLE_P (f))
7506 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7508 if (m)
7510 set_message (m, Qnil, nbytes, multibyte);
7511 if (minibuffer_auto_raise)
7512 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7514 else
7515 clear_message (1, 1);
7517 do_pending_window_change (0);
7518 echo_area_display (1);
7519 do_pending_window_change (0);
7520 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7521 (*frame_up_to_date_hook) (f);
7526 /* Display an echo area message M with a specified length of NBYTES
7527 bytes. The string may include null characters. If M is not a
7528 string, clear out any existing message, and let the mini-buffer
7529 text show through.
7531 This function cancels echoing. */
7533 void
7534 message3 (m, nbytes, multibyte)
7535 Lisp_Object m;
7536 int nbytes;
7537 int multibyte;
7539 struct gcpro gcpro1;
7541 GCPRO1 (m);
7542 clear_message (1,1);
7543 cancel_echoing ();
7545 /* First flush out any partial line written with print. */
7546 message_log_maybe_newline ();
7547 if (STRINGP (m))
7549 char *buffer;
7550 USE_SAFE_ALLOCA;
7552 SAFE_ALLOCA (buffer, char *, nbytes);
7553 bcopy (SDATA (m), buffer, nbytes);
7554 message_dolog (buffer, nbytes, 1, multibyte);
7555 SAFE_FREE ();
7557 message3_nolog (m, nbytes, multibyte);
7559 UNGCPRO;
7563 /* The non-logging version of message3.
7564 This does not cancel echoing, because it is used for echoing.
7565 Perhaps we need to make a separate function for echoing
7566 and make this cancel echoing. */
7568 void
7569 message3_nolog (m, nbytes, multibyte)
7570 Lisp_Object m;
7571 int nbytes, multibyte;
7573 struct frame *sf = SELECTED_FRAME ();
7574 message_enable_multibyte = multibyte;
7576 if (noninteractive)
7578 if (noninteractive_need_newline)
7579 putc ('\n', stderr);
7580 noninteractive_need_newline = 0;
7581 if (STRINGP (m))
7582 fwrite (SDATA (m), nbytes, 1, stderr);
7583 if (cursor_in_echo_area == 0)
7584 fprintf (stderr, "\n");
7585 fflush (stderr);
7587 /* A null message buffer means that the frame hasn't really been
7588 initialized yet. Error messages get reported properly by
7589 cmd_error, so this must be just an informative message; toss it. */
7590 else if (INTERACTIVE
7591 && sf->glyphs_initialized_p
7592 && FRAME_MESSAGE_BUF (sf))
7594 Lisp_Object mini_window;
7595 Lisp_Object frame;
7596 struct frame *f;
7598 /* Get the frame containing the mini-buffer
7599 that the selected frame is using. */
7600 mini_window = FRAME_MINIBUF_WINDOW (sf);
7601 frame = XWINDOW (mini_window)->frame;
7602 f = XFRAME (frame);
7604 FRAME_SAMPLE_VISIBILITY (f);
7605 if (FRAME_VISIBLE_P (sf)
7606 && !FRAME_VISIBLE_P (f))
7607 Fmake_frame_visible (frame);
7609 if (STRINGP (m) && SCHARS (m) > 0)
7611 set_message (NULL, m, nbytes, multibyte);
7612 if (minibuffer_auto_raise)
7613 Fraise_frame (frame);
7614 /* Assume we are not echoing.
7615 (If we are, echo_now will override this.) */
7616 echo_message_buffer = Qnil;
7618 else
7619 clear_message (1, 1);
7621 do_pending_window_change (0);
7622 echo_area_display (1);
7623 do_pending_window_change (0);
7624 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7625 (*frame_up_to_date_hook) (f);
7630 /* Display a null-terminated echo area message M. If M is 0, clear
7631 out any existing message, and let the mini-buffer text show through.
7633 The buffer M must continue to exist until after the echo area gets
7634 cleared or some other message gets displayed there. Do not pass
7635 text that is stored in a Lisp string. Do not pass text in a buffer
7636 that was alloca'd. */
7638 void
7639 message1 (m)
7640 char *m;
7642 message2 (m, (m ? strlen (m) : 0), 0);
7646 /* The non-logging counterpart of message1. */
7648 void
7649 message1_nolog (m)
7650 char *m;
7652 message2_nolog (m, (m ? strlen (m) : 0), 0);
7655 /* Display a message M which contains a single %s
7656 which gets replaced with STRING. */
7658 void
7659 message_with_string (m, string, log)
7660 char *m;
7661 Lisp_Object string;
7662 int log;
7664 CHECK_STRING (string);
7666 if (noninteractive)
7668 if (m)
7670 if (noninteractive_need_newline)
7671 putc ('\n', stderr);
7672 noninteractive_need_newline = 0;
7673 fprintf (stderr, m, SDATA (string));
7674 if (cursor_in_echo_area == 0)
7675 fprintf (stderr, "\n");
7676 fflush (stderr);
7679 else if (INTERACTIVE)
7681 /* The frame whose minibuffer we're going to display the message on.
7682 It may be larger than the selected frame, so we need
7683 to use its buffer, not the selected frame's buffer. */
7684 Lisp_Object mini_window;
7685 struct frame *f, *sf = SELECTED_FRAME ();
7687 /* Get the frame containing the minibuffer
7688 that the selected frame is using. */
7689 mini_window = FRAME_MINIBUF_WINDOW (sf);
7690 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7692 /* A null message buffer means that the frame hasn't really been
7693 initialized yet. Error messages get reported properly by
7694 cmd_error, so this must be just an informative message; toss it. */
7695 if (FRAME_MESSAGE_BUF (f))
7697 Lisp_Object args[2], message;
7698 struct gcpro gcpro1, gcpro2;
7700 args[0] = build_string (m);
7701 args[1] = message = string;
7702 GCPRO2 (args[0], message);
7703 gcpro1.nvars = 2;
7705 message = Fformat (2, args);
7707 if (log)
7708 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7709 else
7710 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7712 UNGCPRO;
7714 /* Print should start at the beginning of the message
7715 buffer next time. */
7716 message_buf_print = 0;
7722 /* Dump an informative message to the minibuf. If M is 0, clear out
7723 any existing message, and let the mini-buffer text show through. */
7725 /* VARARGS 1 */
7726 void
7727 message (m, a1, a2, a3)
7728 char *m;
7729 EMACS_INT a1, a2, a3;
7731 if (noninteractive)
7733 if (m)
7735 if (noninteractive_need_newline)
7736 putc ('\n', stderr);
7737 noninteractive_need_newline = 0;
7738 fprintf (stderr, m, a1, a2, a3);
7739 if (cursor_in_echo_area == 0)
7740 fprintf (stderr, "\n");
7741 fflush (stderr);
7744 else if (INTERACTIVE)
7746 /* The frame whose mini-buffer we're going to display the message
7747 on. It may be larger than the selected frame, so we need to
7748 use its buffer, not the selected frame's buffer. */
7749 Lisp_Object mini_window;
7750 struct frame *f, *sf = SELECTED_FRAME ();
7752 /* Get the frame containing the mini-buffer
7753 that the selected frame is using. */
7754 mini_window = FRAME_MINIBUF_WINDOW (sf);
7755 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7757 /* A null message buffer means that the frame hasn't really been
7758 initialized yet. Error messages get reported properly by
7759 cmd_error, so this must be just an informative message; toss
7760 it. */
7761 if (FRAME_MESSAGE_BUF (f))
7763 if (m)
7765 int len;
7766 #ifdef NO_ARG_ARRAY
7767 char *a[3];
7768 a[0] = (char *) a1;
7769 a[1] = (char *) a2;
7770 a[2] = (char *) a3;
7772 len = doprnt (FRAME_MESSAGE_BUF (f),
7773 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7774 #else
7775 len = doprnt (FRAME_MESSAGE_BUF (f),
7776 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7777 (char **) &a1);
7778 #endif /* NO_ARG_ARRAY */
7780 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7782 else
7783 message1 (0);
7785 /* Print should start at the beginning of the message
7786 buffer next time. */
7787 message_buf_print = 0;
7793 /* The non-logging version of message. */
7795 void
7796 message_nolog (m, a1, a2, a3)
7797 char *m;
7798 EMACS_INT a1, a2, a3;
7800 Lisp_Object old_log_max;
7801 old_log_max = Vmessage_log_max;
7802 Vmessage_log_max = Qnil;
7803 message (m, a1, a2, a3);
7804 Vmessage_log_max = old_log_max;
7808 /* Display the current message in the current mini-buffer. This is
7809 only called from error handlers in process.c, and is not time
7810 critical. */
7812 void
7813 update_echo_area ()
7815 if (!NILP (echo_area_buffer[0]))
7817 Lisp_Object string;
7818 string = Fcurrent_message ();
7819 message3 (string, SBYTES (string),
7820 !NILP (current_buffer->enable_multibyte_characters));
7825 /* Make sure echo area buffers in `echo_buffers' are live.
7826 If they aren't, make new ones. */
7828 static void
7829 ensure_echo_area_buffers ()
7831 int i;
7833 for (i = 0; i < 2; ++i)
7834 if (!BUFFERP (echo_buffer[i])
7835 || NILP (XBUFFER (echo_buffer[i])->name))
7837 char name[30];
7838 Lisp_Object old_buffer;
7839 int j;
7841 old_buffer = echo_buffer[i];
7842 sprintf (name, " *Echo Area %d*", i);
7843 echo_buffer[i] = Fget_buffer_create (build_string (name));
7844 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7846 for (j = 0; j < 2; ++j)
7847 if (EQ (old_buffer, echo_area_buffer[j]))
7848 echo_area_buffer[j] = echo_buffer[i];
7853 /* Call FN with args A1..A4 with either the current or last displayed
7854 echo_area_buffer as current buffer.
7856 WHICH zero means use the current message buffer
7857 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7858 from echo_buffer[] and clear it.
7860 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7861 suitable buffer from echo_buffer[] and clear it.
7863 Value is what FN returns. */
7865 static int
7866 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7867 struct window *w;
7868 int which;
7869 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7870 EMACS_INT a1;
7871 Lisp_Object a2;
7872 EMACS_INT a3, a4;
7874 Lisp_Object buffer;
7875 int this_one, the_other, clear_buffer_p, rc;
7876 int count = SPECPDL_INDEX ();
7878 /* If buffers aren't live, make new ones. */
7879 ensure_echo_area_buffers ();
7881 clear_buffer_p = 0;
7883 if (which == 0)
7884 this_one = 0, the_other = 1;
7885 else if (which > 0)
7886 this_one = 1, the_other = 0;
7888 /* Choose a suitable buffer from echo_buffer[] is we don't
7889 have one. */
7890 if (NILP (echo_area_buffer[this_one]))
7892 echo_area_buffer[this_one]
7893 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7894 ? echo_buffer[the_other]
7895 : echo_buffer[this_one]);
7896 clear_buffer_p = 1;
7899 buffer = echo_area_buffer[this_one];
7901 /* Don't get confused by reusing the buffer used for echoing
7902 for a different purpose. */
7903 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7904 cancel_echoing ();
7906 record_unwind_protect (unwind_with_echo_area_buffer,
7907 with_echo_area_buffer_unwind_data (w));
7909 /* Make the echo area buffer current. Note that for display
7910 purposes, it is not necessary that the displayed window's buffer
7911 == current_buffer, except for text property lookup. So, let's
7912 only set that buffer temporarily here without doing a full
7913 Fset_window_buffer. We must also change w->pointm, though,
7914 because otherwise an assertions in unshow_buffer fails, and Emacs
7915 aborts. */
7916 set_buffer_internal_1 (XBUFFER (buffer));
7917 if (w)
7919 w->buffer = buffer;
7920 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7923 current_buffer->undo_list = Qt;
7924 current_buffer->read_only = Qnil;
7925 specbind (Qinhibit_read_only, Qt);
7926 specbind (Qinhibit_modification_hooks, Qt);
7928 if (clear_buffer_p && Z > BEG)
7929 del_range (BEG, Z);
7931 xassert (BEGV >= BEG);
7932 xassert (ZV <= Z && ZV >= BEGV);
7934 rc = fn (a1, a2, a3, a4);
7936 xassert (BEGV >= BEG);
7937 xassert (ZV <= Z && ZV >= BEGV);
7939 unbind_to (count, Qnil);
7940 return rc;
7944 /* Save state that should be preserved around the call to the function
7945 FN called in with_echo_area_buffer. */
7947 static Lisp_Object
7948 with_echo_area_buffer_unwind_data (w)
7949 struct window *w;
7951 int i = 0;
7952 Lisp_Object vector;
7954 /* Reduce consing by keeping one vector in
7955 Vwith_echo_area_save_vector. */
7956 vector = Vwith_echo_area_save_vector;
7957 Vwith_echo_area_save_vector = Qnil;
7959 if (NILP (vector))
7960 vector = Fmake_vector (make_number (7), Qnil);
7962 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7963 AREF (vector, i) = Vdeactivate_mark, ++i;
7964 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7966 if (w)
7968 XSETWINDOW (AREF (vector, i), w); ++i;
7969 AREF (vector, i) = w->buffer; ++i;
7970 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7971 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7973 else
7975 int end = i + 4;
7976 for (; i < end; ++i)
7977 AREF (vector, i) = Qnil;
7980 xassert (i == ASIZE (vector));
7981 return vector;
7985 /* Restore global state from VECTOR which was created by
7986 with_echo_area_buffer_unwind_data. */
7988 static Lisp_Object
7989 unwind_with_echo_area_buffer (vector)
7990 Lisp_Object vector;
7992 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7993 Vdeactivate_mark = AREF (vector, 1);
7994 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7996 if (WINDOWP (AREF (vector, 3)))
7998 struct window *w;
7999 Lisp_Object buffer, charpos, bytepos;
8001 w = XWINDOW (AREF (vector, 3));
8002 buffer = AREF (vector, 4);
8003 charpos = AREF (vector, 5);
8004 bytepos = AREF (vector, 6);
8006 w->buffer = buffer;
8007 set_marker_both (w->pointm, buffer,
8008 XFASTINT (charpos), XFASTINT (bytepos));
8011 Vwith_echo_area_save_vector = vector;
8012 return Qnil;
8016 /* Set up the echo area for use by print functions. MULTIBYTE_P
8017 non-zero means we will print multibyte. */
8019 void
8020 setup_echo_area_for_printing (multibyte_p)
8021 int multibyte_p;
8023 /* If we can't find an echo area any more, exit. */
8024 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8025 Fkill_emacs (Qnil);
8027 ensure_echo_area_buffers ();
8029 if (!message_buf_print)
8031 /* A message has been output since the last time we printed.
8032 Choose a fresh echo area buffer. */
8033 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8034 echo_area_buffer[0] = echo_buffer[1];
8035 else
8036 echo_area_buffer[0] = echo_buffer[0];
8038 /* Switch to that buffer and clear it. */
8039 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8040 current_buffer->truncate_lines = Qnil;
8042 if (Z > BEG)
8044 int count = SPECPDL_INDEX ();
8045 specbind (Qinhibit_read_only, Qt);
8046 /* Note that undo recording is always disabled. */
8047 del_range (BEG, Z);
8048 unbind_to (count, Qnil);
8050 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8052 /* Set up the buffer for the multibyteness we need. */
8053 if (multibyte_p
8054 != !NILP (current_buffer->enable_multibyte_characters))
8055 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8057 /* Raise the frame containing the echo area. */
8058 if (minibuffer_auto_raise)
8060 struct frame *sf = SELECTED_FRAME ();
8061 Lisp_Object mini_window;
8062 mini_window = FRAME_MINIBUF_WINDOW (sf);
8063 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8066 message_log_maybe_newline ();
8067 message_buf_print = 1;
8069 else
8071 if (NILP (echo_area_buffer[0]))
8073 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8074 echo_area_buffer[0] = echo_buffer[1];
8075 else
8076 echo_area_buffer[0] = echo_buffer[0];
8079 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8081 /* Someone switched buffers between print requests. */
8082 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8083 current_buffer->truncate_lines = Qnil;
8089 /* Display an echo area message in window W. Value is non-zero if W's
8090 height is changed. If display_last_displayed_message_p is
8091 non-zero, display the message that was last displayed, otherwise
8092 display the current message. */
8094 static int
8095 display_echo_area (w)
8096 struct window *w;
8098 int i, no_message_p, window_height_changed_p, count;
8100 /* Temporarily disable garbage collections while displaying the echo
8101 area. This is done because a GC can print a message itself.
8102 That message would modify the echo area buffer's contents while a
8103 redisplay of the buffer is going on, and seriously confuse
8104 redisplay. */
8105 count = inhibit_garbage_collection ();
8107 /* If there is no message, we must call display_echo_area_1
8108 nevertheless because it resizes the window. But we will have to
8109 reset the echo_area_buffer in question to nil at the end because
8110 with_echo_area_buffer will sets it to an empty buffer. */
8111 i = display_last_displayed_message_p ? 1 : 0;
8112 no_message_p = NILP (echo_area_buffer[i]);
8114 window_height_changed_p
8115 = with_echo_area_buffer (w, display_last_displayed_message_p,
8116 display_echo_area_1,
8117 (EMACS_INT) w, Qnil, 0, 0);
8119 if (no_message_p)
8120 echo_area_buffer[i] = Qnil;
8122 unbind_to (count, Qnil);
8123 return window_height_changed_p;
8127 /* Helper for display_echo_area. Display the current buffer which
8128 contains the current echo area message in window W, a mini-window,
8129 a pointer to which is passed in A1. A2..A4 are currently not used.
8130 Change the height of W so that all of the message is displayed.
8131 Value is non-zero if height of W was changed. */
8133 static int
8134 display_echo_area_1 (a1, a2, a3, a4)
8135 EMACS_INT a1;
8136 Lisp_Object a2;
8137 EMACS_INT a3, a4;
8139 struct window *w = (struct window *) a1;
8140 Lisp_Object window;
8141 struct text_pos start;
8142 int window_height_changed_p = 0;
8144 /* Do this before displaying, so that we have a large enough glyph
8145 matrix for the display. If we can't get enough space for the
8146 whole text, display the last N lines. That works by setting w->start. */
8147 window_height_changed_p = resize_mini_window (w, 0);
8149 /* Use the starting position chosen by resize_mini_window. */
8150 SET_TEXT_POS_FROM_MARKER (start, w->start);
8152 /* Display. */
8153 clear_glyph_matrix (w->desired_matrix);
8154 XSETWINDOW (window, w);
8155 try_window (window, start, 0);
8157 return window_height_changed_p;
8161 /* Resize the echo area window to exactly the size needed for the
8162 currently displayed message, if there is one. If a mini-buffer
8163 is active, don't shrink it. */
8165 void
8166 resize_echo_area_exactly ()
8168 if (BUFFERP (echo_area_buffer[0])
8169 && WINDOWP (echo_area_window))
8171 struct window *w = XWINDOW (echo_area_window);
8172 int resized_p;
8173 Lisp_Object resize_exactly;
8175 if (minibuf_level == 0)
8176 resize_exactly = Qt;
8177 else
8178 resize_exactly = Qnil;
8180 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8181 (EMACS_INT) w, resize_exactly, 0, 0);
8182 if (resized_p)
8184 ++windows_or_buffers_changed;
8185 ++update_mode_lines;
8186 redisplay_internal (0);
8192 /* Callback function for with_echo_area_buffer, when used from
8193 resize_echo_area_exactly. A1 contains a pointer to the window to
8194 resize, EXACTLY non-nil means resize the mini-window exactly to the
8195 size of the text displayed. A3 and A4 are not used. Value is what
8196 resize_mini_window returns. */
8198 static int
8199 resize_mini_window_1 (a1, exactly, a3, a4)
8200 EMACS_INT a1;
8201 Lisp_Object exactly;
8202 EMACS_INT a3, a4;
8204 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8208 /* Resize mini-window W to fit the size of its contents. EXACT:P
8209 means size the window exactly to the size needed. Otherwise, it's
8210 only enlarged until W's buffer is empty.
8212 Set W->start to the right place to begin display. If the whole
8213 contents fit, start at the beginning. Otherwise, start so as
8214 to make the end of the contents appear. This is particularly
8215 important for y-or-n-p, but seems desirable generally.
8217 Value is non-zero if the window height has been changed. */
8220 resize_mini_window (w, exact_p)
8221 struct window *w;
8222 int exact_p;
8224 struct frame *f = XFRAME (w->frame);
8225 int window_height_changed_p = 0;
8227 xassert (MINI_WINDOW_P (w));
8229 /* By default, start display at the beginning. */
8230 set_marker_both (w->start, w->buffer,
8231 BUF_BEGV (XBUFFER (w->buffer)),
8232 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8234 /* Don't resize windows while redisplaying a window; it would
8235 confuse redisplay functions when the size of the window they are
8236 displaying changes from under them. Such a resizing can happen,
8237 for instance, when which-func prints a long message while
8238 we are running fontification-functions. We're running these
8239 functions with safe_call which binds inhibit-redisplay to t. */
8240 if (!NILP (Vinhibit_redisplay))
8241 return 0;
8243 /* Nil means don't try to resize. */
8244 if (NILP (Vresize_mini_windows)
8245 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8246 return 0;
8248 if (!FRAME_MINIBUF_ONLY_P (f))
8250 struct it it;
8251 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8252 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8253 int height, max_height;
8254 int unit = FRAME_LINE_HEIGHT (f);
8255 struct text_pos start;
8256 struct buffer *old_current_buffer = NULL;
8258 if (current_buffer != XBUFFER (w->buffer))
8260 old_current_buffer = current_buffer;
8261 set_buffer_internal (XBUFFER (w->buffer));
8264 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8266 /* Compute the max. number of lines specified by the user. */
8267 if (FLOATP (Vmax_mini_window_height))
8268 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8269 else if (INTEGERP (Vmax_mini_window_height))
8270 max_height = XINT (Vmax_mini_window_height);
8271 else
8272 max_height = total_height / 4;
8274 /* Correct that max. height if it's bogus. */
8275 max_height = max (1, max_height);
8276 max_height = min (total_height, max_height);
8278 /* Find out the height of the text in the window. */
8279 if (it.truncate_lines_p)
8280 height = 1;
8281 else
8283 last_height = 0;
8284 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8285 if (it.max_ascent == 0 && it.max_descent == 0)
8286 height = it.current_y + last_height;
8287 else
8288 height = it.current_y + it.max_ascent + it.max_descent;
8289 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8290 height = (height + unit - 1) / unit;
8293 /* Compute a suitable window start. */
8294 if (height > max_height)
8296 height = max_height;
8297 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8298 move_it_vertically_backward (&it, (height - 1) * unit);
8299 start = it.current.pos;
8301 else
8302 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8303 SET_MARKER_FROM_TEXT_POS (w->start, start);
8305 if (EQ (Vresize_mini_windows, Qgrow_only))
8307 /* Let it grow only, until we display an empty message, in which
8308 case the window shrinks again. */
8309 if (height > WINDOW_TOTAL_LINES (w))
8311 int old_height = WINDOW_TOTAL_LINES (w);
8312 freeze_window_starts (f, 1);
8313 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8314 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8316 else if (height < WINDOW_TOTAL_LINES (w)
8317 && (exact_p || BEGV == ZV))
8319 int old_height = WINDOW_TOTAL_LINES (w);
8320 freeze_window_starts (f, 0);
8321 shrink_mini_window (w);
8322 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8325 else
8327 /* Always resize to exact size needed. */
8328 if (height > WINDOW_TOTAL_LINES (w))
8330 int old_height = WINDOW_TOTAL_LINES (w);
8331 freeze_window_starts (f, 1);
8332 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8333 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8335 else if (height < WINDOW_TOTAL_LINES (w))
8337 int old_height = WINDOW_TOTAL_LINES (w);
8338 freeze_window_starts (f, 0);
8339 shrink_mini_window (w);
8341 if (height)
8343 freeze_window_starts (f, 1);
8344 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8351 if (old_current_buffer)
8352 set_buffer_internal (old_current_buffer);
8355 return window_height_changed_p;
8359 /* Value is the current message, a string, or nil if there is no
8360 current message. */
8362 Lisp_Object
8363 current_message ()
8365 Lisp_Object msg;
8367 if (NILP (echo_area_buffer[0]))
8368 msg = Qnil;
8369 else
8371 with_echo_area_buffer (0, 0, current_message_1,
8372 (EMACS_INT) &msg, Qnil, 0, 0);
8373 if (NILP (msg))
8374 echo_area_buffer[0] = Qnil;
8377 return msg;
8381 static int
8382 current_message_1 (a1, a2, a3, a4)
8383 EMACS_INT a1;
8384 Lisp_Object a2;
8385 EMACS_INT a3, a4;
8387 Lisp_Object *msg = (Lisp_Object *) a1;
8389 if (Z > BEG)
8390 *msg = make_buffer_string (BEG, Z, 1);
8391 else
8392 *msg = Qnil;
8393 return 0;
8397 /* Push the current message on Vmessage_stack for later restauration
8398 by restore_message. Value is non-zero if the current message isn't
8399 empty. This is a relatively infrequent operation, so it's not
8400 worth optimizing. */
8403 push_message ()
8405 Lisp_Object msg;
8406 msg = current_message ();
8407 Vmessage_stack = Fcons (msg, Vmessage_stack);
8408 return STRINGP (msg);
8412 /* Restore message display from the top of Vmessage_stack. */
8414 void
8415 restore_message ()
8417 Lisp_Object msg;
8419 xassert (CONSP (Vmessage_stack));
8420 msg = XCAR (Vmessage_stack);
8421 if (STRINGP (msg))
8422 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8423 else
8424 message3_nolog (msg, 0, 0);
8428 /* Handler for record_unwind_protect calling pop_message. */
8430 Lisp_Object
8431 pop_message_unwind (dummy)
8432 Lisp_Object dummy;
8434 pop_message ();
8435 return Qnil;
8438 /* Pop the top-most entry off Vmessage_stack. */
8440 void
8441 pop_message ()
8443 xassert (CONSP (Vmessage_stack));
8444 Vmessage_stack = XCDR (Vmessage_stack);
8448 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8449 exits. If the stack is not empty, we have a missing pop_message
8450 somewhere. */
8452 void
8453 check_message_stack ()
8455 if (!NILP (Vmessage_stack))
8456 abort ();
8460 /* Truncate to NCHARS what will be displayed in the echo area the next
8461 time we display it---but don't redisplay it now. */
8463 void
8464 truncate_echo_area (nchars)
8465 int nchars;
8467 if (nchars == 0)
8468 echo_area_buffer[0] = Qnil;
8469 /* A null message buffer means that the frame hasn't really been
8470 initialized yet. Error messages get reported properly by
8471 cmd_error, so this must be just an informative message; toss it. */
8472 else if (!noninteractive
8473 && INTERACTIVE
8474 && !NILP (echo_area_buffer[0]))
8476 struct frame *sf = SELECTED_FRAME ();
8477 if (FRAME_MESSAGE_BUF (sf))
8478 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8483 /* Helper function for truncate_echo_area. Truncate the current
8484 message to at most NCHARS characters. */
8486 static int
8487 truncate_message_1 (nchars, a2, a3, a4)
8488 EMACS_INT nchars;
8489 Lisp_Object a2;
8490 EMACS_INT a3, a4;
8492 if (BEG + nchars < Z)
8493 del_range (BEG + nchars, Z);
8494 if (Z == BEG)
8495 echo_area_buffer[0] = Qnil;
8496 return 0;
8500 /* Set the current message to a substring of S or STRING.
8502 If STRING is a Lisp string, set the message to the first NBYTES
8503 bytes from STRING. NBYTES zero means use the whole string. If
8504 STRING is multibyte, the message will be displayed multibyte.
8506 If S is not null, set the message to the first LEN bytes of S. LEN
8507 zero means use the whole string. MULTIBYTE_P non-zero means S is
8508 multibyte. Display the message multibyte in that case.
8510 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8511 to t before calling set_message_1 (which calls insert).
8514 void
8515 set_message (s, string, nbytes, multibyte_p)
8516 const char *s;
8517 Lisp_Object string;
8518 int nbytes, multibyte_p;
8520 message_enable_multibyte
8521 = ((s && multibyte_p)
8522 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8524 with_echo_area_buffer (0, 0, set_message_1,
8525 (EMACS_INT) s, string, nbytes, multibyte_p);
8526 message_buf_print = 0;
8527 help_echo_showing_p = 0;
8531 /* Helper function for set_message. Arguments have the same meaning
8532 as there, with A1 corresponding to S and A2 corresponding to STRING
8533 This function is called with the echo area buffer being
8534 current. */
8536 static int
8537 set_message_1 (a1, a2, nbytes, multibyte_p)
8538 EMACS_INT a1;
8539 Lisp_Object a2;
8540 EMACS_INT nbytes, multibyte_p;
8542 const char *s = (const char *) a1;
8543 Lisp_Object string = a2;
8545 /* Change multibyteness of the echo buffer appropriately. */
8546 if (message_enable_multibyte
8547 != !NILP (current_buffer->enable_multibyte_characters))
8548 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8550 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8552 /* Insert new message at BEG. */
8553 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8554 Ferase_buffer ();
8556 if (STRINGP (string))
8558 int nchars;
8560 if (nbytes == 0)
8561 nbytes = SBYTES (string);
8562 nchars = string_byte_to_char (string, nbytes);
8564 /* This function takes care of single/multibyte conversion. We
8565 just have to ensure that the echo area buffer has the right
8566 setting of enable_multibyte_characters. */
8567 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8569 else if (s)
8571 if (nbytes == 0)
8572 nbytes = strlen (s);
8574 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8576 /* Convert from multi-byte to single-byte. */
8577 int i, c, n;
8578 unsigned char work[1];
8580 /* Convert a multibyte string to single-byte. */
8581 for (i = 0; i < nbytes; i += n)
8583 c = string_char_and_length (s + i, nbytes - i, &n);
8584 work[0] = (SINGLE_BYTE_CHAR_P (c)
8586 : multibyte_char_to_unibyte (c, Qnil));
8587 insert_1_both (work, 1, 1, 1, 0, 0);
8590 else if (!multibyte_p
8591 && !NILP (current_buffer->enable_multibyte_characters))
8593 /* Convert from single-byte to multi-byte. */
8594 int i, c, n;
8595 const unsigned char *msg = (const unsigned char *) s;
8596 unsigned char str[MAX_MULTIBYTE_LENGTH];
8598 /* Convert a single-byte string to multibyte. */
8599 for (i = 0; i < nbytes; i++)
8601 c = unibyte_char_to_multibyte (msg[i]);
8602 n = CHAR_STRING (c, str);
8603 insert_1_both (str, 1, n, 1, 0, 0);
8606 else
8607 insert_1 (s, nbytes, 1, 0, 0);
8610 return 0;
8614 /* Clear messages. CURRENT_P non-zero means clear the current
8615 message. LAST_DISPLAYED_P non-zero means clear the message
8616 last displayed. */
8618 void
8619 clear_message (current_p, last_displayed_p)
8620 int current_p, last_displayed_p;
8622 if (current_p)
8624 echo_area_buffer[0] = Qnil;
8625 message_cleared_p = 1;
8628 if (last_displayed_p)
8629 echo_area_buffer[1] = Qnil;
8631 message_buf_print = 0;
8634 /* Clear garbaged frames.
8636 This function is used where the old redisplay called
8637 redraw_garbaged_frames which in turn called redraw_frame which in
8638 turn called clear_frame. The call to clear_frame was a source of
8639 flickering. I believe a clear_frame is not necessary. It should
8640 suffice in the new redisplay to invalidate all current matrices,
8641 and ensure a complete redisplay of all windows. */
8643 static void
8644 clear_garbaged_frames ()
8646 if (frame_garbaged)
8648 Lisp_Object tail, frame;
8649 int changed_count = 0;
8651 FOR_EACH_FRAME (tail, frame)
8653 struct frame *f = XFRAME (frame);
8655 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8657 if (f->resized_p)
8659 Fredraw_frame (frame);
8660 f->force_flush_display_p = 1;
8662 clear_current_matrices (f);
8663 changed_count++;
8664 f->garbaged = 0;
8665 f->resized_p = 0;
8669 frame_garbaged = 0;
8670 if (changed_count)
8671 ++windows_or_buffers_changed;
8676 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8677 is non-zero update selected_frame. Value is non-zero if the
8678 mini-windows height has been changed. */
8680 static int
8681 echo_area_display (update_frame_p)
8682 int update_frame_p;
8684 Lisp_Object mini_window;
8685 struct window *w;
8686 struct frame *f;
8687 int window_height_changed_p = 0;
8688 struct frame *sf = SELECTED_FRAME ();
8690 mini_window = FRAME_MINIBUF_WINDOW (sf);
8691 w = XWINDOW (mini_window);
8692 f = XFRAME (WINDOW_FRAME (w));
8694 /* Don't display if frame is invisible or not yet initialized. */
8695 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8696 return 0;
8698 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8699 #ifndef MAC_OS8
8700 #ifdef HAVE_WINDOW_SYSTEM
8701 /* When Emacs starts, selected_frame may be a visible terminal
8702 frame, even if we run under a window system. If we let this
8703 through, a message would be displayed on the terminal. */
8704 if (EQ (selected_frame, Vterminal_frame)
8705 && !NILP (Vwindow_system))
8706 return 0;
8707 #endif /* HAVE_WINDOW_SYSTEM */
8708 #endif
8710 /* Redraw garbaged frames. */
8711 if (frame_garbaged)
8712 clear_garbaged_frames ();
8714 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8716 echo_area_window = mini_window;
8717 window_height_changed_p = display_echo_area (w);
8718 w->must_be_updated_p = 1;
8720 /* Update the display, unless called from redisplay_internal.
8721 Also don't update the screen during redisplay itself. The
8722 update will happen at the end of redisplay, and an update
8723 here could cause confusion. */
8724 if (update_frame_p && !redisplaying_p)
8726 int n = 0;
8728 /* If the display update has been interrupted by pending
8729 input, update mode lines in the frame. Due to the
8730 pending input, it might have been that redisplay hasn't
8731 been called, so that mode lines above the echo area are
8732 garbaged. This looks odd, so we prevent it here. */
8733 if (!display_completed)
8734 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8736 if (window_height_changed_p
8737 /* Don't do this if Emacs is shutting down. Redisplay
8738 needs to run hooks. */
8739 && !NILP (Vrun_hooks))
8741 /* Must update other windows. Likewise as in other
8742 cases, don't let this update be interrupted by
8743 pending input. */
8744 int count = SPECPDL_INDEX ();
8745 specbind (Qredisplay_dont_pause, Qt);
8746 windows_or_buffers_changed = 1;
8747 redisplay_internal (0);
8748 unbind_to (count, Qnil);
8750 else if (FRAME_WINDOW_P (f) && n == 0)
8752 /* Window configuration is the same as before.
8753 Can do with a display update of the echo area,
8754 unless we displayed some mode lines. */
8755 update_single_window (w, 1);
8756 rif->flush_display (f);
8758 else
8759 update_frame (f, 1, 1);
8761 /* If cursor is in the echo area, make sure that the next
8762 redisplay displays the minibuffer, so that the cursor will
8763 be replaced with what the minibuffer wants. */
8764 if (cursor_in_echo_area)
8765 ++windows_or_buffers_changed;
8768 else if (!EQ (mini_window, selected_window))
8769 windows_or_buffers_changed++;
8771 /* The current message is now also the last one displayed. */
8772 echo_area_buffer[1] = echo_area_buffer[0];
8774 /* Prevent redisplay optimization in redisplay_internal by resetting
8775 this_line_start_pos. This is done because the mini-buffer now
8776 displays the message instead of its buffer text. */
8777 if (EQ (mini_window, selected_window))
8778 CHARPOS (this_line_start_pos) = 0;
8780 return window_height_changed_p;
8785 /***********************************************************************
8786 Mode Lines and Frame Titles
8787 ***********************************************************************/
8789 /* A buffer for constructing non-propertized mode-line strings and
8790 frame titles in it; allocated from the heap in init_xdisp and
8791 resized as needed in store_mode_line_noprop_char. */
8793 static char *mode_line_noprop_buf;
8795 /* The buffer's end, and a current output position in it. */
8797 static char *mode_line_noprop_buf_end;
8798 static char *mode_line_noprop_ptr;
8800 #define MODE_LINE_NOPROP_LEN(start) \
8801 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8803 static enum {
8804 MODE_LINE_DISPLAY = 0,
8805 MODE_LINE_TITLE,
8806 MODE_LINE_NOPROP,
8807 MODE_LINE_STRING
8808 } mode_line_target;
8810 /* Alist that caches the results of :propertize.
8811 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8812 static Lisp_Object mode_line_proptrans_alist;
8814 /* List of strings making up the mode-line. */
8815 static Lisp_Object mode_line_string_list;
8817 /* Base face property when building propertized mode line string. */
8818 static Lisp_Object mode_line_string_face;
8819 static Lisp_Object mode_line_string_face_prop;
8822 /* Unwind data for mode line strings */
8824 static Lisp_Object Vmode_line_unwind_vector;
8826 static Lisp_Object
8827 format_mode_line_unwind_data (obuf, save_proptrans)
8828 struct buffer *obuf;
8830 Lisp_Object vector;
8832 /* Reduce consing by keeping one vector in
8833 Vwith_echo_area_save_vector. */
8834 vector = Vmode_line_unwind_vector;
8835 Vmode_line_unwind_vector = Qnil;
8837 if (NILP (vector))
8838 vector = Fmake_vector (make_number (7), Qnil);
8840 AREF (vector, 0) = make_number (mode_line_target);
8841 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8842 AREF (vector, 2) = mode_line_string_list;
8843 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8844 AREF (vector, 4) = mode_line_string_face;
8845 AREF (vector, 5) = mode_line_string_face_prop;
8847 if (obuf)
8848 XSETBUFFER (AREF (vector, 6), obuf);
8849 else
8850 AREF (vector, 6) = Qnil;
8852 return vector;
8855 static Lisp_Object
8856 unwind_format_mode_line (vector)
8857 Lisp_Object vector;
8859 mode_line_target = XINT (AREF (vector, 0));
8860 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8861 mode_line_string_list = AREF (vector, 2);
8862 if (! EQ (AREF (vector, 3), Qt))
8863 mode_line_proptrans_alist = AREF (vector, 3);
8864 mode_line_string_face = AREF (vector, 4);
8865 mode_line_string_face_prop = AREF (vector, 5);
8867 if (!NILP (AREF (vector, 6)))
8869 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8870 AREF (vector, 6) = Qnil;
8873 Vmode_line_unwind_vector = vector;
8874 return Qnil;
8878 /* Store a single character C for the frame title in mode_line_noprop_buf.
8879 Re-allocate mode_line_noprop_buf if necessary. */
8881 static void
8882 #ifdef PROTOTYPES
8883 store_mode_line_noprop_char (char c)
8884 #else
8885 store_mode_line_noprop_char (c)
8886 char c;
8887 #endif
8889 /* If output position has reached the end of the allocated buffer,
8890 double the buffer's size. */
8891 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8893 int len = MODE_LINE_NOPROP_LEN (0);
8894 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8895 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8896 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8897 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8900 *mode_line_noprop_ptr++ = c;
8904 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8905 mode_line_noprop_ptr. STR is the string to store. Do not copy
8906 characters that yield more columns than PRECISION; PRECISION <= 0
8907 means copy the whole string. Pad with spaces until FIELD_WIDTH
8908 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8909 pad. Called from display_mode_element when it is used to build a
8910 frame title. */
8912 static int
8913 store_mode_line_noprop (str, field_width, precision)
8914 const unsigned char *str;
8915 int field_width, precision;
8917 int n = 0;
8918 int dummy, nbytes;
8920 /* Copy at most PRECISION chars from STR. */
8921 nbytes = strlen (str);
8922 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8923 while (nbytes--)
8924 store_mode_line_noprop_char (*str++);
8926 /* Fill up with spaces until FIELD_WIDTH reached. */
8927 while (field_width > 0
8928 && n < field_width)
8930 store_mode_line_noprop_char (' ');
8931 ++n;
8934 return n;
8937 /***********************************************************************
8938 Frame Titles
8939 ***********************************************************************/
8941 #ifdef HAVE_WINDOW_SYSTEM
8943 /* Set the title of FRAME, if it has changed. The title format is
8944 Vicon_title_format if FRAME is iconified, otherwise it is
8945 frame_title_format. */
8947 static void
8948 x_consider_frame_title (frame)
8949 Lisp_Object frame;
8951 struct frame *f = XFRAME (frame);
8953 if (FRAME_WINDOW_P (f)
8954 || FRAME_MINIBUF_ONLY_P (f)
8955 || f->explicit_name)
8957 /* Do we have more than one visible frame on this X display? */
8958 Lisp_Object tail;
8959 Lisp_Object fmt;
8960 int title_start;
8961 char *title;
8962 int len;
8963 struct it it;
8964 int count = SPECPDL_INDEX ();
8966 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8968 Lisp_Object other_frame = XCAR (tail);
8969 struct frame *tf = XFRAME (other_frame);
8971 if (tf != f
8972 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8973 && !FRAME_MINIBUF_ONLY_P (tf)
8974 && !EQ (other_frame, tip_frame)
8975 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8976 break;
8979 /* Set global variable indicating that multiple frames exist. */
8980 multiple_frames = CONSP (tail);
8982 /* Switch to the buffer of selected window of the frame. Set up
8983 mode_line_target so that display_mode_element will output into
8984 mode_line_noprop_buf; then display the title. */
8985 record_unwind_protect (unwind_format_mode_line,
8986 format_mode_line_unwind_data (current_buffer, 0));
8988 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8989 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8991 mode_line_target = MODE_LINE_TITLE;
8992 title_start = MODE_LINE_NOPROP_LEN (0);
8993 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8994 NULL, DEFAULT_FACE_ID);
8995 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8996 len = MODE_LINE_NOPROP_LEN (title_start);
8997 title = mode_line_noprop_buf + title_start;
8998 unbind_to (count, Qnil);
9000 /* Set the title only if it's changed. This avoids consing in
9001 the common case where it hasn't. (If it turns out that we've
9002 already wasted too much time by walking through the list with
9003 display_mode_element, then we might need to optimize at a
9004 higher level than this.) */
9005 if (! STRINGP (f->name)
9006 || SBYTES (f->name) != len
9007 || bcmp (title, SDATA (f->name), len) != 0)
9008 x_implicitly_set_name (f, make_string (title, len), Qnil);
9012 #endif /* not HAVE_WINDOW_SYSTEM */
9017 /***********************************************************************
9018 Menu Bars
9019 ***********************************************************************/
9022 /* Prepare for redisplay by updating menu-bar item lists when
9023 appropriate. This can call eval. */
9025 void
9026 prepare_menu_bars ()
9028 int all_windows;
9029 struct gcpro gcpro1, gcpro2;
9030 struct frame *f;
9031 Lisp_Object tooltip_frame;
9033 #ifdef HAVE_WINDOW_SYSTEM
9034 tooltip_frame = tip_frame;
9035 #else
9036 tooltip_frame = Qnil;
9037 #endif
9039 /* Update all frame titles based on their buffer names, etc. We do
9040 this before the menu bars so that the buffer-menu will show the
9041 up-to-date frame titles. */
9042 #ifdef HAVE_WINDOW_SYSTEM
9043 if (windows_or_buffers_changed || update_mode_lines)
9045 Lisp_Object tail, frame;
9047 FOR_EACH_FRAME (tail, frame)
9049 f = XFRAME (frame);
9050 if (!EQ (frame, tooltip_frame)
9051 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9052 x_consider_frame_title (frame);
9055 #endif /* HAVE_WINDOW_SYSTEM */
9057 /* Update the menu bar item lists, if appropriate. This has to be
9058 done before any actual redisplay or generation of display lines. */
9059 all_windows = (update_mode_lines
9060 || buffer_shared > 1
9061 || windows_or_buffers_changed);
9062 if (all_windows)
9064 Lisp_Object tail, frame;
9065 int count = SPECPDL_INDEX ();
9066 /* 1 means that update_menu_bar has run its hooks
9067 so any further calls to update_menu_bar shouldn't do so again. */
9068 int menu_bar_hooks_run = 0;
9070 record_unwind_save_match_data ();
9072 FOR_EACH_FRAME (tail, frame)
9074 f = XFRAME (frame);
9076 /* Ignore tooltip frame. */
9077 if (EQ (frame, tooltip_frame))
9078 continue;
9080 /* If a window on this frame changed size, report that to
9081 the user and clear the size-change flag. */
9082 if (FRAME_WINDOW_SIZES_CHANGED (f))
9084 Lisp_Object functions;
9086 /* Clear flag first in case we get an error below. */
9087 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9088 functions = Vwindow_size_change_functions;
9089 GCPRO2 (tail, functions);
9091 while (CONSP (functions))
9093 call1 (XCAR (functions), frame);
9094 functions = XCDR (functions);
9096 UNGCPRO;
9099 GCPRO1 (tail);
9100 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9101 #ifdef HAVE_WINDOW_SYSTEM
9102 update_tool_bar (f, 0);
9103 #ifdef MAC_OS
9104 mac_update_title_bar (f, 0);
9105 #endif
9106 #endif
9107 UNGCPRO;
9110 unbind_to (count, Qnil);
9112 else
9114 struct frame *sf = SELECTED_FRAME ();
9115 update_menu_bar (sf, 1, 0);
9116 #ifdef HAVE_WINDOW_SYSTEM
9117 update_tool_bar (sf, 1);
9118 #ifdef MAC_OS
9119 mac_update_title_bar (sf, 1);
9120 #endif
9121 #endif
9124 /* Motif needs this. See comment in xmenu.c. Turn it off when
9125 pending_menu_activation is not defined. */
9126 #ifdef USE_X_TOOLKIT
9127 pending_menu_activation = 0;
9128 #endif
9132 /* Update the menu bar item list for frame F. This has to be done
9133 before we start to fill in any display lines, because it can call
9134 eval.
9136 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9138 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9139 already ran the menu bar hooks for this redisplay, so there
9140 is no need to run them again. The return value is the
9141 updated value of this flag, to pass to the next call. */
9143 static int
9144 update_menu_bar (f, save_match_data, hooks_run)
9145 struct frame *f;
9146 int save_match_data;
9147 int hooks_run;
9149 Lisp_Object window;
9150 register struct window *w;
9152 /* If called recursively during a menu update, do nothing. This can
9153 happen when, for instance, an activate-menubar-hook causes a
9154 redisplay. */
9155 if (inhibit_menubar_update)
9156 return hooks_run;
9158 window = FRAME_SELECTED_WINDOW (f);
9159 w = XWINDOW (window);
9161 #if 0 /* The if statement below this if statement used to include the
9162 condition !NILP (w->update_mode_line), rather than using
9163 update_mode_lines directly, and this if statement may have
9164 been added to make that condition work. Now the if
9165 statement below matches its comment, this isn't needed. */
9166 if (update_mode_lines)
9167 w->update_mode_line = Qt;
9168 #endif
9170 if (FRAME_WINDOW_P (f)
9172 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9173 || defined (USE_GTK)
9174 FRAME_EXTERNAL_MENU_BAR (f)
9175 #else
9176 FRAME_MENU_BAR_LINES (f) > 0
9177 #endif
9178 : FRAME_MENU_BAR_LINES (f) > 0)
9180 /* If the user has switched buffers or windows, we need to
9181 recompute to reflect the new bindings. But we'll
9182 recompute when update_mode_lines is set too; that means
9183 that people can use force-mode-line-update to request
9184 that the menu bar be recomputed. The adverse effect on
9185 the rest of the redisplay algorithm is about the same as
9186 windows_or_buffers_changed anyway. */
9187 if (windows_or_buffers_changed
9188 /* This used to test w->update_mode_line, but we believe
9189 there is no need to recompute the menu in that case. */
9190 || update_mode_lines
9191 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9192 < BUF_MODIFF (XBUFFER (w->buffer)))
9193 != !NILP (w->last_had_star))
9194 || ((!NILP (Vtransient_mark_mode)
9195 && !NILP (XBUFFER (w->buffer)->mark_active))
9196 != !NILP (w->region_showing)))
9198 struct buffer *prev = current_buffer;
9199 int count = SPECPDL_INDEX ();
9201 specbind (Qinhibit_menubar_update, Qt);
9203 set_buffer_internal_1 (XBUFFER (w->buffer));
9204 if (save_match_data)
9205 record_unwind_save_match_data ();
9206 if (NILP (Voverriding_local_map_menu_flag))
9208 specbind (Qoverriding_terminal_local_map, Qnil);
9209 specbind (Qoverriding_local_map, Qnil);
9212 if (!hooks_run)
9214 /* Run the Lucid hook. */
9215 safe_run_hooks (Qactivate_menubar_hook);
9217 /* If it has changed current-menubar from previous value,
9218 really recompute the menu-bar from the value. */
9219 if (! NILP (Vlucid_menu_bar_dirty_flag))
9220 call0 (Qrecompute_lucid_menubar);
9222 safe_run_hooks (Qmenu_bar_update_hook);
9224 hooks_run = 1;
9227 XSETFRAME (Vmenu_updating_frame, f);
9228 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9230 /* Redisplay the menu bar in case we changed it. */
9231 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9232 || defined (USE_GTK)
9233 if (FRAME_WINDOW_P (f))
9235 #ifdef MAC_OS
9236 /* All frames on Mac OS share the same menubar. So only
9237 the selected frame should be allowed to set it. */
9238 if (f == SELECTED_FRAME ())
9239 #endif
9240 set_frame_menubar (f, 0, 0);
9242 else
9243 /* On a terminal screen, the menu bar is an ordinary screen
9244 line, and this makes it get updated. */
9245 w->update_mode_line = Qt;
9246 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9247 /* In the non-toolkit version, the menu bar is an ordinary screen
9248 line, and this makes it get updated. */
9249 w->update_mode_line = Qt;
9250 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9252 unbind_to (count, Qnil);
9253 set_buffer_internal_1 (prev);
9257 return hooks_run;
9262 /***********************************************************************
9263 Output Cursor
9264 ***********************************************************************/
9266 #ifdef HAVE_WINDOW_SYSTEM
9268 /* EXPORT:
9269 Nominal cursor position -- where to draw output.
9270 HPOS and VPOS are window relative glyph matrix coordinates.
9271 X and Y are window relative pixel coordinates. */
9273 struct cursor_pos output_cursor;
9276 /* EXPORT:
9277 Set the global variable output_cursor to CURSOR. All cursor
9278 positions are relative to updated_window. */
9280 void
9281 set_output_cursor (cursor)
9282 struct cursor_pos *cursor;
9284 output_cursor.hpos = cursor->hpos;
9285 output_cursor.vpos = cursor->vpos;
9286 output_cursor.x = cursor->x;
9287 output_cursor.y = cursor->y;
9291 /* EXPORT for RIF:
9292 Set a nominal cursor position.
9294 HPOS and VPOS are column/row positions in a window glyph matrix. X
9295 and Y are window text area relative pixel positions.
9297 If this is done during an update, updated_window will contain the
9298 window that is being updated and the position is the future output
9299 cursor position for that window. If updated_window is null, use
9300 selected_window and display the cursor at the given position. */
9302 void
9303 x_cursor_to (vpos, hpos, y, x)
9304 int vpos, hpos, y, x;
9306 struct window *w;
9308 /* If updated_window is not set, work on selected_window. */
9309 if (updated_window)
9310 w = updated_window;
9311 else
9312 w = XWINDOW (selected_window);
9314 /* Set the output cursor. */
9315 output_cursor.hpos = hpos;
9316 output_cursor.vpos = vpos;
9317 output_cursor.x = x;
9318 output_cursor.y = y;
9320 /* If not called as part of an update, really display the cursor.
9321 This will also set the cursor position of W. */
9322 if (updated_window == NULL)
9324 BLOCK_INPUT;
9325 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9326 if (rif->flush_display_optional)
9327 rif->flush_display_optional (SELECTED_FRAME ());
9328 UNBLOCK_INPUT;
9332 #endif /* HAVE_WINDOW_SYSTEM */
9335 /***********************************************************************
9336 Tool-bars
9337 ***********************************************************************/
9339 #ifdef HAVE_WINDOW_SYSTEM
9341 /* Where the mouse was last time we reported a mouse event. */
9343 FRAME_PTR last_mouse_frame;
9345 /* Tool-bar item index of the item on which a mouse button was pressed
9346 or -1. */
9348 int last_tool_bar_item;
9351 /* Update the tool-bar item list for frame F. This has to be done
9352 before we start to fill in any display lines. Called from
9353 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9354 and restore it here. */
9356 static void
9357 update_tool_bar (f, save_match_data)
9358 struct frame *f;
9359 int save_match_data;
9361 #ifdef USE_GTK
9362 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9363 #else
9364 int do_update = WINDOWP (f->tool_bar_window)
9365 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9366 #endif
9368 if (do_update)
9370 Lisp_Object window;
9371 struct window *w;
9373 window = FRAME_SELECTED_WINDOW (f);
9374 w = XWINDOW (window);
9376 /* If the user has switched buffers or windows, we need to
9377 recompute to reflect the new bindings. But we'll
9378 recompute when update_mode_lines is set too; that means
9379 that people can use force-mode-line-update to request
9380 that the menu bar be recomputed. The adverse effect on
9381 the rest of the redisplay algorithm is about the same as
9382 windows_or_buffers_changed anyway. */
9383 if (windows_or_buffers_changed
9384 || !NILP (w->update_mode_line)
9385 || update_mode_lines
9386 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9387 < BUF_MODIFF (XBUFFER (w->buffer)))
9388 != !NILP (w->last_had_star))
9389 || ((!NILP (Vtransient_mark_mode)
9390 && !NILP (XBUFFER (w->buffer)->mark_active))
9391 != !NILP (w->region_showing)))
9393 struct buffer *prev = current_buffer;
9394 int count = SPECPDL_INDEX ();
9395 Lisp_Object new_tool_bar;
9396 int new_n_tool_bar;
9397 struct gcpro gcpro1;
9399 /* Set current_buffer to the buffer of the selected
9400 window of the frame, so that we get the right local
9401 keymaps. */
9402 set_buffer_internal_1 (XBUFFER (w->buffer));
9404 /* Save match data, if we must. */
9405 if (save_match_data)
9406 record_unwind_save_match_data ();
9408 /* Make sure that we don't accidentally use bogus keymaps. */
9409 if (NILP (Voverriding_local_map_menu_flag))
9411 specbind (Qoverriding_terminal_local_map, Qnil);
9412 specbind (Qoverriding_local_map, Qnil);
9415 GCPRO1 (new_tool_bar);
9417 /* Build desired tool-bar items from keymaps. */
9418 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9419 &new_n_tool_bar);
9421 /* Redisplay the tool-bar if we changed it. */
9422 if (new_n_tool_bar != f->n_tool_bar_items
9423 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9425 /* Redisplay that happens asynchronously due to an expose event
9426 may access f->tool_bar_items. Make sure we update both
9427 variables within BLOCK_INPUT so no such event interrupts. */
9428 BLOCK_INPUT;
9429 f->tool_bar_items = new_tool_bar;
9430 f->n_tool_bar_items = new_n_tool_bar;
9431 w->update_mode_line = Qt;
9432 UNBLOCK_INPUT;
9435 UNGCPRO;
9437 unbind_to (count, Qnil);
9438 set_buffer_internal_1 (prev);
9444 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9445 F's desired tool-bar contents. F->tool_bar_items must have
9446 been set up previously by calling prepare_menu_bars. */
9448 static void
9449 build_desired_tool_bar_string (f)
9450 struct frame *f;
9452 int i, size, size_needed;
9453 struct gcpro gcpro1, gcpro2, gcpro3;
9454 Lisp_Object image, plist, props;
9456 image = plist = props = Qnil;
9457 GCPRO3 (image, plist, props);
9459 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9460 Otherwise, make a new string. */
9462 /* The size of the string we might be able to reuse. */
9463 size = (STRINGP (f->desired_tool_bar_string)
9464 ? SCHARS (f->desired_tool_bar_string)
9465 : 0);
9467 /* We need one space in the string for each image. */
9468 size_needed = f->n_tool_bar_items;
9470 /* Reuse f->desired_tool_bar_string, if possible. */
9471 if (size < size_needed || NILP (f->desired_tool_bar_string))
9472 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9473 make_number (' '));
9474 else
9476 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9477 Fremove_text_properties (make_number (0), make_number (size),
9478 props, f->desired_tool_bar_string);
9481 /* Put a `display' property on the string for the images to display,
9482 put a `menu_item' property on tool-bar items with a value that
9483 is the index of the item in F's tool-bar item vector. */
9484 for (i = 0; i < f->n_tool_bar_items; ++i)
9486 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9488 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9489 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9490 int hmargin, vmargin, relief, idx, end;
9491 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9493 /* If image is a vector, choose the image according to the
9494 button state. */
9495 image = PROP (TOOL_BAR_ITEM_IMAGES);
9496 if (VECTORP (image))
9498 if (enabled_p)
9499 idx = (selected_p
9500 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9501 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9502 else
9503 idx = (selected_p
9504 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9505 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9507 xassert (ASIZE (image) >= idx);
9508 image = AREF (image, idx);
9510 else
9511 idx = -1;
9513 /* Ignore invalid image specifications. */
9514 if (!valid_image_p (image))
9515 continue;
9517 /* Display the tool-bar button pressed, or depressed. */
9518 plist = Fcopy_sequence (XCDR (image));
9520 /* Compute margin and relief to draw. */
9521 relief = (tool_bar_button_relief >= 0
9522 ? tool_bar_button_relief
9523 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9524 hmargin = vmargin = relief;
9526 if (INTEGERP (Vtool_bar_button_margin)
9527 && XINT (Vtool_bar_button_margin) > 0)
9529 hmargin += XFASTINT (Vtool_bar_button_margin);
9530 vmargin += XFASTINT (Vtool_bar_button_margin);
9532 else if (CONSP (Vtool_bar_button_margin))
9534 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9535 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9536 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9538 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9539 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9540 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9543 if (auto_raise_tool_bar_buttons_p)
9545 /* Add a `:relief' property to the image spec if the item is
9546 selected. */
9547 if (selected_p)
9549 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9550 hmargin -= relief;
9551 vmargin -= relief;
9554 else
9556 /* If image is selected, display it pressed, i.e. with a
9557 negative relief. If it's not selected, display it with a
9558 raised relief. */
9559 plist = Fplist_put (plist, QCrelief,
9560 (selected_p
9561 ? make_number (-relief)
9562 : make_number (relief)));
9563 hmargin -= relief;
9564 vmargin -= relief;
9567 /* Put a margin around the image. */
9568 if (hmargin || vmargin)
9570 if (hmargin == vmargin)
9571 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9572 else
9573 plist = Fplist_put (plist, QCmargin,
9574 Fcons (make_number (hmargin),
9575 make_number (vmargin)));
9578 /* If button is not enabled, and we don't have special images
9579 for the disabled state, make the image appear disabled by
9580 applying an appropriate algorithm to it. */
9581 if (!enabled_p && idx < 0)
9582 plist = Fplist_put (plist, QCconversion, Qdisabled);
9584 /* Put a `display' text property on the string for the image to
9585 display. Put a `menu-item' property on the string that gives
9586 the start of this item's properties in the tool-bar items
9587 vector. */
9588 image = Fcons (Qimage, plist);
9589 props = list4 (Qdisplay, image,
9590 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9592 /* Let the last image hide all remaining spaces in the tool bar
9593 string. The string can be longer than needed when we reuse a
9594 previous string. */
9595 if (i + 1 == f->n_tool_bar_items)
9596 end = SCHARS (f->desired_tool_bar_string);
9597 else
9598 end = i + 1;
9599 Fadd_text_properties (make_number (i), make_number (end),
9600 props, f->desired_tool_bar_string);
9601 #undef PROP
9604 UNGCPRO;
9608 /* Display one line of the tool-bar of frame IT->f.
9610 HEIGHT specifies the desired height of the tool-bar line.
9611 If the actual height of the glyph row is less than HEIGHT, the
9612 row's height is increased to HEIGHT, and the icons are centered
9613 vertically in the new height.
9615 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9616 count a final empty row in case the tool-bar width exactly matches
9617 the window width.
9620 static void
9621 display_tool_bar_line (it, height)
9622 struct it *it;
9623 int height;
9625 struct glyph_row *row = it->glyph_row;
9626 int max_x = it->last_visible_x;
9627 struct glyph *last;
9629 prepare_desired_row (row);
9630 row->y = it->current_y;
9632 /* Note that this isn't made use of if the face hasn't a box,
9633 so there's no need to check the face here. */
9634 it->start_of_box_run_p = 1;
9636 while (it->current_x < max_x)
9638 int x, n_glyphs_before, i, nglyphs;
9639 struct it it_before;
9641 /* Get the next display element. */
9642 if (!get_next_display_element (it))
9644 /* Don't count empty row if we are counting needed tool-bar lines. */
9645 if (height < 0 && !it->hpos)
9646 return;
9647 break;
9650 /* Produce glyphs. */
9651 n_glyphs_before = row->used[TEXT_AREA];
9652 it_before = *it;
9654 PRODUCE_GLYPHS (it);
9656 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9657 i = 0;
9658 x = it_before.current_x;
9659 while (i < nglyphs)
9661 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9663 if (x + glyph->pixel_width > max_x)
9665 /* Glyph doesn't fit on line. Backtrack. */
9666 row->used[TEXT_AREA] = n_glyphs_before;
9667 *it = it_before;
9668 /* If this is the only glyph on this line, it will never fit on the
9669 toolbar, so skip it. But ensure there is at least one glyph,
9670 so we don't accidentally disable the tool-bar. */
9671 if (n_glyphs_before == 0
9672 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9673 break;
9674 goto out;
9677 ++it->hpos;
9678 x += glyph->pixel_width;
9679 ++i;
9682 /* Stop at line ends. */
9683 if (ITERATOR_AT_END_OF_LINE_P (it))
9684 break;
9686 set_iterator_to_next (it, 1);
9689 out:;
9691 row->displays_text_p = row->used[TEXT_AREA] != 0;
9692 /* Use default face for the border below the tool bar. */
9693 if (!row->displays_text_p)
9694 it->face_id = DEFAULT_FACE_ID;
9695 extend_face_to_end_of_line (it);
9696 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9697 last->right_box_line_p = 1;
9698 if (last == row->glyphs[TEXT_AREA])
9699 last->left_box_line_p = 1;
9701 /* Make line the desired height and center it vertically. */
9702 if ((height -= it->max_ascent + it->max_descent) > 0)
9704 /* Don't add more than one line height. */
9705 height %= FRAME_LINE_HEIGHT (it->f);
9706 it->max_ascent += height / 2;
9707 it->max_descent += (height + 1) / 2;
9710 compute_line_metrics (it);
9712 /* If line is empty, make it occupy the rest of the tool-bar. */
9713 if (!row->displays_text_p)
9715 row->height = row->phys_height = it->last_visible_y - row->y;
9716 row->ascent = row->phys_ascent = 0;
9717 row->extra_line_spacing = 0;
9720 row->full_width_p = 1;
9721 row->continued_p = 0;
9722 row->truncated_on_left_p = 0;
9723 row->truncated_on_right_p = 0;
9725 it->current_x = it->hpos = 0;
9726 it->current_y += row->height;
9727 ++it->vpos;
9728 ++it->glyph_row;
9732 /* Max tool-bar height. */
9734 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9735 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9737 /* Value is the number of screen lines needed to make all tool-bar
9738 items of frame F visible. The number of actual rows needed is
9739 returned in *N_ROWS if non-NULL. */
9741 static int
9742 tool_bar_lines_needed (f, n_rows)
9743 struct frame *f;
9744 int *n_rows;
9746 struct window *w = XWINDOW (f->tool_bar_window);
9747 struct it it;
9748 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9749 the desired matrix, so use (unused) mode-line row as temporary row to
9750 avoid destroying the first tool-bar row. */
9751 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9753 /* Initialize an iterator for iteration over
9754 F->desired_tool_bar_string in the tool-bar window of frame F. */
9755 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9756 it.first_visible_x = 0;
9757 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9758 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9760 while (!ITERATOR_AT_END_P (&it))
9762 clear_glyph_row (temp_row);
9763 it.glyph_row = temp_row;
9764 display_tool_bar_line (&it, -1);
9766 clear_glyph_row (temp_row);
9768 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9769 if (n_rows)
9770 *n_rows = it.vpos > 0 ? it.vpos : -1;
9772 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9776 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9777 0, 1, 0,
9778 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9779 (frame)
9780 Lisp_Object frame;
9782 struct frame *f;
9783 struct window *w;
9784 int nlines = 0;
9786 if (NILP (frame))
9787 frame = selected_frame;
9788 else
9789 CHECK_FRAME (frame);
9790 f = XFRAME (frame);
9792 if (WINDOWP (f->tool_bar_window)
9793 || (w = XWINDOW (f->tool_bar_window),
9794 WINDOW_TOTAL_LINES (w) > 0))
9796 update_tool_bar (f, 1);
9797 if (f->n_tool_bar_items)
9799 build_desired_tool_bar_string (f);
9800 nlines = tool_bar_lines_needed (f, NULL);
9804 return make_number (nlines);
9808 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9809 height should be changed. */
9811 static int
9812 redisplay_tool_bar (f)
9813 struct frame *f;
9815 struct window *w;
9816 struct it it;
9817 struct glyph_row *row;
9818 int change_height_p = 0;
9820 #ifdef USE_GTK
9821 if (FRAME_EXTERNAL_TOOL_BAR (f))
9822 update_frame_tool_bar (f);
9823 return 0;
9824 #endif
9826 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9827 do anything. This means you must start with tool-bar-lines
9828 non-zero to get the auto-sizing effect. Or in other words, you
9829 can turn off tool-bars by specifying tool-bar-lines zero. */
9830 if (!WINDOWP (f->tool_bar_window)
9831 || (w = XWINDOW (f->tool_bar_window),
9832 WINDOW_TOTAL_LINES (w) == 0))
9833 return 0;
9835 /* Set up an iterator for the tool-bar window. */
9836 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9837 it.first_visible_x = 0;
9838 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9839 row = it.glyph_row;
9841 /* Build a string that represents the contents of the tool-bar. */
9842 build_desired_tool_bar_string (f);
9843 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9845 if (f->n_tool_bar_rows == 0)
9847 int nlines;
9849 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9850 nlines != WINDOW_TOTAL_LINES (w)))
9852 extern Lisp_Object Qtool_bar_lines;
9853 Lisp_Object frame;
9854 int old_height = WINDOW_TOTAL_LINES (w);
9856 XSETFRAME (frame, f);
9857 Fmodify_frame_parameters (frame,
9858 Fcons (Fcons (Qtool_bar_lines,
9859 make_number (nlines)),
9860 Qnil));
9861 if (WINDOW_TOTAL_LINES (w) != old_height)
9863 clear_glyph_matrix (w->desired_matrix);
9864 fonts_changed_p = 1;
9865 return 1;
9870 /* Display as many lines as needed to display all tool-bar items. */
9872 if (f->n_tool_bar_rows > 0)
9874 int border, rows, height, extra;
9876 if (INTEGERP (Vtool_bar_border))
9877 border = XINT (Vtool_bar_border);
9878 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9879 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9880 else if (EQ (Vtool_bar_border, Qborder_width))
9881 border = f->border_width;
9882 else
9883 border = 0;
9884 if (border < 0)
9885 border = 0;
9887 rows = f->n_tool_bar_rows;
9888 height = max (1, (it.last_visible_y - border) / rows);
9889 extra = it.last_visible_y - border - height * rows;
9891 while (it.current_y < it.last_visible_y)
9893 int h = 0;
9894 if (extra > 0 && rows-- > 0)
9896 h = (extra + rows - 1) / rows;
9897 extra -= h;
9899 display_tool_bar_line (&it, height + h);
9902 else
9904 while (it.current_y < it.last_visible_y)
9905 display_tool_bar_line (&it, 0);
9908 /* It doesn't make much sense to try scrolling in the tool-bar
9909 window, so don't do it. */
9910 w->desired_matrix->no_scrolling_p = 1;
9911 w->must_be_updated_p = 1;
9913 if (auto_resize_tool_bars_p)
9915 int nlines, nrows;
9916 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9918 /* If we couldn't display everything, change the tool-bar's
9919 height if there is room for more. */
9920 if (IT_STRING_CHARPOS (it) < it.end_charpos
9921 && it.current_y < max_tool_bar_height)
9922 change_height_p = 1;
9924 row = it.glyph_row - 1;
9926 /* If there are blank lines at the end, except for a partially
9927 visible blank line at the end that is smaller than
9928 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9929 if (!row->displays_text_p
9930 && row->height >= FRAME_LINE_HEIGHT (f))
9931 change_height_p = 1;
9933 /* If row displays tool-bar items, but is partially visible,
9934 change the tool-bar's height. */
9935 if (row->displays_text_p
9936 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9937 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9938 change_height_p = 1;
9940 /* Resize windows as needed by changing the `tool-bar-lines'
9941 frame parameter. */
9942 if (change_height_p
9943 && (nlines = tool_bar_lines_needed (f, &nrows),
9944 nlines != WINDOW_TOTAL_LINES (w)))
9946 extern Lisp_Object Qtool_bar_lines;
9947 Lisp_Object frame;
9948 int old_height = WINDOW_TOTAL_LINES (w);
9950 XSETFRAME (frame, f);
9951 Fmodify_frame_parameters (frame,
9952 Fcons (Fcons (Qtool_bar_lines,
9953 make_number (nlines)),
9954 Qnil));
9955 if (WINDOW_TOTAL_LINES (w) != old_height)
9957 clear_glyph_matrix (w->desired_matrix);
9958 f->n_tool_bar_rows = nrows;
9959 fonts_changed_p = 1;
9964 return change_height_p;
9968 /* Get information about the tool-bar item which is displayed in GLYPH
9969 on frame F. Return in *PROP_IDX the index where tool-bar item
9970 properties start in F->tool_bar_items. Value is zero if
9971 GLYPH doesn't display a tool-bar item. */
9973 static int
9974 tool_bar_item_info (f, glyph, prop_idx)
9975 struct frame *f;
9976 struct glyph *glyph;
9977 int *prop_idx;
9979 Lisp_Object prop;
9980 int success_p;
9981 int charpos;
9983 /* This function can be called asynchronously, which means we must
9984 exclude any possibility that Fget_text_property signals an
9985 error. */
9986 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9987 charpos = max (0, charpos);
9989 /* Get the text property `menu-item' at pos. The value of that
9990 property is the start index of this item's properties in
9991 F->tool_bar_items. */
9992 prop = Fget_text_property (make_number (charpos),
9993 Qmenu_item, f->current_tool_bar_string);
9994 if (INTEGERP (prop))
9996 *prop_idx = XINT (prop);
9997 success_p = 1;
9999 else
10000 success_p = 0;
10002 return success_p;
10006 /* Get information about the tool-bar item at position X/Y on frame F.
10007 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10008 the current matrix of the tool-bar window of F, or NULL if not
10009 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10010 item in F->tool_bar_items. Value is
10012 -1 if X/Y is not on a tool-bar item
10013 0 if X/Y is on the same item that was highlighted before.
10014 1 otherwise. */
10016 static int
10017 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10018 struct frame *f;
10019 int x, y;
10020 struct glyph **glyph;
10021 int *hpos, *vpos, *prop_idx;
10023 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10024 struct window *w = XWINDOW (f->tool_bar_window);
10025 int area;
10027 /* Find the glyph under X/Y. */
10028 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10029 if (*glyph == NULL)
10030 return -1;
10032 /* Get the start of this tool-bar item's properties in
10033 f->tool_bar_items. */
10034 if (!tool_bar_item_info (f, *glyph, prop_idx))
10035 return -1;
10037 /* Is mouse on the highlighted item? */
10038 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10039 && *vpos >= dpyinfo->mouse_face_beg_row
10040 && *vpos <= dpyinfo->mouse_face_end_row
10041 && (*vpos > dpyinfo->mouse_face_beg_row
10042 || *hpos >= dpyinfo->mouse_face_beg_col)
10043 && (*vpos < dpyinfo->mouse_face_end_row
10044 || *hpos < dpyinfo->mouse_face_end_col
10045 || dpyinfo->mouse_face_past_end))
10046 return 0;
10048 return 1;
10052 /* EXPORT:
10053 Handle mouse button event on the tool-bar of frame F, at
10054 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10055 0 for button release. MODIFIERS is event modifiers for button
10056 release. */
10058 void
10059 handle_tool_bar_click (f, x, y, down_p, modifiers)
10060 struct frame *f;
10061 int x, y, down_p;
10062 unsigned int modifiers;
10064 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10065 struct window *w = XWINDOW (f->tool_bar_window);
10066 int hpos, vpos, prop_idx;
10067 struct glyph *glyph;
10068 Lisp_Object enabled_p;
10070 /* If not on the highlighted tool-bar item, return. */
10071 frame_to_window_pixel_xy (w, &x, &y);
10072 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10073 return;
10075 /* If item is disabled, do nothing. */
10076 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10077 if (NILP (enabled_p))
10078 return;
10080 if (down_p)
10082 /* Show item in pressed state. */
10083 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10084 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10085 last_tool_bar_item = prop_idx;
10087 else
10089 Lisp_Object key, frame;
10090 struct input_event event;
10091 EVENT_INIT (event);
10093 /* Show item in released state. */
10094 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10095 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10097 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10099 XSETFRAME (frame, f);
10100 event.kind = TOOL_BAR_EVENT;
10101 event.frame_or_window = frame;
10102 event.arg = frame;
10103 kbd_buffer_store_event (&event);
10105 event.kind = TOOL_BAR_EVENT;
10106 event.frame_or_window = frame;
10107 event.arg = key;
10108 event.modifiers = modifiers;
10109 kbd_buffer_store_event (&event);
10110 last_tool_bar_item = -1;
10115 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10116 tool-bar window-relative coordinates X/Y. Called from
10117 note_mouse_highlight. */
10119 static void
10120 note_tool_bar_highlight (f, x, y)
10121 struct frame *f;
10122 int x, y;
10124 Lisp_Object window = f->tool_bar_window;
10125 struct window *w = XWINDOW (window);
10126 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10127 int hpos, vpos;
10128 struct glyph *glyph;
10129 struct glyph_row *row;
10130 int i;
10131 Lisp_Object enabled_p;
10132 int prop_idx;
10133 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10134 int mouse_down_p, rc;
10136 /* Function note_mouse_highlight is called with negative x(y
10137 values when mouse moves outside of the frame. */
10138 if (x <= 0 || y <= 0)
10140 clear_mouse_face (dpyinfo);
10141 return;
10144 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10145 if (rc < 0)
10147 /* Not on tool-bar item. */
10148 clear_mouse_face (dpyinfo);
10149 return;
10151 else if (rc == 0)
10152 /* On same tool-bar item as before. */
10153 goto set_help_echo;
10155 clear_mouse_face (dpyinfo);
10157 /* Mouse is down, but on different tool-bar item? */
10158 mouse_down_p = (dpyinfo->grabbed
10159 && f == last_mouse_frame
10160 && FRAME_LIVE_P (f));
10161 if (mouse_down_p
10162 && last_tool_bar_item != prop_idx)
10163 return;
10165 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10166 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10168 /* If tool-bar item is not enabled, don't highlight it. */
10169 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10170 if (!NILP (enabled_p))
10172 /* Compute the x-position of the glyph. In front and past the
10173 image is a space. We include this in the highlighted area. */
10174 row = MATRIX_ROW (w->current_matrix, vpos);
10175 for (i = x = 0; i < hpos; ++i)
10176 x += row->glyphs[TEXT_AREA][i].pixel_width;
10178 /* Record this as the current active region. */
10179 dpyinfo->mouse_face_beg_col = hpos;
10180 dpyinfo->mouse_face_beg_row = vpos;
10181 dpyinfo->mouse_face_beg_x = x;
10182 dpyinfo->mouse_face_beg_y = row->y;
10183 dpyinfo->mouse_face_past_end = 0;
10185 dpyinfo->mouse_face_end_col = hpos + 1;
10186 dpyinfo->mouse_face_end_row = vpos;
10187 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10188 dpyinfo->mouse_face_end_y = row->y;
10189 dpyinfo->mouse_face_window = window;
10190 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10192 /* Display it as active. */
10193 show_mouse_face (dpyinfo, draw);
10194 dpyinfo->mouse_face_image_state = draw;
10197 set_help_echo:
10199 /* Set help_echo_string to a help string to display for this tool-bar item.
10200 XTread_socket does the rest. */
10201 help_echo_object = help_echo_window = Qnil;
10202 help_echo_pos = -1;
10203 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10204 if (NILP (help_echo_string))
10205 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10208 #endif /* HAVE_WINDOW_SYSTEM */
10212 /************************************************************************
10213 Horizontal scrolling
10214 ************************************************************************/
10216 static int hscroll_window_tree P_ ((Lisp_Object));
10217 static int hscroll_windows P_ ((Lisp_Object));
10219 /* For all leaf windows in the window tree rooted at WINDOW, set their
10220 hscroll value so that PT is (i) visible in the window, and (ii) so
10221 that it is not within a certain margin at the window's left and
10222 right border. Value is non-zero if any window's hscroll has been
10223 changed. */
10225 static int
10226 hscroll_window_tree (window)
10227 Lisp_Object window;
10229 int hscrolled_p = 0;
10230 int hscroll_relative_p = FLOATP (Vhscroll_step);
10231 int hscroll_step_abs = 0;
10232 double hscroll_step_rel = 0;
10234 if (hscroll_relative_p)
10236 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10237 if (hscroll_step_rel < 0)
10239 hscroll_relative_p = 0;
10240 hscroll_step_abs = 0;
10243 else if (INTEGERP (Vhscroll_step))
10245 hscroll_step_abs = XINT (Vhscroll_step);
10246 if (hscroll_step_abs < 0)
10247 hscroll_step_abs = 0;
10249 else
10250 hscroll_step_abs = 0;
10252 while (WINDOWP (window))
10254 struct window *w = XWINDOW (window);
10256 if (WINDOWP (w->hchild))
10257 hscrolled_p |= hscroll_window_tree (w->hchild);
10258 else if (WINDOWP (w->vchild))
10259 hscrolled_p |= hscroll_window_tree (w->vchild);
10260 else if (w->cursor.vpos >= 0)
10262 int h_margin;
10263 int text_area_width;
10264 struct glyph_row *current_cursor_row
10265 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10266 struct glyph_row *desired_cursor_row
10267 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10268 struct glyph_row *cursor_row
10269 = (desired_cursor_row->enabled_p
10270 ? desired_cursor_row
10271 : current_cursor_row);
10273 text_area_width = window_box_width (w, TEXT_AREA);
10275 /* Scroll when cursor is inside this scroll margin. */
10276 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10278 if ((XFASTINT (w->hscroll)
10279 && w->cursor.x <= h_margin)
10280 || (cursor_row->enabled_p
10281 && cursor_row->truncated_on_right_p
10282 && (w->cursor.x >= text_area_width - h_margin)))
10284 struct it it;
10285 int hscroll;
10286 struct buffer *saved_current_buffer;
10287 int pt;
10288 int wanted_x;
10290 /* Find point in a display of infinite width. */
10291 saved_current_buffer = current_buffer;
10292 current_buffer = XBUFFER (w->buffer);
10294 if (w == XWINDOW (selected_window))
10295 pt = BUF_PT (current_buffer);
10296 else
10298 pt = marker_position (w->pointm);
10299 pt = max (BEGV, pt);
10300 pt = min (ZV, pt);
10303 /* Move iterator to pt starting at cursor_row->start in
10304 a line with infinite width. */
10305 init_to_row_start (&it, w, cursor_row);
10306 it.last_visible_x = INFINITY;
10307 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10308 current_buffer = saved_current_buffer;
10310 /* Position cursor in window. */
10311 if (!hscroll_relative_p && hscroll_step_abs == 0)
10312 hscroll = max (0, (it.current_x
10313 - (ITERATOR_AT_END_OF_LINE_P (&it)
10314 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10315 : (text_area_width / 2))))
10316 / FRAME_COLUMN_WIDTH (it.f);
10317 else if (w->cursor.x >= text_area_width - h_margin)
10319 if (hscroll_relative_p)
10320 wanted_x = text_area_width * (1 - hscroll_step_rel)
10321 - h_margin;
10322 else
10323 wanted_x = text_area_width
10324 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10325 - h_margin;
10326 hscroll
10327 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10329 else
10331 if (hscroll_relative_p)
10332 wanted_x = text_area_width * hscroll_step_rel
10333 + h_margin;
10334 else
10335 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10336 + h_margin;
10337 hscroll
10338 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10340 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10342 /* Don't call Fset_window_hscroll if value hasn't
10343 changed because it will prevent redisplay
10344 optimizations. */
10345 if (XFASTINT (w->hscroll) != hscroll)
10347 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10348 w->hscroll = make_number (hscroll);
10349 hscrolled_p = 1;
10354 window = w->next;
10357 /* Value is non-zero if hscroll of any leaf window has been changed. */
10358 return hscrolled_p;
10362 /* Set hscroll so that cursor is visible and not inside horizontal
10363 scroll margins for all windows in the tree rooted at WINDOW. See
10364 also hscroll_window_tree above. Value is non-zero if any window's
10365 hscroll has been changed. If it has, desired matrices on the frame
10366 of WINDOW are cleared. */
10368 static int
10369 hscroll_windows (window)
10370 Lisp_Object window;
10372 int hscrolled_p;
10374 if (automatic_hscrolling_p)
10376 hscrolled_p = hscroll_window_tree (window);
10377 if (hscrolled_p)
10378 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10380 else
10381 hscrolled_p = 0;
10382 return hscrolled_p;
10387 /************************************************************************
10388 Redisplay
10389 ************************************************************************/
10391 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10392 to a non-zero value. This is sometimes handy to have in a debugger
10393 session. */
10395 #if GLYPH_DEBUG
10397 /* First and last unchanged row for try_window_id. */
10399 int debug_first_unchanged_at_end_vpos;
10400 int debug_last_unchanged_at_beg_vpos;
10402 /* Delta vpos and y. */
10404 int debug_dvpos, debug_dy;
10406 /* Delta in characters and bytes for try_window_id. */
10408 int debug_delta, debug_delta_bytes;
10410 /* Values of window_end_pos and window_end_vpos at the end of
10411 try_window_id. */
10413 EMACS_INT debug_end_pos, debug_end_vpos;
10415 /* Append a string to W->desired_matrix->method. FMT is a printf
10416 format string. A1...A9 are a supplement for a variable-length
10417 argument list. If trace_redisplay_p is non-zero also printf the
10418 resulting string to stderr. */
10420 static void
10421 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10422 struct window *w;
10423 char *fmt;
10424 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10426 char buffer[512];
10427 char *method = w->desired_matrix->method;
10428 int len = strlen (method);
10429 int size = sizeof w->desired_matrix->method;
10430 int remaining = size - len - 1;
10432 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10433 if (len && remaining)
10435 method[len] = '|';
10436 --remaining, ++len;
10439 strncpy (method + len, buffer, remaining);
10441 if (trace_redisplay_p)
10442 fprintf (stderr, "%p (%s): %s\n",
10444 ((BUFFERP (w->buffer)
10445 && STRINGP (XBUFFER (w->buffer)->name))
10446 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10447 : "no buffer"),
10448 buffer);
10451 #endif /* GLYPH_DEBUG */
10454 /* Value is non-zero if all changes in window W, which displays
10455 current_buffer, are in the text between START and END. START is a
10456 buffer position, END is given as a distance from Z. Used in
10457 redisplay_internal for display optimization. */
10459 static INLINE int
10460 text_outside_line_unchanged_p (w, start, end)
10461 struct window *w;
10462 int start, end;
10464 int unchanged_p = 1;
10466 /* If text or overlays have changed, see where. */
10467 if (XFASTINT (w->last_modified) < MODIFF
10468 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10470 /* Gap in the line? */
10471 if (GPT < start || Z - GPT < end)
10472 unchanged_p = 0;
10474 /* Changes start in front of the line, or end after it? */
10475 if (unchanged_p
10476 && (BEG_UNCHANGED < start - 1
10477 || END_UNCHANGED < end))
10478 unchanged_p = 0;
10480 /* If selective display, can't optimize if changes start at the
10481 beginning of the line. */
10482 if (unchanged_p
10483 && INTEGERP (current_buffer->selective_display)
10484 && XINT (current_buffer->selective_display) > 0
10485 && (BEG_UNCHANGED < start || GPT <= start))
10486 unchanged_p = 0;
10488 /* If there are overlays at the start or end of the line, these
10489 may have overlay strings with newlines in them. A change at
10490 START, for instance, may actually concern the display of such
10491 overlay strings as well, and they are displayed on different
10492 lines. So, quickly rule out this case. (For the future, it
10493 might be desirable to implement something more telling than
10494 just BEG/END_UNCHANGED.) */
10495 if (unchanged_p)
10497 if (BEG + BEG_UNCHANGED == start
10498 && overlay_touches_p (start))
10499 unchanged_p = 0;
10500 if (END_UNCHANGED == end
10501 && overlay_touches_p (Z - end))
10502 unchanged_p = 0;
10506 return unchanged_p;
10510 /* Do a frame update, taking possible shortcuts into account. This is
10511 the main external entry point for redisplay.
10513 If the last redisplay displayed an echo area message and that message
10514 is no longer requested, we clear the echo area or bring back the
10515 mini-buffer if that is in use. */
10517 void
10518 redisplay ()
10520 redisplay_internal (0);
10524 static Lisp_Object
10525 overlay_arrow_string_or_property (var)
10526 Lisp_Object var;
10528 Lisp_Object val;
10530 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10531 return val;
10533 return Voverlay_arrow_string;
10536 /* Return 1 if there are any overlay-arrows in current_buffer. */
10537 static int
10538 overlay_arrow_in_current_buffer_p ()
10540 Lisp_Object vlist;
10542 for (vlist = Voverlay_arrow_variable_list;
10543 CONSP (vlist);
10544 vlist = XCDR (vlist))
10546 Lisp_Object var = XCAR (vlist);
10547 Lisp_Object val;
10549 if (!SYMBOLP (var))
10550 continue;
10551 val = find_symbol_value (var);
10552 if (MARKERP (val)
10553 && current_buffer == XMARKER (val)->buffer)
10554 return 1;
10556 return 0;
10560 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10561 has changed. */
10563 static int
10564 overlay_arrows_changed_p ()
10566 Lisp_Object vlist;
10568 for (vlist = Voverlay_arrow_variable_list;
10569 CONSP (vlist);
10570 vlist = XCDR (vlist))
10572 Lisp_Object var = XCAR (vlist);
10573 Lisp_Object val, pstr;
10575 if (!SYMBOLP (var))
10576 continue;
10577 val = find_symbol_value (var);
10578 if (!MARKERP (val))
10579 continue;
10580 if (! EQ (COERCE_MARKER (val),
10581 Fget (var, Qlast_arrow_position))
10582 || ! (pstr = overlay_arrow_string_or_property (var),
10583 EQ (pstr, Fget (var, Qlast_arrow_string))))
10584 return 1;
10586 return 0;
10589 /* Mark overlay arrows to be updated on next redisplay. */
10591 static void
10592 update_overlay_arrows (up_to_date)
10593 int up_to_date;
10595 Lisp_Object vlist;
10597 for (vlist = Voverlay_arrow_variable_list;
10598 CONSP (vlist);
10599 vlist = XCDR (vlist))
10601 Lisp_Object var = XCAR (vlist);
10603 if (!SYMBOLP (var))
10604 continue;
10606 if (up_to_date > 0)
10608 Lisp_Object val = find_symbol_value (var);
10609 Fput (var, Qlast_arrow_position,
10610 COERCE_MARKER (val));
10611 Fput (var, Qlast_arrow_string,
10612 overlay_arrow_string_or_property (var));
10614 else if (up_to_date < 0
10615 || !NILP (Fget (var, Qlast_arrow_position)))
10617 Fput (var, Qlast_arrow_position, Qt);
10618 Fput (var, Qlast_arrow_string, Qt);
10624 /* Return overlay arrow string to display at row.
10625 Return integer (bitmap number) for arrow bitmap in left fringe.
10626 Return nil if no overlay arrow. */
10628 static Lisp_Object
10629 overlay_arrow_at_row (it, row)
10630 struct it *it;
10631 struct glyph_row *row;
10633 Lisp_Object vlist;
10635 for (vlist = Voverlay_arrow_variable_list;
10636 CONSP (vlist);
10637 vlist = XCDR (vlist))
10639 Lisp_Object var = XCAR (vlist);
10640 Lisp_Object val;
10642 if (!SYMBOLP (var))
10643 continue;
10645 val = find_symbol_value (var);
10647 if (MARKERP (val)
10648 && current_buffer == XMARKER (val)->buffer
10649 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10651 if (FRAME_WINDOW_P (it->f)
10652 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10654 #ifdef HAVE_WINDOW_SYSTEM
10655 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10657 int fringe_bitmap;
10658 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10659 return make_number (fringe_bitmap);
10661 #endif
10662 return make_number (-1); /* Use default arrow bitmap */
10664 return overlay_arrow_string_or_property (var);
10668 return Qnil;
10671 /* Return 1 if point moved out of or into a composition. Otherwise
10672 return 0. PREV_BUF and PREV_PT are the last point buffer and
10673 position. BUF and PT are the current point buffer and position. */
10676 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10677 struct buffer *prev_buf, *buf;
10678 int prev_pt, pt;
10680 int start, end;
10681 Lisp_Object prop;
10682 Lisp_Object buffer;
10684 XSETBUFFER (buffer, buf);
10685 /* Check a composition at the last point if point moved within the
10686 same buffer. */
10687 if (prev_buf == buf)
10689 if (prev_pt == pt)
10690 /* Point didn't move. */
10691 return 0;
10693 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10694 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10695 && COMPOSITION_VALID_P (start, end, prop)
10696 && start < prev_pt && end > prev_pt)
10697 /* The last point was within the composition. Return 1 iff
10698 point moved out of the composition. */
10699 return (pt <= start || pt >= end);
10702 /* Check a composition at the current point. */
10703 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10704 && find_composition (pt, -1, &start, &end, &prop, buffer)
10705 && COMPOSITION_VALID_P (start, end, prop)
10706 && start < pt && end > pt);
10710 /* Reconsider the setting of B->clip_changed which is displayed
10711 in window W. */
10713 static INLINE void
10714 reconsider_clip_changes (w, b)
10715 struct window *w;
10716 struct buffer *b;
10718 if (b->clip_changed
10719 && !NILP (w->window_end_valid)
10720 && w->current_matrix->buffer == b
10721 && w->current_matrix->zv == BUF_ZV (b)
10722 && w->current_matrix->begv == BUF_BEGV (b))
10723 b->clip_changed = 0;
10725 /* If display wasn't paused, and W is not a tool bar window, see if
10726 point has been moved into or out of a composition. In that case,
10727 we set b->clip_changed to 1 to force updating the screen. If
10728 b->clip_changed has already been set to 1, we can skip this
10729 check. */
10730 if (!b->clip_changed
10731 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10733 int pt;
10735 if (w == XWINDOW (selected_window))
10736 pt = BUF_PT (current_buffer);
10737 else
10738 pt = marker_position (w->pointm);
10740 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10741 || pt != XINT (w->last_point))
10742 && check_point_in_composition (w->current_matrix->buffer,
10743 XINT (w->last_point),
10744 XBUFFER (w->buffer), pt))
10745 b->clip_changed = 1;
10750 /* Select FRAME to forward the values of frame-local variables into C
10751 variables so that the redisplay routines can access those values
10752 directly. */
10754 static void
10755 select_frame_for_redisplay (frame)
10756 Lisp_Object frame;
10758 Lisp_Object tail, sym, val;
10759 Lisp_Object old = selected_frame;
10761 selected_frame = frame;
10763 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10764 if (CONSP (XCAR (tail))
10765 && (sym = XCAR (XCAR (tail)),
10766 SYMBOLP (sym))
10767 && (sym = indirect_variable (sym),
10768 val = SYMBOL_VALUE (sym),
10769 (BUFFER_LOCAL_VALUEP (val)
10770 || SOME_BUFFER_LOCAL_VALUEP (val)))
10771 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10772 /* Use find_symbol_value rather than Fsymbol_value
10773 to avoid an error if it is void. */
10774 find_symbol_value (sym);
10776 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10777 if (CONSP (XCAR (tail))
10778 && (sym = XCAR (XCAR (tail)),
10779 SYMBOLP (sym))
10780 && (sym = indirect_variable (sym),
10781 val = SYMBOL_VALUE (sym),
10782 (BUFFER_LOCAL_VALUEP (val)
10783 || SOME_BUFFER_LOCAL_VALUEP (val)))
10784 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10785 find_symbol_value (sym);
10789 #define STOP_POLLING \
10790 do { if (! polling_stopped_here) stop_polling (); \
10791 polling_stopped_here = 1; } while (0)
10793 #define RESUME_POLLING \
10794 do { if (polling_stopped_here) start_polling (); \
10795 polling_stopped_here = 0; } while (0)
10798 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10799 response to any user action; therefore, we should preserve the echo
10800 area. (Actually, our caller does that job.) Perhaps in the future
10801 avoid recentering windows if it is not necessary; currently that
10802 causes some problems. */
10804 static void
10805 redisplay_internal (preserve_echo_area)
10806 int preserve_echo_area;
10808 struct window *w = XWINDOW (selected_window);
10809 struct frame *f;
10810 int pause;
10811 int must_finish = 0;
10812 struct text_pos tlbufpos, tlendpos;
10813 int number_of_visible_frames;
10814 int count;
10815 struct frame *sf;
10816 int polling_stopped_here = 0;
10818 /* Non-zero means redisplay has to consider all windows on all
10819 frames. Zero means, only selected_window is considered. */
10820 int consider_all_windows_p;
10822 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10824 /* No redisplay if running in batch mode or frame is not yet fully
10825 initialized, or redisplay is explicitly turned off by setting
10826 Vinhibit_redisplay. */
10827 if (noninteractive
10828 || !NILP (Vinhibit_redisplay))
10829 return;
10831 /* Don't examine these until after testing Vinhibit_redisplay.
10832 When Emacs is shutting down, perhaps because its connection to
10833 X has dropped, we should not look at them at all. */
10834 f = XFRAME (w->frame);
10835 sf = SELECTED_FRAME ();
10837 if (!f->glyphs_initialized_p)
10838 return;
10840 /* The flag redisplay_performed_directly_p is set by
10841 direct_output_for_insert when it already did the whole screen
10842 update necessary. */
10843 if (redisplay_performed_directly_p)
10845 redisplay_performed_directly_p = 0;
10846 if (!hscroll_windows (selected_window))
10847 return;
10850 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10851 if (popup_activated ())
10852 return;
10853 #endif
10855 /* I don't think this happens but let's be paranoid. */
10856 if (redisplaying_p)
10857 return;
10859 /* Record a function that resets redisplaying_p to its old value
10860 when we leave this function. */
10861 count = SPECPDL_INDEX ();
10862 record_unwind_protect (unwind_redisplay,
10863 Fcons (make_number (redisplaying_p), selected_frame));
10864 ++redisplaying_p;
10865 specbind (Qinhibit_free_realized_faces, Qnil);
10868 Lisp_Object tail, frame;
10870 FOR_EACH_FRAME (tail, frame)
10872 struct frame *f = XFRAME (frame);
10873 f->already_hscrolled_p = 0;
10877 retry:
10878 pause = 0;
10879 reconsider_clip_changes (w, current_buffer);
10880 last_escape_glyph_frame = NULL;
10881 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10883 /* If new fonts have been loaded that make a glyph matrix adjustment
10884 necessary, do it. */
10885 if (fonts_changed_p)
10887 adjust_glyphs (NULL);
10888 ++windows_or_buffers_changed;
10889 fonts_changed_p = 0;
10892 /* If face_change_count is non-zero, init_iterator will free all
10893 realized faces, which includes the faces referenced from current
10894 matrices. So, we can't reuse current matrices in this case. */
10895 if (face_change_count)
10896 ++windows_or_buffers_changed;
10898 if (! FRAME_WINDOW_P (sf)
10899 && previous_terminal_frame != sf)
10901 /* Since frames on an ASCII terminal share the same display
10902 area, displaying a different frame means redisplay the whole
10903 thing. */
10904 windows_or_buffers_changed++;
10905 SET_FRAME_GARBAGED (sf);
10906 XSETFRAME (Vterminal_frame, sf);
10908 previous_terminal_frame = sf;
10910 /* Set the visible flags for all frames. Do this before checking
10911 for resized or garbaged frames; they want to know if their frames
10912 are visible. See the comment in frame.h for
10913 FRAME_SAMPLE_VISIBILITY. */
10915 Lisp_Object tail, frame;
10917 number_of_visible_frames = 0;
10919 FOR_EACH_FRAME (tail, frame)
10921 struct frame *f = XFRAME (frame);
10923 FRAME_SAMPLE_VISIBILITY (f);
10924 if (FRAME_VISIBLE_P (f))
10925 ++number_of_visible_frames;
10926 clear_desired_matrices (f);
10930 /* Notice any pending interrupt request to change frame size. */
10931 do_pending_window_change (1);
10933 /* Clear frames marked as garbaged. */
10934 if (frame_garbaged)
10935 clear_garbaged_frames ();
10937 /* Build menubar and tool-bar items. */
10938 if (NILP (Vmemory_full))
10939 prepare_menu_bars ();
10941 if (windows_or_buffers_changed)
10942 update_mode_lines++;
10944 /* Detect case that we need to write or remove a star in the mode line. */
10945 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10947 w->update_mode_line = Qt;
10948 if (buffer_shared > 1)
10949 update_mode_lines++;
10952 /* If %c is in the mode line, update it if needed. */
10953 if (!NILP (w->column_number_displayed)
10954 /* This alternative quickly identifies a common case
10955 where no change is needed. */
10956 && !(PT == XFASTINT (w->last_point)
10957 && XFASTINT (w->last_modified) >= MODIFF
10958 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10959 && (XFASTINT (w->column_number_displayed)
10960 != (int) current_column ())) /* iftc */
10961 w->update_mode_line = Qt;
10963 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10965 /* The variable buffer_shared is set in redisplay_window and
10966 indicates that we redisplay a buffer in different windows. See
10967 there. */
10968 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10969 || cursor_type_changed);
10971 /* If specs for an arrow have changed, do thorough redisplay
10972 to ensure we remove any arrow that should no longer exist. */
10973 if (overlay_arrows_changed_p ())
10974 consider_all_windows_p = windows_or_buffers_changed = 1;
10976 /* Normally the message* functions will have already displayed and
10977 updated the echo area, but the frame may have been trashed, or
10978 the update may have been preempted, so display the echo area
10979 again here. Checking message_cleared_p captures the case that
10980 the echo area should be cleared. */
10981 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10982 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10983 || (message_cleared_p
10984 && minibuf_level == 0
10985 /* If the mini-window is currently selected, this means the
10986 echo-area doesn't show through. */
10987 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10989 int window_height_changed_p = echo_area_display (0);
10990 must_finish = 1;
10992 /* If we don't display the current message, don't clear the
10993 message_cleared_p flag, because, if we did, we wouldn't clear
10994 the echo area in the next redisplay which doesn't preserve
10995 the echo area. */
10996 if (!display_last_displayed_message_p)
10997 message_cleared_p = 0;
10999 if (fonts_changed_p)
11000 goto retry;
11001 else if (window_height_changed_p)
11003 consider_all_windows_p = 1;
11004 ++update_mode_lines;
11005 ++windows_or_buffers_changed;
11007 /* If window configuration was changed, frames may have been
11008 marked garbaged. Clear them or we will experience
11009 surprises wrt scrolling. */
11010 if (frame_garbaged)
11011 clear_garbaged_frames ();
11014 else if (EQ (selected_window, minibuf_window)
11015 && (current_buffer->clip_changed
11016 || XFASTINT (w->last_modified) < MODIFF
11017 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11018 && resize_mini_window (w, 0))
11020 /* Resized active mini-window to fit the size of what it is
11021 showing if its contents might have changed. */
11022 must_finish = 1;
11023 consider_all_windows_p = 1;
11024 ++windows_or_buffers_changed;
11025 ++update_mode_lines;
11027 /* If window configuration was changed, frames may have been
11028 marked garbaged. Clear them or we will experience
11029 surprises wrt scrolling. */
11030 if (frame_garbaged)
11031 clear_garbaged_frames ();
11035 /* If showing the region, and mark has changed, we must redisplay
11036 the whole window. The assignment to this_line_start_pos prevents
11037 the optimization directly below this if-statement. */
11038 if (((!NILP (Vtransient_mark_mode)
11039 && !NILP (XBUFFER (w->buffer)->mark_active))
11040 != !NILP (w->region_showing))
11041 || (!NILP (w->region_showing)
11042 && !EQ (w->region_showing,
11043 Fmarker_position (XBUFFER (w->buffer)->mark))))
11044 CHARPOS (this_line_start_pos) = 0;
11046 /* Optimize the case that only the line containing the cursor in the
11047 selected window has changed. Variables starting with this_ are
11048 set in display_line and record information about the line
11049 containing the cursor. */
11050 tlbufpos = this_line_start_pos;
11051 tlendpos = this_line_end_pos;
11052 if (!consider_all_windows_p
11053 && CHARPOS (tlbufpos) > 0
11054 && NILP (w->update_mode_line)
11055 && !current_buffer->clip_changed
11056 && !current_buffer->prevent_redisplay_optimizations_p
11057 && FRAME_VISIBLE_P (XFRAME (w->frame))
11058 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11059 /* Make sure recorded data applies to current buffer, etc. */
11060 && this_line_buffer == current_buffer
11061 && current_buffer == XBUFFER (w->buffer)
11062 && NILP (w->force_start)
11063 && NILP (w->optional_new_start)
11064 /* Point must be on the line that we have info recorded about. */
11065 && PT >= CHARPOS (tlbufpos)
11066 && PT <= Z - CHARPOS (tlendpos)
11067 /* All text outside that line, including its final newline,
11068 must be unchanged */
11069 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11070 CHARPOS (tlendpos)))
11072 if (CHARPOS (tlbufpos) > BEGV
11073 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11074 && (CHARPOS (tlbufpos) == ZV
11075 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11076 /* Former continuation line has disappeared by becoming empty */
11077 goto cancel;
11078 else if (XFASTINT (w->last_modified) < MODIFF
11079 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11080 || MINI_WINDOW_P (w))
11082 /* We have to handle the case of continuation around a
11083 wide-column character (See the comment in indent.c around
11084 line 885).
11086 For instance, in the following case:
11088 -------- Insert --------
11089 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11090 J_I_ ==> J_I_ `^^' are cursors.
11091 ^^ ^^
11092 -------- --------
11094 As we have to redraw the line above, we should goto cancel. */
11096 struct it it;
11097 int line_height_before = this_line_pixel_height;
11099 /* Note that start_display will handle the case that the
11100 line starting at tlbufpos is a continuation lines. */
11101 start_display (&it, w, tlbufpos);
11103 /* Implementation note: It this still necessary? */
11104 if (it.current_x != this_line_start_x)
11105 goto cancel;
11107 TRACE ((stderr, "trying display optimization 1\n"));
11108 w->cursor.vpos = -1;
11109 overlay_arrow_seen = 0;
11110 it.vpos = this_line_vpos;
11111 it.current_y = this_line_y;
11112 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11113 display_line (&it);
11115 /* If line contains point, is not continued,
11116 and ends at same distance from eob as before, we win */
11117 if (w->cursor.vpos >= 0
11118 /* Line is not continued, otherwise this_line_start_pos
11119 would have been set to 0 in display_line. */
11120 && CHARPOS (this_line_start_pos)
11121 /* Line ends as before. */
11122 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11123 /* Line has same height as before. Otherwise other lines
11124 would have to be shifted up or down. */
11125 && this_line_pixel_height == line_height_before)
11127 /* If this is not the window's last line, we must adjust
11128 the charstarts of the lines below. */
11129 if (it.current_y < it.last_visible_y)
11131 struct glyph_row *row
11132 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11133 int delta, delta_bytes;
11135 if (Z - CHARPOS (tlendpos) == ZV)
11137 /* This line ends at end of (accessible part of)
11138 buffer. There is no newline to count. */
11139 delta = (Z
11140 - CHARPOS (tlendpos)
11141 - MATRIX_ROW_START_CHARPOS (row));
11142 delta_bytes = (Z_BYTE
11143 - BYTEPOS (tlendpos)
11144 - MATRIX_ROW_START_BYTEPOS (row));
11146 else
11148 /* This line ends in a newline. Must take
11149 account of the newline and the rest of the
11150 text that follows. */
11151 delta = (Z
11152 - CHARPOS (tlendpos)
11153 - MATRIX_ROW_START_CHARPOS (row));
11154 delta_bytes = (Z_BYTE
11155 - BYTEPOS (tlendpos)
11156 - MATRIX_ROW_START_BYTEPOS (row));
11159 increment_matrix_positions (w->current_matrix,
11160 this_line_vpos + 1,
11161 w->current_matrix->nrows,
11162 delta, delta_bytes);
11165 /* If this row displays text now but previously didn't,
11166 or vice versa, w->window_end_vpos may have to be
11167 adjusted. */
11168 if ((it.glyph_row - 1)->displays_text_p)
11170 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11171 XSETINT (w->window_end_vpos, this_line_vpos);
11173 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11174 && this_line_vpos > 0)
11175 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11176 w->window_end_valid = Qnil;
11178 /* Update hint: No need to try to scroll in update_window. */
11179 w->desired_matrix->no_scrolling_p = 1;
11181 #if GLYPH_DEBUG
11182 *w->desired_matrix->method = 0;
11183 debug_method_add (w, "optimization 1");
11184 #endif
11185 #ifdef HAVE_WINDOW_SYSTEM
11186 update_window_fringes (w, 0);
11187 #endif
11188 goto update;
11190 else
11191 goto cancel;
11193 else if (/* Cursor position hasn't changed. */
11194 PT == XFASTINT (w->last_point)
11195 /* Make sure the cursor was last displayed
11196 in this window. Otherwise we have to reposition it. */
11197 && 0 <= w->cursor.vpos
11198 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11200 if (!must_finish)
11202 do_pending_window_change (1);
11204 /* We used to always goto end_of_redisplay here, but this
11205 isn't enough if we have a blinking cursor. */
11206 if (w->cursor_off_p == w->last_cursor_off_p)
11207 goto end_of_redisplay;
11209 goto update;
11211 /* If highlighting the region, or if the cursor is in the echo area,
11212 then we can't just move the cursor. */
11213 else if (! (!NILP (Vtransient_mark_mode)
11214 && !NILP (current_buffer->mark_active))
11215 && (EQ (selected_window, current_buffer->last_selected_window)
11216 || highlight_nonselected_windows)
11217 && NILP (w->region_showing)
11218 && NILP (Vshow_trailing_whitespace)
11219 && !cursor_in_echo_area)
11221 struct it it;
11222 struct glyph_row *row;
11224 /* Skip from tlbufpos to PT and see where it is. Note that
11225 PT may be in invisible text. If so, we will end at the
11226 next visible position. */
11227 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11228 NULL, DEFAULT_FACE_ID);
11229 it.current_x = this_line_start_x;
11230 it.current_y = this_line_y;
11231 it.vpos = this_line_vpos;
11233 /* The call to move_it_to stops in front of PT, but
11234 moves over before-strings. */
11235 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11237 if (it.vpos == this_line_vpos
11238 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11239 row->enabled_p))
11241 xassert (this_line_vpos == it.vpos);
11242 xassert (this_line_y == it.current_y);
11243 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11244 #if GLYPH_DEBUG
11245 *w->desired_matrix->method = 0;
11246 debug_method_add (w, "optimization 3");
11247 #endif
11248 goto update;
11250 else
11251 goto cancel;
11254 cancel:
11255 /* Text changed drastically or point moved off of line. */
11256 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11259 CHARPOS (this_line_start_pos) = 0;
11260 consider_all_windows_p |= buffer_shared > 1;
11261 ++clear_face_cache_count;
11262 #ifdef HAVE_WINDOW_SYSTEM
11263 ++clear_image_cache_count;
11264 #endif
11266 /* Build desired matrices, and update the display. If
11267 consider_all_windows_p is non-zero, do it for all windows on all
11268 frames. Otherwise do it for selected_window, only. */
11270 if (consider_all_windows_p)
11272 Lisp_Object tail, frame;
11274 FOR_EACH_FRAME (tail, frame)
11275 XFRAME (frame)->updated_p = 0;
11277 /* Recompute # windows showing selected buffer. This will be
11278 incremented each time such a window is displayed. */
11279 buffer_shared = 0;
11281 FOR_EACH_FRAME (tail, frame)
11283 struct frame *f = XFRAME (frame);
11285 if (FRAME_WINDOW_P (f) || f == sf)
11287 if (! EQ (frame, selected_frame))
11288 /* Select the frame, for the sake of frame-local
11289 variables. */
11290 select_frame_for_redisplay (frame);
11292 /* Mark all the scroll bars to be removed; we'll redeem
11293 the ones we want when we redisplay their windows. */
11294 if (condemn_scroll_bars_hook)
11295 condemn_scroll_bars_hook (f);
11297 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11298 redisplay_windows (FRAME_ROOT_WINDOW (f));
11300 /* Any scroll bars which redisplay_windows should have
11301 nuked should now go away. */
11302 if (judge_scroll_bars_hook)
11303 judge_scroll_bars_hook (f);
11305 /* If fonts changed, display again. */
11306 /* ??? rms: I suspect it is a mistake to jump all the way
11307 back to retry here. It should just retry this frame. */
11308 if (fonts_changed_p)
11309 goto retry;
11311 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11313 /* See if we have to hscroll. */
11314 if (!f->already_hscrolled_p)
11316 f->already_hscrolled_p = 1;
11317 if (hscroll_windows (f->root_window))
11318 goto retry;
11321 /* Prevent various kinds of signals during display
11322 update. stdio is not robust about handling
11323 signals, which can cause an apparent I/O
11324 error. */
11325 if (interrupt_input)
11326 unrequest_sigio ();
11327 STOP_POLLING;
11329 /* Update the display. */
11330 set_window_update_flags (XWINDOW (f->root_window), 1);
11331 pause |= update_frame (f, 0, 0);
11332 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11333 if (pause)
11334 break;
11335 #endif
11337 f->updated_p = 1;
11342 if (!pause)
11344 /* Do the mark_window_display_accurate after all windows have
11345 been redisplayed because this call resets flags in buffers
11346 which are needed for proper redisplay. */
11347 FOR_EACH_FRAME (tail, frame)
11349 struct frame *f = XFRAME (frame);
11350 if (f->updated_p)
11352 mark_window_display_accurate (f->root_window, 1);
11353 if (frame_up_to_date_hook)
11354 frame_up_to_date_hook (f);
11359 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11361 Lisp_Object mini_window;
11362 struct frame *mini_frame;
11364 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11365 /* Use list_of_error, not Qerror, so that
11366 we catch only errors and don't run the debugger. */
11367 internal_condition_case_1 (redisplay_window_1, selected_window,
11368 list_of_error,
11369 redisplay_window_error);
11371 /* Compare desired and current matrices, perform output. */
11373 update:
11374 /* If fonts changed, display again. */
11375 if (fonts_changed_p)
11376 goto retry;
11378 /* Prevent various kinds of signals during display update.
11379 stdio is not robust about handling signals,
11380 which can cause an apparent I/O error. */
11381 if (interrupt_input)
11382 unrequest_sigio ();
11383 STOP_POLLING;
11385 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11387 if (hscroll_windows (selected_window))
11388 goto retry;
11390 XWINDOW (selected_window)->must_be_updated_p = 1;
11391 pause = update_frame (sf, 0, 0);
11394 /* We may have called echo_area_display at the top of this
11395 function. If the echo area is on another frame, that may
11396 have put text on a frame other than the selected one, so the
11397 above call to update_frame would not have caught it. Catch
11398 it here. */
11399 mini_window = FRAME_MINIBUF_WINDOW (sf);
11400 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11402 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11404 XWINDOW (mini_window)->must_be_updated_p = 1;
11405 pause |= update_frame (mini_frame, 0, 0);
11406 if (!pause && hscroll_windows (mini_window))
11407 goto retry;
11411 /* If display was paused because of pending input, make sure we do a
11412 thorough update the next time. */
11413 if (pause)
11415 /* Prevent the optimization at the beginning of
11416 redisplay_internal that tries a single-line update of the
11417 line containing the cursor in the selected window. */
11418 CHARPOS (this_line_start_pos) = 0;
11420 /* Let the overlay arrow be updated the next time. */
11421 update_overlay_arrows (0);
11423 /* If we pause after scrolling, some rows in the current
11424 matrices of some windows are not valid. */
11425 if (!WINDOW_FULL_WIDTH_P (w)
11426 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11427 update_mode_lines = 1;
11429 else
11431 if (!consider_all_windows_p)
11433 /* This has already been done above if
11434 consider_all_windows_p is set. */
11435 mark_window_display_accurate_1 (w, 1);
11437 /* Say overlay arrows are up to date. */
11438 update_overlay_arrows (1);
11440 if (frame_up_to_date_hook != 0)
11441 frame_up_to_date_hook (sf);
11444 update_mode_lines = 0;
11445 windows_or_buffers_changed = 0;
11446 cursor_type_changed = 0;
11449 /* Start SIGIO interrupts coming again. Having them off during the
11450 code above makes it less likely one will discard output, but not
11451 impossible, since there might be stuff in the system buffer here.
11452 But it is much hairier to try to do anything about that. */
11453 if (interrupt_input)
11454 request_sigio ();
11455 RESUME_POLLING;
11457 /* If a frame has become visible which was not before, redisplay
11458 again, so that we display it. Expose events for such a frame
11459 (which it gets when becoming visible) don't call the parts of
11460 redisplay constructing glyphs, so simply exposing a frame won't
11461 display anything in this case. So, we have to display these
11462 frames here explicitly. */
11463 if (!pause)
11465 Lisp_Object tail, frame;
11466 int new_count = 0;
11468 FOR_EACH_FRAME (tail, frame)
11470 int this_is_visible = 0;
11472 if (XFRAME (frame)->visible)
11473 this_is_visible = 1;
11474 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11475 if (XFRAME (frame)->visible)
11476 this_is_visible = 1;
11478 if (this_is_visible)
11479 new_count++;
11482 if (new_count != number_of_visible_frames)
11483 windows_or_buffers_changed++;
11486 /* Change frame size now if a change is pending. */
11487 do_pending_window_change (1);
11489 /* If we just did a pending size change, or have additional
11490 visible frames, redisplay again. */
11491 if (windows_or_buffers_changed && !pause)
11492 goto retry;
11494 /* Clear the face cache eventually. */
11495 if (consider_all_windows_p)
11497 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11499 clear_face_cache (0);
11500 clear_face_cache_count = 0;
11502 #ifdef HAVE_WINDOW_SYSTEM
11503 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11505 Lisp_Object tail, frame;
11506 FOR_EACH_FRAME (tail, frame)
11508 struct frame *f = XFRAME (frame);
11509 if (FRAME_WINDOW_P (f))
11510 clear_image_cache (f, 0);
11512 clear_image_cache_count = 0;
11514 #endif /* HAVE_WINDOW_SYSTEM */
11517 end_of_redisplay:
11518 unbind_to (count, Qnil);
11519 RESUME_POLLING;
11523 /* Redisplay, but leave alone any recent echo area message unless
11524 another message has been requested in its place.
11526 This is useful in situations where you need to redisplay but no
11527 user action has occurred, making it inappropriate for the message
11528 area to be cleared. See tracking_off and
11529 wait_reading_process_output for examples of these situations.
11531 FROM_WHERE is an integer saying from where this function was
11532 called. This is useful for debugging. */
11534 void
11535 redisplay_preserve_echo_area (from_where)
11536 int from_where;
11538 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11540 if (!NILP (echo_area_buffer[1]))
11542 /* We have a previously displayed message, but no current
11543 message. Redisplay the previous message. */
11544 display_last_displayed_message_p = 1;
11545 redisplay_internal (1);
11546 display_last_displayed_message_p = 0;
11548 else
11549 redisplay_internal (1);
11551 if (rif != NULL && rif->flush_display_optional)
11552 rif->flush_display_optional (NULL);
11556 /* Function registered with record_unwind_protect in
11557 redisplay_internal. Reset redisplaying_p to the value it had
11558 before redisplay_internal was called, and clear
11559 prevent_freeing_realized_faces_p. It also selects the previously
11560 selected frame. */
11562 static Lisp_Object
11563 unwind_redisplay (val)
11564 Lisp_Object val;
11566 Lisp_Object old_redisplaying_p, old_frame;
11568 old_redisplaying_p = XCAR (val);
11569 redisplaying_p = XFASTINT (old_redisplaying_p);
11570 old_frame = XCDR (val);
11571 if (! EQ (old_frame, selected_frame))
11572 select_frame_for_redisplay (old_frame);
11573 return Qnil;
11577 /* Mark the display of window W as accurate or inaccurate. If
11578 ACCURATE_P is non-zero mark display of W as accurate. If
11579 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11580 redisplay_internal is called. */
11582 static void
11583 mark_window_display_accurate_1 (w, accurate_p)
11584 struct window *w;
11585 int accurate_p;
11587 if (BUFFERP (w->buffer))
11589 struct buffer *b = XBUFFER (w->buffer);
11591 w->last_modified
11592 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11593 w->last_overlay_modified
11594 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11595 w->last_had_star
11596 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11598 if (accurate_p)
11600 b->clip_changed = 0;
11601 b->prevent_redisplay_optimizations_p = 0;
11603 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11604 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11605 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11606 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11608 w->current_matrix->buffer = b;
11609 w->current_matrix->begv = BUF_BEGV (b);
11610 w->current_matrix->zv = BUF_ZV (b);
11612 w->last_cursor = w->cursor;
11613 w->last_cursor_off_p = w->cursor_off_p;
11615 if (w == XWINDOW (selected_window))
11616 w->last_point = make_number (BUF_PT (b));
11617 else
11618 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11622 if (accurate_p)
11624 w->window_end_valid = w->buffer;
11625 #if 0 /* This is incorrect with variable-height lines. */
11626 xassert (XINT (w->window_end_vpos)
11627 < (WINDOW_TOTAL_LINES (w)
11628 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11629 #endif
11630 w->update_mode_line = Qnil;
11635 /* Mark the display of windows in the window tree rooted at WINDOW as
11636 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11637 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11638 be redisplayed the next time redisplay_internal is called. */
11640 void
11641 mark_window_display_accurate (window, accurate_p)
11642 Lisp_Object window;
11643 int accurate_p;
11645 struct window *w;
11647 for (; !NILP (window); window = w->next)
11649 w = XWINDOW (window);
11650 mark_window_display_accurate_1 (w, accurate_p);
11652 if (!NILP (w->vchild))
11653 mark_window_display_accurate (w->vchild, accurate_p);
11654 if (!NILP (w->hchild))
11655 mark_window_display_accurate (w->hchild, accurate_p);
11658 if (accurate_p)
11660 update_overlay_arrows (1);
11662 else
11664 /* Force a thorough redisplay the next time by setting
11665 last_arrow_position and last_arrow_string to t, which is
11666 unequal to any useful value of Voverlay_arrow_... */
11667 update_overlay_arrows (-1);
11672 /* Return value in display table DP (Lisp_Char_Table *) for character
11673 C. Since a display table doesn't have any parent, we don't have to
11674 follow parent. Do not call this function directly but use the
11675 macro DISP_CHAR_VECTOR. */
11677 Lisp_Object
11678 disp_char_vector (dp, c)
11679 struct Lisp_Char_Table *dp;
11680 int c;
11682 int code[4], i;
11683 Lisp_Object val;
11685 if (SINGLE_BYTE_CHAR_P (c))
11686 return (dp->contents[c]);
11688 SPLIT_CHAR (c, code[0], code[1], code[2]);
11689 if (code[1] < 32)
11690 code[1] = -1;
11691 else if (code[2] < 32)
11692 code[2] = -1;
11694 /* Here, the possible range of code[0] (== charset ID) is
11695 128..max_charset. Since the top level char table contains data
11696 for multibyte characters after 256th element, we must increment
11697 code[0] by 128 to get a correct index. */
11698 code[0] += 128;
11699 code[3] = -1; /* anchor */
11701 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11703 val = dp->contents[code[i]];
11704 if (!SUB_CHAR_TABLE_P (val))
11705 return (NILP (val) ? dp->defalt : val);
11708 /* Here, val is a sub char table. We return the default value of
11709 it. */
11710 return (dp->defalt);
11715 /***********************************************************************
11716 Window Redisplay
11717 ***********************************************************************/
11719 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11721 static void
11722 redisplay_windows (window)
11723 Lisp_Object window;
11725 while (!NILP (window))
11727 struct window *w = XWINDOW (window);
11729 if (!NILP (w->hchild))
11730 redisplay_windows (w->hchild);
11731 else if (!NILP (w->vchild))
11732 redisplay_windows (w->vchild);
11733 else
11735 displayed_buffer = XBUFFER (w->buffer);
11736 /* Use list_of_error, not Qerror, so that
11737 we catch only errors and don't run the debugger. */
11738 internal_condition_case_1 (redisplay_window_0, window,
11739 list_of_error,
11740 redisplay_window_error);
11743 window = w->next;
11747 static Lisp_Object
11748 redisplay_window_error ()
11750 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11751 return Qnil;
11754 static Lisp_Object
11755 redisplay_window_0 (window)
11756 Lisp_Object window;
11758 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11759 redisplay_window (window, 0);
11760 return Qnil;
11763 static Lisp_Object
11764 redisplay_window_1 (window)
11765 Lisp_Object window;
11767 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11768 redisplay_window (window, 1);
11769 return Qnil;
11773 /* Increment GLYPH until it reaches END or CONDITION fails while
11774 adding (GLYPH)->pixel_width to X. */
11776 #define SKIP_GLYPHS(glyph, end, x, condition) \
11777 do \
11779 (x) += (glyph)->pixel_width; \
11780 ++(glyph); \
11782 while ((glyph) < (end) && (condition))
11785 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11786 DELTA is the number of bytes by which positions recorded in ROW
11787 differ from current buffer positions.
11789 Return 0 if cursor is not on this row. 1 otherwise. */
11792 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11793 struct window *w;
11794 struct glyph_row *row;
11795 struct glyph_matrix *matrix;
11796 int delta, delta_bytes, dy, dvpos;
11798 struct glyph *glyph = row->glyphs[TEXT_AREA];
11799 struct glyph *end = glyph + row->used[TEXT_AREA];
11800 struct glyph *cursor = NULL;
11801 /* The first glyph that starts a sequence of glyphs from string. */
11802 struct glyph *string_start;
11803 /* The X coordinate of string_start. */
11804 int string_start_x;
11805 /* The last known character position. */
11806 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11807 /* The last known character position before string_start. */
11808 int string_before_pos;
11809 int x = row->x;
11810 int cursor_x = x;
11811 int cursor_from_overlay_pos = 0;
11812 int pt_old = PT - delta;
11814 /* Skip over glyphs not having an object at the start of the row.
11815 These are special glyphs like truncation marks on terminal
11816 frames. */
11817 if (row->displays_text_p)
11818 while (glyph < end
11819 && INTEGERP (glyph->object)
11820 && glyph->charpos < 0)
11822 x += glyph->pixel_width;
11823 ++glyph;
11826 string_start = NULL;
11827 while (glyph < end
11828 && !INTEGERP (glyph->object)
11829 && (!BUFFERP (glyph->object)
11830 || (last_pos = glyph->charpos) < pt_old))
11832 if (! STRINGP (glyph->object))
11834 string_start = NULL;
11835 x += glyph->pixel_width;
11836 ++glyph;
11837 if (cursor_from_overlay_pos
11838 && last_pos >= cursor_from_overlay_pos)
11840 cursor_from_overlay_pos = 0;
11841 cursor = 0;
11844 else
11846 if (string_start == NULL)
11848 string_before_pos = last_pos;
11849 string_start = glyph;
11850 string_start_x = x;
11852 /* Skip all glyphs from string. */
11855 Lisp_Object cprop;
11856 int pos;
11857 if ((cursor == NULL || glyph > cursor)
11858 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11859 Qcursor, (glyph)->object),
11860 !NILP (cprop))
11861 && (pos = string_buffer_position (w, glyph->object,
11862 string_before_pos),
11863 (pos == 0 /* From overlay */
11864 || pos == pt_old)))
11866 /* Estimate overlay buffer position from the buffer
11867 positions of the glyphs before and after the overlay.
11868 Add 1 to last_pos so that if point corresponds to the
11869 glyph right after the overlay, we still use a 'cursor'
11870 property found in that overlay. */
11871 cursor_from_overlay_pos = (pos ? 0 : last_pos
11872 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11873 cursor = glyph;
11874 cursor_x = x;
11876 x += glyph->pixel_width;
11877 ++glyph;
11879 while (glyph < end && EQ (glyph->object, string_start->object));
11883 if (cursor != NULL)
11885 glyph = cursor;
11886 x = cursor_x;
11888 else if (row->ends_in_ellipsis_p && glyph == end)
11890 /* Scan back over the ellipsis glyphs, decrementing positions. */
11891 while (glyph > row->glyphs[TEXT_AREA]
11892 && (glyph - 1)->charpos == last_pos)
11893 glyph--, x -= glyph->pixel_width;
11894 /* That loop always goes one position too far,
11895 including the glyph before the ellipsis.
11896 So scan forward over that one. */
11897 x += glyph->pixel_width;
11898 glyph++;
11900 else if (string_start
11901 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11903 /* We may have skipped over point because the previous glyphs
11904 are from string. As there's no easy way to know the
11905 character position of the current glyph, find the correct
11906 glyph on point by scanning from string_start again. */
11907 Lisp_Object limit;
11908 Lisp_Object string;
11909 struct glyph *stop = glyph;
11910 int pos;
11912 limit = make_number (pt_old + 1);
11913 glyph = string_start;
11914 x = string_start_x;
11915 string = glyph->object;
11916 pos = string_buffer_position (w, string, string_before_pos);
11917 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11918 because we always put cursor after overlay strings. */
11919 while (pos == 0 && glyph < stop)
11921 string = glyph->object;
11922 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11923 if (glyph < stop)
11924 pos = string_buffer_position (w, glyph->object, string_before_pos);
11927 while (glyph < stop)
11929 pos = XINT (Fnext_single_char_property_change
11930 (make_number (pos), Qdisplay, Qnil, limit));
11931 if (pos > pt_old)
11932 break;
11933 /* Skip glyphs from the same string. */
11934 string = glyph->object;
11935 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11936 /* Skip glyphs from an overlay. */
11937 while (glyph < stop
11938 && ! string_buffer_position (w, glyph->object, pos))
11940 string = glyph->object;
11941 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11945 /* If we reached the end of the line, and end was from a string,
11946 cursor is not on this line. */
11947 if (glyph == end && row->continued_p)
11948 return 0;
11951 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11952 w->cursor.x = x;
11953 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11954 w->cursor.y = row->y + dy;
11956 if (w == XWINDOW (selected_window))
11958 if (!row->continued_p
11959 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11960 && row->x == 0)
11962 this_line_buffer = XBUFFER (w->buffer);
11964 CHARPOS (this_line_start_pos)
11965 = MATRIX_ROW_START_CHARPOS (row) + delta;
11966 BYTEPOS (this_line_start_pos)
11967 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11969 CHARPOS (this_line_end_pos)
11970 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11971 BYTEPOS (this_line_end_pos)
11972 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11974 this_line_y = w->cursor.y;
11975 this_line_pixel_height = row->height;
11976 this_line_vpos = w->cursor.vpos;
11977 this_line_start_x = row->x;
11979 else
11980 CHARPOS (this_line_start_pos) = 0;
11983 return 1;
11987 /* Run window scroll functions, if any, for WINDOW with new window
11988 start STARTP. Sets the window start of WINDOW to that position.
11990 We assume that the window's buffer is really current. */
11992 static INLINE struct text_pos
11993 run_window_scroll_functions (window, startp)
11994 Lisp_Object window;
11995 struct text_pos startp;
11997 struct window *w = XWINDOW (window);
11998 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12000 if (current_buffer != XBUFFER (w->buffer))
12001 abort ();
12003 if (!NILP (Vwindow_scroll_functions))
12005 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12006 make_number (CHARPOS (startp)));
12007 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12008 /* In case the hook functions switch buffers. */
12009 if (current_buffer != XBUFFER (w->buffer))
12010 set_buffer_internal_1 (XBUFFER (w->buffer));
12013 return startp;
12017 /* Make sure the line containing the cursor is fully visible.
12018 A value of 1 means there is nothing to be done.
12019 (Either the line is fully visible, or it cannot be made so,
12020 or we cannot tell.)
12022 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12023 is higher than window.
12025 A value of 0 means the caller should do scrolling
12026 as if point had gone off the screen. */
12028 static int
12029 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12030 struct window *w;
12031 int force_p;
12032 int current_matrix_p;
12034 struct glyph_matrix *matrix;
12035 struct glyph_row *row;
12036 int window_height;
12038 if (!make_cursor_line_fully_visible_p)
12039 return 1;
12041 /* It's not always possible to find the cursor, e.g, when a window
12042 is full of overlay strings. Don't do anything in that case. */
12043 if (w->cursor.vpos < 0)
12044 return 1;
12046 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12047 row = MATRIX_ROW (matrix, w->cursor.vpos);
12049 /* If the cursor row is not partially visible, there's nothing to do. */
12050 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12051 return 1;
12053 /* If the row the cursor is in is taller than the window's height,
12054 it's not clear what to do, so do nothing. */
12055 window_height = window_box_height (w);
12056 if (row->height >= window_height)
12058 if (!force_p || MINI_WINDOW_P (w)
12059 || w->vscroll || w->cursor.vpos == 0)
12060 return 1;
12062 return 0;
12064 #if 0
12065 /* This code used to try to scroll the window just enough to make
12066 the line visible. It returned 0 to say that the caller should
12067 allocate larger glyph matrices. */
12069 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12071 int dy = row->height - row->visible_height;
12072 w->vscroll = 0;
12073 w->cursor.y += dy;
12074 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12076 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12078 int dy = - (row->height - row->visible_height);
12079 w->vscroll = dy;
12080 w->cursor.y += dy;
12081 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12084 /* When we change the cursor y-position of the selected window,
12085 change this_line_y as well so that the display optimization for
12086 the cursor line of the selected window in redisplay_internal uses
12087 the correct y-position. */
12088 if (w == XWINDOW (selected_window))
12089 this_line_y = w->cursor.y;
12091 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12092 redisplay with larger matrices. */
12093 if (matrix->nrows < required_matrix_height (w))
12095 fonts_changed_p = 1;
12096 return 0;
12099 return 1;
12100 #endif /* 0 */
12104 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12105 non-zero means only WINDOW is redisplayed in redisplay_internal.
12106 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12107 in redisplay_window to bring a partially visible line into view in
12108 the case that only the cursor has moved.
12110 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12111 last screen line's vertical height extends past the end of the screen.
12113 Value is
12115 1 if scrolling succeeded
12117 0 if scrolling didn't find point.
12119 -1 if new fonts have been loaded so that we must interrupt
12120 redisplay, adjust glyph matrices, and try again. */
12122 enum
12124 SCROLLING_SUCCESS,
12125 SCROLLING_FAILED,
12126 SCROLLING_NEED_LARGER_MATRICES
12129 static int
12130 try_scrolling (window, just_this_one_p, scroll_conservatively,
12131 scroll_step, temp_scroll_step, last_line_misfit)
12132 Lisp_Object window;
12133 int just_this_one_p;
12134 EMACS_INT scroll_conservatively, scroll_step;
12135 int temp_scroll_step;
12136 int last_line_misfit;
12138 struct window *w = XWINDOW (window);
12139 struct frame *f = XFRAME (w->frame);
12140 struct text_pos scroll_margin_pos;
12141 struct text_pos pos;
12142 struct text_pos startp;
12143 struct it it;
12144 Lisp_Object window_end;
12145 int this_scroll_margin;
12146 int dy = 0;
12147 int scroll_max;
12148 int rc;
12149 int amount_to_scroll = 0;
12150 Lisp_Object aggressive;
12151 int height;
12152 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12154 #if GLYPH_DEBUG
12155 debug_method_add (w, "try_scrolling");
12156 #endif
12158 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12160 /* Compute scroll margin height in pixels. We scroll when point is
12161 within this distance from the top or bottom of the window. */
12162 if (scroll_margin > 0)
12164 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12165 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12167 else
12168 this_scroll_margin = 0;
12170 /* Force scroll_conservatively to have a reasonable value so it doesn't
12171 cause an overflow while computing how much to scroll. */
12172 if (scroll_conservatively)
12173 scroll_conservatively = min (scroll_conservatively,
12174 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12176 /* Compute how much we should try to scroll maximally to bring point
12177 into view. */
12178 if (scroll_step || scroll_conservatively || temp_scroll_step)
12179 scroll_max = max (scroll_step,
12180 max (scroll_conservatively, temp_scroll_step));
12181 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12182 || NUMBERP (current_buffer->scroll_up_aggressively))
12183 /* We're trying to scroll because of aggressive scrolling
12184 but no scroll_step is set. Choose an arbitrary one. Maybe
12185 there should be a variable for this. */
12186 scroll_max = 10;
12187 else
12188 scroll_max = 0;
12189 scroll_max *= FRAME_LINE_HEIGHT (f);
12191 /* Decide whether we have to scroll down. Start at the window end
12192 and move this_scroll_margin up to find the position of the scroll
12193 margin. */
12194 window_end = Fwindow_end (window, Qt);
12196 too_near_end:
12198 CHARPOS (scroll_margin_pos) = XINT (window_end);
12199 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12201 if (this_scroll_margin || extra_scroll_margin_lines)
12203 start_display (&it, w, scroll_margin_pos);
12204 if (this_scroll_margin)
12205 move_it_vertically_backward (&it, this_scroll_margin);
12206 if (extra_scroll_margin_lines)
12207 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12208 scroll_margin_pos = it.current.pos;
12211 if (PT >= CHARPOS (scroll_margin_pos))
12213 int y0;
12215 /* Point is in the scroll margin at the bottom of the window, or
12216 below. Compute a new window start that makes point visible. */
12218 /* Compute the distance from the scroll margin to PT.
12219 Give up if the distance is greater than scroll_max. */
12220 start_display (&it, w, scroll_margin_pos);
12221 y0 = it.current_y;
12222 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12223 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12225 /* To make point visible, we have to move the window start
12226 down so that the line the cursor is in is visible, which
12227 means we have to add in the height of the cursor line. */
12228 dy = line_bottom_y (&it) - y0;
12230 if (dy > scroll_max)
12231 return SCROLLING_FAILED;
12233 /* Move the window start down. If scrolling conservatively,
12234 move it just enough down to make point visible. If
12235 scroll_step is set, move it down by scroll_step. */
12236 start_display (&it, w, startp);
12238 if (scroll_conservatively)
12239 /* Set AMOUNT_TO_SCROLL to at least one line,
12240 and at most scroll_conservatively lines. */
12241 amount_to_scroll
12242 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12243 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12244 else if (scroll_step || temp_scroll_step)
12245 amount_to_scroll = scroll_max;
12246 else
12248 aggressive = current_buffer->scroll_up_aggressively;
12249 height = WINDOW_BOX_TEXT_HEIGHT (w);
12250 if (NUMBERP (aggressive))
12252 double float_amount = XFLOATINT (aggressive) * height;
12253 amount_to_scroll = float_amount;
12254 if (amount_to_scroll == 0 && float_amount > 0)
12255 amount_to_scroll = 1;
12259 if (amount_to_scroll <= 0)
12260 return SCROLLING_FAILED;
12262 /* If moving by amount_to_scroll leaves STARTP unchanged,
12263 move it down one screen line. */
12265 move_it_vertically (&it, amount_to_scroll);
12266 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12267 move_it_by_lines (&it, 1, 1);
12268 startp = it.current.pos;
12270 else
12272 /* See if point is inside the scroll margin at the top of the
12273 window. */
12274 scroll_margin_pos = startp;
12275 if (this_scroll_margin)
12277 start_display (&it, w, startp);
12278 move_it_vertically (&it, this_scroll_margin);
12279 scroll_margin_pos = it.current.pos;
12282 if (PT < CHARPOS (scroll_margin_pos))
12284 /* Point is in the scroll margin at the top of the window or
12285 above what is displayed in the window. */
12286 int y0;
12288 /* Compute the vertical distance from PT to the scroll
12289 margin position. Give up if distance is greater than
12290 scroll_max. */
12291 SET_TEXT_POS (pos, PT, PT_BYTE);
12292 start_display (&it, w, pos);
12293 y0 = it.current_y;
12294 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12295 it.last_visible_y, -1,
12296 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12297 dy = it.current_y - y0;
12298 if (dy > scroll_max)
12299 return SCROLLING_FAILED;
12301 /* Compute new window start. */
12302 start_display (&it, w, startp);
12304 if (scroll_conservatively)
12305 amount_to_scroll
12306 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12307 else if (scroll_step || temp_scroll_step)
12308 amount_to_scroll = scroll_max;
12309 else
12311 aggressive = current_buffer->scroll_down_aggressively;
12312 height = WINDOW_BOX_TEXT_HEIGHT (w);
12313 if (NUMBERP (aggressive))
12315 double float_amount = XFLOATINT (aggressive) * height;
12316 amount_to_scroll = float_amount;
12317 if (amount_to_scroll == 0 && float_amount > 0)
12318 amount_to_scroll = 1;
12322 if (amount_to_scroll <= 0)
12323 return SCROLLING_FAILED;
12325 move_it_vertically_backward (&it, amount_to_scroll);
12326 startp = it.current.pos;
12330 /* Run window scroll functions. */
12331 startp = run_window_scroll_functions (window, startp);
12333 /* Display the window. Give up if new fonts are loaded, or if point
12334 doesn't appear. */
12335 if (!try_window (window, startp, 0))
12336 rc = SCROLLING_NEED_LARGER_MATRICES;
12337 else if (w->cursor.vpos < 0)
12339 clear_glyph_matrix (w->desired_matrix);
12340 rc = SCROLLING_FAILED;
12342 else
12344 /* Maybe forget recorded base line for line number display. */
12345 if (!just_this_one_p
12346 || current_buffer->clip_changed
12347 || BEG_UNCHANGED < CHARPOS (startp))
12348 w->base_line_number = Qnil;
12350 /* If cursor ends up on a partially visible line,
12351 treat that as being off the bottom of the screen. */
12352 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12354 clear_glyph_matrix (w->desired_matrix);
12355 ++extra_scroll_margin_lines;
12356 goto too_near_end;
12358 rc = SCROLLING_SUCCESS;
12361 return rc;
12365 /* Compute a suitable window start for window W if display of W starts
12366 on a continuation line. Value is non-zero if a new window start
12367 was computed.
12369 The new window start will be computed, based on W's width, starting
12370 from the start of the continued line. It is the start of the
12371 screen line with the minimum distance from the old start W->start. */
12373 static int
12374 compute_window_start_on_continuation_line (w)
12375 struct window *w;
12377 struct text_pos pos, start_pos;
12378 int window_start_changed_p = 0;
12380 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12382 /* If window start is on a continuation line... Window start may be
12383 < BEGV in case there's invisible text at the start of the
12384 buffer (M-x rmail, for example). */
12385 if (CHARPOS (start_pos) > BEGV
12386 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12388 struct it it;
12389 struct glyph_row *row;
12391 /* Handle the case that the window start is out of range. */
12392 if (CHARPOS (start_pos) < BEGV)
12393 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12394 else if (CHARPOS (start_pos) > ZV)
12395 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12397 /* Find the start of the continued line. This should be fast
12398 because scan_buffer is fast (newline cache). */
12399 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12400 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12401 row, DEFAULT_FACE_ID);
12402 reseat_at_previous_visible_line_start (&it);
12404 /* If the line start is "too far" away from the window start,
12405 say it takes too much time to compute a new window start. */
12406 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12407 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12409 int min_distance, distance;
12411 /* Move forward by display lines to find the new window
12412 start. If window width was enlarged, the new start can
12413 be expected to be > the old start. If window width was
12414 decreased, the new window start will be < the old start.
12415 So, we're looking for the display line start with the
12416 minimum distance from the old window start. */
12417 pos = it.current.pos;
12418 min_distance = INFINITY;
12419 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12420 distance < min_distance)
12422 min_distance = distance;
12423 pos = it.current.pos;
12424 move_it_by_lines (&it, 1, 0);
12427 /* Set the window start there. */
12428 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12429 window_start_changed_p = 1;
12433 return window_start_changed_p;
12437 /* Try cursor movement in case text has not changed in window WINDOW,
12438 with window start STARTP. Value is
12440 CURSOR_MOVEMENT_SUCCESS if successful
12442 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12444 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12445 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12446 we want to scroll as if scroll-step were set to 1. See the code.
12448 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12449 which case we have to abort this redisplay, and adjust matrices
12450 first. */
12452 enum
12454 CURSOR_MOVEMENT_SUCCESS,
12455 CURSOR_MOVEMENT_CANNOT_BE_USED,
12456 CURSOR_MOVEMENT_MUST_SCROLL,
12457 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12460 static int
12461 try_cursor_movement (window, startp, scroll_step)
12462 Lisp_Object window;
12463 struct text_pos startp;
12464 int *scroll_step;
12466 struct window *w = XWINDOW (window);
12467 struct frame *f = XFRAME (w->frame);
12468 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12470 #if GLYPH_DEBUG
12471 if (inhibit_try_cursor_movement)
12472 return rc;
12473 #endif
12475 /* Handle case where text has not changed, only point, and it has
12476 not moved off the frame. */
12477 if (/* Point may be in this window. */
12478 PT >= CHARPOS (startp)
12479 /* Selective display hasn't changed. */
12480 && !current_buffer->clip_changed
12481 /* Function force-mode-line-update is used to force a thorough
12482 redisplay. It sets either windows_or_buffers_changed or
12483 update_mode_lines. So don't take a shortcut here for these
12484 cases. */
12485 && !update_mode_lines
12486 && !windows_or_buffers_changed
12487 && !cursor_type_changed
12488 /* Can't use this case if highlighting a region. When a
12489 region exists, cursor movement has to do more than just
12490 set the cursor. */
12491 && !(!NILP (Vtransient_mark_mode)
12492 && !NILP (current_buffer->mark_active))
12493 && NILP (w->region_showing)
12494 && NILP (Vshow_trailing_whitespace)
12495 /* Right after splitting windows, last_point may be nil. */
12496 && INTEGERP (w->last_point)
12497 /* This code is not used for mini-buffer for the sake of the case
12498 of redisplaying to replace an echo area message; since in
12499 that case the mini-buffer contents per se are usually
12500 unchanged. This code is of no real use in the mini-buffer
12501 since the handling of this_line_start_pos, etc., in redisplay
12502 handles the same cases. */
12503 && !EQ (window, minibuf_window)
12504 /* When splitting windows or for new windows, it happens that
12505 redisplay is called with a nil window_end_vpos or one being
12506 larger than the window. This should really be fixed in
12507 window.c. I don't have this on my list, now, so we do
12508 approximately the same as the old redisplay code. --gerd. */
12509 && INTEGERP (w->window_end_vpos)
12510 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12511 && (FRAME_WINDOW_P (f)
12512 || !overlay_arrow_in_current_buffer_p ()))
12514 int this_scroll_margin, top_scroll_margin;
12515 struct glyph_row *row = NULL;
12517 #if GLYPH_DEBUG
12518 debug_method_add (w, "cursor movement");
12519 #endif
12521 /* Scroll if point within this distance from the top or bottom
12522 of the window. This is a pixel value. */
12523 this_scroll_margin = max (0, scroll_margin);
12524 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12525 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12527 top_scroll_margin = this_scroll_margin;
12528 if (WINDOW_WANTS_HEADER_LINE_P (w))
12529 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12531 /* Start with the row the cursor was displayed during the last
12532 not paused redisplay. Give up if that row is not valid. */
12533 if (w->last_cursor.vpos < 0
12534 || w->last_cursor.vpos >= w->current_matrix->nrows)
12535 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12536 else
12538 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12539 if (row->mode_line_p)
12540 ++row;
12541 if (!row->enabled_p)
12542 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12545 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12547 int scroll_p = 0;
12548 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12550 if (PT > XFASTINT (w->last_point))
12552 /* Point has moved forward. */
12553 while (MATRIX_ROW_END_CHARPOS (row) < PT
12554 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12556 xassert (row->enabled_p);
12557 ++row;
12560 /* The end position of a row equals the start position
12561 of the next row. If PT is there, we would rather
12562 display it in the next line. */
12563 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12564 && MATRIX_ROW_END_CHARPOS (row) == PT
12565 && !cursor_row_p (w, row))
12566 ++row;
12568 /* If within the scroll margin, scroll. Note that
12569 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12570 the next line would be drawn, and that
12571 this_scroll_margin can be zero. */
12572 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12573 || PT > MATRIX_ROW_END_CHARPOS (row)
12574 /* Line is completely visible last line in window
12575 and PT is to be set in the next line. */
12576 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12577 && PT == MATRIX_ROW_END_CHARPOS (row)
12578 && !row->ends_at_zv_p
12579 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12580 scroll_p = 1;
12582 else if (PT < XFASTINT (w->last_point))
12584 /* Cursor has to be moved backward. Note that PT >=
12585 CHARPOS (startp) because of the outer if-statement. */
12586 while (!row->mode_line_p
12587 && (MATRIX_ROW_START_CHARPOS (row) > PT
12588 || (MATRIX_ROW_START_CHARPOS (row) == PT
12589 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12590 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12591 row > w->current_matrix->rows
12592 && (row-1)->ends_in_newline_from_string_p))))
12593 && (row->y > top_scroll_margin
12594 || CHARPOS (startp) == BEGV))
12596 xassert (row->enabled_p);
12597 --row;
12600 /* Consider the following case: Window starts at BEGV,
12601 there is invisible, intangible text at BEGV, so that
12602 display starts at some point START > BEGV. It can
12603 happen that we are called with PT somewhere between
12604 BEGV and START. Try to handle that case. */
12605 if (row < w->current_matrix->rows
12606 || row->mode_line_p)
12608 row = w->current_matrix->rows;
12609 if (row->mode_line_p)
12610 ++row;
12613 /* Due to newlines in overlay strings, we may have to
12614 skip forward over overlay strings. */
12615 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12616 && MATRIX_ROW_END_CHARPOS (row) == PT
12617 && !cursor_row_p (w, row))
12618 ++row;
12620 /* If within the scroll margin, scroll. */
12621 if (row->y < top_scroll_margin
12622 && CHARPOS (startp) != BEGV)
12623 scroll_p = 1;
12625 else
12627 /* Cursor did not move. So don't scroll even if cursor line
12628 is partially visible, as it was so before. */
12629 rc = CURSOR_MOVEMENT_SUCCESS;
12632 if (PT < MATRIX_ROW_START_CHARPOS (row)
12633 || PT > MATRIX_ROW_END_CHARPOS (row))
12635 /* if PT is not in the glyph row, give up. */
12636 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12638 else if (rc != CURSOR_MOVEMENT_SUCCESS
12639 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12640 && make_cursor_line_fully_visible_p)
12642 if (PT == MATRIX_ROW_END_CHARPOS (row)
12643 && !row->ends_at_zv_p
12644 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12645 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12646 else if (row->height > window_box_height (w))
12648 /* If we end up in a partially visible line, let's
12649 make it fully visible, except when it's taller
12650 than the window, in which case we can't do much
12651 about it. */
12652 *scroll_step = 1;
12653 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12655 else
12657 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12658 if (!cursor_row_fully_visible_p (w, 0, 1))
12659 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12660 else
12661 rc = CURSOR_MOVEMENT_SUCCESS;
12664 else if (scroll_p)
12665 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12666 else
12670 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12672 rc = CURSOR_MOVEMENT_SUCCESS;
12673 break;
12675 ++row;
12677 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12678 && MATRIX_ROW_START_CHARPOS (row) == PT
12679 && cursor_row_p (w, row));
12684 return rc;
12687 void
12688 set_vertical_scroll_bar (w)
12689 struct window *w;
12691 int start, end, whole;
12693 /* Calculate the start and end positions for the current window.
12694 At some point, it would be nice to choose between scrollbars
12695 which reflect the whole buffer size, with special markers
12696 indicating narrowing, and scrollbars which reflect only the
12697 visible region.
12699 Note that mini-buffers sometimes aren't displaying any text. */
12700 if (!MINI_WINDOW_P (w)
12701 || (w == XWINDOW (minibuf_window)
12702 && NILP (echo_area_buffer[0])))
12704 struct buffer *buf = XBUFFER (w->buffer);
12705 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12706 start = marker_position (w->start) - BUF_BEGV (buf);
12707 /* I don't think this is guaranteed to be right. For the
12708 moment, we'll pretend it is. */
12709 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12711 if (end < start)
12712 end = start;
12713 if (whole < (end - start))
12714 whole = end - start;
12716 else
12717 start = end = whole = 0;
12719 /* Indicate what this scroll bar ought to be displaying now. */
12720 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12724 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12725 selected_window is redisplayed.
12727 We can return without actually redisplaying the window if
12728 fonts_changed_p is nonzero. In that case, redisplay_internal will
12729 retry. */
12731 static void
12732 redisplay_window (window, just_this_one_p)
12733 Lisp_Object window;
12734 int just_this_one_p;
12736 struct window *w = XWINDOW (window);
12737 struct frame *f = XFRAME (w->frame);
12738 struct buffer *buffer = XBUFFER (w->buffer);
12739 struct buffer *old = current_buffer;
12740 struct text_pos lpoint, opoint, startp;
12741 int update_mode_line;
12742 int tem;
12743 struct it it;
12744 /* Record it now because it's overwritten. */
12745 int current_matrix_up_to_date_p = 0;
12746 int used_current_matrix_p = 0;
12747 /* This is less strict than current_matrix_up_to_date_p.
12748 It indictes that the buffer contents and narrowing are unchanged. */
12749 int buffer_unchanged_p = 0;
12750 int temp_scroll_step = 0;
12751 int count = SPECPDL_INDEX ();
12752 int rc;
12753 int centering_position = -1;
12754 int last_line_misfit = 0;
12756 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12757 opoint = lpoint;
12759 /* W must be a leaf window here. */
12760 xassert (!NILP (w->buffer));
12761 #if GLYPH_DEBUG
12762 *w->desired_matrix->method = 0;
12763 #endif
12765 specbind (Qinhibit_point_motion_hooks, Qt);
12767 reconsider_clip_changes (w, buffer);
12769 /* Has the mode line to be updated? */
12770 update_mode_line = (!NILP (w->update_mode_line)
12771 || update_mode_lines
12772 || buffer->clip_changed
12773 || buffer->prevent_redisplay_optimizations_p);
12775 if (MINI_WINDOW_P (w))
12777 if (w == XWINDOW (echo_area_window)
12778 && !NILP (echo_area_buffer[0]))
12780 if (update_mode_line)
12781 /* We may have to update a tty frame's menu bar or a
12782 tool-bar. Example `M-x C-h C-h C-g'. */
12783 goto finish_menu_bars;
12784 else
12785 /* We've already displayed the echo area glyphs in this window. */
12786 goto finish_scroll_bars;
12788 else if ((w != XWINDOW (minibuf_window)
12789 || minibuf_level == 0)
12790 /* When buffer is nonempty, redisplay window normally. */
12791 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12792 /* Quail displays non-mini buffers in minibuffer window.
12793 In that case, redisplay the window normally. */
12794 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12796 /* W is a mini-buffer window, but it's not active, so clear
12797 it. */
12798 int yb = window_text_bottom_y (w);
12799 struct glyph_row *row;
12800 int y;
12802 for (y = 0, row = w->desired_matrix->rows;
12803 y < yb;
12804 y += row->height, ++row)
12805 blank_row (w, row, y);
12806 goto finish_scroll_bars;
12809 clear_glyph_matrix (w->desired_matrix);
12812 /* Otherwise set up data on this window; select its buffer and point
12813 value. */
12814 /* Really select the buffer, for the sake of buffer-local
12815 variables. */
12816 set_buffer_internal_1 (XBUFFER (w->buffer));
12817 SET_TEXT_POS (opoint, PT, PT_BYTE);
12819 current_matrix_up_to_date_p
12820 = (!NILP (w->window_end_valid)
12821 && !current_buffer->clip_changed
12822 && !current_buffer->prevent_redisplay_optimizations_p
12823 && XFASTINT (w->last_modified) >= MODIFF
12824 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12826 buffer_unchanged_p
12827 = (!NILP (w->window_end_valid)
12828 && !current_buffer->clip_changed
12829 && XFASTINT (w->last_modified) >= MODIFF
12830 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12832 /* When windows_or_buffers_changed is non-zero, we can't rely on
12833 the window end being valid, so set it to nil there. */
12834 if (windows_or_buffers_changed)
12836 /* If window starts on a continuation line, maybe adjust the
12837 window start in case the window's width changed. */
12838 if (XMARKER (w->start)->buffer == current_buffer)
12839 compute_window_start_on_continuation_line (w);
12841 w->window_end_valid = Qnil;
12844 /* Some sanity checks. */
12845 CHECK_WINDOW_END (w);
12846 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12847 abort ();
12848 if (BYTEPOS (opoint) < CHARPOS (opoint))
12849 abort ();
12851 /* If %c is in mode line, update it if needed. */
12852 if (!NILP (w->column_number_displayed)
12853 /* This alternative quickly identifies a common case
12854 where no change is needed. */
12855 && !(PT == XFASTINT (w->last_point)
12856 && XFASTINT (w->last_modified) >= MODIFF
12857 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12858 && (XFASTINT (w->column_number_displayed)
12859 != (int) current_column ())) /* iftc */
12860 update_mode_line = 1;
12862 /* Count number of windows showing the selected buffer. An indirect
12863 buffer counts as its base buffer. */
12864 if (!just_this_one_p)
12866 struct buffer *current_base, *window_base;
12867 current_base = current_buffer;
12868 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12869 if (current_base->base_buffer)
12870 current_base = current_base->base_buffer;
12871 if (window_base->base_buffer)
12872 window_base = window_base->base_buffer;
12873 if (current_base == window_base)
12874 buffer_shared++;
12877 /* Point refers normally to the selected window. For any other
12878 window, set up appropriate value. */
12879 if (!EQ (window, selected_window))
12881 int new_pt = XMARKER (w->pointm)->charpos;
12882 int new_pt_byte = marker_byte_position (w->pointm);
12883 if (new_pt < BEGV)
12885 new_pt = BEGV;
12886 new_pt_byte = BEGV_BYTE;
12887 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12889 else if (new_pt > (ZV - 1))
12891 new_pt = ZV;
12892 new_pt_byte = ZV_BYTE;
12893 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12896 /* We don't use SET_PT so that the point-motion hooks don't run. */
12897 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12900 /* If any of the character widths specified in the display table
12901 have changed, invalidate the width run cache. It's true that
12902 this may be a bit late to catch such changes, but the rest of
12903 redisplay goes (non-fatally) haywire when the display table is
12904 changed, so why should we worry about doing any better? */
12905 if (current_buffer->width_run_cache)
12907 struct Lisp_Char_Table *disptab = buffer_display_table ();
12909 if (! disptab_matches_widthtab (disptab,
12910 XVECTOR (current_buffer->width_table)))
12912 invalidate_region_cache (current_buffer,
12913 current_buffer->width_run_cache,
12914 BEG, Z);
12915 recompute_width_table (current_buffer, disptab);
12919 /* If window-start is screwed up, choose a new one. */
12920 if (XMARKER (w->start)->buffer != current_buffer)
12921 goto recenter;
12923 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12925 /* If someone specified a new starting point but did not insist,
12926 check whether it can be used. */
12927 if (!NILP (w->optional_new_start)
12928 && CHARPOS (startp) >= BEGV
12929 && CHARPOS (startp) <= ZV)
12931 w->optional_new_start = Qnil;
12932 start_display (&it, w, startp);
12933 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12934 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12935 if (IT_CHARPOS (it) == PT)
12936 w->force_start = Qt;
12937 /* IT may overshoot PT if text at PT is invisible. */
12938 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12939 w->force_start = Qt;
12942 /* Handle case where place to start displaying has been specified,
12943 unless the specified location is outside the accessible range. */
12944 if (!NILP (w->force_start)
12945 || w->frozen_window_start_p)
12947 /* We set this later on if we have to adjust point. */
12948 int new_vpos = -1;
12949 int val;
12951 w->force_start = Qnil;
12952 w->vscroll = 0;
12953 w->window_end_valid = Qnil;
12955 /* Forget any recorded base line for line number display. */
12956 if (!buffer_unchanged_p)
12957 w->base_line_number = Qnil;
12959 /* Redisplay the mode line. Select the buffer properly for that.
12960 Also, run the hook window-scroll-functions
12961 because we have scrolled. */
12962 /* Note, we do this after clearing force_start because
12963 if there's an error, it is better to forget about force_start
12964 than to get into an infinite loop calling the hook functions
12965 and having them get more errors. */
12966 if (!update_mode_line
12967 || ! NILP (Vwindow_scroll_functions))
12969 update_mode_line = 1;
12970 w->update_mode_line = Qt;
12971 startp = run_window_scroll_functions (window, startp);
12974 w->last_modified = make_number (0);
12975 w->last_overlay_modified = make_number (0);
12976 if (CHARPOS (startp) < BEGV)
12977 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12978 else if (CHARPOS (startp) > ZV)
12979 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12981 /* Redisplay, then check if cursor has been set during the
12982 redisplay. Give up if new fonts were loaded. */
12983 val = try_window (window, startp, 1);
12984 if (!val)
12986 w->force_start = Qt;
12987 clear_glyph_matrix (w->desired_matrix);
12988 goto need_larger_matrices;
12990 /* Point was outside the scroll margins. */
12991 if (val < 0)
12992 new_vpos = window_box_height (w) / 2;
12994 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12996 /* If point does not appear, try to move point so it does
12997 appear. The desired matrix has been built above, so we
12998 can use it here. */
12999 new_vpos = window_box_height (w) / 2;
13002 if (!cursor_row_fully_visible_p (w, 0, 0))
13004 /* Point does appear, but on a line partly visible at end of window.
13005 Move it back to a fully-visible line. */
13006 new_vpos = window_box_height (w);
13009 /* If we need to move point for either of the above reasons,
13010 now actually do it. */
13011 if (new_vpos >= 0)
13013 struct glyph_row *row;
13015 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13016 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13017 ++row;
13019 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13020 MATRIX_ROW_START_BYTEPOS (row));
13022 if (w != XWINDOW (selected_window))
13023 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13024 else if (current_buffer == old)
13025 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13027 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13029 /* If we are highlighting the region, then we just changed
13030 the region, so redisplay to show it. */
13031 if (!NILP (Vtransient_mark_mode)
13032 && !NILP (current_buffer->mark_active))
13034 clear_glyph_matrix (w->desired_matrix);
13035 if (!try_window (window, startp, 0))
13036 goto need_larger_matrices;
13040 #if GLYPH_DEBUG
13041 debug_method_add (w, "forced window start");
13042 #endif
13043 goto done;
13046 /* Handle case where text has not changed, only point, and it has
13047 not moved off the frame, and we are not retrying after hscroll.
13048 (current_matrix_up_to_date_p is nonzero when retrying.) */
13049 if (current_matrix_up_to_date_p
13050 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13051 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13053 switch (rc)
13055 case CURSOR_MOVEMENT_SUCCESS:
13056 used_current_matrix_p = 1;
13057 goto done;
13059 #if 0 /* try_cursor_movement never returns this value. */
13060 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13061 goto need_larger_matrices;
13062 #endif
13064 case CURSOR_MOVEMENT_MUST_SCROLL:
13065 goto try_to_scroll;
13067 default:
13068 abort ();
13071 /* If current starting point was originally the beginning of a line
13072 but no longer is, find a new starting point. */
13073 else if (!NILP (w->start_at_line_beg)
13074 && !(CHARPOS (startp) <= BEGV
13075 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13077 #if GLYPH_DEBUG
13078 debug_method_add (w, "recenter 1");
13079 #endif
13080 goto recenter;
13083 /* Try scrolling with try_window_id. Value is > 0 if update has
13084 been done, it is -1 if we know that the same window start will
13085 not work. It is 0 if unsuccessful for some other reason. */
13086 else if ((tem = try_window_id (w)) != 0)
13088 #if GLYPH_DEBUG
13089 debug_method_add (w, "try_window_id %d", tem);
13090 #endif
13092 if (fonts_changed_p)
13093 goto need_larger_matrices;
13094 if (tem > 0)
13095 goto done;
13097 /* Otherwise try_window_id has returned -1 which means that we
13098 don't want the alternative below this comment to execute. */
13100 else if (CHARPOS (startp) >= BEGV
13101 && CHARPOS (startp) <= ZV
13102 && PT >= CHARPOS (startp)
13103 && (CHARPOS (startp) < ZV
13104 /* Avoid starting at end of buffer. */
13105 || CHARPOS (startp) == BEGV
13106 || (XFASTINT (w->last_modified) >= MODIFF
13107 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13110 /* If first window line is a continuation line, and window start
13111 is inside the modified region, but the first change is before
13112 current window start, we must select a new window start.*/
13113 if (NILP (w->start_at_line_beg)
13114 && CHARPOS (startp) > BEGV)
13116 /* Make sure beg_unchanged and end_unchanged are up to date.
13117 Do it only if buffer has really changed. This may or may
13118 not have been done by try_window_id (see which) already. */
13119 if (MODIFF > SAVE_MODIFF
13120 /* This seems to happen sometimes after saving a buffer. */
13121 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13123 if (GPT - BEG < BEG_UNCHANGED)
13124 BEG_UNCHANGED = GPT - BEG;
13125 if (Z - GPT < END_UNCHANGED)
13126 END_UNCHANGED = Z - GPT;
13129 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13130 && CHARPOS (startp) <= Z - END_UNCHANGED)
13132 /* There doesn't seems to be a simple way to find a new
13133 window start that is near the old window start, so
13134 we just recenter. */
13135 goto recenter;
13139 #if GLYPH_DEBUG
13140 debug_method_add (w, "same window start");
13141 #endif
13143 /* Try to redisplay starting at same place as before.
13144 If point has not moved off frame, accept the results. */
13145 if (!current_matrix_up_to_date_p
13146 /* Don't use try_window_reusing_current_matrix in this case
13147 because a window scroll function can have changed the
13148 buffer. */
13149 || !NILP (Vwindow_scroll_functions)
13150 || MINI_WINDOW_P (w)
13151 || !(used_current_matrix_p
13152 = try_window_reusing_current_matrix (w)))
13154 IF_DEBUG (debug_method_add (w, "1"));
13155 if (try_window (window, startp, 1) < 0)
13156 /* -1 means we need to scroll.
13157 0 means we need new matrices, but fonts_changed_p
13158 is set in that case, so we will detect it below. */
13159 goto try_to_scroll;
13162 if (fonts_changed_p)
13163 goto need_larger_matrices;
13165 if (w->cursor.vpos >= 0)
13167 if (!just_this_one_p
13168 || current_buffer->clip_changed
13169 || BEG_UNCHANGED < CHARPOS (startp))
13170 /* Forget any recorded base line for line number display. */
13171 w->base_line_number = Qnil;
13173 if (!cursor_row_fully_visible_p (w, 1, 0))
13175 clear_glyph_matrix (w->desired_matrix);
13176 last_line_misfit = 1;
13178 /* Drop through and scroll. */
13179 else
13180 goto done;
13182 else
13183 clear_glyph_matrix (w->desired_matrix);
13186 try_to_scroll:
13188 w->last_modified = make_number (0);
13189 w->last_overlay_modified = make_number (0);
13191 /* Redisplay the mode line. Select the buffer properly for that. */
13192 if (!update_mode_line)
13194 update_mode_line = 1;
13195 w->update_mode_line = Qt;
13198 /* Try to scroll by specified few lines. */
13199 if ((scroll_conservatively
13200 || scroll_step
13201 || temp_scroll_step
13202 || NUMBERP (current_buffer->scroll_up_aggressively)
13203 || NUMBERP (current_buffer->scroll_down_aggressively))
13204 && !current_buffer->clip_changed
13205 && CHARPOS (startp) >= BEGV
13206 && CHARPOS (startp) <= ZV)
13208 /* The function returns -1 if new fonts were loaded, 1 if
13209 successful, 0 if not successful. */
13210 int rc = try_scrolling (window, just_this_one_p,
13211 scroll_conservatively,
13212 scroll_step,
13213 temp_scroll_step, last_line_misfit);
13214 switch (rc)
13216 case SCROLLING_SUCCESS:
13217 goto done;
13219 case SCROLLING_NEED_LARGER_MATRICES:
13220 goto need_larger_matrices;
13222 case SCROLLING_FAILED:
13223 break;
13225 default:
13226 abort ();
13230 /* Finally, just choose place to start which centers point */
13232 recenter:
13233 if (centering_position < 0)
13234 centering_position = window_box_height (w) / 2;
13236 #if GLYPH_DEBUG
13237 debug_method_add (w, "recenter");
13238 #endif
13240 /* w->vscroll = 0; */
13242 /* Forget any previously recorded base line for line number display. */
13243 if (!buffer_unchanged_p)
13244 w->base_line_number = Qnil;
13246 /* Move backward half the height of the window. */
13247 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13248 it.current_y = it.last_visible_y;
13249 move_it_vertically_backward (&it, centering_position);
13250 xassert (IT_CHARPOS (it) >= BEGV);
13252 /* The function move_it_vertically_backward may move over more
13253 than the specified y-distance. If it->w is small, e.g. a
13254 mini-buffer window, we may end up in front of the window's
13255 display area. Start displaying at the start of the line
13256 containing PT in this case. */
13257 if (it.current_y <= 0)
13259 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13260 move_it_vertically_backward (&it, 0);
13261 #if 0
13262 /* I think this assert is bogus if buffer contains
13263 invisible text or images. KFS. */
13264 xassert (IT_CHARPOS (it) <= PT);
13265 #endif
13266 it.current_y = 0;
13269 it.current_x = it.hpos = 0;
13271 /* Set startp here explicitly in case that helps avoid an infinite loop
13272 in case the window-scroll-functions functions get errors. */
13273 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13275 /* Run scroll hooks. */
13276 startp = run_window_scroll_functions (window, it.current.pos);
13278 /* Redisplay the window. */
13279 if (!current_matrix_up_to_date_p
13280 || windows_or_buffers_changed
13281 || cursor_type_changed
13282 /* Don't use try_window_reusing_current_matrix in this case
13283 because it can have changed the buffer. */
13284 || !NILP (Vwindow_scroll_functions)
13285 || !just_this_one_p
13286 || MINI_WINDOW_P (w)
13287 || !(used_current_matrix_p
13288 = try_window_reusing_current_matrix (w)))
13289 try_window (window, startp, 0);
13291 /* If new fonts have been loaded (due to fontsets), give up. We
13292 have to start a new redisplay since we need to re-adjust glyph
13293 matrices. */
13294 if (fonts_changed_p)
13295 goto need_larger_matrices;
13297 /* If cursor did not appear assume that the middle of the window is
13298 in the first line of the window. Do it again with the next line.
13299 (Imagine a window of height 100, displaying two lines of height
13300 60. Moving back 50 from it->last_visible_y will end in the first
13301 line.) */
13302 if (w->cursor.vpos < 0)
13304 if (!NILP (w->window_end_valid)
13305 && PT >= Z - XFASTINT (w->window_end_pos))
13307 clear_glyph_matrix (w->desired_matrix);
13308 move_it_by_lines (&it, 1, 0);
13309 try_window (window, it.current.pos, 0);
13311 else if (PT < IT_CHARPOS (it))
13313 clear_glyph_matrix (w->desired_matrix);
13314 move_it_by_lines (&it, -1, 0);
13315 try_window (window, it.current.pos, 0);
13317 else
13319 /* Not much we can do about it. */
13323 /* Consider the following case: Window starts at BEGV, there is
13324 invisible, intangible text at BEGV, so that display starts at
13325 some point START > BEGV. It can happen that we are called with
13326 PT somewhere between BEGV and START. Try to handle that case. */
13327 if (w->cursor.vpos < 0)
13329 struct glyph_row *row = w->current_matrix->rows;
13330 if (row->mode_line_p)
13331 ++row;
13332 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13335 if (!cursor_row_fully_visible_p (w, 0, 0))
13337 /* If vscroll is enabled, disable it and try again. */
13338 if (w->vscroll)
13340 w->vscroll = 0;
13341 clear_glyph_matrix (w->desired_matrix);
13342 goto recenter;
13345 /* If centering point failed to make the whole line visible,
13346 put point at the top instead. That has to make the whole line
13347 visible, if it can be done. */
13348 if (centering_position == 0)
13349 goto done;
13351 clear_glyph_matrix (w->desired_matrix);
13352 centering_position = 0;
13353 goto recenter;
13356 done:
13358 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13359 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13360 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13361 ? Qt : Qnil);
13363 /* Display the mode line, if we must. */
13364 if ((update_mode_line
13365 /* If window not full width, must redo its mode line
13366 if (a) the window to its side is being redone and
13367 (b) we do a frame-based redisplay. This is a consequence
13368 of how inverted lines are drawn in frame-based redisplay. */
13369 || (!just_this_one_p
13370 && !FRAME_WINDOW_P (f)
13371 && !WINDOW_FULL_WIDTH_P (w))
13372 /* Line number to display. */
13373 || INTEGERP (w->base_line_pos)
13374 /* Column number is displayed and different from the one displayed. */
13375 || (!NILP (w->column_number_displayed)
13376 && (XFASTINT (w->column_number_displayed)
13377 != (int) current_column ()))) /* iftc */
13378 /* This means that the window has a mode line. */
13379 && (WINDOW_WANTS_MODELINE_P (w)
13380 || WINDOW_WANTS_HEADER_LINE_P (w)))
13382 display_mode_lines (w);
13384 /* If mode line height has changed, arrange for a thorough
13385 immediate redisplay using the correct mode line height. */
13386 if (WINDOW_WANTS_MODELINE_P (w)
13387 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13389 fonts_changed_p = 1;
13390 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13391 = DESIRED_MODE_LINE_HEIGHT (w);
13394 /* If top line height has changed, arrange for a thorough
13395 immediate redisplay using the correct mode line height. */
13396 if (WINDOW_WANTS_HEADER_LINE_P (w)
13397 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13399 fonts_changed_p = 1;
13400 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13401 = DESIRED_HEADER_LINE_HEIGHT (w);
13404 if (fonts_changed_p)
13405 goto need_larger_matrices;
13408 if (!line_number_displayed
13409 && !BUFFERP (w->base_line_pos))
13411 w->base_line_pos = Qnil;
13412 w->base_line_number = Qnil;
13415 finish_menu_bars:
13417 /* When we reach a frame's selected window, redo the frame's menu bar. */
13418 if (update_mode_line
13419 && EQ (FRAME_SELECTED_WINDOW (f), window))
13421 int redisplay_menu_p = 0;
13422 int redisplay_tool_bar_p = 0;
13424 if (FRAME_WINDOW_P (f))
13426 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13427 || defined (USE_GTK)
13428 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13429 #else
13430 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13431 #endif
13433 else
13434 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13436 if (redisplay_menu_p)
13437 display_menu_bar (w);
13439 #ifdef HAVE_WINDOW_SYSTEM
13440 #ifdef USE_GTK
13441 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13442 #else
13443 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13444 && (FRAME_TOOL_BAR_LINES (f) > 0
13445 || auto_resize_tool_bars_p);
13447 #endif
13449 if (redisplay_tool_bar_p)
13450 redisplay_tool_bar (f);
13451 #endif
13454 #ifdef HAVE_WINDOW_SYSTEM
13455 if (FRAME_WINDOW_P (f)
13456 && update_window_fringes (w, (just_this_one_p
13457 || (!used_current_matrix_p && !overlay_arrow_seen)
13458 || w->pseudo_window_p)))
13460 update_begin (f);
13461 BLOCK_INPUT;
13462 if (draw_window_fringes (w, 1))
13463 x_draw_vertical_border (w);
13464 UNBLOCK_INPUT;
13465 update_end (f);
13467 #endif /* HAVE_WINDOW_SYSTEM */
13469 /* We go to this label, with fonts_changed_p nonzero,
13470 if it is necessary to try again using larger glyph matrices.
13471 We have to redeem the scroll bar even in this case,
13472 because the loop in redisplay_internal expects that. */
13473 need_larger_matrices:
13475 finish_scroll_bars:
13477 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13479 /* Set the thumb's position and size. */
13480 set_vertical_scroll_bar (w);
13482 /* Note that we actually used the scroll bar attached to this
13483 window, so it shouldn't be deleted at the end of redisplay. */
13484 redeem_scroll_bar_hook (w);
13487 /* Restore current_buffer and value of point in it. */
13488 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13489 set_buffer_internal_1 (old);
13490 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13492 unbind_to (count, Qnil);
13496 /* Build the complete desired matrix of WINDOW with a window start
13497 buffer position POS.
13499 Value is 1 if successful. It is zero if fonts were loaded during
13500 redisplay which makes re-adjusting glyph matrices necessary, and -1
13501 if point would appear in the scroll margins.
13502 (We check that only if CHECK_MARGINS is nonzero. */
13505 try_window (window, pos, check_margins)
13506 Lisp_Object window;
13507 struct text_pos pos;
13508 int check_margins;
13510 struct window *w = XWINDOW (window);
13511 struct it it;
13512 struct glyph_row *last_text_row = NULL;
13514 /* Make POS the new window start. */
13515 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13517 /* Mark cursor position as unknown. No overlay arrow seen. */
13518 w->cursor.vpos = -1;
13519 overlay_arrow_seen = 0;
13521 /* Initialize iterator and info to start at POS. */
13522 start_display (&it, w, pos);
13524 /* Display all lines of W. */
13525 while (it.current_y < it.last_visible_y)
13527 if (display_line (&it))
13528 last_text_row = it.glyph_row - 1;
13529 if (fonts_changed_p)
13530 return 0;
13533 /* Don't let the cursor end in the scroll margins. */
13534 if (check_margins
13535 && !MINI_WINDOW_P (w))
13537 int this_scroll_margin;
13539 this_scroll_margin = max (0, scroll_margin);
13540 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13541 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13543 if ((w->cursor.y >= 0 /* not vscrolled */
13544 && w->cursor.y < this_scroll_margin
13545 && CHARPOS (pos) > BEGV
13546 && IT_CHARPOS (it) < ZV)
13547 /* rms: considering make_cursor_line_fully_visible_p here
13548 seems to give wrong results. We don't want to recenter
13549 when the last line is partly visible, we want to allow
13550 that case to be handled in the usual way. */
13551 || (w->cursor.y + 1) > it.last_visible_y)
13553 w->cursor.vpos = -1;
13554 clear_glyph_matrix (w->desired_matrix);
13555 return -1;
13559 /* If bottom moved off end of frame, change mode line percentage. */
13560 if (XFASTINT (w->window_end_pos) <= 0
13561 && Z != IT_CHARPOS (it))
13562 w->update_mode_line = Qt;
13564 /* Set window_end_pos to the offset of the last character displayed
13565 on the window from the end of current_buffer. Set
13566 window_end_vpos to its row number. */
13567 if (last_text_row)
13569 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13570 w->window_end_bytepos
13571 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13572 w->window_end_pos
13573 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13574 w->window_end_vpos
13575 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13576 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13577 ->displays_text_p);
13579 else
13581 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13582 w->window_end_pos = make_number (Z - ZV);
13583 w->window_end_vpos = make_number (0);
13586 /* But that is not valid info until redisplay finishes. */
13587 w->window_end_valid = Qnil;
13588 return 1;
13593 /************************************************************************
13594 Window redisplay reusing current matrix when buffer has not changed
13595 ************************************************************************/
13597 /* Try redisplay of window W showing an unchanged buffer with a
13598 different window start than the last time it was displayed by
13599 reusing its current matrix. Value is non-zero if successful.
13600 W->start is the new window start. */
13602 static int
13603 try_window_reusing_current_matrix (w)
13604 struct window *w;
13606 struct frame *f = XFRAME (w->frame);
13607 struct glyph_row *row, *bottom_row;
13608 struct it it;
13609 struct run run;
13610 struct text_pos start, new_start;
13611 int nrows_scrolled, i;
13612 struct glyph_row *last_text_row;
13613 struct glyph_row *last_reused_text_row;
13614 struct glyph_row *start_row;
13615 int start_vpos, min_y, max_y;
13617 #if GLYPH_DEBUG
13618 if (inhibit_try_window_reusing)
13619 return 0;
13620 #endif
13622 if (/* This function doesn't handle terminal frames. */
13623 !FRAME_WINDOW_P (f)
13624 /* Don't try to reuse the display if windows have been split
13625 or such. */
13626 || windows_or_buffers_changed
13627 || cursor_type_changed)
13628 return 0;
13630 /* Can't do this if region may have changed. */
13631 if ((!NILP (Vtransient_mark_mode)
13632 && !NILP (current_buffer->mark_active))
13633 || !NILP (w->region_showing)
13634 || !NILP (Vshow_trailing_whitespace))
13635 return 0;
13637 /* If top-line visibility has changed, give up. */
13638 if (WINDOW_WANTS_HEADER_LINE_P (w)
13639 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13640 return 0;
13642 /* Give up if old or new display is scrolled vertically. We could
13643 make this function handle this, but right now it doesn't. */
13644 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13645 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13646 return 0;
13648 /* The variable new_start now holds the new window start. The old
13649 start `start' can be determined from the current matrix. */
13650 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13651 start = start_row->start.pos;
13652 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13654 /* Clear the desired matrix for the display below. */
13655 clear_glyph_matrix (w->desired_matrix);
13657 if (CHARPOS (new_start) <= CHARPOS (start))
13659 int first_row_y;
13661 /* Don't use this method if the display starts with an ellipsis
13662 displayed for invisible text. It's not easy to handle that case
13663 below, and it's certainly not worth the effort since this is
13664 not a frequent case. */
13665 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13666 return 0;
13668 IF_DEBUG (debug_method_add (w, "twu1"));
13670 /* Display up to a row that can be reused. The variable
13671 last_text_row is set to the last row displayed that displays
13672 text. Note that it.vpos == 0 if or if not there is a
13673 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13674 start_display (&it, w, new_start);
13675 first_row_y = it.current_y;
13676 w->cursor.vpos = -1;
13677 last_text_row = last_reused_text_row = NULL;
13679 while (it.current_y < it.last_visible_y
13680 && !fonts_changed_p)
13682 /* If we have reached into the characters in the START row,
13683 that means the line boundaries have changed. So we
13684 can't start copying with the row START. Maybe it will
13685 work to start copying with the following row. */
13686 while (IT_CHARPOS (it) > CHARPOS (start))
13688 /* Advance to the next row as the "start". */
13689 start_row++;
13690 start = start_row->start.pos;
13691 /* If there are no more rows to try, or just one, give up. */
13692 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13693 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13694 || CHARPOS (start) == ZV)
13696 clear_glyph_matrix (w->desired_matrix);
13697 return 0;
13700 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13702 /* If we have reached alignment,
13703 we can copy the rest of the rows. */
13704 if (IT_CHARPOS (it) == CHARPOS (start))
13705 break;
13707 if (display_line (&it))
13708 last_text_row = it.glyph_row - 1;
13711 /* A value of current_y < last_visible_y means that we stopped
13712 at the previous window start, which in turn means that we
13713 have at least one reusable row. */
13714 if (it.current_y < it.last_visible_y)
13716 /* IT.vpos always starts from 0; it counts text lines. */
13717 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13719 /* Find PT if not already found in the lines displayed. */
13720 if (w->cursor.vpos < 0)
13722 int dy = it.current_y - start_row->y;
13724 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13725 row = row_containing_pos (w, PT, row, NULL, dy);
13726 if (row)
13727 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13728 dy, nrows_scrolled);
13729 else
13731 clear_glyph_matrix (w->desired_matrix);
13732 return 0;
13736 /* Scroll the display. Do it before the current matrix is
13737 changed. The problem here is that update has not yet
13738 run, i.e. part of the current matrix is not up to date.
13739 scroll_run_hook will clear the cursor, and use the
13740 current matrix to get the height of the row the cursor is
13741 in. */
13742 run.current_y = start_row->y;
13743 run.desired_y = it.current_y;
13744 run.height = it.last_visible_y - it.current_y;
13746 if (run.height > 0 && run.current_y != run.desired_y)
13748 update_begin (f);
13749 rif->update_window_begin_hook (w);
13750 rif->clear_window_mouse_face (w);
13751 rif->scroll_run_hook (w, &run);
13752 rif->update_window_end_hook (w, 0, 0);
13753 update_end (f);
13756 /* Shift current matrix down by nrows_scrolled lines. */
13757 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13758 rotate_matrix (w->current_matrix,
13759 start_vpos,
13760 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13761 nrows_scrolled);
13763 /* Disable lines that must be updated. */
13764 for (i = 0; i < it.vpos; ++i)
13765 (start_row + i)->enabled_p = 0;
13767 /* Re-compute Y positions. */
13768 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13769 max_y = it.last_visible_y;
13770 for (row = start_row + nrows_scrolled;
13771 row < bottom_row;
13772 ++row)
13774 row->y = it.current_y;
13775 row->visible_height = row->height;
13777 if (row->y < min_y)
13778 row->visible_height -= min_y - row->y;
13779 if (row->y + row->height > max_y)
13780 row->visible_height -= row->y + row->height - max_y;
13781 row->redraw_fringe_bitmaps_p = 1;
13783 it.current_y += row->height;
13785 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13786 last_reused_text_row = row;
13787 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13788 break;
13791 /* Disable lines in the current matrix which are now
13792 below the window. */
13793 for (++row; row < bottom_row; ++row)
13794 row->enabled_p = row->mode_line_p = 0;
13797 /* Update window_end_pos etc.; last_reused_text_row is the last
13798 reused row from the current matrix containing text, if any.
13799 The value of last_text_row is the last displayed line
13800 containing text. */
13801 if (last_reused_text_row)
13803 w->window_end_bytepos
13804 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13805 w->window_end_pos
13806 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13807 w->window_end_vpos
13808 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13809 w->current_matrix));
13811 else if (last_text_row)
13813 w->window_end_bytepos
13814 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13815 w->window_end_pos
13816 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13817 w->window_end_vpos
13818 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13820 else
13822 /* This window must be completely empty. */
13823 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13824 w->window_end_pos = make_number (Z - ZV);
13825 w->window_end_vpos = make_number (0);
13827 w->window_end_valid = Qnil;
13829 /* Update hint: don't try scrolling again in update_window. */
13830 w->desired_matrix->no_scrolling_p = 1;
13832 #if GLYPH_DEBUG
13833 debug_method_add (w, "try_window_reusing_current_matrix 1");
13834 #endif
13835 return 1;
13837 else if (CHARPOS (new_start) > CHARPOS (start))
13839 struct glyph_row *pt_row, *row;
13840 struct glyph_row *first_reusable_row;
13841 struct glyph_row *first_row_to_display;
13842 int dy;
13843 int yb = window_text_bottom_y (w);
13845 /* Find the row starting at new_start, if there is one. Don't
13846 reuse a partially visible line at the end. */
13847 first_reusable_row = start_row;
13848 while (first_reusable_row->enabled_p
13849 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13850 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13851 < CHARPOS (new_start)))
13852 ++first_reusable_row;
13854 /* Give up if there is no row to reuse. */
13855 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13856 || !first_reusable_row->enabled_p
13857 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13858 != CHARPOS (new_start)))
13859 return 0;
13861 /* We can reuse fully visible rows beginning with
13862 first_reusable_row to the end of the window. Set
13863 first_row_to_display to the first row that cannot be reused.
13864 Set pt_row to the row containing point, if there is any. */
13865 pt_row = NULL;
13866 for (first_row_to_display = first_reusable_row;
13867 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13868 ++first_row_to_display)
13870 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13871 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13872 pt_row = first_row_to_display;
13875 /* Start displaying at the start of first_row_to_display. */
13876 xassert (first_row_to_display->y < yb);
13877 init_to_row_start (&it, w, first_row_to_display);
13879 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13880 - start_vpos);
13881 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13882 - nrows_scrolled);
13883 it.current_y = (first_row_to_display->y - first_reusable_row->y
13884 + WINDOW_HEADER_LINE_HEIGHT (w));
13886 /* Display lines beginning with first_row_to_display in the
13887 desired matrix. Set last_text_row to the last row displayed
13888 that displays text. */
13889 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13890 if (pt_row == NULL)
13891 w->cursor.vpos = -1;
13892 last_text_row = NULL;
13893 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13894 if (display_line (&it))
13895 last_text_row = it.glyph_row - 1;
13897 /* Give up If point isn't in a row displayed or reused. */
13898 if (w->cursor.vpos < 0)
13900 clear_glyph_matrix (w->desired_matrix);
13901 return 0;
13904 /* If point is in a reused row, adjust y and vpos of the cursor
13905 position. */
13906 if (pt_row)
13908 w->cursor.vpos -= nrows_scrolled;
13909 w->cursor.y -= first_reusable_row->y - start_row->y;
13912 /* Scroll the display. */
13913 run.current_y = first_reusable_row->y;
13914 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13915 run.height = it.last_visible_y - run.current_y;
13916 dy = run.current_y - run.desired_y;
13918 if (run.height)
13920 update_begin (f);
13921 rif->update_window_begin_hook (w);
13922 rif->clear_window_mouse_face (w);
13923 rif->scroll_run_hook (w, &run);
13924 rif->update_window_end_hook (w, 0, 0);
13925 update_end (f);
13928 /* Adjust Y positions of reused rows. */
13929 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13930 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13931 max_y = it.last_visible_y;
13932 for (row = first_reusable_row; row < first_row_to_display; ++row)
13934 row->y -= dy;
13935 row->visible_height = row->height;
13936 if (row->y < min_y)
13937 row->visible_height -= min_y - row->y;
13938 if (row->y + row->height > max_y)
13939 row->visible_height -= row->y + row->height - max_y;
13940 row->redraw_fringe_bitmaps_p = 1;
13943 /* Scroll the current matrix. */
13944 xassert (nrows_scrolled > 0);
13945 rotate_matrix (w->current_matrix,
13946 start_vpos,
13947 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13948 -nrows_scrolled);
13950 /* Disable rows not reused. */
13951 for (row -= nrows_scrolled; row < bottom_row; ++row)
13952 row->enabled_p = 0;
13954 /* Point may have moved to a different line, so we cannot assume that
13955 the previous cursor position is valid; locate the correct row. */
13956 if (pt_row)
13958 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13959 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13960 row++)
13962 w->cursor.vpos++;
13963 w->cursor.y = row->y;
13965 if (row < bottom_row)
13967 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13968 while (glyph->charpos < PT)
13970 w->cursor.hpos++;
13971 w->cursor.x += glyph->pixel_width;
13972 glyph++;
13977 /* Adjust window end. A null value of last_text_row means that
13978 the window end is in reused rows which in turn means that
13979 only its vpos can have changed. */
13980 if (last_text_row)
13982 w->window_end_bytepos
13983 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13984 w->window_end_pos
13985 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13986 w->window_end_vpos
13987 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13989 else
13991 w->window_end_vpos
13992 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13995 w->window_end_valid = Qnil;
13996 w->desired_matrix->no_scrolling_p = 1;
13998 #if GLYPH_DEBUG
13999 debug_method_add (w, "try_window_reusing_current_matrix 2");
14000 #endif
14001 return 1;
14004 return 0;
14009 /************************************************************************
14010 Window redisplay reusing current matrix when buffer has changed
14011 ************************************************************************/
14013 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14014 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14015 int *, int *));
14016 static struct glyph_row *
14017 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14018 struct glyph_row *));
14021 /* Return the last row in MATRIX displaying text. If row START is
14022 non-null, start searching with that row. IT gives the dimensions
14023 of the display. Value is null if matrix is empty; otherwise it is
14024 a pointer to the row found. */
14026 static struct glyph_row *
14027 find_last_row_displaying_text (matrix, it, start)
14028 struct glyph_matrix *matrix;
14029 struct it *it;
14030 struct glyph_row *start;
14032 struct glyph_row *row, *row_found;
14034 /* Set row_found to the last row in IT->w's current matrix
14035 displaying text. The loop looks funny but think of partially
14036 visible lines. */
14037 row_found = NULL;
14038 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14039 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14041 xassert (row->enabled_p);
14042 row_found = row;
14043 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14044 break;
14045 ++row;
14048 return row_found;
14052 /* Return the last row in the current matrix of W that is not affected
14053 by changes at the start of current_buffer that occurred since W's
14054 current matrix was built. Value is null if no such row exists.
14056 BEG_UNCHANGED us the number of characters unchanged at the start of
14057 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14058 first changed character in current_buffer. Characters at positions <
14059 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14060 when the current matrix was built. */
14062 static struct glyph_row *
14063 find_last_unchanged_at_beg_row (w)
14064 struct window *w;
14066 int first_changed_pos = BEG + BEG_UNCHANGED;
14067 struct glyph_row *row;
14068 struct glyph_row *row_found = NULL;
14069 int yb = window_text_bottom_y (w);
14071 /* Find the last row displaying unchanged text. */
14072 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14073 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14074 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14076 if (/* If row ends before first_changed_pos, it is unchanged,
14077 except in some case. */
14078 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14079 /* When row ends in ZV and we write at ZV it is not
14080 unchanged. */
14081 && !row->ends_at_zv_p
14082 /* When first_changed_pos is the end of a continued line,
14083 row is not unchanged because it may be no longer
14084 continued. */
14085 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14086 && (row->continued_p
14087 || row->exact_window_width_line_p)))
14088 row_found = row;
14090 /* Stop if last visible row. */
14091 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14092 break;
14094 ++row;
14097 return row_found;
14101 /* Find the first glyph row in the current matrix of W that is not
14102 affected by changes at the end of current_buffer since the
14103 time W's current matrix was built.
14105 Return in *DELTA the number of chars by which buffer positions in
14106 unchanged text at the end of current_buffer must be adjusted.
14108 Return in *DELTA_BYTES the corresponding number of bytes.
14110 Value is null if no such row exists, i.e. all rows are affected by
14111 changes. */
14113 static struct glyph_row *
14114 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14115 struct window *w;
14116 int *delta, *delta_bytes;
14118 struct glyph_row *row;
14119 struct glyph_row *row_found = NULL;
14121 *delta = *delta_bytes = 0;
14123 /* Display must not have been paused, otherwise the current matrix
14124 is not up to date. */
14125 if (NILP (w->window_end_valid))
14126 abort ();
14128 /* A value of window_end_pos >= END_UNCHANGED means that the window
14129 end is in the range of changed text. If so, there is no
14130 unchanged row at the end of W's current matrix. */
14131 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14132 return NULL;
14134 /* Set row to the last row in W's current matrix displaying text. */
14135 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14137 /* If matrix is entirely empty, no unchanged row exists. */
14138 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14140 /* The value of row is the last glyph row in the matrix having a
14141 meaningful buffer position in it. The end position of row
14142 corresponds to window_end_pos. This allows us to translate
14143 buffer positions in the current matrix to current buffer
14144 positions for characters not in changed text. */
14145 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14146 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14147 int last_unchanged_pos, last_unchanged_pos_old;
14148 struct glyph_row *first_text_row
14149 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14151 *delta = Z - Z_old;
14152 *delta_bytes = Z_BYTE - Z_BYTE_old;
14154 /* Set last_unchanged_pos to the buffer position of the last
14155 character in the buffer that has not been changed. Z is the
14156 index + 1 of the last character in current_buffer, i.e. by
14157 subtracting END_UNCHANGED we get the index of the last
14158 unchanged character, and we have to add BEG to get its buffer
14159 position. */
14160 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14161 last_unchanged_pos_old = last_unchanged_pos - *delta;
14163 /* Search backward from ROW for a row displaying a line that
14164 starts at a minimum position >= last_unchanged_pos_old. */
14165 for (; row > first_text_row; --row)
14167 /* This used to abort, but it can happen.
14168 It is ok to just stop the search instead here. KFS. */
14169 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14170 break;
14172 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14173 row_found = row;
14177 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14178 abort ();
14180 return row_found;
14184 /* Make sure that glyph rows in the current matrix of window W
14185 reference the same glyph memory as corresponding rows in the
14186 frame's frame matrix. This function is called after scrolling W's
14187 current matrix on a terminal frame in try_window_id and
14188 try_window_reusing_current_matrix. */
14190 static void
14191 sync_frame_with_window_matrix_rows (w)
14192 struct window *w;
14194 struct frame *f = XFRAME (w->frame);
14195 struct glyph_row *window_row, *window_row_end, *frame_row;
14197 /* Preconditions: W must be a leaf window and full-width. Its frame
14198 must have a frame matrix. */
14199 xassert (NILP (w->hchild) && NILP (w->vchild));
14200 xassert (WINDOW_FULL_WIDTH_P (w));
14201 xassert (!FRAME_WINDOW_P (f));
14203 /* If W is a full-width window, glyph pointers in W's current matrix
14204 have, by definition, to be the same as glyph pointers in the
14205 corresponding frame matrix. Note that frame matrices have no
14206 marginal areas (see build_frame_matrix). */
14207 window_row = w->current_matrix->rows;
14208 window_row_end = window_row + w->current_matrix->nrows;
14209 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14210 while (window_row < window_row_end)
14212 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14213 struct glyph *end = window_row->glyphs[LAST_AREA];
14215 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14216 frame_row->glyphs[TEXT_AREA] = start;
14217 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14218 frame_row->glyphs[LAST_AREA] = end;
14220 /* Disable frame rows whose corresponding window rows have
14221 been disabled in try_window_id. */
14222 if (!window_row->enabled_p)
14223 frame_row->enabled_p = 0;
14225 ++window_row, ++frame_row;
14230 /* Find the glyph row in window W containing CHARPOS. Consider all
14231 rows between START and END (not inclusive). END null means search
14232 all rows to the end of the display area of W. Value is the row
14233 containing CHARPOS or null. */
14235 struct glyph_row *
14236 row_containing_pos (w, charpos, start, end, dy)
14237 struct window *w;
14238 int charpos;
14239 struct glyph_row *start, *end;
14240 int dy;
14242 struct glyph_row *row = start;
14243 int last_y;
14245 /* If we happen to start on a header-line, skip that. */
14246 if (row->mode_line_p)
14247 ++row;
14249 if ((end && row >= end) || !row->enabled_p)
14250 return NULL;
14252 last_y = window_text_bottom_y (w) - dy;
14254 while (1)
14256 /* Give up if we have gone too far. */
14257 if (end && row >= end)
14258 return NULL;
14259 /* This formerly returned if they were equal.
14260 I think that both quantities are of a "last plus one" type;
14261 if so, when they are equal, the row is within the screen. -- rms. */
14262 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14263 return NULL;
14265 /* If it is in this row, return this row. */
14266 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14267 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14268 /* The end position of a row equals the start
14269 position of the next row. If CHARPOS is there, we
14270 would rather display it in the next line, except
14271 when this line ends in ZV. */
14272 && !row->ends_at_zv_p
14273 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14274 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14275 return row;
14276 ++row;
14281 /* Try to redisplay window W by reusing its existing display. W's
14282 current matrix must be up to date when this function is called,
14283 i.e. window_end_valid must not be nil.
14285 Value is
14287 1 if display has been updated
14288 0 if otherwise unsuccessful
14289 -1 if redisplay with same window start is known not to succeed
14291 The following steps are performed:
14293 1. Find the last row in the current matrix of W that is not
14294 affected by changes at the start of current_buffer. If no such row
14295 is found, give up.
14297 2. Find the first row in W's current matrix that is not affected by
14298 changes at the end of current_buffer. Maybe there is no such row.
14300 3. Display lines beginning with the row + 1 found in step 1 to the
14301 row found in step 2 or, if step 2 didn't find a row, to the end of
14302 the window.
14304 4. If cursor is not known to appear on the window, give up.
14306 5. If display stopped at the row found in step 2, scroll the
14307 display and current matrix as needed.
14309 6. Maybe display some lines at the end of W, if we must. This can
14310 happen under various circumstances, like a partially visible line
14311 becoming fully visible, or because newly displayed lines are displayed
14312 in smaller font sizes.
14314 7. Update W's window end information. */
14316 static int
14317 try_window_id (w)
14318 struct window *w;
14320 struct frame *f = XFRAME (w->frame);
14321 struct glyph_matrix *current_matrix = w->current_matrix;
14322 struct glyph_matrix *desired_matrix = w->desired_matrix;
14323 struct glyph_row *last_unchanged_at_beg_row;
14324 struct glyph_row *first_unchanged_at_end_row;
14325 struct glyph_row *row;
14326 struct glyph_row *bottom_row;
14327 int bottom_vpos;
14328 struct it it;
14329 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14330 struct text_pos start_pos;
14331 struct run run;
14332 int first_unchanged_at_end_vpos = 0;
14333 struct glyph_row *last_text_row, *last_text_row_at_end;
14334 struct text_pos start;
14335 int first_changed_charpos, last_changed_charpos;
14337 #if GLYPH_DEBUG
14338 if (inhibit_try_window_id)
14339 return 0;
14340 #endif
14342 /* This is handy for debugging. */
14343 #if 0
14344 #define GIVE_UP(X) \
14345 do { \
14346 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14347 return 0; \
14348 } while (0)
14349 #else
14350 #define GIVE_UP(X) return 0
14351 #endif
14353 SET_TEXT_POS_FROM_MARKER (start, w->start);
14355 /* Don't use this for mini-windows because these can show
14356 messages and mini-buffers, and we don't handle that here. */
14357 if (MINI_WINDOW_P (w))
14358 GIVE_UP (1);
14360 /* This flag is used to prevent redisplay optimizations. */
14361 if (windows_or_buffers_changed || cursor_type_changed)
14362 GIVE_UP (2);
14364 /* Verify that narrowing has not changed.
14365 Also verify that we were not told to prevent redisplay optimizations.
14366 It would be nice to further
14367 reduce the number of cases where this prevents try_window_id. */
14368 if (current_buffer->clip_changed
14369 || current_buffer->prevent_redisplay_optimizations_p)
14370 GIVE_UP (3);
14372 /* Window must either use window-based redisplay or be full width. */
14373 if (!FRAME_WINDOW_P (f)
14374 && (!line_ins_del_ok
14375 || !WINDOW_FULL_WIDTH_P (w)))
14376 GIVE_UP (4);
14378 /* Give up if point is not known NOT to appear in W. */
14379 if (PT < CHARPOS (start))
14380 GIVE_UP (5);
14382 /* Another way to prevent redisplay optimizations. */
14383 if (XFASTINT (w->last_modified) == 0)
14384 GIVE_UP (6);
14386 /* Verify that window is not hscrolled. */
14387 if (XFASTINT (w->hscroll) != 0)
14388 GIVE_UP (7);
14390 /* Verify that display wasn't paused. */
14391 if (NILP (w->window_end_valid))
14392 GIVE_UP (8);
14394 /* Can't use this if highlighting a region because a cursor movement
14395 will do more than just set the cursor. */
14396 if (!NILP (Vtransient_mark_mode)
14397 && !NILP (current_buffer->mark_active))
14398 GIVE_UP (9);
14400 /* Likewise if highlighting trailing whitespace. */
14401 if (!NILP (Vshow_trailing_whitespace))
14402 GIVE_UP (11);
14404 /* Likewise if showing a region. */
14405 if (!NILP (w->region_showing))
14406 GIVE_UP (10);
14408 /* Can use this if overlay arrow position and or string have changed. */
14409 if (overlay_arrows_changed_p ())
14410 GIVE_UP (12);
14413 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14414 only if buffer has really changed. The reason is that the gap is
14415 initially at Z for freshly visited files. The code below would
14416 set end_unchanged to 0 in that case. */
14417 if (MODIFF > SAVE_MODIFF
14418 /* This seems to happen sometimes after saving a buffer. */
14419 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14421 if (GPT - BEG < BEG_UNCHANGED)
14422 BEG_UNCHANGED = GPT - BEG;
14423 if (Z - GPT < END_UNCHANGED)
14424 END_UNCHANGED = Z - GPT;
14427 /* The position of the first and last character that has been changed. */
14428 first_changed_charpos = BEG + BEG_UNCHANGED;
14429 last_changed_charpos = Z - END_UNCHANGED;
14431 /* If window starts after a line end, and the last change is in
14432 front of that newline, then changes don't affect the display.
14433 This case happens with stealth-fontification. Note that although
14434 the display is unchanged, glyph positions in the matrix have to
14435 be adjusted, of course. */
14436 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14437 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14438 && ((last_changed_charpos < CHARPOS (start)
14439 && CHARPOS (start) == BEGV)
14440 || (last_changed_charpos < CHARPOS (start) - 1
14441 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14443 int Z_old, delta, Z_BYTE_old, delta_bytes;
14444 struct glyph_row *r0;
14446 /* Compute how many chars/bytes have been added to or removed
14447 from the buffer. */
14448 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14449 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14450 delta = Z - Z_old;
14451 delta_bytes = Z_BYTE - Z_BYTE_old;
14453 /* Give up if PT is not in the window. Note that it already has
14454 been checked at the start of try_window_id that PT is not in
14455 front of the window start. */
14456 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14457 GIVE_UP (13);
14459 /* If window start is unchanged, we can reuse the whole matrix
14460 as is, after adjusting glyph positions. No need to compute
14461 the window end again, since its offset from Z hasn't changed. */
14462 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14463 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14464 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14465 /* PT must not be in a partially visible line. */
14466 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14467 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14469 /* Adjust positions in the glyph matrix. */
14470 if (delta || delta_bytes)
14472 struct glyph_row *r1
14473 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14474 increment_matrix_positions (w->current_matrix,
14475 MATRIX_ROW_VPOS (r0, current_matrix),
14476 MATRIX_ROW_VPOS (r1, current_matrix),
14477 delta, delta_bytes);
14480 /* Set the cursor. */
14481 row = row_containing_pos (w, PT, r0, NULL, 0);
14482 if (row)
14483 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14484 else
14485 abort ();
14486 return 1;
14490 /* Handle the case that changes are all below what is displayed in
14491 the window, and that PT is in the window. This shortcut cannot
14492 be taken if ZV is visible in the window, and text has been added
14493 there that is visible in the window. */
14494 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14495 /* ZV is not visible in the window, or there are no
14496 changes at ZV, actually. */
14497 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14498 || first_changed_charpos == last_changed_charpos))
14500 struct glyph_row *r0;
14502 /* Give up if PT is not in the window. Note that it already has
14503 been checked at the start of try_window_id that PT is not in
14504 front of the window start. */
14505 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14506 GIVE_UP (14);
14508 /* If window start is unchanged, we can reuse the whole matrix
14509 as is, without changing glyph positions since no text has
14510 been added/removed in front of the window end. */
14511 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14512 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14513 /* PT must not be in a partially visible line. */
14514 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14515 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14517 /* We have to compute the window end anew since text
14518 can have been added/removed after it. */
14519 w->window_end_pos
14520 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14521 w->window_end_bytepos
14522 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14524 /* Set the cursor. */
14525 row = row_containing_pos (w, PT, r0, NULL, 0);
14526 if (row)
14527 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14528 else
14529 abort ();
14530 return 2;
14534 /* Give up if window start is in the changed area.
14536 The condition used to read
14538 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14540 but why that was tested escapes me at the moment. */
14541 if (CHARPOS (start) >= first_changed_charpos
14542 && CHARPOS (start) <= last_changed_charpos)
14543 GIVE_UP (15);
14545 /* Check that window start agrees with the start of the first glyph
14546 row in its current matrix. Check this after we know the window
14547 start is not in changed text, otherwise positions would not be
14548 comparable. */
14549 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14550 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14551 GIVE_UP (16);
14553 /* Give up if the window ends in strings. Overlay strings
14554 at the end are difficult to handle, so don't try. */
14555 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14556 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14557 GIVE_UP (20);
14559 /* Compute the position at which we have to start displaying new
14560 lines. Some of the lines at the top of the window might be
14561 reusable because they are not displaying changed text. Find the
14562 last row in W's current matrix not affected by changes at the
14563 start of current_buffer. Value is null if changes start in the
14564 first line of window. */
14565 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14566 if (last_unchanged_at_beg_row)
14568 /* Avoid starting to display in the moddle of a character, a TAB
14569 for instance. This is easier than to set up the iterator
14570 exactly, and it's not a frequent case, so the additional
14571 effort wouldn't really pay off. */
14572 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14573 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14574 && last_unchanged_at_beg_row > w->current_matrix->rows)
14575 --last_unchanged_at_beg_row;
14577 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14578 GIVE_UP (17);
14580 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14581 GIVE_UP (18);
14582 start_pos = it.current.pos;
14584 /* Start displaying new lines in the desired matrix at the same
14585 vpos we would use in the current matrix, i.e. below
14586 last_unchanged_at_beg_row. */
14587 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14588 current_matrix);
14589 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14590 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14592 xassert (it.hpos == 0 && it.current_x == 0);
14594 else
14596 /* There are no reusable lines at the start of the window.
14597 Start displaying in the first text line. */
14598 start_display (&it, w, start);
14599 it.vpos = it.first_vpos;
14600 start_pos = it.current.pos;
14603 /* Find the first row that is not affected by changes at the end of
14604 the buffer. Value will be null if there is no unchanged row, in
14605 which case we must redisplay to the end of the window. delta
14606 will be set to the value by which buffer positions beginning with
14607 first_unchanged_at_end_row have to be adjusted due to text
14608 changes. */
14609 first_unchanged_at_end_row
14610 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14611 IF_DEBUG (debug_delta = delta);
14612 IF_DEBUG (debug_delta_bytes = delta_bytes);
14614 /* Set stop_pos to the buffer position up to which we will have to
14615 display new lines. If first_unchanged_at_end_row != NULL, this
14616 is the buffer position of the start of the line displayed in that
14617 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14618 that we don't stop at a buffer position. */
14619 stop_pos = 0;
14620 if (first_unchanged_at_end_row)
14622 xassert (last_unchanged_at_beg_row == NULL
14623 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14625 /* If this is a continuation line, move forward to the next one
14626 that isn't. Changes in lines above affect this line.
14627 Caution: this may move first_unchanged_at_end_row to a row
14628 not displaying text. */
14629 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14630 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14631 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14632 < it.last_visible_y))
14633 ++first_unchanged_at_end_row;
14635 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14636 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14637 >= it.last_visible_y))
14638 first_unchanged_at_end_row = NULL;
14639 else
14641 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14642 + delta);
14643 first_unchanged_at_end_vpos
14644 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14645 xassert (stop_pos >= Z - END_UNCHANGED);
14648 else if (last_unchanged_at_beg_row == NULL)
14649 GIVE_UP (19);
14652 #if GLYPH_DEBUG
14654 /* Either there is no unchanged row at the end, or the one we have
14655 now displays text. This is a necessary condition for the window
14656 end pos calculation at the end of this function. */
14657 xassert (first_unchanged_at_end_row == NULL
14658 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14660 debug_last_unchanged_at_beg_vpos
14661 = (last_unchanged_at_beg_row
14662 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14663 : -1);
14664 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14666 #endif /* GLYPH_DEBUG != 0 */
14669 /* Display new lines. Set last_text_row to the last new line
14670 displayed which has text on it, i.e. might end up as being the
14671 line where the window_end_vpos is. */
14672 w->cursor.vpos = -1;
14673 last_text_row = NULL;
14674 overlay_arrow_seen = 0;
14675 while (it.current_y < it.last_visible_y
14676 && !fonts_changed_p
14677 && (first_unchanged_at_end_row == NULL
14678 || IT_CHARPOS (it) < stop_pos))
14680 if (display_line (&it))
14681 last_text_row = it.glyph_row - 1;
14684 if (fonts_changed_p)
14685 return -1;
14688 /* Compute differences in buffer positions, y-positions etc. for
14689 lines reused at the bottom of the window. Compute what we can
14690 scroll. */
14691 if (first_unchanged_at_end_row
14692 /* No lines reused because we displayed everything up to the
14693 bottom of the window. */
14694 && it.current_y < it.last_visible_y)
14696 dvpos = (it.vpos
14697 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14698 current_matrix));
14699 dy = it.current_y - first_unchanged_at_end_row->y;
14700 run.current_y = first_unchanged_at_end_row->y;
14701 run.desired_y = run.current_y + dy;
14702 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14704 else
14706 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14707 first_unchanged_at_end_row = NULL;
14709 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14712 /* Find the cursor if not already found. We have to decide whether
14713 PT will appear on this window (it sometimes doesn't, but this is
14714 not a very frequent case.) This decision has to be made before
14715 the current matrix is altered. A value of cursor.vpos < 0 means
14716 that PT is either in one of the lines beginning at
14717 first_unchanged_at_end_row or below the window. Don't care for
14718 lines that might be displayed later at the window end; as
14719 mentioned, this is not a frequent case. */
14720 if (w->cursor.vpos < 0)
14722 /* Cursor in unchanged rows at the top? */
14723 if (PT < CHARPOS (start_pos)
14724 && last_unchanged_at_beg_row)
14726 row = row_containing_pos (w, PT,
14727 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14728 last_unchanged_at_beg_row + 1, 0);
14729 if (row)
14730 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14733 /* Start from first_unchanged_at_end_row looking for PT. */
14734 else if (first_unchanged_at_end_row)
14736 row = row_containing_pos (w, PT - delta,
14737 first_unchanged_at_end_row, NULL, 0);
14738 if (row)
14739 set_cursor_from_row (w, row, w->current_matrix, delta,
14740 delta_bytes, dy, dvpos);
14743 /* Give up if cursor was not found. */
14744 if (w->cursor.vpos < 0)
14746 clear_glyph_matrix (w->desired_matrix);
14747 return -1;
14751 /* Don't let the cursor end in the scroll margins. */
14753 int this_scroll_margin, cursor_height;
14755 this_scroll_margin = max (0, scroll_margin);
14756 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14757 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14758 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14760 if ((w->cursor.y < this_scroll_margin
14761 && CHARPOS (start) > BEGV)
14762 /* Old redisplay didn't take scroll margin into account at the bottom,
14763 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14764 || (w->cursor.y + (make_cursor_line_fully_visible_p
14765 ? cursor_height + this_scroll_margin
14766 : 1)) > it.last_visible_y)
14768 w->cursor.vpos = -1;
14769 clear_glyph_matrix (w->desired_matrix);
14770 return -1;
14774 /* Scroll the display. Do it before changing the current matrix so
14775 that xterm.c doesn't get confused about where the cursor glyph is
14776 found. */
14777 if (dy && run.height)
14779 update_begin (f);
14781 if (FRAME_WINDOW_P (f))
14783 rif->update_window_begin_hook (w);
14784 rif->clear_window_mouse_face (w);
14785 rif->scroll_run_hook (w, &run);
14786 rif->update_window_end_hook (w, 0, 0);
14788 else
14790 /* Terminal frame. In this case, dvpos gives the number of
14791 lines to scroll by; dvpos < 0 means scroll up. */
14792 int first_unchanged_at_end_vpos
14793 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14794 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14795 int end = (WINDOW_TOP_EDGE_LINE (w)
14796 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14797 + window_internal_height (w));
14799 /* Perform the operation on the screen. */
14800 if (dvpos > 0)
14802 /* Scroll last_unchanged_at_beg_row to the end of the
14803 window down dvpos lines. */
14804 set_terminal_window (end);
14806 /* On dumb terminals delete dvpos lines at the end
14807 before inserting dvpos empty lines. */
14808 if (!scroll_region_ok)
14809 ins_del_lines (end - dvpos, -dvpos);
14811 /* Insert dvpos empty lines in front of
14812 last_unchanged_at_beg_row. */
14813 ins_del_lines (from, dvpos);
14815 else if (dvpos < 0)
14817 /* Scroll up last_unchanged_at_beg_vpos to the end of
14818 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14819 set_terminal_window (end);
14821 /* Delete dvpos lines in front of
14822 last_unchanged_at_beg_vpos. ins_del_lines will set
14823 the cursor to the given vpos and emit |dvpos| delete
14824 line sequences. */
14825 ins_del_lines (from + dvpos, dvpos);
14827 /* On a dumb terminal insert dvpos empty lines at the
14828 end. */
14829 if (!scroll_region_ok)
14830 ins_del_lines (end + dvpos, -dvpos);
14833 set_terminal_window (0);
14836 update_end (f);
14839 /* Shift reused rows of the current matrix to the right position.
14840 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14841 text. */
14842 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14843 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14844 if (dvpos < 0)
14846 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14847 bottom_vpos, dvpos);
14848 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14849 bottom_vpos, 0);
14851 else if (dvpos > 0)
14853 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14854 bottom_vpos, dvpos);
14855 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14856 first_unchanged_at_end_vpos + dvpos, 0);
14859 /* For frame-based redisplay, make sure that current frame and window
14860 matrix are in sync with respect to glyph memory. */
14861 if (!FRAME_WINDOW_P (f))
14862 sync_frame_with_window_matrix_rows (w);
14864 /* Adjust buffer positions in reused rows. */
14865 if (delta)
14866 increment_matrix_positions (current_matrix,
14867 first_unchanged_at_end_vpos + dvpos,
14868 bottom_vpos, delta, delta_bytes);
14870 /* Adjust Y positions. */
14871 if (dy)
14872 shift_glyph_matrix (w, current_matrix,
14873 first_unchanged_at_end_vpos + dvpos,
14874 bottom_vpos, dy);
14876 if (first_unchanged_at_end_row)
14878 first_unchanged_at_end_row += dvpos;
14879 if (first_unchanged_at_end_row->y >= it.last_visible_y
14880 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14881 first_unchanged_at_end_row = NULL;
14884 /* If scrolling up, there may be some lines to display at the end of
14885 the window. */
14886 last_text_row_at_end = NULL;
14887 if (dy < 0)
14889 /* Scrolling up can leave for example a partially visible line
14890 at the end of the window to be redisplayed. */
14891 /* Set last_row to the glyph row in the current matrix where the
14892 window end line is found. It has been moved up or down in
14893 the matrix by dvpos. */
14894 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14895 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14897 /* If last_row is the window end line, it should display text. */
14898 xassert (last_row->displays_text_p);
14900 /* If window end line was partially visible before, begin
14901 displaying at that line. Otherwise begin displaying with the
14902 line following it. */
14903 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14905 init_to_row_start (&it, w, last_row);
14906 it.vpos = last_vpos;
14907 it.current_y = last_row->y;
14909 else
14911 init_to_row_end (&it, w, last_row);
14912 it.vpos = 1 + last_vpos;
14913 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14914 ++last_row;
14917 /* We may start in a continuation line. If so, we have to
14918 get the right continuation_lines_width and current_x. */
14919 it.continuation_lines_width = last_row->continuation_lines_width;
14920 it.hpos = it.current_x = 0;
14922 /* Display the rest of the lines at the window end. */
14923 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14924 while (it.current_y < it.last_visible_y
14925 && !fonts_changed_p)
14927 /* Is it always sure that the display agrees with lines in
14928 the current matrix? I don't think so, so we mark rows
14929 displayed invalid in the current matrix by setting their
14930 enabled_p flag to zero. */
14931 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14932 if (display_line (&it))
14933 last_text_row_at_end = it.glyph_row - 1;
14937 /* Update window_end_pos and window_end_vpos. */
14938 if (first_unchanged_at_end_row
14939 && !last_text_row_at_end)
14941 /* Window end line if one of the preserved rows from the current
14942 matrix. Set row to the last row displaying text in current
14943 matrix starting at first_unchanged_at_end_row, after
14944 scrolling. */
14945 xassert (first_unchanged_at_end_row->displays_text_p);
14946 row = find_last_row_displaying_text (w->current_matrix, &it,
14947 first_unchanged_at_end_row);
14948 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14950 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14951 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14952 w->window_end_vpos
14953 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14954 xassert (w->window_end_bytepos >= 0);
14955 IF_DEBUG (debug_method_add (w, "A"));
14957 else if (last_text_row_at_end)
14959 w->window_end_pos
14960 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14961 w->window_end_bytepos
14962 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14963 w->window_end_vpos
14964 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14965 xassert (w->window_end_bytepos >= 0);
14966 IF_DEBUG (debug_method_add (w, "B"));
14968 else if (last_text_row)
14970 /* We have displayed either to the end of the window or at the
14971 end of the window, i.e. the last row with text is to be found
14972 in the desired matrix. */
14973 w->window_end_pos
14974 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14975 w->window_end_bytepos
14976 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14977 w->window_end_vpos
14978 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14979 xassert (w->window_end_bytepos >= 0);
14981 else if (first_unchanged_at_end_row == NULL
14982 && last_text_row == NULL
14983 && last_text_row_at_end == NULL)
14985 /* Displayed to end of window, but no line containing text was
14986 displayed. Lines were deleted at the end of the window. */
14987 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14988 int vpos = XFASTINT (w->window_end_vpos);
14989 struct glyph_row *current_row = current_matrix->rows + vpos;
14990 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14992 for (row = NULL;
14993 row == NULL && vpos >= first_vpos;
14994 --vpos, --current_row, --desired_row)
14996 if (desired_row->enabled_p)
14998 if (desired_row->displays_text_p)
14999 row = desired_row;
15001 else if (current_row->displays_text_p)
15002 row = current_row;
15005 xassert (row != NULL);
15006 w->window_end_vpos = make_number (vpos + 1);
15007 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15008 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15009 xassert (w->window_end_bytepos >= 0);
15010 IF_DEBUG (debug_method_add (w, "C"));
15012 else
15013 abort ();
15015 #if 0 /* This leads to problems, for instance when the cursor is
15016 at ZV, and the cursor line displays no text. */
15017 /* Disable rows below what's displayed in the window. This makes
15018 debugging easier. */
15019 enable_glyph_matrix_rows (current_matrix,
15020 XFASTINT (w->window_end_vpos) + 1,
15021 bottom_vpos, 0);
15022 #endif
15024 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15025 debug_end_vpos = XFASTINT (w->window_end_vpos));
15027 /* Record that display has not been completed. */
15028 w->window_end_valid = Qnil;
15029 w->desired_matrix->no_scrolling_p = 1;
15030 return 3;
15032 #undef GIVE_UP
15037 /***********************************************************************
15038 More debugging support
15039 ***********************************************************************/
15041 #if GLYPH_DEBUG
15043 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15044 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15045 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15048 /* Dump the contents of glyph matrix MATRIX on stderr.
15050 GLYPHS 0 means don't show glyph contents.
15051 GLYPHS 1 means show glyphs in short form
15052 GLYPHS > 1 means show glyphs in long form. */
15054 void
15055 dump_glyph_matrix (matrix, glyphs)
15056 struct glyph_matrix *matrix;
15057 int glyphs;
15059 int i;
15060 for (i = 0; i < matrix->nrows; ++i)
15061 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15065 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15066 the glyph row and area where the glyph comes from. */
15068 void
15069 dump_glyph (row, glyph, area)
15070 struct glyph_row *row;
15071 struct glyph *glyph;
15072 int area;
15074 if (glyph->type == CHAR_GLYPH)
15076 fprintf (stderr,
15077 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15078 glyph - row->glyphs[TEXT_AREA],
15079 'C',
15080 glyph->charpos,
15081 (BUFFERP (glyph->object)
15082 ? 'B'
15083 : (STRINGP (glyph->object)
15084 ? 'S'
15085 : '-')),
15086 glyph->pixel_width,
15087 glyph->u.ch,
15088 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15089 ? glyph->u.ch
15090 : '.'),
15091 glyph->face_id,
15092 glyph->left_box_line_p,
15093 glyph->right_box_line_p);
15095 else if (glyph->type == STRETCH_GLYPH)
15097 fprintf (stderr,
15098 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15099 glyph - row->glyphs[TEXT_AREA],
15100 'S',
15101 glyph->charpos,
15102 (BUFFERP (glyph->object)
15103 ? 'B'
15104 : (STRINGP (glyph->object)
15105 ? 'S'
15106 : '-')),
15107 glyph->pixel_width,
15109 '.',
15110 glyph->face_id,
15111 glyph->left_box_line_p,
15112 glyph->right_box_line_p);
15114 else if (glyph->type == IMAGE_GLYPH)
15116 fprintf (stderr,
15117 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15118 glyph - row->glyphs[TEXT_AREA],
15119 'I',
15120 glyph->charpos,
15121 (BUFFERP (glyph->object)
15122 ? 'B'
15123 : (STRINGP (glyph->object)
15124 ? 'S'
15125 : '-')),
15126 glyph->pixel_width,
15127 glyph->u.img_id,
15128 '.',
15129 glyph->face_id,
15130 glyph->left_box_line_p,
15131 glyph->right_box_line_p);
15133 else if (glyph->type == COMPOSITE_GLYPH)
15135 fprintf (stderr,
15136 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15137 glyph - row->glyphs[TEXT_AREA],
15138 '+',
15139 glyph->charpos,
15140 (BUFFERP (glyph->object)
15141 ? 'B'
15142 : (STRINGP (glyph->object)
15143 ? 'S'
15144 : '-')),
15145 glyph->pixel_width,
15146 glyph->u.cmp_id,
15147 '.',
15148 glyph->face_id,
15149 glyph->left_box_line_p,
15150 glyph->right_box_line_p);
15155 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15156 GLYPHS 0 means don't show glyph contents.
15157 GLYPHS 1 means show glyphs in short form
15158 GLYPHS > 1 means show glyphs in long form. */
15160 void
15161 dump_glyph_row (row, vpos, glyphs)
15162 struct glyph_row *row;
15163 int vpos, glyphs;
15165 if (glyphs != 1)
15167 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15168 fprintf (stderr, "======================================================================\n");
15170 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15171 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15172 vpos,
15173 MATRIX_ROW_START_CHARPOS (row),
15174 MATRIX_ROW_END_CHARPOS (row),
15175 row->used[TEXT_AREA],
15176 row->contains_overlapping_glyphs_p,
15177 row->enabled_p,
15178 row->truncated_on_left_p,
15179 row->truncated_on_right_p,
15180 row->continued_p,
15181 MATRIX_ROW_CONTINUATION_LINE_P (row),
15182 row->displays_text_p,
15183 row->ends_at_zv_p,
15184 row->fill_line_p,
15185 row->ends_in_middle_of_char_p,
15186 row->starts_in_middle_of_char_p,
15187 row->mouse_face_p,
15188 row->x,
15189 row->y,
15190 row->pixel_width,
15191 row->height,
15192 row->visible_height,
15193 row->ascent,
15194 row->phys_ascent);
15195 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15196 row->end.overlay_string_index,
15197 row->continuation_lines_width);
15198 fprintf (stderr, "%9d %5d\n",
15199 CHARPOS (row->start.string_pos),
15200 CHARPOS (row->end.string_pos));
15201 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15202 row->end.dpvec_index);
15205 if (glyphs > 1)
15207 int area;
15209 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15211 struct glyph *glyph = row->glyphs[area];
15212 struct glyph *glyph_end = glyph + row->used[area];
15214 /* Glyph for a line end in text. */
15215 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15216 ++glyph_end;
15218 if (glyph < glyph_end)
15219 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15221 for (; glyph < glyph_end; ++glyph)
15222 dump_glyph (row, glyph, area);
15225 else if (glyphs == 1)
15227 int area;
15229 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15231 char *s = (char *) alloca (row->used[area] + 1);
15232 int i;
15234 for (i = 0; i < row->used[area]; ++i)
15236 struct glyph *glyph = row->glyphs[area] + i;
15237 if (glyph->type == CHAR_GLYPH
15238 && glyph->u.ch < 0x80
15239 && glyph->u.ch >= ' ')
15240 s[i] = glyph->u.ch;
15241 else
15242 s[i] = '.';
15245 s[i] = '\0';
15246 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15252 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15253 Sdump_glyph_matrix, 0, 1, "p",
15254 doc: /* Dump the current matrix of the selected window to stderr.
15255 Shows contents of glyph row structures. With non-nil
15256 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15257 glyphs in short form, otherwise show glyphs in long form. */)
15258 (glyphs)
15259 Lisp_Object glyphs;
15261 struct window *w = XWINDOW (selected_window);
15262 struct buffer *buffer = XBUFFER (w->buffer);
15264 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15265 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15266 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15267 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15268 fprintf (stderr, "=============================================\n");
15269 dump_glyph_matrix (w->current_matrix,
15270 NILP (glyphs) ? 0 : XINT (glyphs));
15271 return Qnil;
15275 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15276 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15279 struct frame *f = XFRAME (selected_frame);
15280 dump_glyph_matrix (f->current_matrix, 1);
15281 return Qnil;
15285 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15286 doc: /* Dump glyph row ROW to stderr.
15287 GLYPH 0 means don't dump glyphs.
15288 GLYPH 1 means dump glyphs in short form.
15289 GLYPH > 1 or omitted means dump glyphs in long form. */)
15290 (row, glyphs)
15291 Lisp_Object row, glyphs;
15293 struct glyph_matrix *matrix;
15294 int vpos;
15296 CHECK_NUMBER (row);
15297 matrix = XWINDOW (selected_window)->current_matrix;
15298 vpos = XINT (row);
15299 if (vpos >= 0 && vpos < matrix->nrows)
15300 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15301 vpos,
15302 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15303 return Qnil;
15307 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15308 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15309 GLYPH 0 means don't dump glyphs.
15310 GLYPH 1 means dump glyphs in short form.
15311 GLYPH > 1 or omitted means dump glyphs in long form. */)
15312 (row, glyphs)
15313 Lisp_Object row, glyphs;
15315 struct frame *sf = SELECTED_FRAME ();
15316 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15317 int vpos;
15319 CHECK_NUMBER (row);
15320 vpos = XINT (row);
15321 if (vpos >= 0 && vpos < m->nrows)
15322 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15323 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15324 return Qnil;
15328 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15329 doc: /* Toggle tracing of redisplay.
15330 With ARG, turn tracing on if and only if ARG is positive. */)
15331 (arg)
15332 Lisp_Object arg;
15334 if (NILP (arg))
15335 trace_redisplay_p = !trace_redisplay_p;
15336 else
15338 arg = Fprefix_numeric_value (arg);
15339 trace_redisplay_p = XINT (arg) > 0;
15342 return Qnil;
15346 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15347 doc: /* Like `format', but print result to stderr.
15348 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15349 (nargs, args)
15350 int nargs;
15351 Lisp_Object *args;
15353 Lisp_Object s = Fformat (nargs, args);
15354 fprintf (stderr, "%s", SDATA (s));
15355 return Qnil;
15358 #endif /* GLYPH_DEBUG */
15362 /***********************************************************************
15363 Building Desired Matrix Rows
15364 ***********************************************************************/
15366 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15367 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15369 static struct glyph_row *
15370 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15371 struct window *w;
15372 Lisp_Object overlay_arrow_string;
15374 struct frame *f = XFRAME (WINDOW_FRAME (w));
15375 struct buffer *buffer = XBUFFER (w->buffer);
15376 struct buffer *old = current_buffer;
15377 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15378 int arrow_len = SCHARS (overlay_arrow_string);
15379 const unsigned char *arrow_end = arrow_string + arrow_len;
15380 const unsigned char *p;
15381 struct it it;
15382 int multibyte_p;
15383 int n_glyphs_before;
15385 set_buffer_temp (buffer);
15386 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15387 it.glyph_row->used[TEXT_AREA] = 0;
15388 SET_TEXT_POS (it.position, 0, 0);
15390 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15391 p = arrow_string;
15392 while (p < arrow_end)
15394 Lisp_Object face, ilisp;
15396 /* Get the next character. */
15397 if (multibyte_p)
15398 it.c = string_char_and_length (p, arrow_len, &it.len);
15399 else
15400 it.c = *p, it.len = 1;
15401 p += it.len;
15403 /* Get its face. */
15404 ilisp = make_number (p - arrow_string);
15405 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15406 it.face_id = compute_char_face (f, it.c, face);
15408 /* Compute its width, get its glyphs. */
15409 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15410 SET_TEXT_POS (it.position, -1, -1);
15411 PRODUCE_GLYPHS (&it);
15413 /* If this character doesn't fit any more in the line, we have
15414 to remove some glyphs. */
15415 if (it.current_x > it.last_visible_x)
15417 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15418 break;
15422 set_buffer_temp (old);
15423 return it.glyph_row;
15427 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15428 glyphs are only inserted for terminal frames since we can't really
15429 win with truncation glyphs when partially visible glyphs are
15430 involved. Which glyphs to insert is determined by
15431 produce_special_glyphs. */
15433 static void
15434 insert_left_trunc_glyphs (it)
15435 struct it *it;
15437 struct it truncate_it;
15438 struct glyph *from, *end, *to, *toend;
15440 xassert (!FRAME_WINDOW_P (it->f));
15442 /* Get the truncation glyphs. */
15443 truncate_it = *it;
15444 truncate_it.current_x = 0;
15445 truncate_it.face_id = DEFAULT_FACE_ID;
15446 truncate_it.glyph_row = &scratch_glyph_row;
15447 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15448 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15449 truncate_it.object = make_number (0);
15450 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15452 /* Overwrite glyphs from IT with truncation glyphs. */
15453 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15454 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15455 to = it->glyph_row->glyphs[TEXT_AREA];
15456 toend = to + it->glyph_row->used[TEXT_AREA];
15458 while (from < end)
15459 *to++ = *from++;
15461 /* There may be padding glyphs left over. Overwrite them too. */
15462 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15464 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15465 while (from < end)
15466 *to++ = *from++;
15469 if (to > toend)
15470 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15474 /* Compute the pixel height and width of IT->glyph_row.
15476 Most of the time, ascent and height of a display line will be equal
15477 to the max_ascent and max_height values of the display iterator
15478 structure. This is not the case if
15480 1. We hit ZV without displaying anything. In this case, max_ascent
15481 and max_height will be zero.
15483 2. We have some glyphs that don't contribute to the line height.
15484 (The glyph row flag contributes_to_line_height_p is for future
15485 pixmap extensions).
15487 The first case is easily covered by using default values because in
15488 these cases, the line height does not really matter, except that it
15489 must not be zero. */
15491 static void
15492 compute_line_metrics (it)
15493 struct it *it;
15495 struct glyph_row *row = it->glyph_row;
15496 int area, i;
15498 if (FRAME_WINDOW_P (it->f))
15500 int i, min_y, max_y;
15502 /* The line may consist of one space only, that was added to
15503 place the cursor on it. If so, the row's height hasn't been
15504 computed yet. */
15505 if (row->height == 0)
15507 if (it->max_ascent + it->max_descent == 0)
15508 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15509 row->ascent = it->max_ascent;
15510 row->height = it->max_ascent + it->max_descent;
15511 row->phys_ascent = it->max_phys_ascent;
15512 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15513 row->extra_line_spacing = it->max_extra_line_spacing;
15516 /* Compute the width of this line. */
15517 row->pixel_width = row->x;
15518 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15519 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15521 xassert (row->pixel_width >= 0);
15522 xassert (row->ascent >= 0 && row->height > 0);
15524 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15525 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15527 /* If first line's physical ascent is larger than its logical
15528 ascent, use the physical ascent, and make the row taller.
15529 This makes accented characters fully visible. */
15530 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15531 && row->phys_ascent > row->ascent)
15533 row->height += row->phys_ascent - row->ascent;
15534 row->ascent = row->phys_ascent;
15537 /* Compute how much of the line is visible. */
15538 row->visible_height = row->height;
15540 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15541 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15543 if (row->y < min_y)
15544 row->visible_height -= min_y - row->y;
15545 if (row->y + row->height > max_y)
15546 row->visible_height -= row->y + row->height - max_y;
15548 else
15550 row->pixel_width = row->used[TEXT_AREA];
15551 if (row->continued_p)
15552 row->pixel_width -= it->continuation_pixel_width;
15553 else if (row->truncated_on_right_p)
15554 row->pixel_width -= it->truncation_pixel_width;
15555 row->ascent = row->phys_ascent = 0;
15556 row->height = row->phys_height = row->visible_height = 1;
15557 row->extra_line_spacing = 0;
15560 /* Compute a hash code for this row. */
15561 row->hash = 0;
15562 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15563 for (i = 0; i < row->used[area]; ++i)
15564 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15565 + row->glyphs[area][i].u.val
15566 + row->glyphs[area][i].face_id
15567 + row->glyphs[area][i].padding_p
15568 + (row->glyphs[area][i].type << 2));
15570 it->max_ascent = it->max_descent = 0;
15571 it->max_phys_ascent = it->max_phys_descent = 0;
15575 /* Append one space to the glyph row of iterator IT if doing a
15576 window-based redisplay. The space has the same face as
15577 IT->face_id. Value is non-zero if a space was added.
15579 This function is called to make sure that there is always one glyph
15580 at the end of a glyph row that the cursor can be set on under
15581 window-systems. (If there weren't such a glyph we would not know
15582 how wide and tall a box cursor should be displayed).
15584 At the same time this space let's a nicely handle clearing to the
15585 end of the line if the row ends in italic text. */
15587 static int
15588 append_space_for_newline (it, default_face_p)
15589 struct it *it;
15590 int default_face_p;
15592 if (FRAME_WINDOW_P (it->f))
15594 int n = it->glyph_row->used[TEXT_AREA];
15596 if (it->glyph_row->glyphs[TEXT_AREA] + n
15597 < it->glyph_row->glyphs[1 + TEXT_AREA])
15599 /* Save some values that must not be changed.
15600 Must save IT->c and IT->len because otherwise
15601 ITERATOR_AT_END_P wouldn't work anymore after
15602 append_space_for_newline has been called. */
15603 enum display_element_type saved_what = it->what;
15604 int saved_c = it->c, saved_len = it->len;
15605 int saved_x = it->current_x;
15606 int saved_face_id = it->face_id;
15607 struct text_pos saved_pos;
15608 Lisp_Object saved_object;
15609 struct face *face;
15611 saved_object = it->object;
15612 saved_pos = it->position;
15614 it->what = IT_CHARACTER;
15615 bzero (&it->position, sizeof it->position);
15616 it->object = make_number (0);
15617 it->c = ' ';
15618 it->len = 1;
15620 if (default_face_p)
15621 it->face_id = DEFAULT_FACE_ID;
15622 else if (it->face_before_selective_p)
15623 it->face_id = it->saved_face_id;
15624 face = FACE_FROM_ID (it->f, it->face_id);
15625 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15627 PRODUCE_GLYPHS (it);
15629 it->override_ascent = -1;
15630 it->constrain_row_ascent_descent_p = 0;
15631 it->current_x = saved_x;
15632 it->object = saved_object;
15633 it->position = saved_pos;
15634 it->what = saved_what;
15635 it->face_id = saved_face_id;
15636 it->len = saved_len;
15637 it->c = saved_c;
15638 return 1;
15642 return 0;
15646 /* Extend the face of the last glyph in the text area of IT->glyph_row
15647 to the end of the display line. Called from display_line.
15648 If the glyph row is empty, add a space glyph to it so that we
15649 know the face to draw. Set the glyph row flag fill_line_p. */
15651 static void
15652 extend_face_to_end_of_line (it)
15653 struct it *it;
15655 struct face *face;
15656 struct frame *f = it->f;
15658 /* If line is already filled, do nothing. */
15659 if (it->current_x >= it->last_visible_x)
15660 return;
15662 /* Face extension extends the background and box of IT->face_id
15663 to the end of the line. If the background equals the background
15664 of the frame, we don't have to do anything. */
15665 if (it->face_before_selective_p)
15666 face = FACE_FROM_ID (it->f, it->saved_face_id);
15667 else
15668 face = FACE_FROM_ID (f, it->face_id);
15670 if (FRAME_WINDOW_P (f)
15671 && it->glyph_row->displays_text_p
15672 && face->box == FACE_NO_BOX
15673 && face->background == FRAME_BACKGROUND_PIXEL (f)
15674 && !face->stipple)
15675 return;
15677 /* Set the glyph row flag indicating that the face of the last glyph
15678 in the text area has to be drawn to the end of the text area. */
15679 it->glyph_row->fill_line_p = 1;
15681 /* If current character of IT is not ASCII, make sure we have the
15682 ASCII face. This will be automatically undone the next time
15683 get_next_display_element returns a multibyte character. Note
15684 that the character will always be single byte in unibyte text. */
15685 if (!SINGLE_BYTE_CHAR_P (it->c))
15687 it->face_id = FACE_FOR_CHAR (f, face, 0);
15690 if (FRAME_WINDOW_P (f))
15692 /* If the row is empty, add a space with the current face of IT,
15693 so that we know which face to draw. */
15694 if (it->glyph_row->used[TEXT_AREA] == 0)
15696 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15697 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15698 it->glyph_row->used[TEXT_AREA] = 1;
15701 else
15703 /* Save some values that must not be changed. */
15704 int saved_x = it->current_x;
15705 struct text_pos saved_pos;
15706 Lisp_Object saved_object;
15707 enum display_element_type saved_what = it->what;
15708 int saved_face_id = it->face_id;
15710 saved_object = it->object;
15711 saved_pos = it->position;
15713 it->what = IT_CHARACTER;
15714 bzero (&it->position, sizeof it->position);
15715 it->object = make_number (0);
15716 it->c = ' ';
15717 it->len = 1;
15718 it->face_id = face->id;
15720 PRODUCE_GLYPHS (it);
15722 while (it->current_x <= it->last_visible_x)
15723 PRODUCE_GLYPHS (it);
15725 /* Don't count these blanks really. It would let us insert a left
15726 truncation glyph below and make us set the cursor on them, maybe. */
15727 it->current_x = saved_x;
15728 it->object = saved_object;
15729 it->position = saved_pos;
15730 it->what = saved_what;
15731 it->face_id = saved_face_id;
15736 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15737 trailing whitespace. */
15739 static int
15740 trailing_whitespace_p (charpos)
15741 int charpos;
15743 int bytepos = CHAR_TO_BYTE (charpos);
15744 int c = 0;
15746 while (bytepos < ZV_BYTE
15747 && (c = FETCH_CHAR (bytepos),
15748 c == ' ' || c == '\t'))
15749 ++bytepos;
15751 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15753 if (bytepos != PT_BYTE)
15754 return 1;
15756 return 0;
15760 /* Highlight trailing whitespace, if any, in ROW. */
15762 void
15763 highlight_trailing_whitespace (f, row)
15764 struct frame *f;
15765 struct glyph_row *row;
15767 int used = row->used[TEXT_AREA];
15769 if (used)
15771 struct glyph *start = row->glyphs[TEXT_AREA];
15772 struct glyph *glyph = start + used - 1;
15774 /* Skip over glyphs inserted to display the cursor at the
15775 end of a line, for extending the face of the last glyph
15776 to the end of the line on terminals, and for truncation
15777 and continuation glyphs. */
15778 while (glyph >= start
15779 && glyph->type == CHAR_GLYPH
15780 && INTEGERP (glyph->object))
15781 --glyph;
15783 /* If last glyph is a space or stretch, and it's trailing
15784 whitespace, set the face of all trailing whitespace glyphs in
15785 IT->glyph_row to `trailing-whitespace'. */
15786 if (glyph >= start
15787 && BUFFERP (glyph->object)
15788 && (glyph->type == STRETCH_GLYPH
15789 || (glyph->type == CHAR_GLYPH
15790 && glyph->u.ch == ' '))
15791 && trailing_whitespace_p (glyph->charpos))
15793 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15794 if (face_id < 0)
15795 return;
15797 while (glyph >= start
15798 && BUFFERP (glyph->object)
15799 && (glyph->type == STRETCH_GLYPH
15800 || (glyph->type == CHAR_GLYPH
15801 && glyph->u.ch == ' ')))
15802 (glyph--)->face_id = face_id;
15808 /* Value is non-zero if glyph row ROW in window W should be
15809 used to hold the cursor. */
15811 static int
15812 cursor_row_p (w, row)
15813 struct window *w;
15814 struct glyph_row *row;
15816 int cursor_row_p = 1;
15818 if (PT == MATRIX_ROW_END_CHARPOS (row))
15820 /* If the row ends with a newline from a string, we don't want
15821 the cursor there, but we still want it at the start of the
15822 string if the string starts in this row.
15823 If the row is continued it doesn't end in a newline. */
15824 if (CHARPOS (row->end.string_pos) >= 0)
15825 cursor_row_p = (row->continued_p
15826 || PT >= MATRIX_ROW_START_CHARPOS (row));
15827 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15829 /* If the row ends in middle of a real character,
15830 and the line is continued, we want the cursor here.
15831 That's because MATRIX_ROW_END_CHARPOS would equal
15832 PT if PT is before the character. */
15833 if (!row->ends_in_ellipsis_p)
15834 cursor_row_p = row->continued_p;
15835 else
15836 /* If the row ends in an ellipsis, then
15837 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15838 We want that position to be displayed after the ellipsis. */
15839 cursor_row_p = 0;
15841 /* If the row ends at ZV, display the cursor at the end of that
15842 row instead of at the start of the row below. */
15843 else if (row->ends_at_zv_p)
15844 cursor_row_p = 1;
15845 else
15846 cursor_row_p = 0;
15849 return cursor_row_p;
15853 /* Construct the glyph row IT->glyph_row in the desired matrix of
15854 IT->w from text at the current position of IT. See dispextern.h
15855 for an overview of struct it. Value is non-zero if
15856 IT->glyph_row displays text, as opposed to a line displaying ZV
15857 only. */
15859 static int
15860 display_line (it)
15861 struct it *it;
15863 struct glyph_row *row = it->glyph_row;
15864 Lisp_Object overlay_arrow_string;
15866 /* We always start displaying at hpos zero even if hscrolled. */
15867 xassert (it->hpos == 0 && it->current_x == 0);
15869 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15870 >= it->w->desired_matrix->nrows)
15872 it->w->nrows_scale_factor++;
15873 fonts_changed_p = 1;
15874 return 0;
15877 /* Is IT->w showing the region? */
15878 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15880 /* Clear the result glyph row and enable it. */
15881 prepare_desired_row (row);
15883 row->y = it->current_y;
15884 row->start = it->start;
15885 row->continuation_lines_width = it->continuation_lines_width;
15886 row->displays_text_p = 1;
15887 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15888 it->starts_in_middle_of_char_p = 0;
15890 /* Arrange the overlays nicely for our purposes. Usually, we call
15891 display_line on only one line at a time, in which case this
15892 can't really hurt too much, or we call it on lines which appear
15893 one after another in the buffer, in which case all calls to
15894 recenter_overlay_lists but the first will be pretty cheap. */
15895 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15897 /* Move over display elements that are not visible because we are
15898 hscrolled. This may stop at an x-position < IT->first_visible_x
15899 if the first glyph is partially visible or if we hit a line end. */
15900 if (it->current_x < it->first_visible_x)
15902 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15903 MOVE_TO_POS | MOVE_TO_X);
15906 /* Get the initial row height. This is either the height of the
15907 text hscrolled, if there is any, or zero. */
15908 row->ascent = it->max_ascent;
15909 row->height = it->max_ascent + it->max_descent;
15910 row->phys_ascent = it->max_phys_ascent;
15911 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15912 row->extra_line_spacing = it->max_extra_line_spacing;
15914 /* Loop generating characters. The loop is left with IT on the next
15915 character to display. */
15916 while (1)
15918 int n_glyphs_before, hpos_before, x_before;
15919 int x, i, nglyphs;
15920 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15922 /* Retrieve the next thing to display. Value is zero if end of
15923 buffer reached. */
15924 if (!get_next_display_element (it))
15926 /* Maybe add a space at the end of this line that is used to
15927 display the cursor there under X. Set the charpos of the
15928 first glyph of blank lines not corresponding to any text
15929 to -1. */
15930 #ifdef HAVE_WINDOW_SYSTEM
15931 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15932 row->exact_window_width_line_p = 1;
15933 else
15934 #endif /* HAVE_WINDOW_SYSTEM */
15935 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15936 || row->used[TEXT_AREA] == 0)
15938 row->glyphs[TEXT_AREA]->charpos = -1;
15939 row->displays_text_p = 0;
15941 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15942 && (!MINI_WINDOW_P (it->w)
15943 || (minibuf_level && EQ (it->window, minibuf_window))))
15944 row->indicate_empty_line_p = 1;
15947 it->continuation_lines_width = 0;
15948 row->ends_at_zv_p = 1;
15949 break;
15952 /* Now, get the metrics of what we want to display. This also
15953 generates glyphs in `row' (which is IT->glyph_row). */
15954 n_glyphs_before = row->used[TEXT_AREA];
15955 x = it->current_x;
15957 /* Remember the line height so far in case the next element doesn't
15958 fit on the line. */
15959 if (!it->truncate_lines_p)
15961 ascent = it->max_ascent;
15962 descent = it->max_descent;
15963 phys_ascent = it->max_phys_ascent;
15964 phys_descent = it->max_phys_descent;
15967 PRODUCE_GLYPHS (it);
15969 /* If this display element was in marginal areas, continue with
15970 the next one. */
15971 if (it->area != TEXT_AREA)
15973 row->ascent = max (row->ascent, it->max_ascent);
15974 row->height = max (row->height, it->max_ascent + it->max_descent);
15975 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15976 row->phys_height = max (row->phys_height,
15977 it->max_phys_ascent + it->max_phys_descent);
15978 row->extra_line_spacing = max (row->extra_line_spacing,
15979 it->max_extra_line_spacing);
15980 set_iterator_to_next (it, 1);
15981 continue;
15984 /* Does the display element fit on the line? If we truncate
15985 lines, we should draw past the right edge of the window. If
15986 we don't truncate, we want to stop so that we can display the
15987 continuation glyph before the right margin. If lines are
15988 continued, there are two possible strategies for characters
15989 resulting in more than 1 glyph (e.g. tabs): Display as many
15990 glyphs as possible in this line and leave the rest for the
15991 continuation line, or display the whole element in the next
15992 line. Original redisplay did the former, so we do it also. */
15993 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15994 hpos_before = it->hpos;
15995 x_before = x;
15997 if (/* Not a newline. */
15998 nglyphs > 0
15999 /* Glyphs produced fit entirely in the line. */
16000 && it->current_x < it->last_visible_x)
16002 it->hpos += nglyphs;
16003 row->ascent = max (row->ascent, it->max_ascent);
16004 row->height = max (row->height, it->max_ascent + it->max_descent);
16005 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16006 row->phys_height = max (row->phys_height,
16007 it->max_phys_ascent + it->max_phys_descent);
16008 row->extra_line_spacing = max (row->extra_line_spacing,
16009 it->max_extra_line_spacing);
16010 if (it->current_x - it->pixel_width < it->first_visible_x)
16011 row->x = x - it->first_visible_x;
16013 else
16015 int new_x;
16016 struct glyph *glyph;
16018 for (i = 0; i < nglyphs; ++i, x = new_x)
16020 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16021 new_x = x + glyph->pixel_width;
16023 if (/* Lines are continued. */
16024 !it->truncate_lines_p
16025 && (/* Glyph doesn't fit on the line. */
16026 new_x > it->last_visible_x
16027 /* Or it fits exactly on a window system frame. */
16028 || (new_x == it->last_visible_x
16029 && FRAME_WINDOW_P (it->f))))
16031 /* End of a continued line. */
16033 if (it->hpos == 0
16034 || (new_x == it->last_visible_x
16035 && FRAME_WINDOW_P (it->f)))
16037 /* Current glyph is the only one on the line or
16038 fits exactly on the line. We must continue
16039 the line because we can't draw the cursor
16040 after the glyph. */
16041 row->continued_p = 1;
16042 it->current_x = new_x;
16043 it->continuation_lines_width += new_x;
16044 ++it->hpos;
16045 if (i == nglyphs - 1)
16047 set_iterator_to_next (it, 1);
16048 #ifdef HAVE_WINDOW_SYSTEM
16049 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16051 if (!get_next_display_element (it))
16053 row->exact_window_width_line_p = 1;
16054 it->continuation_lines_width = 0;
16055 row->continued_p = 0;
16056 row->ends_at_zv_p = 1;
16058 else if (ITERATOR_AT_END_OF_LINE_P (it))
16060 row->continued_p = 0;
16061 row->exact_window_width_line_p = 1;
16064 #endif /* HAVE_WINDOW_SYSTEM */
16067 else if (CHAR_GLYPH_PADDING_P (*glyph)
16068 && !FRAME_WINDOW_P (it->f))
16070 /* A padding glyph that doesn't fit on this line.
16071 This means the whole character doesn't fit
16072 on the line. */
16073 row->used[TEXT_AREA] = n_glyphs_before;
16075 /* Fill the rest of the row with continuation
16076 glyphs like in 20.x. */
16077 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16078 < row->glyphs[1 + TEXT_AREA])
16079 produce_special_glyphs (it, IT_CONTINUATION);
16081 row->continued_p = 1;
16082 it->current_x = x_before;
16083 it->continuation_lines_width += x_before;
16085 /* Restore the height to what it was before the
16086 element not fitting on the line. */
16087 it->max_ascent = ascent;
16088 it->max_descent = descent;
16089 it->max_phys_ascent = phys_ascent;
16090 it->max_phys_descent = phys_descent;
16092 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16094 /* A TAB that extends past the right edge of the
16095 window. This produces a single glyph on
16096 window system frames. We leave the glyph in
16097 this row and let it fill the row, but don't
16098 consume the TAB. */
16099 it->continuation_lines_width += it->last_visible_x;
16100 row->ends_in_middle_of_char_p = 1;
16101 row->continued_p = 1;
16102 glyph->pixel_width = it->last_visible_x - x;
16103 it->starts_in_middle_of_char_p = 1;
16105 else
16107 /* Something other than a TAB that draws past
16108 the right edge of the window. Restore
16109 positions to values before the element. */
16110 row->used[TEXT_AREA] = n_glyphs_before + i;
16112 /* Display continuation glyphs. */
16113 if (!FRAME_WINDOW_P (it->f))
16114 produce_special_glyphs (it, IT_CONTINUATION);
16115 row->continued_p = 1;
16117 it->current_x = x_before;
16118 it->continuation_lines_width += x;
16119 extend_face_to_end_of_line (it);
16121 if (nglyphs > 1 && i > 0)
16123 row->ends_in_middle_of_char_p = 1;
16124 it->starts_in_middle_of_char_p = 1;
16127 /* Restore the height to what it was before the
16128 element not fitting on the line. */
16129 it->max_ascent = ascent;
16130 it->max_descent = descent;
16131 it->max_phys_ascent = phys_ascent;
16132 it->max_phys_descent = phys_descent;
16135 break;
16137 else if (new_x > it->first_visible_x)
16139 /* Increment number of glyphs actually displayed. */
16140 ++it->hpos;
16142 if (x < it->first_visible_x)
16143 /* Glyph is partially visible, i.e. row starts at
16144 negative X position. */
16145 row->x = x - it->first_visible_x;
16147 else
16149 /* Glyph is completely off the left margin of the
16150 window. This should not happen because of the
16151 move_it_in_display_line at the start of this
16152 function, unless the text display area of the
16153 window is empty. */
16154 xassert (it->first_visible_x <= it->last_visible_x);
16158 row->ascent = max (row->ascent, it->max_ascent);
16159 row->height = max (row->height, it->max_ascent + it->max_descent);
16160 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16161 row->phys_height = max (row->phys_height,
16162 it->max_phys_ascent + it->max_phys_descent);
16163 row->extra_line_spacing = max (row->extra_line_spacing,
16164 it->max_extra_line_spacing);
16166 /* End of this display line if row is continued. */
16167 if (row->continued_p || row->ends_at_zv_p)
16168 break;
16171 at_end_of_line:
16172 /* Is this a line end? If yes, we're also done, after making
16173 sure that a non-default face is extended up to the right
16174 margin of the window. */
16175 if (ITERATOR_AT_END_OF_LINE_P (it))
16177 int used_before = row->used[TEXT_AREA];
16179 row->ends_in_newline_from_string_p = STRINGP (it->object);
16181 #ifdef HAVE_WINDOW_SYSTEM
16182 /* Add a space at the end of the line that is used to
16183 display the cursor there. */
16184 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16185 append_space_for_newline (it, 0);
16186 #endif /* HAVE_WINDOW_SYSTEM */
16188 /* Extend the face to the end of the line. */
16189 extend_face_to_end_of_line (it);
16191 /* Make sure we have the position. */
16192 if (used_before == 0)
16193 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16195 /* Consume the line end. This skips over invisible lines. */
16196 set_iterator_to_next (it, 1);
16197 it->continuation_lines_width = 0;
16198 break;
16201 /* Proceed with next display element. Note that this skips
16202 over lines invisible because of selective display. */
16203 set_iterator_to_next (it, 1);
16205 /* If we truncate lines, we are done when the last displayed
16206 glyphs reach past the right margin of the window. */
16207 if (it->truncate_lines_p
16208 && (FRAME_WINDOW_P (it->f)
16209 ? (it->current_x >= it->last_visible_x)
16210 : (it->current_x > it->last_visible_x)))
16212 /* Maybe add truncation glyphs. */
16213 if (!FRAME_WINDOW_P (it->f))
16215 int i, n;
16217 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16218 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16219 break;
16221 for (n = row->used[TEXT_AREA]; i < n; ++i)
16223 row->used[TEXT_AREA] = i;
16224 produce_special_glyphs (it, IT_TRUNCATION);
16227 #ifdef HAVE_WINDOW_SYSTEM
16228 else
16230 /* Don't truncate if we can overflow newline into fringe. */
16231 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16233 if (!get_next_display_element (it))
16235 it->continuation_lines_width = 0;
16236 row->ends_at_zv_p = 1;
16237 row->exact_window_width_line_p = 1;
16238 break;
16240 if (ITERATOR_AT_END_OF_LINE_P (it))
16242 row->exact_window_width_line_p = 1;
16243 goto at_end_of_line;
16247 #endif /* HAVE_WINDOW_SYSTEM */
16249 row->truncated_on_right_p = 1;
16250 it->continuation_lines_width = 0;
16251 reseat_at_next_visible_line_start (it, 0);
16252 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16253 it->hpos = hpos_before;
16254 it->current_x = x_before;
16255 break;
16259 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16260 at the left window margin. */
16261 if (it->first_visible_x
16262 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16264 if (!FRAME_WINDOW_P (it->f))
16265 insert_left_trunc_glyphs (it);
16266 row->truncated_on_left_p = 1;
16269 /* If the start of this line is the overlay arrow-position, then
16270 mark this glyph row as the one containing the overlay arrow.
16271 This is clearly a mess with variable size fonts. It would be
16272 better to let it be displayed like cursors under X. */
16273 if ((row->displays_text_p || !overlay_arrow_seen)
16274 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16275 !NILP (overlay_arrow_string)))
16277 /* Overlay arrow in window redisplay is a fringe bitmap. */
16278 if (STRINGP (overlay_arrow_string))
16280 struct glyph_row *arrow_row
16281 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16282 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16283 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16284 struct glyph *p = row->glyphs[TEXT_AREA];
16285 struct glyph *p2, *end;
16287 /* Copy the arrow glyphs. */
16288 while (glyph < arrow_end)
16289 *p++ = *glyph++;
16291 /* Throw away padding glyphs. */
16292 p2 = p;
16293 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16294 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16295 ++p2;
16296 if (p2 > p)
16298 while (p2 < end)
16299 *p++ = *p2++;
16300 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16303 else
16305 xassert (INTEGERP (overlay_arrow_string));
16306 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16308 overlay_arrow_seen = 1;
16311 /* Compute pixel dimensions of this line. */
16312 compute_line_metrics (it);
16314 /* Remember the position at which this line ends. */
16315 row->end = it->current;
16317 /* Record whether this row ends inside an ellipsis. */
16318 row->ends_in_ellipsis_p
16319 = (it->method == GET_FROM_DISPLAY_VECTOR
16320 && it->ellipsis_p);
16322 /* Save fringe bitmaps in this row. */
16323 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16324 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16325 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16326 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16328 it->left_user_fringe_bitmap = 0;
16329 it->left_user_fringe_face_id = 0;
16330 it->right_user_fringe_bitmap = 0;
16331 it->right_user_fringe_face_id = 0;
16333 /* Maybe set the cursor. */
16334 if (it->w->cursor.vpos < 0
16335 && PT >= MATRIX_ROW_START_CHARPOS (row)
16336 && PT <= MATRIX_ROW_END_CHARPOS (row)
16337 && cursor_row_p (it->w, row))
16338 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16340 /* Highlight trailing whitespace. */
16341 if (!NILP (Vshow_trailing_whitespace))
16342 highlight_trailing_whitespace (it->f, it->glyph_row);
16344 /* Prepare for the next line. This line starts horizontally at (X
16345 HPOS) = (0 0). Vertical positions are incremented. As a
16346 convenience for the caller, IT->glyph_row is set to the next
16347 row to be used. */
16348 it->current_x = it->hpos = 0;
16349 it->current_y += row->height;
16350 ++it->vpos;
16351 ++it->glyph_row;
16352 it->start = it->current;
16353 return row->displays_text_p;
16358 /***********************************************************************
16359 Menu Bar
16360 ***********************************************************************/
16362 /* Redisplay the menu bar in the frame for window W.
16364 The menu bar of X frames that don't have X toolkit support is
16365 displayed in a special window W->frame->menu_bar_window.
16367 The menu bar of terminal frames is treated specially as far as
16368 glyph matrices are concerned. Menu bar lines are not part of
16369 windows, so the update is done directly on the frame matrix rows
16370 for the menu bar. */
16372 static void
16373 display_menu_bar (w)
16374 struct window *w;
16376 struct frame *f = XFRAME (WINDOW_FRAME (w));
16377 struct it it;
16378 Lisp_Object items;
16379 int i;
16381 /* Don't do all this for graphical frames. */
16382 #ifdef HAVE_NTGUI
16383 if (!NILP (Vwindow_system))
16384 return;
16385 #endif
16386 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16387 if (FRAME_X_P (f))
16388 return;
16389 #endif
16390 #ifdef MAC_OS
16391 if (FRAME_MAC_P (f))
16392 return;
16393 #endif
16395 #ifdef USE_X_TOOLKIT
16396 xassert (!FRAME_WINDOW_P (f));
16397 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16398 it.first_visible_x = 0;
16399 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16400 #else /* not USE_X_TOOLKIT */
16401 if (FRAME_WINDOW_P (f))
16403 /* Menu bar lines are displayed in the desired matrix of the
16404 dummy window menu_bar_window. */
16405 struct window *menu_w;
16406 xassert (WINDOWP (f->menu_bar_window));
16407 menu_w = XWINDOW (f->menu_bar_window);
16408 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16409 MENU_FACE_ID);
16410 it.first_visible_x = 0;
16411 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16413 else
16415 /* This is a TTY frame, i.e. character hpos/vpos are used as
16416 pixel x/y. */
16417 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16418 MENU_FACE_ID);
16419 it.first_visible_x = 0;
16420 it.last_visible_x = FRAME_COLS (f);
16422 #endif /* not USE_X_TOOLKIT */
16424 if (! mode_line_inverse_video)
16425 /* Force the menu-bar to be displayed in the default face. */
16426 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16428 /* Clear all rows of the menu bar. */
16429 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16431 struct glyph_row *row = it.glyph_row + i;
16432 clear_glyph_row (row);
16433 row->enabled_p = 1;
16434 row->full_width_p = 1;
16437 /* Display all items of the menu bar. */
16438 items = FRAME_MENU_BAR_ITEMS (it.f);
16439 for (i = 0; i < XVECTOR (items)->size; i += 4)
16441 Lisp_Object string;
16443 /* Stop at nil string. */
16444 string = AREF (items, i + 1);
16445 if (NILP (string))
16446 break;
16448 /* Remember where item was displayed. */
16449 AREF (items, i + 3) = make_number (it.hpos);
16451 /* Display the item, pad with one space. */
16452 if (it.current_x < it.last_visible_x)
16453 display_string (NULL, string, Qnil, 0, 0, &it,
16454 SCHARS (string) + 1, 0, 0, -1);
16457 /* Fill out the line with spaces. */
16458 if (it.current_x < it.last_visible_x)
16459 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16461 /* Compute the total height of the lines. */
16462 compute_line_metrics (&it);
16467 /***********************************************************************
16468 Mode Line
16469 ***********************************************************************/
16471 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16472 FORCE is non-zero, redisplay mode lines unconditionally.
16473 Otherwise, redisplay only mode lines that are garbaged. Value is
16474 the number of windows whose mode lines were redisplayed. */
16476 static int
16477 redisplay_mode_lines (window, force)
16478 Lisp_Object window;
16479 int force;
16481 int nwindows = 0;
16483 while (!NILP (window))
16485 struct window *w = XWINDOW (window);
16487 if (WINDOWP (w->hchild))
16488 nwindows += redisplay_mode_lines (w->hchild, force);
16489 else if (WINDOWP (w->vchild))
16490 nwindows += redisplay_mode_lines (w->vchild, force);
16491 else if (force
16492 || FRAME_GARBAGED_P (XFRAME (w->frame))
16493 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16495 struct text_pos lpoint;
16496 struct buffer *old = current_buffer;
16498 /* Set the window's buffer for the mode line display. */
16499 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16500 set_buffer_internal_1 (XBUFFER (w->buffer));
16502 /* Point refers normally to the selected window. For any
16503 other window, set up appropriate value. */
16504 if (!EQ (window, selected_window))
16506 struct text_pos pt;
16508 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16509 if (CHARPOS (pt) < BEGV)
16510 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16511 else if (CHARPOS (pt) > (ZV - 1))
16512 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16513 else
16514 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16517 /* Display mode lines. */
16518 clear_glyph_matrix (w->desired_matrix);
16519 if (display_mode_lines (w))
16521 ++nwindows;
16522 w->must_be_updated_p = 1;
16525 /* Restore old settings. */
16526 set_buffer_internal_1 (old);
16527 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16530 window = w->next;
16533 return nwindows;
16537 /* Display the mode and/or top line of window W. Value is the number
16538 of mode lines displayed. */
16540 static int
16541 display_mode_lines (w)
16542 struct window *w;
16544 Lisp_Object old_selected_window, old_selected_frame;
16545 int n = 0;
16547 old_selected_frame = selected_frame;
16548 selected_frame = w->frame;
16549 old_selected_window = selected_window;
16550 XSETWINDOW (selected_window, w);
16552 /* These will be set while the mode line specs are processed. */
16553 line_number_displayed = 0;
16554 w->column_number_displayed = Qnil;
16556 if (WINDOW_WANTS_MODELINE_P (w))
16558 struct window *sel_w = XWINDOW (old_selected_window);
16560 /* Select mode line face based on the real selected window. */
16561 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16562 current_buffer->mode_line_format);
16563 ++n;
16566 if (WINDOW_WANTS_HEADER_LINE_P (w))
16568 display_mode_line (w, HEADER_LINE_FACE_ID,
16569 current_buffer->header_line_format);
16570 ++n;
16573 selected_frame = old_selected_frame;
16574 selected_window = old_selected_window;
16575 return n;
16579 /* Display mode or top line of window W. FACE_ID specifies which line
16580 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16581 FORMAT is the mode line format to display. Value is the pixel
16582 height of the mode line displayed. */
16584 static int
16585 display_mode_line (w, face_id, format)
16586 struct window *w;
16587 enum face_id face_id;
16588 Lisp_Object format;
16590 struct it it;
16591 struct face *face;
16592 int count = SPECPDL_INDEX ();
16594 init_iterator (&it, w, -1, -1, NULL, face_id);
16595 /* Don't extend on a previously drawn mode-line.
16596 This may happen if called from pos_visible_p. */
16597 it.glyph_row->enabled_p = 0;
16598 prepare_desired_row (it.glyph_row);
16600 it.glyph_row->mode_line_p = 1;
16602 if (! mode_line_inverse_video)
16603 /* Force the mode-line to be displayed in the default face. */
16604 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16606 record_unwind_protect (unwind_format_mode_line,
16607 format_mode_line_unwind_data (NULL, 0));
16609 mode_line_target = MODE_LINE_DISPLAY;
16611 /* Temporarily make frame's keyboard the current kboard so that
16612 kboard-local variables in the mode_line_format will get the right
16613 values. */
16614 push_frame_kboard (it.f);
16615 record_unwind_save_match_data ();
16616 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16617 pop_frame_kboard ();
16619 unbind_to (count, Qnil);
16621 /* Fill up with spaces. */
16622 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16624 compute_line_metrics (&it);
16625 it.glyph_row->full_width_p = 1;
16626 it.glyph_row->continued_p = 0;
16627 it.glyph_row->truncated_on_left_p = 0;
16628 it.glyph_row->truncated_on_right_p = 0;
16630 /* Make a 3D mode-line have a shadow at its right end. */
16631 face = FACE_FROM_ID (it.f, face_id);
16632 extend_face_to_end_of_line (&it);
16633 if (face->box != FACE_NO_BOX)
16635 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16636 + it.glyph_row->used[TEXT_AREA] - 1);
16637 last->right_box_line_p = 1;
16640 return it.glyph_row->height;
16643 /* Move element ELT in LIST to the front of LIST.
16644 Return the updated list. */
16646 static Lisp_Object
16647 move_elt_to_front (elt, list)
16648 Lisp_Object elt, list;
16650 register Lisp_Object tail, prev;
16651 register Lisp_Object tem;
16653 tail = list;
16654 prev = Qnil;
16655 while (CONSP (tail))
16657 tem = XCAR (tail);
16659 if (EQ (elt, tem))
16661 /* Splice out the link TAIL. */
16662 if (NILP (prev))
16663 list = XCDR (tail);
16664 else
16665 Fsetcdr (prev, XCDR (tail));
16667 /* Now make it the first. */
16668 Fsetcdr (tail, list);
16669 return tail;
16671 else
16672 prev = tail;
16673 tail = XCDR (tail);
16674 QUIT;
16677 /* Not found--return unchanged LIST. */
16678 return list;
16681 /* Contribute ELT to the mode line for window IT->w. How it
16682 translates into text depends on its data type.
16684 IT describes the display environment in which we display, as usual.
16686 DEPTH is the depth in recursion. It is used to prevent
16687 infinite recursion here.
16689 FIELD_WIDTH is the number of characters the display of ELT should
16690 occupy in the mode line, and PRECISION is the maximum number of
16691 characters to display from ELT's representation. See
16692 display_string for details.
16694 Returns the hpos of the end of the text generated by ELT.
16696 PROPS is a property list to add to any string we encounter.
16698 If RISKY is nonzero, remove (disregard) any properties in any string
16699 we encounter, and ignore :eval and :propertize.
16701 The global variable `mode_line_target' determines whether the
16702 output is passed to `store_mode_line_noprop',
16703 `store_mode_line_string', or `display_string'. */
16705 static int
16706 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16707 struct it *it;
16708 int depth;
16709 int field_width, precision;
16710 Lisp_Object elt, props;
16711 int risky;
16713 int n = 0, field, prec;
16714 int literal = 0;
16716 tail_recurse:
16717 if (depth > 100)
16718 elt = build_string ("*too-deep*");
16720 depth++;
16722 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16724 case Lisp_String:
16726 /* A string: output it and check for %-constructs within it. */
16727 unsigned char c;
16728 int offset = 0;
16730 if (SCHARS (elt) > 0
16731 && (!NILP (props) || risky))
16733 Lisp_Object oprops, aelt;
16734 oprops = Ftext_properties_at (make_number (0), elt);
16736 /* If the starting string's properties are not what
16737 we want, translate the string. Also, if the string
16738 is risky, do that anyway. */
16740 if (NILP (Fequal (props, oprops)) || risky)
16742 /* If the starting string has properties,
16743 merge the specified ones onto the existing ones. */
16744 if (! NILP (oprops) && !risky)
16746 Lisp_Object tem;
16748 oprops = Fcopy_sequence (oprops);
16749 tem = props;
16750 while (CONSP (tem))
16752 oprops = Fplist_put (oprops, XCAR (tem),
16753 XCAR (XCDR (tem)));
16754 tem = XCDR (XCDR (tem));
16756 props = oprops;
16759 aelt = Fassoc (elt, mode_line_proptrans_alist);
16760 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16762 /* AELT is what we want. Move it to the front
16763 without consing. */
16764 elt = XCAR (aelt);
16765 mode_line_proptrans_alist
16766 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16768 else
16770 Lisp_Object tem;
16772 /* If AELT has the wrong props, it is useless.
16773 so get rid of it. */
16774 if (! NILP (aelt))
16775 mode_line_proptrans_alist
16776 = Fdelq (aelt, mode_line_proptrans_alist);
16778 elt = Fcopy_sequence (elt);
16779 Fset_text_properties (make_number (0), Flength (elt),
16780 props, elt);
16781 /* Add this item to mode_line_proptrans_alist. */
16782 mode_line_proptrans_alist
16783 = Fcons (Fcons (elt, props),
16784 mode_line_proptrans_alist);
16785 /* Truncate mode_line_proptrans_alist
16786 to at most 50 elements. */
16787 tem = Fnthcdr (make_number (50),
16788 mode_line_proptrans_alist);
16789 if (! NILP (tem))
16790 XSETCDR (tem, Qnil);
16795 offset = 0;
16797 if (literal)
16799 prec = precision - n;
16800 switch (mode_line_target)
16802 case MODE_LINE_NOPROP:
16803 case MODE_LINE_TITLE:
16804 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16805 break;
16806 case MODE_LINE_STRING:
16807 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16808 break;
16809 case MODE_LINE_DISPLAY:
16810 n += display_string (NULL, elt, Qnil, 0, 0, it,
16811 0, prec, 0, STRING_MULTIBYTE (elt));
16812 break;
16815 break;
16818 /* Handle the non-literal case. */
16820 while ((precision <= 0 || n < precision)
16821 && SREF (elt, offset) != 0
16822 && (mode_line_target != MODE_LINE_DISPLAY
16823 || it->current_x < it->last_visible_x))
16825 int last_offset = offset;
16827 /* Advance to end of string or next format specifier. */
16828 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16831 if (offset - 1 != last_offset)
16833 int nchars, nbytes;
16835 /* Output to end of string or up to '%'. Field width
16836 is length of string. Don't output more than
16837 PRECISION allows us. */
16838 offset--;
16840 prec = c_string_width (SDATA (elt) + last_offset,
16841 offset - last_offset, precision - n,
16842 &nchars, &nbytes);
16844 switch (mode_line_target)
16846 case MODE_LINE_NOPROP:
16847 case MODE_LINE_TITLE:
16848 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16849 break;
16850 case MODE_LINE_STRING:
16852 int bytepos = last_offset;
16853 int charpos = string_byte_to_char (elt, bytepos);
16854 int endpos = (precision <= 0
16855 ? string_byte_to_char (elt, offset)
16856 : charpos + nchars);
16858 n += store_mode_line_string (NULL,
16859 Fsubstring (elt, make_number (charpos),
16860 make_number (endpos)),
16861 0, 0, 0, Qnil);
16863 break;
16864 case MODE_LINE_DISPLAY:
16866 int bytepos = last_offset;
16867 int charpos = string_byte_to_char (elt, bytepos);
16869 if (precision <= 0)
16870 nchars = string_byte_to_char (elt, offset) - charpos;
16871 n += display_string (NULL, elt, Qnil, 0, charpos,
16872 it, 0, nchars, 0,
16873 STRING_MULTIBYTE (elt));
16875 break;
16878 else /* c == '%' */
16880 int percent_position = offset;
16882 /* Get the specified minimum width. Zero means
16883 don't pad. */
16884 field = 0;
16885 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16886 field = field * 10 + c - '0';
16888 /* Don't pad beyond the total padding allowed. */
16889 if (field_width - n > 0 && field > field_width - n)
16890 field = field_width - n;
16892 /* Note that either PRECISION <= 0 or N < PRECISION. */
16893 prec = precision - n;
16895 if (c == 'M')
16896 n += display_mode_element (it, depth, field, prec,
16897 Vglobal_mode_string, props,
16898 risky);
16899 else if (c != 0)
16901 int multibyte;
16902 int bytepos, charpos;
16903 unsigned char *spec;
16905 bytepos = percent_position;
16906 charpos = (STRING_MULTIBYTE (elt)
16907 ? string_byte_to_char (elt, bytepos)
16908 : bytepos);
16910 spec
16911 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16913 switch (mode_line_target)
16915 case MODE_LINE_NOPROP:
16916 case MODE_LINE_TITLE:
16917 n += store_mode_line_noprop (spec, field, prec);
16918 break;
16919 case MODE_LINE_STRING:
16921 int len = strlen (spec);
16922 Lisp_Object tem = make_string (spec, len);
16923 props = Ftext_properties_at (make_number (charpos), elt);
16924 /* Should only keep face property in props */
16925 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16927 break;
16928 case MODE_LINE_DISPLAY:
16930 int nglyphs_before, nwritten;
16932 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16933 nwritten = display_string (spec, Qnil, elt,
16934 charpos, 0, it,
16935 field, prec, 0,
16936 multibyte);
16938 /* Assign to the glyphs written above the
16939 string where the `%x' came from, position
16940 of the `%'. */
16941 if (nwritten > 0)
16943 struct glyph *glyph
16944 = (it->glyph_row->glyphs[TEXT_AREA]
16945 + nglyphs_before);
16946 int i;
16948 for (i = 0; i < nwritten; ++i)
16950 glyph[i].object = elt;
16951 glyph[i].charpos = charpos;
16954 n += nwritten;
16957 break;
16960 else /* c == 0 */
16961 break;
16965 break;
16967 case Lisp_Symbol:
16968 /* A symbol: process the value of the symbol recursively
16969 as if it appeared here directly. Avoid error if symbol void.
16970 Special case: if value of symbol is a string, output the string
16971 literally. */
16973 register Lisp_Object tem;
16975 /* If the variable is not marked as risky to set
16976 then its contents are risky to use. */
16977 if (NILP (Fget (elt, Qrisky_local_variable)))
16978 risky = 1;
16980 tem = Fboundp (elt);
16981 if (!NILP (tem))
16983 tem = Fsymbol_value (elt);
16984 /* If value is a string, output that string literally:
16985 don't check for % within it. */
16986 if (STRINGP (tem))
16987 literal = 1;
16989 if (!EQ (tem, elt))
16991 /* Give up right away for nil or t. */
16992 elt = tem;
16993 goto tail_recurse;
16997 break;
16999 case Lisp_Cons:
17001 register Lisp_Object car, tem;
17003 /* A cons cell: five distinct cases.
17004 If first element is :eval or :propertize, do something special.
17005 If first element is a string or a cons, process all the elements
17006 and effectively concatenate them.
17007 If first element is a negative number, truncate displaying cdr to
17008 at most that many characters. If positive, pad (with spaces)
17009 to at least that many characters.
17010 If first element is a symbol, process the cadr or caddr recursively
17011 according to whether the symbol's value is non-nil or nil. */
17012 car = XCAR (elt);
17013 if (EQ (car, QCeval))
17015 /* An element of the form (:eval FORM) means evaluate FORM
17016 and use the result as mode line elements. */
17018 if (risky)
17019 break;
17021 if (CONSP (XCDR (elt)))
17023 Lisp_Object spec;
17024 spec = safe_eval (XCAR (XCDR (elt)));
17025 n += display_mode_element (it, depth, field_width - n,
17026 precision - n, spec, props,
17027 risky);
17030 else if (EQ (car, QCpropertize))
17032 /* An element of the form (:propertize ELT PROPS...)
17033 means display ELT but applying properties PROPS. */
17035 if (risky)
17036 break;
17038 if (CONSP (XCDR (elt)))
17039 n += display_mode_element (it, depth, field_width - n,
17040 precision - n, XCAR (XCDR (elt)),
17041 XCDR (XCDR (elt)), risky);
17043 else if (SYMBOLP (car))
17045 tem = Fboundp (car);
17046 elt = XCDR (elt);
17047 if (!CONSP (elt))
17048 goto invalid;
17049 /* elt is now the cdr, and we know it is a cons cell.
17050 Use its car if CAR has a non-nil value. */
17051 if (!NILP (tem))
17053 tem = Fsymbol_value (car);
17054 if (!NILP (tem))
17056 elt = XCAR (elt);
17057 goto tail_recurse;
17060 /* Symbol's value is nil (or symbol is unbound)
17061 Get the cddr of the original list
17062 and if possible find the caddr and use that. */
17063 elt = XCDR (elt);
17064 if (NILP (elt))
17065 break;
17066 else if (!CONSP (elt))
17067 goto invalid;
17068 elt = XCAR (elt);
17069 goto tail_recurse;
17071 else if (INTEGERP (car))
17073 register int lim = XINT (car);
17074 elt = XCDR (elt);
17075 if (lim < 0)
17077 /* Negative int means reduce maximum width. */
17078 if (precision <= 0)
17079 precision = -lim;
17080 else
17081 precision = min (precision, -lim);
17083 else if (lim > 0)
17085 /* Padding specified. Don't let it be more than
17086 current maximum. */
17087 if (precision > 0)
17088 lim = min (precision, lim);
17090 /* If that's more padding than already wanted, queue it.
17091 But don't reduce padding already specified even if
17092 that is beyond the current truncation point. */
17093 field_width = max (lim, field_width);
17095 goto tail_recurse;
17097 else if (STRINGP (car) || CONSP (car))
17099 register int limit = 50;
17100 /* Limit is to protect against circular lists. */
17101 while (CONSP (elt)
17102 && --limit > 0
17103 && (precision <= 0 || n < precision))
17105 n += display_mode_element (it, depth,
17106 /* Do padding only after the last
17107 element in the list. */
17108 (! CONSP (XCDR (elt))
17109 ? field_width - n
17110 : 0),
17111 precision - n, XCAR (elt),
17112 props, risky);
17113 elt = XCDR (elt);
17117 break;
17119 default:
17120 invalid:
17121 elt = build_string ("*invalid*");
17122 goto tail_recurse;
17125 /* Pad to FIELD_WIDTH. */
17126 if (field_width > 0 && n < field_width)
17128 switch (mode_line_target)
17130 case MODE_LINE_NOPROP:
17131 case MODE_LINE_TITLE:
17132 n += store_mode_line_noprop ("", field_width - n, 0);
17133 break;
17134 case MODE_LINE_STRING:
17135 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17136 break;
17137 case MODE_LINE_DISPLAY:
17138 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17139 0, 0, 0);
17140 break;
17144 return n;
17147 /* Store a mode-line string element in mode_line_string_list.
17149 If STRING is non-null, display that C string. Otherwise, the Lisp
17150 string LISP_STRING is displayed.
17152 FIELD_WIDTH is the minimum number of output glyphs to produce.
17153 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17154 with spaces. FIELD_WIDTH <= 0 means don't pad.
17156 PRECISION is the maximum number of characters to output from
17157 STRING. PRECISION <= 0 means don't truncate the string.
17159 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17160 properties to the string.
17162 PROPS are the properties to add to the string.
17163 The mode_line_string_face face property is always added to the string.
17166 static int
17167 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17168 char *string;
17169 Lisp_Object lisp_string;
17170 int copy_string;
17171 int field_width;
17172 int precision;
17173 Lisp_Object props;
17175 int len;
17176 int n = 0;
17178 if (string != NULL)
17180 len = strlen (string);
17181 if (precision > 0 && len > precision)
17182 len = precision;
17183 lisp_string = make_string (string, len);
17184 if (NILP (props))
17185 props = mode_line_string_face_prop;
17186 else if (!NILP (mode_line_string_face))
17188 Lisp_Object face = Fplist_get (props, Qface);
17189 props = Fcopy_sequence (props);
17190 if (NILP (face))
17191 face = mode_line_string_face;
17192 else
17193 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17194 props = Fplist_put (props, Qface, face);
17196 Fadd_text_properties (make_number (0), make_number (len),
17197 props, lisp_string);
17199 else
17201 len = XFASTINT (Flength (lisp_string));
17202 if (precision > 0 && len > precision)
17204 len = precision;
17205 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17206 precision = -1;
17208 if (!NILP (mode_line_string_face))
17210 Lisp_Object face;
17211 if (NILP (props))
17212 props = Ftext_properties_at (make_number (0), lisp_string);
17213 face = Fplist_get (props, Qface);
17214 if (NILP (face))
17215 face = mode_line_string_face;
17216 else
17217 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17218 props = Fcons (Qface, Fcons (face, Qnil));
17219 if (copy_string)
17220 lisp_string = Fcopy_sequence (lisp_string);
17222 if (!NILP (props))
17223 Fadd_text_properties (make_number (0), make_number (len),
17224 props, lisp_string);
17227 if (len > 0)
17229 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17230 n += len;
17233 if (field_width > len)
17235 field_width -= len;
17236 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17237 if (!NILP (props))
17238 Fadd_text_properties (make_number (0), make_number (field_width),
17239 props, lisp_string);
17240 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17241 n += field_width;
17244 return n;
17248 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17249 1, 4, 0,
17250 doc: /* Format a string out of a mode line format specification.
17251 First arg FORMAT specifies the mode line format (see `mode-line-format'
17252 for details) to use.
17254 Optional second arg FACE specifies the face property to put
17255 on all characters for which no face is specified.
17256 t means whatever face the window's mode line currently uses
17257 \(either `mode-line' or `mode-line-inactive', depending).
17258 nil means the default is no face property.
17259 If FACE is an integer, the value string has no text properties.
17261 Optional third and fourth args WINDOW and BUFFER specify the window
17262 and buffer to use as the context for the formatting (defaults
17263 are the selected window and the window's buffer). */)
17264 (format, face, window, buffer)
17265 Lisp_Object format, face, window, buffer;
17267 struct it it;
17268 int len;
17269 struct window *w;
17270 struct buffer *old_buffer = NULL;
17271 int face_id = -1;
17272 int no_props = INTEGERP (face);
17273 int count = SPECPDL_INDEX ();
17274 Lisp_Object str;
17275 int string_start = 0;
17277 if (NILP (window))
17278 window = selected_window;
17279 CHECK_WINDOW (window);
17280 w = XWINDOW (window);
17282 if (NILP (buffer))
17283 buffer = w->buffer;
17284 CHECK_BUFFER (buffer);
17286 if (NILP (format))
17287 return build_string ("");
17289 if (no_props)
17290 face = Qnil;
17292 if (!NILP (face))
17294 if (EQ (face, Qt))
17295 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17296 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17299 if (face_id < 0)
17300 face_id = DEFAULT_FACE_ID;
17302 if (XBUFFER (buffer) != current_buffer)
17303 old_buffer = current_buffer;
17305 /* Save things including mode_line_proptrans_alist,
17306 and set that to nil so that we don't alter the outer value. */
17307 record_unwind_protect (unwind_format_mode_line,
17308 format_mode_line_unwind_data (old_buffer, 1));
17309 mode_line_proptrans_alist = Qnil;
17311 if (old_buffer)
17312 set_buffer_internal_1 (XBUFFER (buffer));
17314 init_iterator (&it, w, -1, -1, NULL, face_id);
17316 if (no_props)
17318 mode_line_target = MODE_LINE_NOPROP;
17319 mode_line_string_face_prop = Qnil;
17320 mode_line_string_list = Qnil;
17321 string_start = MODE_LINE_NOPROP_LEN (0);
17323 else
17325 mode_line_target = MODE_LINE_STRING;
17326 mode_line_string_list = Qnil;
17327 mode_line_string_face = face;
17328 mode_line_string_face_prop
17329 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17332 push_frame_kboard (it.f);
17333 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17334 pop_frame_kboard ();
17336 if (no_props)
17338 len = MODE_LINE_NOPROP_LEN (string_start);
17339 str = make_string (mode_line_noprop_buf + string_start, len);
17341 else
17343 mode_line_string_list = Fnreverse (mode_line_string_list);
17344 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17345 make_string ("", 0));
17348 unbind_to (count, Qnil);
17349 return str;
17352 /* Write a null-terminated, right justified decimal representation of
17353 the positive integer D to BUF using a minimal field width WIDTH. */
17355 static void
17356 pint2str (buf, width, d)
17357 register char *buf;
17358 register int width;
17359 register int d;
17361 register char *p = buf;
17363 if (d <= 0)
17364 *p++ = '0';
17365 else
17367 while (d > 0)
17369 *p++ = d % 10 + '0';
17370 d /= 10;
17374 for (width -= (int) (p - buf); width > 0; --width)
17375 *p++ = ' ';
17376 *p-- = '\0';
17377 while (p > buf)
17379 d = *buf;
17380 *buf++ = *p;
17381 *p-- = d;
17385 /* Write a null-terminated, right justified decimal and "human
17386 readable" representation of the nonnegative integer D to BUF using
17387 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17389 static const char power_letter[] =
17391 0, /* not used */
17392 'k', /* kilo */
17393 'M', /* mega */
17394 'G', /* giga */
17395 'T', /* tera */
17396 'P', /* peta */
17397 'E', /* exa */
17398 'Z', /* zetta */
17399 'Y' /* yotta */
17402 static void
17403 pint2hrstr (buf, width, d)
17404 char *buf;
17405 int width;
17406 int d;
17408 /* We aim to represent the nonnegative integer D as
17409 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17410 int quotient = d;
17411 int remainder = 0;
17412 /* -1 means: do not use TENTHS. */
17413 int tenths = -1;
17414 int exponent = 0;
17416 /* Length of QUOTIENT.TENTHS as a string. */
17417 int length;
17419 char * psuffix;
17420 char * p;
17422 if (1000 <= quotient)
17424 /* Scale to the appropriate EXPONENT. */
17427 remainder = quotient % 1000;
17428 quotient /= 1000;
17429 exponent++;
17431 while (1000 <= quotient);
17433 /* Round to nearest and decide whether to use TENTHS or not. */
17434 if (quotient <= 9)
17436 tenths = remainder / 100;
17437 if (50 <= remainder % 100)
17439 if (tenths < 9)
17440 tenths++;
17441 else
17443 quotient++;
17444 if (quotient == 10)
17445 tenths = -1;
17446 else
17447 tenths = 0;
17451 else
17452 if (500 <= remainder)
17454 if (quotient < 999)
17455 quotient++;
17456 else
17458 quotient = 1;
17459 exponent++;
17460 tenths = 0;
17465 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17466 if (tenths == -1 && quotient <= 99)
17467 if (quotient <= 9)
17468 length = 1;
17469 else
17470 length = 2;
17471 else
17472 length = 3;
17473 p = psuffix = buf + max (width, length);
17475 /* Print EXPONENT. */
17476 if (exponent)
17477 *psuffix++ = power_letter[exponent];
17478 *psuffix = '\0';
17480 /* Print TENTHS. */
17481 if (tenths >= 0)
17483 *--p = '0' + tenths;
17484 *--p = '.';
17487 /* Print QUOTIENT. */
17490 int digit = quotient % 10;
17491 *--p = '0' + digit;
17493 while ((quotient /= 10) != 0);
17495 /* Print leading spaces. */
17496 while (buf < p)
17497 *--p = ' ';
17500 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17501 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17502 type of CODING_SYSTEM. Return updated pointer into BUF. */
17504 static unsigned char invalid_eol_type[] = "(*invalid*)";
17506 static char *
17507 decode_mode_spec_coding (coding_system, buf, eol_flag)
17508 Lisp_Object coding_system;
17509 register char *buf;
17510 int eol_flag;
17512 Lisp_Object val;
17513 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17514 const unsigned char *eol_str;
17515 int eol_str_len;
17516 /* The EOL conversion we are using. */
17517 Lisp_Object eoltype;
17519 val = Fget (coding_system, Qcoding_system);
17520 eoltype = Qnil;
17522 if (!VECTORP (val)) /* Not yet decided. */
17524 if (multibyte)
17525 *buf++ = '-';
17526 if (eol_flag)
17527 eoltype = eol_mnemonic_undecided;
17528 /* Don't mention EOL conversion if it isn't decided. */
17530 else
17532 Lisp_Object eolvalue;
17534 eolvalue = Fget (coding_system, Qeol_type);
17536 if (multibyte)
17537 *buf++ = XFASTINT (AREF (val, 1));
17539 if (eol_flag)
17541 /* The EOL conversion that is normal on this system. */
17543 if (NILP (eolvalue)) /* Not yet decided. */
17544 eoltype = eol_mnemonic_undecided;
17545 else if (VECTORP (eolvalue)) /* Not yet decided. */
17546 eoltype = eol_mnemonic_undecided;
17547 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17548 eoltype = (XFASTINT (eolvalue) == 0
17549 ? eol_mnemonic_unix
17550 : (XFASTINT (eolvalue) == 1
17551 ? eol_mnemonic_dos : eol_mnemonic_mac));
17555 if (eol_flag)
17557 /* Mention the EOL conversion if it is not the usual one. */
17558 if (STRINGP (eoltype))
17560 eol_str = SDATA (eoltype);
17561 eol_str_len = SBYTES (eoltype);
17563 else if (INTEGERP (eoltype)
17564 && CHAR_VALID_P (XINT (eoltype), 0))
17566 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17567 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17568 eol_str = tmp;
17570 else
17572 eol_str = invalid_eol_type;
17573 eol_str_len = sizeof (invalid_eol_type) - 1;
17575 bcopy (eol_str, buf, eol_str_len);
17576 buf += eol_str_len;
17579 return buf;
17582 /* Return a string for the output of a mode line %-spec for window W,
17583 generated by character C. PRECISION >= 0 means don't return a
17584 string longer than that value. FIELD_WIDTH > 0 means pad the
17585 string returned with spaces to that value. Return 1 in *MULTIBYTE
17586 if the result is multibyte text.
17588 Note we operate on the current buffer for most purposes,
17589 the exception being w->base_line_pos. */
17591 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17593 static char *
17594 decode_mode_spec (w, c, field_width, precision, multibyte)
17595 struct window *w;
17596 register int c;
17597 int field_width, precision;
17598 int *multibyte;
17600 Lisp_Object obj;
17601 struct frame *f = XFRAME (WINDOW_FRAME (w));
17602 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17603 struct buffer *b = current_buffer;
17605 obj = Qnil;
17606 *multibyte = 0;
17608 switch (c)
17610 case '*':
17611 if (!NILP (b->read_only))
17612 return "%";
17613 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17614 return "*";
17615 return "-";
17617 case '+':
17618 /* This differs from %* only for a modified read-only buffer. */
17619 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17620 return "*";
17621 if (!NILP (b->read_only))
17622 return "%";
17623 return "-";
17625 case '&':
17626 /* This differs from %* in ignoring read-only-ness. */
17627 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17628 return "*";
17629 return "-";
17631 case '%':
17632 return "%";
17634 case '[':
17636 int i;
17637 char *p;
17639 if (command_loop_level > 5)
17640 return "[[[... ";
17641 p = decode_mode_spec_buf;
17642 for (i = 0; i < command_loop_level; i++)
17643 *p++ = '[';
17644 *p = 0;
17645 return decode_mode_spec_buf;
17648 case ']':
17650 int i;
17651 char *p;
17653 if (command_loop_level > 5)
17654 return " ...]]]";
17655 p = decode_mode_spec_buf;
17656 for (i = 0; i < command_loop_level; i++)
17657 *p++ = ']';
17658 *p = 0;
17659 return decode_mode_spec_buf;
17662 case '-':
17664 register int i;
17666 /* Let lots_of_dashes be a string of infinite length. */
17667 if (mode_line_target == MODE_LINE_NOPROP ||
17668 mode_line_target == MODE_LINE_STRING)
17669 return "--";
17670 if (field_width <= 0
17671 || field_width > sizeof (lots_of_dashes))
17673 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17674 decode_mode_spec_buf[i] = '-';
17675 decode_mode_spec_buf[i] = '\0';
17676 return decode_mode_spec_buf;
17678 else
17679 return lots_of_dashes;
17682 case 'b':
17683 obj = b->name;
17684 break;
17686 case 'c':
17687 /* %c and %l are ignored in `frame-title-format'.
17688 (In redisplay_internal, the frame title is drawn _before_ the
17689 windows are updated, so the stuff which depends on actual
17690 window contents (such as %l) may fail to render properly, or
17691 even crash emacs.) */
17692 if (mode_line_target == MODE_LINE_TITLE)
17693 return "";
17694 else
17696 int col = (int) current_column (); /* iftc */
17697 w->column_number_displayed = make_number (col);
17698 pint2str (decode_mode_spec_buf, field_width, col);
17699 return decode_mode_spec_buf;
17702 case 'e':
17703 #ifndef SYSTEM_MALLOC
17705 if (NILP (Vmemory_full))
17706 return "";
17707 else
17708 return "!MEM FULL! ";
17710 #else
17711 return "";
17712 #endif
17714 case 'F':
17715 /* %F displays the frame name. */
17716 if (!NILP (f->title))
17717 return (char *) SDATA (f->title);
17718 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17719 return (char *) SDATA (f->name);
17720 return "Emacs";
17722 case 'f':
17723 obj = b->filename;
17724 break;
17726 case 'i':
17728 int size = ZV - BEGV;
17729 pint2str (decode_mode_spec_buf, field_width, size);
17730 return decode_mode_spec_buf;
17733 case 'I':
17735 int size = ZV - BEGV;
17736 pint2hrstr (decode_mode_spec_buf, field_width, size);
17737 return decode_mode_spec_buf;
17740 case 'l':
17742 int startpos, startpos_byte, line, linepos, linepos_byte;
17743 int topline, nlines, junk, height;
17745 /* %c and %l are ignored in `frame-title-format'. */
17746 if (mode_line_target == MODE_LINE_TITLE)
17747 return "";
17749 startpos = XMARKER (w->start)->charpos;
17750 startpos_byte = marker_byte_position (w->start);
17751 height = WINDOW_TOTAL_LINES (w);
17753 /* If we decided that this buffer isn't suitable for line numbers,
17754 don't forget that too fast. */
17755 if (EQ (w->base_line_pos, w->buffer))
17756 goto no_value;
17757 /* But do forget it, if the window shows a different buffer now. */
17758 else if (BUFFERP (w->base_line_pos))
17759 w->base_line_pos = Qnil;
17761 /* If the buffer is very big, don't waste time. */
17762 if (INTEGERP (Vline_number_display_limit)
17763 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17765 w->base_line_pos = Qnil;
17766 w->base_line_number = Qnil;
17767 goto no_value;
17770 if (!NILP (w->base_line_number)
17771 && !NILP (w->base_line_pos)
17772 && XFASTINT (w->base_line_pos) <= startpos)
17774 line = XFASTINT (w->base_line_number);
17775 linepos = XFASTINT (w->base_line_pos);
17776 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17778 else
17780 line = 1;
17781 linepos = BUF_BEGV (b);
17782 linepos_byte = BUF_BEGV_BYTE (b);
17785 /* Count lines from base line to window start position. */
17786 nlines = display_count_lines (linepos, linepos_byte,
17787 startpos_byte,
17788 startpos, &junk);
17790 topline = nlines + line;
17792 /* Determine a new base line, if the old one is too close
17793 or too far away, or if we did not have one.
17794 "Too close" means it's plausible a scroll-down would
17795 go back past it. */
17796 if (startpos == BUF_BEGV (b))
17798 w->base_line_number = make_number (topline);
17799 w->base_line_pos = make_number (BUF_BEGV (b));
17801 else if (nlines < height + 25 || nlines > height * 3 + 50
17802 || linepos == BUF_BEGV (b))
17804 int limit = BUF_BEGV (b);
17805 int limit_byte = BUF_BEGV_BYTE (b);
17806 int position;
17807 int distance = (height * 2 + 30) * line_number_display_limit_width;
17809 if (startpos - distance > limit)
17811 limit = startpos - distance;
17812 limit_byte = CHAR_TO_BYTE (limit);
17815 nlines = display_count_lines (startpos, startpos_byte,
17816 limit_byte,
17817 - (height * 2 + 30),
17818 &position);
17819 /* If we couldn't find the lines we wanted within
17820 line_number_display_limit_width chars per line,
17821 give up on line numbers for this window. */
17822 if (position == limit_byte && limit == startpos - distance)
17824 w->base_line_pos = w->buffer;
17825 w->base_line_number = Qnil;
17826 goto no_value;
17829 w->base_line_number = make_number (topline - nlines);
17830 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17833 /* Now count lines from the start pos to point. */
17834 nlines = display_count_lines (startpos, startpos_byte,
17835 PT_BYTE, PT, &junk);
17837 /* Record that we did display the line number. */
17838 line_number_displayed = 1;
17840 /* Make the string to show. */
17841 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17842 return decode_mode_spec_buf;
17843 no_value:
17845 char* p = decode_mode_spec_buf;
17846 int pad = field_width - 2;
17847 while (pad-- > 0)
17848 *p++ = ' ';
17849 *p++ = '?';
17850 *p++ = '?';
17851 *p = '\0';
17852 return decode_mode_spec_buf;
17855 break;
17857 case 'm':
17858 obj = b->mode_name;
17859 break;
17861 case 'n':
17862 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17863 return " Narrow";
17864 break;
17866 case 'p':
17868 int pos = marker_position (w->start);
17869 int total = BUF_ZV (b) - BUF_BEGV (b);
17871 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17873 if (pos <= BUF_BEGV (b))
17874 return "All";
17875 else
17876 return "Bottom";
17878 else if (pos <= BUF_BEGV (b))
17879 return "Top";
17880 else
17882 if (total > 1000000)
17883 /* Do it differently for a large value, to avoid overflow. */
17884 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17885 else
17886 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17887 /* We can't normally display a 3-digit number,
17888 so get us a 2-digit number that is close. */
17889 if (total == 100)
17890 total = 99;
17891 sprintf (decode_mode_spec_buf, "%2d%%", total);
17892 return decode_mode_spec_buf;
17896 /* Display percentage of size above the bottom of the screen. */
17897 case 'P':
17899 int toppos = marker_position (w->start);
17900 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17901 int total = BUF_ZV (b) - BUF_BEGV (b);
17903 if (botpos >= BUF_ZV (b))
17905 if (toppos <= BUF_BEGV (b))
17906 return "All";
17907 else
17908 return "Bottom";
17910 else
17912 if (total > 1000000)
17913 /* Do it differently for a large value, to avoid overflow. */
17914 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17915 else
17916 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17917 /* We can't normally display a 3-digit number,
17918 so get us a 2-digit number that is close. */
17919 if (total == 100)
17920 total = 99;
17921 if (toppos <= BUF_BEGV (b))
17922 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17923 else
17924 sprintf (decode_mode_spec_buf, "%2d%%", total);
17925 return decode_mode_spec_buf;
17929 case 's':
17930 /* status of process */
17931 obj = Fget_buffer_process (Fcurrent_buffer ());
17932 if (NILP (obj))
17933 return "no process";
17934 #ifdef subprocesses
17935 obj = Fsymbol_name (Fprocess_status (obj));
17936 #endif
17937 break;
17939 case 't': /* indicate TEXT or BINARY */
17940 #ifdef MODE_LINE_BINARY_TEXT
17941 return MODE_LINE_BINARY_TEXT (b);
17942 #else
17943 return "T";
17944 #endif
17946 case 'z':
17947 /* coding-system (not including end-of-line format) */
17948 case 'Z':
17949 /* coding-system (including end-of-line type) */
17951 int eol_flag = (c == 'Z');
17952 char *p = decode_mode_spec_buf;
17954 if (! FRAME_WINDOW_P (f))
17956 /* No need to mention EOL here--the terminal never needs
17957 to do EOL conversion. */
17958 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17959 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17961 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17962 p, eol_flag);
17964 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17965 #ifdef subprocesses
17966 obj = Fget_buffer_process (Fcurrent_buffer ());
17967 if (PROCESSP (obj))
17969 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17970 p, eol_flag);
17971 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17972 p, eol_flag);
17974 #endif /* subprocesses */
17975 #endif /* 0 */
17976 *p = 0;
17977 return decode_mode_spec_buf;
17981 if (STRINGP (obj))
17983 *multibyte = STRING_MULTIBYTE (obj);
17984 return (char *) SDATA (obj);
17986 else
17987 return "";
17991 /* Count up to COUNT lines starting from START / START_BYTE.
17992 But don't go beyond LIMIT_BYTE.
17993 Return the number of lines thus found (always nonnegative).
17995 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17997 static int
17998 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17999 int start, start_byte, limit_byte, count;
18000 int *byte_pos_ptr;
18002 register unsigned char *cursor;
18003 unsigned char *base;
18005 register int ceiling;
18006 register unsigned char *ceiling_addr;
18007 int orig_count = count;
18009 /* If we are not in selective display mode,
18010 check only for newlines. */
18011 int selective_display = (!NILP (current_buffer->selective_display)
18012 && !INTEGERP (current_buffer->selective_display));
18014 if (count > 0)
18016 while (start_byte < limit_byte)
18018 ceiling = BUFFER_CEILING_OF (start_byte);
18019 ceiling = min (limit_byte - 1, ceiling);
18020 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18021 base = (cursor = BYTE_POS_ADDR (start_byte));
18022 while (1)
18024 if (selective_display)
18025 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18027 else
18028 while (*cursor != '\n' && ++cursor != ceiling_addr)
18031 if (cursor != ceiling_addr)
18033 if (--count == 0)
18035 start_byte += cursor - base + 1;
18036 *byte_pos_ptr = start_byte;
18037 return orig_count;
18039 else
18040 if (++cursor == ceiling_addr)
18041 break;
18043 else
18044 break;
18046 start_byte += cursor - base;
18049 else
18051 while (start_byte > limit_byte)
18053 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18054 ceiling = max (limit_byte, ceiling);
18055 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18056 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18057 while (1)
18059 if (selective_display)
18060 while (--cursor != ceiling_addr
18061 && *cursor != '\n' && *cursor != 015)
18063 else
18064 while (--cursor != ceiling_addr && *cursor != '\n')
18067 if (cursor != ceiling_addr)
18069 if (++count == 0)
18071 start_byte += cursor - base + 1;
18072 *byte_pos_ptr = start_byte;
18073 /* When scanning backwards, we should
18074 not count the newline posterior to which we stop. */
18075 return - orig_count - 1;
18078 else
18079 break;
18081 /* Here we add 1 to compensate for the last decrement
18082 of CURSOR, which took it past the valid range. */
18083 start_byte += cursor - base + 1;
18087 *byte_pos_ptr = limit_byte;
18089 if (count < 0)
18090 return - orig_count + count;
18091 return orig_count - count;
18097 /***********************************************************************
18098 Displaying strings
18099 ***********************************************************************/
18101 /* Display a NUL-terminated string, starting with index START.
18103 If STRING is non-null, display that C string. Otherwise, the Lisp
18104 string LISP_STRING is displayed.
18106 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18107 FACE_STRING. Display STRING or LISP_STRING with the face at
18108 FACE_STRING_POS in FACE_STRING:
18110 Display the string in the environment given by IT, but use the
18111 standard display table, temporarily.
18113 FIELD_WIDTH is the minimum number of output glyphs to produce.
18114 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18115 with spaces. If STRING has more characters, more than FIELD_WIDTH
18116 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18118 PRECISION is the maximum number of characters to output from
18119 STRING. PRECISION < 0 means don't truncate the string.
18121 This is roughly equivalent to printf format specifiers:
18123 FIELD_WIDTH PRECISION PRINTF
18124 ----------------------------------------
18125 -1 -1 %s
18126 -1 10 %.10s
18127 10 -1 %10s
18128 20 10 %20.10s
18130 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18131 display them, and < 0 means obey the current buffer's value of
18132 enable_multibyte_characters.
18134 Value is the number of columns displayed. */
18136 static int
18137 display_string (string, lisp_string, face_string, face_string_pos,
18138 start, it, field_width, precision, max_x, multibyte)
18139 unsigned char *string;
18140 Lisp_Object lisp_string;
18141 Lisp_Object face_string;
18142 int face_string_pos;
18143 int start;
18144 struct it *it;
18145 int field_width, precision, max_x;
18146 int multibyte;
18148 int hpos_at_start = it->hpos;
18149 int saved_face_id = it->face_id;
18150 struct glyph_row *row = it->glyph_row;
18152 /* Initialize the iterator IT for iteration over STRING beginning
18153 with index START. */
18154 reseat_to_string (it, string, lisp_string, start,
18155 precision, field_width, multibyte);
18157 /* If displaying STRING, set up the face of the iterator
18158 from LISP_STRING, if that's given. */
18159 if (STRINGP (face_string))
18161 int endptr;
18162 struct face *face;
18164 it->face_id
18165 = face_at_string_position (it->w, face_string, face_string_pos,
18166 0, it->region_beg_charpos,
18167 it->region_end_charpos,
18168 &endptr, it->base_face_id, 0);
18169 face = FACE_FROM_ID (it->f, it->face_id);
18170 it->face_box_p = face->box != FACE_NO_BOX;
18173 /* Set max_x to the maximum allowed X position. Don't let it go
18174 beyond the right edge of the window. */
18175 if (max_x <= 0)
18176 max_x = it->last_visible_x;
18177 else
18178 max_x = min (max_x, it->last_visible_x);
18180 /* Skip over display elements that are not visible. because IT->w is
18181 hscrolled. */
18182 if (it->current_x < it->first_visible_x)
18183 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18184 MOVE_TO_POS | MOVE_TO_X);
18186 row->ascent = it->max_ascent;
18187 row->height = it->max_ascent + it->max_descent;
18188 row->phys_ascent = it->max_phys_ascent;
18189 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18190 row->extra_line_spacing = it->max_extra_line_spacing;
18192 /* This condition is for the case that we are called with current_x
18193 past last_visible_x. */
18194 while (it->current_x < max_x)
18196 int x_before, x, n_glyphs_before, i, nglyphs;
18198 /* Get the next display element. */
18199 if (!get_next_display_element (it))
18200 break;
18202 /* Produce glyphs. */
18203 x_before = it->current_x;
18204 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18205 PRODUCE_GLYPHS (it);
18207 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18208 i = 0;
18209 x = x_before;
18210 while (i < nglyphs)
18212 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18214 if (!it->truncate_lines_p
18215 && x + glyph->pixel_width > max_x)
18217 /* End of continued line or max_x reached. */
18218 if (CHAR_GLYPH_PADDING_P (*glyph))
18220 /* A wide character is unbreakable. */
18221 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18222 it->current_x = x_before;
18224 else
18226 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18227 it->current_x = x;
18229 break;
18231 else if (x + glyph->pixel_width > it->first_visible_x)
18233 /* Glyph is at least partially visible. */
18234 ++it->hpos;
18235 if (x < it->first_visible_x)
18236 it->glyph_row->x = x - it->first_visible_x;
18238 else
18240 /* Glyph is off the left margin of the display area.
18241 Should not happen. */
18242 abort ();
18245 row->ascent = max (row->ascent, it->max_ascent);
18246 row->height = max (row->height, it->max_ascent + it->max_descent);
18247 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18248 row->phys_height = max (row->phys_height,
18249 it->max_phys_ascent + it->max_phys_descent);
18250 row->extra_line_spacing = max (row->extra_line_spacing,
18251 it->max_extra_line_spacing);
18252 x += glyph->pixel_width;
18253 ++i;
18256 /* Stop if max_x reached. */
18257 if (i < nglyphs)
18258 break;
18260 /* Stop at line ends. */
18261 if (ITERATOR_AT_END_OF_LINE_P (it))
18263 it->continuation_lines_width = 0;
18264 break;
18267 set_iterator_to_next (it, 1);
18269 /* Stop if truncating at the right edge. */
18270 if (it->truncate_lines_p
18271 && it->current_x >= it->last_visible_x)
18273 /* Add truncation mark, but don't do it if the line is
18274 truncated at a padding space. */
18275 if (IT_CHARPOS (*it) < it->string_nchars)
18277 if (!FRAME_WINDOW_P (it->f))
18279 int i, n;
18281 if (it->current_x > it->last_visible_x)
18283 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18284 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18285 break;
18286 for (n = row->used[TEXT_AREA]; i < n; ++i)
18288 row->used[TEXT_AREA] = i;
18289 produce_special_glyphs (it, IT_TRUNCATION);
18292 produce_special_glyphs (it, IT_TRUNCATION);
18294 it->glyph_row->truncated_on_right_p = 1;
18296 break;
18300 /* Maybe insert a truncation at the left. */
18301 if (it->first_visible_x
18302 && IT_CHARPOS (*it) > 0)
18304 if (!FRAME_WINDOW_P (it->f))
18305 insert_left_trunc_glyphs (it);
18306 it->glyph_row->truncated_on_left_p = 1;
18309 it->face_id = saved_face_id;
18311 /* Value is number of columns displayed. */
18312 return it->hpos - hpos_at_start;
18317 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18318 appears as an element of LIST or as the car of an element of LIST.
18319 If PROPVAL is a list, compare each element against LIST in that
18320 way, and return 1/2 if any element of PROPVAL is found in LIST.
18321 Otherwise return 0. This function cannot quit.
18322 The return value is 2 if the text is invisible but with an ellipsis
18323 and 1 if it's invisible and without an ellipsis. */
18326 invisible_p (propval, list)
18327 register Lisp_Object propval;
18328 Lisp_Object list;
18330 register Lisp_Object tail, proptail;
18332 for (tail = list; CONSP (tail); tail = XCDR (tail))
18334 register Lisp_Object tem;
18335 tem = XCAR (tail);
18336 if (EQ (propval, tem))
18337 return 1;
18338 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18339 return NILP (XCDR (tem)) ? 1 : 2;
18342 if (CONSP (propval))
18344 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18346 Lisp_Object propelt;
18347 propelt = XCAR (proptail);
18348 for (tail = list; CONSP (tail); tail = XCDR (tail))
18350 register Lisp_Object tem;
18351 tem = XCAR (tail);
18352 if (EQ (propelt, tem))
18353 return 1;
18354 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18355 return NILP (XCDR (tem)) ? 1 : 2;
18360 return 0;
18363 /* Calculate a width or height in pixels from a specification using
18364 the following elements:
18366 SPEC ::=
18367 NUM - a (fractional) multiple of the default font width/height
18368 (NUM) - specifies exactly NUM pixels
18369 UNIT - a fixed number of pixels, see below.
18370 ELEMENT - size of a display element in pixels, see below.
18371 (NUM . SPEC) - equals NUM * SPEC
18372 (+ SPEC SPEC ...) - add pixel values
18373 (- SPEC SPEC ...) - subtract pixel values
18374 (- SPEC) - negate pixel value
18376 NUM ::=
18377 INT or FLOAT - a number constant
18378 SYMBOL - use symbol's (buffer local) variable binding.
18380 UNIT ::=
18381 in - pixels per inch *)
18382 mm - pixels per 1/1000 meter *)
18383 cm - pixels per 1/100 meter *)
18384 width - width of current font in pixels.
18385 height - height of current font in pixels.
18387 *) using the ratio(s) defined in display-pixels-per-inch.
18389 ELEMENT ::=
18391 left-fringe - left fringe width in pixels
18392 right-fringe - right fringe width in pixels
18394 left-margin - left margin width in pixels
18395 right-margin - right margin width in pixels
18397 scroll-bar - scroll-bar area width in pixels
18399 Examples:
18401 Pixels corresponding to 5 inches:
18402 (5 . in)
18404 Total width of non-text areas on left side of window (if scroll-bar is on left):
18405 '(space :width (+ left-fringe left-margin scroll-bar))
18407 Align to first text column (in header line):
18408 '(space :align-to 0)
18410 Align to middle of text area minus half the width of variable `my-image'
18411 containing a loaded image:
18412 '(space :align-to (0.5 . (- text my-image)))
18414 Width of left margin minus width of 1 character in the default font:
18415 '(space :width (- left-margin 1))
18417 Width of left margin minus width of 2 characters in the current font:
18418 '(space :width (- left-margin (2 . width)))
18420 Center 1 character over left-margin (in header line):
18421 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18423 Different ways to express width of left fringe plus left margin minus one pixel:
18424 '(space :width (- (+ left-fringe left-margin) (1)))
18425 '(space :width (+ left-fringe left-margin (- (1))))
18426 '(space :width (+ left-fringe left-margin (-1)))
18430 #define NUMVAL(X) \
18431 ((INTEGERP (X) || FLOATP (X)) \
18432 ? XFLOATINT (X) \
18433 : - 1)
18436 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18437 double *res;
18438 struct it *it;
18439 Lisp_Object prop;
18440 void *font;
18441 int width_p, *align_to;
18443 double pixels;
18445 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18446 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18448 if (NILP (prop))
18449 return OK_PIXELS (0);
18451 if (SYMBOLP (prop))
18453 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18455 char *unit = SDATA (SYMBOL_NAME (prop));
18457 if (unit[0] == 'i' && unit[1] == 'n')
18458 pixels = 1.0;
18459 else if (unit[0] == 'm' && unit[1] == 'm')
18460 pixels = 25.4;
18461 else if (unit[0] == 'c' && unit[1] == 'm')
18462 pixels = 2.54;
18463 else
18464 pixels = 0;
18465 if (pixels > 0)
18467 double ppi;
18468 #ifdef HAVE_WINDOW_SYSTEM
18469 if (FRAME_WINDOW_P (it->f)
18470 && (ppi = (width_p
18471 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18472 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18473 ppi > 0))
18474 return OK_PIXELS (ppi / pixels);
18475 #endif
18477 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18478 || (CONSP (Vdisplay_pixels_per_inch)
18479 && (ppi = (width_p
18480 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18481 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18482 ppi > 0)))
18483 return OK_PIXELS (ppi / pixels);
18485 return 0;
18489 #ifdef HAVE_WINDOW_SYSTEM
18490 if (EQ (prop, Qheight))
18491 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18492 if (EQ (prop, Qwidth))
18493 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18494 #else
18495 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18496 return OK_PIXELS (1);
18497 #endif
18499 if (EQ (prop, Qtext))
18500 return OK_PIXELS (width_p
18501 ? window_box_width (it->w, TEXT_AREA)
18502 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18504 if (align_to && *align_to < 0)
18506 *res = 0;
18507 if (EQ (prop, Qleft))
18508 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18509 if (EQ (prop, Qright))
18510 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18511 if (EQ (prop, Qcenter))
18512 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18513 + window_box_width (it->w, TEXT_AREA) / 2);
18514 if (EQ (prop, Qleft_fringe))
18515 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18516 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18517 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18518 if (EQ (prop, Qright_fringe))
18519 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18520 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18521 : window_box_right_offset (it->w, TEXT_AREA));
18522 if (EQ (prop, Qleft_margin))
18523 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18524 if (EQ (prop, Qright_margin))
18525 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18526 if (EQ (prop, Qscroll_bar))
18527 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18529 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18530 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18531 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18532 : 0)));
18534 else
18536 if (EQ (prop, Qleft_fringe))
18537 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18538 if (EQ (prop, Qright_fringe))
18539 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18540 if (EQ (prop, Qleft_margin))
18541 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18542 if (EQ (prop, Qright_margin))
18543 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18544 if (EQ (prop, Qscroll_bar))
18545 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18548 prop = Fbuffer_local_value (prop, it->w->buffer);
18551 if (INTEGERP (prop) || FLOATP (prop))
18553 int base_unit = (width_p
18554 ? FRAME_COLUMN_WIDTH (it->f)
18555 : FRAME_LINE_HEIGHT (it->f));
18556 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18559 if (CONSP (prop))
18561 Lisp_Object car = XCAR (prop);
18562 Lisp_Object cdr = XCDR (prop);
18564 if (SYMBOLP (car))
18566 #ifdef HAVE_WINDOW_SYSTEM
18567 if (valid_image_p (prop))
18569 int id = lookup_image (it->f, prop);
18570 struct image *img = IMAGE_FROM_ID (it->f, id);
18572 return OK_PIXELS (width_p ? img->width : img->height);
18574 #endif
18575 if (EQ (car, Qplus) || EQ (car, Qminus))
18577 int first = 1;
18578 double px;
18580 pixels = 0;
18581 while (CONSP (cdr))
18583 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18584 font, width_p, align_to))
18585 return 0;
18586 if (first)
18587 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18588 else
18589 pixels += px;
18590 cdr = XCDR (cdr);
18592 if (EQ (car, Qminus))
18593 pixels = -pixels;
18594 return OK_PIXELS (pixels);
18597 car = Fbuffer_local_value (car, it->w->buffer);
18600 if (INTEGERP (car) || FLOATP (car))
18602 double fact;
18603 pixels = XFLOATINT (car);
18604 if (NILP (cdr))
18605 return OK_PIXELS (pixels);
18606 if (calc_pixel_width_or_height (&fact, it, cdr,
18607 font, width_p, align_to))
18608 return OK_PIXELS (pixels * fact);
18609 return 0;
18612 return 0;
18615 return 0;
18619 /***********************************************************************
18620 Glyph Display
18621 ***********************************************************************/
18623 #ifdef HAVE_WINDOW_SYSTEM
18625 #if GLYPH_DEBUG
18627 void
18628 dump_glyph_string (s)
18629 struct glyph_string *s;
18631 fprintf (stderr, "glyph string\n");
18632 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18633 s->x, s->y, s->width, s->height);
18634 fprintf (stderr, " ybase = %d\n", s->ybase);
18635 fprintf (stderr, " hl = %d\n", s->hl);
18636 fprintf (stderr, " left overhang = %d, right = %d\n",
18637 s->left_overhang, s->right_overhang);
18638 fprintf (stderr, " nchars = %d\n", s->nchars);
18639 fprintf (stderr, " extends to end of line = %d\n",
18640 s->extends_to_end_of_line_p);
18641 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18642 fprintf (stderr, " bg width = %d\n", s->background_width);
18645 #endif /* GLYPH_DEBUG */
18647 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18648 of XChar2b structures for S; it can't be allocated in
18649 init_glyph_string because it must be allocated via `alloca'. W
18650 is the window on which S is drawn. ROW and AREA are the glyph row
18651 and area within the row from which S is constructed. START is the
18652 index of the first glyph structure covered by S. HL is a
18653 face-override for drawing S. */
18655 #ifdef HAVE_NTGUI
18656 #define OPTIONAL_HDC(hdc) hdc,
18657 #define DECLARE_HDC(hdc) HDC hdc;
18658 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18659 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18660 #endif
18662 #ifndef OPTIONAL_HDC
18663 #define OPTIONAL_HDC(hdc)
18664 #define DECLARE_HDC(hdc)
18665 #define ALLOCATE_HDC(hdc, f)
18666 #define RELEASE_HDC(hdc, f)
18667 #endif
18669 static void
18670 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18671 struct glyph_string *s;
18672 DECLARE_HDC (hdc)
18673 XChar2b *char2b;
18674 struct window *w;
18675 struct glyph_row *row;
18676 enum glyph_row_area area;
18677 int start;
18678 enum draw_glyphs_face hl;
18680 bzero (s, sizeof *s);
18681 s->w = w;
18682 s->f = XFRAME (w->frame);
18683 #ifdef HAVE_NTGUI
18684 s->hdc = hdc;
18685 #endif
18686 s->display = FRAME_X_DISPLAY (s->f);
18687 s->window = FRAME_X_WINDOW (s->f);
18688 s->char2b = char2b;
18689 s->hl = hl;
18690 s->row = row;
18691 s->area = area;
18692 s->first_glyph = row->glyphs[area] + start;
18693 s->height = row->height;
18694 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18696 /* Display the internal border below the tool-bar window. */
18697 if (WINDOWP (s->f->tool_bar_window)
18698 && s->w == XWINDOW (s->f->tool_bar_window))
18699 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18701 s->ybase = s->y + row->ascent;
18705 /* Append the list of glyph strings with head H and tail T to the list
18706 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18708 static INLINE void
18709 append_glyph_string_lists (head, tail, h, t)
18710 struct glyph_string **head, **tail;
18711 struct glyph_string *h, *t;
18713 if (h)
18715 if (*head)
18716 (*tail)->next = h;
18717 else
18718 *head = h;
18719 h->prev = *tail;
18720 *tail = t;
18725 /* Prepend the list of glyph strings with head H and tail T to the
18726 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18727 result. */
18729 static INLINE void
18730 prepend_glyph_string_lists (head, tail, h, t)
18731 struct glyph_string **head, **tail;
18732 struct glyph_string *h, *t;
18734 if (h)
18736 if (*head)
18737 (*head)->prev = t;
18738 else
18739 *tail = t;
18740 t->next = *head;
18741 *head = h;
18746 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18747 Set *HEAD and *TAIL to the resulting list. */
18749 static INLINE void
18750 append_glyph_string (head, tail, s)
18751 struct glyph_string **head, **tail;
18752 struct glyph_string *s;
18754 s->next = s->prev = NULL;
18755 append_glyph_string_lists (head, tail, s, s);
18759 /* Get face and two-byte form of character glyph GLYPH on frame F.
18760 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18761 a pointer to a realized face that is ready for display. */
18763 static INLINE struct face *
18764 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18765 struct frame *f;
18766 struct glyph *glyph;
18767 XChar2b *char2b;
18768 int *two_byte_p;
18770 struct face *face;
18772 xassert (glyph->type == CHAR_GLYPH);
18773 face = FACE_FROM_ID (f, glyph->face_id);
18775 if (two_byte_p)
18776 *two_byte_p = 0;
18778 if (!glyph->multibyte_p)
18780 /* Unibyte case. We don't have to encode, but we have to make
18781 sure to use a face suitable for unibyte. */
18782 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18784 else if (glyph->u.ch < 128)
18786 /* Case of ASCII in a face known to fit ASCII. */
18787 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18789 else
18791 int c1, c2, charset;
18793 /* Split characters into bytes. If c2 is -1 afterwards, C is
18794 really a one-byte character so that byte1 is zero. */
18795 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18796 if (c2 > 0)
18797 STORE_XCHAR2B (char2b, c1, c2);
18798 else
18799 STORE_XCHAR2B (char2b, 0, c1);
18801 /* Maybe encode the character in *CHAR2B. */
18802 if (charset != CHARSET_ASCII)
18804 struct font_info *font_info
18805 = FONT_INFO_FROM_ID (f, face->font_info_id);
18806 if (font_info)
18807 glyph->font_type
18808 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18812 /* Make sure X resources of the face are allocated. */
18813 xassert (face != NULL);
18814 PREPARE_FACE_FOR_DISPLAY (f, face);
18815 return face;
18819 /* Fill glyph string S with composition components specified by S->cmp.
18821 FACES is an array of faces for all components of this composition.
18822 S->gidx is the index of the first component for S.
18824 OVERLAPS non-zero means S should draw the foreground only, and use
18825 its physical height for clipping. See also draw_glyphs.
18827 Value is the index of a component not in S. */
18829 static int
18830 fill_composite_glyph_string (s, faces, overlaps)
18831 struct glyph_string *s;
18832 struct face **faces;
18833 int overlaps;
18835 int i;
18837 xassert (s);
18839 s->for_overlaps = overlaps;
18841 s->face = faces[s->gidx];
18842 s->font = s->face->font;
18843 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18845 /* For all glyphs of this composition, starting at the offset
18846 S->gidx, until we reach the end of the definition or encounter a
18847 glyph that requires the different face, add it to S. */
18848 ++s->nchars;
18849 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18850 ++s->nchars;
18852 /* All glyph strings for the same composition has the same width,
18853 i.e. the width set for the first component of the composition. */
18855 s->width = s->first_glyph->pixel_width;
18857 /* If the specified font could not be loaded, use the frame's
18858 default font, but record the fact that we couldn't load it in
18859 the glyph string so that we can draw rectangles for the
18860 characters of the glyph string. */
18861 if (s->font == NULL)
18863 s->font_not_found_p = 1;
18864 s->font = FRAME_FONT (s->f);
18867 /* Adjust base line for subscript/superscript text. */
18868 s->ybase += s->first_glyph->voffset;
18870 xassert (s->face && s->face->gc);
18872 /* This glyph string must always be drawn with 16-bit functions. */
18873 s->two_byte_p = 1;
18875 return s->gidx + s->nchars;
18879 /* Fill glyph string S from a sequence of character glyphs.
18881 FACE_ID is the face id of the string. START is the index of the
18882 first glyph to consider, END is the index of the last + 1.
18883 OVERLAPS non-zero means S should draw the foreground only, and use
18884 its physical height for clipping. See also draw_glyphs.
18886 Value is the index of the first glyph not in S. */
18888 static int
18889 fill_glyph_string (s, face_id, start, end, overlaps)
18890 struct glyph_string *s;
18891 int face_id;
18892 int start, end, overlaps;
18894 struct glyph *glyph, *last;
18895 int voffset;
18896 int glyph_not_available_p;
18898 xassert (s->f == XFRAME (s->w->frame));
18899 xassert (s->nchars == 0);
18900 xassert (start >= 0 && end > start);
18902 s->for_overlaps = overlaps,
18903 glyph = s->row->glyphs[s->area] + start;
18904 last = s->row->glyphs[s->area] + end;
18905 voffset = glyph->voffset;
18907 glyph_not_available_p = glyph->glyph_not_available_p;
18909 while (glyph < last
18910 && glyph->type == CHAR_GLYPH
18911 && glyph->voffset == voffset
18912 /* Same face id implies same font, nowadays. */
18913 && glyph->face_id == face_id
18914 && glyph->glyph_not_available_p == glyph_not_available_p)
18916 int two_byte_p;
18918 s->face = get_glyph_face_and_encoding (s->f, glyph,
18919 s->char2b + s->nchars,
18920 &two_byte_p);
18921 s->two_byte_p = two_byte_p;
18922 ++s->nchars;
18923 xassert (s->nchars <= end - start);
18924 s->width += glyph->pixel_width;
18925 ++glyph;
18928 s->font = s->face->font;
18929 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18931 /* If the specified font could not be loaded, use the frame's font,
18932 but record the fact that we couldn't load it in
18933 S->font_not_found_p so that we can draw rectangles for the
18934 characters of the glyph string. */
18935 if (s->font == NULL || glyph_not_available_p)
18937 s->font_not_found_p = 1;
18938 s->font = FRAME_FONT (s->f);
18941 /* Adjust base line for subscript/superscript text. */
18942 s->ybase += voffset;
18944 xassert (s->face && s->face->gc);
18945 return glyph - s->row->glyphs[s->area];
18949 /* Fill glyph string S from image glyph S->first_glyph. */
18951 static void
18952 fill_image_glyph_string (s)
18953 struct glyph_string *s;
18955 xassert (s->first_glyph->type == IMAGE_GLYPH);
18956 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18957 xassert (s->img);
18958 s->slice = s->first_glyph->slice;
18959 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18960 s->font = s->face->font;
18961 s->width = s->first_glyph->pixel_width;
18963 /* Adjust base line for subscript/superscript text. */
18964 s->ybase += s->first_glyph->voffset;
18968 /* Fill glyph string S from a sequence of stretch glyphs.
18970 ROW is the glyph row in which the glyphs are found, AREA is the
18971 area within the row. START is the index of the first glyph to
18972 consider, END is the index of the last + 1.
18974 Value is the index of the first glyph not in S. */
18976 static int
18977 fill_stretch_glyph_string (s, row, area, start, end)
18978 struct glyph_string *s;
18979 struct glyph_row *row;
18980 enum glyph_row_area area;
18981 int start, end;
18983 struct glyph *glyph, *last;
18984 int voffset, face_id;
18986 xassert (s->first_glyph->type == STRETCH_GLYPH);
18988 glyph = s->row->glyphs[s->area] + start;
18989 last = s->row->glyphs[s->area] + end;
18990 face_id = glyph->face_id;
18991 s->face = FACE_FROM_ID (s->f, face_id);
18992 s->font = s->face->font;
18993 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18994 s->width = glyph->pixel_width;
18995 s->nchars = 1;
18996 voffset = glyph->voffset;
18998 for (++glyph;
18999 (glyph < last
19000 && glyph->type == STRETCH_GLYPH
19001 && glyph->voffset == voffset
19002 && glyph->face_id == face_id);
19003 ++glyph)
19004 s->width += glyph->pixel_width;
19006 /* Adjust base line for subscript/superscript text. */
19007 s->ybase += voffset;
19009 /* The case that face->gc == 0 is handled when drawing the glyph
19010 string by calling PREPARE_FACE_FOR_DISPLAY. */
19011 xassert (s->face);
19012 return glyph - s->row->glyphs[s->area];
19016 /* EXPORT for RIF:
19017 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19018 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19019 assumed to be zero. */
19021 void
19022 x_get_glyph_overhangs (glyph, f, left, right)
19023 struct glyph *glyph;
19024 struct frame *f;
19025 int *left, *right;
19027 *left = *right = 0;
19029 if (glyph->type == CHAR_GLYPH)
19031 XFontStruct *font;
19032 struct face *face;
19033 struct font_info *font_info;
19034 XChar2b char2b;
19035 XCharStruct *pcm;
19037 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19038 font = face->font;
19039 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19040 if (font /* ++KFS: Should this be font_info ? */
19041 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19043 if (pcm->rbearing > pcm->width)
19044 *right = pcm->rbearing - pcm->width;
19045 if (pcm->lbearing < 0)
19046 *left = -pcm->lbearing;
19052 /* Return the index of the first glyph preceding glyph string S that
19053 is overwritten by S because of S's left overhang. Value is -1
19054 if no glyphs are overwritten. */
19056 static int
19057 left_overwritten (s)
19058 struct glyph_string *s;
19060 int k;
19062 if (s->left_overhang)
19064 int x = 0, i;
19065 struct glyph *glyphs = s->row->glyphs[s->area];
19066 int first = s->first_glyph - glyphs;
19068 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19069 x -= glyphs[i].pixel_width;
19071 k = i + 1;
19073 else
19074 k = -1;
19076 return k;
19080 /* Return the index of the first glyph preceding glyph string S that
19081 is overwriting S because of its right overhang. Value is -1 if no
19082 glyph in front of S overwrites S. */
19084 static int
19085 left_overwriting (s)
19086 struct glyph_string *s;
19088 int i, k, x;
19089 struct glyph *glyphs = s->row->glyphs[s->area];
19090 int first = s->first_glyph - glyphs;
19092 k = -1;
19093 x = 0;
19094 for (i = first - 1; i >= 0; --i)
19096 int left, right;
19097 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19098 if (x + right > 0)
19099 k = i;
19100 x -= glyphs[i].pixel_width;
19103 return k;
19107 /* Return the index of the last glyph following glyph string S that is
19108 not overwritten by S because of S's right overhang. Value is -1 if
19109 no such glyph is found. */
19111 static int
19112 right_overwritten (s)
19113 struct glyph_string *s;
19115 int k = -1;
19117 if (s->right_overhang)
19119 int x = 0, i;
19120 struct glyph *glyphs = s->row->glyphs[s->area];
19121 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19122 int end = s->row->used[s->area];
19124 for (i = first; i < end && s->right_overhang > x; ++i)
19125 x += glyphs[i].pixel_width;
19127 k = i;
19130 return k;
19134 /* Return the index of the last glyph following glyph string S that
19135 overwrites S because of its left overhang. Value is negative
19136 if no such glyph is found. */
19138 static int
19139 right_overwriting (s)
19140 struct glyph_string *s;
19142 int i, k, x;
19143 int end = s->row->used[s->area];
19144 struct glyph *glyphs = s->row->glyphs[s->area];
19145 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19147 k = -1;
19148 x = 0;
19149 for (i = first; i < end; ++i)
19151 int left, right;
19152 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19153 if (x - left < 0)
19154 k = i;
19155 x += glyphs[i].pixel_width;
19158 return k;
19162 /* Get face and two-byte form of character C in face FACE_ID on frame
19163 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19164 means we want to display multibyte text. DISPLAY_P non-zero means
19165 make sure that X resources for the face returned are allocated.
19166 Value is a pointer to a realized face that is ready for display if
19167 DISPLAY_P is non-zero. */
19169 static INLINE struct face *
19170 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19171 struct frame *f;
19172 int c, face_id;
19173 XChar2b *char2b;
19174 int multibyte_p, display_p;
19176 struct face *face = FACE_FROM_ID (f, face_id);
19178 if (!multibyte_p)
19180 /* Unibyte case. We don't have to encode, but we have to make
19181 sure to use a face suitable for unibyte. */
19182 STORE_XCHAR2B (char2b, 0, c);
19183 face_id = FACE_FOR_CHAR (f, face, c);
19184 face = FACE_FROM_ID (f, face_id);
19186 else if (c < 128)
19188 /* Case of ASCII in a face known to fit ASCII. */
19189 STORE_XCHAR2B (char2b, 0, c);
19191 else
19193 int c1, c2, charset;
19195 /* Split characters into bytes. If c2 is -1 afterwards, C is
19196 really a one-byte character so that byte1 is zero. */
19197 SPLIT_CHAR (c, charset, c1, c2);
19198 if (c2 > 0)
19199 STORE_XCHAR2B (char2b, c1, c2);
19200 else
19201 STORE_XCHAR2B (char2b, 0, c1);
19203 /* Maybe encode the character in *CHAR2B. */
19204 if (face->font != NULL)
19206 struct font_info *font_info
19207 = FONT_INFO_FROM_ID (f, face->font_info_id);
19208 if (font_info)
19209 rif->encode_char (c, char2b, font_info, 0);
19213 /* Make sure X resources of the face are allocated. */
19214 #ifdef HAVE_X_WINDOWS
19215 if (display_p)
19216 #endif
19218 xassert (face != NULL);
19219 PREPARE_FACE_FOR_DISPLAY (f, face);
19222 return face;
19226 /* Set background width of glyph string S. START is the index of the
19227 first glyph following S. LAST_X is the right-most x-position + 1
19228 in the drawing area. */
19230 static INLINE void
19231 set_glyph_string_background_width (s, start, last_x)
19232 struct glyph_string *s;
19233 int start;
19234 int last_x;
19236 /* If the face of this glyph string has to be drawn to the end of
19237 the drawing area, set S->extends_to_end_of_line_p. */
19239 if (start == s->row->used[s->area]
19240 && s->area == TEXT_AREA
19241 && ((s->row->fill_line_p
19242 && (s->hl == DRAW_NORMAL_TEXT
19243 || s->hl == DRAW_IMAGE_RAISED
19244 || s->hl == DRAW_IMAGE_SUNKEN))
19245 || s->hl == DRAW_MOUSE_FACE))
19246 s->extends_to_end_of_line_p = 1;
19248 /* If S extends its face to the end of the line, set its
19249 background_width to the distance to the right edge of the drawing
19250 area. */
19251 if (s->extends_to_end_of_line_p)
19252 s->background_width = last_x - s->x + 1;
19253 else
19254 s->background_width = s->width;
19258 /* Compute overhangs and x-positions for glyph string S and its
19259 predecessors, or successors. X is the starting x-position for S.
19260 BACKWARD_P non-zero means process predecessors. */
19262 static void
19263 compute_overhangs_and_x (s, x, backward_p)
19264 struct glyph_string *s;
19265 int x;
19266 int backward_p;
19268 if (backward_p)
19270 while (s)
19272 if (rif->compute_glyph_string_overhangs)
19273 rif->compute_glyph_string_overhangs (s);
19274 x -= s->width;
19275 s->x = x;
19276 s = s->prev;
19279 else
19281 while (s)
19283 if (rif->compute_glyph_string_overhangs)
19284 rif->compute_glyph_string_overhangs (s);
19285 s->x = x;
19286 x += s->width;
19287 s = s->next;
19294 /* The following macros are only called from draw_glyphs below.
19295 They reference the following parameters of that function directly:
19296 `w', `row', `area', and `overlap_p'
19297 as well as the following local variables:
19298 `s', `f', and `hdc' (in W32) */
19300 #ifdef HAVE_NTGUI
19301 /* On W32, silently add local `hdc' variable to argument list of
19302 init_glyph_string. */
19303 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19304 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19305 #else
19306 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19307 init_glyph_string (s, char2b, w, row, area, start, hl)
19308 #endif
19310 /* Add a glyph string for a stretch glyph to the list of strings
19311 between HEAD and TAIL. START is the index of the stretch glyph in
19312 row area AREA of glyph row ROW. END is the index of the last glyph
19313 in that glyph row area. X is the current output position assigned
19314 to the new glyph string constructed. HL overrides that face of the
19315 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19316 is the right-most x-position of the drawing area. */
19318 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19319 and below -- keep them on one line. */
19320 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19321 do \
19323 s = (struct glyph_string *) alloca (sizeof *s); \
19324 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19325 START = fill_stretch_glyph_string (s, row, area, START, END); \
19326 append_glyph_string (&HEAD, &TAIL, s); \
19327 s->x = (X); \
19329 while (0)
19332 /* Add a glyph string for an image glyph to the list of strings
19333 between HEAD and TAIL. START is the index of the image glyph in
19334 row area AREA of glyph row ROW. END is the index of the last glyph
19335 in that glyph row area. X is the current output position assigned
19336 to the new glyph string constructed. HL overrides that face of the
19337 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19338 is the right-most x-position of the drawing area. */
19340 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19341 do \
19343 s = (struct glyph_string *) alloca (sizeof *s); \
19344 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19345 fill_image_glyph_string (s); \
19346 append_glyph_string (&HEAD, &TAIL, s); \
19347 ++START; \
19348 s->x = (X); \
19350 while (0)
19353 /* Add a glyph string for a sequence of character glyphs to the list
19354 of strings between HEAD and TAIL. START is the index of the first
19355 glyph in row area AREA of glyph row ROW that is part of the new
19356 glyph string. END is the index of the last glyph in that glyph row
19357 area. X is the current output position assigned to the new glyph
19358 string constructed. HL overrides that face of the glyph; e.g. it
19359 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19360 right-most x-position of the drawing area. */
19362 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19363 do \
19365 int c, face_id; \
19366 XChar2b *char2b; \
19368 c = (row)->glyphs[area][START].u.ch; \
19369 face_id = (row)->glyphs[area][START].face_id; \
19371 s = (struct glyph_string *) alloca (sizeof *s); \
19372 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19373 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19374 append_glyph_string (&HEAD, &TAIL, s); \
19375 s->x = (X); \
19376 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19378 while (0)
19381 /* Add a glyph string for a composite sequence to the list of strings
19382 between HEAD and TAIL. START is the index of the first glyph in
19383 row area AREA of glyph row ROW that is part of the new glyph
19384 string. END is the index of the last glyph in that glyph row area.
19385 X is the current output position assigned to the new glyph string
19386 constructed. HL overrides that face of the glyph; e.g. it is
19387 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19388 x-position of the drawing area. */
19390 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19391 do { \
19392 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19393 int face_id = (row)->glyphs[area][START].face_id; \
19394 struct face *base_face = FACE_FROM_ID (f, face_id); \
19395 struct composition *cmp = composition_table[cmp_id]; \
19396 int glyph_len = cmp->glyph_len; \
19397 XChar2b *char2b; \
19398 struct face **faces; \
19399 struct glyph_string *first_s = NULL; \
19400 int n; \
19402 base_face = base_face->ascii_face; \
19403 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19404 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19405 /* At first, fill in `char2b' and `faces'. */ \
19406 for (n = 0; n < glyph_len; n++) \
19408 int c = COMPOSITION_GLYPH (cmp, n); \
19409 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19410 faces[n] = FACE_FROM_ID (f, this_face_id); \
19411 get_char_face_and_encoding (f, c, this_face_id, \
19412 char2b + n, 1, 1); \
19415 /* Make glyph_strings for each glyph sequence that is drawable by \
19416 the same face, and append them to HEAD/TAIL. */ \
19417 for (n = 0; n < cmp->glyph_len;) \
19419 s = (struct glyph_string *) alloca (sizeof *s); \
19420 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19421 append_glyph_string (&(HEAD), &(TAIL), s); \
19422 s->cmp = cmp; \
19423 s->gidx = n; \
19424 s->x = (X); \
19426 if (n == 0) \
19427 first_s = s; \
19429 n = fill_composite_glyph_string (s, faces, overlaps); \
19432 ++START; \
19433 s = first_s; \
19434 } while (0)
19437 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19438 of AREA of glyph row ROW on window W between indices START and END.
19439 HL overrides the face for drawing glyph strings, e.g. it is
19440 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19441 x-positions of the drawing area.
19443 This is an ugly monster macro construct because we must use alloca
19444 to allocate glyph strings (because draw_glyphs can be called
19445 asynchronously). */
19447 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19448 do \
19450 HEAD = TAIL = NULL; \
19451 while (START < END) \
19453 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19454 switch (first_glyph->type) \
19456 case CHAR_GLYPH: \
19457 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19458 HL, X, LAST_X); \
19459 break; \
19461 case COMPOSITE_GLYPH: \
19462 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19463 HL, X, LAST_X); \
19464 break; \
19466 case STRETCH_GLYPH: \
19467 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19468 HL, X, LAST_X); \
19469 break; \
19471 case IMAGE_GLYPH: \
19472 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19473 HL, X, LAST_X); \
19474 break; \
19476 default: \
19477 abort (); \
19480 set_glyph_string_background_width (s, START, LAST_X); \
19481 (X) += s->width; \
19484 while (0)
19487 /* Draw glyphs between START and END in AREA of ROW on window W,
19488 starting at x-position X. X is relative to AREA in W. HL is a
19489 face-override with the following meaning:
19491 DRAW_NORMAL_TEXT draw normally
19492 DRAW_CURSOR draw in cursor face
19493 DRAW_MOUSE_FACE draw in mouse face.
19494 DRAW_INVERSE_VIDEO draw in mode line face
19495 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19496 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19498 If OVERLAPS is non-zero, draw only the foreground of characters and
19499 clip to the physical height of ROW. Non-zero value also defines
19500 the overlapping part to be drawn:
19502 OVERLAPS_PRED overlap with preceding rows
19503 OVERLAPS_SUCC overlap with succeeding rows
19504 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19505 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19507 Value is the x-position reached, relative to AREA of W. */
19509 static int
19510 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19511 struct window *w;
19512 int x;
19513 struct glyph_row *row;
19514 enum glyph_row_area area;
19515 int start, end;
19516 enum draw_glyphs_face hl;
19517 int overlaps;
19519 struct glyph_string *head, *tail;
19520 struct glyph_string *s;
19521 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19522 int last_x, area_width;
19523 int x_reached;
19524 int i, j;
19525 struct frame *f = XFRAME (WINDOW_FRAME (w));
19526 DECLARE_HDC (hdc);
19528 ALLOCATE_HDC (hdc, f);
19530 /* Let's rather be paranoid than getting a SEGV. */
19531 end = min (end, row->used[area]);
19532 start = max (0, start);
19533 start = min (end, start);
19535 /* Translate X to frame coordinates. Set last_x to the right
19536 end of the drawing area. */
19537 if (row->full_width_p)
19539 /* X is relative to the left edge of W, without scroll bars
19540 or fringes. */
19541 x += WINDOW_LEFT_EDGE_X (w);
19542 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19544 else
19546 int area_left = window_box_left (w, area);
19547 x += area_left;
19548 area_width = window_box_width (w, area);
19549 last_x = area_left + area_width;
19552 /* Build a doubly-linked list of glyph_string structures between
19553 head and tail from what we have to draw. Note that the macro
19554 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19555 the reason we use a separate variable `i'. */
19556 i = start;
19557 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19558 if (tail)
19559 x_reached = tail->x + tail->background_width;
19560 else
19561 x_reached = x;
19563 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19564 the row, redraw some glyphs in front or following the glyph
19565 strings built above. */
19566 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19568 int dummy_x = 0;
19569 struct glyph_string *h, *t;
19571 /* Compute overhangs for all glyph strings. */
19572 if (rif->compute_glyph_string_overhangs)
19573 for (s = head; s; s = s->next)
19574 rif->compute_glyph_string_overhangs (s);
19576 /* Prepend glyph strings for glyphs in front of the first glyph
19577 string that are overwritten because of the first glyph
19578 string's left overhang. The background of all strings
19579 prepended must be drawn because the first glyph string
19580 draws over it. */
19581 i = left_overwritten (head);
19582 if (i >= 0)
19584 j = i;
19585 BUILD_GLYPH_STRINGS (j, start, h, t,
19586 DRAW_NORMAL_TEXT, dummy_x, last_x);
19587 start = i;
19588 compute_overhangs_and_x (t, head->x, 1);
19589 prepend_glyph_string_lists (&head, &tail, h, t);
19590 clip_head = head;
19593 /* Prepend glyph strings for glyphs in front of the first glyph
19594 string that overwrite that glyph string because of their
19595 right overhang. For these strings, only the foreground must
19596 be drawn, because it draws over the glyph string at `head'.
19597 The background must not be drawn because this would overwrite
19598 right overhangs of preceding glyphs for which no glyph
19599 strings exist. */
19600 i = left_overwriting (head);
19601 if (i >= 0)
19603 clip_head = head;
19604 BUILD_GLYPH_STRINGS (i, start, h, t,
19605 DRAW_NORMAL_TEXT, dummy_x, last_x);
19606 for (s = h; s; s = s->next)
19607 s->background_filled_p = 1;
19608 compute_overhangs_and_x (t, head->x, 1);
19609 prepend_glyph_string_lists (&head, &tail, h, t);
19612 /* Append glyphs strings for glyphs following the last glyph
19613 string tail that are overwritten by tail. The background of
19614 these strings has to be drawn because tail's foreground draws
19615 over it. */
19616 i = right_overwritten (tail);
19617 if (i >= 0)
19619 BUILD_GLYPH_STRINGS (end, i, h, t,
19620 DRAW_NORMAL_TEXT, x, last_x);
19621 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19622 append_glyph_string_lists (&head, &tail, h, t);
19623 clip_tail = tail;
19626 /* Append glyph strings for glyphs following the last glyph
19627 string tail that overwrite tail. The foreground of such
19628 glyphs has to be drawn because it writes into the background
19629 of tail. The background must not be drawn because it could
19630 paint over the foreground of following glyphs. */
19631 i = right_overwriting (tail);
19632 if (i >= 0)
19634 clip_tail = tail;
19635 BUILD_GLYPH_STRINGS (end, i, h, t,
19636 DRAW_NORMAL_TEXT, x, last_x);
19637 for (s = h; s; s = s->next)
19638 s->background_filled_p = 1;
19639 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19640 append_glyph_string_lists (&head, &tail, h, t);
19642 if (clip_head || clip_tail)
19643 for (s = head; s; s = s->next)
19645 s->clip_head = clip_head;
19646 s->clip_tail = clip_tail;
19650 /* Draw all strings. */
19651 for (s = head; s; s = s->next)
19652 rif->draw_glyph_string (s);
19654 if (area == TEXT_AREA
19655 && !row->full_width_p
19656 /* When drawing overlapping rows, only the glyph strings'
19657 foreground is drawn, which doesn't erase a cursor
19658 completely. */
19659 && !overlaps)
19661 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19662 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19663 : (tail ? tail->x + tail->background_width : x));
19665 int text_left = window_box_left (w, TEXT_AREA);
19666 x0 -= text_left;
19667 x1 -= text_left;
19669 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19670 row->y, MATRIX_ROW_BOTTOM_Y (row));
19673 /* Value is the x-position up to which drawn, relative to AREA of W.
19674 This doesn't include parts drawn because of overhangs. */
19675 if (row->full_width_p)
19676 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19677 else
19678 x_reached -= window_box_left (w, area);
19680 RELEASE_HDC (hdc, f);
19682 return x_reached;
19685 /* Expand row matrix if too narrow. Don't expand if area
19686 is not present. */
19688 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19690 if (!fonts_changed_p \
19691 && (it->glyph_row->glyphs[area] \
19692 < it->glyph_row->glyphs[area + 1])) \
19694 it->w->ncols_scale_factor++; \
19695 fonts_changed_p = 1; \
19699 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19700 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19702 static INLINE void
19703 append_glyph (it)
19704 struct it *it;
19706 struct glyph *glyph;
19707 enum glyph_row_area area = it->area;
19709 xassert (it->glyph_row);
19710 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19712 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19713 if (glyph < it->glyph_row->glyphs[area + 1])
19715 glyph->charpos = CHARPOS (it->position);
19716 glyph->object = it->object;
19717 glyph->pixel_width = it->pixel_width;
19718 glyph->ascent = it->ascent;
19719 glyph->descent = it->descent;
19720 glyph->voffset = it->voffset;
19721 glyph->type = CHAR_GLYPH;
19722 glyph->multibyte_p = it->multibyte_p;
19723 glyph->left_box_line_p = it->start_of_box_run_p;
19724 glyph->right_box_line_p = it->end_of_box_run_p;
19725 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19726 || it->phys_descent > it->descent);
19727 glyph->padding_p = 0;
19728 glyph->glyph_not_available_p = it->glyph_not_available_p;
19729 glyph->face_id = it->face_id;
19730 glyph->u.ch = it->char_to_display;
19731 glyph->slice = null_glyph_slice;
19732 glyph->font_type = FONT_TYPE_UNKNOWN;
19733 ++it->glyph_row->used[area];
19735 else
19736 IT_EXPAND_MATRIX_WIDTH (it, area);
19739 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19740 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19742 static INLINE void
19743 append_composite_glyph (it)
19744 struct it *it;
19746 struct glyph *glyph;
19747 enum glyph_row_area area = it->area;
19749 xassert (it->glyph_row);
19751 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19752 if (glyph < it->glyph_row->glyphs[area + 1])
19754 glyph->charpos = CHARPOS (it->position);
19755 glyph->object = it->object;
19756 glyph->pixel_width = it->pixel_width;
19757 glyph->ascent = it->ascent;
19758 glyph->descent = it->descent;
19759 glyph->voffset = it->voffset;
19760 glyph->type = COMPOSITE_GLYPH;
19761 glyph->multibyte_p = it->multibyte_p;
19762 glyph->left_box_line_p = it->start_of_box_run_p;
19763 glyph->right_box_line_p = it->end_of_box_run_p;
19764 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19765 || it->phys_descent > it->descent);
19766 glyph->padding_p = 0;
19767 glyph->glyph_not_available_p = 0;
19768 glyph->face_id = it->face_id;
19769 glyph->u.cmp_id = it->cmp_id;
19770 glyph->slice = null_glyph_slice;
19771 glyph->font_type = FONT_TYPE_UNKNOWN;
19772 ++it->glyph_row->used[area];
19774 else
19775 IT_EXPAND_MATRIX_WIDTH (it, area);
19779 /* Change IT->ascent and IT->height according to the setting of
19780 IT->voffset. */
19782 static INLINE void
19783 take_vertical_position_into_account (it)
19784 struct it *it;
19786 if (it->voffset)
19788 if (it->voffset < 0)
19789 /* Increase the ascent so that we can display the text higher
19790 in the line. */
19791 it->ascent -= it->voffset;
19792 else
19793 /* Increase the descent so that we can display the text lower
19794 in the line. */
19795 it->descent += it->voffset;
19800 /* Produce glyphs/get display metrics for the image IT is loaded with.
19801 See the description of struct display_iterator in dispextern.h for
19802 an overview of struct display_iterator. */
19804 static void
19805 produce_image_glyph (it)
19806 struct it *it;
19808 struct image *img;
19809 struct face *face;
19810 int glyph_ascent, crop;
19811 struct glyph_slice slice;
19813 xassert (it->what == IT_IMAGE);
19815 face = FACE_FROM_ID (it->f, it->face_id);
19816 xassert (face);
19817 /* Make sure X resources of the face is loaded. */
19818 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19820 if (it->image_id < 0)
19822 /* Fringe bitmap. */
19823 it->ascent = it->phys_ascent = 0;
19824 it->descent = it->phys_descent = 0;
19825 it->pixel_width = 0;
19826 it->nglyphs = 0;
19827 return;
19830 img = IMAGE_FROM_ID (it->f, it->image_id);
19831 xassert (img);
19832 /* Make sure X resources of the image is loaded. */
19833 prepare_image_for_display (it->f, img);
19835 slice.x = slice.y = 0;
19836 slice.width = img->width;
19837 slice.height = img->height;
19839 if (INTEGERP (it->slice.x))
19840 slice.x = XINT (it->slice.x);
19841 else if (FLOATP (it->slice.x))
19842 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19844 if (INTEGERP (it->slice.y))
19845 slice.y = XINT (it->slice.y);
19846 else if (FLOATP (it->slice.y))
19847 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19849 if (INTEGERP (it->slice.width))
19850 slice.width = XINT (it->slice.width);
19851 else if (FLOATP (it->slice.width))
19852 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19854 if (INTEGERP (it->slice.height))
19855 slice.height = XINT (it->slice.height);
19856 else if (FLOATP (it->slice.height))
19857 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19859 if (slice.x >= img->width)
19860 slice.x = img->width;
19861 if (slice.y >= img->height)
19862 slice.y = img->height;
19863 if (slice.x + slice.width >= img->width)
19864 slice.width = img->width - slice.x;
19865 if (slice.y + slice.height > img->height)
19866 slice.height = img->height - slice.y;
19868 if (slice.width == 0 || slice.height == 0)
19869 return;
19871 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19873 it->descent = slice.height - glyph_ascent;
19874 if (slice.y == 0)
19875 it->descent += img->vmargin;
19876 if (slice.y + slice.height == img->height)
19877 it->descent += img->vmargin;
19878 it->phys_descent = it->descent;
19880 it->pixel_width = slice.width;
19881 if (slice.x == 0)
19882 it->pixel_width += img->hmargin;
19883 if (slice.x + slice.width == img->width)
19884 it->pixel_width += img->hmargin;
19886 /* It's quite possible for images to have an ascent greater than
19887 their height, so don't get confused in that case. */
19888 if (it->descent < 0)
19889 it->descent = 0;
19891 #if 0 /* this breaks image tiling */
19892 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19893 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19894 if (face_ascent > it->ascent)
19895 it->ascent = it->phys_ascent = face_ascent;
19896 #endif
19898 it->nglyphs = 1;
19900 if (face->box != FACE_NO_BOX)
19902 if (face->box_line_width > 0)
19904 if (slice.y == 0)
19905 it->ascent += face->box_line_width;
19906 if (slice.y + slice.height == img->height)
19907 it->descent += face->box_line_width;
19910 if (it->start_of_box_run_p && slice.x == 0)
19911 it->pixel_width += abs (face->box_line_width);
19912 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19913 it->pixel_width += abs (face->box_line_width);
19916 take_vertical_position_into_account (it);
19918 /* Automatically crop wide image glyphs at right edge so we can
19919 draw the cursor on same display row. */
19920 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
19921 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
19923 it->pixel_width -= crop;
19924 slice.width -= crop;
19927 if (it->glyph_row)
19929 struct glyph *glyph;
19930 enum glyph_row_area area = it->area;
19932 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19933 if (glyph < it->glyph_row->glyphs[area + 1])
19935 glyph->charpos = CHARPOS (it->position);
19936 glyph->object = it->object;
19937 glyph->pixel_width = it->pixel_width;
19938 glyph->ascent = glyph_ascent;
19939 glyph->descent = it->descent;
19940 glyph->voffset = it->voffset;
19941 glyph->type = IMAGE_GLYPH;
19942 glyph->multibyte_p = it->multibyte_p;
19943 glyph->left_box_line_p = it->start_of_box_run_p;
19944 glyph->right_box_line_p = it->end_of_box_run_p;
19945 glyph->overlaps_vertically_p = 0;
19946 glyph->padding_p = 0;
19947 glyph->glyph_not_available_p = 0;
19948 glyph->face_id = it->face_id;
19949 glyph->u.img_id = img->id;
19950 glyph->slice = slice;
19951 glyph->font_type = FONT_TYPE_UNKNOWN;
19952 ++it->glyph_row->used[area];
19954 else
19955 IT_EXPAND_MATRIX_WIDTH (it, area);
19960 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19961 of the glyph, WIDTH and HEIGHT are the width and height of the
19962 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19964 static void
19965 append_stretch_glyph (it, object, width, height, ascent)
19966 struct it *it;
19967 Lisp_Object object;
19968 int width, height;
19969 int ascent;
19971 struct glyph *glyph;
19972 enum glyph_row_area area = it->area;
19974 xassert (ascent >= 0 && ascent <= height);
19976 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19977 if (glyph < it->glyph_row->glyphs[area + 1])
19979 glyph->charpos = CHARPOS (it->position);
19980 glyph->object = object;
19981 glyph->pixel_width = width;
19982 glyph->ascent = ascent;
19983 glyph->descent = height - ascent;
19984 glyph->voffset = it->voffset;
19985 glyph->type = STRETCH_GLYPH;
19986 glyph->multibyte_p = it->multibyte_p;
19987 glyph->left_box_line_p = it->start_of_box_run_p;
19988 glyph->right_box_line_p = it->end_of_box_run_p;
19989 glyph->overlaps_vertically_p = 0;
19990 glyph->padding_p = 0;
19991 glyph->glyph_not_available_p = 0;
19992 glyph->face_id = it->face_id;
19993 glyph->u.stretch.ascent = ascent;
19994 glyph->u.stretch.height = height;
19995 glyph->slice = null_glyph_slice;
19996 glyph->font_type = FONT_TYPE_UNKNOWN;
19997 ++it->glyph_row->used[area];
19999 else
20000 IT_EXPAND_MATRIX_WIDTH (it, area);
20004 /* Produce a stretch glyph for iterator IT. IT->object is the value
20005 of the glyph property displayed. The value must be a list
20006 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20007 being recognized:
20009 1. `:width WIDTH' specifies that the space should be WIDTH *
20010 canonical char width wide. WIDTH may be an integer or floating
20011 point number.
20013 2. `:relative-width FACTOR' specifies that the width of the stretch
20014 should be computed from the width of the first character having the
20015 `glyph' property, and should be FACTOR times that width.
20017 3. `:align-to HPOS' specifies that the space should be wide enough
20018 to reach HPOS, a value in canonical character units.
20020 Exactly one of the above pairs must be present.
20022 4. `:height HEIGHT' specifies that the height of the stretch produced
20023 should be HEIGHT, measured in canonical character units.
20025 5. `:relative-height FACTOR' specifies that the height of the
20026 stretch should be FACTOR times the height of the characters having
20027 the glyph property.
20029 Either none or exactly one of 4 or 5 must be present.
20031 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20032 of the stretch should be used for the ascent of the stretch.
20033 ASCENT must be in the range 0 <= ASCENT <= 100. */
20035 static void
20036 produce_stretch_glyph (it)
20037 struct it *it;
20039 /* (space :width WIDTH :height HEIGHT ...) */
20040 Lisp_Object prop, plist;
20041 int width = 0, height = 0, align_to = -1;
20042 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20043 int ascent = 0;
20044 double tem;
20045 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20046 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20048 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20050 /* List should start with `space'. */
20051 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20052 plist = XCDR (it->object);
20054 /* Compute the width of the stretch. */
20055 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20056 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20058 /* Absolute width `:width WIDTH' specified and valid. */
20059 zero_width_ok_p = 1;
20060 width = (int)tem;
20062 else if (prop = Fplist_get (plist, QCrelative_width),
20063 NUMVAL (prop) > 0)
20065 /* Relative width `:relative-width FACTOR' specified and valid.
20066 Compute the width of the characters having the `glyph'
20067 property. */
20068 struct it it2;
20069 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20071 it2 = *it;
20072 if (it->multibyte_p)
20074 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20075 - IT_BYTEPOS (*it));
20076 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20078 else
20079 it2.c = *p, it2.len = 1;
20081 it2.glyph_row = NULL;
20082 it2.what = IT_CHARACTER;
20083 x_produce_glyphs (&it2);
20084 width = NUMVAL (prop) * it2.pixel_width;
20086 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20087 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20089 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20090 align_to = (align_to < 0
20092 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20093 else if (align_to < 0)
20094 align_to = window_box_left_offset (it->w, TEXT_AREA);
20095 width = max (0, (int)tem + align_to - it->current_x);
20096 zero_width_ok_p = 1;
20098 else
20099 /* Nothing specified -> width defaults to canonical char width. */
20100 width = FRAME_COLUMN_WIDTH (it->f);
20102 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20103 width = 1;
20105 /* Compute height. */
20106 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20107 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20109 height = (int)tem;
20110 zero_height_ok_p = 1;
20112 else if (prop = Fplist_get (plist, QCrelative_height),
20113 NUMVAL (prop) > 0)
20114 height = FONT_HEIGHT (font) * NUMVAL (prop);
20115 else
20116 height = FONT_HEIGHT (font);
20118 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20119 height = 1;
20121 /* Compute percentage of height used for ascent. If
20122 `:ascent ASCENT' is present and valid, use that. Otherwise,
20123 derive the ascent from the font in use. */
20124 if (prop = Fplist_get (plist, QCascent),
20125 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20126 ascent = height * NUMVAL (prop) / 100.0;
20127 else if (!NILP (prop)
20128 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20129 ascent = min (max (0, (int)tem), height);
20130 else
20131 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20133 if (width > 0 && !it->truncate_lines_p
20134 && it->current_x + width > it->last_visible_x)
20135 width = it->last_visible_x - it->current_x - 1;
20137 if (width > 0 && height > 0 && it->glyph_row)
20139 Lisp_Object object = it->stack[it->sp - 1].string;
20140 if (!STRINGP (object))
20141 object = it->w->buffer;
20142 append_stretch_glyph (it, object, width, height, ascent);
20145 it->pixel_width = width;
20146 it->ascent = it->phys_ascent = ascent;
20147 it->descent = it->phys_descent = height - it->ascent;
20148 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20150 take_vertical_position_into_account (it);
20153 /* Get line-height and line-spacing property at point.
20154 If line-height has format (HEIGHT TOTAL), return TOTAL
20155 in TOTAL_HEIGHT. */
20157 static Lisp_Object
20158 get_line_height_property (it, prop)
20159 struct it *it;
20160 Lisp_Object prop;
20162 Lisp_Object position;
20164 if (STRINGP (it->object))
20165 position = make_number (IT_STRING_CHARPOS (*it));
20166 else if (BUFFERP (it->object))
20167 position = make_number (IT_CHARPOS (*it));
20168 else
20169 return Qnil;
20171 return Fget_char_property (position, prop, it->object);
20174 /* Calculate line-height and line-spacing properties.
20175 An integer value specifies explicit pixel value.
20176 A float value specifies relative value to current face height.
20177 A cons (float . face-name) specifies relative value to
20178 height of specified face font.
20180 Returns height in pixels, or nil. */
20183 static Lisp_Object
20184 calc_line_height_property (it, val, font, boff, override)
20185 struct it *it;
20186 Lisp_Object val;
20187 XFontStruct *font;
20188 int boff, override;
20190 Lisp_Object face_name = Qnil;
20191 int ascent, descent, height;
20193 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20194 return val;
20196 if (CONSP (val))
20198 face_name = XCAR (val);
20199 val = XCDR (val);
20200 if (!NUMBERP (val))
20201 val = make_number (1);
20202 if (NILP (face_name))
20204 height = it->ascent + it->descent;
20205 goto scale;
20209 if (NILP (face_name))
20211 font = FRAME_FONT (it->f);
20212 boff = FRAME_BASELINE_OFFSET (it->f);
20214 else if (EQ (face_name, Qt))
20216 override = 0;
20218 else
20220 int face_id;
20221 struct face *face;
20222 struct font_info *font_info;
20224 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20225 if (face_id < 0)
20226 return make_number (-1);
20228 face = FACE_FROM_ID (it->f, face_id);
20229 font = face->font;
20230 if (font == NULL)
20231 return make_number (-1);
20233 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20234 boff = font_info->baseline_offset;
20235 if (font_info->vertical_centering)
20236 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20239 ascent = FONT_BASE (font) + boff;
20240 descent = FONT_DESCENT (font) - boff;
20242 if (override)
20244 it->override_ascent = ascent;
20245 it->override_descent = descent;
20246 it->override_boff = boff;
20249 height = ascent + descent;
20251 scale:
20252 if (FLOATP (val))
20253 height = (int)(XFLOAT_DATA (val) * height);
20254 else if (INTEGERP (val))
20255 height *= XINT (val);
20257 return make_number (height);
20261 /* RIF:
20262 Produce glyphs/get display metrics for the display element IT is
20263 loaded with. See the description of struct it in dispextern.h
20264 for an overview of struct it. */
20266 void
20267 x_produce_glyphs (it)
20268 struct it *it;
20270 int extra_line_spacing = it->extra_line_spacing;
20272 it->glyph_not_available_p = 0;
20274 if (it->what == IT_CHARACTER)
20276 XChar2b char2b;
20277 XFontStruct *font;
20278 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20279 XCharStruct *pcm;
20280 int font_not_found_p;
20281 struct font_info *font_info;
20282 int boff; /* baseline offset */
20283 /* We may change it->multibyte_p upon unibyte<->multibyte
20284 conversion. So, save the current value now and restore it
20285 later.
20287 Note: It seems that we don't have to record multibyte_p in
20288 struct glyph because the character code itself tells if or
20289 not the character is multibyte. Thus, in the future, we must
20290 consider eliminating the field `multibyte_p' in the struct
20291 glyph. */
20292 int saved_multibyte_p = it->multibyte_p;
20294 /* Maybe translate single-byte characters to multibyte, or the
20295 other way. */
20296 it->char_to_display = it->c;
20297 if (!ASCII_BYTE_P (it->c))
20299 if (unibyte_display_via_language_environment
20300 && SINGLE_BYTE_CHAR_P (it->c)
20301 && (it->c >= 0240
20302 || !NILP (Vnonascii_translation_table)))
20304 it->char_to_display = unibyte_char_to_multibyte (it->c);
20305 it->multibyte_p = 1;
20306 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20307 face = FACE_FROM_ID (it->f, it->face_id);
20309 else if (!SINGLE_BYTE_CHAR_P (it->c)
20310 && !it->multibyte_p)
20312 it->multibyte_p = 1;
20313 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20314 face = FACE_FROM_ID (it->f, it->face_id);
20318 /* Get font to use. Encode IT->char_to_display. */
20319 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20320 &char2b, it->multibyte_p, 0);
20321 font = face->font;
20323 /* When no suitable font found, use the default font. */
20324 font_not_found_p = font == NULL;
20325 if (font_not_found_p)
20327 font = FRAME_FONT (it->f);
20328 boff = FRAME_BASELINE_OFFSET (it->f);
20329 font_info = NULL;
20331 else
20333 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20334 boff = font_info->baseline_offset;
20335 if (font_info->vertical_centering)
20336 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20339 if (it->char_to_display >= ' '
20340 && (!it->multibyte_p || it->char_to_display < 128))
20342 /* Either unibyte or ASCII. */
20343 int stretched_p;
20345 it->nglyphs = 1;
20347 pcm = rif->per_char_metric (font, &char2b,
20348 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20350 if (it->override_ascent >= 0)
20352 it->ascent = it->override_ascent;
20353 it->descent = it->override_descent;
20354 boff = it->override_boff;
20356 else
20358 it->ascent = FONT_BASE (font) + boff;
20359 it->descent = FONT_DESCENT (font) - boff;
20362 if (pcm)
20364 it->phys_ascent = pcm->ascent + boff;
20365 it->phys_descent = pcm->descent - boff;
20366 it->pixel_width = pcm->width;
20368 else
20370 it->glyph_not_available_p = 1;
20371 it->phys_ascent = it->ascent;
20372 it->phys_descent = it->descent;
20373 it->pixel_width = FONT_WIDTH (font);
20376 if (it->constrain_row_ascent_descent_p)
20378 if (it->descent > it->max_descent)
20380 it->ascent += it->descent - it->max_descent;
20381 it->descent = it->max_descent;
20383 if (it->ascent > it->max_ascent)
20385 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20386 it->ascent = it->max_ascent;
20388 it->phys_ascent = min (it->phys_ascent, it->ascent);
20389 it->phys_descent = min (it->phys_descent, it->descent);
20390 extra_line_spacing = 0;
20393 /* If this is a space inside a region of text with
20394 `space-width' property, change its width. */
20395 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20396 if (stretched_p)
20397 it->pixel_width *= XFLOATINT (it->space_width);
20399 /* If face has a box, add the box thickness to the character
20400 height. If character has a box line to the left and/or
20401 right, add the box line width to the character's width. */
20402 if (face->box != FACE_NO_BOX)
20404 int thick = face->box_line_width;
20406 if (thick > 0)
20408 it->ascent += thick;
20409 it->descent += thick;
20411 else
20412 thick = -thick;
20414 if (it->start_of_box_run_p)
20415 it->pixel_width += thick;
20416 if (it->end_of_box_run_p)
20417 it->pixel_width += thick;
20420 /* If face has an overline, add the height of the overline
20421 (1 pixel) and a 1 pixel margin to the character height. */
20422 if (face->overline_p)
20423 it->ascent += overline_margin;
20425 if (it->constrain_row_ascent_descent_p)
20427 if (it->ascent > it->max_ascent)
20428 it->ascent = it->max_ascent;
20429 if (it->descent > it->max_descent)
20430 it->descent = it->max_descent;
20433 take_vertical_position_into_account (it);
20435 /* If we have to actually produce glyphs, do it. */
20436 if (it->glyph_row)
20438 if (stretched_p)
20440 /* Translate a space with a `space-width' property
20441 into a stretch glyph. */
20442 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20443 / FONT_HEIGHT (font));
20444 append_stretch_glyph (it, it->object, it->pixel_width,
20445 it->ascent + it->descent, ascent);
20447 else
20448 append_glyph (it);
20450 /* If characters with lbearing or rbearing are displayed
20451 in this line, record that fact in a flag of the
20452 glyph row. This is used to optimize X output code. */
20453 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20454 it->glyph_row->contains_overlapping_glyphs_p = 1;
20457 else if (it->char_to_display == '\n')
20459 /* A newline has no width but we need the height of the line.
20460 But if previous part of the line set a height, don't
20461 increase that height */
20463 Lisp_Object height;
20464 Lisp_Object total_height = Qnil;
20466 it->override_ascent = -1;
20467 it->pixel_width = 0;
20468 it->nglyphs = 0;
20470 height = get_line_height_property(it, Qline_height);
20471 /* Split (line-height total-height) list */
20472 if (CONSP (height)
20473 && CONSP (XCDR (height))
20474 && NILP (XCDR (XCDR (height))))
20476 total_height = XCAR (XCDR (height));
20477 height = XCAR (height);
20479 height = calc_line_height_property(it, height, font, boff, 1);
20481 if (it->override_ascent >= 0)
20483 it->ascent = it->override_ascent;
20484 it->descent = it->override_descent;
20485 boff = it->override_boff;
20487 else
20489 it->ascent = FONT_BASE (font) + boff;
20490 it->descent = FONT_DESCENT (font) - boff;
20493 if (EQ (height, Qt))
20495 if (it->descent > it->max_descent)
20497 it->ascent += it->descent - it->max_descent;
20498 it->descent = it->max_descent;
20500 if (it->ascent > it->max_ascent)
20502 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20503 it->ascent = it->max_ascent;
20505 it->phys_ascent = min (it->phys_ascent, it->ascent);
20506 it->phys_descent = min (it->phys_descent, it->descent);
20507 it->constrain_row_ascent_descent_p = 1;
20508 extra_line_spacing = 0;
20510 else
20512 Lisp_Object spacing;
20514 it->phys_ascent = it->ascent;
20515 it->phys_descent = it->descent;
20517 if ((it->max_ascent > 0 || it->max_descent > 0)
20518 && face->box != FACE_NO_BOX
20519 && face->box_line_width > 0)
20521 it->ascent += face->box_line_width;
20522 it->descent += face->box_line_width;
20524 if (!NILP (height)
20525 && XINT (height) > it->ascent + it->descent)
20526 it->ascent = XINT (height) - it->descent;
20528 if (!NILP (total_height))
20529 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20530 else
20532 spacing = get_line_height_property(it, Qline_spacing);
20533 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20535 if (INTEGERP (spacing))
20537 extra_line_spacing = XINT (spacing);
20538 if (!NILP (total_height))
20539 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20543 else if (it->char_to_display == '\t')
20545 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20546 int x = it->current_x + it->continuation_lines_width;
20547 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20549 /* If the distance from the current position to the next tab
20550 stop is less than a space character width, use the
20551 tab stop after that. */
20552 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20553 next_tab_x += tab_width;
20555 it->pixel_width = next_tab_x - x;
20556 it->nglyphs = 1;
20557 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20558 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20560 if (it->glyph_row)
20562 append_stretch_glyph (it, it->object, it->pixel_width,
20563 it->ascent + it->descent, it->ascent);
20566 else
20568 /* A multi-byte character. Assume that the display width of the
20569 character is the width of the character multiplied by the
20570 width of the font. */
20572 /* If we found a font, this font should give us the right
20573 metrics. If we didn't find a font, use the frame's
20574 default font and calculate the width of the character
20575 from the charset width; this is what old redisplay code
20576 did. */
20578 pcm = rif->per_char_metric (font, &char2b,
20579 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20581 if (font_not_found_p || !pcm)
20583 int charset = CHAR_CHARSET (it->char_to_display);
20585 it->glyph_not_available_p = 1;
20586 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20587 * CHARSET_WIDTH (charset));
20588 it->phys_ascent = FONT_BASE (font) + boff;
20589 it->phys_descent = FONT_DESCENT (font) - boff;
20591 else
20593 it->pixel_width = pcm->width;
20594 it->phys_ascent = pcm->ascent + boff;
20595 it->phys_descent = pcm->descent - boff;
20596 if (it->glyph_row
20597 && (pcm->lbearing < 0
20598 || pcm->rbearing > pcm->width))
20599 it->glyph_row->contains_overlapping_glyphs_p = 1;
20601 it->nglyphs = 1;
20602 it->ascent = FONT_BASE (font) + boff;
20603 it->descent = FONT_DESCENT (font) - boff;
20604 if (face->box != FACE_NO_BOX)
20606 int thick = face->box_line_width;
20608 if (thick > 0)
20610 it->ascent += thick;
20611 it->descent += thick;
20613 else
20614 thick = - thick;
20616 if (it->start_of_box_run_p)
20617 it->pixel_width += thick;
20618 if (it->end_of_box_run_p)
20619 it->pixel_width += thick;
20622 /* If face has an overline, add the height of the overline
20623 (1 pixel) and a 1 pixel margin to the character height. */
20624 if (face->overline_p)
20625 it->ascent += overline_margin;
20627 take_vertical_position_into_account (it);
20629 if (it->glyph_row)
20630 append_glyph (it);
20632 it->multibyte_p = saved_multibyte_p;
20634 else if (it->what == IT_COMPOSITION)
20636 /* Note: A composition is represented as one glyph in the
20637 glyph matrix. There are no padding glyphs. */
20638 XChar2b char2b;
20639 XFontStruct *font;
20640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20641 XCharStruct *pcm;
20642 int font_not_found_p;
20643 struct font_info *font_info;
20644 int boff; /* baseline offset */
20645 struct composition *cmp = composition_table[it->cmp_id];
20647 /* Maybe translate single-byte characters to multibyte. */
20648 it->char_to_display = it->c;
20649 if (unibyte_display_via_language_environment
20650 && SINGLE_BYTE_CHAR_P (it->c)
20651 && (it->c >= 0240
20652 || (it->c >= 0200
20653 && !NILP (Vnonascii_translation_table))))
20655 it->char_to_display = unibyte_char_to_multibyte (it->c);
20658 /* Get face and font to use. Encode IT->char_to_display. */
20659 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20660 face = FACE_FROM_ID (it->f, it->face_id);
20661 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20662 &char2b, it->multibyte_p, 0);
20663 font = face->font;
20665 /* When no suitable font found, use the default font. */
20666 font_not_found_p = font == NULL;
20667 if (font_not_found_p)
20669 font = FRAME_FONT (it->f);
20670 boff = FRAME_BASELINE_OFFSET (it->f);
20671 font_info = NULL;
20673 else
20675 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20676 boff = font_info->baseline_offset;
20677 if (font_info->vertical_centering)
20678 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20681 /* There are no padding glyphs, so there is only one glyph to
20682 produce for the composition. Important is that pixel_width,
20683 ascent and descent are the values of what is drawn by
20684 draw_glyphs (i.e. the values of the overall glyphs composed). */
20685 it->nglyphs = 1;
20687 /* If we have not yet calculated pixel size data of glyphs of
20688 the composition for the current face font, calculate them
20689 now. Theoretically, we have to check all fonts for the
20690 glyphs, but that requires much time and memory space. So,
20691 here we check only the font of the first glyph. This leads
20692 to incorrect display very rarely, and C-l (recenter) can
20693 correct the display anyway. */
20694 if (cmp->font != (void *) font)
20696 /* Ascent and descent of the font of the first character of
20697 this composition (adjusted by baseline offset). Ascent
20698 and descent of overall glyphs should not be less than
20699 them respectively. */
20700 int font_ascent = FONT_BASE (font) + boff;
20701 int font_descent = FONT_DESCENT (font) - boff;
20702 /* Bounding box of the overall glyphs. */
20703 int leftmost, rightmost, lowest, highest;
20704 int i, width, ascent, descent;
20706 cmp->font = (void *) font;
20708 /* Initialize the bounding box. */
20709 if (font_info
20710 && (pcm = rif->per_char_metric (font, &char2b,
20711 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20713 width = pcm->width;
20714 ascent = pcm->ascent;
20715 descent = pcm->descent;
20717 else
20719 width = FONT_WIDTH (font);
20720 ascent = FONT_BASE (font);
20721 descent = FONT_DESCENT (font);
20724 rightmost = width;
20725 lowest = - descent + boff;
20726 highest = ascent + boff;
20727 leftmost = 0;
20729 if (font_info
20730 && font_info->default_ascent
20731 && CHAR_TABLE_P (Vuse_default_ascent)
20732 && !NILP (Faref (Vuse_default_ascent,
20733 make_number (it->char_to_display))))
20734 highest = font_info->default_ascent + boff;
20736 /* Draw the first glyph at the normal position. It may be
20737 shifted to right later if some other glyphs are drawn at
20738 the left. */
20739 cmp->offsets[0] = 0;
20740 cmp->offsets[1] = boff;
20742 /* Set cmp->offsets for the remaining glyphs. */
20743 for (i = 1; i < cmp->glyph_len; i++)
20745 int left, right, btm, top;
20746 int ch = COMPOSITION_GLYPH (cmp, i);
20747 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20749 face = FACE_FROM_ID (it->f, face_id);
20750 get_char_face_and_encoding (it->f, ch, face->id,
20751 &char2b, it->multibyte_p, 0);
20752 font = face->font;
20753 if (font == NULL)
20755 font = FRAME_FONT (it->f);
20756 boff = FRAME_BASELINE_OFFSET (it->f);
20757 font_info = NULL;
20759 else
20761 font_info
20762 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20763 boff = font_info->baseline_offset;
20764 if (font_info->vertical_centering)
20765 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20768 if (font_info
20769 && (pcm = rif->per_char_metric (font, &char2b,
20770 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20772 width = pcm->width;
20773 ascent = pcm->ascent;
20774 descent = pcm->descent;
20776 else
20778 width = FONT_WIDTH (font);
20779 ascent = 1;
20780 descent = 0;
20783 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20785 /* Relative composition with or without
20786 alternate chars. */
20787 left = (leftmost + rightmost - width) / 2;
20788 btm = - descent + boff;
20789 if (font_info && font_info->relative_compose
20790 && (! CHAR_TABLE_P (Vignore_relative_composition)
20791 || NILP (Faref (Vignore_relative_composition,
20792 make_number (ch)))))
20795 if (- descent >= font_info->relative_compose)
20796 /* One extra pixel between two glyphs. */
20797 btm = highest + 1;
20798 else if (ascent <= 0)
20799 /* One extra pixel between two glyphs. */
20800 btm = lowest - 1 - ascent - descent;
20803 else
20805 /* A composition rule is specified by an integer
20806 value that encodes global and new reference
20807 points (GREF and NREF). GREF and NREF are
20808 specified by numbers as below:
20810 0---1---2 -- ascent
20814 9--10--11 -- center
20816 ---3---4---5--- baseline
20818 6---7---8 -- descent
20820 int rule = COMPOSITION_RULE (cmp, i);
20821 int gref, nref, grefx, grefy, nrefx, nrefy;
20823 COMPOSITION_DECODE_RULE (rule, gref, nref);
20824 grefx = gref % 3, nrefx = nref % 3;
20825 grefy = gref / 3, nrefy = nref / 3;
20827 left = (leftmost
20828 + grefx * (rightmost - leftmost) / 2
20829 - nrefx * width / 2);
20830 btm = ((grefy == 0 ? highest
20831 : grefy == 1 ? 0
20832 : grefy == 2 ? lowest
20833 : (highest + lowest) / 2)
20834 - (nrefy == 0 ? ascent + descent
20835 : nrefy == 1 ? descent - boff
20836 : nrefy == 2 ? 0
20837 : (ascent + descent) / 2));
20840 cmp->offsets[i * 2] = left;
20841 cmp->offsets[i * 2 + 1] = btm + descent;
20843 /* Update the bounding box of the overall glyphs. */
20844 right = left + width;
20845 top = btm + descent + ascent;
20846 if (left < leftmost)
20847 leftmost = left;
20848 if (right > rightmost)
20849 rightmost = right;
20850 if (top > highest)
20851 highest = top;
20852 if (btm < lowest)
20853 lowest = btm;
20856 /* If there are glyphs whose x-offsets are negative,
20857 shift all glyphs to the right and make all x-offsets
20858 non-negative. */
20859 if (leftmost < 0)
20861 for (i = 0; i < cmp->glyph_len; i++)
20862 cmp->offsets[i * 2] -= leftmost;
20863 rightmost -= leftmost;
20866 cmp->pixel_width = rightmost;
20867 cmp->ascent = highest;
20868 cmp->descent = - lowest;
20869 if (cmp->ascent < font_ascent)
20870 cmp->ascent = font_ascent;
20871 if (cmp->descent < font_descent)
20872 cmp->descent = font_descent;
20875 it->pixel_width = cmp->pixel_width;
20876 it->ascent = it->phys_ascent = cmp->ascent;
20877 it->descent = it->phys_descent = cmp->descent;
20879 if (face->box != FACE_NO_BOX)
20881 int thick = face->box_line_width;
20883 if (thick > 0)
20885 it->ascent += thick;
20886 it->descent += thick;
20888 else
20889 thick = - thick;
20891 if (it->start_of_box_run_p)
20892 it->pixel_width += thick;
20893 if (it->end_of_box_run_p)
20894 it->pixel_width += thick;
20897 /* If face has an overline, add the height of the overline
20898 (1 pixel) and a 1 pixel margin to the character height. */
20899 if (face->overline_p)
20900 it->ascent += overline_margin;
20902 take_vertical_position_into_account (it);
20904 if (it->glyph_row)
20905 append_composite_glyph (it);
20907 else if (it->what == IT_IMAGE)
20908 produce_image_glyph (it);
20909 else if (it->what == IT_STRETCH)
20910 produce_stretch_glyph (it);
20912 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20913 because this isn't true for images with `:ascent 100'. */
20914 xassert (it->ascent >= 0 && it->descent >= 0);
20915 if (it->area == TEXT_AREA)
20916 it->current_x += it->pixel_width;
20918 if (extra_line_spacing > 0)
20920 it->descent += extra_line_spacing;
20921 if (extra_line_spacing > it->max_extra_line_spacing)
20922 it->max_extra_line_spacing = extra_line_spacing;
20925 it->max_ascent = max (it->max_ascent, it->ascent);
20926 it->max_descent = max (it->max_descent, it->descent);
20927 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20928 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20931 /* EXPORT for RIF:
20932 Output LEN glyphs starting at START at the nominal cursor position.
20933 Advance the nominal cursor over the text. The global variable
20934 updated_window contains the window being updated, updated_row is
20935 the glyph row being updated, and updated_area is the area of that
20936 row being updated. */
20938 void
20939 x_write_glyphs (start, len)
20940 struct glyph *start;
20941 int len;
20943 int x, hpos;
20945 xassert (updated_window && updated_row);
20946 BLOCK_INPUT;
20948 /* Write glyphs. */
20950 hpos = start - updated_row->glyphs[updated_area];
20951 x = draw_glyphs (updated_window, output_cursor.x,
20952 updated_row, updated_area,
20953 hpos, hpos + len,
20954 DRAW_NORMAL_TEXT, 0);
20956 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20957 if (updated_area == TEXT_AREA
20958 && updated_window->phys_cursor_on_p
20959 && updated_window->phys_cursor.vpos == output_cursor.vpos
20960 && updated_window->phys_cursor.hpos >= hpos
20961 && updated_window->phys_cursor.hpos < hpos + len)
20962 updated_window->phys_cursor_on_p = 0;
20964 UNBLOCK_INPUT;
20966 /* Advance the output cursor. */
20967 output_cursor.hpos += len;
20968 output_cursor.x = x;
20972 /* EXPORT for RIF:
20973 Insert LEN glyphs from START at the nominal cursor position. */
20975 void
20976 x_insert_glyphs (start, len)
20977 struct glyph *start;
20978 int len;
20980 struct frame *f;
20981 struct window *w;
20982 int line_height, shift_by_width, shifted_region_width;
20983 struct glyph_row *row;
20984 struct glyph *glyph;
20985 int frame_x, frame_y, hpos;
20987 xassert (updated_window && updated_row);
20988 BLOCK_INPUT;
20989 w = updated_window;
20990 f = XFRAME (WINDOW_FRAME (w));
20992 /* Get the height of the line we are in. */
20993 row = updated_row;
20994 line_height = row->height;
20996 /* Get the width of the glyphs to insert. */
20997 shift_by_width = 0;
20998 for (glyph = start; glyph < start + len; ++glyph)
20999 shift_by_width += glyph->pixel_width;
21001 /* Get the width of the region to shift right. */
21002 shifted_region_width = (window_box_width (w, updated_area)
21003 - output_cursor.x
21004 - shift_by_width);
21006 /* Shift right. */
21007 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21008 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21010 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21011 line_height, shift_by_width);
21013 /* Write the glyphs. */
21014 hpos = start - row->glyphs[updated_area];
21015 draw_glyphs (w, output_cursor.x, row, updated_area,
21016 hpos, hpos + len,
21017 DRAW_NORMAL_TEXT, 0);
21019 /* Advance the output cursor. */
21020 output_cursor.hpos += len;
21021 output_cursor.x += shift_by_width;
21022 UNBLOCK_INPUT;
21026 /* EXPORT for RIF:
21027 Erase the current text line from the nominal cursor position
21028 (inclusive) to pixel column TO_X (exclusive). The idea is that
21029 everything from TO_X onward is already erased.
21031 TO_X is a pixel position relative to updated_area of
21032 updated_window. TO_X == -1 means clear to the end of this area. */
21034 void
21035 x_clear_end_of_line (to_x)
21036 int to_x;
21038 struct frame *f;
21039 struct window *w = updated_window;
21040 int max_x, min_y, max_y;
21041 int from_x, from_y, to_y;
21043 xassert (updated_window && updated_row);
21044 f = XFRAME (w->frame);
21046 if (updated_row->full_width_p)
21047 max_x = WINDOW_TOTAL_WIDTH (w);
21048 else
21049 max_x = window_box_width (w, updated_area);
21050 max_y = window_text_bottom_y (w);
21052 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21053 of window. For TO_X > 0, truncate to end of drawing area. */
21054 if (to_x == 0)
21055 return;
21056 else if (to_x < 0)
21057 to_x = max_x;
21058 else
21059 to_x = min (to_x, max_x);
21061 to_y = min (max_y, output_cursor.y + updated_row->height);
21063 /* Notice if the cursor will be cleared by this operation. */
21064 if (!updated_row->full_width_p)
21065 notice_overwritten_cursor (w, updated_area,
21066 output_cursor.x, -1,
21067 updated_row->y,
21068 MATRIX_ROW_BOTTOM_Y (updated_row));
21070 from_x = output_cursor.x;
21072 /* Translate to frame coordinates. */
21073 if (updated_row->full_width_p)
21075 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21076 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21078 else
21080 int area_left = window_box_left (w, updated_area);
21081 from_x += area_left;
21082 to_x += area_left;
21085 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21086 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21087 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21089 /* Prevent inadvertently clearing to end of the X window. */
21090 if (to_x > from_x && to_y > from_y)
21092 BLOCK_INPUT;
21093 rif->clear_frame_area (f, from_x, from_y,
21094 to_x - from_x, to_y - from_y);
21095 UNBLOCK_INPUT;
21099 #endif /* HAVE_WINDOW_SYSTEM */
21103 /***********************************************************************
21104 Cursor types
21105 ***********************************************************************/
21107 /* Value is the internal representation of the specified cursor type
21108 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21109 of the bar cursor. */
21111 static enum text_cursor_kinds
21112 get_specified_cursor_type (arg, width)
21113 Lisp_Object arg;
21114 int *width;
21116 enum text_cursor_kinds type;
21118 if (NILP (arg))
21119 return NO_CURSOR;
21121 if (EQ (arg, Qbox))
21122 return FILLED_BOX_CURSOR;
21124 if (EQ (arg, Qhollow))
21125 return HOLLOW_BOX_CURSOR;
21127 if (EQ (arg, Qbar))
21129 *width = 2;
21130 return BAR_CURSOR;
21133 if (CONSP (arg)
21134 && EQ (XCAR (arg), Qbar)
21135 && INTEGERP (XCDR (arg))
21136 && XINT (XCDR (arg)) >= 0)
21138 *width = XINT (XCDR (arg));
21139 return BAR_CURSOR;
21142 if (EQ (arg, Qhbar))
21144 *width = 2;
21145 return HBAR_CURSOR;
21148 if (CONSP (arg)
21149 && EQ (XCAR (arg), Qhbar)
21150 && INTEGERP (XCDR (arg))
21151 && XINT (XCDR (arg)) >= 0)
21153 *width = XINT (XCDR (arg));
21154 return HBAR_CURSOR;
21157 /* Treat anything unknown as "hollow box cursor".
21158 It was bad to signal an error; people have trouble fixing
21159 .Xdefaults with Emacs, when it has something bad in it. */
21160 type = HOLLOW_BOX_CURSOR;
21162 return type;
21165 /* Set the default cursor types for specified frame. */
21166 void
21167 set_frame_cursor_types (f, arg)
21168 struct frame *f;
21169 Lisp_Object arg;
21171 int width;
21172 Lisp_Object tem;
21174 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21175 FRAME_CURSOR_WIDTH (f) = width;
21177 /* By default, set up the blink-off state depending on the on-state. */
21179 tem = Fassoc (arg, Vblink_cursor_alist);
21180 if (!NILP (tem))
21182 FRAME_BLINK_OFF_CURSOR (f)
21183 = get_specified_cursor_type (XCDR (tem), &width);
21184 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21186 else
21187 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21191 /* Return the cursor we want to be displayed in window W. Return
21192 width of bar/hbar cursor through WIDTH arg. Return with
21193 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21194 (i.e. if the `system caret' should track this cursor).
21196 In a mini-buffer window, we want the cursor only to appear if we
21197 are reading input from this window. For the selected window, we
21198 want the cursor type given by the frame parameter or buffer local
21199 setting of cursor-type. If explicitly marked off, draw no cursor.
21200 In all other cases, we want a hollow box cursor. */
21202 static enum text_cursor_kinds
21203 get_window_cursor_type (w, glyph, width, active_cursor)
21204 struct window *w;
21205 struct glyph *glyph;
21206 int *width;
21207 int *active_cursor;
21209 struct frame *f = XFRAME (w->frame);
21210 struct buffer *b = XBUFFER (w->buffer);
21211 int cursor_type = DEFAULT_CURSOR;
21212 Lisp_Object alt_cursor;
21213 int non_selected = 0;
21215 *active_cursor = 1;
21217 /* Echo area */
21218 if (cursor_in_echo_area
21219 && FRAME_HAS_MINIBUF_P (f)
21220 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21222 if (w == XWINDOW (echo_area_window))
21224 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21226 *width = FRAME_CURSOR_WIDTH (f);
21227 return FRAME_DESIRED_CURSOR (f);
21229 else
21230 return get_specified_cursor_type (b->cursor_type, width);
21233 *active_cursor = 0;
21234 non_selected = 1;
21237 /* Nonselected window or nonselected frame. */
21238 else if (w != XWINDOW (f->selected_window)
21239 #ifdef HAVE_WINDOW_SYSTEM
21240 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21241 #endif
21244 *active_cursor = 0;
21246 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21247 return NO_CURSOR;
21249 non_selected = 1;
21252 /* Never display a cursor in a window in which cursor-type is nil. */
21253 if (NILP (b->cursor_type))
21254 return NO_CURSOR;
21256 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21257 if (non_selected)
21259 alt_cursor = b->cursor_in_non_selected_windows;
21260 return get_specified_cursor_type (alt_cursor, width);
21263 /* Get the normal cursor type for this window. */
21264 if (EQ (b->cursor_type, Qt))
21266 cursor_type = FRAME_DESIRED_CURSOR (f);
21267 *width = FRAME_CURSOR_WIDTH (f);
21269 else
21270 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21272 /* Use normal cursor if not blinked off. */
21273 if (!w->cursor_off_p)
21275 #ifdef HAVE_WINDOW_SYSTEM
21276 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21278 if (cursor_type == FILLED_BOX_CURSOR)
21280 /* Using a block cursor on large images can be very annoying.
21281 So use a hollow cursor for "large" images.
21282 If image is not transparent (no mask), also use hollow cursor. */
21283 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21284 if (img != NULL && IMAGEP (img->spec))
21286 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21287 where N = size of default frame font size.
21288 This should cover most of the "tiny" icons people may use. */
21289 if (!img->mask
21290 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21291 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21292 cursor_type = HOLLOW_BOX_CURSOR;
21295 else if (cursor_type != NO_CURSOR)
21297 /* Display current only supports BOX and HOLLOW cursors for images.
21298 So for now, unconditionally use a HOLLOW cursor when cursor is
21299 not a solid box cursor. */
21300 cursor_type = HOLLOW_BOX_CURSOR;
21303 #endif
21304 return cursor_type;
21307 /* Cursor is blinked off, so determine how to "toggle" it. */
21309 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21310 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21311 return get_specified_cursor_type (XCDR (alt_cursor), width);
21313 /* Then see if frame has specified a specific blink off cursor type. */
21314 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21316 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21317 return FRAME_BLINK_OFF_CURSOR (f);
21320 #if 0
21321 /* Some people liked having a permanently visible blinking cursor,
21322 while others had very strong opinions against it. So it was
21323 decided to remove it. KFS 2003-09-03 */
21325 /* Finally perform built-in cursor blinking:
21326 filled box <-> hollow box
21327 wide [h]bar <-> narrow [h]bar
21328 narrow [h]bar <-> no cursor
21329 other type <-> no cursor */
21331 if (cursor_type == FILLED_BOX_CURSOR)
21332 return HOLLOW_BOX_CURSOR;
21334 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21336 *width = 1;
21337 return cursor_type;
21339 #endif
21341 return NO_CURSOR;
21345 #ifdef HAVE_WINDOW_SYSTEM
21347 /* Notice when the text cursor of window W has been completely
21348 overwritten by a drawing operation that outputs glyphs in AREA
21349 starting at X0 and ending at X1 in the line starting at Y0 and
21350 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21351 the rest of the line after X0 has been written. Y coordinates
21352 are window-relative. */
21354 static void
21355 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21356 struct window *w;
21357 enum glyph_row_area area;
21358 int x0, y0, x1, y1;
21360 int cx0, cx1, cy0, cy1;
21361 struct glyph_row *row;
21363 if (!w->phys_cursor_on_p)
21364 return;
21365 if (area != TEXT_AREA)
21366 return;
21368 if (w->phys_cursor.vpos < 0
21369 || w->phys_cursor.vpos >= w->current_matrix->nrows
21370 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21371 !(row->enabled_p && row->displays_text_p)))
21372 return;
21374 if (row->cursor_in_fringe_p)
21376 row->cursor_in_fringe_p = 0;
21377 draw_fringe_bitmap (w, row, 0);
21378 w->phys_cursor_on_p = 0;
21379 return;
21382 cx0 = w->phys_cursor.x;
21383 cx1 = cx0 + w->phys_cursor_width;
21384 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21385 return;
21387 /* The cursor image will be completely removed from the
21388 screen if the output area intersects the cursor area in
21389 y-direction. When we draw in [y0 y1[, and some part of
21390 the cursor is at y < y0, that part must have been drawn
21391 before. When scrolling, the cursor is erased before
21392 actually scrolling, so we don't come here. When not
21393 scrolling, the rows above the old cursor row must have
21394 changed, and in this case these rows must have written
21395 over the cursor image.
21397 Likewise if part of the cursor is below y1, with the
21398 exception of the cursor being in the first blank row at
21399 the buffer and window end because update_text_area
21400 doesn't draw that row. (Except when it does, but
21401 that's handled in update_text_area.) */
21403 cy0 = w->phys_cursor.y;
21404 cy1 = cy0 + w->phys_cursor_height;
21405 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21406 return;
21408 w->phys_cursor_on_p = 0;
21411 #endif /* HAVE_WINDOW_SYSTEM */
21414 /************************************************************************
21415 Mouse Face
21416 ************************************************************************/
21418 #ifdef HAVE_WINDOW_SYSTEM
21420 /* EXPORT for RIF:
21421 Fix the display of area AREA of overlapping row ROW in window W
21422 with respect to the overlapping part OVERLAPS. */
21424 void
21425 x_fix_overlapping_area (w, row, area, overlaps)
21426 struct window *w;
21427 struct glyph_row *row;
21428 enum glyph_row_area area;
21429 int overlaps;
21431 int i, x;
21433 BLOCK_INPUT;
21435 x = 0;
21436 for (i = 0; i < row->used[area];)
21438 if (row->glyphs[area][i].overlaps_vertically_p)
21440 int start = i, start_x = x;
21444 x += row->glyphs[area][i].pixel_width;
21445 ++i;
21447 while (i < row->used[area]
21448 && row->glyphs[area][i].overlaps_vertically_p);
21450 draw_glyphs (w, start_x, row, area,
21451 start, i,
21452 DRAW_NORMAL_TEXT, overlaps);
21454 else
21456 x += row->glyphs[area][i].pixel_width;
21457 ++i;
21461 UNBLOCK_INPUT;
21465 /* EXPORT:
21466 Draw the cursor glyph of window W in glyph row ROW. See the
21467 comment of draw_glyphs for the meaning of HL. */
21469 void
21470 draw_phys_cursor_glyph (w, row, hl)
21471 struct window *w;
21472 struct glyph_row *row;
21473 enum draw_glyphs_face hl;
21475 /* If cursor hpos is out of bounds, don't draw garbage. This can
21476 happen in mini-buffer windows when switching between echo area
21477 glyphs and mini-buffer. */
21478 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21480 int on_p = w->phys_cursor_on_p;
21481 int x1;
21482 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21483 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21484 hl, 0);
21485 w->phys_cursor_on_p = on_p;
21487 if (hl == DRAW_CURSOR)
21488 w->phys_cursor_width = x1 - w->phys_cursor.x;
21489 /* When we erase the cursor, and ROW is overlapped by other
21490 rows, make sure that these overlapping parts of other rows
21491 are redrawn. */
21492 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21494 w->phys_cursor_width = x1 - w->phys_cursor.x;
21496 if (row > w->current_matrix->rows
21497 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21498 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21499 OVERLAPS_ERASED_CURSOR);
21501 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21502 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21503 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21504 OVERLAPS_ERASED_CURSOR);
21510 /* EXPORT:
21511 Erase the image of a cursor of window W from the screen. */
21513 void
21514 erase_phys_cursor (w)
21515 struct window *w;
21517 struct frame *f = XFRAME (w->frame);
21518 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21519 int hpos = w->phys_cursor.hpos;
21520 int vpos = w->phys_cursor.vpos;
21521 int mouse_face_here_p = 0;
21522 struct glyph_matrix *active_glyphs = w->current_matrix;
21523 struct glyph_row *cursor_row;
21524 struct glyph *cursor_glyph;
21525 enum draw_glyphs_face hl;
21527 /* No cursor displayed or row invalidated => nothing to do on the
21528 screen. */
21529 if (w->phys_cursor_type == NO_CURSOR)
21530 goto mark_cursor_off;
21532 /* VPOS >= active_glyphs->nrows means that window has been resized.
21533 Don't bother to erase the cursor. */
21534 if (vpos >= active_glyphs->nrows)
21535 goto mark_cursor_off;
21537 /* If row containing cursor is marked invalid, there is nothing we
21538 can do. */
21539 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21540 if (!cursor_row->enabled_p)
21541 goto mark_cursor_off;
21543 /* If line spacing is > 0, old cursor may only be partially visible in
21544 window after split-window. So adjust visible height. */
21545 cursor_row->visible_height = min (cursor_row->visible_height,
21546 window_text_bottom_y (w) - cursor_row->y);
21548 /* If row is completely invisible, don't attempt to delete a cursor which
21549 isn't there. This can happen if cursor is at top of a window, and
21550 we switch to a buffer with a header line in that window. */
21551 if (cursor_row->visible_height <= 0)
21552 goto mark_cursor_off;
21554 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21555 if (cursor_row->cursor_in_fringe_p)
21557 cursor_row->cursor_in_fringe_p = 0;
21558 draw_fringe_bitmap (w, cursor_row, 0);
21559 goto mark_cursor_off;
21562 /* This can happen when the new row is shorter than the old one.
21563 In this case, either draw_glyphs or clear_end_of_line
21564 should have cleared the cursor. Note that we wouldn't be
21565 able to erase the cursor in this case because we don't have a
21566 cursor glyph at hand. */
21567 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21568 goto mark_cursor_off;
21570 /* If the cursor is in the mouse face area, redisplay that when
21571 we clear the cursor. */
21572 if (! NILP (dpyinfo->mouse_face_window)
21573 && w == XWINDOW (dpyinfo->mouse_face_window)
21574 && (vpos > dpyinfo->mouse_face_beg_row
21575 || (vpos == dpyinfo->mouse_face_beg_row
21576 && hpos >= dpyinfo->mouse_face_beg_col))
21577 && (vpos < dpyinfo->mouse_face_end_row
21578 || (vpos == dpyinfo->mouse_face_end_row
21579 && hpos < dpyinfo->mouse_face_end_col))
21580 /* Don't redraw the cursor's spot in mouse face if it is at the
21581 end of a line (on a newline). The cursor appears there, but
21582 mouse highlighting does not. */
21583 && cursor_row->used[TEXT_AREA] > hpos)
21584 mouse_face_here_p = 1;
21586 /* Maybe clear the display under the cursor. */
21587 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21589 int x, y, left_x;
21590 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21591 int width;
21593 cursor_glyph = get_phys_cursor_glyph (w);
21594 if (cursor_glyph == NULL)
21595 goto mark_cursor_off;
21597 width = cursor_glyph->pixel_width;
21598 left_x = window_box_left_offset (w, TEXT_AREA);
21599 x = w->phys_cursor.x;
21600 if (x < left_x)
21601 width -= left_x - x;
21602 width = min (width, window_box_width (w, TEXT_AREA) - x);
21603 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21604 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21606 if (width > 0)
21607 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21610 /* Erase the cursor by redrawing the character underneath it. */
21611 if (mouse_face_here_p)
21612 hl = DRAW_MOUSE_FACE;
21613 else
21614 hl = DRAW_NORMAL_TEXT;
21615 draw_phys_cursor_glyph (w, cursor_row, hl);
21617 mark_cursor_off:
21618 w->phys_cursor_on_p = 0;
21619 w->phys_cursor_type = NO_CURSOR;
21623 /* EXPORT:
21624 Display or clear cursor of window W. If ON is zero, clear the
21625 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21626 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21628 void
21629 display_and_set_cursor (w, on, hpos, vpos, x, y)
21630 struct window *w;
21631 int on, hpos, vpos, x, y;
21633 struct frame *f = XFRAME (w->frame);
21634 int new_cursor_type;
21635 int new_cursor_width;
21636 int active_cursor;
21637 struct glyph_row *glyph_row;
21638 struct glyph *glyph;
21640 /* This is pointless on invisible frames, and dangerous on garbaged
21641 windows and frames; in the latter case, the frame or window may
21642 be in the midst of changing its size, and x and y may be off the
21643 window. */
21644 if (! FRAME_VISIBLE_P (f)
21645 || FRAME_GARBAGED_P (f)
21646 || vpos >= w->current_matrix->nrows
21647 || hpos >= w->current_matrix->matrix_w)
21648 return;
21650 /* If cursor is off and we want it off, return quickly. */
21651 if (!on && !w->phys_cursor_on_p)
21652 return;
21654 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21655 /* If cursor row is not enabled, we don't really know where to
21656 display the cursor. */
21657 if (!glyph_row->enabled_p)
21659 w->phys_cursor_on_p = 0;
21660 return;
21663 glyph = NULL;
21664 if (!glyph_row->exact_window_width_line_p
21665 || hpos < glyph_row->used[TEXT_AREA])
21666 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21668 xassert (interrupt_input_blocked);
21670 /* Set new_cursor_type to the cursor we want to be displayed. */
21671 new_cursor_type = get_window_cursor_type (w, glyph,
21672 &new_cursor_width, &active_cursor);
21674 /* If cursor is currently being shown and we don't want it to be or
21675 it is in the wrong place, or the cursor type is not what we want,
21676 erase it. */
21677 if (w->phys_cursor_on_p
21678 && (!on
21679 || w->phys_cursor.x != x
21680 || w->phys_cursor.y != y
21681 || new_cursor_type != w->phys_cursor_type
21682 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21683 && new_cursor_width != w->phys_cursor_width)))
21684 erase_phys_cursor (w);
21686 /* Don't check phys_cursor_on_p here because that flag is only set
21687 to zero in some cases where we know that the cursor has been
21688 completely erased, to avoid the extra work of erasing the cursor
21689 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21690 still not be visible, or it has only been partly erased. */
21691 if (on)
21693 w->phys_cursor_ascent = glyph_row->ascent;
21694 w->phys_cursor_height = glyph_row->height;
21696 /* Set phys_cursor_.* before x_draw_.* is called because some
21697 of them may need the information. */
21698 w->phys_cursor.x = x;
21699 w->phys_cursor.y = glyph_row->y;
21700 w->phys_cursor.hpos = hpos;
21701 w->phys_cursor.vpos = vpos;
21704 rif->draw_window_cursor (w, glyph_row, x, y,
21705 new_cursor_type, new_cursor_width,
21706 on, active_cursor);
21710 /* Switch the display of W's cursor on or off, according to the value
21711 of ON. */
21713 static void
21714 update_window_cursor (w, on)
21715 struct window *w;
21716 int on;
21718 /* Don't update cursor in windows whose frame is in the process
21719 of being deleted. */
21720 if (w->current_matrix)
21722 BLOCK_INPUT;
21723 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21724 w->phys_cursor.x, w->phys_cursor.y);
21725 UNBLOCK_INPUT;
21730 /* Call update_window_cursor with parameter ON_P on all leaf windows
21731 in the window tree rooted at W. */
21733 static void
21734 update_cursor_in_window_tree (w, on_p)
21735 struct window *w;
21736 int on_p;
21738 while (w)
21740 if (!NILP (w->hchild))
21741 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21742 else if (!NILP (w->vchild))
21743 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21744 else
21745 update_window_cursor (w, on_p);
21747 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21752 /* EXPORT:
21753 Display the cursor on window W, or clear it, according to ON_P.
21754 Don't change the cursor's position. */
21756 void
21757 x_update_cursor (f, on_p)
21758 struct frame *f;
21759 int on_p;
21761 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21765 /* EXPORT:
21766 Clear the cursor of window W to background color, and mark the
21767 cursor as not shown. This is used when the text where the cursor
21768 is is about to be rewritten. */
21770 void
21771 x_clear_cursor (w)
21772 struct window *w;
21774 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21775 update_window_cursor (w, 0);
21779 /* EXPORT:
21780 Display the active region described by mouse_face_* according to DRAW. */
21782 void
21783 show_mouse_face (dpyinfo, draw)
21784 Display_Info *dpyinfo;
21785 enum draw_glyphs_face draw;
21787 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21788 struct frame *f = XFRAME (WINDOW_FRAME (w));
21790 if (/* If window is in the process of being destroyed, don't bother
21791 to do anything. */
21792 w->current_matrix != NULL
21793 /* Don't update mouse highlight if hidden */
21794 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21795 /* Recognize when we are called to operate on rows that don't exist
21796 anymore. This can happen when a window is split. */
21797 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21799 int phys_cursor_on_p = w->phys_cursor_on_p;
21800 struct glyph_row *row, *first, *last;
21802 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21803 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21805 for (row = first; row <= last && row->enabled_p; ++row)
21807 int start_hpos, end_hpos, start_x;
21809 /* For all but the first row, the highlight starts at column 0. */
21810 if (row == first)
21812 start_hpos = dpyinfo->mouse_face_beg_col;
21813 start_x = dpyinfo->mouse_face_beg_x;
21815 else
21817 start_hpos = 0;
21818 start_x = 0;
21821 if (row == last)
21822 end_hpos = dpyinfo->mouse_face_end_col;
21823 else
21825 end_hpos = row->used[TEXT_AREA];
21826 if (draw == DRAW_NORMAL_TEXT)
21827 row->fill_line_p = 1; /* Clear to end of line */
21830 if (end_hpos > start_hpos)
21832 draw_glyphs (w, start_x, row, TEXT_AREA,
21833 start_hpos, end_hpos,
21834 draw, 0);
21836 row->mouse_face_p
21837 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21841 /* When we've written over the cursor, arrange for it to
21842 be displayed again. */
21843 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21845 BLOCK_INPUT;
21846 display_and_set_cursor (w, 1,
21847 w->phys_cursor.hpos, w->phys_cursor.vpos,
21848 w->phys_cursor.x, w->phys_cursor.y);
21849 UNBLOCK_INPUT;
21853 /* Change the mouse cursor. */
21854 if (draw == DRAW_NORMAL_TEXT)
21855 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21856 else if (draw == DRAW_MOUSE_FACE)
21857 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21858 else
21859 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21862 /* EXPORT:
21863 Clear out the mouse-highlighted active region.
21864 Redraw it un-highlighted first. Value is non-zero if mouse
21865 face was actually drawn unhighlighted. */
21868 clear_mouse_face (dpyinfo)
21869 Display_Info *dpyinfo;
21871 int cleared = 0;
21873 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21875 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21876 cleared = 1;
21879 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21880 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21881 dpyinfo->mouse_face_window = Qnil;
21882 dpyinfo->mouse_face_overlay = Qnil;
21883 return cleared;
21887 /* EXPORT:
21888 Non-zero if physical cursor of window W is within mouse face. */
21891 cursor_in_mouse_face_p (w)
21892 struct window *w;
21894 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21895 int in_mouse_face = 0;
21897 if (WINDOWP (dpyinfo->mouse_face_window)
21898 && XWINDOW (dpyinfo->mouse_face_window) == w)
21900 int hpos = w->phys_cursor.hpos;
21901 int vpos = w->phys_cursor.vpos;
21903 if (vpos >= dpyinfo->mouse_face_beg_row
21904 && vpos <= dpyinfo->mouse_face_end_row
21905 && (vpos > dpyinfo->mouse_face_beg_row
21906 || hpos >= dpyinfo->mouse_face_beg_col)
21907 && (vpos < dpyinfo->mouse_face_end_row
21908 || hpos < dpyinfo->mouse_face_end_col
21909 || dpyinfo->mouse_face_past_end))
21910 in_mouse_face = 1;
21913 return in_mouse_face;
21919 /* Find the glyph matrix position of buffer position CHARPOS in window
21920 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21921 current glyphs must be up to date. If CHARPOS is above window
21922 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21923 of last line in W. In the row containing CHARPOS, stop before glyphs
21924 having STOP as object. */
21926 #if 1 /* This is a version of fast_find_position that's more correct
21927 in the presence of hscrolling, for example. I didn't install
21928 it right away because the problem fixed is minor, it failed
21929 in 20.x as well, and I think it's too risky to install
21930 so near the release of 21.1. 2001-09-25 gerd. */
21932 static int
21933 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21934 struct window *w;
21935 int charpos;
21936 int *hpos, *vpos, *x, *y;
21937 Lisp_Object stop;
21939 struct glyph_row *row, *first;
21940 struct glyph *glyph, *end;
21941 int past_end = 0;
21943 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21944 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21946 *x = first->x;
21947 *y = first->y;
21948 *hpos = 0;
21949 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21950 return 1;
21953 row = row_containing_pos (w, charpos, first, NULL, 0);
21954 if (row == NULL)
21956 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21957 past_end = 1;
21960 /* If whole rows or last part of a row came from a display overlay,
21961 row_containing_pos will skip over such rows because their end pos
21962 equals the start pos of the overlay or interval.
21964 Move back if we have a STOP object and previous row's
21965 end glyph came from STOP. */
21966 if (!NILP (stop))
21968 struct glyph_row *prev;
21969 while ((prev = row - 1, prev >= first)
21970 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21971 && prev->used[TEXT_AREA] > 0)
21973 struct glyph *beg = prev->glyphs[TEXT_AREA];
21974 glyph = beg + prev->used[TEXT_AREA];
21975 while (--glyph >= beg
21976 && INTEGERP (glyph->object));
21977 if (glyph < beg
21978 || !EQ (stop, glyph->object))
21979 break;
21980 row = prev;
21984 *x = row->x;
21985 *y = row->y;
21986 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21988 glyph = row->glyphs[TEXT_AREA];
21989 end = glyph + row->used[TEXT_AREA];
21991 /* Skip over glyphs not having an object at the start of the row.
21992 These are special glyphs like truncation marks on terminal
21993 frames. */
21994 if (row->displays_text_p)
21995 while (glyph < end
21996 && INTEGERP (glyph->object)
21997 && !EQ (stop, glyph->object)
21998 && glyph->charpos < 0)
22000 *x += glyph->pixel_width;
22001 ++glyph;
22004 while (glyph < end
22005 && !INTEGERP (glyph->object)
22006 && !EQ (stop, glyph->object)
22007 && (!BUFFERP (glyph->object)
22008 || glyph->charpos < charpos))
22010 *x += glyph->pixel_width;
22011 ++glyph;
22014 *hpos = glyph - row->glyphs[TEXT_AREA];
22015 return !past_end;
22018 #else /* not 1 */
22020 static int
22021 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22022 struct window *w;
22023 int pos;
22024 int *hpos, *vpos, *x, *y;
22025 Lisp_Object stop;
22027 int i;
22028 int lastcol;
22029 int maybe_next_line_p = 0;
22030 int line_start_position;
22031 int yb = window_text_bottom_y (w);
22032 struct glyph_row *row, *best_row;
22033 int row_vpos, best_row_vpos;
22034 int current_x;
22036 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22037 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22039 while (row->y < yb)
22041 if (row->used[TEXT_AREA])
22042 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22043 else
22044 line_start_position = 0;
22046 if (line_start_position > pos)
22047 break;
22048 /* If the position sought is the end of the buffer,
22049 don't include the blank lines at the bottom of the window. */
22050 else if (line_start_position == pos
22051 && pos == BUF_ZV (XBUFFER (w->buffer)))
22053 maybe_next_line_p = 1;
22054 break;
22056 else if (line_start_position > 0)
22058 best_row = row;
22059 best_row_vpos = row_vpos;
22062 if (row->y + row->height >= yb)
22063 break;
22065 ++row;
22066 ++row_vpos;
22069 /* Find the right column within BEST_ROW. */
22070 lastcol = 0;
22071 current_x = best_row->x;
22072 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22074 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22075 int charpos = glyph->charpos;
22077 if (BUFFERP (glyph->object))
22079 if (charpos == pos)
22081 *hpos = i;
22082 *vpos = best_row_vpos;
22083 *x = current_x;
22084 *y = best_row->y;
22085 return 1;
22087 else if (charpos > pos)
22088 break;
22090 else if (EQ (glyph->object, stop))
22091 break;
22093 if (charpos > 0)
22094 lastcol = i;
22095 current_x += glyph->pixel_width;
22098 /* If we're looking for the end of the buffer,
22099 and we didn't find it in the line we scanned,
22100 use the start of the following line. */
22101 if (maybe_next_line_p)
22103 ++best_row;
22104 ++best_row_vpos;
22105 lastcol = 0;
22106 current_x = best_row->x;
22109 *vpos = best_row_vpos;
22110 *hpos = lastcol + 1;
22111 *x = current_x;
22112 *y = best_row->y;
22113 return 0;
22116 #endif /* not 1 */
22119 /* Find the position of the glyph for position POS in OBJECT in
22120 window W's current matrix, and return in *X, *Y the pixel
22121 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22123 RIGHT_P non-zero means return the position of the right edge of the
22124 glyph, RIGHT_P zero means return the left edge position.
22126 If no glyph for POS exists in the matrix, return the position of
22127 the glyph with the next smaller position that is in the matrix, if
22128 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22129 exists in the matrix, return the position of the glyph with the
22130 next larger position in OBJECT.
22132 Value is non-zero if a glyph was found. */
22134 static int
22135 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22136 struct window *w;
22137 int pos;
22138 Lisp_Object object;
22139 int *hpos, *vpos, *x, *y;
22140 int right_p;
22142 int yb = window_text_bottom_y (w);
22143 struct glyph_row *r;
22144 struct glyph *best_glyph = NULL;
22145 struct glyph_row *best_row = NULL;
22146 int best_x = 0;
22148 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22149 r->enabled_p && r->y < yb;
22150 ++r)
22152 struct glyph *g = r->glyphs[TEXT_AREA];
22153 struct glyph *e = g + r->used[TEXT_AREA];
22154 int gx;
22156 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22157 if (EQ (g->object, object))
22159 if (g->charpos == pos)
22161 best_glyph = g;
22162 best_x = gx;
22163 best_row = r;
22164 goto found;
22166 else if (best_glyph == NULL
22167 || ((abs (g->charpos - pos)
22168 < abs (best_glyph->charpos - pos))
22169 && (right_p
22170 ? g->charpos < pos
22171 : g->charpos > pos)))
22173 best_glyph = g;
22174 best_x = gx;
22175 best_row = r;
22180 found:
22182 if (best_glyph)
22184 *x = best_x;
22185 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22187 if (right_p)
22189 *x += best_glyph->pixel_width;
22190 ++*hpos;
22193 *y = best_row->y;
22194 *vpos = best_row - w->current_matrix->rows;
22197 return best_glyph != NULL;
22201 /* See if position X, Y is within a hot-spot of an image. */
22203 static int
22204 on_hot_spot_p (hot_spot, x, y)
22205 Lisp_Object hot_spot;
22206 int x, y;
22208 if (!CONSP (hot_spot))
22209 return 0;
22211 if (EQ (XCAR (hot_spot), Qrect))
22213 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22214 Lisp_Object rect = XCDR (hot_spot);
22215 Lisp_Object tem;
22216 if (!CONSP (rect))
22217 return 0;
22218 if (!CONSP (XCAR (rect)))
22219 return 0;
22220 if (!CONSP (XCDR (rect)))
22221 return 0;
22222 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22223 return 0;
22224 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22225 return 0;
22226 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22227 return 0;
22228 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22229 return 0;
22230 return 1;
22232 else if (EQ (XCAR (hot_spot), Qcircle))
22234 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22235 Lisp_Object circ = XCDR (hot_spot);
22236 Lisp_Object lr, lx0, ly0;
22237 if (CONSP (circ)
22238 && CONSP (XCAR (circ))
22239 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22240 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22241 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22243 double r = XFLOATINT (lr);
22244 double dx = XINT (lx0) - x;
22245 double dy = XINT (ly0) - y;
22246 return (dx * dx + dy * dy <= r * r);
22249 else if (EQ (XCAR (hot_spot), Qpoly))
22251 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22252 if (VECTORP (XCDR (hot_spot)))
22254 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22255 Lisp_Object *poly = v->contents;
22256 int n = v->size;
22257 int i;
22258 int inside = 0;
22259 Lisp_Object lx, ly;
22260 int x0, y0;
22262 /* Need an even number of coordinates, and at least 3 edges. */
22263 if (n < 6 || n & 1)
22264 return 0;
22266 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22267 If count is odd, we are inside polygon. Pixels on edges
22268 may or may not be included depending on actual geometry of the
22269 polygon. */
22270 if ((lx = poly[n-2], !INTEGERP (lx))
22271 || (ly = poly[n-1], !INTEGERP (lx)))
22272 return 0;
22273 x0 = XINT (lx), y0 = XINT (ly);
22274 for (i = 0; i < n; i += 2)
22276 int x1 = x0, y1 = y0;
22277 if ((lx = poly[i], !INTEGERP (lx))
22278 || (ly = poly[i+1], !INTEGERP (ly)))
22279 return 0;
22280 x0 = XINT (lx), y0 = XINT (ly);
22282 /* Does this segment cross the X line? */
22283 if (x0 >= x)
22285 if (x1 >= x)
22286 continue;
22288 else if (x1 < x)
22289 continue;
22290 if (y > y0 && y > y1)
22291 continue;
22292 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22293 inside = !inside;
22295 return inside;
22298 /* If we don't understand the format, pretend we're not in the hot-spot. */
22299 return 0;
22302 Lisp_Object
22303 find_hot_spot (map, x, y)
22304 Lisp_Object map;
22305 int x, y;
22307 while (CONSP (map))
22309 if (CONSP (XCAR (map))
22310 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22311 return XCAR (map);
22312 map = XCDR (map);
22315 return Qnil;
22318 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22319 3, 3, 0,
22320 doc: /* Lookup in image map MAP coordinates X and Y.
22321 An image map is an alist where each element has the format (AREA ID PLIST).
22322 An AREA is specified as either a rectangle, a circle, or a polygon:
22323 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22324 pixel coordinates of the upper left and bottom right corners.
22325 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22326 and the radius of the circle; r may be a float or integer.
22327 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22328 vector describes one corner in the polygon.
22329 Returns the alist element for the first matching AREA in MAP. */)
22330 (map, x, y)
22331 Lisp_Object map;
22332 Lisp_Object x, y;
22334 if (NILP (map))
22335 return Qnil;
22337 CHECK_NUMBER (x);
22338 CHECK_NUMBER (y);
22340 return find_hot_spot (map, XINT (x), XINT (y));
22344 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22345 static void
22346 define_frame_cursor1 (f, cursor, pointer)
22347 struct frame *f;
22348 Cursor cursor;
22349 Lisp_Object pointer;
22351 /* Do not change cursor shape while dragging mouse. */
22352 if (!NILP (do_mouse_tracking))
22353 return;
22355 if (!NILP (pointer))
22357 if (EQ (pointer, Qarrow))
22358 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22359 else if (EQ (pointer, Qhand))
22360 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22361 else if (EQ (pointer, Qtext))
22362 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22363 else if (EQ (pointer, intern ("hdrag")))
22364 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22365 #ifdef HAVE_X_WINDOWS
22366 else if (EQ (pointer, intern ("vdrag")))
22367 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22368 #endif
22369 else if (EQ (pointer, intern ("hourglass")))
22370 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22371 else if (EQ (pointer, Qmodeline))
22372 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22373 else
22374 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22377 if (cursor != No_Cursor)
22378 rif->define_frame_cursor (f, cursor);
22381 /* Take proper action when mouse has moved to the mode or header line
22382 or marginal area AREA of window W, x-position X and y-position Y.
22383 X is relative to the start of the text display area of W, so the
22384 width of bitmap areas and scroll bars must be subtracted to get a
22385 position relative to the start of the mode line. */
22387 static void
22388 note_mode_line_or_margin_highlight (window, x, y, area)
22389 Lisp_Object window;
22390 int x, y;
22391 enum window_part area;
22393 struct window *w = XWINDOW (window);
22394 struct frame *f = XFRAME (w->frame);
22395 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22396 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22397 Lisp_Object pointer = Qnil;
22398 int charpos, dx, dy, width, height;
22399 Lisp_Object string, object = Qnil;
22400 Lisp_Object pos, help;
22402 Lisp_Object mouse_face;
22403 int original_x_pixel = x;
22404 struct glyph * glyph = NULL;
22405 struct glyph_row *row;
22407 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22409 int x0;
22410 struct glyph *end;
22412 string = mode_line_string (w, area, &x, &y, &charpos,
22413 &object, &dx, &dy, &width, &height);
22415 row = (area == ON_MODE_LINE
22416 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22417 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22419 /* Find glyph */
22420 if (row->mode_line_p && row->enabled_p)
22422 glyph = row->glyphs[TEXT_AREA];
22423 end = glyph + row->used[TEXT_AREA];
22425 for (x0 = original_x_pixel;
22426 glyph < end && x0 >= glyph->pixel_width;
22427 ++glyph)
22428 x0 -= glyph->pixel_width;
22430 if (glyph >= end)
22431 glyph = NULL;
22434 else
22436 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22437 string = marginal_area_string (w, area, &x, &y, &charpos,
22438 &object, &dx, &dy, &width, &height);
22441 help = Qnil;
22443 if (IMAGEP (object))
22445 Lisp_Object image_map, hotspot;
22446 if ((image_map = Fplist_get (XCDR (object), QCmap),
22447 !NILP (image_map))
22448 && (hotspot = find_hot_spot (image_map, dx, dy),
22449 CONSP (hotspot))
22450 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22452 Lisp_Object area_id, plist;
22454 area_id = XCAR (hotspot);
22455 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22456 If so, we could look for mouse-enter, mouse-leave
22457 properties in PLIST (and do something...). */
22458 hotspot = XCDR (hotspot);
22459 if (CONSP (hotspot)
22460 && (plist = XCAR (hotspot), CONSP (plist)))
22462 pointer = Fplist_get (plist, Qpointer);
22463 if (NILP (pointer))
22464 pointer = Qhand;
22465 help = Fplist_get (plist, Qhelp_echo);
22466 if (!NILP (help))
22468 help_echo_string = help;
22469 /* Is this correct? ++kfs */
22470 XSETWINDOW (help_echo_window, w);
22471 help_echo_object = w->buffer;
22472 help_echo_pos = charpos;
22476 if (NILP (pointer))
22477 pointer = Fplist_get (XCDR (object), QCpointer);
22480 if (STRINGP (string))
22482 pos = make_number (charpos);
22483 /* If we're on a string with `help-echo' text property, arrange
22484 for the help to be displayed. This is done by setting the
22485 global variable help_echo_string to the help string. */
22486 if (NILP (help))
22488 help = Fget_text_property (pos, Qhelp_echo, string);
22489 if (!NILP (help))
22491 help_echo_string = help;
22492 XSETWINDOW (help_echo_window, w);
22493 help_echo_object = string;
22494 help_echo_pos = charpos;
22498 if (NILP (pointer))
22499 pointer = Fget_text_property (pos, Qpointer, string);
22501 /* Change the mouse pointer according to what is under X/Y. */
22502 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22504 Lisp_Object map;
22505 map = Fget_text_property (pos, Qlocal_map, string);
22506 if (!KEYMAPP (map))
22507 map = Fget_text_property (pos, Qkeymap, string);
22508 if (!KEYMAPP (map))
22509 cursor = dpyinfo->vertical_scroll_bar_cursor;
22512 /* Change the mouse face according to what is under X/Y. */
22513 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22514 if (!NILP (mouse_face)
22515 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22516 && glyph)
22518 Lisp_Object b, e;
22520 struct glyph * tmp_glyph;
22522 int gpos;
22523 int gseq_length;
22524 int total_pixel_width;
22525 int ignore;
22527 int vpos, hpos;
22529 b = Fprevious_single_property_change (make_number (charpos + 1),
22530 Qmouse_face, string, Qnil);
22531 if (NILP (b))
22532 b = make_number (0);
22534 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22535 if (NILP (e))
22536 e = make_number (SCHARS (string));
22538 /* Calculate the position(glyph position: GPOS) of GLYPH in
22539 displayed string. GPOS is different from CHARPOS.
22541 CHARPOS is the position of glyph in internal string
22542 object. A mode line string format has structures which
22543 is converted to a flatten by emacs lisp interpreter.
22544 The internal string is an element of the structures.
22545 The displayed string is the flatten string. */
22546 for (tmp_glyph = glyph - 1, gpos = 0;
22547 tmp_glyph->charpos >= XINT (b);
22548 tmp_glyph--, gpos++)
22550 if (!EQ (tmp_glyph->object, glyph->object))
22551 break;
22554 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22555 displayed string holding GLYPH.
22557 GSEQ_LENGTH is different from SCHARS (STRING).
22558 SCHARS (STRING) returns the length of the internal string. */
22559 for (tmp_glyph = glyph, gseq_length = gpos;
22560 tmp_glyph->charpos < XINT (e);
22561 tmp_glyph++, gseq_length++)
22563 if (!EQ (tmp_glyph->object, glyph->object))
22564 break;
22567 total_pixel_width = 0;
22568 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22569 total_pixel_width += tmp_glyph->pixel_width;
22571 /* Pre calculation of re-rendering position */
22572 vpos = (x - gpos);
22573 hpos = (area == ON_MODE_LINE
22574 ? (w->current_matrix)->nrows - 1
22575 : 0);
22577 /* If the re-rendering position is included in the last
22578 re-rendering area, we should do nothing. */
22579 if ( EQ (window, dpyinfo->mouse_face_window)
22580 && dpyinfo->mouse_face_beg_col <= vpos
22581 && vpos < dpyinfo->mouse_face_end_col
22582 && dpyinfo->mouse_face_beg_row == hpos )
22583 return;
22585 if (clear_mouse_face (dpyinfo))
22586 cursor = No_Cursor;
22588 dpyinfo->mouse_face_beg_col = vpos;
22589 dpyinfo->mouse_face_beg_row = hpos;
22591 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22592 dpyinfo->mouse_face_beg_y = 0;
22594 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22595 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22597 dpyinfo->mouse_face_end_x = 0;
22598 dpyinfo->mouse_face_end_y = 0;
22600 dpyinfo->mouse_face_past_end = 0;
22601 dpyinfo->mouse_face_window = window;
22603 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22604 charpos,
22605 0, 0, 0, &ignore,
22606 glyph->face_id, 1);
22607 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22609 if (NILP (pointer))
22610 pointer = Qhand;
22612 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22613 clear_mouse_face (dpyinfo);
22615 define_frame_cursor1 (f, cursor, pointer);
22619 /* EXPORT:
22620 Take proper action when the mouse has moved to position X, Y on
22621 frame F as regards highlighting characters that have mouse-face
22622 properties. Also de-highlighting chars where the mouse was before.
22623 X and Y can be negative or out of range. */
22625 void
22626 note_mouse_highlight (f, x, y)
22627 struct frame *f;
22628 int x, y;
22630 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22631 enum window_part part;
22632 Lisp_Object window;
22633 struct window *w;
22634 Cursor cursor = No_Cursor;
22635 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22636 struct buffer *b;
22638 /* When a menu is active, don't highlight because this looks odd. */
22639 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22640 if (popup_activated ())
22641 return;
22642 #endif
22644 if (NILP (Vmouse_highlight)
22645 || !f->glyphs_initialized_p)
22646 return;
22648 dpyinfo->mouse_face_mouse_x = x;
22649 dpyinfo->mouse_face_mouse_y = y;
22650 dpyinfo->mouse_face_mouse_frame = f;
22652 if (dpyinfo->mouse_face_defer)
22653 return;
22655 if (gc_in_progress)
22657 dpyinfo->mouse_face_deferred_gc = 1;
22658 return;
22661 /* Which window is that in? */
22662 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22664 /* If we were displaying active text in another window, clear that.
22665 Also clear if we move out of text area in same window. */
22666 if (! EQ (window, dpyinfo->mouse_face_window)
22667 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22668 && !NILP (dpyinfo->mouse_face_window)))
22669 clear_mouse_face (dpyinfo);
22671 /* Not on a window -> return. */
22672 if (!WINDOWP (window))
22673 return;
22675 /* Reset help_echo_string. It will get recomputed below. */
22676 help_echo_string = Qnil;
22678 /* Convert to window-relative pixel coordinates. */
22679 w = XWINDOW (window);
22680 frame_to_window_pixel_xy (w, &x, &y);
22682 /* Handle tool-bar window differently since it doesn't display a
22683 buffer. */
22684 if (EQ (window, f->tool_bar_window))
22686 note_tool_bar_highlight (f, x, y);
22687 return;
22690 /* Mouse is on the mode, header line or margin? */
22691 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22692 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22694 note_mode_line_or_margin_highlight (window, x, y, part);
22695 return;
22698 if (part == ON_VERTICAL_BORDER)
22700 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22701 help_echo_string = build_string ("drag-mouse-1: resize");
22703 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22704 || part == ON_SCROLL_BAR)
22705 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22706 else
22707 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22709 /* Are we in a window whose display is up to date?
22710 And verify the buffer's text has not changed. */
22711 b = XBUFFER (w->buffer);
22712 if (part == ON_TEXT
22713 && EQ (w->window_end_valid, w->buffer)
22714 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22715 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22717 int hpos, vpos, pos, i, dx, dy, area;
22718 struct glyph *glyph;
22719 Lisp_Object object;
22720 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22721 Lisp_Object *overlay_vec = NULL;
22722 int noverlays;
22723 struct buffer *obuf;
22724 int obegv, ozv, same_region;
22726 /* Find the glyph under X/Y. */
22727 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22729 /* Look for :pointer property on image. */
22730 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22732 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22733 if (img != NULL && IMAGEP (img->spec))
22735 Lisp_Object image_map, hotspot;
22736 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22737 !NILP (image_map))
22738 && (hotspot = find_hot_spot (image_map,
22739 glyph->slice.x + dx,
22740 glyph->slice.y + dy),
22741 CONSP (hotspot))
22742 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22744 Lisp_Object area_id, plist;
22746 area_id = XCAR (hotspot);
22747 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22748 If so, we could look for mouse-enter, mouse-leave
22749 properties in PLIST (and do something...). */
22750 hotspot = XCDR (hotspot);
22751 if (CONSP (hotspot)
22752 && (plist = XCAR (hotspot), CONSP (plist)))
22754 pointer = Fplist_get (plist, Qpointer);
22755 if (NILP (pointer))
22756 pointer = Qhand;
22757 help_echo_string = Fplist_get (plist, Qhelp_echo);
22758 if (!NILP (help_echo_string))
22760 help_echo_window = window;
22761 help_echo_object = glyph->object;
22762 help_echo_pos = glyph->charpos;
22766 if (NILP (pointer))
22767 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22771 /* Clear mouse face if X/Y not over text. */
22772 if (glyph == NULL
22773 || area != TEXT_AREA
22774 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22776 if (clear_mouse_face (dpyinfo))
22777 cursor = No_Cursor;
22778 if (NILP (pointer))
22780 if (area != TEXT_AREA)
22781 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22782 else
22783 pointer = Vvoid_text_area_pointer;
22785 goto set_cursor;
22788 pos = glyph->charpos;
22789 object = glyph->object;
22790 if (!STRINGP (object) && !BUFFERP (object))
22791 goto set_cursor;
22793 /* If we get an out-of-range value, return now; avoid an error. */
22794 if (BUFFERP (object) && pos > BUF_Z (b))
22795 goto set_cursor;
22797 /* Make the window's buffer temporarily current for
22798 overlays_at and compute_char_face. */
22799 obuf = current_buffer;
22800 current_buffer = b;
22801 obegv = BEGV;
22802 ozv = ZV;
22803 BEGV = BEG;
22804 ZV = Z;
22806 /* Is this char mouse-active or does it have help-echo? */
22807 position = make_number (pos);
22809 if (BUFFERP (object))
22811 /* Put all the overlays we want in a vector in overlay_vec. */
22812 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22813 /* Sort overlays into increasing priority order. */
22814 noverlays = sort_overlays (overlay_vec, noverlays, w);
22816 else
22817 noverlays = 0;
22819 same_region = (EQ (window, dpyinfo->mouse_face_window)
22820 && vpos >= dpyinfo->mouse_face_beg_row
22821 && vpos <= dpyinfo->mouse_face_end_row
22822 && (vpos > dpyinfo->mouse_face_beg_row
22823 || hpos >= dpyinfo->mouse_face_beg_col)
22824 && (vpos < dpyinfo->mouse_face_end_row
22825 || hpos < dpyinfo->mouse_face_end_col
22826 || dpyinfo->mouse_face_past_end));
22828 if (same_region)
22829 cursor = No_Cursor;
22831 /* Check mouse-face highlighting. */
22832 if (! same_region
22833 /* If there exists an overlay with mouse-face overlapping
22834 the one we are currently highlighting, we have to
22835 check if we enter the overlapping overlay, and then
22836 highlight only that. */
22837 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22838 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22840 /* Find the highest priority overlay that has a mouse-face
22841 property. */
22842 overlay = Qnil;
22843 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22845 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22846 if (!NILP (mouse_face))
22847 overlay = overlay_vec[i];
22850 /* If we're actually highlighting the same overlay as
22851 before, there's no need to do that again. */
22852 if (!NILP (overlay)
22853 && EQ (overlay, dpyinfo->mouse_face_overlay))
22854 goto check_help_echo;
22856 dpyinfo->mouse_face_overlay = overlay;
22858 /* Clear the display of the old active region, if any. */
22859 if (clear_mouse_face (dpyinfo))
22860 cursor = No_Cursor;
22862 /* If no overlay applies, get a text property. */
22863 if (NILP (overlay))
22864 mouse_face = Fget_text_property (position, Qmouse_face, object);
22866 /* Handle the overlay case. */
22867 if (!NILP (overlay))
22869 /* Find the range of text around this char that
22870 should be active. */
22871 Lisp_Object before, after;
22872 int ignore;
22874 before = Foverlay_start (overlay);
22875 after = Foverlay_end (overlay);
22876 /* Record this as the current active region. */
22877 fast_find_position (w, XFASTINT (before),
22878 &dpyinfo->mouse_face_beg_col,
22879 &dpyinfo->mouse_face_beg_row,
22880 &dpyinfo->mouse_face_beg_x,
22881 &dpyinfo->mouse_face_beg_y, Qnil);
22883 dpyinfo->mouse_face_past_end
22884 = !fast_find_position (w, XFASTINT (after),
22885 &dpyinfo->mouse_face_end_col,
22886 &dpyinfo->mouse_face_end_row,
22887 &dpyinfo->mouse_face_end_x,
22888 &dpyinfo->mouse_face_end_y, Qnil);
22889 dpyinfo->mouse_face_window = window;
22891 dpyinfo->mouse_face_face_id
22892 = face_at_buffer_position (w, pos, 0, 0,
22893 &ignore, pos + 1,
22894 !dpyinfo->mouse_face_hidden);
22896 /* Display it as active. */
22897 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22898 cursor = No_Cursor;
22900 /* Handle the text property case. */
22901 else if (!NILP (mouse_face) && BUFFERP (object))
22903 /* Find the range of text around this char that
22904 should be active. */
22905 Lisp_Object before, after, beginning, end;
22906 int ignore;
22908 beginning = Fmarker_position (w->start);
22909 end = make_number (BUF_Z (XBUFFER (object))
22910 - XFASTINT (w->window_end_pos));
22911 before
22912 = Fprevious_single_property_change (make_number (pos + 1),
22913 Qmouse_face,
22914 object, beginning);
22915 after
22916 = Fnext_single_property_change (position, Qmouse_face,
22917 object, end);
22919 /* Record this as the current active region. */
22920 fast_find_position (w, XFASTINT (before),
22921 &dpyinfo->mouse_face_beg_col,
22922 &dpyinfo->mouse_face_beg_row,
22923 &dpyinfo->mouse_face_beg_x,
22924 &dpyinfo->mouse_face_beg_y, Qnil);
22925 dpyinfo->mouse_face_past_end
22926 = !fast_find_position (w, XFASTINT (after),
22927 &dpyinfo->mouse_face_end_col,
22928 &dpyinfo->mouse_face_end_row,
22929 &dpyinfo->mouse_face_end_x,
22930 &dpyinfo->mouse_face_end_y, Qnil);
22931 dpyinfo->mouse_face_window = window;
22933 if (BUFFERP (object))
22934 dpyinfo->mouse_face_face_id
22935 = face_at_buffer_position (w, pos, 0, 0,
22936 &ignore, pos + 1,
22937 !dpyinfo->mouse_face_hidden);
22939 /* Display it as active. */
22940 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22941 cursor = No_Cursor;
22943 else if (!NILP (mouse_face) && STRINGP (object))
22945 Lisp_Object b, e;
22946 int ignore;
22948 b = Fprevious_single_property_change (make_number (pos + 1),
22949 Qmouse_face,
22950 object, Qnil);
22951 e = Fnext_single_property_change (position, Qmouse_face,
22952 object, Qnil);
22953 if (NILP (b))
22954 b = make_number (0);
22955 if (NILP (e))
22956 e = make_number (SCHARS (object) - 1);
22958 fast_find_string_pos (w, XINT (b), object,
22959 &dpyinfo->mouse_face_beg_col,
22960 &dpyinfo->mouse_face_beg_row,
22961 &dpyinfo->mouse_face_beg_x,
22962 &dpyinfo->mouse_face_beg_y, 0);
22963 fast_find_string_pos (w, XINT (e), object,
22964 &dpyinfo->mouse_face_end_col,
22965 &dpyinfo->mouse_face_end_row,
22966 &dpyinfo->mouse_face_end_x,
22967 &dpyinfo->mouse_face_end_y, 1);
22968 dpyinfo->mouse_face_past_end = 0;
22969 dpyinfo->mouse_face_window = window;
22970 dpyinfo->mouse_face_face_id
22971 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22972 glyph->face_id, 1);
22973 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22974 cursor = No_Cursor;
22976 else if (STRINGP (object) && NILP (mouse_face))
22978 /* A string which doesn't have mouse-face, but
22979 the text ``under'' it might have. */
22980 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22981 int start = MATRIX_ROW_START_CHARPOS (r);
22983 pos = string_buffer_position (w, object, start);
22984 if (pos > 0)
22985 mouse_face = get_char_property_and_overlay (make_number (pos),
22986 Qmouse_face,
22987 w->buffer,
22988 &overlay);
22989 if (!NILP (mouse_face) && !NILP (overlay))
22991 Lisp_Object before = Foverlay_start (overlay);
22992 Lisp_Object after = Foverlay_end (overlay);
22993 int ignore;
22995 /* Note that we might not be able to find position
22996 BEFORE in the glyph matrix if the overlay is
22997 entirely covered by a `display' property. In
22998 this case, we overshoot. So let's stop in
22999 the glyph matrix before glyphs for OBJECT. */
23000 fast_find_position (w, XFASTINT (before),
23001 &dpyinfo->mouse_face_beg_col,
23002 &dpyinfo->mouse_face_beg_row,
23003 &dpyinfo->mouse_face_beg_x,
23004 &dpyinfo->mouse_face_beg_y,
23005 object);
23007 dpyinfo->mouse_face_past_end
23008 = !fast_find_position (w, XFASTINT (after),
23009 &dpyinfo->mouse_face_end_col,
23010 &dpyinfo->mouse_face_end_row,
23011 &dpyinfo->mouse_face_end_x,
23012 &dpyinfo->mouse_face_end_y,
23013 Qnil);
23014 dpyinfo->mouse_face_window = window;
23015 dpyinfo->mouse_face_face_id
23016 = face_at_buffer_position (w, pos, 0, 0,
23017 &ignore, pos + 1,
23018 !dpyinfo->mouse_face_hidden);
23020 /* Display it as active. */
23021 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23022 cursor = No_Cursor;
23027 check_help_echo:
23029 /* Look for a `help-echo' property. */
23030 if (NILP (help_echo_string)) {
23031 Lisp_Object help, overlay;
23033 /* Check overlays first. */
23034 help = overlay = Qnil;
23035 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23037 overlay = overlay_vec[i];
23038 help = Foverlay_get (overlay, Qhelp_echo);
23041 if (!NILP (help))
23043 help_echo_string = help;
23044 help_echo_window = window;
23045 help_echo_object = overlay;
23046 help_echo_pos = pos;
23048 else
23050 Lisp_Object object = glyph->object;
23051 int charpos = glyph->charpos;
23053 /* Try text properties. */
23054 if (STRINGP (object)
23055 && charpos >= 0
23056 && charpos < SCHARS (object))
23058 help = Fget_text_property (make_number (charpos),
23059 Qhelp_echo, object);
23060 if (NILP (help))
23062 /* If the string itself doesn't specify a help-echo,
23063 see if the buffer text ``under'' it does. */
23064 struct glyph_row *r
23065 = MATRIX_ROW (w->current_matrix, vpos);
23066 int start = MATRIX_ROW_START_CHARPOS (r);
23067 int pos = string_buffer_position (w, object, start);
23068 if (pos > 0)
23070 help = Fget_char_property (make_number (pos),
23071 Qhelp_echo, w->buffer);
23072 if (!NILP (help))
23074 charpos = pos;
23075 object = w->buffer;
23080 else if (BUFFERP (object)
23081 && charpos >= BEGV
23082 && charpos < ZV)
23083 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23084 object);
23086 if (!NILP (help))
23088 help_echo_string = help;
23089 help_echo_window = window;
23090 help_echo_object = object;
23091 help_echo_pos = charpos;
23096 /* Look for a `pointer' property. */
23097 if (NILP (pointer))
23099 /* Check overlays first. */
23100 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23101 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23103 if (NILP (pointer))
23105 Lisp_Object object = glyph->object;
23106 int charpos = glyph->charpos;
23108 /* Try text properties. */
23109 if (STRINGP (object)
23110 && charpos >= 0
23111 && charpos < SCHARS (object))
23113 pointer = Fget_text_property (make_number (charpos),
23114 Qpointer, object);
23115 if (NILP (pointer))
23117 /* If the string itself doesn't specify a pointer,
23118 see if the buffer text ``under'' it does. */
23119 struct glyph_row *r
23120 = MATRIX_ROW (w->current_matrix, vpos);
23121 int start = MATRIX_ROW_START_CHARPOS (r);
23122 int pos = string_buffer_position (w, object, start);
23123 if (pos > 0)
23124 pointer = Fget_char_property (make_number (pos),
23125 Qpointer, w->buffer);
23128 else if (BUFFERP (object)
23129 && charpos >= BEGV
23130 && charpos < ZV)
23131 pointer = Fget_text_property (make_number (charpos),
23132 Qpointer, object);
23136 BEGV = obegv;
23137 ZV = ozv;
23138 current_buffer = obuf;
23141 set_cursor:
23143 define_frame_cursor1 (f, cursor, pointer);
23147 /* EXPORT for RIF:
23148 Clear any mouse-face on window W. This function is part of the
23149 redisplay interface, and is called from try_window_id and similar
23150 functions to ensure the mouse-highlight is off. */
23152 void
23153 x_clear_window_mouse_face (w)
23154 struct window *w;
23156 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23157 Lisp_Object window;
23159 BLOCK_INPUT;
23160 XSETWINDOW (window, w);
23161 if (EQ (window, dpyinfo->mouse_face_window))
23162 clear_mouse_face (dpyinfo);
23163 UNBLOCK_INPUT;
23167 /* EXPORT:
23168 Just discard the mouse face information for frame F, if any.
23169 This is used when the size of F is changed. */
23171 void
23172 cancel_mouse_face (f)
23173 struct frame *f;
23175 Lisp_Object window;
23176 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23178 window = dpyinfo->mouse_face_window;
23179 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23181 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23182 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23183 dpyinfo->mouse_face_window = Qnil;
23188 #endif /* HAVE_WINDOW_SYSTEM */
23191 /***********************************************************************
23192 Exposure Events
23193 ***********************************************************************/
23195 #ifdef HAVE_WINDOW_SYSTEM
23197 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23198 which intersects rectangle R. R is in window-relative coordinates. */
23200 static void
23201 expose_area (w, row, r, area)
23202 struct window *w;
23203 struct glyph_row *row;
23204 XRectangle *r;
23205 enum glyph_row_area area;
23207 struct glyph *first = row->glyphs[area];
23208 struct glyph *end = row->glyphs[area] + row->used[area];
23209 struct glyph *last;
23210 int first_x, start_x, x;
23212 if (area == TEXT_AREA && row->fill_line_p)
23213 /* If row extends face to end of line write the whole line. */
23214 draw_glyphs (w, 0, row, area,
23215 0, row->used[area],
23216 DRAW_NORMAL_TEXT, 0);
23217 else
23219 /* Set START_X to the window-relative start position for drawing glyphs of
23220 AREA. The first glyph of the text area can be partially visible.
23221 The first glyphs of other areas cannot. */
23222 start_x = window_box_left_offset (w, area);
23223 x = start_x;
23224 if (area == TEXT_AREA)
23225 x += row->x;
23227 /* Find the first glyph that must be redrawn. */
23228 while (first < end
23229 && x + first->pixel_width < r->x)
23231 x += first->pixel_width;
23232 ++first;
23235 /* Find the last one. */
23236 last = first;
23237 first_x = x;
23238 while (last < end
23239 && x < r->x + r->width)
23241 x += last->pixel_width;
23242 ++last;
23245 /* Repaint. */
23246 if (last > first)
23247 draw_glyphs (w, first_x - start_x, row, area,
23248 first - row->glyphs[area], last - row->glyphs[area],
23249 DRAW_NORMAL_TEXT, 0);
23254 /* Redraw the parts of the glyph row ROW on window W intersecting
23255 rectangle R. R is in window-relative coordinates. Value is
23256 non-zero if mouse-face was overwritten. */
23258 static int
23259 expose_line (w, row, r)
23260 struct window *w;
23261 struct glyph_row *row;
23262 XRectangle *r;
23264 xassert (row->enabled_p);
23266 if (row->mode_line_p || w->pseudo_window_p)
23267 draw_glyphs (w, 0, row, TEXT_AREA,
23268 0, row->used[TEXT_AREA],
23269 DRAW_NORMAL_TEXT, 0);
23270 else
23272 if (row->used[LEFT_MARGIN_AREA])
23273 expose_area (w, row, r, LEFT_MARGIN_AREA);
23274 if (row->used[TEXT_AREA])
23275 expose_area (w, row, r, TEXT_AREA);
23276 if (row->used[RIGHT_MARGIN_AREA])
23277 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23278 draw_row_fringe_bitmaps (w, row);
23281 return row->mouse_face_p;
23285 /* Redraw those parts of glyphs rows during expose event handling that
23286 overlap other rows. Redrawing of an exposed line writes over parts
23287 of lines overlapping that exposed line; this function fixes that.
23289 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23290 row in W's current matrix that is exposed and overlaps other rows.
23291 LAST_OVERLAPPING_ROW is the last such row. */
23293 static void
23294 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23295 struct window *w;
23296 struct glyph_row *first_overlapping_row;
23297 struct glyph_row *last_overlapping_row;
23299 struct glyph_row *row;
23301 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23302 if (row->overlapping_p)
23304 xassert (row->enabled_p && !row->mode_line_p);
23306 if (row->used[LEFT_MARGIN_AREA])
23307 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23309 if (row->used[TEXT_AREA])
23310 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23312 if (row->used[RIGHT_MARGIN_AREA])
23313 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23318 /* Return non-zero if W's cursor intersects rectangle R. */
23320 static int
23321 phys_cursor_in_rect_p (w, r)
23322 struct window *w;
23323 XRectangle *r;
23325 XRectangle cr, result;
23326 struct glyph *cursor_glyph;
23328 cursor_glyph = get_phys_cursor_glyph (w);
23329 if (cursor_glyph)
23331 /* r is relative to W's box, but w->phys_cursor.x is relative
23332 to left edge of W's TEXT area. Adjust it. */
23333 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23334 cr.y = w->phys_cursor.y;
23335 cr.width = cursor_glyph->pixel_width;
23336 cr.height = w->phys_cursor_height;
23337 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23338 I assume the effect is the same -- and this is portable. */
23339 return x_intersect_rectangles (&cr, r, &result);
23341 else
23342 return 0;
23346 /* EXPORT:
23347 Draw a vertical window border to the right of window W if W doesn't
23348 have vertical scroll bars. */
23350 void
23351 x_draw_vertical_border (w)
23352 struct window *w;
23354 /* We could do better, if we knew what type of scroll-bar the adjacent
23355 windows (on either side) have... But we don't :-(
23356 However, I think this works ok. ++KFS 2003-04-25 */
23358 /* Redraw borders between horizontally adjacent windows. Don't
23359 do it for frames with vertical scroll bars because either the
23360 right scroll bar of a window, or the left scroll bar of its
23361 neighbor will suffice as a border. */
23362 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23363 return;
23365 if (!WINDOW_RIGHTMOST_P (w)
23366 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23368 int x0, x1, y0, y1;
23370 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23371 y1 -= 1;
23373 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23374 x1 -= 1;
23376 rif->draw_vertical_window_border (w, x1, y0, y1);
23378 else if (!WINDOW_LEFTMOST_P (w)
23379 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23381 int x0, x1, y0, y1;
23383 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23384 y1 -= 1;
23386 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23387 x0 -= 1;
23389 rif->draw_vertical_window_border (w, x0, y0, y1);
23394 /* Redraw the part of window W intersection rectangle FR. Pixel
23395 coordinates in FR are frame-relative. Call this function with
23396 input blocked. Value is non-zero if the exposure overwrites
23397 mouse-face. */
23399 static int
23400 expose_window (w, fr)
23401 struct window *w;
23402 XRectangle *fr;
23404 struct frame *f = XFRAME (w->frame);
23405 XRectangle wr, r;
23406 int mouse_face_overwritten_p = 0;
23408 /* If window is not yet fully initialized, do nothing. This can
23409 happen when toolkit scroll bars are used and a window is split.
23410 Reconfiguring the scroll bar will generate an expose for a newly
23411 created window. */
23412 if (w->current_matrix == NULL)
23413 return 0;
23415 /* When we're currently updating the window, display and current
23416 matrix usually don't agree. Arrange for a thorough display
23417 later. */
23418 if (w == updated_window)
23420 SET_FRAME_GARBAGED (f);
23421 return 0;
23424 /* Frame-relative pixel rectangle of W. */
23425 wr.x = WINDOW_LEFT_EDGE_X (w);
23426 wr.y = WINDOW_TOP_EDGE_Y (w);
23427 wr.width = WINDOW_TOTAL_WIDTH (w);
23428 wr.height = WINDOW_TOTAL_HEIGHT (w);
23430 if (x_intersect_rectangles (fr, &wr, &r))
23432 int yb = window_text_bottom_y (w);
23433 struct glyph_row *row;
23434 int cursor_cleared_p;
23435 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23437 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23438 r.x, r.y, r.width, r.height));
23440 /* Convert to window coordinates. */
23441 r.x -= WINDOW_LEFT_EDGE_X (w);
23442 r.y -= WINDOW_TOP_EDGE_Y (w);
23444 /* Turn off the cursor. */
23445 if (!w->pseudo_window_p
23446 && phys_cursor_in_rect_p (w, &r))
23448 x_clear_cursor (w);
23449 cursor_cleared_p = 1;
23451 else
23452 cursor_cleared_p = 0;
23454 /* Update lines intersecting rectangle R. */
23455 first_overlapping_row = last_overlapping_row = NULL;
23456 for (row = w->current_matrix->rows;
23457 row->enabled_p;
23458 ++row)
23460 int y0 = row->y;
23461 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23463 if ((y0 >= r.y && y0 < r.y + r.height)
23464 || (y1 > r.y && y1 < r.y + r.height)
23465 || (r.y >= y0 && r.y < y1)
23466 || (r.y + r.height > y0 && r.y + r.height < y1))
23468 /* A header line may be overlapping, but there is no need
23469 to fix overlapping areas for them. KFS 2005-02-12 */
23470 if (row->overlapping_p && !row->mode_line_p)
23472 if (first_overlapping_row == NULL)
23473 first_overlapping_row = row;
23474 last_overlapping_row = row;
23477 if (expose_line (w, row, &r))
23478 mouse_face_overwritten_p = 1;
23481 if (y1 >= yb)
23482 break;
23485 /* Display the mode line if there is one. */
23486 if (WINDOW_WANTS_MODELINE_P (w)
23487 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23488 row->enabled_p)
23489 && row->y < r.y + r.height)
23491 if (expose_line (w, row, &r))
23492 mouse_face_overwritten_p = 1;
23495 if (!w->pseudo_window_p)
23497 /* Fix the display of overlapping rows. */
23498 if (first_overlapping_row)
23499 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23501 /* Draw border between windows. */
23502 x_draw_vertical_border (w);
23504 /* Turn the cursor on again. */
23505 if (cursor_cleared_p)
23506 update_window_cursor (w, 1);
23510 return mouse_face_overwritten_p;
23515 /* Redraw (parts) of all windows in the window tree rooted at W that
23516 intersect R. R contains frame pixel coordinates. Value is
23517 non-zero if the exposure overwrites mouse-face. */
23519 static int
23520 expose_window_tree (w, r)
23521 struct window *w;
23522 XRectangle *r;
23524 struct frame *f = XFRAME (w->frame);
23525 int mouse_face_overwritten_p = 0;
23527 while (w && !FRAME_GARBAGED_P (f))
23529 if (!NILP (w->hchild))
23530 mouse_face_overwritten_p
23531 |= expose_window_tree (XWINDOW (w->hchild), r);
23532 else if (!NILP (w->vchild))
23533 mouse_face_overwritten_p
23534 |= expose_window_tree (XWINDOW (w->vchild), r);
23535 else
23536 mouse_face_overwritten_p |= expose_window (w, r);
23538 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23541 return mouse_face_overwritten_p;
23545 /* EXPORT:
23546 Redisplay an exposed area of frame F. X and Y are the upper-left
23547 corner of the exposed rectangle. W and H are width and height of
23548 the exposed area. All are pixel values. W or H zero means redraw
23549 the entire frame. */
23551 void
23552 expose_frame (f, x, y, w, h)
23553 struct frame *f;
23554 int x, y, w, h;
23556 XRectangle r;
23557 int mouse_face_overwritten_p = 0;
23559 TRACE ((stderr, "expose_frame "));
23561 /* No need to redraw if frame will be redrawn soon. */
23562 if (FRAME_GARBAGED_P (f))
23564 TRACE ((stderr, " garbaged\n"));
23565 return;
23568 /* If basic faces haven't been realized yet, there is no point in
23569 trying to redraw anything. This can happen when we get an expose
23570 event while Emacs is starting, e.g. by moving another window. */
23571 if (FRAME_FACE_CACHE (f) == NULL
23572 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23574 TRACE ((stderr, " no faces\n"));
23575 return;
23578 if (w == 0 || h == 0)
23580 r.x = r.y = 0;
23581 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23582 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23584 else
23586 r.x = x;
23587 r.y = y;
23588 r.width = w;
23589 r.height = h;
23592 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23593 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23595 if (WINDOWP (f->tool_bar_window))
23596 mouse_face_overwritten_p
23597 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23599 #ifdef HAVE_X_WINDOWS
23600 #ifndef MSDOS
23601 #ifndef USE_X_TOOLKIT
23602 if (WINDOWP (f->menu_bar_window))
23603 mouse_face_overwritten_p
23604 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23605 #endif /* not USE_X_TOOLKIT */
23606 #endif
23607 #endif
23609 /* Some window managers support a focus-follows-mouse style with
23610 delayed raising of frames. Imagine a partially obscured frame,
23611 and moving the mouse into partially obscured mouse-face on that
23612 frame. The visible part of the mouse-face will be highlighted,
23613 then the WM raises the obscured frame. With at least one WM, KDE
23614 2.1, Emacs is not getting any event for the raising of the frame
23615 (even tried with SubstructureRedirectMask), only Expose events.
23616 These expose events will draw text normally, i.e. not
23617 highlighted. Which means we must redo the highlight here.
23618 Subsume it under ``we love X''. --gerd 2001-08-15 */
23619 /* Included in Windows version because Windows most likely does not
23620 do the right thing if any third party tool offers
23621 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23622 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23624 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23625 if (f == dpyinfo->mouse_face_mouse_frame)
23627 int x = dpyinfo->mouse_face_mouse_x;
23628 int y = dpyinfo->mouse_face_mouse_y;
23629 clear_mouse_face (dpyinfo);
23630 note_mouse_highlight (f, x, y);
23636 /* EXPORT:
23637 Determine the intersection of two rectangles R1 and R2. Return
23638 the intersection in *RESULT. Value is non-zero if RESULT is not
23639 empty. */
23642 x_intersect_rectangles (r1, r2, result)
23643 XRectangle *r1, *r2, *result;
23645 XRectangle *left, *right;
23646 XRectangle *upper, *lower;
23647 int intersection_p = 0;
23649 /* Rearrange so that R1 is the left-most rectangle. */
23650 if (r1->x < r2->x)
23651 left = r1, right = r2;
23652 else
23653 left = r2, right = r1;
23655 /* X0 of the intersection is right.x0, if this is inside R1,
23656 otherwise there is no intersection. */
23657 if (right->x <= left->x + left->width)
23659 result->x = right->x;
23661 /* The right end of the intersection is the minimum of the
23662 the right ends of left and right. */
23663 result->width = (min (left->x + left->width, right->x + right->width)
23664 - result->x);
23666 /* Same game for Y. */
23667 if (r1->y < r2->y)
23668 upper = r1, lower = r2;
23669 else
23670 upper = r2, lower = r1;
23672 /* The upper end of the intersection is lower.y0, if this is inside
23673 of upper. Otherwise, there is no intersection. */
23674 if (lower->y <= upper->y + upper->height)
23676 result->y = lower->y;
23678 /* The lower end of the intersection is the minimum of the lower
23679 ends of upper and lower. */
23680 result->height = (min (lower->y + lower->height,
23681 upper->y + upper->height)
23682 - result->y);
23683 intersection_p = 1;
23687 return intersection_p;
23690 #endif /* HAVE_WINDOW_SYSTEM */
23693 /***********************************************************************
23694 Initialization
23695 ***********************************************************************/
23697 void
23698 syms_of_xdisp ()
23700 Vwith_echo_area_save_vector = Qnil;
23701 staticpro (&Vwith_echo_area_save_vector);
23703 Vmessage_stack = Qnil;
23704 staticpro (&Vmessage_stack);
23706 Qinhibit_redisplay = intern ("inhibit-redisplay");
23707 staticpro (&Qinhibit_redisplay);
23709 message_dolog_marker1 = Fmake_marker ();
23710 staticpro (&message_dolog_marker1);
23711 message_dolog_marker2 = Fmake_marker ();
23712 staticpro (&message_dolog_marker2);
23713 message_dolog_marker3 = Fmake_marker ();
23714 staticpro (&message_dolog_marker3);
23716 #if GLYPH_DEBUG
23717 defsubr (&Sdump_frame_glyph_matrix);
23718 defsubr (&Sdump_glyph_matrix);
23719 defsubr (&Sdump_glyph_row);
23720 defsubr (&Sdump_tool_bar_row);
23721 defsubr (&Strace_redisplay);
23722 defsubr (&Strace_to_stderr);
23723 #endif
23724 #ifdef HAVE_WINDOW_SYSTEM
23725 defsubr (&Stool_bar_lines_needed);
23726 defsubr (&Slookup_image_map);
23727 #endif
23728 defsubr (&Sformat_mode_line);
23730 staticpro (&Qmenu_bar_update_hook);
23731 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23733 staticpro (&Qoverriding_terminal_local_map);
23734 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23736 staticpro (&Qoverriding_local_map);
23737 Qoverriding_local_map = intern ("overriding-local-map");
23739 staticpro (&Qwindow_scroll_functions);
23740 Qwindow_scroll_functions = intern ("window-scroll-functions");
23742 staticpro (&Qredisplay_end_trigger_functions);
23743 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23745 staticpro (&Qinhibit_point_motion_hooks);
23746 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23748 QCdata = intern (":data");
23749 staticpro (&QCdata);
23750 Qdisplay = intern ("display");
23751 staticpro (&Qdisplay);
23752 Qspace_width = intern ("space-width");
23753 staticpro (&Qspace_width);
23754 Qraise = intern ("raise");
23755 staticpro (&Qraise);
23756 Qslice = intern ("slice");
23757 staticpro (&Qslice);
23758 Qspace = intern ("space");
23759 staticpro (&Qspace);
23760 Qmargin = intern ("margin");
23761 staticpro (&Qmargin);
23762 Qpointer = intern ("pointer");
23763 staticpro (&Qpointer);
23764 Qleft_margin = intern ("left-margin");
23765 staticpro (&Qleft_margin);
23766 Qright_margin = intern ("right-margin");
23767 staticpro (&Qright_margin);
23768 Qcenter = intern ("center");
23769 staticpro (&Qcenter);
23770 Qline_height = intern ("line-height");
23771 staticpro (&Qline_height);
23772 QCalign_to = intern (":align-to");
23773 staticpro (&QCalign_to);
23774 QCrelative_width = intern (":relative-width");
23775 staticpro (&QCrelative_width);
23776 QCrelative_height = intern (":relative-height");
23777 staticpro (&QCrelative_height);
23778 QCeval = intern (":eval");
23779 staticpro (&QCeval);
23780 QCpropertize = intern (":propertize");
23781 staticpro (&QCpropertize);
23782 QCfile = intern (":file");
23783 staticpro (&QCfile);
23784 Qfontified = intern ("fontified");
23785 staticpro (&Qfontified);
23786 Qfontification_functions = intern ("fontification-functions");
23787 staticpro (&Qfontification_functions);
23788 Qtrailing_whitespace = intern ("trailing-whitespace");
23789 staticpro (&Qtrailing_whitespace);
23790 Qescape_glyph = intern ("escape-glyph");
23791 staticpro (&Qescape_glyph);
23792 Qnobreak_space = intern ("nobreak-space");
23793 staticpro (&Qnobreak_space);
23794 Qimage = intern ("image");
23795 staticpro (&Qimage);
23796 QCmap = intern (":map");
23797 staticpro (&QCmap);
23798 QCpointer = intern (":pointer");
23799 staticpro (&QCpointer);
23800 Qrect = intern ("rect");
23801 staticpro (&Qrect);
23802 Qcircle = intern ("circle");
23803 staticpro (&Qcircle);
23804 Qpoly = intern ("poly");
23805 staticpro (&Qpoly);
23806 Qmessage_truncate_lines = intern ("message-truncate-lines");
23807 staticpro (&Qmessage_truncate_lines);
23808 Qgrow_only = intern ("grow-only");
23809 staticpro (&Qgrow_only);
23810 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23811 staticpro (&Qinhibit_menubar_update);
23812 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23813 staticpro (&Qinhibit_eval_during_redisplay);
23814 Qposition = intern ("position");
23815 staticpro (&Qposition);
23816 Qbuffer_position = intern ("buffer-position");
23817 staticpro (&Qbuffer_position);
23818 Qobject = intern ("object");
23819 staticpro (&Qobject);
23820 Qbar = intern ("bar");
23821 staticpro (&Qbar);
23822 Qhbar = intern ("hbar");
23823 staticpro (&Qhbar);
23824 Qbox = intern ("box");
23825 staticpro (&Qbox);
23826 Qhollow = intern ("hollow");
23827 staticpro (&Qhollow);
23828 Qhand = intern ("hand");
23829 staticpro (&Qhand);
23830 Qarrow = intern ("arrow");
23831 staticpro (&Qarrow);
23832 Qtext = intern ("text");
23833 staticpro (&Qtext);
23834 Qrisky_local_variable = intern ("risky-local-variable");
23835 staticpro (&Qrisky_local_variable);
23836 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23837 staticpro (&Qinhibit_free_realized_faces);
23839 list_of_error = Fcons (Fcons (intern ("error"),
23840 Fcons (intern ("void-variable"), Qnil)),
23841 Qnil);
23842 staticpro (&list_of_error);
23844 Qlast_arrow_position = intern ("last-arrow-position");
23845 staticpro (&Qlast_arrow_position);
23846 Qlast_arrow_string = intern ("last-arrow-string");
23847 staticpro (&Qlast_arrow_string);
23849 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23850 staticpro (&Qoverlay_arrow_string);
23851 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23852 staticpro (&Qoverlay_arrow_bitmap);
23854 echo_buffer[0] = echo_buffer[1] = Qnil;
23855 staticpro (&echo_buffer[0]);
23856 staticpro (&echo_buffer[1]);
23858 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23859 staticpro (&echo_area_buffer[0]);
23860 staticpro (&echo_area_buffer[1]);
23862 Vmessages_buffer_name = build_string ("*Messages*");
23863 staticpro (&Vmessages_buffer_name);
23865 mode_line_proptrans_alist = Qnil;
23866 staticpro (&mode_line_proptrans_alist);
23867 mode_line_string_list = Qnil;
23868 staticpro (&mode_line_string_list);
23869 mode_line_string_face = Qnil;
23870 staticpro (&mode_line_string_face);
23871 mode_line_string_face_prop = Qnil;
23872 staticpro (&mode_line_string_face_prop);
23873 Vmode_line_unwind_vector = Qnil;
23874 staticpro (&Vmode_line_unwind_vector);
23876 help_echo_string = Qnil;
23877 staticpro (&help_echo_string);
23878 help_echo_object = Qnil;
23879 staticpro (&help_echo_object);
23880 help_echo_window = Qnil;
23881 staticpro (&help_echo_window);
23882 previous_help_echo_string = Qnil;
23883 staticpro (&previous_help_echo_string);
23884 help_echo_pos = -1;
23886 #ifdef HAVE_WINDOW_SYSTEM
23887 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23888 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23889 For example, if a block cursor is over a tab, it will be drawn as
23890 wide as that tab on the display. */);
23891 x_stretch_cursor_p = 0;
23892 #endif
23894 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23895 doc: /* *Non-nil means highlight trailing whitespace.
23896 The face used for trailing whitespace is `trailing-whitespace'. */);
23897 Vshow_trailing_whitespace = Qnil;
23899 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23900 doc: /* *Control highlighting of nobreak space and soft hyphen.
23901 A value of t means highlight the character itself (for nobreak space,
23902 use face `nobreak-space').
23903 A value of nil means no highlighting.
23904 Other values mean display the escape glyph followed by an ordinary
23905 space or ordinary hyphen. */);
23906 Vnobreak_char_display = Qt;
23908 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23909 doc: /* *The pointer shape to show in void text areas.
23910 A value of nil means to show the text pointer. Other options are `arrow',
23911 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23912 Vvoid_text_area_pointer = Qarrow;
23914 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23915 doc: /* Non-nil means don't actually do any redisplay.
23916 This is used for internal purposes. */);
23917 Vinhibit_redisplay = Qnil;
23919 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23920 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23921 Vglobal_mode_string = Qnil;
23923 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23924 doc: /* Marker for where to display an arrow on top of the buffer text.
23925 This must be the beginning of a line in order to work.
23926 See also `overlay-arrow-string'. */);
23927 Voverlay_arrow_position = Qnil;
23929 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23930 doc: /* String to display as an arrow in non-window frames.
23931 See also `overlay-arrow-position'. */);
23932 Voverlay_arrow_string = build_string ("=>");
23934 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23935 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23936 The symbols on this list are examined during redisplay to determine
23937 where to display overlay arrows. */);
23938 Voverlay_arrow_variable_list
23939 = Fcons (intern ("overlay-arrow-position"), Qnil);
23941 DEFVAR_INT ("scroll-step", &scroll_step,
23942 doc: /* *The number of lines to try scrolling a window by when point moves out.
23943 If that fails to bring point back on frame, point is centered instead.
23944 If this is zero, point is always centered after it moves off frame.
23945 If you want scrolling to always be a line at a time, you should set
23946 `scroll-conservatively' to a large value rather than set this to 1. */);
23948 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23949 doc: /* *Scroll up to this many lines, to bring point back on screen.
23950 A value of zero means to scroll the text to center point vertically
23951 in the window. */);
23952 scroll_conservatively = 0;
23954 DEFVAR_INT ("scroll-margin", &scroll_margin,
23955 doc: /* *Number of lines of margin at the top and bottom of a window.
23956 Recenter the window whenever point gets within this many lines
23957 of the top or bottom of the window. */);
23958 scroll_margin = 0;
23960 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23961 doc: /* Pixels per inch value for non-window system displays.
23962 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23963 Vdisplay_pixels_per_inch = make_float (72.0);
23965 #if GLYPH_DEBUG
23966 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23967 #endif
23969 DEFVAR_BOOL ("truncate-partial-width-windows",
23970 &truncate_partial_width_windows,
23971 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23972 truncate_partial_width_windows = 1;
23974 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23975 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23976 Any other value means to use the appropriate face, `mode-line',
23977 `header-line', or `menu' respectively. */);
23978 mode_line_inverse_video = 1;
23980 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23981 doc: /* *Maximum buffer size for which line number should be displayed.
23982 If the buffer is bigger than this, the line number does not appear
23983 in the mode line. A value of nil means no limit. */);
23984 Vline_number_display_limit = Qnil;
23986 DEFVAR_INT ("line-number-display-limit-width",
23987 &line_number_display_limit_width,
23988 doc: /* *Maximum line width (in characters) for line number display.
23989 If the average length of the lines near point is bigger than this, then the
23990 line number may be omitted from the mode line. */);
23991 line_number_display_limit_width = 200;
23993 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23994 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23995 highlight_nonselected_windows = 0;
23997 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23998 doc: /* Non-nil if more than one frame is visible on this display.
23999 Minibuffer-only frames don't count, but iconified frames do.
24000 This variable is not guaranteed to be accurate except while processing
24001 `frame-title-format' and `icon-title-format'. */);
24003 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24004 doc: /* Template for displaying the title bar of visible frames.
24005 \(Assuming the window manager supports this feature.)
24007 This variable has the same structure as `mode-line-format', except that
24008 the %c and %l constructs are ignored. It is used only on frames for
24009 which no explicit name has been set \(see `modify-frame-parameters'). */);
24011 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24012 doc: /* Template for displaying the title bar of an iconified frame.
24013 \(Assuming the window manager supports this feature.)
24014 This variable has the same structure as `mode-line-format' (which see),
24015 and is used only on frames for which no explicit name has been set
24016 \(see `modify-frame-parameters'). */);
24017 Vicon_title_format
24018 = Vframe_title_format
24019 = Fcons (intern ("multiple-frames"),
24020 Fcons (build_string ("%b"),
24021 Fcons (Fcons (empty_string,
24022 Fcons (intern ("invocation-name"),
24023 Fcons (build_string ("@"),
24024 Fcons (intern ("system-name"),
24025 Qnil)))),
24026 Qnil)));
24028 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24029 doc: /* Maximum number of lines to keep in the message log buffer.
24030 If nil, disable message logging. If t, log messages but don't truncate
24031 the buffer when it becomes large. */);
24032 Vmessage_log_max = make_number (50);
24034 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24035 doc: /* Functions called before redisplay, if window sizes have changed.
24036 The value should be a list of functions that take one argument.
24037 Just before redisplay, for each frame, if any of its windows have changed
24038 size since the last redisplay, or have been split or deleted,
24039 all the functions in the list are called, with the frame as argument. */);
24040 Vwindow_size_change_functions = Qnil;
24042 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24043 doc: /* List of functions to call before redisplaying a window with scrolling.
24044 Each function is called with two arguments, the window
24045 and its new display-start position. Note that the value of `window-end'
24046 is not valid when these functions are called. */);
24047 Vwindow_scroll_functions = Qnil;
24049 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24050 doc: /* Functions called when redisplay of a window reaches the end trigger.
24051 Each function is called with two arguments, the window and the end trigger value.
24052 See `set-window-redisplay-end-trigger'. */);
24053 Vredisplay_end_trigger_functions = Qnil;
24055 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24056 doc: /* *Non-nil means autoselect window with mouse pointer.
24057 If nil, do not autoselect windows.
24058 A positive number means delay autoselection by that many seconds: a
24059 window is autoselected only after the mouse has remained in that
24060 window for the duration of the delay.
24061 A negative number has a similar effect, but causes windows to be
24062 autoselected only after the mouse has stopped moving. \(Because of
24063 the way Emacs compares mouse events, you will occasionally wait twice
24064 that time before the window gets selected.\)
24065 Any other value means to autoselect window instantaneously when the
24066 mouse pointer enters it.
24068 Autoselection selects the minibuffer only if it is active, and never
24069 unselects the minibuffer if it is active. */);
24070 Vmouse_autoselect_window = Qnil;
24072 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
24073 doc: /* *Non-nil means automatically resize tool-bars.
24074 This increases a tool-bar's height if not all tool-bar items are visible.
24075 It decreases a tool-bar's height when it would display blank lines
24076 otherwise. */);
24077 auto_resize_tool_bars_p = 1;
24079 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24080 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24081 auto_raise_tool_bar_buttons_p = 1;
24083 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24084 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24085 make_cursor_line_fully_visible_p = 1;
24087 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24088 doc: /* *Border below tool-bar in pixels.
24089 If an integer, use it as the height of the border.
24090 If it is one of `internal-border-width' or `border-width', use the
24091 value of the corresponding frame parameter.
24092 Otherwise, no border is added below the tool-bar. */);
24093 Vtool_bar_border = Qinternal_border_width;
24095 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24096 doc: /* *Margin around tool-bar buttons in pixels.
24097 If an integer, use that for both horizontal and vertical margins.
24098 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24099 HORZ specifying the horizontal margin, and VERT specifying the
24100 vertical margin. */);
24101 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24103 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24104 doc: /* *Relief thickness of tool-bar buttons. */);
24105 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24107 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24108 doc: /* List of functions to call to fontify regions of text.
24109 Each function is called with one argument POS. Functions must
24110 fontify a region starting at POS in the current buffer, and give
24111 fontified regions the property `fontified'. */);
24112 Vfontification_functions = Qnil;
24113 Fmake_variable_buffer_local (Qfontification_functions);
24115 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24116 &unibyte_display_via_language_environment,
24117 doc: /* *Non-nil means display unibyte text according to language environment.
24118 Specifically this means that unibyte non-ASCII characters
24119 are displayed by converting them to the equivalent multibyte characters
24120 according to the current language environment. As a result, they are
24121 displayed according to the current fontset. */);
24122 unibyte_display_via_language_environment = 0;
24124 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24125 doc: /* *Maximum height for resizing mini-windows.
24126 If a float, it specifies a fraction of the mini-window frame's height.
24127 If an integer, it specifies a number of lines. */);
24128 Vmax_mini_window_height = make_float (0.25);
24130 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24131 doc: /* *How to resize mini-windows.
24132 A value of nil means don't automatically resize mini-windows.
24133 A value of t means resize them to fit the text displayed in them.
24134 A value of `grow-only', the default, means let mini-windows grow
24135 only, until their display becomes empty, at which point the windows
24136 go back to their normal size. */);
24137 Vresize_mini_windows = Qgrow_only;
24139 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24140 doc: /* Alist specifying how to blink the cursor off.
24141 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24142 `cursor-type' frame-parameter or variable equals ON-STATE,
24143 comparing using `equal', Emacs uses OFF-STATE to specify
24144 how to blink it off. ON-STATE and OFF-STATE are values for
24145 the `cursor-type' frame parameter.
24147 If a frame's ON-STATE has no entry in this list,
24148 the frame's other specifications determine how to blink the cursor off. */);
24149 Vblink_cursor_alist = Qnil;
24151 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24152 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24153 automatic_hscrolling_p = 1;
24155 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24156 doc: /* *How many columns away from the window edge point is allowed to get
24157 before automatic hscrolling will horizontally scroll the window. */);
24158 hscroll_margin = 5;
24160 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24161 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24162 When point is less than `hscroll-margin' columns from the window
24163 edge, automatic hscrolling will scroll the window by the amount of columns
24164 determined by this variable. If its value is a positive integer, scroll that
24165 many columns. If it's a positive floating-point number, it specifies the
24166 fraction of the window's width to scroll. If it's nil or zero, point will be
24167 centered horizontally after the scroll. Any other value, including negative
24168 numbers, are treated as if the value were zero.
24170 Automatic hscrolling always moves point outside the scroll margin, so if
24171 point was more than scroll step columns inside the margin, the window will
24172 scroll more than the value given by the scroll step.
24174 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24175 and `scroll-right' overrides this variable's effect. */);
24176 Vhscroll_step = make_number (0);
24178 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24179 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24180 Bind this around calls to `message' to let it take effect. */);
24181 message_truncate_lines = 0;
24183 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24184 doc: /* Normal hook run to update the menu bar definitions.
24185 Redisplay runs this hook before it redisplays the menu bar.
24186 This is used to update submenus such as Buffers,
24187 whose contents depend on various data. */);
24188 Vmenu_bar_update_hook = Qnil;
24190 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24191 doc: /* Frame for which we are updating a menu.
24192 The enable predicate for a menu binding should check this variable. */);
24193 Vmenu_updating_frame = Qnil;
24195 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24196 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24197 inhibit_menubar_update = 0;
24199 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24200 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24201 inhibit_eval_during_redisplay = 0;
24203 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24204 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24205 inhibit_free_realized_faces = 0;
24207 #if GLYPH_DEBUG
24208 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24209 doc: /* Inhibit try_window_id display optimization. */);
24210 inhibit_try_window_id = 0;
24212 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24213 doc: /* Inhibit try_window_reusing display optimization. */);
24214 inhibit_try_window_reusing = 0;
24216 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24217 doc: /* Inhibit try_cursor_movement display optimization. */);
24218 inhibit_try_cursor_movement = 0;
24219 #endif /* GLYPH_DEBUG */
24221 DEFVAR_INT ("overline-margin", &overline_margin,
24222 doc: /* *Space between overline and text, in pixels.
24223 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24224 margin to the caracter height. */);
24225 overline_margin = 2;
24229 /* Initialize this module when Emacs starts. */
24231 void
24232 init_xdisp ()
24234 Lisp_Object root_window;
24235 struct window *mini_w;
24237 current_header_line_height = current_mode_line_height = -1;
24239 CHARPOS (this_line_start_pos) = 0;
24241 mini_w = XWINDOW (minibuf_window);
24242 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24244 if (!noninteractive)
24246 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24247 int i;
24249 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24250 set_window_height (root_window,
24251 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24253 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24254 set_window_height (minibuf_window, 1, 0);
24256 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24257 mini_w->total_cols = make_number (FRAME_COLS (f));
24259 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24260 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24261 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24263 /* The default ellipsis glyphs `...'. */
24264 for (i = 0; i < 3; ++i)
24265 default_invis_vector[i] = make_number ('.');
24269 /* Allocate the buffer for frame titles.
24270 Also used for `format-mode-line'. */
24271 int size = 100;
24272 mode_line_noprop_buf = (char *) xmalloc (size);
24273 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24274 mode_line_noprop_ptr = mode_line_noprop_buf;
24275 mode_line_target = MODE_LINE_DISPLAY;
24278 help_echo_showing_p = 0;
24282 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24283 (do not change this comment) */