(handle_fontified_prop): Don't fontify at EOB.
[emacs.git] / src / xdisp.c
bloba8da5aa848e8d06f3eeaaefad45e100a5ce2a2e3
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))
1351 visible_p = 1;
1352 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1353 *x = it2.current_x;
1354 *y = it2.current_y + it2.max_ascent - it2.ascent;
1355 *rtop = max (0, -it2.current_y);
1356 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1357 - it.last_visible_y));
1358 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1359 it.last_visible_y)
1360 - max (it2.current_y,
1361 WINDOW_HEADER_LINE_HEIGHT (w))));
1362 *vpos = it2.vpos;
1366 if (old_buffer)
1367 set_buffer_internal_1 (old_buffer);
1369 current_header_line_height = current_mode_line_height = -1;
1371 if (visible_p && XFASTINT (w->hscroll) > 0)
1372 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1374 #if 0
1375 /* Debugging code. */
1376 if (visible_p)
1377 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1378 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1379 else
1380 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1381 #endif
1383 return visible_p;
1387 /* Return the next character from STR which is MAXLEN bytes long.
1388 Return in *LEN the length of the character. This is like
1389 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1390 we find one, we return a `?', but with the length of the invalid
1391 character. */
1393 static INLINE int
1394 string_char_and_length (str, maxlen, len)
1395 const unsigned char *str;
1396 int maxlen, *len;
1398 int c;
1400 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1401 if (!CHAR_VALID_P (c, 1))
1402 /* We may not change the length here because other places in Emacs
1403 don't use this function, i.e. they silently accept invalid
1404 characters. */
1405 c = '?';
1407 return c;
1412 /* Given a position POS containing a valid character and byte position
1413 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1415 static struct text_pos
1416 string_pos_nchars_ahead (pos, string, nchars)
1417 struct text_pos pos;
1418 Lisp_Object string;
1419 int nchars;
1421 xassert (STRINGP (string) && nchars >= 0);
1423 if (STRING_MULTIBYTE (string))
1425 int rest = SBYTES (string) - BYTEPOS (pos);
1426 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1427 int len;
1429 while (nchars--)
1431 string_char_and_length (p, rest, &len);
1432 p += len, rest -= len;
1433 xassert (rest >= 0);
1434 CHARPOS (pos) += 1;
1435 BYTEPOS (pos) += len;
1438 else
1439 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1441 return pos;
1445 /* Value is the text position, i.e. character and byte position,
1446 for character position CHARPOS in STRING. */
1448 static INLINE struct text_pos
1449 string_pos (charpos, string)
1450 int charpos;
1451 Lisp_Object string;
1453 struct text_pos pos;
1454 xassert (STRINGP (string));
1455 xassert (charpos >= 0);
1456 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1457 return pos;
1461 /* Value is a text position, i.e. character and byte position, for
1462 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1463 means recognize multibyte characters. */
1465 static struct text_pos
1466 c_string_pos (charpos, s, multibyte_p)
1467 int charpos;
1468 unsigned char *s;
1469 int multibyte_p;
1471 struct text_pos pos;
1473 xassert (s != NULL);
1474 xassert (charpos >= 0);
1476 if (multibyte_p)
1478 int rest = strlen (s), len;
1480 SET_TEXT_POS (pos, 0, 0);
1481 while (charpos--)
1483 string_char_and_length (s, rest, &len);
1484 s += len, rest -= len;
1485 xassert (rest >= 0);
1486 CHARPOS (pos) += 1;
1487 BYTEPOS (pos) += len;
1490 else
1491 SET_TEXT_POS (pos, charpos, charpos);
1493 return pos;
1497 /* Value is the number of characters in C string S. MULTIBYTE_P
1498 non-zero means recognize multibyte characters. */
1500 static int
1501 number_of_chars (s, multibyte_p)
1502 unsigned char *s;
1503 int multibyte_p;
1505 int nchars;
1507 if (multibyte_p)
1509 int rest = strlen (s), len;
1510 unsigned char *p = (unsigned char *) s;
1512 for (nchars = 0; rest > 0; ++nchars)
1514 string_char_and_length (p, rest, &len);
1515 rest -= len, p += len;
1518 else
1519 nchars = strlen (s);
1521 return nchars;
1525 /* Compute byte position NEWPOS->bytepos corresponding to
1526 NEWPOS->charpos. POS is a known position in string STRING.
1527 NEWPOS->charpos must be >= POS.charpos. */
1529 static void
1530 compute_string_pos (newpos, pos, string)
1531 struct text_pos *newpos, pos;
1532 Lisp_Object string;
1534 xassert (STRINGP (string));
1535 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1537 if (STRING_MULTIBYTE (string))
1538 *newpos = string_pos_nchars_ahead (pos, string,
1539 CHARPOS (*newpos) - CHARPOS (pos));
1540 else
1541 BYTEPOS (*newpos) = CHARPOS (*newpos);
1544 /* EXPORT:
1545 Return an estimation of the pixel height of mode or top lines on
1546 frame F. FACE_ID specifies what line's height to estimate. */
1549 estimate_mode_line_height (f, face_id)
1550 struct frame *f;
1551 enum face_id face_id;
1553 #ifdef HAVE_WINDOW_SYSTEM
1554 if (FRAME_WINDOW_P (f))
1556 int height = FONT_HEIGHT (FRAME_FONT (f));
1558 /* This function is called so early when Emacs starts that the face
1559 cache and mode line face are not yet initialized. */
1560 if (FRAME_FACE_CACHE (f))
1562 struct face *face = FACE_FROM_ID (f, face_id);
1563 if (face)
1565 if (face->font)
1566 height = FONT_HEIGHT (face->font);
1567 if (face->box_line_width > 0)
1568 height += 2 * face->box_line_width;
1572 return height;
1574 #endif
1576 return 1;
1579 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1580 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1581 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1582 not force the value into range. */
1584 void
1585 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1586 FRAME_PTR f;
1587 register int pix_x, pix_y;
1588 int *x, *y;
1589 NativeRectangle *bounds;
1590 int noclip;
1593 #ifdef HAVE_WINDOW_SYSTEM
1594 if (FRAME_WINDOW_P (f))
1596 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1597 even for negative values. */
1598 if (pix_x < 0)
1599 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1600 if (pix_y < 0)
1601 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1603 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1604 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1606 if (bounds)
1607 STORE_NATIVE_RECT (*bounds,
1608 FRAME_COL_TO_PIXEL_X (f, pix_x),
1609 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1610 FRAME_COLUMN_WIDTH (f) - 1,
1611 FRAME_LINE_HEIGHT (f) - 1);
1613 if (!noclip)
1615 if (pix_x < 0)
1616 pix_x = 0;
1617 else if (pix_x > FRAME_TOTAL_COLS (f))
1618 pix_x = FRAME_TOTAL_COLS (f);
1620 if (pix_y < 0)
1621 pix_y = 0;
1622 else if (pix_y > FRAME_LINES (f))
1623 pix_y = FRAME_LINES (f);
1626 #endif
1628 *x = pix_x;
1629 *y = pix_y;
1633 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1634 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1635 can't tell the positions because W's display is not up to date,
1636 return 0. */
1639 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1640 struct window *w;
1641 int hpos, vpos;
1642 int *frame_x, *frame_y;
1644 #ifdef HAVE_WINDOW_SYSTEM
1645 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1647 int success_p;
1649 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1650 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1652 if (display_completed)
1654 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1655 struct glyph *glyph = row->glyphs[TEXT_AREA];
1656 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1658 hpos = row->x;
1659 vpos = row->y;
1660 while (glyph < end)
1662 hpos += glyph->pixel_width;
1663 ++glyph;
1666 /* If first glyph is partially visible, its first visible position is still 0. */
1667 if (hpos < 0)
1668 hpos = 0;
1670 success_p = 1;
1672 else
1674 hpos = vpos = 0;
1675 success_p = 0;
1678 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1679 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1680 return success_p;
1682 #endif
1684 *frame_x = hpos;
1685 *frame_y = vpos;
1686 return 1;
1690 #ifdef HAVE_WINDOW_SYSTEM
1692 /* Find the glyph under window-relative coordinates X/Y in window W.
1693 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1694 strings. Return in *HPOS and *VPOS the row and column number of
1695 the glyph found. Return in *AREA the glyph area containing X.
1696 Value is a pointer to the glyph found or null if X/Y is not on
1697 text, or we can't tell because W's current matrix is not up to
1698 date. */
1700 static struct glyph *
1701 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1702 struct window *w;
1703 int x, y;
1704 int *hpos, *vpos, *dx, *dy, *area;
1706 struct glyph *glyph, *end;
1707 struct glyph_row *row = NULL;
1708 int x0, i;
1710 /* Find row containing Y. Give up if some row is not enabled. */
1711 for (i = 0; i < w->current_matrix->nrows; ++i)
1713 row = MATRIX_ROW (w->current_matrix, i);
1714 if (!row->enabled_p)
1715 return NULL;
1716 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1717 break;
1720 *vpos = i;
1721 *hpos = 0;
1723 /* Give up if Y is not in the window. */
1724 if (i == w->current_matrix->nrows)
1725 return NULL;
1727 /* Get the glyph area containing X. */
1728 if (w->pseudo_window_p)
1730 *area = TEXT_AREA;
1731 x0 = 0;
1733 else
1735 if (x < window_box_left_offset (w, TEXT_AREA))
1737 *area = LEFT_MARGIN_AREA;
1738 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1740 else if (x < window_box_right_offset (w, TEXT_AREA))
1742 *area = TEXT_AREA;
1743 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1745 else
1747 *area = RIGHT_MARGIN_AREA;
1748 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1752 /* Find glyph containing X. */
1753 glyph = row->glyphs[*area];
1754 end = glyph + row->used[*area];
1755 x -= x0;
1756 while (glyph < end && x >= glyph->pixel_width)
1758 x -= glyph->pixel_width;
1759 ++glyph;
1762 if (glyph == end)
1763 return NULL;
1765 if (dx)
1767 *dx = x;
1768 *dy = y - (row->y + row->ascent - glyph->ascent);
1771 *hpos = glyph - row->glyphs[*area];
1772 return glyph;
1776 /* EXPORT:
1777 Convert frame-relative x/y to coordinates relative to window W.
1778 Takes pseudo-windows into account. */
1780 void
1781 frame_to_window_pixel_xy (w, x, y)
1782 struct window *w;
1783 int *x, *y;
1785 if (w->pseudo_window_p)
1787 /* A pseudo-window is always full-width, and starts at the
1788 left edge of the frame, plus a frame border. */
1789 struct frame *f = XFRAME (w->frame);
1790 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1791 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1793 else
1795 *x -= WINDOW_LEFT_EDGE_X (w);
1796 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1800 /* EXPORT:
1801 Return in RECTS[] at most N clipping rectangles for glyph string S.
1802 Return the number of stored rectangles. */
1805 get_glyph_string_clip_rects (s, rects, n)
1806 struct glyph_string *s;
1807 NativeRectangle *rects;
1808 int n;
1810 XRectangle r;
1812 if (n <= 0)
1813 return 0;
1815 if (s->row->full_width_p)
1817 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1818 r.x = WINDOW_LEFT_EDGE_X (s->w);
1819 r.width = WINDOW_TOTAL_WIDTH (s->w);
1821 /* Unless displaying a mode or menu bar line, which are always
1822 fully visible, clip to the visible part of the row. */
1823 if (s->w->pseudo_window_p)
1824 r.height = s->row->visible_height;
1825 else
1826 r.height = s->height;
1828 else
1830 /* This is a text line that may be partially visible. */
1831 r.x = window_box_left (s->w, s->area);
1832 r.width = window_box_width (s->w, s->area);
1833 r.height = s->row->visible_height;
1836 if (s->clip_head)
1837 if (r.x < s->clip_head->x)
1839 if (r.width >= s->clip_head->x - r.x)
1840 r.width -= s->clip_head->x - r.x;
1841 else
1842 r.width = 0;
1843 r.x = s->clip_head->x;
1845 if (s->clip_tail)
1846 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1848 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1849 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1850 else
1851 r.width = 0;
1854 /* If S draws overlapping rows, it's sufficient to use the top and
1855 bottom of the window for clipping because this glyph string
1856 intentionally draws over other lines. */
1857 if (s->for_overlaps)
1859 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1860 r.height = window_text_bottom_y (s->w) - r.y;
1862 /* Alas, the above simple strategy does not work for the
1863 environments with anti-aliased text: if the same text is
1864 drawn onto the same place multiple times, it gets thicker.
1865 If the overlap we are processing is for the erased cursor, we
1866 take the intersection with the rectagle of the cursor. */
1867 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1869 XRectangle rc, r_save = r;
1871 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1872 rc.y = s->w->phys_cursor.y;
1873 rc.width = s->w->phys_cursor_width;
1874 rc.height = s->w->phys_cursor_height;
1876 x_intersect_rectangles (&r_save, &rc, &r);
1879 else
1881 /* Don't use S->y for clipping because it doesn't take partially
1882 visible lines into account. For example, it can be negative for
1883 partially visible lines at the top of a window. */
1884 if (!s->row->full_width_p
1885 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1886 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1887 else
1888 r.y = max (0, s->row->y);
1890 /* If drawing a tool-bar window, draw it over the internal border
1891 at the top of the window. */
1892 if (WINDOWP (s->f->tool_bar_window)
1893 && s->w == XWINDOW (s->f->tool_bar_window))
1894 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1897 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1899 /* If drawing the cursor, don't let glyph draw outside its
1900 advertised boundaries. Cleartype does this under some circumstances. */
1901 if (s->hl == DRAW_CURSOR)
1903 struct glyph *glyph = s->first_glyph;
1904 int height, max_y;
1906 if (s->x > r.x)
1908 r.width -= s->x - r.x;
1909 r.x = s->x;
1911 r.width = min (r.width, glyph->pixel_width);
1913 /* If r.y is below window bottom, ensure that we still see a cursor. */
1914 height = min (glyph->ascent + glyph->descent,
1915 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1916 max_y = window_text_bottom_y (s->w) - height;
1917 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1918 if (s->ybase - glyph->ascent > max_y)
1920 r.y = max_y;
1921 r.height = height;
1923 else
1925 /* Don't draw cursor glyph taller than our actual glyph. */
1926 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1927 if (height < r.height)
1929 max_y = r.y + r.height;
1930 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1931 r.height = min (max_y - r.y, height);
1936 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1937 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1939 #ifdef CONVERT_FROM_XRECT
1940 CONVERT_FROM_XRECT (r, *rects);
1941 #else
1942 *rects = r;
1943 #endif
1944 return 1;
1946 else
1948 /* If we are processing overlapping and allowed to return
1949 multiple clipping rectangles, we exclude the row of the glyph
1950 string from the clipping rectangle. This is to avoid drawing
1951 the same text on the environment with anti-aliasing. */
1952 #ifdef CONVERT_FROM_XRECT
1953 XRectangle rs[2];
1954 #else
1955 XRectangle *rs = rects;
1956 #endif
1957 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1959 if (s->for_overlaps & OVERLAPS_PRED)
1961 rs[i] = r;
1962 if (r.y + r.height > row_y)
1964 if (r.y < row_y)
1965 rs[i].height = row_y - r.y;
1966 else
1967 rs[i].height = 0;
1969 i++;
1971 if (s->for_overlaps & OVERLAPS_SUCC)
1973 rs[i] = r;
1974 if (r.y < row_y + s->row->visible_height)
1976 if (r.y + r.height > row_y + s->row->visible_height)
1978 rs[i].y = row_y + s->row->visible_height;
1979 rs[i].height = r.y + r.height - rs[i].y;
1981 else
1982 rs[i].height = 0;
1984 i++;
1987 n = i;
1988 #ifdef CONVERT_FROM_XRECT
1989 for (i = 0; i < n; i++)
1990 CONVERT_FROM_XRECT (rs[i], rects[i]);
1991 #endif
1992 return n;
1996 /* EXPORT:
1997 Return in *NR the clipping rectangle for glyph string S. */
1999 void
2000 get_glyph_string_clip_rect (s, nr)
2001 struct glyph_string *s;
2002 NativeRectangle *nr;
2004 get_glyph_string_clip_rects (s, nr, 1);
2008 /* EXPORT:
2009 Return the position and height of the phys cursor in window W.
2010 Set w->phys_cursor_width to width of phys cursor.
2013 void
2014 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2015 struct window *w;
2016 struct glyph_row *row;
2017 struct glyph *glyph;
2018 int *xp, *yp, *heightp;
2020 struct frame *f = XFRAME (WINDOW_FRAME (w));
2021 int x, y, wd, h, h0, y0;
2023 /* Compute the width of the rectangle to draw. If on a stretch
2024 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2025 rectangle as wide as the glyph, but use a canonical character
2026 width instead. */
2027 wd = glyph->pixel_width - 1;
2028 #ifdef HAVE_NTGUI
2029 wd++; /* Why? */
2030 #endif
2032 x = w->phys_cursor.x;
2033 if (x < 0)
2035 wd += x;
2036 x = 0;
2039 if (glyph->type == STRETCH_GLYPH
2040 && !x_stretch_cursor_p)
2041 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2042 w->phys_cursor_width = wd;
2044 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2046 /* If y is below window bottom, ensure that we still see a cursor. */
2047 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2049 h = max (h0, glyph->ascent + glyph->descent);
2050 h0 = min (h0, glyph->ascent + glyph->descent);
2052 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2053 if (y < y0)
2055 h = max (h - (y0 - y) + 1, h0);
2056 y = y0 - 1;
2058 else
2060 y0 = window_text_bottom_y (w) - h0;
2061 if (y > y0)
2063 h += y - y0;
2064 y = y0;
2068 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2069 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2070 *heightp = h;
2074 * Remember which glyph the mouse is over.
2077 void
2078 remember_mouse_glyph (f, gx, gy, rect)
2079 struct frame *f;
2080 int gx, gy;
2081 NativeRectangle *rect;
2083 Lisp_Object window;
2084 struct window *w;
2085 struct glyph_row *r, *gr, *end_row;
2086 enum window_part part;
2087 enum glyph_row_area area;
2088 int x, y, width, height;
2090 /* Try to determine frame pixel position and size of the glyph under
2091 frame pixel coordinates X/Y on frame F. */
2093 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2094 if (NILP (window))
2096 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2097 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2098 goto virtual_glyph;
2101 w = XWINDOW (window);
2102 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2103 height = WINDOW_FRAME_LINE_HEIGHT (w);
2105 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2106 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2108 if (w->pseudo_window_p)
2110 area = TEXT_AREA;
2111 part = ON_MODE_LINE; /* Don't adjust margin. */
2112 goto text_glyph;
2115 switch (part)
2117 case ON_LEFT_MARGIN:
2118 area = LEFT_MARGIN_AREA;
2119 goto text_glyph;
2121 case ON_RIGHT_MARGIN:
2122 area = RIGHT_MARGIN_AREA;
2123 goto text_glyph;
2125 case ON_HEADER_LINE:
2126 case ON_MODE_LINE:
2127 gr = (part == ON_HEADER_LINE
2128 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2129 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2130 gy = gr->y;
2131 area = TEXT_AREA;
2132 goto text_glyph_row_found;
2134 case ON_TEXT:
2135 area = TEXT_AREA;
2137 text_glyph:
2138 gr = 0; gy = 0;
2139 for (; r <= end_row && r->enabled_p; ++r)
2140 if (r->y + r->height > y)
2142 gr = r; gy = r->y;
2143 break;
2146 text_glyph_row_found:
2147 if (gr && gy <= y)
2149 struct glyph *g = gr->glyphs[area];
2150 struct glyph *end = g + gr->used[area];
2152 height = gr->height;
2153 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2154 if (gx + g->pixel_width > x)
2155 break;
2157 if (g < end)
2159 if (g->type == IMAGE_GLYPH)
2161 /* Don't remember when mouse is over image, as
2162 image may have hot-spots. */
2163 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2164 return;
2166 width = g->pixel_width;
2168 else
2170 /* Use nominal char spacing at end of line. */
2171 x -= gx;
2172 gx += (x / width) * width;
2175 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2176 gx += window_box_left_offset (w, area);
2178 else
2180 /* Use nominal line height at end of window. */
2181 gx = (x / width) * width;
2182 y -= gy;
2183 gy += (y / height) * height;
2185 break;
2187 case ON_LEFT_FRINGE:
2188 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2189 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2190 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2191 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2192 goto row_glyph;
2194 case ON_RIGHT_FRINGE:
2195 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2196 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2197 : window_box_right_offset (w, TEXT_AREA));
2198 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2199 goto row_glyph;
2201 case ON_SCROLL_BAR:
2202 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2204 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2205 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2206 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2207 : 0)));
2208 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2210 row_glyph:
2211 gr = 0, gy = 0;
2212 for (; r <= end_row && r->enabled_p; ++r)
2213 if (r->y + r->height > y)
2215 gr = r; gy = r->y;
2216 break;
2219 if (gr && gy <= y)
2220 height = gr->height;
2221 else
2223 /* Use nominal line height at end of window. */
2224 y -= gy;
2225 gy += (y / height) * height;
2227 break;
2229 default:
2231 virtual_glyph:
2232 /* If there is no glyph under the mouse, then we divide the screen
2233 into a grid of the smallest glyph in the frame, and use that
2234 as our "glyph". */
2236 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2237 round down even for negative values. */
2238 if (gx < 0)
2239 gx -= width - 1;
2240 if (gy < 0)
2241 gy -= height - 1;
2243 gx = (gx / width) * width;
2244 gy = (gy / height) * height;
2246 goto store_rect;
2249 gx += WINDOW_LEFT_EDGE_X (w);
2250 gy += WINDOW_TOP_EDGE_Y (w);
2252 store_rect:
2253 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2255 /* Visible feedback for debugging. */
2256 #if 0
2257 #if HAVE_X_WINDOWS
2258 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2259 f->output_data.x->normal_gc,
2260 gx, gy, width, height);
2261 #endif
2262 #endif
2266 #endif /* HAVE_WINDOW_SYSTEM */
2269 /***********************************************************************
2270 Lisp form evaluation
2271 ***********************************************************************/
2273 /* Error handler for safe_eval and safe_call. */
2275 static Lisp_Object
2276 safe_eval_handler (arg)
2277 Lisp_Object arg;
2279 add_to_log ("Error during redisplay: %s", arg, Qnil);
2280 return Qnil;
2284 /* Evaluate SEXPR and return the result, or nil if something went
2285 wrong. Prevent redisplay during the evaluation. */
2287 Lisp_Object
2288 safe_eval (sexpr)
2289 Lisp_Object sexpr;
2291 Lisp_Object val;
2293 if (inhibit_eval_during_redisplay)
2294 val = Qnil;
2295 else
2297 int count = SPECPDL_INDEX ();
2298 struct gcpro gcpro1;
2300 GCPRO1 (sexpr);
2301 specbind (Qinhibit_redisplay, Qt);
2302 /* Use Qt to ensure debugger does not run,
2303 so there is no possibility of wanting to redisplay. */
2304 val = internal_condition_case_1 (Feval, sexpr, Qt,
2305 safe_eval_handler);
2306 UNGCPRO;
2307 val = unbind_to (count, val);
2310 return val;
2314 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2315 Return the result, or nil if something went wrong. Prevent
2316 redisplay during the evaluation. */
2318 Lisp_Object
2319 safe_call (nargs, args)
2320 int nargs;
2321 Lisp_Object *args;
2323 Lisp_Object val;
2325 if (inhibit_eval_during_redisplay)
2326 val = Qnil;
2327 else
2329 int count = SPECPDL_INDEX ();
2330 struct gcpro gcpro1;
2332 GCPRO1 (args[0]);
2333 gcpro1.nvars = nargs;
2334 specbind (Qinhibit_redisplay, Qt);
2335 /* Use Qt to ensure debugger does not run,
2336 so there is no possibility of wanting to redisplay. */
2337 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2338 safe_eval_handler);
2339 UNGCPRO;
2340 val = unbind_to (count, val);
2343 return val;
2347 /* Call function FN with one argument ARG.
2348 Return the result, or nil if something went wrong. */
2350 Lisp_Object
2351 safe_call1 (fn, arg)
2352 Lisp_Object fn, arg;
2354 Lisp_Object args[2];
2355 args[0] = fn;
2356 args[1] = arg;
2357 return safe_call (2, args);
2362 /***********************************************************************
2363 Debugging
2364 ***********************************************************************/
2366 #if 0
2368 /* Define CHECK_IT to perform sanity checks on iterators.
2369 This is for debugging. It is too slow to do unconditionally. */
2371 static void
2372 check_it (it)
2373 struct it *it;
2375 if (it->method == GET_FROM_STRING)
2377 xassert (STRINGP (it->string));
2378 xassert (IT_STRING_CHARPOS (*it) >= 0);
2380 else
2382 xassert (IT_STRING_CHARPOS (*it) < 0);
2383 if (it->method == GET_FROM_BUFFER)
2385 /* Check that character and byte positions agree. */
2386 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2390 if (it->dpvec)
2391 xassert (it->current.dpvec_index >= 0);
2392 else
2393 xassert (it->current.dpvec_index < 0);
2396 #define CHECK_IT(IT) check_it ((IT))
2398 #else /* not 0 */
2400 #define CHECK_IT(IT) (void) 0
2402 #endif /* not 0 */
2405 #if GLYPH_DEBUG
2407 /* Check that the window end of window W is what we expect it
2408 to be---the last row in the current matrix displaying text. */
2410 static void
2411 check_window_end (w)
2412 struct window *w;
2414 if (!MINI_WINDOW_P (w)
2415 && !NILP (w->window_end_valid))
2417 struct glyph_row *row;
2418 xassert ((row = MATRIX_ROW (w->current_matrix,
2419 XFASTINT (w->window_end_vpos)),
2420 !row->enabled_p
2421 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2422 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2426 #define CHECK_WINDOW_END(W) check_window_end ((W))
2428 #else /* not GLYPH_DEBUG */
2430 #define CHECK_WINDOW_END(W) (void) 0
2432 #endif /* not GLYPH_DEBUG */
2436 /***********************************************************************
2437 Iterator initialization
2438 ***********************************************************************/
2440 /* Initialize IT for displaying current_buffer in window W, starting
2441 at character position CHARPOS. CHARPOS < 0 means that no buffer
2442 position is specified which is useful when the iterator is assigned
2443 a position later. BYTEPOS is the byte position corresponding to
2444 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2446 If ROW is not null, calls to produce_glyphs with IT as parameter
2447 will produce glyphs in that row.
2449 BASE_FACE_ID is the id of a base face to use. It must be one of
2450 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2451 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2452 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2454 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2455 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2456 will be initialized to use the corresponding mode line glyph row of
2457 the desired matrix of W. */
2459 void
2460 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2461 struct it *it;
2462 struct window *w;
2463 int charpos, bytepos;
2464 struct glyph_row *row;
2465 enum face_id base_face_id;
2467 int highlight_region_p;
2469 /* Some precondition checks. */
2470 xassert (w != NULL && it != NULL);
2471 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2472 && charpos <= ZV));
2474 /* If face attributes have been changed since the last redisplay,
2475 free realized faces now because they depend on face definitions
2476 that might have changed. Don't free faces while there might be
2477 desired matrices pending which reference these faces. */
2478 if (face_change_count && !inhibit_free_realized_faces)
2480 face_change_count = 0;
2481 free_all_realized_faces (Qnil);
2484 /* Use one of the mode line rows of W's desired matrix if
2485 appropriate. */
2486 if (row == NULL)
2488 if (base_face_id == MODE_LINE_FACE_ID
2489 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2490 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2491 else if (base_face_id == HEADER_LINE_FACE_ID)
2492 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2495 /* Clear IT. */
2496 bzero (it, sizeof *it);
2497 it->current.overlay_string_index = -1;
2498 it->current.dpvec_index = -1;
2499 it->base_face_id = base_face_id;
2500 it->string = Qnil;
2501 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2503 /* The window in which we iterate over current_buffer: */
2504 XSETWINDOW (it->window, w);
2505 it->w = w;
2506 it->f = XFRAME (w->frame);
2508 /* Extra space between lines (on window systems only). */
2509 if (base_face_id == DEFAULT_FACE_ID
2510 && FRAME_WINDOW_P (it->f))
2512 if (NATNUMP (current_buffer->extra_line_spacing))
2513 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2514 else if (FLOATP (current_buffer->extra_line_spacing))
2515 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2516 * FRAME_LINE_HEIGHT (it->f));
2517 else if (it->f->extra_line_spacing > 0)
2518 it->extra_line_spacing = it->f->extra_line_spacing;
2519 it->max_extra_line_spacing = 0;
2522 /* If realized faces have been removed, e.g. because of face
2523 attribute changes of named faces, recompute them. When running
2524 in batch mode, the face cache of Vterminal_frame is null. If
2525 we happen to get called, make a dummy face cache. */
2526 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2527 init_frame_faces (it->f);
2528 if (FRAME_FACE_CACHE (it->f)->used == 0)
2529 recompute_basic_faces (it->f);
2531 /* Current value of the `slice', `space-width', and 'height' properties. */
2532 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2533 it->space_width = Qnil;
2534 it->font_height = Qnil;
2535 it->override_ascent = -1;
2537 /* Are control characters displayed as `^C'? */
2538 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2540 /* -1 means everything between a CR and the following line end
2541 is invisible. >0 means lines indented more than this value are
2542 invisible. */
2543 it->selective = (INTEGERP (current_buffer->selective_display)
2544 ? XFASTINT (current_buffer->selective_display)
2545 : (!NILP (current_buffer->selective_display)
2546 ? -1 : 0));
2547 it->selective_display_ellipsis_p
2548 = !NILP (current_buffer->selective_display_ellipses);
2550 /* Display table to use. */
2551 it->dp = window_display_table (w);
2553 /* Are multibyte characters enabled in current_buffer? */
2554 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2556 /* Non-zero if we should highlight the region. */
2557 highlight_region_p
2558 = (!NILP (Vtransient_mark_mode)
2559 && !NILP (current_buffer->mark_active)
2560 && XMARKER (current_buffer->mark)->buffer != 0);
2562 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2563 start and end of a visible region in window IT->w. Set both to
2564 -1 to indicate no region. */
2565 if (highlight_region_p
2566 /* Maybe highlight only in selected window. */
2567 && (/* Either show region everywhere. */
2568 highlight_nonselected_windows
2569 /* Or show region in the selected window. */
2570 || w == XWINDOW (selected_window)
2571 /* Or show the region if we are in the mini-buffer and W is
2572 the window the mini-buffer refers to. */
2573 || (MINI_WINDOW_P (XWINDOW (selected_window))
2574 && WINDOWP (minibuf_selected_window)
2575 && w == XWINDOW (minibuf_selected_window))))
2577 int charpos = marker_position (current_buffer->mark);
2578 it->region_beg_charpos = min (PT, charpos);
2579 it->region_end_charpos = max (PT, charpos);
2581 else
2582 it->region_beg_charpos = it->region_end_charpos = -1;
2584 /* Get the position at which the redisplay_end_trigger hook should
2585 be run, if it is to be run at all. */
2586 if (MARKERP (w->redisplay_end_trigger)
2587 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2588 it->redisplay_end_trigger_charpos
2589 = marker_position (w->redisplay_end_trigger);
2590 else if (INTEGERP (w->redisplay_end_trigger))
2591 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2593 /* Correct bogus values of tab_width. */
2594 it->tab_width = XINT (current_buffer->tab_width);
2595 if (it->tab_width <= 0 || it->tab_width > 1000)
2596 it->tab_width = 8;
2598 /* Are lines in the display truncated? */
2599 it->truncate_lines_p
2600 = (base_face_id != DEFAULT_FACE_ID
2601 || XINT (it->w->hscroll)
2602 || (truncate_partial_width_windows
2603 && !WINDOW_FULL_WIDTH_P (it->w))
2604 || !NILP (current_buffer->truncate_lines));
2606 /* Get dimensions of truncation and continuation glyphs. These are
2607 displayed as fringe bitmaps under X, so we don't need them for such
2608 frames. */
2609 if (!FRAME_WINDOW_P (it->f))
2611 if (it->truncate_lines_p)
2613 /* We will need the truncation glyph. */
2614 xassert (it->glyph_row == NULL);
2615 produce_special_glyphs (it, IT_TRUNCATION);
2616 it->truncation_pixel_width = it->pixel_width;
2618 else
2620 /* We will need the continuation glyph. */
2621 xassert (it->glyph_row == NULL);
2622 produce_special_glyphs (it, IT_CONTINUATION);
2623 it->continuation_pixel_width = it->pixel_width;
2626 /* Reset these values to zero because the produce_special_glyphs
2627 above has changed them. */
2628 it->pixel_width = it->ascent = it->descent = 0;
2629 it->phys_ascent = it->phys_descent = 0;
2632 /* Set this after getting the dimensions of truncation and
2633 continuation glyphs, so that we don't produce glyphs when calling
2634 produce_special_glyphs, above. */
2635 it->glyph_row = row;
2636 it->area = TEXT_AREA;
2638 /* Get the dimensions of the display area. The display area
2639 consists of the visible window area plus a horizontally scrolled
2640 part to the left of the window. All x-values are relative to the
2641 start of this total display area. */
2642 if (base_face_id != DEFAULT_FACE_ID)
2644 /* Mode lines, menu bar in terminal frames. */
2645 it->first_visible_x = 0;
2646 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2648 else
2650 it->first_visible_x
2651 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2652 it->last_visible_x = (it->first_visible_x
2653 + window_box_width (w, TEXT_AREA));
2655 /* If we truncate lines, leave room for the truncator glyph(s) at
2656 the right margin. Otherwise, leave room for the continuation
2657 glyph(s). Truncation and continuation glyphs are not inserted
2658 for window-based redisplay. */
2659 if (!FRAME_WINDOW_P (it->f))
2661 if (it->truncate_lines_p)
2662 it->last_visible_x -= it->truncation_pixel_width;
2663 else
2664 it->last_visible_x -= it->continuation_pixel_width;
2667 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2668 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2671 /* Leave room for a border glyph. */
2672 if (!FRAME_WINDOW_P (it->f)
2673 && !WINDOW_RIGHTMOST_P (it->w))
2674 it->last_visible_x -= 1;
2676 it->last_visible_y = window_text_bottom_y (w);
2678 /* For mode lines and alike, arrange for the first glyph having a
2679 left box line if the face specifies a box. */
2680 if (base_face_id != DEFAULT_FACE_ID)
2682 struct face *face;
2684 it->face_id = base_face_id;
2686 /* If we have a boxed mode line, make the first character appear
2687 with a left box line. */
2688 face = FACE_FROM_ID (it->f, base_face_id);
2689 if (face->box != FACE_NO_BOX)
2690 it->start_of_box_run_p = 1;
2693 /* If a buffer position was specified, set the iterator there,
2694 getting overlays and face properties from that position. */
2695 if (charpos >= BUF_BEG (current_buffer))
2697 it->end_charpos = ZV;
2698 it->face_id = -1;
2699 IT_CHARPOS (*it) = charpos;
2701 /* Compute byte position if not specified. */
2702 if (bytepos < charpos)
2703 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2704 else
2705 IT_BYTEPOS (*it) = bytepos;
2707 it->start = it->current;
2709 /* Compute faces etc. */
2710 reseat (it, it->current.pos, 1);
2713 CHECK_IT (it);
2717 /* Initialize IT for the display of window W with window start POS. */
2719 void
2720 start_display (it, w, pos)
2721 struct it *it;
2722 struct window *w;
2723 struct text_pos pos;
2725 struct glyph_row *row;
2726 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2728 row = w->desired_matrix->rows + first_vpos;
2729 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2730 it->first_vpos = first_vpos;
2732 /* Don't reseat to previous visible line start if current start
2733 position is in a string or image. */
2734 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2736 int start_at_line_beg_p;
2737 int first_y = it->current_y;
2739 /* If window start is not at a line start, skip forward to POS to
2740 get the correct continuation lines width. */
2741 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2742 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2743 if (!start_at_line_beg_p)
2745 int new_x;
2747 reseat_at_previous_visible_line_start (it);
2748 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2750 new_x = it->current_x + it->pixel_width;
2752 /* If lines are continued, this line may end in the middle
2753 of a multi-glyph character (e.g. a control character
2754 displayed as \003, or in the middle of an overlay
2755 string). In this case move_it_to above will not have
2756 taken us to the start of the continuation line but to the
2757 end of the continued line. */
2758 if (it->current_x > 0
2759 && !it->truncate_lines_p /* Lines are continued. */
2760 && (/* And glyph doesn't fit on the line. */
2761 new_x > it->last_visible_x
2762 /* Or it fits exactly and we're on a window
2763 system frame. */
2764 || (new_x == it->last_visible_x
2765 && FRAME_WINDOW_P (it->f))))
2767 if (it->current.dpvec_index >= 0
2768 || it->current.overlay_string_index >= 0)
2770 set_iterator_to_next (it, 1);
2771 move_it_in_display_line_to (it, -1, -1, 0);
2774 it->continuation_lines_width += it->current_x;
2777 /* We're starting a new display line, not affected by the
2778 height of the continued line, so clear the appropriate
2779 fields in the iterator structure. */
2780 it->max_ascent = it->max_descent = 0;
2781 it->max_phys_ascent = it->max_phys_descent = 0;
2783 it->current_y = first_y;
2784 it->vpos = 0;
2785 it->current_x = it->hpos = 0;
2789 #if 0 /* Don't assert the following because start_display is sometimes
2790 called intentionally with a window start that is not at a
2791 line start. Please leave this code in as a comment. */
2793 /* Window start should be on a line start, now. */
2794 xassert (it->continuation_lines_width
2795 || IT_CHARPOS (it) == BEGV
2796 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2797 #endif /* 0 */
2801 /* Return 1 if POS is a position in ellipses displayed for invisible
2802 text. W is the window we display, for text property lookup. */
2804 static int
2805 in_ellipses_for_invisible_text_p (pos, w)
2806 struct display_pos *pos;
2807 struct window *w;
2809 Lisp_Object prop, window;
2810 int ellipses_p = 0;
2811 int charpos = CHARPOS (pos->pos);
2813 /* If POS specifies a position in a display vector, this might
2814 be for an ellipsis displayed for invisible text. We won't
2815 get the iterator set up for delivering that ellipsis unless
2816 we make sure that it gets aware of the invisible text. */
2817 if (pos->dpvec_index >= 0
2818 && pos->overlay_string_index < 0
2819 && CHARPOS (pos->string_pos) < 0
2820 && charpos > BEGV
2821 && (XSETWINDOW (window, w),
2822 prop = Fget_char_property (make_number (charpos),
2823 Qinvisible, window),
2824 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2826 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2827 window);
2828 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2831 return ellipses_p;
2835 /* Initialize IT for stepping through current_buffer in window W,
2836 starting at position POS that includes overlay string and display
2837 vector/ control character translation position information. Value
2838 is zero if there are overlay strings with newlines at POS. */
2840 static int
2841 init_from_display_pos (it, w, pos)
2842 struct it *it;
2843 struct window *w;
2844 struct display_pos *pos;
2846 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2847 int i, overlay_strings_with_newlines = 0;
2849 /* If POS specifies a position in a display vector, this might
2850 be for an ellipsis displayed for invisible text. We won't
2851 get the iterator set up for delivering that ellipsis unless
2852 we make sure that it gets aware of the invisible text. */
2853 if (in_ellipses_for_invisible_text_p (pos, w))
2855 --charpos;
2856 bytepos = 0;
2859 /* Keep in mind: the call to reseat in init_iterator skips invisible
2860 text, so we might end up at a position different from POS. This
2861 is only a problem when POS is a row start after a newline and an
2862 overlay starts there with an after-string, and the overlay has an
2863 invisible property. Since we don't skip invisible text in
2864 display_line and elsewhere immediately after consuming the
2865 newline before the row start, such a POS will not be in a string,
2866 but the call to init_iterator below will move us to the
2867 after-string. */
2868 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2870 /* This only scans the current chunk -- it should scan all chunks.
2871 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2872 to 16 in 22.1 to make this a lesser problem. */
2873 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2875 const char *s = SDATA (it->overlay_strings[i]);
2876 const char *e = s + SBYTES (it->overlay_strings[i]);
2878 while (s < e && *s != '\n')
2879 ++s;
2881 if (s < e)
2883 overlay_strings_with_newlines = 1;
2884 break;
2888 /* If position is within an overlay string, set up IT to the right
2889 overlay string. */
2890 if (pos->overlay_string_index >= 0)
2892 int relative_index;
2894 /* If the first overlay string happens to have a `display'
2895 property for an image, the iterator will be set up for that
2896 image, and we have to undo that setup first before we can
2897 correct the overlay string index. */
2898 if (it->method == GET_FROM_IMAGE)
2899 pop_it (it);
2901 /* We already have the first chunk of overlay strings in
2902 IT->overlay_strings. Load more until the one for
2903 pos->overlay_string_index is in IT->overlay_strings. */
2904 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2906 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2907 it->current.overlay_string_index = 0;
2908 while (n--)
2910 load_overlay_strings (it, 0);
2911 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2915 it->current.overlay_string_index = pos->overlay_string_index;
2916 relative_index = (it->current.overlay_string_index
2917 % OVERLAY_STRING_CHUNK_SIZE);
2918 it->string = it->overlay_strings[relative_index];
2919 xassert (STRINGP (it->string));
2920 it->current.string_pos = pos->string_pos;
2921 it->method = GET_FROM_STRING;
2924 #if 0 /* This is bogus because POS not having an overlay string
2925 position does not mean it's after the string. Example: A
2926 line starting with a before-string and initialization of IT
2927 to the previous row's end position. */
2928 else if (it->current.overlay_string_index >= 0)
2930 /* If POS says we're already after an overlay string ending at
2931 POS, make sure to pop the iterator because it will be in
2932 front of that overlay string. When POS is ZV, we've thereby
2933 also ``processed'' overlay strings at ZV. */
2934 while (it->sp)
2935 pop_it (it);
2936 xassert (it->current.overlay_string_index == -1);
2937 xassert (it->method == GET_FROM_BUFFER);
2938 if (CHARPOS (pos->pos) == ZV)
2939 it->overlay_strings_at_end_processed_p = 1;
2941 #endif /* 0 */
2943 if (CHARPOS (pos->string_pos) >= 0)
2945 /* Recorded position is not in an overlay string, but in another
2946 string. This can only be a string from a `display' property.
2947 IT should already be filled with that string. */
2948 it->current.string_pos = pos->string_pos;
2949 xassert (STRINGP (it->string));
2952 /* Restore position in display vector translations, control
2953 character translations or ellipses. */
2954 if (pos->dpvec_index >= 0)
2956 if (it->dpvec == NULL)
2957 get_next_display_element (it);
2958 xassert (it->dpvec && it->current.dpvec_index == 0);
2959 it->current.dpvec_index = pos->dpvec_index;
2962 CHECK_IT (it);
2963 return !overlay_strings_with_newlines;
2967 /* Initialize IT for stepping through current_buffer in window W
2968 starting at ROW->start. */
2970 static void
2971 init_to_row_start (it, w, row)
2972 struct it *it;
2973 struct window *w;
2974 struct glyph_row *row;
2976 init_from_display_pos (it, w, &row->start);
2977 it->start = row->start;
2978 it->continuation_lines_width = row->continuation_lines_width;
2979 CHECK_IT (it);
2983 /* Initialize IT for stepping through current_buffer in window W
2984 starting in the line following ROW, i.e. starting at ROW->end.
2985 Value is zero if there are overlay strings with newlines at ROW's
2986 end position. */
2988 static int
2989 init_to_row_end (it, w, row)
2990 struct it *it;
2991 struct window *w;
2992 struct glyph_row *row;
2994 int success = 0;
2996 if (init_from_display_pos (it, w, &row->end))
2998 if (row->continued_p)
2999 it->continuation_lines_width
3000 = row->continuation_lines_width + row->pixel_width;
3001 CHECK_IT (it);
3002 success = 1;
3005 return success;
3011 /***********************************************************************
3012 Text properties
3013 ***********************************************************************/
3015 /* Called when IT reaches IT->stop_charpos. Handle text property and
3016 overlay changes. Set IT->stop_charpos to the next position where
3017 to stop. */
3019 static void
3020 handle_stop (it)
3021 struct it *it;
3023 enum prop_handled handled;
3024 int handle_overlay_change_p;
3025 struct props *p;
3027 it->dpvec = NULL;
3028 it->current.dpvec_index = -1;
3029 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3030 it->ignore_overlay_strings_at_pos_p = 0;
3032 /* Use face of preceding text for ellipsis (if invisible) */
3033 if (it->selective_display_ellipsis_p)
3034 it->saved_face_id = it->face_id;
3038 handled = HANDLED_NORMALLY;
3040 /* Call text property handlers. */
3041 for (p = it_props; p->handler; ++p)
3043 handled = p->handler (it);
3045 if (handled == HANDLED_RECOMPUTE_PROPS)
3046 break;
3047 else if (handled == HANDLED_RETURN)
3049 /* We still want to show before and after strings from
3050 overlays even if the actual buffer text is replaced. */
3051 if (!handle_overlay_change_p || it->sp > 1)
3052 return;
3053 if (!get_overlay_strings_1 (it, 0, 0))
3054 return;
3055 it->ignore_overlay_strings_at_pos_p = 1;
3056 it->string_from_display_prop_p = 0;
3057 handle_overlay_change_p = 0;
3058 handled = HANDLED_RECOMPUTE_PROPS;
3059 break;
3061 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3062 handle_overlay_change_p = 0;
3065 if (handled != HANDLED_RECOMPUTE_PROPS)
3067 /* Don't check for overlay strings below when set to deliver
3068 characters from a display vector. */
3069 if (it->method == GET_FROM_DISPLAY_VECTOR)
3070 handle_overlay_change_p = 0;
3072 /* Handle overlay changes. */
3073 if (handle_overlay_change_p)
3074 handled = handle_overlay_change (it);
3076 /* Determine where to stop next. */
3077 if (handled == HANDLED_NORMALLY)
3078 compute_stop_pos (it);
3081 while (handled == HANDLED_RECOMPUTE_PROPS);
3085 /* Compute IT->stop_charpos from text property and overlay change
3086 information for IT's current position. */
3088 static void
3089 compute_stop_pos (it)
3090 struct it *it;
3092 register INTERVAL iv, next_iv;
3093 Lisp_Object object, limit, position;
3095 /* If nowhere else, stop at the end. */
3096 it->stop_charpos = it->end_charpos;
3098 if (STRINGP (it->string))
3100 /* Strings are usually short, so don't limit the search for
3101 properties. */
3102 object = it->string;
3103 limit = Qnil;
3104 position = make_number (IT_STRING_CHARPOS (*it));
3106 else
3108 int charpos;
3110 /* If next overlay change is in front of the current stop pos
3111 (which is IT->end_charpos), stop there. Note: value of
3112 next_overlay_change is point-max if no overlay change
3113 follows. */
3114 charpos = next_overlay_change (IT_CHARPOS (*it));
3115 if (charpos < it->stop_charpos)
3116 it->stop_charpos = charpos;
3118 /* If showing the region, we have to stop at the region
3119 start or end because the face might change there. */
3120 if (it->region_beg_charpos > 0)
3122 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3123 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3124 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3125 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3128 /* Set up variables for computing the stop position from text
3129 property changes. */
3130 XSETBUFFER (object, current_buffer);
3131 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3132 position = make_number (IT_CHARPOS (*it));
3136 /* Get the interval containing IT's position. Value is a null
3137 interval if there isn't such an interval. */
3138 iv = validate_interval_range (object, &position, &position, 0);
3139 if (!NULL_INTERVAL_P (iv))
3141 Lisp_Object values_here[LAST_PROP_IDX];
3142 struct props *p;
3144 /* Get properties here. */
3145 for (p = it_props; p->handler; ++p)
3146 values_here[p->idx] = textget (iv->plist, *p->name);
3148 /* Look for an interval following iv that has different
3149 properties. */
3150 for (next_iv = next_interval (iv);
3151 (!NULL_INTERVAL_P (next_iv)
3152 && (NILP (limit)
3153 || XFASTINT (limit) > next_iv->position));
3154 next_iv = next_interval (next_iv))
3156 for (p = it_props; p->handler; ++p)
3158 Lisp_Object new_value;
3160 new_value = textget (next_iv->plist, *p->name);
3161 if (!EQ (values_here[p->idx], new_value))
3162 break;
3165 if (p->handler)
3166 break;
3169 if (!NULL_INTERVAL_P (next_iv))
3171 if (INTEGERP (limit)
3172 && next_iv->position >= XFASTINT (limit))
3173 /* No text property change up to limit. */
3174 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3175 else
3176 /* Text properties change in next_iv. */
3177 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3181 xassert (STRINGP (it->string)
3182 || (it->stop_charpos >= BEGV
3183 && it->stop_charpos >= IT_CHARPOS (*it)));
3187 /* Return the position of the next overlay change after POS in
3188 current_buffer. Value is point-max if no overlay change
3189 follows. This is like `next-overlay-change' but doesn't use
3190 xmalloc. */
3192 static int
3193 next_overlay_change (pos)
3194 int pos;
3196 int noverlays;
3197 int endpos;
3198 Lisp_Object *overlays;
3199 int i;
3201 /* Get all overlays at the given position. */
3202 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3204 /* If any of these overlays ends before endpos,
3205 use its ending point instead. */
3206 for (i = 0; i < noverlays; ++i)
3208 Lisp_Object oend;
3209 int oendpos;
3211 oend = OVERLAY_END (overlays[i]);
3212 oendpos = OVERLAY_POSITION (oend);
3213 endpos = min (endpos, oendpos);
3216 return endpos;
3221 /***********************************************************************
3222 Fontification
3223 ***********************************************************************/
3225 /* Handle changes in the `fontified' property of the current buffer by
3226 calling hook functions from Qfontification_functions to fontify
3227 regions of text. */
3229 static enum prop_handled
3230 handle_fontified_prop (it)
3231 struct it *it;
3233 Lisp_Object prop, pos;
3234 enum prop_handled handled = HANDLED_NORMALLY;
3236 if (!NILP (Vmemory_full))
3237 return handled;
3239 /* Get the value of the `fontified' property at IT's current buffer
3240 position. (The `fontified' property doesn't have a special
3241 meaning in strings.) If the value is nil, call functions from
3242 Qfontification_functions. */
3243 if (!STRINGP (it->string)
3244 && it->s == NULL
3245 && !NILP (Vfontification_functions)
3246 && !NILP (Vrun_hooks)
3247 && (pos = make_number (IT_CHARPOS (*it)),
3248 prop = Fget_char_property (pos, Qfontified, Qnil),
3249 /* Ignore the special cased nil value always present at EOB since
3250 no amount of fontifying will be able to change it. */
3251 NILP (prop) && IT_CHARPOS (*it) < Z))
3253 int count = SPECPDL_INDEX ();
3254 Lisp_Object val;
3256 val = Vfontification_functions;
3257 specbind (Qfontification_functions, Qnil);
3259 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3260 safe_call1 (val, pos);
3261 else
3263 Lisp_Object globals, fn;
3264 struct gcpro gcpro1, gcpro2;
3266 globals = Qnil;
3267 GCPRO2 (val, globals);
3269 for (; CONSP (val); val = XCDR (val))
3271 fn = XCAR (val);
3273 if (EQ (fn, Qt))
3275 /* A value of t indicates this hook has a local
3276 binding; it means to run the global binding too.
3277 In a global value, t should not occur. If it
3278 does, we must ignore it to avoid an endless
3279 loop. */
3280 for (globals = Fdefault_value (Qfontification_functions);
3281 CONSP (globals);
3282 globals = XCDR (globals))
3284 fn = XCAR (globals);
3285 if (!EQ (fn, Qt))
3286 safe_call1 (fn, pos);
3289 else
3290 safe_call1 (fn, pos);
3293 UNGCPRO;
3296 unbind_to (count, Qnil);
3298 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3299 something. This avoids an endless loop if they failed to
3300 fontify the text for which reason ever. */
3301 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3302 handled = HANDLED_RECOMPUTE_PROPS;
3305 return handled;
3310 /***********************************************************************
3311 Faces
3312 ***********************************************************************/
3314 /* Set up iterator IT from face properties at its current position.
3315 Called from handle_stop. */
3317 static enum prop_handled
3318 handle_face_prop (it)
3319 struct it *it;
3321 int new_face_id, next_stop;
3323 if (!STRINGP (it->string))
3325 new_face_id
3326 = face_at_buffer_position (it->w,
3327 IT_CHARPOS (*it),
3328 it->region_beg_charpos,
3329 it->region_end_charpos,
3330 &next_stop,
3331 (IT_CHARPOS (*it)
3332 + TEXT_PROP_DISTANCE_LIMIT),
3335 /* Is this a start of a run of characters with box face?
3336 Caveat: this can be called for a freshly initialized
3337 iterator; face_id is -1 in this case. We know that the new
3338 face will not change until limit, i.e. if the new face has a
3339 box, all characters up to limit will have one. But, as
3340 usual, we don't know whether limit is really the end. */
3341 if (new_face_id != it->face_id)
3343 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345 /* If new face has a box but old face has not, this is
3346 the start of a run of characters with box, i.e. it has
3347 a shadow on the left side. The value of face_id of the
3348 iterator will be -1 if this is the initial call that gets
3349 the face. In this case, we have to look in front of IT's
3350 position and see whether there is a face != new_face_id. */
3351 it->start_of_box_run_p
3352 = (new_face->box != FACE_NO_BOX
3353 && (it->face_id >= 0
3354 || IT_CHARPOS (*it) == BEG
3355 || new_face_id != face_before_it_pos (it)));
3356 it->face_box_p = new_face->box != FACE_NO_BOX;
3359 else
3361 int base_face_id, bufpos;
3363 if (it->current.overlay_string_index >= 0)
3364 bufpos = IT_CHARPOS (*it);
3365 else
3366 bufpos = 0;
3368 /* For strings from a buffer, i.e. overlay strings or strings
3369 from a `display' property, use the face at IT's current
3370 buffer position as the base face to merge with, so that
3371 overlay strings appear in the same face as surrounding
3372 text, unless they specify their own faces. */
3373 base_face_id = underlying_face_id (it);
3375 new_face_id = face_at_string_position (it->w,
3376 it->string,
3377 IT_STRING_CHARPOS (*it),
3378 bufpos,
3379 it->region_beg_charpos,
3380 it->region_end_charpos,
3381 &next_stop,
3382 base_face_id, 0);
3384 #if 0 /* This shouldn't be neccessary. Let's check it. */
3385 /* If IT is used to display a mode line we would really like to
3386 use the mode line face instead of the frame's default face. */
3387 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3388 && new_face_id == DEFAULT_FACE_ID)
3389 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3390 #endif
3392 /* Is this a start of a run of characters with box? Caveat:
3393 this can be called for a freshly allocated iterator; face_id
3394 is -1 is this case. We know that the new face will not
3395 change until the next check pos, i.e. if the new face has a
3396 box, all characters up to that position will have a
3397 box. But, as usual, we don't know whether that position
3398 is really the end. */
3399 if (new_face_id != it->face_id)
3401 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3402 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3404 /* If new face has a box but old face hasn't, this is the
3405 start of a run of characters with box, i.e. it has a
3406 shadow on the left side. */
3407 it->start_of_box_run_p
3408 = new_face->box && (old_face == NULL || !old_face->box);
3409 it->face_box_p = new_face->box != FACE_NO_BOX;
3413 it->face_id = new_face_id;
3414 return HANDLED_NORMALLY;
3418 /* Return the ID of the face ``underlying'' IT's current position,
3419 which is in a string. If the iterator is associated with a
3420 buffer, return the face at IT's current buffer position.
3421 Otherwise, use the iterator's base_face_id. */
3423 static int
3424 underlying_face_id (it)
3425 struct it *it;
3427 int face_id = it->base_face_id, i;
3429 xassert (STRINGP (it->string));
3431 for (i = it->sp - 1; i >= 0; --i)
3432 if (NILP (it->stack[i].string))
3433 face_id = it->stack[i].face_id;
3435 return face_id;
3439 /* Compute the face one character before or after the current position
3440 of IT. BEFORE_P non-zero means get the face in front of IT's
3441 position. Value is the id of the face. */
3443 static int
3444 face_before_or_after_it_pos (it, before_p)
3445 struct it *it;
3446 int before_p;
3448 int face_id, limit;
3449 int next_check_charpos;
3450 struct text_pos pos;
3452 xassert (it->s == NULL);
3454 if (STRINGP (it->string))
3456 int bufpos, base_face_id;
3458 /* No face change past the end of the string (for the case
3459 we are padding with spaces). No face change before the
3460 string start. */
3461 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3462 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3463 return it->face_id;
3465 /* Set pos to the position before or after IT's current position. */
3466 if (before_p)
3467 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3468 else
3469 /* For composition, we must check the character after the
3470 composition. */
3471 pos = (it->what == IT_COMPOSITION
3472 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3473 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3475 if (it->current.overlay_string_index >= 0)
3476 bufpos = IT_CHARPOS (*it);
3477 else
3478 bufpos = 0;
3480 base_face_id = underlying_face_id (it);
3482 /* Get the face for ASCII, or unibyte. */
3483 face_id = face_at_string_position (it->w,
3484 it->string,
3485 CHARPOS (pos),
3486 bufpos,
3487 it->region_beg_charpos,
3488 it->region_end_charpos,
3489 &next_check_charpos,
3490 base_face_id, 0);
3492 /* Correct the face for charsets different from ASCII. Do it
3493 for the multibyte case only. The face returned above is
3494 suitable for unibyte text if IT->string is unibyte. */
3495 if (STRING_MULTIBYTE (it->string))
3497 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3498 int rest = SBYTES (it->string) - BYTEPOS (pos);
3499 int c, len;
3500 struct face *face = FACE_FROM_ID (it->f, face_id);
3502 c = string_char_and_length (p, rest, &len);
3503 face_id = FACE_FOR_CHAR (it->f, face, c);
3506 else
3508 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3509 || (IT_CHARPOS (*it) <= BEGV && before_p))
3510 return it->face_id;
3512 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3513 pos = it->current.pos;
3515 if (before_p)
3516 DEC_TEXT_POS (pos, it->multibyte_p);
3517 else
3519 if (it->what == IT_COMPOSITION)
3520 /* For composition, we must check the position after the
3521 composition. */
3522 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3523 else
3524 INC_TEXT_POS (pos, it->multibyte_p);
3527 /* Determine face for CHARSET_ASCII, or unibyte. */
3528 face_id = face_at_buffer_position (it->w,
3529 CHARPOS (pos),
3530 it->region_beg_charpos,
3531 it->region_end_charpos,
3532 &next_check_charpos,
3533 limit, 0);
3535 /* Correct the face for charsets different from ASCII. Do it
3536 for the multibyte case only. The face returned above is
3537 suitable for unibyte text if current_buffer is unibyte. */
3538 if (it->multibyte_p)
3540 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3541 struct face *face = FACE_FROM_ID (it->f, face_id);
3542 face_id = FACE_FOR_CHAR (it->f, face, c);
3546 return face_id;
3551 /***********************************************************************
3552 Invisible text
3553 ***********************************************************************/
3555 /* Set up iterator IT from invisible properties at its current
3556 position. Called from handle_stop. */
3558 static enum prop_handled
3559 handle_invisible_prop (it)
3560 struct it *it;
3562 enum prop_handled handled = HANDLED_NORMALLY;
3564 if (STRINGP (it->string))
3566 extern Lisp_Object Qinvisible;
3567 Lisp_Object prop, end_charpos, limit, charpos;
3569 /* Get the value of the invisible text property at the
3570 current position. Value will be nil if there is no such
3571 property. */
3572 charpos = make_number (IT_STRING_CHARPOS (*it));
3573 prop = Fget_text_property (charpos, Qinvisible, it->string);
3575 if (!NILP (prop)
3576 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3578 handled = HANDLED_RECOMPUTE_PROPS;
3580 /* Get the position at which the next change of the
3581 invisible text property can be found in IT->string.
3582 Value will be nil if the property value is the same for
3583 all the rest of IT->string. */
3584 XSETINT (limit, SCHARS (it->string));
3585 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3586 it->string, limit);
3588 /* Text at current position is invisible. The next
3589 change in the property is at position end_charpos.
3590 Move IT's current position to that position. */
3591 if (INTEGERP (end_charpos)
3592 && XFASTINT (end_charpos) < XFASTINT (limit))
3594 struct text_pos old;
3595 old = it->current.string_pos;
3596 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3597 compute_string_pos (&it->current.string_pos, old, it->string);
3599 else
3601 /* The rest of the string is invisible. If this is an
3602 overlay string, proceed with the next overlay string
3603 or whatever comes and return a character from there. */
3604 if (it->current.overlay_string_index >= 0)
3606 next_overlay_string (it);
3607 /* Don't check for overlay strings when we just
3608 finished processing them. */
3609 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3611 else
3613 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3614 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3619 else
3621 int invis_p, newpos, next_stop, start_charpos;
3622 Lisp_Object pos, prop, overlay;
3624 /* First of all, is there invisible text at this position? */
3625 start_charpos = IT_CHARPOS (*it);
3626 pos = make_number (IT_CHARPOS (*it));
3627 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3628 &overlay);
3629 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3631 /* If we are on invisible text, skip over it. */
3632 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3634 /* Record whether we have to display an ellipsis for the
3635 invisible text. */
3636 int display_ellipsis_p = invis_p == 2;
3638 handled = HANDLED_RECOMPUTE_PROPS;
3640 /* Loop skipping over invisible text. The loop is left at
3641 ZV or with IT on the first char being visible again. */
3644 /* Try to skip some invisible text. Return value is the
3645 position reached which can be equal to IT's position
3646 if there is nothing invisible here. This skips both
3647 over invisible text properties and overlays with
3648 invisible property. */
3649 newpos = skip_invisible (IT_CHARPOS (*it),
3650 &next_stop, ZV, it->window);
3652 /* If we skipped nothing at all we weren't at invisible
3653 text in the first place. If everything to the end of
3654 the buffer was skipped, end the loop. */
3655 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3656 invis_p = 0;
3657 else
3659 /* We skipped some characters but not necessarily
3660 all there are. Check if we ended up on visible
3661 text. Fget_char_property returns the property of
3662 the char before the given position, i.e. if we
3663 get invis_p = 0, this means that the char at
3664 newpos is visible. */
3665 pos = make_number (newpos);
3666 prop = Fget_char_property (pos, Qinvisible, it->window);
3667 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3670 /* If we ended up on invisible text, proceed to
3671 skip starting with next_stop. */
3672 if (invis_p)
3673 IT_CHARPOS (*it) = next_stop;
3675 /* If there are adjacent invisible texts, don't lose the
3676 second one's ellipsis. */
3677 if (invis_p == 2)
3678 display_ellipsis_p = 1;
3680 while (invis_p);
3682 /* The position newpos is now either ZV or on visible text. */
3683 IT_CHARPOS (*it) = newpos;
3684 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3686 /* If there are before-strings at the start of invisible
3687 text, and the text is invisible because of a text
3688 property, arrange to show before-strings because 20.x did
3689 it that way. (If the text is invisible because of an
3690 overlay property instead of a text property, this is
3691 already handled in the overlay code.) */
3692 if (NILP (overlay)
3693 && get_overlay_strings (it, start_charpos))
3695 handled = HANDLED_RECOMPUTE_PROPS;
3696 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3698 else if (display_ellipsis_p)
3700 /* Make sure that the glyphs of the ellipsis will get
3701 correct `charpos' values. If we would not update
3702 it->position here, the glyphs would belong to the
3703 last visible character _before_ the invisible
3704 text, which confuses `set_cursor_from_row'.
3706 We use the last invisible position instead of the
3707 first because this way the cursor is always drawn on
3708 the first "." of the ellipsis, whenever PT is inside
3709 the invisible text. Otherwise the cursor would be
3710 placed _after_ the ellipsis when the point is after the
3711 first invisible character. */
3712 if (!STRINGP (it->object))
3714 it->position.charpos = IT_CHARPOS (*it) - 1;
3715 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3717 setup_for_ellipsis (it, 0);
3722 return handled;
3726 /* Make iterator IT return `...' next.
3727 Replaces LEN characters from buffer. */
3729 static void
3730 setup_for_ellipsis (it, len)
3731 struct it *it;
3732 int len;
3734 /* Use the display table definition for `...'. Invalid glyphs
3735 will be handled by the method returning elements from dpvec. */
3736 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3738 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3739 it->dpvec = v->contents;
3740 it->dpend = v->contents + v->size;
3742 else
3744 /* Default `...'. */
3745 it->dpvec = default_invis_vector;
3746 it->dpend = default_invis_vector + 3;
3749 it->dpvec_char_len = len;
3750 it->current.dpvec_index = 0;
3751 it->dpvec_face_id = -1;
3753 /* Remember the current face id in case glyphs specify faces.
3754 IT's face is restored in set_iterator_to_next.
3755 saved_face_id was set to preceding char's face in handle_stop. */
3756 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3757 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3759 it->method = GET_FROM_DISPLAY_VECTOR;
3760 it->ellipsis_p = 1;
3765 /***********************************************************************
3766 'display' property
3767 ***********************************************************************/
3769 /* Set up iterator IT from `display' property at its current position.
3770 Called from handle_stop.
3771 We return HANDLED_RETURN if some part of the display property
3772 overrides the display of the buffer text itself.
3773 Otherwise we return HANDLED_NORMALLY. */
3775 static enum prop_handled
3776 handle_display_prop (it)
3777 struct it *it;
3779 Lisp_Object prop, object;
3780 struct text_pos *position;
3781 /* Nonzero if some property replaces the display of the text itself. */
3782 int display_replaced_p = 0;
3784 if (STRINGP (it->string))
3786 object = it->string;
3787 position = &it->current.string_pos;
3789 else
3791 XSETWINDOW (object, it->w);
3792 position = &it->current.pos;
3795 /* Reset those iterator values set from display property values. */
3796 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3797 it->space_width = Qnil;
3798 it->font_height = Qnil;
3799 it->voffset = 0;
3801 /* We don't support recursive `display' properties, i.e. string
3802 values that have a string `display' property, that have a string
3803 `display' property etc. */
3804 if (!it->string_from_display_prop_p)
3805 it->area = TEXT_AREA;
3807 prop = Fget_char_property (make_number (position->charpos),
3808 Qdisplay, object);
3809 if (NILP (prop))
3810 return HANDLED_NORMALLY;
3812 if (!STRINGP (it->string))
3813 object = it->w->buffer;
3815 if (CONSP (prop)
3816 /* Simple properties. */
3817 && !EQ (XCAR (prop), Qimage)
3818 && !EQ (XCAR (prop), Qspace)
3819 && !EQ (XCAR (prop), Qwhen)
3820 && !EQ (XCAR (prop), Qslice)
3821 && !EQ (XCAR (prop), Qspace_width)
3822 && !EQ (XCAR (prop), Qheight)
3823 && !EQ (XCAR (prop), Qraise)
3824 /* Marginal area specifications. */
3825 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3826 && !EQ (XCAR (prop), Qleft_fringe)
3827 && !EQ (XCAR (prop), Qright_fringe)
3828 && !NILP (XCAR (prop)))
3830 for (; CONSP (prop); prop = XCDR (prop))
3832 if (handle_single_display_spec (it, XCAR (prop), object,
3833 position, display_replaced_p))
3834 display_replaced_p = 1;
3837 else if (VECTORP (prop))
3839 int i;
3840 for (i = 0; i < ASIZE (prop); ++i)
3841 if (handle_single_display_spec (it, AREF (prop, i), object,
3842 position, display_replaced_p))
3843 display_replaced_p = 1;
3845 else
3847 int ret = handle_single_display_spec (it, prop, object, position, 0);
3848 if (ret < 0) /* Replaced by "", i.e. nothing. */
3849 return HANDLED_RECOMPUTE_PROPS;
3850 if (ret)
3851 display_replaced_p = 1;
3854 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3858 /* Value is the position of the end of the `display' property starting
3859 at START_POS in OBJECT. */
3861 static struct text_pos
3862 display_prop_end (it, object, start_pos)
3863 struct it *it;
3864 Lisp_Object object;
3865 struct text_pos start_pos;
3867 Lisp_Object end;
3868 struct text_pos end_pos;
3870 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3871 Qdisplay, object, Qnil);
3872 CHARPOS (end_pos) = XFASTINT (end);
3873 if (STRINGP (object))
3874 compute_string_pos (&end_pos, start_pos, it->string);
3875 else
3876 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3878 return end_pos;
3882 /* Set up IT from a single `display' specification PROP. OBJECT
3883 is the object in which the `display' property was found. *POSITION
3884 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3885 means that we previously saw a display specification which already
3886 replaced text display with something else, for example an image;
3887 we ignore such properties after the first one has been processed.
3889 If PROP is a `space' or `image' specification, and in some other
3890 cases too, set *POSITION to the position where the `display'
3891 property ends.
3893 Value is non-zero if something was found which replaces the display
3894 of buffer or string text. Specifically, the value is -1 if that
3895 "something" is "nothing". */
3897 static int
3898 handle_single_display_spec (it, spec, object, position,
3899 display_replaced_before_p)
3900 struct it *it;
3901 Lisp_Object spec;
3902 Lisp_Object object;
3903 struct text_pos *position;
3904 int display_replaced_before_p;
3906 Lisp_Object form;
3907 Lisp_Object location, value;
3908 struct text_pos start_pos, save_pos;
3909 int valid_p;
3911 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3912 If the result is non-nil, use VALUE instead of SPEC. */
3913 form = Qt;
3914 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3916 spec = XCDR (spec);
3917 if (!CONSP (spec))
3918 return 0;
3919 form = XCAR (spec);
3920 spec = XCDR (spec);
3923 if (!NILP (form) && !EQ (form, Qt))
3925 int count = SPECPDL_INDEX ();
3926 struct gcpro gcpro1;
3928 /* Bind `object' to the object having the `display' property, a
3929 buffer or string. Bind `position' to the position in the
3930 object where the property was found, and `buffer-position'
3931 to the current position in the buffer. */
3932 specbind (Qobject, object);
3933 specbind (Qposition, make_number (CHARPOS (*position)));
3934 specbind (Qbuffer_position,
3935 make_number (STRINGP (object)
3936 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3937 GCPRO1 (form);
3938 form = safe_eval (form);
3939 UNGCPRO;
3940 unbind_to (count, Qnil);
3943 if (NILP (form))
3944 return 0;
3946 /* Handle `(height HEIGHT)' specifications. */
3947 if (CONSP (spec)
3948 && EQ (XCAR (spec), Qheight)
3949 && CONSP (XCDR (spec)))
3951 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3952 return 0;
3954 it->font_height = XCAR (XCDR (spec));
3955 if (!NILP (it->font_height))
3957 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3958 int new_height = -1;
3960 if (CONSP (it->font_height)
3961 && (EQ (XCAR (it->font_height), Qplus)
3962 || EQ (XCAR (it->font_height), Qminus))
3963 && CONSP (XCDR (it->font_height))
3964 && INTEGERP (XCAR (XCDR (it->font_height))))
3966 /* `(+ N)' or `(- N)' where N is an integer. */
3967 int steps = XINT (XCAR (XCDR (it->font_height)));
3968 if (EQ (XCAR (it->font_height), Qplus))
3969 steps = - steps;
3970 it->face_id = smaller_face (it->f, it->face_id, steps);
3972 else if (FUNCTIONP (it->font_height))
3974 /* Call function with current height as argument.
3975 Value is the new height. */
3976 Lisp_Object height;
3977 height = safe_call1 (it->font_height,
3978 face->lface[LFACE_HEIGHT_INDEX]);
3979 if (NUMBERP (height))
3980 new_height = XFLOATINT (height);
3982 else if (NUMBERP (it->font_height))
3984 /* Value is a multiple of the canonical char height. */
3985 struct face *face;
3987 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3988 new_height = (XFLOATINT (it->font_height)
3989 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3991 else
3993 /* Evaluate IT->font_height with `height' bound to the
3994 current specified height to get the new height. */
3995 int count = SPECPDL_INDEX ();
3997 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3998 value = safe_eval (it->font_height);
3999 unbind_to (count, Qnil);
4001 if (NUMBERP (value))
4002 new_height = XFLOATINT (value);
4005 if (new_height > 0)
4006 it->face_id = face_with_height (it->f, it->face_id, new_height);
4009 return 0;
4012 /* Handle `(space_width WIDTH)'. */
4013 if (CONSP (spec)
4014 && EQ (XCAR (spec), Qspace_width)
4015 && CONSP (XCDR (spec)))
4017 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4018 return 0;
4020 value = XCAR (XCDR (spec));
4021 if (NUMBERP (value) && XFLOATINT (value) > 0)
4022 it->space_width = value;
4024 return 0;
4027 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4028 if (CONSP (spec)
4029 && EQ (XCAR (spec), Qslice))
4031 Lisp_Object tem;
4033 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4034 return 0;
4036 if (tem = XCDR (spec), CONSP (tem))
4038 it->slice.x = XCAR (tem);
4039 if (tem = XCDR (tem), CONSP (tem))
4041 it->slice.y = XCAR (tem);
4042 if (tem = XCDR (tem), CONSP (tem))
4044 it->slice.width = XCAR (tem);
4045 if (tem = XCDR (tem), CONSP (tem))
4046 it->slice.height = XCAR (tem);
4051 return 0;
4054 /* Handle `(raise FACTOR)'. */
4055 if (CONSP (spec)
4056 && EQ (XCAR (spec), Qraise)
4057 && CONSP (XCDR (spec)))
4059 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4060 return 0;
4062 #ifdef HAVE_WINDOW_SYSTEM
4063 value = XCAR (XCDR (spec));
4064 if (NUMBERP (value))
4066 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4067 it->voffset = - (XFLOATINT (value)
4068 * (FONT_HEIGHT (face->font)));
4070 #endif /* HAVE_WINDOW_SYSTEM */
4072 return 0;
4075 /* Don't handle the other kinds of display specifications
4076 inside a string that we got from a `display' property. */
4077 if (it->string_from_display_prop_p)
4078 return 0;
4080 /* Characters having this form of property are not displayed, so
4081 we have to find the end of the property. */
4082 start_pos = *position;
4083 *position = display_prop_end (it, object, start_pos);
4084 value = Qnil;
4086 /* Stop the scan at that end position--we assume that all
4087 text properties change there. */
4088 it->stop_charpos = position->charpos;
4090 /* Handle `(left-fringe BITMAP [FACE])'
4091 and `(right-fringe BITMAP [FACE])'. */
4092 if (CONSP (spec)
4093 && (EQ (XCAR (spec), Qleft_fringe)
4094 || EQ (XCAR (spec), Qright_fringe))
4095 && CONSP (XCDR (spec)))
4097 int face_id = DEFAULT_FACE_ID;
4098 int fringe_bitmap;
4100 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4101 /* If we return here, POSITION has been advanced
4102 across the text with this property. */
4103 return 0;
4105 #ifdef HAVE_WINDOW_SYSTEM
4106 value = XCAR (XCDR (spec));
4107 if (!SYMBOLP (value)
4108 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4109 /* If we return here, POSITION has been advanced
4110 across the text with this property. */
4111 return 0;
4113 if (CONSP (XCDR (XCDR (spec))))
4115 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4116 int face_id2 = lookup_derived_face (it->f, face_name,
4117 'A', FRINGE_FACE_ID, 0);
4118 if (face_id2 >= 0)
4119 face_id = face_id2;
4122 /* Save current settings of IT so that we can restore them
4123 when we are finished with the glyph property value. */
4125 save_pos = it->position;
4126 it->position = *position;
4127 push_it (it);
4128 it->position = save_pos;
4130 it->area = TEXT_AREA;
4131 it->what = IT_IMAGE;
4132 it->image_id = -1; /* no image */
4133 it->position = start_pos;
4134 it->object = NILP (object) ? it->w->buffer : object;
4135 it->method = GET_FROM_IMAGE;
4136 it->face_id = face_id;
4138 /* Say that we haven't consumed the characters with
4139 `display' property yet. The call to pop_it in
4140 set_iterator_to_next will clean this up. */
4141 *position = start_pos;
4143 if (EQ (XCAR (spec), Qleft_fringe))
4145 it->left_user_fringe_bitmap = fringe_bitmap;
4146 it->left_user_fringe_face_id = face_id;
4148 else
4150 it->right_user_fringe_bitmap = fringe_bitmap;
4151 it->right_user_fringe_face_id = face_id;
4153 #endif /* HAVE_WINDOW_SYSTEM */
4154 return 1;
4157 /* Prepare to handle `((margin left-margin) ...)',
4158 `((margin right-margin) ...)' and `((margin nil) ...)'
4159 prefixes for display specifications. */
4160 location = Qunbound;
4161 if (CONSP (spec) && CONSP (XCAR (spec)))
4163 Lisp_Object tem;
4165 value = XCDR (spec);
4166 if (CONSP (value))
4167 value = XCAR (value);
4169 tem = XCAR (spec);
4170 if (EQ (XCAR (tem), Qmargin)
4171 && (tem = XCDR (tem),
4172 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4173 (NILP (tem)
4174 || EQ (tem, Qleft_margin)
4175 || EQ (tem, Qright_margin))))
4176 location = tem;
4179 if (EQ (location, Qunbound))
4181 location = Qnil;
4182 value = spec;
4185 /* After this point, VALUE is the property after any
4186 margin prefix has been stripped. It must be a string,
4187 an image specification, or `(space ...)'.
4189 LOCATION specifies where to display: `left-margin',
4190 `right-margin' or nil. */
4192 valid_p = (STRINGP (value)
4193 #ifdef HAVE_WINDOW_SYSTEM
4194 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4195 #endif /* not HAVE_WINDOW_SYSTEM */
4196 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4198 if (valid_p && !display_replaced_before_p)
4200 /* Save current settings of IT so that we can restore them
4201 when we are finished with the glyph property value. */
4202 save_pos = it->position;
4203 it->position = *position;
4204 push_it (it);
4205 it->position = save_pos;
4207 if (NILP (location))
4208 it->area = TEXT_AREA;
4209 else if (EQ (location, Qleft_margin))
4210 it->area = LEFT_MARGIN_AREA;
4211 else
4212 it->area = RIGHT_MARGIN_AREA;
4214 if (STRINGP (value))
4216 if (SCHARS (value) == 0)
4218 pop_it (it);
4219 return -1; /* Replaced by "", i.e. nothing. */
4221 it->string = value;
4222 it->multibyte_p = STRING_MULTIBYTE (it->string);
4223 it->current.overlay_string_index = -1;
4224 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4225 it->end_charpos = it->string_nchars = SCHARS (it->string);
4226 it->method = GET_FROM_STRING;
4227 it->stop_charpos = 0;
4228 it->string_from_display_prop_p = 1;
4229 /* Say that we haven't consumed the characters with
4230 `display' property yet. The call to pop_it in
4231 set_iterator_to_next will clean this up. */
4232 *position = start_pos;
4234 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4236 it->method = GET_FROM_STRETCH;
4237 it->object = value;
4238 *position = it->position = start_pos;
4240 #ifdef HAVE_WINDOW_SYSTEM
4241 else
4243 it->what = IT_IMAGE;
4244 it->image_id = lookup_image (it->f, value);
4245 it->position = start_pos;
4246 it->object = NILP (object) ? it->w->buffer : object;
4247 it->method = GET_FROM_IMAGE;
4249 /* Say that we haven't consumed the characters with
4250 `display' property yet. The call to pop_it in
4251 set_iterator_to_next will clean this up. */
4252 *position = start_pos;
4254 #endif /* HAVE_WINDOW_SYSTEM */
4256 return 1;
4259 /* Invalid property or property not supported. Restore
4260 POSITION to what it was before. */
4261 *position = start_pos;
4262 return 0;
4266 /* Check if SPEC is a display specification value whose text should be
4267 treated as intangible. */
4269 static int
4270 single_display_spec_intangible_p (prop)
4271 Lisp_Object prop;
4273 /* Skip over `when FORM'. */
4274 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4276 prop = XCDR (prop);
4277 if (!CONSP (prop))
4278 return 0;
4279 prop = XCDR (prop);
4282 if (STRINGP (prop))
4283 return 1;
4285 if (!CONSP (prop))
4286 return 0;
4288 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4289 we don't need to treat text as intangible. */
4290 if (EQ (XCAR (prop), Qmargin))
4292 prop = XCDR (prop);
4293 if (!CONSP (prop))
4294 return 0;
4296 prop = XCDR (prop);
4297 if (!CONSP (prop)
4298 || EQ (XCAR (prop), Qleft_margin)
4299 || EQ (XCAR (prop), Qright_margin))
4300 return 0;
4303 return (CONSP (prop)
4304 && (EQ (XCAR (prop), Qimage)
4305 || EQ (XCAR (prop), Qspace)));
4309 /* Check if PROP is a display property value whose text should be
4310 treated as intangible. */
4313 display_prop_intangible_p (prop)
4314 Lisp_Object prop;
4316 if (CONSP (prop)
4317 && CONSP (XCAR (prop))
4318 && !EQ (Qmargin, XCAR (XCAR (prop))))
4320 /* A list of sub-properties. */
4321 while (CONSP (prop))
4323 if (single_display_spec_intangible_p (XCAR (prop)))
4324 return 1;
4325 prop = XCDR (prop);
4328 else if (VECTORP (prop))
4330 /* A vector of sub-properties. */
4331 int i;
4332 for (i = 0; i < ASIZE (prop); ++i)
4333 if (single_display_spec_intangible_p (AREF (prop, i)))
4334 return 1;
4336 else
4337 return single_display_spec_intangible_p (prop);
4339 return 0;
4343 /* Return 1 if PROP is a display sub-property value containing STRING. */
4345 static int
4346 single_display_spec_string_p (prop, string)
4347 Lisp_Object prop, string;
4349 if (EQ (string, prop))
4350 return 1;
4352 /* Skip over `when FORM'. */
4353 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4355 prop = XCDR (prop);
4356 if (!CONSP (prop))
4357 return 0;
4358 prop = XCDR (prop);
4361 if (CONSP (prop))
4362 /* Skip over `margin LOCATION'. */
4363 if (EQ (XCAR (prop), Qmargin))
4365 prop = XCDR (prop);
4366 if (!CONSP (prop))
4367 return 0;
4369 prop = XCDR (prop);
4370 if (!CONSP (prop))
4371 return 0;
4374 return CONSP (prop) && EQ (XCAR (prop), string);
4378 /* Return 1 if STRING appears in the `display' property PROP. */
4380 static int
4381 display_prop_string_p (prop, string)
4382 Lisp_Object prop, string;
4384 if (CONSP (prop)
4385 && CONSP (XCAR (prop))
4386 && !EQ (Qmargin, XCAR (XCAR (prop))))
4388 /* A list of sub-properties. */
4389 while (CONSP (prop))
4391 if (single_display_spec_string_p (XCAR (prop), string))
4392 return 1;
4393 prop = XCDR (prop);
4396 else if (VECTORP (prop))
4398 /* A vector of sub-properties. */
4399 int i;
4400 for (i = 0; i < ASIZE (prop); ++i)
4401 if (single_display_spec_string_p (AREF (prop, i), string))
4402 return 1;
4404 else
4405 return single_display_spec_string_p (prop, string);
4407 return 0;
4411 /* Determine from which buffer position in W's buffer STRING comes
4412 from. AROUND_CHARPOS is an approximate position where it could
4413 be from. Value is the buffer position or 0 if it couldn't be
4414 determined.
4416 W's buffer must be current.
4418 This function is necessary because we don't record buffer positions
4419 in glyphs generated from strings (to keep struct glyph small).
4420 This function may only use code that doesn't eval because it is
4421 called asynchronously from note_mouse_highlight. */
4424 string_buffer_position (w, string, around_charpos)
4425 struct window *w;
4426 Lisp_Object string;
4427 int around_charpos;
4429 Lisp_Object limit, prop, pos;
4430 const int MAX_DISTANCE = 1000;
4431 int found = 0;
4433 pos = make_number (around_charpos);
4434 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4435 while (!found && !EQ (pos, limit))
4437 prop = Fget_char_property (pos, Qdisplay, Qnil);
4438 if (!NILP (prop) && display_prop_string_p (prop, string))
4439 found = 1;
4440 else
4441 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4444 if (!found)
4446 pos = make_number (around_charpos);
4447 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4448 while (!found && !EQ (pos, limit))
4450 prop = Fget_char_property (pos, Qdisplay, Qnil);
4451 if (!NILP (prop) && display_prop_string_p (prop, string))
4452 found = 1;
4453 else
4454 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4455 limit);
4459 return found ? XINT (pos) : 0;
4464 /***********************************************************************
4465 `composition' property
4466 ***********************************************************************/
4468 /* Set up iterator IT from `composition' property at its current
4469 position. Called from handle_stop. */
4471 static enum prop_handled
4472 handle_composition_prop (it)
4473 struct it *it;
4475 Lisp_Object prop, string;
4476 int pos, pos_byte, end;
4477 enum prop_handled handled = HANDLED_NORMALLY;
4479 if (STRINGP (it->string))
4481 pos = IT_STRING_CHARPOS (*it);
4482 pos_byte = IT_STRING_BYTEPOS (*it);
4483 string = it->string;
4485 else
4487 pos = IT_CHARPOS (*it);
4488 pos_byte = IT_BYTEPOS (*it);
4489 string = Qnil;
4492 /* If there's a valid composition and point is not inside of the
4493 composition (in the case that the composition is from the current
4494 buffer), draw a glyph composed from the composition components. */
4495 if (find_composition (pos, -1, &pos, &end, &prop, string)
4496 && COMPOSITION_VALID_P (pos, end, prop)
4497 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4499 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4501 if (id >= 0)
4503 struct composition *cmp = composition_table[id];
4505 if (cmp->glyph_len == 0)
4507 /* No glyph. */
4508 if (STRINGP (it->string))
4510 IT_STRING_CHARPOS (*it) = end;
4511 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4512 end);
4514 else
4516 IT_CHARPOS (*it) = end;
4517 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4519 return HANDLED_RECOMPUTE_PROPS;
4522 it->stop_charpos = end;
4523 push_it (it);
4525 it->method = GET_FROM_COMPOSITION;
4526 it->cmp_id = id;
4527 it->cmp_len = COMPOSITION_LENGTH (prop);
4528 /* For a terminal, draw only the first character of the
4529 components. */
4530 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4531 it->len = (STRINGP (it->string)
4532 ? string_char_to_byte (it->string, end)
4533 : CHAR_TO_BYTE (end)) - pos_byte;
4534 handled = HANDLED_RETURN;
4538 return handled;
4543 /***********************************************************************
4544 Overlay strings
4545 ***********************************************************************/
4547 /* The following structure is used to record overlay strings for
4548 later sorting in load_overlay_strings. */
4550 struct overlay_entry
4552 Lisp_Object overlay;
4553 Lisp_Object string;
4554 int priority;
4555 int after_string_p;
4559 /* Set up iterator IT from overlay strings at its current position.
4560 Called from handle_stop. */
4562 static enum prop_handled
4563 handle_overlay_change (it)
4564 struct it *it;
4566 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4567 return HANDLED_RECOMPUTE_PROPS;
4568 else
4569 return HANDLED_NORMALLY;
4573 /* Set up the next overlay string for delivery by IT, if there is an
4574 overlay string to deliver. Called by set_iterator_to_next when the
4575 end of the current overlay string is reached. If there are more
4576 overlay strings to display, IT->string and
4577 IT->current.overlay_string_index are set appropriately here.
4578 Otherwise IT->string is set to nil. */
4580 static void
4581 next_overlay_string (it)
4582 struct it *it;
4584 ++it->current.overlay_string_index;
4585 if (it->current.overlay_string_index == it->n_overlay_strings)
4587 /* No more overlay strings. Restore IT's settings to what
4588 they were before overlay strings were processed, and
4589 continue to deliver from current_buffer. */
4590 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4592 pop_it (it);
4593 xassert (it->sp > 0
4594 || it->method == GET_FROM_COMPOSITION
4595 || (NILP (it->string)
4596 && it->method == GET_FROM_BUFFER
4597 && it->stop_charpos >= BEGV
4598 && it->stop_charpos <= it->end_charpos));
4599 it->current.overlay_string_index = -1;
4600 it->n_overlay_strings = 0;
4602 /* If we're at the end of the buffer, record that we have
4603 processed the overlay strings there already, so that
4604 next_element_from_buffer doesn't try it again. */
4605 if (IT_CHARPOS (*it) >= it->end_charpos)
4606 it->overlay_strings_at_end_processed_p = 1;
4608 /* If we have to display `...' for invisible text, set
4609 the iterator up for that. */
4610 if (display_ellipsis_p)
4611 setup_for_ellipsis (it, 0);
4613 else
4615 /* There are more overlay strings to process. If
4616 IT->current.overlay_string_index has advanced to a position
4617 where we must load IT->overlay_strings with more strings, do
4618 it. */
4619 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4621 if (it->current.overlay_string_index && i == 0)
4622 load_overlay_strings (it, 0);
4624 /* Initialize IT to deliver display elements from the overlay
4625 string. */
4626 it->string = it->overlay_strings[i];
4627 it->multibyte_p = STRING_MULTIBYTE (it->string);
4628 SET_TEXT_POS (it->current.string_pos, 0, 0);
4629 it->method = GET_FROM_STRING;
4630 it->stop_charpos = 0;
4633 CHECK_IT (it);
4637 /* Compare two overlay_entry structures E1 and E2. Used as a
4638 comparison function for qsort in load_overlay_strings. Overlay
4639 strings for the same position are sorted so that
4641 1. All after-strings come in front of before-strings, except
4642 when they come from the same overlay.
4644 2. Within after-strings, strings are sorted so that overlay strings
4645 from overlays with higher priorities come first.
4647 2. Within before-strings, strings are sorted so that overlay
4648 strings from overlays with higher priorities come last.
4650 Value is analogous to strcmp. */
4653 static int
4654 compare_overlay_entries (e1, e2)
4655 void *e1, *e2;
4657 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4658 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4659 int result;
4661 if (entry1->after_string_p != entry2->after_string_p)
4663 /* Let after-strings appear in front of before-strings if
4664 they come from different overlays. */
4665 if (EQ (entry1->overlay, entry2->overlay))
4666 result = entry1->after_string_p ? 1 : -1;
4667 else
4668 result = entry1->after_string_p ? -1 : 1;
4670 else if (entry1->after_string_p)
4671 /* After-strings sorted in order of decreasing priority. */
4672 result = entry2->priority - entry1->priority;
4673 else
4674 /* Before-strings sorted in order of increasing priority. */
4675 result = entry1->priority - entry2->priority;
4677 return result;
4681 /* Load the vector IT->overlay_strings with overlay strings from IT's
4682 current buffer position, or from CHARPOS if that is > 0. Set
4683 IT->n_overlays to the total number of overlay strings found.
4685 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4686 a time. On entry into load_overlay_strings,
4687 IT->current.overlay_string_index gives the number of overlay
4688 strings that have already been loaded by previous calls to this
4689 function.
4691 IT->add_overlay_start contains an additional overlay start
4692 position to consider for taking overlay strings from, if non-zero.
4693 This position comes into play when the overlay has an `invisible'
4694 property, and both before and after-strings. When we've skipped to
4695 the end of the overlay, because of its `invisible' property, we
4696 nevertheless want its before-string to appear.
4697 IT->add_overlay_start will contain the overlay start position
4698 in this case.
4700 Overlay strings are sorted so that after-string strings come in
4701 front of before-string strings. Within before and after-strings,
4702 strings are sorted by overlay priority. See also function
4703 compare_overlay_entries. */
4705 static void
4706 load_overlay_strings (it, charpos)
4707 struct it *it;
4708 int charpos;
4710 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4711 Lisp_Object overlay, window, str, invisible;
4712 struct Lisp_Overlay *ov;
4713 int start, end;
4714 int size = 20;
4715 int n = 0, i, j, invis_p;
4716 struct overlay_entry *entries
4717 = (struct overlay_entry *) alloca (size * sizeof *entries);
4719 if (charpos <= 0)
4720 charpos = IT_CHARPOS (*it);
4722 /* Append the overlay string STRING of overlay OVERLAY to vector
4723 `entries' which has size `size' and currently contains `n'
4724 elements. AFTER_P non-zero means STRING is an after-string of
4725 OVERLAY. */
4726 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4727 do \
4729 Lisp_Object priority; \
4731 if (n == size) \
4733 int new_size = 2 * size; \
4734 struct overlay_entry *old = entries; \
4735 entries = \
4736 (struct overlay_entry *) alloca (new_size \
4737 * sizeof *entries); \
4738 bcopy (old, entries, size * sizeof *entries); \
4739 size = new_size; \
4742 entries[n].string = (STRING); \
4743 entries[n].overlay = (OVERLAY); \
4744 priority = Foverlay_get ((OVERLAY), Qpriority); \
4745 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4746 entries[n].after_string_p = (AFTER_P); \
4747 ++n; \
4749 while (0)
4751 /* Process overlay before the overlay center. */
4752 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4754 XSETMISC (overlay, ov);
4755 xassert (OVERLAYP (overlay));
4756 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4757 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4759 if (end < charpos)
4760 break;
4762 /* Skip this overlay if it doesn't start or end at IT's current
4763 position. */
4764 if (end != charpos && start != charpos)
4765 continue;
4767 /* Skip this overlay if it doesn't apply to IT->w. */
4768 window = Foverlay_get (overlay, Qwindow);
4769 if (WINDOWP (window) && XWINDOW (window) != it->w)
4770 continue;
4772 /* If the text ``under'' the overlay is invisible, both before-
4773 and after-strings from this overlay are visible; start and
4774 end position are indistinguishable. */
4775 invisible = Foverlay_get (overlay, Qinvisible);
4776 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4778 /* If overlay has a non-empty before-string, record it. */
4779 if ((start == charpos || (end == charpos && invis_p))
4780 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4781 && SCHARS (str))
4782 RECORD_OVERLAY_STRING (overlay, str, 0);
4784 /* If overlay has a non-empty after-string, record it. */
4785 if ((end == charpos || (start == charpos && invis_p))
4786 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4787 && SCHARS (str))
4788 RECORD_OVERLAY_STRING (overlay, str, 1);
4791 /* Process overlays after the overlay center. */
4792 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4794 XSETMISC (overlay, ov);
4795 xassert (OVERLAYP (overlay));
4796 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4797 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4799 if (start > charpos)
4800 break;
4802 /* Skip this overlay if it doesn't start or end at IT's current
4803 position. */
4804 if (end != charpos && start != charpos)
4805 continue;
4807 /* Skip this overlay if it doesn't apply to IT->w. */
4808 window = Foverlay_get (overlay, Qwindow);
4809 if (WINDOWP (window) && XWINDOW (window) != it->w)
4810 continue;
4812 /* If the text ``under'' the overlay is invisible, it has a zero
4813 dimension, and both before- and after-strings apply. */
4814 invisible = Foverlay_get (overlay, Qinvisible);
4815 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4817 /* If overlay has a non-empty before-string, record it. */
4818 if ((start == charpos || (end == charpos && invis_p))
4819 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4820 && SCHARS (str))
4821 RECORD_OVERLAY_STRING (overlay, str, 0);
4823 /* If overlay has a non-empty after-string, record it. */
4824 if ((end == charpos || (start == charpos && invis_p))
4825 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4826 && SCHARS (str))
4827 RECORD_OVERLAY_STRING (overlay, str, 1);
4830 #undef RECORD_OVERLAY_STRING
4832 /* Sort entries. */
4833 if (n > 1)
4834 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4836 /* Record the total number of strings to process. */
4837 it->n_overlay_strings = n;
4839 /* IT->current.overlay_string_index is the number of overlay strings
4840 that have already been consumed by IT. Copy some of the
4841 remaining overlay strings to IT->overlay_strings. */
4842 i = 0;
4843 j = it->current.overlay_string_index;
4844 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4845 it->overlay_strings[i++] = entries[j++].string;
4847 CHECK_IT (it);
4851 /* Get the first chunk of overlay strings at IT's current buffer
4852 position, or at CHARPOS if that is > 0. Value is non-zero if at
4853 least one overlay string was found. */
4855 static int
4856 get_overlay_strings_1 (it, charpos, compute_stop_p)
4857 struct it *it;
4858 int charpos;
4860 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4861 process. This fills IT->overlay_strings with strings, and sets
4862 IT->n_overlay_strings to the total number of strings to process.
4863 IT->pos.overlay_string_index has to be set temporarily to zero
4864 because load_overlay_strings needs this; it must be set to -1
4865 when no overlay strings are found because a zero value would
4866 indicate a position in the first overlay string. */
4867 it->current.overlay_string_index = 0;
4868 load_overlay_strings (it, charpos);
4870 /* If we found overlay strings, set up IT to deliver display
4871 elements from the first one. Otherwise set up IT to deliver
4872 from current_buffer. */
4873 if (it->n_overlay_strings)
4875 /* Make sure we know settings in current_buffer, so that we can
4876 restore meaningful values when we're done with the overlay
4877 strings. */
4878 if (compute_stop_p)
4879 compute_stop_pos (it);
4880 xassert (it->face_id >= 0);
4882 /* Save IT's settings. They are restored after all overlay
4883 strings have been processed. */
4884 xassert (!compute_stop_p || it->sp == 0);
4885 push_it (it);
4887 /* Set up IT to deliver display elements from the first overlay
4888 string. */
4889 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4890 it->string = it->overlay_strings[0];
4891 it->stop_charpos = 0;
4892 xassert (STRINGP (it->string));
4893 it->end_charpos = SCHARS (it->string);
4894 it->multibyte_p = STRING_MULTIBYTE (it->string);
4895 it->method = GET_FROM_STRING;
4896 return 1;
4899 it->current.overlay_string_index = -1;
4900 return 0;
4903 static int
4904 get_overlay_strings (it, charpos)
4905 struct it *it;
4906 int charpos;
4908 it->string = Qnil;
4909 it->method = GET_FROM_BUFFER;
4911 (void) get_overlay_strings_1 (it, charpos, 1);
4913 CHECK_IT (it);
4915 /* Value is non-zero if we found at least one overlay string. */
4916 return STRINGP (it->string);
4921 /***********************************************************************
4922 Saving and restoring state
4923 ***********************************************************************/
4925 /* Save current settings of IT on IT->stack. Called, for example,
4926 before setting up IT for an overlay string, to be able to restore
4927 IT's settings to what they were after the overlay string has been
4928 processed. */
4930 static void
4931 push_it (it)
4932 struct it *it;
4934 struct iterator_stack_entry *p;
4936 xassert (it->sp < IT_STACK_SIZE);
4937 p = it->stack + it->sp;
4939 p->stop_charpos = it->stop_charpos;
4940 xassert (it->face_id >= 0);
4941 p->face_id = it->face_id;
4942 p->string = it->string;
4943 p->method = it->method;
4944 switch (p->method)
4946 case GET_FROM_IMAGE:
4947 p->u.image.object = it->object;
4948 p->u.image.image_id = it->image_id;
4949 p->u.image.slice = it->slice;
4950 break;
4951 case GET_FROM_COMPOSITION:
4952 p->u.comp.object = it->object;
4953 p->u.comp.c = it->c;
4954 p->u.comp.len = it->len;
4955 p->u.comp.cmp_id = it->cmp_id;
4956 p->u.comp.cmp_len = it->cmp_len;
4957 break;
4958 case GET_FROM_STRETCH:
4959 p->u.stretch.object = it->object;
4960 break;
4962 p->position = it->position;
4963 p->current = it->current;
4964 p->end_charpos = it->end_charpos;
4965 p->string_nchars = it->string_nchars;
4966 p->area = it->area;
4967 p->multibyte_p = it->multibyte_p;
4968 p->space_width = it->space_width;
4969 p->font_height = it->font_height;
4970 p->voffset = it->voffset;
4971 p->string_from_display_prop_p = it->string_from_display_prop_p;
4972 p->display_ellipsis_p = 0;
4973 ++it->sp;
4977 /* Restore IT's settings from IT->stack. Called, for example, when no
4978 more overlay strings must be processed, and we return to delivering
4979 display elements from a buffer, or when the end of a string from a
4980 `display' property is reached and we return to delivering display
4981 elements from an overlay string, or from a buffer. */
4983 static void
4984 pop_it (it)
4985 struct it *it;
4987 struct iterator_stack_entry *p;
4989 xassert (it->sp > 0);
4990 --it->sp;
4991 p = it->stack + it->sp;
4992 it->stop_charpos = p->stop_charpos;
4993 it->face_id = p->face_id;
4994 it->current = p->current;
4995 it->position = p->position;
4996 it->string = p->string;
4997 if (NILP (it->string))
4998 SET_TEXT_POS (it->current.string_pos, -1, -1);
4999 it->method = p->method;
5000 switch (it->method)
5002 case GET_FROM_IMAGE:
5003 it->image_id = p->u.image.image_id;
5004 it->object = p->u.image.object;
5005 it->slice = p->u.image.slice;
5006 break;
5007 case GET_FROM_COMPOSITION:
5008 it->object = p->u.comp.object;
5009 it->c = p->u.comp.c;
5010 it->len = p->u.comp.len;
5011 it->cmp_id = p->u.comp.cmp_id;
5012 it->cmp_len = p->u.comp.cmp_len;
5013 break;
5014 case GET_FROM_STRETCH:
5015 it->object = p->u.comp.object;
5016 break;
5017 case GET_FROM_BUFFER:
5018 it->object = it->w->buffer;
5019 break;
5020 case GET_FROM_STRING:
5021 it->object = it->string;
5022 break;
5024 it->end_charpos = p->end_charpos;
5025 it->string_nchars = p->string_nchars;
5026 it->area = p->area;
5027 it->multibyte_p = p->multibyte_p;
5028 it->space_width = p->space_width;
5029 it->font_height = p->font_height;
5030 it->voffset = p->voffset;
5031 it->string_from_display_prop_p = p->string_from_display_prop_p;
5036 /***********************************************************************
5037 Moving over lines
5038 ***********************************************************************/
5040 /* Set IT's current position to the previous line start. */
5042 static void
5043 back_to_previous_line_start (it)
5044 struct it *it;
5046 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5047 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5051 /* Move IT to the next line start.
5053 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5054 we skipped over part of the text (as opposed to moving the iterator
5055 continuously over the text). Otherwise, don't change the value
5056 of *SKIPPED_P.
5058 Newlines may come from buffer text, overlay strings, or strings
5059 displayed via the `display' property. That's the reason we can't
5060 simply use find_next_newline_no_quit.
5062 Note that this function may not skip over invisible text that is so
5063 because of text properties and immediately follows a newline. If
5064 it would, function reseat_at_next_visible_line_start, when called
5065 from set_iterator_to_next, would effectively make invisible
5066 characters following a newline part of the wrong glyph row, which
5067 leads to wrong cursor motion. */
5069 static int
5070 forward_to_next_line_start (it, skipped_p)
5071 struct it *it;
5072 int *skipped_p;
5074 int old_selective, newline_found_p, n;
5075 const int MAX_NEWLINE_DISTANCE = 500;
5077 /* If already on a newline, just consume it to avoid unintended
5078 skipping over invisible text below. */
5079 if (it->what == IT_CHARACTER
5080 && it->c == '\n'
5081 && CHARPOS (it->position) == IT_CHARPOS (*it))
5083 set_iterator_to_next (it, 0);
5084 it->c = 0;
5085 return 1;
5088 /* Don't handle selective display in the following. It's (a)
5089 unnecessary because it's done by the caller, and (b) leads to an
5090 infinite recursion because next_element_from_ellipsis indirectly
5091 calls this function. */
5092 old_selective = it->selective;
5093 it->selective = 0;
5095 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5096 from buffer text. */
5097 for (n = newline_found_p = 0;
5098 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5099 n += STRINGP (it->string) ? 0 : 1)
5101 if (!get_next_display_element (it))
5102 return 0;
5103 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5104 set_iterator_to_next (it, 0);
5107 /* If we didn't find a newline near enough, see if we can use a
5108 short-cut. */
5109 if (!newline_found_p)
5111 int start = IT_CHARPOS (*it);
5112 int limit = find_next_newline_no_quit (start, 1);
5113 Lisp_Object pos;
5115 xassert (!STRINGP (it->string));
5117 /* If there isn't any `display' property in sight, and no
5118 overlays, we can just use the position of the newline in
5119 buffer text. */
5120 if (it->stop_charpos >= limit
5121 || ((pos = Fnext_single_property_change (make_number (start),
5122 Qdisplay,
5123 Qnil, make_number (limit)),
5124 NILP (pos))
5125 && next_overlay_change (start) == ZV))
5127 IT_CHARPOS (*it) = limit;
5128 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5129 *skipped_p = newline_found_p = 1;
5131 else
5133 while (get_next_display_element (it)
5134 && !newline_found_p)
5136 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5137 set_iterator_to_next (it, 0);
5142 it->selective = old_selective;
5143 return newline_found_p;
5147 /* Set IT's current position to the previous visible line start. Skip
5148 invisible text that is so either due to text properties or due to
5149 selective display. Caution: this does not change IT->current_x and
5150 IT->hpos. */
5152 static void
5153 back_to_previous_visible_line_start (it)
5154 struct it *it;
5156 while (IT_CHARPOS (*it) > BEGV)
5158 back_to_previous_line_start (it);
5160 if (IT_CHARPOS (*it) <= BEGV)
5161 break;
5163 /* If selective > 0, then lines indented more than that values
5164 are invisible. */
5165 if (it->selective > 0
5166 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5167 (double) it->selective)) /* iftc */
5168 continue;
5170 /* Check the newline before point for invisibility. */
5172 Lisp_Object prop;
5173 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5174 Qinvisible, it->window);
5175 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5176 continue;
5179 if (IT_CHARPOS (*it) <= BEGV)
5180 break;
5183 struct it it2;
5184 int pos;
5185 int beg, end;
5186 Lisp_Object val, overlay;
5188 /* If newline is part of a composition, continue from start of composition */
5189 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5190 && beg < IT_CHARPOS (*it))
5191 goto replaced;
5193 /* If newline is replaced by a display property, find start of overlay
5194 or interval and continue search from that point. */
5195 it2 = *it;
5196 pos = --IT_CHARPOS (it2);
5197 --IT_BYTEPOS (it2);
5198 it2.sp = 0;
5199 if (handle_display_prop (&it2) == HANDLED_RETURN
5200 && !NILP (val = get_char_property_and_overlay
5201 (make_number (pos), Qdisplay, Qnil, &overlay))
5202 && (OVERLAYP (overlay)
5203 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5204 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5205 goto replaced;
5207 /* Newline is not replaced by anything -- so we are done. */
5208 break;
5210 replaced:
5211 if (beg < BEGV)
5212 beg = BEGV;
5213 IT_CHARPOS (*it) = beg;
5214 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5218 it->continuation_lines_width = 0;
5220 xassert (IT_CHARPOS (*it) >= BEGV);
5221 xassert (IT_CHARPOS (*it) == BEGV
5222 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5223 CHECK_IT (it);
5227 /* Reseat iterator IT at the previous visible line start. Skip
5228 invisible text that is so either due to text properties or due to
5229 selective display. At the end, update IT's overlay information,
5230 face information etc. */
5232 void
5233 reseat_at_previous_visible_line_start (it)
5234 struct it *it;
5236 back_to_previous_visible_line_start (it);
5237 reseat (it, it->current.pos, 1);
5238 CHECK_IT (it);
5242 /* Reseat iterator IT on the next visible line start in the current
5243 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5244 preceding the line start. Skip over invisible text that is so
5245 because of selective display. Compute faces, overlays etc at the
5246 new position. Note that this function does not skip over text that
5247 is invisible because of text properties. */
5249 static void
5250 reseat_at_next_visible_line_start (it, on_newline_p)
5251 struct it *it;
5252 int on_newline_p;
5254 int newline_found_p, skipped_p = 0;
5256 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5258 /* Skip over lines that are invisible because they are indented
5259 more than the value of IT->selective. */
5260 if (it->selective > 0)
5261 while (IT_CHARPOS (*it) < ZV
5262 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5263 (double) it->selective)) /* iftc */
5265 xassert (IT_BYTEPOS (*it) == BEGV
5266 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5267 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5270 /* Position on the newline if that's what's requested. */
5271 if (on_newline_p && newline_found_p)
5273 if (STRINGP (it->string))
5275 if (IT_STRING_CHARPOS (*it) > 0)
5277 --IT_STRING_CHARPOS (*it);
5278 --IT_STRING_BYTEPOS (*it);
5281 else if (IT_CHARPOS (*it) > BEGV)
5283 --IT_CHARPOS (*it);
5284 --IT_BYTEPOS (*it);
5285 reseat (it, it->current.pos, 0);
5288 else if (skipped_p)
5289 reseat (it, it->current.pos, 0);
5291 CHECK_IT (it);
5296 /***********************************************************************
5297 Changing an iterator's position
5298 ***********************************************************************/
5300 /* Change IT's current position to POS in current_buffer. If FORCE_P
5301 is non-zero, always check for text properties at the new position.
5302 Otherwise, text properties are only looked up if POS >=
5303 IT->check_charpos of a property. */
5305 static void
5306 reseat (it, pos, force_p)
5307 struct it *it;
5308 struct text_pos pos;
5309 int force_p;
5311 int original_pos = IT_CHARPOS (*it);
5313 reseat_1 (it, pos, 0);
5315 /* Determine where to check text properties. Avoid doing it
5316 where possible because text property lookup is very expensive. */
5317 if (force_p
5318 || CHARPOS (pos) > it->stop_charpos
5319 || CHARPOS (pos) < original_pos)
5320 handle_stop (it);
5322 CHECK_IT (it);
5326 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5327 IT->stop_pos to POS, also. */
5329 static void
5330 reseat_1 (it, pos, set_stop_p)
5331 struct it *it;
5332 struct text_pos pos;
5333 int set_stop_p;
5335 /* Don't call this function when scanning a C string. */
5336 xassert (it->s == NULL);
5338 /* POS must be a reasonable value. */
5339 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5341 it->current.pos = it->position = pos;
5342 it->end_charpos = ZV;
5343 it->dpvec = NULL;
5344 it->current.dpvec_index = -1;
5345 it->current.overlay_string_index = -1;
5346 IT_STRING_CHARPOS (*it) = -1;
5347 IT_STRING_BYTEPOS (*it) = -1;
5348 it->string = Qnil;
5349 it->method = GET_FROM_BUFFER;
5350 it->object = it->w->buffer;
5351 it->area = TEXT_AREA;
5352 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5353 it->sp = 0;
5354 it->string_from_display_prop_p = 0;
5355 it->face_before_selective_p = 0;
5357 if (set_stop_p)
5358 it->stop_charpos = CHARPOS (pos);
5362 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5363 If S is non-null, it is a C string to iterate over. Otherwise,
5364 STRING gives a Lisp string to iterate over.
5366 If PRECISION > 0, don't return more then PRECISION number of
5367 characters from the string.
5369 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5370 characters have been returned. FIELD_WIDTH < 0 means an infinite
5371 field width.
5373 MULTIBYTE = 0 means disable processing of multibyte characters,
5374 MULTIBYTE > 0 means enable it,
5375 MULTIBYTE < 0 means use IT->multibyte_p.
5377 IT must be initialized via a prior call to init_iterator before
5378 calling this function. */
5380 static void
5381 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5382 struct it *it;
5383 unsigned char *s;
5384 Lisp_Object string;
5385 int charpos;
5386 int precision, field_width, multibyte;
5388 /* No region in strings. */
5389 it->region_beg_charpos = it->region_end_charpos = -1;
5391 /* No text property checks performed by default, but see below. */
5392 it->stop_charpos = -1;
5394 /* Set iterator position and end position. */
5395 bzero (&it->current, sizeof it->current);
5396 it->current.overlay_string_index = -1;
5397 it->current.dpvec_index = -1;
5398 xassert (charpos >= 0);
5400 /* If STRING is specified, use its multibyteness, otherwise use the
5401 setting of MULTIBYTE, if specified. */
5402 if (multibyte >= 0)
5403 it->multibyte_p = multibyte > 0;
5405 if (s == NULL)
5407 xassert (STRINGP (string));
5408 it->string = string;
5409 it->s = NULL;
5410 it->end_charpos = it->string_nchars = SCHARS (string);
5411 it->method = GET_FROM_STRING;
5412 it->current.string_pos = string_pos (charpos, string);
5414 else
5416 it->s = s;
5417 it->string = Qnil;
5419 /* Note that we use IT->current.pos, not it->current.string_pos,
5420 for displaying C strings. */
5421 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5422 if (it->multibyte_p)
5424 it->current.pos = c_string_pos (charpos, s, 1);
5425 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5427 else
5429 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5430 it->end_charpos = it->string_nchars = strlen (s);
5433 it->method = GET_FROM_C_STRING;
5436 /* PRECISION > 0 means don't return more than PRECISION characters
5437 from the string. */
5438 if (precision > 0 && it->end_charpos - charpos > precision)
5439 it->end_charpos = it->string_nchars = charpos + precision;
5441 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5442 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5443 FIELD_WIDTH < 0 means infinite field width. This is useful for
5444 padding with `-' at the end of a mode line. */
5445 if (field_width < 0)
5446 field_width = INFINITY;
5447 if (field_width > it->end_charpos - charpos)
5448 it->end_charpos = charpos + field_width;
5450 /* Use the standard display table for displaying strings. */
5451 if (DISP_TABLE_P (Vstandard_display_table))
5452 it->dp = XCHAR_TABLE (Vstandard_display_table);
5454 it->stop_charpos = charpos;
5455 CHECK_IT (it);
5460 /***********************************************************************
5461 Iteration
5462 ***********************************************************************/
5464 /* Map enum it_method value to corresponding next_element_from_* function. */
5466 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5468 next_element_from_buffer,
5469 next_element_from_display_vector,
5470 next_element_from_composition,
5471 next_element_from_string,
5472 next_element_from_c_string,
5473 next_element_from_image,
5474 next_element_from_stretch
5478 /* Load IT's display element fields with information about the next
5479 display element from the current position of IT. Value is zero if
5480 end of buffer (or C string) is reached. */
5482 static struct frame *last_escape_glyph_frame = NULL;
5483 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5484 static int last_escape_glyph_merged_face_id = 0;
5487 get_next_display_element (it)
5488 struct it *it;
5490 /* Non-zero means that we found a display element. Zero means that
5491 we hit the end of what we iterate over. Performance note: the
5492 function pointer `method' used here turns out to be faster than
5493 using a sequence of if-statements. */
5494 int success_p;
5496 get_next:
5497 success_p = (*get_next_element[it->method]) (it);
5499 if (it->what == IT_CHARACTER)
5501 /* Map via display table or translate control characters.
5502 IT->c, IT->len etc. have been set to the next character by
5503 the function call above. If we have a display table, and it
5504 contains an entry for IT->c, translate it. Don't do this if
5505 IT->c itself comes from a display table, otherwise we could
5506 end up in an infinite recursion. (An alternative could be to
5507 count the recursion depth of this function and signal an
5508 error when a certain maximum depth is reached.) Is it worth
5509 it? */
5510 if (success_p && it->dpvec == NULL)
5512 Lisp_Object dv;
5514 if (it->dp
5515 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5516 VECTORP (dv)))
5518 struct Lisp_Vector *v = XVECTOR (dv);
5520 /* Return the first character from the display table
5521 entry, if not empty. If empty, don't display the
5522 current character. */
5523 if (v->size)
5525 it->dpvec_char_len = it->len;
5526 it->dpvec = v->contents;
5527 it->dpend = v->contents + v->size;
5528 it->current.dpvec_index = 0;
5529 it->dpvec_face_id = -1;
5530 it->saved_face_id = it->face_id;
5531 it->method = GET_FROM_DISPLAY_VECTOR;
5532 it->ellipsis_p = 0;
5534 else
5536 set_iterator_to_next (it, 0);
5538 goto get_next;
5541 /* Translate control characters into `\003' or `^C' form.
5542 Control characters coming from a display table entry are
5543 currently not translated because we use IT->dpvec to hold
5544 the translation. This could easily be changed but I
5545 don't believe that it is worth doing.
5547 If it->multibyte_p is nonzero, eight-bit characters and
5548 non-printable multibyte characters are also translated to
5549 octal form.
5551 If it->multibyte_p is zero, eight-bit characters that
5552 don't have corresponding multibyte char code are also
5553 translated to octal form. */
5554 else if ((it->c < ' '
5555 && (it->area != TEXT_AREA
5556 /* In mode line, treat \n like other crl chars. */
5557 || (it->c != '\t'
5558 && it->glyph_row && it->glyph_row->mode_line_p)
5559 || (it->c != '\n' && it->c != '\t')))
5560 || (it->multibyte_p
5561 ? ((it->c >= 127
5562 && it->len == 1)
5563 || !CHAR_PRINTABLE_P (it->c)
5564 || (!NILP (Vnobreak_char_display)
5565 && (it->c == 0x8a0 || it->c == 0x8ad
5566 || it->c == 0x920 || it->c == 0x92d
5567 || it->c == 0xe20 || it->c == 0xe2d
5568 || it->c == 0xf20 || it->c == 0xf2d)))
5569 : (it->c >= 127
5570 && (!unibyte_display_via_language_environment
5571 || it->c == unibyte_char_to_multibyte (it->c)))))
5573 /* IT->c is a control character which must be displayed
5574 either as '\003' or as `^C' where the '\\' and '^'
5575 can be defined in the display table. Fill
5576 IT->ctl_chars with glyphs for what we have to
5577 display. Then, set IT->dpvec to these glyphs. */
5578 GLYPH g;
5579 int ctl_len;
5580 int face_id, lface_id = 0 ;
5581 GLYPH escape_glyph;
5583 /* Handle control characters with ^. */
5585 if (it->c < 128 && it->ctl_arrow_p)
5587 g = '^'; /* default glyph for Control */
5588 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5589 if (it->dp
5590 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5591 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5593 g = XINT (DISP_CTRL_GLYPH (it->dp));
5594 lface_id = FAST_GLYPH_FACE (g);
5596 if (lface_id)
5598 g = FAST_GLYPH_CHAR (g);
5599 face_id = merge_faces (it->f, Qt, lface_id,
5600 it->face_id);
5602 else if (it->f == last_escape_glyph_frame
5603 && it->face_id == last_escape_glyph_face_id)
5605 face_id = last_escape_glyph_merged_face_id;
5607 else
5609 /* Merge the escape-glyph face into the current face. */
5610 face_id = merge_faces (it->f, Qescape_glyph, 0,
5611 it->face_id);
5612 last_escape_glyph_frame = it->f;
5613 last_escape_glyph_face_id = it->face_id;
5614 last_escape_glyph_merged_face_id = face_id;
5617 XSETINT (it->ctl_chars[0], g);
5618 g = it->c ^ 0100;
5619 XSETINT (it->ctl_chars[1], g);
5620 ctl_len = 2;
5621 goto display_control;
5624 /* Handle non-break space in the mode where it only gets
5625 highlighting. */
5627 if (EQ (Vnobreak_char_display, Qt)
5628 && (it->c == 0x8a0 || it->c == 0x920
5629 || it->c == 0xe20 || it->c == 0xf20))
5631 /* Merge the no-break-space face into the current face. */
5632 face_id = merge_faces (it->f, Qnobreak_space, 0,
5633 it->face_id);
5635 g = it->c = ' ';
5636 XSETINT (it->ctl_chars[0], g);
5637 ctl_len = 1;
5638 goto display_control;
5641 /* Handle sequences that start with the "escape glyph". */
5643 /* the default escape glyph is \. */
5644 escape_glyph = '\\';
5646 if (it->dp
5647 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5648 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5650 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5651 lface_id = FAST_GLYPH_FACE (escape_glyph);
5653 if (lface_id)
5655 /* The display table specified a face.
5656 Merge it into face_id and also into escape_glyph. */
5657 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5658 face_id = merge_faces (it->f, Qt, lface_id,
5659 it->face_id);
5661 else if (it->f == last_escape_glyph_frame
5662 && it->face_id == last_escape_glyph_face_id)
5664 face_id = last_escape_glyph_merged_face_id;
5666 else
5668 /* Merge the escape-glyph face into the current face. */
5669 face_id = merge_faces (it->f, Qescape_glyph, 0,
5670 it->face_id);
5671 last_escape_glyph_frame = it->f;
5672 last_escape_glyph_face_id = it->face_id;
5673 last_escape_glyph_merged_face_id = face_id;
5676 /* Handle soft hyphens in the mode where they only get
5677 highlighting. */
5679 if (EQ (Vnobreak_char_display, Qt)
5680 && (it->c == 0x8ad || it->c == 0x92d
5681 || it->c == 0xe2d || it->c == 0xf2d))
5683 g = it->c = '-';
5684 XSETINT (it->ctl_chars[0], g);
5685 ctl_len = 1;
5686 goto display_control;
5689 /* Handle non-break space and soft hyphen
5690 with the escape glyph. */
5692 if (it->c == 0x8a0 || it->c == 0x8ad
5693 || it->c == 0x920 || it->c == 0x92d
5694 || it->c == 0xe20 || it->c == 0xe2d
5695 || it->c == 0xf20 || it->c == 0xf2d)
5697 XSETINT (it->ctl_chars[0], escape_glyph);
5698 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5699 XSETINT (it->ctl_chars[1], g);
5700 ctl_len = 2;
5701 goto display_control;
5705 unsigned char str[MAX_MULTIBYTE_LENGTH];
5706 int len;
5707 int i;
5709 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5710 if (SINGLE_BYTE_CHAR_P (it->c))
5711 str[0] = it->c, len = 1;
5712 else
5714 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5715 if (len < 0)
5717 /* It's an invalid character, which shouldn't
5718 happen actually, but due to bugs it may
5719 happen. Let's print the char as is, there's
5720 not much meaningful we can do with it. */
5721 str[0] = it->c;
5722 str[1] = it->c >> 8;
5723 str[2] = it->c >> 16;
5724 str[3] = it->c >> 24;
5725 len = 4;
5729 for (i = 0; i < len; i++)
5731 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5732 /* Insert three more glyphs into IT->ctl_chars for
5733 the octal display of the character. */
5734 g = ((str[i] >> 6) & 7) + '0';
5735 XSETINT (it->ctl_chars[i * 4 + 1], g);
5736 g = ((str[i] >> 3) & 7) + '0';
5737 XSETINT (it->ctl_chars[i * 4 + 2], g);
5738 g = (str[i] & 7) + '0';
5739 XSETINT (it->ctl_chars[i * 4 + 3], g);
5741 ctl_len = len * 4;
5744 display_control:
5745 /* Set up IT->dpvec and return first character from it. */
5746 it->dpvec_char_len = it->len;
5747 it->dpvec = it->ctl_chars;
5748 it->dpend = it->dpvec + ctl_len;
5749 it->current.dpvec_index = 0;
5750 it->dpvec_face_id = face_id;
5751 it->saved_face_id = it->face_id;
5752 it->method = GET_FROM_DISPLAY_VECTOR;
5753 it->ellipsis_p = 0;
5754 goto get_next;
5758 /* Adjust face id for a multibyte character. There are no
5759 multibyte character in unibyte text. */
5760 if (it->multibyte_p
5761 && success_p
5762 && FRAME_WINDOW_P (it->f))
5764 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5765 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5769 /* Is this character the last one of a run of characters with
5770 box? If yes, set IT->end_of_box_run_p to 1. */
5771 if (it->face_box_p
5772 && it->s == NULL)
5774 int face_id;
5775 struct face *face;
5777 it->end_of_box_run_p
5778 = ((face_id = face_after_it_pos (it),
5779 face_id != it->face_id)
5780 && (face = FACE_FROM_ID (it->f, face_id),
5781 face->box == FACE_NO_BOX));
5784 /* Value is 0 if end of buffer or string reached. */
5785 return success_p;
5789 /* Move IT to the next display element.
5791 RESEAT_P non-zero means if called on a newline in buffer text,
5792 skip to the next visible line start.
5794 Functions get_next_display_element and set_iterator_to_next are
5795 separate because I find this arrangement easier to handle than a
5796 get_next_display_element function that also increments IT's
5797 position. The way it is we can first look at an iterator's current
5798 display element, decide whether it fits on a line, and if it does,
5799 increment the iterator position. The other way around we probably
5800 would either need a flag indicating whether the iterator has to be
5801 incremented the next time, or we would have to implement a
5802 decrement position function which would not be easy to write. */
5804 void
5805 set_iterator_to_next (it, reseat_p)
5806 struct it *it;
5807 int reseat_p;
5809 /* Reset flags indicating start and end of a sequence of characters
5810 with box. Reset them at the start of this function because
5811 moving the iterator to a new position might set them. */
5812 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5814 switch (it->method)
5816 case GET_FROM_BUFFER:
5817 /* The current display element of IT is a character from
5818 current_buffer. Advance in the buffer, and maybe skip over
5819 invisible lines that are so because of selective display. */
5820 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5821 reseat_at_next_visible_line_start (it, 0);
5822 else
5824 xassert (it->len != 0);
5825 IT_BYTEPOS (*it) += it->len;
5826 IT_CHARPOS (*it) += 1;
5827 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5829 break;
5831 case GET_FROM_COMPOSITION:
5832 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5833 xassert (it->sp > 0);
5834 pop_it (it);
5835 if (it->method == GET_FROM_STRING)
5837 IT_STRING_BYTEPOS (*it) += it->len;
5838 IT_STRING_CHARPOS (*it) += it->cmp_len;
5839 goto consider_string_end;
5841 else if (it->method == GET_FROM_BUFFER)
5843 IT_BYTEPOS (*it) += it->len;
5844 IT_CHARPOS (*it) += it->cmp_len;
5846 break;
5848 case GET_FROM_C_STRING:
5849 /* Current display element of IT is from a C string. */
5850 IT_BYTEPOS (*it) += it->len;
5851 IT_CHARPOS (*it) += 1;
5852 break;
5854 case GET_FROM_DISPLAY_VECTOR:
5855 /* Current display element of IT is from a display table entry.
5856 Advance in the display table definition. Reset it to null if
5857 end reached, and continue with characters from buffers/
5858 strings. */
5859 ++it->current.dpvec_index;
5861 /* Restore face of the iterator to what they were before the
5862 display vector entry (these entries may contain faces). */
5863 it->face_id = it->saved_face_id;
5865 if (it->dpvec + it->current.dpvec_index == it->dpend)
5867 int recheck_faces = it->ellipsis_p;
5869 if (it->s)
5870 it->method = GET_FROM_C_STRING;
5871 else if (STRINGP (it->string))
5872 it->method = GET_FROM_STRING;
5873 else
5875 it->method = GET_FROM_BUFFER;
5876 it->object = it->w->buffer;
5879 it->dpvec = NULL;
5880 it->current.dpvec_index = -1;
5882 /* Skip over characters which were displayed via IT->dpvec. */
5883 if (it->dpvec_char_len < 0)
5884 reseat_at_next_visible_line_start (it, 1);
5885 else if (it->dpvec_char_len > 0)
5887 if (it->method == GET_FROM_STRING
5888 && it->n_overlay_strings > 0)
5889 it->ignore_overlay_strings_at_pos_p = 1;
5890 it->len = it->dpvec_char_len;
5891 set_iterator_to_next (it, reseat_p);
5894 /* Maybe recheck faces after display vector */
5895 if (recheck_faces)
5896 it->stop_charpos = IT_CHARPOS (*it);
5898 break;
5900 case GET_FROM_STRING:
5901 /* Current display element is a character from a Lisp string. */
5902 xassert (it->s == NULL && STRINGP (it->string));
5903 IT_STRING_BYTEPOS (*it) += it->len;
5904 IT_STRING_CHARPOS (*it) += 1;
5906 consider_string_end:
5908 if (it->current.overlay_string_index >= 0)
5910 /* IT->string is an overlay string. Advance to the
5911 next, if there is one. */
5912 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5913 next_overlay_string (it);
5915 else
5917 /* IT->string is not an overlay string. If we reached
5918 its end, and there is something on IT->stack, proceed
5919 with what is on the stack. This can be either another
5920 string, this time an overlay string, or a buffer. */
5921 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5922 && it->sp > 0)
5924 pop_it (it);
5925 if (it->method == GET_FROM_STRING)
5926 goto consider_string_end;
5929 break;
5931 case GET_FROM_IMAGE:
5932 case GET_FROM_STRETCH:
5933 /* The position etc with which we have to proceed are on
5934 the stack. The position may be at the end of a string,
5935 if the `display' property takes up the whole string. */
5936 xassert (it->sp > 0);
5937 pop_it (it);
5938 if (it->method == GET_FROM_STRING)
5939 goto consider_string_end;
5940 break;
5942 default:
5943 /* There are no other methods defined, so this should be a bug. */
5944 abort ();
5947 xassert (it->method != GET_FROM_STRING
5948 || (STRINGP (it->string)
5949 && IT_STRING_CHARPOS (*it) >= 0));
5952 /* Load IT's display element fields with information about the next
5953 display element which comes from a display table entry or from the
5954 result of translating a control character to one of the forms `^C'
5955 or `\003'.
5957 IT->dpvec holds the glyphs to return as characters.
5958 IT->saved_face_id holds the face id before the display vector--
5959 it is restored into IT->face_idin set_iterator_to_next. */
5961 static int
5962 next_element_from_display_vector (it)
5963 struct it *it;
5965 /* Precondition. */
5966 xassert (it->dpvec && it->current.dpvec_index >= 0);
5968 it->face_id = it->saved_face_id;
5970 if (INTEGERP (*it->dpvec)
5971 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5973 GLYPH g;
5975 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5976 it->c = FAST_GLYPH_CHAR (g);
5977 it->len = CHAR_BYTES (it->c);
5979 /* The entry may contain a face id to use. Such a face id is
5980 the id of a Lisp face, not a realized face. A face id of
5981 zero means no face is specified. */
5982 if (it->dpvec_face_id >= 0)
5983 it->face_id = it->dpvec_face_id;
5984 else
5986 int lface_id = FAST_GLYPH_FACE (g);
5987 if (lface_id > 0)
5988 it->face_id = merge_faces (it->f, Qt, lface_id,
5989 it->saved_face_id);
5992 else
5993 /* Display table entry is invalid. Return a space. */
5994 it->c = ' ', it->len = 1;
5996 /* Don't change position and object of the iterator here. They are
5997 still the values of the character that had this display table
5998 entry or was translated, and that's what we want. */
5999 it->what = IT_CHARACTER;
6000 return 1;
6004 /* Load IT with the next display element from Lisp string IT->string.
6005 IT->current.string_pos is the current position within the string.
6006 If IT->current.overlay_string_index >= 0, the Lisp string is an
6007 overlay string. */
6009 static int
6010 next_element_from_string (it)
6011 struct it *it;
6013 struct text_pos position;
6015 xassert (STRINGP (it->string));
6016 xassert (IT_STRING_CHARPOS (*it) >= 0);
6017 position = it->current.string_pos;
6019 /* Time to check for invisible text? */
6020 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6021 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6023 handle_stop (it);
6025 /* Since a handler may have changed IT->method, we must
6026 recurse here. */
6027 return get_next_display_element (it);
6030 if (it->current.overlay_string_index >= 0)
6032 /* Get the next character from an overlay string. In overlay
6033 strings, There is no field width or padding with spaces to
6034 do. */
6035 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6037 it->what = IT_EOB;
6038 return 0;
6040 else if (STRING_MULTIBYTE (it->string))
6042 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6043 const unsigned char *s = (SDATA (it->string)
6044 + IT_STRING_BYTEPOS (*it));
6045 it->c = string_char_and_length (s, remaining, &it->len);
6047 else
6049 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6050 it->len = 1;
6053 else
6055 /* Get the next character from a Lisp string that is not an
6056 overlay string. Such strings come from the mode line, for
6057 example. We may have to pad with spaces, or truncate the
6058 string. See also next_element_from_c_string. */
6059 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6061 it->what = IT_EOB;
6062 return 0;
6064 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6066 /* Pad with spaces. */
6067 it->c = ' ', it->len = 1;
6068 CHARPOS (position) = BYTEPOS (position) = -1;
6070 else if (STRING_MULTIBYTE (it->string))
6072 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6073 const unsigned char *s = (SDATA (it->string)
6074 + IT_STRING_BYTEPOS (*it));
6075 it->c = string_char_and_length (s, maxlen, &it->len);
6077 else
6079 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6080 it->len = 1;
6084 /* Record what we have and where it came from. */
6085 it->what = IT_CHARACTER;
6086 it->object = it->string;
6087 it->position = position;
6088 return 1;
6092 /* Load IT with next display element from C string IT->s.
6093 IT->string_nchars is the maximum number of characters to return
6094 from the string. IT->end_charpos may be greater than
6095 IT->string_nchars when this function is called, in which case we
6096 may have to return padding spaces. Value is zero if end of string
6097 reached, including padding spaces. */
6099 static int
6100 next_element_from_c_string (it)
6101 struct it *it;
6103 int success_p = 1;
6105 xassert (it->s);
6106 it->what = IT_CHARACTER;
6107 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6108 it->object = Qnil;
6110 /* IT's position can be greater IT->string_nchars in case a field
6111 width or precision has been specified when the iterator was
6112 initialized. */
6113 if (IT_CHARPOS (*it) >= it->end_charpos)
6115 /* End of the game. */
6116 it->what = IT_EOB;
6117 success_p = 0;
6119 else if (IT_CHARPOS (*it) >= it->string_nchars)
6121 /* Pad with spaces. */
6122 it->c = ' ', it->len = 1;
6123 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6125 else if (it->multibyte_p)
6127 /* Implementation note: The calls to strlen apparently aren't a
6128 performance problem because there is no noticeable performance
6129 difference between Emacs running in unibyte or multibyte mode. */
6130 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6131 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6132 maxlen, &it->len);
6134 else
6135 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6137 return success_p;
6141 /* Set up IT to return characters from an ellipsis, if appropriate.
6142 The definition of the ellipsis glyphs may come from a display table
6143 entry. This function Fills IT with the first glyph from the
6144 ellipsis if an ellipsis is to be displayed. */
6146 static int
6147 next_element_from_ellipsis (it)
6148 struct it *it;
6150 if (it->selective_display_ellipsis_p)
6151 setup_for_ellipsis (it, it->len);
6152 else
6154 /* The face at the current position may be different from the
6155 face we find after the invisible text. Remember what it
6156 was in IT->saved_face_id, and signal that it's there by
6157 setting face_before_selective_p. */
6158 it->saved_face_id = it->face_id;
6159 it->method = GET_FROM_BUFFER;
6160 it->object = it->w->buffer;
6161 reseat_at_next_visible_line_start (it, 1);
6162 it->face_before_selective_p = 1;
6165 return get_next_display_element (it);
6169 /* Deliver an image display element. The iterator IT is already
6170 filled with image information (done in handle_display_prop). Value
6171 is always 1. */
6174 static int
6175 next_element_from_image (it)
6176 struct it *it;
6178 it->what = IT_IMAGE;
6179 return 1;
6183 /* Fill iterator IT with next display element from a stretch glyph
6184 property. IT->object is the value of the text property. Value is
6185 always 1. */
6187 static int
6188 next_element_from_stretch (it)
6189 struct it *it;
6191 it->what = IT_STRETCH;
6192 return 1;
6196 /* Load IT with the next display element from current_buffer. Value
6197 is zero if end of buffer reached. IT->stop_charpos is the next
6198 position at which to stop and check for text properties or buffer
6199 end. */
6201 static int
6202 next_element_from_buffer (it)
6203 struct it *it;
6205 int success_p = 1;
6207 /* Check this assumption, otherwise, we would never enter the
6208 if-statement, below. */
6209 xassert (IT_CHARPOS (*it) >= BEGV
6210 && IT_CHARPOS (*it) <= it->stop_charpos);
6212 if (IT_CHARPOS (*it) >= it->stop_charpos)
6214 if (IT_CHARPOS (*it) >= it->end_charpos)
6216 int overlay_strings_follow_p;
6218 /* End of the game, except when overlay strings follow that
6219 haven't been returned yet. */
6220 if (it->overlay_strings_at_end_processed_p)
6221 overlay_strings_follow_p = 0;
6222 else
6224 it->overlay_strings_at_end_processed_p = 1;
6225 overlay_strings_follow_p = get_overlay_strings (it, 0);
6228 if (overlay_strings_follow_p)
6229 success_p = get_next_display_element (it);
6230 else
6232 it->what = IT_EOB;
6233 it->position = it->current.pos;
6234 success_p = 0;
6237 else
6239 handle_stop (it);
6240 return get_next_display_element (it);
6243 else
6245 /* No face changes, overlays etc. in sight, so just return a
6246 character from current_buffer. */
6247 unsigned char *p;
6249 /* Maybe run the redisplay end trigger hook. Performance note:
6250 This doesn't seem to cost measurable time. */
6251 if (it->redisplay_end_trigger_charpos
6252 && it->glyph_row
6253 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6254 run_redisplay_end_trigger_hook (it);
6256 /* Get the next character, maybe multibyte. */
6257 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6258 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6260 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6261 - IT_BYTEPOS (*it));
6262 it->c = string_char_and_length (p, maxlen, &it->len);
6264 else
6265 it->c = *p, it->len = 1;
6267 /* Record what we have and where it came from. */
6268 it->what = IT_CHARACTER;;
6269 it->object = it->w->buffer;
6270 it->position = it->current.pos;
6272 /* Normally we return the character found above, except when we
6273 really want to return an ellipsis for selective display. */
6274 if (it->selective)
6276 if (it->c == '\n')
6278 /* A value of selective > 0 means hide lines indented more
6279 than that number of columns. */
6280 if (it->selective > 0
6281 && IT_CHARPOS (*it) + 1 < ZV
6282 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6283 IT_BYTEPOS (*it) + 1,
6284 (double) it->selective)) /* iftc */
6286 success_p = next_element_from_ellipsis (it);
6287 it->dpvec_char_len = -1;
6290 else if (it->c == '\r' && it->selective == -1)
6292 /* A value of selective == -1 means that everything from the
6293 CR to the end of the line is invisible, with maybe an
6294 ellipsis displayed for it. */
6295 success_p = next_element_from_ellipsis (it);
6296 it->dpvec_char_len = -1;
6301 /* Value is zero if end of buffer reached. */
6302 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6303 return success_p;
6307 /* Run the redisplay end trigger hook for IT. */
6309 static void
6310 run_redisplay_end_trigger_hook (it)
6311 struct it *it;
6313 Lisp_Object args[3];
6315 /* IT->glyph_row should be non-null, i.e. we should be actually
6316 displaying something, or otherwise we should not run the hook. */
6317 xassert (it->glyph_row);
6319 /* Set up hook arguments. */
6320 args[0] = Qredisplay_end_trigger_functions;
6321 args[1] = it->window;
6322 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6323 it->redisplay_end_trigger_charpos = 0;
6325 /* Since we are *trying* to run these functions, don't try to run
6326 them again, even if they get an error. */
6327 it->w->redisplay_end_trigger = Qnil;
6328 Frun_hook_with_args (3, args);
6330 /* Notice if it changed the face of the character we are on. */
6331 handle_face_prop (it);
6335 /* Deliver a composition display element. The iterator IT is already
6336 filled with composition information (done in
6337 handle_composition_prop). Value is always 1. */
6339 static int
6340 next_element_from_composition (it)
6341 struct it *it;
6343 it->what = IT_COMPOSITION;
6344 it->position = (STRINGP (it->string)
6345 ? it->current.string_pos
6346 : it->current.pos);
6347 if (STRINGP (it->string))
6348 it->object = it->string;
6349 else
6350 it->object = it->w->buffer;
6351 return 1;
6356 /***********************************************************************
6357 Moving an iterator without producing glyphs
6358 ***********************************************************************/
6360 /* Check if iterator is at a position corresponding to a valid buffer
6361 position after some move_it_ call. */
6363 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6364 ((it)->method == GET_FROM_STRING \
6365 ? IT_STRING_CHARPOS (*it) == 0 \
6366 : 1)
6369 /* Move iterator IT to a specified buffer or X position within one
6370 line on the display without producing glyphs.
6372 OP should be a bit mask including some or all of these bits:
6373 MOVE_TO_X: Stop on reaching x-position TO_X.
6374 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6375 Regardless of OP's value, stop in reaching the end of the display line.
6377 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6378 This means, in particular, that TO_X includes window's horizontal
6379 scroll amount.
6381 The return value has several possible values that
6382 say what condition caused the scan to stop:
6384 MOVE_POS_MATCH_OR_ZV
6385 - when TO_POS or ZV was reached.
6387 MOVE_X_REACHED
6388 -when TO_X was reached before TO_POS or ZV were reached.
6390 MOVE_LINE_CONTINUED
6391 - when we reached the end of the display area and the line must
6392 be continued.
6394 MOVE_LINE_TRUNCATED
6395 - when we reached the end of the display area and the line is
6396 truncated.
6398 MOVE_NEWLINE_OR_CR
6399 - when we stopped at a line end, i.e. a newline or a CR and selective
6400 display is on. */
6402 static enum move_it_result
6403 move_it_in_display_line_to (it, to_charpos, to_x, op)
6404 struct it *it;
6405 int to_charpos, to_x, op;
6407 enum move_it_result result = MOVE_UNDEFINED;
6408 struct glyph_row *saved_glyph_row;
6410 /* Don't produce glyphs in produce_glyphs. */
6411 saved_glyph_row = it->glyph_row;
6412 it->glyph_row = NULL;
6414 #define BUFFER_POS_REACHED_P() \
6415 ((op & MOVE_TO_POS) != 0 \
6416 && BUFFERP (it->object) \
6417 && IT_CHARPOS (*it) >= to_charpos \
6418 && (it->method == GET_FROM_BUFFER \
6419 || (it->method == GET_FROM_DISPLAY_VECTOR \
6420 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6423 while (1)
6425 int x, i, ascent = 0, descent = 0;
6427 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6428 if ((op & MOVE_TO_POS) != 0
6429 && BUFFERP (it->object)
6430 && it->method == GET_FROM_BUFFER
6431 && IT_CHARPOS (*it) > to_charpos)
6433 result = MOVE_POS_MATCH_OR_ZV;
6434 break;
6437 /* Stop when ZV reached.
6438 We used to stop here when TO_CHARPOS reached as well, but that is
6439 too soon if this glyph does not fit on this line. So we handle it
6440 explicitly below. */
6441 if (!get_next_display_element (it)
6442 || (it->truncate_lines_p
6443 && BUFFER_POS_REACHED_P ()))
6445 result = MOVE_POS_MATCH_OR_ZV;
6446 break;
6449 /* The call to produce_glyphs will get the metrics of the
6450 display element IT is loaded with. We record in x the
6451 x-position before this display element in case it does not
6452 fit on the line. */
6453 x = it->current_x;
6455 /* Remember the line height so far in case the next element doesn't
6456 fit on the line. */
6457 if (!it->truncate_lines_p)
6459 ascent = it->max_ascent;
6460 descent = it->max_descent;
6463 PRODUCE_GLYPHS (it);
6465 if (it->area != TEXT_AREA)
6467 set_iterator_to_next (it, 1);
6468 continue;
6471 /* The number of glyphs we get back in IT->nglyphs will normally
6472 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6473 character on a terminal frame, or (iii) a line end. For the
6474 second case, IT->nglyphs - 1 padding glyphs will be present
6475 (on X frames, there is only one glyph produced for a
6476 composite character.
6478 The behavior implemented below means, for continuation lines,
6479 that as many spaces of a TAB as fit on the current line are
6480 displayed there. For terminal frames, as many glyphs of a
6481 multi-glyph character are displayed in the current line, too.
6482 This is what the old redisplay code did, and we keep it that
6483 way. Under X, the whole shape of a complex character must
6484 fit on the line or it will be completely displayed in the
6485 next line.
6487 Note that both for tabs and padding glyphs, all glyphs have
6488 the same width. */
6489 if (it->nglyphs)
6491 /* More than one glyph or glyph doesn't fit on line. All
6492 glyphs have the same width. */
6493 int single_glyph_width = it->pixel_width / it->nglyphs;
6494 int new_x;
6495 int x_before_this_char = x;
6496 int hpos_before_this_char = it->hpos;
6498 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6500 new_x = x + single_glyph_width;
6502 /* We want to leave anything reaching TO_X to the caller. */
6503 if ((op & MOVE_TO_X) && new_x > to_x)
6505 if (BUFFER_POS_REACHED_P ())
6506 goto buffer_pos_reached;
6507 it->current_x = x;
6508 result = MOVE_X_REACHED;
6509 break;
6511 else if (/* Lines are continued. */
6512 !it->truncate_lines_p
6513 && (/* And glyph doesn't fit on the line. */
6514 new_x > it->last_visible_x
6515 /* Or it fits exactly and we're on a window
6516 system frame. */
6517 || (new_x == it->last_visible_x
6518 && FRAME_WINDOW_P (it->f))))
6520 if (/* IT->hpos == 0 means the very first glyph
6521 doesn't fit on the line, e.g. a wide image. */
6522 it->hpos == 0
6523 || (new_x == it->last_visible_x
6524 && FRAME_WINDOW_P (it->f)))
6526 ++it->hpos;
6527 it->current_x = new_x;
6529 /* The character's last glyph just barely fits
6530 in this row. */
6531 if (i == it->nglyphs - 1)
6533 /* If this is the destination position,
6534 return a position *before* it in this row,
6535 now that we know it fits in this row. */
6536 if (BUFFER_POS_REACHED_P ())
6538 it->hpos = hpos_before_this_char;
6539 it->current_x = x_before_this_char;
6540 result = MOVE_POS_MATCH_OR_ZV;
6541 break;
6544 set_iterator_to_next (it, 1);
6545 #ifdef HAVE_WINDOW_SYSTEM
6546 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6548 if (!get_next_display_element (it))
6550 result = MOVE_POS_MATCH_OR_ZV;
6551 break;
6553 if (BUFFER_POS_REACHED_P ())
6555 if (ITERATOR_AT_END_OF_LINE_P (it))
6556 result = MOVE_POS_MATCH_OR_ZV;
6557 else
6558 result = MOVE_LINE_CONTINUED;
6559 break;
6561 if (ITERATOR_AT_END_OF_LINE_P (it))
6563 result = MOVE_NEWLINE_OR_CR;
6564 break;
6567 #endif /* HAVE_WINDOW_SYSTEM */
6570 else
6572 it->current_x = x;
6573 it->max_ascent = ascent;
6574 it->max_descent = descent;
6577 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6578 IT_CHARPOS (*it)));
6579 result = MOVE_LINE_CONTINUED;
6580 break;
6582 else if (BUFFER_POS_REACHED_P ())
6583 goto buffer_pos_reached;
6584 else if (new_x > it->first_visible_x)
6586 /* Glyph is visible. Increment number of glyphs that
6587 would be displayed. */
6588 ++it->hpos;
6590 else
6592 /* Glyph is completely off the left margin of the display
6593 area. Nothing to do. */
6597 if (result != MOVE_UNDEFINED)
6598 break;
6600 else if (BUFFER_POS_REACHED_P ())
6602 buffer_pos_reached:
6603 it->current_x = x;
6604 it->max_ascent = ascent;
6605 it->max_descent = descent;
6606 result = MOVE_POS_MATCH_OR_ZV;
6607 break;
6609 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6611 /* Stop when TO_X specified and reached. This check is
6612 necessary here because of lines consisting of a line end,
6613 only. The line end will not produce any glyphs and we
6614 would never get MOVE_X_REACHED. */
6615 xassert (it->nglyphs == 0);
6616 result = MOVE_X_REACHED;
6617 break;
6620 /* Is this a line end? If yes, we're done. */
6621 if (ITERATOR_AT_END_OF_LINE_P (it))
6623 result = MOVE_NEWLINE_OR_CR;
6624 break;
6627 /* The current display element has been consumed. Advance
6628 to the next. */
6629 set_iterator_to_next (it, 1);
6631 /* Stop if lines are truncated and IT's current x-position is
6632 past the right edge of the window now. */
6633 if (it->truncate_lines_p
6634 && it->current_x >= it->last_visible_x)
6636 #ifdef HAVE_WINDOW_SYSTEM
6637 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6639 if (!get_next_display_element (it)
6640 || BUFFER_POS_REACHED_P ())
6642 result = MOVE_POS_MATCH_OR_ZV;
6643 break;
6645 if (ITERATOR_AT_END_OF_LINE_P (it))
6647 result = MOVE_NEWLINE_OR_CR;
6648 break;
6651 #endif /* HAVE_WINDOW_SYSTEM */
6652 result = MOVE_LINE_TRUNCATED;
6653 break;
6657 #undef BUFFER_POS_REACHED_P
6659 /* Restore the iterator settings altered at the beginning of this
6660 function. */
6661 it->glyph_row = saved_glyph_row;
6662 return result;
6666 /* Move IT forward until it satisfies one or more of the criteria in
6667 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6669 OP is a bit-mask that specifies where to stop, and in particular,
6670 which of those four position arguments makes a difference. See the
6671 description of enum move_operation_enum.
6673 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6674 screen line, this function will set IT to the next position >
6675 TO_CHARPOS. */
6677 void
6678 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6679 struct it *it;
6680 int to_charpos, to_x, to_y, to_vpos;
6681 int op;
6683 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6684 int line_height;
6685 int reached = 0;
6687 for (;;)
6689 if (op & MOVE_TO_VPOS)
6691 /* If no TO_CHARPOS and no TO_X specified, stop at the
6692 start of the line TO_VPOS. */
6693 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6695 if (it->vpos == to_vpos)
6697 reached = 1;
6698 break;
6700 else
6701 skip = move_it_in_display_line_to (it, -1, -1, 0);
6703 else
6705 /* TO_VPOS >= 0 means stop at TO_X in the line at
6706 TO_VPOS, or at TO_POS, whichever comes first. */
6707 if (it->vpos == to_vpos)
6709 reached = 2;
6710 break;
6713 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6715 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6717 reached = 3;
6718 break;
6720 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6722 /* We have reached TO_X but not in the line we want. */
6723 skip = move_it_in_display_line_to (it, to_charpos,
6724 -1, MOVE_TO_POS);
6725 if (skip == MOVE_POS_MATCH_OR_ZV)
6727 reached = 4;
6728 break;
6733 else if (op & MOVE_TO_Y)
6735 struct it it_backup;
6737 /* TO_Y specified means stop at TO_X in the line containing
6738 TO_Y---or at TO_CHARPOS if this is reached first. The
6739 problem is that we can't really tell whether the line
6740 contains TO_Y before we have completely scanned it, and
6741 this may skip past TO_X. What we do is to first scan to
6742 TO_X.
6744 If TO_X is not specified, use a TO_X of zero. The reason
6745 is to make the outcome of this function more predictable.
6746 If we didn't use TO_X == 0, we would stop at the end of
6747 the line which is probably not what a caller would expect
6748 to happen. */
6749 skip = move_it_in_display_line_to (it, to_charpos,
6750 ((op & MOVE_TO_X)
6751 ? to_x : 0),
6752 (MOVE_TO_X
6753 | (op & MOVE_TO_POS)));
6755 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6756 if (skip == MOVE_POS_MATCH_OR_ZV)
6758 reached = 5;
6759 break;
6762 /* If TO_X was reached, we would like to know whether TO_Y
6763 is in the line. This can only be said if we know the
6764 total line height which requires us to scan the rest of
6765 the line. */
6766 if (skip == MOVE_X_REACHED)
6768 it_backup = *it;
6769 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6770 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6771 op & MOVE_TO_POS);
6772 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6775 /* Now, decide whether TO_Y is in this line. */
6776 line_height = it->max_ascent + it->max_descent;
6777 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6779 if (to_y >= it->current_y
6780 && to_y < it->current_y + line_height)
6782 if (skip == MOVE_X_REACHED)
6783 /* If TO_Y is in this line and TO_X was reached above,
6784 we scanned too far. We have to restore IT's settings
6785 to the ones before skipping. */
6786 *it = it_backup;
6787 reached = 6;
6789 else if (skip == MOVE_X_REACHED)
6791 skip = skip2;
6792 if (skip == MOVE_POS_MATCH_OR_ZV)
6793 reached = 7;
6796 if (reached)
6797 break;
6799 else if (BUFFERP (it->object)
6800 && it->method == GET_FROM_BUFFER
6801 && IT_CHARPOS (*it) >= to_charpos)
6802 skip = MOVE_POS_MATCH_OR_ZV;
6803 else
6804 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6806 switch (skip)
6808 case MOVE_POS_MATCH_OR_ZV:
6809 reached = 8;
6810 goto out;
6812 case MOVE_NEWLINE_OR_CR:
6813 set_iterator_to_next (it, 1);
6814 it->continuation_lines_width = 0;
6815 break;
6817 case MOVE_LINE_TRUNCATED:
6818 it->continuation_lines_width = 0;
6819 reseat_at_next_visible_line_start (it, 0);
6820 if ((op & MOVE_TO_POS) != 0
6821 && IT_CHARPOS (*it) > to_charpos)
6823 reached = 9;
6824 goto out;
6826 break;
6828 case MOVE_LINE_CONTINUED:
6829 it->continuation_lines_width += it->current_x;
6830 break;
6832 default:
6833 abort ();
6836 /* Reset/increment for the next run. */
6837 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6838 it->current_x = it->hpos = 0;
6839 it->current_y += it->max_ascent + it->max_descent;
6840 ++it->vpos;
6841 last_height = it->max_ascent + it->max_descent;
6842 last_max_ascent = it->max_ascent;
6843 it->max_ascent = it->max_descent = 0;
6846 out:
6848 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6852 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6854 If DY > 0, move IT backward at least that many pixels. DY = 0
6855 means move IT backward to the preceding line start or BEGV. This
6856 function may move over more than DY pixels if IT->current_y - DY
6857 ends up in the middle of a line; in this case IT->current_y will be
6858 set to the top of the line moved to. */
6860 void
6861 move_it_vertically_backward (it, dy)
6862 struct it *it;
6863 int dy;
6865 int nlines, h;
6866 struct it it2, it3;
6867 int start_pos;
6869 move_further_back:
6870 xassert (dy >= 0);
6872 start_pos = IT_CHARPOS (*it);
6874 /* Estimate how many newlines we must move back. */
6875 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6877 /* Set the iterator's position that many lines back. */
6878 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6879 back_to_previous_visible_line_start (it);
6881 /* Reseat the iterator here. When moving backward, we don't want
6882 reseat to skip forward over invisible text, set up the iterator
6883 to deliver from overlay strings at the new position etc. So,
6884 use reseat_1 here. */
6885 reseat_1 (it, it->current.pos, 1);
6887 /* We are now surely at a line start. */
6888 it->current_x = it->hpos = 0;
6889 it->continuation_lines_width = 0;
6891 /* Move forward and see what y-distance we moved. First move to the
6892 start of the next line so that we get its height. We need this
6893 height to be able to tell whether we reached the specified
6894 y-distance. */
6895 it2 = *it;
6896 it2.max_ascent = it2.max_descent = 0;
6899 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6900 MOVE_TO_POS | MOVE_TO_VPOS);
6902 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6903 xassert (IT_CHARPOS (*it) >= BEGV);
6904 it3 = it2;
6906 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6907 xassert (IT_CHARPOS (*it) >= BEGV);
6908 /* H is the actual vertical distance from the position in *IT
6909 and the starting position. */
6910 h = it2.current_y - it->current_y;
6911 /* NLINES is the distance in number of lines. */
6912 nlines = it2.vpos - it->vpos;
6914 /* Correct IT's y and vpos position
6915 so that they are relative to the starting point. */
6916 it->vpos -= nlines;
6917 it->current_y -= h;
6919 if (dy == 0)
6921 /* DY == 0 means move to the start of the screen line. The
6922 value of nlines is > 0 if continuation lines were involved. */
6923 if (nlines > 0)
6924 move_it_by_lines (it, nlines, 1);
6925 #if 0
6926 /* I think this assert is bogus if buffer contains
6927 invisible text or images. KFS. */
6928 xassert (IT_CHARPOS (*it) <= start_pos);
6929 #endif
6931 else
6933 /* The y-position we try to reach, relative to *IT.
6934 Note that H has been subtracted in front of the if-statement. */
6935 int target_y = it->current_y + h - dy;
6936 int y0 = it3.current_y;
6937 int y1 = line_bottom_y (&it3);
6938 int line_height = y1 - y0;
6940 /* If we did not reach target_y, try to move further backward if
6941 we can. If we moved too far backward, try to move forward. */
6942 if (target_y < it->current_y
6943 /* This is heuristic. In a window that's 3 lines high, with
6944 a line height of 13 pixels each, recentering with point
6945 on the bottom line will try to move -39/2 = 19 pixels
6946 backward. Try to avoid moving into the first line. */
6947 && (it->current_y - target_y
6948 > min (window_box_height (it->w), line_height * 2 / 3))
6949 && IT_CHARPOS (*it) > BEGV)
6951 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6952 target_y - it->current_y));
6953 dy = it->current_y - target_y;
6954 goto move_further_back;
6956 else if (target_y >= it->current_y + line_height
6957 && IT_CHARPOS (*it) < ZV)
6959 /* Should move forward by at least one line, maybe more.
6961 Note: Calling move_it_by_lines can be expensive on
6962 terminal frames, where compute_motion is used (via
6963 vmotion) to do the job, when there are very long lines
6964 and truncate-lines is nil. That's the reason for
6965 treating terminal frames specially here. */
6967 if (!FRAME_WINDOW_P (it->f))
6968 move_it_vertically (it, target_y - (it->current_y + line_height));
6969 else
6973 move_it_by_lines (it, 1, 1);
6975 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6978 #if 0
6979 /* I think this assert is bogus if buffer contains
6980 invisible text or images. KFS. */
6981 xassert (IT_CHARPOS (*it) >= BEGV);
6982 #endif
6988 /* Move IT by a specified amount of pixel lines DY. DY negative means
6989 move backwards. DY = 0 means move to start of screen line. At the
6990 end, IT will be on the start of a screen line. */
6992 void
6993 move_it_vertically (it, dy)
6994 struct it *it;
6995 int dy;
6997 if (dy <= 0)
6998 move_it_vertically_backward (it, -dy);
6999 else
7001 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7002 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7003 MOVE_TO_POS | MOVE_TO_Y);
7004 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7006 /* If buffer ends in ZV without a newline, move to the start of
7007 the line to satisfy the post-condition. */
7008 if (IT_CHARPOS (*it) == ZV
7009 && ZV > BEGV
7010 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7011 move_it_by_lines (it, 0, 0);
7016 /* Move iterator IT past the end of the text line it is in. */
7018 void
7019 move_it_past_eol (it)
7020 struct it *it;
7022 enum move_it_result rc;
7024 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7025 if (rc == MOVE_NEWLINE_OR_CR)
7026 set_iterator_to_next (it, 0);
7030 #if 0 /* Currently not used. */
7032 /* Return non-zero if some text between buffer positions START_CHARPOS
7033 and END_CHARPOS is invisible. IT->window is the window for text
7034 property lookup. */
7036 static int
7037 invisible_text_between_p (it, start_charpos, end_charpos)
7038 struct it *it;
7039 int start_charpos, end_charpos;
7041 Lisp_Object prop, limit;
7042 int invisible_found_p;
7044 xassert (it != NULL && start_charpos <= end_charpos);
7046 /* Is text at START invisible? */
7047 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7048 it->window);
7049 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7050 invisible_found_p = 1;
7051 else
7053 limit = Fnext_single_char_property_change (make_number (start_charpos),
7054 Qinvisible, Qnil,
7055 make_number (end_charpos));
7056 invisible_found_p = XFASTINT (limit) < end_charpos;
7059 return invisible_found_p;
7062 #endif /* 0 */
7065 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7066 negative means move up. DVPOS == 0 means move to the start of the
7067 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7068 NEED_Y_P is zero, IT->current_y will be left unchanged.
7070 Further optimization ideas: If we would know that IT->f doesn't use
7071 a face with proportional font, we could be faster for
7072 truncate-lines nil. */
7074 void
7075 move_it_by_lines (it, dvpos, need_y_p)
7076 struct it *it;
7077 int dvpos, need_y_p;
7079 struct position pos;
7081 if (!FRAME_WINDOW_P (it->f))
7083 struct text_pos textpos;
7085 /* We can use vmotion on frames without proportional fonts. */
7086 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7087 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7088 reseat (it, textpos, 1);
7089 it->vpos += pos.vpos;
7090 it->current_y += pos.vpos;
7092 else if (dvpos == 0)
7094 /* DVPOS == 0 means move to the start of the screen line. */
7095 move_it_vertically_backward (it, 0);
7096 xassert (it->current_x == 0 && it->hpos == 0);
7097 /* Let next call to line_bottom_y calculate real line height */
7098 last_height = 0;
7100 else if (dvpos > 0)
7102 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7103 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7104 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7106 else
7108 struct it it2;
7109 int start_charpos, i;
7111 /* Start at the beginning of the screen line containing IT's
7112 position. This may actually move vertically backwards,
7113 in case of overlays, so adjust dvpos accordingly. */
7114 dvpos += it->vpos;
7115 move_it_vertically_backward (it, 0);
7116 dvpos -= it->vpos;
7118 /* Go back -DVPOS visible lines and reseat the iterator there. */
7119 start_charpos = IT_CHARPOS (*it);
7120 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7121 back_to_previous_visible_line_start (it);
7122 reseat (it, it->current.pos, 1);
7124 /* Move further back if we end up in a string or an image. */
7125 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7127 /* First try to move to start of display line. */
7128 dvpos += it->vpos;
7129 move_it_vertically_backward (it, 0);
7130 dvpos -= it->vpos;
7131 if (IT_POS_VALID_AFTER_MOVE_P (it))
7132 break;
7133 /* If start of line is still in string or image,
7134 move further back. */
7135 back_to_previous_visible_line_start (it);
7136 reseat (it, it->current.pos, 1);
7137 dvpos--;
7140 it->current_x = it->hpos = 0;
7142 /* Above call may have moved too far if continuation lines
7143 are involved. Scan forward and see if it did. */
7144 it2 = *it;
7145 it2.vpos = it2.current_y = 0;
7146 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7147 it->vpos -= it2.vpos;
7148 it->current_y -= it2.current_y;
7149 it->current_x = it->hpos = 0;
7151 /* If we moved too far back, move IT some lines forward. */
7152 if (it2.vpos > -dvpos)
7154 int delta = it2.vpos + dvpos;
7155 it2 = *it;
7156 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7157 /* Move back again if we got too far ahead. */
7158 if (IT_CHARPOS (*it) >= start_charpos)
7159 *it = it2;
7164 /* Return 1 if IT points into the middle of a display vector. */
7167 in_display_vector_p (it)
7168 struct it *it;
7170 return (it->method == GET_FROM_DISPLAY_VECTOR
7171 && it->current.dpvec_index > 0
7172 && it->dpvec + it->current.dpvec_index != it->dpend);
7176 /***********************************************************************
7177 Messages
7178 ***********************************************************************/
7181 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7182 to *Messages*. */
7184 void
7185 add_to_log (format, arg1, arg2)
7186 char *format;
7187 Lisp_Object arg1, arg2;
7189 Lisp_Object args[3];
7190 Lisp_Object msg, fmt;
7191 char *buffer;
7192 int len;
7193 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7194 USE_SAFE_ALLOCA;
7196 /* Do nothing if called asynchronously. Inserting text into
7197 a buffer may call after-change-functions and alike and
7198 that would means running Lisp asynchronously. */
7199 if (handling_signal)
7200 return;
7202 fmt = msg = Qnil;
7203 GCPRO4 (fmt, msg, arg1, arg2);
7205 args[0] = fmt = build_string (format);
7206 args[1] = arg1;
7207 args[2] = arg2;
7208 msg = Fformat (3, args);
7210 len = SBYTES (msg) + 1;
7211 SAFE_ALLOCA (buffer, char *, len);
7212 bcopy (SDATA (msg), buffer, len);
7214 message_dolog (buffer, len - 1, 1, 0);
7215 SAFE_FREE ();
7217 UNGCPRO;
7221 /* Output a newline in the *Messages* buffer if "needs" one. */
7223 void
7224 message_log_maybe_newline ()
7226 if (message_log_need_newline)
7227 message_dolog ("", 0, 1, 0);
7231 /* Add a string M of length NBYTES to the message log, optionally
7232 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7233 nonzero, means interpret the contents of M as multibyte. This
7234 function calls low-level routines in order to bypass text property
7235 hooks, etc. which might not be safe to run.
7237 This may GC (insert may run before/after change hooks),
7238 so the buffer M must NOT point to a Lisp string. */
7240 void
7241 message_dolog (m, nbytes, nlflag, multibyte)
7242 const char *m;
7243 int nbytes, nlflag, multibyte;
7245 if (!NILP (Vmemory_full))
7246 return;
7248 if (!NILP (Vmessage_log_max))
7250 struct buffer *oldbuf;
7251 Lisp_Object oldpoint, oldbegv, oldzv;
7252 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7253 int point_at_end = 0;
7254 int zv_at_end = 0;
7255 Lisp_Object old_deactivate_mark, tem;
7256 struct gcpro gcpro1;
7258 old_deactivate_mark = Vdeactivate_mark;
7259 oldbuf = current_buffer;
7260 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7261 current_buffer->undo_list = Qt;
7263 oldpoint = message_dolog_marker1;
7264 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7265 oldbegv = message_dolog_marker2;
7266 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7267 oldzv = message_dolog_marker3;
7268 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7269 GCPRO1 (old_deactivate_mark);
7271 if (PT == Z)
7272 point_at_end = 1;
7273 if (ZV == Z)
7274 zv_at_end = 1;
7276 BEGV = BEG;
7277 BEGV_BYTE = BEG_BYTE;
7278 ZV = Z;
7279 ZV_BYTE = Z_BYTE;
7280 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7282 /* Insert the string--maybe converting multibyte to single byte
7283 or vice versa, so that all the text fits the buffer. */
7284 if (multibyte
7285 && NILP (current_buffer->enable_multibyte_characters))
7287 int i, c, char_bytes;
7288 unsigned char work[1];
7290 /* Convert a multibyte string to single-byte
7291 for the *Message* buffer. */
7292 for (i = 0; i < nbytes; i += char_bytes)
7294 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7295 work[0] = (SINGLE_BYTE_CHAR_P (c)
7297 : multibyte_char_to_unibyte (c, Qnil));
7298 insert_1_both (work, 1, 1, 1, 0, 0);
7301 else if (! multibyte
7302 && ! NILP (current_buffer->enable_multibyte_characters))
7304 int i, c, char_bytes;
7305 unsigned char *msg = (unsigned char *) m;
7306 unsigned char str[MAX_MULTIBYTE_LENGTH];
7307 /* Convert a single-byte string to multibyte
7308 for the *Message* buffer. */
7309 for (i = 0; i < nbytes; i++)
7311 c = unibyte_char_to_multibyte (msg[i]);
7312 char_bytes = CHAR_STRING (c, str);
7313 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7316 else if (nbytes)
7317 insert_1 (m, nbytes, 1, 0, 0);
7319 if (nlflag)
7321 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7322 insert_1 ("\n", 1, 1, 0, 0);
7324 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7325 this_bol = PT;
7326 this_bol_byte = PT_BYTE;
7328 /* See if this line duplicates the previous one.
7329 If so, combine duplicates. */
7330 if (this_bol > BEG)
7332 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7333 prev_bol = PT;
7334 prev_bol_byte = PT_BYTE;
7336 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7337 this_bol, this_bol_byte);
7338 if (dup)
7340 del_range_both (prev_bol, prev_bol_byte,
7341 this_bol, this_bol_byte, 0);
7342 if (dup > 1)
7344 char dupstr[40];
7345 int duplen;
7347 /* If you change this format, don't forget to also
7348 change message_log_check_duplicate. */
7349 sprintf (dupstr, " [%d times]", dup);
7350 duplen = strlen (dupstr);
7351 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7352 insert_1 (dupstr, duplen, 1, 0, 1);
7357 /* If we have more than the desired maximum number of lines
7358 in the *Messages* buffer now, delete the oldest ones.
7359 This is safe because we don't have undo in this buffer. */
7361 if (NATNUMP (Vmessage_log_max))
7363 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7364 -XFASTINT (Vmessage_log_max) - 1, 0);
7365 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7368 BEGV = XMARKER (oldbegv)->charpos;
7369 BEGV_BYTE = marker_byte_position (oldbegv);
7371 if (zv_at_end)
7373 ZV = Z;
7374 ZV_BYTE = Z_BYTE;
7376 else
7378 ZV = XMARKER (oldzv)->charpos;
7379 ZV_BYTE = marker_byte_position (oldzv);
7382 if (point_at_end)
7383 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7384 else
7385 /* We can't do Fgoto_char (oldpoint) because it will run some
7386 Lisp code. */
7387 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7388 XMARKER (oldpoint)->bytepos);
7390 UNGCPRO;
7391 unchain_marker (XMARKER (oldpoint));
7392 unchain_marker (XMARKER (oldbegv));
7393 unchain_marker (XMARKER (oldzv));
7395 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7396 set_buffer_internal (oldbuf);
7397 if (NILP (tem))
7398 windows_or_buffers_changed = old_windows_or_buffers_changed;
7399 message_log_need_newline = !nlflag;
7400 Vdeactivate_mark = old_deactivate_mark;
7405 /* We are at the end of the buffer after just having inserted a newline.
7406 (Note: We depend on the fact we won't be crossing the gap.)
7407 Check to see if the most recent message looks a lot like the previous one.
7408 Return 0 if different, 1 if the new one should just replace it, or a
7409 value N > 1 if we should also append " [N times]". */
7411 static int
7412 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7413 int prev_bol, this_bol;
7414 int prev_bol_byte, this_bol_byte;
7416 int i;
7417 int len = Z_BYTE - 1 - this_bol_byte;
7418 int seen_dots = 0;
7419 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7420 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7422 for (i = 0; i < len; i++)
7424 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7425 seen_dots = 1;
7426 if (p1[i] != p2[i])
7427 return seen_dots;
7429 p1 += len;
7430 if (*p1 == '\n')
7431 return 2;
7432 if (*p1++ == ' ' && *p1++ == '[')
7434 int n = 0;
7435 while (*p1 >= '0' && *p1 <= '9')
7436 n = n * 10 + *p1++ - '0';
7437 if (strncmp (p1, " times]\n", 8) == 0)
7438 return n+1;
7440 return 0;
7444 /* Display an echo area message M with a specified length of NBYTES
7445 bytes. The string may include null characters. If M is 0, clear
7446 out any existing message, and let the mini-buffer text show
7447 through.
7449 This may GC, so the buffer M must NOT point to a Lisp string. */
7451 void
7452 message2 (m, nbytes, multibyte)
7453 const char *m;
7454 int nbytes;
7455 int multibyte;
7457 /* First flush out any partial line written with print. */
7458 message_log_maybe_newline ();
7459 if (m)
7460 message_dolog (m, nbytes, 1, multibyte);
7461 message2_nolog (m, nbytes, multibyte);
7465 /* The non-logging counterpart of message2. */
7467 void
7468 message2_nolog (m, nbytes, multibyte)
7469 const char *m;
7470 int nbytes, multibyte;
7472 struct frame *sf = SELECTED_FRAME ();
7473 message_enable_multibyte = multibyte;
7475 if (noninteractive)
7477 if (noninteractive_need_newline)
7478 putc ('\n', stderr);
7479 noninteractive_need_newline = 0;
7480 if (m)
7481 fwrite (m, nbytes, 1, stderr);
7482 if (cursor_in_echo_area == 0)
7483 fprintf (stderr, "\n");
7484 fflush (stderr);
7486 /* A null message buffer means that the frame hasn't really been
7487 initialized yet. Error messages get reported properly by
7488 cmd_error, so this must be just an informative message; toss it. */
7489 else if (INTERACTIVE
7490 && sf->glyphs_initialized_p
7491 && FRAME_MESSAGE_BUF (sf))
7493 Lisp_Object mini_window;
7494 struct frame *f;
7496 /* Get the frame containing the mini-buffer
7497 that the selected frame is using. */
7498 mini_window = FRAME_MINIBUF_WINDOW (sf);
7499 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7501 FRAME_SAMPLE_VISIBILITY (f);
7502 if (FRAME_VISIBLE_P (sf)
7503 && ! FRAME_VISIBLE_P (f))
7504 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7506 if (m)
7508 set_message (m, Qnil, nbytes, multibyte);
7509 if (minibuffer_auto_raise)
7510 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7512 else
7513 clear_message (1, 1);
7515 do_pending_window_change (0);
7516 echo_area_display (1);
7517 do_pending_window_change (0);
7518 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7519 (*frame_up_to_date_hook) (f);
7524 /* Display an echo area message M with a specified length of NBYTES
7525 bytes. The string may include null characters. If M is not a
7526 string, clear out any existing message, and let the mini-buffer
7527 text show through.
7529 This function cancels echoing. */
7531 void
7532 message3 (m, nbytes, multibyte)
7533 Lisp_Object m;
7534 int nbytes;
7535 int multibyte;
7537 struct gcpro gcpro1;
7539 GCPRO1 (m);
7540 clear_message (1,1);
7541 cancel_echoing ();
7543 /* First flush out any partial line written with print. */
7544 message_log_maybe_newline ();
7545 if (STRINGP (m))
7547 char *buffer;
7548 USE_SAFE_ALLOCA;
7550 SAFE_ALLOCA (buffer, char *, nbytes);
7551 bcopy (SDATA (m), buffer, nbytes);
7552 message_dolog (buffer, nbytes, 1, multibyte);
7553 SAFE_FREE ();
7555 message3_nolog (m, nbytes, multibyte);
7557 UNGCPRO;
7561 /* The non-logging version of message3.
7562 This does not cancel echoing, because it is used for echoing.
7563 Perhaps we need to make a separate function for echoing
7564 and make this cancel echoing. */
7566 void
7567 message3_nolog (m, nbytes, multibyte)
7568 Lisp_Object m;
7569 int nbytes, multibyte;
7571 struct frame *sf = SELECTED_FRAME ();
7572 message_enable_multibyte = multibyte;
7574 if (noninteractive)
7576 if (noninteractive_need_newline)
7577 putc ('\n', stderr);
7578 noninteractive_need_newline = 0;
7579 if (STRINGP (m))
7580 fwrite (SDATA (m), nbytes, 1, stderr);
7581 if (cursor_in_echo_area == 0)
7582 fprintf (stderr, "\n");
7583 fflush (stderr);
7585 /* A null message buffer means that the frame hasn't really been
7586 initialized yet. Error messages get reported properly by
7587 cmd_error, so this must be just an informative message; toss it. */
7588 else if (INTERACTIVE
7589 && sf->glyphs_initialized_p
7590 && FRAME_MESSAGE_BUF (sf))
7592 Lisp_Object mini_window;
7593 Lisp_Object frame;
7594 struct frame *f;
7596 /* Get the frame containing the mini-buffer
7597 that the selected frame is using. */
7598 mini_window = FRAME_MINIBUF_WINDOW (sf);
7599 frame = XWINDOW (mini_window)->frame;
7600 f = XFRAME (frame);
7602 FRAME_SAMPLE_VISIBILITY (f);
7603 if (FRAME_VISIBLE_P (sf)
7604 && !FRAME_VISIBLE_P (f))
7605 Fmake_frame_visible (frame);
7607 if (STRINGP (m) && SCHARS (m) > 0)
7609 set_message (NULL, m, nbytes, multibyte);
7610 if (minibuffer_auto_raise)
7611 Fraise_frame (frame);
7612 /* Assume we are not echoing.
7613 (If we are, echo_now will override this.) */
7614 echo_message_buffer = Qnil;
7616 else
7617 clear_message (1, 1);
7619 do_pending_window_change (0);
7620 echo_area_display (1);
7621 do_pending_window_change (0);
7622 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7623 (*frame_up_to_date_hook) (f);
7628 /* Display a null-terminated echo area message M. If M is 0, clear
7629 out any existing message, and let the mini-buffer text show through.
7631 The buffer M must continue to exist until after the echo area gets
7632 cleared or some other message gets displayed there. Do not pass
7633 text that is stored in a Lisp string. Do not pass text in a buffer
7634 that was alloca'd. */
7636 void
7637 message1 (m)
7638 char *m;
7640 message2 (m, (m ? strlen (m) : 0), 0);
7644 /* The non-logging counterpart of message1. */
7646 void
7647 message1_nolog (m)
7648 char *m;
7650 message2_nolog (m, (m ? strlen (m) : 0), 0);
7653 /* Display a message M which contains a single %s
7654 which gets replaced with STRING. */
7656 void
7657 message_with_string (m, string, log)
7658 char *m;
7659 Lisp_Object string;
7660 int log;
7662 CHECK_STRING (string);
7664 if (noninteractive)
7666 if (m)
7668 if (noninteractive_need_newline)
7669 putc ('\n', stderr);
7670 noninteractive_need_newline = 0;
7671 fprintf (stderr, m, SDATA (string));
7672 if (cursor_in_echo_area == 0)
7673 fprintf (stderr, "\n");
7674 fflush (stderr);
7677 else if (INTERACTIVE)
7679 /* The frame whose minibuffer we're going to display the message on.
7680 It may be larger than the selected frame, so we need
7681 to use its buffer, not the selected frame's buffer. */
7682 Lisp_Object mini_window;
7683 struct frame *f, *sf = SELECTED_FRAME ();
7685 /* Get the frame containing the minibuffer
7686 that the selected frame is using. */
7687 mini_window = FRAME_MINIBUF_WINDOW (sf);
7688 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7690 /* A null message buffer means that the frame hasn't really been
7691 initialized yet. Error messages get reported properly by
7692 cmd_error, so this must be just an informative message; toss it. */
7693 if (FRAME_MESSAGE_BUF (f))
7695 Lisp_Object args[2], message;
7696 struct gcpro gcpro1, gcpro2;
7698 args[0] = build_string (m);
7699 args[1] = message = string;
7700 GCPRO2 (args[0], message);
7701 gcpro1.nvars = 2;
7703 message = Fformat (2, args);
7705 if (log)
7706 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7707 else
7708 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7710 UNGCPRO;
7712 /* Print should start at the beginning of the message
7713 buffer next time. */
7714 message_buf_print = 0;
7720 /* Dump an informative message to the minibuf. If M is 0, clear out
7721 any existing message, and let the mini-buffer text show through. */
7723 /* VARARGS 1 */
7724 void
7725 message (m, a1, a2, a3)
7726 char *m;
7727 EMACS_INT a1, a2, a3;
7729 if (noninteractive)
7731 if (m)
7733 if (noninteractive_need_newline)
7734 putc ('\n', stderr);
7735 noninteractive_need_newline = 0;
7736 fprintf (stderr, m, a1, a2, a3);
7737 if (cursor_in_echo_area == 0)
7738 fprintf (stderr, "\n");
7739 fflush (stderr);
7742 else if (INTERACTIVE)
7744 /* The frame whose mini-buffer we're going to display the message
7745 on. It may be larger than the selected frame, so we need to
7746 use its buffer, not the selected frame's buffer. */
7747 Lisp_Object mini_window;
7748 struct frame *f, *sf = SELECTED_FRAME ();
7750 /* Get the frame containing the mini-buffer
7751 that the selected frame is using. */
7752 mini_window = FRAME_MINIBUF_WINDOW (sf);
7753 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7755 /* A null message buffer means that the frame hasn't really been
7756 initialized yet. Error messages get reported properly by
7757 cmd_error, so this must be just an informative message; toss
7758 it. */
7759 if (FRAME_MESSAGE_BUF (f))
7761 if (m)
7763 int len;
7764 #ifdef NO_ARG_ARRAY
7765 char *a[3];
7766 a[0] = (char *) a1;
7767 a[1] = (char *) a2;
7768 a[2] = (char *) a3;
7770 len = doprnt (FRAME_MESSAGE_BUF (f),
7771 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7772 #else
7773 len = doprnt (FRAME_MESSAGE_BUF (f),
7774 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7775 (char **) &a1);
7776 #endif /* NO_ARG_ARRAY */
7778 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7780 else
7781 message1 (0);
7783 /* Print should start at the beginning of the message
7784 buffer next time. */
7785 message_buf_print = 0;
7791 /* The non-logging version of message. */
7793 void
7794 message_nolog (m, a1, a2, a3)
7795 char *m;
7796 EMACS_INT a1, a2, a3;
7798 Lisp_Object old_log_max;
7799 old_log_max = Vmessage_log_max;
7800 Vmessage_log_max = Qnil;
7801 message (m, a1, a2, a3);
7802 Vmessage_log_max = old_log_max;
7806 /* Display the current message in the current mini-buffer. This is
7807 only called from error handlers in process.c, and is not time
7808 critical. */
7810 void
7811 update_echo_area ()
7813 if (!NILP (echo_area_buffer[0]))
7815 Lisp_Object string;
7816 string = Fcurrent_message ();
7817 message3 (string, SBYTES (string),
7818 !NILP (current_buffer->enable_multibyte_characters));
7823 /* Make sure echo area buffers in `echo_buffers' are live.
7824 If they aren't, make new ones. */
7826 static void
7827 ensure_echo_area_buffers ()
7829 int i;
7831 for (i = 0; i < 2; ++i)
7832 if (!BUFFERP (echo_buffer[i])
7833 || NILP (XBUFFER (echo_buffer[i])->name))
7835 char name[30];
7836 Lisp_Object old_buffer;
7837 int j;
7839 old_buffer = echo_buffer[i];
7840 sprintf (name, " *Echo Area %d*", i);
7841 echo_buffer[i] = Fget_buffer_create (build_string (name));
7842 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7844 for (j = 0; j < 2; ++j)
7845 if (EQ (old_buffer, echo_area_buffer[j]))
7846 echo_area_buffer[j] = echo_buffer[i];
7851 /* Call FN with args A1..A4 with either the current or last displayed
7852 echo_area_buffer as current buffer.
7854 WHICH zero means use the current message buffer
7855 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7856 from echo_buffer[] and clear it.
7858 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7859 suitable buffer from echo_buffer[] and clear it.
7861 Value is what FN returns. */
7863 static int
7864 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7865 struct window *w;
7866 int which;
7867 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7868 EMACS_INT a1;
7869 Lisp_Object a2;
7870 EMACS_INT a3, a4;
7872 Lisp_Object buffer;
7873 int this_one, the_other, clear_buffer_p, rc;
7874 int count = SPECPDL_INDEX ();
7876 /* If buffers aren't live, make new ones. */
7877 ensure_echo_area_buffers ();
7879 clear_buffer_p = 0;
7881 if (which == 0)
7882 this_one = 0, the_other = 1;
7883 else if (which > 0)
7884 this_one = 1, the_other = 0;
7886 /* Choose a suitable buffer from echo_buffer[] is we don't
7887 have one. */
7888 if (NILP (echo_area_buffer[this_one]))
7890 echo_area_buffer[this_one]
7891 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7892 ? echo_buffer[the_other]
7893 : echo_buffer[this_one]);
7894 clear_buffer_p = 1;
7897 buffer = echo_area_buffer[this_one];
7899 /* Don't get confused by reusing the buffer used for echoing
7900 for a different purpose. */
7901 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7902 cancel_echoing ();
7904 record_unwind_protect (unwind_with_echo_area_buffer,
7905 with_echo_area_buffer_unwind_data (w));
7907 /* Make the echo area buffer current. Note that for display
7908 purposes, it is not necessary that the displayed window's buffer
7909 == current_buffer, except for text property lookup. So, let's
7910 only set that buffer temporarily here without doing a full
7911 Fset_window_buffer. We must also change w->pointm, though,
7912 because otherwise an assertions in unshow_buffer fails, and Emacs
7913 aborts. */
7914 set_buffer_internal_1 (XBUFFER (buffer));
7915 if (w)
7917 w->buffer = buffer;
7918 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7921 current_buffer->undo_list = Qt;
7922 current_buffer->read_only = Qnil;
7923 specbind (Qinhibit_read_only, Qt);
7924 specbind (Qinhibit_modification_hooks, Qt);
7926 if (clear_buffer_p && Z > BEG)
7927 del_range (BEG, Z);
7929 xassert (BEGV >= BEG);
7930 xassert (ZV <= Z && ZV >= BEGV);
7932 rc = fn (a1, a2, a3, a4);
7934 xassert (BEGV >= BEG);
7935 xassert (ZV <= Z && ZV >= BEGV);
7937 unbind_to (count, Qnil);
7938 return rc;
7942 /* Save state that should be preserved around the call to the function
7943 FN called in with_echo_area_buffer. */
7945 static Lisp_Object
7946 with_echo_area_buffer_unwind_data (w)
7947 struct window *w;
7949 int i = 0;
7950 Lisp_Object vector;
7952 /* Reduce consing by keeping one vector in
7953 Vwith_echo_area_save_vector. */
7954 vector = Vwith_echo_area_save_vector;
7955 Vwith_echo_area_save_vector = Qnil;
7957 if (NILP (vector))
7958 vector = Fmake_vector (make_number (7), Qnil);
7960 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7961 AREF (vector, i) = Vdeactivate_mark, ++i;
7962 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7964 if (w)
7966 XSETWINDOW (AREF (vector, i), w); ++i;
7967 AREF (vector, i) = w->buffer; ++i;
7968 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7969 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7971 else
7973 int end = i + 4;
7974 for (; i < end; ++i)
7975 AREF (vector, i) = Qnil;
7978 xassert (i == ASIZE (vector));
7979 return vector;
7983 /* Restore global state from VECTOR which was created by
7984 with_echo_area_buffer_unwind_data. */
7986 static Lisp_Object
7987 unwind_with_echo_area_buffer (vector)
7988 Lisp_Object vector;
7990 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7991 Vdeactivate_mark = AREF (vector, 1);
7992 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7994 if (WINDOWP (AREF (vector, 3)))
7996 struct window *w;
7997 Lisp_Object buffer, charpos, bytepos;
7999 w = XWINDOW (AREF (vector, 3));
8000 buffer = AREF (vector, 4);
8001 charpos = AREF (vector, 5);
8002 bytepos = AREF (vector, 6);
8004 w->buffer = buffer;
8005 set_marker_both (w->pointm, buffer,
8006 XFASTINT (charpos), XFASTINT (bytepos));
8009 Vwith_echo_area_save_vector = vector;
8010 return Qnil;
8014 /* Set up the echo area for use by print functions. MULTIBYTE_P
8015 non-zero means we will print multibyte. */
8017 void
8018 setup_echo_area_for_printing (multibyte_p)
8019 int multibyte_p;
8021 /* If we can't find an echo area any more, exit. */
8022 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8023 Fkill_emacs (Qnil);
8025 ensure_echo_area_buffers ();
8027 if (!message_buf_print)
8029 /* A message has been output since the last time we printed.
8030 Choose a fresh echo area buffer. */
8031 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8032 echo_area_buffer[0] = echo_buffer[1];
8033 else
8034 echo_area_buffer[0] = echo_buffer[0];
8036 /* Switch to that buffer and clear it. */
8037 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8038 current_buffer->truncate_lines = Qnil;
8040 if (Z > BEG)
8042 int count = SPECPDL_INDEX ();
8043 specbind (Qinhibit_read_only, Qt);
8044 /* Note that undo recording is always disabled. */
8045 del_range (BEG, Z);
8046 unbind_to (count, Qnil);
8048 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8050 /* Set up the buffer for the multibyteness we need. */
8051 if (multibyte_p
8052 != !NILP (current_buffer->enable_multibyte_characters))
8053 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8055 /* Raise the frame containing the echo area. */
8056 if (minibuffer_auto_raise)
8058 struct frame *sf = SELECTED_FRAME ();
8059 Lisp_Object mini_window;
8060 mini_window = FRAME_MINIBUF_WINDOW (sf);
8061 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8064 message_log_maybe_newline ();
8065 message_buf_print = 1;
8067 else
8069 if (NILP (echo_area_buffer[0]))
8071 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8072 echo_area_buffer[0] = echo_buffer[1];
8073 else
8074 echo_area_buffer[0] = echo_buffer[0];
8077 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8079 /* Someone switched buffers between print requests. */
8080 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8081 current_buffer->truncate_lines = Qnil;
8087 /* Display an echo area message in window W. Value is non-zero if W's
8088 height is changed. If display_last_displayed_message_p is
8089 non-zero, display the message that was last displayed, otherwise
8090 display the current message. */
8092 static int
8093 display_echo_area (w)
8094 struct window *w;
8096 int i, no_message_p, window_height_changed_p, count;
8098 /* Temporarily disable garbage collections while displaying the echo
8099 area. This is done because a GC can print a message itself.
8100 That message would modify the echo area buffer's contents while a
8101 redisplay of the buffer is going on, and seriously confuse
8102 redisplay. */
8103 count = inhibit_garbage_collection ();
8105 /* If there is no message, we must call display_echo_area_1
8106 nevertheless because it resizes the window. But we will have to
8107 reset the echo_area_buffer in question to nil at the end because
8108 with_echo_area_buffer will sets it to an empty buffer. */
8109 i = display_last_displayed_message_p ? 1 : 0;
8110 no_message_p = NILP (echo_area_buffer[i]);
8112 window_height_changed_p
8113 = with_echo_area_buffer (w, display_last_displayed_message_p,
8114 display_echo_area_1,
8115 (EMACS_INT) w, Qnil, 0, 0);
8117 if (no_message_p)
8118 echo_area_buffer[i] = Qnil;
8120 unbind_to (count, Qnil);
8121 return window_height_changed_p;
8125 /* Helper for display_echo_area. Display the current buffer which
8126 contains the current echo area message in window W, a mini-window,
8127 a pointer to which is passed in A1. A2..A4 are currently not used.
8128 Change the height of W so that all of the message is displayed.
8129 Value is non-zero if height of W was changed. */
8131 static int
8132 display_echo_area_1 (a1, a2, a3, a4)
8133 EMACS_INT a1;
8134 Lisp_Object a2;
8135 EMACS_INT a3, a4;
8137 struct window *w = (struct window *) a1;
8138 Lisp_Object window;
8139 struct text_pos start;
8140 int window_height_changed_p = 0;
8142 /* Do this before displaying, so that we have a large enough glyph
8143 matrix for the display. If we can't get enough space for the
8144 whole text, display the last N lines. That works by setting w->start. */
8145 window_height_changed_p = resize_mini_window (w, 0);
8147 /* Use the starting position chosen by resize_mini_window. */
8148 SET_TEXT_POS_FROM_MARKER (start, w->start);
8150 /* Display. */
8151 clear_glyph_matrix (w->desired_matrix);
8152 XSETWINDOW (window, w);
8153 try_window (window, start, 0);
8155 return window_height_changed_p;
8159 /* Resize the echo area window to exactly the size needed for the
8160 currently displayed message, if there is one. If a mini-buffer
8161 is active, don't shrink it. */
8163 void
8164 resize_echo_area_exactly ()
8166 if (BUFFERP (echo_area_buffer[0])
8167 && WINDOWP (echo_area_window))
8169 struct window *w = XWINDOW (echo_area_window);
8170 int resized_p;
8171 Lisp_Object resize_exactly;
8173 if (minibuf_level == 0)
8174 resize_exactly = Qt;
8175 else
8176 resize_exactly = Qnil;
8178 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8179 (EMACS_INT) w, resize_exactly, 0, 0);
8180 if (resized_p)
8182 ++windows_or_buffers_changed;
8183 ++update_mode_lines;
8184 redisplay_internal (0);
8190 /* Callback function for with_echo_area_buffer, when used from
8191 resize_echo_area_exactly. A1 contains a pointer to the window to
8192 resize, EXACTLY non-nil means resize the mini-window exactly to the
8193 size of the text displayed. A3 and A4 are not used. Value is what
8194 resize_mini_window returns. */
8196 static int
8197 resize_mini_window_1 (a1, exactly, a3, a4)
8198 EMACS_INT a1;
8199 Lisp_Object exactly;
8200 EMACS_INT a3, a4;
8202 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8206 /* Resize mini-window W to fit the size of its contents. EXACT:P
8207 means size the window exactly to the size needed. Otherwise, it's
8208 only enlarged until W's buffer is empty.
8210 Set W->start to the right place to begin display. If the whole
8211 contents fit, start at the beginning. Otherwise, start so as
8212 to make the end of the contents appear. This is particularly
8213 important for y-or-n-p, but seems desirable generally.
8215 Value is non-zero if the window height has been changed. */
8218 resize_mini_window (w, exact_p)
8219 struct window *w;
8220 int exact_p;
8222 struct frame *f = XFRAME (w->frame);
8223 int window_height_changed_p = 0;
8225 xassert (MINI_WINDOW_P (w));
8227 /* By default, start display at the beginning. */
8228 set_marker_both (w->start, w->buffer,
8229 BUF_BEGV (XBUFFER (w->buffer)),
8230 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8232 /* Don't resize windows while redisplaying a window; it would
8233 confuse redisplay functions when the size of the window they are
8234 displaying changes from under them. Such a resizing can happen,
8235 for instance, when which-func prints a long message while
8236 we are running fontification-functions. We're running these
8237 functions with safe_call which binds inhibit-redisplay to t. */
8238 if (!NILP (Vinhibit_redisplay))
8239 return 0;
8241 /* Nil means don't try to resize. */
8242 if (NILP (Vresize_mini_windows)
8243 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8244 return 0;
8246 if (!FRAME_MINIBUF_ONLY_P (f))
8248 struct it it;
8249 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8250 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8251 int height, max_height;
8252 int unit = FRAME_LINE_HEIGHT (f);
8253 struct text_pos start;
8254 struct buffer *old_current_buffer = NULL;
8256 if (current_buffer != XBUFFER (w->buffer))
8258 old_current_buffer = current_buffer;
8259 set_buffer_internal (XBUFFER (w->buffer));
8262 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8264 /* Compute the max. number of lines specified by the user. */
8265 if (FLOATP (Vmax_mini_window_height))
8266 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8267 else if (INTEGERP (Vmax_mini_window_height))
8268 max_height = XINT (Vmax_mini_window_height);
8269 else
8270 max_height = total_height / 4;
8272 /* Correct that max. height if it's bogus. */
8273 max_height = max (1, max_height);
8274 max_height = min (total_height, max_height);
8276 /* Find out the height of the text in the window. */
8277 if (it.truncate_lines_p)
8278 height = 1;
8279 else
8281 last_height = 0;
8282 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8283 if (it.max_ascent == 0 && it.max_descent == 0)
8284 height = it.current_y + last_height;
8285 else
8286 height = it.current_y + it.max_ascent + it.max_descent;
8287 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8288 height = (height + unit - 1) / unit;
8291 /* Compute a suitable window start. */
8292 if (height > max_height)
8294 height = max_height;
8295 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8296 move_it_vertically_backward (&it, (height - 1) * unit);
8297 start = it.current.pos;
8299 else
8300 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8301 SET_MARKER_FROM_TEXT_POS (w->start, start);
8303 if (EQ (Vresize_mini_windows, Qgrow_only))
8305 /* Let it grow only, until we display an empty message, in which
8306 case the window shrinks again. */
8307 if (height > WINDOW_TOTAL_LINES (w))
8309 int old_height = WINDOW_TOTAL_LINES (w);
8310 freeze_window_starts (f, 1);
8311 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8312 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8314 else if (height < WINDOW_TOTAL_LINES (w)
8315 && (exact_p || BEGV == ZV))
8317 int old_height = WINDOW_TOTAL_LINES (w);
8318 freeze_window_starts (f, 0);
8319 shrink_mini_window (w);
8320 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8323 else
8325 /* Always resize to exact size needed. */
8326 if (height > WINDOW_TOTAL_LINES (w))
8328 int old_height = WINDOW_TOTAL_LINES (w);
8329 freeze_window_starts (f, 1);
8330 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8331 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8333 else if (height < WINDOW_TOTAL_LINES (w))
8335 int old_height = WINDOW_TOTAL_LINES (w);
8336 freeze_window_starts (f, 0);
8337 shrink_mini_window (w);
8339 if (height)
8341 freeze_window_starts (f, 1);
8342 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8345 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8349 if (old_current_buffer)
8350 set_buffer_internal (old_current_buffer);
8353 return window_height_changed_p;
8357 /* Value is the current message, a string, or nil if there is no
8358 current message. */
8360 Lisp_Object
8361 current_message ()
8363 Lisp_Object msg;
8365 if (NILP (echo_area_buffer[0]))
8366 msg = Qnil;
8367 else
8369 with_echo_area_buffer (0, 0, current_message_1,
8370 (EMACS_INT) &msg, Qnil, 0, 0);
8371 if (NILP (msg))
8372 echo_area_buffer[0] = Qnil;
8375 return msg;
8379 static int
8380 current_message_1 (a1, a2, a3, a4)
8381 EMACS_INT a1;
8382 Lisp_Object a2;
8383 EMACS_INT a3, a4;
8385 Lisp_Object *msg = (Lisp_Object *) a1;
8387 if (Z > BEG)
8388 *msg = make_buffer_string (BEG, Z, 1);
8389 else
8390 *msg = Qnil;
8391 return 0;
8395 /* Push the current message on Vmessage_stack for later restauration
8396 by restore_message. Value is non-zero if the current message isn't
8397 empty. This is a relatively infrequent operation, so it's not
8398 worth optimizing. */
8401 push_message ()
8403 Lisp_Object msg;
8404 msg = current_message ();
8405 Vmessage_stack = Fcons (msg, Vmessage_stack);
8406 return STRINGP (msg);
8410 /* Restore message display from the top of Vmessage_stack. */
8412 void
8413 restore_message ()
8415 Lisp_Object msg;
8417 xassert (CONSP (Vmessage_stack));
8418 msg = XCAR (Vmessage_stack);
8419 if (STRINGP (msg))
8420 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8421 else
8422 message3_nolog (msg, 0, 0);
8426 /* Handler for record_unwind_protect calling pop_message. */
8428 Lisp_Object
8429 pop_message_unwind (dummy)
8430 Lisp_Object dummy;
8432 pop_message ();
8433 return Qnil;
8436 /* Pop the top-most entry off Vmessage_stack. */
8438 void
8439 pop_message ()
8441 xassert (CONSP (Vmessage_stack));
8442 Vmessage_stack = XCDR (Vmessage_stack);
8446 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8447 exits. If the stack is not empty, we have a missing pop_message
8448 somewhere. */
8450 void
8451 check_message_stack ()
8453 if (!NILP (Vmessage_stack))
8454 abort ();
8458 /* Truncate to NCHARS what will be displayed in the echo area the next
8459 time we display it---but don't redisplay it now. */
8461 void
8462 truncate_echo_area (nchars)
8463 int nchars;
8465 if (nchars == 0)
8466 echo_area_buffer[0] = Qnil;
8467 /* A null message buffer means that the frame hasn't really been
8468 initialized yet. Error messages get reported properly by
8469 cmd_error, so this must be just an informative message; toss it. */
8470 else if (!noninteractive
8471 && INTERACTIVE
8472 && !NILP (echo_area_buffer[0]))
8474 struct frame *sf = SELECTED_FRAME ();
8475 if (FRAME_MESSAGE_BUF (sf))
8476 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8481 /* Helper function for truncate_echo_area. Truncate the current
8482 message to at most NCHARS characters. */
8484 static int
8485 truncate_message_1 (nchars, a2, a3, a4)
8486 EMACS_INT nchars;
8487 Lisp_Object a2;
8488 EMACS_INT a3, a4;
8490 if (BEG + nchars < Z)
8491 del_range (BEG + nchars, Z);
8492 if (Z == BEG)
8493 echo_area_buffer[0] = Qnil;
8494 return 0;
8498 /* Set the current message to a substring of S or STRING.
8500 If STRING is a Lisp string, set the message to the first NBYTES
8501 bytes from STRING. NBYTES zero means use the whole string. If
8502 STRING is multibyte, the message will be displayed multibyte.
8504 If S is not null, set the message to the first LEN bytes of S. LEN
8505 zero means use the whole string. MULTIBYTE_P non-zero means S is
8506 multibyte. Display the message multibyte in that case.
8508 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8509 to t before calling set_message_1 (which calls insert).
8512 void
8513 set_message (s, string, nbytes, multibyte_p)
8514 const char *s;
8515 Lisp_Object string;
8516 int nbytes, multibyte_p;
8518 message_enable_multibyte
8519 = ((s && multibyte_p)
8520 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8522 with_echo_area_buffer (0, 0, set_message_1,
8523 (EMACS_INT) s, string, nbytes, multibyte_p);
8524 message_buf_print = 0;
8525 help_echo_showing_p = 0;
8529 /* Helper function for set_message. Arguments have the same meaning
8530 as there, with A1 corresponding to S and A2 corresponding to STRING
8531 This function is called with the echo area buffer being
8532 current. */
8534 static int
8535 set_message_1 (a1, a2, nbytes, multibyte_p)
8536 EMACS_INT a1;
8537 Lisp_Object a2;
8538 EMACS_INT nbytes, multibyte_p;
8540 const char *s = (const char *) a1;
8541 Lisp_Object string = a2;
8543 /* Change multibyteness of the echo buffer appropriately. */
8544 if (message_enable_multibyte
8545 != !NILP (current_buffer->enable_multibyte_characters))
8546 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8548 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8550 /* Insert new message at BEG. */
8551 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8552 Ferase_buffer ();
8554 if (STRINGP (string))
8556 int nchars;
8558 if (nbytes == 0)
8559 nbytes = SBYTES (string);
8560 nchars = string_byte_to_char (string, nbytes);
8562 /* This function takes care of single/multibyte conversion. We
8563 just have to ensure that the echo area buffer has the right
8564 setting of enable_multibyte_characters. */
8565 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8567 else if (s)
8569 if (nbytes == 0)
8570 nbytes = strlen (s);
8572 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8574 /* Convert from multi-byte to single-byte. */
8575 int i, c, n;
8576 unsigned char work[1];
8578 /* Convert a multibyte string to single-byte. */
8579 for (i = 0; i < nbytes; i += n)
8581 c = string_char_and_length (s + i, nbytes - i, &n);
8582 work[0] = (SINGLE_BYTE_CHAR_P (c)
8584 : multibyte_char_to_unibyte (c, Qnil));
8585 insert_1_both (work, 1, 1, 1, 0, 0);
8588 else if (!multibyte_p
8589 && !NILP (current_buffer->enable_multibyte_characters))
8591 /* Convert from single-byte to multi-byte. */
8592 int i, c, n;
8593 const unsigned char *msg = (const unsigned char *) s;
8594 unsigned char str[MAX_MULTIBYTE_LENGTH];
8596 /* Convert a single-byte string to multibyte. */
8597 for (i = 0; i < nbytes; i++)
8599 c = unibyte_char_to_multibyte (msg[i]);
8600 n = CHAR_STRING (c, str);
8601 insert_1_both (str, 1, n, 1, 0, 0);
8604 else
8605 insert_1 (s, nbytes, 1, 0, 0);
8608 return 0;
8612 /* Clear messages. CURRENT_P non-zero means clear the current
8613 message. LAST_DISPLAYED_P non-zero means clear the message
8614 last displayed. */
8616 void
8617 clear_message (current_p, last_displayed_p)
8618 int current_p, last_displayed_p;
8620 if (current_p)
8622 echo_area_buffer[0] = Qnil;
8623 message_cleared_p = 1;
8626 if (last_displayed_p)
8627 echo_area_buffer[1] = Qnil;
8629 message_buf_print = 0;
8632 /* Clear garbaged frames.
8634 This function is used where the old redisplay called
8635 redraw_garbaged_frames which in turn called redraw_frame which in
8636 turn called clear_frame. The call to clear_frame was a source of
8637 flickering. I believe a clear_frame is not necessary. It should
8638 suffice in the new redisplay to invalidate all current matrices,
8639 and ensure a complete redisplay of all windows. */
8641 static void
8642 clear_garbaged_frames ()
8644 if (frame_garbaged)
8646 Lisp_Object tail, frame;
8647 int changed_count = 0;
8649 FOR_EACH_FRAME (tail, frame)
8651 struct frame *f = XFRAME (frame);
8653 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8655 if (f->resized_p)
8657 Fredraw_frame (frame);
8658 f->force_flush_display_p = 1;
8660 clear_current_matrices (f);
8661 changed_count++;
8662 f->garbaged = 0;
8663 f->resized_p = 0;
8667 frame_garbaged = 0;
8668 if (changed_count)
8669 ++windows_or_buffers_changed;
8674 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8675 is non-zero update selected_frame. Value is non-zero if the
8676 mini-windows height has been changed. */
8678 static int
8679 echo_area_display (update_frame_p)
8680 int update_frame_p;
8682 Lisp_Object mini_window;
8683 struct window *w;
8684 struct frame *f;
8685 int window_height_changed_p = 0;
8686 struct frame *sf = SELECTED_FRAME ();
8688 mini_window = FRAME_MINIBUF_WINDOW (sf);
8689 w = XWINDOW (mini_window);
8690 f = XFRAME (WINDOW_FRAME (w));
8692 /* Don't display if frame is invisible or not yet initialized. */
8693 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8694 return 0;
8696 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8697 #ifndef MAC_OS8
8698 #ifdef HAVE_WINDOW_SYSTEM
8699 /* When Emacs starts, selected_frame may be a visible terminal
8700 frame, even if we run under a window system. If we let this
8701 through, a message would be displayed on the terminal. */
8702 if (EQ (selected_frame, Vterminal_frame)
8703 && !NILP (Vwindow_system))
8704 return 0;
8705 #endif /* HAVE_WINDOW_SYSTEM */
8706 #endif
8708 /* Redraw garbaged frames. */
8709 if (frame_garbaged)
8710 clear_garbaged_frames ();
8712 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8714 echo_area_window = mini_window;
8715 window_height_changed_p = display_echo_area (w);
8716 w->must_be_updated_p = 1;
8718 /* Update the display, unless called from redisplay_internal.
8719 Also don't update the screen during redisplay itself. The
8720 update will happen at the end of redisplay, and an update
8721 here could cause confusion. */
8722 if (update_frame_p && !redisplaying_p)
8724 int n = 0;
8726 /* If the display update has been interrupted by pending
8727 input, update mode lines in the frame. Due to the
8728 pending input, it might have been that redisplay hasn't
8729 been called, so that mode lines above the echo area are
8730 garbaged. This looks odd, so we prevent it here. */
8731 if (!display_completed)
8732 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8734 if (window_height_changed_p
8735 /* Don't do this if Emacs is shutting down. Redisplay
8736 needs to run hooks. */
8737 && !NILP (Vrun_hooks))
8739 /* Must update other windows. Likewise as in other
8740 cases, don't let this update be interrupted by
8741 pending input. */
8742 int count = SPECPDL_INDEX ();
8743 specbind (Qredisplay_dont_pause, Qt);
8744 windows_or_buffers_changed = 1;
8745 redisplay_internal (0);
8746 unbind_to (count, Qnil);
8748 else if (FRAME_WINDOW_P (f) && n == 0)
8750 /* Window configuration is the same as before.
8751 Can do with a display update of the echo area,
8752 unless we displayed some mode lines. */
8753 update_single_window (w, 1);
8754 rif->flush_display (f);
8756 else
8757 update_frame (f, 1, 1);
8759 /* If cursor is in the echo area, make sure that the next
8760 redisplay displays the minibuffer, so that the cursor will
8761 be replaced with what the minibuffer wants. */
8762 if (cursor_in_echo_area)
8763 ++windows_or_buffers_changed;
8766 else if (!EQ (mini_window, selected_window))
8767 windows_or_buffers_changed++;
8769 /* The current message is now also the last one displayed. */
8770 echo_area_buffer[1] = echo_area_buffer[0];
8772 /* Prevent redisplay optimization in redisplay_internal by resetting
8773 this_line_start_pos. This is done because the mini-buffer now
8774 displays the message instead of its buffer text. */
8775 if (EQ (mini_window, selected_window))
8776 CHARPOS (this_line_start_pos) = 0;
8778 return window_height_changed_p;
8783 /***********************************************************************
8784 Mode Lines and Frame Titles
8785 ***********************************************************************/
8787 /* A buffer for constructing non-propertized mode-line strings and
8788 frame titles in it; allocated from the heap in init_xdisp and
8789 resized as needed in store_mode_line_noprop_char. */
8791 static char *mode_line_noprop_buf;
8793 /* The buffer's end, and a current output position in it. */
8795 static char *mode_line_noprop_buf_end;
8796 static char *mode_line_noprop_ptr;
8798 #define MODE_LINE_NOPROP_LEN(start) \
8799 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8801 static enum {
8802 MODE_LINE_DISPLAY = 0,
8803 MODE_LINE_TITLE,
8804 MODE_LINE_NOPROP,
8805 MODE_LINE_STRING
8806 } mode_line_target;
8808 /* Alist that caches the results of :propertize.
8809 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8810 static Lisp_Object mode_line_proptrans_alist;
8812 /* List of strings making up the mode-line. */
8813 static Lisp_Object mode_line_string_list;
8815 /* Base face property when building propertized mode line string. */
8816 static Lisp_Object mode_line_string_face;
8817 static Lisp_Object mode_line_string_face_prop;
8820 /* Unwind data for mode line strings */
8822 static Lisp_Object Vmode_line_unwind_vector;
8824 static Lisp_Object
8825 format_mode_line_unwind_data (obuf, save_proptrans)
8826 struct buffer *obuf;
8828 Lisp_Object vector;
8830 /* Reduce consing by keeping one vector in
8831 Vwith_echo_area_save_vector. */
8832 vector = Vmode_line_unwind_vector;
8833 Vmode_line_unwind_vector = Qnil;
8835 if (NILP (vector))
8836 vector = Fmake_vector (make_number (7), Qnil);
8838 AREF (vector, 0) = make_number (mode_line_target);
8839 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8840 AREF (vector, 2) = mode_line_string_list;
8841 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8842 AREF (vector, 4) = mode_line_string_face;
8843 AREF (vector, 5) = mode_line_string_face_prop;
8845 if (obuf)
8846 XSETBUFFER (AREF (vector, 6), obuf);
8847 else
8848 AREF (vector, 6) = Qnil;
8850 return vector;
8853 static Lisp_Object
8854 unwind_format_mode_line (vector)
8855 Lisp_Object vector;
8857 mode_line_target = XINT (AREF (vector, 0));
8858 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8859 mode_line_string_list = AREF (vector, 2);
8860 if (! EQ (AREF (vector, 3), Qt))
8861 mode_line_proptrans_alist = AREF (vector, 3);
8862 mode_line_string_face = AREF (vector, 4);
8863 mode_line_string_face_prop = AREF (vector, 5);
8865 if (!NILP (AREF (vector, 6)))
8867 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8868 AREF (vector, 6) = Qnil;
8871 Vmode_line_unwind_vector = vector;
8872 return Qnil;
8876 /* Store a single character C for the frame title in mode_line_noprop_buf.
8877 Re-allocate mode_line_noprop_buf if necessary. */
8879 static void
8880 #ifdef PROTOTYPES
8881 store_mode_line_noprop_char (char c)
8882 #else
8883 store_mode_line_noprop_char (c)
8884 char c;
8885 #endif
8887 /* If output position has reached the end of the allocated buffer,
8888 double the buffer's size. */
8889 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8891 int len = MODE_LINE_NOPROP_LEN (0);
8892 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8893 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8894 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8895 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8898 *mode_line_noprop_ptr++ = c;
8902 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8903 mode_line_noprop_ptr. STR is the string to store. Do not copy
8904 characters that yield more columns than PRECISION; PRECISION <= 0
8905 means copy the whole string. Pad with spaces until FIELD_WIDTH
8906 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8907 pad. Called from display_mode_element when it is used to build a
8908 frame title. */
8910 static int
8911 store_mode_line_noprop (str, field_width, precision)
8912 const unsigned char *str;
8913 int field_width, precision;
8915 int n = 0;
8916 int dummy, nbytes;
8918 /* Copy at most PRECISION chars from STR. */
8919 nbytes = strlen (str);
8920 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8921 while (nbytes--)
8922 store_mode_line_noprop_char (*str++);
8924 /* Fill up with spaces until FIELD_WIDTH reached. */
8925 while (field_width > 0
8926 && n < field_width)
8928 store_mode_line_noprop_char (' ');
8929 ++n;
8932 return n;
8935 /***********************************************************************
8936 Frame Titles
8937 ***********************************************************************/
8939 #ifdef HAVE_WINDOW_SYSTEM
8941 /* Set the title of FRAME, if it has changed. The title format is
8942 Vicon_title_format if FRAME is iconified, otherwise it is
8943 frame_title_format. */
8945 static void
8946 x_consider_frame_title (frame)
8947 Lisp_Object frame;
8949 struct frame *f = XFRAME (frame);
8951 if (FRAME_WINDOW_P (f)
8952 || FRAME_MINIBUF_ONLY_P (f)
8953 || f->explicit_name)
8955 /* Do we have more than one visible frame on this X display? */
8956 Lisp_Object tail;
8957 Lisp_Object fmt;
8958 int title_start;
8959 char *title;
8960 int len;
8961 struct it it;
8962 int count = SPECPDL_INDEX ();
8964 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8966 Lisp_Object other_frame = XCAR (tail);
8967 struct frame *tf = XFRAME (other_frame);
8969 if (tf != f
8970 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8971 && !FRAME_MINIBUF_ONLY_P (tf)
8972 && !EQ (other_frame, tip_frame)
8973 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8974 break;
8977 /* Set global variable indicating that multiple frames exist. */
8978 multiple_frames = CONSP (tail);
8980 /* Switch to the buffer of selected window of the frame. Set up
8981 mode_line_target so that display_mode_element will output into
8982 mode_line_noprop_buf; then display the title. */
8983 record_unwind_protect (unwind_format_mode_line,
8984 format_mode_line_unwind_data (current_buffer, 0));
8986 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8987 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8989 mode_line_target = MODE_LINE_TITLE;
8990 title_start = MODE_LINE_NOPROP_LEN (0);
8991 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8992 NULL, DEFAULT_FACE_ID);
8993 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8994 len = MODE_LINE_NOPROP_LEN (title_start);
8995 title = mode_line_noprop_buf + title_start;
8996 unbind_to (count, Qnil);
8998 /* Set the title only if it's changed. This avoids consing in
8999 the common case where it hasn't. (If it turns out that we've
9000 already wasted too much time by walking through the list with
9001 display_mode_element, then we might need to optimize at a
9002 higher level than this.) */
9003 if (! STRINGP (f->name)
9004 || SBYTES (f->name) != len
9005 || bcmp (title, SDATA (f->name), len) != 0)
9006 x_implicitly_set_name (f, make_string (title, len), Qnil);
9010 #endif /* not HAVE_WINDOW_SYSTEM */
9015 /***********************************************************************
9016 Menu Bars
9017 ***********************************************************************/
9020 /* Prepare for redisplay by updating menu-bar item lists when
9021 appropriate. This can call eval. */
9023 void
9024 prepare_menu_bars ()
9026 int all_windows;
9027 struct gcpro gcpro1, gcpro2;
9028 struct frame *f;
9029 Lisp_Object tooltip_frame;
9031 #ifdef HAVE_WINDOW_SYSTEM
9032 tooltip_frame = tip_frame;
9033 #else
9034 tooltip_frame = Qnil;
9035 #endif
9037 /* Update all frame titles based on their buffer names, etc. We do
9038 this before the menu bars so that the buffer-menu will show the
9039 up-to-date frame titles. */
9040 #ifdef HAVE_WINDOW_SYSTEM
9041 if (windows_or_buffers_changed || update_mode_lines)
9043 Lisp_Object tail, frame;
9045 FOR_EACH_FRAME (tail, frame)
9047 f = XFRAME (frame);
9048 if (!EQ (frame, tooltip_frame)
9049 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9050 x_consider_frame_title (frame);
9053 #endif /* HAVE_WINDOW_SYSTEM */
9055 /* Update the menu bar item lists, if appropriate. This has to be
9056 done before any actual redisplay or generation of display lines. */
9057 all_windows = (update_mode_lines
9058 || buffer_shared > 1
9059 || windows_or_buffers_changed);
9060 if (all_windows)
9062 Lisp_Object tail, frame;
9063 int count = SPECPDL_INDEX ();
9064 /* 1 means that update_menu_bar has run its hooks
9065 so any further calls to update_menu_bar shouldn't do so again. */
9066 int menu_bar_hooks_run = 0;
9068 record_unwind_save_match_data ();
9070 FOR_EACH_FRAME (tail, frame)
9072 f = XFRAME (frame);
9074 /* Ignore tooltip frame. */
9075 if (EQ (frame, tooltip_frame))
9076 continue;
9078 /* If a window on this frame changed size, report that to
9079 the user and clear the size-change flag. */
9080 if (FRAME_WINDOW_SIZES_CHANGED (f))
9082 Lisp_Object functions;
9084 /* Clear flag first in case we get an error below. */
9085 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9086 functions = Vwindow_size_change_functions;
9087 GCPRO2 (tail, functions);
9089 while (CONSP (functions))
9091 call1 (XCAR (functions), frame);
9092 functions = XCDR (functions);
9094 UNGCPRO;
9097 GCPRO1 (tail);
9098 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9099 #ifdef HAVE_WINDOW_SYSTEM
9100 update_tool_bar (f, 0);
9101 #ifdef MAC_OS
9102 mac_update_title_bar (f, 0);
9103 #endif
9104 #endif
9105 UNGCPRO;
9108 unbind_to (count, Qnil);
9110 else
9112 struct frame *sf = SELECTED_FRAME ();
9113 update_menu_bar (sf, 1, 0);
9114 #ifdef HAVE_WINDOW_SYSTEM
9115 update_tool_bar (sf, 1);
9116 #ifdef MAC_OS
9117 mac_update_title_bar (sf, 1);
9118 #endif
9119 #endif
9122 /* Motif needs this. See comment in xmenu.c. Turn it off when
9123 pending_menu_activation is not defined. */
9124 #ifdef USE_X_TOOLKIT
9125 pending_menu_activation = 0;
9126 #endif
9130 /* Update the menu bar item list for frame F. This has to be done
9131 before we start to fill in any display lines, because it can call
9132 eval.
9134 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9136 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9137 already ran the menu bar hooks for this redisplay, so there
9138 is no need to run them again. The return value is the
9139 updated value of this flag, to pass to the next call. */
9141 static int
9142 update_menu_bar (f, save_match_data, hooks_run)
9143 struct frame *f;
9144 int save_match_data;
9145 int hooks_run;
9147 Lisp_Object window;
9148 register struct window *w;
9150 /* If called recursively during a menu update, do nothing. This can
9151 happen when, for instance, an activate-menubar-hook causes a
9152 redisplay. */
9153 if (inhibit_menubar_update)
9154 return hooks_run;
9156 window = FRAME_SELECTED_WINDOW (f);
9157 w = XWINDOW (window);
9159 #if 0 /* The if statement below this if statement used to include the
9160 condition !NILP (w->update_mode_line), rather than using
9161 update_mode_lines directly, and this if statement may have
9162 been added to make that condition work. Now the if
9163 statement below matches its comment, this isn't needed. */
9164 if (update_mode_lines)
9165 w->update_mode_line = Qt;
9166 #endif
9168 if (FRAME_WINDOW_P (f)
9170 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9171 || defined (USE_GTK)
9172 FRAME_EXTERNAL_MENU_BAR (f)
9173 #else
9174 FRAME_MENU_BAR_LINES (f) > 0
9175 #endif
9176 : FRAME_MENU_BAR_LINES (f) > 0)
9178 /* If the user has switched buffers or windows, we need to
9179 recompute to reflect the new bindings. But we'll
9180 recompute when update_mode_lines is set too; that means
9181 that people can use force-mode-line-update to request
9182 that the menu bar be recomputed. The adverse effect on
9183 the rest of the redisplay algorithm is about the same as
9184 windows_or_buffers_changed anyway. */
9185 if (windows_or_buffers_changed
9186 /* This used to test w->update_mode_line, but we believe
9187 there is no need to recompute the menu in that case. */
9188 || update_mode_lines
9189 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9190 < BUF_MODIFF (XBUFFER (w->buffer)))
9191 != !NILP (w->last_had_star))
9192 || ((!NILP (Vtransient_mark_mode)
9193 && !NILP (XBUFFER (w->buffer)->mark_active))
9194 != !NILP (w->region_showing)))
9196 struct buffer *prev = current_buffer;
9197 int count = SPECPDL_INDEX ();
9199 specbind (Qinhibit_menubar_update, Qt);
9201 set_buffer_internal_1 (XBUFFER (w->buffer));
9202 if (save_match_data)
9203 record_unwind_save_match_data ();
9204 if (NILP (Voverriding_local_map_menu_flag))
9206 specbind (Qoverriding_terminal_local_map, Qnil);
9207 specbind (Qoverriding_local_map, Qnil);
9210 if (!hooks_run)
9212 /* Run the Lucid hook. */
9213 safe_run_hooks (Qactivate_menubar_hook);
9215 /* If it has changed current-menubar from previous value,
9216 really recompute the menu-bar from the value. */
9217 if (! NILP (Vlucid_menu_bar_dirty_flag))
9218 call0 (Qrecompute_lucid_menubar);
9220 safe_run_hooks (Qmenu_bar_update_hook);
9222 hooks_run = 1;
9225 XSETFRAME (Vmenu_updating_frame, f);
9226 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9228 /* Redisplay the menu bar in case we changed it. */
9229 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9230 || defined (USE_GTK)
9231 if (FRAME_WINDOW_P (f))
9233 #ifdef MAC_OS
9234 /* All frames on Mac OS share the same menubar. So only
9235 the selected frame should be allowed to set it. */
9236 if (f == SELECTED_FRAME ())
9237 #endif
9238 set_frame_menubar (f, 0, 0);
9240 else
9241 /* On a terminal screen, the menu bar is an ordinary screen
9242 line, and this makes it get updated. */
9243 w->update_mode_line = Qt;
9244 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9245 /* In the non-toolkit version, the menu bar is an ordinary screen
9246 line, and this makes it get updated. */
9247 w->update_mode_line = Qt;
9248 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9250 unbind_to (count, Qnil);
9251 set_buffer_internal_1 (prev);
9255 return hooks_run;
9260 /***********************************************************************
9261 Output Cursor
9262 ***********************************************************************/
9264 #ifdef HAVE_WINDOW_SYSTEM
9266 /* EXPORT:
9267 Nominal cursor position -- where to draw output.
9268 HPOS and VPOS are window relative glyph matrix coordinates.
9269 X and Y are window relative pixel coordinates. */
9271 struct cursor_pos output_cursor;
9274 /* EXPORT:
9275 Set the global variable output_cursor to CURSOR. All cursor
9276 positions are relative to updated_window. */
9278 void
9279 set_output_cursor (cursor)
9280 struct cursor_pos *cursor;
9282 output_cursor.hpos = cursor->hpos;
9283 output_cursor.vpos = cursor->vpos;
9284 output_cursor.x = cursor->x;
9285 output_cursor.y = cursor->y;
9289 /* EXPORT for RIF:
9290 Set a nominal cursor position.
9292 HPOS and VPOS are column/row positions in a window glyph matrix. X
9293 and Y are window text area relative pixel positions.
9295 If this is done during an update, updated_window will contain the
9296 window that is being updated and the position is the future output
9297 cursor position for that window. If updated_window is null, use
9298 selected_window and display the cursor at the given position. */
9300 void
9301 x_cursor_to (vpos, hpos, y, x)
9302 int vpos, hpos, y, x;
9304 struct window *w;
9306 /* If updated_window is not set, work on selected_window. */
9307 if (updated_window)
9308 w = updated_window;
9309 else
9310 w = XWINDOW (selected_window);
9312 /* Set the output cursor. */
9313 output_cursor.hpos = hpos;
9314 output_cursor.vpos = vpos;
9315 output_cursor.x = x;
9316 output_cursor.y = y;
9318 /* If not called as part of an update, really display the cursor.
9319 This will also set the cursor position of W. */
9320 if (updated_window == NULL)
9322 BLOCK_INPUT;
9323 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9324 if (rif->flush_display_optional)
9325 rif->flush_display_optional (SELECTED_FRAME ());
9326 UNBLOCK_INPUT;
9330 #endif /* HAVE_WINDOW_SYSTEM */
9333 /***********************************************************************
9334 Tool-bars
9335 ***********************************************************************/
9337 #ifdef HAVE_WINDOW_SYSTEM
9339 /* Where the mouse was last time we reported a mouse event. */
9341 FRAME_PTR last_mouse_frame;
9343 /* Tool-bar item index of the item on which a mouse button was pressed
9344 or -1. */
9346 int last_tool_bar_item;
9349 /* Update the tool-bar item list for frame F. This has to be done
9350 before we start to fill in any display lines. Called from
9351 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9352 and restore it here. */
9354 static void
9355 update_tool_bar (f, save_match_data)
9356 struct frame *f;
9357 int save_match_data;
9359 #ifdef USE_GTK
9360 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9361 #else
9362 int do_update = WINDOWP (f->tool_bar_window)
9363 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9364 #endif
9366 if (do_update)
9368 Lisp_Object window;
9369 struct window *w;
9371 window = FRAME_SELECTED_WINDOW (f);
9372 w = XWINDOW (window);
9374 /* If the user has switched buffers or windows, we need to
9375 recompute to reflect the new bindings. But we'll
9376 recompute when update_mode_lines is set too; that means
9377 that people can use force-mode-line-update to request
9378 that the menu bar be recomputed. The adverse effect on
9379 the rest of the redisplay algorithm is about the same as
9380 windows_or_buffers_changed anyway. */
9381 if (windows_or_buffers_changed
9382 || !NILP (w->update_mode_line)
9383 || update_mode_lines
9384 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9385 < BUF_MODIFF (XBUFFER (w->buffer)))
9386 != !NILP (w->last_had_star))
9387 || ((!NILP (Vtransient_mark_mode)
9388 && !NILP (XBUFFER (w->buffer)->mark_active))
9389 != !NILP (w->region_showing)))
9391 struct buffer *prev = current_buffer;
9392 int count = SPECPDL_INDEX ();
9393 Lisp_Object new_tool_bar;
9394 int new_n_tool_bar;
9395 struct gcpro gcpro1;
9397 /* Set current_buffer to the buffer of the selected
9398 window of the frame, so that we get the right local
9399 keymaps. */
9400 set_buffer_internal_1 (XBUFFER (w->buffer));
9402 /* Save match data, if we must. */
9403 if (save_match_data)
9404 record_unwind_save_match_data ();
9406 /* Make sure that we don't accidentally use bogus keymaps. */
9407 if (NILP (Voverriding_local_map_menu_flag))
9409 specbind (Qoverriding_terminal_local_map, Qnil);
9410 specbind (Qoverriding_local_map, Qnil);
9413 GCPRO1 (new_tool_bar);
9415 /* Build desired tool-bar items from keymaps. */
9416 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9417 &new_n_tool_bar);
9419 /* Redisplay the tool-bar if we changed it. */
9420 if (new_n_tool_bar != f->n_tool_bar_items
9421 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9423 /* Redisplay that happens asynchronously due to an expose event
9424 may access f->tool_bar_items. Make sure we update both
9425 variables within BLOCK_INPUT so no such event interrupts. */
9426 BLOCK_INPUT;
9427 f->tool_bar_items = new_tool_bar;
9428 f->n_tool_bar_items = new_n_tool_bar;
9429 w->update_mode_line = Qt;
9430 UNBLOCK_INPUT;
9433 UNGCPRO;
9435 unbind_to (count, Qnil);
9436 set_buffer_internal_1 (prev);
9442 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9443 F's desired tool-bar contents. F->tool_bar_items must have
9444 been set up previously by calling prepare_menu_bars. */
9446 static void
9447 build_desired_tool_bar_string (f)
9448 struct frame *f;
9450 int i, size, size_needed;
9451 struct gcpro gcpro1, gcpro2, gcpro3;
9452 Lisp_Object image, plist, props;
9454 image = plist = props = Qnil;
9455 GCPRO3 (image, plist, props);
9457 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9458 Otherwise, make a new string. */
9460 /* The size of the string we might be able to reuse. */
9461 size = (STRINGP (f->desired_tool_bar_string)
9462 ? SCHARS (f->desired_tool_bar_string)
9463 : 0);
9465 /* We need one space in the string for each image. */
9466 size_needed = f->n_tool_bar_items;
9468 /* Reuse f->desired_tool_bar_string, if possible. */
9469 if (size < size_needed || NILP (f->desired_tool_bar_string))
9470 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9471 make_number (' '));
9472 else
9474 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9475 Fremove_text_properties (make_number (0), make_number (size),
9476 props, f->desired_tool_bar_string);
9479 /* Put a `display' property on the string for the images to display,
9480 put a `menu_item' property on tool-bar items with a value that
9481 is the index of the item in F's tool-bar item vector. */
9482 for (i = 0; i < f->n_tool_bar_items; ++i)
9484 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9486 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9487 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9488 int hmargin, vmargin, relief, idx, end;
9489 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9491 /* If image is a vector, choose the image according to the
9492 button state. */
9493 image = PROP (TOOL_BAR_ITEM_IMAGES);
9494 if (VECTORP (image))
9496 if (enabled_p)
9497 idx = (selected_p
9498 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9499 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9500 else
9501 idx = (selected_p
9502 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9503 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9505 xassert (ASIZE (image) >= idx);
9506 image = AREF (image, idx);
9508 else
9509 idx = -1;
9511 /* Ignore invalid image specifications. */
9512 if (!valid_image_p (image))
9513 continue;
9515 /* Display the tool-bar button pressed, or depressed. */
9516 plist = Fcopy_sequence (XCDR (image));
9518 /* Compute margin and relief to draw. */
9519 relief = (tool_bar_button_relief >= 0
9520 ? tool_bar_button_relief
9521 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9522 hmargin = vmargin = relief;
9524 if (INTEGERP (Vtool_bar_button_margin)
9525 && XINT (Vtool_bar_button_margin) > 0)
9527 hmargin += XFASTINT (Vtool_bar_button_margin);
9528 vmargin += XFASTINT (Vtool_bar_button_margin);
9530 else if (CONSP (Vtool_bar_button_margin))
9532 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9533 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9534 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9536 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9537 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9538 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9541 if (auto_raise_tool_bar_buttons_p)
9543 /* Add a `:relief' property to the image spec if the item is
9544 selected. */
9545 if (selected_p)
9547 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9548 hmargin -= relief;
9549 vmargin -= relief;
9552 else
9554 /* If image is selected, display it pressed, i.e. with a
9555 negative relief. If it's not selected, display it with a
9556 raised relief. */
9557 plist = Fplist_put (plist, QCrelief,
9558 (selected_p
9559 ? make_number (-relief)
9560 : make_number (relief)));
9561 hmargin -= relief;
9562 vmargin -= relief;
9565 /* Put a margin around the image. */
9566 if (hmargin || vmargin)
9568 if (hmargin == vmargin)
9569 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9570 else
9571 plist = Fplist_put (plist, QCmargin,
9572 Fcons (make_number (hmargin),
9573 make_number (vmargin)));
9576 /* If button is not enabled, and we don't have special images
9577 for the disabled state, make the image appear disabled by
9578 applying an appropriate algorithm to it. */
9579 if (!enabled_p && idx < 0)
9580 plist = Fplist_put (plist, QCconversion, Qdisabled);
9582 /* Put a `display' text property on the string for the image to
9583 display. Put a `menu-item' property on the string that gives
9584 the start of this item's properties in the tool-bar items
9585 vector. */
9586 image = Fcons (Qimage, plist);
9587 props = list4 (Qdisplay, image,
9588 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9590 /* Let the last image hide all remaining spaces in the tool bar
9591 string. The string can be longer than needed when we reuse a
9592 previous string. */
9593 if (i + 1 == f->n_tool_bar_items)
9594 end = SCHARS (f->desired_tool_bar_string);
9595 else
9596 end = i + 1;
9597 Fadd_text_properties (make_number (i), make_number (end),
9598 props, f->desired_tool_bar_string);
9599 #undef PROP
9602 UNGCPRO;
9606 /* Display one line of the tool-bar of frame IT->f.
9608 HEIGHT specifies the desired height of the tool-bar line.
9609 If the actual height of the glyph row is less than HEIGHT, the
9610 row's height is increased to HEIGHT, and the icons are centered
9611 vertically in the new height.
9613 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9614 count a final empty row in case the tool-bar width exactly matches
9615 the window width.
9618 static void
9619 display_tool_bar_line (it, height)
9620 struct it *it;
9621 int height;
9623 struct glyph_row *row = it->glyph_row;
9624 int max_x = it->last_visible_x;
9625 struct glyph *last;
9627 prepare_desired_row (row);
9628 row->y = it->current_y;
9630 /* Note that this isn't made use of if the face hasn't a box,
9631 so there's no need to check the face here. */
9632 it->start_of_box_run_p = 1;
9634 while (it->current_x < max_x)
9636 int x, n_glyphs_before, i, nglyphs;
9637 struct it it_before;
9639 /* Get the next display element. */
9640 if (!get_next_display_element (it))
9642 /* Don't count empty row if we are counting needed tool-bar lines. */
9643 if (height < 0 && !it->hpos)
9644 return;
9645 break;
9648 /* Produce glyphs. */
9649 n_glyphs_before = row->used[TEXT_AREA];
9650 it_before = *it;
9652 PRODUCE_GLYPHS (it);
9654 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9655 i = 0;
9656 x = it_before.current_x;
9657 while (i < nglyphs)
9659 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9661 if (x + glyph->pixel_width > max_x)
9663 /* Glyph doesn't fit on line. Backtrack. */
9664 row->used[TEXT_AREA] = n_glyphs_before;
9665 *it = it_before;
9666 /* If this is the only glyph on this line, it will never fit on the
9667 toolbar, so skip it. But ensure there is at least one glyph,
9668 so we don't accidentally disable the tool-bar. */
9669 if (n_glyphs_before == 0
9670 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9671 break;
9672 goto out;
9675 ++it->hpos;
9676 x += glyph->pixel_width;
9677 ++i;
9680 /* Stop at line ends. */
9681 if (ITERATOR_AT_END_OF_LINE_P (it))
9682 break;
9684 set_iterator_to_next (it, 1);
9687 out:;
9689 row->displays_text_p = row->used[TEXT_AREA] != 0;
9690 /* Use default face for the border below the tool bar. */
9691 if (!row->displays_text_p)
9692 it->face_id = DEFAULT_FACE_ID;
9693 extend_face_to_end_of_line (it);
9694 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9695 last->right_box_line_p = 1;
9696 if (last == row->glyphs[TEXT_AREA])
9697 last->left_box_line_p = 1;
9699 /* Make line the desired height and center it vertically. */
9700 if ((height -= it->max_ascent + it->max_descent) > 0)
9702 /* Don't add more than one line height. */
9703 height %= FRAME_LINE_HEIGHT (it->f);
9704 it->max_ascent += height / 2;
9705 it->max_descent += (height + 1) / 2;
9708 compute_line_metrics (it);
9710 /* If line is empty, make it occupy the rest of the tool-bar. */
9711 if (!row->displays_text_p)
9713 row->height = row->phys_height = it->last_visible_y - row->y;
9714 row->ascent = row->phys_ascent = 0;
9715 row->extra_line_spacing = 0;
9718 row->full_width_p = 1;
9719 row->continued_p = 0;
9720 row->truncated_on_left_p = 0;
9721 row->truncated_on_right_p = 0;
9723 it->current_x = it->hpos = 0;
9724 it->current_y += row->height;
9725 ++it->vpos;
9726 ++it->glyph_row;
9730 /* Max tool-bar height. */
9732 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9733 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9735 /* Value is the number of screen lines needed to make all tool-bar
9736 items of frame F visible. The number of actual rows needed is
9737 returned in *N_ROWS if non-NULL. */
9739 static int
9740 tool_bar_lines_needed (f, n_rows)
9741 struct frame *f;
9742 int *n_rows;
9744 struct window *w = XWINDOW (f->tool_bar_window);
9745 struct it it;
9746 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9747 the desired matrix, so use (unused) mode-line row as temporary row to
9748 avoid destroying the first tool-bar row. */
9749 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9751 /* Initialize an iterator for iteration over
9752 F->desired_tool_bar_string in the tool-bar window of frame F. */
9753 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9754 it.first_visible_x = 0;
9755 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9756 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9758 while (!ITERATOR_AT_END_P (&it))
9760 clear_glyph_row (temp_row);
9761 it.glyph_row = temp_row;
9762 display_tool_bar_line (&it, -1);
9764 clear_glyph_row (temp_row);
9766 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9767 if (n_rows)
9768 *n_rows = it.vpos > 0 ? it.vpos : -1;
9770 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9774 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9775 0, 1, 0,
9776 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9777 (frame)
9778 Lisp_Object frame;
9780 struct frame *f;
9781 struct window *w;
9782 int nlines = 0;
9784 if (NILP (frame))
9785 frame = selected_frame;
9786 else
9787 CHECK_FRAME (frame);
9788 f = XFRAME (frame);
9790 if (WINDOWP (f->tool_bar_window)
9791 || (w = XWINDOW (f->tool_bar_window),
9792 WINDOW_TOTAL_LINES (w) > 0))
9794 update_tool_bar (f, 1);
9795 if (f->n_tool_bar_items)
9797 build_desired_tool_bar_string (f);
9798 nlines = tool_bar_lines_needed (f, NULL);
9802 return make_number (nlines);
9806 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9807 height should be changed. */
9809 static int
9810 redisplay_tool_bar (f)
9811 struct frame *f;
9813 struct window *w;
9814 struct it it;
9815 struct glyph_row *row;
9816 int change_height_p = 0;
9818 #ifdef USE_GTK
9819 if (FRAME_EXTERNAL_TOOL_BAR (f))
9820 update_frame_tool_bar (f);
9821 return 0;
9822 #endif
9824 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9825 do anything. This means you must start with tool-bar-lines
9826 non-zero to get the auto-sizing effect. Or in other words, you
9827 can turn off tool-bars by specifying tool-bar-lines zero. */
9828 if (!WINDOWP (f->tool_bar_window)
9829 || (w = XWINDOW (f->tool_bar_window),
9830 WINDOW_TOTAL_LINES (w) == 0))
9831 return 0;
9833 /* Set up an iterator for the tool-bar window. */
9834 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9835 it.first_visible_x = 0;
9836 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9837 row = it.glyph_row;
9839 /* Build a string that represents the contents of the tool-bar. */
9840 build_desired_tool_bar_string (f);
9841 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9843 if (f->n_tool_bar_rows == 0)
9845 int nlines;
9847 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9848 nlines != WINDOW_TOTAL_LINES (w)))
9850 extern Lisp_Object Qtool_bar_lines;
9851 Lisp_Object frame;
9852 int old_height = WINDOW_TOTAL_LINES (w);
9854 XSETFRAME (frame, f);
9855 Fmodify_frame_parameters (frame,
9856 Fcons (Fcons (Qtool_bar_lines,
9857 make_number (nlines)),
9858 Qnil));
9859 if (WINDOW_TOTAL_LINES (w) != old_height)
9861 clear_glyph_matrix (w->desired_matrix);
9862 fonts_changed_p = 1;
9863 return 1;
9868 /* Display as many lines as needed to display all tool-bar items. */
9870 if (f->n_tool_bar_rows > 0)
9872 int border, rows, height, extra;
9874 if (INTEGERP (Vtool_bar_border))
9875 border = XINT (Vtool_bar_border);
9876 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9877 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9878 else if (EQ (Vtool_bar_border, Qborder_width))
9879 border = f->border_width;
9880 else
9881 border = 0;
9882 if (border < 0)
9883 border = 0;
9885 rows = f->n_tool_bar_rows;
9886 height = max (1, (it.last_visible_y - border) / rows);
9887 extra = it.last_visible_y - border - height * rows;
9889 while (it.current_y < it.last_visible_y)
9891 int h = 0;
9892 if (extra > 0 && rows-- > 0)
9894 h = (extra + rows - 1) / rows;
9895 extra -= h;
9897 display_tool_bar_line (&it, height + h);
9900 else
9902 while (it.current_y < it.last_visible_y)
9903 display_tool_bar_line (&it, 0);
9906 /* It doesn't make much sense to try scrolling in the tool-bar
9907 window, so don't do it. */
9908 w->desired_matrix->no_scrolling_p = 1;
9909 w->must_be_updated_p = 1;
9911 if (auto_resize_tool_bars_p)
9913 int nlines, nrows;
9914 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9916 /* If we couldn't display everything, change the tool-bar's
9917 height if there is room for more. */
9918 if (IT_STRING_CHARPOS (it) < it.end_charpos
9919 && it.current_y < max_tool_bar_height)
9920 change_height_p = 1;
9922 row = it.glyph_row - 1;
9924 /* If there are blank lines at the end, except for a partially
9925 visible blank line at the end that is smaller than
9926 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9927 if (!row->displays_text_p
9928 && row->height >= FRAME_LINE_HEIGHT (f))
9929 change_height_p = 1;
9931 /* If row displays tool-bar items, but is partially visible,
9932 change the tool-bar's height. */
9933 if (row->displays_text_p
9934 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9935 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9936 change_height_p = 1;
9938 /* Resize windows as needed by changing the `tool-bar-lines'
9939 frame parameter. */
9940 if (change_height_p
9941 && (nlines = tool_bar_lines_needed (f, &nrows),
9942 nlines != WINDOW_TOTAL_LINES (w)))
9944 extern Lisp_Object Qtool_bar_lines;
9945 Lisp_Object frame;
9946 int old_height = WINDOW_TOTAL_LINES (w);
9948 XSETFRAME (frame, f);
9949 Fmodify_frame_parameters (frame,
9950 Fcons (Fcons (Qtool_bar_lines,
9951 make_number (nlines)),
9952 Qnil));
9953 if (WINDOW_TOTAL_LINES (w) != old_height)
9955 clear_glyph_matrix (w->desired_matrix);
9956 f->n_tool_bar_rows = nrows;
9957 fonts_changed_p = 1;
9962 return change_height_p;
9966 /* Get information about the tool-bar item which is displayed in GLYPH
9967 on frame F. Return in *PROP_IDX the index where tool-bar item
9968 properties start in F->tool_bar_items. Value is zero if
9969 GLYPH doesn't display a tool-bar item. */
9971 static int
9972 tool_bar_item_info (f, glyph, prop_idx)
9973 struct frame *f;
9974 struct glyph *glyph;
9975 int *prop_idx;
9977 Lisp_Object prop;
9978 int success_p;
9979 int charpos;
9981 /* This function can be called asynchronously, which means we must
9982 exclude any possibility that Fget_text_property signals an
9983 error. */
9984 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9985 charpos = max (0, charpos);
9987 /* Get the text property `menu-item' at pos. The value of that
9988 property is the start index of this item's properties in
9989 F->tool_bar_items. */
9990 prop = Fget_text_property (make_number (charpos),
9991 Qmenu_item, f->current_tool_bar_string);
9992 if (INTEGERP (prop))
9994 *prop_idx = XINT (prop);
9995 success_p = 1;
9997 else
9998 success_p = 0;
10000 return success_p;
10004 /* Get information about the tool-bar item at position X/Y on frame F.
10005 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10006 the current matrix of the tool-bar window of F, or NULL if not
10007 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10008 item in F->tool_bar_items. Value is
10010 -1 if X/Y is not on a tool-bar item
10011 0 if X/Y is on the same item that was highlighted before.
10012 1 otherwise. */
10014 static int
10015 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10016 struct frame *f;
10017 int x, y;
10018 struct glyph **glyph;
10019 int *hpos, *vpos, *prop_idx;
10021 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10022 struct window *w = XWINDOW (f->tool_bar_window);
10023 int area;
10025 /* Find the glyph under X/Y. */
10026 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10027 if (*glyph == NULL)
10028 return -1;
10030 /* Get the start of this tool-bar item's properties in
10031 f->tool_bar_items. */
10032 if (!tool_bar_item_info (f, *glyph, prop_idx))
10033 return -1;
10035 /* Is mouse on the highlighted item? */
10036 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10037 && *vpos >= dpyinfo->mouse_face_beg_row
10038 && *vpos <= dpyinfo->mouse_face_end_row
10039 && (*vpos > dpyinfo->mouse_face_beg_row
10040 || *hpos >= dpyinfo->mouse_face_beg_col)
10041 && (*vpos < dpyinfo->mouse_face_end_row
10042 || *hpos < dpyinfo->mouse_face_end_col
10043 || dpyinfo->mouse_face_past_end))
10044 return 0;
10046 return 1;
10050 /* EXPORT:
10051 Handle mouse button event on the tool-bar of frame F, at
10052 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10053 0 for button release. MODIFIERS is event modifiers for button
10054 release. */
10056 void
10057 handle_tool_bar_click (f, x, y, down_p, modifiers)
10058 struct frame *f;
10059 int x, y, down_p;
10060 unsigned int modifiers;
10062 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10063 struct window *w = XWINDOW (f->tool_bar_window);
10064 int hpos, vpos, prop_idx;
10065 struct glyph *glyph;
10066 Lisp_Object enabled_p;
10068 /* If not on the highlighted tool-bar item, return. */
10069 frame_to_window_pixel_xy (w, &x, &y);
10070 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10071 return;
10073 /* If item is disabled, do nothing. */
10074 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10075 if (NILP (enabled_p))
10076 return;
10078 if (down_p)
10080 /* Show item in pressed state. */
10081 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10082 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10083 last_tool_bar_item = prop_idx;
10085 else
10087 Lisp_Object key, frame;
10088 struct input_event event;
10089 EVENT_INIT (event);
10091 /* Show item in released state. */
10092 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10093 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10095 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10097 XSETFRAME (frame, f);
10098 event.kind = TOOL_BAR_EVENT;
10099 event.frame_or_window = frame;
10100 event.arg = frame;
10101 kbd_buffer_store_event (&event);
10103 event.kind = TOOL_BAR_EVENT;
10104 event.frame_or_window = frame;
10105 event.arg = key;
10106 event.modifiers = modifiers;
10107 kbd_buffer_store_event (&event);
10108 last_tool_bar_item = -1;
10113 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10114 tool-bar window-relative coordinates X/Y. Called from
10115 note_mouse_highlight. */
10117 static void
10118 note_tool_bar_highlight (f, x, y)
10119 struct frame *f;
10120 int x, y;
10122 Lisp_Object window = f->tool_bar_window;
10123 struct window *w = XWINDOW (window);
10124 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10125 int hpos, vpos;
10126 struct glyph *glyph;
10127 struct glyph_row *row;
10128 int i;
10129 Lisp_Object enabled_p;
10130 int prop_idx;
10131 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10132 int mouse_down_p, rc;
10134 /* Function note_mouse_highlight is called with negative x(y
10135 values when mouse moves outside of the frame. */
10136 if (x <= 0 || y <= 0)
10138 clear_mouse_face (dpyinfo);
10139 return;
10142 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10143 if (rc < 0)
10145 /* Not on tool-bar item. */
10146 clear_mouse_face (dpyinfo);
10147 return;
10149 else if (rc == 0)
10150 /* On same tool-bar item as before. */
10151 goto set_help_echo;
10153 clear_mouse_face (dpyinfo);
10155 /* Mouse is down, but on different tool-bar item? */
10156 mouse_down_p = (dpyinfo->grabbed
10157 && f == last_mouse_frame
10158 && FRAME_LIVE_P (f));
10159 if (mouse_down_p
10160 && last_tool_bar_item != prop_idx)
10161 return;
10163 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10164 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10166 /* If tool-bar item is not enabled, don't highlight it. */
10167 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10168 if (!NILP (enabled_p))
10170 /* Compute the x-position of the glyph. In front and past the
10171 image is a space. We include this in the highlighted area. */
10172 row = MATRIX_ROW (w->current_matrix, vpos);
10173 for (i = x = 0; i < hpos; ++i)
10174 x += row->glyphs[TEXT_AREA][i].pixel_width;
10176 /* Record this as the current active region. */
10177 dpyinfo->mouse_face_beg_col = hpos;
10178 dpyinfo->mouse_face_beg_row = vpos;
10179 dpyinfo->mouse_face_beg_x = x;
10180 dpyinfo->mouse_face_beg_y = row->y;
10181 dpyinfo->mouse_face_past_end = 0;
10183 dpyinfo->mouse_face_end_col = hpos + 1;
10184 dpyinfo->mouse_face_end_row = vpos;
10185 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10186 dpyinfo->mouse_face_end_y = row->y;
10187 dpyinfo->mouse_face_window = window;
10188 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10190 /* Display it as active. */
10191 show_mouse_face (dpyinfo, draw);
10192 dpyinfo->mouse_face_image_state = draw;
10195 set_help_echo:
10197 /* Set help_echo_string to a help string to display for this tool-bar item.
10198 XTread_socket does the rest. */
10199 help_echo_object = help_echo_window = Qnil;
10200 help_echo_pos = -1;
10201 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10202 if (NILP (help_echo_string))
10203 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10206 #endif /* HAVE_WINDOW_SYSTEM */
10210 /************************************************************************
10211 Horizontal scrolling
10212 ************************************************************************/
10214 static int hscroll_window_tree P_ ((Lisp_Object));
10215 static int hscroll_windows P_ ((Lisp_Object));
10217 /* For all leaf windows in the window tree rooted at WINDOW, set their
10218 hscroll value so that PT is (i) visible in the window, and (ii) so
10219 that it is not within a certain margin at the window's left and
10220 right border. Value is non-zero if any window's hscroll has been
10221 changed. */
10223 static int
10224 hscroll_window_tree (window)
10225 Lisp_Object window;
10227 int hscrolled_p = 0;
10228 int hscroll_relative_p = FLOATP (Vhscroll_step);
10229 int hscroll_step_abs = 0;
10230 double hscroll_step_rel = 0;
10232 if (hscroll_relative_p)
10234 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10235 if (hscroll_step_rel < 0)
10237 hscroll_relative_p = 0;
10238 hscroll_step_abs = 0;
10241 else if (INTEGERP (Vhscroll_step))
10243 hscroll_step_abs = XINT (Vhscroll_step);
10244 if (hscroll_step_abs < 0)
10245 hscroll_step_abs = 0;
10247 else
10248 hscroll_step_abs = 0;
10250 while (WINDOWP (window))
10252 struct window *w = XWINDOW (window);
10254 if (WINDOWP (w->hchild))
10255 hscrolled_p |= hscroll_window_tree (w->hchild);
10256 else if (WINDOWP (w->vchild))
10257 hscrolled_p |= hscroll_window_tree (w->vchild);
10258 else if (w->cursor.vpos >= 0)
10260 int h_margin;
10261 int text_area_width;
10262 struct glyph_row *current_cursor_row
10263 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10264 struct glyph_row *desired_cursor_row
10265 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10266 struct glyph_row *cursor_row
10267 = (desired_cursor_row->enabled_p
10268 ? desired_cursor_row
10269 : current_cursor_row);
10271 text_area_width = window_box_width (w, TEXT_AREA);
10273 /* Scroll when cursor is inside this scroll margin. */
10274 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10276 if ((XFASTINT (w->hscroll)
10277 && w->cursor.x <= h_margin)
10278 || (cursor_row->enabled_p
10279 && cursor_row->truncated_on_right_p
10280 && (w->cursor.x >= text_area_width - h_margin)))
10282 struct it it;
10283 int hscroll;
10284 struct buffer *saved_current_buffer;
10285 int pt;
10286 int wanted_x;
10288 /* Find point in a display of infinite width. */
10289 saved_current_buffer = current_buffer;
10290 current_buffer = XBUFFER (w->buffer);
10292 if (w == XWINDOW (selected_window))
10293 pt = BUF_PT (current_buffer);
10294 else
10296 pt = marker_position (w->pointm);
10297 pt = max (BEGV, pt);
10298 pt = min (ZV, pt);
10301 /* Move iterator to pt starting at cursor_row->start in
10302 a line with infinite width. */
10303 init_to_row_start (&it, w, cursor_row);
10304 it.last_visible_x = INFINITY;
10305 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10306 current_buffer = saved_current_buffer;
10308 /* Position cursor in window. */
10309 if (!hscroll_relative_p && hscroll_step_abs == 0)
10310 hscroll = max (0, (it.current_x
10311 - (ITERATOR_AT_END_OF_LINE_P (&it)
10312 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10313 : (text_area_width / 2))))
10314 / FRAME_COLUMN_WIDTH (it.f);
10315 else if (w->cursor.x >= text_area_width - h_margin)
10317 if (hscroll_relative_p)
10318 wanted_x = text_area_width * (1 - hscroll_step_rel)
10319 - h_margin;
10320 else
10321 wanted_x = text_area_width
10322 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10323 - h_margin;
10324 hscroll
10325 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10327 else
10329 if (hscroll_relative_p)
10330 wanted_x = text_area_width * hscroll_step_rel
10331 + h_margin;
10332 else
10333 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10334 + h_margin;
10335 hscroll
10336 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10338 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10340 /* Don't call Fset_window_hscroll if value hasn't
10341 changed because it will prevent redisplay
10342 optimizations. */
10343 if (XFASTINT (w->hscroll) != hscroll)
10345 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10346 w->hscroll = make_number (hscroll);
10347 hscrolled_p = 1;
10352 window = w->next;
10355 /* Value is non-zero if hscroll of any leaf window has been changed. */
10356 return hscrolled_p;
10360 /* Set hscroll so that cursor is visible and not inside horizontal
10361 scroll margins for all windows in the tree rooted at WINDOW. See
10362 also hscroll_window_tree above. Value is non-zero if any window's
10363 hscroll has been changed. If it has, desired matrices on the frame
10364 of WINDOW are cleared. */
10366 static int
10367 hscroll_windows (window)
10368 Lisp_Object window;
10370 int hscrolled_p;
10372 if (automatic_hscrolling_p)
10374 hscrolled_p = hscroll_window_tree (window);
10375 if (hscrolled_p)
10376 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10378 else
10379 hscrolled_p = 0;
10380 return hscrolled_p;
10385 /************************************************************************
10386 Redisplay
10387 ************************************************************************/
10389 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10390 to a non-zero value. This is sometimes handy to have in a debugger
10391 session. */
10393 #if GLYPH_DEBUG
10395 /* First and last unchanged row for try_window_id. */
10397 int debug_first_unchanged_at_end_vpos;
10398 int debug_last_unchanged_at_beg_vpos;
10400 /* Delta vpos and y. */
10402 int debug_dvpos, debug_dy;
10404 /* Delta in characters and bytes for try_window_id. */
10406 int debug_delta, debug_delta_bytes;
10408 /* Values of window_end_pos and window_end_vpos at the end of
10409 try_window_id. */
10411 EMACS_INT debug_end_pos, debug_end_vpos;
10413 /* Append a string to W->desired_matrix->method. FMT is a printf
10414 format string. A1...A9 are a supplement for a variable-length
10415 argument list. If trace_redisplay_p is non-zero also printf the
10416 resulting string to stderr. */
10418 static void
10419 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10420 struct window *w;
10421 char *fmt;
10422 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10424 char buffer[512];
10425 char *method = w->desired_matrix->method;
10426 int len = strlen (method);
10427 int size = sizeof w->desired_matrix->method;
10428 int remaining = size - len - 1;
10430 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10431 if (len && remaining)
10433 method[len] = '|';
10434 --remaining, ++len;
10437 strncpy (method + len, buffer, remaining);
10439 if (trace_redisplay_p)
10440 fprintf (stderr, "%p (%s): %s\n",
10442 ((BUFFERP (w->buffer)
10443 && STRINGP (XBUFFER (w->buffer)->name))
10444 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10445 : "no buffer"),
10446 buffer);
10449 #endif /* GLYPH_DEBUG */
10452 /* Value is non-zero if all changes in window W, which displays
10453 current_buffer, are in the text between START and END. START is a
10454 buffer position, END is given as a distance from Z. Used in
10455 redisplay_internal for display optimization. */
10457 static INLINE int
10458 text_outside_line_unchanged_p (w, start, end)
10459 struct window *w;
10460 int start, end;
10462 int unchanged_p = 1;
10464 /* If text or overlays have changed, see where. */
10465 if (XFASTINT (w->last_modified) < MODIFF
10466 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10468 /* Gap in the line? */
10469 if (GPT < start || Z - GPT < end)
10470 unchanged_p = 0;
10472 /* Changes start in front of the line, or end after it? */
10473 if (unchanged_p
10474 && (BEG_UNCHANGED < start - 1
10475 || END_UNCHANGED < end))
10476 unchanged_p = 0;
10478 /* If selective display, can't optimize if changes start at the
10479 beginning of the line. */
10480 if (unchanged_p
10481 && INTEGERP (current_buffer->selective_display)
10482 && XINT (current_buffer->selective_display) > 0
10483 && (BEG_UNCHANGED < start || GPT <= start))
10484 unchanged_p = 0;
10486 /* If there are overlays at the start or end of the line, these
10487 may have overlay strings with newlines in them. A change at
10488 START, for instance, may actually concern the display of such
10489 overlay strings as well, and they are displayed on different
10490 lines. So, quickly rule out this case. (For the future, it
10491 might be desirable to implement something more telling than
10492 just BEG/END_UNCHANGED.) */
10493 if (unchanged_p)
10495 if (BEG + BEG_UNCHANGED == start
10496 && overlay_touches_p (start))
10497 unchanged_p = 0;
10498 if (END_UNCHANGED == end
10499 && overlay_touches_p (Z - end))
10500 unchanged_p = 0;
10504 return unchanged_p;
10508 /* Do a frame update, taking possible shortcuts into account. This is
10509 the main external entry point for redisplay.
10511 If the last redisplay displayed an echo area message and that message
10512 is no longer requested, we clear the echo area or bring back the
10513 mini-buffer if that is in use. */
10515 void
10516 redisplay ()
10518 redisplay_internal (0);
10522 static Lisp_Object
10523 overlay_arrow_string_or_property (var)
10524 Lisp_Object var;
10526 Lisp_Object val;
10528 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10529 return val;
10531 return Voverlay_arrow_string;
10534 /* Return 1 if there are any overlay-arrows in current_buffer. */
10535 static int
10536 overlay_arrow_in_current_buffer_p ()
10538 Lisp_Object vlist;
10540 for (vlist = Voverlay_arrow_variable_list;
10541 CONSP (vlist);
10542 vlist = XCDR (vlist))
10544 Lisp_Object var = XCAR (vlist);
10545 Lisp_Object val;
10547 if (!SYMBOLP (var))
10548 continue;
10549 val = find_symbol_value (var);
10550 if (MARKERP (val)
10551 && current_buffer == XMARKER (val)->buffer)
10552 return 1;
10554 return 0;
10558 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10559 has changed. */
10561 static int
10562 overlay_arrows_changed_p ()
10564 Lisp_Object vlist;
10566 for (vlist = Voverlay_arrow_variable_list;
10567 CONSP (vlist);
10568 vlist = XCDR (vlist))
10570 Lisp_Object var = XCAR (vlist);
10571 Lisp_Object val, pstr;
10573 if (!SYMBOLP (var))
10574 continue;
10575 val = find_symbol_value (var);
10576 if (!MARKERP (val))
10577 continue;
10578 if (! EQ (COERCE_MARKER (val),
10579 Fget (var, Qlast_arrow_position))
10580 || ! (pstr = overlay_arrow_string_or_property (var),
10581 EQ (pstr, Fget (var, Qlast_arrow_string))))
10582 return 1;
10584 return 0;
10587 /* Mark overlay arrows to be updated on next redisplay. */
10589 static void
10590 update_overlay_arrows (up_to_date)
10591 int up_to_date;
10593 Lisp_Object vlist;
10595 for (vlist = Voverlay_arrow_variable_list;
10596 CONSP (vlist);
10597 vlist = XCDR (vlist))
10599 Lisp_Object var = XCAR (vlist);
10601 if (!SYMBOLP (var))
10602 continue;
10604 if (up_to_date > 0)
10606 Lisp_Object val = find_symbol_value (var);
10607 Fput (var, Qlast_arrow_position,
10608 COERCE_MARKER (val));
10609 Fput (var, Qlast_arrow_string,
10610 overlay_arrow_string_or_property (var));
10612 else if (up_to_date < 0
10613 || !NILP (Fget (var, Qlast_arrow_position)))
10615 Fput (var, Qlast_arrow_position, Qt);
10616 Fput (var, Qlast_arrow_string, Qt);
10622 /* Return overlay arrow string to display at row.
10623 Return integer (bitmap number) for arrow bitmap in left fringe.
10624 Return nil if no overlay arrow. */
10626 static Lisp_Object
10627 overlay_arrow_at_row (it, row)
10628 struct it *it;
10629 struct glyph_row *row;
10631 Lisp_Object vlist;
10633 for (vlist = Voverlay_arrow_variable_list;
10634 CONSP (vlist);
10635 vlist = XCDR (vlist))
10637 Lisp_Object var = XCAR (vlist);
10638 Lisp_Object val;
10640 if (!SYMBOLP (var))
10641 continue;
10643 val = find_symbol_value (var);
10645 if (MARKERP (val)
10646 && current_buffer == XMARKER (val)->buffer
10647 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10649 if (FRAME_WINDOW_P (it->f)
10650 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10652 #ifdef HAVE_WINDOW_SYSTEM
10653 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10655 int fringe_bitmap;
10656 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10657 return make_number (fringe_bitmap);
10659 #endif
10660 return make_number (-1); /* Use default arrow bitmap */
10662 return overlay_arrow_string_or_property (var);
10666 return Qnil;
10669 /* Return 1 if point moved out of or into a composition. Otherwise
10670 return 0. PREV_BUF and PREV_PT are the last point buffer and
10671 position. BUF and PT are the current point buffer and position. */
10674 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10675 struct buffer *prev_buf, *buf;
10676 int prev_pt, pt;
10678 int start, end;
10679 Lisp_Object prop;
10680 Lisp_Object buffer;
10682 XSETBUFFER (buffer, buf);
10683 /* Check a composition at the last point if point moved within the
10684 same buffer. */
10685 if (prev_buf == buf)
10687 if (prev_pt == pt)
10688 /* Point didn't move. */
10689 return 0;
10691 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10692 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10693 && COMPOSITION_VALID_P (start, end, prop)
10694 && start < prev_pt && end > prev_pt)
10695 /* The last point was within the composition. Return 1 iff
10696 point moved out of the composition. */
10697 return (pt <= start || pt >= end);
10700 /* Check a composition at the current point. */
10701 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10702 && find_composition (pt, -1, &start, &end, &prop, buffer)
10703 && COMPOSITION_VALID_P (start, end, prop)
10704 && start < pt && end > pt);
10708 /* Reconsider the setting of B->clip_changed which is displayed
10709 in window W. */
10711 static INLINE void
10712 reconsider_clip_changes (w, b)
10713 struct window *w;
10714 struct buffer *b;
10716 if (b->clip_changed
10717 && !NILP (w->window_end_valid)
10718 && w->current_matrix->buffer == b
10719 && w->current_matrix->zv == BUF_ZV (b)
10720 && w->current_matrix->begv == BUF_BEGV (b))
10721 b->clip_changed = 0;
10723 /* If display wasn't paused, and W is not a tool bar window, see if
10724 point has been moved into or out of a composition. In that case,
10725 we set b->clip_changed to 1 to force updating the screen. If
10726 b->clip_changed has already been set to 1, we can skip this
10727 check. */
10728 if (!b->clip_changed
10729 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10731 int pt;
10733 if (w == XWINDOW (selected_window))
10734 pt = BUF_PT (current_buffer);
10735 else
10736 pt = marker_position (w->pointm);
10738 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10739 || pt != XINT (w->last_point))
10740 && check_point_in_composition (w->current_matrix->buffer,
10741 XINT (w->last_point),
10742 XBUFFER (w->buffer), pt))
10743 b->clip_changed = 1;
10748 /* Select FRAME to forward the values of frame-local variables into C
10749 variables so that the redisplay routines can access those values
10750 directly. */
10752 static void
10753 select_frame_for_redisplay (frame)
10754 Lisp_Object frame;
10756 Lisp_Object tail, sym, val;
10757 Lisp_Object old = selected_frame;
10759 selected_frame = frame;
10761 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10762 if (CONSP (XCAR (tail))
10763 && (sym = XCAR (XCAR (tail)),
10764 SYMBOLP (sym))
10765 && (sym = indirect_variable (sym),
10766 val = SYMBOL_VALUE (sym),
10767 (BUFFER_LOCAL_VALUEP (val)
10768 || SOME_BUFFER_LOCAL_VALUEP (val)))
10769 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10770 /* Use find_symbol_value rather than Fsymbol_value
10771 to avoid an error if it is void. */
10772 find_symbol_value (sym);
10774 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10775 if (CONSP (XCAR (tail))
10776 && (sym = XCAR (XCAR (tail)),
10777 SYMBOLP (sym))
10778 && (sym = indirect_variable (sym),
10779 val = SYMBOL_VALUE (sym),
10780 (BUFFER_LOCAL_VALUEP (val)
10781 || SOME_BUFFER_LOCAL_VALUEP (val)))
10782 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10783 find_symbol_value (sym);
10787 #define STOP_POLLING \
10788 do { if (! polling_stopped_here) stop_polling (); \
10789 polling_stopped_here = 1; } while (0)
10791 #define RESUME_POLLING \
10792 do { if (polling_stopped_here) start_polling (); \
10793 polling_stopped_here = 0; } while (0)
10796 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10797 response to any user action; therefore, we should preserve the echo
10798 area. (Actually, our caller does that job.) Perhaps in the future
10799 avoid recentering windows if it is not necessary; currently that
10800 causes some problems. */
10802 static void
10803 redisplay_internal (preserve_echo_area)
10804 int preserve_echo_area;
10806 struct window *w = XWINDOW (selected_window);
10807 struct frame *f;
10808 int pause;
10809 int must_finish = 0;
10810 struct text_pos tlbufpos, tlendpos;
10811 int number_of_visible_frames;
10812 int count;
10813 struct frame *sf;
10814 int polling_stopped_here = 0;
10816 /* Non-zero means redisplay has to consider all windows on all
10817 frames. Zero means, only selected_window is considered. */
10818 int consider_all_windows_p;
10820 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10822 /* No redisplay if running in batch mode or frame is not yet fully
10823 initialized, or redisplay is explicitly turned off by setting
10824 Vinhibit_redisplay. */
10825 if (noninteractive
10826 || !NILP (Vinhibit_redisplay))
10827 return;
10829 /* Don't examine these until after testing Vinhibit_redisplay.
10830 When Emacs is shutting down, perhaps because its connection to
10831 X has dropped, we should not look at them at all. */
10832 f = XFRAME (w->frame);
10833 sf = SELECTED_FRAME ();
10835 if (!f->glyphs_initialized_p)
10836 return;
10838 /* The flag redisplay_performed_directly_p is set by
10839 direct_output_for_insert when it already did the whole screen
10840 update necessary. */
10841 if (redisplay_performed_directly_p)
10843 redisplay_performed_directly_p = 0;
10844 if (!hscroll_windows (selected_window))
10845 return;
10848 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10849 if (popup_activated ())
10850 return;
10851 #endif
10853 /* I don't think this happens but let's be paranoid. */
10854 if (redisplaying_p)
10855 return;
10857 /* Record a function that resets redisplaying_p to its old value
10858 when we leave this function. */
10859 count = SPECPDL_INDEX ();
10860 record_unwind_protect (unwind_redisplay,
10861 Fcons (make_number (redisplaying_p), selected_frame));
10862 ++redisplaying_p;
10863 specbind (Qinhibit_free_realized_faces, Qnil);
10866 Lisp_Object tail, frame;
10868 FOR_EACH_FRAME (tail, frame)
10870 struct frame *f = XFRAME (frame);
10871 f->already_hscrolled_p = 0;
10875 retry:
10876 pause = 0;
10877 reconsider_clip_changes (w, current_buffer);
10878 last_escape_glyph_frame = NULL;
10879 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10881 /* If new fonts have been loaded that make a glyph matrix adjustment
10882 necessary, do it. */
10883 if (fonts_changed_p)
10885 adjust_glyphs (NULL);
10886 ++windows_or_buffers_changed;
10887 fonts_changed_p = 0;
10890 /* If face_change_count is non-zero, init_iterator will free all
10891 realized faces, which includes the faces referenced from current
10892 matrices. So, we can't reuse current matrices in this case. */
10893 if (face_change_count)
10894 ++windows_or_buffers_changed;
10896 if (! FRAME_WINDOW_P (sf)
10897 && previous_terminal_frame != sf)
10899 /* Since frames on an ASCII terminal share the same display
10900 area, displaying a different frame means redisplay the whole
10901 thing. */
10902 windows_or_buffers_changed++;
10903 SET_FRAME_GARBAGED (sf);
10904 XSETFRAME (Vterminal_frame, sf);
10906 previous_terminal_frame = sf;
10908 /* Set the visible flags for all frames. Do this before checking
10909 for resized or garbaged frames; they want to know if their frames
10910 are visible. See the comment in frame.h for
10911 FRAME_SAMPLE_VISIBILITY. */
10913 Lisp_Object tail, frame;
10915 number_of_visible_frames = 0;
10917 FOR_EACH_FRAME (tail, frame)
10919 struct frame *f = XFRAME (frame);
10921 FRAME_SAMPLE_VISIBILITY (f);
10922 if (FRAME_VISIBLE_P (f))
10923 ++number_of_visible_frames;
10924 clear_desired_matrices (f);
10928 /* Notice any pending interrupt request to change frame size. */
10929 do_pending_window_change (1);
10931 /* Clear frames marked as garbaged. */
10932 if (frame_garbaged)
10933 clear_garbaged_frames ();
10935 /* Build menubar and tool-bar items. */
10936 if (NILP (Vmemory_full))
10937 prepare_menu_bars ();
10939 if (windows_or_buffers_changed)
10940 update_mode_lines++;
10942 /* Detect case that we need to write or remove a star in the mode line. */
10943 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10945 w->update_mode_line = Qt;
10946 if (buffer_shared > 1)
10947 update_mode_lines++;
10950 /* If %c is in the mode line, update it if needed. */
10951 if (!NILP (w->column_number_displayed)
10952 /* This alternative quickly identifies a common case
10953 where no change is needed. */
10954 && !(PT == XFASTINT (w->last_point)
10955 && XFASTINT (w->last_modified) >= MODIFF
10956 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10957 && (XFASTINT (w->column_number_displayed)
10958 != (int) current_column ())) /* iftc */
10959 w->update_mode_line = Qt;
10961 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10963 /* The variable buffer_shared is set in redisplay_window and
10964 indicates that we redisplay a buffer in different windows. See
10965 there. */
10966 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10967 || cursor_type_changed);
10969 /* If specs for an arrow have changed, do thorough redisplay
10970 to ensure we remove any arrow that should no longer exist. */
10971 if (overlay_arrows_changed_p ())
10972 consider_all_windows_p = windows_or_buffers_changed = 1;
10974 /* Normally the message* functions will have already displayed and
10975 updated the echo area, but the frame may have been trashed, or
10976 the update may have been preempted, so display the echo area
10977 again here. Checking message_cleared_p captures the case that
10978 the echo area should be cleared. */
10979 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10980 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10981 || (message_cleared_p
10982 && minibuf_level == 0
10983 /* If the mini-window is currently selected, this means the
10984 echo-area doesn't show through. */
10985 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10987 int window_height_changed_p = echo_area_display (0);
10988 must_finish = 1;
10990 /* If we don't display the current message, don't clear the
10991 message_cleared_p flag, because, if we did, we wouldn't clear
10992 the echo area in the next redisplay which doesn't preserve
10993 the echo area. */
10994 if (!display_last_displayed_message_p)
10995 message_cleared_p = 0;
10997 if (fonts_changed_p)
10998 goto retry;
10999 else if (window_height_changed_p)
11001 consider_all_windows_p = 1;
11002 ++update_mode_lines;
11003 ++windows_or_buffers_changed;
11005 /* If window configuration was changed, frames may have been
11006 marked garbaged. Clear them or we will experience
11007 surprises wrt scrolling. */
11008 if (frame_garbaged)
11009 clear_garbaged_frames ();
11012 else if (EQ (selected_window, minibuf_window)
11013 && (current_buffer->clip_changed
11014 || XFASTINT (w->last_modified) < MODIFF
11015 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11016 && resize_mini_window (w, 0))
11018 /* Resized active mini-window to fit the size of what it is
11019 showing if its contents might have changed. */
11020 must_finish = 1;
11021 consider_all_windows_p = 1;
11022 ++windows_or_buffers_changed;
11023 ++update_mode_lines;
11025 /* If window configuration was changed, frames may have been
11026 marked garbaged. Clear them or we will experience
11027 surprises wrt scrolling. */
11028 if (frame_garbaged)
11029 clear_garbaged_frames ();
11033 /* If showing the region, and mark has changed, we must redisplay
11034 the whole window. The assignment to this_line_start_pos prevents
11035 the optimization directly below this if-statement. */
11036 if (((!NILP (Vtransient_mark_mode)
11037 && !NILP (XBUFFER (w->buffer)->mark_active))
11038 != !NILP (w->region_showing))
11039 || (!NILP (w->region_showing)
11040 && !EQ (w->region_showing,
11041 Fmarker_position (XBUFFER (w->buffer)->mark))))
11042 CHARPOS (this_line_start_pos) = 0;
11044 /* Optimize the case that only the line containing the cursor in the
11045 selected window has changed. Variables starting with this_ are
11046 set in display_line and record information about the line
11047 containing the cursor. */
11048 tlbufpos = this_line_start_pos;
11049 tlendpos = this_line_end_pos;
11050 if (!consider_all_windows_p
11051 && CHARPOS (tlbufpos) > 0
11052 && NILP (w->update_mode_line)
11053 && !current_buffer->clip_changed
11054 && !current_buffer->prevent_redisplay_optimizations_p
11055 && FRAME_VISIBLE_P (XFRAME (w->frame))
11056 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11057 /* Make sure recorded data applies to current buffer, etc. */
11058 && this_line_buffer == current_buffer
11059 && current_buffer == XBUFFER (w->buffer)
11060 && NILP (w->force_start)
11061 && NILP (w->optional_new_start)
11062 /* Point must be on the line that we have info recorded about. */
11063 && PT >= CHARPOS (tlbufpos)
11064 && PT <= Z - CHARPOS (tlendpos)
11065 /* All text outside that line, including its final newline,
11066 must be unchanged */
11067 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11068 CHARPOS (tlendpos)))
11070 if (CHARPOS (tlbufpos) > BEGV
11071 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11072 && (CHARPOS (tlbufpos) == ZV
11073 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11074 /* Former continuation line has disappeared by becoming empty */
11075 goto cancel;
11076 else if (XFASTINT (w->last_modified) < MODIFF
11077 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11078 || MINI_WINDOW_P (w))
11080 /* We have to handle the case of continuation around a
11081 wide-column character (See the comment in indent.c around
11082 line 885).
11084 For instance, in the following case:
11086 -------- Insert --------
11087 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11088 J_I_ ==> J_I_ `^^' are cursors.
11089 ^^ ^^
11090 -------- --------
11092 As we have to redraw the line above, we should goto cancel. */
11094 struct it it;
11095 int line_height_before = this_line_pixel_height;
11097 /* Note that start_display will handle the case that the
11098 line starting at tlbufpos is a continuation lines. */
11099 start_display (&it, w, tlbufpos);
11101 /* Implementation note: It this still necessary? */
11102 if (it.current_x != this_line_start_x)
11103 goto cancel;
11105 TRACE ((stderr, "trying display optimization 1\n"));
11106 w->cursor.vpos = -1;
11107 overlay_arrow_seen = 0;
11108 it.vpos = this_line_vpos;
11109 it.current_y = this_line_y;
11110 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11111 display_line (&it);
11113 /* If line contains point, is not continued,
11114 and ends at same distance from eob as before, we win */
11115 if (w->cursor.vpos >= 0
11116 /* Line is not continued, otherwise this_line_start_pos
11117 would have been set to 0 in display_line. */
11118 && CHARPOS (this_line_start_pos)
11119 /* Line ends as before. */
11120 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11121 /* Line has same height as before. Otherwise other lines
11122 would have to be shifted up or down. */
11123 && this_line_pixel_height == line_height_before)
11125 /* If this is not the window's last line, we must adjust
11126 the charstarts of the lines below. */
11127 if (it.current_y < it.last_visible_y)
11129 struct glyph_row *row
11130 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11131 int delta, delta_bytes;
11133 if (Z - CHARPOS (tlendpos) == ZV)
11135 /* This line ends at end of (accessible part of)
11136 buffer. There is no newline to count. */
11137 delta = (Z
11138 - CHARPOS (tlendpos)
11139 - MATRIX_ROW_START_CHARPOS (row));
11140 delta_bytes = (Z_BYTE
11141 - BYTEPOS (tlendpos)
11142 - MATRIX_ROW_START_BYTEPOS (row));
11144 else
11146 /* This line ends in a newline. Must take
11147 account of the newline and the rest of the
11148 text that follows. */
11149 delta = (Z
11150 - CHARPOS (tlendpos)
11151 - MATRIX_ROW_START_CHARPOS (row));
11152 delta_bytes = (Z_BYTE
11153 - BYTEPOS (tlendpos)
11154 - MATRIX_ROW_START_BYTEPOS (row));
11157 increment_matrix_positions (w->current_matrix,
11158 this_line_vpos + 1,
11159 w->current_matrix->nrows,
11160 delta, delta_bytes);
11163 /* If this row displays text now but previously didn't,
11164 or vice versa, w->window_end_vpos may have to be
11165 adjusted. */
11166 if ((it.glyph_row - 1)->displays_text_p)
11168 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11169 XSETINT (w->window_end_vpos, this_line_vpos);
11171 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11172 && this_line_vpos > 0)
11173 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11174 w->window_end_valid = Qnil;
11176 /* Update hint: No need to try to scroll in update_window. */
11177 w->desired_matrix->no_scrolling_p = 1;
11179 #if GLYPH_DEBUG
11180 *w->desired_matrix->method = 0;
11181 debug_method_add (w, "optimization 1");
11182 #endif
11183 #ifdef HAVE_WINDOW_SYSTEM
11184 update_window_fringes (w, 0);
11185 #endif
11186 goto update;
11188 else
11189 goto cancel;
11191 else if (/* Cursor position hasn't changed. */
11192 PT == XFASTINT (w->last_point)
11193 /* Make sure the cursor was last displayed
11194 in this window. Otherwise we have to reposition it. */
11195 && 0 <= w->cursor.vpos
11196 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11198 if (!must_finish)
11200 do_pending_window_change (1);
11202 /* We used to always goto end_of_redisplay here, but this
11203 isn't enough if we have a blinking cursor. */
11204 if (w->cursor_off_p == w->last_cursor_off_p)
11205 goto end_of_redisplay;
11207 goto update;
11209 /* If highlighting the region, or if the cursor is in the echo area,
11210 then we can't just move the cursor. */
11211 else if (! (!NILP (Vtransient_mark_mode)
11212 && !NILP (current_buffer->mark_active))
11213 && (EQ (selected_window, current_buffer->last_selected_window)
11214 || highlight_nonselected_windows)
11215 && NILP (w->region_showing)
11216 && NILP (Vshow_trailing_whitespace)
11217 && !cursor_in_echo_area)
11219 struct it it;
11220 struct glyph_row *row;
11222 /* Skip from tlbufpos to PT and see where it is. Note that
11223 PT may be in invisible text. If so, we will end at the
11224 next visible position. */
11225 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11226 NULL, DEFAULT_FACE_ID);
11227 it.current_x = this_line_start_x;
11228 it.current_y = this_line_y;
11229 it.vpos = this_line_vpos;
11231 /* The call to move_it_to stops in front of PT, but
11232 moves over before-strings. */
11233 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11235 if (it.vpos == this_line_vpos
11236 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11237 row->enabled_p))
11239 xassert (this_line_vpos == it.vpos);
11240 xassert (this_line_y == it.current_y);
11241 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11242 #if GLYPH_DEBUG
11243 *w->desired_matrix->method = 0;
11244 debug_method_add (w, "optimization 3");
11245 #endif
11246 goto update;
11248 else
11249 goto cancel;
11252 cancel:
11253 /* Text changed drastically or point moved off of line. */
11254 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11257 CHARPOS (this_line_start_pos) = 0;
11258 consider_all_windows_p |= buffer_shared > 1;
11259 ++clear_face_cache_count;
11260 #ifdef HAVE_WINDOW_SYSTEM
11261 ++clear_image_cache_count;
11262 #endif
11264 /* Build desired matrices, and update the display. If
11265 consider_all_windows_p is non-zero, do it for all windows on all
11266 frames. Otherwise do it for selected_window, only. */
11268 if (consider_all_windows_p)
11270 Lisp_Object tail, frame;
11272 FOR_EACH_FRAME (tail, frame)
11273 XFRAME (frame)->updated_p = 0;
11275 /* Recompute # windows showing selected buffer. This will be
11276 incremented each time such a window is displayed. */
11277 buffer_shared = 0;
11279 FOR_EACH_FRAME (tail, frame)
11281 struct frame *f = XFRAME (frame);
11283 if (FRAME_WINDOW_P (f) || f == sf)
11285 if (! EQ (frame, selected_frame))
11286 /* Select the frame, for the sake of frame-local
11287 variables. */
11288 select_frame_for_redisplay (frame);
11290 /* Mark all the scroll bars to be removed; we'll redeem
11291 the ones we want when we redisplay their windows. */
11292 if (condemn_scroll_bars_hook)
11293 condemn_scroll_bars_hook (f);
11295 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11296 redisplay_windows (FRAME_ROOT_WINDOW (f));
11298 /* Any scroll bars which redisplay_windows should have
11299 nuked should now go away. */
11300 if (judge_scroll_bars_hook)
11301 judge_scroll_bars_hook (f);
11303 /* If fonts changed, display again. */
11304 /* ??? rms: I suspect it is a mistake to jump all the way
11305 back to retry here. It should just retry this frame. */
11306 if (fonts_changed_p)
11307 goto retry;
11309 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11311 /* See if we have to hscroll. */
11312 if (!f->already_hscrolled_p)
11314 f->already_hscrolled_p = 1;
11315 if (hscroll_windows (f->root_window))
11316 goto retry;
11319 /* Prevent various kinds of signals during display
11320 update. stdio is not robust about handling
11321 signals, which can cause an apparent I/O
11322 error. */
11323 if (interrupt_input)
11324 unrequest_sigio ();
11325 STOP_POLLING;
11327 /* Update the display. */
11328 set_window_update_flags (XWINDOW (f->root_window), 1);
11329 pause |= update_frame (f, 0, 0);
11330 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11331 if (pause)
11332 break;
11333 #endif
11335 f->updated_p = 1;
11340 if (!pause)
11342 /* Do the mark_window_display_accurate after all windows have
11343 been redisplayed because this call resets flags in buffers
11344 which are needed for proper redisplay. */
11345 FOR_EACH_FRAME (tail, frame)
11347 struct frame *f = XFRAME (frame);
11348 if (f->updated_p)
11350 mark_window_display_accurate (f->root_window, 1);
11351 if (frame_up_to_date_hook)
11352 frame_up_to_date_hook (f);
11357 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11359 Lisp_Object mini_window;
11360 struct frame *mini_frame;
11362 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11363 /* Use list_of_error, not Qerror, so that
11364 we catch only errors and don't run the debugger. */
11365 internal_condition_case_1 (redisplay_window_1, selected_window,
11366 list_of_error,
11367 redisplay_window_error);
11369 /* Compare desired and current matrices, perform output. */
11371 update:
11372 /* If fonts changed, display again. */
11373 if (fonts_changed_p)
11374 goto retry;
11376 /* Prevent various kinds of signals during display update.
11377 stdio is not robust about handling signals,
11378 which can cause an apparent I/O error. */
11379 if (interrupt_input)
11380 unrequest_sigio ();
11381 STOP_POLLING;
11383 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11385 if (hscroll_windows (selected_window))
11386 goto retry;
11388 XWINDOW (selected_window)->must_be_updated_p = 1;
11389 pause = update_frame (sf, 0, 0);
11392 /* We may have called echo_area_display at the top of this
11393 function. If the echo area is on another frame, that may
11394 have put text on a frame other than the selected one, so the
11395 above call to update_frame would not have caught it. Catch
11396 it here. */
11397 mini_window = FRAME_MINIBUF_WINDOW (sf);
11398 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11400 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11402 XWINDOW (mini_window)->must_be_updated_p = 1;
11403 pause |= update_frame (mini_frame, 0, 0);
11404 if (!pause && hscroll_windows (mini_window))
11405 goto retry;
11409 /* If display was paused because of pending input, make sure we do a
11410 thorough update the next time. */
11411 if (pause)
11413 /* Prevent the optimization at the beginning of
11414 redisplay_internal that tries a single-line update of the
11415 line containing the cursor in the selected window. */
11416 CHARPOS (this_line_start_pos) = 0;
11418 /* Let the overlay arrow be updated the next time. */
11419 update_overlay_arrows (0);
11421 /* If we pause after scrolling, some rows in the current
11422 matrices of some windows are not valid. */
11423 if (!WINDOW_FULL_WIDTH_P (w)
11424 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11425 update_mode_lines = 1;
11427 else
11429 if (!consider_all_windows_p)
11431 /* This has already been done above if
11432 consider_all_windows_p is set. */
11433 mark_window_display_accurate_1 (w, 1);
11435 /* Say overlay arrows are up to date. */
11436 update_overlay_arrows (1);
11438 if (frame_up_to_date_hook != 0)
11439 frame_up_to_date_hook (sf);
11442 update_mode_lines = 0;
11443 windows_or_buffers_changed = 0;
11444 cursor_type_changed = 0;
11447 /* Start SIGIO interrupts coming again. Having them off during the
11448 code above makes it less likely one will discard output, but not
11449 impossible, since there might be stuff in the system buffer here.
11450 But it is much hairier to try to do anything about that. */
11451 if (interrupt_input)
11452 request_sigio ();
11453 RESUME_POLLING;
11455 /* If a frame has become visible which was not before, redisplay
11456 again, so that we display it. Expose events for such a frame
11457 (which it gets when becoming visible) don't call the parts of
11458 redisplay constructing glyphs, so simply exposing a frame won't
11459 display anything in this case. So, we have to display these
11460 frames here explicitly. */
11461 if (!pause)
11463 Lisp_Object tail, frame;
11464 int new_count = 0;
11466 FOR_EACH_FRAME (tail, frame)
11468 int this_is_visible = 0;
11470 if (XFRAME (frame)->visible)
11471 this_is_visible = 1;
11472 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11473 if (XFRAME (frame)->visible)
11474 this_is_visible = 1;
11476 if (this_is_visible)
11477 new_count++;
11480 if (new_count != number_of_visible_frames)
11481 windows_or_buffers_changed++;
11484 /* Change frame size now if a change is pending. */
11485 do_pending_window_change (1);
11487 /* If we just did a pending size change, or have additional
11488 visible frames, redisplay again. */
11489 if (windows_or_buffers_changed && !pause)
11490 goto retry;
11492 /* Clear the face cache eventually. */
11493 if (consider_all_windows_p)
11495 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11497 clear_face_cache (0);
11498 clear_face_cache_count = 0;
11500 #ifdef HAVE_WINDOW_SYSTEM
11501 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11503 Lisp_Object tail, frame;
11504 FOR_EACH_FRAME (tail, frame)
11506 struct frame *f = XFRAME (frame);
11507 if (FRAME_WINDOW_P (f))
11508 clear_image_cache (f, 0);
11510 clear_image_cache_count = 0;
11512 #endif /* HAVE_WINDOW_SYSTEM */
11515 end_of_redisplay:
11516 unbind_to (count, Qnil);
11517 RESUME_POLLING;
11521 /* Redisplay, but leave alone any recent echo area message unless
11522 another message has been requested in its place.
11524 This is useful in situations where you need to redisplay but no
11525 user action has occurred, making it inappropriate for the message
11526 area to be cleared. See tracking_off and
11527 wait_reading_process_output for examples of these situations.
11529 FROM_WHERE is an integer saying from where this function was
11530 called. This is useful for debugging. */
11532 void
11533 redisplay_preserve_echo_area (from_where)
11534 int from_where;
11536 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11538 if (!NILP (echo_area_buffer[1]))
11540 /* We have a previously displayed message, but no current
11541 message. Redisplay the previous message. */
11542 display_last_displayed_message_p = 1;
11543 redisplay_internal (1);
11544 display_last_displayed_message_p = 0;
11546 else
11547 redisplay_internal (1);
11549 if (rif != NULL && rif->flush_display_optional)
11550 rif->flush_display_optional (NULL);
11554 /* Function registered with record_unwind_protect in
11555 redisplay_internal. Reset redisplaying_p to the value it had
11556 before redisplay_internal was called, and clear
11557 prevent_freeing_realized_faces_p. It also selects the previously
11558 selected frame. */
11560 static Lisp_Object
11561 unwind_redisplay (val)
11562 Lisp_Object val;
11564 Lisp_Object old_redisplaying_p, old_frame;
11566 old_redisplaying_p = XCAR (val);
11567 redisplaying_p = XFASTINT (old_redisplaying_p);
11568 old_frame = XCDR (val);
11569 if (! EQ (old_frame, selected_frame))
11570 select_frame_for_redisplay (old_frame);
11571 return Qnil;
11575 /* Mark the display of window W as accurate or inaccurate. If
11576 ACCURATE_P is non-zero mark display of W as accurate. If
11577 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11578 redisplay_internal is called. */
11580 static void
11581 mark_window_display_accurate_1 (w, accurate_p)
11582 struct window *w;
11583 int accurate_p;
11585 if (BUFFERP (w->buffer))
11587 struct buffer *b = XBUFFER (w->buffer);
11589 w->last_modified
11590 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11591 w->last_overlay_modified
11592 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11593 w->last_had_star
11594 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11596 if (accurate_p)
11598 b->clip_changed = 0;
11599 b->prevent_redisplay_optimizations_p = 0;
11601 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11602 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11603 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11604 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11606 w->current_matrix->buffer = b;
11607 w->current_matrix->begv = BUF_BEGV (b);
11608 w->current_matrix->zv = BUF_ZV (b);
11610 w->last_cursor = w->cursor;
11611 w->last_cursor_off_p = w->cursor_off_p;
11613 if (w == XWINDOW (selected_window))
11614 w->last_point = make_number (BUF_PT (b));
11615 else
11616 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11620 if (accurate_p)
11622 w->window_end_valid = w->buffer;
11623 #if 0 /* This is incorrect with variable-height lines. */
11624 xassert (XINT (w->window_end_vpos)
11625 < (WINDOW_TOTAL_LINES (w)
11626 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11627 #endif
11628 w->update_mode_line = Qnil;
11633 /* Mark the display of windows in the window tree rooted at WINDOW as
11634 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11635 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11636 be redisplayed the next time redisplay_internal is called. */
11638 void
11639 mark_window_display_accurate (window, accurate_p)
11640 Lisp_Object window;
11641 int accurate_p;
11643 struct window *w;
11645 for (; !NILP (window); window = w->next)
11647 w = XWINDOW (window);
11648 mark_window_display_accurate_1 (w, accurate_p);
11650 if (!NILP (w->vchild))
11651 mark_window_display_accurate (w->vchild, accurate_p);
11652 if (!NILP (w->hchild))
11653 mark_window_display_accurate (w->hchild, accurate_p);
11656 if (accurate_p)
11658 update_overlay_arrows (1);
11660 else
11662 /* Force a thorough redisplay the next time by setting
11663 last_arrow_position and last_arrow_string to t, which is
11664 unequal to any useful value of Voverlay_arrow_... */
11665 update_overlay_arrows (-1);
11670 /* Return value in display table DP (Lisp_Char_Table *) for character
11671 C. Since a display table doesn't have any parent, we don't have to
11672 follow parent. Do not call this function directly but use the
11673 macro DISP_CHAR_VECTOR. */
11675 Lisp_Object
11676 disp_char_vector (dp, c)
11677 struct Lisp_Char_Table *dp;
11678 int c;
11680 int code[4], i;
11681 Lisp_Object val;
11683 if (SINGLE_BYTE_CHAR_P (c))
11684 return (dp->contents[c]);
11686 SPLIT_CHAR (c, code[0], code[1], code[2]);
11687 if (code[1] < 32)
11688 code[1] = -1;
11689 else if (code[2] < 32)
11690 code[2] = -1;
11692 /* Here, the possible range of code[0] (== charset ID) is
11693 128..max_charset. Since the top level char table contains data
11694 for multibyte characters after 256th element, we must increment
11695 code[0] by 128 to get a correct index. */
11696 code[0] += 128;
11697 code[3] = -1; /* anchor */
11699 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11701 val = dp->contents[code[i]];
11702 if (!SUB_CHAR_TABLE_P (val))
11703 return (NILP (val) ? dp->defalt : val);
11706 /* Here, val is a sub char table. We return the default value of
11707 it. */
11708 return (dp->defalt);
11713 /***********************************************************************
11714 Window Redisplay
11715 ***********************************************************************/
11717 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11719 static void
11720 redisplay_windows (window)
11721 Lisp_Object window;
11723 while (!NILP (window))
11725 struct window *w = XWINDOW (window);
11727 if (!NILP (w->hchild))
11728 redisplay_windows (w->hchild);
11729 else if (!NILP (w->vchild))
11730 redisplay_windows (w->vchild);
11731 else
11733 displayed_buffer = XBUFFER (w->buffer);
11734 /* Use list_of_error, not Qerror, so that
11735 we catch only errors and don't run the debugger. */
11736 internal_condition_case_1 (redisplay_window_0, window,
11737 list_of_error,
11738 redisplay_window_error);
11741 window = w->next;
11745 static Lisp_Object
11746 redisplay_window_error ()
11748 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11749 return Qnil;
11752 static Lisp_Object
11753 redisplay_window_0 (window)
11754 Lisp_Object window;
11756 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11757 redisplay_window (window, 0);
11758 return Qnil;
11761 static Lisp_Object
11762 redisplay_window_1 (window)
11763 Lisp_Object window;
11765 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11766 redisplay_window (window, 1);
11767 return Qnil;
11771 /* Increment GLYPH until it reaches END or CONDITION fails while
11772 adding (GLYPH)->pixel_width to X. */
11774 #define SKIP_GLYPHS(glyph, end, x, condition) \
11775 do \
11777 (x) += (glyph)->pixel_width; \
11778 ++(glyph); \
11780 while ((glyph) < (end) && (condition))
11783 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11784 DELTA is the number of bytes by which positions recorded in ROW
11785 differ from current buffer positions.
11787 Return 0 if cursor is not on this row. 1 otherwise. */
11790 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11791 struct window *w;
11792 struct glyph_row *row;
11793 struct glyph_matrix *matrix;
11794 int delta, delta_bytes, dy, dvpos;
11796 struct glyph *glyph = row->glyphs[TEXT_AREA];
11797 struct glyph *end = glyph + row->used[TEXT_AREA];
11798 struct glyph *cursor = NULL;
11799 /* The first glyph that starts a sequence of glyphs from string. */
11800 struct glyph *string_start;
11801 /* The X coordinate of string_start. */
11802 int string_start_x;
11803 /* The last known character position. */
11804 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11805 /* The last known character position before string_start. */
11806 int string_before_pos;
11807 int x = row->x;
11808 int cursor_x = x;
11809 int cursor_from_overlay_pos = 0;
11810 int pt_old = PT - delta;
11812 /* Skip over glyphs not having an object at the start of the row.
11813 These are special glyphs like truncation marks on terminal
11814 frames. */
11815 if (row->displays_text_p)
11816 while (glyph < end
11817 && INTEGERP (glyph->object)
11818 && glyph->charpos < 0)
11820 x += glyph->pixel_width;
11821 ++glyph;
11824 string_start = NULL;
11825 while (glyph < end
11826 && !INTEGERP (glyph->object)
11827 && (!BUFFERP (glyph->object)
11828 || (last_pos = glyph->charpos) < pt_old))
11830 if (! STRINGP (glyph->object))
11832 string_start = NULL;
11833 x += glyph->pixel_width;
11834 ++glyph;
11835 if (cursor_from_overlay_pos
11836 && last_pos >= cursor_from_overlay_pos)
11838 cursor_from_overlay_pos = 0;
11839 cursor = 0;
11842 else
11844 if (string_start == NULL)
11846 string_before_pos = last_pos;
11847 string_start = glyph;
11848 string_start_x = x;
11850 /* Skip all glyphs from string. */
11853 Lisp_Object cprop;
11854 int pos;
11855 if ((cursor == NULL || glyph > cursor)
11856 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11857 Qcursor, (glyph)->object),
11858 !NILP (cprop))
11859 && (pos = string_buffer_position (w, glyph->object,
11860 string_before_pos),
11861 (pos == 0 /* From overlay */
11862 || pos == pt_old)))
11864 /* Estimate overlay buffer position from the buffer
11865 positions of the glyphs before and after the overlay.
11866 Add 1 to last_pos so that if point corresponds to the
11867 glyph right after the overlay, we still use a 'cursor'
11868 property found in that overlay. */
11869 cursor_from_overlay_pos = (pos ? 0 : last_pos
11870 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11871 cursor = glyph;
11872 cursor_x = x;
11874 x += glyph->pixel_width;
11875 ++glyph;
11877 while (glyph < end && EQ (glyph->object, string_start->object));
11881 if (cursor != NULL)
11883 glyph = cursor;
11884 x = cursor_x;
11886 else if (row->ends_in_ellipsis_p && glyph == end)
11888 /* Scan back over the ellipsis glyphs, decrementing positions. */
11889 while (glyph > row->glyphs[TEXT_AREA]
11890 && (glyph - 1)->charpos == last_pos)
11891 glyph--, x -= glyph->pixel_width;
11892 /* That loop always goes one position too far,
11893 including the glyph before the ellipsis.
11894 So scan forward over that one. */
11895 x += glyph->pixel_width;
11896 glyph++;
11898 else if (string_start
11899 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11901 /* We may have skipped over point because the previous glyphs
11902 are from string. As there's no easy way to know the
11903 character position of the current glyph, find the correct
11904 glyph on point by scanning from string_start again. */
11905 Lisp_Object limit;
11906 Lisp_Object string;
11907 struct glyph *stop = glyph;
11908 int pos;
11910 limit = make_number (pt_old + 1);
11911 glyph = string_start;
11912 x = string_start_x;
11913 string = glyph->object;
11914 pos = string_buffer_position (w, string, string_before_pos);
11915 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11916 because we always put cursor after overlay strings. */
11917 while (pos == 0 && glyph < stop)
11919 string = glyph->object;
11920 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11921 if (glyph < stop)
11922 pos = string_buffer_position (w, glyph->object, string_before_pos);
11925 while (glyph < stop)
11927 pos = XINT (Fnext_single_char_property_change
11928 (make_number (pos), Qdisplay, Qnil, limit));
11929 if (pos > pt_old)
11930 break;
11931 /* Skip glyphs from the same string. */
11932 string = glyph->object;
11933 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11934 /* Skip glyphs from an overlay. */
11935 while (glyph < stop
11936 && ! string_buffer_position (w, glyph->object, pos))
11938 string = glyph->object;
11939 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11943 /* If we reached the end of the line, and end was from a string,
11944 cursor is not on this line. */
11945 if (glyph == end && row->continued_p)
11946 return 0;
11949 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11950 w->cursor.x = x;
11951 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11952 w->cursor.y = row->y + dy;
11954 if (w == XWINDOW (selected_window))
11956 if (!row->continued_p
11957 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11958 && row->x == 0)
11960 this_line_buffer = XBUFFER (w->buffer);
11962 CHARPOS (this_line_start_pos)
11963 = MATRIX_ROW_START_CHARPOS (row) + delta;
11964 BYTEPOS (this_line_start_pos)
11965 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11967 CHARPOS (this_line_end_pos)
11968 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11969 BYTEPOS (this_line_end_pos)
11970 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11972 this_line_y = w->cursor.y;
11973 this_line_pixel_height = row->height;
11974 this_line_vpos = w->cursor.vpos;
11975 this_line_start_x = row->x;
11977 else
11978 CHARPOS (this_line_start_pos) = 0;
11981 return 1;
11985 /* Run window scroll functions, if any, for WINDOW with new window
11986 start STARTP. Sets the window start of WINDOW to that position.
11988 We assume that the window's buffer is really current. */
11990 static INLINE struct text_pos
11991 run_window_scroll_functions (window, startp)
11992 Lisp_Object window;
11993 struct text_pos startp;
11995 struct window *w = XWINDOW (window);
11996 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11998 if (current_buffer != XBUFFER (w->buffer))
11999 abort ();
12001 if (!NILP (Vwindow_scroll_functions))
12003 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12004 make_number (CHARPOS (startp)));
12005 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12006 /* In case the hook functions switch buffers. */
12007 if (current_buffer != XBUFFER (w->buffer))
12008 set_buffer_internal_1 (XBUFFER (w->buffer));
12011 return startp;
12015 /* Make sure the line containing the cursor is fully visible.
12016 A value of 1 means there is nothing to be done.
12017 (Either the line is fully visible, or it cannot be made so,
12018 or we cannot tell.)
12020 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12021 is higher than window.
12023 A value of 0 means the caller should do scrolling
12024 as if point had gone off the screen. */
12026 static int
12027 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12028 struct window *w;
12029 int force_p;
12030 int current_matrix_p;
12032 struct glyph_matrix *matrix;
12033 struct glyph_row *row;
12034 int window_height;
12036 if (!make_cursor_line_fully_visible_p)
12037 return 1;
12039 /* It's not always possible to find the cursor, e.g, when a window
12040 is full of overlay strings. Don't do anything in that case. */
12041 if (w->cursor.vpos < 0)
12042 return 1;
12044 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12045 row = MATRIX_ROW (matrix, w->cursor.vpos);
12047 /* If the cursor row is not partially visible, there's nothing to do. */
12048 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12049 return 1;
12051 /* If the row the cursor is in is taller than the window's height,
12052 it's not clear what to do, so do nothing. */
12053 window_height = window_box_height (w);
12054 if (row->height >= window_height)
12056 if (!force_p || MINI_WINDOW_P (w)
12057 || w->vscroll || w->cursor.vpos == 0)
12058 return 1;
12060 return 0;
12062 #if 0
12063 /* This code used to try to scroll the window just enough to make
12064 the line visible. It returned 0 to say that the caller should
12065 allocate larger glyph matrices. */
12067 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12069 int dy = row->height - row->visible_height;
12070 w->vscroll = 0;
12071 w->cursor.y += dy;
12072 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12074 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12076 int dy = - (row->height - row->visible_height);
12077 w->vscroll = dy;
12078 w->cursor.y += dy;
12079 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12082 /* When we change the cursor y-position of the selected window,
12083 change this_line_y as well so that the display optimization for
12084 the cursor line of the selected window in redisplay_internal uses
12085 the correct y-position. */
12086 if (w == XWINDOW (selected_window))
12087 this_line_y = w->cursor.y;
12089 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12090 redisplay with larger matrices. */
12091 if (matrix->nrows < required_matrix_height (w))
12093 fonts_changed_p = 1;
12094 return 0;
12097 return 1;
12098 #endif /* 0 */
12102 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12103 non-zero means only WINDOW is redisplayed in redisplay_internal.
12104 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12105 in redisplay_window to bring a partially visible line into view in
12106 the case that only the cursor has moved.
12108 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12109 last screen line's vertical height extends past the end of the screen.
12111 Value is
12113 1 if scrolling succeeded
12115 0 if scrolling didn't find point.
12117 -1 if new fonts have been loaded so that we must interrupt
12118 redisplay, adjust glyph matrices, and try again. */
12120 enum
12122 SCROLLING_SUCCESS,
12123 SCROLLING_FAILED,
12124 SCROLLING_NEED_LARGER_MATRICES
12127 static int
12128 try_scrolling (window, just_this_one_p, scroll_conservatively,
12129 scroll_step, temp_scroll_step, last_line_misfit)
12130 Lisp_Object window;
12131 int just_this_one_p;
12132 EMACS_INT scroll_conservatively, scroll_step;
12133 int temp_scroll_step;
12134 int last_line_misfit;
12136 struct window *w = XWINDOW (window);
12137 struct frame *f = XFRAME (w->frame);
12138 struct text_pos scroll_margin_pos;
12139 struct text_pos pos;
12140 struct text_pos startp;
12141 struct it it;
12142 Lisp_Object window_end;
12143 int this_scroll_margin;
12144 int dy = 0;
12145 int scroll_max;
12146 int rc;
12147 int amount_to_scroll = 0;
12148 Lisp_Object aggressive;
12149 int height;
12150 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12152 #if GLYPH_DEBUG
12153 debug_method_add (w, "try_scrolling");
12154 #endif
12156 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12158 /* Compute scroll margin height in pixels. We scroll when point is
12159 within this distance from the top or bottom of the window. */
12160 if (scroll_margin > 0)
12162 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12163 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12165 else
12166 this_scroll_margin = 0;
12168 /* Force scroll_conservatively to have a reasonable value so it doesn't
12169 cause an overflow while computing how much to scroll. */
12170 if (scroll_conservatively)
12171 scroll_conservatively = min (scroll_conservatively,
12172 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12174 /* Compute how much we should try to scroll maximally to bring point
12175 into view. */
12176 if (scroll_step || scroll_conservatively || temp_scroll_step)
12177 scroll_max = max (scroll_step,
12178 max (scroll_conservatively, temp_scroll_step));
12179 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12180 || NUMBERP (current_buffer->scroll_up_aggressively))
12181 /* We're trying to scroll because of aggressive scrolling
12182 but no scroll_step is set. Choose an arbitrary one. Maybe
12183 there should be a variable for this. */
12184 scroll_max = 10;
12185 else
12186 scroll_max = 0;
12187 scroll_max *= FRAME_LINE_HEIGHT (f);
12189 /* Decide whether we have to scroll down. Start at the window end
12190 and move this_scroll_margin up to find the position of the scroll
12191 margin. */
12192 window_end = Fwindow_end (window, Qt);
12194 too_near_end:
12196 CHARPOS (scroll_margin_pos) = XINT (window_end);
12197 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12199 if (this_scroll_margin || extra_scroll_margin_lines)
12201 start_display (&it, w, scroll_margin_pos);
12202 if (this_scroll_margin)
12203 move_it_vertically_backward (&it, this_scroll_margin);
12204 if (extra_scroll_margin_lines)
12205 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12206 scroll_margin_pos = it.current.pos;
12209 if (PT >= CHARPOS (scroll_margin_pos))
12211 int y0;
12213 /* Point is in the scroll margin at the bottom of the window, or
12214 below. Compute a new window start that makes point visible. */
12216 /* Compute the distance from the scroll margin to PT.
12217 Give up if the distance is greater than scroll_max. */
12218 start_display (&it, w, scroll_margin_pos);
12219 y0 = it.current_y;
12220 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12221 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12223 /* To make point visible, we have to move the window start
12224 down so that the line the cursor is in is visible, which
12225 means we have to add in the height of the cursor line. */
12226 dy = line_bottom_y (&it) - y0;
12228 if (dy > scroll_max)
12229 return SCROLLING_FAILED;
12231 /* Move the window start down. If scrolling conservatively,
12232 move it just enough down to make point visible. If
12233 scroll_step is set, move it down by scroll_step. */
12234 start_display (&it, w, startp);
12236 if (scroll_conservatively)
12237 /* Set AMOUNT_TO_SCROLL to at least one line,
12238 and at most scroll_conservatively lines. */
12239 amount_to_scroll
12240 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12241 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12242 else if (scroll_step || temp_scroll_step)
12243 amount_to_scroll = scroll_max;
12244 else
12246 aggressive = current_buffer->scroll_up_aggressively;
12247 height = WINDOW_BOX_TEXT_HEIGHT (w);
12248 if (NUMBERP (aggressive))
12250 double float_amount = XFLOATINT (aggressive) * height;
12251 amount_to_scroll = float_amount;
12252 if (amount_to_scroll == 0 && float_amount > 0)
12253 amount_to_scroll = 1;
12257 if (amount_to_scroll <= 0)
12258 return SCROLLING_FAILED;
12260 /* If moving by amount_to_scroll leaves STARTP unchanged,
12261 move it down one screen line. */
12263 move_it_vertically (&it, amount_to_scroll);
12264 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12265 move_it_by_lines (&it, 1, 1);
12266 startp = it.current.pos;
12268 else
12270 /* See if point is inside the scroll margin at the top of the
12271 window. */
12272 scroll_margin_pos = startp;
12273 if (this_scroll_margin)
12275 start_display (&it, w, startp);
12276 move_it_vertically (&it, this_scroll_margin);
12277 scroll_margin_pos = it.current.pos;
12280 if (PT < CHARPOS (scroll_margin_pos))
12282 /* Point is in the scroll margin at the top of the window or
12283 above what is displayed in the window. */
12284 int y0;
12286 /* Compute the vertical distance from PT to the scroll
12287 margin position. Give up if distance is greater than
12288 scroll_max. */
12289 SET_TEXT_POS (pos, PT, PT_BYTE);
12290 start_display (&it, w, pos);
12291 y0 = it.current_y;
12292 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12293 it.last_visible_y, -1,
12294 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12295 dy = it.current_y - y0;
12296 if (dy > scroll_max)
12297 return SCROLLING_FAILED;
12299 /* Compute new window start. */
12300 start_display (&it, w, startp);
12302 if (scroll_conservatively)
12303 amount_to_scroll
12304 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12305 else if (scroll_step || temp_scroll_step)
12306 amount_to_scroll = scroll_max;
12307 else
12309 aggressive = current_buffer->scroll_down_aggressively;
12310 height = WINDOW_BOX_TEXT_HEIGHT (w);
12311 if (NUMBERP (aggressive))
12313 double float_amount = XFLOATINT (aggressive) * height;
12314 amount_to_scroll = float_amount;
12315 if (amount_to_scroll == 0 && float_amount > 0)
12316 amount_to_scroll = 1;
12320 if (amount_to_scroll <= 0)
12321 return SCROLLING_FAILED;
12323 move_it_vertically_backward (&it, amount_to_scroll);
12324 startp = it.current.pos;
12328 /* Run window scroll functions. */
12329 startp = run_window_scroll_functions (window, startp);
12331 /* Display the window. Give up if new fonts are loaded, or if point
12332 doesn't appear. */
12333 if (!try_window (window, startp, 0))
12334 rc = SCROLLING_NEED_LARGER_MATRICES;
12335 else if (w->cursor.vpos < 0)
12337 clear_glyph_matrix (w->desired_matrix);
12338 rc = SCROLLING_FAILED;
12340 else
12342 /* Maybe forget recorded base line for line number display. */
12343 if (!just_this_one_p
12344 || current_buffer->clip_changed
12345 || BEG_UNCHANGED < CHARPOS (startp))
12346 w->base_line_number = Qnil;
12348 /* If cursor ends up on a partially visible line,
12349 treat that as being off the bottom of the screen. */
12350 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12352 clear_glyph_matrix (w->desired_matrix);
12353 ++extra_scroll_margin_lines;
12354 goto too_near_end;
12356 rc = SCROLLING_SUCCESS;
12359 return rc;
12363 /* Compute a suitable window start for window W if display of W starts
12364 on a continuation line. Value is non-zero if a new window start
12365 was computed.
12367 The new window start will be computed, based on W's width, starting
12368 from the start of the continued line. It is the start of the
12369 screen line with the minimum distance from the old start W->start. */
12371 static int
12372 compute_window_start_on_continuation_line (w)
12373 struct window *w;
12375 struct text_pos pos, start_pos;
12376 int window_start_changed_p = 0;
12378 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12380 /* If window start is on a continuation line... Window start may be
12381 < BEGV in case there's invisible text at the start of the
12382 buffer (M-x rmail, for example). */
12383 if (CHARPOS (start_pos) > BEGV
12384 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12386 struct it it;
12387 struct glyph_row *row;
12389 /* Handle the case that the window start is out of range. */
12390 if (CHARPOS (start_pos) < BEGV)
12391 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12392 else if (CHARPOS (start_pos) > ZV)
12393 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12395 /* Find the start of the continued line. This should be fast
12396 because scan_buffer is fast (newline cache). */
12397 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12398 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12399 row, DEFAULT_FACE_ID);
12400 reseat_at_previous_visible_line_start (&it);
12402 /* If the line start is "too far" away from the window start,
12403 say it takes too much time to compute a new window start. */
12404 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12405 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12407 int min_distance, distance;
12409 /* Move forward by display lines to find the new window
12410 start. If window width was enlarged, the new start can
12411 be expected to be > the old start. If window width was
12412 decreased, the new window start will be < the old start.
12413 So, we're looking for the display line start with the
12414 minimum distance from the old window start. */
12415 pos = it.current.pos;
12416 min_distance = INFINITY;
12417 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12418 distance < min_distance)
12420 min_distance = distance;
12421 pos = it.current.pos;
12422 move_it_by_lines (&it, 1, 0);
12425 /* Set the window start there. */
12426 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12427 window_start_changed_p = 1;
12431 return window_start_changed_p;
12435 /* Try cursor movement in case text has not changed in window WINDOW,
12436 with window start STARTP. Value is
12438 CURSOR_MOVEMENT_SUCCESS if successful
12440 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12442 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12443 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12444 we want to scroll as if scroll-step were set to 1. See the code.
12446 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12447 which case we have to abort this redisplay, and adjust matrices
12448 first. */
12450 enum
12452 CURSOR_MOVEMENT_SUCCESS,
12453 CURSOR_MOVEMENT_CANNOT_BE_USED,
12454 CURSOR_MOVEMENT_MUST_SCROLL,
12455 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12458 static int
12459 try_cursor_movement (window, startp, scroll_step)
12460 Lisp_Object window;
12461 struct text_pos startp;
12462 int *scroll_step;
12464 struct window *w = XWINDOW (window);
12465 struct frame *f = XFRAME (w->frame);
12466 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12468 #if GLYPH_DEBUG
12469 if (inhibit_try_cursor_movement)
12470 return rc;
12471 #endif
12473 /* Handle case where text has not changed, only point, and it has
12474 not moved off the frame. */
12475 if (/* Point may be in this window. */
12476 PT >= CHARPOS (startp)
12477 /* Selective display hasn't changed. */
12478 && !current_buffer->clip_changed
12479 /* Function force-mode-line-update is used to force a thorough
12480 redisplay. It sets either windows_or_buffers_changed or
12481 update_mode_lines. So don't take a shortcut here for these
12482 cases. */
12483 && !update_mode_lines
12484 && !windows_or_buffers_changed
12485 && !cursor_type_changed
12486 /* Can't use this case if highlighting a region. When a
12487 region exists, cursor movement has to do more than just
12488 set the cursor. */
12489 && !(!NILP (Vtransient_mark_mode)
12490 && !NILP (current_buffer->mark_active))
12491 && NILP (w->region_showing)
12492 && NILP (Vshow_trailing_whitespace)
12493 /* Right after splitting windows, last_point may be nil. */
12494 && INTEGERP (w->last_point)
12495 /* This code is not used for mini-buffer for the sake of the case
12496 of redisplaying to replace an echo area message; since in
12497 that case the mini-buffer contents per se are usually
12498 unchanged. This code is of no real use in the mini-buffer
12499 since the handling of this_line_start_pos, etc., in redisplay
12500 handles the same cases. */
12501 && !EQ (window, minibuf_window)
12502 /* When splitting windows or for new windows, it happens that
12503 redisplay is called with a nil window_end_vpos or one being
12504 larger than the window. This should really be fixed in
12505 window.c. I don't have this on my list, now, so we do
12506 approximately the same as the old redisplay code. --gerd. */
12507 && INTEGERP (w->window_end_vpos)
12508 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12509 && (FRAME_WINDOW_P (f)
12510 || !overlay_arrow_in_current_buffer_p ()))
12512 int this_scroll_margin, top_scroll_margin;
12513 struct glyph_row *row = NULL;
12515 #if GLYPH_DEBUG
12516 debug_method_add (w, "cursor movement");
12517 #endif
12519 /* Scroll if point within this distance from the top or bottom
12520 of the window. This is a pixel value. */
12521 this_scroll_margin = max (0, scroll_margin);
12522 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12523 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12525 top_scroll_margin = this_scroll_margin;
12526 if (WINDOW_WANTS_HEADER_LINE_P (w))
12527 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12529 /* Start with the row the cursor was displayed during the last
12530 not paused redisplay. Give up if that row is not valid. */
12531 if (w->last_cursor.vpos < 0
12532 || w->last_cursor.vpos >= w->current_matrix->nrows)
12533 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12534 else
12536 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12537 if (row->mode_line_p)
12538 ++row;
12539 if (!row->enabled_p)
12540 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12543 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12545 int scroll_p = 0;
12546 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12548 if (PT > XFASTINT (w->last_point))
12550 /* Point has moved forward. */
12551 while (MATRIX_ROW_END_CHARPOS (row) < PT
12552 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12554 xassert (row->enabled_p);
12555 ++row;
12558 /* The end position of a row equals the start position
12559 of the next row. If PT is there, we would rather
12560 display it in the next line. */
12561 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12562 && MATRIX_ROW_END_CHARPOS (row) == PT
12563 && !cursor_row_p (w, row))
12564 ++row;
12566 /* If within the scroll margin, scroll. Note that
12567 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12568 the next line would be drawn, and that
12569 this_scroll_margin can be zero. */
12570 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12571 || PT > MATRIX_ROW_END_CHARPOS (row)
12572 /* Line is completely visible last line in window
12573 and PT is to be set in the next line. */
12574 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12575 && PT == MATRIX_ROW_END_CHARPOS (row)
12576 && !row->ends_at_zv_p
12577 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12578 scroll_p = 1;
12580 else if (PT < XFASTINT (w->last_point))
12582 /* Cursor has to be moved backward. Note that PT >=
12583 CHARPOS (startp) because of the outer if-statement. */
12584 while (!row->mode_line_p
12585 && (MATRIX_ROW_START_CHARPOS (row) > PT
12586 || (MATRIX_ROW_START_CHARPOS (row) == PT
12587 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12588 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12589 row > w->current_matrix->rows
12590 && (row-1)->ends_in_newline_from_string_p))))
12591 && (row->y > top_scroll_margin
12592 || CHARPOS (startp) == BEGV))
12594 xassert (row->enabled_p);
12595 --row;
12598 /* Consider the following case: Window starts at BEGV,
12599 there is invisible, intangible text at BEGV, so that
12600 display starts at some point START > BEGV. It can
12601 happen that we are called with PT somewhere between
12602 BEGV and START. Try to handle that case. */
12603 if (row < w->current_matrix->rows
12604 || row->mode_line_p)
12606 row = w->current_matrix->rows;
12607 if (row->mode_line_p)
12608 ++row;
12611 /* Due to newlines in overlay strings, we may have to
12612 skip forward over overlay strings. */
12613 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12614 && MATRIX_ROW_END_CHARPOS (row) == PT
12615 && !cursor_row_p (w, row))
12616 ++row;
12618 /* If within the scroll margin, scroll. */
12619 if (row->y < top_scroll_margin
12620 && CHARPOS (startp) != BEGV)
12621 scroll_p = 1;
12623 else
12625 /* Cursor did not move. So don't scroll even if cursor line
12626 is partially visible, as it was so before. */
12627 rc = CURSOR_MOVEMENT_SUCCESS;
12630 if (PT < MATRIX_ROW_START_CHARPOS (row)
12631 || PT > MATRIX_ROW_END_CHARPOS (row))
12633 /* if PT is not in the glyph row, give up. */
12634 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12636 else if (rc != CURSOR_MOVEMENT_SUCCESS
12637 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12638 && make_cursor_line_fully_visible_p)
12640 if (PT == MATRIX_ROW_END_CHARPOS (row)
12641 && !row->ends_at_zv_p
12642 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12643 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12644 else if (row->height > window_box_height (w))
12646 /* If we end up in a partially visible line, let's
12647 make it fully visible, except when it's taller
12648 than the window, in which case we can't do much
12649 about it. */
12650 *scroll_step = 1;
12651 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12653 else
12655 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12656 if (!cursor_row_fully_visible_p (w, 0, 1))
12657 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12658 else
12659 rc = CURSOR_MOVEMENT_SUCCESS;
12662 else if (scroll_p)
12663 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12664 else
12668 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12670 rc = CURSOR_MOVEMENT_SUCCESS;
12671 break;
12673 ++row;
12675 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12676 && MATRIX_ROW_START_CHARPOS (row) == PT
12677 && cursor_row_p (w, row));
12682 return rc;
12685 void
12686 set_vertical_scroll_bar (w)
12687 struct window *w;
12689 int start, end, whole;
12691 /* Calculate the start and end positions for the current window.
12692 At some point, it would be nice to choose between scrollbars
12693 which reflect the whole buffer size, with special markers
12694 indicating narrowing, and scrollbars which reflect only the
12695 visible region.
12697 Note that mini-buffers sometimes aren't displaying any text. */
12698 if (!MINI_WINDOW_P (w)
12699 || (w == XWINDOW (minibuf_window)
12700 && NILP (echo_area_buffer[0])))
12702 struct buffer *buf = XBUFFER (w->buffer);
12703 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12704 start = marker_position (w->start) - BUF_BEGV (buf);
12705 /* I don't think this is guaranteed to be right. For the
12706 moment, we'll pretend it is. */
12707 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12709 if (end < start)
12710 end = start;
12711 if (whole < (end - start))
12712 whole = end - start;
12714 else
12715 start = end = whole = 0;
12717 /* Indicate what this scroll bar ought to be displaying now. */
12718 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12722 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12723 selected_window is redisplayed.
12725 We can return without actually redisplaying the window if
12726 fonts_changed_p is nonzero. In that case, redisplay_internal will
12727 retry. */
12729 static void
12730 redisplay_window (window, just_this_one_p)
12731 Lisp_Object window;
12732 int just_this_one_p;
12734 struct window *w = XWINDOW (window);
12735 struct frame *f = XFRAME (w->frame);
12736 struct buffer *buffer = XBUFFER (w->buffer);
12737 struct buffer *old = current_buffer;
12738 struct text_pos lpoint, opoint, startp;
12739 int update_mode_line;
12740 int tem;
12741 struct it it;
12742 /* Record it now because it's overwritten. */
12743 int current_matrix_up_to_date_p = 0;
12744 int used_current_matrix_p = 0;
12745 /* This is less strict than current_matrix_up_to_date_p.
12746 It indictes that the buffer contents and narrowing are unchanged. */
12747 int buffer_unchanged_p = 0;
12748 int temp_scroll_step = 0;
12749 int count = SPECPDL_INDEX ();
12750 int rc;
12751 int centering_position = -1;
12752 int last_line_misfit = 0;
12754 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12755 opoint = lpoint;
12757 /* W must be a leaf window here. */
12758 xassert (!NILP (w->buffer));
12759 #if GLYPH_DEBUG
12760 *w->desired_matrix->method = 0;
12761 #endif
12763 specbind (Qinhibit_point_motion_hooks, Qt);
12765 reconsider_clip_changes (w, buffer);
12767 /* Has the mode line to be updated? */
12768 update_mode_line = (!NILP (w->update_mode_line)
12769 || update_mode_lines
12770 || buffer->clip_changed
12771 || buffer->prevent_redisplay_optimizations_p);
12773 if (MINI_WINDOW_P (w))
12775 if (w == XWINDOW (echo_area_window)
12776 && !NILP (echo_area_buffer[0]))
12778 if (update_mode_line)
12779 /* We may have to update a tty frame's menu bar or a
12780 tool-bar. Example `M-x C-h C-h C-g'. */
12781 goto finish_menu_bars;
12782 else
12783 /* We've already displayed the echo area glyphs in this window. */
12784 goto finish_scroll_bars;
12786 else if ((w != XWINDOW (minibuf_window)
12787 || minibuf_level == 0)
12788 /* When buffer is nonempty, redisplay window normally. */
12789 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12790 /* Quail displays non-mini buffers in minibuffer window.
12791 In that case, redisplay the window normally. */
12792 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12794 /* W is a mini-buffer window, but it's not active, so clear
12795 it. */
12796 int yb = window_text_bottom_y (w);
12797 struct glyph_row *row;
12798 int y;
12800 for (y = 0, row = w->desired_matrix->rows;
12801 y < yb;
12802 y += row->height, ++row)
12803 blank_row (w, row, y);
12804 goto finish_scroll_bars;
12807 clear_glyph_matrix (w->desired_matrix);
12810 /* Otherwise set up data on this window; select its buffer and point
12811 value. */
12812 /* Really select the buffer, for the sake of buffer-local
12813 variables. */
12814 set_buffer_internal_1 (XBUFFER (w->buffer));
12815 SET_TEXT_POS (opoint, PT, PT_BYTE);
12817 current_matrix_up_to_date_p
12818 = (!NILP (w->window_end_valid)
12819 && !current_buffer->clip_changed
12820 && !current_buffer->prevent_redisplay_optimizations_p
12821 && XFASTINT (w->last_modified) >= MODIFF
12822 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12824 buffer_unchanged_p
12825 = (!NILP (w->window_end_valid)
12826 && !current_buffer->clip_changed
12827 && XFASTINT (w->last_modified) >= MODIFF
12828 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12830 /* When windows_or_buffers_changed is non-zero, we can't rely on
12831 the window end being valid, so set it to nil there. */
12832 if (windows_or_buffers_changed)
12834 /* If window starts on a continuation line, maybe adjust the
12835 window start in case the window's width changed. */
12836 if (XMARKER (w->start)->buffer == current_buffer)
12837 compute_window_start_on_continuation_line (w);
12839 w->window_end_valid = Qnil;
12842 /* Some sanity checks. */
12843 CHECK_WINDOW_END (w);
12844 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12845 abort ();
12846 if (BYTEPOS (opoint) < CHARPOS (opoint))
12847 abort ();
12849 /* If %c is in mode line, update it if needed. */
12850 if (!NILP (w->column_number_displayed)
12851 /* This alternative quickly identifies a common case
12852 where no change is needed. */
12853 && !(PT == XFASTINT (w->last_point)
12854 && XFASTINT (w->last_modified) >= MODIFF
12855 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12856 && (XFASTINT (w->column_number_displayed)
12857 != (int) current_column ())) /* iftc */
12858 update_mode_line = 1;
12860 /* Count number of windows showing the selected buffer. An indirect
12861 buffer counts as its base buffer. */
12862 if (!just_this_one_p)
12864 struct buffer *current_base, *window_base;
12865 current_base = current_buffer;
12866 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12867 if (current_base->base_buffer)
12868 current_base = current_base->base_buffer;
12869 if (window_base->base_buffer)
12870 window_base = window_base->base_buffer;
12871 if (current_base == window_base)
12872 buffer_shared++;
12875 /* Point refers normally to the selected window. For any other
12876 window, set up appropriate value. */
12877 if (!EQ (window, selected_window))
12879 int new_pt = XMARKER (w->pointm)->charpos;
12880 int new_pt_byte = marker_byte_position (w->pointm);
12881 if (new_pt < BEGV)
12883 new_pt = BEGV;
12884 new_pt_byte = BEGV_BYTE;
12885 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12887 else if (new_pt > (ZV - 1))
12889 new_pt = ZV;
12890 new_pt_byte = ZV_BYTE;
12891 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12894 /* We don't use SET_PT so that the point-motion hooks don't run. */
12895 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12898 /* If any of the character widths specified in the display table
12899 have changed, invalidate the width run cache. It's true that
12900 this may be a bit late to catch such changes, but the rest of
12901 redisplay goes (non-fatally) haywire when the display table is
12902 changed, so why should we worry about doing any better? */
12903 if (current_buffer->width_run_cache)
12905 struct Lisp_Char_Table *disptab = buffer_display_table ();
12907 if (! disptab_matches_widthtab (disptab,
12908 XVECTOR (current_buffer->width_table)))
12910 invalidate_region_cache (current_buffer,
12911 current_buffer->width_run_cache,
12912 BEG, Z);
12913 recompute_width_table (current_buffer, disptab);
12917 /* If window-start is screwed up, choose a new one. */
12918 if (XMARKER (w->start)->buffer != current_buffer)
12919 goto recenter;
12921 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12923 /* If someone specified a new starting point but did not insist,
12924 check whether it can be used. */
12925 if (!NILP (w->optional_new_start)
12926 && CHARPOS (startp) >= BEGV
12927 && CHARPOS (startp) <= ZV)
12929 w->optional_new_start = Qnil;
12930 start_display (&it, w, startp);
12931 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12932 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12933 if (IT_CHARPOS (it) == PT)
12934 w->force_start = Qt;
12935 /* IT may overshoot PT if text at PT is invisible. */
12936 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12937 w->force_start = Qt;
12940 /* Handle case where place to start displaying has been specified,
12941 unless the specified location is outside the accessible range. */
12942 if (!NILP (w->force_start)
12943 || w->frozen_window_start_p)
12945 /* We set this later on if we have to adjust point. */
12946 int new_vpos = -1;
12947 int val;
12949 w->force_start = Qnil;
12950 w->vscroll = 0;
12951 w->window_end_valid = Qnil;
12953 /* Forget any recorded base line for line number display. */
12954 if (!buffer_unchanged_p)
12955 w->base_line_number = Qnil;
12957 /* Redisplay the mode line. Select the buffer properly for that.
12958 Also, run the hook window-scroll-functions
12959 because we have scrolled. */
12960 /* Note, we do this after clearing force_start because
12961 if there's an error, it is better to forget about force_start
12962 than to get into an infinite loop calling the hook functions
12963 and having them get more errors. */
12964 if (!update_mode_line
12965 || ! NILP (Vwindow_scroll_functions))
12967 update_mode_line = 1;
12968 w->update_mode_line = Qt;
12969 startp = run_window_scroll_functions (window, startp);
12972 w->last_modified = make_number (0);
12973 w->last_overlay_modified = make_number (0);
12974 if (CHARPOS (startp) < BEGV)
12975 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12976 else if (CHARPOS (startp) > ZV)
12977 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12979 /* Redisplay, then check if cursor has been set during the
12980 redisplay. Give up if new fonts were loaded. */
12981 val = try_window (window, startp, 1);
12982 if (!val)
12984 w->force_start = Qt;
12985 clear_glyph_matrix (w->desired_matrix);
12986 goto need_larger_matrices;
12988 /* Point was outside the scroll margins. */
12989 if (val < 0)
12990 new_vpos = window_box_height (w) / 2;
12992 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12994 /* If point does not appear, try to move point so it does
12995 appear. The desired matrix has been built above, so we
12996 can use it here. */
12997 new_vpos = window_box_height (w) / 2;
13000 if (!cursor_row_fully_visible_p (w, 0, 0))
13002 /* Point does appear, but on a line partly visible at end of window.
13003 Move it back to a fully-visible line. */
13004 new_vpos = window_box_height (w);
13007 /* If we need to move point for either of the above reasons,
13008 now actually do it. */
13009 if (new_vpos >= 0)
13011 struct glyph_row *row;
13013 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13014 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13015 ++row;
13017 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13018 MATRIX_ROW_START_BYTEPOS (row));
13020 if (w != XWINDOW (selected_window))
13021 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13022 else if (current_buffer == old)
13023 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13025 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13027 /* If we are highlighting the region, then we just changed
13028 the region, so redisplay to show it. */
13029 if (!NILP (Vtransient_mark_mode)
13030 && !NILP (current_buffer->mark_active))
13032 clear_glyph_matrix (w->desired_matrix);
13033 if (!try_window (window, startp, 0))
13034 goto need_larger_matrices;
13038 #if GLYPH_DEBUG
13039 debug_method_add (w, "forced window start");
13040 #endif
13041 goto done;
13044 /* Handle case where text has not changed, only point, and it has
13045 not moved off the frame, and we are not retrying after hscroll.
13046 (current_matrix_up_to_date_p is nonzero when retrying.) */
13047 if (current_matrix_up_to_date_p
13048 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13049 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13051 switch (rc)
13053 case CURSOR_MOVEMENT_SUCCESS:
13054 used_current_matrix_p = 1;
13055 goto done;
13057 #if 0 /* try_cursor_movement never returns this value. */
13058 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13059 goto need_larger_matrices;
13060 #endif
13062 case CURSOR_MOVEMENT_MUST_SCROLL:
13063 goto try_to_scroll;
13065 default:
13066 abort ();
13069 /* If current starting point was originally the beginning of a line
13070 but no longer is, find a new starting point. */
13071 else if (!NILP (w->start_at_line_beg)
13072 && !(CHARPOS (startp) <= BEGV
13073 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13075 #if GLYPH_DEBUG
13076 debug_method_add (w, "recenter 1");
13077 #endif
13078 goto recenter;
13081 /* Try scrolling with try_window_id. Value is > 0 if update has
13082 been done, it is -1 if we know that the same window start will
13083 not work. It is 0 if unsuccessful for some other reason. */
13084 else if ((tem = try_window_id (w)) != 0)
13086 #if GLYPH_DEBUG
13087 debug_method_add (w, "try_window_id %d", tem);
13088 #endif
13090 if (fonts_changed_p)
13091 goto need_larger_matrices;
13092 if (tem > 0)
13093 goto done;
13095 /* Otherwise try_window_id has returned -1 which means that we
13096 don't want the alternative below this comment to execute. */
13098 else if (CHARPOS (startp) >= BEGV
13099 && CHARPOS (startp) <= ZV
13100 && PT >= CHARPOS (startp)
13101 && (CHARPOS (startp) < ZV
13102 /* Avoid starting at end of buffer. */
13103 || CHARPOS (startp) == BEGV
13104 || (XFASTINT (w->last_modified) >= MODIFF
13105 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13108 /* If first window line is a continuation line, and window start
13109 is inside the modified region, but the first change is before
13110 current window start, we must select a new window start.*/
13111 if (NILP (w->start_at_line_beg)
13112 && CHARPOS (startp) > BEGV)
13114 /* Make sure beg_unchanged and end_unchanged are up to date.
13115 Do it only if buffer has really changed. This may or may
13116 not have been done by try_window_id (see which) already. */
13117 if (MODIFF > SAVE_MODIFF
13118 /* This seems to happen sometimes after saving a buffer. */
13119 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13121 if (GPT - BEG < BEG_UNCHANGED)
13122 BEG_UNCHANGED = GPT - BEG;
13123 if (Z - GPT < END_UNCHANGED)
13124 END_UNCHANGED = Z - GPT;
13127 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13128 && CHARPOS (startp) <= Z - END_UNCHANGED)
13130 /* There doesn't seems to be a simple way to find a new
13131 window start that is near the old window start, so
13132 we just recenter. */
13133 goto recenter;
13137 #if GLYPH_DEBUG
13138 debug_method_add (w, "same window start");
13139 #endif
13141 /* Try to redisplay starting at same place as before.
13142 If point has not moved off frame, accept the results. */
13143 if (!current_matrix_up_to_date_p
13144 /* Don't use try_window_reusing_current_matrix in this case
13145 because a window scroll function can have changed the
13146 buffer. */
13147 || !NILP (Vwindow_scroll_functions)
13148 || MINI_WINDOW_P (w)
13149 || !(used_current_matrix_p
13150 = try_window_reusing_current_matrix (w)))
13152 IF_DEBUG (debug_method_add (w, "1"));
13153 if (try_window (window, startp, 1) < 0)
13154 /* -1 means we need to scroll.
13155 0 means we need new matrices, but fonts_changed_p
13156 is set in that case, so we will detect it below. */
13157 goto try_to_scroll;
13160 if (fonts_changed_p)
13161 goto need_larger_matrices;
13163 if (w->cursor.vpos >= 0)
13165 if (!just_this_one_p
13166 || current_buffer->clip_changed
13167 || BEG_UNCHANGED < CHARPOS (startp))
13168 /* Forget any recorded base line for line number display. */
13169 w->base_line_number = Qnil;
13171 if (!cursor_row_fully_visible_p (w, 1, 0))
13173 clear_glyph_matrix (w->desired_matrix);
13174 last_line_misfit = 1;
13176 /* Drop through and scroll. */
13177 else
13178 goto done;
13180 else
13181 clear_glyph_matrix (w->desired_matrix);
13184 try_to_scroll:
13186 w->last_modified = make_number (0);
13187 w->last_overlay_modified = make_number (0);
13189 /* Redisplay the mode line. Select the buffer properly for that. */
13190 if (!update_mode_line)
13192 update_mode_line = 1;
13193 w->update_mode_line = Qt;
13196 /* Try to scroll by specified few lines. */
13197 if ((scroll_conservatively
13198 || scroll_step
13199 || temp_scroll_step
13200 || NUMBERP (current_buffer->scroll_up_aggressively)
13201 || NUMBERP (current_buffer->scroll_down_aggressively))
13202 && !current_buffer->clip_changed
13203 && CHARPOS (startp) >= BEGV
13204 && CHARPOS (startp) <= ZV)
13206 /* The function returns -1 if new fonts were loaded, 1 if
13207 successful, 0 if not successful. */
13208 int rc = try_scrolling (window, just_this_one_p,
13209 scroll_conservatively,
13210 scroll_step,
13211 temp_scroll_step, last_line_misfit);
13212 switch (rc)
13214 case SCROLLING_SUCCESS:
13215 goto done;
13217 case SCROLLING_NEED_LARGER_MATRICES:
13218 goto need_larger_matrices;
13220 case SCROLLING_FAILED:
13221 break;
13223 default:
13224 abort ();
13228 /* Finally, just choose place to start which centers point */
13230 recenter:
13231 if (centering_position < 0)
13232 centering_position = window_box_height (w) / 2;
13234 #if GLYPH_DEBUG
13235 debug_method_add (w, "recenter");
13236 #endif
13238 /* w->vscroll = 0; */
13240 /* Forget any previously recorded base line for line number display. */
13241 if (!buffer_unchanged_p)
13242 w->base_line_number = Qnil;
13244 /* Move backward half the height of the window. */
13245 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13246 it.current_y = it.last_visible_y;
13247 move_it_vertically_backward (&it, centering_position);
13248 xassert (IT_CHARPOS (it) >= BEGV);
13250 /* The function move_it_vertically_backward may move over more
13251 than the specified y-distance. If it->w is small, e.g. a
13252 mini-buffer window, we may end up in front of the window's
13253 display area. Start displaying at the start of the line
13254 containing PT in this case. */
13255 if (it.current_y <= 0)
13257 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13258 move_it_vertically_backward (&it, 0);
13259 #if 0
13260 /* I think this assert is bogus if buffer contains
13261 invisible text or images. KFS. */
13262 xassert (IT_CHARPOS (it) <= PT);
13263 #endif
13264 it.current_y = 0;
13267 it.current_x = it.hpos = 0;
13269 /* Set startp here explicitly in case that helps avoid an infinite loop
13270 in case the window-scroll-functions functions get errors. */
13271 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13273 /* Run scroll hooks. */
13274 startp = run_window_scroll_functions (window, it.current.pos);
13276 /* Redisplay the window. */
13277 if (!current_matrix_up_to_date_p
13278 || windows_or_buffers_changed
13279 || cursor_type_changed
13280 /* Don't use try_window_reusing_current_matrix in this case
13281 because it can have changed the buffer. */
13282 || !NILP (Vwindow_scroll_functions)
13283 || !just_this_one_p
13284 || MINI_WINDOW_P (w)
13285 || !(used_current_matrix_p
13286 = try_window_reusing_current_matrix (w)))
13287 try_window (window, startp, 0);
13289 /* If new fonts have been loaded (due to fontsets), give up. We
13290 have to start a new redisplay since we need to re-adjust glyph
13291 matrices. */
13292 if (fonts_changed_p)
13293 goto need_larger_matrices;
13295 /* If cursor did not appear assume that the middle of the window is
13296 in the first line of the window. Do it again with the next line.
13297 (Imagine a window of height 100, displaying two lines of height
13298 60. Moving back 50 from it->last_visible_y will end in the first
13299 line.) */
13300 if (w->cursor.vpos < 0)
13302 if (!NILP (w->window_end_valid)
13303 && PT >= Z - XFASTINT (w->window_end_pos))
13305 clear_glyph_matrix (w->desired_matrix);
13306 move_it_by_lines (&it, 1, 0);
13307 try_window (window, it.current.pos, 0);
13309 else if (PT < IT_CHARPOS (it))
13311 clear_glyph_matrix (w->desired_matrix);
13312 move_it_by_lines (&it, -1, 0);
13313 try_window (window, it.current.pos, 0);
13315 else
13317 /* Not much we can do about it. */
13321 /* Consider the following case: Window starts at BEGV, there is
13322 invisible, intangible text at BEGV, so that display starts at
13323 some point START > BEGV. It can happen that we are called with
13324 PT somewhere between BEGV and START. Try to handle that case. */
13325 if (w->cursor.vpos < 0)
13327 struct glyph_row *row = w->current_matrix->rows;
13328 if (row->mode_line_p)
13329 ++row;
13330 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13333 if (!cursor_row_fully_visible_p (w, 0, 0))
13335 /* If vscroll is enabled, disable it and try again. */
13336 if (w->vscroll)
13338 w->vscroll = 0;
13339 clear_glyph_matrix (w->desired_matrix);
13340 goto recenter;
13343 /* If centering point failed to make the whole line visible,
13344 put point at the top instead. That has to make the whole line
13345 visible, if it can be done. */
13346 if (centering_position == 0)
13347 goto done;
13349 clear_glyph_matrix (w->desired_matrix);
13350 centering_position = 0;
13351 goto recenter;
13354 done:
13356 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13357 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13358 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13359 ? Qt : Qnil);
13361 /* Display the mode line, if we must. */
13362 if ((update_mode_line
13363 /* If window not full width, must redo its mode line
13364 if (a) the window to its side is being redone and
13365 (b) we do a frame-based redisplay. This is a consequence
13366 of how inverted lines are drawn in frame-based redisplay. */
13367 || (!just_this_one_p
13368 && !FRAME_WINDOW_P (f)
13369 && !WINDOW_FULL_WIDTH_P (w))
13370 /* Line number to display. */
13371 || INTEGERP (w->base_line_pos)
13372 /* Column number is displayed and different from the one displayed. */
13373 || (!NILP (w->column_number_displayed)
13374 && (XFASTINT (w->column_number_displayed)
13375 != (int) current_column ()))) /* iftc */
13376 /* This means that the window has a mode line. */
13377 && (WINDOW_WANTS_MODELINE_P (w)
13378 || WINDOW_WANTS_HEADER_LINE_P (w)))
13380 display_mode_lines (w);
13382 /* If mode line height has changed, arrange for a thorough
13383 immediate redisplay using the correct mode line height. */
13384 if (WINDOW_WANTS_MODELINE_P (w)
13385 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13387 fonts_changed_p = 1;
13388 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13389 = DESIRED_MODE_LINE_HEIGHT (w);
13392 /* If top line height has changed, arrange for a thorough
13393 immediate redisplay using the correct mode line height. */
13394 if (WINDOW_WANTS_HEADER_LINE_P (w)
13395 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13397 fonts_changed_p = 1;
13398 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13399 = DESIRED_HEADER_LINE_HEIGHT (w);
13402 if (fonts_changed_p)
13403 goto need_larger_matrices;
13406 if (!line_number_displayed
13407 && !BUFFERP (w->base_line_pos))
13409 w->base_line_pos = Qnil;
13410 w->base_line_number = Qnil;
13413 finish_menu_bars:
13415 /* When we reach a frame's selected window, redo the frame's menu bar. */
13416 if (update_mode_line
13417 && EQ (FRAME_SELECTED_WINDOW (f), window))
13419 int redisplay_menu_p = 0;
13420 int redisplay_tool_bar_p = 0;
13422 if (FRAME_WINDOW_P (f))
13424 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13425 || defined (USE_GTK)
13426 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13427 #else
13428 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13429 #endif
13431 else
13432 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13434 if (redisplay_menu_p)
13435 display_menu_bar (w);
13437 #ifdef HAVE_WINDOW_SYSTEM
13438 #ifdef USE_GTK
13439 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13440 #else
13441 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13442 && (FRAME_TOOL_BAR_LINES (f) > 0
13443 || auto_resize_tool_bars_p);
13445 #endif
13447 if (redisplay_tool_bar_p)
13448 redisplay_tool_bar (f);
13449 #endif
13452 #ifdef HAVE_WINDOW_SYSTEM
13453 if (FRAME_WINDOW_P (f)
13454 && update_window_fringes (w, (just_this_one_p
13455 || (!used_current_matrix_p && !overlay_arrow_seen)
13456 || w->pseudo_window_p)))
13458 update_begin (f);
13459 BLOCK_INPUT;
13460 if (draw_window_fringes (w, 1))
13461 x_draw_vertical_border (w);
13462 UNBLOCK_INPUT;
13463 update_end (f);
13465 #endif /* HAVE_WINDOW_SYSTEM */
13467 /* We go to this label, with fonts_changed_p nonzero,
13468 if it is necessary to try again using larger glyph matrices.
13469 We have to redeem the scroll bar even in this case,
13470 because the loop in redisplay_internal expects that. */
13471 need_larger_matrices:
13473 finish_scroll_bars:
13475 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13477 /* Set the thumb's position and size. */
13478 set_vertical_scroll_bar (w);
13480 /* Note that we actually used the scroll bar attached to this
13481 window, so it shouldn't be deleted at the end of redisplay. */
13482 redeem_scroll_bar_hook (w);
13485 /* Restore current_buffer and value of point in it. */
13486 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13487 set_buffer_internal_1 (old);
13488 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13490 unbind_to (count, Qnil);
13494 /* Build the complete desired matrix of WINDOW with a window start
13495 buffer position POS.
13497 Value is 1 if successful. It is zero if fonts were loaded during
13498 redisplay which makes re-adjusting glyph matrices necessary, and -1
13499 if point would appear in the scroll margins.
13500 (We check that only if CHECK_MARGINS is nonzero. */
13503 try_window (window, pos, check_margins)
13504 Lisp_Object window;
13505 struct text_pos pos;
13506 int check_margins;
13508 struct window *w = XWINDOW (window);
13509 struct it it;
13510 struct glyph_row *last_text_row = NULL;
13512 /* Make POS the new window start. */
13513 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13515 /* Mark cursor position as unknown. No overlay arrow seen. */
13516 w->cursor.vpos = -1;
13517 overlay_arrow_seen = 0;
13519 /* Initialize iterator and info to start at POS. */
13520 start_display (&it, w, pos);
13522 /* Display all lines of W. */
13523 while (it.current_y < it.last_visible_y)
13525 if (display_line (&it))
13526 last_text_row = it.glyph_row - 1;
13527 if (fonts_changed_p)
13528 return 0;
13531 /* Don't let the cursor end in the scroll margins. */
13532 if (check_margins
13533 && !MINI_WINDOW_P (w))
13535 int this_scroll_margin;
13537 this_scroll_margin = max (0, scroll_margin);
13538 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13539 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13541 if ((w->cursor.y >= 0 /* not vscrolled */
13542 && w->cursor.y < this_scroll_margin
13543 && CHARPOS (pos) > BEGV
13544 && IT_CHARPOS (it) < ZV)
13545 /* rms: considering make_cursor_line_fully_visible_p here
13546 seems to give wrong results. We don't want to recenter
13547 when the last line is partly visible, we want to allow
13548 that case to be handled in the usual way. */
13549 || (w->cursor.y + 1) > it.last_visible_y)
13551 w->cursor.vpos = -1;
13552 clear_glyph_matrix (w->desired_matrix);
13553 return -1;
13557 /* If bottom moved off end of frame, change mode line percentage. */
13558 if (XFASTINT (w->window_end_pos) <= 0
13559 && Z != IT_CHARPOS (it))
13560 w->update_mode_line = Qt;
13562 /* Set window_end_pos to the offset of the last character displayed
13563 on the window from the end of current_buffer. Set
13564 window_end_vpos to its row number. */
13565 if (last_text_row)
13567 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13568 w->window_end_bytepos
13569 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13570 w->window_end_pos
13571 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13572 w->window_end_vpos
13573 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13574 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13575 ->displays_text_p);
13577 else
13579 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13580 w->window_end_pos = make_number (Z - ZV);
13581 w->window_end_vpos = make_number (0);
13584 /* But that is not valid info until redisplay finishes. */
13585 w->window_end_valid = Qnil;
13586 return 1;
13591 /************************************************************************
13592 Window redisplay reusing current matrix when buffer has not changed
13593 ************************************************************************/
13595 /* Try redisplay of window W showing an unchanged buffer with a
13596 different window start than the last time it was displayed by
13597 reusing its current matrix. Value is non-zero if successful.
13598 W->start is the new window start. */
13600 static int
13601 try_window_reusing_current_matrix (w)
13602 struct window *w;
13604 struct frame *f = XFRAME (w->frame);
13605 struct glyph_row *row, *bottom_row;
13606 struct it it;
13607 struct run run;
13608 struct text_pos start, new_start;
13609 int nrows_scrolled, i;
13610 struct glyph_row *last_text_row;
13611 struct glyph_row *last_reused_text_row;
13612 struct glyph_row *start_row;
13613 int start_vpos, min_y, max_y;
13615 #if GLYPH_DEBUG
13616 if (inhibit_try_window_reusing)
13617 return 0;
13618 #endif
13620 if (/* This function doesn't handle terminal frames. */
13621 !FRAME_WINDOW_P (f)
13622 /* Don't try to reuse the display if windows have been split
13623 or such. */
13624 || windows_or_buffers_changed
13625 || cursor_type_changed)
13626 return 0;
13628 /* Can't do this if region may have changed. */
13629 if ((!NILP (Vtransient_mark_mode)
13630 && !NILP (current_buffer->mark_active))
13631 || !NILP (w->region_showing)
13632 || !NILP (Vshow_trailing_whitespace))
13633 return 0;
13635 /* If top-line visibility has changed, give up. */
13636 if (WINDOW_WANTS_HEADER_LINE_P (w)
13637 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13638 return 0;
13640 /* Give up if old or new display is scrolled vertically. We could
13641 make this function handle this, but right now it doesn't. */
13642 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13643 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13644 return 0;
13646 /* The variable new_start now holds the new window start. The old
13647 start `start' can be determined from the current matrix. */
13648 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13649 start = start_row->start.pos;
13650 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13652 /* Clear the desired matrix for the display below. */
13653 clear_glyph_matrix (w->desired_matrix);
13655 if (CHARPOS (new_start) <= CHARPOS (start))
13657 int first_row_y;
13659 /* Don't use this method if the display starts with an ellipsis
13660 displayed for invisible text. It's not easy to handle that case
13661 below, and it's certainly not worth the effort since this is
13662 not a frequent case. */
13663 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13664 return 0;
13666 IF_DEBUG (debug_method_add (w, "twu1"));
13668 /* Display up to a row that can be reused. The variable
13669 last_text_row is set to the last row displayed that displays
13670 text. Note that it.vpos == 0 if or if not there is a
13671 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13672 start_display (&it, w, new_start);
13673 first_row_y = it.current_y;
13674 w->cursor.vpos = -1;
13675 last_text_row = last_reused_text_row = NULL;
13677 while (it.current_y < it.last_visible_y
13678 && !fonts_changed_p)
13680 /* If we have reached into the characters in the START row,
13681 that means the line boundaries have changed. So we
13682 can't start copying with the row START. Maybe it will
13683 work to start copying with the following row. */
13684 while (IT_CHARPOS (it) > CHARPOS (start))
13686 /* Advance to the next row as the "start". */
13687 start_row++;
13688 start = start_row->start.pos;
13689 /* If there are no more rows to try, or just one, give up. */
13690 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13691 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13692 || CHARPOS (start) == ZV)
13694 clear_glyph_matrix (w->desired_matrix);
13695 return 0;
13698 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13700 /* If we have reached alignment,
13701 we can copy the rest of the rows. */
13702 if (IT_CHARPOS (it) == CHARPOS (start))
13703 break;
13705 if (display_line (&it))
13706 last_text_row = it.glyph_row - 1;
13709 /* A value of current_y < last_visible_y means that we stopped
13710 at the previous window start, which in turn means that we
13711 have at least one reusable row. */
13712 if (it.current_y < it.last_visible_y)
13714 /* IT.vpos always starts from 0; it counts text lines. */
13715 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13717 /* Find PT if not already found in the lines displayed. */
13718 if (w->cursor.vpos < 0)
13720 int dy = it.current_y - start_row->y;
13722 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13723 row = row_containing_pos (w, PT, row, NULL, dy);
13724 if (row)
13725 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13726 dy, nrows_scrolled);
13727 else
13729 clear_glyph_matrix (w->desired_matrix);
13730 return 0;
13734 /* Scroll the display. Do it before the current matrix is
13735 changed. The problem here is that update has not yet
13736 run, i.e. part of the current matrix is not up to date.
13737 scroll_run_hook will clear the cursor, and use the
13738 current matrix to get the height of the row the cursor is
13739 in. */
13740 run.current_y = start_row->y;
13741 run.desired_y = it.current_y;
13742 run.height = it.last_visible_y - it.current_y;
13744 if (run.height > 0 && run.current_y != run.desired_y)
13746 update_begin (f);
13747 rif->update_window_begin_hook (w);
13748 rif->clear_window_mouse_face (w);
13749 rif->scroll_run_hook (w, &run);
13750 rif->update_window_end_hook (w, 0, 0);
13751 update_end (f);
13754 /* Shift current matrix down by nrows_scrolled lines. */
13755 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13756 rotate_matrix (w->current_matrix,
13757 start_vpos,
13758 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13759 nrows_scrolled);
13761 /* Disable lines that must be updated. */
13762 for (i = 0; i < it.vpos; ++i)
13763 (start_row + i)->enabled_p = 0;
13765 /* Re-compute Y positions. */
13766 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13767 max_y = it.last_visible_y;
13768 for (row = start_row + nrows_scrolled;
13769 row < bottom_row;
13770 ++row)
13772 row->y = it.current_y;
13773 row->visible_height = row->height;
13775 if (row->y < min_y)
13776 row->visible_height -= min_y - row->y;
13777 if (row->y + row->height > max_y)
13778 row->visible_height -= row->y + row->height - max_y;
13779 row->redraw_fringe_bitmaps_p = 1;
13781 it.current_y += row->height;
13783 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13784 last_reused_text_row = row;
13785 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13786 break;
13789 /* Disable lines in the current matrix which are now
13790 below the window. */
13791 for (++row; row < bottom_row; ++row)
13792 row->enabled_p = row->mode_line_p = 0;
13795 /* Update window_end_pos etc.; last_reused_text_row is the last
13796 reused row from the current matrix containing text, if any.
13797 The value of last_text_row is the last displayed line
13798 containing text. */
13799 if (last_reused_text_row)
13801 w->window_end_bytepos
13802 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13803 w->window_end_pos
13804 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13805 w->window_end_vpos
13806 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13807 w->current_matrix));
13809 else if (last_text_row)
13811 w->window_end_bytepos
13812 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13813 w->window_end_pos
13814 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13815 w->window_end_vpos
13816 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13818 else
13820 /* This window must be completely empty. */
13821 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13822 w->window_end_pos = make_number (Z - ZV);
13823 w->window_end_vpos = make_number (0);
13825 w->window_end_valid = Qnil;
13827 /* Update hint: don't try scrolling again in update_window. */
13828 w->desired_matrix->no_scrolling_p = 1;
13830 #if GLYPH_DEBUG
13831 debug_method_add (w, "try_window_reusing_current_matrix 1");
13832 #endif
13833 return 1;
13835 else if (CHARPOS (new_start) > CHARPOS (start))
13837 struct glyph_row *pt_row, *row;
13838 struct glyph_row *first_reusable_row;
13839 struct glyph_row *first_row_to_display;
13840 int dy;
13841 int yb = window_text_bottom_y (w);
13843 /* Find the row starting at new_start, if there is one. Don't
13844 reuse a partially visible line at the end. */
13845 first_reusable_row = start_row;
13846 while (first_reusable_row->enabled_p
13847 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13848 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13849 < CHARPOS (new_start)))
13850 ++first_reusable_row;
13852 /* Give up if there is no row to reuse. */
13853 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13854 || !first_reusable_row->enabled_p
13855 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13856 != CHARPOS (new_start)))
13857 return 0;
13859 /* We can reuse fully visible rows beginning with
13860 first_reusable_row to the end of the window. Set
13861 first_row_to_display to the first row that cannot be reused.
13862 Set pt_row to the row containing point, if there is any. */
13863 pt_row = NULL;
13864 for (first_row_to_display = first_reusable_row;
13865 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13866 ++first_row_to_display)
13868 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13869 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13870 pt_row = first_row_to_display;
13873 /* Start displaying at the start of first_row_to_display. */
13874 xassert (first_row_to_display->y < yb);
13875 init_to_row_start (&it, w, first_row_to_display);
13877 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13878 - start_vpos);
13879 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13880 - nrows_scrolled);
13881 it.current_y = (first_row_to_display->y - first_reusable_row->y
13882 + WINDOW_HEADER_LINE_HEIGHT (w));
13884 /* Display lines beginning with first_row_to_display in the
13885 desired matrix. Set last_text_row to the last row displayed
13886 that displays text. */
13887 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13888 if (pt_row == NULL)
13889 w->cursor.vpos = -1;
13890 last_text_row = NULL;
13891 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13892 if (display_line (&it))
13893 last_text_row = it.glyph_row - 1;
13895 /* Give up If point isn't in a row displayed or reused. */
13896 if (w->cursor.vpos < 0)
13898 clear_glyph_matrix (w->desired_matrix);
13899 return 0;
13902 /* If point is in a reused row, adjust y and vpos of the cursor
13903 position. */
13904 if (pt_row)
13906 w->cursor.vpos -= nrows_scrolled;
13907 w->cursor.y -= first_reusable_row->y - start_row->y;
13910 /* Scroll the display. */
13911 run.current_y = first_reusable_row->y;
13912 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13913 run.height = it.last_visible_y - run.current_y;
13914 dy = run.current_y - run.desired_y;
13916 if (run.height)
13918 update_begin (f);
13919 rif->update_window_begin_hook (w);
13920 rif->clear_window_mouse_face (w);
13921 rif->scroll_run_hook (w, &run);
13922 rif->update_window_end_hook (w, 0, 0);
13923 update_end (f);
13926 /* Adjust Y positions of reused rows. */
13927 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13928 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13929 max_y = it.last_visible_y;
13930 for (row = first_reusable_row; row < first_row_to_display; ++row)
13932 row->y -= dy;
13933 row->visible_height = row->height;
13934 if (row->y < min_y)
13935 row->visible_height -= min_y - row->y;
13936 if (row->y + row->height > max_y)
13937 row->visible_height -= row->y + row->height - max_y;
13938 row->redraw_fringe_bitmaps_p = 1;
13941 /* Scroll the current matrix. */
13942 xassert (nrows_scrolled > 0);
13943 rotate_matrix (w->current_matrix,
13944 start_vpos,
13945 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13946 -nrows_scrolled);
13948 /* Disable rows not reused. */
13949 for (row -= nrows_scrolled; row < bottom_row; ++row)
13950 row->enabled_p = 0;
13952 /* Point may have moved to a different line, so we cannot assume that
13953 the previous cursor position is valid; locate the correct row. */
13954 if (pt_row)
13956 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13957 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13958 row++)
13960 w->cursor.vpos++;
13961 w->cursor.y = row->y;
13963 if (row < bottom_row)
13965 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13966 while (glyph->charpos < PT)
13968 w->cursor.hpos++;
13969 w->cursor.x += glyph->pixel_width;
13970 glyph++;
13975 /* Adjust window end. A null value of last_text_row means that
13976 the window end is in reused rows which in turn means that
13977 only its vpos can have changed. */
13978 if (last_text_row)
13980 w->window_end_bytepos
13981 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13982 w->window_end_pos
13983 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13984 w->window_end_vpos
13985 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13987 else
13989 w->window_end_vpos
13990 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13993 w->window_end_valid = Qnil;
13994 w->desired_matrix->no_scrolling_p = 1;
13996 #if GLYPH_DEBUG
13997 debug_method_add (w, "try_window_reusing_current_matrix 2");
13998 #endif
13999 return 1;
14002 return 0;
14007 /************************************************************************
14008 Window redisplay reusing current matrix when buffer has changed
14009 ************************************************************************/
14011 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14012 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14013 int *, int *));
14014 static struct glyph_row *
14015 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14016 struct glyph_row *));
14019 /* Return the last row in MATRIX displaying text. If row START is
14020 non-null, start searching with that row. IT gives the dimensions
14021 of the display. Value is null if matrix is empty; otherwise it is
14022 a pointer to the row found. */
14024 static struct glyph_row *
14025 find_last_row_displaying_text (matrix, it, start)
14026 struct glyph_matrix *matrix;
14027 struct it *it;
14028 struct glyph_row *start;
14030 struct glyph_row *row, *row_found;
14032 /* Set row_found to the last row in IT->w's current matrix
14033 displaying text. The loop looks funny but think of partially
14034 visible lines. */
14035 row_found = NULL;
14036 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14037 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14039 xassert (row->enabled_p);
14040 row_found = row;
14041 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14042 break;
14043 ++row;
14046 return row_found;
14050 /* Return the last row in the current matrix of W that is not affected
14051 by changes at the start of current_buffer that occurred since W's
14052 current matrix was built. Value is null if no such row exists.
14054 BEG_UNCHANGED us the number of characters unchanged at the start of
14055 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14056 first changed character in current_buffer. Characters at positions <
14057 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14058 when the current matrix was built. */
14060 static struct glyph_row *
14061 find_last_unchanged_at_beg_row (w)
14062 struct window *w;
14064 int first_changed_pos = BEG + BEG_UNCHANGED;
14065 struct glyph_row *row;
14066 struct glyph_row *row_found = NULL;
14067 int yb = window_text_bottom_y (w);
14069 /* Find the last row displaying unchanged text. */
14070 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14071 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14072 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14074 if (/* If row ends before first_changed_pos, it is unchanged,
14075 except in some case. */
14076 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14077 /* When row ends in ZV and we write at ZV it is not
14078 unchanged. */
14079 && !row->ends_at_zv_p
14080 /* When first_changed_pos is the end of a continued line,
14081 row is not unchanged because it may be no longer
14082 continued. */
14083 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14084 && (row->continued_p
14085 || row->exact_window_width_line_p)))
14086 row_found = row;
14088 /* Stop if last visible row. */
14089 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14090 break;
14092 ++row;
14095 return row_found;
14099 /* Find the first glyph row in the current matrix of W that is not
14100 affected by changes at the end of current_buffer since the
14101 time W's current matrix was built.
14103 Return in *DELTA the number of chars by which buffer positions in
14104 unchanged text at the end of current_buffer must be adjusted.
14106 Return in *DELTA_BYTES the corresponding number of bytes.
14108 Value is null if no such row exists, i.e. all rows are affected by
14109 changes. */
14111 static struct glyph_row *
14112 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14113 struct window *w;
14114 int *delta, *delta_bytes;
14116 struct glyph_row *row;
14117 struct glyph_row *row_found = NULL;
14119 *delta = *delta_bytes = 0;
14121 /* Display must not have been paused, otherwise the current matrix
14122 is not up to date. */
14123 if (NILP (w->window_end_valid))
14124 abort ();
14126 /* A value of window_end_pos >= END_UNCHANGED means that the window
14127 end is in the range of changed text. If so, there is no
14128 unchanged row at the end of W's current matrix. */
14129 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14130 return NULL;
14132 /* Set row to the last row in W's current matrix displaying text. */
14133 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14135 /* If matrix is entirely empty, no unchanged row exists. */
14136 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14138 /* The value of row is the last glyph row in the matrix having a
14139 meaningful buffer position in it. The end position of row
14140 corresponds to window_end_pos. This allows us to translate
14141 buffer positions in the current matrix to current buffer
14142 positions for characters not in changed text. */
14143 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14144 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14145 int last_unchanged_pos, last_unchanged_pos_old;
14146 struct glyph_row *first_text_row
14147 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14149 *delta = Z - Z_old;
14150 *delta_bytes = Z_BYTE - Z_BYTE_old;
14152 /* Set last_unchanged_pos to the buffer position of the last
14153 character in the buffer that has not been changed. Z is the
14154 index + 1 of the last character in current_buffer, i.e. by
14155 subtracting END_UNCHANGED we get the index of the last
14156 unchanged character, and we have to add BEG to get its buffer
14157 position. */
14158 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14159 last_unchanged_pos_old = last_unchanged_pos - *delta;
14161 /* Search backward from ROW for a row displaying a line that
14162 starts at a minimum position >= last_unchanged_pos_old. */
14163 for (; row > first_text_row; --row)
14165 /* This used to abort, but it can happen.
14166 It is ok to just stop the search instead here. KFS. */
14167 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14168 break;
14170 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14171 row_found = row;
14175 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14176 abort ();
14178 return row_found;
14182 /* Make sure that glyph rows in the current matrix of window W
14183 reference the same glyph memory as corresponding rows in the
14184 frame's frame matrix. This function is called after scrolling W's
14185 current matrix on a terminal frame in try_window_id and
14186 try_window_reusing_current_matrix. */
14188 static void
14189 sync_frame_with_window_matrix_rows (w)
14190 struct window *w;
14192 struct frame *f = XFRAME (w->frame);
14193 struct glyph_row *window_row, *window_row_end, *frame_row;
14195 /* Preconditions: W must be a leaf window and full-width. Its frame
14196 must have a frame matrix. */
14197 xassert (NILP (w->hchild) && NILP (w->vchild));
14198 xassert (WINDOW_FULL_WIDTH_P (w));
14199 xassert (!FRAME_WINDOW_P (f));
14201 /* If W is a full-width window, glyph pointers in W's current matrix
14202 have, by definition, to be the same as glyph pointers in the
14203 corresponding frame matrix. Note that frame matrices have no
14204 marginal areas (see build_frame_matrix). */
14205 window_row = w->current_matrix->rows;
14206 window_row_end = window_row + w->current_matrix->nrows;
14207 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14208 while (window_row < window_row_end)
14210 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14211 struct glyph *end = window_row->glyphs[LAST_AREA];
14213 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14214 frame_row->glyphs[TEXT_AREA] = start;
14215 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14216 frame_row->glyphs[LAST_AREA] = end;
14218 /* Disable frame rows whose corresponding window rows have
14219 been disabled in try_window_id. */
14220 if (!window_row->enabled_p)
14221 frame_row->enabled_p = 0;
14223 ++window_row, ++frame_row;
14228 /* Find the glyph row in window W containing CHARPOS. Consider all
14229 rows between START and END (not inclusive). END null means search
14230 all rows to the end of the display area of W. Value is the row
14231 containing CHARPOS or null. */
14233 struct glyph_row *
14234 row_containing_pos (w, charpos, start, end, dy)
14235 struct window *w;
14236 int charpos;
14237 struct glyph_row *start, *end;
14238 int dy;
14240 struct glyph_row *row = start;
14241 int last_y;
14243 /* If we happen to start on a header-line, skip that. */
14244 if (row->mode_line_p)
14245 ++row;
14247 if ((end && row >= end) || !row->enabled_p)
14248 return NULL;
14250 last_y = window_text_bottom_y (w) - dy;
14252 while (1)
14254 /* Give up if we have gone too far. */
14255 if (end && row >= end)
14256 return NULL;
14257 /* This formerly returned if they were equal.
14258 I think that both quantities are of a "last plus one" type;
14259 if so, when they are equal, the row is within the screen. -- rms. */
14260 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14261 return NULL;
14263 /* If it is in this row, return this row. */
14264 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14265 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14266 /* The end position of a row equals the start
14267 position of the next row. If CHARPOS is there, we
14268 would rather display it in the next line, except
14269 when this line ends in ZV. */
14270 && !row->ends_at_zv_p
14271 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14272 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14273 return row;
14274 ++row;
14279 /* Try to redisplay window W by reusing its existing display. W's
14280 current matrix must be up to date when this function is called,
14281 i.e. window_end_valid must not be nil.
14283 Value is
14285 1 if display has been updated
14286 0 if otherwise unsuccessful
14287 -1 if redisplay with same window start is known not to succeed
14289 The following steps are performed:
14291 1. Find the last row in the current matrix of W that is not
14292 affected by changes at the start of current_buffer. If no such row
14293 is found, give up.
14295 2. Find the first row in W's current matrix that is not affected by
14296 changes at the end of current_buffer. Maybe there is no such row.
14298 3. Display lines beginning with the row + 1 found in step 1 to the
14299 row found in step 2 or, if step 2 didn't find a row, to the end of
14300 the window.
14302 4. If cursor is not known to appear on the window, give up.
14304 5. If display stopped at the row found in step 2, scroll the
14305 display and current matrix as needed.
14307 6. Maybe display some lines at the end of W, if we must. This can
14308 happen under various circumstances, like a partially visible line
14309 becoming fully visible, or because newly displayed lines are displayed
14310 in smaller font sizes.
14312 7. Update W's window end information. */
14314 static int
14315 try_window_id (w)
14316 struct window *w;
14318 struct frame *f = XFRAME (w->frame);
14319 struct glyph_matrix *current_matrix = w->current_matrix;
14320 struct glyph_matrix *desired_matrix = w->desired_matrix;
14321 struct glyph_row *last_unchanged_at_beg_row;
14322 struct glyph_row *first_unchanged_at_end_row;
14323 struct glyph_row *row;
14324 struct glyph_row *bottom_row;
14325 int bottom_vpos;
14326 struct it it;
14327 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14328 struct text_pos start_pos;
14329 struct run run;
14330 int first_unchanged_at_end_vpos = 0;
14331 struct glyph_row *last_text_row, *last_text_row_at_end;
14332 struct text_pos start;
14333 int first_changed_charpos, last_changed_charpos;
14335 #if GLYPH_DEBUG
14336 if (inhibit_try_window_id)
14337 return 0;
14338 #endif
14340 /* This is handy for debugging. */
14341 #if 0
14342 #define GIVE_UP(X) \
14343 do { \
14344 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14345 return 0; \
14346 } while (0)
14347 #else
14348 #define GIVE_UP(X) return 0
14349 #endif
14351 SET_TEXT_POS_FROM_MARKER (start, w->start);
14353 /* Don't use this for mini-windows because these can show
14354 messages and mini-buffers, and we don't handle that here. */
14355 if (MINI_WINDOW_P (w))
14356 GIVE_UP (1);
14358 /* This flag is used to prevent redisplay optimizations. */
14359 if (windows_or_buffers_changed || cursor_type_changed)
14360 GIVE_UP (2);
14362 /* Verify that narrowing has not changed.
14363 Also verify that we were not told to prevent redisplay optimizations.
14364 It would be nice to further
14365 reduce the number of cases where this prevents try_window_id. */
14366 if (current_buffer->clip_changed
14367 || current_buffer->prevent_redisplay_optimizations_p)
14368 GIVE_UP (3);
14370 /* Window must either use window-based redisplay or be full width. */
14371 if (!FRAME_WINDOW_P (f)
14372 && (!line_ins_del_ok
14373 || !WINDOW_FULL_WIDTH_P (w)))
14374 GIVE_UP (4);
14376 /* Give up if point is not known NOT to appear in W. */
14377 if (PT < CHARPOS (start))
14378 GIVE_UP (5);
14380 /* Another way to prevent redisplay optimizations. */
14381 if (XFASTINT (w->last_modified) == 0)
14382 GIVE_UP (6);
14384 /* Verify that window is not hscrolled. */
14385 if (XFASTINT (w->hscroll) != 0)
14386 GIVE_UP (7);
14388 /* Verify that display wasn't paused. */
14389 if (NILP (w->window_end_valid))
14390 GIVE_UP (8);
14392 /* Can't use this if highlighting a region because a cursor movement
14393 will do more than just set the cursor. */
14394 if (!NILP (Vtransient_mark_mode)
14395 && !NILP (current_buffer->mark_active))
14396 GIVE_UP (9);
14398 /* Likewise if highlighting trailing whitespace. */
14399 if (!NILP (Vshow_trailing_whitespace))
14400 GIVE_UP (11);
14402 /* Likewise if showing a region. */
14403 if (!NILP (w->region_showing))
14404 GIVE_UP (10);
14406 /* Can use this if overlay arrow position and or string have changed. */
14407 if (overlay_arrows_changed_p ())
14408 GIVE_UP (12);
14411 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14412 only if buffer has really changed. The reason is that the gap is
14413 initially at Z for freshly visited files. The code below would
14414 set end_unchanged to 0 in that case. */
14415 if (MODIFF > SAVE_MODIFF
14416 /* This seems to happen sometimes after saving a buffer. */
14417 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14419 if (GPT - BEG < BEG_UNCHANGED)
14420 BEG_UNCHANGED = GPT - BEG;
14421 if (Z - GPT < END_UNCHANGED)
14422 END_UNCHANGED = Z - GPT;
14425 /* The position of the first and last character that has been changed. */
14426 first_changed_charpos = BEG + BEG_UNCHANGED;
14427 last_changed_charpos = Z - END_UNCHANGED;
14429 /* If window starts after a line end, and the last change is in
14430 front of that newline, then changes don't affect the display.
14431 This case happens with stealth-fontification. Note that although
14432 the display is unchanged, glyph positions in the matrix have to
14433 be adjusted, of course. */
14434 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14435 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14436 && ((last_changed_charpos < CHARPOS (start)
14437 && CHARPOS (start) == BEGV)
14438 || (last_changed_charpos < CHARPOS (start) - 1
14439 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14441 int Z_old, delta, Z_BYTE_old, delta_bytes;
14442 struct glyph_row *r0;
14444 /* Compute how many chars/bytes have been added to or removed
14445 from the buffer. */
14446 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14447 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14448 delta = Z - Z_old;
14449 delta_bytes = Z_BYTE - Z_BYTE_old;
14451 /* Give up if PT is not in the window. Note that it already has
14452 been checked at the start of try_window_id that PT is not in
14453 front of the window start. */
14454 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14455 GIVE_UP (13);
14457 /* If window start is unchanged, we can reuse the whole matrix
14458 as is, after adjusting glyph positions. No need to compute
14459 the window end again, since its offset from Z hasn't changed. */
14460 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14461 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14462 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14463 /* PT must not be in a partially visible line. */
14464 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14465 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14467 /* Adjust positions in the glyph matrix. */
14468 if (delta || delta_bytes)
14470 struct glyph_row *r1
14471 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14472 increment_matrix_positions (w->current_matrix,
14473 MATRIX_ROW_VPOS (r0, current_matrix),
14474 MATRIX_ROW_VPOS (r1, current_matrix),
14475 delta, delta_bytes);
14478 /* Set the cursor. */
14479 row = row_containing_pos (w, PT, r0, NULL, 0);
14480 if (row)
14481 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14482 else
14483 abort ();
14484 return 1;
14488 /* Handle the case that changes are all below what is displayed in
14489 the window, and that PT is in the window. This shortcut cannot
14490 be taken if ZV is visible in the window, and text has been added
14491 there that is visible in the window. */
14492 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14493 /* ZV is not visible in the window, or there are no
14494 changes at ZV, actually. */
14495 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14496 || first_changed_charpos == last_changed_charpos))
14498 struct glyph_row *r0;
14500 /* Give up if PT is not in the window. Note that it already has
14501 been checked at the start of try_window_id that PT is not in
14502 front of the window start. */
14503 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14504 GIVE_UP (14);
14506 /* If window start is unchanged, we can reuse the whole matrix
14507 as is, without changing glyph positions since no text has
14508 been added/removed in front of the window end. */
14509 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14510 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14511 /* PT must not be in a partially visible line. */
14512 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14513 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14515 /* We have to compute the window end anew since text
14516 can have been added/removed after it. */
14517 w->window_end_pos
14518 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14519 w->window_end_bytepos
14520 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14522 /* Set the cursor. */
14523 row = row_containing_pos (w, PT, r0, NULL, 0);
14524 if (row)
14525 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14526 else
14527 abort ();
14528 return 2;
14532 /* Give up if window start is in the changed area.
14534 The condition used to read
14536 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14538 but why that was tested escapes me at the moment. */
14539 if (CHARPOS (start) >= first_changed_charpos
14540 && CHARPOS (start) <= last_changed_charpos)
14541 GIVE_UP (15);
14543 /* Check that window start agrees with the start of the first glyph
14544 row in its current matrix. Check this after we know the window
14545 start is not in changed text, otherwise positions would not be
14546 comparable. */
14547 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14548 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14549 GIVE_UP (16);
14551 /* Give up if the window ends in strings. Overlay strings
14552 at the end are difficult to handle, so don't try. */
14553 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14554 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14555 GIVE_UP (20);
14557 /* Compute the position at which we have to start displaying new
14558 lines. Some of the lines at the top of the window might be
14559 reusable because they are not displaying changed text. Find the
14560 last row in W's current matrix not affected by changes at the
14561 start of current_buffer. Value is null if changes start in the
14562 first line of window. */
14563 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14564 if (last_unchanged_at_beg_row)
14566 /* Avoid starting to display in the moddle of a character, a TAB
14567 for instance. This is easier than to set up the iterator
14568 exactly, and it's not a frequent case, so the additional
14569 effort wouldn't really pay off. */
14570 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14571 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14572 && last_unchanged_at_beg_row > w->current_matrix->rows)
14573 --last_unchanged_at_beg_row;
14575 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14576 GIVE_UP (17);
14578 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14579 GIVE_UP (18);
14580 start_pos = it.current.pos;
14582 /* Start displaying new lines in the desired matrix at the same
14583 vpos we would use in the current matrix, i.e. below
14584 last_unchanged_at_beg_row. */
14585 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14586 current_matrix);
14587 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14588 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14590 xassert (it.hpos == 0 && it.current_x == 0);
14592 else
14594 /* There are no reusable lines at the start of the window.
14595 Start displaying in the first text line. */
14596 start_display (&it, w, start);
14597 it.vpos = it.first_vpos;
14598 start_pos = it.current.pos;
14601 /* Find the first row that is not affected by changes at the end of
14602 the buffer. Value will be null if there is no unchanged row, in
14603 which case we must redisplay to the end of the window. delta
14604 will be set to the value by which buffer positions beginning with
14605 first_unchanged_at_end_row have to be adjusted due to text
14606 changes. */
14607 first_unchanged_at_end_row
14608 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14609 IF_DEBUG (debug_delta = delta);
14610 IF_DEBUG (debug_delta_bytes = delta_bytes);
14612 /* Set stop_pos to the buffer position up to which we will have to
14613 display new lines. If first_unchanged_at_end_row != NULL, this
14614 is the buffer position of the start of the line displayed in that
14615 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14616 that we don't stop at a buffer position. */
14617 stop_pos = 0;
14618 if (first_unchanged_at_end_row)
14620 xassert (last_unchanged_at_beg_row == NULL
14621 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14623 /* If this is a continuation line, move forward to the next one
14624 that isn't. Changes in lines above affect this line.
14625 Caution: this may move first_unchanged_at_end_row to a row
14626 not displaying text. */
14627 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14628 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14629 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14630 < it.last_visible_y))
14631 ++first_unchanged_at_end_row;
14633 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14634 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14635 >= it.last_visible_y))
14636 first_unchanged_at_end_row = NULL;
14637 else
14639 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14640 + delta);
14641 first_unchanged_at_end_vpos
14642 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14643 xassert (stop_pos >= Z - END_UNCHANGED);
14646 else if (last_unchanged_at_beg_row == NULL)
14647 GIVE_UP (19);
14650 #if GLYPH_DEBUG
14652 /* Either there is no unchanged row at the end, or the one we have
14653 now displays text. This is a necessary condition for the window
14654 end pos calculation at the end of this function. */
14655 xassert (first_unchanged_at_end_row == NULL
14656 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14658 debug_last_unchanged_at_beg_vpos
14659 = (last_unchanged_at_beg_row
14660 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14661 : -1);
14662 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14664 #endif /* GLYPH_DEBUG != 0 */
14667 /* Display new lines. Set last_text_row to the last new line
14668 displayed which has text on it, i.e. might end up as being the
14669 line where the window_end_vpos is. */
14670 w->cursor.vpos = -1;
14671 last_text_row = NULL;
14672 overlay_arrow_seen = 0;
14673 while (it.current_y < it.last_visible_y
14674 && !fonts_changed_p
14675 && (first_unchanged_at_end_row == NULL
14676 || IT_CHARPOS (it) < stop_pos))
14678 if (display_line (&it))
14679 last_text_row = it.glyph_row - 1;
14682 if (fonts_changed_p)
14683 return -1;
14686 /* Compute differences in buffer positions, y-positions etc. for
14687 lines reused at the bottom of the window. Compute what we can
14688 scroll. */
14689 if (first_unchanged_at_end_row
14690 /* No lines reused because we displayed everything up to the
14691 bottom of the window. */
14692 && it.current_y < it.last_visible_y)
14694 dvpos = (it.vpos
14695 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14696 current_matrix));
14697 dy = it.current_y - first_unchanged_at_end_row->y;
14698 run.current_y = first_unchanged_at_end_row->y;
14699 run.desired_y = run.current_y + dy;
14700 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14702 else
14704 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14705 first_unchanged_at_end_row = NULL;
14707 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14710 /* Find the cursor if not already found. We have to decide whether
14711 PT will appear on this window (it sometimes doesn't, but this is
14712 not a very frequent case.) This decision has to be made before
14713 the current matrix is altered. A value of cursor.vpos < 0 means
14714 that PT is either in one of the lines beginning at
14715 first_unchanged_at_end_row or below the window. Don't care for
14716 lines that might be displayed later at the window end; as
14717 mentioned, this is not a frequent case. */
14718 if (w->cursor.vpos < 0)
14720 /* Cursor in unchanged rows at the top? */
14721 if (PT < CHARPOS (start_pos)
14722 && last_unchanged_at_beg_row)
14724 row = row_containing_pos (w, PT,
14725 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14726 last_unchanged_at_beg_row + 1, 0);
14727 if (row)
14728 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14731 /* Start from first_unchanged_at_end_row looking for PT. */
14732 else if (first_unchanged_at_end_row)
14734 row = row_containing_pos (w, PT - delta,
14735 first_unchanged_at_end_row, NULL, 0);
14736 if (row)
14737 set_cursor_from_row (w, row, w->current_matrix, delta,
14738 delta_bytes, dy, dvpos);
14741 /* Give up if cursor was not found. */
14742 if (w->cursor.vpos < 0)
14744 clear_glyph_matrix (w->desired_matrix);
14745 return -1;
14749 /* Don't let the cursor end in the scroll margins. */
14751 int this_scroll_margin, cursor_height;
14753 this_scroll_margin = max (0, scroll_margin);
14754 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14755 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14756 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14758 if ((w->cursor.y < this_scroll_margin
14759 && CHARPOS (start) > BEGV)
14760 /* Old redisplay didn't take scroll margin into account at the bottom,
14761 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14762 || (w->cursor.y + (make_cursor_line_fully_visible_p
14763 ? cursor_height + this_scroll_margin
14764 : 1)) > it.last_visible_y)
14766 w->cursor.vpos = -1;
14767 clear_glyph_matrix (w->desired_matrix);
14768 return -1;
14772 /* Scroll the display. Do it before changing the current matrix so
14773 that xterm.c doesn't get confused about where the cursor glyph is
14774 found. */
14775 if (dy && run.height)
14777 update_begin (f);
14779 if (FRAME_WINDOW_P (f))
14781 rif->update_window_begin_hook (w);
14782 rif->clear_window_mouse_face (w);
14783 rif->scroll_run_hook (w, &run);
14784 rif->update_window_end_hook (w, 0, 0);
14786 else
14788 /* Terminal frame. In this case, dvpos gives the number of
14789 lines to scroll by; dvpos < 0 means scroll up. */
14790 int first_unchanged_at_end_vpos
14791 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14792 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14793 int end = (WINDOW_TOP_EDGE_LINE (w)
14794 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14795 + window_internal_height (w));
14797 /* Perform the operation on the screen. */
14798 if (dvpos > 0)
14800 /* Scroll last_unchanged_at_beg_row to the end of the
14801 window down dvpos lines. */
14802 set_terminal_window (end);
14804 /* On dumb terminals delete dvpos lines at the end
14805 before inserting dvpos empty lines. */
14806 if (!scroll_region_ok)
14807 ins_del_lines (end - dvpos, -dvpos);
14809 /* Insert dvpos empty lines in front of
14810 last_unchanged_at_beg_row. */
14811 ins_del_lines (from, dvpos);
14813 else if (dvpos < 0)
14815 /* Scroll up last_unchanged_at_beg_vpos to the end of
14816 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14817 set_terminal_window (end);
14819 /* Delete dvpos lines in front of
14820 last_unchanged_at_beg_vpos. ins_del_lines will set
14821 the cursor to the given vpos and emit |dvpos| delete
14822 line sequences. */
14823 ins_del_lines (from + dvpos, dvpos);
14825 /* On a dumb terminal insert dvpos empty lines at the
14826 end. */
14827 if (!scroll_region_ok)
14828 ins_del_lines (end + dvpos, -dvpos);
14831 set_terminal_window (0);
14834 update_end (f);
14837 /* Shift reused rows of the current matrix to the right position.
14838 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14839 text. */
14840 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14841 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14842 if (dvpos < 0)
14844 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14845 bottom_vpos, dvpos);
14846 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14847 bottom_vpos, 0);
14849 else if (dvpos > 0)
14851 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14852 bottom_vpos, dvpos);
14853 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14854 first_unchanged_at_end_vpos + dvpos, 0);
14857 /* For frame-based redisplay, make sure that current frame and window
14858 matrix are in sync with respect to glyph memory. */
14859 if (!FRAME_WINDOW_P (f))
14860 sync_frame_with_window_matrix_rows (w);
14862 /* Adjust buffer positions in reused rows. */
14863 if (delta)
14864 increment_matrix_positions (current_matrix,
14865 first_unchanged_at_end_vpos + dvpos,
14866 bottom_vpos, delta, delta_bytes);
14868 /* Adjust Y positions. */
14869 if (dy)
14870 shift_glyph_matrix (w, current_matrix,
14871 first_unchanged_at_end_vpos + dvpos,
14872 bottom_vpos, dy);
14874 if (first_unchanged_at_end_row)
14876 first_unchanged_at_end_row += dvpos;
14877 if (first_unchanged_at_end_row->y >= it.last_visible_y
14878 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14879 first_unchanged_at_end_row = NULL;
14882 /* If scrolling up, there may be some lines to display at the end of
14883 the window. */
14884 last_text_row_at_end = NULL;
14885 if (dy < 0)
14887 /* Scrolling up can leave for example a partially visible line
14888 at the end of the window to be redisplayed. */
14889 /* Set last_row to the glyph row in the current matrix where the
14890 window end line is found. It has been moved up or down in
14891 the matrix by dvpos. */
14892 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14893 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14895 /* If last_row is the window end line, it should display text. */
14896 xassert (last_row->displays_text_p);
14898 /* If window end line was partially visible before, begin
14899 displaying at that line. Otherwise begin displaying with the
14900 line following it. */
14901 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14903 init_to_row_start (&it, w, last_row);
14904 it.vpos = last_vpos;
14905 it.current_y = last_row->y;
14907 else
14909 init_to_row_end (&it, w, last_row);
14910 it.vpos = 1 + last_vpos;
14911 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14912 ++last_row;
14915 /* We may start in a continuation line. If so, we have to
14916 get the right continuation_lines_width and current_x. */
14917 it.continuation_lines_width = last_row->continuation_lines_width;
14918 it.hpos = it.current_x = 0;
14920 /* Display the rest of the lines at the window end. */
14921 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14922 while (it.current_y < it.last_visible_y
14923 && !fonts_changed_p)
14925 /* Is it always sure that the display agrees with lines in
14926 the current matrix? I don't think so, so we mark rows
14927 displayed invalid in the current matrix by setting their
14928 enabled_p flag to zero. */
14929 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14930 if (display_line (&it))
14931 last_text_row_at_end = it.glyph_row - 1;
14935 /* Update window_end_pos and window_end_vpos. */
14936 if (first_unchanged_at_end_row
14937 && !last_text_row_at_end)
14939 /* Window end line if one of the preserved rows from the current
14940 matrix. Set row to the last row displaying text in current
14941 matrix starting at first_unchanged_at_end_row, after
14942 scrolling. */
14943 xassert (first_unchanged_at_end_row->displays_text_p);
14944 row = find_last_row_displaying_text (w->current_matrix, &it,
14945 first_unchanged_at_end_row);
14946 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14948 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14949 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14950 w->window_end_vpos
14951 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14952 xassert (w->window_end_bytepos >= 0);
14953 IF_DEBUG (debug_method_add (w, "A"));
14955 else if (last_text_row_at_end)
14957 w->window_end_pos
14958 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14959 w->window_end_bytepos
14960 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14961 w->window_end_vpos
14962 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14963 xassert (w->window_end_bytepos >= 0);
14964 IF_DEBUG (debug_method_add (w, "B"));
14966 else if (last_text_row)
14968 /* We have displayed either to the end of the window or at the
14969 end of the window, i.e. the last row with text is to be found
14970 in the desired matrix. */
14971 w->window_end_pos
14972 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14973 w->window_end_bytepos
14974 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14975 w->window_end_vpos
14976 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14977 xassert (w->window_end_bytepos >= 0);
14979 else if (first_unchanged_at_end_row == NULL
14980 && last_text_row == NULL
14981 && last_text_row_at_end == NULL)
14983 /* Displayed to end of window, but no line containing text was
14984 displayed. Lines were deleted at the end of the window. */
14985 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14986 int vpos = XFASTINT (w->window_end_vpos);
14987 struct glyph_row *current_row = current_matrix->rows + vpos;
14988 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14990 for (row = NULL;
14991 row == NULL && vpos >= first_vpos;
14992 --vpos, --current_row, --desired_row)
14994 if (desired_row->enabled_p)
14996 if (desired_row->displays_text_p)
14997 row = desired_row;
14999 else if (current_row->displays_text_p)
15000 row = current_row;
15003 xassert (row != NULL);
15004 w->window_end_vpos = make_number (vpos + 1);
15005 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15006 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15007 xassert (w->window_end_bytepos >= 0);
15008 IF_DEBUG (debug_method_add (w, "C"));
15010 else
15011 abort ();
15013 #if 0 /* This leads to problems, for instance when the cursor is
15014 at ZV, and the cursor line displays no text. */
15015 /* Disable rows below what's displayed in the window. This makes
15016 debugging easier. */
15017 enable_glyph_matrix_rows (current_matrix,
15018 XFASTINT (w->window_end_vpos) + 1,
15019 bottom_vpos, 0);
15020 #endif
15022 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15023 debug_end_vpos = XFASTINT (w->window_end_vpos));
15025 /* Record that display has not been completed. */
15026 w->window_end_valid = Qnil;
15027 w->desired_matrix->no_scrolling_p = 1;
15028 return 3;
15030 #undef GIVE_UP
15035 /***********************************************************************
15036 More debugging support
15037 ***********************************************************************/
15039 #if GLYPH_DEBUG
15041 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15042 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15043 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15046 /* Dump the contents of glyph matrix MATRIX on stderr.
15048 GLYPHS 0 means don't show glyph contents.
15049 GLYPHS 1 means show glyphs in short form
15050 GLYPHS > 1 means show glyphs in long form. */
15052 void
15053 dump_glyph_matrix (matrix, glyphs)
15054 struct glyph_matrix *matrix;
15055 int glyphs;
15057 int i;
15058 for (i = 0; i < matrix->nrows; ++i)
15059 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15063 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15064 the glyph row and area where the glyph comes from. */
15066 void
15067 dump_glyph (row, glyph, area)
15068 struct glyph_row *row;
15069 struct glyph *glyph;
15070 int area;
15072 if (glyph->type == CHAR_GLYPH)
15074 fprintf (stderr,
15075 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15076 glyph - row->glyphs[TEXT_AREA],
15077 'C',
15078 glyph->charpos,
15079 (BUFFERP (glyph->object)
15080 ? 'B'
15081 : (STRINGP (glyph->object)
15082 ? 'S'
15083 : '-')),
15084 glyph->pixel_width,
15085 glyph->u.ch,
15086 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15087 ? glyph->u.ch
15088 : '.'),
15089 glyph->face_id,
15090 glyph->left_box_line_p,
15091 glyph->right_box_line_p);
15093 else if (glyph->type == STRETCH_GLYPH)
15095 fprintf (stderr,
15096 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15097 glyph - row->glyphs[TEXT_AREA],
15098 'S',
15099 glyph->charpos,
15100 (BUFFERP (glyph->object)
15101 ? 'B'
15102 : (STRINGP (glyph->object)
15103 ? 'S'
15104 : '-')),
15105 glyph->pixel_width,
15107 '.',
15108 glyph->face_id,
15109 glyph->left_box_line_p,
15110 glyph->right_box_line_p);
15112 else if (glyph->type == IMAGE_GLYPH)
15114 fprintf (stderr,
15115 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15116 glyph - row->glyphs[TEXT_AREA],
15117 'I',
15118 glyph->charpos,
15119 (BUFFERP (glyph->object)
15120 ? 'B'
15121 : (STRINGP (glyph->object)
15122 ? 'S'
15123 : '-')),
15124 glyph->pixel_width,
15125 glyph->u.img_id,
15126 '.',
15127 glyph->face_id,
15128 glyph->left_box_line_p,
15129 glyph->right_box_line_p);
15131 else if (glyph->type == COMPOSITE_GLYPH)
15133 fprintf (stderr,
15134 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15135 glyph - row->glyphs[TEXT_AREA],
15136 '+',
15137 glyph->charpos,
15138 (BUFFERP (glyph->object)
15139 ? 'B'
15140 : (STRINGP (glyph->object)
15141 ? 'S'
15142 : '-')),
15143 glyph->pixel_width,
15144 glyph->u.cmp_id,
15145 '.',
15146 glyph->face_id,
15147 glyph->left_box_line_p,
15148 glyph->right_box_line_p);
15153 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15154 GLYPHS 0 means don't show glyph contents.
15155 GLYPHS 1 means show glyphs in short form
15156 GLYPHS > 1 means show glyphs in long form. */
15158 void
15159 dump_glyph_row (row, vpos, glyphs)
15160 struct glyph_row *row;
15161 int vpos, glyphs;
15163 if (glyphs != 1)
15165 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15166 fprintf (stderr, "======================================================================\n");
15168 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15169 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15170 vpos,
15171 MATRIX_ROW_START_CHARPOS (row),
15172 MATRIX_ROW_END_CHARPOS (row),
15173 row->used[TEXT_AREA],
15174 row->contains_overlapping_glyphs_p,
15175 row->enabled_p,
15176 row->truncated_on_left_p,
15177 row->truncated_on_right_p,
15178 row->continued_p,
15179 MATRIX_ROW_CONTINUATION_LINE_P (row),
15180 row->displays_text_p,
15181 row->ends_at_zv_p,
15182 row->fill_line_p,
15183 row->ends_in_middle_of_char_p,
15184 row->starts_in_middle_of_char_p,
15185 row->mouse_face_p,
15186 row->x,
15187 row->y,
15188 row->pixel_width,
15189 row->height,
15190 row->visible_height,
15191 row->ascent,
15192 row->phys_ascent);
15193 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15194 row->end.overlay_string_index,
15195 row->continuation_lines_width);
15196 fprintf (stderr, "%9d %5d\n",
15197 CHARPOS (row->start.string_pos),
15198 CHARPOS (row->end.string_pos));
15199 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15200 row->end.dpvec_index);
15203 if (glyphs > 1)
15205 int area;
15207 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15209 struct glyph *glyph = row->glyphs[area];
15210 struct glyph *glyph_end = glyph + row->used[area];
15212 /* Glyph for a line end in text. */
15213 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15214 ++glyph_end;
15216 if (glyph < glyph_end)
15217 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15219 for (; glyph < glyph_end; ++glyph)
15220 dump_glyph (row, glyph, area);
15223 else if (glyphs == 1)
15225 int area;
15227 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15229 char *s = (char *) alloca (row->used[area] + 1);
15230 int i;
15232 for (i = 0; i < row->used[area]; ++i)
15234 struct glyph *glyph = row->glyphs[area] + i;
15235 if (glyph->type == CHAR_GLYPH
15236 && glyph->u.ch < 0x80
15237 && glyph->u.ch >= ' ')
15238 s[i] = glyph->u.ch;
15239 else
15240 s[i] = '.';
15243 s[i] = '\0';
15244 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15250 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15251 Sdump_glyph_matrix, 0, 1, "p",
15252 doc: /* Dump the current matrix of the selected window to stderr.
15253 Shows contents of glyph row structures. With non-nil
15254 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15255 glyphs in short form, otherwise show glyphs in long form. */)
15256 (glyphs)
15257 Lisp_Object glyphs;
15259 struct window *w = XWINDOW (selected_window);
15260 struct buffer *buffer = XBUFFER (w->buffer);
15262 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15263 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15264 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15265 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15266 fprintf (stderr, "=============================================\n");
15267 dump_glyph_matrix (w->current_matrix,
15268 NILP (glyphs) ? 0 : XINT (glyphs));
15269 return Qnil;
15273 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15274 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15277 struct frame *f = XFRAME (selected_frame);
15278 dump_glyph_matrix (f->current_matrix, 1);
15279 return Qnil;
15283 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15284 doc: /* Dump glyph row ROW to stderr.
15285 GLYPH 0 means don't dump glyphs.
15286 GLYPH 1 means dump glyphs in short form.
15287 GLYPH > 1 or omitted means dump glyphs in long form. */)
15288 (row, glyphs)
15289 Lisp_Object row, glyphs;
15291 struct glyph_matrix *matrix;
15292 int vpos;
15294 CHECK_NUMBER (row);
15295 matrix = XWINDOW (selected_window)->current_matrix;
15296 vpos = XINT (row);
15297 if (vpos >= 0 && vpos < matrix->nrows)
15298 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15299 vpos,
15300 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15301 return Qnil;
15305 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15306 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15307 GLYPH 0 means don't dump glyphs.
15308 GLYPH 1 means dump glyphs in short form.
15309 GLYPH > 1 or omitted means dump glyphs in long form. */)
15310 (row, glyphs)
15311 Lisp_Object row, glyphs;
15313 struct frame *sf = SELECTED_FRAME ();
15314 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15315 int vpos;
15317 CHECK_NUMBER (row);
15318 vpos = XINT (row);
15319 if (vpos >= 0 && vpos < m->nrows)
15320 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15321 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15322 return Qnil;
15326 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15327 doc: /* Toggle tracing of redisplay.
15328 With ARG, turn tracing on if and only if ARG is positive. */)
15329 (arg)
15330 Lisp_Object arg;
15332 if (NILP (arg))
15333 trace_redisplay_p = !trace_redisplay_p;
15334 else
15336 arg = Fprefix_numeric_value (arg);
15337 trace_redisplay_p = XINT (arg) > 0;
15340 return Qnil;
15344 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15345 doc: /* Like `format', but print result to stderr.
15346 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15347 (nargs, args)
15348 int nargs;
15349 Lisp_Object *args;
15351 Lisp_Object s = Fformat (nargs, args);
15352 fprintf (stderr, "%s", SDATA (s));
15353 return Qnil;
15356 #endif /* GLYPH_DEBUG */
15360 /***********************************************************************
15361 Building Desired Matrix Rows
15362 ***********************************************************************/
15364 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15365 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15367 static struct glyph_row *
15368 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15369 struct window *w;
15370 Lisp_Object overlay_arrow_string;
15372 struct frame *f = XFRAME (WINDOW_FRAME (w));
15373 struct buffer *buffer = XBUFFER (w->buffer);
15374 struct buffer *old = current_buffer;
15375 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15376 int arrow_len = SCHARS (overlay_arrow_string);
15377 const unsigned char *arrow_end = arrow_string + arrow_len;
15378 const unsigned char *p;
15379 struct it it;
15380 int multibyte_p;
15381 int n_glyphs_before;
15383 set_buffer_temp (buffer);
15384 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15385 it.glyph_row->used[TEXT_AREA] = 0;
15386 SET_TEXT_POS (it.position, 0, 0);
15388 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15389 p = arrow_string;
15390 while (p < arrow_end)
15392 Lisp_Object face, ilisp;
15394 /* Get the next character. */
15395 if (multibyte_p)
15396 it.c = string_char_and_length (p, arrow_len, &it.len);
15397 else
15398 it.c = *p, it.len = 1;
15399 p += it.len;
15401 /* Get its face. */
15402 ilisp = make_number (p - arrow_string);
15403 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15404 it.face_id = compute_char_face (f, it.c, face);
15406 /* Compute its width, get its glyphs. */
15407 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15408 SET_TEXT_POS (it.position, -1, -1);
15409 PRODUCE_GLYPHS (&it);
15411 /* If this character doesn't fit any more in the line, we have
15412 to remove some glyphs. */
15413 if (it.current_x > it.last_visible_x)
15415 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15416 break;
15420 set_buffer_temp (old);
15421 return it.glyph_row;
15425 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15426 glyphs are only inserted for terminal frames since we can't really
15427 win with truncation glyphs when partially visible glyphs are
15428 involved. Which glyphs to insert is determined by
15429 produce_special_glyphs. */
15431 static void
15432 insert_left_trunc_glyphs (it)
15433 struct it *it;
15435 struct it truncate_it;
15436 struct glyph *from, *end, *to, *toend;
15438 xassert (!FRAME_WINDOW_P (it->f));
15440 /* Get the truncation glyphs. */
15441 truncate_it = *it;
15442 truncate_it.current_x = 0;
15443 truncate_it.face_id = DEFAULT_FACE_ID;
15444 truncate_it.glyph_row = &scratch_glyph_row;
15445 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15446 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15447 truncate_it.object = make_number (0);
15448 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15450 /* Overwrite glyphs from IT with truncation glyphs. */
15451 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15452 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15453 to = it->glyph_row->glyphs[TEXT_AREA];
15454 toend = to + it->glyph_row->used[TEXT_AREA];
15456 while (from < end)
15457 *to++ = *from++;
15459 /* There may be padding glyphs left over. Overwrite them too. */
15460 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15462 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15463 while (from < end)
15464 *to++ = *from++;
15467 if (to > toend)
15468 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15472 /* Compute the pixel height and width of IT->glyph_row.
15474 Most of the time, ascent and height of a display line will be equal
15475 to the max_ascent and max_height values of the display iterator
15476 structure. This is not the case if
15478 1. We hit ZV without displaying anything. In this case, max_ascent
15479 and max_height will be zero.
15481 2. We have some glyphs that don't contribute to the line height.
15482 (The glyph row flag contributes_to_line_height_p is for future
15483 pixmap extensions).
15485 The first case is easily covered by using default values because in
15486 these cases, the line height does not really matter, except that it
15487 must not be zero. */
15489 static void
15490 compute_line_metrics (it)
15491 struct it *it;
15493 struct glyph_row *row = it->glyph_row;
15494 int area, i;
15496 if (FRAME_WINDOW_P (it->f))
15498 int i, min_y, max_y;
15500 /* The line may consist of one space only, that was added to
15501 place the cursor on it. If so, the row's height hasn't been
15502 computed yet. */
15503 if (row->height == 0)
15505 if (it->max_ascent + it->max_descent == 0)
15506 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15507 row->ascent = it->max_ascent;
15508 row->height = it->max_ascent + it->max_descent;
15509 row->phys_ascent = it->max_phys_ascent;
15510 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15511 row->extra_line_spacing = it->max_extra_line_spacing;
15514 /* Compute the width of this line. */
15515 row->pixel_width = row->x;
15516 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15517 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15519 xassert (row->pixel_width >= 0);
15520 xassert (row->ascent >= 0 && row->height > 0);
15522 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15523 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15525 /* If first line's physical ascent is larger than its logical
15526 ascent, use the physical ascent, and make the row taller.
15527 This makes accented characters fully visible. */
15528 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15529 && row->phys_ascent > row->ascent)
15531 row->height += row->phys_ascent - row->ascent;
15532 row->ascent = row->phys_ascent;
15535 /* Compute how much of the line is visible. */
15536 row->visible_height = row->height;
15538 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15539 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15541 if (row->y < min_y)
15542 row->visible_height -= min_y - row->y;
15543 if (row->y + row->height > max_y)
15544 row->visible_height -= row->y + row->height - max_y;
15546 else
15548 row->pixel_width = row->used[TEXT_AREA];
15549 if (row->continued_p)
15550 row->pixel_width -= it->continuation_pixel_width;
15551 else if (row->truncated_on_right_p)
15552 row->pixel_width -= it->truncation_pixel_width;
15553 row->ascent = row->phys_ascent = 0;
15554 row->height = row->phys_height = row->visible_height = 1;
15555 row->extra_line_spacing = 0;
15558 /* Compute a hash code for this row. */
15559 row->hash = 0;
15560 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15561 for (i = 0; i < row->used[area]; ++i)
15562 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15563 + row->glyphs[area][i].u.val
15564 + row->glyphs[area][i].face_id
15565 + row->glyphs[area][i].padding_p
15566 + (row->glyphs[area][i].type << 2));
15568 it->max_ascent = it->max_descent = 0;
15569 it->max_phys_ascent = it->max_phys_descent = 0;
15573 /* Append one space to the glyph row of iterator IT if doing a
15574 window-based redisplay. The space has the same face as
15575 IT->face_id. Value is non-zero if a space was added.
15577 This function is called to make sure that there is always one glyph
15578 at the end of a glyph row that the cursor can be set on under
15579 window-systems. (If there weren't such a glyph we would not know
15580 how wide and tall a box cursor should be displayed).
15582 At the same time this space let's a nicely handle clearing to the
15583 end of the line if the row ends in italic text. */
15585 static int
15586 append_space_for_newline (it, default_face_p)
15587 struct it *it;
15588 int default_face_p;
15590 if (FRAME_WINDOW_P (it->f))
15592 int n = it->glyph_row->used[TEXT_AREA];
15594 if (it->glyph_row->glyphs[TEXT_AREA] + n
15595 < it->glyph_row->glyphs[1 + TEXT_AREA])
15597 /* Save some values that must not be changed.
15598 Must save IT->c and IT->len because otherwise
15599 ITERATOR_AT_END_P wouldn't work anymore after
15600 append_space_for_newline has been called. */
15601 enum display_element_type saved_what = it->what;
15602 int saved_c = it->c, saved_len = it->len;
15603 int saved_x = it->current_x;
15604 int saved_face_id = it->face_id;
15605 struct text_pos saved_pos;
15606 Lisp_Object saved_object;
15607 struct face *face;
15609 saved_object = it->object;
15610 saved_pos = it->position;
15612 it->what = IT_CHARACTER;
15613 bzero (&it->position, sizeof it->position);
15614 it->object = make_number (0);
15615 it->c = ' ';
15616 it->len = 1;
15618 if (default_face_p)
15619 it->face_id = DEFAULT_FACE_ID;
15620 else if (it->face_before_selective_p)
15621 it->face_id = it->saved_face_id;
15622 face = FACE_FROM_ID (it->f, it->face_id);
15623 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15625 PRODUCE_GLYPHS (it);
15627 it->override_ascent = -1;
15628 it->constrain_row_ascent_descent_p = 0;
15629 it->current_x = saved_x;
15630 it->object = saved_object;
15631 it->position = saved_pos;
15632 it->what = saved_what;
15633 it->face_id = saved_face_id;
15634 it->len = saved_len;
15635 it->c = saved_c;
15636 return 1;
15640 return 0;
15644 /* Extend the face of the last glyph in the text area of IT->glyph_row
15645 to the end of the display line. Called from display_line.
15646 If the glyph row is empty, add a space glyph to it so that we
15647 know the face to draw. Set the glyph row flag fill_line_p. */
15649 static void
15650 extend_face_to_end_of_line (it)
15651 struct it *it;
15653 struct face *face;
15654 struct frame *f = it->f;
15656 /* If line is already filled, do nothing. */
15657 if (it->current_x >= it->last_visible_x)
15658 return;
15660 /* Face extension extends the background and box of IT->face_id
15661 to the end of the line. If the background equals the background
15662 of the frame, we don't have to do anything. */
15663 if (it->face_before_selective_p)
15664 face = FACE_FROM_ID (it->f, it->saved_face_id);
15665 else
15666 face = FACE_FROM_ID (f, it->face_id);
15668 if (FRAME_WINDOW_P (f)
15669 && it->glyph_row->displays_text_p
15670 && face->box == FACE_NO_BOX
15671 && face->background == FRAME_BACKGROUND_PIXEL (f)
15672 && !face->stipple)
15673 return;
15675 /* Set the glyph row flag indicating that the face of the last glyph
15676 in the text area has to be drawn to the end of the text area. */
15677 it->glyph_row->fill_line_p = 1;
15679 /* If current character of IT is not ASCII, make sure we have the
15680 ASCII face. This will be automatically undone the next time
15681 get_next_display_element returns a multibyte character. Note
15682 that the character will always be single byte in unibyte text. */
15683 if (!SINGLE_BYTE_CHAR_P (it->c))
15685 it->face_id = FACE_FOR_CHAR (f, face, 0);
15688 if (FRAME_WINDOW_P (f))
15690 /* If the row is empty, add a space with the current face of IT,
15691 so that we know which face to draw. */
15692 if (it->glyph_row->used[TEXT_AREA] == 0)
15694 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15695 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15696 it->glyph_row->used[TEXT_AREA] = 1;
15699 else
15701 /* Save some values that must not be changed. */
15702 int saved_x = it->current_x;
15703 struct text_pos saved_pos;
15704 Lisp_Object saved_object;
15705 enum display_element_type saved_what = it->what;
15706 int saved_face_id = it->face_id;
15708 saved_object = it->object;
15709 saved_pos = it->position;
15711 it->what = IT_CHARACTER;
15712 bzero (&it->position, sizeof it->position);
15713 it->object = make_number (0);
15714 it->c = ' ';
15715 it->len = 1;
15716 it->face_id = face->id;
15718 PRODUCE_GLYPHS (it);
15720 while (it->current_x <= it->last_visible_x)
15721 PRODUCE_GLYPHS (it);
15723 /* Don't count these blanks really. It would let us insert a left
15724 truncation glyph below and make us set the cursor on them, maybe. */
15725 it->current_x = saved_x;
15726 it->object = saved_object;
15727 it->position = saved_pos;
15728 it->what = saved_what;
15729 it->face_id = saved_face_id;
15734 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15735 trailing whitespace. */
15737 static int
15738 trailing_whitespace_p (charpos)
15739 int charpos;
15741 int bytepos = CHAR_TO_BYTE (charpos);
15742 int c = 0;
15744 while (bytepos < ZV_BYTE
15745 && (c = FETCH_CHAR (bytepos),
15746 c == ' ' || c == '\t'))
15747 ++bytepos;
15749 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15751 if (bytepos != PT_BYTE)
15752 return 1;
15754 return 0;
15758 /* Highlight trailing whitespace, if any, in ROW. */
15760 void
15761 highlight_trailing_whitespace (f, row)
15762 struct frame *f;
15763 struct glyph_row *row;
15765 int used = row->used[TEXT_AREA];
15767 if (used)
15769 struct glyph *start = row->glyphs[TEXT_AREA];
15770 struct glyph *glyph = start + used - 1;
15772 /* Skip over glyphs inserted to display the cursor at the
15773 end of a line, for extending the face of the last glyph
15774 to the end of the line on terminals, and for truncation
15775 and continuation glyphs. */
15776 while (glyph >= start
15777 && glyph->type == CHAR_GLYPH
15778 && INTEGERP (glyph->object))
15779 --glyph;
15781 /* If last glyph is a space or stretch, and it's trailing
15782 whitespace, set the face of all trailing whitespace glyphs in
15783 IT->glyph_row to `trailing-whitespace'. */
15784 if (glyph >= start
15785 && BUFFERP (glyph->object)
15786 && (glyph->type == STRETCH_GLYPH
15787 || (glyph->type == CHAR_GLYPH
15788 && glyph->u.ch == ' '))
15789 && trailing_whitespace_p (glyph->charpos))
15791 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15792 if (face_id < 0)
15793 return;
15795 while (glyph >= start
15796 && BUFFERP (glyph->object)
15797 && (glyph->type == STRETCH_GLYPH
15798 || (glyph->type == CHAR_GLYPH
15799 && glyph->u.ch == ' ')))
15800 (glyph--)->face_id = face_id;
15806 /* Value is non-zero if glyph row ROW in window W should be
15807 used to hold the cursor. */
15809 static int
15810 cursor_row_p (w, row)
15811 struct window *w;
15812 struct glyph_row *row;
15814 int cursor_row_p = 1;
15816 if (PT == MATRIX_ROW_END_CHARPOS (row))
15818 /* If the row ends with a newline from a string, we don't want
15819 the cursor there, but we still want it at the start of the
15820 string if the string starts in this row.
15821 If the row is continued it doesn't end in a newline. */
15822 if (CHARPOS (row->end.string_pos) >= 0)
15823 cursor_row_p = (row->continued_p
15824 || PT >= MATRIX_ROW_START_CHARPOS (row));
15825 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15827 /* If the row ends in middle of a real character,
15828 and the line is continued, we want the cursor here.
15829 That's because MATRIX_ROW_END_CHARPOS would equal
15830 PT if PT is before the character. */
15831 if (!row->ends_in_ellipsis_p)
15832 cursor_row_p = row->continued_p;
15833 else
15834 /* If the row ends in an ellipsis, then
15835 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15836 We want that position to be displayed after the ellipsis. */
15837 cursor_row_p = 0;
15839 /* If the row ends at ZV, display the cursor at the end of that
15840 row instead of at the start of the row below. */
15841 else if (row->ends_at_zv_p)
15842 cursor_row_p = 1;
15843 else
15844 cursor_row_p = 0;
15847 return cursor_row_p;
15851 /* Construct the glyph row IT->glyph_row in the desired matrix of
15852 IT->w from text at the current position of IT. See dispextern.h
15853 for an overview of struct it. Value is non-zero if
15854 IT->glyph_row displays text, as opposed to a line displaying ZV
15855 only. */
15857 static int
15858 display_line (it)
15859 struct it *it;
15861 struct glyph_row *row = it->glyph_row;
15862 Lisp_Object overlay_arrow_string;
15864 /* We always start displaying at hpos zero even if hscrolled. */
15865 xassert (it->hpos == 0 && it->current_x == 0);
15867 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15868 >= it->w->desired_matrix->nrows)
15870 it->w->nrows_scale_factor++;
15871 fonts_changed_p = 1;
15872 return 0;
15875 /* Is IT->w showing the region? */
15876 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15878 /* Clear the result glyph row and enable it. */
15879 prepare_desired_row (row);
15881 row->y = it->current_y;
15882 row->start = it->start;
15883 row->continuation_lines_width = it->continuation_lines_width;
15884 row->displays_text_p = 1;
15885 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15886 it->starts_in_middle_of_char_p = 0;
15888 /* Arrange the overlays nicely for our purposes. Usually, we call
15889 display_line on only one line at a time, in which case this
15890 can't really hurt too much, or we call it on lines which appear
15891 one after another in the buffer, in which case all calls to
15892 recenter_overlay_lists but the first will be pretty cheap. */
15893 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15895 /* Move over display elements that are not visible because we are
15896 hscrolled. This may stop at an x-position < IT->first_visible_x
15897 if the first glyph is partially visible or if we hit a line end. */
15898 if (it->current_x < it->first_visible_x)
15900 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15901 MOVE_TO_POS | MOVE_TO_X);
15904 /* Get the initial row height. This is either the height of the
15905 text hscrolled, if there is any, or zero. */
15906 row->ascent = it->max_ascent;
15907 row->height = it->max_ascent + it->max_descent;
15908 row->phys_ascent = it->max_phys_ascent;
15909 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15910 row->extra_line_spacing = it->max_extra_line_spacing;
15912 /* Loop generating characters. The loop is left with IT on the next
15913 character to display. */
15914 while (1)
15916 int n_glyphs_before, hpos_before, x_before;
15917 int x, i, nglyphs;
15918 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15920 /* Retrieve the next thing to display. Value is zero if end of
15921 buffer reached. */
15922 if (!get_next_display_element (it))
15924 /* Maybe add a space at the end of this line that is used to
15925 display the cursor there under X. Set the charpos of the
15926 first glyph of blank lines not corresponding to any text
15927 to -1. */
15928 #ifdef HAVE_WINDOW_SYSTEM
15929 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15930 row->exact_window_width_line_p = 1;
15931 else
15932 #endif /* HAVE_WINDOW_SYSTEM */
15933 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15934 || row->used[TEXT_AREA] == 0)
15936 row->glyphs[TEXT_AREA]->charpos = -1;
15937 row->displays_text_p = 0;
15939 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15940 && (!MINI_WINDOW_P (it->w)
15941 || (minibuf_level && EQ (it->window, minibuf_window))))
15942 row->indicate_empty_line_p = 1;
15945 it->continuation_lines_width = 0;
15946 row->ends_at_zv_p = 1;
15947 break;
15950 /* Now, get the metrics of what we want to display. This also
15951 generates glyphs in `row' (which is IT->glyph_row). */
15952 n_glyphs_before = row->used[TEXT_AREA];
15953 x = it->current_x;
15955 /* Remember the line height so far in case the next element doesn't
15956 fit on the line. */
15957 if (!it->truncate_lines_p)
15959 ascent = it->max_ascent;
15960 descent = it->max_descent;
15961 phys_ascent = it->max_phys_ascent;
15962 phys_descent = it->max_phys_descent;
15965 PRODUCE_GLYPHS (it);
15967 /* If this display element was in marginal areas, continue with
15968 the next one. */
15969 if (it->area != TEXT_AREA)
15971 row->ascent = max (row->ascent, it->max_ascent);
15972 row->height = max (row->height, it->max_ascent + it->max_descent);
15973 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15974 row->phys_height = max (row->phys_height,
15975 it->max_phys_ascent + it->max_phys_descent);
15976 row->extra_line_spacing = max (row->extra_line_spacing,
15977 it->max_extra_line_spacing);
15978 set_iterator_to_next (it, 1);
15979 continue;
15982 /* Does the display element fit on the line? If we truncate
15983 lines, we should draw past the right edge of the window. If
15984 we don't truncate, we want to stop so that we can display the
15985 continuation glyph before the right margin. If lines are
15986 continued, there are two possible strategies for characters
15987 resulting in more than 1 glyph (e.g. tabs): Display as many
15988 glyphs as possible in this line and leave the rest for the
15989 continuation line, or display the whole element in the next
15990 line. Original redisplay did the former, so we do it also. */
15991 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15992 hpos_before = it->hpos;
15993 x_before = x;
15995 if (/* Not a newline. */
15996 nglyphs > 0
15997 /* Glyphs produced fit entirely in the line. */
15998 && it->current_x < it->last_visible_x)
16000 it->hpos += nglyphs;
16001 row->ascent = max (row->ascent, it->max_ascent);
16002 row->height = max (row->height, it->max_ascent + it->max_descent);
16003 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16004 row->phys_height = max (row->phys_height,
16005 it->max_phys_ascent + it->max_phys_descent);
16006 row->extra_line_spacing = max (row->extra_line_spacing,
16007 it->max_extra_line_spacing);
16008 if (it->current_x - it->pixel_width < it->first_visible_x)
16009 row->x = x - it->first_visible_x;
16011 else
16013 int new_x;
16014 struct glyph *glyph;
16016 for (i = 0; i < nglyphs; ++i, x = new_x)
16018 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16019 new_x = x + glyph->pixel_width;
16021 if (/* Lines are continued. */
16022 !it->truncate_lines_p
16023 && (/* Glyph doesn't fit on the line. */
16024 new_x > it->last_visible_x
16025 /* Or it fits exactly on a window system frame. */
16026 || (new_x == it->last_visible_x
16027 && FRAME_WINDOW_P (it->f))))
16029 /* End of a continued line. */
16031 if (it->hpos == 0
16032 || (new_x == it->last_visible_x
16033 && FRAME_WINDOW_P (it->f)))
16035 /* Current glyph is the only one on the line or
16036 fits exactly on the line. We must continue
16037 the line because we can't draw the cursor
16038 after the glyph. */
16039 row->continued_p = 1;
16040 it->current_x = new_x;
16041 it->continuation_lines_width += new_x;
16042 ++it->hpos;
16043 if (i == nglyphs - 1)
16045 set_iterator_to_next (it, 1);
16046 #ifdef HAVE_WINDOW_SYSTEM
16047 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16049 if (!get_next_display_element (it))
16051 row->exact_window_width_line_p = 1;
16052 it->continuation_lines_width = 0;
16053 row->continued_p = 0;
16054 row->ends_at_zv_p = 1;
16056 else if (ITERATOR_AT_END_OF_LINE_P (it))
16058 row->continued_p = 0;
16059 row->exact_window_width_line_p = 1;
16062 #endif /* HAVE_WINDOW_SYSTEM */
16065 else if (CHAR_GLYPH_PADDING_P (*glyph)
16066 && !FRAME_WINDOW_P (it->f))
16068 /* A padding glyph that doesn't fit on this line.
16069 This means the whole character doesn't fit
16070 on the line. */
16071 row->used[TEXT_AREA] = n_glyphs_before;
16073 /* Fill the rest of the row with continuation
16074 glyphs like in 20.x. */
16075 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16076 < row->glyphs[1 + TEXT_AREA])
16077 produce_special_glyphs (it, IT_CONTINUATION);
16079 row->continued_p = 1;
16080 it->current_x = x_before;
16081 it->continuation_lines_width += x_before;
16083 /* Restore the height to what it was before the
16084 element not fitting on the line. */
16085 it->max_ascent = ascent;
16086 it->max_descent = descent;
16087 it->max_phys_ascent = phys_ascent;
16088 it->max_phys_descent = phys_descent;
16090 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16092 /* A TAB that extends past the right edge of the
16093 window. This produces a single glyph on
16094 window system frames. We leave the glyph in
16095 this row and let it fill the row, but don't
16096 consume the TAB. */
16097 it->continuation_lines_width += it->last_visible_x;
16098 row->ends_in_middle_of_char_p = 1;
16099 row->continued_p = 1;
16100 glyph->pixel_width = it->last_visible_x - x;
16101 it->starts_in_middle_of_char_p = 1;
16103 else
16105 /* Something other than a TAB that draws past
16106 the right edge of the window. Restore
16107 positions to values before the element. */
16108 row->used[TEXT_AREA] = n_glyphs_before + i;
16110 /* Display continuation glyphs. */
16111 if (!FRAME_WINDOW_P (it->f))
16112 produce_special_glyphs (it, IT_CONTINUATION);
16113 row->continued_p = 1;
16115 it->current_x = x_before;
16116 it->continuation_lines_width += x;
16117 extend_face_to_end_of_line (it);
16119 if (nglyphs > 1 && i > 0)
16121 row->ends_in_middle_of_char_p = 1;
16122 it->starts_in_middle_of_char_p = 1;
16125 /* Restore the height to what it was before the
16126 element not fitting on the line. */
16127 it->max_ascent = ascent;
16128 it->max_descent = descent;
16129 it->max_phys_ascent = phys_ascent;
16130 it->max_phys_descent = phys_descent;
16133 break;
16135 else if (new_x > it->first_visible_x)
16137 /* Increment number of glyphs actually displayed. */
16138 ++it->hpos;
16140 if (x < it->first_visible_x)
16141 /* Glyph is partially visible, i.e. row starts at
16142 negative X position. */
16143 row->x = x - it->first_visible_x;
16145 else
16147 /* Glyph is completely off the left margin of the
16148 window. This should not happen because of the
16149 move_it_in_display_line at the start of this
16150 function, unless the text display area of the
16151 window is empty. */
16152 xassert (it->first_visible_x <= it->last_visible_x);
16156 row->ascent = max (row->ascent, it->max_ascent);
16157 row->height = max (row->height, it->max_ascent + it->max_descent);
16158 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16159 row->phys_height = max (row->phys_height,
16160 it->max_phys_ascent + it->max_phys_descent);
16161 row->extra_line_spacing = max (row->extra_line_spacing,
16162 it->max_extra_line_spacing);
16164 /* End of this display line if row is continued. */
16165 if (row->continued_p || row->ends_at_zv_p)
16166 break;
16169 at_end_of_line:
16170 /* Is this a line end? If yes, we're also done, after making
16171 sure that a non-default face is extended up to the right
16172 margin of the window. */
16173 if (ITERATOR_AT_END_OF_LINE_P (it))
16175 int used_before = row->used[TEXT_AREA];
16177 row->ends_in_newline_from_string_p = STRINGP (it->object);
16179 #ifdef HAVE_WINDOW_SYSTEM
16180 /* Add a space at the end of the line that is used to
16181 display the cursor there. */
16182 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16183 append_space_for_newline (it, 0);
16184 #endif /* HAVE_WINDOW_SYSTEM */
16186 /* Extend the face to the end of the line. */
16187 extend_face_to_end_of_line (it);
16189 /* Make sure we have the position. */
16190 if (used_before == 0)
16191 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16193 /* Consume the line end. This skips over invisible lines. */
16194 set_iterator_to_next (it, 1);
16195 it->continuation_lines_width = 0;
16196 break;
16199 /* Proceed with next display element. Note that this skips
16200 over lines invisible because of selective display. */
16201 set_iterator_to_next (it, 1);
16203 /* If we truncate lines, we are done when the last displayed
16204 glyphs reach past the right margin of the window. */
16205 if (it->truncate_lines_p
16206 && (FRAME_WINDOW_P (it->f)
16207 ? (it->current_x >= it->last_visible_x)
16208 : (it->current_x > it->last_visible_x)))
16210 /* Maybe add truncation glyphs. */
16211 if (!FRAME_WINDOW_P (it->f))
16213 int i, n;
16215 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16216 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16217 break;
16219 for (n = row->used[TEXT_AREA]; i < n; ++i)
16221 row->used[TEXT_AREA] = i;
16222 produce_special_glyphs (it, IT_TRUNCATION);
16225 #ifdef HAVE_WINDOW_SYSTEM
16226 else
16228 /* Don't truncate if we can overflow newline into fringe. */
16229 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16231 if (!get_next_display_element (it))
16233 it->continuation_lines_width = 0;
16234 row->ends_at_zv_p = 1;
16235 row->exact_window_width_line_p = 1;
16236 break;
16238 if (ITERATOR_AT_END_OF_LINE_P (it))
16240 row->exact_window_width_line_p = 1;
16241 goto at_end_of_line;
16245 #endif /* HAVE_WINDOW_SYSTEM */
16247 row->truncated_on_right_p = 1;
16248 it->continuation_lines_width = 0;
16249 reseat_at_next_visible_line_start (it, 0);
16250 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16251 it->hpos = hpos_before;
16252 it->current_x = x_before;
16253 break;
16257 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16258 at the left window margin. */
16259 if (it->first_visible_x
16260 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16262 if (!FRAME_WINDOW_P (it->f))
16263 insert_left_trunc_glyphs (it);
16264 row->truncated_on_left_p = 1;
16267 /* If the start of this line is the overlay arrow-position, then
16268 mark this glyph row as the one containing the overlay arrow.
16269 This is clearly a mess with variable size fonts. It would be
16270 better to let it be displayed like cursors under X. */
16271 if ((row->displays_text_p || !overlay_arrow_seen)
16272 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16273 !NILP (overlay_arrow_string)))
16275 /* Overlay arrow in window redisplay is a fringe bitmap. */
16276 if (STRINGP (overlay_arrow_string))
16278 struct glyph_row *arrow_row
16279 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16280 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16281 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16282 struct glyph *p = row->glyphs[TEXT_AREA];
16283 struct glyph *p2, *end;
16285 /* Copy the arrow glyphs. */
16286 while (glyph < arrow_end)
16287 *p++ = *glyph++;
16289 /* Throw away padding glyphs. */
16290 p2 = p;
16291 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16292 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16293 ++p2;
16294 if (p2 > p)
16296 while (p2 < end)
16297 *p++ = *p2++;
16298 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16301 else
16303 xassert (INTEGERP (overlay_arrow_string));
16304 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16306 overlay_arrow_seen = 1;
16309 /* Compute pixel dimensions of this line. */
16310 compute_line_metrics (it);
16312 /* Remember the position at which this line ends. */
16313 row->end = it->current;
16315 /* Record whether this row ends inside an ellipsis. */
16316 row->ends_in_ellipsis_p
16317 = (it->method == GET_FROM_DISPLAY_VECTOR
16318 && it->ellipsis_p);
16320 /* Save fringe bitmaps in this row. */
16321 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16322 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16323 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16324 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16326 it->left_user_fringe_bitmap = 0;
16327 it->left_user_fringe_face_id = 0;
16328 it->right_user_fringe_bitmap = 0;
16329 it->right_user_fringe_face_id = 0;
16331 /* Maybe set the cursor. */
16332 if (it->w->cursor.vpos < 0
16333 && PT >= MATRIX_ROW_START_CHARPOS (row)
16334 && PT <= MATRIX_ROW_END_CHARPOS (row)
16335 && cursor_row_p (it->w, row))
16336 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16338 /* Highlight trailing whitespace. */
16339 if (!NILP (Vshow_trailing_whitespace))
16340 highlight_trailing_whitespace (it->f, it->glyph_row);
16342 /* Prepare for the next line. This line starts horizontally at (X
16343 HPOS) = (0 0). Vertical positions are incremented. As a
16344 convenience for the caller, IT->glyph_row is set to the next
16345 row to be used. */
16346 it->current_x = it->hpos = 0;
16347 it->current_y += row->height;
16348 ++it->vpos;
16349 ++it->glyph_row;
16350 it->start = it->current;
16351 return row->displays_text_p;
16356 /***********************************************************************
16357 Menu Bar
16358 ***********************************************************************/
16360 /* Redisplay the menu bar in the frame for window W.
16362 The menu bar of X frames that don't have X toolkit support is
16363 displayed in a special window W->frame->menu_bar_window.
16365 The menu bar of terminal frames is treated specially as far as
16366 glyph matrices are concerned. Menu bar lines are not part of
16367 windows, so the update is done directly on the frame matrix rows
16368 for the menu bar. */
16370 static void
16371 display_menu_bar (w)
16372 struct window *w;
16374 struct frame *f = XFRAME (WINDOW_FRAME (w));
16375 struct it it;
16376 Lisp_Object items;
16377 int i;
16379 /* Don't do all this for graphical frames. */
16380 #ifdef HAVE_NTGUI
16381 if (!NILP (Vwindow_system))
16382 return;
16383 #endif
16384 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16385 if (FRAME_X_P (f))
16386 return;
16387 #endif
16388 #ifdef MAC_OS
16389 if (FRAME_MAC_P (f))
16390 return;
16391 #endif
16393 #ifdef USE_X_TOOLKIT
16394 xassert (!FRAME_WINDOW_P (f));
16395 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16396 it.first_visible_x = 0;
16397 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16398 #else /* not USE_X_TOOLKIT */
16399 if (FRAME_WINDOW_P (f))
16401 /* Menu bar lines are displayed in the desired matrix of the
16402 dummy window menu_bar_window. */
16403 struct window *menu_w;
16404 xassert (WINDOWP (f->menu_bar_window));
16405 menu_w = XWINDOW (f->menu_bar_window);
16406 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16407 MENU_FACE_ID);
16408 it.first_visible_x = 0;
16409 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16411 else
16413 /* This is a TTY frame, i.e. character hpos/vpos are used as
16414 pixel x/y. */
16415 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16416 MENU_FACE_ID);
16417 it.first_visible_x = 0;
16418 it.last_visible_x = FRAME_COLS (f);
16420 #endif /* not USE_X_TOOLKIT */
16422 if (! mode_line_inverse_video)
16423 /* Force the menu-bar to be displayed in the default face. */
16424 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16426 /* Clear all rows of the menu bar. */
16427 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16429 struct glyph_row *row = it.glyph_row + i;
16430 clear_glyph_row (row);
16431 row->enabled_p = 1;
16432 row->full_width_p = 1;
16435 /* Display all items of the menu bar. */
16436 items = FRAME_MENU_BAR_ITEMS (it.f);
16437 for (i = 0; i < XVECTOR (items)->size; i += 4)
16439 Lisp_Object string;
16441 /* Stop at nil string. */
16442 string = AREF (items, i + 1);
16443 if (NILP (string))
16444 break;
16446 /* Remember where item was displayed. */
16447 AREF (items, i + 3) = make_number (it.hpos);
16449 /* Display the item, pad with one space. */
16450 if (it.current_x < it.last_visible_x)
16451 display_string (NULL, string, Qnil, 0, 0, &it,
16452 SCHARS (string) + 1, 0, 0, -1);
16455 /* Fill out the line with spaces. */
16456 if (it.current_x < it.last_visible_x)
16457 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16459 /* Compute the total height of the lines. */
16460 compute_line_metrics (&it);
16465 /***********************************************************************
16466 Mode Line
16467 ***********************************************************************/
16469 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16470 FORCE is non-zero, redisplay mode lines unconditionally.
16471 Otherwise, redisplay only mode lines that are garbaged. Value is
16472 the number of windows whose mode lines were redisplayed. */
16474 static int
16475 redisplay_mode_lines (window, force)
16476 Lisp_Object window;
16477 int force;
16479 int nwindows = 0;
16481 while (!NILP (window))
16483 struct window *w = XWINDOW (window);
16485 if (WINDOWP (w->hchild))
16486 nwindows += redisplay_mode_lines (w->hchild, force);
16487 else if (WINDOWP (w->vchild))
16488 nwindows += redisplay_mode_lines (w->vchild, force);
16489 else if (force
16490 || FRAME_GARBAGED_P (XFRAME (w->frame))
16491 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16493 struct text_pos lpoint;
16494 struct buffer *old = current_buffer;
16496 /* Set the window's buffer for the mode line display. */
16497 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16498 set_buffer_internal_1 (XBUFFER (w->buffer));
16500 /* Point refers normally to the selected window. For any
16501 other window, set up appropriate value. */
16502 if (!EQ (window, selected_window))
16504 struct text_pos pt;
16506 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16507 if (CHARPOS (pt) < BEGV)
16508 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16509 else if (CHARPOS (pt) > (ZV - 1))
16510 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16511 else
16512 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16515 /* Display mode lines. */
16516 clear_glyph_matrix (w->desired_matrix);
16517 if (display_mode_lines (w))
16519 ++nwindows;
16520 w->must_be_updated_p = 1;
16523 /* Restore old settings. */
16524 set_buffer_internal_1 (old);
16525 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16528 window = w->next;
16531 return nwindows;
16535 /* Display the mode and/or top line of window W. Value is the number
16536 of mode lines displayed. */
16538 static int
16539 display_mode_lines (w)
16540 struct window *w;
16542 Lisp_Object old_selected_window, old_selected_frame;
16543 int n = 0;
16545 old_selected_frame = selected_frame;
16546 selected_frame = w->frame;
16547 old_selected_window = selected_window;
16548 XSETWINDOW (selected_window, w);
16550 /* These will be set while the mode line specs are processed. */
16551 line_number_displayed = 0;
16552 w->column_number_displayed = Qnil;
16554 if (WINDOW_WANTS_MODELINE_P (w))
16556 struct window *sel_w = XWINDOW (old_selected_window);
16558 /* Select mode line face based on the real selected window. */
16559 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16560 current_buffer->mode_line_format);
16561 ++n;
16564 if (WINDOW_WANTS_HEADER_LINE_P (w))
16566 display_mode_line (w, HEADER_LINE_FACE_ID,
16567 current_buffer->header_line_format);
16568 ++n;
16571 selected_frame = old_selected_frame;
16572 selected_window = old_selected_window;
16573 return n;
16577 /* Display mode or top line of window W. FACE_ID specifies which line
16578 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16579 FORMAT is the mode line format to display. Value is the pixel
16580 height of the mode line displayed. */
16582 static int
16583 display_mode_line (w, face_id, format)
16584 struct window *w;
16585 enum face_id face_id;
16586 Lisp_Object format;
16588 struct it it;
16589 struct face *face;
16590 int count = SPECPDL_INDEX ();
16592 init_iterator (&it, w, -1, -1, NULL, face_id);
16593 prepare_desired_row (it.glyph_row);
16595 it.glyph_row->mode_line_p = 1;
16597 if (! mode_line_inverse_video)
16598 /* Force the mode-line to be displayed in the default face. */
16599 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16601 record_unwind_protect (unwind_format_mode_line,
16602 format_mode_line_unwind_data (NULL, 0));
16604 mode_line_target = MODE_LINE_DISPLAY;
16606 /* Temporarily make frame's keyboard the current kboard so that
16607 kboard-local variables in the mode_line_format will get the right
16608 values. */
16609 push_frame_kboard (it.f);
16610 record_unwind_save_match_data ();
16611 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16612 pop_frame_kboard ();
16614 unbind_to (count, Qnil);
16616 /* Fill up with spaces. */
16617 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16619 compute_line_metrics (&it);
16620 it.glyph_row->full_width_p = 1;
16621 it.glyph_row->continued_p = 0;
16622 it.glyph_row->truncated_on_left_p = 0;
16623 it.glyph_row->truncated_on_right_p = 0;
16625 /* Make a 3D mode-line have a shadow at its right end. */
16626 face = FACE_FROM_ID (it.f, face_id);
16627 extend_face_to_end_of_line (&it);
16628 if (face->box != FACE_NO_BOX)
16630 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16631 + it.glyph_row->used[TEXT_AREA] - 1);
16632 last->right_box_line_p = 1;
16635 return it.glyph_row->height;
16638 /* Move element ELT in LIST to the front of LIST.
16639 Return the updated list. */
16641 static Lisp_Object
16642 move_elt_to_front (elt, list)
16643 Lisp_Object elt, list;
16645 register Lisp_Object tail, prev;
16646 register Lisp_Object tem;
16648 tail = list;
16649 prev = Qnil;
16650 while (CONSP (tail))
16652 tem = XCAR (tail);
16654 if (EQ (elt, tem))
16656 /* Splice out the link TAIL. */
16657 if (NILP (prev))
16658 list = XCDR (tail);
16659 else
16660 Fsetcdr (prev, XCDR (tail));
16662 /* Now make it the first. */
16663 Fsetcdr (tail, list);
16664 return tail;
16666 else
16667 prev = tail;
16668 tail = XCDR (tail);
16669 QUIT;
16672 /* Not found--return unchanged LIST. */
16673 return list;
16676 /* Contribute ELT to the mode line for window IT->w. How it
16677 translates into text depends on its data type.
16679 IT describes the display environment in which we display, as usual.
16681 DEPTH is the depth in recursion. It is used to prevent
16682 infinite recursion here.
16684 FIELD_WIDTH is the number of characters the display of ELT should
16685 occupy in the mode line, and PRECISION is the maximum number of
16686 characters to display from ELT's representation. See
16687 display_string for details.
16689 Returns the hpos of the end of the text generated by ELT.
16691 PROPS is a property list to add to any string we encounter.
16693 If RISKY is nonzero, remove (disregard) any properties in any string
16694 we encounter, and ignore :eval and :propertize.
16696 The global variable `mode_line_target' determines whether the
16697 output is passed to `store_mode_line_noprop',
16698 `store_mode_line_string', or `display_string'. */
16700 static int
16701 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16702 struct it *it;
16703 int depth;
16704 int field_width, precision;
16705 Lisp_Object elt, props;
16706 int risky;
16708 int n = 0, field, prec;
16709 int literal = 0;
16711 tail_recurse:
16712 if (depth > 100)
16713 elt = build_string ("*too-deep*");
16715 depth++;
16717 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16719 case Lisp_String:
16721 /* A string: output it and check for %-constructs within it. */
16722 unsigned char c;
16723 int offset = 0;
16725 if (SCHARS (elt) > 0
16726 && (!NILP (props) || risky))
16728 Lisp_Object oprops, aelt;
16729 oprops = Ftext_properties_at (make_number (0), elt);
16731 /* If the starting string's properties are not what
16732 we want, translate the string. Also, if the string
16733 is risky, do that anyway. */
16735 if (NILP (Fequal (props, oprops)) || risky)
16737 /* If the starting string has properties,
16738 merge the specified ones onto the existing ones. */
16739 if (! NILP (oprops) && !risky)
16741 Lisp_Object tem;
16743 oprops = Fcopy_sequence (oprops);
16744 tem = props;
16745 while (CONSP (tem))
16747 oprops = Fplist_put (oprops, XCAR (tem),
16748 XCAR (XCDR (tem)));
16749 tem = XCDR (XCDR (tem));
16751 props = oprops;
16754 aelt = Fassoc (elt, mode_line_proptrans_alist);
16755 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16757 /* AELT is what we want. Move it to the front
16758 without consing. */
16759 elt = XCAR (aelt);
16760 mode_line_proptrans_alist
16761 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16763 else
16765 Lisp_Object tem;
16767 /* If AELT has the wrong props, it is useless.
16768 so get rid of it. */
16769 if (! NILP (aelt))
16770 mode_line_proptrans_alist
16771 = Fdelq (aelt, mode_line_proptrans_alist);
16773 elt = Fcopy_sequence (elt);
16774 Fset_text_properties (make_number (0), Flength (elt),
16775 props, elt);
16776 /* Add this item to mode_line_proptrans_alist. */
16777 mode_line_proptrans_alist
16778 = Fcons (Fcons (elt, props),
16779 mode_line_proptrans_alist);
16780 /* Truncate mode_line_proptrans_alist
16781 to at most 50 elements. */
16782 tem = Fnthcdr (make_number (50),
16783 mode_line_proptrans_alist);
16784 if (! NILP (tem))
16785 XSETCDR (tem, Qnil);
16790 offset = 0;
16792 if (literal)
16794 prec = precision - n;
16795 switch (mode_line_target)
16797 case MODE_LINE_NOPROP:
16798 case MODE_LINE_TITLE:
16799 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16800 break;
16801 case MODE_LINE_STRING:
16802 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16803 break;
16804 case MODE_LINE_DISPLAY:
16805 n += display_string (NULL, elt, Qnil, 0, 0, it,
16806 0, prec, 0, STRING_MULTIBYTE (elt));
16807 break;
16810 break;
16813 /* Handle the non-literal case. */
16815 while ((precision <= 0 || n < precision)
16816 && SREF (elt, offset) != 0
16817 && (mode_line_target != MODE_LINE_DISPLAY
16818 || it->current_x < it->last_visible_x))
16820 int last_offset = offset;
16822 /* Advance to end of string or next format specifier. */
16823 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16826 if (offset - 1 != last_offset)
16828 int nchars, nbytes;
16830 /* Output to end of string or up to '%'. Field width
16831 is length of string. Don't output more than
16832 PRECISION allows us. */
16833 offset--;
16835 prec = c_string_width (SDATA (elt) + last_offset,
16836 offset - last_offset, precision - n,
16837 &nchars, &nbytes);
16839 switch (mode_line_target)
16841 case MODE_LINE_NOPROP:
16842 case MODE_LINE_TITLE:
16843 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16844 break;
16845 case MODE_LINE_STRING:
16847 int bytepos = last_offset;
16848 int charpos = string_byte_to_char (elt, bytepos);
16849 int endpos = (precision <= 0
16850 ? string_byte_to_char (elt, offset)
16851 : charpos + nchars);
16853 n += store_mode_line_string (NULL,
16854 Fsubstring (elt, make_number (charpos),
16855 make_number (endpos)),
16856 0, 0, 0, Qnil);
16858 break;
16859 case MODE_LINE_DISPLAY:
16861 int bytepos = last_offset;
16862 int charpos = string_byte_to_char (elt, bytepos);
16864 if (precision <= 0)
16865 nchars = string_byte_to_char (elt, offset) - charpos;
16866 n += display_string (NULL, elt, Qnil, 0, charpos,
16867 it, 0, nchars, 0,
16868 STRING_MULTIBYTE (elt));
16870 break;
16873 else /* c == '%' */
16875 int percent_position = offset;
16877 /* Get the specified minimum width. Zero means
16878 don't pad. */
16879 field = 0;
16880 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16881 field = field * 10 + c - '0';
16883 /* Don't pad beyond the total padding allowed. */
16884 if (field_width - n > 0 && field > field_width - n)
16885 field = field_width - n;
16887 /* Note that either PRECISION <= 0 or N < PRECISION. */
16888 prec = precision - n;
16890 if (c == 'M')
16891 n += display_mode_element (it, depth, field, prec,
16892 Vglobal_mode_string, props,
16893 risky);
16894 else if (c != 0)
16896 int multibyte;
16897 int bytepos, charpos;
16898 unsigned char *spec;
16900 bytepos = percent_position;
16901 charpos = (STRING_MULTIBYTE (elt)
16902 ? string_byte_to_char (elt, bytepos)
16903 : bytepos);
16905 spec
16906 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16908 switch (mode_line_target)
16910 case MODE_LINE_NOPROP:
16911 case MODE_LINE_TITLE:
16912 n += store_mode_line_noprop (spec, field, prec);
16913 break;
16914 case MODE_LINE_STRING:
16916 int len = strlen (spec);
16917 Lisp_Object tem = make_string (spec, len);
16918 props = Ftext_properties_at (make_number (charpos), elt);
16919 /* Should only keep face property in props */
16920 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16922 break;
16923 case MODE_LINE_DISPLAY:
16925 int nglyphs_before, nwritten;
16927 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16928 nwritten = display_string (spec, Qnil, elt,
16929 charpos, 0, it,
16930 field, prec, 0,
16931 multibyte);
16933 /* Assign to the glyphs written above the
16934 string where the `%x' came from, position
16935 of the `%'. */
16936 if (nwritten > 0)
16938 struct glyph *glyph
16939 = (it->glyph_row->glyphs[TEXT_AREA]
16940 + nglyphs_before);
16941 int i;
16943 for (i = 0; i < nwritten; ++i)
16945 glyph[i].object = elt;
16946 glyph[i].charpos = charpos;
16949 n += nwritten;
16952 break;
16955 else /* c == 0 */
16956 break;
16960 break;
16962 case Lisp_Symbol:
16963 /* A symbol: process the value of the symbol recursively
16964 as if it appeared here directly. Avoid error if symbol void.
16965 Special case: if value of symbol is a string, output the string
16966 literally. */
16968 register Lisp_Object tem;
16970 /* If the variable is not marked as risky to set
16971 then its contents are risky to use. */
16972 if (NILP (Fget (elt, Qrisky_local_variable)))
16973 risky = 1;
16975 tem = Fboundp (elt);
16976 if (!NILP (tem))
16978 tem = Fsymbol_value (elt);
16979 /* If value is a string, output that string literally:
16980 don't check for % within it. */
16981 if (STRINGP (tem))
16982 literal = 1;
16984 if (!EQ (tem, elt))
16986 /* Give up right away for nil or t. */
16987 elt = tem;
16988 goto tail_recurse;
16992 break;
16994 case Lisp_Cons:
16996 register Lisp_Object car, tem;
16998 /* A cons cell: five distinct cases.
16999 If first element is :eval or :propertize, do something special.
17000 If first element is a string or a cons, process all the elements
17001 and effectively concatenate them.
17002 If first element is a negative number, truncate displaying cdr to
17003 at most that many characters. If positive, pad (with spaces)
17004 to at least that many characters.
17005 If first element is a symbol, process the cadr or caddr recursively
17006 according to whether the symbol's value is non-nil or nil. */
17007 car = XCAR (elt);
17008 if (EQ (car, QCeval))
17010 /* An element of the form (:eval FORM) means evaluate FORM
17011 and use the result as mode line elements. */
17013 if (risky)
17014 break;
17016 if (CONSP (XCDR (elt)))
17018 Lisp_Object spec;
17019 spec = safe_eval (XCAR (XCDR (elt)));
17020 n += display_mode_element (it, depth, field_width - n,
17021 precision - n, spec, props,
17022 risky);
17025 else if (EQ (car, QCpropertize))
17027 /* An element of the form (:propertize ELT PROPS...)
17028 means display ELT but applying properties PROPS. */
17030 if (risky)
17031 break;
17033 if (CONSP (XCDR (elt)))
17034 n += display_mode_element (it, depth, field_width - n,
17035 precision - n, XCAR (XCDR (elt)),
17036 XCDR (XCDR (elt)), risky);
17038 else if (SYMBOLP (car))
17040 tem = Fboundp (car);
17041 elt = XCDR (elt);
17042 if (!CONSP (elt))
17043 goto invalid;
17044 /* elt is now the cdr, and we know it is a cons cell.
17045 Use its car if CAR has a non-nil value. */
17046 if (!NILP (tem))
17048 tem = Fsymbol_value (car);
17049 if (!NILP (tem))
17051 elt = XCAR (elt);
17052 goto tail_recurse;
17055 /* Symbol's value is nil (or symbol is unbound)
17056 Get the cddr of the original list
17057 and if possible find the caddr and use that. */
17058 elt = XCDR (elt);
17059 if (NILP (elt))
17060 break;
17061 else if (!CONSP (elt))
17062 goto invalid;
17063 elt = XCAR (elt);
17064 goto tail_recurse;
17066 else if (INTEGERP (car))
17068 register int lim = XINT (car);
17069 elt = XCDR (elt);
17070 if (lim < 0)
17072 /* Negative int means reduce maximum width. */
17073 if (precision <= 0)
17074 precision = -lim;
17075 else
17076 precision = min (precision, -lim);
17078 else if (lim > 0)
17080 /* Padding specified. Don't let it be more than
17081 current maximum. */
17082 if (precision > 0)
17083 lim = min (precision, lim);
17085 /* If that's more padding than already wanted, queue it.
17086 But don't reduce padding already specified even if
17087 that is beyond the current truncation point. */
17088 field_width = max (lim, field_width);
17090 goto tail_recurse;
17092 else if (STRINGP (car) || CONSP (car))
17094 register int limit = 50;
17095 /* Limit is to protect against circular lists. */
17096 while (CONSP (elt)
17097 && --limit > 0
17098 && (precision <= 0 || n < precision))
17100 n += display_mode_element (it, depth,
17101 /* Do padding only after the last
17102 element in the list. */
17103 (! CONSP (XCDR (elt))
17104 ? field_width - n
17105 : 0),
17106 precision - n, XCAR (elt),
17107 props, risky);
17108 elt = XCDR (elt);
17112 break;
17114 default:
17115 invalid:
17116 elt = build_string ("*invalid*");
17117 goto tail_recurse;
17120 /* Pad to FIELD_WIDTH. */
17121 if (field_width > 0 && n < field_width)
17123 switch (mode_line_target)
17125 case MODE_LINE_NOPROP:
17126 case MODE_LINE_TITLE:
17127 n += store_mode_line_noprop ("", field_width - n, 0);
17128 break;
17129 case MODE_LINE_STRING:
17130 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17131 break;
17132 case MODE_LINE_DISPLAY:
17133 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17134 0, 0, 0);
17135 break;
17139 return n;
17142 /* Store a mode-line string element in mode_line_string_list.
17144 If STRING is non-null, display that C string. Otherwise, the Lisp
17145 string LISP_STRING is displayed.
17147 FIELD_WIDTH is the minimum number of output glyphs to produce.
17148 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17149 with spaces. FIELD_WIDTH <= 0 means don't pad.
17151 PRECISION is the maximum number of characters to output from
17152 STRING. PRECISION <= 0 means don't truncate the string.
17154 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17155 properties to the string.
17157 PROPS are the properties to add to the string.
17158 The mode_line_string_face face property is always added to the string.
17161 static int
17162 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17163 char *string;
17164 Lisp_Object lisp_string;
17165 int copy_string;
17166 int field_width;
17167 int precision;
17168 Lisp_Object props;
17170 int len;
17171 int n = 0;
17173 if (string != NULL)
17175 len = strlen (string);
17176 if (precision > 0 && len > precision)
17177 len = precision;
17178 lisp_string = make_string (string, len);
17179 if (NILP (props))
17180 props = mode_line_string_face_prop;
17181 else if (!NILP (mode_line_string_face))
17183 Lisp_Object face = Fplist_get (props, Qface);
17184 props = Fcopy_sequence (props);
17185 if (NILP (face))
17186 face = mode_line_string_face;
17187 else
17188 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17189 props = Fplist_put (props, Qface, face);
17191 Fadd_text_properties (make_number (0), make_number (len),
17192 props, lisp_string);
17194 else
17196 len = XFASTINT (Flength (lisp_string));
17197 if (precision > 0 && len > precision)
17199 len = precision;
17200 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17201 precision = -1;
17203 if (!NILP (mode_line_string_face))
17205 Lisp_Object face;
17206 if (NILP (props))
17207 props = Ftext_properties_at (make_number (0), lisp_string);
17208 face = Fplist_get (props, Qface);
17209 if (NILP (face))
17210 face = mode_line_string_face;
17211 else
17212 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17213 props = Fcons (Qface, Fcons (face, Qnil));
17214 if (copy_string)
17215 lisp_string = Fcopy_sequence (lisp_string);
17217 if (!NILP (props))
17218 Fadd_text_properties (make_number (0), make_number (len),
17219 props, lisp_string);
17222 if (len > 0)
17224 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17225 n += len;
17228 if (field_width > len)
17230 field_width -= len;
17231 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17232 if (!NILP (props))
17233 Fadd_text_properties (make_number (0), make_number (field_width),
17234 props, lisp_string);
17235 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17236 n += field_width;
17239 return n;
17243 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17244 1, 4, 0,
17245 doc: /* Format a string out of a mode line format specification.
17246 First arg FORMAT specifies the mode line format (see `mode-line-format'
17247 for details) to use.
17249 Optional second arg FACE specifies the face property to put
17250 on all characters for which no face is specified.
17251 t means whatever face the window's mode line currently uses
17252 \(either `mode-line' or `mode-line-inactive', depending).
17253 nil means the default is no face property.
17254 If FACE is an integer, the value string has no text properties.
17256 Optional third and fourth args WINDOW and BUFFER specify the window
17257 and buffer to use as the context for the formatting (defaults
17258 are the selected window and the window's buffer). */)
17259 (format, face, window, buffer)
17260 Lisp_Object format, face, window, buffer;
17262 struct it it;
17263 int len;
17264 struct window *w;
17265 struct buffer *old_buffer = NULL;
17266 int face_id = -1;
17267 int no_props = INTEGERP (face);
17268 int count = SPECPDL_INDEX ();
17269 Lisp_Object str;
17270 int string_start = 0;
17272 if (NILP (window))
17273 window = selected_window;
17274 CHECK_WINDOW (window);
17275 w = XWINDOW (window);
17277 if (NILP (buffer))
17278 buffer = w->buffer;
17279 CHECK_BUFFER (buffer);
17281 if (NILP (format))
17282 return build_string ("");
17284 if (no_props)
17285 face = Qnil;
17287 if (!NILP (face))
17289 if (EQ (face, Qt))
17290 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17291 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17294 if (face_id < 0)
17295 face_id = DEFAULT_FACE_ID;
17297 if (XBUFFER (buffer) != current_buffer)
17298 old_buffer = current_buffer;
17300 /* Save things including mode_line_proptrans_alist,
17301 and set that to nil so that we don't alter the outer value. */
17302 record_unwind_protect (unwind_format_mode_line,
17303 format_mode_line_unwind_data (old_buffer, 1));
17304 mode_line_proptrans_alist = Qnil;
17306 if (old_buffer)
17307 set_buffer_internal_1 (XBUFFER (buffer));
17309 init_iterator (&it, w, -1, -1, NULL, face_id);
17311 if (no_props)
17313 mode_line_target = MODE_LINE_NOPROP;
17314 mode_line_string_face_prop = Qnil;
17315 mode_line_string_list = Qnil;
17316 string_start = MODE_LINE_NOPROP_LEN (0);
17318 else
17320 mode_line_target = MODE_LINE_STRING;
17321 mode_line_string_list = Qnil;
17322 mode_line_string_face = face;
17323 mode_line_string_face_prop
17324 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17327 push_frame_kboard (it.f);
17328 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17329 pop_frame_kboard ();
17331 if (no_props)
17333 len = MODE_LINE_NOPROP_LEN (string_start);
17334 str = make_string (mode_line_noprop_buf + string_start, len);
17336 else
17338 mode_line_string_list = Fnreverse (mode_line_string_list);
17339 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17340 make_string ("", 0));
17343 unbind_to (count, Qnil);
17344 return str;
17347 /* Write a null-terminated, right justified decimal representation of
17348 the positive integer D to BUF using a minimal field width WIDTH. */
17350 static void
17351 pint2str (buf, width, d)
17352 register char *buf;
17353 register int width;
17354 register int d;
17356 register char *p = buf;
17358 if (d <= 0)
17359 *p++ = '0';
17360 else
17362 while (d > 0)
17364 *p++ = d % 10 + '0';
17365 d /= 10;
17369 for (width -= (int) (p - buf); width > 0; --width)
17370 *p++ = ' ';
17371 *p-- = '\0';
17372 while (p > buf)
17374 d = *buf;
17375 *buf++ = *p;
17376 *p-- = d;
17380 /* Write a null-terminated, right justified decimal and "human
17381 readable" representation of the nonnegative integer D to BUF using
17382 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17384 static const char power_letter[] =
17386 0, /* not used */
17387 'k', /* kilo */
17388 'M', /* mega */
17389 'G', /* giga */
17390 'T', /* tera */
17391 'P', /* peta */
17392 'E', /* exa */
17393 'Z', /* zetta */
17394 'Y' /* yotta */
17397 static void
17398 pint2hrstr (buf, width, d)
17399 char *buf;
17400 int width;
17401 int d;
17403 /* We aim to represent the nonnegative integer D as
17404 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17405 int quotient = d;
17406 int remainder = 0;
17407 /* -1 means: do not use TENTHS. */
17408 int tenths = -1;
17409 int exponent = 0;
17411 /* Length of QUOTIENT.TENTHS as a string. */
17412 int length;
17414 char * psuffix;
17415 char * p;
17417 if (1000 <= quotient)
17419 /* Scale to the appropriate EXPONENT. */
17422 remainder = quotient % 1000;
17423 quotient /= 1000;
17424 exponent++;
17426 while (1000 <= quotient);
17428 /* Round to nearest and decide whether to use TENTHS or not. */
17429 if (quotient <= 9)
17431 tenths = remainder / 100;
17432 if (50 <= remainder % 100)
17434 if (tenths < 9)
17435 tenths++;
17436 else
17438 quotient++;
17439 if (quotient == 10)
17440 tenths = -1;
17441 else
17442 tenths = 0;
17446 else
17447 if (500 <= remainder)
17449 if (quotient < 999)
17450 quotient++;
17451 else
17453 quotient = 1;
17454 exponent++;
17455 tenths = 0;
17460 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17461 if (tenths == -1 && quotient <= 99)
17462 if (quotient <= 9)
17463 length = 1;
17464 else
17465 length = 2;
17466 else
17467 length = 3;
17468 p = psuffix = buf + max (width, length);
17470 /* Print EXPONENT. */
17471 if (exponent)
17472 *psuffix++ = power_letter[exponent];
17473 *psuffix = '\0';
17475 /* Print TENTHS. */
17476 if (tenths >= 0)
17478 *--p = '0' + tenths;
17479 *--p = '.';
17482 /* Print QUOTIENT. */
17485 int digit = quotient % 10;
17486 *--p = '0' + digit;
17488 while ((quotient /= 10) != 0);
17490 /* Print leading spaces. */
17491 while (buf < p)
17492 *--p = ' ';
17495 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17496 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17497 type of CODING_SYSTEM. Return updated pointer into BUF. */
17499 static unsigned char invalid_eol_type[] = "(*invalid*)";
17501 static char *
17502 decode_mode_spec_coding (coding_system, buf, eol_flag)
17503 Lisp_Object coding_system;
17504 register char *buf;
17505 int eol_flag;
17507 Lisp_Object val;
17508 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17509 const unsigned char *eol_str;
17510 int eol_str_len;
17511 /* The EOL conversion we are using. */
17512 Lisp_Object eoltype;
17514 val = Fget (coding_system, Qcoding_system);
17515 eoltype = Qnil;
17517 if (!VECTORP (val)) /* Not yet decided. */
17519 if (multibyte)
17520 *buf++ = '-';
17521 if (eol_flag)
17522 eoltype = eol_mnemonic_undecided;
17523 /* Don't mention EOL conversion if it isn't decided. */
17525 else
17527 Lisp_Object eolvalue;
17529 eolvalue = Fget (coding_system, Qeol_type);
17531 if (multibyte)
17532 *buf++ = XFASTINT (AREF (val, 1));
17534 if (eol_flag)
17536 /* The EOL conversion that is normal on this system. */
17538 if (NILP (eolvalue)) /* Not yet decided. */
17539 eoltype = eol_mnemonic_undecided;
17540 else if (VECTORP (eolvalue)) /* Not yet decided. */
17541 eoltype = eol_mnemonic_undecided;
17542 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17543 eoltype = (XFASTINT (eolvalue) == 0
17544 ? eol_mnemonic_unix
17545 : (XFASTINT (eolvalue) == 1
17546 ? eol_mnemonic_dos : eol_mnemonic_mac));
17550 if (eol_flag)
17552 /* Mention the EOL conversion if it is not the usual one. */
17553 if (STRINGP (eoltype))
17555 eol_str = SDATA (eoltype);
17556 eol_str_len = SBYTES (eoltype);
17558 else if (INTEGERP (eoltype)
17559 && CHAR_VALID_P (XINT (eoltype), 0))
17561 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17562 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17563 eol_str = tmp;
17565 else
17567 eol_str = invalid_eol_type;
17568 eol_str_len = sizeof (invalid_eol_type) - 1;
17570 bcopy (eol_str, buf, eol_str_len);
17571 buf += eol_str_len;
17574 return buf;
17577 /* Return a string for the output of a mode line %-spec for window W,
17578 generated by character C. PRECISION >= 0 means don't return a
17579 string longer than that value. FIELD_WIDTH > 0 means pad the
17580 string returned with spaces to that value. Return 1 in *MULTIBYTE
17581 if the result is multibyte text.
17583 Note we operate on the current buffer for most purposes,
17584 the exception being w->base_line_pos. */
17586 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17588 static char *
17589 decode_mode_spec (w, c, field_width, precision, multibyte)
17590 struct window *w;
17591 register int c;
17592 int field_width, precision;
17593 int *multibyte;
17595 Lisp_Object obj;
17596 struct frame *f = XFRAME (WINDOW_FRAME (w));
17597 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17598 struct buffer *b = current_buffer;
17600 obj = Qnil;
17601 *multibyte = 0;
17603 switch (c)
17605 case '*':
17606 if (!NILP (b->read_only))
17607 return "%";
17608 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17609 return "*";
17610 return "-";
17612 case '+':
17613 /* This differs from %* only for a modified read-only buffer. */
17614 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17615 return "*";
17616 if (!NILP (b->read_only))
17617 return "%";
17618 return "-";
17620 case '&':
17621 /* This differs from %* in ignoring read-only-ness. */
17622 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17623 return "*";
17624 return "-";
17626 case '%':
17627 return "%";
17629 case '[':
17631 int i;
17632 char *p;
17634 if (command_loop_level > 5)
17635 return "[[[... ";
17636 p = decode_mode_spec_buf;
17637 for (i = 0; i < command_loop_level; i++)
17638 *p++ = '[';
17639 *p = 0;
17640 return decode_mode_spec_buf;
17643 case ']':
17645 int i;
17646 char *p;
17648 if (command_loop_level > 5)
17649 return " ...]]]";
17650 p = decode_mode_spec_buf;
17651 for (i = 0; i < command_loop_level; i++)
17652 *p++ = ']';
17653 *p = 0;
17654 return decode_mode_spec_buf;
17657 case '-':
17659 register int i;
17661 /* Let lots_of_dashes be a string of infinite length. */
17662 if (mode_line_target == MODE_LINE_NOPROP ||
17663 mode_line_target == MODE_LINE_STRING)
17664 return "--";
17665 if (field_width <= 0
17666 || field_width > sizeof (lots_of_dashes))
17668 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17669 decode_mode_spec_buf[i] = '-';
17670 decode_mode_spec_buf[i] = '\0';
17671 return decode_mode_spec_buf;
17673 else
17674 return lots_of_dashes;
17677 case 'b':
17678 obj = b->name;
17679 break;
17681 case 'c':
17683 int col = (int) current_column (); /* iftc */
17684 w->column_number_displayed = make_number (col);
17685 pint2str (decode_mode_spec_buf, field_width, col);
17686 return decode_mode_spec_buf;
17689 case 'e':
17690 #ifndef SYSTEM_MALLOC
17692 if (NILP (Vmemory_full))
17693 return "";
17694 else
17695 return "!MEM FULL! ";
17697 #else
17698 return "";
17699 #endif
17701 case 'F':
17702 /* %F displays the frame name. */
17703 if (!NILP (f->title))
17704 return (char *) SDATA (f->title);
17705 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17706 return (char *) SDATA (f->name);
17707 return "Emacs";
17709 case 'f':
17710 obj = b->filename;
17711 break;
17713 case 'i':
17715 int size = ZV - BEGV;
17716 pint2str (decode_mode_spec_buf, field_width, size);
17717 return decode_mode_spec_buf;
17720 case 'I':
17722 int size = ZV - BEGV;
17723 pint2hrstr (decode_mode_spec_buf, field_width, size);
17724 return decode_mode_spec_buf;
17727 case 'l':
17729 int startpos = XMARKER (w->start)->charpos;
17730 int startpos_byte = marker_byte_position (w->start);
17731 int line, linepos, linepos_byte, topline;
17732 int nlines, junk;
17733 int height = WINDOW_TOTAL_LINES (w);
17735 /* If we decided that this buffer isn't suitable for line numbers,
17736 don't forget that too fast. */
17737 if (EQ (w->base_line_pos, w->buffer))
17738 goto no_value;
17739 /* But do forget it, if the window shows a different buffer now. */
17740 else if (BUFFERP (w->base_line_pos))
17741 w->base_line_pos = Qnil;
17743 /* If the buffer is very big, don't waste time. */
17744 if (INTEGERP (Vline_number_display_limit)
17745 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17747 w->base_line_pos = Qnil;
17748 w->base_line_number = Qnil;
17749 goto no_value;
17752 if (!NILP (w->base_line_number)
17753 && !NILP (w->base_line_pos)
17754 && XFASTINT (w->base_line_pos) <= startpos)
17756 line = XFASTINT (w->base_line_number);
17757 linepos = XFASTINT (w->base_line_pos);
17758 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17760 else
17762 line = 1;
17763 linepos = BUF_BEGV (b);
17764 linepos_byte = BUF_BEGV_BYTE (b);
17767 /* Count lines from base line to window start position. */
17768 nlines = display_count_lines (linepos, linepos_byte,
17769 startpos_byte,
17770 startpos, &junk);
17772 topline = nlines + line;
17774 /* Determine a new base line, if the old one is too close
17775 or too far away, or if we did not have one.
17776 "Too close" means it's plausible a scroll-down would
17777 go back past it. */
17778 if (startpos == BUF_BEGV (b))
17780 w->base_line_number = make_number (topline);
17781 w->base_line_pos = make_number (BUF_BEGV (b));
17783 else if (nlines < height + 25 || nlines > height * 3 + 50
17784 || linepos == BUF_BEGV (b))
17786 int limit = BUF_BEGV (b);
17787 int limit_byte = BUF_BEGV_BYTE (b);
17788 int position;
17789 int distance = (height * 2 + 30) * line_number_display_limit_width;
17791 if (startpos - distance > limit)
17793 limit = startpos - distance;
17794 limit_byte = CHAR_TO_BYTE (limit);
17797 nlines = display_count_lines (startpos, startpos_byte,
17798 limit_byte,
17799 - (height * 2 + 30),
17800 &position);
17801 /* If we couldn't find the lines we wanted within
17802 line_number_display_limit_width chars per line,
17803 give up on line numbers for this window. */
17804 if (position == limit_byte && limit == startpos - distance)
17806 w->base_line_pos = w->buffer;
17807 w->base_line_number = Qnil;
17808 goto no_value;
17811 w->base_line_number = make_number (topline - nlines);
17812 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17815 /* Now count lines from the start pos to point. */
17816 nlines = display_count_lines (startpos, startpos_byte,
17817 PT_BYTE, PT, &junk);
17819 /* Record that we did display the line number. */
17820 line_number_displayed = 1;
17822 /* Make the string to show. */
17823 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17824 return decode_mode_spec_buf;
17825 no_value:
17827 char* p = decode_mode_spec_buf;
17828 int pad = field_width - 2;
17829 while (pad-- > 0)
17830 *p++ = ' ';
17831 *p++ = '?';
17832 *p++ = '?';
17833 *p = '\0';
17834 return decode_mode_spec_buf;
17837 break;
17839 case 'm':
17840 obj = b->mode_name;
17841 break;
17843 case 'n':
17844 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17845 return " Narrow";
17846 break;
17848 case 'p':
17850 int pos = marker_position (w->start);
17851 int total = BUF_ZV (b) - BUF_BEGV (b);
17853 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17855 if (pos <= BUF_BEGV (b))
17856 return "All";
17857 else
17858 return "Bottom";
17860 else if (pos <= BUF_BEGV (b))
17861 return "Top";
17862 else
17864 if (total > 1000000)
17865 /* Do it differently for a large value, to avoid overflow. */
17866 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17867 else
17868 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17869 /* We can't normally display a 3-digit number,
17870 so get us a 2-digit number that is close. */
17871 if (total == 100)
17872 total = 99;
17873 sprintf (decode_mode_spec_buf, "%2d%%", total);
17874 return decode_mode_spec_buf;
17878 /* Display percentage of size above the bottom of the screen. */
17879 case 'P':
17881 int toppos = marker_position (w->start);
17882 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17883 int total = BUF_ZV (b) - BUF_BEGV (b);
17885 if (botpos >= BUF_ZV (b))
17887 if (toppos <= BUF_BEGV (b))
17888 return "All";
17889 else
17890 return "Bottom";
17892 else
17894 if (total > 1000000)
17895 /* Do it differently for a large value, to avoid overflow. */
17896 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17897 else
17898 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17899 /* We can't normally display a 3-digit number,
17900 so get us a 2-digit number that is close. */
17901 if (total == 100)
17902 total = 99;
17903 if (toppos <= BUF_BEGV (b))
17904 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17905 else
17906 sprintf (decode_mode_spec_buf, "%2d%%", total);
17907 return decode_mode_spec_buf;
17911 case 's':
17912 /* status of process */
17913 obj = Fget_buffer_process (Fcurrent_buffer ());
17914 if (NILP (obj))
17915 return "no process";
17916 #ifdef subprocesses
17917 obj = Fsymbol_name (Fprocess_status (obj));
17918 #endif
17919 break;
17921 case 't': /* indicate TEXT or BINARY */
17922 #ifdef MODE_LINE_BINARY_TEXT
17923 return MODE_LINE_BINARY_TEXT (b);
17924 #else
17925 return "T";
17926 #endif
17928 case 'z':
17929 /* coding-system (not including end-of-line format) */
17930 case 'Z':
17931 /* coding-system (including end-of-line type) */
17933 int eol_flag = (c == 'Z');
17934 char *p = decode_mode_spec_buf;
17936 if (! FRAME_WINDOW_P (f))
17938 /* No need to mention EOL here--the terminal never needs
17939 to do EOL conversion. */
17940 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17941 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17943 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17944 p, eol_flag);
17946 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17947 #ifdef subprocesses
17948 obj = Fget_buffer_process (Fcurrent_buffer ());
17949 if (PROCESSP (obj))
17951 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17952 p, eol_flag);
17953 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17954 p, eol_flag);
17956 #endif /* subprocesses */
17957 #endif /* 0 */
17958 *p = 0;
17959 return decode_mode_spec_buf;
17963 if (STRINGP (obj))
17965 *multibyte = STRING_MULTIBYTE (obj);
17966 return (char *) SDATA (obj);
17968 else
17969 return "";
17973 /* Count up to COUNT lines starting from START / START_BYTE.
17974 But don't go beyond LIMIT_BYTE.
17975 Return the number of lines thus found (always nonnegative).
17977 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17979 static int
17980 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17981 int start, start_byte, limit_byte, count;
17982 int *byte_pos_ptr;
17984 register unsigned char *cursor;
17985 unsigned char *base;
17987 register int ceiling;
17988 register unsigned char *ceiling_addr;
17989 int orig_count = count;
17991 /* If we are not in selective display mode,
17992 check only for newlines. */
17993 int selective_display = (!NILP (current_buffer->selective_display)
17994 && !INTEGERP (current_buffer->selective_display));
17996 if (count > 0)
17998 while (start_byte < limit_byte)
18000 ceiling = BUFFER_CEILING_OF (start_byte);
18001 ceiling = min (limit_byte - 1, ceiling);
18002 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18003 base = (cursor = BYTE_POS_ADDR (start_byte));
18004 while (1)
18006 if (selective_display)
18007 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18009 else
18010 while (*cursor != '\n' && ++cursor != ceiling_addr)
18013 if (cursor != ceiling_addr)
18015 if (--count == 0)
18017 start_byte += cursor - base + 1;
18018 *byte_pos_ptr = start_byte;
18019 return orig_count;
18021 else
18022 if (++cursor == ceiling_addr)
18023 break;
18025 else
18026 break;
18028 start_byte += cursor - base;
18031 else
18033 while (start_byte > limit_byte)
18035 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18036 ceiling = max (limit_byte, ceiling);
18037 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18038 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18039 while (1)
18041 if (selective_display)
18042 while (--cursor != ceiling_addr
18043 && *cursor != '\n' && *cursor != 015)
18045 else
18046 while (--cursor != ceiling_addr && *cursor != '\n')
18049 if (cursor != ceiling_addr)
18051 if (++count == 0)
18053 start_byte += cursor - base + 1;
18054 *byte_pos_ptr = start_byte;
18055 /* When scanning backwards, we should
18056 not count the newline posterior to which we stop. */
18057 return - orig_count - 1;
18060 else
18061 break;
18063 /* Here we add 1 to compensate for the last decrement
18064 of CURSOR, which took it past the valid range. */
18065 start_byte += cursor - base + 1;
18069 *byte_pos_ptr = limit_byte;
18071 if (count < 0)
18072 return - orig_count + count;
18073 return orig_count - count;
18079 /***********************************************************************
18080 Displaying strings
18081 ***********************************************************************/
18083 /* Display a NUL-terminated string, starting with index START.
18085 If STRING is non-null, display that C string. Otherwise, the Lisp
18086 string LISP_STRING is displayed.
18088 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18089 FACE_STRING. Display STRING or LISP_STRING with the face at
18090 FACE_STRING_POS in FACE_STRING:
18092 Display the string in the environment given by IT, but use the
18093 standard display table, temporarily.
18095 FIELD_WIDTH is the minimum number of output glyphs to produce.
18096 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18097 with spaces. If STRING has more characters, more than FIELD_WIDTH
18098 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18100 PRECISION is the maximum number of characters to output from
18101 STRING. PRECISION < 0 means don't truncate the string.
18103 This is roughly equivalent to printf format specifiers:
18105 FIELD_WIDTH PRECISION PRINTF
18106 ----------------------------------------
18107 -1 -1 %s
18108 -1 10 %.10s
18109 10 -1 %10s
18110 20 10 %20.10s
18112 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18113 display them, and < 0 means obey the current buffer's value of
18114 enable_multibyte_characters.
18116 Value is the number of columns displayed. */
18118 static int
18119 display_string (string, lisp_string, face_string, face_string_pos,
18120 start, it, field_width, precision, max_x, multibyte)
18121 unsigned char *string;
18122 Lisp_Object lisp_string;
18123 Lisp_Object face_string;
18124 int face_string_pos;
18125 int start;
18126 struct it *it;
18127 int field_width, precision, max_x;
18128 int multibyte;
18130 int hpos_at_start = it->hpos;
18131 int saved_face_id = it->face_id;
18132 struct glyph_row *row = it->glyph_row;
18134 /* Initialize the iterator IT for iteration over STRING beginning
18135 with index START. */
18136 reseat_to_string (it, string, lisp_string, start,
18137 precision, field_width, multibyte);
18139 /* If displaying STRING, set up the face of the iterator
18140 from LISP_STRING, if that's given. */
18141 if (STRINGP (face_string))
18143 int endptr;
18144 struct face *face;
18146 it->face_id
18147 = face_at_string_position (it->w, face_string, face_string_pos,
18148 0, it->region_beg_charpos,
18149 it->region_end_charpos,
18150 &endptr, it->base_face_id, 0);
18151 face = FACE_FROM_ID (it->f, it->face_id);
18152 it->face_box_p = face->box != FACE_NO_BOX;
18155 /* Set max_x to the maximum allowed X position. Don't let it go
18156 beyond the right edge of the window. */
18157 if (max_x <= 0)
18158 max_x = it->last_visible_x;
18159 else
18160 max_x = min (max_x, it->last_visible_x);
18162 /* Skip over display elements that are not visible. because IT->w is
18163 hscrolled. */
18164 if (it->current_x < it->first_visible_x)
18165 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18166 MOVE_TO_POS | MOVE_TO_X);
18168 row->ascent = it->max_ascent;
18169 row->height = it->max_ascent + it->max_descent;
18170 row->phys_ascent = it->max_phys_ascent;
18171 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18172 row->extra_line_spacing = it->max_extra_line_spacing;
18174 /* This condition is for the case that we are called with current_x
18175 past last_visible_x. */
18176 while (it->current_x < max_x)
18178 int x_before, x, n_glyphs_before, i, nglyphs;
18180 /* Get the next display element. */
18181 if (!get_next_display_element (it))
18182 break;
18184 /* Produce glyphs. */
18185 x_before = it->current_x;
18186 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18187 PRODUCE_GLYPHS (it);
18189 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18190 i = 0;
18191 x = x_before;
18192 while (i < nglyphs)
18194 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18196 if (!it->truncate_lines_p
18197 && x + glyph->pixel_width > max_x)
18199 /* End of continued line or max_x reached. */
18200 if (CHAR_GLYPH_PADDING_P (*glyph))
18202 /* A wide character is unbreakable. */
18203 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18204 it->current_x = x_before;
18206 else
18208 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18209 it->current_x = x;
18211 break;
18213 else if (x + glyph->pixel_width > it->first_visible_x)
18215 /* Glyph is at least partially visible. */
18216 ++it->hpos;
18217 if (x < it->first_visible_x)
18218 it->glyph_row->x = x - it->first_visible_x;
18220 else
18222 /* Glyph is off the left margin of the display area.
18223 Should not happen. */
18224 abort ();
18227 row->ascent = max (row->ascent, it->max_ascent);
18228 row->height = max (row->height, it->max_ascent + it->max_descent);
18229 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18230 row->phys_height = max (row->phys_height,
18231 it->max_phys_ascent + it->max_phys_descent);
18232 row->extra_line_spacing = max (row->extra_line_spacing,
18233 it->max_extra_line_spacing);
18234 x += glyph->pixel_width;
18235 ++i;
18238 /* Stop if max_x reached. */
18239 if (i < nglyphs)
18240 break;
18242 /* Stop at line ends. */
18243 if (ITERATOR_AT_END_OF_LINE_P (it))
18245 it->continuation_lines_width = 0;
18246 break;
18249 set_iterator_to_next (it, 1);
18251 /* Stop if truncating at the right edge. */
18252 if (it->truncate_lines_p
18253 && it->current_x >= it->last_visible_x)
18255 /* Add truncation mark, but don't do it if the line is
18256 truncated at a padding space. */
18257 if (IT_CHARPOS (*it) < it->string_nchars)
18259 if (!FRAME_WINDOW_P (it->f))
18261 int i, n;
18263 if (it->current_x > it->last_visible_x)
18265 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18266 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18267 break;
18268 for (n = row->used[TEXT_AREA]; i < n; ++i)
18270 row->used[TEXT_AREA] = i;
18271 produce_special_glyphs (it, IT_TRUNCATION);
18274 produce_special_glyphs (it, IT_TRUNCATION);
18276 it->glyph_row->truncated_on_right_p = 1;
18278 break;
18282 /* Maybe insert a truncation at the left. */
18283 if (it->first_visible_x
18284 && IT_CHARPOS (*it) > 0)
18286 if (!FRAME_WINDOW_P (it->f))
18287 insert_left_trunc_glyphs (it);
18288 it->glyph_row->truncated_on_left_p = 1;
18291 it->face_id = saved_face_id;
18293 /* Value is number of columns displayed. */
18294 return it->hpos - hpos_at_start;
18299 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18300 appears as an element of LIST or as the car of an element of LIST.
18301 If PROPVAL is a list, compare each element against LIST in that
18302 way, and return 1/2 if any element of PROPVAL is found in LIST.
18303 Otherwise return 0. This function cannot quit.
18304 The return value is 2 if the text is invisible but with an ellipsis
18305 and 1 if it's invisible and without an ellipsis. */
18308 invisible_p (propval, list)
18309 register Lisp_Object propval;
18310 Lisp_Object list;
18312 register Lisp_Object tail, proptail;
18314 for (tail = list; CONSP (tail); tail = XCDR (tail))
18316 register Lisp_Object tem;
18317 tem = XCAR (tail);
18318 if (EQ (propval, tem))
18319 return 1;
18320 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18321 return NILP (XCDR (tem)) ? 1 : 2;
18324 if (CONSP (propval))
18326 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18328 Lisp_Object propelt;
18329 propelt = XCAR (proptail);
18330 for (tail = list; CONSP (tail); tail = XCDR (tail))
18332 register Lisp_Object tem;
18333 tem = XCAR (tail);
18334 if (EQ (propelt, tem))
18335 return 1;
18336 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18337 return NILP (XCDR (tem)) ? 1 : 2;
18342 return 0;
18345 /* Calculate a width or height in pixels from a specification using
18346 the following elements:
18348 SPEC ::=
18349 NUM - a (fractional) multiple of the default font width/height
18350 (NUM) - specifies exactly NUM pixels
18351 UNIT - a fixed number of pixels, see below.
18352 ELEMENT - size of a display element in pixels, see below.
18353 (NUM . SPEC) - equals NUM * SPEC
18354 (+ SPEC SPEC ...) - add pixel values
18355 (- SPEC SPEC ...) - subtract pixel values
18356 (- SPEC) - negate pixel value
18358 NUM ::=
18359 INT or FLOAT - a number constant
18360 SYMBOL - use symbol's (buffer local) variable binding.
18362 UNIT ::=
18363 in - pixels per inch *)
18364 mm - pixels per 1/1000 meter *)
18365 cm - pixels per 1/100 meter *)
18366 width - width of current font in pixels.
18367 height - height of current font in pixels.
18369 *) using the ratio(s) defined in display-pixels-per-inch.
18371 ELEMENT ::=
18373 left-fringe - left fringe width in pixels
18374 right-fringe - right fringe width in pixels
18376 left-margin - left margin width in pixels
18377 right-margin - right margin width in pixels
18379 scroll-bar - scroll-bar area width in pixels
18381 Examples:
18383 Pixels corresponding to 5 inches:
18384 (5 . in)
18386 Total width of non-text areas on left side of window (if scroll-bar is on left):
18387 '(space :width (+ left-fringe left-margin scroll-bar))
18389 Align to first text column (in header line):
18390 '(space :align-to 0)
18392 Align to middle of text area minus half the width of variable `my-image'
18393 containing a loaded image:
18394 '(space :align-to (0.5 . (- text my-image)))
18396 Width of left margin minus width of 1 character in the default font:
18397 '(space :width (- left-margin 1))
18399 Width of left margin minus width of 2 characters in the current font:
18400 '(space :width (- left-margin (2 . width)))
18402 Center 1 character over left-margin (in header line):
18403 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18405 Different ways to express width of left fringe plus left margin minus one pixel:
18406 '(space :width (- (+ left-fringe left-margin) (1)))
18407 '(space :width (+ left-fringe left-margin (- (1))))
18408 '(space :width (+ left-fringe left-margin (-1)))
18412 #define NUMVAL(X) \
18413 ((INTEGERP (X) || FLOATP (X)) \
18414 ? XFLOATINT (X) \
18415 : - 1)
18418 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18419 double *res;
18420 struct it *it;
18421 Lisp_Object prop;
18422 void *font;
18423 int width_p, *align_to;
18425 double pixels;
18427 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18428 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18430 if (NILP (prop))
18431 return OK_PIXELS (0);
18433 if (SYMBOLP (prop))
18435 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18437 char *unit = SDATA (SYMBOL_NAME (prop));
18439 if (unit[0] == 'i' && unit[1] == 'n')
18440 pixels = 1.0;
18441 else if (unit[0] == 'm' && unit[1] == 'm')
18442 pixels = 25.4;
18443 else if (unit[0] == 'c' && unit[1] == 'm')
18444 pixels = 2.54;
18445 else
18446 pixels = 0;
18447 if (pixels > 0)
18449 double ppi;
18450 #ifdef HAVE_WINDOW_SYSTEM
18451 if (FRAME_WINDOW_P (it->f)
18452 && (ppi = (width_p
18453 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18454 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18455 ppi > 0))
18456 return OK_PIXELS (ppi / pixels);
18457 #endif
18459 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18460 || (CONSP (Vdisplay_pixels_per_inch)
18461 && (ppi = (width_p
18462 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18463 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18464 ppi > 0)))
18465 return OK_PIXELS (ppi / pixels);
18467 return 0;
18471 #ifdef HAVE_WINDOW_SYSTEM
18472 if (EQ (prop, Qheight))
18473 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18474 if (EQ (prop, Qwidth))
18475 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18476 #else
18477 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18478 return OK_PIXELS (1);
18479 #endif
18481 if (EQ (prop, Qtext))
18482 return OK_PIXELS (width_p
18483 ? window_box_width (it->w, TEXT_AREA)
18484 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18486 if (align_to && *align_to < 0)
18488 *res = 0;
18489 if (EQ (prop, Qleft))
18490 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18491 if (EQ (prop, Qright))
18492 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18493 if (EQ (prop, Qcenter))
18494 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18495 + window_box_width (it->w, TEXT_AREA) / 2);
18496 if (EQ (prop, Qleft_fringe))
18497 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18498 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18499 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18500 if (EQ (prop, Qright_fringe))
18501 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18502 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18503 : window_box_right_offset (it->w, TEXT_AREA));
18504 if (EQ (prop, Qleft_margin))
18505 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18506 if (EQ (prop, Qright_margin))
18507 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18508 if (EQ (prop, Qscroll_bar))
18509 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18511 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18512 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18513 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18514 : 0)));
18516 else
18518 if (EQ (prop, Qleft_fringe))
18519 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18520 if (EQ (prop, Qright_fringe))
18521 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18522 if (EQ (prop, Qleft_margin))
18523 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18524 if (EQ (prop, Qright_margin))
18525 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18526 if (EQ (prop, Qscroll_bar))
18527 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18530 prop = Fbuffer_local_value (prop, it->w->buffer);
18533 if (INTEGERP (prop) || FLOATP (prop))
18535 int base_unit = (width_p
18536 ? FRAME_COLUMN_WIDTH (it->f)
18537 : FRAME_LINE_HEIGHT (it->f));
18538 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18541 if (CONSP (prop))
18543 Lisp_Object car = XCAR (prop);
18544 Lisp_Object cdr = XCDR (prop);
18546 if (SYMBOLP (car))
18548 #ifdef HAVE_WINDOW_SYSTEM
18549 if (valid_image_p (prop))
18551 int id = lookup_image (it->f, prop);
18552 struct image *img = IMAGE_FROM_ID (it->f, id);
18554 return OK_PIXELS (width_p ? img->width : img->height);
18556 #endif
18557 if (EQ (car, Qplus) || EQ (car, Qminus))
18559 int first = 1;
18560 double px;
18562 pixels = 0;
18563 while (CONSP (cdr))
18565 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18566 font, width_p, align_to))
18567 return 0;
18568 if (first)
18569 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18570 else
18571 pixels += px;
18572 cdr = XCDR (cdr);
18574 if (EQ (car, Qminus))
18575 pixels = -pixels;
18576 return OK_PIXELS (pixels);
18579 car = Fbuffer_local_value (car, it->w->buffer);
18582 if (INTEGERP (car) || FLOATP (car))
18584 double fact;
18585 pixels = XFLOATINT (car);
18586 if (NILP (cdr))
18587 return OK_PIXELS (pixels);
18588 if (calc_pixel_width_or_height (&fact, it, cdr,
18589 font, width_p, align_to))
18590 return OK_PIXELS (pixels * fact);
18591 return 0;
18594 return 0;
18597 return 0;
18601 /***********************************************************************
18602 Glyph Display
18603 ***********************************************************************/
18605 #ifdef HAVE_WINDOW_SYSTEM
18607 #if GLYPH_DEBUG
18609 void
18610 dump_glyph_string (s)
18611 struct glyph_string *s;
18613 fprintf (stderr, "glyph string\n");
18614 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18615 s->x, s->y, s->width, s->height);
18616 fprintf (stderr, " ybase = %d\n", s->ybase);
18617 fprintf (stderr, " hl = %d\n", s->hl);
18618 fprintf (stderr, " left overhang = %d, right = %d\n",
18619 s->left_overhang, s->right_overhang);
18620 fprintf (stderr, " nchars = %d\n", s->nchars);
18621 fprintf (stderr, " extends to end of line = %d\n",
18622 s->extends_to_end_of_line_p);
18623 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18624 fprintf (stderr, " bg width = %d\n", s->background_width);
18627 #endif /* GLYPH_DEBUG */
18629 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18630 of XChar2b structures for S; it can't be allocated in
18631 init_glyph_string because it must be allocated via `alloca'. W
18632 is the window on which S is drawn. ROW and AREA are the glyph row
18633 and area within the row from which S is constructed. START is the
18634 index of the first glyph structure covered by S. HL is a
18635 face-override for drawing S. */
18637 #ifdef HAVE_NTGUI
18638 #define OPTIONAL_HDC(hdc) hdc,
18639 #define DECLARE_HDC(hdc) HDC hdc;
18640 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18641 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18642 #endif
18644 #ifndef OPTIONAL_HDC
18645 #define OPTIONAL_HDC(hdc)
18646 #define DECLARE_HDC(hdc)
18647 #define ALLOCATE_HDC(hdc, f)
18648 #define RELEASE_HDC(hdc, f)
18649 #endif
18651 static void
18652 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18653 struct glyph_string *s;
18654 DECLARE_HDC (hdc)
18655 XChar2b *char2b;
18656 struct window *w;
18657 struct glyph_row *row;
18658 enum glyph_row_area area;
18659 int start;
18660 enum draw_glyphs_face hl;
18662 bzero (s, sizeof *s);
18663 s->w = w;
18664 s->f = XFRAME (w->frame);
18665 #ifdef HAVE_NTGUI
18666 s->hdc = hdc;
18667 #endif
18668 s->display = FRAME_X_DISPLAY (s->f);
18669 s->window = FRAME_X_WINDOW (s->f);
18670 s->char2b = char2b;
18671 s->hl = hl;
18672 s->row = row;
18673 s->area = area;
18674 s->first_glyph = row->glyphs[area] + start;
18675 s->height = row->height;
18676 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18678 /* Display the internal border below the tool-bar window. */
18679 if (WINDOWP (s->f->tool_bar_window)
18680 && s->w == XWINDOW (s->f->tool_bar_window))
18681 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18683 s->ybase = s->y + row->ascent;
18687 /* Append the list of glyph strings with head H and tail T to the list
18688 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18690 static INLINE void
18691 append_glyph_string_lists (head, tail, h, t)
18692 struct glyph_string **head, **tail;
18693 struct glyph_string *h, *t;
18695 if (h)
18697 if (*head)
18698 (*tail)->next = h;
18699 else
18700 *head = h;
18701 h->prev = *tail;
18702 *tail = t;
18707 /* Prepend the list of glyph strings with head H and tail T to the
18708 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18709 result. */
18711 static INLINE void
18712 prepend_glyph_string_lists (head, tail, h, t)
18713 struct glyph_string **head, **tail;
18714 struct glyph_string *h, *t;
18716 if (h)
18718 if (*head)
18719 (*head)->prev = t;
18720 else
18721 *tail = t;
18722 t->next = *head;
18723 *head = h;
18728 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18729 Set *HEAD and *TAIL to the resulting list. */
18731 static INLINE void
18732 append_glyph_string (head, tail, s)
18733 struct glyph_string **head, **tail;
18734 struct glyph_string *s;
18736 s->next = s->prev = NULL;
18737 append_glyph_string_lists (head, tail, s, s);
18741 /* Get face and two-byte form of character glyph GLYPH on frame F.
18742 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18743 a pointer to a realized face that is ready for display. */
18745 static INLINE struct face *
18746 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18747 struct frame *f;
18748 struct glyph *glyph;
18749 XChar2b *char2b;
18750 int *two_byte_p;
18752 struct face *face;
18754 xassert (glyph->type == CHAR_GLYPH);
18755 face = FACE_FROM_ID (f, glyph->face_id);
18757 if (two_byte_p)
18758 *two_byte_p = 0;
18760 if (!glyph->multibyte_p)
18762 /* Unibyte case. We don't have to encode, but we have to make
18763 sure to use a face suitable for unibyte. */
18764 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18766 else if (glyph->u.ch < 128)
18768 /* Case of ASCII in a face known to fit ASCII. */
18769 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18771 else
18773 int c1, c2, charset;
18775 /* Split characters into bytes. If c2 is -1 afterwards, C is
18776 really a one-byte character so that byte1 is zero. */
18777 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18778 if (c2 > 0)
18779 STORE_XCHAR2B (char2b, c1, c2);
18780 else
18781 STORE_XCHAR2B (char2b, 0, c1);
18783 /* Maybe encode the character in *CHAR2B. */
18784 if (charset != CHARSET_ASCII)
18786 struct font_info *font_info
18787 = FONT_INFO_FROM_ID (f, face->font_info_id);
18788 if (font_info)
18789 glyph->font_type
18790 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18794 /* Make sure X resources of the face are allocated. */
18795 xassert (face != NULL);
18796 PREPARE_FACE_FOR_DISPLAY (f, face);
18797 return face;
18801 /* Fill glyph string S with composition components specified by S->cmp.
18803 FACES is an array of faces for all components of this composition.
18804 S->gidx is the index of the first component for S.
18806 OVERLAPS non-zero means S should draw the foreground only, and use
18807 its physical height for clipping. See also draw_glyphs.
18809 Value is the index of a component not in S. */
18811 static int
18812 fill_composite_glyph_string (s, faces, overlaps)
18813 struct glyph_string *s;
18814 struct face **faces;
18815 int overlaps;
18817 int i;
18819 xassert (s);
18821 s->for_overlaps = overlaps;
18823 s->face = faces[s->gidx];
18824 s->font = s->face->font;
18825 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18827 /* For all glyphs of this composition, starting at the offset
18828 S->gidx, until we reach the end of the definition or encounter a
18829 glyph that requires the different face, add it to S. */
18830 ++s->nchars;
18831 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18832 ++s->nchars;
18834 /* All glyph strings for the same composition has the same width,
18835 i.e. the width set for the first component of the composition. */
18837 s->width = s->first_glyph->pixel_width;
18839 /* If the specified font could not be loaded, use the frame's
18840 default font, but record the fact that we couldn't load it in
18841 the glyph string so that we can draw rectangles for the
18842 characters of the glyph string. */
18843 if (s->font == NULL)
18845 s->font_not_found_p = 1;
18846 s->font = FRAME_FONT (s->f);
18849 /* Adjust base line for subscript/superscript text. */
18850 s->ybase += s->first_glyph->voffset;
18852 xassert (s->face && s->face->gc);
18854 /* This glyph string must always be drawn with 16-bit functions. */
18855 s->two_byte_p = 1;
18857 return s->gidx + s->nchars;
18861 /* Fill glyph string S from a sequence of character glyphs.
18863 FACE_ID is the face id of the string. START is the index of the
18864 first glyph to consider, END is the index of the last + 1.
18865 OVERLAPS non-zero means S should draw the foreground only, and use
18866 its physical height for clipping. See also draw_glyphs.
18868 Value is the index of the first glyph not in S. */
18870 static int
18871 fill_glyph_string (s, face_id, start, end, overlaps)
18872 struct glyph_string *s;
18873 int face_id;
18874 int start, end, overlaps;
18876 struct glyph *glyph, *last;
18877 int voffset;
18878 int glyph_not_available_p;
18880 xassert (s->f == XFRAME (s->w->frame));
18881 xassert (s->nchars == 0);
18882 xassert (start >= 0 && end > start);
18884 s->for_overlaps = overlaps,
18885 glyph = s->row->glyphs[s->area] + start;
18886 last = s->row->glyphs[s->area] + end;
18887 voffset = glyph->voffset;
18889 glyph_not_available_p = glyph->glyph_not_available_p;
18891 while (glyph < last
18892 && glyph->type == CHAR_GLYPH
18893 && glyph->voffset == voffset
18894 /* Same face id implies same font, nowadays. */
18895 && glyph->face_id == face_id
18896 && glyph->glyph_not_available_p == glyph_not_available_p)
18898 int two_byte_p;
18900 s->face = get_glyph_face_and_encoding (s->f, glyph,
18901 s->char2b + s->nchars,
18902 &two_byte_p);
18903 s->two_byte_p = two_byte_p;
18904 ++s->nchars;
18905 xassert (s->nchars <= end - start);
18906 s->width += glyph->pixel_width;
18907 ++glyph;
18910 s->font = s->face->font;
18911 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18913 /* If the specified font could not be loaded, use the frame's font,
18914 but record the fact that we couldn't load it in
18915 S->font_not_found_p so that we can draw rectangles for the
18916 characters of the glyph string. */
18917 if (s->font == NULL || glyph_not_available_p)
18919 s->font_not_found_p = 1;
18920 s->font = FRAME_FONT (s->f);
18923 /* Adjust base line for subscript/superscript text. */
18924 s->ybase += voffset;
18926 xassert (s->face && s->face->gc);
18927 return glyph - s->row->glyphs[s->area];
18931 /* Fill glyph string S from image glyph S->first_glyph. */
18933 static void
18934 fill_image_glyph_string (s)
18935 struct glyph_string *s;
18937 xassert (s->first_glyph->type == IMAGE_GLYPH);
18938 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18939 xassert (s->img);
18940 s->slice = s->first_glyph->slice;
18941 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18942 s->font = s->face->font;
18943 s->width = s->first_glyph->pixel_width;
18945 /* Adjust base line for subscript/superscript text. */
18946 s->ybase += s->first_glyph->voffset;
18950 /* Fill glyph string S from a sequence of stretch glyphs.
18952 ROW is the glyph row in which the glyphs are found, AREA is the
18953 area within the row. START is the index of the first glyph to
18954 consider, END is the index of the last + 1.
18956 Value is the index of the first glyph not in S. */
18958 static int
18959 fill_stretch_glyph_string (s, row, area, start, end)
18960 struct glyph_string *s;
18961 struct glyph_row *row;
18962 enum glyph_row_area area;
18963 int start, end;
18965 struct glyph *glyph, *last;
18966 int voffset, face_id;
18968 xassert (s->first_glyph->type == STRETCH_GLYPH);
18970 glyph = s->row->glyphs[s->area] + start;
18971 last = s->row->glyphs[s->area] + end;
18972 face_id = glyph->face_id;
18973 s->face = FACE_FROM_ID (s->f, face_id);
18974 s->font = s->face->font;
18975 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18976 s->width = glyph->pixel_width;
18977 s->nchars = 1;
18978 voffset = glyph->voffset;
18980 for (++glyph;
18981 (glyph < last
18982 && glyph->type == STRETCH_GLYPH
18983 && glyph->voffset == voffset
18984 && glyph->face_id == face_id);
18985 ++glyph)
18986 s->width += glyph->pixel_width;
18988 /* Adjust base line for subscript/superscript text. */
18989 s->ybase += voffset;
18991 /* The case that face->gc == 0 is handled when drawing the glyph
18992 string by calling PREPARE_FACE_FOR_DISPLAY. */
18993 xassert (s->face);
18994 return glyph - s->row->glyphs[s->area];
18998 /* EXPORT for RIF:
18999 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19000 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19001 assumed to be zero. */
19003 void
19004 x_get_glyph_overhangs (glyph, f, left, right)
19005 struct glyph *glyph;
19006 struct frame *f;
19007 int *left, *right;
19009 *left = *right = 0;
19011 if (glyph->type == CHAR_GLYPH)
19013 XFontStruct *font;
19014 struct face *face;
19015 struct font_info *font_info;
19016 XChar2b char2b;
19017 XCharStruct *pcm;
19019 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19020 font = face->font;
19021 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19022 if (font /* ++KFS: Should this be font_info ? */
19023 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19025 if (pcm->rbearing > pcm->width)
19026 *right = pcm->rbearing - pcm->width;
19027 if (pcm->lbearing < 0)
19028 *left = -pcm->lbearing;
19034 /* Return the index of the first glyph preceding glyph string S that
19035 is overwritten by S because of S's left overhang. Value is -1
19036 if no glyphs are overwritten. */
19038 static int
19039 left_overwritten (s)
19040 struct glyph_string *s;
19042 int k;
19044 if (s->left_overhang)
19046 int x = 0, i;
19047 struct glyph *glyphs = s->row->glyphs[s->area];
19048 int first = s->first_glyph - glyphs;
19050 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19051 x -= glyphs[i].pixel_width;
19053 k = i + 1;
19055 else
19056 k = -1;
19058 return k;
19062 /* Return the index of the first glyph preceding glyph string S that
19063 is overwriting S because of its right overhang. Value is -1 if no
19064 glyph in front of S overwrites S. */
19066 static int
19067 left_overwriting (s)
19068 struct glyph_string *s;
19070 int i, k, x;
19071 struct glyph *glyphs = s->row->glyphs[s->area];
19072 int first = s->first_glyph - glyphs;
19074 k = -1;
19075 x = 0;
19076 for (i = first - 1; i >= 0; --i)
19078 int left, right;
19079 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19080 if (x + right > 0)
19081 k = i;
19082 x -= glyphs[i].pixel_width;
19085 return k;
19089 /* Return the index of the last glyph following glyph string S that is
19090 not overwritten by S because of S's right overhang. Value is -1 if
19091 no such glyph is found. */
19093 static int
19094 right_overwritten (s)
19095 struct glyph_string *s;
19097 int k = -1;
19099 if (s->right_overhang)
19101 int x = 0, i;
19102 struct glyph *glyphs = s->row->glyphs[s->area];
19103 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19104 int end = s->row->used[s->area];
19106 for (i = first; i < end && s->right_overhang > x; ++i)
19107 x += glyphs[i].pixel_width;
19109 k = i;
19112 return k;
19116 /* Return the index of the last glyph following glyph string S that
19117 overwrites S because of its left overhang. Value is negative
19118 if no such glyph is found. */
19120 static int
19121 right_overwriting (s)
19122 struct glyph_string *s;
19124 int i, k, x;
19125 int end = s->row->used[s->area];
19126 struct glyph *glyphs = s->row->glyphs[s->area];
19127 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19129 k = -1;
19130 x = 0;
19131 for (i = first; i < end; ++i)
19133 int left, right;
19134 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19135 if (x - left < 0)
19136 k = i;
19137 x += glyphs[i].pixel_width;
19140 return k;
19144 /* Get face and two-byte form of character C in face FACE_ID on frame
19145 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19146 means we want to display multibyte text. DISPLAY_P non-zero means
19147 make sure that X resources for the face returned are allocated.
19148 Value is a pointer to a realized face that is ready for display if
19149 DISPLAY_P is non-zero. */
19151 static INLINE struct face *
19152 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19153 struct frame *f;
19154 int c, face_id;
19155 XChar2b *char2b;
19156 int multibyte_p, display_p;
19158 struct face *face = FACE_FROM_ID (f, face_id);
19160 if (!multibyte_p)
19162 /* Unibyte case. We don't have to encode, but we have to make
19163 sure to use a face suitable for unibyte. */
19164 STORE_XCHAR2B (char2b, 0, c);
19165 face_id = FACE_FOR_CHAR (f, face, c);
19166 face = FACE_FROM_ID (f, face_id);
19168 else if (c < 128)
19170 /* Case of ASCII in a face known to fit ASCII. */
19171 STORE_XCHAR2B (char2b, 0, c);
19173 else
19175 int c1, c2, charset;
19177 /* Split characters into bytes. If c2 is -1 afterwards, C is
19178 really a one-byte character so that byte1 is zero. */
19179 SPLIT_CHAR (c, charset, c1, c2);
19180 if (c2 > 0)
19181 STORE_XCHAR2B (char2b, c1, c2);
19182 else
19183 STORE_XCHAR2B (char2b, 0, c1);
19185 /* Maybe encode the character in *CHAR2B. */
19186 if (face->font != NULL)
19188 struct font_info *font_info
19189 = FONT_INFO_FROM_ID (f, face->font_info_id);
19190 if (font_info)
19191 rif->encode_char (c, char2b, font_info, 0);
19195 /* Make sure X resources of the face are allocated. */
19196 #ifdef HAVE_X_WINDOWS
19197 if (display_p)
19198 #endif
19200 xassert (face != NULL);
19201 PREPARE_FACE_FOR_DISPLAY (f, face);
19204 return face;
19208 /* Set background width of glyph string S. START is the index of the
19209 first glyph following S. LAST_X is the right-most x-position + 1
19210 in the drawing area. */
19212 static INLINE void
19213 set_glyph_string_background_width (s, start, last_x)
19214 struct glyph_string *s;
19215 int start;
19216 int last_x;
19218 /* If the face of this glyph string has to be drawn to the end of
19219 the drawing area, set S->extends_to_end_of_line_p. */
19221 if (start == s->row->used[s->area]
19222 && s->area == TEXT_AREA
19223 && ((s->row->fill_line_p
19224 && (s->hl == DRAW_NORMAL_TEXT
19225 || s->hl == DRAW_IMAGE_RAISED
19226 || s->hl == DRAW_IMAGE_SUNKEN))
19227 || s->hl == DRAW_MOUSE_FACE))
19228 s->extends_to_end_of_line_p = 1;
19230 /* If S extends its face to the end of the line, set its
19231 background_width to the distance to the right edge of the drawing
19232 area. */
19233 if (s->extends_to_end_of_line_p)
19234 s->background_width = last_x - s->x + 1;
19235 else
19236 s->background_width = s->width;
19240 /* Compute overhangs and x-positions for glyph string S and its
19241 predecessors, or successors. X is the starting x-position for S.
19242 BACKWARD_P non-zero means process predecessors. */
19244 static void
19245 compute_overhangs_and_x (s, x, backward_p)
19246 struct glyph_string *s;
19247 int x;
19248 int backward_p;
19250 if (backward_p)
19252 while (s)
19254 if (rif->compute_glyph_string_overhangs)
19255 rif->compute_glyph_string_overhangs (s);
19256 x -= s->width;
19257 s->x = x;
19258 s = s->prev;
19261 else
19263 while (s)
19265 if (rif->compute_glyph_string_overhangs)
19266 rif->compute_glyph_string_overhangs (s);
19267 s->x = x;
19268 x += s->width;
19269 s = s->next;
19276 /* The following macros are only called from draw_glyphs below.
19277 They reference the following parameters of that function directly:
19278 `w', `row', `area', and `overlap_p'
19279 as well as the following local variables:
19280 `s', `f', and `hdc' (in W32) */
19282 #ifdef HAVE_NTGUI
19283 /* On W32, silently add local `hdc' variable to argument list of
19284 init_glyph_string. */
19285 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19286 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19287 #else
19288 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19289 init_glyph_string (s, char2b, w, row, area, start, hl)
19290 #endif
19292 /* Add a glyph string for a stretch glyph to the list of strings
19293 between HEAD and TAIL. START is the index of the stretch glyph in
19294 row area AREA of glyph row ROW. END is the index of the last glyph
19295 in that glyph row area. X is the current output position assigned
19296 to the new glyph string constructed. HL overrides that face of the
19297 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19298 is the right-most x-position of the drawing area. */
19300 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19301 and below -- keep them on one line. */
19302 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19303 do \
19305 s = (struct glyph_string *) alloca (sizeof *s); \
19306 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19307 START = fill_stretch_glyph_string (s, row, area, START, END); \
19308 append_glyph_string (&HEAD, &TAIL, s); \
19309 s->x = (X); \
19311 while (0)
19314 /* Add a glyph string for an image glyph to the list of strings
19315 between HEAD and TAIL. START is the index of the image glyph in
19316 row area AREA of glyph row ROW. END is the index of the last glyph
19317 in that glyph row area. X is the current output position assigned
19318 to the new glyph string constructed. HL overrides that face of the
19319 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19320 is the right-most x-position of the drawing area. */
19322 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19323 do \
19325 s = (struct glyph_string *) alloca (sizeof *s); \
19326 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19327 fill_image_glyph_string (s); \
19328 append_glyph_string (&HEAD, &TAIL, s); \
19329 ++START; \
19330 s->x = (X); \
19332 while (0)
19335 /* Add a glyph string for a sequence of character glyphs to the list
19336 of strings between HEAD and TAIL. START is the index of the first
19337 glyph in row area AREA of glyph row ROW that is part of the new
19338 glyph string. END is the index of the last glyph in that glyph row
19339 area. X is the current output position assigned to the new glyph
19340 string constructed. HL overrides that face of the glyph; e.g. it
19341 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19342 right-most x-position of the drawing area. */
19344 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19345 do \
19347 int c, face_id; \
19348 XChar2b *char2b; \
19350 c = (row)->glyphs[area][START].u.ch; \
19351 face_id = (row)->glyphs[area][START].face_id; \
19353 s = (struct glyph_string *) alloca (sizeof *s); \
19354 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19355 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19356 append_glyph_string (&HEAD, &TAIL, s); \
19357 s->x = (X); \
19358 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19360 while (0)
19363 /* Add a glyph string for a composite sequence to the list of strings
19364 between HEAD and TAIL. START is the index of the first glyph in
19365 row area AREA of glyph row ROW that is part of the new glyph
19366 string. END is the index of the last glyph in that glyph row area.
19367 X is the current output position assigned to the new glyph string
19368 constructed. HL overrides that face of the glyph; e.g. it is
19369 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19370 x-position of the drawing area. */
19372 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19373 do { \
19374 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19375 int face_id = (row)->glyphs[area][START].face_id; \
19376 struct face *base_face = FACE_FROM_ID (f, face_id); \
19377 struct composition *cmp = composition_table[cmp_id]; \
19378 int glyph_len = cmp->glyph_len; \
19379 XChar2b *char2b; \
19380 struct face **faces; \
19381 struct glyph_string *first_s = NULL; \
19382 int n; \
19384 base_face = base_face->ascii_face; \
19385 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19386 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19387 /* At first, fill in `char2b' and `faces'. */ \
19388 for (n = 0; n < glyph_len; n++) \
19390 int c = COMPOSITION_GLYPH (cmp, n); \
19391 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19392 faces[n] = FACE_FROM_ID (f, this_face_id); \
19393 get_char_face_and_encoding (f, c, this_face_id, \
19394 char2b + n, 1, 1); \
19397 /* Make glyph_strings for each glyph sequence that is drawable by \
19398 the same face, and append them to HEAD/TAIL. */ \
19399 for (n = 0; n < cmp->glyph_len;) \
19401 s = (struct glyph_string *) alloca (sizeof *s); \
19402 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19403 append_glyph_string (&(HEAD), &(TAIL), s); \
19404 s->cmp = cmp; \
19405 s->gidx = n; \
19406 s->x = (X); \
19408 if (n == 0) \
19409 first_s = s; \
19411 n = fill_composite_glyph_string (s, faces, overlaps); \
19414 ++START; \
19415 s = first_s; \
19416 } while (0)
19419 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19420 of AREA of glyph row ROW on window W between indices START and END.
19421 HL overrides the face for drawing glyph strings, e.g. it is
19422 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19423 x-positions of the drawing area.
19425 This is an ugly monster macro construct because we must use alloca
19426 to allocate glyph strings (because draw_glyphs can be called
19427 asynchronously). */
19429 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19430 do \
19432 HEAD = TAIL = NULL; \
19433 while (START < END) \
19435 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19436 switch (first_glyph->type) \
19438 case CHAR_GLYPH: \
19439 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19440 HL, X, LAST_X); \
19441 break; \
19443 case COMPOSITE_GLYPH: \
19444 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19445 HL, X, LAST_X); \
19446 break; \
19448 case STRETCH_GLYPH: \
19449 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19450 HL, X, LAST_X); \
19451 break; \
19453 case IMAGE_GLYPH: \
19454 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19455 HL, X, LAST_X); \
19456 break; \
19458 default: \
19459 abort (); \
19462 set_glyph_string_background_width (s, START, LAST_X); \
19463 (X) += s->width; \
19466 while (0)
19469 /* Draw glyphs between START and END in AREA of ROW on window W,
19470 starting at x-position X. X is relative to AREA in W. HL is a
19471 face-override with the following meaning:
19473 DRAW_NORMAL_TEXT draw normally
19474 DRAW_CURSOR draw in cursor face
19475 DRAW_MOUSE_FACE draw in mouse face.
19476 DRAW_INVERSE_VIDEO draw in mode line face
19477 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19478 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19480 If OVERLAPS is non-zero, draw only the foreground of characters and
19481 clip to the physical height of ROW. Non-zero value also defines
19482 the overlapping part to be drawn:
19484 OVERLAPS_PRED overlap with preceding rows
19485 OVERLAPS_SUCC overlap with succeeding rows
19486 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19487 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19489 Value is the x-position reached, relative to AREA of W. */
19491 static int
19492 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19493 struct window *w;
19494 int x;
19495 struct glyph_row *row;
19496 enum glyph_row_area area;
19497 int start, end;
19498 enum draw_glyphs_face hl;
19499 int overlaps;
19501 struct glyph_string *head, *tail;
19502 struct glyph_string *s;
19503 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19504 int last_x, area_width;
19505 int x_reached;
19506 int i, j;
19507 struct frame *f = XFRAME (WINDOW_FRAME (w));
19508 DECLARE_HDC (hdc);
19510 ALLOCATE_HDC (hdc, f);
19512 /* Let's rather be paranoid than getting a SEGV. */
19513 end = min (end, row->used[area]);
19514 start = max (0, start);
19515 start = min (end, start);
19517 /* Translate X to frame coordinates. Set last_x to the right
19518 end of the drawing area. */
19519 if (row->full_width_p)
19521 /* X is relative to the left edge of W, without scroll bars
19522 or fringes. */
19523 x += WINDOW_LEFT_EDGE_X (w);
19524 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19526 else
19528 int area_left = window_box_left (w, area);
19529 x += area_left;
19530 area_width = window_box_width (w, area);
19531 last_x = area_left + area_width;
19534 /* Build a doubly-linked list of glyph_string structures between
19535 head and tail from what we have to draw. Note that the macro
19536 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19537 the reason we use a separate variable `i'. */
19538 i = start;
19539 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19540 if (tail)
19541 x_reached = tail->x + tail->background_width;
19542 else
19543 x_reached = x;
19545 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19546 the row, redraw some glyphs in front or following the glyph
19547 strings built above. */
19548 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19550 int dummy_x = 0;
19551 struct glyph_string *h, *t;
19553 /* Compute overhangs for all glyph strings. */
19554 if (rif->compute_glyph_string_overhangs)
19555 for (s = head; s; s = s->next)
19556 rif->compute_glyph_string_overhangs (s);
19558 /* Prepend glyph strings for glyphs in front of the first glyph
19559 string that are overwritten because of the first glyph
19560 string's left overhang. The background of all strings
19561 prepended must be drawn because the first glyph string
19562 draws over it. */
19563 i = left_overwritten (head);
19564 if (i >= 0)
19566 j = i;
19567 BUILD_GLYPH_STRINGS (j, start, h, t,
19568 DRAW_NORMAL_TEXT, dummy_x, last_x);
19569 start = i;
19570 compute_overhangs_and_x (t, head->x, 1);
19571 prepend_glyph_string_lists (&head, &tail, h, t);
19572 clip_head = head;
19575 /* Prepend glyph strings for glyphs in front of the first glyph
19576 string that overwrite that glyph string because of their
19577 right overhang. For these strings, only the foreground must
19578 be drawn, because it draws over the glyph string at `head'.
19579 The background must not be drawn because this would overwrite
19580 right overhangs of preceding glyphs for which no glyph
19581 strings exist. */
19582 i = left_overwriting (head);
19583 if (i >= 0)
19585 clip_head = head;
19586 BUILD_GLYPH_STRINGS (i, start, h, t,
19587 DRAW_NORMAL_TEXT, dummy_x, last_x);
19588 for (s = h; s; s = s->next)
19589 s->background_filled_p = 1;
19590 compute_overhangs_and_x (t, head->x, 1);
19591 prepend_glyph_string_lists (&head, &tail, h, t);
19594 /* Append glyphs strings for glyphs following the last glyph
19595 string tail that are overwritten by tail. The background of
19596 these strings has to be drawn because tail's foreground draws
19597 over it. */
19598 i = right_overwritten (tail);
19599 if (i >= 0)
19601 BUILD_GLYPH_STRINGS (end, i, h, t,
19602 DRAW_NORMAL_TEXT, x, last_x);
19603 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19604 append_glyph_string_lists (&head, &tail, h, t);
19605 clip_tail = tail;
19608 /* Append glyph strings for glyphs following the last glyph
19609 string tail that overwrite tail. The foreground of such
19610 glyphs has to be drawn because it writes into the background
19611 of tail. The background must not be drawn because it could
19612 paint over the foreground of following glyphs. */
19613 i = right_overwriting (tail);
19614 if (i >= 0)
19616 clip_tail = tail;
19617 BUILD_GLYPH_STRINGS (end, i, h, t,
19618 DRAW_NORMAL_TEXT, x, last_x);
19619 for (s = h; s; s = s->next)
19620 s->background_filled_p = 1;
19621 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19622 append_glyph_string_lists (&head, &tail, h, t);
19624 if (clip_head || clip_tail)
19625 for (s = head; s; s = s->next)
19627 s->clip_head = clip_head;
19628 s->clip_tail = clip_tail;
19632 /* Draw all strings. */
19633 for (s = head; s; s = s->next)
19634 rif->draw_glyph_string (s);
19636 if (area == TEXT_AREA
19637 && !row->full_width_p
19638 /* When drawing overlapping rows, only the glyph strings'
19639 foreground is drawn, which doesn't erase a cursor
19640 completely. */
19641 && !overlaps)
19643 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19644 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19645 : (tail ? tail->x + tail->background_width : x));
19647 int text_left = window_box_left (w, TEXT_AREA);
19648 x0 -= text_left;
19649 x1 -= text_left;
19651 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19652 row->y, MATRIX_ROW_BOTTOM_Y (row));
19655 /* Value is the x-position up to which drawn, relative to AREA of W.
19656 This doesn't include parts drawn because of overhangs. */
19657 if (row->full_width_p)
19658 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19659 else
19660 x_reached -= window_box_left (w, area);
19662 RELEASE_HDC (hdc, f);
19664 return x_reached;
19667 /* Expand row matrix if too narrow. Don't expand if area
19668 is not present. */
19670 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19672 if (!fonts_changed_p \
19673 && (it->glyph_row->glyphs[area] \
19674 < it->glyph_row->glyphs[area + 1])) \
19676 it->w->ncols_scale_factor++; \
19677 fonts_changed_p = 1; \
19681 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19682 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19684 static INLINE void
19685 append_glyph (it)
19686 struct it *it;
19688 struct glyph *glyph;
19689 enum glyph_row_area area = it->area;
19691 xassert (it->glyph_row);
19692 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19694 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19695 if (glyph < it->glyph_row->glyphs[area + 1])
19697 glyph->charpos = CHARPOS (it->position);
19698 glyph->object = it->object;
19699 glyph->pixel_width = it->pixel_width;
19700 glyph->ascent = it->ascent;
19701 glyph->descent = it->descent;
19702 glyph->voffset = it->voffset;
19703 glyph->type = CHAR_GLYPH;
19704 glyph->multibyte_p = it->multibyte_p;
19705 glyph->left_box_line_p = it->start_of_box_run_p;
19706 glyph->right_box_line_p = it->end_of_box_run_p;
19707 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19708 || it->phys_descent > it->descent);
19709 glyph->padding_p = 0;
19710 glyph->glyph_not_available_p = it->glyph_not_available_p;
19711 glyph->face_id = it->face_id;
19712 glyph->u.ch = it->char_to_display;
19713 glyph->slice = null_glyph_slice;
19714 glyph->font_type = FONT_TYPE_UNKNOWN;
19715 ++it->glyph_row->used[area];
19717 else
19718 IT_EXPAND_MATRIX_WIDTH (it, area);
19721 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19722 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19724 static INLINE void
19725 append_composite_glyph (it)
19726 struct it *it;
19728 struct glyph *glyph;
19729 enum glyph_row_area area = it->area;
19731 xassert (it->glyph_row);
19733 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19734 if (glyph < it->glyph_row->glyphs[area + 1])
19736 glyph->charpos = CHARPOS (it->position);
19737 glyph->object = it->object;
19738 glyph->pixel_width = it->pixel_width;
19739 glyph->ascent = it->ascent;
19740 glyph->descent = it->descent;
19741 glyph->voffset = it->voffset;
19742 glyph->type = COMPOSITE_GLYPH;
19743 glyph->multibyte_p = it->multibyte_p;
19744 glyph->left_box_line_p = it->start_of_box_run_p;
19745 glyph->right_box_line_p = it->end_of_box_run_p;
19746 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19747 || it->phys_descent > it->descent);
19748 glyph->padding_p = 0;
19749 glyph->glyph_not_available_p = 0;
19750 glyph->face_id = it->face_id;
19751 glyph->u.cmp_id = it->cmp_id;
19752 glyph->slice = null_glyph_slice;
19753 glyph->font_type = FONT_TYPE_UNKNOWN;
19754 ++it->glyph_row->used[area];
19756 else
19757 IT_EXPAND_MATRIX_WIDTH (it, area);
19761 /* Change IT->ascent and IT->height according to the setting of
19762 IT->voffset. */
19764 static INLINE void
19765 take_vertical_position_into_account (it)
19766 struct it *it;
19768 if (it->voffset)
19770 if (it->voffset < 0)
19771 /* Increase the ascent so that we can display the text higher
19772 in the line. */
19773 it->ascent -= it->voffset;
19774 else
19775 /* Increase the descent so that we can display the text lower
19776 in the line. */
19777 it->descent += it->voffset;
19782 /* Produce glyphs/get display metrics for the image IT is loaded with.
19783 See the description of struct display_iterator in dispextern.h for
19784 an overview of struct display_iterator. */
19786 static void
19787 produce_image_glyph (it)
19788 struct it *it;
19790 struct image *img;
19791 struct face *face;
19792 int glyph_ascent, crop;
19793 struct glyph_slice slice;
19795 xassert (it->what == IT_IMAGE);
19797 face = FACE_FROM_ID (it->f, it->face_id);
19798 xassert (face);
19799 /* Make sure X resources of the face is loaded. */
19800 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19802 if (it->image_id < 0)
19804 /* Fringe bitmap. */
19805 it->ascent = it->phys_ascent = 0;
19806 it->descent = it->phys_descent = 0;
19807 it->pixel_width = 0;
19808 it->nglyphs = 0;
19809 return;
19812 img = IMAGE_FROM_ID (it->f, it->image_id);
19813 xassert (img);
19814 /* Make sure X resources of the image is loaded. */
19815 prepare_image_for_display (it->f, img);
19817 slice.x = slice.y = 0;
19818 slice.width = img->width;
19819 slice.height = img->height;
19821 if (INTEGERP (it->slice.x))
19822 slice.x = XINT (it->slice.x);
19823 else if (FLOATP (it->slice.x))
19824 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19826 if (INTEGERP (it->slice.y))
19827 slice.y = XINT (it->slice.y);
19828 else if (FLOATP (it->slice.y))
19829 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19831 if (INTEGERP (it->slice.width))
19832 slice.width = XINT (it->slice.width);
19833 else if (FLOATP (it->slice.width))
19834 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19836 if (INTEGERP (it->slice.height))
19837 slice.height = XINT (it->slice.height);
19838 else if (FLOATP (it->slice.height))
19839 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19841 if (slice.x >= img->width)
19842 slice.x = img->width;
19843 if (slice.y >= img->height)
19844 slice.y = img->height;
19845 if (slice.x + slice.width >= img->width)
19846 slice.width = img->width - slice.x;
19847 if (slice.y + slice.height > img->height)
19848 slice.height = img->height - slice.y;
19850 if (slice.width == 0 || slice.height == 0)
19851 return;
19853 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19855 it->descent = slice.height - glyph_ascent;
19856 if (slice.y == 0)
19857 it->descent += img->vmargin;
19858 if (slice.y + slice.height == img->height)
19859 it->descent += img->vmargin;
19860 it->phys_descent = it->descent;
19862 it->pixel_width = slice.width;
19863 if (slice.x == 0)
19864 it->pixel_width += img->hmargin;
19865 if (slice.x + slice.width == img->width)
19866 it->pixel_width += img->hmargin;
19868 /* It's quite possible for images to have an ascent greater than
19869 their height, so don't get confused in that case. */
19870 if (it->descent < 0)
19871 it->descent = 0;
19873 #if 0 /* this breaks image tiling */
19874 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19875 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19876 if (face_ascent > it->ascent)
19877 it->ascent = it->phys_ascent = face_ascent;
19878 #endif
19880 it->nglyphs = 1;
19882 if (face->box != FACE_NO_BOX)
19884 if (face->box_line_width > 0)
19886 if (slice.y == 0)
19887 it->ascent += face->box_line_width;
19888 if (slice.y + slice.height == img->height)
19889 it->descent += face->box_line_width;
19892 if (it->start_of_box_run_p && slice.x == 0)
19893 it->pixel_width += abs (face->box_line_width);
19894 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19895 it->pixel_width += abs (face->box_line_width);
19898 take_vertical_position_into_account (it);
19900 /* Automatically crop wide image glyphs at right edge so we can
19901 draw the cursor on same display row. */
19902 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
19903 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
19905 it->pixel_width -= crop;
19906 slice.width -= crop;
19909 if (it->glyph_row)
19911 struct glyph *glyph;
19912 enum glyph_row_area area = it->area;
19914 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19915 if (glyph < it->glyph_row->glyphs[area + 1])
19917 glyph->charpos = CHARPOS (it->position);
19918 glyph->object = it->object;
19919 glyph->pixel_width = it->pixel_width;
19920 glyph->ascent = glyph_ascent;
19921 glyph->descent = it->descent;
19922 glyph->voffset = it->voffset;
19923 glyph->type = IMAGE_GLYPH;
19924 glyph->multibyte_p = it->multibyte_p;
19925 glyph->left_box_line_p = it->start_of_box_run_p;
19926 glyph->right_box_line_p = it->end_of_box_run_p;
19927 glyph->overlaps_vertically_p = 0;
19928 glyph->padding_p = 0;
19929 glyph->glyph_not_available_p = 0;
19930 glyph->face_id = it->face_id;
19931 glyph->u.img_id = img->id;
19932 glyph->slice = slice;
19933 glyph->font_type = FONT_TYPE_UNKNOWN;
19934 ++it->glyph_row->used[area];
19936 else
19937 IT_EXPAND_MATRIX_WIDTH (it, area);
19942 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19943 of the glyph, WIDTH and HEIGHT are the width and height of the
19944 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19946 static void
19947 append_stretch_glyph (it, object, width, height, ascent)
19948 struct it *it;
19949 Lisp_Object object;
19950 int width, height;
19951 int ascent;
19953 struct glyph *glyph;
19954 enum glyph_row_area area = it->area;
19956 xassert (ascent >= 0 && ascent <= height);
19958 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19959 if (glyph < it->glyph_row->glyphs[area + 1])
19961 glyph->charpos = CHARPOS (it->position);
19962 glyph->object = object;
19963 glyph->pixel_width = width;
19964 glyph->ascent = ascent;
19965 glyph->descent = height - ascent;
19966 glyph->voffset = it->voffset;
19967 glyph->type = STRETCH_GLYPH;
19968 glyph->multibyte_p = it->multibyte_p;
19969 glyph->left_box_line_p = it->start_of_box_run_p;
19970 glyph->right_box_line_p = it->end_of_box_run_p;
19971 glyph->overlaps_vertically_p = 0;
19972 glyph->padding_p = 0;
19973 glyph->glyph_not_available_p = 0;
19974 glyph->face_id = it->face_id;
19975 glyph->u.stretch.ascent = ascent;
19976 glyph->u.stretch.height = height;
19977 glyph->slice = null_glyph_slice;
19978 glyph->font_type = FONT_TYPE_UNKNOWN;
19979 ++it->glyph_row->used[area];
19981 else
19982 IT_EXPAND_MATRIX_WIDTH (it, area);
19986 /* Produce a stretch glyph for iterator IT. IT->object is the value
19987 of the glyph property displayed. The value must be a list
19988 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19989 being recognized:
19991 1. `:width WIDTH' specifies that the space should be WIDTH *
19992 canonical char width wide. WIDTH may be an integer or floating
19993 point number.
19995 2. `:relative-width FACTOR' specifies that the width of the stretch
19996 should be computed from the width of the first character having the
19997 `glyph' property, and should be FACTOR times that width.
19999 3. `:align-to HPOS' specifies that the space should be wide enough
20000 to reach HPOS, a value in canonical character units.
20002 Exactly one of the above pairs must be present.
20004 4. `:height HEIGHT' specifies that the height of the stretch produced
20005 should be HEIGHT, measured in canonical character units.
20007 5. `:relative-height FACTOR' specifies that the height of the
20008 stretch should be FACTOR times the height of the characters having
20009 the glyph property.
20011 Either none or exactly one of 4 or 5 must be present.
20013 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20014 of the stretch should be used for the ascent of the stretch.
20015 ASCENT must be in the range 0 <= ASCENT <= 100. */
20017 static void
20018 produce_stretch_glyph (it)
20019 struct it *it;
20021 /* (space :width WIDTH :height HEIGHT ...) */
20022 Lisp_Object prop, plist;
20023 int width = 0, height = 0, align_to = -1;
20024 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20025 int ascent = 0;
20026 double tem;
20027 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20028 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20030 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20032 /* List should start with `space'. */
20033 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20034 plist = XCDR (it->object);
20036 /* Compute the width of the stretch. */
20037 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20038 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20040 /* Absolute width `:width WIDTH' specified and valid. */
20041 zero_width_ok_p = 1;
20042 width = (int)tem;
20044 else if (prop = Fplist_get (plist, QCrelative_width),
20045 NUMVAL (prop) > 0)
20047 /* Relative width `:relative-width FACTOR' specified and valid.
20048 Compute the width of the characters having the `glyph'
20049 property. */
20050 struct it it2;
20051 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20053 it2 = *it;
20054 if (it->multibyte_p)
20056 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20057 - IT_BYTEPOS (*it));
20058 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20060 else
20061 it2.c = *p, it2.len = 1;
20063 it2.glyph_row = NULL;
20064 it2.what = IT_CHARACTER;
20065 x_produce_glyphs (&it2);
20066 width = NUMVAL (prop) * it2.pixel_width;
20068 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20069 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20071 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20072 align_to = (align_to < 0
20074 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20075 else if (align_to < 0)
20076 align_to = window_box_left_offset (it->w, TEXT_AREA);
20077 width = max (0, (int)tem + align_to - it->current_x);
20078 zero_width_ok_p = 1;
20080 else
20081 /* Nothing specified -> width defaults to canonical char width. */
20082 width = FRAME_COLUMN_WIDTH (it->f);
20084 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20085 width = 1;
20087 /* Compute height. */
20088 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20089 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20091 height = (int)tem;
20092 zero_height_ok_p = 1;
20094 else if (prop = Fplist_get (plist, QCrelative_height),
20095 NUMVAL (prop) > 0)
20096 height = FONT_HEIGHT (font) * NUMVAL (prop);
20097 else
20098 height = FONT_HEIGHT (font);
20100 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20101 height = 1;
20103 /* Compute percentage of height used for ascent. If
20104 `:ascent ASCENT' is present and valid, use that. Otherwise,
20105 derive the ascent from the font in use. */
20106 if (prop = Fplist_get (plist, QCascent),
20107 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20108 ascent = height * NUMVAL (prop) / 100.0;
20109 else if (!NILP (prop)
20110 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20111 ascent = min (max (0, (int)tem), height);
20112 else
20113 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20115 if (width > 0 && !it->truncate_lines_p
20116 && it->current_x + width > it->last_visible_x)
20117 width = it->last_visible_x - it->current_x - 1;
20119 if (width > 0 && height > 0 && it->glyph_row)
20121 Lisp_Object object = it->stack[it->sp - 1].string;
20122 if (!STRINGP (object))
20123 object = it->w->buffer;
20124 append_stretch_glyph (it, object, width, height, ascent);
20127 it->pixel_width = width;
20128 it->ascent = it->phys_ascent = ascent;
20129 it->descent = it->phys_descent = height - it->ascent;
20130 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20132 take_vertical_position_into_account (it);
20135 /* Get line-height and line-spacing property at point.
20136 If line-height has format (HEIGHT TOTAL), return TOTAL
20137 in TOTAL_HEIGHT. */
20139 static Lisp_Object
20140 get_line_height_property (it, prop)
20141 struct it *it;
20142 Lisp_Object prop;
20144 Lisp_Object position;
20146 if (STRINGP (it->object))
20147 position = make_number (IT_STRING_CHARPOS (*it));
20148 else if (BUFFERP (it->object))
20149 position = make_number (IT_CHARPOS (*it));
20150 else
20151 return Qnil;
20153 return Fget_char_property (position, prop, it->object);
20156 /* Calculate line-height and line-spacing properties.
20157 An integer value specifies explicit pixel value.
20158 A float value specifies relative value to current face height.
20159 A cons (float . face-name) specifies relative value to
20160 height of specified face font.
20162 Returns height in pixels, or nil. */
20165 static Lisp_Object
20166 calc_line_height_property (it, val, font, boff, override)
20167 struct it *it;
20168 Lisp_Object val;
20169 XFontStruct *font;
20170 int boff, override;
20172 Lisp_Object face_name = Qnil;
20173 int ascent, descent, height;
20175 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20176 return val;
20178 if (CONSP (val))
20180 face_name = XCAR (val);
20181 val = XCDR (val);
20182 if (!NUMBERP (val))
20183 val = make_number (1);
20184 if (NILP (face_name))
20186 height = it->ascent + it->descent;
20187 goto scale;
20191 if (NILP (face_name))
20193 font = FRAME_FONT (it->f);
20194 boff = FRAME_BASELINE_OFFSET (it->f);
20196 else if (EQ (face_name, Qt))
20198 override = 0;
20200 else
20202 int face_id;
20203 struct face *face;
20204 struct font_info *font_info;
20206 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20207 if (face_id < 0)
20208 return make_number (-1);
20210 face = FACE_FROM_ID (it->f, face_id);
20211 font = face->font;
20212 if (font == NULL)
20213 return make_number (-1);
20215 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20216 boff = font_info->baseline_offset;
20217 if (font_info->vertical_centering)
20218 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20221 ascent = FONT_BASE (font) + boff;
20222 descent = FONT_DESCENT (font) - boff;
20224 if (override)
20226 it->override_ascent = ascent;
20227 it->override_descent = descent;
20228 it->override_boff = boff;
20231 height = ascent + descent;
20233 scale:
20234 if (FLOATP (val))
20235 height = (int)(XFLOAT_DATA (val) * height);
20236 else if (INTEGERP (val))
20237 height *= XINT (val);
20239 return make_number (height);
20243 /* RIF:
20244 Produce glyphs/get display metrics for the display element IT is
20245 loaded with. See the description of struct it in dispextern.h
20246 for an overview of struct it. */
20248 void
20249 x_produce_glyphs (it)
20250 struct it *it;
20252 int extra_line_spacing = it->extra_line_spacing;
20254 it->glyph_not_available_p = 0;
20256 if (it->what == IT_CHARACTER)
20258 XChar2b char2b;
20259 XFontStruct *font;
20260 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20261 XCharStruct *pcm;
20262 int font_not_found_p;
20263 struct font_info *font_info;
20264 int boff; /* baseline offset */
20265 /* We may change it->multibyte_p upon unibyte<->multibyte
20266 conversion. So, save the current value now and restore it
20267 later.
20269 Note: It seems that we don't have to record multibyte_p in
20270 struct glyph because the character code itself tells if or
20271 not the character is multibyte. Thus, in the future, we must
20272 consider eliminating the field `multibyte_p' in the struct
20273 glyph. */
20274 int saved_multibyte_p = it->multibyte_p;
20276 /* Maybe translate single-byte characters to multibyte, or the
20277 other way. */
20278 it->char_to_display = it->c;
20279 if (!ASCII_BYTE_P (it->c))
20281 if (unibyte_display_via_language_environment
20282 && SINGLE_BYTE_CHAR_P (it->c)
20283 && (it->c >= 0240
20284 || !NILP (Vnonascii_translation_table)))
20286 it->char_to_display = unibyte_char_to_multibyte (it->c);
20287 it->multibyte_p = 1;
20288 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20289 face = FACE_FROM_ID (it->f, it->face_id);
20291 else if (!SINGLE_BYTE_CHAR_P (it->c)
20292 && !it->multibyte_p)
20294 it->multibyte_p = 1;
20295 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20296 face = FACE_FROM_ID (it->f, it->face_id);
20300 /* Get font to use. Encode IT->char_to_display. */
20301 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20302 &char2b, it->multibyte_p, 0);
20303 font = face->font;
20305 /* When no suitable font found, use the default font. */
20306 font_not_found_p = font == NULL;
20307 if (font_not_found_p)
20309 font = FRAME_FONT (it->f);
20310 boff = FRAME_BASELINE_OFFSET (it->f);
20311 font_info = NULL;
20313 else
20315 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20316 boff = font_info->baseline_offset;
20317 if (font_info->vertical_centering)
20318 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20321 if (it->char_to_display >= ' '
20322 && (!it->multibyte_p || it->char_to_display < 128))
20324 /* Either unibyte or ASCII. */
20325 int stretched_p;
20327 it->nglyphs = 1;
20329 pcm = rif->per_char_metric (font, &char2b,
20330 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20332 if (it->override_ascent >= 0)
20334 it->ascent = it->override_ascent;
20335 it->descent = it->override_descent;
20336 boff = it->override_boff;
20338 else
20340 it->ascent = FONT_BASE (font) + boff;
20341 it->descent = FONT_DESCENT (font) - boff;
20344 if (pcm)
20346 it->phys_ascent = pcm->ascent + boff;
20347 it->phys_descent = pcm->descent - boff;
20348 it->pixel_width = pcm->width;
20350 else
20352 it->glyph_not_available_p = 1;
20353 it->phys_ascent = it->ascent;
20354 it->phys_descent = it->descent;
20355 it->pixel_width = FONT_WIDTH (font);
20358 if (it->constrain_row_ascent_descent_p)
20360 if (it->descent > it->max_descent)
20362 it->ascent += it->descent - it->max_descent;
20363 it->descent = it->max_descent;
20365 if (it->ascent > it->max_ascent)
20367 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20368 it->ascent = it->max_ascent;
20370 it->phys_ascent = min (it->phys_ascent, it->ascent);
20371 it->phys_descent = min (it->phys_descent, it->descent);
20372 extra_line_spacing = 0;
20375 /* If this is a space inside a region of text with
20376 `space-width' property, change its width. */
20377 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20378 if (stretched_p)
20379 it->pixel_width *= XFLOATINT (it->space_width);
20381 /* If face has a box, add the box thickness to the character
20382 height. If character has a box line to the left and/or
20383 right, add the box line width to the character's width. */
20384 if (face->box != FACE_NO_BOX)
20386 int thick = face->box_line_width;
20388 if (thick > 0)
20390 it->ascent += thick;
20391 it->descent += thick;
20393 else
20394 thick = -thick;
20396 if (it->start_of_box_run_p)
20397 it->pixel_width += thick;
20398 if (it->end_of_box_run_p)
20399 it->pixel_width += thick;
20402 /* If face has an overline, add the height of the overline
20403 (1 pixel) and a 1 pixel margin to the character height. */
20404 if (face->overline_p)
20405 it->ascent += overline_margin;
20407 if (it->constrain_row_ascent_descent_p)
20409 if (it->ascent > it->max_ascent)
20410 it->ascent = it->max_ascent;
20411 if (it->descent > it->max_descent)
20412 it->descent = it->max_descent;
20415 take_vertical_position_into_account (it);
20417 /* If we have to actually produce glyphs, do it. */
20418 if (it->glyph_row)
20420 if (stretched_p)
20422 /* Translate a space with a `space-width' property
20423 into a stretch glyph. */
20424 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20425 / FONT_HEIGHT (font));
20426 append_stretch_glyph (it, it->object, it->pixel_width,
20427 it->ascent + it->descent, ascent);
20429 else
20430 append_glyph (it);
20432 /* If characters with lbearing or rbearing are displayed
20433 in this line, record that fact in a flag of the
20434 glyph row. This is used to optimize X output code. */
20435 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20436 it->glyph_row->contains_overlapping_glyphs_p = 1;
20439 else if (it->char_to_display == '\n')
20441 /* A newline has no width but we need the height of the line.
20442 But if previous part of the line set a height, don't
20443 increase that height */
20445 Lisp_Object height;
20446 Lisp_Object total_height = Qnil;
20448 it->override_ascent = -1;
20449 it->pixel_width = 0;
20450 it->nglyphs = 0;
20452 height = get_line_height_property(it, Qline_height);
20453 /* Split (line-height total-height) list */
20454 if (CONSP (height)
20455 && CONSP (XCDR (height))
20456 && NILP (XCDR (XCDR (height))))
20458 total_height = XCAR (XCDR (height));
20459 height = XCAR (height);
20461 height = calc_line_height_property(it, height, font, boff, 1);
20463 if (it->override_ascent >= 0)
20465 it->ascent = it->override_ascent;
20466 it->descent = it->override_descent;
20467 boff = it->override_boff;
20469 else
20471 it->ascent = FONT_BASE (font) + boff;
20472 it->descent = FONT_DESCENT (font) - boff;
20475 if (EQ (height, Qt))
20477 if (it->descent > it->max_descent)
20479 it->ascent += it->descent - it->max_descent;
20480 it->descent = it->max_descent;
20482 if (it->ascent > it->max_ascent)
20484 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20485 it->ascent = it->max_ascent;
20487 it->phys_ascent = min (it->phys_ascent, it->ascent);
20488 it->phys_descent = min (it->phys_descent, it->descent);
20489 it->constrain_row_ascent_descent_p = 1;
20490 extra_line_spacing = 0;
20492 else
20494 Lisp_Object spacing;
20496 it->phys_ascent = it->ascent;
20497 it->phys_descent = it->descent;
20499 if ((it->max_ascent > 0 || it->max_descent > 0)
20500 && face->box != FACE_NO_BOX
20501 && face->box_line_width > 0)
20503 it->ascent += face->box_line_width;
20504 it->descent += face->box_line_width;
20506 if (!NILP (height)
20507 && XINT (height) > it->ascent + it->descent)
20508 it->ascent = XINT (height) - it->descent;
20510 if (!NILP (total_height))
20511 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20512 else
20514 spacing = get_line_height_property(it, Qline_spacing);
20515 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20517 if (INTEGERP (spacing))
20519 extra_line_spacing = XINT (spacing);
20520 if (!NILP (total_height))
20521 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20525 else if (it->char_to_display == '\t')
20527 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20528 int x = it->current_x + it->continuation_lines_width;
20529 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20531 /* If the distance from the current position to the next tab
20532 stop is less than a space character width, use the
20533 tab stop after that. */
20534 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20535 next_tab_x += tab_width;
20537 it->pixel_width = next_tab_x - x;
20538 it->nglyphs = 1;
20539 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20540 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20542 if (it->glyph_row)
20544 append_stretch_glyph (it, it->object, it->pixel_width,
20545 it->ascent + it->descent, it->ascent);
20548 else
20550 /* A multi-byte character. Assume that the display width of the
20551 character is the width of the character multiplied by the
20552 width of the font. */
20554 /* If we found a font, this font should give us the right
20555 metrics. If we didn't find a font, use the frame's
20556 default font and calculate the width of the character
20557 from the charset width; this is what old redisplay code
20558 did. */
20560 pcm = rif->per_char_metric (font, &char2b,
20561 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20563 if (font_not_found_p || !pcm)
20565 int charset = CHAR_CHARSET (it->char_to_display);
20567 it->glyph_not_available_p = 1;
20568 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20569 * CHARSET_WIDTH (charset));
20570 it->phys_ascent = FONT_BASE (font) + boff;
20571 it->phys_descent = FONT_DESCENT (font) - boff;
20573 else
20575 it->pixel_width = pcm->width;
20576 it->phys_ascent = pcm->ascent + boff;
20577 it->phys_descent = pcm->descent - boff;
20578 if (it->glyph_row
20579 && (pcm->lbearing < 0
20580 || pcm->rbearing > pcm->width))
20581 it->glyph_row->contains_overlapping_glyphs_p = 1;
20583 it->nglyphs = 1;
20584 it->ascent = FONT_BASE (font) + boff;
20585 it->descent = FONT_DESCENT (font) - boff;
20586 if (face->box != FACE_NO_BOX)
20588 int thick = face->box_line_width;
20590 if (thick > 0)
20592 it->ascent += thick;
20593 it->descent += thick;
20595 else
20596 thick = - thick;
20598 if (it->start_of_box_run_p)
20599 it->pixel_width += thick;
20600 if (it->end_of_box_run_p)
20601 it->pixel_width += thick;
20604 /* If face has an overline, add the height of the overline
20605 (1 pixel) and a 1 pixel margin to the character height. */
20606 if (face->overline_p)
20607 it->ascent += overline_margin;
20609 take_vertical_position_into_account (it);
20611 if (it->glyph_row)
20612 append_glyph (it);
20614 it->multibyte_p = saved_multibyte_p;
20616 else if (it->what == IT_COMPOSITION)
20618 /* Note: A composition is represented as one glyph in the
20619 glyph matrix. There are no padding glyphs. */
20620 XChar2b char2b;
20621 XFontStruct *font;
20622 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20623 XCharStruct *pcm;
20624 int font_not_found_p;
20625 struct font_info *font_info;
20626 int boff; /* baseline offset */
20627 struct composition *cmp = composition_table[it->cmp_id];
20629 /* Maybe translate single-byte characters to multibyte. */
20630 it->char_to_display = it->c;
20631 if (unibyte_display_via_language_environment
20632 && SINGLE_BYTE_CHAR_P (it->c)
20633 && (it->c >= 0240
20634 || (it->c >= 0200
20635 && !NILP (Vnonascii_translation_table))))
20637 it->char_to_display = unibyte_char_to_multibyte (it->c);
20640 /* Get face and font to use. Encode IT->char_to_display. */
20641 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20642 face = FACE_FROM_ID (it->f, it->face_id);
20643 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20644 &char2b, it->multibyte_p, 0);
20645 font = face->font;
20647 /* When no suitable font found, use the default font. */
20648 font_not_found_p = font == NULL;
20649 if (font_not_found_p)
20651 font = FRAME_FONT (it->f);
20652 boff = FRAME_BASELINE_OFFSET (it->f);
20653 font_info = NULL;
20655 else
20657 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20658 boff = font_info->baseline_offset;
20659 if (font_info->vertical_centering)
20660 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20663 /* There are no padding glyphs, so there is only one glyph to
20664 produce for the composition. Important is that pixel_width,
20665 ascent and descent are the values of what is drawn by
20666 draw_glyphs (i.e. the values of the overall glyphs composed). */
20667 it->nglyphs = 1;
20669 /* If we have not yet calculated pixel size data of glyphs of
20670 the composition for the current face font, calculate them
20671 now. Theoretically, we have to check all fonts for the
20672 glyphs, but that requires much time and memory space. So,
20673 here we check only the font of the first glyph. This leads
20674 to incorrect display very rarely, and C-l (recenter) can
20675 correct the display anyway. */
20676 if (cmp->font != (void *) font)
20678 /* Ascent and descent of the font of the first character of
20679 this composition (adjusted by baseline offset). Ascent
20680 and descent of overall glyphs should not be less than
20681 them respectively. */
20682 int font_ascent = FONT_BASE (font) + boff;
20683 int font_descent = FONT_DESCENT (font) - boff;
20684 /* Bounding box of the overall glyphs. */
20685 int leftmost, rightmost, lowest, highest;
20686 int i, width, ascent, descent;
20688 cmp->font = (void *) font;
20690 /* Initialize the bounding box. */
20691 if (font_info
20692 && (pcm = rif->per_char_metric (font, &char2b,
20693 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20695 width = pcm->width;
20696 ascent = pcm->ascent;
20697 descent = pcm->descent;
20699 else
20701 width = FONT_WIDTH (font);
20702 ascent = FONT_BASE (font);
20703 descent = FONT_DESCENT (font);
20706 rightmost = width;
20707 lowest = - descent + boff;
20708 highest = ascent + boff;
20709 leftmost = 0;
20711 if (font_info
20712 && font_info->default_ascent
20713 && CHAR_TABLE_P (Vuse_default_ascent)
20714 && !NILP (Faref (Vuse_default_ascent,
20715 make_number (it->char_to_display))))
20716 highest = font_info->default_ascent + boff;
20718 /* Draw the first glyph at the normal position. It may be
20719 shifted to right later if some other glyphs are drawn at
20720 the left. */
20721 cmp->offsets[0] = 0;
20722 cmp->offsets[1] = boff;
20724 /* Set cmp->offsets for the remaining glyphs. */
20725 for (i = 1; i < cmp->glyph_len; i++)
20727 int left, right, btm, top;
20728 int ch = COMPOSITION_GLYPH (cmp, i);
20729 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20731 face = FACE_FROM_ID (it->f, face_id);
20732 get_char_face_and_encoding (it->f, ch, face->id,
20733 &char2b, it->multibyte_p, 0);
20734 font = face->font;
20735 if (font == NULL)
20737 font = FRAME_FONT (it->f);
20738 boff = FRAME_BASELINE_OFFSET (it->f);
20739 font_info = NULL;
20741 else
20743 font_info
20744 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20745 boff = font_info->baseline_offset;
20746 if (font_info->vertical_centering)
20747 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20750 if (font_info
20751 && (pcm = rif->per_char_metric (font, &char2b,
20752 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20754 width = pcm->width;
20755 ascent = pcm->ascent;
20756 descent = pcm->descent;
20758 else
20760 width = FONT_WIDTH (font);
20761 ascent = 1;
20762 descent = 0;
20765 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20767 /* Relative composition with or without
20768 alternate chars. */
20769 left = (leftmost + rightmost - width) / 2;
20770 btm = - descent + boff;
20771 if (font_info && font_info->relative_compose
20772 && (! CHAR_TABLE_P (Vignore_relative_composition)
20773 || NILP (Faref (Vignore_relative_composition,
20774 make_number (ch)))))
20777 if (- descent >= font_info->relative_compose)
20778 /* One extra pixel between two glyphs. */
20779 btm = highest + 1;
20780 else if (ascent <= 0)
20781 /* One extra pixel between two glyphs. */
20782 btm = lowest - 1 - ascent - descent;
20785 else
20787 /* A composition rule is specified by an integer
20788 value that encodes global and new reference
20789 points (GREF and NREF). GREF and NREF are
20790 specified by numbers as below:
20792 0---1---2 -- ascent
20796 9--10--11 -- center
20798 ---3---4---5--- baseline
20800 6---7---8 -- descent
20802 int rule = COMPOSITION_RULE (cmp, i);
20803 int gref, nref, grefx, grefy, nrefx, nrefy;
20805 COMPOSITION_DECODE_RULE (rule, gref, nref);
20806 grefx = gref % 3, nrefx = nref % 3;
20807 grefy = gref / 3, nrefy = nref / 3;
20809 left = (leftmost
20810 + grefx * (rightmost - leftmost) / 2
20811 - nrefx * width / 2);
20812 btm = ((grefy == 0 ? highest
20813 : grefy == 1 ? 0
20814 : grefy == 2 ? lowest
20815 : (highest + lowest) / 2)
20816 - (nrefy == 0 ? ascent + descent
20817 : nrefy == 1 ? descent - boff
20818 : nrefy == 2 ? 0
20819 : (ascent + descent) / 2));
20822 cmp->offsets[i * 2] = left;
20823 cmp->offsets[i * 2 + 1] = btm + descent;
20825 /* Update the bounding box of the overall glyphs. */
20826 right = left + width;
20827 top = btm + descent + ascent;
20828 if (left < leftmost)
20829 leftmost = left;
20830 if (right > rightmost)
20831 rightmost = right;
20832 if (top > highest)
20833 highest = top;
20834 if (btm < lowest)
20835 lowest = btm;
20838 /* If there are glyphs whose x-offsets are negative,
20839 shift all glyphs to the right and make all x-offsets
20840 non-negative. */
20841 if (leftmost < 0)
20843 for (i = 0; i < cmp->glyph_len; i++)
20844 cmp->offsets[i * 2] -= leftmost;
20845 rightmost -= leftmost;
20848 cmp->pixel_width = rightmost;
20849 cmp->ascent = highest;
20850 cmp->descent = - lowest;
20851 if (cmp->ascent < font_ascent)
20852 cmp->ascent = font_ascent;
20853 if (cmp->descent < font_descent)
20854 cmp->descent = font_descent;
20857 it->pixel_width = cmp->pixel_width;
20858 it->ascent = it->phys_ascent = cmp->ascent;
20859 it->descent = it->phys_descent = cmp->descent;
20861 if (face->box != FACE_NO_BOX)
20863 int thick = face->box_line_width;
20865 if (thick > 0)
20867 it->ascent += thick;
20868 it->descent += thick;
20870 else
20871 thick = - thick;
20873 if (it->start_of_box_run_p)
20874 it->pixel_width += thick;
20875 if (it->end_of_box_run_p)
20876 it->pixel_width += thick;
20879 /* If face has an overline, add the height of the overline
20880 (1 pixel) and a 1 pixel margin to the character height. */
20881 if (face->overline_p)
20882 it->ascent += overline_margin;
20884 take_vertical_position_into_account (it);
20886 if (it->glyph_row)
20887 append_composite_glyph (it);
20889 else if (it->what == IT_IMAGE)
20890 produce_image_glyph (it);
20891 else if (it->what == IT_STRETCH)
20892 produce_stretch_glyph (it);
20894 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20895 because this isn't true for images with `:ascent 100'. */
20896 xassert (it->ascent >= 0 && it->descent >= 0);
20897 if (it->area == TEXT_AREA)
20898 it->current_x += it->pixel_width;
20900 if (extra_line_spacing > 0)
20902 it->descent += extra_line_spacing;
20903 if (extra_line_spacing > it->max_extra_line_spacing)
20904 it->max_extra_line_spacing = extra_line_spacing;
20907 it->max_ascent = max (it->max_ascent, it->ascent);
20908 it->max_descent = max (it->max_descent, it->descent);
20909 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20910 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20913 /* EXPORT for RIF:
20914 Output LEN glyphs starting at START at the nominal cursor position.
20915 Advance the nominal cursor over the text. The global variable
20916 updated_window contains the window being updated, updated_row is
20917 the glyph row being updated, and updated_area is the area of that
20918 row being updated. */
20920 void
20921 x_write_glyphs (start, len)
20922 struct glyph *start;
20923 int len;
20925 int x, hpos;
20927 xassert (updated_window && updated_row);
20928 BLOCK_INPUT;
20930 /* Write glyphs. */
20932 hpos = start - updated_row->glyphs[updated_area];
20933 x = draw_glyphs (updated_window, output_cursor.x,
20934 updated_row, updated_area,
20935 hpos, hpos + len,
20936 DRAW_NORMAL_TEXT, 0);
20938 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20939 if (updated_area == TEXT_AREA
20940 && updated_window->phys_cursor_on_p
20941 && updated_window->phys_cursor.vpos == output_cursor.vpos
20942 && updated_window->phys_cursor.hpos >= hpos
20943 && updated_window->phys_cursor.hpos < hpos + len)
20944 updated_window->phys_cursor_on_p = 0;
20946 UNBLOCK_INPUT;
20948 /* Advance the output cursor. */
20949 output_cursor.hpos += len;
20950 output_cursor.x = x;
20954 /* EXPORT for RIF:
20955 Insert LEN glyphs from START at the nominal cursor position. */
20957 void
20958 x_insert_glyphs (start, len)
20959 struct glyph *start;
20960 int len;
20962 struct frame *f;
20963 struct window *w;
20964 int line_height, shift_by_width, shifted_region_width;
20965 struct glyph_row *row;
20966 struct glyph *glyph;
20967 int frame_x, frame_y, hpos;
20969 xassert (updated_window && updated_row);
20970 BLOCK_INPUT;
20971 w = updated_window;
20972 f = XFRAME (WINDOW_FRAME (w));
20974 /* Get the height of the line we are in. */
20975 row = updated_row;
20976 line_height = row->height;
20978 /* Get the width of the glyphs to insert. */
20979 shift_by_width = 0;
20980 for (glyph = start; glyph < start + len; ++glyph)
20981 shift_by_width += glyph->pixel_width;
20983 /* Get the width of the region to shift right. */
20984 shifted_region_width = (window_box_width (w, updated_area)
20985 - output_cursor.x
20986 - shift_by_width);
20988 /* Shift right. */
20989 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20990 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20992 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20993 line_height, shift_by_width);
20995 /* Write the glyphs. */
20996 hpos = start - row->glyphs[updated_area];
20997 draw_glyphs (w, output_cursor.x, row, updated_area,
20998 hpos, hpos + len,
20999 DRAW_NORMAL_TEXT, 0);
21001 /* Advance the output cursor. */
21002 output_cursor.hpos += len;
21003 output_cursor.x += shift_by_width;
21004 UNBLOCK_INPUT;
21008 /* EXPORT for RIF:
21009 Erase the current text line from the nominal cursor position
21010 (inclusive) to pixel column TO_X (exclusive). The idea is that
21011 everything from TO_X onward is already erased.
21013 TO_X is a pixel position relative to updated_area of
21014 updated_window. TO_X == -1 means clear to the end of this area. */
21016 void
21017 x_clear_end_of_line (to_x)
21018 int to_x;
21020 struct frame *f;
21021 struct window *w = updated_window;
21022 int max_x, min_y, max_y;
21023 int from_x, from_y, to_y;
21025 xassert (updated_window && updated_row);
21026 f = XFRAME (w->frame);
21028 if (updated_row->full_width_p)
21029 max_x = WINDOW_TOTAL_WIDTH (w);
21030 else
21031 max_x = window_box_width (w, updated_area);
21032 max_y = window_text_bottom_y (w);
21034 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21035 of window. For TO_X > 0, truncate to end of drawing area. */
21036 if (to_x == 0)
21037 return;
21038 else if (to_x < 0)
21039 to_x = max_x;
21040 else
21041 to_x = min (to_x, max_x);
21043 to_y = min (max_y, output_cursor.y + updated_row->height);
21045 /* Notice if the cursor will be cleared by this operation. */
21046 if (!updated_row->full_width_p)
21047 notice_overwritten_cursor (w, updated_area,
21048 output_cursor.x, -1,
21049 updated_row->y,
21050 MATRIX_ROW_BOTTOM_Y (updated_row));
21052 from_x = output_cursor.x;
21054 /* Translate to frame coordinates. */
21055 if (updated_row->full_width_p)
21057 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21058 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21060 else
21062 int area_left = window_box_left (w, updated_area);
21063 from_x += area_left;
21064 to_x += area_left;
21067 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21068 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21069 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21071 /* Prevent inadvertently clearing to end of the X window. */
21072 if (to_x > from_x && to_y > from_y)
21074 BLOCK_INPUT;
21075 rif->clear_frame_area (f, from_x, from_y,
21076 to_x - from_x, to_y - from_y);
21077 UNBLOCK_INPUT;
21081 #endif /* HAVE_WINDOW_SYSTEM */
21085 /***********************************************************************
21086 Cursor types
21087 ***********************************************************************/
21089 /* Value is the internal representation of the specified cursor type
21090 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21091 of the bar cursor. */
21093 static enum text_cursor_kinds
21094 get_specified_cursor_type (arg, width)
21095 Lisp_Object arg;
21096 int *width;
21098 enum text_cursor_kinds type;
21100 if (NILP (arg))
21101 return NO_CURSOR;
21103 if (EQ (arg, Qbox))
21104 return FILLED_BOX_CURSOR;
21106 if (EQ (arg, Qhollow))
21107 return HOLLOW_BOX_CURSOR;
21109 if (EQ (arg, Qbar))
21111 *width = 2;
21112 return BAR_CURSOR;
21115 if (CONSP (arg)
21116 && EQ (XCAR (arg), Qbar)
21117 && INTEGERP (XCDR (arg))
21118 && XINT (XCDR (arg)) >= 0)
21120 *width = XINT (XCDR (arg));
21121 return BAR_CURSOR;
21124 if (EQ (arg, Qhbar))
21126 *width = 2;
21127 return HBAR_CURSOR;
21130 if (CONSP (arg)
21131 && EQ (XCAR (arg), Qhbar)
21132 && INTEGERP (XCDR (arg))
21133 && XINT (XCDR (arg)) >= 0)
21135 *width = XINT (XCDR (arg));
21136 return HBAR_CURSOR;
21139 /* Treat anything unknown as "hollow box cursor".
21140 It was bad to signal an error; people have trouble fixing
21141 .Xdefaults with Emacs, when it has something bad in it. */
21142 type = HOLLOW_BOX_CURSOR;
21144 return type;
21147 /* Set the default cursor types for specified frame. */
21148 void
21149 set_frame_cursor_types (f, arg)
21150 struct frame *f;
21151 Lisp_Object arg;
21153 int width;
21154 Lisp_Object tem;
21156 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21157 FRAME_CURSOR_WIDTH (f) = width;
21159 /* By default, set up the blink-off state depending on the on-state. */
21161 tem = Fassoc (arg, Vblink_cursor_alist);
21162 if (!NILP (tem))
21164 FRAME_BLINK_OFF_CURSOR (f)
21165 = get_specified_cursor_type (XCDR (tem), &width);
21166 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21168 else
21169 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21173 /* Return the cursor we want to be displayed in window W. Return
21174 width of bar/hbar cursor through WIDTH arg. Return with
21175 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21176 (i.e. if the `system caret' should track this cursor).
21178 In a mini-buffer window, we want the cursor only to appear if we
21179 are reading input from this window. For the selected window, we
21180 want the cursor type given by the frame parameter or buffer local
21181 setting of cursor-type. If explicitly marked off, draw no cursor.
21182 In all other cases, we want a hollow box cursor. */
21184 static enum text_cursor_kinds
21185 get_window_cursor_type (w, glyph, width, active_cursor)
21186 struct window *w;
21187 struct glyph *glyph;
21188 int *width;
21189 int *active_cursor;
21191 struct frame *f = XFRAME (w->frame);
21192 struct buffer *b = XBUFFER (w->buffer);
21193 int cursor_type = DEFAULT_CURSOR;
21194 Lisp_Object alt_cursor;
21195 int non_selected = 0;
21197 *active_cursor = 1;
21199 /* Echo area */
21200 if (cursor_in_echo_area
21201 && FRAME_HAS_MINIBUF_P (f)
21202 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21204 if (w == XWINDOW (echo_area_window))
21206 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21208 *width = FRAME_CURSOR_WIDTH (f);
21209 return FRAME_DESIRED_CURSOR (f);
21211 else
21212 return get_specified_cursor_type (b->cursor_type, width);
21215 *active_cursor = 0;
21216 non_selected = 1;
21219 /* Nonselected window or nonselected frame. */
21220 else if (w != XWINDOW (f->selected_window)
21221 #ifdef HAVE_WINDOW_SYSTEM
21222 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21223 #endif
21226 *active_cursor = 0;
21228 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21229 return NO_CURSOR;
21231 non_selected = 1;
21234 /* Never display a cursor in a window in which cursor-type is nil. */
21235 if (NILP (b->cursor_type))
21236 return NO_CURSOR;
21238 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21239 if (non_selected)
21241 alt_cursor = b->cursor_in_non_selected_windows;
21242 return get_specified_cursor_type (alt_cursor, width);
21245 /* Get the normal cursor type for this window. */
21246 if (EQ (b->cursor_type, Qt))
21248 cursor_type = FRAME_DESIRED_CURSOR (f);
21249 *width = FRAME_CURSOR_WIDTH (f);
21251 else
21252 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21254 /* Use normal cursor if not blinked off. */
21255 if (!w->cursor_off_p)
21257 #ifdef HAVE_WINDOW_SYSTEM
21258 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21260 if (cursor_type == FILLED_BOX_CURSOR)
21262 /* Using a block cursor on large images can be very annoying.
21263 So use a hollow cursor for "large" images.
21264 If image is not transparent (no mask), also use hollow cursor. */
21265 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21266 if (img != NULL && IMAGEP (img->spec))
21268 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21269 where N = size of default frame font size.
21270 This should cover most of the "tiny" icons people may use. */
21271 if (!img->mask
21272 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21273 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21274 cursor_type = HOLLOW_BOX_CURSOR;
21277 else if (cursor_type != NO_CURSOR)
21279 /* Display current only supports BOX and HOLLOW cursors for images.
21280 So for now, unconditionally use a HOLLOW cursor when cursor is
21281 not a solid box cursor. */
21282 cursor_type = HOLLOW_BOX_CURSOR;
21285 #endif
21286 return cursor_type;
21289 /* Cursor is blinked off, so determine how to "toggle" it. */
21291 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21292 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21293 return get_specified_cursor_type (XCDR (alt_cursor), width);
21295 /* Then see if frame has specified a specific blink off cursor type. */
21296 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21298 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21299 return FRAME_BLINK_OFF_CURSOR (f);
21302 #if 0
21303 /* Some people liked having a permanently visible blinking cursor,
21304 while others had very strong opinions against it. So it was
21305 decided to remove it. KFS 2003-09-03 */
21307 /* Finally perform built-in cursor blinking:
21308 filled box <-> hollow box
21309 wide [h]bar <-> narrow [h]bar
21310 narrow [h]bar <-> no cursor
21311 other type <-> no cursor */
21313 if (cursor_type == FILLED_BOX_CURSOR)
21314 return HOLLOW_BOX_CURSOR;
21316 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21318 *width = 1;
21319 return cursor_type;
21321 #endif
21323 return NO_CURSOR;
21327 #ifdef HAVE_WINDOW_SYSTEM
21329 /* Notice when the text cursor of window W has been completely
21330 overwritten by a drawing operation that outputs glyphs in AREA
21331 starting at X0 and ending at X1 in the line starting at Y0 and
21332 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21333 the rest of the line after X0 has been written. Y coordinates
21334 are window-relative. */
21336 static void
21337 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21338 struct window *w;
21339 enum glyph_row_area area;
21340 int x0, y0, x1, y1;
21342 int cx0, cx1, cy0, cy1;
21343 struct glyph_row *row;
21345 if (!w->phys_cursor_on_p)
21346 return;
21347 if (area != TEXT_AREA)
21348 return;
21350 if (w->phys_cursor.vpos < 0
21351 || w->phys_cursor.vpos >= w->current_matrix->nrows
21352 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21353 !(row->enabled_p && row->displays_text_p)))
21354 return;
21356 if (row->cursor_in_fringe_p)
21358 row->cursor_in_fringe_p = 0;
21359 draw_fringe_bitmap (w, row, 0);
21360 w->phys_cursor_on_p = 0;
21361 return;
21364 cx0 = w->phys_cursor.x;
21365 cx1 = cx0 + w->phys_cursor_width;
21366 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21367 return;
21369 /* The cursor image will be completely removed from the
21370 screen if the output area intersects the cursor area in
21371 y-direction. When we draw in [y0 y1[, and some part of
21372 the cursor is at y < y0, that part must have been drawn
21373 before. When scrolling, the cursor is erased before
21374 actually scrolling, so we don't come here. When not
21375 scrolling, the rows above the old cursor row must have
21376 changed, and in this case these rows must have written
21377 over the cursor image.
21379 Likewise if part of the cursor is below y1, with the
21380 exception of the cursor being in the first blank row at
21381 the buffer and window end because update_text_area
21382 doesn't draw that row. (Except when it does, but
21383 that's handled in update_text_area.) */
21385 cy0 = w->phys_cursor.y;
21386 cy1 = cy0 + w->phys_cursor_height;
21387 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21388 return;
21390 w->phys_cursor_on_p = 0;
21393 #endif /* HAVE_WINDOW_SYSTEM */
21396 /************************************************************************
21397 Mouse Face
21398 ************************************************************************/
21400 #ifdef HAVE_WINDOW_SYSTEM
21402 /* EXPORT for RIF:
21403 Fix the display of area AREA of overlapping row ROW in window W
21404 with respect to the overlapping part OVERLAPS. */
21406 void
21407 x_fix_overlapping_area (w, row, area, overlaps)
21408 struct window *w;
21409 struct glyph_row *row;
21410 enum glyph_row_area area;
21411 int overlaps;
21413 int i, x;
21415 BLOCK_INPUT;
21417 x = 0;
21418 for (i = 0; i < row->used[area];)
21420 if (row->glyphs[area][i].overlaps_vertically_p)
21422 int start = i, start_x = x;
21426 x += row->glyphs[area][i].pixel_width;
21427 ++i;
21429 while (i < row->used[area]
21430 && row->glyphs[area][i].overlaps_vertically_p);
21432 draw_glyphs (w, start_x, row, area,
21433 start, i,
21434 DRAW_NORMAL_TEXT, overlaps);
21436 else
21438 x += row->glyphs[area][i].pixel_width;
21439 ++i;
21443 UNBLOCK_INPUT;
21447 /* EXPORT:
21448 Draw the cursor glyph of window W in glyph row ROW. See the
21449 comment of draw_glyphs for the meaning of HL. */
21451 void
21452 draw_phys_cursor_glyph (w, row, hl)
21453 struct window *w;
21454 struct glyph_row *row;
21455 enum draw_glyphs_face hl;
21457 /* If cursor hpos is out of bounds, don't draw garbage. This can
21458 happen in mini-buffer windows when switching between echo area
21459 glyphs and mini-buffer. */
21460 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21462 int on_p = w->phys_cursor_on_p;
21463 int x1;
21464 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21465 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21466 hl, 0);
21467 w->phys_cursor_on_p = on_p;
21469 if (hl == DRAW_CURSOR)
21470 w->phys_cursor_width = x1 - w->phys_cursor.x;
21471 /* When we erase the cursor, and ROW is overlapped by other
21472 rows, make sure that these overlapping parts of other rows
21473 are redrawn. */
21474 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21476 w->phys_cursor_width = x1 - w->phys_cursor.x;
21478 if (row > w->current_matrix->rows
21479 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21480 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21481 OVERLAPS_ERASED_CURSOR);
21483 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21484 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21485 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21486 OVERLAPS_ERASED_CURSOR);
21492 /* EXPORT:
21493 Erase the image of a cursor of window W from the screen. */
21495 void
21496 erase_phys_cursor (w)
21497 struct window *w;
21499 struct frame *f = XFRAME (w->frame);
21500 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21501 int hpos = w->phys_cursor.hpos;
21502 int vpos = w->phys_cursor.vpos;
21503 int mouse_face_here_p = 0;
21504 struct glyph_matrix *active_glyphs = w->current_matrix;
21505 struct glyph_row *cursor_row;
21506 struct glyph *cursor_glyph;
21507 enum draw_glyphs_face hl;
21509 /* No cursor displayed or row invalidated => nothing to do on the
21510 screen. */
21511 if (w->phys_cursor_type == NO_CURSOR)
21512 goto mark_cursor_off;
21514 /* VPOS >= active_glyphs->nrows means that window has been resized.
21515 Don't bother to erase the cursor. */
21516 if (vpos >= active_glyphs->nrows)
21517 goto mark_cursor_off;
21519 /* If row containing cursor is marked invalid, there is nothing we
21520 can do. */
21521 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21522 if (!cursor_row->enabled_p)
21523 goto mark_cursor_off;
21525 /* If line spacing is > 0, old cursor may only be partially visible in
21526 window after split-window. So adjust visible height. */
21527 cursor_row->visible_height = min (cursor_row->visible_height,
21528 window_text_bottom_y (w) - cursor_row->y);
21530 /* If row is completely invisible, don't attempt to delete a cursor which
21531 isn't there. This can happen if cursor is at top of a window, and
21532 we switch to a buffer with a header line in that window. */
21533 if (cursor_row->visible_height <= 0)
21534 goto mark_cursor_off;
21536 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21537 if (cursor_row->cursor_in_fringe_p)
21539 cursor_row->cursor_in_fringe_p = 0;
21540 draw_fringe_bitmap (w, cursor_row, 0);
21541 goto mark_cursor_off;
21544 /* This can happen when the new row is shorter than the old one.
21545 In this case, either draw_glyphs or clear_end_of_line
21546 should have cleared the cursor. Note that we wouldn't be
21547 able to erase the cursor in this case because we don't have a
21548 cursor glyph at hand. */
21549 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21550 goto mark_cursor_off;
21552 /* If the cursor is in the mouse face area, redisplay that when
21553 we clear the cursor. */
21554 if (! NILP (dpyinfo->mouse_face_window)
21555 && w == XWINDOW (dpyinfo->mouse_face_window)
21556 && (vpos > dpyinfo->mouse_face_beg_row
21557 || (vpos == dpyinfo->mouse_face_beg_row
21558 && hpos >= dpyinfo->mouse_face_beg_col))
21559 && (vpos < dpyinfo->mouse_face_end_row
21560 || (vpos == dpyinfo->mouse_face_end_row
21561 && hpos < dpyinfo->mouse_face_end_col))
21562 /* Don't redraw the cursor's spot in mouse face if it is at the
21563 end of a line (on a newline). The cursor appears there, but
21564 mouse highlighting does not. */
21565 && cursor_row->used[TEXT_AREA] > hpos)
21566 mouse_face_here_p = 1;
21568 /* Maybe clear the display under the cursor. */
21569 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21571 int x, y, left_x;
21572 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21573 int width;
21575 cursor_glyph = get_phys_cursor_glyph (w);
21576 if (cursor_glyph == NULL)
21577 goto mark_cursor_off;
21579 width = cursor_glyph->pixel_width;
21580 left_x = window_box_left_offset (w, TEXT_AREA);
21581 x = w->phys_cursor.x;
21582 if (x < left_x)
21583 width -= left_x - x;
21584 width = min (width, window_box_width (w, TEXT_AREA) - x);
21585 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21586 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21588 if (width > 0)
21589 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21592 /* Erase the cursor by redrawing the character underneath it. */
21593 if (mouse_face_here_p)
21594 hl = DRAW_MOUSE_FACE;
21595 else
21596 hl = DRAW_NORMAL_TEXT;
21597 draw_phys_cursor_glyph (w, cursor_row, hl);
21599 mark_cursor_off:
21600 w->phys_cursor_on_p = 0;
21601 w->phys_cursor_type = NO_CURSOR;
21605 /* EXPORT:
21606 Display or clear cursor of window W. If ON is zero, clear the
21607 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21608 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21610 void
21611 display_and_set_cursor (w, on, hpos, vpos, x, y)
21612 struct window *w;
21613 int on, hpos, vpos, x, y;
21615 struct frame *f = XFRAME (w->frame);
21616 int new_cursor_type;
21617 int new_cursor_width;
21618 int active_cursor;
21619 struct glyph_row *glyph_row;
21620 struct glyph *glyph;
21622 /* This is pointless on invisible frames, and dangerous on garbaged
21623 windows and frames; in the latter case, the frame or window may
21624 be in the midst of changing its size, and x and y may be off the
21625 window. */
21626 if (! FRAME_VISIBLE_P (f)
21627 || FRAME_GARBAGED_P (f)
21628 || vpos >= w->current_matrix->nrows
21629 || hpos >= w->current_matrix->matrix_w)
21630 return;
21632 /* If cursor is off and we want it off, return quickly. */
21633 if (!on && !w->phys_cursor_on_p)
21634 return;
21636 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21637 /* If cursor row is not enabled, we don't really know where to
21638 display the cursor. */
21639 if (!glyph_row->enabled_p)
21641 w->phys_cursor_on_p = 0;
21642 return;
21645 glyph = NULL;
21646 if (!glyph_row->exact_window_width_line_p
21647 || hpos < glyph_row->used[TEXT_AREA])
21648 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21650 xassert (interrupt_input_blocked);
21652 /* Set new_cursor_type to the cursor we want to be displayed. */
21653 new_cursor_type = get_window_cursor_type (w, glyph,
21654 &new_cursor_width, &active_cursor);
21656 /* If cursor is currently being shown and we don't want it to be or
21657 it is in the wrong place, or the cursor type is not what we want,
21658 erase it. */
21659 if (w->phys_cursor_on_p
21660 && (!on
21661 || w->phys_cursor.x != x
21662 || w->phys_cursor.y != y
21663 || new_cursor_type != w->phys_cursor_type
21664 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21665 && new_cursor_width != w->phys_cursor_width)))
21666 erase_phys_cursor (w);
21668 /* Don't check phys_cursor_on_p here because that flag is only set
21669 to zero in some cases where we know that the cursor has been
21670 completely erased, to avoid the extra work of erasing the cursor
21671 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21672 still not be visible, or it has only been partly erased. */
21673 if (on)
21675 w->phys_cursor_ascent = glyph_row->ascent;
21676 w->phys_cursor_height = glyph_row->height;
21678 /* Set phys_cursor_.* before x_draw_.* is called because some
21679 of them may need the information. */
21680 w->phys_cursor.x = x;
21681 w->phys_cursor.y = glyph_row->y;
21682 w->phys_cursor.hpos = hpos;
21683 w->phys_cursor.vpos = vpos;
21686 rif->draw_window_cursor (w, glyph_row, x, y,
21687 new_cursor_type, new_cursor_width,
21688 on, active_cursor);
21692 /* Switch the display of W's cursor on or off, according to the value
21693 of ON. */
21695 static void
21696 update_window_cursor (w, on)
21697 struct window *w;
21698 int on;
21700 /* Don't update cursor in windows whose frame is in the process
21701 of being deleted. */
21702 if (w->current_matrix)
21704 BLOCK_INPUT;
21705 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21706 w->phys_cursor.x, w->phys_cursor.y);
21707 UNBLOCK_INPUT;
21712 /* Call update_window_cursor with parameter ON_P on all leaf windows
21713 in the window tree rooted at W. */
21715 static void
21716 update_cursor_in_window_tree (w, on_p)
21717 struct window *w;
21718 int on_p;
21720 while (w)
21722 if (!NILP (w->hchild))
21723 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21724 else if (!NILP (w->vchild))
21725 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21726 else
21727 update_window_cursor (w, on_p);
21729 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21734 /* EXPORT:
21735 Display the cursor on window W, or clear it, according to ON_P.
21736 Don't change the cursor's position. */
21738 void
21739 x_update_cursor (f, on_p)
21740 struct frame *f;
21741 int on_p;
21743 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21747 /* EXPORT:
21748 Clear the cursor of window W to background color, and mark the
21749 cursor as not shown. This is used when the text where the cursor
21750 is is about to be rewritten. */
21752 void
21753 x_clear_cursor (w)
21754 struct window *w;
21756 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21757 update_window_cursor (w, 0);
21761 /* EXPORT:
21762 Display the active region described by mouse_face_* according to DRAW. */
21764 void
21765 show_mouse_face (dpyinfo, draw)
21766 Display_Info *dpyinfo;
21767 enum draw_glyphs_face draw;
21769 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21770 struct frame *f = XFRAME (WINDOW_FRAME (w));
21772 if (/* If window is in the process of being destroyed, don't bother
21773 to do anything. */
21774 w->current_matrix != NULL
21775 /* Don't update mouse highlight if hidden */
21776 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21777 /* Recognize when we are called to operate on rows that don't exist
21778 anymore. This can happen when a window is split. */
21779 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21781 int phys_cursor_on_p = w->phys_cursor_on_p;
21782 struct glyph_row *row, *first, *last;
21784 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21785 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21787 for (row = first; row <= last && row->enabled_p; ++row)
21789 int start_hpos, end_hpos, start_x;
21791 /* For all but the first row, the highlight starts at column 0. */
21792 if (row == first)
21794 start_hpos = dpyinfo->mouse_face_beg_col;
21795 start_x = dpyinfo->mouse_face_beg_x;
21797 else
21799 start_hpos = 0;
21800 start_x = 0;
21803 if (row == last)
21804 end_hpos = dpyinfo->mouse_face_end_col;
21805 else
21807 end_hpos = row->used[TEXT_AREA];
21808 if (draw == DRAW_NORMAL_TEXT)
21809 row->fill_line_p = 1; /* Clear to end of line */
21812 if (end_hpos > start_hpos)
21814 draw_glyphs (w, start_x, row, TEXT_AREA,
21815 start_hpos, end_hpos,
21816 draw, 0);
21818 row->mouse_face_p
21819 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21823 /* When we've written over the cursor, arrange for it to
21824 be displayed again. */
21825 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21827 BLOCK_INPUT;
21828 display_and_set_cursor (w, 1,
21829 w->phys_cursor.hpos, w->phys_cursor.vpos,
21830 w->phys_cursor.x, w->phys_cursor.y);
21831 UNBLOCK_INPUT;
21835 /* Change the mouse cursor. */
21836 if (draw == DRAW_NORMAL_TEXT)
21837 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21838 else if (draw == DRAW_MOUSE_FACE)
21839 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21840 else
21841 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21844 /* EXPORT:
21845 Clear out the mouse-highlighted active region.
21846 Redraw it un-highlighted first. Value is non-zero if mouse
21847 face was actually drawn unhighlighted. */
21850 clear_mouse_face (dpyinfo)
21851 Display_Info *dpyinfo;
21853 int cleared = 0;
21855 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21857 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21858 cleared = 1;
21861 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21862 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21863 dpyinfo->mouse_face_window = Qnil;
21864 dpyinfo->mouse_face_overlay = Qnil;
21865 return cleared;
21869 /* EXPORT:
21870 Non-zero if physical cursor of window W is within mouse face. */
21873 cursor_in_mouse_face_p (w)
21874 struct window *w;
21876 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21877 int in_mouse_face = 0;
21879 if (WINDOWP (dpyinfo->mouse_face_window)
21880 && XWINDOW (dpyinfo->mouse_face_window) == w)
21882 int hpos = w->phys_cursor.hpos;
21883 int vpos = w->phys_cursor.vpos;
21885 if (vpos >= dpyinfo->mouse_face_beg_row
21886 && vpos <= dpyinfo->mouse_face_end_row
21887 && (vpos > dpyinfo->mouse_face_beg_row
21888 || hpos >= dpyinfo->mouse_face_beg_col)
21889 && (vpos < dpyinfo->mouse_face_end_row
21890 || hpos < dpyinfo->mouse_face_end_col
21891 || dpyinfo->mouse_face_past_end))
21892 in_mouse_face = 1;
21895 return in_mouse_face;
21901 /* Find the glyph matrix position of buffer position CHARPOS in window
21902 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21903 current glyphs must be up to date. If CHARPOS is above window
21904 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21905 of last line in W. In the row containing CHARPOS, stop before glyphs
21906 having STOP as object. */
21908 #if 1 /* This is a version of fast_find_position that's more correct
21909 in the presence of hscrolling, for example. I didn't install
21910 it right away because the problem fixed is minor, it failed
21911 in 20.x as well, and I think it's too risky to install
21912 so near the release of 21.1. 2001-09-25 gerd. */
21914 static int
21915 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21916 struct window *w;
21917 int charpos;
21918 int *hpos, *vpos, *x, *y;
21919 Lisp_Object stop;
21921 struct glyph_row *row, *first;
21922 struct glyph *glyph, *end;
21923 int past_end = 0;
21925 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21926 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21928 *x = first->x;
21929 *y = first->y;
21930 *hpos = 0;
21931 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21932 return 1;
21935 row = row_containing_pos (w, charpos, first, NULL, 0);
21936 if (row == NULL)
21938 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21939 past_end = 1;
21942 /* If whole rows or last part of a row came from a display overlay,
21943 row_containing_pos will skip over such rows because their end pos
21944 equals the start pos of the overlay or interval.
21946 Move back if we have a STOP object and previous row's
21947 end glyph came from STOP. */
21948 if (!NILP (stop))
21950 struct glyph_row *prev;
21951 while ((prev = row - 1, prev >= first)
21952 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21953 && prev->used[TEXT_AREA] > 0)
21955 struct glyph *beg = prev->glyphs[TEXT_AREA];
21956 glyph = beg + prev->used[TEXT_AREA];
21957 while (--glyph >= beg
21958 && INTEGERP (glyph->object));
21959 if (glyph < beg
21960 || !EQ (stop, glyph->object))
21961 break;
21962 row = prev;
21966 *x = row->x;
21967 *y = row->y;
21968 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21970 glyph = row->glyphs[TEXT_AREA];
21971 end = glyph + row->used[TEXT_AREA];
21973 /* Skip over glyphs not having an object at the start of the row.
21974 These are special glyphs like truncation marks on terminal
21975 frames. */
21976 if (row->displays_text_p)
21977 while (glyph < end
21978 && INTEGERP (glyph->object)
21979 && !EQ (stop, glyph->object)
21980 && glyph->charpos < 0)
21982 *x += glyph->pixel_width;
21983 ++glyph;
21986 while (glyph < end
21987 && !INTEGERP (glyph->object)
21988 && !EQ (stop, glyph->object)
21989 && (!BUFFERP (glyph->object)
21990 || glyph->charpos < charpos))
21992 *x += glyph->pixel_width;
21993 ++glyph;
21996 *hpos = glyph - row->glyphs[TEXT_AREA];
21997 return !past_end;
22000 #else /* not 1 */
22002 static int
22003 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22004 struct window *w;
22005 int pos;
22006 int *hpos, *vpos, *x, *y;
22007 Lisp_Object stop;
22009 int i;
22010 int lastcol;
22011 int maybe_next_line_p = 0;
22012 int line_start_position;
22013 int yb = window_text_bottom_y (w);
22014 struct glyph_row *row, *best_row;
22015 int row_vpos, best_row_vpos;
22016 int current_x;
22018 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22019 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22021 while (row->y < yb)
22023 if (row->used[TEXT_AREA])
22024 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22025 else
22026 line_start_position = 0;
22028 if (line_start_position > pos)
22029 break;
22030 /* If the position sought is the end of the buffer,
22031 don't include the blank lines at the bottom of the window. */
22032 else if (line_start_position == pos
22033 && pos == BUF_ZV (XBUFFER (w->buffer)))
22035 maybe_next_line_p = 1;
22036 break;
22038 else if (line_start_position > 0)
22040 best_row = row;
22041 best_row_vpos = row_vpos;
22044 if (row->y + row->height >= yb)
22045 break;
22047 ++row;
22048 ++row_vpos;
22051 /* Find the right column within BEST_ROW. */
22052 lastcol = 0;
22053 current_x = best_row->x;
22054 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22056 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22057 int charpos = glyph->charpos;
22059 if (BUFFERP (glyph->object))
22061 if (charpos == pos)
22063 *hpos = i;
22064 *vpos = best_row_vpos;
22065 *x = current_x;
22066 *y = best_row->y;
22067 return 1;
22069 else if (charpos > pos)
22070 break;
22072 else if (EQ (glyph->object, stop))
22073 break;
22075 if (charpos > 0)
22076 lastcol = i;
22077 current_x += glyph->pixel_width;
22080 /* If we're looking for the end of the buffer,
22081 and we didn't find it in the line we scanned,
22082 use the start of the following line. */
22083 if (maybe_next_line_p)
22085 ++best_row;
22086 ++best_row_vpos;
22087 lastcol = 0;
22088 current_x = best_row->x;
22091 *vpos = best_row_vpos;
22092 *hpos = lastcol + 1;
22093 *x = current_x;
22094 *y = best_row->y;
22095 return 0;
22098 #endif /* not 1 */
22101 /* Find the position of the glyph for position POS in OBJECT in
22102 window W's current matrix, and return in *X, *Y the pixel
22103 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22105 RIGHT_P non-zero means return the position of the right edge of the
22106 glyph, RIGHT_P zero means return the left edge position.
22108 If no glyph for POS exists in the matrix, return the position of
22109 the glyph with the next smaller position that is in the matrix, if
22110 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22111 exists in the matrix, return the position of the glyph with the
22112 next larger position in OBJECT.
22114 Value is non-zero if a glyph was found. */
22116 static int
22117 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22118 struct window *w;
22119 int pos;
22120 Lisp_Object object;
22121 int *hpos, *vpos, *x, *y;
22122 int right_p;
22124 int yb = window_text_bottom_y (w);
22125 struct glyph_row *r;
22126 struct glyph *best_glyph = NULL;
22127 struct glyph_row *best_row = NULL;
22128 int best_x = 0;
22130 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22131 r->enabled_p && r->y < yb;
22132 ++r)
22134 struct glyph *g = r->glyphs[TEXT_AREA];
22135 struct glyph *e = g + r->used[TEXT_AREA];
22136 int gx;
22138 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22139 if (EQ (g->object, object))
22141 if (g->charpos == pos)
22143 best_glyph = g;
22144 best_x = gx;
22145 best_row = r;
22146 goto found;
22148 else if (best_glyph == NULL
22149 || ((abs (g->charpos - pos)
22150 < abs (best_glyph->charpos - pos))
22151 && (right_p
22152 ? g->charpos < pos
22153 : g->charpos > pos)))
22155 best_glyph = g;
22156 best_x = gx;
22157 best_row = r;
22162 found:
22164 if (best_glyph)
22166 *x = best_x;
22167 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22169 if (right_p)
22171 *x += best_glyph->pixel_width;
22172 ++*hpos;
22175 *y = best_row->y;
22176 *vpos = best_row - w->current_matrix->rows;
22179 return best_glyph != NULL;
22183 /* See if position X, Y is within a hot-spot of an image. */
22185 static int
22186 on_hot_spot_p (hot_spot, x, y)
22187 Lisp_Object hot_spot;
22188 int x, y;
22190 if (!CONSP (hot_spot))
22191 return 0;
22193 if (EQ (XCAR (hot_spot), Qrect))
22195 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22196 Lisp_Object rect = XCDR (hot_spot);
22197 Lisp_Object tem;
22198 if (!CONSP (rect))
22199 return 0;
22200 if (!CONSP (XCAR (rect)))
22201 return 0;
22202 if (!CONSP (XCDR (rect)))
22203 return 0;
22204 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22205 return 0;
22206 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22207 return 0;
22208 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22209 return 0;
22210 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22211 return 0;
22212 return 1;
22214 else if (EQ (XCAR (hot_spot), Qcircle))
22216 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22217 Lisp_Object circ = XCDR (hot_spot);
22218 Lisp_Object lr, lx0, ly0;
22219 if (CONSP (circ)
22220 && CONSP (XCAR (circ))
22221 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22222 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22223 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22225 double r = XFLOATINT (lr);
22226 double dx = XINT (lx0) - x;
22227 double dy = XINT (ly0) - y;
22228 return (dx * dx + dy * dy <= r * r);
22231 else if (EQ (XCAR (hot_spot), Qpoly))
22233 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22234 if (VECTORP (XCDR (hot_spot)))
22236 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22237 Lisp_Object *poly = v->contents;
22238 int n = v->size;
22239 int i;
22240 int inside = 0;
22241 Lisp_Object lx, ly;
22242 int x0, y0;
22244 /* Need an even number of coordinates, and at least 3 edges. */
22245 if (n < 6 || n & 1)
22246 return 0;
22248 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22249 If count is odd, we are inside polygon. Pixels on edges
22250 may or may not be included depending on actual geometry of the
22251 polygon. */
22252 if ((lx = poly[n-2], !INTEGERP (lx))
22253 || (ly = poly[n-1], !INTEGERP (lx)))
22254 return 0;
22255 x0 = XINT (lx), y0 = XINT (ly);
22256 for (i = 0; i < n; i += 2)
22258 int x1 = x0, y1 = y0;
22259 if ((lx = poly[i], !INTEGERP (lx))
22260 || (ly = poly[i+1], !INTEGERP (ly)))
22261 return 0;
22262 x0 = XINT (lx), y0 = XINT (ly);
22264 /* Does this segment cross the X line? */
22265 if (x0 >= x)
22267 if (x1 >= x)
22268 continue;
22270 else if (x1 < x)
22271 continue;
22272 if (y > y0 && y > y1)
22273 continue;
22274 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22275 inside = !inside;
22277 return inside;
22280 /* If we don't understand the format, pretend we're not in the hot-spot. */
22281 return 0;
22284 Lisp_Object
22285 find_hot_spot (map, x, y)
22286 Lisp_Object map;
22287 int x, y;
22289 while (CONSP (map))
22291 if (CONSP (XCAR (map))
22292 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22293 return XCAR (map);
22294 map = XCDR (map);
22297 return Qnil;
22300 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22301 3, 3, 0,
22302 doc: /* Lookup in image map MAP coordinates X and Y.
22303 An image map is an alist where each element has the format (AREA ID PLIST).
22304 An AREA is specified as either a rectangle, a circle, or a polygon:
22305 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22306 pixel coordinates of the upper left and bottom right corners.
22307 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22308 and the radius of the circle; r may be a float or integer.
22309 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22310 vector describes one corner in the polygon.
22311 Returns the alist element for the first matching AREA in MAP. */)
22312 (map, x, y)
22313 Lisp_Object map;
22314 Lisp_Object x, y;
22316 if (NILP (map))
22317 return Qnil;
22319 CHECK_NUMBER (x);
22320 CHECK_NUMBER (y);
22322 return find_hot_spot (map, XINT (x), XINT (y));
22326 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22327 static void
22328 define_frame_cursor1 (f, cursor, pointer)
22329 struct frame *f;
22330 Cursor cursor;
22331 Lisp_Object pointer;
22333 /* Do not change cursor shape while dragging mouse. */
22334 if (!NILP (do_mouse_tracking))
22335 return;
22337 if (!NILP (pointer))
22339 if (EQ (pointer, Qarrow))
22340 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22341 else if (EQ (pointer, Qhand))
22342 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22343 else if (EQ (pointer, Qtext))
22344 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22345 else if (EQ (pointer, intern ("hdrag")))
22346 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22347 #ifdef HAVE_X_WINDOWS
22348 else if (EQ (pointer, intern ("vdrag")))
22349 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22350 #endif
22351 else if (EQ (pointer, intern ("hourglass")))
22352 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22353 else if (EQ (pointer, Qmodeline))
22354 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22355 else
22356 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22359 if (cursor != No_Cursor)
22360 rif->define_frame_cursor (f, cursor);
22363 /* Take proper action when mouse has moved to the mode or header line
22364 or marginal area AREA of window W, x-position X and y-position Y.
22365 X is relative to the start of the text display area of W, so the
22366 width of bitmap areas and scroll bars must be subtracted to get a
22367 position relative to the start of the mode line. */
22369 static void
22370 note_mode_line_or_margin_highlight (window, x, y, area)
22371 Lisp_Object window;
22372 int x, y;
22373 enum window_part area;
22375 struct window *w = XWINDOW (window);
22376 struct frame *f = XFRAME (w->frame);
22377 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22378 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22379 Lisp_Object pointer = Qnil;
22380 int charpos, dx, dy, width, height;
22381 Lisp_Object string, object = Qnil;
22382 Lisp_Object pos, help;
22384 Lisp_Object mouse_face;
22385 int original_x_pixel = x;
22386 struct glyph * glyph = NULL;
22387 struct glyph_row *row;
22389 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22391 int x0;
22392 struct glyph *end;
22394 string = mode_line_string (w, area, &x, &y, &charpos,
22395 &object, &dx, &dy, &width, &height);
22397 row = (area == ON_MODE_LINE
22398 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22399 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22401 /* Find glyph */
22402 if (row->mode_line_p && row->enabled_p)
22404 glyph = row->glyphs[TEXT_AREA];
22405 end = glyph + row->used[TEXT_AREA];
22407 for (x0 = original_x_pixel;
22408 glyph < end && x0 >= glyph->pixel_width;
22409 ++glyph)
22410 x0 -= glyph->pixel_width;
22412 if (glyph >= end)
22413 glyph = NULL;
22416 else
22418 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22419 string = marginal_area_string (w, area, &x, &y, &charpos,
22420 &object, &dx, &dy, &width, &height);
22423 help = Qnil;
22425 if (IMAGEP (object))
22427 Lisp_Object image_map, hotspot;
22428 if ((image_map = Fplist_get (XCDR (object), QCmap),
22429 !NILP (image_map))
22430 && (hotspot = find_hot_spot (image_map, dx, dy),
22431 CONSP (hotspot))
22432 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22434 Lisp_Object area_id, plist;
22436 area_id = XCAR (hotspot);
22437 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22438 If so, we could look for mouse-enter, mouse-leave
22439 properties in PLIST (and do something...). */
22440 hotspot = XCDR (hotspot);
22441 if (CONSP (hotspot)
22442 && (plist = XCAR (hotspot), CONSP (plist)))
22444 pointer = Fplist_get (plist, Qpointer);
22445 if (NILP (pointer))
22446 pointer = Qhand;
22447 help = Fplist_get (plist, Qhelp_echo);
22448 if (!NILP (help))
22450 help_echo_string = help;
22451 /* Is this correct? ++kfs */
22452 XSETWINDOW (help_echo_window, w);
22453 help_echo_object = w->buffer;
22454 help_echo_pos = charpos;
22458 if (NILP (pointer))
22459 pointer = Fplist_get (XCDR (object), QCpointer);
22462 if (STRINGP (string))
22464 pos = make_number (charpos);
22465 /* If we're on a string with `help-echo' text property, arrange
22466 for the help to be displayed. This is done by setting the
22467 global variable help_echo_string to the help string. */
22468 if (NILP (help))
22470 help = Fget_text_property (pos, Qhelp_echo, string);
22471 if (!NILP (help))
22473 help_echo_string = help;
22474 XSETWINDOW (help_echo_window, w);
22475 help_echo_object = string;
22476 help_echo_pos = charpos;
22480 if (NILP (pointer))
22481 pointer = Fget_text_property (pos, Qpointer, string);
22483 /* Change the mouse pointer according to what is under X/Y. */
22484 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22486 Lisp_Object map;
22487 map = Fget_text_property (pos, Qlocal_map, string);
22488 if (!KEYMAPP (map))
22489 map = Fget_text_property (pos, Qkeymap, string);
22490 if (!KEYMAPP (map))
22491 cursor = dpyinfo->vertical_scroll_bar_cursor;
22494 /* Change the mouse face according to what is under X/Y. */
22495 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22496 if (!NILP (mouse_face)
22497 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22498 && glyph)
22500 Lisp_Object b, e;
22502 struct glyph * tmp_glyph;
22504 int gpos;
22505 int gseq_length;
22506 int total_pixel_width;
22507 int ignore;
22509 int vpos, hpos;
22511 b = Fprevious_single_property_change (make_number (charpos + 1),
22512 Qmouse_face, string, Qnil);
22513 if (NILP (b))
22514 b = make_number (0);
22516 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22517 if (NILP (e))
22518 e = make_number (SCHARS (string));
22520 /* Calculate the position(glyph position: GPOS) of GLYPH in
22521 displayed string. GPOS is different from CHARPOS.
22523 CHARPOS is the position of glyph in internal string
22524 object. A mode line string format has structures which
22525 is converted to a flatten by emacs lisp interpreter.
22526 The internal string is an element of the structures.
22527 The displayed string is the flatten string. */
22528 for (tmp_glyph = glyph - 1, gpos = 0;
22529 tmp_glyph->charpos >= XINT (b);
22530 tmp_glyph--, gpos++)
22532 if (!EQ (tmp_glyph->object, glyph->object))
22533 break;
22536 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22537 displayed string holding GLYPH.
22539 GSEQ_LENGTH is different from SCHARS (STRING).
22540 SCHARS (STRING) returns the length of the internal string. */
22541 for (tmp_glyph = glyph, gseq_length = gpos;
22542 tmp_glyph->charpos < XINT (e);
22543 tmp_glyph++, gseq_length++)
22545 if (!EQ (tmp_glyph->object, glyph->object))
22546 break;
22549 total_pixel_width = 0;
22550 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22551 total_pixel_width += tmp_glyph->pixel_width;
22553 /* Pre calculation of re-rendering position */
22554 vpos = (x - gpos);
22555 hpos = (area == ON_MODE_LINE
22556 ? (w->current_matrix)->nrows - 1
22557 : 0);
22559 /* If the re-rendering position is included in the last
22560 re-rendering area, we should do nothing. */
22561 if ( EQ (window, dpyinfo->mouse_face_window)
22562 && dpyinfo->mouse_face_beg_col <= vpos
22563 && vpos < dpyinfo->mouse_face_end_col
22564 && dpyinfo->mouse_face_beg_row == hpos )
22565 return;
22567 if (clear_mouse_face (dpyinfo))
22568 cursor = No_Cursor;
22570 dpyinfo->mouse_face_beg_col = vpos;
22571 dpyinfo->mouse_face_beg_row = hpos;
22573 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22574 dpyinfo->mouse_face_beg_y = 0;
22576 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22577 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22579 dpyinfo->mouse_face_end_x = 0;
22580 dpyinfo->mouse_face_end_y = 0;
22582 dpyinfo->mouse_face_past_end = 0;
22583 dpyinfo->mouse_face_window = window;
22585 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22586 charpos,
22587 0, 0, 0, &ignore,
22588 glyph->face_id, 1);
22589 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22591 if (NILP (pointer))
22592 pointer = Qhand;
22594 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22595 clear_mouse_face (dpyinfo);
22597 define_frame_cursor1 (f, cursor, pointer);
22601 /* EXPORT:
22602 Take proper action when the mouse has moved to position X, Y on
22603 frame F as regards highlighting characters that have mouse-face
22604 properties. Also de-highlighting chars where the mouse was before.
22605 X and Y can be negative or out of range. */
22607 void
22608 note_mouse_highlight (f, x, y)
22609 struct frame *f;
22610 int x, y;
22612 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22613 enum window_part part;
22614 Lisp_Object window;
22615 struct window *w;
22616 Cursor cursor = No_Cursor;
22617 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22618 struct buffer *b;
22620 /* When a menu is active, don't highlight because this looks odd. */
22621 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22622 if (popup_activated ())
22623 return;
22624 #endif
22626 if (NILP (Vmouse_highlight)
22627 || !f->glyphs_initialized_p)
22628 return;
22630 dpyinfo->mouse_face_mouse_x = x;
22631 dpyinfo->mouse_face_mouse_y = y;
22632 dpyinfo->mouse_face_mouse_frame = f;
22634 if (dpyinfo->mouse_face_defer)
22635 return;
22637 if (gc_in_progress)
22639 dpyinfo->mouse_face_deferred_gc = 1;
22640 return;
22643 /* Which window is that in? */
22644 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22646 /* If we were displaying active text in another window, clear that.
22647 Also clear if we move out of text area in same window. */
22648 if (! EQ (window, dpyinfo->mouse_face_window)
22649 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22650 && !NILP (dpyinfo->mouse_face_window)))
22651 clear_mouse_face (dpyinfo);
22653 /* Not on a window -> return. */
22654 if (!WINDOWP (window))
22655 return;
22657 /* Reset help_echo_string. It will get recomputed below. */
22658 help_echo_string = Qnil;
22660 /* Convert to window-relative pixel coordinates. */
22661 w = XWINDOW (window);
22662 frame_to_window_pixel_xy (w, &x, &y);
22664 /* Handle tool-bar window differently since it doesn't display a
22665 buffer. */
22666 if (EQ (window, f->tool_bar_window))
22668 note_tool_bar_highlight (f, x, y);
22669 return;
22672 /* Mouse is on the mode, header line or margin? */
22673 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22674 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22676 note_mode_line_or_margin_highlight (window, x, y, part);
22677 return;
22680 if (part == ON_VERTICAL_BORDER)
22682 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22683 help_echo_string = build_string ("drag-mouse-1: resize");
22685 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22686 || part == ON_SCROLL_BAR)
22687 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22688 else
22689 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22691 /* Are we in a window whose display is up to date?
22692 And verify the buffer's text has not changed. */
22693 b = XBUFFER (w->buffer);
22694 if (part == ON_TEXT
22695 && EQ (w->window_end_valid, w->buffer)
22696 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22697 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22699 int hpos, vpos, pos, i, dx, dy, area;
22700 struct glyph *glyph;
22701 Lisp_Object object;
22702 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22703 Lisp_Object *overlay_vec = NULL;
22704 int noverlays;
22705 struct buffer *obuf;
22706 int obegv, ozv, same_region;
22708 /* Find the glyph under X/Y. */
22709 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22711 /* Look for :pointer property on image. */
22712 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22714 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22715 if (img != NULL && IMAGEP (img->spec))
22717 Lisp_Object image_map, hotspot;
22718 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22719 !NILP (image_map))
22720 && (hotspot = find_hot_spot (image_map,
22721 glyph->slice.x + dx,
22722 glyph->slice.y + dy),
22723 CONSP (hotspot))
22724 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22726 Lisp_Object area_id, plist;
22728 area_id = XCAR (hotspot);
22729 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22730 If so, we could look for mouse-enter, mouse-leave
22731 properties in PLIST (and do something...). */
22732 hotspot = XCDR (hotspot);
22733 if (CONSP (hotspot)
22734 && (plist = XCAR (hotspot), CONSP (plist)))
22736 pointer = Fplist_get (plist, Qpointer);
22737 if (NILP (pointer))
22738 pointer = Qhand;
22739 help_echo_string = Fplist_get (plist, Qhelp_echo);
22740 if (!NILP (help_echo_string))
22742 help_echo_window = window;
22743 help_echo_object = glyph->object;
22744 help_echo_pos = glyph->charpos;
22748 if (NILP (pointer))
22749 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22753 /* Clear mouse face if X/Y not over text. */
22754 if (glyph == NULL
22755 || area != TEXT_AREA
22756 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22758 if (clear_mouse_face (dpyinfo))
22759 cursor = No_Cursor;
22760 if (NILP (pointer))
22762 if (area != TEXT_AREA)
22763 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22764 else
22765 pointer = Vvoid_text_area_pointer;
22767 goto set_cursor;
22770 pos = glyph->charpos;
22771 object = glyph->object;
22772 if (!STRINGP (object) && !BUFFERP (object))
22773 goto set_cursor;
22775 /* If we get an out-of-range value, return now; avoid an error. */
22776 if (BUFFERP (object) && pos > BUF_Z (b))
22777 goto set_cursor;
22779 /* Make the window's buffer temporarily current for
22780 overlays_at and compute_char_face. */
22781 obuf = current_buffer;
22782 current_buffer = b;
22783 obegv = BEGV;
22784 ozv = ZV;
22785 BEGV = BEG;
22786 ZV = Z;
22788 /* Is this char mouse-active or does it have help-echo? */
22789 position = make_number (pos);
22791 if (BUFFERP (object))
22793 /* Put all the overlays we want in a vector in overlay_vec. */
22794 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22795 /* Sort overlays into increasing priority order. */
22796 noverlays = sort_overlays (overlay_vec, noverlays, w);
22798 else
22799 noverlays = 0;
22801 same_region = (EQ (window, dpyinfo->mouse_face_window)
22802 && vpos >= dpyinfo->mouse_face_beg_row
22803 && vpos <= dpyinfo->mouse_face_end_row
22804 && (vpos > dpyinfo->mouse_face_beg_row
22805 || hpos >= dpyinfo->mouse_face_beg_col)
22806 && (vpos < dpyinfo->mouse_face_end_row
22807 || hpos < dpyinfo->mouse_face_end_col
22808 || dpyinfo->mouse_face_past_end));
22810 if (same_region)
22811 cursor = No_Cursor;
22813 /* Check mouse-face highlighting. */
22814 if (! same_region
22815 /* If there exists an overlay with mouse-face overlapping
22816 the one we are currently highlighting, we have to
22817 check if we enter the overlapping overlay, and then
22818 highlight only that. */
22819 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22820 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22822 /* Find the highest priority overlay that has a mouse-face
22823 property. */
22824 overlay = Qnil;
22825 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22827 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22828 if (!NILP (mouse_face))
22829 overlay = overlay_vec[i];
22832 /* If we're actually highlighting the same overlay as
22833 before, there's no need to do that again. */
22834 if (!NILP (overlay)
22835 && EQ (overlay, dpyinfo->mouse_face_overlay))
22836 goto check_help_echo;
22838 dpyinfo->mouse_face_overlay = overlay;
22840 /* Clear the display of the old active region, if any. */
22841 if (clear_mouse_face (dpyinfo))
22842 cursor = No_Cursor;
22844 /* If no overlay applies, get a text property. */
22845 if (NILP (overlay))
22846 mouse_face = Fget_text_property (position, Qmouse_face, object);
22848 /* Handle the overlay case. */
22849 if (!NILP (overlay))
22851 /* Find the range of text around this char that
22852 should be active. */
22853 Lisp_Object before, after;
22854 int ignore;
22856 before = Foverlay_start (overlay);
22857 after = Foverlay_end (overlay);
22858 /* Record this as the current active region. */
22859 fast_find_position (w, XFASTINT (before),
22860 &dpyinfo->mouse_face_beg_col,
22861 &dpyinfo->mouse_face_beg_row,
22862 &dpyinfo->mouse_face_beg_x,
22863 &dpyinfo->mouse_face_beg_y, Qnil);
22865 dpyinfo->mouse_face_past_end
22866 = !fast_find_position (w, XFASTINT (after),
22867 &dpyinfo->mouse_face_end_col,
22868 &dpyinfo->mouse_face_end_row,
22869 &dpyinfo->mouse_face_end_x,
22870 &dpyinfo->mouse_face_end_y, Qnil);
22871 dpyinfo->mouse_face_window = window;
22873 dpyinfo->mouse_face_face_id
22874 = face_at_buffer_position (w, pos, 0, 0,
22875 &ignore, pos + 1,
22876 !dpyinfo->mouse_face_hidden);
22878 /* Display it as active. */
22879 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22880 cursor = No_Cursor;
22882 /* Handle the text property case. */
22883 else if (!NILP (mouse_face) && BUFFERP (object))
22885 /* Find the range of text around this char that
22886 should be active. */
22887 Lisp_Object before, after, beginning, end;
22888 int ignore;
22890 beginning = Fmarker_position (w->start);
22891 end = make_number (BUF_Z (XBUFFER (object))
22892 - XFASTINT (w->window_end_pos));
22893 before
22894 = Fprevious_single_property_change (make_number (pos + 1),
22895 Qmouse_face,
22896 object, beginning);
22897 after
22898 = Fnext_single_property_change (position, Qmouse_face,
22899 object, end);
22901 /* Record this as the current active region. */
22902 fast_find_position (w, XFASTINT (before),
22903 &dpyinfo->mouse_face_beg_col,
22904 &dpyinfo->mouse_face_beg_row,
22905 &dpyinfo->mouse_face_beg_x,
22906 &dpyinfo->mouse_face_beg_y, Qnil);
22907 dpyinfo->mouse_face_past_end
22908 = !fast_find_position (w, XFASTINT (after),
22909 &dpyinfo->mouse_face_end_col,
22910 &dpyinfo->mouse_face_end_row,
22911 &dpyinfo->mouse_face_end_x,
22912 &dpyinfo->mouse_face_end_y, Qnil);
22913 dpyinfo->mouse_face_window = window;
22915 if (BUFFERP (object))
22916 dpyinfo->mouse_face_face_id
22917 = face_at_buffer_position (w, pos, 0, 0,
22918 &ignore, pos + 1,
22919 !dpyinfo->mouse_face_hidden);
22921 /* Display it as active. */
22922 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22923 cursor = No_Cursor;
22925 else if (!NILP (mouse_face) && STRINGP (object))
22927 Lisp_Object b, e;
22928 int ignore;
22930 b = Fprevious_single_property_change (make_number (pos + 1),
22931 Qmouse_face,
22932 object, Qnil);
22933 e = Fnext_single_property_change (position, Qmouse_face,
22934 object, Qnil);
22935 if (NILP (b))
22936 b = make_number (0);
22937 if (NILP (e))
22938 e = make_number (SCHARS (object) - 1);
22940 fast_find_string_pos (w, XINT (b), object,
22941 &dpyinfo->mouse_face_beg_col,
22942 &dpyinfo->mouse_face_beg_row,
22943 &dpyinfo->mouse_face_beg_x,
22944 &dpyinfo->mouse_face_beg_y, 0);
22945 fast_find_string_pos (w, XINT (e), object,
22946 &dpyinfo->mouse_face_end_col,
22947 &dpyinfo->mouse_face_end_row,
22948 &dpyinfo->mouse_face_end_x,
22949 &dpyinfo->mouse_face_end_y, 1);
22950 dpyinfo->mouse_face_past_end = 0;
22951 dpyinfo->mouse_face_window = window;
22952 dpyinfo->mouse_face_face_id
22953 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22954 glyph->face_id, 1);
22955 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22956 cursor = No_Cursor;
22958 else if (STRINGP (object) && NILP (mouse_face))
22960 /* A string which doesn't have mouse-face, but
22961 the text ``under'' it might have. */
22962 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22963 int start = MATRIX_ROW_START_CHARPOS (r);
22965 pos = string_buffer_position (w, object, start);
22966 if (pos > 0)
22967 mouse_face = get_char_property_and_overlay (make_number (pos),
22968 Qmouse_face,
22969 w->buffer,
22970 &overlay);
22971 if (!NILP (mouse_face) && !NILP (overlay))
22973 Lisp_Object before = Foverlay_start (overlay);
22974 Lisp_Object after = Foverlay_end (overlay);
22975 int ignore;
22977 /* Note that we might not be able to find position
22978 BEFORE in the glyph matrix if the overlay is
22979 entirely covered by a `display' property. In
22980 this case, we overshoot. So let's stop in
22981 the glyph matrix before glyphs for OBJECT. */
22982 fast_find_position (w, XFASTINT (before),
22983 &dpyinfo->mouse_face_beg_col,
22984 &dpyinfo->mouse_face_beg_row,
22985 &dpyinfo->mouse_face_beg_x,
22986 &dpyinfo->mouse_face_beg_y,
22987 object);
22989 dpyinfo->mouse_face_past_end
22990 = !fast_find_position (w, XFASTINT (after),
22991 &dpyinfo->mouse_face_end_col,
22992 &dpyinfo->mouse_face_end_row,
22993 &dpyinfo->mouse_face_end_x,
22994 &dpyinfo->mouse_face_end_y,
22995 Qnil);
22996 dpyinfo->mouse_face_window = window;
22997 dpyinfo->mouse_face_face_id
22998 = face_at_buffer_position (w, pos, 0, 0,
22999 &ignore, pos + 1,
23000 !dpyinfo->mouse_face_hidden);
23002 /* Display it as active. */
23003 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23004 cursor = No_Cursor;
23009 check_help_echo:
23011 /* Look for a `help-echo' property. */
23012 if (NILP (help_echo_string)) {
23013 Lisp_Object help, overlay;
23015 /* Check overlays first. */
23016 help = overlay = Qnil;
23017 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23019 overlay = overlay_vec[i];
23020 help = Foverlay_get (overlay, Qhelp_echo);
23023 if (!NILP (help))
23025 help_echo_string = help;
23026 help_echo_window = window;
23027 help_echo_object = overlay;
23028 help_echo_pos = pos;
23030 else
23032 Lisp_Object object = glyph->object;
23033 int charpos = glyph->charpos;
23035 /* Try text properties. */
23036 if (STRINGP (object)
23037 && charpos >= 0
23038 && charpos < SCHARS (object))
23040 help = Fget_text_property (make_number (charpos),
23041 Qhelp_echo, object);
23042 if (NILP (help))
23044 /* If the string itself doesn't specify a help-echo,
23045 see if the buffer text ``under'' it does. */
23046 struct glyph_row *r
23047 = MATRIX_ROW (w->current_matrix, vpos);
23048 int start = MATRIX_ROW_START_CHARPOS (r);
23049 int pos = string_buffer_position (w, object, start);
23050 if (pos > 0)
23052 help = Fget_char_property (make_number (pos),
23053 Qhelp_echo, w->buffer);
23054 if (!NILP (help))
23056 charpos = pos;
23057 object = w->buffer;
23062 else if (BUFFERP (object)
23063 && charpos >= BEGV
23064 && charpos < ZV)
23065 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23066 object);
23068 if (!NILP (help))
23070 help_echo_string = help;
23071 help_echo_window = window;
23072 help_echo_object = object;
23073 help_echo_pos = charpos;
23078 /* Look for a `pointer' property. */
23079 if (NILP (pointer))
23081 /* Check overlays first. */
23082 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23083 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23085 if (NILP (pointer))
23087 Lisp_Object object = glyph->object;
23088 int charpos = glyph->charpos;
23090 /* Try text properties. */
23091 if (STRINGP (object)
23092 && charpos >= 0
23093 && charpos < SCHARS (object))
23095 pointer = Fget_text_property (make_number (charpos),
23096 Qpointer, object);
23097 if (NILP (pointer))
23099 /* If the string itself doesn't specify a pointer,
23100 see if the buffer text ``under'' it does. */
23101 struct glyph_row *r
23102 = MATRIX_ROW (w->current_matrix, vpos);
23103 int start = MATRIX_ROW_START_CHARPOS (r);
23104 int pos = string_buffer_position (w, object, start);
23105 if (pos > 0)
23106 pointer = Fget_char_property (make_number (pos),
23107 Qpointer, w->buffer);
23110 else if (BUFFERP (object)
23111 && charpos >= BEGV
23112 && charpos < ZV)
23113 pointer = Fget_text_property (make_number (charpos),
23114 Qpointer, object);
23118 BEGV = obegv;
23119 ZV = ozv;
23120 current_buffer = obuf;
23123 set_cursor:
23125 define_frame_cursor1 (f, cursor, pointer);
23129 /* EXPORT for RIF:
23130 Clear any mouse-face on window W. This function is part of the
23131 redisplay interface, and is called from try_window_id and similar
23132 functions to ensure the mouse-highlight is off. */
23134 void
23135 x_clear_window_mouse_face (w)
23136 struct window *w;
23138 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23139 Lisp_Object window;
23141 BLOCK_INPUT;
23142 XSETWINDOW (window, w);
23143 if (EQ (window, dpyinfo->mouse_face_window))
23144 clear_mouse_face (dpyinfo);
23145 UNBLOCK_INPUT;
23149 /* EXPORT:
23150 Just discard the mouse face information for frame F, if any.
23151 This is used when the size of F is changed. */
23153 void
23154 cancel_mouse_face (f)
23155 struct frame *f;
23157 Lisp_Object window;
23158 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23160 window = dpyinfo->mouse_face_window;
23161 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23163 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23164 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23165 dpyinfo->mouse_face_window = Qnil;
23170 #endif /* HAVE_WINDOW_SYSTEM */
23173 /***********************************************************************
23174 Exposure Events
23175 ***********************************************************************/
23177 #ifdef HAVE_WINDOW_SYSTEM
23179 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23180 which intersects rectangle R. R is in window-relative coordinates. */
23182 static void
23183 expose_area (w, row, r, area)
23184 struct window *w;
23185 struct glyph_row *row;
23186 XRectangle *r;
23187 enum glyph_row_area area;
23189 struct glyph *first = row->glyphs[area];
23190 struct glyph *end = row->glyphs[area] + row->used[area];
23191 struct glyph *last;
23192 int first_x, start_x, x;
23194 if (area == TEXT_AREA && row->fill_line_p)
23195 /* If row extends face to end of line write the whole line. */
23196 draw_glyphs (w, 0, row, area,
23197 0, row->used[area],
23198 DRAW_NORMAL_TEXT, 0);
23199 else
23201 /* Set START_X to the window-relative start position for drawing glyphs of
23202 AREA. The first glyph of the text area can be partially visible.
23203 The first glyphs of other areas cannot. */
23204 start_x = window_box_left_offset (w, area);
23205 x = start_x;
23206 if (area == TEXT_AREA)
23207 x += row->x;
23209 /* Find the first glyph that must be redrawn. */
23210 while (first < end
23211 && x + first->pixel_width < r->x)
23213 x += first->pixel_width;
23214 ++first;
23217 /* Find the last one. */
23218 last = first;
23219 first_x = x;
23220 while (last < end
23221 && x < r->x + r->width)
23223 x += last->pixel_width;
23224 ++last;
23227 /* Repaint. */
23228 if (last > first)
23229 draw_glyphs (w, first_x - start_x, row, area,
23230 first - row->glyphs[area], last - row->glyphs[area],
23231 DRAW_NORMAL_TEXT, 0);
23236 /* Redraw the parts of the glyph row ROW on window W intersecting
23237 rectangle R. R is in window-relative coordinates. Value is
23238 non-zero if mouse-face was overwritten. */
23240 static int
23241 expose_line (w, row, r)
23242 struct window *w;
23243 struct glyph_row *row;
23244 XRectangle *r;
23246 xassert (row->enabled_p);
23248 if (row->mode_line_p || w->pseudo_window_p)
23249 draw_glyphs (w, 0, row, TEXT_AREA,
23250 0, row->used[TEXT_AREA],
23251 DRAW_NORMAL_TEXT, 0);
23252 else
23254 if (row->used[LEFT_MARGIN_AREA])
23255 expose_area (w, row, r, LEFT_MARGIN_AREA);
23256 if (row->used[TEXT_AREA])
23257 expose_area (w, row, r, TEXT_AREA);
23258 if (row->used[RIGHT_MARGIN_AREA])
23259 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23260 draw_row_fringe_bitmaps (w, row);
23263 return row->mouse_face_p;
23267 /* Redraw those parts of glyphs rows during expose event handling that
23268 overlap other rows. Redrawing of an exposed line writes over parts
23269 of lines overlapping that exposed line; this function fixes that.
23271 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23272 row in W's current matrix that is exposed and overlaps other rows.
23273 LAST_OVERLAPPING_ROW is the last such row. */
23275 static void
23276 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23277 struct window *w;
23278 struct glyph_row *first_overlapping_row;
23279 struct glyph_row *last_overlapping_row;
23281 struct glyph_row *row;
23283 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23284 if (row->overlapping_p)
23286 xassert (row->enabled_p && !row->mode_line_p);
23288 if (row->used[LEFT_MARGIN_AREA])
23289 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23291 if (row->used[TEXT_AREA])
23292 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23294 if (row->used[RIGHT_MARGIN_AREA])
23295 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23300 /* Return non-zero if W's cursor intersects rectangle R. */
23302 static int
23303 phys_cursor_in_rect_p (w, r)
23304 struct window *w;
23305 XRectangle *r;
23307 XRectangle cr, result;
23308 struct glyph *cursor_glyph;
23310 cursor_glyph = get_phys_cursor_glyph (w);
23311 if (cursor_glyph)
23313 /* r is relative to W's box, but w->phys_cursor.x is relative
23314 to left edge of W's TEXT area. Adjust it. */
23315 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23316 cr.y = w->phys_cursor.y;
23317 cr.width = cursor_glyph->pixel_width;
23318 cr.height = w->phys_cursor_height;
23319 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23320 I assume the effect is the same -- and this is portable. */
23321 return x_intersect_rectangles (&cr, r, &result);
23323 else
23324 return 0;
23328 /* EXPORT:
23329 Draw a vertical window border to the right of window W if W doesn't
23330 have vertical scroll bars. */
23332 void
23333 x_draw_vertical_border (w)
23334 struct window *w;
23336 /* We could do better, if we knew what type of scroll-bar the adjacent
23337 windows (on either side) have... But we don't :-(
23338 However, I think this works ok. ++KFS 2003-04-25 */
23340 /* Redraw borders between horizontally adjacent windows. Don't
23341 do it for frames with vertical scroll bars because either the
23342 right scroll bar of a window, or the left scroll bar of its
23343 neighbor will suffice as a border. */
23344 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23345 return;
23347 if (!WINDOW_RIGHTMOST_P (w)
23348 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23350 int x0, x1, y0, y1;
23352 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23353 y1 -= 1;
23355 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23356 x1 -= 1;
23358 rif->draw_vertical_window_border (w, x1, y0, y1);
23360 else if (!WINDOW_LEFTMOST_P (w)
23361 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23363 int x0, x1, y0, y1;
23365 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23366 y1 -= 1;
23368 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23369 x0 -= 1;
23371 rif->draw_vertical_window_border (w, x0, y0, y1);
23376 /* Redraw the part of window W intersection rectangle FR. Pixel
23377 coordinates in FR are frame-relative. Call this function with
23378 input blocked. Value is non-zero if the exposure overwrites
23379 mouse-face. */
23381 static int
23382 expose_window (w, fr)
23383 struct window *w;
23384 XRectangle *fr;
23386 struct frame *f = XFRAME (w->frame);
23387 XRectangle wr, r;
23388 int mouse_face_overwritten_p = 0;
23390 /* If window is not yet fully initialized, do nothing. This can
23391 happen when toolkit scroll bars are used and a window is split.
23392 Reconfiguring the scroll bar will generate an expose for a newly
23393 created window. */
23394 if (w->current_matrix == NULL)
23395 return 0;
23397 /* When we're currently updating the window, display and current
23398 matrix usually don't agree. Arrange for a thorough display
23399 later. */
23400 if (w == updated_window)
23402 SET_FRAME_GARBAGED (f);
23403 return 0;
23406 /* Frame-relative pixel rectangle of W. */
23407 wr.x = WINDOW_LEFT_EDGE_X (w);
23408 wr.y = WINDOW_TOP_EDGE_Y (w);
23409 wr.width = WINDOW_TOTAL_WIDTH (w);
23410 wr.height = WINDOW_TOTAL_HEIGHT (w);
23412 if (x_intersect_rectangles (fr, &wr, &r))
23414 int yb = window_text_bottom_y (w);
23415 struct glyph_row *row;
23416 int cursor_cleared_p;
23417 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23419 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23420 r.x, r.y, r.width, r.height));
23422 /* Convert to window coordinates. */
23423 r.x -= WINDOW_LEFT_EDGE_X (w);
23424 r.y -= WINDOW_TOP_EDGE_Y (w);
23426 /* Turn off the cursor. */
23427 if (!w->pseudo_window_p
23428 && phys_cursor_in_rect_p (w, &r))
23430 x_clear_cursor (w);
23431 cursor_cleared_p = 1;
23433 else
23434 cursor_cleared_p = 0;
23436 /* Update lines intersecting rectangle R. */
23437 first_overlapping_row = last_overlapping_row = NULL;
23438 for (row = w->current_matrix->rows;
23439 row->enabled_p;
23440 ++row)
23442 int y0 = row->y;
23443 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23445 if ((y0 >= r.y && y0 < r.y + r.height)
23446 || (y1 > r.y && y1 < r.y + r.height)
23447 || (r.y >= y0 && r.y < y1)
23448 || (r.y + r.height > y0 && r.y + r.height < y1))
23450 /* A header line may be overlapping, but there is no need
23451 to fix overlapping areas for them. KFS 2005-02-12 */
23452 if (row->overlapping_p && !row->mode_line_p)
23454 if (first_overlapping_row == NULL)
23455 first_overlapping_row = row;
23456 last_overlapping_row = row;
23459 if (expose_line (w, row, &r))
23460 mouse_face_overwritten_p = 1;
23463 if (y1 >= yb)
23464 break;
23467 /* Display the mode line if there is one. */
23468 if (WINDOW_WANTS_MODELINE_P (w)
23469 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23470 row->enabled_p)
23471 && row->y < r.y + r.height)
23473 if (expose_line (w, row, &r))
23474 mouse_face_overwritten_p = 1;
23477 if (!w->pseudo_window_p)
23479 /* Fix the display of overlapping rows. */
23480 if (first_overlapping_row)
23481 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23483 /* Draw border between windows. */
23484 x_draw_vertical_border (w);
23486 /* Turn the cursor on again. */
23487 if (cursor_cleared_p)
23488 update_window_cursor (w, 1);
23492 return mouse_face_overwritten_p;
23497 /* Redraw (parts) of all windows in the window tree rooted at W that
23498 intersect R. R contains frame pixel coordinates. Value is
23499 non-zero if the exposure overwrites mouse-face. */
23501 static int
23502 expose_window_tree (w, r)
23503 struct window *w;
23504 XRectangle *r;
23506 struct frame *f = XFRAME (w->frame);
23507 int mouse_face_overwritten_p = 0;
23509 while (w && !FRAME_GARBAGED_P (f))
23511 if (!NILP (w->hchild))
23512 mouse_face_overwritten_p
23513 |= expose_window_tree (XWINDOW (w->hchild), r);
23514 else if (!NILP (w->vchild))
23515 mouse_face_overwritten_p
23516 |= expose_window_tree (XWINDOW (w->vchild), r);
23517 else
23518 mouse_face_overwritten_p |= expose_window (w, r);
23520 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23523 return mouse_face_overwritten_p;
23527 /* EXPORT:
23528 Redisplay an exposed area of frame F. X and Y are the upper-left
23529 corner of the exposed rectangle. W and H are width and height of
23530 the exposed area. All are pixel values. W or H zero means redraw
23531 the entire frame. */
23533 void
23534 expose_frame (f, x, y, w, h)
23535 struct frame *f;
23536 int x, y, w, h;
23538 XRectangle r;
23539 int mouse_face_overwritten_p = 0;
23541 TRACE ((stderr, "expose_frame "));
23543 /* No need to redraw if frame will be redrawn soon. */
23544 if (FRAME_GARBAGED_P (f))
23546 TRACE ((stderr, " garbaged\n"));
23547 return;
23550 /* If basic faces haven't been realized yet, there is no point in
23551 trying to redraw anything. This can happen when we get an expose
23552 event while Emacs is starting, e.g. by moving another window. */
23553 if (FRAME_FACE_CACHE (f) == NULL
23554 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23556 TRACE ((stderr, " no faces\n"));
23557 return;
23560 if (w == 0 || h == 0)
23562 r.x = r.y = 0;
23563 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23564 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23566 else
23568 r.x = x;
23569 r.y = y;
23570 r.width = w;
23571 r.height = h;
23574 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23575 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23577 if (WINDOWP (f->tool_bar_window))
23578 mouse_face_overwritten_p
23579 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23581 #ifdef HAVE_X_WINDOWS
23582 #ifndef MSDOS
23583 #ifndef USE_X_TOOLKIT
23584 if (WINDOWP (f->menu_bar_window))
23585 mouse_face_overwritten_p
23586 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23587 #endif /* not USE_X_TOOLKIT */
23588 #endif
23589 #endif
23591 /* Some window managers support a focus-follows-mouse style with
23592 delayed raising of frames. Imagine a partially obscured frame,
23593 and moving the mouse into partially obscured mouse-face on that
23594 frame. The visible part of the mouse-face will be highlighted,
23595 then the WM raises the obscured frame. With at least one WM, KDE
23596 2.1, Emacs is not getting any event for the raising of the frame
23597 (even tried with SubstructureRedirectMask), only Expose events.
23598 These expose events will draw text normally, i.e. not
23599 highlighted. Which means we must redo the highlight here.
23600 Subsume it under ``we love X''. --gerd 2001-08-15 */
23601 /* Included in Windows version because Windows most likely does not
23602 do the right thing if any third party tool offers
23603 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23604 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23606 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23607 if (f == dpyinfo->mouse_face_mouse_frame)
23609 int x = dpyinfo->mouse_face_mouse_x;
23610 int y = dpyinfo->mouse_face_mouse_y;
23611 clear_mouse_face (dpyinfo);
23612 note_mouse_highlight (f, x, y);
23618 /* EXPORT:
23619 Determine the intersection of two rectangles R1 and R2. Return
23620 the intersection in *RESULT. Value is non-zero if RESULT is not
23621 empty. */
23624 x_intersect_rectangles (r1, r2, result)
23625 XRectangle *r1, *r2, *result;
23627 XRectangle *left, *right;
23628 XRectangle *upper, *lower;
23629 int intersection_p = 0;
23631 /* Rearrange so that R1 is the left-most rectangle. */
23632 if (r1->x < r2->x)
23633 left = r1, right = r2;
23634 else
23635 left = r2, right = r1;
23637 /* X0 of the intersection is right.x0, if this is inside R1,
23638 otherwise there is no intersection. */
23639 if (right->x <= left->x + left->width)
23641 result->x = right->x;
23643 /* The right end of the intersection is the minimum of the
23644 the right ends of left and right. */
23645 result->width = (min (left->x + left->width, right->x + right->width)
23646 - result->x);
23648 /* Same game for Y. */
23649 if (r1->y < r2->y)
23650 upper = r1, lower = r2;
23651 else
23652 upper = r2, lower = r1;
23654 /* The upper end of the intersection is lower.y0, if this is inside
23655 of upper. Otherwise, there is no intersection. */
23656 if (lower->y <= upper->y + upper->height)
23658 result->y = lower->y;
23660 /* The lower end of the intersection is the minimum of the lower
23661 ends of upper and lower. */
23662 result->height = (min (lower->y + lower->height,
23663 upper->y + upper->height)
23664 - result->y);
23665 intersection_p = 1;
23669 return intersection_p;
23672 #endif /* HAVE_WINDOW_SYSTEM */
23675 /***********************************************************************
23676 Initialization
23677 ***********************************************************************/
23679 void
23680 syms_of_xdisp ()
23682 Vwith_echo_area_save_vector = Qnil;
23683 staticpro (&Vwith_echo_area_save_vector);
23685 Vmessage_stack = Qnil;
23686 staticpro (&Vmessage_stack);
23688 Qinhibit_redisplay = intern ("inhibit-redisplay");
23689 staticpro (&Qinhibit_redisplay);
23691 message_dolog_marker1 = Fmake_marker ();
23692 staticpro (&message_dolog_marker1);
23693 message_dolog_marker2 = Fmake_marker ();
23694 staticpro (&message_dolog_marker2);
23695 message_dolog_marker3 = Fmake_marker ();
23696 staticpro (&message_dolog_marker3);
23698 #if GLYPH_DEBUG
23699 defsubr (&Sdump_frame_glyph_matrix);
23700 defsubr (&Sdump_glyph_matrix);
23701 defsubr (&Sdump_glyph_row);
23702 defsubr (&Sdump_tool_bar_row);
23703 defsubr (&Strace_redisplay);
23704 defsubr (&Strace_to_stderr);
23705 #endif
23706 #ifdef HAVE_WINDOW_SYSTEM
23707 defsubr (&Stool_bar_lines_needed);
23708 defsubr (&Slookup_image_map);
23709 #endif
23710 defsubr (&Sformat_mode_line);
23712 staticpro (&Qmenu_bar_update_hook);
23713 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23715 staticpro (&Qoverriding_terminal_local_map);
23716 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23718 staticpro (&Qoverriding_local_map);
23719 Qoverriding_local_map = intern ("overriding-local-map");
23721 staticpro (&Qwindow_scroll_functions);
23722 Qwindow_scroll_functions = intern ("window-scroll-functions");
23724 staticpro (&Qredisplay_end_trigger_functions);
23725 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23727 staticpro (&Qinhibit_point_motion_hooks);
23728 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23730 QCdata = intern (":data");
23731 staticpro (&QCdata);
23732 Qdisplay = intern ("display");
23733 staticpro (&Qdisplay);
23734 Qspace_width = intern ("space-width");
23735 staticpro (&Qspace_width);
23736 Qraise = intern ("raise");
23737 staticpro (&Qraise);
23738 Qslice = intern ("slice");
23739 staticpro (&Qslice);
23740 Qspace = intern ("space");
23741 staticpro (&Qspace);
23742 Qmargin = intern ("margin");
23743 staticpro (&Qmargin);
23744 Qpointer = intern ("pointer");
23745 staticpro (&Qpointer);
23746 Qleft_margin = intern ("left-margin");
23747 staticpro (&Qleft_margin);
23748 Qright_margin = intern ("right-margin");
23749 staticpro (&Qright_margin);
23750 Qcenter = intern ("center");
23751 staticpro (&Qcenter);
23752 Qline_height = intern ("line-height");
23753 staticpro (&Qline_height);
23754 QCalign_to = intern (":align-to");
23755 staticpro (&QCalign_to);
23756 QCrelative_width = intern (":relative-width");
23757 staticpro (&QCrelative_width);
23758 QCrelative_height = intern (":relative-height");
23759 staticpro (&QCrelative_height);
23760 QCeval = intern (":eval");
23761 staticpro (&QCeval);
23762 QCpropertize = intern (":propertize");
23763 staticpro (&QCpropertize);
23764 QCfile = intern (":file");
23765 staticpro (&QCfile);
23766 Qfontified = intern ("fontified");
23767 staticpro (&Qfontified);
23768 Qfontification_functions = intern ("fontification-functions");
23769 staticpro (&Qfontification_functions);
23770 Qtrailing_whitespace = intern ("trailing-whitespace");
23771 staticpro (&Qtrailing_whitespace);
23772 Qescape_glyph = intern ("escape-glyph");
23773 staticpro (&Qescape_glyph);
23774 Qnobreak_space = intern ("nobreak-space");
23775 staticpro (&Qnobreak_space);
23776 Qimage = intern ("image");
23777 staticpro (&Qimage);
23778 QCmap = intern (":map");
23779 staticpro (&QCmap);
23780 QCpointer = intern (":pointer");
23781 staticpro (&QCpointer);
23782 Qrect = intern ("rect");
23783 staticpro (&Qrect);
23784 Qcircle = intern ("circle");
23785 staticpro (&Qcircle);
23786 Qpoly = intern ("poly");
23787 staticpro (&Qpoly);
23788 Qmessage_truncate_lines = intern ("message-truncate-lines");
23789 staticpro (&Qmessage_truncate_lines);
23790 Qgrow_only = intern ("grow-only");
23791 staticpro (&Qgrow_only);
23792 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23793 staticpro (&Qinhibit_menubar_update);
23794 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23795 staticpro (&Qinhibit_eval_during_redisplay);
23796 Qposition = intern ("position");
23797 staticpro (&Qposition);
23798 Qbuffer_position = intern ("buffer-position");
23799 staticpro (&Qbuffer_position);
23800 Qobject = intern ("object");
23801 staticpro (&Qobject);
23802 Qbar = intern ("bar");
23803 staticpro (&Qbar);
23804 Qhbar = intern ("hbar");
23805 staticpro (&Qhbar);
23806 Qbox = intern ("box");
23807 staticpro (&Qbox);
23808 Qhollow = intern ("hollow");
23809 staticpro (&Qhollow);
23810 Qhand = intern ("hand");
23811 staticpro (&Qhand);
23812 Qarrow = intern ("arrow");
23813 staticpro (&Qarrow);
23814 Qtext = intern ("text");
23815 staticpro (&Qtext);
23816 Qrisky_local_variable = intern ("risky-local-variable");
23817 staticpro (&Qrisky_local_variable);
23818 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23819 staticpro (&Qinhibit_free_realized_faces);
23821 list_of_error = Fcons (Fcons (intern ("error"),
23822 Fcons (intern ("void-variable"), Qnil)),
23823 Qnil);
23824 staticpro (&list_of_error);
23826 Qlast_arrow_position = intern ("last-arrow-position");
23827 staticpro (&Qlast_arrow_position);
23828 Qlast_arrow_string = intern ("last-arrow-string");
23829 staticpro (&Qlast_arrow_string);
23831 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23832 staticpro (&Qoverlay_arrow_string);
23833 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23834 staticpro (&Qoverlay_arrow_bitmap);
23836 echo_buffer[0] = echo_buffer[1] = Qnil;
23837 staticpro (&echo_buffer[0]);
23838 staticpro (&echo_buffer[1]);
23840 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23841 staticpro (&echo_area_buffer[0]);
23842 staticpro (&echo_area_buffer[1]);
23844 Vmessages_buffer_name = build_string ("*Messages*");
23845 staticpro (&Vmessages_buffer_name);
23847 mode_line_proptrans_alist = Qnil;
23848 staticpro (&mode_line_proptrans_alist);
23849 mode_line_string_list = Qnil;
23850 staticpro (&mode_line_string_list);
23851 mode_line_string_face = Qnil;
23852 staticpro (&mode_line_string_face);
23853 mode_line_string_face_prop = Qnil;
23854 staticpro (&mode_line_string_face_prop);
23855 Vmode_line_unwind_vector = Qnil;
23856 staticpro (&Vmode_line_unwind_vector);
23858 help_echo_string = Qnil;
23859 staticpro (&help_echo_string);
23860 help_echo_object = Qnil;
23861 staticpro (&help_echo_object);
23862 help_echo_window = Qnil;
23863 staticpro (&help_echo_window);
23864 previous_help_echo_string = Qnil;
23865 staticpro (&previous_help_echo_string);
23866 help_echo_pos = -1;
23868 #ifdef HAVE_WINDOW_SYSTEM
23869 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23870 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23871 For example, if a block cursor is over a tab, it will be drawn as
23872 wide as that tab on the display. */);
23873 x_stretch_cursor_p = 0;
23874 #endif
23876 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23877 doc: /* *Non-nil means highlight trailing whitespace.
23878 The face used for trailing whitespace is `trailing-whitespace'. */);
23879 Vshow_trailing_whitespace = Qnil;
23881 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23882 doc: /* *Control highlighting of nobreak space and soft hyphen.
23883 A value of t means highlight the character itself (for nobreak space,
23884 use face `nobreak-space').
23885 A value of nil means no highlighting.
23886 Other values mean display the escape glyph followed by an ordinary
23887 space or ordinary hyphen. */);
23888 Vnobreak_char_display = Qt;
23890 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23891 doc: /* *The pointer shape to show in void text areas.
23892 A value of nil means to show the text pointer. Other options are `arrow',
23893 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23894 Vvoid_text_area_pointer = Qarrow;
23896 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23897 doc: /* Non-nil means don't actually do any redisplay.
23898 This is used for internal purposes. */);
23899 Vinhibit_redisplay = Qnil;
23901 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23902 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23903 Vglobal_mode_string = Qnil;
23905 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23906 doc: /* Marker for where to display an arrow on top of the buffer text.
23907 This must be the beginning of a line in order to work.
23908 See also `overlay-arrow-string'. */);
23909 Voverlay_arrow_position = Qnil;
23911 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23912 doc: /* String to display as an arrow in non-window frames.
23913 See also `overlay-arrow-position'. */);
23914 Voverlay_arrow_string = build_string ("=>");
23916 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23917 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23918 The symbols on this list are examined during redisplay to determine
23919 where to display overlay arrows. */);
23920 Voverlay_arrow_variable_list
23921 = Fcons (intern ("overlay-arrow-position"), Qnil);
23923 DEFVAR_INT ("scroll-step", &scroll_step,
23924 doc: /* *The number of lines to try scrolling a window by when point moves out.
23925 If that fails to bring point back on frame, point is centered instead.
23926 If this is zero, point is always centered after it moves off frame.
23927 If you want scrolling to always be a line at a time, you should set
23928 `scroll-conservatively' to a large value rather than set this to 1. */);
23930 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23931 doc: /* *Scroll up to this many lines, to bring point back on screen.
23932 A value of zero means to scroll the text to center point vertically
23933 in the window. */);
23934 scroll_conservatively = 0;
23936 DEFVAR_INT ("scroll-margin", &scroll_margin,
23937 doc: /* *Number of lines of margin at the top and bottom of a window.
23938 Recenter the window whenever point gets within this many lines
23939 of the top or bottom of the window. */);
23940 scroll_margin = 0;
23942 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23943 doc: /* Pixels per inch value for non-window system displays.
23944 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23945 Vdisplay_pixels_per_inch = make_float (72.0);
23947 #if GLYPH_DEBUG
23948 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23949 #endif
23951 DEFVAR_BOOL ("truncate-partial-width-windows",
23952 &truncate_partial_width_windows,
23953 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23954 truncate_partial_width_windows = 1;
23956 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23957 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23958 Any other value means to use the appropriate face, `mode-line',
23959 `header-line', or `menu' respectively. */);
23960 mode_line_inverse_video = 1;
23962 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23963 doc: /* *Maximum buffer size for which line number should be displayed.
23964 If the buffer is bigger than this, the line number does not appear
23965 in the mode line. A value of nil means no limit. */);
23966 Vline_number_display_limit = Qnil;
23968 DEFVAR_INT ("line-number-display-limit-width",
23969 &line_number_display_limit_width,
23970 doc: /* *Maximum line width (in characters) for line number display.
23971 If the average length of the lines near point is bigger than this, then the
23972 line number may be omitted from the mode line. */);
23973 line_number_display_limit_width = 200;
23975 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23976 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23977 highlight_nonselected_windows = 0;
23979 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23980 doc: /* Non-nil if more than one frame is visible on this display.
23981 Minibuffer-only frames don't count, but iconified frames do.
23982 This variable is not guaranteed to be accurate except while processing
23983 `frame-title-format' and `icon-title-format'. */);
23985 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23986 doc: /* Template for displaying the title bar of visible frames.
23987 \(Assuming the window manager supports this feature.)
23988 This variable has the same structure as `mode-line-format' (which see),
23989 and is used only on frames for which no explicit name has been set
23990 \(see `modify-frame-parameters'). */);
23992 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23993 doc: /* Template for displaying the title bar of an iconified frame.
23994 \(Assuming the window manager supports this feature.)
23995 This variable has the same structure as `mode-line-format' (which see),
23996 and is used only on frames for which no explicit name has been set
23997 \(see `modify-frame-parameters'). */);
23998 Vicon_title_format
23999 = Vframe_title_format
24000 = Fcons (intern ("multiple-frames"),
24001 Fcons (build_string ("%b"),
24002 Fcons (Fcons (empty_string,
24003 Fcons (intern ("invocation-name"),
24004 Fcons (build_string ("@"),
24005 Fcons (intern ("system-name"),
24006 Qnil)))),
24007 Qnil)));
24009 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24010 doc: /* Maximum number of lines to keep in the message log buffer.
24011 If nil, disable message logging. If t, log messages but don't truncate
24012 the buffer when it becomes large. */);
24013 Vmessage_log_max = make_number (50);
24015 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24016 doc: /* Functions called before redisplay, if window sizes have changed.
24017 The value should be a list of functions that take one argument.
24018 Just before redisplay, for each frame, if any of its windows have changed
24019 size since the last redisplay, or have been split or deleted,
24020 all the functions in the list are called, with the frame as argument. */);
24021 Vwindow_size_change_functions = Qnil;
24023 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24024 doc: /* List of functions to call before redisplaying a window with scrolling.
24025 Each function is called with two arguments, the window
24026 and its new display-start position. Note that the value of `window-end'
24027 is not valid when these functions are called. */);
24028 Vwindow_scroll_functions = Qnil;
24030 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24031 doc: /* Functions called when redisplay of a window reaches the end trigger.
24032 Each function is called with two arguments, the window and the end trigger value.
24033 See `set-window-redisplay-end-trigger'. */);
24034 Vredisplay_end_trigger_functions = Qnil;
24036 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24037 doc: /* *Non-nil means autoselect window with mouse pointer.
24038 If nil, do not autoselect windows.
24039 A positive number means delay autoselection by that many seconds: a
24040 window is autoselected only after the mouse has remained in that
24041 window for the duration of the delay.
24042 A negative number has a similar effect, but causes windows to be
24043 autoselected only after the mouse has stopped moving. \(Because of
24044 the way Emacs compares mouse events, you will occasionally wait twice
24045 that time before the window gets selected.\)
24046 Any other value means to autoselect window instantaneously when the
24047 mouse pointer enters it.
24049 Autoselection selects the minibuffer only if it is active, and never
24050 unselects the minibuffer if it is active. */);
24051 Vmouse_autoselect_window = Qnil;
24053 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
24054 doc: /* *Non-nil means automatically resize tool-bars.
24055 This increases a tool-bar's height if not all tool-bar items are visible.
24056 It decreases a tool-bar's height when it would display blank lines
24057 otherwise. */);
24058 auto_resize_tool_bars_p = 1;
24060 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24061 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24062 auto_raise_tool_bar_buttons_p = 1;
24064 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24065 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24066 make_cursor_line_fully_visible_p = 1;
24068 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24069 doc: /* *Border below tool-bar in pixels.
24070 If an integer, use it as the height of the border.
24071 If it is one of `internal-border-width' or `border-width', use the
24072 value of the corresponding frame parameter.
24073 Otherwise, no border is added below the tool-bar. */);
24074 Vtool_bar_border = Qinternal_border_width;
24076 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24077 doc: /* *Margin around tool-bar buttons in pixels.
24078 If an integer, use that for both horizontal and vertical margins.
24079 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24080 HORZ specifying the horizontal margin, and VERT specifying the
24081 vertical margin. */);
24082 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24084 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24085 doc: /* *Relief thickness of tool-bar buttons. */);
24086 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24088 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24089 doc: /* List of functions to call to fontify regions of text.
24090 Each function is called with one argument POS. Functions must
24091 fontify a region starting at POS in the current buffer, and give
24092 fontified regions the property `fontified'. */);
24093 Vfontification_functions = Qnil;
24094 Fmake_variable_buffer_local (Qfontification_functions);
24096 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24097 &unibyte_display_via_language_environment,
24098 doc: /* *Non-nil means display unibyte text according to language environment.
24099 Specifically this means that unibyte non-ASCII characters
24100 are displayed by converting them to the equivalent multibyte characters
24101 according to the current language environment. As a result, they are
24102 displayed according to the current fontset. */);
24103 unibyte_display_via_language_environment = 0;
24105 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24106 doc: /* *Maximum height for resizing mini-windows.
24107 If a float, it specifies a fraction of the mini-window frame's height.
24108 If an integer, it specifies a number of lines. */);
24109 Vmax_mini_window_height = make_float (0.25);
24111 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24112 doc: /* *How to resize mini-windows.
24113 A value of nil means don't automatically resize mini-windows.
24114 A value of t means resize them to fit the text displayed in them.
24115 A value of `grow-only', the default, means let mini-windows grow
24116 only, until their display becomes empty, at which point the windows
24117 go back to their normal size. */);
24118 Vresize_mini_windows = Qgrow_only;
24120 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24121 doc: /* Alist specifying how to blink the cursor off.
24122 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24123 `cursor-type' frame-parameter or variable equals ON-STATE,
24124 comparing using `equal', Emacs uses OFF-STATE to specify
24125 how to blink it off. ON-STATE and OFF-STATE are values for
24126 the `cursor-type' frame parameter.
24128 If a frame's ON-STATE has no entry in this list,
24129 the frame's other specifications determine how to blink the cursor off. */);
24130 Vblink_cursor_alist = Qnil;
24132 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24133 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24134 automatic_hscrolling_p = 1;
24136 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24137 doc: /* *How many columns away from the window edge point is allowed to get
24138 before automatic hscrolling will horizontally scroll the window. */);
24139 hscroll_margin = 5;
24141 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24142 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24143 When point is less than `hscroll-margin' columns from the window
24144 edge, automatic hscrolling will scroll the window by the amount of columns
24145 determined by this variable. If its value is a positive integer, scroll that
24146 many columns. If it's a positive floating-point number, it specifies the
24147 fraction of the window's width to scroll. If it's nil or zero, point will be
24148 centered horizontally after the scroll. Any other value, including negative
24149 numbers, are treated as if the value were zero.
24151 Automatic hscrolling always moves point outside the scroll margin, so if
24152 point was more than scroll step columns inside the margin, the window will
24153 scroll more than the value given by the scroll step.
24155 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24156 and `scroll-right' overrides this variable's effect. */);
24157 Vhscroll_step = make_number (0);
24159 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24160 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24161 Bind this around calls to `message' to let it take effect. */);
24162 message_truncate_lines = 0;
24164 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24165 doc: /* Normal hook run to update the menu bar definitions.
24166 Redisplay runs this hook before it redisplays the menu bar.
24167 This is used to update submenus such as Buffers,
24168 whose contents depend on various data. */);
24169 Vmenu_bar_update_hook = Qnil;
24171 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24172 doc: /* Frame for which we are updating a menu.
24173 The enable predicate for a menu binding should check this variable. */);
24174 Vmenu_updating_frame = Qnil;
24176 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24177 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24178 inhibit_menubar_update = 0;
24180 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24181 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24182 inhibit_eval_during_redisplay = 0;
24184 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24185 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24186 inhibit_free_realized_faces = 0;
24188 #if GLYPH_DEBUG
24189 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24190 doc: /* Inhibit try_window_id display optimization. */);
24191 inhibit_try_window_id = 0;
24193 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24194 doc: /* Inhibit try_window_reusing display optimization. */);
24195 inhibit_try_window_reusing = 0;
24197 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24198 doc: /* Inhibit try_cursor_movement display optimization. */);
24199 inhibit_try_cursor_movement = 0;
24200 #endif /* GLYPH_DEBUG */
24202 DEFVAR_INT ("overline-margin", &overline_margin,
24203 doc: /* *Space between overline and text, in pixels.
24204 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24205 margin to the caracter height. */);
24206 overline_margin = 2;
24210 /* Initialize this module when Emacs starts. */
24212 void
24213 init_xdisp ()
24215 Lisp_Object root_window;
24216 struct window *mini_w;
24218 current_header_line_height = current_mode_line_height = -1;
24220 CHARPOS (this_line_start_pos) = 0;
24222 mini_w = XWINDOW (minibuf_window);
24223 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24225 if (!noninteractive)
24227 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24228 int i;
24230 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24231 set_window_height (root_window,
24232 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24234 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24235 set_window_height (minibuf_window, 1, 0);
24237 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24238 mini_w->total_cols = make_number (FRAME_COLS (f));
24240 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24241 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24242 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24244 /* The default ellipsis glyphs `...'. */
24245 for (i = 0; i < 3; ++i)
24246 default_invis_vector[i] = make_number ('.');
24250 /* Allocate the buffer for frame titles.
24251 Also used for `format-mode-line'. */
24252 int size = 100;
24253 mode_line_noprop_buf = (char *) xmalloc (size);
24254 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24255 mode_line_noprop_ptr = mode_line_noprop_buf;
24256 mode_line_target = MODE_LINE_DISPLAY;
24259 help_echo_showing_p = 0;
24263 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24264 (do not change this comment) */